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));
199 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
202 if(fstat(fileno(fp), &s)) {
203 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
208 if(s.st_mode & ~0100700)
209 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
212 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
215 if(!myself->connection->ecdsa)
216 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
218 return myself->connection->ecdsa;
221 static bool read_rsa_private_key(void) {
226 /* First, check for simple PrivateKey statement */
228 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
229 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
230 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
234 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
237 return myself->connection->rsa;
240 /* Else, check for PrivateKeyFile statement and read it */
242 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
243 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
245 fp = fopen(fname, "r");
248 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
249 fname, strerror(errno));
254 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
257 if(fstat(fileno(fp), &s)) {
258 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
263 if(s.st_mode & ~0100700)
264 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
267 myself->connection->rsa = rsa_read_pem_private_key(fp);
270 if(!myself->connection->rsa)
271 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
273 return myself->connection->rsa;
276 static timeout_t keyexpire_timeout;
278 static void keyexpire_handler(void *data) {
280 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
283 void regenerate_key(void) {
284 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
289 Read Subnets from all host config files
291 void load_all_subnets(void) {
296 xasprintf(&dname, "%s" SLASH "hosts", confbase);
297 dir = opendir(dname);
299 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
304 while((ent = readdir(dir))) {
305 if(!check_id(ent->d_name))
308 node_t *n = lookup_node(ent->d_name);
309 #ifdef _DIRENT_HAVE_D_TYPE
310 //if(ent->d_type != DT_REG)
315 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
317 splay_tree_t *config_tree;
318 init_configuration(&config_tree);
319 read_config_options(config_tree, ent->d_name);
320 read_config_file(config_tree, fname);
325 n->name = xstrdup(ent->d_name);
329 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
332 if(!get_config_subnet(cfg, &s))
335 if((s2 = lookup_subnet(n, s))) {
342 exit_configuration(&config_tree);
348 void load_all_nodes(void) {
353 xasprintf(&dname, "%s" SLASH "hosts", confbase);
354 dir = opendir(dname);
356 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
361 while((ent = readdir(dir))) {
362 if(!check_id(ent->d_name))
365 node_t *n = lookup_node(ent->d_name);
370 n->name = xstrdup(ent->d_name);
378 char *get_name(void) {
381 get_config_string(lookup_config(config_tree, "Name"), &name);
387 char *envname = getenv(name + 1);
389 if(strcmp(name + 1, "HOST")) {
390 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
394 if(gethostname(envname, 32)) {
395 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno));
401 name = xstrdup(envname);
402 for(char *c = name; *c; c++)
407 if(!check_id(name)) {
408 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
416 bool setup_myself_reloadable(void) {
425 free(scriptinterpreter);
426 scriptinterpreter = NULL;
427 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
430 free(scriptextension);
431 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
433 scriptextension = xstrdup(".bat");
435 scriptextension = xstrdup("");
438 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
440 if((space = strchr(proxy, ' ')))
443 if(!strcasecmp(proxy, "none")) {
444 proxytype = PROXY_NONE;
445 } else if(!strcasecmp(proxy, "socks4")) {
446 proxytype = PROXY_SOCKS4;
447 } else if(!strcasecmp(proxy, "socks4a")) {
448 proxytype = PROXY_SOCKS4A;
449 } else if(!strcasecmp(proxy, "socks5")) {
450 proxytype = PROXY_SOCKS5;
451 } else if(!strcasecmp(proxy, "http")) {
452 proxytype = PROXY_HTTP;
453 } else if(!strcasecmp(proxy, "exec")) {
454 proxytype = PROXY_EXEC;
456 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
466 if(!space || !*space) {
467 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
470 proxyhost = xstrdup(space);
478 if(space && (space = strchr(space, ' ')))
479 *space++ = 0, proxyport = space;
480 if(space && (space = strchr(space, ' ')))
481 *space++ = 0, proxyuser = space;
482 if(space && (space = strchr(space, ' ')))
483 *space++ = 0, proxypass = space;
484 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
485 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
488 proxyhost = xstrdup(proxyhost);
489 proxyport = xstrdup(proxyport);
490 if(proxyuser && *proxyuser)
491 proxyuser = xstrdup(proxyuser);
492 if(proxypass && *proxypass)
493 proxypass = xstrdup(proxypass);
500 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
501 myself->options |= OPTION_INDIRECT;
503 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
504 myself->options |= OPTION_TCPONLY;
506 if(myself->options & OPTION_TCPONLY)
507 myself->options |= OPTION_INDIRECT;
509 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
510 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
512 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
513 if(!strcasecmp(rmode, "router"))
514 routing_mode = RMODE_ROUTER;
515 else if(!strcasecmp(rmode, "switch"))
516 routing_mode = RMODE_SWITCH;
517 else if(!strcasecmp(rmode, "hub"))
518 routing_mode = RMODE_HUB;
520 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
526 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
527 if(!strcasecmp(fmode, "off"))
528 forwarding_mode = FMODE_OFF;
529 else if(!strcasecmp(fmode, "internal"))
530 forwarding_mode = FMODE_INTERNAL;
531 else if(!strcasecmp(fmode, "kernel"))
532 forwarding_mode = FMODE_KERNEL;
534 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
541 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
543 myself->options |= OPTION_PMTU_DISCOVERY;
546 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
548 myself->options |= OPTION_CLAMP_MSS;
550 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
551 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
552 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
553 if(!strcasecmp(bmode, "no"))
554 broadcast_mode = BMODE_NONE;
555 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
556 broadcast_mode = BMODE_MST;
557 else if(!strcasecmp(bmode, "direct"))
558 broadcast_mode = BMODE_DIRECT;
560 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
566 #if !defined(SOL_IP) || !defined(IP_TOS)
567 if(priorityinheritance)
568 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
571 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
574 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
575 if(maxtimeout <= 0) {
576 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
582 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
583 if(!strcasecmp(afname, "IPv4"))
584 addressfamily = AF_INET;
585 else if(!strcasecmp(afname, "IPv6"))
586 addressfamily = AF_INET6;
587 else if(!strcasecmp(afname, "any"))
588 addressfamily = AF_UNSPEC;
590 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
596 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
598 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
601 get_config_int(lookup_config(config_tree, "AutoConnect"), &autoconnect);
603 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
609 Configure node_t myself and set up the local sockets (listen only)
611 static bool setup_myself(void) {
612 char *name, *hostname, *cipher, *digest, *type;
614 char *address = NULL;
616 if(!(name = get_name())) {
617 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
622 myself->connection = new_connection();
624 myself->connection->name = xstrdup(name);
625 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
626 read_config_options(config_tree, name);
627 read_config_file(config_tree, fname);
630 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
631 myport = xstrdup("655");
633 xasprintf(&myself->hostname, "MYSELF port %s", myport);
634 myself->connection->hostname = xstrdup(myself->hostname);
636 myself->connection->options = 0;
637 myself->connection->protocol_major = PROT_MAJOR;
638 myself->connection->protocol_minor = PROT_MINOR;
640 myself->options |= PROT_MINOR << 24;
642 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
644 if(experimental && !read_ecdsa_private_key())
647 if(!read_rsa_private_key())
651 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
653 if(!ai || !ai->ai_addr)
656 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
657 sockaddr2str(&sa, NULL, &myport);
660 /* Read in all the subnets specified in the host configuration file */
662 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
665 if(!get_config_subnet(cfg, &subnet))
668 subnet_add(myself, subnet);
671 /* Check some options */
673 if(!setup_myself_reloadable())
676 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
677 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
678 strictsubnets |= tunnelserver;
682 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
683 if(udp_rcvbuf <= 0) {
684 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
689 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
690 if(udp_sndbuf <= 0) {
691 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
697 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
698 if(replaywin_int < 0) {
699 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
702 replaywin = (unsigned)replaywin_int;
703 sptps_replaywin = replaywin;
706 /* Generate packet encryption key */
708 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
709 cipher = xstrdup("blowfish");
711 if(!(myself->incipher = cipher_open_by_name(cipher))) {
712 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
719 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
721 /* Check if we want to use message authentication codes... */
724 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
727 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
731 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
732 digest = xstrdup("sha1");
734 if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
735 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
743 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
744 if(myself->incompression < 0 || myself->incompression > 11) {
745 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
749 myself->incompression = 0;
751 myself->connection->outcompression = 0;
755 myself->nexthop = myself;
756 myself->via = myself;
757 myself->status.reachable = true;
758 myself->last_state_change = now.tv_sec;
759 myself->status.sptps = experimental;
773 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
774 if(!strcasecmp(type, "dummy"))
775 devops = dummy_devops;
776 else if(!strcasecmp(type, "raw_socket"))
777 devops = raw_socket_devops;
778 else if(!strcasecmp(type, "multicast"))
779 devops = multicast_devops;
781 else if(!strcasecmp(type, "uml"))
785 else if(!strcasecmp(type, "vde"))
794 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
796 /* Run tinc-up script to further initialize the tap interface */
798 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
799 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
800 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
801 xasprintf(&envp[3], "NAME=%s", myself->name);
804 execute_script("tinc-up", envp);
806 for(int i = 0; i < 4; i++)
809 /* Run subnet-up scripts for our own subnets */
811 subnet_update(myself, NULL, true);
816 int unix_fd = socket(AF_UNIX, SOCK_STREAM, 0);
818 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create UNIX socket: %s", sockstrerror(errno));
822 struct sockaddr_un sa;
823 sa.sun_family = AF_UNIX;
824 strncpy(sa.sun_path, unixsocketname, sizeof sa.sun_path);
826 if(connect(unix_fd, (struct sockaddr *)&sa, sizeof sa) >= 0) {
827 logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket %s is still in use!", unixsocketname);
831 unlink(unixsocketname);
833 if(bind(unix_fd, (struct sockaddr *)&sa, sizeof sa) < 0) {
834 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind UNIX socket to %s: %s", unixsocketname, sockstrerror(errno));
838 if(listen(unix_fd, 3) < 0) {
839 logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on UNIX socket %s: %s", unixsocketname, sockstrerror(errno));
843 io_add(&unix_socket, handle_new_unix_connection, &unix_socket, unix_fd, IO_READ);
846 if(!do_detach && getenv("LISTEN_FDS")) {
850 listen_sockets = atoi(getenv("LISTEN_FDS"));
852 unsetenv("LISTEN_FDS");
855 if(listen_sockets > MAXSOCKETS) {
856 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
860 for(int i = 0; i < listen_sockets; i++) {
862 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
863 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
868 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
871 int udp_fd = setup_vpn_in_socket(&sa);
875 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
876 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
878 if(debug_level >= DEBUG_CONNECTIONS) {
879 hostname = sockaddr2hostname(&sa);
880 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
884 memcpy(&listen_socket[i].sa, &sa, salen);
888 config_t *cfg = lookup_config(config_tree, "BindToAddress");
891 get_config_string(cfg, &address);
893 cfg = lookup_config_next(config_tree, cfg);
898 char *space = strchr(address, ' ');
904 if(!strcmp(address, "*"))
908 struct addrinfo *ai, hint = {0};
909 hint.ai_family = addressfamily;
910 hint.ai_socktype = SOCK_STREAM;
911 hint.ai_protocol = IPPROTO_TCP;
912 hint.ai_flags = AI_PASSIVE;
914 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
918 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
923 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
924 if(listen_sockets >= MAXSOCKETS) {
925 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
929 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
934 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
941 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
942 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
944 if(debug_level >= DEBUG_CONNECTIONS) {
945 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
946 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
950 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
959 logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
961 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
965 last_config_check = now.tv_sec;
973 bool setup_network(void) {
980 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
981 if(pinginterval < 1) {
982 pinginterval = 86400;
987 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
989 if(pingtimeout < 1 || pingtimeout > pinginterval)
990 pingtimeout = pinginterval;
992 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
993 maxoutbufsize = 10 * MTU;
1002 close all open network connections
1004 void close_network_connections(void) {
1005 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1007 connection_t *c = node->data;
1008 /* Keep control connections open until the end, so they know when we really terminated */
1009 if(c->status.control)
1012 terminate_connection(c, false);
1015 list_delete_list(outgoing_list);
1017 if(myself && myself->connection) {
1018 subnet_update(myself, NULL, false);
1019 terminate_connection(myself->connection, false);
1020 free_connection(myself->connection);
1023 for(int i = 0; i < listen_sockets; i++) {
1024 io_del(&listen_socket[i].tcp);
1025 io_del(&listen_socket[i].udp);
1026 close(listen_socket[i].tcp.fd);
1027 close(listen_socket[i].udp.fd);
1031 io_del(&unix_socket);
1032 close(unix_socket.fd);
1036 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
1037 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
1038 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
1039 xasprintf(&envp[3], "NAME=%s", myself->name);
1048 execute_script("tinc-down", envp);
1050 if(myport) free(myport);
1052 for(int i = 0; i < 4; i++)