summaryrefslogtreecommitdiff
path: root/lib9p/defs.c
blob: 886a0c1849247bc8bd9232255162a3d0b760d5b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* 9p/defs.c - TODO
 *
 * Copyright (C) 2024  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-Licence-Identifier: AGPL-3.0-or-later
 */

#include <inttypes.h> /* for PRIu{n} */
#include <stdarg.h>   /* for va_* */
#include <stdio.h>    /* for vsnprintf() */
#include <string.h>   /* for strncpy() */

#include "9p/defs.h"
#include "9p/linux-errno.h"
#include "9p/internal.h"

static struct version *versions[_P9_VER_CNT] = {
	[P9_VER_9P2000] = &version_9P2000,
	/* [P9_VER_9P2000u] = &version_9P2000u, */
};

int p9_error(struct p9_ctx *ctx, uint32_t linux_errno, char const *msg) {
	strncpy(ctx->err_msg, msg, sizeof(ctx->err_msg));
	ctx->err_msg[sizeof(ctx->err_msg)-1] = '\0';
	ctx->err_num = linux_errno;
	return -1;
}

int p9_errorf(struct p9_ctx *ctx, uint32_t linux_errno, char const *fmt, ...) {
	int n;
	va_list args;

	va_start(args, fmt);
	n = vsnprintf(ctx->err_msg, sizeof(ctx->err_msg), fmt, args);
	va_end(args);
	if ((size_t)(n+1) < sizeof(ctx->err_msg))
		memset(&ctx->err_msg[n+1], 0, sizeof(ctx->err_msg)-(n+1));

	ctx->err_num = linux_errno;

	return -1;
}

size_t p9_unmarshal_size(struct p9_ctx *ctx, uint8_t *net_bytes) {
	/* Header */
	uint32_t net_len = decode_u32le(net_bytes);
	if (net_len < 7)
		return p9_error(ctx, LINUX_EBADMSG, "message is too short");
	uint8_t typ = net_bytes[4];
	uint32_t net_offset = 7;

	/* Body */
	if (!versions[ctx->version]->msgs[typ].unmarshal_extrasize)
		return p9_errorf(ctx, LINUX_EOPNOTSUPP, "unknown message type %"PRIu8, typ);
	size_t host_size = versions[ctx->version]->msgs[typ].unmarshal_basesize;
	if (versions[ctx->version]->msgs[typ].unmarshal_extrasize(net_len, net_bytes, &net_offset, &host_size))
		return p9_error(ctx, LINUX_EBADMSG, "message is too short for content");

	return host_size;
}

uint8_t p9_unmarshal(struct p9_ctx *ctx, uint8_t *net_bytes, uint16_t *out_tag, void *out_body) {
	/* Header */
	uint8_t typ = net_bytes[4];
	*out_tag = decode_u16le(&net_bytes[5]);
	uint32_t net_offset = 7;

	/* Body */
	void *host_extra = out_body + versions[ctx->version]->msgs[typ].unmarshal_basesize;
	if (versions[ctx->version]->msgs[typ].unmarshal(net_bytes, &net_offset, &host_extra, out_body))
		return p9_error(ctx, LINUX_EBADMSG, "message contains invalid UTF-8");

	return typ;
}

uint32_t _p9_marshal(struct p9_ctx *ctx, uint8_t typ, uint16_t msgid, void *body, uint8_t *out_bytes) {
	/* Header */
	out_bytes[4] = typ;
	encode_u16le(msgid, &out_bytes[5]);
	uint32_t net_offset = 7;
	
	/* Body */
	if (versions[ctx->version]->msgs[typ].marshal(ctx, body, out_bytes, &net_offset))
		return 0;

	/* Header, again */
	encode_u32le(net_offset, out_bytes);

	return net_offset;
}