df5fae77ccb2009cd450ccea483df6bd6bf5514e
[tinc] / src / net.c
1 /*
2     net.c -- most of the network code
3     Copyright (C) 1998-2005 Ivo Timmermans,
4                   2000-2006 Guus Sliepen <guus@tinc-vpn.org>
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$
21 */
22
23 #include "system.h"
24
25 #include <openssl/rand.h>
26
27 #include "utils.h"
28 #include "avl_tree.h"
29 #include "conf.h"
30 #include "connection.h"
31 #include "device.h"
32 #include "graph.h"
33 #include "logger.h"
34 #include "meta.h"
35 #include "net.h"
36 #include "netutl.h"
37 #include "process.h"
38 #include "protocol.h"
39 #include "subnet.h"
40 #include "xalloc.h"
41
42 volatile bool running = false;
43
44 time_t now = 0;
45
46 /* Purge edges and subnets of unreachable nodes. Use carefully. */
47
48 static void purge(void)
49 {
50         avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
51         node_t *n;
52         edge_t *e;
53         subnet_t *s;
54
55         cp();
56
57         ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
58
59         /* Remove all edges and subnets owned by unreachable nodes. */
60
61         for(nnode = node_tree->head; nnode; nnode = nnext) {
62                 nnext = nnode->next;
63                 n = nnode->data;
64
65                 if(!n->status.reachable) {
66                         ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
67                                            n->hostname);
68
69                         for(snode = n->subnet_tree->head; snode; snode = snext) {
70                                 snext = snode->next;
71                                 s = snode->data;
72                                 if(!tunnelserver)
73                                         send_del_subnet(broadcast, s);
74                                 subnet_del(n, s);
75                         }
76
77                         for(enode = n->edge_tree->head; enode; enode = enext) {
78                                 enext = enode->next;
79                                 e = enode->data;
80                                 if(!tunnelserver)
81                                         send_del_edge(broadcast, e);
82                                 edge_del(e);
83                         }
84                 }
85         }
86
87         /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
88
89         for(nnode = node_tree->head; nnode; nnode = nnext) {
90                 nnext = nnode->next;
91                 n = nnode->data;
92
93                 if(!n->status.reachable) {
94                         for(enode = edge_weight_tree->head; enode; enode = enext) {
95                                 enext = enode->next;
96                                 e = enode->data;
97
98                                 if(e->to == n)
99                                         break;
100                         }
101
102                         if(!enode)
103                                 node_del(n);
104                 }
105         }
106 }
107
108 /*
109   put all file descriptors into events
110   While we're at it, purge stuf that needs to be removed.
111 */
112 static int build_fdset(void)
113 {
114         avl_node_t *node, *next;
115         connection_t *c;
116         int i, max = 0;
117
118         cp();
119
120         for(node = connection_tree->head; node; node = next) {
121                 next = node->next;
122                 c = node->data;
123
124                 if(c->status.remove) {
125                         connection_del(c);
126                         if(!connection_tree->head)
127                                 purge();
128                 }
129         }
130
131         return 0;
132 }
133
134 /*
135   Terminate a connection:
136   - Close the socket
137   - Remove associated edge and tell other connections about it if report = true
138   - Check if we need to retry making an outgoing connection
139   - Deactivate the host
140 */
141 void terminate_connection(connection_t *c, bool report)
142 {
143         cp();
144
145         if(c->status.remove)
146                 return;
147
148         ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
149                            c->name, c->hostname);
150
151         c->status.remove = true;
152         c->status.active = false;
153
154         if(c->node)
155                 c->node->connection = NULL;
156
157         if(c->socket)
158                 closesocket(c->socket);
159
160         event_del(&c->ev);
161
162         if(c->edge) {
163                 if(report && !tunnelserver)
164                         send_del_edge(broadcast, c->edge);
165
166                 edge_del(c->edge);
167
168                 /* Run MST and SSSP algorithms */
169
170                 graph();
171
172                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
173
174                 if(report && !c->node->status.reachable) {
175                         edge_t *e;
176                         e = lookup_edge(c->node, myself);
177                         if(e) {
178                                 if(!tunnelserver)
179                                         send_del_edge(broadcast, e);
180                                 edge_del(e);
181                         }
182                 }
183         }
184
185         /* Check if this was our outgoing connection */
186
187         if(c->outgoing) {
188                 retry_outgoing(c->outgoing);
189                 c->outgoing = NULL;
190         }
191
192         free(c->outbuf);
193         c->outbuf = NULL;
194         c->outbuflen = 0;
195         c->outbufsize = 0;
196         c->outbufstart = 0;
197 }
198
199 /*
200   Check if the other end is active.
201   If we have sent packets, but didn't receive any,
202   then possibly the other end is dead. We send a
203   PING request over the meta connection. If the other
204   end does not reply in time, we consider them dead
205   and close the connection.
206 */
207 static void check_dead_connections(void)
208 {
209         avl_node_t *node, *next;
210         connection_t *c;
211
212         cp();
213
214         for(node = connection_tree->head; node; node = next) {
215                 next = node->next;
216                 c = node->data;
217
218                 if(c->last_ping_time + pingtimeout < now) {
219                         if(c->status.active) {
220                                 if(c->status.pinged) {
221                                         ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
222                                                            c->name, c->hostname, now - c->last_ping_time);
223                                         c->status.timeout = true;
224                                         terminate_connection(c, true);
225                                 } else if(c->last_ping_time + pinginterval < now) {
226                                         send_ping(c);
227                                 }
228                         } else {
229                                 if(c->status.remove) {
230                                         logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
231                                                    c->name, c->hostname, c->status.value);
232                                         connection_del(c);
233                                         continue;
234                                 }
235                                 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
236                                                    c->name, c->hostname);
237                                 if(c->status.connecting) {
238                                         c->status.connecting = false;
239                                         closesocket(c->socket);
240                                         do_outgoing_connection(c);
241                                 } else {
242                                         terminate_connection(c, false);
243                                 }
244                         }
245                 }
246
247                 if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) {
248                         if(c->status.active) {
249                                 ifdebug(CONNECTIONS) logger(LOG_INFO,
250                                                 _("%s (%s) could not flush for %ld seconds (%d bytes remaining)"),
251                                                 c->name, c->hostname, now - c->last_flushed_time, c->outbuflen);
252                                 c->status.timeout = true;
253                                 terminate_connection(c, true);
254                         }
255                 }
256         }
257 }
258
259 void handle_meta_connection_data(int fd, short events, void *data)
260 {
261         connection_t *c = data;
262         int result;
263         socklen_t len = sizeof(result);
264
265         if (c->status.remove)
266                 return;
267
268         if(c->status.connecting) {
269                 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
270
271                 if(!result)
272                         finish_connecting(c);
273                 else {
274                         ifdebug(CONNECTIONS) logger(LOG_DEBUG,
275                                            _("Error while connecting to %s (%s): %s"),
276                                            c->name, c->hostname, strerror(result));
277                         c->status.connecting = false;
278                         closesocket(c->socket);
279                         do_outgoing_connection(c);
280                         return;
281                 }
282         }
283
284         if (!receive_meta(c)) {
285                 terminate_connection(c, c->status.active);
286                 return;
287         }
288 }
289
290 static void dummy(int a, short b, void *c)
291 {
292 }
293
294 static void sigterm_handler(int signal, short events, void *data) {
295         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
296         running = false;
297         event_loopexit(NULL);
298 }
299
300 static void sigint_handler(int signal, short events, void *data) {
301         static int saved_debug_level = -1;
302
303         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
304
305         if(saved_debug_level != -1) {
306                 logger(LOG_NOTICE, _("Reverting to old debug level (%d)"),
307                         saved_debug_level);
308                 debug_level = saved_debug_level;
309                 saved_debug_level = -1;
310         } else {
311                 logger(LOG_NOTICE,
312                         _("Temporarily setting debug level to 5.  Kill me with SIGINT again to go back to level %d."),
313                         debug_level);
314                 saved_debug_level = debug_level;
315                 debug_level = 5;
316         }
317 }
318
319 static void sigusr1_handler(int signal, short events, void *data) {
320         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
321         dump_connections();
322 }
323
324 static void sigusr2_handler(int signal, short events, void *data) {
325         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
326         dump_device_stats();
327         dump_nodes();
328         dump_edges();
329         dump_subnets();
330 }
331
332 static void sigwinch_handler(int signal, short events, void *data) {
333         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
334         purge();
335 }
336
337 static void sighup_handler(int signal, short events, void *data) {
338         connection_t *c;
339         avl_node_t *node;
340         char *fname;
341         struct stat s;
342         static time_t last_config_check = 0;
343         
344         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
345
346         /* Reread our own configuration file */
347
348         exit_configuration(&config_tree);
349         init_configuration(&config_tree);
350
351         if(!read_server_config()) {
352                 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
353                 event_loopexit(NULL);
354                 return;
355         }
356
357         /* Close connections to hosts that have a changed or deleted host config file */
358         
359         for(node = connection_tree->head; node; node = node->next) {
360                 c = node->data;
361                 
362                 if(c->outgoing) {
363                         free(c->outgoing->name);
364                         if(c->outgoing->ai)
365                                 freeaddrinfo(c->outgoing->ai);
366                         free(c->outgoing);
367                         c->outgoing = NULL;
368                 }
369                 
370                 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
371                 if(stat(fname, &s) || s.st_mtime > last_config_check)
372                         terminate_connection(c, c->status.active);
373                 free(fname);
374         }
375
376         last_config_check = time(NULL);
377
378         /* Try to make outgoing connections */
379         
380         try_outgoing_connections();
381 }
382
383 static void sigalrm_handler(int signal, short events, void *data) {
384         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
385
386         connection_t *c;
387         avl_node_t *node;
388
389         for(node = connection_tree->head; node; node = node->next) {
390                 c = node->data;
391                 
392                 if(c->outgoing && !c->node) {
393                         if(timeout_initialized(&c->outgoing->ev))
394                                 event_del(&c->outgoing->ev);
395                         if(c->status.connecting)
396                                 close(c->socket);
397                         c->outgoing->timeout = 0;
398                         do_outgoing_connection(c);
399                 }
400         }
401 }
402
403 /*
404   this is where it all happens...
405 */
406 int main_loop(void)
407 {
408         struct timeval tv;
409         int r;
410         time_t last_ping_check;
411         struct event timeout;
412         struct event sighup_event;
413         struct event sigint_event;
414         struct event sigterm_event;
415         struct event sigquit_event;
416         struct event sigusr1_event;
417         struct event sigusr2_event;
418         struct event sigwinch_event;
419         struct event sigalrm_event;
420
421         cp();
422
423         signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
424         signal_add(&sighup_event, NULL);
425         signal_set(&sigint_event, SIGINT, sigint_handler, NULL);
426         signal_add(&sigint_event, NULL);
427         signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
428         signal_add(&sigterm_event, NULL);
429         signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
430         signal_add(&sigquit_event, NULL);
431         signal_set(&sigusr1_event, SIGUSR1, sigusr1_handler, NULL);
432         signal_add(&sigusr1_event, NULL);
433         signal_set(&sigusr2_event, SIGUSR2, sigusr2_handler, NULL);
434         signal_add(&sigusr2_event, NULL);
435         signal_set(&sigwinch_event, SIGWINCH, sigwinch_handler, NULL);
436         signal_add(&sigwinch_event, NULL);
437         signal_set(&sigalrm_event, SIGALRM, sigalrm_handler, NULL);
438         signal_add(&sigalrm_event, NULL);
439
440         last_ping_check = now;
441         
442         srand(now);
443
444         running = true;
445
446         while(running) {
447                 now = time(NULL);
448
449         //      tv.tv_sec = 1 + (rand() & 7);   /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
450                 tv.tv_sec = 1;
451                 tv.tv_usec = 0;
452
453                 /* XXX: libevent transition: old timeout code in this loop */
454                 timeout_set(&timeout, dummy, NULL);
455                 timeout_add(&timeout, &tv);
456
457                 r = build_fdset();
458                 if(r < 0) {
459                         logger(LOG_ERR, _("Error building fdset: %s"), strerror(errno));
460                         cp_trace();
461                         dump_connections();
462                         return 1;
463                 }
464
465                 r = event_loop(EVLOOP_ONCE);
466                 now = time(NULL);
467                 if(r < 0) {
468                         logger(LOG_ERR, _("Error while waiting for input: %s"),
469                                    strerror(errno));
470                         cp_trace();
471                         dump_connections();
472                         return 1;
473                 }
474
475                 /* XXX: more libevent transition */
476                 timeout_del(&timeout);
477
478                 /* Let's check if everybody is still alive */
479
480                 if(last_ping_check + pingtimeout < now) {
481                         check_dead_connections();
482                         last_ping_check = now;
483                 }
484         }
485
486         signal_del(&sighup_event);
487         signal_del(&sigint_event);
488         signal_del(&sigterm_event);
489         signal_del(&sigquit_event);
490         signal_del(&sigusr1_event);
491         signal_del(&sigusr2_event);
492         signal_del(&sigwinch_event);
493         signal_del(&sigalrm_event);
494
495         return 0;
496 }