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.6 2000/11/24 23:13:01 guus Exp $
30 #include "net.h" /* Don't ask. */
40 /* Root of the connection list */
42 rbltree_t *connection_tree;
45 connection_t *myself = NULL;
47 /* Initialization and callbacks */
49 int connection_compare(connection_t *a, connection_t *b)
52 result = a->address - b->address;
56 return a->port - b->port;
59 int id_compare(connection_t *a, connection_t *b)
61 return strcmp(a->name, b->name);
64 void init_connections(void)
66 connection_tree = new_rbltree((rbl_compare_t)connection_compare, (rbl_action_t)free_connection);
67 id_tree = new_rbltree((rbl_compare_t)id_compare, NULL);
70 /* Creation and deletion of connection elements */
72 connection_t *new_connection(void)
74 connection_t *p = (connection_t *)xmalloc_and_zero(sizeof(*p));
76 p->subnet_tree = new_rbltree((rbl_compare_t)subnet_compare, NULL);
81 void free_connection(connection_t *p)
88 if(p->name && p->name!=unknown)
95 free(p->cipher_pktkey);
99 clear_config(&p->config);
105 remove all marked connections
107 void prune_connection_tree(void)
112 RBL_FOREACH(connection_tree, rbl)
114 cl = (connection_t *) rbl->data;
115 if(cl->status.remove)
122 free all elements of connection
124 void destroy_connection_tree(void)
127 rbl_delete_rbltree(id_tree);
128 rbl_delete_rbltree(connection_tree);
132 /* Linked list management */
134 void connection_add(connection_t *cl)
137 rbl_insert(connection_tree, cl);
141 void id_add(connection_t *cl)
144 rbl_insert(id_tree, cl);
148 void connection_del(connection_t *cl)
151 rbl_delete(id_tree, cl);
152 rbl_delete(connection_tree, cl);
156 /* Lookup functions */
158 connection_t *lookup_connection(ipv4_t address, short unsigned int port)
162 cl.address = address;
165 return rbl_search(connection_tree, &cl);
168 connection_t *lookup_id(char *name)
173 p = rbl_search(id_tree, &cl);
174 if(p && p->status.active)
182 void dump_connection_list(void)
187 syslog(LOG_DEBUG, _("Connection list:"));
189 syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
190 myself->name, myself->hostname, myself->port, myself->flags,
191 myself->socket, myself->meta_socket, myself->status);
193 RBL_FOREACH(connection_tree, rbl)
195 cl = (connection_t *)rbl->data;
196 syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
197 cl->name, cl->hostname, cl->port, cl->flags,
198 cl->socket, cl->meta_socket, cl->status);
201 syslog(LOG_DEBUG, _("End of connection list."));
205 int read_host_config(connection_t *cl)
210 asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
211 x = read_config_file(&cl->config, fname);