2 connection.c -- connection list management
3 Copyright (C) 2000,2001 Guus Sliepen <guus@sliepen.warande.net>,
4 2000,2001 Ivo Timmermans <itimmermans@bigfoot.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: connection.c,v 1.1.2.15 2001/07/21 15:34:18 guus Exp $
32 #include "net.h" /* Don't ask. */
42 /* Root of the connection list */
44 avl_tree_t *connection_tree; /* Meta connections */
45 avl_tree_t *active_tree; /* Activated hosts, sorted by address and port */
46 avl_tree_t *id_tree; /* Activated hosts, sorted by name */
47 avl_tree_t *prune_tree; /* connection_t structures which have to be freed */
49 /* Pointer to connection describing myself */
51 connection_t *myself = NULL;
53 /* Initialization and callbacks */
55 int connection_compare(connection_t *a, connection_t *b)
57 return a->meta_socket - b->meta_socket;
60 int active_compare(connection_t *a, connection_t *b)
64 result = a->address - b->address;
68 return a->port - b->port;
71 int id_compare(connection_t *a, connection_t *b)
73 return strcmp(a->name, b->name);
76 int prune_compare(connection_t *a, connection_t *b)
86 void init_connections(void)
88 connection_tree = avl_alloc_tree((avl_compare_t)connection_compare, NULL);
89 active_tree = avl_alloc_tree((avl_compare_t)active_compare, NULL);
90 id_tree = avl_alloc_tree((avl_compare_t)id_compare, NULL);
91 prune_tree = avl_alloc_tree((avl_compare_t)prune_compare, (avl_action_t)free_connection);
94 /* Creation and deletion of connection elements */
96 connection_t *new_connection(void)
98 connection_t *p = (connection_t *)xmalloc_and_zero(sizeof(*p));
100 p->subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
101 p->queue = list_alloc((list_action_t)free);
106 void free_connection(connection_t *p)
110 list_delete_list(p->queue);
116 RSA_free(p->rsa_key);
118 free(p->cipher_pktkey);
122 clear_config(&p->config);
130 void destroy_trees(void)
133 avl_delete_tree(id_tree);
134 avl_delete_tree(active_tree);
135 avl_delete_tree(connection_tree);
136 avl_delete_tree(prune_tree);
140 /* Connection management */
142 void connection_add(connection_t *cl)
145 avl_insert(connection_tree, cl);
149 void connection_del(connection_t *cl)
155 avl_delete(connection_tree, cl);
159 void active_add(connection_t *cl)
162 avl_insert(active_tree, cl);
163 avl_insert(id_tree, cl);
164 cl->status.active = 1;
168 void active_del(connection_t *cl)
171 if(cl->status.active)
173 avl_delete(id_tree, cl);
174 avl_delete(active_tree, cl);
179 void id_add(connection_t *cl)
182 avl_insert(id_tree, cl);
186 void prune_add(connection_t *cl)
189 avl_insert(prune_tree, cl);
193 void prune_flush(void)
195 avl_node_t *node, *next;
197 for(node = prune_tree->head; node; node = next)
200 avl_delete_node(prune_tree, node);
205 /* Lookup functions */
207 connection_t *lookup_active(ipv4_t address, short unsigned int port)
211 cl.address = address;
214 return avl_search(active_tree, &cl);
217 connection_t *lookup_id(char *name)
222 p = avl_search(id_tree, &cl);
231 void dump_connection_list(void)
236 syslog(LOG_DEBUG, _("Connection list:"));
238 for(node = connection_tree->head; node; node = node->next)
240 cl = (connection_t *)node->data;
241 syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"),
242 cl->name, cl->hostname, cl->port, cl->options,
243 cl->socket, cl->meta_socket, cl->status);
246 syslog(LOG_DEBUG, _("Known hosts:"));
248 for(node = id_tree->head; node; node = node->next)
250 cl = (connection_t *)node->data;
251 syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"),
252 cl->name, cl->hostname, cl->port, cl->options,
253 cl->socket, cl->meta_socket, cl->status);
256 syslog(LOG_DEBUG, _("End of connection list."));
260 int read_host_config(connection_t *cl)
265 asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
266 x = read_config_file(&cl->config, fname);