mirror of
https://github.com/yrutschle/sslh.git
synced 2025-04-04 19:30:04 +03:00
refactor: connect_addr() update the *cnx object upon connecting to backend server, instead of each caller doing it
This commit is contained in:
parent
7a6673a877
commit
5a0897c5cb
10
common.c
10
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 */
|
||||
|
2
common.h
2
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);
|
||||
|
@ -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");
|
||||
|
||||
|
@ -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");
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user