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"
53 static io_t device_io;
55 bool device_standby = false;
57 char *proxyhost = NULL;
58 char *proxyport = NULL;
59 char *proxyuser = NULL;
60 char *proxypass = NULL;
62 proxytype_t proxytype;
64 bool disablebuggypeers;
66 char *scriptinterpreter;
67 char *scriptextension;
69 bool node_read_ecdsa_public_key(node_t *n) {
70 if(ecdsa_active(n->ecdsa)) {
79 init_configuration(&config);
81 if(!read_host_config(&config, n->name, true)) {
85 /* First, check for simple Ed25519PublicKey statement */
87 if(get_config_string(lookup_config(&config, "Ed25519PublicKey"), &p)) {
88 n->ecdsa = ecdsa_set_base64_public_key(p);
93 /* Else, check for Ed25519PublicKeyFile statement and read it */
95 if(!get_config_string(lookup_config(&config, "Ed25519PublicKeyFile"), &pubname)) {
96 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
99 fp = fopen(pubname, "r");
105 n->ecdsa = ecdsa_read_pem_public_key(fp);
109 splay_empty_tree(&config);
114 static bool read_invitation_key(void) {
116 char fname[PATH_MAX];
119 ecdsa_free(invitation_key);
120 invitation_key = NULL;
123 snprintf(fname, sizeof(fname), "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
125 fp = fopen(fname, "r");
128 invitation_key = ecdsa_read_pem_private_key(fp);
131 if(!invitation_key) {
132 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
136 return invitation_key;
139 #ifndef DISABLE_LEGACY
140 static timeout_t keyexpire_timeout;
142 static void keyexpire_handler(void *data) {
144 timeout_set(data, &(struct timeval) {
145 keylifetime, rand() % 100000
150 void regenerate_key(void) {
151 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
154 for splay_each(node_t, n, &node_tree) {
155 n->status.validkey_in = false;
159 void load_all_nodes(void) {
162 char dname[PATH_MAX];
164 snprintf(dname, sizeof(dname), "%s" SLASH "hosts", confbase);
165 dir = opendir(dname);
168 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
172 while((ent = readdir(dir))) {
173 if(!check_id(ent->d_name)) {
177 node_t *n = lookup_node(ent->d_name);
180 init_configuration(&config);
181 read_config_options(&config, ent->d_name);
182 read_host_config(&config, ent->d_name, true);
186 n->name = xstrdup(ent->d_name);
191 for(config_t *cfg = lookup_config(&config, "Subnet"); cfg; cfg = lookup_config_next(&config, cfg)) {
194 if(!get_config_subnet(cfg, &s)) {
198 if((s2 = lookup_subnet(n, s))) {
207 if(lookup_config(&config, "Address")) {
208 n->status.has_address = true;
211 splay_empty_tree(&config);
217 char *get_name(void) {
221 get_config_string(lookup_config(&config_tree, "Name"), &name);
227 returned_name = replace_name(name);
229 return returned_name;
232 bool setup_myself_reloadable(void) {
233 free(scriptinterpreter);
234 scriptinterpreter = NULL;
236 get_config_string(lookup_config(&config_tree, "ScriptsInterpreter"), &scriptinterpreter);
238 free(scriptextension);
240 if(!get_config_string(lookup_config(&config_tree, "ScriptsExtension"), &scriptextension)) {
241 scriptextension = xstrdup("");
246 get_config_string(lookup_config(&config_tree, "Proxy"), &proxy);
251 if((space = strchr(proxy, ' '))) {
255 if(!strcasecmp(proxy, "none")) {
256 proxytype = PROXY_NONE;
257 } else if(!strcasecmp(proxy, "socks4")) {
258 proxytype = PROXY_SOCKS4;
259 } else if(!strcasecmp(proxy, "socks4a")) {
260 proxytype = PROXY_SOCKS4A;
261 } else if(!strcasecmp(proxy, "socks5")) {
262 proxytype = PROXY_SOCKS5;
263 } else if(!strcasecmp(proxy, "http")) {
264 proxytype = PROXY_HTTP;
265 } else if(!strcasecmp(proxy, "exec")) {
266 proxytype = PROXY_EXEC;
268 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
291 if(!space || !*space) {
292 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
297 proxyhost = xstrdup(space);
306 if(space && (space = strchr(space, ' '))) {
307 *space++ = 0, proxyport = space;
310 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
311 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
318 if(space && (space = strchr(space, ' '))) {
319 *space++ = 0, proxyuser = space;
322 if(space && (space = strchr(space, ' '))) {
323 *space++ = 0, proxypass = space;
326 proxyhost = xstrdup(proxyhost);
327 proxyport = xstrdup(proxyport);
329 if(proxyuser && *proxyuser) {
330 proxyuser = xstrdup(proxyuser);
333 if(proxypass && *proxypass) {
334 proxypass = xstrdup(proxypass);
345 if(get_config_bool(lookup_config(&config_tree, "IndirectData"), &choice) && choice) {
346 myself->options |= OPTION_INDIRECT;
349 if(get_config_bool(lookup_config(&config_tree, "TCPOnly"), &choice) && choice) {
350 myself->options |= OPTION_TCPONLY;
353 if(myself->options & OPTION_TCPONLY) {
354 myself->options |= OPTION_INDIRECT;
357 get_config_bool(lookup_config(&config_tree, "UDPDiscovery"), &udp_discovery);
358 get_config_int(lookup_config(&config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval);
359 get_config_int(lookup_config(&config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval);
360 get_config_int(lookup_config(&config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout);
362 get_config_int(lookup_config(&config_tree, "MTUInfoInterval"), &mtu_info_interval);
363 get_config_int(lookup_config(&config_tree, "UDPInfoInterval"), &udp_info_interval);
365 get_config_bool(lookup_config(&config_tree, "DirectOnly"), &directonly);
366 get_config_bool(lookup_config(&config_tree, "LocalDiscovery"), &localdiscovery);
370 if(get_config_string(lookup_config(&config_tree, "Mode"), &rmode)) {
371 if(!strcasecmp(rmode, "router")) {
372 routing_mode = RMODE_ROUTER;
373 } else if(!strcasecmp(rmode, "switch")) {
374 routing_mode = RMODE_SWITCH;
375 } else if(!strcasecmp(rmode, "hub")) {
376 routing_mode = RMODE_HUB;
378 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
388 if(get_config_string(lookup_config(&config_tree, "Forwarding"), &fmode)) {
389 if(!strcasecmp(fmode, "off")) {
390 forwarding_mode = FMODE_OFF;
391 } else if(!strcasecmp(fmode, "internal")) {
392 forwarding_mode = FMODE_INTERNAL;
393 } else if(!strcasecmp(fmode, "kernel")) {
394 forwarding_mode = FMODE_KERNEL;
396 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
404 choice = !(myself->options & OPTION_TCPONLY);
405 get_config_bool(lookup_config(&config_tree, "PMTUDiscovery"), &choice);
408 myself->options |= OPTION_PMTU_DISCOVERY;
412 get_config_bool(lookup_config(&config_tree, "ClampMSS"), &choice);
415 myself->options |= OPTION_CLAMP_MSS;
418 get_config_bool(lookup_config(&config_tree, "PriorityInheritance"), &priorityinheritance);
419 get_config_bool(lookup_config(&config_tree, "DecrementTTL"), &decrement_ttl);
423 if(get_config_string(lookup_config(&config_tree, "Broadcast"), &bmode)) {
424 if(!strcasecmp(bmode, "no")) {
425 broadcast_mode = BMODE_NONE;
426 } else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst")) {
427 broadcast_mode = BMODE_MST;
428 } else if(!strcasecmp(bmode, "direct")) {
429 broadcast_mode = BMODE_DIRECT;
431 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
439 /* Delete all broadcast subnets before re-adding them */
441 for splay_each(subnet_t, s, &subnet_tree) {
443 splay_delete_node(&subnet_tree, node);
447 const char *const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
449 for(size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
450 subnet_t *s = new_subnet();
452 if(!str2net(s, DEFAULT_BROADCAST_SUBNETS[i])) {
459 for(config_t *cfg = lookup_config(&config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(&config_tree, cfg)) {
462 if(!get_config_subnet(cfg, &s)) {
471 if(priorityinheritance) {
472 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv4 connections", "PriorityInheritance");
477 #if !defined(IPV6_TCLASS)
479 if(priorityinheritance) {
480 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform for IPv6 connections", "PriorityInheritance");
485 if(!get_config_int(lookup_config(&config_tree, "MACExpire"), &macexpire)) {
489 if(get_config_int(lookup_config(&config_tree, "MaxTimeout"), &maxtimeout)) {
490 if(maxtimeout <= 0) {
491 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
500 if(get_config_string(lookup_config(&config_tree, "AddressFamily"), &afname)) {
501 if(!strcasecmp(afname, "IPv4")) {
502 addressfamily = AF_INET;
503 } else if(!strcasecmp(afname, "IPv6")) {
504 addressfamily = AF_INET6;
505 } else if(!strcasecmp(afname, "any")) {
506 addressfamily = AF_UNSPEC;
508 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
516 get_config_bool(lookup_config(&config_tree, "Hostnames"), &hostnames);
518 if(!get_config_int(lookup_config(&config_tree, "KeyExpire"), &keylifetime)) {
522 if(!get_config_bool(lookup_config(&config_tree, "AutoConnect"), &autoconnect)) {
526 get_config_bool(lookup_config(&config_tree, "DisableBuggyPeers"), &disablebuggypeers);
528 if(!get_config_int(lookup_config(&config_tree, "InvitationExpire"), &invitation_lifetime)) {
529 invitation_lifetime = 604800; // 1 week
532 read_invitation_key();
538 Add listening sockets.
540 static bool add_listen_address(char *address, bool bindto) {
544 char *space = strchr(address, ' ');
551 if(!strcmp(address, "*")) {
556 struct addrinfo *ai, hint = {0};
558 hint.ai_family = addressfamily;
560 hint.ai_socktype = SOCK_STREAM;
562 hint.ai_protocol = IPPROTO_TCP;
564 hint.ai_flags = AI_PASSIVE;
566 #if HAVE_DECL_RES_INIT
570 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
575 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
579 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
580 // Ignore duplicate addresses
583 for(int i = 0; i < listen_sockets; i++)
584 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
593 if(listen_sockets >= MAXSOCKETS) {
594 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
599 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
605 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
612 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
613 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
615 if(debug_level >= DEBUG_CONNECTIONS) {
616 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
617 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
621 listen_socket[listen_sockets].bindto = bindto;
622 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
630 void device_enable(void) {
635 /* Run tinc-up script to further initialize the tap interface */
638 environment_init(&env);
639 execute_script("tinc-up", &env);
640 environment_exit(&env);
643 void device_disable(void) {
645 environment_init(&env);
646 execute_script("tinc-down", &env);
647 environment_exit(&env);
655 Configure node_t myself and set up the local sockets (listen only)
657 static bool setup_myself(void) {
658 char *name, *hostname, *type;
659 char *address = NULL;
660 bool port_specified = false;
662 if(!(name = get_name())) {
663 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
667 myname = xstrdup(name);
669 myself->connection = new_connection();
671 myself->connection->name = xstrdup(name);
672 read_host_config(&config_tree, name, true);
674 if(!get_config_string(lookup_config(&config_tree, "Port"), &myport)) {
675 myport = xstrdup("655");
677 port_specified = true;
680 myself->connection->options = 0;
681 myself->connection->protocol_major = PROT_MAJOR;
682 myself->connection->protocol_minor = PROT_MINOR;
684 myself->options |= PROT_MINOR << 24;
686 #ifdef DISABLE_LEGACY
687 myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL);
688 experimental = myself->connection->ecdsa != NULL;
691 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
697 if(!get_config_bool(lookup_config(&config_tree, "ExperimentalProtocol"), &experimental)) {
698 myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL);
699 experimental = myself->connection->ecdsa != NULL;
702 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
706 myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL);
708 if(!myself->connection->ecdsa) {
714 myself->connection->rsa = read_rsa_private_key(&config_tree, NULL);
716 if(!myself->connection->rsa) {
718 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
720 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
727 /* Ensure myport is numeric */
730 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
733 if(!ai || !ai->ai_addr) {
738 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
740 sockaddr2str(&sa, NULL, &myport);
743 /* Read in all the subnets specified in the host configuration file */
745 for(config_t *cfg = lookup_config(&config_tree, "Subnet"); cfg; cfg = lookup_config_next(&config_tree, cfg)) {
748 if(!get_config_subnet(cfg, &subnet)) {
752 subnet_add(myself, subnet);
755 /* Check some options */
757 if(!setup_myself_reloadable()) {
761 get_config_bool(lookup_config(&config_tree, "StrictSubnets"), &strictsubnets);
762 get_config_bool(lookup_config(&config_tree, "TunnelServer"), &tunnelserver);
763 strictsubnets |= tunnelserver;
765 if(get_config_int(lookup_config(&config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
766 if(max_connection_burst <= 0) {
767 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
772 if(get_config_int(lookup_config(&config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
774 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
778 udp_rcvbuf_warnings = true;
781 if(get_config_int(lookup_config(&config_tree, "UDPSndBuf"), &udp_sndbuf)) {
783 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
787 udp_sndbuf_warnings = true;
790 get_config_int(lookup_config(&config_tree, "FWMark"), &fwmark);
794 logger(DEBUG_ALWAYS, LOG_ERR, "FWMark not supported on this platform!");
802 if(get_config_int(lookup_config(&config_tree, "ReplayWindow"), &replaywin_int)) {
803 if(replaywin_int < 0) {
804 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
808 replaywin = (unsigned)replaywin_int;
809 sptps_replaywin = replaywin;
812 #ifndef DISABLE_LEGACY
813 /* Generate packet encryption key */
817 if(!get_config_string(lookup_config(&config_tree, "Cipher"), &cipher)) {
818 cipher = xstrdup("aes-256-cbc");
821 if(!strcasecmp(cipher, "none")) {
822 myself->incipher = NULL;
823 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
824 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
831 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval) {
832 keylifetime, rand() % 100000
835 /* Check if we want to use message authentication codes... */
838 get_config_int(lookup_config(&config_tree, "MACLength"), &maclength);
841 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
847 if(!get_config_string(lookup_config(&config_tree, "Digest"), &digest)) {
848 digest = xstrdup("sha256");
851 if(!strcasecmp(digest, "none")) {
852 myself->indigest = NULL;
853 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
854 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
863 if(get_config_int(lookup_config(&config_tree, "Compression"), &myself->incompression)) {
864 switch(myself->incompression) {
869 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
870 logger(DEBUG_ALWAYS, LOG_ERR, "LZ4 compression is unavailable on this node.");
874 case COMPRESS_LZO_HI:
875 case COMPRESS_LZO_LO:
879 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
880 logger(DEBUG_ALWAYS, LOG_ERR, "LZO compression is unavailable on this node.");
884 case COMPRESS_ZLIB_9:
885 case COMPRESS_ZLIB_8:
886 case COMPRESS_ZLIB_7:
887 case COMPRESS_ZLIB_6:
888 case COMPRESS_ZLIB_5:
889 case COMPRESS_ZLIB_4:
890 case COMPRESS_ZLIB_3:
891 case COMPRESS_ZLIB_2:
892 case COMPRESS_ZLIB_1:
896 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
897 logger(DEBUG_ALWAYS, LOG_ERR, "ZLIB compression is unavailable on this node.");
905 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
906 logger(DEBUG_ALWAYS, LOG_ERR, "Compression level %i is unrecognized by this node.", myself->incompression);
910 myself->incompression = COMPRESS_NONE;
913 myself->connection->outcompression = COMPRESS_NONE;
917 myself->nexthop = myself;
918 myself->via = myself;
919 myself->status.reachable = true;
920 myself->last_state_change = now.tv_sec;
921 myself->status.sptps = experimental;
932 if(get_config_string(lookup_config(&config_tree, "DeviceType"), &type)) {
933 if(!strcasecmp(type, "dummy")) {
934 devops = dummy_devops;
935 } else if(!strcasecmp(type, "raw_socket")) {
936 devops = raw_socket_devops;
937 } else if(!strcasecmp(type, "multicast")) {
938 devops = multicast_devops;
942 else if(!strcasecmp(type, "fd")) {
948 else if(!strcasecmp(type, "uml")) {
954 else if(!strcasecmp(type, "vde")) {
962 get_config_bool(lookup_config(&config_tree, "DeviceStandby"), &device_standby);
964 if(!devops.setup()) {
969 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
974 if(!do_detach && getenv("LISTEN_FDS")) {
978 listen_sockets = atoi(getenv("LISTEN_FDS"));
980 unsetenv("LISTEN_FDS");
983 if(listen_sockets > MAXSOCKETS) {
984 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
988 for(int i = 0; i < listen_sockets; i++) {
991 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
992 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
997 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
1000 int udp_fd = setup_vpn_in_socket(&sa);
1006 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
1007 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
1009 if(debug_level >= DEBUG_CONNECTIONS) {
1010 hostname = sockaddr2hostname(&sa);
1011 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
1015 memcpy(&listen_socket[i].sa, &sa, salen);
1021 for(config_t *cfg = lookup_config(&config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(&config_tree, cfg)) {
1023 get_config_string(cfg, &address);
1025 if(!add_listen_address(address, true)) {
1030 for(config_t *cfg = lookup_config(&config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(&config_tree, cfg)) {
1032 get_config_string(cfg, &address);
1034 if(!add_listen_address(address, false)) {
1040 if(!add_listen_address(address, NULL)) {
1045 if(!listen_sockets) {
1046 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1050 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1052 if(!port_specified || atoi(myport) == 0) {
1054 socklen_t salen = sizeof(sa);
1056 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1058 sockaddr2str(&sa, NULL, &myport);
1061 myport = xstrdup("655");
1066 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1067 myself->connection->hostname = xstrdup(myself->hostname);
1070 get_config_string(lookup_config(&config_tree, "UPnP"), &upnp);
1071 bool upnp_tcp = false;
1072 bool upnp_udp = false;
1075 if(!strcasecmp(upnp, "yes")) {
1076 upnp_tcp = upnp_udp = true;
1077 } else if(!strcasecmp(upnp, "udponly")) {
1084 if(upnp_tcp || upnp_udp) {
1085 #ifdef HAVE_MINIUPNPC
1086 upnp_init(upnp_tcp, upnp_udp);
1088 logger(DEBUG_ALWAYS, LOG_WARNING, "UPnP was requested, but tinc isn't built with miniupnpc support!");
1094 last_config_check = now.tv_sec;
1102 bool setup_network(void) {
1106 if(get_config_int(lookup_config(&config_tree, "PingInterval"), &pinginterval)) {
1107 if(pinginterval < 1) {
1108 pinginterval = 86400;
1114 if(!get_config_int(lookup_config(&config_tree, "PingTimeout"), &pingtimeout)) {
1118 if(pingtimeout < 1 || pingtimeout > pinginterval) {
1119 pingtimeout = pinginterval;
1122 if(!get_config_int(lookup_config(&config_tree, "MaxOutputBufferSize"), &maxoutbufsize)) {
1123 maxoutbufsize = 10 * MTU;
1126 if(!setup_myself()) {
1130 if(!init_control()) {
1134 if(!device_standby) {
1138 /* Run subnet-up scripts for our own subnets */
1140 subnet_update(myself, NULL, true);
1146 close all open network connections
1148 void close_network_connections(void) {
1149 for(list_node_t *node = connection_list.head, *next; node; node = next) {
1151 connection_t *c = node->data;
1153 /* Keep control connections open until the end, so they know when we really terminated */
1154 if(c->status.control) {
1159 terminate_connection(c, false);
1162 list_empty_list(&outgoing_list);
1164 if(myself && myself->connection) {
1165 subnet_update(myself, NULL, false);
1166 free_connection(myself->connection);
1169 for(int i = 0; i < listen_sockets; i++) {
1170 io_del(&listen_socket[i].tcp);
1171 io_del(&listen_socket[i].udp);
1172 close(listen_socket[i].tcp.fd);
1173 close(listen_socket[i].udp.fd);
1182 if(!device_standby) {
1188 if(device_fd >= 0) {
1198 free(scriptextension);
1199 free(scriptinterpreter);