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"
36 * Maximum supported length for a hostname
38 #define MAX_HOSTNAME 1024
42 * Possible hostnames for "loopback".
44 static const char *loopback[] = {
52 * Handle to a request given to the resolver. Can be used to cancel
53 * the request prior to the timeout or successful execution. Also
54 * used to track our internal state for the request.
56 struct GNUNET_RESOLVER_RequestHandle
60 * Callback if this is an name resolution request,
63 GNUNET_RESOLVER_AddressCallback addr_callback;
66 * Callback if this is a reverse lookup request,
69 GNUNET_RESOLVER_HostnameCallback name_callback;
72 * Closure for the respective "callback".
77 * Our connection to the resolver service.
79 struct GNUNET_CLIENT_Connection *client;
84 struct GNUNET_SCHEDULER_Handle *sched;
87 * Name of the host that we are resolving.
92 * When should this request time out?
94 struct GNUNET_TIME_Absolute timeout;
97 * Task handle for numeric lookups.
99 GNUNET_SCHEDULER_TaskIdentifier task;
102 * Desired address family.
107 * Length of the "struct sockaddr" that follows this
108 * struct (only for reverse lookup).
115 * Check that the resolver service runs on localhost
119 check_config (const struct GNUNET_CONFIGURATION_Handle *cfg)
123 struct sockaddr_in v4;
124 struct sockaddr_in6 v6;
126 memset (&v4, 0, sizeof (v4));
127 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
128 v4.sin_family = AF_INET;
129 #if HAVE_SOCKADDR_IN_SIN_LEN
130 v4.sin_len = sizeof (v4);
132 memset (&v6, 0, sizeof (v6));
133 v6.sin6_family = AF_INET6;
134 #if HAVE_SOCKADDR_IN_SIN_LEN
135 v6.sin6_len = sizeof (v6);
138 GNUNET_CONFIGURATION_get_value_string (cfg,
140 "HOSTNAME", &hostname))
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
143 _("Must specify `%s' for `%s' in configuration!\n"),
144 "HOSTNAME", "resolver");
147 if ((1 != inet_pton (AF_INET,
149 &v4)) || (1 != inet_pton (AF_INET6, hostname, &v6)))
151 GNUNET_free (hostname);
155 while (loopback[i] != NULL)
156 if (0 == strcasecmp (loopback[i++], hostname))
158 GNUNET_free (hostname);
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
163 ("Must specify `%s' or numeric IP address for `%s' of `%s' in configuration!\n"),
164 "localhost", "HOSTNAME", "resolver");
165 GNUNET_free (hostname);
171 * Convert IP address to string without DNS resolution.
173 * @param sa the address
174 * @param salen number of bytes in sa
175 * @return address as a string, NULL on error
178 no_resolve (const struct sockaddr *sa, socklen_t salen)
181 char inet4[INET_ADDRSTRLEN];
182 char inet6[INET6_ADDRSTRLEN];
184 if (salen < sizeof (struct sockaddr))
186 switch (sa->sa_family)
189 if (salen != sizeof (struct sockaddr_in))
192 &((struct sockaddr_in *) sa)->sin_addr,
193 inet4, INET_ADDRSTRLEN);
194 ret = GNUNET_strdup (inet4);
197 if (salen != sizeof (struct sockaddr_in6))
200 &((struct sockaddr_in6 *) sa)->sin6_addr,
201 inet6, INET6_ADDRSTRLEN);
202 ret = GNUNET_strdup (inet6);
213 * Process the reply from the resolver (which is presumably
214 * the numeric IP address for a name resolution request).
216 * @param cls the "GNUNET_RESOLVER_RequestHandle" for which this is a reply
217 * @param msg reply from the resolver or NULL on error
220 handle_address_response (void *cls, const struct GNUNET_MessageHeader *msg)
222 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
224 const struct sockaddr *sa;
230 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
231 _("Timeout trying to resolve hostname `%s'.\n"),
233 rh->addr_callback (rh->cls, NULL, 0);
234 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
238 if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
241 rh->addr_callback (rh->cls, NULL, 0);
242 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
247 size = ntohs (msg->size);
248 if (size == sizeof (struct GNUNET_MessageHeader))
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 _("Received end message resolving hostname `%s'.\n"),
255 rh->addr_callback (rh->cls, NULL, 0);
256 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
260 sa = (const struct sockaddr *) &msg[1];
261 salen = size - sizeof (struct GNUNET_MessageHeader);
262 if (salen < sizeof (struct sockaddr))
265 rh->addr_callback (rh->cls, NULL, 0);
266 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
272 char *ips = no_resolve (sa, salen);
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "Resolver returns `%s' for `%s'.\n", ips,
279 rh->addr_callback (rh->cls, sa, salen);
280 GNUNET_CLIENT_receive (rh->client,
281 &handle_address_response,
283 GNUNET_TIME_absolute_get_remaining (rh->timeout));
288 * We've been asked to lookup the address for a hostname and were
289 * given a valid numeric string. Perform the callbacks for the
292 * @param cls struct GNUNET_RESOLVER_RequestHandle for the request
293 * @param tc unused scheduler context
296 numeric_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
298 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
299 struct sockaddr_in v4;
300 struct sockaddr_in6 v6;
302 memset (&v4, 0, sizeof (v4));
303 v4.sin_family = AF_INET;
304 #if HAVE_SOCKADDR_IN_SIN_LEN
305 v4.sin_len = sizeof (v4);
307 memset (&v6, 0, sizeof (v6));
308 v6.sin6_family = AF_INET6;
309 #if HAVE_SOCKADDR_IN_SIN_LEN
310 v6.sin6_len = sizeof (v6);
313 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET)) &&
314 (1 == inet_pton (AF_INET, rh->hostname, &v4.sin_addr)))
316 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
317 if ((rh->domain == AF_UNSPEC) &&
318 (1 == inet_pton (AF_INET6, rh->hostname, &v6.sin6_addr)))
320 /* this can happen on some systems IF "hostname" is "localhost" */
321 rh->addr_callback (rh->cls,
322 (const struct sockaddr *) &v6, sizeof (v6));
324 rh->addr_callback (rh->cls, NULL, 0);
328 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET6)) &&
329 (1 == inet_pton (AF_INET6, rh->hostname, &v6.sin6_addr)))
331 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
332 rh->addr_callback (rh->cls, NULL, 0);
336 /* why are we here? this task should not have been scheduled! */
344 * We've been asked to lookup the address for a hostname and were
345 * given a variant of "loopback". Perform the callbacks for the
346 * respective loopback numeric addresses.
348 * @param cls struct GNUNET_RESOLVER_RequestHandle for the request
349 * @param tc unused scheduler context
352 loopback_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
354 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
355 struct sockaddr_in v4;
356 struct sockaddr_in6 v6;
358 memset (&v4, 0, sizeof (v4));
359 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
360 v4.sin_family = AF_INET;
361 #if HAVE_SOCKADDR_IN_SIN_LEN
362 v4.sin_len = sizeof (v4);
364 memset (&v6, 0, sizeof (v6));
365 v6.sin6_family = AF_INET6;
366 #if HAVE_SOCKADDR_IN_SIN_LEN
367 v6.sin6_len = sizeof (v6);
369 v6.sin6_addr = in6addr_loopback;
373 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
376 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
379 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
380 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
386 rh->addr_callback (rh->cls, NULL, 0);
392 * Convert a string to one or more IP addresses.
394 * @param sched scheduler to use
395 * @param cfg configuration to use
396 * @param hostname the hostname to resolve
397 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
398 * @param callback function to call with addresses
399 * @param callback_cls closure for callback
400 * @param timeout how long to try resolving
401 * @return handle that can be used to cancel the request, NULL on error
403 struct GNUNET_RESOLVER_RequestHandle *
404 GNUNET_RESOLVER_ip_get (struct GNUNET_SCHEDULER_Handle *sched,
405 const struct GNUNET_CONFIGURATION_Handle *cfg,
406 const char *hostname,
408 struct GNUNET_TIME_Relative timeout,
409 GNUNET_RESOLVER_AddressCallback callback,
412 struct GNUNET_CLIENT_Connection *client;
413 struct GNUNET_RESOLVER_GetMessage *msg;
414 struct GNUNET_RESOLVER_RequestHandle *rh;
419 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
422 slen = strlen (hostname) + 1;
423 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
424 GNUNET_SERVER_MAX_MESSAGE_SIZE)
429 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen);
432 rh->addr_callback = callback;
433 rh->cls = callback_cls;
434 memcpy (&rh[1], hostname, slen);
435 rh->hostname = (const char *) &rh[1];
436 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
438 /* first, check if this is a numeric address */
439 if (((1 == inet_pton (AF_INET,
442 ((domain == AF_INET) || (domain == AF_UNSPEC))) ||
443 ((1 == inet_pton (AF_INET6,
446 ((domain == AF_INET6) || (domain == AF_UNSPEC))))
448 rh->task = GNUNET_SCHEDULER_add_now (sched,
449 &numeric_resolution, rh);
452 /* then, check if this is a loopback address */
454 while (loopback[i] != NULL)
455 if (0 == strcasecmp (loopback[i++], hostname))
457 rh->task = GNUNET_SCHEDULER_add_now (sched,
458 &loopback_resolution, rh);
462 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
470 msg = (struct GNUNET_RESOLVER_GetMessage *) buf;
472 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
473 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
474 msg->direction = htonl (GNUNET_NO);
475 msg->domain = htonl (domain);
476 memcpy (&msg[1], hostname, slen);
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 _("Resolver requests DNS resolution of hostname `%s'.\n"),
484 GNUNET_CLIENT_transmit_and_get_response (client,
488 &handle_address_response, rh))
491 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
499 * Process response with a hostname for a reverse DNS lookup.
501 * @param cls our "struct GNUNET_RESOLVER_RequestHandle" context
502 * @param msg message with the hostname, NULL on error
505 handle_hostname_response (void *cls, const struct GNUNET_MessageHeader *msg)
507 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
509 const char *hostname;
513 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
514 _("Timeout trying to resolve IP address `%s'.\n"),
515 GNUNET_a2s ((const void*) &rh[1], rh->salen));
516 rh->name_callback (rh->cls, NULL);
517 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
521 size = ntohs (msg->size);
522 if (size == sizeof (struct GNUNET_MessageHeader))
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 _("Received end message resolving IP address `%s'.\n"),
527 GNUNET_a2s ((const void*) &rh[1], rh->salen));
529 rh->name_callback (rh->cls, NULL);
530 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
534 hostname = (const char *) &msg[1];
535 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
538 rh->name_callback (rh->cls, NULL);
539 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 _("Resolver returns `%s' for IP `%s'.\n"),
547 GNUNET_a2s ((const void*) &rh[1], rh->salen));
549 rh->name_callback (rh->cls, hostname);
550 GNUNET_CLIENT_receive (rh->client,
551 &handle_hostname_response,
553 GNUNET_TIME_absolute_get_remaining (rh->timeout));
559 * We've been asked to convert an address to a string without
560 * a reverse lookup. Do it.
562 * @param cls struct GNUNET_RESOLVER_RequestHandle for the request
563 * @param tc unused scheduler context
566 numeric_reverse (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
568 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
571 result = no_resolve ((const struct sockaddr *) &rh[1], rh->salen);
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), result);
577 rh->name_callback (rh->cls, result);
578 GNUNET_free (result);
580 rh->name_callback (rh->cls, NULL);
587 * Get an IP address as a string.
589 * @param sched scheduler to use
590 * @param cfg configuration to use
591 * @param sa host address
592 * @param salen length of host address
593 * @param do_resolve use GNUNET_NO to return numeric hostname
594 * @param timeout how long to try resolving
595 * @param callback function to call with hostnames
596 * @param cls closure for callback
597 * @return handle that can be used to cancel the request
599 struct GNUNET_RESOLVER_RequestHandle *
600 GNUNET_RESOLVER_hostname_get (struct GNUNET_SCHEDULER_Handle *sched,
601 const struct GNUNET_CONFIGURATION_Handle *cfg,
602 const struct sockaddr *sa,
605 struct GNUNET_TIME_Relative timeout,
606 GNUNET_RESOLVER_HostnameCallback callback,
609 struct GNUNET_CLIENT_Connection *client;
610 struct GNUNET_RESOLVER_GetMessage *msg;
611 struct GNUNET_RESOLVER_RequestHandle *rh;
612 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
615 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + salen);
616 rh->name_callback = callback;
618 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
621 memcpy (&rh[1], sa, salen);
623 if (GNUNET_NO == do_resolve)
625 rh->task = GNUNET_SCHEDULER_add_now (sched,
626 &numeric_reverse, rh);
629 if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
630 GNUNET_SERVER_MAX_MESSAGE_SIZE)
636 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
644 msg = (struct GNUNET_RESOLVER_GetMessage *) buf;
646 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
647 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
648 msg->direction = htonl (GNUNET_YES);
649 msg->domain = htonl (sa->sa_family);
650 memcpy (&msg[1], sa, salen);
652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
653 _("Resolver requests DNS resolution of IP address.\n"));
656 GNUNET_CLIENT_transmit_and_get_response (client,
660 &handle_hostname_response, rh))
662 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
671 * Perform a reverse DNS lookup.
673 * @param sched scheduler to use
674 * @param cfg configuration to use
675 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
676 * @param callback function to call with addresses
677 * @param cls closure for callback
678 * @param timeout how long to try resolving
679 * @return handle that can be used to cancel the request, NULL on error
681 struct GNUNET_RESOLVER_RequestHandle *
682 GNUNET_RESOLVER_hostname_resolve (struct GNUNET_SCHEDULER_Handle *sched,
683 const struct GNUNET_CONFIGURATION_Handle
685 struct GNUNET_TIME_Relative timeout,
686 GNUNET_RESOLVER_AddressCallback callback,
689 char hostname[MAX_HOSTNAME];
692 if (0 != gethostname (hostname, sizeof (hostname) - 1))
694 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
695 GNUNET_ERROR_TYPE_BULK, "gethostname");
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
700 _("Resolving our hostname `%s'\n"), hostname);
702 return GNUNET_RESOLVER_ip_get (sched,
703 cfg, hostname, domain, timeout, callback,
709 * Cancel a request that is still pending with the resolver.
710 * Note that a client MUST NOT cancel a request that has
711 * been completed (i.e, the callback has been called to
712 * signal timeout or the final result).
714 * @param h handle of request to cancel
717 GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *h)
719 if (h->client != NULL)
720 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
721 if (h->task != GNUNET_SCHEDULER_NO_TASK)
722 GNUNET_SCHEDULER_cancel (h->sched, h->task);
728 /* end of resolver_api.c */