2 This file is part of GNUnet.
3 Copyright (C) 2007-2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file util/gnunet-service-resolver.c
21 * @brief code to do DNS resolution
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_statistics_service.h"
32 * How long do we wait for DNS answers?
34 #define DNS_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
37 * Maximum number of hostnames we cache results for.
39 #define MAX_CACHE 1024
42 * Entry in list of cached DNS records for a hostname.
44 struct RecordListEntry
47 * This is a doubly linked list.
49 struct RecordListEntry *next;
52 * This is a doubly linked list.
54 struct RecordListEntry *prev;
59 struct GNUNET_DNSPARSER_Record *record;
64 * A cached DNS lookup result.
69 * This is a doubly linked list.
71 struct ResolveCache *next;
74 * This is a doubly linked list.
76 struct ResolveCache *prev;
79 * Which hostname is this cache for?
84 * head of a double linked list containing the lookup results
86 struct RecordListEntry *records_head;
89 * tail of a double linked list containing the lookup results
91 struct RecordListEntry *records_tail;
97 * Information about pending lookups.
104 struct ActiveLookup *next;
109 struct ActiveLookup *prev;
112 * The client that queried the records contained in this cache entry.
114 struct GNUNET_SERVICE_Client *client;
117 * handle for cancelling a request
119 struct GNUNET_DNSSTUB_RequestSocket *resolve_handle;
122 * handle for the resolution timeout task
124 struct GNUNET_SCHEDULER_Task *timeout_task;
127 * Which hostname are we resolving?
132 * type of queried DNS record
134 uint16_t record_type;
137 * Unique request ID of a client if a query for this hostname/record_type
138 * is currently pending, undefined otherwise.
143 * Unique DNS request ID of a client if a query for this hostname/record_type
144 * is currently pending, undefined otherwise.
152 * Start of the linked list of cached DNS lookup results.
154 static struct ResolveCache *cache_head;
157 * Tail of the linked list of cached DNS lookup results.
159 static struct ResolveCache *cache_tail;
162 * Start of the linked list of active DNS lookups.
164 static struct ActiveLookup *lookup_head;
167 * Tail of the linked list of active DNS lookups.
169 static struct ActiveLookup *lookup_tail;
172 * context of dnsstub library
174 static struct GNUNET_DNSSTUB_Context *dnsstub_ctx;
177 * How many entries do we have in #cache_head DLL?
179 static unsigned int cache_size;
182 * Remove @a entry from cache.
184 * @param rc entry to free
187 free_cache_entry (struct ResolveCache *rc)
189 struct RecordListEntry *pos;
191 while (NULL != (pos = rc->records_head))
193 GNUNET_CONTAINER_DLL_remove (rc->records_head,
196 GNUNET_DNSPARSER_free_record (pos->record);
197 GNUNET_free (pos->record);
200 GNUNET_free_non_null (rc->hostname);
201 GNUNET_CONTAINER_DLL_remove (cache_head,
210 * Release resources associated with @a al
212 * @param al an active lookup
215 free_active_lookup (struct ActiveLookup *al)
217 GNUNET_CONTAINER_DLL_remove (lookup_head,
220 if (NULL != al->resolve_handle)
222 GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
223 al->resolve_handle = NULL;
225 if (NULL != al->timeout_task)
227 GNUNET_SCHEDULER_cancel (al->timeout_task);
228 al->timeout_task = NULL;
230 GNUNET_free_non_null (al->hostname);
237 * Find out if the configuration file line contains a string
238 * starting with "nameserver ", and if so, return a copy of
239 * the nameserver's IP.
241 * @param line line to parse
242 * @param line_len number of characters in @a line
243 * @return NULL if no nameserver is configured in this @a line
246 extract_dns_server (const char* line,
249 if (0 == strncmp (line,
251 strlen ("nameserver ")))
252 return GNUNET_strndup (line + strlen ("nameserver "),
253 line_len - strlen ("nameserver "));
259 * Reads the list of nameservers from /etc/resolve.conf
261 * @param server_addrs[out] a list of null-terminated server address strings
262 * @return the number of server addresses in @server_addrs, -1 on error
265 lookup_dns_servers (char ***server_addrs)
267 struct GNUNET_DISK_FileHandle *fh;
271 unsigned int num_dns_servers;
273 fh = GNUNET_DISK_file_open ("/etc/resolv.conf",
274 GNUNET_DISK_OPEN_READ,
275 GNUNET_DISK_PERM_NONE);
278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
279 "Could not open /etc/resolv.conf. "
280 "DNS resolution will not be possible.\n");
283 bytes_read = GNUNET_DISK_file_read (fh,
286 *server_addrs = NULL;
289 while (read_offset < bytes_read)
295 newline = strchr (buf + read_offset,
299 line_len = newline - buf - read_offset;
300 dns_server = extract_dns_server (buf + read_offset,
302 if (NULL != dns_server)
303 GNUNET_array_append (*server_addrs,
306 read_offset += line_len + 1;
308 GNUNET_DISK_file_close (fh);
309 return (int) num_dns_servers;
314 * Compute name to use for DNS reverse lookups from @a ip.
316 * @param ip IP address to resolve, in binary format, network byte order
317 * @param af address family of @a ip, AF_INET or AF_INET6
320 make_reverse_hostname (const void *ip,
323 char *buf = GNUNET_new_array (80,
329 struct in_addr *addr = (struct in_addr *)ip;
330 uint32_t ip_int = addr->s_addr;
332 for (int i = 3; i >= 0; i--)
334 int n = GNUNET_snprintf (buf + pos,
337 ((uint8_t *)&ip_int)[i]);
345 pos += GNUNET_snprintf (buf + pos,
349 else if (AF_INET6 == af)
351 struct in6_addr *addr = (struct in6_addr *)ip;
352 for (int i = 15; i >= 0; i--)
354 int n = GNUNET_snprintf (buf + pos,
357 addr->s6_addr[i] & 0xf);
364 n = GNUNET_snprintf (buf + pos,
367 addr->s6_addr[i] >> 4);
375 pos += GNUNET_snprintf (buf + pos,
385 * Send DNS @a record back to our @a client.
387 * @param record information to transmit
388 * @param record_type requested record type from client
389 * @param request_id to which request are we responding
390 * @param client where to send @a record
391 * @return #GNUNET_YES if we sent a reply,
392 * #GNUNET_NO if the record type is not understood or
393 * does not match @a record_type
396 send_reply (struct GNUNET_DNSPARSER_Record *record,
397 uint16_t record_type,
399 struct GNUNET_SERVICE_Client *client)
401 struct GNUNET_RESOLVER_ResponseMessage *msg;
402 struct GNUNET_MQ_Envelope *env;
406 switch (record->type)
408 case GNUNET_DNSPARSER_TYPE_CNAME:
409 if (GNUNET_DNSPARSER_TYPE_CNAME != record_type)
411 payload = record->data.hostname;
412 payload_len = strlen (record->data.hostname) + 1;
414 case GNUNET_DNSPARSER_TYPE_PTR:
415 if (GNUNET_DNSPARSER_TYPE_PTR != record_type)
417 payload = record->data.hostname;
418 payload_len = strlen (record->data.hostname) + 1;
420 case GNUNET_DNSPARSER_TYPE_A:
421 if ( (GNUNET_DNSPARSER_TYPE_A != record_type) &&
422 (GNUNET_DNSPARSER_TYPE_ALL != record_type) )
424 payload = record->data.raw.data;
425 payload_len = record->data.raw.data_len;
427 case GNUNET_DNSPARSER_TYPE_AAAA:
428 if ( (GNUNET_DNSPARSER_TYPE_AAAA != record_type) &&
429 (GNUNET_DNSPARSER_TYPE_ALL != record_type) )
431 payload = record->data.raw.data;
432 payload_len = record->data.raw.data_len;
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "Cannot handle DNS response type %u: not supported here\n",
440 env = GNUNET_MQ_msg_extra (msg,
442 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
443 msg->id = request_id;
444 GNUNET_memcpy (&msg[1],
447 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
454 * Send message to @a client that we transmitted all
455 * responses for @a request_id
457 * @param request_id to which request are we responding
458 * @param client where to send @a record
461 send_end_msg (uint16_t request_id,
462 struct GNUNET_SERVICE_Client *client)
464 struct GNUNET_RESOLVER_ResponseMessage *msg;
465 struct GNUNET_MQ_Envelope *env;
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "Sending END message\n");
469 env = GNUNET_MQ_msg (msg,
470 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
471 msg->id = request_id;
472 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
478 * Remove expired entries from @a rc
480 * @param rc entry in resolver cache
481 * @return #GNUNET_YES if @a rc was completely expired
482 * #GNUNET_NO if some entries are left
485 remove_expired (struct ResolveCache *rc)
487 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
488 struct RecordListEntry *n;
490 for (struct RecordListEntry *pos = rc->records_head;
495 if (now.abs_value_us > pos->record->expiration_time.abs_value_us)
496 GNUNET_CONTAINER_DLL_remove (rc->records_head,
500 if (NULL == rc->records_head)
502 free_cache_entry (rc);
510 * Process DNS request for @a hostname with request ID @a request_id
511 * from @a client demanding records of type @a record_type.
513 * @param hostname DNS name to resolve
514 * @param record_type desired record type
515 * @param request_id client's request ID
516 * @param client who should get the result?
519 process_get (const char *hostname,
520 uint16_t record_type,
522 struct GNUNET_SERVICE_Client *client);
526 * Get an IP address as a string (works for both IPv4 and IPv6). Note
527 * that the resolution happens asynchronously and that the first call
528 * may not immediately result in the FQN (but instead in a
529 * human-readable IP address).
531 * @param hostname what hostname was to be resolved
532 * @param record_type what type of record was requested
533 * @param request_id unique identification of the client's request
534 * @param client handle to the client making the request (for sending the reply)
537 try_cache (const char *hostname,
538 uint16_t record_type,
540 struct GNUNET_SERVICE_Client *client)
542 struct ResolveCache *pos;
543 struct ResolveCache *next;
547 for (pos = next; NULL != pos; pos = next)
550 if (GNUNET_YES == remove_expired (pos))
552 if (0 == strcmp (pos->hostname,
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 "No cache entry for '%s'\n",
563 if (cache_head != pos)
565 /* move result to head to achieve LRU for cache eviction */
566 GNUNET_CONTAINER_DLL_remove (cache_head,
569 GNUNET_CONTAINER_DLL_insert (cache_head,
574 for (struct RecordListEntry *rle = pos->records_head;
578 const struct GNUNET_DNSPARSER_Record *record = rle->record;
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581 "Found cache entry for '%s', record type '%u'\n",
584 if ( (GNUNET_DNSPARSER_TYPE_CNAME == record->type) &&
585 (GNUNET_DNSPARSER_TYPE_CNAME != record_type) &&
586 (GNUNET_NO == found) )
588 const char *hostname = record->data.hostname;
590 process_get (hostname,
594 return GNUNET_YES; /* counts as a cache "hit" */
596 found |= send_reply (rle->record,
601 if (GNUNET_NO == found)
602 return GNUNET_NO; /* had records, but none matched! */
603 send_end_msg (request_id,
610 * We got a result from DNS. Add it to the cache and
611 * see if we can make our client happy...
613 * @param cls the `struct ActiveLookup`
614 * @param dns the DNS response
615 * @param dns_len number of bytes in @a dns
618 handle_resolve_result (void *cls,
619 const struct GNUNET_TUN_DnsHeader *dns,
622 struct ActiveLookup *al = cls;
623 struct GNUNET_DNSPARSER_Packet *parsed;
624 struct ResolveCache *rc;
626 parsed = GNUNET_DNSPARSER_parse ((const char *)dns,
630 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
631 "Failed to parse DNS reply (hostname %s, request ID %u)\n",
636 if (al->dns_id != ntohs (parsed->id))
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639 "Request ID in DNS reply does not match\n");
640 GNUNET_DNSPARSER_free_packet (parsed);
643 if (0 == parsed->num_answers)
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "DNS reply (hostname %s, request ID %u) contains no answers\n",
649 GNUNET_DNSPARSER_free_packet (parsed);
650 send_end_msg (al->request_id,
652 free_active_lookup (al);
655 /* LRU-based cache eviction: we remove from tail */
656 while (cache_size > MAX_CACHE)
657 free_cache_entry (cache_tail);
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 "Got reply for hostname %s and request ID %u\n",
664 for (unsigned int i = 0; i != parsed->num_answers; i++)
666 struct GNUNET_DNSPARSER_Record *record = &parsed->answers[i];
667 struct RecordListEntry *rle;
669 for (rc = cache_head; NULL != rc; rc = rc->next)
670 if (0 == strcasecmp (rc->hostname,
675 rc = GNUNET_new (struct ResolveCache);
676 rc->hostname = GNUNET_strdup (record->name);
677 GNUNET_CONTAINER_DLL_insert (cache_head,
682 /* TODO: ought to check first if we have this exact record
683 already in the cache! */
684 rle = GNUNET_new (struct RecordListEntry);
685 rle->record = GNUNET_DNSPARSER_duplicate_record (record);
686 GNUNET_CONTAINER_DLL_insert (rc->records_head,
691 /* resume by trying again from cache */
693 try_cache (al->hostname,
697 /* cache failed, tell client we could not get an answer */
698 send_end_msg (al->request_id,
700 free_active_lookup (al);
701 GNUNET_DNSPARSER_free_packet (parsed);
706 * We encountered a timeout trying to perform a
709 * @param cls a `struct ActiveLookup`
712 handle_resolve_timeout (void *cls)
714 struct ActiveLookup *al = cls;
716 al->timeout_task = NULL;
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "DNS lookup timeout!\n");
719 send_end_msg (al->request_id,
721 free_active_lookup (al);
726 * Initiate an active lookup, then cache the result and
727 * try to then complete the resolution.
729 * @param hostname DNS name to resolve
730 * @param record_type record type to locate
731 * @param request_id client request ID
732 * @param client handle to the client
735 resolve_and_cache (const char* hostname,
736 uint16_t record_type,
738 struct GNUNET_SERVICE_Client *client)
742 struct GNUNET_DNSPARSER_Query query;
743 struct GNUNET_DNSPARSER_Packet packet;
744 struct ActiveLookup *al;
747 dns_id =(uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
750 "resolve_and_cache\n");
751 query.name = (char *)hostname;
752 query.type = record_type;
753 query.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
757 packet.num_queries = 1;
758 packet.queries = &query;
759 packet.id = htons (dns_id);
760 packet.flags.recursion_desired = 1;
762 GNUNET_DNSPARSER_pack (&packet,
767 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
768 "Failed to pack query for hostname `%s'\n",
770 return GNUNET_SYSERR;
773 al = GNUNET_new (struct ActiveLookup);
774 al->hostname = GNUNET_strdup (hostname);
775 al->record_type = record_type;
776 al->request_id = request_id;
779 al->timeout_task = GNUNET_SCHEDULER_add_delayed (DNS_TIMEOUT,
780 &handle_resolve_timeout,
783 GNUNET_DNSSTUB_resolve (dnsstub_ctx,
786 &handle_resolve_result,
788 GNUNET_free (packet_buf);
789 GNUNET_CONTAINER_DLL_insert (lookup_head,
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793 "Resolving %s, request_id = %u, dns_id = %u\n",
795 (unsigned int) request_id,
796 (unsigned int) dns_id);
802 * Process DNS request for @a hostname with request ID @a request_id
803 * from @a client demanding records of type @a record_type.
805 * @param hostname DNS name to resolve
806 * @param record_type desired record type
807 * @param request_id client's request ID
808 * @param client who should get the result?
811 process_get (const char *hostname,
812 uint16_t record_type,
814 struct GNUNET_SERVICE_Client *client)
823 resolve_and_cache (hostname,
828 send_end_msg (request_id,
836 * Verify well-formedness of GET-message.
838 * @param cls closure, unused
839 * @param get the actual message
840 * @return #GNUNET_OK if @a get is well-formed
843 check_get (void *cls,
844 const struct GNUNET_RESOLVER_GetMessage *get)
851 size = ntohs (get->header.size) - sizeof (*get);
852 direction = ntohl (get->direction);
853 if (GNUNET_NO == direction)
855 /* IP from hostname */
856 const char *hostname;
858 hostname = (const char *) &get[1];
859 if (hostname[size - 1] != '\0')
862 return GNUNET_SYSERR;
866 af = ntohl (get->af);
870 if (size != sizeof (struct in_addr))
873 return GNUNET_SYSERR;
877 if (size != sizeof (struct in6_addr))
880 return GNUNET_SYSERR;
885 return GNUNET_SYSERR;
892 * Handle GET-message.
894 * @param cls identification of the client
895 * @param msg the actual message
898 handle_get (void *cls,
899 const struct GNUNET_RESOLVER_GetMessage *msg)
901 struct GNUNET_SERVICE_Client *client = cls;
907 direction = ntohl (msg->direction);
908 af = ntohl (msg->af);
909 request_id = ntohs (msg->id);
910 if (GNUNET_NO == direction)
912 /* IP from hostname */
913 hostname = GNUNET_strdup ((const char *) &msg[1]);
918 process_get (hostname,
919 GNUNET_DNSPARSER_TYPE_ALL,
926 process_get (hostname,
927 GNUNET_DNSPARSER_TYPE_A,
934 process_get (hostname,
935 GNUNET_DNSPARSER_TYPE_AAAA,
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 "got invalid af: %d\n",
951 /* hostname from IP */
952 hostname = make_reverse_hostname (&msg[1],
954 process_get (hostname,
955 GNUNET_DNSPARSER_TYPE_PTR,
959 GNUNET_free_non_null (hostname);
960 GNUNET_SERVICE_client_continue (client);
965 * Service is shutting down, clean up.
967 * @param cls NULL, unused
970 shutdown_task (void *cls)
974 while (NULL != lookup_head)
975 free_active_lookup (lookup_head);
976 while (NULL != cache_head)
977 free_cache_entry (cache_head);
978 GNUNET_DNSSTUB_stop (dnsstub_ctx);
983 * Service is starting, initialize everything.
985 * @param cls NULL, unused
986 * @param cfg our configuration
987 * @param sh service handle
991 const struct GNUNET_CONFIGURATION_Handle *cfg,
992 struct GNUNET_SERVICE_Handle *sh)
999 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1001 dnsstub_ctx = GNUNET_DNSSTUB_start (128);
1002 num_dns_servers = lookup_dns_servers (&dns_servers);
1003 if (0 >= num_dns_servers)
1005 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1006 _("No DNS server available. DNS resolution will not be possible.\n"));
1008 for (int i = 0; i < num_dns_servers; i++)
1010 int result = GNUNET_DNSSTUB_add_dns_ip (dnsstub_ctx, dns_servers[i]);
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 "Adding DNS server '%s': %s\n",
1014 GNUNET_OK == result ? "success" : "failure");
1015 GNUNET_free (dns_servers[i]);
1017 GNUNET_free_non_null (dns_servers);
1022 * Callback called when a client connects to the service.
1024 * @param cls closure for the service, unused
1025 * @param c the new client that connected to the service
1026 * @param mq the message queue used to send messages to the client
1030 connect_cb (void *cls,
1031 struct GNUNET_SERVICE_Client *c,
1032 struct GNUNET_MQ_Handle *mq)
1042 * Callback called when a client disconnected from the service
1044 * @param cls closure for the service
1045 * @param c the client that disconnected
1046 * @param internal_cls should be equal to @a c
1049 disconnect_cb (void *cls,
1050 struct GNUNET_SERVICE_Client *c,
1053 struct ActiveLookup *n;
1056 GNUNET_assert (c == internal_cls);
1058 for (struct ActiveLookup *al = n;
1063 if (al->client == c)
1064 free_active_lookup (al);
1070 * Define "main" method using service macro.
1074 GNUNET_SERVICE_OPTION_NONE,
1079 GNUNET_MQ_hd_var_size (get,
1080 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
1081 struct GNUNET_RESOLVER_GetMessage,
1083 GNUNET_MQ_handler_end ());
1086 #if defined(LINUX) && defined(__GLIBC__)
1090 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1092 void __attribute__ ((constructor))
1093 GNUNET_RESOLVER_memory_init ()
1095 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1096 mallopt (M_TOP_PAD, 1 * 1024);
1102 /* end of gnunet-service-resolver.c */