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

[or-cvs] r8424: added changes to Tor (in bsockets/trunk/contrib: . tor tor/src tor/src/common)



Author: chiussi
Date: 2006-09-19 13:47:16 -0400 (Tue, 19 Sep 2006)
New Revision: 8424

Added:
   bsockets/trunk/contrib/tor/
   bsockets/trunk/contrib/tor/src/
   bsockets/trunk/contrib/tor/src/common/
   bsockets/trunk/contrib/tor/src/common/bio_bsock.c
   bsockets/trunk/contrib/tor/src/common/compat.c.diff
   bsockets/trunk/contrib/tor/src/common/compat.h.diff
   bsockets/trunk/contrib/tor/src/common/tortls.c.diff
Log:
added changes to Tor



Added: bsockets/trunk/contrib/tor/src/common/bio_bsock.c
===================================================================
--- bsockets/trunk/contrib/tor/src/common/bio_bsock.c	2006-09-19 17:41:22 UTC (rev 8423)
+++ bsockets/trunk/contrib/tor/src/common/bio_bsock.c	2006-09-19 17:47:16 UTC (rev 8424)
@@ -0,0 +1,262 @@
+
+#include <stdio.h>
+#include <errno.h>
+#define USE_SOCKETS
+//#include "cryptlib.h"
+#include <openssl/bio.h>
+#include <openssl/ssl.h>
+
+#include <bsocket.h>
+
+static int bsock_write(BIO *h, const char *buf, int num);
+static int bsock_read(BIO *h, char *buf, int size);
+//static int bsock_puts(BIO *h, const char *str);
+static long bsock_ctrl(BIO *h, int cmd, long arg1, void *arg2);
+static int bsock_new(BIO *h);
+static int bsock_free(BIO *data);
+int BIO_bsock_should_retry(int s);
+
+static BIO_METHOD methods_bsockp=
+	{
+	BIO_TYPE_SOCKET,
+	"bsocket",
+	bsock_write,
+	bsock_read,
+	NULL,
+	NULL, /* sock_gets, */
+	bsock_ctrl,
+	NULL,
+	NULL,
+	NULL,
+	};
+
+BIO_METHOD *BIO_s_bsocket(void)
+	{
+	return(&methods_bsockp);
+	}
+
+int SSL_set_bfd(SSL *s,int fd)
+	{
+	int ret=0;
+	BIO *bio=NULL;
+
+	bio=BIO_new(BIO_s_bsocket());
+
+	if (bio == NULL)
+		{
+		//SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
+		goto err;
+		}
+	BIO_set_fd(bio,fd,BIO_NOCLOSE);
+	SSL_set_bio(s,bio,bio);
+	ret=1;
+err:
+	return(ret);
+	}
+
+//BIO *BIO_new_socket(int fd, int close_flag)
+//	{
+//	BIO *ret;
+//
+//	ret=BIO_new(BIO_s_bsocket());
+//	if (ret == NULL) return(NULL);
+//	BIO_set_fd(ret,fd,close_flag);
+//	return(ret);
+//	}
+
+static int sock_new(BIO *bi)
+	{
+	bi->init=0;
+	bi->num=0;
+	bi->ptr=NULL;
+	bi->flags=0;
+	return(1);
+	}
+
+static int sock_free(BIO *a)
+	{
+	if (a == NULL) return(0);
+	if (a->shutdown)
+{
+		if (a->init)
+		{
+			//SHUTDOWN2(a->num);
+			}
+		a->init=0;
+		a->flags=0;
+		}
+	return(1);
+	}
+
+static int bsock_read(BIO *b, char *out, int outl)
+	{
+	int ret=0;
+
+	if (out != NULL)
+		{
+		errno=0;
+		ret=brecv(b->num,out,outl);
+		BIO_clear_retry_flags(b);
+		if (ret <= 0)
+			{
+			if (BIO_sock_should_retry(ret))
+				BIO_set_retry_read(b);
+			}
+		}
+	return(ret);
+	}
+
+static int bsock_write(BIO *b, const char *in, int inl)
+	{
+	int ret;
+
+	errno=0;
+	ret=bsend(b->num,in,inl);
+	BIO_clear_retry_flags(b);
+	if (ret <= 0)
+		{
+		if (BIO_sock_should_retry(ret))
+			BIO_set_retry_write(b);
+		}
+	return(ret);
+	}
+
+static long bsock_ctrl(BIO *b, int cmd, long num, void *ptr)
+	{
+	long ret=1;
+	int *ip;
+
+	switch (cmd)
+		{
+	case BIO_CTRL_RESET:
+		num=0;
+	case BIO_C_FILE_SEEK:
+		ret=0;
+		break;
+	case BIO_C_FILE_TELL:
+	case BIO_CTRL_INFO:
+		ret=0;
+		break;
+	case BIO_C_SET_FD:
+		sock_free(b);
+		b->num= *((int *)ptr);
+		b->shutdown=(int)num;
+		b->init=1;
+		break;
+	case BIO_C_GET_FD:
+		if (b->init)
+			{
+			ip=(int *)ptr;
+			if (ip != NULL) *ip=b->num;
+			ret=b->num;
+			}
+		else
+			ret= -1;
+		break;
+	case BIO_CTRL_GET_CLOSE:
+		ret=b->shutdown;
+		break;
+	case BIO_CTRL_SET_CLOSE:
+		b->shutdown=(int)num;
+		break;
+	case BIO_CTRL_PENDING:
+	case BIO_CTRL_WPENDING:
+		ret=0;
+		break;
+	case BIO_CTRL_DUP:
+	case BIO_CTRL_FLUSH:
+		ret=1;
+		break;
+	default:
+		ret=0;
+		break;
+		}
+	return(ret);
+	}
+/*
+static int sock_puts(BIO *bp, const char *str)
+	{
+	int n,ret;
+
+	n=strlen(str);
+	ret=sock_write(bp,str,n);
+	return(ret);
+	}
+*/
+int BIO_bsock_should_retry(int i)
+	{
+	int err;
+
+	if ((i == 0) || (i == -1))
+		{
+		err=errno;
+
+#if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
+		if ((i == -1) && (err == 0))
+			return(1);
+#endif
+
+		return(BIO_sock_non_fatal_error(err));
+		}
+	return(0);
+	}
+/*
+int BIO_sock_non_fatal_error(int err)
+	{
+	switch (err)
+		{
+#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_NETWARE)
+# if defined(WSAEWOULDBLOCK)
+	case WSAEWOULDBLOCK:
+# endif
+*/
+//# if 0 /* This appears to always be an error */
+//
+//#  if defined(WSAENOTCONN)
+//	case WSAENOTCONN:
+//#  endif
+//# endif
+//#endif
+//
+//#ifdef EWOULDBLOCK
+//# ifdef WSAEWOULDBLOCK
+//#  if WSAEWOULDBLOCK != EWOULDBLOCK
+//	case EWOULDBLOCK:
+//#  endif
+//# else
+//	case EWOULDBLOCK:
+//# endif
+//#endif
+
+//#if defined(ENOTCONN)
+//	case ENOTCONN:
+//#endif
+//
+//#ifdef EINTR
+//	case EINTR:
+//#endif
+
+//#ifdef EAGAIN
+//# if EWOULDBLOCK != EAGAIN
+//	case EAGAIN:
+//# endif
+//#endif
+//
+//#ifdef EPROTO
+//	case EPROTO:
+//#endif
+//
+//#ifdef EINPROGRESS
+//	case EINPROGRESS:
+//#endif
+
+//#ifdef EALREADY
+//	case EALREADY:
+//#endif
+//		return(1);
+		/* break; */
+//	default:
+//		break;
+//		}
+//	return(0);
+//	}


Property changes on: bsockets/trunk/contrib/tor/src/common/bio_bsock.c
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/tor/src/common/compat.c.diff
===================================================================
--- bsockets/trunk/contrib/tor/src/common/compat.c.diff	2006-09-19 17:41:22 UTC (rev 8423)
+++ bsockets/trunk/contrib/tor/src/common/compat.c.diff	2006-09-19 17:47:16 UTC (rev 8424)
@@ -0,0 +1,185 @@
+4c4
+< /* $Id: compat.c 8387 2006-09-14 04:53:42Z weasel $ */
+---
+> /* $Id: compat.c 8327 2006-09-06 08:42:20Z nickm $ */
+6c6
+<   "$Id: compat.c 8387 2006-09-14 04:53:42Z weasel $";
+---
+>   "$Id: compat.c 8327 2006-09-06 08:42:20Z nickm $";
+105a106,107
+> #include <bsocket.h>
+> 
+133c135
+<   size += (size%page_size) ? page_size-(size%page_size) : 0;
+---
+>   size += (page_size + (page_size-(size%page_size)));
+428,433c430,435
+< #ifdef MS_WINDOWS
+<   unsigned long nonblocking = 1;
+<   ioctlsocket(socket, FIONBIO, (unsigned long*) &nonblocking);
+< #else
+<   fcntl(socket, F_SETFL, O_NONBLOCK);
+< #endif
+---
+> //#ifdef MS_WINDOWS
+> //  unsigned long nonblocking = 1;
+> //  ioctlsocket(socket, FIONBIO, (unsigned long*) &nonblocking);
+> //#else
+>   bfcntl(socket, F_SETFL, O_NONBLOCK);
+> //#endif
+456,554c458
+< //don't use win32 socketpairs (they are always bad)
+< #if defined(HAVE_SOCKETPAIR) && !defined(MS_WINDOWS)
+<   int r;
+<   r = socketpair(family, type, protocol, fd);
+<   return r < 0 ? -errno : r;
+< #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
+<      * for now, and really, when localhost is down sometimes, we
+<      * have other problems too.
+<      */
+<     int listener = -1;
+<     int connector = -1;
+<     int acceptor = -1;
+<     struct sockaddr_in listen_addr;
+<     struct sockaddr_in connect_addr;
+<     int size;
+<     int saved_errno = -1;
+< 
+<     if (protocol
+< #ifdef AF_UNIX
+<         || family != AF_UNIX
+< #endif
+<         ) {
+< #ifdef MS_WINDOWS
+<       return -WSAEAFNOSUPPORT;
+< #else
+<       return -EAFNOSUPPORT;
+< #endif
+<     }
+<     if (!fd) {
+<       return -EINVAL;
+<     }
+< 
+<     listener = socket(AF_INET, type, 0);
+<     if (listener < 0)
+<       return -tor_socket_errno(-1);
+<     memset(&listen_addr, 0, sizeof(listen_addr));
+<     listen_addr.sin_family = AF_INET;
+<     listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+<     listen_addr.sin_port = 0;   /* kernel chooses port.  */
+<     if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr))
+<         == -1)
+<       goto tidy_up_and_fail;
+<     if (listen(listener, 1) == -1)
+<       goto tidy_up_and_fail;
+< 
+<     connector = socket(AF_INET, type, 0);
+<     if (connector < 0)
+<       goto tidy_up_and_fail;
+<     /* We want to find out the port number to connect to.  */
+<     size = sizeof(connect_addr);
+<     if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
+<       goto tidy_up_and_fail;
+<     if (size != sizeof (connect_addr))
+<       goto abort_tidy_up_and_fail;
+<     if (connect(connector, (struct sockaddr *) &connect_addr,
+<                 sizeof(connect_addr)) == -1)
+<       goto tidy_up_and_fail;
+< 
+<     size = sizeof(listen_addr);
+<     acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
+<     if (acceptor < 0)
+<       goto tidy_up_and_fail;
+<     if (size != sizeof(listen_addr))
+<       goto abort_tidy_up_and_fail;
+<     tor_close_socket(listener);
+<     /* Now check we are talking to ourself by matching port and host on the
+<        two sockets.  */
+<     if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
+<       goto tidy_up_and_fail;
+<     if (size != sizeof (connect_addr)
+<         || listen_addr.sin_family != connect_addr.sin_family
+<         || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
+<         || listen_addr.sin_port != connect_addr.sin_port) {
+<       goto abort_tidy_up_and_fail;
+<     }
+<     fd[0] = connector;
+<     fd[1] = acceptor;
+< 
+<     return 0;
+< 
+<   abort_tidy_up_and_fail:
+< #ifdef MS_WINDOWS
+<     saved_errno = WSAECONNABORTED;
+< #else
+<     saved_errno = ECONNABORTED; /* I hope this is portable and appropriate.  */
+< #endif
+<   tidy_up_and_fail:
+<     if (saved_errno < 0)
+<       saved_errno = errno;
+<     if (listener != -1)
+<       tor_close_socket(listener);
+<     if (connector != -1)
+<       tor_close_socket(connector);
+<     if (acceptor != -1)
+<       tor_close_socket(acceptor);
+<     return -saved_errno;
+< #endif
+---
+> 	return bsocketpair(family,type,protocol,fd);
+1236,1250c1140,1154
+< #ifdef MS_WINDOWS
+< int
+< tor_socket_errno(int sock)
+< {
+<   int optval, optvallen=sizeof(optval);
+<   int err = WSAGetLastError();
+<   if (err == WSAEWOULDBLOCK && sock >= 0) {
+<     if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval, &optvallen))
+<       return err;
+<     if (optval)
+<       return optval;
+<   }
+<   return err;
+< }
+< #endif
+---
+> //#ifdef MS_WINDOWS
+> //int
+> //tor_socket_errno(int sock)
+> //{
+> //  int optval, optvallen=sizeof(optval);
+> //  int err = WSAGetLastError();
+> //  if (err == WSAEWOULDBLOCK && sock >= 0) {
+> //    if (bgetsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval, &optvallen))
+> //      return err;
+> //    if (optval)
+> //      return optval;
+> //  }
+> //  return err;
+> //}
+> //a#endif
+1316,1325c1220,1229
+< const char *
+< tor_socket_strerror(int e)
+< {
+<   int i;
+<   for (i=0; windows_socket_errors[i].code >= 0; ++i) {
+<     if (e == windows_socket_errors[i].code)
+<       return windows_socket_errors[i].msg;
+<   }
+<   return strerror(e);
+< }
+---
+> //const char *
+> //tor_socket_strerror(int e)
+> //{
+> //  int i;
+> //  for (i=0; windows_socket_errors[i].code >= 0; ++i) {
+> //    if (e == windows_socket_errors[i].code)
+> //      return windows_socket_errors[i].msg;
+> //  }
+> //  return strerror(e);
+> //}


Property changes on: bsockets/trunk/contrib/tor/src/common/compat.c.diff
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/tor/src/common/compat.h.diff
===================================================================
--- bsockets/trunk/contrib/tor/src/common/compat.h.diff	2006-09-19 17:41:22 UTC (rev 8423)
+++ bsockets/trunk/contrib/tor/src/common/compat.h.diff	2006-09-19 17:47:16 UTC (rev 8424)
@@ -0,0 +1,48 @@
+23a24
+> 
+209c210
+< #define tor_close_socket(s) closesocket(s)
+---
+> #define tor_close_socket(s) bclose(s)
+230,232c231,233
+< #ifdef MS_WINDOWS
+< /** Return true if e is EAGAIN or the local equivalent. */
+< #define ERRNO_IS_EAGAIN(e)           ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
+---
+> //#ifdef MS_WINDOWS
+> ///** Return true if e is EAGAIN or the local equivalent. */
+> //#define ERRNO_IS_EAGAIN(e)           ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
+234c235
+< #define ERRNO_IS_EINPROGRESS(e)      ((e) == WSAEINPROGRESS)
+---
+> //#define ERRNO_IS_EINPROGRESS(e)      ((e) == WSAEINPROGRESS)
+237,238c238,239
+< #define ERRNO_IS_CONN_EINPROGRESS(e) \
+<   ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
+---
+> //#define ERRNO_IS_CONN_EINPROGRESS(e)
+> //  ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
+241c242
+< #define ERRNO_IS_ACCEPT_EAGAIN(e)    ERRNO_IS_EAGAIN(e)
+---
+> //#define ERRNO_IS_ACCEPT_EAGAIN(e)    ERRNO_IS_EAGAIN(e)
+244,245c245,246
+< #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
+<   ((e) == WSAEMFILE || (e) == WSAENOBUFS)
+---
+> //#define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)
+> //  ((e) == WSAEMFILE || (e) == WSAENOBUFS)
+247,250c248,251
+< #define ERRNO_IS_EADDRINUSE(e)      ((e) == WSAEADDRINUSE)
+< int tor_socket_errno(int sock);
+< const char *tor_socket_strerror(int e);
+< #else
+---
+> //#define ERRNO_IS_EADDRINUSE(e)      ((e) == WSAEADDRINUSE)
+> //int tor_socket_errno(int sock);
+> //const char *tor_socket_strerror(int e);
+> //#else
+260c261
+< #endif
+---
+> //#endif


Property changes on: bsockets/trunk/contrib/tor/src/common/compat.h.diff
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/tor/src/common/tortls.c.diff
===================================================================
--- bsockets/trunk/contrib/tor/src/common/tortls.c.diff	2006-09-19 17:41:22 UTC (rev 8423)
+++ bsockets/trunk/contrib/tor/src/common/tortls.c.diff	2006-09-19 17:47:16 UTC (rev 8424)
@@ -0,0 +1,5 @@
+425c425,426
+<   SSL_set_fd(result->ssl, sock);
+---
+>   SSL_set_bfd(result->ssl, sock);
+> 


Property changes on: bsockets/trunk/contrib/tor/src/common/tortls.c.diff
___________________________________________________________________
Name: svn:executable
   + *