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 /* Purge edges and subnets of unreachable nodes. Use carefully. */
44 static void purge(void)
46 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
53 ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
55 /* Remove all edges and subnets owned by unreachable nodes. */
57 for(nnode = node_tree->head; nnode; nnode = nnext) {
61 if(!n->status.reachable) {
62 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
65 for(snode = n->subnet_tree->head; snode; snode = snext) {
69 send_del_subnet(broadcast, s);
73 for(enode = n->edge_tree->head; enode; enode = enext) {
77 send_del_edge(broadcast, e);
83 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
85 for(nnode = node_tree->head; nnode; nnode = nnext) {
89 if(!n->status.reachable) {
90 for(enode = edge_weight_tree->head; enode; enode = enext) {
105 put all file descriptors into events
106 While we're at it, purge stuf that needs to be removed.
108 static int build_fdset(void)
110 avl_node_t *node, *next;
116 for(node = connection_tree->head; node; node = next) {
120 if(c->status.remove) {
122 if(!connection_tree->head)
131 Terminate a connection:
133 - Remove associated edge and tell other connections about it if report = true
134 - Check if we need to retry making an outgoing connection
135 - Deactivate the host
137 void terminate_connection(connection_t *c, bool report)
144 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
145 c->name, c->hostname);
147 c->status.remove = true;
148 c->status.active = false;
151 c->node->connection = NULL;
154 closesocket(c->socket);
159 if(report && !tunnelserver)
160 send_del_edge(broadcast, c->edge);
164 /* Run MST and SSSP algorithms */
168 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
170 if(report && !c->node->status.reachable) {
172 e = lookup_edge(c->node, myself);
175 send_del_edge(broadcast, e);
181 /* Check if this was our outgoing connection */
184 retry_outgoing(c->outgoing);
196 Check if the other end is active.
197 If we have sent packets, but didn't receive any,
198 then possibly the other end is dead. We send a
199 PING request over the meta connection. If the other
200 end does not reply in time, we consider them dead
201 and close the connection.
203 static void timeout_handler(int fd, short events, void *event)
205 avl_node_t *node, *next;
207 time_t now = time(NULL);
211 for(node = connection_tree->head; node; node = next) {
215 if(c->last_ping_time + pingtimeout < now) {
216 if(c->status.active) {
217 if(c->status.pinged) {
218 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
219 c->name, c->hostname, now - c->last_ping_time);
220 c->status.timeout = true;
221 terminate_connection(c, true);
222 } else if(c->last_ping_time + pinginterval < now) {
226 if(c->status.remove) {
227 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
228 c->name, c->hostname, c->status.value);
232 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
233 c->name, c->hostname);
234 if(c->status.connecting) {
235 c->status.connecting = false;
236 closesocket(c->socket);
237 do_outgoing_connection(c);
239 terminate_connection(c, false);
245 event_add(event, &(struct timeval){pingtimeout, 0});
248 void handle_meta_connection_data(int fd, short events, void *data)
250 connection_t *c = data;
252 socklen_t len = sizeof(result);
254 if (c->status.remove)
257 if(c->status.connecting) {
258 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
261 finish_connecting(c);
263 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
264 _("Error while connecting to %s (%s): %s"),
265 c->name, c->hostname, strerror(result));
266 c->status.connecting = false;
267 closesocket(c->socket);
268 do_outgoing_connection(c);
273 if (!receive_meta(c)) {
274 terminate_connection(c, c->status.active);
279 static void sigterm_handler(int signal, short events, void *data) {
280 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
281 event_loopexit(NULL);
284 static void sigint_handler(int signal, short events, void *data) {
285 static int saved_debug_level = -1;
287 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
289 if(saved_debug_level != -1) {
290 logger(LOG_NOTICE, _("Reverting to old debug level (%d)"),
292 debug_level = saved_debug_level;
293 saved_debug_level = -1;
296 _("Temporarily setting debug level to 5. Kill me with SIGINT again to go back to level %d."),
298 saved_debug_level = debug_level;
303 static void sigusr1_handler(int signal, short events, void *data) {
304 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
308 static void sigusr2_handler(int signal, short events, void *data) {
309 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
316 static void sigwinch_handler(int signal, short events, void *data) {
317 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
321 static void sighup_handler(int signal, short events, void *data) {
326 static time_t last_config_check = 0;
328 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
330 /* Reread our own configuration file */
332 exit_configuration(&config_tree);
333 init_configuration(&config_tree);
335 if(!read_server_config()) {
336 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
337 event_loopexit(NULL);
341 /* Close connections to hosts that have a changed or deleted host config file */
343 for(node = connection_tree->head; node; node = node->next) {
347 free(c->outgoing->name);
349 freeaddrinfo(c->outgoing->ai);
354 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
355 if(stat(fname, &s) || s.st_mtime > last_config_check)
356 terminate_connection(c, c->status.active);
360 last_config_check = time(NULL);
362 /* Try to make outgoing connections */
364 try_outgoing_connections();
367 static void sigalrm_handler(int signal, short events, void *data) {
368 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
373 for(node = connection_tree->head; node; node = node->next) {
376 if(c->outgoing && !c->node) {
377 if(timeout_initialized(&c->outgoing->ev))
378 event_del(&c->outgoing->ev);
379 if(c->status.connecting)
381 c->outgoing->timeout = 0;
382 do_outgoing_connection(c);
388 this is where it all happens...
394 struct event timeout_event;
395 struct event sighup_event;
396 struct event sigint_event;
397 struct event sigterm_event;
398 struct event sigquit_event;
399 struct event sigusr1_event;
400 struct event sigusr2_event;
401 struct event sigwinch_event;
402 struct event sigalrm_event;
406 timeout_set(&timeout_event, timeout_handler, &timeout_event);
407 event_add(&timeout_event, &(struct timeval){pingtimeout, 0});
408 signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
409 signal_add(&sighup_event, NULL);
410 signal_set(&sigint_event, SIGINT, sigint_handler, NULL);
411 signal_add(&sigint_event, NULL);
412 signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
413 signal_add(&sigterm_event, NULL);
414 signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
415 signal_add(&sigquit_event, NULL);
416 signal_set(&sigusr1_event, SIGUSR1, sigusr1_handler, NULL);
417 signal_add(&sigusr1_event, NULL);
418 signal_set(&sigusr2_event, SIGUSR2, sigusr2_handler, NULL);
419 signal_add(&sigusr2_event, NULL);
420 signal_set(&sigwinch_event, SIGWINCH, sigwinch_handler, NULL);
421 signal_add(&sigwinch_event, NULL);
422 signal_set(&sigalrm_event, SIGALRM, sigalrm_handler, NULL);
423 signal_add(&sigalrm_event, NULL);
425 if(event_loop(0) < 0) {
426 logger(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
430 signal_del(&sighup_event);
431 signal_del(&sigint_event);
432 signal_del(&sigterm_event);
433 signal_del(&sigquit_event);
434 signal_del(&sigusr1_event);
435 signal_del(&sigusr2_event);
436 signal_del(&sigwinch_event);
437 signal_del(&sigalrm_event);
438 event_del(&timeout_event);