2 This file is part of GNUnet.
3 (C) 2009 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 2, 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.
22 * @file util/resolver_api.c
23 * @brief resolver for writing a tool
24 * @author Christian Grothoff
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_client_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_resolver_service.h"
31 #include "gnunet_server_lib.h"
38 struct GetAddressContext
44 GNUNET_RESOLVER_AddressCallback callback;
47 * Closure for "callback".
54 struct GNUNET_RESOLVER_GetMessage *msg;
59 struct GNUNET_CLIENT_Connection *client;
64 struct GNUNET_TIME_Absolute timeout;
69 * Possible hostnames for "loopback".
71 static const char *loopback[] = {
79 * Check that the resolver service runs on localhost
83 check_config (const struct GNUNET_CONFIGURATION_Handle *cfg)
91 GNUNET_CONFIGURATION_get_value_string (cfg,
96 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
97 _("Must specify `%s' for `%s' in configuration!\n"),
102 if ( (0 == inet_pton (AF_INET,
105 (0 == inet_pton (AF_INET6,
110 while (loopback[i] != NULL)
111 if (0 == strcmp (loopback[i++], hostname))
113 GNUNET_free (hostname);
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
117 _("Must specify `%s' for `%s' in configuration!\n"),
120 GNUNET_free (hostname);
126 * Convert IP address to string without DNS resolution.
128 * @param sa the address
129 * @param salen number of bytes in sa
130 * @return address as a string, NULL on error
133 no_resolve (const struct sockaddr *sa, socklen_t salen)
136 char inet4[INET_ADDRSTRLEN];
137 char inet6[INET6_ADDRSTRLEN];
139 if (salen < sizeof (struct sockaddr))
141 switch (sa->sa_family)
144 if (salen != sizeof (struct sockaddr_in))
147 &((struct sockaddr_in *) sa)->sin_addr,
148 inet4, INET_ADDRSTRLEN);
149 ret = GNUNET_strdup (inet4);
152 if (salen != sizeof (struct sockaddr_in6))
155 &((struct sockaddr_in6 *) sa)->sin6_addr,
156 inet6, INET6_ADDRSTRLEN);
157 ret = GNUNET_strdup (inet6);
174 handle_address_response (void *cls, const struct GNUNET_MessageHeader *msg)
176 struct GetAddressContext *gac = cls;
178 const struct sockaddr *sa;
184 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
185 _("Timeout trying to resolve hostname.\n"));
186 gac->callback (gac->cls, NULL, 0);
187 GNUNET_CLIENT_disconnect (gac->client);
191 if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
194 gac->callback (gac->cls, NULL, 0);
195 GNUNET_CLIENT_disconnect (gac->client);
200 size = ntohs (msg->size);
201 if (size == sizeof (struct GNUNET_MessageHeader))
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205 _("Received end message resolving hostname.\n"));
207 gac->callback (gac->cls, NULL, 0);
208 GNUNET_CLIENT_disconnect (gac->client);
212 sa = (const struct sockaddr *) &msg[1];
213 salen = size - sizeof (struct GNUNET_MessageHeader);
214 if (salen < sizeof (struct sockaddr))
217 gac->callback (gac->cls, NULL, 0);
218 GNUNET_CLIENT_disconnect (gac->client);
224 char *ips = no_resolve (sa, salen);
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), ips);
229 gac->callback (gac->cls, sa, salen);
230 GNUNET_CLIENT_receive (gac->client,
231 &handle_address_response,
233 GNUNET_TIME_absolute_get_remaining (gac->timeout));
241 * @param size number of bytes available in buf
242 * @param buf target buffer, NULL on error
243 * @return number of bytes written to buf
246 transmit_get_ip (void *cls, size_t size, void *buf)
248 struct GetAddressContext *actx = cls;
253 /* timeout / error */
254 GNUNET_free (actx->msg);
255 actx->callback (actx->cls, NULL, 0);
256 GNUNET_CLIENT_disconnect (actx->client);
260 ms = ntohs (actx->msg->header.size);
261 GNUNET_assert (size >= ms);
262 memcpy (buf, actx->msg, ms);
263 GNUNET_free (actx->msg);
265 GNUNET_CLIENT_receive (actx->client,
266 &handle_address_response,
268 GNUNET_TIME_absolute_get_remaining (actx->timeout));
275 * Convert a string to one or more IP addresses.
277 * @param sched scheduler to use
278 * @param cfg configuration to use
279 * @param hostname the hostname to resolve
280 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
281 * @param callback function to call with addresses
282 * @param callback_cls closure for callback
283 * @param timeout how long to try resolving
286 GNUNET_RESOLVER_ip_get (struct GNUNET_SCHEDULER_Handle *sched,
287 const struct GNUNET_CONFIGURATION_Handle *cfg,
288 const char *hostname,
290 struct GNUNET_TIME_Relative timeout,
291 GNUNET_RESOLVER_AddressCallback callback,
294 struct GNUNET_CLIENT_Connection *client;
295 struct GNUNET_RESOLVER_GetMessage *msg;
296 struct GetAddressContext *actx;
299 struct sockaddr_in v4;
300 struct sockaddr_in6 v6;
302 memset (&v4, 0, sizeof(v4));
303 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
304 v4.sin_family = AF_INET;
305 #if HAVE_SOCKADDR_IN_SIN_LEN
306 v4.sin_len = sizeof(v4);
308 memset (&v6, 0, sizeof(v6));
309 v6.sin6_family = AF_INET6;
310 #if HAVE_SOCKADDR_IN_SIN_LEN
311 v6.sin6_len = sizeof(v6);
313 /* first, check if this is a numeric address */
314 if ( ( (domain == AF_UNSPEC) || (domain == AF_INET) ) &&
315 (0 == inet_pton (AF_INET,
319 callback (callback_cls,
320 (const struct sockaddr*) &v4,
322 if ( (domain == AF_UNSPEC) &&
323 (0 == inet_pton (AF_INET6,
327 /* this can happen on some systems IF "hostname" is "localhost" */
328 callback (callback_cls,
329 (const struct sockaddr*) &v6,
332 callback (callback_cls, NULL, 0);
335 if ( ( (domain == AF_UNSPEC) ||(domain == AF_INET) ) &&
336 (0 == inet_pton (AF_INET6,
340 callback (callback_cls,
341 (const struct sockaddr*) &v6,
343 callback (callback_cls, NULL, 0);
347 /* then, check if this is a loopback address */
349 while (loopback[i] != NULL)
350 if (0 == strcmp (loopback[i++], hostname))
352 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
353 v6.sin6_addr = in6addr_loopback;
357 callback (callback_cls,
358 (const struct sockaddr*) &v4,
362 callback (callback_cls,
363 (const struct sockaddr*) &v6,
367 callback (callback_cls,
368 (const struct sockaddr*) &v6,
370 callback (callback_cls,
371 (const struct sockaddr*) &v4,
375 callback (callback_cls, NULL, 0);
378 slen = strlen (hostname) + 1;
379 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
380 GNUNET_SERVER_MAX_MESSAGE_SIZE)
383 callback (callback_cls, NULL, 0);
386 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
389 callback (callback_cls, NULL, 0);
392 msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
394 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
395 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
396 msg->direction = htonl (GNUNET_NO);
397 msg->domain = htonl (domain);
398 memcpy (&msg[1], hostname, slen);
399 actx = GNUNET_malloc (sizeof (struct GetAddressContext));
400 actx->callback = callback;
401 actx->cls = callback_cls;
402 actx->client = client;
403 actx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 _("Resolver requests DNS resolution of hostname `%s'.\n"),
412 GNUNET_CLIENT_notify_transmit_ready (client,
415 GNUNET_RESOLVER_GetMessage),
416 timeout, &transmit_get_ip, actx))
420 callback (callback_cls, NULL, 0);
421 GNUNET_CLIENT_disconnect (client);
430 struct GetHostnameContext
436 GNUNET_RESOLVER_HostnameCallback callback;
446 struct GNUNET_RESOLVER_GetMessage *msg;
451 struct GNUNET_CLIENT_Connection *client;
456 struct GNUNET_TIME_Absolute timeout;
467 handle_hostname_response (void *cls, const struct GNUNET_MessageHeader *msg)
469 struct GetHostnameContext *ghc = cls;
471 const char *hostname;
475 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
476 _("Timeout trying to resolve IP address.\n"));
477 ghc->callback (ghc->cls, NULL);
478 GNUNET_CLIENT_disconnect (ghc->client);
482 size = ntohs (msg->size);
483 if (size == sizeof (struct GNUNET_MessageHeader))
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 _("Received end message resolving IP address.\n"));
489 ghc->callback (ghc->cls, NULL);
490 GNUNET_CLIENT_disconnect (ghc->client);
494 hostname = (const char *) &msg[1];
495 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
498 ghc->callback (ghc->cls, NULL);
499 GNUNET_CLIENT_disconnect (ghc->client);
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 _("Resolver returns `%s'.\n"), hostname);
507 ghc->callback (ghc->cls, hostname);
508 GNUNET_CLIENT_receive (ghc->client,
509 &handle_hostname_response,
511 GNUNET_TIME_absolute_get_remaining (ghc->timeout));
519 * @param size number of bytes available in buf
520 * @param buf target buffer, NULL on error
521 * @return number of bytes written to buf
524 transmit_get_hostname (void *cls, size_t size, void *buf)
526 struct GetHostnameContext *hctx = cls;
531 GNUNET_free (hctx->msg);
532 hctx->callback (hctx->cls, NULL);
533 GNUNET_CLIENT_disconnect (hctx->client);
537 msize = ntohs (hctx->msg->header.size);
538 GNUNET_assert (size >= msize);
539 memcpy (buf, hctx->msg, msize);
540 GNUNET_free (hctx->msg);
542 GNUNET_CLIENT_receive (hctx->client,
543 &handle_hostname_response,
545 GNUNET_TIME_absolute_get_remaining (hctx->timeout));
553 * Get an IP address as a string.
555 * @param sched scheduler to use
556 * @param cfg configuration to use
557 * @param sa host address
558 * @param salen length of host address
559 * @param do_resolve use GNUNET_NO to return numeric hostname
560 * @param timeout how long to try resolving
561 * @param callback function to call with hostnames
562 * @param cls closure for callback
565 GNUNET_RESOLVER_hostname_get (struct GNUNET_SCHEDULER_Handle *sched,
566 const struct GNUNET_CONFIGURATION_Handle *cfg,
567 const struct sockaddr *sa,
570 struct GNUNET_TIME_Relative timeout,
571 GNUNET_RESOLVER_HostnameCallback callback,
575 struct GNUNET_CLIENT_Connection *client;
576 struct GNUNET_RESOLVER_GetMessage *msg;
577 struct GetHostnameContext *hctx;
580 if (GNUNET_NO == do_resolve)
582 result = no_resolve (sa, salen);
584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
585 _("Resolver returns `%s'.\n"), result);
587 callback (cls, result);
590 GNUNET_free (result);
591 callback (cls, NULL);
595 if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
596 GNUNET_SERVER_MAX_MESSAGE_SIZE)
599 callback (cls, NULL);
602 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
605 callback (cls, NULL);
608 msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
610 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
611 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
612 msg->direction = htonl (GNUNET_YES);
613 msg->domain = htonl (sa->sa_family);
614 memcpy (&msg[1], sa, salen);
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617 _("Resolver requests DNS resolution of IP address.\n"));
619 hctx = GNUNET_malloc (sizeof (struct GetHostnameContext));
620 hctx->callback = callback;
622 hctx->client = client;
623 hctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
626 GNUNET_CLIENT_notify_transmit_ready (client,
628 GNUNET_RESOLVER_GetMessage)
630 &transmit_get_hostname, hctx))
633 callback (cls, NULL);
634 GNUNET_CLIENT_disconnect (client);
640 * Maximum supported length of hostname
642 #define MAX_HOSTNAME 1024
646 * Resolve our hostname to an IP address.
648 * @param sched scheduler to use
649 * @param cfg configuration to use
650 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
651 * @param callback function to call with addresses
652 * @param cls closure for callback
653 * @param timeout how long to try resolving
656 GNUNET_RESOLVER_hostname_resolve (struct GNUNET_SCHEDULER_Handle *sched,
657 const struct GNUNET_CONFIGURATION_Handle *cfg,
659 struct GNUNET_TIME_Relative timeout,
660 GNUNET_RESOLVER_AddressCallback callback,
663 char hostname[MAX_HOSTNAME];
666 if (0 != gethostname (hostname, sizeof (hostname) - 1))
668 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
669 GNUNET_ERROR_TYPE_BULK, "gethostname");
670 callback (cls, NULL, 0);
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 _("Resolving our hostname `%s'\n"), hostname);
677 GNUNET_RESOLVER_ip_get (sched,
678 cfg, hostname, domain, timeout, callback, cls);
684 /* end of resolver_api.c */