Browse Source

Merge in some bsockets calls, all wrapped inside #if defined(USE_BSOCKETS)

svn:r8427
Nick Mathewson 17 years ago
parent
commit
93beeac01d
5 changed files with 38 additions and 10 deletions
  1. 9 3
      src/common/compat.c
  2. 12 2
      src/common/compat.h
  3. 13 1
      src/common/tortls.c
  4. 2 2
      src/common/util.c
  5. 2 2
      src/or/buffers.c

+ 9 - 3
src/common/compat.c

@@ -92,6 +92,10 @@ const char compat_c_id[] =
 #include <sys/mman.h>
 #endif
 
+#ifdef USE_BSOCKETS
+#include <bsocket.h>
+#endif
+
 #include "log.h"
 #include "util.h"
 
@@ -425,7 +429,7 @@ touch_file(const char *fname)
 void
 set_socket_nonblocking(int socket)
 {
-#ifdef MS_WINDOWS
+#if defined(MS_WINDOWS) && !defined(USE_BSOCKETS)
   unsigned long nonblocking = 1;
   ioctlsocket(socket, FIONBIO, (unsigned long*) &nonblocking);
 #else
@@ -458,6 +462,8 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
   int r;
   r = socketpair(family, type, protocol, fd);
   return r < 0 ? -errno : r;
+#elif defined(USE_BSOCKETS)
+  return bsockepair(family, type, protocol, fd);
 #else
     /* This socketpair does not work when localhost is down. So
      * it's really not the same thing at all. But it's close enough
@@ -1233,7 +1239,7 @@ struct tor_mutex_t {
  * should call tor_socket_errno <em>at most once</em> on the failing
  * socket to get the error.
  */
-#ifdef MS_WINDOWS
+#if defined(MS_WINDOWS) && !defined(USE_BSOCKETS)
 int
 tor_socket_errno(int sock)
 {
@@ -1249,7 +1255,7 @@ tor_socket_errno(int sock)
 }
 #endif
 
-#ifdef MS_WINDOWS
+#if defined(MS_WINDOWS) && !defined(USE_BSOCKETS)
 #define E(code, s) { code, (s " [" #code " ]") }
 struct { int code; const char *msg; } windows_socket_errors[] = {
   E(WSAEINTR, "Interrupted function call"),

+ 12 - 2
src/common/compat.h

@@ -199,7 +199,9 @@ int touch_file(const char *fname);
 #endif
 
 /* ===== Net compatibility */
-#ifdef MS_WINDOWS
+#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
@@ -211,6 +213,14 @@ int touch_file(const char *fname);
 #define tor_close_socket(s) close(s)
 #endif
 
+#ifdef USE_BSOCKETS
+#define tor_socket_send(s, buf, len, flags) bsend(s, buf, len, flags)
+#define tor_socket_recv(s, buf, len, flags) brecv(s, buf, len, flags)
+#else
+#define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
+#define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
+#endif
+
 #if (SIZEOF_SOCKLEN_T == 0)
 typedef int socklen_t;
 #endif
@@ -227,7 +237,7 @@ int network_init(void);
  * errnos against expected values, and use tor_socket_errno to find
  * the actual errno after a socket operation fails.
  */
-#ifdef MS_WINDOWS
+#if defined(MS_WINDOWS) && !defined(USE_BSOCKETS)
 /** Return true if e is EAGAIN or the local equivalent. */
 #define ERRNO_IS_EAGAIN(e)           ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
 /** Return true if e is EINPROGRESS or the local equivalent. */

+ 13 - 1
src/common/tortls.c

@@ -414,7 +414,9 @@ tor_tls_context_new(crypto_pk_env_t *identity, const char *nickname,
 tor_tls_t *
 tor_tls_new(int sock, int isServer)
 {
+  BIO *bio = NULL;
   tor_tls_t *result = tor_malloc(sizeof(tor_tls_t));
+
   tor_assert(global_tls_context); /* make sure somebody made it first */
   if (!(result->ssl = SSL_new(global_tls_context->ctx))) {
     tls_log_errors(LOG_WARN, "generating TLS context");
@@ -422,7 +424,17 @@ tor_tls_new(int sock, int isServer)
     return NULL;
   }
   result->socket = sock;
-  SSL_set_fd(result->ssl, sock);
+#ifdef USE_BSOCKETS
+  bio = BIO_new_bsocket(sock, BIO_NOCLOSE);
+#else
+  bio = BIO_new_socket(sock, BIO_NOCLOSE);
+#endif
+  if (! bio) {
+    tls_log_errors(LOG_WARN, "opening BIO");
+    tor_free(result);
+    return NULL;
+  }
+  SSL_set_bio(result->ssl, bio, bio);
   result->state = TOR_TLS_ST_HANDSHAKE;
   result->isServer = isServer;
   result->wantwrite_n = 0;

+ 2 - 2
src/common/util.c

@@ -936,7 +936,7 @@ write_all(int fd, const char *buf, size_t count, int isSocket)
 
   while (written != count) {
     if (isSocket)
-      result = send(fd, buf+written, count-written, 0);
+      result = tor_socket_send(fd, buf+written, count-written, 0);
     else
       result = write(fd, buf+written, count-written);
     if (result<0)
@@ -962,7 +962,7 @@ read_all(int fd, char *buf, size_t count, int isSocket)
 
   while (numread != count) {
     if (isSocket)
-      result = recv(fd, buf+numread, count-numread, 0);
+      result = tor_socket_recv(fd, buf+numread, count-numread, 0);
     else
       result = read(fd, buf+numread, count-numread);
     if (result<0)

+ 2 - 2
src/or/buffers.c

@@ -408,7 +408,7 @@ read_to_buf_impl(int s, size_t at_most, buf_t *buf,
   int read_result;
 
 //  log_fn(LOG_DEBUG,"reading at most %d bytes.",at_most);
-  read_result = recv(s, pos, at_most, 0);
+  read_result = tor_socket_recv(s, pos, at_most, 0);
   if (read_result < 0) {
     int e = tor_socket_errno(s);
     if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
@@ -582,7 +582,7 @@ flush_buf_impl(int s, buf_t *buf, size_t sz, size_t *buf_flushlen)
 {
   int write_result;
 
-  write_result = send(s, buf->cur, sz, 0);
+  write_result = tor_socket_send(s, buf->cur, sz, 0);
   if (write_result < 0) {
     int e = tor_socket_errno(s);
     if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */