[tor-commits] [tor/release-0.2.2] Use a 64-bit type to hold sockets on win64.

arma at torproject.org arma at torproject.org
Mon May 30 20:20:30 UTC 2011


commit cfeafe5e77c9dd5587b1ec553eb1065f0bf841fd
Author: Nick Mathewson <nickm at torproject.org>
Date:   Mon May 23 00:17:48 2011 -0400

    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.
---
 changes/bug3270              |    4 +++
 src/common/compat.c          |   32 +++++++++++----------
 src/common/compat.h          |   21 ++++++++++----
 src/common/compat_libevent.h |    4 +++
 src/common/util.c            |    8 +++---
 src/common/util.h            |    4 +-
 src/or/buffers.c             |    8 +++---
 src/or/buffers.h             |    4 +-
 src/or/connection.c          |   50 +++++++++++++++++----------------
 src/or/control.c             |    2 +-
 src/or/cpuworker.c           |   10 +++---
 src/or/dnsserv.c             |    2 +-
 src/or/eventdns.c            |    2 +-
 src/or/eventdns.h            |    2 +-
 src/or/main.c                |   62 +++++++++++++++++++++--------------------
 src/or/or.h                  |    2 +-
 16 files changed, 120 insertions(+), 97 deletions(-)

diff --git a/changes/bug3270 b/changes/bug3270
new file mode 100644
index 0000000..b37bb98
--- /dev/null
+++ b/changes/bug3270
@@ -0,0 +1,4 @@
+  o Minor bugfixes
+    - Use a wide type to hold sockets when built for 64-bit Windows builds.
+      Fixes bug 3270.
+
diff --git a/src/common/compat.c b/src/common/compat.c
index fc066da..9377959 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)
diff --git a/src/common/compat.h b/src/common/compat.h
index eff51ab..8f558e0 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -390,9 +390,18 @@ int tor_fd_seekend(int fd);
 typedef int socklen_t;
 #endif
 
-int tor_close_socket(int s);
-int tor_open_socket(int domain, int type, int protocol);
-int tor_accept_socket(int sockfd, struct sockaddr *addr, socklen_t *len);
+#ifdef MS_WINDOWS
+#define tor_socket_t intptr_t
+#define SOCKET_OK(s) ((s) != INVALID_SOCKET)
+#else
+#define tor_socket_t int
+#define SOCKET_OK(s) ((s) >= 0)
+#endif
+
+int tor_close_socket(tor_socket_t s);
+tor_socket_t tor_open_socket(int domain, int type, int protocol);
+tor_socket_t tor_accept_socket(int sockfd, struct sockaddr *addr,
+                                  socklen_t *len);
 int get_n_open_sockets(void);
 
 #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
@@ -464,8 +473,8 @@ int tor_inet_aton(const char *cp, struct in_addr *addr) ATTR_NONNULL((1,2));
 const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
 int tor_inet_pton(int af, const char *src, void *dst);
 int tor_lookup_hostname(const char *name, uint32_t *addr) ATTR_NONNULL((1,2));
-void set_socket_nonblocking(int socket);
-int tor_socketpair(int family, int type, int protocol, int fd[2]);
+void set_socket_nonblocking(tor_socket_t socket);
+int tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2]);
 int network_init(void);
 
 /* For stupid historical reasons, windows sockets have an independent
@@ -492,7 +501,7 @@ int network_init(void);
   ((e) == WSAEMFILE || (e) == WSAENOBUFS)
 /** Return true if e is EADDRINUSE or the local equivalent. */
 #define ERRNO_IS_EADDRINUSE(e)      ((e) == WSAEADDRINUSE)
-int tor_socket_errno(int sock);
+int tor_socket_errno(tor_socket_t sock);
 const char *tor_socket_strerror(int e);
 #else
 #define ERRNO_IS_EAGAIN(e)           ((e) == EAGAIN)
diff --git a/src/common/compat_libevent.h b/src/common/compat_libevent.h
index fdf5e0a..1decc8d 100644
--- a/src/common/compat_libevent.h
+++ b/src/common/compat_libevent.h
@@ -9,11 +9,15 @@
 struct event;
 struct event_base;
 
+
 #ifdef HAVE_EVENT2_EVENT_H
 #include <event2/util.h>
 #else
+#ifndef EVUTIL_SOCKET_DEFINED
+#define EVUTIL_SOCKET_DEFINED
 #define evutil_socket_t int
 #endif
+#endif
 
 void configure_libevent_logging(void);
 void suppress_libevent_log_msg(const char *msg);
diff --git a/src/common/util.c b/src/common/util.c
index 7ffe0a9..bf7bb73 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -1568,7 +1568,7 @@ rate_limit_log(ratelim_t *lim, time_t now)
  * was returned by open().  Return the number of bytes written, or -1
  * on error.  Only use if fd is a blocking fd.  */
 ssize_t
-write_all(int fd, const char *buf, size_t count, int isSocket)
+write_all(tor_socket_t fd, const char *buf, size_t count, int isSocket)
 {
   size_t written = 0;
   ssize_t result;
@@ -1578,7 +1578,7 @@ write_all(int fd, const char *buf, size_t count, int isSocket)
     if (isSocket)
       result = tor_socket_send(fd, buf+written, count-written, 0);
     else
-      result = write(fd, buf+written, count-written);
+      result = write((int)fd, buf+written, count-written);
     if (result<0)
       return -1;
     written += result;
@@ -1592,7 +1592,7 @@ write_all(int fd, const char *buf, size_t count, int isSocket)
  * open().  Return the number of bytes read, or -1 on error. Only use
  * if fd is a blocking fd. */
 ssize_t
-read_all(int fd, char *buf, size_t count, int isSocket)
+read_all(tor_socket_t fd, char *buf, size_t count, int isSocket)
 {
   size_t numread = 0;
   ssize_t result;
@@ -1604,7 +1604,7 @@ read_all(int fd, char *buf, size_t count, int isSocket)
     if (isSocket)
       result = tor_socket_recv(fd, buf+numread, count-numread, 0);
     else
-      result = read(fd, buf+numread, count-numread);
+      result = read((int)fd, buf+numread, count-numread);
     if (result<0)
       return -1;
     else if (result == 0)
diff --git a/src/common/util.h b/src/common/util.h
index f32709a..ebbcf78 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -276,8 +276,8 @@ typedef struct ratelim_t {
 char *rate_limit_log(ratelim_t *lim, time_t now);
 
 /* File helpers */
-ssize_t write_all(int fd, const char *buf, size_t count, int isSocket);
-ssize_t read_all(int fd, char *buf, size_t count, int isSocket);
+ssize_t write_all(tor_socket_t fd, const char *buf, size_t count, int isSocket);
+ssize_t read_all(tor_socket_t fd, char *buf, size_t count, int isSocket);
 
 /** Return values from file_status(); see that function's documentation
  * for details. */
diff --git a/src/or/buffers.c b/src/or/buffers.c
index db92695..0516363 100644
--- a/src/or/buffers.c
+++ b/src/or/buffers.c
@@ -587,7 +587,7 @@ buf_add_chunk_with_capacity(buf_t *buf, size_t capacity, int capped)
  * *<b>reached_eof</b> to 1.  Return -1 on error, 0 on eof or blocking,
  * and the number of bytes read otherwise. */
 static INLINE int
-read_to_chunk(buf_t *buf, chunk_t *chunk, int fd, size_t at_most,
+read_to_chunk(buf_t *buf, chunk_t *chunk, tor_socket_t fd, size_t at_most,
               int *reached_eof, int *socket_error)
 {
   ssize_t read_result;
@@ -668,7 +668,7 @@ read_to_chunk_tls(buf_t *buf, chunk_t *chunk, tor_tls_t *tls,
  */
 /* XXXX023 indicate "read blocked" somehow? */
 int
-read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof,
+read_to_buf(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof,
             int *socket_error)
 {
   /* XXXX023 It's stupid to overload the return values for these functions:
@@ -767,7 +767,7 @@ read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf)
  * written on success, 0 on blocking, -1 on failure.
  */
 static INLINE int
-flush_chunk(int s, buf_t *buf, chunk_t *chunk, size_t sz,
+flush_chunk(tor_socket_t s, buf_t *buf, chunk_t *chunk, size_t sz,
             size_t *buf_flushlen)
 {
   ssize_t write_result;
@@ -854,7 +854,7 @@ flush_chunk_tls(tor_tls_t *tls, buf_t *buf, chunk_t *chunk,
  * -1 on failure.  Return 0 if write() would block.
  */
 int
-flush_buf(int s, buf_t *buf, size_t sz, size_t *buf_flushlen)
+flush_buf(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen)
 {
   /* XXXX023 It's stupid to overload the return values for these functions:
    * "error status" and "number of bytes flushed" are not mutually exclusive.
diff --git a/src/or/buffers.h b/src/or/buffers.h
index e50b9ff..63fab49 100644
--- a/src/or/buffers.h
+++ b/src/or/buffers.h
@@ -24,11 +24,11 @@ size_t buf_datalen(const buf_t *buf);
 size_t buf_allocation(const buf_t *buf);
 size_t buf_slack(const buf_t *buf);
 
-int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof,
+int read_to_buf(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof,
                 int *socket_error);
 int read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf);
 
-int flush_buf(int s, buf_t *buf, size_t sz, size_t *buf_flushlen);
+int flush_buf(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen);
 int flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t sz, size_t *buf_flushlen);
 
 int write_to_buf(const char *string, size_t string_len, buf_t *buf);
diff --git a/src/or/connection.c b/src/or/connection.c
index 7241458..e58aba3 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -54,8 +54,8 @@ static int connection_reached_eof(connection_t *conn);
 static int connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
                                   int *socket_error);
 static int connection_process_inbuf(connection_t *conn, int package_partial);
-static void client_check_address_changed(int sock);
-static void set_constrained_socket_buffers(int sock, int size);
+static void client_check_address_changed(tor_socket_t sock);
+static void set_constrained_socket_buffers(tor_socket_t sock, int size);
 
 static const char *connection_proxy_state_to_string(int state);
 static int connection_read_https_proxy_response(connection_t *conn);
@@ -439,8 +439,8 @@ _connection_free(connection_t *conn)
     rend_data_free(dir_conn->rend_data);
   }
 
-  if (conn->s >= 0) {
-    log_debug(LD_NET,"closing fd %d.",conn->s);
+  if (SOCKET_OK(conn->s)) {
+    log_debug(LD_NET,"closing fd %d.",(int)conn->s);
     tor_close_socket(conn->s);
     conn->s = -1;
   }
@@ -663,14 +663,14 @@ connection_close_immediate(connection_t *conn)
   }
   if (conn->outbuf_flushlen) {
     log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
-             conn->s, conn_type_to_string(conn->type),
+             (int)conn->s, conn_type_to_string(conn->type),
              conn_state_to_string(conn->type, conn->state),
              (int)conn->outbuf_flushlen);
   }
 
   connection_unregister_events(conn);
 
-  if (conn->s >= 0)
+  if (SOCKET_OK(conn->s))
     tor_close_socket(conn->s);
   conn->s = -1;
   if (conn->linked)
@@ -740,7 +740,7 @@ connection_expire_held_open(void)
         log_fn(severity, LD_NET,
                "Giving up on marked_for_close conn that's been flushing "
                "for 15s (fd %d, type %s, state %s).",
-               conn->s, conn_type_to_string(conn->type),
+               (int)conn->s, conn_type_to_string(conn->type),
                conn_state_to_string(conn->type, conn->state));
         conn->hold_open_until_flushed = 0;
       }
@@ -893,7 +893,7 @@ check_location_for_unix_socket(or_options_t *options, const char *path)
 /** 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)
+make_socket_reuseable(tor_socket_t sock)
 {
 #ifdef MS_WINDOWS
   (void) sock;
@@ -921,7 +921,7 @@ connection_create_listener(const struct sockaddr *listensockaddr,
                            int type, char* address)
 {
   connection_t *conn;
-  int s; /* the socket we're going to make */
+  tor_socket_t s; /* the socket we're going to make */
   uint16_t usePort = 0, gotPort = 0;
   int start_reading = 0;
 
@@ -944,7 +944,7 @@ connection_create_listener(const struct sockaddr *listensockaddr,
     s = tor_open_socket(PF_INET,
                         is_tcp ? SOCK_STREAM : SOCK_DGRAM,
                         is_tcp ? IPPROTO_TCP: IPPROTO_UDP);
-    if (s < 0) {
+    if (!SOCKET_OK(s)) {
       log_warn(LD_NET,"Socket creation failed.");
       goto err;
     }
@@ -1137,7 +1137,7 @@ check_sockaddr_family_match(sa_family_t got, connection_t *listener)
 static int
 connection_handle_listener_read(connection_t *conn, int new_type)
 {
-  int news; /* the new socket */
+  tor_socket_t news; /* the new socket */
   connection_t *newconn;
   /* information about the remote peer when connecting to other routers */
   char addrbuf[256];
@@ -1150,7 +1150,7 @@ connection_handle_listener_read(connection_t *conn, int new_type)
   memset(addrbuf, 0, sizeof(addrbuf));
 
   news = tor_accept_socket(conn->s,remote,&remotelen);
-  if (news < 0) { /* accept() error */
+  if (!SOCKET_OK(news)) { /* accept() error */
     int e = tor_socket_errno(conn->s);
     if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
       return 0; /* he hung up before we could accept(). that's fine. */
@@ -1166,7 +1166,7 @@ connection_handle_listener_read(connection_t *conn, int new_type)
   }
   log_debug(LD_NET,
             "Connection accepted on socket %d (child of fd %d).",
-            news,conn->s);
+            (int)news,(int)conn->s);
 
   make_socket_reuseable(news);
   set_socket_nonblocking(news);
@@ -1319,7 +1319,8 @@ int
 connection_connect(connection_t *conn, const char *address,
                    const tor_addr_t *addr, uint16_t port, int *socket_error)
 {
-  int s, inprogress = 0;
+  tor_socket_t s;
+  int inprogress = 0;
   char addrbuf[256];
   struct sockaddr *dest_addr;
   socklen_t dest_addr_len;
@@ -2393,7 +2394,7 @@ connection_bucket_refill(int seconds_elapsed, time_t now)
             TO_OR_CONN(conn)->read_bucket > 0)) {
         /* and either a non-cell conn or a cell conn with non-empty bucket */
       LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
-                         "waking up conn (fd %d) for read", conn->s));
+                         "waking up conn (fd %d) for read", (int)conn->s));
       conn->read_blocked_on_bw = 0;
       connection_start_reading(conn);
     }
@@ -2406,7 +2407,7 @@ connection_bucket_refill(int seconds_elapsed, time_t now)
             conn->state != OR_CONN_STATE_OPEN ||
             TO_OR_CONN(conn)->write_bucket > 0)) {
       LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
-                         "waking up conn (fd %d) for write", conn->s));
+                         "waking up conn (fd %d) for write", (int)conn->s));
       conn->write_blocked_on_bw = 0;
       connection_start_writing(conn);
     }
@@ -2598,7 +2599,7 @@ connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
     log_debug(LD_NET,
               "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
               " at_most %ld.",
-              conn->s,(long)buf_datalen(conn->inbuf),
+              (int)conn->s,(long)buf_datalen(conn->inbuf),
               tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
 
     initial_size = buf_datalen(conn->inbuf);
@@ -2769,7 +2770,7 @@ connection_handle_write_impl(connection_t *conn, int force)
 
   tor_assert(!connection_is_listener(conn));
 
-  if (conn->marked_for_close || conn->s < 0)
+  if (conn->marked_for_close || !SOCKET_OK(conn->s))
     return 0; /* do nothing */
 
   if (conn->in_flushed_some) {
@@ -2985,12 +2986,13 @@ _connection_write_to_buf_impl(const char *string, size_t len,
       /* if it failed, it means we have our package/delivery windows set
          wrong compared to our max outbuf size. close the whole circuit. */
       log_warn(LD_NET,
-               "write_to_buf failed. Closing circuit (fd %d).", conn->s);
+               "write_to_buf failed. Closing circuit (fd %d).", (int)conn->s);
       circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
                              END_CIRC_REASON_INTERNAL);
     } else {
       log_warn(LD_NET,
-               "write_to_buf failed. Closing connection (fd %d).", conn->s);
+               "write_to_buf failed. Closing connection (fd %d).",
+               (int)conn->s);
       connection_mark_for_close(conn);
     }
     return;
@@ -3030,7 +3032,7 @@ _connection_write_to_buf_impl(const char *string, size_t len,
         /* this connection is broken. remove it. */
         log_warn(LD_BUG, "unhandled error on write for "
                  "conn (type %d, fd %d); removing",
-                 conn->type, conn->s);
+                 conn->type, (int)conn->s);
         tor_fragile_assert();
         /* do a close-immediate here, so we don't try to flush */
         connection_close_immediate(conn);
@@ -3253,7 +3255,7 @@ alloc_http_authenticator(const char *authenticator)
  * call init_keys().
  */
 static void
-client_check_address_changed(int sock)
+client_check_address_changed(tor_socket_t sock)
 {
   uint32_t iface_ip, ip_out; /* host order */
   struct sockaddr_in out_addr;
@@ -3309,7 +3311,7 @@ client_check_address_changed(int sock)
  * to the desired size to stay below system TCP buffer limits.
  */
 static void
-set_constrained_socket_buffers(int sock, int size)
+set_constrained_socket_buffers(tor_socket_t sock, int size)
 {
   void *sz = (void*)&size;
   socklen_t sz_sz = (socklen_t) sizeof(size);
@@ -3541,7 +3543,7 @@ assert_connection_ok(connection_t *conn, time_t now)
     tor_assert(conn->linked);
   }
   if (conn->linked)
-    tor_assert(conn->s < 0);
+    tor_assert(!SOCKET_OK(conn->s));
 
   if (conn->outbuf_flushlen > 0) {
     tor_assert(connection_is_writing(conn) || conn->write_blocked_on_bw ||
diff --git a/src/or/control.c b/src/or/control.c
index 384e579..a595b2a 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -1490,7 +1490,7 @@ getinfo_helper_listeners(control_connection_t *control_conn,
     struct sockaddr_storage ss;
     socklen_t ss_len = sizeof(ss);
 
-    if (conn->type != type || conn->marked_for_close || conn->s < 0)
+    if (conn->type != type || conn->marked_for_close || !SOCKET_OK(conn->s))
       continue;
 
     if (getsockname(conn->s, (struct sockaddr *)&ss, &ss_len) < 0) {
diff --git a/src/or/cpuworker.c b/src/or/cpuworker.c
index 7cbc191..c5e4863 100644
--- a/src/or/cpuworker.c
+++ b/src/or/cpuworker.c
@@ -226,8 +226,8 @@ cpuworker_main(void *data)
 {
   char question[ONIONSKIN_CHALLENGE_LEN];
   uint8_t question_type;
-  int *fdarray = data;
-  int fd;
+  tor_socket_t *fdarray = data;
+  tor_socket_t fd;
 
   /* variables for onion processing */
   char keys[CPATH_KEY_MATERIAL_LEN];
@@ -317,12 +317,12 @@ cpuworker_main(void *data)
 static int
 spawn_cpuworker(void)
 {
-  int *fdarray;
-  int fd;
+  tor_socket_t *fdarray;
+  tor_socket_t fd;
   connection_t *conn;
   int err;
 
-  fdarray = tor_malloc(sizeof(int)*2);
+  fdarray = tor_malloc(sizeof(tor_socket_t)*2);
   if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray)) < 0) {
     log_warn(LD_NET, "Couldn't construct socketpair for cpuworker: %s",
              tor_socket_strerror(-err));
diff --git a/src/or/dnsserv.c b/src/or/dnsserv.c
index 243b730..009ab5f 100644
--- a/src/or/dnsserv.c
+++ b/src/or/dnsserv.c
@@ -306,7 +306,7 @@ void
 dnsserv_configure_listener(connection_t *conn)
 {
   tor_assert(conn);
-  tor_assert(conn->s >= 0);
+  tor_assert(SOCKET_OK(conn->s));
   tor_assert(conn->type == CONN_TYPE_AP_DNS_LISTENER);
 
   conn->dns_server_port =
diff --git a/src/or/eventdns.c b/src/or/eventdns.c
index fc005df..d06fb2c 100644
--- a/src/or/eventdns.c
+++ b/src/or/eventdns.c
@@ -1560,7 +1560,7 @@ evdns_request_data_build(const char *const name, const size_t name_len,
 
 /* exported function */
 struct evdns_server_port *
-evdns_add_server_port(int socket, int is_tcp, evdns_request_callback_fn_type cb, void *user_data)
+evdns_add_server_port(tor_socket_t socket, int is_tcp, evdns_request_callback_fn_type cb, void *user_data)
 {
 	struct evdns_server_port *port;
 	if (!(port = mm_malloc(sizeof(struct evdns_server_port))))
diff --git a/src/or/eventdns.h b/src/or/eventdns.h
index 2fe4ac9..3ff8bba 100644
--- a/src/or/eventdns.h
+++ b/src/or/eventdns.h
@@ -319,7 +319,7 @@ typedef void (*evdns_request_callback_fn_type)(struct evdns_server_request *, vo
 
 #define EVDNS_CLASS_INET   1
 
-struct evdns_server_port *evdns_add_server_port(int socket, int is_tcp, evdns_request_callback_fn_type callback, void *user_data);
+struct evdns_server_port *evdns_add_server_port(tor_socket_t socket, int is_tcp, evdns_request_callback_fn_type callback, void *user_data);
 void evdns_close_server_port(struct evdns_server_port *port);
 
 int evdns_server_request_add_reply(struct evdns_server_request *req, int section, const char *name, int type, int class, int ttl, int datalen, int is_name, const char *data);
diff --git a/src/or/main.c b/src/or/main.c
index d1ceeec..a8146e7 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -62,8 +62,8 @@ void evdns_shutdown(int);
 
 static void dumpmemusage(int severity);
 static void dumpstats(int severity); /* log stats */
-static void conn_read_callback(int fd, short event, void *_conn);
-static void conn_write_callback(int fd, short event, void *_conn);
+static void conn_read_callback(evutil_socket_t fd, short event, void *_conn);
+static void conn_write_callback(evutil_socket_t fd, short event, void *_conn);
 static void second_elapsed_callback(periodic_timer_t *timer, void *args);
 static int conn_close_if_marked(int i);
 static void connection_start_reading_from_linked_conn(connection_t *conn);
@@ -158,7 +158,7 @@ int
 connection_add(connection_t *conn)
 {
   tor_assert(conn);
-  tor_assert(conn->s >= 0 ||
+  tor_assert(SOCKET_OK(conn->s) ||
              conn->linked ||
              (conn->type == CONN_TYPE_AP &&
               TO_EDGE_CONN(conn)->is_dns_request));
@@ -167,7 +167,7 @@ connection_add(connection_t *conn)
   conn->conn_array_index = smartlist_len(connection_array);
   smartlist_add(connection_array, conn);
 
-  if (conn->s >= 0 || conn->linked) {
+  if (SOCKET_OK(conn->s) || conn->linked) {
     conn->read_event = tor_event_new(tor_libevent_get_base(),
          conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
     conn->write_event = tor_event_new(tor_libevent_get_base(),
@@ -175,7 +175,7 @@ connection_add(connection_t *conn)
   }
 
   log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
-            conn_type_to_string(conn->type), conn->s, conn->address,
+            conn_type_to_string(conn->type), (int)conn->s, conn->address,
             smartlist_len(connection_array));
 
   return 0;
@@ -187,12 +187,12 @@ connection_unregister_events(connection_t *conn)
 {
   if (conn->read_event) {
     if (event_del(conn->read_event))
-      log_warn(LD_BUG, "Error removing read event for %d", conn->s);
+      log_warn(LD_BUG, "Error removing read event for %d", (int)conn->s);
     tor_free(conn->read_event);
   }
   if (conn->write_event) {
     if (event_del(conn->write_event))
-      log_warn(LD_BUG, "Error removing write event for %d", conn->s);
+      log_warn(LD_BUG, "Error removing write event for %d", (int)conn->s);
     tor_free(conn->write_event);
   }
   if (conn->dns_server_port) {
@@ -213,7 +213,7 @@ connection_remove(connection_t *conn)
   tor_assert(conn);
 
   log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
-            conn->s, conn_type_to_string(conn->type),
+            (int)conn->s, conn_type_to_string(conn->type),
             smartlist_len(connection_array));
 
   tor_assert(conn->conn_array_index >= 0);
@@ -344,7 +344,7 @@ connection_stop_reading(connection_t *conn)
     if (event_del(conn->read_event))
       log_warn(LD_NET, "Error from libevent setting read event state for %d "
                "to unwatched: %s",
-               conn->s,
+               (int)conn->s,
                tor_socket_strerror(tor_socket_errno(conn->s)));
   }
 }
@@ -364,7 +364,7 @@ connection_start_reading(connection_t *conn)
     if (event_add(conn->read_event, NULL))
       log_warn(LD_NET, "Error from libevent setting read event state for %d "
                "to watched: %s",
-               conn->s,
+               (int)conn->s,
                tor_socket_strerror(tor_socket_errno(conn->s)));
   }
 }
@@ -394,7 +394,7 @@ connection_stop_writing(connection_t *conn)
     if (event_del(conn->write_event))
       log_warn(LD_NET, "Error from libevent setting write event state for %d "
                "to unwatched: %s",
-               conn->s,
+               (int)conn->s,
                tor_socket_strerror(tor_socket_errno(conn->s)));
   }
 }
@@ -415,7 +415,7 @@ connection_start_writing(connection_t *conn)
     if (event_add(conn->write_event, NULL))
       log_warn(LD_NET, "Error from libevent setting write event state for %d "
                "to watched: %s",
-               conn->s,
+               (int)conn->s,
                tor_socket_strerror(tor_socket_errno(conn->s)));
   }
 }
@@ -501,13 +501,13 @@ close_closeable_connections(void)
 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
  * some data to read. */
 static void
-conn_read_callback(int fd, short event, void *_conn)
+conn_read_callback(evutil_socket_t fd, short event, void *_conn)
 {
   connection_t *conn = _conn;
   (void)fd;
   (void)event;
 
-  log_debug(LD_NET,"socket %d wants to read.",conn->s);
+  log_debug(LD_NET,"socket %d wants to read.",(int)conn->s);
 
   /* assert_connection_ok(conn, time(NULL)); */
 
@@ -516,7 +516,7 @@ conn_read_callback(int fd, short event, void *_conn)
 #ifndef MS_WINDOWS
       log_warn(LD_BUG,"Unhandled error on read for %s connection "
                "(fd %d); removing",
-               conn_type_to_string(conn->type), conn->s);
+               conn_type_to_string(conn->type), (int)conn->s);
       tor_fragile_assert();
 #endif
       if (CONN_IS_EDGE(conn))
@@ -533,13 +533,14 @@ conn_read_callback(int fd, short event, void *_conn)
 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
  * some data to write. */
 static void
-conn_write_callback(int fd, short events, void *_conn)
+conn_write_callback(evutil_socket_t fd, short events, void *_conn)
 {
   connection_t *conn = _conn;
   (void)fd;
   (void)events;
 
-  LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",conn->s));
+  LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",
+                     (int)conn->s));
 
   /* assert_connection_ok(conn, time(NULL)); */
 
@@ -548,7 +549,7 @@ conn_write_callback(int fd, short events, void *_conn)
       /* this connection is broken. remove it. */
       log_fn(LOG_WARN,LD_BUG,
              "unhandled error on write for %s connection (fd %d); removing",
-             conn_type_to_string(conn->type), conn->s);
+             conn_type_to_string(conn->type), (int)conn->s);
       tor_fragile_assert();
       if (CONN_IS_EDGE(conn)) {
         /* otherwise we cry wolf about duplicate close */
@@ -589,8 +590,9 @@ conn_close_if_marked(int i)
   assert_connection_ok(conn, now);
   /* assert_all_pending_dns_resolves_ok(); */
 
-  log_debug(LD_NET,"Cleaning up connection (fd %d).",conn->s);
-  if ((conn->s >= 0 || conn->linked_conn) && connection_wants_to_flush(conn)) {
+  log_debug(LD_NET,"Cleaning up connection (fd %d).",(int)conn->s);
+  if ((SOCKET_OK(conn->s) || conn->linked_conn)
+      && connection_wants_to_flush(conn)) {
     /* s == -1 means it's an incomplete edge connection, or that the socket
      * has already been closed as unflushable. */
     ssize_t sz = connection_bucket_write_limit(conn, now);
@@ -599,7 +601,7 @@ conn_close_if_marked(int i)
                "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
                "to flush %d bytes. (Marked at %s:%d)",
                escaped_safe_str_client(conn->address),
-               conn->s, conn_type_to_string(conn->type), conn->state,
+               (int)conn->s, conn_type_to_string(conn->type), conn->state,
                (int)conn->outbuf_flushlen,
                 conn->marked_for_close_file, conn->marked_for_close);
     if (conn->linked_conn) {
@@ -630,7 +632,7 @@ conn_close_if_marked(int i)
       if (retval > 0) {
         LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
                            "Holding conn (fd %d) open for more flushing.",
-                           conn->s));
+                           (int)conn->s));
         conn->timestamp_lastwritten = now; /* reset so we can flush more */
       }
       return 0;
@@ -652,7 +654,7 @@ conn_close_if_marked(int i)
              "(fd %d, type %s, state %d, marked at %s:%d).",
              (int)buf_datalen(conn->outbuf),
              escaped_safe_str_client(conn->address),
-             conn->s, conn_type_to_string(conn->type), conn->state,
+             (int)conn->s, conn_type_to_string(conn->type), conn->state,
              conn->marked_for_close_file,
              conn->marked_for_close);
     }
@@ -759,7 +761,7 @@ run_connection_housekeeping(int i, time_t now)
        (!DIR_CONN_IS_SERVER(conn) &&
         conn->timestamp_lastread + DIR_CONN_MAX_STALL < now))) {
     log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
-             conn->s, conn->purpose);
+             (int)conn->s, conn->purpose);
     /* This check is temporary; it's to let us know whether we should consider
      * parsing partial serverdesc responses. */
     if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
@@ -787,7 +789,7 @@ run_connection_housekeeping(int i, time_t now)
      * mark it now. */
     log_info(LD_OR,
              "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
-             conn->s, conn->address, conn->port);
+             (int)conn->s, conn->address, conn->port);
     if (conn->state == OR_CONN_STATE_CONNECTING)
       connection_or_connect_failed(TO_OR_CONN(conn),
                                    END_OR_CONN_REASON_TIMEOUT,
@@ -798,7 +800,7 @@ run_connection_housekeeping(int i, time_t now)
     if (past_keepalive) {
       /* We never managed to actually get this connection open and happy. */
       log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
-               conn->s,conn->address, conn->port);
+               (int)conn->s,conn->address, conn->port);
       connection_mark_for_close(conn);
     }
   } else if (we_are_hibernating() && !or_conn->n_circuits &&
@@ -806,14 +808,14 @@ run_connection_housekeeping(int i, time_t now)
     /* We're hibernating, there's no circuits, and nothing to flush.*/
     log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
              "[Hibernating or exiting].",
-             conn->s,conn->address, conn->port);
+             (int)conn->s,conn->address, conn->port);
     connection_mark_for_close(conn);
     conn->hold_open_until_flushed = 1;
   } else if (!or_conn->n_circuits &&
              now >= or_conn->timestamp_last_added_nonpadding +
                                          IDLE_OR_CONN_TIMEOUT) {
     log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
-             "[idle %d].", conn->s,conn->address, conn->port,
+             "[idle %d].", (int)conn->s,conn->address, conn->port,
              (int)(now - or_conn->timestamp_last_added_nonpadding));
     connection_mark_for_close(conn);
     conn->hold_open_until_flushed = 1;
@@ -823,7 +825,7 @@ run_connection_housekeeping(int i, time_t now)
     log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
            "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
            "flush; %d seconds since last write)",
-           conn->s, conn->address, conn->port,
+           (int)conn->s, conn->address, conn->port,
            (int)buf_datalen(conn->outbuf),
            (int)(now-conn->timestamp_lastwritten));
     connection_mark_for_close(conn);
@@ -1700,7 +1702,7 @@ dumpstats(int severity)
     int i = conn_sl_idx;
     log(severity, LD_GENERAL,
         "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
-        i, conn->s, conn->type, conn_type_to_string(conn->type),
+        i, (int)conn->s, conn->type, conn_type_to_string(conn->type),
         conn->state, conn_state_to_string(conn->type, conn->state),
         (int)(now - conn->timestamp_created));
     if (!connection_is_listener(conn)) {
diff --git a/src/or/or.h b/src/or/or.h
index b9d8319..ae56dcd 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -970,7 +970,7 @@ typedef struct connection_t {
   unsigned int proxy_state:4;
 
   /** Our socket; -1 if this connection is closed, or has no socket. */
-  evutil_socket_t s;
+  tor_socket_t s;
   int conn_array_index; /**< Index into the global connection array. */
   struct event *read_event; /**< Libevent event structure. */
   struct event *write_event; /**< Libevent event structure. */





More information about the tor-commits mailing list