]> git.meshlink.io Git - meshlink/blob - src/net.c
added log info
[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 static const int min(int a, int b) {
35         return a < b ? a : b;
36 }
37
38 /*
39   Terminate a connection:
40   - Mark it as inactive
41   - Remove the edge representing this connection
42   - Kill it with fire
43   - Check if we need to retry making an outgoing connection
44 */
45 void terminate_connection(meshlink_handle_t *mesh, connection_t *c, bool report) {
46         logger(mesh, MESHLINK_INFO, "Closing connection with %s (%s)", c->name, c->hostname);
47
48         c->status.active = false;
49
50         if(c->node && c->node->connection == c)
51                 c->node->connection = NULL;
52
53         if(c->edge) {
54                 if(report)
55                         send_del_edge(mesh, mesh->everyone, c->edge);
56
57                 edge_del(mesh, c->edge);
58                 c->edge = NULL;
59
60                 /* Run MST and SSSP algorithms */
61
62                 graph(mesh);
63
64                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
65
66                 if(report && !c->node->status.reachable) {
67                         edge_t *e;
68                         e = lookup_edge(c->node, mesh->self);
69                         if(e) {
70                                 send_del_edge(mesh, mesh->everyone, e);
71                                 edge_del(mesh, e);
72                         }
73                 }
74         }
75
76         outgoing_t *outgoing = c->outgoing;
77         connection_del(mesh, c);
78
79         /* Check if this was our outgoing connection */
80
81         if(outgoing)
82                 do_outgoing_connection(mesh, outgoing);
83
84 #ifndef HAVE_MINGW
85         /* Clean up dead proxy processes */
86
87         while(waitpid(-1, NULL, WNOHANG) > 0);
88 #endif
89 }
90
91 /*
92   Check if the other end is active.
93   If we have sent packets, but didn't receive any,
94   then possibly the other end is dead. We send a
95   PING request over the meta connection. If the other
96   end does not reply in time, we consider them dead
97   and close the connection.
98 */
99 static void timeout_handler(event_loop_t *loop, void *data) {
100         meshlink_handle_t *mesh = loop->data;
101         logger(mesh, MESHLINK_DEBUG, "timeout_handler()");
102
103         for list_each(connection_t, c, mesh->connections) {
104                 // Also make sure that if outstanding key requests for the UDP counterpart of a connection has timed out, we restart it.
105                 if(c->node) {
106                         if(c->node->status.waitingforkey && c->node->last_req_key + mesh->pingtimeout <= mesh->loop.now.tv_sec)
107                                 send_req_key(mesh, c->node);
108                 }
109                 if(c->last_ping_time + mesh->pingtimeout <= mesh->loop.now.tv_sec) {
110                         if(c->status.active) {
111                                 if(c->status.pinged) {
112                                         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);
113                                 } else if(c->last_ping_time + mesh->pinginterval <= mesh->loop.now.tv_sec) {
114                                         send_ping(mesh, c);
115                                         continue;
116                                 } else {
117                                         continue;
118                                 }
119                         } else {
120                                 if(c->status.connecting)
121                                         logger(mesh, MESHLINK_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
122                                 else
123                                         logger(mesh, MESHLINK_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
124                         }
125                         terminate_connection(mesh, c, c->status.active);
126                 }
127         }
128
129         timeout_set(&mesh->loop, data, &(struct timeval){mesh->pingtimeout, rand() % 100000});
130 }
131
132 /// Utility function to establish connections based on condition check
133 /** The function iterates over all nodes, but skips those that do
134  *  not pass the condition check.
135  *  
136  *  The condition check function is passed
137  *  a pointer to a random number r between 0 and rand_modulo, a pointer to the
138  *  current node index i, and the node pointer n. This function should return true
139  *  if a connection attempt to the node should be made.
140  *  
141  *  @param mesh         A pointer to the mesh structure
142  *  @param rand_modulo  Random index is selected between 0 and rand_modulo
143  *  @cond_check         A function pointer. This function should return true
144  *                      if a connection attempt to the node should be made
145  */
146 static void cond_add_connection(meshlink_handle_t *mesh, int rand_modulo, bool (*cond_check)(int*, int*, node_t*)) {
147         int r = rand() % rand_modulo;
148         int i = 0;
149
150         for splay_each(node_t, n, mesh->nodes) {
151                 /* skip nodes that do not pass condition check */
152                 if(!(*cond_check)(&i, &r, n))
153                         continue;
154
155                 /* check if there is already a connection attempt to this node */
156                 bool found = false;
157                 for list_each(outgoing_t, outgoing, mesh->outgoings) {
158                         if(!strcmp(outgoing->name, n->name)) {
159                                 found = true;
160                                 break;
161                         }
162                 }
163
164                 if(!found) {
165                         //TODO: if the node is blacklisted the connection will not happen, but
166                         //the user will read this debug message "Autoconnecting to %s" that is misleading
167                         logger(mesh, MESHLINK_INFO, "Autoconnecting to %s", n->name);
168                         outgoing_t *outgoing = xzalloc(sizeof *outgoing);
169                         outgoing->mesh = mesh;
170                         outgoing->name = xstrdup(n->name);
171                         list_insert_tail(mesh->outgoings, outgoing);
172                         setup_outgoing_connection(mesh, outgoing);
173                 }
174                 break;
175         }
176 }
177
178 static bool found_random_node(int *i, int *r, node_t *n) {
179         if((*i)++ != *r)
180                 return false;
181
182         if(n->connection)
183                 return false;
184         
185         return true;
186 }
187
188 static bool found_random_unreachable_node(int *i, int *r, node_t *n) {
189         if(n->status.reachable)
190                 return false;
191         
192         if((*i)++ != *r)
193                 return false;
194
195         if(n->connection)
196                 return false;
197
198         return true;
199 }
200
201 static void periodic_handler(event_loop_t *loop, void *data) {
202         meshlink_handle_t *mesh = loop->data;
203
204         /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
205            This usually only happens when another node has the same Name as this node.
206            If so, sleep for a short while to prevent a storm of contradicting messages.
207         */
208
209         if(mesh->contradicting_del_edge > 100 && mesh->contradicting_add_edge > 100) {
210                 logger(mesh, MESHLINK_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", mesh->sleeptime);
211                 usleep(mesh->sleeptime * 1000000LL);
212                 mesh->sleeptime *= 2;
213                 if(mesh->sleeptime < 0)
214                         mesh->sleeptime = 3600;
215         } else {
216                 mesh->sleeptime /= 2;
217                 if(mesh->sleeptime < 10)
218                         mesh->sleeptime = 10;
219         }
220
221         mesh->contradicting_add_edge = 0;
222         mesh->contradicting_del_edge = 0;
223
224         int timeout = 5;
225
226         /* Check if we need to make or break connections. */
227
228         if(mesh->nodes->count > 1) {
229
230                 logger(mesh, MESHLINK_INFO, "--- autoconnect begin ---");
231
232                 splay_tree_t* ccounts = splay_alloc_tree(dclass_ccount_compare, NULL);
233
234                 /* Count number of active connections per device class */
235                 int num_total = 0;
236                 for list_each(connection_t, c, mesh->connections) {
237                         if(c->status.active)
238                         {
239                                 dclass_ccount_t key;
240                                 key.dclass = c->node->dclass;
241
242                                 dclass_ccount_t* ccount = splay_search(ccounts, &key);
243
244                                 if(!ccount)
245                                 {
246                                         ccount = dclass_ccount_alloc();
247                                         ccount->dclass = c->node->dclass;
248                                         splay_insert(ccounts, ccount);
249                                 }
250
251                                 ccount->ccount++;
252                                 num_total++;
253                         }
254                 }
255
256                 /* Count number of unreachable nodes */
257                 int num_unreachable = 0;
258                 for splay_each(node_t, n, mesh->nodes) {
259                         if(!n->status.reachable)
260                                 num_unreachable++;
261                 }
262
263                 bool satisfied = dclass_ccounts_satisfied(mesh->self->dclass, ccounts, num_total);
264                 int maxcc = max_ccount_from_dclass(mesh->self->dclass);
265
266                 logger(mesh, MESHLINK_INFO, "* num_total = %d, satisfied = %d, maxcc = %d", num_total, satisfied, maxcc);
267
268                 if(!satisfied) {
269                         logger(mesh, MESHLINK_INFO, "* Not enough active connections, try to add one.");
270                         /* Not enough active connections, try to add one.
271                            Choose a random node, if we don't have a connection to it,
272                            and we are not already trying to make one, create an
273                            outgoing connection to this node.
274                         */
275                         cond_add_connection(mesh, mesh->nodes->count, &found_random_node);
276                 }
277
278                 if(satisfied && num_unreachable > 0) {
279                         logger(mesh, MESHLINK_INFO, "* Min number of connections established. Now heal possible partitions.");
280                         /* Min number of connections established. Now try
281                            to connect to some unreachable nodes to attempt
282                            to heal possible partitions.
283                         */
284                         cond_add_connection(mesh, num_unreachable, &found_random_unreachable_node);
285                 }
286                 
287                 if(num_total > maxcc) {
288                         logger(mesh, MESHLINK_INFO, "* Too many active connections, try to remove one.");
289                         /* Too many active connections, try to remove one.
290                            Choose a random outgoing connection to a node
291                            that has at least one other connection.
292                         */
293                         int r = rand() % num_total;
294                         int i = 0;
295
296                         for list_each(connection_t, c, mesh->connections) {
297                                 if(!c->status.active)
298                                         continue;
299
300                                 if(i++ != r)
301                                         continue;
302
303                                 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
304                                         break;
305
306                                 logger(mesh, MESHLINK_INFO, "Autodisconnecting from %s", c->name);
307                                 list_delete(mesh->outgoings, c->outgoing);
308                                 c->outgoing = NULL;
309                                 terminate_connection(mesh, c, c->status.active);
310                                 break;
311                         }
312                 }
313
314                 if(satisfied) {
315                         logger(mesh, MESHLINK_INFO, "* We have enough active connections, remove pending outgoing connections.");
316                         /* If we have enough active connections,
317                            remove any pending outgoing connections.
318                            Do not remove pending connections to unreachable
319                            nodes.
320                         */
321                         node_t *o_node = NULL;
322                         for list_each(outgoing_t, o, mesh->outgoings) {
323                                 o_node = lookup_node(mesh, o->name);
324                                 /* o_node is NULL if it is not part of the graph yet */
325                                 if(!o_node || !o_node->status.reachable)
326                                         continue;
327
328                                 bool found = false;
329                                 for list_each(connection_t, c, mesh->connections) {
330                                         if(c->outgoing == o) {
331                                                 found = true;
332                                                 break;
333                                         }
334                                 }
335                                 if(!found) {
336                                         logger(mesh, MESHLINK_INFO, "Cancelled outgoing connection to %s", o->name);
337                                         /* The node variable is leaked in from using the list_each macro.
338                                            The o variable could be used, but using node directly
339                                            is more efficient.
340                                         */
341                                         list_delete_node(mesh->outgoings, node);
342                                 }
343                         }
344                 }
345
346                 if (!satisfied && (num_total + mesh->outgoings->count) < mesh->nodes->count)
347                 {
348                         logger(mesh, MESHLINK_INFO, "* No timeout.");
349                         timeout = 0;
350                 }
351
352                 splay_free_tree(ccounts);
353
354                 logger(mesh, MESHLINK_INFO, "--- autoconnect end ---");
355         }
356
357         timeout_set(&mesh->loop, data, &(struct timeval){timeout, rand() % 100000});
358 }
359
360 void handle_meta_connection_data(meshlink_handle_t *mesh, connection_t *c) {
361         if (!receive_meta(mesh, c)) {
362                 terminate_connection(mesh, c, c->status.active);
363                 return;
364         }
365 }
366
367 void retry(meshlink_handle_t *mesh) {
368         /* Reset the reconnection timers for all outgoing connections */
369         for list_each(outgoing_t, outgoing, mesh->outgoings) {
370                 outgoing->timeout = 0;
371                 if(outgoing->ev.cb)
372                         timeout_set(&mesh->loop, &outgoing->ev, &(struct timeval){0, 0});
373         }
374
375         /* Check for outgoing connections that are in progress, and reset their ping timers */
376         for list_each(connection_t, c, mesh->connections) {
377                 if(c->outgoing && !c->node)
378                         c->last_ping_time = 0;
379         }
380
381         /* Kick the ping timeout handler */
382         timeout_set(&mesh->loop, &mesh->pingtimer, &(struct timeval){0, 0});
383 }
384
385 /*
386   this is where it all happens...
387 */
388 int main_loop(meshlink_handle_t *mesh) {
389         timeout_add(&mesh->loop, &mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timeval){mesh->pingtimeout, rand() % 100000});
390         timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval){0, 0});
391
392         //Add signal handler
393         mesh->datafromapp.signum = 0;
394         signal_add(&(mesh->loop),&(mesh->datafromapp), (signal_cb_t)meshlink_send_from_queue,mesh, mesh->datafromapp.signum);
395
396         if(!event_loop_run(&mesh->loop)) {
397                 logger(mesh, MESHLINK_ERROR, "Error while waiting for input: %s", strerror(errno));
398                 return 1;
399         }
400
401         timeout_del(&mesh->loop, &mesh->periodictimer);
402         timeout_del(&mesh->loop, &mesh->pingtimer);
403
404         return 0;
405 }