]> git.meshlink.io Git - meshlink/blob - src/net.c
7ced1d15ffe688b936c89eecc1db5415af7525f5
[meshlink] / src / net.c
1 /*
2     net.c -- most of the network code
3     Copyright (C) 2014-2017 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 #include <assert.h>
35
36 #if !defined(min)
37 static inline int min(int a, int b) {
38         return a < b ? a : b;
39 }
40 #endif
41
42 /*
43   Terminate a connection:
44   - Mark it as inactive
45   - Remove the edge representing this connection
46   - Kill it with fire
47   - Check if we need to retry making an outgoing connection
48 */
49 void terminate_connection(meshlink_handle_t *mesh, connection_t *c, bool report) {
50         logger(mesh, MESHLINK_INFO, "Closing connection with %s", c->name);
51
52         c->status.active = false;
53
54         if(c->node && c->node->connection == c) {
55                 c->node->connection = NULL;
56         }
57
58         if(c->edge) {
59                 if(report) {
60                         send_del_edge(mesh, mesh->everyone, c->edge, 0);
61                 }
62
63                 edge_del(mesh, c->edge);
64                 c->edge = NULL;
65
66                 /* Run MST and SSSP algorithms */
67
68                 graph(mesh);
69
70                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
71
72                 if(report && c->node && !c->node->status.reachable) {
73                         edge_t *e;
74                         e = lookup_edge(c->node, mesh->self);
75
76                         if(e) {
77                                 send_del_edge(mesh, mesh->everyone, e, 0);
78                                 edge_del(mesh, e);
79                         }
80                 }
81         }
82
83         outgoing_t *outgoing = c->outgoing;
84         connection_del(mesh, c);
85
86         /* Check if this was our outgoing connection */
87
88         if(outgoing) {
89                 do_outgoing_connection(mesh, outgoing);
90         }
91
92 #ifndef HAVE_MINGW
93         /* Clean up dead proxy processes */
94
95         while(waitpid(-1, NULL, WNOHANG) > 0);
96
97 #endif
98 }
99
100 /*
101   Check if the other end is active.
102   If we have sent packets, but didn't receive any,
103   then possibly the other end is dead. We send a
104   PING request over the meta connection. If the other
105   end does not reply in time, we consider them dead
106   and close the connection.
107 */
108 static void timeout_handler(event_loop_t *loop, void *data) {
109         meshlink_handle_t *mesh = loop->data;
110         logger(mesh, MESHLINK_DEBUG, "timeout_handler()");
111
112         for list_each(connection_t, c, mesh->connections) {
113                 // Also make sure that if outstanding key requests for the UDP counterpart of a connection has timed out, we restart it.
114                 if(c->node) {
115                         if(c->node->status.waitingforkey && c->node->last_req_key + mesh->pingtimeout <= mesh->loop.now.tv_sec) {
116                                 send_req_key(mesh, c->node);
117                         }
118                 }
119
120                 if(c->last_ping_time + mesh->pingtimeout <= mesh->loop.now.tv_sec) {
121                         if(c->status.active) {
122                                 if(c->status.pinged) {
123                                         logger(mesh, MESHLINK_INFO, "%s didn't respond to PING in %ld seconds", c->name, (long)mesh->loop.now.tv_sec - c->last_ping_time);
124                                 } else if(c->last_ping_time + mesh->pinginterval <= mesh->loop.now.tv_sec) {
125                                         send_ping(mesh, c);
126                                         continue;
127                                 } else {
128                                         continue;
129                                 }
130                         } else {
131                                 if(c->status.connecting) {
132                                         logger(mesh, MESHLINK_WARNING, "Timeout while connecting to %s", c->name);
133                                 } else {
134                                         logger(mesh, MESHLINK_WARNING, "Timeout from %s during authentication", c->name);
135                                 }
136                         }
137
138                         terminate_connection(mesh, c, c->status.active);
139                 }
140         }
141
142         timeout_set(&mesh->loop, data, &(struct timeval) {
143                 mesh->pingtimeout, rand() % 100000
144         });
145 }
146
147 // devclass asc, last_successfull_connection desc
148 static int node_compare_devclass_asc_lsc_desc(const void *a, const void *b) {
149         const node_t *na = a, *nb = b;
150
151         if(na->devclass < nb->devclass) {
152                 return -1;
153         }
154
155         if(na->devclass > nb->devclass) {
156                 return 1;
157         }
158
159         if(na->last_successfull_connection == nb->last_successfull_connection) {
160                 return 0;
161         }
162
163         if(na->last_successfull_connection == 0 || na->last_successfull_connection > nb->last_successfull_connection) {
164                 return -1;
165         }
166
167         if(nb->last_successfull_connection == 0 || na->last_successfull_connection < nb->last_successfull_connection) {
168                 return 1;
169         }
170
171         if(na < nb) {
172                 return -1;
173         }
174
175         if(na > nb) {
176                 return 1;
177         }
178
179         return 0;
180 }
181
182 // last_successfull_connection desc
183 static int node_compare_lsc_desc(const void *a, const void *b) {
184         const node_t *na = a, *nb = b;
185
186         if(na->last_successfull_connection == nb->last_successfull_connection) {
187                 return 0;
188         }
189
190         if(na->last_successfull_connection == 0 || na->last_successfull_connection > nb->last_successfull_connection) {
191                 return -1;
192         }
193
194         if(nb->last_successfull_connection == 0 || na->last_successfull_connection < nb->last_successfull_connection) {
195                 return 1;
196         }
197
198         if(na < nb) {
199                 return -1;
200         }
201
202         if(na > nb) {
203                 return 1;
204         }
205
206         return 0;
207 }
208
209 // devclass desc
210 static int node_compare_devclass_desc(const void *a, const void *b) {
211         const node_t *na = a, *nb = b;
212
213         if(na->devclass < nb->devclass) {
214                 return -1;
215         }
216
217         if(na->devclass > nb->devclass) {
218                 return 1;
219         }
220
221         if(na < nb) {
222                 return -1;
223         }
224
225         if(na > nb) {
226                 return 1;
227         }
228
229         return 0;
230 }
231
232
233 /*
234
235 autoconnect()
236 {
237         timeout = 5
238
239         // find the best one for initial connect
240
241         if cur < min
242                 newcon =
243                         first from nodes
244                                 where dclass <= my.dclass and !connection and (timestamp - last_retry) > retry_timeout
245                                 order by dclass asc, last_connection desc
246                 if newcon
247                         timeout = 0
248                         goto connect
249
250
251         // find better nodes to connect to: in case we have less than min connections within [BACKBONE, i] and there are nodes which we are not connected to within the range
252
253         if min <= cur < max
254                 j = 0
255                 for i = BACKBONE to my.dclass
256                         j += count(from connections where node.dclass = i)
257                         if j < min
258                                 newcon =
259                                         first from nodes
260                                                 where dclass = i and !connection and (timestamp - last_retry) > retry_timeout
261                                                 order by last_connection desc
262                                 if newcon
263                                         goto connect
264                         else
265                                 break
266
267
268         // heal partitions
269
270         if min <= cur < max
271                 newcon =
272                         first from nodes
273                                 where dclass <= my.dclass and !reachable and (timestamp - last_retry) > retry_timeout
274                                 order by dclass asc, last_connection desc
275                 if newcon
276                         goto connect
277
278
279         // connect
280
281 connect:
282         if newcon
283                 connect newcon
284
285
286         // disconnect outgoing connections in case we have more than min connections within [BACKBONE, i] and there are nodes which we are connected to within the range [i, PORTABLE]
287
288         if min < cur <= max
289                 j = 0
290                 for i = BACKBONE to my.dclass
291                         j += count(from connections where node.dclass = i)
292                         if min < j
293                                 delcon =
294                                         first from nodes
295                                                 where dclass >= i and outgoing_connection
296                                                 order by dclass desc
297                                 if disconnect
298                                         goto disconnect
299                                 else
300                                         break
301
302
303         // disconnect connections in case we have more than enough connections
304
305         if max < cur
306                 delcon =
307                         first from nodes
308                                 where outgoing_connection
309                                 order by dclass desc
310                 goto disconnect
311
312         // disconnect
313
314 disconnect
315         if delcon
316                 disconnect delcon
317
318
319         // next iteration
320         next (timeout, autoconnect)
321
322 }
323
324 */
325
326
327 static void periodic_handler(event_loop_t *loop, void *data) {
328         meshlink_handle_t *mesh = loop->data;
329
330         /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
331            This usually only happens when another node has the same Name as this node.
332            If so, sleep for a short while to prevent a storm of contradicting messages.
333         */
334
335         if(mesh->contradicting_del_edge > 100 && mesh->contradicting_add_edge > 100) {
336                 logger(mesh, MESHLINK_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", mesh->sleeptime);
337                 usleep(mesh->sleeptime * 1000000LL);
338                 mesh->sleeptime *= 2;
339
340                 if(mesh->sleeptime < 0) {
341                         mesh->sleeptime = 3600;
342                 }
343         } else {
344                 mesh->sleeptime /= 2;
345
346                 if(mesh->sleeptime < 10) {
347                         mesh->sleeptime = 10;
348                 }
349         }
350
351         mesh->contradicting_add_edge = 0;
352         mesh->contradicting_del_edge = 0;
353
354         int timeout = 5;
355
356         /* Check if we need to make or break connections. */
357
358         if(mesh->nodes->count > 1) {
359
360                 logger(mesh, MESHLINK_DEBUG, "--- autoconnect begin ---");
361
362                 int retry_timeout = min(mesh->nodes->count * 5, 60);
363
364                 logger(mesh, MESHLINK_DEBUG, "* devclass = %d", mesh->devclass);
365                 logger(mesh, MESHLINK_DEBUG, "* nodes = %d", mesh->nodes->count);
366                 logger(mesh, MESHLINK_DEBUG, "* retry_timeout = %d", retry_timeout);
367
368
369                 // connect disconnect nodes
370
371                 node_t *connect_to = NULL;
372                 node_t *disconnect_from = NULL;
373
374
375                 // get cur_connects
376
377                 unsigned int cur_connects = 0;
378
379                 for list_each(connection_t, c, mesh->connections) {
380                         if(c->status.active) {
381                                 cur_connects += 1;
382                         }
383                 }
384
385                 logger(mesh, MESHLINK_DEBUG, "* cur_connects = %d", cur_connects);
386                 logger(mesh, MESHLINK_DEBUG, "* outgoings = %d", mesh->outgoings->count);
387
388                 // get min_connects and max_connects
389
390                 unsigned int min_connects = dev_class_traits[mesh->devclass].min_connects;
391                 unsigned int max_connects = dev_class_traits[mesh->devclass].max_connects;
392
393                 logger(mesh, MESHLINK_DEBUG, "* min_connects = %d", min_connects);
394                 logger(mesh, MESHLINK_DEBUG, "* max_connects = %d", max_connects);
395
396                 // find the best one for initial connect
397
398                 if(cur_connects < min_connects) {
399                         splay_tree_t *nodes = splay_alloc_tree(node_compare_devclass_asc_lsc_desc, NULL);
400
401                         for splay_each(node_t, n, mesh->nodes) {
402                                 logger(mesh, MESHLINK_DEBUG, "* %s->devclass = %d", n->name, n->devclass);
403
404                                 if(n != mesh->self && n->devclass <= mesh->devclass && !n->connection && !n->status.blacklisted && (n->last_connect_try == 0 || (mesh->loop.now.tv_sec - n->last_connect_try) > retry_timeout)) {
405                                         splay_insert(nodes, n);
406                                 }
407                         }
408
409                         if(nodes->head) {
410                                 //timeout = 0;
411                                 connect_to = (node_t *)nodes->head->data;
412
413                                 logger(mesh, MESHLINK_DEBUG, "* found best one for initial connect: %s", connect_to->name);
414                         } else {
415                                 logger(mesh, MESHLINK_DEBUG, "* could not find node for initial connect");
416                         }
417
418                         splay_free_tree(nodes);
419                 }
420
421
422                 // find better nodes to connect to
423
424                 if(!connect_to && min_connects <= cur_connects && cur_connects < max_connects) {
425                         unsigned int connects = 0;
426
427                         for(dev_class_t devclass = 0; devclass <= mesh->devclass; ++devclass) {
428                                 for list_each(connection_t, c, mesh->connections) {
429                                         if(c->status.active && c->node && c->node->devclass == devclass) {
430                                                 connects += 1;
431                                         }
432                                 }
433
434                                 if(connects < min_connects) {
435                                         splay_tree_t *nodes = splay_alloc_tree(node_compare_lsc_desc, NULL);
436
437                                         for splay_each(node_t, n, mesh->nodes) {
438                                                 if(n != mesh->self && n->devclass == devclass && !n->connection && !n->status.blacklisted && (n->last_connect_try == 0 || (mesh->loop.now.tv_sec - n->last_connect_try) > retry_timeout)) {
439                                                         splay_insert(nodes, n);
440                                                 }
441                                         }
442
443                                         if(nodes->head) {
444                                                 logger(mesh, MESHLINK_DEBUG, "* found better node");
445                                                 connect_to = (node_t *)nodes->head->data;
446
447                                                 splay_free_tree(nodes);
448                                                 break;
449                                         }
450
451                                         splay_free_tree(nodes);
452                                 } else {
453                                         break;
454                                 }
455                         }
456
457                         if(!connect_to) {
458                                 logger(mesh, MESHLINK_DEBUG, "* could not find better nodes");
459                         }
460                 }
461
462
463                 // heal partitions
464
465                 if(!connect_to && min_connects <= cur_connects && cur_connects < max_connects) {
466                         splay_tree_t *nodes = splay_alloc_tree(node_compare_devclass_asc_lsc_desc, NULL);
467
468                         for splay_each(node_t, n, mesh->nodes) {
469                                 if(n != mesh->self && n->devclass <= mesh->devclass && !n->status.reachable && !n->status.blacklisted && (n->last_connect_try == 0 || (mesh->loop.now.tv_sec - n->last_connect_try) > retry_timeout)) {
470                                         splay_insert(nodes, n);
471                                 }
472                         }
473
474                         if(nodes->head) {
475                                 logger(mesh, MESHLINK_DEBUG, "* try to heal partition");
476                                 connect_to = (node_t *)nodes->head->data;
477                         } else {
478                                 logger(mesh, MESHLINK_DEBUG, "* could not find nodes for partition healing");
479                         }
480
481                         splay_free_tree(nodes);
482                 }
483
484
485                 // perform connect
486
487                 if(connect_to && !connect_to->connection) {
488                         connect_to->last_connect_try = mesh->loop.now.tv_sec;
489                         logger(mesh, MESHLINK_DEBUG, "Autoconnect trying to connect to %s", connect_to->name);
490
491                         /* check if there is already a connection attempt to this node */
492                         bool skip = false;
493
494                         for list_each(outgoing_t, outgoing, mesh->outgoings) {
495                                 if(outgoing->node == connect_to) {
496                                         logger(mesh, MESHLINK_DEBUG, "* skip autoconnect since it is an outgoing connection already");
497                                         skip = true;
498                                         break;
499                                 }
500                         }
501
502                         if(!connect_to->status.reachable && !node_read_public_key(mesh, connect_to)) {
503                                 logger(mesh, MESHLINK_DEBUG, "* skip autoconnect since we don't know this node's public key");
504                                 skip = true;
505                         }
506
507                         if(!skip) {
508                                 logger(mesh, MESHLINK_DEBUG, "Autoconnecting to %s", connect_to->name);
509                                 outgoing_t *outgoing = xzalloc(sizeof(outgoing_t));
510                                 outgoing->node = connect_to;
511                                 list_insert_tail(mesh->outgoings, outgoing);
512                                 setup_outgoing_connection(mesh, outgoing);
513                         }
514                 }
515
516
517                 // disconnect suboptimal outgoing connections
518
519                 if(min_connects < cur_connects /*&& cur_connects <= max_connects*/) {
520                         unsigned int connects = 0;
521
522                         for(dev_class_t devclass = 0; devclass <= mesh->devclass; ++devclass) {
523                                 for list_each(connection_t, c, mesh->connections) {
524                                         if(c->status.active && c->node && c->node->devclass == devclass) {
525                                                 connects += 1;
526                                         }
527                                 }
528
529                                 if(min_connects < connects) {
530                                         splay_tree_t *nodes = splay_alloc_tree(node_compare_devclass_desc, NULL);
531
532                                         for list_each(connection_t, c, mesh->connections) {
533                                                 if(c->outgoing && c->node && c->node->devclass >= devclass) {
534                                                         splay_insert(nodes, c->node);
535                                                 }
536                                         }
537
538                                         if(nodes->head) {
539                                                 logger(mesh, MESHLINK_DEBUG, "* disconnect suboptimal outgoing connection");
540                                                 disconnect_from = (node_t *)nodes->head->data;
541                                         }
542
543                                         splay_free_tree(nodes);
544                                         break;
545                                 }
546                         }
547
548                         if(!disconnect_from) {
549                                 logger(mesh, MESHLINK_DEBUG, "* no suboptimal outgoing connections");
550                         }
551                 }
552
553
554                 // disconnect connections (too many connections)
555
556                 if(!disconnect_from && max_connects < cur_connects) {
557                         splay_tree_t *nodes = splay_alloc_tree(node_compare_devclass_desc, NULL);
558
559                         for list_each(connection_t, c, mesh->connections) {
560                                 if(c->status.active && c->node) {
561                                         splay_insert(nodes, c->node);
562                                 }
563                         }
564
565                         if(nodes->head) {
566                                 logger(mesh, MESHLINK_DEBUG, "* disconnect connection (too many connections)");
567
568                                 //timeout = 0;
569                                 disconnect_from = (node_t *)nodes->head->data;
570                         } else {
571                                 logger(mesh, MESHLINK_DEBUG, "* no node we want to disconnect, even though we have too many connections");
572                         }
573
574                         splay_free_tree(nodes);
575                 }
576
577
578                 // perform disconnect
579
580                 if(disconnect_from && disconnect_from->connection) {
581                         logger(mesh, MESHLINK_DEBUG, "Autodisconnecting from %s", disconnect_from->connection->name);
582                         list_delete(mesh->outgoings, disconnect_from->connection->outgoing);
583                         disconnect_from->connection->outgoing = NULL;
584                         terminate_connection(mesh, disconnect_from->connection, disconnect_from->connection->status.active);
585                 }
586
587                 // reduce timeout if we don't have enough connections + outgoings
588                 if(cur_connects + mesh->outgoings->count < 3) {
589                         timeout = 1;
590                 }
591
592                 // done!
593
594                 logger(mesh, MESHLINK_DEBUG, "--- autoconnect end ---");
595         }
596
597         for splay_each(node_t, n, mesh->nodes) {
598                 if(n->status.dirty) {
599                         node_write_config(mesh, n);
600                         n->status.dirty = false;
601                 }
602         }
603
604         timeout_set(&mesh->loop, data, &(struct timeval) {
605                 timeout, rand() % 100000
606         });
607 }
608
609 void handle_meta_connection_data(meshlink_handle_t *mesh, connection_t *c) {
610         if(!receive_meta(mesh, c)) {
611                 terminate_connection(mesh, c, c->status.active);
612                 return;
613         }
614 }
615
616 void retry(meshlink_handle_t *mesh) {
617         /* Reset the reconnection timers for all outgoing connections */
618         for list_each(outgoing_t, outgoing, mesh->outgoings) {
619                 outgoing->timeout = 0;
620
621                 if(outgoing->ev.cb)
622                         timeout_set(&mesh->loop, &outgoing->ev, &(struct timeval) {
623                         0, 0
624                 });
625         }
626
627 #ifdef HAVE_IFADDRS_H
628         struct ifaddrs *ifa = NULL;
629         getifaddrs(&ifa);
630 #endif
631
632         /* For active connections, check if their addresses are still valid.
633          * If yes, reset their ping timers, otherwise terminate them. */
634         for list_each(connection_t, c, mesh->connections) {
635                 if(!c->status.active) {
636                         continue;
637                 }
638
639                 if(!c->status.pinged) {
640                         c->last_ping_time = 0;
641                 }
642
643 #ifdef HAVE_IFADDRS_H
644
645                 if(!ifa) {
646                         continue;
647                 }
648
649                 sockaddr_t sa;
650                 socklen_t salen = sizeof(sa);
651
652                 if(getsockname(c->socket, &sa.sa, &salen)) {
653                         continue;
654                 }
655
656                 bool found = false;
657
658                 for(struct ifaddrs *ifap = ifa; ifap; ifap = ifap->ifa_next) {
659                         if(ifap->ifa_addr && !sockaddrcmp_noport(&sa, (sockaddr_t *)ifap->ifa_addr)) {
660                                 found = true;
661                                 break;
662                         }
663
664                 }
665
666                 if(!found) {
667                         logger(mesh, MESHLINK_DEBUG, "Local address for connection to %s no longer valid, terminating", c->name);
668                         terminate_connection(mesh, c, c->status.active);
669                 }
670
671 #endif
672         }
673
674 #ifdef HAVE_IFADDRS_H
675
676         if(ifa) {
677                 freeifaddrs(ifa);
678         }
679
680 #endif
681
682         /* Kick the ping timeout handler */
683         timeout_set(&mesh->loop, &mesh->pingtimer, &(struct timeval) {
684                 0, 0
685         });
686 }
687
688 /*
689   this is where it all happens...
690 */
691 int main_loop(meshlink_handle_t *mesh) {
692         timeout_add(&mesh->loop, &mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timeval) {
693                 mesh->pingtimeout, rand() % 100000
694         });
695         timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval) {
696                 0, 0
697         });
698
699         //Add signal handler
700         mesh->datafromapp.signum = 0;
701         signal_add(&(mesh->loop), &(mesh->datafromapp), (signal_cb_t)meshlink_send_from_queue, mesh, mesh->datafromapp.signum);
702
703         if(!event_loop_run(&(mesh->loop), &(mesh->mesh_mutex))) {
704                 logger(mesh, MESHLINK_ERROR, "Error while waiting for input: %s", strerror(errno));
705                 return 1;
706         }
707
708         timeout_del(&mesh->loop, &mesh->periodictimer);
709         timeout_del(&mesh->loop, &mesh->pingtimer);
710
711         return 0;
712 }