Show next- and lastbutonehop when dumping connectionlist to syslog.
[oweals/tinc.git] / 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.16 2001/09/24 14:16:29 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   }
176 cp
177 }
178
179 void id_add(connection_t *cl)
180 {
181 cp
182   avl_insert(id_tree, cl);
183 cp
184 }
185
186 void prune_add(connection_t *cl)
187 {
188 cp
189   avl_insert(prune_tree, cl);
190 cp
191 }
192
193 void prune_flush(void)
194 {
195   avl_node_t *node, *next;
196 cp
197   for(node = prune_tree->head; node; node = next)
198     {
199       next = node->next;
200       avl_delete_node(prune_tree, node);
201     }
202 cp
203 }
204
205 /* Lookup functions */
206
207 connection_t *lookup_active(ipv4_t address, short unsigned int port)
208 {
209   connection_t cl;
210 cp
211   cl.address = address;
212   cl.port = port;
213
214   return avl_search(active_tree, &cl);
215 }
216
217 connection_t *lookup_id(char *name)
218 {
219   connection_t cl, *p;
220 cp
221   cl.name = name;
222   p = avl_search(id_tree, &cl);
223   if(p)
224     return p;
225   else
226     return NULL;
227 }
228
229 /* Debugging */
230
231 void dump_connection_list(void)
232 {
233   avl_node_t *node;
234   connection_t *cl;
235 cp
236   syslog(LOG_DEBUG, _("Connection list:"));
237
238   for(node = connection_tree->head; node; node = node->next)
239     {
240       cl = (connection_t *)node->data;
241       syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"),
242              cl->name, cl->hostname, cl->port, cl->options,
243              cl->socket, cl->meta_socket, cl->status);
244     }
245     
246   syslog(LOG_DEBUG, _("Known hosts:"));
247
248   for(node = id_tree->head; node; node = node->next)
249     {
250       cl = (connection_t *)node->data;
251       syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x nexthop %s lastbutonehop %s"),
252              cl->name, cl->hostname, cl->port, cl->options,
253              cl->socket, cl->meta_socket, cl->status, cl->nexthop->name, cl->lastbutonehop->name);
254     }
255     
256   syslog(LOG_DEBUG, _("End of connection list."));
257 cp
258 }
259
260 int read_host_config(connection_t *cl)
261 {
262   char *fname;
263   int x;
264 cp
265   asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
266   x = read_config_file(&cl->config, fname);
267   free(fname);
268 cp
269   return x;
270 }