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
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
546 fail_resolution (void *cls)
548 struct GNS_ResolverHandle *rh = cls;
551 rh->proc (rh->proc_cls, 0, NULL);
552 GNS_resolver_lookup_cancel (rh);
556 #if (defined WINDOWS) || (defined DARWIN)
557 /* Don't have this on W32, here's a naive implementation
558 * Was somehow removed on OS X ... */
560 memrchr (const void *s,
564 const unsigned char *ucs = s;
567 for (i = n - 1; i >= 0; i--)
568 if (c == (int) ucs[i])
569 return (void *) &ucs[i];
576 * Get the next, rightmost label from the name that we are trying to resolve,
577 * and update the resolution position accordingly. Labels usually consist
578 * of up to 63 characters without a period ("."); however, we use a special
579 * convention to support SRV and TLSA records where the domain name
580 * includes an encoding for a service and protocol in the name. The
581 * syntax (see RFC 2782) here is "_Service._Proto.Name" and in this
582 * special case we include the "_Service._Proto" in the rightmost label.
583 * Thus, for "_443._tcp.foo.bar" we first return the label "bar" and then
584 * the label "_443._tcp.foo". The special case is detected by the
585 * presence of labels beginning with an underscore. Whenever a label
586 * begins with an underscore, it is combined with the label to its right
587 * (and the "." is preserved).
589 * @param rh handle to the resolution operation to get the next label from
590 * @return NULL if there are no more labels
593 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
604 if (0 == rh->name_resolution_pos)
606 dot = memrchr (rh->name,
608 rh->name_resolution_pos);
611 /* done, this was the last one */
612 len = rh->name_resolution_pos;
614 rh->name_resolution_pos = 0;
618 /* advance by one label */
619 len = rh->name_resolution_pos - (dot - rh->name) - 1;
621 rh->name_resolution_pos = dot - rh->name;
625 ret = GNUNET_strndup (rp, len);
626 /* If we have labels starting with underscore with label on
627 * the right (SRV/DANE/BOX case), determine port/protocol;
628 * The format of `rh->name` must be "_PORT._PROTOCOL".
630 if ( ('_' == rh->name[0]) &&
631 (NULL != (dot = memrchr (rh->name,
633 rh->name_resolution_pos))) &&
635 (NULL == memrchr (rh->name,
639 srv_name = GNUNET_strndup (&rh->name[1],
640 (dot - rh->name) - 1);
641 proto_name = GNUNET_strndup (&dot[2],
642 rh->name_resolution_pos - (dot - rh->name) - 1);
643 rh->name_resolution_pos = 0;
644 pe = getprotobyname (proto_name);
647 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
648 _("Protocol `%s' unknown, skipping labels.\n"),
650 GNUNET_free (proto_name);
651 GNUNET_free (srv_name);
654 se = getservbyname (srv_name,
658 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
659 _("Service `%s' unknown for protocol `%s', skipping labels.\n"),
662 GNUNET_free (proto_name);
663 GNUNET_free (srv_name);
666 rh->protocol = pe->p_proto;
667 rh->service = se->s_port;
674 * Gives the cummulative result obtained to the callback and clean up the request.
676 * @param rh resolution process that has culminated in a result
679 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
681 struct DnsResult *pos;
686 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
689 struct GNUNET_GNSRECORD_Data rd[n];
692 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
694 rd[i].data = pos->data;
695 rd[i].data_size = pos->data_size;
696 rd[i].record_type = pos->record_type;
697 if (0 == pos->expiration_time)
699 rd[i].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
700 rd[i].expiration_time = 0;
704 rd[i].flags = GNUNET_GNSRECORD_RF_NONE;
705 rd[i].expiration_time = pos->expiration_time;
709 GNUNET_assert (i == n);
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
711 "Transmitting standard DNS result with %u records\n",
713 rh->proc (rh->proc_cls,
717 GNS_resolver_lookup_cancel (rh);
722 * Add a result from DNS to the records to be returned to the application.
724 * @param rh resolution request to extend with a result
725 * @param expiration_time expiration time for the answer
726 * @param record_type DNS record type of the answer
727 * @param data_size number of bytes in @a data
728 * @param data binary data to return in DNS record
731 add_dns_result (struct GNS_ResolverHandle *rh,
732 uint64_t expiration_time,
733 uint32_t record_type,
737 struct DnsResult *res;
739 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
740 res->expiration_time = expiration_time;
741 res->data_size = data_size;
742 res->record_type = record_type;
744 memcpy (&res[1], data, data_size);
745 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
752 * We had to do a DNS lookup. Convert the result (if any) and return
755 * @param cls closure with the `struct GNS_ResolverHandle`
756 * @param addr one of the addresses of the host, NULL for the last address
757 * @param addrlen length of the address
760 handle_dns_result (void *cls,
761 const struct sockaddr *addr,
764 struct GNS_ResolverHandle *rh = cls;
765 const struct sockaddr_in *sa4;
766 const struct sockaddr_in6 *sa6;
770 rh->std_resolve = NULL;
771 transmit_lookup_dns_result (rh);
774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
775 "Received %u bytes of DNS IP data\n",
777 switch (addr->sa_family)
780 sa4 = (const struct sockaddr_in *) addr;
782 0 /* expiration time is unknown */,
783 GNUNET_DNSPARSER_TYPE_A,
784 sizeof (struct in_addr),
788 sa6 = (const struct sockaddr_in6 *) addr;
790 0 /* expiration time is unknown */,
791 GNUNET_DNSPARSER_TYPE_AAAA,
792 sizeof (struct in6_addr),
803 * Task scheduled to continue with the resolution process.
805 * @param cls the 'struct GNS_ResolverHandle' of the resolution
806 * @param tc task context
809 recursive_resolution (void *cls);
813 * Begin the resolution process from 'name', starting with
814 * the identification of the zone specified by 'name'.
816 * @param rh resolution to perform
819 start_resolver_lookup (struct GNS_ResolverHandle *rh);
823 * Function called with the result of a DNS resolution.
825 * @param cls the request handle of the resolution that
826 * we were attempting to make
827 * @param rs socket that received the response
828 * @param dns dns response, never NULL
829 * @param dns_len number of bytes in @a dns
832 dns_result_parser (void *cls,
833 struct GNUNET_DNSSTUB_RequestSocket *rs,
834 const struct GNUNET_TUN_DnsHeader *dns,
837 struct GNS_ResolverHandle *rh = cls;
838 struct GNUNET_DNSPARSER_Packet *p;
839 const struct GNUNET_DNSPARSER_Record *rec;
840 unsigned int rd_count;
843 rh->dns_request = NULL;
844 GNUNET_SCHEDULER_cancel (rh->task_id);
846 p = GNUNET_DNSPARSER_parse ((const char *) dns,
850 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
851 _("Failed to parse DNS response\n"));
852 rh->proc (rh->proc_cls, 0, NULL);
853 GNS_resolver_lookup_cancel (rh);
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 "Received DNS response for `%s' with %u answers\n",
859 (unsigned int) p->num_answers);
860 if ( (p->num_answers > 0) &&
861 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
862 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
864 GNUNET_free (rh->name);
865 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
866 rh->name_resolution_pos = strlen (rh->name);
867 start_resolver_lookup (rh);
868 GNUNET_DNSPARSER_free_packet (p);
872 /* convert from (parsed) DNS to (binary) GNS format! */
873 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
875 struct GNUNET_GNSRECORD_Data rd[rd_count];
877 char buf[UINT16_MAX];
883 memset (rd, 0, sizeof (rd));
884 for (i=0;i<rd_count;i++)
886 if (i < p->num_answers)
887 rec = &p->answers[i];
888 else if (i < p->num_answers + p->num_authority_records)
889 rec = &p->authority_records[i - p->num_answers];
891 rec = &p->additional_records[i - p->num_answers - p->num_authority_records];
892 /* As we copied the full DNS name to 'rh->ac_tail->label', this
893 should be the correct check to see if this record is actually
894 a record for our label... */
895 if (0 != strcmp (rec->name,
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899 "Dropping record `%s', does not match desired name `%s'\n",
905 rd[i - skip].record_type = rec->type;
906 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
909 case GNUNET_DNSPARSER_TYPE_A:
910 if (rec->data.raw.data_len != sizeof (struct in_addr))
916 rd[i - skip].data_size = rec->data.raw.data_len;
917 rd[i - skip].data = rec->data.raw.data;
919 case GNUNET_DNSPARSER_TYPE_AAAA:
920 if (rec->data.raw.data_len != sizeof (struct in6_addr))
926 rd[i - skip].data_size = rec->data.raw.data_len;
927 rd[i - skip].data = rec->data.raw.data;
929 case GNUNET_DNSPARSER_TYPE_CNAME:
930 case GNUNET_DNSPARSER_TYPE_PTR:
931 case GNUNET_DNSPARSER_TYPE_NS:
934 GNUNET_DNSPARSER_builder_add_name (buf,
943 rd[i - skip].data_size = buf_off - buf_start;
944 rd[i - skip].data = &buf[buf_start];
946 case GNUNET_DNSPARSER_TYPE_SOA:
949 GNUNET_DNSPARSER_builder_add_soa (buf,
958 rd[i - skip].data_size = buf_off - buf_start;
959 rd[i - skip].data = &buf[buf_start];
961 case GNUNET_DNSPARSER_TYPE_MX:
964 GNUNET_DNSPARSER_builder_add_mx (buf,
973 rd[i - skip].data_size = buf_off - buf_start;
974 rd[i - skip].data = &buf[buf_start];
976 case GNUNET_DNSPARSER_TYPE_SRV:
979 GNUNET_DNSPARSER_builder_add_srv (buf,
988 rd[i - skip].data_size = buf_off - buf_start;
989 rd[i - skip].data = &buf[buf_start];
992 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
993 _("Skipping record of unsupported type %d\n"),
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1000 "Returning DNS response for `%s' with %u answers\n",
1002 (unsigned int) p->num_answers);
1003 rh->proc (rh->proc_cls, rd_count - skip, rd);
1004 GNS_resolver_lookup_cancel (rh);
1006 GNUNET_DNSPARSER_free_packet (p);
1011 * Perform recursive DNS resolution. Asks the given DNS resolver to
1012 * resolve "rh->dns_name", possibly recursively proceeding following
1013 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
1014 * we find the answer.
1016 * @param rh resolution information
1019 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
1021 struct AuthorityChain *ac;
1023 struct GNUNET_DNSPARSER_Query *query;
1024 struct GNUNET_DNSPARSER_Packet *p;
1026 size_t dns_request_length;
1029 GNUNET_assert (NULL != ac);
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031 "Starting DNS lookup for `%s'\n",
1033 GNUNET_assert (GNUNET_NO == ac->gns_authority);
1034 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
1037 sa_len = sizeof (struct sockaddr_in);
1040 sa_len = sizeof (struct sockaddr_in6);
1044 rh->proc (rh->proc_cls, 0, NULL);
1045 GNS_resolver_lookup_cancel (rh);
1048 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
1049 query->name = GNUNET_strdup (ac->label);
1050 query->type = rh->record_type;
1051 query->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1052 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
1055 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1057 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
1058 p->flags.recursion_desired = 1;
1060 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
1063 rh->proc (rh->proc_cls, 0, NULL);
1064 GNS_resolver_lookup_cancel (rh);
1068 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
1069 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
1075 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
1079 GNUNET_free (dns_request);
1080 GNUNET_DNSPARSER_free_packet (p);
1085 * We encountered a CNAME record during our resolution.
1086 * Merge it into our chain.
1088 * @param rh resolution we are performing
1089 * @param cname value of the cname record we got for the current
1090 * authority chain tail
1093 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
1098 struct AuthorityChain *ac;
1100 nlen = strlen (cname);
1103 &cname[nlen - 2])) )
1105 /* CNAME resolution continues relative to current domain */
1106 if (0 == rh->name_resolution_pos)
1108 res = GNUNET_strndup (cname, nlen - 2);
1109 rh->name_resolution_pos = nlen - 2;
1113 GNUNET_asprintf (&res,
1115 (int) rh->name_resolution_pos,
1119 rh->name_resolution_pos = strlen (res);
1121 GNUNET_free (rh->name);
1123 ac = GNUNET_new (struct AuthorityChain);
1125 ac->gns_authority = GNUNET_YES;
1126 ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
1127 ac->label = resolver_lookup_get_next_label (rh);
1128 ac->suggested_shortening_label = NULL;
1129 ac->shortening_started = GNUNET_NO;
1130 /* add AC to tail */
1131 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1134 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1138 /* name is absolute, start from the beginning */
1139 GNUNET_free (rh->name);
1140 rh->name = GNUNET_strdup (cname);
1141 rh->name_resolution_pos = strlen (rh->name);
1142 start_resolver_lookup (rh);
1147 * Process a records that were decrypted from a block.
1149 * @param cls closure with the 'struct GNS_ResolverHandle'
1150 * @param rd_count number of entries in @a rd array
1151 * @param rd array of records with data to store
1154 handle_gns_resolution_result (void *cls,
1155 unsigned int rd_count,
1156 const struct GNUNET_GNSRECORD_Data *rd);
1160 * Callback invoked from the VPN service once a redirection is
1161 * available. Provides the IP address that can now be used to
1162 * reach the requested destination. Replaces the "VPN" record
1163 * with the respective A/AAAA record and continues processing.
1165 * @param cls closure
1166 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1167 * will match 'result_af' from the request
1168 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1169 * that the VPN allocated for the redirection;
1170 * traffic to this IP will now be redirected to the
1171 * specified target peer; NULL on error
1174 vpn_allocation_cb (void *cls,
1176 const void *address)
1178 struct VpnContext *vpn_ctx = cls;
1179 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1180 struct GNUNET_GNSRECORD_Data rd[vpn_ctx->rd_count];
1183 vpn_ctx->vpn_request = NULL;
1185 GNUNET_assert (GNUNET_OK ==
1186 GNUNET_GNSRECORD_records_deserialize (vpn_ctx->rd_data_size,
1190 for (i=0;i<vpn_ctx->rd_count;i++)
1192 if (GNUNET_GNSRECORD_TYPE_VPN == rd[i].record_type)
1197 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1198 rd[i].data_size = sizeof (struct in_addr);
1199 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1201 rd[i].data = address;
1204 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1205 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1207 rd[i].data = address;
1208 rd[i].data_size = sizeof (struct in6_addr);
1216 GNUNET_assert (i < vpn_ctx->rd_count);
1217 handle_gns_resolution_result (rh,
1220 GNUNET_free (vpn_ctx->rd_data);
1221 GNUNET_free (vpn_ctx);
1226 * We've resolved the IP address for the DNS resolver to use
1227 * after encountering a GNS2DNS record.
1229 * TODO: Right now we only foward the request to ONE DNS resolver,
1230 * even if we get multiple IP addresses back; a correct implementation
1231 * should try all DNS resolvers.
1233 * @param cls the `struct GNS_ResolverHandle` where we encountered
1234 * the GNS2DNS record
1235 * @param rd_count number of records in @a rd
1236 * @param rd addresses for the DNS resolver (presumably)
1239 handle_gns2dns_result (void *cls,
1240 unsigned int rd_count,
1241 const struct GNUNET_GNSRECORD_Data *rd)
1243 struct GNS_ResolverHandle *rh = cls;
1244 struct AuthorityChain *ac;
1246 struct sockaddr *sa;
1247 struct sockaddr_in v4;
1248 struct sockaddr_in6 v6;
1251 /* find suitable A/AAAA record */
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 "Received %u results for IP address of DNS server for GNS2DNS transition\n",
1255 /* enable cleanup of 'rh' handle that comes next... */
1256 GNUNET_CONTAINER_DLL_insert (rlh_head,
1259 rh->g2dc->rh = NULL;
1262 for (j=0;j<rd_count;j++)
1264 switch (rd[j].record_type)
1266 case GNUNET_DNSPARSER_TYPE_A:
1267 if (sizeof (struct in_addr) != rd[j].data_size)
1269 GNUNET_break_op (0);
1270 rh->proc (rh->proc_cls, 0, NULL);
1271 GNS_resolver_lookup_cancel (rh);
1274 /* FIXME: might want to check if we support IPv4 here,
1275 and otherwise skip this one and hope we find another */
1276 memset (&v4, 0, sizeof (v4));
1277 sa_len = sizeof (v4);
1278 v4.sin_family = AF_INET;
1279 v4.sin_port = htons (53);
1280 #if HAVE_SOCKADDR_IN_SIN_LEN
1281 v4.sin_len = (u_char) sa_len;
1283 memcpy (&v4.sin_addr,
1285 sizeof (struct in_addr));
1286 sa = (struct sockaddr *) &v4;
1288 case GNUNET_DNSPARSER_TYPE_AAAA:
1289 if (sizeof (struct in6_addr) != rd[j].data_size)
1291 GNUNET_break_op (0);
1292 rh->proc (rh->proc_cls, 0, NULL);
1293 GNS_resolver_lookup_cancel (rh);
1296 /* FIXME: might want to check if we support IPv6 here,
1297 and otherwise skip this one and hope we find another */
1298 memset (&v6, 0, sizeof (v6));
1299 sa_len = sizeof (v6);
1300 v6.sin6_family = AF_INET6;
1301 v6.sin6_port = htons (53);
1302 #if HAVE_SOCKADDR_IN_SIN_LEN
1303 v6.sin6_len = (u_char) sa_len;
1305 memcpy (&v6.sin6_addr,
1307 sizeof (struct in6_addr));
1308 sa = (struct sockaddr *) &v6;
1318 /* we cannot continue; NS without A/AAAA */
1319 rh->proc (rh->proc_cls, 0, NULL);
1320 GNS_resolver_lookup_cancel (rh);
1323 /* expand authority chain */
1324 ac = GNUNET_new (struct AuthorityChain);
1326 strcpy (ac->authority_info.dns_authority.name,
1328 memcpy (&ac->authority_info.dns_authority.dns_ip,
1331 /* for DNS recursion, the label is the full DNS name,
1332 created from the remainder of the GNS name and the
1333 name in the NS record */
1334 GNUNET_asprintf (&ac->label,
1336 (int) rh->name_resolution_pos,
1338 (0 != rh->name_resolution_pos) ? "." : "",
1340 GNUNET_free (rh->g2dc->ns);
1341 GNUNET_free (rh->g2dc);
1343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1344 "Will continue resolution using DNS server `%s' to resolve `%s'\n",
1345 GNUNET_a2s (sa, sa_len),
1347 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1350 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1352 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1353 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1355 rh->proc (rh->proc_cls, 0, NULL);
1356 GNS_resolver_lookup_cancel (rh);
1360 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1366 * Process a records that were decrypted from a block.
1368 * @param cls closure with the `struct GNS_ResolverHandle`
1369 * @param rd_count number of entries in @a rd array
1370 * @param rd array of records with data to store
1373 handle_gns_resolution_result (void *cls,
1374 unsigned int rd_count,
1375 const struct GNUNET_GNSRECORD_Data *rd)
1377 struct GNS_ResolverHandle *rh = cls;
1378 struct AuthorityChain *ac;
1379 struct AuthorityChain *shorten_ac;
1382 struct VpnContext *vpn_ctx;
1383 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1385 struct GNUNET_HashCode vhash;
1387 char scratch[UINT16_MAX];
1389 size_t scratch_start;
1391 struct GNUNET_GNSRECORD_Data rd_new[rd_count];
1392 unsigned int rd_off;
1394 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1395 "Resolution succeeded for `%s' in zone %s, got %u records\n",
1397 GNUNET_GNSRECORD_z2s (&rh->ac_tail->authority_info.gns_authority),
1399 if (0 == rh->name_resolution_pos)
1401 /* top-level match, are we done yet? */
1402 if ( (rd_count > 0) &&
1403 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1404 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1407 cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
1410 if ( (NULL == cname) ||
1411 (off != rd[0].data_size) )
1413 GNUNET_break_op (0);
1414 rh->proc (rh->proc_cls, 0, NULL);
1415 GNS_resolver_lookup_cancel (rh);
1416 GNUNET_free_non_null (cname);
1419 handle_gns_cname_result (rh,
1421 GNUNET_free (cname);
1424 /* If A/AAAA was requested, but we got a VPN
1425 record, we convert it to A/AAAA using GNUnet VPN */
1426 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1427 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1429 for (i=0;i<rd_count;i++)
1431 switch (rd[i].record_type)
1433 case GNUNET_GNSRECORD_TYPE_VPN:
1435 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1436 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) >
1439 GNUNET_break_op (0);
1440 rh->proc (rh->proc_cls, 0, NULL);
1441 GNS_resolver_lookup_cancel (rh);
1444 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1445 vname = (const char *) &vpn[1];
1446 if ('\0' != vname[rd[i].data_size - 1 - sizeof (struct GNUNET_TUN_GnsVpnRecord)])
1448 GNUNET_break_op (0);
1449 rh->proc (rh->proc_cls, 0, NULL);
1450 GNS_resolver_lookup_cancel (rh);
1453 GNUNET_TUN_service_name_to_hash (vname,
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "Attempting VPN allocation for %s-%s (AF: %d, proto %d)\n",
1457 GNUNET_i2s (&vpn->peer),
1460 (int) ntohs (vpn->proto));
1461 vpn_ctx = GNUNET_new (struct VpnContext);
1462 rh->vpn_ctx = vpn_ctx;
1464 vpn_ctx->rd_data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
1466 vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
1467 vpn_ctx->rd_count = rd_count;
1468 GNUNET_assert (vpn_ctx->rd_data_size ==
1469 GNUNET_GNSRECORD_records_serialize (rd_count,
1471 vpn_ctx->rd_data_size,
1473 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1478 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1483 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1485 /* delegation to DNS */
1486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1487 "Found GNS2DNS record, delegating to DNS!\n");
1494 } /* end: name_resolution_pos */
1495 /* convert relative names in record values to absolute names,
1496 using 'scratch' array for memory allocations */
1499 shorten_ac = rh->ac_tail;
1500 for (i=0;i<rd_count;i++)
1502 if ( (0 != rh->protocol) &&
1503 (0 != rh->service) &&
1504 (GNUNET_GNSRECORD_TYPE_BOX != rd[i].record_type) )
1505 continue; /* we _only_ care about boxed records */
1507 rd_new[rd_off] = rd[i];
1508 /* Check if the embedded name(s) end in "+", and if so,
1509 replace the "+" with the zone at "ac_tail", changing the name
1510 to a ".zkey". The name is allocated on the 'scratch' array,
1511 so we can free it afterwards. */
1512 switch (rd[i].record_type)
1514 case GNUNET_DNSPARSER_TYPE_CNAME:
1519 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1522 if ( (NULL == cname) ||
1523 (off != rd[i].data_size) )
1525 GNUNET_break_op (0); /* record not well-formed */
1529 cname = translate_dot_plus (rh, cname);
1530 GNUNET_break (NULL != cname);
1531 scratch_start = scratch_off;
1533 GNUNET_DNSPARSER_builder_add_name (scratch,
1542 rd_new[rd_off].data = &scratch[scratch_start];
1543 rd_new[rd_off].data_size = scratch_off - scratch_start;
1547 GNUNET_free_non_null (cname);
1550 case GNUNET_DNSPARSER_TYPE_SOA:
1552 struct GNUNET_DNSPARSER_SoaRecord *soa;
1555 soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
1558 if ( (NULL == soa) ||
1559 (off != rd[i].data_size) )
1561 GNUNET_break_op (0); /* record not well-formed */
1565 soa->mname = translate_dot_plus (rh, soa->mname);
1566 soa->rname = translate_dot_plus (rh, soa->rname);
1567 scratch_start = scratch_off;
1569 GNUNET_DNSPARSER_builder_add_soa (scratch,
1578 rd_new[rd_off].data = &scratch[scratch_start];
1579 rd_new[rd_off].data_size = scratch_off - scratch_start;
1584 GNUNET_DNSPARSER_free_soa (soa);
1587 case GNUNET_DNSPARSER_TYPE_MX:
1589 struct GNUNET_DNSPARSER_MxRecord *mx;
1592 mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
1595 if ( (NULL == mx) ||
1596 (off != rd[i].data_size) )
1598 GNUNET_break_op (0); /* record not well-formed */
1602 mx->mxhost = translate_dot_plus (rh, mx->mxhost);
1603 scratch_start = scratch_off;
1605 GNUNET_DNSPARSER_builder_add_mx (scratch,
1614 rd_new[rd_off].data = &scratch[scratch_start];
1615 rd_new[rd_off].data_size = scratch_off - scratch_start;
1620 GNUNET_DNSPARSER_free_mx (mx);
1623 case GNUNET_DNSPARSER_TYPE_SRV:
1625 struct GNUNET_DNSPARSER_SrvRecord *srv;
1628 srv = GNUNET_DNSPARSER_parse_srv (rd[i].data,
1631 if ( (NULL == srv) ||
1632 (off != rd[i].data_size) )
1634 GNUNET_break_op (0); /* record not well-formed */
1638 srv->target = translate_dot_plus (rh, srv->target);
1639 scratch_start = scratch_off;
1641 GNUNET_DNSPARSER_builder_add_srv (scratch,
1650 rd_new[rd_off].data = &scratch[scratch_start];
1651 rd_new[rd_off].data_size = scratch_off - scratch_start;
1656 GNUNET_DNSPARSER_free_srv (srv);
1660 case GNUNET_GNSRECORD_TYPE_NICK:
1665 if ((rd[i].data_size > 0) &&
1666 (nick[rd[i].data_size -1] != '\0'))
1668 GNUNET_break_op (0);
1671 if (NULL == shorten_ac->suggested_shortening_label)
1672 shorten_ac->suggested_shortening_label = GNUNET_strdup (nick);
1675 case GNUNET_GNSRECORD_TYPE_PKEY:
1677 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
1679 if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
1681 GNUNET_break_op (0);
1684 memcpy (&pub, rd[i].data, rd[i].data_size);
1686 if (GNUNET_GNSRECORD_TYPE_PKEY != rh->record_type)
1688 /* try to resolve "+" */
1689 struct AuthorityChain *ac;
1691 ac = GNUNET_new (struct AuthorityChain);
1693 ac->gns_authority = GNUNET_YES;
1694 ac->authority_info.gns_authority = pub;
1695 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
1696 ac->suggested_shortening_label = NULL;
1697 ac->shortening_started = GNUNET_NO;
1698 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1701 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1707 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1709 /* delegation to DNS */
1710 if (GNUNET_GNSRECORD_TYPE_GNS2DNS == rh->record_type)
1713 break; /* do not follow to DNS, we wanted the GNS2DNS record! */
1715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1716 "Found GNS2DNS record, delegating to DNS!\n");
1719 case GNUNET_GNSRECORD_TYPE_BOX:
1721 /* unbox SRV/TLSA records if a specific one was requested */
1722 if ( (0 != rh->protocol) &&
1723 (0 != rh->service) &&
1724 (rd[i].data_size >= sizeof (struct GNUNET_GNSRECORD_BoxRecord)) )
1726 const struct GNUNET_GNSRECORD_BoxRecord *box;
1729 if ( (ntohs (box->protocol) == rh->protocol) &&
1730 (ntohs (box->service) == rh->service) )
1732 /* Box matches, unbox! */
1733 rd_new[rd_off].record_type = ntohl (box->record_type);
1734 rd_new[rd_off].data_size -= sizeof (struct GNUNET_GNSRECORD_BoxRecord);
1735 rd_new[rd_off].data = &box[1];
1741 /* no specific protocol/service specified, preserve all BOX
1742 records (for modern, GNS-enabled applications) */
1750 } /* end: for rd_count */
1752 /* trigger shortening */
1753 if ((NULL != rh->shorten_key) &&
1754 (NULL != shorten_ac) &&
1755 (GNUNET_NO == shorten_ac->shortening_started) &&
1756 (NULL != shorten_ac->suggested_shortening_label))
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1759 "Start shortening for label `%s' based on nick `%s'\n",
1761 shorten_ac->suggested_shortening_label);
1762 shorten_ac->shortening_started = GNUNET_YES;
1763 GNS_shorten_start (shorten_ac->label,
1764 shorten_ac->suggested_shortening_label,
1765 &shorten_ac->authority_info.gns_authority,
1769 /* yes, we are done, return result */
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1771 "Returning GNS response for `%s' with %u answers\n",
1774 rh->proc (rh->proc_cls, rd_off, rd_new);
1775 GNS_resolver_lookup_cancel (rh);
1779 /* need to recurse, check if we can */
1780 for (i=0;i<rd_count;i++)
1782 switch (rd[i].record_type)
1784 case GNUNET_GNSRECORD_TYPE_PKEY:
1785 /* delegation to another zone */
1786 if (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
1789 GNUNET_break_op (0);
1790 rh->proc (rh->proc_cls, 0, NULL);
1791 GNS_resolver_lookup_cancel (rh);
1794 /* expand authority chain */
1795 ac = GNUNET_new (struct AuthorityChain);
1797 ac->gns_authority = GNUNET_YES;
1798 ac->suggested_shortening_label = NULL;
1799 ac->shortening_started = GNUNET_NO;
1800 memcpy (&ac->authority_info.gns_authority,
1802 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1803 ac->label = resolver_lookup_get_next_label (rh);
1804 /* add AC to tail */
1805 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1809 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1812 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1814 /* resolution continues within DNS */
1815 struct Gns2DnsContext *g2dc;
1820 ns = GNUNET_DNSPARSER_parse_name (rd[i].data,
1823 ip = GNUNET_DNSPARSER_parse_name (rd[i].data,
1826 if ( (NULL == ns) ||
1828 (off != rd[i].data_size) )
1830 GNUNET_break_op (0);
1831 GNUNET_free_non_null (ns);
1832 GNUNET_free_non_null (ip);
1833 rh->proc (rh->proc_cls, 0, NULL);
1834 GNS_resolver_lookup_cancel (rh);
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1838 "Resolving `%s' to determine IP address of DNS server for GNS2DNS transition\n",
1840 /* resolve 'ip' to determine the IP(s) of the DNS
1842 g2dc = GNUNET_new (struct Gns2DnsContext);
1845 g2dc->rh = GNUNET_new (struct GNS_ResolverHandle);
1846 g2dc->rh->authority_zone = rh->ac_tail->authority_info.gns_authority;
1847 ip = translate_dot_plus (rh, ip);
1848 g2dc->rh->name = ip;
1849 g2dc->rh->name_resolution_pos = strlen (ip);
1850 g2dc->rh->proc = &handle_gns2dns_result;
1851 g2dc->rh->proc_cls = rh;
1852 g2dc->rh->record_type = GNUNET_GNSRECORD_TYPE_ANY;
1853 g2dc->rh->options = GNUNET_GNS_LO_DEFAULT;
1854 g2dc->rh->loop_limiter = rh->loop_limiter + 1;
1856 start_resolver_lookup (g2dc->rh);
1859 case GNUNET_DNSPARSER_TYPE_CNAME:
1864 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1867 if ( (NULL == cname) ||
1868 (off != rd[i].data_size) )
1870 GNUNET_break_op (0); /* record not well-formed */
1871 rh->proc (rh->proc_cls, 0, NULL);
1872 GNS_resolver_lookup_cancel (rh);
1873 GNUNET_free_non_null (cname);
1876 handle_gns_cname_result (rh,
1878 GNUNET_free (cname);
1881 /* FIXME: handle DNAME */
1887 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1888 _("GNS lookup recursion failed (no delegation record found)\n"));
1889 rh->proc (rh->proc_cls, 0, NULL);
1890 GNS_resolver_lookup_cancel (rh);
1895 * Function called once the namestore has completed the request for
1898 * @param cls closure with the `struct CacheOps`
1899 * @param success #GNUNET_OK on success
1900 * @param emsg error message
1903 namecache_cache_continuation (void *cls,
1907 struct CacheOps *co = cls;
1909 co->namecache_qe_cache = NULL;
1911 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1912 _("Failed to cache GNS resolution: %s\n"),
1914 GNUNET_CONTAINER_DLL_remove (co_head,
1922 * Iterator called on each result obtained for a DHT
1923 * operation that expects a reply
1925 * @param cls closure with the `struct GNS_ResolverHandle`
1926 * @param exp when will this value expire
1927 * @param key key of the result
1928 * @param get_path peers on reply path (or NULL if not recorded)
1929 * [0] = datastore's first neighbor, [length - 1] = local peer
1930 * @param get_path_length number of entries in @a get_path
1931 * @param put_path peers on the PUT path (or NULL if not recorded)
1932 * [0] = origin, [length - 1] = datastore
1933 * @param put_path_length number of entries in @a put_path
1934 * @param type type of the result
1935 * @param size number of bytes in data
1936 * @param data pointer to the result data
1939 handle_dht_response (void *cls,
1940 struct GNUNET_TIME_Absolute exp,
1941 const struct GNUNET_HashCode *key,
1942 const struct GNUNET_PeerIdentity *get_path,
1943 unsigned int get_path_length,
1944 const struct GNUNET_PeerIdentity *put_path,
1945 unsigned int put_path_length,
1946 enum GNUNET_BLOCK_Type type,
1947 size_t size, const void *data)
1949 struct GNS_ResolverHandle *rh = cls;
1950 struct AuthorityChain *ac = rh->ac_tail;
1951 const struct GNUNET_GNSRECORD_Block *block;
1952 struct CacheOps *co;
1954 GNUNET_DHT_get_stop (rh->get_handle);
1955 rh->get_handle = NULL;
1956 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1957 rh->dht_heap_node = NULL;
1958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1959 "Handling response from the DHT\n");
1960 if (size < sizeof (struct GNUNET_GNSRECORD_Block))
1962 /* how did this pass DHT block validation!? */
1964 rh->proc (rh->proc_cls, 0, NULL);
1965 GNS_resolver_lookup_cancel (rh);
1970 ntohl (block->purpose.size) +
1971 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
1972 sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
1974 /* how did this pass DHT block validation!? */
1976 rh->proc (rh->proc_cls, 0, NULL);
1977 GNS_resolver_lookup_cancel (rh);
1981 GNUNET_GNSRECORD_block_decrypt (block,
1982 &ac->authority_info.gns_authority,
1984 &handle_gns_resolution_result,
1987 GNUNET_break_op (0); /* block was ill-formed */
1988 rh->proc (rh->proc_cls, 0, NULL);
1989 GNS_resolver_lookup_cancel (rh);
1992 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us)
1994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1995 "Received expired block from the DHT, will not cache it.\n");
1998 /* Cache well-formed blocks */
1999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2000 "Caching response from the DHT in namecache\n");
2001 co = GNUNET_new (struct CacheOps);
2002 co->namecache_qe_cache = GNUNET_NAMECACHE_block_cache (namecache_handle,
2004 &namecache_cache_continuation,
2006 GNUNET_CONTAINER_DLL_insert (co_head,
2013 * Initiate a DHT query for a set of GNS records.
2015 * @param rh resolution handle
2016 * @param query key to use in the DHT lookup
2019 start_dht_request (struct GNS_ResolverHandle *rh,
2020 const struct GNUNET_HashCode *query)
2022 struct GNS_ResolverHandle *rx;
2024 GNUNET_assert (NULL == rh->get_handle);
2025 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
2026 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2028 DHT_GNS_REPLICATION_LEVEL,
2029 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2031 &handle_dht_response, rh);
2032 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2034 GNUNET_TIME_absolute_get ().abs_value_us);
2035 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
2037 /* fail longest-standing DHT request */
2038 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
2039 GNUNET_assert (NULL != rx);
2040 rx->proc (rx->proc_cls, 0, NULL);
2041 GNS_resolver_lookup_cancel (rx);
2047 * Process a records that were decrypted from a block that we got from
2048 * the namecache. Simply calls #handle_gns_resolution_result().
2050 * @param cls closure with the `struct GNS_ResolverHandle`
2051 * @param rd_count number of entries in @a rd array
2052 * @param rd array of records with data to store
2055 handle_gns_namecache_resolution_result (void *cls,
2056 unsigned int rd_count,
2057 const struct GNUNET_GNSRECORD_Data *rd)
2059 struct GNS_ResolverHandle *rh = cls;
2061 handle_gns_resolution_result (rh,
2068 * Process a record that was stored in the namecache.
2070 * @param cls closure with the `struct GNS_ResolverHandle`
2071 * @param block block that was stored in the namecache
2074 handle_namecache_block_response (void *cls,
2075 const struct GNUNET_GNSRECORD_Block *block)
2077 struct GNS_ResolverHandle *rh = cls;
2078 struct AuthorityChain *ac = rh->ac_tail;
2079 const char *label = ac->label;
2080 const struct GNUNET_CRYPTO_EcdsaPublicKey *auth = &ac->authority_info.gns_authority;
2081 struct GNUNET_HashCode query;
2083 GNUNET_assert (NULL != rh->namecache_qe);
2084 rh->namecache_qe = NULL;
2085 if ( ( (GNUNET_GNS_LO_DEFAULT == rh->options) ||
2086 ( (GNUNET_GNS_LO_LOCAL_MASTER == rh->options) &&
2087 (ac != rh->ac_head) ) ) &&
2088 ( (NULL == block) ||
2089 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
2091 /* namecache knows nothing; try DHT lookup */
2092 GNUNET_GNSRECORD_query_from_public_key (auth,
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2096 "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
2098 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
2099 GNUNET_h2s (&query));
2100 start_dht_request (rh, &query);
2104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2105 "Received result from namecache for label `%s'\n",
2108 if ( (NULL == block) ||
2109 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
2111 /* DHT not permitted and no local result, fail */
2112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2113 "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
2115 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2116 rh->proc (rh->proc_cls, 0, NULL);
2117 GNS_resolver_lookup_cancel (rh);
2121 GNUNET_GNSRECORD_block_decrypt (block,
2124 &handle_gns_namecache_resolution_result,
2127 GNUNET_break_op (0); /* block was ill-formed */
2128 /* try DHT instead */
2129 GNUNET_GNSRECORD_query_from_public_key (auth,
2132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2133 "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
2135 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
2136 GNUNET_h2s (&query));
2137 start_dht_request (rh, &query);
2144 * Lookup tail of our authority chain in the namecache.
2146 * @param rh query we are processing
2149 recursive_gns_resolution_namecache (struct GNS_ResolverHandle *rh)
2151 struct AuthorityChain *ac = rh->ac_tail;
2152 struct GNUNET_HashCode query;
2154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2155 "Starting GNS resolution for `%s' in zone %s\n",
2157 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2158 GNUNET_GNSRECORD_query_from_public_key (&ac->authority_info.gns_authority,
2161 if (GNUNET_YES == use_cache)
2164 = GNUNET_NAMECACHE_lookup_block (namecache_handle,
2166 &handle_namecache_block_response,
2168 GNUNET_assert (NULL != rh->namecache_qe);
2172 start_dht_request (rh, &query);
2178 * Function called with the result from a revocation check.
2180 * @param cls the `struct GNS_ResovlerHandle`
2181 * @param is_valid #GNUNET_YES if the zone was not yet revoked
2184 handle_revocation_result (void *cls,
2187 struct GNS_ResolverHandle *rh = cls;
2188 struct AuthorityChain *ac = rh->ac_tail;
2190 rh->rev_check = NULL;
2191 if (GNUNET_YES != is_valid)
2193 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2194 _("Zone %s was revoked, resolution fails\n"),
2195 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2196 rh->proc (rh->proc_cls, 0, NULL);
2197 GNS_resolver_lookup_cancel (rh);
2200 recursive_gns_resolution_namecache (rh);
2205 * Perform revocation check on tail of our authority chain.
2207 * @param rh query we are processing
2210 recursive_gns_resolution_revocation (struct GNS_ResolverHandle *rh)
2212 struct AuthorityChain *ac = rh->ac_tail;
2214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2215 "Starting revocation check for zone %s\n",
2216 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2217 rh->rev_check = GNUNET_REVOCATION_query (cfg,
2218 &ac->authority_info.gns_authority,
2219 &handle_revocation_result,
2221 GNUNET_assert (NULL != rh->rev_check);
2226 * Task scheduled to continue with the resolution process.
2228 * @param cls the `struct GNS_ResolverHandle` of the resolution
2231 recursive_resolution (void *cls)
2233 struct GNS_ResolverHandle *rh = cls;
2236 if (MAX_RECURSION < rh->loop_limiter++)
2238 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2239 "Encountered unbounded recursion resolving `%s'\n",
2241 rh->proc (rh->proc_cls, 0, NULL);
2242 GNS_resolver_lookup_cancel (rh);
2245 if (GNUNET_YES == rh->ac_tail->gns_authority)
2246 recursive_gns_resolution_revocation (rh);
2248 recursive_dns_resolution (rh);
2253 * Begin the resolution process from 'name', starting with
2254 * the identification of the zone specified by 'name'.
2256 * @param rh resolution to perform
2259 start_resolver_lookup (struct GNS_ResolverHandle *rh)
2261 struct AuthorityChain *ac;
2266 if (1 == inet_pton (AF_INET,
2270 /* name is IPv4 address, pretend it's an A record */
2271 struct GNUNET_GNSRECORD_Data rd;
2274 rd.data_size = sizeof (v4);
2275 rd.expiration_time = UINT64_MAX;
2276 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
2278 rh->proc (rh->proc_cls, 1, &rd);
2279 GNS_resolver_lookup_cancel (rh);
2282 if (1 == inet_pton (AF_INET6,
2286 /* name is IPv6 address, pretend it's an AAAA record */
2287 struct GNUNET_GNSRECORD_Data rd;
2290 rd.data_size = sizeof (v6);
2291 rd.expiration_time = UINT64_MAX;
2292 rd.record_type = GNUNET_DNSPARSER_TYPE_AAAA;
2294 rh->proc (rh->proc_cls, 1, &rd);
2295 GNS_resolver_lookup_cancel (rh);
2298 if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
2299 (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
2300 ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
2301 (GNUNET_YES != is_zkey_tld (rh->name)) ) )
2303 /* use standard DNS lookup */
2306 switch (rh->record_type)
2308 case GNUNET_DNSPARSER_TYPE_A:
2311 case GNUNET_DNSPARSER_TYPE_AAAA:
2318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2319 "Doing standard DNS lookup for `%s'\n",
2321 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
2328 if (is_zkey_tld (rh->name))
2330 /* Name ends with ".zkey", try to replace authority zone with zkey
2332 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
2333 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
2336 GNUNET_CRYPTO_ecdsa_public_key_from_string (y,
2338 &rh->authority_zone)) )
2340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2341 _("Hostname `%s' is not well-formed, resolution fails\n"),
2343 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
2345 GNUNET_free_non_null (y);
2349 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
2350 GNUNET_free (resolver_lookup_get_next_label (rh));
2352 ac = GNUNET_new (struct AuthorityChain);
2354 ac->label = resolver_lookup_get_next_label (rh);
2355 ac->suggested_shortening_label = NULL;
2356 if (NULL == ac->label)
2357 /* name was just "gnu", so we default to label '+' */
2358 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
2359 ac->gns_authority = GNUNET_YES;
2360 ac->authority_info.gns_authority = rh->authority_zone;
2361 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
2364 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
2370 * Lookup of a record in a specific zone calls lookup result processor
2373 * @param zone the zone to perform the lookup in
2374 * @param record_type the record type to look up
2375 * @param name the name to look up
2376 * @param shorten_key a private key for use with PSEU import (can be NULL)
2377 * @param options local options to control local lookup
2378 * @param proc the processor to call on result
2379 * @param proc_cls the closure to pass to @a proc
2380 * @return handle to cancel operation
2382 struct GNS_ResolverHandle *
2383 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
2384 uint32_t record_type,
2386 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key,
2387 enum GNUNET_GNS_LocalOptions options,
2388 GNS_ResultProcessor proc, void *proc_cls)
2390 struct GNS_ResolverHandle *rh;
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393 (NULL == shorten_key)
2394 ? "Starting lookup for `%s' with shortening disabled\n"
2395 : "Starting lookup for `%s' with shortening enabled\n",
2397 rh = GNUNET_new (struct GNS_ResolverHandle);
2398 GNUNET_CONTAINER_DLL_insert (rlh_head,
2401 rh->authority_zone = *zone;
2403 rh->proc_cls = proc_cls;
2404 rh->options = options;
2405 rh->record_type = record_type;
2406 rh->name = GNUNET_strdup (name);
2407 rh->name_resolution_pos = strlen (name);
2408 if (NULL != shorten_key)
2410 rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
2411 *rh->shorten_key = *shorten_key;
2413 start_resolver_lookup (rh);
2419 * Cancel active resolution (i.e. client disconnected).
2421 * @param rh resolution to abort
2424 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
2426 struct DnsResult *dr;
2427 struct AuthorityChain *ac;
2428 struct VpnContext *vpn_ctx;
2430 GNUNET_CONTAINER_DLL_remove (rlh_head,
2433 while (NULL != (ac = rh->ac_head))
2435 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
2438 GNUNET_free (ac->label);
2439 GNUNET_free_non_null (ac->suggested_shortening_label);
2442 if (NULL != rh->g2dc)
2444 /* rh->g2dc->rh is NOT in the DLL yet, so to enable us
2445 using GNS_resolver_lookup_cancel here, we need to
2447 if (NULL != rh->g2dc->rh)
2449 GNUNET_CONTAINER_DLL_insert (rlh_head,
2452 GNS_resolver_lookup_cancel (rh->g2dc->rh);
2453 rh->g2dc->rh = NULL;
2455 GNUNET_free (rh->g2dc->ns);
2456 GNUNET_free (rh->g2dc);
2459 if (NULL != rh->task_id)
2461 GNUNET_SCHEDULER_cancel (rh->task_id);
2464 if (NULL != rh->get_handle)
2466 GNUNET_DHT_get_stop (rh->get_handle);
2467 rh->get_handle = NULL;
2469 if (NULL != rh->dht_heap_node)
2471 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
2472 rh->dht_heap_node = NULL;
2474 if (NULL != (vpn_ctx = rh->vpn_ctx))
2476 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
2477 GNUNET_free (vpn_ctx->rd_data);
2478 GNUNET_free (vpn_ctx);
2480 if (NULL != rh->dns_request)
2482 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
2483 rh->dns_request = NULL;
2485 if (NULL != rh->namecache_qe)
2487 GNUNET_NAMECACHE_cancel (rh->namecache_qe);
2488 rh->namecache_qe = NULL;
2490 if (NULL != rh->rev_check)
2492 GNUNET_REVOCATION_query_cancel (rh->rev_check);
2493 rh->rev_check = NULL;
2495 if (NULL != rh->std_resolve)
2497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2498 "Canceling standard DNS resolution\n");
2499 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
2500 rh->std_resolve = NULL;
2502 while (NULL != (dr = rh->dns_result_head))
2504 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
2505 rh->dns_result_tail,
2509 GNUNET_free_non_null (rh->shorten_key);
2510 GNUNET_free (rh->name);
2515 /* ***************** Resolver initialization ********************* */
2519 * Initialize the resolver
2521 * @param nc the namecache handle
2522 * @param dht the dht handle
2523 * @param c configuration handle
2524 * @param max_bg_queries maximum number of parallel background queries in dht
2527 GNS_resolver_init (struct GNUNET_NAMECACHE_Handle *nc,
2528 struct GNUNET_DHT_Handle *dht,
2529 const struct GNUNET_CONFIGURATION_Handle *c,
2530 unsigned long long max_bg_queries)
2535 namecache_handle = nc;
2538 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2539 max_allowed_background_queries = max_bg_queries;
2540 if (GNUNET_SYSERR == (use_cache = GNUNET_CONFIGURATION_get_value_yesno (c,
2543 use_cache = GNUNET_YES;
2544 if (GNUNET_NO == use_cache)
2545 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Namecache disabled\n");
2548 GNUNET_CONFIGURATION_get_value_string (c,
2553 /* user did not specify DNS resolver, use 8.8.8.8 */
2554 dns_ip = GNUNET_strdup ("8.8.8.8");
2556 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
2557 GNUNET_free (dns_ip);
2558 vpn_handle = GNUNET_VPN_connect (cfg);
2566 GNS_resolver_done ()
2568 struct GNS_ResolverHandle *rh;
2569 struct CacheOps *co;
2571 /* abort active resolutions */
2572 while (NULL != (rh = rlh_head))
2574 rh->proc (rh->proc_cls, 0, NULL);
2575 GNS_resolver_lookup_cancel (rh);
2577 while (NULL != (co = co_head))
2579 GNUNET_CONTAINER_DLL_remove (co_head,
2582 GNUNET_NAMECACHE_cancel (co->namecache_qe_cache);
2585 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
2586 dht_lookup_heap = NULL;
2587 GNUNET_DNSSTUB_stop (dns_handle);
2589 GNUNET_VPN_disconnect (vpn_handle);
2592 namecache_handle = NULL;
2596 /* *************** common helper functions (do not really belong here) *********** */
2599 * Checks if @a name ends in ".TLD"
2601 * @param name the name to check
2602 * @param tld the TLD to check for
2603 * @return GNUNET_YES or GNUNET_NO
2606 is_tld (const char* name, const char* tld)
2610 if (strlen (name) <= strlen (tld))
2612 offset = strlen (name) - strlen (tld);
2613 if (0 != strcmp (name + offset, tld))
2619 /* end of gnunet-service-gns_resolver.c */