Various fixes, tinc is now somewhat capable of actually working again.
authorGuus Sliepen <guus@tinc-vpn.org>
Tue, 30 Oct 2001 12:59:12 +0000 (12:59 +0000)
committerGuus Sliepen <guus@tinc-vpn.org>
Tue, 30 Oct 2001 12:59:12 +0000 (12:59 +0000)
src/Makefile.am
src/edge.c
src/graph.c
src/net.c
src/node.c
src/protocol.c
src/subnet.c

index c74433d..8f5d21d 100644 (file)
@@ -1,5 +1,5 @@
 ## Produce this file with automake to get Makefile.in
-# $Id: Makefile.am,v 1.4.4.16 2001/10/28 10:16:18 guus Exp $
+# $Id: Makefile.am,v 1.4.4.17 2001/10/30 12:59:12 guus Exp $
 
 sbin_PROGRAMS = tincd
 
@@ -8,7 +8,7 @@ tincd_SOURCES = conf.c connection.c device.c edge.c graph.c meta.c net.c netutl.
 
 INCLUDES = @INCLUDES@ -I$(top_builddir) -I$(top_srcdir)/lib -I$(top_srcdir)/intl
 
-noinst_HEADERS = conf.h connection.h device.h edge.h meta.h net.h netutl.h node.h process.h    \
+noinst_HEADERS = conf.h connection.h device.h edge.h graph.h meta.h net.h netutl.h node.h process.h    \
        protocol.h route.h subnet.h
 
 LIBS = @LIBS@ @INTLLIBS@
index 92abbb3..451bb08 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.
 
-    $Id: edge.c,v 1.1.2.3 2001/10/28 22:42:49 guus Exp $
+    $Id: edge.c,v 1.1.2.4 2001/10/30 12:59:12 guus Exp $
 */
 
 #include "config.h"
@@ -111,7 +111,7 @@ cp
 avl_tree_t *new_edge_tree(void)
 {
 cp
-  edge_tree = avl_alloc_tree((avl_compare_t)edge_name_compare, NULL);
+  return avl_alloc_tree((avl_compare_t)edge_name_compare, NULL);
 cp
 }
 
@@ -195,8 +195,8 @@ cp
   for(node = edge_tree->head; node; node = node->next)
     {
       e = (edge_t *)node->data;
-      syslog(LOG_DEBUG, _(" %s - %s options %ld"),
-             e->from->name, e->to->name, e->options);
+      syslog(LOG_DEBUG, _(" %s - %s options %ld weight %d"),
+             e->from->name, e->to->name, e->options, e->weight);
     }
     
   syslog(LOG_DEBUG, _("End of edges."));
index 7b8880d..53cf6a9 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.
 
-    $Id: graph.c,v 1.1.2.3 2001/10/29 13:14:57 guus Exp $
+    $Id: graph.c,v 1.1.2.4 2001/10/30 12:59:12 guus Exp $
 */
 
 /* We need to generate two trees from the graph:
@@ -32,7 +32,9 @@
    favour Kruskal's, because we make an extra AVL tree of edges sorted on
    weights (metric). That tree only has to be updated when an edge is added or
    removed, and during the MST algorithm we just have go linearly through that
-   tree, adding safe edges until #edges = #nodes - 1.
+   tree, adding safe edges until #edges = #nodes - 1. The implementation here
+   however is not so fast, because I tried to avoid having to make a forest and
+   merge trees.
 
    For the SSSP algorithm Dijkstra's seems to be a nice choice. Currently a
    simple breadth-first search is presented here.
@@ -65,8 +67,6 @@ void mst_kruskal(void)
   int safe_edges = 0;
   int skipped;
 
-  syslog(LOG_DEBUG, _("Running Kruskal's algorithm:"));
-
   /* Clear visited status on nodes */
 
   for(node = node_tree->head; node; node = node->next)
@@ -93,10 +93,6 @@ void mst_kruskal(void)
   while(safe_edges < nodes - 1)
   for(skipped = 0, node = edge_weight_tree->head; node; node = node->next)
     {
-// Algorithm should work without this:
-//      if(safe_edges = nodes - 1)
-//        break;
-
       e = (edge_t *)node->data;
 
       if(e->from->status.visited == e->to->status.visited)
@@ -112,18 +108,9 @@ void mst_kruskal(void)
 
       safe_edges++;
 
-      syslog(LOG_DEBUG, _("Adding safe edge %s - %s weight %d"), e->from->name, e->to->name, e->weight);
-
       if(skipped)
         break;
     }
-
-  syslog(LOG_DEBUG, _("Done."));
-
-  if(safe_edges != nodes - 1)
-    {
-      syslog(LOG_ERR, _("Implementation of Kruskal's algorithm is screwed: %d nodes, found %d safe edges"), nodes, safe_edges);
-    }
 }
 
 /* Implementation of a simple breadth-first search algorithm.
@@ -135,12 +122,8 @@ void sssp_bfs(void)
   avl_node_t *node, *from, *next, *to;
   edge_t *e;
   node_t *n, *check;
-  int nodes = 0;
-  int visited = 0;
   avl_tree_t *todo_tree;
 
-  syslog(LOG_DEBUG, _("Running BFS algorithm:"));
-
   todo_tree = avl_alloc_tree(NULL, NULL);
 
   /* Clear visited status on nodes */
@@ -149,7 +132,6 @@ void sssp_bfs(void)
     {
       n = (node_t *)node->data;
       n->status.visited = 0;
-      nodes++;
     }
 
   /* Begin with myself */
@@ -160,7 +142,6 @@ void sssp_bfs(void)
   node = avl_alloc_node();
   node->data = myself;
   avl_insert_top(todo_tree, node);
-  visited++;
 
   /* Loop while todo_tree is filled */
 
@@ -183,13 +164,11 @@ void sssp_bfs(void)
               if(!check->status.visited)
                 {
                   check->status.visited = 1;
-                  check->nexthop = (n->nexthop == myself) ? n : n->nexthop;
+                  check->nexthop = (n->nexthop == myself) ? check : n->nexthop;
                   check->via = check; /* FIXME: only if !(e->options & INDIRECT), otherwise use n->via */
                   node = avl_alloc_node();
                   node->data = check;
                   avl_insert_before(todo_tree, from, node);
-                  visited++;
-                  syslog(LOG_DEBUG, _("Node %s nexthop %s via %s"), check->name, check->nexthop->name, check->via->name);
                 }
             }
 
@@ -197,12 +176,5 @@ void sssp_bfs(void)
         }
     }
 
-  syslog(LOG_DEBUG, _("Done."));
-
   avl_free_tree(todo_tree);
-
-  if(visited != nodes)
-    {
-      syslog(LOG_ERR, _("Implementation of BFS algorithm is screwed: %d nodes, visited %d"), nodes, visited);
-    }
 }
index 0e7dcc9..1e37a79 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.141 2001/10/28 10:16:18 guus Exp $
+    $Id: net.c,v 1.35.4.142 2001/10/30 12:59:12 guus Exp $
 */
 
 #include "config.h"
@@ -192,32 +192,17 @@ cp
       return;
     }
 
-  if(!n->status.active)
-    {
-      if(debug_lvl >= DEBUG_TRAFFIC)
-       syslog(LOG_INFO, _("%s (%s) is not active, dropping packet"),
-              n->name, n->hostname);
-
-      return;
-    }
-/* FIXME
-  if(n->via == myself)
-    via = n->nexthop;
-  else
-    via = n->via;
-
-  if(via != n && debug_lvl >= DEBUG_TRAFFIC)
+  if(n->via != n && debug_lvl >= DEBUG_TRAFFIC)
     syslog(LOG_ERR, _("Sending packet to %s via %s (%s)"),
-           n->name, via->name, via->hostname);
+           n->name, n->via->name, n->via->hostname);
 
-  if((myself->options | via->options) & OPTION_TCPONLY)
+  if((myself->options | n->via->options) & OPTION_TCPONLY)
     {
-      if(send_tcppacket(via->connection, packet))
-        terminate_connection(via->connection, 1);
+      if(send_tcppacket(n->via->connection, packet))
+        terminate_connection(n->via->connection, 1);
     }
   else
-    send_udppacket(via, packet);
-*/
+    send_udppacket(n->via, packet);
 }
 
 /* Broadcast a packet to all active direct connections */
@@ -922,7 +907,7 @@ cp
 
   if(!n)
     {
-      syslog(LOG_WARNING, _("Received UDP packets on port %hd from unknown source %x:%hd"), myself->port, ntohl(from.sin_addr.s_addr), ntohs(from.sin_port));
+      syslog(LOG_WARNING, _("Received UDP packet on port %hd from unknown source %x:%hd"), myself->port, ntohl(from.sin_addr.s_addr), ntohs(from.sin_port));
       return;
     }
 /*
@@ -1061,7 +1046,7 @@ cp
       get_config_string(cfg, &name);
       cfg = lookup_config_next(config_tree, cfg);  /* Next time skip to next ConnectTo line */
 
-      if(!setup_outgoing_connection(name))   /* function returns 0 when there are no problems */
+      if(setup_outgoing_connection(name))   /* function returns 0 when there are no problems */
         retry = 1;
 
     }
index f50d365..95a1d4f 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.
 
-    $Id: node.c,v 1.1.2.4 2001/10/28 22:42:49 guus Exp $
+    $Id: node.c,v 1.1.2.5 2001/10/30 12:59:12 guus Exp $
 */
 
 #include "config.h"
@@ -43,7 +43,7 @@ int node_compare(node_t *a, node_t *b)
   return strcmp(a->name, b->name);
 }
 
-int node_udp_compare(connection_t *a, connection_t *b)
+int node_udp_compare(node_t *a, node_t *b)
 {
   if(a->address < b->address)
     return -1;
@@ -142,9 +142,9 @@ cp
   for(node = node_tree->head; node; node = node->next)
     {
       n = (node_t *)node->data;
-      syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld status %04x"),
+      syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld status %04x nexthop %s via %s"),
              n->name, n->hostname, n->port, n->options,
-             n->status);
+             n->status, n->nexthop->name, n->via->name);
     }
     
   syslog(LOG_DEBUG, _("End of nodes."));
index 63b10b1..2744592 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.
 
-    $Id: protocol.c,v 1.28.4.112 2001/10/28 22:42:49 guus Exp $
+    $Id: protocol.c,v 1.28.4.113 2001/10/30 12:59:12 guus Exp $
 */
 
 #include "config.h"
@@ -56,6 +56,7 @@
 #include "connection.h"
 #include "node.h"
 #include "edge.h"
+#include "graph.h"
 
 #include "system.h"
 
@@ -543,6 +544,8 @@ int ack_h(connection_t *c)
   int weight;
   node_t *n;
   subnet_t *s;
+  edge_t *e;
+  connection_t *other;
   avl_node_t *node, *node2;
 cp
   if(sscanf(c->buffer, "%*d %hd %d", &port, &weight) != 2)
@@ -559,6 +562,7 @@ cp
     {
       n = new_node();
       n->name = xstrdup(c->name);
+      n->address = c->address;
       n->hostname = xstrdup(c->hostname);
       n->port = port;
 
@@ -617,6 +621,8 @@ cp
 
   c->allow_request = ALL;
   c->status.active = 1;
+  c->node->cipher = EVP_bf_cbc();
+  c->node->keylength = c->node->cipher->key_len + c->node->cipher->iv_len;
 
   if(debug_lvl >= DEBUG_CONNECTIONS)
     syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, c->hostname);
@@ -639,13 +645,6 @@ cp
       if(n == c->node || n == myself)
         continue;
 
-      /* Notify others of this connection */
-
-      if(n->connection)
-        send_add_node(n->connection, c->node);
-
-      /* Notify new connection of everything we know */
-
       send_add_node(c, n);
 
       for(node2 = c->node->subnet_tree->head; node2; node2 = node2->next)
@@ -654,6 +653,36 @@ cp
           send_add_subnet(c, s);
         }
     }
+
+  /* Send all known edges */
+
+  for(node = edge_tree->head; node; node = node->next)
+    {
+      e = (edge_t *)node->data;
+
+      if(e == c->edge)
+        continue;
+
+      send_add_edge(c, e);
+    }
+
+  /* Notify others of this connection */
+
+  for(node = connection_tree->head; node; node = node->next)
+    {
+      other = (connection_t *)node->data;
+
+      if(other == c)
+        continue;
+      
+      send_add_node(other, c->node);
+      send_add_edge(other, c->edge);
+    }
+
+  /* Run MST and SSSP algorithms */
+  
+  mst_kruskal();
+  sssp_bfs();
 cp
   return 0;
 }
@@ -947,8 +976,8 @@ cp
 int send_add_edge(connection_t *c, edge_t *e)
 {
 cp
-  return send_request(c, "%d %s %s %lx", ADD_NODE,
-                      e->from->name, e->to->name, e->options);
+  return send_request(c, "%d %s %s %lx %d", ADD_NODE,
+                      e->from->name, e->to->name, e->options, e->weight);
 }
 
 int add_edge_h(connection_t *c)
@@ -959,9 +988,10 @@ int add_edge_h(connection_t *c)
   char from_name[MAX_STRING_SIZE];
   char to_name[MAX_STRING_SIZE];
   long int options;
+  int weight;
   avl_node_t *node;
 cp
-  if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx", from_name, to_name, &options) != 3)
+  if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx %d", from_name, to_name, &options, &weight) != 4)
     {
        syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
        return -1;
@@ -1013,6 +1043,7 @@ cp
       e->from = from;
       e->to = to;
       e->options = options;
+      e->weight = weight;
       edge_add(e);
     }
 
@@ -1025,6 +1056,10 @@ cp
         send_add_edge(other, e);
     }
 
+  /* Run MST before or after we tell the rest? */
+
+  mst_kruskal();
+  sssp_bfs();
 cp
   return 0;
 }
@@ -1111,6 +1146,11 @@ cp
   /* Delete the edge */
   
   edge_del(e);
+
+  /* Run MST before or after we tell the rest? */
+
+  mst_kruskal();
+  sssp_bfs();
 cp
   return 0;
 }
index 99d97bc..1b7bc38 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.
 
-    $Id: subnet.c,v 1.1.2.27 2001/10/28 22:42:49 guus Exp $
+    $Id: subnet.c,v 1.1.2.28 2001/10/30 12:59:12 guus Exp $
 */
 
 #include "config.h"
@@ -44,14 +44,8 @@ avl_tree_t *subnet_tree;
 
 int subnet_compare_mac(subnet_t *a, subnet_t *b)
 {
-  int result;
 cp
-  result = memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
-  
-  if(result)
-    return result;
-
-  return strcmp(a->owner->name, b->owner->name);
+  return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
 }
 
 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
@@ -69,7 +63,7 @@ cp
   else if(a->net.ipv4.mask > b->net.ipv4.mask)
     return 1;
 
-  return strcmp(a->owner->name, b->owner->name);
+  return 0;
 }
 
 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
@@ -88,7 +82,7 @@ cp
   if(result)
     return result;
 
-  return strcmp(a->owner->name, b->owner->name);
+  return 0;
 }
 
 int subnet_compare(subnet_t *a, subnet_t *b)
@@ -158,7 +152,7 @@ cp
   free(subnet);
 }
 
-/* Linked list management */
+/* Adding and removing subnets */
 
 void subnet_add(node_t *n, subnet_t *subnet)
 {