Terminate a connection on any error. Furthermore, disallow del_host,
[tinc] / src / net.c
index f8c7306..9daf8c8 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -51,6 +51,8 @@ int total_tap_out = 0;
 int total_socket_in = 0;
 int total_socket_out = 0;
 
+static int seconds_till_retry;
+
 /* The global list of existing connections */
 conn_list_t *conn_list = NULL;
 conn_list_t *myself = NULL;
@@ -541,6 +543,30 @@ cp
   return 0;
 }
 
+RETSIGTYPE
+sigalrm_handler(int a)
+{
+  config_t const *cfg;
+cp
+  cfg = get_config_val(upstreamip);
+
+  if(!setup_outgoing_connection(cfg->data.ip->ip))
+    {
+      signal(SIGALRM, SIG_IGN);
+    }
+  else
+    {
+      signal(SIGALRM, sigalrm_handler);
+      seconds_till_retry += 5;
+      if(seconds_till_retry>300)    /* Don't wait more than 5 minutes. */
+        seconds_till_retry = 300;
+      alarm(seconds_till_retry);
+      syslog(LOG_ERR, "Still failed to connect to other. Will retry in %d seconds.",
+            seconds_till_retry);
+    }
+cp
+}
+
 /*
   setup all initial network connections
 */
@@ -564,33 +590,14 @@ cp
     return 0;
 
   if(setup_outgoing_connection(cfg->data.ip->ip))
-    return -1;
-cp
-  return 0;
-}
-
-RETSIGTYPE
-sigalrm_handler(int a)
-{
-  config_t const *cfg;
-  static int seconds_till_retry;
-cp
-  cfg = get_config_val(upstreamip);
-
-  if(!setup_outgoing_connection(cfg->data.ip->ip))
-    {
-      signal(SIGALRM, SIG_IGN);
-      seconds_till_retry = 5;
-    }
-  else
     {
       signal(SIGALRM, sigalrm_handler);
-      seconds_till_retry += 5;
+      seconds_till_retry = 300;
       alarm(seconds_till_retry);
-      syslog(LOG_ERR, "Still failed to connect to other. Will retry in %d seconds.",
-            seconds_till_retry);
+      syslog(LOG_NOTICE, "Try to re-establish outgoing connection in 5 minutes.");
     }
 cp
+  return 0;
 }
 
 /*
@@ -821,11 +828,13 @@ cp
 
   if(cl->status.outgoing)
     {
-      alarm(5);
       signal(SIGALRM, sigalrm_handler);
+      seconds_till_retry = 5;
+      alarm(seconds_till_retry);
       syslog(LOG_NOTICE, "Try to re-establish outgoing connection in 5 seconds.");
     }
   
+  cl->status.active = 0;
   cl->status.remove = 1;
 cp
 }
@@ -926,8 +935,8 @@ cp
 
   if(cl->buflen >= MAXBUFSIZE)
     {
-      syslog(LOG_ERR, "Metadata read buffer full! Discarding contents.");
-      cl->buflen = 0;
+      syslog(LOG_ERR, "Metadata read buffer overflow.");
+      return -1;
     }
 
   lenin = read(cl->meta_socket, cl->buffer, MAXBUFSIZE-cl->buflen);
@@ -962,17 +971,22 @@ cp
               if(request_handlers[request] == NULL)
                 {
                   syslog(LOG_ERR, "Unknown request: %s", cl->buffer);
-                  return 0;
+                  return -1;
                 }
 
               if(debug_lvl > 3)
                 syslog(LOG_DEBUG, "Got request: %s", cl->buffer);                             
 
-              request_handlers[request](cl);
+              if(request_handlers[request](cl))  /* Something went wrong. Probably scriptkiddies. Terminate. */
+                {
+                  syslog(LOG_ERR, "Error while processing request from IP_ADDR_S", IP_ADDR_V(cl->real_ip));
+                  return -1;
+                }
             }
           else
             {
-              syslog(LOG_ERR, "Bogus data received: %s", cl->buffer);
+              syslog(LOG_ERR, "Bogus data received.");
+              return -1;
             }
 
           cl->buflen -= cl->reqlen;