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.12 2001/06/29 13:09:55 guus Exp $
32 #include "net.h" /* Don't ask. */
42 /* Root of the connection list */
44 avl_tree_t *connection_tree;
47 /* Pointer to connection describing myself */
49 connection_t *myself = NULL;
51 /* Initialization and callbacks */
53 int connection_compare(connection_t *a, connection_t *b)
57 result = a->address - b->address;
61 return a->port - b->port;
64 int id_compare(connection_t *a, connection_t *b)
66 return strcmp(a->name, b->name);
69 void init_connections(void)
71 connection_tree = avl_alloc_tree((avl_compare_t)connection_compare, (avl_action_t)free_connection);
72 id_tree = avl_alloc_tree((avl_compare_t)id_compare, NULL);
75 /* Creation and deletion of connection elements */
77 connection_t *new_connection(void)
79 connection_t *p = (connection_t *)xmalloc_and_zero(sizeof(*p));
81 p->subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
82 p->queue = list_alloc((list_action_t)free);
87 void free_connection(connection_t *p)
91 list_delete_list(p->queue);
99 free(p->cipher_pktkey);
103 clear_config(&p->config);
109 remove all marked connections
111 void prune_connection_tree(void)
113 avl_node_t *node, *next;
116 for(node = connection_tree->head; node; node = next)
119 cl = (connection_t *)node->data;
120 if(cl->status.remove)
127 free all elements of connection
129 void destroy_connection_tree(void)
132 avl_delete_tree(id_tree);
133 avl_delete_tree(connection_tree);
137 /* Linked list management */
139 void connection_add(connection_t *cl)
142 avl_insert(connection_tree, cl);
146 void id_add(connection_t *cl)
149 avl_insert(id_tree, cl);
153 void connection_del(connection_t *cl)
156 avl_delete(id_tree, cl);
157 avl_delete(connection_tree, cl);
161 /* Lookup functions */
163 connection_t *lookup_connection(ipv4_t address, short unsigned int port)
167 cl.address = address;
170 return avl_search(connection_tree, &cl);
173 connection_t *lookup_id(char *name)
178 p = avl_search(id_tree, &cl);
179 if(p && p->status.active)
187 void dump_connection_list(void)
192 syslog(LOG_DEBUG, _("Connection list:"));
194 syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"),
195 myself->name, myself->hostname, myself->port, myself->options,
196 myself->socket, myself->meta_socket, myself->status);
198 for(node = connection_tree->head; node; node = node->next)
200 cl = (connection_t *)node->data;
201 syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"),
202 cl->name, cl->hostname, cl->port, cl->options,
203 cl->socket, cl->meta_socket, cl->status);
206 syslog(LOG_DEBUG, _("End of connection list."));
210 int read_host_config(connection_t *cl)
215 asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
216 x = read_config_file(&cl->config, fname);