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.4 2000/11/22 18:54:07 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(sizeof(*p));
76 /* initialise all those stupid pointers at once */
77 memset(p, '\0', sizeof(*p));
79 p->subnet_tree = new_rbltree((rbl_compare_t)subnet_compare, NULL);
84 void free_connection(connection_t *p)
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)
115 RBL_FOREACH(connection_tree, rbl)
117 cl = (connection_t *) rbl->data;
118 if(cl->status.remove)
125 free all elements of connection
127 void destroy_connection_tree(void)
130 rbl_delete_rbltree(id_tree);
131 rbl_delete_rbltree(connection_tree);
135 /* Linked list management */
137 void connection_add(connection_t *cl)
140 rbl_insert(connection_tree, cl);
144 void id_add(connection_t *cl)
147 rbl_insert(id_tree, cl);
151 void connection_del(connection_t *cl)
154 rbl_delete(id_tree, cl);
155 rbl_delete(connection_tree, cl);
159 /* Lookup functions */
161 connection_t *lookup_connection(ipv4_t address, short unsigned int port)
165 cl.address = address;
168 return rbl_search(connection_tree, &cl);
171 connection_t *lookup_id(char *name)
176 p = rbl_search(id_tree, &cl);
177 if(p && p->status.active)
185 void dump_connection_list(void)
190 syslog(LOG_DEBUG, _("Connection list:"));
192 syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
193 myself->name, myself->hostname, myself->port, myself->flags,
194 myself->socket, myself->meta_socket, myself->status);
196 RBL_FOREACH(connection_tree, rbl)
198 cl = (connection_t *)rbl->data;
199 syslog(LOG_DEBUG, _(" %s at %s port %hd flags %d sockets %d, %d status %04x"),
200 cl->name, cl->hostname, cl->port, cl->flags,
201 cl->socket, cl->meta_socket, cl->status);
204 syslog(LOG_DEBUG, _("End of connection list."));
208 int read_host_config(connection_t *cl)
213 asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
214 x = read_config_file(&cl->config, fname);