From 76e6067e13831569deca05dbb557d921998e3eb2 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Thu, 11 Jun 2020 23:08:45 +0200 Subject: Create a helper function to loop through different possible connections and convert proxy to use it --- proxy.c | 61 +++++++++---------------------------------------------------- 1 file changed, 9 insertions(+), 52 deletions(-) (limited to 'proxy.c') diff --git a/proxy.c b/proxy.c index 6b724f2..58f81ca 100644 --- a/proxy.c +++ b/proxy.c @@ -179,70 +179,27 @@ proxy_server_data_in(struct cfg *cfg, struct uring_task *task, int res) uring_tbuf_write(cfg, task, proxy_server_data_out); } -static void proxy_connect_next_remote(struct cfg *cfg, struct server_proxy *proxy); - static void -proxy_server_connected(struct cfg *cfg, struct uring_task *task, int res) +proxy_connected_cb(struct cfg *cfg, struct connection *conn, int res) { - struct server_proxy *proxy = container_of(task, struct server_proxy, servertask); + struct server_proxy *proxy = container_of(conn, struct server_proxy, server_conn); - fprintf(stderr, "%s: connected %i\n", __func__, res); if (res < 0) { - uring_task_close_fd(cfg, task); - proxy_connect_next_remote(cfg, proxy); + fprintf(stderr, "%s: proxy connection to remote server failed\n", + proxy->scfg->name); + proxy_delete(cfg, proxy); return; } + proxy->sfd = proxy->servertask.fd; fprintf(stderr, "%s: proxy connection %s -> %s opened\n", - proxy->scfg->name, proxy->clientstr, proxy->serverstr); + proxy->scfg->name, proxy->clientstr, proxy->server_conn.remotestr); proxy->begin = time(NULL); + uring_tbuf_read(cfg, &proxy->clienttask, proxy_client_data_in); uring_tbuf_read(cfg, &proxy->servertask, proxy_server_data_in); } -static void -proxy_connect_next_remote(struct cfg *cfg, struct server_proxy *proxy) -{ - struct sockaddr_in46 *remote, *tmp; - struct server *scfg = proxy->scfg; - int sfd; - unsigned i = 0; - -again: - remote = NULL; - list_for_each_entry(tmp, &scfg->remotes, list) { - if (i == proxy->next_remote) { - remote = tmp; - break; - } - i++; - } - - if (!remote) { - fprintf(stderr, "No more remote addresses to attempt\n"); - proxy_delete(cfg, proxy); - return; - } - - proxy->next_remote++; - proxy->server = *remote; - sockaddr_to_str(&proxy->server, proxy->serverstr, sizeof(proxy->serverstr)); - fprintf(stderr, "%s: attempting proxy connection to %s (len %u)\n", - scfg->name, proxy->serverstr, proxy->server.addrlen); - - sfd = socket(proxy->server.storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0); - if (sfd < 0) { - perror("socket"); - goto again; - } - - socket_set_gaming_options(cfg, sfd); - - proxy->sfd = sfd; - uring_task_set_fd(&proxy->servertask, sfd); - uring_connect(cfg, &proxy->servertask, &proxy->server, proxy_server_connected); -} - struct server_proxy * proxy_new(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *client, int fd) { @@ -269,7 +226,7 @@ proxy_new(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *client, in uring_task_set_buf(&proxy->servertask, &proxy->serverbuf); list_add(&proxy->list, &scfg->proxys); - proxy_connect_next_remote(cfg, proxy); + connect_any(cfg, &proxy->servertask, &scfg->remotes, &proxy->server_conn, proxy_connected_cb); return proxy; } -- cgit v1.2.3