- log
[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_malloc (sizeof (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   msg->sc_data4 = 0;
368   for (i = 0; i < 8; i++)
369     msg->sc_data4 |= rq->sc.Data4[i] << ((7 - i) * 8);
370   msg->sc_data4 = GNUNET_htonll (msg->sc_data4);
371   qs = (WSAQUERYSETW *) &msg[1];
372   ptr = (char *) &qs[1];
373   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
374   qs->dwSize = sizeof (WSAQUERYSETW);
375   qs->lpszServiceInstanceName = (wchar_t *) ptr;
376   ptr += (wcslen (rq->name) + 1) * sizeof (wchar_t);
377   size_recalc += (wcslen (rq->name) + 1) * sizeof (wchar_t);
378   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
379   wcscpy (qs->lpszServiceInstanceName, rq->name);
380   qs->lpServiceClassId = (GUID *) ptr;
381   ptr += sizeof (GUID);
382   size_recalc += sizeof (GUID);
383   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
384   memcpy (qs->lpServiceClassId, &rq->sc, sizeof (GUID));
385   qs->lpVersion = NULL;
386   qs->dwNameSpace = NS_DNS;
387   qs->lpNSProviderId = (GUID *) ptr;
388   ptr += sizeof (GUID);
389   size_recalc += sizeof (GUID);
390   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
391   memcpy (qs->lpNSProviderId, &GNUNET_NAMESPACE_PROVIDER_DNS, sizeof (GUID));
392   qs->lpszContext = NULL;
393   qs->dwNumberOfProtocols = 0;
394   qs->lpafpProtocols = NULL;
395   /* Don't bother with this... */
396   qs->lpszQueryString = NULL;
397   qs->dwNumberOfCsAddrs = rd_count;
398   qs->lpcsaBuffer = (CSADDR_INFO *) ptr;
399   ptr += sizeof (CSADDR_INFO) * csanum;
400   j = 0;
401   for (i = 0; i < rd_count; i++)
402   {
403     switch (rd[i].record_type)
404     {
405     case GNUNET_DNSPARSER_TYPE_A:
406       if (rd[i].data_size != sizeof (struct in_addr))
407         continue;
408       qs->lpcsaBuffer[j].iSocketType = SOCK_STREAM;
409       qs->lpcsaBuffer[j].iProtocol = IPPROTO_TCP;
410
411       qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength = sizeof (struct sockaddr_in);
412       qs->lpcsaBuffer[j].LocalAddr.lpSockaddr = (SOCKADDR *) ptr;
413       ptr += qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength;
414       memset (qs->lpcsaBuffer[j].LocalAddr.lpSockaddr, 0, qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength);
415       ((struct sockaddr_in *)qs->lpcsaBuffer[j].LocalAddr.lpSockaddr)->sin_family = AF_INET;
416
417       qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength = sizeof (struct sockaddr_in);
418       qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr = (SOCKADDR *) ptr;
419       ptr += qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength;
420       memset (qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr, 0, qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength);
421       ((struct sockaddr_in *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin_family = AF_INET;
422       ((struct sockaddr_in *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin_port = htonl (53); /* Don't ask why it's 53 */
423       ((struct sockaddr_in *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin_addr = *(struct in_addr *) rd[i].data;
424       size_recalc += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in) * 2;
425       j++;
426       break;
427     case GNUNET_DNSPARSER_TYPE_AAAA:
428       if (rd[i].data_size != sizeof (struct in6_addr))
429         continue;
430       qs->lpcsaBuffer[j].iSocketType = SOCK_STREAM;
431       qs->lpcsaBuffer[j].iProtocol = IPPROTO_TCP;
432
433       qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength = sizeof (struct sockaddr_in6);
434       qs->lpcsaBuffer[j].LocalAddr.lpSockaddr = (SOCKADDR *) ptr;
435       ptr += qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength;
436       memset (qs->lpcsaBuffer[j].LocalAddr.lpSockaddr, 0, qs->lpcsaBuffer[j].LocalAddr.iSockaddrLength);
437       ((struct sockaddr_in6 *)qs->lpcsaBuffer[j].LocalAddr.lpSockaddr)->sin6_family = AF_INET6;
438
439       qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength = sizeof (struct sockaddr_in6);
440       qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr = (SOCKADDR *) ptr;
441       ptr += qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength;
442       memset (qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr, 0, qs->lpcsaBuffer[j].RemoteAddr.iSockaddrLength);
443       ((struct sockaddr_in6 *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin6_family = AF_INET6;
444       ((struct sockaddr_in6 *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin6_port = htonl (53); /* Don't ask why it's 53 */
445       ((struct sockaddr_in6 *)qs->lpcsaBuffer[j].RemoteAddr.lpSockaddr)->sin6_addr = *(struct in6_addr *) rd[i].data;
446       size_recalc += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in6) * 2;
447       j++;
448       break;
449     default:
450       break;
451     }
452   }
453   GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
454   qs->dwOutputFlags = 0;
455   if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &rq->sc))
456   {
457     struct hostent *he;
458     qs->lpBlob = (BLOB *) ptr;
459     ptr += sizeof (BLOB);
460
461     size_recalc += sizeof (BLOB);
462     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
463
464     qs->lpBlob->cbSize = blobsize;
465     qs->lpBlob->pBlobData = (BYTE *) ptr;
466     ptr += sizeof (struct hostent);
467
468     size_recalc += sizeof (struct hostent);
469     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
470
471     he = (struct hostent *) qs->lpBlob->pBlobData;
472     he->h_name = (char *) ptr;
473     ptr += strlen (rq->u8name) + 1;
474
475     size_recalc += strlen (rq->u8name) + 1;
476     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
477
478     strcpy (he->h_name, rq->u8name);
479     he->h_aliases = (char **) ptr;
480     ptr += sizeof (void *);
481
482     size_recalc += sizeof (void *); /* For aliases */
483     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
484
485     he->h_aliases[0] = NULL;
486     he->h_addrtype = rq->af;
487     he->h_length = rq->af == AF_INET || rq->af == AF_UNSPEC ? sizeof (struct in_addr) : sizeof (struct in6_addr);
488     he->h_addr_list = (char **) ptr;
489     ptr += sizeof (void *) * (blobaddrcount + 1);
490
491     size_recalc += sizeof (void *) * (blobaddrcount + 1); /* For addresses */
492     GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
493
494     j = 0;
495     for (i = 0; i < rd_count; i++)
496     {
497       if ((rq->af == AF_INET || rq->af == AF_UNSPEC) &&
498           rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
499       {
500         he->h_addr_list[j] = (char *) ptr;
501         ptr += sizeof (struct in_addr);
502
503         size_recalc += sizeof (struct in_addr);
504         GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
505
506         memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in_addr));
507         j++;
508       }
509       else if (rq->af == AF_INET6 && rd[i].record_type == GNUNET_DNSPARSER_TYPE_AAAA)
510       {
511         he->h_addr_list[j] = (char *) ptr;
512         ptr += sizeof (struct in6_addr);
513
514         size_recalc += sizeof (struct in6_addr);
515         GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
516
517         memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in6_addr));
518         j++;
519       }
520     }
521     he->h_addr_list[j] = NULL;
522   }
523   msgend = (struct GNUNET_MessageHeader *) ptr;
524   ptr += sizeof (struct GNUNET_MessageHeader);
525   size_recalc += sizeof (struct GNUNET_MessageHeader);
526
527   msgend->type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
528   msgend->size = htons (sizeof (struct GNUNET_MessageHeader));
529
530   if ((char *) ptr - (char *) msg != size || size_recalc != size || size_recalc != ((char *) ptr - (char *) msg))
531   {
532     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);
533   }
534   MarshallWSAQUERYSETW (qs, &rq->sc);
535   transmit (rq->client, &msg->header);
536   GNUNET_free_non_null (rq->name);
537   if (rq->u8name)
538     free (rq->u8name);
539   GNUNET_free (rq);
540 }
541
542
543 static void
544 get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
545     const wchar_t *name, int af, GUID sc)
546 {
547   struct request *rq;
548   char *hostname;
549   size_t strl;
550   size_t namelen;
551   uint32_t rtype;
552
553   if (IsEqualGUID (&SVCID_DNS_TYPE_A, &sc))
554     rtype = GNUNET_DNSPARSER_TYPE_A;
555   else if (IsEqualGUID (&SVCID_DNS_TYPE_NS, &sc))
556     rtype = GNUNET_DNSPARSER_TYPE_NS;
557   else if (IsEqualGUID (&SVCID_DNS_TYPE_CNAME, &sc))
558     rtype = GNUNET_DNSPARSER_TYPE_CNAME;
559   else if (IsEqualGUID (&SVCID_DNS_TYPE_SOA, &sc))
560     rtype = GNUNET_DNSPARSER_TYPE_SOA;
561   else if (IsEqualGUID (&SVCID_DNS_TYPE_PTR, &sc))
562     rtype = GNUNET_DNSPARSER_TYPE_PTR;
563   else if (IsEqualGUID (&SVCID_DNS_TYPE_MX, &sc))
564     rtype = GNUNET_DNSPARSER_TYPE_MX;
565   else if (IsEqualGUID (&SVCID_DNS_TYPE_TEXT, &sc))
566     rtype = GNUNET_DNSPARSER_TYPE_TXT;
567   else if (IsEqualGUID (&SVCID_DNS_TYPE_AAAA, &sc))
568     rtype = GNUNET_DNSPARSER_TYPE_AAAA;
569   else if (IsEqualGUID (&SVCID_DNS_TYPE_SRV, &sc))
570     rtype = GNUNET_DNSPARSER_TYPE_SRV;
571   else if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &sc))
572     rtype = GNUNET_DNSPARSER_TYPE_A;
573   else
574   {
575     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576                 "Unknown GUID: %08X-%04X-%04X-%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
577                 sc.Data1, sc.Data2, sc.Data3, sc.Data4[0], sc.Data4[1], sc.Data4[2],
578                 sc.Data4[3], sc.Data4[4], sc.Data4[5], sc.Data4[6], sc.Data4[7]);
579     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
580     return;
581   }
582
583   if (name)
584     namelen = wcslen (name);
585   else
586     namelen = 0;
587   if (namelen > 0)
588     hostname = (char *) u16_to_u8 (name, namelen + 1, NULL, &strl);
589   else
590     hostname = NULL;
591
592   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593               "W32 DNS resolver asked to look up %s for `%s'.\n",
594               af == AF_INET ? "IPv4" : af == AF_INET6 ? "IPv6" : "anything",
595               hostname);
596
597   rq = GNUNET_malloc (sizeof (struct request));
598   rq->sc = sc;
599   rq->client = client;
600   rq->af = af;
601   if (rq->af != AF_INET && rq->af != AF_INET6)
602     rq->af = AF_INET;
603   if (namelen)
604   {
605     rq->name = GNUNET_malloc ((namelen + 1) * sizeof (wchar_t));
606     memcpy (rq->name, name, (namelen + 1) * sizeof (wchar_t));
607     rq->u8name = hostname;
608   }
609
610   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
611               "Launching a lookup for client %p with rq %p\n",
612               client, rq);
613
614   rq->lookup_request = GNUNET_GNS_lookup (gns, hostname, &gns_master_pubkey,
615       rtype, GNUNET_NO /* Use DHT */, &gns_short_privkey, &process_lookup_result,
616       rq);
617
618   if (NULL != rq->lookup_request)
619   {
620     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621                 "Lookup launched, waiting for a reply\n");
622     GNUNET_SERVER_receive_done (client, GNUNET_OK);
623   }
624   else
625   {
626     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627                 "Lookup was not launched, disconnecting the client\n");
628     GNUNET_free_non_null (rq->name);
629     if (rq->u8name)
630       free (rq->u8name);
631     GNUNET_free (rq);
632     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
633   }
634 }
635
636
637 /**
638  * Handle GET-message.
639  *
640  * @param cls closure
641  * @param client identification of the client
642  * @param message the actual message
643  */
644 static void
645 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
646             const struct GNUNET_MessageHeader *message)
647 {
648   uint16_t msize;
649   const struct GNUNET_W32RESOLVER_GetMessage *msg;
650   GUID sc;
651   uint16_t size;
652   uint64_t data4;
653   int i;
654   const wchar_t *hostname;
655   int af;
656
657   if (!got_egos)
658   {
659     /*
660      * FIXME: be done with GNUNET_OK, put the get request into a queue?
661      * or postpone GNUNET_SERVER_add_handlers() until egos are obtained?
662      */
663     GNUNET_SERVER_receive_done (client, GNUNET_NO);
664     return;
665   }
666
667   msize = ntohs (message->size);
668   if (msize < sizeof (struct GNUNET_W32RESOLVER_GetMessage))
669   {
670     GNUNET_break (0);
671     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
672     return;
673   }
674   msg = (const struct GNUNET_W32RESOLVER_GetMessage *) message;
675   size = msize - sizeof (struct GNUNET_W32RESOLVER_GetMessage);
676   af = ntohl (msg->af);
677   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got NBO GUID: %08X-%04X-%04X-%016llX\n",
678       msg->sc_data1, msg->sc_data2, msg->sc_data3, msg->sc_data4);
679   sc.Data1 = ntohl (msg->sc_data1);
680   sc.Data2 = ntohs (msg->sc_data2);
681   sc.Data3 = ntohs (msg->sc_data3);
682   data4 = GNUNET_ntohll (msg->sc_data4);
683   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got GUID: %08X-%04X-%04X-%016llX\n",
684       sc.Data1, sc.Data2, sc.Data3, data4);
685   for (i = 0; i < 8; i++)
686     sc.Data4[i] = 0xFF & (data4 >> ((7 - i) * 8));
687
688   hostname = (const wchar_t *) &msg[1];
689   if (hostname[size - 1] != L'\0')
690   {
691     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "name of length %u, not 0-terminated: %*S\n",
692         size, size, hostname);
693     GNUNET_break (0);
694     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
695     return;
696   }
697   get_ip_from_hostname (client, hostname, af, sc);
698 }
699
700
701 /**
702  * Method called to with the ego we are to use for shortening
703  * during the lookup.
704  *
705  * @param cls closure (NULL, unused)
706  * @param ego ego handle, NULL if not found
707  * @param ctx context for application to store data for this ego
708  *                 (during the lifetime of this process, initially NULL)
709  * @param name name assigned by the user for this ego,
710  *                   NULL if the user just deleted the ego and it
711  *                   must thus no longer be used
712  */
713 static void
714 identity_shorten_cb (void *cls,
715          struct GNUNET_IDENTITY_Ego *ego,
716          void **ctx,
717          const char *name)
718 {
719   id_op = NULL;
720   if (NULL == ego)
721   {
722     fprintf (stderr,
723        _("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"));
724     GNUNET_SCHEDULER_shutdown ();
725     return;
726   }
727   gns_short_privkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
728   got_egos = 1;
729 }
730
731 /**
732  * Method called to with the ego we are to use for the lookup,
733  * when the ego is the one for the default master zone.
734  *
735  * @param cls closure (NULL, unused)
736  * @param ego ego handle, NULL if not found
737  * @param ctx context for application to store data for this ego
738  *                 (during the lifetime of this process, initially NULL)
739  * @param name name assigned by the user for this ego,
740  *                   NULL if the user just deleted the ego and it
741  *                   must thus no longer be used
742  */
743 static void
744 identity_master_cb (void *cls,
745         struct GNUNET_IDENTITY_Ego *ego,
746         void **ctx,
747         const char *name)
748 {
749   id_op = NULL;
750   if (NULL == ego)
751   {
752     fprintf (stderr,
753        _("Ego for `gns-master' not found, cannot perform lookup.  Did you run gnunet-gns-import.sh?\n"));
754     GNUNET_SCHEDULER_shutdown ();
755     return;
756   }
757   GNUNET_IDENTITY_ego_get_public_key (ego, &gns_master_pubkey);
758   id_op = GNUNET_IDENTITY_get (identity, "gns-short", &identity_shorten_cb,
759       NULL);
760   GNUNET_assert (NULL != id_op);
761 }
762
763
764 /**
765  * Start up gns-helper-w32 service.
766  *
767  * @param cls closure
768  * @param server the initialized server
769  * @param cfg configuration to use
770  */
771 static void
772 run (void *cls, struct GNUNET_SERVER_Handle *server,
773     const struct GNUNET_CONFIGURATION_Handle *cfg)
774 {
775   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
776     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_W32RESOLVER_REQUEST, 0},
777     {NULL, NULL, 0, 0}
778   };
779
780   gns = GNUNET_GNS_connect (cfg);
781   if (NULL == gns)
782   {
783     fprintf (stderr, _("Failed to connect to GNS\n"));
784     GNUNET_SCHEDULER_shutdown ();
785     return;
786   }
787   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
788       NULL);
789
790   identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
791   if (NULL == identity)
792   {
793     fprintf (stderr, _("Failed to connect to identity service\n"));
794     GNUNET_SCHEDULER_shutdown ();
795     return;
796   }
797
798   id_op = GNUNET_IDENTITY_get (identity, "gns-master", &identity_master_cb,
799       NULL);
800   GNUNET_assert (NULL != id_op);
801
802   GNUNET_SERVER_add_handlers (server, handlers);
803 }
804
805
806 /**
807  * The main function for gns-helper-w32.
808  *
809  * @param argc number of arguments from the command line
810  * @param argv command line arguments
811  * @return 0 ok, 1 on error
812  */
813 int
814 main (int argc, char *const *argv)
815 {
816   int ret;
817
818   ret = GNUNET_SERVICE_run (argc, argv, "gns-helper-service-w32",
819       GNUNET_SERVICE_OPTION_NONE, &run, NULL);
820   return (GNUNET_OK == ret) ? 0 : 1;
821 }
822
823 /* end of gnunet-gns-helper-service-w32.c */