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_CLIENT_Connection *client;
59 struct GNUNET_TIME_Absolute timeout;
64 * Possible hostnames for "loopback".
66 static const char *loopback[] = {
74 * Check that the resolver service runs on localhost
78 check_config (const struct GNUNET_CONFIGURATION_Handle *cfg)
86 GNUNET_CONFIGURATION_get_value_string (cfg,
91 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
92 _("Must specify `%s' for `%s' in configuration!\n"),
97 if ( (0 == inet_pton (AF_INET,
100 (0 == inet_pton (AF_INET6,
104 GNUNET_free (hostname);
108 while (loopback[i] != NULL)
109 if (0 == strcmp (loopback[i++], hostname))
111 GNUNET_free (hostname);
114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115 _("Must specify `%s' for `%s' in configuration!\n"),
118 GNUNET_free (hostname);
124 * Convert IP address to string without DNS resolution.
126 * @param sa the address
127 * @param salen number of bytes in sa
128 * @return address as a string, NULL on error
131 no_resolve (const struct sockaddr *sa, socklen_t salen)
134 char inet4[INET_ADDRSTRLEN];
135 char inet6[INET6_ADDRSTRLEN];
137 if (salen < sizeof (struct sockaddr))
139 switch (sa->sa_family)
142 if (salen != sizeof (struct sockaddr_in))
145 &((struct sockaddr_in *) sa)->sin_addr,
146 inet4, INET_ADDRSTRLEN);
147 ret = GNUNET_strdup (inet4);
150 if (salen != sizeof (struct sockaddr_in6))
153 &((struct sockaddr_in6 *) sa)->sin6_addr,
154 inet6, INET6_ADDRSTRLEN);
155 ret = GNUNET_strdup (inet6);
172 handle_address_response (void *cls, const struct GNUNET_MessageHeader *msg)
174 struct GetAddressContext *gac = cls;
176 const struct sockaddr *sa;
182 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
183 _("Timeout trying to resolve hostname.\n"));
184 gac->callback (gac->cls, NULL, 0);
185 GNUNET_CLIENT_disconnect (gac->client);
189 if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
192 gac->callback (gac->cls, NULL, 0);
193 GNUNET_CLIENT_disconnect (gac->client);
198 size = ntohs (msg->size);
199 if (size == sizeof (struct GNUNET_MessageHeader))
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 _("Received end message resolving hostname.\n"));
205 gac->callback (gac->cls, NULL, 0);
206 GNUNET_CLIENT_disconnect (gac->client);
210 sa = (const struct sockaddr *) &msg[1];
211 salen = size - sizeof (struct GNUNET_MessageHeader);
212 if (salen < sizeof (struct sockaddr))
215 gac->callback (gac->cls, NULL, 0);
216 GNUNET_CLIENT_disconnect (gac->client);
222 char *ips = no_resolve (sa, salen);
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), ips);
227 gac->callback (gac->cls, sa, salen);
228 GNUNET_CLIENT_receive (gac->client,
229 &handle_address_response,
231 GNUNET_TIME_absolute_get_remaining (gac->timeout));
236 * Convert a string to one or more IP addresses.
238 * @param sched scheduler to use
239 * @param cfg configuration to use
240 * @param hostname the hostname to resolve
241 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
242 * @param callback function to call with addresses
243 * @param callback_cls closure for callback
244 * @param timeout how long to try resolving
247 GNUNET_RESOLVER_ip_get (struct GNUNET_SCHEDULER_Handle *sched,
248 const struct GNUNET_CONFIGURATION_Handle *cfg,
249 const char *hostname,
251 struct GNUNET_TIME_Relative timeout,
252 GNUNET_RESOLVER_AddressCallback callback,
255 struct GNUNET_CLIENT_Connection *client;
256 struct GNUNET_RESOLVER_GetMessage *msg;
257 struct GetAddressContext *actx;
260 struct sockaddr_in v4;
261 struct sockaddr_in6 v6;
263 memset (&v4, 0, sizeof(v4));
264 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
265 v4.sin_family = AF_INET;
266 #if HAVE_SOCKADDR_IN_SIN_LEN
267 v4.sin_len = sizeof(v4);
269 memset (&v6, 0, sizeof(v6));
270 v6.sin6_family = AF_INET6;
271 #if HAVE_SOCKADDR_IN_SIN_LEN
272 v6.sin6_len = sizeof(v6);
274 /* first, check if this is a numeric address */
275 if ( ( (domain == AF_UNSPEC) || (domain == AF_INET) ) &&
276 (0 == inet_pton (AF_INET,
280 callback (callback_cls,
281 (const struct sockaddr*) &v4,
283 if ( (domain == AF_UNSPEC) &&
284 (0 == inet_pton (AF_INET6,
288 /* this can happen on some systems IF "hostname" is "localhost" */
289 callback (callback_cls,
290 (const struct sockaddr*) &v6,
293 callback (callback_cls, NULL, 0);
296 if ( ( (domain == AF_UNSPEC) ||(domain == AF_INET) ) &&
297 (0 == inet_pton (AF_INET6,
301 callback (callback_cls,
302 (const struct sockaddr*) &v6,
304 callback (callback_cls, NULL, 0);
308 /* then, check if this is a loopback address */
310 while (loopback[i] != NULL)
311 if (0 == strcmp (loopback[i++], hostname))
313 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
314 v6.sin6_addr = in6addr_loopback;
318 callback (callback_cls,
319 (const struct sockaddr*) &v4,
323 callback (callback_cls,
324 (const struct sockaddr*) &v6,
328 callback (callback_cls,
329 (const struct sockaddr*) &v6,
331 callback (callback_cls,
332 (const struct sockaddr*) &v4,
336 callback (callback_cls, NULL, 0);
339 slen = strlen (hostname) + 1;
340 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
341 GNUNET_SERVER_MAX_MESSAGE_SIZE)
344 callback (callback_cls, NULL, 0);
347 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
350 callback (callback_cls, NULL, 0);
353 msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
355 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
356 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
357 msg->direction = htonl (GNUNET_NO);
358 msg->domain = htonl (domain);
359 memcpy (&msg[1], hostname, slen);
360 actx = GNUNET_malloc (sizeof (struct GetAddressContext));
361 actx->callback = callback;
362 actx->cls = callback_cls;
363 actx->client = client;
364 actx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 _("Resolver requests DNS resolution of hostname `%s'.\n"),
372 GNUNET_CLIENT_transmit_and_get_response (client,
376 &handle_address_response,
381 callback (callback_cls, NULL, 0);
382 GNUNET_CLIENT_disconnect (client);
392 struct GetHostnameContext
398 GNUNET_RESOLVER_HostnameCallback callback;
408 struct GNUNET_CLIENT_Connection *client;
413 struct GNUNET_TIME_Absolute timeout;
424 handle_hostname_response (void *cls, const struct GNUNET_MessageHeader *msg)
426 struct GetHostnameContext *ghc = cls;
428 const char *hostname;
432 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
433 _("Timeout trying to resolve IP address.\n"));
434 ghc->callback (ghc->cls, NULL);
435 GNUNET_CLIENT_disconnect (ghc->client);
439 size = ntohs (msg->size);
440 if (size == sizeof (struct GNUNET_MessageHeader))
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
444 _("Received end message resolving IP address.\n"));
446 ghc->callback (ghc->cls, NULL);
447 GNUNET_CLIENT_disconnect (ghc->client);
451 hostname = (const char *) &msg[1];
452 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
455 ghc->callback (ghc->cls, NULL);
456 GNUNET_CLIENT_disconnect (ghc->client);
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 _("Resolver returns `%s'.\n"), hostname);
464 ghc->callback (ghc->cls, hostname);
465 GNUNET_CLIENT_receive (ghc->client,
466 &handle_hostname_response,
468 GNUNET_TIME_absolute_get_remaining (ghc->timeout));
473 * Get an IP address as a string.
475 * @param sched scheduler to use
476 * @param cfg configuration to use
477 * @param sa host address
478 * @param salen length of host address
479 * @param do_resolve use GNUNET_NO to return numeric hostname
480 * @param timeout how long to try resolving
481 * @param callback function to call with hostnames
482 * @param cls closure for callback
485 GNUNET_RESOLVER_hostname_get (struct GNUNET_SCHEDULER_Handle *sched,
486 const struct GNUNET_CONFIGURATION_Handle *cfg,
487 const struct sockaddr *sa,
490 struct GNUNET_TIME_Relative timeout,
491 GNUNET_RESOLVER_HostnameCallback callback,
495 struct GNUNET_CLIENT_Connection *client;
496 struct GNUNET_RESOLVER_GetMessage *msg;
497 struct GetHostnameContext *hctx;
500 if (GNUNET_NO == do_resolve)
502 result = no_resolve (sa, salen);
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 _("Resolver returns `%s'.\n"), result);
507 callback (cls, result);
510 GNUNET_free (result);
511 callback (cls, NULL);
515 if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
516 GNUNET_SERVER_MAX_MESSAGE_SIZE)
519 callback (cls, NULL);
522 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
525 callback (cls, NULL);
528 msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
530 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
531 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
532 msg->direction = htonl (GNUNET_YES);
533 msg->domain = htonl (sa->sa_family);
534 memcpy (&msg[1], sa, salen);
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537 _("Resolver requests DNS resolution of IP address.\n"));
539 hctx = GNUNET_malloc (sizeof (struct GetHostnameContext));
540 hctx->callback = callback;
542 hctx->client = client;
543 hctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
545 GNUNET_CLIENT_transmit_and_get_response (client,
549 &handle_hostname_response,
553 callback (cls, NULL);
554 GNUNET_CLIENT_disconnect (client);
562 * Maximum supported length of hostname
564 #define MAX_HOSTNAME 1024
568 * Resolve our hostname to an IP address.
570 * @param sched scheduler to use
571 * @param cfg configuration to use
572 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
573 * @param callback function to call with addresses
574 * @param cls closure for callback
575 * @param timeout how long to try resolving
578 GNUNET_RESOLVER_hostname_resolve (struct GNUNET_SCHEDULER_Handle *sched,
579 const struct GNUNET_CONFIGURATION_Handle *cfg,
581 struct GNUNET_TIME_Relative timeout,
582 GNUNET_RESOLVER_AddressCallback callback,
585 char hostname[MAX_HOSTNAME];
588 if (0 != gethostname (hostname, sizeof (hostname) - 1))
590 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
591 GNUNET_ERROR_TYPE_BULK, "gethostname");
592 callback (cls, NULL, 0);
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597 _("Resolving our hostname `%s'\n"), hostname);
599 GNUNET_RESOLVER_ip_get (sched,
600 cfg, hostname, domain, timeout, callback, cls);
606 /* end of resolver_api.c */