/* lib9p/9p.c - Base 9P protocol utilities for both clients and servers * * Copyright (C) 2024 Luke T. Shumaker * SPDX-Licence-Identifier: AGPL-3.0-or-later */ #include /* for PRIu{n} */ #include /* for va_* */ #include /* for vsnprintf() */ #include /* for strncpy() */ #include #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; } bool lib9p_ctx_has_error(struct lib9p_ctx *ctx) { assert(ctx); return ctx->err_num || ctx->err_msg[0]; } 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; }