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;
45 /* Purge edges and subnets of unreachable nodes. Use carefully. */
48 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Purging unreachable nodes");
50 /* Remove all edges and subnets owned by unreachable nodes. */
52 for splay_each(node_t, n, node_tree) {
53 if(!n->status.reachable) {
54 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Purging node %s (%s)", n->name, n->hostname);
56 for splay_each(subnet_t, s, n->subnet_tree) {
57 send_del_subnet(everyone, s);
62 for splay_each(edge_t, e, n->edge_tree) {
64 send_del_edge(everyone, e);
70 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
72 for splay_each(node_t, n, node_tree) {
73 if(!n->status.reachable) {
74 for splay_each(edge_t, e, edge_weight_tree)
78 if(!autoconnect && (!strictsubnets || !n->subnet_tree->head))
79 /* in strictsubnets mode do not delete nodes with subnets */
86 Terminate a connection:
88 - Remove the edge representing this connection
90 - Check if we need to retry making an outgoing connection
92 void terminate_connection(connection_t *c, bool report) {
93 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname);
95 c->status.active = false;
97 if(c->node && c->node->connection == c)
98 c->node->connection = NULL;
101 if(report && !tunnelserver)
102 send_del_edge(everyone, c->edge);
107 /* Run MST and SSSP algorithms */
111 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
113 if(report && !c->node->status.reachable) {
115 e = lookup_edge(c->node, myself);
118 send_del_edge(everyone, e);
124 outgoing_t *outgoing = c->outgoing;
127 /* Check if this was our outgoing connection */
130 do_outgoing_connection(outgoing);
134 Check if the other end is active.
135 If we have sent packets, but didn't receive any,
136 then possibly the other end is dead. We send a
137 PING request over the meta connection. If the other
138 end does not reply in time, we consider them dead
139 and close the connection.
141 static void timeout_handler(void *data) {
142 for list_each(connection_t, c, connection_list) {
143 if(c->status.control)
146 if(c->last_ping_time + pingtimeout <= now.tv_sec) {
147 if(c->status.active) {
148 if(c->status.pinged) {
149 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);
150 } else if(c->last_ping_time + pinginterval <= now.tv_sec) {
157 if(c->status.connecting)
158 logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
160 logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
162 terminate_connection(c, c->status.active);
166 timeout_set(data, &(struct timeval){pingtimeout, rand() % 100000});
169 static void periodic_handler(void *data) {
170 /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
171 This usually only happens when another node has the same Name as this node.
172 If so, sleep for a short while to prevent a storm of contradicting messages.
175 if(contradicting_del_edge > 100 && contradicting_add_edge > 100) {
176 logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime);
177 usleep(sleeptime * 1000000LL);
187 contradicting_add_edge = 0;
188 contradicting_del_edge = 0;
190 /* If AutoConnect is set, check if we need to make or break connections. */
192 if(autoconnect && node_tree->count > 1) {
193 /* Count number of active connections */
195 for list_each(connection_t, c, connection_list) {
196 if(c->status.active && !c->status.control)
200 if(nc < autoconnect) {
201 /* Not enough active connections, try to add one.
202 Choose a random node, if we don't have a connection to it,
203 and we are not already trying to make one, create an
204 outgoing connection to this node.
206 int r = rand() % node_tree->count;
209 for splay_each(node_t, n, node_tree) {
218 for list_each(outgoing_t, outgoing, outgoing_list) {
219 if(!strcmp(outgoing->name, n->name)) {
226 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
227 outgoing_t *outgoing = xmalloc_and_zero(sizeof *outgoing);
228 outgoing->name = xstrdup(n->name);
229 list_insert_tail(outgoing_list, outgoing);
230 setup_outgoing_connection(outgoing);
234 } else if(nc > autoconnect) {
235 /* Too many active connections, try to remove one.
236 Choose a random outgoing connection to a node
237 that has at least one other connection.
242 for list_each(connection_t, c, connection_list) {
243 if(!c->status.active || c->status.control)
249 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
252 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
253 list_delete(outgoing_list, c->outgoing);
255 terminate_connection(c, c->status.active);
260 if(nc >= autoconnect) {
261 /* If we have enough active connections,
262 remove any pending outgoing connections.
264 for list_each(outgoing_t, o, outgoing_list) {
266 for list_each(connection_t, c, connection_list) {
267 if(c->outgoing == o) {
273 logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
274 list_delete_node(outgoing_list, node);
280 timeout_set(data, &(struct timeval){5, rand() % 100000});
283 void handle_meta_connection_data(connection_t *c) {
284 if (!receive_meta(c)) {
285 terminate_connection(c, c->status.active);
290 static void sigterm_handler(void *data) {
291 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
295 static void sighup_handler(void *data) {
296 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
298 reload_configuration();
301 static void sigalrm_handler(void *data) {
302 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
306 int reload_configuration(void) {
309 /* Reread our own configuration file */
311 exit_configuration(&config_tree);
312 init_configuration(&config_tree);
314 if(!read_server_config()) {
315 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file, exitting.");
320 read_config_options(config_tree, NULL);
322 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, myself->name);
323 read_config_file(config_tree, fname);
326 /* Parse some options that are allowed to be changed while tinc is running */
328 setup_myself_reloadable();
330 /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
333 for splay_each(subnet_t, subnet, subnet_tree)
338 for splay_each(subnet_t, subnet, subnet_tree) {
339 if(subnet->expires == 1) {
340 send_del_subnet(everyone, subnet);
341 if(subnet->owner->status.reachable)
342 subnet_update(subnet->owner, subnet, false);
343 subnet_del(subnet->owner, subnet);
344 } else if(subnet->expires == -1) {
347 send_add_subnet(everyone, subnet);
348 if(subnet->owner->status.reachable)
349 subnet_update(subnet->owner, subnet, true);
352 } else { /* Only read our own subnets back in */
353 for splay_each(subnet_t, subnet, myself->subnet_tree)
357 config_t *cfg = lookup_config(config_tree, "Subnet");
360 subnet_t *subnet, *s2;
362 if(!get_config_subnet(cfg, &subnet))
365 if((s2 = lookup_subnet(myself, subnet))) {
371 subnet_add(myself, subnet);
372 send_add_subnet(everyone, subnet);
373 subnet_update(myself, subnet, true);
376 cfg = lookup_config_next(config_tree, cfg);
379 for splay_each(subnet_t, subnet, myself->subnet_tree) {
380 if(subnet->expires == 1) {
381 send_del_subnet(everyone, subnet);
382 subnet_update(myself, subnet, false);
383 subnet_del(myself, subnet);
388 /* Try to make outgoing connections */
390 try_outgoing_connections();
392 /* Close connections to hosts that have a changed or deleted host config file */
394 for list_each(connection_t, c, connection_list) {
395 if(c->status.control)
398 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
400 if(stat(fname, &s) || s.st_mtime > last_config_check) {
401 logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name);
402 terminate_connection(c, c->status.active);
407 last_config_check = time(NULL);
413 for list_each(connection_t, c, connection_list) {
414 if(c->outgoing && !c->node) {
415 timeout_del(&c->outgoing->ev);
416 if(c->status.connecting)
418 c->outgoing->timeout = 0;
419 terminate_connection(c, c->status.active);
425 this is where it all happens...
427 int main_loop(void) {
428 timeout_t pingtimer = {{0}};
429 timeout_t periodictimer = {{0}};
431 timeout_add(&pingtimer, timeout_handler, &pingtimer, &(struct timeval){pingtimeout, rand() % 100000});
432 timeout_add(&periodictimer, periodic_handler, &periodictimer, &(struct timeval){pingtimeout, rand() % 100000});
435 signal_t sighup = {0};
436 signal_t sigterm = {0};
437 signal_t sigquit = {0};
438 signal_t sigint = {0};
439 signal_t sigalrm = {0};
441 signal_add(&sighup, sighup_handler, &sighup, SIGHUP);
442 signal_add(&sigterm, sigterm_handler, &sigterm, SIGTERM);
443 signal_add(&sigquit, sigterm_handler, &sigquit, SIGQUIT);
444 signal_add(&sigint, sigterm_handler, &sigint, SIGINT);
445 signal_add(&sigalrm, sigalrm_handler, &sigalrm, SIGALRM);
449 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
455 signal_del(&sigalrm);
456 signal_del(&sigquit);
457 signal_del(&sigterm);
460 timeout_del(&periodictimer);
461 timeout_del(&pingtimer);