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"
47 static io_t device_io;
54 proxytype_t proxytype;
56 bool disablebuggypeers;
58 char *scriptinterpreter;
59 char *scriptextension;
61 bool node_read_ecdsa_public_key(node_t *n) {
62 if(ecdsa_active(n->ecdsa))
65 splay_tree_t *config_tree;
70 init_configuration(&config_tree);
71 if(!read_host_config(config_tree, n->name))
74 /* First, check for simple ECDSAPublicKey statement */
76 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
77 n->ecdsa = ecdsa_set_base64_public_key(p);
82 /* Else, check for ECDSAPublicKeyFile statement and read it */
84 if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &pubname))
85 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
87 fp = fopen(pubname, "r");
90 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s", pubname, strerror(errno));
94 n->ecdsa = ecdsa_read_pem_public_key(fp);
98 exit_configuration(&config_tree);
103 bool read_ecdsa_public_key(connection_t *c) {
104 if(ecdsa_active(c->ecdsa))
111 if(!c->config_tree) {
112 init_configuration(&c->config_tree);
113 if(!read_host_config(c->config_tree, c->name))
117 /* First, check for simple ECDSAPublicKey statement */
119 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
120 c->ecdsa = ecdsa_set_base64_public_key(p);
125 /* Else, check for ECDSAPublicKeyFile statement and read it */
127 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
128 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
130 fp = fopen(fname, "r");
133 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s",
134 fname, strerror(errno));
139 c->ecdsa = ecdsa_read_pem_public_key(fp);
143 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname);
148 bool read_rsa_public_key(connection_t *c) {
149 if(ecdsa_active(c->ecdsa))
156 /* First, check for simple PublicKey statement */
158 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
159 c->rsa = rsa_set_hex_public_key(n, "FFFF");
164 /* Else, check for PublicKeyFile statement and read it */
166 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
167 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
169 fp = fopen(fname, "r");
172 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
177 c->rsa = rsa_read_pem_public_key(fp);
181 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
186 static bool read_ecdsa_private_key(void) {
190 /* Check for PrivateKeyFile statement and read it */
192 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
193 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
195 fp = fopen(fname, "r");
198 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
200 logger(DEBUG_ALWAYS, LOG_INFO, "Create an ECDSA keypair with `tinc -n %s generate-ecdsa-keys'.", netname ?: ".");
205 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
208 if(fstat(fileno(fp), &s)) {
209 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
214 if(s.st_mode & ~0100700)
215 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
218 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
221 if(!myself->connection->ecdsa)
222 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
224 return myself->connection->ecdsa;
227 static bool read_invitation_key(void) {
232 ecdsa_free(invitation_key);
233 invitation_key = NULL;
236 xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", confbase);
238 fp = fopen(fname, "r");
241 invitation_key = ecdsa_read_pem_private_key(fp);
244 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
248 return invitation_key;
251 static bool read_rsa_private_key(void) {
256 /* First, check for simple PrivateKey statement */
258 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
259 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
260 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
264 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
267 return myself->connection->rsa;
270 /* Else, check for PrivateKeyFile statement and read it */
272 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
273 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
275 fp = fopen(fname, "r");
278 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
279 fname, strerror(errno));
284 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
287 if(fstat(fileno(fp), &s)) {
288 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
293 if(s.st_mode & ~0100700)
294 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
297 myself->connection->rsa = rsa_read_pem_private_key(fp);
300 if(!myself->connection->rsa)
301 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
303 return myself->connection->rsa;
306 static timeout_t keyexpire_timeout;
308 static void keyexpire_handler(void *data) {
310 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
313 void regenerate_key(void) {
314 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
319 Read Subnets from all host config files
321 void load_all_subnets(void) {
326 xasprintf(&dname, "%s" SLASH "hosts", confbase);
327 dir = opendir(dname);
329 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
334 while((ent = readdir(dir))) {
335 if(!check_id(ent->d_name))
338 node_t *n = lookup_node(ent->d_name);
339 #ifdef _DIRENT_HAVE_D_TYPE
340 //if(ent->d_type != DT_REG)
344 splay_tree_t *config_tree;
345 init_configuration(&config_tree);
346 read_config_options(config_tree, ent->d_name);
347 read_host_config(config_tree, ent->d_name);
351 n->name = xstrdup(ent->d_name);
355 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
358 if(!get_config_subnet(cfg, &s))
361 if((s2 = lookup_subnet(n, s))) {
368 exit_configuration(&config_tree);
374 void load_all_nodes(void) {
379 xasprintf(&dname, "%s" SLASH "hosts", confbase);
380 dir = opendir(dname);
382 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
387 while((ent = readdir(dir))) {
388 if(!check_id(ent->d_name))
391 node_t *n = lookup_node(ent->d_name);
396 n->name = xstrdup(ent->d_name);
404 char *get_name(void) {
407 get_config_string(lookup_config(config_tree, "Name"), &name);
413 char *envname = getenv(name + 1);
414 char hostname[32] = "";
416 if(strcmp(name + 1, "HOST")) {
417 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
420 if(gethostname(hostname, sizeof hostname) || !*hostname) {
421 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno));
428 name = xstrdup(envname);
429 for(char *c = name; *c; c++)
434 if(!check_id(name)) {
435 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
443 bool setup_myself_reloadable(void) {
449 char *address = NULL;
453 free(scriptinterpreter);
454 scriptinterpreter = NULL;
455 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
458 free(scriptextension);
459 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
460 scriptextension = xstrdup("");
462 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
464 if((space = strchr(proxy, ' ')))
467 if(!strcasecmp(proxy, "none")) {
468 proxytype = PROXY_NONE;
469 } else if(!strcasecmp(proxy, "socks4")) {
470 proxytype = PROXY_SOCKS4;
471 } else if(!strcasecmp(proxy, "socks4a")) {
472 proxytype = PROXY_SOCKS4A;
473 } else if(!strcasecmp(proxy, "socks5")) {
474 proxytype = PROXY_SOCKS5;
475 } else if(!strcasecmp(proxy, "http")) {
476 proxytype = PROXY_HTTP;
477 } else if(!strcasecmp(proxy, "exec")) {
478 proxytype = PROXY_EXEC;
480 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
490 if(!space || !*space) {
491 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
494 proxyhost = xstrdup(space);
502 if(space && (space = strchr(space, ' ')))
503 *space++ = 0, proxyport = space;
504 if(space && (space = strchr(space, ' ')))
505 *space++ = 0, proxyuser = space;
506 if(space && (space = strchr(space, ' ')))
507 *space++ = 0, proxypass = space;
508 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
509 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
512 proxyhost = xstrdup(proxyhost);
513 proxyport = xstrdup(proxyport);
514 if(proxyuser && *proxyuser)
515 proxyuser = xstrdup(proxyuser);
516 if(proxypass && *proxypass)
517 proxypass = xstrdup(proxypass);
524 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
525 myself->options |= OPTION_INDIRECT;
527 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
528 myself->options |= OPTION_TCPONLY;
530 if(myself->options & OPTION_TCPONLY)
531 myself->options |= OPTION_INDIRECT;
533 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
534 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
536 memset(&localdiscovery_address, 0, sizeof localdiscovery_address);
537 if(get_config_string(lookup_config(config_tree, "LocalDiscoveryAddress"), &address)) {
538 struct addrinfo *ai = str2addrinfo(address, myport, SOCK_DGRAM);
542 memcpy(&localdiscovery_address, ai->ai_addr, ai->ai_addrlen);
546 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
547 if(!strcasecmp(rmode, "router"))
548 routing_mode = RMODE_ROUTER;
549 else if(!strcasecmp(rmode, "switch"))
550 routing_mode = RMODE_SWITCH;
551 else if(!strcasecmp(rmode, "hub"))
552 routing_mode = RMODE_HUB;
554 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
560 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
561 if(!strcasecmp(fmode, "off"))
562 forwarding_mode = FMODE_OFF;
563 else if(!strcasecmp(fmode, "internal"))
564 forwarding_mode = FMODE_INTERNAL;
565 else if(!strcasecmp(fmode, "kernel"))
566 forwarding_mode = FMODE_KERNEL;
568 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
575 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
577 myself->options |= OPTION_PMTU_DISCOVERY;
580 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
582 myself->options |= OPTION_CLAMP_MSS;
584 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
585 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
586 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
587 if(!strcasecmp(bmode, "no"))
588 broadcast_mode = BMODE_NONE;
589 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
590 broadcast_mode = BMODE_MST;
591 else if(!strcasecmp(bmode, "direct"))
592 broadcast_mode = BMODE_DIRECT;
594 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
600 #if !defined(SOL_IP) || !defined(IP_TOS)
601 if(priorityinheritance)
602 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
605 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
608 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
609 if(maxtimeout <= 0) {
610 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
616 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
617 if(!strcasecmp(afname, "IPv4"))
618 addressfamily = AF_INET;
619 else if(!strcasecmp(afname, "IPv6"))
620 addressfamily = AF_INET6;
621 else if(!strcasecmp(afname, "any"))
622 addressfamily = AF_UNSPEC;
624 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
630 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
632 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
635 get_config_int(lookup_config(config_tree, "AutoConnect"), &autoconnect);
639 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
641 read_invitation_key();
647 Configure node_t myself and set up the local sockets (listen only)
649 static bool setup_myself(void) {
650 char *name, *hostname, *cipher, *digest, *type;
651 char *address = NULL;
652 bool port_specified = false;
654 if(!(name = get_name())) {
655 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
660 myself->connection = new_connection();
662 myself->connection->name = xstrdup(name);
663 read_host_config(config_tree, name);
665 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
666 myport = xstrdup("655");
668 port_specified = true;
670 myself->connection->options = 0;
671 myself->connection->protocol_major = PROT_MAJOR;
672 myself->connection->protocol_minor = PROT_MINOR;
674 myself->options |= PROT_MINOR << 24;
676 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
678 if(experimental && !read_ecdsa_private_key())
681 if(!read_rsa_private_key())
684 /* Ensure myport is numeric */
687 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
689 if(!ai || !ai->ai_addr)
692 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
693 sockaddr2str(&sa, NULL, &myport);
696 /* Read in all the subnets specified in the host configuration file */
698 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
701 if(!get_config_subnet(cfg, &subnet))
704 subnet_add(myself, subnet);
707 /* Check some options */
709 if(!setup_myself_reloadable())
712 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
713 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
714 strictsubnets |= tunnelserver;
716 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
717 if(max_connection_burst <= 0) {
718 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
723 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
724 if(udp_rcvbuf <= 0) {
725 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
730 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
731 if(udp_sndbuf <= 0) {
732 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
738 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
739 if(replaywin_int < 0) {
740 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
743 replaywin = (unsigned)replaywin_int;
744 sptps_replaywin = replaywin;
747 /* Generate packet encryption key */
749 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
750 cipher = xstrdup("blowfish");
752 if(!strcasecmp(cipher, "none")) {
753 myself->incipher = NULL;
754 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
755 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
761 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
763 /* Check if we want to use message authentication codes... */
766 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
769 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
773 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
774 digest = xstrdup("sha1");
776 if(!strcasecmp(digest, "none")) {
777 myself->indigest = NULL;
778 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
779 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
787 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
788 if(myself->incompression < 0 || myself->incompression > 11) {
789 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
793 myself->incompression = 0;
795 myself->connection->outcompression = 0;
799 myself->nexthop = myself;
800 myself->via = myself;
801 myself->status.reachable = true;
802 myself->last_state_change = now.tv_sec;
803 myself->status.sptps = experimental;
817 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
818 if(!strcasecmp(type, "dummy"))
819 devops = dummy_devops;
820 else if(!strcasecmp(type, "raw_socket"))
821 devops = raw_socket_devops;
822 else if(!strcasecmp(type, "multicast"))
823 devops = multicast_devops;
825 else if(!strcasecmp(type, "uml"))
829 else if(!strcasecmp(type, "vde"))
838 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
842 if(!do_detach && getenv("LISTEN_FDS")) {
846 listen_sockets = atoi(getenv("LISTEN_FDS"));
848 unsetenv("LISTEN_FDS");
851 if(listen_sockets > MAXSOCKETS) {
852 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
856 for(int i = 0; i < listen_sockets; i++) {
858 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
859 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
864 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
867 int udp_fd = setup_vpn_in_socket(&sa);
871 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
872 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
874 if(debug_level >= DEBUG_CONNECTIONS) {
875 hostname = sockaddr2hostname(&sa);
876 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
880 memcpy(&listen_socket[i].sa, &sa, salen);
884 config_t *cfg = lookup_config(config_tree, "BindToAddress");
887 get_config_string(cfg, &address);
889 cfg = lookup_config_next(config_tree, cfg);
894 char *space = strchr(address, ' ');
900 if(!strcmp(address, "*"))
904 struct addrinfo *ai, hint = {0};
905 hint.ai_family = addressfamily;
906 hint.ai_socktype = SOCK_STREAM;
907 hint.ai_protocol = IPPROTO_TCP;
908 hint.ai_flags = AI_PASSIVE;
910 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
914 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
918 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
919 if(listen_sockets >= MAXSOCKETS) {
920 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
924 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
929 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
936 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
937 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
939 if(debug_level >= DEBUG_CONNECTIONS) {
940 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
941 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
945 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
953 if(!listen_sockets) {
954 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
958 /* If no Port option was specified, set myport to the port used by the first listening socket. */
960 if(!port_specified) {
962 socklen_t salen = sizeof sa;
963 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
965 sockaddr2str(&sa, NULL, &myport);
967 myport = xstrdup("655");
971 xasprintf(&myself->hostname, "MYSELF port %s", myport);
972 myself->connection->hostname = xstrdup(myself->hostname);
976 last_config_check = now.tv_sec;
984 bool setup_network(void) {
991 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
992 if(pinginterval < 1) {
993 pinginterval = 86400;
998 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
1000 if(pingtimeout < 1 || pingtimeout > pinginterval)
1001 pingtimeout = pinginterval;
1003 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
1004 maxoutbufsize = 10 * MTU;
1012 /* Run tinc-up script to further initialize the tap interface */
1014 char *envp[5] = {NULL};
1015 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
1016 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
1017 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
1018 xasprintf(&envp[3], "NAME=%s", myself->name);
1020 execute_script("tinc-up", envp);
1022 for(int i = 0; i < 4; i++)
1025 /* Run subnet-up scripts for our own subnets */
1027 subnet_update(myself, NULL, true);
1033 close all open network connections
1035 void close_network_connections(void) {
1036 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1038 connection_t *c = node->data;
1039 /* Keep control connections open until the end, so they know when we really terminated */
1040 if(c->status.control)
1043 terminate_connection(c, false);
1046 list_delete_list(outgoing_list);
1048 if(myself && myself->connection) {
1049 subnet_update(myself, NULL, false);
1050 terminate_connection(myself->connection, false);
1051 free_connection(myself->connection);
1054 for(int i = 0; i < listen_sockets; i++) {
1055 io_del(&listen_socket[i].tcp);
1056 io_del(&listen_socket[i].udp);
1057 close(listen_socket[i].tcp.fd);
1058 close(listen_socket[i].udp.fd);
1061 char *envp[5] = {NULL};
1062 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
1063 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
1064 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
1065 xasprintf(&envp[3], "NAME=%s", myself->name);
1073 execute_script("tinc-down", envp);
1075 if(myport) free(myport);
1077 for(int i = 0; i < 4; i++)