]> git.meshlink.io Git - meshlink/blob - src/net.c
Remove files not used by MeshLink.
[meshlink] / src / net.c
1 /*
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>
7
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.
12
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.
17
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.
21 */
22
23 #include "system.h"
24
25 #include "utils.h"
26 #include "conf.h"
27 #include "connection.h"
28 #include "graph.h"
29 #include "logger.h"
30 #include "meta.h"
31 #include "names.h"
32 #include "net.h"
33 #include "netutl.h"
34 #include "protocol.h"
35 #include "subnet.h"
36 #include "xalloc.h"
37
38 int contradicting_add_edge = 0;
39 int contradicting_del_edge = 0;
40 static int sleeptime = 10;
41 time_t last_config_check = 0;
42 static timeout_t pingtimer;
43 static timeout_t periodictimer;
44
45 /* Purge edges and subnets of unreachable nodes. Use carefully. */
46
47 void purge(void) {
48         logger(DEBUG_PROTOCOL, LOG_DEBUG, "Purging unreachable nodes");
49
50         /* Remove all edges and subnets owned by unreachable nodes. */
51
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);
55
56                         for splay_each(subnet_t, s, n->subnet_tree) {
57                                 send_del_subnet(everyone, s);
58                                 if(!strictsubnets)
59                                         subnet_del(n, s);
60                         }
61
62                         for splay_each(edge_t, e, n->edge_tree) {
63                                 if(!tunnelserver)
64                                         send_del_edge(everyone, e);
65                                 edge_del(e);
66                         }
67                 }
68         }
69
70         /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
71
72         for splay_each(node_t, n, node_tree) {
73                 if(!n->status.reachable) {
74                         for splay_each(edge_t, e, edge_weight_tree)
75                                 if(e->to == n)
76                                         return;
77
78                         if(!autoconnect && (!strictsubnets || !n->subnet_tree->head))
79                                 /* in strictsubnets mode do not delete nodes with subnets */
80                                 node_del(n);
81                 }
82         }
83 }
84
85 /*
86   Terminate a connection:
87   - Mark it as inactive
88   - Remove the edge representing this connection
89   - Kill it with fire
90   - Check if we need to retry making an outgoing connection
91 */
92 void terminate_connection(connection_t *c, bool report) {
93         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname);
94
95         c->status.active = false;
96
97         if(c->node && c->node->connection == c)
98                 c->node->connection = NULL;
99
100         if(c->edge) {
101                 if(report && !tunnelserver)
102                         send_del_edge(everyone, c->edge);
103
104                 edge_del(c->edge);
105                 c->edge = NULL;
106
107                 /* Run MST and SSSP algorithms */
108
109                 graph();
110
111                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
112
113                 if(report && !c->node->status.reachable) {
114                         edge_t *e;
115                         e = lookup_edge(c->node, myself);
116                         if(e) {
117                                 if(!tunnelserver)
118                                         send_del_edge(everyone, e);
119                                 edge_del(e);
120                         }
121                 }
122         }
123
124         outgoing_t *outgoing = c->outgoing;
125         connection_del(c);
126
127         /* Check if this was our outgoing connection */
128
129         if(outgoing)
130                 do_outgoing_connection(outgoing);
131
132 #ifndef HAVE_MINGW
133         /* Clean up dead proxy processes */
134
135         while(waitpid(-1, NULL, WNOHANG) > 0);
136 #endif
137 }
138
139 /*
140   Check if the other end is active.
141   If we have sent packets, but didn't receive any,
142   then possibly the other end is dead. We send a
143   PING request over the meta connection. If the other
144   end does not reply in time, we consider them dead
145   and close the connection.
146 */
147 static void timeout_handler(void *data) {
148         for list_each(connection_t, c, connection_list) {
149                 if(c->status.control)
150                         continue;
151
152                 if(c->last_ping_time + pingtimeout <= now.tv_sec) {
153                         if(c->status.active) {
154                                 if(c->status.pinged) {
155                                         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);
156                                 } else if(c->last_ping_time + pinginterval <= now.tv_sec) {
157                                         send_ping(c);
158                                         continue;
159                                 } else {
160                                         continue;
161                                 }
162                         } else {
163                                 if(c->status.connecting)
164                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
165                                 else
166                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
167                         }
168                         terminate_connection(c, c->status.active);
169                 }
170         }
171
172         timeout_set(data, &(struct timeval){pingtimeout, rand() % 100000});
173 }
174
175 static void periodic_handler(void *data) {
176         /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
177            This usually only happens when another node has the same Name as this node.
178            If so, sleep for a short while to prevent a storm of contradicting messages.
179         */
180
181         if(contradicting_del_edge > 100 && contradicting_add_edge > 100) {
182                 logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime);
183                 usleep(sleeptime * 1000000LL);
184                 sleeptime *= 2;
185                 if(sleeptime < 0)
186                         sleeptime = 3600;
187         } else {
188                 sleeptime /= 2;
189                 if(sleeptime < 10)
190                         sleeptime = 10;
191         }
192
193         contradicting_add_edge = 0;
194         contradicting_del_edge = 0;
195
196         /* If AutoConnect is set, check if we need to make or break connections. */
197
198         if(autoconnect && node_tree->count > 1) {
199                 /* Count number of active connections */
200                 int nc = 0;
201                 for list_each(connection_t, c, connection_list) {
202                         if(c->status.active && !c->status.control)
203                                 nc++;
204                 }
205
206                 if(nc < autoconnect) {
207                         /* Not enough active connections, try to add one.
208                            Choose a random node, if we don't have a connection to it,
209                            and we are not already trying to make one, create an
210                            outgoing connection to this node.
211                         */
212                         int r = rand() % node_tree->count;
213                         int i = 0;
214
215                         for splay_each(node_t, n, node_tree) {
216                                 if(i++ != r)
217                                         continue;
218
219                                 if(n->connection)
220                                         break;
221
222                                 bool found = false;
223
224                                 for list_each(outgoing_t, outgoing, outgoing_list) {
225                                         if(!strcmp(outgoing->name, n->name)) {
226                                                 found = true;
227                                                 break;
228                                         }
229                                 }
230
231                                 if(!found) {
232                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
233                                         outgoing_t *outgoing = xzalloc(sizeof *outgoing);
234                                         outgoing->name = xstrdup(n->name);
235                                         list_insert_tail(outgoing_list, outgoing);
236                                         setup_outgoing_connection(outgoing);
237                                 }
238                                 break;
239                         }
240                 } else if(nc > autoconnect) {
241                         /* Too many active connections, try to remove one.
242                            Choose a random outgoing connection to a node
243                            that has at least one other connection.
244                         */
245                         int r = rand() % nc;
246                         int i = 0;
247
248                         for list_each(connection_t, c, connection_list) {
249                                 if(!c->status.active || c->status.control)
250                                         continue;
251
252                                 if(i++ != r)
253                                         continue;
254
255                                 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
256                                         break;
257
258                                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
259                                 list_delete(outgoing_list, c->outgoing);
260                                 c->outgoing = NULL;
261                                 terminate_connection(c, c->status.active);
262                                 break;
263                         }
264                 }
265
266                 if(nc >= autoconnect) {
267                         /* If we have enough active connections,
268                            remove any pending outgoing connections.
269                         */
270                         for list_each(outgoing_t, o, outgoing_list) {
271                                 bool found = false;
272                                 for list_each(connection_t, c, connection_list) {
273                                         if(c->outgoing == o) {
274                                                 found = true;
275                                                 break;
276                                         }
277                                 }
278                                 if(!found) {
279                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
280                                         list_delete_node(outgoing_list, node);
281                                 }
282                         }
283                 }
284         }
285
286         timeout_set(data, &(struct timeval){5, rand() % 100000});
287 }
288
289 void handle_meta_connection_data(connection_t *c) {
290         if (!receive_meta(c)) {
291                 terminate_connection(c, c->status.active);
292                 return;
293         }
294 }
295
296 #ifndef HAVE_MINGW
297 static void sigterm_handler(void *data) {
298         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
299         event_exit();
300 }
301
302 static void sighup_handler(void *data) {
303         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
304         reopenlogger();
305         if(reload_configuration())
306                 exit(1);
307 }
308
309 static void sigalrm_handler(void *data) {
310         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
311         retry();
312 }
313 #endif
314
315 int reload_configuration(void) {
316         char *fname = NULL;
317
318         /* Reread our own configuration file */
319
320         exit_configuration(&config_tree);
321         init_configuration(&config_tree);
322
323         if(!read_server_config()) {
324                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file.");
325                 return EINVAL;
326         }
327
328         read_config_options(config_tree, NULL);
329
330         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, myself->name);
331         read_config_file(config_tree, fname);
332         free(fname);
333
334         /* Parse some options that are allowed to be changed while tinc is running */
335
336         setup_myself_reloadable();
337
338         /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
339
340         if(strictsubnets) {
341                 for splay_each(subnet_t, subnet, subnet_tree)
342                         subnet->expires = 1;
343
344                 load_all_subnets();
345
346                 for splay_each(subnet_t, subnet, subnet_tree) {
347                         if(subnet->expires == 1) {
348                                 send_del_subnet(everyone, subnet);
349                                 if(subnet->owner->status.reachable)
350                                         subnet_update(subnet->owner, subnet, false);
351                                 subnet_del(subnet->owner, subnet);
352                         } else if(subnet->expires == -1) {
353                                 subnet->expires = 0;
354                         } else {
355                                 send_add_subnet(everyone, subnet);
356                                 if(subnet->owner->status.reachable)
357                                         subnet_update(subnet->owner, subnet, true);
358                         }
359                 }
360         } else { /* Only read our own subnets back in */
361                 for splay_each(subnet_t, subnet, myself->subnet_tree)
362                         if(!subnet->expires)
363                                 subnet->expires = 1;
364
365                 config_t *cfg = lookup_config(config_tree, "Subnet");
366
367                 while(cfg) {
368                         subnet_t *subnet, *s2;
369
370                         if(!get_config_subnet(cfg, &subnet))
371                                 continue;
372
373                         if((s2 = lookup_subnet(myself, subnet))) {
374                                 if(s2->expires == 1)
375                                         s2->expires = 0;
376
377                                 free_subnet(subnet);
378                         } else {
379                                 subnet_add(myself, subnet);
380                                 send_add_subnet(everyone, subnet);
381                                 subnet_update(myself, subnet, true);
382                         }
383
384                         cfg = lookup_config_next(config_tree, cfg);
385                 }
386
387                 for splay_each(subnet_t, subnet, myself->subnet_tree) {
388                         if(subnet->expires == 1) {
389                                 send_del_subnet(everyone, subnet);
390                                 subnet_update(myself, subnet, false);
391                                 subnet_del(myself, subnet);
392                         }
393                 }
394         }
395
396         /* Try to make outgoing connections */
397
398         try_outgoing_connections();
399
400         /* Close connections to hosts that have a changed or deleted host config file */
401
402         for list_each(connection_t, c, connection_list) {
403                 if(c->status.control)
404                         continue;
405
406                 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
407                 struct stat s;
408                 if(stat(fname, &s) || s.st_mtime > last_config_check) {
409                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name);
410                         terminate_connection(c, c->status.active);
411                 }
412                 free(fname);
413         }
414
415         last_config_check = now.tv_sec;
416
417         return 0;
418 }
419
420 void retry(void) {
421         /* Reset the reconnection timers for all outgoing connections */
422         for list_each(outgoing_t, outgoing, outgoing_list) {
423                 outgoing->timeout = 0;
424                 if(outgoing->ev.cb)
425                         timeout_set(&outgoing->ev, &(struct timeval){0, 0});
426         }
427
428         /* Check for outgoing connections that are in progress, and reset their ping timers */
429         for list_each(connection_t, c, connection_list) {
430                 if(c->outgoing && !c->node)
431                         c->last_ping_time = 0;
432         }
433
434         /* Kick the ping timeout handler */
435         timeout_set(&pingtimer, &(struct timeval){0, 0});
436 }
437
438 /*
439   this is where it all happens...
440 */
441 int main_loop(void) {
442         timeout_add(&pingtimer, timeout_handler, &pingtimer, &(struct timeval){pingtimeout, rand() % 100000});
443         timeout_add(&periodictimer, periodic_handler, &periodictimer, &(struct timeval){pingtimeout, rand() % 100000});
444
445 #ifndef HAVE_MINGW
446         signal_t sighup = {0};
447         signal_t sigterm = {0};
448         signal_t sigquit = {0};
449         signal_t sigint = {0};
450         signal_t sigalrm = {0};
451
452         signal_add(&sighup, sighup_handler, &sighup, SIGHUP);
453         signal_add(&sigterm, sigterm_handler, &sigterm, SIGTERM);
454         signal_add(&sigquit, sigterm_handler, &sigquit, SIGQUIT);
455         signal_add(&sigint, sigterm_handler, &sigint, SIGINT);
456         signal_add(&sigalrm, sigalrm_handler, &sigalrm, SIGALRM);
457 #endif
458
459         if(!event_loop()) {
460                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
461                 return 1;
462         }
463
464 #ifndef HAVE_MINGW
465         signal_del(&sighup);
466         signal_del(&sigterm);
467         signal_del(&sigquit);
468         signal_del(&sigint);
469         signal_del(&sigalrm);
470 #endif
471
472         timeout_del(&periodictimer);
473         timeout_del(&pingtimer);
474
475         return 0;
476 }