2 This file is part of GNUnet.
3 Copyright (C) 2011-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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_namestore_service.h"
34 #include "gnunet_dns_service.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_revocation_service.h"
37 #include "gnunet_dnsparser_lib.h"
38 #include "gnunet_tun_lib.h"
39 #include "gnunet_gns_service.h"
41 #include "gnunet-service-gns_resolver.h"
42 #include "gnunet-service-gns_shorten.h"
43 #include "gnunet_vpn_service.h"
47 * Default DHT timeout for lookups.
49 #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
52 * Default timeout for DNS lookups.
54 #define DNS_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
57 * Default timeout for VPN redirections.
59 #define VPN_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
62 * DHT replication level
64 #define DHT_GNS_REPLICATION_LEVEL 5
67 * How deep do we allow recursions to go before we abort?
69 #define MAX_RECURSION 256
73 * DLL to hold the authority chain we had to pass in the resolution
81 struct AuthorityChain *prev;
86 struct AuthorityChain *next;
89 * Resolver handle this entry in the chain belongs to.
91 struct GNS_ResolverHandle *rh;
94 * label/name corresponding to the authority
99 * label/name suggested for shortening to the authority
101 char *suggested_shortening_label;
104 * Do we already try to shorten this authority?
106 int shortening_started;
109 * #GNUNET_YES if the authority was a GNS authority,
110 * #GNUNET_NO if the authority was a DNS authority.
115 * Information about the resolver authority for this label.
121 * The zone of the GNS authority
123 struct GNUNET_CRYPTO_EcdsaPublicKey gns_authority;
128 * Domain of the DNS resolver that is the authority.
129 * (appended to construct the DNS name to resolve;
130 * this is NOT the DNS name of the DNS server!).
132 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
135 * IP address of the DNS resolver that is authoritative.
136 * (this implementation currently only supports one
139 struct sockaddr_storage dns_ip;
149 * A result we got from DNS.
157 struct DnsResult *next;
162 struct DnsResult *prev;
165 * Binary value stored in the DNS record (appended to this struct)
170 * Expiration time for the DNS record, 0 if we didn't
171 * get anything useful (i.e. 'gethostbyname()' was used).
173 uint64_t expiration_time;
176 * Number of bytes in @e data.
181 * Type of the GNS/DNS record.
183 uint32_t record_type;
189 * Closure for #vpn_allocation_cb.
195 * Which resolution process are we processing.
197 struct GNS_ResolverHandle *rh;
200 * Handle to the VPN request that we were performing.
202 struct GNUNET_VPN_RedirectionRequest *vpn_request;
205 * Number of records serialized in @e rd_data.
207 unsigned int rd_count;
210 * Serialized records.
215 * Number of bytes in @e rd_data.
222 * Information we keep during the resolution of an
223 * IP address for a DNS server while handling a
226 struct Gns2DnsContext
230 * DNS domain in which the resolution will continue
231 * (first part of the GNS2DNS record).
236 * Handle for the resolution of the IP part of the
237 * GNS2DNS record. Will return to us the addresses
238 * of the DNS resolver to use.
240 struct GNS_ResolverHandle *rh;
246 * Handle to a currenty pending resolution. On result (positive or
247 * negative) the #GNS_ResultProcessor is called.
249 struct GNS_ResolverHandle
255 struct GNS_ResolverHandle *next;
260 struct GNS_ResolverHandle *prev;
263 * The top-level GNS authoritative zone to query
265 struct GNUNET_CRYPTO_EcdsaPublicKey authority_zone;
268 * called when resolution phase finishes
270 GNS_ResultProcessor proc;
273 * closure passed to @e proc
278 * Handle used during GNS2DNS resolution for looking up the
279 * IP address of the DNS server.
281 struct Gns2DnsContext *g2dc;
284 * Handle for DHT lookups. should be NULL if no lookups are in progress
286 struct GNUNET_DHT_GetHandle *get_handle;
289 * Handle to a VPN request, NULL if none is active.
291 struct VpnContext *vpn_ctx;
294 * Socket for a DNS request, NULL if none is active.
296 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
299 * Handle for standard DNS resolution, NULL if none is active.
301 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
304 * Pending Namecache lookup task
306 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe;
309 * Pending revocation check.
311 struct GNUNET_REVOCATION_Query *rev_check;
314 * Heap node associated with this lookup. Used to limit number of
315 * concurrent requests.
317 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
320 * DLL to store the authority chain
322 struct AuthorityChain *ac_head;
325 * DLL to store the authority chain
327 struct AuthorityChain *ac_tail;
330 * Private key of the shorten zone, NULL to not shorten.
332 struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key;
335 * ID of a task associated with the resolution process.
337 struct GNUNET_SCHEDULER_Task * task_id;
340 * The name to resolve
345 * DLL of results we got from DNS.
347 struct DnsResult *dns_result_head;
350 * DLL of results we got from DNS.
352 struct DnsResult *dns_result_tail;
355 * Current offset in 'name' where we are resolving.
357 size_t name_resolution_pos;
362 enum GNUNET_GNS_LocalOptions options;
365 * For SRV and TLSA records, the number of the
366 * protocol specified in the name. 0 if no protocol was given.
371 * For SRV and TLSA records, the number of the
372 * service specified in the name. 0 if no service was given.
377 * Desired type for the resolution.
382 * We increment the loop limiter for each step in a recursive
383 * resolution. If it passes our threshold (i.e. due to
384 * self-recursion in the resolution, i.e CNAME fun), we stop.
386 unsigned int loop_limiter;
392 * Active namestore caching operations.
398 * Organized in a DLL.
400 struct CacheOps *next;
403 * Organized in a DLL.
405 struct CacheOps *prev;
408 * Pending Namestore caching task.
410 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe_cache;
416 * Our handle to the namecache service
418 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
421 * Our handle to the vpn service
423 static struct GNUNET_VPN_Handle *vpn_handle;
426 * Resolver handle to the dht
428 static struct GNUNET_DHT_Handle *dht_handle;
431 * Handle to perform DNS lookups.
433 static struct GNUNET_DNSSTUB_Context *dns_handle;
436 * Heap for limiting parallel DHT lookups
438 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
441 * Maximum amount of parallel queries to the DHT
443 static unsigned long long max_allowed_background_queries;
446 * Head of resolver lookup list
448 static struct GNS_ResolverHandle *rlh_head;
451 * Tail of resolver lookup list
453 static struct GNS_ResolverHandle *rlh_tail;
456 * Organized in a DLL.
458 static struct CacheOps *co_head;
461 * Organized in a DLL.
463 static struct CacheOps *co_tail;
468 static int use_cache;
471 * Global configuration.
473 static const struct GNUNET_CONFIGURATION_Handle *cfg;
477 * Determine if this name is canonical (is a legal name in a zone, without delegation);
478 * note that we do not test that the name does not contain illegal characters, we only
479 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
480 * even though they consist of multiple labels.
483 * a.b.gnu = not canonical
485 * _foo._srv = canonical
486 * _f.bar = not canonical
488 * @param name the name to test
489 * @return #GNUNET_YES if canonical
492 is_canonical (const char *name)
497 if (NULL == strchr (name, '.'))
502 while (NULL != (dot = strchr (pos, '.')))
510 /* ************************** Resolution **************************** */
513 * Expands a name ending in .+ with the zone of origin.
515 * @param rh resolution context
516 * @param name name to modify (to be free'd or returned)
517 * @return updated name
520 translate_dot_plus (struct GNS_ResolverHandle *rh,
524 size_t s_len = strlen (name);
526 if (0 != strcmp (&name[s_len - 2],
528 return name; /* did not end in ".+" */
529 GNUNET_assert (GNUNET_YES == rh->ac_tail->gns_authority);
530 GNUNET_asprintf (&ret,
534 GNUNET_GNSRECORD_pkey_to_zkey (&rh->ac_tail->authority_info.gns_authority));
541 * Task scheduled to asynchronously fail a resolution.
543 * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
544 * @param tc task context
547 fail_resolution (void *cls,
548 const struct GNUNET_SCHEDULER_TaskContext *tc)
550 struct GNS_ResolverHandle *rh = cls;
553 rh->proc (rh->proc_cls, 0, NULL);
554 GNS_resolver_lookup_cancel (rh);
558 #if (defined WINDOWS) || (defined DARWIN)
559 /* Don't have this on W32, here's a naive implementation
560 * Was somehow removed on OS X ... */
562 memrchr (const void *s,
566 const unsigned char *ucs = s;
569 for (i = n - 1; i >= 0; i--)
570 if (c == (int) ucs[i])
571 return (void *) &ucs[i];
578 * Get the next, rightmost label from the name that we are trying to resolve,
579 * and update the resolution position accordingly. Labels usually consist
580 * of up to 63 characters without a period ("."); however, we use a special
581 * convention to support SRV and TLSA records where the domain name
582 * includes an encoding for a service and protocol in the name. The
583 * syntax (see RFC 2782) here is "_Service._Proto.Name" and in this
584 * special case we include the "_Service._Proto" in the rightmost label.
585 * Thus, for "_443._tcp.foo.bar" we first return the label "bar" and then
586 * the label "_443._tcp.foo". The special case is detected by the
587 * presence of labels beginning with an underscore. Whenever a label
588 * begins with an underscore, it is combined with the label to its right
589 * (and the "." is preserved).
591 * @param rh handle to the resolution operation to get the next label from
592 * @return NULL if there are no more labels
595 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
606 if (0 == rh->name_resolution_pos)
608 dot = memrchr (rh->name,
610 rh->name_resolution_pos);
613 /* done, this was the last one */
614 len = rh->name_resolution_pos;
616 rh->name_resolution_pos = 0;
620 /* advance by one label */
621 len = rh->name_resolution_pos - (dot - rh->name) - 1;
623 rh->name_resolution_pos = dot - rh->name;
627 ret = GNUNET_strndup (rp, len);
628 /* If we have labels starting with underscore with label on
629 * the right (SRV/DANE/BOX case), determine port/protocol;
630 * The format of `rh->name` must be "_PORT._PROTOCOL".
632 if ( ('_' == rh->name[0]) &&
633 (NULL != (dot = memrchr (rh->name,
635 rh->name_resolution_pos))) &&
637 (NULL == memrchr (rh->name,
641 srv_name = GNUNET_strndup (&rh->name[1],
642 (dot - rh->name) - 1);
643 proto_name = GNUNET_strndup (&dot[2],
644 rh->name_resolution_pos - (dot - rh->name) - 1);
645 rh->name_resolution_pos = 0;
646 pe = getprotobyname (proto_name);
649 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
650 _("Protocol `%s' unknown, skipping labels.\n"),
652 GNUNET_free (proto_name);
653 GNUNET_free (srv_name);
656 se = getservbyname (srv_name,
660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
661 _("Service `%s' unknown for protocol `%s', skipping labels.\n"),
664 GNUNET_free (proto_name);
665 GNUNET_free (srv_name);
668 rh->protocol = pe->p_proto;
669 rh->service = se->s_port;
676 * Gives the cummulative result obtained to the callback and clean up the request.
678 * @param rh resolution process that has culminated in a result
681 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
683 struct DnsResult *pos;
688 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
691 struct GNUNET_GNSRECORD_Data rd[n];
694 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
696 rd[i].data = pos->data;
697 rd[i].data_size = pos->data_size;
698 rd[i].record_type = pos->record_type;
699 if (0 == pos->expiration_time)
701 rd[i].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
702 rd[i].expiration_time = 0;
706 rd[i].flags = GNUNET_GNSRECORD_RF_NONE;
707 rd[i].expiration_time = pos->expiration_time;
711 GNUNET_assert (i == n);
712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
713 "Transmitting standard DNS result with %u records\n",
715 rh->proc (rh->proc_cls,
719 GNS_resolver_lookup_cancel (rh);
724 * Add a result from DNS to the records to be returned to the application.
726 * @param rh resolution request to extend with a result
727 * @param expiration_time expiration time for the answer
728 * @param record_type DNS record type of the answer
729 * @param data_size number of bytes in @a data
730 * @param data binary data to return in DNS record
733 add_dns_result (struct GNS_ResolverHandle *rh,
734 uint64_t expiration_time,
735 uint32_t record_type,
739 struct DnsResult *res;
741 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
742 res->expiration_time = expiration_time;
743 res->data_size = data_size;
744 res->record_type = record_type;
746 memcpy (&res[1], data, data_size);
747 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
754 * We had to do a DNS lookup. Convert the result (if any) and return
757 * @param cls closure with the `struct GNS_ResolverHandle`
758 * @param addr one of the addresses of the host, NULL for the last address
759 * @param addrlen length of the address
762 handle_dns_result (void *cls,
763 const struct sockaddr *addr,
766 struct GNS_ResolverHandle *rh = cls;
767 const struct sockaddr_in *sa4;
768 const struct sockaddr_in6 *sa6;
772 rh->std_resolve = NULL;
773 transmit_lookup_dns_result (rh);
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777 "Received %u bytes of DNS IP data\n",
779 switch (addr->sa_family)
782 sa4 = (const struct sockaddr_in *) addr;
784 0 /* expiration time is unknown */,
785 GNUNET_DNSPARSER_TYPE_A,
786 sizeof (struct in_addr),
790 sa6 = (const struct sockaddr_in6 *) addr;
792 0 /* expiration time is unknown */,
793 GNUNET_DNSPARSER_TYPE_AAAA,
794 sizeof (struct in6_addr),
805 * Task scheduled to continue with the resolution process.
807 * @param cls the 'struct GNS_ResolverHandle' of the resolution
808 * @param tc task context
811 recursive_resolution (void *cls,
812 const struct GNUNET_SCHEDULER_TaskContext *tc);
816 * Begin the resolution process from 'name', starting with
817 * the identification of the zone specified by 'name'.
819 * @param rh resolution to perform
822 start_resolver_lookup (struct GNS_ResolverHandle *rh);
826 * Function called with the result of a DNS resolution.
828 * @param cls the request handle of the resolution that
829 * we were attempting to make
830 * @param rs socket that received the response
831 * @param dns dns response, never NULL
832 * @param dns_len number of bytes in @a dns
835 dns_result_parser (void *cls,
836 struct GNUNET_DNSSTUB_RequestSocket *rs,
837 const struct GNUNET_TUN_DnsHeader *dns,
840 struct GNS_ResolverHandle *rh = cls;
841 struct GNUNET_DNSPARSER_Packet *p;
842 const struct GNUNET_DNSPARSER_Record *rec;
843 unsigned int rd_count;
846 rh->dns_request = NULL;
847 GNUNET_SCHEDULER_cancel (rh->task_id);
849 p = GNUNET_DNSPARSER_parse ((const char *) dns,
853 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
854 _("Failed to parse DNS response\n"));
855 rh->proc (rh->proc_cls, 0, NULL);
856 GNS_resolver_lookup_cancel (rh);
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "Received DNS response for `%s' with %u answers\n",
862 (unsigned int) p->num_answers);
863 if ( (p->num_answers > 0) &&
864 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
865 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
867 GNUNET_free (rh->name);
868 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
869 rh->name_resolution_pos = strlen (rh->name);
870 start_resolver_lookup (rh);
871 GNUNET_DNSPARSER_free_packet (p);
875 /* convert from (parsed) DNS to (binary) GNS format! */
876 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
878 struct GNUNET_GNSRECORD_Data rd[rd_count];
880 char buf[UINT16_MAX];
886 memset (rd, 0, sizeof (rd));
887 for (i=0;i<rd_count;i++)
889 if (i < p->num_answers)
890 rec = &p->answers[i];
891 else if (i < p->num_answers + p->num_authority_records)
892 rec = &p->authority_records[i - p->num_answers];
894 rec = &p->additional_records[i - p->num_answers - p->num_authority_records];
895 /* As we copied the full DNS name to 'rh->ac_tail->label', this
896 should be the correct check to see if this record is actually
897 a record for our label... */
898 if (0 != strcmp (rec->name,
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
902 "Dropping record `%s', does not match desired name `%s'\n",
908 rd[i - skip].record_type = rec->type;
909 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
912 case GNUNET_DNSPARSER_TYPE_A:
913 if (rec->data.raw.data_len != sizeof (struct in_addr))
919 rd[i - skip].data_size = rec->data.raw.data_len;
920 rd[i - skip].data = rec->data.raw.data;
922 case GNUNET_DNSPARSER_TYPE_AAAA:
923 if (rec->data.raw.data_len != sizeof (struct in6_addr))
929 rd[i - skip].data_size = rec->data.raw.data_len;
930 rd[i - skip].data = rec->data.raw.data;
932 case GNUNET_DNSPARSER_TYPE_CNAME:
933 case GNUNET_DNSPARSER_TYPE_PTR:
934 case GNUNET_DNSPARSER_TYPE_NS:
937 GNUNET_DNSPARSER_builder_add_name (buf,
946 rd[i - skip].data_size = buf_off - buf_start;
947 rd[i - skip].data = &buf[buf_start];
949 case GNUNET_DNSPARSER_TYPE_SOA:
952 GNUNET_DNSPARSER_builder_add_soa (buf,
961 rd[i - skip].data_size = buf_off - buf_start;
962 rd[i - skip].data = &buf[buf_start];
964 case GNUNET_DNSPARSER_TYPE_MX:
967 GNUNET_DNSPARSER_builder_add_mx (buf,
976 rd[i - skip].data_size = buf_off - buf_start;
977 rd[i - skip].data = &buf[buf_start];
979 case GNUNET_DNSPARSER_TYPE_SRV:
982 GNUNET_DNSPARSER_builder_add_srv (buf,
991 rd[i - skip].data_size = buf_off - buf_start;
992 rd[i - skip].data = &buf[buf_start];
995 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
996 _("Skipping record of unsupported type %d\n"),
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1003 "Returning DNS response for `%s' with %u answers\n",
1005 (unsigned int) p->num_answers);
1006 rh->proc (rh->proc_cls, rd_count - skip, rd);
1007 GNS_resolver_lookup_cancel (rh);
1009 GNUNET_DNSPARSER_free_packet (p);
1014 * Perform recursive DNS resolution. Asks the given DNS resolver to
1015 * resolve "rh->dns_name", possibly recursively proceeding following
1016 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
1017 * we find the answer.
1019 * @param rh resolution information
1022 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
1024 struct AuthorityChain *ac;
1026 struct GNUNET_DNSPARSER_Query *query;
1027 struct GNUNET_DNSPARSER_Packet *p;
1029 size_t dns_request_length;
1032 GNUNET_assert (NULL != ac);
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1034 "Starting DNS lookup for `%s'\n",
1036 GNUNET_assert (GNUNET_NO == ac->gns_authority);
1037 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
1040 sa_len = sizeof (struct sockaddr_in);
1043 sa_len = sizeof (struct sockaddr_in6);
1047 rh->proc (rh->proc_cls, 0, NULL);
1048 GNS_resolver_lookup_cancel (rh);
1051 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
1052 query->name = GNUNET_strdup (ac->label);
1053 query->type = rh->record_type;
1054 query->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1055 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
1058 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1060 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
1061 p->flags.recursion_desired = 1;
1063 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
1066 rh->proc (rh->proc_cls, 0, NULL);
1067 GNS_resolver_lookup_cancel (rh);
1071 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
1072 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
1078 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
1082 GNUNET_free (dns_request);
1083 GNUNET_DNSPARSER_free_packet (p);
1088 * We encountered a CNAME record during our resolution.
1089 * Merge it into our chain.
1091 * @param rh resolution we are performing
1092 * @param cname value of the cname record we got for the current
1093 * authority chain tail
1096 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
1101 struct AuthorityChain *ac;
1103 nlen = strlen (cname);
1106 &cname[nlen - 2])) )
1108 /* CNAME resolution continues relative to current domain */
1109 if (0 == rh->name_resolution_pos)
1111 res = GNUNET_strndup (cname, nlen - 2);
1112 rh->name_resolution_pos = nlen - 2;
1116 GNUNET_asprintf (&res,
1118 (int) rh->name_resolution_pos,
1122 rh->name_resolution_pos = strlen (res);
1124 GNUNET_free (rh->name);
1126 ac = GNUNET_new (struct AuthorityChain);
1128 ac->gns_authority = GNUNET_YES;
1129 ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
1130 ac->label = resolver_lookup_get_next_label (rh);
1131 ac->suggested_shortening_label = NULL;
1132 ac->shortening_started = GNUNET_NO;
1133 /* add AC to tail */
1134 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1137 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1141 /* name is absolute, start from the beginning */
1142 GNUNET_free (rh->name);
1143 rh->name = GNUNET_strdup (cname);
1144 rh->name_resolution_pos = strlen (rh->name);
1145 start_resolver_lookup (rh);
1150 * Process a records that were decrypted from a block.
1152 * @param cls closure with the 'struct GNS_ResolverHandle'
1153 * @param rd_count number of entries in @a rd array
1154 * @param rd array of records with data to store
1157 handle_gns_resolution_result (void *cls,
1158 unsigned int rd_count,
1159 const struct GNUNET_GNSRECORD_Data *rd);
1163 * Callback invoked from the VPN service once a redirection is
1164 * available. Provides the IP address that can now be used to
1165 * reach the requested destination. Replaces the "VPN" record
1166 * with the respective A/AAAA record and continues processing.
1168 * @param cls closure
1169 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1170 * will match 'result_af' from the request
1171 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1172 * that the VPN allocated for the redirection;
1173 * traffic to this IP will now be redirected to the
1174 * specified target peer; NULL on error
1177 vpn_allocation_cb (void *cls,
1179 const void *address)
1181 struct VpnContext *vpn_ctx = cls;
1182 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1183 struct GNUNET_GNSRECORD_Data rd[vpn_ctx->rd_count];
1186 vpn_ctx->vpn_request = NULL;
1188 GNUNET_assert (GNUNET_OK ==
1189 GNUNET_GNSRECORD_records_deserialize (vpn_ctx->rd_data_size,
1193 for (i=0;i<vpn_ctx->rd_count;i++)
1195 if (GNUNET_GNSRECORD_TYPE_VPN == rd[i].record_type)
1200 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1201 rd[i].data_size = sizeof (struct in_addr);
1202 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1204 rd[i].data = address;
1207 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1208 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1210 rd[i].data = address;
1211 rd[i].data_size = sizeof (struct in6_addr);
1219 GNUNET_assert (i < vpn_ctx->rd_count);
1220 handle_gns_resolution_result (rh,
1223 GNUNET_free (vpn_ctx->rd_data);
1224 GNUNET_free (vpn_ctx);
1229 * We've resolved the IP address for the DNS resolver to use
1230 * after encountering a GNS2DNS record.
1232 * TODO: Right now we only foward the request to ONE DNS resolver,
1233 * even if we get multiple IP addresses back; a correct implementation
1234 * should try all DNS resolvers.
1236 * @param cls the `struct GNS_ResolverHandle` where we encountered
1237 * the GNS2DNS record
1238 * @param rd_count number of records in @a rd
1239 * @param rd addresses for the DNS resolver (presumably)
1242 handle_gns2dns_result (void *cls,
1243 unsigned int rd_count,
1244 const struct GNUNET_GNSRECORD_Data *rd)
1246 struct GNS_ResolverHandle *rh = cls;
1247 struct AuthorityChain *ac;
1249 struct sockaddr *sa;
1250 struct sockaddr_in v4;
1251 struct sockaddr_in6 v6;
1254 /* find suitable A/AAAA record */
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1256 "Received %u results for IP address of DNS server for GNS2DNS transition\n",
1258 /* enable cleanup of 'rh' handle that comes next... */
1259 GNUNET_CONTAINER_DLL_insert (rlh_head,
1262 rh->g2dc->rh = NULL;
1265 for (j=0;j<rd_count;j++)
1267 switch (rd[j].record_type)
1269 case GNUNET_DNSPARSER_TYPE_A:
1270 if (sizeof (struct in_addr) != rd[j].data_size)
1272 GNUNET_break_op (0);
1273 rh->proc (rh->proc_cls, 0, NULL);
1274 GNS_resolver_lookup_cancel (rh);
1277 /* FIXME: might want to check if we support IPv4 here,
1278 and otherwise skip this one and hope we find another */
1279 memset (&v4, 0, sizeof (v4));
1280 sa_len = sizeof (v4);
1281 v4.sin_family = AF_INET;
1282 v4.sin_port = htons (53);
1283 #if HAVE_SOCKADDR_IN_SIN_LEN
1284 v4.sin_len = (u_char) sa_len;
1286 memcpy (&v4.sin_addr,
1288 sizeof (struct in_addr));
1289 sa = (struct sockaddr *) &v4;
1291 case GNUNET_DNSPARSER_TYPE_AAAA:
1292 if (sizeof (struct in6_addr) != rd[j].data_size)
1294 GNUNET_break_op (0);
1295 rh->proc (rh->proc_cls, 0, NULL);
1296 GNS_resolver_lookup_cancel (rh);
1299 /* FIXME: might want to check if we support IPv6 here,
1300 and otherwise skip this one and hope we find another */
1301 memset (&v6, 0, sizeof (v6));
1302 sa_len = sizeof (v6);
1303 v6.sin6_family = AF_INET6;
1304 v6.sin6_port = htons (53);
1305 #if HAVE_SOCKADDR_IN_SIN_LEN
1306 v6.sin6_len = (u_char) sa_len;
1308 memcpy (&v6.sin6_addr,
1310 sizeof (struct in6_addr));
1311 sa = (struct sockaddr *) &v6;
1321 /* we cannot continue; NS without A/AAAA */
1322 rh->proc (rh->proc_cls, 0, NULL);
1323 GNS_resolver_lookup_cancel (rh);
1326 /* expand authority chain */
1327 ac = GNUNET_new (struct AuthorityChain);
1329 strcpy (ac->authority_info.dns_authority.name,
1331 memcpy (&ac->authority_info.dns_authority.dns_ip,
1334 /* for DNS recursion, the label is the full DNS name,
1335 created from the remainder of the GNS name and the
1336 name in the NS record */
1337 GNUNET_asprintf (&ac->label,
1339 (int) rh->name_resolution_pos,
1341 (0 != rh->name_resolution_pos) ? "." : "",
1343 GNUNET_free (rh->g2dc->ns);
1344 GNUNET_free (rh->g2dc);
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1347 "Will continue resolution using DNS server `%s' to resolve `%s'\n",
1348 GNUNET_a2s (sa, sa_len),
1350 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1353 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1355 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1356 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1358 rh->proc (rh->proc_cls, 0, NULL);
1359 GNS_resolver_lookup_cancel (rh);
1363 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1369 * Process a records that were decrypted from a block.
1371 * @param cls closure with the `struct GNS_ResolverHandle`
1372 * @param rd_count number of entries in @a rd array
1373 * @param rd array of records with data to store
1376 handle_gns_resolution_result (void *cls,
1377 unsigned int rd_count,
1378 const struct GNUNET_GNSRECORD_Data *rd)
1380 struct GNS_ResolverHandle *rh = cls;
1381 struct AuthorityChain *ac;
1382 struct AuthorityChain *shorten_ac;
1385 struct VpnContext *vpn_ctx;
1386 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1388 struct GNUNET_HashCode vhash;
1390 char scratch[UINT16_MAX];
1392 size_t scratch_start;
1394 struct GNUNET_GNSRECORD_Data rd_new[rd_count];
1395 unsigned int rd_off;
1397 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1398 "Resolution succeeded for `%s' in zone %s, got %u records\n",
1400 GNUNET_GNSRECORD_z2s (&rh->ac_tail->authority_info.gns_authority),
1402 if (0 == rh->name_resolution_pos)
1404 /* top-level match, are we done yet? */
1405 if ( (rd_count > 0) &&
1406 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1407 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1410 cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
1413 if ( (NULL == cname) ||
1414 (off != rd[0].data_size) )
1416 GNUNET_break_op (0);
1417 rh->proc (rh->proc_cls, 0, NULL);
1418 GNS_resolver_lookup_cancel (rh);
1419 GNUNET_free_non_null (cname);
1422 handle_gns_cname_result (rh,
1424 GNUNET_free (cname);
1427 /* If A/AAAA was requested, but we got a VPN
1428 record, we convert it to A/AAAA using GNUnet VPN */
1429 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1430 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1432 for (i=0;i<rd_count;i++)
1434 switch (rd[i].record_type)
1436 case GNUNET_GNSRECORD_TYPE_VPN:
1438 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1439 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) >
1442 GNUNET_break_op (0);
1443 rh->proc (rh->proc_cls, 0, NULL);
1444 GNS_resolver_lookup_cancel (rh);
1447 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1448 vname = (const char *) &vpn[1];
1449 if ('\0' != vname[rd[i].data_size - 1 - sizeof (struct GNUNET_TUN_GnsVpnRecord)])
1451 GNUNET_break_op (0);
1452 rh->proc (rh->proc_cls, 0, NULL);
1453 GNS_resolver_lookup_cancel (rh);
1456 GNUNET_TUN_service_name_to_hash (vname,
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1459 "Attempting VPN allocation for %s-%s (AF: %d, proto %d)\n",
1460 GNUNET_i2s (&vpn->peer),
1463 (int) ntohs (vpn->proto));
1464 vpn_ctx = GNUNET_new (struct VpnContext);
1465 rh->vpn_ctx = vpn_ctx;
1467 vpn_ctx->rd_data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
1469 vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
1470 vpn_ctx->rd_count = rd_count;
1471 GNUNET_assert (vpn_ctx->rd_data_size ==
1472 GNUNET_GNSRECORD_records_serialize (rd_count,
1474 vpn_ctx->rd_data_size,
1476 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1481 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1486 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1488 /* delegation to DNS */
1489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1490 "Found GNS2DNS record, delegating to DNS!\n");
1497 } /* end: name_resolution_pos */
1498 /* convert relative names in record values to absolute names,
1499 using 'scratch' array for memory allocations */
1502 shorten_ac = rh->ac_tail;
1503 for (i=0;i<rd_count;i++)
1505 if ( (0 != rh->protocol) &&
1506 (0 != rh->service) &&
1507 (GNUNET_GNSRECORD_TYPE_BOX != rd[i].record_type) )
1508 continue; /* we _only_ care about boxed records */
1510 rd_new[rd_off] = rd[i];
1511 /* Check if the embedded name(s) end in "+", and if so,
1512 replace the "+" with the zone at "ac_tail", changing the name
1513 to a ".zkey". The name is allocated on the 'scratch' array,
1514 so we can free it afterwards. */
1515 switch (rd[i].record_type)
1517 case GNUNET_DNSPARSER_TYPE_CNAME:
1522 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1525 if ( (NULL == cname) ||
1526 (off != rd[i].data_size) )
1528 GNUNET_break_op (0); /* record not well-formed */
1532 cname = translate_dot_plus (rh, cname);
1533 GNUNET_break (NULL != cname);
1534 scratch_start = scratch_off;
1536 GNUNET_DNSPARSER_builder_add_name (scratch,
1545 rd_new[rd_off].data = &scratch[scratch_start];
1546 rd_new[rd_off].data_size = scratch_off - scratch_start;
1550 GNUNET_free_non_null (cname);
1553 case GNUNET_DNSPARSER_TYPE_SOA:
1555 struct GNUNET_DNSPARSER_SoaRecord *soa;
1558 soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
1561 if ( (NULL == soa) ||
1562 (off != rd[i].data_size) )
1564 GNUNET_break_op (0); /* record not well-formed */
1568 soa->mname = translate_dot_plus (rh, soa->mname);
1569 soa->rname = translate_dot_plus (rh, soa->rname);
1570 scratch_start = scratch_off;
1572 GNUNET_DNSPARSER_builder_add_soa (scratch,
1581 rd_new[rd_off].data = &scratch[scratch_start];
1582 rd_new[rd_off].data_size = scratch_off - scratch_start;
1587 GNUNET_DNSPARSER_free_soa (soa);
1590 case GNUNET_DNSPARSER_TYPE_MX:
1592 struct GNUNET_DNSPARSER_MxRecord *mx;
1595 mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
1598 if ( (NULL == mx) ||
1599 (off != rd[i].data_size) )
1601 GNUNET_break_op (0); /* record not well-formed */
1605 mx->mxhost = translate_dot_plus (rh, mx->mxhost);
1606 scratch_start = scratch_off;
1608 GNUNET_DNSPARSER_builder_add_mx (scratch,
1617 rd_new[rd_off].data = &scratch[scratch_start];
1618 rd_new[rd_off].data_size = scratch_off - scratch_start;
1623 GNUNET_DNSPARSER_free_mx (mx);
1626 case GNUNET_DNSPARSER_TYPE_SRV:
1628 struct GNUNET_DNSPARSER_SrvRecord *srv;
1631 srv = GNUNET_DNSPARSER_parse_srv (rd[i].data,
1634 if ( (NULL == srv) ||
1635 (off != rd[i].data_size) )
1637 GNUNET_break_op (0); /* record not well-formed */
1641 srv->target = translate_dot_plus (rh, srv->target);
1642 scratch_start = scratch_off;
1644 GNUNET_DNSPARSER_builder_add_srv (scratch,
1653 rd_new[rd_off].data = &scratch[scratch_start];
1654 rd_new[rd_off].data_size = scratch_off - scratch_start;
1659 GNUNET_DNSPARSER_free_srv (srv);
1663 case GNUNET_GNSRECORD_TYPE_NICK:
1668 if ((rd[i].data_size > 0) &&
1669 (nick[rd[i].data_size -1] != '\0'))
1671 GNUNET_break_op (0);
1674 if (NULL == shorten_ac->suggested_shortening_label)
1675 shorten_ac->suggested_shortening_label = GNUNET_strdup (nick);
1678 case GNUNET_GNSRECORD_TYPE_PKEY:
1680 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
1682 if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
1684 GNUNET_break_op (0);
1687 memcpy (&pub, rd[i].data, rd[i].data_size);
1689 if (GNUNET_GNSRECORD_TYPE_PKEY != rh->record_type)
1691 /* try to resolve "+" */
1692 struct AuthorityChain *ac;
1694 ac = GNUNET_new (struct AuthorityChain);
1696 ac->gns_authority = GNUNET_YES;
1697 ac->authority_info.gns_authority = pub;
1698 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
1699 ac->suggested_shortening_label = NULL;
1700 ac->shortening_started = GNUNET_NO;
1701 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1704 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1710 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1712 /* delegation to DNS */
1713 if (GNUNET_GNSRECORD_TYPE_GNS2DNS == rh->record_type)
1716 break; /* do not follow to DNS, we wanted the GNS2DNS record! */
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1719 "Found GNS2DNS record, delegating to DNS!\n");
1722 case GNUNET_GNSRECORD_TYPE_BOX:
1724 /* unbox SRV/TLSA records if a specific one was requested */
1725 if ( (0 != rh->protocol) &&
1726 (0 != rh->service) &&
1727 (rd[i].data_size >= sizeof (struct GNUNET_GNSRECORD_BoxRecord)) )
1729 const struct GNUNET_GNSRECORD_BoxRecord *box;
1732 if ( (ntohs (box->protocol) == rh->protocol) &&
1733 (ntohs (box->service) == rh->service) )
1735 /* Box matches, unbox! */
1736 rd_new[rd_off].record_type = ntohl (box->record_type);
1737 rd_new[rd_off].data_size -= sizeof (struct GNUNET_GNSRECORD_BoxRecord);
1738 rd_new[rd_off].data = &box[1];
1744 /* no specific protocol/service specified, preserve all BOX
1745 records (for modern, GNS-enabled applications) */
1753 } /* end: for rd_count */
1755 /* trigger shortening */
1756 if ((NULL != rh->shorten_key) &&
1757 (NULL != shorten_ac) &&
1758 (GNUNET_NO == shorten_ac->shortening_started) &&
1759 (NULL != shorten_ac->suggested_shortening_label))
1761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1762 "Start shortening for label `%s' based on nick `%s'\n",
1764 shorten_ac->suggested_shortening_label);
1765 shorten_ac->shortening_started = GNUNET_YES;
1766 GNS_shorten_start (shorten_ac->label,
1767 shorten_ac->suggested_shortening_label,
1768 &shorten_ac->authority_info.gns_authority,
1772 /* yes, we are done, return result */
1773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1774 "Returning GNS response for `%s' with %u answers\n",
1777 rh->proc (rh->proc_cls, rd_off, rd_new);
1778 GNS_resolver_lookup_cancel (rh);
1782 /* need to recurse, check if we can */
1783 for (i=0;i<rd_count;i++)
1785 switch (rd[i].record_type)
1787 case GNUNET_GNSRECORD_TYPE_PKEY:
1788 /* delegation to another zone */
1789 if (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
1792 GNUNET_break_op (0);
1793 rh->proc (rh->proc_cls, 0, NULL);
1794 GNS_resolver_lookup_cancel (rh);
1797 /* expand authority chain */
1798 ac = GNUNET_new (struct AuthorityChain);
1800 ac->gns_authority = GNUNET_YES;
1801 ac->suggested_shortening_label = NULL;
1802 ac->shortening_started = GNUNET_NO;
1803 memcpy (&ac->authority_info.gns_authority,
1805 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1806 ac->label = resolver_lookup_get_next_label (rh);
1807 /* add AC to tail */
1808 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1812 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1815 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1817 /* resolution continues within DNS */
1818 struct Gns2DnsContext *g2dc;
1823 ns = GNUNET_DNSPARSER_parse_name (rd[i].data,
1826 ip = GNUNET_DNSPARSER_parse_name (rd[i].data,
1829 if ( (NULL == ns) ||
1831 (off != rd[i].data_size) )
1833 GNUNET_break_op (0);
1834 GNUNET_free_non_null (ns);
1835 GNUNET_free_non_null (ip);
1836 rh->proc (rh->proc_cls, 0, NULL);
1837 GNS_resolver_lookup_cancel (rh);
1840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1841 "Resolving `%s' to determine IP address of DNS server for GNS2DNS transition\n",
1843 /* resolve 'ip' to determine the IP(s) of the DNS
1845 g2dc = GNUNET_new (struct Gns2DnsContext);
1848 g2dc->rh = GNUNET_new (struct GNS_ResolverHandle);
1849 g2dc->rh->authority_zone = rh->ac_tail->authority_info.gns_authority;
1850 ip = translate_dot_plus (rh, ip);
1851 g2dc->rh->name = ip;
1852 g2dc->rh->name_resolution_pos = strlen (ip);
1853 g2dc->rh->proc = &handle_gns2dns_result;
1854 g2dc->rh->proc_cls = rh;
1855 g2dc->rh->record_type = GNUNET_GNSRECORD_TYPE_ANY;
1856 g2dc->rh->options = GNUNET_GNS_LO_DEFAULT;
1857 g2dc->rh->loop_limiter = rh->loop_limiter + 1;
1859 start_resolver_lookup (g2dc->rh);
1862 case GNUNET_DNSPARSER_TYPE_CNAME:
1867 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1870 if ( (NULL == cname) ||
1871 (off != rd[i].data_size) )
1873 GNUNET_break_op (0); /* record not well-formed */
1874 rh->proc (rh->proc_cls, 0, NULL);
1875 GNS_resolver_lookup_cancel (rh);
1876 GNUNET_free_non_null (cname);
1879 handle_gns_cname_result (rh,
1881 GNUNET_free (cname);
1884 /* FIXME: handle DNAME */
1890 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1891 _("GNS lookup recursion failed (no delegation record found)\n"));
1892 rh->proc (rh->proc_cls, 0, NULL);
1893 GNS_resolver_lookup_cancel (rh);
1898 * Function called once the namestore has completed the request for
1901 * @param cls closure with the `struct CacheOps`
1902 * @param success #GNUNET_OK on success
1903 * @param emsg error message
1906 namecache_cache_continuation (void *cls,
1910 struct CacheOps *co = cls;
1912 co->namecache_qe_cache = NULL;
1914 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1915 _("Failed to cache GNS resolution: %s\n"),
1917 GNUNET_CONTAINER_DLL_remove (co_head,
1925 * Iterator called on each result obtained for a DHT
1926 * operation that expects a reply
1928 * @param cls closure with the `struct GNS_ResolverHandle`
1929 * @param exp when will this value expire
1930 * @param key key of the result
1931 * @param get_path peers on reply path (or NULL if not recorded)
1932 * [0] = datastore's first neighbor, [length - 1] = local peer
1933 * @param get_path_length number of entries in @a get_path
1934 * @param put_path peers on the PUT path (or NULL if not recorded)
1935 * [0] = origin, [length - 1] = datastore
1936 * @param put_path_length number of entries in @a put_path
1937 * @param type type of the result
1938 * @param size number of bytes in data
1939 * @param data pointer to the result data
1942 handle_dht_response (void *cls,
1943 struct GNUNET_TIME_Absolute exp,
1944 const struct GNUNET_HashCode *key,
1945 const struct GNUNET_PeerIdentity *get_path,
1946 unsigned int get_path_length,
1947 const struct GNUNET_PeerIdentity *put_path,
1948 unsigned int put_path_length,
1949 enum GNUNET_BLOCK_Type type,
1950 size_t size, const void *data)
1952 struct GNS_ResolverHandle *rh = cls;
1953 struct AuthorityChain *ac = rh->ac_tail;
1954 const struct GNUNET_GNSRECORD_Block *block;
1955 struct CacheOps *co;
1957 GNUNET_DHT_get_stop (rh->get_handle);
1958 rh->get_handle = NULL;
1959 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1960 rh->dht_heap_node = NULL;
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1962 "Handling response from the DHT\n");
1963 if (size < sizeof (struct GNUNET_GNSRECORD_Block))
1965 /* how did this pass DHT block validation!? */
1967 rh->proc (rh->proc_cls, 0, NULL);
1968 GNS_resolver_lookup_cancel (rh);
1973 ntohl (block->purpose.size) +
1974 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
1975 sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
1977 /* how did this pass DHT block validation!? */
1979 rh->proc (rh->proc_cls, 0, NULL);
1980 GNS_resolver_lookup_cancel (rh);
1984 GNUNET_GNSRECORD_block_decrypt (block,
1985 &ac->authority_info.gns_authority,
1987 &handle_gns_resolution_result,
1990 GNUNET_break_op (0); /* block was ill-formed */
1991 rh->proc (rh->proc_cls, 0, NULL);
1992 GNS_resolver_lookup_cancel (rh);
1995 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us)
1997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1998 "Received expired block from the DHT, will not cache it.\n");
2001 /* Cache well-formed blocks */
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2003 "Caching response from the DHT in namecache\n");
2004 co = GNUNET_new (struct CacheOps);
2005 co->namecache_qe_cache = GNUNET_NAMECACHE_block_cache (namecache_handle,
2007 &namecache_cache_continuation,
2009 GNUNET_CONTAINER_DLL_insert (co_head,
2016 * Initiate a DHT query for a set of GNS records.
2018 * @param rh resolution handle
2019 * @param query key to use in the DHT lookup
2022 start_dht_request (struct GNS_ResolverHandle *rh,
2023 const struct GNUNET_HashCode *query)
2025 struct GNS_ResolverHandle *rx;
2027 GNUNET_assert (NULL == rh->get_handle);
2028 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
2029 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2031 DHT_GNS_REPLICATION_LEVEL,
2032 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2034 &handle_dht_response, rh);
2035 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2037 GNUNET_TIME_absolute_get ().abs_value_us);
2038 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
2040 /* fail longest-standing DHT request */
2041 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
2042 GNUNET_assert (NULL != rx);
2043 rx->proc (rx->proc_cls, 0, NULL);
2044 GNS_resolver_lookup_cancel (rx);
2050 * Process a records that were decrypted from a block that we got from
2051 * the namecache. Simply calls #handle_gns_resolution_result().
2053 * @param cls closure with the `struct GNS_ResolverHandle`
2054 * @param rd_count number of entries in @a rd array
2055 * @param rd array of records with data to store
2058 handle_gns_namecache_resolution_result (void *cls,
2059 unsigned int rd_count,
2060 const struct GNUNET_GNSRECORD_Data *rd)
2062 struct GNS_ResolverHandle *rh = cls;
2064 handle_gns_resolution_result (rh,
2071 * Process a record that was stored in the namecache.
2073 * @param cls closure with the `struct GNS_ResolverHandle`
2074 * @param block block that was stored in the namecache
2077 handle_namecache_block_response (void *cls,
2078 const struct GNUNET_GNSRECORD_Block *block)
2080 struct GNS_ResolverHandle *rh = cls;
2081 struct AuthorityChain *ac = rh->ac_tail;
2082 const char *label = ac->label;
2083 const struct GNUNET_CRYPTO_EcdsaPublicKey *auth = &ac->authority_info.gns_authority;
2084 struct GNUNET_HashCode query;
2086 GNUNET_assert (NULL != rh->namecache_qe);
2087 rh->namecache_qe = NULL;
2088 if ( ( (GNUNET_GNS_LO_DEFAULT == rh->options) ||
2089 ( (GNUNET_GNS_LO_LOCAL_MASTER == rh->options) &&
2090 (ac != rh->ac_head) ) ) &&
2091 ( (NULL == block) ||
2092 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
2094 /* namecache knows nothing; try DHT lookup */
2095 GNUNET_GNSRECORD_query_from_public_key (auth,
2098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2099 "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
2101 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
2102 GNUNET_h2s (&query));
2103 start_dht_request (rh, &query);
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2108 "Received result from namecache for label `%s'\n",
2111 if ( (NULL == block) ||
2112 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
2114 /* DHT not permitted and no local result, fail */
2115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2116 "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
2118 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2119 rh->proc (rh->proc_cls, 0, NULL);
2120 GNS_resolver_lookup_cancel (rh);
2124 GNUNET_GNSRECORD_block_decrypt (block,
2127 &handle_gns_namecache_resolution_result,
2130 GNUNET_break_op (0); /* block was ill-formed */
2131 /* try DHT instead */
2132 GNUNET_GNSRECORD_query_from_public_key (auth,
2135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2136 "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
2138 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
2139 GNUNET_h2s (&query));
2140 start_dht_request (rh, &query);
2147 * Lookup tail of our authority chain in the namecache.
2149 * @param rh query we are processing
2152 recursive_gns_resolution_namecache (struct GNS_ResolverHandle *rh)
2154 struct AuthorityChain *ac = rh->ac_tail;
2155 struct GNUNET_HashCode query;
2157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2158 "Starting GNS resolution for `%s' in zone %s\n",
2160 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2161 GNUNET_GNSRECORD_query_from_public_key (&ac->authority_info.gns_authority,
2164 if (GNUNET_YES == use_cache)
2167 = GNUNET_NAMECACHE_lookup_block (namecache_handle,
2169 &handle_namecache_block_response,
2171 GNUNET_assert (NULL != rh->namecache_qe);
2175 start_dht_request (rh, &query);
2181 * Function called with the result from a revocation check.
2183 * @param cls the `struct GNS_ResovlerHandle`
2184 * @param is_valid #GNUNET_YES if the zone was not yet revoked
2187 handle_revocation_result (void *cls,
2190 struct GNS_ResolverHandle *rh = cls;
2191 struct AuthorityChain *ac = rh->ac_tail;
2193 rh->rev_check = NULL;
2194 if (GNUNET_YES != is_valid)
2196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2197 _("Zone %s was revoked, resolution fails\n"),
2198 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2199 rh->proc (rh->proc_cls, 0, NULL);
2200 GNS_resolver_lookup_cancel (rh);
2203 recursive_gns_resolution_namecache (rh);
2208 * Perform revocation check on tail of our authority chain.
2210 * @param rh query we are processing
2213 recursive_gns_resolution_revocation (struct GNS_ResolverHandle *rh)
2215 struct AuthorityChain *ac = rh->ac_tail;
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2218 "Starting revocation check for zone %s\n",
2219 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2220 rh->rev_check = GNUNET_REVOCATION_query (cfg,
2221 &ac->authority_info.gns_authority,
2222 &handle_revocation_result,
2224 GNUNET_assert (NULL != rh->rev_check);
2229 * Task scheduled to continue with the resolution process.
2231 * @param cls the `struct GNS_ResolverHandle` of the resolution
2232 * @param tc task context
2235 recursive_resolution (void *cls,
2236 const struct GNUNET_SCHEDULER_TaskContext *tc)
2238 struct GNS_ResolverHandle *rh = cls;
2241 if (MAX_RECURSION < rh->loop_limiter++)
2243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2244 "Encountered unbounded recursion resolving `%s'\n",
2246 rh->proc (rh->proc_cls, 0, NULL);
2247 GNS_resolver_lookup_cancel (rh);
2250 if (GNUNET_YES == rh->ac_tail->gns_authority)
2251 recursive_gns_resolution_revocation (rh);
2253 recursive_dns_resolution (rh);
2258 * Begin the resolution process from 'name', starting with
2259 * the identification of the zone specified by 'name'.
2261 * @param rh resolution to perform
2264 start_resolver_lookup (struct GNS_ResolverHandle *rh)
2266 struct AuthorityChain *ac;
2271 if (1 == inet_pton (AF_INET,
2275 /* name is IPv4 address, pretend it's an A record */
2276 struct GNUNET_GNSRECORD_Data rd;
2279 rd.data_size = sizeof (v4);
2280 rd.expiration_time = UINT64_MAX;
2281 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
2283 rh->proc (rh->proc_cls, 1, &rd);
2284 GNS_resolver_lookup_cancel (rh);
2287 if (1 == inet_pton (AF_INET6,
2291 /* name is IPv6 address, pretend it's an AAAA record */
2292 struct GNUNET_GNSRECORD_Data rd;
2295 rd.data_size = sizeof (v6);
2296 rd.expiration_time = UINT64_MAX;
2297 rd.record_type = GNUNET_DNSPARSER_TYPE_AAAA;
2299 rh->proc (rh->proc_cls, 1, &rd);
2300 GNS_resolver_lookup_cancel (rh);
2303 if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
2304 (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
2305 ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
2306 (GNUNET_YES != is_zkey_tld (rh->name)) ) )
2308 /* use standard DNS lookup */
2311 switch (rh->record_type)
2313 case GNUNET_DNSPARSER_TYPE_A:
2316 case GNUNET_DNSPARSER_TYPE_AAAA:
2323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2324 "Doing standard DNS lookup for `%s'\n",
2326 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
2333 if (is_zkey_tld (rh->name))
2335 /* Name ends with ".zkey", try to replace authority zone with zkey
2337 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
2338 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
2341 GNUNET_CRYPTO_ecdsa_public_key_from_string (y,
2343 &rh->authority_zone)) )
2345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2346 _("Hostname `%s' is not well-formed, resolution fails\n"),
2348 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
2350 GNUNET_free_non_null (y);
2354 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
2355 GNUNET_free (resolver_lookup_get_next_label (rh));
2357 ac = GNUNET_new (struct AuthorityChain);
2359 ac->label = resolver_lookup_get_next_label (rh);
2360 ac->suggested_shortening_label = NULL;
2361 if (NULL == ac->label)
2362 /* name was just "gnu", so we default to label '+' */
2363 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
2364 ac->gns_authority = GNUNET_YES;
2365 ac->authority_info.gns_authority = rh->authority_zone;
2366 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
2369 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
2375 * Lookup of a record in a specific zone calls lookup result processor
2378 * @param zone the zone to perform the lookup in
2379 * @param record_type the record type to look up
2380 * @param name the name to look up
2381 * @param shorten_key a private key for use with PSEU import (can be NULL)
2382 * @param options local options to control local lookup
2383 * @param proc the processor to call on result
2384 * @param proc_cls the closure to pass to @a proc
2385 * @return handle to cancel operation
2387 struct GNS_ResolverHandle *
2388 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
2389 uint32_t record_type,
2391 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key,
2392 enum GNUNET_GNS_LocalOptions options,
2393 GNS_ResultProcessor proc, void *proc_cls)
2395 struct GNS_ResolverHandle *rh;
2397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2398 (NULL == shorten_key)
2399 ? "Starting lookup for `%s' with shortening disabled\n"
2400 : "Starting lookup for `%s' with shortening enabled\n",
2402 rh = GNUNET_new (struct GNS_ResolverHandle);
2403 GNUNET_CONTAINER_DLL_insert (rlh_head,
2406 rh->authority_zone = *zone;
2408 rh->proc_cls = proc_cls;
2409 rh->options = options;
2410 rh->record_type = record_type;
2411 rh->name = GNUNET_strdup (name);
2412 rh->name_resolution_pos = strlen (name);
2413 if (NULL != shorten_key)
2415 rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
2416 *rh->shorten_key = *shorten_key;
2418 start_resolver_lookup (rh);
2424 * Cancel active resolution (i.e. client disconnected).
2426 * @param rh resolution to abort
2429 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
2431 struct DnsResult *dr;
2432 struct AuthorityChain *ac;
2433 struct VpnContext *vpn_ctx;
2435 GNUNET_CONTAINER_DLL_remove (rlh_head,
2438 while (NULL != (ac = rh->ac_head))
2440 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
2443 GNUNET_free (ac->label);
2444 GNUNET_free_non_null (ac->suggested_shortening_label);
2447 if (NULL != rh->g2dc)
2449 /* rh->g2dc->rh is NOT in the DLL yet, so to enable us
2450 using GNS_resolver_lookup_cancel here, we need to
2452 if (NULL != rh->g2dc->rh)
2454 GNUNET_CONTAINER_DLL_insert (rlh_head,
2457 GNS_resolver_lookup_cancel (rh->g2dc->rh);
2458 rh->g2dc->rh = NULL;
2460 GNUNET_free (rh->g2dc->ns);
2461 GNUNET_free (rh->g2dc);
2464 if (NULL != rh->task_id)
2466 GNUNET_SCHEDULER_cancel (rh->task_id);
2469 if (NULL != rh->get_handle)
2471 GNUNET_DHT_get_stop (rh->get_handle);
2472 rh->get_handle = NULL;
2474 if (NULL != rh->dht_heap_node)
2476 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
2477 rh->dht_heap_node = NULL;
2479 if (NULL != (vpn_ctx = rh->vpn_ctx))
2481 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
2482 GNUNET_free (vpn_ctx->rd_data);
2483 GNUNET_free (vpn_ctx);
2485 if (NULL != rh->dns_request)
2487 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
2488 rh->dns_request = NULL;
2490 if (NULL != rh->namecache_qe)
2492 GNUNET_NAMECACHE_cancel (rh->namecache_qe);
2493 rh->namecache_qe = NULL;
2495 if (NULL != rh->rev_check)
2497 GNUNET_REVOCATION_query_cancel (rh->rev_check);
2498 rh->rev_check = NULL;
2500 if (NULL != rh->std_resolve)
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2503 "Canceling standard DNS resolution\n");
2504 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
2505 rh->std_resolve = NULL;
2507 while (NULL != (dr = rh->dns_result_head))
2509 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
2510 rh->dns_result_tail,
2514 GNUNET_free_non_null (rh->shorten_key);
2515 GNUNET_free (rh->name);
2520 /* ***************** Resolver initialization ********************* */
2524 * Initialize the resolver
2526 * @param nc the namecache handle
2527 * @param dht the dht handle
2528 * @param c configuration handle
2529 * @param max_bg_queries maximum number of parallel background queries in dht
2532 GNS_resolver_init (struct GNUNET_NAMECACHE_Handle *nc,
2533 struct GNUNET_DHT_Handle *dht,
2534 const struct GNUNET_CONFIGURATION_Handle *c,
2535 unsigned long long max_bg_queries)
2540 namecache_handle = nc;
2543 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2544 max_allowed_background_queries = max_bg_queries;
2545 if (GNUNET_SYSERR == (use_cache = GNUNET_CONFIGURATION_get_value_yesno (c,
2548 use_cache = GNUNET_YES;
2549 if (GNUNET_NO == use_cache)
2550 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Namecache disabled\n");
2553 GNUNET_CONFIGURATION_get_value_string (c,
2558 /* user did not specify DNS resolver, use 8.8.8.8 */
2559 dns_ip = GNUNET_strdup ("8.8.8.8");
2561 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
2562 GNUNET_free (dns_ip);
2563 vpn_handle = GNUNET_VPN_connect (cfg);
2571 GNS_resolver_done ()
2573 struct GNS_ResolverHandle *rh;
2574 struct CacheOps *co;
2576 /* abort active resolutions */
2577 while (NULL != (rh = rlh_head))
2579 rh->proc (rh->proc_cls, 0, NULL);
2580 GNS_resolver_lookup_cancel (rh);
2582 while (NULL != (co = co_head))
2584 GNUNET_CONTAINER_DLL_remove (co_head,
2587 GNUNET_NAMECACHE_cancel (co->namecache_qe_cache);
2590 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
2591 dht_lookup_heap = NULL;
2592 GNUNET_DNSSTUB_stop (dns_handle);
2594 GNUNET_VPN_disconnect (vpn_handle);
2597 namecache_handle = NULL;
2601 /* *************** common helper functions (do not really belong here) *********** */
2604 * Checks if @a name ends in ".TLD"
2606 * @param name the name to check
2607 * @param tld the TLD to check for
2608 * @return GNUNET_YES or GNUNET_NO
2611 is_tld (const char* name, const char* tld)
2615 if (strlen (name) <= strlen (tld))
2617 offset = strlen (name) - strlen (tld);
2618 if (0 != strcmp (name + offset, tld))
2624 /* end of gnunet-service-gns_resolver.c */