X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fnet_socket.c;h=15d32db2fefaefe5bce6030441dd6af5c9f602b8;hb=46f3eba7755089ff68fdc137b0754cae2fa523eb;hp=43ea4f8db3b1811acc4618483debf1c6b58ff8f5;hpb=6123ed30992d671b94fc016660086be6a62a3871;p=tinc diff --git a/src/net_socket.c b/src/net_socket.c index 43ea4f8d..15d32db2 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -22,6 +22,7 @@ #include "system.h" +#include "address_cache.h" #include "conf.h" #include "connection.h" #include "control_common.h" @@ -40,7 +41,8 @@ int maxtimeout = 900; int seconds_till_retry = 5; int udp_rcvbuf = 1024 * 1024; int udp_sndbuf = 1024 * 1024; -int max_connection_burst = 100; +int max_connection_burst = 10; +int fwmark; listen_socket_t listen_socket[MAXSOCKETS]; int listen_sockets; @@ -58,32 +60,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) { @@ -175,12 +185,22 @@ 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 @@ -271,7 +291,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa) { logger(DEBUG_ALWAYS, LOG_WARNING, "Can't set UDP SO_SNDBUF to %i: %s", udp_sndbuf, sockstrerror(sockerrno)); } -#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 +303,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 +319,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)) { @@ -489,73 +517,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->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 +567,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 +596,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,7 +606,7 @@ begin: c->last_ping_time = time(NULL); c->status.connecting = true; - c->name = xstrdup(outgoing->name); + c->name = xstrdup(outgoing->node->name); #ifndef DISABLE_LEGACY c->outcipher = myself->connection->outcipher; c->outdigest = myself->connection->outdigest; @@ -641,50 +622,13 @@ 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) { 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) { + logger(DEBUG_CONNECTIONS, LOG_INFO, "Already connected to %s", n->name); if(!n->connection->outgoing) { n->connection->outgoing = outgoing; @@ -694,19 +638,8 @@ void setup_outgoing_connection(outgoing_t *outgoing, bool verbose) { } } - 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(!outgoing->address_cache) { + outgoing->address_cache = open_address_cache(n); } do_outgoing_connection(outgoing); @@ -739,12 +672,6 @@ void handle_new_meta_connection(void *data, int flags) { // 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; @@ -760,7 +687,7 @@ void handle_new_meta_connection(void *data, int flags) { samehost_burst++; if(samehost_burst > max_connection_burst) { - tarpit = fd; + tarpit(fd); return; } } @@ -783,7 +710,7 @@ 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; } @@ -812,7 +739,6 @@ void handle_new_meta_connection(void *data, int flags) { connection_add(c); c->allow_request = ID; - send_id(c); } #ifndef HAVE_MINGW @@ -849,28 +775,14 @@ 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); + if(outgoing->address_cache) { + close_address_cache(outgoing->address_cache); } free(outgoing); @@ -909,7 +821,7 @@ void try_outgoing_connections(void) { bool found = false; for list_each(outgoing_t, outgoing, outgoing_list) { - if(!strcmp(outgoing->name, name)) { + if(!strcmp(outgoing->node->name, name)) { found = true; outgoing->timeout = 0; break; @@ -918,7 +830,15 @@ void try_outgoing_connections(void) { if(!found) { outgoing_t *outgoing = xzalloc(sizeof(*outgoing)); - outgoing->name = name; + node_t *n = lookup_node(name); + + if(!n) { + n = new_node(); + n->name = xstrdup(name); + node_add(n); + } + + outgoing->node = n; list_insert_tail(outgoing_list, outgoing); setup_outgoing_connection(outgoing, true); }