9a17198affcae6337f0e3c695dc45a8b06f1a3a6
[oweals/gnunet.git] / src / gns / gnunet-gns-helper-service-w32.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file gnunet-gns-helper-service-w32.c
22  * @brief an intermediary service to access distributed GNS
23  * @author Christian Grothoff
24  * @author LRN
25  */
26 #define INITGUID
27 #include "platform.h"
28 #include <gnunet_util_lib.h>
29 #include <gnunet_identity_service.h>
30 #include <gnunet_dnsparser_lib.h>
31 #include <gnunet_namestore_service.h>
32 #include <gnunet_gns_service.h>
33 #include "gnunet_w32nsp_lib.h"
34 #include "w32resolver.h"
35 #include <nspapi.h>
36 #include <unistr.h>
37
38 #define DEFINE_DNS_GUID(a,x) DEFINE_GUID(a, 0x00090035, 0x0000, x, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46)
39 DEFINE_DNS_GUID(SVCID_DNS_TYPE_A, 0x0001);
40 DEFINE_DNS_GUID(SVCID_DNS_TYPE_NS, 0x0002);
41 DEFINE_DNS_GUID(SVCID_DNS_TYPE_CNAME, 0x0005);
42 DEFINE_DNS_GUID(SVCID_DNS_TYPE_SOA, 0x0006);
43 DEFINE_DNS_GUID(SVCID_DNS_TYPE_PTR, 0x000c);
44 DEFINE_DNS_GUID(SVCID_DNS_TYPE_MX, 0x000f);
45 DEFINE_DNS_GUID(SVCID_DNS_TYPE_TEXT, 0x0010);
46 DEFINE_DNS_GUID(SVCID_DNS_TYPE_AAAA, 0x001c);
47 DEFINE_DNS_GUID(SVCID_DNS_TYPE_SRV, 0x0021);
48 DEFINE_GUID(SVCID_HOSTNAME, 0x0002a800, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
49 DEFINE_GUID(SVCID_INET_HOSTADDRBYNAME, 0x0002a803, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
50
51 struct request
52 {
53   struct GNUNET_SERVER_Client *client;
54   GUID sc;
55   int af;
56   wchar_t *name;
57   char *u8name;
58   struct GNUNET_GNS_LookupRequest *lookup_request;
59 };
60
61 /**
62  * Handle to GNS service.
63  */
64 static struct GNUNET_GNS_Handle *gns;
65
66 /**
67  * Active operation on identity service.
68  */
69 static struct GNUNET_IDENTITY_Operation *id_op;
70
71 /**
72  * Handle for identity service.
73  */
74 static struct GNUNET_IDENTITY_Handle *identity;
75
76 /**
77  * Public key of the gns-master ego
78  */
79 static struct GNUNET_CRYPTO_EcdsaPublicKey gns_master_pubkey;
80
81 /**
82  * Private key of the gns-short ego
83  */
84 static struct GNUNET_CRYPTO_EcdsaPrivateKey gns_short_privkey;
85
86 /**
87  * Set to 1 once egos are obtained.
88  */
89 static int got_egos = 0;
90
91 /**
92  * Task run on shutdown.  Cleans up everything.
93  *
94  * @param cls unused
95  * @param tc scheduler context
96  */
97 static void
98 do_shutdown (void *cls,
99              const struct GNUNET_SCHEDULER_TaskContext *tc)
100 {
101   if (NULL != id_op)
102   {
103     GNUNET_IDENTITY_cancel (id_op);
104     id_op = NULL;
105   }
106   if (NULL != identity)
107   {
108     GNUNET_IDENTITY_disconnect (identity);
109     identity = NULL;
110   }
111   if (NULL != gns)
112   {
113     GNUNET_GNS_disconnect (gns);
114     gns = NULL;
115   }
116 }
117
118 /**
119  * Context for transmitting replies to clients.
120  */
121 struct TransmitCallbackContext
122 {
123
124   /**
125    * We keep these in a doubly-linked list (for cleanup).
126    */
127   struct TransmitCallbackContext *next;
128
129   /**
130    * We keep these in a doubly-linked list (for cleanup).
131    */
132   struct TransmitCallbackContext *prev;
133
134   /**
135    * The message that we're asked to transmit.
136    */
137   struct GNUNET_MessageHeader *msg;
138
139   /**
140    * Handle for the transmission request.
141    */
142   struct GNUNET_SERVER_TransmitHandle *th;
143
144 };
145
146
147 /**
148  * Head of the doubly-linked list (for cleanup).
149  */
150 static struct TransmitCallbackContext *tcc_head;
151
152 /**
153  * Tail of the doubly-linked list (for cleanup).
154  */
155 static struct TransmitCallbackContext *tcc_tail;
156
157 /**
158  * Have we already cleaned up the TCCs and are hence no longer
159  * willing (or able) to transmit anything to anyone?
160  */
161 static int cleaning_done;
162
163
164 /**
165  * Function called to notify a client about the socket
166  * being ready to queue more data.  "buf" will be
167  * NULL and "size" zero if the socket was closed for
168  * writing in the meantime.
169  *
170  * @param cls closure
171  * @param size number of bytes available in buf
172  * @param buf where the callee should write the message
173  * @return number of bytes written to buf
174  */
175 static size_t
176 transmit_callback (void *cls, size_t size, void *buf)
177 {
178   struct TransmitCallbackContext *tcc = cls;
179   size_t msize;
180
181   tcc->th = NULL;
182   GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
183   msize = ntohs (tcc->msg->size);
184   if (size == 0)
185   {
186     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
187                 _("Transmission to client failed!\n"));
188     GNUNET_free (tcc->msg);
189     GNUNET_free (tcc);
190     return 0;
191   }
192   GNUNET_assert (size >= msize);
193   memcpy (buf, tcc->msg, msize);
194   GNUNET_free (tcc->msg);
195   GNUNET_free (tcc);
196   return msize;
197 }
198
199
200 /**
201  * Transmit the given message to the client.
202  *
203  * @param client target of the message
204  * @param msg message to transmit, will be freed!
205  */
206 static void
207 transmit (struct GNUNET_SERVER_Client *client,
208           struct GNUNET_MessageHeader *msg)
209 {
210   struct TransmitCallbackContext *tcc;
211
212   if (GNUNET_YES == cleaning_done)
213   {
214     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
215                 _("Shutdown in progress, aborting transmission.\n"));
216     GNUNET_free (msg);
217     return;
218   }
219   tcc = GNUNET_new (struct TransmitCallbackContext);
220   tcc->msg = msg;
221   if (NULL ==
222       (tcc->th =
223        GNUNET_SERVER_notify_transmit_ready (client,
224                                             ntohs (msg->size),
225                                             GNUNET_TIME_UNIT_FOREVER_REL,
226                                             &transmit_callback, tcc)))
227   {
228     GNUNET_break (0);
229     GNUNET_free (msg);
230     GNUNET_free (tcc);
231     return;
232   }
233   GNUNET_CONTAINER_DLL_insert (tcc_head, tcc_tail, tcc);
234 }
235
236
237 #define MarshallPtr(ptr, base, type) \
238   if (ptr) \
239     ptr = (type *) ((char *) ptr - (char *) base)
240
241
242 void
243 MarshallWSAQUERYSETW (WSAQUERYSETW *qs, GUID *sc)
244 {
245   int i;
246   MarshallPtr (qs->lpszServiceInstanceName, qs, wchar_t);
247   MarshallPtr (qs->lpServiceClassId, qs, GUID);
248   MarshallPtr (qs->lpVersion, qs, WSAVERSION);
249   MarshallPtr (qs->lpNSProviderId, qs, GUID);
250   MarshallPtr (qs->lpszContext, qs, wchar_t);
251   MarshallPtr (qs->lpafpProtocols, qs, AFPROTOCOLS);
252   MarshallPtr (qs->lpszQueryString, qs, wchar_t);
253   for (i = 0; i < qs->dwNumberOfCsAddrs; i++)
254   {
255     MarshallPtr (qs->lpcsaBuffer[i].LocalAddr.lpSockaddr, qs, SOCKADDR);
256     MarshallPtr (qs->lpcsaBuffer[i].RemoteAddr.lpSockaddr, qs, SOCKADDR);
257   }
258   MarshallPtr (qs->lpcsaBuffer, qs, CSADDR_INFO);
259   if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, sc) && qs->lpBlob != NULL && qs->lpBlob->pBlobData != NULL)
260   {
261     struct hostent *he;
262     he = (struct hostent *) qs->lpBlob->pBlobData;
263     for (i = 0; he->h_aliases[i] != NULL; i++)
264       MarshallPtr (he->h_aliases[i], he, char);
265     MarshallPtr (he->h_aliases, he, char *);
266     MarshallPtr (he->h_name, he, char);
267     for (i = 0; he->h_addr_list[i] != NULL; i++)
268       MarshallPtr (he->h_addr_list[i], he, void);
269     MarshallPtr (he->h_addr_list, he, char *);
270     MarshallPtr (qs->lpBlob->pBlobData, qs, void);
271   }
272   MarshallPtr (qs->lpBlob, qs, BLOB);
273 }
274
275
276 static void
277 process_lookup_result (void* cls, uint32_t rd_count,
278     const struct GNUNET_GNSRECORD_Data *rd)
279 {
280   int i, j, csanum;
281   struct request *rq = (struct request *) cls;
282   struct GNUNET_W32RESOLVER_GetMessage *msg;
283   struct GNUNET_MessageHeader *msgend;
284   WSAQUERYSETW *qs;
285   size_t size;
286   size_t size_recalc;
287   char *ptr;
288   size_t blobsize = 0;
289   size_t blobaddrcount = 0;
290
291   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292       "Got lookup result with count %u for rq %p with client %p\n",
293       rd_count, rq, rq->client);
294
295   if (rd_count == 0)
296   {
297     size = sizeof (struct GNUNET_MessageHeader);
298     msg = GNUNET_malloc (size);
299     msg->header.size = htons (size);
300     msg->header.type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
301     transmit (rq->client, &msg->header);
302     GNUNET_free_non_null (rq->name);
303     if (rq->u8name)
304       free (rq->u8name);
305     GNUNET_free (rq);
306     return;
307   }
308
309   size = sizeof (struct GNUNET_W32RESOLVER_GetMessage) + sizeof (WSAQUERYSETW);
310   size += (wcslen (rq->name) + 1) * sizeof (wchar_t);
311   size += sizeof (GUID);
312   /* lpszComment ? a TXT record? */
313   size += sizeof (GUID);
314   /* lpszContext ? Not sure what it is */
315   csanum = 0;
316   for (i = 0; i < rd_count; i++)
317   {
318     switch (rd[i].record_type)
319     {
320     case GNUNET_DNSPARSER_TYPE_A:
321       if (rd[i].data_size != sizeof (struct in_addr))
322         continue;
323       size += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in) * 2;
324       csanum++;
325       break;
326     case GNUNET_DNSPARSER_TYPE_AAAA:
327       if (rd[i].data_size != sizeof (struct in6_addr))
328         continue;
329       size += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in6) * 2;
330       csanum++;
331       break;
332     }
333   }
334   if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &rq->sc))
335   {
336     size += sizeof (BLOB);
337     blobsize += sizeof (struct hostent);
338     blobsize += strlen (rq->u8name) + 1;
339     blobsize += sizeof (void *); /* For aliases */
340     blobsize += sizeof (void *); /* For addresses */
341     for (i = 0; i < rd_count; i++)
342     {
343       if ((rq->af == AF_INET || rq->af == AF_UNSPEC) && rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
344       {
345         blobsize += sizeof (void *);
346         blobsize += sizeof (struct in_addr);
347         blobaddrcount++;
348       }
349       else if (rq->af == AF_INET6 && rd[i].record_type == GNUNET_DNSPARSER_TYPE_AAAA)
350       {
351         blobsize += sizeof (void *);
352         blobsize += sizeof (struct in6_addr);
353         blobaddrcount++;
354       }
355     }
356     size += blobsize;
357   }
358   size += sizeof (struct GNUNET_MessageHeader);
359   size_recalc = sizeof (struct GNUNET_W32RESOLVER_GetMessage) + sizeof (WSAQUERYSETW);
360   msg = GNUNET_malloc (size);
361   msg->header.size = htons (size - sizeof (struct GNUNET_MessageHeader));
362   msg->header.type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
363   msg->af = htonl (rq->af);
364   msg->sc_data1 = htonl (rq->sc.Data1);
365   msg->sc_data2 = htons (rq->sc.Data2);
366   msg->sc_data3 = htons (rq->sc.Data3);
367   for (i = 0; i < 8; i++)
368     msg->sc_data4[i] = rq->sc.Data4[i];
369   qs = (WSAQUERYSETW *) &msg[1];
370   ptr = (char *) &qs[1];
371   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
372   qs->dwSize = sizeof (WSAQUERYSETW);
373   qs->lpszServiceInstanceName = (wchar_t *) ptr;
374   ptr += (wcslen (rq->name) + 1) * sizeof (wchar_t);
375   size_recalc += (wcslen (rq->name) + 1) * sizeof (wchar_t);
376   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
377   wcscpy (qs->lpszServiceInstanceName, rq->name);
378   qs->lpServiceClassId = (GUID *) ptr;
379   ptr += sizeof (GUID);
380   size_recalc += sizeof (GUID);
381   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
382   memcpy (qs->lpServiceClassId, &rq->sc, sizeof (GUID));
383   qs->lpVersion = NULL;
384   qs->dwNameSpace = NS_DNS;
385   qs->lpNSProviderId = (GUID *) ptr;
386   ptr += sizeof (GUID);
387   size_recalc += sizeof (GUID);
388   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
389   memcpy (qs->lpNSProviderId, &GNUNET_NAMESPACE_PROVIDER_DNS, sizeof (GUID));
390   qs->lpszContext = NULL;
391   qs->dwNumberOfProtocols = 0;
392   qs->lpafpProtocols = NULL;
393   /* Don't bother with this... */
394   qs->lpszQueryString = NULL;
395   qs->dwNumberOfCsAddrs = rd_count;
396   qs->lpcsaBuffer = (CSADDR_INFO *) ptr;
397   ptr += sizeof (CSADDR_INFO) * csanum;
398   j = 0;
399   for (i = 0; i < rd_count; i++)
400   {
401     switch (rd[i].record_type)
402     {
403     case GNUNET_DNSPARSER_TYPE_A:
404       if (rd[i].data_size != sizeof (struct in_addr))
405         continue;
406       qs->lpcsaBuffer[j].iSocketType = SOCK_STREAM;
407       qs->lpcsaBuffer[j].iProtocol = IPPROTO_TCP;
408
409       qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength = sizeof (struct sockaddr_in);
410       qs->lpcsaBuffer[j].LocalAddr.lpSockaddr = (SOCKADDR *) ptr;
411       ptr += qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength;
412       memset (qs->lpcsaBuffer[j].LocalAddr.lpSockaddr, 0, qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength);
413       ((struct sockaddr_in *)qs->lpcsaBuffer[j].LocalAddr.lpSockaddr)->sin_family = AF_INET;
414
415       qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength = sizeof (struct sockaddr_in);
416       qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr = (SOCKADDR *) ptr;
417       ptr += qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength;
418       memset (qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr, 0, qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength);
419       ((struct sockaddr_in *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin_family = AF_INET;
420       ((struct sockaddr_in *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin_port = htonl (53); /* Don't ask why it's 53 */
421       ((struct sockaddr_in *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin_addr = *(struct in_addr *) rd[i].data;
422       size_recalc += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in) * 2;
423       j++;
424       break;
425     case GNUNET_DNSPARSER_TYPE_AAAA:
426       if (rd[i].data_size != sizeof (struct in6_addr))
427         continue;
428       qs->lpcsaBuffer[j].iSocketType = SOCK_STREAM;
429       qs->lpcsaBuffer[j].iProtocol = IPPROTO_TCP;
430
431       qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength = sizeof (struct sockaddr_in6);
432       qs->lpcsaBuffer[j].LocalAddr.lpSockaddr = (SOCKADDR *) ptr;
433       ptr += qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength;
434       memset (qs->lpcsaBuffer[j].LocalAddr.lpSockaddr, 0, qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength);
435       ((struct sockaddr_in6 *)qs->lpcsaBuffer[j].LocalAddr.lpSockaddr)->sin6_family = AF_INET6;
436
437       qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength = sizeof (struct sockaddr_in6);
438       qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr = (SOCKADDR *) ptr;
439       ptr += qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength;
440       memset (qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr, 0, qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength);
441       ((struct sockaddr_in6 *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin6_family = AF_INET6;
442       ((struct sockaddr_in6 *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin6_port = htonl (53); /* Don't ask why it's 53 */
443       ((struct sockaddr_in6 *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin6_addr = *(struct in6_addr *) rd[i].data;
444       size_recalc += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in6) * 2;
445       j++;
446       break;
447     default:
448       break;
449     }
450   }
451   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
452   qs->dwOutputFlags = 0;
453   if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &rq->sc))
454   {
455     struct hostent *he;
456     qs->lpBlob = (BLOB *) ptr;
457     ptr += sizeof (BLOB);
458
459     size_recalc += sizeof (BLOB);
460     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
461
462     qs->lpBlob->cbSize = blobsize;
463     qs->lpBlob->pBlobData = (BYTE *) ptr;
464     ptr += sizeof (struct hostent);
465
466     size_recalc += sizeof (struct hostent);
467     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
468
469     he = (struct hostent *) qs->lpBlob->pBlobData;
470     he->h_name = (char *) ptr;
471     ptr += strlen (rq->u8name) + 1;
472
473     size_recalc += strlen (rq->u8name) + 1;
474     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
475
476     strcpy (he->h_name, rq->u8name);
477     he->h_aliases = (char **) ptr;
478     ptr += sizeof (void *);
479
480     size_recalc += sizeof (void *); /* For aliases */
481     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
482
483     he->h_aliases[0] = NULL;
484     he->h_addrtype = rq->af;
485     he->h_length = rq->af == AF_INET || rq->af == AF_UNSPEC ? sizeof (struct in_addr) : sizeof (struct in6_addr);
486     he->h_addr_list = (char **) ptr;
487     ptr += sizeof (void *) * (blobaddrcount + 1);
488
489     size_recalc += sizeof (void *) * (blobaddrcount + 1); /* For addresses */
490     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
491
492     j = 0;
493     for (i = 0; i < rd_count; i++)
494     {
495       if ((rq->af == AF_INET || rq->af == AF_UNSPEC) &&
496           rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
497       {
498         he->h_addr_list[j] = (char *) ptr;
499         ptr += sizeof (struct in_addr);
500
501         size_recalc += sizeof (struct in_addr);
502         GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
503
504         memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in_addr));
505         j++;
506       }
507       else if (rq->af == AF_INET6 && rd[i].record_type == GNUNET_DNSPARSER_TYPE_AAAA)
508       {
509         he->h_addr_list[j] = (char *) ptr;
510         ptr += sizeof (struct in6_addr);
511
512         size_recalc += sizeof (struct in6_addr);
513         GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
514
515         memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in6_addr));
516         j++;
517       }
518     }
519     he->h_addr_list[j] = NULL;
520   }
521   msgend = (struct GNUNET_MessageHeader *) ptr;
522   ptr += sizeof (struct GNUNET_MessageHeader);
523   size_recalc += sizeof (struct GNUNET_MessageHeader);
524
525   msgend->type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
526   msgend->size = htons (sizeof (struct GNUNET_MessageHeader));
527
528   if ((char *) ptr - (char *) msg != size || size_recalc != size || size_recalc != ((char *) ptr - (char *) msg))
529   {
530     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);
531   }
532   MarshallWSAQUERYSETW (qs, &rq->sc);
533   transmit (rq->client, &msg->header);
534   GNUNET_free_non_null (rq->name);
535   if (rq->u8name)
536     free (rq->u8name);
537   GNUNET_free (rq);
538 }
539
540
541 static void
542 get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
543     const wchar_t *name, int af, GUID sc)
544 {
545   struct request *rq;
546   char *hostname;
547   size_t strl;
548   size_t namelen;
549   uint32_t rtype;
550
551   if (IsEqualGUID (&SVCID_DNS_TYPE_A, &sc))
552     rtype = GNUNET_DNSPARSER_TYPE_A;
553   else if (IsEqualGUID (&SVCID_DNS_TYPE_NS, &sc))
554     rtype = GNUNET_DNSPARSER_TYPE_NS;
555   else if (IsEqualGUID (&SVCID_DNS_TYPE_CNAME, &sc))
556     rtype = GNUNET_DNSPARSER_TYPE_CNAME;
557   else if (IsEqualGUID (&SVCID_DNS_TYPE_SOA, &sc))
558     rtype = GNUNET_DNSPARSER_TYPE_SOA;
559   else if (IsEqualGUID (&SVCID_DNS_TYPE_PTR, &sc))
560     rtype = GNUNET_DNSPARSER_TYPE_PTR;
561   else if (IsEqualGUID (&SVCID_DNS_TYPE_MX, &sc))
562     rtype = GNUNET_DNSPARSER_TYPE_MX;
563   else if (IsEqualGUID (&SVCID_DNS_TYPE_TEXT, &sc))
564     rtype = GNUNET_DNSPARSER_TYPE_TXT;
565   else if (IsEqualGUID (&SVCID_DNS_TYPE_AAAA, &sc))
566     rtype = GNUNET_DNSPARSER_TYPE_AAAA;
567   else if (IsEqualGUID (&SVCID_DNS_TYPE_SRV, &sc))
568     rtype = GNUNET_DNSPARSER_TYPE_SRV;
569   else if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &sc))
570     rtype = GNUNET_DNSPARSER_TYPE_A;
571   else
572   {
573     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574                 "Unknown GUID: %08X-%04X-%04X-%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
575                 sc.Data1, sc.Data2, sc.Data3, sc.Data4[0], sc.Data4[1], sc.Data4[2],
576                 sc.Data4[3], sc.Data4[4], sc.Data4[5], sc.Data4[6], sc.Data4[7]);
577     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
578     return;
579   }
580
581   if (name)
582     namelen = wcslen (name);
583   else
584     namelen = 0;
585   if (namelen > 0)
586     hostname = (char *) u16_to_u8 (name, namelen + 1, NULL, &strl);
587   else
588     hostname = NULL;
589
590   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591               "W32 DNS resolver asked to look up %s for `%s'.\n",
592               af == AF_INET ? "IPv4" : af == AF_INET6 ? "IPv6" : "anything",
593               hostname);
594
595   rq = GNUNET_new (struct request);
596   rq->sc = sc;
597   rq->client = client;
598   rq->af = af;
599   if (rq->af != AF_INET && rq->af != AF_INET6)
600     rq->af = AF_INET;
601   if (namelen)
602   {
603     rq->name = GNUNET_malloc ((namelen + 1) * sizeof (wchar_t));
604     memcpy (rq->name, name, (namelen + 1) * sizeof (wchar_t));
605     rq->u8name = hostname;
606   }
607
608   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
609               "Launching a lookup for client %p with rq %p\n",
610               client, rq);
611
612   rq->lookup_request = GNUNET_GNS_lookup (gns, hostname, &gns_master_pubkey,
613       rtype, GNUNET_NO /* Use DHT */, &gns_short_privkey, &process_lookup_result,
614       rq);
615
616   if (NULL != rq->lookup_request)
617   {
618     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619                 "Lookup launched, waiting for a reply\n");
620     GNUNET_SERVER_receive_done (client, GNUNET_OK);
621   }
622   else
623   {
624     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625                 "Lookup was not launched, disconnecting the client\n");
626     GNUNET_free_non_null (rq->name);
627     if (rq->u8name)
628       free (rq->u8name);
629     GNUNET_free (rq);
630     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
631   }
632 }
633
634
635 /**
636  * Handle GET-message.
637  *
638  * @param cls closure
639  * @param client identification of the client
640  * @param message the actual message
641  */
642 static void
643 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
644             const struct GNUNET_MessageHeader *message)
645 {
646   uint16_t msize;
647   const struct GNUNET_W32RESOLVER_GetMessage *msg;
648   GUID sc;
649   uint16_t size;
650   int i;
651   const wchar_t *hostname;
652   int af;
653
654   if (!got_egos)
655   {
656     /*
657      * FIXME: be done with GNUNET_OK, put the get request into a queue?
658      * or postpone GNUNET_SERVER_add_handlers() until egos are obtained?
659      */
660     GNUNET_SERVER_receive_done (client, GNUNET_NO);
661     return;
662   }
663
664   msize = ntohs (message->size);
665   if (msize < sizeof (struct GNUNET_W32RESOLVER_GetMessage))
666   {
667     GNUNET_break (0);
668     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
669     return;
670   }
671   msg = (const struct GNUNET_W32RESOLVER_GetMessage *) message;
672   size = msize - sizeof (struct GNUNET_W32RESOLVER_GetMessage);
673   af = ntohl (msg->af);
674   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675       "Got NBO GUID: %08X-%04X-%04X-%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
676       msg->sc_data1, msg->sc_data2, msg->sc_data3, msg->sc_data4[0], msg->sc_data4[1],
677       msg->sc_data4[2], msg->sc_data4[3], msg->sc_data4[4], msg->sc_data4[5],
678       msg->sc_data4[6], msg->sc_data4[7]);
679   sc.Data1 = ntohl (msg->sc_data1);
680   sc.Data2 = ntohs (msg->sc_data2);
681   sc.Data3 = ntohs (msg->sc_data3);
682   for (i = 0; i < 8; i++)
683     sc.Data4[i] = msg->sc_data4[i];
684   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685               "Got GUID: %08X-%04X-%04X-%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
686               sc.Data1, sc.Data2, sc.Data3, sc.Data4[0], sc.Data4[1], sc.Data4[2],
687               sc.Data4[3], sc.Data4[4], sc.Data4[5], sc.Data4[6], sc.Data4[7]);
688
689   hostname = (const wchar_t *) &msg[1];
690   if (hostname[size - 1] != L'\0')
691   {
692     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "name of length %u, not 0-terminated: %*S\n",
693         size, size, hostname);
694     GNUNET_break (0);
695     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
696     return;
697   }
698   get_ip_from_hostname (client, hostname, af, sc);
699 }
700
701
702 /**
703  * Method called to with the ego we are to use for shortening
704  * during the lookup.
705  *
706  * @param cls closure (NULL, unused)
707  * @param ego ego handle, NULL if not found
708  * @param ctx context for application to store data for this ego
709  *                 (during the lifetime of this process, initially NULL)
710  * @param name name assigned by the user for this ego,
711  *                   NULL if the user just deleted the ego and it
712  *                   must thus no longer be used
713  */
714 static void
715 identity_shorten_cb (void *cls,
716          struct GNUNET_IDENTITY_Ego *ego,
717          void **ctx,
718          const char *name)
719 {
720   id_op = NULL;
721   if (NULL == ego)
722   {
723     fprintf (stderr,
724        _("Ego for `gns-short' not found. This is not really fatal, but i'll pretend that it is and refuse to perform a lookup.  Did you run gnunet-gns-import.sh?\n"));
725     GNUNET_SCHEDULER_shutdown ();
726     return;
727   }
728   gns_short_privkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
729   got_egos = 1;
730 }
731
732 /**
733  * Method called to with the ego we are to use for the lookup,
734  * when the ego is the one for the default master zone.
735  *
736  * @param cls closure (NULL, unused)
737  * @param ego ego handle, NULL if not found
738  * @param ctx context for application to store data for this ego
739  *                 (during the lifetime of this process, initially NULL)
740  * @param name name assigned by the user for this ego,
741  *                   NULL if the user just deleted the ego and it
742  *                   must thus no longer be used
743  */
744 static void
745 identity_master_cb (void *cls,
746         struct GNUNET_IDENTITY_Ego *ego,
747         void **ctx,
748         const char *name)
749 {
750   id_op = NULL;
751   if (NULL == ego)
752   {
753     fprintf (stderr,
754        _("Ego for `gns-master' not found, cannot perform lookup.  Did you run gnunet-gns-import.sh?\n"));
755     GNUNET_SCHEDULER_shutdown ();
756     return;
757   }
758   GNUNET_IDENTITY_ego_get_public_key (ego, &gns_master_pubkey);
759   id_op = GNUNET_IDENTITY_get (identity, "gns-short", &identity_shorten_cb,
760       NULL);
761   GNUNET_assert (NULL != id_op);
762 }
763
764
765 /**
766  * Start up gns-helper-w32 service.
767  *
768  * @param cls closure
769  * @param server the initialized server
770  * @param cfg configuration to use
771  */
772 static void
773 run (void *cls, struct GNUNET_SERVER_Handle *server,
774     const struct GNUNET_CONFIGURATION_Handle *cfg)
775 {
776   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
777     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_W32RESOLVER_REQUEST, 0},
778     {NULL, NULL, 0, 0}
779   };
780
781   gns = GNUNET_GNS_connect (cfg);
782   if (NULL == gns)
783   {
784     fprintf (stderr, _("Failed to connect to GNS\n"));
785     GNUNET_SCHEDULER_shutdown ();
786     return;
787   }
788   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
789       NULL);
790
791   identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
792   if (NULL == identity)
793   {
794     fprintf (stderr, _("Failed to connect to identity service\n"));
795     GNUNET_SCHEDULER_shutdown ();
796     return;
797   }
798
799   id_op = GNUNET_IDENTITY_get (identity, "gns-master", &identity_master_cb,
800       NULL);
801   GNUNET_assert (NULL != id_op);
802
803   GNUNET_SERVER_add_handlers (server, handlers);
804 }
805
806
807 /**
808  * The main function for gns-helper-w32.
809  *
810  * @param argc number of arguments from the command line
811  * @param argv command line arguments
812  * @return 0 ok, 1 on error
813  */
814 int
815 main (int argc, char *const *argv)
816 {
817   int ret;
818
819   ret = GNUNET_SERVICE_run (argc, argv, "gns-helper-service-w32",
820       GNUNET_SERVICE_OPTION_NONE, &run, NULL);
821   return (GNUNET_OK == ret) ? 0 : 1;
822 }
823
824 /* end of gnunet-gns-helper-service-w32.c */