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;
85 struct GNUNET_SCHEDULER_Handle *sched;
88 * Name of the host that we are resolving.
93 * When should this request time out?
95 struct GNUNET_TIME_Absolute timeout;
98 * Task handle for numeric lookups.
100 GNUNET_SCHEDULER_TaskIdentifier task;
103 * Desired address family.
108 * Length of the "struct sockaddr" that follows this
109 * struct (only for reverse lookup).
116 * Check that the resolver service runs on localhost
120 check_config (const struct GNUNET_CONFIGURATION_Handle *cfg)
124 struct sockaddr_in v4;
125 struct sockaddr_in6 v6;
127 memset (&v4, 0, sizeof (v4));
128 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
129 v4.sin_family = AF_INET;
130 #if HAVE_SOCKADDR_IN_SIN_LEN
131 v4.sin_len = sizeof (v4);
133 memset (&v6, 0, sizeof (v6));
134 v6.sin6_family = AF_INET6;
135 #if HAVE_SOCKADDR_IN_SIN_LEN
136 v6.sin6_len = sizeof (v6);
139 GNUNET_CONFIGURATION_get_value_string (cfg,
141 "HOSTNAME", &hostname))
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
144 _("Must specify `%s' for `%s' in configuration!\n"),
145 "HOSTNAME", "resolver");
148 if ((1 != inet_pton (AF_INET,
150 &v4)) || (1 != inet_pton (AF_INET6, hostname, &v6)))
152 GNUNET_free (hostname);
156 while (loopback[i] != NULL)
157 if (0 == strcasecmp (loopback[i++], hostname))
159 GNUNET_free (hostname);
162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
164 ("Must specify `%s' or numeric IP address for `%s' of `%s' in configuration!\n"),
165 "localhost", "HOSTNAME", "resolver");
166 GNUNET_free (hostname);
172 * Convert IP address to string without DNS resolution.
174 * @param sa the address
175 * @param salen number of bytes in sa
176 * @return address as a string, NULL on error
179 no_resolve (const struct sockaddr *sa, socklen_t salen)
182 char inet4[INET_ADDRSTRLEN];
183 char inet6[INET6_ADDRSTRLEN];
185 if (salen < sizeof (struct sockaddr))
187 switch (sa->sa_family)
190 if (salen != sizeof (struct sockaddr_in))
193 &((struct sockaddr_in *) sa)->sin_addr,
194 inet4, INET_ADDRSTRLEN);
195 ret = GNUNET_strdup (inet4);
198 if (salen != sizeof (struct sockaddr_in6))
201 &((struct sockaddr_in6 *) sa)->sin6_addr,
202 inet6, INET6_ADDRSTRLEN);
203 ret = GNUNET_strdup (inet6);
214 * Process the reply from the resolver (which is presumably
215 * the numeric IP address for a name resolution request).
217 * @param cls the "GNUNET_RESOLVER_RequestHandle" for which this is a reply
218 * @param msg reply from the resolver or NULL on error
221 handle_address_response (void *cls, const struct GNUNET_MessageHeader *msg)
223 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
225 const struct sockaddr *sa;
231 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
232 _("Timeout trying to resolve hostname `%s'.\n"),
234 rh->addr_callback (rh->cls, NULL, 0);
235 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
239 if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
242 rh->addr_callback (rh->cls, NULL, 0);
243 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
248 size = ntohs (msg->size);
249 if (size == sizeof (struct GNUNET_MessageHeader))
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 _("Received end message resolving hostname `%s'.\n"),
256 rh->addr_callback (rh->cls, NULL, 0);
257 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
261 sa = (const struct sockaddr *) &msg[1];
262 salen = size - sizeof (struct GNUNET_MessageHeader);
263 if (salen < sizeof (struct sockaddr))
266 rh->addr_callback (rh->cls, NULL, 0);
267 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
273 char *ips = no_resolve (sa, salen);
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Resolver returns `%s' for `%s'.\n", ips,
280 rh->addr_callback (rh->cls, sa, salen);
281 GNUNET_CLIENT_receive (rh->client,
282 &handle_address_response,
284 GNUNET_TIME_absolute_get_remaining (rh->timeout));
289 * We've been asked to lookup the address for a hostname and were
290 * given a valid numeric string. Perform the callbacks for the
293 * @param cls struct GNUNET_RESOLVER_RequestHandle for the request
294 * @param tc unused scheduler context
297 numeric_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
299 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
300 struct sockaddr_in v4;
301 struct sockaddr_in6 v6;
303 memset (&v4, 0, sizeof (v4));
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);
314 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET)) &&
315 (1 == inet_pton (AF_INET, rh->hostname, &v4.sin_addr)))
317 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
318 if ((rh->domain == AF_UNSPEC) &&
319 (1 == inet_pton (AF_INET6, rh->hostname, &v6.sin6_addr)))
321 /* this can happen on some systems IF "hostname" is "localhost" */
322 rh->addr_callback (rh->cls,
323 (const struct sockaddr *) &v6, sizeof (v6));
325 rh->addr_callback (rh->cls, NULL, 0);
329 if (((rh->domain == AF_UNSPEC) || (rh->domain == AF_INET6)) &&
330 (1 == inet_pton (AF_INET6, rh->hostname, &v6.sin6_addr)))
332 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
333 rh->addr_callback (rh->cls, NULL, 0);
337 /* why are we here? this task should not have been scheduled! */
345 * We've been asked to lookup the address for a hostname and were
346 * given a variant of "loopback". Perform the callbacks for the
347 * respective loopback numeric addresses.
349 * @param cls struct GNUNET_RESOLVER_RequestHandle for the request
350 * @param tc unused scheduler context
353 loopback_resolution (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
355 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
356 struct sockaddr_in v4;
357 struct sockaddr_in6 v6;
359 memset (&v4, 0, sizeof (v4));
360 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
361 v4.sin_family = AF_INET;
362 #if HAVE_SOCKADDR_IN_SIN_LEN
363 v4.sin_len = sizeof (v4);
365 memset (&v6, 0, sizeof (v6));
366 v6.sin6_family = AF_INET6;
367 #if HAVE_SOCKADDR_IN_SIN_LEN
368 v6.sin6_len = sizeof (v6);
370 v6.sin6_addr = in6addr_loopback;
374 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
377 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
380 rh->addr_callback (rh->cls, (const struct sockaddr *) &v6, sizeof (v6));
381 rh->addr_callback (rh->cls, (const struct sockaddr *) &v4, sizeof (v4));
387 rh->addr_callback (rh->cls, NULL, 0);
393 * Convert a string to one or more IP addresses.
395 * @param sched scheduler to use
396 * @param cfg configuration to use
397 * @param hostname the hostname to resolve
398 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
399 * @param callback function to call with addresses
400 * @param callback_cls closure for callback
401 * @param timeout how long to try resolving
402 * @return handle that can be used to cancel the request, NULL on error
404 struct GNUNET_RESOLVER_RequestHandle *
405 GNUNET_RESOLVER_ip_get (struct GNUNET_SCHEDULER_Handle *sched,
406 const struct GNUNET_CONFIGURATION_Handle *cfg,
407 const char *hostname,
409 struct GNUNET_TIME_Relative timeout,
410 GNUNET_RESOLVER_AddressCallback callback,
413 struct GNUNET_CLIENT_Connection *client;
414 struct GNUNET_RESOLVER_GetMessage *msg;
415 struct GNUNET_RESOLVER_RequestHandle *rh;
420 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
423 slen = strlen (hostname) + 1;
424 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
425 GNUNET_SERVER_MAX_MESSAGE_SIZE)
430 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 (sched,
450 &numeric_resolution, rh);
453 /* then, check if this is a loopback address */
455 while (loopback[i] != NULL)
456 if (0 == strcasecmp (loopback[i++], hostname))
458 rh->task = GNUNET_SCHEDULER_add_now (sched,
459 &loopback_resolution, rh);
463 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
471 msg = (struct GNUNET_RESOLVER_GetMessage *) buf;
473 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
474 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
475 msg->direction = htonl (GNUNET_NO);
476 msg->domain = htonl (domain);
477 memcpy (&msg[1], hostname, slen);
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481 _("Resolver requests DNS resolution of hostname `%s'.\n"),
485 GNUNET_CLIENT_transmit_and_get_response (client,
489 &handle_address_response, rh))
492 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
500 * Process response with a hostname for a reverse DNS lookup.
502 * @param cls our "struct GNUNET_RESOLVER_RequestHandle" context
503 * @param msg message with the hostname, NULL on error
506 handle_hostname_response (void *cls, const struct GNUNET_MessageHeader *msg)
508 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
510 const char *hostname;
514 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
515 _("Timeout trying to resolve IP address `%s'.\n"),
516 GNUNET_a2s ((const void*) &rh[1], rh->salen));
517 rh->name_callback (rh->cls, NULL);
518 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
522 size = ntohs (msg->size);
523 if (size == sizeof (struct GNUNET_MessageHeader))
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 _("Received end message resolving IP address `%s'.\n"),
528 GNUNET_a2s ((const void*) &rh[1], rh->salen));
530 rh->name_callback (rh->cls, NULL);
531 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
535 hostname = (const char *) &msg[1];
536 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
539 rh->name_callback (rh->cls, NULL);
540 GNUNET_CLIENT_disconnect (rh->client, GNUNET_NO);
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 _("Resolver returns `%s' for IP `%s'.\n"),
548 GNUNET_a2s ((const void*) &rh[1], rh->salen));
550 rh->name_callback (rh->cls, hostname);
551 GNUNET_CLIENT_receive (rh->client,
552 &handle_hostname_response,
554 GNUNET_TIME_absolute_get_remaining (rh->timeout));
560 * We've been asked to convert an address to a string without
561 * a reverse lookup. Do it.
563 * @param cls struct GNUNET_RESOLVER_RequestHandle for the request
564 * @param tc unused scheduler context
567 numeric_reverse (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
569 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
572 result = no_resolve ((const struct sockaddr *) &rh[1], rh->salen);
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), result);
578 rh->name_callback (rh->cls, result);
579 GNUNET_free (result);
581 rh->name_callback (rh->cls, NULL);
588 * Get an IP address as a string.
590 * @param sched scheduler to use
591 * @param cfg configuration to use
592 * @param sa host address
593 * @param salen length of host address
594 * @param do_resolve use GNUNET_NO to return numeric hostname
595 * @param timeout how long to try resolving
596 * @param callback function to call with hostnames
597 * @param cls closure for callback
598 * @return handle that can be used to cancel the request
600 struct GNUNET_RESOLVER_RequestHandle *
601 GNUNET_RESOLVER_hostname_get (struct GNUNET_SCHEDULER_Handle *sched,
602 const struct GNUNET_CONFIGURATION_Handle *cfg,
603 const struct sockaddr *sa,
606 struct GNUNET_TIME_Relative timeout,
607 GNUNET_RESOLVER_HostnameCallback callback,
610 struct GNUNET_CLIENT_Connection *client;
611 struct GNUNET_RESOLVER_GetMessage *msg;
612 struct GNUNET_RESOLVER_RequestHandle *rh;
613 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
616 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + salen);
617 rh->name_callback = callback;
619 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
622 memcpy (&rh[1], sa, salen);
624 if (GNUNET_NO == do_resolve)
626 rh->task = GNUNET_SCHEDULER_add_now (sched,
627 &numeric_reverse, rh);
630 if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
631 GNUNET_SERVER_MAX_MESSAGE_SIZE)
637 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
645 msg = (struct GNUNET_RESOLVER_GetMessage *) buf;
647 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
648 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
649 msg->direction = htonl (GNUNET_YES);
650 msg->domain = htonl (sa->sa_family);
651 memcpy (&msg[1], sa, salen);
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 _("Resolver requests DNS resolution of IP address.\n"));
657 GNUNET_CLIENT_transmit_and_get_response (client,
661 &handle_hostname_response, rh))
663 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
672 * Get local fully qualified domain name
676 GNUNET_RESOLVER_local_fqdn_get ( void )
678 struct hostent *host;
679 char hostname[GNUNET_OS_get_hostname_max_length() + 1];
682 if (0 != gethostname (hostname, sizeof (hostname) - 1))
684 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
685 GNUNET_ERROR_TYPE_BULK, "gethostname");
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 _("Resolving our FQDN `%s'\n"), hostname);
692 host = gethostbyname ( hostname );
695 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
696 _("Could not resolve our FQDN : %s\n"),
697 hstrerror (h_errno));
700 return GNUNET_strdup (host->h_name);
704 * Looking our own hostname.
706 * @param sched scheduler to use
707 * @param cfg configuration to use
708 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
709 * @param callback function to call with addresses
710 * @param cls closure for callback
711 * @param timeout how long to try resolving
712 * @return handle that can be used to cancel the request, NULL on error
714 struct GNUNET_RESOLVER_RequestHandle *
715 GNUNET_RESOLVER_hostname_resolve (struct GNUNET_SCHEDULER_Handle *sched,
716 const struct GNUNET_CONFIGURATION_Handle
718 struct GNUNET_TIME_Relative timeout,
719 GNUNET_RESOLVER_AddressCallback callback,
722 char hostname[GNUNET_OS_get_hostname_max_length() + 1];
725 if (0 != gethostname (hostname, sizeof (hostname) - 1))
727 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
728 GNUNET_ERROR_TYPE_BULK, "gethostname");
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 _("Resolving our hostname `%s'\n"), hostname);
735 return GNUNET_RESOLVER_ip_get (sched,
736 cfg, hostname, domain, timeout, callback,
742 * Cancel a request that is still pending with the resolver.
743 * Note that a client MUST NOT cancel a request that has
744 * been completed (i.e, the callback has been called to
745 * signal timeout or the final result).
747 * @param h handle of request to cancel
750 GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *h)
752 if (h->client != NULL)
753 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
754 if (h->task != GNUNET_SCHEDULER_NO_TASK)
755 GNUNET_SCHEDULER_cancel (h->sched, h->task);
761 /* end of resolver_api.c */