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
49 * This is a doubly linked list.
51 struct RecordListEntry *next;
54 * This is a doubly linked list.
56 struct RecordListEntry *prev;
61 struct GNUNET_DNSPARSER_Record *record;
67 * A cached DNS lookup result.
72 * This is a doubly linked list.
74 struct ResolveCache *next;
77 * This is a doubly linked list.
79 struct ResolveCache *prev;
82 * Which hostname is this cache for?
87 * head of a double linked list containing the lookup results
89 struct RecordListEntry *records_head;
92 * tail of a double linked list containing the lookup results
94 struct RecordListEntry *records_tail;
100 * Information about pending lookups.
107 struct ActiveLookup *next;
112 struct ActiveLookup *prev;
115 * The client that queried the records contained in this cache entry.
117 struct GNUNET_SERVICE_Client *client;
120 * handle for cancelling a request
122 struct GNUNET_DNSSTUB_RequestSocket *resolve_handle;
125 * handle for the resolution timeout task
127 struct GNUNET_SCHEDULER_Task *timeout_task;
130 * Which hostname are we resolving?
135 * If @a record_type is #GNUNET_DNSPARSER_TYPE_ALL, did we go again
136 * for the AAAA records yet?
141 * type of queried DNS record
143 uint16_t record_type;
146 * Unique request ID of a client if a query for this hostname/record_type
147 * is currently pending, undefined otherwise.
149 uint32_t client_request_id;
152 * Unique DNS request ID of a client if a query for this hostname/record_type
153 * is currently pending, undefined otherwise.
161 * Start of the linked list of cached DNS lookup results.
163 static struct ResolveCache *cache_head;
166 * Tail of the linked list of cached DNS lookup results.
168 static struct ResolveCache *cache_tail;
171 * Head of the linked list of DNS lookup results from /etc/hosts.
173 static struct ResolveCache *hosts_head;
176 * Tail of the linked list of DNS lookup results from /etc/hosts.
178 static struct ResolveCache *hosts_tail;
181 * Start of the linked list of active DNS lookups.
183 static struct ActiveLookup *lookup_head;
186 * Tail of the linked list of active DNS lookups.
188 static struct ActiveLookup *lookup_tail;
191 * context of dnsstub library
193 static struct GNUNET_DNSSTUB_Context *dnsstub_ctx;
196 * My domain, to be appended to the hostname to get a FQDN.
198 static char *my_domain;
201 * How many entries do we have in #cache_head DLL?
203 static unsigned int cache_size;
207 * Remove @a entry from cache.
209 * @param rc entry to free
212 free_cache_entry (struct ResolveCache *rc)
214 struct RecordListEntry *pos;
216 while (NULL != (pos = rc->records_head))
218 GNUNET_CONTAINER_DLL_remove (rc->records_head,
221 GNUNET_DNSPARSER_free_record (pos->record);
222 GNUNET_free (pos->record);
225 GNUNET_free_non_null (rc->hostname);
226 GNUNET_CONTAINER_DLL_remove (cache_head,
235 * Remove @a entry from cache.
237 * @param rc entry to free
240 free_hosts_entry (struct ResolveCache *rc)
242 struct RecordListEntry *pos;
244 while (NULL != (pos = rc->records_head))
246 GNUNET_CONTAINER_DLL_remove (rc->records_head,
249 GNUNET_DNSPARSER_free_record (pos->record);
250 GNUNET_free (pos->record);
253 GNUNET_free_non_null (rc->hostname);
254 GNUNET_CONTAINER_DLL_remove (hosts_head,
263 * Release resources associated with @a al
265 * @param al an active lookup
268 free_active_lookup (struct ActiveLookup *al)
270 GNUNET_CONTAINER_DLL_remove (lookup_head,
273 if (NULL != al->resolve_handle)
275 GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
276 al->resolve_handle = NULL;
278 if (NULL != al->timeout_task)
280 GNUNET_SCHEDULER_cancel (al->timeout_task);
281 al->timeout_task = NULL;
283 GNUNET_free_non_null (al->hostname);
290 * Find out if the configuration file line contains a string
291 * starting with "nameserver ", and if so, return a copy of
292 * the nameserver's IP.
294 * @param line line to parse
295 * @param line_len number of characters in @a line
296 * @return NULL if no nameserver is configured in this @a line
299 extract_dns_server (const char* line,
302 if (0 == strncmp (line,
304 strlen ("nameserver ")))
305 return GNUNET_strndup (line + strlen ("nameserver "),
306 line_len - strlen ("nameserver "));
312 * Find out if the configuration file line contains a string
313 * starting with "search ", and if so, return a copy of
314 * the machine's search domain.
316 * @param line line to parse
317 * @param line_len number of characters in @a line
318 * @return NULL if no nameserver is configured in this @a line
321 extract_search_domain (const char* line,
324 if (0 == strncmp (line,
327 return GNUNET_strndup (line + strlen ("search "),
328 line_len - strlen ("search "));
334 * Reads the list of nameservers from /etc/resolve.conf
336 * @param server_addrs[out] a list of null-terminated server address strings
337 * @return the number of server addresses in @server_addrs, -1 on error
340 lookup_dns_servers (char ***server_addrs)
342 struct GNUNET_DISK_FileHandle *fh;
343 struct GNUNET_DISK_MapHandle *mh;
347 unsigned int num_dns_servers;
349 fh = GNUNET_DISK_file_open ("/etc/resolv.conf",
350 GNUNET_DISK_OPEN_READ,
351 GNUNET_DISK_PERM_NONE);
354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
355 "Could not open /etc/resolv.conf. "
356 "DNS resolution will not be possible.\n");
360 GNUNET_DISK_file_handle_size (fh,
363 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
364 "Could not determine size of /etc/resolv.conf. "
365 "DNS resolution will not be possible.\n");
366 GNUNET_DISK_file_close (fh);
369 if ((size_t) bytes_read > SIZE_MAX)
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372 "/etc/resolv.conf file too large to mmap. "
373 "DNS resolution will not be possible.\n");
374 GNUNET_DISK_file_close (fh);
377 buf = GNUNET_DISK_file_map (fh,
379 GNUNET_DISK_MAP_TYPE_READ,
380 (size_t) bytes_read);
381 *server_addrs = NULL;
384 while (read_offset < (size_t) bytes_read)
390 newline = strchr (buf + read_offset,
394 line_len = newline - buf - read_offset;
395 dns_server = extract_dns_server (buf + read_offset,
397 if (NULL != dns_server)
399 GNUNET_array_append (*server_addrs,
403 else if (NULL == my_domain)
405 my_domain = extract_search_domain (buf + read_offset,
408 read_offset += line_len + 1;
410 GNUNET_DISK_file_unmap (mh);
411 GNUNET_DISK_file_close (fh);
412 return (int) num_dns_servers;
417 * Compute name to use for DNS reverse lookups from @a ip.
419 * @param ip IP address to resolve, in binary format, network byte order
420 * @param af address family of @a ip, AF_INET or AF_INET6
423 make_reverse_hostname (const void *ip,
426 char *buf = GNUNET_new_array (80,
432 struct in_addr *addr = (struct in_addr *)ip;
433 uint32_t ip_int = addr->s_addr;
435 for (int i = 3; i >= 0; i--)
437 int n = GNUNET_snprintf (buf + pos,
440 ((uint8_t *)&ip_int)[i]);
448 pos += GNUNET_snprintf (buf + pos,
452 else if (AF_INET6 == af)
454 struct in6_addr *addr = (struct in6_addr *)ip;
455 for (int i = 15; i >= 0; i--)
457 int n = GNUNET_snprintf (buf + pos,
460 addr->s6_addr[i] & 0xf);
467 n = GNUNET_snprintf (buf + pos,
470 addr->s6_addr[i] >> 4);
478 pos += GNUNET_snprintf (buf + pos,
488 * Send DNS @a record back to our @a client.
490 * @param record information to transmit
491 * @param record_type requested record type from client
492 * @param client_request_id to which request are we responding
493 * @param client where to send @a record
494 * @return #GNUNET_YES if we sent a reply,
495 * #GNUNET_NO if the record type is not understood or
496 * does not match @a record_type
499 send_reply (struct GNUNET_DNSPARSER_Record *record,
500 uint16_t record_type,
501 uint32_t client_request_id,
502 struct GNUNET_SERVICE_Client *client)
504 struct GNUNET_RESOLVER_ResponseMessage *msg;
505 struct GNUNET_MQ_Envelope *env;
509 switch (record->type)
511 case GNUNET_DNSPARSER_TYPE_CNAME:
512 if (GNUNET_DNSPARSER_TYPE_CNAME != record_type)
514 payload = record->data.hostname;
515 payload_len = strlen (record->data.hostname) + 1;
517 case GNUNET_DNSPARSER_TYPE_PTR:
518 if (GNUNET_DNSPARSER_TYPE_PTR != record_type)
520 payload = record->data.hostname;
521 payload_len = strlen (record->data.hostname) + 1;
523 case GNUNET_DNSPARSER_TYPE_A:
524 if ( (GNUNET_DNSPARSER_TYPE_A != record_type) &&
525 (GNUNET_DNSPARSER_TYPE_ALL != record_type) )
527 payload = record->data.raw.data;
528 payload_len = record->data.raw.data_len;
530 case GNUNET_DNSPARSER_TYPE_AAAA:
531 if ( (GNUNET_DNSPARSER_TYPE_AAAA != record_type) &&
532 (GNUNET_DNSPARSER_TYPE_ALL != record_type) )
534 payload = record->data.raw.data;
535 payload_len = record->data.raw.data_len;
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Cannot handle DNS response type %u: not supported here\n",
543 env = GNUNET_MQ_msg_extra (msg,
545 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
546 msg->client_id = client_request_id;
547 GNUNET_memcpy (&msg[1],
550 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
557 * Send message to @a client that we transmitted all
558 * responses for @a client_request_id
560 * @param client_request_id to which request are we responding
561 * @param client where to send @a record
564 send_end_msg (uint32_t client_request_id,
565 struct GNUNET_SERVICE_Client *client)
567 struct GNUNET_RESOLVER_ResponseMessage *msg;
568 struct GNUNET_MQ_Envelope *env;
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
571 "Sending END message\n");
572 env = GNUNET_MQ_msg (msg,
573 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
574 msg->client_id = client_request_id;
575 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
581 * Remove expired entries from @a rc
583 * @param rc entry in resolver cache
584 * @return #GNUNET_YES if @a rc was completely expired
585 * #GNUNET_NO if some entries are left
588 remove_expired (struct ResolveCache *rc)
590 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
591 struct RecordListEntry *n;
593 for (struct RecordListEntry *pos = rc->records_head;
598 if (now.abs_value_us > pos->record->expiration_time.abs_value_us)
600 GNUNET_CONTAINER_DLL_remove (rc->records_head,
603 GNUNET_DNSPARSER_free_record (pos->record);
604 GNUNET_free (pos->record);
608 if (NULL == rc->records_head)
610 free_cache_entry (rc);
618 * Process DNS request for @a hostname with request ID @a request_id
619 * from @a client demanding records of type @a record_type.
621 * @param hostname DNS name to resolve
622 * @param record_type desired record type
623 * @param client_request_id client's request ID
624 * @param client who should get the result?
627 process_get (const char *hostname,
628 uint16_t record_type,
629 uint32_t client_request_id,
630 struct GNUNET_SERVICE_Client *client);
634 * Get an IP address as a string (works for both IPv4 and IPv6). Note
635 * that the resolution happens asynchronously and that the first call
636 * may not immediately result in the FQN (but instead in a
637 * human-readable IP address).
639 * @param hostname what hostname was to be resolved
640 * @param record_type what type of record was requested
641 * @param client_request_id unique identification of the client's request
642 * @param client handle to the client making the request (for sending the reply)
645 try_cache (const char *hostname,
646 uint16_t record_type,
647 uint32_t client_request_id,
648 struct GNUNET_SERVICE_Client *client)
650 struct ResolveCache *pos;
651 struct ResolveCache *next;
655 in_hosts = GNUNET_NO;
656 for (pos = hosts_head; NULL != pos; pos = pos->next)
657 if (0 == strcmp (pos->hostname,
660 in_hosts = GNUNET_YES;
666 for (pos = next; NULL != pos; pos = next)
669 if (GNUNET_YES == remove_expired (pos))
671 if (0 == strcmp (pos->hostname,
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
679 "No cache entry for '%s'\n",
683 if ( (GNUNET_NO == in_hosts) &&
684 (cache_head != pos) )
686 /* move result to head to achieve LRU for cache eviction */
687 GNUNET_CONTAINER_DLL_remove (cache_head,
690 GNUNET_CONTAINER_DLL_insert (cache_head,
695 for (struct RecordListEntry *rle = pos->records_head;
699 const struct GNUNET_DNSPARSER_Record *record = rle->record;
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "Found cache entry for '%s', record type '%u'\n",
705 if ( (GNUNET_DNSPARSER_TYPE_CNAME == record->type) &&
706 (GNUNET_DNSPARSER_TYPE_CNAME != record_type) &&
707 (GNUNET_NO == found) )
709 const char *hostname = record->data.hostname;
711 process_get (hostname,
715 return GNUNET_YES; /* counts as a cache "hit" */
717 found |= send_reply (rle->record,
722 if (GNUNET_NO == found)
723 return GNUNET_NO; /* had records, but none matched! */
724 send_end_msg (client_request_id,
731 * Create DNS query for @a hostname of type @a type
732 * with DNS request ID @a dns_id.
734 * @param hostname DNS name to query
735 * @param type requested DNS record type
736 * @param dns_id what should be the DNS request ID
737 * @param packet_buf[out] where to write the request packet
738 * @param packet_size[out] set to size of @a packet_buf on success
739 * @return #GNUNET_OK on success
742 pack (const char *hostname,
748 struct GNUNET_DNSPARSER_Query query;
749 struct GNUNET_DNSPARSER_Packet packet;
751 query.name = (char *)hostname;
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",
771 return GNUNET_SYSERR;
777 cache_answers(const char* name,
778 struct GNUNET_DNSPARSER_Record *records,
779 unsigned int num_records)
781 struct ResolveCache *rc;
782 struct GNUNET_DNSPARSER_Record *record;
783 struct RecordListEntry *rle;
785 for (unsigned int i = 0; i != num_records; i++)
787 record = &records[i];
789 for (rc = cache_head; NULL != rc; rc = rc->next)
790 if (0 == strcasecmp (rc->hostname,
795 rc = GNUNET_new (struct ResolveCache);
796 rc->hostname = GNUNET_strdup (name);
797 GNUNET_CONTAINER_DLL_insert (cache_head,
802 /* TODO: ought to check first if we have this exact record
803 already in the cache! */
804 rle = GNUNET_new (struct RecordListEntry);
805 rle->record = GNUNET_DNSPARSER_duplicate_record (record);
806 GNUNET_CONTAINER_DLL_insert (rc->records_head,
813 * We got a result from DNS. Add it to the cache and
814 * see if we can make our client happy...
816 * @param cls the `struct ActiveLookup`
817 * @param dns the DNS response
818 * @param dns_len number of bytes in @a dns
821 handle_resolve_result (void *cls,
822 const struct GNUNET_TUN_DnsHeader *dns,
825 struct ActiveLookup *al = cls;
826 struct GNUNET_DNSPARSER_Packet *parsed;
828 parsed = GNUNET_DNSPARSER_parse ((const char *)dns,
832 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
833 "Failed to parse DNS reply (hostname %s, request ID %u)\n",
838 if (al->dns_id != ntohs (parsed->id))
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
841 "Request ID in DNS reply does not match\n");
842 GNUNET_DNSPARSER_free_packet (parsed);
845 if (0 == parsed->num_answers + parsed->num_authority_records + parsed->num_additional_records)
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "DNS reply (hostname %s, request ID %u) contains no answers\n",
850 (unsigned int) al->client_request_id);
851 /* resume by trying again from cache */
853 try_cache (al->hostname,
855 al->client_request_id,
857 /* cache failed, tell client we could not get an answer */
859 send_end_msg (al->client_request_id,
862 GNUNET_DNSPARSER_free_packet (parsed);
863 free_active_lookup (al);
866 /* LRU-based cache eviction: we remove from tail */
867 while (cache_size > MAX_CACHE)
868 free_cache_entry (cache_tail);
870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
871 "Got reply for hostname %s and request ID %u\n",
873 (unsigned int) al->client_request_id);
875 cache_answers(al->hostname,
876 parsed->answers, parsed->num_answers);
877 cache_answers(al->hostname,
878 parsed->authority_records, parsed->num_authority_records);
879 cache_answers(al->hostname,
880 parsed->additional_records, parsed->num_additional_records);
882 /* see if we need to do the 2nd request for AAAA records */
883 if ( (GNUNET_DNSPARSER_TYPE_ALL == al->record_type) &&
884 (GNUNET_NO == al->did_aaaa) )
890 dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
894 GNUNET_DNSPARSER_TYPE_AAAA,
899 al->did_aaaa = GNUNET_YES;
901 GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
903 GNUNET_DNSSTUB_resolve (dnsstub_ctx,
906 &handle_resolve_result,
908 GNUNET_free (packet_buf);
909 GNUNET_DNSPARSER_free_packet (parsed);
914 /* resume by trying again from cache */
916 try_cache (al->hostname,
918 al->client_request_id,
920 /* cache failed, tell client we could not get an answer */
922 send_end_msg (al->client_request_id,
925 free_active_lookup (al);
926 GNUNET_DNSPARSER_free_packet (parsed);
931 * We encountered a timeout trying to perform a
934 * @param cls a `struct ActiveLookup`
937 handle_resolve_timeout (void *cls)
939 struct ActiveLookup *al = cls;
941 al->timeout_task = NULL;
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 "DNS lookup timeout!\n");
944 send_end_msg (al->client_request_id,
946 free_active_lookup (al);
951 * Initiate an active lookup, then cache the result and
952 * try to then complete the resolution.
954 * @param hostname DNS name to resolve
955 * @param record_type record type to locate
956 * @param client_request_id client request ID
957 * @param client handle to the client
958 * @return #GNUNET_OK if the DNS query is now pending
961 resolve_and_cache (const char* hostname,
962 uint16_t record_type,
963 uint32_t client_request_id,
964 struct GNUNET_SERVICE_Client *client)
968 struct ActiveLookup *al;
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "resolve_and_cache `%s'\n",
975 dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
978 if (GNUNET_DNSPARSER_TYPE_ALL == record_type)
979 type = GNUNET_DNSPARSER_TYPE_A;
989 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
990 "Failed to pack query for hostname `%s'\n",
992 return GNUNET_SYSERR;
995 al = GNUNET_new (struct ActiveLookup);
996 al->hostname = GNUNET_strdup (hostname);
997 al->record_type = record_type;
998 al->client_request_id = client_request_id;
1000 al->client = client;
1001 al->timeout_task = GNUNET_SCHEDULER_add_delayed (DNS_TIMEOUT,
1002 &handle_resolve_timeout,
1004 al->resolve_handle =
1005 GNUNET_DNSSTUB_resolve (dnsstub_ctx,
1008 &handle_resolve_result,
1010 GNUNET_free (packet_buf);
1011 GNUNET_CONTAINER_DLL_insert (lookup_head,
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1015 "Resolving %s, client_request_id = %u, dns_id = %u\n",
1017 (unsigned int) client_request_id,
1018 (unsigned int) dns_id);
1024 * Process DNS request for @a hostname with request ID @a client_request_id
1025 * from @a client demanding records of type @a record_type.
1027 * @param hostname DNS name to resolve
1028 * @param record_type desired record type
1029 * @param client_request_id client's request ID
1030 * @param client who should get the result?
1033 process_get (const char *hostname,
1034 uint16_t record_type,
1035 uint32_t client_request_id,
1036 struct GNUNET_SERVICE_Client *client)
1041 try_cache (hostname,
1046 if ( (NULL != my_domain) &&
1047 (NULL == strchr (hostname,
1048 (unsigned char) '.')) &&
1049 (strlen (hostname) + strlen (my_domain) <= 253) )
1051 GNUNET_snprintf (fqdn,
1057 else if (strlen (hostname) < 255)
1059 GNUNET_snprintf (fqdn,
1067 GNUNET_SERVICE_client_drop (client);
1077 resolve_and_cache (fqdn,
1082 send_end_msg (client_request_id,
1090 * Verify well-formedness of GET-message.
1092 * @param cls closure, unused
1093 * @param get the actual message
1094 * @return #GNUNET_OK if @a get is well-formed
1097 check_get (void *cls,
1098 const struct GNUNET_RESOLVER_GetMessage *get)
1105 size = ntohs (get->header.size) - sizeof (*get);
1106 direction = ntohl (get->direction);
1107 if (GNUNET_NO == direction)
1109 GNUNET_MQ_check_zero_termination (get);
1112 af = ntohl (get->af);
1116 if (size != sizeof (struct in_addr))
1119 return GNUNET_SYSERR;
1123 if (size != sizeof (struct in6_addr))
1126 return GNUNET_SYSERR;
1131 return GNUNET_SYSERR;
1138 * Handle GET-message.
1140 * @param cls identification of the client
1141 * @param msg the actual message
1144 handle_get (void *cls,
1145 const struct GNUNET_RESOLVER_GetMessage *msg)
1147 struct GNUNET_SERVICE_Client *client = cls;
1150 uint32_t client_request_id;
1153 direction = ntohl (msg->direction);
1154 af = ntohl (msg->af);
1155 client_request_id = msg->client_id;
1156 GNUNET_SERVICE_client_continue (client);
1157 if (GNUNET_NO == direction)
1159 /* IP from hostname */
1160 hostname = GNUNET_strdup ((const char *) &msg[1]);
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1162 "Client asks to resolve `%s'\n",
1168 process_get (hostname,
1169 GNUNET_DNSPARSER_TYPE_ALL,
1176 process_get (hostname,
1177 GNUNET_DNSPARSER_TYPE_A,
1184 process_get (hostname,
1185 GNUNET_DNSPARSER_TYPE_AAAA,
1192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1193 "got invalid af: %d\n",
1201 /* hostname from IP */
1202 hostname = make_reverse_hostname (&msg[1],
1204 process_get (hostname,
1205 GNUNET_DNSPARSER_TYPE_PTR,
1209 GNUNET_free_non_null (hostname);
1214 * Service is shutting down, clean up.
1216 * @param cls NULL, unused
1219 shutdown_task (void *cls)
1223 while (NULL != lookup_head)
1224 free_active_lookup (lookup_head);
1225 while (NULL != cache_head)
1226 free_cache_entry (cache_head);
1227 while (NULL != hosts_head)
1228 free_hosts_entry (hosts_head);
1229 GNUNET_DNSSTUB_stop (dnsstub_ctx);
1230 GNUNET_free_non_null (my_domain);
1235 * Add information about a host from /etc/hosts
1238 * @param hostname the name of the host
1239 * @param rec_type DNS record type to use
1240 * @param data payload
1241 * @param data_size number of bytes in @a data
1244 add_host (const char *hostname,
1249 struct ResolveCache *rc;
1250 struct RecordListEntry *rle;
1251 struct GNUNET_DNSPARSER_Record *rec;
1253 rec = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_Record));
1254 rec->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS;
1255 rec->type = rec_type;
1256 rec->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1257 rec->name = GNUNET_strdup (hostname);
1258 rec->data.raw.data = GNUNET_memdup (data,
1260 rec->data.raw.data_len = data_size;
1261 rle = GNUNET_new (struct RecordListEntry);
1263 rc = GNUNET_new (struct ResolveCache);
1264 rc->hostname = GNUNET_strdup (hostname);
1265 GNUNET_CONTAINER_DLL_insert (rc->records_head,
1268 GNUNET_CONTAINER_DLL_insert (hosts_head,
1275 * Extract host information from a line in /etc/hosts
1277 * @param line the line to parse
1278 * @param line_len number of bytes in @a line
1281 extract_hosts (const char *line,
1290 /* ignore everything after '#' */
1292 (unsigned char) '#',
1295 line_len = c - line;
1296 /* ignore leading whitespace */
1297 while ( (0 < line_len) &&
1298 isspace ((unsigned char) *line) )
1303 tbuf = GNUNET_strndup (line,
1305 tok = strtok (tbuf, " \t");
1311 if (1 == inet_pton (AF_INET,
1315 while (NULL != (tok = strtok (NULL, " \t")))
1317 GNUNET_DNSPARSER_TYPE_A,
1319 sizeof (struct in_addr));
1321 else if (1 == inet_pton (AF_INET6,
1325 while (NULL != (tok = strtok (NULL, " \t")))
1327 GNUNET_DNSPARSER_TYPE_AAAA,
1329 sizeof (struct in6_addr));
1336 * Reads the list of hosts from /etc/hosts.
1339 load_etc_hosts (void)
1341 struct GNUNET_DISK_FileHandle *fh;
1342 struct GNUNET_DISK_MapHandle *mh;
1347 fh = GNUNET_DISK_file_open ("/etc/hosts",
1348 GNUNET_DISK_OPEN_READ,
1349 GNUNET_DISK_PERM_NONE);
1352 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1353 "Failed to open /etc/hosts");
1357 GNUNET_DISK_file_handle_size (fh,
1360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1361 "Could not determin size of /etc/hosts. "
1362 "DNS resolution will not be possible.\n");
1363 GNUNET_DISK_file_close (fh);
1366 if ((size_t) bytes_read > SIZE_MAX)
1368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1369 "/etc/hosts file too large to mmap. "
1370 "DNS resolution will not be possible.\n");
1371 GNUNET_DISK_file_close (fh);
1374 buf = GNUNET_DISK_file_map (fh,
1376 GNUNET_DISK_MAP_TYPE_READ,
1377 (size_t) bytes_read);
1379 while (read_offset < (size_t) bytes_read)
1381 const char *newline;
1384 newline = strchr (buf + read_offset,
1386 if (NULL == newline)
1388 line_len = newline - buf - read_offset;
1389 extract_hosts (buf + read_offset,
1391 read_offset += line_len + 1;
1393 GNUNET_DISK_file_unmap (mh);
1394 GNUNET_DISK_file_close (fh);
1399 * Service is starting, initialize everything.
1401 * @param cls NULL, unused
1402 * @param cfg our configuration
1403 * @param sh service handle
1407 const struct GNUNET_CONFIGURATION_Handle *cfg,
1408 struct GNUNET_SERVICE_Handle *sh)
1411 int num_dns_servers;
1416 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1418 dnsstub_ctx = GNUNET_DNSSTUB_start (128);
1420 num_dns_servers = lookup_dns_servers (&dns_servers);
1421 if (0 >= num_dns_servers)
1423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1424 _("No DNS server available. DNS resolution will not be possible.\n"));
1427 for (int i = 0; i < num_dns_servers; i++)
1429 int result = GNUNET_DNSSTUB_add_dns_ip (dnsstub_ctx, dns_servers[i]);
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1431 "Adding DNS server '%s': %s\n",
1433 GNUNET_OK == result ? "success" : "failure");
1434 GNUNET_free (dns_servers[i]);
1436 GNUNET_free_non_null (dns_servers);
1441 * Callback called when a client connects to the service.
1443 * @param cls closure for the service, unused
1444 * @param c the new client that connected to the service
1445 * @param mq the message queue used to send messages to the client
1449 connect_cb (void *cls,
1450 struct GNUNET_SERVICE_Client *c,
1451 struct GNUNET_MQ_Handle *mq)
1461 * Callback called when a client disconnected from the service
1463 * @param cls closure for the service
1464 * @param c the client that disconnected
1465 * @param internal_cls should be equal to @a c
1468 disconnect_cb (void *cls,
1469 struct GNUNET_SERVICE_Client *c,
1472 struct ActiveLookup *n;
1475 GNUNET_assert (c == internal_cls);
1477 for (struct ActiveLookup *al = n;
1482 if (al->client == c)
1483 free_active_lookup (al);
1489 * Define "main" method using service macro.
1493 GNUNET_SERVICE_OPTION_NONE,
1498 GNUNET_MQ_hd_var_size (get,
1499 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
1500 struct GNUNET_RESOLVER_GetMessage,
1502 GNUNET_MQ_handler_end ());
1505 #if defined(LINUX) && defined(__GLIBC__)
1509 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1511 void __attribute__ ((constructor))
1512 GNUNET_RESOLVER_memory_init ()
1514 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1515 mallopt (M_TOP_PAD, 1 * 1024);
1521 /* end of gnunet-service-resolver.c */