3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2013 Guus Sliepen <guus@tinc-vpn.org>
5 2006 Scott Lamb <slamb@slamb.org>
6 2010 Brandon Black <blblack@gmail.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "connection.h"
46 static io_t device_io;
53 proxytype_t proxytype;
55 bool disablebuggypeers;
57 char *scriptinterpreter;
58 char *scriptextension;
60 bool node_read_ecdsa_public_key(node_t *n) {
61 if(ecdsa_active(n->ecdsa))
64 splay_tree_t *config_tree;
66 char *pubname = NULL, *hcfname = NULL;
69 xasprintf(&hcfname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
71 init_configuration(&config_tree);
72 if(!read_config_file(config_tree, hcfname))
75 /* First, check for simple ECDSAPublicKey statement */
77 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
78 n->ecdsa = ecdsa_set_base64_public_key(p);
83 /* Else, check for ECDSAPublicKeyFile statement and read it */
85 if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &pubname))
86 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
88 fp = fopen(pubname, "r");
91 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s", pubname, strerror(errno));
95 n->ecdsa = ecdsa_read_pem_public_key(fp);
99 exit_configuration(&config_tree);
105 bool read_ecdsa_public_key(connection_t *c) {
106 if(ecdsa_active(c->ecdsa))
113 /* First, check for simple ECDSAPublicKey statement */
115 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
116 c->ecdsa = ecdsa_set_base64_public_key(p);
121 /* Else, check for ECDSAPublicKeyFile statement and read it */
123 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
124 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
126 fp = fopen(fname, "r");
129 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s",
130 fname, strerror(errno));
135 c->ecdsa = ecdsa_read_pem_public_key(fp);
139 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname);
144 bool read_rsa_public_key(connection_t *c) {
145 if(ecdsa_active(c->ecdsa))
152 /* First, check for simple PublicKey statement */
154 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
155 c->rsa = rsa_set_hex_public_key(n, "FFFF");
160 /* Else, check for PublicKeyFile statement and read it */
162 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
163 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
165 fp = fopen(fname, "r");
168 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
173 c->rsa = rsa_read_pem_public_key(fp);
177 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
182 static bool read_ecdsa_private_key(void) {
186 /* Check for PrivateKeyFile statement and read it */
188 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
189 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
191 fp = fopen(fname, "r");
194 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
196 logger(DEBUG_ALWAYS, LOG_INFO, "Create an ECDSA keypair with `tinc -n %s generate-ecdsa-keys'.", netname ?: ".");
201 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
204 if(fstat(fileno(fp), &s)) {
205 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
210 if(s.st_mode & ~0100700)
211 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
214 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
217 if(!myself->connection->ecdsa)
218 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
220 return myself->connection->ecdsa;
223 static bool read_rsa_private_key(void) {
228 /* First, check for simple PrivateKey statement */
230 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
231 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
232 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
236 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
239 return myself->connection->rsa;
242 /* Else, check for PrivateKeyFile statement and read it */
244 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
245 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
247 fp = fopen(fname, "r");
250 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
251 fname, strerror(errno));
256 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
259 if(fstat(fileno(fp), &s)) {
260 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
265 if(s.st_mode & ~0100700)
266 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
269 myself->connection->rsa = rsa_read_pem_private_key(fp);
272 if(!myself->connection->rsa)
273 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
275 return myself->connection->rsa;
278 static timeout_t keyexpire_timeout;
280 static void keyexpire_handler(void *data) {
282 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
285 void regenerate_key(void) {
286 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
291 Read Subnets from all host config files
293 void load_all_subnets(void) {
298 xasprintf(&dname, "%s" SLASH "hosts", confbase);
299 dir = opendir(dname);
301 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
306 while((ent = readdir(dir))) {
307 if(!check_id(ent->d_name))
310 node_t *n = lookup_node(ent->d_name);
311 #ifdef _DIRENT_HAVE_D_TYPE
312 //if(ent->d_type != DT_REG)
317 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
319 splay_tree_t *config_tree;
320 init_configuration(&config_tree);
321 read_config_options(config_tree, ent->d_name);
322 read_config_file(config_tree, fname);
327 n->name = xstrdup(ent->d_name);
331 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
334 if(!get_config_subnet(cfg, &s))
337 if((s2 = lookup_subnet(n, s))) {
344 exit_configuration(&config_tree);
350 void load_all_nodes(void) {
355 xasprintf(&dname, "%s" SLASH "hosts", confbase);
356 dir = opendir(dname);
358 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
363 while((ent = readdir(dir))) {
364 if(!check_id(ent->d_name))
367 node_t *n = lookup_node(ent->d_name);
372 n->name = xstrdup(ent->d_name);
380 char *get_name(void) {
383 get_config_string(lookup_config(config_tree, "Name"), &name);
389 char *envname = getenv(name + 1);
391 if(strcmp(name + 1, "HOST")) {
392 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
396 if(gethostname(envname, 32)) {
397 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno));
403 name = xstrdup(envname);
404 for(char *c = name; *c; c++)
409 if(!check_id(name)) {
410 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
418 bool setup_myself_reloadable(void) {
427 free(scriptinterpreter);
428 scriptinterpreter = NULL;
429 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
432 free(scriptextension);
433 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
435 scriptextension = xstrdup(".bat");
437 scriptextension = xstrdup("");
440 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
442 if((space = strchr(proxy, ' ')))
445 if(!strcasecmp(proxy, "none")) {
446 proxytype = PROXY_NONE;
447 } else if(!strcasecmp(proxy, "socks4")) {
448 proxytype = PROXY_SOCKS4;
449 } else if(!strcasecmp(proxy, "socks4a")) {
450 proxytype = PROXY_SOCKS4A;
451 } else if(!strcasecmp(proxy, "socks5")) {
452 proxytype = PROXY_SOCKS5;
453 } else if(!strcasecmp(proxy, "http")) {
454 proxytype = PROXY_HTTP;
455 } else if(!strcasecmp(proxy, "exec")) {
456 proxytype = PROXY_EXEC;
458 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
468 if(!space || !*space) {
469 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
472 proxyhost = xstrdup(space);
480 if(space && (space = strchr(space, ' ')))
481 *space++ = 0, proxyport = space;
482 if(space && (space = strchr(space, ' ')))
483 *space++ = 0, proxyuser = space;
484 if(space && (space = strchr(space, ' ')))
485 *space++ = 0, proxypass = space;
486 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
487 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
490 proxyhost = xstrdup(proxyhost);
491 proxyport = xstrdup(proxyport);
492 if(proxyuser && *proxyuser)
493 proxyuser = xstrdup(proxyuser);
494 if(proxypass && *proxypass)
495 proxypass = xstrdup(proxypass);
502 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
503 myself->options |= OPTION_INDIRECT;
505 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
506 myself->options |= OPTION_TCPONLY;
508 if(myself->options & OPTION_TCPONLY)
509 myself->options |= OPTION_INDIRECT;
511 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
512 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
514 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
515 if(!strcasecmp(rmode, "router"))
516 routing_mode = RMODE_ROUTER;
517 else if(!strcasecmp(rmode, "switch"))
518 routing_mode = RMODE_SWITCH;
519 else if(!strcasecmp(rmode, "hub"))
520 routing_mode = RMODE_HUB;
522 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
528 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
529 if(!strcasecmp(fmode, "off"))
530 forwarding_mode = FMODE_OFF;
531 else if(!strcasecmp(fmode, "internal"))
532 forwarding_mode = FMODE_INTERNAL;
533 else if(!strcasecmp(fmode, "kernel"))
534 forwarding_mode = FMODE_KERNEL;
536 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
543 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
545 myself->options |= OPTION_PMTU_DISCOVERY;
548 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
550 myself->options |= OPTION_CLAMP_MSS;
552 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
553 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
554 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
555 if(!strcasecmp(bmode, "no"))
556 broadcast_mode = BMODE_NONE;
557 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
558 broadcast_mode = BMODE_MST;
559 else if(!strcasecmp(bmode, "direct"))
560 broadcast_mode = BMODE_DIRECT;
562 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
568 #if !defined(SOL_IP) || !defined(IP_TOS)
569 if(priorityinheritance)
570 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
573 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
576 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
577 if(maxtimeout <= 0) {
578 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
584 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
585 if(!strcasecmp(afname, "IPv4"))
586 addressfamily = AF_INET;
587 else if(!strcasecmp(afname, "IPv6"))
588 addressfamily = AF_INET6;
589 else if(!strcasecmp(afname, "any"))
590 addressfamily = AF_UNSPEC;
592 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
598 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
600 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
603 get_config_int(lookup_config(config_tree, "AutoConnect"), &autoconnect);
605 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
611 Configure node_t myself and set up the local sockets (listen only)
613 static bool setup_myself(void) {
614 char *name, *hostname, *cipher, *digest, *type;
616 char *address = NULL;
618 if(!(name = get_name())) {
619 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
624 myself->connection = new_connection();
626 myself->connection->name = xstrdup(name);
627 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
628 read_config_options(config_tree, name);
629 read_config_file(config_tree, fname);
632 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
633 myport = xstrdup("655");
635 xasprintf(&myself->hostname, "MYSELF port %s", myport);
636 myself->connection->hostname = xstrdup(myself->hostname);
638 myself->connection->options = 0;
639 myself->connection->protocol_major = PROT_MAJOR;
640 myself->connection->protocol_minor = PROT_MINOR;
642 myself->options |= PROT_MINOR << 24;
644 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
646 if(experimental && !read_ecdsa_private_key())
649 if(!read_rsa_private_key())
653 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
655 if(!ai || !ai->ai_addr)
658 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
659 sockaddr2str(&sa, NULL, &myport);
662 /* Read in all the subnets specified in the host configuration file */
664 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
667 if(!get_config_subnet(cfg, &subnet))
670 subnet_add(myself, subnet);
673 /* Check some options */
675 if(!setup_myself_reloadable())
678 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
679 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
680 strictsubnets |= tunnelserver;
684 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
685 if(udp_rcvbuf <= 0) {
686 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
691 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
692 if(udp_sndbuf <= 0) {
693 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
699 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
700 if(replaywin_int < 0) {
701 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
704 replaywin = (unsigned)replaywin_int;
705 sptps_replaywin = replaywin;
708 /* Generate packet encryption key */
710 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
711 cipher = xstrdup("blowfish");
713 if(!(myself->incipher = cipher_open_by_name(cipher))) {
714 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
721 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
723 /* Check if we want to use message authentication codes... */
726 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
729 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
733 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
734 digest = xstrdup("sha1");
736 if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
737 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
745 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
746 if(myself->incompression < 0 || myself->incompression > 11) {
747 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
751 myself->incompression = 0;
753 myself->connection->outcompression = 0;
757 myself->nexthop = myself;
758 myself->via = myself;
759 myself->status.reachable = true;
760 myself->last_state_change = now.tv_sec;
761 myself->status.sptps = experimental;
775 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
776 if(!strcasecmp(type, "dummy"))
777 devops = dummy_devops;
778 else if(!strcasecmp(type, "raw_socket"))
779 devops = raw_socket_devops;
780 else if(!strcasecmp(type, "multicast"))
781 devops = multicast_devops;
783 else if(!strcasecmp(type, "uml"))
787 else if(!strcasecmp(type, "vde"))
796 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
798 /* Run tinc-up script to further initialize the tap interface */
800 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
801 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
802 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
803 xasprintf(&envp[3], "NAME=%s", myself->name);
806 execute_script("tinc-up", envp);
808 for(int i = 0; i < 4; i++)
811 /* Run subnet-up scripts for our own subnets */
813 subnet_update(myself, NULL, true);
818 int unix_fd = socket(AF_UNIX, SOCK_STREAM, 0);
820 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create UNIX socket: %s", sockstrerror(errno));
824 struct sockaddr_un sa;
825 sa.sun_family = AF_UNIX;
826 strncpy(sa.sun_path, unixsocketname, sizeof sa.sun_path);
828 if(connect(unix_fd, (struct sockaddr *)&sa, sizeof sa) >= 0) {
829 logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket %s is still in use!", unixsocketname);
833 unlink(unixsocketname);
835 if(bind(unix_fd, (struct sockaddr *)&sa, sizeof sa) < 0) {
836 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind UNIX socket to %s: %s", unixsocketname, sockstrerror(errno));
840 if(listen(unix_fd, 3) < 0) {
841 logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on UNIX socket %s: %s", unixsocketname, sockstrerror(errno));
845 io_add(&unix_socket, handle_new_unix_connection, &unix_socket, unix_fd, IO_READ);
848 if(!do_detach && getenv("LISTEN_FDS")) {
852 listen_sockets = atoi(getenv("LISTEN_FDS"));
854 unsetenv("LISTEN_FDS");
857 if(listen_sockets > MAXSOCKETS) {
858 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
862 for(int i = 0; i < listen_sockets; i++) {
864 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
865 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
870 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
873 int udp_fd = setup_vpn_in_socket(&sa);
877 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
878 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
880 if(debug_level >= DEBUG_CONNECTIONS) {
881 hostname = sockaddr2hostname(&sa);
882 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
886 memcpy(&listen_socket[i].sa, &sa, salen);
890 config_t *cfg = lookup_config(config_tree, "BindToAddress");
893 get_config_string(cfg, &address);
895 cfg = lookup_config_next(config_tree, cfg);
900 char *space = strchr(address, ' ');
906 if(!strcmp(address, "*"))
910 struct addrinfo *ai, hint = {0};
911 hint.ai_family = addressfamily;
912 hint.ai_socktype = SOCK_STREAM;
913 hint.ai_protocol = IPPROTO_TCP;
914 hint.ai_flags = AI_PASSIVE;
916 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
920 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
925 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
926 if(listen_sockets >= MAXSOCKETS) {
927 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
931 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
936 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
943 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
944 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
946 if(debug_level >= DEBUG_CONNECTIONS) {
947 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
948 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
952 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
961 logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
963 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
967 last_config_check = now.tv_sec;
975 bool setup_network(void) {
982 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
983 if(pinginterval < 1) {
984 pinginterval = 86400;
989 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
991 if(pingtimeout < 1 || pingtimeout > pinginterval)
992 pingtimeout = pinginterval;
994 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
995 maxoutbufsize = 10 * MTU;
1004 close all open network connections
1006 void close_network_connections(void) {
1007 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1009 connection_t *c = node->data;
1010 /* Keep control connections open until the end, so they know when we really terminated */
1011 if(c->status.control)
1014 terminate_connection(c, false);
1017 list_delete_list(outgoing_list);
1019 if(myself && myself->connection) {
1020 subnet_update(myself, NULL, false);
1021 terminate_connection(myself->connection, false);
1022 free_connection(myself->connection);
1025 for(int i = 0; i < listen_sockets; i++) {
1026 io_del(&listen_socket[i].tcp);
1027 io_del(&listen_socket[i].udp);
1028 close(listen_socket[i].tcp.fd);
1029 close(listen_socket[i].udp.fd);
1033 io_del(&unix_socket);
1034 close(unix_socket.fd);
1038 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
1039 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
1040 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
1041 xasprintf(&envp[3], "NAME=%s", myself->name);
1050 execute_script("tinc-down", envp);
1052 if(myport) free(myport);
1054 for(int i = 0; i < 4; i++)