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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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"
34 * How long do we wait for DNS answers?
36 #define DNS_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
39 * Maximum number of hostnames we cache results for.
41 #define MAX_CACHE 1024
44 * Entry in list of cached DNS records for a hostname.
46 struct RecordListEntry {
48 * This is a doubly linked list.
50 struct RecordListEntry *next;
53 * This is a doubly linked list.
55 struct RecordListEntry *prev;
60 struct GNUNET_DNSPARSER_Record *record;
65 * 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;
96 * Information about pending lookups.
102 struct ActiveLookup *next;
107 struct ActiveLookup *prev;
110 * The client that queried the records contained in this cache entry.
112 struct GNUNET_SERVICE_Client *client;
115 * handle for cancelling a request
117 struct GNUNET_DNSSTUB_RequestSocket *resolve_handle;
120 * handle for the resolution timeout task
122 struct GNUNET_SCHEDULER_Task *timeout_task;
125 * Which hostname are we resolving?
130 * If @a record_type is #GNUNET_DNSPARSER_TYPE_ALL, did we go again
131 * for the AAAA records yet?
136 * type of queried DNS record
138 uint16_t record_type;
141 * Unique request ID of a client if a query for this hostname/record_type
142 * is currently pending, undefined otherwise.
144 uint32_t client_request_id;
147 * Unique DNS request ID of a client if a query for this hostname/record_type
148 * is currently pending, undefined otherwise.
155 * Start of the linked list of cached DNS lookup results.
157 static struct ResolveCache *cache_head;
160 * Tail of the linked list of cached DNS lookup results.
162 static struct ResolveCache *cache_tail;
165 * Head of the linked list of DNS lookup results from /etc/hosts.
167 static struct ResolveCache *hosts_head;
170 * Tail of the linked list of DNS lookup results from /etc/hosts.
172 static struct ResolveCache *hosts_tail;
175 * Start of the linked list of active DNS lookups.
177 static struct ActiveLookup *lookup_head;
180 * Tail of the linked list of active DNS lookups.
182 static struct ActiveLookup *lookup_tail;
185 * context of dnsstub library
187 static struct GNUNET_DNSSTUB_Context *dnsstub_ctx;
190 * My domain, to be appended to the hostname to get a FQDN.
192 static char *my_domain;
195 * How many entries do we have in #cache_head DLL?
197 static unsigned int cache_size;
201 * Remove @a entry from cache.
203 * @param rc entry to free
206 free_cache_entry(struct ResolveCache *rc)
208 struct RecordListEntry *pos;
210 while (NULL != (pos = rc->records_head))
212 GNUNET_CONTAINER_DLL_remove(rc->records_head, rc->records_tail, pos);
213 GNUNET_DNSPARSER_free_record(pos->record);
214 GNUNET_free(pos->record);
217 GNUNET_free_non_null(rc->hostname);
218 GNUNET_CONTAINER_DLL_remove(cache_head, cache_tail, rc);
225 * Remove @a entry from cache.
227 * @param rc entry to free
230 free_hosts_entry(struct ResolveCache *rc)
232 struct RecordListEntry *pos;
234 while (NULL != (pos = rc->records_head))
236 GNUNET_CONTAINER_DLL_remove(rc->records_head, rc->records_tail, pos);
237 GNUNET_DNSPARSER_free_record(pos->record);
238 GNUNET_free(pos->record);
241 GNUNET_free_non_null(rc->hostname);
242 GNUNET_CONTAINER_DLL_remove(hosts_head, hosts_tail, rc);
249 * Release resources associated with @a al
251 * @param al an active lookup
254 free_active_lookup(struct ActiveLookup *al)
256 GNUNET_CONTAINER_DLL_remove(lookup_head, lookup_tail, al);
257 if (NULL != al->resolve_handle)
259 GNUNET_DNSSTUB_resolve_cancel(al->resolve_handle);
260 al->resolve_handle = NULL;
262 if (NULL != al->timeout_task)
264 GNUNET_SCHEDULER_cancel(al->timeout_task);
265 al->timeout_task = NULL;
267 GNUNET_free_non_null(al->hostname);
273 * Find out if the configuration file line contains a string
274 * starting with "nameserver ", and if so, return a copy of
275 * the nameserver's IP.
277 * @param line line to parse
278 * @param line_len number of characters in @a line
279 * @return NULL if no nameserver is configured in this @a line
282 extract_dns_server(const char *line, size_t line_len)
284 if (0 == strncmp(line, "nameserver ", strlen("nameserver ")))
285 return GNUNET_strndup(line + strlen("nameserver "),
286 line_len - strlen("nameserver "));
292 * Find out if the configuration file line contains a string
293 * starting with "search ", and if so, return a copy of
294 * the machine's search domain.
296 * @param line line to parse
297 * @param line_len number of characters in @a line
298 * @return NULL if no nameserver is configured in this @a line
301 extract_search_domain(const char *line, size_t line_len)
303 if (0 == strncmp(line, "search ", strlen("search ")))
304 return GNUNET_strndup(line + strlen("search "),
305 line_len - strlen("search "));
311 * Reads the list of nameservers from /etc/resolve.conf
313 * @param server_addrs[out] a list of null-terminated server address strings
314 * @return the number of server addresses in @server_addrs, -1 on error
317 lookup_dns_servers(char ***server_addrs)
319 struct GNUNET_DISK_FileHandle *fh;
320 struct GNUNET_DISK_MapHandle *mh;
324 unsigned int num_dns_servers;
326 fh = GNUNET_DISK_file_open("/etc/resolv.conf",
327 GNUNET_DISK_OPEN_READ,
328 GNUNET_DISK_PERM_NONE);
331 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
332 "Could not open /etc/resolv.conf. "
333 "DNS resolution will not be possible.\n");
336 if (GNUNET_OK != GNUNET_DISK_file_handle_size(fh, &bytes_read))
338 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
339 "Could not determine size of /etc/resolv.conf. "
340 "DNS resolution will not be possible.\n");
341 GNUNET_DISK_file_close(fh);
344 if (((unsigned long long)bytes_read) > (unsigned long long)SIZE_MAX)
346 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
347 "/etc/resolv.conf file too large to mmap. "
348 "DNS resolution will not be possible.\n");
349 GNUNET_DISK_file_close(fh);
352 buf = GNUNET_DISK_file_map(fh,
354 GNUNET_DISK_MAP_TYPE_READ,
356 *server_addrs = NULL;
359 while (read_offset < (size_t)bytes_read)
365 newline = strchr(buf + read_offset, '\n');
368 line_len = newline - buf - read_offset;
369 dns_server = extract_dns_server(buf + read_offset, line_len);
370 if (NULL != dns_server)
372 GNUNET_array_append(*server_addrs, num_dns_servers, dns_server);
374 else if (NULL == my_domain)
376 my_domain = extract_search_domain(buf + read_offset, line_len);
378 read_offset += line_len + 1;
380 GNUNET_DISK_file_unmap(mh);
381 GNUNET_DISK_file_close(fh);
382 return (int)num_dns_servers;
387 * Compute name to use for DNS reverse lookups from @a ip.
389 * @param ip IP address to resolve, in binary format, network byte order
390 * @param af address family of @a ip, AF_INET or AF_INET6
393 make_reverse_hostname(const void *ip, int af)
395 char *buf = GNUNET_new_array(80, char);
400 struct in_addr *addr = (struct in_addr *)ip;
401 uint32_t ip_int = addr->s_addr;
403 for (int i = 3; i >= 0; i--)
406 GNUNET_snprintf(buf + pos, 80 - pos, "%u.", ((uint8_t *)&ip_int)[i]);
414 pos += GNUNET_snprintf(buf + pos, 80 - pos, "in-addr.arpa");
416 else if (AF_INET6 == af)
418 struct in6_addr *addr = (struct in6_addr *)ip;
419 for (int i = 15; i >= 0; i--)
422 GNUNET_snprintf(buf + pos, 80 - pos, "%x.", addr->s6_addr[i] & 0xf);
429 n = GNUNET_snprintf(buf + pos, 80 - pos, "%x.", addr->s6_addr[i] >> 4);
437 pos += GNUNET_snprintf(buf + pos, 80 - pos, "ip6.arpa");
445 * Send DNS @a record back to our @a client.
447 * @param record information to transmit
448 * @param record_type requested record type from client
449 * @param client_request_id to which request are we responding
450 * @param client where to send @a record
451 * @return #GNUNET_YES if we sent a reply,
452 * #GNUNET_NO if the record type is not understood or
453 * does not match @a record_type
456 send_reply(struct GNUNET_DNSPARSER_Record *record,
457 uint16_t record_type,
458 uint32_t client_request_id,
459 struct GNUNET_SERVICE_Client *client)
461 struct GNUNET_RESOLVER_ResponseMessage *msg;
462 struct GNUNET_MQ_Envelope *env;
466 switch (record->type)
468 case GNUNET_DNSPARSER_TYPE_CNAME:
469 if (GNUNET_DNSPARSER_TYPE_CNAME != record_type)
471 payload = record->data.hostname;
472 payload_len = strlen(record->data.hostname) + 1;
475 case GNUNET_DNSPARSER_TYPE_PTR:
476 if (GNUNET_DNSPARSER_TYPE_PTR != record_type)
478 payload = record->data.hostname;
479 payload_len = strlen(record->data.hostname) + 1;
482 case GNUNET_DNSPARSER_TYPE_A:
483 if ((GNUNET_DNSPARSER_TYPE_A != record_type) &&
484 (GNUNET_DNSPARSER_TYPE_ALL != record_type))
486 payload = record->data.raw.data;
487 payload_len = record->data.raw.data_len;
490 case GNUNET_DNSPARSER_TYPE_AAAA:
491 if ((GNUNET_DNSPARSER_TYPE_AAAA != record_type) &&
492 (GNUNET_DNSPARSER_TYPE_ALL != record_type))
494 payload = record->data.raw.data;
495 payload_len = record->data.raw.data_len;
499 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
500 "Cannot handle DNS response type %u: not supported here\n",
504 env = GNUNET_MQ_msg_extra(msg,
506 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
507 msg->client_id = client_request_id;
508 GNUNET_memcpy(&msg[1], payload, payload_len);
509 GNUNET_MQ_send(GNUNET_SERVICE_client_get_mq(client), env);
515 * Send message to @a client that we transmitted all
516 * responses for @a client_request_id
518 * @param client_request_id to which request are we responding
519 * @param client where to send @a record
522 send_end_msg(uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
524 struct GNUNET_RESOLVER_ResponseMessage *msg;
525 struct GNUNET_MQ_Envelope *env;
527 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending END message\n");
528 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
529 msg->client_id = client_request_id;
530 GNUNET_MQ_send(GNUNET_SERVICE_client_get_mq(client), env);
535 * Remove expired entries from @a rc
537 * @param rc entry in resolver cache
538 * @return #GNUNET_YES if @a rc was completely expired
539 * #GNUNET_NO if some entries are left
542 remove_expired(struct ResolveCache *rc)
544 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
545 struct RecordListEntry *n;
547 for (struct RecordListEntry *pos = rc->records_head; NULL != pos; pos = n)
550 if (now.abs_value_us > pos->record->expiration_time.abs_value_us)
552 GNUNET_CONTAINER_DLL_remove(rc->records_head, rc->records_tail, pos);
553 GNUNET_DNSPARSER_free_record(pos->record);
554 GNUNET_free(pos->record);
558 if (NULL == rc->records_head)
560 free_cache_entry(rc);
568 * Process DNS request for @a hostname with request ID @a request_id
569 * from @a client demanding records of type @a record_type.
571 * @param hostname DNS name to resolve
572 * @param record_type desired record type
573 * @param client_request_id client's request ID
574 * @param client who should get the result?
577 process_get(const char *hostname,
578 uint16_t record_type,
579 uint32_t client_request_id,
580 struct GNUNET_SERVICE_Client *client);
584 * Get an IP address as a string (works for both IPv4 and IPv6). Note
585 * that the resolution happens asynchronously and that the first call
586 * may not immediately result in the FQN (but instead in a
587 * human-readable IP address).
589 * @param hostname what hostname was to be resolved
590 * @param record_type what type of record was requested
591 * @param client_request_id unique identification of the client's request
592 * @param client handle to the client making the request (for sending the reply)
595 try_cache(const char *hostname,
596 uint16_t record_type,
597 uint32_t client_request_id,
598 struct GNUNET_SERVICE_Client *client)
600 struct ResolveCache *pos;
601 struct ResolveCache *next;
605 in_hosts = GNUNET_NO;
606 for (pos = hosts_head; NULL != pos; pos = pos->next)
607 if (0 == strcmp(pos->hostname, hostname))
609 in_hosts = GNUNET_YES;
615 for (pos = next; NULL != pos; pos = next)
618 if (GNUNET_YES == remove_expired(pos))
620 if (0 == strcmp(pos->hostname, hostname))
626 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No cache entry for '%s'\n", hostname);
629 if ((GNUNET_NO == in_hosts) && (cache_head != pos))
631 /* move result to head to achieve LRU for cache eviction */
632 GNUNET_CONTAINER_DLL_remove(cache_head, cache_tail, pos);
633 GNUNET_CONTAINER_DLL_insert(cache_head, cache_tail, pos);
636 for (struct RecordListEntry *rle = pos->records_head; NULL != rle;
639 const struct GNUNET_DNSPARSER_Record *record = rle->record;
641 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
642 "Found cache entry for '%s', record type '%u'\n",
645 if ((GNUNET_DNSPARSER_TYPE_CNAME == record->type) &&
646 (GNUNET_DNSPARSER_TYPE_CNAME != record_type) && (GNUNET_NO == found))
648 const char *hostname = record->data.hostname;
650 process_get(hostname, record_type, client_request_id, client);
651 return GNUNET_YES; /* counts as a cache "hit" */
653 found |= send_reply(rle->record, record_type, client_request_id, client);
655 if (GNUNET_NO == found)
656 return GNUNET_NO; /* had records, but none matched! */
657 send_end_msg(client_request_id, client);
663 * Create DNS query for @a hostname of type @a type
664 * with DNS request ID @a dns_id.
666 * @param hostname DNS name to query
667 * @param type requested DNS record type
668 * @param dns_id what should be the DNS request ID
669 * @param packet_buf[out] where to write the request packet
670 * @param packet_size[out] set to size of @a packet_buf on success
671 * @return #GNUNET_OK on success
674 pack(const char *hostname,
680 struct GNUNET_DNSPARSER_Query query;
681 struct GNUNET_DNSPARSER_Packet packet;
683 query.name = (char *)hostname;
685 query.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
686 memset(&packet, 0, sizeof(packet));
687 packet.num_queries = 1;
688 packet.queries = &query;
689 packet.id = htons(dns_id);
690 packet.flags.recursion_desired = 1;
692 GNUNET_DNSPARSER_pack(&packet, UINT16_MAX, packet_buf, packet_size))
694 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
695 "Failed to pack query for hostname `%s'\n",
698 return GNUNET_SYSERR;
704 cache_answers(const char *name,
705 struct GNUNET_DNSPARSER_Record *records,
706 unsigned int num_records)
708 struct ResolveCache *rc;
709 struct GNUNET_DNSPARSER_Record *record;
710 struct RecordListEntry *rle;
712 for (unsigned int i = 0; i != num_records; i++)
714 record = &records[i];
716 for (rc = cache_head; NULL != rc; rc = rc->next)
717 if (0 == strcasecmp(rc->hostname, name))
721 rc = GNUNET_new(struct ResolveCache);
722 rc->hostname = GNUNET_strdup(name);
723 GNUNET_CONTAINER_DLL_insert(cache_head, cache_tail, rc);
726 /* TODO: ought to check first if we have this exact record
727 already in the cache! */
728 rle = GNUNET_new(struct RecordListEntry);
729 rle->record = GNUNET_DNSPARSER_duplicate_record(record);
730 GNUNET_CONTAINER_DLL_insert(rc->records_head, rc->records_tail, rle);
735 * We got a result from DNS. Add it to the cache and
736 * see if we can make our client happy...
738 * @param cls the `struct ActiveLookup`
739 * @param dns the DNS response
740 * @param dns_len number of bytes in @a dns
743 handle_resolve_result(void *cls,
744 const struct GNUNET_TUN_DnsHeader *dns,
747 struct ActiveLookup *al = cls;
748 struct GNUNET_DNSPARSER_Packet *parsed;
750 parsed = GNUNET_DNSPARSER_parse((const char *)dns, dns_len);
753 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
754 "Failed to parse DNS reply (hostname %s, request ID %u)\n",
759 if (al->dns_id != ntohs(parsed->id))
761 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
762 "Request ID in DNS reply does not match\n");
763 GNUNET_DNSPARSER_free_packet(parsed);
766 if (0 == parsed->num_answers + parsed->num_authority_records +
767 parsed->num_additional_records)
769 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
770 "DNS reply (hostname %s, request ID %u) contains no answers\n",
772 (unsigned int)al->client_request_id);
773 /* resume by trying again from cache */
774 if (GNUNET_NO == try_cache(al->hostname,
776 al->client_request_id,
778 /* cache failed, tell client we could not get an answer */
780 send_end_msg(al->client_request_id, al->client);
782 GNUNET_DNSPARSER_free_packet(parsed);
783 free_active_lookup(al);
786 /* LRU-based cache eviction: we remove from tail */
787 while (cache_size > MAX_CACHE)
788 free_cache_entry(cache_tail);
790 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
791 "Got reply for hostname %s and request ID %u\n",
793 (unsigned int)al->client_request_id);
795 cache_answers(al->hostname, parsed->answers, parsed->num_answers);
796 cache_answers(al->hostname,
797 parsed->authority_records,
798 parsed->num_authority_records);
799 cache_answers(al->hostname,
800 parsed->additional_records,
801 parsed->num_additional_records);
803 /* see if we need to do the 2nd request for AAAA records */
804 if ((GNUNET_DNSPARSER_TYPE_ALL == al->record_type) &&
805 (GNUNET_NO == al->did_aaaa))
811 dns_id = (uint16_t)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE,
813 if (GNUNET_OK == pack(al->hostname,
814 GNUNET_DNSPARSER_TYPE_AAAA,
819 al->did_aaaa = GNUNET_YES;
821 GNUNET_DNSSTUB_resolve_cancel(al->resolve_handle);
822 al->resolve_handle = GNUNET_DNSSTUB_resolve(dnsstub_ctx,
825 &handle_resolve_result,
827 GNUNET_free(packet_buf);
828 GNUNET_DNSPARSER_free_packet(parsed);
833 /* resume by trying again from cache */
834 if (GNUNET_NO == try_cache(al->hostname,
836 al->client_request_id,
838 /* cache failed, tell client we could not get an answer */
840 send_end_msg(al->client_request_id, al->client);
842 free_active_lookup(al);
843 GNUNET_DNSPARSER_free_packet(parsed);
848 * We encountered a timeout trying to perform a
851 * @param cls a `struct ActiveLookup`
854 handle_resolve_timeout(void *cls)
856 struct ActiveLookup *al = cls;
858 al->timeout_task = NULL;
859 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DNS lookup timeout!\n");
860 send_end_msg(al->client_request_id, al->client);
861 free_active_lookup(al);
866 * Initiate an active lookup, then cache the result and
867 * try to then complete the resolution.
869 * @param hostname DNS name to resolve
870 * @param record_type record type to locate
871 * @param client_request_id client request ID
872 * @param client handle to the client
873 * @return #GNUNET_OK if the DNS query is now pending
876 resolve_and_cache(const char *hostname,
877 uint16_t record_type,
878 uint32_t client_request_id,
879 struct GNUNET_SERVICE_Client *client)
883 struct ActiveLookup *al;
887 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "resolve_and_cache `%s'\n", hostname);
888 dns_id = (uint16_t)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE,
891 if (GNUNET_DNSPARSER_TYPE_ALL == record_type)
892 type = GNUNET_DNSPARSER_TYPE_A;
895 if (GNUNET_OK != pack(hostname, type, dns_id, &packet_buf, &packet_size))
897 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
898 "Failed to pack query for hostname `%s'\n",
900 return GNUNET_SYSERR;
903 al = GNUNET_new(struct ActiveLookup);
904 al->hostname = GNUNET_strdup(hostname);
905 al->record_type = record_type;
906 al->client_request_id = client_request_id;
910 GNUNET_SCHEDULER_add_delayed(DNS_TIMEOUT, &handle_resolve_timeout, al);
911 al->resolve_handle = GNUNET_DNSSTUB_resolve(dnsstub_ctx,
914 &handle_resolve_result,
916 GNUNET_free(packet_buf);
917 GNUNET_CONTAINER_DLL_insert(lookup_head, lookup_tail, al);
918 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
919 "Resolving %s, client_request_id = %u, dns_id = %u\n",
921 (unsigned int)client_request_id,
922 (unsigned int)dns_id);
928 * Process DNS request for @a hostname with request ID @a client_request_id
929 * from @a client demanding records of type @a record_type.
931 * @param hostname DNS name to resolve
932 * @param record_type desired record type
933 * @param client_request_id client's request ID
934 * @param client who should get the result?
937 process_get(const char *hostname,
938 uint16_t record_type,
939 uint32_t client_request_id,
940 struct GNUNET_SERVICE_Client *client)
944 if (GNUNET_NO != try_cache(hostname, record_type, client_request_id, client))
946 if ((NULL != my_domain) && (NULL == strchr(hostname, (unsigned char)'.')) &&
947 (strlen(hostname) + strlen(my_domain) <= 253))
949 GNUNET_snprintf(fqdn, sizeof(fqdn), "%s.%s", hostname, my_domain);
951 else if (strlen(hostname) < 255)
953 GNUNET_snprintf(fqdn, sizeof(fqdn), "%s", hostname);
958 GNUNET_SERVICE_client_drop(client);
961 if (GNUNET_NO == try_cache(fqdn, record_type, client_request_id, client))
964 resolve_and_cache(fqdn, record_type, client_request_id, client))
966 send_end_msg(client_request_id, client);
973 * Verify well-formedness of GET-message.
975 * @param cls closure, unused
976 * @param get the actual message
977 * @return #GNUNET_OK if @a get is well-formed
980 check_get(void *cls, const struct GNUNET_RESOLVER_GetMessage *get)
987 size = ntohs(get->header.size) - sizeof(*get);
988 direction = ntohl(get->direction);
989 if (GNUNET_NO == direction)
991 GNUNET_MQ_check_zero_termination(get);
998 if (size != sizeof(struct in_addr))
1001 return GNUNET_SYSERR;
1006 if (size != sizeof(struct in6_addr))
1009 return GNUNET_SYSERR;
1015 return GNUNET_SYSERR;
1022 * Handle GET-message.
1024 * @param cls identification of the client
1025 * @param msg the actual message
1028 handle_get(void *cls, const struct GNUNET_RESOLVER_GetMessage *msg)
1030 struct GNUNET_SERVICE_Client *client = cls;
1033 uint32_t client_request_id;
1036 direction = ntohl(msg->direction);
1037 af = ntohl(msg->af);
1038 client_request_id = msg->client_id;
1039 GNUNET_SERVICE_client_continue(client);
1040 if (GNUNET_NO == direction)
1042 /* IP from hostname */
1043 hostname = GNUNET_strdup((const char *)&msg[1]);
1044 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1045 "Client asks to resolve `%s'\n",
1050 process_get(hostname,
1051 GNUNET_DNSPARSER_TYPE_ALL,
1058 process_get(hostname,
1059 GNUNET_DNSPARSER_TYPE_A,
1066 process_get(hostname,
1067 GNUNET_DNSPARSER_TYPE_AAAA,
1074 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got invalid af: %d\n", af);
1081 /* hostname from IP */
1082 hostname = make_reverse_hostname(&msg[1], af);
1083 process_get(hostname,
1084 GNUNET_DNSPARSER_TYPE_PTR,
1088 GNUNET_free_non_null(hostname);
1093 * Service is shutting down, clean up.
1095 * @param cls NULL, unused
1098 shutdown_task(void *cls)
1102 while (NULL != lookup_head)
1103 free_active_lookup(lookup_head);
1104 while (NULL != cache_head)
1105 free_cache_entry(cache_head);
1106 while (NULL != hosts_head)
1107 free_hosts_entry(hosts_head);
1108 GNUNET_DNSSTUB_stop(dnsstub_ctx);
1109 GNUNET_free_non_null(my_domain);
1114 * Add information about a host from /etc/hosts
1117 * @param hostname the name of the host
1118 * @param rec_type DNS record type to use
1119 * @param data payload
1120 * @param data_size number of bytes in @a data
1123 add_host(const char *hostname,
1128 struct ResolveCache *rc;
1129 struct RecordListEntry *rle;
1130 struct GNUNET_DNSPARSER_Record *rec;
1132 rec = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record));
1133 rec->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS;
1134 rec->type = rec_type;
1135 rec->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1136 rec->name = GNUNET_strdup(hostname);
1137 rec->data.raw.data = GNUNET_memdup(data, data_size);
1138 rec->data.raw.data_len = data_size;
1139 rle = GNUNET_new(struct RecordListEntry);
1141 rc = GNUNET_new(struct ResolveCache);
1142 rc->hostname = GNUNET_strdup(hostname);
1143 GNUNET_CONTAINER_DLL_insert(rc->records_head, rc->records_tail, rle);
1144 GNUNET_CONTAINER_DLL_insert(hosts_head, hosts_tail, rc);
1149 * Extract host information from a line in /etc/hosts
1151 * @param line the line to parse
1152 * @param line_len number of bytes in @a line
1155 extract_hosts(const char *line, size_t line_len)
1163 /* ignore everything after '#' */
1164 c = memrchr(line, (unsigned char)'#', line_len);
1166 line_len = c - line;
1167 /* ignore leading whitespace */
1168 while ((0 < line_len) && isspace((unsigned char)*line))
1173 tbuf = GNUNET_strndup(line, line_len);
1174 tok = strtok(tbuf, " \t");
1180 if (1 == inet_pton(AF_INET, tok, &v4))
1182 while (NULL != (tok = strtok(NULL, " \t")))
1183 add_host(tok, GNUNET_DNSPARSER_TYPE_A, &v4, sizeof(struct in_addr));
1185 else if (1 == inet_pton(AF_INET6, tok, &v6))
1187 while (NULL != (tok = strtok(NULL, " \t")))
1188 add_host(tok, GNUNET_DNSPARSER_TYPE_AAAA, &v6, sizeof(struct in6_addr));
1195 * Reads the list of hosts from /etc/hosts.
1198 load_etc_hosts(void)
1200 struct GNUNET_DISK_FileHandle *fh;
1201 struct GNUNET_DISK_MapHandle *mh;
1206 fh = GNUNET_DISK_file_open("/etc/hosts",
1207 GNUNET_DISK_OPEN_READ,
1208 GNUNET_DISK_PERM_NONE);
1211 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Failed to open /etc/hosts");
1214 if (GNUNET_OK != GNUNET_DISK_file_handle_size(fh, &bytes_read))
1216 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1217 "Could not determin size of /etc/hosts. "
1218 "DNS resolution will not be possible.\n");
1219 GNUNET_DISK_file_close(fh);
1222 if (((unsigned long long)bytes_read) > (unsigned long long)SIZE_MAX)
1224 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1225 "/etc/hosts file too large to mmap. "
1226 "DNS resolution will not be possible.\n");
1227 GNUNET_DISK_file_close(fh);
1230 buf = GNUNET_DISK_file_map(fh,
1232 GNUNET_DISK_MAP_TYPE_READ,
1233 (size_t)bytes_read);
1235 while (read_offset < (size_t)bytes_read)
1237 const char *newline;
1240 newline = strchr(buf + read_offset, '\n');
1241 if (NULL == newline)
1243 line_len = newline - buf - read_offset;
1244 extract_hosts(buf + read_offset, line_len);
1245 read_offset += line_len + 1;
1247 GNUNET_DISK_file_unmap(mh);
1248 GNUNET_DISK_file_close(fh);
1253 * Service is starting, initialize everything.
1255 * @param cls NULL, unused
1256 * @param cfg our configuration
1257 * @param sh service handle
1261 const struct GNUNET_CONFIGURATION_Handle *cfg,
1262 struct GNUNET_SERVICE_Handle *sh)
1265 int num_dns_servers;
1270 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, cls);
1271 dnsstub_ctx = GNUNET_DNSSTUB_start(128);
1273 num_dns_servers = lookup_dns_servers(&dns_servers);
1274 if (0 >= num_dns_servers)
1277 GNUNET_ERROR_TYPE_ERROR,
1278 _("No DNS server available. DNS resolution will not be possible.\n"));
1281 for (int i = 0; i < num_dns_servers; i++)
1283 int result = GNUNET_DNSSTUB_add_dns_ip(dnsstub_ctx, dns_servers[i]);
1284 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1285 "Adding DNS server '%s': %s\n",
1287 GNUNET_OK == result ? "success" : "failure");
1288 GNUNET_free(dns_servers[i]);
1290 GNUNET_free_non_null(dns_servers);
1295 * Callback called when a client connects to the service.
1297 * @param cls closure for the service, unused
1298 * @param c the new client that connected to the service
1299 * @param mq the message queue used to send messages to the client
1303 connect_cb(void *cls,
1304 struct GNUNET_SERVICE_Client *c,
1305 struct GNUNET_MQ_Handle *mq)
1315 * Callback called when a client disconnected from the service
1317 * @param cls closure for the service
1318 * @param c the client that disconnected
1319 * @param internal_cls should be equal to @a c
1322 disconnect_cb(void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
1324 struct ActiveLookup *n;
1328 GNUNET_assert(c == internal_cls);
1330 for (struct ActiveLookup *al = n; NULL != al; al = n)
1333 if (al->client == c)
1334 free_active_lookup(al);
1340 * Define "main" method using service macro.
1342 GNUNET_SERVICE_MAIN(
1344 GNUNET_SERVICE_OPTION_NONE,
1349 GNUNET_MQ_hd_var_size(get,
1350 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
1351 struct GNUNET_RESOLVER_GetMessage,
1353 GNUNET_MQ_handler_end());
1356 #if defined(LINUX) && defined(__GLIBC__)
1360 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1362 void __attribute__ ((constructor)) GNUNET_RESOLVER_memory_init()
1364 mallopt(M_TRIM_THRESHOLD, 4 * 1024);
1365 mallopt(M_TOP_PAD, 1 * 1024);
1371 /* end of gnunet-service-resolver.c */