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->last_ping_time + pingtimeout <= now.tv_sec) {
140 if(c->status.active) {
141 if(c->status.pinged) {
142 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);
143 } else if(c->last_ping_time + pinginterval <= now.tv_sec) {
150 if(c->status.connecting)
151 logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
153 logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
155 terminate_connection(c, c->status.active);
159 timeout_set(data, &(struct timeval){pingtimeout, rand() % 100000});
162 static void periodic_handler(void *data) {
163 /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
164 This usually only happens when another node has the same Name as this node.
165 If so, sleep for a short while to prevent a storm of contradicting messages.
168 if(contradicting_del_edge > 100 && contradicting_add_edge > 100) {
169 logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime);
170 usleep(sleeptime * 1000000LL);
180 contradicting_add_edge = 0;
181 contradicting_del_edge = 0;
183 /* If AutoConnect is set, check if we need to make or break connections. */
185 if(autoconnect && node_tree->count > 1) {
186 /* Count number of active connections */
188 for list_each(connection_t, c, connection_list) {
193 if(nc < autoconnect) {
194 /* Not enough active connections, try to add one.
195 Choose a random node, if we don't have a connection to it,
196 and we are not already trying to make one, create an
197 outgoing connection to this node.
199 int r = rand() % node_tree->count;
202 for splay_each(node_t, n, node_tree) {
211 for list_each(outgoing_t, outgoing, outgoing_list) {
212 if(!strcmp(outgoing->name, n->name)) {
219 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
220 outgoing_t *outgoing = xzalloc(sizeof *outgoing);
221 outgoing->name = xstrdup(n->name);
222 list_insert_tail(outgoing_list, outgoing);
223 setup_outgoing_connection(outgoing);
227 } else if(nc > autoconnect) {
228 /* Too many active connections, try to remove one.
229 Choose a random outgoing connection to a node
230 that has at least one other connection.
235 for list_each(connection_t, c, connection_list) {
236 if(!c->status.active)
242 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
245 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
246 list_delete(outgoing_list, c->outgoing);
248 terminate_connection(c, c->status.active);
253 if(nc >= autoconnect) {
254 /* If we have enough active connections,
255 remove any pending outgoing connections.
257 for list_each(outgoing_t, o, outgoing_list) {
259 for list_each(connection_t, c, connection_list) {
260 if(c->outgoing == o) {
266 logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
267 list_delete_node(outgoing_list, node);
273 timeout_set(data, &(struct timeval){5, rand() % 100000});
276 void handle_meta_connection_data(connection_t *c) {
277 if (!receive_meta(c)) {
278 terminate_connection(c, c->status.active);
284 static void sigterm_handler(void *data) {
285 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
289 static void sighup_handler(void *data) {
290 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
292 if(reload_configuration())
296 static void sigalrm_handler(void *data) {
297 logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
302 int reload_configuration(void) {
305 /* Reread our own configuration file */
307 exit_configuration(&config_tree);
308 init_configuration(&config_tree);
310 if(!read_server_config()) {
311 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file.");
315 read_config_options(config_tree, NULL);
317 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, myself->name);
318 read_config_file(config_tree, fname);
321 /* Parse some options that are allowed to be changed while tinc is running */
323 setup_myself_reloadable();
325 /* Try to make outgoing connections */
327 try_outgoing_connections();
329 /* Close connections to hosts that have a changed or deleted host config file */
331 for list_each(connection_t, c, connection_list) {
332 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
334 if(stat(fname, &s) || s.st_mtime > last_config_check) {
335 logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name);
336 terminate_connection(c, c->status.active);
341 last_config_check = now.tv_sec;
347 /* Reset the reconnection timers for all outgoing connections */
348 for list_each(outgoing_t, outgoing, outgoing_list) {
349 outgoing->timeout = 0;
351 timeout_set(&outgoing->ev, &(struct timeval){0, 0});
354 /* Check for outgoing connections that are in progress, and reset their ping timers */
355 for list_each(connection_t, c, connection_list) {
356 if(c->outgoing && !c->node)
357 c->last_ping_time = 0;
360 /* Kick the ping timeout handler */
361 timeout_set(&pingtimer, &(struct timeval){0, 0});
365 this is where it all happens...
367 int main_loop(void) {
368 timeout_add(&pingtimer, timeout_handler, &pingtimer, &(struct timeval){pingtimeout, rand() % 100000});
369 timeout_add(&periodictimer, periodic_handler, &periodictimer, &(struct timeval){pingtimeout, rand() % 100000});
372 signal_t sighup = {0};
373 signal_t sigterm = {0};
374 signal_t sigquit = {0};
375 signal_t sigint = {0};
376 signal_t sigalrm = {0};
378 signal_add(&sighup, sighup_handler, &sighup, SIGHUP);
379 signal_add(&sigterm, sigterm_handler, &sigterm, SIGTERM);
380 signal_add(&sigquit, sigterm_handler, &sigquit, SIGQUIT);
381 signal_add(&sigint, sigterm_handler, &sigint, SIGINT);
382 signal_add(&sigalrm, sigalrm_handler, &sigalrm, SIGALRM);
386 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
392 signal_del(&sigterm);
393 signal_del(&sigquit);
395 signal_del(&sigalrm);
398 timeout_del(&periodictimer);
399 timeout_del(&pingtimer);