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