2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file gnunet-gns-helper-service-w32.c
22 * @brief an intermediary service to access distributed GNS
23 * @author Christian Grothoff
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"
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);
53 struct GNUNET_SERVER_Client *client;
58 struct GNUNET_GNS_LookupRequest *lookup_request;
62 * Handle to GNS service.
64 static struct GNUNET_GNS_Handle *gns;
67 * Active operation on identity service.
69 static struct GNUNET_IDENTITY_Operation *id_op;
72 * Handle for identity service.
74 static struct GNUNET_IDENTITY_Handle *identity;
77 * Public key of the gns-master ego
79 static struct GNUNET_CRYPTO_EcdsaPublicKey gns_master_pubkey;
82 * Private key of the gns-short ego
84 static struct GNUNET_CRYPTO_EcdsaPrivateKey gns_short_privkey;
87 * Set to 1 once egos are obtained.
89 static int got_egos = 0;
92 * Task run on shutdown. Cleans up everything.
95 * @param tc scheduler context
98 do_shutdown (void *cls,
99 const struct GNUNET_SCHEDULER_TaskContext *tc)
103 GNUNET_IDENTITY_cancel (id_op);
106 if (NULL != identity)
108 GNUNET_IDENTITY_disconnect (identity);
113 GNUNET_GNS_disconnect (gns);
119 * Context for transmitting replies to clients.
121 struct TransmitCallbackContext
125 * We keep these in a doubly-linked list (for cleanup).
127 struct TransmitCallbackContext *next;
130 * We keep these in a doubly-linked list (for cleanup).
132 struct TransmitCallbackContext *prev;
135 * The message that we're asked to transmit.
137 struct GNUNET_MessageHeader *msg;
140 * Handle for the transmission request.
142 struct GNUNET_SERVER_TransmitHandle *th;
146 * Handle for the client to which to send
148 struct GNUNET_SERVER_Client *client;
153 * Head of the doubly-linked list (for cleanup).
155 static struct TransmitCallbackContext *tcc_head;
158 * Tail of the doubly-linked list (for cleanup).
160 static struct TransmitCallbackContext *tcc_tail;
163 * Have we already cleaned up the TCCs and are hence no longer
164 * willing (or able) to transmit anything to anyone?
166 static int cleaning_done;
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.
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
181 transmit_callback (void *cls, size_t size, void *buf)
183 struct TransmitCallbackContext *tcc = cls;
187 GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
188 msize = ntohs (tcc->msg->size);
191 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
192 _("Transmission to client failed!\n"));
193 GNUNET_free (tcc->msg);
197 GNUNET_assert (size >= msize);
198 memcpy (buf, tcc->msg, msize);
199 GNUNET_free (tcc->msg);
201 for (tcc = tcc_head; tcc; tcc = tcc->next)
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);
217 * Transmit the given message to the client.
219 * @param client target of the message
220 * @param msg message to transmit, will be freed!
223 transmit (struct GNUNET_SERVER_Client *client,
224 struct GNUNET_MessageHeader *msg)
226 struct TransmitCallbackContext *tcc;
228 if (GNUNET_YES == cleaning_done)
230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
231 _("Shutdown in progress, aborting transmission.\n"));
235 tcc = GNUNET_new (struct TransmitCallbackContext);
237 tcc->client = client;
238 tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
240 GNUNET_TIME_UNIT_FOREVER_REL,
241 &transmit_callback, tcc);
242 GNUNET_CONTAINER_DLL_insert (tcc_head, tcc_tail, tcc);
246 #define MarshallPtr(ptr, base, type) \
248 ptr = (type *) ((char *) ptr - (char *) base)
252 MarshallWSAQUERYSETW (WSAQUERYSETW *qs, GUID *sc)
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++)
264 MarshallPtr (qs->lpcsaBuffer[i].LocalAddr.lpSockaddr, qs, SOCKADDR);
265 MarshallPtr (qs->lpcsaBuffer[i].RemoteAddr.lpSockaddr, qs, SOCKADDR);
267 MarshallPtr (qs->lpcsaBuffer, qs, CSADDR_INFO);
268 if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, sc) && qs->lpBlob != NULL && qs->lpBlob->pBlobData != NULL)
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);
281 MarshallPtr (qs->lpBlob, qs, BLOB);
286 process_lookup_result (void* cls, uint32_t rd_count,
287 const struct GNUNET_GNSRECORD_Data *rd)
290 struct request *rq = (struct request *) cls;
291 struct GNUNET_W32RESOLVER_GetMessage *msg;
292 struct GNUNET_MessageHeader *msgend;
298 size_t blobaddrcount = 0;
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);
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);
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 */
325 for (i = 0; i < rd_count; i++)
327 switch (rd[i].record_type)
329 case GNUNET_DNSPARSER_TYPE_A:
330 if (rd[i].data_size != sizeof (struct in_addr))
332 size += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in) * 2;
335 case GNUNET_DNSPARSER_TYPE_AAAA:
336 if (rd[i].data_size != sizeof (struct in6_addr))
338 size += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in6) * 2;
343 if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &rq->sc))
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++)
352 if ((rq->af == AF_INET || rq->af == AF_UNSPEC) && rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
354 blobsize += sizeof (void *);
355 blobsize += sizeof (struct in_addr);
358 else if (rq->af == AF_INET6 && rd[i].record_type == GNUNET_DNSPARSER_TYPE_AAAA)
360 blobsize += sizeof (void *);
361 blobsize += sizeof (struct in6_addr);
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;
407 for (i = 0; i < rd_count; i++)
409 switch (rd[i].record_type)
411 case GNUNET_DNSPARSER_TYPE_A:
412 if (rd[i].data_size != sizeof (struct in_addr))
414 qs->lpcsaBuffer[j].iSocketType = SOCK_STREAM;
415 qs->lpcsaBuffer[j].iProtocol = IPPROTO_TCP;
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;
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;
433 case GNUNET_DNSPARSER_TYPE_AAAA:
434 if (rd[i].data_size != sizeof (struct in6_addr))
436 qs->lpcsaBuffer[j].iSocketType = SOCK_STREAM;
437 qs->lpcsaBuffer[j].iProtocol = IPPROTO_TCP;
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;
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;
459 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
460 qs->dwOutputFlags = 0;
461 if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &rq->sc))
464 qs->lpBlob = (BLOB *) ptr;
465 ptr += sizeof (BLOB);
467 size_recalc += sizeof (BLOB);
468 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
470 qs->lpBlob->cbSize = blobsize;
471 qs->lpBlob->pBlobData = (BYTE *) ptr;
472 ptr += sizeof (struct hostent);
474 size_recalc += sizeof (struct hostent);
475 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
477 he = (struct hostent *) qs->lpBlob->pBlobData;
478 he->h_name = (char *) ptr;
479 ptr += strlen (rq->u8name) + 1;
481 size_recalc += strlen (rq->u8name) + 1;
482 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
484 strcpy (he->h_name, rq->u8name);
485 he->h_aliases = (char **) ptr;
486 ptr += sizeof (void *);
488 size_recalc += sizeof (void *); /* For aliases */
489 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
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);
497 size_recalc += sizeof (void *) * (blobaddrcount + 1); /* For addresses */
498 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
501 for (i = 0; i < rd_count; i++)
503 if ((rq->af == AF_INET || rq->af == AF_UNSPEC) &&
504 rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
506 he->h_addr_list[j] = (char *) ptr;
507 ptr += sizeof (struct in_addr);
509 size_recalc += sizeof (struct in_addr);
510 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
512 memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in_addr));
515 else if (rq->af == AF_INET6 && rd[i].record_type == GNUNET_DNSPARSER_TYPE_AAAA)
517 he->h_addr_list[j] = (char *) ptr;
518 ptr += sizeof (struct in6_addr);
520 size_recalc += sizeof (struct in6_addr);
521 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
523 memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in6_addr));
527 he->h_addr_list[j] = NULL;
529 msgend = GNUNET_new (struct GNUNET_MessageHeader);
531 msgend->type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
532 msgend->size = htons (sizeof (struct GNUNET_MessageHeader));
534 if ((char *) ptr - (char *) msg != size || size_recalc != size || size_recalc != ((char *) ptr - (char *) msg))
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);
538 MarshallWSAQUERYSETW (qs, &rq->sc);
539 transmit (rq->client, &msg->header);
540 transmit (rq->client, msgend);
541 GNUNET_free_non_null (rq->name);
549 get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
550 const wchar_t *name, int af, GUID sc)
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;
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);
589 namelen = wcslen (name);
593 hostname = (char *) u16_to_u8 (name, namelen + 1, NULL, &strl);
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",
602 rq = GNUNET_new (struct request);
606 if (rq->af != AF_INET && rq->af != AF_INET6)
610 rq->name = GNUNET_malloc ((namelen + 1) * sizeof (wchar_t));
611 memcpy (rq->name, name, (namelen + 1) * sizeof (wchar_t));
612 rq->u8name = hostname;
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "Launching a lookup for client %p with rq %p\n",
619 rq->lookup_request = GNUNET_GNS_lookup (gns, hostname, &gns_master_pubkey,
620 rtype, GNUNET_NO /* Use DHT */, &gns_short_privkey, &process_lookup_result,
623 if (NULL != rq->lookup_request)
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "Lookup launched, waiting for a reply\n");
627 GNUNET_SERVER_receive_done (client, GNUNET_OK);
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "Lookup was not launched, disconnecting the client\n");
633 GNUNET_free_non_null (rq->name);
637 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
643 * Handle GET-message.
646 * @param client identification of the client
647 * @param message the actual message
650 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
651 const struct GNUNET_MessageHeader *message)
654 const struct GNUNET_W32RESOLVER_GetMessage *msg;
658 const wchar_t *hostname;
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?
667 GNUNET_SERVER_receive_done (client, GNUNET_NO);
671 msize = ntohs (message->size);
672 if (msize < sizeof (struct GNUNET_W32RESOLVER_GetMessage))
675 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
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]);
696 hostname = (const wchar_t *) &msg[1];
697 if (hostname[size - 1] != L'\0')
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "name of length %u, not 0-terminated: %*S\n",
700 size, size, hostname);
702 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
705 get_ip_from_hostname (client, hostname, af, sc);
710 * Method called to with the ego we are to use for shortening
713 * @param cls closure (NULL, unused)
714 * @param ego ego handle, NULL if not found
715 * @param ctx context for application to store data for this ego
716 * (during the lifetime of this process, initially NULL)
717 * @param name name assigned by the user for this ego,
718 * NULL if the user just deleted the ego and it
719 * must thus no longer be used
722 identity_shorten_cb (void *cls,
723 struct GNUNET_IDENTITY_Ego *ego,
731 _("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"));
732 GNUNET_SCHEDULER_shutdown ();
735 gns_short_privkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
740 * Method called to with the ego we are to use for the lookup,
741 * when the ego is the one for the default master zone.
743 * @param cls closure (NULL, unused)
744 * @param ego ego handle, NULL if not found
745 * @param ctx context for application to store data for this ego
746 * (during the lifetime of this process, initially NULL)
747 * @param name name assigned by the user for this ego,
748 * NULL if the user just deleted the ego and it
749 * must thus no longer be used
752 identity_master_cb (void *cls,
753 struct GNUNET_IDENTITY_Ego *ego,
761 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
762 GNUNET_SCHEDULER_shutdown ();
765 GNUNET_IDENTITY_ego_get_public_key (ego, &gns_master_pubkey);
766 id_op = GNUNET_IDENTITY_get (identity, "gns-short", &identity_shorten_cb,
768 GNUNET_assert (NULL != id_op);
773 * Start up gns-helper-w32 service.
776 * @param server the initialized server
777 * @param cfg configuration to use
780 run (void *cls, struct GNUNET_SERVER_Handle *server,
781 const struct GNUNET_CONFIGURATION_Handle *cfg)
783 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
784 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_W32RESOLVER_REQUEST, 0},
788 gns = GNUNET_GNS_connect (cfg);
791 fprintf (stderr, _("Failed to connect to GNS\n"));
792 GNUNET_SCHEDULER_shutdown ();
795 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
798 identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
799 if (NULL == identity)
801 fprintf (stderr, _("Failed to connect to identity service\n"));
802 GNUNET_SCHEDULER_shutdown ();
806 id_op = GNUNET_IDENTITY_get (identity, "gns-master", &identity_master_cb,
808 GNUNET_assert (NULL != id_op);
810 GNUNET_SERVER_add_handlers (server, handlers);
815 * The main function for gns-helper-w32.
817 * @param argc number of arguments from the command line
818 * @param argv command line arguments
819 * @return 0 ok, 1 on error
822 main (int argc, char *const *argv)
826 ret = GNUNET_SERVICE_run (argc, argv, "gns-helper-service-w32",
827 GNUNET_SERVICE_OPTION_NONE, &run, NULL);
828 return (GNUNET_OK == ret) ? 0 : 1;
831 /* end of gnunet-gns-helper-service-w32.c */