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.170 2002/04/18 20:09:05 zarq 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"
71 #ifndef HAVE_RAND_PSEUDO_BYTES
72 #define RAND_pseudo_bytes RAND_bytes
81 /* Purge edges and subnets of unreachable nodes. Use carefully. */
85 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext, *cnode;
91 if(debug_lvl >= DEBUG_PROTOCOL)
92 syslog(LOG_DEBUG, _("Purging unreachable nodes"));
94 for(nnode = node_tree->head; nnode; nnode = nnext)
97 n = (node_t *)nnode->data;
99 if(!n->status.reachable)
101 if(debug_lvl >= DEBUG_SCARY_THINGS)
102 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
104 for(snode = n->subnet_tree->head; snode; snode = snext)
107 s = (subnet_t *)snode->data;
109 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
111 c = (connection_t *)cnode->data;
113 send_del_subnet(c, s);
119 for(enode = n->edge_tree->head; enode; enode = enext)
122 e = (edge_t *)enode->data;
124 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
126 c = (connection_t *)cnode->data;
141 put all file descriptors in an fd_set array
142 While we're at it, purge stuff that needs to be removed.
144 void build_fdset(fd_set *fs)
146 avl_node_t *node, *next;
152 for(node = connection_tree->head; node; node = next)
155 c = (connection_t *)node->data;
160 FD_SET(c->socket, fs);
163 if(!connection_tree->head)
166 for(i = 0; i < listen_sockets; i++)
168 FD_SET(listen_socket[i].tcp, fs);
169 FD_SET(listen_socket[i].udp, fs);
172 FD_SET(device_fd, fs);
177 Terminate a connection:
179 - Remove associated edge and tell other connections about it if report = 1
180 - Check if we need to retry making an outgoing connection
181 - Deactivate the host
183 void terminate_connection(connection_t *c, int report)
191 if(debug_lvl >= DEBUG_CONNECTIONS)
192 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
193 c->name, c->hostname);
195 c->status.remove = 1;
196 c->status.active = 0;
199 c->node->connection = NULL;
208 for(node = connection_tree->head; node; node = node->next)
210 other = (connection_t *)node->data;
211 if(other->status.active && other != c)
212 send_del_edge(other, c->edge);
218 /* Run MST and SSSP algorithms */
223 /* Check if this was our outgoing connection */
227 retry_outgoing(c->outgoing);
234 Check if the other end is active.
235 If we have sent packets, but didn't receive any,
236 then possibly the other end is dead. We send a
237 PING request over the meta connection. If the other
238 end does not reply in time, we consider them dead
239 and close the connection.
241 void check_dead_connections(void)
243 avl_node_t *node, *next;
246 for(node = connection_tree->head; node; node = next)
249 c = (connection_t *)node->data;
250 if(c->last_ping_time + pingtimeout < now)
256 if(debug_lvl >= DEBUG_PROTOCOL)
257 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
258 c->name, c->hostname);
259 c->status.timeout = 1;
260 terminate_connection(c, 1);
269 if(debug_lvl >= DEBUG_CONNECTIONS)
270 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
271 c->name, c->hostname);
272 terminate_connection(c, 0);
280 check all connections to see if anything
281 happened on their sockets
283 void check_network_activity(fd_set *f)
288 int len = sizeof(result);
291 if(FD_ISSET(device_fd, f))
293 if(!read_packet(&packet))
294 route_outgoing(&packet);
297 for(node = connection_tree->head; node; node = node->next)
299 c = (connection_t *)node->data;
304 if(FD_ISSET(c->socket, f))
306 if(c->status.connecting)
308 c->status.connecting = 0;
309 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
311 finish_connecting(c);
314 if(debug_lvl >= DEBUG_CONNECTIONS)
315 syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
317 do_outgoing_connection(c);
321 if(receive_meta(c) < 0)
323 terminate_connection(c, c->status.active);
329 for(i = 0; i < listen_sockets; i++)
331 if(FD_ISSET(listen_socket[i].udp, f))
332 handle_incoming_vpn_data(listen_socket[i].udp);
333 if(FD_ISSET(listen_socket[i].tcp, f))
334 handle_new_meta_connection(listen_socket[i].tcp);
340 this is where it all happens...
347 time_t last_ping_check;
350 last_ping_check = now;
358 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
363 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
365 if(errno != EINTR && errno != EAGAIN)
367 syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
376 check_network_activity(&fset);
384 /* Let's check if everybody is still alive */
386 if(last_ping_check + pingtimeout < now)
388 check_dead_connections();
389 last_ping_check = now;
391 if(routing_mode== RMODE_SWITCH)
396 /* Should we regenerate our key? */
400 if(debug_lvl >= DEBUG_STATUS)
401 syslog(LOG_INFO, _("Regenerating symmetric key"));
403 RAND_pseudo_bytes(myself->key, myself->keylength);
404 send_key_changed(myself->connection, myself);
405 keyexpires = now + keylifetime;
410 while((event = get_expired_event()))
412 event->handler(event->data);
418 syslog(LOG_INFO, _("Flushing event queue"));
420 while(event_tree->head)
422 event = (event_t *)event_tree->head->data;
423 event->handler(event->data);
432 close_network_connections();
433 exit_configuration(&config_tree);
435 syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
438 init_configuration(&config_tree);
440 if(read_server_config())
442 syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
446 if(setup_network_connections())