Cleanups:
[tinc] / src / net.c
index ca70886..b0bfaca 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.175 2002/09/03 20:43:25 guus Exp $
+    $Id: net.c,v 1.35.4.179 2002/09/09 19:39:58 guus Exp $
 */
 
 #include "config.h"
@@ -65,6 +65,7 @@
 #include "process.h"
 #include "protocol.h"
 #include "subnet.h"
+#include "graph.h"
 #include "process.h"
 #include "route.h"
 #include "device.h"
@@ -82,15 +83,15 @@ int sigalrm = 0;
 
 time_t now = 0;
 
-/* Purge subnets of unreachable nodes. Use carefully. */
+/* Purge edges and subnets of unreachable nodes. Use carefully. */
 
 void purge(void)
 {
-  avl_node_t *nnode, *nnext, *snode, *snext, *cnode;
+  avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
   node_t *n;
+  edge_t *e;
   subnet_t *s;
-  connection_t *c;
-cp
+  cp();
   if(debug_lvl >= DEBUG_PROTOCOL)
     syslog(LOG_DEBUG, _("Purging unreachable nodes"));
 
@@ -108,21 +109,22 @@ cp
       {
         snext = snode->next;
         s = (subnet_t *)snode->data;
-
-        for(cnode = connection_tree->head; cnode; cnode = cnode->next)
-        {
-          c = (connection_t *)cnode->data;
-          if(c->status.active)
-            send_del_subnet(c, s);
-        }
-
+       send_del_subnet(broadcast, s);
         subnet_del(n, s);
       }
 
+      for(enode = n->edge_tree->head; enode; enode = enext)
+      {
+        enext = enode->next;
+        e = (edge_t *)enode->data;
+       send_del_edge(broadcast, e);
+        edge_del(e);
+      }
+
       node_del(n);
     }
   }
-cp
+  cp();
 }
 
 /*
@@ -134,7 +136,7 @@ void build_fdset(fd_set *fs)
   avl_node_t *node, *next;
   connection_t *c;
   int i;
-cp
+  cp();
   FD_ZERO(fs);
 
   for(node = connection_tree->head; node; node = next)
@@ -159,22 +161,19 @@ cp
     }
 
   FD_SET(device_fd, fs);
-cp
+  cp();
 }
 
 /*
   Terminate a connection:
   - Close the socket
-  - Tell other connections about it if report = 1
+  - Remove associated edge and tell other connections about it if report = 1
   - Check if we need to retry making an outgoing connection
   - Deactivate the host
 */
 void terminate_connection(connection_t *c, int report)
 {
-  avl_node_t *node, *node2;
-  connection_t *other;
-  node_t *n;
-cp
+  cp();
   if(c->status.remove)
     return;
 
@@ -186,31 +185,23 @@ cp
   c->status.active = 0;
 
   if(c->node)
-    {
-      if(report && c->node->connection)
-        {
-          for(node = connection_tree->head; node; node = node->next)
-            {
-              other = (connection_t *)node->data;
-              if(other == c)
-                continue;
-              for(node2 = node_tree->head; node2; node2 = node2->next)
-                {
-                  n = (node_t *)node2->data;
-                  if(n->nexthop == c->node)
-                    {
-                      send_del_node(other, n);
-                      n->status.reachable = 0;
-                    }
-                }
-            }
-        }
-      c->node->connection = NULL;
-    }
+    c->node->connection = NULL;
 
   if(c->socket)
     close(c->socket);
 
+  if(c->edge)
+    {
+      if(report)
+        send_del_edge(broadcast, c->edge);
+
+      edge_del(c->edge);
+
+      /* Run MST and SSSP algorithms */
+
+      graph();
+    }
+
   /* Check if this was our outgoing connection */
 
   if(c->outgoing)
@@ -218,7 +209,7 @@ cp
       retry_outgoing(c->outgoing);
       c->outgoing = NULL;
     }
-cp
+  cp();
 }
 
 /*
@@ -231,13 +222,14 @@ cp
 */
 void check_dead_connections(void)
 {
-  avl_node_t *node;
+  avl_node_t *node, *next;
   connection_t *c;
-cp
-  for(node = connection_tree->head; node; node = node->next)
+  cp();
+  for(node = connection_tree->head; node; node = next)
     {
+      next = node->next;
       c = (connection_t *)node->data;
-      if(c->last_ping_time + pingtimeout < now && !c->status.remove)
+      if(c->last_ping_time + pingtimeout < now)
         {
           if(c->status.active)
             {
@@ -256,6 +248,12 @@ cp
             }
           else
             {
+              if(c->status.remove)
+                {
+                  syslog(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."), c->name, c->hostname, c->status);
+                  connection_del(c);
+                  continue;
+                }
               if(debug_lvl >= DEBUG_CONNECTIONS)
                 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
                        c->name, c->hostname);
@@ -263,7 +261,7 @@ cp
             }
         }
     }
-cp
+  cp();
 }
 
 /*
@@ -277,7 +275,7 @@ void check_network_activity(fd_set *f)
   int result, i;
   int len = sizeof(result);
   vpn_packet_t packet;
-cp
+  cp();
   if(FD_ISSET(device_fd, f))
     {
       if(!read_packet(&packet))
@@ -323,7 +321,7 @@ cp
       if(FD_ISSET(listen_socket[i].tcp, f))
        handle_new_meta_connection(listen_socket[i].tcp);
     }
-cp
+  cp();
 }
 
 /*
@@ -336,7 +334,7 @@ void main_loop(void)
   int r;
   time_t last_ping_check;
   event_t *event;
-cp
+  cp();
   last_ping_check = now;
 
   srand(now);
@@ -350,7 +348,9 @@ cp
 
       build_fdset(&fset);
 
-      if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
+      r = select(FD_SETSIZE, &fset, NULL, NULL, &tv);
+
+      if(r < 0)
         {
           if(errno != EINTR && errno != EAGAIN)
             {
@@ -391,7 +391,7 @@ cp
                 syslog(LOG_INFO, _("Regenerating symmetric key"));
 
               RAND_pseudo_bytes(myself->key, myself->keylength);
-              send_key_changed(myself->connection, myself);
+              send_key_changed(broadcast, myself);
               keyexpires = now + keylifetime;
             }
         }
@@ -439,5 +439,5 @@ cp
           continue;
         }
     }
-cp
+  cp();
 }