From: Guus Sliepen Date: Fri, 14 Jan 2011 17:03:09 +0000 (+0100) Subject: Use threads for TCP listening sockets. X-Git-Url: https://tinc-vpn.org/git/browse?a=commitdiff_plain;h=f4851be7f5dade24a5f43d366188c033c0f8b426;p=tinc Use threads for TCP listening sockets. --- diff --git a/src/net.h b/src/net.h index 66e29642..68cca02e 100644 --- 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 *); diff --git a/src/net_setup.c b/src/net_setup.c index dc1d665d..73d4f775 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -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); } diff --git a/src/net_socket.c b/src/net_socket.c index ca681e85..8a733896 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -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(""); - 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(""); + 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(); + } } }