Merge remote-tracking branch 'gnunet/master' into identity_oidc
[oweals/gnunet.git] / src / gns / gnunet-gns-helper-service-w32.c
index 97e94304f3b0ae49b64bfc7629220424854e8a53..a59cc59818a5df409d1390657e44a2837caec8ed 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2012 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2012, 2017 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
@@ -14,8 +14,8 @@
 
      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.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 /**
  * @file gnunet-gns-helper-service-w32.c
  * @author Christian Grothoff
  * @author LRN
  */
-#define INITGUID
 #include "platform.h"
 #include <gnunet_util_lib.h>
+#include <gnunet_identity_service.h>
 #include <gnunet_dnsparser_lib.h>
 #include <gnunet_namestore_service.h>
 #include <gnunet_gns_service.h>
+#include <initguid.h>
 #include "gnunet_w32nsp_lib.h"
 #include "w32resolver.h"
 #include <nspapi.h>
@@ -47,158 +48,109 @@ DEFINE_DNS_GUID(SVCID_DNS_TYPE_SRV, 0x0021);
 DEFINE_GUID(SVCID_HOSTNAME, 0x0002a800, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
 DEFINE_GUID(SVCID_INET_HOSTADDRBYNAME, 0x0002a803, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
 
-struct request
-{
-  struct GNUNET_SERVER_Client *client;
-  GUID sc;
-  int af;
-  wchar_t *name;
-  char *u8name;
-};
-
-/**
- * Handle to GNS service.
- */
-static struct GNUNET_GNS_Handle *gns;
-
-static struct GNUNET_CRYPTO_EccPublicKey *zone = NULL;
-static struct GNUNET_CRYPTO_ShortHashCode user_zone;
-struct GNUNET_CRYPTO_EccPrivateKey *shorten_key = NULL;
-
-
-/**
- * Task run on shutdown.  Cleans up everything.
- *
- * @param cls unused
- * @param tc scheduler context
- */
-static void
-do_shutdown (void *cls,
-            const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  if (NULL != gns)
-  {
-    GNUNET_GNS_disconnect (gns);
-    gns = NULL;
-  }
-}
 
-/**
- * Context for transmitting replies to clients.
- */
-struct TransmitCallbackContext
+struct request
 {
-
   /**
    * We keep these in a doubly-linked list (for cleanup).
    */
-  struct TransmitCallbackContext *next;
+  struct request *next;
 
   /**
    * We keep these in a doubly-linked list (for cleanup).
    */
-  struct TransmitCallbackContext *prev;
+  struct request *prev;
 
   /**
-   * The message that we're asked to transmit.
+   * Client that issued the request
    */
-  struct GNUNET_MessageHeader *msg;
+  struct GNUNET_SERVICE_Client *client;
 
-  /**
-   * Handle for the transmission request.
-   */
-  struct GNUNET_SERVER_TransmitHandle *th;
+  GUID sc;
+
+  int af;
+
+  wchar_t *name;
+
+  char *u8name;
 
+  struct GNUNET_GNS_LookupRequest *lookup_request;
 };
 
 
 /**
  * Head of the doubly-linked list (for cleanup).
  */
-static struct TransmitCallbackContext *tcc_head;
+static struct request *rq_head;
 
 /**
  * Tail of the doubly-linked list (for cleanup).
  */
-static struct TransmitCallbackContext *tcc_tail;
+static struct request *rq_tail;
+
+/**
+ * Handle to GNS service.
+ */
+static struct GNUNET_GNS_Handle *gns;
 
 /**
- * Have we already cleaned up the TCCs and are hence no longer
- * willing (or able) to transmit anything to anyone?
+ * Active operation on identity service.
  */
-static int cleaning_done;
+static struct GNUNET_IDENTITY_Operation *id_op;
 
+/**
+ * Handle for identity service.
+ */
+static struct GNUNET_IDENTITY_Handle *identity;
 
 /**
- * Function called to notify a client about the socket
- * begin ready to queue more data.  "buf" will be
- * NULL and "size" zero if the socket was closed for
- * writing in the meantime.
- *
- * @param cls closure
- * @param size number of bytes available in buf
- * @param buf where the callee should write the message
- * @return number of bytes written to buf
+ * Public key of the gns-master ego
  */
-static size_t
-transmit_callback (void *cls, size_t size, void *buf)
-{
-  struct TransmitCallbackContext *tcc = cls;
-  size_t msize;
+static struct GNUNET_CRYPTO_EcdsaPublicKey gns_master_pubkey;
 
-  tcc->th = NULL;
-  GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
-  msize = ntohs (tcc->msg->size);
-  if (size == 0)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _("Transmission to client failed!\n"));
-    GNUNET_free (tcc->msg);
-    GNUNET_free (tcc);
-    return 0;
-  }
-  GNUNET_assert (size >= msize);
-  memcpy (buf, tcc->msg, msize);
-  GNUNET_free (tcc->msg);
-  GNUNET_free (tcc);
-  return msize;
-}
+/**
+ * Set to 1 once egos are obtained.
+ */
+static int got_egos;
 
 
 /**
- * Transmit the given message to the client.
+ * Task run on shutdown.  Cleans up everything.
  *
- * @param client target of the message
- * @param msg message to transmit, will be freed!
+ * @param cls unused
  */
 static void
-transmit (struct GNUNET_SERVER_Client *client, 
-         struct GNUNET_MessageHeader *msg)
+do_shutdown (void *cls)
 {
-  struct TransmitCallbackContext *tcc;
+  struct request *rq;
 
-  if (GNUNET_YES == cleaning_done)
+  if (NULL != id_op)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _("Shutdown in progress, aborting transmission.\n"));
-    GNUNET_free (msg);
-    return;
+    GNUNET_IDENTITY_cancel (id_op);
+    id_op = NULL;
   }
-  tcc = GNUNET_malloc (sizeof (struct TransmitCallbackContext));
-  tcc->msg = msg;
-  if (NULL ==
-      (tcc->th =
-       GNUNET_SERVER_notify_transmit_ready (client, 
-                                           ntohs (msg->size),
-                                            GNUNET_TIME_UNIT_FOREVER_REL,
-                                            &transmit_callback, tcc)))
+  if (NULL != identity)
   {
-    GNUNET_break (0);
-    GNUNET_free (msg);
-    GNUNET_free (tcc);
-    return;
+    GNUNET_IDENTITY_disconnect (identity);
+    identity = NULL;
+  }
+  while (NULL != (rq = rq_head))
+  {
+    if (NULL != rq->lookup_request)
+      GNUNET_GNS_lookup_cancel (rq->lookup_request);
+    GNUNET_CONTAINER_DLL_remove (rq_head,
+                                 rq_tail,
+                                 rq);
+    GNUNET_free_non_null (rq->name);
+    if (rq->u8name)
+      free (rq->u8name);
+    GNUNET_free (rq);
+  }
+  if (NULL != gns)
+  {
+    GNUNET_GNS_disconnect (gns);
+    gns = NULL;
   }
-  GNUNET_CONTAINER_DLL_insert (tcc_head, tcc_tail, tcc);
 }
 
 
@@ -210,7 +162,6 @@ transmit (struct GNUNET_SERVER_Client *client,
 void
 MarshallWSAQUERYSETW (WSAQUERYSETW *qs, GUID *sc)
 {
-  int i;
   MarshallPtr (qs->lpszServiceInstanceName, qs, wchar_t);
   MarshallPtr (qs->lpServiceClassId, qs, GUID);
   MarshallPtr (qs->lpVersion, qs, WSAVERSION);
@@ -218,7 +169,7 @@ MarshallWSAQUERYSETW (WSAQUERYSETW *qs, GUID *sc)
   MarshallPtr (qs->lpszContext, qs, wchar_t);
   MarshallPtr (qs->lpafpProtocols, qs, AFPROTOCOLS);
   MarshallPtr (qs->lpszQueryString, qs, wchar_t);
-  for (i = 0; i < qs->dwNumberOfCsAddrs; i++)
+  for (int i = 0; i < qs->dwNumberOfCsAddrs; i++)
   {
     MarshallPtr (qs->lpcsaBuffer[i].LocalAddr.lpSockaddr, qs, SOCKADDR);
     MarshallPtr (qs->lpcsaBuffer[i].RemoteAddr.lpSockaddr, qs, SOCKADDR);
@@ -227,12 +178,13 @@ MarshallWSAQUERYSETW (WSAQUERYSETW *qs, GUID *sc)
   if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, sc) && qs->lpBlob != NULL && qs->lpBlob->pBlobData != NULL)
   {
     struct hostent *he;
+
     he = (struct hostent *) qs->lpBlob->pBlobData;
-    for (i = 0; he->h_aliases[i] != NULL; i++)
+    for (int i = 0; he->h_aliases[i] != NULL; i++)
       MarshallPtr (he->h_aliases[i], he, char);
     MarshallPtr (he->h_aliases, he, char *);
     MarshallPtr (he->h_name, he, char);
-    for (i = 0; he->h_addr_list[i] != NULL; i++)
+    for (int i = 0; he->h_addr_list[i] != NULL; i++)
       MarshallPtr (he->h_addr_list[i], he, void);
     MarshallPtr (he->h_addr_list, he, char *);
     MarshallPtr (qs->lpBlob->pBlobData, qs, void);
@@ -242,14 +194,16 @@ MarshallWSAQUERYSETW (WSAQUERYSETW *qs, GUID *sc)
 
 
 static void
-process_ip_lookup_result (void* cls, 
-                         uint32_t rd_count,
-                         const struct GNUNET_NAMESTORE_RecordData *rd)
+process_lookup_result (void *cls,
+                       uint32_t rd_count,
+                       const struct GNUNET_GNSRECORD_Data *rd)
 {
+  struct request *rq = cls;
   int i, j, csanum;
-  struct request *rq = (struct request *) cls;
   struct GNUNET_W32RESOLVER_GetMessage *msg;
+  struct GNUNET_MQ_Envelope *msg_env;
   struct GNUNET_MessageHeader *msgend;
+  struct GNUNET_MQ_Envelope *msgend_env;
   WSAQUERYSETW *qs;
   size_t size;
   size_t size_recalc;
@@ -259,15 +213,23 @@ process_ip_lookup_result (void* cls,
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Got lookup result with count %u for rq %p with client %p\n",
-              rd_count, rq, rq->client);
+              rd_count,
+              rq,
+              rq->client);
+  rq->lookup_request = NULL;
 
-  if (rd_count == 0)
+  if (0 == rd_count)
   {
-    size = sizeof (struct GNUNET_MessageHeader);
-    msg = GNUNET_malloc (size);
-    msg->header.size = htons (size);
-    msg->header.type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
-    transmit (rq->client, &msg->header);
+    msgend_env = GNUNET_MQ_msg (msgend, GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
+    GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (rq->client),
+                    msgend_env);
+    GNUNET_CONTAINER_DLL_remove (rq_head,
+                                 rq_tail,
+                                 rq);
+    GNUNET_free_non_null (rq->name);
+    if (rq->u8name)
+      free (rq->u8name);
+    GNUNET_free (rq);
     return;
   }
 
@@ -320,19 +282,16 @@ process_ip_lookup_result (void* cls,
     }
     size += blobsize;
   }
-  size += sizeof (struct GNUNET_MessageHeader);
   size_recalc = sizeof (struct GNUNET_W32RESOLVER_GetMessage) + sizeof (WSAQUERYSETW);
-  msg = GNUNET_malloc (size);
-  msg->header.size = htons (size - sizeof (struct GNUNET_MessageHeader));
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
+  msg_env = GNUNET_MQ_msg_extra (msg,
+                                 size - sizeof (struct GNUNET_MessageHeader),
+                                 GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
   msg->af = htonl (rq->af);
   msg->sc_data1 = htonl (rq->sc.Data1);
   msg->sc_data2 = htons (rq->sc.Data2);
   msg->sc_data3 = htons (rq->sc.Data3);
-  msg->sc_data4 = 0;
   for (i = 0; i < 8; i++)
-    msg->sc_data4 |= rq->sc.Data4[i] << ((7 - i) * 8);
-  msg->sc_data4 = GNUNET_htonll (msg->sc_data4);
+    msg->sc_data4[i] = rq->sc.Data4[i];
   qs = (WSAQUERYSETW *) &msg[1];
   ptr = (char *) &qs[1];
   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
@@ -346,14 +305,14 @@ process_ip_lookup_result (void* cls,
   ptr += sizeof (GUID);
   size_recalc += sizeof (GUID);
   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
-  memcpy (qs->lpServiceClassId, &rq->sc, sizeof (GUID));
+  GNUNET_memcpy (qs->lpServiceClassId, &rq->sc, sizeof (GUID));
   qs->lpVersion = NULL;
   qs->dwNameSpace = NS_DNS;
   qs->lpNSProviderId = (GUID *) ptr;
   ptr += sizeof (GUID);
   size_recalc += sizeof (GUID);
   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
-  memcpy (qs->lpNSProviderId, &GNUNET_NAMESPACE_PROVIDER_DNS, sizeof (GUID));
+  GNUNET_memcpy (qs->lpNSProviderId, &GNUNET_NAMESPACE_PROVIDER_DNS, sizeof (GUID));
   qs->lpszContext = NULL;
   qs->dwNumberOfProtocols = 0;
   qs->lpafpProtocols = NULL;
@@ -468,7 +427,7 @@ process_ip_lookup_result (void* cls,
         size_recalc += sizeof (struct in_addr);
         GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
 
-        memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in_addr));
+        GNUNET_memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in_addr));
         j++;
       }
       else if (rq->af == AF_INET6 && rd[i].record_type == GNUNET_DNSPARSER_TYPE_AAAA)
@@ -479,31 +438,42 @@ process_ip_lookup_result (void* cls,
         size_recalc += sizeof (struct in6_addr);
         GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
 
-        memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in6_addr));
+        GNUNET_memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in6_addr));
         j++;
       }
     }
     he->h_addr_list[j] = NULL;
   }
-  msgend = (struct GNUNET_MessageHeader *) ptr;
-  ptr += sizeof (struct GNUNET_MessageHeader);
-  size_recalc += sizeof (struct GNUNET_MessageHeader);
-
-  msgend->type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
-  msgend->size = htons (sizeof (struct GNUNET_MessageHeader));
+  msgend_env = GNUNET_MQ_msg (msgend, GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
 
   if ((char *) ptr - (char *) msg != size || size_recalc != size || size_recalc != ((char *) ptr - (char *) msg))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error in WSAQUERYSETW size calc: expected %lu, got %lu (recalc %lu)\n", size, (unsigned long) ((char *) ptr - (char *) msg), size_recalc);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Error in WSAQUERYSETW size calc: expected %u, got %lu (recalc %u)\n",
+                size,
+                (unsigned long) ((char *) ptr - (char *) msg),
+                size_recalc);
   }
   MarshallWSAQUERYSETW (qs, &rq->sc);
-  transmit (rq->client, &msg->header);
+  GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (rq->client),
+                  msg_env);
+  GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (rq->client),
+                  msgend_env);
+  GNUNET_CONTAINER_DLL_remove (rq_head,
+                               rq_tail,
+                               rq);
+  GNUNET_free_non_null (rq->name);
+  if (rq->u8name)
+    free (rq->u8name);
+  GNUNET_free (rq);
 }
 
 
 static void
-get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
-    const wchar_t *name, int af, GUID sc)
+get_ip_from_hostname (struct GNUNET_SERVICE_Client *client,
+                      const wchar_t *name,
+                      int af,
+                      GUID sc)
 {
   struct request *rq;
   char *hostname;
@@ -534,10 +504,19 @@ get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
   else
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Unknown GUID: %08X-%04X-%04X-%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
-                sc.Data1, sc.Data2, sc.Data3, sc.Data4[0], sc.Data4[1], sc.Data4[2],
-                sc.Data4[3], sc.Data4[4], sc.Data4[5], sc.Data4[6], sc.Data4[7]);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+                "Unknown GUID: %08lX-%04X-%04X-%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
+                sc.Data1,
+                sc.Data2,
+                sc.Data3,
+                sc.Data4[0],
+                sc.Data4[1],
+                sc.Data4[2],
+                sc.Data4[3],
+                sc.Data4[4],
+                sc.Data4[5],
+                sc.Data4[6],
+                sc.Data4[7]);
+    GNUNET_SERVICE_client_drop (client);
     return;
   }
 
@@ -547,12 +526,15 @@ get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
     namelen = 0;
   if (namelen > 0)
     hostname = (char *) u16_to_u8 (name, namelen + 1, NULL, &strl);
-  
+  else
+    hostname = NULL;
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "W32 DNS resolver asked to look up %s for `%s'.\n",
               af == AF_INET ? "IPv4" : af == AF_INET6 ? "IPv6" : "anything",
               hostname);
-  rq = GNUNET_malloc (sizeof (struct request));
+
+  rq = GNUNET_new (struct request);
   rq->sc = sc;
   rq->client = client;
   rq->af = af;
@@ -561,87 +543,168 @@ get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
   if (namelen)
   {
     rq->name = GNUNET_malloc ((namelen + 1) * sizeof (wchar_t));
-    memcpy (rq->name, name, (namelen + 1) * sizeof (wchar_t));
+    GNUNET_memcpy (rq->name,
+                   name,
+                   (namelen + 1) * sizeof (wchar_t));
     rq->u8name = hostname;
   }
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Launching a lookup for client %p with rq %p\n",
-              client, rq);
-
-  if (NULL != GNUNET_GNS_lookup (gns, hostname, zone, rtype,
-      GNUNET_YES, shorten_key, &process_ip_lookup_result, rq))
+              client,
+              rq);
+  rq->lookup_request = GNUNET_GNS_lookup (gns,
+                                          hostname,
+                                          &gns_master_pubkey,
+                                          rtype,
+                                          GNUNET_NO /* Use DHT */,
+                                          &process_lookup_result,
+                                          rq);
+  if (NULL != rq->lookup_request)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Lookup launched, waiting for a reply\n");
-    GNUNET_SERVER_receive_done (client, GNUNET_OK);
+    GNUNET_SERVICE_client_continue (client);
+    GNUNET_CONTAINER_DLL_insert (rq_head,
+                                 rq_tail,
+                                 rq);
   }
   else
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Lookup was not, disconnecting the client\n");
-    if (namelen)
-    {
-      GNUNET_free (rq->name);
+                "Lookup was not launched, disconnecting the client\n");
+    GNUNET_free_non_null (rq->name);
+    if (rq->u8name)
       free (rq->u8name);
-    }
     GNUNET_free (rq);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    GNUNET_SERVICE_client_drop (client);
+  }
+}
+
+
+/**
+ * Check GET-message.
+ *
+ * @param cls identification of the client
+ * @param msg the actual message
+ * @return #GNUNET_OK if @a msg is well-formed
+ */
+static int
+check_get (void *cls,
+            const struct GNUNET_W32RESOLVER_GetMessage *msg)
+{
+  uint16_t size;
+  const wchar_t *hostname;
+
+  if (! got_egos)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("Not ready to process requests, lacking ego data\n"));
+    return GNUNET_SYSERR;
   }
+  size = ntohs (msg->header.size) - sizeof (struct GNUNET_W32RESOLVER_GetMessage);
+  hostname = (const wchar_t *) &msg[1];
+  if (hostname[size / 2 - 1] != L'\0')
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  return GNUNET_OK;
 }
 
 
 /**
  * Handle GET-message.
  *
- * @param cls closure
- * @param client identification of the client
- * @param message the actual message
+ * @param cls identification of the client
+ * @param msg the actual message
  */
 static void
-handle_get (void *cls, struct GNUNET_SERVER_Client *client,
-            const struct GNUNET_MessageHeader *message)
+handle_get (void *cls,
+            const struct GNUNET_W32RESOLVER_GetMessage *msg)
 {
-  uint16_t msize;
-  const struct GNUNET_W32RESOLVER_GetMessage *msg;
+  struct GNUNET_SERVICE_Client *client = cls;
   GUID sc;
   uint16_t size;
-  uint64_t data4;
-  int i;
   const wchar_t *hostname;
   int af;
 
-  msize = ntohs (message->size);
-  if (msize < sizeof (struct GNUNET_W32RESOLVER_GetMessage))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-  msg = (const struct GNUNET_W32RESOLVER_GetMessage *) message;
-  size = msize - sizeof (struct GNUNET_W32RESOLVER_GetMessage);
+  size = ntohs (msg->header.size) - sizeof (struct GNUNET_W32RESOLVER_GetMessage);
   af = ntohl (msg->af);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got NBO GUID: %08X-%04X-%04X-%016llX\n",
-      msg->sc_data1, msg->sc_data2, msg->sc_data3, msg->sc_data4);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Got NBO GUID: %08X-%04X-%04X-%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
+              msg->sc_data1,
+              msg->sc_data2,
+              msg->sc_data3,
+              msg->sc_data4[0],
+              msg->sc_data4[1],
+              msg->sc_data4[2],
+              msg->sc_data4[3],
+              msg->sc_data4[4],
+              msg->sc_data4[5],
+              msg->sc_data4[6],
+              msg->sc_data4[7]);
   sc.Data1 = ntohl (msg->sc_data1);
   sc.Data2 = ntohs (msg->sc_data2);
   sc.Data3 = ntohs (msg->sc_data3);
-  data4 = GNUNET_ntohll (msg->sc_data4);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got GUID: %08X-%04X-%04X-%016llX\n",
-      sc.Data1, sc.Data2, sc.Data3, data4);
-  for (i = 0; i < 8; i++)
-    sc.Data4[i] = 0xFF & (data4 >> ((7 - i) * 8));
-  
+  for (int i = 0; i < 8; i++)
+    sc.Data4[i] = msg->sc_data4[i];
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Got GUID: %08lX-%04X-%04X-%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
+              sc.Data1,
+              sc.Data2,
+              sc.Data3,
+              sc.Data4[0],
+              sc.Data4[1],
+              sc.Data4[2],
+              sc.Data4[3],
+              sc.Data4[4],
+              sc.Data4[5],
+              sc.Data4[6],
+              sc.Data4[7]);
   hostname = (const wchar_t *) &msg[1];
-  if (hostname[size - 1] != L'\0')
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Name of %u bytes (last word is 0x%0X): %*S\n",
+              size,
+              hostname[size / 2 - 2],
+              size / 2,
+              hostname);
+  get_ip_from_hostname (client,
+                        hostname,
+                        af,
+                        sc);
+}
+
+
+/**
+ * Method called to with the ego we are to use for the lookup,
+ * when the ego is the one for the default master zone.
+ *
+ * @param cls closure (NULL, unused)
+ * @param ego ego handle, NULL if not found
+ * @param ctx context for application to store data for this ego
+ *                 (during the lifetime of this process, initially NULL)
+ * @param name name assigned by the user for this ego,
+ *                   NULL if the user just deleted the ego and it
+ *                   must thus no longer be used
+ */
+static void
+identity_master_cb (void *cls,
+                    struct GNUNET_IDENTITY_Ego *ego,
+                    void **ctx,
+                    const char *name)
+{
+  id_op = NULL;
+  if (NULL == ego)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "name of length %u, not 0-terminated: %*S\n",
-        size, size, hostname);
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("Ego for `gns-master' not found, cannot perform lookup.  Did you run gnunet-gns-import.sh?\n"));
+    GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  get_ip_from_hostname (client, hostname, af, sc);
+  GNUNET_IDENTITY_ego_get_public_key (ego,
+                                      &gns_master_pubkey);
+  got_egos = 1;
 }
 
 
@@ -649,92 +712,90 @@ handle_get (void *cls, struct GNUNET_SERVER_Client *client,
  * Start up gns-helper-w32 service.
  *
  * @param cls closure
- * @param server the initialized server
  * @param cfg configuration to use
+ * @param service the initialized service
  */
 static void
-run (void *cls, struct GNUNET_SERVER_Handle *server,
-     const struct GNUNET_CONFIGURATION_Handle *cfg)
+run (void *cls,
+     const struct GNUNET_CONFIGURATION_Handle *cfg,
+     struct GNUNET_SERVICE_Handle *service)
 {
-  static const struct GNUNET_SERVER_MessageHandler handlers[] = {
-    {&handle_get, NULL, GNUNET_MESSAGE_TYPE_W32RESOLVER_REQUEST, 0},
-    {NULL, NULL, 0, 0}
-  };
-
-  char* keyfile;
-  struct GNUNET_CRYPTO_EccPrivateKey *key = NULL;
-  struct GNUNET_CRYPTO_EccPublicKey pkey;
-  struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
-
-  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
-                                                           "ZONEKEY", &keyfile))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                "No private key for root zone found, using default!\n");
-    zone = NULL;
-  }
-  else
-  {
-    if (GNUNET_YES == GNUNET_DISK_file_test (keyfile))
-    {
-      key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
-      GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
-      GNUNET_CRYPTO_short_hash(&pkey,
-                         sizeof(struct GNUNET_CRYPTO_EccPublicKey),
-                         &user_zone);
-      GNUNET_CRYPTO_short_hash_to_enc (&user_zone, &zonename);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Using zone: %s!\n", &zonename);
-      GNUNET_CRYPTO_ecc_key_free(key);
-    }
-    GNUNET_free(keyfile);
-  }
-
-  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
-                                                   "SHORTEN_ZONEKEY", &keyfile))
+  gns = GNUNET_GNS_connect (cfg);
+  if (NULL == gns)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                "No shorten key found!\n");
-    shorten_key = NULL;
+    fprintf (stderr,
+             _("Failed to connect to GNS\n"));
+    GNUNET_SCHEDULER_shutdown ();
+    return;
   }
-  else
+  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
+                                NULL);
+  identity = GNUNET_IDENTITY_connect (cfg,
+                                      NULL,
+                                      NULL);
+  if (NULL == identity)
   {
-    if (GNUNET_YES == GNUNET_DISK_file_test (keyfile))
-    {
-      shorten_key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
-    }
-    GNUNET_free(keyfile);
-  }
-
-  gns = GNUNET_GNS_connect (cfg);
-  if (gns == NULL)
+    fprintf (stderr,
+             _("Failed to connect to identity service\n"));
+    GNUNET_SCHEDULER_shutdown ();
     return;
+  }
+  id_op = GNUNET_IDENTITY_get (identity,
+                               "gns-master",
+                               &identity_master_cb,
+                               NULL);
+  GNUNET_assert (NULL != id_op);
+}
 
-  GNUNET_SERVER_add_handlers (server, handlers);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
-                                NULL);
 
+/**
+ * Handle client connecting to the service.
+ *
+ * @param cls NULL
+ * @param client the new client
+ * @param mq the message queue of @a client
+ * @return @a client
+ */
+static void *
+client_connect_cb (void *cls,
+                   struct GNUNET_SERVICE_Client *client,
+                   struct GNUNET_MQ_Handle *mq)
+{
+  return client;
 }
 
 
 /**
- * The main function for gns-helper-w32.
+ * Callback called when a client disconnected from the service
  *
- * @param argc number of arguments from the command line
- * @param argv command line arguments
- * @return 0 ok, 1 on error
+ * @param cls closure for the service
+ * @param c the client that disconnected
+ * @param internal_cls should be equal to @a c
  */
-int
-main (int argc, char *const *argv)
+static void
+client_disconnect_cb (void *cls,
+                      struct GNUNET_SERVICE_Client *client,
+                      void *internal_cls)
 {
-  int ret;
+  GNUNET_assert (internal_cls == client);
+}
 
-  ret =
-      (GNUNET_OK ==
-       GNUNET_SERVICE_run (argc, argv, "gns-helper-service-w32", GNUNET_SERVICE_OPTION_NONE,
-                           &run, NULL)) ? 0 : 1;
 
-  return ret;
-}
+/**
+ * Define "main" method using service macro.
+ */
+GNUNET_SERVICE_MAIN
+("gns-helper-service-w32",
+ GNUNET_SERVICE_OPTION_NONE,
+ &run,
+ &client_connect_cb,
+ &client_disconnect_cb,
+ NULL,
+ GNUNET_MQ_hd_var_size (get,
+                        GNUNET_MESSAGE_TYPE_W32RESOLVER_REQUEST,
+                        struct GNUNET_W32RESOLVER_GetMessage,
+                        NULL),
+ GNUNET_MQ_handler_end());
+
 
 /* end of gnunet-gns-helper-service-w32.c */