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);
54 * We keep these in a doubly-linked list (for cleanup).
59 * We keep these in a doubly-linked list (for cleanup).
63 struct GNUNET_SERVER_Client *client;
68 struct GNUNET_GNS_LookupRequest *lookup_request;
72 * Head of the doubly-linked list (for cleanup).
74 static struct request *rq_head;
77 * Tail of the doubly-linked list (for cleanup).
79 static struct request *rq_tail;
82 * Handle to GNS service.
84 static struct GNUNET_GNS_Handle *gns;
87 * Active operation on identity service.
89 static struct GNUNET_IDENTITY_Operation *id_op;
92 * Handle for identity service.
94 static struct GNUNET_IDENTITY_Handle *identity;
97 * Public key of the gns-master ego
99 static struct GNUNET_CRYPTO_EcdsaPublicKey gns_master_pubkey;
102 * Private key of the gns-short ego
104 static struct GNUNET_CRYPTO_EcdsaPrivateKey gns_short_privkey;
107 * Set to 1 once egos are obtained.
109 static int got_egos = 0;
112 * Task run on shutdown. Cleans up everything.
115 * @param tc scheduler context
118 do_shutdown (void *cls,
119 const struct GNUNET_SCHEDULER_TaskContext *tc)
124 GNUNET_IDENTITY_cancel (id_op);
127 if (NULL != identity)
129 GNUNET_IDENTITY_disconnect (identity);
132 while (NULL != (rq = rq_head))
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);
144 GNUNET_GNS_disconnect (gns);
150 * Context for transmitting replies to clients.
152 struct TransmitCallbackContext
156 * We keep these in a doubly-linked list (for cleanup).
158 struct TransmitCallbackContext *next;
161 * We keep these in a doubly-linked list (for cleanup).
163 struct TransmitCallbackContext *prev;
166 * The message that we're asked to transmit.
168 struct GNUNET_MessageHeader *msg;
171 * Handle for the transmission request.
173 struct GNUNET_SERVER_TransmitHandle *th;
177 * Handle for the client to which to send
179 struct GNUNET_SERVER_Client *client;
184 * Head of the doubly-linked list (for cleanup).
186 static struct TransmitCallbackContext *tcc_head;
189 * Tail of the doubly-linked list (for cleanup).
191 static struct TransmitCallbackContext *tcc_tail;
194 * Have we already cleaned up the TCCs and are hence no longer
195 * willing (or able) to transmit anything to anyone?
197 static int cleaning_done;
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.
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
212 transmit_callback (void *cls, size_t size, void *buf)
214 struct TransmitCallbackContext *tcc = cls;
218 GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
219 msize = ntohs (tcc->msg->size);
222 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
223 _("Transmission to client failed!\n"));
224 GNUNET_free (tcc->msg);
228 GNUNET_assert (size >= msize);
229 memcpy (buf, tcc->msg, msize);
230 GNUNET_free (tcc->msg);
232 for (tcc = tcc_head; tcc; tcc = tcc->next)
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);
248 * Transmit the given message to the client.
250 * @param client target of the message
251 * @param msg message to transmit, will be freed!
254 transmit (struct GNUNET_SERVER_Client *client,
255 struct GNUNET_MessageHeader *msg)
257 struct TransmitCallbackContext *tcc;
259 if (GNUNET_YES == cleaning_done)
261 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
262 _("Shutdown in progress, aborting transmission.\n"));
266 tcc = GNUNET_new (struct TransmitCallbackContext);
268 tcc->client = client;
269 tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
271 GNUNET_TIME_UNIT_FOREVER_REL,
272 &transmit_callback, tcc);
273 GNUNET_CONTAINER_DLL_insert (tcc_head, tcc_tail, tcc);
277 #define MarshallPtr(ptr, base, type) \
279 ptr = (type *) ((char *) ptr - (char *) base)
283 MarshallWSAQUERYSETW (WSAQUERYSETW *qs, GUID *sc)
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++)
295 MarshallPtr (qs->lpcsaBuffer[i].LocalAddr.lpSockaddr, qs, SOCKADDR);
296 MarshallPtr (qs->lpcsaBuffer[i].RemoteAddr.lpSockaddr, qs, SOCKADDR);
298 MarshallPtr (qs->lpcsaBuffer, qs, CSADDR_INFO);
299 if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, sc) && qs->lpBlob != NULL && qs->lpBlob->pBlobData != NULL)
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);
312 MarshallPtr (qs->lpBlob, qs, BLOB);
317 process_lookup_result (void* cls, uint32_t rd_count,
318 const struct GNUNET_GNSRECORD_Data *rd)
321 struct request *rq = (struct request *) cls;
322 struct GNUNET_W32RESOLVER_GetMessage *msg;
323 struct GNUNET_MessageHeader *msgend;
329 size_t blobaddrcount = 0;
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;
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);
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 */
358 for (i = 0; i < rd_count; i++)
360 switch (rd[i].record_type)
362 case GNUNET_DNSPARSER_TYPE_A:
363 if (rd[i].data_size != sizeof (struct in_addr))
365 size += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in) * 2;
368 case GNUNET_DNSPARSER_TYPE_AAAA:
369 if (rd[i].data_size != sizeof (struct in6_addr))
371 size += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in6) * 2;
376 if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &rq->sc))
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++)
385 if ((rq->af == AF_INET || rq->af == AF_UNSPEC) && rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
387 blobsize += sizeof (void *);
388 blobsize += sizeof (struct in_addr);
391 else if (rq->af == AF_INET6 && rd[i].record_type == GNUNET_DNSPARSER_TYPE_AAAA)
393 blobsize += sizeof (void *);
394 blobsize += sizeof (struct in6_addr);
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;
440 for (i = 0; i < rd_count; i++)
442 switch (rd[i].record_type)
444 case GNUNET_DNSPARSER_TYPE_A:
445 if (rd[i].data_size != sizeof (struct in_addr))
447 qs->lpcsaBuffer[j].iSocketType = SOCK_STREAM;
448 qs->lpcsaBuffer[j].iProtocol = IPPROTO_TCP;
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;
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;
466 case GNUNET_DNSPARSER_TYPE_AAAA:
467 if (rd[i].data_size != sizeof (struct in6_addr))
469 qs->lpcsaBuffer[j].iSocketType = SOCK_STREAM;
470 qs->lpcsaBuffer[j].iProtocol = IPPROTO_TCP;
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;
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;
492 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
493 qs->dwOutputFlags = 0;
494 if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &rq->sc))
497 qs->lpBlob = (BLOB *) ptr;
498 ptr += sizeof (BLOB);
500 size_recalc += sizeof (BLOB);
501 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
503 qs->lpBlob->cbSize = blobsize;
504 qs->lpBlob->pBlobData = (BYTE *) ptr;
505 ptr += sizeof (struct hostent);
507 size_recalc += sizeof (struct hostent);
508 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
510 he = (struct hostent *) qs->lpBlob->pBlobData;
511 he->h_name = (char *) ptr;
512 ptr += strlen (rq->u8name) + 1;
514 size_recalc += strlen (rq->u8name) + 1;
515 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
517 strcpy (he->h_name, rq->u8name);
518 he->h_aliases = (char **) ptr;
519 ptr += sizeof (void *);
521 size_recalc += sizeof (void *); /* For aliases */
522 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
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);
530 size_recalc += sizeof (void *) * (blobaddrcount + 1); /* For addresses */
531 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
534 for (i = 0; i < rd_count; i++)
536 if ((rq->af == AF_INET || rq->af == AF_UNSPEC) &&
537 rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
539 he->h_addr_list[j] = (char *) ptr;
540 ptr += sizeof (struct in_addr);
542 size_recalc += sizeof (struct in_addr);
543 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
545 memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in_addr));
548 else if (rq->af == AF_INET6 && rd[i].record_type == GNUNET_DNSPARSER_TYPE_AAAA)
550 he->h_addr_list[j] = (char *) ptr;
551 ptr += sizeof (struct in6_addr);
553 size_recalc += sizeof (struct in6_addr);
554 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
556 memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in6_addr));
560 he->h_addr_list[j] = NULL;
562 msgend = GNUNET_new (struct GNUNET_MessageHeader);
564 msgend->type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
565 msgend->size = htons (sizeof (struct GNUNET_MessageHeader));
567 if ((char *) ptr - (char *) msg != size || size_recalc != size || size_recalc != ((char *) ptr - (char *) msg))
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);
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);
583 get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
584 const wchar_t *name, int af, GUID sc)
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;
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);
623 namelen = wcslen (name);
627 hostname = (char *) u16_to_u8 (name, namelen + 1, NULL, &strl);
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",
636 rq = GNUNET_new (struct request);
640 if (rq->af != AF_INET && rq->af != AF_INET6)
644 rq->name = GNUNET_malloc ((namelen + 1) * sizeof (wchar_t));
645 memcpy (rq->name, name, (namelen + 1) * sizeof (wchar_t));
646 rq->u8name = hostname;
649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
650 "Launching a lookup for client %p with rq %p\n",
653 rq->lookup_request = GNUNET_GNS_lookup (gns, hostname, &gns_master_pubkey,
654 rtype, GNUNET_NO /* Use DHT */, &gns_short_privkey, &process_lookup_result,
657 if (NULL != rq->lookup_request)
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);
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "Lookup was not launched, disconnecting the client\n");
668 GNUNET_free_non_null (rq->name);
672 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
678 * Handle GET-message.
681 * @param client identification of the client
682 * @param message the actual message
685 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
686 const struct GNUNET_MessageHeader *message)
689 const struct GNUNET_W32RESOLVER_GetMessage *msg;
693 const wchar_t *hostname;
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?
702 GNUNET_SERVER_receive_done (client, GNUNET_NO);
706 msize = ntohs (message->size);
707 if (msize <= sizeof (struct GNUNET_W32RESOLVER_GetMessage))
710 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
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]);
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')
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);
739 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
742 get_ip_from_hostname (client, hostname, af, sc);
747 * Method called to with the ego we are to use for shortening
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
759 identity_shorten_cb (void *cls,
760 struct GNUNET_IDENTITY_Ego *ego,
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 ();
772 gns_short_privkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
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.
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
789 identity_master_cb (void *cls,
790 struct GNUNET_IDENTITY_Ego *ego,
798 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
799 GNUNET_SCHEDULER_shutdown ();
802 GNUNET_IDENTITY_ego_get_public_key (ego, &gns_master_pubkey);
803 id_op = GNUNET_IDENTITY_get (identity, "gns-short", &identity_shorten_cb,
805 GNUNET_assert (NULL != id_op);
810 * Start up gns-helper-w32 service.
813 * @param server the initialized server
814 * @param cfg configuration to use
817 run (void *cls, struct GNUNET_SERVER_Handle *server,
818 const struct GNUNET_CONFIGURATION_Handle *cfg)
820 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
821 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_W32RESOLVER_REQUEST, 0},
825 gns = GNUNET_GNS_connect (cfg);
828 fprintf (stderr, _("Failed to connect to GNS\n"));
829 GNUNET_SCHEDULER_shutdown ();
832 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
835 identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
836 if (NULL == identity)
838 fprintf (stderr, _("Failed to connect to identity service\n"));
839 GNUNET_SCHEDULER_shutdown ();
843 id_op = GNUNET_IDENTITY_get (identity, "gns-master", &identity_master_cb,
845 GNUNET_assert (NULL != id_op);
847 GNUNET_SERVER_add_handlers (server, handlers);
852 * The main function for gns-helper-w32.
854 * @param argc number of arguments from the command line
855 * @param argv command line arguments
856 * @return 0 ok, 1 on error
859 main (int argc, char *const *argv)
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;
868 /* end of gnunet-gns-helper-service-w32.c */