]> git.meshlink.io Git - meshlink/blob - src/net.c
Fix compile error.
[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 /*
35   Terminate a connection:
36   - Mark it as inactive
37   - Remove the edge representing this connection
38   - Kill it with fire
39   - Check if we need to retry making an outgoing connection
40 */
41 void terminate_connection(meshlink_handle_t *mesh, connection_t *c, bool report) {
42         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname);
43
44         c->status.active = false;
45
46         if(c->node && c->node->connection == c)
47                 c->node->connection = NULL;
48
49         if(c->edge) {
50                 if(report)
51                         send_del_edge(mesh, mesh->everyone, c->edge);
52
53                 edge_del(mesh, c->edge);
54                 c->edge = NULL;
55
56                 /* Run MST and SSSP algorithms */
57
58                 graph(mesh);
59
60                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
61
62                 if(report && !c->node->status.reachable) {
63                         edge_t *e;
64                         e = lookup_edge(c->node, mesh->self);
65                         if(e) {
66                                 send_del_edge(mesh, mesh->everyone, e);
67                                 edge_del(mesh, e);
68                         }
69                 }
70         }
71
72         outgoing_t *outgoing = c->outgoing;
73         connection_del(mesh, c);
74
75         /* Check if this was our outgoing connection */
76
77         if(outgoing)
78                 do_outgoing_connection(mesh, outgoing);
79
80 #ifndef HAVE_MINGW
81         /* Clean up dead proxy processes */
82
83         while(waitpid(-1, NULL, WNOHANG) > 0);
84 #endif
85 }
86
87 /*
88   Check if the other end is active.
89   If we have sent packets, but didn't receive any,
90   then possibly the other end is dead. We send a
91   PING request over the meta connection. If the other
92   end does not reply in time, we consider them dead
93   and close the connection.
94 */
95 static void timeout_handler(event_loop_t *loop, void *data) {
96         meshlink_handle_t *mesh = loop->data;
97
98         for list_each(connection_t, c, mesh->connections) {
99                 if(c->last_ping_time + mesh->pingtimeout <= mesh->loop.now.tv_sec) {
100                         if(c->status.active) {
101                                 if(c->status.pinged) {
102                                         logger(DEBUG_CONNECTIONS, LOG_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);
103                                 } else if(c->last_ping_time + mesh->pinginterval <= mesh->loop.now.tv_sec) {
104                                         send_ping(mesh, c);
105                                         continue;
106                                 } else {
107                                         continue;
108                                 }
109                         } else {
110                                 if(c->status.connecting)
111                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
112                                 else
113                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
114                         }
115                         terminate_connection(mesh, c, c->status.active);
116                 }
117         }
118
119         timeout_set(&mesh->loop, data, &(struct timeval){mesh->pingtimeout, rand() % 100000});
120 }
121
122 static void periodic_handler(event_loop_t *loop, void *data) {
123         meshlink_handle_t *mesh = loop->data;
124
125         /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
126            This usually only happens when another node has the same Name as this node.
127            If so, sleep for a short while to prevent a storm of contradicting messages.
128         */
129
130         if(mesh->contradicting_del_edge > 100 && mesh->contradicting_add_edge > 100) {
131                 logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", mesh->sleeptime);
132                 usleep(mesh->sleeptime * 1000000LL);
133                 mesh->sleeptime *= 2;
134                 if(mesh->sleeptime < 0)
135                         mesh->sleeptime = 3600;
136         } else {
137                 mesh->sleeptime /= 2;
138                 if(mesh->sleeptime < 10)
139                         mesh->sleeptime = 10;
140         }
141
142         mesh->contradicting_add_edge = 0;
143         mesh->contradicting_del_edge = 0;
144
145         /* If AutoConnect is set, check if we need to make or break connections. */
146
147         if(autoconnect && mesh->nodes->count > 1) {
148                 /* Count number of active connections */
149                 int nc = 0;
150                 for list_each(connection_t, c, mesh->connections) {
151                         if(c->status.active)
152                                 nc++;
153                 }
154
155                 if(nc < autoconnect) {
156                         /* Not enough active connections, try to add one.
157                            Choose a random node, if we don't have a connection to it,
158                            and we are not already trying to make one, create an
159                            outgoing connection to this node.
160                         */
161                         int r = rand() % mesh->nodes->count;
162                         int i = 0;
163
164                         for splay_each(node_t, n, mesh->nodes) {
165                                 bool trying_unreachable = false;
166
167                                 if(i++ != r) {
168                                         if(n->status.reachable) {
169                                                 continue;
170                                         } else {
171                                                 /* If we see an unreachable node
172                                                    before node i, try it anyway.
173                                                 */
174                                                 trying_unreachable = true;
175                                         }
176                                 }
177
178                                 if(n->connection)
179                                         break;
180
181                                 bool found = false;
182
183                                 for list_each(outgoing_t, outgoing, mesh->outgoings) {
184                                         if(!strcmp(outgoing->name, n->name)) {
185                                                 found = true;
186                                                 break;
187                                         }
188                                 }
189
190                                 if(!found) {
191                                         //TODO: if the node is blacklisted the connection will not happen, but
192                                         //the user will read this debug message "Autoconnecting to %s" that is misleading
193                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
194                                         outgoing_t *outgoing = xzalloc(sizeof *outgoing);
195                                         outgoing->name = xstrdup(n->name);
196                                         list_insert_tail(mesh->outgoings, outgoing);
197                                         setup_outgoing_connection(mesh, outgoing);
198                                 } else if(trying_unreachable) {
199                                         /* We're trying an unreachable node instead
200                                            of node i. We already have an outgoing
201                                            to it. Try the next node rather than
202                                            breaking here, to avoid churning on a
203                                            connection attempt to the first
204                                            unreachable node.
205                                         */
206                                         continue;
207                                 }
208                                 break;
209                         }
210                 } else if(nc > autoconnect) {
211                         /* Too many active connections, try to remove one.
212                            Choose a random outgoing connection to a node
213                            that has at least one other connection.
214                         */
215                         int r = rand() % nc;
216                         int i = 0;
217
218                         for list_each(connection_t, c, mesh->connections) {
219                                 if(!c->status.active)
220                                         continue;
221
222                                 if(i++ != r)
223                                         continue;
224
225                                 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
226                                         break;
227
228                                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
229                                 list_delete(mesh->outgoings, c->outgoing);
230                                 c->outgoing = NULL;
231                                 terminate_connection(mesh, c, c->status.active);
232                                 break;
233                         }
234                 }
235
236                 if(nc >= autoconnect) {
237                         /* If we have enough active connections,
238                            remove any pending outgoing connections.
239                         */
240                         for list_each(outgoing_t, o, mesh->outgoings) {
241                                 bool found = false;
242                                 for list_each(connection_t, c, mesh->connections) {
243                                         if(c->outgoing == o) {
244                                                 found = true;
245                                                 break;
246                                         }
247                                 }
248                                 if(!found) {
249                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
250                                         list_delete_node(mesh->outgoings, node);
251                                 }
252                         }
253                 }
254         }
255
256         timeout_set(&mesh->loop, data, &(struct timeval){5, rand() % 100000});
257 }
258
259 void handle_meta_connection_data(meshlink_handle_t *mesh, connection_t *c) {
260         if (!receive_meta(mesh, c)) {
261                 terminate_connection(mesh, c, c->status.active);
262                 return;
263         }
264 }
265
266 void retry(meshlink_handle_t *mesh) {
267         /* Reset the reconnection timers for all outgoing connections */
268         for list_each(outgoing_t, outgoing, mesh->outgoings) {
269                 outgoing->timeout = 0;
270                 if(outgoing->ev.cb)
271                         timeout_set(&mesh->loop, &outgoing->ev, &(struct timeval){0, 0});
272         }
273
274         /* Check for outgoing connections that are in progress, and reset their ping timers */
275         for list_each(connection_t, c, mesh->connections) {
276                 if(c->outgoing && !c->node)
277                         c->last_ping_time = 0;
278         }
279
280         /* Kick the ping timeout handler */
281         timeout_set(&mesh->loop, &mesh->pingtimer, &(struct timeval){0, 0});
282 }
283
284 /*
285   this is where it all happens...
286 */
287 int main_loop(meshlink_handle_t *mesh) {
288         timeout_add(&mesh->loop, &mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timeval){mesh->pingtimeout, rand() % 100000});
289         timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval){mesh->pingtimeout, rand() % 100000});
290
291         //Add signal handler
292         mesh->datafromapp.signum = 0;
293         signal_add(&(mesh->loop),&(mesh->datafromapp), (signal_cb_t)meshlink_send_from_queue,mesh, mesh->datafromapp.signum);
294
295         if(!event_loop_run(&mesh->loop)) {
296                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
297                 return 1;
298         }
299
300         timeout_del(&mesh->loop, &mesh->periodictimer);
301         timeout_del(&mesh->loop, &mesh->pingtimer);
302
303         return 0;
304 }