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_RESOLVER_GetMessage *msg;
59 struct GNUNET_CLIENT_Connection *client;
64 struct GNUNET_TIME_Absolute timeout;
69 * Possible hostnames for "loopback".
71 static const char *loopback[] = {
79 * Check that the resolver service runs on localhost
83 check_config (const struct GNUNET_CONFIGURATION_Handle *cfg)
91 GNUNET_CONFIGURATION_get_value_string (cfg,
96 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
97 _("Must specify `%s' for `%s' in configuration!\n"),
102 if ( (0 == inet_pton (AF_INET,
105 (0 == inet_pton (AF_INET6,
109 GNUNET_free (hostname);
113 while (loopback[i] != NULL)
114 if (0 == strcmp (loopback[i++], hostname))
116 GNUNET_free (hostname);
119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
120 _("Must specify `%s' for `%s' in configuration!\n"),
123 GNUNET_free (hostname);
129 * Convert IP address to string without DNS resolution.
131 * @param sa the address
132 * @param salen number of bytes in sa
133 * @return address as a string, NULL on error
136 no_resolve (const struct sockaddr *sa, socklen_t salen)
139 char inet4[INET_ADDRSTRLEN];
140 char inet6[INET6_ADDRSTRLEN];
142 if (salen < sizeof (struct sockaddr))
144 switch (sa->sa_family)
147 if (salen != sizeof (struct sockaddr_in))
150 &((struct sockaddr_in *) sa)->sin_addr,
151 inet4, INET_ADDRSTRLEN);
152 ret = GNUNET_strdup (inet4);
155 if (salen != sizeof (struct sockaddr_in6))
158 &((struct sockaddr_in6 *) sa)->sin6_addr,
159 inet6, INET6_ADDRSTRLEN);
160 ret = GNUNET_strdup (inet6);
177 handle_address_response (void *cls, const struct GNUNET_MessageHeader *msg)
179 struct GetAddressContext *gac = cls;
181 const struct sockaddr *sa;
187 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
188 _("Timeout trying to resolve hostname.\n"));
189 gac->callback (gac->cls, NULL, 0);
190 GNUNET_CLIENT_disconnect (gac->client);
194 if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
197 gac->callback (gac->cls, NULL, 0);
198 GNUNET_CLIENT_disconnect (gac->client);
203 size = ntohs (msg->size);
204 if (size == sizeof (struct GNUNET_MessageHeader))
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 _("Received end message resolving hostname.\n"));
210 gac->callback (gac->cls, NULL, 0);
211 GNUNET_CLIENT_disconnect (gac->client);
215 sa = (const struct sockaddr *) &msg[1];
216 salen = size - sizeof (struct GNUNET_MessageHeader);
217 if (salen < sizeof (struct sockaddr))
220 gac->callback (gac->cls, NULL, 0);
221 GNUNET_CLIENT_disconnect (gac->client);
227 char *ips = no_resolve (sa, salen);
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), ips);
232 gac->callback (gac->cls, sa, salen);
233 GNUNET_CLIENT_receive (gac->client,
234 &handle_address_response,
236 GNUNET_TIME_absolute_get_remaining (gac->timeout));
244 * @param size number of bytes available in buf
245 * @param buf target buffer, NULL on error
246 * @return number of bytes written to buf
249 transmit_get_ip (void *cls, size_t size, void *buf)
251 struct GetAddressContext *actx = cls;
256 /* timeout / error */
257 GNUNET_free (actx->msg);
258 actx->callback (actx->cls, NULL, 0);
259 GNUNET_CLIENT_disconnect (actx->client);
263 ms = ntohs (actx->msg->header.size);
264 GNUNET_assert (size >= ms);
265 memcpy (buf, actx->msg, ms);
266 GNUNET_free (actx->msg);
268 GNUNET_CLIENT_receive (actx->client,
269 &handle_address_response,
271 GNUNET_TIME_absolute_get_remaining (actx->timeout));
278 * Convert a string to one or more IP addresses.
280 * @param sched scheduler to use
281 * @param cfg configuration to use
282 * @param hostname the hostname to resolve
283 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
284 * @param callback function to call with addresses
285 * @param callback_cls closure for callback
286 * @param timeout how long to try resolving
289 GNUNET_RESOLVER_ip_get (struct GNUNET_SCHEDULER_Handle *sched,
290 const struct GNUNET_CONFIGURATION_Handle *cfg,
291 const char *hostname,
293 struct GNUNET_TIME_Relative timeout,
294 GNUNET_RESOLVER_AddressCallback callback,
297 struct GNUNET_CLIENT_Connection *client;
298 struct GNUNET_RESOLVER_GetMessage *msg;
299 struct GetAddressContext *actx;
302 struct sockaddr_in v4;
303 struct sockaddr_in6 v6;
305 memset (&v4, 0, sizeof(v4));
306 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
307 v4.sin_family = AF_INET;
308 #if HAVE_SOCKADDR_IN_SIN_LEN
309 v4.sin_len = sizeof(v4);
311 memset (&v6, 0, sizeof(v6));
312 v6.sin6_family = AF_INET6;
313 #if HAVE_SOCKADDR_IN_SIN_LEN
314 v6.sin6_len = sizeof(v6);
316 /* first, check if this is a numeric address */
317 if ( ( (domain == AF_UNSPEC) || (domain == AF_INET) ) &&
318 (0 == inet_pton (AF_INET,
322 callback (callback_cls,
323 (const struct sockaddr*) &v4,
325 if ( (domain == AF_UNSPEC) &&
326 (0 == inet_pton (AF_INET6,
330 /* this can happen on some systems IF "hostname" is "localhost" */
331 callback (callback_cls,
332 (const struct sockaddr*) &v6,
335 callback (callback_cls, NULL, 0);
338 if ( ( (domain == AF_UNSPEC) ||(domain == AF_INET) ) &&
339 (0 == inet_pton (AF_INET6,
343 callback (callback_cls,
344 (const struct sockaddr*) &v6,
346 callback (callback_cls, NULL, 0);
350 /* then, check if this is a loopback address */
352 while (loopback[i] != NULL)
353 if (0 == strcmp (loopback[i++], hostname))
355 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
356 v6.sin6_addr = in6addr_loopback;
360 callback (callback_cls,
361 (const struct sockaddr*) &v4,
365 callback (callback_cls,
366 (const struct sockaddr*) &v6,
370 callback (callback_cls,
371 (const struct sockaddr*) &v6,
373 callback (callback_cls,
374 (const struct sockaddr*) &v4,
378 callback (callback_cls, NULL, 0);
381 slen = strlen (hostname) + 1;
382 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
383 GNUNET_SERVER_MAX_MESSAGE_SIZE)
386 callback (callback_cls, NULL, 0);
389 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
392 callback (callback_cls, NULL, 0);
395 msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
397 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
398 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
399 msg->direction = htonl (GNUNET_NO);
400 msg->domain = htonl (domain);
401 memcpy (&msg[1], hostname, slen);
402 actx = GNUNET_malloc (sizeof (struct GetAddressContext));
403 actx->callback = callback;
404 actx->cls = callback_cls;
405 actx->client = client;
406 actx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 _("Resolver requests DNS resolution of hostname `%s'.\n"),
415 GNUNET_CLIENT_notify_transmit_ready (client,
418 GNUNET_RESOLVER_GetMessage),
419 timeout, &transmit_get_ip, actx))
423 callback (callback_cls, NULL, 0);
424 GNUNET_CLIENT_disconnect (client);
433 struct GetHostnameContext
439 GNUNET_RESOLVER_HostnameCallback callback;
449 struct GNUNET_RESOLVER_GetMessage *msg;
454 struct GNUNET_CLIENT_Connection *client;
459 struct GNUNET_TIME_Absolute timeout;
470 handle_hostname_response (void *cls, const struct GNUNET_MessageHeader *msg)
472 struct GetHostnameContext *ghc = cls;
474 const char *hostname;
478 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
479 _("Timeout trying to resolve IP address.\n"));
480 ghc->callback (ghc->cls, NULL);
481 GNUNET_CLIENT_disconnect (ghc->client);
485 size = ntohs (msg->size);
486 if (size == sizeof (struct GNUNET_MessageHeader))
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 _("Received end message resolving IP address.\n"));
492 ghc->callback (ghc->cls, NULL);
493 GNUNET_CLIENT_disconnect (ghc->client);
497 hostname = (const char *) &msg[1];
498 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
501 ghc->callback (ghc->cls, NULL);
502 GNUNET_CLIENT_disconnect (ghc->client);
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 _("Resolver returns `%s'.\n"), hostname);
510 ghc->callback (ghc->cls, hostname);
511 GNUNET_CLIENT_receive (ghc->client,
512 &handle_hostname_response,
514 GNUNET_TIME_absolute_get_remaining (ghc->timeout));
522 * @param size number of bytes available in buf
523 * @param buf target buffer, NULL on error
524 * @return number of bytes written to buf
527 transmit_get_hostname (void *cls, size_t size, void *buf)
529 struct GetHostnameContext *hctx = cls;
534 GNUNET_free (hctx->msg);
535 hctx->callback (hctx->cls, NULL);
536 GNUNET_CLIENT_disconnect (hctx->client);
540 msize = ntohs (hctx->msg->header.size);
541 GNUNET_assert (size >= msize);
542 memcpy (buf, hctx->msg, msize);
543 GNUNET_free (hctx->msg);
545 GNUNET_CLIENT_receive (hctx->client,
546 &handle_hostname_response,
548 GNUNET_TIME_absolute_get_remaining (hctx->timeout));
556 * Get an IP address as a string.
558 * @param sched scheduler to use
559 * @param cfg configuration to use
560 * @param sa host address
561 * @param salen length of host address
562 * @param do_resolve use GNUNET_NO to return numeric hostname
563 * @param timeout how long to try resolving
564 * @param callback function to call with hostnames
565 * @param cls closure for callback
568 GNUNET_RESOLVER_hostname_get (struct GNUNET_SCHEDULER_Handle *sched,
569 const struct GNUNET_CONFIGURATION_Handle *cfg,
570 const struct sockaddr *sa,
573 struct GNUNET_TIME_Relative timeout,
574 GNUNET_RESOLVER_HostnameCallback callback,
578 struct GNUNET_CLIENT_Connection *client;
579 struct GNUNET_RESOLVER_GetMessage *msg;
580 struct GetHostnameContext *hctx;
583 if (GNUNET_NO == do_resolve)
585 result = no_resolve (sa, salen);
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 _("Resolver returns `%s'.\n"), result);
590 callback (cls, result);
593 GNUNET_free (result);
594 callback (cls, NULL);
598 if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
599 GNUNET_SERVER_MAX_MESSAGE_SIZE)
602 callback (cls, NULL);
605 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
608 callback (cls, NULL);
611 msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
613 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
614 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
615 msg->direction = htonl (GNUNET_YES);
616 msg->domain = htonl (sa->sa_family);
617 memcpy (&msg[1], sa, salen);
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
620 _("Resolver requests DNS resolution of IP address.\n"));
622 hctx = GNUNET_malloc (sizeof (struct GetHostnameContext));
623 hctx->callback = callback;
625 hctx->client = client;
626 hctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
629 GNUNET_CLIENT_notify_transmit_ready (client,
631 GNUNET_RESOLVER_GetMessage)
633 &transmit_get_hostname, hctx))
636 callback (cls, NULL);
637 GNUNET_CLIENT_disconnect (client);
643 * Maximum supported length of hostname
645 #define MAX_HOSTNAME 1024
649 * Resolve our hostname to an IP address.
651 * @param sched scheduler to use
652 * @param cfg configuration to use
653 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
654 * @param callback function to call with addresses
655 * @param cls closure for callback
656 * @param timeout how long to try resolving
659 GNUNET_RESOLVER_hostname_resolve (struct GNUNET_SCHEDULER_Handle *sched,
660 const struct GNUNET_CONFIGURATION_Handle *cfg,
662 struct GNUNET_TIME_Relative timeout,
663 GNUNET_RESOLVER_AddressCallback callback,
666 char hostname[MAX_HOSTNAME];
669 if (0 != gethostname (hostname, sizeof (hostname) - 1))
671 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
672 GNUNET_ERROR_TYPE_BULK, "gethostname");
673 callback (cls, NULL, 0);
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678 _("Resolving our hostname `%s'\n"), hostname);
680 GNUNET_RESOLVER_ip_get (sched,
681 cfg, hostname, domain, timeout, callback, cls);
687 /* end of resolver_api.c */