- Global time_t now, so that we don't have to call time() too often.
[tinc] / src / net_socket.c
index 9ce3fae..63760af 100644 (file)
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     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.2 2002/02/20 19:25:09 guus Exp $
+    $Id: net_socket.c,v 1.1.2.7 2002/03/01 14:09:31 guus Exp $
 */
 
 #include "config.h"
 */
 
 #include "config.h"
@@ -70,8 +70,10 @@ int addressfamily = AF_INET;
 int maxtimeout = 900;
 int seconds_till_retry = 5;
 
 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 listen_sockets = 0;
+
 /* Setup sockets */
 
 int setup_listen_socket(sockaddr_t *sa)
 /* Setup sockets */
 
 int setup_listen_socket(sockaddr_t *sa)
@@ -81,6 +83,7 @@ int setup_listen_socket(sockaddr_t *sa)
   int option;
 #ifdef HAVE_LINUX
   char *interface;
   int option;
 #ifdef HAVE_LINUX
   char *interface;
+  struct ifreq ifr;
 #endif
 cp
   if((nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
 #endif
 cp
   if((nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
@@ -108,15 +111,19 @@ cp
   setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
 
   if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
   setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
 
   if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
-    if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)))
-      {
-        close(nfd);
-        syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
-        return -1;
-      }
+    {
+      memset(&ifr, 0, sizeof(ifr));
+      strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
+      if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
+       {
+          close(nfd);
+          syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
+          return -1;
+       }
+    }
 #endif
 
 #endif
 
-  if(bind(nfd, &sa->sa, sizeof(*sa)))
+  if(bind(nfd, &sa->sa, SALEN(sa->sa)))
     {
       close(nfd);
       addrstr = sockaddr2hostname(sa);
     {
       close(nfd);
       addrstr = sockaddr2hostname(sa);
@@ -142,6 +149,7 @@ int setup_vpn_in_socket(sockaddr_t *sa)
   int option;
 #ifdef HAVE_LINUX
   char *interface;
   int option;
 #ifdef HAVE_LINUX
   char *interface;
+  struct ifreq ifr;
 #endif
 cp
   if((nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
 #endif
 cp
   if((nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
@@ -162,15 +170,19 @@ cp
   setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
 #ifdef HAVE_LINUX
   if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
   setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
 #ifdef HAVE_LINUX
   if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
-    if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)))
-      {
-        close(nfd);
-        syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
-        return -1;
-      }
+    {
+      memset(&ifr, 0, sizeof(ifr));
+      strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
+      if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)))
+       {
+          close(nfd);
+          syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface, strerror(errno));
+          return -1;
+       }
+    }
 #endif
 
 #endif
 
-  if(bind(nfd, &sa->sa, sizeof(*sa)))
+  if(bind(nfd, &sa->sa, SALEN(sa->sa)))
     {
       close(nfd);
       addrstr = sockaddr2hostname(sa);
     {
       close(nfd);
       addrstr = sockaddr2hostname(sa);
@@ -192,7 +204,7 @@ cp
 
   event = new_event();
   event->handler = (event_handler_t)setup_outgoing_connection;
 
   event = new_event();
   event->handler = (event_handler_t)setup_outgoing_connection;
-  event->time = time(NULL) + outgoing->timeout;
+  event->time = now + outgoing->timeout;
   event->data = outgoing;
   event_add(event);
 
   event->data = outgoing;
   event_add(event);
 
@@ -228,10 +240,10 @@ cp
 
   /* Connect */
 
 
   /* Connect */
 
-  if(connect(c->socket, &c->address.sa, sizeof(c->address)) == -1)
+  if(connect(c->socket, &c->address.sa, SALEN(c->address.sa)) == -1)
     {
       close(c->socket);
     {
       close(c->socket);
-      syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
+      syslog(LOG_ERR, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(errno));
       return -1;
     }
 
       return -1;
     }
 
@@ -248,7 +260,7 @@ cp
   if(debug_lvl >= DEBUG_CONNECTIONS)
     syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
 
   if(debug_lvl >= DEBUG_CONNECTIONS)
     syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
 
-  c->last_ping_time = time(NULL);
+  c->last_ping_time = now;
 
   send_id(c);
 cp
 
   send_id(c);
 cp
@@ -334,7 +346,7 @@ begin:
 
   /* Connect */
 
 
   /* Connect */
 
-  result = connect(c->socket, &c->address.sa, sizeof(c->address));
+  result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
 
   if(result == -1)
     {
 
   if(result == -1)
     {
@@ -395,7 +407,7 @@ cp
     }
   
   c->outgoing = outgoing;
     }
   
   c->outgoing = outgoing;
-  c->last_ping_time = time(NULL);
+  c->last_ping_time = now;
 
   connection_add(c);
 
 
   connection_add(c);
 
@@ -406,13 +418,13 @@ cp
   accept a new tcp connect and create a
   new connection
 */
   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
 {
   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;
     {
       syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
       return -1;
@@ -427,7 +439,7 @@ cp
   c->address = sa;
   c->hostname = sockaddr2hostname(&sa);
   c->socket = fd;
   c->address = sa;
   c->hostname = sockaddr2hostname(&sa);
   c->socket = fd;
-  c->last_ping_time = time(NULL);
+  c->last_ping_time = now;
 
   if(debug_lvl >= DEBUG_CONNECTIONS)
     syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
 
   if(debug_lvl >= DEBUG_CONNECTIONS)
     syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);