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_gnsrecord_lib.h"
38 #include "gnunet_namestore_service.h"
39 #include "gnunet_dns_service.h"
40 #include "gnunet_resolver_service.h"
41 #include "gnunet_dnsparser_lib.h"
42 #include "gnunet_gns_service.h"
44 #include "gnunet-service-gns_resolver.h"
45 #include "gnunet-service-gns_shorten.h"
46 #include "gnunet_vpn_service.h"
50 * Default DHT timeout for lookups.
52 #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
55 * Default timeout for DNS lookups.
57 #define DNS_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
60 * Default timeout for VPN redirections.
62 #define VPN_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
65 * DHT replication level
67 #define DHT_GNS_REPLICATION_LEVEL 5
70 * How deep do we allow recursions to go before we abort?
72 #define MAX_RECURSION 256
76 * DLL to hold the authority chain we had to pass in the resolution
84 struct AuthorityChain *prev;
89 struct AuthorityChain *next;
92 * Resolver handle this entry in the chain belongs to.
94 struct GNS_ResolverHandle *rh;
97 * label/name corresponding to the authority
102 * #GNUNET_YES if the authority was a GNS authority,
103 * #GNUNET_NO if the authority was a DNS authority.
108 * Information about the resolver authority for this label.
114 * The zone of the GNS authority
116 struct GNUNET_CRYPTO_EcdsaPublicKey gns_authority;
121 * Domain of the DNS resolver that is the authority.
122 * (appended to construct the DNS name to resolve;
123 * this is NOT the DNS name of the DNS server!).
125 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
128 * IP address of the DNS resolver that is authoritative.
129 * (this implementation currently only supports one
132 struct sockaddr_storage dns_ip;
142 * A result we got from DNS.
150 struct DnsResult *next;
155 struct DnsResult *prev;
158 * Binary value stored in the DNS record (appended to this struct)
163 * Expiration time for the DNS record, 0 if we didn't
164 * get anything useful (i.e. 'gethostbyname' was used).
166 uint64_t expiration_time;
169 * Number of bytes in 'data'.
174 * Type of the GNS/DNS record.
176 uint32_t record_type;
182 * Closure for #vpn_allocation_cb.
188 * Which resolution process are we processing.
190 struct GNS_ResolverHandle *rh;
193 * Handle to the VPN request that we were performing.
195 struct GNUNET_VPN_RedirectionRequest *vpn_request;
198 * Number of records serialized in 'rd_data'.
200 unsigned int rd_count;
203 * Serialized records.
208 * Number of bytes in 'rd_data'.
215 * Handle to a currenty pending resolution. On result (positive or
216 * negative) the #GNS_ResultProcessor is called.
218 struct GNS_ResolverHandle
224 struct GNS_ResolverHandle *next;
229 struct GNS_ResolverHandle *prev;
232 * The top-level GNS authoritative zone to query
234 struct GNUNET_CRYPTO_EcdsaPublicKey authority_zone;
237 * called when resolution phase finishes
239 GNS_ResultProcessor proc;
242 * closure passed to proc
247 * Handle for DHT lookups. should be NULL if no lookups are in progress
249 struct GNUNET_DHT_GetHandle *get_handle;
252 * Handle to a VPN request, NULL if none is active.
254 struct VpnContext *vpn_ctx;
257 * Socket for a DNS request, NULL if none is active.
259 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
262 * Handle for standard DNS resolution, NULL if none is active.
264 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
267 * Pending Namestore lookup task
269 struct GNUNET_NAMESTORE_QueueEntry *namestore_qe;
272 * Heap node associated with this lookup. Used to limit number of
273 * concurrent requests.
275 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
278 * DLL to store the authority chain
280 struct AuthorityChain *ac_head;
283 * DLL to store the authority chain
285 struct AuthorityChain *ac_tail;
288 * Private key of the shorten zone, NULL to not shorten.
290 struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key;
293 * ID of a task associated with the resolution process.
295 GNUNET_SCHEDULER_TaskIdentifier task_id;
298 * The name to resolve
303 * DLL of results we got from DNS.
305 struct DnsResult *dns_result_head;
308 * DLL of results we got from DNS.
310 struct DnsResult *dns_result_tail;
313 * Current offset in 'name' where we are resolving.
315 size_t name_resolution_pos;
323 * Desired type for the resolution.
328 * We increment the loop limiter for each step in a recursive
329 * resolution. If it passes our threshold (i.e. due to
330 * self-recursion in the resolution, i.e CNAME fun), we stop.
332 unsigned int loop_limiter;
338 * Active namestore caching operations.
344 * Organized in a DLL.
346 struct CacheOps *next;
349 * Organized in a DLL.
351 struct CacheOps *prev;
354 * Pending Namestore caching task.
356 struct GNUNET_NAMESTORE_QueueEntry *namestore_qe_cache;
362 * Our handle to the namestore service
364 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
367 * Our handle to the vpn service
369 static struct GNUNET_VPN_Handle *vpn_handle;
372 * Resolver handle to the dht
374 static struct GNUNET_DHT_Handle *dht_handle;
377 * Handle to perform DNS lookups.
379 static struct GNUNET_DNSSTUB_Context *dns_handle;
382 * Heap for limiting parallel DHT lookups
384 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
387 * Maximum amount of parallel queries to the DHT
389 static unsigned long long max_allowed_background_queries;
392 * Head of resolver lookup list
394 static struct GNS_ResolverHandle *rlh_head;
397 * Tail of resolver lookup list
399 static struct GNS_ResolverHandle *rlh_tail;
402 * Organized in a DLL.
404 static struct CacheOps *co_head;
407 * Organized in a DLL.
409 static struct CacheOps *co_tail;
413 * Global configuration.
415 static const struct GNUNET_CONFIGURATION_Handle *cfg;
419 * Check if name is in srv format (_x._y.xxx)
422 * @return #GNUNET_YES if true
425 is_srv (const char *name)
432 if (NULL == strstr (name, "._"))
435 ndup = GNUNET_strdup (name);
437 if (NULL == strtok (NULL, "."))
439 if (NULL == strtok (NULL, "."))
441 if (NULL != strtok (NULL, "."))
450 * Determine if this name is canonical (is a legal name in a zone, without delegation);
451 * note that we do not test that the name does not contain illegal characters, we only
452 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
453 * even though they consist of multiple labels.
456 * a.b.gnu = not canonical
458 * _foo._srv = canonical
459 * _f.bar = not canonical
461 * @param name the name to test
462 * @return #GNUNET_YES if canonical
465 is_canonical (const char *name)
470 if (NULL == strchr (name, '.'))
475 while (NULL != (dot = strchr (pos, '.')))
483 /* ************************** Resolution **************************** */
486 * Exands a name ending in .+ with the zone of origin.
488 * @param rh resolution context
489 * @param name name to modify (to be free'd or returned)
490 * @return updated name
493 translate_dot_plus (struct GNS_ResolverHandle *rh,
497 size_t s_len = strlen (name);
499 if (0 != strcmp (&name[s_len - 2],
501 return name; /* did not end in ".+" */
502 GNUNET_assert (GNUNET_YES == rh->ac_tail->gns_authority);
503 GNUNET_asprintf (&ret,
507 GNUNET_NAMESTORE_pkey_to_zkey (&rh->ac_tail->authority_info.gns_authority));
514 * Task scheduled to asynchronously fail a resolution.
516 * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
517 * @param tc task context
520 fail_resolution (void *cls,
521 const struct GNUNET_SCHEDULER_TaskContext *tc)
523 struct GNS_ResolverHandle *rh = cls;
525 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
526 rh->proc (rh->proc_cls, 0, NULL);
527 GNS_resolver_lookup_cancel (rh);
531 #if (defined WINDOWS) || (defined DARWIN)
532 /* Don't have this on W32, here's a naive implementation
533 * Was somehow removed on OS X ... */
535 memrchr (const void *s,
539 const unsigned char *ucs = s;
542 for (i = n - 1; i >= 0; i--)
543 if (c == (int) ucs[i])
544 return (void *) &ucs[i];
551 * Get the next, rightmost label from the name that we are trying to resolve,
552 * and update the resolution position accordingly.
554 * @param rh handle to the resolution operation to get the next label from
555 * @return NULL if there are no more labels
558 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
564 if (0 == rh->name_resolution_pos)
566 dot = memrchr (rh->name, (int) '.', rh->name_resolution_pos);
569 /* done, this was the last one */
570 len = rh->name_resolution_pos;
572 rh->name_resolution_pos = 0;
576 /* advance by one label */
577 len = rh->name_resolution_pos - (dot - rh->name) - 1;
579 rh->name_resolution_pos = dot - rh->name;
581 return GNUNET_strndup (rp, len);
586 * Gives the cummulative result obtained to the callback and clean up the request.
588 * @param rh resolution process that has culminated in a result
591 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
593 struct DnsResult *pos;
598 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
601 struct GNUNET_NAMESTORE_RecordData rd[n];
604 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
606 rd[i].data = pos->data;
607 rd[i].data_size = pos->data_size;
608 rd[i].record_type = pos->record_type;
609 if (0 == pos->expiration_time)
611 rd[i].flags = GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
612 rd[i].expiration_time = 0;
616 rd[i].flags = GNUNET_NAMESTORE_RF_NONE;
617 rd[i].expiration_time = pos->expiration_time;
621 GNUNET_assert (i == n);
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
623 "Transmitting standard DNS result with %u records\n",
625 rh->proc (rh->proc_cls,
629 GNS_resolver_lookup_cancel (rh);
634 * Add a result from DNS to the records to be returned to the application.
636 * @param rh resolution request to extend with a result
637 * @param expiration_time expiration time for the answer
638 * @param record_type DNS record type of the answer
639 * @param data_size number of bytes in @a data
640 * @param data binary data to return in DNS record
643 add_dns_result (struct GNS_ResolverHandle *rh,
644 uint64_t expiration_time,
645 uint32_t record_type,
649 struct DnsResult *res;
651 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
652 res->expiration_time = expiration_time;
653 res->data_size = data_size;
654 res->record_type = record_type;
656 memcpy (&res[1], data, data_size);
657 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
664 * We had to do a DNS lookup. Convert the result (if any) and return
667 * @param cls closure with the 'struct GNS_ResolverHandle'
668 * @param addr one of the addresses of the host, NULL for the last address
669 * @param addrlen length of the address
672 handle_dns_result (void *cls,
673 const struct sockaddr *addr,
676 struct GNS_ResolverHandle *rh = cls;
677 const struct sockaddr_in *sa4;
678 const struct sockaddr_in6 *sa6;
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
681 "Received %u bytes of DNS IP data\n",
685 rh->std_resolve = NULL;
686 transmit_lookup_dns_result (rh);
689 switch (addr->sa_family)
692 sa4 = (const struct sockaddr_in *) addr;
694 0 /* expiration time is unknown */,
695 GNUNET_DNSPARSER_TYPE_A,
696 sizeof (struct in_addr),
700 sa6 = (const struct sockaddr_in6 *) addr;
702 0 /* expiration time is unknown */,
703 GNUNET_DNSPARSER_TYPE_AAAA,
704 sizeof (struct in6_addr),
715 * Task scheduled to continue with the resolution process.
717 * @param cls the 'struct GNS_ResolverHandle' of the resolution
718 * @param tc task context
721 recursive_resolution (void *cls,
722 const struct GNUNET_SCHEDULER_TaskContext *tc);
726 * Begin the resolution process from 'name', starting with
727 * the identification of the zone specified by 'name'.
729 * @param rh resolution to perform
732 start_resolver_lookup (struct GNS_ResolverHandle *rh);
736 * Function called with the result of a DNS resolution.
738 * @param cls the request handle of the resolution that
739 * we were attempting to make
740 * @param rs socket that received the response
741 * @param dns dns response, never NULL
742 * @param dns_len number of bytes in @a dns
745 dns_result_parser (void *cls,
746 struct GNUNET_DNSSTUB_RequestSocket *rs,
747 const struct GNUNET_TUN_DnsHeader *dns,
750 struct GNS_ResolverHandle *rh = cls;
751 struct GNUNET_DNSPARSER_Packet *p;
752 const struct GNUNET_DNSPARSER_Record *rec;
753 unsigned int rd_count;
756 rh->dns_request = NULL;
757 GNUNET_SCHEDULER_cancel (rh->task_id);
758 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
759 p = GNUNET_DNSPARSER_parse ((const char *) dns,
763 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
764 _("Failed to parse DNS response\n"));
765 rh->proc (rh->proc_cls, 0, NULL);
766 GNS_resolver_lookup_cancel (rh);
769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
770 "Received DNS response for `%s' with %u answers\n",
772 (unsigned int) p->num_answers);
773 if ( (p->num_answers > 0) &&
774 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
775 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
777 GNUNET_free (rh->name);
778 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
779 start_resolver_lookup (rh);
780 GNUNET_DNSPARSER_free_packet (p);
783 /* FIXME: add DNAME support */
785 /* convert from (parsed) DNS to (binary) GNS format! */
786 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
788 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
790 char buf[UINT16_MAX];
796 memset (rd, 0, sizeof (rd));
797 for (i=0;i<rd_count;i++)
799 if (i < p->num_answers)
800 rec = &p->answers[i];
801 else if (i < p->num_answers + p->num_authority_records)
802 rec = &p->authority_records[i - p->num_answers];
804 rec = &p->authority_records[i - p->num_answers - p->num_authority_records];
805 /* As we copied the full DNS name to 'rh->ac_tail->label', this
806 should be the correct check to see if this record is actually
807 a record for our label... */
808 if (0 != strcmp (rec->name,
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "Dropping record `%s', does not match desired name `%s'\n",
818 rd[i - skip].record_type = rec->type;
819 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
822 case GNUNET_DNSPARSER_TYPE_A:
823 if (rec->data.raw.data_len != sizeof (struct in_addr))
829 rd[i - skip].data_size = rec->data.raw.data_len;
830 rd[i - skip].data = rec->data.raw.data;
832 case GNUNET_DNSPARSER_TYPE_AAAA:
833 if (rec->data.raw.data_len != sizeof (struct in6_addr))
839 rd[i - skip].data_size = rec->data.raw.data_len;
840 rd[i - skip].data = rec->data.raw.data;
842 case GNUNET_DNSPARSER_TYPE_CNAME:
843 case GNUNET_DNSPARSER_TYPE_PTR:
844 case GNUNET_DNSPARSER_TYPE_NS:
847 GNUNET_DNSPARSER_builder_add_name (buf,
856 rd[i - skip].data_size = buf_off - buf_start;
857 rd[i - skip].data = &buf[buf_start];
859 case GNUNET_DNSPARSER_TYPE_SOA:
862 GNUNET_DNSPARSER_builder_add_soa (buf,
871 rd[i - skip].data_size = buf_off - buf_start;
872 rd[i - skip].data = &buf[buf_start];
874 case GNUNET_DNSPARSER_TYPE_MX:
877 GNUNET_DNSPARSER_builder_add_mx (buf,
886 rd[i - skip].data_size = buf_off - buf_start;
887 rd[i - skip].data = &buf[buf_start];
889 case GNUNET_DNSPARSER_TYPE_SRV:
892 GNUNET_DNSPARSER_builder_add_srv (buf,
901 rd[i - skip].data_size = buf_off - buf_start;
902 rd[i - skip].data = &buf[buf_start];
905 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
906 _("Skipping record of unsupported type %d\n"),
912 rh->proc (rh->proc_cls, rd_count - skip, rd);
913 GNS_resolver_lookup_cancel (rh);
915 GNUNET_DNSPARSER_free_packet (p);
920 * Perform recursive DNS resolution. Asks the given DNS resolver to
921 * resolve "rh->dns_name", possibly recursively proceeding following
922 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
923 * we find the answer.
925 * @param rh resolution information
928 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
930 struct AuthorityChain *ac;
932 struct GNUNET_DNSPARSER_Query *query;
933 struct GNUNET_DNSPARSER_Packet *p;
935 size_t dns_request_length;
938 GNUNET_assert (NULL != ac);
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
940 "Starting DNS lookup for `%s'\n",
942 GNUNET_assert (GNUNET_NO == ac->gns_authority);
943 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
946 sa_len = sizeof (struct sockaddr_in);
949 sa_len = sizeof (struct sockaddr_in6);
953 rh->proc (rh->proc_cls, 0, NULL);
954 GNS_resolver_lookup_cancel (rh);
957 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
958 query->name = GNUNET_strdup (ac->label);
959 query->type = rh->record_type;
960 query->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
961 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
964 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
966 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
967 p->flags.recursion_desired = 1;
969 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
972 rh->proc (rh->proc_cls, 0, NULL);
973 GNS_resolver_lookup_cancel (rh);
977 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
978 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
984 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
988 GNUNET_free (dns_request);
989 GNUNET_DNSPARSER_free_packet (p);
994 * We encountered a CNAME record during our resolution.
995 * Merge it into our chain.
997 * @param rh resolution we are performing
998 * @param cname value of the cname record we got for the current
999 * authority chain tail
1002 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
1007 struct AuthorityChain *ac;
1009 nlen = strlen (cname);
1012 &cname[nlen - 2])) )
1014 /* CNAME resolution continues relative to current domain */
1015 if (0 == rh->name_resolution_pos)
1017 res = GNUNET_strndup (cname, nlen - 2);
1018 rh->name_resolution_pos = nlen - 2;
1022 GNUNET_asprintf (&res,
1024 (int) rh->name_resolution_pos,
1028 rh->name_resolution_pos = strlen (res);
1030 GNUNET_free (rh->name);
1032 ac = GNUNET_new (struct AuthorityChain);
1034 ac->gns_authority = GNUNET_YES;
1035 ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
1036 ac->label = resolver_lookup_get_next_label (rh);
1037 /* tigger shortening */
1038 if (NULL != rh->shorten_key)
1039 GNS_shorten_start (rh->ac_tail->label,
1040 &ac->authority_info.gns_authority,
1042 /* add AC to tail */
1043 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1046 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1050 /* name is absolute, start from the beginning */
1051 GNUNET_free (rh->name);
1052 rh->name = GNUNET_strdup (cname);
1053 start_resolver_lookup (rh);
1058 * Process a records that were decrypted from a block.
1060 * @param cls closure with the 'struct GNS_ResolverHandle'
1061 * @param rd_count number of entries in @a rd array
1062 * @param rd array of records with data to store
1065 handle_gns_resolution_result (void *cls,
1066 unsigned int rd_count,
1067 const struct GNUNET_NAMESTORE_RecordData *rd);
1071 * Callback invoked from the VPN service once a redirection is
1072 * available. Provides the IP address that can now be used to
1073 * reach the requested destination. Replaces the "VPN" record
1074 * with the respective A/AAAA record and continues processing.
1076 * @param cls closure
1077 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1078 * will match 'result_af' from the request
1079 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1080 * that the VPN allocated for the redirection;
1081 * traffic to this IP will now be redirected to the
1082 * specified target peer; NULL on error
1085 vpn_allocation_cb (void *cls,
1087 const void *address)
1089 struct VpnContext *vpn_ctx = cls;
1090 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1091 struct GNUNET_NAMESTORE_RecordData rd[vpn_ctx->rd_count];
1094 vpn_ctx->vpn_request = NULL;
1096 GNUNET_assert (GNUNET_OK ==
1097 GNUNET_NAMESTORE_records_deserialize (vpn_ctx->rd_data_size,
1101 for (i=0;i<vpn_ctx->rd_count;i++)
1103 if (GNUNET_NAMESTORE_TYPE_VPN == rd[i].record_type)
1108 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1109 rd[i].data_size = sizeof (struct in_addr);
1110 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1112 rd[i].data = address;
1115 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1116 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1118 rd[i].data = address;
1119 rd[i].data_size = sizeof (struct in6_addr);
1127 GNUNET_assert (i < vpn_ctx->rd_count);
1128 handle_gns_resolution_result (rh,
1131 GNUNET_free (vpn_ctx->rd_data);
1132 GNUNET_free (vpn_ctx);
1137 * Process a records that were decrypted from a block.
1139 * @param cls closure with the `struct GNS_ResolverHandle`
1140 * @param rd_count number of entries in @a rd array
1141 * @param rd array of records with data to store
1144 handle_gns_resolution_result (void *cls,
1145 unsigned int rd_count,
1146 const struct GNUNET_NAMESTORE_RecordData *rd)
1148 struct GNS_ResolverHandle *rh = cls;
1149 struct AuthorityChain *ac;
1152 struct sockaddr *sa;
1153 struct sockaddr_in v4;
1154 struct sockaddr_in6 v6;
1157 struct VpnContext *vpn_ctx;
1158 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1160 struct GNUNET_HashCode vhash;
1162 char scratch[UINT16_MAX];
1164 size_t scratch_start;
1166 struct GNUNET_NAMESTORE_RecordData rd_new[rd_count];
1167 unsigned int rd_off;
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Resolution succeeded for `%s' in zone %s, got %u records\n",
1172 GNUNET_NAMESTORE_z2s (&rh->ac_tail->authority_info.gns_authority),
1174 if (0 == rh->name_resolution_pos)
1176 /* top-level match, are we done yet? */
1177 if ( (rd_count > 0) &&
1178 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1179 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1182 cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
1185 if ( (NULL == cname) ||
1186 (off != rd[0].data_size) )
1188 GNUNET_break_op (0);
1189 rh->proc (rh->proc_cls, 0, NULL);
1190 GNS_resolver_lookup_cancel (rh);
1193 handle_gns_cname_result (rh,
1195 GNUNET_free (cname);
1198 /* If A/AAAA was requested, but we got a VPN
1199 record, we convert it to A/AAAA using GNUnet VPN */
1200 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1201 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1203 for (i=0;i<rd_count;i++)
1205 switch (rd[i].record_type)
1207 case GNUNET_NAMESTORE_TYPE_VPN:
1209 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1210 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) <
1213 GNUNET_break_op (0);
1214 rh->proc (rh->proc_cls, 0, NULL);
1215 GNS_resolver_lookup_cancel (rh);
1218 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1219 vname = (const char *) &vpn[1];
1220 if ('\0' != vname[rd[i].data_size - 1 - sizeof (struct GNUNET_TUN_GnsVpnRecord)])
1222 GNUNET_break_op (0);
1223 rh->proc (rh->proc_cls, 0, NULL);
1224 GNS_resolver_lookup_cancel (rh);
1227 GNUNET_CRYPTO_hash (vname,
1228 strlen (vname), // FIXME: +1?
1230 vpn_ctx = GNUNET_new (struct VpnContext);
1231 rh->vpn_ctx = vpn_ctx;
1233 vpn_ctx->rd_data_size = GNUNET_NAMESTORE_records_get_size (rd_count,
1235 vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
1236 (void) GNUNET_NAMESTORE_records_serialize (rd_count,
1238 vpn_ctx->rd_data_size,
1240 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1245 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1250 case GNUNET_NAMESTORE_TYPE_GNS2DNS:
1252 /* delegation to DNS */
1260 /* convert relative names in record values to absolute names,
1261 using 'scratch' array for memory allocations */
1264 for (i=0;i<rd_count;i++)
1266 rd_new[rd_off] = rd[i];
1267 /* Check if the embedded name(s) end in "+", and if so,
1268 replace the "+" with the zone at "ac_tail", changing the name
1269 to a ".zkey". The name is allocated on the 'scratch' array,
1270 so we can free it afterwards. */
1271 switch (rd[i].record_type)
1273 case GNUNET_DNSPARSER_TYPE_CNAME:
1278 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1281 if ( (NULL == cname) ||
1282 (off != rd[i].data_size) )
1284 GNUNET_break_op (0); /* record not well-formed */
1288 cname = translate_dot_plus (rh, cname);
1289 scratch_start = scratch_off;
1291 GNUNET_DNSPARSER_builder_add_name (scratch,
1300 rd_new[rd_off].data = &scratch[scratch_start];
1301 rd_new[rd_off].data_size = scratch_off - scratch_start;
1305 GNUNET_free_non_null (cname);
1308 case GNUNET_DNSPARSER_TYPE_SOA:
1310 struct GNUNET_DNSPARSER_SoaRecord *soa;
1313 soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
1316 if ( (NULL == soa) ||
1317 (off != rd[i].data_size) )
1319 GNUNET_break_op (0); /* record not well-formed */
1323 soa->mname = translate_dot_plus (rh, soa->mname);
1324 soa->rname = translate_dot_plus (rh, soa->rname);
1325 scratch_start = scratch_off;
1327 GNUNET_DNSPARSER_builder_add_soa (scratch,
1336 rd_new[rd_off].data = &scratch[scratch_start];
1337 rd_new[rd_off].data_size = scratch_off - scratch_start;
1342 GNUNET_DNSPARSER_free_soa (soa);
1345 case GNUNET_DNSPARSER_TYPE_MX:
1347 struct GNUNET_DNSPARSER_MxRecord *mx;
1350 mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
1353 if ( (NULL == mx) ||
1354 (off != rd[i].data_size) )
1356 GNUNET_break_op (0); /* record not well-formed */
1360 mx->mxhost = translate_dot_plus (rh, mx->mxhost);
1361 scratch_start = scratch_off;
1363 GNUNET_DNSPARSER_builder_add_mx (scratch,
1372 rd_new[rd_off].data = &scratch[scratch_start];
1373 rd_new[rd_off].data_size = scratch_off - scratch_start;
1378 GNUNET_DNSPARSER_free_mx (mx);
1381 case GNUNET_DNSPARSER_TYPE_SRV:
1383 struct GNUNET_DNSPARSER_SrvRecord *srv;
1386 /* FIXME: passing rh->name here is is not necessarily what we want
1387 (SRV support not finished) */
1388 srv = GNUNET_DNSPARSER_parse_srv (rh->name,
1392 if ( (NULL == srv) ||
1393 (off != rd[i].data_size) )
1395 GNUNET_break_op (0); /* record not well-formed */
1399 srv->domain_name = translate_dot_plus (rh, srv->domain_name);
1400 srv->target = translate_dot_plus (rh, srv->target);
1401 scratch_start = scratch_off;
1403 GNUNET_DNSPARSER_builder_add_srv (scratch,
1412 rd_new[rd_off].data = &scratch[scratch_start];
1413 rd_new[rd_off].data_size = scratch_off - scratch_start;
1418 GNUNET_DNSPARSER_free_srv (srv);
1421 case GNUNET_NAMESTORE_TYPE_PKEY:
1422 /* tigger shortening */
1423 if (NULL != rh->shorten_key)
1425 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
1427 if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
1429 GNUNET_break_op (0);
1432 memcpy (&pub, rd[i].data, rd[i].data_size);
1433 GNS_shorten_start (rh->ac_tail->label,
1445 /* yes, we are done, return result */
1446 rh->proc (rh->proc_cls, rd_off, rd_new);
1447 GNS_resolver_lookup_cancel (rh);
1451 /* need to recurse, check if we can */
1452 for (i=0;i<rd_count;i++)
1454 switch (rd[i].record_type)
1456 case GNUNET_NAMESTORE_TYPE_PKEY:
1457 /* delegation to another zone */
1458 if (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
1461 GNUNET_break_op (0);
1462 rh->proc (rh->proc_cls, 0, NULL);
1463 GNS_resolver_lookup_cancel (rh);
1466 /* expand authority chain */
1467 ac = GNUNET_new (struct AuthorityChain);
1469 ac->gns_authority = GNUNET_YES;
1470 memcpy (&ac->authority_info.gns_authority,
1472 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1473 ac->label = resolver_lookup_get_next_label (rh);
1474 /* tigger shortening */
1475 if (NULL != rh->shorten_key)
1476 GNS_shorten_start (rh->ac_tail->label,
1477 &ac->authority_info.gns_authority,
1479 /* add AC to tail */
1480 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1484 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1487 case GNUNET_NAMESTORE_TYPE_GNS2DNS:
1490 /* resolution continues within DNS */
1491 if (GNUNET_DNSPARSER_MAX_NAME_LENGTH < rd[i].data_size)
1493 GNUNET_break_op (0);
1494 rh->proc (rh->proc_cls, 0, NULL);
1495 GNS_resolver_lookup_cancel (rh);
1498 /* find associated A/AAAA record */
1501 for (j=0;j<rd_count;j++)
1503 switch (rd[j].record_type)
1505 case GNUNET_DNSPARSER_TYPE_A:
1506 if (sizeof (struct in_addr) != rd[j].data_size)
1508 GNUNET_break_op (0);
1509 rh->proc (rh->proc_cls, 0, NULL);
1510 GNS_resolver_lookup_cancel (rh);
1513 /* FIXME: might want to check if we support IPv4 here,
1514 and otherwise skip this one and hope we find another */
1515 memset (&v4, 0, sizeof (v4));
1516 sa_len = sizeof (v4);
1517 v4.sin_family = AF_INET;
1518 v4.sin_port = htons (53);
1519 #if HAVE_SOCKADDR_IN_SIN_LEN
1520 v4.sin_len = (u_char) sa_len;
1522 memcpy (&v4.sin_addr,
1524 sizeof (struct in_addr));
1525 sa = (struct sockaddr *) &v4;
1527 case GNUNET_DNSPARSER_TYPE_AAAA:
1528 if (sizeof (struct in6_addr) != rd[j].data_size)
1530 GNUNET_break_op (0);
1531 rh->proc (rh->proc_cls, 0, NULL);
1532 GNS_resolver_lookup_cancel (rh);
1535 /* FIXME: might want to check if we support IPv6 here,
1536 and otherwise skip this one and hope we find another */
1537 memset (&v6, 0, sizeof (v6));
1538 sa_len = sizeof (v6);
1539 v6.sin6_family = AF_INET6;
1540 v6.sin6_port = htons (53);
1541 #if HAVE_SOCKADDR_IN_SIN_LEN
1542 v6.sin6_len = (u_char) sa_len;
1544 memcpy (&v6.sin6_addr,
1546 sizeof (struct in6_addr));
1547 sa = (struct sockaddr *) &v6;
1557 /* we cannot continue; NS without A/AAAA */
1558 rh->proc (rh->proc_cls, 0, NULL);
1559 GNS_resolver_lookup_cancel (rh);
1562 /* expand authority chain */
1563 ac = GNUNET_new (struct AuthorityChain);
1566 ns = GNUNET_DNSPARSER_parse_name (rd[i].data,
1569 if ( (NULL == ns) ||
1570 (off != rd[i].data_size) )
1572 GNUNET_break_op (0); /* record not well-formed */
1573 rh->proc (rh->proc_cls, 0, NULL);
1574 GNS_resolver_lookup_cancel (rh);
1575 GNUNET_free_non_null (ns);
1579 strcpy (ac->authority_info.dns_authority.name,
1581 memcpy (&ac->authority_info.dns_authority.dns_ip,
1584 /* for DNS recursion, the label is the full DNS name,
1585 created from the remainder of the GNS name and the
1586 name in the NS record */
1587 GNUNET_asprintf (&ac->label,
1589 (int) rh->name_resolution_pos,
1591 (0 != rh->name_resolution_pos) ? "." : "",
1594 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1597 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1599 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1600 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1602 rh->proc (rh->proc_cls, 0, NULL);
1603 GNS_resolver_lookup_cancel (rh);
1607 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1611 case GNUNET_DNSPARSER_TYPE_CNAME:
1616 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1619 if ( (NULL == cname) ||
1620 (off != rd[i].data_size) )
1622 GNUNET_break_op (0); /* record not well-formed */
1623 rh->proc (rh->proc_cls, 0, NULL);
1624 GNS_resolver_lookup_cancel (rh);
1625 GNUNET_free_non_null (cname);
1628 handle_gns_cname_result (rh,
1630 GNUNET_free (cname);
1633 /* FIXME: handle DNAME */
1639 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1640 _("GNS lookup recursion failed (no delegation record found)\n"));
1641 rh->proc (rh->proc_cls, 0, NULL);
1642 GNS_resolver_lookup_cancel (rh);
1647 * Function called once the namestore has completed the request for
1650 * @param cls closure with the `struct CacheOps`
1651 * @param success #GNUNET_OK on success
1652 * @param emsg error message
1655 namestore_cache_continuation (void *cls,
1659 struct CacheOps *co = cls;
1661 co->namestore_qe_cache = NULL;
1663 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1664 _("Failed to cache GNS resolution: %s\n"),
1666 GNUNET_CONTAINER_DLL_remove (co_head,
1674 * Iterator called on each result obtained for a DHT
1675 * operation that expects a reply
1677 * @param cls closure with the `struct GNS_ResolverHandle`
1678 * @param exp when will this value expire
1679 * @param key key of the result
1680 * @param get_path peers on reply path (or NULL if not recorded)
1681 * [0] = datastore's first neighbor, [length - 1] = local peer
1682 * @param get_path_length number of entries in @a get_path
1683 * @param put_path peers on the PUT path (or NULL if not recorded)
1684 * [0] = origin, [length - 1] = datastore
1685 * @param put_path_length number of entries in @a put_path
1686 * @param type type of the result
1687 * @param size number of bytes in data
1688 * @param data pointer to the result data
1691 handle_dht_response (void *cls,
1692 struct GNUNET_TIME_Absolute exp,
1693 const struct GNUNET_HashCode *key,
1694 const struct GNUNET_PeerIdentity *get_path,
1695 unsigned int get_path_length,
1696 const struct GNUNET_PeerIdentity *put_path,
1697 unsigned int put_path_length,
1698 enum GNUNET_BLOCK_Type type,
1699 size_t size, const void *data)
1701 struct GNS_ResolverHandle *rh = cls;
1702 struct AuthorityChain *ac = rh->ac_tail;
1703 const struct GNUNET_NAMESTORE_Block *block;
1704 struct CacheOps *co;
1706 GNUNET_DHT_get_stop (rh->get_handle);
1707 rh->get_handle = NULL;
1708 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1709 rh->dht_heap_node = NULL;
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1711 "Handling response from the DHT\n");
1712 if (size < sizeof (struct GNUNET_NAMESTORE_Block))
1714 /* how did this pass DHT block validation!? */
1716 rh->proc (rh->proc_cls, 0, NULL);
1717 GNS_resolver_lookup_cancel (rh);
1722 ntohl (block->purpose.size) +
1723 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
1724 sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
1726 /* how did this pass DHT block validation!? */
1728 rh->proc (rh->proc_cls, 0, NULL);
1729 GNS_resolver_lookup_cancel (rh);
1733 GNUNET_NAMESTORE_block_decrypt (block,
1734 &ac->authority_info.gns_authority,
1736 &handle_gns_resolution_result,
1739 GNUNET_break_op (0); /* block was ill-formed */
1740 rh->proc (rh->proc_cls, 0, NULL);
1741 GNS_resolver_lookup_cancel (rh);
1744 /* Cache well-formed blocks */
1745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1746 "Caching response from the DHT in namestore\n");
1747 co = GNUNET_new (struct CacheOps);
1748 co->namestore_qe_cache = GNUNET_NAMESTORE_block_cache (namestore_handle,
1750 &namestore_cache_continuation,
1752 GNUNET_CONTAINER_DLL_insert (co_head,
1759 * Process a record that was stored in the namestore.
1761 * @param cls closure with the `struct GNS_ResolverHandle`
1762 * @param block block that was stored in the namestore
1765 handle_namestore_block_response (void *cls,
1766 const struct GNUNET_NAMESTORE_Block *block)
1768 struct GNS_ResolverHandle *rh = cls;
1769 struct GNS_ResolverHandle *rx;
1770 struct AuthorityChain *ac = rh->ac_tail;
1771 const char *label = ac->label;
1772 const struct GNUNET_CRYPTO_EcdsaPublicKey *auth = &ac->authority_info.gns_authority;
1773 struct GNUNET_HashCode query;
1775 GNUNET_NAMESTORE_query_from_public_key (auth,
1778 GNUNET_assert (NULL != rh->namestore_qe);
1779 rh->namestore_qe = NULL;
1780 if ( (GNUNET_NO == rh->only_cached) &&
1781 ( (NULL == block) ||
1782 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
1784 /* Namestore knows nothing; try DHT lookup */
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 "Starting DHT lookup for `%s' in zone %s\n",
1788 GNUNET_NAMESTORE_z2s (&ac->authority_info.gns_authority));
1789 GNUNET_assert (NULL == rh->get_handle);
1790 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1791 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1793 DHT_GNS_REPLICATION_LEVEL,
1794 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1796 &handle_dht_response, rh);
1797 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1799 GNUNET_TIME_absolute_get ().abs_value_us);
1800 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
1802 /* fail longest-standing DHT request */
1803 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
1804 GNUNET_assert (NULL != rx);
1805 rx->proc (rx->proc_cls, 0, NULL);
1806 GNS_resolver_lookup_cancel (rx);
1810 if ( (NULL == block) ||
1811 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
1813 /* DHT not permitted and no local result, fail */
1814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1815 "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
1817 GNUNET_NAMESTORE_z2s (&ac->authority_info.gns_authority));
1818 rh->proc (rh->proc_cls, 0, NULL);
1819 GNS_resolver_lookup_cancel (rh);
1822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1823 "Decrypting block from the namestore\n");
1825 GNUNET_NAMESTORE_block_decrypt (block,
1828 &handle_gns_resolution_result,
1831 GNUNET_break_op (0); /* block was ill-formed */
1832 rh->proc (rh->proc_cls, 0, NULL);
1833 GNS_resolver_lookup_cancel (rh);
1840 * Lookup tail of our authority chain in the namestore.
1842 * @param rh query we are processing
1845 recursive_gns_resolution_namestore (struct GNS_ResolverHandle *rh)
1847 struct AuthorityChain *ac = rh->ac_tail;
1848 struct GNUNET_HashCode query;
1850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1851 "Starting GNS resolution for `%s' in zone %s\n",
1853 GNUNET_NAMESTORE_z2s (&ac->authority_info.gns_authority));
1854 GNUNET_NAMESTORE_query_from_public_key (&ac->authority_info.gns_authority,
1857 rh->namestore_qe = GNUNET_NAMESTORE_lookup_block (namestore_handle,
1859 &handle_namestore_block_response,
1861 GNUNET_assert (NULL != rh->namestore_qe);
1866 * Task scheduled to continue with the resolution process.
1868 * @param cls the `struct GNS_ResolverHandle` of the resolution
1869 * @param tc task context
1872 recursive_resolution (void *cls,
1873 const struct GNUNET_SCHEDULER_TaskContext *tc)
1875 struct GNS_ResolverHandle *rh = cls;
1877 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
1878 if (MAX_RECURSION < rh->loop_limiter++)
1880 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1881 "Encountered unbounded recursion resolving `%s'\n",
1883 rh->proc (rh->proc_cls, 0, NULL);
1884 GNS_resolver_lookup_cancel (rh);
1887 if (GNUNET_YES == rh->ac_tail->gns_authority)
1888 recursive_gns_resolution_namestore (rh);
1890 recursive_dns_resolution (rh);
1895 * Begin the resolution process from 'name', starting with
1896 * the identification of the zone specified by 'name'.
1898 * @param rh resolution to perform
1901 start_resolver_lookup (struct GNS_ResolverHandle *rh)
1903 struct AuthorityChain *ac;
1908 if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
1909 (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
1910 ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
1911 (GNUNET_YES != is_zkey_tld (rh->name)) ) )
1913 /* use standard DNS lookup */
1916 switch (rh->record_type)
1918 case GNUNET_DNSPARSER_TYPE_A:
1921 case GNUNET_DNSPARSER_TYPE_AAAA:
1928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1929 "Doing standard DNS lookup for `%s'\n",
1931 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
1938 if (is_zkey_tld (rh->name))
1940 /* Name ends with ".zkey", try to replace authority zone with zkey
1942 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
1943 x = resolver_lookup_get_next_label (rh); /* will return 'x' coordinate */
1944 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
1945 GNUNET_asprintf (&pkey,
1951 GNUNET_CRYPTO_ecdsa_public_key_from_string (pkey,
1953 &rh->authority_zone)) )
1955 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1956 _("Hostname `%s' is not well-formed, resolution fails\n"),
1958 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
1960 GNUNET_free_non_null (x);
1961 GNUNET_free_non_null (y);
1966 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
1967 GNUNET_free (resolver_lookup_get_next_label (rh));
1969 ac = GNUNET_new (struct AuthorityChain);
1971 ac->label = resolver_lookup_get_next_label (rh);
1972 if (NULL == ac->label)
1973 /* name was just "gnu", so we default to label '+' */
1974 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
1975 ac->gns_authority = GNUNET_YES;
1976 ac->authority_info.gns_authority = rh->authority_zone;
1977 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1980 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1986 * Lookup of a record in a specific zone calls lookup result processor
1989 * @param zone the zone to perform the lookup in
1990 * @param record_type the record type to look up
1991 * @param name the name to look up
1992 * @param shorten_key a private key for use with PSEU import (can be NULL)
1993 * @param only_cached #GNUNET_NO to only check locally not DHT for performance
1994 * @param proc the processor to call on result
1995 * @param proc_cls the closure to pass to @a proc
1996 * @return handle to cancel operation
1998 struct GNS_ResolverHandle *
1999 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
2000 uint32_t record_type,
2002 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key,
2004 GNS_ResultProcessor proc, void *proc_cls)
2006 struct GNS_ResolverHandle *rh;
2008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2009 (NULL == shorten_key)
2010 ? "Starting lookup for `%s' with shortening disabled\n"
2011 : "Starting lookup for `%s' with shortening enabled\n",
2013 rh = GNUNET_new (struct GNS_ResolverHandle);
2014 GNUNET_CONTAINER_DLL_insert (rlh_head,
2017 rh->authority_zone = *zone;
2019 rh->proc_cls = proc_cls;
2020 rh->only_cached = only_cached;
2021 rh->record_type = record_type;
2022 rh->name = GNUNET_strdup (name);
2023 rh->name_resolution_pos = strlen (name);
2024 if (NULL != shorten_key)
2026 rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
2027 *rh->shorten_key = *shorten_key;
2029 start_resolver_lookup (rh);
2035 * Cancel active resolution (i.e. client disconnected).
2037 * @param rh resolution to abort
2040 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
2042 struct DnsResult *dr;
2043 struct AuthorityChain *ac;
2044 struct VpnContext *vpn_ctx;
2046 GNUNET_CONTAINER_DLL_remove (rlh_head,
2049 while (NULL != (ac = rh->ac_head))
2051 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
2054 GNUNET_free (ac->label);
2057 if (GNUNET_SCHEDULER_NO_TASK != rh->task_id)
2059 GNUNET_SCHEDULER_cancel (rh->task_id);
2060 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
2062 if (NULL != rh->get_handle)
2064 GNUNET_DHT_get_stop (rh->get_handle);
2065 rh->get_handle = NULL;
2067 if (NULL != rh->dht_heap_node)
2069 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
2070 rh->dht_heap_node = NULL;
2072 if (NULL != (vpn_ctx = rh->vpn_ctx))
2074 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
2075 GNUNET_free (vpn_ctx->rd_data);
2076 GNUNET_free (vpn_ctx);
2078 if (NULL != rh->dns_request)
2080 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
2081 rh->dns_request = NULL;
2083 if (NULL != rh->namestore_qe)
2085 GNUNET_NAMESTORE_cancel (rh->namestore_qe);
2086 rh->namestore_qe = NULL;
2088 if (NULL != rh->std_resolve)
2090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2091 "Canceling standard DNS resolution\n");
2092 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
2093 rh->std_resolve = NULL;
2095 while (NULL != (dr = rh->dns_result_head))
2097 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
2098 rh->dns_result_tail,
2102 GNUNET_free_non_null (rh->shorten_key);
2103 GNUNET_free (rh->name);
2108 /* ***************** Resolver initialization ********************* */
2112 * Initialize the resolver
2114 * @param nh the namestore handle
2115 * @param dht the dht handle
2116 * @param c configuration handle
2117 * @param max_bg_queries maximum number of parallel background queries in dht
2120 GNS_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
2121 struct GNUNET_DHT_Handle *dht,
2122 const struct GNUNET_CONFIGURATION_Handle *c,
2123 unsigned long long max_bg_queries)
2128 namestore_handle = nh;
2131 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2132 max_allowed_background_queries = max_bg_queries;
2134 GNUNET_CONFIGURATION_get_value_string (c,
2139 /* user did not specify DNS resolver, use 8.8.8.8 */
2140 dns_ip = GNUNET_strdup ("8.8.8.8");
2142 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
2143 GNUNET_free (dns_ip);
2144 vpn_handle = GNUNET_VPN_connect (cfg);
2152 GNS_resolver_done ()
2154 struct GNS_ResolverHandle *rh;
2155 struct CacheOps *co;
2157 /* abort active resolutions */
2158 while (NULL != (rh = rlh_head))
2160 rh->proc (rh->proc_cls, 0, NULL);
2161 GNS_resolver_lookup_cancel (rh);
2163 while (NULL != (co = co_head))
2165 GNUNET_CONTAINER_DLL_remove (co_head,
2168 GNUNET_NAMESTORE_cancel (co->namestore_qe_cache);
2171 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
2172 dht_lookup_heap = NULL;
2173 GNUNET_DNSSTUB_stop (dns_handle);
2175 GNUNET_VPN_disconnect (vpn_handle);
2178 namestore_handle = NULL;
2182 /* *************** common helper functions (do not really belong here) *********** */
2185 * Checks if @a name ends in ".TLD"
2187 * @param name the name to check
2188 * @param tld the TLD to check for
2189 * @return GNUNET_YES or GNUNET_NO
2192 is_tld (const char* name, const char* tld)
2196 if (strlen (name) <= strlen (tld))
2198 offset = strlen (name) - strlen (tld);
2199 if (0 != strcmp (name + offset, tld))
2205 /* end of gnunet-service-gns_resolver.c */