- Cleaned up and checked for some more NULL pointers in rbl.c

- Two connection lists: one for incoming connections, sorted on ip/port,
  one for connections whose identity we know, sorted on id ofcourse...
This commit is contained in:
Guus Sliepen 2000-11-22 18:54:08 +00:00
parent 785684f0ec
commit f8b4a000d0
4 changed files with 89 additions and 44 deletions

View file

@ -17,13 +17,14 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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 "config.h"
#include <stdlib.h> #include <stdlib.h>
#include <xalloc.h> #include <xalloc.h>
#include <stdio.h>
#include "rbl.h" #include "rbl.h"
#include <system.h> #include <system.h>
@ -90,27 +91,32 @@ 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 */ /* Search exact match or return NULL pointer */
rbl_t *rbl_search_rbl(rbltree_t *tree, void *data) rbl_t *rbl_search_rbl(rbltree_t *tree, void *data)
{ {
rbl_t *rbl, *next; rbl_t *rbl;
int result; int result;
next = rbl = tree->top; rbl = tree->top;
while(next) while(rbl)
{ {
rbl = next;
result = tree->compare(data, rbl->data); result = tree->compare(data, rbl->data);
if(result < 0) if(result < 0)
next = rbl->left; rbl = rbl->left;
else if(result > 0) else if(result > 0)
next = rbl->right; rbl = rbl->right;
else else
return rbl; return rbl;
} }
@ -449,38 +455,27 @@ rbl_t *rbl_unlink(rbltree_t *tree, void *data)
rbl = rbl_search_rbl(tree, data); rbl = rbl_search_rbl(tree, data);
if(rbl) if(rbl)
return rbl_unlink_rbl(rbl); rbl_unlink_rbl(rbl);
else
return NULL; return rbl;
} }
/* Unlink node and free it */ /* Unlink node and free it */
void rbl_delete_rbl(rbl_t *rbl) 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 */ /* Search node in tree, unlink and free it */
void rbl_delete(rbltree_t *tree, void *data) void rbl_delete(rbltree_t *tree, void *data)
{ {
free_rbl(rbl_unlink(tree, data)); rbl_t *rbl;
}
rbl_unlink_rbltree_branch(rbl_t *rbl) rbl = rbl_unlink(tree, data);
{
if(rbl->left)
rbl_unlink_rbltree_branch(rbl->left);
if(rbl->right) if(rbl)
rbl_unlink_rbltree_branch(rbl->right); free_rbl(rbl);
if(rbl->parent)
{
if(rbl == rbl->parent->left)
rbl->parent->left = NULL;
else
rbl->parent->right = NULL;
}
} }
/* 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) for(rbl = tree->head; rbl; rbl = next)
{ {
next = rbl->next; next = rbl->next;
if(tree->delete) free_rbl(rbl);
tree->delete(rbl->data);
} }
tree->top = NULL; tree->top = NULL;

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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"
@ -40,11 +40,23 @@
/* Root of the connection list */ /* Root of the connection list */
rbltree_t *connection_tree; rbltree_t *connection_tree;
rbltree_t *id_tree;
connection_t *myself = NULL; connection_t *myself = NULL;
/* Initialization and callbacks */ /* Initialization and callbacks */
int connection_compare(connection_t *a, connection_t *b) 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) void init_connections(void)
{ {
connection_tree = new_rbltree((rbl_compare_t)connection_compare, (rbl_action_t)free_connection); 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) void destroy_connection_tree(void)
{ {
cp cp
rbl_delete_rbltree(id_tree);
rbl_delete_rbltree(connection_tree); rbl_delete_rbltree(connection_tree);
cp 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) void connection_del(connection_t *cl)
{ {
cp cp
rbl_delete(id_tree, cl);
rbl_delete(connection_tree, cl); rbl_delete(connection_tree, cl);
cp cp
} }
/* Lookup functions */ /* 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 *lookup_id(char *name)
{ {
connection_t cl, *p; connection_t cl, *p;
cp cp
cl.name = name; cl.name = name;
p = rbl_search(connection_tree, &cl); p = rbl_search(id_tree, &cl);
if(p && p->status.active) if(p && p->status.active)
return p; return p;
else else
return NULL; return NULL;
cp
} }
/* Debugging */ /* Debugging */

View file

@ -16,7 +16,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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;
} }

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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)) if(read_host_config(cl))
{ {
syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), cl->hostname, cl->name); 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 connection list. If so, we are probably making a loop, which
is not desirable. is not desirable.
*/ */
cp
if(cl->status.outgoing) if(cl->status.outgoing)
{ {
if((old = lookup_id(cl->name))) 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))) if((cfg = get_config_val(cl->config, config_publickey)))
{ {
cl->rsa_key = RSA_new(); 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"), syslog(LOG_ERR, _("Got ADD_SUBNET for %s from %s (%s) which is not in our connection list"),
name, cl->name, cl->hostname); 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); free(name);
return -1; return -1;
} }
@ -896,6 +913,7 @@ cp
/* Hook it up into the connection */ /* Hook it up into the connection */
connection_add(new); connection_add(new);
id_add(new);
/* Tell the rest about the new host */ /* Tell the rest about the new host */