summaryrefslogtreecommitdiff
path: root/lib9p/9p.c
blob: 0e710715a173e09b98f5c22a2d0fc187fed3231f (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
90
91
92
93
94
95
96
/* lib9p/9p.c - Base 9P protocol utilities for both clients and servers
 *
 * 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 <lib9p/9p.h>

#include "internal.h"

int lib9p_error(struct lib9p_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 lib9p_errorf(struct lib9p_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;
}

ssize_t lib9p_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes) {
	/* Inspect the first 5 bytes ourselves.  */
	struct _validate_ctx subctx = {
		.ctx        = ctx,
		.net_size   = decode_u32le(net_bytes),
		.net_bytes  = net_bytes,

		.net_offset = 0,
		.host_extra = 0,
	};
	if (subctx.net_size < 5)
		return lib9p_error(ctx, LINUX_EBADMSG, "message is impossibly short");
	uint8_t typ = net_bytes[4];
	struct _vtable_msg vtable = _lib9p_vtables[ctx->version].msgs[typ];
	if (!vtable.validate)
		return lib9p_errorf(ctx, LINUX_EOPNOTSUPP, "unknown message type: %s (protocol_version=%s)",
		                    lib9p_msg_type_str(typ), lib9p_version_str(ctx->version));

	/* Now use the message-type-specific vtable to process the whole thing.  */
	if (vtable.validate(&subctx))
		return -1;
	assert(subctx.net_offset <= subctx.net_size);
	if (subctx.net_offset < subctx.net_size)
		return lib9p_errorf(ctx, LINUX_EBADMSG, "message has %"PRIu32" extra bytes",
		                   subctx.net_size - subctx.net_offset);

	/* Return.  */
	ssize_t ret;
	if (__builtin_add_overflow(vtable.basesize, subctx.host_extra, &ret))
		return lib9p_error(ctx, LINUX_EMSGSIZE, "unmarshalled payload overflows SSIZE_MAX");
	return ret;
}

void lib9p_unmarshal(struct lib9p_ctx *ctx, uint8_t *net_bytes,
                     enum lib9p_msg_type *ret_typ, void *ret_body) {
	struct _unmarshal_ctx subctx = {
		.ctx        = ctx,
		.net_bytes  = net_bytes,

		.net_offset = 0,
	};

	*ret_typ = net_bytes[4];
	struct _vtable_msg vtable = _lib9p_vtables[ctx->version].msgs[*ret_typ];
	subctx.extra = ret_body + vtable.basesize;
	vtable.unmarshal(&subctx, ret_body);
}

bool lib9p_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body,
                   uint8_t *ret_bytes) {
	struct _marshal_ctx subctx = {
		.ctx        = ctx,
		.net_bytes  = ret_bytes,
		.net_offset = 0,
	};

	struct _vtable_msg vtable = _lib9p_vtables[ctx->version].msgs[typ];
	return vtable.marshal(&subctx, body);
}