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;
44 /* Purge edges and subnets of unreachable nodes. Use carefully. */
46 static void purge(void)
48 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
55 ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
57 /* Remove all edges and subnets owned by unreachable nodes. */
59 for(nnode = node_tree->head; nnode; nnode = nnext) {
63 if(!n->status.reachable) {
64 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
67 for(snode = n->subnet_tree->head; snode; snode = snext) {
71 send_del_subnet(broadcast, s);
75 for(enode = n->edge_tree->head; enode; enode = enext) {
79 send_del_edge(broadcast, e);
85 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
87 for(nnode = node_tree->head; nnode; nnode = nnext) {
91 if(!n->status.reachable) {
92 for(enode = edge_weight_tree->head; enode; enode = enext) {
107 put all file descriptors into events
108 While we're at it, purge stuf that needs to be removed.
110 static int build_fdset(void)
112 avl_node_t *node, *next;
118 for(node = connection_tree->head; node; node = next) {
122 if(c->status.remove) {
124 if(!connection_tree->head)
133 Terminate a connection:
135 - Remove associated edge and tell other connections about it if report = true
136 - Check if we need to retry making an outgoing connection
137 - Deactivate the host
139 void terminate_connection(connection_t *c, bool report)
146 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
147 c->name, c->hostname);
149 c->status.remove = true;
150 c->status.active = false;
153 c->node->connection = NULL;
156 closesocket(c->socket);
161 if(report && !tunnelserver)
162 send_del_edge(broadcast, c->edge);
166 /* Run MST and SSSP algorithms */
170 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
172 if(report && !c->node->status.reachable) {
174 e = lookup_edge(c->node, myself);
177 send_del_edge(broadcast, e);
183 /* Check if this was our outgoing connection */
186 retry_outgoing(c->outgoing);
198 Check if the other end is active.
199 If we have sent packets, but didn't receive any,
200 then possibly the other end is dead. We send a
201 PING request over the meta connection. If the other
202 end does not reply in time, we consider them dead
203 and close the connection.
205 static void timeout_handler(int fd, short events, void *event)
207 avl_node_t *node, *next;
209 time_t now = time(NULL);
213 for(node = connection_tree->head; node; node = next) {
217 if(c->last_ping_time + pingtimeout < now) {
218 if(c->status.active) {
219 if(c->status.pinged) {
220 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
221 c->name, c->hostname, now - c->last_ping_time);
222 c->status.timeout = true;
223 terminate_connection(c, true);
224 } else if(c->last_ping_time + pinginterval < now) {
228 if(c->status.remove) {
229 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
230 c->name, c->hostname, c->status.value);
234 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
235 c->name, c->hostname);
236 if(c->status.connecting) {
237 c->status.connecting = false;
238 closesocket(c->socket);
239 do_outgoing_connection(c);
241 terminate_connection(c, false);
247 event_add(event, &(struct timeval){pingtimeout, 0});
250 void handle_meta_connection_data(int fd, short events, void *data)
252 connection_t *c = data;
254 socklen_t len = sizeof(result);
256 if (c->status.remove)
259 if(c->status.connecting) {
260 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
263 finish_connecting(c);
265 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
266 _("Error while connecting to %s (%s): %s"),
267 c->name, c->hostname, strerror(result));
268 c->status.connecting = false;
269 closesocket(c->socket);
270 do_outgoing_connection(c);
275 if (!receive_meta(c)) {
276 terminate_connection(c, c->status.active);
281 static void dummy(int a, short b, void *c)
285 static void sigterm_handler(int signal, short events, void *data) {
286 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
288 event_loopexit(NULL);
291 static void sigint_handler(int signal, short events, void *data) {
292 static int saved_debug_level = -1;
294 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
296 if(saved_debug_level != -1) {
297 logger(LOG_NOTICE, _("Reverting to old debug level (%d)"),
299 debug_level = saved_debug_level;
300 saved_debug_level = -1;
303 _("Temporarily setting debug level to 5. Kill me with SIGINT again to go back to level %d."),
305 saved_debug_level = debug_level;
310 static void sigusr1_handler(int signal, short events, void *data) {
311 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
315 static void sigusr2_handler(int signal, short events, void *data) {
316 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
323 static void sigwinch_handler(int signal, short events, void *data) {
324 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
328 static void sighup_handler(int signal, short events, void *data) {
333 static time_t last_config_check = 0;
335 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
337 /* Reread our own configuration file */
339 exit_configuration(&config_tree);
340 init_configuration(&config_tree);
342 if(!read_server_config()) {
343 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
344 event_loopexit(NULL);
348 /* Close connections to hosts that have a changed or deleted host config file */
350 for(node = connection_tree->head; node; node = node->next) {
354 free(c->outgoing->name);
356 freeaddrinfo(c->outgoing->ai);
361 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
362 if(stat(fname, &s) || s.st_mtime > last_config_check)
363 terminate_connection(c, c->status.active);
367 last_config_check = time(NULL);
369 /* Try to make outgoing connections */
371 try_outgoing_connections();
374 static void sigalrm_handler(int signal, short events, void *data) {
375 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
380 for(node = connection_tree->head; node; node = node->next) {
383 if(c->outgoing && !c->node) {
384 if(timeout_initialized(&c->outgoing->ev))
385 event_del(&c->outgoing->ev);
386 if(c->status.connecting)
388 c->outgoing->timeout = 0;
389 do_outgoing_connection(c);
395 this is where it all happens...
401 struct event timeout_event;
402 struct event sighup_event;
403 struct event sigint_event;
404 struct event sigterm_event;
405 struct event sigquit_event;
406 struct event sigusr1_event;
407 struct event sigusr2_event;
408 struct event sigwinch_event;
409 struct event sigalrm_event;
413 timeout_set(&timeout_event, timeout_handler, &timeout_event);
414 event_add(&timeout_event, &(struct timeval){pingtimeout, 0});
415 signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
416 signal_add(&sighup_event, NULL);
417 signal_set(&sigint_event, SIGINT, sigint_handler, NULL);
418 signal_add(&sigint_event, NULL);
419 signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
420 signal_add(&sigterm_event, NULL);
421 signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
422 signal_add(&sigquit_event, NULL);
423 signal_set(&sigusr1_event, SIGUSR1, sigusr1_handler, NULL);
424 signal_add(&sigusr1_event, NULL);
425 signal_set(&sigusr2_event, SIGUSR2, sigusr2_handler, NULL);
426 signal_add(&sigusr2_event, NULL);
427 signal_set(&sigwinch_event, SIGWINCH, sigwinch_handler, NULL);
428 signal_add(&sigwinch_event, NULL);
429 signal_set(&sigalrm_event, SIGALRM, sigalrm_handler, NULL);
430 signal_add(&sigalrm_event, NULL);
432 if(event_loop(0) < 0) {
433 logger(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
437 signal_del(&sighup_event);
438 signal_del(&sigint_event);
439 signal_del(&sigterm_event);
440 signal_del(&sigquit_event);
441 signal_del(&sigusr1_event);
442 signal_del(&sigusr2_event);
443 signal_del(&sigwinch_event);
444 signal_del(&sigalrm_event);
445 event_del(&timeout_event);