]> git.meshlink.io Git - meshlink/blob - src/net.c
Move config_tree to mesh->config.
[meshlink] / src / net.c
1 /*
2     net.c -- most of the network code
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21
22 #include "utils.h"
23 #include "conf.h"
24 #include "connection.h"
25 #include "graph.h"
26 #include "logger.h"
27 #include "meshlink_internal.h"
28 #include "meta.h"
29 #include "net.h"
30 #include "netutl.h"
31 #include "protocol.h"
32 #include "xalloc.h"
33
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;
40
41 //TODO: move this to a better place
42 char *confbase;
43
44 /* Purge edges of unreachable nodes. Use carefully. */
45
46 // TODO: remove
47 void purge(void) {
48         logger(DEBUG_PROTOCOL, LOG_DEBUG, "Purging unreachable nodes");
49
50         /* Remove all edges owned by unreachable nodes. */
51
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);
55
56                         for splay_each(edge_t, e, n->edge_tree) {
57                                 send_del_edge(everyone, e);
58                                 edge_del(e);
59                         }
60                 }
61         }
62
63         /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
64
65         for splay_each(node_t, n, mesh->nodes) {
66                 if(!n->status.reachable) {
67                         for splay_each(edge_t, e, mesh->edges)
68                                 if(e->to == n)
69                                         return;
70                 }
71         }
72 }
73
74 /*
75   Terminate a connection:
76   - Mark it as inactive
77   - Remove the edge representing this connection
78   - Kill it with fire
79   - Check if we need to retry making an outgoing connection
80 */
81 void terminate_connection(connection_t *c, bool report) {
82         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname);
83
84         c->status.active = false;
85
86         if(c->node && c->node->connection == c)
87                 c->node->connection = NULL;
88
89         if(c->edge) {
90                 if(report)
91                         send_del_edge(everyone, c->edge);
92
93                 edge_del(c->edge);
94                 c->edge = NULL;
95
96                 /* Run MST and SSSP algorithms */
97
98                 graph();
99
100                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
101
102                 if(report && !c->node->status.reachable) {
103                         edge_t *e;
104                         e = lookup_edge(c->node, mesh->self);
105                         if(e) {
106                                 send_del_edge(everyone, e);
107                                 edge_del(e);
108                         }
109                 }
110         }
111
112         outgoing_t *outgoing = c->outgoing;
113         connection_del(c);
114
115         /* Check if this was our outgoing connection */
116
117         if(outgoing)
118                 do_outgoing_connection(outgoing);
119
120 #ifndef HAVE_MINGW
121         /* Clean up dead proxy processes */
122
123         while(waitpid(-1, NULL, WNOHANG) > 0);
124 #endif
125 }
126
127 /*
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.
134 */
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) {
142                                         send_ping(c);
143                                         continue;
144                                 } else {
145                                         continue;
146                                 }
147                         } else {
148                                 if(c->status.connecting)
149                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
150                                 else
151                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
152                         }
153                         terminate_connection(c, c->status.active);
154                 }
155         }
156
157         timeout_set(data, &(struct timeval){pingtimeout, rand() % 100000});
158 }
159
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.
164         */
165
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);
169                 sleeptime *= 2;
170                 if(sleeptime < 0)
171                         sleeptime = 3600;
172         } else {
173                 sleeptime /= 2;
174                 if(sleeptime < 10)
175                         sleeptime = 10;
176         }
177
178         contradicting_add_edge = 0;
179         contradicting_del_edge = 0;
180
181         /* If AutoConnect is set, check if we need to make or break connections. */
182
183         if(autoconnect && mesh->nodes->count > 1) {
184                 /* Count number of active connections */
185                 int nc = 0;
186                 for list_each(connection_t, c, mesh->connections) {
187                         if(c->status.active)
188                                 nc++;
189                 }
190
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.
196                         */
197                         int r = rand() % mesh->nodes->count;
198                         int i = 0;
199
200                         for splay_each(node_t, n, mesh->nodes) {
201                                 if(i++ != r)
202                                         continue;
203
204                                 if(n->connection)
205                                         break;
206
207                                 bool found = false;
208
209                                 for list_each(outgoing_t, outgoing, mesh->outgoings) {
210                                         if(!strcmp(outgoing->name, n->name)) {
211                                                 found = true;
212                                                 break;
213                                         }
214                                 }
215
216                                 if(!found) {
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);
222                                 }
223                                 break;
224                         }
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.
229                         */
230                         int r = rand() % nc;
231                         int i = 0;
232
233                         for list_each(connection_t, c, mesh->connections) {
234                                 if(!c->status.active)
235                                         continue;
236
237                                 if(i++ != r)
238                                         continue;
239
240                                 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
241                                         break;
242
243                                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
244                                 list_delete(mesh->outgoings, c->outgoing);
245                                 c->outgoing = NULL;
246                                 terminate_connection(c, c->status.active);
247                                 break;
248                         }
249                 }
250
251                 if(nc >= autoconnect) {
252                         /* If we have enough active connections,
253                            remove any pending outgoing connections.
254                         */
255                         for list_each(outgoing_t, o, mesh->outgoings) {
256                                 bool found = false;
257                                 for list_each(connection_t, c, mesh->connections) {
258                                         if(c->outgoing == o) {
259                                                 found = true;
260                                                 break;
261                                         }
262                                 }
263                                 if(!found) {
264                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
265                                         list_delete_node(mesh->outgoings, node);
266                                 }
267                         }
268                 }
269         }
270
271         timeout_set(data, &(struct timeval){5, rand() % 100000});
272 }
273
274 void handle_meta_connection_data(connection_t *c) {
275         if (!receive_meta(c)) {
276                 terminate_connection(c, c->status.active);
277                 return;
278         }
279 }
280
281 int reload_configuration(void) {
282         char *fname = NULL;
283
284         /* Reread our own configuration file */
285
286         exit_configuration(&mesh->config);
287         init_configuration(&mesh->config);
288
289         if(!read_server_config()) {
290                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file.");
291                 return EINVAL;
292         }
293
294         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, mesh->self->name);
295         read_config_file(mesh->config, fname);
296         free(fname);
297
298         /* Parse some options that are allowed to be changed while tinc is running */
299
300         setup_myself_reloadable();
301
302         /* Try to make outgoing connections */
303
304         try_outgoing_connections();
305
306         /* Close connections to hosts that have a changed or deleted host config file */
307
308         for list_each(connection_t, c, mesh->connections) {
309                 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
310                 struct stat s;
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);
314                 }
315                 free(fname);
316         }
317
318         last_config_check = now.tv_sec;
319
320         return 0;
321 }
322
323 void retry(void) {
324         /* Reset the reconnection timers for all outgoing connections */
325         for list_each(outgoing_t, outgoing, mesh->outgoings) {
326                 outgoing->timeout = 0;
327                 if(outgoing->ev.cb)
328                         timeout_set(&outgoing->ev, &(struct timeval){0, 0});
329         }
330
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;
335         }
336
337         /* Kick the ping timeout handler */
338         timeout_set(&pingtimer, &(struct timeval){0, 0});
339 }
340
341 /*
342   this is where it all happens...
343 */
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});
347
348         if(!event_loop()) {
349                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
350                 return 1;
351         }
352
353         timeout_del(&periodictimer);
354         timeout_del(&pingtimer);
355
356         return 0;
357 }