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_EcdsaPublicKey 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_EcdsaPublicKey authority_zone;
236 * called when resolution phase finishes
238 GNS_ResultProcessor proc;
241 * closure passed to proc
246 * Handle for DHT lookups. should be NULL if no lookups are in progress
248 struct GNUNET_DHT_GetHandle *get_handle;
251 * Handle to a VPN request, NULL if none is active.
253 struct VpnContext *vpn_ctx;
256 * Socket for a DNS request, NULL if none is active.
258 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
261 * Handle for standard DNS resolution, NULL if none is active.
263 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
266 * Pending Namestore lookup task
268 struct GNUNET_NAMESTORE_QueueEntry *namestore_qe;
271 * Heap node associated with this lookup. Used to limit number of
272 * concurrent requests.
274 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
277 * DLL to store the authority chain
279 struct AuthorityChain *ac_head;
282 * DLL to store the authority chain
284 struct AuthorityChain *ac_tail;
287 * Private key of the shorten zone, NULL to not shorten.
289 struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key;
292 * ID of a task associated with the resolution process.
294 GNUNET_SCHEDULER_TaskIdentifier task_id;
297 * The name to resolve
302 * DLL of results we got from DNS.
304 struct DnsResult *dns_result_head;
307 * DLL of results we got from DNS.
309 struct DnsResult *dns_result_tail;
312 * Current offset in 'name' where we are resolving.
314 size_t name_resolution_pos;
322 * Desired type for the resolution.
327 * We increment the loop limiter for each step in a recursive
328 * resolution. If it passes our threshold (i.e. due to
329 * self-recursion in the resolution, i.e CNAME fun), we stop.
331 unsigned int loop_limiter;
337 * Active namestore caching operations.
343 * Organized in a DLL.
345 struct CacheOps *next;
348 * Organized in a DLL.
350 struct CacheOps *prev;
353 * Pending Namestore caching task.
355 struct GNUNET_NAMESTORE_QueueEntry *namestore_qe_cache;
361 * Our handle to the namestore service
363 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
366 * Our handle to the vpn service
368 static struct GNUNET_VPN_Handle *vpn_handle;
371 * Resolver handle to the dht
373 static struct GNUNET_DHT_Handle *dht_handle;
376 * Handle to perform DNS lookups.
378 static struct GNUNET_DNSSTUB_Context *dns_handle;
381 * Heap for limiting parallel DHT lookups
383 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
386 * Maximum amount of parallel queries to the DHT
388 static unsigned long long max_allowed_background_queries;
391 * Head of resolver lookup list
393 static struct GNS_ResolverHandle *rlh_head;
396 * Tail of resolver lookup list
398 static struct GNS_ResolverHandle *rlh_tail;
401 * Organized in a DLL.
403 static struct CacheOps *co_head;
406 * Organized in a DLL.
408 static struct CacheOps *co_tail;
412 * Global configuration.
414 static const struct GNUNET_CONFIGURATION_Handle *cfg;
418 * Check if name is in srv format (_x._y.xxx)
421 * @return #GNUNET_YES if true
424 is_srv (const char *name)
431 if (NULL == strstr (name, "._"))
434 ndup = GNUNET_strdup (name);
436 if (NULL == strtok (NULL, "."))
438 if (NULL == strtok (NULL, "."))
440 if (NULL != strtok (NULL, "."))
449 * Determine if this name is canonical (is a legal name in a zone, without delegation);
450 * note that we do not test that the name does not contain illegal characters, we only
451 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
452 * even though they consist of multiple labels.
455 * a.b.gnu = not canonical
457 * _foo._srv = canonical
458 * _f.bar = not canonical
460 * @param name the name to test
461 * @return #GNUNET_YES if canonical
464 is_canonical (const char *name)
469 if (NULL == strchr (name, '.'))
474 while (NULL != (dot = strchr (pos, '.')))
482 /* ************************** Resolution **************************** */
485 * Exands a name ending in .+ with the zone of origin.
487 * @param rh resolution context
488 * @param name name to modify (to be free'd or returned)
489 * @return updated name
492 translate_dot_plus (struct GNS_ResolverHandle *rh,
496 size_t s_len = strlen (name);
498 if (0 != strcmp (&name[s_len - 2],
500 return name; /* did not end in ".+" */
501 GNUNET_assert (GNUNET_YES == rh->ac_tail->gns_authority);
502 GNUNET_asprintf (&ret,
506 GNUNET_NAMESTORE_pkey_to_zkey (&rh->ac_tail->authority_info.gns_authority));
513 * Task scheduled to asynchronously fail a resolution.
515 * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
516 * @param tc task context
519 fail_resolution (void *cls,
520 const struct GNUNET_SCHEDULER_TaskContext *tc)
522 struct GNS_ResolverHandle *rh = cls;
524 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
525 rh->proc (rh->proc_cls, 0, NULL);
526 GNS_resolver_lookup_cancel (rh);
530 #if (defined WINDOWS) || (defined DARWIN)
531 /* Don't have this on W32, here's a naive implementation
532 * Was somehow removed on OS X ... */
534 memrchr (const void *s,
538 const unsigned char *ucs = s;
541 for (i = n - 1; i >= 0; i--)
542 if (c == (int) ucs[i])
543 return (void *) &ucs[i];
550 * Get the next, rightmost label from the name that we are trying to resolve,
551 * and update the resolution position accordingly.
553 * @param rh handle to the resolution operation to get the next label from
554 * @return NULL if there are no more labels
557 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
563 if (0 == rh->name_resolution_pos)
565 dot = memrchr (rh->name, (int) '.', rh->name_resolution_pos);
568 /* done, this was the last one */
569 len = rh->name_resolution_pos;
571 rh->name_resolution_pos = 0;
575 /* advance by one label */
576 len = rh->name_resolution_pos - (dot - rh->name) - 1;
578 rh->name_resolution_pos = dot - rh->name;
580 return GNUNET_strndup (rp, len);
585 * Gives the cummulative result obtained to the callback and clean up the request.
587 * @param rh resolution process that has culminated in a result
590 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
592 struct DnsResult *pos;
597 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
600 struct GNUNET_NAMESTORE_RecordData rd[n];
603 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
605 rd[i].data = pos->data;
606 rd[i].data_size = pos->data_size;
607 rd[i].record_type = pos->record_type;
608 if (0 == pos->expiration_time)
610 rd[i].flags = GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
611 rd[i].expiration_time = 0;
615 rd[i].flags = GNUNET_NAMESTORE_RF_NONE;
616 rd[i].expiration_time = pos->expiration_time;
620 GNUNET_assert (i == n);
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
622 "Transmitting standard DNS result with %u records\n",
624 rh->proc (rh->proc_cls,
628 GNS_resolver_lookup_cancel (rh);
633 * Add a result from DNS to the records to be returned to the application.
635 * @param rh resolution request to extend with a result
636 * @param expiration_time expiration time for the answer
637 * @param record_type DNS record type of the answer
638 * @param data_size number of bytes in @a data
639 * @param data binary data to return in DNS record
642 add_dns_result (struct GNS_ResolverHandle *rh,
643 uint64_t expiration_time,
644 uint32_t record_type,
648 struct DnsResult *res;
650 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
651 res->expiration_time = expiration_time;
652 res->data_size = data_size;
653 res->record_type = record_type;
655 memcpy (&res[1], data, data_size);
656 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
663 * We had to do a DNS lookup. Convert the result (if any) and return
666 * @param cls closure with the 'struct GNS_ResolverHandle'
667 * @param addr one of the addresses of the host, NULL for the last address
668 * @param addrlen length of the address
671 handle_dns_result (void *cls,
672 const struct sockaddr *addr,
675 struct GNS_ResolverHandle *rh = cls;
676 const struct sockaddr_in *sa4;
677 const struct sockaddr_in6 *sa6;
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "Received %u bytes of DNS IP data\n",
684 rh->std_resolve = NULL;
685 transmit_lookup_dns_result (rh);
688 switch (addr->sa_family)
691 sa4 = (const struct sockaddr_in *) addr;
693 0 /* expiration time is unknown */,
694 GNUNET_DNSPARSER_TYPE_A,
695 sizeof (struct in_addr),
699 sa6 = (const struct sockaddr_in6 *) addr;
701 0 /* expiration time is unknown */,
702 GNUNET_DNSPARSER_TYPE_AAAA,
703 sizeof (struct in6_addr),
714 * Task scheduled to continue with the resolution process.
716 * @param cls the 'struct GNS_ResolverHandle' of the resolution
717 * @param tc task context
720 recursive_resolution (void *cls,
721 const struct GNUNET_SCHEDULER_TaskContext *tc);
725 * Begin the resolution process from 'name', starting with
726 * the identification of the zone specified by 'name'.
728 * @param rh resolution to perform
731 start_resolver_lookup (struct GNS_ResolverHandle *rh);
735 * Function called with the result of a DNS resolution.
737 * @param cls the request handle of the resolution that
738 * we were attempting to make
739 * @param rs socket that received the response
740 * @param dns dns response, never NULL
741 * @param dns_len number of bytes in @a dns
744 dns_result_parser (void *cls,
745 struct GNUNET_DNSSTUB_RequestSocket *rs,
746 const struct GNUNET_TUN_DnsHeader *dns,
749 struct GNS_ResolverHandle *rh = cls;
750 struct GNUNET_DNSPARSER_Packet *p;
751 const struct GNUNET_DNSPARSER_Record *rec;
752 unsigned int rd_count;
755 rh->dns_request = NULL;
756 GNUNET_SCHEDULER_cancel (rh->task_id);
757 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
758 p = GNUNET_DNSPARSER_parse ((const char *) dns,
762 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
763 _("Failed to parse DNS response\n"));
764 rh->proc (rh->proc_cls, 0, NULL);
765 GNS_resolver_lookup_cancel (rh);
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769 "Received DNS response for `%s' with %u answers\n",
771 (unsigned int) p->num_answers);
772 if ( (p->num_answers > 0) &&
773 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
774 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
776 GNUNET_free (rh->name);
777 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
778 start_resolver_lookup (rh);
779 GNUNET_DNSPARSER_free_packet (p);
782 /* FIXME: add DNAME support */
784 /* convert from (parsed) DNS to (binary) GNS format! */
785 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
787 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
789 char buf[UINT16_MAX];
795 memset (rd, 0, sizeof (rd));
796 for (i=0;i<rd_count;i++)
798 if (i < p->num_answers)
799 rec = &p->answers[i];
800 else if (i < p->num_answers + p->num_authority_records)
801 rec = &p->authority_records[i - p->num_answers];
803 rec = &p->authority_records[i - p->num_answers - p->num_authority_records];
804 /* As we copied the full DNS name to 'rh->ac_tail->label', this
805 should be the correct check to see if this record is actually
806 a record for our label... */
807 if (0 != strcmp (rec->name,
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 "Dropping record `%s', does not match desired name `%s'\n",
817 rd[i - skip].record_type = rec->type;
818 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
821 case GNUNET_DNSPARSER_TYPE_A:
822 if (rec->data.raw.data_len != sizeof (struct in_addr))
828 rd[i - skip].data_size = rec->data.raw.data_len;
829 rd[i - skip].data = rec->data.raw.data;
831 case GNUNET_DNSPARSER_TYPE_AAAA:
832 if (rec->data.raw.data_len != sizeof (struct in6_addr))
838 rd[i - skip].data_size = rec->data.raw.data_len;
839 rd[i - skip].data = rec->data.raw.data;
841 case GNUNET_DNSPARSER_TYPE_CNAME:
842 case GNUNET_DNSPARSER_TYPE_PTR:
843 case GNUNET_DNSPARSER_TYPE_NS:
846 GNUNET_DNSPARSER_builder_add_name (buf,
855 rd[i - skip].data_size = buf_off - buf_start;
856 rd[i - skip].data = &buf[buf_start];
858 case GNUNET_DNSPARSER_TYPE_SOA:
861 GNUNET_DNSPARSER_builder_add_soa (buf,
870 rd[i - skip].data_size = buf_off - buf_start;
871 rd[i - skip].data = &buf[buf_start];
873 case GNUNET_DNSPARSER_TYPE_MX:
876 GNUNET_DNSPARSER_builder_add_mx (buf,
885 rd[i - skip].data_size = buf_off - buf_start;
886 rd[i - skip].data = &buf[buf_start];
888 case GNUNET_DNSPARSER_TYPE_SRV:
891 GNUNET_DNSPARSER_builder_add_srv (buf,
900 rd[i - skip].data_size = buf_off - buf_start;
901 rd[i - skip].data = &buf[buf_start];
904 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
905 _("Skipping record of unsupported type %d\n"),
911 rh->proc (rh->proc_cls, rd_count - skip, rd);
912 GNS_resolver_lookup_cancel (rh);
914 GNUNET_DNSPARSER_free_packet (p);
919 * Perform recursive DNS resolution. Asks the given DNS resolver to
920 * resolve "rh->dns_name", possibly recursively proceeding following
921 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
922 * we find the answer.
924 * @param rh resolution information
927 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
929 struct AuthorityChain *ac;
931 struct GNUNET_DNSPARSER_Query *query;
932 struct GNUNET_DNSPARSER_Packet *p;
934 size_t dns_request_length;
937 GNUNET_assert (NULL != ac);
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
939 "Starting DNS lookup for `%s'\n",
941 GNUNET_assert (GNUNET_NO == ac->gns_authority);
942 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
945 sa_len = sizeof (struct sockaddr_in);
948 sa_len = sizeof (struct sockaddr_in6);
952 rh->proc (rh->proc_cls, 0, NULL);
953 GNS_resolver_lookup_cancel (rh);
956 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
957 query->name = GNUNET_strdup (ac->label);
958 query->type = rh->record_type;
959 query->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
960 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
963 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
965 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
966 p->flags.recursion_desired = 1;
968 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
971 rh->proc (rh->proc_cls, 0, NULL);
972 GNS_resolver_lookup_cancel (rh);
976 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
977 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
983 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
987 GNUNET_free (dns_request);
988 GNUNET_DNSPARSER_free_packet (p);
993 * We encountered a CNAME record during our resolution.
994 * Merge it into our chain.
996 * @param rh resolution we are performing
997 * @param cname value of the cname record we got for the current
998 * authority chain tail
1001 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
1006 struct AuthorityChain *ac;
1008 nlen = strlen (cname);
1011 &cname[nlen - 2])) )
1013 /* CNAME resolution continues relative to current domain */
1014 if (0 == rh->name_resolution_pos)
1016 res = GNUNET_strndup (cname, nlen - 2);
1017 rh->name_resolution_pos = nlen - 2;
1021 GNUNET_asprintf (&res,
1023 (int) rh->name_resolution_pos,
1027 rh->name_resolution_pos = strlen (res);
1029 GNUNET_free (rh->name);
1031 ac = GNUNET_new (struct AuthorityChain);
1033 ac->gns_authority = GNUNET_YES;
1034 ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
1035 ac->label = resolver_lookup_get_next_label (rh);
1036 /* tigger shortening */
1037 if (NULL != rh->shorten_key)
1038 GNS_shorten_start (rh->ac_tail->label,
1039 &ac->authority_info.gns_authority,
1041 /* add AC to tail */
1042 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1045 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1049 /* name is absolute, start from the beginning */
1050 GNUNET_free (rh->name);
1051 rh->name = GNUNET_strdup (cname);
1052 start_resolver_lookup (rh);
1057 * Process a records that were decrypted from a block.
1059 * @param cls closure with the 'struct GNS_ResolverHandle'
1060 * @param rd_count number of entries in @a rd array
1061 * @param rd array of records with data to store
1064 handle_gns_resolution_result (void *cls,
1065 unsigned int rd_count,
1066 const struct GNUNET_NAMESTORE_RecordData *rd);
1070 * Callback invoked from the VPN service once a redirection is
1071 * available. Provides the IP address that can now be used to
1072 * reach the requested destination. Replaces the "VPN" record
1073 * with the respective A/AAAA record and continues processing.
1075 * @param cls closure
1076 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1077 * will match 'result_af' from the request
1078 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1079 * that the VPN allocated for the redirection;
1080 * traffic to this IP will now be redirected to the
1081 * specified target peer; NULL on error
1084 vpn_allocation_cb (void *cls,
1086 const void *address)
1088 struct VpnContext *vpn_ctx = cls;
1089 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1090 struct GNUNET_NAMESTORE_RecordData rd[vpn_ctx->rd_count];
1093 vpn_ctx->vpn_request = NULL;
1095 GNUNET_assert (GNUNET_OK ==
1096 GNUNET_NAMESTORE_records_deserialize (vpn_ctx->rd_data_size,
1100 for (i=0;i<vpn_ctx->rd_count;i++)
1102 if (GNUNET_NAMESTORE_TYPE_VPN == rd[i].record_type)
1107 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1108 rd[i].data_size = sizeof (struct in_addr);
1109 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1111 rd[i].data = address;
1114 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1115 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1117 rd[i].data = address;
1118 rd[i].data_size = sizeof (struct in6_addr);
1126 GNUNET_assert (i < vpn_ctx->rd_count);
1127 handle_gns_resolution_result (rh,
1130 GNUNET_free (vpn_ctx->rd_data);
1131 GNUNET_free (vpn_ctx);
1136 * Process a records that were decrypted from a block.
1138 * @param cls closure with the `struct GNS_ResolverHandle`
1139 * @param rd_count number of entries in @a rd array
1140 * @param rd array of records with data to store
1143 handle_gns_resolution_result (void *cls,
1144 unsigned int rd_count,
1145 const struct GNUNET_NAMESTORE_RecordData *rd)
1147 struct GNS_ResolverHandle *rh = cls;
1148 struct AuthorityChain *ac;
1151 struct sockaddr *sa;
1152 struct sockaddr_in v4;
1153 struct sockaddr_in6 v6;
1156 struct VpnContext *vpn_ctx;
1157 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1159 struct GNUNET_HashCode vhash;
1161 char scratch[UINT16_MAX];
1163 size_t scratch_start;
1165 struct GNUNET_NAMESTORE_RecordData rd_new[rd_count];
1166 unsigned int rd_off;
1168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1169 "Resolution succeeded for `%s' in zone %s, got %u records\n",
1171 GNUNET_NAMESTORE_z2s (&rh->ac_tail->authority_info.gns_authority),
1173 if (0 == rh->name_resolution_pos)
1175 /* top-level match, are we done yet? */
1176 if ( (rd_count > 0) &&
1177 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1178 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1181 cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
1184 if ( (NULL == cname) ||
1185 (off != rd[0].data_size) )
1187 GNUNET_break_op (0);
1188 rh->proc (rh->proc_cls, 0, NULL);
1189 GNS_resolver_lookup_cancel (rh);
1192 handle_gns_cname_result (rh,
1194 GNUNET_free (cname);
1197 /* If A/AAAA was requested, but we got a VPN
1198 record, we convert it to A/AAAA using GNUnet VPN */
1199 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1200 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1202 for (i=0;i<rd_count;i++)
1204 switch (rd[i].record_type)
1206 case GNUNET_NAMESTORE_TYPE_VPN:
1208 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1209 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) <
1212 GNUNET_break_op (0);
1213 rh->proc (rh->proc_cls, 0, NULL);
1214 GNS_resolver_lookup_cancel (rh);
1217 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1218 vname = (const char *) &vpn[1];
1219 if ('\0' != vname[rd[i].data_size - 1 - sizeof (struct GNUNET_TUN_GnsVpnRecord)])
1221 GNUNET_break_op (0);
1222 rh->proc (rh->proc_cls, 0, NULL);
1223 GNS_resolver_lookup_cancel (rh);
1226 GNUNET_CRYPTO_hash (vname,
1227 strlen (vname), // FIXME: +1?
1229 vpn_ctx = GNUNET_new (struct VpnContext);
1230 rh->vpn_ctx = vpn_ctx;
1232 vpn_ctx->rd_data_size = GNUNET_NAMESTORE_records_get_size (rd_count,
1234 vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
1235 (void) GNUNET_NAMESTORE_records_serialize (rd_count,
1237 vpn_ctx->rd_data_size,
1239 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1244 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1249 case GNUNET_NAMESTORE_TYPE_GNS2DNS:
1251 /* delegation to DNS */
1259 /* convert relative names in record values to absolute names,
1260 using 'scratch' array for memory allocations */
1263 for (i=0;i<rd_count;i++)
1265 rd_new[rd_off] = rd[i];
1266 /* Check if the embedded name(s) end in "+", and if so,
1267 replace the "+" with the zone at "ac_tail", changing the name
1268 to a ".zkey". The name is allocated on the 'scratch' array,
1269 so we can free it afterwards. */
1270 switch (rd[i].record_type)
1272 case GNUNET_DNSPARSER_TYPE_CNAME:
1277 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1280 if ( (NULL == cname) ||
1281 (off != rd[i].data_size) )
1283 GNUNET_break_op (0); /* record not well-formed */
1287 cname = translate_dot_plus (rh, cname);
1288 scratch_start = scratch_off;
1290 GNUNET_DNSPARSER_builder_add_name (scratch,
1299 rd_new[rd_off].data = &scratch[scratch_start];
1300 rd_new[rd_off].data_size = scratch_off - scratch_start;
1304 GNUNET_free_non_null (cname);
1307 case GNUNET_DNSPARSER_TYPE_SOA:
1309 struct GNUNET_DNSPARSER_SoaRecord *soa;
1312 soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
1315 if ( (NULL == soa) ||
1316 (off != rd[i].data_size) )
1318 GNUNET_break_op (0); /* record not well-formed */
1322 soa->mname = translate_dot_plus (rh, soa->mname);
1323 soa->rname = translate_dot_plus (rh, soa->rname);
1324 scratch_start = scratch_off;
1326 GNUNET_DNSPARSER_builder_add_soa (scratch,
1335 rd_new[rd_off].data = &scratch[scratch_start];
1336 rd_new[rd_off].data_size = scratch_off - scratch_start;
1341 GNUNET_DNSPARSER_free_soa (soa);
1344 case GNUNET_DNSPARSER_TYPE_MX:
1346 struct GNUNET_DNSPARSER_MxRecord *mx;
1349 mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
1352 if ( (NULL == mx) ||
1353 (off != rd[i].data_size) )
1355 GNUNET_break_op (0); /* record not well-formed */
1359 mx->mxhost = translate_dot_plus (rh, mx->mxhost);
1360 scratch_start = scratch_off;
1362 GNUNET_DNSPARSER_builder_add_mx (scratch,
1371 rd_new[rd_off].data = &scratch[scratch_start];
1372 rd_new[rd_off].data_size = scratch_off - scratch_start;
1377 GNUNET_DNSPARSER_free_mx (mx);
1380 case GNUNET_DNSPARSER_TYPE_SRV:
1382 struct GNUNET_DNSPARSER_SrvRecord *srv;
1385 /* FIXME: passing rh->name here is is not necessarily what we want
1386 (SRV support not finished) */
1387 srv = GNUNET_DNSPARSER_parse_srv (rh->name,
1391 if ( (NULL == srv) ||
1392 (off != rd[i].data_size) )
1394 GNUNET_break_op (0); /* record not well-formed */
1398 srv->domain_name = translate_dot_plus (rh, srv->domain_name);
1399 srv->target = translate_dot_plus (rh, srv->target);
1400 scratch_start = scratch_off;
1402 GNUNET_DNSPARSER_builder_add_srv (scratch,
1411 rd_new[rd_off].data = &scratch[scratch_start];
1412 rd_new[rd_off].data_size = scratch_off - scratch_start;
1417 GNUNET_DNSPARSER_free_srv (srv);
1420 case GNUNET_NAMESTORE_TYPE_PKEY:
1421 /* tigger shortening */
1422 if (NULL != rh->shorten_key)
1424 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
1426 if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
1428 GNUNET_break_op (0);
1431 memcpy (&pub, rd[i].data, rd[i].data_size);
1432 GNS_shorten_start (rh->ac_tail->label,
1444 /* yes, we are done, return result */
1445 rh->proc (rh->proc_cls, rd_off, rd_new);
1446 GNS_resolver_lookup_cancel (rh);
1450 /* need to recurse, check if we can */
1451 for (i=0;i<rd_count;i++)
1453 switch (rd[i].record_type)
1455 case GNUNET_NAMESTORE_TYPE_PKEY:
1456 /* delegation to another zone */
1457 if (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
1460 GNUNET_break_op (0);
1461 rh->proc (rh->proc_cls, 0, NULL);
1462 GNS_resolver_lookup_cancel (rh);
1465 /* expand authority chain */
1466 ac = GNUNET_new (struct AuthorityChain);
1468 ac->gns_authority = GNUNET_YES;
1469 memcpy (&ac->authority_info.gns_authority,
1471 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1472 ac->label = resolver_lookup_get_next_label (rh);
1473 /* tigger shortening */
1474 if (NULL != rh->shorten_key)
1475 GNS_shorten_start (rh->ac_tail->label,
1476 &ac->authority_info.gns_authority,
1478 /* add AC to tail */
1479 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1483 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1486 case GNUNET_NAMESTORE_TYPE_GNS2DNS:
1489 /* resolution continues within DNS */
1490 if (GNUNET_DNSPARSER_MAX_NAME_LENGTH < rd[i].data_size)
1492 GNUNET_break_op (0);
1493 rh->proc (rh->proc_cls, 0, NULL);
1494 GNS_resolver_lookup_cancel (rh);
1497 /* find associated A/AAAA record */
1500 for (j=0;j<rd_count;j++)
1502 switch (rd[j].record_type)
1504 case GNUNET_DNSPARSER_TYPE_A:
1505 if (sizeof (struct in_addr) != rd[j].data_size)
1507 GNUNET_break_op (0);
1508 rh->proc (rh->proc_cls, 0, NULL);
1509 GNS_resolver_lookup_cancel (rh);
1512 /* FIXME: might want to check if we support IPv4 here,
1513 and otherwise skip this one and hope we find another */
1514 memset (&v4, 0, sizeof (v4));
1515 sa_len = sizeof (v4);
1516 v4.sin_family = AF_INET;
1517 v4.sin_port = htons (53);
1518 #if HAVE_SOCKADDR_IN_SIN_LEN
1519 v4.sin_len = (u_char) sa_len;
1521 memcpy (&v4.sin_addr,
1523 sizeof (struct in_addr));
1524 sa = (struct sockaddr *) &v4;
1526 case GNUNET_DNSPARSER_TYPE_AAAA:
1527 if (sizeof (struct in6_addr) != rd[j].data_size)
1529 GNUNET_break_op (0);
1530 rh->proc (rh->proc_cls, 0, NULL);
1531 GNS_resolver_lookup_cancel (rh);
1534 /* FIXME: might want to check if we support IPv6 here,
1535 and otherwise skip this one and hope we find another */
1536 memset (&v6, 0, sizeof (v6));
1537 sa_len = sizeof (v6);
1538 v6.sin6_family = AF_INET6;
1539 v6.sin6_port = htons (53);
1540 #if HAVE_SOCKADDR_IN_SIN_LEN
1541 v6.sin6_len = (u_char) sa_len;
1543 memcpy (&v6.sin6_addr,
1545 sizeof (struct in6_addr));
1546 sa = (struct sockaddr *) &v6;
1556 /* we cannot continue; NS without A/AAAA */
1557 rh->proc (rh->proc_cls, 0, NULL);
1558 GNS_resolver_lookup_cancel (rh);
1561 /* expand authority chain */
1562 ac = GNUNET_new (struct AuthorityChain);
1565 ns = GNUNET_DNSPARSER_parse_name (rd[i].data,
1568 if ( (NULL == ns) ||
1569 (off != rd[i].data_size) )
1571 GNUNET_break_op (0); /* record not well-formed */
1572 rh->proc (rh->proc_cls, 0, NULL);
1573 GNS_resolver_lookup_cancel (rh);
1574 GNUNET_free_non_null (ns);
1578 strcpy (ac->authority_info.dns_authority.name,
1580 memcpy (&ac->authority_info.dns_authority.dns_ip,
1583 /* for DNS recursion, the label is the full DNS name,
1584 created from the remainder of the GNS name and the
1585 name in the NS record */
1586 GNUNET_asprintf (&ac->label,
1588 (int) rh->name_resolution_pos,
1590 (0 != rh->name_resolution_pos) ? "." : "",
1593 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1596 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1598 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1599 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1601 rh->proc (rh->proc_cls, 0, NULL);
1602 GNS_resolver_lookup_cancel (rh);
1606 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1610 case GNUNET_DNSPARSER_TYPE_CNAME:
1615 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1618 if ( (NULL == cname) ||
1619 (off != rd[i].data_size) )
1621 GNUNET_break_op (0); /* record not well-formed */
1622 rh->proc (rh->proc_cls, 0, NULL);
1623 GNS_resolver_lookup_cancel (rh);
1624 GNUNET_free_non_null (cname);
1627 handle_gns_cname_result (rh,
1629 GNUNET_free (cname);
1632 /* FIXME: handle DNAME */
1638 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1639 _("GNS lookup recursion failed (no delegation record found)\n"));
1640 rh->proc (rh->proc_cls, 0, NULL);
1641 GNS_resolver_lookup_cancel (rh);
1646 * Function called once the namestore has completed the request for
1649 * @param cls closure with the `struct CacheOps`
1650 * @param success #GNUNET_OK on success
1651 * @param emsg error message
1654 namestore_cache_continuation (void *cls,
1658 struct CacheOps *co = cls;
1660 co->namestore_qe_cache = NULL;
1662 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1663 _("Failed to cache GNS resolution: %s\n"),
1665 GNUNET_CONTAINER_DLL_remove (co_head,
1673 * Iterator called on each result obtained for a DHT
1674 * operation that expects a reply
1676 * @param cls closure with the `struct GNS_ResolverHandle`
1677 * @param exp when will this value expire
1678 * @param key key of the result
1679 * @param get_path peers on reply path (or NULL if not recorded)
1680 * [0] = datastore's first neighbor, [length - 1] = local peer
1681 * @param get_path_length number of entries in @a get_path
1682 * @param put_path peers on the PUT path (or NULL if not recorded)
1683 * [0] = origin, [length - 1] = datastore
1684 * @param put_path_length number of entries in @a put_path
1685 * @param type type of the result
1686 * @param size number of bytes in data
1687 * @param data pointer to the result data
1690 handle_dht_response (void *cls,
1691 struct GNUNET_TIME_Absolute exp,
1692 const struct GNUNET_HashCode *key,
1693 const struct GNUNET_PeerIdentity *get_path,
1694 unsigned int get_path_length,
1695 const struct GNUNET_PeerIdentity *put_path,
1696 unsigned int put_path_length,
1697 enum GNUNET_BLOCK_Type type,
1698 size_t size, const void *data)
1700 struct GNS_ResolverHandle *rh = cls;
1701 struct AuthorityChain *ac = rh->ac_tail;
1702 const struct GNUNET_NAMESTORE_Block *block;
1703 struct CacheOps *co;
1705 GNUNET_DHT_get_stop (rh->get_handle);
1706 rh->get_handle = NULL;
1707 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1708 rh->dht_heap_node = NULL;
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1710 "Handling response from the DHT\n");
1711 if (size < sizeof (struct GNUNET_NAMESTORE_Block))
1713 /* how did this pass DHT block validation!? */
1715 rh->proc (rh->proc_cls, 0, NULL);
1716 GNS_resolver_lookup_cancel (rh);
1721 ntohl (block->purpose.size) +
1722 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
1723 sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
1725 /* how did this pass DHT block validation!? */
1727 rh->proc (rh->proc_cls, 0, NULL);
1728 GNS_resolver_lookup_cancel (rh);
1732 GNUNET_NAMESTORE_block_decrypt (block,
1733 &ac->authority_info.gns_authority,
1735 &handle_gns_resolution_result,
1738 GNUNET_break_op (0); /* block was ill-formed */
1739 rh->proc (rh->proc_cls, 0, NULL);
1740 GNS_resolver_lookup_cancel (rh);
1743 /* Cache well-formed blocks */
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1745 "Caching response from the DHT in namestore\n");
1746 co = GNUNET_new (struct CacheOps);
1747 co->namestore_qe_cache = GNUNET_NAMESTORE_block_cache (namestore_handle,
1749 &namestore_cache_continuation,
1751 GNUNET_CONTAINER_DLL_insert (co_head,
1758 * Process a record that was stored in the namestore.
1760 * @param cls closure with the `struct GNS_ResolverHandle`
1761 * @param block block that was stored in the namestore
1764 handle_namestore_block_response (void *cls,
1765 const struct GNUNET_NAMESTORE_Block *block)
1767 struct GNS_ResolverHandle *rh = cls;
1768 struct GNS_ResolverHandle *rx;
1769 struct AuthorityChain *ac = rh->ac_tail;
1770 const char *label = ac->label;
1771 const struct GNUNET_CRYPTO_EcdsaPublicKey *auth = &ac->authority_info.gns_authority;
1772 struct GNUNET_HashCode query;
1774 GNUNET_NAMESTORE_query_from_public_key (auth,
1777 GNUNET_assert (NULL != rh->namestore_qe);
1778 rh->namestore_qe = NULL;
1779 if ( (GNUNET_NO == rh->only_cached) &&
1780 ( (NULL == block) ||
1781 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
1783 /* Namestore knows nothing; try DHT lookup */
1784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1785 "Starting DHT lookup for `%s' in zone %s\n",
1787 GNUNET_NAMESTORE_z2s (&ac->authority_info.gns_authority));
1788 GNUNET_assert (NULL == rh->get_handle);
1789 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1790 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1792 DHT_GNS_REPLICATION_LEVEL,
1793 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1795 &handle_dht_response, rh);
1796 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1798 GNUNET_TIME_absolute_get ().abs_value_us);
1799 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
1801 /* fail longest-standing DHT request */
1802 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
1803 GNUNET_assert (NULL != rx);
1804 rx->proc (rx->proc_cls, 0, NULL);
1805 GNS_resolver_lookup_cancel (rx);
1809 if ( (NULL == block) ||
1810 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
1812 /* DHT not permitted and no local result, fail */
1813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1814 "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
1816 GNUNET_NAMESTORE_z2s (&ac->authority_info.gns_authority));
1817 rh->proc (rh->proc_cls, 0, NULL);
1818 GNS_resolver_lookup_cancel (rh);
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1822 "Decrypting block from the namestore\n");
1824 GNUNET_NAMESTORE_block_decrypt (block,
1827 &handle_gns_resolution_result,
1830 GNUNET_break_op (0); /* block was ill-formed */
1831 rh->proc (rh->proc_cls, 0, NULL);
1832 GNS_resolver_lookup_cancel (rh);
1839 * Lookup tail of our authority chain in the namestore.
1841 * @param rh query we are processing
1844 recursive_gns_resolution_namestore (struct GNS_ResolverHandle *rh)
1846 struct AuthorityChain *ac = rh->ac_tail;
1847 struct GNUNET_HashCode query;
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1850 "Starting GNS resolution for `%s' in zone %s\n",
1852 GNUNET_NAMESTORE_z2s (&ac->authority_info.gns_authority));
1853 GNUNET_NAMESTORE_query_from_public_key (&ac->authority_info.gns_authority,
1856 rh->namestore_qe = GNUNET_NAMESTORE_lookup_block (namestore_handle,
1858 &handle_namestore_block_response,
1860 GNUNET_assert (NULL != rh->namestore_qe);
1865 * Task scheduled to continue with the resolution process.
1867 * @param cls the `struct GNS_ResolverHandle` of the resolution
1868 * @param tc task context
1871 recursive_resolution (void *cls,
1872 const struct GNUNET_SCHEDULER_TaskContext *tc)
1874 struct GNS_ResolverHandle *rh = cls;
1876 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
1877 if (MAX_RECURSION < rh->loop_limiter++)
1879 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1880 "Encountered unbounded recursion resolving `%s'\n",
1882 rh->proc (rh->proc_cls, 0, NULL);
1883 GNS_resolver_lookup_cancel (rh);
1886 if (GNUNET_YES == rh->ac_tail->gns_authority)
1887 recursive_gns_resolution_namestore (rh);
1889 recursive_dns_resolution (rh);
1894 * Begin the resolution process from 'name', starting with
1895 * the identification of the zone specified by 'name'.
1897 * @param rh resolution to perform
1900 start_resolver_lookup (struct GNS_ResolverHandle *rh)
1902 struct AuthorityChain *ac;
1907 if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
1908 (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
1909 ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
1910 (GNUNET_YES != is_zkey_tld (rh->name)) ) )
1912 /* use standard DNS lookup */
1915 switch (rh->record_type)
1917 case GNUNET_DNSPARSER_TYPE_A:
1920 case GNUNET_DNSPARSER_TYPE_AAAA:
1927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1928 "Doing standard DNS lookup for `%s'\n",
1930 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
1937 if (is_zkey_tld (rh->name))
1939 /* Name ends with ".zkey", try to replace authority zone with zkey
1941 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
1942 x = resolver_lookup_get_next_label (rh); /* will return 'x' coordinate */
1943 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
1944 GNUNET_asprintf (&pkey,
1950 GNUNET_CRYPTO_ecdsa_public_key_from_string (pkey,
1952 &rh->authority_zone)) )
1954 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1955 _("Hostname `%s' is not well-formed, resolution fails\n"),
1957 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
1959 GNUNET_free_non_null (x);
1960 GNUNET_free_non_null (y);
1965 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
1966 GNUNET_free (resolver_lookup_get_next_label (rh));
1968 ac = GNUNET_new (struct AuthorityChain);
1970 ac->label = resolver_lookup_get_next_label (rh);
1971 if (NULL == ac->label)
1972 /* name was just "gnu", so we default to label '+' */
1973 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
1974 ac->gns_authority = GNUNET_YES;
1975 ac->authority_info.gns_authority = rh->authority_zone;
1976 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1979 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1985 * Lookup of a record in a specific zone calls lookup result processor
1988 * @param zone the zone to perform the lookup in
1989 * @param record_type the record type to look up
1990 * @param name the name to look up
1991 * @param shorten_key a private key for use with PSEU import (can be NULL)
1992 * @param only_cached #GNUNET_NO to only check locally not DHT for performance
1993 * @param proc the processor to call on result
1994 * @param proc_cls the closure to pass to @a proc
1995 * @return handle to cancel operation
1997 struct GNS_ResolverHandle *
1998 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
1999 uint32_t record_type,
2001 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key,
2003 GNS_ResultProcessor proc, void *proc_cls)
2005 struct GNS_ResolverHandle *rh;
2007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2008 (NULL == shorten_key)
2009 ? "Starting lookup for `%s' with shortening disabled\n"
2010 : "Starting lookup for `%s' with shortening enabled\n",
2012 rh = GNUNET_new (struct GNS_ResolverHandle);
2013 GNUNET_CONTAINER_DLL_insert (rlh_head,
2016 rh->authority_zone = *zone;
2018 rh->proc_cls = proc_cls;
2019 rh->only_cached = only_cached;
2020 rh->record_type = record_type;
2021 rh->name = GNUNET_strdup (name);
2022 rh->name_resolution_pos = strlen (name);
2023 if (NULL != shorten_key)
2025 rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
2026 *rh->shorten_key = *shorten_key;
2028 start_resolver_lookup (rh);
2034 * Cancel active resolution (i.e. client disconnected).
2036 * @param rh resolution to abort
2039 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
2041 struct DnsResult *dr;
2042 struct AuthorityChain *ac;
2043 struct VpnContext *vpn_ctx;
2045 GNUNET_CONTAINER_DLL_remove (rlh_head,
2048 while (NULL != (ac = rh->ac_head))
2050 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
2053 GNUNET_free (ac->label);
2056 if (GNUNET_SCHEDULER_NO_TASK != rh->task_id)
2058 GNUNET_SCHEDULER_cancel (rh->task_id);
2059 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
2061 if (NULL != rh->get_handle)
2063 GNUNET_DHT_get_stop (rh->get_handle);
2064 rh->get_handle = NULL;
2066 if (NULL != rh->dht_heap_node)
2068 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
2069 rh->dht_heap_node = NULL;
2071 if (NULL != (vpn_ctx = rh->vpn_ctx))
2073 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
2074 GNUNET_free (vpn_ctx->rd_data);
2075 GNUNET_free (vpn_ctx);
2077 if (NULL != rh->dns_request)
2079 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
2080 rh->dns_request = NULL;
2082 if (NULL != rh->namestore_qe)
2084 GNUNET_NAMESTORE_cancel (rh->namestore_qe);
2085 rh->namestore_qe = NULL;
2087 if (NULL != rh->std_resolve)
2089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2090 "Canceling standard DNS resolution\n");
2091 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
2092 rh->std_resolve = NULL;
2094 while (NULL != (dr = rh->dns_result_head))
2096 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
2097 rh->dns_result_tail,
2101 GNUNET_free_non_null (rh->shorten_key);
2102 GNUNET_free (rh->name);
2107 /* ***************** Resolver initialization ********************* */
2111 * Initialize the resolver
2113 * @param nh the namestore handle
2114 * @param dht the dht handle
2115 * @param c configuration handle
2116 * @param max_bg_queries maximum number of parallel background queries in dht
2119 GNS_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
2120 struct GNUNET_DHT_Handle *dht,
2121 const struct GNUNET_CONFIGURATION_Handle *c,
2122 unsigned long long max_bg_queries)
2127 namestore_handle = nh;
2130 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2131 max_allowed_background_queries = max_bg_queries;
2133 GNUNET_CONFIGURATION_get_value_string (c,
2138 /* user did not specify DNS resolver, use 8.8.8.8 */
2139 dns_ip = GNUNET_strdup ("8.8.8.8");
2141 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
2142 GNUNET_free (dns_ip);
2143 vpn_handle = GNUNET_VPN_connect (cfg);
2151 GNS_resolver_done ()
2153 struct GNS_ResolverHandle *rh;
2154 struct CacheOps *co;
2156 /* abort active resolutions */
2157 while (NULL != (rh = rlh_head))
2159 rh->proc (rh->proc_cls, 0, NULL);
2160 GNS_resolver_lookup_cancel (rh);
2162 while (NULL != (co = co_head))
2164 GNUNET_CONTAINER_DLL_remove (co_head,
2167 GNUNET_NAMESTORE_cancel (co->namestore_qe_cache);
2170 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
2171 dht_lookup_heap = NULL;
2172 GNUNET_DNSSTUB_stop (dns_handle);
2174 GNUNET_VPN_disconnect (vpn_handle);
2177 namestore_handle = NULL;
2181 /* *************** common helper functions (do not really belong here) *********** */
2184 * Checks if @a name ends in ".TLD"
2186 * @param name the name to check
2187 * @param tld the TLD to check for
2188 * @return GNUNET_YES or GNUNET_NO
2191 is_tld (const char* name, const char* tld)
2195 if (strlen (name) <= strlen (tld))
2197 offset = strlen (name) - strlen (tld);
2198 if (0 != strcmp (name + offset, tld))
2204 /* end of gnunet-service-gns_resolver.c */