2 connection.c -- connection list management
3 Copyright (C) 2000 Guus Sliepen <guus@sliepen.warande.net>,
4 2000 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.8 2001/01/07 15:25:40 guus Exp $
31 #include "net.h" /* Don't ask. */
41 /* Root of the connection list */
43 avl_tree_t *connection_tree;
46 /* Pointer to connection describing myself */
48 connection_t *myself = NULL;
50 /* Initialization and callbacks */
52 int connection_compare(connection_t *a, connection_t *b)
56 result = a->address - b->address;
60 return a->port - b->port;
63 int id_compare(connection_t *a, connection_t *b)
65 return strcmp(a->name, b->name);
68 void init_connections(void)
70 connection_tree = avl_alloc_tree((avl_compare_t)connection_compare, (avl_action_t)free_connection);
71 id_tree = avl_alloc_tree((avl_compare_t)id_compare, NULL);
74 /* Creation and deletion of connection elements */
76 connection_t *new_connection(void)
78 connection_t *p = (connection_t *)xmalloc_and_zero(sizeof(*p));
80 p->subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
81 p->queue = list_alloc((list_action_t)free);
86 void free_connection(connection_t *p)
90 list_delete_list(p->queue);
91 if(p->name && p->name!=unknown)
98 free(p->cipher_pktkey);
102 clear_config(&p->config);
108 remove all marked connections
110 void prune_connection_tree(void)
112 avl_node_t *node, *next;
115 for(node = connection_tree->head; node; node = next)
118 cl = (connection_t *)node->data;
119 if(cl->status.remove)
126 free all elements of connection
128 void destroy_connection_tree(void)
131 avl_delete_tree(id_tree);
132 avl_delete_tree(connection_tree);
136 /* Linked list management */
138 void connection_add(connection_t *cl)
141 avl_insert(connection_tree, cl);
145 void id_add(connection_t *cl)
148 avl_insert(id_tree, cl);
152 void connection_del(connection_t *cl)
155 avl_delete(id_tree, cl);
156 avl_delete(connection_tree, cl);
160 /* Lookup functions */
162 connection_t *lookup_connection(ipv4_t address, short unsigned int port)
166 cl.address = address;
169 return avl_search(connection_tree, &cl);
172 connection_t *lookup_id(char *name)
177 p = avl_search(id_tree, &cl);
178 if(p && p->status.active)
186 void dump_connection_list(void)
191 syslog(LOG_DEBUG, _("Connection list:"));
193 syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
194 myself->name, myself->hostname, myself->port, myself->flags,
195 myself->socket, myself->meta_socket, myself->status);
197 for(node = connection_tree->head; node; node = node->next)
199 cl = (connection_t *)node->data;
200 syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
201 cl->name, cl->hostname, cl->port, cl->flags,
202 cl->socket, cl->meta_socket, cl->status);
205 syslog(LOG_DEBUG, _("End of connection list."));
209 int read_host_config(connection_t *cl)
214 asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
215 x = read_config_file(&cl->config, fname);