2 net.c -- most of the network code
3 Copyright (C) 1998-2001 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000,2001 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.149 2001/11/16 12:22:02 zarq Exp $
28 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
37 #include <sys/signal.h>
39 #include <sys/types.h>
42 #include <sys/ioctl.h>
43 /* SunOS really wants sys/socket.h BEFORE net/if.h,
44 and FreeBSD wants these lines below the rest. */
45 #include <arpa/inet.h>
46 #include <sys/socket.h>
49 #include <openssl/rand.h>
50 #include <openssl/evp.h>
51 #include <openssl/pem.h>
53 #ifndef HAVE_RAND_PSEUDO_BYTES
54 #define RAND_pseudo_bytes RAND_bytes
63 #include "connection.h"
76 int seconds_till_retry = 5;
88 void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
94 /* Decrypt the packet */
96 EVP_DecryptInit(&ctx, myself->cipher, myself->key, myself->key + myself->cipher->key_len);
97 EVP_DecryptUpdate(&ctx, outpkt.salt, &outlen, inpkt->salt, inpkt->len);
98 EVP_DecryptFinal(&ctx, outpkt.salt + outlen, &outpad);
100 outpkt.len = outlen - sizeof(outpkt.salt);
102 receive_packet(n, &outpkt);
106 void receive_tcppacket(connection_t *c, char *buffer, int len)
111 memcpy(outpkt.data, buffer, len);
113 receive_packet(c->node, &outpkt);
117 void receive_packet(node_t *n, vpn_packet_t *packet)
120 if(debug_lvl >= DEBUG_TRAFFIC)
121 syslog(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"), packet->len, n->name, n->hostname);
123 route_incoming(n, packet);
127 void send_udppacket(node_t *n, vpn_packet_t *inpkt)
132 struct sockaddr_in to;
133 socklen_t tolen = sizeof(to);
136 if(!n->status.validkey)
138 if(debug_lvl >= DEBUG_TRAFFIC)
139 syslog(LOG_INFO, _("No valid key known yet for %s (%s), queueing packet"),
140 n->name, n->hostname);
142 /* Since packet is on the stack of handle_tap_input(),
143 we have to make a copy of it first. */
145 copy = xmalloc(sizeof(vpn_packet_t));
146 memcpy(copy, inpkt, sizeof(vpn_packet_t));
148 list_insert_tail(n->queue, copy);
150 if(!n->status.waitingforkey)
151 send_req_key(n->nexthop->connection, myself, n);
155 /* Encrypt the packet. */
157 RAND_pseudo_bytes(inpkt->salt, sizeof(inpkt->salt));
159 EVP_EncryptInit(&ctx, n->cipher, n->key, n->key + n->cipher->key_len);
160 EVP_EncryptUpdate(&ctx, outpkt.salt, &outlen, inpkt->salt, inpkt->len + sizeof(inpkt->salt));
161 EVP_EncryptFinal(&ctx, outpkt.salt + outlen, &outpad);
164 to.sin_family = AF_INET;
165 to.sin_addr.s_addr = htonl(n->address);
166 to.sin_port = htons(n->port);
168 if((sendto(udp_socket, (char *) outpkt.salt, outlen, 0, (const struct sockaddr *)&to, tolen)) < 0)
170 syslog(LOG_ERR, _("Error sending packet to %s (%s): %m"),
171 n->name, n->hostname);
178 send a packet to the given vpn ip.
180 void send_packet(node_t *n, vpn_packet_t *packet)
183 if(debug_lvl >= DEBUG_TRAFFIC)
184 syslog(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
185 packet->len, n->name, n->hostname);
189 if(debug_lvl >= DEBUG_TRAFFIC)
191 syslog(LOG_NOTICE, _("Packet is looping back to us!"));
197 if(n->via != n && debug_lvl >= DEBUG_TRAFFIC)
198 syslog(LOG_ERR, _("Sending packet to %s via %s (%s)"),
199 n->name, n->via->name, n->via->hostname);
201 if((myself->options | n->via->options) & OPTION_TCPONLY)
203 if(send_tcppacket(n->via->connection, packet))
204 terminate_connection(n->via->connection, 1);
207 send_udppacket(n->via, packet);
210 /* Broadcast a packet to all active direct connections */
212 void broadcast_packet(node_t *from, vpn_packet_t *packet)
217 if(debug_lvl >= DEBUG_TRAFFIC)
218 syslog(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
219 packet->len, from->name, from->hostname);
221 for(node = connection_tree->head; node; node = node->next)
223 c = (connection_t *)node->data;
224 if(c->status.active && c != from->nexthop->connection)
225 send_packet(c->node, packet);
230 void flush_queue(node_t *n)
232 list_node_t *node, *next;
234 if(debug_lvl >= DEBUG_TRAFFIC)
235 syslog(LOG_INFO, _("Flushing queue for %s (%s)"), n->name, n->hostname);
237 for(node = n->queue->head; node; node = next)
240 send_udppacket(n, (vpn_packet_t *)node->data);
241 list_delete_node(n->queue, node);
248 int setup_listen_socket(int port)
251 struct sockaddr_in a;
259 if((nfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
261 syslog(LOG_ERR, _("Creating metasocket failed: %m"));
265 flags = fcntl(nfd, F_GETFL);
266 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
269 syslog(LOG_ERR, _("System call `%s' failed: %m"),
274 /* Optimize TCP settings */
277 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
278 setsockopt(nfd, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option));
280 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
282 option = IPTOS_LOWDELAY;
283 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
285 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
286 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)))
289 syslog(LOG_ERR, _("Can't bind to interface %s: %m"), interface);
294 memset(&a, 0, sizeof(a));
295 a.sin_family = AF_INET;
296 a.sin_addr.s_addr = htonl(INADDR_ANY);
297 a.sin_port = htons(port);
299 if(get_config_string(lookup_config(config_tree, "BindToAddress"), &address))
301 ipmask = strtoip(address);
304 a.sin_addr.s_addr = htonl(ipmask->address);
309 if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
312 syslog(LOG_ERR, _("Can't bind to port %hd/tcp: %m"), port);
319 syslog(LOG_ERR, _("System call `%s' failed: %m"),
327 int setup_vpn_in_socket(int port)
330 struct sockaddr_in a;
333 if((nfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
336 syslog(LOG_ERR, _("Creating socket failed: %m"));
340 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
342 flags = fcntl(nfd, F_GETFL);
343 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
346 syslog(LOG_ERR, _("System call `%s' failed: %m"),
351 memset(&a, 0, sizeof(a));
352 a.sin_family = AF_INET;
353 a.sin_port = htons(port);
354 a.sin_addr.s_addr = htonl(INADDR_ANY);
356 if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
359 syslog(LOG_ERR, _("Can't bind to port %hd/udp: %m"), port);
366 int setup_outgoing_socket(connection_t *c)
369 struct sockaddr_in a;
371 if(debug_lvl >= DEBUG_CONNECTIONS)
372 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
374 c->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
378 syslog(LOG_ERR, _("Creating socket for %s port %d failed: %m"),
379 c->hostname, c->port);
383 /* Bind first to get a fix on our source port???
385 a.sin_family = AF_INET;
386 a.sin_port = htons(0);
387 a.sin_addr.s_addr = htonl(INADDR_ANY);
389 if(bind(c->socket, (struct sockaddr *)&a, sizeof(struct sockaddr)))
392 syslog(LOG_ERR, _("System call `%s' failed: %m"), "bind");
398 /* Optimize TCP settings?
401 setsockopt(c->socket, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option));
403 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
405 option = IPTOS_LOWDELAY;
406 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
413 if(connect(c->socket, c->address->ai_addr, c->address->ai_addrlen) == -1)
416 syslog(LOG_ERR, _("%s port %s: %m"), c->hostname, c->port);
420 flags = fcntl(c->socket, F_GETFL);
422 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0)
425 syslog(LOG_ERR, _("fcntl for %s port %s: %m"),
426 c->hostname, c->port);
430 if(debug_lvl >= DEBUG_CONNECTIONS)
431 syslog(LOG_INFO, _("Connected to %s port %s"),
432 c->hostname, c->port);
437 int setup_outgoing_connection(char *name)
441 struct addrinfo *ai, *aitop, hints;
442 int r, ipv6preferred;
445 n = lookup_node(name);
450 if(debug_lvl >= DEBUG_CONNECTIONS)
451 syslog(LOG_INFO, _("Already connected to %s"), name);
455 c = new_connection();
456 c->name = xstrdup(name);
458 init_configuration(&c->config_tree);
459 read_connection_config(c);
461 if(!get_config_string(lookup_config(c->config_tree, "Address"), &c->hostname))
463 syslog(LOG_ERR, _("No address specified for %s"), c->name);
468 if(!get_config_string(lookup_config(c->config_tree, "Port"), &c->port))
470 syslog(LOG_ERR, _("No port specified for %s"), c->name);
475 hints.ai_socktype = SOCK_STREAM;
476 hints.ai_family = AF_INET;
477 if(get_config_bool(lookup_config(c->config_tree, "IPv6Preferred"), &ipv6preferred))
480 hints.ai_family = PF_UNSPEC;
483 if((r = getaddrinfo(c->hostname, c->port, &hints, &aitop)) != 0)
485 syslog(LOG_ERR, _("Looking up %s failed: %s\n"),
486 c->hostname, gai_strerror(r));
490 for(ai = aitop; ai != NULL; ai = ai->ai_next)
492 if(setup_outgoing_socket(c) < 0)
498 /* No connection alternative succeeded */
503 c->status.outgoing = 1;
504 c->last_ping_time = time(NULL);
513 int read_rsa_public_key(connection_t *c)
520 c->rsa_key = RSA_new();
522 /* First, check for simple PublicKey statement */
524 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key))
526 BN_hex2bn(&c->rsa_key->n, key);
527 BN_hex2bn(&c->rsa_key->e, "FFFF");
531 /* Else, check for PublicKeyFile statement and read it */
533 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
535 if(is_safe_path(fname))
537 if((fp = fopen(fname, "r")) == NULL)
539 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %m"),
543 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
547 syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %m"),
557 /* Else, check if a harnessed public key is in the config file */
559 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
560 if((fp = fopen(fname, "r")))
562 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
572 syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
577 int read_rsa_private_key(void)
582 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
584 myself->connection->rsa_key = RSA_new();
585 BN_hex2bn(&myself->connection->rsa_key->d, key);
586 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
588 else if(get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
590 if((fp = fopen(fname, "r")) == NULL)
592 syslog(LOG_ERR, _("Error reading RSA private key file `%s': %m"),
596 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
598 if(!myself->connection->rsa_key)
600 syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %m"),
607 syslog(LOG_ERR, _("No private key for tinc daemon specified!"));
615 Configure node_t myself and set up the local sockets (listen only)
617 int setup_myself(void)
625 myself->connection = new_connection();
626 init_configuration(&myself->connection->config_tree);
628 asprintf(&myself->hostname, _("MYSELF"));
629 asprintf(&myself->connection->hostname, _("MYSELF"));
631 myself->connection->options = 0;
632 myself->connection->protocol_version = PROT_CURRENT;
634 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) /* Not acceptable */
636 syslog(LOG_ERR, _("Name for tinc daemon required!"));
642 syslog(LOG_ERR, _("Invalid name for myself!"));
648 myself->connection->name = xstrdup(name);
651 if(read_rsa_private_key())
654 if(read_connection_config(myself->connection))
656 syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
660 if(read_rsa_public_key(myself->connection))
665 if(RSA_check_key(rsa_key) != 1)
667 syslog(LOG_ERR, _("Invalid public/private keypair!"));
671 if(!get_config_port(lookup_config(myself->connection->config_tree, "Port"), &myself->port))
674 myself->connection->port = myself->port;
676 /* Read in all the subnets specified in the host configuration file */
678 cfg = lookup_config(myself->connection->config_tree, "Subnet");
682 if(!get_config_subnet(cfg, &subnet))
685 subnet_add(myself, subnet);
687 cfg = lookup_config_next(myself->connection->config_tree, cfg);
691 /* Check some options */
693 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
695 myself->options |= OPTION_INDIRECT;
697 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
699 myself->options |= OPTION_TCPONLY;
701 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
703 myself->options |= OPTION_INDIRECT;
705 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
707 myself->options |= OPTION_TCPONLY;
709 if(myself->options & OPTION_TCPONLY)
710 myself->options |= OPTION_INDIRECT;
712 if(get_config_string(lookup_config(config_tree, "Mode"), &mode))
714 if(!strcasecmp(mode, "router"))
715 routing_mode = RMODE_ROUTER;
716 else if (!strcasecmp(mode, "switch"))
717 routing_mode = RMODE_SWITCH;
718 else if (!strcasecmp(mode, "hub"))
719 routing_mode = RMODE_HUB;
722 syslog(LOG_ERR, _("Invalid routing mode!"));
727 routing_mode = RMODE_ROUTER;
732 if((tcp_socket = setup_listen_socket(myself->port)) < 0)
734 syslog(LOG_ERR, _("Unable to set up a listening TCP socket!"));
738 if((udp_socket = setup_vpn_in_socket(myself->port)) < 0)
740 syslog(LOG_ERR, _("Unable to set up a listening UDP socket!"));
744 /* Generate packet encryption key */
746 myself->cipher = EVP_bf_cbc();
748 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
750 myself->key = (char *)xmalloc(myself->keylength);
751 RAND_pseudo_bytes(myself->key, myself->keylength);
753 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
756 keyexpires = time(NULL) + keylifetime;
760 myself->nexthop = myself;
761 myself->via = myself;
762 myself->status.active = 1;
765 syslog(LOG_NOTICE, _("Ready: listening on port %hd"), myself->port);
771 setup all initial network connections
773 int setup_network_connections(void)
781 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &timeout))
791 if(setup_device() < 0)
794 /* Run tinc-up script to further initialize the tap interface */
795 execute_script("tinc-up");
797 if(setup_myself() < 0)
800 signal(SIGALRM, try_outgoing_connections);
807 close all open network connections
809 void close_network_connections(void)
811 avl_node_t *node, *next;
814 for(node = connection_tree->head; node; node = next)
817 c = (connection_t *)node->data;
818 c->status.outgoing = 0;
819 terminate_connection(c, 0);
822 terminate_connection(myself->connection, 0);
832 execute_script("tinc-down");
840 handle an incoming tcp connect call and open
843 connection_t *create_new_connection(int sfd)
846 struct sockaddr_in ci;
847 int len = sizeof(ci);
849 c = new_connection();
851 if(getpeername(sfd, (struct sockaddr *) &ci, (socklen_t *) &len) < 0)
853 syslog(LOG_ERR, _("System call `%s' failed: %m"),
859 c->address = ntohl(ci.sin_addr.s_addr);
860 c->hostname = hostlookup(ci.sin_addr.s_addr);
861 c->port = htons(ci.sin_port);
863 c->last_ping_time = time(NULL);
865 if(debug_lvl >= DEBUG_CONNECTIONS)
866 syslog(LOG_NOTICE, _("Connection from %s port %d"),
867 c->hostname, c->port);
869 c->allow_request = ID;
875 put all file descriptors in an fd_set array
877 void build_fdset(fd_set *fs)
884 for(node = connection_tree->head; node; node = node->next)
886 c = (connection_t *)node->data;
887 FD_SET(c->socket, fs);
890 FD_SET(tcp_socket, fs);
891 FD_SET(udp_socket, fs);
892 FD_SET(device_fd, fs);
897 receive incoming data from the listening
898 udp socket and write it to the ethertap
899 device after being decrypted
901 void handle_incoming_vpn_data(void)
904 int x, l = sizeof(x);
905 struct sockaddr_in from;
906 socklen_t fromlen = sizeof(from);
909 if(getsockopt(udp_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
911 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m"),
912 __FILE__, __LINE__, udp_socket);
917 syslog(LOG_ERR, _("Incoming data socket error: %s"), strerror(x));
921 if((pkt.len = recvfrom(udp_socket, (char *) pkt.salt, MTU, 0, (struct sockaddr *)&from, &fromlen)) <= 0)
923 syslog(LOG_ERR, _("Receiving packet failed: %m"));
927 n = lookup_node_udp(ntohl(from.sin_addr.s_addr), ntohs(from.sin_port));
931 syslog(LOG_WARNING, _("Received UDP packet on port %hd from unknown source %x:%hd"), myself->port, ntohl(from.sin_addr.s_addr), ntohs(from.sin_port));
936 n->connection->last_ping_time = time(NULL);
938 receive_udppacket(n, &pkt);
943 Terminate a connection:
945 - Remove associated edge and tell other connections about it if report = 1
946 - Check if we need to retry making an outgoing connection
947 - Deactivate the host
949 void terminate_connection(connection_t *c, int report)
957 if(debug_lvl >= DEBUG_CONNECTIONS)
958 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
959 c->name, c->hostname);
961 c->status.remove = 1;
970 for(node = connection_tree->head; node; node = node->next)
972 other = (connection_t *)node->data;
973 if(other->status.active && other != c)
974 send_del_edge(other, c->edge);
981 /* Check if this was our outgoing connection */
983 if(c->status.outgoing)
985 c->status.outgoing = 0;
986 signal(SIGALRM, try_outgoing_connections);
987 alarm(seconds_till_retry);
988 syslog(LOG_NOTICE, _("Trying to re-establish outgoing connection in %d seconds"), seconds_till_retry);
993 c->status.active = 0;
995 c->node->connection = NULL;
1001 Check if the other end is active.
1002 If we have sent packets, but didn't receive any,
1003 then possibly the other end is dead. We send a
1004 PING request over the meta connection. If the other
1005 end does not reply in time, we consider them dead
1006 and close the connection.
1008 void check_dead_connections(void)
1011 avl_node_t *node, *next;
1016 for(node = connection_tree->head; node; node = next)
1019 c = (connection_t *)node->data;
1020 if(c->last_ping_time + timeout < now)
1022 if(c->status.active)
1024 if(c->status.pinged)
1026 if(debug_lvl >= DEBUG_PROTOCOL)
1027 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
1028 c->name, c->hostname);
1029 c->status.timeout = 1;
1030 terminate_connection(c, 1);
1039 if(debug_lvl >= DEBUG_CONNECTIONS)
1040 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
1041 c->name, c->hostname);
1042 terminate_connection(c, 0);
1050 accept a new tcp connect and create a
1053 int handle_new_meta_connection()
1056 struct sockaddr client;
1057 int fd, len = sizeof(client);
1059 if((fd = accept(tcp_socket, &client, &len)) < 0)
1061 syslog(LOG_ERR, _("Accepting a new connection failed: %m"));
1065 if(!(new = create_new_connection(fd)))
1069 syslog(LOG_NOTICE, _("Closed attempted connection"));
1073 connection_add(new);
1080 void randomized_alarm(int seconds)
1083 RAND_pseudo_bytes(&r, 1);
1084 alarm((seconds * (int)r) / 128 + 1);
1087 /* This function is severely fucked up.
1088 We want to redesign it so the following rules apply:
1090 - Try all ConnectTo's in a row:
1091 - if a connect() fails, try next one immediately,
1092 - if it works, wait 5 seconds or so.
1093 - If none of them were succesful, increase delay and retry.
1094 - If all were succesful, don't try anymore.
1098 try_outgoing_connections(int a)
1100 static config_t *cfg = NULL;
1101 static int retry = 0;
1105 cfg = lookup_config(config_tree, "ConnectTo");
1112 get_config_string(cfg, &name);
1116 syslog(LOG_ERR, _("Invalid name for outgoing connection in %s line %d"), cfg->file, cfg->line);
1120 if(setup_outgoing_connection(name)) /* function returns 0 when there are no problems */
1123 cfg = lookup_config_next(config._tree, cfg); /* Next time skip to next ConnectTo line */
1126 get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout);
1130 seconds_till_retry += 5;
1131 if(seconds_till_retry > maxtimeout) /* Don't wait more than MAXTIMEOUT seconds. */
1132 seconds_till_retry = maxtimeout;
1134 syslog(LOG_ERR, _("Failed to setup any outgoing connection, will retry in %d seconds"),
1135 seconds_till_retry);
1137 /* Randomize timeout to avoid global synchronisation effects */
1138 randomized_alarm(seconds_till_retry);
1142 seconds_till_retry = 5;
1148 check all connections to see if anything
1149 happened on their sockets
1151 void check_network_activity(fd_set *f)
1156 if(FD_ISSET(udp_socket, f))
1157 handle_incoming_vpn_data();
1159 for(node = connection_tree->head; node; node = node->next)
1161 c = (connection_t *)node->data;
1163 if(c->status.remove)
1166 if(FD_ISSET(c->socket, f))
1167 if(receive_meta(c) < 0)
1169 terminate_connection(c, c->status.active);
1174 if(FD_ISSET(tcp_socket, f))
1175 handle_new_meta_connection();
1179 void prune_connections(void)
1182 avl_node_t *node, *next;
1184 for(node = connection_tree->head; node; node = next)
1187 c = (connection_t *)node->data;
1189 if(c->status.remove)
1196 this is where it all happens...
1198 void main_loop(void)
1203 time_t last_ping_check;
1205 vpn_packet_t packet;
1207 last_ping_check = time(NULL);
1211 tv.tv_sec = timeout;
1216 prune_connections();
1222 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
1224 if(errno != EINTR) /* because of alarm */
1226 syslog(LOG_ERR, _("Error while waiting for input: %m"));
1233 syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds"));
1235 close_network_connections();
1236 exit_configuration(&config_tree);
1238 if(read_server_config())
1240 syslog(LOG_ERR, _("Unable to reread configuration file, exiting"));
1246 if(setup_network_connections())
1254 /* Let's check if everybody is still alive */
1256 if(last_ping_check + timeout < t)
1258 check_dead_connections();
1259 last_ping_check = time(NULL);
1261 /* Should we regenerate our key? */
1265 if(debug_lvl >= DEBUG_STATUS)
1266 syslog(LOG_INFO, _("Regenerating symmetric key"));
1268 RAND_pseudo_bytes(myself->key, myself->keylength);
1269 send_key_changed(myself->connection, myself);
1270 keyexpires = time(NULL) + keylifetime;
1276 check_network_activity(&fset);
1278 /* local tap data */
1279 if(FD_ISSET(device_fd, &fset))
1281 if(read_packet(&packet))
1284 route_outgoing(&packet);