[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r8427: Merge in some bsockets calls, all wrapped inside #if defined (in tor/trunk/src: common or)



Author: nickm
Date: 2006-09-19 16:41:31 -0400 (Tue, 19 Sep 2006)
New Revision: 8427

Modified:
   tor/trunk/src/common/compat.c
   tor/trunk/src/common/compat.h
   tor/trunk/src/common/tortls.c
   tor/trunk/src/common/util.c
   tor/trunk/src/or/buffers.c
Log:
Merge in some bsockets calls, all wrapped inside #if defined(USE_BSOCKETS)

Modified: tor/trunk/src/common/compat.c
===================================================================
--- tor/trunk/src/common/compat.c	2006-09-19 20:13:17 UTC (rev 8426)
+++ tor/trunk/src/common/compat.c	2006-09-19 20:41:31 UTC (rev 8427)
@@ -92,6 +92,10 @@
 #include <sys/mman.h>
 #endif
 
+#ifdef USE_BSOCKETS
+#include <bsocket.h>
+#endif
+
 #include "log.h"
 #include "util.h"
 
@@ -425,7 +429,7 @@
 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 @@
   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 @@
  * 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 @@
 }
 #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"),

Modified: tor/trunk/src/common/compat.h
===================================================================
--- tor/trunk/src/common/compat.h	2006-09-19 20:13:17 UTC (rev 8426)
+++ tor/trunk/src/common/compat.h	2006-09-19 20:41:31 UTC (rev 8427)
@@ -199,7 +199,9 @@
 #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 @@
 #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 @@
  * 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. */

Modified: tor/trunk/src/common/tortls.c
===================================================================
--- tor/trunk/src/common/tortls.c	2006-09-19 20:13:17 UTC (rev 8426)
+++ tor/trunk/src/common/tortls.c	2006-09-19 20:41:31 UTC (rev 8427)
@@ -414,7 +414,9 @@
 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 @@
     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;

Modified: tor/trunk/src/common/util.c
===================================================================
--- tor/trunk/src/common/util.c	2006-09-19 20:13:17 UTC (rev 8426)
+++ tor/trunk/src/common/util.c	2006-09-19 20:41:31 UTC (rev 8427)
@@ -936,7 +936,7 @@
 
   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 @@
 
   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)

Modified: tor/trunk/src/or/buffers.c
===================================================================
--- tor/trunk/src/or/buffers.c	2006-09-19 20:13:17 UTC (rev 8426)
+++ tor/trunk/src/or/buffers.c	2006-09-19 20:41:31 UTC (rev 8427)
@@ -408,7 +408,7 @@
   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 @@
 {
   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 */