refactor: connect_addr() update the *cnx object upon connecting to backend server, instead of each caller doing it

This commit is contained in:
Yves Rutschle 2025-03-09 09:43:41 +01:00
parent 7a6673a877
commit 5a0897c5cb
5 changed files with 15 additions and 21 deletions

View File

@ -477,11 +477,12 @@ static int connect_unix(struct connection *cnx, int fd_from, connect_blocking bl
return fd; 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 * If transparent proxying is on, use fd_from peer address on external address
* of new file descriptor. */ * 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; int fd;
@ -490,8 +491,7 @@ int connect_addr(struct connection *cnx, int fd_from, connect_blocking blocking)
} else { } else {
fd = connect_inet(cnx, fd_from, blocking); fd = connect_inet(cnx, fd_from, blocking);
} }
cnx->q[1].fd = fd;
return fd;
} }
/* Store some data to write to the queue later */ /* Store some data to write to the queue later */

View File

@ -156,7 +156,7 @@ typedef enum {
/* common.c */ /* common.c */
void init_cnx(struct connection *cnx); void init_cnx(struct connection *cnx);
int set_nonblock(int fd); 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); int fd2fd(struct queue *target, struct queue *from);
char* sprintaddr(char* buf, size_t size, struct addrinfo *a); char* sprintaddr(char* buf, size_t size, struct addrinfo *a);
void resolve_name(struct addrinfo **out, char* fullname); void resolve_name(struct addrinfo **out, char* fullname);

View File

@ -74,7 +74,6 @@ void start_shoveler(int in_socket)
fd_set fds; fd_set fds;
struct timeval tv; struct timeval tv;
int res = PROBE_AGAIN; int res = PROBE_AGAIN;
int out_socket;
struct connection cnx; struct connection cnx;
struct connection_desc desc; struct connection_desc desc;
@ -110,13 +109,11 @@ void start_shoveler(int in_socket)
} }
/* Connect the target socket */ /* Connect the target socket */
out_socket = connect_addr(&cnx, in_socket, BLOCKING); connect_addr(&cnx, in_socket, BLOCKING);
CHECK_RES_DIE(out_socket, "connect"); CHECK_RES_DIE(cnx.q[1].fd, "connect");
set_capabilities(0); set_capabilities(0);
cnx.q[1].fd = out_socket;
get_connection_desc(&desc, &cnx); get_connection_desc(&desc, &cnx);
log_connection(&desc, &cnx); log_connection(&desc, &cnx);
set_proctitle_shovel(&desc, &cnx); set_proctitle_shovel(&desc, &cnx);
@ -126,7 +123,7 @@ void start_shoveler(int in_socket)
shovel(&cnx); shovel(&cnx);
close(in_socket); close(in_socket);
close(out_socket); close(cnx.q[1].fd);
print_message(msg_fd, "connection closed down\n"); print_message(msg_fd, "connection closed down\n");

View File

@ -153,7 +153,7 @@ static int connect_queue(struct connection* cnx,
{ {
struct queue *q = &cnx->q[1]; 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) { if (q->fd != -1) {
log_connection(NULL, cnx); log_connection(NULL, cnx);
flush_deferred(q); flush_deferred(q);
@ -227,7 +227,6 @@ static void shovel_single(struct connection *cnx)
static void connect_proxy(struct connection *cnx) static void connect_proxy(struct connection *cnx)
{ {
int in_socket; int in_socket;
int out_socket;
/* Minimize the file descriptor value to help select() */ /* Minimize the file descriptor value to help select() */
in_socket = dup(cnx->q[0].fd); in_socket = dup(cnx->q[0].fd);
@ -238,18 +237,16 @@ static void connect_proxy(struct connection *cnx)
cnx->q[0].fd = in_socket; cnx->q[0].fd = in_socket;
} }
/* Connect the target socket */ /* Connect the backend server socket */
out_socket = connect_addr(cnx, in_socket, BLOCKING); connect_addr(cnx, in_socket, BLOCKING);
CHECK_RES_DIE(out_socket, "connect"); CHECK_RES_DIE(cnx->q[1].fd, "connect");
cnx->q[1].fd = out_socket;
log_connection(NULL, cnx); log_connection(NULL, cnx);
shovel_single(cnx); shovel_single(cnx);
close(in_socket); close(in_socket);
close(out_socket); close(cnx->q[1].fd);
print_message(msg_fd, "connection closed down\n"); print_message(msg_fd, "connection closed down\n");

View File

@ -1,5 +1,5 @@
#ifndef VERSION_H #ifndef VERSION_H
#define VERSION_H #define VERSION_H
#define VERSION "v2.1.4-29-ge527b8e-dirty" #define VERSION "v2.1.4-35-g7a6673a-dirty"
#endif #endif