diff options
-rw-r--r-- | main.h | 10 | ||||
-rw-r--r-- | meson.build | 2 | ||||
-rw-r--r-- | proxy.c | 67 | ||||
-rw-r--r-- | proxy.h | 19 | ||||
-rw-r--r-- | server.c | 108 | ||||
-rw-r--r-- | utils.c | 28 | ||||
-rw-r--r-- | utils.h | 17 |
7 files changed, 139 insertions, 112 deletions
@@ -33,16 +33,6 @@ struct uring_task { callback_t callback; }; -struct sockaddr_in46 { - union { - struct sockaddr_storage storage; - struct sockaddr_in in4; - struct sockaddr_in6 in6; - }; - socklen_t addrlen; - struct list_head list; -}; - struct cfg { const char *homedir; struct uring_ev *uev; diff --git a/meson.build b/meson.build index efa0cdb..4c7fb08 100644 --- a/meson.build +++ b/meson.build @@ -4,6 +4,6 @@ uring = dependency('liburing') executable('ctest', 'ctest.c') executable('stest', 'stest.c') executable('mcproxy', - ['main.c', 'uring.c', 'server.c', 'cfgdir.c', 'config.c', 'utils.c'], + ['main.c', 'uring.c', 'server.c', 'proxy.c', 'cfgdir.c', 'config.c', 'utils.c'], dependencies: uring) @@ -0,0 +1,67 @@ +#include "main.h" +#include "uring.h" +#include "server.h" +#include "proxy.h" +#include "utils.h" + +void +proxy_refdump(struct server_proxy *proxy) +{ + uring_task_refdump(&proxy->task); +} + +static void +proxy_free(struct uring_task *task) +{ + struct server_proxy *proxy = container_of(task, struct server_proxy, task); + + list_del(&proxy->list); + free(proxy); +} + +static void +proxy_connected(struct cfg *cfg, struct uring_task *task, int res) +{ + //struct server_proxy *proxy = container_of(task, struct server_proxy, task); + + fprintf(stderr, "%s: connected %i\n", __func__, res); + return; +} + +struct server_proxy * +proxy_new(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *client, int fd) +{ + struct server_proxy *proxy; + struct sockaddr_in46 *remote; + int sfd; + + proxy = zmalloc(sizeof(*proxy)); + if (!proxy) { + perror("malloc"); + return NULL; + } + + remote = list_first_entry(&scfg->remotes, struct sockaddr_in46, list); + 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, 0); + if (sfd < 0) { + perror("socket"); + uring_close(cfg, NULL, fd, NULL); + free(proxy); + return NULL; + } + + proxy->client = *client; + sockaddr_to_str(&proxy->client, proxy->clientstr, sizeof(proxy->clientstr)); + uring_task_init(&proxy->task, "server_proxy", &scfg->task, proxy_free); + uring_task_set_fd(&proxy->task, sfd); + list_add(&proxy->list, &scfg->proxys); + uring_connect(cfg, &proxy->task, &proxy->server, proxy_connected); + + return proxy; +} + @@ -0,0 +1,19 @@ +#ifndef fooproxyhfoo +#define fooproxyhfoo + +struct server_proxy { + struct sockaddr_in46 client; + char clientstr[ADDRSTRLEN]; + struct sockaddr_in46 server; + char serverstr[ADDRSTRLEN]; + struct uring_task task; + char buf[4096]; + size_t len; + struct list_head list; +}; + +void proxy_refdump(struct server_proxy *proxy); + +struct server_proxy *proxy_new(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *client, int fd); + +#endif @@ -6,28 +6,15 @@ #include "main.h" #include "uring.h" -#include "config.h" #include "server.h" - -#define ADDRSTRLEN (9 /*strlen("AF_INETX ")*/ + INET6_ADDRSTRLEN + 6 /*strlen(" 65535")*/ + 1) +#include "proxy.h" +#include "utils.h" struct server_local { struct sockaddr_in46 addr; char addrstr[ADDRSTRLEN]; struct sockaddr_in46 peer; - char peerstr[ADDRSTRLEN]; - struct uring_task task; - struct list_head list; -}; - -struct server_proxy { - struct sockaddr_in46 client; - char clientstr[ADDRSTRLEN]; - struct sockaddr_in46 server; - char serverstr[ADDRSTRLEN]; struct uring_task task; - char buf[4096]; - size_t len; struct list_head list; }; @@ -41,7 +28,7 @@ server_refdump(struct server *server) list_for_each_entry(local, &server->locals, list) uring_task_refdump(&local->task); list_for_each_entry(proxy, &server->proxys, list) - uring_task_refdump(&proxy->task); + proxy_refdump(proxy); } static void @@ -92,30 +79,6 @@ server_delete_by_name(struct cfg *cfg, const char *name) } } -static char * -server_print_addr(struct sockaddr_in46 *addr, char *buf, size_t buflen) -{ - char abuf[ADDRSTRLEN]; - - switch (addr->storage.ss_family) { - case AF_INET: - snprintf(buf, buflen, "AF_INET4 %s %u", - inet_ntop(addr->in4.sin_family, &addr->in4.sin_addr, abuf, sizeof(abuf)), - (unsigned)ntohs(addr->in4.sin_port)); - break; - case AF_INET6: - snprintf(buf, buflen, "AF_INET6 %s %u", - inet_ntop(addr->in6.sin6_family, &addr->in6.sin6_addr, abuf, sizeof(abuf)), - (unsigned)ntohs(addr->in6.sin6_port)); - break; - default: - snprintf(buf, buflen, "AF_UNKNOWN"); - break; - } - - return buf; -} - static void server_dump(struct server *scfg) { @@ -142,38 +105,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", server_print_addr(remote, abuf, sizeof(abuf))); -} - -/* -struct server_active_proxy { - struct sockaddr_in46 client; - char clientstr[ADDRSTRLEN]; - struct sockaddr_in46 server; - char serverstr[ADDRSTRLEN] - struct uring_task task; - char buf[4096]; - size_t len; - struct list_head list; -}; -*/ - -static void -server_proxy_free(struct uring_task *task) -{ - struct server_proxy *proxy = container_of(task, struct server_proxy, task); - - list_del(&proxy->list); - free(proxy); -} - -static void -server_proxy_connected(struct cfg *cfg, struct uring_task *task, int res) -{ - //struct server_proxy *proxy = container_of(task, struct server_proxy, task); - - fprintf(stderr, "%s: connected %i\n", __func__, res); - return; + fprintf(stderr, " * %s\n", sockaddr_to_str(remote, abuf, sizeof(abuf))); } static void @@ -193,9 +125,7 @@ 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 sockaddr_in46 *remote; char abuf[ADDRSTRLEN]; - int sfd; fprintf(stderr, "%s called: task 0x%p and res %i\n", __func__, task, res); fprintf(stderr, "%s called: scfg name is %s\n", __func__, scfg->name); @@ -211,38 +141,18 @@ server_local_accept(struct cfg *cfg, struct uring_task *task, int res) goto out; } - server_print_addr(&local->peer, local->peerstr, sizeof(local->peerstr)); - fprintf(stderr, "%s: incoming proxy connection: %s -> %s\n", scfg->name, local->peerstr, local->addrstr); + sockaddr_to_str(&local->peer, abuf, sizeof(abuf)); + fprintf(stderr, "%s: incoming proxy connection: %s -> %s\n", scfg->name, abuf, local->addrstr); if (list_empty(&scfg->remotes)) { error("scfg->remotes empty!\n"); - goto out; - } - - proxy = zmalloc(sizeof(*proxy)); - if (!proxy) { - perror("malloc"); uring_close(cfg, NULL, res, NULL); goto out; } - 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->storage.ss_family, SOCK_STREAM, 0); - if (sfd < 0) { - perror("socket"); + proxy = proxy_new(cfg, scfg, &local->peer, res); + if (!proxy) uring_close(cfg, NULL, res, NULL); - goto out; - } - - proxy->client = local->peer; - memcpy(proxy->clientstr, local->peerstr, sizeof(proxy->clientstr)); - 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, server_proxy_connected); out: uring_accept(cfg, &local->task, &local->peer, server_local_accept); @@ -329,7 +239,7 @@ server_add_local(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *add local->addr.storage = addr->storage; local->addr.addrlen = addr->addrlen; uring_task_init(&local->task, "local", &scfg->task, server_local_free); - server_print_addr(&local->addr, local->addrstr, sizeof(local->addrstr)); + sockaddr_to_str(&local->addr, local->addrstr, sizeof(local->addrstr)); fprintf(stderr, "Adding local: %s\n", local->addrstr); list_add(&local->list, &scfg->locals); free(addr); @@ -2,6 +2,34 @@ #include <errno.h> #include <stdint.h> #include <limits.h> +#include <arpa/inet.h> + +#include "main.h" +#include "utils.h" + +char * +sockaddr_to_str(struct sockaddr_in46 *addr, char *buf, size_t buflen) +{ + char abuf[ADDRSTRLEN]; + + switch (addr->storage.ss_family) { + case AF_INET: + snprintf(buf, buflen, "AF_INET4 %s %u", + inet_ntop(addr->in4.sin_family, &addr->in4.sin_addr, abuf, sizeof(abuf)), + (unsigned)ntohs(addr->in4.sin_port)); + break; + case AF_INET6: + snprintf(buf, buflen, "AF_INET6 %s %u", + inet_ntop(addr->in6.sin6_family, &addr->in6.sin6_addr, abuf, sizeof(abuf)), + (unsigned)ntohs(addr->in6.sin6_port)); + break; + default: + snprintf(buf, buflen, "AF_UNKNOWN"); + break; + } + + return buf; +} int strtou16_strict(const char *str, uint16_t *result) @@ -5,8 +5,6 @@ #include <stdbool.h> #include <stdlib.h> -int strtou16_strict(const char *str, uint16_t *result); - struct list_head { struct list_head *next; struct list_head *prev; @@ -86,6 +84,21 @@ static inline void* zmalloc(size_t size) return calloc(1, size); } +#define ADDRSTRLEN (9 /*strlen("AF_INETX ")*/ + INET6_ADDRSTRLEN + 6 /*strlen(" 65535")*/ + 1) +struct sockaddr_in46 { + union { + struct sockaddr_storage storage; + struct sockaddr_in in4; + struct sockaddr_in6 in6; + }; + socklen_t addrlen; + struct list_head list; +}; + +int strtou16_strict(const char *str, uint16_t *result); + +char *sockaddr_to_str(struct sockaddr_in46 *addr, char *buf, size_t buflen); + /* #define _cleanup_(x) __attribute__((cleanup(x))) |