Fix retrying outgoing connections.
[tinc] / src / net.c
1 /*
2     net.c -- most of the network code
3     Copyright (C) 1998-2005 Ivo Timmermans,
4                   2000-2006 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id$
21 */
22
23 #include "system.h"
24
25 #include <openssl/rand.h>
26
27 #include "utils.h"
28 #include "splay_tree.h"
29 #include "conf.h"
30 #include "connection.h"
31 #include "device.h"
32 #include "graph.h"
33 #include "logger.h"
34 #include "meta.h"
35 #include "net.h"
36 #include "netutl.h"
37 #include "process.h"
38 #include "protocol.h"
39 #include "subnet.h"
40 #include "xalloc.h"
41
42 /* Purge edges and subnets of unreachable nodes. Use carefully. */
43
44 static void purge(void) {
45         splay_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
46         node_t *n;
47         edge_t *e;
48         subnet_t *s;
49
50         cp();
51
52         ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
53
54         /* Remove all edges and subnets owned by unreachable nodes. */
55
56         for(nnode = node_tree->head; nnode; nnode = nnext) {
57                 nnext = nnode->next;
58                 n = nnode->data;
59
60                 if(!n->status.reachable) {
61                         ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
62                                            n->hostname);
63
64                         for(snode = n->subnet_tree->head; snode; snode = snext) {
65                                 snext = snode->next;
66                                 s = snode->data;
67                                 if(!tunnelserver)
68                                         send_del_subnet(broadcast, s);
69                                 subnet_del(n, s);
70                         }
71
72                         for(enode = n->edge_tree->head; enode; enode = enext) {
73                                 enext = enode->next;
74                                 e = enode->data;
75                                 if(!tunnelserver)
76                                         send_del_edge(broadcast, e);
77                                 edge_del(e);
78                         }
79                 }
80         }
81
82         /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
83
84         for(nnode = node_tree->head; nnode; nnode = nnext) {
85                 nnext = nnode->next;
86                 n = nnode->data;
87
88                 if(!n->status.reachable) {
89                         for(enode = edge_weight_tree->head; enode; enode = enext) {
90                                 enext = enode->next;
91                                 e = enode->data;
92
93                                 if(e->to == n)
94                                         break;
95                         }
96
97                         if(!enode)
98                                 node_del(n);
99                 }
100         }
101 }
102
103 /*
104   Terminate a connection:
105   - Close the socket
106   - Remove associated edge and tell other connections about it if report = true
107   - Check if we need to retry making an outgoing connection
108   - Deactivate the host
109 */
110 void terminate_connection(connection_t *c, bool report) {
111         cp();
112
113         ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
114                            c->name, c->hostname);
115
116         c->status.active = false;
117
118         if(c->node)
119                 c->node->connection = NULL;
120
121         if(c->socket)
122                 closesocket(c->socket);
123
124         event_del(&c->ev);
125
126         if(c->edge) {
127                 if(report && !tunnelserver)
128                         send_del_edge(broadcast, c->edge);
129
130                 edge_del(c->edge);
131
132                 /* Run MST and SSSP algorithms */
133
134                 graph();
135
136                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
137
138                 if(report && !c->node->status.reachable) {
139                         edge_t *e;
140                         e = lookup_edge(c->node, myself);
141                         if(e) {
142                                 if(!tunnelserver)
143                                         send_del_edge(broadcast, e);
144                                 edge_del(e);
145                         }
146                 }
147         }
148
149         free(c->outbuf);
150         c->outbuf = NULL;
151         c->outbuflen = 0;
152         c->outbufsize = 0;
153         c->outbufstart = 0;
154
155         /* Check if this was our outgoing connection */
156
157         if(c->outgoing)
158                 retry_outgoing(c->outgoing);
159
160         connection_del(c);
161 }
162
163 /*
164   Check if the other end is active.
165   If we have sent packets, but didn't receive any,
166   then possibly the other end is dead. We send a
167   PING request over the meta connection. If the other
168   end does not reply in time, we consider them dead
169   and close the connection.
170 */
171 static void timeout_handler(int fd, short events, void *event) {
172         splay_node_t *node, *next;
173         connection_t *c;
174         time_t now = time(NULL);
175
176         cp();
177
178         for(node = connection_tree->head; node; node = next) {
179                 next = node->next;
180                 c = node->data;
181
182                 if(c->last_ping_time + pingtimeout < now) {
183                         if(c->status.active) {
184                                 if(c->status.pinged) {
185                                         ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
186                                                            c->name, c->hostname, now - c->last_ping_time);
187                                         terminate_connection(c, true);
188                                         continue;
189                                 } else if(c->last_ping_time + pinginterval < now) {
190                                         send_ping(c);
191                                 }
192                         } else {
193                                 if(c->status.connecting) {
194                                         ifdebug(CONNECTIONS)
195                                                 logger(LOG_WARNING, _("Timeout while connecting to %s (%s)"), c->name, c->hostname);
196                                         c->status.connecting = false;
197                                         closesocket(c->socket);
198                                         do_outgoing_connection(c);
199                                 } else {
200                                         ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"), c->name, c->hostname);
201                                         terminate_connection(c, false);
202                                         continue;
203                                 }
204                         }
205                 }
206         }
207
208         event_add(event, &(struct timeval){pingtimeout, 0});
209 }
210
211 void handle_meta_connection_data(int fd, short events, void *data) {
212         connection_t *c = data;
213         int result;
214         socklen_t len = sizeof(result);
215
216         if(c->status.connecting) {
217                 c->status.connecting = false;
218
219                 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
220
221                 if(!result)
222                         finish_connecting(c);
223                 else {
224                         ifdebug(CONNECTIONS) logger(LOG_DEBUG,
225                                            _("Error while connecting to %s (%s): %s"),
226                                            c->name, c->hostname, strerror(result));
227                         closesocket(c->socket);
228                         do_outgoing_connection(c);
229                         return;
230                 }
231         }
232
233         if (!receive_meta(c)) {
234                 terminate_connection(c, c->status.active);
235                 return;
236         }
237 }
238
239 static void sigterm_handler(int signal, short events, void *data) {
240         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
241         event_loopexit(NULL);
242 }
243
244 static void sigint_handler(int signal, short events, void *data) {
245         static int saved_debug_level = -1;
246
247         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
248
249         if(saved_debug_level != -1) {
250                 logger(LOG_NOTICE, _("Reverting to old debug level (%d)"),
251                         saved_debug_level);
252                 debug_level = saved_debug_level;
253                 saved_debug_level = -1;
254         } else {
255                 logger(LOG_NOTICE,
256                         _("Temporarily setting debug level to 5.  Kill me with SIGINT again to go back to level %d."),
257                         debug_level);
258                 saved_debug_level = debug_level;
259                 debug_level = 5;
260         }
261 }
262
263 static void sigusr1_handler(int signal, short events, void *data) {
264         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
265         dump_connections();
266 }
267
268 static void sigusr2_handler(int signal, short events, void *data) {
269         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
270         dump_device_stats();
271         dump_nodes();
272         dump_edges();
273         dump_subnets();
274 }
275
276 static void sigwinch_handler(int signal, short events, void *data) {
277         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
278         purge();
279 }
280
281 static void sighup_handler(int signal, short events, void *data) {
282         connection_t *c;
283         splay_node_t *node;
284         char *fname;
285         struct stat s;
286         static time_t last_config_check = 0;
287         
288         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
289
290         /* Reread our own configuration file */
291
292         exit_configuration(&config_tree);
293         init_configuration(&config_tree);
294
295         if(!read_server_config()) {
296                 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
297                 event_loopexit(NULL);
298                 return;
299         }
300
301         /* Close connections to hosts that have a changed or deleted host config file */
302         
303         for(node = connection_tree->head; node; node = node->next) {
304                 c = node->data;
305                 
306                 if(c->outgoing) {
307                         free(c->outgoing->name);
308                         if(c->outgoing->ai)
309                                 freeaddrinfo(c->outgoing->ai);
310                         free(c->outgoing);
311                         c->outgoing = NULL;
312                 }
313                 
314                 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
315                 if(stat(fname, &s) || s.st_mtime > last_config_check)
316                         terminate_connection(c, c->status.active);
317                 free(fname);
318         }
319
320         last_config_check = time(NULL);
321
322         /* Try to make outgoing connections */
323         
324         try_outgoing_connections();
325 }
326
327 static void sigalrm_handler(int signal, short events, void *data) {
328         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
329
330         connection_t *c;
331         splay_node_t *node;
332
333         for(node = connection_tree->head; node; node = node->next) {
334                 c = node->data;
335                 
336                 if(c->outgoing && !c->node) {
337                         if(timeout_initialized(&c->outgoing->ev))
338                                 event_del(&c->outgoing->ev);
339                         if(c->status.connecting)
340                                 close(c->socket);
341                         c->outgoing->timeout = 0;
342                         do_outgoing_connection(c);
343                 }
344         }
345 }
346
347 /*
348   this is where it all happens...
349 */
350 int main_loop(void) {
351         struct event timeout_event;
352         struct event sighup_event;
353         struct event sigint_event;
354         struct event sigterm_event;
355         struct event sigquit_event;
356         struct event sigusr1_event;
357         struct event sigusr2_event;
358         struct event sigwinch_event;
359         struct event sigalrm_event;
360
361         cp();
362
363         timeout_set(&timeout_event, timeout_handler, &timeout_event);
364         event_add(&timeout_event, &(struct timeval){pingtimeout, 0});
365         signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
366         signal_add(&sighup_event, NULL);
367         signal_set(&sigint_event, SIGINT, sigint_handler, NULL);
368         signal_add(&sigint_event, NULL);
369         signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
370         signal_add(&sigterm_event, NULL);
371         signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
372         signal_add(&sigquit_event, NULL);
373         signal_set(&sigusr1_event, SIGUSR1, sigusr1_handler, NULL);
374         signal_add(&sigusr1_event, NULL);
375         signal_set(&sigusr2_event, SIGUSR2, sigusr2_handler, NULL);
376         signal_add(&sigusr2_event, NULL);
377         signal_set(&sigwinch_event, SIGWINCH, sigwinch_handler, NULL);
378         signal_add(&sigwinch_event, NULL);
379         signal_set(&sigalrm_event, SIGALRM, sigalrm_handler, NULL);
380         signal_add(&sigalrm_event, NULL);
381
382         if(event_loop(0) < 0) {
383                 logger(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
384                 return 1;
385         }
386
387         signal_del(&sighup_event);
388         signal_del(&sigint_event);
389         signal_del(&sigterm_event);
390         signal_del(&sigquit_event);
391         signal_del(&sigusr1_event);
392         signal_del(&sigusr2_event);
393         signal_del(&sigwinch_event);
394         signal_del(&sigalrm_event);
395         event_del(&timeout_event);
396
397         return 0;
398 }