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;
70 xasprintf(&hcfname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
72 init_configuration(&config_tree);
73 if(!read_config_file(config_tree, hcfname))
76 /* First, check for simple ECDSAPublicKey statement */
78 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
79 result = ecdsa_set_base64_public_key(&n->ecdsa, p);
84 /* Else, check for ECDSAPublicKeyFile statement and read it */
86 if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &pubname))
87 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
89 fp = fopen(pubname, "r");
92 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s", pubname, strerror(errno));
96 result = ecdsa_read_pem_public_key(&n->ecdsa, fp);
100 exit_configuration(&config_tree);
106 bool read_ecdsa_public_key(connection_t *c) {
112 /* First, check for simple ECDSAPublicKey statement */
114 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
115 result = ecdsa_set_base64_public_key(&c->ecdsa, p);
120 /* Else, check for ECDSAPublicKeyFile statement and read it */
122 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
123 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
125 fp = fopen(fname, "r");
128 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s",
129 fname, strerror(errno));
134 result = ecdsa_read_pem_public_key(&c->ecdsa, fp);
138 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname);
143 bool read_rsa_public_key(connection_t *c) {
149 /* First, check for simple PublicKey statement */
151 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
152 result = rsa_set_hex_public_key(&c->rsa, n, "FFFF");
157 /* Else, check for PublicKeyFile statement and read it */
159 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
160 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
162 fp = fopen(fname, "r");
165 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
170 result = rsa_read_pem_public_key(&c->rsa, fp);
174 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
179 static bool read_ecdsa_private_key(void) {
184 /* Check for PrivateKeyFile statement and read it */
186 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
187 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
189 fp = fopen(fname, "r");
192 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
197 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
200 if(fstat(fileno(fp), &s)) {
201 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
206 if(s.st_mode & ~0100700)
207 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
210 result = ecdsa_read_pem_private_key(&myself->connection->ecdsa, fp);
214 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
219 static bool read_rsa_private_key(void) {
225 /* First, check for simple PrivateKey statement */
227 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
228 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
229 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
233 result = rsa_set_hex_private_key(&myself->connection->rsa, n, "FFFF", d);
239 /* Else, check for PrivateKeyFile statement and read it */
241 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
242 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
244 fp = fopen(fname, "r");
247 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
248 fname, strerror(errno));
253 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
256 if(fstat(fileno(fp), &s)) {
257 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
262 if(s.st_mode & ~0100700)
263 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
266 result = rsa_read_pem_private_key(&myself->connection->rsa, fp);
270 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
275 static timeout_t keyexpire_timeout;
277 static void keyexpire_handler(void *data) {
279 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
282 void regenerate_key(void) {
283 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
288 Read Subnets from all host config files
290 void load_all_subnets(void) {
295 xasprintf(&dname, "%s" SLASH "hosts", confbase);
296 dir = opendir(dname);
298 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
303 while((ent = readdir(dir))) {
304 if(!check_id(ent->d_name))
307 node_t *n = lookup_node(ent->d_name);
308 #ifdef _DIRENT_HAVE_D_TYPE
309 //if(ent->d_type != DT_REG)
314 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
316 splay_tree_t *config_tree;
317 init_configuration(&config_tree);
318 read_config_options(config_tree, ent->d_name);
319 read_config_file(config_tree, fname);
324 n->name = xstrdup(ent->d_name);
328 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
331 if(!get_config_subnet(cfg, &s))
334 if((s2 = lookup_subnet(n, s))) {
341 exit_configuration(&config_tree);
347 void load_all_nodes(void) {
352 xasprintf(&dname, "%s" SLASH "hosts", confbase);
353 dir = opendir(dname);
355 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
360 while((ent = readdir(dir))) {
361 if(!check_id(ent->d_name))
364 node_t *n = lookup_node(ent->d_name);
369 n->name = xstrdup(ent->d_name);
377 char *get_name(void) {
380 get_config_string(lookup_config(config_tree, "Name"), &name);
386 char *envname = getenv(name + 1);
388 if(strcmp(name + 1, "HOST")) {
389 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
393 if(gethostname(envname, 32)) {
394 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno));
400 name = xstrdup(envname);
401 for(char *c = name; *c; c++)
406 if(!check_id(name)) {
407 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
415 bool setup_myself_reloadable(void) {
424 free(scriptinterpreter);
425 scriptinterpreter = NULL;
426 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
429 free(scriptextension);
430 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
432 scriptextension = xstrdup(".bat");
434 scriptextension = xstrdup("");
437 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
439 if((space = strchr(proxy, ' ')))
442 if(!strcasecmp(proxy, "none")) {
443 proxytype = PROXY_NONE;
444 } else if(!strcasecmp(proxy, "socks4")) {
445 proxytype = PROXY_SOCKS4;
446 } else if(!strcasecmp(proxy, "socks4a")) {
447 proxytype = PROXY_SOCKS4A;
448 } else if(!strcasecmp(proxy, "socks5")) {
449 proxytype = PROXY_SOCKS5;
450 } else if(!strcasecmp(proxy, "http")) {
451 proxytype = PROXY_HTTP;
452 } else if(!strcasecmp(proxy, "exec")) {
453 proxytype = PROXY_EXEC;
455 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
465 if(!space || !*space) {
466 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
469 proxyhost = xstrdup(space);
477 if(space && (space = strchr(space, ' ')))
478 *space++ = 0, proxyport = space;
479 if(space && (space = strchr(space, ' ')))
480 *space++ = 0, proxyuser = space;
481 if(space && (space = strchr(space, ' ')))
482 *space++ = 0, proxypass = space;
483 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
484 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
487 proxyhost = xstrdup(proxyhost);
488 proxyport = xstrdup(proxyport);
489 if(proxyuser && *proxyuser)
490 proxyuser = xstrdup(proxyuser);
491 if(proxypass && *proxypass)
492 proxypass = xstrdup(proxypass);
499 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
500 myself->options |= OPTION_INDIRECT;
502 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
503 myself->options |= OPTION_TCPONLY;
505 if(myself->options & OPTION_TCPONLY)
506 myself->options |= OPTION_INDIRECT;
508 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
509 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
511 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
512 if(!strcasecmp(rmode, "router"))
513 routing_mode = RMODE_ROUTER;
514 else if(!strcasecmp(rmode, "switch"))
515 routing_mode = RMODE_SWITCH;
516 else if(!strcasecmp(rmode, "hub"))
517 routing_mode = RMODE_HUB;
519 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
525 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
526 if(!strcasecmp(fmode, "off"))
527 forwarding_mode = FMODE_OFF;
528 else if(!strcasecmp(fmode, "internal"))
529 forwarding_mode = FMODE_INTERNAL;
530 else if(!strcasecmp(fmode, "kernel"))
531 forwarding_mode = FMODE_KERNEL;
533 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
540 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
542 myself->options |= OPTION_PMTU_DISCOVERY;
545 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
547 myself->options |= OPTION_CLAMP_MSS;
549 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
550 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
551 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
552 if(!strcasecmp(bmode, "no"))
553 broadcast_mode = BMODE_NONE;
554 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
555 broadcast_mode = BMODE_MST;
556 else if(!strcasecmp(bmode, "direct"))
557 broadcast_mode = BMODE_DIRECT;
559 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
565 #if !defined(SOL_IP) || !defined(IP_TOS)
566 if(priorityinheritance)
567 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
570 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
573 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
574 if(maxtimeout <= 0) {
575 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
581 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
582 if(!strcasecmp(afname, "IPv4"))
583 addressfamily = AF_INET;
584 else if(!strcasecmp(afname, "IPv6"))
585 addressfamily = AF_INET6;
586 else if(!strcasecmp(afname, "any"))
587 addressfamily = AF_UNSPEC;
589 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
595 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
597 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
600 get_config_int(lookup_config(config_tree, "AutoConnect"), &autoconnect);
602 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
608 Configure node_t myself and set up the local sockets (listen only)
610 static bool setup_myself(void) {
611 char *name, *hostname, *cipher, *digest, *type;
613 char *address = NULL;
615 if(!(name = get_name())) {
616 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
621 myself->connection = new_connection();
623 myself->connection->name = xstrdup(name);
624 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
625 read_config_options(config_tree, name);
626 read_config_file(config_tree, fname);
629 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
630 myport = xstrdup("655");
632 xasprintf(&myself->hostname, "MYSELF port %s", myport);
633 myself->connection->hostname = xstrdup(myself->hostname);
635 myself->connection->options = 0;
636 myself->connection->protocol_major = PROT_MAJOR;
637 myself->connection->protocol_minor = PROT_MINOR;
639 myself->options |= PROT_MINOR << 24;
641 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
643 if(experimental && !read_ecdsa_private_key())
646 if(!read_rsa_private_key())
650 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
652 if(!ai || !ai->ai_addr)
655 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
656 sockaddr2str(&sa, NULL, &myport);
659 /* Read in all the subnets specified in the host configuration file */
661 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
664 if(!get_config_subnet(cfg, &subnet))
667 subnet_add(myself, subnet);
670 /* Check some options */
672 if(!setup_myself_reloadable())
675 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
676 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
677 strictsubnets |= tunnelserver;
681 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
682 if(udp_rcvbuf <= 0) {
683 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
688 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
689 if(udp_sndbuf <= 0) {
690 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
696 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
697 if(replaywin_int < 0) {
698 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
701 replaywin = (unsigned)replaywin_int;
702 sptps_replaywin = replaywin;
705 /* Generate packet encryption key */
707 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
708 cipher = xstrdup("blowfish");
710 if(!cipher_open_by_name(&myself->incipher, cipher)) {
711 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
718 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
720 /* Check if we want to use message authentication codes... */
723 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
726 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
730 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
731 digest = xstrdup("sha1");
733 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
734 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
742 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
743 if(myself->incompression < 0 || myself->incompression > 11) {
744 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
748 myself->incompression = 0;
750 myself->connection->outcompression = 0;
754 myself->nexthop = myself;
755 myself->via = myself;
756 myself->status.reachable = true;
757 myself->last_state_change = now.tv_sec;
758 myself->status.sptps = experimental;
772 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
773 if(!strcasecmp(type, "dummy"))
774 devops = dummy_devops;
775 else if(!strcasecmp(type, "raw_socket"))
776 devops = raw_socket_devops;
777 else if(!strcasecmp(type, "multicast"))
778 devops = multicast_devops;
780 else if(!strcasecmp(type, "uml"))
784 else if(!strcasecmp(type, "vde"))
793 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
795 /* Run tinc-up script to further initialize the tap interface */
797 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
798 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
799 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
800 xasprintf(&envp[3], "NAME=%s", myself->name);
803 execute_script("tinc-up", envp);
805 for(int i = 0; i < 4; i++)
808 /* Run subnet-up scripts for our own subnets */
810 subnet_update(myself, NULL, true);
815 int unix_fd = socket(AF_UNIX, SOCK_STREAM, 0);
817 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create UNIX socket: %s", sockstrerror(errno));
821 struct sockaddr_un sa;
822 sa.sun_family = AF_UNIX;
823 strncpy(sa.sun_path, unixsocketname, sizeof sa.sun_path);
825 if(connect(unix_fd, (struct sockaddr *)&sa, sizeof sa) >= 0) {
826 logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket %s is still in use!", unixsocketname);
830 unlink(unixsocketname);
832 if(bind(unix_fd, (struct sockaddr *)&sa, sizeof sa) < 0) {
833 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind UNIX socket to %s: %s", unixsocketname, sockstrerror(errno));
837 if(listen(unix_fd, 3) < 0) {
838 logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on UNIX socket %s: %s", unixsocketname, sockstrerror(errno));
842 io_add(&unix_socket, handle_new_unix_connection, &unix_socket, unix_fd, IO_READ);
845 if(!do_detach && getenv("LISTEN_FDS")) {
849 listen_sockets = atoi(getenv("LISTEN_FDS"));
851 unsetenv("LISTEN_FDS");
854 if(listen_sockets > MAXSOCKETS) {
855 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
859 for(int i = 0; i < listen_sockets; i++) {
861 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
862 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
867 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
870 int udp_fd = setup_vpn_in_socket(&sa);
874 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
875 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
877 if(debug_level >= DEBUG_CONNECTIONS) {
878 hostname = sockaddr2hostname(&sa);
879 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
883 memcpy(&listen_socket[i].sa, &sa, salen);
887 config_t *cfg = lookup_config(config_tree, "BindToAddress");
890 get_config_string(cfg, &address);
892 cfg = lookup_config_next(config_tree, cfg);
897 char *space = strchr(address, ' ');
903 if(!strcmp(address, "*"))
907 struct addrinfo *ai, hint = {0};
908 hint.ai_family = addressfamily;
909 hint.ai_socktype = SOCK_STREAM;
910 hint.ai_protocol = IPPROTO_TCP;
911 hint.ai_flags = AI_PASSIVE;
913 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
917 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
922 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
923 if(listen_sockets >= MAXSOCKETS) {
924 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
928 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
933 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
940 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
941 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
943 if(debug_level >= DEBUG_CONNECTIONS) {
944 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
945 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
949 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
958 logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
960 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
964 last_config_check = now.tv_sec;
972 bool setup_network(void) {
979 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
980 if(pinginterval < 1) {
981 pinginterval = 86400;
986 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
988 if(pingtimeout < 1 || pingtimeout > pinginterval)
989 pingtimeout = pinginterval;
991 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
992 maxoutbufsize = 10 * MTU;
1001 close all open network connections
1003 void close_network_connections(void) {
1004 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1006 connection_t *c = node->data;
1007 /* Keep control connections open until the end, so they know when we really terminated */
1008 if(c->status.control)
1011 terminate_connection(c, false);
1014 list_delete_list(outgoing_list);
1016 if(myself && myself->connection) {
1017 subnet_update(myself, NULL, false);
1018 terminate_connection(myself->connection, false);
1019 free_connection(myself->connection);
1022 for(int i = 0; i < listen_sockets; i++) {
1023 io_del(&listen_socket[i].tcp);
1024 io_del(&listen_socket[i].udp);
1025 close(listen_socket[i].tcp.fd);
1026 close(listen_socket[i].udp.fd);
1030 io_del(&unix_socket);
1031 close(unix_socket.fd);
1035 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
1036 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
1037 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
1038 xasprintf(&envp[3], "NAME=%s", myself->name);
1047 execute_script("tinc-down", envp);
1049 if(myport) free(myport);
1051 for(int i = 0; i < 4; i++)