b27387e1aaf21d3d7ac91bca847d0699ab0021a6
[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    * Handle for the client to which to send
147    */
148   struct GNUNET_SERVER_Client *client;
149 };
150
151
152 /**
153  * Head of the doubly-linked list (for cleanup).
154  */
155 static struct TransmitCallbackContext *tcc_head;
156
157 /**
158  * Tail of the doubly-linked list (for cleanup).
159  */
160 static struct TransmitCallbackContext *tcc_tail;
161
162 /**
163  * Have we already cleaned up the TCCs and are hence no longer
164  * willing (or able) to transmit anything to anyone?
165  */
166 static int cleaning_done;
167
168
169 /**
170  * Function called to notify a client about the socket
171  * being ready to queue more data.  "buf" will be
172  * NULL and "size" zero if the socket was closed for
173  * writing in the meantime.
174  *
175  * @param cls closure
176  * @param size number of bytes available in buf
177  * @param buf where the callee should write the message
178  * @return number of bytes written to buf
179  */
180 static size_t
181 transmit_callback (void *cls, size_t size, void *buf)
182 {
183   struct TransmitCallbackContext *tcc = cls;
184   size_t msize;
185
186   tcc->th = NULL;
187   GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
188   msize = ntohs (tcc->msg->size);
189   if (size == 0)
190   {
191     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
192                 _("Transmission to client failed!\n"));
193     GNUNET_free (tcc->msg);
194     GNUNET_free (tcc);
195     return 0;
196   }
197   GNUNET_assert (size >= msize);
198   memcpy (buf, tcc->msg, msize);
199   GNUNET_free (tcc->msg);
200   GNUNET_free (tcc);
201   for (tcc = tcc_head; tcc; tcc = tcc->next)
202   {
203     if (NULL == tcc->th)
204     {
205       tcc->th = GNUNET_SERVER_notify_transmit_ready (tcc->client,
206           ntohs (tcc->msg->size),
207           GNUNET_TIME_UNIT_FOREVER_REL,
208           &transmit_callback, tcc);
209       break;
210     }
211   }
212   return msize;
213 }
214
215
216 /**
217  * Transmit the given message to the client.
218  *
219  * @param client target of the message
220  * @param msg message to transmit, will be freed!
221  */
222 static void
223 transmit (struct GNUNET_SERVER_Client *client,
224           struct GNUNET_MessageHeader *msg)
225 {
226   struct TransmitCallbackContext *tcc;
227
228   if (GNUNET_YES == cleaning_done)
229   {
230     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
231                 _("Shutdown in progress, aborting transmission.\n"));
232     GNUNET_free (msg);
233     return;
234   }
235   tcc = GNUNET_new (struct TransmitCallbackContext);
236   tcc->msg = msg;
237   tcc->client = client;
238   tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
239       ntohs (msg->size),
240       GNUNET_TIME_UNIT_FOREVER_REL,
241       &transmit_callback, tcc);
242   GNUNET_CONTAINER_DLL_insert (tcc_head, tcc_tail, tcc);
243 }
244
245
246 #define MarshallPtr(ptr, base, type) \
247   if (ptr) \
248     ptr = (type *) ((char *) ptr - (char *) base)
249
250
251 void
252 MarshallWSAQUERYSETW (WSAQUERYSETW *qs, GUID *sc)
253 {
254   int i;
255   MarshallPtr (qs->lpszServiceInstanceName, qs, wchar_t);
256   MarshallPtr (qs->lpServiceClassId, qs, GUID);
257   MarshallPtr (qs->lpVersion, qs, WSAVERSION);
258   MarshallPtr (qs->lpNSProviderId, qs, GUID);
259   MarshallPtr (qs->lpszContext, qs, wchar_t);
260   MarshallPtr (qs->lpafpProtocols, qs, AFPROTOCOLS);
261   MarshallPtr (qs->lpszQueryString, qs, wchar_t);
262   for (i = 0; i < qs->dwNumberOfCsAddrs; i++)
263   {
264     MarshallPtr (qs->lpcsaBuffer[i].LocalAddr.lpSockaddr, qs, SOCKADDR);
265     MarshallPtr (qs->lpcsaBuffer[i].RemoteAddr.lpSockaddr, qs, SOCKADDR);
266   }
267   MarshallPtr (qs->lpcsaBuffer, qs, CSADDR_INFO);
268   if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, sc) && qs->lpBlob != NULL && qs->lpBlob->pBlobData != NULL)
269   {
270     struct hostent *he;
271     he = (struct hostent *) qs->lpBlob->pBlobData;
272     for (i = 0; he->h_aliases[i] != NULL; i++)
273       MarshallPtr (he->h_aliases[i], he, char);
274     MarshallPtr (he->h_aliases, he, char *);
275     MarshallPtr (he->h_name, he, char);
276     for (i = 0; he->h_addr_list[i] != NULL; i++)
277       MarshallPtr (he->h_addr_list[i], he, void);
278     MarshallPtr (he->h_addr_list, he, char *);
279     MarshallPtr (qs->lpBlob->pBlobData, qs, void);
280   }
281   MarshallPtr (qs->lpBlob, qs, BLOB);
282 }
283
284
285 static void
286 process_lookup_result (void* cls, uint32_t rd_count,
287     const struct GNUNET_GNSRECORD_Data *rd)
288 {
289   int i, j, csanum;
290   struct request *rq = (struct request *) cls;
291   struct GNUNET_W32RESOLVER_GetMessage *msg;
292   struct GNUNET_MessageHeader *msgend;
293   WSAQUERYSETW *qs;
294   size_t size;
295   size_t size_recalc;
296   char *ptr;
297   size_t blobsize = 0;
298   size_t blobaddrcount = 0;
299
300   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301       "Got lookup result with count %u for rq %p with client %p\n",
302       rd_count, rq, rq->client);
303
304   if (rd_count == 0)
305   {
306     size = sizeof (struct GNUNET_MessageHeader);
307     msg = GNUNET_malloc (size);
308     msg->header.size = htons (size);
309     msg->header.type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
310     transmit (rq->client, &msg->header);
311     GNUNET_free_non_null (rq->name);
312     if (rq->u8name)
313       free (rq->u8name);
314     GNUNET_free (rq);
315     return;
316   }
317
318   size = sizeof (struct GNUNET_W32RESOLVER_GetMessage) + sizeof (WSAQUERYSETW);
319   size += (wcslen (rq->name) + 1) * sizeof (wchar_t);
320   size += sizeof (GUID);
321   /* lpszComment ? a TXT record? */
322   size += sizeof (GUID);
323   /* lpszContext ? Not sure what it is */
324   csanum = 0;
325   for (i = 0; i < rd_count; i++)
326   {
327     switch (rd[i].record_type)
328     {
329     case GNUNET_DNSPARSER_TYPE_A:
330       if (rd[i].data_size != sizeof (struct in_addr))
331         continue;
332       size += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in) * 2;
333       csanum++;
334       break;
335     case GNUNET_DNSPARSER_TYPE_AAAA:
336       if (rd[i].data_size != sizeof (struct in6_addr))
337         continue;
338       size += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in6) * 2;
339       csanum++;
340       break;
341     }
342   }
343   if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &rq->sc))
344   {
345     size += sizeof (BLOB);
346     blobsize += sizeof (struct hostent);
347     blobsize += strlen (rq->u8name) + 1;
348     blobsize += sizeof (void *); /* For aliases */
349     blobsize += sizeof (void *); /* For addresses */
350     for (i = 0; i < rd_count; i++)
351     {
352       if ((rq->af == AF_INET || rq->af == AF_UNSPEC) && rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
353       {
354         blobsize += sizeof (void *);
355         blobsize += sizeof (struct in_addr);
356         blobaddrcount++;
357       }
358       else if (rq->af == AF_INET6 && rd[i].record_type == GNUNET_DNSPARSER_TYPE_AAAA)
359       {
360         blobsize += sizeof (void *);
361         blobsize += sizeof (struct in6_addr);
362         blobaddrcount++;
363       }
364     }
365     size += blobsize;
366   }
367   size_recalc = sizeof (struct GNUNET_W32RESOLVER_GetMessage) + sizeof (WSAQUERYSETW);
368   msg = GNUNET_malloc (size);
369   msg->header.size = htons (size - sizeof (struct GNUNET_MessageHeader));
370   msg->header.type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
371   msg->af = htonl (rq->af);
372   msg->sc_data1 = htonl (rq->sc.Data1);
373   msg->sc_data2 = htons (rq->sc.Data2);
374   msg->sc_data3 = htons (rq->sc.Data3);
375   for (i = 0; i < 8; i++)
376     msg->sc_data4[i] = rq->sc.Data4[i];
377   qs = (WSAQUERYSETW *) &msg[1];
378   ptr = (char *) &qs[1];
379   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
380   qs->dwSize = sizeof (WSAQUERYSETW);
381   qs->lpszServiceInstanceName = (wchar_t *) ptr;
382   ptr += (wcslen (rq->name) + 1) * sizeof (wchar_t);
383   size_recalc += (wcslen (rq->name) + 1) * sizeof (wchar_t);
384   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
385   wcscpy (qs->lpszServiceInstanceName, rq->name);
386   qs->lpServiceClassId = (GUID *) ptr;
387   ptr += sizeof (GUID);
388   size_recalc += sizeof (GUID);
389   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
390   memcpy (qs->lpServiceClassId, &rq->sc, sizeof (GUID));
391   qs->lpVersion = NULL;
392   qs->dwNameSpace = NS_DNS;
393   qs->lpNSProviderId = (GUID *) ptr;
394   ptr += sizeof (GUID);
395   size_recalc += sizeof (GUID);
396   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
397   memcpy (qs->lpNSProviderId, &GNUNET_NAMESPACE_PROVIDER_DNS, sizeof (GUID));
398   qs->lpszContext = NULL;
399   qs->dwNumberOfProtocols = 0;
400   qs->lpafpProtocols = NULL;
401   /* Don't bother with this... */
402   qs->lpszQueryString = NULL;
403   qs->dwNumberOfCsAddrs = rd_count;
404   qs->lpcsaBuffer = (CSADDR_INFO *) ptr;
405   ptr += sizeof (CSADDR_INFO) * csanum;
406   j = 0;
407   for (i = 0; i < rd_count; i++)
408   {
409     switch (rd[i].record_type)
410     {
411     case GNUNET_DNSPARSER_TYPE_A:
412       if (rd[i].data_size != sizeof (struct in_addr))
413         continue;
414       qs->lpcsaBuffer[j].iSocketType = SOCK_STREAM;
415       qs->lpcsaBuffer[j].iProtocol = IPPROTO_TCP;
416
417       qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength = sizeof (struct sockaddr_in);
418       qs->lpcsaBuffer[j].LocalAddr.lpSockaddr = (SOCKADDR *) ptr;
419       ptr += qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength;
420       memset (qs->lpcsaBuffer[j].LocalAddr.lpSockaddr, 0, qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength);
421       ((struct sockaddr_in *)qs->lpcsaBuffer[j].LocalAddr.lpSockaddr)->sin_family = AF_INET;
422
423       qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength = sizeof (struct sockaddr_in);
424       qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr = (SOCKADDR *) ptr;
425       ptr += qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength;
426       memset (qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr, 0, qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength);
427       ((struct sockaddr_in *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin_family = AF_INET;
428       ((struct sockaddr_in *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin_port = htonl (53); /* Don't ask why it's 53 */
429       ((struct sockaddr_in *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin_addr = *(struct in_addr *) rd[i].data;
430       size_recalc += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in) * 2;
431       j++;
432       break;
433     case GNUNET_DNSPARSER_TYPE_AAAA:
434       if (rd[i].data_size != sizeof (struct in6_addr))
435         continue;
436       qs->lpcsaBuffer[j].iSocketType = SOCK_STREAM;
437       qs->lpcsaBuffer[j].iProtocol = IPPROTO_TCP;
438
439       qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength = sizeof (struct sockaddr_in6);
440       qs->lpcsaBuffer[j].LocalAddr.lpSockaddr = (SOCKADDR *) ptr;
441       ptr += qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength;
442       memset (qs->lpcsaBuffer[j].LocalAddr.lpSockaddr, 0, qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength);
443       ((struct sockaddr_in6 *)qs->lpcsaBuffer[j].LocalAddr.lpSockaddr)->sin6_family = AF_INET6;
444
445       qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength = sizeof (struct sockaddr_in6);
446       qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr = (SOCKADDR *) ptr;
447       ptr += qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength;
448       memset (qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr, 0, qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength);
449       ((struct sockaddr_in6 *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin6_family = AF_INET6;
450       ((struct sockaddr_in6 *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin6_port = htonl (53); /* Don't ask why it's 53 */
451       ((struct sockaddr_in6 *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin6_addr = *(struct in6_addr *) rd[i].data;
452       size_recalc += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in6) * 2;
453       j++;
454       break;
455     default:
456       break;
457     }
458   }
459   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
460   qs->dwOutputFlags = 0;
461   if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &rq->sc))
462   {
463     struct hostent *he;
464     qs->lpBlob = (BLOB *) ptr;
465     ptr += sizeof (BLOB);
466
467     size_recalc += sizeof (BLOB);
468     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
469
470     qs->lpBlob->cbSize = blobsize;
471     qs->lpBlob->pBlobData = (BYTE *) ptr;
472     ptr += sizeof (struct hostent);
473
474     size_recalc += sizeof (struct hostent);
475     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
476
477     he = (struct hostent *) qs->lpBlob->pBlobData;
478     he->h_name = (char *) ptr;
479     ptr += strlen (rq->u8name) + 1;
480
481     size_recalc += strlen (rq->u8name) + 1;
482     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
483
484     strcpy (he->h_name, rq->u8name);
485     he->h_aliases = (char **) ptr;
486     ptr += sizeof (void *);
487
488     size_recalc += sizeof (void *); /* For aliases */
489     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
490
491     he->h_aliases[0] = NULL;
492     he->h_addrtype = rq->af;
493     he->h_length = rq->af == AF_INET || rq->af == AF_UNSPEC ? sizeof (struct in_addr) : sizeof (struct in6_addr);
494     he->h_addr_list = (char **) ptr;
495     ptr += sizeof (void *) * (blobaddrcount + 1);
496
497     size_recalc += sizeof (void *) * (blobaddrcount + 1); /* For addresses */
498     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
499
500     j = 0;
501     for (i = 0; i < rd_count; i++)
502     {
503       if ((rq->af == AF_INET || rq->af == AF_UNSPEC) &&
504           rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
505       {
506         he->h_addr_list[j] = (char *) ptr;
507         ptr += sizeof (struct in_addr);
508
509         size_recalc += sizeof (struct in_addr);
510         GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
511
512         memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in_addr));
513         j++;
514       }
515       else if (rq->af == AF_INET6 && rd[i].record_type == GNUNET_DNSPARSER_TYPE_AAAA)
516       {
517         he->h_addr_list[j] = (char *) ptr;
518         ptr += sizeof (struct in6_addr);
519
520         size_recalc += sizeof (struct in6_addr);
521         GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
522
523         memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in6_addr));
524         j++;
525       }
526     }
527     he->h_addr_list[j] = NULL;
528   }
529   msgend = GNUNET_new (struct GNUNET_MessageHeader);
530
531   msgend->type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
532   msgend->size = htons (sizeof (struct GNUNET_MessageHeader));
533
534   if ((char *) ptr - (char *) msg != size || size_recalc != size || size_recalc != ((char *) ptr - (char *) msg))
535   {
536     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);
537   }
538   MarshallWSAQUERYSETW (qs, &rq->sc);
539   transmit (rq->client, &msg->header);
540   transmit (rq->client, msgend);
541   GNUNET_free_non_null (rq->name);
542   if (rq->u8name)
543     free (rq->u8name);
544   GNUNET_free (rq);
545 }
546
547
548 static void
549 get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
550     const wchar_t *name, int af, GUID sc)
551 {
552   struct request *rq;
553   char *hostname;
554   size_t strl;
555   size_t namelen;
556   uint32_t rtype;
557
558   if (IsEqualGUID (&SVCID_DNS_TYPE_A, &sc))
559     rtype = GNUNET_DNSPARSER_TYPE_A;
560   else if (IsEqualGUID (&SVCID_DNS_TYPE_NS, &sc))
561     rtype = GNUNET_DNSPARSER_TYPE_NS;
562   else if (IsEqualGUID (&SVCID_DNS_TYPE_CNAME, &sc))
563     rtype = GNUNET_DNSPARSER_TYPE_CNAME;
564   else if (IsEqualGUID (&SVCID_DNS_TYPE_SOA, &sc))
565     rtype = GNUNET_DNSPARSER_TYPE_SOA;
566   else if (IsEqualGUID (&SVCID_DNS_TYPE_PTR, &sc))
567     rtype = GNUNET_DNSPARSER_TYPE_PTR;
568   else if (IsEqualGUID (&SVCID_DNS_TYPE_MX, &sc))
569     rtype = GNUNET_DNSPARSER_TYPE_MX;
570   else if (IsEqualGUID (&SVCID_DNS_TYPE_TEXT, &sc))
571     rtype = GNUNET_DNSPARSER_TYPE_TXT;
572   else if (IsEqualGUID (&SVCID_DNS_TYPE_AAAA, &sc))
573     rtype = GNUNET_DNSPARSER_TYPE_AAAA;
574   else if (IsEqualGUID (&SVCID_DNS_TYPE_SRV, &sc))
575     rtype = GNUNET_DNSPARSER_TYPE_SRV;
576   else if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &sc))
577     rtype = GNUNET_DNSPARSER_TYPE_A;
578   else
579   {
580     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581                 "Unknown GUID: %08X-%04X-%04X-%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
582                 sc.Data1, sc.Data2, sc.Data3, sc.Data4[0], sc.Data4[1], sc.Data4[2],
583                 sc.Data4[3], sc.Data4[4], sc.Data4[5], sc.Data4[6], sc.Data4[7]);
584     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
585     return;
586   }
587
588   if (name)
589     namelen = wcslen (name);
590   else
591     namelen = 0;
592   if (namelen > 0)
593     hostname = (char *) u16_to_u8 (name, namelen + 1, NULL, &strl);
594   else
595     hostname = NULL;
596
597   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598               "W32 DNS resolver asked to look up %s for `%s'.\n",
599               af == AF_INET ? "IPv4" : af == AF_INET6 ? "IPv6" : "anything",
600               hostname);
601
602   rq = GNUNET_new (struct request);
603   rq->sc = sc;
604   rq->client = client;
605   rq->af = af;
606   if (rq->af != AF_INET && rq->af != AF_INET6)
607     rq->af = AF_INET;
608   if (namelen)
609   {
610     rq->name = GNUNET_malloc ((namelen + 1) * sizeof (wchar_t));
611     memcpy (rq->name, name, (namelen + 1) * sizeof (wchar_t));
612     rq->u8name = hostname;
613   }
614
615   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616               "Launching a lookup for client %p with rq %p\n",
617               client, rq);
618
619   rq->lookup_request = GNUNET_GNS_lookup (gns, hostname, &gns_master_pubkey,
620       rtype, GNUNET_NO /* Use DHT */, &gns_short_privkey, &process_lookup_result,
621       rq);
622
623   if (NULL != rq->lookup_request)
624   {
625     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626                 "Lookup launched, waiting for a reply\n");
627     GNUNET_SERVER_receive_done (client, GNUNET_OK);
628   }
629   else
630   {
631     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632                 "Lookup was not launched, disconnecting the client\n");
633     GNUNET_free_non_null (rq->name);
634     if (rq->u8name)
635       free (rq->u8name);
636     GNUNET_free (rq);
637     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
638   }
639 }
640
641
642 /**
643  * Handle GET-message.
644  *
645  * @param cls closure
646  * @param client identification of the client
647  * @param message the actual message
648  */
649 static void
650 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
651             const struct GNUNET_MessageHeader *message)
652 {
653   uint16_t msize;
654   const struct GNUNET_W32RESOLVER_GetMessage *msg;
655   GUID sc;
656   uint16_t size;
657   int i;
658   const wchar_t *hostname;
659   int af;
660
661   if (!got_egos)
662   {
663     /*
664      * FIXME: be done with GNUNET_OK, put the get request into a queue?
665      * or postpone GNUNET_SERVER_add_handlers() until egos are obtained?
666      */
667     GNUNET_SERVER_receive_done (client, GNUNET_NO);
668     return;
669   }
670
671   msize = ntohs (message->size);
672   if (msize <= sizeof (struct GNUNET_W32RESOLVER_GetMessage))
673   {
674     GNUNET_break (0);
675     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
676     return;
677   }
678   msg = (const struct GNUNET_W32RESOLVER_GetMessage *) message;
679   size = msize - sizeof (struct GNUNET_W32RESOLVER_GetMessage);
680   af = ntohl (msg->af);
681   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682       "Got NBO GUID: %08X-%04X-%04X-%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
683       msg->sc_data1, msg->sc_data2, msg->sc_data3, msg->sc_data4[0], msg->sc_data4[1],
684       msg->sc_data4[2], msg->sc_data4[3], msg->sc_data4[4], msg->sc_data4[5],
685       msg->sc_data4[6], msg->sc_data4[7]);
686   sc.Data1 = ntohl (msg->sc_data1);
687   sc.Data2 = ntohs (msg->sc_data2);
688   sc.Data3 = ntohs (msg->sc_data3);
689   for (i = 0; i < 8; i++)
690     sc.Data4[i] = msg->sc_data4[i];
691   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
692               "Got GUID: %08X-%04X-%04X-%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
693               sc.Data1, sc.Data2, sc.Data3, sc.Data4[0], sc.Data4[1], sc.Data4[2],
694               sc.Data4[3], sc.Data4[4], sc.Data4[5], sc.Data4[6], sc.Data4[7]);
695
696   hostname = (const wchar_t *) &msg[1];
697   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "name of %u bytes (last word is 0x%0X): %*S\n",
698         size, hostname[size / 2 - 2], size / 2, hostname);
699   if (hostname[size / 2 - 1] != L'\0')
700   {
701     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "name of length %u, not 0-terminated (%d-th word is 0x%0X): %*S\n",
702         size, size / 2 - 1, hostname[size / 2 - 1], size, hostname);
703     GNUNET_break (0);
704     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
705     return;
706   }
707   get_ip_from_hostname (client, hostname, af, sc);
708 }
709
710
711 /**
712  * Method called to with the ego we are to use for shortening
713  * during the lookup.
714  *
715  * @param cls closure (NULL, unused)
716  * @param ego ego handle, NULL if not found
717  * @param ctx context for application to store data for this ego
718  *                 (during the lifetime of this process, initially NULL)
719  * @param name name assigned by the user for this ego,
720  *                   NULL if the user just deleted the ego and it
721  *                   must thus no longer be used
722  */
723 static void
724 identity_shorten_cb (void *cls,
725          struct GNUNET_IDENTITY_Ego *ego,
726          void **ctx,
727          const char *name)
728 {
729   id_op = NULL;
730   if (NULL == ego)
731   {
732     fprintf (stderr,
733        _("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"));
734     GNUNET_SCHEDULER_shutdown ();
735     return;
736   }
737   gns_short_privkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
738   got_egos = 1;
739 }
740
741 /**
742  * Method called to with the ego we are to use for the lookup,
743  * when the ego is the one for the default master zone.
744  *
745  * @param cls closure (NULL, unused)
746  * @param ego ego handle, NULL if not found
747  * @param ctx context for application to store data for this ego
748  *                 (during the lifetime of this process, initially NULL)
749  * @param name name assigned by the user for this ego,
750  *                   NULL if the user just deleted the ego and it
751  *                   must thus no longer be used
752  */
753 static void
754 identity_master_cb (void *cls,
755         struct GNUNET_IDENTITY_Ego *ego,
756         void **ctx,
757         const char *name)
758 {
759   id_op = NULL;
760   if (NULL == ego)
761   {
762     fprintf (stderr,
763        _("Ego for `gns-master' not found, cannot perform lookup.  Did you run gnunet-gns-import.sh?\n"));
764     GNUNET_SCHEDULER_shutdown ();
765     return;
766   }
767   GNUNET_IDENTITY_ego_get_public_key (ego, &gns_master_pubkey);
768   id_op = GNUNET_IDENTITY_get (identity, "gns-short", &identity_shorten_cb,
769       NULL);
770   GNUNET_assert (NULL != id_op);
771 }
772
773
774 /**
775  * Start up gns-helper-w32 service.
776  *
777  * @param cls closure
778  * @param server the initialized server
779  * @param cfg configuration to use
780  */
781 static void
782 run (void *cls, struct GNUNET_SERVER_Handle *server,
783     const struct GNUNET_CONFIGURATION_Handle *cfg)
784 {
785   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
786     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_W32RESOLVER_REQUEST, 0},
787     {NULL, NULL, 0, 0}
788   };
789
790   gns = GNUNET_GNS_connect (cfg);
791   if (NULL == gns)
792   {
793     fprintf (stderr, _("Failed to connect to GNS\n"));
794     GNUNET_SCHEDULER_shutdown ();
795     return;
796   }
797   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
798       NULL);
799
800   identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
801   if (NULL == identity)
802   {
803     fprintf (stderr, _("Failed to connect to identity service\n"));
804     GNUNET_SCHEDULER_shutdown ();
805     return;
806   }
807
808   id_op = GNUNET_IDENTITY_get (identity, "gns-master", &identity_master_cb,
809       NULL);
810   GNUNET_assert (NULL != id_op);
811
812   GNUNET_SERVER_add_handlers (server, handlers);
813 }
814
815
816 /**
817  * The main function for gns-helper-w32.
818  *
819  * @param argc number of arguments from the command line
820  * @param argv command line arguments
821  * @return 0 ok, 1 on error
822  */
823 int
824 main (int argc, char *const *argv)
825 {
826   int ret;
827
828   ret = GNUNET_SERVICE_run (argc, argv, "gns-helper-service-w32",
829       GNUNET_SERVICE_OPTION_NONE, &run, NULL);
830   return (GNUNET_OK == ret) ? 0 : 1;
831 }
832
833 /* end of gnunet-gns-helper-service-w32.c */