3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2012 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"
45 static struct event device_ev;
52 proxytype_t proxytype;
54 char *scriptinterpreter;
55 char *scriptextension;
57 bool node_read_ecdsa_public_key(node_t *n) {
58 if(ecdsa_active(&n->ecdsa))
61 splay_tree_t *config_tree;
63 char *pubname = NULL, *hcfname = NULL;
67 xasprintf(&hcfname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
69 init_configuration(&config_tree);
70 if(!read_config_file(config_tree, hcfname))
73 /* First, check for simple ECDSAPublicKey statement */
75 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
76 result = ecdsa_set_base64_public_key(&n->ecdsa, p);
81 /* Else, check for ECDSAPublicKeyFile statement and read it */
83 if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &pubname))
84 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
86 fp = fopen(pubname, "r");
89 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s", pubname, strerror(errno));
93 result = ecdsa_read_pem_public_key(&n->ecdsa, fp);
97 exit_configuration(&config_tree);
103 bool read_ecdsa_public_key(connection_t *c) {
109 /* First, check for simple ECDSAPublicKey statement */
111 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
112 result = ecdsa_set_base64_public_key(&c->ecdsa, p);
117 /* Else, check for ECDSAPublicKeyFile statement and read it */
119 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
120 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
122 fp = fopen(fname, "r");
125 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s",
126 fname, strerror(errno));
131 result = ecdsa_read_pem_public_key(&c->ecdsa, fp);
135 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname);
140 bool read_rsa_public_key(connection_t *c) {
146 /* First, check for simple PublicKey statement */
148 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
149 result = rsa_set_hex_public_key(&c->rsa, n, "FFFF");
154 /* Else, check for PublicKeyFile statement and read it */
156 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
157 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
159 fp = fopen(fname, "r");
162 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
167 result = rsa_read_pem_public_key(&c->rsa, fp);
171 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
176 static bool read_ecdsa_private_key(void) {
181 /* Check for PrivateKeyFile statement and read it */
183 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
184 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
186 fp = fopen(fname, "r");
189 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
194 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
197 if(fstat(fileno(fp), &s)) {
198 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
203 if(s.st_mode & ~0100700)
204 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
207 result = ecdsa_read_pem_private_key(&myself->connection->ecdsa, fp);
211 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
216 static bool read_rsa_private_key(void) {
222 /* First, check for simple PrivateKey statement */
224 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
225 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
226 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
230 result = rsa_set_hex_private_key(&myself->connection->rsa, n, "FFFF", d);
236 /* Else, check for PrivateKeyFile statement and read it */
238 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
239 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
241 fp = fopen(fname, "r");
244 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
245 fname, strerror(errno));
250 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
253 if(fstat(fileno(fp), &s)) {
254 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
259 if(s.st_mode & ~0100700)
260 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
263 result = rsa_read_pem_private_key(&myself->connection->rsa, fp);
267 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
272 static struct event keyexpire_event;
274 static void keyexpire_handler(int fd, short events, void *data) {
278 void regenerate_key(void) {
279 if(timeout_initialized(&keyexpire_event)) {
280 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
281 event_del(&keyexpire_event);
284 timeout_set(&keyexpire_event, keyexpire_handler, NULL);
287 event_add(&keyexpire_event, &(struct timeval){keylifetime, 0});
291 Read Subnets from all host config files
293 void load_all_subnets(void) {
298 splay_tree_t *config_tree;
303 xasprintf(&dname, "%s" SLASH "hosts", confbase);
304 dir = opendir(dname);
306 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
311 while((ent = readdir(dir))) {
312 if(!check_id(ent->d_name))
315 n = lookup_node(ent->d_name);
316 #ifdef _DIRENT_HAVE_D_TYPE
317 //if(ent->d_type != DT_REG)
321 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
322 init_configuration(&config_tree);
323 read_config_options(config_tree, ent->d_name);
324 read_config_file(config_tree, fname);
329 n->name = xstrdup(ent->d_name);
333 for(cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
334 if(!get_config_subnet(cfg, &s))
337 if((s2 = lookup_subnet(n, s))) {
344 exit_configuration(&config_tree);
350 char *get_name(void) {
353 get_config_string(lookup_config(config_tree, "Name"), &name);
359 char *envname = getenv(name + 1);
361 if(strcmp(name + 1, "HOST")) {
362 fprintf(stderr, "Invalid Name: environment variable %s does not exist\n", name + 1);
365 envname = alloca(32);
366 if(gethostname(envname, 32)) {
367 fprintf(stderr, "Could not get hostname: %s\n", strerror(errno));
373 name = xstrdup(envname);
374 for(char *c = name; *c; c++)
379 if(!check_id(name)) {
380 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
388 bool setup_myself_reloadable(void) {
397 free(scriptinterpreter);
398 scriptinterpreter = NULL;
399 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
402 free(scriptextension);
403 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
405 scriptextension = xstrdup(".bat");
407 scriptextension = xstrdup("");
410 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
412 if((space = strchr(proxy, ' ')))
415 if(!strcasecmp(proxy, "none")) {
416 proxytype = PROXY_NONE;
417 } else if(!strcasecmp(proxy, "socks4")) {
418 proxytype = PROXY_SOCKS4;
419 } else if(!strcasecmp(proxy, "socks4a")) {
420 proxytype = PROXY_SOCKS4A;
421 } else if(!strcasecmp(proxy, "socks5")) {
422 proxytype = PROXY_SOCKS5;
423 } else if(!strcasecmp(proxy, "http")) {
424 proxytype = PROXY_HTTP;
425 } else if(!strcasecmp(proxy, "exec")) {
426 proxytype = PROXY_EXEC;
428 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
438 if(!space || !*space) {
439 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
442 proxyhost = xstrdup(space);
450 if(space && (space = strchr(space, ' ')))
451 *space++ = 0, proxyport = space;
452 if(space && (space = strchr(space, ' ')))
453 *space++ = 0, proxyuser = space;
454 if(space && (space = strchr(space, ' ')))
455 *space++ = 0, proxypass = space;
456 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
457 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
460 proxyhost = xstrdup(proxyhost);
461 proxyport = xstrdup(proxyport);
462 if(proxyuser && *proxyuser)
463 proxyuser = xstrdup(proxyuser);
464 if(proxypass && *proxypass)
465 proxypass = xstrdup(proxypass);
472 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
473 myself->options |= OPTION_INDIRECT;
475 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
476 myself->options |= OPTION_TCPONLY;
478 if(myself->options & OPTION_TCPONLY)
479 myself->options |= OPTION_INDIRECT;
481 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
482 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
484 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
485 if(!strcasecmp(rmode, "router"))
486 routing_mode = RMODE_ROUTER;
487 else if(!strcasecmp(rmode, "switch"))
488 routing_mode = RMODE_SWITCH;
489 else if(!strcasecmp(rmode, "hub"))
490 routing_mode = RMODE_HUB;
492 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
498 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
499 if(!strcasecmp(fmode, "off"))
500 forwarding_mode = FMODE_OFF;
501 else if(!strcasecmp(fmode, "internal"))
502 forwarding_mode = FMODE_INTERNAL;
503 else if(!strcasecmp(fmode, "kernel"))
504 forwarding_mode = FMODE_KERNEL;
506 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
513 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
515 myself->options |= OPTION_PMTU_DISCOVERY;
518 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
520 myself->options |= OPTION_CLAMP_MSS;
522 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
523 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
524 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
525 if(!strcasecmp(bmode, "no"))
526 broadcast_mode = BMODE_NONE;
527 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
528 broadcast_mode = BMODE_MST;
529 else if(!strcasecmp(bmode, "direct"))
530 broadcast_mode = BMODE_DIRECT;
532 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
538 #if !defined(SOL_IP) || !defined(IP_TOS)
539 if(priorityinheritance)
540 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
543 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
546 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
547 if(maxtimeout <= 0) {
548 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
554 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
555 if(!strcasecmp(afname, "IPv4"))
556 addressfamily = AF_INET;
557 else if(!strcasecmp(afname, "IPv6"))
558 addressfamily = AF_INET6;
559 else if(!strcasecmp(afname, "any"))
560 addressfamily = AF_UNSPEC;
562 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
568 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
570 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
577 Configure node_t myself and set up the local sockets (listen only)
579 static bool setup_myself(void) {
582 char *name, *hostname, *cipher, *digest, *type;
584 char *address = NULL;
586 struct addrinfo *ai, *aip, hint = {0};
590 if(!(name = get_name())) {
591 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
596 myself->connection = new_connection();
598 myself->connection->name = xstrdup(name);
599 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
600 read_config_options(config_tree, name);
601 read_config_file(config_tree, fname);
604 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
605 myport = xstrdup("655");
607 xasprintf(&myself->hostname, "MYSELF port %s", myport);
608 myself->connection->hostname = xstrdup(myself->hostname);
610 myself->connection->options = 0;
611 myself->connection->protocol_major = PROT_MAJOR;
612 myself->connection->protocol_minor = PROT_MINOR;
614 myself->options |= PROT_MINOR << 24;
616 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
618 if(experimental && !read_ecdsa_private_key())
621 if(!read_rsa_private_key())
625 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
627 if(!ai || !ai->ai_addr)
630 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
631 sockaddr2str(&sa, NULL, &myport);
634 /* Read in all the subnets specified in the host configuration file */
636 cfg = lookup_config(config_tree, "Subnet");
639 if(!get_config_subnet(cfg, &subnet))
642 subnet_add(myself, subnet);
644 cfg = lookup_config_next(config_tree, cfg);
647 /* Check some options */
649 if(!setup_myself_reloadable())
652 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
653 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
654 strictsubnets |= tunnelserver;
658 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
659 if(udp_rcvbuf <= 0) {
660 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
665 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
666 if(udp_sndbuf <= 0) {
667 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
672 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
673 if(replaywin_int < 0) {
674 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
677 replaywin = (unsigned)replaywin_int;
680 /* Generate packet encryption key */
682 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
683 cipher = xstrdup("blowfish");
685 if(!cipher_open_by_name(&myself->incipher, cipher)) {
686 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
692 /* Check if we want to use message authentication codes... */
695 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
698 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
702 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
703 digest = xstrdup("sha1");
705 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
706 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
712 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
713 if(myself->incompression < 0 || myself->incompression > 11) {
714 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
718 myself->incompression = 0;
720 myself->connection->outcompression = 0;
724 myself->nexthop = myself;
725 myself->via = myself;
726 myself->status.reachable = true;
727 myself->last_state_change = time(NULL);
728 myself->status.sptps = experimental;
740 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
741 if(!strcasecmp(type, "dummy"))
742 devops = dummy_devops;
743 else if(!strcasecmp(type, "raw_socket"))
744 devops = raw_socket_devops;
745 else if(!strcasecmp(type, "multicast"))
746 devops = multicast_devops;
748 else if(!strcasecmp(type, "uml"))
752 else if(!strcasecmp(type, "vde"))
761 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
763 if (event_add(&device_ev, NULL) < 0) {
764 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
770 /* Run tinc-up script to further initialize the tap interface */
771 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
772 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
773 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
774 xasprintf(&envp[3], "NAME=%s", myself->name);
777 execute_script("tinc-up", envp);
779 for(i = 0; i < 4; i++)
782 /* Run subnet-up scripts for our own subnets */
784 subnet_update(myself, NULL, true);
788 if(!do_detach && getenv("LISTEN_FDS")) {
792 listen_sockets = atoi(getenv("LISTEN_FDS"));
794 unsetenv("LISTEN_FDS");
797 if(listen_sockets > MAXSOCKETS) {
798 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
802 for(i = 0; i < listen_sockets; i++) {
804 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
805 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
809 listen_socket[i].tcp = i + 3;
812 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
815 listen_socket[i].udp = setup_vpn_in_socket(&sa);
816 if(listen_socket[i].udp < 0)
819 event_set(&listen_socket[i].ev_tcp, listen_socket[i].tcp, EV_READ|EV_PERSIST, handle_new_meta_connection, NULL);
820 if(event_add(&listen_socket[i].ev_tcp, NULL) < 0) {
821 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
825 event_set(&listen_socket[i].ev_udp, listen_socket[i].udp, EV_READ|EV_PERSIST, handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
826 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
827 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
831 if(debug_level >= DEBUG_CONNECTIONS) {
832 hostname = sockaddr2hostname(&sa);
833 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
837 memcpy(&listen_socket[i].sa, &sa, salen);
841 cfg = lookup_config(config_tree, "BindToAddress");
844 get_config_string(cfg, &address);
846 cfg = lookup_config_next(config_tree, cfg);
851 char *space = strchr(address, ' ');
857 if(!strcmp(address, "*"))
861 hint.ai_family = addressfamily;
862 hint.ai_socktype = SOCK_STREAM;
863 hint.ai_protocol = IPPROTO_TCP;
864 hint.ai_flags = AI_PASSIVE;
866 err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
870 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
875 for(aip = ai; aip; aip = aip->ai_next) {
876 if(listen_sockets >= MAXSOCKETS) {
877 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
881 listen_socket[listen_sockets].tcp =
882 setup_listen_socket((sockaddr_t *) aip->ai_addr);
884 if(listen_socket[listen_sockets].tcp < 0)
887 listen_socket[listen_sockets].udp =
888 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
890 if(listen_socket[listen_sockets].udp < 0) {
891 close(listen_socket[listen_sockets].tcp);
895 event_set(&listen_socket[listen_sockets].ev_tcp,
896 listen_socket[listen_sockets].tcp,
898 handle_new_meta_connection, NULL);
899 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
900 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
904 event_set(&listen_socket[listen_sockets].ev_udp,
905 listen_socket[listen_sockets].udp,
907 handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
908 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
909 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
913 if(debug_level >= DEBUG_CONNECTIONS) {
914 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
915 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
919 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
928 logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
930 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
934 last_config_check = time(NULL);
942 bool setup_network(void) {
949 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
950 if(pinginterval < 1) {
951 pinginterval = 86400;
956 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
958 if(pingtimeout < 1 || pingtimeout > pinginterval)
959 pingtimeout = pinginterval;
961 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
962 maxoutbufsize = 10 * MTU;
971 close all open network connections
973 void close_network_connections(void) {
974 for(list_node_t *node = connection_list->head, *next; node; node = next) {
976 connection_t *c = node->data;
977 /* Keep control connections open until the end, so they know when we really terminated */
978 if(c->status.control)
981 terminate_connection(c, false);
984 list_delete_list(outgoing_list);
986 if(myself && myself->connection) {
987 subnet_update(myself, NULL, false);
988 terminate_connection(myself->connection, false);
989 free_connection(myself->connection);
992 for(int i = 0; i < listen_sockets; i++) {
993 event_del(&listen_socket[i].ev_tcp);
994 event_del(&listen_socket[i].ev_udp);
995 close(listen_socket[i].tcp);
996 close(listen_socket[i].udp);
1000 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
1001 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
1002 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
1003 xasprintf(&envp[3], "NAME=%s", myself->name);
1012 execute_script("tinc-down", envp);
1014 if(myport) free(myport);
1016 for(int i = 0; i < 4; i++)