2 net.c -- most of the network code
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "connection.h"
33 int contradicting_add_edge = 0;
34 int contradicting_del_edge = 0;
35 static int sleeptime = 10;
36 time_t last_config_check = 0;
37 static timeout_t pingtimer;
38 static timeout_t periodictimer;
40 //TODO: move this to a better place
43 /* Purge edges of unreachable nodes. Use carefully. */
47 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Purging unreachable nodes");
49 /* Remove all edges owned by unreachable nodes. */
51 for splay_each(node_t, n, node_tree) {
52 if(!n->status.reachable) {
53 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Purging node %s (%s)", n->name, n->hostname);
55 for splay_each(edge_t, e, n->edge_tree) {
56 send_del_edge(everyone, e);
62 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
64 for splay_each(node_t, n, node_tree) {
65 if(!n->status.reachable) {
66 for splay_each(edge_t, e, edge_weight_tree)
74 Terminate a connection:
76 - Remove the edge representing this connection
78 - Check if we need to retry making an outgoing connection
80 void terminate_connection(connection_t *c, bool report) {
81 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname);
83 c->status.active = false;
85 if(c->node && c->node->connection == c)
86 c->node->connection = NULL;
90 send_del_edge(everyone, c->edge);
95 /* Run MST and SSSP algorithms */
99 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
101 if(report && !c->node->status.reachable) {
103 e = lookup_edge(c->node, myself);
105 send_del_edge(everyone, e);
111 outgoing_t *outgoing = c->outgoing;
114 /* Check if this was our outgoing connection */
117 do_outgoing_connection(outgoing);
120 /* Clean up dead proxy processes */
122 while(waitpid(-1, NULL, WNOHANG) > 0);
127 Check if the other end is active.
128 If we have sent packets, but didn't receive any,
129 then possibly the other end is dead. We send a
130 PING request over the meta connection. If the other
131 end does not reply in time, we consider them dead
132 and close the connection.
134 static void timeout_handler(void *data) {
135 for list_each(connection_t, c, connection_list) {
136 if(c->last_ping_time + pingtimeout <= now.tv_sec) {
137 if(c->status.active) {
138 if(c->status.pinged) {
139 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);
140 } else if(c->last_ping_time + pinginterval <= now.tv_sec) {
147 if(c->status.connecting)
148 logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
150 logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
152 terminate_connection(c, c->status.active);
156 timeout_set(data, &(struct timeval){pingtimeout, rand() % 100000});
159 static void periodic_handler(void *data) {
160 /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
161 This usually only happens when another node has the same Name as this node.
162 If so, sleep for a short while to prevent a storm of contradicting messages.
165 if(contradicting_del_edge > 100 && contradicting_add_edge > 100) {
166 logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime);
167 usleep(sleeptime * 1000000LL);
177 contradicting_add_edge = 0;
178 contradicting_del_edge = 0;
180 /* If AutoConnect is set, check if we need to make or break connections. */
182 if(autoconnect && node_tree->count > 1) {
183 /* Count number of active connections */
185 for list_each(connection_t, c, connection_list) {
190 if(nc < autoconnect) {
191 /* Not enough active connections, try to add one.
192 Choose a random node, if we don't have a connection to it,
193 and we are not already trying to make one, create an
194 outgoing connection to this node.
196 int r = rand() % node_tree->count;
199 for splay_each(node_t, n, node_tree) {
208 for list_each(outgoing_t, outgoing, outgoing_list) {
209 if(!strcmp(outgoing->name, n->name)) {
216 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
217 outgoing_t *outgoing = xzalloc(sizeof *outgoing);
218 outgoing->name = xstrdup(n->name);
219 list_insert_tail(outgoing_list, outgoing);
220 setup_outgoing_connection(outgoing);
224 } else if(nc > autoconnect) {
225 /* Too many active connections, try to remove one.
226 Choose a random outgoing connection to a node
227 that has at least one other connection.
232 for list_each(connection_t, c, connection_list) {
233 if(!c->status.active)
239 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
242 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
243 list_delete(outgoing_list, c->outgoing);
245 terminate_connection(c, c->status.active);
250 if(nc >= autoconnect) {
251 /* If we have enough active connections,
252 remove any pending outgoing connections.
254 for list_each(outgoing_t, o, outgoing_list) {
256 for list_each(connection_t, c, connection_list) {
257 if(c->outgoing == o) {
263 logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
264 list_delete_node(outgoing_list, node);
270 timeout_set(data, &(struct timeval){5, rand() % 100000});
273 void handle_meta_connection_data(connection_t *c) {
274 if (!receive_meta(c)) {
275 terminate_connection(c, c->status.active);
280 int reload_configuration(void) {
283 /* Reread our own configuration file */
285 exit_configuration(&config_tree);
286 init_configuration(&config_tree);
288 if(!read_server_config()) {
289 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file.");
293 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, myself->name);
294 read_config_file(config_tree, fname);
297 /* Parse some options that are allowed to be changed while tinc is running */
299 setup_myself_reloadable();
301 /* Try to make outgoing connections */
303 try_outgoing_connections();
305 /* Close connections to hosts that have a changed or deleted host config file */
307 for list_each(connection_t, c, connection_list) {
308 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
310 if(stat(fname, &s) || s.st_mtime > last_config_check) {
311 logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name);
312 terminate_connection(c, c->status.active);
317 last_config_check = now.tv_sec;
323 /* Reset the reconnection timers for all outgoing connections */
324 for list_each(outgoing_t, outgoing, outgoing_list) {
325 outgoing->timeout = 0;
327 timeout_set(&outgoing->ev, &(struct timeval){0, 0});
330 /* Check for outgoing connections that are in progress, and reset their ping timers */
331 for list_each(connection_t, c, connection_list) {
332 if(c->outgoing && !c->node)
333 c->last_ping_time = 0;
336 /* Kick the ping timeout handler */
337 timeout_set(&pingtimer, &(struct timeval){0, 0});
341 this is where it all happens...
343 int main_loop(void) {
344 timeout_add(&pingtimer, timeout_handler, &pingtimer, &(struct timeval){pingtimeout, rand() % 100000});
345 timeout_add(&periodictimer, periodic_handler, &periodictimer, &(struct timeval){pingtimeout, rand() % 100000});
348 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
352 timeout_del(&periodictimer);
353 timeout_del(&pingtimer);