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 if(!c->config_tree) {
114 init_configuration(&c->config_tree);
115 if(!read_host_config(c->config_tree, c->name))
119 /* First, check for simple ECDSAPublicKey statement */
121 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
122 c->ecdsa = ecdsa_set_base64_public_key(p);
127 /* Else, check for ECDSAPublicKeyFile statement and read it */
129 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
130 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
132 fp = fopen(fname, "r");
135 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s",
136 fname, strerror(errno));
141 c->ecdsa = ecdsa_read_pem_public_key(fp);
145 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname);
150 bool read_rsa_public_key(connection_t *c) {
151 if(ecdsa_active(c->ecdsa))
158 /* First, check for simple PublicKey statement */
160 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
161 c->rsa = rsa_set_hex_public_key(n, "FFFF");
166 /* Else, check for PublicKeyFile statement and read it */
168 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
169 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
171 fp = fopen(fname, "r");
174 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
179 c->rsa = rsa_read_pem_public_key(fp);
183 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
188 static bool read_ecdsa_private_key(void) {
192 /* Check for PrivateKeyFile statement and read it */
194 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
195 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
197 fp = fopen(fname, "r");
200 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
202 logger(DEBUG_ALWAYS, LOG_INFO, "Create an ECDSA keypair with `tinc -n %s generate-ecdsa-keys'.", netname ?: ".");
207 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
210 if(fstat(fileno(fp), &s)) {
211 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
216 if(s.st_mode & ~0100700)
217 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
220 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
223 if(!myself->connection->ecdsa)
224 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
226 return myself->connection->ecdsa;
229 static bool read_rsa_private_key(void) {
234 /* First, check for simple PrivateKey statement */
236 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
237 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
238 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
242 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
245 return myself->connection->rsa;
248 /* Else, check for PrivateKeyFile statement and read it */
250 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
251 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
253 fp = fopen(fname, "r");
256 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
257 fname, strerror(errno));
262 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
265 if(fstat(fileno(fp), &s)) {
266 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
271 if(s.st_mode & ~0100700)
272 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
275 myself->connection->rsa = rsa_read_pem_private_key(fp);
278 if(!myself->connection->rsa)
279 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
281 return myself->connection->rsa;
284 static timeout_t keyexpire_timeout;
286 static void keyexpire_handler(void *data) {
288 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
291 void regenerate_key(void) {
292 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
297 Read Subnets from all host config files
299 void load_all_subnets(void) {
304 xasprintf(&dname, "%s" SLASH "hosts", confbase);
305 dir = opendir(dname);
307 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
312 while((ent = readdir(dir))) {
313 if(!check_id(ent->d_name))
316 node_t *n = lookup_node(ent->d_name);
317 #ifdef _DIRENT_HAVE_D_TYPE
318 //if(ent->d_type != DT_REG)
323 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
325 splay_tree_t *config_tree;
326 init_configuration(&config_tree);
327 read_config_options(config_tree, ent->d_name);
328 read_config_file(config_tree, fname);
333 n->name = xstrdup(ent->d_name);
337 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
340 if(!get_config_subnet(cfg, &s))
343 if((s2 = lookup_subnet(n, s))) {
350 exit_configuration(&config_tree);
356 void load_all_nodes(void) {
361 xasprintf(&dname, "%s" SLASH "hosts", confbase);
362 dir = opendir(dname);
364 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
369 while((ent = readdir(dir))) {
370 if(!check_id(ent->d_name))
373 node_t *n = lookup_node(ent->d_name);
378 n->name = xstrdup(ent->d_name);
386 char *get_name(void) {
389 get_config_string(lookup_config(config_tree, "Name"), &name);
395 char *envname = getenv(name + 1);
397 if(strcmp(name + 1, "HOST")) {
398 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
402 if(gethostname(envname, 32)) {
403 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno));
409 name = xstrdup(envname);
410 for(char *c = name; *c; c++)
415 if(!check_id(name)) {
416 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
424 bool setup_myself_reloadable(void) {
433 free(scriptinterpreter);
434 scriptinterpreter = NULL;
435 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
438 free(scriptextension);
439 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
441 scriptextension = xstrdup(".bat");
443 scriptextension = xstrdup("");
446 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
448 if((space = strchr(proxy, ' ')))
451 if(!strcasecmp(proxy, "none")) {
452 proxytype = PROXY_NONE;
453 } else if(!strcasecmp(proxy, "socks4")) {
454 proxytype = PROXY_SOCKS4;
455 } else if(!strcasecmp(proxy, "socks4a")) {
456 proxytype = PROXY_SOCKS4A;
457 } else if(!strcasecmp(proxy, "socks5")) {
458 proxytype = PROXY_SOCKS5;
459 } else if(!strcasecmp(proxy, "http")) {
460 proxytype = PROXY_HTTP;
461 } else if(!strcasecmp(proxy, "exec")) {
462 proxytype = PROXY_EXEC;
464 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
474 if(!space || !*space) {
475 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
478 proxyhost = xstrdup(space);
486 if(space && (space = strchr(space, ' ')))
487 *space++ = 0, proxyport = space;
488 if(space && (space = strchr(space, ' ')))
489 *space++ = 0, proxyuser = space;
490 if(space && (space = strchr(space, ' ')))
491 *space++ = 0, proxypass = space;
492 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
493 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
496 proxyhost = xstrdup(proxyhost);
497 proxyport = xstrdup(proxyport);
498 if(proxyuser && *proxyuser)
499 proxyuser = xstrdup(proxyuser);
500 if(proxypass && *proxypass)
501 proxypass = xstrdup(proxypass);
508 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
509 myself->options |= OPTION_INDIRECT;
511 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
512 myself->options |= OPTION_TCPONLY;
514 if(myself->options & OPTION_TCPONLY)
515 myself->options |= OPTION_INDIRECT;
517 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
518 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
520 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
521 if(!strcasecmp(rmode, "router"))
522 routing_mode = RMODE_ROUTER;
523 else if(!strcasecmp(rmode, "switch"))
524 routing_mode = RMODE_SWITCH;
525 else if(!strcasecmp(rmode, "hub"))
526 routing_mode = RMODE_HUB;
528 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
534 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
535 if(!strcasecmp(fmode, "off"))
536 forwarding_mode = FMODE_OFF;
537 else if(!strcasecmp(fmode, "internal"))
538 forwarding_mode = FMODE_INTERNAL;
539 else if(!strcasecmp(fmode, "kernel"))
540 forwarding_mode = FMODE_KERNEL;
542 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
549 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
551 myself->options |= OPTION_PMTU_DISCOVERY;
554 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
556 myself->options |= OPTION_CLAMP_MSS;
558 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
559 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
560 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
561 if(!strcasecmp(bmode, "no"))
562 broadcast_mode = BMODE_NONE;
563 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
564 broadcast_mode = BMODE_MST;
565 else if(!strcasecmp(bmode, "direct"))
566 broadcast_mode = BMODE_DIRECT;
568 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
574 #if !defined(SOL_IP) || !defined(IP_TOS)
575 if(priorityinheritance)
576 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
579 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
582 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
583 if(maxtimeout <= 0) {
584 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
590 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
591 if(!strcasecmp(afname, "IPv4"))
592 addressfamily = AF_INET;
593 else if(!strcasecmp(afname, "IPv6"))
594 addressfamily = AF_INET6;
595 else if(!strcasecmp(afname, "any"))
596 addressfamily = AF_UNSPEC;
598 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
604 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
606 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
609 get_config_int(lookup_config(config_tree, "AutoConnect"), &autoconnect);
611 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
617 Configure node_t myself and set up the local sockets (listen only)
619 static bool setup_myself(void) {
620 char *name, *hostname, *cipher, *digest, *type;
622 char *address = NULL;
624 if(!(name = get_name())) {
625 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
630 myself->connection = new_connection();
632 myself->connection->name = xstrdup(name);
633 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
634 read_config_options(config_tree, name);
635 read_config_file(config_tree, fname);
638 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
639 myport = xstrdup("655");
641 xasprintf(&myself->hostname, "MYSELF port %s", myport);
642 myself->connection->hostname = xstrdup(myself->hostname);
644 myself->connection->options = 0;
645 myself->connection->protocol_major = PROT_MAJOR;
646 myself->connection->protocol_minor = PROT_MINOR;
648 myself->options |= PROT_MINOR << 24;
650 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
652 if(experimental && !read_ecdsa_private_key())
655 if(!read_rsa_private_key())
659 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
661 if(!ai || !ai->ai_addr)
664 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
665 sockaddr2str(&sa, NULL, &myport);
668 /* Read in all the subnets specified in the host configuration file */
670 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
673 if(!get_config_subnet(cfg, &subnet))
676 subnet_add(myself, subnet);
679 /* Check some options */
681 if(!setup_myself_reloadable())
684 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
685 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
686 strictsubnets |= tunnelserver;
690 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
691 if(udp_rcvbuf <= 0) {
692 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
697 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
698 if(udp_sndbuf <= 0) {
699 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
705 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
706 if(replaywin_int < 0) {
707 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
710 replaywin = (unsigned)replaywin_int;
711 sptps_replaywin = replaywin;
714 /* Generate packet encryption key */
716 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
717 cipher = xstrdup("blowfish");
719 if(!(myself->incipher = cipher_open_by_name(cipher))) {
720 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
727 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
729 /* Check if we want to use message authentication codes... */
732 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
735 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
739 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
740 digest = xstrdup("sha1");
742 if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
743 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
751 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
752 if(myself->incompression < 0 || myself->incompression > 11) {
753 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
757 myself->incompression = 0;
759 myself->connection->outcompression = 0;
763 myself->nexthop = myself;
764 myself->via = myself;
765 myself->status.reachable = true;
766 myself->last_state_change = now.tv_sec;
767 myself->status.sptps = experimental;
781 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
782 if(!strcasecmp(type, "dummy"))
783 devops = dummy_devops;
784 else if(!strcasecmp(type, "raw_socket"))
785 devops = raw_socket_devops;
786 else if(!strcasecmp(type, "multicast"))
787 devops = multicast_devops;
789 else if(!strcasecmp(type, "uml"))
793 else if(!strcasecmp(type, "vde"))
802 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
804 /* Run tinc-up script to further initialize the tap interface */
806 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
807 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
808 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
809 xasprintf(&envp[3], "NAME=%s", myself->name);
812 execute_script("tinc-up", envp);
814 for(int i = 0; i < 4; i++)
817 /* Run subnet-up scripts for our own subnets */
819 subnet_update(myself, NULL, true);
824 int unix_fd = socket(AF_UNIX, SOCK_STREAM, 0);
826 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create UNIX socket: %s", sockstrerror(errno));
830 struct sockaddr_un sa;
831 sa.sun_family = AF_UNIX;
832 strncpy(sa.sun_path, unixsocketname, sizeof sa.sun_path);
834 if(connect(unix_fd, (struct sockaddr *)&sa, sizeof sa) >= 0) {
835 logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket %s is still in use!", unixsocketname);
839 unlink(unixsocketname);
841 if(bind(unix_fd, (struct sockaddr *)&sa, sizeof sa) < 0) {
842 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind UNIX socket to %s: %s", unixsocketname, sockstrerror(errno));
846 if(listen(unix_fd, 3) < 0) {
847 logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on UNIX socket %s: %s", unixsocketname, sockstrerror(errno));
851 io_add(&unix_socket, handle_new_unix_connection, &unix_socket, unix_fd, IO_READ);
854 if(!do_detach && getenv("LISTEN_FDS")) {
858 listen_sockets = atoi(getenv("LISTEN_FDS"));
860 unsetenv("LISTEN_FDS");
863 if(listen_sockets > MAXSOCKETS) {
864 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
868 for(int i = 0; i < listen_sockets; i++) {
870 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
871 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
876 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
879 int udp_fd = setup_vpn_in_socket(&sa);
883 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
884 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
886 if(debug_level >= DEBUG_CONNECTIONS) {
887 hostname = sockaddr2hostname(&sa);
888 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
892 memcpy(&listen_socket[i].sa, &sa, salen);
896 config_t *cfg = lookup_config(config_tree, "BindToAddress");
899 get_config_string(cfg, &address);
901 cfg = lookup_config_next(config_tree, cfg);
906 char *space = strchr(address, ' ');
912 if(!strcmp(address, "*"))
916 struct addrinfo *ai, hint = {0};
917 hint.ai_family = addressfamily;
918 hint.ai_socktype = SOCK_STREAM;
919 hint.ai_protocol = IPPROTO_TCP;
920 hint.ai_flags = AI_PASSIVE;
922 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
926 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
931 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
932 if(listen_sockets >= MAXSOCKETS) {
933 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
937 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
942 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
949 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
950 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
952 if(debug_level >= DEBUG_CONNECTIONS) {
953 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
954 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
958 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
967 logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
969 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
973 last_config_check = now.tv_sec;
981 bool setup_network(void) {
988 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
989 if(pinginterval < 1) {
990 pinginterval = 86400;
995 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
997 if(pingtimeout < 1 || pingtimeout > pinginterval)
998 pingtimeout = pinginterval;
1000 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
1001 maxoutbufsize = 10 * MTU;
1010 close all open network connections
1012 void close_network_connections(void) {
1013 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1015 connection_t *c = node->data;
1016 /* Keep control connections open until the end, so they know when we really terminated */
1017 if(c->status.control)
1020 terminate_connection(c, false);
1023 list_delete_list(outgoing_list);
1025 if(myself && myself->connection) {
1026 subnet_update(myself, NULL, false);
1027 terminate_connection(myself->connection, false);
1028 free_connection(myself->connection);
1031 for(int i = 0; i < listen_sockets; i++) {
1032 io_del(&listen_socket[i].tcp);
1033 io_del(&listen_socket[i].udp);
1034 close(listen_socket[i].tcp.fd);
1035 close(listen_socket[i].udp.fd);
1039 io_del(&unix_socket);
1040 close(unix_socket.fd);
1044 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
1045 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
1046 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
1047 xasprintf(&envp[3], "NAME=%s", myself->name);
1056 execute_script("tinc-down", envp);
1058 if(myport) free(myport);
1060 for(int i = 0; i < 4; i++)