From e5d833027f7fcdef4d77d26b31b5907873331d0e Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Tue, 16 Jun 2020 10:23:30 +0200 Subject: Add msghdr and iovec to task buffers --- announce.c | 13 ++++++------- main.h | 2 ++ server.c | 5 ----- server.h | 4 +--- uring.c | 22 ++++++++++++++++++++-- uring.h | 3 +-- 6 files changed, 30 insertions(+), 19 deletions(-) diff --git a/announce.c b/announce.c index 7f69e51..3edee1b 100644 --- a/announce.c +++ b/announce.c @@ -39,20 +39,18 @@ mcast_send(struct cfg *cfg, struct announce *aev, struct server *server) if (!server || !server->pretty_name || server->announce_port < 1) return; - len = snprintf(server->mcast_buf, sizeof(server->mcast_buf), + len = snprintf(server->mcast_buf.buf, sizeof(server->mcast_buf.buf), "[MOTD]%s[/MOTD][AD]%" PRIu16 "[/AD]", server->pretty_name, server->announce_port); - if (len < 1 || len >= sizeof(server->mcast_buf)) { + if (len < 1 || len >= sizeof(server->mcast_buf.buf)) { error("%s: snprintf returned %i\n", __func__, len); return; } - server->mcast_msg.msg_name = &aev->mcast_task.addr; - server->mcast_msg.msg_namelen = sizeof(aev->mcast_task.addr); - server->mcast_iov.iov_len = len; - - uring_sendmsg(cfg, &aev->mcast_task, &server->mcast_msg, mcast_sent); + server->mcast_buf.len = len; + uring_task_set_buf(&aev->mcast_task, &server->mcast_buf); + uring_tbuf_sendmsg(cfg, &aev->mcast_task, mcast_sent); } static void @@ -182,6 +180,7 @@ announce_init(struct cfg *cfg) aev->mcast_task.addr.in4.sin_family = AF_INET; aev->mcast_task.addr.in4.sin_addr.s_addr = inet_addr("224.0.2.60"); aev->mcast_task.addr.in4.sin_port = htons(4445); + aev->mcast_task.addr.addrlen = sizeof(aev->mcast_task.addr.in4); cfg->aev = aev; uring_read(cfg, &aev->task, &aev->value, sizeof(aev->value), announce_cb); diff --git a/main.h b/main.h index d2efe9f..23be852 100644 --- a/main.h +++ b/main.h @@ -31,6 +31,8 @@ struct uring_task_buf { char buf[4096]; size_t len; size_t done; + struct iovec iov; + struct msghdr msg; }; struct uring_task { diff --git a/server.c b/server.c index aa865ac..cdd8202 100644 --- a/server.c +++ b/server.c @@ -741,11 +741,6 @@ server_new(struct cfg *cfg, const char *name) list_init(&scfg->locals); list_init(&scfg->proxys); list_init(&scfg->rcons); - memset(&scfg->mcast_iov, 0, sizeof(scfg->mcast_iov)); - scfg->mcast_iov.iov_base = scfg->mcast_buf; - memset(&scfg->mcast_msg, 0, sizeof(scfg->mcast_msg)); - scfg->mcast_msg.msg_iov = &scfg->mcast_iov; - scfg->mcast_msg.msg_iovlen = 1; scfg->idle_timeout = 0; list_add(&scfg->list, &cfg->servers); diff --git a/server.h b/server.h index fbaa3fa..0a4eb9d 100644 --- a/server.h +++ b/server.h @@ -48,9 +48,7 @@ struct server { char *rcon_password; /* For announce messages */ - struct iovec mcast_iov; - struct msghdr mcast_msg; - char mcast_buf[4096]; + struct uring_task_buf mcast_buf; /* For checking idle status */ struct idle *idle; diff --git a/uring.c b/uring.c index 4c524c7..6c16849 100644 --- a/uring.c +++ b/uring.c @@ -159,6 +159,16 @@ uring_task_get(struct cfg *cfg, struct uring_task *task) void uring_task_set_buf(struct uring_task *task, struct uring_task_buf *tbuf) { + if (tbuf) { + /* iov_len and msg_namelen are set at send/receive time */ + tbuf->iov.iov_base = tbuf->buf; + tbuf->msg.msg_name = &task->addr.storage; + tbuf->msg.msg_iov = &tbuf->iov; + tbuf->msg.msg_iovlen = 1; + tbuf->msg.msg_control = NULL; + tbuf->msg.msg_controllen = 0; + tbuf->msg.msg_flags = 0; + } task->tbuf = tbuf; } @@ -419,13 +429,21 @@ uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, callbac } void -uring_sendmsg(struct cfg *cfg, struct uring_task *task, struct msghdr *msg, callback_t callback) +uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, callback_t callback) { struct io_uring_sqe *sqe; + if (!task->tbuf) { + error("%s: called with no tbuf set\n", __func__); + return; + } + sqe = get_sqe(cfg, task); + task->tbuf->done = 0; + task->tbuf->iov.iov_len = task->tbuf->len; + task->tbuf->msg.msg_namelen = task->addr.addrlen; task->callback = callback; - io_uring_prep_sendmsg(sqe, task->fd, msg, 0); + io_uring_prep_sendmsg(sqe, task->fd, &task->tbuf->msg, 0); io_uring_sqe_set_data(sqe, task); } diff --git a/uring.h b/uring.h index 680684a..b4160d8 100644 --- a/uring.h +++ b/uring.h @@ -51,8 +51,7 @@ uring_read(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, call void uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, callback_t callback); -void uring_sendmsg(struct cfg *cfg, struct uring_task *task, struct msghdr *msg, - callback_t callback); +void uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, callback_t callback); void uring_connect(struct cfg *cfg, struct uring_task *task, struct sockaddr_in46 *addr, callback_t callback); -- cgit v1.2.3