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 CORE-level broadcasts!), (#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_namestore_service.h"
38 #include "gnunet_dns_service.h"
39 #include "gnunet_resolver_service.h"
40 #include "gnunet_dnsparser_lib.h"
41 #include "gnunet_gns_service.h"
43 #include "gnunet-service-gns_resolver.h"
44 #include "gnunet-service-gns_shorten.h"
45 #include "gnunet_vpn_service.h"
49 * Default DHT timeout for lookups.
51 #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
54 * Default timeout for DNS lookups.
56 #define DNS_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
59 * Default timeout for VPN redirections.
61 #define VPN_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
64 * DHT replication level
66 #define DHT_GNS_REPLICATION_LEVEL 5
69 * How deep do we allow recursions to go before we abort?
71 #define MAX_RECURSION 256
75 * DLL to hold the authority chain we had to pass in the resolution
83 struct AuthorityChain *prev;
88 struct AuthorityChain *next;
91 * Resolver handle this entry in the chain belongs to.
93 struct GNS_ResolverHandle *rh;
96 * label/name corresponding to the authority
101 * #GNUNET_YES if the authority was a GNS authority,
102 * #GNUNET_NO if the authority was a DNS authority.
107 * Information about the resolver authority for this label.
113 * The zone of the GNS authority
115 struct GNUNET_CRYPTO_EccPublicSignKey gns_authority;
120 * Domain of the DNS resolver that is the authority.
121 * (appended to construct the DNS name to resolve;
122 * this is NOT the DNS name of the DNS server!).
124 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
127 * IP address of the DNS resolver that is authoritative.
128 * (this implementation currently only supports one
131 struct sockaddr_storage dns_ip;
141 * A result we got from DNS.
149 struct DnsResult *next;
154 struct DnsResult *prev;
157 * Binary value stored in the DNS record (appended to this struct)
162 * Expiration time for the DNS record, 0 if we didn't
163 * get anything useful (i.e. 'gethostbyname' was used).
165 uint64_t expiration_time;
168 * Number of bytes in 'data'.
173 * Type of the GNS/DNS record.
175 uint32_t record_type;
181 * Closure for #vpn_allocation_cb.
187 * Which resolution process are we processing.
189 struct GNS_ResolverHandle *rh;
192 * Handle to the VPN request that we were performing.
194 struct GNUNET_VPN_RedirectionRequest *vpn_request;
197 * Number of records serialized in 'rd_data'.
199 unsigned int rd_count;
202 * Serialized records.
207 * Number of bytes in 'rd_data'.
214 * Handle to a currenty pending resolution. On result (positive or
215 * negative) the #GNS_ResultProcessor is called.
217 struct GNS_ResolverHandle
223 struct GNS_ResolverHandle *next;
228 struct GNS_ResolverHandle *prev;
231 * The top-level GNS authoritative zone to query
233 struct GNUNET_CRYPTO_EccPublicSignKey authority_zone;
236 * called when resolution phase finishes
238 GNS_ResultProcessor proc;
241 * closure passed to proc
246 * Handle for DHT lookups. should be NULL if no lookups are in progress
248 struct GNUNET_DHT_GetHandle *get_handle;
251 * Handle to a VPN request, NULL if none is active.
253 struct VpnContext *vpn_ctx;
256 * Socket for a DNS request, NULL if none is active.
258 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
261 * Handle for standard DNS resolution, NULL if none is active.
263 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
266 * Pending Namestore lookup task
268 struct GNUNET_NAMESTORE_QueueEntry *namestore_qe;
271 * Heap node associated with this lookup. Used to limit number of
272 * concurrent requests.
274 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
277 * DLL to store the authority chain
279 struct AuthorityChain *ac_head;
282 * DLL to store the authority chain
284 struct AuthorityChain *ac_tail;
287 * Private key of the shorten zone, NULL to not shorten.
289 struct GNUNET_CRYPTO_EccPrivateKey *shorten_key;
292 * ID of a task associated with the resolution process.
294 GNUNET_SCHEDULER_TaskIdentifier task_id;
297 * The name to resolve
302 * DLL of results we got from DNS.
304 struct DnsResult *dns_result_head;
307 * DLL of results we got from DNS.
309 struct DnsResult *dns_result_tail;
312 * Current offset in 'name' where we are resolving.
314 size_t name_resolution_pos;
322 * Desired type for the resolution.
327 * We increment the loop limiter for each step in a recursive
328 * resolution. If it passes our threshold (i.e. due to
329 * self-recursion in the resolution, i.e CNAME fun), we stop.
331 unsigned int loop_limiter;
337 * Active namestore caching operations.
343 * Organized in a DLL.
345 struct CacheOps *next;
348 * Organized in a DLL.
350 struct CacheOps *prev;
353 * Pending Namestore caching task.
355 struct GNUNET_NAMESTORE_QueueEntry *namestore_qe_cache;
361 * Our handle to the namestore service
363 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
366 * Our handle to the vpn service
368 static struct GNUNET_VPN_Handle *vpn_handle;
371 * Resolver handle to the dht
373 static struct GNUNET_DHT_Handle *dht_handle;
376 * Handle to perform DNS lookups.
378 static struct GNUNET_DNSSTUB_Context *dns_handle;
381 * Heap for limiting parallel DHT lookups
383 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
386 * Maximum amount of parallel queries to the DHT
388 static unsigned long long max_allowed_background_queries;
391 * Head of resolver lookup list
393 static struct GNS_ResolverHandle *rlh_head;
396 * Tail of resolver lookup list
398 static struct GNS_ResolverHandle *rlh_tail;
401 * Organized in a DLL.
403 static struct CacheOps *co_head;
406 * Organized in a DLL.
408 static struct CacheOps *co_tail;
412 * Global configuration.
414 static const struct GNUNET_CONFIGURATION_Handle *cfg;
418 * Check if name is in srv format (_x._y.xxx)
421 * @return #GNUNET_YES if true
424 is_srv (const char *name)
431 if (NULL == strstr (name, "._"))
434 ndup = GNUNET_strdup (name);
436 if (NULL == strtok (NULL, "."))
438 if (NULL == strtok (NULL, "."))
440 if (NULL != strtok (NULL, "."))
448 * Determine if this name is canonical (is a legal name in a zone, without delegation);
449 * note that we do not test that the name does not contain illegal characters, we only
450 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
451 * even though they consist of multiple labels.
454 * a.b.gnu = not canonical
456 * _foo._srv = canonical
457 * _f.bar = not canonical
459 * @param name the name to test
460 * @return #GNUNET_YES if canonical
463 is_canonical (const char *name)
468 if (NULL == strchr (name, '.'))
473 while (NULL != (dot = strchr (pos, '.')))
481 /* ************************** Resolution **************************** */
484 * Exands a name ending in .+ with the zone of origin.
486 * @param rh resolution context
487 * @param name name to modify (to be free'd or returned)
488 * @return updated name
491 translate_dot_plus (struct GNS_ResolverHandle *rh,
495 size_t s_len = strlen (name);
497 if (0 != strcmp (&name[s_len - 2],
499 return name; /* did not end in ".+" */
500 GNUNET_assert (GNUNET_YES == rh->ac_tail->gns_authority);
501 GNUNET_asprintf (&ret,
505 GNUNET_NAMESTORE_pkey_to_zkey (&rh->ac_tail->authority_info.gns_authority));
512 * Task scheduled to asynchronously fail a resolution.
514 * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
515 * @param tc task context
518 fail_resolution (void *cls,
519 const struct GNUNET_SCHEDULER_TaskContext *tc)
521 struct GNS_ResolverHandle *rh = cls;
523 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
524 rh->proc (rh->proc_cls, 0, NULL);
525 GNS_resolver_lookup_cancel (rh);
529 #if (defined WINDOWS) || (defined DARWIN)
530 /* Don't have this on W32, here's a naive implementation
531 * Was somehow removed on OS X ... */
533 memrchr (const void *s,
537 const unsigned char *ucs = s;
540 for (i = n - 1; i >= 0; i--)
541 if (c == (int) ucs[i])
542 return (void *) &ucs[i];
549 * Get the next, rightmost label from the name that we are trying to resolve,
550 * and update the resolution position accordingly.
552 * @param rh handle to the resolution operation to get the next label from
553 * @return NULL if there are no more labels
556 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
562 if (0 == rh->name_resolution_pos)
564 dot = memrchr (rh->name, (int) '.', rh->name_resolution_pos);
567 /* done, this was the last one */
568 len = rh->name_resolution_pos;
570 rh->name_resolution_pos = 0;
574 /* advance by one label */
575 len = rh->name_resolution_pos - (dot - rh->name) - 1;
577 rh->name_resolution_pos = dot - rh->name;
579 return GNUNET_strndup (rp, len);
584 * Gives the cummulative result obtained to the callback and clean up the request.
586 * @param rh resolution process that has culminated in a result
589 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
591 struct DnsResult *pos;
596 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
599 struct GNUNET_NAMESTORE_RecordData rd[n];
602 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
604 rd[i].data = pos->data;
605 rd[i].data_size = pos->data_size;
606 rd[i].record_type = pos->record_type;
607 if (0 == pos->expiration_time)
609 rd[i].flags = GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
610 rd[i].expiration_time = 0;
614 rd[i].flags = GNUNET_NAMESTORE_RF_NONE;
615 rd[i].expiration_time = pos->expiration_time;
619 GNUNET_assert (i == n);
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "Transmitting standard DNS result with %u records\n",
623 rh->proc (rh->proc_cls,
627 GNS_resolver_lookup_cancel (rh);
632 * Add a result from DNS to the records to be returned to the application.
634 * @param rh resolution request to extend with a result
635 * @param expiration_time expiration time for the answer
636 * @param record_type DNS record type of the answer
637 * @param data_size number of bytes in @a data
638 * @param data binary data to return in DNS record
641 add_dns_result (struct GNS_ResolverHandle *rh,
642 uint64_t expiration_time,
643 uint32_t record_type,
647 struct DnsResult *res;
649 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
650 res->expiration_time = expiration_time;
651 res->data_size = data_size;
652 res->record_type = record_type;
654 memcpy (&res[1], data, data_size);
655 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
662 * We had to do a DNS lookup. Convert the result (if any) and return
665 * @param cls closure with the 'struct GNS_ResolverHandle'
666 * @param addr one of the addresses of the host, NULL for the last address
667 * @param addrlen length of the address
670 handle_dns_result (void *cls,
671 const struct sockaddr *addr,
674 struct GNS_ResolverHandle *rh = cls;
675 const struct sockaddr_in *sa4;
676 const struct sockaddr_in6 *sa6;
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
679 "Received %u bytes of DNS IP data\n",
683 rh->std_resolve = NULL;
684 transmit_lookup_dns_result (rh);
687 switch (addr->sa_family)
690 sa4 = (const struct sockaddr_in *) addr;
692 0 /* expiration time is unknown */,
693 GNUNET_DNSPARSER_TYPE_A,
694 sizeof (struct in_addr),
698 sa6 = (const struct sockaddr_in6 *) addr;
700 0 /* expiration time is unknown */,
701 GNUNET_DNSPARSER_TYPE_AAAA,
702 sizeof (struct in6_addr),
713 * Task scheduled to continue with the resolution process.
715 * @param cls the 'struct GNS_ResolverHandle' of the resolution
716 * @param tc task context
719 recursive_resolution (void *cls,
720 const struct GNUNET_SCHEDULER_TaskContext *tc);
724 * Begin the resolution process from 'name', starting with
725 * the identification of the zone specified by 'name'.
727 * @param rh resolution to perform
730 start_resolver_lookup (struct GNS_ResolverHandle *rh);
734 * Function called with the result of a DNS resolution.
736 * @param cls the request handle of the resolution that
737 * we were attempting to make
738 * @param rs socket that received the response
739 * @param dns dns response, never NULL
740 * @param dns_len number of bytes in @a dns
743 dns_result_parser (void *cls,
744 struct GNUNET_DNSSTUB_RequestSocket *rs,
745 const struct GNUNET_TUN_DnsHeader *dns,
748 struct GNS_ResolverHandle *rh = cls;
749 struct GNUNET_DNSPARSER_Packet *p;
750 const struct GNUNET_DNSPARSER_Record *rec;
751 unsigned int rd_count;
754 rh->dns_request = NULL;
755 GNUNET_SCHEDULER_cancel (rh->task_id);
756 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
757 p = GNUNET_DNSPARSER_parse ((const char *) dns,
761 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
762 _("Failed to parse DNS response\n"));
763 rh->proc (rh->proc_cls, 0, NULL);
764 GNS_resolver_lookup_cancel (rh);
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "Received DNS response for `%s' with %u answers\n",
770 (unsigned int) p->num_answers);
771 if ( (p->num_answers > 0) &&
772 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
773 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
775 GNUNET_free (rh->name);
776 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
777 start_resolver_lookup (rh);
778 GNUNET_DNSPARSER_free_packet (p);
781 /* FIXME: add DNAME support */
783 /* convert from (parsed) DNS to (binary) GNS format! */
784 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
786 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
788 char buf[UINT16_MAX];
794 memset (rd, 0, sizeof (rd));
795 for (i=0;i<rd_count;i++)
797 if (i < p->num_answers)
798 rec = &p->answers[i];
799 else if (i < p->num_answers + p->num_authority_records)
800 rec = &p->authority_records[i - p->num_answers];
802 rec = &p->authority_records[i - p->num_answers - p->num_authority_records];
803 /* As we copied the full DNS name to 'rh->ac_tail->label', this
804 should be the correct check to see if this record is actually
805 a record for our label... */
806 if (0 != strcmp (rec->name,
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Dropping record `%s', does not match desired name `%s'\n",
816 rd[i - skip].record_type = rec->type;
817 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
820 case GNUNET_DNSPARSER_TYPE_A:
821 if (rec->data.raw.data_len != sizeof (struct in_addr))
827 rd[i - skip].data_size = rec->data.raw.data_len;
828 rd[i - skip].data = rec->data.raw.data;
830 case GNUNET_DNSPARSER_TYPE_AAAA:
831 if (rec->data.raw.data_len != sizeof (struct in6_addr))
837 rd[i - skip].data_size = rec->data.raw.data_len;
838 rd[i - skip].data = rec->data.raw.data;
840 case GNUNET_DNSPARSER_TYPE_CNAME:
841 case GNUNET_DNSPARSER_TYPE_PTR:
842 case GNUNET_DNSPARSER_TYPE_NS:
845 GNUNET_DNSPARSER_builder_add_name (buf,
854 rd[i - skip].data_size = buf_off - buf_start;
855 rd[i - skip].data = &buf[buf_start];
857 case GNUNET_DNSPARSER_TYPE_SOA:
860 GNUNET_DNSPARSER_builder_add_soa (buf,
869 rd[i - skip].data_size = buf_off - buf_start;
870 rd[i - skip].data = &buf[buf_start];
872 case GNUNET_DNSPARSER_TYPE_MX:
875 GNUNET_DNSPARSER_builder_add_mx (buf,
884 rd[i - skip].data_size = buf_off - buf_start;
885 rd[i - skip].data = &buf[buf_start];
887 case GNUNET_DNSPARSER_TYPE_SRV:
890 GNUNET_DNSPARSER_builder_add_srv (buf,
899 rd[i - skip].data_size = buf_off - buf_start;
900 rd[i - skip].data = &buf[buf_start];
903 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
904 _("Skipping record of unsupported type %d\n"),
910 rh->proc (rh->proc_cls, rd_count - skip, rd);
911 GNS_resolver_lookup_cancel (rh);
913 GNUNET_DNSPARSER_free_packet (p);
918 * Perform recursive DNS resolution. Asks the given DNS resolver to
919 * resolve "rh->dns_name", possibly recursively proceeding following
920 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
921 * we find the answer.
923 * @param rh resolution information
926 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
928 struct AuthorityChain *ac;
930 struct GNUNET_DNSPARSER_Query *query;
931 struct GNUNET_DNSPARSER_Packet *p;
933 size_t dns_request_length;
936 GNUNET_assert (NULL != ac);
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 "Starting DNS lookup for `%s'\n",
940 GNUNET_assert (GNUNET_NO == ac->gns_authority);
941 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
944 sa_len = sizeof (struct sockaddr_in);
947 sa_len = sizeof (struct sockaddr_in6);
951 rh->proc (rh->proc_cls, 0, NULL);
952 GNS_resolver_lookup_cancel (rh);
955 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
956 query->name = GNUNET_strdup (ac->label);
957 query->type = rh->record_type;
958 query->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
959 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
962 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
964 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
965 p->flags.recursion_desired = 1;
967 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
970 rh->proc (rh->proc_cls, 0, NULL);
971 GNS_resolver_lookup_cancel (rh);
975 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
976 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
982 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
986 GNUNET_free (dns_request);
987 GNUNET_DNSPARSER_free_packet (p);
992 * We encountered a CNAME record during our resolution.
993 * Merge it into our chain.
995 * @param rh resolution we are performing
996 * @param cname value of the cname record we got for the current
997 * authority chain tail
1000 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
1005 struct AuthorityChain *ac;
1007 nlen = strlen (cname);
1010 &cname[nlen - 2])) )
1012 /* CNAME resolution continues relative to current domain */
1013 if (0 == rh->name_resolution_pos)
1015 res = GNUNET_strndup (cname, nlen - 2);
1016 rh->name_resolution_pos = nlen - 2;
1020 GNUNET_asprintf (&res,
1022 (int) rh->name_resolution_pos,
1026 rh->name_resolution_pos = strlen (res);
1028 GNUNET_free (rh->name);
1030 ac = GNUNET_new (struct AuthorityChain);
1032 ac->gns_authority = GNUNET_YES;
1033 ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
1034 ac->label = resolver_lookup_get_next_label (rh);
1035 /* tigger shortening */
1036 if (NULL != rh->shorten_key)
1037 GNS_shorten_start (rh->ac_tail->label,
1038 &ac->authority_info.gns_authority,
1040 /* add AC to tail */
1041 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1044 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1048 /* name is absolute, start from the beginning */
1049 GNUNET_free (rh->name);
1050 rh->name = GNUNET_strdup (cname);
1051 start_resolver_lookup (rh);
1056 * Process a records that were decrypted from a block.
1058 * @param cls closure with the 'struct GNS_ResolverHandle'
1059 * @param rd_count number of entries in @a rd array
1060 * @param rd array of records with data to store
1063 handle_gns_resolution_result (void *cls,
1064 unsigned int rd_count,
1065 const struct GNUNET_NAMESTORE_RecordData *rd);
1069 * Callback invoked from the VPN service once a redirection is
1070 * available. Provides the IP address that can now be used to
1071 * reach the requested destination. Replaces the "VPN" record
1072 * with the respective A/AAAA record and continues processing.
1074 * @param cls closure
1075 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1076 * will match 'result_af' from the request
1077 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1078 * that the VPN allocated for the redirection;
1079 * traffic to this IP will now be redirected to the
1080 * specified target peer; NULL on error
1083 vpn_allocation_cb (void *cls,
1085 const void *address)
1087 struct VpnContext *vpn_ctx = cls;
1088 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1089 struct GNUNET_NAMESTORE_RecordData rd[vpn_ctx->rd_count];
1092 vpn_ctx->vpn_request = NULL;
1094 GNUNET_assert (GNUNET_OK ==
1095 GNUNET_NAMESTORE_records_deserialize (vpn_ctx->rd_data_size,
1099 for (i=0;i<vpn_ctx->rd_count;i++)
1101 if (GNUNET_NAMESTORE_TYPE_VPN == rd[i].record_type)
1106 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1107 rd[i].data_size = sizeof (struct in_addr);
1108 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1110 rd[i].data = address;
1113 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1114 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1116 rd[i].data = address;
1117 rd[i].data_size = sizeof (struct in6_addr);
1125 GNUNET_assert (i < vpn_ctx->rd_count);
1126 handle_gns_resolution_result (rh,
1129 GNUNET_free (vpn_ctx->rd_data);
1130 GNUNET_free (vpn_ctx);
1135 * Process a records that were decrypted from a block.
1137 * @param cls closure with the `struct GNS_ResolverHandle`
1138 * @param rd_count number of entries in @a rd array
1139 * @param rd array of records with data to store
1142 handle_gns_resolution_result (void *cls,
1143 unsigned int rd_count,
1144 const struct GNUNET_NAMESTORE_RecordData *rd)
1146 struct GNS_ResolverHandle *rh = cls;
1147 struct AuthorityChain *ac;
1150 struct sockaddr *sa;
1151 struct sockaddr_in v4;
1152 struct sockaddr_in6 v6;
1155 struct VpnContext *vpn_ctx;
1156 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1158 struct GNUNET_HashCode vhash;
1160 char scratch[UINT16_MAX];
1162 size_t scratch_start;
1164 struct GNUNET_NAMESTORE_RecordData rd_new[rd_count];
1165 unsigned int rd_off;
1167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1168 "Resolution succeeded for `%s' in zone %s, got %u records\n",
1170 GNUNET_NAMESTORE_z2s (&rh->ac_tail->authority_info.gns_authority),
1172 if (0 == rh->name_resolution_pos)
1174 /* top-level match, are we done yet? */
1175 if ( (rd_count > 0) &&
1176 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1177 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1180 cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
1183 if ( (NULL == cname) ||
1184 (off != rd[0].data_size) )
1186 GNUNET_break_op (0);
1187 rh->proc (rh->proc_cls, 0, NULL);
1188 GNS_resolver_lookup_cancel (rh);
1191 handle_gns_cname_result (rh,
1193 GNUNET_free (cname);
1196 /* If A/AAAA was requested, but we got a VPN
1197 record, we convert it to A/AAAA using GNUnet VPN */
1198 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1199 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1201 for (i=0;i<rd_count;i++)
1203 switch (rd[i].record_type)
1205 case GNUNET_NAMESTORE_TYPE_VPN:
1207 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1208 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) <
1211 GNUNET_break_op (0);
1212 rh->proc (rh->proc_cls, 0, NULL);
1213 GNS_resolver_lookup_cancel (rh);
1216 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1217 vname = (const char *) &vpn[1];
1218 if ('\0' != vname[rd[i].data_size - 1 - 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 GNUNET_CRYPTO_hash (vname,
1226 strlen (vname), // FIXME: +1?
1228 vpn_ctx = GNUNET_new (struct VpnContext);
1229 rh->vpn_ctx = vpn_ctx;
1231 vpn_ctx->rd_data_size = GNUNET_NAMESTORE_records_get_size (rd_count,
1233 vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
1234 (void) GNUNET_NAMESTORE_records_serialize (rd_count,
1236 vpn_ctx->rd_data_size,
1238 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1243 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1248 case GNUNET_NAMESTORE_TYPE_GNS2DNS:
1250 /* delegation to DNS */
1258 /* convert relative names in record values to absolute names,
1259 using 'scratch' array for memory allocations */
1262 for (i=0;i<rd_count;i++)
1264 rd_new[rd_off] = rd[i];
1265 /* Check if the embedded name(s) end in "+", and if so,
1266 replace the "+" with the zone at "ac_tail", changing the name
1267 to a ".zkey". The name is allocated on the 'scratch' array,
1268 so we can free it afterwards. */
1269 switch (rd[i].record_type)
1271 case GNUNET_DNSPARSER_TYPE_CNAME:
1276 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1279 if ( (NULL == cname) ||
1280 (off != rd[i].data_size) )
1282 GNUNET_break_op (0); /* record not well-formed */
1286 cname = translate_dot_plus (rh, cname);
1287 scratch_start = scratch_off;
1289 GNUNET_DNSPARSER_builder_add_name (scratch,
1298 rd_new[rd_off].data = &scratch[scratch_start];
1299 rd_new[rd_off].data_size = scratch_off - scratch_start;
1303 GNUNET_free_non_null (cname);
1306 case GNUNET_DNSPARSER_TYPE_SOA:
1308 struct GNUNET_DNSPARSER_SoaRecord *soa;
1311 soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
1314 if ( (NULL == soa) ||
1315 (off != rd[i].data_size) )
1317 GNUNET_break_op (0); /* record not well-formed */
1321 soa->mname = translate_dot_plus (rh, soa->mname);
1322 soa->rname = translate_dot_plus (rh, soa->rname);
1323 scratch_start = scratch_off;
1325 GNUNET_DNSPARSER_builder_add_soa (scratch,
1334 rd_new[rd_off].data = &scratch[scratch_start];
1335 rd_new[rd_off].data_size = scratch_off - scratch_start;
1340 GNUNET_DNSPARSER_free_soa (soa);
1343 case GNUNET_DNSPARSER_TYPE_MX:
1345 struct GNUNET_DNSPARSER_MxRecord *mx;
1348 mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
1351 if ( (NULL == mx) ||
1352 (off != rd[i].data_size) )
1354 GNUNET_break_op (0); /* record not well-formed */
1358 mx->mxhost = translate_dot_plus (rh, mx->mxhost);
1359 scratch_start = scratch_off;
1361 GNUNET_DNSPARSER_builder_add_mx (scratch,
1370 rd_new[rd_off].data = &scratch[scratch_start];
1371 rd_new[rd_off].data_size = scratch_off - scratch_start;
1376 GNUNET_DNSPARSER_free_mx (mx);
1379 case GNUNET_DNSPARSER_TYPE_SRV:
1381 struct GNUNET_DNSPARSER_SrvRecord *srv;
1384 /* FIXME: passing rh->name here is is not necessarily what we want
1385 (SRV support not finished) */
1386 srv = GNUNET_DNSPARSER_parse_srv (rh->name,
1390 if ( (NULL == srv) ||
1391 (off != rd[i].data_size) )
1393 GNUNET_break_op (0); /* record not well-formed */
1397 srv->domain_name = translate_dot_plus (rh, srv->domain_name);
1398 srv->target = translate_dot_plus (rh, srv->target);
1399 scratch_start = scratch_off;
1401 GNUNET_DNSPARSER_builder_add_srv (scratch,
1410 rd_new[rd_off].data = &scratch[scratch_start];
1411 rd_new[rd_off].data_size = scratch_off - scratch_start;
1416 GNUNET_DNSPARSER_free_srv (srv);
1419 case GNUNET_NAMESTORE_TYPE_PKEY:
1420 /* tigger shortening */
1421 if (NULL != rh->shorten_key)
1423 struct GNUNET_CRYPTO_EccPublicSignKey pub;
1425 if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EccPublicSignKey))
1427 GNUNET_break_op (0);
1430 memcpy (&pub, rd[i].data, rd[i].data_size);
1431 GNS_shorten_start (rh->ac_tail->label,
1443 /* yes, we are done, return result */
1444 rh->proc (rh->proc_cls, rd_off, rd_new);
1445 GNS_resolver_lookup_cancel (rh);
1449 /* need to recurse, check if we can */
1450 for (i=0;i<rd_count;i++)
1452 switch (rd[i].record_type)
1454 case GNUNET_NAMESTORE_TYPE_PKEY:
1455 /* delegation to another zone */
1456 if (sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) !=
1459 GNUNET_break_op (0);
1460 rh->proc (rh->proc_cls, 0, NULL);
1461 GNS_resolver_lookup_cancel (rh);
1464 /* expand authority chain */
1465 ac = GNUNET_new (struct AuthorityChain);
1467 ac->gns_authority = GNUNET_YES;
1468 memcpy (&ac->authority_info.gns_authority,
1470 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey));
1471 ac->label = resolver_lookup_get_next_label (rh);
1472 /* tigger shortening */
1473 if (NULL != rh->shorten_key)
1474 GNS_shorten_start (rh->ac_tail->label,
1475 &ac->authority_info.gns_authority,
1477 /* add AC to tail */
1478 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1482 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1485 case GNUNET_NAMESTORE_TYPE_GNS2DNS:
1488 /* resolution continues within DNS */
1489 if (GNUNET_DNSPARSER_MAX_NAME_LENGTH < rd[i].data_size)
1491 GNUNET_break_op (0);
1492 rh->proc (rh->proc_cls, 0, NULL);
1493 GNS_resolver_lookup_cancel (rh);
1496 /* find associated A/AAAA record */
1499 for (j=0;j<rd_count;j++)
1501 switch (rd[j].record_type)
1503 case GNUNET_DNSPARSER_TYPE_A:
1504 if (sizeof (struct in_addr) != rd[j].data_size)
1506 GNUNET_break_op (0);
1507 rh->proc (rh->proc_cls, 0, NULL);
1508 GNS_resolver_lookup_cancel (rh);
1511 /* FIXME: might want to check if we support IPv4 here,
1512 and otherwise skip this one and hope we find another */
1513 memset (&v4, 0, sizeof (v4));
1514 sa_len = sizeof (v4);
1515 v4.sin_family = AF_INET;
1516 v4.sin_port = htons (53);
1517 #if HAVE_SOCKADDR_IN_SIN_LEN
1518 v4.sin_len = (u_char) sa_len;
1520 memcpy (&v4.sin_addr,
1522 sizeof (struct in_addr));
1523 sa = (struct sockaddr *) &v4;
1525 case GNUNET_DNSPARSER_TYPE_AAAA:
1526 if (sizeof (struct in6_addr) != rd[j].data_size)
1528 GNUNET_break_op (0);
1529 rh->proc (rh->proc_cls, 0, NULL);
1530 GNS_resolver_lookup_cancel (rh);
1533 /* FIXME: might want to check if we support IPv6 here,
1534 and otherwise skip this one and hope we find another */
1535 memset (&v6, 0, sizeof (v6));
1536 sa_len = sizeof (v6);
1537 v6.sin6_family = AF_INET6;
1538 v6.sin6_port = htons (53);
1539 #if HAVE_SOCKADDR_IN_SIN_LEN
1540 v6.sin6_len = (u_char) sa_len;
1542 memcpy (&v6.sin6_addr,
1544 sizeof (struct in6_addr));
1545 sa = (struct sockaddr *) &v6;
1555 /* we cannot continue; NS without A/AAAA */
1556 rh->proc (rh->proc_cls, 0, NULL);
1557 GNS_resolver_lookup_cancel (rh);
1560 /* expand authority chain */
1561 ac = GNUNET_new (struct AuthorityChain);
1564 ns = GNUNET_DNSPARSER_parse_name (rd[i].data,
1567 if ( (NULL == ns) ||
1568 (off != rd[i].data_size) )
1570 GNUNET_break_op (0); /* record not well-formed */
1571 rh->proc (rh->proc_cls, 0, NULL);
1572 GNS_resolver_lookup_cancel (rh);
1575 strcpy (ac->authority_info.dns_authority.name,
1577 memcpy (&ac->authority_info.dns_authority.dns_ip,
1580 /* for DNS recursion, the label is the full DNS name,
1581 created from the remainder of the GNS name and the
1582 name in the NS record */
1583 GNUNET_asprintf (&ac->label,
1585 (int) rh->name_resolution_pos,
1587 (0 != rh->name_resolution_pos) ? "." : "",
1590 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1593 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1596 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1598 rh->proc (rh->proc_cls, 0, NULL);
1599 GNS_resolver_lookup_cancel (rh);
1603 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1607 case GNUNET_DNSPARSER_TYPE_CNAME:
1612 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1615 if ( (NULL == cname) ||
1616 (off != rd[i].data_size) )
1618 GNUNET_break_op (0); /* record not well-formed */
1619 rh->proc (rh->proc_cls, 0, NULL);
1620 GNS_resolver_lookup_cancel (rh);
1623 handle_gns_cname_result (rh,
1625 GNUNET_free (cname);
1628 /* FIXME: handle DNAME */
1634 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1635 _("GNS lookup recursion failed (no delegation record found)\n"));
1636 rh->proc (rh->proc_cls, 0, NULL);
1637 GNS_resolver_lookup_cancel (rh);
1642 * Function called once the namestore has completed the request for
1645 * @param cls closure with the `struct CacheOps`
1646 * @param success #GNUNET_OK on success
1647 * @param emsg error message
1650 namestore_cache_continuation (void *cls,
1654 struct CacheOps *co = cls;
1656 co->namestore_qe_cache = NULL;
1658 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1659 _("Failed to cache GNS resolution: %s\n"),
1661 GNUNET_CONTAINER_DLL_remove (co_head,
1669 * Iterator called on each result obtained for a DHT
1670 * operation that expects a reply
1672 * @param cls closure with the `struct GNS_ResolverHandle`
1673 * @param exp when will this value expire
1674 * @param key key of the result
1675 * @param get_path peers on reply path (or NULL if not recorded)
1676 * [0] = datastore's first neighbor, [length - 1] = local peer
1677 * @param get_path_length number of entries in @a get_path
1678 * @param put_path peers on the PUT path (or NULL if not recorded)
1679 * [0] = origin, [length - 1] = datastore
1680 * @param put_path_length number of entries in @a put_path
1681 * @param type type of the result
1682 * @param size number of bytes in data
1683 * @param data pointer to the result data
1686 handle_dht_response (void *cls,
1687 struct GNUNET_TIME_Absolute exp,
1688 const struct GNUNET_HashCode *key,
1689 const struct GNUNET_PeerIdentity *get_path,
1690 unsigned int get_path_length,
1691 const struct GNUNET_PeerIdentity *put_path,
1692 unsigned int put_path_length,
1693 enum GNUNET_BLOCK_Type type,
1694 size_t size, const void *data)
1696 struct GNS_ResolverHandle *rh = cls;
1697 struct AuthorityChain *ac = rh->ac_tail;
1698 const struct GNUNET_NAMESTORE_Block *block;
1699 struct CacheOps *co;
1701 GNUNET_DHT_get_stop (rh->get_handle);
1702 rh->get_handle = NULL;
1703 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1704 rh->dht_heap_node = NULL;
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1706 "Handling response from the DHT\n");
1707 if (size < sizeof (struct GNUNET_NAMESTORE_Block))
1709 /* how did this pass DHT block validation!? */
1711 rh->proc (rh->proc_cls, 0, NULL);
1712 GNS_resolver_lookup_cancel (rh);
1717 ntohl (block->purpose.size) +
1718 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) +
1719 sizeof (struct GNUNET_CRYPTO_EccSignature))
1721 /* how did this pass DHT block validation!? */
1723 rh->proc (rh->proc_cls, 0, NULL);
1724 GNS_resolver_lookup_cancel (rh);
1728 GNUNET_NAMESTORE_block_decrypt (block,
1729 &ac->authority_info.gns_authority,
1731 &handle_gns_resolution_result,
1734 GNUNET_break_op (0); /* block was ill-formed */
1735 rh->proc (rh->proc_cls, 0, NULL);
1736 GNS_resolver_lookup_cancel (rh);
1739 /* Cache well-formed blocks */
1740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1741 "Caching response from the DHT in namestore\n");
1742 co = GNUNET_new (struct CacheOps);
1743 co->namestore_qe_cache = GNUNET_NAMESTORE_block_cache (namestore_handle,
1745 &namestore_cache_continuation,
1747 GNUNET_CONTAINER_DLL_insert (co_head,
1754 * Process a record that was stored in the namestore.
1756 * @param cls closure with the `struct GNS_ResolverHandle`
1757 * @param block block that was stored in the namestore
1760 handle_namestore_block_response (void *cls,
1761 const struct GNUNET_NAMESTORE_Block *block)
1763 struct GNS_ResolverHandle *rh = cls;
1764 struct GNS_ResolverHandle *rx;
1765 struct AuthorityChain *ac = rh->ac_tail;
1766 const char *label = ac->label;
1767 const struct GNUNET_CRYPTO_EccPublicSignKey *auth = &ac->authority_info.gns_authority;
1768 struct GNUNET_HashCode query;
1770 GNUNET_NAMESTORE_query_from_public_key (auth,
1773 GNUNET_assert (NULL != rh->namestore_qe);
1774 rh->namestore_qe = NULL;
1775 if ( (GNUNET_NO == rh->only_cached) &&
1776 ( (NULL == block) ||
1777 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
1779 /* Namestore knows nothing; try DHT lookup */
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1781 "Starting DHT lookup for `%s' in zone %s\n",
1783 GNUNET_NAMESTORE_z2s (&ac->authority_info.gns_authority));
1784 GNUNET_assert (NULL == rh->get_handle);
1785 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1786 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1788 DHT_GNS_REPLICATION_LEVEL,
1789 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1791 &handle_dht_response, rh);
1792 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1794 GNUNET_TIME_absolute_get ().abs_value_us);
1795 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
1797 /* fail longest-standing DHT request */
1798 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
1799 GNUNET_assert (NULL != rx);
1800 rx->proc (rx->proc_cls, 0, NULL);
1801 GNS_resolver_lookup_cancel (rx);
1805 if ( (NULL == block) ||
1806 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
1808 /* DHT not permitted and no local result, fail */
1809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1810 "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
1812 GNUNET_NAMESTORE_z2s (&ac->authority_info.gns_authority));
1813 rh->proc (rh->proc_cls, 0, NULL);
1814 GNS_resolver_lookup_cancel (rh);
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1818 "Decrypting block from the namestore\n");
1820 GNUNET_NAMESTORE_block_decrypt (block,
1823 &handle_gns_resolution_result,
1826 GNUNET_break_op (0); /* block was ill-formed */
1827 rh->proc (rh->proc_cls, 0, NULL);
1828 GNS_resolver_lookup_cancel (rh);
1835 * Lookup tail of our authority chain in the namestore.
1837 * @param rh query we are processing
1840 recursive_gns_resolution_namestore (struct GNS_ResolverHandle *rh)
1842 struct AuthorityChain *ac = rh->ac_tail;
1843 struct GNUNET_HashCode query;
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1846 "Starting GNS resolution for `%s' in zone %s\n",
1848 GNUNET_NAMESTORE_z2s (&ac->authority_info.gns_authority));
1849 GNUNET_NAMESTORE_query_from_public_key (&ac->authority_info.gns_authority,
1852 rh->namestore_qe = GNUNET_NAMESTORE_lookup_block (namestore_handle,
1854 &handle_namestore_block_response,
1856 GNUNET_assert (NULL != rh->namestore_qe);
1861 * Task scheduled to continue with the resolution process.
1863 * @param cls the `struct GNS_ResolverHandle` of the resolution
1864 * @param tc task context
1867 recursive_resolution (void *cls,
1868 const struct GNUNET_SCHEDULER_TaskContext *tc)
1870 struct GNS_ResolverHandle *rh = cls;
1872 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
1873 if (MAX_RECURSION < rh->loop_limiter++)
1875 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1876 "Encountered unbounded recursion resolving `%s'\n",
1878 rh->proc (rh->proc_cls, 0, NULL);
1879 GNS_resolver_lookup_cancel (rh);
1882 if (GNUNET_YES == rh->ac_tail->gns_authority)
1883 recursive_gns_resolution_namestore (rh);
1885 recursive_dns_resolution (rh);
1890 * Begin the resolution process from 'name', starting with
1891 * the identification of the zone specified by 'name'.
1893 * @param rh resolution to perform
1896 start_resolver_lookup (struct GNS_ResolverHandle *rh)
1898 struct AuthorityChain *ac;
1903 if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
1904 (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
1905 ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
1906 (GNUNET_YES != is_zkey_tld (rh->name)) ) )
1908 /* use standard DNS lookup */
1911 switch (rh->record_type)
1913 case GNUNET_DNSPARSER_TYPE_A:
1916 case GNUNET_DNSPARSER_TYPE_AAAA:
1923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1924 "Doing standard DNS lookup for `%s'\n",
1926 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
1933 if (is_zkey_tld (rh->name))
1935 /* Name ends with ".zkey", try to replace authority zone with zkey
1937 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
1938 x = resolver_lookup_get_next_label (rh); /* will return 'x' coordinate */
1939 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
1940 GNUNET_asprintf (&pkey,
1946 GNUNET_CRYPTO_ecc_public_sign_key_from_string (pkey,
1948 &rh->authority_zone)) )
1950 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1951 _("Hostname `%s' is not well-formed, resolution fails\n"),
1953 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
1955 GNUNET_free_non_null (x);
1956 GNUNET_free_non_null (y);
1961 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
1962 GNUNET_free (resolver_lookup_get_next_label (rh));
1964 ac = GNUNET_new (struct AuthorityChain);
1966 ac->label = resolver_lookup_get_next_label (rh);
1967 if (NULL == ac->label)
1968 /* name was just "gnu", so we default to label '+' */
1969 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
1970 ac->gns_authority = GNUNET_YES;
1971 ac->authority_info.gns_authority = rh->authority_zone;
1972 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1975 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1981 * Lookup of a record in a specific zone calls lookup result processor
1984 * @param zone the zone to perform the lookup in
1985 * @param record_type the record type to look up
1986 * @param name the name to look up
1987 * @param shorten_key a private key for use with PSEU import (can be NULL)
1988 * @param only_cached #GNUNET_NO to only check locally not DHT for performance
1989 * @param proc the processor to call on result
1990 * @param proc_cls the closure to pass to @a proc
1991 * @return handle to cancel operation
1993 struct GNS_ResolverHandle *
1994 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EccPublicSignKey *zone,
1995 uint32_t record_type,
1997 const struct GNUNET_CRYPTO_EccPrivateKey *shorten_key,
1999 GNS_ResultProcessor proc, void *proc_cls)
2001 struct GNS_ResolverHandle *rh;
2003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2004 "Starting lookup for `%s'\n",
2006 rh = GNUNET_new (struct GNS_ResolverHandle);
2007 GNUNET_CONTAINER_DLL_insert (rlh_head,
2010 rh->authority_zone = *zone;
2012 rh->proc_cls = proc_cls;
2013 rh->only_cached = only_cached;
2014 rh->record_type = record_type;
2015 rh->name = GNUNET_strdup (name);
2016 rh->name_resolution_pos = strlen (name);
2017 if (NULL != shorten_key)
2019 rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
2020 *rh->shorten_key = *shorten_key;
2022 start_resolver_lookup (rh);
2028 * Cancel active resolution (i.e. client disconnected).
2030 * @param rh resolution to abort
2033 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
2035 struct DnsResult *dr;
2036 struct AuthorityChain *ac;
2037 struct VpnContext *vpn_ctx;
2039 GNUNET_CONTAINER_DLL_remove (rlh_head,
2042 while (NULL != (ac = rh->ac_head))
2044 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
2047 GNUNET_free (ac->label);
2050 if (GNUNET_SCHEDULER_NO_TASK != rh->task_id)
2052 GNUNET_SCHEDULER_cancel (rh->task_id);
2053 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
2055 if (NULL != rh->get_handle)
2057 GNUNET_DHT_get_stop (rh->get_handle);
2058 rh->get_handle = NULL;
2060 if (NULL != rh->dht_heap_node)
2062 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
2063 rh->dht_heap_node = NULL;
2065 if (NULL != (vpn_ctx = rh->vpn_ctx))
2067 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
2068 GNUNET_free (vpn_ctx->rd_data);
2069 GNUNET_free (vpn_ctx);
2071 if (NULL != rh->dns_request)
2073 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
2074 rh->dns_request = NULL;
2076 if (NULL != rh->namestore_qe)
2078 GNUNET_NAMESTORE_cancel (rh->namestore_qe);
2079 rh->namestore_qe = NULL;
2081 if (NULL != rh->std_resolve)
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2084 "Canceling standard DNS resolution\n");
2085 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
2086 rh->std_resolve = NULL;
2088 while (NULL != (dr = rh->dns_result_head))
2090 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
2091 rh->dns_result_tail,
2095 GNUNET_free_non_null (rh->shorten_key);
2096 GNUNET_free (rh->name);
2101 /* ***************** Resolver initialization ********************* */
2105 * Initialize the resolver
2107 * @param nh the namestore handle
2108 * @param dht the dht handle
2109 * @param c configuration handle
2110 * @param max_bg_queries maximum number of parallel background queries in dht
2113 GNS_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
2114 struct GNUNET_DHT_Handle *dht,
2115 const struct GNUNET_CONFIGURATION_Handle *c,
2116 unsigned long long max_bg_queries)
2121 namestore_handle = nh;
2124 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2125 max_allowed_background_queries = max_bg_queries;
2127 GNUNET_CONFIGURATION_get_value_string (c,
2132 /* user did not specify DNS resolver, use 8.8.8.8 */
2133 dns_ip = GNUNET_strdup ("8.8.8.8");
2135 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
2136 GNUNET_free (dns_ip);
2137 vpn_handle = GNUNET_VPN_connect (cfg);
2145 GNS_resolver_done ()
2147 struct GNS_ResolverHandle *rh;
2148 struct CacheOps *co;
2150 /* abort active resolutions */
2151 while (NULL != (rh = rlh_head))
2153 rh->proc (rh->proc_cls, 0, NULL);
2154 GNS_resolver_lookup_cancel (rh);
2156 while (NULL != (co = co_head))
2158 GNUNET_CONTAINER_DLL_remove (co_head,
2161 GNUNET_NAMESTORE_cancel (co->namestore_qe_cache);
2164 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
2165 dht_lookup_heap = NULL;
2166 GNUNET_DNSSTUB_stop (dns_handle);
2168 GNUNET_VPN_disconnect (vpn_handle);
2171 namestore_handle = NULL;
2175 /* *************** common helper functions (do not really belong here) *********** */
2178 * Checks if @a name ends in ".TLD"
2180 * @param name the name to check
2181 * @param tld the TLD to check for
2182 * @return GNUNET_YES or GNUNET_NO
2185 is_tld (const char* name, const char* tld)
2189 if (strlen (name) <= strlen (tld))
2191 offset = strlen (name) - strlen (tld);
2192 if (0 != strcmp (name + offset, tld))
2198 /* end of gnunet-service-gns_resolver.c */