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.37 2002/04/09 15:26:00 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"
77 /* Purge edges and subnets of unreachable nodes. Use carefully. */
81 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext, *cnode;
87 if(debug_lvl >= DEBUG_PROTOCOL)
88 syslog(LOG_DEBUG, _("Purging unreachable nodes"));
90 for(nnode = node_tree->head; nnode; nnode = nnext)
93 n = (node_t *)nnode->data;
95 if(!n->status.reachable)
97 if(debug_lvl >= DEBUG_SCARY_THINGS)
98 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
100 for(snode = n->subnet_tree->head; snode; snode = snext)
103 s = (subnet_t *)snode->data;
105 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
107 c = (connection_t *)cnode->data;
109 send_del_subnet(c, s);
115 for(enode = n->edge_tree->head; enode; enode = enext)
118 e = (edge_t *)enode->data;
120 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
122 c = (connection_t *)cnode->data;
137 put all file descriptors in an fd_set array
138 While we're at it, purge stuff that needs to be removed.
140 void build_fdset(fd_set *fs)
142 avl_node_t *node, *next;
148 for(node = connection_tree->head; node; node = next)
151 c = (connection_t *)node->data;
156 FD_SET(c->socket, fs);
159 if(!connection_tree->head)
162 for(i = 0; i < listen_sockets; i++)
164 FD_SET(listen_socket[i].tcp, fs);
165 FD_SET(listen_socket[i].udp, fs);
168 FD_SET(device_fd, fs);
173 Terminate a connection:
175 - Remove associated edge and tell other connections about it if report = 1
176 - Check if we need to retry making an outgoing connection
177 - Deactivate the host
179 void terminate_connection(connection_t *c, int report)
187 if(debug_lvl >= DEBUG_CONNECTIONS)
188 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
189 c->name, c->hostname);
191 c->status.remove = 1;
192 c->status.active = 0;
195 c->node->connection = NULL;
204 for(node = connection_tree->head; node; node = node->next)
206 other = (connection_t *)node->data;
207 if(other->status.active && other != c)
208 send_del_edge(other, c->edge);
214 /* Run MST and SSSP algorithms */
219 /* Check if this was our outgoing connection */
223 retry_outgoing(c->outgoing);
230 Check if the other end is active.
231 If we have sent packets, but didn't receive any,
232 then possibly the other end is dead. We send a
233 PING request over the meta connection. If the other
234 end does not reply in time, we consider them dead
235 and close the connection.
237 void check_dead_connections(void)
239 avl_node_t *node, *next;
242 for(node = connection_tree->head; node; node = next)
245 c = (connection_t *)node->data;
246 if(c->last_ping_time + pingtimeout < now)
252 if(debug_lvl >= DEBUG_PROTOCOL)
253 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
254 c->name, c->hostname);
255 c->status.timeout = 1;
256 terminate_connection(c, 1);
265 if(debug_lvl >= DEBUG_CONNECTIONS)
266 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
267 c->name, c->hostname);
268 terminate_connection(c, 0);
276 check all connections to see if anything
277 happened on their sockets
279 void check_network_activity(fd_set *f)
284 int len = sizeof(result);
287 if(FD_ISSET(device_fd, f))
289 if(!read_packet(&packet))
290 route_outgoing(&packet);
293 for(node = connection_tree->head; node; node = node->next)
295 c = (connection_t *)node->data;
300 if(FD_ISSET(c->socket, f))
302 if(c->status.connecting)
304 c->status.connecting = 0;
305 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
307 finish_connecting(c);
310 if(debug_lvl >= DEBUG_CONNECTIONS)
311 syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
313 do_outgoing_connection(c);
317 if(receive_meta(c) < 0)
319 terminate_connection(c, c->status.active);
325 for(i = 0; i < listen_sockets; i++)
327 if(FD_ISSET(listen_socket[i].udp, f))
328 handle_incoming_vpn_data(listen_socket[i].udp);
329 if(FD_ISSET(listen_socket[i].tcp, f))
330 handle_new_meta_connection(listen_socket[i].tcp);
336 this is where it all happens...
343 time_t last_ping_check;
346 last_ping_check = now;
354 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
359 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
361 if(errno != EINTR && errno != EAGAIN)
363 syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
372 check_network_activity(&fset);
380 /* Let's check if everybody is still alive */
382 if(last_ping_check + pingtimeout < now)
384 check_dead_connections();
385 last_ping_check = now;
387 if(routing_mode== RMODE_SWITCH)
392 /* Should we regenerate our key? */
396 if(debug_lvl >= DEBUG_STATUS)
397 syslog(LOG_INFO, _("Regenerating symmetric key"));
399 RAND_pseudo_bytes(myself->key, myself->keylength);
400 send_key_changed(myself->connection, myself);
401 keyexpires = now + keylifetime;
406 while((event = get_expired_event()))
408 event->handler(event->data);
414 syslog(LOG_INFO, _("Flushing event queue"));
416 while(event_tree->head)
418 event = (event_t *)event_tree->head->data;
419 event->handler(event->data);
428 close_network_connections();
429 exit_configuration(&config_tree);
431 syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
434 init_configuration(&config_tree);
436 if(read_server_config())
438 syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
442 if(setup_network_connections())