- Use strerror() instead of sys_errlist[] for increased portability
[tinc] / src / net.c
index cd433d0..72c1b5e 100644 (file)
--- 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.24 2000/08/08 17:07:47 guus Exp $
+    $Id: net.c,v 1.35.4.27 2000/09/06 11:49:03 guus Exp $
 */
 
 #include "config.h"
@@ -157,7 +157,7 @@ cp
   rp->from = ntohl(rp->from);
 
   total_socket_in += rp->len;
-  
+
   if(rp->len >= 0)
     {
       f = lookup_conn(rp->from);
@@ -184,7 +184,7 @@ cp
         }
       else
        {
-          /* Can we add to queue? */
+          add_queue(&(cl->sq), rp, rp->len + 2);
          if(!cl->status.waitingforkey)
            send_key_request(rp->from);
        }
@@ -460,6 +460,7 @@ int setup_listen_meta_socket(int port)
   int nfd, flags;
   struct sockaddr_in a;
   const int one = 1;
+  config_t const *cfg;
 cp
   if((nfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
     {
@@ -473,6 +474,12 @@ cp
       return -1;
     }
 
+  if(setsockopt(nfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)))
+    {
+      syslog(LOG_ERR, _("setsockopt: %m"));
+      return -1;
+    }
+
   flags = fcntl(nfd, F_GETFL);
   if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
     {
@@ -480,10 +487,23 @@ cp
       return -1;
     }
 
+  if((cfg = get_config_val(interface)))
+    {
+      if(setsockopt(nfd, SOL_SOCKET, SO_KEEPALIVE, cfg->data.ptr, strlen(cfg->data.ptr)))
+        {
+          syslog(LOG_ERR, _("Unable to bind listen socket to interface %s: %m"), cfg->data.ptr);
+          return -1;
+        }
+    }
+
   memset(&a, 0, sizeof(a));
   a.sin_family = AF_INET;
   a.sin_port = htons(port);
-  a.sin_addr.s_addr = htonl(INADDR_ANY);
+  
+  if((cfg = get_config_val(interfaceip)))
+    a.sin_addr.s_addr = htonl(cfg->data.ip->ip);
+  else
+    a.sin_addr.s_addr = htonl(INADDR_ANY);
 
   if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
     {
@@ -579,13 +599,13 @@ cp
     }
 
   flags = fcntl(cl->meta_socket, F_GETFL);
-/*  if(fcntl(cl->meta_socket, F_SETFL, flags | O_NONBLOCK) < 0)
+  if(fcntl(cl->meta_socket, F_SETFL, flags | O_NONBLOCK) < 0)
     {
       syslog(LOG_ERR, _("fcntl for %s port %d: %m"),
              cl->real_hostname, cl->port);
       return -1;
     }
-*/
+
   if(debug_lvl > 0)
     syslog(LOG_INFO, _("Connected to %s port %hd"),
          cl->real_hostname, cl->port);
@@ -914,7 +934,7 @@ cp
   if(x)
     {
       syslog(LOG_ERR, _("Incoming data socket error for %s (%s): %s"),
-             cl->vpn_hostname, cl->real_hostname, sys_errlist[x]);
+             cl->vpn_hostname, cl->real_hostname, strerror(x));
       return -1;
     }
 
@@ -1112,17 +1132,11 @@ cp
   if(x)
     {
       syslog(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
-             cl->vpn_hostname, cl->real_hostname, sys_errlist[x]);
+             cl->vpn_hostname, cl->real_hostname, strerror(x));
       return -1;
     }
 
-  if(cl->buflen >= MAXBUFSIZE)
-    {
-      syslog(LOG_ERR, _("Metadata read buffer overflow!"));
-      return -1;
-    }
-
-  lenin = read(cl->meta_socket, cl->buffer, MAXBUFSIZE-cl->buflen);
+  lenin = read(cl->meta_socket, cl->buffer, MAXBUFSIZE - cl->buflen);
 
   if(lenin<=0)
     {
@@ -1213,6 +1227,12 @@ cp
         }
     }
 
+  if(cl->buflen >= MAXBUFSIZE)
+    {
+      syslog(LOG_ERR, _("Metadata read buffer overflow!"));
+      return -1;
+    }
+
   cl->last_ping_time = time(NULL);
   cl->want_ping = 0;
 cp  
@@ -1244,7 +1264,7 @@ cp
            */
            getsockopt(p->socket, SOL_SOCKET, SO_ERROR, &x, &l);
            syslog(LOG_ERR, _("Outgoing data socket error for %s (%s): %s"),
-                   p->vpn_hostname, p->real_hostname, sys_errlist[x]);
+                   p->vpn_hostname, p->real_hostname, strerror(x));
            terminate_connection(p);
            return;
          }