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_os_lib.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_container_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_resolver_service.h"
33 #include "gnunet_server_lib.h"
37 * Maximum supported length for a hostname
39 #define MAX_HOSTNAME 1024
43 * Possible hostnames for "loopback".
45 static const char *loopback[] = {
55 static const struct GNUNET_CONFIGURATION_Handle *cfg;
58 * Our connection to the resolver service, created on-demand, but then
59 * persists until error or shutdown.
61 static struct GNUNET_CLIENT_Connection *client;
64 * Head of DLL of requests.
66 static struct GNUNET_RESOLVER_RequestHandle *req_head;
69 * Tail of DLL of requests.
71 static struct GNUNET_RESOLVER_RequestHandle *req_tail;
74 * How long should we wait to reconnect?
76 static struct GNUNET_TIME_Relative backoff;
79 * Task for reconnecting.
81 static GNUNET_SCHEDULER_TaskIdentifier r_task;
84 * Task ID of shutdown task; only present while we have a
85 * connection to the resolver service.
87 static GNUNET_SCHEDULER_TaskIdentifier s_task;
91 * Handle to a request given to the resolver. Can be used to cancel
92 * the request prior to the timeout or successful execution. Also
93 * used to track our internal state for the request.
95 struct GNUNET_RESOLVER_RequestHandle
99 * Next entry in DLL of requests.
101 struct GNUNET_RESOLVER_RequestHandle *next;
104 * Previous entry in DLL of requests.
106 struct GNUNET_RESOLVER_RequestHandle *prev;
109 * Callback if this is an name resolution request,
112 GNUNET_RESOLVER_AddressCallback addr_callback;
115 * Callback if this is a reverse lookup request,
118 GNUNET_RESOLVER_HostnameCallback name_callback;
121 * Closure for the respective "callback".
126 * When should this request time out?
128 struct GNUNET_TIME_Absolute timeout;
131 * Task handle for numeric lookups.
133 GNUNET_SCHEDULER_TaskIdentifier task;
136 * Desired address family.
141 * Has this request been transmitted to the service?
146 * Did we add this request to the queue?
151 * Desired direction (IP to name or name to IP)
156 * Length of the data that follows this struct.
163 * Check that the resolver service runs on localhost
167 check_config (const struct GNUNET_CONFIGURATION_Handle *cfg)
171 struct sockaddr_in v4;
172 struct sockaddr_in6 v6;
174 memset (&v4, 0, sizeof (v4));
175 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
176 v4.sin_family = AF_INET;
177 #if HAVE_SOCKADDR_IN_SIN_LEN
178 v4.sin_len = sizeof (v4);
180 memset (&v6, 0, sizeof (v6));
181 v6.sin6_family = AF_INET6;
182 #if HAVE_SOCKADDR_IN_SIN_LEN
183 v6.sin6_len = sizeof (v6);
186 GNUNET_CONFIGURATION_get_value_string (cfg,
188 "HOSTNAME", &hostname))
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
191 _("Must specify `%s' for `%s' in configuration!\n"),
192 "HOSTNAME", "resolver");
195 if ((1 != inet_pton (AF_INET,
197 &v4)) || (1 != inet_pton (AF_INET6, hostname, &v6)))
199 GNUNET_free (hostname);
203 while (loopback[i] != NULL)
204 if (0 == strcasecmp (loopback[i++], hostname))
206 GNUNET_free (hostname);
209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
211 ("Must specify `%s' or numeric IP address for `%s' of `%s' in configuration!\n"),
212 "localhost", "HOSTNAME", "resolver");
213 GNUNET_free (hostname);
219 * Create the connection to the resolver service.
221 * @param c configuration to use
224 GNUNET_RESOLVER_connect (const struct GNUNET_CONFIGURATION_Handle *c)
232 * Destroy the connection to the resolver service.
235 GNUNET_RESOLVER_disconnect ()
237 GNUNET_assert (NULL == req_head);
238 GNUNET_assert (NULL == req_tail);
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243 "Disconnecting from DNS service\n");
245 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
248 if (r_task != GNUNET_SCHEDULER_NO_TASK)
250 GNUNET_SCHEDULER_cancel (r_task);
251 r_task = GNUNET_SCHEDULER_NO_TASK;
253 if (s_task != GNUNET_SCHEDULER_NO_TASK)
255 GNUNET_SCHEDULER_cancel (s_task);
256 s_task = GNUNET_SCHEDULER_NO_TASK;
262 * Convert IP address to string without DNS resolution.
264 * @param sa the address
265 * @param salen number of bytes in sa
266 * @return address as a string, NULL on error
269 no_resolve (const struct sockaddr *sa, socklen_t salen)
272 char inet4[INET_ADDRSTRLEN];
273 char inet6[INET6_ADDRSTRLEN];
275 if (salen < sizeof (struct sockaddr))
277 switch (sa->sa_family)
280 if (salen != sizeof (struct sockaddr_in))
284 &((struct sockaddr_in *) sa)->sin_addr,
285 inet4, INET_ADDRSTRLEN))
287 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
290 ret = GNUNET_strdup (inet4);
293 if (salen != sizeof (struct sockaddr_in6))
297 &((struct sockaddr_in6 *) sa)->sin6_addr,
298 inet6, INET6_ADDRSTRLEN))
300 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
303 ret = GNUNET_strdup (inet6);
314 * Adjust exponential back-off and reconnect to the service.
321 * Process pending requests to the resolver.
323 * @param h handle to the resolver
330 * Process response with a hostname for a DNS lookup.
332 * @param cls our "struct GNUNET_RESOLVER_RequestHandle" context
333 * @param msg message with the hostname, NULL on error
336 handle_response (void *cls,
337 const struct GNUNET_MessageHeader *msg)
339 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
341 const char *hostname;
342 const struct sockaddr *sa;
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 "Receiving response from DNS service\n");
351 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
352 _("Timeout trying to resolve IP address `%s'.\n"),
353 GNUNET_a2s ((const void*) &rh[1], rh->data_len));
354 if (rh->was_transmitted != GNUNET_SYSERR)
356 if (NULL != rh->name_callback)
357 rh->name_callback (rh->cls, NULL);
358 if (NULL != rh->addr_callback)
359 rh->addr_callback (rh->cls, NULL, 0);
361 GNUNET_CONTAINER_DLL_remove (req_head,
365 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
370 if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
373 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
378 size = ntohs (msg->size);
379 if (size == sizeof (struct GNUNET_MessageHeader))
381 if (rh->was_transmitted != GNUNET_SYSERR)
383 if (NULL != rh->name_callback)
384 rh->name_callback (rh->cls, NULL);
385 if (NULL != rh->addr_callback)
386 rh->addr_callback (rh->cls, NULL, 0);
388 GNUNET_CONTAINER_DLL_remove (req_head,
395 if (NULL != rh->name_callback)
397 hostname = (const char *) &msg[1];
398 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
401 if (rh->was_transmitted != GNUNET_SYSERR)
402 rh->name_callback (rh->cls, NULL);
403 GNUNET_CONTAINER_DLL_remove (req_head,
407 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
414 _("Resolver returns `%s' for IP `%s'.\n"),
416 GNUNET_a2s ((const void*) &rh[1], rh->data_len));
418 if (rh->was_transmitted != GNUNET_SYSERR)
419 rh->name_callback (rh->cls, hostname);
420 GNUNET_CLIENT_receive (client,
423 GNUNET_TIME_absolute_get_remaining (rh->timeout));
425 if (NULL != rh->addr_callback)
427 sa = (const struct sockaddr *) &msg[1];
428 salen = size - sizeof (struct GNUNET_MessageHeader);
429 if (salen < sizeof (struct sockaddr))
432 if (rh->was_transmitted != GNUNET_SYSERR)
433 rh->addr_callback (rh->cls, NULL, 0);
434 GNUNET_CONTAINER_DLL_remove (req_head,
438 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
445 char *ips = no_resolve (sa, salen);
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Resolver returns `%s' for `%s'.\n",
449 (const char*) &rh[1]);
453 rh->addr_callback (rh->cls, sa, salen);
454 GNUNET_CLIENT_receive (client,
457 GNUNET_TIME_absolute_get_remaining (rh->timeout));
463 * We've been asked to lookup the address for a hostname and were
464 * given a valid numeric string. Perform the callbacks for the
467 * @param cls struct GNUNET_RESOLVER_RequestHandle for the request
468 * @param tc unused scheduler context
471 numeric_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
473 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
474 struct sockaddr_in v4;
475 struct sockaddr_in6 v6;
476 const char *hostname;
478 memset (&v4, 0, sizeof (v4));
479 v4.sin_family = AF_INET;
480 #if HAVE_SOCKADDR_IN_SIN_LEN
481 v4.sin_len = sizeof (v4);
483 memset (&v6, 0, sizeof (v6));
484 v6.sin6_family = AF_INET6;
485 #if HAVE_SOCKADDR_IN_SIN_LEN
486 v6.sin6_len = sizeof (v6);
488 hostname = (const char*) &rh[1];
489 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET)) &&
490 (1 == inet_pton (AF_INET, hostname, &v4.sin_addr)))
492 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
493 if ((rh->domain == AF_UNSPEC) &&
494 (1 == inet_pton (AF_INET6, hostname, &v6.sin6_addr)))
496 /* this can happen on some systems IF "hostname" is "localhost" */
497 rh->addr_callback (rh->cls,
498 (const struct sockaddr *) &v6, sizeof (v6));
500 rh->addr_callback (rh->cls, NULL, 0);
504 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET6)) &&
505 (1 == inet_pton (AF_INET6, hostname, &v6.sin6_addr)))
507 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
508 rh->addr_callback (rh->cls, NULL, 0);
512 /* why are we here? this task should not have been scheduled! */
519 * We've been asked to lookup the address for a hostname and were
520 * given a variant of "loopback". Perform the callbacks for the
521 * respective loopback numeric addresses.
523 * @param cls struct GNUNET_RESOLVER_RequestHandle for the request
524 * @param tc unused scheduler context
527 loopback_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
529 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
530 struct sockaddr_in v4;
531 struct sockaddr_in6 v6;
533 memset (&v4, 0, sizeof (v4));
534 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
535 v4.sin_family = AF_INET;
536 #if HAVE_SOCKADDR_IN_SIN_LEN
537 v4.sin_len = sizeof (v4);
539 memset (&v6, 0, sizeof (v6));
540 v6.sin6_family = AF_INET6;
541 #if HAVE_SOCKADDR_IN_SIN_LEN
542 v6.sin6_len = sizeof (v6);
544 v6.sin6_addr = in6addr_loopback;
548 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
551 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
554 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
555 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
561 rh->addr_callback (rh->cls, NULL, 0);
567 * Task executed on system shutdown.
570 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
572 s_task = GNUNET_SCHEDULER_NO_TASK;
573 GNUNET_RESOLVER_disconnect ();
578 * Process pending requests to the resolver.
580 * @param h handle to the resolver
585 struct GNUNET_RESOLVER_GetMessage *msg;
586 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
587 struct GNUNET_RESOLVER_RequestHandle *rh;
597 /* nothing to do, release socket really soon if there is nothing
599 s_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
600 &shutdown_task, NULL);
603 if (GNUNET_YES == rh->was_transmitted)
604 return; /* waiting for reply */
605 msg = (struct GNUNET_RESOLVER_GetMessage *) buf;
607 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + rh->data_len);
608 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
609 msg->direction = htonl (rh->direction);
610 msg->domain = htonl (rh->domain);
611 memcpy (&msg[1], &rh[1], rh->data_len);
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
614 "Transmitting DNS resolution request to DNS service\n");
617 GNUNET_CLIENT_transmit_and_get_response (client,
619 GNUNET_TIME_absolute_get_remaining (rh->timeout),
621 &handle_response, rh))
623 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
628 rh->was_transmitted = GNUNET_YES;
633 * Now try to reconnect to the resolver service.
636 * @param tc scheduler context
639 reconnect_task (void *cls,
640 const struct GNUNET_SCHEDULER_TaskContext *tc)
642 r_task = GNUNET_SCHEDULER_NO_TASK;
643 if (NULL == req_head)
644 return; /* no work pending */
645 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
649 "Trying to connect to DNS service\n");
651 client = GNUNET_CLIENT_connect ("resolver", cfg);
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
655 "Failed to connect, will try again later\n");
664 * Adjust exponential back-off and reconnect to the service.
669 struct GNUNET_RESOLVER_RequestHandle *rh;
671 if (GNUNET_SCHEDULER_NO_TASK != r_task)
673 GNUNET_assert (NULL == client);
674 if (NULL != (rh = req_head))
676 switch (rh->was_transmitted)
679 /* nothing more to do */
682 /* disconnected, transmit again! */
683 rh->was_transmitted = GNUNET_NO;
686 /* request was cancelled, remove entirely */
687 GNUNET_CONTAINER_DLL_remove (req_head,
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
699 "Will try to connect to DNS service in %llu ms\n",
700 (unsigned long long) backoff.rel_value);
702 r_task = GNUNET_SCHEDULER_add_delayed (backoff,
705 backoff = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
706 GNUNET_TIME_relative_multiply (backoff, 2));
711 * Convert a string to one or more IP addresses.
713 * @param hostname the hostname to resolve
714 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
715 * @param callback function to call with addresses
716 * @param callback_cls closure for callback
717 * @param timeout how long to try resolving
718 * @return handle that can be used to cancel the request, NULL on error
720 struct GNUNET_RESOLVER_RequestHandle *
721 GNUNET_RESOLVER_ip_get (const char *hostname,
723 struct GNUNET_TIME_Relative timeout,
724 GNUNET_RESOLVER_AddressCallback callback,
727 struct GNUNET_RESOLVER_RequestHandle *rh;
733 slen = strlen (hostname) + 1;
734 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
739 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen);
741 rh->addr_callback = callback;
742 rh->cls = callback_cls;
743 memcpy (&rh[1], hostname, slen);
745 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
746 rh->direction = GNUNET_NO;
747 /* first, check if this is a numeric address */
748 if (((1 == inet_pton (AF_INET,
751 ((domain == AF_INET) || (domain == AF_UNSPEC))) ||
752 ((1 == inet_pton (AF_INET6,
755 ((domain == AF_INET6) || (domain == AF_UNSPEC))))
757 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution, rh);
760 /* then, check if this is a loopback address */
762 while (loopback[i] != NULL)
763 if (0 == strcasecmp (loopback[i++], hostname))
765 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution, rh);
768 GNUNET_CONTAINER_DLL_insert_tail (req_head,
771 rh->was_queued = GNUNET_YES;
772 if (s_task != GNUNET_SCHEDULER_NO_TASK)
774 GNUNET_SCHEDULER_cancel (s_task);
775 s_task = GNUNET_SCHEDULER_NO_TASK;
783 * We've been asked to convert an address to a string without
784 * a reverse lookup. Do it.
786 * @param cls struct GNUNET_RESOLVER_RequestHandle for the request
787 * @param tc unused scheduler context
790 numeric_reverse (void *cls,
791 const struct GNUNET_SCHEDULER_TaskContext *tc)
793 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
796 result = no_resolve ((const struct sockaddr *) &rh[1], rh->data_len);
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), result);
802 rh->name_callback (rh->cls, result);
803 GNUNET_free (result);
805 rh->name_callback (rh->cls, NULL);
811 * Get an IP address as a string.
813 * @param sa host address
814 * @param salen length of host address
815 * @param do_resolve use GNUNET_NO to return numeric hostname
816 * @param timeout how long to try resolving
817 * @param callback function to call with hostnames
818 * @param cls closure for callback
819 * @return handle that can be used to cancel the request
821 struct GNUNET_RESOLVER_RequestHandle *
822 GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa,
825 struct GNUNET_TIME_Relative timeout,
826 GNUNET_RESOLVER_HostnameCallback callback,
829 struct GNUNET_RESOLVER_RequestHandle *rh;
832 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + salen);
833 rh->name_callback = callback;
835 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
836 memcpy (&rh[1], sa, salen);
837 rh->data_len = salen;
838 rh->direction = GNUNET_YES;
839 if (GNUNET_NO == do_resolve)
841 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse, rh);
844 if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
850 GNUNET_CONTAINER_DLL_insert_tail (req_head,
853 rh->was_queued = GNUNET_YES;
854 if (s_task != GNUNET_SCHEDULER_NO_TASK)
856 GNUNET_SCHEDULER_cancel (s_task);
857 s_task = GNUNET_SCHEDULER_NO_TASK;
865 * Get local fully qualified domain name
870 GNUNET_RESOLVER_local_fqdn_get ()
872 struct hostent *host;
873 char hostname[GNUNET_OS_get_hostname_max_length() + 1];
875 if (0 != gethostname (hostname, sizeof (hostname) - 1))
877 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
878 GNUNET_ERROR_TYPE_BULK, "gethostname");
882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
883 _("Resolving our FQDN `%s'\n"), hostname);
885 host = gethostbyname (hostname);
888 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
889 _("Could not resolve our FQDN : %s\n"),
890 hstrerror (h_errno));
893 return GNUNET_strdup (host->h_name);
898 * Looking our own hostname.
900 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
901 * @param callback function to call with addresses
902 * @param cls closure for callback
903 * @param timeout how long to try resolving
904 * @return handle that can be used to cancel the request, NULL on error
906 struct GNUNET_RESOLVER_RequestHandle *
907 GNUNET_RESOLVER_hostname_resolve (int domain,
908 struct GNUNET_TIME_Relative timeout,
909 GNUNET_RESOLVER_AddressCallback callback,
912 char hostname[GNUNET_OS_get_hostname_max_length() + 1];
914 if (0 != gethostname (hostname, sizeof (hostname) - 1))
916 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
917 GNUNET_ERROR_TYPE_BULK, "gethostname");
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 _("Resolving our hostname `%s'\n"), hostname);
924 return GNUNET_RESOLVER_ip_get (hostname,
932 * Cancel a request that is still pending with the resolver.
933 * Note that a client MUST NOT cancel a request that has
934 * been completed (i.e, the callback has been called to
935 * signal timeout or the final result).
937 * @param rh handle of request to cancel
940 GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh)
942 if (rh->task != GNUNET_SCHEDULER_NO_TASK)
944 GNUNET_SCHEDULER_cancel (rh->task);
945 rh->task = GNUNET_SCHEDULER_NO_TASK;
947 if (rh->was_transmitted == GNUNET_NO)
949 if (rh->was_queued == GNUNET_YES)
950 GNUNET_CONTAINER_DLL_remove (req_head,
956 GNUNET_assert (rh->was_transmitted == GNUNET_YES);
957 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */
961 /* end of resolver_api.c */