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 * #GNUNET_YES if the authority was a GNS authority,
105 * #GNUNET_NO if the authority was a DNS authority.
110 * Information about the resolver authority for this label.
116 * The zone of the GNS authority
118 struct GNUNET_CRYPTO_EcdsaPublicKey gns_authority;
123 * Domain of the DNS resolver that is the authority.
124 * (appended to construct the DNS name to resolve;
125 * this is NOT the DNS name of the DNS server!).
127 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
130 * IP address of the DNS resolver that is authoritative.
131 * (this implementation currently only supports one
134 struct sockaddr_storage dns_ip;
144 * A result we got from DNS.
152 struct DnsResult *next;
157 struct DnsResult *prev;
160 * Binary value stored in the DNS record (appended to this struct)
165 * Expiration time for the DNS record, 0 if we didn't
166 * get anything useful (i.e. 'gethostbyname' was used).
168 uint64_t expiration_time;
171 * Number of bytes in @e data.
176 * Type of the GNS/DNS record.
178 uint32_t record_type;
184 * Closure for #vpn_allocation_cb.
190 * Which resolution process are we processing.
192 struct GNS_ResolverHandle *rh;
195 * Handle to the VPN request that we were performing.
197 struct GNUNET_VPN_RedirectionRequest *vpn_request;
200 * Number of records serialized in @e rd_data.
202 unsigned int rd_count;
205 * Serialized records.
210 * Number of bytes in @e rd_data.
217 * Information we keep during the resolution of an
218 * IP address for a DNS server while handling a
221 struct Gns2DnsContext
225 * DNS domain in which the resolution will continue
226 * (first part of the GNS2DNS record).
231 * Handle for the resolution of the IP part of the
232 * GNS2DNS record. Will return to us the addresses
233 * of the DNS resolver to use.
235 struct GNS_ResolverHandle *rh;
241 * Handle to a currenty pending resolution. On result (positive or
242 * negative) the #GNS_ResultProcessor is called.
244 struct GNS_ResolverHandle
250 struct GNS_ResolverHandle *next;
255 struct GNS_ResolverHandle *prev;
258 * The top-level GNS authoritative zone to query
260 struct GNUNET_CRYPTO_EcdsaPublicKey authority_zone;
263 * called when resolution phase finishes
265 GNS_ResultProcessor proc;
268 * closure passed to @e proc
273 * Handle used during GNS2DNS resolution for looking up the
274 * IP address of the DNS server.
276 struct Gns2DnsContext *g2dc;
279 * Handle for DHT lookups. should be NULL if no lookups are in progress
281 struct GNUNET_DHT_GetHandle *get_handle;
284 * Handle to a VPN request, NULL if none is active.
286 struct VpnContext *vpn_ctx;
289 * Socket for a DNS request, NULL if none is active.
291 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
294 * Handle for standard DNS resolution, NULL if none is active.
296 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
299 * Pending Namecache lookup task
301 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe;
304 * Pending revocation check.
306 struct GNUNET_REVOCATION_Query *rev_check;
309 * Heap node associated with this lookup. Used to limit number of
310 * concurrent requests.
312 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
315 * DLL to store the authority chain
317 struct AuthorityChain *ac_head;
320 * DLL to store the authority chain
322 struct AuthorityChain *ac_tail;
325 * Private key of the shorten zone, NULL to not shorten.
327 struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key;
330 * ID of a task associated with the resolution process.
332 GNUNET_SCHEDULER_TaskIdentifier task_id;
335 * The name to resolve
340 * DLL of results we got from DNS.
342 struct DnsResult *dns_result_head;
345 * DLL of results we got from DNS.
347 struct DnsResult *dns_result_tail;
350 * Current offset in 'name' where we are resolving.
352 size_t name_resolution_pos;
360 * Desired type for the resolution.
365 * We increment the loop limiter for each step in a recursive
366 * resolution. If it passes our threshold (i.e. due to
367 * self-recursion in the resolution, i.e CNAME fun), we stop.
369 unsigned int loop_limiter;
375 * Active namestore caching operations.
381 * Organized in a DLL.
383 struct CacheOps *next;
386 * Organized in a DLL.
388 struct CacheOps *prev;
391 * Pending Namestore caching task.
393 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe_cache;
399 * Our handle to the namecache service
401 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
404 * Our handle to the vpn service
406 static struct GNUNET_VPN_Handle *vpn_handle;
409 * Resolver handle to the dht
411 static struct GNUNET_DHT_Handle *dht_handle;
414 * Handle to perform DNS lookups.
416 static struct GNUNET_DNSSTUB_Context *dns_handle;
419 * Heap for limiting parallel DHT lookups
421 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
424 * Maximum amount of parallel queries to the DHT
426 static unsigned long long max_allowed_background_queries;
429 * Head of resolver lookup list
431 static struct GNS_ResolverHandle *rlh_head;
434 * Tail of resolver lookup list
436 static struct GNS_ResolverHandle *rlh_tail;
439 * Organized in a DLL.
441 static struct CacheOps *co_head;
444 * Organized in a DLL.
446 static struct CacheOps *co_tail;
450 * Global configuration.
452 static const struct GNUNET_CONFIGURATION_Handle *cfg;
456 * Check if name is in srv format (_x._y.xxx)
459 * @return #GNUNET_YES if true
462 is_srv (const char *name)
469 if (NULL == strstr (name, "._"))
472 ndup = GNUNET_strdup (name);
474 if (NULL == strtok (NULL, "."))
476 if (NULL == strtok (NULL, "."))
478 if (NULL != strtok (NULL, "."))
487 * Determine if this name is canonical (is a legal name in a zone, without delegation);
488 * note that we do not test that the name does not contain illegal characters, we only
489 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
490 * even though they consist of multiple labels.
493 * a.b.gnu = not canonical
495 * _foo._srv = canonical
496 * _f.bar = not canonical
498 * @param name the name to test
499 * @return #GNUNET_YES if canonical
502 is_canonical (const char *name)
507 if (NULL == strchr (name, '.'))
512 while (NULL != (dot = strchr (pos, '.')))
520 /* ************************** Resolution **************************** */
523 * Expands a name ending in .+ with the zone of origin.
525 * @param rh resolution context
526 * @param name name to modify (to be free'd or returned)
527 * @return updated name
530 translate_dot_plus (struct GNS_ResolverHandle *rh,
534 size_t s_len = strlen (name);
536 if (0 != strcmp (&name[s_len - 2],
538 return name; /* did not end in ".+" */
539 GNUNET_assert (GNUNET_YES == rh->ac_tail->gns_authority);
540 GNUNET_asprintf (&ret,
544 GNUNET_GNSRECORD_pkey_to_zkey (&rh->ac_tail->authority_info.gns_authority));
551 * Task scheduled to asynchronously fail a resolution.
553 * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
554 * @param tc task context
557 fail_resolution (void *cls,
558 const struct GNUNET_SCHEDULER_TaskContext *tc)
560 struct GNS_ResolverHandle *rh = cls;
562 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
563 rh->proc (rh->proc_cls, 0, NULL);
564 GNS_resolver_lookup_cancel (rh);
568 #if (defined WINDOWS) || (defined DARWIN)
569 /* Don't have this on W32, here's a naive implementation
570 * Was somehow removed on OS X ... */
572 memrchr (const void *s,
576 const unsigned char *ucs = s;
579 for (i = n - 1; i >= 0; i--)
580 if (c == (int) ucs[i])
581 return (void *) &ucs[i];
588 * Get the next, rightmost label from the name that we are trying to resolve,
589 * and update the resolution position accordingly.
591 * @param rh handle to the resolution operation to get the next label from
592 * @return NULL if there are no more labels
595 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
601 if (0 == rh->name_resolution_pos)
603 dot = memrchr (rh->name, (int) '.', rh->name_resolution_pos);
606 /* done, this was the last one */
607 len = rh->name_resolution_pos;
609 rh->name_resolution_pos = 0;
613 /* advance by one label */
614 len = rh->name_resolution_pos - (dot - rh->name) - 1;
616 rh->name_resolution_pos = dot - rh->name;
618 return GNUNET_strndup (rp, len);
623 * Gives the cummulative result obtained to the callback and clean up the request.
625 * @param rh resolution process that has culminated in a result
628 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
630 struct DnsResult *pos;
635 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
638 struct GNUNET_GNSRECORD_Data rd[n];
641 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
643 rd[i].data = pos->data;
644 rd[i].data_size = pos->data_size;
645 rd[i].record_type = pos->record_type;
646 if (0 == pos->expiration_time)
648 rd[i].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
649 rd[i].expiration_time = 0;
653 rd[i].flags = GNUNET_GNSRECORD_RF_NONE;
654 rd[i].expiration_time = pos->expiration_time;
658 GNUNET_assert (i == n);
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 "Transmitting standard DNS result with %u records\n",
662 rh->proc (rh->proc_cls,
666 GNS_resolver_lookup_cancel (rh);
671 * Add a result from DNS to the records to be returned to the application.
673 * @param rh resolution request to extend with a result
674 * @param expiration_time expiration time for the answer
675 * @param record_type DNS record type of the answer
676 * @param data_size number of bytes in @a data
677 * @param data binary data to return in DNS record
680 add_dns_result (struct GNS_ResolverHandle *rh,
681 uint64_t expiration_time,
682 uint32_t record_type,
686 struct DnsResult *res;
688 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
689 res->expiration_time = expiration_time;
690 res->data_size = data_size;
691 res->record_type = record_type;
693 memcpy (&res[1], data, data_size);
694 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
701 * We had to do a DNS lookup. Convert the result (if any) and return
704 * @param cls closure with the `struct GNS_ResolverHandle`
705 * @param addr one of the addresses of the host, NULL for the last address
706 * @param addrlen length of the address
709 handle_dns_result (void *cls,
710 const struct sockaddr *addr,
713 struct GNS_ResolverHandle *rh = cls;
714 const struct sockaddr_in *sa4;
715 const struct sockaddr_in6 *sa6;
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "Received %u bytes of DNS IP data\n",
722 rh->std_resolve = NULL;
723 transmit_lookup_dns_result (rh);
726 switch (addr->sa_family)
729 sa4 = (const struct sockaddr_in *) addr;
731 0 /* expiration time is unknown */,
732 GNUNET_DNSPARSER_TYPE_A,
733 sizeof (struct in_addr),
737 sa6 = (const struct sockaddr_in6 *) addr;
739 0 /* expiration time is unknown */,
740 GNUNET_DNSPARSER_TYPE_AAAA,
741 sizeof (struct in6_addr),
752 * Task scheduled to continue with the resolution process.
754 * @param cls the 'struct GNS_ResolverHandle' of the resolution
755 * @param tc task context
758 recursive_resolution (void *cls,
759 const struct GNUNET_SCHEDULER_TaskContext *tc);
763 * Begin the resolution process from 'name', starting with
764 * the identification of the zone specified by 'name'.
766 * @param rh resolution to perform
769 start_resolver_lookup (struct GNS_ResolverHandle *rh);
773 * Function called with the result of a DNS resolution.
775 * @param cls the request handle of the resolution that
776 * we were attempting to make
777 * @param rs socket that received the response
778 * @param dns dns response, never NULL
779 * @param dns_len number of bytes in @a dns
782 dns_result_parser (void *cls,
783 struct GNUNET_DNSSTUB_RequestSocket *rs,
784 const struct GNUNET_TUN_DnsHeader *dns,
787 struct GNS_ResolverHandle *rh = cls;
788 struct GNUNET_DNSPARSER_Packet *p;
789 const struct GNUNET_DNSPARSER_Record *rec;
790 unsigned int rd_count;
793 rh->dns_request = NULL;
794 GNUNET_SCHEDULER_cancel (rh->task_id);
795 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
796 p = GNUNET_DNSPARSER_parse ((const char *) dns,
800 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
801 _("Failed to parse DNS response\n"));
802 rh->proc (rh->proc_cls, 0, NULL);
803 GNS_resolver_lookup_cancel (rh);
806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
807 "Received DNS response for `%s' with %u answers\n",
809 (unsigned int) p->num_answers);
810 if ( (p->num_answers > 0) &&
811 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
812 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
814 GNUNET_free (rh->name);
815 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
816 start_resolver_lookup (rh);
817 GNUNET_DNSPARSER_free_packet (p);
820 /* FIXME: add DNAME support */
822 /* convert from (parsed) DNS to (binary) GNS format! */
823 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
825 struct GNUNET_GNSRECORD_Data rd[rd_count];
827 char buf[UINT16_MAX];
833 memset (rd, 0, sizeof (rd));
834 for (i=0;i<rd_count;i++)
836 if (i < p->num_answers)
837 rec = &p->answers[i];
838 else if (i < p->num_answers + p->num_authority_records)
839 rec = &p->authority_records[i - p->num_answers];
841 rec = &p->authority_records[i - p->num_answers - p->num_authority_records];
842 /* As we copied the full DNS name to 'rh->ac_tail->label', this
843 should be the correct check to see if this record is actually
844 a record for our label... */
845 if (0 != strcmp (rec->name,
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "Dropping record `%s', does not match desired name `%s'\n",
855 rd[i - skip].record_type = rec->type;
856 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
859 case GNUNET_DNSPARSER_TYPE_A:
860 if (rec->data.raw.data_len != sizeof (struct in_addr))
866 rd[i - skip].data_size = rec->data.raw.data_len;
867 rd[i - skip].data = rec->data.raw.data;
869 case GNUNET_DNSPARSER_TYPE_AAAA:
870 if (rec->data.raw.data_len != sizeof (struct in6_addr))
876 rd[i - skip].data_size = rec->data.raw.data_len;
877 rd[i - skip].data = rec->data.raw.data;
879 case GNUNET_DNSPARSER_TYPE_CNAME:
880 case GNUNET_DNSPARSER_TYPE_PTR:
881 case GNUNET_DNSPARSER_TYPE_NS:
884 GNUNET_DNSPARSER_builder_add_name (buf,
893 rd[i - skip].data_size = buf_off - buf_start;
894 rd[i - skip].data = &buf[buf_start];
896 case GNUNET_DNSPARSER_TYPE_SOA:
899 GNUNET_DNSPARSER_builder_add_soa (buf,
908 rd[i - skip].data_size = buf_off - buf_start;
909 rd[i - skip].data = &buf[buf_start];
911 case GNUNET_DNSPARSER_TYPE_MX:
914 GNUNET_DNSPARSER_builder_add_mx (buf,
923 rd[i - skip].data_size = buf_off - buf_start;
924 rd[i - skip].data = &buf[buf_start];
926 case GNUNET_DNSPARSER_TYPE_SRV:
929 GNUNET_DNSPARSER_builder_add_srv (buf,
938 rd[i - skip].data_size = buf_off - buf_start;
939 rd[i - skip].data = &buf[buf_start];
942 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
943 _("Skipping record of unsupported type %d\n"),
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "Returning DNS response for `%s' with %u answers\n",
952 (unsigned int) p->num_answers);
953 rh->proc (rh->proc_cls, rd_count - skip, rd);
954 GNS_resolver_lookup_cancel (rh);
956 GNUNET_DNSPARSER_free_packet (p);
961 * Perform recursive DNS resolution. Asks the given DNS resolver to
962 * resolve "rh->dns_name", possibly recursively proceeding following
963 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
964 * we find the answer.
966 * @param rh resolution information
969 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
971 struct AuthorityChain *ac;
973 struct GNUNET_DNSPARSER_Query *query;
974 struct GNUNET_DNSPARSER_Packet *p;
976 size_t dns_request_length;
979 GNUNET_assert (NULL != ac);
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "Starting DNS lookup for `%s'\n",
983 GNUNET_assert (GNUNET_NO == ac->gns_authority);
984 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
987 sa_len = sizeof (struct sockaddr_in);
990 sa_len = sizeof (struct sockaddr_in6);
994 rh->proc (rh->proc_cls, 0, NULL);
995 GNS_resolver_lookup_cancel (rh);
998 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
999 query->name = GNUNET_strdup (ac->label);
1000 query->type = rh->record_type;
1001 query->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1002 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
1005 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1007 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
1008 p->flags.recursion_desired = 1;
1010 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
1013 rh->proc (rh->proc_cls, 0, NULL);
1014 GNS_resolver_lookup_cancel (rh);
1018 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
1019 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
1025 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
1029 GNUNET_free (dns_request);
1030 GNUNET_DNSPARSER_free_packet (p);
1035 * We encountered a CNAME record during our resolution.
1036 * Merge it into our chain.
1038 * @param rh resolution we are performing
1039 * @param cname value of the cname record we got for the current
1040 * authority chain tail
1043 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
1048 struct AuthorityChain *ac;
1050 nlen = strlen (cname);
1053 &cname[nlen - 2])) )
1055 /* CNAME resolution continues relative to current domain */
1056 if (0 == rh->name_resolution_pos)
1058 res = GNUNET_strndup (cname, nlen - 2);
1059 rh->name_resolution_pos = nlen - 2;
1063 GNUNET_asprintf (&res,
1065 (int) rh->name_resolution_pos,
1069 rh->name_resolution_pos = strlen (res);
1071 GNUNET_free (rh->name);
1073 ac = GNUNET_new (struct AuthorityChain);
1075 ac->gns_authority = GNUNET_YES;
1076 ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
1077 ac->label = resolver_lookup_get_next_label (rh);
1078 /* tigger shortening */
1079 if (NULL != rh->shorten_key)
1080 GNS_shorten_start (rh->ac_tail->label,
1082 &ac->authority_info.gns_authority,
1084 /* add AC to tail */
1085 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1088 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1092 /* name is absolute, start from the beginning */
1093 GNUNET_free (rh->name);
1094 rh->name = GNUNET_strdup (cname);
1095 start_resolver_lookup (rh);
1100 * Process a records that were decrypted from a block.
1102 * @param cls closure with the 'struct GNS_ResolverHandle'
1103 * @param rd_count number of entries in @a rd array
1104 * @param rd array of records with data to store
1107 handle_gns_resolution_result (void *cls,
1108 unsigned int rd_count,
1109 const struct GNUNET_GNSRECORD_Data *rd);
1113 * Callback invoked from the VPN service once a redirection is
1114 * available. Provides the IP address that can now be used to
1115 * reach the requested destination. Replaces the "VPN" record
1116 * with the respective A/AAAA record and continues processing.
1118 * @param cls closure
1119 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1120 * will match 'result_af' from the request
1121 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1122 * that the VPN allocated for the redirection;
1123 * traffic to this IP will now be redirected to the
1124 * specified target peer; NULL on error
1127 vpn_allocation_cb (void *cls,
1129 const void *address)
1131 struct VpnContext *vpn_ctx = cls;
1132 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1133 struct GNUNET_GNSRECORD_Data rd[vpn_ctx->rd_count];
1136 vpn_ctx->vpn_request = NULL;
1138 GNUNET_assert (GNUNET_OK ==
1139 GNUNET_GNSRECORD_records_deserialize (vpn_ctx->rd_data_size,
1143 for (i=0;i<vpn_ctx->rd_count;i++)
1145 if (GNUNET_GNSRECORD_TYPE_VPN == rd[i].record_type)
1150 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1151 rd[i].data_size = sizeof (struct in_addr);
1152 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1154 rd[i].data = address;
1157 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1158 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1160 rd[i].data = address;
1161 rd[i].data_size = sizeof (struct in6_addr);
1169 GNUNET_assert (i < vpn_ctx->rd_count);
1170 handle_gns_resolution_result (rh,
1173 GNUNET_free (vpn_ctx->rd_data);
1174 GNUNET_free (vpn_ctx);
1179 * We've resolved the IP address for the DNS resolver to use
1180 * after encountering a GNS2DNS record.
1182 * TODO: Right now we only foward the request to ONE DNS resolver,
1183 * even if we get multiple IP addresses back; a correct implementation
1184 * should try all DNS resolvers.
1186 * @param cls the `struct GNS_ResolverHandle` where we encountered
1187 * the GNS2DNS record
1188 * @param rd_count number of records in @a rd
1189 * @param rd addresses for the DNS resolver (presumably)
1192 handle_gns2dns_result (void *cls,
1193 unsigned int rd_count,
1194 const struct GNUNET_GNSRECORD_Data *rd)
1196 struct GNS_ResolverHandle *rh = cls;
1197 struct AuthorityChain *ac;
1199 struct sockaddr *sa;
1200 struct sockaddr_in v4;
1201 struct sockaddr_in6 v6;
1204 /* find suitable A/AAAA record */
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1206 "Received %u results for IP address of DNS server for GNS2DNS transition\n",
1208 /* enable cleanup of 'rh' handle that comes next... */
1209 GNUNET_CONTAINER_DLL_insert (rlh_head,
1212 rh->g2dc->rh = NULL;
1215 for (j=0;j<rd_count;j++)
1217 switch (rd[j].record_type)
1219 case GNUNET_DNSPARSER_TYPE_A:
1220 if (sizeof (struct in_addr) != rd[j].data_size)
1222 GNUNET_break_op (0);
1223 rh->proc (rh->proc_cls, 0, NULL);
1224 GNS_resolver_lookup_cancel (rh);
1227 /* FIXME: might want to check if we support IPv4 here,
1228 and otherwise skip this one and hope we find another */
1229 memset (&v4, 0, sizeof (v4));
1230 sa_len = sizeof (v4);
1231 v4.sin_family = AF_INET;
1232 v4.sin_port = htons (53);
1233 #if HAVE_SOCKADDR_IN_SIN_LEN
1234 v4.sin_len = (u_char) sa_len;
1236 memcpy (&v4.sin_addr,
1238 sizeof (struct in_addr));
1239 sa = (struct sockaddr *) &v4;
1241 case GNUNET_DNSPARSER_TYPE_AAAA:
1242 if (sizeof (struct in6_addr) != rd[j].data_size)
1244 GNUNET_break_op (0);
1245 rh->proc (rh->proc_cls, 0, NULL);
1246 GNS_resolver_lookup_cancel (rh);
1249 /* FIXME: might want to check if we support IPv6 here,
1250 and otherwise skip this one and hope we find another */
1251 memset (&v6, 0, sizeof (v6));
1252 sa_len = sizeof (v6);
1253 v6.sin6_family = AF_INET6;
1254 v6.sin6_port = htons (53);
1255 #if HAVE_SOCKADDR_IN_SIN_LEN
1256 v6.sin6_len = (u_char) sa_len;
1258 memcpy (&v6.sin6_addr,
1260 sizeof (struct in6_addr));
1261 sa = (struct sockaddr *) &v6;
1271 /* we cannot continue; NS without A/AAAA */
1272 rh->proc (rh->proc_cls, 0, NULL);
1273 GNS_resolver_lookup_cancel (rh);
1276 /* expand authority chain */
1277 ac = GNUNET_new (struct AuthorityChain);
1279 strcpy (ac->authority_info.dns_authority.name,
1281 memcpy (&ac->authority_info.dns_authority.dns_ip,
1284 /* for DNS recursion, the label is the full DNS name,
1285 created from the remainder of the GNS name and the
1286 name in the NS record */
1287 GNUNET_asprintf (&ac->label,
1289 (int) rh->name_resolution_pos,
1291 (0 != rh->name_resolution_pos) ? "." : "",
1293 GNUNET_free (rh->g2dc->ns);
1294 GNUNET_free (rh->g2dc);
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1297 "Will continue resolution using DNS server `%s' to resolve `%s'\n",
1298 GNUNET_a2s (sa, sa_len),
1300 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1303 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1305 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1306 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1308 rh->proc (rh->proc_cls, 0, NULL);
1309 GNS_resolver_lookup_cancel (rh);
1313 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1319 * Process a records that were decrypted from a block.
1321 * @param cls closure with the `struct GNS_ResolverHandle`
1322 * @param rd_count number of entries in @a rd array
1323 * @param rd array of records with data to store
1326 handle_gns_resolution_result (void *cls,
1327 unsigned int rd_count,
1328 const struct GNUNET_GNSRECORD_Data *rd)
1330 struct GNS_ResolverHandle *rh = cls;
1331 struct AuthorityChain *ac;
1334 struct VpnContext *vpn_ctx;
1335 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1337 const char *suggested_label;
1338 struct GNUNET_HashCode vhash;
1340 char scratch[UINT16_MAX];
1342 size_t scratch_start;
1345 struct GNUNET_GNSRECORD_Data rd_new[rd_count];
1346 unsigned int rd_off;
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349 "Resolution succeeded for `%s' in zone %s, got %u records\n",
1351 GNUNET_GNSRECORD_z2s (&rh->ac_tail->authority_info.gns_authority),
1353 if (0 == rh->name_resolution_pos)
1355 /* top-level match, are we done yet? */
1356 if ( (rd_count > 0) &&
1357 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1358 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1361 cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
1364 if ( (NULL == cname) ||
1365 (off != rd[0].data_size) )
1367 GNUNET_break_op (0);
1368 rh->proc (rh->proc_cls, 0, NULL);
1369 GNS_resolver_lookup_cancel (rh);
1370 GNUNET_free_non_null (cname);
1373 handle_gns_cname_result (rh,
1375 GNUNET_free (cname);
1378 /* If A/AAAA was requested, but we got a VPN
1379 record, we convert it to A/AAAA using GNUnet VPN */
1380 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1381 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1383 for (i=0;i<rd_count;i++)
1385 switch (rd[i].record_type)
1387 case GNUNET_GNSRECORD_TYPE_VPN:
1389 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1390 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) <
1393 GNUNET_break_op (0);
1394 rh->proc (rh->proc_cls, 0, NULL);
1395 GNS_resolver_lookup_cancel (rh);
1398 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1399 vname = (const char *) &vpn[1];
1400 if ('\0' != vname[rd[i].data_size - 1 - 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 GNUNET_CRYPTO_hash (vname,
1408 strlen (vname), // FIXME: +1?
1410 vpn_ctx = GNUNET_new (struct VpnContext);
1411 rh->vpn_ctx = vpn_ctx;
1413 vpn_ctx->rd_data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
1415 vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
1416 (void) GNUNET_GNSRECORD_records_serialize (rd_count,
1418 vpn_ctx->rd_data_size,
1420 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1425 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1430 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1432 /* delegation to DNS */
1433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1434 "Found GNS2DNS record, delegating to DNS!\n");
1442 /* convert relative names in record values to absolute names,
1443 using 'scratch' array for memory allocations */
1446 for (i=0;i<rd_count;i++)
1448 rd_new[rd_off] = rd[i];
1449 /* Check if the embedded name(s) end in "+", and if so,
1450 replace the "+" with the zone at "ac_tail", changing the name
1451 to a ".zkey". The name is allocated on the 'scratch' array,
1452 so we can free it afterwards. */
1453 switch (rd[i].record_type)
1455 case GNUNET_DNSPARSER_TYPE_CNAME:
1460 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1463 if ( (NULL == cname) ||
1464 (off != rd[i].data_size) )
1466 GNUNET_break_op (0); /* record not well-formed */
1470 cname = translate_dot_plus (rh, cname);
1471 GNUNET_break (NULL != cname);
1472 scratch_start = scratch_off;
1474 GNUNET_DNSPARSER_builder_add_name (scratch,
1483 rd_new[rd_off].data = &scratch[scratch_start];
1484 rd_new[rd_off].data_size = scratch_off - scratch_start;
1488 GNUNET_free_non_null (cname);
1491 case GNUNET_DNSPARSER_TYPE_SOA:
1493 struct GNUNET_DNSPARSER_SoaRecord *soa;
1496 soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
1499 if ( (NULL == soa) ||
1500 (off != rd[i].data_size) )
1502 GNUNET_break_op (0); /* record not well-formed */
1506 soa->mname = translate_dot_plus (rh, soa->mname);
1507 soa->rname = translate_dot_plus (rh, soa->rname);
1508 scratch_start = scratch_off;
1510 GNUNET_DNSPARSER_builder_add_soa (scratch,
1519 rd_new[rd_off].data = &scratch[scratch_start];
1520 rd_new[rd_off].data_size = scratch_off - scratch_start;
1525 GNUNET_DNSPARSER_free_soa (soa);
1528 case GNUNET_DNSPARSER_TYPE_MX:
1530 struct GNUNET_DNSPARSER_MxRecord *mx;
1533 mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
1536 if ( (NULL == mx) ||
1537 (off != rd[i].data_size) )
1539 GNUNET_break_op (0); /* record not well-formed */
1543 mx->mxhost = translate_dot_plus (rh, mx->mxhost);
1544 scratch_start = scratch_off;
1546 GNUNET_DNSPARSER_builder_add_mx (scratch,
1555 rd_new[rd_off].data = &scratch[scratch_start];
1556 rd_new[rd_off].data_size = scratch_off - scratch_start;
1561 GNUNET_DNSPARSER_free_mx (mx);
1564 case GNUNET_DNSPARSER_TYPE_SRV:
1566 struct GNUNET_DNSPARSER_SrvRecord *srv;
1569 /* FIXME: passing rh->name here is is not necessarily what we want
1570 (SRV support not finished) */
1571 srv = GNUNET_DNSPARSER_parse_srv (rh->name,
1575 if ( (NULL == srv) ||
1576 (off != rd[i].data_size) )
1578 GNUNET_break_op (0); /* record not well-formed */
1582 srv->domain_name = translate_dot_plus (rh, srv->domain_name);
1583 srv->target = translate_dot_plus (rh, srv->target);
1584 scratch_start = scratch_off;
1586 GNUNET_DNSPARSER_builder_add_srv (scratch,
1595 rd_new[rd_off].data = &scratch[scratch_start];
1596 rd_new[rd_off].data_size = scratch_off - scratch_start;
1601 GNUNET_DNSPARSER_free_srv (srv);
1604 case GNUNET_GNSRECORD_TYPE_PKEY:
1606 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
1608 if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
1610 GNUNET_break_op (0);
1613 memcpy (&pub, rd[i].data, rd[i].data_size);
1615 /* tigger shortening */
1616 if (NULL != rh->shorten_key)
1618 suggested_label = NULL;
1619 for (c2 = 0; c2< rd_count; c2++)
1621 if ((GNUNET_GNSRECORD_TYPE_NICK ==rd[c2].record_type) &&
1622 (rd[i].data_size > 0) &&
1623 (((const char *) rd[c2].data)[rd[c2].data_size -1] == '\0'))
1624 suggested_label = (const char *) rd->data;
1626 if (NULL != suggested_label)
1627 GNS_shorten_start (rh->ac_tail->label,
1633 if (GNUNET_GNSRECORD_TYPE_PKEY != rh->record_type)
1635 /* try to resolve "+" */
1636 struct AuthorityChain *ac;
1638 ac = GNUNET_new (struct AuthorityChain);
1640 ac->gns_authority = GNUNET_YES;
1641 ac->authority_info.gns_authority = pub;
1642 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
1643 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1646 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1652 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1654 /* delegation to DNS */
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1656 "Found GNS2DNS record, delegating to DNS!\n");
1665 /* yes, we are done, return result */
1666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1667 "Returning GNS response for `%s' with %u answers\n",
1670 rh->proc (rh->proc_cls, rd_off, rd_new);
1671 GNS_resolver_lookup_cancel (rh);
1675 /* need to recurse, check if we can */
1676 for (i=0;i<rd_count;i++)
1678 switch (rd[i].record_type)
1680 case GNUNET_GNSRECORD_TYPE_PKEY:
1681 /* delegation to another zone */
1682 if (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
1685 GNUNET_break_op (0);
1686 rh->proc (rh->proc_cls, 0, NULL);
1687 GNS_resolver_lookup_cancel (rh);
1690 /* expand authority chain */
1691 ac = GNUNET_new (struct AuthorityChain);
1693 ac->gns_authority = GNUNET_YES;
1694 memcpy (&ac->authority_info.gns_authority,
1696 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1697 ac->label = resolver_lookup_get_next_label (rh);
1698 /* tigger shortening */
1699 if (NULL != rh->shorten_key)
1701 suggested_label = NULL;
1702 for (c2 = 0; c2< rd_count; c2++)
1704 if ((GNUNET_GNSRECORD_TYPE_NICK ==rd[c2].record_type) &&
1705 (rd[c2].data_size > 0) &&
1706 ((const char *) rd[c2].data)[rd[c2].data_size -1] == '\0')
1707 suggested_label = (const char *) rd[c2].data;
1709 if (NULL != suggested_label)
1710 GNS_shorten_start (rh->ac_tail->label,
1712 &ac->authority_info.gns_authority,
1715 /* add AC to tail */
1716 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1720 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1723 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1725 /* resolution continues within DNS */
1726 struct Gns2DnsContext *g2dc;
1731 ns = GNUNET_DNSPARSER_parse_name (rd[i].data,
1734 ip = GNUNET_DNSPARSER_parse_name (rd[i].data,
1737 if ( (NULL == ns) ||
1739 (off != rd[i].data_size) )
1741 GNUNET_break_op (0);
1742 GNUNET_free_non_null (ns);
1743 GNUNET_free_non_null (ip);
1744 rh->proc (rh->proc_cls, 0, NULL);
1745 GNS_resolver_lookup_cancel (rh);
1748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1749 "Resolving `%s' to determine IP address of DNS server for GNS2DNS transition\n",
1751 /* resolve 'ip' to determine the IP(s) of the DNS
1753 g2dc = GNUNET_new (struct Gns2DnsContext);
1755 g2dc->rh = GNUNET_new (struct GNS_ResolverHandle);
1756 g2dc->rh->authority_zone = rh->ac_tail->authority_info.gns_authority;
1757 ip = translate_dot_plus (rh, ip);
1758 g2dc->rh->name = ip;
1759 g2dc->rh->name_resolution_pos = strlen (ip);
1760 g2dc->rh->proc = &handle_gns2dns_result;
1761 g2dc->rh->proc_cls = rh;
1762 g2dc->rh->record_type = GNUNET_GNSRECORD_TYPE_ANY;
1763 g2dc->rh->only_cached = GNUNET_NO;
1764 g2dc->rh->loop_limiter = rh->loop_limiter + 1;
1766 start_resolver_lookup (g2dc->rh);
1769 case GNUNET_DNSPARSER_TYPE_CNAME:
1774 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1777 if ( (NULL == cname) ||
1778 (off != rd[i].data_size) )
1780 GNUNET_break_op (0); /* record not well-formed */
1781 rh->proc (rh->proc_cls, 0, NULL);
1782 GNS_resolver_lookup_cancel (rh);
1783 GNUNET_free_non_null (cname);
1786 handle_gns_cname_result (rh,
1788 GNUNET_free (cname);
1791 /* FIXME: handle DNAME */
1797 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1798 _("GNS lookup recursion failed (no delegation record found)\n"));
1799 rh->proc (rh->proc_cls, 0, NULL);
1800 GNS_resolver_lookup_cancel (rh);
1805 * Function called once the namestore has completed the request for
1808 * @param cls closure with the `struct CacheOps`
1809 * @param success #GNUNET_OK on success
1810 * @param emsg error message
1813 namecache_cache_continuation (void *cls,
1817 struct CacheOps *co = cls;
1819 co->namecache_qe_cache = NULL;
1821 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1822 _("Failed to cache GNS resolution: %s\n"),
1824 GNUNET_CONTAINER_DLL_remove (co_head,
1832 * Iterator called on each result obtained for a DHT
1833 * operation that expects a reply
1835 * @param cls closure with the `struct GNS_ResolverHandle`
1836 * @param exp when will this value expire
1837 * @param key key of the result
1838 * @param get_path peers on reply path (or NULL if not recorded)
1839 * [0] = datastore's first neighbor, [length - 1] = local peer
1840 * @param get_path_length number of entries in @a get_path
1841 * @param put_path peers on the PUT path (or NULL if not recorded)
1842 * [0] = origin, [length - 1] = datastore
1843 * @param put_path_length number of entries in @a put_path
1844 * @param type type of the result
1845 * @param size number of bytes in data
1846 * @param data pointer to the result data
1849 handle_dht_response (void *cls,
1850 struct GNUNET_TIME_Absolute exp,
1851 const struct GNUNET_HashCode *key,
1852 const struct GNUNET_PeerIdentity *get_path,
1853 unsigned int get_path_length,
1854 const struct GNUNET_PeerIdentity *put_path,
1855 unsigned int put_path_length,
1856 enum GNUNET_BLOCK_Type type,
1857 size_t size, const void *data)
1859 struct GNS_ResolverHandle *rh = cls;
1860 struct AuthorityChain *ac = rh->ac_tail;
1861 const struct GNUNET_GNSRECORD_Block *block;
1862 struct CacheOps *co;
1864 GNUNET_DHT_get_stop (rh->get_handle);
1865 rh->get_handle = NULL;
1866 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1867 rh->dht_heap_node = NULL;
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1869 "Handling response from the DHT\n");
1870 if (size < sizeof (struct GNUNET_GNSRECORD_Block))
1872 /* how did this pass DHT block validation!? */
1874 rh->proc (rh->proc_cls, 0, NULL);
1875 GNS_resolver_lookup_cancel (rh);
1880 ntohl (block->purpose.size) +
1881 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
1882 sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
1884 /* how did this pass DHT block validation!? */
1886 rh->proc (rh->proc_cls, 0, NULL);
1887 GNS_resolver_lookup_cancel (rh);
1891 GNUNET_GNSRECORD_block_decrypt (block,
1892 &ac->authority_info.gns_authority,
1894 &handle_gns_resolution_result,
1897 GNUNET_break_op (0); /* block was ill-formed */
1898 rh->proc (rh->proc_cls, 0, NULL);
1899 GNS_resolver_lookup_cancel (rh);
1902 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us)
1904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1905 "Received expired block from the DHT, will not cache it.\n");
1908 /* Cache well-formed blocks */
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1910 "Caching response from the DHT in namestore\n");
1911 co = GNUNET_new (struct CacheOps);
1912 co->namecache_qe_cache = GNUNET_NAMECACHE_block_cache (namecache_handle,
1914 &namecache_cache_continuation,
1916 GNUNET_CONTAINER_DLL_insert (co_head,
1923 * Process a record that was stored in the namestore.
1925 * @param cls closure with the `struct GNS_ResolverHandle`
1926 * @param block block that was stored in the namestore
1929 handle_namestore_block_response (void *cls,
1930 const struct GNUNET_GNSRECORD_Block *block)
1932 struct GNS_ResolverHandle *rh = cls;
1933 struct GNS_ResolverHandle *rx;
1934 struct AuthorityChain *ac = rh->ac_tail;
1935 const char *label = ac->label;
1936 const struct GNUNET_CRYPTO_EcdsaPublicKey *auth = &ac->authority_info.gns_authority;
1937 struct GNUNET_HashCode query;
1939 GNUNET_GNSRECORD_query_from_public_key (auth,
1942 GNUNET_assert (NULL != rh->namecache_qe);
1943 rh->namecache_qe = NULL;
1944 if ( (GNUNET_NO == rh->only_cached) &&
1945 ( (NULL == block) ||
1946 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
1948 /* Namestore knows nothing; try DHT lookup */
1949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1950 "Starting DHT lookup for `%s' in zone %s\n",
1952 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
1953 GNUNET_assert (NULL == rh->get_handle);
1954 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1955 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1957 DHT_GNS_REPLICATION_LEVEL,
1958 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1960 &handle_dht_response, rh);
1961 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1963 GNUNET_TIME_absolute_get ().abs_value_us);
1964 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
1966 /* fail longest-standing DHT request */
1967 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
1968 GNUNET_assert (NULL != rx);
1969 rx->proc (rx->proc_cls, 0, NULL);
1970 GNS_resolver_lookup_cancel (rx);
1974 if ( (NULL == block) ||
1975 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
1977 /* DHT not permitted and no local result, fail */
1978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1979 "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
1981 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
1982 rh->proc (rh->proc_cls, 0, NULL);
1983 GNS_resolver_lookup_cancel (rh);
1987 GNUNET_GNSRECORD_block_decrypt (block,
1990 &handle_gns_resolution_result,
1993 GNUNET_break_op (0); /* block was ill-formed */
1994 rh->proc (rh->proc_cls, 0, NULL);
1995 GNS_resolver_lookup_cancel (rh);
2002 * Lookup tail of our authority chain in the namestore.
2004 * @param rh query we are processing
2007 recursive_gns_resolution_namestore (struct GNS_ResolverHandle *rh)
2009 struct AuthorityChain *ac = rh->ac_tail;
2010 struct GNUNET_HashCode query;
2012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2013 "Starting GNS resolution for `%s' in zone %s\n",
2015 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2016 GNUNET_GNSRECORD_query_from_public_key (&ac->authority_info.gns_authority,
2019 rh->namecache_qe = GNUNET_NAMECACHE_lookup_block (namecache_handle,
2021 &handle_namestore_block_response,
2023 GNUNET_assert (NULL != rh->namecache_qe);
2028 * Function called with the result from a revocation check.
2030 * @param cls the `struct GNS_ResovlerHandle`
2031 * @param is_valid #GNUNET_YES if the zone was not yet revoked
2034 handle_revocation_result (void *cls,
2037 struct GNS_ResolverHandle *rh = cls;
2038 struct AuthorityChain *ac = rh->ac_tail;
2040 rh->rev_check = NULL;
2041 if (GNUNET_YES != is_valid)
2043 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2044 _("Zone %s was revoked, resolution fails\n"),
2045 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2046 rh->proc (rh->proc_cls, 0, NULL);
2047 GNS_resolver_lookup_cancel (rh);
2050 recursive_gns_resolution_namestore (rh);
2055 * Perform revocation check on tail of our authority chain.
2057 * @param rh query we are processing
2060 recursive_gns_resolution_revocation (struct GNS_ResolverHandle *rh)
2062 struct AuthorityChain *ac = rh->ac_tail;
2064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2065 "Starting revocation check for zone %s\n",
2066 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2067 rh->rev_check = GNUNET_REVOCATION_query (cfg,
2068 &ac->authority_info.gns_authority,
2069 &handle_revocation_result,
2071 GNUNET_assert (NULL != rh->rev_check);
2076 * Task scheduled to continue with the resolution process.
2078 * @param cls the `struct GNS_ResolverHandle` of the resolution
2079 * @param tc task context
2082 recursive_resolution (void *cls,
2083 const struct GNUNET_SCHEDULER_TaskContext *tc)
2085 struct GNS_ResolverHandle *rh = cls;
2087 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
2088 if (MAX_RECURSION < rh->loop_limiter++)
2090 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2091 "Encountered unbounded recursion resolving `%s'\n",
2093 rh->proc (rh->proc_cls, 0, NULL);
2094 GNS_resolver_lookup_cancel (rh);
2097 if (GNUNET_YES == rh->ac_tail->gns_authority)
2098 recursive_gns_resolution_revocation (rh);
2100 recursive_dns_resolution (rh);
2105 * Begin the resolution process from 'name', starting with
2106 * the identification of the zone specified by 'name'.
2108 * @param rh resolution to perform
2111 start_resolver_lookup (struct GNS_ResolverHandle *rh)
2113 struct AuthorityChain *ac;
2118 if (1 == inet_pton (AF_INET,
2122 /* name is IPv4 address, pretend it's an A record */
2123 struct GNUNET_GNSRECORD_Data rd;
2126 rd.data_size = sizeof (v4);
2127 rd.expiration_time = UINT64_MAX;
2128 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
2130 rh->proc (rh->proc_cls, 1, &rd);
2131 GNS_resolver_lookup_cancel (rh);
2134 if (1 == inet_pton (AF_INET6,
2138 /* name is IPv6 address, pretend it's an AAAA record */
2139 struct GNUNET_GNSRECORD_Data rd;
2142 rd.data_size = sizeof (v6);
2143 rd.expiration_time = UINT64_MAX;
2144 rd.record_type = GNUNET_DNSPARSER_TYPE_AAAA;
2146 rh->proc (rh->proc_cls, 1, &rd);
2147 GNS_resolver_lookup_cancel (rh);
2150 if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
2151 (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
2152 ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
2153 (GNUNET_YES != is_zkey_tld (rh->name)) ) )
2155 /* use standard DNS lookup */
2158 switch (rh->record_type)
2160 case GNUNET_DNSPARSER_TYPE_A:
2163 case GNUNET_DNSPARSER_TYPE_AAAA:
2170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2171 "Doing standard DNS lookup for `%s'\n",
2173 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
2180 if (is_zkey_tld (rh->name))
2182 /* Name ends with ".zkey", try to replace authority zone with zkey
2184 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
2185 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
2188 GNUNET_CRYPTO_ecdsa_public_key_from_string (y,
2190 &rh->authority_zone)) )
2192 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2193 _("Hostname `%s' is not well-formed, resolution fails\n"),
2195 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
2197 GNUNET_free_non_null (y);
2201 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
2202 GNUNET_free (resolver_lookup_get_next_label (rh));
2204 ac = GNUNET_new (struct AuthorityChain);
2206 ac->label = resolver_lookup_get_next_label (rh);
2207 if (NULL == ac->label)
2208 /* name was just "gnu", so we default to label '+' */
2209 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
2210 ac->gns_authority = GNUNET_YES;
2211 ac->authority_info.gns_authority = rh->authority_zone;
2212 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
2215 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
2221 * Lookup of a record in a specific zone calls lookup result processor
2224 * @param zone the zone to perform the lookup in
2225 * @param record_type the record type to look up
2226 * @param name the name to look up
2227 * @param shorten_key a private key for use with PSEU import (can be NULL)
2228 * @param only_cached #GNUNET_NO to only check locally not DHT for performance
2229 * @param proc the processor to call on result
2230 * @param proc_cls the closure to pass to @a proc
2231 * @return handle to cancel operation
2233 struct GNS_ResolverHandle *
2234 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
2235 uint32_t record_type,
2237 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key,
2239 GNS_ResultProcessor proc, void *proc_cls)
2241 struct GNS_ResolverHandle *rh;
2243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2244 (NULL == shorten_key)
2245 ? "Starting lookup for `%s' with shortening disabled\n"
2246 : "Starting lookup for `%s' with shortening enabled\n",
2248 rh = GNUNET_new (struct GNS_ResolverHandle);
2249 GNUNET_CONTAINER_DLL_insert (rlh_head,
2252 rh->authority_zone = *zone;
2254 rh->proc_cls = proc_cls;
2255 rh->only_cached = only_cached;
2256 rh->record_type = record_type;
2257 rh->name = GNUNET_strdup (name);
2258 rh->name_resolution_pos = strlen (name);
2259 if (NULL != shorten_key)
2261 rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
2262 *rh->shorten_key = *shorten_key;
2264 start_resolver_lookup (rh);
2270 * Cancel active resolution (i.e. client disconnected).
2272 * @param rh resolution to abort
2275 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
2277 struct DnsResult *dr;
2278 struct AuthorityChain *ac;
2279 struct VpnContext *vpn_ctx;
2281 GNUNET_CONTAINER_DLL_remove (rlh_head,
2284 while (NULL != (ac = rh->ac_head))
2286 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
2289 GNUNET_free (ac->label);
2292 if (NULL != rh->g2dc)
2294 /* rh->g2dc->rh is NOT in the DLL yet, so to enable us
2295 using GNS_resolver_lookup_cancel here, we need to
2297 if (NULL != rh->g2dc->rh)
2299 GNUNET_CONTAINER_DLL_insert (rlh_head,
2302 GNS_resolver_lookup_cancel (rh->g2dc->rh);
2303 rh->g2dc->rh = NULL;
2305 GNUNET_free (rh->g2dc->ns);
2306 GNUNET_free (rh->g2dc);
2309 if (GNUNET_SCHEDULER_NO_TASK != rh->task_id)
2311 GNUNET_SCHEDULER_cancel (rh->task_id);
2312 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
2314 if (NULL != rh->get_handle)
2316 GNUNET_DHT_get_stop (rh->get_handle);
2317 rh->get_handle = NULL;
2319 if (NULL != rh->dht_heap_node)
2321 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
2322 rh->dht_heap_node = NULL;
2324 if (NULL != (vpn_ctx = rh->vpn_ctx))
2326 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
2327 GNUNET_free (vpn_ctx->rd_data);
2328 GNUNET_free (vpn_ctx);
2330 if (NULL != rh->dns_request)
2332 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
2333 rh->dns_request = NULL;
2335 if (NULL != rh->namecache_qe)
2337 GNUNET_NAMECACHE_cancel (rh->namecache_qe);
2338 rh->namecache_qe = NULL;
2340 if (NULL != rh->rev_check)
2342 GNUNET_REVOCATION_query_cancel (rh->rev_check);
2343 rh->rev_check = NULL;
2345 if (NULL != rh->std_resolve)
2347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2348 "Canceling standard DNS resolution\n");
2349 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
2350 rh->std_resolve = NULL;
2352 while (NULL != (dr = rh->dns_result_head))
2354 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
2355 rh->dns_result_tail,
2359 GNUNET_free_non_null (rh->shorten_key);
2360 GNUNET_free (rh->name);
2365 /* ***************** Resolver initialization ********************* */
2369 * Initialize the resolver
2371 * @param nc the namecache handle
2372 * @param dht the dht handle
2373 * @param c configuration handle
2374 * @param max_bg_queries maximum number of parallel background queries in dht
2377 GNS_resolver_init (struct GNUNET_NAMECACHE_Handle *nc,
2378 struct GNUNET_DHT_Handle *dht,
2379 const struct GNUNET_CONFIGURATION_Handle *c,
2380 unsigned long long max_bg_queries)
2385 namecache_handle = nc;
2388 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2389 max_allowed_background_queries = max_bg_queries;
2391 GNUNET_CONFIGURATION_get_value_string (c,
2396 /* user did not specify DNS resolver, use 8.8.8.8 */
2397 dns_ip = GNUNET_strdup ("8.8.8.8");
2399 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
2400 GNUNET_free (dns_ip);
2401 vpn_handle = GNUNET_VPN_connect (cfg);
2409 GNS_resolver_done ()
2411 struct GNS_ResolverHandle *rh;
2412 struct CacheOps *co;
2414 /* abort active resolutions */
2415 while (NULL != (rh = rlh_head))
2417 rh->proc (rh->proc_cls, 0, NULL);
2418 GNS_resolver_lookup_cancel (rh);
2420 while (NULL != (co = co_head))
2422 GNUNET_CONTAINER_DLL_remove (co_head,
2425 GNUNET_NAMECACHE_cancel (co->namecache_qe_cache);
2428 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
2429 dht_lookup_heap = NULL;
2430 GNUNET_DNSSTUB_stop (dns_handle);
2432 GNUNET_VPN_disconnect (vpn_handle);
2435 namecache_handle = NULL;
2439 /* *************** common helper functions (do not really belong here) *********** */
2442 * Checks if @a name ends in ".TLD"
2444 * @param name the name to check
2445 * @param tld the TLD to check for
2446 * @return GNUNET_YES or GNUNET_NO
2449 is_tld (const char* name, const char* tld)
2453 if (strlen (name) <= strlen (tld))
2455 offset = strlen (name) - strlen (tld);
2456 if (0 != strcmp (name + offset, tld))
2462 /* end of gnunet-service-gns_resolver.c */