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 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 * Our handle to the namestore service
339 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
342 * Our handle to the vpn service
344 static struct GNUNET_VPN_Handle *vpn_handle;
347 * Resolver handle to the dht
349 static struct GNUNET_DHT_Handle *dht_handle;
352 * Handle to perform DNS lookups.
354 static struct GNUNET_DNSSTUB_Context *dns_handle;
357 * Heap for limiting parallel DHT lookups
359 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
362 * Maximum amount of parallel queries to the DHT
364 static unsigned long long max_allowed_background_queries;
367 * Head of resolver lookup list
369 static struct GNS_ResolverHandle *rlh_head;
372 * Tail of resolver lookup list
374 static struct GNS_ResolverHandle *rlh_tail;
377 * Global configuration.
379 static const struct GNUNET_CONFIGURATION_Handle *cfg;
383 * Check if name is in srv format (_x._y.xxx)
386 * @return #GNUNET_YES if true
389 is_srv (const char *name)
396 if (NULL == strstr (name, "._"))
399 ndup = GNUNET_strdup (name);
401 if (NULL == strtok (NULL, "."))
403 if (NULL == strtok (NULL, "."))
405 if (NULL != strtok (NULL, "."))
413 * Determine if this name is canonical (is a legal name in a zone, without delegation);
414 * note that we do not test that the name does not contain illegal characters, we only
415 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
416 * even though they consist of multiple labels.
419 * a.b.gnu = not canonical
421 * _foo._srv = canonical
422 * _f.bar = not canonical
424 * @param name the name to test
425 * @return #GNUNET_YES if canonical
428 is_canonical (const char *name)
433 if (NULL == strchr (name, '.'))
438 while (NULL != (dot = strchr (pos, '.')))
446 /* ************************** Resolution **************************** */
449 * Exands a name ending in .+ with the zone of origin.
451 * @param rh resolution context
452 * @param name name to modify (to be free'd or returned)
453 * @return updated name
456 translate_dot_plus (struct GNS_ResolverHandle *rh,
460 size_t s_len = strlen (name);
462 if (0 != strcmp (&name[s_len - 2],
464 return name; /* did not end in ".+" */
465 GNUNET_assert (GNUNET_YES == rh->ac_tail->gns_authority);
466 GNUNET_asprintf (&ret,
470 GNUNET_NAMESTORE_pkey_to_zkey (&rh->ac_tail->authority_info.gns_authority));
477 * Task scheduled to asynchronously fail a resolution.
479 * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
480 * @param tc task context
483 fail_resolution (void *cls,
484 const struct GNUNET_SCHEDULER_TaskContext *tc)
486 struct GNS_ResolverHandle *rh = cls;
488 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
489 rh->proc (rh->proc_cls, 0, NULL);
490 GNS_resolver_lookup_cancel (rh);
494 #if (defined WINDOWS) || (defined DARWIN)
495 /* Don't have this on W32, here's a naive implementation
496 * Was somehow removed on OS X ... */
498 memrchr (const void *s,
502 const unsigned char *ucs = s;
505 for (i = n - 1; i >= 0; i--)
506 if (c == (int) ucs[i])
507 return (void *) &ucs[i];
514 * Get the next, rightmost label from the name that we are trying to resolve,
515 * and update the resolution position accordingly.
517 * @param rh handle to the resolution operation to get the next label from
518 * @return NULL if there are no more labels
521 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
527 if (0 == rh->name_resolution_pos)
529 dot = memrchr (rh->name, (int) '.', rh->name_resolution_pos);
532 /* done, this was the last one */
533 len = rh->name_resolution_pos;
535 rh->name_resolution_pos = 0;
539 /* advance by one label */
540 len = rh->name_resolution_pos - (dot - rh->name) - 1;
542 rh->name_resolution_pos = dot - rh->name;
544 return GNUNET_strndup (rp, len);
549 * Gives the cummulative result obtained to the callback and clean up the request.
551 * @param rh resolution process that has culminated in a result
554 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
556 struct DnsResult *pos;
561 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
564 struct GNUNET_NAMESTORE_RecordData rd[n];
567 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
569 rd[i].data = pos->data;
570 rd[i].data_size = pos->data_size;
571 rd[i].record_type = pos->record_type;
572 if (0 == pos->expiration_time)
574 rd[i].flags = GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
575 rd[i].expiration_time = 0;
579 rd[i].flags = GNUNET_NAMESTORE_RF_NONE;
580 rd[i].expiration_time = pos->expiration_time;
584 GNUNET_assert (i == n);
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 "Transmitting standard DNS result with %u records\n",
588 rh->proc (rh->proc_cls,
592 GNS_resolver_lookup_cancel (rh);
597 * Add a result from DNS to the records to be returned to the application.
599 * @param rh resolution request to extend with a result
600 * @param expiration_time expiration time for the answer
601 * @param record_type DNS record type of the answer
602 * @param data_size number of bytes in @a data
603 * @param data binary data to return in DNS record
606 add_dns_result (struct GNS_ResolverHandle *rh,
607 uint64_t expiration_time,
608 uint32_t record_type,
612 struct DnsResult *res;
614 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
615 res->expiration_time = expiration_time;
616 res->data_size = data_size;
617 res->record_type = record_type;
619 memcpy (&res[1], data, data_size);
620 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
627 * We had to do a DNS lookup. Convert the result (if any) and return
630 * @param cls closure with the 'struct GNS_ResolverHandle'
631 * @param addr one of the addresses of the host, NULL for the last address
632 * @param addrlen length of the address
635 handle_dns_result (void *cls,
636 const struct sockaddr *addr,
639 struct GNS_ResolverHandle *rh = cls;
640 const struct sockaddr_in *sa4;
641 const struct sockaddr_in6 *sa6;
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Received %u bytes of DNS IP data\n",
648 rh->std_resolve = NULL;
649 transmit_lookup_dns_result (rh);
652 switch (addr->sa_family)
655 sa4 = (const struct sockaddr_in *) addr;
657 0 /* expiration time is unknown */,
658 GNUNET_DNSPARSER_TYPE_A,
659 sizeof (struct in_addr),
663 sa6 = (const struct sockaddr_in6 *) addr;
665 0 /* expiration time is unknown */,
666 GNUNET_DNSPARSER_TYPE_AAAA,
667 sizeof (struct in6_addr),
678 * Task scheduled to continue with the resolution process.
680 * @param cls the 'struct GNS_ResolverHandle' of the resolution
681 * @param tc task context
684 recursive_resolution (void *cls,
685 const struct GNUNET_SCHEDULER_TaskContext *tc);
689 * Begin the resolution process from 'name', starting with
690 * the identification of the zone specified by 'name'.
692 * @param rh resolution to perform
695 start_resolver_lookup (struct GNS_ResolverHandle *rh);
699 * Function called with the result of a DNS resolution.
701 * @param cls the request handle of the resolution that
702 * we were attempting to make
703 * @param rs socket that received the response
704 * @param dns dns response, never NULL
705 * @param dns_len number of bytes in @a dns
708 dns_result_parser (void *cls,
709 struct GNUNET_DNSSTUB_RequestSocket *rs,
710 const struct GNUNET_TUN_DnsHeader *dns,
713 struct GNS_ResolverHandle *rh = cls;
714 struct GNUNET_DNSPARSER_Packet *p;
715 const struct GNUNET_DNSPARSER_Record *rec;
716 unsigned int rd_count;
719 rh->dns_request = NULL;
720 GNUNET_SCHEDULER_cancel (rh->task_id);
721 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
722 p = GNUNET_DNSPARSER_parse ((const char *) dns,
726 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
727 _("Failed to parse DNS response\n"));
728 rh->proc (rh->proc_cls, 0, NULL);
729 GNS_resolver_lookup_cancel (rh);
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 "Received DNS response for `%s' with %u answers\n",
735 (unsigned int) p->num_answers);
736 if ( (p->num_answers > 0) &&
737 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
738 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
740 GNUNET_free (rh->name);
741 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
742 start_resolver_lookup (rh);
743 GNUNET_DNSPARSER_free_packet (p);
746 /* FIXME: add DNAME support */
748 /* convert from (parsed) DNS to (binary) GNS format! */
749 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
751 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
753 char buf[UINT16_MAX];
758 memset (rd, 0, sizeof (rd));
759 for (i=0;i<rd_count;i++)
761 if (i < p->num_answers)
762 rec = &p->answers[i];
763 else if (i < p->num_answers + p->num_authority_records)
764 rec = &p->authority_records[i - p->num_answers];
766 rec = &p->authority_records[i - p->num_answers - p->num_authority_records];
767 /* As we copied the full DNS name to 'rh->ac_tail->label', this
768 should be the correct check to see if this record is actually
769 a record for our label... */
770 if (0 != strcmp (rec->name,
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
774 "Dropping record `%s', does not match desired name `%s'\n",
780 rd[i - skip].record_type = rec->type;
781 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
784 case GNUNET_DNSPARSER_TYPE_A:
785 if (rec->data.raw.data_len != sizeof (struct in_addr))
791 rd[i - skip].data_size = rec->data.raw.data_len;
792 rd[i - skip].data = rec->data.raw.data;
794 case GNUNET_DNSPARSER_TYPE_AAAA:
795 if (rec->data.raw.data_len != sizeof (struct in6_addr))
801 rd[i - skip].data_size = rec->data.raw.data_len;
802 rd[i - skip].data = rec->data.raw.data;
804 case GNUNET_DNSPARSER_TYPE_CNAME:
805 case GNUNET_DNSPARSER_TYPE_PTR:
806 case GNUNET_DNSPARSER_TYPE_NS:
808 GNUNET_DNSPARSER_builder_add_name (buf,
818 case GNUNET_DNSPARSER_TYPE_SOA:
820 GNUNET_DNSPARSER_builder_add_soa (buf,
830 case GNUNET_DNSPARSER_TYPE_MX:
832 GNUNET_DNSPARSER_builder_add_mx (buf,
842 case GNUNET_DNSPARSER_TYPE_SRV:
844 GNUNET_DNSPARSER_builder_add_srv (buf,
855 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
856 _("Skipping record of unsupported type %d\n"),
862 rh->proc (rh->proc_cls, rd_count - skip, rd);
863 GNS_resolver_lookup_cancel (rh);
865 GNUNET_DNSPARSER_free_packet (p);
870 * Perform recursive DNS resolution. Asks the given DNS resolver to
871 * resolve "rh->dns_name", possibly recursively proceeding following
872 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
873 * we find the answer.
875 * @param rh resolution information
878 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
880 struct AuthorityChain *ac;
882 struct GNUNET_DNSPARSER_Query *query;
883 struct GNUNET_DNSPARSER_Packet *p;
885 size_t dns_request_length;
888 GNUNET_assert (NULL != ac);
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
890 "Starting DNS lookup for `%s'\n",
892 GNUNET_assert (GNUNET_NO == ac->gns_authority);
893 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
896 sa_len = sizeof (struct sockaddr_in);
899 sa_len = sizeof (struct sockaddr_in6);
903 rh->proc (rh->proc_cls, 0, NULL);
904 GNS_resolver_lookup_cancel (rh);
907 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
908 query->name = GNUNET_strdup (ac->label);
909 query->type = rh->record_type;
910 query->class = GNUNET_TUN_DNS_CLASS_INTERNET;
911 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
914 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
916 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
917 p->flags.recursion_desired = 1;
919 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
922 rh->proc (rh->proc_cls, 0, NULL);
923 GNS_resolver_lookup_cancel (rh);
927 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
928 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
934 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
938 GNUNET_free (dns_request);
939 GNUNET_DNSPARSER_free_packet (p);
944 * We encountered a CNAME record during our resolution.
945 * Merge it into our chain.
947 * @param rh resolution we are performing
948 * @param cname value of the cname record we got for the current
949 * authority chain tail
952 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
957 struct AuthorityChain *ac;
959 nlen = strlen (cname);
964 /* CNAME resolution continues relative to current domain */
965 if (0 == rh->name_resolution_pos)
967 res = GNUNET_strndup (cname, nlen - 2);
968 rh->name_resolution_pos = nlen - 2;
972 GNUNET_asprintf (&res,
974 (int) rh->name_resolution_pos,
978 rh->name_resolution_pos = strlen (res);
980 GNUNET_free (rh->name);
982 ac = GNUNET_new (struct AuthorityChain);
984 ac->gns_authority = GNUNET_YES;
985 ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
986 ac->label = resolver_lookup_get_next_label (rh);
987 /* tigger shortening */
988 if (NULL != rh->shorten_key)
989 GNS_shorten_start (rh->ac_tail->label,
990 &ac->authority_info.gns_authority,
993 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
996 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1000 /* name is absolute, start from the beginning */
1001 GNUNET_free (rh->name);
1002 rh->name = GNUNET_strdup (cname);
1003 start_resolver_lookup (rh);
1008 * Process a records that were decrypted from a block.
1010 * @param cls closure with the 'struct GNS_ResolverHandle'
1011 * @param rd_count number of entries in @a rd array
1012 * @param rd array of records with data to store
1015 handle_gns_resolution_result (void *cls,
1016 unsigned int rd_count,
1017 const struct GNUNET_NAMESTORE_RecordData *rd);
1021 * Callback invoked from the VPN service once a redirection is
1022 * available. Provides the IP address that can now be used to
1023 * reach the requested destination. Replaces the "VPN" record
1024 * with the respective A/AAAA record and continues processing.
1026 * @param cls closure
1027 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1028 * will match 'result_af' from the request
1029 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1030 * that the VPN allocated for the redirection;
1031 * traffic to this IP will now be redirected to the
1032 * specified target peer; NULL on error
1035 vpn_allocation_cb (void *cls,
1037 const void *address)
1039 struct VpnContext *vpn_ctx = cls;
1040 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1041 struct GNUNET_NAMESTORE_RecordData rd[vpn_ctx->rd_count];
1044 vpn_ctx->vpn_request = NULL;
1046 GNUNET_assert (GNUNET_OK ==
1047 GNUNET_NAMESTORE_records_deserialize (vpn_ctx->rd_data_size,
1051 for (i=0;i<vpn_ctx->rd_count;i++)
1053 if (GNUNET_NAMESTORE_TYPE_VPN == rd[i].record_type)
1058 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1059 rd[i].data_size = sizeof (struct in_addr);
1060 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1062 rd[i].data = address;
1065 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1066 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1068 rd[i].data = address;
1069 rd[i].data_size = sizeof (struct in6_addr);
1077 GNUNET_assert (i < vpn_ctx->rd_count);
1078 handle_gns_resolution_result (rh,
1081 GNUNET_free (vpn_ctx->rd_data);
1082 GNUNET_free (vpn_ctx);
1087 * Process a records that were decrypted from a block.
1089 * @param cls closure with the `struct GNS_ResolverHandle`
1090 * @param rd_count number of entries in @a rd array
1091 * @param rd array of records with data to store
1094 handle_gns_resolution_result (void *cls,
1095 unsigned int rd_count,
1096 const struct GNUNET_NAMESTORE_RecordData *rd)
1098 struct GNS_ResolverHandle *rh = cls;
1099 struct AuthorityChain *ac;
1102 struct sockaddr *sa;
1103 struct sockaddr_in v4;
1104 struct sockaddr_in6 v6;
1107 struct VpnContext *vpn_ctx;
1108 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1110 struct GNUNET_HashCode vhash;
1112 char scratch[UINT16_MAX];
1114 size_t scratch_start;
1116 struct GNUNET_NAMESTORE_RecordData rd_new[rd_count];
1117 unsigned int rd_off;
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1120 "Resolution succeeded for `%s' in zone %s, got %u records\n",
1122 GNUNET_NAMESTORE_z2s (&rh->ac_tail->authority_info.gns_authority),
1124 if (0 == rh->name_resolution_pos)
1126 /* top-level match, are we done yet? */
1127 if ( (rd_count > 0) &&
1128 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1129 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1132 cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
1135 if ( (NULL == cname) ||
1136 (off != rd[0].data_size) )
1138 GNUNET_break_op (0);
1139 rh->proc (rh->proc_cls, 0, NULL);
1140 GNS_resolver_lookup_cancel (rh);
1143 handle_gns_cname_result (rh,
1145 GNUNET_free (cname);
1148 /* If A/AAAA was requested, but we got a VPN
1149 record, we convert it to A/AAAA using GNUnet VPN */
1150 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1151 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1153 for (i=0;i<rd_count;i++)
1155 if (GNUNET_NAMESTORE_TYPE_VPN == rd[i].record_type)
1157 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1158 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) <
1161 GNUNET_break_op (0);
1162 rh->proc (rh->proc_cls, 0, NULL);
1163 GNS_resolver_lookup_cancel (rh);
1166 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1167 vname = (const char *) &vpn[1];
1168 if ('\0' != vname[rd[i].data_size - 1 - sizeof (struct GNUNET_TUN_GnsVpnRecord)])
1170 GNUNET_break_op (0);
1171 rh->proc (rh->proc_cls, 0, NULL);
1172 GNS_resolver_lookup_cancel (rh);
1175 GNUNET_CRYPTO_hash (vname,
1176 strlen (vname), // FIXME: +1?
1178 vpn_ctx = GNUNET_new (struct VpnContext);
1179 rh->vpn_ctx = vpn_ctx;
1181 vpn_ctx->rd_data_size = GNUNET_NAMESTORE_records_get_size (rd_count,
1183 vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
1184 (void) GNUNET_NAMESTORE_records_serialize (rd_count,
1186 vpn_ctx->rd_data_size,
1188 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1193 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1200 /* convert relative names in record values to absolute names,
1201 using 'scratch' array for memory allocations */
1204 for (i=0;i<rd_count;i++)
1206 rd_new[rd_off] = rd[i];
1207 /* Check if the embedded name(s) end in "+", and if so,
1208 replace the "+" with the zone at "ac_tail", changing the name
1209 to a ".zkey". The name is allocated on the 'scratch' array,
1210 so we can free it afterwards. */
1211 switch (rd[i].record_type)
1213 case GNUNET_DNSPARSER_TYPE_CNAME:
1218 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1221 if ( (NULL == cname) ||
1222 (off != rd[i].data_size) )
1224 GNUNET_break_op (0); /* record not well-formed */
1228 cname = translate_dot_plus (rh, cname);
1229 scratch_start = scratch_off;
1231 GNUNET_DNSPARSER_builder_add_name (scratch,
1240 rd_new[rd_off].data = &scratch[scratch_start];
1241 rd_new[rd_off].data_size = scratch_off - scratch_start;
1245 GNUNET_free_non_null (cname);
1248 case GNUNET_DNSPARSER_TYPE_SOA:
1250 struct GNUNET_DNSPARSER_SoaRecord *soa;
1253 soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
1256 if ( (NULL == soa) ||
1257 (off != rd[i].data_size) )
1259 GNUNET_break_op (0); /* record not well-formed */
1263 soa->mname = translate_dot_plus (rh, soa->mname);
1264 soa->rname = translate_dot_plus (rh, soa->rname);
1265 scratch_start = scratch_off;
1267 GNUNET_DNSPARSER_builder_add_soa (scratch,
1276 rd_new[rd_off].data = &scratch[scratch_start];
1277 rd_new[rd_off].data_size = scratch_off - scratch_start;
1282 GNUNET_DNSPARSER_free_soa (soa);
1285 case GNUNET_DNSPARSER_TYPE_MX:
1287 struct GNUNET_DNSPARSER_MxRecord *mx;
1290 mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
1293 if ( (NULL == mx) ||
1294 (off != rd[i].data_size) )
1296 GNUNET_break_op (0); /* record not well-formed */
1300 mx->mxhost = translate_dot_plus (rh, mx->mxhost);
1301 scratch_start = scratch_off;
1303 GNUNET_DNSPARSER_builder_add_mx (scratch,
1312 rd_new[rd_off].data = &scratch[scratch_start];
1313 rd_new[rd_off].data_size = scratch_off - scratch_start;
1318 GNUNET_DNSPARSER_free_mx (mx);
1321 case GNUNET_DNSPARSER_TYPE_SRV:
1323 struct GNUNET_DNSPARSER_SrvRecord *srv;
1326 /* FIXME: passing rh->name here is is not necessarily what we want
1327 (SRV support not finished) */
1328 srv = GNUNET_DNSPARSER_parse_srv (rh->name,
1332 if ( (NULL == srv) ||
1333 (off != rd[i].data_size) )
1335 GNUNET_break_op (0); /* record not well-formed */
1339 srv->domain_name = translate_dot_plus (rh, srv->domain_name);
1340 srv->target = translate_dot_plus (rh, srv->target);
1341 scratch_start = scratch_off;
1343 GNUNET_DNSPARSER_builder_add_srv (scratch,
1352 rd_new[rd_off].data = &scratch[scratch_start];
1353 rd_new[rd_off].data_size = scratch_off - scratch_start;
1358 GNUNET_DNSPARSER_free_srv (srv);
1361 case GNUNET_NAMESTORE_TYPE_PKEY:
1362 /* tigger shortening */
1363 if (NULL != rh->shorten_key)
1365 struct GNUNET_CRYPTO_EccPublicSignKey pub;
1367 if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EccPublicSignKey))
1369 GNUNET_break_op (0);
1372 memcpy (&pub, rd[i].data, rd[i].data_size);
1373 GNS_shorten_start (rh->ac_tail->label,
1385 /* yes, we are done, return result */
1386 rh->proc (rh->proc_cls, rd_off, rd_new);
1387 GNS_resolver_lookup_cancel (rh);
1390 /* need to recurse, check if we can */
1391 for (i=0;i<rd_count;i++)
1393 switch (rd[i].record_type)
1395 case GNUNET_NAMESTORE_TYPE_PKEY:
1396 /* delegation to another zone */
1397 if (sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) !=
1400 GNUNET_break_op (0);
1401 rh->proc (rh->proc_cls, 0, NULL);
1402 GNS_resolver_lookup_cancel (rh);
1405 /* expand authority chain */
1406 ac = GNUNET_new (struct AuthorityChain);
1408 ac->gns_authority = GNUNET_YES;
1409 memcpy (&ac->authority_info.gns_authority,
1411 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey));
1412 ac->label = resolver_lookup_get_next_label (rh);
1413 /* tigger shortening */
1414 if (NULL != rh->shorten_key)
1415 GNS_shorten_start (rh->ac_tail->label,
1416 &ac->authority_info.gns_authority,
1418 /* add AC to tail */
1419 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1423 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1426 case GNUNET_DNSPARSER_TYPE_NS:
1429 /* resolution continues within DNS */
1430 if (GNUNET_DNSPARSER_MAX_NAME_LENGTH < rd[i].data_size)
1432 GNUNET_break_op (0);
1433 rh->proc (rh->proc_cls, 0, NULL);
1434 GNS_resolver_lookup_cancel (rh);
1437 /* find associated A/AAAA record */
1440 for (j=0;j<rd_count;j++)
1442 switch (rd[j].record_type)
1444 case GNUNET_DNSPARSER_TYPE_A:
1445 if (sizeof (struct in_addr) != rd[j].data_size)
1447 GNUNET_break_op (0);
1448 rh->proc (rh->proc_cls, 0, NULL);
1449 GNS_resolver_lookup_cancel (rh);
1452 /* FIXME: might want to check if we support IPv4 here,
1453 and otherwise skip this one and hope we find another */
1454 memset (&v4, 0, sizeof (v4));
1455 sa_len = sizeof (v4);
1456 v4.sin_family = AF_INET;
1457 v4.sin_port = htons (53);
1458 #if HAVE_SOCKADDR_IN_SIN_LEN
1459 v4.sin_len = (u_char) sa_len;
1461 memcpy (&v4.sin_addr,
1463 sizeof (struct in_addr));
1464 sa = (struct sockaddr *) &v4;
1466 case GNUNET_DNSPARSER_TYPE_AAAA:
1467 if (sizeof (struct in6_addr) != rd[j].data_size)
1469 GNUNET_break_op (0);
1470 rh->proc (rh->proc_cls, 0, NULL);
1471 GNS_resolver_lookup_cancel (rh);
1474 /* FIXME: might want to check if we support IPv6 here,
1475 and otherwise skip this one and hope we find another */
1476 memset (&v6, 0, sizeof (v6));
1477 sa_len = sizeof (v6);
1478 v6.sin6_family = AF_INET6;
1479 v6.sin6_port = htons (53);
1480 #if HAVE_SOCKADDR_IN_SIN_LEN
1481 v6.sin6_len = (u_char) sa_len;
1483 memcpy (&v6.sin6_addr,
1485 sizeof (struct in6_addr));
1486 sa = (struct sockaddr *) &v6;
1496 /* we cannot continue; NS without A/AAAA */
1497 rh->proc (rh->proc_cls, 0, NULL);
1498 GNS_resolver_lookup_cancel (rh);
1501 /* expand authority chain */
1502 ac = GNUNET_new (struct AuthorityChain);
1505 ns = GNUNET_DNSPARSER_parse_name (rd[i].data,
1508 if ( (NULL == ns) ||
1509 (off != rd[i].data_size) )
1511 GNUNET_break_op (0); /* record not well-formed */
1512 rh->proc (rh->proc_cls, 0, NULL);
1513 GNS_resolver_lookup_cancel (rh);
1516 strcpy (ac->authority_info.dns_authority.name,
1518 memcpy (&ac->authority_info.dns_authority.dns_ip,
1521 /* for DNS recursion, the label is the full DNS name,
1522 created from the remainder of the GNS name and the
1523 name in the NS record */
1524 GNUNET_asprintf (&ac->label,
1526 (int) rh->name_resolution_pos,
1530 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1533 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1535 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1536 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1538 rh->proc (rh->proc_cls, 0, NULL);
1539 GNS_resolver_lookup_cancel (rh);
1543 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1547 case GNUNET_DNSPARSER_TYPE_CNAME:
1552 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1555 if ( (NULL == cname) ||
1556 (off != rd[i].data_size) )
1558 GNUNET_break_op (0); /* record not well-formed */
1559 rh->proc (rh->proc_cls, 0, NULL);
1560 GNS_resolver_lookup_cancel (rh);
1563 handle_gns_cname_result (rh,
1565 GNUNET_free (cname);
1568 /* FIXME: handle DNAME */
1574 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1575 _("GNS lookup recursion failed (no delegation record found)\n"));
1576 rh->proc (rh->proc_cls, 0, NULL);
1577 GNS_resolver_lookup_cancel (rh);
1582 * Function called once the namestore has completed the request for
1585 * @param cls closure with the 'struct GNS_ResolverHandle'
1586 * @param success #GNUNET_OK on success
1587 * @param emsg error message
1590 namestore_cache_continuation (void *cls,
1594 struct GNS_ResolverHandle *rh = cls;
1596 rh->namestore_qe = NULL;
1598 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1599 _("Failed to cache GNS resolution: %s\n"),
1605 * Iterator called on each result obtained for a DHT
1606 * operation that expects a reply
1608 * @param cls closure with the `struct GNS_ResolverHandle`
1609 * @param exp when will this value expire
1610 * @param key key of the result
1611 * @param get_path peers on reply path (or NULL if not recorded)
1612 * [0] = datastore's first neighbor, [length - 1] = local peer
1613 * @param get_path_length number of entries in @a get_path
1614 * @param put_path peers on the PUT path (or NULL if not recorded)
1615 * [0] = origin, [length - 1] = datastore
1616 * @param put_path_length number of entries in @a put_path
1617 * @param type type of the result
1618 * @param size number of bytes in data
1619 * @param data pointer to the result data
1622 handle_dht_response (void *cls,
1623 struct GNUNET_TIME_Absolute exp,
1624 const struct GNUNET_HashCode *key,
1625 const struct GNUNET_PeerIdentity *get_path,
1626 unsigned int get_path_length,
1627 const struct GNUNET_PeerIdentity *put_path,
1628 unsigned int put_path_length,
1629 enum GNUNET_BLOCK_Type type,
1630 size_t size, const void *data)
1632 struct GNS_ResolverHandle *rh = cls;
1633 struct AuthorityChain *ac = rh->ac_tail;
1634 const struct GNUNET_NAMESTORE_Block *block;
1636 GNUNET_DHT_get_stop (rh->get_handle);
1637 rh->get_handle = NULL;
1638 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1639 rh->dht_heap_node = NULL;
1640 if (size < sizeof (struct GNUNET_NAMESTORE_Block))
1642 /* how did this pass DHT block validation!? */
1644 rh->proc (rh->proc_cls, 0, NULL);
1645 GNS_resolver_lookup_cancel (rh);
1650 ntohl (block->purpose.size) +
1651 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) +
1652 sizeof (struct GNUNET_CRYPTO_EccSignature))
1654 /* how did this pass DHT block validation!? */
1656 rh->proc (rh->proc_cls, 0, NULL);
1657 GNS_resolver_lookup_cancel (rh);
1661 GNUNET_NAMESTORE_block_decrypt (block,
1662 &ac->authority_info.gns_authority,
1664 &handle_gns_resolution_result,
1667 GNUNET_break_op (0); /* block was ill-formed */
1668 rh->proc (rh->proc_cls, 0, NULL);
1669 GNS_resolver_lookup_cancel (rh);
1672 /* Cache well-formed blocks */
1673 rh->namestore_qe = GNUNET_NAMESTORE_block_cache (namestore_handle,
1675 &namestore_cache_continuation,
1681 * Process a record that was stored in the namestore.
1683 * @param cls closure with the `struct GNS_ResolverHandle`
1684 * @param block block that was stored in the namestore
1687 handle_namestore_block_response (void *cls,
1688 const struct GNUNET_NAMESTORE_Block *block)
1690 struct GNS_ResolverHandle *rh = cls;
1691 struct GNS_ResolverHandle *rx;
1692 struct AuthorityChain *ac = rh->ac_tail;
1693 const char *label = ac->label;
1694 const struct GNUNET_CRYPTO_EccPublicSignKey *auth = &ac->authority_info.gns_authority;
1695 struct GNUNET_HashCode query;
1697 GNUNET_NAMESTORE_query_from_public_key (auth,
1700 rh->namestore_qe = NULL;
1701 if ( (GNUNET_NO == rh->only_cached) &&
1702 ( (NULL == block) ||
1703 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
1705 /* Namestore knows nothing; try DHT lookup */
1706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1707 "Starting DHT lookup for `%s' in zone %s\n",
1709 GNUNET_NAMESTORE_z2s (&ac->authority_info.gns_authority));
1710 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1711 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1713 DHT_GNS_REPLICATION_LEVEL,
1714 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1716 &handle_dht_response, rh);
1717 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1719 GNUNET_TIME_absolute_get ().abs_value_us);
1720 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
1722 /* fail longest-standing DHT request */
1723 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
1724 GNUNET_assert (NULL != rx);
1725 rx->proc (rx->proc_cls, 0, NULL);
1726 GNS_resolver_lookup_cancel (rx);
1730 if ( (NULL == block) ||
1731 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
1733 /* DHT not permitted and no local result, fail */
1734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1735 "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
1737 GNUNET_NAMESTORE_z2s (&ac->authority_info.gns_authority));
1738 rh->proc (rh->proc_cls, 0, NULL);
1739 GNS_resolver_lookup_cancel (rh);
1743 GNUNET_NAMESTORE_block_decrypt (block,
1746 &handle_gns_resolution_result,
1749 GNUNET_break_op (0); /* block was ill-formed */
1750 rh->proc (rh->proc_cls, 0, NULL);
1751 GNS_resolver_lookup_cancel (rh);
1758 * Lookup tail of our authority chain in the namestore.
1760 * @param rh query we are processing
1763 recursive_gns_resolution_namestore (struct GNS_ResolverHandle *rh)
1765 struct AuthorityChain *ac = rh->ac_tail;
1766 struct GNUNET_HashCode query;
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1769 "Starting GNS resolution for `%s' in zone %s\n",
1771 GNUNET_NAMESTORE_z2s (&ac->authority_info.gns_authority));
1772 GNUNET_NAMESTORE_query_from_public_key (&ac->authority_info.gns_authority,
1775 rh->namestore_qe = GNUNET_NAMESTORE_lookup_block (namestore_handle,
1777 &handle_namestore_block_response,
1783 * Task scheduled to continue with the resolution process.
1785 * @param cls the `struct GNS_ResolverHandle` of the resolution
1786 * @param tc task context
1789 recursive_resolution (void *cls,
1790 const struct GNUNET_SCHEDULER_TaskContext *tc)
1792 struct GNS_ResolverHandle *rh = cls;
1794 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
1795 if (MAX_RECURSION < rh->loop_limiter++)
1797 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1798 "Encountered unbounded recursion resolving `%s'\n",
1800 rh->proc (rh->proc_cls, 0, NULL);
1801 GNS_resolver_lookup_cancel (rh);
1804 if (GNUNET_YES == rh->ac_tail->gns_authority)
1805 recursive_gns_resolution_namestore (rh);
1807 recursive_dns_resolution (rh);
1812 * Begin the resolution process from 'name', starting with
1813 * the identification of the zone specified by 'name'.
1815 * @param rh resolution to perform
1818 start_resolver_lookup (struct GNS_ResolverHandle *rh)
1820 struct AuthorityChain *ac;
1825 if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
1826 (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
1827 ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
1828 (GNUNET_YES != is_zkey_tld (rh->name)) ) )
1830 /* use standard DNS lookup */
1833 switch (rh->record_type)
1835 case GNUNET_DNSPARSER_TYPE_A:
1838 case GNUNET_DNSPARSER_TYPE_AAAA:
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1846 "Doing standard DNS lookup for `%s'\n",
1848 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
1855 if (is_zkey_tld (rh->name))
1857 /* Name ends with ".zkey", try to replace authority zone with zkey
1859 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
1860 x = resolver_lookup_get_next_label (rh); /* will return 'x' coordinate */
1861 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
1862 GNUNET_asprintf (&pkey,
1868 GNUNET_CRYPTO_ecc_public_sign_key_from_string (pkey,
1870 &rh->authority_zone)) )
1872 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1873 _("Hostname `%s' is not well-formed, resolution fails\n"),
1875 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
1877 GNUNET_free_non_null (x);
1878 GNUNET_free_non_null (y);
1883 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
1884 GNUNET_free (resolver_lookup_get_next_label (rh));
1886 ac = GNUNET_new (struct AuthorityChain);
1888 ac->label = resolver_lookup_get_next_label (rh);
1889 if (NULL == ac->label)
1890 /* name was just "gnu", so we default to label '+' */
1891 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
1892 ac->gns_authority = GNUNET_YES;
1893 ac->authority_info.gns_authority = rh->authority_zone;
1894 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1897 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1903 * Lookup of a record in a specific zone calls lookup result processor
1906 * @param zone the zone to perform the lookup in
1907 * @param record_type the record type to look up
1908 * @param name the name to look up
1909 * @param shorten_key a private key for use with PSEU import (can be NULL)
1910 * @param only_cached #GNUNET_NO to only check locally not DHT for performance
1911 * @param proc the processor to call on result
1912 * @param proc_cls the closure to pass to @a proc
1913 * @return handle to cancel operation
1915 struct GNS_ResolverHandle *
1916 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EccPublicSignKey *zone,
1917 uint32_t record_type,
1919 const struct GNUNET_CRYPTO_EccPrivateKey *shorten_key,
1921 GNS_ResultProcessor proc, void *proc_cls)
1923 struct GNS_ResolverHandle *rh;
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1926 "Starting lookup for `%s'\n",
1928 rh = GNUNET_new (struct GNS_ResolverHandle);
1929 GNUNET_CONTAINER_DLL_insert (rlh_head,
1932 rh->authority_zone = *zone;
1934 rh->proc_cls = proc_cls;
1935 rh->only_cached = only_cached;
1936 rh->record_type = record_type;
1937 rh->name = GNUNET_strdup (name);
1938 rh->name_resolution_pos = strlen (name);
1939 if (NULL != shorten_key)
1941 rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
1942 *rh->shorten_key = *shorten_key;
1944 start_resolver_lookup (rh);
1950 * Cancel active resolution (i.e. client disconnected).
1952 * @param rh resolution to abort
1955 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
1957 struct DnsResult *dr;
1958 struct AuthorityChain *ac;
1959 struct VpnContext *vpn_ctx;
1961 GNUNET_CONTAINER_DLL_remove (rlh_head,
1964 while (NULL != (ac = rh->ac_head))
1966 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
1969 GNUNET_free (ac->label);
1972 if (GNUNET_SCHEDULER_NO_TASK != rh->task_id)
1974 GNUNET_SCHEDULER_cancel (rh->task_id);
1975 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
1977 if (NULL != rh->get_handle)
1979 GNUNET_DHT_get_stop (rh->get_handle);
1980 rh->get_handle = NULL;
1982 if (NULL != rh->dht_heap_node)
1984 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1985 rh->dht_heap_node = NULL;
1987 if (NULL != (vpn_ctx = rh->vpn_ctx))
1989 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
1990 GNUNET_free (vpn_ctx->rd_data);
1991 GNUNET_free (vpn_ctx);
1993 if (NULL != rh->dns_request)
1995 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
1996 rh->dns_request = NULL;
1998 if (NULL != rh->namestore_qe)
2000 GNUNET_NAMESTORE_cancel (rh->namestore_qe);
2001 rh->namestore_qe = NULL;
2003 if (NULL != rh->std_resolve)
2005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2006 "Canceling standard DNS resolution\n");
2007 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
2008 rh->std_resolve = NULL;
2010 while (NULL != (dr = rh->dns_result_head))
2012 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
2013 rh->dns_result_tail,
2017 GNUNET_free_non_null (rh->shorten_key);
2018 GNUNET_free (rh->name);
2023 /* ***************** Resolver initialization ********************* */
2027 * Initialize the resolver
2029 * @param nh the namestore handle
2030 * @param dht the dht handle
2031 * @param c configuration handle
2032 * @param max_bg_queries maximum number of parallel background queries in dht
2035 GNS_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
2036 struct GNUNET_DHT_Handle *dht,
2037 const struct GNUNET_CONFIGURATION_Handle *c,
2038 unsigned long long max_bg_queries)
2043 namestore_handle = nh;
2046 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2047 max_allowed_background_queries = max_bg_queries;
2049 GNUNET_CONFIGURATION_get_value_string (c,
2054 /* user did not specify DNS resolver, use 8.8.8.8 */
2055 dns_ip = GNUNET_strdup ("8.8.8.8");
2057 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
2058 GNUNET_free (dns_ip);
2059 vpn_handle = GNUNET_VPN_connect (cfg);
2067 GNS_resolver_done ()
2069 struct GNS_ResolverHandle *rh;
2071 /* abort active resolutions */
2072 while (NULL != (rh = rlh_head))
2074 rh->proc (rh->proc_cls, 0, NULL);
2075 GNS_resolver_lookup_cancel (rh);
2077 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
2078 dht_lookup_heap = NULL;
2079 GNUNET_DNSSTUB_stop (dns_handle);
2081 GNUNET_VPN_disconnect (vpn_handle);
2084 namestore_handle = NULL;
2088 /* *************** common helper functions (do not really belong here) *********** */
2091 * Checks if @a name ends in ".TLD"
2093 * @param name the name to check
2094 * @param tld the TLD to check for
2095 * @return GNUNET_YES or GNUNET_NO
2098 is_tld (const char* name, const char* tld)
2102 if (strlen (name) <= strlen (tld))
2104 offset = strlen (name) - strlen (tld);
2105 if (0 != strcmp (name + offset, tld))
2111 /* end of gnunet-service-gns_resolver.c */