2 This file is part of GNUnet.
3 (C) 2011-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 * - GNS: handle special SRV names --- no delegation, direct lookup;
29 * can likely be done in 'resolver_lookup_get_next_label'. (#3003)
30 * - revocation checks (use REVOCATION service!), (#3004)
31 * - DNAME support (#3005)
34 #include "gnunet_util_lib.h"
35 #include "gnunet_dnsstub_lib.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_gnsrecord_lib.h"
38 #include "gnunet_namecache_service.h"
39 #include "gnunet_namestore_service.h"
40 #include "gnunet_dns_service.h"
41 #include "gnunet_resolver_service.h"
42 #include "gnunet_revocation_service.h"
43 #include "gnunet_dnsparser_lib.h"
44 #include "gnunet_gns_service.h"
46 #include "gnunet-service-gns_resolver.h"
47 #include "gnunet-service-gns_shorten.h"
48 #include "gnunet_vpn_service.h"
52 * Default DHT timeout for lookups.
54 #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
57 * Default timeout for DNS lookups.
59 #define DNS_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
62 * Default timeout for VPN redirections.
64 #define VPN_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
67 * DHT replication level
69 #define DHT_GNS_REPLICATION_LEVEL 5
72 * How deep do we allow recursions to go before we abort?
74 #define MAX_RECURSION 256
78 * DLL to hold the authority chain we had to pass in the resolution
86 struct AuthorityChain *prev;
91 struct AuthorityChain *next;
94 * Resolver handle this entry in the chain belongs to.
96 struct GNS_ResolverHandle *rh;
99 * label/name corresponding to the authority
104 * label/name suggested for shortening to the authority
106 char *suggested_shortening_label;
109 * Do we already try to shorten this authority?
111 int shortening_started;
114 * #GNUNET_YES if the authority was a GNS authority,
115 * #GNUNET_NO if the authority was a DNS authority.
120 * Information about the resolver authority for this label.
126 * The zone of the GNS authority
128 struct GNUNET_CRYPTO_EcdsaPublicKey gns_authority;
133 * Domain of the DNS resolver that is the authority.
134 * (appended to construct the DNS name to resolve;
135 * this is NOT the DNS name of the DNS server!).
137 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
140 * IP address of the DNS resolver that is authoritative.
141 * (this implementation currently only supports one
144 struct sockaddr_storage dns_ip;
154 * A result we got from DNS.
162 struct DnsResult *next;
167 struct DnsResult *prev;
170 * Binary value stored in the DNS record (appended to this struct)
175 * Expiration time for the DNS record, 0 if we didn't
176 * get anything useful (i.e. 'gethostbyname' was used).
178 uint64_t expiration_time;
181 * Number of bytes in @e data.
186 * Type of the GNS/DNS record.
188 uint32_t record_type;
194 * Closure for #vpn_allocation_cb.
200 * Which resolution process are we processing.
202 struct GNS_ResolverHandle *rh;
205 * Handle to the VPN request that we were performing.
207 struct GNUNET_VPN_RedirectionRequest *vpn_request;
210 * Number of records serialized in @e rd_data.
212 unsigned int rd_count;
215 * Serialized records.
220 * Number of bytes in @e rd_data.
227 * Information we keep during the resolution of an
228 * IP address for a DNS server while handling a
231 struct Gns2DnsContext
235 * DNS domain in which the resolution will continue
236 * (first part of the GNS2DNS record).
241 * Handle for the resolution of the IP part of the
242 * GNS2DNS record. Will return to us the addresses
243 * of the DNS resolver to use.
245 struct GNS_ResolverHandle *rh;
251 * Handle to a currenty pending resolution. On result (positive or
252 * negative) the #GNS_ResultProcessor is called.
254 struct GNS_ResolverHandle
260 struct GNS_ResolverHandle *next;
265 struct GNS_ResolverHandle *prev;
268 * The top-level GNS authoritative zone to query
270 struct GNUNET_CRYPTO_EcdsaPublicKey authority_zone;
273 * called when resolution phase finishes
275 GNS_ResultProcessor proc;
278 * closure passed to @e proc
283 * Handle used during GNS2DNS resolution for looking up the
284 * IP address of the DNS server.
286 struct Gns2DnsContext *g2dc;
289 * Handle for DHT lookups. should be NULL if no lookups are in progress
291 struct GNUNET_DHT_GetHandle *get_handle;
294 * Handle to a VPN request, NULL if none is active.
296 struct VpnContext *vpn_ctx;
299 * Socket for a DNS request, NULL if none is active.
301 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
304 * Handle for standard DNS resolution, NULL if none is active.
306 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
309 * Pending Namecache lookup task
311 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe;
314 * Pending revocation check.
316 struct GNUNET_REVOCATION_Query *rev_check;
319 * Heap node associated with this lookup. Used to limit number of
320 * concurrent requests.
322 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
325 * DLL to store the authority chain
327 struct AuthorityChain *ac_head;
330 * DLL to store the authority chain
332 struct AuthorityChain *ac_tail;
335 * Private key of the shorten zone, NULL to not shorten.
337 struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key;
340 * ID of a task associated with the resolution process.
342 GNUNET_SCHEDULER_TaskIdentifier task_id;
345 * The name to resolve
350 * DLL of results we got from DNS.
352 struct DnsResult *dns_result_head;
355 * DLL of results we got from DNS.
357 struct DnsResult *dns_result_tail;
360 * Current offset in 'name' where we are resolving.
362 size_t name_resolution_pos;
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 * Check if name is in srv format (_x._y.xxx)
473 * @return #GNUNET_YES if true
476 is_srv (const char *name)
483 if (NULL == strstr (name, "._"))
486 ndup = GNUNET_strdup (name);
488 if (NULL == strtok (NULL, "."))
490 if (NULL == strtok (NULL, "."))
492 if (NULL != strtok (NULL, "."))
501 * Determine if this name is canonical (is a legal name in a zone, without delegation);
502 * note that we do not test that the name does not contain illegal characters, we only
503 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
504 * even though they consist of multiple labels.
507 * a.b.gnu = not canonical
509 * _foo._srv = canonical
510 * _f.bar = not canonical
512 * @param name the name to test
513 * @return #GNUNET_YES if canonical
516 is_canonical (const char *name)
521 if (NULL == strchr (name, '.'))
526 while (NULL != (dot = strchr (pos, '.')))
534 /* ************************** Resolution **************************** */
537 * Expands a name ending in .+ with the zone of origin.
539 * @param rh resolution context
540 * @param name name to modify (to be free'd or returned)
541 * @return updated name
544 translate_dot_plus (struct GNS_ResolverHandle *rh,
548 size_t s_len = strlen (name);
550 if (0 != strcmp (&name[s_len - 2],
552 return name; /* did not end in ".+" */
553 GNUNET_assert (GNUNET_YES == rh->ac_tail->gns_authority);
554 GNUNET_asprintf (&ret,
558 GNUNET_GNSRECORD_pkey_to_zkey (&rh->ac_tail->authority_info.gns_authority));
565 * Task scheduled to asynchronously fail a resolution.
567 * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
568 * @param tc task context
571 fail_resolution (void *cls,
572 const struct GNUNET_SCHEDULER_TaskContext *tc)
574 struct GNS_ResolverHandle *rh = cls;
576 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
577 rh->proc (rh->proc_cls, 0, NULL);
578 GNS_resolver_lookup_cancel (rh);
582 #if (defined WINDOWS) || (defined DARWIN)
583 /* Don't have this on W32, here's a naive implementation
584 * Was somehow removed on OS X ... */
586 memrchr (const void *s,
590 const unsigned char *ucs = s;
593 for (i = n - 1; i >= 0; i--)
594 if (c == (int) ucs[i])
595 return (void *) &ucs[i];
602 * Get the next, rightmost label from the name that we are trying to resolve,
603 * and update the resolution position accordingly.
605 * @param rh handle to the resolution operation to get the next label from
606 * @return NULL if there are no more labels
609 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
615 if (0 == rh->name_resolution_pos)
617 dot = memrchr (rh->name, (int) '.', rh->name_resolution_pos);
620 /* done, this was the last one */
621 len = rh->name_resolution_pos;
623 rh->name_resolution_pos = 0;
627 /* advance by one label */
628 len = rh->name_resolution_pos - (dot - rh->name) - 1;
630 rh->name_resolution_pos = dot - rh->name;
632 return GNUNET_strndup (rp, len);
637 * Gives the cummulative result obtained to the callback and clean up the request.
639 * @param rh resolution process that has culminated in a result
642 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
644 struct DnsResult *pos;
649 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
652 struct GNUNET_GNSRECORD_Data rd[n];
655 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
657 rd[i].data = pos->data;
658 rd[i].data_size = pos->data_size;
659 rd[i].record_type = pos->record_type;
660 if (0 == pos->expiration_time)
662 rd[i].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
663 rd[i].expiration_time = 0;
667 rd[i].flags = GNUNET_GNSRECORD_RF_NONE;
668 rd[i].expiration_time = pos->expiration_time;
672 GNUNET_assert (i == n);
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674 "Transmitting standard DNS result with %u records\n",
676 rh->proc (rh->proc_cls,
680 GNS_resolver_lookup_cancel (rh);
685 * Add a result from DNS to the records to be returned to the application.
687 * @param rh resolution request to extend with a result
688 * @param expiration_time expiration time for the answer
689 * @param record_type DNS record type of the answer
690 * @param data_size number of bytes in @a data
691 * @param data binary data to return in DNS record
694 add_dns_result (struct GNS_ResolverHandle *rh,
695 uint64_t expiration_time,
696 uint32_t record_type,
700 struct DnsResult *res;
702 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
703 res->expiration_time = expiration_time;
704 res->data_size = data_size;
705 res->record_type = record_type;
707 memcpy (&res[1], data, data_size);
708 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
715 * We had to do a DNS lookup. Convert the result (if any) and return
718 * @param cls closure with the `struct GNS_ResolverHandle`
719 * @param addr one of the addresses of the host, NULL for the last address
720 * @param addrlen length of the address
723 handle_dns_result (void *cls,
724 const struct sockaddr *addr,
727 struct GNS_ResolverHandle *rh = cls;
728 const struct sockaddr_in *sa4;
729 const struct sockaddr_in6 *sa6;
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "Received %u bytes of DNS IP data\n",
736 rh->std_resolve = NULL;
737 transmit_lookup_dns_result (rh);
740 switch (addr->sa_family)
743 sa4 = (const struct sockaddr_in *) addr;
745 0 /* expiration time is unknown */,
746 GNUNET_DNSPARSER_TYPE_A,
747 sizeof (struct in_addr),
751 sa6 = (const struct sockaddr_in6 *) addr;
753 0 /* expiration time is unknown */,
754 GNUNET_DNSPARSER_TYPE_AAAA,
755 sizeof (struct in6_addr),
766 * Task scheduled to continue with the resolution process.
768 * @param cls the 'struct GNS_ResolverHandle' of the resolution
769 * @param tc task context
772 recursive_resolution (void *cls,
773 const struct GNUNET_SCHEDULER_TaskContext *tc);
777 * Begin the resolution process from 'name', starting with
778 * the identification of the zone specified by 'name'.
780 * @param rh resolution to perform
783 start_resolver_lookup (struct GNS_ResolverHandle *rh);
787 * Function called with the result of a DNS resolution.
789 * @param cls the request handle of the resolution that
790 * we were attempting to make
791 * @param rs socket that received the response
792 * @param dns dns response, never NULL
793 * @param dns_len number of bytes in @a dns
796 dns_result_parser (void *cls,
797 struct GNUNET_DNSSTUB_RequestSocket *rs,
798 const struct GNUNET_TUN_DnsHeader *dns,
801 struct GNS_ResolverHandle *rh = cls;
802 struct GNUNET_DNSPARSER_Packet *p;
803 const struct GNUNET_DNSPARSER_Record *rec;
804 unsigned int rd_count;
807 rh->dns_request = NULL;
808 GNUNET_SCHEDULER_cancel (rh->task_id);
809 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
810 p = GNUNET_DNSPARSER_parse ((const char *) dns,
814 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
815 _("Failed to parse DNS response\n"));
816 rh->proc (rh->proc_cls, 0, NULL);
817 GNS_resolver_lookup_cancel (rh);
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Received DNS response for `%s' with %u answers\n",
823 (unsigned int) p->num_answers);
824 if ( (p->num_answers > 0) &&
825 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
826 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
828 GNUNET_free (rh->name);
829 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
830 start_resolver_lookup (rh);
831 GNUNET_DNSPARSER_free_packet (p);
834 /* FIXME: add DNAME support */
836 /* convert from (parsed) DNS to (binary) GNS format! */
837 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
839 struct GNUNET_GNSRECORD_Data rd[rd_count];
841 char buf[UINT16_MAX];
847 memset (rd, 0, sizeof (rd));
848 for (i=0;i<rd_count;i++)
850 if (i < p->num_answers)
851 rec = &p->answers[i];
852 else if (i < p->num_answers + p->num_authority_records)
853 rec = &p->authority_records[i - p->num_answers];
855 rec = &p->authority_records[i - p->num_answers - p->num_authority_records];
856 /* As we copied the full DNS name to 'rh->ac_tail->label', this
857 should be the correct check to see if this record is actually
858 a record for our label... */
859 if (0 != strcmp (rec->name,
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 "Dropping record `%s', does not match desired name `%s'\n",
869 rd[i - skip].record_type = rec->type;
870 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
873 case GNUNET_DNSPARSER_TYPE_A:
874 if (rec->data.raw.data_len != sizeof (struct in_addr))
880 rd[i - skip].data_size = rec->data.raw.data_len;
881 rd[i - skip].data = rec->data.raw.data;
883 case GNUNET_DNSPARSER_TYPE_AAAA:
884 if (rec->data.raw.data_len != sizeof (struct in6_addr))
890 rd[i - skip].data_size = rec->data.raw.data_len;
891 rd[i - skip].data = rec->data.raw.data;
893 case GNUNET_DNSPARSER_TYPE_CNAME:
894 case GNUNET_DNSPARSER_TYPE_PTR:
895 case GNUNET_DNSPARSER_TYPE_NS:
898 GNUNET_DNSPARSER_builder_add_name (buf,
907 rd[i - skip].data_size = buf_off - buf_start;
908 rd[i - skip].data = &buf[buf_start];
910 case GNUNET_DNSPARSER_TYPE_SOA:
913 GNUNET_DNSPARSER_builder_add_soa (buf,
922 rd[i - skip].data_size = buf_off - buf_start;
923 rd[i - skip].data = &buf[buf_start];
925 case GNUNET_DNSPARSER_TYPE_MX:
928 GNUNET_DNSPARSER_builder_add_mx (buf,
937 rd[i - skip].data_size = buf_off - buf_start;
938 rd[i - skip].data = &buf[buf_start];
940 case GNUNET_DNSPARSER_TYPE_SRV:
943 GNUNET_DNSPARSER_builder_add_srv (buf,
952 rd[i - skip].data_size = buf_off - buf_start;
953 rd[i - skip].data = &buf[buf_start];
956 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
957 _("Skipping record of unsupported type %d\n"),
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "Returning DNS response for `%s' with %u answers\n",
966 (unsigned int) p->num_answers);
967 rh->proc (rh->proc_cls, rd_count - skip, rd);
968 GNS_resolver_lookup_cancel (rh);
970 GNUNET_DNSPARSER_free_packet (p);
975 * Perform recursive DNS resolution. Asks the given DNS resolver to
976 * resolve "rh->dns_name", possibly recursively proceeding following
977 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
978 * we find the answer.
980 * @param rh resolution information
983 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
985 struct AuthorityChain *ac;
987 struct GNUNET_DNSPARSER_Query *query;
988 struct GNUNET_DNSPARSER_Packet *p;
990 size_t dns_request_length;
993 GNUNET_assert (NULL != ac);
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "Starting DNS lookup for `%s'\n",
997 GNUNET_assert (GNUNET_NO == ac->gns_authority);
998 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
1001 sa_len = sizeof (struct sockaddr_in);
1004 sa_len = sizeof (struct sockaddr_in6);
1008 rh->proc (rh->proc_cls, 0, NULL);
1009 GNS_resolver_lookup_cancel (rh);
1012 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
1013 query->name = GNUNET_strdup (ac->label);
1014 query->type = rh->record_type;
1015 query->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1016 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
1019 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1021 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
1022 p->flags.recursion_desired = 1;
1024 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
1027 rh->proc (rh->proc_cls, 0, NULL);
1028 GNS_resolver_lookup_cancel (rh);
1032 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
1033 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
1039 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
1043 GNUNET_free (dns_request);
1044 GNUNET_DNSPARSER_free_packet (p);
1049 * We encountered a CNAME record during our resolution.
1050 * Merge it into our chain.
1052 * @param rh resolution we are performing
1053 * @param cname value of the cname record we got for the current
1054 * authority chain tail
1057 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
1062 struct AuthorityChain *ac;
1064 nlen = strlen (cname);
1067 &cname[nlen - 2])) )
1069 /* CNAME resolution continues relative to current domain */
1070 if (0 == rh->name_resolution_pos)
1072 res = GNUNET_strndup (cname, nlen - 2);
1073 rh->name_resolution_pos = nlen - 2;
1077 GNUNET_asprintf (&res,
1079 (int) rh->name_resolution_pos,
1083 rh->name_resolution_pos = strlen (res);
1085 GNUNET_free (rh->name);
1087 ac = GNUNET_new (struct AuthorityChain);
1089 ac->gns_authority = GNUNET_YES;
1090 ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
1091 ac->label = resolver_lookup_get_next_label (rh);
1092 ac->suggested_shortening_label = NULL;
1093 ac->shortening_started = GNUNET_NO;
1094 /* add AC to tail */
1095 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1098 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1102 /* name is absolute, start from the beginning */
1103 GNUNET_free (rh->name);
1104 rh->name = GNUNET_strdup (cname);
1105 start_resolver_lookup (rh);
1110 * Process a records that were decrypted from a block.
1112 * @param cls closure with the 'struct GNS_ResolverHandle'
1113 * @param rd_count number of entries in @a rd array
1114 * @param rd array of records with data to store
1117 handle_gns_resolution_result (void *cls,
1118 unsigned int rd_count,
1119 const struct GNUNET_GNSRECORD_Data *rd);
1123 * Callback invoked from the VPN service once a redirection is
1124 * available. Provides the IP address that can now be used to
1125 * reach the requested destination. Replaces the "VPN" record
1126 * with the respective A/AAAA record and continues processing.
1128 * @param cls closure
1129 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1130 * will match 'result_af' from the request
1131 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1132 * that the VPN allocated for the redirection;
1133 * traffic to this IP will now be redirected to the
1134 * specified target peer; NULL on error
1137 vpn_allocation_cb (void *cls,
1139 const void *address)
1141 struct VpnContext *vpn_ctx = cls;
1142 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1143 struct GNUNET_GNSRECORD_Data rd[vpn_ctx->rd_count];
1146 vpn_ctx->vpn_request = NULL;
1148 GNUNET_assert (GNUNET_OK ==
1149 GNUNET_GNSRECORD_records_deserialize (vpn_ctx->rd_data_size,
1153 for (i=0;i<vpn_ctx->rd_count;i++)
1155 if (GNUNET_GNSRECORD_TYPE_VPN == rd[i].record_type)
1160 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1161 rd[i].data_size = sizeof (struct in_addr);
1162 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1164 rd[i].data = address;
1167 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1168 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1170 rd[i].data = address;
1171 rd[i].data_size = sizeof (struct in6_addr);
1179 GNUNET_assert (i < vpn_ctx->rd_count);
1180 handle_gns_resolution_result (rh,
1183 GNUNET_free (vpn_ctx->rd_data);
1184 GNUNET_free (vpn_ctx);
1189 * We've resolved the IP address for the DNS resolver to use
1190 * after encountering a GNS2DNS record.
1192 * TODO: Right now we only foward the request to ONE DNS resolver,
1193 * even if we get multiple IP addresses back; a correct implementation
1194 * should try all DNS resolvers.
1196 * @param cls the `struct GNS_ResolverHandle` where we encountered
1197 * the GNS2DNS record
1198 * @param rd_count number of records in @a rd
1199 * @param rd addresses for the DNS resolver (presumably)
1202 handle_gns2dns_result (void *cls,
1203 unsigned int rd_count,
1204 const struct GNUNET_GNSRECORD_Data *rd)
1206 struct GNS_ResolverHandle *rh = cls;
1207 struct AuthorityChain *ac;
1209 struct sockaddr *sa;
1210 struct sockaddr_in v4;
1211 struct sockaddr_in6 v6;
1214 /* find suitable A/AAAA record */
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1216 "Received %u results for IP address of DNS server for GNS2DNS transition\n",
1218 /* enable cleanup of 'rh' handle that comes next... */
1219 GNUNET_CONTAINER_DLL_insert (rlh_head,
1222 rh->g2dc->rh = NULL;
1225 for (j=0;j<rd_count;j++)
1227 switch (rd[j].record_type)
1229 case GNUNET_DNSPARSER_TYPE_A:
1230 if (sizeof (struct in_addr) != rd[j].data_size)
1232 GNUNET_break_op (0);
1233 rh->proc (rh->proc_cls, 0, NULL);
1234 GNS_resolver_lookup_cancel (rh);
1237 /* FIXME: might want to check if we support IPv4 here,
1238 and otherwise skip this one and hope we find another */
1239 memset (&v4, 0, sizeof (v4));
1240 sa_len = sizeof (v4);
1241 v4.sin_family = AF_INET;
1242 v4.sin_port = htons (53);
1243 #if HAVE_SOCKADDR_IN_SIN_LEN
1244 v4.sin_len = (u_char) sa_len;
1246 memcpy (&v4.sin_addr,
1248 sizeof (struct in_addr));
1249 sa = (struct sockaddr *) &v4;
1251 case GNUNET_DNSPARSER_TYPE_AAAA:
1252 if (sizeof (struct in6_addr) != rd[j].data_size)
1254 GNUNET_break_op (0);
1255 rh->proc (rh->proc_cls, 0, NULL);
1256 GNS_resolver_lookup_cancel (rh);
1259 /* FIXME: might want to check if we support IPv6 here,
1260 and otherwise skip this one and hope we find another */
1261 memset (&v6, 0, sizeof (v6));
1262 sa_len = sizeof (v6);
1263 v6.sin6_family = AF_INET6;
1264 v6.sin6_port = htons (53);
1265 #if HAVE_SOCKADDR_IN_SIN_LEN
1266 v6.sin6_len = (u_char) sa_len;
1268 memcpy (&v6.sin6_addr,
1270 sizeof (struct in6_addr));
1271 sa = (struct sockaddr *) &v6;
1281 /* we cannot continue; NS without A/AAAA */
1282 rh->proc (rh->proc_cls, 0, NULL);
1283 GNS_resolver_lookup_cancel (rh);
1286 /* expand authority chain */
1287 ac = GNUNET_new (struct AuthorityChain);
1289 strcpy (ac->authority_info.dns_authority.name,
1291 memcpy (&ac->authority_info.dns_authority.dns_ip,
1294 /* for DNS recursion, the label is the full DNS name,
1295 created from the remainder of the GNS name and the
1296 name in the NS record */
1297 GNUNET_asprintf (&ac->label,
1299 (int) rh->name_resolution_pos,
1301 (0 != rh->name_resolution_pos) ? "." : "",
1303 GNUNET_free (rh->g2dc->ns);
1304 GNUNET_free (rh->g2dc);
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1307 "Will continue resolution using DNS server `%s' to resolve `%s'\n",
1308 GNUNET_a2s (sa, sa_len),
1310 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1313 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1316 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1318 rh->proc (rh->proc_cls, 0, NULL);
1319 GNS_resolver_lookup_cancel (rh);
1323 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1329 * Process a records that were decrypted from a block.
1331 * @param cls closure with the `struct GNS_ResolverHandle`
1332 * @param rd_count number of entries in @a rd array
1333 * @param rd array of records with data to store
1336 handle_gns_resolution_result (void *cls,
1337 unsigned int rd_count,
1338 const struct GNUNET_GNSRECORD_Data *rd)
1340 struct GNS_ResolverHandle *rh = cls;
1341 struct AuthorityChain *ac;
1342 struct AuthorityChain *shorten_ac;
1345 struct VpnContext *vpn_ctx;
1346 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1348 struct GNUNET_HashCode vhash;
1350 char scratch[UINT16_MAX];
1352 size_t scratch_start;
1354 struct GNUNET_GNSRECORD_Data rd_new[rd_count];
1355 unsigned int rd_off;
1357 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1358 "Resolution succeeded for `%s' in zone %s, got %u records\n",
1360 GNUNET_GNSRECORD_z2s (&rh->ac_tail->authority_info.gns_authority),
1362 if (0 == rh->name_resolution_pos)
1364 /* top-level match, are we done yet? */
1365 if ( (rd_count > 0) &&
1366 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1367 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1370 cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
1373 if ( (NULL == cname) ||
1374 (off != rd[0].data_size) )
1376 GNUNET_break_op (0);
1377 rh->proc (rh->proc_cls, 0, NULL);
1378 GNS_resolver_lookup_cancel (rh);
1379 GNUNET_free_non_null (cname);
1382 handle_gns_cname_result (rh,
1384 GNUNET_free (cname);
1387 /* If A/AAAA was requested, but we got a VPN
1388 record, we convert it to A/AAAA using GNUnet VPN */
1389 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1390 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1392 for (i=0;i<rd_count;i++)
1394 switch (rd[i].record_type)
1396 case GNUNET_GNSRECORD_TYPE_VPN:
1398 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1399 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) <
1402 GNUNET_break_op (0);
1403 rh->proc (rh->proc_cls, 0, NULL);
1404 GNS_resolver_lookup_cancel (rh);
1407 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1408 vname = (const char *) &vpn[1];
1409 if ('\0' != vname[rd[i].data_size - 1 - sizeof (struct GNUNET_TUN_GnsVpnRecord)])
1411 GNUNET_break_op (0);
1412 rh->proc (rh->proc_cls, 0, NULL);
1413 GNS_resolver_lookup_cancel (rh);
1416 GNUNET_CRYPTO_hash (vname,
1417 strlen (vname), // FIXME: +1?
1419 vpn_ctx = GNUNET_new (struct VpnContext);
1420 rh->vpn_ctx = vpn_ctx;
1422 vpn_ctx->rd_data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
1424 vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
1425 (void) GNUNET_GNSRECORD_records_serialize (rd_count,
1427 vpn_ctx->rd_data_size,
1429 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1434 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1439 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1441 /* delegation to DNS */
1442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1443 "Found GNS2DNS record, delegating to DNS!\n");
1450 } /* end: name_resolution_pos */
1451 /* convert relative names in record values to absolute names,
1452 using 'scratch' array for memory allocations */
1455 shorten_ac = rh->ac_tail;
1456 for (i=0;i<rd_count;i++)
1458 rd_new[rd_off] = rd[i];
1459 /* Check if the embedded name(s) end in "+", and if so,
1460 replace the "+" with the zone at "ac_tail", changing the name
1461 to a ".zkey". The name is allocated on the 'scratch' array,
1462 so we can free it afterwards. */
1463 switch (rd[i].record_type)
1465 case GNUNET_DNSPARSER_TYPE_CNAME:
1470 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1473 if ( (NULL == cname) ||
1474 (off != rd[i].data_size) )
1476 GNUNET_break_op (0); /* record not well-formed */
1480 cname = translate_dot_plus (rh, cname);
1481 GNUNET_break (NULL != cname);
1482 scratch_start = scratch_off;
1484 GNUNET_DNSPARSER_builder_add_name (scratch,
1493 rd_new[rd_off].data = &scratch[scratch_start];
1494 rd_new[rd_off].data_size = scratch_off - scratch_start;
1498 GNUNET_free_non_null (cname);
1501 case GNUNET_DNSPARSER_TYPE_SOA:
1503 struct GNUNET_DNSPARSER_SoaRecord *soa;
1506 soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
1509 if ( (NULL == soa) ||
1510 (off != rd[i].data_size) )
1512 GNUNET_break_op (0); /* record not well-formed */
1516 soa->mname = translate_dot_plus (rh, soa->mname);
1517 soa->rname = translate_dot_plus (rh, soa->rname);
1518 scratch_start = scratch_off;
1520 GNUNET_DNSPARSER_builder_add_soa (scratch,
1529 rd_new[rd_off].data = &scratch[scratch_start];
1530 rd_new[rd_off].data_size = scratch_off - scratch_start;
1535 GNUNET_DNSPARSER_free_soa (soa);
1538 case GNUNET_DNSPARSER_TYPE_MX:
1540 struct GNUNET_DNSPARSER_MxRecord *mx;
1543 mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
1546 if ( (NULL == mx) ||
1547 (off != rd[i].data_size) )
1549 GNUNET_break_op (0); /* record not well-formed */
1553 mx->mxhost = translate_dot_plus (rh, mx->mxhost);
1554 scratch_start = scratch_off;
1556 GNUNET_DNSPARSER_builder_add_mx (scratch,
1565 rd_new[rd_off].data = &scratch[scratch_start];
1566 rd_new[rd_off].data_size = scratch_off - scratch_start;
1571 GNUNET_DNSPARSER_free_mx (mx);
1574 case GNUNET_DNSPARSER_TYPE_SRV:
1576 struct GNUNET_DNSPARSER_SrvRecord *srv;
1579 /* FIXME: passing rh->name here is is not necessarily what we want
1580 (SRV support not finished) */
1581 srv = GNUNET_DNSPARSER_parse_srv (rh->name,
1585 if ( (NULL == srv) ||
1586 (off != rd[i].data_size) )
1588 GNUNET_break_op (0); /* record not well-formed */
1592 srv->domain_name = translate_dot_plus (rh, srv->domain_name);
1593 srv->target = translate_dot_plus (rh, srv->target);
1594 scratch_start = scratch_off;
1596 GNUNET_DNSPARSER_builder_add_srv (scratch,
1605 rd_new[rd_off].data = &scratch[scratch_start];
1606 rd_new[rd_off].data_size = scratch_off - scratch_start;
1611 GNUNET_DNSPARSER_free_srv (srv);
1615 case GNUNET_GNSRECORD_TYPE_NICK:
1619 if ((rd[i].data_size > 0) &&
1620 (nick[rd[i].data_size -1] != '\0'))
1622 GNUNET_break_op (0);
1625 if (NULL == shorten_ac->suggested_shortening_label)
1626 shorten_ac->suggested_shortening_label = GNUNET_strdup (nick);
1629 case GNUNET_GNSRECORD_TYPE_PKEY:
1631 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
1633 if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
1635 GNUNET_break_op (0);
1638 memcpy (&pub, rd[i].data, rd[i].data_size);
1640 if (GNUNET_GNSRECORD_TYPE_PKEY != rh->record_type)
1642 /* try to resolve "+" */
1643 struct AuthorityChain *ac;
1645 ac = GNUNET_new (struct AuthorityChain);
1647 ac->gns_authority = GNUNET_YES;
1648 ac->authority_info.gns_authority = pub;
1649 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
1650 ac->suggested_shortening_label = NULL;
1651 ac->shortening_started = GNUNET_NO;
1652 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1655 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1661 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1663 /* delegation to DNS */
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1665 "Found GNS2DNS record, delegating to DNS!\n");
1672 } /* end: for rd_count */
1674 /* trigger shortening */
1675 if ((NULL != rh->shorten_key) &&
1676 (NULL != shorten_ac) &&
1677 (GNUNET_NO == shorten_ac->shortening_started) &&
1678 (NULL != shorten_ac->suggested_shortening_label))
1680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1681 "Start shortening for label `%s' based on nick `%s'\n",
1683 shorten_ac->suggested_shortening_label);
1684 shorten_ac->shortening_started = GNUNET_YES;
1685 GNS_shorten_start (shorten_ac->label,
1686 shorten_ac->suggested_shortening_label,
1687 &shorten_ac->authority_info.gns_authority,
1691 /* yes, we are done, return result */
1692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1693 "Returning GNS response for `%s' with %u answers\n",
1696 rh->proc (rh->proc_cls, rd_off, rd_new);
1697 GNS_resolver_lookup_cancel (rh);
1701 /* need to recurse, check if we can */
1702 for (i=0;i<rd_count;i++)
1704 switch (rd[i].record_type)
1706 case GNUNET_GNSRECORD_TYPE_PKEY:
1707 /* delegation to another zone */
1708 if (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
1711 GNUNET_break_op (0);
1712 rh->proc (rh->proc_cls, 0, NULL);
1713 GNS_resolver_lookup_cancel (rh);
1716 /* expand authority chain */
1717 ac = GNUNET_new (struct AuthorityChain);
1719 ac->gns_authority = GNUNET_YES;
1720 ac->suggested_shortening_label = NULL;
1721 ac->shortening_started = GNUNET_NO;
1722 memcpy (&ac->authority_info.gns_authority,
1724 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1725 ac->label = resolver_lookup_get_next_label (rh);
1726 /* add AC to tail */
1727 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1731 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1734 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1736 /* resolution continues within DNS */
1737 struct Gns2DnsContext *g2dc;
1742 ns = GNUNET_DNSPARSER_parse_name (rd[i].data,
1745 ip = GNUNET_DNSPARSER_parse_name (rd[i].data,
1748 if ( (NULL == ns) ||
1750 (off != rd[i].data_size) )
1752 GNUNET_break_op (0);
1753 GNUNET_free_non_null (ns);
1754 GNUNET_free_non_null (ip);
1755 rh->proc (rh->proc_cls, 0, NULL);
1756 GNS_resolver_lookup_cancel (rh);
1759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1760 "Resolving `%s' to determine IP address of DNS server for GNS2DNS transition\n",
1762 /* resolve 'ip' to determine the IP(s) of the DNS
1764 g2dc = GNUNET_new (struct Gns2DnsContext);
1766 g2dc->rh = GNUNET_new (struct GNS_ResolverHandle);
1767 g2dc->rh->authority_zone = rh->ac_tail->authority_info.gns_authority;
1768 ip = translate_dot_plus (rh, ip);
1769 g2dc->rh->name = ip;
1770 g2dc->rh->name_resolution_pos = strlen (ip);
1771 g2dc->rh->proc = &handle_gns2dns_result;
1772 g2dc->rh->proc_cls = rh;
1773 g2dc->rh->record_type = GNUNET_GNSRECORD_TYPE_ANY;
1774 g2dc->rh->only_cached = GNUNET_NO;
1775 g2dc->rh->loop_limiter = rh->loop_limiter + 1;
1777 start_resolver_lookup (g2dc->rh);
1780 case GNUNET_DNSPARSER_TYPE_CNAME:
1785 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1788 if ( (NULL == cname) ||
1789 (off != rd[i].data_size) )
1791 GNUNET_break_op (0); /* record not well-formed */
1792 rh->proc (rh->proc_cls, 0, NULL);
1793 GNS_resolver_lookup_cancel (rh);
1794 GNUNET_free_non_null (cname);
1797 handle_gns_cname_result (rh,
1799 GNUNET_free (cname);
1802 /* FIXME: handle DNAME */
1808 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1809 _("GNS lookup recursion failed (no delegation record found)\n"));
1810 rh->proc (rh->proc_cls, 0, NULL);
1811 GNS_resolver_lookup_cancel (rh);
1816 * Function called once the namestore has completed the request for
1819 * @param cls closure with the `struct CacheOps`
1820 * @param success #GNUNET_OK on success
1821 * @param emsg error message
1824 namecache_cache_continuation (void *cls,
1828 struct CacheOps *co = cls;
1830 co->namecache_qe_cache = NULL;
1832 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1833 _("Failed to cache GNS resolution: %s\n"),
1835 GNUNET_CONTAINER_DLL_remove (co_head,
1843 * Iterator called on each result obtained for a DHT
1844 * operation that expects a reply
1846 * @param cls closure with the `struct GNS_ResolverHandle`
1847 * @param exp when will this value expire
1848 * @param key key of the result
1849 * @param get_path peers on reply path (or NULL if not recorded)
1850 * [0] = datastore's first neighbor, [length - 1] = local peer
1851 * @param get_path_length number of entries in @a get_path
1852 * @param put_path peers on the PUT path (or NULL if not recorded)
1853 * [0] = origin, [length - 1] = datastore
1854 * @param put_path_length number of entries in @a put_path
1855 * @param type type of the result
1856 * @param size number of bytes in data
1857 * @param data pointer to the result data
1860 handle_dht_response (void *cls,
1861 struct GNUNET_TIME_Absolute exp,
1862 const struct GNUNET_HashCode *key,
1863 const struct GNUNET_PeerIdentity *get_path,
1864 unsigned int get_path_length,
1865 const struct GNUNET_PeerIdentity *put_path,
1866 unsigned int put_path_length,
1867 enum GNUNET_BLOCK_Type type,
1868 size_t size, const void *data)
1870 struct GNS_ResolverHandle *rh = cls;
1871 struct AuthorityChain *ac = rh->ac_tail;
1872 const struct GNUNET_GNSRECORD_Block *block;
1873 struct CacheOps *co;
1875 GNUNET_DHT_get_stop (rh->get_handle);
1876 rh->get_handle = NULL;
1877 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1878 rh->dht_heap_node = NULL;
1879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1880 "Handling response from the DHT\n");
1881 if (size < sizeof (struct GNUNET_GNSRECORD_Block))
1883 /* how did this pass DHT block validation!? */
1885 rh->proc (rh->proc_cls, 0, NULL);
1886 GNS_resolver_lookup_cancel (rh);
1891 ntohl (block->purpose.size) +
1892 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
1893 sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
1895 /* how did this pass DHT block validation!? */
1897 rh->proc (rh->proc_cls, 0, NULL);
1898 GNS_resolver_lookup_cancel (rh);
1902 GNUNET_GNSRECORD_block_decrypt (block,
1903 &ac->authority_info.gns_authority,
1905 &handle_gns_resolution_result,
1908 GNUNET_break_op (0); /* block was ill-formed */
1909 rh->proc (rh->proc_cls, 0, NULL);
1910 GNS_resolver_lookup_cancel (rh);
1913 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us)
1915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1916 "Received expired block from the DHT, will not cache it.\n");
1919 /* Cache well-formed blocks */
1920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1921 "Caching response from the DHT in namecache\n");
1922 co = GNUNET_new (struct CacheOps);
1923 co->namecache_qe_cache = GNUNET_NAMECACHE_block_cache (namecache_handle,
1925 &namecache_cache_continuation,
1927 GNUNET_CONTAINER_DLL_insert (co_head,
1933 start_dht_request (struct GNS_ResolverHandle *rh, struct GNUNET_HashCode query)
1935 struct GNS_ResolverHandle *rx;
1936 GNUNET_assert (NULL == rh->get_handle);
1937 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1938 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1940 DHT_GNS_REPLICATION_LEVEL,
1941 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1943 &handle_dht_response, rh);
1944 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1946 GNUNET_TIME_absolute_get ().abs_value_us);
1947 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
1949 /* fail longest-standing DHT request */
1950 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
1951 GNUNET_assert (NULL != rx);
1952 rx->proc (rx->proc_cls, 0, NULL);
1953 GNS_resolver_lookup_cancel (rx);
1959 * Process a record that was stored in the namecache.
1961 * @param cls closure with the `struct GNS_ResolverHandle`
1962 * @param block block that was stored in the namecache
1965 handle_namecache_block_response (void *cls,
1966 const struct GNUNET_GNSRECORD_Block *block)
1968 struct GNS_ResolverHandle *rh = cls;
1969 struct AuthorityChain *ac = rh->ac_tail;
1970 const char *label = ac->label;
1971 const struct GNUNET_CRYPTO_EcdsaPublicKey *auth = &ac->authority_info.gns_authority;
1972 struct GNUNET_HashCode query;
1974 GNUNET_assert (NULL != rh->namecache_qe);
1975 rh->namecache_qe = NULL;
1976 if ( (GNUNET_NO == rh->only_cached) &&
1977 ( (NULL == block) ||
1978 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
1980 /* namecache knows nothing; try DHT lookup */
1981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1982 "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
1984 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
1985 GNUNET_h2s (&query));
1986 GNUNET_GNSRECORD_query_from_public_key (auth,
1989 start_dht_request(rh, query);
1992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1993 "Received result from namecache for label `%s'\n",
1996 if ( (NULL == block) ||
1997 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
1999 /* DHT not permitted and no local result, fail */
2000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2001 "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
2003 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2004 rh->proc (rh->proc_cls, 0, NULL);
2005 GNS_resolver_lookup_cancel (rh);
2009 GNUNET_GNSRECORD_block_decrypt (block,
2012 &handle_gns_resolution_result,
2015 GNUNET_break_op (0); /* block was ill-formed */
2016 rh->proc (rh->proc_cls, 0, NULL);
2017 GNS_resolver_lookup_cancel (rh);
2024 * Lookup tail of our authority chain in the namecache.
2026 * @param rh query we are processing
2029 recursive_gns_resolution_namecache (struct GNS_ResolverHandle *rh)
2031 struct AuthorityChain *ac = rh->ac_tail;
2032 struct GNUNET_HashCode query;
2034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2035 "Starting GNS resolution for `%s' in zone %s\n",
2037 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2038 GNUNET_GNSRECORD_query_from_public_key (&ac->authority_info.gns_authority,
2041 if (GNUNET_YES == use_cache)
2043 rh->namecache_qe = GNUNET_NAMECACHE_lookup_block (namecache_handle,
2045 &handle_namecache_block_response,
2047 GNUNET_assert (NULL != rh->namecache_qe);
2050 start_dht_request (rh, query);
2055 * Function called with the result from a revocation check.
2057 * @param cls the `struct GNS_ResovlerHandle`
2058 * @param is_valid #GNUNET_YES if the zone was not yet revoked
2061 handle_revocation_result (void *cls,
2064 struct GNS_ResolverHandle *rh = cls;
2065 struct AuthorityChain *ac = rh->ac_tail;
2067 rh->rev_check = NULL;
2068 if (GNUNET_YES != is_valid)
2070 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2071 _("Zone %s was revoked, resolution fails\n"),
2072 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2073 rh->proc (rh->proc_cls, 0, NULL);
2074 GNS_resolver_lookup_cancel (rh);
2077 recursive_gns_resolution_namecache (rh);
2082 * Perform revocation check on tail of our authority chain.
2084 * @param rh query we are processing
2087 recursive_gns_resolution_revocation (struct GNS_ResolverHandle *rh)
2089 struct AuthorityChain *ac = rh->ac_tail;
2091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2092 "Starting revocation check for zone %s\n",
2093 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2094 rh->rev_check = GNUNET_REVOCATION_query (cfg,
2095 &ac->authority_info.gns_authority,
2096 &handle_revocation_result,
2098 GNUNET_assert (NULL != rh->rev_check);
2103 * Task scheduled to continue with the resolution process.
2105 * @param cls the `struct GNS_ResolverHandle` of the resolution
2106 * @param tc task context
2109 recursive_resolution (void *cls,
2110 const struct GNUNET_SCHEDULER_TaskContext *tc)
2112 struct GNS_ResolverHandle *rh = cls;
2114 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
2115 if (MAX_RECURSION < rh->loop_limiter++)
2117 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2118 "Encountered unbounded recursion resolving `%s'\n",
2120 rh->proc (rh->proc_cls, 0, NULL);
2121 GNS_resolver_lookup_cancel (rh);
2124 if (GNUNET_YES == rh->ac_tail->gns_authority)
2125 recursive_gns_resolution_revocation (rh);
2127 recursive_dns_resolution (rh);
2132 * Begin the resolution process from 'name', starting with
2133 * the identification of the zone specified by 'name'.
2135 * @param rh resolution to perform
2138 start_resolver_lookup (struct GNS_ResolverHandle *rh)
2140 struct AuthorityChain *ac;
2145 if (1 == inet_pton (AF_INET,
2149 /* name is IPv4 address, pretend it's an A record */
2150 struct GNUNET_GNSRECORD_Data rd;
2153 rd.data_size = sizeof (v4);
2154 rd.expiration_time = UINT64_MAX;
2155 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
2157 rh->proc (rh->proc_cls, 1, &rd);
2158 GNS_resolver_lookup_cancel (rh);
2161 if (1 == inet_pton (AF_INET6,
2165 /* name is IPv6 address, pretend it's an AAAA record */
2166 struct GNUNET_GNSRECORD_Data rd;
2169 rd.data_size = sizeof (v6);
2170 rd.expiration_time = UINT64_MAX;
2171 rd.record_type = GNUNET_DNSPARSER_TYPE_AAAA;
2173 rh->proc (rh->proc_cls, 1, &rd);
2174 GNS_resolver_lookup_cancel (rh);
2177 if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
2178 (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
2179 ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
2180 (GNUNET_YES != is_zkey_tld (rh->name)) ) )
2182 /* use standard DNS lookup */
2185 switch (rh->record_type)
2187 case GNUNET_DNSPARSER_TYPE_A:
2190 case GNUNET_DNSPARSER_TYPE_AAAA:
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 "Doing standard DNS lookup for `%s'\n",
2200 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
2207 if (is_zkey_tld (rh->name))
2209 /* Name ends with ".zkey", try to replace authority zone with zkey
2211 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
2212 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
2215 GNUNET_CRYPTO_ecdsa_public_key_from_string (y,
2217 &rh->authority_zone)) )
2219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2220 _("Hostname `%s' is not well-formed, resolution fails\n"),
2222 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
2224 GNUNET_free_non_null (y);
2228 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
2229 GNUNET_free (resolver_lookup_get_next_label (rh));
2231 ac = GNUNET_new (struct AuthorityChain);
2233 ac->label = resolver_lookup_get_next_label (rh);
2234 ac->suggested_shortening_label = NULL;
2235 if (NULL == ac->label)
2236 /* name was just "gnu", so we default to label '+' */
2237 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
2238 ac->gns_authority = GNUNET_YES;
2239 ac->authority_info.gns_authority = rh->authority_zone;
2240 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
2243 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
2249 * Lookup of a record in a specific zone calls lookup result processor
2252 * @param zone the zone to perform the lookup in
2253 * @param record_type the record type to look up
2254 * @param name the name to look up
2255 * @param shorten_key a private key for use with PSEU import (can be NULL)
2256 * @param only_cached #GNUNET_NO to only check locally not DHT for performance
2257 * @param proc the processor to call on result
2258 * @param proc_cls the closure to pass to @a proc
2259 * @return handle to cancel operation
2261 struct GNS_ResolverHandle *
2262 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
2263 uint32_t record_type,
2265 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key,
2267 GNS_ResultProcessor proc, void *proc_cls)
2269 struct GNS_ResolverHandle *rh;
2271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2272 (NULL == shorten_key)
2273 ? "Starting lookup for `%s' with shortening disabled\n"
2274 : "Starting lookup for `%s' with shortening enabled\n",
2276 rh = GNUNET_new (struct GNS_ResolverHandle);
2277 GNUNET_CONTAINER_DLL_insert (rlh_head,
2280 rh->authority_zone = *zone;
2282 rh->proc_cls = proc_cls;
2283 rh->only_cached = only_cached;
2284 rh->record_type = record_type;
2285 rh->name = GNUNET_strdup (name);
2286 rh->name_resolution_pos = strlen (name);
2287 if (NULL != shorten_key)
2289 rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
2290 *rh->shorten_key = *shorten_key;
2292 start_resolver_lookup (rh);
2298 * Cancel active resolution (i.e. client disconnected).
2300 * @param rh resolution to abort
2303 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
2305 struct DnsResult *dr;
2306 struct AuthorityChain *ac;
2307 struct VpnContext *vpn_ctx;
2309 GNUNET_CONTAINER_DLL_remove (rlh_head,
2312 while (NULL != (ac = rh->ac_head))
2314 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
2317 GNUNET_free (ac->label);
2318 GNUNET_free_non_null (ac->suggested_shortening_label);
2321 if (NULL != rh->g2dc)
2323 /* rh->g2dc->rh is NOT in the DLL yet, so to enable us
2324 using GNS_resolver_lookup_cancel here, we need to
2326 if (NULL != rh->g2dc->rh)
2328 GNUNET_CONTAINER_DLL_insert (rlh_head,
2331 GNS_resolver_lookup_cancel (rh->g2dc->rh);
2332 rh->g2dc->rh = NULL;
2334 GNUNET_free (rh->g2dc->ns);
2335 GNUNET_free (rh->g2dc);
2338 if (GNUNET_SCHEDULER_NO_TASK != rh->task_id)
2340 GNUNET_SCHEDULER_cancel (rh->task_id);
2341 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
2343 if (NULL != rh->get_handle)
2345 GNUNET_DHT_get_stop (rh->get_handle);
2346 rh->get_handle = NULL;
2348 if (NULL != rh->dht_heap_node)
2350 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
2351 rh->dht_heap_node = NULL;
2353 if (NULL != (vpn_ctx = rh->vpn_ctx))
2355 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
2356 GNUNET_free (vpn_ctx->rd_data);
2357 GNUNET_free (vpn_ctx);
2359 if (NULL != rh->dns_request)
2361 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
2362 rh->dns_request = NULL;
2364 if (NULL != rh->namecache_qe)
2366 GNUNET_NAMECACHE_cancel (rh->namecache_qe);
2367 rh->namecache_qe = NULL;
2369 if (NULL != rh->rev_check)
2371 GNUNET_REVOCATION_query_cancel (rh->rev_check);
2372 rh->rev_check = NULL;
2374 if (NULL != rh->std_resolve)
2376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2377 "Canceling standard DNS resolution\n");
2378 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
2379 rh->std_resolve = NULL;
2381 while (NULL != (dr = rh->dns_result_head))
2383 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
2384 rh->dns_result_tail,
2388 GNUNET_free_non_null (rh->shorten_key);
2389 GNUNET_free (rh->name);
2394 /* ***************** Resolver initialization ********************* */
2398 * Initialize the resolver
2400 * @param nc the namecache handle
2401 * @param dht the dht handle
2402 * @param c configuration handle
2403 * @param max_bg_queries maximum number of parallel background queries in dht
2406 GNS_resolver_init (struct GNUNET_NAMECACHE_Handle *nc,
2407 struct GNUNET_DHT_Handle *dht,
2408 const struct GNUNET_CONFIGURATION_Handle *c,
2409 unsigned long long max_bg_queries)
2414 namecache_handle = nc;
2417 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2418 max_allowed_background_queries = max_bg_queries;
2419 if (GNUNET_SYSERR == (use_cache = GNUNET_CONFIGURATION_get_value_yesno (c,
2422 use_cache = GNUNET_YES;
2423 if (GNUNET_NO == use_cache)
2424 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Namecache disabled\n");
2427 GNUNET_CONFIGURATION_get_value_string (c,
2432 /* user did not specify DNS resolver, use 8.8.8.8 */
2433 dns_ip = GNUNET_strdup ("8.8.8.8");
2435 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
2436 GNUNET_free (dns_ip);
2437 vpn_handle = GNUNET_VPN_connect (cfg);
2445 GNS_resolver_done ()
2447 struct GNS_ResolverHandle *rh;
2448 struct CacheOps *co;
2450 /* abort active resolutions */
2451 while (NULL != (rh = rlh_head))
2453 rh->proc (rh->proc_cls, 0, NULL);
2454 GNS_resolver_lookup_cancel (rh);
2456 while (NULL != (co = co_head))
2458 GNUNET_CONTAINER_DLL_remove (co_head,
2461 GNUNET_NAMECACHE_cancel (co->namecache_qe_cache);
2464 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
2465 dht_lookup_heap = NULL;
2466 GNUNET_DNSSTUB_stop (dns_handle);
2468 GNUNET_VPN_disconnect (vpn_handle);
2471 namecache_handle = NULL;
2475 /* *************** common helper functions (do not really belong here) *********** */
2478 * Checks if @a name ends in ".TLD"
2480 * @param name the name to check
2481 * @param tld the TLD to check for
2482 * @return GNUNET_YES or GNUNET_NO
2485 is_tld (const char* name, const char* tld)
2489 if (strlen (name) <= strlen (tld))
2491 offset = strlen (name) - strlen (tld);
2492 if (0 != strcmp (name + offset, tld))
2498 /* end of gnunet-service-gns_resolver.c */