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_protocols.h"
31 #include "gnunet_resolver_service.h"
32 #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[] = {
53 * Handle to a request given to the resolver. Can be used to cancel
54 * the request prior to the timeout or successful execution. Also
55 * used to track our internal state for the request.
57 struct GNUNET_RESOLVER_RequestHandle
61 * Callback if this is an name resolution request,
64 GNUNET_RESOLVER_AddressCallback addr_callback;
67 * Callback if this is a reverse lookup request,
70 GNUNET_RESOLVER_HostnameCallback name_callback;
73 * Closure for the respective "callback".
78 * Our connection to the resolver service.
80 struct GNUNET_CLIENT_Connection *client;
83 * Name of the host that we are resolving.
88 * When should this request time out?
90 struct GNUNET_TIME_Absolute timeout;
93 * Task handle for numeric lookups.
95 GNUNET_SCHEDULER_TaskIdentifier task;
98 * Desired address family.
103 * Length of the "struct sockaddr" that follows this
104 * struct (only for reverse lookup).
111 * Check that the resolver service runs on localhost
115 check_config (const struct GNUNET_CONFIGURATION_Handle *cfg)
119 struct sockaddr_in v4;
120 struct sockaddr_in6 v6;
122 memset (&v4, 0, sizeof (v4));
123 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
124 v4.sin_family = AF_INET;
125 #if HAVE_SOCKADDR_IN_SIN_LEN
126 v4.sin_len = sizeof (v4);
128 memset (&v6, 0, sizeof (v6));
129 v6.sin6_family = AF_INET6;
130 #if HAVE_SOCKADDR_IN_SIN_LEN
131 v6.sin6_len = sizeof (v6);
134 GNUNET_CONFIGURATION_get_value_string (cfg,
136 "HOSTNAME", &hostname))
138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
139 _("Must specify `%s' for `%s' in configuration!\n"),
140 "HOSTNAME", "resolver");
143 if ((1 != inet_pton (AF_INET,
145 &v4)) || (1 != inet_pton (AF_INET6, hostname, &v6)))
147 GNUNET_free (hostname);
151 while (loopback[i] != NULL)
152 if (0 == strcasecmp (loopback[i++], hostname))
154 GNUNET_free (hostname);
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159 ("Must specify `%s' or numeric IP address for `%s' of `%s' in configuration!\n"),
160 "localhost", "HOSTNAME", "resolver");
161 GNUNET_free (hostname);
167 * Convert IP address to string without DNS resolution.
169 * @param sa the address
170 * @param salen number of bytes in sa
171 * @return address as a string, NULL on error
174 no_resolve (const struct sockaddr *sa, socklen_t salen)
177 char inet4[INET_ADDRSTRLEN];
178 char inet6[INET6_ADDRSTRLEN];
180 if (salen < sizeof (struct sockaddr))
182 switch (sa->sa_family)
185 if (salen != sizeof (struct sockaddr_in))
189 &((struct sockaddr_in *) sa)->sin_addr,
190 inet4, INET_ADDRSTRLEN))
192 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
195 ret = GNUNET_strdup (inet4);
198 if (salen != sizeof (struct sockaddr_in6))
202 &((struct sockaddr_in6 *) sa)->sin6_addr,
203 inet6, INET6_ADDRSTRLEN))
205 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
208 ret = GNUNET_strdup (inet6);
219 * Process the reply from the resolver (which is presumably
220 * the numeric IP address for a name resolution request).
222 * @param cls the "GNUNET_RESOLVER_RequestHandle" for which this is a reply
223 * @param msg reply from the resolver or NULL on error
226 handle_address_response (void *cls, const struct GNUNET_MessageHeader *msg)
228 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
230 const struct sockaddr *sa;
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
236 _("Timeout trying to resolve hostname `%s'.\n"),
238 rh->addr_callback (rh->cls, NULL, 0);
239 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
243 if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
246 rh->addr_callback (rh->cls, NULL, 0);
247 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
252 size = ntohs (msg->size);
253 if (size == sizeof (struct GNUNET_MessageHeader))
256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257 _("Received end message resolving hostname `%s'.\n"),
260 rh->addr_callback (rh->cls, NULL, 0);
261 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
265 sa = (const struct sockaddr *) &msg[1];
266 salen = size - sizeof (struct GNUNET_MessageHeader);
267 if (salen < sizeof (struct sockaddr))
270 rh->addr_callback (rh->cls, NULL, 0);
271 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
277 char *ips = no_resolve (sa, salen);
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279 "Resolver returns `%s' for `%s'.\n", ips,
284 rh->addr_callback (rh->cls, sa, salen);
285 GNUNET_CLIENT_receive (rh->client,
286 &handle_address_response,
288 GNUNET_TIME_absolute_get_remaining (rh->timeout));
293 * We've been asked to lookup the address for a hostname and were
294 * given a valid numeric string. Perform the callbacks for the
297 * @param cls struct GNUNET_RESOLVER_RequestHandle for the request
298 * @param tc unused scheduler context
301 numeric_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
303 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
304 struct sockaddr_in v4;
305 struct sockaddr_in6 v6;
307 memset (&v4, 0, sizeof (v4));
308 v4.sin_family = AF_INET;
309 #if HAVE_SOCKADDR_IN_SIN_LEN
310 v4.sin_len = sizeof (v4);
312 memset (&v6, 0, sizeof (v6));
313 v6.sin6_family = AF_INET6;
314 #if HAVE_SOCKADDR_IN_SIN_LEN
315 v6.sin6_len = sizeof (v6);
318 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET)) &&
319 (1 == inet_pton (AF_INET, rh->hostname, &v4.sin_addr)))
321 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
322 if ((rh->domain == AF_UNSPEC) &&
323 (1 == inet_pton (AF_INET6, rh->hostname, &v6.sin6_addr)))
325 /* this can happen on some systems IF "hostname" is "localhost" */
326 rh->addr_callback (rh->cls,
327 (const struct sockaddr *) &v6, sizeof (v6));
329 rh->addr_callback (rh->cls, NULL, 0);
333 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET6)) &&
334 (1 == inet_pton (AF_INET6, rh->hostname, &v6.sin6_addr)))
336 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
337 rh->addr_callback (rh->cls, NULL, 0);
341 /* why are we here? this task should not have been scheduled! */
349 * We've been asked to lookup the address for a hostname and were
350 * given a variant of "loopback". Perform the callbacks for the
351 * respective loopback numeric addresses.
353 * @param cls struct GNUNET_RESOLVER_RequestHandle for the request
354 * @param tc unused scheduler context
357 loopback_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
359 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
360 struct sockaddr_in v4;
361 struct sockaddr_in6 v6;
363 memset (&v4, 0, sizeof (v4));
364 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
365 v4.sin_family = AF_INET;
366 #if HAVE_SOCKADDR_IN_SIN_LEN
367 v4.sin_len = sizeof (v4);
369 memset (&v6, 0, sizeof (v6));
370 v6.sin6_family = AF_INET6;
371 #if HAVE_SOCKADDR_IN_SIN_LEN
372 v6.sin6_len = sizeof (v6);
374 v6.sin6_addr = in6addr_loopback;
378 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
381 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
384 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
385 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
391 rh->addr_callback (rh->cls, NULL, 0);
397 * Convert a string to one or more IP addresses.
399 * @param cfg configuration to use
400 * @param hostname the hostname to resolve
401 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
402 * @param callback function to call with addresses
403 * @param callback_cls closure for callback
404 * @param timeout how long to try resolving
405 * @return handle that can be used to cancel the request, NULL on error
407 struct GNUNET_RESOLVER_RequestHandle *
408 GNUNET_RESOLVER_ip_get (const struct GNUNET_CONFIGURATION_Handle *cfg,
409 const char *hostname,
411 struct GNUNET_TIME_Relative timeout,
412 GNUNET_RESOLVER_AddressCallback callback,
415 struct GNUNET_CLIENT_Connection *client;
416 struct GNUNET_RESOLVER_GetMessage *msg;
417 struct GNUNET_RESOLVER_RequestHandle *rh;
422 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
425 slen = strlen (hostname) + 1;
426 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
431 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen);
433 rh->addr_callback = callback;
434 rh->cls = callback_cls;
435 memcpy (&rh[1], hostname, slen);
436 rh->hostname = (const char *) &rh[1];
437 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
439 /* first, check if this is a numeric address */
440 if (((1 == inet_pton (AF_INET,
443 ((domain == AF_INET) || (domain == AF_UNSPEC))) ||
444 ((1 == inet_pton (AF_INET6,
447 ((domain == AF_INET6) || (domain == AF_UNSPEC))))
449 rh->task = GNUNET_SCHEDULER_add_now (&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 (&loopback_resolution, rh);
461 client = GNUNET_CLIENT_connect ("resolver", cfg);
469 msg = (struct GNUNET_RESOLVER_GetMessage *) buf;
471 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
472 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
473 msg->direction = htonl (GNUNET_NO);
474 msg->domain = htonl (domain);
475 memcpy (&msg[1], hostname, slen);
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 _("Resolver requests DNS resolution of hostname `%s'.\n"),
483 GNUNET_CLIENT_transmit_and_get_response (client,
487 &handle_address_response, rh))
490 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
498 * Process response with a hostname for a reverse DNS lookup.
500 * @param cls our "struct GNUNET_RESOLVER_RequestHandle" context
501 * @param msg message with the hostname, NULL on error
504 handle_hostname_response (void *cls, const struct GNUNET_MessageHeader *msg)
506 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
508 const char *hostname;
512 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
513 _("Timeout trying to resolve IP address `%s'.\n"),
514 GNUNET_a2s ((const void*) &rh[1], rh->salen));
515 rh->name_callback (rh->cls, NULL);
516 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
520 size = ntohs (msg->size);
521 if (size == sizeof (struct GNUNET_MessageHeader))
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
525 _("Received end message resolving IP address `%s'.\n"),
526 GNUNET_a2s ((const void*) &rh[1], rh->salen));
528 rh->name_callback (rh->cls, NULL);
529 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
533 hostname = (const char *) &msg[1];
534 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
537 rh->name_callback (rh->cls, NULL);
538 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
544 _("Resolver returns `%s' for IP `%s'.\n"),
546 GNUNET_a2s ((const void*) &rh[1], rh->salen));
548 rh->name_callback (rh->cls, hostname);
549 GNUNET_CLIENT_receive (rh->client,
550 &handle_hostname_response,
552 GNUNET_TIME_absolute_get_remaining (rh->timeout));
558 * We've been asked to convert an address to a string without
559 * a reverse lookup. Do it.
561 * @param cls struct GNUNET_RESOLVER_RequestHandle for the request
562 * @param tc unused scheduler context
565 numeric_reverse (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
567 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
570 result = no_resolve ((const struct sockaddr *) &rh[1], rh->salen);
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), result);
576 rh->name_callback (rh->cls, result);
577 GNUNET_free (result);
579 rh->name_callback (rh->cls, NULL);
586 * Get an IP address as a string.
588 * @param cfg configuration to use
589 * @param sa host address
590 * @param salen length of host address
591 * @param do_resolve use GNUNET_NO to return numeric hostname
592 * @param timeout how long to try resolving
593 * @param callback function to call with hostnames
594 * @param cls closure for callback
595 * @return handle that can be used to cancel the request
597 struct GNUNET_RESOLVER_RequestHandle *
598 GNUNET_RESOLVER_hostname_get (const struct GNUNET_CONFIGURATION_Handle *cfg,
599 const struct sockaddr *sa,
602 struct GNUNET_TIME_Relative timeout,
603 GNUNET_RESOLVER_HostnameCallback callback,
606 struct GNUNET_CLIENT_Connection *client;
607 struct GNUNET_RESOLVER_GetMessage *msg;
608 struct GNUNET_RESOLVER_RequestHandle *rh;
609 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
612 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + salen);
613 rh->name_callback = callback;
615 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
617 memcpy (&rh[1], sa, salen);
619 if (GNUNET_NO == do_resolve)
621 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse, rh);
624 if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
630 client = GNUNET_CLIENT_connect ("resolver", cfg);
638 msg = (struct GNUNET_RESOLVER_GetMessage *) buf;
640 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
641 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
642 msg->direction = htonl (GNUNET_YES);
643 msg->domain = htonl (sa->sa_family);
644 memcpy (&msg[1], sa, salen);
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
647 _("Resolver requests DNS resolution of IP address.\n"));
650 GNUNET_CLIENT_transmit_and_get_response (client,
654 &handle_hostname_response, rh))
656 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
665 * Get local fully qualified domain name
669 GNUNET_RESOLVER_local_fqdn_get ( void )
671 struct hostent *host;
672 char hostname[GNUNET_OS_get_hostname_max_length() + 1];
675 if (0 != gethostname (hostname, sizeof (hostname) - 1))
677 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
678 GNUNET_ERROR_TYPE_BULK, "gethostname");
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
683 _("Resolving our FQDN `%s'\n"), hostname);
685 host = gethostbyname ( hostname );
688 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
689 _("Could not resolve our FQDN : %s\n"),
690 hstrerror (h_errno));
693 return GNUNET_strdup (host->h_name);
697 * Looking our own hostname.
699 * @param cfg configuration to use
700 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
701 * @param callback function to call with addresses
702 * @param cls closure for callback
703 * @param timeout how long to try resolving
704 * @return handle that can be used to cancel the request, NULL on error
706 struct GNUNET_RESOLVER_RequestHandle *
707 GNUNET_RESOLVER_hostname_resolve (const struct GNUNET_CONFIGURATION_Handle
709 struct GNUNET_TIME_Relative timeout,
710 GNUNET_RESOLVER_AddressCallback callback,
713 char hostname[GNUNET_OS_get_hostname_max_length() + 1];
716 if (0 != gethostname (hostname, sizeof (hostname) - 1))
718 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
719 GNUNET_ERROR_TYPE_BULK, "gethostname");
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 _("Resolving our hostname `%s'\n"), hostname);
726 return GNUNET_RESOLVER_ip_get (cfg, hostname, domain, timeout, callback,
732 * Cancel a request that is still pending with the resolver.
733 * Note that a client MUST NOT cancel a request that has
734 * been completed (i.e, the callback has been called to
735 * signal timeout or the final result).
737 * @param h handle of request to cancel
740 GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *h)
742 if (h->client != NULL)
743 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
744 if (h->task != GNUNET_SCHEDULER_NO_TASK)
745 GNUNET_SCHEDULER_cancel (h->task);
751 /* end of resolver_api.c */