summaryrefslogtreecommitdiff
path: root/lib9p/9p.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib9p/9p.c')
-rw-r--r--lib9p/9p.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/lib9p/9p.c b/lib9p/9p.c
new file mode 100644
index 0000000..64b09a6
--- /dev/null
+++ b/lib9p/9p.c
@@ -0,0 +1,94 @@
+/* lib9p/9p.c - Base 9P protocol utilities for both clients and servers
+ *
+ * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-Licence-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"
+
+enum lib9p_version lib9p_ctx_version(lib9p_ctx *ctx) {
+ assert(ctx);
+ return ctx->version;
+}
+
+uint32_t lib9p_ctx_max_msg_size(lib9p_ctx *ctx) {
+ assert(ctx);
+ return ctx->max_msg_size;
+}
+
+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;
+}
+
+size_t lib9p_unmarshal_size(struct lib9p_ctx *ctx, uint8_t *net_bytes) {
+ /* Header */
+ uint32_t net_len = decode_u32le(net_bytes);
+ if (net_len < 7)
+ return lib9p_error(ctx, LINUX_EBADMSG, "message is too short");
+ uint8_t typ = net_bytes[4];
+ uint32_t net_offset = 7;
+
+ /* Body */
+ if (!versions[ctx->version]->msgs[typ].unmarshal_extrasize)
+ return lib9p_errorf(ctx, LINUX_EOPNOTSUPP, "unknown message type %"PRIu8, typ);
+ size_t host_size = versions[ctx->version]->msgs[typ].unmarshal_basesize;
+ if (versions[ctx->version]->msgs[typ].unmarshal_extrasize(net_len, net_bytes, &net_offset, &host_size))
+ return lib9p_error(ctx, LINUX_EBADMSG, "message is too short for content");
+
+ return host_size;
+}
+
+uint8_t lib9p_unmarshal(struct lib9p_ctx *ctx, uint8_t *net_bytes, uint16_t *out_tag, void *out_body) {
+ /* Header */
+ uint8_t typ = net_bytes[4];
+ *out_tag = decode_u16le(&net_bytes[5]);
+ uint32_t net_offset = 7;
+
+ /* Body */
+ void *host_extra = out_body + versions[ctx->version]->msgs[typ].unmarshal_basesize;
+ if (versions[ctx->version]->msgs[typ].unmarshal(net_bytes, &net_offset, &host_extra, out_body))
+ return lib9p_error(ctx, LINUX_EBADMSG, "message contains invalid UTF-8");
+
+ return typ;
+}
+
+uint32_t lib9p_marshal(struct lib9p_ctx *ctx, uint8_t typ, uint16_t msgid, void *body, uint8_t *out_bytes) {
+ /* Header */
+ out_bytes[4] = typ;
+ encode_u16le(msgid, &out_bytes[5]);
+ uint32_t net_offset = 7;
+
+ /* Body */
+ if (versions[ctx->version]->msgs[typ].marshal(ctx, body, out_bytes, &net_offset))
+ return 0;
+
+ /* Header, again */
+ encode_u32le(net_offset, out_bytes);
+
+ return net_offset;
+}