Browse Source

Start of port to win32. Missing are:
- signal support
- forking for DNS farm
- changes for async IO
- daemonizing

In other words, some files still don't build, and the ones that do build,
do nonblocking IO incorrectly.

I'm also not checking in the project files till I have a good place
for them.


svn:r380

Nick Mathewson 21 years ago
parent
commit
c336c99e60

+ 3 - 1
configure.in

@@ -133,12 +133,14 @@ LIBS="$saved_LIBS -lcrypto"
 
 dnl The warning message here is no longer strictly accurate.
 
-AC_CHECK_HEADERS(unistd.h string.h signal.h netdb.h ctype.h poll.h sys/poll.h sys/types.h sys/fcntl.h sys/ioctl.h sys/socket.h sys/time.h netinet/in.h arpa/inet.h errno.h assert.h , , AC_MSG_WARN(some headers were not found, compilation may fail))
+AC_CHECK_HEADERS(unistd.h string.h signal.h netdb.h ctype.h poll.h sys/poll.h sys/types.h sys/fcntl.h sys/ioctl.h sys/socket.h sys/time.h netinet/in.h arpa/inet.h errno.h assert.h time.h, , AC_MSG_WARN(some headers were not found, compilation may fail))
 
 dnl These headers are not essential
 
 AC_CHECK_HEADERS(stdint.h sys/types.h inttypes.h)
 
+AC_CHECK_FUNCS(gettimeofday ftime)
+
 dnl In case we aren't given a working stdint.h, we'll need to grow our own.
 dnl Watch out.
 

+ 6 - 6
src/common/crypto.c

@@ -40,11 +40,11 @@
 #define RETURN_SSL_OUTCOME(exp) return !(exp)
 #endif
 
-static inline const EVP_CIPHER *
-crypto_cipher_evp_cipher(int type, int enc);
+/* static INLINE const EVP_CIPHER *
+   crypto_cipher_evp_cipher(int type, int enc);
+*/
 
-
-static inline int 
+static INLINE int 
 crypto_cipher_iv_length(int type) {
   /*
   printf("%d -> %d IV\n",type, EVP_CIPHER_iv_length(
@@ -61,7 +61,7 @@ crypto_cipher_iv_length(int type) {
     }
 }
 
-static inline int
+static INLINE int
 crypto_cipher_key_length(int type) {
   /*
   printf("%d -> %d\n",type, EVP_CIPHER_key_length(
@@ -78,7 +78,7 @@ crypto_cipher_key_length(int type) {
     }
 }
 
-static inline const EVP_CIPHER *
+static INLINE const EVP_CIPHER *
 crypto_cipher_evp_cipher(int type, int enc) {
   switch(type) 
     {

+ 14 - 3
src/common/fakepoll.c

@@ -6,13 +6,24 @@
  * Nick Mathewson <nickm@freehaven.net>
  */
 
-#include "fakepoll.h"
-
+#include "orconfig.h"
 #ifdef USE_FAKE_POLL
-#include <sys/time.h>
 #include <sys/types.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
+#ifdef HAVE_STRING_H
 #include <string.h>
+#endif
+#if _MSC_VER > 1300
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#elif defined(_MSC_VER)
+#include <winsock.h>
+#endif
+
+#include "fakepoll.h"
+#include "util.h"
 
 int
 poll(struct pollfd *ufds, unsigned int nfds, int timeout)

+ 2 - 2
src/common/log.c

@@ -3,6 +3,7 @@
 /* $Id$ */
 
 #include "../or/or.h"
+#include "util.h"
 
 static const char *sev_to_string(int severity) {
   switch(severity) {
@@ -30,8 +31,7 @@ logv(int severity, const char *funcname, const char *format, va_list ap)
   assert(format);
   if (severity > loglevel)
     return;
-  if (gettimeofday(&now,NULL) < 0)
-    return;
+  my_gettimeofday(&now);
 
   t = time(NULL);
   strftime(buf, 200, "%b %d %H:%M:%S", localtime(&t));

+ 11 - 0
src/common/log.h

@@ -7,7 +7,18 @@
 
 #ifndef __LOG_H
 
+#ifdef HAVE_SYSLOG_H
 #include <syslog.h>
+#else
+#define LOG_DEBUG   0
+#define LOG_INFO    1
+#define LOG_NOTICE  2
+#define LOG_WARNING 3
+#define LOG_ERR     4
+#define LOG_CRIT    5
+#define LOG_ALERT   6
+#define LOG_EMERG   7
+#endif
 
 /* magic to make GCC check for proper format strings. */ 
 #ifdef __GNUC__

+ 14 - 8
src/common/test.h

@@ -10,12 +10,18 @@
 #define STMT_BEGIN  do {
 #define STMT_END    } while (0)
 
+#ifdef __GNUC__
+#define PRETTY_FUNCTION __PRETTY_FUNCTION__
+#else
+#define PRETTY_FUNCTION ""
+#endif
+
 #define test_fail()                                             \
   STMT_BEGIN                                                    \
     printf("\nFile %s: line %d (%s): assertion failed.",        \
       __FILE__,                                                 \
       __LINE__,                                                 \
-      __PRETTY_FUNCTION__);                                     \
+      PRETTY_FUNCTION);                                     \
     return;                                                     \
   STMT_END
 
@@ -25,7 +31,7 @@
     printf("\nFile %s: line %d (%s): assertion failed: (%s)\n", \
       __FILE__,                                                 \
       __LINE__,                                                 \
-      __PRETTY_FUNCTION__,                                      \
+      PRETTY_FUNCTION,                                      \
       #expr);                                                   \
     return;                                                     \
   } STMT_END
@@ -38,7 +44,7 @@
            "      (%ld != %ld)\n",                              \
       __FILE__,                                                 \
       __LINE__,                                                 \
-      __PRETTY_FUNCTION__,                                      \
+      PRETTY_FUNCTION,                                      \
       #expr1, #expr2,                                           \
       v1, v2);                                                  \
     return;                                                     \
@@ -52,7 +58,7 @@
            "      (%ld == %ld)\n",                              \
       __FILE__,                                                 \
       __LINE__,                                                 \
-      __PRETTY_FUNCTION__,                                      \
+      PRETTY_FUNCTION,                                      \
       #expr1, #expr2,                                           \
       v1, v2);                                                  \
     return;                                                     \
@@ -66,7 +72,7 @@
            "      (\"%s\" != \"%s\")\n",                        \
       __FILE__,                                                 \
       __LINE__,                                                 \
-      __PRETTY_FUNCTION__,                                      \
+      PRETTY_FUNCTION,                                      \
       #expr1, #expr2,                                           \
       v1, v2);                                                  \
     return;                                                     \
@@ -80,7 +86,7 @@
            "      (\"%s\" == \"%s\")\n",                        \
       __FILE__,                                                 \
       __LINE__,                                                 \
-      __PRETTY_FUNCTION__,                                      \
+      PRETTY_FUNCTION,                                      \
       #expr1, #expr2,                                           \
       v1, v2);                                                  \
     return;                                                     \
@@ -93,7 +99,7 @@
     printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n", \
       __FILE__,                                                 \
       __LINE__,                                                 \
-      __PRETTY_FUNCTION__,                                      \
+      PRETTY_FUNCTION,                                      \
       #expr1, #expr2);                                          \
     return;                                                     \
   } STMT_END
@@ -105,7 +111,7 @@
     printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \
       __FILE__,                                                 \
       __LINE__,                                                 \
-      __PRETTY_FUNCTION__,                                      \
+      PRETTY_FUNCTION,                                      \
       #expr1, #expr2);                                          \
     return;                                                     \
   } STMT_END

+ 32 - 0
src/common/util.c

@@ -2,8 +2,23 @@
 /* See LICENSE for licensing information */
 /* $Id$ */
 
+#include "orconfig.h"
+
 #include <stdlib.h>
 #include <limits.h>
+#if _MSC_VER > 1300
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#elif defined(_MSC_VER)
+#include <winsock.h>
+#endif
+#ifdef HAVE_SYS_FCNTL_H
+#include <sys/fcntl.h>
+#endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+
 #include "util.h"
 #include "log.h"
 
@@ -23,12 +38,18 @@ void *tor_malloc(size_t size) {
 void 
 my_gettimeofday(struct timeval *timeval) 
 {
+#ifdef HAVE_GETTIMEOFDAY
   if (gettimeofday(timeval, NULL)) {
     log_fn(LOG_ERR, "gettimeofday failed.");
     /* If gettimeofday dies, we have either given a bad timezone (we didn't),
        or segfaulted.*/
     exit(1);
   }
+#elif defined(HAVE_FTIME)
+  ftime(timeval);
+#else
+#error "No way to get time."
+#endif
   return;
 }
 
@@ -75,3 +96,14 @@ void tv_addms(struct timeval *a, long ms) {
   a->tv_sec += ((ms * 1000) / 1000000) + (a->tv_usec / 1000000);
   a->tv_usec %= 1000000;
 }
+
+void set_socket_nonblocking(int socket)
+{
+#ifdef _MSC_VER
+	/* Yes means no and no means yes.  Do you not want to be nonblocking? */
+	int nonblocking = 0;
+	ioctlsocket(socket, FIONBIO, (unsigned long*) &nonblocking);
+#else
+	fcntl(socket, F_SETFL, O_NONBLOCK);
+#endif
+}

+ 26 - 0
src/common/util.h

@@ -5,7 +5,31 @@
 #ifndef __UTIL_H
 #define __UTIL_H
 
+#include "orconfig.h"
+
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
+#endif
+#ifdef HAVE_TIME_H
+#include <time.h>
+#endif
+#ifndef HAVE_GETTIMEOFDAY
+#ifdef HAVE_FTIME 
+#include <sys/timeb.h>
+#define timeval timeb
+#define tv_sec time
+#define tv_usec millitm
+#endif
+#endif
+
+#ifdef _MSC_VER
+/* Windows names string functions funnily. */
+#define strncasecmp strnicmp
+#define strcasecmp stricmp
+#define INLINE __inline
+#else
+#define INLINE inline
+#endif
 
 void *tor_malloc(size_t size);
 
@@ -19,4 +43,6 @@ void tv_addms(struct timeval *a, long ms);
 void tv_add(struct timeval *a, struct timeval *b);
 int tv_cmp(struct timeval *a, struct timeval *b);
 
+void set_socket_nonblocking(int socket);
+
 #endif

+ 1 - 1
src/or/config.c

@@ -83,7 +83,7 @@ struct config_line *config_get_lines(FILE *f) {
     }
 
     /* walk to the end, remove end whitespace */
-    s = index(line, 0); /* now we're at the null */
+    s = strchr(line, 0); /* now we're at the null */
     do {
       *s = 0;
       s--;

+ 4 - 4
src/or/connection.c

@@ -25,7 +25,7 @@ char *conn_type_to_string[] = {
 };
 
 char *conn_state_to_string[][15] = {
-  { },         /* no type associated with 0 */
+	{ NULL },         /* no type associated with 0 */
   { "ready" }, /* op listener, 0 */
   { "awaiting keys", /* op, 0 */
     "open",              /* 1 */
@@ -146,7 +146,7 @@ int connection_create_listener(struct sockaddr_in *bindaddr, int type) {
     return -1;
   }
 
-  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
+  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*)&one, sizeof(one));
 
   if(bind(s,(struct sockaddr *)bindaddr,sizeof(*bindaddr)) < 0) {
     perror("bind ");
@@ -159,7 +159,7 @@ int connection_create_listener(struct sockaddr_in *bindaddr, int type) {
     return -1;
   }
 
-  fcntl(s, F_SETFL, O_NONBLOCK); /* set s to non-blocking */
+  set_socket_nonblocking(s);
 
   conn = connection_new(type);
   if(!conn) {
@@ -199,7 +199,7 @@ int connection_handle_listener_read(connection_t *conn, int new_type, int new_st
   }
   log(LOG_INFO,"Connection accepted on socket %d (child of fd %d).",news, conn->s);
 
-  fcntl(news, F_SETFL, O_NONBLOCK); /* set news to non-blocking */
+  set_socket_nonblocking(news);
 
   newconn = connection_new(new_type);
   newconn->s = news;

+ 1 - 1
src/or/connection_edge.c

@@ -264,7 +264,7 @@ int connection_edge_finished_flushing(connection_t *conn) {
 
   switch(conn->state) {
     case EXIT_CONN_STATE_CONNECTING:
-      if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, &e, &len) < 0)  { /* not yet */
+      if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0)  { /* not yet */
         if(errno != EINPROGRESS){
           /* yuck. kill it. */
           log_fn(LOG_DEBUG,"in-progress exit connect failed. Removing.");

+ 1 - 1
src/or/connection_exit.c

@@ -76,7 +76,7 @@ int connection_exit_connect(connection_t *conn) {
     log_fn(LOG_ERR,"Error creating network socket.");
     return -1;
   }
-  fcntl(s, F_SETFL, O_NONBLOCK); /* set s to non-blocking */
+  set_socket_nonblocking(s);
 
   memset((void *)&dest_addr,0,sizeof(dest_addr));
   dest_addr.sin_family = AF_INET;

+ 2 - 2
src/or/connection_or.c

@@ -61,7 +61,7 @@ int connection_or_finished_flushing(connection_t *conn) {
     case OR_CONN_STATE_OP_SENDING_KEYS:
       return or_handshake_op_finished_sending_keys(conn);
     case OR_CONN_STATE_CLIENT_CONNECTING:
-      if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, &e, &len) < 0)  { /* not yet */
+      if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0)  { /* not yet */
         if(errno != EINPROGRESS){
           /* yuck. kill it. */
           log_fn(LOG_DEBUG,"in-progress connect failed. Removing.");
@@ -147,7 +147,7 @@ connection_t *connection_or_connect(routerinfo_t *router) {
     connection_free(conn);
     return NULL;
   }
-  fcntl(s, F_SETFL, O_NONBLOCK); /* set s to non-blocking */
+  set_socket_nonblocking(s);
 
   memset((void *)&router_addr,0,sizeof(router_addr));
   router_addr.sin_family = AF_INET;

+ 2 - 2
src/or/directory.c

@@ -56,7 +56,7 @@ void directory_initiate_fetch(routerinfo_t *router) {
     connection_free(conn);
     return;
   }
-  fcntl(s, F_SETFL, O_NONBLOCK); /* set s to non-blocking */
+  set_socket_nonblocking(s);
 
   memset((void *)&router_addr,0,sizeof(router_addr));
   router_addr.sin_family = AF_INET;
@@ -254,7 +254,7 @@ int connection_dir_finished_flushing(connection_t *conn) {
 
   switch(conn->state) {
     case DIR_CONN_STATE_CONNECTING:
-      if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, &e, &len) < 0)  { /* not yet */
+      if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0)  { /* not yet */
         if(errno != EINPROGRESS){
           /* yuck. kill it. */
           log_fn(LOG_DEBUG,"in-progress connect failed. Removing.");

+ 1 - 1
src/or/dns.c

@@ -382,7 +382,7 @@ static int dns_spawn_worker(void) {
     return -1;
   }
 
-  fcntl(fd[0], F_SETFL, O_NONBLOCK); /* set it to non-blocking */
+  set_socket_nonblocking(fd[0]);
 
   /* set up conn so it's got all the data we need to remember */
   conn->receiver_bucket = -1; /* non-cell connections don't do receiver buckets */

+ 52 - 0
src/or/or.h

@@ -10,11 +10,21 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <limits.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
+#ifdef HAVE_STRING_H
 #include <string.h>
+#endif
+#ifdef HAVE_SIGNAL_H
 #include <signal.h>
+#endif
+#ifdef HAVE_NETDB_H
 #include <netdb.h>
+#endif
+#ifdef HAVE_CTYPE_H
 #include <ctype.h>
+#endif
 #include "../common/torint.h"
 #ifdef HAVE_SYS_POLL_H
 #include <sys/poll.h>
@@ -23,17 +33,59 @@
 #else
 #include "../common/fakepoll.h"
 #endif
+#ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_FCNTL_H
 #include <sys/fcntl.h>
+#endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+#ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
+#endif
+#ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
+#endif
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
 #include <sys/stat.h>
+#endif
+#ifdef HAVE_NETINET_IN_H
 #include <netinet/in.h>
+#endif
+#ifdef HAVE_ARPA_INET_H
 #include <arpa/inet.h>
+#endif
+#ifdef HAVE_ERRNO_H
 #include <errno.h>
+#endif
+#ifdef HAVE_ASSERT_H
 #include <assert.h>
+#endif
+#ifdef HAVE_TIME_H
 #include <time.h>
+#endif
+#ifdef HAVE_WINSOCK_H
+#include <winsock.h>
+#endif
+#if _MSC_VER > 1300
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#elif defined(_MSC_VER)
+#include <winsock.h>
+#endif
+
+#ifdef _MSC_VER
+#include <io.h>
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#define snprintf
+#endif
+
 
 #include "../common/crypto.h"
 #include "../common/log.h"

+ 14 - 1
src/or/test.c

@@ -3,7 +3,14 @@
 /* $Id$ */
 
 #include <stdio.h>
+#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
+#endif
+
+#ifdef _MSC_VER
+/* For mkdir() */
+#include <direct.h>
+#endif
 
 #include "or.h"
 #include "../common/test.h"
@@ -26,8 +33,14 @@ dump_hex(char *s, int len)
 void
 setup_directory() {
   char buf[256];
+  int r;
   sprintf(buf, "/tmp/tor_test");
-  if (mkdir(buf, 0700) && errno != EEXIST)
+#ifdef _MSC_VER
+  r = mkdir(buf);
+#else
+  r = mkdir(buf, 0700);
+#endif
+  if (r && errno != EEXIST)
     fprintf(stderr, "Can't create directory %s", buf);
 }
 

+ 146 - 0
src/win32/orconfig.h

@@ -0,0 +1,146 @@
+/* $Id$
+ * orconfig.h  -- This file is *not* generated by autoconf.  Instead,
+ * it has to be hand-edited to keep win32 happy.
+ */
+#define HAVE_OPENSSL
+
+/* Define to 1 if you have the <arpa/inet.h> header file. */
+#undef HAVE_ARPA_INET_H
+
+/* Define to 1 if you have the <assert.h> header file. */
+#define HAVE_ASSERT_H
+
+/* Define to 1 if you have the <ctype.h> header file. */
+#define HAVE_CTYPE_H
+
+/* Define to 1 if you have the <errno.h> header file. */
+#define HAVE_ERRNO_H
+
+#define HAVE_FCNTL_H
+
+#define HAVE_FTIME
+
+#undef HAVE_GETTIMEOFDAY
+
+/* Define to 1 if the system has the type `int16_t'. */
+#undef HAVE_INT16_T
+
+/* Define to 1 if the system has the type `int32_t'. */
+#undef HAVE_INT32_T
+
+/* Define to 1 if the system has the type `int64_t'. */
+#undef HAVE_INT64_T
+
+/* Define to 1 if the system has the type `int8_t'. */
+#undef HAVE_INT8_T
+
+/* Define to 1 if you have the <inttypes.h> header file. */
+#undef HAVE_INTTYPES_H
+
+/* Define to 1 if you have the <memory.h> header file. */
+#define HAVE_MEMORY_H
+
+/* Define to 1 if you have the <netdb.h> header file. */
+#undef HAVE_NETDB_H
+
+/* Define to 1 if you have the <netinet/in.h> header file. */
+#undef HAVE_NETINET_IN_H
+
+/* Define to 1 if you have the <poll.h> header file. */
+#undef HAVE_POLL_H
+
+/* Define to 1 if you have the <signal.h> header file. */
+#define HAVE_SIGNAL_H
+
+/* Define to 1 if you have the <stdint.h> header file. */
+#undef HAVE_STDINT_H
+
+/* Define to 1 if you have the <stdlib.h> header file. */
+#define HAVE_STDLIB_H
+
+/* Define to 1 if you have the <strings.h> header file. */
+#undef HAVE_STRINGS_H
+
+/* Define to 1 if you have the <string.h> header file. */
+#define HAVE_STRING_H
+
+/* Define to 1 if you have the <sys/fcntl.h> header file. */
+#undef HAVE_SYS_FCNTL_H
+
+/* Define to 1 if you have the <sys/ioctl.h> header file. */
+#undef HAVE_SYS_IOCTL_H
+
+/* Define to 1 if you have the <sys/poll.h> header file. */
+#undef HAVE_SYS_POLL_H
+
+/* Define to 1 if you have the <sys/socket.h> header file. */
+#undef HAVE_SYS_SOCKET_H
+
+/* Define to 1 if you have the <sys/stat.h> header file. */
+#define HAVE_SYS_STAT_H
+
+/* Define to 1 if you have the <sys/time.h> header file. */
+#undef HAVE_SYS_TIME_H
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#define HAVE_SYS_TYPES_H
+
+/* Define to 1 if you have the <time.h> header file */
+#define HAVE_TIME_H
+
+/* Define to 1 if the system has the type `uint16_t'. */
+#undef HAVE_UINT16_T
+
+/* Define to 1 if the system has the type `uint32_t'. */
+#undef HAVE_UINT32_T
+
+/* Define to 1 if the system has the type `uint64_t'. */
+#undef HAVE_UINT64_T
+
+/* Define to 1 if the system has the type `uint8_t'. */
+#undef HAVE_UINT8_T
+
+/* Define to 1 if you have the <unistd.h> header file. */
+#undef HAVE_UNISTD_H
+
+/* Name of package */
+#define PACKAGE "tor"
+
+/* Define to the address where bug reports for this package should be sent. */
+#undef PACKAGE_BUGREPORT
+
+/* Define to the full name of this package. */
+#undef PACKAGE_NAME
+
+/* Define to the full name and version of this package. */
+#undef PACKAGE_STRING
+
+/* Define to the one symbol short name of this package. */
+#undef PACKAGE_TARNAME
+
+/* Define to the version of this package. */
+#undef PACKAGE_VERSION
+
+/* The size of a `char', as computed by sizeof. */
+#define SIZEOF_CHAR 1
+
+/* The size of a `int', as computed by sizeof. */
+#define SIZEOF_INT 4
+
+/* The size of a `long', as computed by sizeof. */
+#define SIZEOF_LONG 4
+
+/* The size of a `long long', as computed by sizeof. */
+#define SIZEOF_LONG_LONG 0
+
+/* The size of a `short', as computed by sizeof. */
+#define SIZEOF_SHORT 2
+
+/* The size of a `__int64', as computed by sizeof. */
+#define SIZEOF___INT64 8
+
+/* Define to 1 if you have the ANSI C header files. */
+#define STDC_HEADERS
+
+/* Version number of package */
+#define VERSION "0.0.2pre6"