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.172 2002/06/08 12:57:09 guus Exp $
28 #include <netinet/in.h>
29 #ifdef HAVE_NETINET_IP_H
30 #include <netinet/ip.h>
32 #ifdef HAVE_NETINET_TCP_H
33 #include <netinet/tcp.h>
40 #include <sys/types.h>
43 #include <sys/ioctl.h>
44 /* SunOS really wants sys/socket.h BEFORE net/if.h,
45 and FreeBSD wants these lines below the rest. */
46 #include <arpa/inet.h>
47 #include <sys/socket.h>
50 #include <openssl/rand.h>
58 #include "connection.h"
73 #ifndef HAVE_RAND_PSEUDO_BYTES
74 #define RAND_pseudo_bytes RAND_bytes
83 /* Purge edges and subnets of unreachable nodes. Use carefully. */
87 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext, *cnode;
93 if(debug_lvl >= DEBUG_PROTOCOL)
94 syslog(LOG_DEBUG, _("Purging unreachable nodes"));
96 for(nnode = node_tree->head; nnode; nnode = nnext)
99 n = (node_t *)nnode->data;
101 if(!n->status.reachable)
103 if(debug_lvl >= DEBUG_SCARY_THINGS)
104 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
106 for(snode = n->subnet_tree->head; snode; snode = snext)
109 s = (subnet_t *)snode->data;
111 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
113 c = (connection_t *)cnode->data;
115 send_del_subnet(c, s);
121 for(enode = n->edge_tree->head; enode; enode = enext)
124 e = (edge_t *)enode->data;
126 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
128 c = (connection_t *)cnode->data;
143 put all file descriptors in an fd_set array
144 While we're at it, purge stuff that needs to be removed.
146 void build_fdset(fd_set *fs)
148 avl_node_t *node, *next;
154 for(node = connection_tree->head; node; node = next)
157 c = (connection_t *)node->data;
162 if(!connection_tree->head)
166 FD_SET(c->socket, fs);
169 for(i = 0; i < listen_sockets; i++)
171 FD_SET(listen_socket[i].tcp, fs);
172 FD_SET(listen_socket[i].udp, fs);
175 FD_SET(device_fd, fs);
180 Terminate a connection:
182 - Remove associated edge and tell other connections about it if report = 1
183 - Check if we need to retry making an outgoing connection
184 - Deactivate the host
186 void terminate_connection(connection_t *c, int report)
194 if(debug_lvl >= DEBUG_CONNECTIONS)
195 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
196 c->name, c->hostname);
198 c->status.remove = 1;
199 c->status.active = 0;
202 c->node->connection = NULL;
211 for(node = connection_tree->head; node; node = node->next)
213 other = (connection_t *)node->data;
214 if(other->status.active && other != c)
215 send_del_edge(other, c->edge);
221 /* Run MST and SSSP algorithms */
226 /* Check if this was our outgoing connection */
230 retry_outgoing(c->outgoing);
237 Check if the other end is active.
238 If we have sent packets, but didn't receive any,
239 then possibly the other end is dead. We send a
240 PING request over the meta connection. If the other
241 end does not reply in time, we consider them dead
242 and close the connection.
244 void check_dead_connections(void)
246 avl_node_t *node, *next;
249 for(node = connection_tree->head; node; node = next)
252 c = (connection_t *)node->data;
253 if(c->last_ping_time + pingtimeout < now)
259 if(debug_lvl >= DEBUG_PROTOCOL)
260 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
261 c->name, c->hostname);
262 c->status.timeout = 1;
263 terminate_connection(c, 1);
272 if(debug_lvl >= DEBUG_CONNECTIONS)
273 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
274 c->name, c->hostname);
275 terminate_connection(c, 0);
283 check all connections to see if anything
284 happened on their sockets
286 void check_network_activity(fd_set *f)
291 int len = sizeof(result);
294 if(FD_ISSET(device_fd, f))
296 if(!read_packet(&packet))
297 route_outgoing(&packet);
300 for(node = connection_tree->head; node; node = node->next)
302 c = (connection_t *)node->data;
307 if(FD_ISSET(c->socket, f))
309 if(c->status.connecting)
311 c->status.connecting = 0;
312 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
314 finish_connecting(c);
317 if(debug_lvl >= DEBUG_CONNECTIONS)
318 syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
320 do_outgoing_connection(c);
324 if(receive_meta(c) < 0)
326 terminate_connection(c, c->status.active);
332 for(i = 0; i < listen_sockets; i++)
334 if(FD_ISSET(listen_socket[i].udp, f))
335 handle_incoming_vpn_data(listen_socket[i].udp);
336 if(FD_ISSET(listen_socket[i].tcp, f))
337 handle_new_meta_connection(listen_socket[i].tcp);
343 this is where it all happens...
350 time_t last_ping_check;
353 last_ping_check = now;
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 && errno != EAGAIN)
370 syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
379 check_network_activity(&fset);
387 /* Let's check if everybody is still alive */
389 if(last_ping_check + pingtimeout < now)
391 check_dead_connections();
392 last_ping_check = now;
394 if(routing_mode== RMODE_SWITCH)
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 = now + 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, exitting."));
449 if(setup_network_connections())