2 This file is part of GNUnet.
3 (C) 2007-2013 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 3, 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
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_statistics_service.h"
33 * A cached DNS lookup result (for reverse lookup).
38 * This is a doubly linked list.
43 * This is a doubly linked list.
48 * Hostname in human-readable form.
53 * Binary IP address, allocated at the end of this struct.
58 * Last time this entry was updated.
60 struct GNUNET_TIME_Absolute last_refresh;
63 * Last time this entry was requested.
65 struct GNUNET_TIME_Absolute last_request;
68 * Number of bytes in ip.
73 * Address family of the IP.
80 * Start of the linked list of cached DNS lookup results.
82 static struct IPCache *cache_head;
85 * Tail of the linked list of cached DNS lookup results.
87 static struct IPCache *cache_tail;
92 * Resolve the given request using getnameinfo
94 * @param cache the request to resolve (and where to store the result)
97 getnameinfo_resolve (struct IPCache *cache)
100 const struct sockaddr *sa;
101 struct sockaddr_in v4;
102 struct sockaddr_in6 v6;
108 GNUNET_assert (cache->ip_len == sizeof (struct in_addr));
109 sa = (const struct sockaddr*) &v4;
110 memset (&v4, 0, sizeof (v4));
111 v4.sin_addr = * (const struct in_addr*) cache->ip;
112 v4.sin_family = AF_INET;
113 #if HAVE_SOCKADDR_IN_SIN_LEN
114 v4.sin_len = sizeof (v4);
119 GNUNET_assert (cache->ip_len == sizeof (struct in6_addr));
120 sa = (const struct sockaddr*) &v6;
121 memset (&v6, 0, sizeof (v6));
122 v6.sin6_addr = * (const struct in6_addr*) cache->ip;
123 v6.sin6_family = AF_INET6;
124 #if HAVE_SOCKADDR_IN_SIN_LEN
125 v6.sin6_len = sizeof (v6);
134 getnameinfo (sa, salen,
135 hostname, sizeof (hostname),
139 cache->addr = GNUNET_strdup (hostname);
145 #if HAVE_GETHOSTBYADDR
147 * Resolve the given request using gethostbyaddr
149 * @param cache the request to resolve (and where to store the result)
152 gethostbyaddr_resolve (struct IPCache *cache)
156 ent = gethostbyaddr (cache->ip,
161 cache->addr = GNUNET_strdup (ent->h_name);
168 * Resolve the given request using the available methods.
170 * @param cache the request to resolve (and where to store the result)
173 cache_resolve (struct IPCache *cache)
176 if (NULL == cache->addr)
177 getnameinfo_resolve (cache);
179 #if HAVE_GETHOSTBYADDR
180 if (NULL == cache->addr)
181 gethostbyaddr_resolve (cache);
187 * Get an IP address as a string (works for both IPv4 and IPv6). Note
188 * that the resolution happens asynchronously and that the first call
189 * may not immediately result in the FQN (but instead in a
190 * human-readable IP address).
192 * @param client handle to the client making the request (for sending the reply)
193 * @param af AF_INET or AF_INET6
194 * @param ip `struct in_addr` or `struct in6_addr`
197 get_ip_as_string (struct GNUNET_SERVER_Client *client,
202 struct IPCache *next;
203 struct GNUNET_TIME_Absolute now;
204 struct GNUNET_SERVER_TransmitContext *tc;
211 ip_len = sizeof (struct in_addr);
214 ip_len = sizeof (struct in6_addr);
219 now = GNUNET_TIME_absolute_get ();
221 while ( (NULL != (pos = next)) &&
223 (pos->ip_len != ip_len) ||
224 (0 != memcmp (pos->ip, ip, ip_len))) )
227 if (GNUNET_TIME_absolute_get_duration (pos->last_request).rel_value_us <
228 60 * 60 * 1000 * 1000LL)
230 GNUNET_CONTAINER_DLL_remove (cache_head,
233 GNUNET_free_non_null (pos->addr);
240 if ( (1 == inet_pton (af,
243 (GNUNET_TIME_absolute_get_duration (pos->last_request).rel_value_us >
244 120 * 1000 * 1000LL) )
246 /* try again if still numeric AND 2 minutes have expired */
247 GNUNET_free_non_null (pos->addr);
250 pos->last_request = now;
255 pos = GNUNET_malloc (sizeof (struct IPCache) + ip_len);
257 memcpy (&pos[1], ip, ip_len);
258 pos->last_request = now;
259 pos->last_refresh = now;
260 pos->ip_len = ip_len;
262 GNUNET_CONTAINER_DLL_insert (cache_head,
267 tc = GNUNET_SERVER_transmit_context_create (client);
268 if (NULL != pos->addr)
269 GNUNET_SERVER_transmit_context_append_data (tc, pos->addr,
270 strlen (pos->addr) + 1,
271 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
272 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
273 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
274 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
280 getaddrinfo_resolve (struct GNUNET_SERVER_TransmitContext *tc,
281 const char *hostname, int af)
284 struct addrinfo hints;
285 struct addrinfo *result;
286 struct addrinfo *pos;
289 /* Due to a bug, getaddrinfo will not return a mix of different families */
294 ret1 = getaddrinfo_resolve (tc, hostname, AF_INET);
295 ret2 = getaddrinfo_resolve (tc, hostname, AF_INET6);
296 if ((ret1 == GNUNET_OK) || (ret2 == GNUNET_OK))
298 if ((ret1 == GNUNET_SYSERR) || (ret2 == GNUNET_SYSERR))
299 return GNUNET_SYSERR;
304 memset (&hints, 0, sizeof (struct addrinfo));
305 hints.ai_family = af;
306 hints.ai_socktype = SOCK_STREAM; /* go for TCP */
308 if (0 != (s = getaddrinfo (hostname, NULL, &hints, &result)))
310 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Could not resolve `%s' (%s): %s\n"),
313 AF_INET) ? "IPv4" : ((af == AF_INET6) ? "IPv6" : "any"),
315 if ((s == EAI_BADFLAGS) || (s == EAI_MEMORY)
322 return GNUNET_NO; /* other function may still succeed */
323 return GNUNET_SYSERR;
326 return GNUNET_SYSERR;
327 for (pos = result; pos != NULL; pos = pos->ai_next)
329 switch (pos->ai_family)
332 GNUNET_SERVER_transmit_context_append_data (tc,
333 &((struct sockaddr_in*) pos->ai_addr)->sin_addr,
334 sizeof (struct in_addr),
335 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
338 GNUNET_SERVER_transmit_context_append_data (tc,
339 &((struct sockaddr_in6*) pos->ai_addr)->sin6_addr,
340 sizeof (struct in6_addr),
341 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
344 /* unsupported, skip */
348 freeaddrinfo (result);
354 #if HAVE_GETHOSTBYNAME2
356 gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc,
357 const char *hostname, int af)
364 /* gethostbyname2() in plibc is a compat dummy that calls gethostbyname(). */
370 ret1 = gethostbyname2_resolve (tc, hostname, AF_INET);
371 ret2 = gethostbyname2_resolve (tc, hostname, AF_INET6);
372 if ((ret1 == GNUNET_OK) || (ret2 == GNUNET_OK))
374 if ((ret1 == GNUNET_SYSERR) || (ret2 == GNUNET_SYSERR))
375 return GNUNET_SYSERR;
378 hp = gethostbyname2 (hostname, af);
381 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
382 _("Could not find IP of host `%s': %s\n"), hostname,
383 hstrerror (h_errno));
384 return GNUNET_SYSERR;
386 GNUNET_assert (hp->h_addrtype == af);
390 GNUNET_assert (hp->h_length == sizeof (struct in_addr));
391 GNUNET_SERVER_transmit_context_append_data (tc,
394 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
397 GNUNET_assert (hp->h_length == sizeof (struct in6_addr));
398 GNUNET_SERVER_transmit_context_append_data (tc,
401 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
405 return GNUNET_SYSERR;
412 #if HAVE_GETHOSTBYNAME
414 gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc,
415 const char *hostname)
419 hp = GETHOSTBYNAME (hostname);
422 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
423 _("Could not find IP of host `%s': %s\n"), hostname,
424 hstrerror (h_errno));
425 return GNUNET_SYSERR;
427 if (hp->h_addrtype != AF_INET)
430 return GNUNET_SYSERR;
432 GNUNET_assert (hp->h_length == sizeof (struct in_addr));
433 GNUNET_SERVER_transmit_context_append_data (tc,
436 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
443 * Convert a string to an IP address.
445 * @param client where to send the IP address
446 * @param hostname the hostname to resolve
447 * @param af AF_INET or AF_INET6; use AF_UNSPEC for "any"
450 get_ip_from_hostname (struct GNUNET_SERVER_Client *client,
451 const char *hostname,
455 struct GNUNET_SERVER_TransmitContext *tc;
457 tc = GNUNET_SERVER_transmit_context_create (client);
460 if (ret == GNUNET_NO)
461 ret = getaddrinfo_resolve (tc, hostname, af);
463 #if HAVE_GETHOSTBYNAME2
464 if (ret == GNUNET_NO)
465 ret = gethostbyname2_resolve (tc, hostname, af);
467 #if HAVE_GETHOSTBYNAME
468 if ((ret == GNUNET_NO) && ((af == AF_UNSPEC) || (af == PF_INET)))
469 gethostbyname_resolve (tc, hostname);
471 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
472 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
473 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
478 * Handle GET-message.
481 * @param client identification of the client
482 * @param message the actual message
485 handle_get (void *cls,
486 struct GNUNET_SERVER_Client *client,
487 const struct GNUNET_MessageHeader *message)
490 const struct GNUNET_RESOLVER_GetMessage *msg;
496 msize = ntohs (message->size);
497 if (msize < sizeof (struct GNUNET_RESOLVER_GetMessage))
500 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
503 msg = (const struct GNUNET_RESOLVER_GetMessage *) message;
504 size = msize - sizeof (struct GNUNET_RESOLVER_GetMessage);
505 direction = ntohl (msg->direction);
506 af = ntohl (msg->af);
507 if (direction == GNUNET_NO)
509 /* IP from hostname */
510 const char *hostname;
512 hostname = (const char *) &msg[1];
513 if (hostname[size - 1] != '\0')
516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Resolver asked to look up `%s'.\n",
521 get_ip_from_hostname (client, hostname, af);
528 if (size != sizeof (struct in_addr))
531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
536 if (size != sizeof (struct in6_addr))
539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
545 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
549 char buf[INET6_ADDRSTRLEN];
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "Resolver asked to look up IP address `%s'.\n",
553 inet_ntop (af, ip, buf, sizeof (buf)));
555 get_ip_as_string (client, af, ip);
560 * Process resolver requests.
563 * @param server the initialized server
564 * @param cfg configuration to use
567 run (void *cls, struct GNUNET_SERVER_Handle *server,
568 const struct GNUNET_CONFIGURATION_Handle *cfg)
570 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
571 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST, 0},
574 GNUNET_SERVER_add_handlers (server, handlers);
579 * The main function for the resolver service.
581 * @param argc number of arguments from the command line
582 * @param argv command line arguments
583 * @return 0 ok, 1 on error
586 main (int argc, char *const *argv)
593 GNUNET_SERVICE_run (argc, argv, "resolver", GNUNET_SERVICE_OPTION_NONE,
594 &run, NULL)) ? 0 : 1;
595 while (NULL != (pos = cache_head))
597 GNUNET_CONTAINER_DLL_remove (cache_head,
600 GNUNET_free_non_null (pos->addr);
610 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
612 void __attribute__ ((constructor))
613 GNUNET_ARM_memory_init ()
615 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
616 mallopt (M_TOP_PAD, 1 * 1024);
622 /* end of gnunet-service-resolver.c */