2 This file is part of GNUnet.
3 Copyright (C) 2011-2013 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 gns/gnunet-service-gns_resolver.c
23 * @brief GNU Name System resolver logic
24 * @author Martin Schanzenbach
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dnsstub_lib.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_gnsrecord_lib.h"
32 #include "gnunet_namecache_service.h"
33 #include "gnunet_dns_service.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_revocation_service.h"
36 #include "gnunet_dnsparser_lib.h"
37 #include "gnunet_tun_lib.h"
38 #include "gnunet_gns_service.h"
40 #include "gnunet-service-gns.h"
41 #include "gnunet-service-gns_resolver.h"
42 #include "gnunet_vpn_service.h"
46 * Default DHT timeout for lookups.
48 #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60)
51 * Default timeout for DNS lookups.
53 #define DNS_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15)
56 * Default timeout for VPN redirections.
58 #define VPN_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 30)
61 * DHT replication level
63 #define DHT_GNS_REPLICATION_LEVEL 10
66 * How deep do we allow recursions to go before we abort?
68 #define MAX_RECURSION 256
72 * DLL to hold the authority chain we had to pass in the resolution
75 struct AuthorityChain;
79 * Element of a resolution process for looking up the
80 * responsible DNS server hostname in a GNS2DNS recursive
83 struct Gns2DnsPending {
87 struct Gns2DnsPending *next;
92 struct Gns2DnsPending *prev;
95 * Context this activity belongs with.
97 struct AuthorityChain *ac;
100 * Handle for the resolution of the IP part of the
101 * GNS2DNS record. Will return to us the addresses
102 * of the DNS resolver to use.
104 struct GNS_ResolverHandle *rh;
107 * Handle for DNS resolution of the DNS nameserver.
109 struct GNUNET_RESOLVER_RequestHandle *dns_rh;
112 * How many results did we get?
114 unsigned int num_results;
119 * Handle to a currenty pending resolution. On result (positive or
120 * negative) the #GNS_ResultProcessor is called.
122 struct GNS_ResolverHandle;
126 * DLL to hold the authority chain we had to pass in the resolution
129 struct AuthorityChain {
133 struct AuthorityChain *prev;
138 struct AuthorityChain *next;
141 * Resolver handle this entry in the chain belongs to.
143 struct GNS_ResolverHandle *rh;
146 * label/name corresponding to the authority
151 * #GNUNET_YES if the authority was a GNS authority,
152 * #GNUNET_NO if the authority was a DNS authority.
157 * Information about the resolver authority for this label.
161 * The zone of the GNS authority
163 struct GNUNET_CRYPTO_EcdsaPublicKey gns_authority;
167 * Domain of the DNS resolver that is the authority.
168 * (appended to construct the DNS name to resolve;
169 * this is NOT the DNS name of the DNS server!).
171 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
174 * List of resolutions of the 'ip' of the name server that
177 struct Gns2DnsPending *gp_head;
180 * Tail of list of resolutions of the 'ip' of the name server that
183 struct Gns2DnsPending *gp_tail;
186 * Handle to perform DNS lookups with this authority (in GNS2DNS handling).
188 struct GNUNET_DNSSTUB_Context *dns_handle;
191 * Did we succeed in getting an IP address for *any* of the DNS servers listed?
192 * Once we do, we can start with DNS queries.
197 * Did we start the recursive resolution via DNS?
206 * A result we got from DNS.
212 struct DnsResult *next;
217 struct DnsResult *prev;
220 * Binary value stored in the DNS record (appended to this struct)
225 * Expiration time for the DNS record, 0 if we didn't
226 * get anything useful (i.e. 'gethostbyname()' was used).
228 uint64_t expiration_time;
231 * Number of bytes in @e data.
236 * Type of the GNS/DNS record.
238 uint32_t record_type;
243 * Closure for #vpn_allocation_cb.
247 * Which resolution process are we processing.
249 struct GNS_ResolverHandle *rh;
252 * Handle to the VPN request that we were performing.
254 struct GNUNET_VPN_RedirectionRequest *vpn_request;
257 * Number of records serialized in @e rd_data.
259 unsigned int rd_count;
262 * Serialized records.
267 * Number of bytes in @e rd_data.
269 ssize_t rd_data_size;
274 * Handle to a currenty pending resolution. On result (positive or
275 * negative) the #GNS_ResultProcessor is called.
277 struct GNS_ResolverHandle {
281 struct GNS_ResolverHandle *next;
286 struct GNS_ResolverHandle *prev;
289 * The top-level GNS authoritative zone to query
291 struct GNUNET_CRYPTO_EcdsaPublicKey authority_zone;
294 * called when resolution phase finishes
296 GNS_ResultProcessor proc;
299 * closure passed to @e proc
304 * Handle for DHT lookups. should be NULL if no lookups are in progress
306 struct GNUNET_DHT_GetHandle *get_handle;
309 * Handle to a VPN request, NULL if none is active.
311 struct VpnContext *vpn_ctx;
314 * Socket for a DNS request, NULL if none is active.
316 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
319 * Handle for standard DNS resolution, NULL if none is active.
321 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
324 * Pending Namecache lookup task
326 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe;
329 * Pending revocation check.
331 struct GNUNET_REVOCATION_Query *rev_check;
334 * Heap node associated with this lookup. Used to limit number of
335 * concurrent requests.
337 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
340 * DLL to store the authority chain
342 struct AuthorityChain *ac_head;
345 * DLL to store the authority chain
347 struct AuthorityChain *ac_tail;
350 * ID of a task associated with the resolution process.
352 struct GNUNET_SCHEDULER_Task *task_id;
355 * The name to resolve
360 * Legacy Hostname to use if we encountered GNS2DNS record
361 * and thus can deduct the LEHO from that transition.
366 * DLL of results we got from DNS.
368 struct DnsResult *dns_result_head;
371 * DLL of results we got from DNS.
373 struct DnsResult *dns_result_tail;
376 * Current offset in 'name' where we are resolving.
378 size_t name_resolution_pos;
383 enum GNUNET_GNS_LocalOptions options;
386 * For SRV and TLSA records, the number of the
387 * protocol specified in the name. 0 if no protocol was given.
392 * For SRV and TLSA records, the number of the
393 * service specified in the name. 0 if no service was given.
398 * Desired type for the resolution.
403 * We increment the loop limiter for each step in a recursive
404 * resolution. If it passes our threshold (i.e. due to
405 * self-recursion in the resolution, i.e CNAME fun), we stop.
407 unsigned int loop_limiter;
410 * 16 bit random ID we used in the @e dns_request.
412 uint16_t original_dns_id;
417 * Active namestore caching operations.
421 * Organized in a DLL.
423 struct CacheOps *next;
426 * Organized in a DLL.
428 struct CacheOps *prev;
431 * Pending Namestore caching task.
433 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe_cache;
438 * Our handle to the namecache service
440 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
443 * Our handle to the vpn service
445 static struct GNUNET_VPN_Handle *vpn_handle;
448 * Resolver handle to the dht
450 static struct GNUNET_DHT_Handle *dht_handle;
453 * Heap for limiting parallel DHT lookups
455 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
458 * Maximum amount of parallel queries to the DHT
460 static unsigned long long max_allowed_background_queries;
463 * Head of resolver lookup list
465 static struct GNS_ResolverHandle *rlh_head;
468 * Tail of resolver lookup list
470 static struct GNS_ResolverHandle *rlh_tail;
473 * Organized in a DLL.
475 static struct CacheOps *co_head;
478 * Organized in a DLL.
480 static struct CacheOps *co_tail;
485 static int disable_cache;
488 * Global configuration.
490 static const struct GNUNET_CONFIGURATION_Handle *cfg;
494 * Determine if this name is canonical (is a legal name in a zone, without delegation);
495 * note that we do not test that the name does not contain illegal characters, we only
496 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
497 * even though they consist of multiple labels.
500 * a.b.gnu = not canonical
502 * _foo._srv = canonical
503 * _f.bar = not canonical
505 * @param name the name to test
506 * @return #GNUNET_YES if canonical
508 /* dead, but keep for now */ int
509 is_canonical(const char *name)
514 if (NULL == strchr(name,
520 while (NULL != (dot = strchr(pos,
521 (unsigned char)'.')))
529 /* ************************** Resolution **************************** */
532 * Expands a name ending in .+ with the zone of origin.
534 * @param rh resolution context
535 * @param name name to modify (to be free'd or returned)
536 * @return updated name
539 translate_dot_plus(struct GNS_ResolverHandle *rh,
543 size_t s_len = strlen(name);
545 if (0 != strcmp(&name[s_len - 2],
547 return name; /* did not end in ".+" */
548 GNUNET_assert(GNUNET_YES == rh->ac_tail->gns_authority);
549 GNUNET_asprintf(&ret,
553 GNUNET_GNSRECORD_pkey_to_zkey(&rh->ac_tail->authority_info.gns_authority));
560 * Wrapper around #GNS_resolver_lookup_cancel() as a task.
561 * Used for delayed cleanup so we can unwind the stack first.
563 * @param cls the `struct GNS_ResolverHandle`
566 GNS_resolver_lookup_cancel_(void *cls)
568 struct GNS_ResolverHandle *rh = cls;
571 GNS_resolver_lookup_cancel(rh);
576 * Function called to asynchronously fail a resolution.
578 * @param rh the resolution to fail
581 fail_resolution(struct GNS_ResolverHandle *rh)
583 rh->proc(rh->proc_cls,
586 GNUNET_assert(NULL == rh->task_id);
587 rh->task_id = GNUNET_SCHEDULER_add_now(&GNS_resolver_lookup_cancel_,
593 * Function called when a resolution times out.
595 * @param cls the `struct GNS_ResolverHandle`
598 timeout_resolution(void *cls)
600 struct GNS_ResolverHandle *rh = cls;
608 * Get the next, rightmost label from the name that we are trying to resolve,
609 * and update the resolution position accordingly. Labels usually consist
610 * of up to 63 characters without a period ("."); however, we use a special
611 * convention to support SRV and TLSA records where the domain name
612 * includes an encoding for a service and protocol in the name. The
613 * syntax (see RFC 2782) here is "_Service._Proto.Name" and in this
614 * special case we include the "_Service._Proto" in the rightmost label.
615 * Thus, for "_443._tcp.foo.bar" we first return the label "bar" and then
616 * the label "_443._tcp.foo". The special case is detected by the
617 * presence of labels beginning with an underscore. Whenever a label
618 * begins with an underscore, it is combined with the label to its right
619 * (and the "." is preserved).
621 * @param rh handle to the resolution operation to get the next label from
622 * @return NULL if there are no more labels
625 resolver_lookup_get_next_label(struct GNS_ResolverHandle *rh)
636 if (0 == rh->name_resolution_pos)
638 dot = memrchr(rh->name,
640 rh->name_resolution_pos);
643 /* done, this was the last one */
644 len = rh->name_resolution_pos;
646 rh->name_resolution_pos = 0;
650 /* advance by one label */
651 len = rh->name_resolution_pos - (dot - rh->name) - 1;
653 rh->name_resolution_pos = dot - rh->name;
657 ret = GNUNET_strndup(rp, len);
658 /* If we have labels starting with underscore with label on
659 * the right (SRV/DANE/BOX case), determine port/protocol;
660 * The format of `rh->name` must be "_PORT._PROTOCOL".
662 if (('_' == rh->name[0]) &&
663 (NULL != (dot = memrchr(rh->name,
665 rh->name_resolution_pos))) &&
667 (NULL == memrchr(rh->name,
671 srv_name = GNUNET_strndup(&rh->name[1],
672 (dot - rh->name) - 1);
673 proto_name = GNUNET_strndup(&dot[2],
674 rh->name_resolution_pos - (dot - rh->name) - 1);
675 rh->name_resolution_pos = 0;
676 pe = getprotobyname(proto_name);
679 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
680 _("Protocol `%s' unknown, skipping labels.\n"),
682 GNUNET_free(proto_name);
683 GNUNET_free(srv_name);
686 se = getservbyname(srv_name,
690 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
691 _("Service `%s' unknown for protocol `%s', skipping labels.\n"),
694 GNUNET_free(proto_name);
695 GNUNET_free(srv_name);
698 rh->protocol = pe->p_proto;
699 rh->service = se->s_port;
700 GNUNET_free(proto_name);
701 GNUNET_free(srv_name);
708 * Gives the cummulative result obtained to the callback and clean up the request.
710 * @param rh resolution process that has culminated in a result
713 transmit_lookup_dns_result(struct GNS_ResolverHandle *rh)
715 struct DnsResult *pos;
720 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
723 struct GNUNET_GNSRECORD_Data rd[n];
726 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
728 rd[i].data = pos->data;
729 rd[i].data_size = pos->data_size;
730 rd[i].record_type = pos->record_type;
731 if (0 == pos->expiration_time)
733 rd[i].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
734 rd[i].expiration_time = 0;
738 rd[i].flags = GNUNET_GNSRECORD_RF_NONE;
739 rd[i].expiration_time = pos->expiration_time;
743 GNUNET_assert(i == n);
744 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
745 "Transmitting standard DNS result with %u records\n",
747 rh->proc(rh->proc_cls,
751 GNS_resolver_lookup_cancel(rh);
756 * Add a result from DNS to the records to be returned to the application.
758 * @param rh resolution request to extend with a result
759 * @param expiration_time expiration time for the answer
760 * @param record_type DNS record type of the answer
761 * @param data_size number of bytes in @a data
762 * @param data binary data to return in DNS record
765 add_dns_result(struct GNS_ResolverHandle *rh,
766 uint64_t expiration_time,
767 uint32_t record_type,
771 struct DnsResult *res;
773 res = GNUNET_malloc(sizeof(struct DnsResult) + data_size);
774 res->expiration_time = expiration_time;
775 res->data_size = data_size;
776 res->record_type = record_type;
778 GNUNET_memcpy(&res[1],
781 GNUNET_CONTAINER_DLL_insert(rh->dns_result_head,
788 * We had to do a DNS lookup. Convert the result (if any) and return
791 * @param cls closure with the `struct GNS_ResolverHandle`
792 * @param addr one of the addresses of the host, NULL for the last address
793 * @param addrlen length of the address
796 handle_dns_result(void *cls,
797 const struct sockaddr *addr,
800 struct GNS_ResolverHandle *rh = cls;
801 const struct sockaddr_in *sa4;
802 const struct sockaddr_in6 *sa6;
806 rh->std_resolve = NULL;
807 transmit_lookup_dns_result(rh);
810 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
811 "Received %u bytes of DNS IP data\n",
813 switch (addr->sa_family)
816 sa4 = (const struct sockaddr_in *)addr;
818 0 /* expiration time is unknown */,
819 GNUNET_DNSPARSER_TYPE_A,
820 sizeof(struct in_addr),
825 sa6 = (const struct sockaddr_in6 *)addr;
827 0 /* expiration time is unknown */,
828 GNUNET_DNSPARSER_TYPE_AAAA,
829 sizeof(struct in6_addr),
841 * Task scheduled to continue with the resolution process.
843 * @param cls the 'struct GNS_ResolverHandle' of the resolution
844 * @param tc task context
847 recursive_resolution(void *cls);
851 * Begin the resolution process from 'name', starting with
852 * the identification of the zone specified by 'name'.
854 * @param cls closure with `struct GNS_ResolverHandle *rh`
857 start_resolver_lookup(void *cls);
861 * Function called with the result of a DNS resolution.
863 * @param cls the request handle of the resolution that
864 * we were attempting to make
865 * @param dns dns response, never NULL
866 * @param dns_len number of bytes in @a dns
869 dns_result_parser(void *cls,
870 const struct GNUNET_TUN_DnsHeader *dns,
873 struct GNS_ResolverHandle *rh = cls;
874 struct GNUNET_DNSPARSER_Packet *p;
875 const struct GNUNET_DNSPARSER_Record *rec;
876 unsigned int rd_count;
880 rh->dns_request = NULL;
881 GNUNET_SCHEDULER_cancel(rh->task_id);
886 if (rh->original_dns_id != dns->id)
888 /* DNS answer, but for another query */
891 p = GNUNET_DNSPARSER_parse((const char *)dns,
895 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
896 _("Failed to parse DNS response\n"));
900 /* We got a result from DNS */
901 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
902 "Received DNS response for `%s' with %u answers\n",
904 (unsigned int)p->num_answers);
905 if ((p->num_answers > 0) &&
906 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
907 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type))
911 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
912 "Got CNAME `%s' from DNS for `%s'\n",
913 p->answers[0].data.hostname,
915 if (NULL != rh->std_resolve)
917 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
918 "Multiple CNAME results from DNS resolving `%s'! Not really allowed...\n",
920 GNUNET_RESOLVER_request_cancel(rh->std_resolve);
922 GNUNET_free(rh->name);
923 rh->name = GNUNET_strdup(p->answers[0].data.hostname);
924 rh->name_resolution_pos = strlen(rh->name);
925 switch (rh->record_type)
927 case GNUNET_DNSPARSER_TYPE_A:
931 case GNUNET_DNSPARSER_TYPE_AAAA:
939 if (NULL != rh->leho)
941 GNUNET_TIME_UNIT_HOURS.rel_value_us,
942 GNUNET_GNSRECORD_TYPE_LEHO,
945 rh->std_resolve = GNUNET_RESOLVER_ip_get(rh->name,
950 GNUNET_DNSPARSER_free_packet(p);
951 GNUNET_DNSSTUB_resolve_cancel(rh->dns_request);
952 rh->dns_request = NULL;
956 /* convert from (parsed) DNS to (binary) GNS format! */
957 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
959 struct GNUNET_GNSRECORD_Data rd[rd_count + 1]; /* +1 for LEHO */
961 char buf[UINT16_MAX];
970 for (unsigned int i = 0; i < rd_count; i++)
972 if (i < p->num_answers)
973 rec = &p->answers[i];
974 else if (i < p->num_answers + p->num_authority_records)
975 rec = &p->authority_records[i - p->num_answers];
977 rec = &p->additional_records[i - p->num_answers - p->num_authority_records];
978 /* As we copied the full DNS name to 'rh->ac_tail->label', this
979 should be the correct check to see if this record is actually
980 a record for our label... */
981 if (0 != strcmp(rec->name,
984 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
985 "Dropping record `%s', does not match desired name `%s'\n",
991 rd[i - skip].record_type = rec->type;
992 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
995 case GNUNET_DNSPARSER_TYPE_A:
996 if (rec->data.raw.data_len != sizeof(struct in_addr))
1002 rd[i - skip].data_size = rec->data.raw.data_len;
1003 rd[i - skip].data = rec->data.raw.data;
1006 case GNUNET_DNSPARSER_TYPE_AAAA:
1007 if (rec->data.raw.data_len != sizeof(struct in6_addr))
1013 rd[i - skip].data_size = rec->data.raw.data_len;
1014 rd[i - skip].data = rec->data.raw.data;
1017 case GNUNET_DNSPARSER_TYPE_CNAME:
1018 case GNUNET_DNSPARSER_TYPE_PTR:
1019 case GNUNET_DNSPARSER_TYPE_NS:
1020 buf_start = buf_off;
1022 GNUNET_DNSPARSER_builder_add_name(buf,
1025 rec->data.hostname))
1031 rd[i - skip].data_size = buf_off - buf_start;
1032 rd[i - skip].data = &buf[buf_start];
1035 case GNUNET_DNSPARSER_TYPE_SOA:
1036 buf_start = buf_off;
1038 GNUNET_DNSPARSER_builder_add_soa(buf,
1047 rd[i - skip].data_size = buf_off - buf_start;
1048 rd[i - skip].data = &buf[buf_start];
1051 case GNUNET_DNSPARSER_TYPE_MX:
1052 buf_start = buf_off;
1054 GNUNET_DNSPARSER_builder_add_mx(buf,
1063 rd[i - skip].data_size = buf_off - buf_start;
1064 rd[i - skip].data = &buf[buf_start];
1067 case GNUNET_DNSPARSER_TYPE_SRV:
1068 buf_start = buf_off;
1070 GNUNET_DNSPARSER_builder_add_srv(buf,
1079 rd[i - skip].data_size = buf_off - buf_start;
1080 rd[i - skip].data = &buf[buf_start];
1084 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1085 _("Skipping record of unsupported type %d\n"),
1090 } /* end of for all records in answer */
1091 if (NULL != rh->leho)
1093 rd[rd_count - skip].record_type = GNUNET_GNSRECORD_TYPE_LEHO;
1094 rd[rd_count - skip].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1095 rd[rd_count - skip].expiration_time = GNUNET_TIME_UNIT_HOURS.rel_value_us;
1096 rd[rd_count - skip].data = rh->leho;
1097 rd[rd_count - skip].data_size = strlen(rh->leho);
1098 skip--; /* skip one LESS */
1099 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1103 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1104 "Returning DNS response for `%s' with %u answers\n",
1106 (unsigned int)(rd_count - skip));
1107 rh->proc(rh->proc_cls,
1110 GNUNET_DNSSTUB_resolve_cancel(rh->dns_request);
1111 rh->dns_request = NULL;
1113 GNUNET_DNSPARSER_free_packet(p);
1114 if (NULL != rh->task_id)
1115 GNUNET_SCHEDULER_cancel(rh->task_id); /* should be timeout task */
1116 rh->task_id = GNUNET_SCHEDULER_add_now(&GNS_resolver_lookup_cancel_,
1122 * Perform recursive DNS resolution. Asks the given DNS resolver to
1123 * resolve "rh->dns_name", possibly recursively proceeding following
1124 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
1125 * we find the answer.
1127 * @param rh resolution information
1130 recursive_dns_resolution(struct GNS_ResolverHandle *rh)
1132 struct AuthorityChain *ac;
1133 struct GNUNET_DNSPARSER_Query *query;
1134 struct GNUNET_DNSPARSER_Packet *p;
1136 size_t dns_request_length;
1140 GNUNET_assert(NULL != ac);
1141 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1142 "Starting DNS lookup for `%s'\n",
1144 GNUNET_assert(GNUNET_NO == ac->gns_authority);
1145 query = GNUNET_new(struct GNUNET_DNSPARSER_Query);
1146 query->name = GNUNET_strdup(ac->label);
1147 query->type = rh->record_type;
1148 query->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1149 p = GNUNET_new(struct GNUNET_DNSPARSER_Packet);
1152 p->id = (uint16_t)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE,
1154 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
1155 p->flags.recursion_desired = 1;
1156 ret = GNUNET_DNSPARSER_pack(p,
1159 &dns_request_length);
1160 if (GNUNET_OK != ret)
1163 rh->proc(rh->proc_cls,
1166 GNUNET_assert(NULL == rh->task_id);
1167 rh->task_id = GNUNET_SCHEDULER_add_now(&GNS_resolver_lookup_cancel_,
1172 rh->original_dns_id = p->id;
1173 GNUNET_assert(NULL != ac->authority_info.dns_authority.dns_handle);
1174 GNUNET_assert(NULL == rh->dns_request);
1175 rh->leho = GNUNET_strdup(ac->label);
1176 rh->dns_request = GNUNET_DNSSTUB_resolve(ac->authority_info.dns_authority.dns_handle,
1181 rh->task_id = GNUNET_SCHEDULER_add_delayed(DNS_LOOKUP_TIMEOUT,
1182 &timeout_resolution,
1185 if (GNUNET_SYSERR != ret)
1186 GNUNET_free(dns_request);
1187 GNUNET_DNSPARSER_free_packet(p);
1192 * We encountered a CNAME record during our resolution.
1193 * Merge it into our chain.
1195 * @param rh resolution we are performing
1196 * @param cname value of the cname record we got for the current
1197 * authority chain tail
1200 handle_gns_cname_result(struct GNS_ResolverHandle *rh,
1205 struct AuthorityChain *ac;
1208 nlen = strlen(cname);
1213 /* CNAME resolution continues relative to current domain */
1214 if (0 == rh->name_resolution_pos)
1216 res = GNUNET_strndup(cname, nlen - 2);
1217 rh->name_resolution_pos = nlen - 2;
1221 GNUNET_asprintf(&res,
1223 (int)rh->name_resolution_pos,
1227 rh->name_resolution_pos = strlen(res);
1229 GNUNET_free(rh->name);
1231 ac = GNUNET_new(struct AuthorityChain);
1233 ac->gns_authority = GNUNET_YES;
1234 ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
1235 ac->label = resolver_lookup_get_next_label(rh);
1236 /* add AC to tail */
1237 GNUNET_CONTAINER_DLL_insert_tail(rh->ac_head,
1240 rh->task_id = GNUNET_SCHEDULER_add_now(&recursive_resolution,
1244 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1245 "Got CNAME `%s' from GNS for `%s'\n",
1248 if (NULL != rh->std_resolve)
1250 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1251 "Multiple CNAME results from GNS resolving `%s'! Not really allowed...\n",
1253 GNUNET_RESOLVER_request_cancel(rh->std_resolve);
1255 /* name is absolute, go to DNS */
1256 GNUNET_free(rh->name);
1257 rh->name = GNUNET_strdup(cname);
1258 rh->name_resolution_pos = strlen(rh->name);
1259 switch (rh->record_type)
1261 case GNUNET_DNSPARSER_TYPE_A:
1265 case GNUNET_DNSPARSER_TYPE_AAAA:
1273 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1274 "Doing standard DNS lookup for `%s'\n",
1276 rh->std_resolve = GNUNET_RESOLVER_ip_get(rh->name,
1285 * Process a records that were decrypted from a block.
1287 * @param cls closure with the 'struct GNS_ResolverHandle'
1288 * @param rd_count number of entries in @a rd array
1289 * @param rd array of records with data to store
1292 handle_gns_resolution_result(void *cls,
1293 unsigned int rd_count,
1294 const struct GNUNET_GNSRECORD_Data *rd);
1298 * Callback invoked from the VPN service once a redirection is
1299 * available. Provides the IP address that can now be used to
1300 * reach the requested destination. Replaces the "VPN" record
1301 * with the respective A/AAAA record and continues processing.
1303 * @param cls closure
1304 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1305 * will match 'result_af' from the request
1306 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1307 * that the VPN allocated for the redirection;
1308 * traffic to this IP will now be redirected to the
1309 * specified target peer; NULL on error
1312 vpn_allocation_cb(void *cls,
1314 const void *address)
1316 struct VpnContext *vpn_ctx = cls;
1317 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1318 struct GNUNET_GNSRECORD_Data rd[vpn_ctx->rd_count];
1321 vpn_ctx->vpn_request = NULL;
1323 GNUNET_assert(GNUNET_OK ==
1324 GNUNET_GNSRECORD_records_deserialize((size_t)vpn_ctx->rd_data_size,
1328 for (i = 0; i < vpn_ctx->rd_count; i++)
1330 if (GNUNET_GNSRECORD_TYPE_VPN == rd[i].record_type)
1335 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1336 rd[i].data_size = sizeof(struct in_addr);
1337 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute(VPN_TIMEOUT).abs_value_us;
1339 rd[i].data = address;
1343 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1344 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute(VPN_TIMEOUT).abs_value_us;
1346 rd[i].data = address;
1347 rd[i].data_size = sizeof(struct in6_addr);
1356 GNUNET_assert(i < vpn_ctx->rd_count);
1357 if (0 == vpn_ctx->rd_count)
1358 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1359 _("VPN returned empty result for `%s'\n"),
1361 handle_gns_resolution_result(rh,
1364 GNUNET_free(vpn_ctx->rd_data);
1365 GNUNET_free(vpn_ctx);
1370 * We have resolved one or more of the nameservers for a
1371 * GNS2DNS lookup. Once we have some of them, begin using
1372 * the DNSSTUB resolver.
1374 * @param ac context for GNS2DNS resolution
1377 continue_with_gns2dns(struct AuthorityChain *ac)
1379 struct GNS_ResolverHandle *rh = ac->rh;
1381 if ((NULL != ac->authority_info.dns_authority.gp_head) &&
1382 (GNUNET_NO == ac->authority_info.dns_authority.found))
1383 return; /* more pending and none found yet */
1384 if (GNUNET_NO == ac->authority_info.dns_authority.found)
1386 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1387 "Failed to resolve DNS server for `%s' in GNS2DNS resolution\n",
1388 ac->authority_info.dns_authority.name);
1389 fail_resolution(rh);
1392 if (GNUNET_NO != ac->authority_info.dns_authority.launched)
1393 return; /* already running, do not launch again! */
1395 ac->authority_info.dns_authority.launched = GNUNET_YES;
1396 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1397 "Will continue resolution using DNS to resolve `%s'\n",
1399 GNUNET_assert(NULL == rh->task_id);
1400 rh->task_id = GNUNET_SCHEDULER_add_now(&recursive_resolution,
1406 * We've resolved the IP address for the DNS resolver to use
1407 * after encountering a GNS2DNS record.
1409 * @param cls the `struct Gns2DnsPending` used for this request
1410 * @param rd_count number of records in @a rd
1411 * @param rd addresses for the DNS resolver (presumably)
1414 handle_gns2dns_result(void *cls,
1415 unsigned int rd_count,
1416 const struct GNUNET_GNSRECORD_Data *rd)
1418 struct Gns2DnsPending *gp = cls;
1419 struct AuthorityChain *ac = gp->ac;
1421 GNUNET_CONTAINER_DLL_remove(ac->authority_info.dns_authority.gp_head,
1422 ac->authority_info.dns_authority.gp_tail,
1424 /* enable cleanup of 'rh' handle that automatically comes after we return,
1425 and which expects 'rh' to be in the #rlh_head DLL. */
1428 GNUNET_CONTAINER_DLL_insert(rlh_head,
1434 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1435 "Received %u results for IP address of DNS server for GNS2DNS transition\n",
1437 /* find suitable A/AAAA record */
1438 for (unsigned int j = 0; j < rd_count; j++)
1440 switch (rd[j].record_type)
1442 case GNUNET_DNSPARSER_TYPE_A:
1444 struct sockaddr_in v4;
1446 if (sizeof(struct in_addr) != rd[j].data_size)
1454 v4.sin_family = AF_INET;
1455 v4.sin_port = htons(53);
1456 #if HAVE_SOCKADDR_IN_SIN_LEN
1457 v4.sin_len = (u_char)sizeof(v4);
1459 GNUNET_memcpy(&v4.sin_addr,
1461 sizeof(struct in_addr));
1463 GNUNET_DNSSTUB_add_dns_sa(ac->authority_info.dns_authority.dns_handle,
1464 (const struct sockaddr *)&v4))
1465 ac->authority_info.dns_authority.found = GNUNET_YES;
1469 case GNUNET_DNSPARSER_TYPE_AAAA:
1471 struct sockaddr_in6 v6;
1473 if (sizeof(struct in6_addr) != rd[j].data_size)
1478 /* FIXME: might want to check if we support IPv6 here,
1479 and otherwise skip this one and hope we find another */
1483 v6.sin6_family = AF_INET6;
1484 v6.sin6_port = htons(53);
1485 #if HAVE_SOCKADDR_IN_SIN_LEN
1486 v6.sin6_len = (u_char)sizeof(v6);
1488 GNUNET_memcpy(&v6.sin6_addr,
1490 sizeof(struct in6_addr));
1492 GNUNET_DNSSTUB_add_dns_sa(ac->authority_info.dns_authority.dns_handle,
1493 (const struct sockaddr *)&v6))
1494 ac->authority_info.dns_authority.found = GNUNET_YES;
1502 continue_with_gns2dns(ac);
1507 * Function called by the resolver for each address obtained from DNS.
1509 * @param cls closure, a `struct Gns2DnsPending *`
1510 * @param addr one of the addresses of the host, NULL for the last address
1511 * @param addrlen length of @a addr
1514 handle_gns2dns_ip(void *cls,
1515 const struct sockaddr *addr,
1518 struct Gns2DnsPending *gp = cls;
1519 struct AuthorityChain *ac = gp->ac;
1520 struct sockaddr_storage ss;
1521 struct sockaddr_in *v4;
1522 struct sockaddr_in6 *v6;
1526 /* DNS resolution finished */
1527 if (0 == gp->num_results)
1528 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1529 "Failed to use DNS to resolve name of DNS resolver\n");
1530 GNUNET_CONTAINER_DLL_remove(ac->authority_info.dns_authority.gp_head,
1531 ac->authority_info.dns_authority.gp_tail,
1534 continue_with_gns2dns(ac);
1540 switch (ss.ss_family)
1543 v4 = (struct sockaddr_in *)&ss;
1544 v4->sin_port = htons(53);
1549 v6 = (struct sockaddr_in6 *)&ss;
1550 v6->sin6_port = htons(53);
1555 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1556 "Unsupported AF %d\n",
1561 GNUNET_DNSSTUB_add_dns_sa(ac->authority_info.dns_authority.dns_handle,
1562 (struct sockaddr *)&ss))
1563 ac->authority_info.dns_authority.found = GNUNET_YES;
1568 * We found a CNAME record, perform recursive resolution on it.
1570 * @param rh resolution handle
1571 * @param rd record with CNAME to resolve recursively
1574 recursive_cname_resolution(struct GNS_ResolverHandle *rh,
1575 const struct GNUNET_GNSRECORD_Data *rd)
1581 cname = GNUNET_DNSPARSER_parse_name(rd->data,
1584 if ((NULL == cname) ||
1585 (off != rd->data_size))
1587 GNUNET_break_op(0); /* record not well-formed */
1588 GNUNET_free_non_null(cname);
1589 fail_resolution(rh);
1592 handle_gns_cname_result(rh,
1599 * We found a PKEY record, perform recursive resolution on it.
1601 * @param rh resolution handle
1602 * @param rd record with PKEY to resolve recursively
1605 recursive_pkey_resolution(struct GNS_ResolverHandle *rh,
1606 const struct GNUNET_GNSRECORD_Data *rd)
1608 struct AuthorityChain *ac;
1610 /* delegation to another zone */
1611 if (sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) !=
1615 fail_resolution(rh);
1618 /* expand authority chain */
1619 ac = GNUNET_new(struct AuthorityChain);
1621 ac->gns_authority = GNUNET_YES;
1622 GNUNET_memcpy(&ac->authority_info.gns_authority,
1624 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
1625 ac->label = resolver_lookup_get_next_label(rh);
1626 /* add AC to tail */
1627 GNUNET_CONTAINER_DLL_insert_tail(rh->ac_head,
1631 rh->task_id = GNUNET_SCHEDULER_add_now(&recursive_resolution,
1637 * We found one or more GNS2DNS records, perform recursive resolution on it.
1638 * (to be precise, one or more records in @a rd is GNS2DNS, there may be others,
1639 * so this function still needs to check which ones are GNS2DNS).
1641 * @param rh resolution handle
1642 * @param rd_count length of the @a rd array
1643 * @param rd record with PKEY to resolve recursively
1644 * @return #GNUNET_OK if this worked, #GNUNET_SYSERR if no GNS2DNS records were in @a rd
1647 recursive_gns2dns_resolution(struct GNS_ResolverHandle *rh,
1648 unsigned int rd_count,
1649 const struct GNUNET_GNSRECORD_Data *rd)
1651 struct AuthorityChain *ac;
1656 /* expand authority chain */
1657 ac = GNUNET_new(struct AuthorityChain);
1659 ac->authority_info.dns_authority.dns_handle = GNUNET_DNSSTUB_start(4);
1661 for (unsigned int i = 0; i < rd_count; i++)
1666 struct Gns2DnsPending *gp;
1667 struct GNUNET_CRYPTO_EcdsaPublicKey zone;
1668 struct sockaddr_in v4;
1669 struct sockaddr_in6 v6;
1671 if (GNUNET_GNSRECORD_TYPE_GNS2DNS != rd[i].record_type)
1674 n = GNUNET_DNSPARSER_parse_name(rd[i].data,
1677 ip = GNUNET_DNSPARSER_parse_name(rd[i].data,
1682 (off != rd[i].data_size))
1685 GNUNET_free_non_null(n);
1686 GNUNET_free_non_null(ip);
1689 /* resolve 'ip' to determine the IP(s) of the DNS
1690 resolver to use for lookup of 'ns' */
1693 if (0 != strcasecmp(ns,
1696 /* NS values must all be the same for all GNS2DNS records,
1697 anything else leads to insanity */
1710 /* check if 'ip' is already an IPv4/IPv6 address */
1711 if ((1 == inet_pton(AF_INET,
1714 (1 == inet_pton(AF_INET6,
1718 GNUNET_break(GNUNET_OK ==
1719 GNUNET_DNSSTUB_add_dns_ip(ac->authority_info.dns_authority.dns_handle,
1721 ac->authority_info.dns_authority.found = GNUNET_YES;
1725 tld = GNS_get_tld(ip);
1726 if (0 != strcmp(tld,
1729 /* 'ip' is a DNS name */
1730 gp = GNUNET_new(struct Gns2DnsPending);
1732 GNUNET_CONTAINER_DLL_insert(ac->authority_info.dns_authority.gp_head,
1733 ac->authority_info.dns_authority.gp_tail,
1735 gp->dns_rh = GNUNET_RESOLVER_ip_get(ip,
1737 GNUNET_TIME_UNIT_FOREVER_REL,
1743 /* 'ip' should be a GNS name */
1744 gp = GNUNET_new(struct Gns2DnsPending);
1746 GNUNET_CONTAINER_DLL_insert(ac->authority_info.dns_authority.gp_head,
1747 ac->authority_info.dns_authority.gp_tail,
1749 gp->rh = GNUNET_new(struct GNS_ResolverHandle);
1750 ip = translate_dot_plus(rh,
1752 tld = GNS_get_tld(ip);
1754 GNUNET_GNSRECORD_zkey_to_pkey(tld,
1761 gp->rh->authority_zone = zone;
1762 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1763 "Resolving `%s' to determine IP address of DNS server for GNS2DNS transition for `%s'\n",
1767 gp->rh->name_resolution_pos = strlen(ip) - strlen(tld) - 1;
1768 gp->rh->proc = &handle_gns2dns_result;
1769 gp->rh->proc_cls = gp;
1770 gp->rh->record_type = GNUNET_GNSRECORD_TYPE_ANY;
1771 gp->rh->options = GNUNET_GNS_LO_DEFAULT;
1772 gp->rh->loop_limiter = rh->loop_limiter + 1;
1774 = GNUNET_SCHEDULER_add_now(&start_resolver_lookup,
1776 } /* end 'for all records' */
1780 /* not a single GNS2DNS record found */
1782 return GNUNET_SYSERR;
1784 GNUNET_assert(strlen(ns) <= GNUNET_DNSPARSER_MAX_NAME_LENGTH);
1785 strcpy(ac->authority_info.dns_authority.name,
1787 /* for DNS recursion, the label is the full DNS name,
1788 created from the remainder of the GNS name and the
1789 name in the NS record */
1790 GNUNET_asprintf(&ac->label,
1792 (int)rh->name_resolution_pos,
1794 (0 != rh->name_resolution_pos) ? "." : "",
1797 GNUNET_CONTAINER_DLL_insert_tail(rh->ac_head,
1800 if (strlen(ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1802 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1803 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1805 return GNUNET_SYSERR;
1807 continue_with_gns2dns(ac);
1813 * Process a records that were decrypted from a block.
1815 * @param cls closure with the `struct GNS_ResolverHandle`
1816 * @param rd_count number of entries in @a rd array
1817 * @param rd array of records with data to store
1820 handle_gns_resolution_result(void *cls,
1821 unsigned int rd_count,
1822 const struct GNUNET_GNSRECORD_Data *rd)
1824 struct GNS_ResolverHandle *rh = cls;
1826 struct VpnContext *vpn_ctx;
1827 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1829 struct GNUNET_HashCode vhash;
1831 char scratch[UINT16_MAX];
1833 size_t scratch_start;
1835 struct GNUNET_GNSRECORD_Data rd_new[rd_count];
1836 unsigned int rd_off;
1838 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1839 "Resolution succeeded for `%s' in zone %s, got %u records\n",
1841 GNUNET_GNSRECORD_z2s(&rh->ac_tail->authority_info.gns_authority),
1845 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1846 _("GNS lookup failed (zero records found for `%s')\n"),
1848 fail_resolution(rh);
1852 if (0 == rh->name_resolution_pos)
1854 /* top-level match, are we done yet? */
1855 if ((rd_count > 0) &&
1856 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1857 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type))
1860 cname = GNUNET_DNSPARSER_parse_name(rd[0].data,
1863 if ((NULL == cname) ||
1864 (off != rd[0].data_size))
1867 GNUNET_free_non_null(cname);
1868 fail_resolution(rh);
1871 handle_gns_cname_result(rh,
1876 /* If A/AAAA was requested, but we got a VPN
1877 record, we convert it to A/AAAA using GNUnet VPN */
1878 if ((GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1879 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type))
1881 for (unsigned int i = 0; i < rd_count; i++)
1883 switch (rd[i].record_type)
1885 case GNUNET_GNSRECORD_TYPE_VPN:
1887 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1888 if (sizeof(struct GNUNET_TUN_GnsVpnRecord) >
1892 fail_resolution(rh);
1895 vpn = (const struct GNUNET_TUN_GnsVpnRecord *)rd[i].data;
1896 vname = (const char *)&vpn[1];
1897 if ('\0' != vname[rd[i].data_size - 1 - sizeof(struct GNUNET_TUN_GnsVpnRecord)])
1900 fail_resolution(rh);
1903 GNUNET_TUN_service_name_to_hash(vname,
1905 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1906 "Attempting VPN allocation for %s-%s (AF: %d, proto %d)\n",
1907 GNUNET_i2s(&vpn->peer),
1910 (int)ntohs(vpn->proto));
1911 vpn_ctx = GNUNET_new(struct VpnContext);
1912 rh->vpn_ctx = vpn_ctx;
1914 vpn_ctx->rd_data_size = GNUNET_GNSRECORD_records_get_size(rd_count,
1916 if (vpn_ctx->rd_data_size < 0)
1919 GNUNET_free(vpn_ctx);
1920 fail_resolution(rh);
1923 vpn_ctx->rd_data = GNUNET_malloc((size_t)vpn_ctx->rd_data_size);
1924 vpn_ctx->rd_count = rd_count;
1925 GNUNET_assert(vpn_ctx->rd_data_size ==
1926 GNUNET_GNSRECORD_records_serialize(rd_count,
1928 (size_t)vpn_ctx->rd_data_size,
1930 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer(vpn_handle,
1935 GNUNET_TIME_relative_to_absolute(VPN_TIMEOUT),
1941 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1943 /* delegation to DNS */
1944 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1945 "Found GNS2DNS record, delegating to DNS!\n");
1947 recursive_gns2dns_resolution(rh,
1959 } /* end: name_resolution_pos */
1960 /* convert relative names in record values to absolute names,
1961 using 'scratch' array for memory allocations */
1964 for (unsigned int i = 0; i < rd_count; i++)
1966 GNUNET_assert(rd_off <= i);
1967 if ((0 != rh->protocol) &&
1968 (0 != rh->service) &&
1969 (GNUNET_GNSRECORD_TYPE_BOX != rd[i].record_type))
1970 continue; /* we _only_ care about boxed records */
1972 GNUNET_assert(rd_off < rd_count);
1973 rd_new[rd_off] = rd[i];
1974 /* Check if the embedded name(s) end in "+", and if so,
1975 replace the "+" with the zone at "ac_tail", changing the name
1976 to a ".ZONEKEY". The name is allocated on the 'scratch' array,
1977 so we can free it afterwards. */
1978 switch (rd[i].record_type)
1980 case GNUNET_DNSPARSER_TYPE_CNAME:
1985 cname = GNUNET_DNSPARSER_parse_name(rd[i].data,
1988 if ((NULL == cname) ||
1989 (off != rd[i].data_size))
1991 GNUNET_break_op(0); /* record not well-formed */
1995 cname = translate_dot_plus(rh, cname);
1996 GNUNET_break(NULL != cname);
1997 scratch_start = scratch_off;
1999 GNUNET_DNSPARSER_builder_add_name(scratch,
2008 GNUNET_assert(rd_off < rd_count);
2009 rd_new[rd_off].data = &scratch[scratch_start];
2010 rd_new[rd_off].data_size = scratch_off - scratch_start;
2014 GNUNET_free_non_null(cname);
2018 case GNUNET_DNSPARSER_TYPE_SOA:
2020 struct GNUNET_DNSPARSER_SoaRecord *soa;
2023 soa = GNUNET_DNSPARSER_parse_soa(rd[i].data,
2026 if ((NULL == soa) ||
2027 (off != rd[i].data_size))
2029 GNUNET_break_op(0); /* record not well-formed */
2033 soa->mname = translate_dot_plus(rh, soa->mname);
2034 soa->rname = translate_dot_plus(rh, soa->rname);
2035 scratch_start = scratch_off;
2037 GNUNET_DNSPARSER_builder_add_soa(scratch,
2046 GNUNET_assert(rd_off < rd_count);
2047 rd_new[rd_off].data = &scratch[scratch_start];
2048 rd_new[rd_off].data_size = scratch_off - scratch_start;
2053 GNUNET_DNSPARSER_free_soa(soa);
2057 case GNUNET_DNSPARSER_TYPE_MX:
2059 struct GNUNET_DNSPARSER_MxRecord *mx;
2062 mx = GNUNET_DNSPARSER_parse_mx(rd[i].data,
2066 (off != rd[i].data_size))
2068 GNUNET_break_op(0); /* record not well-formed */
2072 mx->mxhost = translate_dot_plus(rh, mx->mxhost);
2073 scratch_start = scratch_off;
2075 GNUNET_DNSPARSER_builder_add_mx(scratch,
2084 GNUNET_assert(rd_off < rd_count);
2085 rd_new[rd_off].data = &scratch[scratch_start];
2086 rd_new[rd_off].data_size = scratch_off - scratch_start;
2091 GNUNET_DNSPARSER_free_mx(mx);
2095 case GNUNET_DNSPARSER_TYPE_SRV:
2097 struct GNUNET_DNSPARSER_SrvRecord *srv;
2100 srv = GNUNET_DNSPARSER_parse_srv(rd[i].data,
2103 if ((NULL == srv) ||
2104 (off != rd[i].data_size))
2106 GNUNET_break_op(0); /* record not well-formed */
2110 srv->target = translate_dot_plus(rh, srv->target);
2111 scratch_start = scratch_off;
2113 GNUNET_DNSPARSER_builder_add_srv(scratch,
2122 GNUNET_assert(rd_off < rd_count);
2123 rd_new[rd_off].data = &scratch[scratch_start];
2124 rd_new[rd_off].data_size = scratch_off - scratch_start;
2129 GNUNET_DNSPARSER_free_srv(srv);
2133 case GNUNET_GNSRECORD_TYPE_NICK:
2138 if ((rd[i].data_size > 0) &&
2139 (nick[rd[i].data_size - 1] != '\0'))
2147 case GNUNET_GNSRECORD_TYPE_PKEY:
2149 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
2151 if (rd[i].data_size != sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey))
2160 if (GNUNET_GNSRECORD_TYPE_PKEY != rh->record_type)
2162 /* try to resolve "@" */
2163 struct AuthorityChain *ac;
2165 ac = GNUNET_new(struct AuthorityChain);
2167 ac->gns_authority = GNUNET_YES;
2168 ac->authority_info.gns_authority = pub;
2169 ac->label = GNUNET_strdup(GNUNET_GNS_EMPTY_LABEL_AT);
2170 GNUNET_CONTAINER_DLL_insert_tail(rh->ac_head,
2173 rh->task_id = GNUNET_SCHEDULER_add_now(&recursive_resolution,
2180 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
2182 /* delegation to DNS */
2183 if (GNUNET_GNSRECORD_TYPE_GNS2DNS == rh->record_type)
2186 break; /* do not follow to DNS, we wanted the GNS2DNS record! */
2188 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2189 "Found GNS2DNS record, delegating to DNS!\n");
2191 recursive_gns2dns_resolution(rh,
2199 case GNUNET_GNSRECORD_TYPE_BOX:
2201 /* unbox SRV/TLSA records if a specific one was requested */
2202 if ((0 != rh->protocol) &&
2203 (0 != rh->service) &&
2204 (rd[i].data_size >= sizeof(struct GNUNET_GNSRECORD_BoxRecord)))
2206 const struct GNUNET_GNSRECORD_BoxRecord *box;
2209 if ((ntohs(box->protocol) == rh->protocol) &&
2210 (ntohs(box->service) == rh->service))
2212 /* Box matches, unbox! */
2213 GNUNET_assert(rd_off < rd_count);
2214 rd_new[rd_off].record_type = ntohl(box->record_type);
2215 rd_new[rd_off].data_size -= sizeof(struct GNUNET_GNSRECORD_BoxRecord);
2216 rd_new[rd_off].data = &box[1];
2222 /* no specific protocol/service specified, preserve all BOX
2223 records (for modern, GNS-enabled applications) */
2233 } /* end: for rd_count */
2235 /* yes, we are done, return result */
2236 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2237 "Returning GNS response for `%s' with %u answers\n",
2240 rh->proc(rh->proc_cls,
2243 rh->task_id = GNUNET_SCHEDULER_add_now(&GNS_resolver_lookup_cancel_,
2248 switch (rd[0].record_type)
2250 case GNUNET_DNSPARSER_TYPE_CNAME:
2251 GNUNET_break_op(1 == rd_count); /* CNAME should be unique */
2252 recursive_cname_resolution(rh,
2256 case GNUNET_GNSRECORD_TYPE_PKEY:
2257 GNUNET_break_op(1 == rd_count); /* PKEY should be unique */
2258 recursive_pkey_resolution(rh,
2264 recursive_gns2dns_resolution(rh,
2271 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2272 _("GNS lookup recursion failed (no delegation record found)\n"));
2273 fail_resolution(rh);
2278 * Function called once the namestore has completed the request for
2281 * @param cls closure with the `struct CacheOps`
2282 * @param success #GNUNET_OK on success
2283 * @param emsg error message
2286 namecache_cache_continuation(void *cls,
2290 struct CacheOps *co = cls;
2292 co->namecache_qe_cache = NULL;
2293 if (GNUNET_OK != success)
2294 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2295 _("Failed to cache GNS resolution: %s\n"),
2297 GNUNET_CONTAINER_DLL_remove(co_head,
2305 * Iterator called on each result obtained for a DHT
2306 * operation that expects a reply
2308 * @param cls closure with the `struct GNS_ResolverHandle`
2309 * @param exp when will this value expire
2310 * @param key key of the result
2311 * @param get_path peers on reply path (or NULL if not recorded)
2312 * [0] = datastore's first neighbor, [length - 1] = local peer
2313 * @param get_path_length number of entries in @a get_path
2314 * @param put_path peers on the PUT path (or NULL if not recorded)
2315 * [0] = origin, [length - 1] = datastore
2316 * @param put_path_length number of entries in @a put_path
2317 * @param type type of the result
2318 * @param size number of bytes in data
2319 * @param data pointer to the result data
2322 handle_dht_response(void *cls,
2323 struct GNUNET_TIME_Absolute exp,
2324 const struct GNUNET_HashCode *key,
2325 const struct GNUNET_PeerIdentity *get_path,
2326 unsigned int get_path_length,
2327 const struct GNUNET_PeerIdentity *put_path,
2328 unsigned int put_path_length,
2329 enum GNUNET_BLOCK_Type type,
2333 struct GNS_ResolverHandle *rh = cls;
2334 struct AuthorityChain *ac = rh->ac_tail;
2335 const struct GNUNET_GNSRECORD_Block *block;
2336 struct CacheOps *co;
2341 (void)get_path_length;
2343 (void)put_path_length;
2345 GNUNET_DHT_get_stop(rh->get_handle);
2346 rh->get_handle = NULL;
2347 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2348 rh->dht_heap_node = NULL;
2349 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2350 "Handling response from the DHT\n");
2351 if (size < sizeof(struct GNUNET_GNSRECORD_Block))
2353 /* how did this pass DHT block validation!? */
2355 fail_resolution(rh);
2360 ntohl(block->purpose.size) +
2361 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) +
2362 sizeof(struct GNUNET_CRYPTO_EcdsaSignature))
2364 /* how did this pass DHT block validation!? */
2366 fail_resolution(rh);
2369 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2370 "Decrypting DHT block of size %u for `%s', expires %s\n",
2371 ntohl(block->purpose.size),
2373 GNUNET_STRINGS_absolute_time_to_string(exp));
2375 GNUNET_GNSRECORD_block_decrypt(block,
2376 &ac->authority_info.gns_authority,
2378 &handle_gns_resolution_result,
2381 GNUNET_break_op(0); /* block was ill-formed */
2382 fail_resolution(rh);
2385 if (0 == GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_ntoh(block->expiration_time)).rel_value_us)
2387 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2388 "Received expired block from the DHT, will not cache it.\n");
2391 if (GNUNET_YES == disable_cache)
2393 /* Cache well-formed blocks */
2394 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2395 "Caching response from the DHT in namecache\n");
2396 co = GNUNET_new(struct CacheOps);
2397 co->namecache_qe_cache = GNUNET_NAMECACHE_block_cache(namecache_handle,
2399 &namecache_cache_continuation,
2401 GNUNET_CONTAINER_DLL_insert(co_head,
2408 * Initiate a DHT query for a set of GNS records.
2410 * @param rh resolution handle
2411 * @param query key to use in the DHT lookup
2414 start_dht_request(struct GNS_ResolverHandle *rh,
2415 const struct GNUNET_HashCode *query)
2417 struct GNS_ResolverHandle *rx;
2419 GNUNET_assert(NULL == rh->get_handle);
2420 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2421 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2423 DHT_GNS_REPLICATION_LEVEL,
2424 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2426 &handle_dht_response, rh);
2427 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert(dht_lookup_heap,
2429 GNUNET_TIME_absolute_get().abs_value_us);
2430 if (GNUNET_CONTAINER_heap_get_size(dht_lookup_heap) > max_allowed_background_queries)
2432 /* fail longest-standing DHT request */
2433 rx = GNUNET_CONTAINER_heap_remove_root(dht_lookup_heap);
2434 rx->dht_heap_node = NULL;
2435 GNUNET_assert(NULL != rx);
2436 fail_resolution(rx);
2442 * Process a records that were decrypted from a block that we got from
2443 * the namecache. Simply calls #handle_gns_resolution_result().
2445 * @param cls closure with the `struct GNS_ResolverHandle`
2446 * @param rd_count number of entries in @a rd array
2447 * @param rd array of records with data to store
2450 handle_gns_namecache_resolution_result(void *cls,
2451 unsigned int rd_count,
2452 const struct GNUNET_GNSRECORD_Data *rd)
2454 struct GNS_ResolverHandle *rh = cls;
2457 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2458 _("GNS namecache returned empty result for `%s'\n"),
2460 handle_gns_resolution_result(rh,
2467 * Process a record that was stored in the namecache.
2469 * @param cls closure with the `struct GNS_ResolverHandle`
2470 * @param block block that was stored in the namecache
2473 handle_namecache_block_response(void *cls,
2474 const struct GNUNET_GNSRECORD_Block *block)
2476 struct GNS_ResolverHandle *rh = cls;
2477 struct AuthorityChain *ac = rh->ac_tail;
2478 const char *label = ac->label;
2479 const struct GNUNET_CRYPTO_EcdsaPublicKey *auth = &ac->authority_info.gns_authority;
2480 struct GNUNET_HashCode query;
2482 GNUNET_assert(NULL != rh->namecache_qe);
2483 rh->namecache_qe = NULL;
2484 if (((GNUNET_GNS_LO_DEFAULT == rh->options) ||
2485 ((GNUNET_GNS_LO_LOCAL_MASTER == rh->options) &&
2486 (ac != rh->ac_head))) &&
2488 (0 == GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_ntoh(block->expiration_time)).rel_value_us)))
2490 /* namecache knows nothing; try DHT lookup */
2491 GNUNET_GNSRECORD_query_from_public_key(auth,
2494 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2495 "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
2497 GNUNET_GNSRECORD_z2s(&ac->authority_info.gns_authority),
2498 GNUNET_h2s(&query));
2499 start_dht_request(rh, &query);
2503 if ((NULL == block) ||
2504 (0 == GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_ntoh(block->expiration_time)).rel_value_us))
2506 /* DHT not permitted and no local result, fail */
2507 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2508 "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
2510 GNUNET_GNSRECORD_z2s(&ac->authority_info.gns_authority));
2511 fail_resolution(rh);
2514 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2515 "Received result from namecache for label `%s'\n",
2519 GNUNET_GNSRECORD_block_decrypt(block,
2522 &handle_gns_namecache_resolution_result,
2525 GNUNET_break_op(0); /* block was ill-formed */
2526 /* try DHT instead */
2527 GNUNET_GNSRECORD_query_from_public_key(auth,
2530 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2531 "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
2533 GNUNET_GNSRECORD_z2s(&ac->authority_info.gns_authority),
2534 GNUNET_h2s(&query));
2535 start_dht_request(rh, &query);
2542 * Lookup tail of our authority chain in the namecache.
2544 * @param rh query we are processing
2547 recursive_gns_resolution_namecache(struct GNS_ResolverHandle *rh)
2549 struct AuthorityChain *ac = rh->ac_tail;
2550 struct GNUNET_HashCode query;
2552 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2553 "Starting GNS resolution for `%s' in zone %s\n",
2555 GNUNET_GNSRECORD_z2s(&ac->authority_info.gns_authority));
2556 GNUNET_GNSRECORD_query_from_public_key(&ac->authority_info.gns_authority,
2559 if (GNUNET_YES != disable_cache)
2562 = GNUNET_NAMECACHE_lookup_block(namecache_handle,
2564 &handle_namecache_block_response,
2566 GNUNET_assert(NULL != rh->namecache_qe);
2570 start_dht_request(rh,
2577 * Function called with the result from a revocation check.
2579 * @param cls the `struct GNS_ResovlerHandle`
2580 * @param is_valid #GNUNET_YES if the zone was not yet revoked
2583 handle_revocation_result(void *cls,
2586 struct GNS_ResolverHandle *rh = cls;
2587 struct AuthorityChain *ac = rh->ac_tail;
2589 rh->rev_check = NULL;
2590 if (GNUNET_YES != is_valid)
2592 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2593 _("Zone %s was revoked, resolution fails\n"),
2594 GNUNET_GNSRECORD_z2s(&ac->authority_info.gns_authority));
2595 fail_resolution(rh);
2598 recursive_gns_resolution_namecache(rh);
2603 * Perform revocation check on tail of our authority chain.
2605 * @param rh query we are processing
2608 recursive_gns_resolution_revocation(struct GNS_ResolverHandle *rh)
2610 struct AuthorityChain *ac = rh->ac_tail;
2612 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2613 "Starting revocation check for zone %s\n",
2614 GNUNET_GNSRECORD_z2s(&ac->authority_info.gns_authority));
2615 rh->rev_check = GNUNET_REVOCATION_query(cfg,
2616 &ac->authority_info.gns_authority,
2617 &handle_revocation_result,
2619 GNUNET_assert(NULL != rh->rev_check);
2624 * Task scheduled to continue with the resolution process.
2626 * @param cls the `struct GNS_ResolverHandle` of the resolution
2629 recursive_resolution(void *cls)
2631 struct GNS_ResolverHandle *rh = cls;
2634 if (MAX_RECURSION < rh->loop_limiter++)
2636 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2637 "Encountered unbounded recursion resolving `%s'\n",
2639 fail_resolution(rh);
2642 if (GNUNET_YES == rh->ac_tail->gns_authority)
2643 recursive_gns_resolution_revocation(rh);
2645 recursive_dns_resolution(rh);
2650 * Begin the resolution process from 'name', starting with
2651 * the identification of the zone specified by 'name'.
2653 * @param cls the `struct GNS_ResolverHandle`
2656 start_resolver_lookup(void *cls)
2658 struct GNS_ResolverHandle *rh = cls;
2659 struct AuthorityChain *ac;
2664 if (1 == inet_pton(AF_INET,
2668 /* name is IPv4 address, pretend it's an A record */
2669 struct GNUNET_GNSRECORD_Data rd;
2672 rd.data_size = sizeof(v4);
2673 rd.expiration_time = UINT64_MAX;
2674 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
2676 rh->proc(rh->proc_cls,
2679 GNUNET_assert(NULL == rh->task_id);
2680 rh->task_id = GNUNET_SCHEDULER_add_now(&GNS_resolver_lookup_cancel_,
2684 if (1 == inet_pton(AF_INET6,
2688 /* name is IPv6 address, pretend it's an AAAA record */
2689 struct GNUNET_GNSRECORD_Data rd;
2692 rd.data_size = sizeof(v6);
2693 rd.expiration_time = UINT64_MAX;
2694 rd.record_type = GNUNET_DNSPARSER_TYPE_AAAA;
2696 rh->proc(rh->proc_cls,
2699 GNUNET_assert(NULL == rh->task_id);
2700 rh->task_id = GNUNET_SCHEDULER_add_now(&GNS_resolver_lookup_cancel_,
2705 ac = GNUNET_new(struct AuthorityChain);
2707 ac->label = resolver_lookup_get_next_label(rh);
2708 if (NULL == ac->label)
2709 /* name was just the "TLD", so we default to label
2710 #GNUNET_GNS_EMPTY_LABEL_AT */
2711 ac->label = GNUNET_strdup(GNUNET_GNS_EMPTY_LABEL_AT);
2712 ac->gns_authority = GNUNET_YES;
2713 ac->authority_info.gns_authority = rh->authority_zone;
2714 GNUNET_CONTAINER_DLL_insert_tail(rh->ac_head,
2717 rh->task_id = GNUNET_SCHEDULER_add_now(&recursive_resolution,
2723 * Lookup of a record in a specific zone calls lookup result processor
2726 * @param zone the zone to perform the lookup in
2727 * @param record_type the record type to look up
2728 * @param name the name to look up
2729 * @param options local options to control local lookup
2730 * @param proc the processor to call on result
2731 * @param proc_cls the closure to pass to @a proc
2732 * @return handle to cancel operation
2734 struct GNS_ResolverHandle *
2735 GNS_resolver_lookup(const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
2736 uint32_t record_type,
2738 enum GNUNET_GNS_LocalOptions options,
2739 GNS_ResultProcessor proc,
2742 struct GNS_ResolverHandle *rh;
2744 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2745 "Starting lookup for `%s'\n",
2747 rh = GNUNET_new(struct GNS_ResolverHandle);
2748 GNUNET_CONTAINER_DLL_insert(rlh_head,
2751 rh->authority_zone = *zone;
2753 rh->proc_cls = proc_cls;
2754 rh->options = options;
2755 rh->record_type = record_type;
2756 rh->name = GNUNET_strdup(name);
2757 rh->name_resolution_pos = strlen(name);
2758 rh->task_id = GNUNET_SCHEDULER_add_now(&start_resolver_lookup,
2765 * Cancel active resolution (i.e. client disconnected).
2767 * @param rh resolution to abort
2770 GNS_resolver_lookup_cancel(struct GNS_ResolverHandle *rh)
2772 struct DnsResult *dr;
2773 struct AuthorityChain *ac;
2774 struct VpnContext *vpn_ctx;
2776 GNUNET_CONTAINER_DLL_remove(rlh_head,
2779 if (NULL != rh->dns_request)
2781 GNUNET_DNSSTUB_resolve_cancel(rh->dns_request);
2782 rh->dns_request = NULL;
2784 while (NULL != (ac = rh->ac_head))
2786 GNUNET_CONTAINER_DLL_remove(rh->ac_head,
2789 if (GNUNET_NO == ac->gns_authority)
2791 struct Gns2DnsPending *gp;
2793 while (NULL != (gp = ac->authority_info.dns_authority.gp_head))
2795 GNUNET_CONTAINER_DLL_remove(ac->authority_info.dns_authority.gp_head,
2796 ac->authority_info.dns_authority.gp_tail,
2800 /* rh->g2dc->rh is NOT in the DLL yet, so to enable us
2801 using GNS_resolver_lookup_cancel here, we need to
2803 GNUNET_CONTAINER_DLL_insert(rlh_head,
2806 GNUNET_assert(NULL == gp->rh->task_id);
2807 gp->rh->task_id = GNUNET_SCHEDULER_add_now(&GNS_resolver_lookup_cancel_,
2811 if (NULL != gp->dns_rh)
2813 GNUNET_RESOLVER_request_cancel(gp->dns_rh);
2818 GNUNET_DNSSTUB_stop(ac->authority_info.dns_authority.dns_handle);
2820 GNUNET_free(ac->label);
2823 if (NULL != rh->task_id)
2825 GNUNET_SCHEDULER_cancel(rh->task_id);
2828 if (NULL != rh->get_handle)
2830 GNUNET_DHT_get_stop(rh->get_handle);
2831 rh->get_handle = NULL;
2833 if (NULL != rh->dht_heap_node)
2835 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2836 rh->dht_heap_node = NULL;
2838 if (NULL != (vpn_ctx = rh->vpn_ctx))
2840 GNUNET_VPN_cancel_request(vpn_ctx->vpn_request);
2841 GNUNET_free(vpn_ctx->rd_data);
2842 GNUNET_free(vpn_ctx);
2844 if (NULL != rh->namecache_qe)
2846 GNUNET_NAMECACHE_cancel(rh->namecache_qe);
2847 rh->namecache_qe = NULL;
2849 if (NULL != rh->rev_check)
2851 GNUNET_REVOCATION_query_cancel(rh->rev_check);
2852 rh->rev_check = NULL;
2854 if (NULL != rh->std_resolve)
2856 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2857 "Canceling standard DNS resolution\n");
2858 GNUNET_RESOLVER_request_cancel(rh->std_resolve);
2859 rh->std_resolve = NULL;
2861 while (NULL != (dr = rh->dns_result_head))
2863 GNUNET_CONTAINER_DLL_remove(rh->dns_result_head,
2864 rh->dns_result_tail,
2868 GNUNET_free_non_null(rh->leho);
2869 GNUNET_free(rh->name);
2874 /* ***************** Resolver initialization ********************* */
2878 * Initialize the resolver
2880 * @param nc the namecache handle
2881 * @param dht the dht handle
2882 * @param c configuration handle
2883 * @param max_bg_queries maximum number of parallel background queries in dht
2886 GNS_resolver_init(struct GNUNET_NAMECACHE_Handle *nc,
2887 struct GNUNET_DHT_Handle *dht,
2888 const struct GNUNET_CONFIGURATION_Handle *c,
2889 unsigned long long max_bg_queries)
2892 namecache_handle = nc;
2895 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
2896 max_allowed_background_queries = max_bg_queries;
2897 disable_cache = GNUNET_CONFIGURATION_get_value_yesno(cfg,
2900 if (GNUNET_YES == disable_cache)
2901 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2902 "Namecache disabled\n");
2903 vpn_handle = GNUNET_VPN_connect(cfg);
2913 struct GNS_ResolverHandle *rh;
2914 struct CacheOps *co;
2916 /* abort active resolutions */
2917 while (NULL != (rh = rlh_head))
2919 rh->proc(rh->proc_cls,
2922 GNS_resolver_lookup_cancel(rh);
2924 while (NULL != (co = co_head))
2926 GNUNET_CONTAINER_DLL_remove(co_head,
2929 GNUNET_NAMECACHE_cancel(co->namecache_qe_cache);
2932 GNUNET_CONTAINER_heap_destroy(dht_lookup_heap);
2933 dht_lookup_heap = NULL;
2934 GNUNET_VPN_disconnect(vpn_handle);
2937 namecache_handle = NULL;
2941 /* end of gnunet-service-gns_resolver.c */