X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fnet_socket.c;h=c8aa78824d51b8536c975291d9b99b4e9daf311b;hb=84b24109000ce66125038793df313205e5836b83;hp=43ea4f8db3b1811acc4618483debf1c6b58ff8f5;hpb=6123ed30992d671b94fc016660086be6a62a3871;p=tinc diff --git a/src/net_socket.c b/src/net_socket.c index 43ea4f8d..c8aa7882 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -1,7 +1,7 @@ /* net_socket.c -- Handle various kinds of sockets. Copyright (C) 1998-2005 Ivo Timmermans, - 2000-2017 Guus Sliepen + 2000-2018 Guus Sliepen 2006 Scott Lamb 2009 Florian Forster @@ -22,12 +22,12 @@ #include "system.h" +#include "address_cache.h" #include "conf.h" #include "connection.h" -#include "control_common.h" +#include "crypto.h" #include "list.h" #include "logger.h" -#include "meta.h" #include "names.h" #include "net.h" #include "netutl.h" @@ -40,14 +40,28 @@ int maxtimeout = 900; int seconds_till_retry = 5; int udp_rcvbuf = 1024 * 1024; int udp_sndbuf = 1024 * 1024; -int max_connection_burst = 100; +bool udp_rcvbuf_warnings; +bool udp_sndbuf_warnings; +int max_connection_burst = 10; +int fwmark; listen_socket_t listen_socket[MAXSOCKETS]; int listen_sockets; -#ifndef HAVE_MINGW +#ifndef HAVE_WINDOWS io_t unix_socket; #endif -list_t *outgoing_list = NULL; + +static void free_outgoing(outgoing_t *outgoing) { + timeout_del(&outgoing->ev); + free(outgoing); +} + +list_t outgoing_list = { + .head = NULL, + .tail = NULL, + .count = 0, + .delete = (list_action_t)free_outgoing, +}; /* Setup sockets */ @@ -58,32 +72,40 @@ static void configure_tcp(connection_t *c) { int flags = fcntl(c->socket, F_GETFL); if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) { - logger(DEBUG_ALWAYS, LOG_ERR, "fcntl for %s: %s", c->hostname, strerror(errno)); + logger(DEBUG_ALWAYS, LOG_ERR, "fcntl for %s fd %d: %s", c->hostname, c->socket, strerror(errno)); } #elif defined(WIN32) unsigned long arg = 1; if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) { - logger(DEBUG_ALWAYS, LOG_ERR, "ioctlsocket for %s: %s", c->hostname, sockstrerror(sockerrno)); + logger(DEBUG_ALWAYS, LOG_ERR, "ioctlsocket for %s fd %d: %s", c->hostname, c->socket, sockstrerror(sockerrno)); } #endif -#if defined(IPPROTO_TCP) && defined(TCP_NODELAY) +#if defined(TCP_NODELAY) option = 1; setsockopt(c->socket, IPPROTO_TCP, TCP_NODELAY, (void *)&option, sizeof(option)); #endif -#if defined(IPPROTO_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY) +#if defined(IP_TOS) && defined(IPTOS_LOWDELAY) option = IPTOS_LOWDELAY; setsockopt(c->socket, IPPROTO_IP, IP_TOS, (void *)&option, sizeof(option)); #endif -#if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS) && defined(IPTOS_LOWDELAY) +#if defined(IPV6_TCLASS) && defined(IPTOS_LOWDELAY) option = IPTOS_LOWDELAY; setsockopt(c->socket, IPPROTO_IPV6, IPV6_TCLASS, (void *)&option, sizeof(option)); #endif + +#if defined(SO_MARK) + + if(fwmark) { + setsockopt(c->socket, SOL_SOCKET, SO_MARK, (void *)&fwmark, sizeof(fwmark)); + } + +#endif } static bool bind_to_interface(int sd) { @@ -94,7 +116,7 @@ static bool bind_to_interface(int sd) { int status; #endif /* defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) */ - if(!get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) { + if(!get_config_string(lookup_config(&config_tree, "BindToInterface"), &iface)) { return true; } @@ -112,6 +134,7 @@ static bool bind_to_interface(int sd) { } #else /* if !defined(SOL_SOCKET) || !defined(SO_BINDTODEVICE) */ + (void)sd; logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "BindToInterface"); #endif @@ -153,9 +176,20 @@ static bool bind_to_address(connection_t *c) { return true; } +static bool try_bind(int nfd, const sockaddr_t *sa, const char *type) { + if(!bind(nfd, &sa->sa, SALEN(sa->sa))) { + return true; + } + + closesocket(nfd); + char *addrstr = sockaddr2hostname(sa); + logger(DEBUG_ALWAYS, LOG_ERR, "Can't bind to %s/%s: %s", addrstr, type, sockstrerror(sockerrno)); + free(addrstr); + return false; +} + int setup_listen_socket(const sockaddr_t *sa) { int nfd; - char *addrstr; int option; char *iface; @@ -175,21 +209,32 @@ int setup_listen_socket(const sockaddr_t *sa) { option = 1; setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option)); -#if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY) +#if defined(IPV6_V6ONLY) if(sa->sa.sa_family == AF_INET6) { setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option)); } +#else +#warning IPV6_V6ONLY not defined +#endif + +#if defined(SO_MARK) + + if(fwmark) { + setsockopt(nfd, SOL_SOCKET, SO_MARK, (void *)&fwmark, sizeof(fwmark)); + } + #endif if(get_config_string - (lookup_config(config_tree, "BindToInterface"), &iface)) { + (lookup_config(&config_tree, "BindToInterface"), &iface)) { #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ); + ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0; if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr))) { closesocket(nfd); @@ -203,11 +248,7 @@ int setup_listen_socket(const sockaddr_t *sa) { #endif } - if(bind(nfd, &sa->sa, SALEN(sa->sa))) { - closesocket(nfd); - addrstr = sockaddr2hostname(sa); - logger(DEBUG_ALWAYS, LOG_ERR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno)); - free(addrstr); + if(!try_bind(nfd, sa, "tcp")) { return -1; } @@ -220,9 +261,36 @@ int setup_listen_socket(const sockaddr_t *sa) { return nfd; } +static void set_udp_buffer(int nfd, int type, const char *name, int size, bool warnings) { + if(!size) { + return; + } + + if(setsockopt(nfd, SOL_SOCKET, type, (void *)&size, sizeof(size))) { + logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP %s to %i: %s", name, size, sockstrerror(sockerrno)); + return; + } + + if(!warnings) { + return; + } + + // The system may cap the requested buffer size. + // Read back the value and check if it is now as requested. + int actual = -1; + socklen_t optlen = sizeof(actual); + + if(getsockopt(nfd, SOL_SOCKET, type, (void *)&actual, &optlen)) { + logger(DEBUG_ALWAYS, LOG_WARNING, "Can't read back UDP %s: %s", name, sockstrerror(sockerrno)); + } else if(optlen != sizeof(actual)) { + logger(DEBUG_ALWAYS, LOG_WARNING, "Can't read back UDP %s: unexpected returned optlen %d", name, (int)optlen); + } else if(actual < size) { + logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP %s to %i, the system set it to %i instead", name, size, actual); + } +} + int setup_vpn_in_socket(const sockaddr_t *sa) { int nfd; - char *addrstr; int option; nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP); @@ -263,15 +331,10 @@ int setup_vpn_in_socket(const sockaddr_t *sa) { setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option)); setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof(option)); - if(udp_rcvbuf && setsockopt(nfd, SOL_SOCKET, SO_RCVBUF, (void *)&udp_rcvbuf, sizeof(udp_rcvbuf))) { - logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP SO_RCVBUF to %i: %s", udp_rcvbuf, sockstrerror(sockerrno)); - } - - if(udp_sndbuf && setsockopt(nfd, SOL_SOCKET, SO_SNDBUF, (void *)&udp_sndbuf, sizeof(udp_sndbuf))) { - logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP SO_SNDBUF to %i: %s", udp_sndbuf, sockstrerror(sockerrno)); - } + set_udp_buffer(nfd, SO_RCVBUF, "SO_RCVBUF", udp_rcvbuf, udp_rcvbuf_warnings); + set_udp_buffer(nfd, SO_SNDBUF, "SO_SNDBUF", udp_sndbuf, udp_sndbuf_warnings); -#if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY) +#if defined(IPV6_V6ONLY) if(sa->sa.sa_family == AF_INET6) { setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option)); @@ -283,14 +346,14 @@ int setup_vpn_in_socket(const sockaddr_t *sa) { #define IP_DONTFRAGMENT IP_DONTFRAG #endif -#if defined(IPPROTO_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) +#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) if(myself->options & OPTION_PMTU_DISCOVERY) { option = IP_PMTUDISC_DO; setsockopt(nfd, IPPROTO_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option)); } -#elif defined(IPPROTO_IP) && defined(IP_DONTFRAGMENT) +#elif defined(IP_DONTFRAGMENT) if(myself->options & OPTION_PMTU_DISCOVERY) { option = 1; @@ -299,20 +362,28 @@ int setup_vpn_in_socket(const sockaddr_t *sa) { #endif -#if defined(IPPROTO_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) +#if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) if(myself->options & OPTION_PMTU_DISCOVERY) { option = IPV6_PMTUDISC_DO; setsockopt(nfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option)); } -#elif defined(IPPROTO_IPV6) && defined(IPV6_DONTFRAG) +#elif defined(IPV6_DONTFRAG) if(myself->options & OPTION_PMTU_DISCOVERY) { option = 1; setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option)); } +#endif + +#if defined(SO_MARK) + + if(fwmark) { + setsockopt(nfd, SOL_SOCKET, SO_MARK, (void *)&fwmark, sizeof(fwmark)); + } + #endif if(!bind_to_interface(nfd)) { @@ -320,11 +391,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa) { return -1; } - if(bind(nfd, &sa->sa, SALEN(sa->sa))) { - closesocket(nfd); - addrstr = sockaddr2hostname(sa); - logger(DEBUG_ALWAYS, LOG_ERR, "Can't bind to %s/udp: %s", addrstr, sockstrerror(sockerrno)); - free(addrstr); + if(!try_bind(nfd, sa, "udp")) { return -1; } @@ -343,7 +410,7 @@ void retry_outgoing(outgoing_t *outgoing) { } timeout_add(&outgoing->ev, retry_outgoing_handler, outgoing, &(struct timeval) { - outgoing->timeout, rand() % 100000 + outgoing->timeout, jitter() }); logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Trying to re-establish outgoing connection in %d seconds", outgoing->timeout); @@ -358,8 +425,8 @@ void finish_connecting(connection_t *c) { send_id(c); } -static void do_outgoing_pipe(connection_t *c, char *command) { -#ifndef HAVE_MINGW +static void do_outgoing_pipe(connection_t *c, const char *command) { +#ifndef HAVE_WINDOWS int fd[2]; if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) { @@ -389,9 +456,12 @@ static void do_outgoing_pipe(connection_t *c, char *command) { sockaddr2str(&c->address, &host, &port); setenv("REMOTEADDRESS", host, true); setenv("REMOTEPORT", port, true); - setenv("NODE", c->name, true); setenv("NAME", myself->name, true); + if(c->name) { + setenv("NODE", c->name, true); + } + if(netname) { setenv("NETNAME", netname, true); } @@ -406,6 +476,8 @@ static void do_outgoing_pipe(connection_t *c, char *command) { exit(result); #else + (void)c; + (void)command; logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type exec not supported on this platform!"); return; #endif @@ -489,73 +561,26 @@ static void handle_meta_io(void *data, int flags) { } } -static void free_known_addresses(struct addrinfo *ai) { - for(struct addrinfo *aip = ai, *next; aip; aip = next) { - next = aip->ai_next; - free(aip); - } -} - bool do_outgoing_connection(outgoing_t *outgoing) { - char *address, *port, *space; + const sockaddr_t *sa; struct addrinfo *proxyai = NULL; int result; begin: + sa = get_recent_address(outgoing->node->address_cache); - if(!outgoing->ai && !outgoing->kai) { - if(!outgoing->cfg) { - logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not set up a meta connection to %s", outgoing->name); - retry_outgoing(outgoing); - return false; - } - - get_config_string(outgoing->cfg, &address); - - space = strchr(address, ' '); - - if(space) { - port = xstrdup(space + 1); - *space = 0; - } else { - if(!get_config_string(lookup_config(outgoing->config_tree, "Port"), &port)) { - port = xstrdup("655"); - } - } - - outgoing->ai = str2addrinfo(address, port, SOCK_STREAM); - free(address); - free(port); - - outgoing->aip = outgoing->ai; - outgoing->cfg = lookup_config_next(outgoing->config_tree, outgoing->cfg); - } - - if(!outgoing->aip) { - if(outgoing->ai) { - freeaddrinfo(outgoing->ai); - } - - outgoing->ai = NULL; - - if(outgoing->kai) { - free_known_addresses(outgoing->kai); - } - - outgoing->kai = NULL; - - goto begin; + if(!sa) { + logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not set up a meta connection to %s", outgoing->node->name); + retry_outgoing(outgoing); + return false; } connection_t *c = new_connection(); c->outgoing = outgoing; - - memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen); - outgoing->aip = outgoing->aip->ai_next; - + memcpy(&c->address, sa, SALEN(sa->sa)); c->hostname = sockaddr2hostname(&c->address); - logger(DEBUG_CONNECTIONS, LOG_INFO, "Trying to connect to %s (%s)", outgoing->name, c->hostname); + logger(DEBUG_CONNECTIONS, LOG_INFO, "Trying to connect to %s (%s)", outgoing->node->name, c->hostname); if(!proxytype) { c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP); @@ -586,7 +611,7 @@ begin: #endif if(proxytype != PROXY_EXEC) { -#if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY) +#if defined(IPV6_V6ONLY) int option = 1; if(c->address.sa.sa_family == AF_INET6) { @@ -615,7 +640,7 @@ begin: } if(result == -1 && !sockinprogress(sockerrno)) { - logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not connect to %s (%s): %s", outgoing->name, c->hostname, sockstrerror(sockerrno)); + logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not connect to %s (%s): %s", outgoing->node->name, c->hostname, sockstrerror(sockerrno)); free_connection(c); goto begin; @@ -625,13 +650,8 @@ begin: c->last_ping_time = time(NULL); c->status.connecting = true; - c->name = xstrdup(outgoing->name); -#ifndef DISABLE_LEGACY - c->outcipher = myself->connection->outcipher; - c->outdigest = myself->connection->outdigest; -#endif + c->name = xstrdup(outgoing->node->name); c->outmaclength = myself->connection->outmaclength; - c->outcompression = myself->connection->outcompression; c->last_ping_time = now.tv_sec; connection_add(c); @@ -641,114 +661,31 @@ begin: return true; } -// Find edges pointing to this node, and use them to build a list of unique, known addresses. -static struct addrinfo *get_known_addresses(node_t *n) { - struct addrinfo *ai = NULL; - struct addrinfo *oai = NULL; - - for splay_each(edge_t, e, n->edge_tree) { - if(!e->reverse) { - continue; - } - - bool found = false; - - for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) { - if(!sockaddrcmp(&e->reverse->address, (sockaddr_t *)aip->ai_addr)) { - found = true; - break; - } - } - - if(found) { - continue; - } - - oai = ai; - ai = xzalloc(sizeof(*ai)); - ai->ai_family = e->reverse->address.sa.sa_family; - ai->ai_socktype = SOCK_STREAM; - ai->ai_protocol = IPPROTO_TCP; - ai->ai_addrlen = SALEN(e->reverse->address.sa); - ai->ai_addr = xmalloc(ai->ai_addrlen); - memcpy(ai->ai_addr, &e->reverse->address, ai->ai_addrlen); - ai->ai_next = oai; - } - - return ai; -} - void setup_outgoing_connection(outgoing_t *outgoing, bool verbose) { + (void)verbose; timeout_del(&outgoing->ev); - node_t *n = lookup_node(outgoing->name); + node_t *n = outgoing->node; - if(n && n->connection) { - logger(DEBUG_CONNECTIONS, LOG_INFO, "Already connected to %s", outgoing->name); - - if(!n->connection->outgoing) { - n->connection->outgoing = outgoing; - return; - } else { - goto remove; - } + if(!n->address_cache) { + n->address_cache = open_address_cache(n); } - init_configuration(&outgoing->config_tree); - read_host_config(outgoing->config_tree, outgoing->name, verbose); - outgoing->cfg = lookup_config(outgoing->config_tree, "Address"); - - if(!outgoing->cfg) { - if(n) { - outgoing->aip = outgoing->kai = get_known_addresses(n); - } - - if(!outgoing->kai) { - logger(verbose ? DEBUG_ALWAYS : DEBUG_CONNECTIONS, LOG_DEBUG, "No address known for %s", outgoing->name); - goto remove; - } + if(n->connection) { + logger(DEBUG_CONNECTIONS, LOG_INFO, "Already connected to %s", n->name); + } else { + do_outgoing_connection(outgoing); } - - do_outgoing_connection(outgoing); - return; - -remove: - list_delete(outgoing_list, outgoing); } -/* - accept a new tcp connect and create a - new connection -*/ -void handle_new_meta_connection(void *data, int flags) { - listen_socket_t *l = data; - connection_t *c; - sockaddr_t sa; - int fd; - socklen_t len = sizeof(sa); - - fd = accept(l->tcp.fd, &sa.sa, &len); - - if(fd < 0) { - logger(DEBUG_ALWAYS, LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno)); - return; - } - - sockaddrunmap(&sa); - +static bool check_tarpit(const sockaddr_t *sa, int fd) { // Check if we get many connections from the same host static sockaddr_t prev_sa; - static int tarpit = -1; - - if(tarpit >= 0) { - closesocket(tarpit); - tarpit = -1; - } - if(!sockaddrcmp_noport(&sa, &prev_sa)) { - static int samehost_burst; - static int samehost_burst_time; + if(!sockaddrcmp_noport(sa, &prev_sa)) { + static time_t samehost_burst; + static time_t samehost_burst_time; if(now.tv_sec - samehost_burst_time > samehost_burst) { samehost_burst = 0; @@ -760,8 +697,8 @@ void handle_new_meta_connection(void *data, int flags) { samehost_burst++; if(samehost_burst > max_connection_burst) { - tarpit = fd; - return; + tarpit(fd); + return true; } } @@ -769,8 +706,8 @@ void handle_new_meta_connection(void *data, int flags) { // Check if we get many connections from different hosts - static int connection_burst; - static int connection_burst_time; + static time_t connection_burst; + static time_t connection_burst_time; if(now.tv_sec - connection_burst_time > connection_burst) { connection_burst = 0; @@ -783,7 +720,35 @@ void handle_new_meta_connection(void *data, int flags) { if(connection_burst >= max_connection_burst) { connection_burst = max_connection_burst; - tarpit = fd; + tarpit(fd); + return true; + } + + return false; +} + +/* + accept a new tcp connect and create a + new connection +*/ +void handle_new_meta_connection(void *data, int flags) { + (void)flags; + listen_socket_t *l = data; + connection_t *c; + sockaddr_t sa; + int fd; + socklen_t len = sizeof(sa); + + fd = accept(l->tcp.fd, &sa.sa, &len); + + if(fd < 0) { + logger(DEBUG_ALWAYS, LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno)); + return; + } + + sockaddrunmap(&sa); + + if(!is_local_connection(&sa) && check_tarpit(&sa, fd)) { return; } @@ -791,12 +756,7 @@ void handle_new_meta_connection(void *data, int flags) { c = new_connection(); c->name = xstrdup(""); -#ifndef DISABLE_LEGACY - c->outcipher = myself->connection->outcipher; - c->outdigest = myself->connection->outdigest; -#endif c->outmaclength = myself->connection->outmaclength; - c->outcompression = myself->connection->outcompression; c->address = sa; c->hostname = sockaddr2hostname(&sa); @@ -812,14 +772,14 @@ void handle_new_meta_connection(void *data, int flags) { connection_add(c); c->allow_request = ID; - send_id(c); } -#ifndef HAVE_MINGW +#ifndef HAVE_WINDOWS /* accept a new UNIX socket connection */ void handle_new_unix_connection(void *data, int flags) { + (void)flags; io_t *io = data; connection_t *c; sockaddr_t sa; @@ -849,47 +809,19 @@ void handle_new_unix_connection(void *data, int flags) { connection_add(c); c->allow_request = ID; - - send_id(c); } #endif -static void free_outgoing(outgoing_t *outgoing) { - timeout_del(&outgoing->ev); - - if(outgoing->ai) { - freeaddrinfo(outgoing->ai); - } - - if(outgoing->kai) { - free_known_addresses(outgoing->kai); - } - - if(outgoing->config_tree) { - exit_configuration(&outgoing->config_tree); - } - - if(outgoing->name) { - free(outgoing->name); - } - - free(outgoing); -} - void try_outgoing_connections(void) { /* If there is no outgoing list yet, create one. Otherwise, mark all outgoings as deleted. */ - if(!outgoing_list) { - outgoing_list = list_alloc((list_action_t)free_outgoing); - } else { - for list_each(outgoing_t, outgoing, outgoing_list) { - outgoing->timeout = -1; - } + for list_each(outgoing_t, outgoing, &outgoing_list) { + outgoing->timeout = -1; } /* Make sure there is one outgoing_t in the list for each ConnectTo. */ - for(config_t *cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) { + for(config_t *cfg = lookup_config(&config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(&config_tree, cfg)) { char *name; get_config_string(cfg, &name); @@ -908,8 +840,8 @@ void try_outgoing_connections(void) { bool found = false; - for list_each(outgoing_t, outgoing, outgoing_list) { - if(!strcmp(outgoing->name, name)) { + for list_each(outgoing_t, outgoing, &outgoing_list) { + if(!strcmp(outgoing->node->name, name)) { found = true; outgoing->timeout = 0; break; @@ -918,15 +850,24 @@ void try_outgoing_connections(void) { if(!found) { outgoing_t *outgoing = xzalloc(sizeof(*outgoing)); - outgoing->name = name; - list_insert_tail(outgoing_list, outgoing); + node_t *n = lookup_node(name); + + if(!n) { + n = new_node(name); + node_add(n); + } + + outgoing->node = n; + list_insert_tail(&outgoing_list, outgoing); setup_outgoing_connection(outgoing, true); } + + free(name); } /* Terminate any connections whose outgoing_t is to be deleted. */ - for list_each(connection_t, c, connection_list) { + for list_each(connection_t, c, &connection_list) { if(c->outgoing && c->outgoing->timeout == -1) { c->outgoing = NULL; logger(DEBUG_CONNECTIONS, LOG_INFO, "No more outgoing connection to %s", c->name); @@ -936,8 +877,8 @@ void try_outgoing_connections(void) { /* Delete outgoing_ts for which there is no ConnectTo. */ - for list_each(outgoing_t, outgoing, outgoing_list) + for list_each(outgoing_t, outgoing, &outgoing_list) if(outgoing->timeout == -1) { - list_delete_node(outgoing_list, node); + list_delete_node(&outgoing_list, node); } }