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.3 2000/11/20 22:13:03 guus Exp $
30 #include "net.h" /* Don't ask. */
40 /* Root of the connection list */
42 rbltree_t *connection_tree;
43 connection_t *myself = NULL;
45 /* Initialization and callbacks */
47 int connection_compare(connection_t *a, connection_t *b)
49 return strcmp(a->name, b->name);
52 void init_connections(void)
54 connection_tree = new_rbltree((rbl_compare_t)connection_compare, (rbl_action_t)free_connection);
57 /* Creation and deletion of connection elements */
59 connection_t *new_connection(void)
61 connection_t *p = (connection_t *)xmalloc(sizeof(*p));
63 /* initialise all those stupid pointers at once */
64 memset(p, '\0', sizeof(*p));
66 p->subnet_tree = new_rbltree((rbl_compare_t)subnet_compare, NULL);
71 void free_connection(connection_t *p)
78 if(p->name && p->name!=unknown)
85 free(p->cipher_pktkey);
89 clear_config(&p->config);
95 remove all marked connections
97 void prune_connection_tree(void)
102 RBL_FOREACH(connection_tree, rbl)
104 cl = (connection_t *) rbl->data;
105 if(cl->status.remove)
112 free all elements of connection
114 void destroy_connection_tree(void)
117 rbl_delete_rbltree(connection_tree);
121 /* Linked list management */
123 void connection_add(connection_t *cl)
126 rbl_insert(connection_tree, cl);
130 void connection_del(connection_t *cl)
133 rbl_delete(connection_tree, cl);
137 /* Lookup functions */
139 connection_t *lookup_id(char *name)
144 p = rbl_search(connection_tree, &cl);
145 if(p && p->status.active)
154 void dump_connection_list(void)
159 syslog(LOG_DEBUG, _("Connection list:"));
161 syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
162 myself->name, myself->hostname, myself->port, myself->flags,
163 myself->socket, myself->meta_socket, myself->status);
165 RBL_FOREACH(connection_tree, rbl)
167 cl = (connection_t *)rbl->data;
168 syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
169 cl->name, cl->hostname, cl->port, cl->flags,
170 cl->socket, cl->meta_socket, cl->status);
173 syslog(LOG_DEBUG, _("End of connection list."));
177 int read_host_config(connection_t *cl)
182 asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
183 x = read_config_file(&cl->config, fname);