From db90b7982410c20bca2c3dc206a690012edecce4 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Fri, 5 Jun 2020 17:07:34 +0200 Subject: remote struct server_remote --- server.c | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/server.c b/server.c index 1045e43..0ef5aa7 100644 --- a/server.c +++ b/server.c @@ -20,13 +20,6 @@ struct server_local { struct list_head list; }; -/* FIXME: This can be a plain sockaddr_in46 */ -struct server_remote { - struct sockaddr_in46 addr; - char addrstr[ADDRSTRLEN]; - struct list_head list; -}; - struct server_proxy { struct sockaddr_in46 client; char clientstr[ADDRSTRLEN]; @@ -67,7 +60,7 @@ void server_delete(struct cfg *cfg, struct server *scfg) { struct server_local *local, *ltmp; - struct server_remote *remote, *rtmp; + struct sockaddr_in46 *remote, *rtmp; fprintf(stderr, "Removing server cfg: %s\n", scfg->name); @@ -127,7 +120,8 @@ static void server_dump(struct server *scfg) { struct server_local *local; - struct server_remote *remote; + struct sockaddr_in46 *remote; + char abuf[ADDRSTRLEN]; fprintf(stderr, "Dumping server %s\n", scfg->name); switch (scfg->type) { @@ -148,7 +142,7 @@ server_dump(struct server *scfg) fprintf(stderr, " * %s\n", local->addrstr); fprintf(stderr, " * Remote ports:\n"); list_for_each_entry(remote, &scfg->remotes, list) - fprintf(stderr, " * %s\n", remote->addrstr); + fprintf(stderr, " * %s\n", server_print_addr(remote, abuf, sizeof(abuf))); } /* @@ -199,7 +193,8 @@ server_local_accept(struct cfg *cfg, struct uring_task *task, int res) struct server_local *local = container_of(task, struct server_local, task); struct server *scfg = container_of(task->parent, struct server, task); struct server_proxy *proxy; - struct server_remote *remote; + struct sockaddr_in46 *remote; + char abuf[ADDRSTRLEN]; int sfd; fprintf(stderr, "%s called: task 0x%p and res %i\n", __func__, task, res); @@ -231,10 +226,11 @@ server_local_accept(struct cfg *cfg, struct uring_task *task, int res) goto out; } - remote = list_first_entry(&scfg->remotes, struct server_remote, list); - fprintf(stderr, "%s: attempting proxy connection to %s (len %u)\n", scfg->name, remote->addrstr, remote->addr.addrlen); + remote = list_first_entry(&scfg->remotes, struct sockaddr_in46, list); + fprintf(stderr, "%s: attempting proxy connection to %s (len %u)\n", + scfg->name, server_print_addr(remote, abuf, sizeof(abuf)), remote->addrlen); - sfd = socket(remote->addr.storage.ss_family, SOCK_STREAM, 0); + sfd = socket(remote->storage.ss_family, SOCK_STREAM, 0); if (sfd < 0) { perror("socket"); uring_close(cfg, NULL, res, NULL); @@ -246,7 +242,7 @@ server_local_accept(struct cfg *cfg, struct uring_task *task, int res) uring_task_init(&proxy->task, "proxy", &scfg->task, server_proxy_free); uring_task_set_fd(&proxy->task, sfd); list_add(&proxy->list, &scfg->proxys); - uring_connect(cfg, &proxy->task, &remote->addr, server_proxy_connected); + uring_connect(cfg, &proxy->task, remote, server_proxy_connected); out: uring_accept(cfg, &local->task, &local->peer, server_local_accept); @@ -311,19 +307,10 @@ server_commit(struct cfg *cfg, struct server *scfg) bool server_add_remote(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *addr) { - struct server_remote *remote; - if (!scfg || !addr) return false; - remote = zmalloc(sizeof(*remote)); - if (!remote) - return false; - - remote->addr.storage = addr->storage; - remote->addr.addrlen = addr->addrlen; - server_print_addr(&remote->addr, remote->addrstr, sizeof(remote->addrstr)); - list_add(&remote->list, &scfg->remotes); + list_add(&addr->list, &scfg->remotes); return true; } @@ -345,6 +332,7 @@ server_add_local(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *add server_print_addr(&local->addr, local->addrstr, sizeof(local->addrstr)); fprintf(stderr, "Adding local: %s\n", local->addrstr); list_add(&local->list, &scfg->locals); + free(addr); return true; } -- cgit v1.2.3