aboutsummaryrefslogtreecommitdiff
path: root/src/common/compat.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2011-05-23 00:17:48 -0400
committerNick Mathewson <nickm@torproject.org>2011-05-23 00:17:48 -0400
commitcfeafe5e77c9dd5587b1ec553eb1065f0bf841fd (patch)
treef720f8ec3f8dcc065ea47b2c10c1e2885403e082 /src/common/compat.c
parent1ba1bdee4bd8f3c00e603fe9b0fd2f14eeb60466 (diff)
downloadtor-cfeafe5e77c9dd5587b1ec553eb1065f0bf841fd.tar.gz
tor-cfeafe5e77c9dd5587b1ec553eb1065f0bf841fd.zip
Use a 64-bit type to hold sockets on win64.
On win64, sockets are of type UINT_PTR; on win32 they're u_int; elsewhere they're int. The correct windows way to check a socket for being set is to compare it with INVALID_SOCKET; elsewhere you see if it is negative. On Libevent 2, all callbacks take sockets as evutil_socket_t; we've been passing them int. This patch should fix compilation and correctness when built for 64-bit windows. Fixes bug 3270.
Diffstat (limited to 'src/common/compat.c')
-rw-r--r--src/common/compat.c32
1 files changed, 17 insertions, 15 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index fc066da681..9377959eb4 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -841,7 +841,7 @@ socket_accounting_unlock(void)
* Windows, where close()ing a socket doesn't work. Returns 0 on success, -1
* on failure. */
int
-tor_close_socket(int s)
+tor_close_socket(tor_socket_t s)
{
int r = 0;
@@ -894,8 +894,10 @@ tor_close_socket(int s)
/** Helper: if DEBUG_SOCKET_COUNTING is enabled, remember that <b>s</b> is
* now an open socket. */
static INLINE void
-mark_socket_open(int s)
+mark_socket_open(tor_socket_t s)
{
+ /* XXXX This bitarray business will NOT work on windows: sockets aren't
+ small ints there. */
if (s > max_socket) {
if (max_socket == -1) {
open_sockets = bitarray_init_zero(s+128);
@@ -917,11 +919,11 @@ mark_socket_open(int s)
/** @} */
/** As socket(), but counts the number of open sockets. */
-int
+tor_socket_t
tor_open_socket(int domain, int type, int protocol)
{
- int s = socket(domain, type, protocol);
- if (s >= 0) {
+ tor_socket_t s = socket(domain, type, protocol);
+ if (SOCKET_OK(s)) {
socket_accounting_lock();
++n_sockets_open;
mark_socket_open(s);
@@ -931,11 +933,11 @@ tor_open_socket(int domain, int type, int protocol)
}
/** As socket(), but counts the number of open sockets. */
-int
+tor_socket_t
tor_accept_socket(int sockfd, struct sockaddr *addr, socklen_t *len)
{
- int s = accept(sockfd, addr, len);
- if (s >= 0) {
+ tor_socket_t s = accept(sockfd, addr, len);
+ if (SOCKET_OK(s)) {
socket_accounting_lock();
++n_sockets_open;
mark_socket_open(s);
@@ -958,7 +960,7 @@ get_n_open_sockets(void)
/** Turn <b>socket</b> into a nonblocking socket.
*/
void
-set_socket_nonblocking(int socket)
+set_socket_nonblocking(tor_socket_t socket)
{
#if defined(MS_WINDOWS)
unsigned long nonblocking = 1;
@@ -986,7 +988,7 @@ set_socket_nonblocking(int socket)
**/
/* It would be nicer just to set errno, but that won't work for windows. */
int
-tor_socketpair(int family, int type, int protocol, int fd[2])
+tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
{
//don't use win32 socketpairs (they are always bad)
#if defined(HAVE_SOCKETPAIR) && !defined(MS_WINDOWS)
@@ -1011,9 +1013,9 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
* for now, and really, when localhost is down sometimes, we
* have other problems too.
*/
- int listener = -1;
- int connector = -1;
- int acceptor = -1;
+ tor_socket_t listener = -1;
+ tor_socket_t connector = -1;
+ tor_socket_t acceptor = -1;
struct sockaddr_in listen_addr;
struct sockaddr_in connect_addr;
int size;
@@ -2577,11 +2579,11 @@ in_main_thread(void)
*/
#if defined(MS_WINDOWS)
int
-tor_socket_errno(int sock)
+tor_socket_errno(tor_socket_t sock)
{
int optval, optvallen=sizeof(optval);
int err = WSAGetLastError();
- if (err == WSAEWOULDBLOCK && sock >= 0) {
+ if (err == WSAEWOULDBLOCK && SOCKET_OK(sock)) {
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval, &optvallen))
return err;
if (optval)