X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fnet_socket.c;h=05ee3b17397839460f2031510ac9a885540ac209;hb=f41e5faeaae94973ef1040356532afa277ae130f;hp=2da6253ee3a3dc6c8d62dd669629ce1fbcc90c8d;hpb=7ee885a1f6776be85e5397eda04f75d98ff0b631;p=tinc diff --git a/src/net_socket.c b/src/net_socket.c index 2da6253e..05ee3b17 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 @@ -41,7 +41,9 @@ 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]; @@ -122,6 +124,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 @@ -210,6 +213,7 @@ int setup_listen_socket(const sockaddr_t *sa) { 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); @@ -240,6 +244,35 @@ 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; @@ -283,13 +316,8 @@ 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(IPV6_V6ONLY) @@ -386,7 +414,7 @@ void finish_connecting(connection_t *c) { send_id(c); } -static void do_outgoing_pipe(connection_t *c, char *command) { +static void do_outgoing_pipe(connection_t *c, const char *command) { #ifndef HAVE_MINGW int fd[2]; @@ -434,6 +462,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 @@ -523,7 +553,7 @@ bool do_outgoing_connection(outgoing_t *outgoing) { int result; begin: - sa = get_recent_address(outgoing->address_cache); + sa = get_recent_address(outgoing->node->address_cache); if(!sa) { logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not set up a meta connection to %s", outgoing->node->name); @@ -623,10 +653,15 @@ begin: } void setup_outgoing_connection(outgoing_t *outgoing, bool verbose) { + (void)verbose; timeout_del(&outgoing->ev); node_t *n = outgoing->node; + if(!n->address_cache) { + n->address_cache = open_address_cache(n); + } + if(n->connection) { logger(DEBUG_CONNECTIONS, LOG_INFO, "Already connected to %s", n->name); @@ -638,10 +673,6 @@ void setup_outgoing_connection(outgoing_t *outgoing, bool verbose) { } } - if(!outgoing->address_cache) { - outgoing->address_cache = open_address_cache(n); - } - do_outgoing_connection(outgoing); return; @@ -654,6 +685,7 @@ remove: new connection */ void handle_new_meta_connection(void *data, int flags) { + (void)flags; listen_socket_t *l = data; connection_t *c; sockaddr_t sa; @@ -672,12 +704,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; @@ -693,7 +719,7 @@ void handle_new_meta_connection(void *data, int flags) { samehost_burst++; if(samehost_burst > max_connection_burst) { - tarpit = fd; + tarpit(fd); return; } } @@ -716,7 +742,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; } @@ -745,7 +771,6 @@ void handle_new_meta_connection(void *data, int flags) { connection_add(c); c->allow_request = ID; - send_id(c); } #ifndef HAVE_MINGW @@ -753,6 +778,7 @@ void handle_new_meta_connection(void *data, int flags) { 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; @@ -782,18 +808,11 @@ 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->address_cache) { - close_address_cache(outgoing->address_cache); - } - free(outgoing); }