Use threads for TCP connections.
authorGuus Sliepen <guus@tinc-vpn.org>
Fri, 14 Jan 2011 16:09:29 +0000 (17:09 +0100)
committerGuus Sliepen <guus@tinc-vpn.org>
Fri, 14 Jan 2011 16:09:29 +0000 (17:09 +0100)
This is a minimal conversion which does not yet get rid of bufferevents and
evbuffers in connection_ts.

src/connection.c
src/connection.h
src/meta.c
src/net.c
src/net.h
src/net_socket.c

index fecb48d42d141d37a1830e039fd91e8d629736d9..cf5ec1b60507d7815c1d6a730eb3a76b0e356b14 100644 (file)
@@ -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);
 }
index 0fc49ef38d4fe19228d191d0db37b5707474d302..ae1af9fde8785d448598f9abd4b15066396925a7 100644 (file)
@@ -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 */
 
index a3f7ef3cfa1f38c7fae38badbfcc11f28eb641a9..1e4eb7fe4d1c1f9024ab57e40bf572e811f1118f 100644 (file)
@@ -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.");
        }
 
index 7d44d17c378f0bbbf2821bd6b4432dd43f5d6705..dbff75b9b1cfc95ced7c6522902ba5c9f17a5049 100644 (file)
--- 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;
+               }
        }
 }
 
index a6d5cb965b822749ad7a35465aad1a81ca25df00..66e29642fda170ff84969f31cd1cdd40d822c980 100644 (file)
--- 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);
index 7c4463eaab1d28089f259dbb380bdfb829a0b504..4316e2d6c86a2b8b73e35668191128f1cc27fb26 100644 (file)
@@ -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) {