From cefee96efe7734230d5dafe4dc2d5782b7f42cf2 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Thu, 11 Jun 2020 23:46:27 +0200 Subject: Convert rcon to use struct connection --- rcon.c | 66 +++++++----------------------------------------------------------- 1 file changed, 7 insertions(+), 59 deletions(-) diff --git a/rcon.c b/rcon.c index 5476160..e6cce64 100644 --- a/rcon.c +++ b/rcon.c @@ -17,10 +17,8 @@ struct rcon { struct server *server; + struct connection conn; struct uring_task task; - unsigned next_rcon; - struct sockaddr_in46 rcon; - char rconstr[ADDRSTRLEN]; struct uring_task_buf tbuf; }; @@ -313,21 +311,14 @@ rcon_login_sent(struct cfg *cfg, struct uring_task *task, int res) uring_tbuf_read_until(cfg, &rcon->task, packet_complete, rcon_login_reply); } -static void rcon_connect_next_rcon(struct cfg *cfg, struct rcon *rcon); - static void -rcon_connected(struct cfg *cfg, struct uring_task *task, int res) +rcon_connected_cb(struct cfg *cfg, struct connection *conn, bool connected) { - struct rcon *rcon = container_of(task, struct rcon, task); + struct rcon *rcon = container_of(conn, struct rcon, conn); - fprintf(stderr, "%s: connected %i\n", __func__, res); - if (task->dead) { - fprintf(stderr, "%s: task dead\n", __func__); - return; - } - - if (res < 0) { - rcon_connect_next_rcon(cfg, rcon); + if (!connected) { + fprintf(stderr, "%s: rcon connection to remote server failed\n", + rcon->server->name); return; } @@ -335,49 +326,6 @@ rcon_connected(struct cfg *cfg, struct uring_task *task, int res) uring_tbuf_write(cfg, &rcon->task, rcon_login_sent); } -/* FIXME: Parts of this could be shared with proxy.c, probably in server.c */ -static void -rcon_connect_next_rcon(struct cfg *cfg, struct rcon *rcon) -{ - struct sockaddr_in46 *remote, *tmp; - struct server *scfg = rcon->server; - int sfd; - unsigned i = 0; - -again: - remote = NULL; - list_for_each_entry(tmp, &scfg->rcons, list) { - if (i == rcon->next_rcon) { - remote = tmp; - break; - } - i++; - } - - if (!remote) { - fprintf(stderr, "No more rcon addresses to attempt\n"); - uring_task_put(cfg, &rcon->task); - return; - } - - rcon->next_rcon++; - rcon->rcon = *remote; - sockaddr_to_str(&rcon->rcon, rcon->rconstr, sizeof(rcon->rconstr)); - fprintf(stderr, "%s: attempting rcon connection to %s (len %u)\n", - scfg->name, rcon->rconstr, rcon->rcon.addrlen); - - sfd = socket(rcon->rcon.storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0); - if (sfd < 0) { - perror("socket"); - goto again; - } - - socket_set_low_latency(cfg, sfd); - - uring_task_set_fd(&rcon->task, sfd); - uring_connect(cfg, &rcon->task, &rcon->rcon, rcon_connected); -} - void rcon_init(struct cfg *cfg, struct server *server) { @@ -398,5 +346,5 @@ rcon_init(struct cfg *cfg, struct server *server) rcon->server = server; server->rcon = rcon; - rcon_connect_next_rcon(cfg, rcon); + connect_any(cfg, &rcon->task, &server->rcons, &rcon->conn, rcon_connected_cb); } -- cgit v1.2.3