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.196 2003/08/02 20:50:38 guus Exp $
25 #include <openssl/rand.h>
30 #include "connection.h"
44 bool do_purge = false;
45 volatile bool running;
49 /* Purge edges and subnets of unreachable nodes. Use carefully. */
51 static void purge(void)
53 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
60 ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
62 for(nnode = node_tree->head; nnode; nnode = nnext) {
64 n = (node_t *) nnode->data;
66 if(!n->status.reachable) {
67 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
70 for(snode = n->subnet_tree->head; snode; snode = snext) {
72 s = (subnet_t *) snode->data;
73 send_del_subnet(broadcast, s);
77 for(enode = n->edge_tree->head; enode; enode = enext) {
79 e = (edge_t *) enode->data;
80 send_del_edge(broadcast, e);
90 put all file descriptors in an fd_set array
91 While we're at it, purge stuff that needs to be removed.
93 static int build_fdset(fd_set * fs)
95 avl_node_t *node, *next;
103 for(node = connection_tree->head; node; node = next) {
105 c = (connection_t *) node->data;
107 if(c->status.remove) {
109 if(!connection_tree->head)
112 FD_SET(c->socket, fs);
118 for(i = 0; i < listen_sockets; i++) {
119 FD_SET(listen_socket[i].tcp, fs);
120 if(listen_socket[i].tcp > max)
121 max = listen_socket[i].tcp;
122 FD_SET(listen_socket[i].udp, fs);
123 if(listen_socket[i].udp > max)
124 max = listen_socket[i].udp;
127 FD_SET(device_fd, fs);
135 Terminate a connection:
137 - Remove associated edge and tell other connections about it if report = true
138 - Check if we need to retry making an outgoing connection
139 - Deactivate the host
141 void terminate_connection(connection_t *c, bool report)
148 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
149 c->name, c->hostname);
151 c->status.remove = true;
152 c->status.active = false;
155 c->node->connection = NULL;
158 closesocket(c->socket);
162 send_del_edge(broadcast, c->edge);
166 /* Run MST and SSSP algorithms */
171 /* Check if this was our outgoing connection */
174 retry_outgoing(c->outgoing);
180 Check if the other end is active.
181 If we have sent packets, but didn't receive any,
182 then possibly the other end is dead. We send a
183 PING request over the meta connection. If the other
184 end does not reply in time, we consider them dead
185 and close the connection.
187 static void check_dead_connections(void)
189 avl_node_t *node, *next;
194 for(node = connection_tree->head; node; node = next) {
196 c = (connection_t *) node->data;
198 if(c->last_ping_time + pingtimeout < now) {
199 if(c->status.active) {
200 if(c->status.pinged) {
201 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING"),
202 c->name, c->hostname);
203 c->status.timeout = true;
204 terminate_connection(c, true);
209 if(c->status.remove) {
210 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
211 c->name, c->hostname, *(uint32_t *)&c->status);
215 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
216 c->name, c->hostname);
217 terminate_connection(c, false);
224 check all connections to see if anything
225 happened on their sockets
227 static void check_network_activity(fd_set * f)
232 int len = sizeof(result);
237 if(FD_ISSET(device_fd, f)) {
238 if(read_packet(&packet))
239 route_outgoing(&packet);
242 for(node = connection_tree->head; node; node = node->next) {
243 c = (connection_t *) node->data;
248 if(FD_ISSET(c->socket, f)) {
249 if(c->status.connecting) {
250 c->status.connecting = false;
251 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
254 finish_connecting(c);
256 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
257 _("Error while connecting to %s (%s): %s"),
258 c->name, c->hostname, strerror(result));
259 closesocket(c->socket);
260 do_outgoing_connection(c);
265 if(!receive_meta(c)) {
266 terminate_connection(c, c->status.active);
272 for(i = 0; i < listen_sockets; i++) {
273 if(FD_ISSET(listen_socket[i].udp, f))
274 handle_incoming_vpn_data(listen_socket[i].udp);
276 if(FD_ISSET(listen_socket[i].tcp, f))
277 handle_new_meta_connection(listen_socket[i].tcp);
282 this is where it all happens...
289 time_t last_ping_check, last_config_check;
294 last_ping_check = now;
295 last_config_check = now;
303 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
306 maxfd = build_fdset(&fset);
308 r = select(maxfd + 1, &fset, NULL, NULL, &tv);
311 if(errno != EINTR && errno != EAGAIN) {
312 logger(LOG_ERR, _("Error while waiting for input: %s"),
322 check_network_activity(&fset);
329 /* Let's check if everybody is still alive */
331 if(last_ping_check + pingtimeout < now) {
332 check_dead_connections();
333 last_ping_check = now;
335 if(routing_mode == RMODE_SWITCH)
340 /* Should we regenerate our key? */
342 if(keyexpires < now) {
343 ifdebug(STATUS) logger(LOG_INFO, _("Regenerating symmetric key"));
345 RAND_pseudo_bytes(myself->key, myself->keylength);
347 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len);
348 send_key_changed(broadcast, myself);
349 keyexpires = now + keylifetime;
354 while((event = get_expired_event())) {
355 event->handler(event->data);
360 logger(LOG_INFO, _("Flushing event queue"));
362 while(event_tree->head) {
363 event = (event_t *) event_tree->head->data;
364 event->handler(event->data);
378 /* Reread our own configuration file */
380 exit_configuration(&config_tree);
381 init_configuration(&config_tree);
383 if(!read_server_config()) {
384 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
388 /* Close connections to hosts that have a changed or deleted host config file */
390 for(node = connection_tree->head; node; node = node->next) {
391 c = (connection_t *) node->data;
394 free(c->outgoing->name);
395 freeaddrinfo(c->outgoing->ai);
400 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
401 if(stat(fname, &s) || s.st_mtime > last_config_check)
402 terminate_connection(c, c->status.active);
406 last_config_check = now;
408 /* Try to make outgoing connections */
410 try_outgoing_connections();