]> git.meshlink.io Git - meshlink/blob - src/net.c
Remove support for control connections.
[meshlink] / src / net.c
1 /*
2     net.c -- most of the network code
3     Copyright (C) 1998-2005 Ivo Timmermans,
4                   2000-2013 Guus Sliepen <guus@tinc-vpn.org>
5                   2006      Scott Lamb <slamb@slamb.org>
6                   2011      Loïc Grenié <loic.grenie@gmail.com>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "system.h"
24
25 #include "utils.h"
26 #include "conf.h"
27 #include "connection.h"
28 #include "graph.h"
29 #include "logger.h"
30 #include "meta.h"
31 #include "names.h"
32 #include "net.h"
33 #include "netutl.h"
34 #include "protocol.h"
35 #include "xalloc.h"
36
37 int contradicting_add_edge = 0;
38 int contradicting_del_edge = 0;
39 static int sleeptime = 10;
40 time_t last_config_check = 0;
41 static timeout_t pingtimer;
42 static timeout_t periodictimer;
43
44 /* Purge edges of unreachable nodes. Use carefully. */
45
46 // TODO: remove
47 void purge(void) {
48         logger(DEBUG_PROTOCOL, LOG_DEBUG, "Purging unreachable nodes");
49
50         /* Remove all edges owned by unreachable nodes. */
51
52         for splay_each(node_t, n, node_tree) {
53                 if(!n->status.reachable) {
54                         logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Purging node %s (%s)", n->name, n->hostname);
55
56                         for splay_each(edge_t, e, n->edge_tree) {
57                                 if(!tunnelserver)
58                                         send_del_edge(everyone, e);
59                                 edge_del(e);
60                         }
61                 }
62         }
63
64         /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
65
66         for splay_each(node_t, n, node_tree) {
67                 if(!n->status.reachable) {
68                         for splay_each(edge_t, e, edge_weight_tree)
69                                 if(e->to == n)
70                                         return;
71                 }
72         }
73 }
74
75 /*
76   Terminate a connection:
77   - Mark it as inactive
78   - Remove the edge representing this connection
79   - Kill it with fire
80   - Check if we need to retry making an outgoing connection
81 */
82 void terminate_connection(connection_t *c, bool report) {
83         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname);
84
85         c->status.active = false;
86
87         if(c->node && c->node->connection == c)
88                 c->node->connection = NULL;
89
90         if(c->edge) {
91                 if(report && !tunnelserver)
92                         send_del_edge(everyone, c->edge);
93
94                 edge_del(c->edge);
95                 c->edge = NULL;
96
97                 /* Run MST and SSSP algorithms */
98
99                 graph();
100
101                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
102
103                 if(report && !c->node->status.reachable) {
104                         edge_t *e;
105                         e = lookup_edge(c->node, myself);
106                         if(e) {
107                                 if(!tunnelserver)
108                                         send_del_edge(everyone, e);
109                                 edge_del(e);
110                         }
111                 }
112         }
113
114         outgoing_t *outgoing = c->outgoing;
115         connection_del(c);
116
117         /* Check if this was our outgoing connection */
118
119         if(outgoing)
120                 do_outgoing_connection(outgoing);
121
122 #ifndef HAVE_MINGW
123         /* Clean up dead proxy processes */
124
125         while(waitpid(-1, NULL, WNOHANG) > 0);
126 #endif
127 }
128
129 /*
130   Check if the other end is active.
131   If we have sent packets, but didn't receive any,
132   then possibly the other end is dead. We send a
133   PING request over the meta connection. If the other
134   end does not reply in time, we consider them dead
135   and close the connection.
136 */
137 static void timeout_handler(void *data) {
138         for list_each(connection_t, c, connection_list) {
139                 if(c->last_ping_time + pingtimeout <= now.tv_sec) {
140                         if(c->status.active) {
141                                 if(c->status.pinged) {
142                                         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);
143                                 } else if(c->last_ping_time + pinginterval <= now.tv_sec) {
144                                         send_ping(c);
145                                         continue;
146                                 } else {
147                                         continue;
148                                 }
149                         } else {
150                                 if(c->status.connecting)
151                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
152                                 else
153                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
154                         }
155                         terminate_connection(c, c->status.active);
156                 }
157         }
158
159         timeout_set(data, &(struct timeval){pingtimeout, rand() % 100000});
160 }
161
162 static void periodic_handler(void *data) {
163         /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
164            This usually only happens when another node has the same Name as this node.
165            If so, sleep for a short while to prevent a storm of contradicting messages.
166         */
167
168         if(contradicting_del_edge > 100 && contradicting_add_edge > 100) {
169                 logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime);
170                 usleep(sleeptime * 1000000LL);
171                 sleeptime *= 2;
172                 if(sleeptime < 0)
173                         sleeptime = 3600;
174         } else {
175                 sleeptime /= 2;
176                 if(sleeptime < 10)
177                         sleeptime = 10;
178         }
179
180         contradicting_add_edge = 0;
181         contradicting_del_edge = 0;
182
183         /* If AutoConnect is set, check if we need to make or break connections. */
184
185         if(autoconnect && node_tree->count > 1) {
186                 /* Count number of active connections */
187                 int nc = 0;
188                 for list_each(connection_t, c, connection_list) {
189                         if(c->status.active)
190                                 nc++;
191                 }
192
193                 if(nc < autoconnect) {
194                         /* Not enough active connections, try to add one.
195                            Choose a random node, if we don't have a connection to it,
196                            and we are not already trying to make one, create an
197                            outgoing connection to this node.
198                         */
199                         int r = rand() % node_tree->count;
200                         int i = 0;
201
202                         for splay_each(node_t, n, node_tree) {
203                                 if(i++ != r)
204                                         continue;
205
206                                 if(n->connection)
207                                         break;
208
209                                 bool found = false;
210
211                                 for list_each(outgoing_t, outgoing, outgoing_list) {
212                                         if(!strcmp(outgoing->name, n->name)) {
213                                                 found = true;
214                                                 break;
215                                         }
216                                 }
217
218                                 if(!found) {
219                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
220                                         outgoing_t *outgoing = xzalloc(sizeof *outgoing);
221                                         outgoing->name = xstrdup(n->name);
222                                         list_insert_tail(outgoing_list, outgoing);
223                                         setup_outgoing_connection(outgoing);
224                                 }
225                                 break;
226                         }
227                 } else if(nc > autoconnect) {
228                         /* Too many active connections, try to remove one.
229                            Choose a random outgoing connection to a node
230                            that has at least one other connection.
231                         */
232                         int r = rand() % nc;
233                         int i = 0;
234
235                         for list_each(connection_t, c, connection_list) {
236                                 if(!c->status.active)
237                                         continue;
238
239                                 if(i++ != r)
240                                         continue;
241
242                                 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
243                                         break;
244
245                                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
246                                 list_delete(outgoing_list, c->outgoing);
247                                 c->outgoing = NULL;
248                                 terminate_connection(c, c->status.active);
249                                 break;
250                         }
251                 }
252
253                 if(nc >= autoconnect) {
254                         /* If we have enough active connections,
255                            remove any pending outgoing connections.
256                         */
257                         for list_each(outgoing_t, o, outgoing_list) {
258                                 bool found = false;
259                                 for list_each(connection_t, c, connection_list) {
260                                         if(c->outgoing == o) {
261                                                 found = true;
262                                                 break;
263                                         }
264                                 }
265                                 if(!found) {
266                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
267                                         list_delete_node(outgoing_list, node);
268                                 }
269                         }
270                 }
271         }
272
273         timeout_set(data, &(struct timeval){5, rand() % 100000});
274 }
275
276 void handle_meta_connection_data(connection_t *c) {
277         if (!receive_meta(c)) {
278                 terminate_connection(c, c->status.active);
279                 return;
280         }
281 }
282
283 #ifndef HAVE_MINGW
284 static void sigterm_handler(void *data) {
285         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
286         event_exit();
287 }
288
289 static void sighup_handler(void *data) {
290         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
291         reopenlogger();
292         if(reload_configuration())
293                 exit(1);
294 }
295
296 static void sigalrm_handler(void *data) {
297         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
298         retry();
299 }
300 #endif
301
302 int reload_configuration(void) {
303         char *fname = NULL;
304
305         /* Reread our own configuration file */
306
307         exit_configuration(&config_tree);
308         init_configuration(&config_tree);
309
310         if(!read_server_config()) {
311                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file.");
312                 return EINVAL;
313         }
314
315         read_config_options(config_tree, NULL);
316
317         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, myself->name);
318         read_config_file(config_tree, fname);
319         free(fname);
320
321         /* Parse some options that are allowed to be changed while tinc is running */
322
323         setup_myself_reloadable();
324
325         /* Try to make outgoing connections */
326
327         try_outgoing_connections();
328
329         /* Close connections to hosts that have a changed or deleted host config file */
330
331         for list_each(connection_t, c, connection_list) {
332                 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
333                 struct stat s;
334                 if(stat(fname, &s) || s.st_mtime > last_config_check) {
335                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name);
336                         terminate_connection(c, c->status.active);
337                 }
338                 free(fname);
339         }
340
341         last_config_check = now.tv_sec;
342
343         return 0;
344 }
345
346 void retry(void) {
347         /* Reset the reconnection timers for all outgoing connections */
348         for list_each(outgoing_t, outgoing, outgoing_list) {
349                 outgoing->timeout = 0;
350                 if(outgoing->ev.cb)
351                         timeout_set(&outgoing->ev, &(struct timeval){0, 0});
352         }
353
354         /* Check for outgoing connections that are in progress, and reset their ping timers */
355         for list_each(connection_t, c, connection_list) {
356                 if(c->outgoing && !c->node)
357                         c->last_ping_time = 0;
358         }
359
360         /* Kick the ping timeout handler */
361         timeout_set(&pingtimer, &(struct timeval){0, 0});
362 }
363
364 /*
365   this is where it all happens...
366 */
367 int main_loop(void) {
368         timeout_add(&pingtimer, timeout_handler, &pingtimer, &(struct timeval){pingtimeout, rand() % 100000});
369         timeout_add(&periodictimer, periodic_handler, &periodictimer, &(struct timeval){pingtimeout, rand() % 100000});
370
371 #ifndef HAVE_MINGW
372         signal_t sighup = {0};
373         signal_t sigterm = {0};
374         signal_t sigquit = {0};
375         signal_t sigint = {0};
376         signal_t sigalrm = {0};
377
378         signal_add(&sighup, sighup_handler, &sighup, SIGHUP);
379         signal_add(&sigterm, sigterm_handler, &sigterm, SIGTERM);
380         signal_add(&sigquit, sigterm_handler, &sigquit, SIGQUIT);
381         signal_add(&sigint, sigterm_handler, &sigint, SIGINT);
382         signal_add(&sigalrm, sigalrm_handler, &sigalrm, SIGALRM);
383 #endif
384
385         if(!event_loop()) {
386                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
387                 return 1;
388         }
389
390 #ifndef HAVE_MINGW
391         signal_del(&sighup);
392         signal_del(&sigterm);
393         signal_del(&sigquit);
394         signal_del(&sigint);
395         signal_del(&sigalrm);
396 #endif
397
398         timeout_del(&periodictimer);
399         timeout_del(&pingtimer);
400
401         return 0;
402 }