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[] = {
81 * Check that the resolver service runs on localhost
85 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"),
103 while (loopback[i] != NULL)
104 if (0 == strcmp (loopback[i++], hostname))
106 GNUNET_free (hostname);
109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
110 _("Must specify `%s' for `%s' in configuration!\n"),
113 GNUNET_free (hostname);
119 * Convert IP address to string without DNS resolution.
122 no_resolve (const struct sockaddr *sa, socklen_t salen)
125 char inet4[INET_ADDRSTRLEN];
126 char inet6[INET6_ADDRSTRLEN];
128 if (salen < sizeof (struct sockaddr))
130 switch (sa->sa_family)
133 if (salen != sizeof (struct sockaddr_in))
136 &((struct sockaddr_in *) sa)->sin_addr,
137 inet4, INET_ADDRSTRLEN);
138 ret = GNUNET_strdup (inet4);
141 if (salen != sizeof (struct sockaddr_in6))
144 &((struct sockaddr_in6 *) sa)->sin6_addr,
145 inet6, INET6_ADDRSTRLEN);
146 ret = GNUNET_strdup (inet6);
157 handle_address_response (void *cls, const struct GNUNET_MessageHeader *msg)
159 struct GetAddressContext *gac = cls;
161 const struct sockaddr *sa;
167 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
168 _("Timeout trying to resolve hostname.\n"));
169 gac->callback (gac->cls, NULL, 0);
170 GNUNET_CLIENT_disconnect (gac->client);
174 if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
177 gac->callback (gac->cls, NULL, 0);
178 GNUNET_CLIENT_disconnect (gac->client);
183 size = ntohs (msg->size);
184 if (size == sizeof (struct GNUNET_MessageHeader))
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 _("Received end message resolving hostname.\n"));
190 gac->callback (gac->cls, NULL, 0);
191 GNUNET_CLIENT_disconnect (gac->client);
195 sa = (const struct sockaddr *) &msg[1];
196 salen = size - sizeof (struct GNUNET_MessageHeader);
197 if (salen < sizeof (struct sockaddr))
200 gac->callback (gac->cls, NULL, 0);
201 GNUNET_CLIENT_disconnect (gac->client);
207 char *ips = no_resolve (sa, salen);
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), ips);
212 gac->callback (gac->cls, sa, salen);
213 GNUNET_CLIENT_receive (gac->client,
214 &handle_address_response,
216 GNUNET_TIME_absolute_get_remaining (gac->timeout));
221 transmit_get_ip (void *cls, size_t size, void *buf)
223 struct GetAddressContext *actx = cls;
228 /* timeout / error */
229 GNUNET_free (actx->msg);
230 actx->callback (actx->cls, NULL, 0);
231 GNUNET_CLIENT_disconnect (actx->client);
235 ms = ntohs (actx->msg->header.size);
236 GNUNET_assert (size >= ms);
237 memcpy (buf, actx->msg, ms);
238 GNUNET_free (actx->msg);
240 GNUNET_CLIENT_receive (actx->client,
241 &handle_address_response,
243 GNUNET_TIME_absolute_get_remaining (actx->timeout));
250 * Convert a string to one or more IP addresses.
252 * @param sched scheduler to use
253 * @param cfg configuration to use
254 * @param hostname the hostname to resolve
255 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
256 * @param callback function to call with addresses
257 * @param callback_cls closure for callback
258 * @param timeout how long to try resolving
261 GNUNET_RESOLVER_ip_get (struct GNUNET_SCHEDULER_Handle *sched,
262 const struct GNUNET_CONFIGURATION_Handle *cfg,
263 const char *hostname,
265 struct GNUNET_TIME_Relative timeout,
266 GNUNET_RESOLVER_AddressCallback callback,
269 struct GNUNET_CLIENT_Connection *client;
270 struct GNUNET_RESOLVER_GetMessage *msg;
271 struct GetAddressContext *actx;
274 struct sockaddr_in v4;
275 struct sockaddr_in6 v6;
279 while (loopback[i] != NULL)
280 if (0 == strcmp (loopback[i++], hostname))
282 memset (&v4, 0, sizeof(v4));
283 #if HAVE_SOCKADDR_IN_SIN_LEN
284 v4.sin_len = sizeof (v4);
286 v4.sin_family = AF_INET;
287 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
289 memset (&v6, 0, sizeof(v6));
290 #if HAVE_SOCKADDR_IN_SIN_LEN
291 v6.sin6_len = sizeof (v6);
293 v6.sin6_family = AF_INET6;
294 v6.sin6_addr = in6addr_loopback;
299 callback (callback_cls,
300 (const struct sockaddr*) &v4,
304 callback (callback_cls,
305 (const struct sockaddr*) &v6,
309 callback (callback_cls,
310 (const struct sockaddr*) &v4,
312 callback (callback_cls,
313 (const struct sockaddr*) &v6,
317 callback (callback_cls, NULL, 0);
320 slen = strlen (hostname) + 1;
321 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
322 GNUNET_SERVER_MAX_MESSAGE_SIZE)
325 callback (callback_cls, NULL, 0);
328 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
331 callback (callback_cls, NULL, 0);
334 msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
336 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
337 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
338 msg->direction = htonl (GNUNET_NO);
339 msg->domain = htonl (domain);
340 memcpy (&msg[1], hostname, slen);
341 actx = GNUNET_malloc (sizeof (struct GetAddressContext));
342 actx->callback = callback;
343 actx->cls = callback_cls;
344 actx->client = client;
345 actx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 _("Resolver requests DNS resolution of hostname `%s'.\n"),
354 GNUNET_CLIENT_notify_transmit_ready (client,
357 GNUNET_RESOLVER_GetMessage),
358 timeout, &transmit_get_ip, actx))
362 callback (callback_cls, NULL, 0);
363 GNUNET_CLIENT_disconnect (client);
369 struct GetHostnameContext
371 GNUNET_RESOLVER_HostnameCallback callback;
373 struct GNUNET_RESOLVER_GetMessage *msg;
374 struct GNUNET_CLIENT_Connection *client;
375 struct GNUNET_TIME_Absolute timeout;
380 handle_hostname_response (void *cls, const struct GNUNET_MessageHeader *msg)
382 struct GetHostnameContext *ghc = cls;
384 const char *hostname;
388 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
389 _("Timeout trying to resolve IP address.\n"));
390 ghc->callback (ghc->cls, NULL);
391 GNUNET_CLIENT_disconnect (ghc->client);
395 size = ntohs (msg->size);
396 if (size == sizeof (struct GNUNET_MessageHeader))
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 _("Received end message resolving IP address.\n"));
402 ghc->callback (ghc->cls, NULL);
403 GNUNET_CLIENT_disconnect (ghc->client);
407 hostname = (const char *) &msg[1];
408 if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
411 ghc->callback (ghc->cls, NULL);
412 GNUNET_CLIENT_disconnect (ghc->client);
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 _("Resolver returns `%s'.\n"), hostname);
420 ghc->callback (ghc->cls, hostname);
421 GNUNET_CLIENT_receive (ghc->client,
422 &handle_hostname_response,
424 GNUNET_TIME_absolute_get_remaining (ghc->timeout));
429 transmit_get_hostname (void *cls, size_t size, void *buf)
431 struct GetHostnameContext *hctx = cls;
436 GNUNET_free (hctx->msg);
437 hctx->callback (hctx->cls, NULL);
438 GNUNET_CLIENT_disconnect (hctx->client);
442 msize = ntohs (hctx->msg->header.size);
443 GNUNET_assert (size >= msize);
444 memcpy (buf, hctx->msg, msize);
445 GNUNET_free (hctx->msg);
447 GNUNET_CLIENT_receive (hctx->client,
448 &handle_hostname_response,
450 GNUNET_TIME_absolute_get_remaining (hctx->timeout));
458 * Get an IP address as a string.
460 * @param sched scheduler to use
461 * @param cfg configuration to use
462 * @param sa host address
463 * @param salen length of host address
464 * @param do_resolve use GNUNET_NO to return numeric hostname
465 * @param timeout how long to try resolving
466 * @param callback function to call with hostnames
467 * @param cls closure for callback
470 GNUNET_RESOLVER_hostname_get (struct GNUNET_SCHEDULER_Handle *sched,
471 const struct GNUNET_CONFIGURATION_Handle *cfg,
472 const struct sockaddr *sa,
475 struct GNUNET_TIME_Relative timeout,
476 GNUNET_RESOLVER_HostnameCallback callback,
480 struct GNUNET_CLIENT_Connection *client;
481 struct GNUNET_RESOLVER_GetMessage *msg;
482 struct GetHostnameContext *hctx;
485 if (GNUNET_NO == do_resolve)
487 result = no_resolve (sa, salen);
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 _("Resolver returns `%s'.\n"), result);
492 callback (cls, result);
495 GNUNET_free (result);
496 callback (cls, NULL);
500 if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
501 GNUNET_SERVER_MAX_MESSAGE_SIZE)
504 callback (cls, NULL);
507 client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
510 callback (cls, NULL);
513 msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
515 htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
516 msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
517 msg->direction = htonl (GNUNET_YES);
518 msg->domain = htonl (sa->sa_family);
519 memcpy (&msg[1], sa, salen);
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522 _("Resolver requests DNS resolution of IP address.\n"));
524 hctx = GNUNET_malloc (sizeof (struct GetHostnameContext));
525 hctx->callback = callback;
527 hctx->client = client;
528 hctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
531 GNUNET_CLIENT_notify_transmit_ready (client,
533 GNUNET_RESOLVER_GetMessage)
535 &transmit_get_hostname, hctx))
538 callback (cls, NULL);
539 GNUNET_CLIENT_disconnect (client);
545 * Maximum supported length of hostname
547 #define MAX_HOSTNAME 1024
551 * Resolve our hostname to an IP address.
553 * @param sched scheduler to use
554 * @param cfg configuration to use
555 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
556 * @param callback function to call with addresses
557 * @param cls closure for callback
558 * @param timeout how long to try resolving
561 GNUNET_RESOLVER_hostname_resolve (struct GNUNET_SCHEDULER_Handle *sched,
562 const struct GNUNET_CONFIGURATION_Handle *cfg,
564 struct GNUNET_TIME_Relative timeout,
565 GNUNET_RESOLVER_AddressCallback callback,
568 char hostname[MAX_HOSTNAME];
571 if (0 != gethostname (hostname, sizeof (hostname) - 1))
573 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
574 GNUNET_ERROR_TYPE_BULK, "gethostname");
575 callback (cls, NULL, 0);
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
580 _("Resolving our hostname `%s'\n"), hostname);
582 GNUNET_RESOLVER_ip_get (sched,
583 cfg, hostname, domain, timeout, callback, cls);
589 /* end of resolver_api.c */