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