refactor: use fd_info instead of passing fd_sets directly

This commit is contained in:
yrutschle 2021-03-23 13:59:15 +01:00
parent 5b93e4ab55
commit 6366d50b89

View File

@ -56,9 +56,11 @@ static int set_nonblock(int fd)
return flags; return flags;
} }
static int tidy_connection(struct connection *cnx, fd_set *fds, fd_set *fds2) static int tidy_connection(struct connection *cnx, struct select_info* fd_info)
{ {
int i; int i;
fd_set* fds = &fd_info->fds_r;
fd_set* fds2 = &fd_info->fds_w;
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
if (cnx->q[i].fd != -1) { if (cnx->q[i].fd != -1) {
@ -118,7 +120,7 @@ static int accept_new_connection(int listen_socket, struct cnx_collection *colle
/* Connect queue 1 of connection to SSL; returns new file descriptor */ /* Connect queue 1 of connection to SSL; returns new file descriptor */
static int connect_queue(struct connection *cnx, fd_set *fds_r, fd_set *fds_w) static int connect_queue(struct connection *cnx, struct select_info* fd_info)
{ {
struct queue *q = &cnx->q[1]; struct queue *q = &cnx->q[1];
@ -128,13 +130,13 @@ static int connect_queue(struct connection *cnx, fd_set *fds_r, fd_set *fds_w)
set_nonblock(q->fd); set_nonblock(q->fd);
flush_deferred(q); flush_deferred(q);
if (q->deferred_data) { if (q->deferred_data) {
FD_SET(q->fd, fds_w); FD_SET(q->fd, &fd_info->fds_w);
FD_CLR(cnx->q[0].fd, fds_r); FD_CLR(cnx->q[0].fd, &fd_info->fds_r);
} }
FD_SET(q->fd, fds_r); FD_SET(q->fd, &fd_info->fds_r);
return q->fd; return q->fd;
} else { } else {
tidy_connection(cnx, fds_r, fds_w); tidy_connection(cnx, fd_info);
return -1; return -1;
} }
} }
@ -142,8 +144,7 @@ static int connect_queue(struct connection *cnx, fd_set *fds_r, fd_set *fds_w)
/* shovels data from active fd to the other /* shovels data from active fd to the other
returns after one socket closed or operation would block returns after one socket closed or operation would block
*/ */
static void shovel(struct connection *cnx, int active_fd, static void shovel(struct connection *cnx, int active_fd, struct select_info* fd_info)
fd_set *fds_r, fd_set *fds_w)
{ {
struct queue *read_q, *write_q; struct queue *read_q, *write_q;
@ -156,12 +157,12 @@ static void shovel(struct connection *cnx, int active_fd,
switch(fd2fd(write_q, read_q)) { switch(fd2fd(write_q, read_q)) {
case -1: case -1:
case FD_CNXCLOSED: case FD_CNXCLOSED:
tidy_connection(cnx, fds_r, fds_w); tidy_connection(cnx, fd_info);
break; break;
case FD_STALLED: case FD_STALLED:
FD_SET(write_q->fd, fds_w); FD_SET(write_q->fd, &fd_info->fds_w);
FD_CLR(read_q->fd, fds_r); FD_CLR(read_q->fd, &fd_info->fds_r);
break; break;
default: /* Nothing */ default: /* Nothing */
@ -292,7 +293,7 @@ static void probing_read_process(struct connection* cnx, struct select_info* fd_
/* libwrap check if required for this protocol */ /* libwrap check if required for this protocol */
if (cnx->proto->service && if (cnx->proto->service &&
check_access_rights(cnx->q[0].fd, cnx->proto->service)) { check_access_rights(cnx->q[0].fd, cnx->proto->service)) {
tidy_connection(cnx, &fd_info->fds_r, &fd_info->fds_w); tidy_connection(cnx, fd_info);
res = -1; res = -1;
} else if (cnx->proto->fork) { } else if (cnx->proto->fork) {
switch (fork()) { switch (fork()) {
@ -306,10 +307,10 @@ static void probing_read_process(struct connection* cnx, struct select_info* fd_
default: /* parent */ default: /* parent */
break; break;
} }
tidy_connection(cnx, &fd_info->fds_r, &fd_info->fds_w); tidy_connection(cnx, fd_info);
res = -1; res = -1;
} else { } else {
res = connect_queue(cnx, &fd_info->fds_r, &fd_info->fds_w); res = connect_queue(cnx, fd_info);
} }
if (res >= fd_info->max_fd) if (res >= fd_info->max_fd)
@ -334,7 +335,7 @@ static void cnx_read_process(struct connection* cnx, int active_q, struct select
break; break;
case ST_SHOVELING: case ST_SHOVELING:
shovel(cnx, active_q, &fd_info->fds_r, &fd_info->fds_w); shovel(cnx, active_q, fd_info);
break; break;
default: /* illegal */ default: /* illegal */
@ -418,7 +419,7 @@ void main_loop(struct listen_endpoint listen_sockets[], int num_addr_listen)
res = flush_deferred(&cnx->q[j]); res = flush_deferred(&cnx->q[j]);
if ((res == -1) && ((errno == EPIPE) || (errno == ECONNRESET))) { if ((res == -1) && ((errno == EPIPE) || (errno == ECONNRESET))) {
if (cnx->state == ST_PROBING) fd_info.num_probing--; if (cnx->state == ST_PROBING) fd_info.num_probing--;
tidy_connection(cnx, &fd_info.fds_r, &fd_info.fds_w); tidy_connection(cnx, &fd_info);
if (cfg.verbose) if (cfg.verbose)
fprintf(stderr, "closed slot %d\n", i); fprintf(stderr, "closed slot %d\n", i);
} else { } else {