]> git.meshlink.io Git - meshlink/blob - src/net.c
"last successfull connect" introduced
[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_successfull_connection desc
135 static int node_compare_devclass_asc_lsc_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_successfull_connection == nb->last_successfull_connection)
146                 return 0;
147
148         if(na->last_successfull_connection == 0 || na->last_successfull_connection > nb->last_successfull_connection)
149                 return -1;
150
151         if(nb->last_successfull_connection == 0 || na->last_successfull_connection < nb->last_successfull_connection)
152                 return 1;
153
154         if(na < nb)
155                 return -1;
156
157         if(na > nb)
158                 return 1;
159
160         return 0;
161 }
162
163 // last_successfull_connection desc
164 static int node_compare_lsc_desc(const void *a, const void *b)
165 {
166         const node_t *na = a, *nb = b;
167
168         if(na->last_successfull_connection == nb->last_successfull_connection)
169                 return 0;
170
171         if(na->last_successfull_connection == 0 || na->last_successfull_connection > nb->last_successfull_connection)
172                 return -1;
173
174         if(nb->last_successfull_connection == 0 || na->last_successfull_connection < nb->last_successfull_connection)
175                 return 1;
176
177         if(na < nb)
178                 return -1;
179
180         if(na > nb)
181                 return 1;
182
183         return 0;
184 }
185
186 // devclass desc
187 static int node_compare_devclass_desc(const void *a, const void *b)
188 {
189         const node_t *na = a, *nb = b;
190
191         if(na->devclass < nb->devclass)
192                 { return -1; }
193
194         if(na->devclass > nb->devclass)
195                 { return 1; }
196
197         if(na < nb)
198                 return -1;
199
200         if(na > nb)
201                 return 1;
202
203         return 0;
204 }
205
206
207 /*
208
209 autoconnect()
210 {
211         timeout = 5
212
213         // find the best one for initial connect
214
215         if cur < min
216                 newcon =
217                         first from nodes
218                                 where dclass <= my.dclass and !connection and (timestamp - last_retry) > retry_timeout
219                                 order by dclass asc, last_connection desc
220                 if newcon
221                         timeout = 0
222                         goto connect
223
224
225         // 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
226
227         if min <= cur < max
228                 j = 0
229                 for i = BACKBONE to my.dclass
230                         j += count(from connections where node.dclass = i)
231                         if j < min
232                                 newcon =
233                                         first from nodes
234                                                 where dclass = i and !connection and (timestamp - last_retry) > retry_timeout
235                                                 order by last_connection desc
236                                 if newcon
237                                         goto connect
238                         else
239                                 break
240
241
242         // heal partitions
243
244         if min <= cur < max
245                 newcon =
246                         first from nodes
247                                 where dclass <= my.dclass and !reachable and (timestamp - last_retry) > retry_timeout
248                                 order by dclass asc, last_connection desc
249                 if newcon
250                         goto connect
251
252
253         // connect
254
255 connect:
256         if newcon
257                 connect newcon
258
259
260         // 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]
261
262         if min < cur <= max
263                 j = 0
264                 for i = BACKBONE to my.dclass
265                         j += count(from connections where node.dclass = i)
266                         if min < j
267                                 delcon =
268                                         first from nodes
269                                                 where dclass >= i and outgoing_connection
270                                                 order by dclass desc
271                                 if disconnect
272                                         goto disconnect
273                                 else
274                                         break
275
276
277         // disconnect connections in case we have more than enough connections
278
279         if max < cur
280                 delcon =
281                         first from nodes
282                                 where outgoing_connection
283                                 order by dclass desc
284                 goto disconnect
285
286         // disconnect
287
288 disconnect
289         if delcon
290                 disconnect delcon
291
292
293         // next iteration
294         next (timeout, autoconnect)
295
296 }
297
298 */
299
300
301 static void periodic_handler(event_loop_t *loop, void *data) {
302         meshlink_handle_t *mesh = loop->data;
303
304         /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
305            This usually only happens when another node has the same Name as this node.
306            If so, sleep for a short while to prevent a storm of contradicting messages.
307         */
308
309         if(mesh->contradicting_del_edge > 100 && mesh->contradicting_add_edge > 100) {
310                 logger(mesh, MESHLINK_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", mesh->sleeptime);
311                 usleep(mesh->sleeptime * 1000000LL);
312                 mesh->sleeptime *= 2;
313                 if(mesh->sleeptime < 0)
314                         mesh->sleeptime = 3600;
315         } else {
316                 mesh->sleeptime /= 2;
317                 if(mesh->sleeptime < 10)
318                         mesh->sleeptime = 10;
319         }
320
321         mesh->contradicting_add_edge = 0;
322         mesh->contradicting_del_edge = 0;
323
324         int timeout = 5;
325
326         /* Check if we need to make or break connections. */
327
328         if(mesh->nodes->count > 1) {
329
330                 logger(mesh, MESHLINK_INFO, "--- autoconnect begin ---");
331
332                 int retry_timeout = min(mesh->nodes->count * 5, 60);
333
334                 logger(mesh, MESHLINK_INFO, "* devclass = %d", mesh->devclass);
335                 logger(mesh, MESHLINK_INFO, "* nodes = %d", mesh->nodes->count);
336                 logger(mesh, MESHLINK_INFO, "* retry_timeout = %d", retry_timeout);
337
338
339                 // connect disconnect nodes
340
341                 node_t* connect_to = NULL;
342                 node_t* disconnect_from = NULL;
343
344
345                 // get cur_connects
346
347                 int cur_connects = 0;
348
349                 for list_each(connection_t, c, mesh->connections)
350                 {
351                         if(!c->status.remove_unused)
352                         {
353                                 cur_connects += 1;
354                         }
355                 }
356
357                 logger(mesh, MESHLINK_INFO, "* cur_connects = %d", cur_connects);
358                 logger(mesh, MESHLINK_INFO, "* outgoings = %d", mesh->outgoings->count);
359
360                 // get min_connects and max_connects
361
362                 assert(mesh->devclass >= 0 && mesh->devclass <= _DEV_CLASS_MAX);
363
364                 int min_connects = dev_class_traits[mesh->devclass].min_connects;
365                 int max_connects = dev_class_traits[mesh->devclass].max_connects;
366
367                 logger(mesh, MESHLINK_INFO, "* min_connects = %d", min_connects);
368                 logger(mesh, MESHLINK_INFO, "* max_connects = %d", max_connects);
369
370
371                 // find the best one for initial connect
372
373                 if(cur_connects < min_connects)
374                 {
375                         splay_tree_t *nodes = splay_alloc_tree(node_compare_devclass_asc_lsc_desc, NULL);
376
377                         for splay_each(node_t, n, mesh->nodes)
378                         {
379                                 logger(mesh, MESHLINK_INFO, "* n->devclass = %d", n->devclass);
380                                 if(n != mesh->self && n->devclass <= mesh->devclass && !n->connection && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout))
381                                         { splay_insert(nodes, n); }
382                         }
383
384                         if(nodes->head)
385                         {
386                                 logger(mesh, MESHLINK_INFO, "* found best one for initial connect");
387
388                                 //timeout = 0;
389                                 connect_to = (node_t*)nodes->head->data;
390                         }
391                         else
392                                 { logger(mesh, MESHLINK_INFO, "* could not find node for initial connect"); }
393
394                         splay_free_tree(nodes);
395                 }
396
397
398                 // find better nodes to connect to
399
400                 if(!connect_to && min_connects <= cur_connects && cur_connects < max_connects)
401                 {
402                         unsigned int connects = 0;
403
404                         for(int devclass = 0; devclass <= mesh->devclass; ++devclass)
405                         {
406                                 for list_each(connection_t, c, mesh->connections)
407                                 {
408                                         if(!c->status.remove_unused && c->node && c->node->devclass == devclass)
409                                                 { connects += 1; }
410                                 }
411
412                                 if( connects < min_connects )
413                                 {
414                                         splay_tree_t *nodes = splay_alloc_tree(node_compare_lsc_desc, NULL);
415
416                                         for splay_each(node_t, n, mesh->nodes)
417                                         {
418                                                 if(n != mesh->self && n->devclass == devclass && !n->connection && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout))
419                                                         { splay_insert(nodes, n); }
420                                         }
421
422                                         if(nodes->head)
423                                         {
424                                                 logger(mesh, MESHLINK_INFO, "* found better node");
425                                                 connect_to = (node_t*)nodes->head->data;
426
427                                                 splay_free_tree(nodes);
428                                                 break;
429                                         }
430
431                                         splay_free_tree(nodes);
432                                 }
433                                 else
434                                         { break; }
435                         }
436
437                         if(!connect_to)
438                                 { logger(mesh, MESHLINK_INFO, "* could not find better nodes"); }
439                 }
440
441
442                 // heal partitions
443
444                 if(!connect_to && min_connects <= cur_connects && cur_connects < max_connects)
445                 {
446                         splay_tree_t *nodes = splay_alloc_tree(node_compare_devclass_asc_lsc_desc, NULL);
447
448                         for splay_each(node_t, n, mesh->nodes)
449                         {
450                                 if(n != mesh->self && n->devclass <= mesh->devclass && !n->status.reachable && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout))
451                                         { splay_insert(nodes, n); }
452                         }
453
454                         if(nodes->head)
455                         {
456                                 logger(mesh, MESHLINK_INFO, "* try to heal partition");
457                                 connect_to = (node_t*)nodes->head->data;
458                         }
459                         else
460                                 { logger(mesh, MESHLINK_INFO, "* could not find nodes for partition healing"); }
461
462                         splay_free_tree(nodes);
463                 }
464
465
466                 // perform connect
467
468                 if(connect_to && !connect_to->connection)
469                 {
470                         connect_to->last_connect_try = time(NULL);
471
472                         /* check if there is already a connection attempt to this node */
473                         bool found = false;
474                         for list_each(outgoing_t, outgoing, mesh->outgoings)
475                         {
476                                 if(!strcmp(outgoing->name, connect_to->name))
477                                 {
478                                         found = true;
479                                         break;
480                                 }
481                         }
482
483                         if(!found)
484                         {
485                                 logger(mesh, MESHLINK_INFO, "Autoconnecting to %s", connect_to->name);
486                                 outgoing_t *outgoing = xzalloc(sizeof(outgoing_t));
487                                 outgoing->mesh = mesh;
488                                 outgoing->name = xstrdup(connect_to->name);
489                                 list_insert_tail(mesh->outgoings, outgoing);
490                                 setup_outgoing_connection(mesh, outgoing);
491                         }
492                         else
493                                 { logger(mesh, MESHLINK_INFO, "* skip autoconnect since it is an outgoing connection already"); }
494                 }
495
496
497                 // disconnect suboptimal outgoing connections
498
499                 if(min_connects < cur_connects && cur_connects <= max_connects)
500                 {
501                         unsigned int connects = 0;
502
503                         for(int devclass = 0; devclass <= mesh->devclass; ++devclass)
504                         {
505                                 for list_each(connection_t, c, mesh->connections)
506                                 {
507                                         if(!c->status.remove_unused && c->node && c->node->devclass == devclass)
508                                                 { connects += 1; }
509                                 }
510
511                                 if( min_connects < connects )
512                                 {
513                                         splay_tree_t *nodes = splay_alloc_tree(node_compare_devclass_desc, NULL);
514
515                                         for list_each(connection_t, c, mesh->connections)
516                                         {
517                                                 if(!c->status.remove_unused && c->outgoing && c->node && c->node->devclass >= devclass)
518                                                         { splay_insert(nodes, c->node); }
519                                         }
520
521                                         if(nodes->head)
522                                         {
523                                                 logger(mesh, MESHLINK_INFO, "* disconnect suboptimal outgoing connection");
524                                                 disconnect_from = (node_t*)nodes->head->data;
525                                         }
526
527                                         splay_free_tree(nodes);
528                                         break;
529                                 }
530                         }
531
532                         if(!disconnect_from)
533                                 { logger(mesh, MESHLINK_INFO, "* no suboptimal outgoing connections"); }
534                 }
535
536
537                 // disconnect connections (too many connections)
538
539                 if(!disconnect_from && max_connects < cur_connects)
540                 {
541                         splay_tree_t *nodes = splay_alloc_tree(node_compare_devclass_desc, NULL);
542
543                         for list_each(connection_t, c, mesh->connections)
544                         {
545                                 if(!c->status.remove_unused && c->node)
546                                         { splay_insert(nodes, c->node); }
547                         }
548
549                         if(nodes->head)
550                         {
551                                 logger(mesh, MESHLINK_INFO, "* disconnect connection (too many connections)");
552
553                                 //timeout = 0;
554                                 disconnect_from = (node_t*)nodes->head->data;
555                         }
556                         else
557                                 { logger(mesh, MESHLINK_INFO, "* no node we want to disconnect, even though we have too many connections"); }
558
559                         splay_free_tree(nodes);
560                 }
561
562
563                 // perform disconnect
564
565                 if(disconnect_from && disconnect_from->connection)
566                 {
567                         logger(mesh, MESHLINK_INFO, "Autodisconnecting from %s", disconnect_from->connection->name);
568                         list_delete(mesh->outgoings, disconnect_from->connection->outgoing);
569                         disconnect_from->connection->outgoing = NULL;
570                         terminate_connection(mesh, disconnect_from->connection, disconnect_from->connection->status.active);
571                 }
572
573
574                 // done!
575
576                 logger(mesh, MESHLINK_INFO, "--- autoconnect end ---");
577         }
578
579         timeout_set(&mesh->loop, data, &(struct timeval){timeout, rand() % 100000});
580 }
581
582 void handle_meta_connection_data(meshlink_handle_t *mesh, connection_t *c) {
583         if (!receive_meta(mesh, c)) {
584                 terminate_connection(mesh, c, c->status.active);
585                 return;
586         }
587 }
588
589 void retry(meshlink_handle_t *mesh) {
590         /* Reset the reconnection timers for all outgoing connections */
591         for list_each(outgoing_t, outgoing, mesh->outgoings) {
592                 outgoing->timeout = 0;
593                 if(outgoing->ev.cb)
594                         timeout_set(&mesh->loop, &outgoing->ev, &(struct timeval){0, 0});
595         }
596
597         /* Check for outgoing connections that are in progress, and reset their ping timers */
598         for list_each(connection_t, c, mesh->connections) {
599                 if(c->outgoing && !c->node)
600                         c->last_ping_time = 0;
601         }
602
603         /* Kick the ping timeout handler */
604         timeout_set(&mesh->loop, &mesh->pingtimer, &(struct timeval){0, 0});
605 }
606
607 /*
608   this is where it all happens...
609 */
610 int main_loop(meshlink_handle_t *mesh) {
611         timeout_add(&mesh->loop, &mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timeval){mesh->pingtimeout, rand() % 100000});
612         timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval){0, 0});
613
614         //Add signal handler
615         mesh->datafromapp.signum = 0;
616         signal_add(&(mesh->loop),&(mesh->datafromapp), (signal_cb_t)meshlink_send_from_queue,mesh, mesh->datafromapp.signum);
617
618         if(!event_loop_run(&(mesh->loop), &(mesh->mesh_mutex))) {
619                 logger(mesh, MESHLINK_ERROR, "Error while waiting for input: %s", strerror(errno));
620                 return 1;
621         }
622
623         timeout_del(&mesh->loop, &mesh->periodictimer);
624         timeout_del(&mesh->loop, &mesh->pingtimer);
625
626         return 0;
627 }