3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2021 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.
28 #include "connection.h"
29 #include "compression.h"
54 static io_t device_io;
56 bool device_standby = false;
58 char *proxyhost = NULL;
59 char *proxyport = NULL;
60 char *proxyuser = NULL;
61 char *proxypass = NULL;
63 proxytype_t proxytype;
65 bool disablebuggypeers;
67 char *scriptinterpreter;
68 char *scriptextension;
70 bool node_read_ecdsa_public_key(node_t *n) {
71 if(ecdsa_active(n->ecdsa)) {
80 init_configuration(&config);
82 if(!read_host_config(&config, n->name, true)) {
86 /* First, check for simple Ed25519PublicKey statement */
88 if(get_config_string(lookup_config(&config, "Ed25519PublicKey"), &p)) {
89 n->ecdsa = ecdsa_set_base64_public_key(p);
94 /* Else, check for Ed25519PublicKeyFile statement and read it */
96 if(!get_config_string(lookup_config(&config, "Ed25519PublicKeyFile"), &pubname)) {
97 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
100 fp = fopen(pubname, "r");
106 n->ecdsa = ecdsa_read_pem_public_key(fp);
110 splay_empty_tree(&config);
115 static bool read_invitation_key(void) {
117 char fname[PATH_MAX];
120 ecdsa_free(invitation_key);
121 invitation_key = NULL;
124 snprintf(fname, sizeof(fname), "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
126 fp = fopen(fname, "r");
129 invitation_key = ecdsa_read_pem_private_key(fp);
132 if(!invitation_key) {
133 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
137 return invitation_key;
140 #ifndef DISABLE_LEGACY
141 static timeout_t keyexpire_timeout;
143 static void keyexpire_handler(void *data) {
145 timeout_set(data, &(struct timeval) {
146 keylifetime, jitter()
151 void regenerate_key(void) {
152 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
155 for splay_each(node_t, n, &node_tree) {
156 n->status.validkey_in = false;
160 void load_all_nodes(void) {
163 char dname[PATH_MAX];
165 snprintf(dname, sizeof(dname), "%s" SLASH "hosts", confbase);
166 dir = opendir(dname);
169 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
173 while((ent = readdir(dir))) {
174 if(!check_id(ent->d_name)) {
178 node_t *n = lookup_node(ent->d_name);
181 init_configuration(&config);
182 read_config_options(&config, ent->d_name);
183 read_host_config(&config, ent->d_name, true);
187 n->name = xstrdup(ent->d_name);
192 for(config_t *cfg = lookup_config(&config, "Subnet"); cfg; cfg = lookup_config_next(&config, cfg)) {
195 if(!get_config_subnet(cfg, &s)) {
199 if((s2 = lookup_subnet(n, s))) {
208 if(lookup_config(&config, "Address")) {
209 n->status.has_address = true;
212 splay_empty_tree(&config);
218 char *get_name(void) {
222 get_config_string(lookup_config(&config_tree, "Name"), &name);
228 returned_name = replace_name(name);
230 return returned_name;
233 bool setup_myself_reloadable(void) {
234 free(scriptinterpreter);
235 scriptinterpreter = NULL;
237 get_config_string(lookup_config(&config_tree, "ScriptsInterpreter"), &scriptinterpreter);
239 free(scriptextension);
241 if(!get_config_string(lookup_config(&config_tree, "ScriptsExtension"), &scriptextension)) {
242 scriptextension = xstrdup("");
247 get_config_string(lookup_config(&config_tree, "Proxy"), &proxy);
252 if((space = strchr(proxy, ' '))) {
256 if(!strcasecmp(proxy, "none")) {
257 proxytype = PROXY_NONE;
258 } else if(!strcasecmp(proxy, "socks4")) {
259 proxytype = PROXY_SOCKS4;
260 } else if(!strcasecmp(proxy, "socks4a")) {
261 proxytype = PROXY_SOCKS4A;
262 } else if(!strcasecmp(proxy, "socks5")) {
263 proxytype = PROXY_SOCKS5;
264 } else if(!strcasecmp(proxy, "http")) {
265 proxytype = PROXY_HTTP;
266 } else if(!strcasecmp(proxy, "exec")) {
267 proxytype = PROXY_EXEC;
269 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
292 if(!space || !*space) {
293 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
298 proxyhost = xstrdup(space);
307 if(space && (space = strchr(space, ' '))) {
308 *space++ = 0, proxyport = space;
311 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
312 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
319 if(space && (space = strchr(space, ' '))) {
320 *space++ = 0, proxyuser = space;
323 if(space && (space = strchr(space, ' '))) {
324 *space++ = 0, proxypass = space;
327 proxyhost = xstrdup(proxyhost);
328 proxyport = xstrdup(proxyport);
330 if(proxyuser && *proxyuser) {
331 proxyuser = xstrdup(proxyuser);
334 if(proxypass && *proxypass) {
335 proxypass = xstrdup(proxypass);
346 if(get_config_bool(lookup_config(&config_tree, "IndirectData"), &choice) && choice) {
347 myself->options |= OPTION_INDIRECT;
350 if(get_config_bool(lookup_config(&config_tree, "TCPOnly"), &choice) && choice) {
351 myself->options |= OPTION_TCPONLY;
354 if(myself->options & OPTION_TCPONLY) {
355 myself->options |= OPTION_INDIRECT;
358 get_config_bool(lookup_config(&config_tree, "UDPDiscovery"), &udp_discovery);
359 get_config_int(lookup_config(&config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval);
360 get_config_int(lookup_config(&config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval);
361 get_config_int(lookup_config(&config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout);
363 get_config_int(lookup_config(&config_tree, "MTUInfoInterval"), &mtu_info_interval);
364 get_config_int(lookup_config(&config_tree, "UDPInfoInterval"), &udp_info_interval);
366 get_config_bool(lookup_config(&config_tree, "DirectOnly"), &directonly);
367 get_config_bool(lookup_config(&config_tree, "LocalDiscovery"), &localdiscovery);
371 if(get_config_string(lookup_config(&config_tree, "Mode"), &rmode)) {
372 if(!strcasecmp(rmode, "router")) {
373 routing_mode = RMODE_ROUTER;
374 } else if(!strcasecmp(rmode, "switch")) {
375 routing_mode = RMODE_SWITCH;
376 } else if(!strcasecmp(rmode, "hub")) {
377 routing_mode = RMODE_HUB;
379 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
389 if(get_config_string(lookup_config(&config_tree, "Forwarding"), &fmode)) {
390 if(!strcasecmp(fmode, "off")) {
391 forwarding_mode = FMODE_OFF;
392 } else if(!strcasecmp(fmode, "internal")) {
393 forwarding_mode = FMODE_INTERNAL;
394 } else if(!strcasecmp(fmode, "kernel")) {
395 forwarding_mode = FMODE_KERNEL;
397 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
405 choice = !(myself->options & OPTION_TCPONLY);
406 get_config_bool(lookup_config(&config_tree, "PMTUDiscovery"), &choice);
409 myself->options |= OPTION_PMTU_DISCOVERY;
413 get_config_bool(lookup_config(&config_tree, "ClampMSS"), &choice);
416 myself->options |= OPTION_CLAMP_MSS;
419 get_config_bool(lookup_config(&config_tree, "PriorityInheritance"), &priorityinheritance);
420 get_config_bool(lookup_config(&config_tree, "DecrementTTL"), &decrement_ttl);
424 if(get_config_string(lookup_config(&config_tree, "Broadcast"), &bmode)) {
425 if(!strcasecmp(bmode, "no")) {
426 broadcast_mode = BMODE_NONE;
427 } else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst")) {
428 broadcast_mode = BMODE_MST;
429 } else if(!strcasecmp(bmode, "direct")) {
430 broadcast_mode = BMODE_DIRECT;
432 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
440 /* Delete all broadcast subnets before re-adding them */
442 for splay_each(subnet_t, s, &subnet_tree) {
444 splay_delete_node(&subnet_tree, node);
448 const char *const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
450 for(size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
451 subnet_t *s = new_subnet();
453 if(!str2net(s, DEFAULT_BROADCAST_SUBNETS[i])) {
460 for(config_t *cfg = lookup_config(&config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(&config_tree, cfg)) {
463 if(!get_config_subnet(cfg, &s)) {
472 if(priorityinheritance) {
473 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv4 connections", "PriorityInheritance");
478 #if !defined(IPV6_TCLASS)
480 if(priorityinheritance) {
481 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv6 connections", "PriorityInheritance");
486 if(!get_config_int(lookup_config(&config_tree, "MACExpire"), &macexpire)) {
490 if(get_config_int(lookup_config(&config_tree, "MaxTimeout"), &maxtimeout)) {
491 if(maxtimeout <= 0) {
492 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
501 if(get_config_string(lookup_config(&config_tree, "AddressFamily"), &afname)) {
502 if(!strcasecmp(afname, "IPv4")) {
503 addressfamily = AF_INET;
504 } else if(!strcasecmp(afname, "IPv6")) {
505 addressfamily = AF_INET6;
506 } else if(!strcasecmp(afname, "any")) {
507 addressfamily = AF_UNSPEC;
509 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
517 get_config_bool(lookup_config(&config_tree, "Hostnames"), &hostnames);
519 if(!get_config_int(lookup_config(&config_tree, "KeyExpire"), &keylifetime)) {
523 if(!get_config_bool(lookup_config(&config_tree, "AutoConnect"), &autoconnect)) {
527 get_config_bool(lookup_config(&config_tree, "DisableBuggyPeers"), &disablebuggypeers);
529 if(!get_config_int(lookup_config(&config_tree, "InvitationExpire"), &invitation_lifetime)) {
530 invitation_lifetime = 604800; // 1 week
533 read_invitation_key();
539 Add listening sockets.
541 static bool add_listen_address(char *address, bool bindto) {
545 char *space = strchr(address, ' ');
552 if(!strcmp(address, "*")) {
557 struct addrinfo *ai, hint = {0};
559 hint.ai_family = addressfamily;
561 hint.ai_socktype = SOCK_STREAM;
563 hint.ai_protocol = IPPROTO_TCP;
565 hint.ai_flags = AI_PASSIVE;
567 #if HAVE_DECL_RES_INIT
571 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
576 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
580 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
581 // Ignore duplicate addresses
584 for(int i = 0; i < listen_sockets; i++)
585 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
594 if(listen_sockets >= MAXSOCKETS) {
595 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
600 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
606 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
613 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
614 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
616 if(debug_level >= DEBUG_CONNECTIONS) {
617 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
618 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
622 listen_socket[listen_sockets].bindto = bindto;
623 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
631 void device_enable(void) {
636 /* Run tinc-up script to further initialize the tap interface */
639 environment_init(&env);
640 execute_script("tinc-up", &env);
641 environment_exit(&env);
644 void device_disable(void) {
646 environment_init(&env);
647 execute_script("tinc-down", &env);
648 environment_exit(&env);
656 Configure node_t myself and set up the local sockets (listen only)
658 static bool setup_myself(void) {
659 char *name, *hostname, *type;
660 char *address = NULL;
661 bool port_specified = false;
663 if(!(name = get_name())) {
664 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
668 myname = xstrdup(name);
670 myself->connection = new_connection();
672 myself->connection->name = xstrdup(name);
673 read_host_config(&config_tree, name, true);
675 if(!get_config_string(lookup_config(&config_tree, "Port"), &myport)) {
676 myport = xstrdup("655");
678 port_specified = true;
681 myself->connection->options = 0;
682 myself->connection->protocol_major = PROT_MAJOR;
683 myself->connection->protocol_minor = PROT_MINOR;
685 myself->options |= PROT_MINOR << 24;
687 #ifdef DISABLE_LEGACY
688 myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL);
689 experimental = myself->connection->ecdsa != NULL;
692 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
698 if(!get_config_bool(lookup_config(&config_tree, "ExperimentalProtocol"), &experimental)) {
699 myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL);
700 experimental = myself->connection->ecdsa != NULL;
703 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
707 myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL);
709 if(!myself->connection->ecdsa) {
715 myself->connection->rsa = read_rsa_private_key(&config_tree, NULL);
717 if(!myself->connection->rsa) {
719 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
721 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
728 /* Ensure myport is numeric */
731 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
734 if(!ai || !ai->ai_addr) {
739 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
741 sockaddr2str(&sa, NULL, &myport);
744 /* Read in all the subnets specified in the host configuration file */
746 for(config_t *cfg = lookup_config(&config_tree, "Subnet"); cfg; cfg = lookup_config_next(&config_tree, cfg)) {
749 if(!get_config_subnet(cfg, &subnet)) {
753 subnet_add(myself, subnet);
756 /* Check some options */
758 if(!setup_myself_reloadable()) {
762 get_config_bool(lookup_config(&config_tree, "StrictSubnets"), &strictsubnets);
763 get_config_bool(lookup_config(&config_tree, "TunnelServer"), &tunnelserver);
764 strictsubnets |= tunnelserver;
766 if(get_config_int(lookup_config(&config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
767 if(max_connection_burst <= 0) {
768 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
773 if(get_config_int(lookup_config(&config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
775 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
779 udp_rcvbuf_warnings = true;
782 if(get_config_int(lookup_config(&config_tree, "UDPSndBuf"), &udp_sndbuf)) {
784 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
788 udp_sndbuf_warnings = true;
791 get_config_int(lookup_config(&config_tree, "FWMark"), &fwmark);
795 logger(DEBUG_ALWAYS, LOG_ERR, "FWMark not supported on this platform!");
803 if(get_config_int(lookup_config(&config_tree, "ReplayWindow"), &replaywin_int)) {
804 if(replaywin_int < 0) {
805 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
809 replaywin = (unsigned)replaywin_int;
810 sptps_replaywin = replaywin;
813 #ifndef DISABLE_LEGACY
814 /* Generate packet encryption key */
818 if(!get_config_string(lookup_config(&config_tree, "Cipher"), &cipher)) {
819 cipher = xstrdup("aes-256-cbc");
822 if(!strcasecmp(cipher, "none")) {
823 myself->incipher = NULL;
825 myself->incipher = cipher_alloc();
827 if(!cipher_open_by_name(myself->incipher, cipher)) {
828 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
829 cipher_free(&myself->incipher);
837 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval) {
838 keylifetime, jitter()
841 /* Check if we want to use message authentication codes... */
844 get_config_int(lookup_config(&config_tree, "MACLength"), &maclength);
847 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
853 if(!get_config_string(lookup_config(&config_tree, "Digest"), &digest)) {
854 digest = xstrdup("sha256");
857 if(!strcasecmp(digest, "none")) {
858 myself->indigest = NULL;
860 myself->indigest = digest_alloc();
862 if(!digest_open_by_name(myself->indigest, digest, maclength)) {
863 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
864 digest_free(&myself->indigest);
874 if(get_config_int(lookup_config(&config_tree, "Compression"), &myself->incompression)) {
875 switch(myself->incompression) {
880 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
881 logger(DEBUG_ALWAYS, LOG_ERR, "LZ4 compression is unavailable on this node.");
885 case COMPRESS_LZO_HI:
886 case COMPRESS_LZO_LO:
890 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
891 logger(DEBUG_ALWAYS, LOG_ERR, "LZO compression is unavailable on this node.");
895 case COMPRESS_ZLIB_9:
896 case COMPRESS_ZLIB_8:
897 case COMPRESS_ZLIB_7:
898 case COMPRESS_ZLIB_6:
899 case COMPRESS_ZLIB_5:
900 case COMPRESS_ZLIB_4:
901 case COMPRESS_ZLIB_3:
902 case COMPRESS_ZLIB_2:
903 case COMPRESS_ZLIB_1:
907 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
908 logger(DEBUG_ALWAYS, LOG_ERR, "ZLIB compression is unavailable on this node.");
916 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
917 logger(DEBUG_ALWAYS, LOG_ERR, "Compression level %i is unrecognized by this node.", myself->incompression);
921 myself->incompression = COMPRESS_NONE;
924 myself->connection->outcompression = COMPRESS_NONE;
928 myself->nexthop = myself;
929 myself->via = myself;
930 myself->status.reachable = true;
931 myself->last_state_change = now.tv_sec;
932 myself->status.sptps = experimental;
943 if(get_config_string(lookup_config(&config_tree, "DeviceType"), &type)) {
944 if(!strcasecmp(type, "dummy")) {
945 devops = dummy_devops;
946 } else if(!strcasecmp(type, "raw_socket")) {
947 devops = raw_socket_devops;
948 } else if(!strcasecmp(type, "multicast")) {
949 devops = multicast_devops;
953 else if(!strcasecmp(type, "fd")) {
959 else if(!strcasecmp(type, "uml")) {
965 else if(!strcasecmp(type, "vde")) {
973 get_config_bool(lookup_config(&config_tree, "DeviceStandby"), &device_standby);
975 if(!devops.setup()) {
980 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
985 if(!do_detach && getenv("LISTEN_FDS")) {
989 listen_sockets = atoi(getenv("LISTEN_FDS"));
991 unsetenv("LISTEN_FDS");
994 if(listen_sockets > MAXSOCKETS) {
995 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
999 for(int i = 0; i < listen_sockets; i++) {
1002 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
1003 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
1008 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
1011 int udp_fd = setup_vpn_in_socket(&sa);
1017 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
1018 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
1020 if(debug_level >= DEBUG_CONNECTIONS) {
1021 hostname = sockaddr2hostname(&sa);
1022 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
1026 memcpy(&listen_socket[i].sa, &sa, salen);
1032 for(config_t *cfg = lookup_config(&config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(&config_tree, cfg)) {
1034 get_config_string(cfg, &address);
1036 if(!add_listen_address(address, true)) {
1041 for(config_t *cfg = lookup_config(&config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(&config_tree, cfg)) {
1043 get_config_string(cfg, &address);
1045 if(!add_listen_address(address, false)) {
1051 if(!add_listen_address(address, NULL)) {
1056 if(!listen_sockets) {
1057 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1061 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1063 if(!port_specified || atoi(myport) == 0) {
1065 socklen_t salen = sizeof(sa);
1067 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1069 sockaddr2str(&sa, NULL, &myport);
1072 myport = xstrdup("655");
1077 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1078 myself->connection->hostname = xstrdup(myself->hostname);
1081 get_config_string(lookup_config(&config_tree, "UPnP"), &upnp);
1082 bool upnp_tcp = false;
1083 bool upnp_udp = false;
1086 if(!strcasecmp(upnp, "yes")) {
1087 upnp_tcp = upnp_udp = true;
1088 } else if(!strcasecmp(upnp, "udponly")) {
1095 if(upnp_tcp || upnp_udp) {
1096 #ifdef HAVE_MINIUPNPC
1097 upnp_init(upnp_tcp, upnp_udp);
1099 logger(DEBUG_ALWAYS, LOG_WARNING, "UPnP was requested, but tinc isn't built with miniupnpc support!");
1105 last_config_check = now.tv_sec;
1113 bool setup_network(void) {
1117 if(get_config_int(lookup_config(&config_tree, "PingInterval"), &pinginterval)) {
1118 if(pinginterval < 1) {
1119 pinginterval = 86400;
1125 if(!get_config_int(lookup_config(&config_tree, "PingTimeout"), &pingtimeout)) {
1129 if(pingtimeout < 1 || pingtimeout > pinginterval) {
1130 pingtimeout = pinginterval;
1133 if(!get_config_int(lookup_config(&config_tree, "MaxOutputBufferSize"), &maxoutbufsize)) {
1134 maxoutbufsize = 10 * MTU;
1137 if(!setup_myself()) {
1141 if(!init_control()) {
1145 if(!device_standby) {
1149 /* Run subnet-up scripts for our own subnets */
1151 subnet_update(myself, NULL, true);
1157 close all open network connections
1159 void close_network_connections(void) {
1160 for(list_node_t *node = connection_list.head, *next; node; node = next) {
1162 connection_t *c = node->data;
1164 /* Keep control connections open until the end, so they know when we really terminated */
1165 if(c->status.control) {
1170 terminate_connection(c, false);
1173 list_empty_list(&outgoing_list);
1175 if(myself && myself->connection) {
1176 subnet_update(myself, NULL, false);
1177 free_connection(myself->connection);
1180 for(int i = 0; i < listen_sockets; i++) {
1181 io_del(&listen_socket[i].tcp);
1182 io_del(&listen_socket[i].udp);
1183 close(listen_socket[i].tcp.fd);
1184 close(listen_socket[i].udp.fd);
1193 if(!device_standby) {
1199 if(device_fd >= 0) {
1209 free(scriptextension);
1210 free(scriptinterpreter);