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;
65 * A cached DNS lookup result.
70 * This is a doubly linked list.
72 struct ResolveCache *next;
75 * This is a doubly linked list.
77 struct ResolveCache *prev;
80 * Which hostname is this cache for?
85 * head of a double linked list containing the lookup results
87 struct RecordListEntry *records_head;
90 * tail of a double linked list containing the lookup results
92 struct RecordListEntry *records_tail;
98 * Information about pending lookups.
105 struct ActiveLookup *next;
110 struct ActiveLookup *prev;
113 * The client that queried the records contained in this cache entry.
115 struct GNUNET_SERVICE_Client *client;
118 * handle for cancelling a request
120 struct GNUNET_DNSSTUB_RequestSocket *resolve_handle;
123 * handle for the resolution timeout task
125 struct GNUNET_SCHEDULER_Task *timeout_task;
128 * Which hostname are we resolving?
133 * If @a record_type is #GNUNET_DNSPARSER_TYPE_ALL, did we go again
134 * for the AAAA records yet?
139 * type of queried DNS record
141 uint16_t record_type;
144 * Unique request ID of a client if a query for this hostname/record_type
145 * is currently pending, undefined otherwise.
147 uint32_t client_request_id;
150 * Unique DNS request ID of a client if a query for this hostname/record_type
151 * is currently pending, undefined otherwise.
159 * Start of the linked list of cached DNS lookup results.
161 static struct ResolveCache *cache_head;
164 * Tail of the linked list of cached DNS lookup results.
166 static struct ResolveCache *cache_tail;
169 * Head of the linked list of DNS lookup results from /etc/hosts.
171 static struct ResolveCache *hosts_head;
174 * Tail of the linked list of DNS lookup results from /etc/hosts.
176 static struct ResolveCache *hosts_tail;
179 * Start of the linked list of active DNS lookups.
181 static struct ActiveLookup *lookup_head;
184 * Tail of the linked list of active DNS lookups.
186 static struct ActiveLookup *lookup_tail;
189 * context of dnsstub library
191 static struct GNUNET_DNSSTUB_Context *dnsstub_ctx;
194 * My domain, to be appended to the hostname to get a FQDN.
196 static char *my_domain;
199 * How many entries do we have in #cache_head DLL?
201 static unsigned int cache_size;
205 * Remove @a entry from cache.
207 * @param rc entry to free
210 free_cache_entry (struct ResolveCache *rc)
212 struct RecordListEntry *pos;
214 while (NULL != (pos = rc->records_head))
216 GNUNET_CONTAINER_DLL_remove (rc->records_head,
219 GNUNET_DNSPARSER_free_record (pos->record);
220 GNUNET_free (pos->record);
223 GNUNET_free_non_null (rc->hostname);
224 GNUNET_CONTAINER_DLL_remove (cache_head,
233 * Remove @a entry from cache.
235 * @param rc entry to free
238 free_hosts_entry (struct ResolveCache *rc)
240 struct RecordListEntry *pos;
242 while (NULL != (pos = rc->records_head))
244 GNUNET_CONTAINER_DLL_remove (rc->records_head,
247 GNUNET_DNSPARSER_free_record (pos->record);
248 GNUNET_free (pos->record);
251 GNUNET_free_non_null (rc->hostname);
252 GNUNET_CONTAINER_DLL_remove (hosts_head,
261 * Release resources associated with @a al
263 * @param al an active lookup
266 free_active_lookup (struct ActiveLookup *al)
268 GNUNET_CONTAINER_DLL_remove (lookup_head,
271 if (NULL != al->resolve_handle)
273 GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
274 al->resolve_handle = NULL;
276 if (NULL != al->timeout_task)
278 GNUNET_SCHEDULER_cancel (al->timeout_task);
279 al->timeout_task = NULL;
281 GNUNET_free_non_null (al->hostname);
288 * Find out if the configuration file line contains a string
289 * starting with "nameserver ", and if so, return a copy of
290 * the nameserver's IP.
292 * @param line line to parse
293 * @param line_len number of characters in @a line
294 * @return NULL if no nameserver is configured in this @a line
297 extract_dns_server (const char* line,
300 if (0 == strncmp (line,
302 strlen ("nameserver ")))
303 return GNUNET_strndup (line + strlen ("nameserver "),
304 line_len - strlen ("nameserver "));
310 * Find out if the configuration file line contains a string
311 * starting with "search ", and if so, return a copy of
312 * the machine's search domain.
314 * @param line line to parse
315 * @param line_len number of characters in @a line
316 * @return NULL if no nameserver is configured in this @a line
319 extract_search_domain (const char* line,
322 if (0 == strncmp (line,
325 return GNUNET_strndup (line + strlen ("search "),
326 line_len - strlen ("search "));
332 * Reads the list of nameservers from /etc/resolve.conf
334 * @param server_addrs[out] a list of null-terminated server address strings
335 * @return the number of server addresses in @server_addrs, -1 on error
338 lookup_dns_servers (char ***server_addrs)
340 struct GNUNET_DISK_FileHandle *fh;
341 struct GNUNET_DISK_MapHandle *mh;
345 unsigned int num_dns_servers;
347 fh = GNUNET_DISK_file_open ("/etc/resolv.conf",
348 GNUNET_DISK_OPEN_READ,
349 GNUNET_DISK_PERM_NONE);
352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
353 "Could not open /etc/resolv.conf. "
354 "DNS resolution will not be possible.\n");
358 GNUNET_DISK_file_handle_size (fh,
361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
362 "Could not determine size of /etc/resolv.conf. "
363 "DNS resolution will not be possible.\n");
364 GNUNET_DISK_file_close (fh);
367 if ((size_t) bytes_read > SIZE_MAX)
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
370 "/etc/resolv.conf file too large to mmap. "
371 "DNS resolution will not be possible.\n");
372 GNUNET_DISK_file_close (fh);
375 buf = GNUNET_DISK_file_map (fh,
377 GNUNET_DISK_MAP_TYPE_READ,
378 (size_t) bytes_read);
379 *server_addrs = NULL;
382 while (read_offset < (size_t) bytes_read)
388 newline = strchr (buf + read_offset,
392 line_len = newline - buf - read_offset;
393 dns_server = extract_dns_server (buf + read_offset,
395 if (NULL != dns_server)
397 GNUNET_array_append (*server_addrs,
401 else if (NULL == my_domain)
403 my_domain = extract_search_domain (buf + read_offset,
406 read_offset += line_len + 1;
408 GNUNET_DISK_file_unmap (mh);
409 GNUNET_DISK_file_close (fh);
410 return (int) num_dns_servers;
415 * Compute name to use for DNS reverse lookups from @a ip.
417 * @param ip IP address to resolve, in binary format, network byte order
418 * @param af address family of @a ip, AF_INET or AF_INET6
421 make_reverse_hostname (const void *ip,
424 char *buf = GNUNET_new_array (80,
430 struct in_addr *addr = (struct in_addr *)ip;
431 uint32_t ip_int = addr->s_addr;
433 for (int i = 3; i >= 0; i--)
435 int n = GNUNET_snprintf (buf + pos,
438 ((uint8_t *)&ip_int)[i]);
446 pos += GNUNET_snprintf (buf + pos,
450 else if (AF_INET6 == af)
452 struct in6_addr *addr = (struct in6_addr *)ip;
453 for (int i = 15; i >= 0; i--)
455 int n = GNUNET_snprintf (buf + pos,
458 addr->s6_addr[i] & 0xf);
465 n = GNUNET_snprintf (buf + pos,
468 addr->s6_addr[i] >> 4);
476 pos += GNUNET_snprintf (buf + pos,
486 * Send DNS @a record back to our @a client.
488 * @param record information to transmit
489 * @param record_type requested record type from client
490 * @param client_request_id to which request are we responding
491 * @param client where to send @a record
492 * @return #GNUNET_YES if we sent a reply,
493 * #GNUNET_NO if the record type is not understood or
494 * does not match @a record_type
497 send_reply (struct GNUNET_DNSPARSER_Record *record,
498 uint16_t record_type,
499 uint32_t client_request_id,
500 struct GNUNET_SERVICE_Client *client)
502 struct GNUNET_RESOLVER_ResponseMessage *msg;
503 struct GNUNET_MQ_Envelope *env;
507 switch (record->type)
509 case GNUNET_DNSPARSER_TYPE_CNAME:
510 if (GNUNET_DNSPARSER_TYPE_CNAME != record_type)
512 payload = record->data.hostname;
513 payload_len = strlen (record->data.hostname) + 1;
515 case GNUNET_DNSPARSER_TYPE_PTR:
516 if (GNUNET_DNSPARSER_TYPE_PTR != record_type)
518 payload = record->data.hostname;
519 payload_len = strlen (record->data.hostname) + 1;
521 case GNUNET_DNSPARSER_TYPE_A:
522 if ( (GNUNET_DNSPARSER_TYPE_A != record_type) &&
523 (GNUNET_DNSPARSER_TYPE_ALL != record_type) )
525 payload = record->data.raw.data;
526 payload_len = record->data.raw.data_len;
528 case GNUNET_DNSPARSER_TYPE_AAAA:
529 if ( (GNUNET_DNSPARSER_TYPE_AAAA != record_type) &&
530 (GNUNET_DNSPARSER_TYPE_ALL != record_type) )
532 payload = record->data.raw.data;
533 payload_len = record->data.raw.data_len;
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537 "Cannot handle DNS response type %u: not supported here\n",
541 env = GNUNET_MQ_msg_extra (msg,
543 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
544 msg->client_id = client_request_id;
545 GNUNET_memcpy (&msg[1],
548 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
555 * Send message to @a client that we transmitted all
556 * responses for @a client_request_id
558 * @param client_request_id to which request are we responding
559 * @param client where to send @a record
562 send_end_msg (uint32_t client_request_id,
563 struct GNUNET_SERVICE_Client *client)
565 struct GNUNET_RESOLVER_ResponseMessage *msg;
566 struct GNUNET_MQ_Envelope *env;
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "Sending END message\n");
570 env = GNUNET_MQ_msg (msg,
571 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
572 msg->client_id = client_request_id;
573 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
579 * Remove expired entries from @a rc
581 * @param rc entry in resolver cache
582 * @return #GNUNET_YES if @a rc was completely expired
583 * #GNUNET_NO if some entries are left
586 remove_expired (struct ResolveCache *rc)
588 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
589 struct RecordListEntry *n;
591 for (struct RecordListEntry *pos = rc->records_head;
596 if (now.abs_value_us > pos->record->expiration_time.abs_value_us)
598 GNUNET_CONTAINER_DLL_remove (rc->records_head,
601 GNUNET_DNSPARSER_free_record (pos->record);
602 GNUNET_free (pos->record);
606 if (NULL == rc->records_head)
608 free_cache_entry (rc);
616 * Process DNS request for @a hostname with request ID @a request_id
617 * from @a client demanding records of type @a record_type.
619 * @param hostname DNS name to resolve
620 * @param record_type desired record type
621 * @param client_request_id client's request ID
622 * @param client who should get the result?
625 process_get (const char *hostname,
626 uint16_t record_type,
627 uint32_t client_request_id,
628 struct GNUNET_SERVICE_Client *client);
632 * Get an IP address as a string (works for both IPv4 and IPv6). Note
633 * that the resolution happens asynchronously and that the first call
634 * may not immediately result in the FQN (but instead in a
635 * human-readable IP address).
637 * @param hostname what hostname was to be resolved
638 * @param record_type what type of record was requested
639 * @param client_request_id unique identification of the client's request
640 * @param client handle to the client making the request (for sending the reply)
643 try_cache (const char *hostname,
644 uint16_t record_type,
645 uint32_t client_request_id,
646 struct GNUNET_SERVICE_Client *client)
648 struct ResolveCache *pos;
649 struct ResolveCache *next;
653 in_hosts = GNUNET_NO;
654 for (pos = hosts_head; NULL != pos; pos = pos->next)
655 if (0 == strcmp (pos->hostname,
658 in_hosts = GNUNET_YES;
664 for (pos = next; NULL != pos; pos = next)
667 if (GNUNET_YES == remove_expired (pos))
669 if (0 == strcmp (pos->hostname,
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 "No cache entry for '%s'\n",
681 if ( (GNUNET_NO == in_hosts) &&
682 (cache_head != pos) )
684 /* move result to head to achieve LRU for cache eviction */
685 GNUNET_CONTAINER_DLL_remove (cache_head,
688 GNUNET_CONTAINER_DLL_insert (cache_head,
693 for (struct RecordListEntry *rle = pos->records_head;
697 const struct GNUNET_DNSPARSER_Record *record = rle->record;
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
700 "Found cache entry for '%s', record type '%u'\n",
703 if ( (GNUNET_DNSPARSER_TYPE_CNAME == record->type) &&
704 (GNUNET_DNSPARSER_TYPE_CNAME != record_type) &&
705 (GNUNET_NO == found) )
707 const char *hostname = record->data.hostname;
709 process_get (hostname,
713 return GNUNET_YES; /* counts as a cache "hit" */
715 found |= send_reply (rle->record,
720 if (GNUNET_NO == found)
721 return GNUNET_NO; /* had records, but none matched! */
722 send_end_msg (client_request_id,
729 * Create DNS query for @a hostname of type @a type
730 * with DNS request ID @a dns_id.
732 * @param hostname DNS name to query
733 * @param type requested DNS record type
734 * @param dns_id what should be the DNS request ID
735 * @param packet_buf[out] where to write the request packet
736 * @param packet_size[out] set to size of @a packet_buf on success
737 * @return #GNUNET_OK on success
740 pack (const char *hostname,
746 struct GNUNET_DNSPARSER_Query query;
747 struct GNUNET_DNSPARSER_Packet packet;
749 query.name = (char *)hostname;
751 query.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
755 packet.num_queries = 1;
756 packet.queries = &query;
757 packet.id = htons (dns_id);
758 packet.flags.recursion_desired = 1;
760 GNUNET_DNSPARSER_pack (&packet,
765 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
766 "Failed to pack query for hostname `%s'\n",
769 return GNUNET_SYSERR;
776 * We got a result from DNS. Add it to the cache and
777 * see if we can make our client happy...
779 * @param cls the `struct ActiveLookup`
780 * @param dns the DNS response
781 * @param dns_len number of bytes in @a dns
784 handle_resolve_result (void *cls,
785 const struct GNUNET_TUN_DnsHeader *dns,
788 struct ActiveLookup *al = cls;
789 struct GNUNET_DNSPARSER_Packet *parsed;
790 struct ResolveCache *rc;
792 parsed = GNUNET_DNSPARSER_parse ((const char *)dns,
796 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
797 "Failed to parse DNS reply (hostname %s, request ID %u)\n",
802 if (al->dns_id != ntohs (parsed->id))
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
805 "Request ID in DNS reply does not match\n");
806 GNUNET_DNSPARSER_free_packet (parsed);
809 if (0 == parsed->num_answers + parsed->num_authority_records + parsed->num_additional_records)
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "DNS reply (hostname %s, request ID %u) contains no answers\n",
814 (unsigned int) al->client_request_id);
815 GNUNET_DNSPARSER_free_packet (parsed);
816 send_end_msg (al->client_request_id,
818 free_active_lookup (al);
821 /* LRU-based cache eviction: we remove from tail */
822 while (cache_size > MAX_CACHE)
823 free_cache_entry (cache_tail);
825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
826 "Got reply for hostname %s and request ID %u\n",
828 (unsigned int) al->client_request_id);
830 for (unsigned int i = 0; i != parsed->num_answers; i++)
832 struct GNUNET_DNSPARSER_Record *record = &parsed->answers[i];
833 struct RecordListEntry *rle;
835 for (rc = cache_head; NULL != rc; rc = rc->next)
836 if (0 == strcasecmp (rc->hostname,
841 rc = GNUNET_new (struct ResolveCache);
842 rc->hostname = GNUNET_strdup (record->name);
843 GNUNET_CONTAINER_DLL_insert (cache_head,
848 /* TODO: ought to check first if we have this exact record
849 already in the cache! */
850 rle = GNUNET_new (struct RecordListEntry);
851 rle->record = GNUNET_DNSPARSER_duplicate_record (record);
852 GNUNET_CONTAINER_DLL_insert (rc->records_head,
856 for (unsigned int i = 0; i != parsed->num_authority_records; i++)
858 struct GNUNET_DNSPARSER_Record *record = &parsed->authority_records[i];
859 struct RecordListEntry *rle;
861 for (rc = cache_head; NULL != rc; rc = rc->next)
862 if (0 == strcasecmp (rc->hostname,
867 rc = GNUNET_new (struct ResolveCache);
868 rc->hostname = GNUNET_strdup (record->name);
869 GNUNET_CONTAINER_DLL_insert (cache_head,
874 /* TODO: ought to check first if we have this exact record
875 already in the cache! */
876 rle = GNUNET_new (struct RecordListEntry);
877 rle->record = GNUNET_DNSPARSER_duplicate_record (record);
878 GNUNET_CONTAINER_DLL_insert (rc->records_head,
882 for (unsigned int i = 0; i != parsed->num_additional_records; i++)
884 struct GNUNET_DNSPARSER_Record *record = &parsed->additional_records[i];
885 struct RecordListEntry *rle;
887 for (rc = cache_head; NULL != rc; rc = rc->next)
888 if (0 == strcasecmp (rc->hostname,
893 rc = GNUNET_new (struct ResolveCache);
894 rc->hostname = GNUNET_strdup (record->name);
895 GNUNET_CONTAINER_DLL_insert (cache_head,
900 /* TODO: ought to check first if we have this exact record
901 already in the cache! */
902 rle = GNUNET_new (struct RecordListEntry);
903 rle->record = GNUNET_DNSPARSER_duplicate_record (record);
904 GNUNET_CONTAINER_DLL_insert (rc->records_head,
908 /* see if we need to do the 2nd request for AAAA records */
909 if ( (GNUNET_DNSPARSER_TYPE_ALL == al->record_type) &&
910 (GNUNET_NO == al->did_aaaa) )
916 dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
920 GNUNET_DNSPARSER_TYPE_AAAA,
925 al->did_aaaa = GNUNET_YES;
927 GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
929 GNUNET_DNSSTUB_resolve (dnsstub_ctx,
932 &handle_resolve_result,
934 GNUNET_free (packet_buf);
935 GNUNET_DNSPARSER_free_packet (parsed);
940 /* resume by trying again from cache */
942 try_cache (al->hostname,
944 al->client_request_id,
946 /* cache failed, tell client we could not get an answer */
948 send_end_msg (al->client_request_id,
951 free_active_lookup (al);
952 GNUNET_DNSPARSER_free_packet (parsed);
957 * We encountered a timeout trying to perform a
960 * @param cls a `struct ActiveLookup`
963 handle_resolve_timeout (void *cls)
965 struct ActiveLookup *al = cls;
967 al->timeout_task = NULL;
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
969 "DNS lookup timeout!\n");
970 send_end_msg (al->client_request_id,
972 free_active_lookup (al);
977 * Initiate an active lookup, then cache the result and
978 * try to then complete the resolution.
980 * @param hostname DNS name to resolve
981 * @param record_type record type to locate
982 * @param client_request_id client request ID
983 * @param client handle to the client
984 * @return #GNUNET_OK if the DNS query is now pending
987 resolve_and_cache (const char* hostname,
988 uint16_t record_type,
989 uint32_t client_request_id,
990 struct GNUNET_SERVICE_Client *client)
994 struct ActiveLookup *al;
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "resolve_and_cache `%s'\n",
1001 dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1004 if (GNUNET_DNSPARSER_TYPE_ALL == record_type)
1005 type = GNUNET_DNSPARSER_TYPE_A;
1015 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1016 "Failed to pack query for hostname `%s'\n",
1018 return GNUNET_SYSERR;
1021 al = GNUNET_new (struct ActiveLookup);
1022 al->hostname = GNUNET_strdup (hostname);
1023 al->record_type = record_type;
1024 al->client_request_id = client_request_id;
1025 al->dns_id = dns_id;
1026 al->client = client;
1027 al->timeout_task = GNUNET_SCHEDULER_add_delayed (DNS_TIMEOUT,
1028 &handle_resolve_timeout,
1030 al->resolve_handle =
1031 GNUNET_DNSSTUB_resolve (dnsstub_ctx,
1034 &handle_resolve_result,
1036 GNUNET_free (packet_buf);
1037 GNUNET_CONTAINER_DLL_insert (lookup_head,
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1041 "Resolving %s, client_request_id = %u, dns_id = %u\n",
1043 (unsigned int) client_request_id,
1044 (unsigned int) dns_id);
1050 * Process DNS request for @a hostname with request ID @a client_request_id
1051 * from @a client demanding records of type @a record_type.
1053 * @param hostname DNS name to resolve
1054 * @param record_type desired record type
1055 * @param client_request_id client's request ID
1056 * @param client who should get the result?
1059 process_get (const char *hostname,
1060 uint16_t record_type,
1061 uint32_t client_request_id,
1062 struct GNUNET_SERVICE_Client *client)
1066 if ( (NULL != my_domain) &&
1067 (NULL == strchr (hostname,
1068 (unsigned char) '.')) &&
1069 (strlen (hostname) + strlen (my_domain) <= 253) )
1071 GNUNET_snprintf (fqdn,
1077 else if (strlen (hostname) < 255)
1079 GNUNET_snprintf (fqdn,
1087 GNUNET_SERVICE_client_drop (client);
1097 resolve_and_cache (fqdn,
1102 send_end_msg (client_request_id,
1110 * Verify well-formedness of GET-message.
1112 * @param cls closure, unused
1113 * @param get the actual message
1114 * @return #GNUNET_OK if @a get is well-formed
1117 check_get (void *cls,
1118 const struct GNUNET_RESOLVER_GetMessage *get)
1125 size = ntohs (get->header.size) - sizeof (*get);
1126 direction = ntohl (get->direction);
1127 if (GNUNET_NO == direction)
1129 /* IP from hostname */
1130 const char *hostname;
1132 hostname = (const char *) &get[1];
1133 if (hostname[size - 1] != '\0')
1136 return GNUNET_SYSERR;
1140 af = ntohl (get->af);
1144 if (size != sizeof (struct in_addr))
1147 return GNUNET_SYSERR;
1151 if (size != sizeof (struct in6_addr))
1154 return GNUNET_SYSERR;
1159 return GNUNET_SYSERR;
1166 * Handle GET-message.
1168 * @param cls identification of the client
1169 * @param msg the actual message
1172 handle_get (void *cls,
1173 const struct GNUNET_RESOLVER_GetMessage *msg)
1175 struct GNUNET_SERVICE_Client *client = cls;
1178 uint32_t client_request_id;
1181 direction = ntohl (msg->direction);
1182 af = ntohl (msg->af);
1183 client_request_id = msg->client_id;
1184 GNUNET_SERVICE_client_continue (client);
1185 if (GNUNET_NO == direction)
1187 /* IP from hostname */
1188 hostname = GNUNET_strdup ((const char *) &msg[1]);
1193 process_get (hostname,
1194 GNUNET_DNSPARSER_TYPE_ALL,
1201 process_get (hostname,
1202 GNUNET_DNSPARSER_TYPE_A,
1209 process_get (hostname,
1210 GNUNET_DNSPARSER_TYPE_AAAA,
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1218 "got invalid af: %d\n",
1226 /* hostname from IP */
1227 hostname = make_reverse_hostname (&msg[1],
1229 process_get (hostname,
1230 GNUNET_DNSPARSER_TYPE_PTR,
1234 GNUNET_free_non_null (hostname);
1239 * Service is shutting down, clean up.
1241 * @param cls NULL, unused
1244 shutdown_task (void *cls)
1248 while (NULL != lookup_head)
1249 free_active_lookup (lookup_head);
1250 while (NULL != cache_head)
1251 free_cache_entry (cache_head);
1252 while (NULL != hosts_head)
1253 free_hosts_entry (hosts_head);
1254 GNUNET_DNSSTUB_stop (dnsstub_ctx);
1255 GNUNET_free (my_domain);
1260 * Add information about a host from /etc/hosts
1263 * @param hostname the name of the host
1264 * @param rec_type DNS record type to use
1265 * @param data payload
1266 * @param data_size number of bytes in @a data
1269 add_host (const char *hostname,
1274 struct ResolveCache *rc;
1275 struct RecordListEntry *rle;
1276 struct GNUNET_DNSPARSER_Record *rec;
1278 rec = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_Record));
1279 rec->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS;
1280 rec->type = rec_type;
1281 rec->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1282 rec->name = GNUNET_strdup (hostname);
1283 rec->data.raw.data = GNUNET_memdup (data,
1285 rec->data.raw.data_len = data_size;
1286 rle = GNUNET_new (struct RecordListEntry);
1288 rc = GNUNET_new (struct ResolveCache);
1289 rc->hostname = GNUNET_strdup (hostname);
1290 GNUNET_CONTAINER_DLL_insert (rc->records_head,
1293 GNUNET_CONTAINER_DLL_insert (hosts_head,
1300 * Extract host information from a line in /etc/hosts
1302 * @param line the line to parse
1303 * @param line_len number of bytes in @a line
1306 extract_hosts (const char *line,
1315 /* ignore everything after '#' */
1317 (unsigned char) '#',
1320 line_len = c - line;
1321 /* ignore leading whitespace */
1322 while ( (0 < line_len) &&
1323 isspace ((unsigned char) *line) )
1328 tbuf = GNUNET_strndup (line,
1330 tok = strtok (tbuf, " \t");
1336 if (1 == inet_pton (AF_INET,
1340 while (NULL != (tok = strtok (NULL, " \t")))
1342 GNUNET_DNSPARSER_TYPE_A,
1344 sizeof (struct in_addr));
1346 else if (1 == inet_pton (AF_INET6,
1350 while (NULL != (tok = strtok (NULL, " \t")))
1352 GNUNET_DNSPARSER_TYPE_AAAA,
1354 sizeof (struct in6_addr));
1361 * Reads the list of hosts from /etc/hosts.
1364 load_etc_hosts (void)
1366 struct GNUNET_DISK_FileHandle *fh;
1367 struct GNUNET_DISK_MapHandle *mh;
1372 fh = GNUNET_DISK_file_open ("/etc/hosts",
1373 GNUNET_DISK_OPEN_READ,
1374 GNUNET_DISK_PERM_NONE);
1377 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1378 "Failed to open /etc/hosts");
1382 GNUNET_DISK_file_handle_size (fh,
1385 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1386 "Could not determin size of /etc/hosts. "
1387 "DNS resolution will not be possible.\n");
1388 GNUNET_DISK_file_close (fh);
1391 if ((size_t) bytes_read > SIZE_MAX)
1393 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1394 "/etc/hosts file too large to mmap. "
1395 "DNS resolution will not be possible.\n");
1396 GNUNET_DISK_file_close (fh);
1399 buf = GNUNET_DISK_file_map (fh,
1401 GNUNET_DISK_MAP_TYPE_READ,
1402 (size_t) bytes_read);
1404 while (read_offset < (size_t) bytes_read)
1406 const char *newline;
1409 newline = strchr (buf + read_offset,
1411 if (NULL == newline)
1413 line_len = newline - buf - read_offset;
1414 extract_hosts (buf + read_offset,
1416 read_offset += line_len + 1;
1418 GNUNET_DISK_file_unmap (mh);
1419 GNUNET_DISK_file_close (fh);
1424 * Service is starting, initialize everything.
1426 * @param cls NULL, unused
1427 * @param cfg our configuration
1428 * @param sh service handle
1432 const struct GNUNET_CONFIGURATION_Handle *cfg,
1433 struct GNUNET_SERVICE_Handle *sh)
1436 int num_dns_servers;
1441 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1443 dnsstub_ctx = GNUNET_DNSSTUB_start (128);
1445 num_dns_servers = lookup_dns_servers (&dns_servers);
1446 if (0 >= num_dns_servers)
1448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1449 _("No DNS server available. DNS resolution will not be possible.\n"));
1452 for (int i = 0; i < num_dns_servers; i++)
1454 int result = GNUNET_DNSSTUB_add_dns_ip (dnsstub_ctx, dns_servers[i]);
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "Adding DNS server '%s': %s\n",
1458 GNUNET_OK == result ? "success" : "failure");
1459 GNUNET_free (dns_servers[i]);
1461 GNUNET_free_non_null (dns_servers);
1466 * Callback called when a client connects to the service.
1468 * @param cls closure for the service, unused
1469 * @param c the new client that connected to the service
1470 * @param mq the message queue used to send messages to the client
1474 connect_cb (void *cls,
1475 struct GNUNET_SERVICE_Client *c,
1476 struct GNUNET_MQ_Handle *mq)
1486 * Callback called when a client disconnected from the service
1488 * @param cls closure for the service
1489 * @param c the client that disconnected
1490 * @param internal_cls should be equal to @a c
1493 disconnect_cb (void *cls,
1494 struct GNUNET_SERVICE_Client *c,
1497 struct ActiveLookup *n;
1500 GNUNET_assert (c == internal_cls);
1502 for (struct ActiveLookup *al = n;
1507 if (al->client == c)
1508 free_active_lookup (al);
1514 * Define "main" method using service macro.
1518 GNUNET_SERVICE_OPTION_NONE,
1523 GNUNET_MQ_hd_var_size (get,
1524 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
1525 struct GNUNET_RESOLVER_GetMessage,
1527 GNUNET_MQ_handler_end ());
1530 #if defined(LINUX) && defined(__GLIBC__)
1534 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1536 void __attribute__ ((constructor))
1537 GNUNET_RESOLVER_memory_init ()
1539 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1540 mallopt (M_TOP_PAD, 1 * 1024);
1546 /* end of gnunet-service-resolver.c */