From: Guus Sliepen Date: Fri, 14 Jan 2011 16:09:29 +0000 (+0100) Subject: Use threads for TCP connections. X-Git-Url: https://git.tinc-vpn.org/git/browse?a=commitdiff_plain;h=1b30cee086148975886f961aadc26e23b1bfd6f2;p=tinc Use threads for TCP connections. This is a minimal conversion which does not yet get rid of bufferevents and evbuffers in connection_ts. --- diff --git a/src/connection.c b/src/connection.c index fecb48d4..cf5ec1b6 100644 --- a/src/connection.c +++ b/src/connection.c @@ -78,8 +78,8 @@ void free_connection(connection_t *c) { if(c->buffer) bufferevent_free(c->buffer); - if(event_initialized(&c->inevent)) - event_del(&c->inevent); + if(c->thread) + thread_destroy(&c->thread); free(c); } diff --git a/src/connection.h b/src/connection.h index 0fc49ef3..ae1af9fd 100644 --- a/src/connection.h +++ b/src/connection.h @@ -81,7 +81,7 @@ typedef struct connection_t { char *hischallenge; /* The challenge we sent to him */ struct bufferevent *buffer; /* buffer events on this metadata connection */ - struct event inevent; /* input event on this metadata connection */ + thread_t thread; int tcplen; /* length of incoming TCPpacket */ int allow_request; /* defined if there's only one request possible */ diff --git a/src/meta.c b/src/meta.c index a3f7ef3c..1e4eb7fe 100644 --- a/src/meta.c +++ b/src/meta.c @@ -52,11 +52,11 @@ bool send_meta(connection_t *c, const char *buffer, int length) { } ifdebug(META) logger(LOG_DEBUG, "Encrypted write %p %p %p %d", c, c->buffer, outbuf, length); - bufferevent_write(c->buffer, (void *)outbuf, length); + write(c->socket, outbuf, length); ifdebug(META) logger(LOG_DEBUG, "Done."); } else { ifdebug(META) logger(LOG_DEBUG, "Unencrypted write %p %p %p %d", c, c->buffer, buffer, length); - bufferevent_write(c->buffer, (void *)buffer, length); + write(c->socket, buffer, length); ifdebug(META) logger(LOG_DEBUG, "Done."); } diff --git a/src/net.c b/src/net.c index 7d44d17c..dbff75b9 100644 --- a/src/net.c +++ b/src/net.c @@ -208,7 +208,7 @@ static void timeout_handler(int fd, short events, void *event) { event_add(event, &(struct timeval){pingtimeout, 0}); } -void handle_meta_connection_data(int fd, short events, void *data) { +void handle_meta_connection_data(void *data) { connection_t *c = data; int result; socklen_t len = sizeof result; @@ -230,9 +230,11 @@ void handle_meta_connection_data(int fd, short events, void *data) { } } - if (!receive_meta(c)) { - terminate_connection(c, c->status.active); - return; + while(true) { + if (!receive_meta(c)) { + terminate_connection(c, c->status.active); + return; + } } } diff --git a/src/net.h b/src/net.h index a6d5cb96..66e29642 100644 --- a/src/net.h +++ b/src/net.h @@ -146,7 +146,7 @@ extern void flush_queue(struct node_t *); extern bool read_rsa_public_key(struct connection_t *); extern void send_mtu_probe(struct node_t *); extern void handle_device_data(int, short, void *); -extern void handle_meta_connection_data(int, short, void *); +extern void handle_meta_connection_data(void *); extern void regenerate_key(); extern void purge(void); extern void retry(void); diff --git a/src/net_socket.c b/src/net_socket.c index 7c4463ea..4316e2d6 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -55,20 +55,6 @@ list_t *outgoing_list = NULL; static void configure_tcp(connection_t *c) { int option; -#ifdef O_NONBLOCK - int flags = fcntl(c->socket, F_GETFL); - - if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) { - logger(LOG_ERR, "fcntl for %s: %s", c->hostname, strerror(errno)); - } -#elif defined(WIN32) - unsigned long arg = 1; - - if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) { - logger(LOG_ERR, "ioctlsocket for %s: %d", c->hostname, sockstrerror(sockerrno)); - } -#endif - #if defined(SOL_TCP) && defined(TCP_NODELAY) option = 1; setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&option, sizeof option); @@ -483,14 +469,17 @@ void setup_outgoing_connection(outgoing_t *outgoing) { do_outgoing_connection(c); - event_set(&c->inevent, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c); - event_add(&c->inevent, NULL); c->buffer = bufferevent_new(c->socket, handle_meta_read, handle_meta_write, handle_meta_connection_error, c); if(!c->buffer) { logger(LOG_ERR, "bufferevent_new() failed: %s", strerror(errno)); abort(); } bufferevent_disable(c->buffer, EV_READ); + + if(!thread_create(&c->thread, handle_meta_connection_data, c)) { + logger(LOG_ERR, "create_thread() failed: %s", strerror(errno)); + abort(); + } } /* @@ -526,8 +515,6 @@ void handle_new_meta_connection(int sock, short events, void *data) { ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection from %s", c->hostname); - event_set(&c->inevent, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c); - event_add(&c->inevent, NULL); c->buffer = bufferevent_new(c->socket, NULL, handle_meta_write, handle_meta_connection_error, c); if(!c->buffer) { logger(LOG_ERR, "bufferevent_new() failed: %s", strerror(errno)); @@ -541,6 +528,11 @@ void handle_new_meta_connection(int sock, short events, void *data) { c->allow_request = ID; send_id(c); + + if(!thread_create(&c->thread, handle_meta_connection_data, c)) { + logger(LOG_ERR, "create_thread() failed: %s", strerror(errno)); + abort(); + } } void free_outgoing(outgoing_t *outgoing) {