summaryrefslogtreecommitdiff
path: root/lib9p/9p.c
blob: af1c6ca6fed6e348c22798215f0f8fcb12cae067 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* 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"

enum lib9p_version lib9p_ctx_version(struct lib9p_ctx *ctx) {
	assert(ctx);
	return ctx->version;
}

uint32_t lib9p_ctx_max_msg_size(struct lib9p_ctx *ctx) {
	assert(ctx);
	return ctx->max_msg_size;
}

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) {
	/* Header */
	struct _validate_ctx subctx = {
		.ctx        = ctx,
		.net_size   = decode_u32le(net_bytes),
		.net_bytes  = net_bytes,

		.net_offset = 7,
		.host_extra = 0,
	};
	if (subctx.net_size < subctx.net_offset)
		return lib9p_error(ctx, LINUX_EBADMSG, "message is too short");
	uint8_t typ = net_bytes[4];

	/* Body */
	struct _vtable_msg vtable = _lib9p_vtables[ctx->version].msgs[typ];
	if (!vtable.validate)
		return lib9p_errorf(ctx, LINUX_EOPNOTSUPP, "unknown message type %s",
		                    lib9p_msg_type_str(typ));
	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);

	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, uint16_t *ret_tag, void *ret_body) {
	/* Header */
	struct _unmarshal_ctx subctx = {
		.ctx        = ctx,
		.net_bytes  = net_bytes,

		.net_offset = 7,
	};
	*ret_typ = net_bytes[4];
	*ret_tag = decode_u16le(&net_bytes[5]);

	/* Body */
	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, uint16_t tag, void *body,
                   uint8_t *ret_bytes) {
	/* Header */
	struct _marshal_ctx subctx = {
		.ctx        = ctx,
		.net_bytes  = ret_bytes,
		.net_offset = 7,
	};
	ret_bytes[4] = typ;
	encode_u16le(tag, &ret_bytes[5]);

	/* Body */
	struct _vtable_msg vtable = _lib9p_vtables[ctx->version].msgs[typ];
	if (vtable.marshal(&subctx, body))
		return true;

	/* Header, again */
	encode_u32le(subctx.net_offset, ret_bytes);

	return false;
}