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"
37 int contradicting_add_edge = 0;
38 int contradicting_del_edge = 0;
39 static int sleeptime = 10;
40 time_t last_config_check = 0;
41 static timeout_t pingtimer;
42 static timeout_t periodictimer;
44 /* Purge edges of unreachable nodes. Use carefully. */
48 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Purging unreachable nodes");
50 /* Remove all edges 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(edge_t, e, n->edge_tree) {
58 send_del_edge(everyone, e);
64 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
66 for splay_each(node_t, n, node_tree) {
67 if(!n->status.reachable) {
68 for splay_each(edge_t, e, edge_weight_tree)
76 Terminate a connection:
78 - Remove the edge representing this connection
80 - Check if we need to retry making an outgoing connection
82 void terminate_connection(connection_t *c, bool report) {
83 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname);
85 c->status.active = false;
87 if(c->node && c->node->connection == c)
88 c->node->connection = NULL;
91 if(report && !tunnelserver)
92 send_del_edge(everyone, c->edge);
97 /* Run MST and SSSP algorithms */
101 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
103 if(report && !c->node->status.reachable) {
105 e = lookup_edge(c->node, myself);
108 send_del_edge(everyone, e);
114 outgoing_t *outgoing = c->outgoing;
117 /* Check if this was our outgoing connection */
120 do_outgoing_connection(outgoing);
123 /* Clean up dead proxy processes */
125 while(waitpid(-1, NULL, WNOHANG) > 0);
130 Check if the other end is active.
131 If we have sent packets, but didn't receive any,
132 then possibly the other end is dead. We send a
133 PING request over the meta connection. If the other
134 end does not reply in time, we consider them dead
135 and close the connection.
137 static void timeout_handler(void *data) {
138 for list_each(connection_t, c, connection_list) {
139 if(c->status.control)
142 if(c->last_ping_time + pingtimeout <= now.tv_sec) {
143 if(c->status.active) {
144 if(c->status.pinged) {
145 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);
146 } else if(c->last_ping_time + pinginterval <= now.tv_sec) {
153 if(c->status.connecting)
154 logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
156 logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
158 terminate_connection(c, c->status.active);
162 timeout_set(data, &(struct timeval){pingtimeout, rand() % 100000});
165 static void periodic_handler(void *data) {
166 /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
167 This usually only happens when another node has the same Name as this node.
168 If so, sleep for a short while to prevent a storm of contradicting messages.
171 if(contradicting_del_edge > 100 && contradicting_add_edge > 100) {
172 logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime);
173 usleep(sleeptime * 1000000LL);
183 contradicting_add_edge = 0;
184 contradicting_del_edge = 0;
186 /* If AutoConnect is set, check if we need to make or break connections. */
188 if(autoconnect && node_tree->count > 1) {
189 /* Count number of active connections */
191 for list_each(connection_t, c, connection_list) {
192 if(c->status.active && !c->status.control)
196 if(nc < autoconnect) {
197 /* Not enough active connections, try to add one.
198 Choose a random node, if we don't have a connection to it,
199 and we are not already trying to make one, create an
200 outgoing connection to this node.
202 int r = rand() % node_tree->count;
205 for splay_each(node_t, n, node_tree) {
214 for list_each(outgoing_t, outgoing, outgoing_list) {
215 if(!strcmp(outgoing->name, n->name)) {
222 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
223 outgoing_t *outgoing = xzalloc(sizeof *outgoing);
224 outgoing->name = xstrdup(n->name);
225 list_insert_tail(outgoing_list, outgoing);
226 setup_outgoing_connection(outgoing);
230 } else if(nc > autoconnect) {
231 /* Too many active connections, try to remove one.
232 Choose a random outgoing connection to a node
233 that has at least one other connection.
238 for list_each(connection_t, c, connection_list) {
239 if(!c->status.active || c->status.control)
245 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
248 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
249 list_delete(outgoing_list, c->outgoing);
251 terminate_connection(c, c->status.active);
256 if(nc >= autoconnect) {
257 /* If we have enough active connections,
258 remove any pending outgoing connections.
260 for list_each(outgoing_t, o, outgoing_list) {
262 for list_each(connection_t, c, connection_list) {
263 if(c->outgoing == o) {
269 logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
270 list_delete_node(outgoing_list, node);
276 timeout_set(data, &(struct timeval){5, rand() % 100000});
279 void handle_meta_connection_data(connection_t *c) {
280 if (!receive_meta(c)) {
281 terminate_connection(c, c->status.active);
287 static void sigterm_handler(void *data) {
288 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
292 static void sighup_handler(void *data) {
293 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
295 if(reload_configuration())
299 static void sigalrm_handler(void *data) {
300 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
305 int reload_configuration(void) {
308 /* Reread our own configuration file */
310 exit_configuration(&config_tree);
311 init_configuration(&config_tree);
313 if(!read_server_config()) {
314 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file.");
318 read_config_options(config_tree, NULL);
320 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, myself->name);
321 read_config_file(config_tree, fname);
324 /* Parse some options that are allowed to be changed while tinc is running */
326 setup_myself_reloadable();
328 /* Try to make outgoing connections */
330 try_outgoing_connections();
332 /* Close connections to hosts that have a changed or deleted host config file */
334 for list_each(connection_t, c, connection_list) {
335 if(c->status.control)
338 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
340 if(stat(fname, &s) || s.st_mtime > last_config_check) {
341 logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name);
342 terminate_connection(c, c->status.active);
347 last_config_check = now.tv_sec;
353 /* Reset the reconnection timers for all outgoing connections */
354 for list_each(outgoing_t, outgoing, outgoing_list) {
355 outgoing->timeout = 0;
357 timeout_set(&outgoing->ev, &(struct timeval){0, 0});
360 /* Check for outgoing connections that are in progress, and reset their ping timers */
361 for list_each(connection_t, c, connection_list) {
362 if(c->outgoing && !c->node)
363 c->last_ping_time = 0;
366 /* Kick the ping timeout handler */
367 timeout_set(&pingtimer, &(struct timeval){0, 0});
371 this is where it all happens...
373 int main_loop(void) {
374 timeout_add(&pingtimer, timeout_handler, &pingtimer, &(struct timeval){pingtimeout, rand() % 100000});
375 timeout_add(&periodictimer, periodic_handler, &periodictimer, &(struct timeval){pingtimeout, rand() % 100000});
378 signal_t sighup = {0};
379 signal_t sigterm = {0};
380 signal_t sigquit = {0};
381 signal_t sigint = {0};
382 signal_t sigalrm = {0};
384 signal_add(&sighup, sighup_handler, &sighup, SIGHUP);
385 signal_add(&sigterm, sigterm_handler, &sigterm, SIGTERM);
386 signal_add(&sigquit, sigterm_handler, &sigquit, SIGQUIT);
387 signal_add(&sigint, sigterm_handler, &sigint, SIGINT);
388 signal_add(&sigalrm, sigalrm_handler, &sigalrm, SIGALRM);
392 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
398 signal_del(&sigterm);
399 signal_del(&sigquit);
401 signal_del(&sigalrm);
404 timeout_del(&periodictimer);
405 timeout_del(&pingtimer);