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 xasprintf(&dname, "%s" SLASH "hosts", confbase);
299 dir = opendir(dname);
301 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
306 while((ent = readdir(dir))) {
307 if(!check_id(ent->d_name))
310 node_t *n = lookup_node(ent->d_name);
311 #ifdef _DIRENT_HAVE_D_TYPE
312 //if(ent->d_type != DT_REG)
317 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
319 splay_tree_t *config_tree;
320 init_configuration(&config_tree);
321 read_config_options(config_tree, ent->d_name);
322 read_config_file(config_tree, fname);
327 n->name = xstrdup(ent->d_name);
331 for(config_t *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) {
580 char *name, *hostname, *cipher, *digest, *type;
582 char *address = NULL;
584 if(!(name = get_name())) {
585 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
590 myself->connection = new_connection();
592 myself->connection->name = xstrdup(name);
593 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
594 read_config_options(config_tree, name);
595 read_config_file(config_tree, fname);
598 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
599 myport = xstrdup("655");
601 xasprintf(&myself->hostname, "MYSELF port %s", myport);
602 myself->connection->hostname = xstrdup(myself->hostname);
604 myself->connection->options = 0;
605 myself->connection->protocol_major = PROT_MAJOR;
606 myself->connection->protocol_minor = PROT_MINOR;
608 myself->options |= PROT_MINOR << 24;
610 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
612 if(experimental && !read_ecdsa_private_key())
615 if(!read_rsa_private_key())
619 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
621 if(!ai || !ai->ai_addr)
624 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
625 sockaddr2str(&sa, NULL, &myport);
628 /* Read in all the subnets specified in the host configuration file */
630 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
633 if(!get_config_subnet(cfg, &subnet))
636 subnet_add(myself, subnet);
639 /* Check some options */
641 if(!setup_myself_reloadable())
644 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
645 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
646 strictsubnets |= tunnelserver;
650 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
651 if(udp_rcvbuf <= 0) {
652 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
657 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
658 if(udp_sndbuf <= 0) {
659 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
665 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
666 if(replaywin_int < 0) {
667 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
670 replaywin = (unsigned)replaywin_int;
673 /* Generate packet encryption key */
675 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
676 cipher = xstrdup("blowfish");
678 if(!cipher_open_by_name(&myself->incipher, cipher)) {
679 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
687 /* Check if we want to use message authentication codes... */
690 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
693 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
697 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
698 digest = xstrdup("sha1");
700 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
701 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
709 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
710 if(myself->incompression < 0 || myself->incompression > 11) {
711 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
715 myself->incompression = 0;
717 myself->connection->outcompression = 0;
721 myself->nexthop = myself;
722 myself->via = myself;
723 myself->status.reachable = true;
724 myself->last_state_change = time(NULL);
725 myself->status.sptps = experimental;
737 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
738 if(!strcasecmp(type, "dummy"))
739 devops = dummy_devops;
740 else if(!strcasecmp(type, "raw_socket"))
741 devops = raw_socket_devops;
742 else if(!strcasecmp(type, "multicast"))
743 devops = multicast_devops;
745 else if(!strcasecmp(type, "uml"))
749 else if(!strcasecmp(type, "vde"))
758 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
760 if (event_add(&device_ev, NULL) < 0) {
761 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
767 /* Run tinc-up script to further initialize the tap interface */
769 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
770 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
771 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
772 xasprintf(&envp[3], "NAME=%s", myself->name);
775 execute_script("tinc-up", envp);
777 for(int i = 0; i < 4; i++)
780 /* Run subnet-up scripts for our own subnets */
782 subnet_update(myself, NULL, true);
786 if(!do_detach && getenv("LISTEN_FDS")) {
790 listen_sockets = atoi(getenv("LISTEN_FDS"));
792 unsetenv("LISTEN_FDS");
795 if(listen_sockets > MAXSOCKETS) {
796 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
800 for(int i = 0; i < listen_sockets; i++) {
802 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
803 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
807 listen_socket[i].tcp = i + 3;
810 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
813 listen_socket[i].udp = setup_vpn_in_socket(&sa);
814 if(listen_socket[i].udp < 0)
817 event_set(&listen_socket[i].ev_tcp, listen_socket[i].tcp, EV_READ|EV_PERSIST, handle_new_meta_connection, NULL);
818 if(event_add(&listen_socket[i].ev_tcp, NULL) < 0) {
819 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
823 event_set(&listen_socket[i].ev_udp, listen_socket[i].udp, EV_READ|EV_PERSIST, handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
824 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
825 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
829 if(debug_level >= DEBUG_CONNECTIONS) {
830 hostname = sockaddr2hostname(&sa);
831 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
835 memcpy(&listen_socket[i].sa, &sa, salen);
839 config_t *cfg = lookup_config(config_tree, "BindToAddress");
842 get_config_string(cfg, &address);
844 cfg = lookup_config_next(config_tree, cfg);
849 char *space = strchr(address, ' ');
855 if(!strcmp(address, "*"))
859 struct addrinfo *ai, hint = {0};
860 hint.ai_family = addressfamily;
861 hint.ai_socktype = SOCK_STREAM;
862 hint.ai_protocol = IPPROTO_TCP;
863 hint.ai_flags = AI_PASSIVE;
865 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
869 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
874 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
875 if(listen_sockets >= MAXSOCKETS) {
876 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
880 listen_socket[listen_sockets].tcp =
881 setup_listen_socket((sockaddr_t *) aip->ai_addr);
883 if(listen_socket[listen_sockets].tcp < 0)
886 listen_socket[listen_sockets].udp =
887 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
889 if(listen_socket[listen_sockets].udp < 0) {
890 close(listen_socket[listen_sockets].tcp);
894 event_set(&listen_socket[listen_sockets].ev_tcp,
895 listen_socket[listen_sockets].tcp,
897 handle_new_meta_connection, NULL);
898 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
899 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
903 event_set(&listen_socket[listen_sockets].ev_udp,
904 listen_socket[listen_sockets].udp,
906 handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
907 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
908 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
912 if(debug_level >= DEBUG_CONNECTIONS) {
913 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
914 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
918 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
927 logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
929 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
933 last_config_check = time(NULL);
941 bool setup_network(void) {
948 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
949 if(pinginterval < 1) {
950 pinginterval = 86400;
955 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
957 if(pingtimeout < 1 || pingtimeout > pinginterval)
958 pingtimeout = pinginterval;
960 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
961 maxoutbufsize = 10 * MTU;
970 close all open network connections
972 void close_network_connections(void) {
973 for(list_node_t *node = connection_list->head, *next; node; node = next) {
975 connection_t *c = node->data;
976 /* Keep control connections open until the end, so they know when we really terminated */
977 if(c->status.control)
980 terminate_connection(c, false);
983 list_delete_list(outgoing_list);
985 if(myself && myself->connection) {
986 subnet_update(myself, NULL, false);
987 terminate_connection(myself->connection, false);
988 free_connection(myself->connection);
991 for(int i = 0; i < listen_sockets; i++) {
992 event_del(&listen_socket[i].ev_tcp);
993 event_del(&listen_socket[i].ev_udp);
994 close(listen_socket[i].tcp);
995 close(listen_socket[i].udp);
999 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
1000 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
1001 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
1002 xasprintf(&envp[3], "NAME=%s", myself->name);
1011 execute_script("tinc-down", envp);
1013 if(myport) free(myport);
1015 for(int i = 0; i < 4; i++)