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"
/* 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 */
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 */
}
/*
- 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)
{
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
}
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
}
cp
cl.name = name;
p = avl_search(id_tree, &cl);
- if(p && p->status.active)
+ if(p)
return p;
else
return NULL;
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;
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
}
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__
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__ */
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"
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
*/
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;
}
}
/*
- 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)
{
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);
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);
}
/*
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)
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"
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);
/* 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;
new->name = xstrdup(name);
active_add(new);
- id_add(new);
/* Tell the rest about the new host */
/* 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
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)))
{
/* 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)
{
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"
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);
}
}