refactor conns.[ch], put conn_s into child struct

this allows to access the conn member from the main thread handling
the childs, plus simplifies the code.
This commit is contained in:
rofl0r 2020-09-14 21:59:27 +01:00
parent 5779ba8697
commit 0c8275a90e
5 changed files with 37 additions and 57 deletions

View File

@ -33,25 +33,26 @@
#include "conf.h" #include "conf.h"
#include "sblist.h" #include "sblist.h"
#include "loop.h" #include "loop.h"
#include "conns.h"
#include <pthread.h> #include <pthread.h>
static vector_t listen_fds; static vector_t listen_fds;
struct client { struct client {
union sockaddr_union addr; union sockaddr_union addr;
int fd;
}; };
struct child { struct child {
pthread_t thread; pthread_t thread;
struct client client; struct client client;
struct conn_s conn;
volatile int done; volatile int done;
}; };
static void* child_thread(void* data) static void* child_thread(void* data)
{ {
struct child *c = data; struct child *c = data;
handle_connection (c->client.fd, &c->client.addr); handle_connection (&c->conn, &c->client.addr);
c->done = 1; c->done = 1;
return NULL; return NULL;
} }
@ -185,7 +186,7 @@ void child_main_loop (void)
continue; continue;
} }
child = safemalloc(sizeof(struct child)); child = safecalloc(1, sizeof(struct child));
if (!child) { if (!child) {
oom: oom:
close(connfd); close(connfd);
@ -202,7 +203,9 @@ oom:
goto oom; goto oom;
} }
child->client.fd = connfd; conn_struct_init(&child->conn);
child->conn.client_fd = connfd;
memcpy(&child->client.addr, &cliaddr_storage, sizeof(cliaddr_storage)); memcpy(&child->client.addr, &cliaddr_storage, sizeof(cliaddr_storage));
attrp = 0; attrp = 0;
@ -233,7 +236,7 @@ void child_kill_children (int sig)
if (!c->done) { if (!c->done) {
/* interrupt blocking operations. /* interrupt blocking operations.
this should cause the threads to shutdown orderly. */ this should cause the threads to shutdown orderly. */
close(c->client.fd); close(c->conn.client_fd);
} }
} }
usleep(16); usleep(16);

View File

@ -30,13 +30,20 @@
#include "log.h" #include "log.h"
#include "stats.h" #include "stats.h"
struct conn_s *initialize_conn (int client_fd, const char *ipaddr, void conn_struct_init(struct conn_s *connptr) {
connptr->error_number = -1;
connptr->client_fd = -1;
connptr->server_fd = -1;
/* There is _no_ content length initially */
connptr->content_length.server = connptr->content_length.client = -1;
}
int conn_init_contents (struct conn_s *connptr, const char *ipaddr,
const char *sock_ipaddr) const char *sock_ipaddr)
{ {
struct conn_s *connptr;
struct buffer_s *cbuffer, *sbuffer; struct buffer_s *cbuffer, *sbuffer;
assert (client_fd >= 0); assert (connptr->client_fd >= 0);
/* /*
* Allocate the memory for all the internal components * Allocate the memory for all the internal components
@ -47,47 +54,16 @@ struct conn_s *initialize_conn (int client_fd, const char *ipaddr,
if (!cbuffer || !sbuffer) if (!cbuffer || !sbuffer)
goto error_exit; goto error_exit;
/*
* Allocate the space for the conn_s structure itself.
*/
connptr = (struct conn_s *) safemalloc (sizeof (struct conn_s));
if (!connptr)
goto error_exit;
connptr->client_fd = client_fd;
connptr->server_fd = -1;
connptr->cbuffer = cbuffer; connptr->cbuffer = cbuffer;
connptr->sbuffer = sbuffer; connptr->sbuffer = sbuffer;
connptr->request_line = NULL;
/* These store any error strings */
connptr->error_variables = NULL;
connptr->error_string = NULL;
connptr->error_number = -1;
connptr->connect_method = FALSE;
connptr->show_stats = FALSE;
connptr->protocol.major = connptr->protocol.minor = 0;
/* There is _no_ content length initially */
connptr->content_length.server = connptr->content_length.client = -1;
connptr->server_ip_addr = (sock_ipaddr ? connptr->server_ip_addr = (sock_ipaddr ?
safestrdup (sock_ipaddr) : NULL); safestrdup (sock_ipaddr) : NULL);
connptr->client_ip_addr = safestrdup (ipaddr); connptr->client_ip_addr = safestrdup (ipaddr);
connptr->upstream_proxy = NULL;
update_stats (STAT_OPEN); update_stats (STAT_OPEN);
#ifdef REVERSE_SUPPORT return 1;
connptr->reversepath = NULL;
#endif
return connptr;
error_exit: error_exit:
/* /*
@ -98,10 +74,10 @@ error_exit:
if (sbuffer) if (sbuffer)
delete_buffer (sbuffer); delete_buffer (sbuffer);
return NULL; return 0;
} }
void destroy_conn (struct conn_s *connptr) void conn_destroy_contents (struct conn_s *connptr)
{ {
assert (connptr != NULL); assert (connptr != NULL);
@ -109,10 +85,12 @@ void destroy_conn (struct conn_s *connptr)
if (close (connptr->client_fd) < 0) if (close (connptr->client_fd) < 0)
log_message (LOG_INFO, "Client (%d) close message: %s", log_message (LOG_INFO, "Client (%d) close message: %s",
connptr->client_fd, strerror (errno)); connptr->client_fd, strerror (errno));
connptr->client_fd = -1;
if (connptr->server_fd != -1) if (connptr->server_fd != -1)
if (close (connptr->server_fd) < 0) if (close (connptr->server_fd) < 0)
log_message (LOG_INFO, "Server (%d) close message: %s", log_message (LOG_INFO, "Server (%d) close message: %s",
connptr->server_fd, strerror (errno)); connptr->server_fd, strerror (errno));
connptr->server_fd = -1;
if (connptr->cbuffer) if (connptr->cbuffer)
delete_buffer (connptr->cbuffer); delete_buffer (connptr->cbuffer);
@ -146,7 +124,5 @@ void destroy_conn (struct conn_s *connptr)
safefree (connptr->reversepath); safefree (connptr->reversepath);
#endif #endif
safefree (connptr);
update_stats (STAT_CLOSE); update_stats (STAT_CLOSE);
} }

View File

@ -87,11 +87,13 @@ struct conn_s {
struct upstream *upstream_proxy; struct upstream *upstream_proxy;
}; };
/* /* expects pointer to zero-initialized struct, set up struct
* Functions for the creation and destruction of a connection structure. with default values for initial use */
*/ extern void conn_struct_init(struct conn_s *connptr);
extern struct conn_s *initialize_conn (int client_fd, const char *ipaddr,
/* second stage initializiation, sets up buffers and connection details */
extern int conn_init_contents (struct conn_s *connptr, const char *ipaddr,
const char *sock_ipaddr); const char *sock_ipaddr);
extern void destroy_conn (struct conn_s *connptr); extern void conn_destroy_contents (struct conn_s *connptr);
#endif #endif

View File

@ -1509,16 +1509,15 @@ static void handle_connection_failure(struct conn_s *connptr, int got_headers)
* tinyproxy code, which was confusing, redundant. Hail progress. * tinyproxy code, which was confusing, redundant. Hail progress.
* - rjkaes * - rjkaes
*/ */
void handle_connection (int fd, union sockaddr_union* addr) void handle_connection (struct conn_s *connptr, union sockaddr_union* addr)
{ {
#define HC_FAIL() \ #define HC_FAIL() \
do {handle_connection_failure(connptr, got_headers); goto done;} \ do {handle_connection_failure(connptr, got_headers); goto done;} \
while(0) while(0)
int got_headers = 0; int got_headers = 0, fd = connptr->client_fd;
ssize_t i; ssize_t i;
struct conn_s *connptr;
struct request_s *request = NULL; struct request_s *request = NULL;
struct timeval tv; struct timeval tv;
orderedmap hashofheaders = NULL; orderedmap hashofheaders = NULL;
@ -1536,9 +1535,8 @@ void handle_connection (int fd, union sockaddr_union* addr)
"Connect (file descriptor %d): %s", "Connect (file descriptor %d): %s",
fd, peer_ipaddr, sock_ipaddr); fd, peer_ipaddr, sock_ipaddr);
connptr = initialize_conn (fd, peer_ipaddr, if(!conn_init_contents (connptr, peer_ipaddr,
config->bindsame ? sock_ipaddr : NULL); config->bindsame ? sock_ipaddr : NULL)) {
if (!connptr) {
close (fd); close (fd);
return; return;
} }
@ -1739,7 +1737,7 @@ e401:
done: done:
free_request_struct (request); free_request_struct (request);
orderedmap_destroy (hashofheaders); orderedmap_destroy (hashofheaders);
destroy_conn (connptr); conn_destroy_contents (connptr);
return; return;
#undef HC_FAIL #undef HC_FAIL
} }

View File

@ -24,6 +24,7 @@
#include "common.h" #include "common.h"
#include "sock.h" #include "sock.h"
#include "conns.h"
/* /*
* Port constants for HTTP (80) and SSL (443) * Port constants for HTTP (80) and SSL (443)
@ -44,6 +45,6 @@ struct request_s {
char *path; char *path;
}; };
extern void handle_connection (int fd, union sockaddr_union* addr); extern void handle_connection (struct conn_s *, union sockaddr_union* addr);
#endif #endif