From 9daf6a0e6b461c6c2a16f810f722b9d10504bf90 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Wed, 10 Jun 2020 13:28:41 +0200 Subject: Introduce a common task buffer and convert cfgdir and rcon to use it --- rcon.c | 155 ++++++++++++++++++++++------------------------------------------- 1 file changed, 53 insertions(+), 102 deletions(-) (limited to 'rcon.c') diff --git a/rcon.c b/rcon.c index 756d480..a9a6328 100644 --- a/rcon.c +++ b/rcon.c @@ -21,9 +21,7 @@ struct rcon { unsigned next_rcon; struct sockaddr_in46 rcon; char rconstr[ADDRSTRLEN]; - char rconbuf[4096]; - size_t rconbuflen; - size_t rconbufdone; + struct uring_task_buf tbuf; }; static void @@ -129,44 +127,46 @@ enum rcon_packet_type { static void create_packet(struct cfg *cfg, struct rcon *rcon, int32_t reqid, enum rcon_packet_type type, const char *msg) { - char *pos = &rcon->rconbuf[4]; + char *pos = &rcon->tbuf.buf[4]; - rcon->rconbufdone = 0; - rcon->rconbuflen = 4; - write_int(&pos, &rcon->rconbuflen, reqid); - write_int(&pos, &rcon->rconbuflen, type); - write_str(&pos, &rcon->rconbuflen, msg); - write_end(&pos, &rcon->rconbuflen); - pos = &rcon->rconbuf[0]; - write_int(&pos, NULL, rcon->rconbuflen - 4); + /* Body */ + rcon->tbuf.len = 4; + write_int(&pos, &rcon->tbuf.len, reqid); + write_int(&pos, &rcon->tbuf.len, type); + write_str(&pos, &rcon->tbuf.len, msg); + write_end(&pos, &rcon->tbuf.len); + + /* Header (length of body) */ + pos = &rcon->tbuf.buf[0]; + write_int(&pos, NULL, rcon->tbuf.len - 4); fprintf(stderr, "Created packet (reqid: %" PRIi32 ", type %" PRIi32 ", len %zu, payload: %s)\n", - reqid, type, rcon->rconbuflen, msg); + reqid, type, rcon->tbuf.len, msg); } -static bool -packet_complete(struct cfg *cfg, struct rcon *rcon) +static int +packet_complete(struct cfg *cfg, struct uring_task *task, int res) { - char *pos = rcon->rconbuf; - size_t len = rcon->rconbufdone; + char *pos = task->tbuf->buf; + size_t len = task->tbuf->len; int32_t plen; - if (rcon->rconbufdone < 14) - return false; + if (task->tbuf->len < 14) + return 0; plen = read_int(&pos, &len); - fprintf(stderr, "Reply is %zu bytes, packet size %" PRIi32 "\n", rcon->rconbufdone, plen + 4); - if (rcon->rconbufdone < plen + 4) - return false; + fprintf(stderr, "Reply is %zu bytes, packet size %" PRIi32 "\n", task->tbuf->len, plen + 4); + if (task->tbuf->len < plen + 4) + return 0; else - return true; + return 1; } static bool read_packet(struct cfg *cfg, struct rcon *rcon, int32_t *id, int32_t *type, char **rmsg) { - char *pos = rcon->rconbuf; - size_t len = rcon->rconbufdone; + char *pos = rcon->tbuf.buf; + size_t len = rcon->tbuf.len; int32_t plen; plen = read_int(&pos, &len); @@ -181,18 +181,7 @@ read_packet(struct cfg *cfg, struct rcon *rcon, int32_t *id, int32_t *type, char fprintf(stderr, "Remaining = %zu\n", len); if (len > 2) { - char *msg; - - msg = malloc(len - 1); - if (!msg) { - perror("malloc"); - return false; - } - - memcpy(msg, pos, len - 2); - msg[len - 2] = '\0'; - - *rmsg = msg; + *rmsg = pos; pos += len - 2; len = 2; } @@ -226,27 +215,16 @@ rcon_stop_reply(struct cfg *cfg, struct uring_task *task, int res) if (res < 0) goto out; - rcon->rconbufdone += res; - - /* FIXME: could be multiple packets */ - if (packet_complete(cfg, rcon)) { - fprintf(stderr, "Packet complete\n"); - read_packet(cfg, rcon, &id, &type, &msg); - if (id != 2) { - fprintf(stderr, "RCon stop cmd failed - unexpected reply id (%" PRIi32 ")\n", id); - goto out; - } else if (type != RCON_PACKET_RESPONSE) { - fprintf(stderr, "RCon stop cmd failed - unexpected reply type (%" PRIi32 ")\n", type); - goto out; - } - fprintf(stderr, "RCon stop successful (%s)\n", msg); - free(msg); - - } else { - fprintf(stderr, "Packet not complete\n"); - uring_read(cfg, &rcon->task, &rcon->rconbuf + rcon->rconbufdone, - sizeof(rcon->rconbuf) - rcon->rconbufdone, 0, rcon_stop_reply); + fprintf(stderr, "Packet complete\n"); + read_packet(cfg, rcon, &id, &type, &msg); + if (id != 2) { + fprintf(stderr, "RCon stop cmd failed - unexpected reply id (%" PRIi32 ")\n", id); + goto out; + } else if (type != RCON_PACKET_RESPONSE) { + fprintf(stderr, "RCon stop cmd failed - unexpected reply type (%" PRIi32 ")\n", type); + goto out; } + fprintf(stderr, "RCon stop successful (%s)\n", msg); out: uring_task_put(cfg, &rcon->task); @@ -263,17 +241,8 @@ rcon_stop_sent(struct cfg *cfg, struct uring_task *task, int res) return; } - rcon->rconbufdone += res; - if (rcon->rconbufdone < rcon->rconbuflen) { - uring_write(cfg, &rcon->task, rcon->rconbuf + rcon->rconbufdone, - rcon->rconbuflen - rcon->rconbufdone, rcon_stop_sent); - return; - } - fprintf(stderr, "%s: stop cmd sent\n", __func__); - rcon->rconbufdone = 0; - rcon->rconbuflen = 0; - uring_read(cfg, &rcon->task, &rcon->rconbuf, sizeof(rcon->rconbuf), 0, rcon_stop_reply); + uring_tbuf_read_until(cfg, &rcon->task, packet_complete, rcon_stop_reply); } static void @@ -288,32 +257,22 @@ rcon_login_reply(struct cfg *cfg, struct uring_task *task, int res) if (res < 0) goto out; - rcon->rconbufdone += res; - - if (packet_complete(cfg, rcon)) { - fprintf(stderr, "Packet complete\n"); - read_packet(cfg, rcon, &id, &type, &msg); - if (id != 1) { - fprintf(stderr, "RCon login failed - unexpected reply id (%" PRIi32 ")\n", id); - goto out; - } else if (type == RCON_PACKET_LOGIN_FAIL) { - fprintf(stderr, "RCon login failed - incorrect password\n"); - goto out; - } else if (type != RCON_PACKET_LOGIN_OK) { - fprintf(stderr, "RCon login failed - unexpected reply type (%" PRIi32 ")\n", type); - goto out; - } - free(msg); - fprintf(stderr, "RCon login successful\n"); - create_packet(cfg, rcon, 2, RCON_PACKET_COMMAND, "stop"); - uring_write(cfg, &rcon->task, rcon->rconbuf, rcon->rconbuflen, rcon_stop_sent); - - } else { - fprintf(stderr, "Packet not complete\n"); - uring_read(cfg, &rcon->task, &rcon->rconbuf + rcon->rconbufdone, - sizeof(rcon->rconbuf) - rcon->rconbufdone, 0, rcon_login_reply); + fprintf(stderr, "Packet complete\n"); + read_packet(cfg, rcon, &id, &type, &msg); + if (id != 1) { + fprintf(stderr, "RCon login failed - unexpected reply id (%" PRIi32 ")\n", id); + goto out; + } else if (type == RCON_PACKET_LOGIN_FAIL) { + fprintf(stderr, "RCon login failed - incorrect password\n"); + goto out; + } else if (type != RCON_PACKET_LOGIN_OK) { + fprintf(stderr, "RCon login failed - unexpected reply type (%" PRIi32 ")\n", type); + goto out; } + fprintf(stderr, "RCon login successful\n"); + create_packet(cfg, rcon, 2, RCON_PACKET_COMMAND, "stop"); + uring_tbuf_write(cfg, &rcon->task, rcon_stop_sent); return; out: @@ -331,17 +290,8 @@ rcon_login_sent(struct cfg *cfg, struct uring_task *task, int res) return; } - rcon->rconbufdone += res; - if (rcon->rconbufdone < rcon->rconbuflen) { - uring_write(cfg, &rcon->task, rcon->rconbuf + rcon->rconbufdone, - rcon->rconbuflen - rcon->rconbufdone, rcon_login_sent); - return; - } - fprintf(stderr, "%s: login sent\n", __func__); - rcon->rconbufdone = 0; - rcon->rconbuflen = 0; - uring_read(cfg, &rcon->task, &rcon->rconbuf, sizeof(rcon->rconbuf), 0, rcon_login_reply); + uring_tbuf_read_until(cfg, &rcon->task, packet_complete, rcon_login_reply); } static void rcon_connect_next_rcon(struct cfg *cfg, struct rcon *rcon); @@ -358,7 +308,7 @@ rcon_connected(struct cfg *cfg, struct uring_task *task, int res) } create_packet(cfg, rcon, 1, RCON_PACKET_LOGIN, rcon->server->rcon_password); - uring_write(cfg, &rcon->task, rcon->rconbuf, rcon->rconbuflen, rcon_login_sent); + uring_tbuf_write(cfg, &rcon->task, rcon_login_sent); } /* FIXME: Parts of this could be shared with proxy.c, probably in server.c */ @@ -418,6 +368,7 @@ rcon_init(struct cfg *cfg, struct server *server) perrordie("malloc"); uring_task_init(&rcon->task, "rcon", &server->task, rcon_free); + uring_task_set_buf(&rcon->task, &rcon->tbuf); rcon->server = server; server->rcon = rcon; -- cgit v1.2.3