Generalized request broadcasting/forwarding.
authorGuus Sliepen <guus@tinc-vpn.org>
Wed, 4 Sep 2002 16:26:45 +0000 (16:26 +0000)
committerGuus Sliepen <guus@tinc-vpn.org>
Wed, 4 Sep 2002 16:26:45 +0000 (16:26 +0000)
src/connection.c
src/connection.h
src/meta.c
src/net.c
src/protocol.c
src/protocol.h
src/protocol_auth.c
src/protocol_edge.c
src/protocol_key.c
src/protocol_subnet.c

index e8a53eccc94cd885f7ee38b0c724f9a4850b89d7..642bfacea01dc0b07480e106ce63706436c9dd6f 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: connection.c,v 1.1.2.30 2002/06/21 10:11:12 guus Exp $
+    $Id: connection.c,v 1.1.2.31 2002/09/04 16:26:44 guus Exp $
 */
 
 #include "config.h"
@@ -41,6 +41,7 @@
 #include "system.h"
 
 avl_tree_t *connection_tree;   /* Meta connections */
+connection_t *broadcast;
 
 int connection_compare(connection_t *a, connection_t *b)
 {
@@ -51,6 +52,10 @@ void init_connections(void)
 {
 cp
   connection_tree = avl_alloc_tree((avl_compare_t)connection_compare, NULL);
+cp
+  broadcast = new_connection();
+  broadcast->name = xstrdup(_("everyone"));
+  broadcast->hostname = xstrdup(_("BROADCAST"));
 cp
 }
 
@@ -58,6 +63,8 @@ void exit_connections(void)
 {
 cp
   avl_delete_tree(connection_tree);
+cp
+  free_connection(broadcast);
 cp
 }
 
index a6d8c58e2e9066eeb3ef9702e53a5efb08b89857..4d2ea2d35b2987d887269ab002165632e6768922 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: connection.h,v 1.1.2.29 2002/09/04 13:48:51 guus Exp $
+    $Id: connection.h,v 1.1.2.30 2002/09/04 16:26:44 guus Exp $
 */
 
 #ifndef __TINC_CONNECTION_H__
@@ -99,6 +99,7 @@ typedef struct connection_t {
 
   char buffer[MAXBUFSIZE];         /* metadata input buffer */
   int buflen;                      /* bytes read into buffer */
+  int reqlen;                      /* length of incoming request */
   int tcplen;                      /* length of incoming TCPpacket */
   int allow_request;               /* defined if there's only one request possible */
 
@@ -108,6 +109,7 @@ typedef struct connection_t {
 } connection_t;
 
 extern avl_tree_t *connection_tree;
+extern connection_t *broadcast;
 
 extern void init_connections(void);
 extern void exit_connections(void);
index 8d6b0a84f561fd5d073b0e1391550bcb7af43052..c3d4f82906614c47c4a6223407ced76c3d5728d0 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: meta.c,v 1.1.2.26 2002/06/21 10:11:12 guus Exp $
+    $Id: meta.c,v 1.1.2.27 2002/09/04 16:26:44 guus Exp $
 */
 
 #include "config.h"
@@ -182,6 +182,7 @@ cp
 
       if(reqlen)
         {
+          c->reqlen = reqlen;
           if(receive_request(c))
             return -1;
 
index 4a5c4b9f72a6631b987ea185140d00be69d58508..13933d9a4bc4c33c9e3c2d0dd7728d86c91a2d12 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.176 2002/09/04 13:48:51 guus Exp $
+    $Id: net.c,v 1.35.4.177 2002/09/04 16:26:44 guus Exp $
 */
 
 #include "config.h"
@@ -87,11 +87,10 @@ time_t now = 0;
 
 void purge(void)
 {
-  avl_node_t *nnode, *nnext, *enode, *enext, *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
   if(debug_lvl >= DEBUG_PROTOCOL)
     syslog(LOG_DEBUG, _("Purging unreachable nodes"));
@@ -110,14 +109,7 @@ 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);
       }
 
@@ -125,14 +117,7 @@ cp
       {
         enext = enode->next;
         e = (edge_t *)enode->data;
-
-        for(cnode = connection_tree->head; cnode; cnode = cnode->next)
-        {
-          c = (connection_t *)cnode->data;
-          if(c->status.active)
-            send_del_edge(c, e);
-        }
-
+       send_del_edge(broadcast, e);
         edge_del(e);
       }
 
@@ -188,8 +173,6 @@ cp
 */
 void terminate_connection(connection_t *c, int report)
 {
-  avl_node_t *node;
-  connection_t *other;
 cp
   if(c->status.remove)
     return;
@@ -210,14 +193,7 @@ cp
   if(c->edge)
     {
       if(report)
-        {
-          for(node = connection_tree->head; node; node = node->next)
-            {
-              other = (connection_t *)node->data;
-              if(other->status.active && other != c)
-                send_del_edge(other, c->edge);
-            }
-        }
+        send_del_edge(broadcast, c->edge);
 
       edge_del(c->edge);
 
@@ -407,7 +383,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;
             }
         }
index 644e89e9443b5585b9f171fbab9dd71b8cbda73e..4127f51a0ec8719b03855cf2be893104f24a65fd 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.132 2002/09/04 13:48:52 guus Exp $
+    $Id: protocol.c,v 1.28.4.133 2002/09/04 16:26:44 guus Exp $
 */
 
 #include "config.h"
@@ -89,7 +89,28 @@ cp
 
   buffer[len++] = '\n';
 cp
-  return send_meta(c, buffer, len);
+  if(c == broadcast)
+    return broadcast_meta(NULL, buffer, len);
+  else
+    return send_meta(c, buffer, len);
+}
+
+int forward_request(connection_t *from)
+{
+  int request;
+cp
+  if(debug_lvl >= DEBUG_PROTOCOL)
+    {
+      sscanf(from->buffer, "%d", &request);
+      if(debug_lvl >= DEBUG_META)
+        syslog(LOG_DEBUG, _("Broadcasting %s from %s (%s): %s"), request_name[request], from->name, from->hostname, from->buffer);
+      else
+        syslog(LOG_DEBUG, _("Broadcasting %s from %s (%s)"), request_name[request], from->name, from->hostname);
+    }
+
+  from->buffer[from->reqlen - 1] = '\n';
+cp
+  return broadcast_meta(from, from->buffer, from->reqlen);
 }
 
 int receive_request(connection_t *c)
index a021f4fe2c109f9a1aad1d170af78ac4583ffb5a..47f772c7de41eecdc24c305a7701915754689dab 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.h,v 1.5.4.33 2002/09/04 13:48:52 guus Exp $
+    $Id: protocol.h,v 1.5.4.34 2002/09/04 16:26:45 guus Exp $
 */
 
 #ifndef __TINC_PROTOCOL_H__
@@ -62,6 +62,7 @@ typedef struct past_request_t {
 /* Basic functions */
 
 extern int send_request(connection_t*, const char*, ...);
+extern int forward_request(connection_t *);
 extern int receive_request(connection_t *);
 extern int check_id(char *);
 
index 42d35e981f9f33af67884147158d3ccdbc705d36..4456ea599b90d930224c8b73ce5ea7f429b2f24e 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_auth.c,v 1.1.4.13 2002/09/04 14:17:28 guus Exp $
+    $Id: protocol_auth.c,v 1.1.4.14 2002/09/04 16:26:45 guus Exp $
 */
 
 #include "config.h"
@@ -483,7 +483,7 @@ void send_everything(connection_t *c)
   subnet_t *s;
   edge_t *e;
 
-  /* Send all known subnets */
+  /* Send all known subnets and edges */
   
   for(node = node_tree->head; node; node = node->next)
     {
@@ -494,15 +494,12 @@ void send_everything(connection_t *c)
           s = (subnet_t *)node2->data;
           send_add_subnet(c, s);
         }
-    }
-
-  /* Send all known edges */
 
-  for(node = edge_tree->head; node; node = node->next)
-    {
-      e = (edge_t *)node->data;
-
-      send_add_edge(c, e);
+      for(node2 = n->edge_tree->head; node2; node2 = node2->next)
+        {
+          e = (edge_t *)node2->data;
+          send_add_edge(c, e);
+        }
     }
 }
 
@@ -513,8 +510,6 @@ int ack_h(connection_t *c)
   int weight;
   long int options;
   node_t *n;
-  connection_t *other;
-  avl_node_t *node;
 cp
   if(sscanf(c->buffer, "%*d "MAX_STRING" %d %lx", hisport, &weight, &options) != 3)
     {
@@ -541,14 +536,24 @@ cp
             syslog(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->name, n->hostname);
           terminate_connection(n->connection, 0);
         }
-          
-      /* FIXME: check if information in existing node matches that of the other end of this connection */
     }
   
   n->connection = c;
   c->node = n;
   c->options |= options;
 
+  /* Activate this connection */
+
+  c->allow_request = ALL;
+  c->status.active = 1;
+
+  if(debug_lvl >= DEBUG_CONNECTIONS)
+    syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, c->hostname);
+
+  /* Send him everything we know */
+
+  send_everything(c);
+
   /* Create an edge_t for this connection */
 
   c->edge = new_edge();
@@ -565,28 +570,10 @@ cp
 cp
   edge_add(c->edge);
 
-  /* Activate this connection */
-
-  c->allow_request = ALL;
-  c->status.active = 1;
-
-  if(debug_lvl >= DEBUG_CONNECTIONS)
-    syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, c->hostname);
-
 cp
-  /* Send him everything we know */
-
-  send_everything(c);
+  /* Notify everyone of the new edge */
 
-  /* Notify others of this connection */
-
-  for(node = connection_tree->head; node; node = node->next)
-    {
-      other = (connection_t *)node->data;
-
-      if(other->status.active && other != c)
-        send_add_edge(other, c->edge);
-    }
+  send_add_edge(broadcast, c->edge);
 
   /* Run MST and SSSP algorithms */
  
index 9b35a9fb91079ea1616085ed32ecd8a4e61ccba4..7663a46fefa11133c5854f5e8a3fa5bd56eda95b 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_edge.c,v 1.1.4.9 2002/09/04 13:48:52 guus Exp $
+    $Id: protocol_edge.c,v 1.1.4.10 2002/09/04 16:26:45 guus Exp $
 */
 
 #include "config.h"
@@ -62,7 +62,6 @@ cp
 
 int add_edge_h(connection_t *c)
 {
-  connection_t *other;
   edge_t *e;
   node_t *from, *to;
   char from_name[MAX_STRING_SIZE];
@@ -72,7 +71,6 @@ int add_edge_h(connection_t *c)
   sockaddr_t address;
   long int options;
   int weight;
-  avl_node_t *node;
 cp
   if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
             from_name, to_name, to_address, to_port, &options, &weight) != 6)
@@ -169,12 +167,7 @@ cp
 
   /* Tell the rest about the new edge */
 
-  for(node = connection_tree->head; node; node = node->next)
-    {
-      other = (connection_t *)node->data;
-      if(other->status.active && other != c)
-        send_request(other, "%s", c->buffer);
-    }
+  forward_request(c);
 
   /* Run MST before or after we tell the rest? */
 
@@ -196,8 +189,6 @@ int del_edge_h(connection_t *c)
   char from_name[MAX_STRING_SIZE];
   char to_name[MAX_STRING_SIZE];
   node_t *from, *to;
-  connection_t *other;
-  avl_node_t *node;
 cp
   if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING"", from_name, to_name) != 2)
     {
@@ -264,12 +255,7 @@ cp
 
   /* Tell the rest about the deleted edge */
 
-  for(node = connection_tree->head; node; node = node->next)
-    {
-      other = (connection_t *)node->data;
-      if(other->status.active && other != c)
-        send_request(other, "%s", c->buffer);
-    }
+  forward_request(c);
 
   /* Delete the edge */
   
index 0f14cd69c53251d0d6e02e3e2fce9b94240eb78e..029b41a4da848593b0687be23cdba68c7e8b048e 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_key.c,v 1.1.4.9 2002/09/04 08:02:33 guus Exp $
+    $Id: protocol_key.c,v 1.1.4.10 2002/09/04 16:26:45 guus Exp $
 */
 
 #include "config.h"
@@ -54,17 +54,13 @@ cp
 
   if(n == myself && !mykeyused)
     return 0;
-
-  send_request(NULL, "%d %lx %s", KEY_CHANGED, random(), n->name);
 cp
-  return 0;
+  return send_request(c, "%d %lx %s", KEY_CHANGED, random(), n->name);
 }
 
 int key_changed_h(connection_t *c)
 {
   char name[MAX_STRING_SIZE];
-  avl_node_t *node;
-  connection_t *other;
   node_t *n;
 cp
   if(sscanf(c->buffer, "%*d %*x "MAX_STRING, name) != 1)
@@ -91,12 +87,7 @@ cp
 
   /* Tell the others */
 
-  for(node = connection_tree->head; node; node = node->next)
-    {
-      other = (connection_t *)node->data;
-      if(other->status.active && other != c)
-        send_request(other, "%s", c->buffer);
-    }
+  forward_request(c);
 cp
   return 0;
 }
index e5103079dd11f9065e8cf33e7a851151abdb986b..a7344d7a5131eb0b29bef3733778216c5e86a9b9 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_subnet.c,v 1.1.4.5 2002/09/03 20:43:26 guus Exp $
+    $Id: protocol_subnet.c,v 1.1.4.6 2002/09/04 16:26:45 guus Exp $
 */
 
 #include "config.h"
@@ -60,9 +60,7 @@ int add_subnet_h(connection_t *c)
   char subnetstr[MAX_STRING_SIZE];
   char name[MAX_STRING_SIZE];
   node_t *owner;
-  connection_t *other;
   subnet_t *s;
-  avl_node_t *node;
 cp
   if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, name, subnetstr) != 2)
     {
@@ -124,13 +122,8 @@ cp
   subnet_add(owner, s);
 
   /* Tell the rest */
-  
-  for(node = connection_tree->head; node; node = node->next)
-    {
-      other = (connection_t *)node->data;
-      if(other->status.active && other != c)
-        send_request(other, "%s", c->buffer);
-    }
+
+  forward_request(c);
 cp
   return 0;
 }
@@ -152,9 +145,7 @@ int del_subnet_h(connection_t *c)
   char subnetstr[MAX_STRING_SIZE];
   char name[MAX_STRING_SIZE];
   node_t *owner;
-  connection_t *other;
   subnet_t *s, *find;
-  avl_node_t *node;
 cp
   if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, name, subnetstr) != 2)
     {
@@ -218,13 +209,8 @@ cp
   }
 
   /* Tell the rest */
-  
-  for(node = connection_tree->head; node; node = node->next)
-    {
-      other = (connection_t *)node->data;
-      if(other->status.active && other != c)
-        send_request(other, "%s", c->buffer);
-    }
+
+  forward_request(c);
 
   /* Finally, delete it. */