- Improved handling of errors on connection attempts.
[tinc] / src / net.c
index 31acd38..bc3540e 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.7 2000/06/26 19:39:34 guus Exp $
+    $Id: net.c,v 1.35.4.10 2000/06/27 20:10:48 guus Exp $
 */
 
 #include "config.h"
@@ -56,6 +56,7 @@ int total_tap_out = 0;
 int total_socket_in = 0;
 int total_socket_out = 0;
 
+int upstreamindex = 0;
 static int seconds_till_retry;
 
 /* The global list of existing connections */
@@ -496,6 +497,9 @@ int setup_outgoing_meta_socket(conn_list_t *cl)
   struct sockaddr_in a;
   config_t const *cfg;
 cp
+  if(debug_lvl > 0)
+    syslog(LOG_INFO, _("Trying to connect to %s"), cl->hostname);
+
   if((cfg = get_config_val(upstreamport)) == NULL)
     cl->port = 655;
   else
@@ -548,7 +552,8 @@ cp
   
   if(setup_outgoing_meta_socket(ncn) < 0)
     {
-      syslog(LOG_ERR, _("Could not set up a meta connection!"));
+      syslog(LOG_ERR, _("Could not set up a meta connection to %s"),
+             ncn->hostname);
       free_conn_element(ncn);
       return -1;
     }
@@ -587,7 +592,7 @@ cp
     myself->port = cfg->data.val;
 
   if(cfg = get_config_val(indirectdata))
-    if(cfg->data.val)
+    if(cfg->data.val == stupid_true)
       myself->flags |= EXPORTINDIRECTDATA;
 
   if((myself->meta_socket = setup_listen_meta_socket(myself->port)) < 0)
@@ -615,22 +620,26 @@ sigalrm_handler(int a)
 {
   config_t const *cfg;
 cp
-  cfg = get_config_val(upstreamip);
+  cfg = get_next_config_val(upstreamip, upstreamindex++);
 
-  if(!setup_outgoing_connection(cfg->data.ip->ip))
-    {
-      signal(SIGALRM, SIG_IGN);
-    }
-  else
+  while(cfg)
     {
-      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);
+      if(!setup_outgoing_connection(cfg->data.ip->ip))   /* function returns 0 when there are no problems */
+        {
+          signal(SIGALRM, SIG_IGN);
+          return;
+        }
+      cfg = get_next_config_val(upstreamip, upstreamindex++); /* Or else we try the next ConnectTo line */
     }
+
+  signal(SIGALRM, sigalrm_handler);
+  upstreamindex = 0;
+  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
 }
 
@@ -652,17 +661,22 @@ cp
   if(setup_myself() < 0)
     return -1;
 
-  if((cfg = get_config_val(upstreamip)) == NULL)
+  if((cfg = get_next_config_val(upstreamip, upstreamindex++)) == NULL)
     /* No upstream IP given, we're listen only. */
     return 0;
 
-  if(setup_outgoing_connection(cfg->data.ip->ip))
+  while(cfg)
     {
-      signal(SIGALRM, sigalrm_handler);
-      seconds_till_retry = 300;
-      alarm(seconds_till_retry);
-      syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in 5 minutes"));
+      if(!setup_outgoing_connection(cfg->data.ip->ip))   /* function returns 0 when there are no problems */
+        return 0;
+      cfg = get_next_config_val(upstreamip, upstreamindex++); /* Or else we try the next ConnectTo line */
     }
+    
+  signal(SIGALRM, sigalrm_handler);
+  upstreamindex = 0;
+  seconds_till_retry = 300;
+  alarm(seconds_till_retry);
+  syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in 5 minutes"));
 cp
   return 0;
 }
@@ -772,7 +786,8 @@ cp
   p->last_ping_time = time(NULL);
   p->want_ping = 0;
   
-  syslog(LOG_NOTICE, _("Connection from %s port %d"),
+  if(debug_lvl > 0)
+    syslog(LOG_NOTICE, _("Connection from %s port %d"),
          p->hostname, htons(ci.sin_port));
 
   if(send_basic_info(p) < 0)
@@ -902,9 +917,15 @@ cp
       syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in 5 seconds"));
     }
   
-  cl->status.active = 0;
   cl->status.remove = 1;
 
+  /* If this cl isn't active, don't send any DEL_HOSTs and don't bother
+     checking for other lost connections. */
+  if(!cl->status.active)
+    return;
+    
+  cl->status.active = 0;
+
 cp
   /* Find all connections that were lost because they were behind cl
      (the connection that was dropped). */