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;
69 init_configuration(&config_tree);
70 if(!read_host_config(config_tree, n->name))
73 /* First, check for simple ECDSAPublicKey statement */
75 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
76 n->ecdsa = ecdsa_set_base64_public_key(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 n->ecdsa = ecdsa_read_pem_public_key(fp);
97 exit_configuration(&config_tree);
102 bool read_ecdsa_public_key(connection_t *c) {
103 if(ecdsa_active(c->ecdsa))
110 if(!c->config_tree) {
111 init_configuration(&c->config_tree);
112 if(!read_host_config(c->config_tree, c->name))
116 /* First, check for simple ECDSAPublicKey statement */
118 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
119 c->ecdsa = ecdsa_set_base64_public_key(p);
124 /* Else, check for ECDSAPublicKeyFile statement and read it */
126 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
127 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
129 fp = fopen(fname, "r");
132 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s",
133 fname, strerror(errno));
138 c->ecdsa = ecdsa_read_pem_public_key(fp);
142 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname);
147 bool read_rsa_public_key(connection_t *c) {
148 if(ecdsa_active(c->ecdsa))
155 /* First, check for simple PublicKey statement */
157 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
158 c->rsa = rsa_set_hex_public_key(n, "FFFF");
163 /* Else, check for PublicKeyFile statement and read it */
165 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
166 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
168 fp = fopen(fname, "r");
171 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
176 c->rsa = rsa_read_pem_public_key(fp);
180 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
185 static bool read_ecdsa_private_key(void) {
189 /* Check for PrivateKeyFile statement and read it */
191 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
192 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
194 fp = fopen(fname, "r");
197 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
199 logger(DEBUG_ALWAYS, LOG_INFO, "Create an ECDSA keypair with `tinc -n %s generate-ecdsa-keys'.", netname ?: ".");
204 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
207 if(fstat(fileno(fp), &s)) {
208 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
213 if(s.st_mode & ~0100700)
214 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
217 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
220 if(!myself->connection->ecdsa)
221 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
223 return myself->connection->ecdsa;
226 static bool read_invitation_key(void) {
231 ecdsa_free(invitation_key);
232 invitation_key = NULL;
235 xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", confbase);
237 fp = fopen(fname, "r");
240 invitation_key = ecdsa_read_pem_private_key(fp);
243 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
247 return invitation_key;
250 static bool read_rsa_private_key(void) {
255 /* First, check for simple PrivateKey statement */
257 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
258 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
259 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
263 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
266 return myself->connection->rsa;
269 /* Else, check for PrivateKeyFile statement and read it */
271 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
272 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
274 fp = fopen(fname, "r");
277 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
278 fname, strerror(errno));
283 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
286 if(fstat(fileno(fp), &s)) {
287 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
292 if(s.st_mode & ~0100700)
293 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
296 myself->connection->rsa = rsa_read_pem_private_key(fp);
299 if(!myself->connection->rsa)
300 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
302 return myself->connection->rsa;
305 static timeout_t keyexpire_timeout;
307 static void keyexpire_handler(void *data) {
309 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
312 void regenerate_key(void) {
313 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
318 Read Subnets from all host config files
320 void load_all_subnets(void) {
325 xasprintf(&dname, "%s" SLASH "hosts", confbase);
326 dir = opendir(dname);
328 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
333 while((ent = readdir(dir))) {
334 if(!check_id(ent->d_name))
337 node_t *n = lookup_node(ent->d_name);
338 #ifdef _DIRENT_HAVE_D_TYPE
339 //if(ent->d_type != DT_REG)
343 splay_tree_t *config_tree;
344 init_configuration(&config_tree);
345 read_config_options(config_tree, ent->d_name);
346 read_host_config(config_tree, ent->d_name);
350 n->name = xstrdup(ent->d_name);
354 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
357 if(!get_config_subnet(cfg, &s))
360 if((s2 = lookup_subnet(n, s))) {
367 exit_configuration(&config_tree);
373 void load_all_nodes(void) {
378 xasprintf(&dname, "%s" SLASH "hosts", confbase);
379 dir = opendir(dname);
381 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
386 while((ent = readdir(dir))) {
387 if(!check_id(ent->d_name))
390 node_t *n = lookup_node(ent->d_name);
395 n->name = xstrdup(ent->d_name);
403 char *get_name(void) {
406 get_config_string(lookup_config(config_tree, "Name"), &name);
412 char *envname = getenv(name + 1);
414 if(strcmp(name + 1, "HOST")) {
415 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
419 if(gethostname(envname, 32)) {
420 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno));
426 name = xstrdup(envname);
427 for(char *c = name; *c; c++)
432 if(!check_id(name)) {
433 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
441 bool setup_myself_reloadable(void) {
447 char *address = NULL;
451 free(scriptinterpreter);
452 scriptinterpreter = NULL;
453 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
456 free(scriptextension);
457 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
458 scriptextension = xstrdup("");
460 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
462 if((space = strchr(proxy, ' ')))
465 if(!strcasecmp(proxy, "none")) {
466 proxytype = PROXY_NONE;
467 } else if(!strcasecmp(proxy, "socks4")) {
468 proxytype = PROXY_SOCKS4;
469 } else if(!strcasecmp(proxy, "socks4a")) {
470 proxytype = PROXY_SOCKS4A;
471 } else if(!strcasecmp(proxy, "socks5")) {
472 proxytype = PROXY_SOCKS5;
473 } else if(!strcasecmp(proxy, "http")) {
474 proxytype = PROXY_HTTP;
475 } else if(!strcasecmp(proxy, "exec")) {
476 proxytype = PROXY_EXEC;
478 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
488 if(!space || !*space) {
489 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
492 proxyhost = xstrdup(space);
500 if(space && (space = strchr(space, ' ')))
501 *space++ = 0, proxyport = space;
502 if(space && (space = strchr(space, ' ')))
503 *space++ = 0, proxyuser = space;
504 if(space && (space = strchr(space, ' ')))
505 *space++ = 0, proxypass = space;
506 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
507 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
510 proxyhost = xstrdup(proxyhost);
511 proxyport = xstrdup(proxyport);
512 if(proxyuser && *proxyuser)
513 proxyuser = xstrdup(proxyuser);
514 if(proxypass && *proxypass)
515 proxypass = xstrdup(proxypass);
522 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
523 myself->options |= OPTION_INDIRECT;
525 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
526 myself->options |= OPTION_TCPONLY;
528 if(myself->options & OPTION_TCPONLY)
529 myself->options |= OPTION_INDIRECT;
531 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
532 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
534 memset(&localdiscovery_address, 0, sizeof localdiscovery_address);
535 if(get_config_string(lookup_config(config_tree, "LocalDiscoveryAddress"), &address)) {
536 struct addrinfo *ai = str2addrinfo(address, myport, SOCK_DGRAM);
540 memcpy(&localdiscovery_address, ai->ai_addr, ai->ai_addrlen);
544 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
545 if(!strcasecmp(rmode, "router"))
546 routing_mode = RMODE_ROUTER;
547 else if(!strcasecmp(rmode, "switch"))
548 routing_mode = RMODE_SWITCH;
549 else if(!strcasecmp(rmode, "hub"))
550 routing_mode = RMODE_HUB;
552 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
558 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
559 if(!strcasecmp(fmode, "off"))
560 forwarding_mode = FMODE_OFF;
561 else if(!strcasecmp(fmode, "internal"))
562 forwarding_mode = FMODE_INTERNAL;
563 else if(!strcasecmp(fmode, "kernel"))
564 forwarding_mode = FMODE_KERNEL;
566 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
573 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
575 myself->options |= OPTION_PMTU_DISCOVERY;
578 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
580 myself->options |= OPTION_CLAMP_MSS;
582 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
583 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
584 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
585 if(!strcasecmp(bmode, "no"))
586 broadcast_mode = BMODE_NONE;
587 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
588 broadcast_mode = BMODE_MST;
589 else if(!strcasecmp(bmode, "direct"))
590 broadcast_mode = BMODE_DIRECT;
592 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
598 #if !defined(SOL_IP) || !defined(IP_TOS)
599 if(priorityinheritance)
600 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
603 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
606 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
607 if(maxtimeout <= 0) {
608 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
614 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
615 if(!strcasecmp(afname, "IPv4"))
616 addressfamily = AF_INET;
617 else if(!strcasecmp(afname, "IPv6"))
618 addressfamily = AF_INET6;
619 else if(!strcasecmp(afname, "any"))
620 addressfamily = AF_UNSPEC;
622 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
628 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
630 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
633 get_config_int(lookup_config(config_tree, "AutoConnect"), &autoconnect);
635 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
637 read_invitation_key();
643 Configure node_t myself and set up the local sockets (listen only)
645 static bool setup_myself(void) {
646 char *name, *hostname, *cipher, *digest, *type;
647 char *address = NULL;
649 if(!(name = get_name())) {
650 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
655 myself->connection = new_connection();
657 myself->connection->name = xstrdup(name);
658 read_host_config(config_tree, name);
660 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
661 myport = xstrdup("655");
663 xasprintf(&myself->hostname, "MYSELF port %s", myport);
664 myself->connection->hostname = xstrdup(myself->hostname);
666 myself->connection->options = 0;
667 myself->connection->protocol_major = PROT_MAJOR;
668 myself->connection->protocol_minor = PROT_MINOR;
670 myself->options |= PROT_MINOR << 24;
672 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
674 if(experimental && !read_ecdsa_private_key())
677 if(!read_rsa_private_key())
681 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
683 if(!ai || !ai->ai_addr)
686 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
687 sockaddr2str(&sa, NULL, &myport);
690 /* Read in all the subnets specified in the host configuration file */
692 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
695 if(!get_config_subnet(cfg, &subnet))
698 subnet_add(myself, subnet);
701 /* Check some options */
703 if(!setup_myself_reloadable())
706 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
707 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
708 strictsubnets |= tunnelserver;
710 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
711 if(max_connection_burst <= 0) {
712 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
717 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
718 if(udp_rcvbuf <= 0) {
719 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
724 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
725 if(udp_sndbuf <= 0) {
726 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
732 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
733 if(replaywin_int < 0) {
734 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
737 replaywin = (unsigned)replaywin_int;
738 sptps_replaywin = replaywin;
741 /* Generate packet encryption key */
743 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
744 cipher = xstrdup("blowfish");
746 if(!(myself->incipher = cipher_open_by_name(cipher))) {
747 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
753 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
755 /* Check if we want to use message authentication codes... */
758 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
761 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
765 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
766 digest = xstrdup("sha1");
768 if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
769 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
777 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
778 if(myself->incompression < 0 || myself->incompression > 11) {
779 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
783 myself->incompression = 0;
785 myself->connection->outcompression = 0;
789 myself->nexthop = myself;
790 myself->via = myself;
791 myself->status.reachable = true;
792 myself->last_state_change = now.tv_sec;
793 myself->status.sptps = experimental;
807 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
808 if(!strcasecmp(type, "dummy"))
809 devops = dummy_devops;
810 else if(!strcasecmp(type, "raw_socket"))
811 devops = raw_socket_devops;
812 else if(!strcasecmp(type, "multicast"))
813 devops = multicast_devops;
815 else if(!strcasecmp(type, "uml"))
819 else if(!strcasecmp(type, "vde"))
828 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
832 if(!do_detach && getenv("LISTEN_FDS")) {
836 listen_sockets = atoi(getenv("LISTEN_FDS"));
838 unsetenv("LISTEN_FDS");
841 if(listen_sockets > MAXSOCKETS) {
842 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
846 for(int i = 0; i < listen_sockets; i++) {
848 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
849 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
854 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
857 int udp_fd = setup_vpn_in_socket(&sa);
861 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
862 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
864 if(debug_level >= DEBUG_CONNECTIONS) {
865 hostname = sockaddr2hostname(&sa);
866 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
870 memcpy(&listen_socket[i].sa, &sa, salen);
874 config_t *cfg = lookup_config(config_tree, "BindToAddress");
877 get_config_string(cfg, &address);
879 cfg = lookup_config_next(config_tree, cfg);
884 char *space = strchr(address, ' ');
890 if(!strcmp(address, "*"))
894 struct addrinfo *ai, hint = {0};
895 hint.ai_family = addressfamily;
896 hint.ai_socktype = SOCK_STREAM;
897 hint.ai_protocol = IPPROTO_TCP;
898 hint.ai_flags = AI_PASSIVE;
900 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
904 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
908 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
909 if(listen_sockets >= MAXSOCKETS) {
910 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
914 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
919 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
926 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
927 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
929 if(debug_level >= DEBUG_CONNECTIONS) {
930 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
931 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
935 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
943 if(!listen_sockets) {
944 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
948 last_config_check = now.tv_sec;
956 bool setup_network(void) {
963 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
964 if(pinginterval < 1) {
965 pinginterval = 86400;
970 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
972 if(pingtimeout < 1 || pingtimeout > pinginterval)
973 pingtimeout = pinginterval;
975 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
976 maxoutbufsize = 10 * MTU;
984 /* Run tinc-up script to further initialize the tap interface */
986 char *envp[5] = {NULL};
987 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
988 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
989 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
990 xasprintf(&envp[3], "NAME=%s", myself->name);
992 execute_script("tinc-up", envp);
994 for(int i = 0; i < 4; i++)
997 /* Run subnet-up scripts for our own subnets */
999 subnet_update(myself, NULL, true);
1005 close all open network connections
1007 void close_network_connections(void) {
1008 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1010 connection_t *c = node->data;
1011 /* Keep control connections open until the end, so they know when we really terminated */
1012 if(c->status.control)
1015 terminate_connection(c, false);
1018 list_delete_list(outgoing_list);
1020 if(myself && myself->connection) {
1021 subnet_update(myself, NULL, false);
1022 terminate_connection(myself->connection, false);
1023 free_connection(myself->connection);
1026 for(int i = 0; i < listen_sockets; i++) {
1027 io_del(&listen_socket[i].tcp);
1028 io_del(&listen_socket[i].udp);
1029 close(listen_socket[i].tcp.fd);
1030 close(listen_socket[i].udp.fd);
1033 char *envp[5] = {NULL};
1034 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
1035 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
1036 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
1037 xasprintf(&envp[3], "NAME=%s", myself->name);
1045 execute_script("tinc-down", envp);
1047 if(myport) free(myport);
1049 for(int i = 0; i < 4; i++)