- Cleaned up and checked for some more NULL pointers in rbl.c
authorGuus Sliepen <guus@tinc-vpn.org>
Wed, 22 Nov 2000 18:54:08 +0000 (18:54 +0000)
committerGuus Sliepen <guus@tinc-vpn.org>
Wed, 22 Nov 2000 18:54:08 +0000 (18:54 +0000)
- Two connection lists: one for incoming connections, sorted on ip/port,
  one for connections whose identity we know, sorted on id ofcourse...

lib/rbl.c
src/connection.c
src/netutl.c
src/protocol.c

index c5114ef..ab35aee 100644 (file)
--- a/lib/rbl.c
+++ b/lib/rbl.c
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: rbl.c,v 1.1.2.9 2000/11/21 09:13:59 guus Exp $
+    $Id: rbl.c,v 1.1.2.10 2000/11/22 18:54:07 guus Exp $
 */
 
 #include "config.h"
 
 #include <stdlib.h>
 #include <xalloc.h>
 */
 
 #include "config.h"
 
 #include <stdlib.h>
 #include <xalloc.h>
+#include <stdio.h>
 
 #include "rbl.h"
 #include <system.h>
 
 #include "rbl.h"
 #include <system.h>
@@ -68,7 +69,7 @@ rbl_t *rbl_search_closest_rbl(rbltree_t *tree, void *data)
 {
   rbl_t *rbl, *next;
   int result;
 {
   rbl_t *rbl, *next;
   int result;
-  
+
   next = rbl = tree->top;
   
   while(next)
   next = rbl = tree->top;
   
   while(next)
@@ -90,31 +91,36 @@ rbl_t *rbl_search_closest_rbl(rbltree_t *tree, void *data)
 
 void *rbl_search_closest(rbltree_t *tree, void *data)
 {
 
 void *rbl_search_closest(rbltree_t *tree, void *data)
 {
-  return rbl_search_closest_rbl(tree, data)->data;
+  rbl_t *rbl;
+  
+  rbl = rbl_search_closest_rbl(tree, data);
+
+  if(rbl)
+    return rbl->data;
+  else
+    return NULL;
 }
 
 /* Search exact match or return NULL pointer */
 rbl_t *rbl_search_rbl(rbltree_t *tree, void *data)
 {
 }
 
 /* Search exact match or return NULL pointer */
 rbl_t *rbl_search_rbl(rbltree_t *tree, void *data)
 {
-  rbl_t *rbl, *next;
+  rbl_t *rbl;
   int result;
   int result;
+
+  rbl = tree->top;
   
   
-  next = rbl = tree->top;
-  
-  while(next)
+  while(rbl)
     {
     {
-      rbl = next;
-      
       result = tree->compare(data, rbl->data);
 
       if(result < 0)
       result = tree->compare(data, rbl->data);
 
       if(result < 0)
-        next = rbl->left;
+        rbl = rbl->left;
       else if(result > 0)
       else if(result > 0)
-        next = rbl->right;
+        rbl = rbl->right;
       else
         return rbl;
     }
       else
         return rbl;
     }
-    
+
   return NULL;
 }
 
   return NULL;
 }
 
@@ -437,7 +443,7 @@ rbl_t *rbl_unlink_rbl(rbl_t *rbl)
   
   if(y->color == RBL_BLACK && x)
     rbl_delete_fixup(x);
   
   if(y->color == RBL_BLACK && x)
     rbl_delete_fixup(x);
-    
+
   return rbl;
 }
 
   return rbl;
 }
 
@@ -449,38 +455,27 @@ rbl_t *rbl_unlink(rbltree_t *tree, void *data)
   rbl = rbl_search_rbl(tree, data);
   
   if(rbl)
   rbl = rbl_search_rbl(tree, data);
   
   if(rbl)
-    return rbl_unlink_rbl(rbl);
-  else
-    return NULL;
+    rbl_unlink_rbl(rbl);
+
+  return rbl;
 }
 
 /* Unlink node and free it */
 void rbl_delete_rbl(rbl_t *rbl)
 {
 }
 
 /* Unlink node and free it */
 void rbl_delete_rbl(rbl_t *rbl)
 {
-  free_rbl(rbl_unlink_rbl(rbl));
+  rbl_unlink_rbl(rbl);
+  free_rbl(rbl);
 }
 
 /* Search node in tree, unlink and free it */
 void rbl_delete(rbltree_t *tree, void *data)
 {
 }
 
 /* Search node in tree, unlink and free it */
 void rbl_delete(rbltree_t *tree, void *data)
 {
-  free_rbl(rbl_unlink(tree, data));
-}
-
-rbl_unlink_rbltree_branch(rbl_t *rbl)
-{
-  if(rbl->left)
-    rbl_unlink_rbltree_branch(rbl->left);
+  rbl_t *rbl;
 
 
-  if(rbl->right)
-    rbl_unlink_rbltree_branch(rbl->right);
+  rbl = rbl_unlink(tree, data);
 
 
-  if(rbl->parent)
-    {
-      if(rbl == rbl->parent->left)
-        rbl->parent->left = NULL;
-      else
-        rbl->parent->right = NULL;
-    }
+  if(rbl)
+    free_rbl(rbl);
 }
 
 /* Optimized unlinking for a complete tree */
 }
 
 /* Optimized unlinking for a complete tree */
@@ -512,8 +507,7 @@ void rbl_delete_rbltree(rbltree_t *tree)
   for(rbl = tree->head; rbl; rbl = next)
     {
       next = rbl->next;
   for(rbl = tree->head; rbl; rbl = next)
     {
       next = rbl->next;
-      if(tree->delete)
-        tree->delete(rbl->data);
+      free_rbl(rbl);
     }
 
   tree->top = NULL;
     }
 
   tree->top = NULL;
index 772b3c3..61657a3 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.
 
     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.3 2000/11/20 22:13:03 guus Exp $
+    $Id: connection.c,v 1.1.2.4 2000/11/22 18:54:07 guus Exp $
 */
 
 #include "config.h"
 */
 
 #include "config.h"
 /* Root of the connection list */
 
 rbltree_t *connection_tree;
 /* Root of the connection list */
 
 rbltree_t *connection_tree;
+rbltree_t *id_tree;
+
 connection_t *myself = NULL;
 
 /* Initialization and callbacks */
 
 int connection_compare(connection_t *a, connection_t *b)
 connection_t *myself = NULL;
 
 /* Initialization and callbacks */
 
 int connection_compare(connection_t *a, connection_t *b)
+{
+  ipv4_t result;
+  result = a->address - b->address;
+  if(result)
+    return result;
+  else
+    return a->port - b->port;
+}
+
+int id_compare(connection_t *a, connection_t *b)
 {
   return strcmp(a->name, b->name);
 }
 {
   return strcmp(a->name, b->name);
 }
@@ -52,6 +64,7 @@ int connection_compare(connection_t *a, connection_t *b)
 void init_connections(void)
 {
   connection_tree = new_rbltree((rbl_compare_t)connection_compare, (rbl_action_t)free_connection);
 void init_connections(void)
 {
   connection_tree = new_rbltree((rbl_compare_t)connection_compare, (rbl_action_t)free_connection);
+  id_tree = new_rbltree((rbl_compare_t)id_compare, NULL);
 }
 
 /* Creation and deletion of connection elements */
 }
 
 /* Creation and deletion of connection elements */
@@ -114,6 +127,7 @@ cp
 void destroy_connection_tree(void)
 {
 cp
 void destroy_connection_tree(void)
 {
 cp
+  rbl_delete_rbltree(id_tree);
   rbl_delete_rbltree(connection_tree);
 cp
 }
   rbl_delete_rbltree(connection_tree);
 cp
 }
@@ -127,26 +141,43 @@ cp
 cp
 }
 
 cp
 }
 
+void id_add(connection_t *cl)
+{
+cp
+  rbl_insert(id_tree, cl);
+cp
+}
+
 void connection_del(connection_t *cl)
 {
 cp
 void connection_del(connection_t *cl)
 {
 cp
+  rbl_delete(id_tree, cl);
   rbl_delete(connection_tree, cl);
 cp
 }
 
 /* Lookup functions */
 
   rbl_delete(connection_tree, cl);
 cp
 }
 
 /* Lookup functions */
 
+connection_t *lookup_connection(ipv4_t address, short unsigned int port)
+{
+  connection_t cl, *p;
+cp
+  cl.address = address;
+  cl.port = port;
+
+  return rbl_search(connection_tree, &cl);
+}
+
 connection_t *lookup_id(char *name)
 {
   connection_t cl, *p;
 cp
   cl.name = name;
 connection_t *lookup_id(char *name)
 {
   connection_t cl, *p;
 cp
   cl.name = name;
-  p = rbl_search(connection_tree, &cl);
+  p = rbl_search(id_tree, &cl);
   if(p && p->status.active)
     return p;
   else
     return NULL;
   if(p && p->status.active)
     return p;
   else
     return NULL;
-cp
 }
 
 /* Debugging */
 }
 
 /* Debugging */
index d92d3ee..7e02ad0 100644 (file)
@@ -16,7 +16,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: netutl.c,v 1.12.4.15 2000/11/04 22:57:31 guus Exp $
+    $Id: netutl.c,v 1.12.4.16 2000/11/22 18:54:08 guus Exp $
 */
 
 #include "config.h"
 */
 
 #include "config.h"
@@ -111,7 +111,9 @@ cp
 
   if(!(h = gethostbyname(p)))
     {
 
   if(!(h = gethostbyname(p)))
     {
-      fprintf(stderr, _("Error looking up `%s': %s\n"), p, strerror(errno));
+      if(debug_lvl >= DEBUG_ERROR)
+        syslog(LOG_WARNING, _("Error looking up `%s': %s\n"), p, strerror(errno));
+        
       return NULL;
     }
 
       return NULL;
     }
 
index 7581804..69a6918 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.
 
     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.62 2000/11/20 19:12:13 guus Exp $
+    $Id: protocol.c,v 1.28.4.63 2000/11/22 18:54:08 guus Exp $
 */
 
 #include "config.h"
 */
 
 #include "config.h"
@@ -216,7 +216,7 @@ cp
     }
 
   /* Load information about peer */
     }
 
   /* Load information about peer */
-cp
+
   if(read_host_config(cl))
     {
       syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), cl->hostname, cl->name);
   if(read_host_config(cl))
     {
       syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), cl->hostname, cl->name);
@@ -227,7 +227,7 @@ cp
      connection list. If so, we are probably making a loop, which
      is not desirable.
    */
      connection list. If so, we are probably making a loop, which
      is not desirable.
    */
-cp
+
   if(cl->status.outgoing)
     {
       if((old = lookup_id(cl->name)))
   if(cl->status.outgoing)
     {
       if((old = lookup_id(cl->name)))
@@ -240,7 +240,13 @@ cp
           return 0;
         }
     }
           return 0;
         }
     }
-cp    
+    
+  /* Now we can add the name to the id tree */
+  
+  id_add(cl);
+
+  /* Read in the public key, so that we can send a challenge */
+
   if((cfg = get_config_val(cl->config, config_publickey)))
     {
       cl->rsa_key = RSA_new();
   if((cfg = get_config_val(cl->config, config_publickey)))
     {
       cl->rsa_key = RSA_new();
@@ -722,6 +728,17 @@ cp
     {
       syslog(LOG_ERR, _("Got ADD_SUBNET for %s from %s (%s) which is not in our connection list"),
              name, cl->name, cl->hostname);
     {
       syslog(LOG_ERR, _("Got ADD_SUBNET for %s from %s (%s) which is not in our connection list"),
              name, cl->name, cl->hostname);
+      cp_trace();
+      dump_connection_list();
+      {
+        connection_t cl;
+        rbl_t *rbl;
+        cl.name = name;
+        rbl = rbl_search_rbl(connection_tree, &cl);
+        syslog(LOG_ERR, "rbl_search_rbl: %p", rbl);
+        if(rbl)
+          syslog(LOG_ERR, "rbl->data->name: %s", ((connection_t *)rbl->data)->name);
+      }
       free(name);
       return -1;
     }
       free(name);
       return -1;
     }
@@ -896,6 +913,7 @@ cp
   /* Hook it up into the connection */
 
   connection_add(new);
   /* Hook it up into the connection */
 
   connection_add(new);
+  id_add(new);
 
   /* Tell the rest about the new host */
 
 
   /* Tell the rest about the new host */