From: Guus Sliepen Date: Tue, 26 Feb 2002 23:26:41 +0000 (+0000) Subject: Allow multiple listening sockets. X-Git-Tag: release-1.0pre6~69 X-Git-Url: https://www.tinc-vpn.org/git/browse?p=tinc;a=commitdiff_plain;h=50403909b6bf6536924d4693bb1f32c248f17fda Allow multiple listening sockets. --- diff --git a/src/net.c b/src/net.c index e42ccf4a..fb70dee4 100644 --- a/src/net.c +++ b/src/net.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net.c,v 1.35.4.157 2002/02/20 16:04:07 guus Exp $ + $Id: net.c,v 1.35.4.158 2002/02/26 23:26:41 guus Exp $ */ #include "config.h" @@ -80,6 +80,7 @@ void build_fdset(fd_set *fs) { avl_node_t *node; connection_t *c; + int i; cp FD_ZERO(fs); @@ -89,8 +90,12 @@ cp FD_SET(c->socket, fs); } - FD_SET(tcp_socket, fs); - FD_SET(udp_socket, fs); + for(i = 0; i < tcp_sockets; i++) + FD_SET(tcp_socket[i], fs); + + for(i = 0; i < udp_sockets; i++) + FD_SET(udp_socket[i], fs); + FD_SET(device_fd, fs); cp } @@ -271,7 +276,7 @@ void check_network_activity(fd_set *f) { connection_t *c; avl_node_t *node; - int result; + int result, i; int len = sizeof(result); vpn_packet_t packet; cp @@ -281,8 +286,9 @@ cp route_outgoing(&packet); } - if(FD_ISSET(udp_socket, f)) - handle_incoming_vpn_data(); + for(i = 0; i < udp_sockets; i++) + if(FD_ISSET(udp_socket[i], f)) + handle_incoming_vpn_data(udp_socket[i]); for(node = connection_tree->head; node; node = node->next) { @@ -316,8 +322,9 @@ cp } } - if(FD_ISSET(tcp_socket, f)) - handle_new_meta_connection(); + for(i = 0; i < tcp_sockets; i++) + if(FD_ISSET(tcp_socket[i], f)) + handle_new_meta_connection(tcp_socket[i]); cp } diff --git a/src/net.h b/src/net.h index df431979..e42f07e5 100644 --- a/src/net.h +++ b/src/net.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net.h,v 1.9.4.42 2002/02/26 22:47:51 guus Exp $ + $Id: net.h,v 1.9.4.43 2002/02/26 23:26:41 guus Exp $ */ #ifndef __TINC_NET_H__ @@ -34,6 +34,7 @@ #define MAXSIZE 1600 /* MTU + header (seqno) and trailer (CBC padding and HMAC) */ #define MAXBUFSIZE 2048 /* Probably way too much, but it must fit every possible request. */ +#define MAXSOCKETS 128 /* Overkill... */ typedef struct mac_t { @@ -105,8 +106,10 @@ extern char *status_text[]; #include "connection.h" /* Yes, very strange placement indeed, but otherwise the typedefs get all tangled up */ -extern int tcp_socket; -extern int udp_socket; +extern int tcp_socket[MAXSOCKETS]; +extern int udp_socket[MAXSOCKETS]; +extern int tcp_sockets; +extern int udp_sockets; extern int keyexpires; extern int keylifetime; extern int do_prune; @@ -114,10 +117,10 @@ extern int do_purge; extern char *myport; extern void retry_outgoing(outgoing_t *); -extern void handle_incoming_vpn_data(void); +extern void handle_incoming_vpn_data(int); extern void finish_connecting(connection_t *); extern void do_outgoing_connection(connection_t *); -extern int handle_new_meta_connection(void); +extern int handle_new_meta_connection(int); extern int setup_listen_socket(sockaddr_t *); extern int setup_vpn_in_socket(sockaddr_t *); extern void send_packet(struct node_t *, vpn_packet_t *); diff --git a/src/net_packet.c b/src/net_packet.c index 42f9d890..725789de 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net_packet.c,v 1.1.2.4 2002/02/20 22:37:38 guus Exp $ + $Id: net_packet.c,v 1.1.2.5 2002/02/26 23:26:41 guus Exp $ */ #include "config.h" @@ -258,7 +258,7 @@ cp /* Send the packet */ - if((sendto(udp_socket, (char *)&inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) + if((sendto(udp_socket[0], (char *)&inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) { syslog(LOG_ERR, _("Error sending packet to %s (%s): %s"), n->name, n->hostname, strerror(errno)); @@ -349,7 +349,7 @@ cp cp } -void handle_incoming_vpn_data(void) +void handle_incoming_vpn_data(int sock) { vpn_packet_t pkt; int x, l = sizeof(x); @@ -358,10 +358,10 @@ void handle_incoming_vpn_data(void) socklen_t fromlen = sizeof(from); node_t *n; cp - if(getsockopt(udp_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0) + if(getsockopt(sock, SOL_SOCKET, SO_ERROR, &x, &l) < 0) { syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%s"), - __FILE__, __LINE__, udp_socket, strerror(errno)); + __FILE__, __LINE__, sock, strerror(errno)); return; } if(x) @@ -370,7 +370,7 @@ cp return; } - if((pkt.len = recvfrom(udp_socket, (char *)&pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen)) <= 0) + if((pkt.len = recvfrom(sock, (char *)&pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen)) <= 0) { syslog(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno)); return; diff --git a/src/net_setup.c b/src/net_setup.c index 93939540..a985b555 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net_setup.c,v 1.1.2.4 2002/02/20 22:15:32 guus Exp $ + $Id: net_setup.c,v 1.1.2.5 2002/02/26 23:26:41 guus Exp $ */ #include "config.h" @@ -219,9 +219,9 @@ int setup_myself(void) { config_t *cfg; subnet_t *subnet; - char *name, *mode, *afname, *cipher, *digest; - struct addrinfo hint, *ai; - int choice, err; + char *name, *hostname, *mode, *afname, *cipher, *digest; + struct addrinfo hint, *ai, *aip; + int choice, err, sock; cp myself = new_node(); myself->connection = new_connection(); @@ -475,10 +475,20 @@ cp return -1; } - if((tcp_socket = setup_listen_socket((sockaddr_t *)ai->ai_addr)) < 0) + tcp_sockets = 0; + + for(aip = ai; aip; aip = aip->ai_next) { - syslog(LOG_ERR, _("Unable to set up a listening TCP socket!")); - return -1; + if((sock = setup_listen_socket((sockaddr_t *)aip->ai_addr)) < 0) + continue; + + tcp_socket[++tcp_sockets] = sock; + if(debug_lvl >= DEBUG_CONNECTIONS) + { + hostname = sockaddr2hostname((sockaddr_t *)aip->ai_addr); + syslog(LOG_NOTICE, _("Listening on %s/tcp"), hostname); + free(hostname); + } } freeaddrinfo(ai); @@ -492,15 +502,25 @@ cp return -1; } - if((udp_socket = setup_vpn_in_socket((sockaddr_t *)ai->ai_addr)) < 0) + udp_sockets = 0; + + for(aip = ai; aip; aip = aip->ai_next) { - syslog(LOG_ERR, _("Unable to set up a listening UDP socket!")); - return -1; + if((sock = setup_vpn_in_socket((sockaddr_t *)aip->ai_addr)) < 0) + continue; + + udp_socket[++udp_sockets] = sock; + if(debug_lvl >= DEBUG_CONNECTIONS) + { + hostname = sockaddr2hostname((sockaddr_t *)aip->ai_addr); + syslog(LOG_NOTICE, _("Listening on %s/udp"), hostname); + free(hostname); + } } freeaddrinfo(ai); - syslog(LOG_NOTICE, _("Ready: listening on port %s"), myport); + syslog(LOG_NOTICE, _("Ready")); cp return 0; } @@ -548,6 +568,7 @@ void close_network_connections(void) { avl_node_t *node, *next; connection_t *c; + int i; cp for(node = connection_tree->head; node; node = next) { @@ -561,8 +582,10 @@ cp if(myself && myself->connection) terminate_connection(myself->connection, 0); - close(udp_socket); - close(tcp_socket); + for(i = 0; i < udp_sockets; i++) + close(udp_socket[i]); + for(i = 0; i < tcp_sockets; i++) + close(tcp_socket[i]); exit_events(); exit_edges(); diff --git a/src/net_socket.c b/src/net_socket.c index 6cf69f22..757199d6 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net_socket.c,v 1.1.2.3 2002/02/20 22:37:38 guus Exp $ + $Id: net_socket.c,v 1.1.2.4 2002/02/26 23:26:41 guus Exp $ */ #include "config.h" @@ -70,8 +70,11 @@ int addressfamily = AF_INET; int maxtimeout = 900; int seconds_till_retry = 5; -int tcp_socket = -1; -int udp_socket = -1; +int tcp_socket[MAXSOCKETS]; +int udp_socket[MAXSOCKETS]; +int tcp_sockets = 0; +int udp_sockets = 0; + /* Setup sockets */ int setup_listen_socket(sockaddr_t *sa) @@ -406,13 +409,13 @@ cp accept a new tcp connect and create a new connection */ -int handle_new_meta_connection() +int handle_new_meta_connection(int sock) { connection_t *c; sockaddr_t sa; int fd, len = sizeof(sa); cp - if((fd = accept(tcp_socket, &sa.sa, &len)) < 0) + if((fd = accept(sock, &sa.sa, &len)) < 0) { syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno)); return -1; diff --git a/src/protocol.h b/src/protocol.h index e1c8ca9a..c265502a 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: protocol.h,v 1.5.4.26 2002/02/11 15:59:18 guus Exp $ + $Id: protocol.h,v 1.5.4.27 2002/02/26 23:26:41 guus Exp $ */ #ifndef __TINC_PROTOCOL_H__ @@ -50,8 +50,8 @@ enum { /* Maximum size of strings in a request */ -#define MAX_STRING_SIZE 1024 -#define MAX_STRING "%1024s" +#define MAX_STRING_SIZE 2048 +#define MAX_STRING "%2048s" /* Basic functions */