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
89 struct Gns2DnsPending *next;
94 struct Gns2DnsPending *prev;
97 * Context this activity belongs with.
99 struct AuthorityChain *ac;
102 * Handle for the resolution of the IP part of the
103 * GNS2DNS record. Will return to us the addresses
104 * of the DNS resolver to use.
106 struct GNS_ResolverHandle *rh;
109 * Handle for DNS resolution of the DNS nameserver.
111 struct GNUNET_RESOLVER_RequestHandle *dns_rh;
114 * How many results did we get?
116 unsigned int num_results;
121 * Handle to a currenty pending resolution. On result (positive or
122 * negative) the #GNS_ResultProcessor is called.
124 struct GNS_ResolverHandle;
128 * DLL to hold the authority chain we had to pass in the resolution
131 struct AuthorityChain
136 struct AuthorityChain *prev;
141 struct AuthorityChain *next;
144 * Resolver handle this entry in the chain belongs to.
146 struct GNS_ResolverHandle *rh;
149 * label/name corresponding to the authority
154 * #GNUNET_YES if the authority was a GNS authority,
155 * #GNUNET_NO if the authority was a DNS authority.
160 * Information about the resolver authority for this label.
166 * The zone of the GNS authority
168 struct GNUNET_CRYPTO_EcdsaPublicKey gns_authority;
173 * Domain of the DNS resolver that is the authority.
174 * (appended to construct the DNS name to resolve;
175 * this is NOT the DNS name of the DNS server!).
177 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
180 * List of resolutions of the 'ip' of the name server that
183 struct Gns2DnsPending *gp_head;
186 * Tail of list of resolutions of the 'ip' of the name server that
189 struct Gns2DnsPending *gp_tail;
192 * Handle to perform DNS lookups with this authority (in GNS2DNS handling).
194 struct GNUNET_DNSSTUB_Context *dns_handle;
197 * Did we succeed in getting an IP address for *any* of the DNS servers listed?
198 * Once we do, we can start with DNS queries.
203 * Did we start the recursive resolution via DNS?
215 * A result we got from DNS.
223 struct DnsResult *next;
228 struct DnsResult *prev;
231 * Binary value stored in the DNS record (appended to this struct)
236 * Expiration time for the DNS record, 0 if we didn't
237 * get anything useful (i.e. 'gethostbyname()' was used).
239 uint64_t expiration_time;
242 * Number of bytes in @e data.
247 * Type of the GNS/DNS record.
249 uint32_t record_type;
255 * Closure for #vpn_allocation_cb.
261 * Which resolution process are we processing.
263 struct GNS_ResolverHandle *rh;
266 * Handle to the VPN request that we were performing.
268 struct GNUNET_VPN_RedirectionRequest *vpn_request;
271 * Number of records serialized in @e rd_data.
273 unsigned int rd_count;
276 * Serialized records.
281 * Number of bytes in @e rd_data.
283 ssize_t rd_data_size;
288 * Handle to a currenty pending resolution. On result (positive or
289 * negative) the #GNS_ResultProcessor is called.
291 struct GNS_ResolverHandle
297 struct GNS_ResolverHandle *next;
302 struct GNS_ResolverHandle *prev;
305 * The top-level GNS authoritative zone to query
307 struct GNUNET_CRYPTO_EcdsaPublicKey authority_zone;
310 * called when resolution phase finishes
312 GNS_ResultProcessor proc;
315 * closure passed to @e proc
320 * Handle for DHT lookups. should be NULL if no lookups are in progress
322 struct GNUNET_DHT_GetHandle *get_handle;
325 * Handle to a VPN request, NULL if none is active.
327 struct VpnContext *vpn_ctx;
330 * Socket for a DNS request, NULL if none is active.
332 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
335 * Handle for standard DNS resolution, NULL if none is active.
337 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
340 * Pending Namecache lookup task
342 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe;
345 * Pending revocation check.
347 struct GNUNET_REVOCATION_Query *rev_check;
350 * Heap node associated with this lookup. Used to limit number of
351 * concurrent requests.
353 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
356 * DLL to store the authority chain
358 struct AuthorityChain *ac_head;
361 * DLL to store the authority chain
363 struct AuthorityChain *ac_tail;
366 * ID of a task associated with the resolution process.
368 struct GNUNET_SCHEDULER_Task *task_id;
371 * The name to resolve
376 * Legacy Hostname to use if we encountered GNS2DNS record
377 * and thus can deduct the LEHO from that transition.
382 * DLL of results we got from DNS.
384 struct DnsResult *dns_result_head;
387 * DLL of results we got from DNS.
389 struct DnsResult *dns_result_tail;
392 * Current offset in 'name' where we are resolving.
394 size_t name_resolution_pos;
399 enum GNUNET_GNS_LocalOptions options;
402 * For SRV and TLSA records, the number of the
403 * protocol specified in the name. 0 if no protocol was given.
408 * For SRV and TLSA records, the number of the
409 * service specified in the name. 0 if no service was given.
414 * Desired type for the resolution.
419 * We increment the loop limiter for each step in a recursive
420 * resolution. If it passes our threshold (i.e. due to
421 * self-recursion in the resolution, i.e CNAME fun), we stop.
423 unsigned int loop_limiter;
426 * 16 bit random ID we used in the @e dns_request.
428 uint16_t original_dns_id;
434 * Active namestore caching operations.
440 * Organized in a DLL.
442 struct CacheOps *next;
445 * Organized in a DLL.
447 struct CacheOps *prev;
450 * Pending Namestore caching task.
452 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe_cache;
458 * Our handle to the namecache service
460 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
463 * Our handle to the vpn service
465 static struct GNUNET_VPN_Handle *vpn_handle;
468 * Resolver handle to the dht
470 static struct GNUNET_DHT_Handle *dht_handle;
473 * Heap for limiting parallel DHT lookups
475 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
478 * Maximum amount of parallel queries to the DHT
480 static unsigned long long max_allowed_background_queries;
483 * Head of resolver lookup list
485 static struct GNS_ResolverHandle *rlh_head;
488 * Tail of resolver lookup list
490 static struct GNS_ResolverHandle *rlh_tail;
493 * Organized in a DLL.
495 static struct CacheOps *co_head;
498 * Organized in a DLL.
500 static struct CacheOps *co_tail;
505 static int disable_cache;
508 * Global configuration.
510 static const struct GNUNET_CONFIGURATION_Handle *cfg;
514 * Determine if this name is canonical (is a legal name in a zone, without delegation);
515 * note that we do not test that the name does not contain illegal characters, we only
516 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
517 * even though they consist of multiple labels.
520 * a.b.gnu = not canonical
522 * _foo._srv = canonical
523 * _f.bar = not canonical
525 * @param name the name to test
526 * @return #GNUNET_YES if canonical
528 /* dead, but keep for now */ int
529 is_canonical (const char *name)
534 if (NULL == strchr (name,
535 (unsigned char) '.'))
540 while (NULL != (dot = strchr (pos,
541 (unsigned char) '.')))
549 /* ************************** Resolution **************************** */
552 * Expands a name ending in .+ with the zone of origin.
554 * @param rh resolution context
555 * @param name name to modify (to be free'd or returned)
556 * @return updated name
559 translate_dot_plus (struct GNS_ResolverHandle *rh,
563 size_t s_len = strlen (name);
565 if (0 != strcmp (&name[s_len - 2],
567 return name; /* did not end in ".+" */
568 GNUNET_assert (GNUNET_YES == rh->ac_tail->gns_authority);
569 GNUNET_asprintf (&ret,
573 GNUNET_GNSRECORD_pkey_to_zkey (&rh->ac_tail->authority_info.gns_authority));
580 * Wrapper around #GNS_resolver_lookup_cancel() as a task.
581 * Used for delayed cleanup so we can unwind the stack first.
583 * @param cls the `struct GNS_ResolverHandle`
586 GNS_resolver_lookup_cancel_ (void *cls)
588 struct GNS_ResolverHandle *rh = cls;
591 GNS_resolver_lookup_cancel (rh);
596 * Function called to asynchronously fail a resolution.
598 * @param rh the resolution to fail
601 fail_resolution (struct GNS_ResolverHandle *rh)
603 rh->proc (rh->proc_cls,
606 GNUNET_assert (NULL == rh->task_id);
607 rh->task_id = GNUNET_SCHEDULER_add_now (&GNS_resolver_lookup_cancel_,
613 * Function called when a resolution times out.
615 * @param cls the `struct GNS_ResolverHandle`
618 timeout_resolution (void *cls)
620 struct GNS_ResolverHandle *rh = cls;
623 fail_resolution (rh);
628 * Get the next, rightmost label from the name that we are trying to resolve,
629 * and update the resolution position accordingly. Labels usually consist
630 * of up to 63 characters without a period ("."); however, we use a special
631 * convention to support SRV and TLSA records where the domain name
632 * includes an encoding for a service and protocol in the name. The
633 * syntax (see RFC 2782) here is "_Service._Proto.Name" and in this
634 * special case we include the "_Service._Proto" in the rightmost label.
635 * Thus, for "_443._tcp.foo.bar" we first return the label "bar" and then
636 * the label "_443._tcp.foo". The special case is detected by the
637 * presence of labels beginning with an underscore. Whenever a label
638 * begins with an underscore, it is combined with the label to its right
639 * (and the "." is preserved).
641 * @param rh handle to the resolution operation to get the next label from
642 * @return NULL if there are no more labels
645 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
656 if (0 == rh->name_resolution_pos)
658 dot = memrchr (rh->name,
660 rh->name_resolution_pos);
663 /* done, this was the last one */
664 len = rh->name_resolution_pos;
666 rh->name_resolution_pos = 0;
670 /* advance by one label */
671 len = rh->name_resolution_pos - (dot - rh->name) - 1;
673 rh->name_resolution_pos = dot - rh->name;
677 ret = GNUNET_strndup (rp, len);
678 /* If we have labels starting with underscore with label on
679 * the right (SRV/DANE/BOX case), determine port/protocol;
680 * The format of `rh->name` must be "_PORT._PROTOCOL".
682 if ( ('_' == rh->name[0]) &&
683 (NULL != (dot = memrchr (rh->name,
685 rh->name_resolution_pos))) &&
687 (NULL == memrchr (rh->name,
691 srv_name = GNUNET_strndup (&rh->name[1],
692 (dot - rh->name) - 1);
693 proto_name = GNUNET_strndup (&dot[2],
694 rh->name_resolution_pos - (dot - rh->name) - 1);
695 rh->name_resolution_pos = 0;
696 pe = getprotobyname (proto_name);
699 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
700 _("Protocol `%s' unknown, skipping labels.\n"),
702 GNUNET_free (proto_name);
703 GNUNET_free (srv_name);
706 se = getservbyname (srv_name,
710 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
711 _("Service `%s' unknown for protocol `%s', skipping labels.\n"),
714 GNUNET_free (proto_name);
715 GNUNET_free (srv_name);
718 rh->protocol = pe->p_proto;
719 rh->service = se->s_port;
720 GNUNET_free (proto_name);
721 GNUNET_free (srv_name);
728 * Gives the cummulative result obtained to the callback and clean up the request.
730 * @param rh resolution process that has culminated in a result
733 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
735 struct DnsResult *pos;
740 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
743 struct GNUNET_GNSRECORD_Data rd[n];
746 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
748 rd[i].data = pos->data;
749 rd[i].data_size = pos->data_size;
750 rd[i].record_type = pos->record_type;
751 if (0 == pos->expiration_time)
753 rd[i].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
754 rd[i].expiration_time = 0;
758 rd[i].flags = GNUNET_GNSRECORD_RF_NONE;
759 rd[i].expiration_time = pos->expiration_time;
763 GNUNET_assert (i == n);
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "Transmitting standard DNS result with %u records\n",
767 rh->proc (rh->proc_cls,
771 GNS_resolver_lookup_cancel (rh);
776 * Add a result from DNS to the records to be returned to the application.
778 * @param rh resolution request to extend with a result
779 * @param expiration_time expiration time for the answer
780 * @param record_type DNS record type of the answer
781 * @param data_size number of bytes in @a data
782 * @param data binary data to return in DNS record
785 add_dns_result (struct GNS_ResolverHandle *rh,
786 uint64_t expiration_time,
787 uint32_t record_type,
791 struct DnsResult *res;
793 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
794 res->expiration_time = expiration_time;
795 res->data_size = data_size;
796 res->record_type = record_type;
798 GNUNET_memcpy (&res[1],
801 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
808 * We had to do a DNS lookup. Convert the result (if any) and return
811 * @param cls closure with the `struct GNS_ResolverHandle`
812 * @param addr one of the addresses of the host, NULL for the last address
813 * @param addrlen length of the address
816 handle_dns_result (void *cls,
817 const struct sockaddr *addr,
820 struct GNS_ResolverHandle *rh = cls;
821 const struct sockaddr_in *sa4;
822 const struct sockaddr_in6 *sa6;
826 rh->std_resolve = NULL;
827 transmit_lookup_dns_result (rh);
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
831 "Received %u bytes of DNS IP data\n",
833 switch (addr->sa_family)
836 sa4 = (const struct sockaddr_in *) addr;
838 0 /* expiration time is unknown */,
839 GNUNET_DNSPARSER_TYPE_A,
840 sizeof (struct in_addr),
844 sa6 = (const struct sockaddr_in6 *) addr;
846 0 /* expiration time is unknown */,
847 GNUNET_DNSPARSER_TYPE_AAAA,
848 sizeof (struct in6_addr),
859 * Task scheduled to continue with the resolution process.
861 * @param cls the 'struct GNS_ResolverHandle' of the resolution
862 * @param tc task context
865 recursive_resolution (void *cls);
869 * Begin the resolution process from 'name', starting with
870 * the identification of the zone specified by 'name'.
872 * @param cls closure with `struct GNS_ResolverHandle *rh`
875 start_resolver_lookup (void *cls);
879 * Function called with the result of a DNS resolution.
881 * @param cls the request handle of the resolution that
882 * we were attempting to make
883 * @param dns dns response, never NULL
884 * @param dns_len number of bytes in @a dns
887 dns_result_parser (void *cls,
888 const struct GNUNET_TUN_DnsHeader *dns,
891 struct GNS_ResolverHandle *rh = cls;
892 struct GNUNET_DNSPARSER_Packet *p;
893 const struct GNUNET_DNSPARSER_Record *rec;
894 unsigned int rd_count;
898 rh->dns_request = NULL;
899 GNUNET_SCHEDULER_cancel (rh->task_id);
901 fail_resolution (rh);
904 if (rh->original_dns_id != dns->id)
906 /* DNS answer, but for another query */
909 p = GNUNET_DNSPARSER_parse ((const char *) dns,
913 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
914 _("Failed to parse DNS response\n"));
918 /* We got a result from DNS */
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
920 "Received DNS response for `%s' with %u answers\n",
922 (unsigned int) p->num_answers);
923 if ( (p->num_answers > 0) &&
924 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
925 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
929 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
930 "Got CNAME `%s' from DNS for `%s'\n",
931 p->answers[0].data.hostname,
933 if (NULL != rh->std_resolve)
935 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
936 "Multiple CNAME results from DNS resolving `%s'! Not really allowed...\n",
938 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
940 GNUNET_free (rh->name);
941 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
942 rh->name_resolution_pos = strlen (rh->name);
943 switch (rh->record_type)
945 case GNUNET_DNSPARSER_TYPE_A:
948 case GNUNET_DNSPARSER_TYPE_AAAA:
955 if (NULL != rh->leho)
957 GNUNET_TIME_UNIT_HOURS.rel_value_us,
958 GNUNET_GNSRECORD_TYPE_LEHO,
961 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
966 GNUNET_DNSPARSER_free_packet (p);
967 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
968 rh->dns_request = NULL;
972 /* convert from (parsed) DNS to (binary) GNS format! */
973 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
975 struct GNUNET_GNSRECORD_Data rd[rd_count + 1]; /* +1 for LEHO */
977 char buf[UINT16_MAX];
986 for (unsigned int i=0;i<rd_count;i++)
988 if (i < p->num_answers)
989 rec = &p->answers[i];
990 else if (i < p->num_answers + p->num_authority_records)
991 rec = &p->authority_records[i - p->num_answers];
993 rec = &p->additional_records[i - p->num_answers - p->num_authority_records];
994 /* As we copied the full DNS name to 'rh->ac_tail->label', this
995 should be the correct check to see if this record is actually
996 a record for our label... */
997 if (0 != strcmp (rec->name,
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1001 "Dropping record `%s', does not match desired name `%s'\n",
1003 rh->ac_tail->label);
1007 rd[i - skip].record_type = rec->type;
1008 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
1011 case GNUNET_DNSPARSER_TYPE_A:
1012 if (rec->data.raw.data_len != sizeof (struct in_addr))
1014 GNUNET_break_op (0);
1018 rd[i - skip].data_size = rec->data.raw.data_len;
1019 rd[i - skip].data = rec->data.raw.data;
1021 case GNUNET_DNSPARSER_TYPE_AAAA:
1022 if (rec->data.raw.data_len != sizeof (struct in6_addr))
1024 GNUNET_break_op (0);
1028 rd[i - skip].data_size = rec->data.raw.data_len;
1029 rd[i - skip].data = rec->data.raw.data;
1031 case GNUNET_DNSPARSER_TYPE_CNAME:
1032 case GNUNET_DNSPARSER_TYPE_PTR:
1033 case GNUNET_DNSPARSER_TYPE_NS:
1034 buf_start = buf_off;
1036 GNUNET_DNSPARSER_builder_add_name (buf,
1039 rec->data.hostname))
1045 rd[i - skip].data_size = buf_off - buf_start;
1046 rd[i - skip].data = &buf[buf_start];
1048 case GNUNET_DNSPARSER_TYPE_SOA:
1049 buf_start = buf_off;
1051 GNUNET_DNSPARSER_builder_add_soa (buf,
1060 rd[i - skip].data_size = buf_off - buf_start;
1061 rd[i - skip].data = &buf[buf_start];
1063 case GNUNET_DNSPARSER_TYPE_MX:
1064 buf_start = buf_off;
1066 GNUNET_DNSPARSER_builder_add_mx (buf,
1075 rd[i - skip].data_size = buf_off - buf_start;
1076 rd[i - skip].data = &buf[buf_start];
1078 case GNUNET_DNSPARSER_TYPE_SRV:
1079 buf_start = buf_off;
1081 GNUNET_DNSPARSER_builder_add_srv (buf,
1090 rd[i - skip].data_size = buf_off - buf_start;
1091 rd[i - skip].data = &buf[buf_start];
1094 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1095 _("Skipping record of unsupported type %d\n"),
1100 } /* end of for all records in answer */
1101 if (NULL != rh->leho)
1103 rd[rd_count - skip].record_type = GNUNET_GNSRECORD_TYPE_LEHO;
1104 rd[rd_count - skip].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1105 rd[rd_count - skip].expiration_time = GNUNET_TIME_UNIT_HOURS.rel_value_us;
1106 rd[rd_count - skip].data = rh->leho;
1107 rd[rd_count - skip].data_size = strlen (rh->leho);
1108 skip--; /* skip one LESS */
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1114 "Returning DNS response for `%s' with %u answers\n",
1116 (unsigned int) (rd_count - skip));
1117 rh->proc (rh->proc_cls,
1120 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
1121 rh->dns_request = NULL;
1123 GNUNET_DNSPARSER_free_packet (p);
1124 if (NULL != rh->task_id)
1125 GNUNET_SCHEDULER_cancel (rh->task_id); /* should be timeout task */
1126 rh->task_id = GNUNET_SCHEDULER_add_now (&GNS_resolver_lookup_cancel_,
1132 * Perform recursive DNS resolution. Asks the given DNS resolver to
1133 * resolve "rh->dns_name", possibly recursively proceeding following
1134 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
1135 * we find the answer.
1137 * @param rh resolution information
1140 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
1142 struct AuthorityChain *ac;
1143 struct GNUNET_DNSPARSER_Query *query;
1144 struct GNUNET_DNSPARSER_Packet *p;
1146 size_t dns_request_length;
1150 GNUNET_assert (NULL != ac);
1151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1152 "Starting DNS lookup for `%s'\n",
1154 GNUNET_assert (GNUNET_NO == ac->gns_authority);
1155 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
1156 query->name = GNUNET_strdup (ac->label);
1157 query->type = rh->record_type;
1158 query->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1159 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
1162 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1164 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
1165 p->flags.recursion_desired = 1;
1166 ret = GNUNET_DNSPARSER_pack (p,
1169 &dns_request_length);
1170 if (GNUNET_OK != ret)
1173 rh->proc (rh->proc_cls,
1176 GNUNET_assert (NULL == rh->task_id);
1177 rh->task_id = GNUNET_SCHEDULER_add_now (&GNS_resolver_lookup_cancel_,
1182 rh->original_dns_id = p->id;
1183 GNUNET_assert (NULL != ac->authority_info.dns_authority.dns_handle);
1184 GNUNET_assert (NULL == rh->dns_request);
1185 rh->leho = GNUNET_strdup (ac->label);
1186 rh->dns_request = GNUNET_DNSSTUB_resolve (ac->authority_info.dns_authority.dns_handle,
1191 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
1192 &timeout_resolution,
1195 if (GNUNET_SYSERR != ret)
1196 GNUNET_free (dns_request);
1197 GNUNET_DNSPARSER_free_packet (p);
1202 * We encountered a CNAME record during our resolution.
1203 * Merge it into our chain.
1205 * @param rh resolution we are performing
1206 * @param cname value of the cname record we got for the current
1207 * authority chain tail
1210 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
1215 struct AuthorityChain *ac;
1218 nlen = strlen (cname);
1221 &cname[nlen - 2])) )
1223 /* CNAME resolution continues relative to current domain */
1224 if (0 == rh->name_resolution_pos)
1226 res = GNUNET_strndup (cname, nlen - 2);
1227 rh->name_resolution_pos = nlen - 2;
1231 GNUNET_asprintf (&res,
1233 (int) rh->name_resolution_pos,
1237 rh->name_resolution_pos = strlen (res);
1239 GNUNET_free (rh->name);
1241 ac = GNUNET_new (struct AuthorityChain);
1243 ac->gns_authority = GNUNET_YES;
1244 ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
1245 ac->label = resolver_lookup_get_next_label (rh);
1246 /* add AC to tail */
1247 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1250 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1254 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1255 "Got CNAME `%s' from GNS for `%s'\n",
1258 if (NULL != rh->std_resolve)
1260 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1261 "Multiple CNAME results from GNS resolving `%s'! Not really allowed...\n",
1263 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
1265 /* name is absolute, go to DNS */
1266 GNUNET_free (rh->name);
1267 rh->name = GNUNET_strdup (cname);
1268 rh->name_resolution_pos = strlen (rh->name);
1269 switch (rh->record_type)
1271 case GNUNET_DNSPARSER_TYPE_A:
1274 case GNUNET_DNSPARSER_TYPE_AAAA:
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1282 "Doing standard DNS lookup for `%s'\n",
1284 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
1293 * Process a records that were decrypted from a block.
1295 * @param cls closure with the 'struct GNS_ResolverHandle'
1296 * @param rd_count number of entries in @a rd array
1297 * @param rd array of records with data to store
1300 handle_gns_resolution_result (void *cls,
1301 unsigned int rd_count,
1302 const struct GNUNET_GNSRECORD_Data *rd);
1306 * Callback invoked from the VPN service once a redirection is
1307 * available. Provides the IP address that can now be used to
1308 * reach the requested destination. Replaces the "VPN" record
1309 * with the respective A/AAAA record and continues processing.
1311 * @param cls closure
1312 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1313 * will match 'result_af' from the request
1314 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1315 * that the VPN allocated for the redirection;
1316 * traffic to this IP will now be redirected to the
1317 * specified target peer; NULL on error
1320 vpn_allocation_cb (void *cls,
1322 const void *address)
1324 struct VpnContext *vpn_ctx = cls;
1325 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1326 struct GNUNET_GNSRECORD_Data rd[vpn_ctx->rd_count];
1329 vpn_ctx->vpn_request = NULL;
1331 GNUNET_assert (GNUNET_OK ==
1332 GNUNET_GNSRECORD_records_deserialize ((size_t) vpn_ctx->rd_data_size,
1336 for (i=0;i<vpn_ctx->rd_count;i++)
1338 if (GNUNET_GNSRECORD_TYPE_VPN == rd[i].record_type)
1343 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1344 rd[i].data_size = sizeof (struct in_addr);
1345 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1347 rd[i].data = address;
1350 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1351 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1353 rd[i].data = address;
1354 rd[i].data_size = sizeof (struct in6_addr);
1362 GNUNET_assert (i < vpn_ctx->rd_count);
1363 if (0 == vpn_ctx->rd_count)
1364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1365 _("VPN returned empty result for `%s'\n"),
1367 handle_gns_resolution_result (rh,
1370 GNUNET_free (vpn_ctx->rd_data);
1371 GNUNET_free (vpn_ctx);
1376 * We have resolved one or more of the nameservers for a
1377 * GNS2DNS lookup. Once we have some of them, begin using
1378 * the DNSSTUB resolver.
1380 * @param ac context for GNS2DNS resolution
1383 continue_with_gns2dns (struct AuthorityChain *ac)
1385 struct GNS_ResolverHandle *rh = ac->rh;
1387 if ( (NULL != ac->authority_info.dns_authority.gp_head) &&
1388 (GNUNET_NO == ac->authority_info.dns_authority.found) )
1389 return; /* more pending and none found yet */
1390 if (GNUNET_NO == ac->authority_info.dns_authority.found)
1392 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1393 "Failed to resolve DNS server for `%s' in GNS2DNS resolution\n",
1394 ac->authority_info.dns_authority.name);
1395 fail_resolution (rh);
1398 if (GNUNET_NO != ac->authority_info.dns_authority.launched)
1399 return; /* already running, do not launch again! */
1401 ac->authority_info.dns_authority.launched = GNUNET_YES;
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 "Will continue resolution using DNS to resolve `%s'\n",
1405 GNUNET_assert (NULL == rh->task_id);
1406 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1413 * We've resolved the IP address for the DNS resolver to use
1414 * after encountering a GNS2DNS record.
1416 * @param cls the `struct Gns2DnsPending` used for this request
1417 * @param rd_count number of records in @a rd
1418 * @param rd addresses for the DNS resolver (presumably)
1421 handle_gns2dns_result (void *cls,
1422 unsigned int rd_count,
1423 const struct GNUNET_GNSRECORD_Data *rd)
1425 struct Gns2DnsPending *gp = cls;
1426 struct AuthorityChain *ac = gp->ac;
1428 GNUNET_CONTAINER_DLL_remove (ac->authority_info.dns_authority.gp_head,
1429 ac->authority_info.dns_authority.gp_tail,
1431 /* enable cleanup of 'rh' handle that automatically comes after we return,
1432 and which expects 'rh' to be in the #rlh_head DLL. */
1435 GNUNET_CONTAINER_DLL_insert (rlh_head,
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 "Received %u results for IP address of DNS server for GNS2DNS transition\n",
1444 /* find suitable A/AAAA record */
1445 for (unsigned int j=0;j<rd_count;j++)
1447 switch (rd[j].record_type)
1449 case GNUNET_DNSPARSER_TYPE_A:
1451 struct sockaddr_in v4;
1453 if (sizeof (struct in_addr) != rd[j].data_size)
1455 GNUNET_break_op (0);
1461 v4.sin_family = AF_INET;
1462 v4.sin_port = htons (53);
1463 #if HAVE_SOCKADDR_IN_SIN_LEN
1464 v4.sin_len = (u_char) sizeof (v4);
1466 GNUNET_memcpy (&v4.sin_addr,
1468 sizeof (struct in_addr));
1470 GNUNET_DNSSTUB_add_dns_sa (ac->authority_info.dns_authority.dns_handle,
1471 (const struct sockaddr *) &v4))
1472 ac->authority_info.dns_authority.found = GNUNET_YES;
1475 case GNUNET_DNSPARSER_TYPE_AAAA:
1477 struct sockaddr_in6 v6;
1479 if (sizeof (struct in6_addr) != rd[j].data_size)
1481 GNUNET_break_op (0);
1484 /* FIXME: might want to check if we support IPv6 here,
1485 and otherwise skip this one and hope we find another */
1489 v6.sin6_family = AF_INET6;
1490 v6.sin6_port = htons (53);
1491 #if HAVE_SOCKADDR_IN_SIN_LEN
1492 v6.sin6_len = (u_char) sizeof (v6);
1494 GNUNET_memcpy (&v6.sin6_addr,
1496 sizeof (struct in6_addr));
1498 GNUNET_DNSSTUB_add_dns_sa (ac->authority_info.dns_authority.dns_handle,
1499 (const struct sockaddr *) &v6))
1500 ac->authority_info.dns_authority.found = GNUNET_YES;
1507 continue_with_gns2dns (ac);
1512 * Function called by the resolver for each address obtained from DNS.
1514 * @param cls closure, a `struct Gns2DnsPending *`
1515 * @param addr one of the addresses of the host, NULL for the last address
1516 * @param addrlen length of @a addr
1519 handle_gns2dns_ip (void *cls,
1520 const struct sockaddr *addr,
1523 struct Gns2DnsPending *gp = cls;
1524 struct AuthorityChain *ac = gp->ac;
1525 struct sockaddr_storage ss;
1526 struct sockaddr_in *v4;
1527 struct sockaddr_in6 *v6;
1531 /* DNS resolution finished */
1532 if (0 == gp->num_results)
1533 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1534 "Failed to use DNS to resolve name of DNS resolver\n");
1535 GNUNET_CONTAINER_DLL_remove (ac->authority_info.dns_authority.gp_head,
1536 ac->authority_info.dns_authority.gp_tail,
1539 continue_with_gns2dns (ac);
1545 switch (ss.ss_family)
1548 v4 = (struct sockaddr_in *) &ss;
1549 v4->sin_port = htons (53);
1553 v6 = (struct sockaddr_in6 *) &ss;
1554 v6->sin6_port = htons (53);
1558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1559 "Unsupported AF %d\n",
1564 GNUNET_DNSSTUB_add_dns_sa (ac->authority_info.dns_authority.dns_handle,
1565 (struct sockaddr *) &ss))
1566 ac->authority_info.dns_authority.found = GNUNET_YES;
1571 * We found a CNAME record, perform recursive resolution on it.
1573 * @param rh resolution handle
1574 * @param rd record with CNAME to resolve recursively
1577 recursive_cname_resolution (struct GNS_ResolverHandle *rh,
1578 const struct GNUNET_GNSRECORD_Data *rd)
1584 cname = GNUNET_DNSPARSER_parse_name (rd->data,
1587 if ( (NULL == cname) ||
1588 (off != rd->data_size) )
1590 GNUNET_break_op (0); /* record not well-formed */
1591 GNUNET_free_non_null (cname);
1592 fail_resolution (rh);
1595 handle_gns_cname_result (rh,
1597 GNUNET_free (cname);
1602 * We found a PKEY record, perform recursive resolution on it.
1604 * @param rh resolution handle
1605 * @param rd record with PKEY to resolve recursively
1608 recursive_pkey_resolution (struct GNS_ResolverHandle *rh,
1609 const struct GNUNET_GNSRECORD_Data *rd)
1611 struct AuthorityChain *ac;
1613 /* delegation to another zone */
1614 if (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
1617 GNUNET_break_op (0);
1618 fail_resolution (rh);
1621 /* expand authority chain */
1622 ac = GNUNET_new (struct AuthorityChain);
1624 ac->gns_authority = GNUNET_YES;
1625 GNUNET_memcpy (&ac->authority_info.gns_authority,
1627 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1628 ac->label = resolver_lookup_get_next_label (rh);
1629 /* add AC to tail */
1630 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1634 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1640 * We found one or more GNS2DNS records, perform recursive resolution on it.
1641 * (to be precise, one or more records in @a rd is GNS2DNS, there may be others,
1642 * so this function still needs to check which ones are GNS2DNS).
1644 * @param rh resolution handle
1645 * @param rd_count length of the @a rd array
1646 * @param rd record with PKEY to resolve recursively
1647 * @return #GNUNET_OK if this worked, #GNUNET_SYSERR if no GNS2DNS records were in @a rd
1650 recursive_gns2dns_resolution (struct GNS_ResolverHandle *rh,
1651 unsigned int rd_count,
1652 const struct GNUNET_GNSRECORD_Data *rd)
1654 struct AuthorityChain *ac;
1659 /* expand authority chain */
1660 ac = GNUNET_new (struct AuthorityChain);
1662 ac->authority_info.dns_authority.dns_handle = GNUNET_DNSSTUB_start (4);
1664 for (unsigned int i=0;i<rd_count;i++)
1669 struct Gns2DnsPending *gp;
1670 struct GNUNET_CRYPTO_EcdsaPublicKey zone;
1671 struct sockaddr_in v4;
1672 struct sockaddr_in6 v6;
1674 if (GNUNET_GNSRECORD_TYPE_GNS2DNS != rd[i].record_type)
1677 n = GNUNET_DNSPARSER_parse_name (rd[i].data,
1680 ip = GNUNET_DNSPARSER_parse_name (rd[i].data,
1685 (off != rd[i].data_size) )
1687 GNUNET_break_op (0);
1688 GNUNET_free_non_null (n);
1689 GNUNET_free_non_null (ip);
1692 /* resolve 'ip' to determine the IP(s) of the DNS
1693 resolver to use for lookup of 'ns' */
1696 if (0 != strcasecmp (ns,
1699 /* NS values must all be the same for all GNS2DNS records,
1700 anything else leads to insanity */
1701 GNUNET_break_op (0);
1713 /* check if 'ip' is already an IPv4/IPv6 address */
1714 if ( (1 == inet_pton (AF_INET,
1717 (1 == inet_pton (AF_INET6,
1721 GNUNET_break (GNUNET_OK ==
1722 GNUNET_DNSSTUB_add_dns_ip (ac->authority_info.dns_authority.dns_handle,
1724 ac->authority_info.dns_authority.found = GNUNET_YES;
1728 tld = GNS_get_tld (ip);
1729 if (0 != strcmp (tld,
1732 /* 'ip' is a DNS name */
1733 gp = GNUNET_new (struct Gns2DnsPending);
1735 GNUNET_CONTAINER_DLL_insert (ac->authority_info.dns_authority.gp_head,
1736 ac->authority_info.dns_authority.gp_tail,
1738 gp->dns_rh = GNUNET_RESOLVER_ip_get (ip,
1740 GNUNET_TIME_UNIT_FOREVER_REL,
1746 /* 'ip' should be a GNS name */
1747 gp = GNUNET_new (struct Gns2DnsPending);
1749 GNUNET_CONTAINER_DLL_insert (ac->authority_info.dns_authority.gp_head,
1750 ac->authority_info.dns_authority.gp_tail,
1752 gp->rh = GNUNET_new (struct GNS_ResolverHandle);
1753 ip = translate_dot_plus (rh,
1755 tld = GNS_get_tld (ip);
1757 GNUNET_GNSRECORD_zkey_to_pkey (tld,
1760 GNUNET_break_op (0);
1764 gp->rh->authority_zone = zone;
1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1766 "Resolving `%s' to determine IP address of DNS server for GNS2DNS transition for `%s'\n",
1770 gp->rh->name_resolution_pos = strlen (ip) - strlen (tld) - 1;
1771 gp->rh->proc = &handle_gns2dns_result;
1772 gp->rh->proc_cls = gp;
1773 gp->rh->record_type = GNUNET_GNSRECORD_TYPE_ANY;
1774 gp->rh->options = GNUNET_GNS_LO_DEFAULT;
1775 gp->rh->loop_limiter = rh->loop_limiter + 1;
1777 = GNUNET_SCHEDULER_add_now (&start_resolver_lookup,
1779 } /* end 'for all records' */
1783 /* not a single GNS2DNS record found */
1785 return GNUNET_SYSERR;
1787 GNUNET_assert (strlen (ns) <= GNUNET_DNSPARSER_MAX_NAME_LENGTH);
1788 strcpy (ac->authority_info.dns_authority.name,
1790 /* for DNS recursion, the label is the full DNS name,
1791 created from the remainder of the GNS name and the
1792 name in the NS record */
1793 GNUNET_asprintf (&ac->label,
1795 (int) rh->name_resolution_pos,
1797 (0 != rh->name_resolution_pos) ? "." : "",
1800 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1803 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1805 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1806 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1808 return GNUNET_SYSERR;
1810 continue_with_gns2dns (ac);
1816 * Process a records that were decrypted from a block.
1818 * @param cls closure with the `struct GNS_ResolverHandle`
1819 * @param rd_count number of entries in @a rd array
1820 * @param rd array of records with data to store
1823 handle_gns_resolution_result (void *cls,
1824 unsigned int rd_count,
1825 const struct GNUNET_GNSRECORD_Data *rd)
1827 struct GNS_ResolverHandle *rh = cls;
1829 struct VpnContext *vpn_ctx;
1830 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1832 struct GNUNET_HashCode vhash;
1834 char scratch[UINT16_MAX];
1836 size_t scratch_start;
1838 struct GNUNET_GNSRECORD_Data rd_new[rd_count];
1839 unsigned int rd_off;
1841 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1842 "Resolution succeeded for `%s' in zone %s, got %u records\n",
1844 GNUNET_GNSRECORD_z2s (&rh->ac_tail->authority_info.gns_authority),
1848 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1849 _("GNS lookup failed (zero records found for `%s')\n"),
1851 fail_resolution (rh);
1855 if (0 == rh->name_resolution_pos)
1857 /* top-level match, are we done yet? */
1858 if ( (rd_count > 0) &&
1859 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1860 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1863 cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
1866 if ( (NULL == cname) ||
1867 (off != rd[0].data_size) )
1869 GNUNET_break_op (0);
1870 GNUNET_free_non_null (cname);
1871 fail_resolution (rh);
1874 handle_gns_cname_result (rh,
1876 GNUNET_free (cname);
1879 /* If A/AAAA was requested, but we got a VPN
1880 record, we convert it to A/AAAA using GNUnet VPN */
1881 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1882 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1884 for (unsigned int i=0;i<rd_count;i++)
1886 switch (rd[i].record_type)
1888 case GNUNET_GNSRECORD_TYPE_VPN:
1890 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1891 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) >
1894 GNUNET_break_op (0);
1895 fail_resolution (rh);
1898 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1899 vname = (const char *) &vpn[1];
1900 if ('\0' != vname[rd[i].data_size - 1 - sizeof (struct GNUNET_TUN_GnsVpnRecord)])
1902 GNUNET_break_op (0);
1903 fail_resolution (rh);
1906 GNUNET_TUN_service_name_to_hash (vname,
1908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1909 "Attempting VPN allocation for %s-%s (AF: %d, proto %d)\n",
1910 GNUNET_i2s (&vpn->peer),
1913 (int) ntohs (vpn->proto));
1914 vpn_ctx = GNUNET_new (struct VpnContext);
1915 rh->vpn_ctx = vpn_ctx;
1917 vpn_ctx->rd_data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
1919 if (vpn_ctx->rd_data_size < 0)
1921 GNUNET_break_op (0);
1922 GNUNET_free (vpn_ctx);
1923 fail_resolution (rh);
1926 vpn_ctx->rd_data = GNUNET_malloc ((size_t) vpn_ctx->rd_data_size);
1927 vpn_ctx->rd_count = rd_count;
1928 GNUNET_assert (vpn_ctx->rd_data_size ==
1929 GNUNET_GNSRECORD_records_serialize (rd_count,
1931 (size_t) vpn_ctx->rd_data_size,
1933 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1938 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1943 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1945 /* delegation to DNS */
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1947 "Found GNS2DNS record, delegating to DNS!\n");
1949 recursive_gns2dns_resolution (rh,
1960 } /* end: name_resolution_pos */
1961 /* convert relative names in record values to absolute names,
1962 using 'scratch' array for memory allocations */
1965 for (unsigned int i=0;i<rd_count;i++)
1967 GNUNET_assert (rd_off <= i);
1968 if ( (0 != rh->protocol) &&
1969 (0 != rh->service) &&
1970 (GNUNET_GNSRECORD_TYPE_BOX != rd[i].record_type) )
1971 continue; /* we _only_ care about boxed records */
1973 GNUNET_assert (rd_off < rd_count);
1974 rd_new[rd_off] = rd[i];
1975 /* Check if the embedded name(s) end in "+", and if so,
1976 replace the "+" with the zone at "ac_tail", changing the name
1977 to a ".ZONEKEY". The name is allocated on the 'scratch' array,
1978 so we can free it afterwards. */
1979 switch (rd[i].record_type)
1981 case GNUNET_DNSPARSER_TYPE_CNAME:
1986 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1989 if ( (NULL == cname) ||
1990 (off != rd[i].data_size) )
1992 GNUNET_break_op (0); /* record not well-formed */
1996 cname = translate_dot_plus (rh, cname);
1997 GNUNET_break (NULL != cname);
1998 scratch_start = scratch_off;
2000 GNUNET_DNSPARSER_builder_add_name (scratch,
2009 GNUNET_assert (rd_off < rd_count);
2010 rd_new[rd_off].data = &scratch[scratch_start];
2011 rd_new[rd_off].data_size = scratch_off - scratch_start;
2015 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);
2056 case GNUNET_DNSPARSER_TYPE_MX:
2058 struct GNUNET_DNSPARSER_MxRecord *mx;
2061 mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
2064 if ( (NULL == mx) ||
2065 (off != rd[i].data_size) )
2067 GNUNET_break_op (0); /* record not well-formed */
2071 mx->mxhost = translate_dot_plus (rh, mx->mxhost);
2072 scratch_start = scratch_off;
2074 GNUNET_DNSPARSER_builder_add_mx (scratch,
2083 GNUNET_assert (rd_off < rd_count);
2084 rd_new[rd_off].data = &scratch[scratch_start];
2085 rd_new[rd_off].data_size = scratch_off - scratch_start;
2090 GNUNET_DNSPARSER_free_mx (mx);
2093 case GNUNET_DNSPARSER_TYPE_SRV:
2095 struct GNUNET_DNSPARSER_SrvRecord *srv;
2098 srv = GNUNET_DNSPARSER_parse_srv (rd[i].data,
2101 if ( (NULL == srv) ||
2102 (off != rd[i].data_size) )
2104 GNUNET_break_op (0); /* record not well-formed */
2108 srv->target = translate_dot_plus (rh, srv->target);
2109 scratch_start = scratch_off;
2111 GNUNET_DNSPARSER_builder_add_srv (scratch,
2120 GNUNET_assert (rd_off < rd_count);
2121 rd_new[rd_off].data = &scratch[scratch_start];
2122 rd_new[rd_off].data_size = scratch_off - scratch_start;
2127 GNUNET_DNSPARSER_free_srv (srv);
2131 case GNUNET_GNSRECORD_TYPE_NICK:
2136 if ((rd[i].data_size > 0) &&
2137 (nick[rd[i].data_size -1] != '\0'))
2139 GNUNET_break_op (0);
2144 case GNUNET_GNSRECORD_TYPE_PKEY:
2146 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
2148 if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
2150 GNUNET_break_op (0);
2153 GNUNET_memcpy (&pub,
2157 if (GNUNET_GNSRECORD_TYPE_PKEY != rh->record_type)
2159 /* try to resolve "@" */
2160 struct AuthorityChain *ac;
2162 ac = GNUNET_new (struct AuthorityChain);
2164 ac->gns_authority = GNUNET_YES;
2165 ac->authority_info.gns_authority = pub;
2166 ac->label = GNUNET_strdup (GNUNET_GNS_EMPTY_LABEL_AT);
2167 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
2170 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
2176 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
2178 /* delegation to DNS */
2179 if (GNUNET_GNSRECORD_TYPE_GNS2DNS == rh->record_type)
2182 break; /* do not follow to DNS, we wanted the GNS2DNS record! */
2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2185 "Found GNS2DNS record, delegating to DNS!\n");
2187 recursive_gns2dns_resolution (rh,
2194 case GNUNET_GNSRECORD_TYPE_BOX:
2196 /* unbox SRV/TLSA records if a specific one was requested */
2197 if ( (0 != rh->protocol) &&
2198 (0 != rh->service) &&
2199 (rd[i].data_size >= sizeof (struct GNUNET_GNSRECORD_BoxRecord)) )
2201 const struct GNUNET_GNSRECORD_BoxRecord *box;
2204 if ( (ntohs (box->protocol) == rh->protocol) &&
2205 (ntohs (box->service) == rh->service) )
2207 /* Box matches, unbox! */
2208 GNUNET_assert (rd_off < rd_count);
2209 rd_new[rd_off].record_type = ntohl (box->record_type);
2210 rd_new[rd_off].data_size -= sizeof (struct GNUNET_GNSRECORD_BoxRecord);
2211 rd_new[rd_off].data = &box[1];
2217 /* no specific protocol/service specified, preserve all BOX
2218 records (for modern, GNS-enabled applications) */
2227 } /* end: for rd_count */
2229 /* yes, we are done, return result */
2230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2231 "Returning GNS response for `%s' with %u answers\n",
2234 rh->proc (rh->proc_cls,
2237 rh->task_id = GNUNET_SCHEDULER_add_now (&GNS_resolver_lookup_cancel_,
2242 switch (rd[0].record_type)
2244 case GNUNET_DNSPARSER_TYPE_CNAME:
2245 GNUNET_break_op (1 == rd_count); /* CNAME should be unique */
2246 recursive_cname_resolution (rh,
2249 case GNUNET_GNSRECORD_TYPE_PKEY:
2250 GNUNET_break_op (1 == rd_count); /* PKEY should be unique */
2251 recursive_pkey_resolution (rh,
2256 recursive_gns2dns_resolution (rh,
2263 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2264 _("GNS lookup recursion failed (no delegation record found)\n"));
2265 fail_resolution (rh);
2270 * Function called once the namestore has completed the request for
2273 * @param cls closure with the `struct CacheOps`
2274 * @param success #GNUNET_OK on success
2275 * @param emsg error message
2278 namecache_cache_continuation (void *cls,
2282 struct CacheOps *co = cls;
2284 co->namecache_qe_cache = NULL;
2285 if (GNUNET_OK != success)
2286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2287 _("Failed to cache GNS resolution: %s\n"),
2289 GNUNET_CONTAINER_DLL_remove (co_head,
2297 * Iterator called on each result obtained for a DHT
2298 * operation that expects a reply
2300 * @param cls closure with the `struct GNS_ResolverHandle`
2301 * @param exp when will this value expire
2302 * @param key key of the result
2303 * @param get_path peers on reply path (or NULL if not recorded)
2304 * [0] = datastore's first neighbor, [length - 1] = local peer
2305 * @param get_path_length number of entries in @a get_path
2306 * @param put_path peers on the PUT path (or NULL if not recorded)
2307 * [0] = origin, [length - 1] = datastore
2308 * @param put_path_length number of entries in @a put_path
2309 * @param type type of the result
2310 * @param size number of bytes in data
2311 * @param data pointer to the result data
2314 handle_dht_response (void *cls,
2315 struct GNUNET_TIME_Absolute exp,
2316 const struct GNUNET_HashCode *key,
2317 const struct GNUNET_PeerIdentity *get_path,
2318 unsigned int get_path_length,
2319 const struct GNUNET_PeerIdentity *put_path,
2320 unsigned int put_path_length,
2321 enum GNUNET_BLOCK_Type type,
2325 struct GNS_ResolverHandle *rh = cls;
2326 struct AuthorityChain *ac = rh->ac_tail;
2327 const struct GNUNET_GNSRECORD_Block *block;
2328 struct CacheOps *co;
2333 (void) get_path_length;
2335 (void) put_path_length;
2337 GNUNET_DHT_get_stop (rh->get_handle);
2338 rh->get_handle = NULL;
2339 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
2340 rh->dht_heap_node = NULL;
2341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2342 "Handling response from the DHT\n");
2343 if (size < sizeof (struct GNUNET_GNSRECORD_Block))
2345 /* how did this pass DHT block validation!? */
2347 fail_resolution (rh);
2352 ntohl (block->purpose.size) +
2353 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
2354 sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
2356 /* how did this pass DHT block validation!? */
2358 fail_resolution (rh);
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2362 "Decrypting DHT block of size %u for `%s', expires %s\n",
2363 ntohl (block->purpose.size),
2365 GNUNET_STRINGS_absolute_time_to_string (exp));
2367 GNUNET_GNSRECORD_block_decrypt (block,
2368 &ac->authority_info.gns_authority,
2370 &handle_gns_resolution_result,
2373 GNUNET_break_op (0); /* block was ill-formed */
2374 fail_resolution (rh);
2377 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us)
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2380 "Received expired block from the DHT, will not cache it.\n");
2383 if (GNUNET_YES == disable_cache)
2385 /* Cache well-formed blocks */
2386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2387 "Caching response from the DHT in namecache\n");
2388 co = GNUNET_new (struct CacheOps);
2389 co->namecache_qe_cache = GNUNET_NAMECACHE_block_cache (namecache_handle,
2391 &namecache_cache_continuation,
2393 GNUNET_CONTAINER_DLL_insert (co_head,
2400 * Initiate a DHT query for a set of GNS records.
2402 * @param rh resolution handle
2403 * @param query key to use in the DHT lookup
2406 start_dht_request (struct GNS_ResolverHandle *rh,
2407 const struct GNUNET_HashCode *query)
2409 struct GNS_ResolverHandle *rx;
2411 GNUNET_assert (NULL == rh->get_handle);
2412 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
2413 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2415 DHT_GNS_REPLICATION_LEVEL,
2416 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2418 &handle_dht_response, rh);
2419 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2421 GNUNET_TIME_absolute_get ().abs_value_us);
2422 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
2424 /* fail longest-standing DHT request */
2425 rx = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2426 rx->dht_heap_node = NULL;
2427 GNUNET_assert (NULL != rx);
2428 fail_resolution (rx);
2434 * Process a records that were decrypted from a block that we got from
2435 * the namecache. Simply calls #handle_gns_resolution_result().
2437 * @param cls closure with the `struct GNS_ResolverHandle`
2438 * @param rd_count number of entries in @a rd array
2439 * @param rd array of records with data to store
2442 handle_gns_namecache_resolution_result (void *cls,
2443 unsigned int rd_count,
2444 const struct GNUNET_GNSRECORD_Data *rd)
2446 struct GNS_ResolverHandle *rh = cls;
2449 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2450 _("GNS namecache returned empty result for `%s'\n"),
2452 handle_gns_resolution_result (rh,
2459 * Process a record that was stored in the namecache.
2461 * @param cls closure with the `struct GNS_ResolverHandle`
2462 * @param block block that was stored in the namecache
2465 handle_namecache_block_response (void *cls,
2466 const struct GNUNET_GNSRECORD_Block *block)
2468 struct GNS_ResolverHandle *rh = cls;
2469 struct AuthorityChain *ac = rh->ac_tail;
2470 const char *label = ac->label;
2471 const struct GNUNET_CRYPTO_EcdsaPublicKey *auth = &ac->authority_info.gns_authority;
2472 struct GNUNET_HashCode query;
2474 GNUNET_assert (NULL != rh->namecache_qe);
2475 rh->namecache_qe = NULL;
2476 if ( ( (GNUNET_GNS_LO_DEFAULT == rh->options) ||
2477 ( (GNUNET_GNS_LO_LOCAL_MASTER == rh->options) &&
2478 (ac != rh->ac_head) ) ) &&
2479 ( (NULL == block) ||
2480 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
2482 /* namecache knows nothing; try DHT lookup */
2483 GNUNET_GNSRECORD_query_from_public_key (auth,
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2487 "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
2489 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
2490 GNUNET_h2s (&query));
2491 start_dht_request (rh, &query);
2495 if ( (NULL == block) ||
2496 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
2498 /* DHT not permitted and no local result, fail */
2499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2500 "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
2502 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2503 fail_resolution (rh);
2506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2507 "Received result from namecache for label `%s'\n",
2511 GNUNET_GNSRECORD_block_decrypt (block,
2514 &handle_gns_namecache_resolution_result,
2517 GNUNET_break_op (0); /* block was ill-formed */
2518 /* try DHT instead */
2519 GNUNET_GNSRECORD_query_from_public_key (auth,
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2523 "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
2525 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
2526 GNUNET_h2s (&query));
2527 start_dht_request (rh, &query);
2534 * Lookup tail of our authority chain in the namecache.
2536 * @param rh query we are processing
2539 recursive_gns_resolution_namecache (struct GNS_ResolverHandle *rh)
2541 struct AuthorityChain *ac = rh->ac_tail;
2542 struct GNUNET_HashCode query;
2544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2545 "Starting GNS resolution for `%s' in zone %s\n",
2547 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2548 GNUNET_GNSRECORD_query_from_public_key (&ac->authority_info.gns_authority,
2551 if (GNUNET_YES != disable_cache)
2554 = GNUNET_NAMECACHE_lookup_block (namecache_handle,
2556 &handle_namecache_block_response,
2558 GNUNET_assert (NULL != rh->namecache_qe);
2562 start_dht_request (rh,
2569 * Function called with the result from a revocation check.
2571 * @param cls the `struct GNS_ResovlerHandle`
2572 * @param is_valid #GNUNET_YES if the zone was not yet revoked
2575 handle_revocation_result (void *cls,
2578 struct GNS_ResolverHandle *rh = cls;
2579 struct AuthorityChain *ac = rh->ac_tail;
2581 rh->rev_check = NULL;
2582 if (GNUNET_YES != is_valid)
2584 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2585 _("Zone %s was revoked, resolution fails\n"),
2586 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2587 fail_resolution (rh);
2590 recursive_gns_resolution_namecache (rh);
2595 * Perform revocation check on tail of our authority chain.
2597 * @param rh query we are processing
2600 recursive_gns_resolution_revocation (struct GNS_ResolverHandle *rh)
2602 struct AuthorityChain *ac = rh->ac_tail;
2604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2605 "Starting revocation check for zone %s\n",
2606 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2607 rh->rev_check = GNUNET_REVOCATION_query (cfg,
2608 &ac->authority_info.gns_authority,
2609 &handle_revocation_result,
2611 GNUNET_assert (NULL != rh->rev_check);
2616 * Task scheduled to continue with the resolution process.
2618 * @param cls the `struct GNS_ResolverHandle` of the resolution
2621 recursive_resolution (void *cls)
2623 struct GNS_ResolverHandle *rh = cls;
2626 if (MAX_RECURSION < rh->loop_limiter++)
2628 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2629 "Encountered unbounded recursion resolving `%s'\n",
2631 fail_resolution (rh);
2634 if (GNUNET_YES == rh->ac_tail->gns_authority)
2635 recursive_gns_resolution_revocation (rh);
2637 recursive_dns_resolution (rh);
2642 * Begin the resolution process from 'name', starting with
2643 * the identification of the zone specified by 'name'.
2645 * @param cls the `struct GNS_ResolverHandle`
2648 start_resolver_lookup (void *cls)
2650 struct GNS_ResolverHandle *rh = cls;
2651 struct AuthorityChain *ac;
2656 if (1 == inet_pton (AF_INET,
2660 /* name is IPv4 address, pretend it's an A record */
2661 struct GNUNET_GNSRECORD_Data rd;
2664 rd.data_size = sizeof (v4);
2665 rd.expiration_time = UINT64_MAX;
2666 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
2668 rh->proc (rh->proc_cls,
2671 GNUNET_assert (NULL == rh->task_id);
2672 rh->task_id = GNUNET_SCHEDULER_add_now (&GNS_resolver_lookup_cancel_,
2676 if (1 == inet_pton (AF_INET6,
2680 /* name is IPv6 address, pretend it's an AAAA record */
2681 struct GNUNET_GNSRECORD_Data rd;
2684 rd.data_size = sizeof (v6);
2685 rd.expiration_time = UINT64_MAX;
2686 rd.record_type = GNUNET_DNSPARSER_TYPE_AAAA;
2688 rh->proc (rh->proc_cls,
2691 GNUNET_assert (NULL == rh->task_id);
2692 rh->task_id = GNUNET_SCHEDULER_add_now (&GNS_resolver_lookup_cancel_,
2697 ac = GNUNET_new (struct AuthorityChain);
2699 ac->label = resolver_lookup_get_next_label (rh);
2700 if (NULL == ac->label)
2701 /* name was just the "TLD", so we default to label
2702 #GNUNET_GNS_EMPTY_LABEL_AT */
2703 ac->label = GNUNET_strdup (GNUNET_GNS_EMPTY_LABEL_AT);
2704 ac->gns_authority = GNUNET_YES;
2705 ac->authority_info.gns_authority = rh->authority_zone;
2706 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
2709 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
2715 * Lookup of a record in a specific zone calls lookup result processor
2718 * @param zone the zone to perform the lookup in
2719 * @param record_type the record type to look up
2720 * @param name the name to look up
2721 * @param options local options to control local lookup
2722 * @param proc the processor to call on result
2723 * @param proc_cls the closure to pass to @a proc
2724 * @return handle to cancel operation
2726 struct GNS_ResolverHandle *
2727 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
2728 uint32_t record_type,
2730 enum GNUNET_GNS_LocalOptions options,
2731 GNS_ResultProcessor proc,
2734 struct GNS_ResolverHandle *rh;
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2737 "Starting lookup for `%s'\n",
2739 rh = GNUNET_new (struct GNS_ResolverHandle);
2740 GNUNET_CONTAINER_DLL_insert (rlh_head,
2743 rh->authority_zone = *zone;
2745 rh->proc_cls = proc_cls;
2746 rh->options = options;
2747 rh->record_type = record_type;
2748 rh->name = GNUNET_strdup (name);
2749 rh->name_resolution_pos = strlen (name);
2750 rh->task_id = GNUNET_SCHEDULER_add_now (&start_resolver_lookup,
2757 * Cancel active resolution (i.e. client disconnected).
2759 * @param rh resolution to abort
2762 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
2764 struct DnsResult *dr;
2765 struct AuthorityChain *ac;
2766 struct VpnContext *vpn_ctx;
2768 GNUNET_CONTAINER_DLL_remove (rlh_head,
2771 if (NULL != rh->dns_request)
2773 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
2774 rh->dns_request = NULL;
2776 while (NULL != (ac = rh->ac_head))
2778 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
2781 if (GNUNET_NO == ac->gns_authority)
2783 struct Gns2DnsPending *gp;
2785 while (NULL != (gp = ac->authority_info.dns_authority.gp_head))
2787 GNUNET_CONTAINER_DLL_remove (ac->authority_info.dns_authority.gp_head,
2788 ac->authority_info.dns_authority.gp_tail,
2792 /* rh->g2dc->rh is NOT in the DLL yet, so to enable us
2793 using GNS_resolver_lookup_cancel here, we need to
2795 GNUNET_CONTAINER_DLL_insert (rlh_head,
2798 GNUNET_assert (NULL == gp->rh->task_id);
2799 gp->rh->task_id = GNUNET_SCHEDULER_add_now (&GNS_resolver_lookup_cancel_,
2803 if (NULL != gp->dns_rh)
2805 GNUNET_RESOLVER_request_cancel (gp->dns_rh);
2810 GNUNET_DNSSTUB_stop (ac->authority_info.dns_authority.dns_handle);
2812 GNUNET_free (ac->label);
2815 if (NULL != rh->task_id)
2817 GNUNET_SCHEDULER_cancel (rh->task_id);
2820 if (NULL != rh->get_handle)
2822 GNUNET_DHT_get_stop (rh->get_handle);
2823 rh->get_handle = NULL;
2825 if (NULL != rh->dht_heap_node)
2827 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
2828 rh->dht_heap_node = NULL;
2830 if (NULL != (vpn_ctx = rh->vpn_ctx))
2832 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
2833 GNUNET_free (vpn_ctx->rd_data);
2834 GNUNET_free (vpn_ctx);
2836 if (NULL != rh->namecache_qe)
2838 GNUNET_NAMECACHE_cancel (rh->namecache_qe);
2839 rh->namecache_qe = NULL;
2841 if (NULL != rh->rev_check)
2843 GNUNET_REVOCATION_query_cancel (rh->rev_check);
2844 rh->rev_check = NULL;
2846 if (NULL != rh->std_resolve)
2848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2849 "Canceling standard DNS resolution\n");
2850 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
2851 rh->std_resolve = NULL;
2853 while (NULL != (dr = rh->dns_result_head))
2855 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
2856 rh->dns_result_tail,
2860 GNUNET_free_non_null (rh->leho);
2861 GNUNET_free (rh->name);
2866 /* ***************** Resolver initialization ********************* */
2870 * Initialize the resolver
2872 * @param nc the namecache handle
2873 * @param dht the dht handle
2874 * @param c configuration handle
2875 * @param max_bg_queries maximum number of parallel background queries in dht
2878 GNS_resolver_init (struct GNUNET_NAMECACHE_Handle *nc,
2879 struct GNUNET_DHT_Handle *dht,
2880 const struct GNUNET_CONFIGURATION_Handle *c,
2881 unsigned long long max_bg_queries)
2884 namecache_handle = nc;
2887 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2888 max_allowed_background_queries = max_bg_queries;
2889 disable_cache = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2892 if (GNUNET_YES == disable_cache)
2893 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2894 "Namecache disabled\n");
2895 vpn_handle = GNUNET_VPN_connect (cfg);
2903 GNS_resolver_done ()
2905 struct GNS_ResolverHandle *rh;
2906 struct CacheOps *co;
2908 /* abort active resolutions */
2909 while (NULL != (rh = rlh_head))
2911 rh->proc (rh->proc_cls,
2914 GNS_resolver_lookup_cancel (rh);
2916 while (NULL != (co = co_head))
2918 GNUNET_CONTAINER_DLL_remove (co_head,
2921 GNUNET_NAMECACHE_cancel (co->namecache_qe_cache);
2924 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
2925 dht_lookup_heap = NULL;
2926 GNUNET_VPN_disconnect (vpn_handle);
2929 namecache_handle = NULL;
2933 /* end of gnunet-service-gns_resolver.c */