3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2014 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"
51 proxytype_t proxytype;
53 bool disablebuggypeers;
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;
67 init_configuration(&config_tree);
68 if(!read_host_config(config_tree, n->name))
71 /* First, check for simple ECDSAPublicKey statement */
73 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
74 n->ecdsa = ecdsa_set_base64_public_key(p);
79 /* Else, check for ECDSAPublicKeyFile statement and read it */
81 if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &pubname))
82 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
84 fp = fopen(pubname, "r");
89 n->ecdsa = ecdsa_read_pem_public_key(fp);
93 exit_configuration(&config_tree);
98 bool read_ecdsa_public_key(connection_t *c) {
99 if(ecdsa_active(c->ecdsa))
106 if(!c->config_tree) {
107 init_configuration(&c->config_tree);
108 if(!read_host_config(c->config_tree, c->name))
112 /* First, check for simple ECDSAPublicKey statement */
114 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
115 c->ecdsa = ecdsa_set_base64_public_key(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 c->ecdsa = ecdsa_read_pem_public_key(fp);
138 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname);
143 bool read_rsa_public_key(connection_t *c) {
144 if(ecdsa_active(c->ecdsa))
151 /* First, check for simple PublicKey statement */
153 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
154 c->rsa = rsa_set_hex_public_key(n, "FFFF");
159 /* Else, check for PublicKeyFile statement and read it */
161 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
162 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
164 fp = fopen(fname, "r");
167 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
172 c->rsa = rsa_read_pem_public_key(fp);
176 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
181 static bool read_ecdsa_private_key(void) {
185 /* Check for PrivateKeyFile statement and read it */
187 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
188 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
190 fp = fopen(fname, "r");
193 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
195 logger(DEBUG_ALWAYS, LOG_INFO, "Create an ECDSA keypair with `tinc -n %s generate-ecdsa-keys'.", netname ?: ".");
200 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
203 if(fstat(fileno(fp), &s)) {
204 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
209 if(s.st_mode & ~0100700)
210 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
213 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
216 if(!myself->connection->ecdsa)
217 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
219 return myself->connection->ecdsa;
222 static bool read_invitation_key(void) {
227 ecdsa_free(invitation_key);
228 invitation_key = NULL;
231 xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", confbase);
233 fp = fopen(fname, "r");
236 invitation_key = ecdsa_read_pem_private_key(fp);
239 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
243 return invitation_key;
246 static bool read_rsa_private_key(void) {
251 /* First, check for simple PrivateKey statement */
253 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
254 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
255 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
259 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
262 return myself->connection->rsa;
265 /* Else, check for PrivateKeyFile statement and read it */
267 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
268 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
270 fp = fopen(fname, "r");
273 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
274 fname, strerror(errno));
279 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
282 if(fstat(fileno(fp), &s)) {
283 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
288 if(s.st_mode & ~0100700)
289 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
292 myself->connection->rsa = rsa_read_pem_private_key(fp);
295 if(!myself->connection->rsa)
296 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
298 return myself->connection->rsa;
301 static timeout_t keyexpire_timeout;
303 static void keyexpire_handler(void *data) {
305 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
308 void regenerate_key(void) {
309 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
314 Read Subnets from all host config files
316 void load_all_subnets(void) {
321 xasprintf(&dname, "%s" SLASH "hosts", confbase);
322 dir = opendir(dname);
324 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
329 while((ent = readdir(dir))) {
330 if(!check_id(ent->d_name))
333 node_t *n = lookup_node(ent->d_name);
334 #ifdef _DIRENT_HAVE_D_TYPE
335 //if(ent->d_type != DT_REG)
339 splay_tree_t *config_tree;
340 init_configuration(&config_tree);
341 read_config_options(config_tree, ent->d_name);
342 read_host_config(config_tree, ent->d_name);
346 n->name = xstrdup(ent->d_name);
350 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
353 if(!get_config_subnet(cfg, &s))
356 if((s2 = lookup_subnet(n, s))) {
363 exit_configuration(&config_tree);
369 void load_all_nodes(void) {
374 xasprintf(&dname, "%s" SLASH "hosts", confbase);
375 dir = opendir(dname);
377 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
382 while((ent = readdir(dir))) {
383 if(!check_id(ent->d_name))
386 node_t *n = lookup_node(ent->d_name);
391 n->name = xstrdup(ent->d_name);
399 char *get_name(void) {
402 get_config_string(lookup_config(config_tree, "Name"), &name);
408 char *envname = getenv(name + 1);
409 char hostname[32] = "";
411 if(strcmp(name + 1, "HOST")) {
412 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
415 if(gethostname(hostname, sizeof hostname) || !*hostname) {
416 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno));
423 name = xstrdup(envname);
424 for(char *c = name; *c; c++)
429 if(!check_id(name)) {
430 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
438 bool setup_myself_reloadable(void) {
444 char *address = NULL;
448 free(scriptinterpreter);
449 scriptinterpreter = NULL;
450 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
453 free(scriptextension);
454 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
455 scriptextension = xstrdup("");
457 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
459 if((space = strchr(proxy, ' ')))
462 if(!strcasecmp(proxy, "none")) {
463 proxytype = PROXY_NONE;
464 } else if(!strcasecmp(proxy, "socks4")) {
465 proxytype = PROXY_SOCKS4;
466 } else if(!strcasecmp(proxy, "socks4a")) {
467 proxytype = PROXY_SOCKS4A;
468 } else if(!strcasecmp(proxy, "socks5")) {
469 proxytype = PROXY_SOCKS5;
470 } else if(!strcasecmp(proxy, "http")) {
471 proxytype = PROXY_HTTP;
472 } else if(!strcasecmp(proxy, "exec")) {
473 proxytype = PROXY_EXEC;
475 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
485 if(!space || !*space) {
486 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
489 proxyhost = xstrdup(space);
497 if(space && (space = strchr(space, ' ')))
498 *space++ = 0, proxyport = space;
499 if(space && (space = strchr(space, ' ')))
500 *space++ = 0, proxyuser = space;
501 if(space && (space = strchr(space, ' ')))
502 *space++ = 0, proxypass = space;
503 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
504 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
507 proxyhost = xstrdup(proxyhost);
508 proxyport = xstrdup(proxyport);
509 if(proxyuser && *proxyuser)
510 proxyuser = xstrdup(proxyuser);
511 if(proxypass && *proxypass)
512 proxypass = xstrdup(proxypass);
519 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
520 myself->options |= OPTION_INDIRECT;
522 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
523 myself->options |= OPTION_TCPONLY;
525 if(myself->options & OPTION_TCPONLY)
526 myself->options |= OPTION_INDIRECT;
528 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
529 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
531 memset(&localdiscovery_address, 0, sizeof localdiscovery_address);
532 if(get_config_string(lookup_config(config_tree, "LocalDiscoveryAddress"), &address)) {
533 struct addrinfo *ai = str2addrinfo(address, myport, SOCK_DGRAM);
537 memcpy(&localdiscovery_address, ai->ai_addr, ai->ai_addrlen);
541 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
542 if(!strcasecmp(rmode, "router"))
543 routing_mode = RMODE_ROUTER;
544 else if(!strcasecmp(rmode, "switch"))
545 routing_mode = RMODE_SWITCH;
546 else if(!strcasecmp(rmode, "hub"))
547 routing_mode = RMODE_HUB;
549 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
555 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
556 if(!strcasecmp(fmode, "off"))
557 forwarding_mode = FMODE_OFF;
558 else if(!strcasecmp(fmode, "internal"))
559 forwarding_mode = FMODE_INTERNAL;
560 else if(!strcasecmp(fmode, "kernel"))
561 forwarding_mode = FMODE_KERNEL;
563 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
570 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
572 myself->options |= OPTION_PMTU_DISCOVERY;
575 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
577 myself->options |= OPTION_CLAMP_MSS;
579 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
580 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
581 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
582 if(!strcasecmp(bmode, "no"))
583 broadcast_mode = BMODE_NONE;
584 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
585 broadcast_mode = BMODE_MST;
586 else if(!strcasecmp(bmode, "direct"))
587 broadcast_mode = BMODE_DIRECT;
589 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
595 #if !defined(SOL_IP) || !defined(IP_TOS)
596 if(priorityinheritance)
597 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
600 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
603 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
604 if(maxtimeout <= 0) {
605 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
611 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
612 if(!strcasecmp(afname, "IPv4"))
613 addressfamily = AF_INET;
614 else if(!strcasecmp(afname, "IPv6"))
615 addressfamily = AF_INET6;
616 else if(!strcasecmp(afname, "any"))
617 addressfamily = AF_UNSPEC;
619 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
625 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
627 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
630 get_config_int(lookup_config(config_tree, "AutoConnect"), &autoconnect);
634 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
636 read_invitation_key();
642 Add listening sockets.
644 static bool add_listen_address(char *address, bool bindto) {
648 char *space = strchr(address, ' ');
654 if(!strcmp(address, "*"))
658 struct addrinfo *ai, hint = {0};
659 hint.ai_family = addressfamily;
660 hint.ai_socktype = SOCK_STREAM;
661 hint.ai_protocol = IPPROTO_TCP;
662 hint.ai_flags = AI_PASSIVE;
664 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
668 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
672 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
673 // Ignore duplicate addresses
676 for(int i = 0; i < listen_sockets; i++)
677 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
685 if(listen_sockets >= MAXSOCKETS) {
686 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
690 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
695 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
702 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
703 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
705 if(debug_level >= DEBUG_CONNECTIONS) {
706 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
707 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
711 listen_socket[listen_sockets].bindto = bindto;
712 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
721 Configure node_t myself and set up the local sockets (listen only)
723 bool setup_myself(void) {
724 char *name, *hostname, *cipher, *digest, *type;
725 char *address = NULL;
726 bool port_specified = false;
728 if(!(name = get_name())) {
729 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
734 myself->connection = new_connection();
736 myself->connection->name = xstrdup(name);
737 read_host_config(config_tree, name);
739 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
740 myport = xstrdup("655");
742 port_specified = true;
744 myself->connection->options = 0;
745 myself->connection->protocol_major = PROT_MAJOR;
746 myself->connection->protocol_minor = PROT_MINOR;
748 myself->options |= PROT_MINOR << 24;
750 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
751 experimental = read_ecdsa_private_key();
753 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
755 if(experimental && !read_ecdsa_private_key())
759 if(!read_rsa_private_key())
762 /* Ensure myport is numeric */
765 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
767 if(!ai || !ai->ai_addr)
770 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
771 sockaddr2str(&sa, NULL, &myport);
774 /* Read in all the subnets specified in the host configuration file */
776 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
779 if(!get_config_subnet(cfg, &subnet))
782 subnet_add(myself, subnet);
785 /* Check some options */
787 if(!setup_myself_reloadable())
790 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
791 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
792 strictsubnets |= tunnelserver;
794 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
795 if(max_connection_burst <= 0) {
796 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
801 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
802 if(udp_rcvbuf <= 0) {
803 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
808 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
809 if(udp_sndbuf <= 0) {
810 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
816 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
817 if(replaywin_int < 0) {
818 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
821 replaywin = (unsigned)replaywin_int;
822 sptps_replaywin = replaywin;
825 /* Generate packet encryption key */
827 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
828 cipher = xstrdup("blowfish");
830 if(!strcasecmp(cipher, "none")) {
831 myself->incipher = NULL;
832 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
833 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
839 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
841 /* Check if we want to use message authentication codes... */
844 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
847 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
851 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
852 digest = xstrdup("sha1");
854 if(!strcasecmp(digest, "none")) {
855 myself->indigest = NULL;
856 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
857 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
865 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
866 if(myself->incompression < 0 || myself->incompression > 11) {
867 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
871 myself->incompression = 0;
873 myself->connection->outcompression = 0;
877 myself->nexthop = myself;
878 myself->via = myself;
879 myself->status.reachable = true;
880 myself->last_state_change = now.tv_sec;
881 myself->status.sptps = experimental;
893 if(!do_detach && getenv("LISTEN_FDS")) {
897 listen_sockets = atoi(getenv("LISTEN_FDS"));
899 unsetenv("LISTEN_FDS");
902 if(listen_sockets > MAXSOCKETS) {
903 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
907 for(int i = 0; i < listen_sockets; i++) {
909 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
910 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
915 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
918 int udp_fd = setup_vpn_in_socket(&sa);
922 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
923 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
925 if(debug_level >= DEBUG_CONNECTIONS) {
926 hostname = sockaddr2hostname(&sa);
927 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
931 memcpy(&listen_socket[i].sa, &sa, salen);
937 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
939 get_config_string(cfg, &address);
940 if(!add_listen_address(address, true))
944 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
946 get_config_string(cfg, &address);
947 if(!add_listen_address(address, false))
952 if(!add_listen_address(address, NULL))
956 if(!listen_sockets) {
957 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
961 /* If no Port option was specified, set myport to the port used by the first listening socket. */
963 if(!port_specified) {
965 socklen_t salen = sizeof sa;
966 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
968 sockaddr2str(&sa, NULL, &myport);
970 myport = xstrdup("655");
974 xasprintf(&myself->hostname, "MYSELF port %s", myport);
975 myself->connection->hostname = xstrdup(myself->hostname);
979 last_config_check = now.tv_sec;
987 bool setup_network(void) {
994 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
995 if(pinginterval < 1) {
996 pinginterval = 86400;
1001 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
1003 if(pingtimeout < 1 || pingtimeout > pinginterval)
1004 pingtimeout = pinginterval;
1006 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
1007 maxoutbufsize = 10 * MTU;
1015 /* Run tinc-up script to further initialize the tap interface */
1017 char *envp[5] = {NULL};
1018 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
1019 xasprintf(&envp[3], "NAME=%s", myself->name);
1021 execute_script("tinc-up", envp);
1023 for(int i = 0; i < 4; i++)
1026 /* Run subnet-up scripts for our own subnets */
1028 subnet_update(myself, NULL, true);
1034 close all open network connections
1036 void close_network_connections(void) {
1037 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1039 connection_t *c = node->data;
1040 /* Keep control connections open until the end, so they know when we really terminated */
1041 if(c->status.control)
1044 terminate_connection(c, false);
1048 list_delete_list(outgoing_list);
1050 if(myself && myself->connection) {
1051 subnet_update(myself, NULL, false);
1052 terminate_connection(myself->connection, false);
1053 free_connection(myself->connection);
1056 for(int i = 0; i < listen_sockets; i++) {
1057 io_del(&listen_socket[i].tcp);
1058 io_del(&listen_socket[i].udp);
1059 close(listen_socket[i].tcp.fd);
1060 close(listen_socket[i].udp.fd);
1063 char *envp[5] = {NULL};
1064 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
1065 xasprintf(&envp[3], "NAME=%s", myself->name);
1073 execute_script("tinc-down", envp);
1075 if(myport) free(myport);
1077 for(int i = 0; i < 4; i++)