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