2 net.c -- most of the network code
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2009 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <openssl/rand.h>
28 #include "connection.h"
42 bool do_purge = false;
43 volatile bool running = false;
47 /* Purge edges and subnets of unreachable nodes. Use carefully. */
49 static void purge(void) {
50 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
57 ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
59 /* Remove all edges and subnets owned by unreachable nodes. */
61 for(nnode = node_tree->head; nnode; nnode = nnext) {
65 if(!n->status.reachable) {
66 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
69 for(snode = n->subnet_tree->head; snode; snode = snext) {
73 send_del_subnet(broadcast, s);
77 for(enode = n->edge_tree->head; enode; enode = enext) {
81 send_del_edge(broadcast, e);
87 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
89 for(nnode = node_tree->head; nnode; nnode = nnext) {
93 if(!n->status.reachable) {
94 for(enode = edge_weight_tree->head; enode; enode = enext) {
109 put all file descriptors in an fd_set array
110 While we're at it, purge stuff that needs to be removed.
112 static int build_fdset(fd_set *readset, fd_set *writeset) {
113 avl_node_t *node, *next;
122 for(node = connection_tree->head; node; node = next) {
126 if(c->status.remove) {
128 if(!connection_tree->head)
131 FD_SET(c->socket, readset);
133 FD_SET(c->socket, writeset);
139 for(i = 0; i < listen_sockets; i++) {
140 FD_SET(listen_socket[i].tcp, readset);
141 if(listen_socket[i].tcp > max)
142 max = listen_socket[i].tcp;
143 FD_SET(listen_socket[i].udp, readset);
144 if(listen_socket[i].udp > max)
145 max = listen_socket[i].udp;
149 FD_SET(device_fd, readset);
157 Terminate a connection:
159 - Remove associated edge and tell other connections about it if report = true
160 - Check if we need to retry making an outgoing connection
161 - Deactivate the host
163 void terminate_connection(connection_t *c, bool report) {
169 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
170 c->name, c->hostname);
172 c->status.remove = true;
173 c->status.active = false;
176 c->node->connection = NULL;
179 closesocket(c->socket);
182 if(report && !tunnelserver)
183 send_del_edge(broadcast, c->edge);
187 /* Run MST and SSSP algorithms */
191 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
193 if(report && !c->node->status.reachable) {
195 e = lookup_edge(c->node, myself);
198 send_del_edge(broadcast, e);
204 /* Check if this was our outgoing connection */
207 retry_outgoing(c->outgoing);
219 Check if the other end is active.
220 If we have sent packets, but didn't receive any,
221 then possibly the other end is dead. We send a
222 PING request over the meta connection. If the other
223 end does not reply in time, we consider them dead
224 and close the connection.
226 static void check_dead_connections(void) {
227 avl_node_t *node, *next;
232 for(node = connection_tree->head; node; node = next) {
236 if(c->last_ping_time + pingtimeout < now) {
237 if(c->status.active) {
238 if(c->status.pinged) {
239 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
240 c->name, c->hostname, now - c->last_ping_time);
241 c->status.timeout = true;
242 terminate_connection(c, true);
243 } else if(c->last_ping_time + pinginterval < now) {
247 if(c->status.remove) {
248 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
249 c->name, c->hostname, bitfield_to_int(&c->status, sizeof c->status));
253 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
254 c->name, c->hostname);
255 if(c->status.connecting) {
256 c->status.connecting = false;
257 closesocket(c->socket);
258 do_outgoing_connection(c);
260 terminate_connection(c, false);
265 if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) {
266 if(c->status.active) {
267 ifdebug(CONNECTIONS) logger(LOG_INFO,
268 _("%s (%s) could not flush for %ld seconds (%d bytes remaining)"),
269 c->name, c->hostname, now - c->last_flushed_time, c->outbuflen);
270 c->status.timeout = true;
271 terminate_connection(c, true);
278 check all connections to see if anything
279 happened on their sockets
281 static void check_network_activity(fd_set * readset, fd_set * writeset) {
285 socklen_t len = sizeof(result);
290 /* check input from kernel */
291 if(device_fd >= 0 && FD_ISSET(device_fd, readset)) {
292 if(read_packet(&packet)) {
294 route(myself, &packet);
298 /* check meta connections */
299 for(node = connection_tree->head; node; node = node->next) {
305 if(FD_ISSET(c->socket, readset)) {
306 if(c->status.connecting) {
307 c->status.connecting = false;
308 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
311 finish_connecting(c);
313 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
314 _("Error while connecting to %s (%s): %s"),
315 c->name, c->hostname, strerror(result));
316 closesocket(c->socket);
317 do_outgoing_connection(c);
322 if(!receive_meta(c)) {
323 terminate_connection(c, c->status.active);
328 if(FD_ISSET(c->socket, writeset)) {
330 terminate_connection(c, c->status.active);
336 for(i = 0; i < listen_sockets; i++) {
337 if(FD_ISSET(listen_socket[i].udp, readset))
338 handle_incoming_vpn_data(listen_socket[i].udp);
340 if(FD_ISSET(listen_socket[i].tcp, readset))
341 handle_new_meta_connection(listen_socket[i].tcp);
346 this is where it all happens...
348 int main_loop(void) {
349 fd_set readset, writeset;
352 time_t last_ping_check, last_config_check, last_graph_dump;
357 last_ping_check = now;
358 last_config_check = now;
359 last_graph_dump = now;
368 // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
372 maxfd = build_fdset(&readset, &writeset);
375 LeaveCriticalSection(&mutex);
377 r = select(maxfd + 1, &readset, &writeset, NULL, &tv);
379 EnterCriticalSection(&mutex);
383 if(errno != EINTR && errno != EAGAIN) {
384 logger(LOG_ERR, _("Error while waiting for input: %s"),
394 check_network_activity(&readset, &writeset);
401 /* Let's check if everybody is still alive */
403 if(last_ping_check + pingtimeout < now) {
404 check_dead_connections();
405 last_ping_check = now;
407 if(routing_mode == RMODE_SWITCH)
412 /* Should we regenerate our key? */
414 if(keyexpires < now) {
418 ifdebug(STATUS) logger(LOG_INFO, _("Expiring symmetric keys"));
420 for(node = node_tree->head; node; node = node->next) {
428 send_key_changed(broadcast, myself);
429 keyexpires = now + keylifetime;
434 logger(LOG_INFO, _("Flushing event queue"));
439 while((event = get_expired_event())) {
440 event->handler(event->data);
452 /* Reread our own configuration file */
454 exit_configuration(&config_tree);
455 init_configuration(&config_tree);
457 if(!read_server_config()) {
458 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
462 /* Close connections to hosts that have a changed or deleted host config file */
464 for(node = connection_tree->head; node; node = node->next) {
467 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
468 if(stat(fname, &s) || s.st_mtime > last_config_check)
469 terminate_connection(c, c->status.active);
473 last_config_check = now;
475 /* Try to make outgoing connections */
477 try_outgoing_connections();
480 /* Dump graph if wanted every 60 seconds*/
482 if(last_graph_dump + 60 < now) {
484 last_graph_dump = now;