2 net.c -- most of the network code
3 Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2002 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.177 2002/09/04 16:26:44 guus Exp $
28 #include <netinet/in.h>
29 #ifdef HAVE_NETINET_IN_SYSTM_H
30 #include <netinet/in_systm.h>
32 #ifdef HAVE_NETINET_IP_H
33 #include <netinet/ip.h>
35 #ifdef HAVE_NETINET_TCP_H
36 #include <netinet/tcp.h>
43 #include <sys/types.h>
46 #include <sys/ioctl.h>
47 /* SunOS really wants sys/socket.h BEFORE net/if.h,
48 and FreeBSD wants these lines below the rest. */
49 #include <arpa/inet.h>
50 #include <sys/socket.h>
53 #include <openssl/rand.h>
61 #include "connection.h"
76 #ifndef HAVE_RAND_PSEUDO_BYTES
77 #define RAND_pseudo_bytes RAND_bytes
86 /* Purge edges and subnets of unreachable nodes. Use carefully. */
90 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
95 if(debug_lvl >= DEBUG_PROTOCOL)
96 syslog(LOG_DEBUG, _("Purging unreachable nodes"));
98 for(nnode = node_tree->head; nnode; nnode = nnext)
101 n = (node_t *)nnode->data;
103 if(!n->status.reachable)
105 if(debug_lvl >= DEBUG_SCARY_THINGS)
106 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
108 for(snode = n->subnet_tree->head; snode; snode = snext)
111 s = (subnet_t *)snode->data;
112 send_del_subnet(broadcast, s);
116 for(enode = n->edge_tree->head; enode; enode = enext)
119 e = (edge_t *)enode->data;
120 send_del_edge(broadcast, e);
131 put all file descriptors in an fd_set array
132 While we're at it, purge stuff that needs to be removed.
134 void build_fdset(fd_set *fs)
136 avl_node_t *node, *next;
142 for(node = connection_tree->head; node; node = next)
145 c = (connection_t *)node->data;
150 if(!connection_tree->head)
154 FD_SET(c->socket, fs);
157 for(i = 0; i < listen_sockets; i++)
159 FD_SET(listen_socket[i].tcp, fs);
160 FD_SET(listen_socket[i].udp, fs);
163 FD_SET(device_fd, fs);
168 Terminate a connection:
170 - Remove associated edge and tell other connections about it if report = 1
171 - Check if we need to retry making an outgoing connection
172 - Deactivate the host
174 void terminate_connection(connection_t *c, int report)
180 if(debug_lvl >= DEBUG_CONNECTIONS)
181 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
182 c->name, c->hostname);
184 c->status.remove = 1;
185 c->status.active = 0;
188 c->node->connection = NULL;
196 send_del_edge(broadcast, c->edge);
200 /* Run MST and SSSP algorithms */
205 /* Check if this was our outgoing connection */
209 retry_outgoing(c->outgoing);
216 Check if the other end is active.
217 If we have sent packets, but didn't receive any,
218 then possibly the other end is dead. We send a
219 PING request over the meta connection. If the other
220 end does not reply in time, we consider them dead
221 and close the connection.
223 void check_dead_connections(void)
225 avl_node_t *node, *next;
228 for(node = connection_tree->head; node; node = next)
231 c = (connection_t *)node->data;
232 if(c->last_ping_time + pingtimeout < now)
238 if(debug_lvl >= DEBUG_PROTOCOL)
239 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
240 c->name, c->hostname);
241 c->status.timeout = 1;
242 terminate_connection(c, 1);
251 if(debug_lvl >= DEBUG_CONNECTIONS)
252 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
253 c->name, c->hostname);
254 terminate_connection(c, 0);
262 check all connections to see if anything
263 happened on their sockets
265 void check_network_activity(fd_set *f)
270 int len = sizeof(result);
273 if(FD_ISSET(device_fd, f))
275 if(!read_packet(&packet))
276 route_outgoing(&packet);
279 for(node = connection_tree->head; node; node = node->next)
281 c = (connection_t *)node->data;
286 if(FD_ISSET(c->socket, f))
288 if(c->status.connecting)
290 c->status.connecting = 0;
291 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
293 finish_connecting(c);
296 if(debug_lvl >= DEBUG_CONNECTIONS)
297 syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
299 do_outgoing_connection(c);
303 if(receive_meta(c) < 0)
305 terminate_connection(c, c->status.active);
311 for(i = 0; i < listen_sockets; i++)
313 if(FD_ISSET(listen_socket[i].udp, f))
314 handle_incoming_vpn_data(listen_socket[i].udp);
315 if(FD_ISSET(listen_socket[i].tcp, f))
316 handle_new_meta_connection(listen_socket[i].tcp);
322 this is where it all happens...
329 time_t last_ping_check;
332 last_ping_check = now;
340 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
345 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
347 if(errno != EINTR && errno != EAGAIN)
349 syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
358 check_network_activity(&fset);
366 /* Let's check if everybody is still alive */
368 if(last_ping_check + pingtimeout < now)
370 check_dead_connections();
371 last_ping_check = now;
373 if(routing_mode== RMODE_SWITCH)
378 /* Should we regenerate our key? */
382 if(debug_lvl >= DEBUG_STATUS)
383 syslog(LOG_INFO, _("Regenerating symmetric key"));
385 RAND_pseudo_bytes(myself->key, myself->keylength);
386 send_key_changed(broadcast, myself);
387 keyexpires = now + keylifetime;
392 while((event = get_expired_event()))
394 event->handler(event->data);
400 syslog(LOG_INFO, _("Flushing event queue"));
402 while(event_tree->head)
404 event = (event_t *)event_tree->head->data;
405 event->handler(event->data);
414 close_network_connections();
415 exit_configuration(&config_tree);
417 syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
420 init_configuration(&config_tree);
422 if(read_server_config())
424 syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
428 if(setup_network_connections())