diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/compat.c | 46 | ||||
-rw-r--r-- | src/common/compat.h | 17 | ||||
-rw-r--r-- | src/common/util.c | 2 |
3 files changed, 49 insertions, 16 deletions
diff --git a/src/common/compat.c b/src/common/compat.c index f15bb1fd38..e9e46275ef 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -468,6 +468,48 @@ touch_file(const char *fname) return 0; } +/** Count of number of sockets currently open. (Undercounts sockets opened by + * eventdns and libevent.) */ +static int n_sockets_open = 0; + +/** As close(), but guaranteed to work for sockets across platforms (including + * Windows, where close()ing a socket doesn't work. */ +void +tor_close_socket(int s) +{ + /* On Windows, you have to call close() on fds returned by open(), + * and closesocket() on fds returned by socket(). On Unix, everything + * gets close()'d. We abstract this difference by always using + * tor_close_socket to close sockets, and always using close() on + * files. + */ +#ifdef USE_BSOCKETS + bclose(s); +#elif defined(MS_WINDOWS) + closesocket(s); +#else + close(s); +#endif + --n_sockets_open; +} + +/** As socket(), but counts the number of open sockets. */ +int +tor_open_socket(int domain, int type, int protocol) +{ + int s = socket(domain, type, protocol); + if (s >= 0) + ++n_sockets_open; + return s; +} + +/** Return the number of sockets we currently have opened. */ +int +get_n_open_sockets(void) +{ + return n_sockets_open; +} + /** Turn <b>socket</b> into a nonblocking socket. */ void @@ -537,7 +579,7 @@ tor_socketpair(int family, int type, int protocol, int fd[2]) return -EINVAL; } - listener = socket(AF_INET, type, 0); + listener = tor_open_socket(AF_INET, type, 0); if (listener < 0) return -tor_socket_errno(-1); memset(&listen_addr, 0, sizeof(listen_addr)); @@ -550,7 +592,7 @@ tor_socketpair(int family, int type, int protocol, int fd[2]) if (listen(listener, 1) == -1) goto tidy_up_and_fail; - connector = socket(AF_INET, type, 0); + connector = tor_open_socket(AF_INET, type, 0); if (connector < 0) goto tidy_up_and_fail; /* We want to find out the port number to connect to. */ diff --git a/src/common/compat.h b/src/common/compat.h index a6c339ee73..3f2abc1968 100644 --- a/src/common/compat.h +++ b/src/common/compat.h @@ -213,19 +213,10 @@ int touch_file(const char *fname); #endif /* ===== Net compatibility */ -#ifdef USE_BSOCKETS -#define tor_close_socket(s) bclose(s) -#elif defined(MS_WINDOWS) -/** On Windows, you have to call close() on fds returned by open(), - * and closesocket() on fds returned by socket(). On Unix, everything - * gets close()'d. We abstract this difference by always using - * tor_close_socket to close sockets, and always using close() on - * files. - */ -#define tor_close_socket(s) closesocket(s) -#else -#define tor_close_socket(s) close(s) -#endif + +void tor_close_socket(int s); +int tor_open_socket(int domain, int type, int protocol); +int get_n_open_sockets(void); #ifdef USE_BSOCKETS #define tor_socket_send(s, buf, len, flags) bsend(s, buf, len, flags) diff --git a/src/common/util.c b/src/common/util.c index a66edf4332..ff658b7069 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -1914,7 +1914,7 @@ get_interface_address(int severity, uint32_t *addr) tor_assert(addr); *addr = 0; - sock = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP); + sock = tor_open_socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP); if (sock < 0) { int e = tor_socket_errno(-1); log_fn(severity, LD_NET, "unable to create socket: %s", |