From 12f6b80429bc05a828051d72cc46f173e4657180 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 20 Jul 2001 20:25:10 +0000 Subject: [PATCH] Added purge_tree for connection_t's which are no longer in the connection, active or id trees, but which may still be referenced. This tree is flushed when it is safe, this replaces purge_connection_tree(). Also lots of bugfixes related to the new trees. --- src/connection.c | 105 +++++++++++++++++++++++++++++++---------------- src/connection.h | 8 ++-- src/net.c | 60 +++++++++++++-------------- src/protocol.c | 14 ++----- src/route.c | 4 +- 5 files changed, 109 insertions(+), 82 deletions(-) diff --git a/src/connection.c b/src/connection.c index c09ca94a..fa2f81d7 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.13 2001/07/15 18:07:31 guus Exp $ + $Id: connection.c,v 1.1.2.14 2001/07/20 20:25:10 guus Exp $ */ #include "config.h" @@ -41,9 +41,10 @@ /* Root of the connection list */ -avl_tree_t *connection_tree; -avl_tree_t *active_tree; -avl_tree_t *id_tree; +avl_tree_t *connection_tree; /* Meta connections */ +avl_tree_t *active_tree; /* Activated hosts, sorted by address and port */ +avl_tree_t *id_tree; /* Activated hosts, sorted by name */ +avl_tree_t *prune_tree; /* connection_t structures which have to be freed */ /* Pointer to connection describing myself */ @@ -72,11 +73,22 @@ int id_compare(connection_t *a, connection_t *b) return strcmp(a->name, b->name); } +int prune_compare(connection_t *a, connection_t *b) +{ + if(a < b) + return -1; + else if(a > b) + return 1; + else + return 0; +} + void init_connections(void) { - connection_tree = avl_alloc_tree((avl_compare_t)connection_compare, (avl_action_t)free_connection); + connection_tree = avl_alloc_tree((avl_compare_t)connection_compare, NULL); active_tree = avl_alloc_tree((avl_compare_t)active_compare, NULL); id_tree = avl_alloc_tree((avl_compare_t)id_compare, NULL); + prune_tree = avl_alloc_tree((avl_compare_t)prune_compare, (avl_action_t)free_connection); } /* Creation and deletion of connection elements */ @@ -113,36 +125,19 @@ cp } /* - remove all marked connections + Free all trees. */ -void prune_connection_tree(void) -{ - avl_node_t *node, *next; - connection_t *cl; -cp - for(node = connection_tree->head; node; node = next) - { - next = node->next; - cl = (connection_t *)node->data; - if(cl->status.remove) - connection_del(cl); - } -cp -} - -/* - free all elements of connection -*/ -void destroy_connection_tree(void) +void destroy_trees(void) { cp avl_delete_tree(id_tree); avl_delete_tree(active_tree); avl_delete_tree(connection_tree); + avl_delete_tree(prune_tree); cp } -/* Linked list management */ +/* Connection management */ void connection_add(connection_t *cl) { @@ -151,10 +146,34 @@ cp cp } +void connection_del(connection_t *cl) +{ +cp + active_del(cl); + + if(cl->status.meta) + avl_delete(connection_tree, cl); +cp +} + void active_add(connection_t *cl) { cp avl_insert(active_tree, cl); + avl_insert(id_tree, cl); + cl->status.active = 1; +cp +} + +void active_del(connection_t *cl) +{ +cp + if(cl->status.active) + { + avl_delete(id_tree, cl); + avl_delete(active_tree, cl); + cl->status.active = 0; + } cp } @@ -165,12 +184,22 @@ cp cp } -void connection_del(connection_t *cl) +void prune_add(connection_t *cl) { cp - avl_delete(id_tree, cl); - avl_delete(active_tree, cl); - avl_delete(connection_tree, cl); + avl_insert(prune_tree, cl); +cp +} + +void prune_flush(void) +{ + avl_node_t *node, *next; +cp + for(node = prune_tree->head; node; node = next) + { + next = node->next; + avl_delete_node(prune_tree, node); + } cp } @@ -192,7 +221,7 @@ connection_t *lookup_id(char *name) cp cl.name = name; p = avl_search(id_tree, &cl); - if(p && p->status.active) + if(p) return p; else return NULL; @@ -207,10 +236,6 @@ void dump_connection_list(void) cp syslog(LOG_DEBUG, _("Connection list:")); - syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"), - myself->name, myself->hostname, myself->port, myself->options, - myself->socket, myself->meta_socket, myself->status); - for(node = connection_tree->head; node; node = node->next) { cl = (connection_t *)node->data; @@ -219,6 +244,16 @@ cp cl->socket, cl->meta_socket, cl->status); } + syslog(LOG_DEBUG, _("Known hosts:")); + + for(node = id_tree->head; node; node = node->next) + { + cl = (connection_t *)node->data; + syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"), + cl->name, cl->hostname, cl->port, cl->options, + cl->socket, cl->meta_socket, cl->status); + } + syslog(LOG_DEBUG, _("End of connection list.")); cp } diff --git a/src/connection.h b/src/connection.h index 4532eb49..fb7d2797 100644 --- a/src/connection.h +++ b/src/connection.h @@ -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.10 2001/07/15 18:07:31 guus Exp $ + $Id: connection.h,v 1.1.2.11 2001/07/20 20:25:10 guus Exp $ */ #ifndef __TINC_CONNECTION_H__ @@ -112,13 +112,15 @@ extern connection_t *new_connection(void); extern void free_connection(connection_t *); extern void id_add(connection_t *); extern void active_add(connection_t *); +extern void active_del(connection_t *); extern void connection_add(connection_t *); extern void connection_del(connection_t *); +extern void prune_add(connection_t *); +extern void prune_flush(void); extern connection_t *lookup_id(char *); extern connection_t *lookup_active(ipv4_t, short unsigned int); extern void dump_connection_list(void); extern int read_host_config(connection_t *); -extern void destroy_connection_tree(void); -extern void prune_connection_tree(void); +extern void destroy_trees(void); #endif /* __TINC_CONNECTION_H__ */ diff --git a/src/net.c b/src/net.c index dc2d65d0..00fbdc5a 100644 --- 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.122 2001/07/20 13:54:19 guus Exp $ + $Id: net.c,v 1.35.4.123 2001/07/20 20:25:10 guus Exp $ */ #include "config.h" @@ -885,10 +885,10 @@ cp keyexpires = time(NULL) + keylifetime; cp - - /* Activate ourselves */ + /* Done */ myself->status.active = 1; + id_add(myself); syslog(LOG_NOTICE, _("Ready: listening on port %hd"), myself->port); cp @@ -1001,30 +1001,24 @@ cp */ void close_network_connections(void) { - avl_node_t *node; + avl_node_t *node, *next; connection_t *p; cp - for(node = connection_tree->head; node; node = node->next) + for(node = connection_tree->head; node; node = next) { + next = node->next; p = (connection_t *)node->data; p->status.outgoing = 0; - p->status.active = 0; terminate_connection(p); } - if(myself) - if(myself->status.active) - { - close(myself->meta_socket); - free_connection(myself); - myself = NULL; - } + terminate_connection(myself); + + destroy_trees(); execute_script("tinc-down"); close(tap_fd); - - destroy_connection_tree(); cp return; } @@ -1137,8 +1131,11 @@ cp } /* - terminate a connection and notify the other - end before closing the sockets + Terminate a connection: + - Close the sockets + - Remove associated hosts and subnets + - Deactivate the host + - Since it might still be referenced, put it on the prune list. */ void terminate_connection(connection_t *cl) { @@ -1148,25 +1145,26 @@ void terminate_connection(connection_t *cl) cp if(cl->status.remove) return; - - if(debug_lvl >= DEBUG_CONNECTIONS) - syslog(LOG_NOTICE, _("Closing connection with %s (%s)"), - cl->name, cl->hostname); - - cl->status.remove = 1; + else + cl->status.remove = 1; if(cl->socket) close(cl->socket); - if(cl->status.meta) - close(cl->meta_socket); if(cl->status.meta) { + if(debug_lvl >= DEBUG_CONNECTIONS) + syslog(LOG_NOTICE, _("Closing connection with %s (%s)"), + cl->name, cl->hostname); + + close(cl->meta_socket); + /* Find all connections that were lost because they were behind cl (the connection that was dropped). */ - for(node = active_tree->head; node; node = node->next) + for(node = active_tree->head; node; node = next) { + next = node->next; p = (connection_t *)node->data; if(p->nexthop == cl && p != cl) terminate_connection(p); @@ -1201,11 +1199,11 @@ cp alarm(seconds_till_retry); syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), seconds_till_retry); } - - /* Deactivate */ - - cl->status.active = 0; cp + /* Schedule it for pruning */ + + prune_add(cl); + connection_del(cl); } /* @@ -1376,7 +1374,7 @@ cp tv.tv_sec = timeout; tv.tv_usec = 0; - prune_connection_tree(); + prune_flush(); build_fdset(&fset); if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0) diff --git a/src/protocol.c b/src/protocol.c index 29282a1c..d0dd9f78 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.101 2001/07/20 13:54:19 guus Exp $ + $Id: protocol.c,v 1.28.4.102 2001/07/20 20:25:10 guus Exp $ */ #include "config.h" @@ -249,10 +249,6 @@ cp return 0; } - /* Now we can add the name to the id tree */ - - id_add(cl); - /* Also check if no other tinc daemon uses the same IP and port for UDP traffic */ old = avl_search(active_tree, cl); @@ -266,7 +262,6 @@ cp /* Activate this connection */ cl->allow_request = ALL; - cl->status.active = 1; cl->nexthop = cl; cl->cipher_pkttype = EVP_bf_cbc(); cl->cipher_pktkeylength = cl->cipher_pkttype->key_len + cl->cipher_pkttype->iv_len; @@ -856,7 +851,6 @@ cp new->name = xstrdup(name); active_add(new); - id_add(new); /* Tell the rest about the new host */ @@ -870,7 +864,6 @@ cp /* Fill in rest of connection structure */ new->nexthop = cl; - new->status.active = 1; new->cipher_pkttype = EVP_bf_cbc(); new->cipher_pktkeylength = cl->cipher_pkttype->key_len + cl->cipher_pkttype->iv_len; cp @@ -921,7 +914,7 @@ cp return 0; } - /* Check if the new host already exists in the connnection list */ + /* Check if the deleted host already exists in the connnection list */ if(!(old = lookup_id(name))) { @@ -940,10 +933,9 @@ cp /* Ok, since EVERYTHING seems to check out all right, delete it */ - old->status.active = 0; terminate_connection(old); - /* Tell the rest about the new host */ + /* Tell the rest about the deleted host */ for(node = connection_tree->head; node; node = node->next) { diff --git a/src/route.c b/src/route.c index 5bf0b55b..a143082d 100644 --- a/src/route.c +++ b/src/route.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: route.c,v 1.1.2.16 2001/07/20 13:54:19 guus Exp $ + $Id: route.c,v 1.1.2.17 2001/07/20 20:25:10 guus Exp $ */ #include "config.h" @@ -78,7 +78,7 @@ cp for(node = connection_tree->head; node; node = node->next) { p = (connection_t *)node->data; - if(p->status.active && p!= myself) + if(p->status.active) send_add_subnet(p, subnet); } } -- 2.39.5