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.13 2001/07/15 18:07:31 guus Exp $
32 #include "net.h" /* Don't ask. */
42 /* Root of the connection list */
44 avl_tree_t *connection_tree;
45 avl_tree_t *active_tree;
48 /* Pointer to connection describing myself */
50 connection_t *myself = NULL;
52 /* Initialization and callbacks */
54 int connection_compare(connection_t *a, connection_t *b)
56 return a->meta_socket - b->meta_socket;
59 int active_compare(connection_t *a, connection_t *b)
63 result = a->address - b->address;
67 return a->port - b->port;
70 int id_compare(connection_t *a, connection_t *b)
72 return strcmp(a->name, b->name);
75 void init_connections(void)
77 connection_tree = avl_alloc_tree((avl_compare_t)connection_compare, (avl_action_t)free_connection);
78 active_tree = avl_alloc_tree((avl_compare_t)active_compare, NULL);
79 id_tree = avl_alloc_tree((avl_compare_t)id_compare, NULL);
82 /* Creation and deletion of connection elements */
84 connection_t *new_connection(void)
86 connection_t *p = (connection_t *)xmalloc_and_zero(sizeof(*p));
88 p->subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
89 p->queue = list_alloc((list_action_t)free);
94 void free_connection(connection_t *p)
98 list_delete_list(p->queue);
104 RSA_free(p->rsa_key);
106 free(p->cipher_pktkey);
110 clear_config(&p->config);
116 remove all marked connections
118 void prune_connection_tree(void)
120 avl_node_t *node, *next;
123 for(node = connection_tree->head; node; node = next)
126 cl = (connection_t *)node->data;
127 if(cl->status.remove)
134 free all elements of connection
136 void destroy_connection_tree(void)
139 avl_delete_tree(id_tree);
140 avl_delete_tree(active_tree);
141 avl_delete_tree(connection_tree);
145 /* Linked list management */
147 void connection_add(connection_t *cl)
150 avl_insert(connection_tree, cl);
154 void active_add(connection_t *cl)
157 avl_insert(active_tree, cl);
161 void id_add(connection_t *cl)
164 avl_insert(id_tree, cl);
168 void connection_del(connection_t *cl)
171 avl_delete(id_tree, cl);
172 avl_delete(active_tree, cl);
173 avl_delete(connection_tree, cl);
177 /* Lookup functions */
179 connection_t *lookup_active(ipv4_t address, short unsigned int port)
183 cl.address = address;
186 return avl_search(active_tree, &cl);
189 connection_t *lookup_id(char *name)
194 p = avl_search(id_tree, &cl);
195 if(p && p->status.active)
203 void dump_connection_list(void)
208 syslog(LOG_DEBUG, _("Connection list:"));
210 syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"),
211 myself->name, myself->hostname, myself->port, myself->options,
212 myself->socket, myself->meta_socket, myself->status);
214 for(node = connection_tree->head; node; node = node->next)
216 cl = (connection_t *)node->data;
217 syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"),
218 cl->name, cl->hostname, cl->port, cl->options,
219 cl->socket, cl->meta_socket, cl->status);
222 syslog(LOG_DEBUG, _("End of connection list."));
226 int read_host_config(connection_t *cl)
231 asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
232 x = read_config_file(&cl->config, fname);