2 net.c -- most of the network code
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2013 Guus Sliepen <guus@tinc-vpn.org>
5 2006 Scott Lamb <slamb@slamb.org>
6 2011 Loïc Grenié <loic.grenie@gmail.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "connection.h"
40 int contradicting_add_edge = 0;
41 int contradicting_del_edge = 0;
42 static int sleeptime = 10;
43 time_t last_config_check = 0;
44 static timeout_t pingtimer;
45 static timeout_t periodictimer;
47 /* Purge edges and subnets of unreachable nodes. Use carefully. */
50 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Purging unreachable nodes");
52 /* Remove all edges and subnets owned by unreachable nodes. */
54 for splay_each(node_t, n, node_tree) {
55 if(!n->status.reachable) {
56 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Purging node %s (%s)", n->name, n->hostname);
58 for splay_each(subnet_t, s, n->subnet_tree) {
59 send_del_subnet(everyone, s);
64 for splay_each(edge_t, e, n->edge_tree) {
66 send_del_edge(everyone, e);
72 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
74 for splay_each(node_t, n, node_tree) {
75 if(!n->status.reachable) {
76 for splay_each(edge_t, e, edge_weight_tree)
80 if(!autoconnect && (!strictsubnets || !n->subnet_tree->head))
81 /* in strictsubnets mode do not delete nodes with subnets */
88 Terminate a connection:
90 - Remove the edge representing this connection
92 - Check if we need to retry making an outgoing connection
94 void terminate_connection(connection_t *c, bool report) {
95 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname);
97 c->status.active = false;
99 if(c->node && c->node->connection == c)
100 c->node->connection = NULL;
103 if(report && !tunnelserver)
104 send_del_edge(everyone, c->edge);
109 /* Run MST and SSSP algorithms */
113 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
115 if(report && !c->node->status.reachable) {
117 e = lookup_edge(c->node, myself);
120 send_del_edge(everyone, e);
126 outgoing_t *outgoing = c->outgoing;
129 /* Check if this was our outgoing connection */
132 do_outgoing_connection(outgoing);
136 Check if the other end is active.
137 If we have sent packets, but didn't receive any,
138 then possibly the other end is dead. We send a
139 PING request over the meta connection. If the other
140 end does not reply in time, we consider them dead
141 and close the connection.
143 static void timeout_handler(void *data) {
144 for list_each(connection_t, c, connection_list) {
145 if(c->status.control)
148 if(c->last_ping_time + pingtimeout <= now.tv_sec) {
149 if(c->status.active) {
150 if(c->status.pinged) {
151 logger(DEBUG_CONNECTIONS, LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds", c->name, c->hostname, (long)now.tv_sec - c->last_ping_time);
152 } else if(c->last_ping_time + pinginterval <= now.tv_sec) {
159 if(c->status.connecting)
160 logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
162 logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
164 terminate_connection(c, c->status.active);
168 timeout_set(data, &(struct timeval){pingtimeout, rand() % 100000});
171 static void periodic_handler(void *data) {
172 /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
173 This usually only happens when another node has the same Name as this node.
174 If so, sleep for a short while to prevent a storm of contradicting messages.
177 if(contradicting_del_edge > 100 && contradicting_add_edge > 100) {
178 logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime);
179 usleep(sleeptime * 1000000LL);
189 contradicting_add_edge = 0;
190 contradicting_del_edge = 0;
192 /* If AutoConnect is set, check if we need to make or break connections. */
194 if(autoconnect && node_tree->count > 1) {
195 /* Count number of active connections */
197 for list_each(connection_t, c, connection_list) {
198 if(c->status.active && !c->status.control)
202 if(nc < autoconnect) {
203 /* Not enough active connections, try to add one.
204 Choose a random node, if we don't have a connection to it,
205 and we are not already trying to make one, create an
206 outgoing connection to this node.
208 int r = rand() % node_tree->count;
211 for splay_each(node_t, n, node_tree) {
220 for list_each(outgoing_t, outgoing, outgoing_list) {
221 if(!strcmp(outgoing->name, n->name)) {
228 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
229 outgoing_t *outgoing = xzalloc(sizeof *outgoing);
230 outgoing->name = xstrdup(n->name);
231 list_insert_tail(outgoing_list, outgoing);
232 setup_outgoing_connection(outgoing);
236 } else if(nc > autoconnect) {
237 /* Too many active connections, try to remove one.
238 Choose a random outgoing connection to a node
239 that has at least one other connection.
244 for list_each(connection_t, c, connection_list) {
245 if(!c->status.active || c->status.control)
251 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
254 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
255 list_delete(outgoing_list, c->outgoing);
257 terminate_connection(c, c->status.active);
262 if(nc >= autoconnect) {
263 /* If we have enough active connections,
264 remove any pending outgoing connections.
266 for list_each(outgoing_t, o, outgoing_list) {
268 for list_each(connection_t, c, connection_list) {
269 if(c->outgoing == o) {
275 logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
276 list_delete_node(outgoing_list, node);
282 timeout_set(data, &(struct timeval){5, rand() % 100000});
285 void handle_meta_connection_data(connection_t *c) {
286 if (!receive_meta(c)) {
287 terminate_connection(c, c->status.active);
293 static void sigterm_handler(void *data) {
294 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
298 static void sighup_handler(void *data) {
299 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
301 if(reload_configuration())
305 static void sigalrm_handler(void *data) {
306 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
311 int reload_configuration(void) {
314 /* Reread our own configuration file */
316 exit_configuration(&config_tree);
317 init_configuration(&config_tree);
319 if(!read_server_config()) {
320 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file.");
324 read_config_options(config_tree, NULL);
326 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, myself->name);
327 read_config_file(config_tree, fname);
330 /* Parse some options that are allowed to be changed while tinc is running */
332 setup_myself_reloadable();
334 /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
337 for splay_each(subnet_t, subnet, subnet_tree)
342 for splay_each(subnet_t, subnet, subnet_tree) {
343 if(subnet->expires == 1) {
344 send_del_subnet(everyone, subnet);
345 if(subnet->owner->status.reachable)
346 subnet_update(subnet->owner, subnet, false);
347 subnet_del(subnet->owner, subnet);
348 } else if(subnet->expires == -1) {
351 send_add_subnet(everyone, subnet);
352 if(subnet->owner->status.reachable)
353 subnet_update(subnet->owner, subnet, true);
356 } else { /* Only read our own subnets back in */
357 for splay_each(subnet_t, subnet, myself->subnet_tree)
361 config_t *cfg = lookup_config(config_tree, "Subnet");
364 subnet_t *subnet, *s2;
366 if(!get_config_subnet(cfg, &subnet))
369 if((s2 = lookup_subnet(myself, subnet))) {
375 subnet_add(myself, subnet);
376 send_add_subnet(everyone, subnet);
377 subnet_update(myself, subnet, true);
380 cfg = lookup_config_next(config_tree, cfg);
383 for splay_each(subnet_t, subnet, myself->subnet_tree) {
384 if(subnet->expires == 1) {
385 send_del_subnet(everyone, subnet);
386 subnet_update(myself, subnet, false);
387 subnet_del(myself, subnet);
392 /* Try to make outgoing connections */
394 try_outgoing_connections();
396 /* Close connections to hosts that have a changed or deleted host config file */
398 for list_each(connection_t, c, connection_list) {
399 if(c->status.control)
402 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
404 if(stat(fname, &s) || s.st_mtime > last_config_check) {
405 logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name);
406 terminate_connection(c, c->status.active);
411 last_config_check = now.tv_sec;
417 /* Reset the reconnection timers for all outgoing connections */
418 for list_each(outgoing_t, outgoing, outgoing_list) {
419 outgoing->timeout = 0;
421 timeout_set(&outgoing->ev, &(struct timeval){0, 0});
424 /* Check for outgoing connections that are in progress, and reset their ping timers */
425 for list_each(connection_t, c, connection_list) {
426 if(c->outgoing && !c->node)
427 c->last_ping_time = 0;
430 /* Kick the ping timeout handler */
431 timeout_set(&pingtimer, &(struct timeval){0, 0});
435 this is where it all happens...
437 int main_loop(void) {
438 timeout_add(&pingtimer, timeout_handler, &pingtimer, &(struct timeval){pingtimeout, rand() % 100000});
439 timeout_add(&periodictimer, periodic_handler, &periodictimer, &(struct timeval){pingtimeout, rand() % 100000});
442 signal_t sighup = {0};
443 signal_t sigterm = {0};
444 signal_t sigquit = {0};
445 signal_t sigint = {0};
446 signal_t sigalrm = {0};
448 signal_add(&sighup, sighup_handler, &sighup, SIGHUP);
449 signal_add(&sigterm, sigterm_handler, &sigterm, SIGTERM);
450 signal_add(&sigquit, sigterm_handler, &sigquit, SIGQUIT);
451 signal_add(&sigint, sigterm_handler, &sigint, SIGINT);
452 signal_add(&sigalrm, sigalrm_handler, &sigalrm, SIGALRM);
456 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
462 signal_del(&sigterm);
463 signal_del(&sigquit);
465 signal_del(&sigalrm);
468 timeout_del(&periodictimer);
469 timeout_del(&pingtimer);