Merge remote branch 'memphiz/win32'
authorRonnie Sahlberg <ronniesahlberg@gmail.com>
Tue, 13 Sep 2011 05:10:45 +0000 (15:10 +1000)
committerRonnie Sahlberg <ronniesahlberg@gmail.com>
Tue, 13 Sep 2011 05:10:45 +0000 (15:10 +1000)
14 files changed:
README
README.win32.txt
lib/libnfs-sync.c
lib/libnfs-win32.def
lib/libnfs.c
lib/socket.c
nfs/nfs.c
nfs/nfsacl.c
portmap/portmap.c
rquota/rquota.c
win32/libnfs/libnfs.vcxproj
win32/win32_compat.c
win32/win32_compat.h
win32/win32_errnowrapper.h [new file with mode: 0644]

diff --git a/README b/README
index 79f49eb531c8266479e491fcf4f75169edfc90cb..001e6b457814163380b105cc3b91f7d07c8923c2 100644 (file)
--- a/README
+++ b/README
@@ -45,7 +45,7 @@ MacOSX: - tested with SDK 10.4 (under Snow Leopard) - should also work with late
 iOS:    - tested with iOS SDK 4.2 - running on iOS 4.3.x
 FreeBSD:- tested with 8.2
 OpenSolaris
-Windows:- tested on Windows 7 using Visual Studio 10
+Windows:- tested on Windows 7 64 and Windows XP 32 using Visual Studio 10 (see README.win32.txt for build instructions)
 
 
 Cygwin
index 7df1745e40801e2749d0fece2d7ff8db1217d8a3..298045ea4919e6e26facdcf9b66b8ba769b8ece7 100644 (file)
@@ -4,8 +4,7 @@ package should work with some effort.
 
 
 1. checkout git://github.com/Memphiz/oncrpc-win32.git project (branch hyper!)
-2. build oncrpc-win32 project (see README.win32 in the projects dir)
-3. checkout libnfs (branch win32)
-4. adapt oncrpc-win32 path in win32/win32build.bat
-4. build libnfs with cd win32;win32build.bat
-5. copy lib/libnfs.dll and <oncrpc-win32-path>/win32/bin/liboncrpc.dll to the dir where the executable is...
+2. build oncrpc-win32 project (see README.libnfs in the projects dir)
+3. checkout libnfs ("parallel" to oncrpc-win32)
+4. load vs2010 project from win32/libnfs/libnfs.sln
+5. build libnfs and nfsclient-sync
index c7698351148f502e735e581e08d0c696c990a358..97d5a611247d585c8c596cc9e275ad8a4e3d39e4 100644 (file)
@@ -47,8 +47,6 @@
 #include <sys/sockio.h>
 #endif
 
-#include <fcntl.h>
-#include <errno.h>
 #include "libnfs.h"
 #include "libnfs-raw.h"
 #include "libnfs-raw-mount.h"
@@ -716,11 +714,8 @@ static void statvfs_cb(int status, struct nfs_context *nfs, void *data, void *pr
                nfs_set_error(nfs, "statvfs call failed with \"%s\"", (char *)data);
                return;
        }
-#ifndef WIN32
+
        memcpy(cb_data->return_data, data, sizeof(struct statvfs));
-#else
-  cb_data->return_data=NULL;
-#endif/**/
 }
 
 int nfs_statvfs(struct nfs_context *nfs, const char *path, struct statvfs *svfs)
@@ -1189,7 +1184,6 @@ void mount_free_export_list(struct exportnode *exports)
 
 
 
-#if !defined(WIN32)
 void free_nfs_srvr_list(struct nfs_server_list *srv)
 {
        while (srv != NULL) {
@@ -1260,7 +1254,138 @@ void callit_cb(struct rpc_context *rpc, int status, void *data _U_, void *privat
        srv_data->srvrs = srvr;
 }
 
-#ifndef WIN32
+#ifdef WIN32
+
+static int send_nfsd_probes(struct rpc_context *rpc, INTERFACE_INFO *InterfaceList, int numIfs, struct nfs_list_data *data)
+{
+  int i=0;
+
+  for(i = 0; i < numIfs; i++) 
+  {
+    SOCKADDR *pAddress;
+    char bcdd[16];
+    unsigned long nFlags = 0;
+
+    pAddress = (SOCKADDR *) & (InterfaceList[i].iiBroadcastAddress);
+
+    if(pAddress->sa_family != AF_INET)
+      continue;
+               
+    nFlags = InterfaceList[i].iiFlags;
+
+    if (!(nFlags & IFF_UP)) 
+    {
+      continue;
+    }
+
+    if (nFlags & IFF_LOOPBACK) 
+    {
+      continue;
+    }
+
+    if (!(nFlags & IFF_BROADCAST)) 
+    {
+      continue;
+    }
+
+    if (getnameinfo(pAddress, sizeof(struct sockaddr_in), &bcdd[0], sizeof(bcdd), NULL, 0, NI_NUMERICHOST) < 0) 
+    {
+      continue;
+    }
+
+    if (rpc_set_udp_destination(rpc, bcdd, 111, 1) < 0) 
+    {
+      return -1;
+    }
+
+    if (rpc_pmap_callit_async(rpc, MOUNT_PROGRAM, 2, 0, NULL, 0, callit_cb, data) < 0) 
+    {
+      return -1;
+    }
+  }
+  return 0;
+}
+
+struct nfs_server_list *nfs_find_local_servers(void)
+{
+  struct rpc_context *rpc;
+  struct nfs_list_data data = {0, NULL};
+  struct timeval tv_start, tv_current;
+  int loop;
+  struct pollfd pfd;
+  INTERFACE_INFO InterfaceList[20];
+  unsigned long nBytesReturned;
+  int nNumInterfaces = 0;
+
+  rpc = rpc_init_udp_context();
+  if (rpc == NULL) 
+  {
+    return NULL;
+  }
+
+  if (rpc_bind_udp(rpc, "0.0.0.0", 0) < 0) 
+  {
+    rpc_destroy_context(rpc);
+    return NULL;
+  }
+
+  if (WSAIoctl(rpc_get_fd(rpc), SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList, sizeof(InterfaceList), &nBytesReturned, 0, 0) == SOCKET_ERROR) 
+  {
+    return NULL;
+  }
+
+  nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO);
+
+  for (loop=0; loop<3; loop++) 
+  {
+    if (send_nfsd_probes(rpc, InterfaceList, nNumInterfaces, &data) != 0) 
+    {
+      rpc_destroy_context(rpc);
+      return NULL;
+    }
+
+    win32_gettimeofday(&tv_start, NULL);
+    for(;;) 
+    {
+      int mpt;
+
+      pfd.fd = rpc_get_fd(rpc);
+      pfd.events = rpc_which_events(rpc);
+
+      win32_gettimeofday(&tv_current, NULL);
+      mpt = 1000
+      -    (tv_current.tv_sec *1000 + tv_current.tv_usec / 1000)
+      +    (tv_start.tv_sec *1000 + tv_start.tv_usec / 1000);
+
+      if (poll(&pfd, 1, mpt) < 0) 
+      {
+        free_nfs_srvr_list(data.srvrs);
+        rpc_destroy_context(rpc);
+        return NULL;
+      }
+      if (pfd.revents == 0) 
+      {
+        break;
+      }
+               
+      if (rpc_service(rpc, pfd.revents) < 0) 
+      {
+        break;
+      }
+    }
+  }
+
+  rpc_destroy_context(rpc);
+
+  if (data.status != 0) 
+  {
+    free_nfs_srvr_list(data.srvrs);
+    return NULL;
+  }
+  return data.srvrs;
+}
+#else
+
 static int send_nfsd_probes(struct rpc_context *rpc, struct ifconf *ifc, struct nfs_list_data *data)
 {
        char *ptr;
@@ -1312,13 +1437,11 @@ static int send_nfsd_probes(struct rpc_context *rpc, struct ifconf *ifc, struct
 
        return 0;
 }
-#endif/*WIN32 TODO implement this with win32api FIXME*/
 
 struct nfs_server_list *nfs_find_local_servers(void)
 {
        struct rpc_context *rpc;
        struct nfs_list_data data = {0, NULL};
-#ifndef WIN32
        struct timeval tv_start, tv_current;
        struct ifconf ifc;
        int size, loop;
@@ -1395,7 +1518,6 @@ struct nfs_server_list *nfs_find_local_servers(void)
                free_nfs_srvr_list(data.srvrs);
                return NULL;
        }
-#endif/*WIN32 - FIXME redef it when send_nfsd_probes is implemented for win32*/
        return data.srvrs;
 }
-#endif
+#endif//WIN32
index e758a35268b3e211d301a7829c13cb0ebccea927..f553e715090e1c25316d66d7266c483c965c2c1c 100644 (file)
@@ -3,6 +3,8 @@ EXPORTS
 mount_free_export_list
 mount_getexports
 mount_getexports_async
+nfs_find_local_servers
+free_nfs_srvr_list
 nfs_access
 nfs_access_async
 nfs_chmod
index ba7b64893f4ede24fc3b6be4f4eab5774dac09fc..29a276fb5ee0b1d6f3ccfa06b696b428589efa92 100644 (file)
@@ -2002,9 +2002,7 @@ static void nfs_statvfs_1_cb(struct rpc_context *rpc _U_, int status, void *comm
        FSSTAT3res *res;
        struct nfs_cb_data *data = private_data;
        struct nfs_context *nfs = data->nfs;
-#ifndef WIN32
        struct statvfs svfs;
-#endif/*WIN32*/
 
        if (status == RPC_STATUS_ERROR) {
                data->cb(-EFAULT, nfs, command_data, data->private_data);
@@ -2025,7 +2023,6 @@ static void nfs_statvfs_1_cb(struct rpc_context *rpc _U_, int status, void *comm
                return;
        }
 
-#ifndef WIN32
        svfs.f_bsize   = 4096;
        svfs.f_frsize  = 4096;
        svfs.f_blocks  = res->FSSTAT3res_u.resok.tbytes/4096;
@@ -2039,9 +2036,6 @@ static void nfs_statvfs_1_cb(struct rpc_context *rpc _U_, int status, void *comm
        svfs.f_namemax = 256;
 
        data->cb(0, nfs, &svfs, data->private_data);
-#else
-  data->cb(0, nfs,NULL, data->private_data);  
-#endif/*WIN32*/
        free_nfs_cb_data(data);
 }
 
@@ -3208,35 +3202,3 @@ const char *nfs_get_export(struct nfs_context *nfs) {
        return nfs->export;
 }
 
-
-#if defined(WIN32)
-int poll(struct pollfd *fds, int nfsd, int timeout)
-{
-       fd_set rfds, wfds, efds;
-       int ret;
-
-       FD_ZERO(&rfds);
-       FD_ZERO(&wfds);
-       FD_ZERO(&efds);
-       if (fds->events & POLLIN) {
-               FD_SET(fds->fd, &rfds);
-       }
-       if (fds->events & POLLOUT) {
-               FD_SET(fds->fd, &wfds);
-       }
-       FD_SET(fds->fd, &efds);
-       select(fds->fd + 1, &rfds, &wfds, &efds, NULL);
-       fds->revents = 0;
-       if (FD_ISSET(fds->fd, &rfds)) {
-               fds->revents |= POLLIN;
-       }
-       if (FD_ISSET(fds->fd, &wfds)) {
-               fds->revents |= POLLOUT;
-       }
-       if (FD_ISSET(fds->fd, &efds)) {
-               fds->revents |= POLLHUP;
-       }
-       return 1;
-}
-#endif
-
index 6e162a85b6ffc2e73ace6c0ed256230815a3ce9e..bc101e7a6821b01b78d987bd5e8aad917ecfe126 100644 (file)
 #include <netdb.h>
 #endif/*WIN32*/
 
-#if defined(WIN32)
-#include <winsock2.h>
-#include <ws2tcpip.h>
-#include <basetsd.h>
-#define ssize_t SSIZE_T
-#define MSG_DONTWAIT 0
-#else
-#include <unistd.h>
-#include <poll.h>
-#include <arpa/inet.h>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
-#include <netdb.h>
-#endif
-
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 #include "libnfs-private.h"
 #include "slist.h"
 
+#ifdef WIN32
+//has to be included after stdlib!!
+#include "win32_errnowrapper.h"
+#endif
+
+
 static int rpc_disconnect_requeue(struct rpc_context *rpc);
 
 static void set_nonblocking(int fd)
@@ -267,7 +258,11 @@ static int rpc_read_from_socket(struct rpc_context *rpc)
 int rpc_service(struct rpc_context *rpc, int revents)
 {
        if (revents & POLLERR) {
+#ifdef WIN32
                char err = 0;
+#else
+               int err = 0;
+#endif
                socklen_t err_size = sizeof(err);
 
                if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
@@ -414,7 +409,7 @@ int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc
        set_nonblocking(rpc->fd);
 
 #if defined(WIN32)
-       if (connect(rpc->fd, (struct sockaddr *)&s, socksize) == 0 && GetLastError() != WSAEINPROGRESS   )
+       if (connect(rpc->fd, (struct sockaddr *)&s, socksize) == 0 && errno != EINPROGRESS   )
 #else
        if (connect(rpc->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS) 
 #endif
index 50836ca12f6d1528e9374bc42afa57e8853388a1..9d8086f7ca1e5611ad907128526f2eb2938b5c3f 100644 (file)
--- a/nfs/nfs.c
+++ b/nfs/nfs.c
 #include "libnfs-private.h"
 #include "libnfs-raw-nfs.h"
 
-#ifdef WIN32
-#define bzero(a,b) memset((a),(0),(b))
-#endif/*WIN32*/
-
 char *nfsstat3_to_str(int error)
 {
        switch (error) {
index ba1ca12376fc0656b28df3166113fcbb74df5114..e48db02fc7f601edf4371347c02026a6a1461cf1 100644 (file)
 #include <sys/stat.h>
 #endif/*WIN32*/
 
-#if defined(WIN32)
-#include <winsock2.h>
-#endif
-
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
index 29f13d00b3d44a70db72b9c33dd7298e7f65ca70..f8c434d19950e449c0f7bb701c6b32cb992eba8b 100644 (file)
 #include "win32_compat.h"
 #endif/*WIN32*/
 
-#if defined(WIN32)
-#include <winsock2.h>
-#endif
-
 #include <stdio.h>
 #include <rpc/rpc.h>
 #include <rpc/xdr.h>
index fa528c29f84725ef2a78c6768366edf697203404..549f2ff93ae9179dba8cd63a045f0c3a7a0d118b 100644 (file)
 #include "win32_compat.h"
 #endif/*WIN32*/
 
-#if defined(WIN32)
-#include <winsock2.h>
-#endif
-
 #include <stdio.h>
 #include <errno.h>
 #include <sys/stat.h>
index fadcd0c70e967ef147a63446f44e7cc585dc13e7..e46c074e87851fc5292e93ccba809e1d8e2f7e23 100644 (file)
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
     <ConfigurationType>DynamicLibrary</ConfigurationType>
     <UseDebugLibraries>true</UseDebugLibraries>
-    <CharacterSet>Unicode</CharacterSet>
+    <CharacterSet>MultiByte</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
     <ConfigurationType>DynamicLibrary</ConfigurationType>
     <UseDebugLibraries>false</UseDebugLibraries>
     <WholeProgramOptimization>true</WholeProgramOptimization>
-    <CharacterSet>Unicode</CharacterSet>
+    <CharacterSet>MultiByte</CharacterSet>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
   <ImportGroup Label="ExtensionSettings">
@@ -53,7 +53,7 @@
       </PrecompiledHeader>
       <WarningLevel>Level3</WarningLevel>
       <Optimization>Disabled</Optimization>
-      <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;_WIN32_WINNT=0x0501;_U_=;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;_WIN32_WINNT=0x0501;_U_=;_USE_32BIT_TIME_T;__STDC_CONSTANT_MACROS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <AdditionalIncludeDirectories>..\..\..\oncrpc-win32\win32\include;..\..\include;..\..\.;..\..\win32;..\..\mount;..\..\nfs;..\..\lib</AdditionalIncludeDirectories>
       <CompileAs>Default</CompileAs>
       <DisableSpecificWarnings>4996</DisableSpecificWarnings>
@@ -63,6 +63,7 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <ModuleDefinitionFile>..\..\lib\libnfs-win32.def</ModuleDefinitionFile>
       <AdditionalDependencies>oncrpc.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <IgnoreSpecificDefaultLibraries>MSVCRT</IgnoreSpecificDefaultLibraries>
     </Link>
     <PreBuildEvent>
       <Command>copy $(ProjectDir)..\..\nfs\nfs.x $(ProjectDir)..\..\nfs\libnfs-raw-nfs.x
@@ -90,7 +91,7 @@ rpcgen.exe -c $(ProjectDir)..\..\mount\libnfs-raw-mount.x &gt;  $(ProjectDir)..\
       <Optimization>MaxSpeed</Optimization>
       <FunctionLevelLinking>true</FunctionLevelLinking>
       <IntrinsicFunctions>true</IntrinsicFunctions>
-      <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;_WIN32_WINNT=0x0501;_U_=;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;_WIN32_WINNT=0x0501;_U_=;_USE_32BIT_TIME_T;__STDC_CONSTANT_MACROS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <AdditionalIncludeDirectories>..\..\..\oncrpc-win32\win32\include;..\..\include;..\..\.;..\..\win32;..\..\mount;..\..\nfs;..\..\lib</AdditionalIncludeDirectories>
       <DisableSpecificWarnings>4996</DisableSpecificWarnings>
     </ClCompile>
index 65450eb7dbc5c08af58bd2afb4d7e8d68a13900f..fe2ca05762b7e685a17c3bdf7d7d416be6a7850f 100644 (file)
@@ -29,6 +29,8 @@ static int dummy ATTRIBUTE((unused));
 #include "win32_compat.h"
 #include <errno.h>
 #include <stdio.h>
+#include < time.h >
+
 #undef poll
 #undef socket
 #undef connect
@@ -39,7 +41,6 @@ static int dummy ATTRIBUTE((unused));
 #undef inet_aton
 #undef gettimeofday
 #undef stat
-#define bzero(a,b) memset((a),(0),(b))
 #define assert(a)
 
 /* Windows needs this header file for the implementation of inet_aton() */
@@ -47,239 +48,154 @@ static int dummy ATTRIBUTE((unused));
 
 int win32_inet_pton(int af, const char * src, void * dst)
 {
-   int temp = sizeof(struct sockaddr_in);
-   int ret = -1;
-   int len = strlen(src) + 1;
-   wchar_t *srcNonConst = (wchar_t *)malloc(len);
-   memset(srcNonConst, 0, len);
-   MultiByteToWideChar(CP_ACP, NULL, src, -1, srcNonConst, len);
-
-   if( WSAStringToAddress(srcNonConst,af,NULL,(LPSOCKADDR)dst,&temp) == 0 )
-   {
-          ret = 1;
-   }
-   else
-   {
-               if(     WSAGetLastError() == WSAEINVAL )
-               {
-                       ret = -1;
-               }
-   }
-   //free(srcNonConst);
-   return ret;
-}
-
-/* 
- * Check whether "cp" is a valid ascii representation of an Internet address
- * and convert to a binary address.  Returns 1 if the address is valid, 0 if
- * not.  This replaces inet_addr, the return value from which cannot
- * distinguish between failure and a local broadcast address.
- *
- * This implementation of the standard inet_aton() function was copied 
- * (with trivial modifications) from the OpenBSD project.
- */
-int
-win32_inet_aton(const char *cp, struct in_addr *addr)
-{
-    register unsigned int val;
-    register int base, n;
-    register char c;
-    unsigned int parts[4];
-    register unsigned int *pp = parts;
-
-    assert(sizeof(val) == 4);
-
-    c = *cp;
-    while(1) {
-        /*
-         * Collect number up to ``.''.
-         * Values are specified as for C:
-         * 0x=hex, 0=octal, isdigit=decimal.
-         */
-        if(!isdigit(c))
-            return (0);
-        val = 0; base = 10;
-        if(c == '0') {
-            c = *++cp;
-            if(c == 'x' || c == 'X')
-                base = 16, c = *++cp;
-            else
-                base = 8;
-        }
-        while(1) {
-            if(isascii(c) && isdigit(c)) {
-                val = (val * base) + (c - '0');
-                c = *++cp;
-            } else if(base == 16 && isascii(c) && isxdigit(c)) {
-                val = (val << 4) |
-                    (c + 10 - (islower(c) ? 'a' : 'A'));
-                c = *++cp;
-            } else
-                break;
-        }
-        if(c == '.') {
-            /*
-             * Internet format:
-             *    a.b.c.d
-             *    a.b.c    (with c treated as 16 bits)
-             *    a.b    (with b treated as 24 bits)
-             */
-            if(pp >= parts + 3)
-                return (0);
-            *pp++ = val;
-            c = *++cp;
-        } else
-            break;
-    }
-    /*
-     * Check for trailing characters.
-     */
-    if(c != '\0' && (!isascii(c) || !isspace(c)))
-        return (0);
-    /*
-     * Concoct the address according to
-     * the number of parts specified.
-     */
-    n = pp - parts + 1;
-    switch(n) {
-
-    case 0:
-        return (0);        /* initial nondigit */
-
-    case 1:                /* a -- 32 bits */
-        break;
-
-    case 2:                /* a.b -- 8.24 bits */
-        if((val > 0xffffff) || (parts[0] > 0xff))
-            return (0);
-        val |= parts[0] << 24;
-        break;
-
-    case 3:                /* a.b.c -- 8.8.16 bits */
-        if((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
-            return (0);
-        val |= (parts[0] << 24) | (parts[1] << 16);
-        break;
-
-    case 4:                /* a.b.c.d -- 8.8.8.8 bits */
-        if((val > 0xff) || (parts[0] > 0xff) ||
-           (parts[1] > 0xff) || (parts[2] > 0xff))
-            return (0);
-        val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
-        break;
-    }
-    if(addr)
-        addr->s_addr = htonl(val);
-    return (1);
-}
-
-unsigned int
-win32_sleep(unsigned int seconds)
-{
-    Sleep(seconds * 1000);
-    return 0;
-}
-
-int win32_poll(struct pollfd *fds, int nfsd, int timeout)
-{
-  fd_set rfds, wfds, efds;
-  int ret;
+  struct sockaddr_in sa;
+  int len = sizeof(SOCKADDR);
+  int ret = -1;
+  int strLen = strlen(src) + 1;
+#ifdef UNICODE
+  wchar_t *srcNonConst = (wchar_t *)malloc(strLen*sizeof(wchar_t));
+  memset(srcNonConst, 0, strLen);
+  MultiByteToWideChar(CP_ACP, 0, src, -1, srcNonConst, strLen);
+#else
+  char *srcNonConst = (char *)malloc(strLen);
+  memset(srcNonConst, 0, strLen);
+  strncpy(srcNonConst, src, strLen);
+#endif
 
-  FD_ZERO(&rfds);
-  FD_ZERO(&wfds);
-  FD_ZERO(&efds);
-  if (fds->events & POLLIN) 
-  {
-    FD_SET(fds->fd, &rfds);
-  }
-  if (fds->events & POLLOUT) 
-  {
-    FD_SET(fds->fd, &wfds);
-  }
-  FD_SET(fds->fd, &efds);
-  ret = select(fds->fd + 1, &rfds, &wfds, &efds, NULL);
-  fds->revents = 0;
-  
-  if (FD_ISSET(fds->fd, &rfds)) 
-  {
-    fds->revents |= POLLIN;
-  }
-  
-  if (FD_ISSET(fds->fd, &wfds)) 
+  if( WSAStringToAddress(srcNonConst,af,NULL,(LPSOCKADDR)&sa,&len) == 0 )
   {
-    fds->revents |= POLLOUT;
+         ret = 1;
   }
-  
-  if (FD_ISSET(fds->fd, &efds)) 
+  else
   {
-    fds->revents |= POLLHUP;
+    if(        WSAGetLastError() == WSAEINVAL )
+    {
+           ret = -1;
+    }
   }
+  free(srcNonConst);
+  memcpy(dst, &sa.sin_addr, sizeof(struct in_addr));
   return ret;
 }
 
-/*int win32_poll(struct pollfd *fds, unsigned int nfds, int timo)
+int win32_poll(struct pollfd *fds, unsigned int nfds, int timo)
 {
-    struct timeval timeout, *toptr;
-    fd_set ifds, ofds, efds, *ip, *op;
-    int i, rc;
-
-    // Set up the file-descriptor sets in ifds, ofds and efds. 
-    FD_ZERO(&ifds);
-    FD_ZERO(&ofds);
-    FD_ZERO(&efds);
-    for (i = 0, op = ip = 0; i < nfds; ++i) {
-       fds[i].revents = 0;
-       if(fds[i].events & (POLLIN|POLLPRI)) {
-               ip = &ifds;
-               FD_SET(fds[i].fd, ip);
-       }
-       if(fds[i].events & POLLOUT) {
-               op = &ofds;
-               FD_SET(fds[i].fd, op);
-       }
-       FD_SET(fds[i].fd, &efds);
-    } 
-
-    // Set up the timeval structure for the timeout parameter
-    if(timo < 0) {
-       toptr = 0;
-    } else {
-       toptr = &timeout;
-       timeout.tv_sec = timo / 1000;
-       timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
+  struct timeval timeout, *toptr;
+  fd_set ifds, ofds, efds, *ip, *op;
+  unsigned int i;
+  int  rc;
+
+  // Set up the file-descriptor sets in ifds, ofds and efds. 
+  FD_ZERO(&ifds);
+  FD_ZERO(&ofds);
+  FD_ZERO(&efds);
+  for (i = 0, op = ip = 0; i < nfds; ++i) 
+  {
+    fds[i].revents = 0;
+    if(fds[i].events & (POLLIN|POLLPRI)) 
+    {
+      ip = &ifds;
+      FD_SET(fds[i].fd, ip);
+    }
+    if(fds[i].events & POLLOUT) 
+    {
+      op = &ofds;
+      FD_SET(fds[i].fd, op);
     }
+    FD_SET(fds[i].fd, &efds);
+  } 
+
+  // Set up the timeval structure for the timeout parameter
+  if(timo < 0) 
+  {
+    toptr = 0;
+  } 
+  else 
+  {
+    toptr = &timeout;
+    timeout.tv_sec = timo / 1000;
+    timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
+  }
 
 #ifdef DEBUG_POLL
-    printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n",
-           (long)timeout.tv_sec, (long)timeout.tv_usec, (long)ip, (long)op);
+  printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n",
+  (long)timeout.tv_sec, (long)timeout.tv_usec, (long)ip, (long)op);
 #endif
-    rc = select(0, ip, op, &efds, toptr);
+  rc = select(0, ip, op, &efds, toptr);
 #ifdef DEBUG_POLL
-    printf("Exiting select rc=%d\n", rc);
+  printf("Exiting select rc=%d\n", rc);
 #endif
 
-    if(rc <= 0)
-       return rc;
+  if(rc <= 0)
+    return rc;
 
-    if(rc > 0) {
-        for (i = 0; i < nfds; ++i) {
-            int fd = fds[i].fd;
-       if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds))
-               fds[i].revents |= POLLIN;
-       if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
-               fds[i].revents |= POLLOUT;
-       if(FD_ISSET(fd, &efds))
-               // Some error was detected ... should be some way to know.
-               fds[i].revents |= POLLHUP;
+  if(rc > 0) 
+  {
+    for (i = 0; i < nfds; ++i) 
+    {
+      int fd = fds[i].fd;
+      if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds))
+        fds[i].revents |= POLLIN;
+      if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
+        fds[i].revents |= POLLOUT;
+      if(FD_ISSET(fd, &efds)) // Some error was detected ... should be some way to know.
+        fds[i].revents |= POLLHUP;
 #ifdef DEBUG_POLL
-        printf("%d %d %d revent = %x\n", 
-                FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds), 
-                fds[i].revents
-        );
+      printf("%d %d %d revent = %x\n", 
+      FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds), 
+      fds[i].revents
+      );
 #endif
-        }
     }
-    return rc;
+  }
+  return rc;
 }
-*/
+
+#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
+  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
+#else
+  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
+#endif
+struct timezone 
+{
+  int  tz_minuteswest; /* minutes W of Greenwich */
+  int  tz_dsttime;     /* type of dst correction */
+};
+int win32_gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+  FILETIME ft;
+  unsigned __int64 tmpres = 0;
+  static int tzflag;
+  if (NULL != tv)
+  {
+    GetSystemTimeAsFileTime(&ft);
+    tmpres |= ft.dwHighDateTime;
+    tmpres <<= 32;
+    tmpres |= ft.dwLowDateTime;
+    /*converting file time to unix epoch*/
+    tmpres -= DELTA_EPOCH_IN_MICROSECS; 
+    tmpres /= 10;  /*convert into microseconds*/
+    tv->tv_sec = (long)(tmpres / 1000000UL);
+    tv->tv_usec = (long)(tmpres % 1000000UL);
+  }
+  if (NULL != tz)
+  {
+    if (!tzflag)
+    {
+      _tzset();
+      tzflag++;
+    }
+    tz->tz_minuteswest = _timezone / 60;
+    tz->tz_dsttime = _daylight;
+  }
+  return 0;
+}
+
 #endif
index 2ddb0ff281a7abf8065a6f87a7019cf667753769..9972e5da6e19a06903f5279ba38b975ab7aa5239 100644 (file)
@@ -29,6 +29,7 @@ THE SOFTWARE.
 
 #include <winsock2.h>
 #include <ws2tcpip.h>
+#include <Ws2ipdef.h>
 #include <basetsd.h>
 #include <io.h>
 #include <sys/stat.h>
@@ -37,7 +38,6 @@ typedef int uid_t;
 typedef int gid_t;
 typedef int socklen_t;
 
-
 #define S_IRUSR 0000400
 #define S_IWUSR 0000200
 #define S_IXUSR 0000100
@@ -74,11 +74,10 @@ struct pollfd {
 
 /* Wrapper macros to call misc. functions win32 is missing */
 #define poll(x, y, z)        win32_poll(x, y, z)
-#define inet_aton(x, y)      win32_inet_aton(x, y)
 #define inet_pton(x,y,z)     win32_inet_pton(x,y,z)
-int     win32_inet_aton(const char *, struct in_addr *);
 int     win32_inet_pton(int af, const char * src, void * dst);
-int     win32_poll(struct pollfd *fds, int nfsd, int timeout);
+int     win32_poll(struct pollfd *fds, unsigned int nfsd, int timeout);
+int     win32_gettimeofday(struct timeval *tv, struct timezone *tz);
 
 #endif//win32_COMPAT_H_
 #endif//WIN32
diff --git a/win32/win32_errnowrapper.h b/win32/win32_errnowrapper.h
new file mode 100644 (file)
index 0000000..4477d3f
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef WIN32_ERRNOWRAPPER_H_
+#define WIN32_ERRNOWRAPPER_H_
+
+#undef errno
+#define errno WSAGetLastError()
+#undef EAGAIN
+#undef EWOULDBLOCK
+#undef EINTR
+#undef EINPROGRESS
+
+#define EWOULDBLOCK WSAEWOULDBLOCK
+#define EAGAIN         WSAEWOULDBLOCK                                  //same on windows
+#define EINTR       WSAEINTR
+#define EINPROGRESS WSAEINPROGRESS
+#endif //WIN32_ERRNOWRAPPER_H_