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"
42 volatile bool running = false;
46 /* Purge edges and subnets of unreachable nodes. Use carefully. */
48 static void purge(void)
50 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
57 ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
59 /* Remove all edges and subnets owned by unreachable nodes. */
61 for(nnode = node_tree->head; nnode; nnode = nnext) {
65 if(!n->status.reachable) {
66 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
69 for(snode = n->subnet_tree->head; snode; snode = snext) {
73 send_del_subnet(broadcast, s);
77 for(enode = n->edge_tree->head; enode; enode = enext) {
81 send_del_edge(broadcast, e);
87 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
89 for(nnode = node_tree->head; nnode; nnode = nnext) {
93 if(!n->status.reachable) {
94 for(enode = edge_weight_tree->head; enode; enode = enext) {
109 put all file descriptors into events
110 While we're at it, purge stuf that needs to be removed.
112 static int build_fdset(void)
114 avl_node_t *node, *next;
120 for(node = connection_tree->head; node; node = next) {
124 if(c->status.remove) {
126 if(!connection_tree->head)
135 Terminate a connection:
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
141 void terminate_connection(connection_t *c, bool report)
148 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
149 c->name, c->hostname);
151 c->status.remove = true;
152 c->status.active = false;
155 c->node->connection = NULL;
158 closesocket(c->socket);
163 if(report && !tunnelserver)
164 send_del_edge(broadcast, c->edge);
168 /* Run MST and SSSP algorithms */
172 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
174 if(report && !c->node->status.reachable) {
176 e = lookup_edge(c->node, myself);
179 send_del_edge(broadcast, e);
185 /* Check if this was our outgoing connection */
188 retry_outgoing(c->outgoing);
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.
207 static void check_dead_connections(void)
209 avl_node_t *node, *next;
214 for(node = connection_tree->head; node; node = next) {
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) {
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);
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);
242 terminate_connection(c, false);
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);
259 void handle_meta_connection_data(int fd, short events, void *data)
261 connection_t *c = data;
263 socklen_t len = sizeof(result);
265 if (c->status.remove)
268 if(c->status.connecting) {
269 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
272 finish_connecting(c);
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);
284 if (!receive_meta(c)) {
285 terminate_connection(c, c->status.active);
290 static void dummy(int a, short b, void *c)
294 static void sigterm_handler(int signal, short events, void *data) {
295 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
297 event_loopexit(NULL);
300 static void sigint_handler(int signal, short events, void *data) {
301 static int saved_debug_level = -1;
303 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
305 if(saved_debug_level != -1) {
306 logger(LOG_NOTICE, _("Reverting to old debug level (%d)"),
308 debug_level = saved_debug_level;
309 saved_debug_level = -1;
312 _("Temporarily setting debug level to 5. Kill me with SIGINT again to go back to level %d."),
314 saved_debug_level = debug_level;
319 static void sigusr1_handler(int signal, short events, void *data) {
320 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
324 static void sigusr2_handler(int signal, short events, void *data) {
325 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
332 static void sigwinch_handler(int signal, short events, void *data) {
333 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
337 static void sighup_handler(int signal, short events, void *data) {
342 static time_t last_config_check = 0;
344 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
346 /* Reread our own configuration file */
348 exit_configuration(&config_tree);
349 init_configuration(&config_tree);
351 if(!read_server_config()) {
352 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
353 event_loopexit(NULL);
357 /* Close connections to hosts that have a changed or deleted host config file */
359 for(node = connection_tree->head; node; node = node->next) {
363 free(c->outgoing->name);
365 freeaddrinfo(c->outgoing->ai);
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);
376 last_config_check = time(NULL);
378 /* Try to make outgoing connections */
380 try_outgoing_connections();
383 static void sigalrm_handler(int signal, short events, void *data) {
384 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
389 for(node = connection_tree->head; node; node = node->next) {
392 if(c->outgoing && !c->node) {
393 if(timeout_initialized(&c->outgoing->ev))
394 event_del(&c->outgoing->ev);
395 if(c->status.connecting)
397 c->outgoing->timeout = 0;
398 do_outgoing_connection(c);
403 static void keyexpire_handler(int fd, short events, void *event) {
404 ifdebug(STATUS) logger(LOG_INFO, _("Regenerating symmetric key"));
406 RAND_pseudo_bytes((unsigned char *)myself->key, myself->keylength);
408 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, (unsigned char *)myself->key, (unsigned char *)myself->key + myself->cipher->key_len);
409 send_key_changed(broadcast, myself);
411 event_add(event, &(struct timeval){keylifetime, 0});
415 this is where it all happens...
421 time_t last_ping_check;
422 struct event timeout;
423 struct event sighup_event;
424 struct event sigint_event;
425 struct event sigterm_event;
426 struct event sigquit_event;
427 struct event sigusr1_event;
428 struct event sigusr2_event;
429 struct event sigwinch_event;
430 struct event sigalrm_event;
431 struct event keyexpire_event;
435 signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
436 signal_add(&sighup_event, NULL);
437 signal_set(&sigint_event, SIGINT, sigint_handler, NULL);
438 signal_add(&sigint_event, NULL);
439 signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
440 signal_add(&sigterm_event, NULL);
441 signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
442 signal_add(&sigquit_event, NULL);
443 signal_set(&sigusr1_event, SIGUSR1, sigusr1_handler, NULL);
444 signal_add(&sigusr1_event, NULL);
445 signal_set(&sigusr2_event, SIGUSR2, sigusr2_handler, NULL);
446 signal_add(&sigusr2_event, NULL);
447 signal_set(&sigwinch_event, SIGWINCH, sigwinch_handler, NULL);
448 signal_add(&sigwinch_event, NULL);
449 signal_set(&sigalrm_event, SIGALRM, sigalrm_handler, NULL);
450 signal_add(&sigalrm_event, NULL);
451 timeout_set(&keyexpire_event, keyexpire_handler, &keyexpire_event);
452 event_add(&keyexpire_event, &(struct timeval){keylifetime, 0});
454 last_ping_check = now;
463 // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
467 /* XXX: libevent transition: old timeout code in this loop */
468 timeout_set(&timeout, dummy, NULL);
469 timeout_add(&timeout, &tv);
473 logger(LOG_ERR, _("Error building fdset: %s"), strerror(errno));
479 r = event_loop(EVLOOP_ONCE);
482 logger(LOG_ERR, _("Error while waiting for input: %s"),
489 /* XXX: more libevent transition */
490 timeout_del(&timeout);
492 /* Let's check if everybody is still alive */
494 if(last_ping_check + pingtimeout < now) {
495 check_dead_connections();
496 last_ping_check = now;
500 signal_del(&sighup_event);
501 signal_del(&sigint_event);
502 signal_del(&sigterm_event);
503 signal_del(&sigquit_event);
504 signal_del(&sigusr1_event);
505 signal_del(&sigusr2_event);
506 signal_del(&sigwinch_event);
507 signal_del(&sigalrm_event);
508 event_del(&keyexpire_event);