2 This file is part of GNUnet.
3 (C) 2011-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file gns/gnunet-service-gns_resolver.c
23 * @brief GNU Name System resolver logic
24 * @author Martin Schanzenbach
25 * @author Christian Grothoff
28 * - GNS: handle special SRV names --- no delegation, direct lookup;
29 * can likely be done in 'resolver_lookup_get_next_label'. (#3003)
30 * - revocation checks (use REVOCATION service!), (#3004)
31 * - DNAME support (#3005)
34 #include "gnunet_util_lib.h"
35 #include "gnunet_dnsstub_lib.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_gnsrecord_lib.h"
38 #include "gnunet_namecache_service.h"
39 #include "gnunet_namestore_service.h"
40 #include "gnunet_dns_service.h"
41 #include "gnunet_resolver_service.h"
42 #include "gnunet_revocation_service.h"
43 #include "gnunet_dnsparser_lib.h"
44 #include "gnunet_gns_service.h"
46 #include "gnunet-service-gns_resolver.h"
47 #include "gnunet-service-gns_shorten.h"
48 #include "gnunet_vpn_service.h"
52 * Default DHT timeout for lookups.
54 #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
57 * Default timeout for DNS lookups.
59 #define DNS_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
62 * Default timeout for VPN redirections.
64 #define VPN_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
67 * DHT replication level
69 #define DHT_GNS_REPLICATION_LEVEL 5
72 * How deep do we allow recursions to go before we abort?
74 #define MAX_RECURSION 256
78 * DLL to hold the authority chain we had to pass in the resolution
86 struct AuthorityChain *prev;
91 struct AuthorityChain *next;
94 * Resolver handle this entry in the chain belongs to.
96 struct GNS_ResolverHandle *rh;
99 * label/name corresponding to the authority
104 * label/name suggested for shortening to the authority
106 char *suggested_shortening_label;
109 * Do we already try to shorten this authority?
111 int shortening_started;
114 * #GNUNET_YES if the authority was a GNS authority,
115 * #GNUNET_NO if the authority was a DNS authority.
120 * Information about the resolver authority for this label.
126 * The zone of the GNS authority
128 struct GNUNET_CRYPTO_EcdsaPublicKey gns_authority;
133 * Domain of the DNS resolver that is the authority.
134 * (appended to construct the DNS name to resolve;
135 * this is NOT the DNS name of the DNS server!).
137 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
140 * IP address of the DNS resolver that is authoritative.
141 * (this implementation currently only supports one
144 struct sockaddr_storage dns_ip;
154 * A result we got from DNS.
162 struct DnsResult *next;
167 struct DnsResult *prev;
170 * Binary value stored in the DNS record (appended to this struct)
175 * Expiration time for the DNS record, 0 if we didn't
176 * get anything useful (i.e. 'gethostbyname' was used).
178 uint64_t expiration_time;
181 * Number of bytes in @e data.
186 * Type of the GNS/DNS record.
188 uint32_t record_type;
194 * Closure for #vpn_allocation_cb.
200 * Which resolution process are we processing.
202 struct GNS_ResolverHandle *rh;
205 * Handle to the VPN request that we were performing.
207 struct GNUNET_VPN_RedirectionRequest *vpn_request;
210 * Number of records serialized in @e rd_data.
212 unsigned int rd_count;
215 * Serialized records.
220 * Number of bytes in @e rd_data.
227 * Information we keep during the resolution of an
228 * IP address for a DNS server while handling a
231 struct Gns2DnsContext
235 * DNS domain in which the resolution will continue
236 * (first part of the GNS2DNS record).
241 * Handle for the resolution of the IP part of the
242 * GNS2DNS record. Will return to us the addresses
243 * of the DNS resolver to use.
245 struct GNS_ResolverHandle *rh;
251 * Handle to a currenty pending resolution. On result (positive or
252 * negative) the #GNS_ResultProcessor is called.
254 struct GNS_ResolverHandle
260 struct GNS_ResolverHandle *next;
265 struct GNS_ResolverHandle *prev;
268 * The top-level GNS authoritative zone to query
270 struct GNUNET_CRYPTO_EcdsaPublicKey authority_zone;
273 * called when resolution phase finishes
275 GNS_ResultProcessor proc;
278 * closure passed to @e proc
283 * Handle used during GNS2DNS resolution for looking up the
284 * IP address of the DNS server.
286 struct Gns2DnsContext *g2dc;
289 * Handle for DHT lookups. should be NULL if no lookups are in progress
291 struct GNUNET_DHT_GetHandle *get_handle;
294 * Handle to a VPN request, NULL if none is active.
296 struct VpnContext *vpn_ctx;
299 * Socket for a DNS request, NULL if none is active.
301 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
304 * Handle for standard DNS resolution, NULL if none is active.
306 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
309 * Pending Namecache lookup task
311 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe;
314 * Pending revocation check.
316 struct GNUNET_REVOCATION_Query *rev_check;
319 * Heap node associated with this lookup. Used to limit number of
320 * concurrent requests.
322 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
325 * DLL to store the authority chain
327 struct AuthorityChain *ac_head;
330 * DLL to store the authority chain
332 struct AuthorityChain *ac_tail;
335 * Private key of the shorten zone, NULL to not shorten.
337 struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key;
340 * ID of a task associated with the resolution process.
342 GNUNET_SCHEDULER_TaskIdentifier task_id;
345 * The name to resolve
350 * DLL of results we got from DNS.
352 struct DnsResult *dns_result_head;
355 * DLL of results we got from DNS.
357 struct DnsResult *dns_result_tail;
360 * Current offset in 'name' where we are resolving.
362 size_t name_resolution_pos;
370 * Desired type for the resolution.
375 * We increment the loop limiter for each step in a recursive
376 * resolution. If it passes our threshold (i.e. due to
377 * self-recursion in the resolution, i.e CNAME fun), we stop.
379 unsigned int loop_limiter;
385 * Active namestore caching operations.
391 * Organized in a DLL.
393 struct CacheOps *next;
396 * Organized in a DLL.
398 struct CacheOps *prev;
401 * Pending Namestore caching task.
403 struct GNUNET_NAMECACHE_QueueEntry *namecache_qe_cache;
409 * Our handle to the namecache service
411 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
414 * Our handle to the vpn service
416 static struct GNUNET_VPN_Handle *vpn_handle;
419 * Resolver handle to the dht
421 static struct GNUNET_DHT_Handle *dht_handle;
424 * Handle to perform DNS lookups.
426 static struct GNUNET_DNSSTUB_Context *dns_handle;
429 * Heap for limiting parallel DHT lookups
431 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
434 * Maximum amount of parallel queries to the DHT
436 static unsigned long long max_allowed_background_queries;
439 * Head of resolver lookup list
441 static struct GNS_ResolverHandle *rlh_head;
444 * Tail of resolver lookup list
446 static struct GNS_ResolverHandle *rlh_tail;
449 * Organized in a DLL.
451 static struct CacheOps *co_head;
454 * Organized in a DLL.
456 static struct CacheOps *co_tail;
460 * Global configuration.
462 static const struct GNUNET_CONFIGURATION_Handle *cfg;
466 * Check if name is in srv format (_x._y.xxx)
469 * @return #GNUNET_YES if true
472 is_srv (const char *name)
479 if (NULL == strstr (name, "._"))
482 ndup = GNUNET_strdup (name);
484 if (NULL == strtok (NULL, "."))
486 if (NULL == strtok (NULL, "."))
488 if (NULL != strtok (NULL, "."))
497 * Determine if this name is canonical (is a legal name in a zone, without delegation);
498 * note that we do not test that the name does not contain illegal characters, we only
499 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
500 * even though they consist of multiple labels.
503 * a.b.gnu = not canonical
505 * _foo._srv = canonical
506 * _f.bar = not canonical
508 * @param name the name to test
509 * @return #GNUNET_YES if canonical
512 is_canonical (const char *name)
517 if (NULL == strchr (name, '.'))
522 while (NULL != (dot = strchr (pos, '.')))
530 /* ************************** Resolution **************************** */
533 * Expands a name ending in .+ with the zone of origin.
535 * @param rh resolution context
536 * @param name name to modify (to be free'd or returned)
537 * @return updated name
540 translate_dot_plus (struct GNS_ResolverHandle *rh,
544 size_t s_len = strlen (name);
546 if (0 != strcmp (&name[s_len - 2],
548 return name; /* did not end in ".+" */
549 GNUNET_assert (GNUNET_YES == rh->ac_tail->gns_authority);
550 GNUNET_asprintf (&ret,
554 GNUNET_GNSRECORD_pkey_to_zkey (&rh->ac_tail->authority_info.gns_authority));
561 * Task scheduled to asynchronously fail a resolution.
563 * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
564 * @param tc task context
567 fail_resolution (void *cls,
568 const struct GNUNET_SCHEDULER_TaskContext *tc)
570 struct GNS_ResolverHandle *rh = cls;
572 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
573 rh->proc (rh->proc_cls, 0, NULL);
574 GNS_resolver_lookup_cancel (rh);
578 #if (defined WINDOWS) || (defined DARWIN)
579 /* Don't have this on W32, here's a naive implementation
580 * Was somehow removed on OS X ... */
582 memrchr (const void *s,
586 const unsigned char *ucs = s;
589 for (i = n - 1; i >= 0; i--)
590 if (c == (int) ucs[i])
591 return (void *) &ucs[i];
598 * Get the next, rightmost label from the name that we are trying to resolve,
599 * and update the resolution position accordingly.
601 * @param rh handle to the resolution operation to get the next label from
602 * @return NULL if there are no more labels
605 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
611 if (0 == rh->name_resolution_pos)
613 dot = memrchr (rh->name, (int) '.', rh->name_resolution_pos);
616 /* done, this was the last one */
617 len = rh->name_resolution_pos;
619 rh->name_resolution_pos = 0;
623 /* advance by one label */
624 len = rh->name_resolution_pos - (dot - rh->name) - 1;
626 rh->name_resolution_pos = dot - rh->name;
628 return GNUNET_strndup (rp, len);
633 * Gives the cummulative result obtained to the callback and clean up the request.
635 * @param rh resolution process that has culminated in a result
638 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
640 struct DnsResult *pos;
645 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
648 struct GNUNET_GNSRECORD_Data rd[n];
651 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
653 rd[i].data = pos->data;
654 rd[i].data_size = pos->data_size;
655 rd[i].record_type = pos->record_type;
656 if (0 == pos->expiration_time)
658 rd[i].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
659 rd[i].expiration_time = 0;
663 rd[i].flags = GNUNET_GNSRECORD_RF_NONE;
664 rd[i].expiration_time = pos->expiration_time;
668 GNUNET_assert (i == n);
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "Transmitting standard DNS result with %u records\n",
672 rh->proc (rh->proc_cls,
676 GNS_resolver_lookup_cancel (rh);
681 * Add a result from DNS to the records to be returned to the application.
683 * @param rh resolution request to extend with a result
684 * @param expiration_time expiration time for the answer
685 * @param record_type DNS record type of the answer
686 * @param data_size number of bytes in @a data
687 * @param data binary data to return in DNS record
690 add_dns_result (struct GNS_ResolverHandle *rh,
691 uint64_t expiration_time,
692 uint32_t record_type,
696 struct DnsResult *res;
698 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
699 res->expiration_time = expiration_time;
700 res->data_size = data_size;
701 res->record_type = record_type;
703 memcpy (&res[1], data, data_size);
704 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
711 * We had to do a DNS lookup. Convert the result (if any) and return
714 * @param cls closure with the `struct GNS_ResolverHandle`
715 * @param addr one of the addresses of the host, NULL for the last address
716 * @param addrlen length of the address
719 handle_dns_result (void *cls,
720 const struct sockaddr *addr,
723 struct GNS_ResolverHandle *rh = cls;
724 const struct sockaddr_in *sa4;
725 const struct sockaddr_in6 *sa6;
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 "Received %u bytes of DNS IP data\n",
732 rh->std_resolve = NULL;
733 transmit_lookup_dns_result (rh);
736 switch (addr->sa_family)
739 sa4 = (const struct sockaddr_in *) addr;
741 0 /* expiration time is unknown */,
742 GNUNET_DNSPARSER_TYPE_A,
743 sizeof (struct in_addr),
747 sa6 = (const struct sockaddr_in6 *) addr;
749 0 /* expiration time is unknown */,
750 GNUNET_DNSPARSER_TYPE_AAAA,
751 sizeof (struct in6_addr),
762 * Task scheduled to continue with the resolution process.
764 * @param cls the 'struct GNS_ResolverHandle' of the resolution
765 * @param tc task context
768 recursive_resolution (void *cls,
769 const struct GNUNET_SCHEDULER_TaskContext *tc);
773 * Begin the resolution process from 'name', starting with
774 * the identification of the zone specified by 'name'.
776 * @param rh resolution to perform
779 start_resolver_lookup (struct GNS_ResolverHandle *rh);
783 * Function called with the result of a DNS resolution.
785 * @param cls the request handle of the resolution that
786 * we were attempting to make
787 * @param rs socket that received the response
788 * @param dns dns response, never NULL
789 * @param dns_len number of bytes in @a dns
792 dns_result_parser (void *cls,
793 struct GNUNET_DNSSTUB_RequestSocket *rs,
794 const struct GNUNET_TUN_DnsHeader *dns,
797 struct GNS_ResolverHandle *rh = cls;
798 struct GNUNET_DNSPARSER_Packet *p;
799 const struct GNUNET_DNSPARSER_Record *rec;
800 unsigned int rd_count;
803 rh->dns_request = NULL;
804 GNUNET_SCHEDULER_cancel (rh->task_id);
805 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
806 p = GNUNET_DNSPARSER_parse ((const char *) dns,
810 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
811 _("Failed to parse DNS response\n"));
812 rh->proc (rh->proc_cls, 0, NULL);
813 GNS_resolver_lookup_cancel (rh);
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
817 "Received DNS response for `%s' with %u answers\n",
819 (unsigned int) p->num_answers);
820 if ( (p->num_answers > 0) &&
821 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
822 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
824 GNUNET_free (rh->name);
825 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
826 start_resolver_lookup (rh);
827 GNUNET_DNSPARSER_free_packet (p);
830 /* FIXME: add DNAME support */
832 /* convert from (parsed) DNS to (binary) GNS format! */
833 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
835 struct GNUNET_GNSRECORD_Data rd[rd_count];
837 char buf[UINT16_MAX];
843 memset (rd, 0, sizeof (rd));
844 for (i=0;i<rd_count;i++)
846 if (i < p->num_answers)
847 rec = &p->answers[i];
848 else if (i < p->num_answers + p->num_authority_records)
849 rec = &p->authority_records[i - p->num_answers];
851 rec = &p->authority_records[i - p->num_answers - p->num_authority_records];
852 /* As we copied the full DNS name to 'rh->ac_tail->label', this
853 should be the correct check to see if this record is actually
854 a record for our label... */
855 if (0 != strcmp (rec->name,
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Dropping record `%s', does not match desired name `%s'\n",
865 rd[i - skip].record_type = rec->type;
866 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
869 case GNUNET_DNSPARSER_TYPE_A:
870 if (rec->data.raw.data_len != sizeof (struct in_addr))
876 rd[i - skip].data_size = rec->data.raw.data_len;
877 rd[i - skip].data = rec->data.raw.data;
879 case GNUNET_DNSPARSER_TYPE_AAAA:
880 if (rec->data.raw.data_len != sizeof (struct in6_addr))
886 rd[i - skip].data_size = rec->data.raw.data_len;
887 rd[i - skip].data = rec->data.raw.data;
889 case GNUNET_DNSPARSER_TYPE_CNAME:
890 case GNUNET_DNSPARSER_TYPE_PTR:
891 case GNUNET_DNSPARSER_TYPE_NS:
894 GNUNET_DNSPARSER_builder_add_name (buf,
903 rd[i - skip].data_size = buf_off - buf_start;
904 rd[i - skip].data = &buf[buf_start];
906 case GNUNET_DNSPARSER_TYPE_SOA:
909 GNUNET_DNSPARSER_builder_add_soa (buf,
918 rd[i - skip].data_size = buf_off - buf_start;
919 rd[i - skip].data = &buf[buf_start];
921 case GNUNET_DNSPARSER_TYPE_MX:
924 GNUNET_DNSPARSER_builder_add_mx (buf,
933 rd[i - skip].data_size = buf_off - buf_start;
934 rd[i - skip].data = &buf[buf_start];
936 case GNUNET_DNSPARSER_TYPE_SRV:
939 GNUNET_DNSPARSER_builder_add_srv (buf,
948 rd[i - skip].data_size = buf_off - buf_start;
949 rd[i - skip].data = &buf[buf_start];
952 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
953 _("Skipping record of unsupported type %d\n"),
959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
960 "Returning DNS response for `%s' with %u answers\n",
962 (unsigned int) p->num_answers);
963 rh->proc (rh->proc_cls, rd_count - skip, rd);
964 GNS_resolver_lookup_cancel (rh);
966 GNUNET_DNSPARSER_free_packet (p);
971 * Perform recursive DNS resolution. Asks the given DNS resolver to
972 * resolve "rh->dns_name", possibly recursively proceeding following
973 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
974 * we find the answer.
976 * @param rh resolution information
979 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
981 struct AuthorityChain *ac;
983 struct GNUNET_DNSPARSER_Query *query;
984 struct GNUNET_DNSPARSER_Packet *p;
986 size_t dns_request_length;
989 GNUNET_assert (NULL != ac);
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991 "Starting DNS lookup for `%s'\n",
993 GNUNET_assert (GNUNET_NO == ac->gns_authority);
994 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
997 sa_len = sizeof (struct sockaddr_in);
1000 sa_len = sizeof (struct sockaddr_in6);
1004 rh->proc (rh->proc_cls, 0, NULL);
1005 GNS_resolver_lookup_cancel (rh);
1008 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
1009 query->name = GNUNET_strdup (ac->label);
1010 query->type = rh->record_type;
1011 query->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1012 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
1015 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1017 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
1018 p->flags.recursion_desired = 1;
1020 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
1023 rh->proc (rh->proc_cls, 0, NULL);
1024 GNS_resolver_lookup_cancel (rh);
1028 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
1029 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
1035 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
1039 GNUNET_free (dns_request);
1040 GNUNET_DNSPARSER_free_packet (p);
1045 * We encountered a CNAME record during our resolution.
1046 * Merge it into our chain.
1048 * @param rh resolution we are performing
1049 * @param cname value of the cname record we got for the current
1050 * authority chain tail
1053 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
1058 struct AuthorityChain *ac;
1060 nlen = strlen (cname);
1063 &cname[nlen - 2])) )
1065 /* CNAME resolution continues relative to current domain */
1066 if (0 == rh->name_resolution_pos)
1068 res = GNUNET_strndup (cname, nlen - 2);
1069 rh->name_resolution_pos = nlen - 2;
1073 GNUNET_asprintf (&res,
1075 (int) rh->name_resolution_pos,
1079 rh->name_resolution_pos = strlen (res);
1081 GNUNET_free (rh->name);
1083 ac = GNUNET_new (struct AuthorityChain);
1085 ac->gns_authority = GNUNET_YES;
1086 ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
1087 ac->label = resolver_lookup_get_next_label (rh);
1088 ac->suggested_shortening_label = NULL;
1089 ac->shortening_started = GNUNET_NO;
1090 /* add AC to tail */
1091 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1094 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1098 /* name is absolute, start from the beginning */
1099 GNUNET_free (rh->name);
1100 rh->name = GNUNET_strdup (cname);
1101 start_resolver_lookup (rh);
1106 * Process a records that were decrypted from a block.
1108 * @param cls closure with the 'struct GNS_ResolverHandle'
1109 * @param rd_count number of entries in @a rd array
1110 * @param rd array of records with data to store
1113 handle_gns_resolution_result (void *cls,
1114 unsigned int rd_count,
1115 const struct GNUNET_GNSRECORD_Data *rd);
1119 * Callback invoked from the VPN service once a redirection is
1120 * available. Provides the IP address that can now be used to
1121 * reach the requested destination. Replaces the "VPN" record
1122 * with the respective A/AAAA record and continues processing.
1124 * @param cls closure
1125 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1126 * will match 'result_af' from the request
1127 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1128 * that the VPN allocated for the redirection;
1129 * traffic to this IP will now be redirected to the
1130 * specified target peer; NULL on error
1133 vpn_allocation_cb (void *cls,
1135 const void *address)
1137 struct VpnContext *vpn_ctx = cls;
1138 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1139 struct GNUNET_GNSRECORD_Data rd[vpn_ctx->rd_count];
1142 vpn_ctx->vpn_request = NULL;
1144 GNUNET_assert (GNUNET_OK ==
1145 GNUNET_GNSRECORD_records_deserialize (vpn_ctx->rd_data_size,
1149 for (i=0;i<vpn_ctx->rd_count;i++)
1151 if (GNUNET_GNSRECORD_TYPE_VPN == rd[i].record_type)
1156 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1157 rd[i].data_size = sizeof (struct in_addr);
1158 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1160 rd[i].data = address;
1163 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1164 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1166 rd[i].data = address;
1167 rd[i].data_size = sizeof (struct in6_addr);
1175 GNUNET_assert (i < vpn_ctx->rd_count);
1176 handle_gns_resolution_result (rh,
1179 GNUNET_free (vpn_ctx->rd_data);
1180 GNUNET_free (vpn_ctx);
1185 * We've resolved the IP address for the DNS resolver to use
1186 * after encountering a GNS2DNS record.
1188 * TODO: Right now we only foward the request to ONE DNS resolver,
1189 * even if we get multiple IP addresses back; a correct implementation
1190 * should try all DNS resolvers.
1192 * @param cls the `struct GNS_ResolverHandle` where we encountered
1193 * the GNS2DNS record
1194 * @param rd_count number of records in @a rd
1195 * @param rd addresses for the DNS resolver (presumably)
1198 handle_gns2dns_result (void *cls,
1199 unsigned int rd_count,
1200 const struct GNUNET_GNSRECORD_Data *rd)
1202 struct GNS_ResolverHandle *rh = cls;
1203 struct AuthorityChain *ac;
1205 struct sockaddr *sa;
1206 struct sockaddr_in v4;
1207 struct sockaddr_in6 v6;
1210 /* find suitable A/AAAA record */
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1212 "Received %u results for IP address of DNS server for GNS2DNS transition\n",
1214 /* enable cleanup of 'rh' handle that comes next... */
1215 GNUNET_CONTAINER_DLL_insert (rlh_head,
1218 rh->g2dc->rh = NULL;
1221 for (j=0;j<rd_count;j++)
1223 switch (rd[j].record_type)
1225 case GNUNET_DNSPARSER_TYPE_A:
1226 if (sizeof (struct in_addr) != rd[j].data_size)
1228 GNUNET_break_op (0);
1229 rh->proc (rh->proc_cls, 0, NULL);
1230 GNS_resolver_lookup_cancel (rh);
1233 /* FIXME: might want to check if we support IPv4 here,
1234 and otherwise skip this one and hope we find another */
1235 memset (&v4, 0, sizeof (v4));
1236 sa_len = sizeof (v4);
1237 v4.sin_family = AF_INET;
1238 v4.sin_port = htons (53);
1239 #if HAVE_SOCKADDR_IN_SIN_LEN
1240 v4.sin_len = (u_char) sa_len;
1242 memcpy (&v4.sin_addr,
1244 sizeof (struct in_addr));
1245 sa = (struct sockaddr *) &v4;
1247 case GNUNET_DNSPARSER_TYPE_AAAA:
1248 if (sizeof (struct in6_addr) != rd[j].data_size)
1250 GNUNET_break_op (0);
1251 rh->proc (rh->proc_cls, 0, NULL);
1252 GNS_resolver_lookup_cancel (rh);
1255 /* FIXME: might want to check if we support IPv6 here,
1256 and otherwise skip this one and hope we find another */
1257 memset (&v6, 0, sizeof (v6));
1258 sa_len = sizeof (v6);
1259 v6.sin6_family = AF_INET6;
1260 v6.sin6_port = htons (53);
1261 #if HAVE_SOCKADDR_IN_SIN_LEN
1262 v6.sin6_len = (u_char) sa_len;
1264 memcpy (&v6.sin6_addr,
1266 sizeof (struct in6_addr));
1267 sa = (struct sockaddr *) &v6;
1277 /* we cannot continue; NS without A/AAAA */
1278 rh->proc (rh->proc_cls, 0, NULL);
1279 GNS_resolver_lookup_cancel (rh);
1282 /* expand authority chain */
1283 ac = GNUNET_new (struct AuthorityChain);
1285 strcpy (ac->authority_info.dns_authority.name,
1287 memcpy (&ac->authority_info.dns_authority.dns_ip,
1290 /* for DNS recursion, the label is the full DNS name,
1291 created from the remainder of the GNS name and the
1292 name in the NS record */
1293 GNUNET_asprintf (&ac->label,
1295 (int) rh->name_resolution_pos,
1297 (0 != rh->name_resolution_pos) ? "." : "",
1299 GNUNET_free (rh->g2dc->ns);
1300 GNUNET_free (rh->g2dc);
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 "Will continue resolution using DNS server `%s' to resolve `%s'\n",
1304 GNUNET_a2s (sa, sa_len),
1306 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1309 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1312 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1314 rh->proc (rh->proc_cls, 0, NULL);
1315 GNS_resolver_lookup_cancel (rh);
1319 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1325 * Process a records that were decrypted from a block.
1327 * @param cls closure with the `struct GNS_ResolverHandle`
1328 * @param rd_count number of entries in @a rd array
1329 * @param rd array of records with data to store
1332 handle_gns_resolution_result (void *cls,
1333 unsigned int rd_count,
1334 const struct GNUNET_GNSRECORD_Data *rd)
1336 struct GNS_ResolverHandle *rh = cls;
1337 struct AuthorityChain *ac;
1338 struct AuthorityChain *shorten_ac;
1341 struct VpnContext *vpn_ctx;
1342 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1344 struct GNUNET_HashCode vhash;
1346 char scratch[UINT16_MAX];
1348 size_t scratch_start;
1350 struct GNUNET_GNSRECORD_Data rd_new[rd_count];
1351 unsigned int rd_off;
1353 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1354 "Resolution succeeded for `%s' in zone %s, got %u records\n",
1356 GNUNET_GNSRECORD_z2s (&rh->ac_tail->authority_info.gns_authority),
1358 if (0 == rh->name_resolution_pos)
1360 /* top-level match, are we done yet? */
1361 if ( (rd_count > 0) &&
1362 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1363 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1366 cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
1369 if ( (NULL == cname) ||
1370 (off != rd[0].data_size) )
1372 GNUNET_break_op (0);
1373 rh->proc (rh->proc_cls, 0, NULL);
1374 GNS_resolver_lookup_cancel (rh);
1375 GNUNET_free_non_null (cname);
1378 handle_gns_cname_result (rh,
1380 GNUNET_free (cname);
1383 /* If A/AAAA was requested, but we got a VPN
1384 record, we convert it to A/AAAA using GNUnet VPN */
1385 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1386 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1388 for (i=0;i<rd_count;i++)
1390 switch (rd[i].record_type)
1392 case GNUNET_GNSRECORD_TYPE_VPN:
1394 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1395 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) <
1398 GNUNET_break_op (0);
1399 rh->proc (rh->proc_cls, 0, NULL);
1400 GNS_resolver_lookup_cancel (rh);
1403 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1404 vname = (const char *) &vpn[1];
1405 if ('\0' != vname[rd[i].data_size - 1 - sizeof (struct GNUNET_TUN_GnsVpnRecord)])
1407 GNUNET_break_op (0);
1408 rh->proc (rh->proc_cls, 0, NULL);
1409 GNS_resolver_lookup_cancel (rh);
1412 GNUNET_CRYPTO_hash (vname,
1413 strlen (vname), // FIXME: +1?
1415 vpn_ctx = GNUNET_new (struct VpnContext);
1416 rh->vpn_ctx = vpn_ctx;
1418 vpn_ctx->rd_data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
1420 vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
1421 (void) GNUNET_GNSRECORD_records_serialize (rd_count,
1423 vpn_ctx->rd_data_size,
1425 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1430 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1435 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1437 /* delegation to DNS */
1438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1439 "Found GNS2DNS record, delegating to DNS!\n");
1446 } /* end: name_resolution_pos */
1447 /* convert relative names in record values to absolute names,
1448 using 'scratch' array for memory allocations */
1451 shorten_ac = rh->ac_tail;
1452 for (i=0;i<rd_count;i++)
1454 rd_new[rd_off] = rd[i];
1455 /* Check if the embedded name(s) end in "+", and if so,
1456 replace the "+" with the zone at "ac_tail", changing the name
1457 to a ".zkey". The name is allocated on the 'scratch' array,
1458 so we can free it afterwards. */
1459 switch (rd[i].record_type)
1461 case GNUNET_DNSPARSER_TYPE_CNAME:
1466 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1469 if ( (NULL == cname) ||
1470 (off != rd[i].data_size) )
1472 GNUNET_break_op (0); /* record not well-formed */
1476 cname = translate_dot_plus (rh, cname);
1477 GNUNET_break (NULL != cname);
1478 scratch_start = scratch_off;
1480 GNUNET_DNSPARSER_builder_add_name (scratch,
1489 rd_new[rd_off].data = &scratch[scratch_start];
1490 rd_new[rd_off].data_size = scratch_off - scratch_start;
1494 GNUNET_free_non_null (cname);
1497 case GNUNET_DNSPARSER_TYPE_SOA:
1499 struct GNUNET_DNSPARSER_SoaRecord *soa;
1502 soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
1505 if ( (NULL == soa) ||
1506 (off != rd[i].data_size) )
1508 GNUNET_break_op (0); /* record not well-formed */
1512 soa->mname = translate_dot_plus (rh, soa->mname);
1513 soa->rname = translate_dot_plus (rh, soa->rname);
1514 scratch_start = scratch_off;
1516 GNUNET_DNSPARSER_builder_add_soa (scratch,
1525 rd_new[rd_off].data = &scratch[scratch_start];
1526 rd_new[rd_off].data_size = scratch_off - scratch_start;
1531 GNUNET_DNSPARSER_free_soa (soa);
1534 case GNUNET_DNSPARSER_TYPE_MX:
1536 struct GNUNET_DNSPARSER_MxRecord *mx;
1539 mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
1542 if ( (NULL == mx) ||
1543 (off != rd[i].data_size) )
1545 GNUNET_break_op (0); /* record not well-formed */
1549 mx->mxhost = translate_dot_plus (rh, mx->mxhost);
1550 scratch_start = scratch_off;
1552 GNUNET_DNSPARSER_builder_add_mx (scratch,
1561 rd_new[rd_off].data = &scratch[scratch_start];
1562 rd_new[rd_off].data_size = scratch_off - scratch_start;
1567 GNUNET_DNSPARSER_free_mx (mx);
1570 case GNUNET_DNSPARSER_TYPE_SRV:
1572 struct GNUNET_DNSPARSER_SrvRecord *srv;
1575 /* FIXME: passing rh->name here is is not necessarily what we want
1576 (SRV support not finished) */
1577 srv = GNUNET_DNSPARSER_parse_srv (rh->name,
1581 if ( (NULL == srv) ||
1582 (off != rd[i].data_size) )
1584 GNUNET_break_op (0); /* record not well-formed */
1588 srv->domain_name = translate_dot_plus (rh, srv->domain_name);
1589 srv->target = translate_dot_plus (rh, srv->target);
1590 scratch_start = scratch_off;
1592 GNUNET_DNSPARSER_builder_add_srv (scratch,
1601 rd_new[rd_off].data = &scratch[scratch_start];
1602 rd_new[rd_off].data_size = scratch_off - scratch_start;
1607 GNUNET_DNSPARSER_free_srv (srv);
1611 case GNUNET_GNSRECORD_TYPE_NICK:
1615 if ((rd[i].data_size > 0) &&
1616 (nick[rd[i].data_size -1] != '\0'))
1618 GNUNET_break_op (0);
1621 if (NULL == shorten_ac->suggested_shortening_label)
1622 shorten_ac->suggested_shortening_label = GNUNET_strdup (nick);
1625 case GNUNET_GNSRECORD_TYPE_PKEY:
1627 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
1629 if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
1631 GNUNET_break_op (0);
1634 memcpy (&pub, rd[i].data, rd[i].data_size);
1636 if (GNUNET_GNSRECORD_TYPE_PKEY != rh->record_type)
1638 /* try to resolve "+" */
1639 struct AuthorityChain *ac;
1641 ac = GNUNET_new (struct AuthorityChain);
1643 ac->gns_authority = GNUNET_YES;
1644 ac->authority_info.gns_authority = pub;
1645 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
1646 ac->suggested_shortening_label = NULL;
1647 ac->shortening_started = GNUNET_NO;
1648 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1651 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1657 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1659 /* delegation to DNS */
1660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1661 "Found GNS2DNS record, delegating to DNS!\n");
1668 } /* end: for rd_count */
1670 /* trigger shortening */
1671 if ((NULL != rh->shorten_key) &&
1672 (NULL != shorten_ac) &&
1673 (GNUNET_NO == shorten_ac->shortening_started) &&
1674 (NULL != shorten_ac->suggested_shortening_label))
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1677 "Start shortening for label `%s' based on nick `%s'\n",
1679 shorten_ac->suggested_shortening_label);
1680 shorten_ac->shortening_started = GNUNET_YES;
1681 GNS_shorten_start (shorten_ac->label,
1682 shorten_ac->suggested_shortening_label,
1683 &shorten_ac->authority_info.gns_authority,
1687 /* yes, we are done, return result */
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1689 "Returning GNS response for `%s' with %u answers\n",
1692 rh->proc (rh->proc_cls, rd_off, rd_new);
1693 GNS_resolver_lookup_cancel (rh);
1697 /* need to recurse, check if we can */
1698 for (i=0;i<rd_count;i++)
1700 switch (rd[i].record_type)
1702 case GNUNET_GNSRECORD_TYPE_PKEY:
1703 /* delegation to another zone */
1704 if (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
1707 GNUNET_break_op (0);
1708 rh->proc (rh->proc_cls, 0, NULL);
1709 GNS_resolver_lookup_cancel (rh);
1712 /* expand authority chain */
1713 ac = GNUNET_new (struct AuthorityChain);
1715 ac->gns_authority = GNUNET_YES;
1716 ac->suggested_shortening_label = NULL;
1717 ac->shortening_started = GNUNET_NO;
1718 memcpy (&ac->authority_info.gns_authority,
1720 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1721 ac->label = resolver_lookup_get_next_label (rh);
1722 /* add AC to tail */
1723 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1727 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1730 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
1732 /* resolution continues within DNS */
1733 struct Gns2DnsContext *g2dc;
1738 ns = GNUNET_DNSPARSER_parse_name (rd[i].data,
1741 ip = GNUNET_DNSPARSER_parse_name (rd[i].data,
1744 if ( (NULL == ns) ||
1746 (off != rd[i].data_size) )
1748 GNUNET_break_op (0);
1749 GNUNET_free_non_null (ns);
1750 GNUNET_free_non_null (ip);
1751 rh->proc (rh->proc_cls, 0, NULL);
1752 GNS_resolver_lookup_cancel (rh);
1755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1756 "Resolving `%s' to determine IP address of DNS server for GNS2DNS transition\n",
1758 /* resolve 'ip' to determine the IP(s) of the DNS
1760 g2dc = GNUNET_new (struct Gns2DnsContext);
1762 g2dc->rh = GNUNET_new (struct GNS_ResolverHandle);
1763 g2dc->rh->authority_zone = rh->ac_tail->authority_info.gns_authority;
1764 ip = translate_dot_plus (rh, ip);
1765 g2dc->rh->name = ip;
1766 g2dc->rh->name_resolution_pos = strlen (ip);
1767 g2dc->rh->proc = &handle_gns2dns_result;
1768 g2dc->rh->proc_cls = rh;
1769 g2dc->rh->record_type = GNUNET_GNSRECORD_TYPE_ANY;
1770 g2dc->rh->only_cached = GNUNET_NO;
1771 g2dc->rh->loop_limiter = rh->loop_limiter + 1;
1773 start_resolver_lookup (g2dc->rh);
1776 case GNUNET_DNSPARSER_TYPE_CNAME:
1781 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1784 if ( (NULL == cname) ||
1785 (off != rd[i].data_size) )
1787 GNUNET_break_op (0); /* record not well-formed */
1788 rh->proc (rh->proc_cls, 0, NULL);
1789 GNS_resolver_lookup_cancel (rh);
1790 GNUNET_free_non_null (cname);
1793 handle_gns_cname_result (rh,
1795 GNUNET_free (cname);
1798 /* FIXME: handle DNAME */
1804 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1805 _("GNS lookup recursion failed (no delegation record found)\n"));
1806 rh->proc (rh->proc_cls, 0, NULL);
1807 GNS_resolver_lookup_cancel (rh);
1812 * Function called once the namestore has completed the request for
1815 * @param cls closure with the `struct CacheOps`
1816 * @param success #GNUNET_OK on success
1817 * @param emsg error message
1820 namecache_cache_continuation (void *cls,
1824 struct CacheOps *co = cls;
1826 co->namecache_qe_cache = NULL;
1828 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1829 _("Failed to cache GNS resolution: %s\n"),
1831 GNUNET_CONTAINER_DLL_remove (co_head,
1839 * Iterator called on each result obtained for a DHT
1840 * operation that expects a reply
1842 * @param cls closure with the `struct GNS_ResolverHandle`
1843 * @param exp when will this value expire
1844 * @param key key of the result
1845 * @param get_path peers on reply path (or NULL if not recorded)
1846 * [0] = datastore's first neighbor, [length - 1] = local peer
1847 * @param get_path_length number of entries in @a get_path
1848 * @param put_path peers on the PUT path (or NULL if not recorded)
1849 * [0] = origin, [length - 1] = datastore
1850 * @param put_path_length number of entries in @a put_path
1851 * @param type type of the result
1852 * @param size number of bytes in data
1853 * @param data pointer to the result data
1856 handle_dht_response (void *cls,
1857 struct GNUNET_TIME_Absolute exp,
1858 const struct GNUNET_HashCode *key,
1859 const struct GNUNET_PeerIdentity *get_path,
1860 unsigned int get_path_length,
1861 const struct GNUNET_PeerIdentity *put_path,
1862 unsigned int put_path_length,
1863 enum GNUNET_BLOCK_Type type,
1864 size_t size, const void *data)
1866 struct GNS_ResolverHandle *rh = cls;
1867 struct AuthorityChain *ac = rh->ac_tail;
1868 const struct GNUNET_GNSRECORD_Block *block;
1869 struct CacheOps *co;
1871 GNUNET_DHT_get_stop (rh->get_handle);
1872 rh->get_handle = NULL;
1873 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1874 rh->dht_heap_node = NULL;
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 "Handling response from the DHT\n");
1877 if (size < sizeof (struct GNUNET_GNSRECORD_Block))
1879 /* how did this pass DHT block validation!? */
1881 rh->proc (rh->proc_cls, 0, NULL);
1882 GNS_resolver_lookup_cancel (rh);
1887 ntohl (block->purpose.size) +
1888 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
1889 sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
1891 /* how did this pass DHT block validation!? */
1893 rh->proc (rh->proc_cls, 0, NULL);
1894 GNS_resolver_lookup_cancel (rh);
1898 GNUNET_GNSRECORD_block_decrypt (block,
1899 &ac->authority_info.gns_authority,
1901 &handle_gns_resolution_result,
1904 GNUNET_break_op (0); /* block was ill-formed */
1905 rh->proc (rh->proc_cls, 0, NULL);
1906 GNS_resolver_lookup_cancel (rh);
1909 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us)
1911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1912 "Received expired block from the DHT, will not cache it.\n");
1915 /* Cache well-formed blocks */
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917 "Caching response from the DHT in namestore\n");
1918 co = GNUNET_new (struct CacheOps);
1919 co->namecache_qe_cache = GNUNET_NAMECACHE_block_cache (namecache_handle,
1921 &namecache_cache_continuation,
1923 GNUNET_CONTAINER_DLL_insert (co_head,
1930 * Process a record that was stored in the namecache.
1932 * @param cls closure with the `struct GNS_ResolverHandle`
1933 * @param block block that was stored in the namecache
1936 handle_namestore_block_response (void *cls,
1937 const struct GNUNET_GNSRECORD_Block *block)
1939 struct GNS_ResolverHandle *rh = cls;
1940 struct GNS_ResolverHandle *rx;
1941 struct AuthorityChain *ac = rh->ac_tail;
1942 const char *label = ac->label;
1943 const struct GNUNET_CRYPTO_EcdsaPublicKey *auth = &ac->authority_info.gns_authority;
1944 struct GNUNET_HashCode query;
1946 GNUNET_GNSRECORD_query_from_public_key (auth,
1949 GNUNET_assert (NULL != rh->namecache_qe);
1950 rh->namecache_qe = NULL;
1951 if ( (GNUNET_NO == rh->only_cached) &&
1952 ( (NULL == block) ||
1953 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
1955 /* namecache knows nothing; try DHT lookup */
1956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1957 "Starting DHT lookup for `%s' in zone %s\n",
1959 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
1960 GNUNET_assert (NULL == rh->get_handle);
1961 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1962 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1964 DHT_GNS_REPLICATION_LEVEL,
1965 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1967 &handle_dht_response, rh);
1968 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1970 GNUNET_TIME_absolute_get ().abs_value_us);
1971 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
1973 /* fail longest-standing DHT request */
1974 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
1975 GNUNET_assert (NULL != rx);
1976 rx->proc (rx->proc_cls, 0, NULL);
1977 GNS_resolver_lookup_cancel (rx);
1981 if ( (NULL == block) ||
1982 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
1984 /* DHT not permitted and no local result, fail */
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1986 "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
1988 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
1989 rh->proc (rh->proc_cls, 0, NULL);
1990 GNS_resolver_lookup_cancel (rh);
1994 GNUNET_GNSRECORD_block_decrypt (block,
1997 &handle_gns_resolution_result,
2000 GNUNET_break_op (0); /* block was ill-formed */
2001 rh->proc (rh->proc_cls, 0, NULL);
2002 GNS_resolver_lookup_cancel (rh);
2009 * Lookup tail of our authority chain in the namecache.
2011 * @param rh query we are processing
2014 recursive_gns_resolution_namecache (struct GNS_ResolverHandle *rh)
2016 struct AuthorityChain *ac = rh->ac_tail;
2017 struct GNUNET_HashCode query;
2019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2020 "Starting GNS resolution for `%s' in zone %s\n",
2022 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2023 GNUNET_GNSRECORD_query_from_public_key (&ac->authority_info.gns_authority,
2026 rh->namecache_qe = GNUNET_NAMECACHE_lookup_block (namecache_handle,
2028 &handle_namestore_block_response,
2030 GNUNET_assert (NULL != rh->namecache_qe);
2035 * Function called with the result from a revocation check.
2037 * @param cls the `struct GNS_ResovlerHandle`
2038 * @param is_valid #GNUNET_YES if the zone was not yet revoked
2041 handle_revocation_result (void *cls,
2044 struct GNS_ResolverHandle *rh = cls;
2045 struct AuthorityChain *ac = rh->ac_tail;
2047 rh->rev_check = NULL;
2048 if (GNUNET_YES != is_valid)
2050 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2051 _("Zone %s was revoked, resolution fails\n"),
2052 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2053 rh->proc (rh->proc_cls, 0, NULL);
2054 GNS_resolver_lookup_cancel (rh);
2057 recursive_gns_resolution_namecache (rh);
2062 * Perform revocation check on tail of our authority chain.
2064 * @param rh query we are processing
2067 recursive_gns_resolution_revocation (struct GNS_ResolverHandle *rh)
2069 struct AuthorityChain *ac = rh->ac_tail;
2071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2072 "Starting revocation check for zone %s\n",
2073 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
2074 rh->rev_check = GNUNET_REVOCATION_query (cfg,
2075 &ac->authority_info.gns_authority,
2076 &handle_revocation_result,
2078 GNUNET_assert (NULL != rh->rev_check);
2083 * Task scheduled to continue with the resolution process.
2085 * @param cls the `struct GNS_ResolverHandle` of the resolution
2086 * @param tc task context
2089 recursive_resolution (void *cls,
2090 const struct GNUNET_SCHEDULER_TaskContext *tc)
2092 struct GNS_ResolverHandle *rh = cls;
2094 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
2095 if (MAX_RECURSION < rh->loop_limiter++)
2097 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2098 "Encountered unbounded recursion resolving `%s'\n",
2100 rh->proc (rh->proc_cls, 0, NULL);
2101 GNS_resolver_lookup_cancel (rh);
2104 if (GNUNET_YES == rh->ac_tail->gns_authority)
2105 recursive_gns_resolution_revocation (rh);
2107 recursive_dns_resolution (rh);
2112 * Begin the resolution process from 'name', starting with
2113 * the identification of the zone specified by 'name'.
2115 * @param rh resolution to perform
2118 start_resolver_lookup (struct GNS_ResolverHandle *rh)
2120 struct AuthorityChain *ac;
2125 if (1 == inet_pton (AF_INET,
2129 /* name is IPv4 address, pretend it's an A record */
2130 struct GNUNET_GNSRECORD_Data rd;
2133 rd.data_size = sizeof (v4);
2134 rd.expiration_time = UINT64_MAX;
2135 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
2137 rh->proc (rh->proc_cls, 1, &rd);
2138 GNS_resolver_lookup_cancel (rh);
2141 if (1 == inet_pton (AF_INET6,
2145 /* name is IPv6 address, pretend it's an AAAA record */
2146 struct GNUNET_GNSRECORD_Data rd;
2149 rd.data_size = sizeof (v6);
2150 rd.expiration_time = UINT64_MAX;
2151 rd.record_type = GNUNET_DNSPARSER_TYPE_AAAA;
2153 rh->proc (rh->proc_cls, 1, &rd);
2154 GNS_resolver_lookup_cancel (rh);
2157 if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
2158 (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
2159 ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
2160 (GNUNET_YES != is_zkey_tld (rh->name)) ) )
2162 /* use standard DNS lookup */
2165 switch (rh->record_type)
2167 case GNUNET_DNSPARSER_TYPE_A:
2170 case GNUNET_DNSPARSER_TYPE_AAAA:
2177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2178 "Doing standard DNS lookup for `%s'\n",
2180 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
2187 if (is_zkey_tld (rh->name))
2189 /* Name ends with ".zkey", try to replace authority zone with zkey
2191 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
2192 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
2195 GNUNET_CRYPTO_ecdsa_public_key_from_string (y,
2197 &rh->authority_zone)) )
2199 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2200 _("Hostname `%s' is not well-formed, resolution fails\n"),
2202 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
2204 GNUNET_free_non_null (y);
2208 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
2209 GNUNET_free (resolver_lookup_get_next_label (rh));
2211 ac = GNUNET_new (struct AuthorityChain);
2213 ac->label = resolver_lookup_get_next_label (rh);
2214 ac->suggested_shortening_label = NULL;
2215 if (NULL == ac->label)
2216 /* name was just "gnu", so we default to label '+' */
2217 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
2218 ac->gns_authority = GNUNET_YES;
2219 ac->authority_info.gns_authority = rh->authority_zone;
2220 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
2223 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
2229 * Lookup of a record in a specific zone calls lookup result processor
2232 * @param zone the zone to perform the lookup in
2233 * @param record_type the record type to look up
2234 * @param name the name to look up
2235 * @param shorten_key a private key for use with PSEU import (can be NULL)
2236 * @param only_cached #GNUNET_NO to only check locally not DHT for performance
2237 * @param proc the processor to call on result
2238 * @param proc_cls the closure to pass to @a proc
2239 * @return handle to cancel operation
2241 struct GNS_ResolverHandle *
2242 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
2243 uint32_t record_type,
2245 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key,
2247 GNS_ResultProcessor proc, void *proc_cls)
2249 struct GNS_ResolverHandle *rh;
2251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2252 (NULL == shorten_key)
2253 ? "Starting lookup for `%s' with shortening disabled\n"
2254 : "Starting lookup for `%s' with shortening enabled\n",
2256 rh = GNUNET_new (struct GNS_ResolverHandle);
2257 GNUNET_CONTAINER_DLL_insert (rlh_head,
2260 rh->authority_zone = *zone;
2262 rh->proc_cls = proc_cls;
2263 rh->only_cached = only_cached;
2264 rh->record_type = record_type;
2265 rh->name = GNUNET_strdup (name);
2266 rh->name_resolution_pos = strlen (name);
2267 if (NULL != shorten_key)
2269 rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
2270 *rh->shorten_key = *shorten_key;
2272 start_resolver_lookup (rh);
2278 * Cancel active resolution (i.e. client disconnected).
2280 * @param rh resolution to abort
2283 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
2285 struct DnsResult *dr;
2286 struct AuthorityChain *ac;
2287 struct VpnContext *vpn_ctx;
2289 GNUNET_CONTAINER_DLL_remove (rlh_head,
2292 while (NULL != (ac = rh->ac_head))
2294 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
2297 GNUNET_free (ac->label);
2298 GNUNET_free_non_null (ac->suggested_shortening_label);
2301 if (NULL != rh->g2dc)
2303 /* rh->g2dc->rh is NOT in the DLL yet, so to enable us
2304 using GNS_resolver_lookup_cancel here, we need to
2306 if (NULL != rh->g2dc->rh)
2308 GNUNET_CONTAINER_DLL_insert (rlh_head,
2311 GNS_resolver_lookup_cancel (rh->g2dc->rh);
2312 rh->g2dc->rh = NULL;
2314 GNUNET_free (rh->g2dc->ns);
2315 GNUNET_free (rh->g2dc);
2318 if (GNUNET_SCHEDULER_NO_TASK != rh->task_id)
2320 GNUNET_SCHEDULER_cancel (rh->task_id);
2321 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
2323 if (NULL != rh->get_handle)
2325 GNUNET_DHT_get_stop (rh->get_handle);
2326 rh->get_handle = NULL;
2328 if (NULL != rh->dht_heap_node)
2330 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
2331 rh->dht_heap_node = NULL;
2333 if (NULL != (vpn_ctx = rh->vpn_ctx))
2335 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
2336 GNUNET_free (vpn_ctx->rd_data);
2337 GNUNET_free (vpn_ctx);
2339 if (NULL != rh->dns_request)
2341 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
2342 rh->dns_request = NULL;
2344 if (NULL != rh->namecache_qe)
2346 GNUNET_NAMECACHE_cancel (rh->namecache_qe);
2347 rh->namecache_qe = NULL;
2349 if (NULL != rh->rev_check)
2351 GNUNET_REVOCATION_query_cancel (rh->rev_check);
2352 rh->rev_check = NULL;
2354 if (NULL != rh->std_resolve)
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2357 "Canceling standard DNS resolution\n");
2358 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
2359 rh->std_resolve = NULL;
2361 while (NULL != (dr = rh->dns_result_head))
2363 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
2364 rh->dns_result_tail,
2368 GNUNET_free_non_null (rh->shorten_key);
2369 GNUNET_free (rh->name);
2374 /* ***************** Resolver initialization ********************* */
2378 * Initialize the resolver
2380 * @param nc the namecache handle
2381 * @param dht the dht handle
2382 * @param c configuration handle
2383 * @param max_bg_queries maximum number of parallel background queries in dht
2386 GNS_resolver_init (struct GNUNET_NAMECACHE_Handle *nc,
2387 struct GNUNET_DHT_Handle *dht,
2388 const struct GNUNET_CONFIGURATION_Handle *c,
2389 unsigned long long max_bg_queries)
2394 namecache_handle = nc;
2397 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2398 max_allowed_background_queries = max_bg_queries;
2400 GNUNET_CONFIGURATION_get_value_string (c,
2405 /* user did not specify DNS resolver, use 8.8.8.8 */
2406 dns_ip = GNUNET_strdup ("8.8.8.8");
2408 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
2409 GNUNET_free (dns_ip);
2410 vpn_handle = GNUNET_VPN_connect (cfg);
2418 GNS_resolver_done ()
2420 struct GNS_ResolverHandle *rh;
2421 struct CacheOps *co;
2423 /* abort active resolutions */
2424 while (NULL != (rh = rlh_head))
2426 rh->proc (rh->proc_cls, 0, NULL);
2427 GNS_resolver_lookup_cancel (rh);
2429 while (NULL != (co = co_head))
2431 GNUNET_CONTAINER_DLL_remove (co_head,
2434 GNUNET_NAMECACHE_cancel (co->namecache_qe_cache);
2437 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
2438 dht_lookup_heap = NULL;
2439 GNUNET_DNSSTUB_stop (dns_handle);
2441 GNUNET_VPN_disconnect (vpn_handle);
2444 namecache_handle = NULL;
2448 /* *************** common helper functions (do not really belong here) *********** */
2451 * Checks if @a name ends in ".TLD"
2453 * @param name the name to check
2454 * @param tld the TLD to check for
2455 * @return GNUNET_YES or GNUNET_NO
2458 is_tld (const char* name, const char* tld)
2462 if (strlen (name) <= strlen (tld))
2464 offset = strlen (name) - strlen (tld);
2465 if (0 != strcmp (name + offset, tld))
2471 /* end of gnunet-service-gns_resolver.c */