summaryrefslogtreecommitdiff
path: root/lib9p/9p.c
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-12-16 14:51:10 -0500
committerLuke T. Shumaker <lukeshu@lukeshu.com>2025-01-11 23:34:14 -0700
commitc3b9d7e802d7d2e31131692d621515ac88178ebb (patch)
treeada9990fb897eb95e4fa5dbc11d332326480aab3 /lib9p/9p.c
parentcd64085694c7c4aa96312e88905015eea4d8b63d (diff)
lib9p: Split lib9p_{validate,unmarshal,marshal} into _Tmsg and _Rmsg variants
Diffstat (limited to 'lib9p/9p.c')
-rw-r--r--lib9p/9p.c75
1 files changed, 60 insertions, 15 deletions
diff --git a/lib9p/9p.c b/lib9p/9p.c
index b464f63..8f3ae78 100644
--- a/lib9p/9p.c
+++ b/lib9p/9p.c
@@ -77,7 +77,11 @@ const char *lib9p_msg_type_str(struct lib9p_ctx *ctx, enum lib9p_msg_type typ) {
/* main message functions *****************************************************/
-ssize_t lib9p_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes) {
+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,
@@ -90,13 +94,16 @@ ssize_t lib9p_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes) {
if (subctx.net_size < 5)
return lib9p_error(ctx, LINUX_EBADMSG, "message is impossibly short");
uint8_t typ = net_bytes[4];
- struct _lib9p_recv_tentry table = ((typ % 2 == 0) ? _lib9p_table_Tmsg_recv : _lib9p_table_Rmsg_recv)[ctx->version][typ/2];
- if (!table.validate)
+ if (typ % 2 != xxx_low_typ_bit)
+ return lib9p_errorf(ctx, LINUX_EOPNOTSUPP, "%s: message_type=%s", xxx_errmsg,
+ lib9p_msg_type_str(ctx, 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_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))
+ /* 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)
@@ -105,13 +112,25 @@ ssize_t lib9p_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes) {
/* Return. */
ssize_t ret;
- if (__builtin_add_overflow(table.basesize, subctx.host_extra, &ret))
+ if (__builtin_add_overflow(tentry.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) {
+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,
@@ -121,21 +140,47 @@ void lib9p_unmarshal(struct lib9p_ctx *ctx, uint8_t *net_bytes,
enum lib9p_msg_type typ = net_bytes[4];
*ret_typ = typ;
- struct _lib9p_recv_tentry table = ((typ % 2 == 0) ? _lib9p_table_Tmsg_recv : _lib9p_table_Rmsg_recv)[ctx->version][typ/2];
- subctx.extra = ret_body + table.basesize;
- table.unmarshal(&subctx, ret_body);
+ 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);
}
-bool lib9p_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body,
- uint8_t *ret_bytes) {
+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 table = ((typ % 2 == 0) ? _lib9p_table_Tmsg_send : _lib9p_table_Rmsg_send)[ctx->version][typ/2];
- return table.marshal(&subctx, body);
+ 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 ************************************************/