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;
55 char *scriptinterpreter;
56 char *scriptextension;
58 bool node_read_ecdsa_public_key(node_t *n) {
59 if(ecdsa_active(&n->ecdsa))
62 splay_tree_t *config_tree;
64 char *pubname = NULL, *hcfname = NULL;
68 xasprintf(&hcfname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
70 init_configuration(&config_tree);
71 if(!read_config_file(config_tree, hcfname))
74 /* First, check for simple ECDSAPublicKey statement */
76 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
77 result = ecdsa_set_base64_public_key(&n->ecdsa, 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 result = ecdsa_read_pem_public_key(&n->ecdsa, fp);
98 exit_configuration(&config_tree);
104 bool read_ecdsa_public_key(connection_t *c) {
110 /* First, check for simple ECDSAPublicKey statement */
112 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
113 result = ecdsa_set_base64_public_key(&c->ecdsa, p);
118 /* Else, check for ECDSAPublicKeyFile statement and read it */
120 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
121 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
123 fp = fopen(fname, "r");
126 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s",
127 fname, strerror(errno));
132 result = ecdsa_read_pem_public_key(&c->ecdsa, fp);
136 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname);
141 bool read_rsa_public_key(connection_t *c) {
147 /* First, check for simple PublicKey statement */
149 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
150 result = rsa_set_hex_public_key(&c->rsa, n, "FFFF");
155 /* Else, check for PublicKeyFile statement and read it */
157 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
158 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
160 fp = fopen(fname, "r");
163 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
168 result = rsa_read_pem_public_key(&c->rsa, fp);
172 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
177 static bool read_ecdsa_private_key(void) {
182 /* Check for PrivateKeyFile statement and read it */
184 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
185 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
187 fp = fopen(fname, "r");
190 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
195 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
198 if(fstat(fileno(fp), &s)) {
199 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
204 if(s.st_mode & ~0100700)
205 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
208 result = ecdsa_read_pem_private_key(&myself->connection->ecdsa, fp);
212 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
217 static bool read_rsa_private_key(void) {
223 /* First, check for simple PrivateKey statement */
225 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
226 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
227 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
231 result = rsa_set_hex_private_key(&myself->connection->rsa, n, "FFFF", d);
237 /* Else, check for PrivateKeyFile statement and read it */
239 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
240 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
242 fp = fopen(fname, "r");
245 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
246 fname, strerror(errno));
251 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
254 if(fstat(fileno(fp), &s)) {
255 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
260 if(s.st_mode & ~0100700)
261 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
264 result = rsa_read_pem_private_key(&myself->connection->rsa, fp);
268 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
273 static struct event keyexpire_event;
275 static void keyexpire_handler(int fd, short events, void *data) {
279 void regenerate_key(void) {
280 if(timeout_initialized(&keyexpire_event)) {
281 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
282 event_del(&keyexpire_event);
285 timeout_set(&keyexpire_event, keyexpire_handler, NULL);
288 event_add(&keyexpire_event, &(struct timeval){keylifetime, rand() % 100000});
292 Read Subnets from all host config files
294 void load_all_subnets(void) {
299 xasprintf(&dname, "%s" SLASH "hosts", confbase);
300 dir = opendir(dname);
302 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
307 while((ent = readdir(dir))) {
308 if(!check_id(ent->d_name))
311 node_t *n = lookup_node(ent->d_name);
312 #ifdef _DIRENT_HAVE_D_TYPE
313 //if(ent->d_type != DT_REG)
318 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
320 splay_tree_t *config_tree;
321 init_configuration(&config_tree);
322 read_config_options(config_tree, ent->d_name);
323 read_config_file(config_tree, fname);
328 n->name = xstrdup(ent->d_name);
332 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
335 if(!get_config_subnet(cfg, &s))
338 if((s2 = lookup_subnet(n, s))) {
345 exit_configuration(&config_tree);
351 void load_all_nodes(void) {
356 xasprintf(&dname, "%s" SLASH "hosts", confbase);
357 dir = opendir(dname);
359 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
364 while((ent = readdir(dir))) {
365 if(!check_id(ent->d_name))
368 node_t *n = lookup_node(ent->d_name);
373 n->name = xstrdup(ent->d_name);
381 char *get_name(void) {
384 get_config_string(lookup_config(config_tree, "Name"), &name);
390 char *envname = getenv(name + 1);
392 if(strcmp(name + 1, "HOST")) {
393 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
397 if(gethostname(envname, 32)) {
398 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno));
404 name = xstrdup(envname);
405 for(char *c = name; *c; c++)
410 if(!check_id(name)) {
411 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
419 bool setup_myself_reloadable(void) {
428 free(scriptinterpreter);
429 scriptinterpreter = NULL;
430 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
433 free(scriptextension);
434 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
436 scriptextension = xstrdup(".bat");
438 scriptextension = xstrdup("");
441 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
443 if((space = strchr(proxy, ' ')))
446 if(!strcasecmp(proxy, "none")) {
447 proxytype = PROXY_NONE;
448 } else if(!strcasecmp(proxy, "socks4")) {
449 proxytype = PROXY_SOCKS4;
450 } else if(!strcasecmp(proxy, "socks4a")) {
451 proxytype = PROXY_SOCKS4A;
452 } else if(!strcasecmp(proxy, "socks5")) {
453 proxytype = PROXY_SOCKS5;
454 } else if(!strcasecmp(proxy, "http")) {
455 proxytype = PROXY_HTTP;
456 } else if(!strcasecmp(proxy, "exec")) {
457 proxytype = PROXY_EXEC;
459 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
469 if(!space || !*space) {
470 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
473 proxyhost = xstrdup(space);
481 if(space && (space = strchr(space, ' ')))
482 *space++ = 0, proxyport = space;
483 if(space && (space = strchr(space, ' ')))
484 *space++ = 0, proxyuser = space;
485 if(space && (space = strchr(space, ' ')))
486 *space++ = 0, proxypass = space;
487 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
488 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
491 proxyhost = xstrdup(proxyhost);
492 proxyport = xstrdup(proxyport);
493 if(proxyuser && *proxyuser)
494 proxyuser = xstrdup(proxyuser);
495 if(proxypass && *proxypass)
496 proxypass = xstrdup(proxypass);
503 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
504 myself->options |= OPTION_INDIRECT;
506 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
507 myself->options |= OPTION_TCPONLY;
509 if(myself->options & OPTION_TCPONLY)
510 myself->options |= OPTION_INDIRECT;
512 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
513 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
515 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
516 if(!strcasecmp(rmode, "router"))
517 routing_mode = RMODE_ROUTER;
518 else if(!strcasecmp(rmode, "switch"))
519 routing_mode = RMODE_SWITCH;
520 else if(!strcasecmp(rmode, "hub"))
521 routing_mode = RMODE_HUB;
523 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
529 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
530 if(!strcasecmp(fmode, "off"))
531 forwarding_mode = FMODE_OFF;
532 else if(!strcasecmp(fmode, "internal"))
533 forwarding_mode = FMODE_INTERNAL;
534 else if(!strcasecmp(fmode, "kernel"))
535 forwarding_mode = FMODE_KERNEL;
537 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
544 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
546 myself->options |= OPTION_PMTU_DISCOVERY;
549 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
551 myself->options |= OPTION_CLAMP_MSS;
553 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
554 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
555 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
556 if(!strcasecmp(bmode, "no"))
557 broadcast_mode = BMODE_NONE;
558 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
559 broadcast_mode = BMODE_MST;
560 else if(!strcasecmp(bmode, "direct"))
561 broadcast_mode = BMODE_DIRECT;
563 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
569 #if !defined(SOL_IP) || !defined(IP_TOS)
570 if(priorityinheritance)
571 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
574 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
577 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
578 if(maxtimeout <= 0) {
579 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
585 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
586 if(!strcasecmp(afname, "IPv4"))
587 addressfamily = AF_INET;
588 else if(!strcasecmp(afname, "IPv6"))
589 addressfamily = AF_INET6;
590 else if(!strcasecmp(afname, "any"))
591 addressfamily = AF_UNSPEC;
593 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
599 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
601 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
604 get_config_int(lookup_config(config_tree, "AutoConnect"), &autoconnect);
610 Configure node_t myself and set up the local sockets (listen only)
612 static bool setup_myself(void) {
613 char *name, *hostname, *cipher, *digest, *type;
615 char *address = NULL;
617 if(!(name = get_name())) {
618 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
623 myself->connection = new_connection();
625 myself->connection->name = xstrdup(name);
626 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
627 read_config_options(config_tree, name);
628 read_config_file(config_tree, fname);
631 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
632 myport = xstrdup("655");
634 xasprintf(&myself->hostname, "MYSELF port %s", myport);
635 myself->connection->hostname = xstrdup(myself->hostname);
637 myself->connection->options = 0;
638 myself->connection->protocol_major = PROT_MAJOR;
639 myself->connection->protocol_minor = PROT_MINOR;
641 myself->options |= PROT_MINOR << 24;
643 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
645 if(experimental && !read_ecdsa_private_key())
648 if(!read_rsa_private_key())
652 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
654 if(!ai || !ai->ai_addr)
657 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
658 sockaddr2str(&sa, NULL, &myport);
661 /* Read in all the subnets specified in the host configuration file */
663 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
666 if(!get_config_subnet(cfg, &subnet))
669 subnet_add(myself, subnet);
672 /* Check some options */
674 if(!setup_myself_reloadable())
677 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
678 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
679 strictsubnets |= tunnelserver;
683 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
684 if(udp_rcvbuf <= 0) {
685 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
690 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
691 if(udp_sndbuf <= 0) {
692 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
698 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
699 if(replaywin_int < 0) {
700 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
703 replaywin = (unsigned)replaywin_int;
704 sptps_replaywin = replaywin;
707 /* Generate packet encryption key */
709 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
710 cipher = xstrdup("blowfish");
712 if(!cipher_open_by_name(&myself->incipher, cipher)) {
713 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
721 /* Check if we want to use message authentication codes... */
724 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
727 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
731 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
732 digest = xstrdup("sha1");
734 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
735 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
743 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
744 if(myself->incompression < 0 || myself->incompression > 11) {
745 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
749 myself->incompression = 0;
751 myself->connection->outcompression = 0;
755 myself->nexthop = myself;
756 myself->via = myself;
757 myself->status.reachable = true;
758 myself->last_state_change = time(NULL);
759 myself->status.sptps = experimental;
773 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
774 if(!strcasecmp(type, "dummy"))
775 devops = dummy_devops;
776 else if(!strcasecmp(type, "raw_socket"))
777 devops = raw_socket_devops;
778 else if(!strcasecmp(type, "multicast"))
779 devops = multicast_devops;
781 else if(!strcasecmp(type, "uml"))
785 else if(!strcasecmp(type, "vde"))
794 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
796 if (event_add(&device_ev, NULL) < 0) {
797 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
803 /* Run tinc-up script to further initialize the tap interface */
805 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
806 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
807 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
808 xasprintf(&envp[3], "NAME=%s", myself->name);
811 execute_script("tinc-up", envp);
813 for(int i = 0; i < 4; i++)
816 /* Run subnet-up scripts for our own subnets */
818 subnet_update(myself, NULL, true);
822 if(!do_detach && getenv("LISTEN_FDS")) {
826 listen_sockets = atoi(getenv("LISTEN_FDS"));
828 unsetenv("LISTEN_FDS");
831 if(listen_sockets > MAXSOCKETS) {
832 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
836 for(int i = 0; i < listen_sockets; i++) {
838 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
839 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
843 listen_socket[i].tcp = i + 3;
846 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
849 listen_socket[i].udp = setup_vpn_in_socket(&sa);
850 if(listen_socket[i].udp < 0)
853 event_set(&listen_socket[i].ev_tcp, listen_socket[i].tcp, EV_READ|EV_PERSIST, handle_new_meta_connection, NULL);
854 if(event_add(&listen_socket[i].ev_tcp, NULL) < 0) {
855 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
859 event_set(&listen_socket[i].ev_udp, listen_socket[i].udp, EV_READ|EV_PERSIST, handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
860 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
861 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
865 if(debug_level >= DEBUG_CONNECTIONS) {
866 hostname = sockaddr2hostname(&sa);
867 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
871 memcpy(&listen_socket[i].sa, &sa, salen);
875 config_t *cfg = lookup_config(config_tree, "BindToAddress");
878 get_config_string(cfg, &address);
880 cfg = lookup_config_next(config_tree, cfg);
885 char *space = strchr(address, ' ');
891 if(!strcmp(address, "*"))
895 struct addrinfo *ai, hint = {0};
896 hint.ai_family = addressfamily;
897 hint.ai_socktype = SOCK_STREAM;
898 hint.ai_protocol = IPPROTO_TCP;
899 hint.ai_flags = AI_PASSIVE;
901 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
905 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
910 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
911 if(listen_sockets >= MAXSOCKETS) {
912 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
916 listen_socket[listen_sockets].tcp =
917 setup_listen_socket((sockaddr_t *) aip->ai_addr);
919 if(listen_socket[listen_sockets].tcp < 0)
922 listen_socket[listen_sockets].udp =
923 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
925 if(listen_socket[listen_sockets].udp < 0) {
926 close(listen_socket[listen_sockets].tcp);
930 event_set(&listen_socket[listen_sockets].ev_tcp,
931 listen_socket[listen_sockets].tcp,
933 handle_new_meta_connection, NULL);
934 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
935 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
939 event_set(&listen_socket[listen_sockets].ev_udp,
940 listen_socket[listen_sockets].udp,
942 handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
943 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
944 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
948 if(debug_level >= DEBUG_CONNECTIONS) {
949 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
950 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
954 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
963 logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
965 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
969 last_config_check = time(NULL);
977 bool setup_network(void) {
984 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
985 if(pinginterval < 1) {
986 pinginterval = 86400;
991 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
993 if(pingtimeout < 1 || pingtimeout > pinginterval)
994 pingtimeout = pinginterval;
996 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
997 maxoutbufsize = 10 * MTU;
1006 close all open network connections
1008 void close_network_connections(void) {
1009 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1011 connection_t *c = node->data;
1012 /* Keep control connections open until the end, so they know when we really terminated */
1013 if(c->status.control)
1016 terminate_connection(c, false);
1019 list_delete_list(outgoing_list);
1021 if(myself && myself->connection) {
1022 subnet_update(myself, NULL, false);
1023 terminate_connection(myself->connection, false);
1024 free_connection(myself->connection);
1027 for(int i = 0; i < listen_sockets; i++) {
1028 event_del(&listen_socket[i].ev_tcp);
1029 event_del(&listen_socket[i].ev_udp);
1030 close(listen_socket[i].tcp);
1031 close(listen_socket[i].udp);
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++)