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 resolver/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,
110 while (loopback[i] != NULL)
111 if (0 == strcmp (loopback[i++], hostname))
113 GNUNET_free (hostname);
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
117 _("Must specify `%s' for `%s' in configuration!\n"),
120 GNUNET_free (hostname);
126 * Convert IP address to string without DNS resolution.
129 no_resolve (const struct sockaddr *sa, socklen_t salen)
132 char inet4[INET_ADDRSTRLEN];
133 char inet6[INET6_ADDRSTRLEN];
135 if (salen < sizeof (struct sockaddr))
137 switch (sa->sa_family)
140 if (salen != sizeof (struct sockaddr_in))
143 &((struct sockaddr_in *) sa)->sin_addr,
144 inet4, INET_ADDRSTRLEN);
145 ret = GNUNET_strdup (inet4);
148 if (salen != sizeof (struct sockaddr_in6))
151 &((struct sockaddr_in6 *) sa)->sin6_addr,
152 inet6, INET6_ADDRSTRLEN);
153 ret = GNUNET_strdup (inet6);
164 handle_address_response (void *cls, const struct GNUNET_MessageHeader *msg)
166 struct GetAddressContext *gac = cls;
168 const struct sockaddr *sa;
174 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
175 _("Timeout trying to resolve hostname.\n"));
176 gac->callback (gac->cls, NULL, 0);
177 GNUNET_CLIENT_disconnect (gac->client);
181 if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
184 gac->callback (gac->cls, NULL, 0);
185 GNUNET_CLIENT_disconnect (gac->client);
190 size = ntohs (msg->size);
191 if (size == sizeof (struct GNUNET_MessageHeader))
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 _("Received end message resolving hostname.\n"));
197 gac->callback (gac->cls, NULL, 0);
198 GNUNET_CLIENT_disconnect (gac->client);
202 sa = (const struct sockaddr *) &msg[1];
203 salen = size - sizeof (struct GNUNET_MessageHeader);
204 if (salen < sizeof (struct sockaddr))
207 gac->callback (gac->cls, NULL, 0);
208 GNUNET_CLIENT_disconnect (gac->client);
214 char *ips = no_resolve (sa, salen);
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), ips);
219 gac->callback (gac->cls, sa, salen);
220 GNUNET_CLIENT_receive (gac->client,
221 &handle_address_response,
223 GNUNET_TIME_absolute_get_remaining (gac->timeout));
228 transmit_get_ip (void *cls, size_t size, void *buf)
230 struct GetAddressContext *actx = cls;
235 /* timeout / error */
236 GNUNET_free (actx->msg);
237 actx->callback (actx->cls, NULL, 0);
238 GNUNET_CLIENT_disconnect (actx->client);
242 ms = ntohs (actx->msg->header.size);
243 GNUNET_assert (size >= ms);
244 memcpy (buf, actx->msg, ms);
245 GNUNET_free (actx->msg);
247 GNUNET_CLIENT_receive (actx->client,
248 &handle_address_response,
250 GNUNET_TIME_absolute_get_remaining (actx->timeout));
257 * Convert a string to one or more IP addresses.
259 * @param sched scheduler to use
260 * @param cfg configuration to use
261 * @param hostname the hostname to resolve
262 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
263 * @param callback function to call with addresses
264 * @param callback_cls closure for callback
265 * @param timeout how long to try resolving
268 GNUNET_RESOLVER_ip_get (struct GNUNET_SCHEDULER_Handle *sched,
269 const struct GNUNET_CONFIGURATION_Handle *cfg,
270 const char *hostname,
272 struct GNUNET_TIME_Relative timeout,
273 GNUNET_RESOLVER_AddressCallback callback,
276 struct GNUNET_CLIENT_Connection *client;
277 struct GNUNET_RESOLVER_GetMessage *msg;
278 struct GetAddressContext *actx;
281 struct sockaddr_in v4;
282 struct sockaddr_in6 v6;
284 memset (&v4, 0, sizeof(v4));
285 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
286 v4.sin_family = AF_INET;
287 #if HAVE_SOCKADDR_IN_SIN_LEN
288 v4.sin_len = sizeof(v4);
290 memset (&v6, 0, sizeof(v6));
291 v6.sin6_family = AF_INET6;
292 #if HAVE_SOCKADDR_IN_SIN_LEN
293 v6.sin6_len = sizeof(v6);
295 /* first, check if this is a numeric address */
296 if ( ( (domain == AF_UNSPEC) ||(domain == AF_INET) ) &&
297 (0 == inet_pton (AF_INET,
301 callback (callback_cls,
302 (const struct sockaddr*) &v4,
304 callback (callback_cls, NULL, 0);
307 if ( ( (domain == AF_UNSPEC) ||(domain == AF_INET) ) &&
308 (0 == inet_pton (AF_INET6,
312 callback (callback_cls,
313 (const struct sockaddr*) &v6,
315 callback (callback_cls, NULL, 0);
319 /* then, check if this is a loopback address */
321 while (loopback[i] != NULL)
322 if (0 == strcmp (loopback[i++], hostname))
324 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
325 v6.sin6_addr = in6addr_loopback;
329 callback (callback_cls,
330 (const struct sockaddr*) &v4,
334 callback (callback_cls,
335 (const struct sockaddr*) &v6,
339 callback (callback_cls,
340 (const struct sockaddr*) &v6,
342 callback (callback_cls,
343 (const struct sockaddr*) &v4,
347 callback (callback_cls, NULL, 0);
350 slen = strlen (hostname) + 1;
351 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
352 GNUNET_SERVER_MAX_MESSAGE_SIZE)
355 callback (callback_cls, NULL, 0);
358 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
361 callback (callback_cls, NULL, 0);
364 msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
366 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
367 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
368 msg->direction = htonl (GNUNET_NO);
369 msg->domain = htonl (domain);
370 memcpy (&msg[1], hostname, slen);
371 actx = GNUNET_malloc (sizeof (struct GetAddressContext));
372 actx->callback = callback;
373 actx->cls = callback_cls;
374 actx->client = client;
375 actx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 _("Resolver requests DNS resolution of hostname `%s'.\n"),
384 GNUNET_CLIENT_notify_transmit_ready (client,
387 GNUNET_RESOLVER_GetMessage),
388 timeout, &transmit_get_ip, actx))
392 callback (callback_cls, NULL, 0);
393 GNUNET_CLIENT_disconnect (client);
399 struct GetHostnameContext
401 GNUNET_RESOLVER_HostnameCallback callback;
403 struct GNUNET_RESOLVER_GetMessage *msg;
404 struct GNUNET_CLIENT_Connection *client;
405 struct GNUNET_TIME_Absolute timeout;
410 handle_hostname_response (void *cls, const struct GNUNET_MessageHeader *msg)
412 struct GetHostnameContext *ghc = cls;
414 const char *hostname;
418 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
419 _("Timeout trying to resolve IP address.\n"));
420 ghc->callback (ghc->cls, NULL);
421 GNUNET_CLIENT_disconnect (ghc->client);
425 size = ntohs (msg->size);
426 if (size == sizeof (struct GNUNET_MessageHeader))
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430 _("Received end message resolving IP address.\n"));
432 ghc->callback (ghc->cls, NULL);
433 GNUNET_CLIENT_disconnect (ghc->client);
437 hostname = (const char *) &msg[1];
438 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
441 ghc->callback (ghc->cls, NULL);
442 GNUNET_CLIENT_disconnect (ghc->client);
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 _("Resolver returns `%s'.\n"), hostname);
450 ghc->callback (ghc->cls, hostname);
451 GNUNET_CLIENT_receive (ghc->client,
452 &handle_hostname_response,
454 GNUNET_TIME_absolute_get_remaining (ghc->timeout));
459 transmit_get_hostname (void *cls, size_t size, void *buf)
461 struct GetHostnameContext *hctx = cls;
466 GNUNET_free (hctx->msg);
467 hctx->callback (hctx->cls, NULL);
468 GNUNET_CLIENT_disconnect (hctx->client);
472 msize = ntohs (hctx->msg->header.size);
473 GNUNET_assert (size >= msize);
474 memcpy (buf, hctx->msg, msize);
475 GNUNET_free (hctx->msg);
477 GNUNET_CLIENT_receive (hctx->client,
478 &handle_hostname_response,
480 GNUNET_TIME_absolute_get_remaining (hctx->timeout));
488 * Get an IP address as a string.
490 * @param sched scheduler to use
491 * @param cfg configuration to use
492 * @param sa host address
493 * @param salen length of host address
494 * @param do_resolve use GNUNET_NO to return numeric hostname
495 * @param timeout how long to try resolving
496 * @param callback function to call with hostnames
497 * @param cls closure for callback
500 GNUNET_RESOLVER_hostname_get (struct GNUNET_SCHEDULER_Handle *sched,
501 const struct GNUNET_CONFIGURATION_Handle *cfg,
502 const struct sockaddr *sa,
505 struct GNUNET_TIME_Relative timeout,
506 GNUNET_RESOLVER_HostnameCallback callback,
510 struct GNUNET_CLIENT_Connection *client;
511 struct GNUNET_RESOLVER_GetMessage *msg;
512 struct GetHostnameContext *hctx;
515 if (GNUNET_NO == do_resolve)
517 result = no_resolve (sa, salen);
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 _("Resolver returns `%s'.\n"), result);
522 callback (cls, result);
525 GNUNET_free (result);
526 callback (cls, NULL);
530 if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
531 GNUNET_SERVER_MAX_MESSAGE_SIZE)
534 callback (cls, NULL);
537 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
540 callback (cls, NULL);
543 msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
545 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
546 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
547 msg->direction = htonl (GNUNET_YES);
548 msg->domain = htonl (sa->sa_family);
549 memcpy (&msg[1], sa, salen);
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 _("Resolver requests DNS resolution of IP address.\n"));
554 hctx = GNUNET_malloc (sizeof (struct GetHostnameContext));
555 hctx->callback = callback;
557 hctx->client = client;
558 hctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
561 GNUNET_CLIENT_notify_transmit_ready (client,
563 GNUNET_RESOLVER_GetMessage)
565 &transmit_get_hostname, hctx))
568 callback (cls, NULL);
569 GNUNET_CLIENT_disconnect (client);
575 * Maximum supported length of hostname
577 #define MAX_HOSTNAME 1024
581 * Resolve our hostname to an IP address.
583 * @param sched scheduler to use
584 * @param cfg configuration to use
585 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
586 * @param callback function to call with addresses
587 * @param cls closure for callback
588 * @param timeout how long to try resolving
591 GNUNET_RESOLVER_hostname_resolve (struct GNUNET_SCHEDULER_Handle *sched,
592 const struct GNUNET_CONFIGURATION_Handle *cfg,
594 struct GNUNET_TIME_Relative timeout,
595 GNUNET_RESOLVER_AddressCallback callback,
598 char hostname[MAX_HOSTNAME];
601 if (0 != gethostname (hostname, sizeof (hostname) - 1))
603 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
604 GNUNET_ERROR_TYPE_BULK, "gethostname");
605 callback (cls, NULL, 0);
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
610 _("Resolving our hostname `%s'\n"), hostname);
612 GNUNET_RESOLVER_ip_get (sched,
613 cfg, hostname, domain, timeout, callback, cls);
619 /* end of resolver_api.c */