summaryrefslogtreecommitdiff
path: root/proxy.c
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-11 23:08:45 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-11 23:08:45 +0200
commit76e6067e13831569deca05dbb557d921998e3eb2 (patch)
tree0b2b1e9f24628d60b4d1ee6682a850eed19c569a /proxy.c
parent79354a389890a7ee4b6cd5b7d7044b521fb133a6 (diff)
Create a helper function to loop through different possible connections and convert proxy to use it
Diffstat (limited to 'proxy.c')
-rw-r--r--proxy.c61
1 files changed, 9 insertions, 52 deletions
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;
}