fa2f81d792ad2f8ecf797bb6861c1b31fa3f1443
[tinc] / src / connection.c
1 /*
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>
5
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.
10
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.
15
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.
19
20     $Id: connection.c,v 1.1.2.14 2001/07/20 20:25:10 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <syslog.h>
27 #include <string.h>
28
29 #include <avl_tree.h>
30 #include <list.h>
31
32 #include "net.h"        /* Don't ask. */
33 #include "netutl.h"
34 #include "config.h"
35 #include "conf.h"
36 #include <utils.h>
37 #include "subnet.h"
38
39 #include "xalloc.h"
40 #include "system.h"
41
42 /* Root of the connection list */
43
44 avl_tree_t *connection_tree;    /* Meta connections */
45 avl_tree_t *active_tree;        /* Activated hosts, sorted by address and port */
46 avl_tree_t *id_tree;            /* Activated hosts, sorted by name */
47 avl_tree_t *prune_tree;         /* connection_t structures which have to be freed */
48
49 /* Pointer to connection describing myself */
50
51 connection_t *myself = NULL;
52
53 /* Initialization and callbacks */
54
55 int connection_compare(connection_t *a, connection_t *b)
56 {
57   return a->meta_socket - b->meta_socket;
58 }
59
60 int active_compare(connection_t *a, connection_t *b)
61 {
62   ipv4_t result;
63
64   result = a->address - b->address;
65   if(result)
66     return result;
67   else
68     return a->port - b->port;
69 }
70
71 int id_compare(connection_t *a, connection_t *b)
72 {
73   return strcmp(a->name, b->name);
74 }
75
76 int prune_compare(connection_t *a, connection_t *b)
77 {
78   if(a < b)
79     return -1;
80   else if(a > b)
81     return 1;
82   else
83     return 0;
84 }
85
86 void init_connections(void)
87 {
88   connection_tree = avl_alloc_tree((avl_compare_t)connection_compare, NULL);
89   active_tree = avl_alloc_tree((avl_compare_t)active_compare, NULL);
90   id_tree = avl_alloc_tree((avl_compare_t)id_compare, NULL);
91   prune_tree = avl_alloc_tree((avl_compare_t)prune_compare, (avl_action_t)free_connection);
92 }
93
94 /* Creation and deletion of connection elements */
95
96 connection_t *new_connection(void)
97 {
98   connection_t *p = (connection_t *)xmalloc_and_zero(sizeof(*p));
99 cp
100   p->subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, NULL);
101   p->queue = list_alloc((list_action_t)free);
102 cp
103   return p;
104 }
105
106 void free_connection(connection_t *p)
107 {
108 cp
109   if(p->queue)
110     list_delete_list(p->queue);
111   if(p->name)
112     free(p->name);
113   if(p->hostname)
114     free(p->hostname);
115   if(p->rsa_key)
116     RSA_free(p->rsa_key);
117   if(p->cipher_pktkey)
118     free(p->cipher_pktkey);
119   if(p->buffer)
120     free(p->buffer);
121   if(p->config)
122     clear_config(&p->config);
123   free(p);
124 cp
125 }
126
127 /*
128   Free all trees.
129 */
130 void destroy_trees(void)
131 {
132 cp
133   avl_delete_tree(id_tree);
134   avl_delete_tree(active_tree);
135   avl_delete_tree(connection_tree);
136   avl_delete_tree(prune_tree);
137 cp
138 }
139
140 /* Connection management */
141
142 void connection_add(connection_t *cl)
143 {
144 cp
145   avl_insert(connection_tree, cl);
146 cp
147 }
148
149 void connection_del(connection_t *cl)
150 {
151 cp
152   active_del(cl);
153
154   if(cl->status.meta)
155     avl_delete(connection_tree, cl);
156 cp
157 }
158
159 void active_add(connection_t *cl)
160 {
161 cp
162   avl_insert(active_tree, cl);
163   avl_insert(id_tree, cl);
164   cl->status.active = 1;
165 cp
166 }
167
168 void active_del(connection_t *cl)
169 {
170 cp
171   if(cl->status.active)
172   {
173     avl_delete(id_tree, cl);
174     avl_delete(active_tree, cl);
175     cl->status.active = 0;
176   }
177 cp
178 }
179
180 void id_add(connection_t *cl)
181 {
182 cp
183   avl_insert(id_tree, cl);
184 cp
185 }
186
187 void prune_add(connection_t *cl)
188 {
189 cp
190   avl_insert(prune_tree, cl);
191 cp
192 }
193
194 void prune_flush(void)
195 {
196   avl_node_t *node, *next;
197 cp
198   for(node = prune_tree->head; node; node = next)
199     {
200       next = node->next;
201       avl_delete_node(prune_tree, node);
202     }
203 cp
204 }
205
206 /* Lookup functions */
207
208 connection_t *lookup_active(ipv4_t address, short unsigned int port)
209 {
210   connection_t cl;
211 cp
212   cl.address = address;
213   cl.port = port;
214
215   return avl_search(active_tree, &cl);
216 }
217
218 connection_t *lookup_id(char *name)
219 {
220   connection_t cl, *p;
221 cp
222   cl.name = name;
223   p = avl_search(id_tree, &cl);
224   if(p)
225     return p;
226   else
227     return NULL;
228 }
229
230 /* Debugging */
231
232 void dump_connection_list(void)
233 {
234   avl_node_t *node;
235   connection_t *cl;
236 cp
237   syslog(LOG_DEBUG, _("Connection list:"));
238
239   for(node = connection_tree->head; node; node = node->next)
240     {
241       cl = (connection_t *)node->data;
242       syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"),
243              cl->name, cl->hostname, cl->port, cl->options,
244              cl->socket, cl->meta_socket, cl->status);
245     }
246     
247   syslog(LOG_DEBUG, _("Known hosts:"));
248
249   for(node = id_tree->head; node; node = node->next)
250     {
251       cl = (connection_t *)node->data;
252       syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"),
253              cl->name, cl->hostname, cl->port, cl->options,
254              cl->socket, cl->meta_socket, cl->status);
255     }
256     
257   syslog(LOG_DEBUG, _("End of connection list."));
258 cp
259 }
260
261 int read_host_config(connection_t *cl)
262 {
263   char *fname;
264   int x;
265 cp
266   asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
267   x = read_config_file(&cl->config, fname);
268   free(fname);
269 cp
270   return x;
271 }