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"
27 #include "meshlink_internal.h"
34 int contradicting_add_edge = 0;
35 int contradicting_del_edge = 0;
36 static int sleeptime = 10;
37 time_t last_config_check = 0;
38 static timeout_t pingtimer;
39 static timeout_t periodictimer;
41 //TODO: move this to a better place
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, mesh->nodes) {
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) {
57 send_del_edge(everyone, e);
63 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
65 for splay_each(node_t, n, mesh->nodes) {
66 if(!n->status.reachable) {
67 for splay_each(edge_t, e, mesh->edges)
75 Terminate a connection:
77 - Remove the edge representing this connection
79 - Check if we need to retry making an outgoing connection
81 void terminate_connection(connection_t *c, bool report) {
82 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname);
84 c->status.active = false;
86 if(c->node && c->node->connection == c)
87 c->node->connection = NULL;
91 send_del_edge(everyone, c->edge);
96 /* Run MST and SSSP algorithms */
100 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
102 if(report && !c->node->status.reachable) {
104 e = lookup_edge(c->node, mesh->self);
106 send_del_edge(everyone, e);
112 outgoing_t *outgoing = c->outgoing;
115 /* Check if this was our outgoing connection */
118 do_outgoing_connection(outgoing);
121 /* Clean up dead proxy processes */
123 while(waitpid(-1, NULL, WNOHANG) > 0);
128 Check if the other end is active.
129 If we have sent packets, but didn't receive any,
130 then possibly the other end is dead. We send a
131 PING request over the meta connection. If the other
132 end does not reply in time, we consider them dead
133 and close the connection.
135 static void timeout_handler(void *data) {
136 for list_each(connection_t, c, mesh->connections) {
137 if(c->last_ping_time + pingtimeout <= now.tv_sec) {
138 if(c->status.active) {
139 if(c->status.pinged) {
140 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);
141 } else if(c->last_ping_time + pinginterval <= now.tv_sec) {
148 if(c->status.connecting)
149 logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
151 logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
153 terminate_connection(c, c->status.active);
157 timeout_set(data, &(struct timeval){pingtimeout, rand() % 100000});
160 static void periodic_handler(void *data) {
161 /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
162 This usually only happens when another node has the same Name as this node.
163 If so, sleep for a short while to prevent a storm of contradicting messages.
166 if(contradicting_del_edge > 100 && contradicting_add_edge > 100) {
167 logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime);
168 usleep(sleeptime * 1000000LL);
178 contradicting_add_edge = 0;
179 contradicting_del_edge = 0;
181 /* If AutoConnect is set, check if we need to make or break connections. */
183 if(autoconnect && mesh->nodes->count > 1) {
184 /* Count number of active connections */
186 for list_each(connection_t, c, mesh->connections) {
191 if(nc < autoconnect) {
192 /* Not enough active connections, try to add one.
193 Choose a random node, if we don't have a connection to it,
194 and we are not already trying to make one, create an
195 outgoing connection to this node.
197 int r = rand() % mesh->nodes->count;
200 for splay_each(node_t, n, mesh->nodes) {
209 for list_each(outgoing_t, outgoing, mesh->outgoings) {
210 if(!strcmp(outgoing->name, n->name)) {
217 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
218 outgoing_t *outgoing = xzalloc(sizeof *outgoing);
219 outgoing->name = xstrdup(n->name);
220 list_insert_tail(mesh->outgoings, outgoing);
221 setup_outgoing_connection(outgoing);
225 } else if(nc > autoconnect) {
226 /* Too many active connections, try to remove one.
227 Choose a random outgoing connection to a node
228 that has at least one other connection.
233 for list_each(connection_t, c, mesh->connections) {
234 if(!c->status.active)
240 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
243 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
244 list_delete(mesh->outgoings, c->outgoing);
246 terminate_connection(c, c->status.active);
251 if(nc >= autoconnect) {
252 /* If we have enough active connections,
253 remove any pending outgoing connections.
255 for list_each(outgoing_t, o, mesh->outgoings) {
257 for list_each(connection_t, c, mesh->connections) {
258 if(c->outgoing == o) {
264 logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
265 list_delete_node(mesh->outgoings, node);
271 timeout_set(data, &(struct timeval){5, rand() % 100000});
274 void handle_meta_connection_data(connection_t *c) {
275 if (!receive_meta(c)) {
276 terminate_connection(c, c->status.active);
281 int reload_configuration(void) {
284 /* Reread our own configuration file */
286 exit_configuration(&mesh->config);
287 init_configuration(&mesh->config);
289 if(!read_server_config()) {
290 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file.");
294 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, mesh->self->name);
295 read_config_file(mesh->config, fname);
298 /* Parse some options that are allowed to be changed while tinc is running */
300 setup_myself_reloadable();
302 /* Try to make outgoing connections */
304 try_outgoing_connections();
306 /* Close connections to hosts that have a changed or deleted host config file */
308 for list_each(connection_t, c, mesh->connections) {
309 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
311 if(stat(fname, &s) || s.st_mtime > last_config_check) {
312 logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name);
313 terminate_connection(c, c->status.active);
318 last_config_check = now.tv_sec;
324 /* Reset the reconnection timers for all outgoing connections */
325 for list_each(outgoing_t, outgoing, mesh->outgoings) {
326 outgoing->timeout = 0;
328 timeout_set(&outgoing->ev, &(struct timeval){0, 0});
331 /* Check for outgoing connections that are in progress, and reset their ping timers */
332 for list_each(connection_t, c, mesh->connections) {
333 if(c->outgoing && !c->node)
334 c->last_ping_time = 0;
337 /* Kick the ping timeout handler */
338 timeout_set(&pingtimer, &(struct timeval){0, 0});
342 this is where it all happens...
344 int main_loop(void) {
345 timeout_add(&pingtimer, timeout_handler, &pingtimer, &(struct timeval){pingtimeout, rand() % 100000});
346 timeout_add(&periodictimer, periodic_handler, &periodictimer, &(struct timeval){pingtimeout, rand() % 100000});
349 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
353 timeout_del(&periodictimer);
354 timeout_del(&pingtimer);