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_dnsparser_lib.h"
43 #include "gnunet_gns_service.h"
45 #include "gnunet-service-gns_resolver.h"
46 #include "gnunet-service-gns_shorten.h"
47 #include "gnunet_vpn_service.h"
51 * Default DHT timeout for lookups.
53 #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
56 * Default timeout for DNS lookups.
58 #define DNS_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
61 * Default timeout for VPN redirections.
63 #define VPN_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
66 * DHT replication level
68 #define DHT_GNS_REPLICATION_LEVEL 5
71 * How deep do we allow recursions to go before we abort?
73 #define MAX_RECURSION 256
77 * DLL to hold the authority chain we had to pass in the resolution
85 struct AuthorityChain *prev;
90 struct AuthorityChain *next;
93 * Resolver handle this entry in the chain belongs to.
95 struct GNS_ResolverHandle *rh;
98 * label/name corresponding to the authority
103 * #GNUNET_YES if the authority was a GNS authority,
104 * #GNUNET_NO if the authority was a DNS authority.
109 * Information about the resolver authority for this label.
115 * The zone of the GNS authority
117 struct GNUNET_CRYPTO_EcdsaPublicKey gns_authority;
122 * Domain of the DNS resolver that is the authority.
123 * (appended to construct the DNS name to resolve;
124 * this is NOT the DNS name of the DNS server!).
126 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
129 * IP address of the DNS resolver that is authoritative.
130 * (this implementation currently only supports one
133 struct sockaddr_storage dns_ip;
143 * A result we got from DNS.
151 struct DnsResult *next;
156 struct DnsResult *prev;
159 * Binary value stored in the DNS record (appended to this struct)
164 * Expiration time for the DNS record, 0 if we didn't
165 * get anything useful (i.e. 'gethostbyname' was used).
167 uint64_t expiration_time;
170 * Number of bytes in @e data.
175 * Type of the GNS/DNS record.
177 uint32_t record_type;
183 * Closure for #vpn_allocation_cb.
189 * Which resolution process are we processing.
191 struct GNS_ResolverHandle *rh;
194 * Handle to the VPN request that we were performing.
196 struct GNUNET_VPN_RedirectionRequest *vpn_request;
199 * Number of records serialized in @e rd_data.
201 unsigned int rd_count;
204 * Serialized records.
209 * Number of bytes in @e rd_data.
216 * Handle to a currenty pending resolution. On result (positive or
217 * negative) the #GNS_ResultProcessor is called.
219 struct GNS_ResolverHandle
225 struct GNS_ResolverHandle *next;
230 struct GNS_ResolverHandle *prev;
233 * The top-level GNS authoritative zone to query
235 struct GNUNET_CRYPTO_EcdsaPublicKey authority_zone;
238 * called when resolution phase finishes
240 GNS_ResultProcessor proc;
243 * closure passed to proc
248 * Handle for DHT lookups. should be NULL if no lookups are in progress
250 struct GNUNET_DHT_GetHandle *get_handle;
253 * Handle to a VPN request, NULL if none is active.
255 struct VpnContext *vpn_ctx;
258 * Socket for a DNS request, NULL if none is active.
260 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
263 * Handle for standard DNS resolution, NULL if none is active.
265 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
268 * Pending Namecache lookup task
270 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe;
273 * Heap node associated with this lookup. Used to limit number of
274 * concurrent requests.
276 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
279 * DLL to store the authority chain
281 struct AuthorityChain *ac_head;
284 * DLL to store the authority chain
286 struct AuthorityChain *ac_tail;
289 * Private key of the shorten zone, NULL to not shorten.
291 struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key;
294 * ID of a task associated with the resolution process.
296 GNUNET_SCHEDULER_TaskIdentifier task_id;
299 * The name to resolve
304 * DLL of results we got from DNS.
306 struct DnsResult *dns_result_head;
309 * DLL of results we got from DNS.
311 struct DnsResult *dns_result_tail;
314 * Current offset in 'name' where we are resolving.
316 size_t name_resolution_pos;
324 * Desired type for the resolution.
329 * We increment the loop limiter for each step in a recursive
330 * resolution. If it passes our threshold (i.e. due to
331 * self-recursion in the resolution, i.e CNAME fun), we stop.
333 unsigned int loop_limiter;
339 * Active namestore caching operations.
345 * Organized in a DLL.
347 struct CacheOps *next;
350 * Organized in a DLL.
352 struct CacheOps *prev;
355 * Pending Namestore caching task.
357 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe_cache;
363 * Our handle to the namestore service
365 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
368 * Our handle to the namecache service
370 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
373 * Our handle to the vpn service
375 static struct GNUNET_VPN_Handle *vpn_handle;
378 * Resolver handle to the dht
380 static struct GNUNET_DHT_Handle *dht_handle;
383 * Handle to perform DNS lookups.
385 static struct GNUNET_DNSSTUB_Context *dns_handle;
388 * Heap for limiting parallel DHT lookups
390 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
393 * Maximum amount of parallel queries to the DHT
395 static unsigned long long max_allowed_background_queries;
398 * Head of resolver lookup list
400 static struct GNS_ResolverHandle *rlh_head;
403 * Tail of resolver lookup list
405 static struct GNS_ResolverHandle *rlh_tail;
408 * Organized in a DLL.
410 static struct CacheOps *co_head;
413 * Organized in a DLL.
415 static struct CacheOps *co_tail;
419 * Global configuration.
421 static const struct GNUNET_CONFIGURATION_Handle *cfg;
425 * Check if name is in srv format (_x._y.xxx)
428 * @return #GNUNET_YES if true
431 is_srv (const char *name)
438 if (NULL == strstr (name, "._"))
441 ndup = GNUNET_strdup (name);
443 if (NULL == strtok (NULL, "."))
445 if (NULL == strtok (NULL, "."))
447 if (NULL != strtok (NULL, "."))
456 * Determine if this name is canonical (is a legal name in a zone, without delegation);
457 * note that we do not test that the name does not contain illegal characters, we only
458 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
459 * even though they consist of multiple labels.
462 * a.b.gnu = not canonical
464 * _foo._srv = canonical
465 * _f.bar = not canonical
467 * @param name the name to test
468 * @return #GNUNET_YES if canonical
471 is_canonical (const char *name)
476 if (NULL == strchr (name, '.'))
481 while (NULL != (dot = strchr (pos, '.')))
489 /* ************************** Resolution **************************** */
492 * Expands a name ending in .+ with the zone of origin.
494 * @param rh resolution context
495 * @param name name to modify (to be free'd or returned)
496 * @return updated name
499 translate_dot_plus (struct GNS_ResolverHandle *rh,
503 size_t s_len = strlen (name);
505 if (0 != strcmp (&name[s_len - 2],
507 return name; /* did not end in ".+" */
508 GNUNET_assert (GNUNET_YES == rh->ac_tail->gns_authority);
509 GNUNET_asprintf (&ret,
513 GNUNET_GNSRECORD_pkey_to_zkey (&rh->ac_tail->authority_info.gns_authority));
520 * Task scheduled to asynchronously fail a resolution.
522 * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
523 * @param tc task context
526 fail_resolution (void *cls,
527 const struct GNUNET_SCHEDULER_TaskContext *tc)
529 struct GNS_ResolverHandle *rh = cls;
531 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
532 rh->proc (rh->proc_cls, 0, NULL);
533 GNS_resolver_lookup_cancel (rh);
537 #if (defined WINDOWS) || (defined DARWIN)
538 /* Don't have this on W32, here's a naive implementation
539 * Was somehow removed on OS X ... */
541 memrchr (const void *s,
545 const unsigned char *ucs = s;
548 for (i = n - 1; i >= 0; i--)
549 if (c == (int) ucs[i])
550 return (void *) &ucs[i];
557 * Get the next, rightmost label from the name that we are trying to resolve,
558 * and update the resolution position accordingly.
560 * @param rh handle to the resolution operation to get the next label from
561 * @return NULL if there are no more labels
564 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
570 if (0 == rh->name_resolution_pos)
572 dot = memrchr (rh->name, (int) '.', rh->name_resolution_pos);
575 /* done, this was the last one */
576 len = rh->name_resolution_pos;
578 rh->name_resolution_pos = 0;
582 /* advance by one label */
583 len = rh->name_resolution_pos - (dot - rh->name) - 1;
585 rh->name_resolution_pos = dot - rh->name;
587 return GNUNET_strndup (rp, len);
592 * Gives the cummulative result obtained to the callback and clean up the request.
594 * @param rh resolution process that has culminated in a result
597 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
599 struct DnsResult *pos;
604 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
607 struct GNUNET_GNSRECORD_Data rd[n];
610 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
612 rd[i].data = pos->data;
613 rd[i].data_size = pos->data_size;
614 rd[i].record_type = pos->record_type;
615 if (0 == pos->expiration_time)
617 rd[i].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
618 rd[i].expiration_time = 0;
622 rd[i].flags = GNUNET_GNSRECORD_RF_NONE;
623 rd[i].expiration_time = pos->expiration_time;
627 GNUNET_assert (i == n);
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629 "Transmitting standard DNS result with %u records\n",
631 rh->proc (rh->proc_cls,
635 GNS_resolver_lookup_cancel (rh);
640 * Add a result from DNS to the records to be returned to the application.
642 * @param rh resolution request to extend with a result
643 * @param expiration_time expiration time for the answer
644 * @param record_type DNS record type of the answer
645 * @param data_size number of bytes in @a data
646 * @param data binary data to return in DNS record
649 add_dns_result (struct GNS_ResolverHandle *rh,
650 uint64_t expiration_time,
651 uint32_t record_type,
655 struct DnsResult *res;
657 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
658 res->expiration_time = expiration_time;
659 res->data_size = data_size;
660 res->record_type = record_type;
662 memcpy (&res[1], data, data_size);
663 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
670 * We had to do a DNS lookup. Convert the result (if any) and return
673 * @param cls closure with the `struct GNS_ResolverHandle`
674 * @param addr one of the addresses of the host, NULL for the last address
675 * @param addrlen length of the address
678 handle_dns_result (void *cls,
679 const struct sockaddr *addr,
682 struct GNS_ResolverHandle *rh = cls;
683 const struct sockaddr_in *sa4;
684 const struct sockaddr_in6 *sa6;
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "Received %u bytes of DNS IP data\n",
691 rh->std_resolve = NULL;
692 transmit_lookup_dns_result (rh);
695 switch (addr->sa_family)
698 sa4 = (const struct sockaddr_in *) addr;
700 0 /* expiration time is unknown */,
701 GNUNET_DNSPARSER_TYPE_A,
702 sizeof (struct in_addr),
706 sa6 = (const struct sockaddr_in6 *) addr;
708 0 /* expiration time is unknown */,
709 GNUNET_DNSPARSER_TYPE_AAAA,
710 sizeof (struct in6_addr),
721 * Task scheduled to continue with the resolution process.
723 * @param cls the 'struct GNS_ResolverHandle' of the resolution
724 * @param tc task context
727 recursive_resolution (void *cls,
728 const struct GNUNET_SCHEDULER_TaskContext *tc);
732 * Begin the resolution process from 'name', starting with
733 * the identification of the zone specified by 'name'.
735 * @param rh resolution to perform
738 start_resolver_lookup (struct GNS_ResolverHandle *rh);
742 * Function called with the result of a DNS resolution.
744 * @param cls the request handle of the resolution that
745 * we were attempting to make
746 * @param rs socket that received the response
747 * @param dns dns response, never NULL
748 * @param dns_len number of bytes in @a dns
751 dns_result_parser (void *cls,
752 struct GNUNET_DNSSTUB_RequestSocket *rs,
753 const struct GNUNET_TUN_DnsHeader *dns,
756 struct GNS_ResolverHandle *rh = cls;
757 struct GNUNET_DNSPARSER_Packet *p;
758 const struct GNUNET_DNSPARSER_Record *rec;
759 unsigned int rd_count;
762 rh->dns_request = NULL;
763 GNUNET_SCHEDULER_cancel (rh->task_id);
764 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
765 p = GNUNET_DNSPARSER_parse ((const char *) dns,
769 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
770 _("Failed to parse DNS response\n"));
771 rh->proc (rh->proc_cls, 0, NULL);
772 GNS_resolver_lookup_cancel (rh);
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "Received DNS response for `%s' with %u answers\n",
778 (unsigned int) p->num_answers);
779 if ( (p->num_answers > 0) &&
780 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
781 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
783 GNUNET_free (rh->name);
784 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
785 start_resolver_lookup (rh);
786 GNUNET_DNSPARSER_free_packet (p);
789 /* FIXME: add DNAME support */
791 /* convert from (parsed) DNS to (binary) GNS format! */
792 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
794 struct GNUNET_GNSRECORD_Data rd[rd_count];
796 char buf[UINT16_MAX];
802 memset (rd, 0, sizeof (rd));
803 for (i=0;i<rd_count;i++)
805 if (i < p->num_answers)
806 rec = &p->answers[i];
807 else if (i < p->num_answers + p->num_authority_records)
808 rec = &p->authority_records[i - p->num_answers];
810 rec = &p->authority_records[i - p->num_answers - p->num_authority_records];
811 /* As we copied the full DNS name to 'rh->ac_tail->label', this
812 should be the correct check to see if this record is actually
813 a record for our label... */
814 if (0 != strcmp (rec->name,
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 "Dropping record `%s', does not match desired name `%s'\n",
824 rd[i - skip].record_type = rec->type;
825 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
828 case GNUNET_DNSPARSER_TYPE_A:
829 if (rec->data.raw.data_len != sizeof (struct in_addr))
835 rd[i - skip].data_size = rec->data.raw.data_len;
836 rd[i - skip].data = rec->data.raw.data;
838 case GNUNET_DNSPARSER_TYPE_AAAA:
839 if (rec->data.raw.data_len != sizeof (struct in6_addr))
845 rd[i - skip].data_size = rec->data.raw.data_len;
846 rd[i - skip].data = rec->data.raw.data;
848 case GNUNET_DNSPARSER_TYPE_CNAME:
849 case GNUNET_DNSPARSER_TYPE_PTR:
850 case GNUNET_DNSPARSER_TYPE_NS:
853 GNUNET_DNSPARSER_builder_add_name (buf,
862 rd[i - skip].data_size = buf_off - buf_start;
863 rd[i - skip].data = &buf[buf_start];
865 case GNUNET_DNSPARSER_TYPE_SOA:
868 GNUNET_DNSPARSER_builder_add_soa (buf,
877 rd[i - skip].data_size = buf_off - buf_start;
878 rd[i - skip].data = &buf[buf_start];
880 case GNUNET_DNSPARSER_TYPE_MX:
883 GNUNET_DNSPARSER_builder_add_mx (buf,
892 rd[i - skip].data_size = buf_off - buf_start;
893 rd[i - skip].data = &buf[buf_start];
895 case GNUNET_DNSPARSER_TYPE_SRV:
898 GNUNET_DNSPARSER_builder_add_srv (buf,
907 rd[i - skip].data_size = buf_off - buf_start;
908 rd[i - skip].data = &buf[buf_start];
911 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
912 _("Skipping record of unsupported type %d\n"),
918 rh->proc (rh->proc_cls, rd_count - skip, rd);
919 GNS_resolver_lookup_cancel (rh);
921 GNUNET_DNSPARSER_free_packet (p);
926 * Perform recursive DNS resolution. Asks the given DNS resolver to
927 * resolve "rh->dns_name", possibly recursively proceeding following
928 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
929 * we find the answer.
931 * @param rh resolution information
934 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
936 struct AuthorityChain *ac;
938 struct GNUNET_DNSPARSER_Query *query;
939 struct GNUNET_DNSPARSER_Packet *p;
941 size_t dns_request_length;
944 GNUNET_assert (NULL != ac);
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
946 "Starting DNS lookup for `%s'\n",
948 GNUNET_assert (GNUNET_NO == ac->gns_authority);
949 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
952 sa_len = sizeof (struct sockaddr_in);
955 sa_len = sizeof (struct sockaddr_in6);
959 rh->proc (rh->proc_cls, 0, NULL);
960 GNS_resolver_lookup_cancel (rh);
963 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
964 query->name = GNUNET_strdup (ac->label);
965 query->type = rh->record_type;
966 query->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
967 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
970 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
972 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
973 p->flags.recursion_desired = 1;
975 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
978 rh->proc (rh->proc_cls, 0, NULL);
979 GNS_resolver_lookup_cancel (rh);
983 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
984 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
990 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
994 GNUNET_free (dns_request);
995 GNUNET_DNSPARSER_free_packet (p);
1000 * We encountered a CNAME record during our resolution.
1001 * Merge it into our chain.
1003 * @param rh resolution we are performing
1004 * @param cname value of the cname record we got for the current
1005 * authority chain tail
1008 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
1013 struct AuthorityChain *ac;
1015 nlen = strlen (cname);
1018 &cname[nlen - 2])) )
1020 /* CNAME resolution continues relative to current domain */
1021 if (0 == rh->name_resolution_pos)
1023 res = GNUNET_strndup (cname, nlen - 2);
1024 rh->name_resolution_pos = nlen - 2;
1028 GNUNET_asprintf (&res,
1030 (int) rh->name_resolution_pos,
1034 rh->name_resolution_pos = strlen (res);
1036 GNUNET_free (rh->name);
1038 ac = GNUNET_new (struct AuthorityChain);
1040 ac->gns_authority = GNUNET_YES;
1041 ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
1042 ac->label = resolver_lookup_get_next_label (rh);
1043 /* tigger shortening */
1044 if (NULL != rh->shorten_key)
1045 GNS_shorten_start (rh->ac_tail->label,
1046 &ac->authority_info.gns_authority,
1048 /* add AC to tail */
1049 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1052 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1056 /* name is absolute, start from the beginning */
1057 GNUNET_free (rh->name);
1058 rh->name = GNUNET_strdup (cname);
1059 start_resolver_lookup (rh);
1064 * Process a records that were decrypted from a block.
1066 * @param cls closure with the 'struct GNS_ResolverHandle'
1067 * @param rd_count number of entries in @a rd array
1068 * @param rd array of records with data to store
1071 handle_gns_resolution_result (void *cls,
1072 unsigned int rd_count,
1073 const struct GNUNET_GNSRECORD_Data *rd);
1077 * Callback invoked from the VPN service once a redirection is
1078 * available. Provides the IP address that can now be used to
1079 * reach the requested destination. Replaces the "VPN" record
1080 * with the respective A/AAAA record and continues processing.
1082 * @param cls closure
1083 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1084 * will match 'result_af' from the request
1085 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1086 * that the VPN allocated for the redirection;
1087 * traffic to this IP will now be redirected to the
1088 * specified target peer; NULL on error
1091 vpn_allocation_cb (void *cls,
1093 const void *address)
1095 struct VpnContext *vpn_ctx = cls;
1096 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1097 struct GNUNET_GNSRECORD_Data rd[vpn_ctx->rd_count];
1100 vpn_ctx->vpn_request = NULL;
1102 GNUNET_assert (GNUNET_OK ==
1103 GNUNET_GNSRECORD_records_deserialize (vpn_ctx->rd_data_size,
1107 for (i=0;i<vpn_ctx->rd_count;i++)
1109 if (GNUNET_GNSRECORD_TYPE_VPN == rd[i].record_type)
1114 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1115 rd[i].data_size = sizeof (struct in_addr);
1116 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1118 rd[i].data = address;
1121 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1122 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1124 rd[i].data = address;
1125 rd[i].data_size = sizeof (struct in6_addr);
1133 GNUNET_assert (i < vpn_ctx->rd_count);
1134 handle_gns_resolution_result (rh,
1137 GNUNET_free (vpn_ctx->rd_data);
1138 GNUNET_free (vpn_ctx);
1143 * Process a records that were decrypted from a block.
1145 * @param cls closure with the `struct GNS_ResolverHandle`
1146 * @param rd_count number of entries in @a rd array
1147 * @param rd array of records with data to store
1150 handle_gns_resolution_result (void *cls,
1151 unsigned int rd_count,
1152 const struct GNUNET_GNSRECORD_Data *rd)
1154 struct GNS_ResolverHandle *rh = cls;
1155 struct AuthorityChain *ac;
1158 struct sockaddr *sa;
1159 struct sockaddr_in v4;
1160 struct sockaddr_in6 v6;
1163 struct VpnContext *vpn_ctx;
1164 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1166 struct GNUNET_HashCode vhash;
1168 char scratch[UINT16_MAX];
1170 size_t scratch_start;
1172 struct GNUNET_GNSRECORD_Data rd_new[rd_count];
1173 unsigned int rd_off;
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "Resolution succeeded for `%s' in zone %s, got %u records\n",
1178 GNUNET_GNSRECORD_z2s (&rh->ac_tail->authority_info.gns_authority),
1180 if (0 == rh->name_resolution_pos)
1182 /* top-level match, are we done yet? */
1183 if ( (rd_count > 0) &&
1184 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1185 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1188 cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
1191 if ( (NULL == cname) ||
1192 (off != rd[0].data_size) )
1194 GNUNET_break_op (0);
1195 rh->proc (rh->proc_cls, 0, NULL);
1196 GNS_resolver_lookup_cancel (rh);
1197 GNUNET_free_non_null (cname);
1200 handle_gns_cname_result (rh,
1202 GNUNET_free (cname);
1205 /* If A/AAAA was requested, but we got a VPN
1206 record, we convert it to A/AAAA using GNUnet VPN */
1207 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1208 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1210 for (i=0;i<rd_count;i++)
1212 switch (rd[i].record_type)
1214 case GNUNET_GNSRECORD_TYPE_VPN:
1216 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1217 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) <
1220 GNUNET_break_op (0);
1221 rh->proc (rh->proc_cls, 0, NULL);
1222 GNS_resolver_lookup_cancel (rh);
1225 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1226 vname = (const char *) &vpn[1];
1227 if ('\0' != vname[rd[i].data_size - 1 - sizeof (struct GNUNET_TUN_GnsVpnRecord)])
1229 GNUNET_break_op (0);
1230 rh->proc (rh->proc_cls, 0, NULL);
1231 GNS_resolver_lookup_cancel (rh);
1234 GNUNET_CRYPTO_hash (vname,
1235 strlen (vname), // FIXME: +1?
1237 vpn_ctx = GNUNET_new (struct VpnContext);
1238 rh->vpn_ctx = vpn_ctx;
1240 vpn_ctx->rd_data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
1242 vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
1243 (void) GNUNET_GNSRECORD_records_serialize (rd_count,
1245 vpn_ctx->rd_data_size,
1247 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1252 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1257 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1259 /* delegation to DNS */
1267 /* convert relative names in record values to absolute names,
1268 using 'scratch' array for memory allocations */
1271 for (i=0;i<rd_count;i++)
1273 rd_new[rd_off] = rd[i];
1274 /* Check if the embedded name(s) end in "+", and if so,
1275 replace the "+" with the zone at "ac_tail", changing the name
1276 to a ".zkey". The name is allocated on the 'scratch' array,
1277 so we can free it afterwards. */
1278 switch (rd[i].record_type)
1280 case GNUNET_DNSPARSER_TYPE_CNAME:
1285 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1288 if ( (NULL == cname) ||
1289 (off != rd[i].data_size) )
1291 GNUNET_break_op (0); /* record not well-formed */
1295 cname = translate_dot_plus (rh, cname);
1296 GNUNET_break (NULL != cname);
1297 scratch_start = scratch_off;
1299 GNUNET_DNSPARSER_builder_add_name (scratch,
1308 rd_new[rd_off].data = &scratch[scratch_start];
1309 rd_new[rd_off].data_size = scratch_off - scratch_start;
1313 GNUNET_free_non_null (cname);
1316 case GNUNET_DNSPARSER_TYPE_SOA:
1318 struct GNUNET_DNSPARSER_SoaRecord *soa;
1321 soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
1324 if ( (NULL == soa) ||
1325 (off != rd[i].data_size) )
1327 GNUNET_break_op (0); /* record not well-formed */
1331 soa->mname = translate_dot_plus (rh, soa->mname);
1332 soa->rname = translate_dot_plus (rh, soa->rname);
1333 scratch_start = scratch_off;
1335 GNUNET_DNSPARSER_builder_add_soa (scratch,
1344 rd_new[rd_off].data = &scratch[scratch_start];
1345 rd_new[rd_off].data_size = scratch_off - scratch_start;
1350 GNUNET_DNSPARSER_free_soa (soa);
1353 case GNUNET_DNSPARSER_TYPE_MX:
1355 struct GNUNET_DNSPARSER_MxRecord *mx;
1358 mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
1361 if ( (NULL == mx) ||
1362 (off != rd[i].data_size) )
1364 GNUNET_break_op (0); /* record not well-formed */
1368 mx->mxhost = translate_dot_plus (rh, mx->mxhost);
1369 scratch_start = scratch_off;
1371 GNUNET_DNSPARSER_builder_add_mx (scratch,
1380 rd_new[rd_off].data = &scratch[scratch_start];
1381 rd_new[rd_off].data_size = scratch_off - scratch_start;
1386 GNUNET_DNSPARSER_free_mx (mx);
1389 case GNUNET_DNSPARSER_TYPE_SRV:
1391 struct GNUNET_DNSPARSER_SrvRecord *srv;
1394 /* FIXME: passing rh->name here is is not necessarily what we want
1395 (SRV support not finished) */
1396 srv = GNUNET_DNSPARSER_parse_srv (rh->name,
1400 if ( (NULL == srv) ||
1401 (off != rd[i].data_size) )
1403 GNUNET_break_op (0); /* record not well-formed */
1407 srv->domain_name = translate_dot_plus (rh, srv->domain_name);
1408 srv->target = translate_dot_plus (rh, srv->target);
1409 scratch_start = scratch_off;
1411 GNUNET_DNSPARSER_builder_add_srv (scratch,
1420 rd_new[rd_off].data = &scratch[scratch_start];
1421 rd_new[rd_off].data_size = scratch_off - scratch_start;
1426 GNUNET_DNSPARSER_free_srv (srv);
1429 case GNUNET_GNSRECORD_TYPE_PKEY:
1431 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
1433 if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
1435 GNUNET_break_op (0);
1438 memcpy (&pub, rd[i].data, rd[i].data_size);
1440 /* tigger shortening */
1441 if (NULL != rh->shorten_key)
1443 GNS_shorten_start (rh->ac_tail->label,
1448 if (GNUNET_GNSRECORD_TYPE_PKEY != rh->record_type)
1450 /* try to resolve "+" */
1451 struct AuthorityChain *ac;
1453 ac = GNUNET_new (struct AuthorityChain);
1455 ac->gns_authority = GNUNET_YES;
1456 ac->authority_info.gns_authority = pub;
1457 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
1458 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1461 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1473 /* yes, we are done, return result */
1474 rh->proc (rh->proc_cls, rd_off, rd_new);
1475 GNS_resolver_lookup_cancel (rh);
1479 /* need to recurse, check if we can */
1480 for (i=0;i<rd_count;i++)
1482 switch (rd[i].record_type)
1484 case GNUNET_GNSRECORD_TYPE_PKEY:
1485 /* delegation to another zone */
1486 if (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
1489 GNUNET_break_op (0);
1490 rh->proc (rh->proc_cls, 0, NULL);
1491 GNS_resolver_lookup_cancel (rh);
1494 /* expand authority chain */
1495 ac = GNUNET_new (struct AuthorityChain);
1497 ac->gns_authority = GNUNET_YES;
1498 memcpy (&ac->authority_info.gns_authority,
1500 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1501 ac->label = resolver_lookup_get_next_label (rh);
1502 /* tigger shortening */
1503 if (NULL != rh->shorten_key)
1504 GNS_shorten_start (rh->ac_tail->label,
1505 &ac->authority_info.gns_authority,
1507 /* add AC to tail */
1508 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1512 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1515 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1518 /* resolution continues within DNS */
1519 if (GNUNET_DNSPARSER_MAX_NAME_LENGTH < rd[i].data_size)
1521 GNUNET_break_op (0);
1522 rh->proc (rh->proc_cls, 0, NULL);
1523 GNS_resolver_lookup_cancel (rh);
1526 /* find associated A/AAAA record */
1529 for (j=0;j<rd_count;j++)
1531 switch (rd[j].record_type)
1533 case GNUNET_DNSPARSER_TYPE_A:
1534 if (sizeof (struct in_addr) != rd[j].data_size)
1536 GNUNET_break_op (0);
1537 rh->proc (rh->proc_cls, 0, NULL);
1538 GNS_resolver_lookup_cancel (rh);
1541 /* FIXME: might want to check if we support IPv4 here,
1542 and otherwise skip this one and hope we find another */
1543 memset (&v4, 0, sizeof (v4));
1544 sa_len = sizeof (v4);
1545 v4.sin_family = AF_INET;
1546 v4.sin_port = htons (53);
1547 #if HAVE_SOCKADDR_IN_SIN_LEN
1548 v4.sin_len = (u_char) sa_len;
1550 memcpy (&v4.sin_addr,
1552 sizeof (struct in_addr));
1553 sa = (struct sockaddr *) &v4;
1555 case GNUNET_DNSPARSER_TYPE_AAAA:
1556 if (sizeof (struct in6_addr) != rd[j].data_size)
1558 GNUNET_break_op (0);
1559 rh->proc (rh->proc_cls, 0, NULL);
1560 GNS_resolver_lookup_cancel (rh);
1563 /* FIXME: might want to check if we support IPv6 here,
1564 and otherwise skip this one and hope we find another */
1565 memset (&v6, 0, sizeof (v6));
1566 sa_len = sizeof (v6);
1567 v6.sin6_family = AF_INET6;
1568 v6.sin6_port = htons (53);
1569 #if HAVE_SOCKADDR_IN_SIN_LEN
1570 v6.sin6_len = (u_char) sa_len;
1572 memcpy (&v6.sin6_addr,
1574 sizeof (struct in6_addr));
1575 sa = (struct sockaddr *) &v6;
1585 /* we cannot continue; NS without A/AAAA */
1586 rh->proc (rh->proc_cls, 0, NULL);
1587 GNS_resolver_lookup_cancel (rh);
1590 /* expand authority chain */
1591 ac = GNUNET_new (struct AuthorityChain);
1594 ns = GNUNET_DNSPARSER_parse_name (rd[i].data,
1597 if ( (NULL == ns) ||
1598 (off != rd[i].data_size) )
1600 GNUNET_break_op (0); /* record not well-formed */
1601 rh->proc (rh->proc_cls, 0, NULL);
1602 GNS_resolver_lookup_cancel (rh);
1603 GNUNET_free_non_null (ns);
1607 strcpy (ac->authority_info.dns_authority.name,
1609 memcpy (&ac->authority_info.dns_authority.dns_ip,
1612 /* for DNS recursion, the label is the full DNS name,
1613 created from the remainder of the GNS name and the
1614 name in the NS record */
1615 GNUNET_asprintf (&ac->label,
1617 (int) rh->name_resolution_pos,
1619 (0 != rh->name_resolution_pos) ? "." : "",
1622 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1625 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1627 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1628 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1630 rh->proc (rh->proc_cls, 0, NULL);
1631 GNS_resolver_lookup_cancel (rh);
1635 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1639 case GNUNET_DNSPARSER_TYPE_CNAME:
1644 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1647 if ( (NULL == cname) ||
1648 (off != rd[i].data_size) )
1650 GNUNET_break_op (0); /* record not well-formed */
1651 rh->proc (rh->proc_cls, 0, NULL);
1652 GNS_resolver_lookup_cancel (rh);
1653 GNUNET_free_non_null (cname);
1656 handle_gns_cname_result (rh,
1658 GNUNET_free (cname);
1661 /* FIXME: handle DNAME */
1667 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1668 _("GNS lookup recursion failed (no delegation record found)\n"));
1669 rh->proc (rh->proc_cls, 0, NULL);
1670 GNS_resolver_lookup_cancel (rh);
1675 * Function called once the namestore has completed the request for
1678 * @param cls closure with the `struct CacheOps`
1679 * @param success #GNUNET_OK on success
1680 * @param emsg error message
1683 namecache_cache_continuation (void *cls,
1687 struct CacheOps *co = cls;
1689 co->namecache_qe_cache = NULL;
1691 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1692 _("Failed to cache GNS resolution: %s\n"),
1694 GNUNET_CONTAINER_DLL_remove (co_head,
1702 * Iterator called on each result obtained for a DHT
1703 * operation that expects a reply
1705 * @param cls closure with the `struct GNS_ResolverHandle`
1706 * @param exp when will this value expire
1707 * @param key key of the result
1708 * @param get_path peers on reply path (or NULL if not recorded)
1709 * [0] = datastore's first neighbor, [length - 1] = local peer
1710 * @param get_path_length number of entries in @a get_path
1711 * @param put_path peers on the PUT path (or NULL if not recorded)
1712 * [0] = origin, [length - 1] = datastore
1713 * @param put_path_length number of entries in @a put_path
1714 * @param type type of the result
1715 * @param size number of bytes in data
1716 * @param data pointer to the result data
1719 handle_dht_response (void *cls,
1720 struct GNUNET_TIME_Absolute exp,
1721 const struct GNUNET_HashCode *key,
1722 const struct GNUNET_PeerIdentity *get_path,
1723 unsigned int get_path_length,
1724 const struct GNUNET_PeerIdentity *put_path,
1725 unsigned int put_path_length,
1726 enum GNUNET_BLOCK_Type type,
1727 size_t size, const void *data)
1729 struct GNS_ResolverHandle *rh = cls;
1730 struct AuthorityChain *ac = rh->ac_tail;
1731 const struct GNUNET_GNSRECORD_Block *block;
1732 struct CacheOps *co;
1734 GNUNET_DHT_get_stop (rh->get_handle);
1735 rh->get_handle = NULL;
1736 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1737 rh->dht_heap_node = NULL;
1738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1739 "Handling response from the DHT\n");
1740 if (size < sizeof (struct GNUNET_GNSRECORD_Block))
1742 /* how did this pass DHT block validation!? */
1744 rh->proc (rh->proc_cls, 0, NULL);
1745 GNS_resolver_lookup_cancel (rh);
1750 ntohl (block->purpose.size) +
1751 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
1752 sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
1754 /* how did this pass DHT block validation!? */
1756 rh->proc (rh->proc_cls, 0, NULL);
1757 GNS_resolver_lookup_cancel (rh);
1761 GNUNET_GNSRECORD_block_decrypt (block,
1762 &ac->authority_info.gns_authority,
1764 &handle_gns_resolution_result,
1767 GNUNET_break_op (0); /* block was ill-formed */
1768 rh->proc (rh->proc_cls, 0, NULL);
1769 GNS_resolver_lookup_cancel (rh);
1772 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us)
1774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1775 "Received expired block from the DHT, will not cache it.\n");
1778 /* Cache well-formed blocks */
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1780 "Caching response from the DHT in namestore\n");
1781 co = GNUNET_new (struct CacheOps);
1782 co->namecache_qe_cache = GNUNET_NAMECACHE_block_cache (namecache_handle,
1784 &namecache_cache_continuation,
1786 GNUNET_CONTAINER_DLL_insert (co_head,
1793 * Process a record that was stored in the namestore.
1795 * @param cls closure with the `struct GNS_ResolverHandle`
1796 * @param block block that was stored in the namestore
1799 handle_namestore_block_response (void *cls,
1800 const struct GNUNET_GNSRECORD_Block *block)
1802 struct GNS_ResolverHandle *rh = cls;
1803 struct GNS_ResolverHandle *rx;
1804 struct AuthorityChain *ac = rh->ac_tail;
1805 const char *label = ac->label;
1806 const struct GNUNET_CRYPTO_EcdsaPublicKey *auth = &ac->authority_info.gns_authority;
1807 struct GNUNET_HashCode query;
1809 GNUNET_GNSRECORD_query_from_public_key (auth,
1812 GNUNET_assert (NULL != rh->namecache_qe);
1813 rh->namecache_qe = NULL;
1814 if ( (GNUNET_NO == rh->only_cached) &&
1815 ( (NULL == block) ||
1816 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
1818 /* Namestore knows nothing; try DHT lookup */
1819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1820 "Starting DHT lookup for `%s' in zone %s\n",
1822 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
1823 GNUNET_assert (NULL == rh->get_handle);
1824 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1825 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1827 DHT_GNS_REPLICATION_LEVEL,
1828 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1830 &handle_dht_response, rh);
1831 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1833 GNUNET_TIME_absolute_get ().abs_value_us);
1834 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
1836 /* fail longest-standing DHT request */
1837 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
1838 GNUNET_assert (NULL != rx);
1839 rx->proc (rx->proc_cls, 0, NULL);
1840 GNS_resolver_lookup_cancel (rx);
1844 if ( (NULL == block) ||
1845 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
1847 /* DHT not permitted and no local result, fail */
1848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1849 "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
1851 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
1852 rh->proc (rh->proc_cls, 0, NULL);
1853 GNS_resolver_lookup_cancel (rh);
1857 GNUNET_GNSRECORD_block_decrypt (block,
1860 &handle_gns_resolution_result,
1863 GNUNET_break_op (0); /* block was ill-formed */
1864 rh->proc (rh->proc_cls, 0, NULL);
1865 GNS_resolver_lookup_cancel (rh);
1872 * Lookup tail of our authority chain in the namestore.
1874 * @param rh query we are processing
1877 recursive_gns_resolution_namestore (struct GNS_ResolverHandle *rh)
1879 struct AuthorityChain *ac = rh->ac_tail;
1880 struct GNUNET_HashCode query;
1882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1883 "Starting GNS resolution for `%s' in zone %s\n",
1885 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
1886 GNUNET_GNSRECORD_query_from_public_key (&ac->authority_info.gns_authority,
1889 rh->namecache_qe = GNUNET_NAMECACHE_lookup_block (namecache_handle,
1891 &handle_namestore_block_response,
1893 GNUNET_assert (NULL != rh->namecache_qe);
1898 * Task scheduled to continue with the resolution process.
1900 * @param cls the `struct GNS_ResolverHandle` of the resolution
1901 * @param tc task context
1904 recursive_resolution (void *cls,
1905 const struct GNUNET_SCHEDULER_TaskContext *tc)
1907 struct GNS_ResolverHandle *rh = cls;
1909 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
1910 if (MAX_RECURSION < rh->loop_limiter++)
1912 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1913 "Encountered unbounded recursion resolving `%s'\n",
1915 rh->proc (rh->proc_cls, 0, NULL);
1916 GNS_resolver_lookup_cancel (rh);
1919 if (GNUNET_YES == rh->ac_tail->gns_authority)
1920 recursive_gns_resolution_namestore (rh);
1922 recursive_dns_resolution (rh);
1927 * Begin the resolution process from 'name', starting with
1928 * the identification of the zone specified by 'name'.
1930 * @param rh resolution to perform
1933 start_resolver_lookup (struct GNS_ResolverHandle *rh)
1935 struct AuthorityChain *ac;
1940 if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
1941 (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
1942 ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
1943 (GNUNET_YES != is_zkey_tld (rh->name)) ) )
1945 /* use standard DNS lookup */
1948 switch (rh->record_type)
1950 case GNUNET_DNSPARSER_TYPE_A:
1953 case GNUNET_DNSPARSER_TYPE_AAAA:
1960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1961 "Doing standard DNS lookup for `%s'\n",
1963 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
1970 if (is_zkey_tld (rh->name))
1972 /* Name ends with ".zkey", try to replace authority zone with zkey
1974 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
1975 x = resolver_lookup_get_next_label (rh); /* will return 'x' coordinate */
1976 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
1977 GNUNET_asprintf (&pkey,
1983 GNUNET_CRYPTO_ecdsa_public_key_from_string (pkey,
1985 &rh->authority_zone)) )
1987 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1988 _("Hostname `%s' is not well-formed, resolution fails\n"),
1990 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
1992 GNUNET_free_non_null (x);
1993 GNUNET_free_non_null (y);
1998 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
1999 GNUNET_free (resolver_lookup_get_next_label (rh));
2001 ac = GNUNET_new (struct AuthorityChain);
2003 ac->label = resolver_lookup_get_next_label (rh);
2004 if (NULL == ac->label)
2005 /* name was just "gnu", so we default to label '+' */
2006 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
2007 ac->gns_authority = GNUNET_YES;
2008 ac->authority_info.gns_authority = rh->authority_zone;
2009 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
2012 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
2018 * Lookup of a record in a specific zone calls lookup result processor
2021 * @param zone the zone to perform the lookup in
2022 * @param record_type the record type to look up
2023 * @param name the name to look up
2024 * @param shorten_key a private key for use with PSEU import (can be NULL)
2025 * @param only_cached #GNUNET_NO to only check locally not DHT for performance
2026 * @param proc the processor to call on result
2027 * @param proc_cls the closure to pass to @a proc
2028 * @return handle to cancel operation
2030 struct GNS_ResolverHandle *
2031 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
2032 uint32_t record_type,
2034 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key,
2036 GNS_ResultProcessor proc, void *proc_cls)
2038 struct GNS_ResolverHandle *rh;
2040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2041 (NULL == shorten_key)
2042 ? "Starting lookup for `%s' with shortening disabled\n"
2043 : "Starting lookup for `%s' with shortening enabled\n",
2045 rh = GNUNET_new (struct GNS_ResolverHandle);
2046 GNUNET_CONTAINER_DLL_insert (rlh_head,
2049 rh->authority_zone = *zone;
2051 rh->proc_cls = proc_cls;
2052 rh->only_cached = only_cached;
2053 rh->record_type = record_type;
2054 rh->name = GNUNET_strdup (name);
2055 rh->name_resolution_pos = strlen (name);
2056 if (NULL != shorten_key)
2058 rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
2059 *rh->shorten_key = *shorten_key;
2061 start_resolver_lookup (rh);
2067 * Cancel active resolution (i.e. client disconnected).
2069 * @param rh resolution to abort
2072 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
2074 struct DnsResult *dr;
2075 struct AuthorityChain *ac;
2076 struct VpnContext *vpn_ctx;
2078 GNUNET_CONTAINER_DLL_remove (rlh_head,
2081 while (NULL != (ac = rh->ac_head))
2083 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
2086 GNUNET_free (ac->label);
2089 if (GNUNET_SCHEDULER_NO_TASK != rh->task_id)
2091 GNUNET_SCHEDULER_cancel (rh->task_id);
2092 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
2094 if (NULL != rh->get_handle)
2096 GNUNET_DHT_get_stop (rh->get_handle);
2097 rh->get_handle = NULL;
2099 if (NULL != rh->dht_heap_node)
2101 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
2102 rh->dht_heap_node = NULL;
2104 if (NULL != (vpn_ctx = rh->vpn_ctx))
2106 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
2107 GNUNET_free (vpn_ctx->rd_data);
2108 GNUNET_free (vpn_ctx);
2110 if (NULL != rh->dns_request)
2112 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
2113 rh->dns_request = NULL;
2115 if (NULL != rh->namecache_qe)
2117 GNUNET_NAMECACHE_cancel (rh->namecache_qe);
2118 rh->namecache_qe = NULL;
2120 if (NULL != rh->std_resolve)
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2123 "Canceling standard DNS resolution\n");
2124 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
2125 rh->std_resolve = NULL;
2127 while (NULL != (dr = rh->dns_result_head))
2129 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
2130 rh->dns_result_tail,
2134 GNUNET_free_non_null (rh->shorten_key);
2135 GNUNET_free (rh->name);
2140 /* ***************** Resolver initialization ********************* */
2144 * Initialize the resolver
2146 * @param nh the namestore handle
2147 * @param nc the namecache handle
2148 * @param dht the dht handle
2149 * @param c configuration handle
2150 * @param max_bg_queries maximum number of parallel background queries in dht
2153 GNS_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
2154 struct GNUNET_NAMECACHE_Handle *nc,
2155 struct GNUNET_DHT_Handle *dht,
2156 const struct GNUNET_CONFIGURATION_Handle *c,
2157 unsigned long long max_bg_queries)
2162 namecache_handle = nc;
2163 namestore_handle = nh;
2166 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2167 max_allowed_background_queries = max_bg_queries;
2169 GNUNET_CONFIGURATION_get_value_string (c,
2174 /* user did not specify DNS resolver, use 8.8.8.8 */
2175 dns_ip = GNUNET_strdup ("8.8.8.8");
2177 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
2178 GNUNET_free (dns_ip);
2179 vpn_handle = GNUNET_VPN_connect (cfg);
2187 GNS_resolver_done ()
2189 struct GNS_ResolverHandle *rh;
2190 struct CacheOps *co;
2192 /* abort active resolutions */
2193 while (NULL != (rh = rlh_head))
2195 rh->proc (rh->proc_cls, 0, NULL);
2196 GNS_resolver_lookup_cancel (rh);
2198 while (NULL != (co = co_head))
2200 GNUNET_CONTAINER_DLL_remove (co_head,
2203 GNUNET_NAMECACHE_cancel (co->namecache_qe_cache);
2206 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
2207 dht_lookup_heap = NULL;
2208 GNUNET_DNSSTUB_stop (dns_handle);
2210 GNUNET_VPN_disconnect (vpn_handle);
2213 namecache_handle = NULL;
2214 namestore_handle = NULL;
2218 /* *************** common helper functions (do not really belong here) *********** */
2221 * Checks if @a name ends in ".TLD"
2223 * @param name the name to check
2224 * @param tld the TLD to check for
2225 * @return GNUNET_YES or GNUNET_NO
2228 is_tld (const char* name, const char* tld)
2232 if (strlen (name) <= strlen (tld))
2234 offset = strlen (name) - strlen (tld);
2235 if (0 != strcmp (name + offset, tld))
2241 /* end of gnunet-service-gns_resolver.c */