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

[or-cvs] Attempt to make sockets code work right on windows.



Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv27349/src/common

Modified Files:
	fakepoll.c util.c util.h 
Log Message:
Attempt to make sockets code work right on windows.

Index: fakepoll.c
===================================================================
RCS file: /home/or/cvsroot/src/common/fakepoll.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- fakepoll.c	12 Aug 2003 08:18:13 -0000	1.6
+++ fakepoll.c	14 Aug 2003 17:13:51 -0000	1.7
@@ -8,6 +8,7 @@
 
 #include "orconfig.h"
 #include "fakepoll.h"
+
 #ifdef USE_FAKE_POLL
 #include <sys/types.h>
 #ifdef HAVE_UNISTD_H
@@ -30,6 +31,9 @@
 {
 	unsigned int idx, maxfd, fd;
 	int r;
+#ifdef MS_WINDOWS
+        int any_fds_set = 0;
+#endif
 	fd_set readfds, writefds, exceptfds;
 #ifdef USING_FAKE_TIMEVAL
 #undef timeval
@@ -46,15 +50,26 @@
 	maxfd = -1;
 	for (idx = 0; idx < nfds; ++idx) {
 		fd = ufds[idx].fd;
-		if (fd > maxfd && ufds[idx].events)
-			maxfd = fd;
-		if (ufds[idx].events & (POLLIN))
+                if (ufds[idx].events) {
+                        if (fd > maxfd) 
+                                maxfd = fd;
+#ifdef MS_WINDOWS
+                        any_fds_set = 1;
+#endif
+                }
+		if (ufds[idx].events & POLLIN)
 			FD_SET(fd, &readfds);
 		if (ufds[idx].events & POLLOUT)
 			FD_SET(fd, &writefds);
-		if (ufds[idx].events & (POLLERR))
+		if (ufds[idx].events & POLLERR)
 			FD_SET(fd, &exceptfds);
 	}
+#ifdef MS_WINDOWS
+        if (!any_fds_set) {
+                usleep(timeout);
+                return 0;
+        }
+#endif
 	r = select(maxfd+1, &readfds, &writefds, &exceptfds, 
 		   timeout == -1 ? NULL : &_timeout);
 	if (r <= 0)

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- util.c	12 Aug 2003 08:18:13 -0000	1.13
+++ util.c	14 Aug 2003 17:13:51 -0000	1.14
@@ -4,7 +4,7 @@
 
 #include "../or/or.h"
 
-#ifdef _MSC_VER
+#ifdef MS_WINDOWS
 #include <io.h>
 #include <limits.h>
 #include <process.h>
@@ -90,7 +90,7 @@
 
 void set_socket_nonblocking(int socket)
 {
-#ifdef _MSC_VER
+#ifdef MS_WINDOWS
 	/* Yes means no and no means yes.  Do you not want to be nonblocking? */
 	int nonblocking = 0;
 	ioctlsocket(socket, FIONBIO, (unsigned long*) &nonblocking);
@@ -101,7 +101,7 @@
 
 int spawn_func(int (*func)(void *), void *data)
 {
-#ifdef _MSC_VER
+#ifdef MS_WINDOWS
   int rv;
   rv = _beginthread(func, 0, data);
   if (rv == (unsigned long) -1)
@@ -125,7 +125,7 @@
 
 void spawn_exit()
 {
-#ifdef _MSC_VER
+#ifdef MS_WINDOWS
   _endthread();
 #else
   exit(0);
@@ -137,7 +137,7 @@
 int
 tor_socketpair(int family, int type, int protocol, int fd[2])
 {
-#ifdef HAVE_SOCKETPAIR_XXX
+#ifdef HAVE_SOCKETPAIR_XXXX
     /* For testing purposes, we never fall back to real socketpairs. */
     return socketpair(family, type, protocol, fd);
 #else
@@ -153,8 +153,8 @@
         || family != AF_UNIX
 #endif
         ) {
-#ifdef _MSC_VER
-		errno = WSAEAFNOSUPPORT;
+#ifdef MS_WINDOWS
+        errno = WSAEAFNOSUPPORT;
 #else
         errno = EAFNOSUPPORT;
 #endif
@@ -213,7 +213,7 @@
     return 0;
 
   abort_tidy_up_and_fail:
-#ifdef _MSC_VER
+#ifdef MS_WINDOWS
   errno = WSAECONNABORTED;
 #else
   errno = ECONNABORTED; /* I hope this is portable and appropriate.  */
@@ -232,3 +232,16 @@
     }
 #endif
 }
+
+#ifdef MS_WINDOWS
+int correct_socket_errno(int s)
+{
+  int r, optval, optvallen=sizeof(optval);
+  assert(errno == WSAEWOULDBLOCK);
+  if (getsockopt(s, SOL_SOCKET, SO_ERROR, (void*)&optval, &optvallen))
+    return errno;
+  if (optval)
+    return optval;
+  return WSAEWOULDBLOCK;
+}
+#endif

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- util.h	12 Aug 2003 08:18:13 -0000	1.7
+++ util.h	14 Aug 2003 17:13:51 -0000	1.8
@@ -13,6 +13,12 @@
 #ifdef HAVE_TIME_H
 #include <time.h>
 #endif
+#if _MSC_VER > 1300
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#elif defined(_MSC_VER)
+#include <winsock.h>
+#endif
 #ifndef HAVE_GETTIMEOFDAY
 #ifdef HAVE_FTIME
 #define USING_FAKE_TIMEVAL
@@ -23,7 +29,7 @@
 #endif
 #endif
 
-#ifdef _MSC_VER
+#ifdef MS_WINDOWS
 /* Windows names string functions funnily. */
 #define strncasecmp strnicmp
 #define strcasecmp stricmp
@@ -54,5 +60,25 @@
 void spawn_exit();
 
 int tor_socketpair(int family, int type, int protocol, int fd[2]);
+
+/* For stupid historical reasons, windows sockets have an independent set of 
+ * errnos which they use as the fancy strikes them.
+ */
+#ifdef MS_WINDOWS
+#define ERRNO_EAGAIN(e)           ((e) == EAGAIN || \
+                                   (e) == WSAEWOULDBLOCK || \
+                                   (e) == EWOULDBLOCK)
+#define ERRNO_EINPROGRESS(e)      ((e) == EINPROGRESS || \
+                                   (e) == WSAEINPROGRESS)
+#define ERRNO_CONN_EINPROGRESS(e) ((e) == EINPROGRESS || \
+                                   (e) == WSAEINPROGRESS || (e) == WSAEINVAL)
+int correct_socket_errno(int s);
+#else
+#define ERRNO_EAGAIN(e)           ((e) == EAGAIN)
+#define ERRNO_EINPROGRESS(e)      ((e) == EINPROGRESS)
+#define ERRNO_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
+#define correct_socket_errno(s)   (errno)
+#endif
+
 
 #endif