summaryrefslogtreecommitdiff
path: root/lib9p/srv.c
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-09-27 22:27:01 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-09-27 22:27:01 -0600
commit7ec97df3ee8edfd102fe573eaa61cf4e5c6284cb (patch)
treee696f30da5645cdd3cb09971d9544622e9943bcf /lib9p/srv.c
parentfa357459f88bb8f0170d1a68df66e7d068d59996 (diff)
wip fixes
Diffstat (limited to 'lib9p/srv.c')
-rw-r--r--lib9p/srv.c69
1 files changed, 36 insertions, 33 deletions
diff --git a/lib9p/srv.c b/lib9p/srv.c
index c6558d6..9a68a2b 100644
--- a/lib9p/srv.c
+++ b/lib9p/srv.c
@@ -1,47 +1,50 @@
#include <assert.h>
+#include <stdio.h> /* for fprintf(), stderr */
+#include <string.h> /* for strerror() */
#include <libcr/coroutine.h>
#include <libcr_ipc/chan.h>
#include <libnetio/netio.h>
#include <lib9p/9p.h>
+#include <lib9p/srv.h>
#include "internal.h"
-struct p9_srvconn {
+struct lib9p_srvconn {
/* immutable */
- p9_srv *srv;
- cid_t reader;
- int fd;
+ struct lib9p_srv *srv;
+ cid_t reader;
+ int fd;
/* mutable */
- uint32_t max_msg_size;
- enum p9_version version;
- unsigned int refcount;
+ uint32_t max_msg_size;
+ enum lib9p_version version;
+ unsigned int refcount;
};
-struct p9_srvreq {
- p9_srvconn *conn;
- uint8_t *msg;
+struct lib9p_srvreq {
+ struct lib9p_srvconn *conn;
+ uint8_t *msg;
};
-COROUTINE p9_srv_read_cr(void *_srv) {
+COROUTINE lib9p_srv_read_cr(void *_srv) {
uint8_t buf[CONFIG_9P_MAX_MSG_SIZE];
- p9_srv *srv = _srv;
+ struct lib9p_srv *srv = _srv;
assert(srv);
cr_begin();
for (;;) {
- struct p9_srvconn conn = {
+ struct lib9p_srvconn conn = {
.srv = srv,
- .reader = cr_getcid();
+ .reader = cr_getcid(),
- .max_msg_size = CONFIG_9P_MAX_MSG_SIZE;
- .version = P9_VER_UNINITIALIZED;
+ .max_msg_size = CONFIG_9P_MAX_MSG_SIZE,
+ .version = LIB9P_VER_UNINITIALIZED,
.refcount = 1,
};
conn.fd = netio_accept(srv->sockfd);
if (conn.fd < 0) {
- fprintf(stderr, "error: accept: %m", -conn.fd);
+ fprintf(stderr, "error: accept: %s", strerror(-conn.fd));
continue;
}
@@ -67,14 +70,14 @@ COROUTINE p9_srv_read_cr(void *_srv) {
goto close;
}
if (goal > conn.max_msg_size) {
- struct p9_ctx ctx = {
+ struct lib9p_ctx ctx = {
.version = conn.version,
.max_msg_size = conn.max_msg_size,
};
if (initialized)
- p9_errorf(&ctx, LINUX_EMSGSIZE, "T-message larger than negotiated limit (%zu > %zu)", goal, conn.max_msg_size);
+ lib9p_errorf(&ctx, LINUX_EMSGSIZE, "T-message larger than negotiated limit (%zu > %zu)", goal, conn.max_msg_size);
else
- p9_errorf(&ctx, LINUX_EMSGSIZE, "T-message larger than server limit (%zu > %zu)", goal, conn.max_msg_size);
+ lib9p_errorf(&ctx, LINUX_EMSGSIZE, "T-message larger than server limit (%zu > %zu)", goal, conn.max_msg_size);
marshal_error(&ctx, buf);
netio_write(conn.fd, buf, decode_u32le(buf));
continue;
@@ -93,7 +96,7 @@ COROUTINE p9_srv_read_cr(void *_srv) {
}
/* Handle the message... */
- if (conn.version == P9_VER_UNINITIALIZED) {
+ if (conn.version == LIB9P_VER_UNINITIALIZED) {
/* ...synchronously if we haven't negotiated the protocol yet, ... */
handle_message(&conn, buf);
} else {
@@ -114,15 +117,15 @@ COROUTINE p9_srv_read_cr(void *_srv) {
cr_end();
}
-COROUTINE p9_srv_write_cr(void *_srv) {
+COROUTINE lib9p_srv_write_cr(void *_srv) {
uint8_t net[CONFIG_9P_MAX_MSG_SIZE];
- p9_srv *srv = _srv;
+ lib9p_srv *srv = _srv;
assert(srv);
cr_begin();
for (;;) {
- struct p9_srvreq req;
+ struct lib9p_srvreq req;
cr_chan_recv(&srv->reqch, &req);
memcpy(net, req.msg, decode_u32le(req.msg));
req.conn->refcount++;
@@ -137,28 +140,28 @@ COROUTINE p9_srv_write_cr(void *_srv) {
cr_end();
}
-void handle_message(p9_srvconn *conn, uint8_t *net) {
+void handle_message(lib9p_srvconn *conn, uint8_t *net) {
uint8_t host[CONFIG_9P_MAX_MSG_SIZE];
- struct p9_ctx ctx = {
+ struct lib9p_ctx ctx = {
.version = req.conn->version,
.max_msg_size = req.conn->max_msg_size,
};
- size_t host_size = p9_unmarshal_size(&ctx, net);
+ size_t host_size = lib9p_unmarshal_size(&ctx, net);
if (host_size == (size_t)-1)
goto write;
if (host_size > sizeof(host)) {
- p9_errorf(&ctx, LINUX_EMSGSIZE, "unmarshalled payload larger than server limit (%zu > %zu)", host_size, sizeof(host));
+ lib9p_errorf(&ctx, LINUX_EMSGSIZE, "unmarshalled payload larger than server limit (%zu > %zu)", host_size, sizeof(host));
goto write;
}
uint16_t tag;
- uint8_t typ = p9_unmarshal(&ctx, net, &tag, host);
+ uint8_t typ = lib9p_unmarshal(&ctx, net, &tag, host);
if (typ == (uint8_t)-1)
goto write;
if (typ % 2 != 0) {
- p9_errorf(&ctx, LINUX_EOPNOTSUPP, "expected a T-message but got an R-message");
+ lib9p_errorf(&ctx, LINUX_EOPNOTSUPP, "expected a T-message but got an R-message");
goto write;
}
@@ -179,8 +182,8 @@ static inline uint16_t min_u16(uint16_t a, b) {
/* We have special code for marshaling Rerror because we don't ever
* want to produce an error because the err_msg is too long for the
* `ctx->max_msg_size`! */
-void marshal_error(struct p9_ctx *ctx, uint16_t tag, uint8_t *net) {
- struct p9_msg_Rerror host = {
+void marshal_error(struct lib9p_ctx *ctx, uint16_t tag, uint8_t *net) {
+ struct lib9p_msg_Rerror host = {
.ename = {
.len = strnlen(ctx->err_msg, CONFIG_9P_MAX_ERR_SIZE),
.utf8 = ctx->err_msg,
@@ -188,7 +191,7 @@ void marshal_error(struct p9_ctx *ctx, uint16_t tag, uint8_t *net) {
};
if (host.ename.len + ctx->Rerror_overhead > ctx->max_msg_size)
host.ename.len = ctx->max_msg_size - overhead;
- p9_marshal(ctx, tag, host, net);
+ lib9p_marshal(ctx, tag, host, net);
}
ERANGE for reply too large