Check for writability when waiting for a socket to finish connecting.
[tinc] / 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 "device.h"
29 #include "graph.h"
30 #include "logger.h"
31 #include "meta.h"
32 #include "names.h"
33 #include "net.h"
34 #include "netutl.h"
35 #include "process.h"
36 #include "protocol.h"
37 #include "subnet.h"
38 #include "xalloc.h"
39
40 int contradicting_add_edge = 0;
41 int contradicting_del_edge = 0;
42 static int sleeptime = 10;
43 time_t last_config_check = 0;
44
45 /* Purge edges and subnets of unreachable nodes. Use carefully. */
46
47 void purge(void) {
48         logger(DEBUG_PROTOCOL, LOG_DEBUG, "Purging unreachable nodes");
49
50         /* Remove all edges and subnets 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(subnet_t, s, n->subnet_tree) {
57                                 send_del_subnet(everyone, s);
58                                 if(!strictsubnets)
59                                         subnet_del(n, s);
60                         }
61
62                         for splay_each(edge_t, e, n->edge_tree) {
63                                 if(!tunnelserver)
64                                         send_del_edge(everyone, e);
65                                 edge_del(e);
66                         }
67                 }
68         }
69
70         /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
71
72         for splay_each(node_t, n, node_tree) {
73                 if(!n->status.reachable) {
74                         for splay_each(edge_t, e, edge_weight_tree)
75                                 if(e->to == n)
76                                         return;
77
78                         if(!autoconnect && (!strictsubnets || !n->subnet_tree->head))
79                                 /* in strictsubnets mode do not delete nodes with subnets */
80                                 node_del(n);
81                 }
82         }
83 }
84
85 /*
86   Terminate a connection:
87   - Mark it as inactive
88   - Remove the edge representing this connection
89   - Kill it with fire
90   - Check if we need to retry making an outgoing connection
91 */
92 void terminate_connection(connection_t *c, bool report) {
93         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname);
94
95         c->status.active = false;
96
97         if(c->node && c->node->connection == c)
98                 c->node->connection = NULL;
99
100         if(c->edge) {
101                 if(report && !tunnelserver)
102                         send_del_edge(everyone, c->edge);
103
104                 edge_del(c->edge);
105                 c->edge = NULL;
106
107                 /* Run MST and SSSP algorithms */
108
109                 graph();
110
111                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
112
113                 if(report && !c->node->status.reachable) {
114                         edge_t *e;
115                         e = lookup_edge(c->node, myself);
116                         if(e) {
117                                 if(!tunnelserver)
118                                         send_del_edge(everyone, e);
119                                 edge_del(e);
120                         }
121                 }
122         }
123
124         outgoing_t *outgoing = c->outgoing;
125         connection_del(c);
126
127         /* Check if this was our outgoing connection */
128
129         if(outgoing)
130                 do_outgoing_connection(outgoing);
131 }
132
133 /*
134   Check if the other end is active.
135   If we have sent packets, but didn't receive any,
136   then possibly the other end is dead. We send a
137   PING request over the meta connection. If the other
138   end does not reply in time, we consider them dead
139   and close the connection.
140 */
141 static void timeout_handler(void *data) {
142         for list_each(connection_t, c, connection_list) {
143                 if(c->status.control)
144                         continue;
145
146                 if(c->last_ping_time + pingtimeout <= now.tv_sec) {
147                         if(c->status.active) {
148                                 if(c->status.pinged) {
149                                         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);
150                                 } else if(c->last_ping_time + pinginterval <= now.tv_sec) {
151                                         send_ping(c);
152                                         continue;
153                                 } else {
154                                         continue;
155                                 }
156                         } else {
157                                 if(c->status.connecting)
158                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
159                                 else
160                                         logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
161                         }
162                         terminate_connection(c, c->status.active);
163                 }
164         }
165
166         timeout_set(data, &(struct timeval){pingtimeout, rand() % 100000});
167 }
168
169 static void periodic_handler(void *data) {
170         /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
171            This usually only happens when another node has the same Name as this node.
172            If so, sleep for a short while to prevent a storm of contradicting messages.
173         */
174
175         if(contradicting_del_edge > 100 && contradicting_add_edge > 100) {
176                 logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime);
177                 usleep(sleeptime * 1000000LL);
178                 sleeptime *= 2;
179                 if(sleeptime < 0)
180                         sleeptime = 3600;
181         } else {
182                 sleeptime /= 2;
183                 if(sleeptime < 10)
184                         sleeptime = 10;
185         }
186
187         contradicting_add_edge = 0;
188         contradicting_del_edge = 0;
189
190         /* If AutoConnect is set, check if we need to make or break connections. */
191
192         if(autoconnect && node_tree->count > 1) {
193                 /* Count number of active connections */
194                 int nc = 0;
195                 for list_each(connection_t, c, connection_list) {
196                         if(c->status.active && !c->status.control)
197                                 nc++;
198                 }
199
200                 if(nc < autoconnect) {
201                         /* Not enough active connections, try to add one.
202                            Choose a random node, if we don't have a connection to it,
203                            and we are not already trying to make one, create an
204                            outgoing connection to this node.
205                         */
206                         int r = rand() % node_tree->count;
207                         int i = 0;
208
209                         for splay_each(node_t, n, node_tree) {
210                                 if(i++ != r)
211                                         continue;
212
213                                 if(n->connection)
214                                         break;
215
216                                 bool found = false;
217
218                                 for list_each(outgoing_t, outgoing, outgoing_list) {
219                                         if(!strcmp(outgoing->name, n->name)) {
220                                                 found = true;
221                                                 break;
222                                         }
223                                 }
224
225                                 if(!found) {
226                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
227                                         outgoing_t *outgoing = xmalloc_and_zero(sizeof *outgoing);
228                                         outgoing->name = xstrdup(n->name);
229                                         list_insert_tail(outgoing_list, outgoing);
230                                         setup_outgoing_connection(outgoing);
231                                 }
232                                 break;
233                         }
234                 } else if(nc > autoconnect) {
235                         /* Too many active connections, try to remove one.
236                            Choose a random outgoing connection to a node
237                            that has at least one other connection.
238                         */
239                         int r = rand() % nc;
240                         int i = 0;
241
242                         for list_each(connection_t, c, connection_list) {
243                                 if(!c->status.active || c->status.control)
244                                         continue;
245
246                                 if(i++ != r)
247                                         continue;
248
249                                 if(!c->outgoing || !c->node || c->node->edge_tree->count < 2)
250                                         break;
251
252                                 logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
253                                 list_delete(outgoing_list, c->outgoing);
254                                 c->outgoing = NULL;
255                                 terminate_connection(c, c->status.active);
256                                 break;
257                         }
258                 }
259
260                 if(nc >= autoconnect) {
261                         /* If we have enough active connections,
262                            remove any pending outgoing connections.
263                         */
264                         for list_each(outgoing_t, o, outgoing_list) {
265                                 bool found = false;
266                                 for list_each(connection_t, c, connection_list) {
267                                         if(c->outgoing == o) {
268                                                 found = true;
269                                                 break;
270                                         }
271                                 }
272                                 if(!found) {
273                                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
274                                         list_delete_node(outgoing_list, node);
275                                 }
276                         }
277                 }
278         }
279
280         timeout_set(data, &(struct timeval){5, rand() % 100000});
281 }
282
283 void handle_meta_connection_data(connection_t *c) {
284         if (!receive_meta(c)) {
285                 terminate_connection(c, c->status.active);
286                 return;
287         }
288 }
289
290 static void sigterm_handler(void *data) {
291         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
292         event_exit();
293 }
294
295 static void sighup_handler(void *data) {
296         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
297         reopenlogger();
298         reload_configuration();
299 }
300
301 static void sigalrm_handler(void *data) {
302         logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s signal", strsignal(((signal_t *)data)->signum));
303         retry();
304 }
305
306 int reload_configuration(void) {
307         char *fname;
308
309         /* Reread our own configuration file */
310
311         exit_configuration(&config_tree);
312         init_configuration(&config_tree);
313
314         if(!read_server_config()) {
315                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file, exitting.");
316                 event_exit();
317                 return EINVAL;
318         }
319
320         read_config_options(config_tree, NULL);
321
322         xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, myself->name);
323         read_config_file(config_tree, fname);
324         free(fname);
325
326         /* Parse some options that are allowed to be changed while tinc is running */
327
328         setup_myself_reloadable();
329
330         /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
331
332         if(strictsubnets) {
333                 for splay_each(subnet_t, subnet, subnet_tree)
334                         subnet->expires = 1;
335
336                 load_all_subnets();
337
338                 for splay_each(subnet_t, subnet, subnet_tree) {
339                         if(subnet->expires == 1) {
340                                 send_del_subnet(everyone, subnet);
341                                 if(subnet->owner->status.reachable)
342                                         subnet_update(subnet->owner, subnet, false);
343                                 subnet_del(subnet->owner, subnet);
344                         } else if(subnet->expires == -1) {
345                                 subnet->expires = 0;
346                         } else {
347                                 send_add_subnet(everyone, subnet);
348                                 if(subnet->owner->status.reachable)
349                                         subnet_update(subnet->owner, subnet, true);
350                         }
351                 }
352         } else { /* Only read our own subnets back in */
353                 for splay_each(subnet_t, subnet, myself->subnet_tree)
354                         if(!subnet->expires)
355                                 subnet->expires = 1;
356
357                 config_t *cfg = lookup_config(config_tree, "Subnet");
358
359                 while(cfg) {
360                         subnet_t *subnet, *s2;
361
362                         if(!get_config_subnet(cfg, &subnet))
363                                 continue;
364
365                         if((s2 = lookup_subnet(myself, subnet))) {
366                                 if(s2->expires == 1)
367                                         s2->expires = 0;
368
369                                 free_subnet(subnet);
370                         } else {
371                                 subnet_add(myself, subnet);
372                                 send_add_subnet(everyone, subnet);
373                                 subnet_update(myself, subnet, true);
374                         }
375
376                         cfg = lookup_config_next(config_tree, cfg);
377                 }
378
379                 for splay_each(subnet_t, subnet, myself->subnet_tree) {
380                         if(subnet->expires == 1) {
381                                 send_del_subnet(everyone, subnet);
382                                 subnet_update(myself, subnet, false);
383                                 subnet_del(myself, subnet);
384                         }
385                 }
386         }
387
388         /* Try to make outgoing connections */
389
390         try_outgoing_connections();
391
392         /* Close connections to hosts that have a changed or deleted host config file */
393
394         for list_each(connection_t, c, connection_list) {
395                 if(c->status.control)
396                         continue;
397
398                 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
399                 struct stat s;
400                 if(stat(fname, &s) || s.st_mtime > last_config_check) {
401                         logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name);
402                         terminate_connection(c, c->status.active);
403                 }
404                 free(fname);
405         }
406
407         last_config_check = time(NULL);
408
409         return 0;
410 }
411
412 void retry(void) {
413         for list_each(connection_t, c, connection_list) {
414                 if(c->outgoing && !c->node) {
415                         timeout_del(&c->outgoing->ev);
416                         if(c->status.connecting)
417                                 close(c->socket);
418                         c->outgoing->timeout = 0;
419                         terminate_connection(c, c->status.active);
420                 }
421         }
422 }
423
424 /*
425   this is where it all happens...
426 */
427 int main_loop(void) {
428         timeout_t pingtimer = {{0}};
429         timeout_t periodictimer = {{0}};
430
431         timeout_add(&pingtimer, timeout_handler, &pingtimer, &(struct timeval){pingtimeout, rand() % 100000});
432         timeout_add(&periodictimer, periodic_handler, &periodictimer, &(struct timeval){pingtimeout, rand() % 100000});
433
434 #ifndef HAVE_MINGW
435         signal_t sighup = {0};
436         signal_t sigterm = {0};
437         signal_t sigquit = {0};
438         signal_t sigint = {0};
439         signal_t sigalrm = {0};
440
441         signal_add(&sighup, sighup_handler, &sighup, SIGHUP);
442         signal_add(&sigterm, sigterm_handler, &sigterm, SIGTERM);
443         signal_add(&sigquit, sigterm_handler, &sigquit, SIGQUIT);
444         signal_add(&sigint, sigterm_handler, &sigint, SIGINT);
445         signal_add(&sigalrm, sigalrm_handler, &sigalrm, SIGALRM);
446 #endif
447
448         if(!event_loop()) {
449                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno));
450                 return 1;
451         }
452
453 #ifndef HAVE_MINGW
454         signal_del(&sighup);
455         signal_del(&sigalrm);
456         signal_del(&sigquit);
457         signal_del(&sigterm);
458 #endif
459
460         timeout_del(&periodictimer);
461         timeout_del(&pingtimer);
462
463         return 0;
464 }