]> git.meshlink.io Git - meshlink/blob - src/net.c
87deb97b60d669bf19897a54b6dbd275de644f72
[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 "meta.h"
28 #include "net.h"
29 #include "netutl.h"
30 #include "protocol.h"
31 #include "xalloc.h"
32
33 int contradicting_add_edge = 0;
34 int contradicting_del_edge = 0;
35 static int sleeptime = 10;
36 time_t last_config_check = 0;
37 static timeout_t pingtimer;
38 static timeout_t periodictimer;
39
40 //TODO: move this to a better place
41 char *confbase;
42
43 /* Purge edges of unreachable nodes. Use carefully. */
44
45 // TODO: remove
46 void purge(void) {
47         logger(DEBUG_PROTOCOL, LOG_DEBUG, "Purging unreachable nodes");
48
49         /* Remove all edges owned by unreachable nodes. */
50
51         for splay_each(node_t, n, node_tree) {
52                 if(!n->status.reachable) {
53                         logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Purging node %s (%s)", n->name, n->hostname);
54
55                         for splay_each(edge_t, e, n->edge_tree) {
56                                 send_del_edge(everyone, e);
57                                 edge_del(e);
58                         }
59                 }
60         }
61
62         /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
63
64         for splay_each(node_t, n, node_tree) {
65                 if(!n->status.reachable) {
66                         for splay_each(edge_t, e, edge_weight_tree)
67                                 if(e->to == n)
68                                         return;
69                 }
70         }
71 }
72
73 /*
74   Terminate a connection:
75   - Mark it as inactive
76   - Remove the edge representing this connection
77   - Kill it with fire
78   - Check if we need to retry making an outgoing connection
79 */
80 void terminate_connection(connection_t *c, bool report) {
81         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname);
82
83         c->status.active = false;
84
85         if(c->node && c->node->connection == c)
86                 c->node->connection = NULL;
87
88         if(c->edge) {
89                 if(report)
90                         send_del_edge(everyone, c->edge);
91
92                 edge_del(c->edge);
93                 c->edge = NULL;
94
95                 /* Run MST and SSSP algorithms */
96
97                 graph();
98
99                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
100
101                 if(report && !c->node->status.reachable) {
102                         edge_t *e;
103                         e = lookup_edge(c->node, myself);
104                         if(e) {
105                                 send_del_edge(everyone, e);
106                                 edge_del(e);
107                         }
108                 }
109         }
110
111         outgoing_t *outgoing = c->outgoing;
112         connection_del(c);
113
114         /* Check if this was our outgoing connection */
115
116         if(outgoing)
117                 do_outgoing_connection(outgoing);
118
119 #ifndef HAVE_MINGW
120         /* Clean up dead proxy processes */
121
122         while(waitpid(-1, NULL, WNOHANG) > 0);
123 #endif
124 }
125
126 /*
127   Check if the other end is active.
128   If we have sent packets, but didn't receive any,
129   then possibly the other end is dead. We send a
130   PING request over the meta connection. If the other
131   end does not reply in time, we consider them dead
132   and close the connection.
133 */
134 static void timeout_handler(void *data) {
135         for list_each(connection_t, c, connection_list) {
136                 if(c->last_ping_time + pingtimeout <= now.tv_sec) {
137                         if(c->status.active) {
138                                 if(c->status.pinged) {
139                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds", c->name, c->hostname, (long)now.tv_sec - c->last_ping_time);
140                                 } else if(c->last_ping_time + pinginterval <= now.tv_sec) {
141                                         send_ping(c);
142                                         continue;
143                                 } else {
144                                         continue;
145                                 }
146                         } else {
147                                 if(c->status.connecting)
148                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
149                                 else
150                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
151                         }
152                         terminate_connection(c, c->status.active);
153                 }
154         }
155
156         timeout_set(data, &(struct timeval){pingtimeout, rand() % 100000});
157 }
158
159 static void periodic_handler(void *data) {
160         /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
161            This usually only happens when another node has the same Name as this node.
162            If so, sleep for a short while to prevent a storm of contradicting messages.
163         */
164
165         if(contradicting_del_edge > 100 && contradicting_add_edge > 100) {
166                 logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime);
167                 usleep(sleeptime * 1000000LL);
168                 sleeptime *= 2;
169                 if(sleeptime < 0)
170                         sleeptime = 3600;
171         } else {
172                 sleeptime /= 2;
173                 if(sleeptime < 10)
174                         sleeptime = 10;
175         }
176
177         contradicting_add_edge = 0;
178         contradicting_del_edge = 0;
179
180         /* If AutoConnect is set, check if we need to make or break connections. */
181
182         if(autoconnect && node_tree->count > 1) {
183                 /* Count number of active connections */
184                 int nc = 0;
185                 for list_each(connection_t, c, connection_list) {
186                         if(c->status.active)
187                                 nc++;
188                 }
189
190                 if(nc < autoconnect) {
191                         /* Not enough active connections, try to add one.
192                            Choose a random node, if we don't have a connection to it,
193                            and we are not already trying to make one, create an
194                            outgoing connection to this node.
195                         */
196                         int r = rand() % node_tree->count;
197                         int i = 0;
198
199                         for splay_each(node_t, n, node_tree) {
200                                 if(i++ != r)
201                                         continue;
202
203                                 if(n->connection)
204                                         break;
205
206                                 bool found = false;
207
208                                 for list_each(outgoing_t, outgoing, outgoing_list) {
209                                         if(!strcmp(outgoing->name, n->name)) {
210                                                 found = true;
211                                                 break;
212                                         }
213                                 }
214
215                                 if(!found) {
216                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
217                                         outgoing_t *outgoing = xzalloc(sizeof *outgoing);
218                                         outgoing->name = xstrdup(n->name);
219                                         list_insert_tail(outgoing_list, outgoing);
220                                         setup_outgoing_connection(outgoing);
221                                 }
222                                 break;
223                         }
224                 } else if(nc > autoconnect) {
225                         /* Too many active connections, try to remove one.
226                            Choose a random outgoing connection to a node
227                            that has at least one other connection.
228                         */
229                         int r = rand() % nc;
230                         int i = 0;
231
232                         for list_each(connection_t, c, connection_list) {
233                                 if(!c->status.active)
234                                         continue;
235
236                                 if(i++ != r)
237                                         continue;
238
239                                 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
240                                         break;
241
242                                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
243                                 list_delete(outgoing_list, c->outgoing);
244                                 c->outgoing = NULL;
245                                 terminate_connection(c, c->status.active);
246                                 break;
247                         }
248                 }
249
250                 if(nc >= autoconnect) {
251                         /* If we have enough active connections,
252                            remove any pending outgoing connections.
253                         */
254                         for list_each(outgoing_t, o, outgoing_list) {
255                                 bool found = false;
256                                 for list_each(connection_t, c, connection_list) {
257                                         if(c->outgoing == o) {
258                                                 found = true;
259                                                 break;
260                                         }
261                                 }
262                                 if(!found) {
263                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
264                                         list_delete_node(outgoing_list, node);
265                                 }
266                         }
267                 }
268         }
269
270         timeout_set(data, &(struct timeval){5, rand() % 100000});
271 }
272
273 void handle_meta_connection_data(connection_t *c) {
274         if (!receive_meta(c)) {
275                 terminate_connection(c, c->status.active);
276                 return;
277         }
278 }
279
280 int reload_configuration(void) {
281         char *fname = NULL;
282
283         /* Reread our own configuration file */
284
285         exit_configuration(&config_tree);
286         init_configuration(&config_tree);
287
288         if(!read_server_config()) {
289                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file.");
290                 return EINVAL;
291         }
292
293         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, myself->name);
294         read_config_file(config_tree, fname);
295         free(fname);
296
297         /* Parse some options that are allowed to be changed while tinc is running */
298
299         setup_myself_reloadable();
300
301         /* Try to make outgoing connections */
302
303         try_outgoing_connections();
304
305         /* Close connections to hosts that have a changed or deleted host config file */
306
307         for list_each(connection_t, c, connection_list) {
308                 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
309                 struct stat s;
310                 if(stat(fname, &s) || s.st_mtime > last_config_check) {
311                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name);
312                         terminate_connection(c, c->status.active);
313                 }
314                 free(fname);
315         }
316
317         last_config_check = now.tv_sec;
318
319         return 0;
320 }
321
322 void retry(void) {
323         /* Reset the reconnection timers for all outgoing connections */
324         for list_each(outgoing_t, outgoing, outgoing_list) {
325                 outgoing->timeout = 0;
326                 if(outgoing->ev.cb)
327                         timeout_set(&outgoing->ev, &(struct timeval){0, 0});
328         }
329
330         /* Check for outgoing connections that are in progress, and reset their ping timers */
331         for list_each(connection_t, c, connection_list) {
332                 if(c->outgoing && !c->node)
333                         c->last_ping_time = 0;
334         }
335
336         /* Kick the ping timeout handler */
337         timeout_set(&pingtimer, &(struct timeval){0, 0});
338 }
339
340 /*
341   this is where it all happens...
342 */
343 int main_loop(void) {
344         timeout_add(&pingtimer, timeout_handler, &pingtimer, &(struct timeval){pingtimeout, rand() % 100000});
345         timeout_add(&periodictimer, periodic_handler, &periodictimer, &(struct timeval){pingtimeout, rand() % 100000});
346
347         if(!event_loop()) {
348                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
349                 return 1;
350         }
351
352         timeout_del(&periodictimer);
353         timeout_del(&pingtimer);
354
355         return 0;
356 }