2 This file is part of GNUnet.
3 (C) 2007, 2008, 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/gnunet-service-resolver.c
23 * @brief code to do DNS resolution
24 * @author Christian Grothoff
29 #include "gnunet_disk_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_service_lib.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_strings_lib.h"
35 #include "gnunet_time_lib.h"
39 * A cached DNS lookup result.
44 * This is a linked list.
49 * Hostname in human-readable form.
54 * Hostname in binary format.
59 * Last time this entry was updated.
61 struct GNUNET_TIME_Absolute last_refresh;
64 * Last time this entry was requested.
66 struct GNUNET_TIME_Absolute last_request;
69 * Number of bytes in sa.
76 * Start of the linked list of cached DNS lookup results.
78 static struct IPCache *head;
83 * Resolve the given request using getnameinfo
85 * @param cache the request to resolve (and where to store the result)
88 getnameinfo_resolve (struct IPCache *cache)
92 if (0 == getnameinfo (cache->sa,
94 hostname, sizeof (hostname), NULL, 0, 0))
95 cache->addr = GNUNET_strdup (hostname);
100 #if HAVE_GETHOSTBYADDR
102 * Resolve the given request using gethostbyaddr
104 * @param cache the request to resolve (and where to store the result)
107 gethostbyaddr_resolve (struct IPCache *cache)
111 switch (cache->sa->sa_family)
114 ent = gethostbyaddr (&((struct sockaddr_in *) cache->sa)->sin_addr,
115 sizeof (struct in_addr), AF_INET);
118 ent = gethostbyaddr (&((struct sockaddr_in6 *) cache->sa)->sin6_addr,
119 sizeof (struct in6_addr), AF_INET6);
125 cache->addr = GNUNET_strdup (ent->h_name);
130 * Resolve the given request using the available methods.
132 * @param cache the request to resolve (and where to store the result)
135 cache_resolve (struct IPCache *cache)
138 if (cache->addr == NULL)
139 getnameinfo_resolve (cache);
141 #if HAVE_GETHOSTBYADDR
142 if (cache->addr == NULL)
143 gethostbyaddr_resolve (cache);
150 * Get an IP address as a string (works for both IPv4 and IPv6). Note
151 * that the resolution happens asynchronously and that the first call
152 * may not immediately result in the FQN (but instead in a
153 * human-readable IP address).
155 * @param client handle to the client making the request (for sending the reply)
156 * @param sa should be of type "struct sockaddr*"
157 * @param salen number of bytes in sa
160 get_ip_as_string (struct GNUNET_SERVER_Client *client,
161 const struct sockaddr *sa, socklen_t salen)
163 struct IPCache *cache;
164 struct IPCache *prev;
165 struct GNUNET_TIME_Absolute now;
166 struct GNUNET_SERVER_TransmitContext *tc;
168 if (salen < sizeof (struct sockaddr))
173 now = GNUNET_TIME_absolute_get ();
176 while ((cache != NULL) &&
177 ((cache->salen != salen) || (0 != memcmp (cache->sa, sa, salen))))
179 if (GNUNET_TIME_absolute_get_duration (cache->last_request).value <
184 prev->next = cache->next;
185 GNUNET_free_non_null (cache->addr);
186 GNUNET_free (cache->sa);
193 GNUNET_free_non_null (cache->addr);
194 GNUNET_free (cache->sa);
205 cache->last_request = now;
206 if (GNUNET_TIME_absolute_get_duration (cache->last_request).value <
209 GNUNET_free_non_null (cache->addr);
212 cache_resolve (cache);
217 cache = GNUNET_malloc (sizeof (struct IPCache));
219 cache->salen = salen;
220 cache->sa = GNUNET_malloc (salen);
221 memcpy (cache->sa, sa, salen);
222 cache->last_request = GNUNET_TIME_absolute_get ();
223 cache->last_refresh = GNUNET_TIME_absolute_get ();
225 cache_resolve (cache);
228 tc = GNUNET_SERVER_transmit_context_create (client);
229 if (cache->addr != NULL)
230 GNUNET_SERVER_transmit_context_append (tc,
232 strlen (cache->addr) + 1,
233 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
234 GNUNET_SERVER_transmit_context_append (tc, NULL, 0,
235 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
236 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
242 getaddrinfo_resolve (struct GNUNET_SERVER_TransmitContext *tc,
243 const char *hostname, int domain)
246 struct addrinfo hints;
247 struct addrinfo *result;
248 struct addrinfo *pos;
250 memset (&hints, 0, sizeof (struct addrinfo));
253 hints.ai_family = domain;
255 hints.ai_family = AF_INET;
257 hints.ai_socktype = SOCK_STREAM; /* go for TCP */
259 if (0 != (s = getaddrinfo (hostname, NULL, &hints, &result)))
261 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
262 _("Could not resolve `%s' (%s): %s\n"), hostname,
264 AF_INET) ? "IPv4" : ((domain ==
265 AF_INET6) ? "IPv6" : "any"),
267 if ((s == EAI_BADFLAGS) || (s == EAI_MEMORY) ||
275 return GNUNET_NO; /* other function may still succeed */
276 return GNUNET_SYSERR;
279 return GNUNET_SYSERR;
283 GNUNET_SERVER_transmit_context_append (tc,
286 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
289 freeaddrinfo (result);
294 #if HAVE_GETHOSTBYNAME2
296 gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc,
297 const char *hostname, int domain)
300 struct sockaddr_in a4;
301 struct sockaddr_in6 a6;
305 if (domain == AF_UNSPEC)
307 ret1 = gethostbyname2_resolve (tc, hostname, AF_INET);
308 ret2 = gethostbyname2_resolve (tc, hostname, AF_INET6);
309 if ((ret1 == GNUNET_OK) || (ret2 == GNUNET_OK))
311 if ((ret1 == GNUNET_SYSERR) || (ret2 == GNUNET_SYSERR))
312 return GNUNET_SYSERR;
315 hp = gethostbyname2 (hostname, domain);
318 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
319 _("Could not find IP of host `%s': %s\n"),
320 hostname, hstrerror (h_errno));
321 return GNUNET_SYSERR;
323 GNUNET_assert (hp->h_addrtype == domain);
324 if (domain == AF_INET)
326 GNUNET_assert (hp->h_length == sizeof (struct in_addr));
327 memset (&a4, 0, sizeof (a4));
328 a4.sin_family = AF_INET;
329 memcpy (&a4.sin_addr, hp->h_addr_list[0], hp->h_length);
330 GNUNET_SERVER_transmit_context_append (tc,
333 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
337 GNUNET_assert (hp->h_length == sizeof (struct in6_addr));
338 memset (&a6, 0, sizeof (a6));
339 a6.sin6_family = AF_INET6;
340 memcpy (&a6.sin6_addr, hp->h_addr_list[0], hp->h_length);
341 GNUNET_SERVER_transmit_context_append (tc,
344 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
350 #if HAVE_GETHOSTBYNAME
352 gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc,
353 const char *hostname)
356 struct sockaddr_in addr;
358 hp = GETHOSTBYNAME (hostname);
361 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
362 _("Could not find IP of host `%s': %s\n"),
363 hostname, hstrerror (h_errno));
364 return GNUNET_SYSERR;
366 if (hp->h_addrtype != AF_INET)
369 return GNUNET_SYSERR;
371 GNUNET_assert (hp->h_length == sizeof (struct in_addr));
372 memset (&addr, 0, sizeof (addr));
373 addr.sin_family = AF_INET;
374 memcpy (&addr.sin_addr, hp->h_addr_list[0], hp->h_length);
375 GNUNET_SERVER_transmit_context_append (tc,
378 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
385 * Convert a string to an IP address.
387 * @param client where to send the IP address
388 * @param hostname the hostname to resolve
389 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
392 get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
393 const char *hostname, int domain)
396 struct GNUNET_SERVER_TransmitContext *tc;
398 tc = GNUNET_SERVER_transmit_context_create (client);
401 if (ret == GNUNET_NO)
402 ret = getaddrinfo_resolve (tc, hostname, domain);
404 #if HAVE_GETHOSTBYNAME2
405 if (ret == GNUNET_NO)
406 ret = gethostbyname2_resolve (tc, hostname, domain);
408 #if HAVE_GETHOSTBYNAME
409 if ((ret == GNUNET_NO) && ((domain == AF_UNSPEC) || (domain == PF_INET)))
410 gethostbyname_resolve (tc, hostname);
412 GNUNET_SERVER_transmit_context_append (tc, NULL, 0,
413 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
414 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
419 * Handle GET-message.
422 * @param client identification of the client
423 * @param message the actual message
426 handle_get (void *cls,
427 struct GNUNET_SERVER_Client *client,
428 const struct GNUNET_MessageHeader *message)
431 const struct GNUNET_RESOLVER_GetMessage *msg;
432 const char *hostname;
437 msize = ntohs (message->size);
438 if (msize < sizeof (struct GNUNET_RESOLVER_GetMessage))
441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
444 msg = (const struct GNUNET_RESOLVER_GetMessage *) message;
445 size = msize - sizeof (struct GNUNET_RESOLVER_GetMessage);
446 direction = ntohl (msg->direction);
447 domain = ntohl (msg->domain);
448 if (direction == GNUNET_NO)
450 /* IP from hostname */
451 hostname = (const char *) &msg[1];
452 if (hostname[size - 1] != '\0')
455 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460 _("Resolver asked to look up `%s'.\n"), hostname);
462 get_ip_from_hostname (client, hostname, domain);
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 _("Resolver asked to look up IP address.\n"));
470 get_ip_as_string (client, (const struct sockaddr *) &msg[1], size);
476 * List of handlers for the messages understood by this
479 static struct GNUNET_SERVER_MessageHandler handlers[] = {
480 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST, 0},
486 * Process resolver requests.
489 * @param sched scheduler to use
490 * @param server the initialized server
491 * @param cfg configuration to use
495 struct GNUNET_SCHEDULER_Handle *sched,
496 struct GNUNET_SERVER_Handle *server,
497 const struct GNUNET_CONFIGURATION_Handle *cfg)
499 GNUNET_SERVER_add_handlers (server, handlers);
504 * The main function for the resolver service.
506 * @param argc number of arguments from the command line
507 * @param argv command line arguments
508 * @return 0 ok, 1 on error
511 main (int argc, char *const *argv)
517 GNUNET_SERVICE_run (argc,
519 "resolver", GNUNET_SERVICE_OPTION_NONE,
520 &run, NULL)) ? 0 : 1;
525 GNUNET_free_non_null (head->addr);
526 GNUNET_free (head->sa);
533 /* end of gnunet-service-resolver.c */