2 net.c -- most of the network code
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2006 Guus Sliepen <guus@tinc-vpn.org>
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.
25 #include <openssl/rand.h>
30 #include "connection.h"
43 volatile bool running = false;
47 /* Purge edges and subnets of unreachable nodes. Use carefully. */
49 static void purge(void)
51 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
58 ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
60 /* Remove all edges and subnets owned by unreachable nodes. */
62 for(nnode = node_tree->head; nnode; nnode = nnext) {
66 if(!n->status.reachable) {
67 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
70 for(snode = n->subnet_tree->head; snode; snode = snext) {
74 send_del_subnet(broadcast, s);
78 for(enode = n->edge_tree->head; enode; enode = enext) {
82 send_del_edge(broadcast, e);
88 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
90 for(nnode = node_tree->head; nnode; nnode = nnext) {
94 if(!n->status.reachable) {
95 for(enode = edge_weight_tree->head; enode; enode = enext) {
110 put all file descriptors into events
111 While we're at it, purge stuf that needs to be removed.
113 static int build_fdset(void)
115 avl_node_t *node, *next;
121 for(node = connection_tree->head; node; node = next) {
125 if(c->status.remove) {
127 if(!connection_tree->head)
136 Terminate a connection:
138 - Remove associated edge and tell other connections about it if report = true
139 - Check if we need to retry making an outgoing connection
140 - Deactivate the host
142 void terminate_connection(connection_t *c, bool report)
149 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
150 c->name, c->hostname);
152 c->status.remove = true;
153 c->status.active = false;
156 c->node->connection = NULL;
159 closesocket(c->socket);
164 if(report && !tunnelserver)
165 send_del_edge(broadcast, c->edge);
169 /* Run MST and SSSP algorithms */
173 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
175 if(report && !c->node->status.reachable) {
177 e = lookup_edge(c->node, myself);
180 send_del_edge(broadcast, e);
186 /* Check if this was our outgoing connection */
189 retry_outgoing(c->outgoing);
201 Check if the other end is active.
202 If we have sent packets, but didn't receive any,
203 then possibly the other end is dead. We send a
204 PING request over the meta connection. If the other
205 end does not reply in time, we consider them dead
206 and close the connection.
208 static void check_dead_connections(void)
210 avl_node_t *node, *next;
215 for(node = connection_tree->head; node; node = next) {
219 if(c->last_ping_time + pingtimeout < now) {
220 if(c->status.active) {
221 if(c->status.pinged) {
222 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
223 c->name, c->hostname, now - c->last_ping_time);
224 c->status.timeout = true;
225 terminate_connection(c, true);
226 } else if(c->last_ping_time + pinginterval < now) {
230 if(c->status.remove) {
231 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
232 c->name, c->hostname, c->status.value);
236 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
237 c->name, c->hostname);
238 if(c->status.connecting) {
239 c->status.connecting = false;
240 closesocket(c->socket);
241 do_outgoing_connection(c);
243 terminate_connection(c, false);
248 if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) {
249 if(c->status.active) {
250 ifdebug(CONNECTIONS) logger(LOG_INFO,
251 _("%s (%s) could not flush for %ld seconds (%d bytes remaining)"),
252 c->name, c->hostname, now - c->last_flushed_time, c->outbuflen);
253 c->status.timeout = true;
254 terminate_connection(c, true);
260 void handle_meta_connection_data(int fd, short events, void *data)
262 connection_t *c = data;
264 socklen_t len = sizeof(result);
266 if (c->status.remove)
269 if(c->status.connecting) {
270 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
273 finish_connecting(c);
275 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
276 _("Error while connecting to %s (%s): %s"),
277 c->name, c->hostname, strerror(result));
278 c->status.connecting = false;
279 closesocket(c->socket);
280 do_outgoing_connection(c);
285 if (!receive_meta(c)) {
286 terminate_connection(c, c->status.active);
291 static void dummy(int a, short b, void *c)
295 static void sigterm_handler(int signal, short events, void *data) {
296 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
298 event_loopexit(NULL);
301 static void sigint_handler(int signal, short events, void *data) {
302 static int saved_debug_level = -1;
304 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
306 if(saved_debug_level != -1) {
307 logger(LOG_NOTICE, _("Reverting to old debug level (%d)"),
309 debug_level = saved_debug_level;
310 saved_debug_level = -1;
313 _("Temporarily setting debug level to 5. Kill me with SIGINT again to go back to level %d."),
315 saved_debug_level = debug_level;
320 static void sigusr1_handler(int signal, short events, void *data) {
321 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
325 static void sigusr2_handler(int signal, short events, void *data) {
326 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
333 static void sigwinch_handler(int signal, short events, void *data) {
334 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
338 static void sighup_handler(int signal, short events, void *data) {
343 static time_t last_config_check = 0;
345 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
347 /* Reread our own configuration file */
349 exit_configuration(&config_tree);
350 init_configuration(&config_tree);
352 if(!read_server_config()) {
353 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
354 event_loopexit(NULL);
358 /* Close connections to hosts that have a changed or deleted host config file */
360 for(node = connection_tree->head; node; node = node->next) {
364 free(c->outgoing->name);
366 freeaddrinfo(c->outgoing->ai);
371 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
372 if(stat(fname, &s) || s.st_mtime > last_config_check)
373 terminate_connection(c, c->status.active);
377 last_config_check = time(NULL);
379 /* Try to make outgoing connections */
381 try_outgoing_connections();
384 static void sigalrm_handler(int signal, short events, void *data) {
385 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
390 for(node = connection_tree->head; node; node = node->next) {
393 if(c->outgoing && !c->node) {
394 if(timeout_initialized(&c->outgoing->ev))
395 event_del(&c->outgoing->ev);
396 if(c->status.connecting)
398 c->outgoing->timeout = 0;
399 do_outgoing_connection(c);
405 this is where it all happens...
411 time_t last_ping_check;
412 struct event timeout;
413 struct event sighup_event;
414 struct event sigint_event;
415 struct event sigterm_event;
416 struct event sigquit_event;
417 struct event sigusr1_event;
418 struct event sigusr2_event;
419 struct event sigwinch_event;
420 struct event sigalrm_event;
424 signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
425 signal_add(&sighup_event, NULL);
426 signal_set(&sigint_event, SIGINT, sigint_handler, NULL);
427 signal_add(&sigint_event, NULL);
428 signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
429 signal_add(&sigterm_event, NULL);
430 signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
431 signal_add(&sigquit_event, NULL);
432 signal_set(&sigusr1_event, SIGUSR1, sigusr1_handler, NULL);
433 signal_add(&sigusr1_event, NULL);
434 signal_set(&sigusr2_event, SIGUSR2, sigusr2_handler, NULL);
435 signal_add(&sigusr2_event, NULL);
436 signal_set(&sigwinch_event, SIGWINCH, sigwinch_handler, NULL);
437 signal_add(&sigwinch_event, NULL);
438 signal_set(&sigalrm_event, SIGALRM, sigalrm_handler, NULL);
439 signal_add(&sigalrm_event, NULL);
441 last_ping_check = now;
450 // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
454 /* XXX: libevent transition: old timeout code in this loop */
455 timeout_set(&timeout, dummy, NULL);
456 timeout_add(&timeout, &tv);
460 logger(LOG_ERR, _("Error building fdset: %s"), strerror(errno));
466 r = event_loop(EVLOOP_ONCE);
469 logger(LOG_ERR, _("Error while waiting for input: %s"),
476 /* XXX: more libevent transition */
477 timeout_del(&timeout);
479 /* Let's check if everybody is still alive */
481 if(last_ping_check + pingtimeout < now) {
482 check_dead_connections();
483 last_ping_check = now;
485 if(routing_mode == RMODE_SWITCH)
488 /* Should we regenerate our key? */
490 if(keyexpires < now) {
491 ifdebug(STATUS) logger(LOG_INFO, _("Regenerating symmetric key"));
493 RAND_pseudo_bytes((unsigned char *)myself->key, myself->keylength);
495 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, (unsigned char *)myself->key, (unsigned char *)myself->key + myself->cipher->key_len);
496 send_key_changed(broadcast, myself);
497 keyexpires = now + keylifetime;
502 signal_del(&sighup_event);
503 signal_del(&sigint_event);
504 signal_del(&sigterm_event);
505 signal_del(&sigquit_event);
506 signal_del(&sigusr1_event);
507 signal_del(&sigusr2_event);
508 signal_del(&sigwinch_event);
509 signal_del(&sigalrm_event);