summaryrefslogtreecommitdiff
path: root/lib9p/9p.c
blob: 9f0bd4c7341c73722fff5457de59d0d9d5a5e890 (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
/* lib9p/9p.c - Base 9P protocol utilities for both clients and servers
 *
 * Copyright (C) 2024  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() */

#include <lib9p/9p.h>

#include "internal.h"

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';

#ifdef 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));

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

	return -1;
}


const char *lib9p_msg_type_str(struct lib9p_ctx *ctx, enum lib9p_msg_type typ) {
    assert(0 <= typ && typ <= 0xFF);
    return _lib9p_versions[ctx->version].msgs[typ].name;
}

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 _table_msg table = _lib9p_versions[ctx->version].msgs[typ];
	if (!table.validate)
		return lib9p_errorf(ctx, LINUX_EOPNOTSUPP, "unknown message type: %s (protocol_version=%s)",
		                    lib9p_msg_type_str(ctx, typ), lib9p_version_str(ctx->version));

	/* Now use the message-type-specific table to process the whole thing.  */
	if (table.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(table.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 _table_msg table = _lib9p_versions[ctx->version].msgs[*ret_typ];
	subctx.extra = ret_body + table.basesize;
	table.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 _table_msg table = _lib9p_versions[ctx->version].msgs[typ];
	return table.marshal(&subctx, body);
}

bool lib9p_validate_stat(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_validate_stat(&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_unmarshal_stat(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_unmarshal_stat(&subctx, ret_obj);
	return subctx.net_offset;
}

uint32_t lib9p_marshal_stat(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_marshal_stat(&subctx, obj))
		return 0;
	return subctx.net_offset;
}