first batch of license fixes (boring)
[oweals/gnunet.git] / src / gns / w32nsp-resolve.c
index 2bbe4502128b10625e09534ea2b6c6f52330fdab..95504180b0870a93f16edba9ce92c00067a54830 100644 (file)
@@ -1,33 +1,31 @@
 /*
      This file is part of GNUnet.
-     (C) 2012 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2012 GNUnet e.V.
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Affero General Public License for more details.
 */
 /**
  * @file gns/w32nsp-resolve.c
  * @brief W32 integration for GNS
  * @author LRN
  */
-#define INITGUID
+/* Instead of including gnunet_common.h */
+#define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
+
 #include <ws2tcpip.h>
 #include <windows.h>
 #include <nspapi.h>
 #include <ws2spi.h>
 #include <nspapi.h>
+#include <initguid.h>
 #include "gnunet_w32nsp_lib.h"
 #include <stdio.h>
 
@@ -206,6 +204,18 @@ main (int argc, char **argv)
     else
       wargc -= 1;
   }
+  else if (wargc == 3)
+  {
+  }
+  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 +229,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 +264,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
             {
@@ -347,7 +360,7 @@ main (int argc, char **argv)
                 struct hostent *he = malloc (result->lpBlob->cbSize);
                 if (he != NULL)
                 {
-                  memcpy (he, result->lpBlob->pBlobData, result->lpBlob->cbSize);
+                  GNUNET_memcpy (he, result->lpBlob->pBlobData, result->lpBlob->cbSize);
                   UnpackHostEnt (he);
                   print_hostent (he);
                   free (he);
@@ -364,6 +377,65 @@ main (int argc, char **argv)
       FreeLibrary (nsp);
     }
   }
+  else if (wargc == 3)
+  {
+    int s;
+    ADDRINFOW hints;
+    ADDRINFOW *result;
+    ADDRINFOW *pos;
+
+    memset (&hints, 0, sizeof (struct addrinfo));
+    hints.ai_family = AF_UNSPEC;
+    hints.ai_socktype = SOCK_STREAM;
+
+    if (0 != (s = GetAddrInfoW (wargv[2], NULL, &hints, &result)))
+    {
+      fprintf (stderr, "Cound not resolve `%S' using GetAddrInfoW: %lu\n",
+          wargv[2], GetLastError ());
+    }
+    else
+    {
+      for (pos = result; pos != NULL; pos = pos->ai_next)
+      {
+        wchar_t tmpbuf[1024];
+        DWORD buflen = 1024;
+        if (0 == WSAAddressToStringW (pos->ai_addr, pos->ai_addrlen, NULL, tmpbuf, &buflen))
+          fprintf (stderr, "Result:\n"
+                         "  flags: 0x%X\n"
+                         "  family: 0x%X\n"
+                         "  socktype: 0x%X\n"
+                         "  protocol: 0x%X\n"
+                         "  addrlen: %u\n"
+                         "  addr: %S\n"
+                         "  canonname: %S\n",
+                         pos->ai_flags,
+                         pos->ai_family,
+                         pos->ai_socktype,
+                         pos->ai_protocol,
+                         pos->ai_addrlen,
+                         tmpbuf,
+                         pos->ai_canonname);
+        else
+          fprintf (stderr, "Result:\n"
+                         "  flags: 0x%X\n"
+                         "  family: 0x%X\n"
+                         "  socktype: 0x%X\n"
+                         "  protocol: 0x%X\n"
+                         "  addrlen: %u\n"
+                         "  addr: %S\n"
+                         "  canonname: %S\n",
+                         pos->ai_flags,
+                         pos->ai_family,
+                         pos->ai_socktype,
+                         pos->ai_protocol,
+                         pos->ai_addrlen,
+                         L"<can't stringify>",
+                         pos->ai_canonname);
+      }
+      if (NULL != result)
+        FreeAddrInfoW (result);
+    }
+  }
   WSACleanup();
   return r;
 }