Use threads for TCP listening sockets.
authorGuus Sliepen <guus@tinc-vpn.org>
Fri, 14 Jan 2011 17:03:09 +0000 (18:03 +0100)
committerGuus Sliepen <guus@tinc-vpn.org>
Fri, 14 Jan 2011 17:03:09 +0000 (18:03 +0100)
src/net.h
src/net_setup.c
src/net_socket.c

index 66e29642fda170ff84969f31cd1cdd40d822c980..68cca02ee08704c7344a32f8a579a00ce4dd9a19 100644 (file)
--- a/src/net.h
+++ b/src/net.h
@@ -85,10 +85,9 @@ typedef struct vpn_packet_t {
 } vpn_packet_t;
 
 typedef struct listen_socket_t {
-       struct event ev_tcp;
-       struct event ev_udp;
        int tcp;
        int udp;
+       thread_t tcp_thread;
        thread_t udp_thread;
        sockaddr_t sa;
 } listen_socket_t;
@@ -130,7 +129,7 @@ extern void retry_outgoing(outgoing_t *);
 extern void handle_incoming_vpn_data(void *);
 extern void finish_connecting(struct connection_t *);
 extern void do_outgoing_connection(struct connection_t *);
-extern void handle_new_meta_connection(int, short, void *);
+extern void handle_new_meta_connection(void *);
 extern int setup_listen_socket(const sockaddr_t *);
 extern int setup_vpn_in_socket(const sockaddr_t *);
 extern void send_packet(const struct node_t *, vpn_packet_t *);
index dc1d665d2c0dba1f3627d561512094d79ba6d7bd..73d4f7752d982daacd438150a8261fff910a0e64 100644 (file)
@@ -520,12 +520,10 @@ bool setup_myself(void) {
                        continue;
                }
 
-               event_set(&listen_socket[listen_sockets].ev_tcp,
-                                 listen_socket[listen_sockets].tcp,
-                                 EV_READ|EV_PERSIST,
-                                 handle_new_meta_connection, NULL);
-               if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
-                       logger(LOG_ERR, "event_add failed: %s", strerror(errno));
+               memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
+
+               if(!thread_create(&listen_socket[listen_sockets].tcp_thread, handle_new_meta_connection, &listen_socket[listen_sockets])) {
+                       logger(LOG_ERR, "thread_create failed: %s", strerror(errno));
                        abort();
                }
 
@@ -540,7 +538,6 @@ bool setup_myself(void) {
                        free(hostname);
                }
 
-               memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
                listen_sockets++;
 
                if(listen_sockets >= MAXSOCKETS) {
@@ -617,10 +614,9 @@ void close_network_connections(void) {
        }
 
        for(i = 0; i < listen_sockets; i++) {
-               event_del(&listen_socket[i].ev_tcp);
-               event_del(&listen_socket[i].ev_udp);
                close(listen_socket[i].tcp);
                close(listen_socket[i].udp);
+               thread_destroy(&listen_socket[i].tcp_thread);
                thread_destroy(&listen_socket[i].udp_thread);
        }
 
index ca681e854577919f68a98f5d80e6b83beaf995b7..8a733896780ea7c589f53125d81482c888903c61 100644 (file)
@@ -464,45 +464,48 @@ void setup_outgoing_connection(outgoing_t *outgoing) {
   accept a new tcp connect and create a
   new connection
 */
-void handle_new_meta_connection(int sock, short events, void *data) {
+void handle_new_meta_connection(void *data) {
+       listen_socket_t *l = data;
        connection_t *c;
        sockaddr_t sa;
        int fd;
        socklen_t len = sizeof sa;
 
-       fd = accept(sock, &sa.sa, &len);
+       while(true) {
+               fd = accept(l->tcp, &sa.sa, &len);
 
-       if(fd < 0) {
-               logger(LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
-               return;
-       }
+               if(fd < 0) {
+                       logger(LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
+                       return;
+               }
 
-       sockaddrunmap(&sa);
+               sockaddrunmap(&sa);
 
-       c = new_connection();
-       c->name = xstrdup("<unknown>");
-       c->outcipher = myself->connection->outcipher;
-       c->outdigest = myself->connection->outdigest;
-       c->outmaclength = myself->connection->outmaclength;
-       c->outcompression = myself->connection->outcompression;
+               c = new_connection();
+               c->name = xstrdup("<unknown>");
+               c->outcipher = myself->connection->outcipher;
+               c->outdigest = myself->connection->outdigest;
+               c->outmaclength = myself->connection->outmaclength;
+               c->outcompression = myself->connection->outcompression;
 
-       c->address = sa;
-       c->hostname = sockaddr2hostname(&sa);
-       c->socket = fd;
-       c->last_ping_time = time(NULL);
+               c->address = sa;
+               c->hostname = sockaddr2hostname(&sa);
+               c->socket = fd;
+               c->last_ping_time = time(NULL);
 
-       ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection from %s", c->hostname);
+               ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection from %s", c->hostname);
 
-       configure_tcp(c);
+               configure_tcp(c);
 
-       connection_add(c);
+               connection_add(c);
 
-       c->allow_request = ID;
-       send_id(c);
+               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();
+               if(!thread_create(&c->thread, handle_meta_connection_data, c)) {
+                       logger(LOG_ERR, "create_thread() failed: %s", strerror(errno));
+                       abort();
+               }
        }
 }