From 2df1929f3e08d704e979b25eba4e491e88f3d728 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Tue, 16 Jun 2020 21:27:28 +0200 Subject: Convert rcon to use debugging --- main.c | 3 +++ main.h | 1 + rcon.c | 86 +++++++++++++++++++++++++++++++++++++----------------------------- 3 files changed, 52 insertions(+), 38 deletions(-) diff --git a/main.c b/main.c index a457caa..e51ab8b 100644 --- a/main.c +++ b/main.c @@ -106,6 +106,9 @@ const struct { },{ .name = "proxy", .val = DBG_PROXY + },{ + .name = "rcon", + .val = DBG_RCON },{ .name = NULL, .val = 0 diff --git a/main.h b/main.h index a4bf7da..f557523 100644 --- a/main.h +++ b/main.h @@ -24,6 +24,7 @@ enum debug_category { DBG_UR = (0x1 << 8), DBG_SRV = (0x1 << 9), DBG_PROXY = (0x1 << 10), + DBG_RCON = (0x1 << 11), }; static inline bool diff --git a/rcon.c b/rcon.c index e6cce64..11383c2 100644 --- a/rcon.c +++ b/rcon.c @@ -27,7 +27,7 @@ rcon_free(struct uring_task *task) { struct rcon *rcon = container_of(task, struct rcon, task); - fprintf(stderr, "%s: called with task 0x%p and idle 0x%p\n", __func__, task, rcon); + debug(DBG_RCON, "task %p, idle %p\n", task, rcon); rcon->server->rcon = NULL; xfree(rcon); } @@ -49,7 +49,7 @@ rcon_delete(struct cfg *cfg, struct server *server) if (!rcon) return; - fprintf(stderr, "%s called, closing fd %i\n", __func__, rcon->task.fd); + debug(DBG_RCON, "closing fd %i\n", rcon->task.fd); uring_task_destroy(cfg, &rcon->task); server->rcon = NULL; } @@ -85,6 +85,7 @@ write_int(char **pos, size_t *len, int32_t orig) p[1] = (val >> 8) & 0xff; p[2] = (val >> 16) & 0xff; p[3] = (val >> 24) & 0xff; + *pos += 4; if (len) *len += 4; @@ -108,6 +109,7 @@ write_end(char **pos, size_t *len) p[0] = 0x00; p[1] = 0x00; + *pos += 2; if (len) *len += 2; @@ -137,8 +139,9 @@ create_packet(struct cfg *cfg, struct rcon *rcon, int32_t reqid, enum rcon_packe 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->tbuf.len, msg); + debug(DBG_RCON, "created packet (reqid: %" PRIi32 ", type %" PRIi32 + ", len %zu, payload: %s)\n", + reqid, type, rcon->tbuf.len, msg); } static int @@ -152,7 +155,9 @@ packet_complete(struct cfg *cfg, struct uring_task *task, int res) return 0; plen = read_int(&pos, &len); - fprintf(stderr, "Reply is %zu bytes, packet size %" PRIi32 "\n", task->tbuf->len, plen + 4); + debug(DBG_RCON, "reply size: %zu bytes, packet size %" PRIi32 "\n", + task->tbuf->len, plen + 4); + if (task->tbuf->len < plen + 4) return 0; else @@ -160,7 +165,8 @@ packet_complete(struct cfg *cfg, struct uring_task *task, int res) } static bool -read_packet(struct cfg *cfg, struct rcon *rcon, int32_t *id, int32_t *type, char **rmsg) +rcon_read_packet(struct cfg *cfg, struct rcon *rcon, int32_t *id, + int32_t *type, char **rmsg) { char *pos = rcon->tbuf.buf; size_t len = rcon->tbuf.len; @@ -172,11 +178,10 @@ read_packet(struct cfg *cfg, struct rcon *rcon, int32_t *id, int32_t *type, char *rmsg = NULL; if (plen < 10) { - fprintf(stderr, "Invalid packet length: %" PRIi32 "\n", plen); + error("invalid packet length: %" PRIi32 "\n", plen); return false; } - fprintf(stderr, "Remaining = %zu\n", len); if (len > 2) { *rmsg = pos; pos += len - 2; @@ -184,18 +189,18 @@ read_packet(struct cfg *cfg, struct rcon *rcon, int32_t *id, int32_t *type, char } if (len < 2) { - fprintf(stderr, "Short message\n"); + error("short message\n"); return false; } if (pos[0] != '\0' || pos[1] != '\0') { - fprintf(stderr, "Invalid trailer\n"); + error("invalid trailer\n"); return false; } - fprintf(stderr, "Response - len: %" PRIi32 ", id: %" PRIi32 - ", type: %" PRIi32 ", msg: %s\n", - plen, *id, *type, *rmsg); + debug(DBG_RCON, "response - len: %" PRIi32 ", id: %" PRIi32 + ", type: %" PRIi32 ", msg: %s\n", + plen, *id, *type, *rmsg); return true; } @@ -208,25 +213,28 @@ rcon_stop_reply(struct cfg *cfg, struct uring_task *task, int res) int32_t type; char *msg; - fprintf(stderr, "%s: result %i\n", __func__, res); if (task->dead) { - fprintf(stderr, "%s: task dead\n", __func__); + debug(DBG_RCON, "task dead\n"); return; } - if (res < 0) + if (res < 0) { + debug(DBG_RCON, "res: %i\n", res); goto out; + } + + debug(DBG_RCON, "packet complete\n"); + rcon_read_packet(cfg, rcon, &id, &type, &msg); - 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); + error("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); + error("rcon stop cmd failed - unexpected reply type (%" PRIi32 ")\n", type); goto out; } - fprintf(stderr, "RCon stop successful (%s)\n", msg); + + verbose("rcon stop cmd successful (%s)\n", msg); out: uring_task_put(cfg, &rcon->task); @@ -237,18 +245,18 @@ rcon_stop_sent(struct cfg *cfg, struct uring_task *task, int res) { struct rcon *rcon = container_of(task, struct rcon, task); - fprintf(stderr, "%s: result %i\n", __func__, res); if (task->dead) { - fprintf(stderr, "%s: task dead\n", __func__); + debug(DBG_RCON, "task dead\n"); return; } if (res < 0) { + debug(DBG_RCON, "res: %i\n", res); uring_task_put(cfg, &rcon->task); return; } - fprintf(stderr, "%s: stop cmd sent\n", __func__); + debug(DBG_RCON, "stop cmd sent\n"); uring_tbuf_read_until(cfg, &rcon->task, packet_complete, rcon_stop_reply); } @@ -260,29 +268,31 @@ rcon_login_reply(struct cfg *cfg, struct uring_task *task, int res) int32_t type; char *msg; - fprintf(stderr, "%s: result %i\n", __func__, res); if (task->dead) { - fprintf(stderr, "%s: task dead\n", __func__); + debug(DBG_RCON, "task dead\n"); return; } - if (res < 0) + if (res < 0) { + debug(DBG_RCON, "res: %i\n", res); goto out; + } + + debug(DBG_RCON, "packet complete\n"); + rcon_read_packet(cfg, rcon, &id, &type, &msg); - 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); + error("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"); + error("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); + error("rcon login failed - unexpected reply type (%" PRIi32 ")\n", type); goto out; } - fprintf(stderr, "RCon login successful\n"); + debug(DBG_RCON, "rcon login successful\n"); create_packet(cfg, rcon, 2, RCON_PACKET_COMMAND, "stop"); uring_tbuf_write(cfg, &rcon->task, rcon_stop_sent); return; @@ -296,18 +306,18 @@ rcon_login_sent(struct cfg *cfg, struct uring_task *task, int res) { struct rcon *rcon = container_of(task, struct rcon, task); - fprintf(stderr, "%s: result %i\n", __func__, res); if (task->dead) { - fprintf(stderr, "%s: task dead\n", __func__); + debug(DBG_RCON, "task dead\n"); return; } if (res < 0) { + debug(DBG_RCON, "res: %i\n", res); uring_task_put(cfg, &rcon->task); return; } - fprintf(stderr, "%s: login sent\n", __func__); + debug(DBG_RCON, "login sent\n"); uring_tbuf_read_until(cfg, &rcon->task, packet_complete, rcon_login_reply); } @@ -317,8 +327,8 @@ rcon_connected_cb(struct cfg *cfg, struct connection *conn, bool connected) struct rcon *rcon = container_of(conn, struct rcon, conn); if (!connected) { - fprintf(stderr, "%s: rcon connection to remote server failed\n", - rcon->server->name); + error("rcon connection to remote server (%s) failed\n", + rcon->server->name); return; } -- cgit v1.2.3