Purge through the control socket
[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 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         if(c->edge) {
125                 if(report && !tunnelserver)
126                         send_del_edge(broadcast, c->edge);
127
128                 edge_del(c->edge);
129
130                 /* Run MST and SSSP algorithms */
131
132                 graph();
133
134                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
135
136                 if(report && !c->node->status.reachable) {
137                         edge_t *e;
138                         e = lookup_edge(c->node, myself);
139                         if(e) {
140                                 if(!tunnelserver)
141                                         send_del_edge(broadcast, e);
142                                 edge_del(e);
143                         }
144                 }
145         }
146
147         /* Check if this was our outgoing connection */
148
149         if(c->outgoing)
150                 retry_outgoing(c->outgoing);
151
152         connection_del(c);
153 }
154
155 /*
156   Check if the other end is active.
157   If we have sent packets, but didn't receive any,
158   then possibly the other end is dead. We send a
159   PING request over the meta connection. If the other
160   end does not reply in time, we consider them dead
161   and close the connection.
162 */
163 static void timeout_handler(int fd, short events, void *event) {
164         splay_node_t *node, *next;
165         connection_t *c;
166         time_t now = time(NULL);
167
168         cp();
169
170         for(node = connection_tree->head; node; node = next) {
171                 next = node->next;
172                 c = node->data;
173
174                 if(c->last_ping_time + pingtimeout < now) {
175                         if(c->status.active) {
176                                 if(c->status.pinged) {
177                                         ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
178                                                            c->name, c->hostname, now - c->last_ping_time);
179                                         terminate_connection(c, true);
180                                         continue;
181                                 } else if(c->last_ping_time + pinginterval < now) {
182                                         send_ping(c);
183                                 }
184                         } else {
185                                 if(c->status.connecting) {
186                                         ifdebug(CONNECTIONS)
187                                                 logger(LOG_WARNING, _("Timeout while connecting to %s (%s)"), c->name, c->hostname);
188                                         c->status.connecting = false;
189                                         closesocket(c->socket);
190                                         do_outgoing_connection(c);
191                                 } else {
192                                         ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"), c->name, c->hostname);
193                                         terminate_connection(c, false);
194                                         continue;
195                                 }
196                         }
197                 }
198         }
199
200         event_add(event, &(struct timeval){pingtimeout, 0});
201 }
202
203 void handle_meta_connection_data(int fd, short events, void *data) {
204         connection_t *c = data;
205         int result;
206         socklen_t len = sizeof(result);
207
208         if(c->status.connecting) {
209                 c->status.connecting = false;
210
211                 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
212
213                 if(!result)
214                         finish_connecting(c);
215                 else {
216                         ifdebug(CONNECTIONS) logger(LOG_DEBUG,
217                                            _("Error while connecting to %s (%s): %s"),
218                                            c->name, c->hostname, strerror(result));
219                         closesocket(c->socket);
220                         do_outgoing_connection(c);
221                         return;
222                 }
223         }
224
225         if (!receive_meta(c)) {
226                 terminate_connection(c, c->status.active);
227                 return;
228         }
229 }
230
231 static void sigterm_handler(int signal, short events, void *data) {
232         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
233         event_loopexit(NULL);
234 }
235
236 static void sigint_handler(int signal, short events, void *data) {
237         static int saved_debug_level = -1;
238
239         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
240
241         if(saved_debug_level != -1) {
242                 logger(LOG_NOTICE, _("Reverting to old debug level (%d)"),
243                         saved_debug_level);
244                 debug_level = saved_debug_level;
245                 saved_debug_level = -1;
246         } else {
247                 logger(LOG_NOTICE,
248                         _("Temporarily setting debug level to 5.  Kill me with SIGINT again to go back to level %d."),
249                         debug_level);
250                 saved_debug_level = debug_level;
251                 debug_level = 5;
252         }
253 }
254
255 static void sighup_handler(int signal, short events, void *data) {
256         connection_t *c;
257         splay_node_t *node, *next;
258         char *fname;
259         struct stat s;
260         static time_t last_config_check = 0;
261         
262         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
263
264         /* Reread our own configuration file */
265
266         exit_configuration(&config_tree);
267         init_configuration(&config_tree);
268
269         if(!read_server_config()) {
270                 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
271                 event_loopexit(NULL);
272                 return;
273         }
274
275         /* Close connections to hosts that have a changed or deleted host config file */
276         
277         for(node = connection_tree->head; node; node = next) {
278                 c = node->data;
279                 next = node->next;
280                 
281                 if(c->outgoing) {
282                         free(c->outgoing->name);
283                         if(c->outgoing->ai)
284                                 freeaddrinfo(c->outgoing->ai);
285                         free(c->outgoing);
286                         c->outgoing = NULL;
287                 }
288                 
289                 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
290                 if(stat(fname, &s) || s.st_mtime > last_config_check)
291                         terminate_connection(c, c->status.active);
292                 free(fname);
293         }
294
295         last_config_check = time(NULL);
296
297         /* Try to make outgoing connections */
298         
299         try_outgoing_connections();
300 }
301
302 static void sigalrm_handler(int signal, short events, void *data) {
303         logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
304
305         connection_t *c;
306         splay_node_t *node;
307
308         for(node = connection_tree->head; node; node = node->next) {
309                 c = node->data;
310                 
311                 if(c->outgoing && !c->node) {
312                         if(timeout_initialized(&c->outgoing->ev))
313                                 event_del(&c->outgoing->ev);
314                         if(c->status.connecting)
315                                 close(c->socket);
316                         c->outgoing->timeout = 0;
317                         do_outgoing_connection(c);
318                 }
319         }
320 }
321
322 /*
323   this is where it all happens...
324 */
325 int main_loop(void) {
326         struct event timeout_event;
327         struct event sighup_event;
328         struct event sigint_event;
329         struct event sigterm_event;
330         struct event sigquit_event;
331         struct event sigalrm_event;
332
333         cp();
334
335         timeout_set(&timeout_event, timeout_handler, &timeout_event);
336         event_add(&timeout_event, &(struct timeval){pingtimeout, 0});
337         signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
338         signal_add(&sighup_event, NULL);
339         signal_set(&sigint_event, SIGINT, sigint_handler, NULL);
340         signal_add(&sigint_event, NULL);
341         signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
342         signal_add(&sigterm_event, NULL);
343         signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
344         signal_add(&sigquit_event, NULL);
345         signal_set(&sigalrm_event, SIGALRM, sigalrm_handler, NULL);
346         signal_add(&sigalrm_event, NULL);
347
348         if(event_loop(0) < 0) {
349                 logger(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
350                 return 1;
351         }
352
353         signal_del(&sighup_event);
354         signal_del(&sigint_event);
355         signal_del(&sigterm_event);
356         signal_del(&sigquit_event);
357         signal_del(&sigalrm_event);
358         event_del(&timeout_event);
359
360         return 0;
361 }