2 net.c -- most of the network code
3 Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000-2002 Guus Sliepen <guus@sliepen.warande.net>
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.157 2002/02/20 16:04:07 guus Exp $
28 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
38 #include <sys/types.h>
41 #include <sys/ioctl.h>
42 /* SunOS really wants sys/socket.h BEFORE net/if.h,
43 and FreeBSD wants these lines below the rest. */
44 #include <arpa/inet.h>
45 #include <sys/socket.h>
48 #include <openssl/rand.h>
56 #include "connection.h"
77 put all file descriptors in an fd_set array
79 void build_fdset(fd_set *fs)
86 for(node = connection_tree->head; node; node = node->next)
88 c = (connection_t *)node->data;
89 FD_SET(c->socket, fs);
92 FD_SET(tcp_socket, fs);
93 FD_SET(udp_socket, fs);
94 FD_SET(device_fd, fs);
98 /* Purge edges and subnets of unreachable nodes. Use carefully. */
102 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext, *cnode;
108 if(debug_lvl >= DEBUG_PROTOCOL)
109 syslog(LOG_DEBUG, _("Purging unreachable nodes"));
111 for(nnode = node_tree->head; nnode; nnode = nnext)
114 n = (node_t *)nnode->data;
116 if(!n->status.reachable)
118 if(debug_lvl >= DEBUG_SCARY_THINGS)
119 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
121 for(snode = n->subnet_tree->head; snode; snode = snext)
124 s = (subnet_t *)snode->data;
126 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
128 c = (connection_t *)cnode->data;
130 send_del_subnet(c, s);
136 for(enode = n->edge_tree->head; enode; enode = enext)
139 e = (edge_t *)enode->data;
141 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
143 c = (connection_t *)cnode->data;
158 Terminate a connection:
160 - Remove associated edge and tell other connections about it if report = 1
161 - Check if we need to retry making an outgoing connection
162 - Deactivate the host
164 void terminate_connection(connection_t *c, int report)
172 if(debug_lvl >= DEBUG_CONNECTIONS)
173 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
174 c->name, c->hostname);
176 c->status.remove = 1;
185 for(node = connection_tree->head; node; node = node->next)
187 other = (connection_t *)node->data;
188 if(other->status.active && other != c)
189 send_del_edge(other, c->edge);
196 /* Run MST and SSSP algorithms */
200 /* Check if this was our outgoing connection */
204 retry_outgoing(c->outgoing);
210 c->status.active = 0;
212 c->node->connection = NULL;
218 Check if the other end is active.
219 If we have sent packets, but didn't receive any,
220 then possibly the other end is dead. We send a
221 PING request over the meta connection. If the other
222 end does not reply in time, we consider them dead
223 and close the connection.
225 void check_dead_connections(void)
228 avl_node_t *node, *next;
233 for(node = connection_tree->head; node; node = next)
236 c = (connection_t *)node->data;
237 if(c->last_ping_time + pingtimeout < now)
243 if(debug_lvl >= DEBUG_PROTOCOL)
244 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
245 c->name, c->hostname);
246 c->status.timeout = 1;
247 terminate_connection(c, 1);
256 if(debug_lvl >= DEBUG_CONNECTIONS)
257 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
258 c->name, c->hostname);
259 terminate_connection(c, 0);
267 check all connections to see if anything
268 happened on their sockets
270 void check_network_activity(fd_set *f)
275 int len = sizeof(result);
278 if(FD_ISSET(device_fd, f))
280 if(!read_packet(&packet))
281 route_outgoing(&packet);
284 if(FD_ISSET(udp_socket, f))
285 handle_incoming_vpn_data();
287 for(node = connection_tree->head; node; node = node->next)
289 c = (connection_t *)node->data;
294 if(FD_ISSET(c->socket, f))
296 if(c->status.connecting)
298 c->status.connecting = 0;
299 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
301 finish_connecting(c);
304 if(debug_lvl >= DEBUG_CONNECTIONS)
305 syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
307 do_outgoing_connection(c);
311 if(receive_meta(c) < 0)
313 terminate_connection(c, c->status.active);
319 if(FD_ISSET(tcp_socket, f))
320 handle_new_meta_connection();
324 void prune_connections(void)
327 avl_node_t *node, *next;
329 for(node = connection_tree->head; node; node = next)
332 c = (connection_t *)node->data;
338 if(!connection_tree->head)
344 this is where it all happens...
351 time_t last_ping_check;
355 last_ping_check = time(NULL);
361 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
366 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
368 if(errno != EINTR) /* because of a signal */
370 syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
376 check_network_activity(&fset);
392 /* Let's check if everybody is still alive */
394 if(last_ping_check + pingtimeout < t)
396 check_dead_connections();
397 last_ping_check = time(NULL);
399 /* Should we regenerate our key? */
403 if(debug_lvl >= DEBUG_STATUS)
404 syslog(LOG_INFO, _("Regenerating symmetric key"));
406 RAND_pseudo_bytes(myself->key, myself->keylength);
407 send_key_changed(myself->connection, myself);
408 keyexpires = time(NULL) + keylifetime;
413 while((event = get_expired_event()))
415 event->handler(event->data);
421 syslog(LOG_INFO, _("Flushing event queue"));
423 while(event_tree->head)
425 event = (event_t *)event_tree->head->data;
426 event->handler(event->data);
435 close_network_connections();
436 exit_configuration(&config_tree);
438 syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds"));
441 init_configuration(&config_tree);
443 if(read_server_config())
445 syslog(LOG_ERR, _("Unable to reread configuration file, exiting"));
449 if(setup_network_connections())