From 5a0897c5cbf9bbe8147e9508cd34ff13d535238f Mon Sep 17 00:00:00 2001 From: Yves Rutschle Date: Sun, 9 Mar 2025 09:43:41 +0100 Subject: [PATCH] refactor: connect_addr() update the *cnx object upon connecting to backend server, instead of each caller doing it --- common.c | 10 +++++----- common.h | 2 +- sslh-fork.c | 9 +++------ tcp-listener.c | 13 +++++-------- version.h | 2 +- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/common.c b/common.c index d1ab402..4bea124 100644 --- a/common.c +++ b/common.c @@ -477,11 +477,12 @@ static int connect_unix(struct connection *cnx, int fd_from, connect_blocking bl return fd; } -/* Connect to first address that works and returns a file descriptor, or -1 if - * none work. +/* + * Connect to the first backend server address that works and updates the *cnx + * object accordingly (in cnx->q[1].fd). Set that to -1 in case of failure. * If transparent proxying is on, use fd_from peer address on external address * of new file descriptor. */ -int connect_addr(struct connection *cnx, int fd_from, connect_blocking blocking) +void connect_addr(struct connection *cnx, int fd_from, connect_blocking blocking) { int fd; @@ -490,8 +491,7 @@ int connect_addr(struct connection *cnx, int fd_from, connect_blocking blocking) } else { fd = connect_inet(cnx, fd_from, blocking); } - - return fd; + cnx->q[1].fd = fd; } /* Store some data to write to the queue later */ diff --git a/common.h b/common.h index d186015..762f49f 100644 --- a/common.h +++ b/common.h @@ -156,7 +156,7 @@ typedef enum { /* common.c */ void init_cnx(struct connection *cnx); int set_nonblock(int fd); -int connect_addr(struct connection *cnx, int fd_from, connect_blocking blocking); +void connect_addr(struct connection *cnx, int fd_from, connect_blocking blocking); int fd2fd(struct queue *target, struct queue *from); char* sprintaddr(char* buf, size_t size, struct addrinfo *a); void resolve_name(struct addrinfo **out, char* fullname); diff --git a/sslh-fork.c b/sslh-fork.c index 4cd7888..27cc0b5 100644 --- a/sslh-fork.c +++ b/sslh-fork.c @@ -74,7 +74,6 @@ void start_shoveler(int in_socket) fd_set fds; struct timeval tv; int res = PROBE_AGAIN; - int out_socket; struct connection cnx; struct connection_desc desc; @@ -110,13 +109,11 @@ void start_shoveler(int in_socket) } /* Connect the target socket */ - out_socket = connect_addr(&cnx, in_socket, BLOCKING); - CHECK_RES_DIE(out_socket, "connect"); + connect_addr(&cnx, in_socket, BLOCKING); + CHECK_RES_DIE(cnx.q[1].fd, "connect"); set_capabilities(0); - cnx.q[1].fd = out_socket; - get_connection_desc(&desc, &cnx); log_connection(&desc, &cnx); set_proctitle_shovel(&desc, &cnx); @@ -126,7 +123,7 @@ void start_shoveler(int in_socket) shovel(&cnx); close(in_socket); - close(out_socket); + close(cnx.q[1].fd); print_message(msg_fd, "connection closed down\n"); diff --git a/tcp-listener.c b/tcp-listener.c index 945f619..7110dbe 100644 --- a/tcp-listener.c +++ b/tcp-listener.c @@ -153,7 +153,7 @@ static int connect_queue(struct connection* cnx, { struct queue *q = &cnx->q[1]; - q->fd = connect_addr(cnx, cnx->q[0].fd, NON_BLOCKING); + connect_addr(cnx, cnx->q[0].fd, NON_BLOCKING); if (q->fd != -1) { log_connection(NULL, cnx); flush_deferred(q); @@ -227,7 +227,6 @@ static void shovel_single(struct connection *cnx) static void connect_proxy(struct connection *cnx) { int in_socket; - int out_socket; /* Minimize the file descriptor value to help select() */ in_socket = dup(cnx->q[0].fd); @@ -238,18 +237,16 @@ static void connect_proxy(struct connection *cnx) cnx->q[0].fd = in_socket; } - /* Connect the target socket */ - out_socket = connect_addr(cnx, in_socket, BLOCKING); - CHECK_RES_DIE(out_socket, "connect"); - - cnx->q[1].fd = out_socket; + /* Connect the backend server socket */ + connect_addr(cnx, in_socket, BLOCKING); + CHECK_RES_DIE(cnx->q[1].fd, "connect"); log_connection(NULL, cnx); shovel_single(cnx); close(in_socket); - close(out_socket); + close(cnx->q[1].fd); print_message(msg_fd, "connection closed down\n"); diff --git a/version.h b/version.h index 2bfe6b1..0768109 100644 --- a/version.h +++ b/version.h @@ -1,5 +1,5 @@ #ifndef VERSION_H #define VERSION_H -#define VERSION "v2.1.4-29-ge527b8e-dirty" +#define VERSION "v2.1.4-35-g7a6673a-dirty" #endif