From 305505f5ec4bb738f175cd897fa409f08d2971a3 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 18 Mar 2002 22:47:20 +0000 Subject: [PATCH] Remember sockaddrs of listening sockets, use appropriate one when sending UDP packets. --- src/net.c | 14 +++++++------- src/net.h | 11 ++++++++--- src/net_packet.c | 20 ++++++++++++++++---- src/net_setup.c | 11 ++++++----- src/net_socket.c | 5 ++--- 5 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/net.c b/src/net.c index acba5ad3..2b79263e 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.163 2002/03/11 11:45:12 guus Exp $ + $Id: net.c,v 1.35.4.164 2002/03/18 22:47:20 guus Exp $ */ #include "config.h" @@ -94,8 +94,8 @@ cp for(i = 0; i < listen_sockets; i++) { - FD_SET(tcp_socket[i], fs); - FD_SET(udp_socket[i], fs); + FD_SET(listen_socket[i].tcp, fs); + FD_SET(listen_socket[i].udp, fs); } FD_SET(device_fd, fs); @@ -287,10 +287,10 @@ cp for(i = 0; i < listen_sockets; i++) { - if(FD_ISSET(udp_socket[i], f)) - handle_incoming_vpn_data(udp_socket[i]); - if(FD_ISSET(tcp_socket[i], f)) - handle_new_meta_connection(tcp_socket[i]); + if(FD_ISSET(listen_socket[i].udp, f)) + handle_incoming_vpn_data(listen_socket[i].udp); + if(FD_ISSET(listen_socket[i].tcp, f)) + handle_new_meta_connection(listen_socket[i].tcp); } for(node = connection_tree->head; node; node = node->next) diff --git a/src/net.h b/src/net.h index 07e589a5..a5765762 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.46 2002/03/01 14:09:31 guus Exp $ + $Id: net.h,v 1.9.4.47 2002/03/18 22:47:20 guus Exp $ */ #ifndef __TINC_NET_H__ @@ -98,6 +98,12 @@ typedef struct outgoing_t { struct addrinfo *aip; } outgoing_t; +typedef struct listen_socket_t { + int tcp; + int udp; + sockaddr_t sa; +} listen_socket_t; + extern int maxtimeout; extern int seconds_till_retry; extern int addressfamily; @@ -107,8 +113,7 @@ extern char *status_text[]; #include "connection.h" /* Yes, very strange placement indeed, but otherwise the typedefs get all tangled up */ -extern int tcp_socket[MAXSOCKETS]; -extern int udp_socket[MAXSOCKETS]; +extern listen_socket_t listen_socket[MAXSOCKETS]; extern int listen_sockets; extern int keyexpires; extern int keylifetime; diff --git a/src/net_packet.c b/src/net_packet.c index 61ec2045..dcdd73ca 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.10 2002/03/17 15:59:29 guus Exp $ + $Id: net_packet.c,v 1.1.2.11 2002/03/18 22:47:20 guus Exp $ */ #include "config.h" @@ -194,7 +194,10 @@ void send_udppacket(node_t *n, vpn_packet_t *inpkt) vpn_packet_t *copy; static int priority = 0; int origpriority; + int sock; cp + /* Make sure we have a valid key */ + if(!n->status.validkey) { if(debug_lvl >= DEBUG_TRAFFIC) @@ -261,20 +264,29 @@ cp inpkt->len += n->maclength; } + /* Determine which socket we have to use */ + + for(sock = 0; sock < listen_sockets; sock++) + if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family) + break; + + if(sock >= listen_sockets) + sock = 0; /* If none is available, just use the first and hope for the best. */ + /* Send the packet */ #if defined(SOL_IP) && defined(IP_TOS) - if(priorityinheritance && origpriority != priority) + if(priorityinheritance && origpriority != priority && listen_socket[sock].sa.sa.sa_family == AF_INET) { priority = origpriority; if(debug_lvl >= DEBUG_TRAFFIC) syslog(LOG_DEBUG, _("Setting outgoing packet priority to %d"), priority); - if(setsockopt(udp_socket[0], SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */ + if(setsockopt(sock, SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */ syslog(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt", strerror(errno)); } #endif - if((sendto(udp_socket[0], (char *)&inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0) + if((sendto(listen_socket[sock].udp, (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)); diff --git a/src/net_setup.c b/src/net_setup.c index a77fa58a..f4c9e534 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.10 2002/03/10 16:09:15 guus Exp $ + $Id: net_setup.c,v 1.1.2.11 2002/03/18 22:47:20 guus Exp $ */ #include "config.h" @@ -486,10 +486,10 @@ cp for(aip = ai; aip; aip = aip->ai_next) { - if((tcp_socket[listen_sockets] = setup_listen_socket((sockaddr_t *)aip->ai_addr)) < 0) + if((listen_socket[listen_sockets].tcp = setup_listen_socket((sockaddr_t *)aip->ai_addr)) < 0) continue; - if((udp_socket[listen_sockets] = setup_vpn_in_socket((sockaddr_t *)aip->ai_addr)) < 0) + if((listen_socket[listen_sockets].udp = setup_vpn_in_socket((sockaddr_t *)aip->ai_addr)) < 0) continue; if(debug_lvl >= DEBUG_CONNECTIONS) @@ -499,6 +499,7 @@ cp free(hostname); } + listen_socket[listen_sockets].sa.sa = *aip->ai_addr; listen_sockets++; } @@ -576,8 +577,8 @@ cp for(i = 0; i < listen_sockets; i++) { - close(udp_socket[i]); - close(tcp_socket[i]); + close(listen_socket[i].tcp); + close(listen_socket[i].udp); } exit_events(); diff --git a/src/net_socket.c b/src/net_socket.c index 277b9b09..fcc2e2fe 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.9 2002/03/17 15:59:29 guus Exp $ + $Id: net_socket.c,v 1.1.2.10 2002/03/18 22:47:20 guus Exp $ */ #include "config.h" @@ -70,8 +70,7 @@ int addressfamily = AF_INET; int maxtimeout = 900; int seconds_till_retry = 5; -int tcp_socket[MAXSOCKETS]; -int udp_socket[MAXSOCKETS]; +listen_socket_t listen_socket[MAXSOCKETS]; int listen_sockets = 0; /* Setup sockets */