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 = xzalloc(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);
291 static void sigterm_handler(void *data) {
292 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
296 static void sighup_handler(void *data) {
297 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
299 reload_configuration();
302 static void sigalrm_handler(void *data) {
303 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
308 int reload_configuration(void) {
311 /* Reread our own configuration file */
313 exit_configuration(&config_tree);
314 init_configuration(&config_tree);
316 if(!read_server_config()) {
317 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file, exitting.");
322 read_config_options(config_tree, NULL);
324 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, myself->name);
325 read_config_file(config_tree, fname);
328 /* Parse some options that are allowed to be changed while tinc is running */
330 setup_myself_reloadable();
332 /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
335 for splay_each(subnet_t, subnet, subnet_tree)
340 for splay_each(subnet_t, subnet, subnet_tree) {
341 if(subnet->expires == 1) {
342 send_del_subnet(everyone, subnet);
343 if(subnet->owner->status.reachable)
344 subnet_update(subnet->owner, subnet, false);
345 subnet_del(subnet->owner, subnet);
346 } else if(subnet->expires == -1) {
349 send_add_subnet(everyone, subnet);
350 if(subnet->owner->status.reachable)
351 subnet_update(subnet->owner, subnet, true);
354 } else { /* Only read our own subnets back in */
355 for splay_each(subnet_t, subnet, myself->subnet_tree)
359 config_t *cfg = lookup_config(config_tree, "Subnet");
362 subnet_t *subnet, *s2;
364 if(!get_config_subnet(cfg, &subnet))
367 if((s2 = lookup_subnet(myself, subnet))) {
373 subnet_add(myself, subnet);
374 send_add_subnet(everyone, subnet);
375 subnet_update(myself, subnet, true);
378 cfg = lookup_config_next(config_tree, cfg);
381 for splay_each(subnet_t, subnet, myself->subnet_tree) {
382 if(subnet->expires == 1) {
383 send_del_subnet(everyone, subnet);
384 subnet_update(myself, subnet, false);
385 subnet_del(myself, subnet);
390 /* Try to make outgoing connections */
392 try_outgoing_connections();
394 /* Close connections to hosts that have a changed or deleted host config file */
396 for list_each(connection_t, c, connection_list) {
397 if(c->status.control)
400 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
402 if(stat(fname, &s) || s.st_mtime > last_config_check) {
403 logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name);
404 terminate_connection(c, c->status.active);
409 last_config_check = now.tv_sec;
415 for list_each(connection_t, c, connection_list) {
416 if(c->outgoing && !c->node) {
417 timeout_del(&c->outgoing->ev);
418 if(c->status.connecting)
420 c->outgoing->timeout = 0;
421 terminate_connection(c, c->status.active);
427 this is where it all happens...
429 int main_loop(void) {
430 timeout_t pingtimer = {{0}};
431 timeout_t periodictimer = {{0}};
433 timeout_add(&pingtimer, timeout_handler, &pingtimer, &(struct timeval){pingtimeout, rand() % 100000});
434 timeout_add(&periodictimer, periodic_handler, &periodictimer, &(struct timeval){pingtimeout, rand() % 100000});
437 signal_t sighup = {0};
438 signal_t sigterm = {0};
439 signal_t sigquit = {0};
440 signal_t sigint = {0};
441 signal_t sigalrm = {0};
443 signal_add(&sighup, sighup_handler, &sighup, SIGHUP);
444 signal_add(&sigterm, sigterm_handler, &sigterm, SIGTERM);
445 signal_add(&sigquit, sigterm_handler, &sigquit, SIGQUIT);
446 signal_add(&sigint, sigterm_handler, &sigint, SIGINT);
447 signal_add(&sigalrm, sigalrm_handler, &sigalrm, SIGALRM);
451 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
457 signal_del(&sigterm);
458 signal_del(&sigquit);
460 signal_del(&sigalrm);
463 timeout_del(&periodictimer);
464 timeout_del(&pingtimer);