From: Guus Sliepen Date: Wed, 22 Nov 2000 18:54:08 +0000 (+0000) Subject: - Cleaned up and checked for some more NULL pointers in rbl.c X-Git-Tag: release-1.0pre4~147 X-Git-Url: https://tinc-vpn.org/git/browse?a=commitdiff_plain;h=f8b4a000d008082e5c7e511a49318b8dea8fd08d;p=tinc - 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... --- diff --git a/lib/rbl.c b/lib/rbl.c index c5114ef5..ab35aee6 100644 --- a/lib/rbl.c +++ b/lib/rbl.c @@ -17,13 +17,14 @@ 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 #include +#include #include "rbl.h" #include @@ -68,7 +69,7 @@ rbl_t *rbl_search_closest_rbl(rbltree_t *tree, void *data) { rbl_t *rbl, *next; int result; - + 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) { - 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) { - rbl_t *rbl, *next; + rbl_t *rbl; int result; + + rbl = tree->top; - next = rbl = tree->top; - - while(next) + while(rbl) { - rbl = next; - result = tree->compare(data, rbl->data); if(result < 0) - next = rbl->left; + rbl = rbl->left; else if(result > 0) - next = rbl->right; + rbl = rbl->right; else return rbl; } - + return NULL; } @@ -437,7 +443,7 @@ rbl_t *rbl_unlink_rbl(rbl_t *rbl) if(y->color == RBL_BLACK && x) rbl_delete_fixup(x); - + return rbl; } @@ -449,38 +455,27 @@ rbl_t *rbl_unlink(rbltree_t *tree, void *data) 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) { - 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) { - 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 */ @@ -512,8 +507,7 @@ void rbl_delete_rbltree(rbltree_t *tree) for(rbl = tree->head; rbl; rbl = next) { next = rbl->next; - if(tree->delete) - tree->delete(rbl->data); + free_rbl(rbl); } tree->top = NULL; diff --git a/src/connection.c b/src/connection.c index 772b3c37..61657a3b 100644 --- a/src/connection.c +++ b/src/connection.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: 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" @@ -40,11 +40,23 @@ /* 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) +{ + 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); } @@ -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); + id_tree = new_rbltree((rbl_compare_t)id_compare, NULL); } /* Creation and deletion of connection elements */ @@ -114,6 +127,7 @@ cp void destroy_connection_tree(void) { cp + rbl_delete_rbltree(id_tree); rbl_delete_rbltree(connection_tree); cp } @@ -127,26 +141,43 @@ cp cp } +void id_add(connection_t *cl) +{ +cp + rbl_insert(id_tree, cl); +cp +} + void connection_del(connection_t *cl) { cp + rbl_delete(id_tree, cl); 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; - p = rbl_search(connection_tree, &cl); + p = rbl_search(id_tree, &cl); if(p && p->status.active) return p; else return NULL; -cp } /* Debugging */ diff --git a/src/netutl.c b/src/netutl.c index d92d3ee5..7e02ad0d 100644 --- a/src/netutl.c +++ b/src/netutl.c @@ -16,7 +16,7 @@ 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" @@ -111,7 +111,9 @@ cp 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; } diff --git a/src/protocol.c b/src/protocol.c index 75818043..69a6918f 100644 --- a/src/protocol.c +++ b/src/protocol.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: 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" @@ -216,7 +216,7 @@ cp } /* Load information about peer */ -cp + 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. */ -cp + if(cl->status.outgoing) { if((old = lookup_id(cl->name))) @@ -240,7 +240,13 @@ cp 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(); @@ -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); + 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; } @@ -896,6 +913,7 @@ cp /* Hook it up into the connection */ connection_add(new); + id_add(new); /* Tell the rest about the new host */