summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/child.c13
-rw-r--r--src/conns.c54
-rw-r--r--src/conns.h12
-rw-r--r--src/reqs.c12
-rw-r--r--src/reqs.h3
5 files changed, 37 insertions, 57 deletions
diff --git a/src/child.c b/src/child.c
index 3b1800f..107475b 100644
--- a/src/child.c
+++ b/src/child.c
@@ -33,25 +33,26 @@
#include "conf.h"
#include "sblist.h"
#include "loop.h"
+#include "conns.h"
#include <pthread.h>
static vector_t listen_fds;
struct client {
union sockaddr_union addr;
- int fd;
};
struct child {
pthread_t thread;
struct client client;
+ struct conn_s conn;
volatile int done;
};
static void* child_thread(void* data)
{
struct child *c = data;
- handle_connection (c->client.fd, &c->client.addr);
+ handle_connection (&c->conn, &c->client.addr);
c->done = 1;
return NULL;
}
@@ -185,7 +186,7 @@ void child_main_loop (void)
continue;
}
- child = safemalloc(sizeof(struct child));
+ child = safecalloc(1, sizeof(struct child));
if (!child) {
oom:
close(connfd);
@@ -202,7 +203,9 @@ 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));
attrp = 0;
@@ -233,7 +236,7 @@ void child_kill_children (int sig)
if (!c->done) {
/* interrupt blocking operations.
this should cause the threads to shutdown orderly. */
- close(c->client.fd);
+ close(c->conn.client_fd);
}
}
usleep(16);
diff --git a/src/conns.c b/src/conns.c
index c6c267f..19aaa49 100644
--- a/src/conns.c
+++ b/src/conns.c
@@ -30,13 +30,20 @@
#include "log.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)
{
- struct conn_s *connptr;
struct buffer_s *cbuffer, *sbuffer;
- assert (client_fd >= 0);
+ assert (connptr->client_fd >= 0);
/*
* 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)
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->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 ?
safestrdup (sock_ipaddr) : NULL);
connptr->client_ip_addr = safestrdup (ipaddr);
- connptr->upstream_proxy = NULL;
-
update_stats (STAT_OPEN);
-#ifdef REVERSE_SUPPORT
- connptr->reversepath = NULL;
-#endif
-
- return connptr;
+ return 1;
error_exit:
/*
@@ -98,10 +74,10 @@ error_exit:
if (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);
@@ -109,10 +85,12 @@ void destroy_conn (struct conn_s *connptr)
if (close (connptr->client_fd) < 0)
log_message (LOG_INFO, "Client (%d) close message: %s",
connptr->client_fd, strerror (errno));
+ connptr->client_fd = -1;
if (connptr->server_fd != -1)
if (close (connptr->server_fd) < 0)
log_message (LOG_INFO, "Server (%d) close message: %s",
connptr->server_fd, strerror (errno));
+ connptr->server_fd = -1;
if (connptr->cbuffer)
delete_buffer (connptr->cbuffer);
@@ -146,7 +124,5 @@ void destroy_conn (struct conn_s *connptr)
safefree (connptr->reversepath);
#endif
- safefree (connptr);
-
update_stats (STAT_CLOSE);
}
diff --git a/src/conns.h b/src/conns.h
index dc1670d..9618efb 100644
--- a/src/conns.h
+++ b/src/conns.h
@@ -87,11 +87,13 @@ struct conn_s {
struct upstream *upstream_proxy;
};
-/*
- * Functions for the creation and destruction of a connection structure.
- */
-extern struct conn_s *initialize_conn (int client_fd, const char *ipaddr,
+/* expects pointer to zero-initialized struct, set up struct
+ with default values for initial use */
+extern void conn_struct_init(struct conn_s *connptr);
+
+/* 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);
-extern void destroy_conn (struct conn_s *connptr);
+extern void conn_destroy_contents (struct conn_s *connptr);
#endif
diff --git a/src/reqs.c b/src/reqs.c
index b962153..2b7649e 100644
--- a/src/reqs.c
+++ b/src/reqs.c
@@ -1509,16 +1509,15 @@ static void handle_connection_failure(struct conn_s *connptr, int got_headers)
* tinyproxy code, which was confusing, redundant. Hail progress.
* - rjkaes
*/
-void handle_connection (int fd, union sockaddr_union* addr)
+void handle_connection (struct conn_s *connptr, union sockaddr_union* addr)
{
#define HC_FAIL() \
do {handle_connection_failure(connptr, got_headers); goto done;} \
while(0)
- int got_headers = 0;
+ int got_headers = 0, fd = connptr->client_fd;
ssize_t i;
- struct conn_s *connptr;
struct request_s *request = NULL;
struct timeval tv;
orderedmap hashofheaders = NULL;
@@ -1536,9 +1535,8 @@ void handle_connection (int fd, union sockaddr_union* addr)
"Connect (file descriptor %d): %s",
fd, peer_ipaddr, sock_ipaddr);
- connptr = initialize_conn (fd, peer_ipaddr,
- config->bindsame ? sock_ipaddr : NULL);
- if (!connptr) {
+ if(!conn_init_contents (connptr, peer_ipaddr,
+ config->bindsame ? sock_ipaddr : NULL)) {
close (fd);
return;
}
@@ -1739,7 +1737,7 @@ e401:
done:
free_request_struct (request);
orderedmap_destroy (hashofheaders);
- destroy_conn (connptr);
+ conn_destroy_contents (connptr);
return;
#undef HC_FAIL
}
diff --git a/src/reqs.h b/src/reqs.h
index c1c5100..4a0374f 100644
--- a/src/reqs.h
+++ b/src/reqs.h
@@ -24,6 +24,7 @@
#include "common.h"
#include "sock.h"
+#include "conns.h"
/*
* Port constants for HTTP (80) and SSL (443)
@@ -44,6 +45,6 @@ struct request_s {
char *path;
};
-extern void handle_connection (int fd, union sockaddr_union* addr);
+extern void handle_connection (struct conn_s *, union sockaddr_union* addr);
#endif