summaryrefslogtreecommitdiff
path: root/lib9p/9p.c
blob: 51ff2ebb5383b77c6f46d00c94071d943c5e23f1 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* lib9p/9p.c - Base 9P protocol utilities for both clients and servers
 *
 * Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-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() */

#define LOG_NAME 9P
#include <libmisc/log.h> /* for const_byte_str() */

#include <lib9p/9p.h>

#include "internal.h"

/* strings ********************************************************************/

const char *lib9p_version_str(enum lib9p_version ver) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
	assert(0 <= ver && ver < LIB9P_VER_NUM);
#pragma GCC diagnostic pop
	return _lib9p_table_ver_name[ver];
}

const char *lib9p_msgtype_str(enum lib9p_version ver, enum lib9p_msg_type typ) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
	assert(0 <= ver && ver < LIB9P_VER_NUM);
	assert(0 <= typ && typ <= 0xFF);
#pragma GCC diagnostic pop
	return _lib9p_table_msg_name[ver][typ] ?: const_byte_str(typ);
}

struct lib9p_s lib9p_str(char *s) {
	if (!s)
		return (struct lib9p_s){0};
	return (struct lib9p_s){
		.len = strlen(s),
		.utf8 = s,
	};
}
struct lib9p_s lib9p_strn(char *s, size_t maxlen) {
	if (maxlen == 0 || !s)
		return (struct lib9p_s){0};
	return (struct lib9p_s){
		.len = strnlen(s, maxlen),
		.utf8 = s,
	};
}
struct lib9p_s lib9p_str_slice(struct lib9p_s s, uint16_t beg, uint16_t end) {
	assert(s.len == 0 || s.utf8);
	assert(beg <= end && end <= s.len);
	return (struct lib9p_s){
		.len = end - beg,
		.utf8 = &s.utf8[beg],
	};
}
bool lib9p_str_eq(struct lib9p_s a, struct lib9p_s b) {
	return a.len == b.len &&
		(a.len == 0 || memcmp(a.utf8, b.utf8, a.len) == 0);
}

/* ctx ************************************************************************/

void lib9p_ctx_clear_error(struct lib9p_ctx *ctx) {
	assert(ctx);
#if CONFIG_9P_ENABLE_9P2000_u
	ctx->err_num = 0;
#endif
	ctx->err_msg[0] = '\0';
}

bool lib9p_ctx_has_error(struct lib9p_ctx *ctx) {
	assert(ctx);
	return ctx->err_msg[0];
}

int lib9p_error(struct lib9p_ctx *ctx, uint32_t linux_errno, char const *msg) {
	if (lib9p_ctx_has_error(ctx))
		return -1;
	strncpy(ctx->err_msg, msg, sizeof(ctx->err_msg));
	ctx->err_msg[sizeof(ctx->err_msg)-1] = '\0';

#if CONFIG_9P_ENABLE_9P2000_u
	ctx->err_num = linux_errno;
#else
	(void)(linux_errno);
#endif

	return -1;
}

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

	if (lib9p_ctx_has_error(ctx))
		return -1;
	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));

#if CONFIG_9P_ENABLE_9P2000_u
	ctx->err_num = linux_errno;
#else
	(void)(linux_errno);
#endif

	return -1;
}

/* main message functions *****************************************************/

static
ssize_t _lib9p_validate(uint8_t                          xxx_low_typ_bit,
                        const char                      *xxx_errmsg,
                        const struct _lib9p_recv_tentry  xxx_table[LIB9P_VER_NUM][0x80],
                        struct lib9p_ctx *ctx, uint8_t *net_bytes) {
	/* Inspect the first 5 bytes ourselves.  */
	struct _validate_ctx subctx = {
		.ctx        = ctx,
		.net_size   = uint32le_decode(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];
	if (typ % 2 != xxx_low_typ_bit)
		return lib9p_errorf(ctx, LINUX_EOPNOTSUPP, "%s: message_type=%s", xxx_errmsg,
		                    lib9p_msgtype_str(ctx->version, typ));
	struct _lib9p_recv_tentry tentry = xxx_table[ctx->version][typ/2];
	if (!tentry.validate)
		return lib9p_errorf(ctx, LINUX_EOPNOTSUPP, "unknown message type: %s (protocol_version=%s)",
		                    lib9p_msgtype_str(ctx->version, typ), lib9p_version_str(ctx->version));

	/* Now use the message-type-specific tentry to process the whole thing.  */
	if (tentry.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(tentry.basesize, subctx.host_extra, &ret))
		return lib9p_error(ctx, LINUX_EMSGSIZE, "unmarshalled payload overflows SSIZE_MAX");
	return ret;
}

ssize_t lib9p_Tmsg_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes) {
	return _lib9p_validate(0, "expected a T-message but got an R-message", _lib9p_table_Tmsg_recv,
	                       ctx, net_bytes);
}

ssize_t lib9p_Rmsg_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes) {
	return _lib9p_validate(1, "expected an R-message but got a T-message", _lib9p_table_Rmsg_recv,
	                       ctx, net_bytes);
}

static
void _lib9p_unmarshal(const struct _lib9p_recv_tentry xxx_table[LIB9P_VER_NUM][0x80],
                      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,
	};

	enum lib9p_msg_type typ = net_bytes[4];
	*ret_typ = typ;
	struct _lib9p_recv_tentry tentry = xxx_table[ctx->version][typ/2];
	subctx.extra = ret_body + tentry.basesize;
	tentry.unmarshal(&subctx, ret_body);
}

void lib9p_Tmsg_unmarshal(struct lib9p_ctx *ctx, uint8_t *net_bytes,
                          enum lib9p_msg_type *ret_typ, void *ret_body) {
	_lib9p_unmarshal(_lib9p_table_Tmsg_recv,
	                 ctx, net_bytes, ret_typ, ret_body);
}

void lib9p_Rmsg_unmarshal(struct lib9p_ctx *ctx, uint8_t *net_bytes,
                          enum lib9p_msg_type *ret_typ, void *ret_body) {
	_lib9p_unmarshal(_lib9p_table_Rmsg_recv,
	                 ctx, net_bytes, ret_typ, ret_body);
}

static
bool _lib9p_marshal(const struct _lib9p_send_tentry xxx_table[LIB9P_VER_NUM][0x80],
                    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 _lib9p_send_tentry tentry = xxx_table[ctx->version][typ/2];
	return tentry.marshal(&subctx, body);
}

bool lib9p_Tmsg_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body,
                        uint8_t *ret_bytes) {
	assert(typ % 2 == 0);
	return _lib9p_marshal(_lib9p_table_Tmsg_send, ctx, typ, body, ret_bytes);
}

bool lib9p_Rmsg_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body,
                        uint8_t *ret_bytes) {
	assert(typ % 2 == 1);
	return _lib9p_marshal(_lib9p_table_Rmsg_send, ctx, typ, body, ret_bytes);
}

/* `struct lib9p_stat` helpers ************************************************/

bool lib9p_stat_validate(struct lib9p_ctx *ctx, uint32_t net_size, uint8_t *net_bytes,
                         uint32_t *ret_net_size, ssize_t *ret_host_size) {
	struct _validate_ctx subctx = {
		.ctx        = ctx,
		.net_size   = net_size,
		.net_bytes  = net_bytes,

		.net_offset = 0,
		.host_extra = 0,
	};
	if (_lib9p_stat_validate(&subctx))
		return true;
	if (ret_net_size)
		*ret_net_size = subctx.net_offset;
	if (ret_host_size)
		if (__builtin_add_overflow(sizeof(struct lib9p_stat), subctx.host_extra, ret_host_size))
			return lib9p_error(ctx, LINUX_EMSGSIZE, "unmarshalled stat object overflows SSIZE_MAX");
	return false;
}

uint32_t lib9p_stat_unmarshal(struct lib9p_ctx *ctx, uint8_t *net_bytes,
                              struct lib9p_stat *ret_obj, void *ret_extra) {
	struct _unmarshal_ctx subctx = {
		.ctx = ctx,
		.net_bytes = net_bytes,
		.net_offset = 0,

		.extra = ret_extra,
	};
	_lib9p_stat_unmarshal(&subctx, ret_obj);
	return subctx.net_offset;
}

uint32_t lib9p_stat_marshal(struct lib9p_ctx *ctx, uint32_t max_net_size, struct lib9p_stat *obj,
                            uint8_t *ret_bytes) {
	struct lib9p_ctx _ctx = *ctx;
	_ctx.max_msg_size = max_net_size;
	struct _marshal_ctx subctx = {
		.ctx        = &_ctx,
		.net_bytes  = ret_bytes,
		.net_offset = 0,
	};
	if (_lib9p_stat_marshal(&subctx, obj))
		return 0;
	return subctx.net_offset;
}