[win32] - completed win32 port
[deb_libnfs.git] / lib / libnfs-sync.c
index e1f95ff0544dc17ee9ab62c421e61917f4111c50..97d5a611247d585c8c596cc9e275ad8a4e3d39e4 100644 (file)
 /*
  * High level api to nfs filesystems
  */
+#ifdef WIN32
+#include "win32_compat.h"
+#define DllExport
+#else
+#include <strings.h>
+#include <unistd.h>
+#include <sys/statvfs.h>
+#include <poll.h>
+#include <sys/ioctl.h>
+#include <netdb.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#endif
 
+#ifdef HAVE_CONFIG_H
 #include "config.h"
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <strings.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/statvfs.h>
-#include <sys/ioctl.h>
-#include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
-#include <poll.h>
-#include <sys/socket.h>
-#include <net/if.h>
-#include <netdb.h>
+
+#ifdef HAVE_SYS_SOCKIO_H
+#include <sys/sockio.h>
+#endif
+
 #include "libnfs.h"
 #include "libnfs-raw.h"
 #include "libnfs-raw-mount.h"
@@ -1171,7 +1184,6 @@ void mount_free_export_list(struct exportnode *exports)
 
 
 
-
 void free_nfs_srvr_list(struct nfs_server_list *srv)
 {
        while (srv != NULL) {
@@ -1219,7 +1231,7 @@ void callit_cb(struct rpc_context *rpc, int status, void *data _U_, void *privat
        /* check for dupes */
        for (srvr = srv_data->srvrs; srvr; srvr = srvr->next) {
                if (!strcmp(hostdd, srvr->addr)) {
-                       return 0;
+                       return;
                }
        }
 
@@ -1242,6 +1254,138 @@ void callit_cb(struct rpc_context *rpc, int status, void *data _U_, void *privat
        srv_data->srvrs = srvr;
 }
 
+#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;
@@ -1374,6 +1518,6 @@ struct nfs_server_list *nfs_find_local_servers(void)
                free_nfs_srvr_list(data.srvrs);
                return NULL;
        }
-
        return data.srvrs;
 }
+#endif//WIN32