From c12f7a1adee16c6872be87dfd7eda62f078ebd5b Mon Sep 17 00:00:00 2001 From: yrutschle Date: Sat, 7 Nov 2020 22:31:49 +0100 Subject: [PATCH] abstract listening sockets so we have protocol information alongside the socket --- common.c | 13 +++++++------ common.h | 9 +++++++-- sslh-fork.c | 12 ++++++------ sslh-main.c | 2 +- test.cfg | 6 +++--- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/common.c b/common.c index 1fc976d..48fe30b 100644 --- a/common.c +++ b/common.c @@ -70,7 +70,7 @@ void check_res_dump(CR_ACTION act, int res, struct addrinfo *addr, char* syscall } } -int get_fd_sockets(int *sockfd[]) +int get_fd_sockets(struct listen_endpoint *sockfd[]) { int sd = 0; @@ -85,7 +85,8 @@ int get_fd_sockets(int *sockfd[]) *sockfd = malloc(sd * sizeof(*sockfd[0])); CHECK_ALLOC(*sockfd, "malloc"); for (i = 0; i < sd; i++) { - (*sockfd)[i] = SD_LISTEN_FDS_START + i; + (*sockfd)[i].socketfd = SD_LISTEN_FDS_START + i; + (*sockfd)[i].type = SOCK_STREAM; } } #endif @@ -157,11 +158,10 @@ int listen_single_addr(struct addrinfo* addr, int keepalive, int udp) } /* Starts listening sockets on specified addresses. - * OUT: *sockfd[] pointer to newly-allocated array of file descriptors + * OUT: *sockfd[] pointer to newly-allocated array of listen_endpoint objects * Returns number of addresses bound - * Bound file descriptors are returned in newly-allocated *sockfd pointer */ -int start_listen_sockets(int *sockfd[]) +int start_listen_sockets(struct listen_endpoint *sockfd[]) { struct addrinfo *addr, *start_addr; char buf[NI_MAXHOST]; @@ -189,7 +189,8 @@ int start_listen_sockets(int *sockfd[]) num_addr++; *sockfd = realloc(*sockfd, num_addr * sizeof(*sockfd)); - (*sockfd)[num_addr-1] = listen_single_addr(addr, keepalive, udp); + (*sockfd)[num_addr-1].socketfd = listen_single_addr(addr, keepalive, udp); + (*sockfd)[num_addr-1].type = udp ? SOCK_DGRAM : SOCK_STREAM; if (cfg.verbose) fprintf(stderr, "\t%s\t[%s]\n", sprintaddr(buf, sizeof(buf), addr), cfg.listen[i].keepalive ? "keepalive" : ""); diff --git a/common.h b/common.h index 5fcd93d..57230c7 100644 --- a/common.h +++ b/common.h @@ -103,6 +103,11 @@ struct connection { struct queue q[2]; }; +struct listen_endpoint { + int socketfd; /* file descriptor of listening socket */ + int type; /* SOCK_DGRAM | SOCK_STREAM */ +}; + #define FD_CNXCLOSED 0 #define FD_NODATA -1 #define FD_STALLED -2 @@ -133,7 +138,7 @@ void log_message(int type, const char* msg, ...); void dump_connection(struct connection *cnx); int resolve_split_name(struct addrinfo **out, char* hostname, char* port); -int start_listen_sockets(int *sockfd[]); +int start_listen_sockets(struct listen_endpoint *sockfd[]); int defer_write(struct queue *q, void* data, int data_size); int flush_deferred(struct queue *q); @@ -146,6 +151,6 @@ extern const char* server_type; /* sslh-fork.c */ void start_shoveler(int); -void main_loop(int *listen_sockets, int num_addr_listen); +void main_loop(struct listen_endpoint *listen_sockets, int num_addr_listen); #endif diff --git a/sslh-fork.c b/sslh-fork.c index f3f2142..d606d07 100644 --- a/sslh-fork.c +++ b/sslh-fork.c @@ -145,7 +145,7 @@ void stop_listeners(int sig) } } -void set_listen_procname(int listen_socket) +void set_listen_procname(struct listen_endpoint *listen_socket) { #ifdef LIBBSD int res; @@ -155,7 +155,7 @@ void set_listen_procname(int listen_socket) addr.ai_addr = (struct sockaddr*)&ss; addr.ai_addrlen = sizeof(ss); - res = getsockname(listen_socket, addr.ai_addr, &addr.ai_addrlen); + res = getsockname(listen_socket->socketfd, addr.ai_addr, &addr.ai_addrlen); if (res != -1) { sprintaddr(listen_addr, sizeof(listen_addr), &addr); setproctitle("listener %s", listen_addr); @@ -163,7 +163,7 @@ void set_listen_procname(int listen_socket) #endif } -void main_loop(int listen_sockets[], int num_addr_listen) +void main_loop(struct listen_endpoint listen_sockets[], int num_addr_listen) { int in_socket, i, res; struct sigaction action; @@ -183,10 +183,10 @@ void main_loop(int listen_sockets[], int num_addr_listen) case 0: /* Listening process just accepts a connection, forks, and goes * back to listening */ - set_listen_procname(listen_sockets[i]); + set_listen_procname(&listen_sockets[i]); while (1) { - in_socket = accept(listen_sockets[i], 0, 0); + in_socket = accept(listen_sockets[i].socketfd, 0, 0); if (cfg.verbose) fprintf(stderr, "accepted fd %d\n", in_socket); switch(fork()) { @@ -196,7 +196,7 @@ void main_loop(int listen_sockets[], int num_addr_listen) /* In child process */ case 0: for (i = 0; i < num_addr_listen; ++i) - close(listen_sockets[i]); + close(listen_sockets[i].socketfd); start_shoveler(in_socket); exit(0); diff --git a/sslh-main.c b/sslh-main.c index 9feec7c..178a770 100644 --- a/sslh-main.c +++ b/sslh-main.c @@ -201,7 +201,7 @@ int main(int argc, char *argv[], char* envp[]) extern char *optarg; extern int optind; int res, num_addr_listen; - int *listen_sockets; + struct listen_endpoint *listen_sockets; #ifdef LIBBSD setproctitle_init(argc, argv, envp); diff --git a/test.cfg b/test.cfg index a958e0a..44a23d9 100644 --- a/test.cfg +++ b/test.cfg @@ -1,7 +1,7 @@ # Configuration file for testing (use both by sslh under # test and the test script `t`) -verbose: 2; +verbose: 3; foreground: true; inetd: false; numeric: false; @@ -17,8 +17,8 @@ syslog_facility: "auth"; listen: ( { host: "localhost"; port: "8080"; keepalive: true; }, - { host: "localhost"; port: "8081"; keepalive: true; }, - { host: "localhost"; is_udp: true; port: "4443"; } + { host: "localhost"; port: "8081"; keepalive: true; } +# { host: "localhost"; is_udp: true; port: "4443"; } );