2 net.c -- most of the network code
3 Copyright (C) 1998-2003 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2003 Guus Sliepen <guus@sliepen.eu.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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: net.c,v 1.35.4.193 2003/07/23 22:17:31 guus Exp $
25 #include <openssl/rand.h>
30 #include "connection.h"
43 bool do_purge = false;
47 /* Purge edges and subnets of unreachable nodes. Use carefully. */
49 static void purge(void)
51 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
58 ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
60 for(nnode = node_tree->head; nnode; nnode = nnext) {
62 n = (node_t *) nnode->data;
64 if(!n->status.reachable) {
65 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
68 for(snode = n->subnet_tree->head; snode; snode = snext) {
70 s = (subnet_t *) snode->data;
71 send_del_subnet(broadcast, s);
75 for(enode = n->edge_tree->head; enode; enode = enext) {
77 e = (edge_t *) enode->data;
78 send_del_edge(broadcast, e);
88 put all file descriptors in an fd_set array
89 While we're at it, purge stuff that needs to be removed.
91 static int build_fdset(fd_set * fs)
93 avl_node_t *node, *next;
101 for(node = connection_tree->head; node; node = next) {
103 c = (connection_t *) node->data;
105 if(c->status.remove) {
107 if(!connection_tree->head)
110 FD_SET(c->socket, fs);
116 for(i = 0; i < listen_sockets; i++) {
117 FD_SET(listen_socket[i].tcp, fs);
118 if(listen_socket[i].tcp > max)
119 max = listen_socket[i].tcp;
120 FD_SET(listen_socket[i].udp, fs);
121 if(listen_socket[i].udp > max)
122 max = listen_socket[i].udp;
125 FD_SET(device_fd, fs);
133 Terminate a connection:
135 - Remove associated edge and tell other connections about it if report = true
136 - Check if we need to retry making an outgoing connection
137 - Deactivate the host
139 void terminate_connection(connection_t *c, bool report)
146 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
147 c->name, c->hostname);
149 c->status.remove = true;
150 c->status.active = false;
153 c->node->connection = NULL;
160 send_del_edge(broadcast, c->edge);
164 /* Run MST and SSSP algorithms */
169 /* Check if this was our outgoing connection */
172 retry_outgoing(c->outgoing);
178 Check if the other end is active.
179 If we have sent packets, but didn't receive any,
180 then possibly the other end is dead. We send a
181 PING request over the meta connection. If the other
182 end does not reply in time, we consider them dead
183 and close the connection.
185 static void check_dead_connections(void)
187 avl_node_t *node, *next;
192 for(node = connection_tree->head; node; node = next) {
194 c = (connection_t *) node->data;
196 if(c->last_ping_time + pingtimeout < now) {
197 if(c->status.active) {
198 if(c->status.pinged) {
199 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING"),
200 c->name, c->hostname);
201 c->status.timeout = true;
202 terminate_connection(c, true);
207 if(c->status.remove) {
208 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
209 c->name, c->hostname, c->status);
213 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
214 c->name, c->hostname);
215 terminate_connection(c, false);
222 check all connections to see if anything
223 happened on their sockets
225 static void check_network_activity(fd_set * f)
230 int len = sizeof(result);
235 if(FD_ISSET(device_fd, f)) {
236 if(read_packet(&packet))
237 route_outgoing(&packet);
240 for(node = connection_tree->head; node; node = node->next) {
241 c = (connection_t *) node->data;
246 if(FD_ISSET(c->socket, f)) {
247 if(c->status.connecting) {
248 c->status.connecting = false;
249 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
252 finish_connecting(c);
254 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
255 _("Error while connecting to %s (%s): %s"),
256 c->name, c->hostname, strerror(result));
258 do_outgoing_connection(c);
263 if(!receive_meta(c)) {
264 terminate_connection(c, c->status.active);
270 for(i = 0; i < listen_sockets; i++) {
271 if(FD_ISSET(listen_socket[i].udp, f))
272 handle_incoming_vpn_data(listen_socket[i].udp);
274 if(FD_ISSET(listen_socket[i].tcp, f))
275 handle_new_meta_connection(listen_socket[i].tcp);
280 this is where it all happens...
287 time_t last_ping_check, last_config_check;
292 last_ping_check = now;
293 last_config_check = now;
299 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
302 maxfd = build_fdset(&fset);
304 r = select(maxfd + 1, &fset, NULL, NULL, &tv);
307 if(errno != EINTR && errno != EAGAIN) {
308 logger(LOG_ERR, _("Error while waiting for input: %s"),
318 check_network_activity(&fset);
325 /* Let's check if everybody is still alive */
327 if(last_ping_check + pingtimeout < now) {
328 check_dead_connections();
329 last_ping_check = now;
331 if(routing_mode == RMODE_SWITCH)
336 /* Should we regenerate our key? */
338 if(keyexpires < now) {
339 ifdebug(STATUS) logger(LOG_INFO, _("Regenerating symmetric key"));
341 RAND_pseudo_bytes(myself->key, myself->keylength);
343 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len);
344 send_key_changed(broadcast, myself);
345 keyexpires = now + keylifetime;
350 while((event = get_expired_event())) {
351 event->handler(event->data);
356 logger(LOG_INFO, _("Flushing event queue"));
358 while(event_tree->head) {
359 event = (event_t *) event_tree->head->data;
360 event->handler(event->data);
374 /* Reread our own configuration file */
376 exit_configuration(&config_tree);
377 init_configuration(&config_tree);
379 if(!read_server_config()) {
380 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
384 /* Close connections to hosts that have a changed or deleted host config file */
386 for(node = connection_tree->head; node; node = node->next) {
387 c = (connection_t *) node->data;
390 free(c->outgoing->name);
391 freeaddrinfo(c->outgoing->ai);
396 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
397 if(stat(fname, &s) || s.st_mtime > last_config_check)
398 terminate_connection(c, c->status.active);
402 last_config_check = now;
404 /* Try to make outgoing connections */
406 try_outgoing_connections();