diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-10-17 14:31:03 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-10-17 14:31:03 -0600 |
commit | f132dab76a07473d41e14f5f4fb1857a3229ec6a (patch) | |
tree | 621a8f1ae6cb15d360cd47c0bccd08a1c2226f4e /lib9p/9p.c | |
parent | a1fb6a6103cc7d38d54270bcdb9779982d329c9e (diff) |
libmisc
Diffstat (limited to 'lib9p/9p.c')
-rw-r--r-- | lib9p/9p.c | 28 |
1 files changed, 17 insertions, 11 deletions
@@ -49,6 +49,12 @@ int lib9p_errorf(struct lib9p_ctx *ctx, uint32_t linux_errno, char const *fmt, . 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 = { @@ -62,13 +68,13 @@ 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 _vtable_msg vtable = _lib9p_vtables[ctx->version].msgs[typ]; - if (!vtable.validate) + 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(typ), lib9p_version_str(ctx->version)); + lib9p_msg_type_str(ctx, typ), lib9p_version_str(ctx->version)); - /* Now use the message-type-specific vtable to process the whole thing. */ - if (vtable.validate(&subctx)) + /* 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) @@ -77,7 +83,7 @@ ssize_t lib9p_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes) { /* Return. */ ssize_t ret; - if (__builtin_add_overflow(vtable.basesize, subctx.host_extra, &ret)) + if (__builtin_add_overflow(table.basesize, subctx.host_extra, &ret)) return lib9p_error(ctx, LINUX_EMSGSIZE, "unmarshalled payload overflows SSIZE_MAX"); return ret; } @@ -92,9 +98,9 @@ void lib9p_unmarshal(struct lib9p_ctx *ctx, uint8_t *net_bytes, }; *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); + 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, @@ -105,8 +111,8 @@ bool lib9p_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body, .net_offset = 0, }; - struct _vtable_msg vtable = _lib9p_vtables[ctx->version].msgs[typ]; - return vtable.marshal(&subctx, body); + 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, |