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_dns_service.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_revocation_service.h"
36 #include "gnunet_dnsparser_lib.h"
37 #include "gnunet_tun_lib.h"
38 #include "gnunet_gns_service.h"
40 #include "gnunet-service-gns_resolver.h"
41 #include "gnunet_vpn_service.h"
45 * Default DHT timeout for lookups.
47 #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
50 * Default timeout for DNS lookups.
52 #define DNS_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
55 * Default timeout for VPN redirections.
57 #define VPN_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
60 * DHT replication level
62 #define DHT_GNS_REPLICATION_LEVEL 5
65 * How deep do we allow recursions to go before we abort?
67 #define MAX_RECURSION 256
71 * DLL to hold the authority chain we had to pass in the resolution
79 struct AuthorityChain *prev;
84 struct AuthorityChain *next;
87 * Resolver handle this entry in the chain belongs to.
89 struct GNS_ResolverHandle *rh;
92 * label/name corresponding to the authority
97 * label/name suggested for shortening to the authority
99 char *suggested_shortening_label;
102 * Do we already try to shorten this authority?
104 int shortening_started;
107 * #GNUNET_YES if the authority was a GNS authority,
108 * #GNUNET_NO if the authority was a DNS authority.
113 * Information about the resolver authority for this label.
119 * The zone of the GNS authority
121 struct GNUNET_CRYPTO_EcdsaPublicKey gns_authority;
126 * Domain of the DNS resolver that is the authority.
127 * (appended to construct the DNS name to resolve;
128 * this is NOT the DNS name of the DNS server!).
130 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
133 * IP address of the DNS resolver that is authoritative.
134 * (this implementation currently only supports one
137 struct sockaddr_storage dns_ip;
147 * A result we got from DNS.
155 struct DnsResult *next;
160 struct DnsResult *prev;
163 * Binary value stored in the DNS record (appended to this struct)
168 * Expiration time for the DNS record, 0 if we didn't
169 * get anything useful (i.e. 'gethostbyname()' was used).
171 uint64_t expiration_time;
174 * Number of bytes in @e data.
179 * Type of the GNS/DNS record.
181 uint32_t record_type;
187 * Closure for #vpn_allocation_cb.
193 * Which resolution process are we processing.
195 struct GNS_ResolverHandle *rh;
198 * Handle to the VPN request that we were performing.
200 struct GNUNET_VPN_RedirectionRequest *vpn_request;
203 * Number of records serialized in @e rd_data.
205 unsigned int rd_count;
208 * Serialized records.
213 * Number of bytes in @e rd_data.
220 * Information we keep during the resolution of an
221 * IP address for a DNS server while handling a
224 struct Gns2DnsContext
228 * DNS domain in which the resolution will continue
229 * (first part of the GNS2DNS record).
234 * Handle for the resolution of the IP part of the
235 * GNS2DNS record. Will return to us the addresses
236 * of the DNS resolver to use.
238 struct GNS_ResolverHandle *rh;
244 * Handle to a currenty pending resolution. On result (positive or
245 * negative) the #GNS_ResultProcessor is called.
247 struct GNS_ResolverHandle
253 struct GNS_ResolverHandle *next;
258 struct GNS_ResolverHandle *prev;
261 * The top-level GNS authoritative zone to query
263 struct GNUNET_CRYPTO_EcdsaPublicKey authority_zone;
266 * called when resolution phase finishes
268 GNS_ResultProcessor proc;
271 * closure passed to @e proc
276 * Handle used during GNS2DNS resolution for looking up the
277 * IP address of the DNS server.
279 struct Gns2DnsContext *g2dc;
282 * Handle for DHT lookups. should be NULL if no lookups are in progress
284 struct GNUNET_DHT_GetHandle *get_handle;
287 * Handle to a VPN request, NULL if none is active.
289 struct VpnContext *vpn_ctx;
292 * Socket for a DNS request, NULL if none is active.
294 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
297 * Handle for standard DNS resolution, NULL if none is active.
299 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
302 * Pending Namecache lookup task
304 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe;
307 * Pending revocation check.
309 struct GNUNET_REVOCATION_Query *rev_check;
312 * Heap node associated with this lookup. Used to limit number of
313 * concurrent requests.
315 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
318 * DLL to store the authority chain
320 struct AuthorityChain *ac_head;
323 * DLL to store the authority chain
325 struct AuthorityChain *ac_tail;
328 * ID of a task associated with the resolution process.
330 struct GNUNET_SCHEDULER_Task * task_id;
333 * The name to resolve
338 * DLL of results we got from DNS.
340 struct DnsResult *dns_result_head;
343 * DLL of results we got from DNS.
345 struct DnsResult *dns_result_tail;
348 * Current offset in 'name' where we are resolving.
350 size_t name_resolution_pos;
355 enum GNUNET_GNS_LocalOptions options;
358 * For SRV and TLSA records, the number of the
359 * protocol specified in the name. 0 if no protocol was given.
364 * For SRV and TLSA records, the number of the
365 * service specified in the name. 0 if no service was given.
370 * Desired type for the resolution.
375 * We increment the loop limiter for each step in a recursive
376 * resolution. If it passes our threshold (i.e. due to
377 * self-recursion in the resolution, i.e CNAME fun), we stop.
379 unsigned int loop_limiter;
385 * Active namestore caching operations.
391 * Organized in a DLL.
393 struct CacheOps *next;
396 * Organized in a DLL.
398 struct CacheOps *prev;
401 * Pending Namestore caching task.
403 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe_cache;
409 * Our handle to the namecache service
411 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
414 * Our handle to the vpn service
416 static struct GNUNET_VPN_Handle *vpn_handle;
419 * Resolver handle to the dht
421 static struct GNUNET_DHT_Handle *dht_handle;
424 * Handle to perform DNS lookups.
426 static struct GNUNET_DNSSTUB_Context *dns_handle;
429 * Heap for limiting parallel DHT lookups
431 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
434 * Maximum amount of parallel queries to the DHT
436 static unsigned long long max_allowed_background_queries;
439 * Head of resolver lookup list
441 static struct GNS_ResolverHandle *rlh_head;
444 * Tail of resolver lookup list
446 static struct GNS_ResolverHandle *rlh_tail;
449 * Organized in a DLL.
451 static struct CacheOps *co_head;
454 * Organized in a DLL.
456 static struct CacheOps *co_tail;
461 static int use_cache;
464 * Global configuration.
466 static const struct GNUNET_CONFIGURATION_Handle *cfg;
470 * Determine if this name is canonical (is a legal name in a zone, without delegation);
471 * note that we do not test that the name does not contain illegal characters, we only
472 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
473 * even though they consist of multiple labels.
476 * a.b.gnu = not canonical
478 * _foo._srv = canonical
479 * _f.bar = not canonical
481 * @param name the name to test
482 * @return #GNUNET_YES if canonical
485 is_canonical (const char *name)
490 if (NULL == strchr (name, '.'))
495 while (NULL != (dot = strchr (pos, '.')))
503 /* ************************** Resolution **************************** */
506 * Expands a name ending in .+ with the zone of origin.
508 * @param rh resolution context
509 * @param name name to modify (to be free'd or returned)
510 * @return updated name
513 translate_dot_plus (struct GNS_ResolverHandle *rh,
517 size_t s_len = strlen (name);
519 if (0 != strcmp (&name[s_len - 2],
521 return name; /* did not end in ".+" */
522 GNUNET_assert (GNUNET_YES == rh->ac_tail->gns_authority);
523 GNUNET_asprintf (&ret,
527 GNUNET_GNSRECORD_pkey_to_zkey (&rh->ac_tail->authority_info.gns_authority));
534 * Task scheduled to asynchronously fail a resolution.
536 * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
539 fail_resolution (void *cls)
541 struct GNS_ResolverHandle *rh = cls;
544 rh->proc (rh->proc_cls, 0, NULL);
545 GNS_resolver_lookup_cancel (rh);
549 #if (defined WINDOWS) || (defined DARWIN)
550 /* Don't have this on W32, here's a naive implementation
551 * Was somehow removed on OS X ... */
553 memrchr (const void *s,
557 const unsigned char *ucs = s;
560 for (i = n - 1; i >= 0; i--)
561 if (c == (int) ucs[i])
562 return (void *) &ucs[i];
569 * Get the next, rightmost label from the name that we are trying to resolve,
570 * and update the resolution position accordingly. Labels usually consist
571 * of up to 63 characters without a period ("."); however, we use a special
572 * convention to support SRV and TLSA records where the domain name
573 * includes an encoding for a service and protocol in the name. The
574 * syntax (see RFC 2782) here is "_Service._Proto.Name" and in this
575 * special case we include the "_Service._Proto" in the rightmost label.
576 * Thus, for "_443._tcp.foo.bar" we first return the label "bar" and then
577 * the label "_443._tcp.foo". The special case is detected by the
578 * presence of labels beginning with an underscore. Whenever a label
579 * begins with an underscore, it is combined with the label to its right
580 * (and the "." is preserved).
582 * @param rh handle to the resolution operation to get the next label from
583 * @return NULL if there are no more labels
586 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
597 if (0 == rh->name_resolution_pos)
599 dot = memrchr (rh->name,
601 rh->name_resolution_pos);
604 /* done, this was the last one */
605 len = rh->name_resolution_pos;
607 rh->name_resolution_pos = 0;
611 /* advance by one label */
612 len = rh->name_resolution_pos - (dot - rh->name) - 1;
614 rh->name_resolution_pos = dot - rh->name;
618 ret = GNUNET_strndup (rp, len);
619 /* If we have labels starting with underscore with label on
620 * the right (SRV/DANE/BOX case), determine port/protocol;
621 * The format of `rh->name` must be "_PORT._PROTOCOL".
623 if ( ('_' == rh->name[0]) &&
624 (NULL != (dot = memrchr (rh->name,
626 rh->name_resolution_pos))) &&
628 (NULL == memrchr (rh->name,
632 srv_name = GNUNET_strndup (&rh->name[1],
633 (dot - rh->name) - 1);
634 proto_name = GNUNET_strndup (&dot[2],
635 rh->name_resolution_pos - (dot - rh->name) - 1);
636 rh->name_resolution_pos = 0;
637 pe = getprotobyname (proto_name);
640 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
641 _("Protocol `%s' unknown, skipping labels.\n"),
643 GNUNET_free (proto_name);
644 GNUNET_free (srv_name);
647 se = getservbyname (srv_name,
651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
652 _("Service `%s' unknown for protocol `%s', skipping labels.\n"),
655 GNUNET_free (proto_name);
656 GNUNET_free (srv_name);
659 rh->protocol = pe->p_proto;
660 rh->service = se->s_port;
667 * Gives the cummulative result obtained to the callback and clean up the request.
669 * @param rh resolution process that has culminated in a result
672 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
674 struct DnsResult *pos;
679 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
682 struct GNUNET_GNSRECORD_Data rd[n];
685 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
687 rd[i].data = pos->data;
688 rd[i].data_size = pos->data_size;
689 rd[i].record_type = pos->record_type;
690 if (0 == pos->expiration_time)
692 rd[i].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
693 rd[i].expiration_time = 0;
697 rd[i].flags = GNUNET_GNSRECORD_RF_NONE;
698 rd[i].expiration_time = pos->expiration_time;
702 GNUNET_assert (i == n);
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
704 "Transmitting standard DNS result with %u records\n",
706 rh->proc (rh->proc_cls,
710 GNS_resolver_lookup_cancel (rh);
715 * Add a result from DNS to the records to be returned to the application.
717 * @param rh resolution request to extend with a result
718 * @param expiration_time expiration time for the answer
719 * @param record_type DNS record type of the answer
720 * @param data_size number of bytes in @a data
721 * @param data binary data to return in DNS record
724 add_dns_result (struct GNS_ResolverHandle *rh,
725 uint64_t expiration_time,
726 uint32_t record_type,
730 struct DnsResult *res;
732 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
733 res->expiration_time = expiration_time;
734 res->data_size = data_size;
735 res->record_type = record_type;
737 GNUNET_memcpy (&res[1], data, data_size);
738 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
745 * We had to do a DNS lookup. Convert the result (if any) and return
748 * @param cls closure with the `struct GNS_ResolverHandle`
749 * @param addr one of the addresses of the host, NULL for the last address
750 * @param addrlen length of the address
753 handle_dns_result (void *cls,
754 const struct sockaddr *addr,
757 struct GNS_ResolverHandle *rh = cls;
758 const struct sockaddr_in *sa4;
759 const struct sockaddr_in6 *sa6;
763 rh->std_resolve = NULL;
764 transmit_lookup_dns_result (rh);
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "Received %u bytes of DNS IP data\n",
770 switch (addr->sa_family)
773 sa4 = (const struct sockaddr_in *) addr;
775 0 /* expiration time is unknown */,
776 GNUNET_DNSPARSER_TYPE_A,
777 sizeof (struct in_addr),
781 sa6 = (const struct sockaddr_in6 *) addr;
783 0 /* expiration time is unknown */,
784 GNUNET_DNSPARSER_TYPE_AAAA,
785 sizeof (struct in6_addr),
796 * Task scheduled to continue with the resolution process.
798 * @param cls the 'struct GNS_ResolverHandle' of the resolution
799 * @param tc task context
802 recursive_resolution (void *cls);
806 * Begin the resolution process from 'name', starting with
807 * the identification of the zone specified by 'name'.
809 * @param rh resolution to perform
812 start_resolver_lookup (struct GNS_ResolverHandle *rh);
816 * Function called with the result of a DNS resolution.
818 * @param cls the request handle of the resolution that
819 * we were attempting to make
820 * @param rs socket that received the response
821 * @param dns dns response, never NULL
822 * @param dns_len number of bytes in @a dns
825 dns_result_parser (void *cls,
826 struct GNUNET_DNSSTUB_RequestSocket *rs,
827 const struct GNUNET_TUN_DnsHeader *dns,
830 struct GNS_ResolverHandle *rh = cls;
831 struct GNUNET_DNSPARSER_Packet *p;
832 const struct GNUNET_DNSPARSER_Record *rec;
833 unsigned int rd_count;
836 rh->dns_request = NULL;
837 GNUNET_SCHEDULER_cancel (rh->task_id);
839 p = GNUNET_DNSPARSER_parse ((const char *) dns,
843 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
844 _("Failed to parse DNS response\n"));
845 rh->proc (rh->proc_cls, 0, NULL);
846 GNS_resolver_lookup_cancel (rh);
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850 "Received DNS response for `%s' with %u answers\n",
852 (unsigned int) p->num_answers);
853 if ( (p->num_answers > 0) &&
854 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
855 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
857 GNUNET_free (rh->name);
858 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
859 rh->name_resolution_pos = strlen (rh->name);
860 start_resolver_lookup (rh);
861 GNUNET_DNSPARSER_free_packet (p);
865 /* convert from (parsed) DNS to (binary) GNS format! */
866 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
868 struct GNUNET_GNSRECORD_Data rd[rd_count];
870 char buf[UINT16_MAX];
876 memset (rd, 0, sizeof (rd));
877 for (i=0;i<rd_count;i++)
879 if (i < p->num_answers)
880 rec = &p->answers[i];
881 else if (i < p->num_answers + p->num_authority_records)
882 rec = &p->authority_records[i - p->num_answers];
884 rec = &p->additional_records[i - p->num_answers - p->num_authority_records];
885 /* As we copied the full DNS name to 'rh->ac_tail->label', this
886 should be the correct check to see if this record is actually
887 a record for our label... */
888 if (0 != strcmp (rec->name,
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "Dropping record `%s', does not match desired name `%s'\n",
898 rd[i - skip].record_type = rec->type;
899 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
902 case GNUNET_DNSPARSER_TYPE_A:
903 if (rec->data.raw.data_len != sizeof (struct in_addr))
909 rd[i - skip].data_size = rec->data.raw.data_len;
910 rd[i - skip].data = rec->data.raw.data;
912 case GNUNET_DNSPARSER_TYPE_AAAA:
913 if (rec->data.raw.data_len != sizeof (struct in6_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_CNAME:
923 case GNUNET_DNSPARSER_TYPE_PTR:
924 case GNUNET_DNSPARSER_TYPE_NS:
927 GNUNET_DNSPARSER_builder_add_name (buf,
936 rd[i - skip].data_size = buf_off - buf_start;
937 rd[i - skip].data = &buf[buf_start];
939 case GNUNET_DNSPARSER_TYPE_SOA:
942 GNUNET_DNSPARSER_builder_add_soa (buf,
951 rd[i - skip].data_size = buf_off - buf_start;
952 rd[i - skip].data = &buf[buf_start];
954 case GNUNET_DNSPARSER_TYPE_MX:
957 GNUNET_DNSPARSER_builder_add_mx (buf,
966 rd[i - skip].data_size = buf_off - buf_start;
967 rd[i - skip].data = &buf[buf_start];
969 case GNUNET_DNSPARSER_TYPE_SRV:
972 GNUNET_DNSPARSER_builder_add_srv (buf,
981 rd[i - skip].data_size = buf_off - buf_start;
982 rd[i - skip].data = &buf[buf_start];
985 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
986 _("Skipping record of unsupported type %d\n"),
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 "Returning DNS response for `%s' with %u answers\n",
995 (unsigned int) p->num_answers);
996 rh->proc (rh->proc_cls, rd_count - skip, rd);
997 GNS_resolver_lookup_cancel (rh);
999 GNUNET_DNSPARSER_free_packet (p);
1004 * Perform recursive DNS resolution. Asks the given DNS resolver to
1005 * resolve "rh->dns_name", possibly recursively proceeding following
1006 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
1007 * we find the answer.
1009 * @param rh resolution information
1012 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
1014 struct AuthorityChain *ac;
1016 struct GNUNET_DNSPARSER_Query *query;
1017 struct GNUNET_DNSPARSER_Packet *p;
1019 size_t dns_request_length;
1022 GNUNET_assert (NULL != ac);
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1024 "Starting DNS lookup for `%s'\n",
1026 GNUNET_assert (GNUNET_NO == ac->gns_authority);
1027 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
1030 sa_len = sizeof (struct sockaddr_in);
1033 sa_len = sizeof (struct sockaddr_in6);
1037 rh->proc (rh->proc_cls, 0, NULL);
1038 GNS_resolver_lookup_cancel (rh);
1041 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
1042 query->name = GNUNET_strdup (ac->label);
1043 query->type = rh->record_type;
1044 query->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1045 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
1048 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1050 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
1051 p->flags.recursion_desired = 1;
1053 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
1056 rh->proc (rh->proc_cls, 0, NULL);
1057 GNS_resolver_lookup_cancel (rh);
1061 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
1062 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
1068 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
1072 GNUNET_free (dns_request);
1073 GNUNET_DNSPARSER_free_packet (p);
1078 * We encountered a CNAME record during our resolution.
1079 * Merge it into our chain.
1081 * @param rh resolution we are performing
1082 * @param cname value of the cname record we got for the current
1083 * authority chain tail
1086 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
1091 struct AuthorityChain *ac;
1093 nlen = strlen (cname);
1096 &cname[nlen - 2])) )
1098 /* CNAME resolution continues relative to current domain */
1099 if (0 == rh->name_resolution_pos)
1101 res = GNUNET_strndup (cname, nlen - 2);
1102 rh->name_resolution_pos = nlen - 2;
1106 GNUNET_asprintf (&res,
1108 (int) rh->name_resolution_pos,
1112 rh->name_resolution_pos = strlen (res);
1114 GNUNET_free (rh->name);
1116 ac = GNUNET_new (struct AuthorityChain);
1118 ac->gns_authority = GNUNET_YES;
1119 ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
1120 ac->label = resolver_lookup_get_next_label (rh);
1121 ac->suggested_shortening_label = NULL;
1122 ac->shortening_started = GNUNET_NO;
1123 /* add AC to tail */
1124 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1127 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1131 /* name is absolute, start from the beginning */
1132 GNUNET_free (rh->name);
1133 rh->name = GNUNET_strdup (cname);
1134 rh->name_resolution_pos = strlen (rh->name);
1135 start_resolver_lookup (rh);
1140 * Process a records that were decrypted from a block.
1142 * @param cls closure with the 'struct GNS_ResolverHandle'
1143 * @param rd_count number of entries in @a rd array
1144 * @param rd array of records with data to store
1147 handle_gns_resolution_result (void *cls,
1148 unsigned int rd_count,
1149 const struct GNUNET_GNSRECORD_Data *rd);
1153 * Callback invoked from the VPN service once a redirection is
1154 * available. Provides the IP address that can now be used to
1155 * reach the requested destination. Replaces the "VPN" record
1156 * with the respective A/AAAA record and continues processing.
1158 * @param cls closure
1159 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1160 * will match 'result_af' from the request
1161 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1162 * that the VPN allocated for the redirection;
1163 * traffic to this IP will now be redirected to the
1164 * specified target peer; NULL on error
1167 vpn_allocation_cb (void *cls,
1169 const void *address)
1171 struct VpnContext *vpn_ctx = cls;
1172 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1173 struct GNUNET_GNSRECORD_Data rd[vpn_ctx->rd_count];
1176 vpn_ctx->vpn_request = NULL;
1178 GNUNET_assert (GNUNET_OK ==
1179 GNUNET_GNSRECORD_records_deserialize (vpn_ctx->rd_data_size,
1183 for (i=0;i<vpn_ctx->rd_count;i++)
1185 if (GNUNET_GNSRECORD_TYPE_VPN == rd[i].record_type)
1190 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1191 rd[i].data_size = sizeof (struct in_addr);
1192 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1194 rd[i].data = address;
1197 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1198 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1200 rd[i].data = address;
1201 rd[i].data_size = sizeof (struct in6_addr);
1209 GNUNET_assert (i < vpn_ctx->rd_count);
1210 handle_gns_resolution_result (rh,
1213 GNUNET_free (vpn_ctx->rd_data);
1214 GNUNET_free (vpn_ctx);
1219 * We've resolved the IP address for the DNS resolver to use
1220 * after encountering a GNS2DNS record.
1222 * TODO: Right now we only foward the request to ONE DNS resolver,
1223 * even if we get multiple IP addresses back; a correct implementation
1224 * should try all DNS resolvers.
1226 * @param cls the `struct GNS_ResolverHandle` where we encountered
1227 * the GNS2DNS record
1228 * @param rd_count number of records in @a rd
1229 * @param rd addresses for the DNS resolver (presumably)
1232 handle_gns2dns_result (void *cls,
1233 unsigned int rd_count,
1234 const struct GNUNET_GNSRECORD_Data *rd)
1236 struct GNS_ResolverHandle *rh = cls;
1237 struct AuthorityChain *ac;
1239 struct sockaddr *sa;
1240 struct sockaddr_in v4;
1241 struct sockaddr_in6 v6;
1244 /* find suitable A/AAAA record */
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1246 "Received %u results for IP address of DNS server for GNS2DNS transition\n",
1248 /* enable cleanup of 'rh' handle that comes next... */
1249 GNUNET_CONTAINER_DLL_insert (rlh_head,
1252 rh->g2dc->rh = NULL;
1255 for (j=0;j<rd_count;j++)
1257 switch (rd[j].record_type)
1259 case GNUNET_DNSPARSER_TYPE_A:
1260 if (sizeof (struct in_addr) != rd[j].data_size)
1262 GNUNET_break_op (0);
1263 rh->proc (rh->proc_cls, 0, NULL);
1264 GNS_resolver_lookup_cancel (rh);
1267 /* FIXME: might want to check if we support IPv4 here,
1268 and otherwise skip this one and hope we find another */
1269 memset (&v4, 0, sizeof (v4));
1270 sa_len = sizeof (v4);
1271 v4.sin_family = AF_INET;
1272 v4.sin_port = htons (53);
1273 #if HAVE_SOCKADDR_IN_SIN_LEN
1274 v4.sin_len = (u_char) sa_len;
1276 GNUNET_memcpy (&v4.sin_addr,
1278 sizeof (struct in_addr));
1279 sa = (struct sockaddr *) &v4;
1281 case GNUNET_DNSPARSER_TYPE_AAAA:
1282 if (sizeof (struct in6_addr) != rd[j].data_size)
1284 GNUNET_break_op (0);
1285 rh->proc (rh->proc_cls, 0, NULL);
1286 GNS_resolver_lookup_cancel (rh);
1289 /* FIXME: might want to check if we support IPv6 here,
1290 and otherwise skip this one and hope we find another */
1291 memset (&v6, 0, sizeof (v6));
1292 sa_len = sizeof (v6);
1293 v6.sin6_family = AF_INET6;
1294 v6.sin6_port = htons (53);
1295 #if HAVE_SOCKADDR_IN_SIN_LEN
1296 v6.sin6_len = (u_char) sa_len;
1298 GNUNET_memcpy (&v6.sin6_addr,
1300 sizeof (struct in6_addr));
1301 sa = (struct sockaddr *) &v6;
1311 /* we cannot continue; NS without A/AAAA */
1312 rh->proc (rh->proc_cls, 0, NULL);
1313 GNS_resolver_lookup_cancel (rh);
1316 /* expand authority chain */
1317 ac = GNUNET_new (struct AuthorityChain);
1319 GNUNET_assert (strlen (rh->g2dc->ns) <= GNUNET_DNSPARSER_MAX_NAME_LENGTH);
1320 strcpy (ac->authority_info.dns_authority.name,
1322 GNUNET_memcpy (&ac->authority_info.dns_authority.dns_ip,
1325 /* for DNS recursion, the label is the full DNS name,
1326 created from the remainder of the GNS name and the
1327 name in the NS record */
1328 GNUNET_asprintf (&ac->label,
1330 (int) rh->name_resolution_pos,
1332 (0 != rh->name_resolution_pos) ? "." : "",
1334 GNUNET_free (rh->g2dc->ns);
1335 GNUNET_free (rh->g2dc);
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 "Will continue resolution using DNS server `%s' to resolve `%s'\n",
1339 GNUNET_a2s (sa, sa_len),
1341 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1344 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1347 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1349 rh->proc (rh->proc_cls, 0, NULL);
1350 GNS_resolver_lookup_cancel (rh);
1354 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1360 * Process a records that were decrypted from a block.
1362 * @param cls closure with the `struct GNS_ResolverHandle`
1363 * @param rd_count number of entries in @a rd array
1364 * @param rd array of records with data to store
1367 handle_gns_resolution_result (void *cls,
1368 unsigned int rd_count,
1369 const struct GNUNET_GNSRECORD_Data *rd)
1371 struct GNS_ResolverHandle *rh = cls;
1372 struct AuthorityChain *ac;
1373 struct AuthorityChain *shorten_ac;
1376 struct VpnContext *vpn_ctx;
1377 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1379 struct GNUNET_HashCode vhash;
1381 char scratch[UINT16_MAX];
1383 size_t scratch_start;
1385 struct GNUNET_GNSRECORD_Data rd_new[rd_count];
1386 unsigned int rd_off;
1388 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1389 "Resolution succeeded for `%s' in zone %s, got %u records\n",
1391 GNUNET_GNSRECORD_z2s (&rh->ac_tail->authority_info.gns_authority),
1393 if (0 == rh->name_resolution_pos)
1395 /* top-level match, are we done yet? */
1396 if ( (rd_count > 0) &&
1397 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1398 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1401 cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
1404 if ( (NULL == cname) ||
1405 (off != rd[0].data_size) )
1407 GNUNET_break_op (0);
1408 rh->proc (rh->proc_cls, 0, NULL);
1409 GNS_resolver_lookup_cancel (rh);
1410 GNUNET_free_non_null (cname);
1413 handle_gns_cname_result (rh,
1415 GNUNET_free (cname);
1418 /* If A/AAAA was requested, but we got a VPN
1419 record, we convert it to A/AAAA using GNUnet VPN */
1420 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1421 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1423 for (i=0;i<rd_count;i++)
1425 switch (rd[i].record_type)
1427 case GNUNET_GNSRECORD_TYPE_VPN:
1429 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1430 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) >
1433 GNUNET_break_op (0);
1434 rh->proc (rh->proc_cls, 0, NULL);
1435 GNS_resolver_lookup_cancel (rh);
1438 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1439 vname = (const char *) &vpn[1];
1440 if ('\0' != vname[rd[i].data_size - 1 - 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 GNUNET_TUN_service_name_to_hash (vname,
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1450 "Attempting VPN allocation for %s-%s (AF: %d, proto %d)\n",
1451 GNUNET_i2s (&vpn->peer),
1454 (int) ntohs (vpn->proto));
1455 vpn_ctx = GNUNET_new (struct VpnContext);
1456 rh->vpn_ctx = vpn_ctx;
1458 vpn_ctx->rd_data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
1460 vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
1461 vpn_ctx->rd_count = rd_count;
1462 GNUNET_assert (vpn_ctx->rd_data_size ==
1463 GNUNET_GNSRECORD_records_serialize (rd_count,
1465 vpn_ctx->rd_data_size,
1467 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1472 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1477 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1479 /* delegation to DNS */
1480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1481 "Found GNS2DNS record, delegating to DNS!\n");
1488 } /* end: name_resolution_pos */
1489 /* convert relative names in record values to absolute names,
1490 using 'scratch' array for memory allocations */
1493 shorten_ac = rh->ac_tail;
1494 for (i=0;i<rd_count;i++)
1496 if ( (0 != rh->protocol) &&
1497 (0 != rh->service) &&
1498 (GNUNET_GNSRECORD_TYPE_BOX != rd[i].record_type) )
1499 continue; /* we _only_ care about boxed records */
1501 rd_new[rd_off] = rd[i];
1502 /* Check if the embedded name(s) end in "+", and if so,
1503 replace the "+" with the zone at "ac_tail", changing the name
1504 to a ".zkey". The name is allocated on the 'scratch' array,
1505 so we can free it afterwards. */
1506 switch (rd[i].record_type)
1508 case GNUNET_DNSPARSER_TYPE_CNAME:
1513 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1516 if ( (NULL == cname) ||
1517 (off != rd[i].data_size) )
1519 GNUNET_break_op (0); /* record not well-formed */
1523 cname = translate_dot_plus (rh, cname);
1524 GNUNET_break (NULL != cname);
1525 scratch_start = scratch_off;
1527 GNUNET_DNSPARSER_builder_add_name (scratch,
1536 rd_new[rd_off].data = &scratch[scratch_start];
1537 rd_new[rd_off].data_size = scratch_off - scratch_start;
1541 GNUNET_free_non_null (cname);
1544 case GNUNET_DNSPARSER_TYPE_SOA:
1546 struct GNUNET_DNSPARSER_SoaRecord *soa;
1549 soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
1552 if ( (NULL == soa) ||
1553 (off != rd[i].data_size) )
1555 GNUNET_break_op (0); /* record not well-formed */
1559 soa->mname = translate_dot_plus (rh, soa->mname);
1560 soa->rname = translate_dot_plus (rh, soa->rname);
1561 scratch_start = scratch_off;
1563 GNUNET_DNSPARSER_builder_add_soa (scratch,
1572 rd_new[rd_off].data = &scratch[scratch_start];
1573 rd_new[rd_off].data_size = scratch_off - scratch_start;
1578 GNUNET_DNSPARSER_free_soa (soa);
1581 case GNUNET_DNSPARSER_TYPE_MX:
1583 struct GNUNET_DNSPARSER_MxRecord *mx;
1586 mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
1589 if ( (NULL == mx) ||
1590 (off != rd[i].data_size) )
1592 GNUNET_break_op (0); /* record not well-formed */
1596 mx->mxhost = translate_dot_plus (rh, mx->mxhost);
1597 scratch_start = scratch_off;
1599 GNUNET_DNSPARSER_builder_add_mx (scratch,
1608 rd_new[rd_off].data = &scratch[scratch_start];
1609 rd_new[rd_off].data_size = scratch_off - scratch_start;
1614 GNUNET_DNSPARSER_free_mx (mx);
1617 case GNUNET_DNSPARSER_TYPE_SRV:
1619 struct GNUNET_DNSPARSER_SrvRecord *srv;
1622 srv = GNUNET_DNSPARSER_parse_srv (rd[i].data,
1625 if ( (NULL == srv) ||
1626 (off != rd[i].data_size) )
1628 GNUNET_break_op (0); /* record not well-formed */
1632 srv->target = translate_dot_plus (rh, srv->target);
1633 scratch_start = scratch_off;
1635 GNUNET_DNSPARSER_builder_add_srv (scratch,
1644 rd_new[rd_off].data = &scratch[scratch_start];
1645 rd_new[rd_off].data_size = scratch_off - scratch_start;
1650 GNUNET_DNSPARSER_free_srv (srv);
1654 case GNUNET_GNSRECORD_TYPE_NICK:
1659 if ((rd[i].data_size > 0) &&
1660 (nick[rd[i].data_size -1] != '\0'))
1662 GNUNET_break_op (0);
1665 if (NULL == shorten_ac->suggested_shortening_label)
1666 shorten_ac->suggested_shortening_label = GNUNET_strdup (nick);
1669 case GNUNET_GNSRECORD_TYPE_PKEY:
1671 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
1673 if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
1675 GNUNET_break_op (0);
1678 GNUNET_memcpy (&pub, rd[i].data, rd[i].data_size);
1680 if (GNUNET_GNSRECORD_TYPE_PKEY != rh->record_type)
1682 /* try to resolve "+" */
1683 struct AuthorityChain *ac;
1685 ac = GNUNET_new (struct AuthorityChain);
1687 ac->gns_authority = GNUNET_YES;
1688 ac->authority_info.gns_authority = pub;
1689 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
1690 ac->suggested_shortening_label = NULL;
1691 ac->shortening_started = GNUNET_NO;
1692 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1695 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1701 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1703 /* delegation to DNS */
1704 if (GNUNET_GNSRECORD_TYPE_GNS2DNS == rh->record_type)
1707 break; /* do not follow to DNS, we wanted the GNS2DNS record! */
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1710 "Found GNS2DNS record, delegating to DNS!\n");
1713 case GNUNET_GNSRECORD_TYPE_BOX:
1715 /* unbox SRV/TLSA records if a specific one was requested */
1716 if ( (0 != rh->protocol) &&
1717 (0 != rh->service) &&
1718 (rd[i].data_size >= sizeof (struct GNUNET_GNSRECORD_BoxRecord)) )
1720 const struct GNUNET_GNSRECORD_BoxRecord *box;
1723 if ( (ntohs (box->protocol) == rh->protocol) &&
1724 (ntohs (box->service) == rh->service) )
1726 /* Box matches, unbox! */
1727 rd_new[rd_off].record_type = ntohl (box->record_type);
1728 rd_new[rd_off].data_size -= sizeof (struct GNUNET_GNSRECORD_BoxRecord);
1729 rd_new[rd_off].data = &box[1];
1735 /* no specific protocol/service specified, preserve all BOX
1736 records (for modern, GNS-enabled applications) */
1744 } /* end: for rd_count */
1746 /* yes, we are done, return result */
1747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1748 "Returning GNS response for `%s' with %u answers\n",
1751 rh->proc (rh->proc_cls, rd_off, rd_new);
1752 GNS_resolver_lookup_cancel (rh);
1756 /* need to recurse, check if we can */
1757 for (i=0;i<rd_count;i++)
1759 switch (rd[i].record_type)
1761 case GNUNET_GNSRECORD_TYPE_PKEY:
1762 /* delegation to another zone */
1763 if (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
1766 GNUNET_break_op (0);
1767 rh->proc (rh->proc_cls, 0, NULL);
1768 GNS_resolver_lookup_cancel (rh);
1771 /* expand authority chain */
1772 ac = GNUNET_new (struct AuthorityChain);
1774 ac->gns_authority = GNUNET_YES;
1775 ac->suggested_shortening_label = NULL;
1776 ac->shortening_started = GNUNET_NO;
1777 GNUNET_memcpy (&ac->authority_info.gns_authority,
1779 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1780 ac->label = resolver_lookup_get_next_label (rh);
1781 /* add AC to tail */
1782 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1786 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1789 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1791 /* resolution continues within DNS */
1792 struct Gns2DnsContext *g2dc;
1797 ns = GNUNET_DNSPARSER_parse_name (rd[i].data,
1800 ip = GNUNET_DNSPARSER_parse_name (rd[i].data,
1803 if ( (NULL == ns) ||
1805 (off != rd[i].data_size) )
1807 GNUNET_break_op (0);
1808 GNUNET_free_non_null (ns);
1809 GNUNET_free_non_null (ip);
1810 rh->proc (rh->proc_cls, 0, NULL);
1811 GNS_resolver_lookup_cancel (rh);
1814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1815 "Resolving `%s' to determine IP address of DNS server for GNS2DNS transition\n",
1817 /* resolve 'ip' to determine the IP(s) of the DNS
1819 g2dc = GNUNET_new (struct Gns2DnsContext);
1822 g2dc->rh = GNUNET_new (struct GNS_ResolverHandle);
1823 g2dc->rh->authority_zone = rh->ac_tail->authority_info.gns_authority;
1824 ip = translate_dot_plus (rh, ip);
1825 g2dc->rh->name = ip;
1826 g2dc->rh->name_resolution_pos = strlen (ip);
1827 g2dc->rh->proc = &handle_gns2dns_result;
1828 g2dc->rh->proc_cls = rh;
1829 g2dc->rh->record_type = GNUNET_GNSRECORD_TYPE_ANY;
1830 g2dc->rh->options = GNUNET_GNS_LO_DEFAULT;
1831 g2dc->rh->loop_limiter = rh->loop_limiter + 1;
1833 start_resolver_lookup (g2dc->rh);
1836 case GNUNET_DNSPARSER_TYPE_CNAME:
1841 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1844 if ( (NULL == cname) ||
1845 (off != rd[i].data_size) )
1847 GNUNET_break_op (0); /* record not well-formed */
1848 rh->proc (rh->proc_cls, 0, NULL);
1849 GNS_resolver_lookup_cancel (rh);
1850 GNUNET_free_non_null (cname);
1853 handle_gns_cname_result (rh,
1855 GNUNET_free (cname);
1858 /* FIXME: handle DNAME */
1864 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1865 _("GNS lookup recursion failed (no delegation record found)\n"));
1866 rh->proc (rh->proc_cls, 0, NULL);
1867 GNS_resolver_lookup_cancel (rh);
1872 * Function called once the namestore has completed the request for
1875 * @param cls closure with the `struct CacheOps`
1876 * @param success #GNUNET_OK on success
1877 * @param emsg error message
1880 namecache_cache_continuation (void *cls,
1884 struct CacheOps *co = cls;
1886 co->namecache_qe_cache = NULL;
1888 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1889 _("Failed to cache GNS resolution: %s\n"),
1891 GNUNET_CONTAINER_DLL_remove (co_head,
1899 * Iterator called on each result obtained for a DHT
1900 * operation that expects a reply
1902 * @param cls closure with the `struct GNS_ResolverHandle`
1903 * @param exp when will this value expire
1904 * @param key key of the result
1905 * @param get_path peers on reply path (or NULL if not recorded)
1906 * [0] = datastore's first neighbor, [length - 1] = local peer
1907 * @param get_path_length number of entries in @a get_path
1908 * @param put_path peers on the PUT path (or NULL if not recorded)
1909 * [0] = origin, [length - 1] = datastore
1910 * @param put_path_length number of entries in @a put_path
1911 * @param type type of the result
1912 * @param size number of bytes in data
1913 * @param data pointer to the result data
1916 handle_dht_response (void *cls,
1917 struct GNUNET_TIME_Absolute exp,
1918 const struct GNUNET_HashCode *key,
1919 const struct GNUNET_PeerIdentity *get_path,
1920 unsigned int get_path_length,
1921 const struct GNUNET_PeerIdentity *put_path,
1922 unsigned int put_path_length,
1923 enum GNUNET_BLOCK_Type type,
1924 size_t size, const void *data)
1926 struct GNS_ResolverHandle *rh = cls;
1927 struct AuthorityChain *ac = rh->ac_tail;
1928 const struct GNUNET_GNSRECORD_Block *block;
1929 struct CacheOps *co;
1931 GNUNET_DHT_get_stop (rh->get_handle);
1932 rh->get_handle = NULL;
1933 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1934 rh->dht_heap_node = NULL;
1935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1936 "Handling response from the DHT\n");
1937 if (size < sizeof (struct GNUNET_GNSRECORD_Block))
1939 /* how did this pass DHT block validation!? */
1941 rh->proc (rh->proc_cls, 0, NULL);
1942 GNS_resolver_lookup_cancel (rh);
1947 ntohl (block->purpose.size) +
1948 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
1949 sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
1951 /* how did this pass DHT block validation!? */
1953 rh->proc (rh->proc_cls, 0, NULL);
1954 GNS_resolver_lookup_cancel (rh);
1958 GNUNET_GNSRECORD_block_decrypt (block,
1959 &ac->authority_info.gns_authority,
1961 &handle_gns_resolution_result,
1964 GNUNET_break_op (0); /* block was ill-formed */
1965 rh->proc (rh->proc_cls, 0, NULL);
1966 GNS_resolver_lookup_cancel (rh);
1969 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us)
1971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1972 "Received expired block from the DHT, will not cache it.\n");
1975 /* Cache well-formed blocks */
1976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1977 "Caching response from the DHT in namecache\n");
1978 co = GNUNET_new (struct CacheOps);
1979 co->namecache_qe_cache = GNUNET_NAMECACHE_block_cache (namecache_handle,
1981 &namecache_cache_continuation,
1983 GNUNET_CONTAINER_DLL_insert (co_head,
1990 * Initiate a DHT query for a set of GNS records.
1992 * @param rh resolution handle
1993 * @param query key to use in the DHT lookup
1996 start_dht_request (struct GNS_ResolverHandle *rh,
1997 const struct GNUNET_HashCode *query)
1999 struct GNS_ResolverHandle *rx;
2001 GNUNET_assert (NULL == rh->get_handle);
2002 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
2003 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2005 DHT_GNS_REPLICATION_LEVEL,
2006 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2008 &handle_dht_response, rh);
2009 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2011 GNUNET_TIME_absolute_get ().abs_value_us);
2012 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
2014 /* fail longest-standing DHT request */
2015 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
2016 GNUNET_assert (NULL != rx);
2017 rx->proc (rx->proc_cls, 0, NULL);
2018 GNS_resolver_lookup_cancel (rx);
2024 * Process a records that were decrypted from a block that we got from
2025 * the namecache. Simply calls #handle_gns_resolution_result().
2027 * @param cls closure with the `struct GNS_ResolverHandle`
2028 * @param rd_count number of entries in @a rd array
2029 * @param rd array of records with data to store
2032 handle_gns_namecache_resolution_result (void *cls,
2033 unsigned int rd_count,
2034 const struct GNUNET_GNSRECORD_Data *rd)
2036 struct GNS_ResolverHandle *rh = cls;
2038 handle_gns_resolution_result (rh,
2045 * Process a record that was stored in the namecache.
2047 * @param cls closure with the `struct GNS_ResolverHandle`
2048 * @param block block that was stored in the namecache
2051 handle_namecache_block_response (void *cls,
2052 const struct GNUNET_GNSRECORD_Block *block)
2054 struct GNS_ResolverHandle *rh = cls;
2055 struct AuthorityChain *ac = rh->ac_tail;
2056 const char *label = ac->label;
2057 const struct GNUNET_CRYPTO_EcdsaPublicKey *auth = &ac->authority_info.gns_authority;
2058 struct GNUNET_HashCode query;
2060 GNUNET_assert (NULL != rh->namecache_qe);
2061 rh->namecache_qe = NULL;
2062 if ( ( (GNUNET_GNS_LO_DEFAULT == rh->options) ||
2063 ( (GNUNET_GNS_LO_LOCAL_MASTER == rh->options) &&
2064 (ac != rh->ac_head) ) ) &&
2065 ( (NULL == block) ||
2066 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
2068 /* namecache knows nothing; try DHT lookup */
2069 GNUNET_GNSRECORD_query_from_public_key (auth,
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2073 "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
2075 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
2076 GNUNET_h2s (&query));
2077 start_dht_request (rh, &query);
2081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2082 "Received result from namecache for label `%s'\n",
2085 if ( (NULL == block) ||
2086 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
2088 /* DHT not permitted and no local result, fail */
2089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2090 "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
2092 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2093 rh->proc (rh->proc_cls, 0, NULL);
2094 GNS_resolver_lookup_cancel (rh);
2098 GNUNET_GNSRECORD_block_decrypt (block,
2101 &handle_gns_namecache_resolution_result,
2104 GNUNET_break_op (0); /* block was ill-formed */
2105 /* try DHT instead */
2106 GNUNET_GNSRECORD_query_from_public_key (auth,
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2110 "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
2112 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
2113 GNUNET_h2s (&query));
2114 start_dht_request (rh, &query);
2121 * Lookup tail of our authority chain in the namecache.
2123 * @param rh query we are processing
2126 recursive_gns_resolution_namecache (struct GNS_ResolverHandle *rh)
2128 struct AuthorityChain *ac = rh->ac_tail;
2129 struct GNUNET_HashCode query;
2131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2132 "Starting GNS resolution for `%s' in zone %s\n",
2134 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2135 GNUNET_GNSRECORD_query_from_public_key (&ac->authority_info.gns_authority,
2138 if (GNUNET_YES == use_cache)
2141 = GNUNET_NAMECACHE_lookup_block (namecache_handle,
2143 &handle_namecache_block_response,
2145 GNUNET_assert (NULL != rh->namecache_qe);
2149 start_dht_request (rh, &query);
2155 * Function called with the result from a revocation check.
2157 * @param cls the `struct GNS_ResovlerHandle`
2158 * @param is_valid #GNUNET_YES if the zone was not yet revoked
2161 handle_revocation_result (void *cls,
2164 struct GNS_ResolverHandle *rh = cls;
2165 struct AuthorityChain *ac = rh->ac_tail;
2167 rh->rev_check = NULL;
2168 if (GNUNET_YES != is_valid)
2170 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2171 _("Zone %s was revoked, resolution fails\n"),
2172 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2173 rh->proc (rh->proc_cls, 0, NULL);
2174 GNS_resolver_lookup_cancel (rh);
2177 recursive_gns_resolution_namecache (rh);
2182 * Perform revocation check on tail of our authority chain.
2184 * @param rh query we are processing
2187 recursive_gns_resolution_revocation (struct GNS_ResolverHandle *rh)
2189 struct AuthorityChain *ac = rh->ac_tail;
2191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2192 "Starting revocation check for zone %s\n",
2193 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2194 rh->rev_check = GNUNET_REVOCATION_query (cfg,
2195 &ac->authority_info.gns_authority,
2196 &handle_revocation_result,
2198 GNUNET_assert (NULL != rh->rev_check);
2203 * Task scheduled to continue with the resolution process.
2205 * @param cls the `struct GNS_ResolverHandle` of the resolution
2208 recursive_resolution (void *cls)
2210 struct GNS_ResolverHandle *rh = cls;
2213 if (MAX_RECURSION < rh->loop_limiter++)
2215 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2216 "Encountered unbounded recursion resolving `%s'\n",
2218 rh->proc (rh->proc_cls, 0, NULL);
2219 GNS_resolver_lookup_cancel (rh);
2222 if (GNUNET_YES == rh->ac_tail->gns_authority)
2223 recursive_gns_resolution_revocation (rh);
2225 recursive_dns_resolution (rh);
2230 * Begin the resolution process from 'name', starting with
2231 * the identification of the zone specified by 'name'.
2233 * @param rh resolution to perform
2236 start_resolver_lookup (struct GNS_ResolverHandle *rh)
2238 struct AuthorityChain *ac;
2243 if (1 == inet_pton (AF_INET,
2247 /* name is IPv4 address, pretend it's an A record */
2248 struct GNUNET_GNSRECORD_Data rd;
2251 rd.data_size = sizeof (v4);
2252 rd.expiration_time = UINT64_MAX;
2253 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
2255 rh->proc (rh->proc_cls, 1, &rd);
2256 GNS_resolver_lookup_cancel (rh);
2259 if (1 == inet_pton (AF_INET6,
2263 /* name is IPv6 address, pretend it's an AAAA record */
2264 struct GNUNET_GNSRECORD_Data rd;
2267 rd.data_size = sizeof (v6);
2268 rd.expiration_time = UINT64_MAX;
2269 rd.record_type = GNUNET_DNSPARSER_TYPE_AAAA;
2271 rh->proc (rh->proc_cls, 1, &rd);
2272 GNS_resolver_lookup_cancel (rh);
2275 if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
2276 (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
2277 ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
2278 (GNUNET_YES != is_zkey_tld (rh->name)) ) )
2280 /* use standard DNS lookup */
2283 switch (rh->record_type)
2285 case GNUNET_DNSPARSER_TYPE_A:
2288 case GNUNET_DNSPARSER_TYPE_AAAA:
2295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2296 "Doing standard DNS lookup for `%s'\n",
2298 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
2305 if (is_zkey_tld (rh->name))
2307 /* Name ends with ".zkey", try to replace authority zone with zkey
2309 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
2310 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
2313 GNUNET_CRYPTO_ecdsa_public_key_from_string (y,
2315 &rh->authority_zone)) )
2317 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2318 _("Hostname `%s' is not well-formed, resolution fails\n"),
2320 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
2322 GNUNET_free_non_null (y);
2326 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
2327 GNUNET_free (resolver_lookup_get_next_label (rh));
2329 ac = GNUNET_new (struct AuthorityChain);
2331 ac->label = resolver_lookup_get_next_label (rh);
2332 ac->suggested_shortening_label = NULL;
2333 if (NULL == ac->label)
2334 /* name was just "gnu", so we default to label '+' */
2335 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
2336 ac->gns_authority = GNUNET_YES;
2337 ac->authority_info.gns_authority = rh->authority_zone;
2338 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
2341 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
2347 * Lookup of a record in a specific zone calls lookup result processor
2350 * @param zone the zone to perform the lookup in
2351 * @param record_type the record type to look up
2352 * @param name the name to look up
2353 * @param options local options to control local lookup
2354 * @param proc the processor to call on result
2355 * @param proc_cls the closure to pass to @a proc
2356 * @return handle to cancel operation
2358 struct GNS_ResolverHandle *
2359 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
2360 uint32_t record_type,
2362 enum GNUNET_GNS_LocalOptions options,
2363 GNS_ResultProcessor proc, void *proc_cls)
2365 struct GNS_ResolverHandle *rh;
2367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2368 "Starting lookup for `%s'\n",
2370 rh = GNUNET_new (struct GNS_ResolverHandle);
2371 GNUNET_CONTAINER_DLL_insert (rlh_head,
2374 rh->authority_zone = *zone;
2376 rh->proc_cls = proc_cls;
2377 rh->options = options;
2378 rh->record_type = record_type;
2379 rh->name = GNUNET_strdup (name);
2380 rh->name_resolution_pos = strlen (name);
2381 start_resolver_lookup (rh);
2387 * Cancel active resolution (i.e. client disconnected).
2389 * @param rh resolution to abort
2392 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
2394 struct DnsResult *dr;
2395 struct AuthorityChain *ac;
2396 struct VpnContext *vpn_ctx;
2398 GNUNET_CONTAINER_DLL_remove (rlh_head,
2401 while (NULL != (ac = rh->ac_head))
2403 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
2406 GNUNET_free (ac->label);
2407 GNUNET_free_non_null (ac->suggested_shortening_label);
2410 if (NULL != rh->g2dc)
2412 /* rh->g2dc->rh is NOT in the DLL yet, so to enable us
2413 using GNS_resolver_lookup_cancel here, we need to
2415 if (NULL != rh->g2dc->rh)
2417 GNUNET_CONTAINER_DLL_insert (rlh_head,
2420 GNS_resolver_lookup_cancel (rh->g2dc->rh);
2421 rh->g2dc->rh = NULL;
2423 GNUNET_free (rh->g2dc->ns);
2424 GNUNET_free (rh->g2dc);
2427 if (NULL != rh->task_id)
2429 GNUNET_SCHEDULER_cancel (rh->task_id);
2432 if (NULL != rh->get_handle)
2434 GNUNET_DHT_get_stop (rh->get_handle);
2435 rh->get_handle = NULL;
2437 if (NULL != rh->dht_heap_node)
2439 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
2440 rh->dht_heap_node = NULL;
2442 if (NULL != (vpn_ctx = rh->vpn_ctx))
2444 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
2445 GNUNET_free (vpn_ctx->rd_data);
2446 GNUNET_free (vpn_ctx);
2448 if (NULL != rh->dns_request)
2450 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
2451 rh->dns_request = NULL;
2453 if (NULL != rh->namecache_qe)
2455 GNUNET_NAMECACHE_cancel (rh->namecache_qe);
2456 rh->namecache_qe = NULL;
2458 if (NULL != rh->rev_check)
2460 GNUNET_REVOCATION_query_cancel (rh->rev_check);
2461 rh->rev_check = NULL;
2463 if (NULL != rh->std_resolve)
2465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2466 "Canceling standard DNS resolution\n");
2467 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
2468 rh->std_resolve = NULL;
2470 while (NULL != (dr = rh->dns_result_head))
2472 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
2473 rh->dns_result_tail,
2477 GNUNET_free (rh->name);
2482 /* ***************** Resolver initialization ********************* */
2486 * Initialize the resolver
2488 * @param nc the namecache handle
2489 * @param dht the dht handle
2490 * @param c configuration handle
2491 * @param max_bg_queries maximum number of parallel background queries in dht
2494 GNS_resolver_init (struct GNUNET_NAMECACHE_Handle *nc,
2495 struct GNUNET_DHT_Handle *dht,
2496 const struct GNUNET_CONFIGURATION_Handle *c,
2497 unsigned long long max_bg_queries)
2502 namecache_handle = nc;
2505 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2506 max_allowed_background_queries = max_bg_queries;
2507 if (GNUNET_SYSERR == (use_cache =
2508 GNUNET_CONFIGURATION_get_value_yesno (c,
2511 use_cache = GNUNET_YES;
2512 if (GNUNET_NO == use_cache)
2513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2514 "Namecache disabled\n");
2517 GNUNET_CONFIGURATION_get_value_string (c,
2522 /* user did not specify DNS resolver, use 8.8.8.8 */
2523 dns_ip = GNUNET_strdup ("8.8.8.8");
2525 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
2526 GNUNET_free (dns_ip);
2527 vpn_handle = GNUNET_VPN_connect (cfg);
2535 GNS_resolver_done ()
2537 struct GNS_ResolverHandle *rh;
2538 struct CacheOps *co;
2540 /* abort active resolutions */
2541 while (NULL != (rh = rlh_head))
2543 rh->proc (rh->proc_cls, 0, NULL);
2544 GNS_resolver_lookup_cancel (rh);
2546 while (NULL != (co = co_head))
2548 GNUNET_CONTAINER_DLL_remove (co_head,
2551 GNUNET_NAMECACHE_cancel (co->namecache_qe_cache);
2554 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
2555 dht_lookup_heap = NULL;
2556 GNUNET_DNSSTUB_stop (dns_handle);
2558 GNUNET_VPN_disconnect (vpn_handle);
2561 namecache_handle = NULL;
2565 /* *************** common helper functions (do not really belong here) *********** */
2568 * Checks if @a name ends in ".TLD"
2570 * @param name the name to check
2571 * @param tld the TLD to check for
2572 * @return GNUNET_YES or GNUNET_NO
2575 is_tld (const char* name, const char* tld)
2579 if (strlen (name) <= strlen (tld))
2581 offset = strlen (name) - strlen (tld);
2582 if (0 != strcmp (name + offset, tld))
2588 /* end of gnunet-service-gns_resolver.c */