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;
148 * Head of the doubly-linked list (for cleanup).
150 static struct TransmitCallbackContext *tcc_head;
153 * Tail of the doubly-linked list (for cleanup).
155 static struct TransmitCallbackContext *tcc_tail;
158 * Have we already cleaned up the TCCs and are hence no longer
159 * willing (or able) to transmit anything to anyone?
161 static int cleaning_done;
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.
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
176 transmit_callback (void *cls, size_t size, void *buf)
178 struct TransmitCallbackContext *tcc = cls;
182 GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
183 msize = ntohs (tcc->msg->size);
186 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
187 _("Transmission to client failed!\n"));
188 GNUNET_free (tcc->msg);
192 GNUNET_assert (size >= msize);
193 memcpy (buf, tcc->msg, msize);
194 GNUNET_free (tcc->msg);
201 * Transmit the given message to the client.
203 * @param client target of the message
204 * @param msg message to transmit, will be freed!
207 transmit (struct GNUNET_SERVER_Client *client,
208 struct GNUNET_MessageHeader *msg)
210 struct TransmitCallbackContext *tcc;
212 if (GNUNET_YES == cleaning_done)
214 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
215 _("Shutdown in progress, aborting transmission.\n"));
219 tcc = GNUNET_malloc (sizeof (struct TransmitCallbackContext));
223 GNUNET_SERVER_notify_transmit_ready (client,
225 GNUNET_TIME_UNIT_FOREVER_REL,
226 &transmit_callback, tcc)))
233 GNUNET_CONTAINER_DLL_insert (tcc_head, tcc_tail, tcc);
237 #define MarshallPtr(ptr, base, type) \
239 ptr = (type *) ((char *) ptr - (char *) base)
243 MarshallWSAQUERYSETW (WSAQUERYSETW *qs, GUID *sc)
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++)
255 MarshallPtr (qs->lpcsaBuffer[i].LocalAddr.lpSockaddr, qs, SOCKADDR);
256 MarshallPtr (qs->lpcsaBuffer[i].RemoteAddr.lpSockaddr, qs, SOCKADDR);
258 MarshallPtr (qs->lpcsaBuffer, qs, CSADDR_INFO);
259 if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, sc) && qs->lpBlob != NULL && qs->lpBlob->pBlobData != NULL)
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);
272 MarshallPtr (qs->lpBlob, qs, BLOB);
277 process_lookup_result (void* cls, uint32_t rd_count,
278 const struct GNUNET_GNSRECORD_Data *rd)
281 struct request *rq = (struct request *) cls;
282 struct GNUNET_W32RESOLVER_GetMessage *msg;
283 struct GNUNET_MessageHeader *msgend;
289 size_t blobaddrcount = 0;
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);
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);
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 */
316 for (i = 0; i < rd_count; i++)
318 switch (rd[i].record_type)
320 case GNUNET_DNSPARSER_TYPE_A:
321 if (rd[i].data_size != sizeof (struct in_addr))
323 size += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in) * 2;
326 case GNUNET_DNSPARSER_TYPE_AAAA:
327 if (rd[i].data_size != sizeof (struct in6_addr))
329 size += sizeof (CSADDR_INFO) + sizeof (struct sockaddr_in6) * 2;
334 if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &rq->sc))
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++)
343 if ((rq->af == AF_INET || rq->af == AF_UNSPEC) && rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
345 blobsize += sizeof (void *);
346 blobsize += sizeof (struct in_addr);
349 else if (rq->af == AF_INET6 && rd[i].record_type == GNUNET_DNSPARSER_TYPE_AAAA)
351 blobsize += sizeof (void *);
352 blobsize += sizeof (struct in6_addr);
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);
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;
401 for (i = 0; i < rd_count; i++)
403 switch (rd[i].record_type)
405 case GNUNET_DNSPARSER_TYPE_A:
406 if (rd[i].data_size != sizeof (struct in_addr))
408 qs->lpcsaBuffer[j].iSocketType = SOCK_STREAM;
409 qs->lpcsaBuffer[j].iProtocol = IPPROTO_TCP;
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;
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;
427 case GNUNET_DNSPARSER_TYPE_AAAA:
428 if (rd[i].data_size != sizeof (struct in6_addr))
430 qs->lpcsaBuffer[j].iSocketType = SOCK_STREAM;
431 qs->lpcsaBuffer[j].iProtocol = IPPROTO_TCP;
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;
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;
453 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
454 qs->dwOutputFlags = 0;
455 if (IsEqualGUID (&SVCID_INET_HOSTADDRBYNAME, &rq->sc))
458 qs->lpBlob = (BLOB *) ptr;
459 ptr += sizeof (BLOB);
461 size_recalc += sizeof (BLOB);
462 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
464 qs->lpBlob->cbSize = blobsize;
465 qs->lpBlob->pBlobData = (BYTE *) ptr;
466 ptr += sizeof (struct hostent);
468 size_recalc += sizeof (struct hostent);
469 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
471 he = (struct hostent *) qs->lpBlob->pBlobData;
472 he->h_name = (char *) ptr;
473 ptr += strlen (rq->u8name) + 1;
475 size_recalc += strlen (rq->u8name) + 1;
476 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
478 strcpy (he->h_name, rq->u8name);
479 he->h_aliases = (char **) ptr;
480 ptr += sizeof (void *);
482 size_recalc += sizeof (void *); /* For aliases */
483 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
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);
491 size_recalc += sizeof (void *) * (blobaddrcount + 1); /* For addresses */
492 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
495 for (i = 0; i < rd_count; i++)
497 if ((rq->af == AF_INET || rq->af == AF_UNSPEC) &&
498 rd[i].record_type == GNUNET_DNSPARSER_TYPE_A)
500 he->h_addr_list[j] = (char *) ptr;
501 ptr += sizeof (struct in_addr);
503 size_recalc += sizeof (struct in_addr);
504 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
506 memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in_addr));
509 else if (rq->af == AF_INET6 && rd[i].record_type == GNUNET_DNSPARSER_TYPE_AAAA)
511 he->h_addr_list[j] = (char *) ptr;
512 ptr += sizeof (struct in6_addr);
514 size_recalc += sizeof (struct in6_addr);
515 GNUNET_break (size_recalc == (size_t) ((char *) ptr - (char *) msg));
517 memcpy (he->h_addr_list[j], rd[i].data, sizeof (struct in6_addr));
521 he->h_addr_list[j] = NULL;
523 msgend = (struct GNUNET_MessageHeader *) ptr;
524 ptr += sizeof (struct GNUNET_MessageHeader);
525 size_recalc += sizeof (struct GNUNET_MessageHeader);
527 msgend->type = htons (GNUNET_MESSAGE_TYPE_W32RESOLVER_RESPONSE);
528 msgend->size = htons (sizeof (struct GNUNET_MessageHeader));
530 if ((char *) ptr - (char *) msg != size || size_recalc != size || size_recalc != ((char *) ptr - (char *) msg))
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);
534 MarshallWSAQUERYSETW (qs, &rq->sc);
535 transmit (rq->client, &msg->header);
536 GNUNET_free_non_null (rq->name);
544 get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
545 const wchar_t *name, int af, GUID sc)
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;
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);
584 namelen = wcslen (name);
588 hostname = (char *) u16_to_u8 (name, namelen + 1, NULL, &strl);
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",
597 rq = GNUNET_malloc (sizeof (struct request));
601 if (rq->af != AF_INET && rq->af != AF_INET6)
605 rq->name = GNUNET_malloc ((namelen + 1) * sizeof (wchar_t));
606 memcpy (rq->name, name, (namelen + 1) * sizeof (wchar_t));
607 rq->u8name = hostname;
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
611 "Launching a lookup for client %p with rq %p\n",
614 rq->lookup_request = GNUNET_GNS_lookup (gns, hostname, &gns_master_pubkey,
615 rtype, GNUNET_NO /* Use DHT */, &gns_short_privkey, &process_lookup_result,
618 if (NULL != rq->lookup_request)
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "Lookup launched, waiting for a reply\n");
622 GNUNET_SERVER_receive_done (client, GNUNET_OK);
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627 "Lookup was not launched, disconnecting the client\n");
628 GNUNET_free_non_null (rq->name);
632 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
638 * Handle GET-message.
641 * @param client identification of the client
642 * @param message the actual message
645 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
646 const struct GNUNET_MessageHeader *message)
649 const struct GNUNET_W32RESOLVER_GetMessage *msg;
654 const wchar_t *hostname;
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?
663 GNUNET_SERVER_receive_done (client, GNUNET_NO);
667 msize = ntohs (message->size);
668 if (msize < sizeof (struct GNUNET_W32RESOLVER_GetMessage))
671 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
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));
688 hostname = (const wchar_t *) &msg[1];
689 if (hostname[size - 1] != L'\0')
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "name of length %u, not 0-terminated: %*S\n",
692 size, size, hostname);
694 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
697 get_ip_from_hostname (client, hostname, af, sc);
702 * Method called to with the ego we are to use for shortening
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
714 identity_shorten_cb (void *cls,
715 struct GNUNET_IDENTITY_Ego *ego,
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 ();
727 gns_short_privkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
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.
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
744 identity_master_cb (void *cls,
745 struct GNUNET_IDENTITY_Ego *ego,
753 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
754 GNUNET_SCHEDULER_shutdown ();
757 GNUNET_IDENTITY_ego_get_public_key (ego, &gns_master_pubkey);
758 id_op = GNUNET_IDENTITY_get (identity, "gns-short", &identity_shorten_cb,
760 GNUNET_assert (NULL != id_op);
765 * Start up gns-helper-w32 service.
768 * @param server the initialized server
769 * @param cfg configuration to use
772 run (void *cls, struct GNUNET_SERVER_Handle *server,
773 const struct GNUNET_CONFIGURATION_Handle *cfg)
775 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
776 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_W32RESOLVER_REQUEST, 0},
780 gns = GNUNET_GNS_connect (cfg);
783 fprintf (stderr, _("Failed to connect to GNS\n"));
784 GNUNET_SCHEDULER_shutdown ();
787 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
790 identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
791 if (NULL == identity)
793 fprintf (stderr, _("Failed to connect to identity service\n"));
794 GNUNET_SCHEDULER_shutdown ();
798 id_op = GNUNET_IDENTITY_get (identity, "gns-master", &identity_master_cb,
800 GNUNET_assert (NULL != id_op);
802 GNUNET_SERVER_add_handlers (server, handlers);
807 * The main function for gns-helper-w32.
809 * @param argc number of arguments from the command line
810 * @param argv command line arguments
811 * @return 0 ok, 1 on error
814 main (int argc, char *const *argv)
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;
823 /* end of gnunet-gns-helper-service-w32.c */