3eec9a647b5e3b2e77116323b05637b5dcdab8cf
[oweals/tinc.git] / src / net.c
1 /*
2     net.c -- most of the network code
3     Copyright (C) 1998-2005 Ivo Timmermans,
4                   2000-2010 Guus Sliepen <guus@tinc-vpn.org>
5                   2006      Scott Lamb <slamb@slamb.org>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "system.h"
23
24 #include "utils.h"
25 #include "splay_tree.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 "net.h"
33 #include "netutl.h"
34 #include "process.h"
35 #include "protocol.h"
36 #include "subnet.h"
37 #include "xalloc.h"
38
39 int contradicting_add_edge = 0;
40 int contradicting_del_edge = 0;
41 bool running = true;
42
43 /* Purge edges and subnets of unreachable nodes. Use carefully. */
44
45 void purge(void) {
46         splay_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
47         node_t *n;
48         edge_t *e;
49         subnet_t *s;
50
51         ifdebug(PROTOCOL) logger(LOG_DEBUG, "Purging unreachable nodes");
52
53         /* Remove all edges and subnets owned by unreachable nodes. */
54
55         for(nnode = node_tree->head; nnode; nnode = nnext) {
56                 nnext = nnode->next;
57                 n = nnode->data;
58
59                 if(!n->status.reachable) {
60                         ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Purging node %s (%s)", n->name,
61                                            n->hostname);
62
63                         for(snode = n->subnet_tree->head; snode; snode = snext) {
64                                 snext = snode->next;
65                                 s = snode->data;
66                                 send_del_subnet(broadcast, s);
67                                 if(!strictsubnets)
68                                         subnet_del(n, s);
69                         }
70
71                         for(enode = n->edge_tree->head; enode; enode = enext) {
72                                 enext = enode->next;
73                                 e = enode->data;
74                                 if(!tunnelserver)
75                                         send_del_edge(broadcast, e);
76                                 edge_del(e);
77                         }
78                 }
79         }
80
81         /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
82
83         for(nnode = node_tree->head; nnode; nnode = nnext) {
84                 nnext = nnode->next;
85                 n = nnode->data;
86
87                 if(!n->status.reachable) {
88                         for(enode = edge_weight_tree->head; enode; enode = enext) {
89                                 enext = enode->next;
90                                 e = enode->data;
91
92                                 if(e->to == n)
93                                         break;
94                         }
95
96                         if(!enode && (!strictsubnets || !n->subnet_tree->head))
97                                 /* in strictsubnets mode do not delete nodes with subnets */
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         ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Closing connection with %s (%s)",
112                            c->name, c->hostname);
113
114         c->status.active = false;
115
116         if(c->node)
117                 c->node->connection = NULL;
118
119         if(c->socket)
120                 closesocket(c->socket);
121
122         if(c->edge) {
123                 if(report && !tunnelserver)
124                         send_del_edge(broadcast, c->edge);
125
126                 edge_del(c->edge);
127
128                 /* Run MST and SSSP algorithms */
129
130                 graph();
131
132                 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
133
134                 if(report && !c->node->status.reachable) {
135                         edge_t *e;
136                         e = lookup_edge(c->node, myself);
137                         if(e) {
138                                 if(!tunnelserver)
139                                         send_del_edge(broadcast, e);
140                                 edge_del(e);
141                         }
142                 }
143         }
144
145         /* Check if this was our outgoing connection */
146
147         if(c->outgoing)
148                 retry_outgoing(c->outgoing);
149
150         connection_del(c);
151 }
152
153 /*
154   Check if the other end is active.
155   If we have sent packets, but didn't receive any,
156   then possibly the other end is dead. We send a
157   PING request over the meta connection. If the other
158   end does not reply in time, we consider them dead
159   and close the connection.
160 */
161 static void timeout_handler(void *arg) {
162         event_t *event = arg;
163         splay_node_t *node, *next;
164         connection_t *c;
165         time_t now = time(NULL);
166
167         for(node = connection_tree->head; node; node = next) {
168                 next = node->next;
169                 c = node->data;
170
171                 if(c->last_ping_time + pingtimeout < now) {
172                         if(c->status.active) {
173                                 if(c->status.pinged) {
174                                         ifdebug(CONNECTIONS) logger(LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds",
175                                                            c->name, c->hostname, now - c->last_ping_time);
176                                         terminate_connection(c, true);
177                                         continue;
178                                 } else if(c->last_ping_time + pinginterval < now) {
179                                         send_ping(c);
180                                 }
181                         } else {
182                                 if(c->status.connecting) {
183                                         ifdebug(CONNECTIONS)
184                                                 logger(LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname);
185                                         c->status.connecting = false;
186                                         closesocket(c->socket);
187                                         do_outgoing_connection(c);
188                                 } else {
189                                         ifdebug(CONNECTIONS) logger(LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
190                                         terminate_connection(c, false);
191                                         continue;
192                                 }
193                         }
194                 }
195         }
196
197         if(contradicting_del_edge && contradicting_add_edge) {
198                 logger(LOG_WARNING, "Possible node with same Name as us!");
199
200                 if(rand() % 3 == 0) {
201                         logger(LOG_ERR, "Shutting down, check configuration of all nodes for duplicate Names!");
202                         abort();
203                         return;
204                 }
205
206                 contradicting_add_edge = 0;
207                 contradicting_del_edge = 0;
208         }
209
210         event->time = now + pingtimeout;
211         event_add(event);
212 }
213
214 void handle_meta_connection_data(void *data) {
215         connection_t *c = data;
216         int result;
217         socklen_t len = sizeof result;
218
219         if(c->status.connecting) {
220                 c->status.connecting = false;
221
222                 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
223
224                 if(!result) {
225                         mutex_lock(&mutex);
226                         finish_connecting(c);
227                         mutex_unlock(&mutex);
228                 } else {
229                         ifdebug(CONNECTIONS) logger(LOG_DEBUG,
230                                            "Error while connecting to %s (%s): %s",
231                                            c->name, c->hostname, sockstrerror(result));
232                         closesocket(c->socket);
233                         mutex_lock(&mutex);
234                         do_outgoing_connection(c);
235                         mutex_unlock(&mutex);
236                         return;
237                 }
238         }
239
240         while(true) {
241                 if (!receive_meta(c)) {
242                         terminate_connection(c, c->status.active);
243                         break;
244                 }
245         }
246 }
247
248 int reload_configuration(void) {
249         connection_t *c;
250         splay_node_t *node, *next;
251         char *fname;
252         struct stat s;
253         static time_t last_config_check = 0;
254
255         /* Reread our own configuration file */
256
257         exit_configuration(&config_tree);
258         init_configuration(&config_tree);
259
260         if(!read_server_config()) {
261                 logger(LOG_ERR, "Unable to reread configuration file, exitting.");
262                 abort();
263                 return EINVAL;
264         }
265
266         /* Close connections to hosts that have a changed or deleted host config file */
267         
268         for(node = connection_tree->head; node; node = next) {
269                 c = node->data;
270                 next = node->next;
271                 
272                 if(c->outgoing) {
273                         free(c->outgoing->name);
274                         if(c->outgoing->ai)
275                                 freeaddrinfo(c->outgoing->ai);
276                         free(c->outgoing);
277                         c->outgoing = NULL;
278                 }
279                 
280                 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
281                 if(stat(fname, &s) || s.st_mtime > last_config_check)
282                         terminate_connection(c, c->status.active);
283                 free(fname);
284         }
285
286         last_config_check = time(NULL);
287
288         /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
289
290         if(strictsubnets) {
291                 subnet_t *subnet;
292
293
294                 for(node = subnet_tree->head; node; node = node->next) {
295                         subnet = node->data;
296                         subnet->expires = 1;
297                 }
298
299                 load_all_subnets();
300
301                 for(node = subnet_tree->head; node; node = next) {
302                         next = node->next;
303                         subnet = node->data;
304                         if(subnet->expires == 1) {
305                                 send_del_subnet(broadcast, subnet);
306                                 if(subnet->owner->status.reachable)
307                                         subnet_update(subnet->owner, subnet, false);
308                                 subnet_del(subnet->owner, subnet);
309                         } else if(subnet->expires == -1) {
310                                 subnet->expires = 0;
311                         } else {
312                                 send_add_subnet(broadcast, subnet);
313                                 if(subnet->owner->status.reachable)
314                                         subnet_update(subnet->owner, subnet, true);
315                         }
316                 }
317         }
318
319         /* Try to make outgoing connections */
320         
321         try_outgoing_connections();
322
323         return 0;
324 }
325
326 void retry(void) {
327         connection_t *c;
328         splay_node_t *node;
329
330         for(node = connection_tree->head; node; node = node->next) {
331                 c = node->data;
332                 
333                 if(c->outgoing && !c->node) {
334                         event_del(&c->outgoing->ev);
335                         if(c->status.connecting)
336                                 close(c->socket);
337                         c->outgoing->timeout = 0;
338                         do_outgoing_connection(c);
339                 }
340         }
341 }
342
343 /*
344   this is where it all happens...
345 */
346 int main_loop(void) {
347         struct event timeout_event;
348
349         timeout_event.time = time(NULL) + pingtimeout;
350         timeout_event.handler = timeout_handler;
351         timeout_event.data = &timeout_event;
352
353         event_add(&timeout_event);
354
355
356         while(true) {
357                 mutex_unlock(&mutex);
358                 usleep(1000000);
359                 mutex_lock(&mutex);
360
361                 struct event *event;
362                 while((event = get_expired_event())) {
363                         event->handler(event->data);
364                 }
365         }
366
367         event_del(&timeout_event);
368
369         return 0;
370 }