summaryrefslogtreecommitdiff
path: root/lib9p/9p.c
blob: 64b09a66e98d4cf31742ee1d366190e73b69625b (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
/* 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;
}