diff options
author | Nick Mathewson <nickm@torproject.org> | 2011-05-16 11:12:22 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2011-05-16 11:12:22 -0400 |
commit | e423447f53f3a1b3b72cea3e52f345143e847d48 (patch) | |
tree | 04a9ad8c15aa1047e075f1276e24aba45dd7d9b5 | |
parent | de8e0ef0bd03607f444c967d8c3d74aa8abee4b8 (diff) | |
parent | 919bf6ff3cfb1387f3f9ecf9ce97d8e95e330a05 (diff) | |
download | tor-e423447f53f3a1b3b72cea3e52f345143e847d48.tar.gz tor-e423447f53f3a1b3b72cea3e52f345143e847d48.zip |
Merge remote-tracking branch 'origin/maint-0.2.2'
Conflicts:
src/or/connection.c
-rw-r--r-- | changes/bug2850 | 5 | ||||
-rw-r--r-- | src/or/connection.c | 34 |
2 files changed, 28 insertions, 11 deletions
diff --git a/changes/bug2850 b/changes/bug2850 new file mode 100644 index 0000000000..77ccbfa25d --- /dev/null +++ b/changes/bug2850 @@ -0,0 +1,5 @@ + - Minor features + o Set SO_REUSEADDR on all sockets, not just listeners. This should + help busy exit nodes avoid running out of useable ports just because + all the ports have been used in the near past. Resolves issue 2850. + diff --git a/src/or/connection.c b/src/or/connection.c index 17411e01f8..50acec32a3 100644 --- a/src/or/connection.c +++ b/src/or/connection.c @@ -933,6 +933,25 @@ check_location_for_unix_socket(or_options_t *options, const char *path) } #endif +/** Tell the TCP stack that it shouldn't wait for a long time after + * <b>sock</b> has closed before reusing its port. */ +static void +make_socket_reuseable(int sock) +{ +#ifdef MS_WINDOWS + (void) sock; +#else + int one=1; + + /* REUSEADDR on normal places means you can rebind to the port + * right after somebody else has let it go. But REUSEADDR on win32 + * means you can bind to the port _even when somebody else + * already has it bound_. So, don't do that on Win32. */ + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one, + (socklen_t)sizeof(one)); +#endif +} + /** Bind a new non-blocking socket listening to the socket described * by <b>listensockaddr</b>. * @@ -957,9 +976,6 @@ connection_create_listener(const struct sockaddr *listensockaddr, if (listensockaddr->sa_family == AF_INET) { tor_addr_t addr; int is_tcp = (type != CONN_TYPE_AP_DNS_LISTENER); -#ifndef MS_WINDOWS - int one=1; -#endif if (is_tcp) start_reading = 1; @@ -976,14 +992,7 @@ connection_create_listener(const struct sockaddr *listensockaddr, goto err; } -#ifndef MS_WINDOWS - /* REUSEADDR on normal places means you can rebind to the port - * right after somebody else has let it go. But REUSEADDR on win32 - * means you can bind to the port _even when somebody else - * already has it bound_. So, don't do that on Win32. */ - setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*) &one, - (socklen_t)sizeof(one)); -#endif + make_socket_reuseable(s); if (bind(s,listensockaddr,socklen) < 0) { const char *helpfulhint = ""; @@ -1202,6 +1211,7 @@ connection_handle_listener_read(connection_t *conn, int new_type) "Connection accepted on socket %d (child of fd %d).", news,conn->s); + make_socket_reuseable(news); set_socket_nonblocking(news); if (options->ConstrainedSockets) @@ -1411,6 +1421,8 @@ connection_connect(connection_t *conn, const char *address, log_debug(LD_NET, "Connecting to %s:%u.", escaped_safe_str_client(address), port); + make_socket_reuseable(s); + if (connect(s, dest_addr, (socklen_t)dest_addr_len) < 0) { int e = tor_socket_errno(s); if (!ERRNO_IS_CONN_EINPROGRESS(e)) { |