Clean up W32 NSP tools, increase verbosity
authorLRN <lrn1986@gmail.com>
Sun, 15 Dec 2013 17:23:48 +0000 (17:23 +0000)
committerLRN <lrn1986@gmail.com>
Sun, 15 Dec 2013 17:23:48 +0000 (17:23 +0000)
src/gns/w32nsp-install.c
src/gns/w32nsp-resolve.c
src/gns/w32nsp-uninstall.c

index 5c2ccf23e1a6ddaee3c363b21a365ba8bd6c5ab0..cb25ca9b25205020a2e861c034aca7d6c6712097 100644 (file)
@@ -24,9 +24,9 @@
  */
 
 #define INITGUID
+#include <ws2spi.h>
 #include <windows.h>
 #include <nspapi.h>
-#include <ws2spi.h>
 #include "gnunet_w32nsp_lib.h"
 #include <stdio.h>
 
@@ -40,42 +40,104 @@ main (int argc, char **argv)
   wchar_t *cmdl;
   int wargc;
   wchar_t **wargv;
+  /* Allocate a 4K buffer to retrieve all the namespace providers */
+  DWORD dwInitialBufferLen = 4096;
+  DWORD dwBufferLen;
+  WSANAMESPACE_INFO *pi;
+  int p_count;
+  int i;
 
-  if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
+  if (WSAStartup (MAKEWORD (2,2), &wsd) != 0)
   {
-    fprintf (stderr, "WSAStartup() failed: %lu\n", GetLastError());
+    fprintf (stderr, "WSAStartup () failed: %lu\n", GetLastError ());
     return 5;
   }
 
+  dwBufferLen = dwInitialBufferLen;
+  pi = malloc (dwBufferLen);
+  if (NULL == pi)
+  {
+    fprintf (stderr, "malloc (%lu) failed: %d\n", dwBufferLen, errno);
+    WSACleanup ();
+    return 6;
+  }
+  p_count = WSAEnumNameSpaceProviders (&dwBufferLen, pi);
+  if (SOCKET_ERROR == p_count)
+  {
+    DWORD err = GetLastError ();
+    if (WSAEFAULT == err && dwBufferLen != dwInitialBufferLen)
+    {
+      free (pi);
+
+      pi = malloc (dwBufferLen);
+      if (pi == NULL)
+      {
+        fprintf (stderr, "malloc (%lu) failed: %d\n", dwBufferLen, errno);
+        WSACleanup ();
+        return 6;
+      }
+
+      p_count = WSAEnumNameSpaceProviders (&dwBufferLen, pi);
+      if (SOCKET_ERROR == p_count)
+      {
+        fprintf (stderr, "WSAEnumNameSpaceProviders (&%lu, %p) failed: %lu\n", dwBufferLen, pi, GetLastError ());
+        free (pi);
+        WSACleanup ();
+        return 7;
+      }
+    }
+    else
+    {
+      fprintf (stderr, "WSAEnumNameSpaceProviders (&%lu, %p) failed: %lu\n", dwBufferLen, pi, GetLastError ());
+      free (pi);
+      WSACleanup ();
+      return 8;
+    }
+  }
+  for (i= 0; i < p_count; i++)
+  {
+    if (IsEqualGUID (&pi[i].NSProviderId, &id))
+    {
+      fprintf (stderr, "GNUnet DNS provider is already installed\n");
+      free (pi);
+      WSACleanup ();
+      return 0;
+    }
+  }
+  free (pi);
+
   cmdl = GetCommandLineW ();
   if (cmdl == NULL)
   {
-    WSACleanup();
+    WSACleanup ();
     return 2;
   }
   wargv = CommandLineToArgvW (cmdl, &wargc);
   if (wargv == NULL)
   {
-    WSACleanup();
+    WSACleanup ();
     return 3;
   }
   r = 4;
 
   if (wargc == 2)
   {
-    ret = WSCInstallNameSpace (L"GNUnet DNS provider", wargv[1], NS_DNS, 1, &id);
+    ret = WSCInstallNameSpace (L"GNUnet DNS provider", wargv[1], NS_DNS, 0, &id);
     if (ret == NO_ERROR)
     {
+      fprintf (stderr, "Installed GNUnet DNS provider\n");
       r = 0;
     }
     else
     {
       r = 1;
       fprintf (stderr,
-          "WSCInstallNameSpace(L\"GNUnet DNS provider\", \"%S\", %d, 0, %p) failed: %lu\n",
+          "WSCInstallNameSpace (L\"GNUnet DNS provider\", \"%S\", %d, 0, %p) failed: %lu\n",
           wargv[1], NS_DNS, &id, GetLastError ());
     }
   }
-  WSACleanup();
+  else
+    fprintf (stderr, "Usage: %S <path-to-libw32nsp>\n", wargv[0]);
+  WSACleanup ();
   return r;
 }
index 2bbe4502128b10625e09534ea2b6c6f52330fdab..31f8063433c259f265a65567265a6037e9d1bc90 100644 (file)
@@ -206,6 +206,15 @@ main (int argc, char **argv)
     else
       wargc -= 1;
   }
+  else
+  {
+    fprintf (stderr, "Usage: %S <record type> <service name> <NSP library path> <NSP id>\n"
+        "record type      - one of the following: A | AAAA | name | addr\n"
+        "service name     - a string to resolve; \" \" (a space) means 'blank'\n"
+        "NSP library path - path to libw32nsp\n"
+        "NSP id           - one of the following: mswdns | gnunetdns\n",
+        wargv[0]);
+  }
 
   if (wargc == 5)
   {
@@ -219,12 +228,15 @@ main (int argc, char **argv)
     else
     {
       LPNSPSTARTUP startup = (LPNSPSTARTUP) GetProcAddress (nsp, "NSPStartup");
+      if (startup == NULL)
+        startup = (LPNSPSTARTUP) GetProcAddress (nsp, "NSPStartup@8");
       if (startup != NULL)
       {
         NSP_ROUTINE api;
+        api.cbSize = sizeof (api);
         ret = startup (&prov, &api);
         if (NO_ERROR != ret)
-          fprintf (stderr, "startup failed\n");
+          fprintf (stderr, "startup failed: %lu\n", GetLastError ());
         else
         {
           HANDLE lookup;
@@ -251,7 +263,7 @@ main (int argc, char **argv)
             err = GetLastError ();
             if (ret != NO_ERROR)
             {
-              fprintf (stderr, "lookup next failed\n");
+              fprintf (stderr, "lookup next failed: %lu\n", err);
             }
             else
             {
index ac5aafe2d52b63a22ede515b9f4531fa45e2db96..6b123cc388f83af668a326d6424f4cf8053b1472 100644 (file)
@@ -1,7 +1,7 @@
 #define INITGUID
+#include <ws2spi.h>
 #include <windows.h>
 #include <nspapi.h>
-#include <ws2spi.h>
 #include "gnunet_w32nsp_lib.h"
 #include <stdio.h>
 
@@ -12,19 +12,20 @@ main (int argc, char **argv)
   GUID id = GNUNET_NAMESPACE_PROVIDER_DNS;
   WSADATA wsd;
 
-  if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
+  if (WSAStartup (MAKEWORD (2,2), &wsd) != 0)
   {
-    fprintf (stderr, "WSAStartup() failed: %lu\n", GetLastError());
+    fprintf (stderr, "WSAStartup () failed: %lu\n", GetLastError ());
     return 5;
   }
 
   ret = WSCUnInstallNameSpace (&id);
   if (ret == NO_ERROR)
   {
+    fprintf (stderr, "Uninstalled GNUnet DNS provider\n");
     WSACleanup ();
     return 0;
   }
-  fprintf (stderr, "WSCUnInstallNameSpace() failed: %lu\n", GetLastError ());
+  fprintf (stderr, "WSCUnInstallNameSpace () failed: %lu\n", GetLastError ());
   WSACleanup ();
   return 1;
 }
\ No newline at end of file