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.141 2001/10/28 10:16:18 guus Exp $
28 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
36 #include <sys/signal.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>
49 #include <openssl/evp.h>
50 #include <openssl/pem.h>
52 #ifndef HAVE_RAND_PSEUDO_BYTES
53 #define RAND_pseudo_bytes RAND_bytes
62 #include "connection.h"
76 int seconds_till_retry = 5;
86 void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
92 /* Decrypt the packet */
94 EVP_DecryptInit(&ctx, myself->cipher, myself->key, myself->key + myself->cipher->key_len);
95 EVP_DecryptUpdate(&ctx, outpkt.salt, &outlen, inpkt->salt, inpkt->len);
96 EVP_DecryptFinal(&ctx, outpkt.salt + outlen, &outpad);
98 outpkt.len = outlen - sizeof(outpkt.salt);
100 receive_packet(n, &outpkt);
104 void receive_tcppacket(connection_t *c, char *buffer, int len)
109 memcpy(outpkt.data, buffer, len);
111 receive_packet(c->node, &outpkt);
115 void receive_packet(node_t *n, vpn_packet_t *packet)
118 if(debug_lvl >= DEBUG_TRAFFIC)
119 syslog(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"), packet->len, n->name, n->hostname);
121 route_incoming(n, packet);
125 void send_udppacket(node_t *n, vpn_packet_t *inpkt)
130 struct sockaddr_in to;
131 socklen_t tolen = sizeof(to);
134 if(!n->status.validkey)
136 if(debug_lvl >= DEBUG_TRAFFIC)
137 syslog(LOG_INFO, _("No valid key known yet for %s (%s), queueing packet"),
138 n->name, n->hostname);
140 /* Since packet is on the stack of handle_tap_input(),
141 we have to make a copy of it first. */
143 copy = xmalloc(sizeof(vpn_packet_t));
144 memcpy(copy, inpkt, sizeof(vpn_packet_t));
146 list_insert_tail(n->queue, copy);
148 if(!n->status.waitingforkey)
149 send_req_key(n->nexthop->connection, myself, n);
153 /* Encrypt the packet. */
155 RAND_pseudo_bytes(inpkt->salt, sizeof(inpkt->salt));
157 EVP_EncryptInit(&ctx, n->cipher, n->key, n->key + n->cipher->key_len);
158 EVP_EncryptUpdate(&ctx, outpkt.salt, &outlen, inpkt->salt, inpkt->len + sizeof(inpkt->salt));
159 EVP_EncryptFinal(&ctx, outpkt.salt + outlen, &outpad);
162 to.sin_family = AF_INET;
163 to.sin_addr.s_addr = htonl(n->address);
164 to.sin_port = htons(n->port);
166 if((sendto(udp_socket, (char *) outpkt.salt, outlen, 0, (const struct sockaddr *)&to, tolen)) < 0)
168 syslog(LOG_ERR, _("Error sending packet to %s (%s): %m"),
169 n->name, n->hostname);
176 send a packet to the given vpn ip.
178 void send_packet(node_t *n, vpn_packet_t *packet)
181 if(debug_lvl >= DEBUG_TRAFFIC)
182 syslog(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
183 packet->len, n->name, n->hostname);
187 if(debug_lvl >= DEBUG_TRAFFIC)
189 syslog(LOG_NOTICE, _("Packet is looping back to us!"));
195 if(!n->status.active)
197 if(debug_lvl >= DEBUG_TRAFFIC)
198 syslog(LOG_INFO, _("%s (%s) is not active, dropping packet"),
199 n->name, n->hostname);
209 if(via != n && debug_lvl >= DEBUG_TRAFFIC)
210 syslog(LOG_ERR, _("Sending packet to %s via %s (%s)"),
211 n->name, via->name, via->hostname);
213 if((myself->options | via->options) & OPTION_TCPONLY)
215 if(send_tcppacket(via->connection, packet))
216 terminate_connection(via->connection, 1);
219 send_udppacket(via, packet);
223 /* Broadcast a packet to all active direct connections */
225 void broadcast_packet(node_t *from, vpn_packet_t *packet)
230 if(debug_lvl >= DEBUG_TRAFFIC)
231 syslog(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
232 packet->len, from->name, from->hostname);
234 for(node = connection_tree->head; node; node = node->next)
236 c = (connection_t *)node->data;
237 if(c->status.active && c != from->nexthop->connection)
238 send_packet(c->node, packet);
243 void flush_queue(node_t *n)
245 list_node_t *node, *next;
247 if(debug_lvl >= DEBUG_TRAFFIC)
248 syslog(LOG_INFO, _("Flushing queue for %s (%s)"), n->name, n->hostname);
250 for(node = n->queue->head; node; node = next)
253 send_udppacket(n, (vpn_packet_t *)node->data);
254 list_delete_node(n->queue, node);
261 int setup_listen_socket(int port)
264 struct sockaddr_in a;
270 if((nfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
272 syslog(LOG_ERR, _("Creating metasocket failed: %m"));
276 flags = fcntl(nfd, F_GETFL);
277 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
280 syslog(LOG_ERR, _("System call `%s' failed: %m"),
285 /* Optimize TCP settings */
288 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
289 setsockopt(nfd, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option));
291 setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
293 option = IPTOS_LOWDELAY;
294 setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
296 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
297 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)))
300 syslog(LOG_ERR, _("Can't bind to interface %s: %m"), interface);
305 memset(&a, 0, sizeof(a));
306 a.sin_family = AF_INET;
307 a.sin_addr.s_addr = htonl(INADDR_ANY);
308 a.sin_port = htons(port);
310 if(get_config_string(lookup_config(config_tree, "BindToAddress"), &address))
312 ipmask = strtoip(address);
315 a.sin_addr.s_addr = htonl(ipmask->address);
320 if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
323 syslog(LOG_ERR, _("Can't bind to port %hd/tcp: %m"), port);
330 syslog(LOG_ERR, _("System call `%s' failed: %m"),
338 int setup_vpn_in_socket(int port)
341 struct sockaddr_in a;
344 if((nfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
347 syslog(LOG_ERR, _("Creating socket failed: %m"));
351 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
353 flags = fcntl(nfd, F_GETFL);
354 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
357 syslog(LOG_ERR, _("System call `%s' failed: %m"),
362 memset(&a, 0, sizeof(a));
363 a.sin_family = AF_INET;
364 a.sin_port = htons(port);
365 a.sin_addr.s_addr = htonl(INADDR_ANY);
367 if(bind(nfd, (struct sockaddr *)&a, sizeof(struct sockaddr)))
370 syslog(LOG_ERR, _("Can't bind to port %hd/udp: %m"), port);
377 int setup_outgoing_socket(connection_t *c)
380 struct sockaddr_in a;
383 if(debug_lvl >= DEBUG_CONNECTIONS)
384 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
386 c->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
390 syslog(LOG_ERR, _("Creating socket for %s port %d failed: %m"),
391 c->hostname, c->port);
395 /* Bind first to get a fix on our source port???
397 a.sin_family = AF_INET;
398 a.sin_port = htons(0);
399 a.sin_addr.s_addr = htonl(INADDR_ANY);
401 if(bind(c->socket, (struct sockaddr *)&a, sizeof(struct sockaddr)))
404 syslog(LOG_ERR, _("System call `%s' failed: %m"), "bind");
410 /* Optimize TCP settings?
413 setsockopt(c->socket, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option));
415 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
417 option = IPTOS_LOWDELAY;
418 setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
425 a.sin_family = AF_INET;
426 a.sin_port = htons(c->port);
427 a.sin_addr.s_addr = htonl(c->address);
429 if(connect(c->socket, (struct sockaddr *)&a, sizeof(a)) == -1)
432 syslog(LOG_ERR, _("%s port %hd: %m"), c->hostname, c->port);
436 flags = fcntl(c->socket, F_GETFL);
438 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0)
441 syslog(LOG_ERR, _("fcntl for %s port %d: %m"),
442 c->hostname, c->port);
446 if(debug_lvl >= DEBUG_CONNECTIONS)
447 syslog(LOG_INFO, _("Connected to %s port %hd"),
448 c->hostname, c->port);
453 int setup_outgoing_connection(char *name)
458 c = new_connection();
459 c->name = xstrdup(name);
461 init_configuration(&c->config_tree);
462 read_connection_config(c);
464 if(!get_config_string(lookup_config(c->config_tree, "Address"), &c->hostname))
466 syslog(LOG_ERR, _("No address specified for %s"), c->name);
471 if(!get_config_port(lookup_config(c->config_tree, "Port"), &c->port))
473 syslog(LOG_ERR, _("No port specified for %s"), c->name);
478 if(!(h = gethostbyname(c->hostname)))
480 syslog(LOG_ERR, _("Error looking up `%s': %m"), c->hostname);
485 c->address = ntohl(*((ipv4_t*)(h->h_addr_list[0])));
486 c->hostname = hostlookup(htonl(c->address));
488 if(setup_outgoing_socket(c) < 0)
490 syslog(LOG_ERR, _("Could not set up a meta connection to %s (%s)"),
491 c->name, c->hostname);
496 c->status.outgoing = 1;
497 c->last_ping_time = time(NULL);
506 int read_rsa_public_key(connection_t *c)
514 c->rsa_key = RSA_new();
516 /* First, check for simple PublicKey statement */
518 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key))
520 BN_hex2bn(&c->rsa_key->n, key);
521 BN_hex2bn(&c->rsa_key->e, "FFFF");
525 /* Else, check for PublicKeyFile statement and read it */
527 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
529 if(is_safe_path(fname))
531 if((fp = fopen(fname, "r")) == NULL)
533 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %m"),
537 result = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
541 syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %m"),
551 /* Else, check if a harnessed public key is in the config file */
555 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
556 if((fp = fopen(fname, "r")))
558 result = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
569 syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
574 int read_rsa_private_key(void)
580 if(!myself->connection->rsa_key)
581 myself->connection->rsa_key = RSA_new();
583 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
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 result = PEM_read_RSAPrivateKey(fp, &myself->connection->rsa_key, NULL, NULL);
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, 0);
826 execute_script("tinc-down");
834 handle an incoming tcp connect call and open
837 connection_t *create_new_connection(int sfd)
840 struct sockaddr_in ci;
841 int len = sizeof(ci);
843 c = new_connection();
845 if(getpeername(sfd, (struct sockaddr *) &ci, (socklen_t *) &len) < 0)
847 syslog(LOG_ERR, _("System call `%s' failed: %m"),
853 c->address = ntohl(ci.sin_addr.s_addr);
854 c->hostname = hostlookup(ci.sin_addr.s_addr);
855 c->port = htons(ci.sin_port); /* This one will be overwritten later */
857 c->last_ping_time = time(NULL);
859 if(debug_lvl >= DEBUG_CONNECTIONS)
860 syslog(LOG_NOTICE, _("Connection from %s port %d"),
861 c->hostname, htons(ci.sin_port));
863 c->allow_request = ID;
869 put all file descriptors in an fd_set array
871 void build_fdset(fd_set *fs)
878 for(node = connection_tree->head; node; node = node->next)
880 c = (connection_t *)node->data;
881 FD_SET(c->socket, fs);
884 FD_SET(tcp_socket, fs);
885 FD_SET(udp_socket, fs);
886 FD_SET(device_fd, fs);
891 receive incoming data from the listening
892 udp socket and write it to the ethertap
893 device after being decrypted
895 void handle_incoming_vpn_data(void)
898 int x, l = sizeof(x);
899 struct sockaddr_in from;
900 socklen_t fromlen = sizeof(from);
903 if(getsockopt(udp_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
905 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m"),
906 __FILE__, __LINE__, udp_socket);
911 syslog(LOG_ERR, _("Incoming data socket error: %s"), strerror(x));
915 if((pkt.len = recvfrom(udp_socket, (char *) pkt.salt, MTU, 0, (struct sockaddr *)&from, &fromlen)) <= 0)
917 syslog(LOG_ERR, _("Receiving packet failed: %m"));
921 n = lookup_node_udp(ntohl(from.sin_addr.s_addr), ntohs(from.sin_port));
925 syslog(LOG_WARNING, _("Received UDP packets on port %hd from unknown source %x:%hd"), myself->port, ntohl(from.sin_addr.s_addr), ntohs(from.sin_port));
930 n->connection->last_ping_time = time(NULL);
932 receive_udppacket(n, &pkt);
937 Terminate a connection:
939 - Remove associated hosts and subnets
940 - Deactivate the host
941 - Since it might still be referenced, put it on the prune list.
942 - If report == 1, then send DEL_HOST messages to the other tinc daemons.
944 void terminate_connection(connection_t *c, int report)
946 /* Needs a serious rewrite. */
950 Check if the other end is active.
951 If we have sent packets, but didn't receive any,
952 then possibly the other end is dead. We send a
953 PING request over the meta connection. If the other
954 end does not reply in time, we consider them dead
955 and close the connection.
957 void check_dead_connections(void)
960 avl_node_t *node, *next;
965 for(node = connection_tree->head; node; node = next)
968 c = (connection_t *)node->data;
969 if(c->last_ping_time + timeout < now)
975 if(debug_lvl >= DEBUG_PROTOCOL)
976 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
977 c->name, c->hostname);
978 c->status.timeout = 1;
979 terminate_connection(c, 1);
988 if(debug_lvl >= DEBUG_CONNECTIONS)
989 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
990 c->name, c->hostname);
991 terminate_connection(c, 0);
999 accept a new tcp connect and create a
1002 int handle_new_meta_connection()
1005 struct sockaddr client;
1006 int fd, len = sizeof(client);
1008 if((fd = accept(tcp_socket, &client, &len)) < 0)
1010 syslog(LOG_ERR, _("Accepting a new connection failed: %m"));
1014 if(!(new = create_new_connection(fd)))
1018 syslog(LOG_NOTICE, _("Closed attempted connection"));
1022 connection_add(new);
1029 void randomized_alarm(int seconds)
1032 RAND_pseudo_bytes(&r, 1);
1033 alarm((seconds * (int)r) / 128 + 1);
1036 /* This function is severely fucked up.
1037 We want to redesign it so the following rules apply:
1039 - Try all ConnectTo's in a row:
1040 - if a connect() fails, try next one immediately,
1041 - if it works, wait 5 seconds or so.
1042 - If none of them were succesful, increase delay and retry.
1043 - If all were succesful, don't try anymore.
1047 try_outgoing_connections(int a)
1049 static config_t *cfg = NULL;
1050 static int retry = 0;
1054 cfg = lookup_config(config_tree, "ConnectTo");
1061 get_config_string(cfg, &name);
1062 cfg = lookup_config_next(config_tree, cfg); /* Next time skip to next ConnectTo line */
1064 if(!setup_outgoing_connection(name)) /* function returns 0 when there are no problems */
1069 get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout);
1073 seconds_till_retry += 5;
1074 if(seconds_till_retry > maxtimeout) /* Don't wait more than MAXTIMEOUT seconds. */
1075 seconds_till_retry = maxtimeout;
1077 syslog(LOG_ERR, _("Failed to setup all outgoing connections, will retry in %d seconds"),
1078 seconds_till_retry);
1080 /* Randomize timeout to avoid global synchronisation effects */
1081 randomized_alarm(seconds_till_retry);
1085 seconds_till_retry = 5;
1091 check all connections to see if anything
1092 happened on their sockets
1094 void check_network_activity(fd_set *f)
1099 if(FD_ISSET(udp_socket, f))
1100 handle_incoming_vpn_data();
1102 for(node = connection_tree->head; node; node = node->next)
1104 c = (connection_t *)node->data;
1106 if(c->status.remove)
1109 if(FD_ISSET(c->socket, f))
1110 if(receive_meta(c) < 0)
1112 terminate_connection(c, c->status.active);
1117 if(FD_ISSET(tcp_socket, f))
1118 handle_new_meta_connection();
1123 this is where it all happens...
1125 void main_loop(void)
1130 time_t last_ping_check;
1132 vpn_packet_t packet;
1134 last_ping_check = time(NULL);
1138 tv.tv_sec = timeout;
1143 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
1145 if(errno != EINTR) /* because of alarm */
1147 syslog(LOG_ERR, _("Error while waiting for input: %m"));
1154 syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds"));
1156 close_network_connections();
1157 exit_configuration(&config_tree);
1159 if(read_server_config())
1161 syslog(LOG_ERR, _("Unable to reread configuration file, exiting"));
1167 if(setup_network_connections())
1175 /* Let's check if everybody is still alive */
1177 if(last_ping_check + timeout < t)
1179 check_dead_connections();
1180 last_ping_check = time(NULL);
1182 /* Should we regenerate our key? */
1186 if(debug_lvl >= DEBUG_STATUS)
1187 syslog(LOG_INFO, _("Regenerating symmetric key"));
1189 RAND_pseudo_bytes(myself->key, myself->keylength);
1190 send_key_changed(myself->connection, myself);
1191 keyexpires = time(NULL) + keylifetime;
1197 check_network_activity(&fset);
1199 /* local tap data */
1200 if(FD_ISSET(device_fd, &fset))
1202 if(read_packet(&packet))
1205 route_outgoing(&packet);