2 This file is part of GNUnet.
3 (C) 2011-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file gns/gnunet-service-gns_resolver.c
23 * @brief GNU Name System resolver logic
24 * @author Martin Schanzenbach
25 * @author Christian Grothoff
28 * - GNS: handle special SRV names --- no delegation, direct lookup;
29 * can likely be done in 'resolver_lookup_get_next_label'. (#3003)
30 * - revocation checks (use CORE-level broadcasts!), (#3004)
31 * - DNAME support (#3005)
34 #include "gnunet_util_lib.h"
35 #include "gnunet_dnsstub_lib.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_namestore_service.h"
38 #include "gnunet_dns_service.h"
39 #include "gnunet_resolver_service.h"
40 #include "gnunet_dnsparser_lib.h"
41 #include "gnunet_gns_service.h"
43 #include "gnunet-service-gns_resolver.h"
44 #include "gnunet-service-gns_shorten.h"
45 #include "gnunet_vpn_service.h"
49 * Default DHT timeout for lookups.
51 #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
54 * Default timeout for DNS lookups.
56 #define DNS_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
59 * Default timeout for VPN redirections.
61 #define VPN_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
64 * DHT replication level
66 #define DHT_GNS_REPLICATION_LEVEL 5
69 * How deep do we allow recursions to go before we abort?
71 #define MAX_RECURSION 256
75 * DLL to hold the authority chain we had to pass in the resolution
83 struct AuthorityChain *prev;
88 struct AuthorityChain *next;
91 * Resolver handle this entry in the chain belongs to.
93 struct GNS_ResolverHandle *rh;
96 * label/name corresponding to the authority
101 * #GNUNET_YES if the authority was a GNS authority,
102 * #GNUNET_NO if the authority was a DNS authority.
107 * Information about the resolver authority for this label.
113 * The zone of the GNS authority
115 struct GNUNET_CRYPTO_EccPublicSignKey gns_authority;
120 * Domain of the DNS resolver that is the authority.
121 * (appended to construct the DNS name to resolve;
122 * this is NOT the DNS name of the DNS server!).
124 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
127 * IP address of the DNS resolver that is authoritative.
128 * (this implementation currently only supports one
131 struct sockaddr_storage dns_ip;
141 * A result we got from DNS.
149 struct DnsResult *next;
154 struct DnsResult *prev;
157 * Binary value stored in the DNS record (appended to this struct)
162 * Expiration time for the DNS record, 0 if we didn't
163 * get anything useful (i.e. 'gethostbyname' was used).
165 uint64_t expiration_time;
168 * Number of bytes in 'data'.
173 * Type of the GNS/DNS record.
175 uint32_t record_type;
181 * Closure for #vpn_allocation_cb.
187 * Which resolution process are we processing.
189 struct GNS_ResolverHandle *rh;
192 * Handle to the VPN request that we were performing.
194 struct GNUNET_VPN_RedirectionRequest *vpn_request;
197 * Number of records serialized in 'rd_data'.
199 unsigned int rd_count;
202 * Serialized records.
207 * Number of bytes in 'rd_data'.
214 * Handle to a currenty pending resolution. On result (positive or
215 * negative) the #GNS_ResultProcessor is called.
217 struct GNS_ResolverHandle
223 struct GNS_ResolverHandle *next;
228 struct GNS_ResolverHandle *prev;
231 * The top-level GNS authoritative zone to query
233 struct GNUNET_CRYPTO_EccPublicSignKey authority_zone;
236 * called when resolution phase finishes
238 GNS_ResultProcessor proc;
241 * closure passed to proc
246 * Handle for DHT lookups. should be NULL if no lookups are in progress
248 struct GNUNET_DHT_GetHandle *get_handle;
251 * Handle to a VPN request, NULL if none is active.
253 struct VpnContext *vpn_ctx;
256 * Socket for a DNS request, NULL if none is active.
258 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
261 * Handle for standard DNS resolution, NULL if none is active.
263 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
266 * Pending Namestore task
268 struct GNUNET_NAMESTORE_QueueEntry *namestore_qe;
271 * Heap node associated with this lookup. Used to limit number of
272 * concurrent requests.
274 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
277 * DLL to store the authority chain
279 struct AuthorityChain *ac_head;
282 * DLL to store the authority chain
284 struct AuthorityChain *ac_tail;
287 * Private key of the shorten zone, NULL to not shorten.
289 struct GNUNET_CRYPTO_EccPrivateKey *shorten_key;
292 * ID of a task associated with the resolution process.
294 GNUNET_SCHEDULER_TaskIdentifier task_id;
297 * The name to resolve
302 * DLL of results we got from DNS.
304 struct DnsResult *dns_result_head;
307 * DLL of results we got from DNS.
309 struct DnsResult *dns_result_tail;
312 * Current offset in 'name' where we are resolving.
314 size_t name_resolution_pos;
322 * Desired type for the resolution.
327 * We increment the loop limiter for each step in a recursive
328 * resolution. If it passes our threshold (i.e. due to
329 * self-recursion in the resolution, i.e CNAME fun), we stop.
331 unsigned int loop_limiter;
337 * Our handle to the namestore service
339 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
342 * Our handle to the vpn service
344 static struct GNUNET_VPN_Handle *vpn_handle;
347 * Resolver handle to the dht
349 static struct GNUNET_DHT_Handle *dht_handle;
352 * Handle to perform DNS lookups.
354 static struct GNUNET_DNSSTUB_Context *dns_handle;
357 * Heap for limiting parallel DHT lookups
359 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
362 * Maximum amount of parallel queries to the DHT
364 static unsigned long long max_allowed_background_queries;
367 * Head of resolver lookup list
369 static struct GNS_ResolverHandle *rlh_head;
372 * Tail of resolver lookup list
374 static struct GNS_ResolverHandle *rlh_tail;
377 * Global configuration.
379 static const struct GNUNET_CONFIGURATION_Handle *cfg;
383 * Check if name is in srv format (_x._y.xxx)
386 * @return #GNUNET_YES if true
389 is_srv (const char *name)
396 if (NULL == strstr (name, "._"))
399 ndup = GNUNET_strdup (name);
401 if (NULL == strtok (NULL, "."))
403 if (NULL == strtok (NULL, "."))
405 if (NULL != strtok (NULL, "."))
413 * Determine if this name is canonical (is a legal name in a zone, without delegation);
414 * note that we do not test that the name does not contain illegal characters, we only
415 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
416 * even though they consist of multiple labels.
419 * a.b.gnu = not canonical
421 * _foo._srv = canonical
422 * _f.bar = not canonical
424 * @param name the name to test
425 * @return #GNUNET_YES if canonical
428 is_canonical (const char *name)
433 if (NULL == strchr (name, '.'))
438 while (NULL != (dot = strchr (pos, '.')))
446 /* ************************** Resolution **************************** */
449 * Exands a name ending in .+ with the zone of origin.
451 * @param rh resolution context
452 * @param name name to modify (to be free'd or returned)
453 * @return updated name
456 translate_dot_plus (struct GNS_ResolverHandle *rh,
460 size_t s_len = strlen (name);
462 if (0 != strcmp (&name[s_len - 2],
464 return name; /* did not end in ".+" */
465 GNUNET_assert (GNUNET_YES == rh->ac_tail->gns_authority);
466 GNUNET_asprintf (&ret,
470 GNUNET_NAMESTORE_pkey_to_zkey (&rh->ac_tail->authority_info.gns_authority));
477 * Task scheduled to asynchronously fail a resolution.
479 * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
480 * @param tc task context
483 fail_resolution (void *cls,
484 const struct GNUNET_SCHEDULER_TaskContext *tc)
486 struct GNS_ResolverHandle *rh = cls;
488 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
489 rh->proc (rh->proc_cls, 0, NULL);
490 GNS_resolver_lookup_cancel (rh);
494 #if (defined WINDOWS) || (defined DARWIN)
495 /* Don't have this on W32, here's a naive implementation
496 * Was somehow removed on OS X ... */
498 memrchr (const void *s, int c, size_t n)
501 unsigned char *ucs = (unsigned char *) s;
503 for (i = n - 1; i >= 0; i--)
505 return (void *) &ucs[i];
512 * Get the next, rightmost label from the name that we are trying to resolve,
513 * and update the resolution position accordingly.
515 * @param rh handle to the resolution operation to get the next label from
516 * @return NULL if there are no more labels
519 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
525 if (0 == rh->name_resolution_pos)
527 dot = memrchr (rh->name, (int) '.', rh->name_resolution_pos);
530 /* done, this was the last one */
531 len = rh->name_resolution_pos;
533 rh->name_resolution_pos = 0;
537 /* advance by one label */
538 len = rh->name_resolution_pos - (dot - rh->name) - 1;
540 rh->name_resolution_pos = dot - rh->name;
542 return GNUNET_strndup (rp, len);
547 * Gives the cummulative result obtained to the callback and clean up the request.
549 * @param rh resolution process that has culminated in a result
552 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
554 struct DnsResult *pos;
559 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
562 struct GNUNET_NAMESTORE_RecordData rd[n];
565 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
567 rd[i].data = pos->data;
568 rd[i].data_size = pos->data_size;
569 rd[i].record_type = pos->record_type;
570 if (0 == pos->expiration_time)
572 rd[i].flags = GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
573 rd[i].expiration_time = 0;
577 rd[i].flags = GNUNET_NAMESTORE_RF_NONE;
578 rd[i].expiration_time = pos->expiration_time;
582 GNUNET_assert (i == n);
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
584 "Transmitting standard DNS result with %u records\n",
586 rh->proc (rh->proc_cls,
590 GNS_resolver_lookup_cancel (rh);
595 * Add a result from DNS to the records to be returned to the application.
597 * @param rh resolution request to extend with a result
598 * @param expiration_time expiration time for the answer
599 * @param record_type DNS record type of the answer
600 * @param data_size number of bytes in @a data
601 * @param data binary data to return in DNS record
604 add_dns_result (struct GNS_ResolverHandle *rh,
605 uint64_t expiration_time,
606 uint32_t record_type,
610 struct DnsResult *res;
612 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
613 res->expiration_time = expiration_time;
614 res->data_size = data_size;
615 res->record_type = record_type;
617 memcpy (&res[1], data, data_size);
618 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
625 * We had to do a DNS lookup. Convert the result (if any) and return
628 * @param cls closure with the 'struct GNS_ResolverHandle'
629 * @param addr one of the addresses of the host, NULL for the last address
630 * @param addrlen length of the address
633 handle_dns_result (void *cls,
634 const struct sockaddr *addr,
637 struct GNS_ResolverHandle *rh = cls;
638 const struct sockaddr_in *sa4;
639 const struct sockaddr_in6 *sa6;
641 rh->std_resolve = NULL;
644 transmit_lookup_dns_result (rh);
647 switch (addr->sa_family)
650 sa4 = (const struct sockaddr_in *) addr;
652 0 /* expiration time is unknown */,
653 GNUNET_DNSPARSER_TYPE_A,
654 sizeof (struct in_addr),
658 sa6 = (const struct sockaddr_in6 *) addr;
660 0 /* expiration time is unknown */,
661 GNUNET_DNSPARSER_TYPE_AAAA,
662 sizeof (struct in6_addr),
673 * Task scheduled to continue with the resolution process.
675 * @param cls the 'struct GNS_ResolverHandle' of the resolution
676 * @param tc task context
679 recursive_resolution (void *cls,
680 const struct GNUNET_SCHEDULER_TaskContext *tc);
684 * Begin the resolution process from 'name', starting with
685 * the identification of the zone specified by 'name'.
687 * @param rh resolution to perform
690 start_resolver_lookup (struct GNS_ResolverHandle *rh);
694 * Function called with the result of a DNS resolution.
696 * @param cls the request handle of the resolution that
697 * we were attempting to make
698 * @param rs socket that received the response
699 * @param dns dns response, never NULL
700 * @param dns_len number of bytes in @a dns
703 dns_result_parser (void *cls,
704 struct GNUNET_DNSSTUB_RequestSocket *rs,
705 const struct GNUNET_TUN_DnsHeader *dns,
708 struct GNS_ResolverHandle *rh = cls;
709 struct GNUNET_DNSPARSER_Packet *p;
710 const struct GNUNET_DNSPARSER_Record *rec;
711 unsigned int rd_count;
714 rh->dns_request = NULL;
715 GNUNET_SCHEDULER_cancel (rh->task_id);
716 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
717 p = GNUNET_DNSPARSER_parse ((const char *) dns,
721 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
722 _("Failed to parse DNS response\n"));
723 rh->proc (rh->proc_cls, 0, NULL);
724 GNS_resolver_lookup_cancel (rh);
728 if ( (p->num_answers > 0) &&
729 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
730 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
732 GNUNET_free (rh->name);
733 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
734 start_resolver_lookup (rh);
735 GNUNET_DNSPARSER_free_packet (p);
738 /* FIXME: add DNAME support */
740 /* convert from (parsed) DNS to (binary) GNS format! */
741 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
743 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
745 char buf[UINT16_MAX];
750 memset (rd, 0, sizeof (rd));
751 for (i=0;i<rd_count;i++)
753 if (i < p->num_answers)
754 rec = &p->answers[i];
755 else if (i < p->num_answers + p->num_authority_records)
756 rec = &p->authority_records[i - p->num_answers];
758 rec = &p->authority_records[i - p->num_answers - p->num_authority_records];
759 /* As we copied the full DNS name to 'rh->ac_tail->label', this
760 should be the correct check to see if this record is actually
761 a record for our label... */
762 if (0 != strcmp (rec->name,
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "Dropping record `%s', does not match desired name `%s'\n",
772 rd[i - skip].record_type = rec->type;
773 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
776 case GNUNET_DNSPARSER_TYPE_A:
777 if (rec->data.raw.data_len != sizeof (struct in_addr))
783 rd[i - skip].data_size = rec->data.raw.data_len;
784 rd[i - skip].data = rec->data.raw.data;
786 case GNUNET_DNSPARSER_TYPE_AAAA:
787 if (rec->data.raw.data_len != sizeof (struct in6_addr))
793 rd[i - skip].data_size = rec->data.raw.data_len;
794 rd[i - skip].data = rec->data.raw.data;
796 case GNUNET_DNSPARSER_TYPE_CNAME:
797 case GNUNET_DNSPARSER_TYPE_PTR:
798 case GNUNET_DNSPARSER_TYPE_NS:
800 GNUNET_DNSPARSER_builder_add_name (buf,
810 case GNUNET_DNSPARSER_TYPE_SOA:
812 GNUNET_DNSPARSER_builder_add_soa (buf,
822 case GNUNET_DNSPARSER_TYPE_MX:
824 GNUNET_DNSPARSER_builder_add_mx (buf,
834 case GNUNET_DNSPARSER_TYPE_SRV:
836 GNUNET_DNSPARSER_builder_add_srv (buf,
847 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
848 _("Skipping record of unsupported type %d\n"),
854 rh->proc (rh->proc_cls, rd_count - skip, rd);
855 GNS_resolver_lookup_cancel (rh);
857 GNUNET_DNSPARSER_free_packet (p);
862 * Perform recursive DNS resolution. Asks the given DNS resolver to
863 * resolve "rh->dns_name", possibly recursively proceeding following
864 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
865 * we find the answer.
867 * @param rh resolution information
870 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
872 struct AuthorityChain *ac;
874 struct GNUNET_DNSPARSER_Query *query;
875 struct GNUNET_DNSPARSER_Packet *p;
877 size_t dns_request_length;
880 GNUNET_assert (NULL != ac);
881 GNUNET_assert (GNUNET_NO == ac->gns_authority);
882 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
885 sa_len = sizeof (struct sockaddr_in);
888 sa_len = sizeof (struct sockaddr_in6);
892 rh->proc (rh->proc_cls, 0, NULL);
893 GNS_resolver_lookup_cancel (rh);
896 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
897 query->name = GNUNET_strdup (ac->label);
898 query->type = rh->record_type;
899 query->class = GNUNET_TUN_DNS_CLASS_INTERNET;
900 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
903 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
905 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
906 p->flags.recursion_desired = 1;
908 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
911 rh->proc (rh->proc_cls, 0, NULL);
912 GNS_resolver_lookup_cancel (rh);
916 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
917 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
923 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
927 GNUNET_free (dns_request);
928 GNUNET_DNSPARSER_free_packet (p);
933 * We encountered a CNAME record during our resolution.
934 * Merge it into our chain.
936 * @param rh resolution we are performing
937 * @param cname value of the cname record we got for the current
938 * authority chain tail
941 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
947 nlen = strlen (cname);
952 /* CNAME resolution continues relative to current domain */
953 if (0 == rh->name_resolution_pos)
955 res = GNUNET_strndup (cname, nlen - 2);
956 rh->name_resolution_pos = nlen - 2;
960 GNUNET_asprintf (&res,
962 (int) rh->name_resolution_pos,
966 rh->name_resolution_pos = strlen (res);
968 GNUNET_free (rh->name);
970 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
974 /* name is absolute, start from the beginning */
975 GNUNET_free (rh->name);
976 rh->name = GNUNET_strdup (cname);
977 start_resolver_lookup (rh);
982 * Process a records that were decrypted from a block.
984 * @param cls closure with the 'struct GNS_ResolverHandle'
985 * @param rd_count number of entries in @a rd array
986 * @param rd array of records with data to store
989 handle_gns_resolution_result (void *cls,
990 unsigned int rd_count,
991 const struct GNUNET_NAMESTORE_RecordData *rd);
995 * Callback invoked from the VPN service once a redirection is
996 * available. Provides the IP address that can now be used to
997 * reach the requested destination. Replaces the "VPN" record
998 * with the respective A/AAAA record and continues processing.
1000 * @param cls closure
1001 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1002 * will match 'result_af' from the request
1003 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1004 * that the VPN allocated for the redirection;
1005 * traffic to this IP will now be redirected to the
1006 * specified target peer; NULL on error
1009 vpn_allocation_cb (void *cls,
1011 const void *address)
1013 struct VpnContext *vpn_ctx = cls;
1014 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1015 struct GNUNET_NAMESTORE_RecordData rd[vpn_ctx->rd_count];
1018 vpn_ctx->vpn_request = NULL;
1020 GNUNET_assert (GNUNET_OK ==
1021 GNUNET_NAMESTORE_records_deserialize (vpn_ctx->rd_data_size,
1025 for (i=0;i<vpn_ctx->rd_count;i++)
1027 if (GNUNET_NAMESTORE_TYPE_VPN == rd[i].record_type)
1032 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1033 rd[i].data_size = sizeof (struct in_addr);
1034 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1036 rd[i].data = address;
1039 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1040 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1042 rd[i].data = address;
1043 rd[i].data_size = sizeof (struct in6_addr);
1051 GNUNET_assert (i < vpn_ctx->rd_count);
1052 handle_gns_resolution_result (rh,
1055 GNUNET_free (vpn_ctx->rd_data);
1056 GNUNET_free (vpn_ctx);
1061 * Process a records that were decrypted from a block.
1063 * @param cls closure with the 'struct GNS_ResolverHandle'
1064 * @param rd_count number of entries in @a rd array
1065 * @param rd array of records with data to store
1068 handle_gns_resolution_result (void *cls,
1069 unsigned int rd_count,
1070 const struct GNUNET_NAMESTORE_RecordData *rd)
1072 struct GNS_ResolverHandle *rh = cls;
1073 struct AuthorityChain *ac;
1076 struct sockaddr *sa;
1077 struct sockaddr_in v4;
1078 struct sockaddr_in6 v6;
1081 struct VpnContext *vpn_ctx;
1082 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1084 struct GNUNET_HashCode vhash;
1086 char scratch[UINT16_MAX];
1088 size_t scratch_start;
1090 struct GNUNET_NAMESTORE_RecordData rd_new[rd_count];
1091 unsigned int rd_off;
1093 if (0 == rh->name_resolution_pos)
1095 /* top-level match, are we done yet? */
1096 if ( (rd_count > 0) &&
1097 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1098 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1100 cname = GNUNET_strndup (rd[0].data,
1102 handle_gns_cname_result (rh,
1104 GNUNET_free (cname);
1107 /* If A/AAAA was requested, but we got a VPN
1108 record, we convert it to A/AAAA using GNUnet VPN */
1109 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1110 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1112 for (i=0;i<rd_count;i++)
1114 if (GNUNET_NAMESTORE_TYPE_VPN == rd[i].record_type)
1116 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1117 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) <
1120 GNUNET_break_op (0);
1121 rh->proc (rh->proc_cls, 0, NULL);
1122 GNS_resolver_lookup_cancel (rh);
1125 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1126 vname = (const char *) &vpn[1];
1127 if ('\0' != vname[rd[i].data_size - 1 - sizeof (struct GNUNET_TUN_GnsVpnRecord)])
1129 GNUNET_break_op (0);
1130 rh->proc (rh->proc_cls, 0, NULL);
1131 GNS_resolver_lookup_cancel (rh);
1134 GNUNET_CRYPTO_hash (vname,
1135 strlen (vname), // FIXME: +1?
1137 vpn_ctx = GNUNET_new (struct VpnContext);
1138 rh->vpn_ctx = vpn_ctx;
1140 vpn_ctx->rd_data_size = GNUNET_NAMESTORE_records_get_size (rd_count,
1142 vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
1143 (void) GNUNET_NAMESTORE_records_serialize (rd_count,
1145 vpn_ctx->rd_data_size,
1147 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1153 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1160 /* convert relative names in record values to absolute names,
1161 using 'scratch' array for memory allocations */
1164 for (i=0;i<rd_count;i++)
1166 rd_new[rd_off] = rd[i];
1167 /* Check if the embedded name(s) end in "+", and if so,
1168 replace the "+" with the zone at "ac_tail", changing the name
1169 to a ".zkey". The name is allocated on the 'scratch' array,
1170 so we can free it afterwards. */
1171 switch (rd[i].record_type)
1173 case GNUNET_DNSPARSER_TYPE_CNAME:
1178 cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
1181 if ( (NULL == cname) ||
1182 (off != rd[i].data_size) )
1184 GNUNET_break_op (0); /* record not well-formed */
1188 cname = translate_dot_plus (rh, cname);
1189 scratch_start = scratch_off;
1191 GNUNET_DNSPARSER_builder_add_name (scratch,
1200 rd_new[rd_off].data = &scratch[scratch_start];
1201 rd_new[rd_off].data_size = scratch_off - scratch_start;
1205 GNUNET_free_non_null (cname);
1208 case GNUNET_DNSPARSER_TYPE_SOA:
1210 struct GNUNET_DNSPARSER_SoaRecord *soa;
1213 soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
1216 if ( (NULL == soa) ||
1217 (off != rd[i].data_size) )
1219 GNUNET_break_op (0); /* record not well-formed */
1223 soa->mname = translate_dot_plus (rh, soa->mname);
1224 soa->rname = translate_dot_plus (rh, soa->rname);
1225 scratch_start = scratch_off;
1227 GNUNET_DNSPARSER_builder_add_soa (scratch,
1236 rd_new[rd_off].data = &scratch[scratch_start];
1237 rd_new[rd_off].data_size = scratch_off - scratch_start;
1242 GNUNET_DNSPARSER_free_soa (soa);
1245 case GNUNET_DNSPARSER_TYPE_MX:
1247 struct GNUNET_DNSPARSER_MxRecord *mx;
1250 mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
1253 if ( (NULL == mx) ||
1254 (off != rd[i].data_size) )
1256 GNUNET_break_op (0); /* record not well-formed */
1260 mx->mxhost = translate_dot_plus (rh, mx->mxhost);
1261 scratch_start = scratch_off;
1263 GNUNET_DNSPARSER_builder_add_mx (scratch,
1272 rd_new[rd_off].data = &scratch[scratch_start];
1273 rd_new[rd_off].data_size = scratch_off - scratch_start;
1278 GNUNET_DNSPARSER_free_mx (mx);
1281 case GNUNET_DNSPARSER_TYPE_SRV:
1283 struct GNUNET_DNSPARSER_SrvRecord *srv;
1286 /* FIXME: passing rh->name here is is not necessarily what we want
1287 (SRV support not finished) */
1288 srv = GNUNET_DNSPARSER_parse_srv (rh->name,
1292 if ( (NULL == srv) ||
1293 (off != rd[i].data_size) )
1295 GNUNET_break_op (0); /* record not well-formed */
1299 srv->domain_name = translate_dot_plus (rh, srv->domain_name);
1300 srv->target = translate_dot_plus (rh, srv->target);
1301 scratch_start = scratch_off;
1303 GNUNET_DNSPARSER_builder_add_srv (scratch,
1312 rd_new[rd_off].data = &scratch[scratch_start];
1313 rd_new[rd_off].data_size = scratch_off - scratch_start;
1318 GNUNET_DNSPARSER_free_srv (srv);
1321 case GNUNET_NAMESTORE_TYPE_PKEY:
1322 /* tigger shortening */
1323 if (NULL != rh->shorten_key)
1325 struct GNUNET_CRYPTO_EccPublicSignKey pub;
1327 if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EccPublicSignKey))
1329 GNUNET_break_op (0);
1332 memcpy (&pub, rd[i].data, rd[i].data_size);
1333 GNS_shorten_start (rh->ac_tail->label,
1342 /* yes, we are done, return result */
1343 rh->proc (rh->proc_cls, rd_off, rd_new);
1344 GNS_resolver_lookup_cancel (rh);
1347 /* need to recurse, check if we can */
1348 for (i=0;i<rd_count;i++)
1350 switch (rd[i].record_type)
1352 case GNUNET_NAMESTORE_TYPE_PKEY:
1353 /* delegation to another zone */
1354 if (sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) !=
1357 GNUNET_break_op (0);
1358 rh->proc (rh->proc_cls, 0, NULL);
1359 GNS_resolver_lookup_cancel (rh);
1362 /* expand authority chain */
1363 ac = GNUNET_new (struct AuthorityChain);
1365 ac->gns_authority = GNUNET_YES;
1366 memcpy (&ac->authority_info.gns_authority,
1368 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey));
1369 ac->label = resolver_lookup_get_next_label (rh);
1370 /* tigger shortening */
1371 if (NULL != rh->shorten_key)
1372 GNS_shorten_start (rh->ac_tail->label,
1373 &ac->authority_info.gns_authority,
1375 /* add AC to tail */
1376 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1380 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1383 case GNUNET_DNSPARSER_TYPE_NS:
1384 /* resolution continues within DNS */
1385 if (GNUNET_DNSPARSER_MAX_NAME_LENGTH < rd[i].data_size)
1387 GNUNET_break_op (0);
1388 rh->proc (rh->proc_cls, 0, NULL);
1389 GNS_resolver_lookup_cancel (rh);
1392 /* find associated A/AAAA record */
1395 for (j=0;j<rd_count;j++)
1397 switch (rd[j].record_type)
1399 case GNUNET_DNSPARSER_TYPE_A:
1400 if (sizeof (struct in_addr) != rd[i].data_size)
1402 GNUNET_break_op (0);
1403 rh->proc (rh->proc_cls, 0, NULL);
1404 GNS_resolver_lookup_cancel (rh);
1407 /* FIXME: might want to check if we support IPv4 here,
1408 and otherwise skip this one and hope we find another */
1409 memset (&v4, 0, sizeof (v4));
1410 sa_len = sizeof (v4);
1411 v4.sin_family = AF_INET;
1412 v4.sin_port = htons (53);
1413 #if HAVE_SOCKADDR_IN_SIN_LEN
1414 v4.sin_len = (u_char) sa_len;
1416 memcpy (&v4.sin_addr,
1418 sizeof (struct in_addr));
1419 sa = (struct sockaddr *) &v4;
1421 case GNUNET_DNSPARSER_TYPE_AAAA:
1422 if (sizeof (struct in6_addr) != rd[i].data_size)
1424 GNUNET_break_op (0);
1425 rh->proc (rh->proc_cls, 0, NULL);
1426 GNS_resolver_lookup_cancel (rh);
1429 /* FIXME: might want to check if we support IPv6 here,
1430 and otherwise skip this one and hope we find another */
1431 memset (&v6, 0, sizeof (v6));
1432 sa_len = sizeof (v6);
1433 v6.sin6_family = AF_INET6;
1434 v6.sin6_port = htons (53);
1435 #if HAVE_SOCKADDR_IN_SIN_LEN
1436 v6.sin6_len = (u_char) sa_len;
1438 memcpy (&v6.sin6_addr,
1440 sizeof (struct in6_addr));
1441 sa = (struct sockaddr *) &v6;
1451 /* we cannot continue; NS without A/AAAA */
1452 rh->proc (rh->proc_cls, 0, NULL);
1453 GNS_resolver_lookup_cancel (rh);
1456 /* expand authority chain */
1457 ac = GNUNET_new (struct AuthorityChain);
1459 strncpy (ac->authority_info.dns_authority.name,
1462 ac->authority_info.dns_authority.name[rd[i].data_size] = '\0';
1463 memcpy (&ac->authority_info.dns_authority.dns_ip,
1466 /* for DNS recursion, the label is the full DNS name,
1467 created from the remainder of the GNS name and the
1468 name in the NS record */
1469 GNUNET_asprintf (&ac->label,
1471 (int) rh->name_resolution_pos,
1473 ac->authority_info.dns_authority.name);
1474 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1477 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1480 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1482 rh->proc (rh->proc_cls, 0, NULL);
1483 GNS_resolver_lookup_cancel (rh);
1487 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1490 case GNUNET_DNSPARSER_TYPE_CNAME:
1491 cname = GNUNET_strndup (rd[i].data,
1493 handle_gns_cname_result (rh,
1495 GNUNET_free (cname);
1497 /* FIXME: handle DNAME */
1503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1504 _("GNS lookup recursion failed (no delegation record found)\n"));
1505 rh->proc (rh->proc_cls, 0, NULL);
1506 GNS_resolver_lookup_cancel (rh);
1511 * Function called once the namestore has completed the request for
1514 * @param cls closure with the 'struct GNS_ResolverHandle'
1515 * @param success #GNUNET_OK on success
1516 * @param emsg error message
1519 namestore_cache_continuation (void *cls,
1523 struct GNS_ResolverHandle *rh = cls;
1525 rh->namestore_qe = NULL;
1527 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1528 _("Failed to cache GNS resolution: %s\n"),
1534 * Iterator called on each result obtained for a DHT
1535 * operation that expects a reply
1537 * @param cls closure with the 'struct GNS_ResolverHandle'
1538 * @param exp when will this value expire
1539 * @param key key of the result
1540 * @param get_path peers on reply path (or NULL if not recorded)
1541 * [0] = datastore's first neighbor, [length - 1] = local peer
1542 * @param get_path_length number of entries in get_path
1543 * @param put_path peers on the PUT path (or NULL if not recorded)
1544 * [0] = origin, [length - 1] = datastore
1545 * @param put_path_length number of entries in get_path
1546 * @param type type of the result
1547 * @param size number of bytes in data
1548 * @param data pointer to the result data
1551 handle_dht_response (void *cls,
1552 struct GNUNET_TIME_Absolute exp,
1553 const struct GNUNET_HashCode * key,
1554 const struct GNUNET_PeerIdentity *get_path,
1555 unsigned int get_path_length,
1556 const struct GNUNET_PeerIdentity *put_path,
1557 unsigned int put_path_length,
1558 enum GNUNET_BLOCK_Type type,
1559 size_t size, const void *data)
1561 struct GNS_ResolverHandle *rh = cls;
1562 struct AuthorityChain *ac = rh->ac_tail;
1563 const struct GNUNET_NAMESTORE_Block *block;
1565 GNUNET_DHT_get_stop (rh->get_handle);
1566 rh->get_handle = NULL;
1567 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1568 rh->dht_heap_node = NULL;
1569 if (size < sizeof (struct GNUNET_NAMESTORE_Block))
1571 /* how did this pass DHT block validation!? */
1573 rh->proc (rh->proc_cls, 0, NULL);
1574 GNS_resolver_lookup_cancel (rh);
1579 ntohs (block->purpose.size) +
1580 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) +
1581 sizeof (struct GNUNET_CRYPTO_EccSignature))
1583 /* how did this pass DHT block validation!? */
1585 rh->proc (rh->proc_cls, 0, NULL);
1586 GNS_resolver_lookup_cancel (rh);
1590 GNUNET_NAMESTORE_block_decrypt (block,
1591 &ac->authority_info.gns_authority,
1593 &handle_gns_resolution_result,
1596 GNUNET_break_op (0); /* block was ill-formed */
1597 rh->proc (rh->proc_cls, 0, NULL);
1598 GNS_resolver_lookup_cancel (rh);
1601 /* Cache well-formed blocks */
1602 rh->namestore_qe = GNUNET_NAMESTORE_block_cache (namestore_handle,
1604 &namestore_cache_continuation,
1610 * Process a record that was stored in the namestore.
1612 * @param cls closure with the `struct GNS_ResolverHandle`
1613 * @param block block that was stored in the namestore
1616 handle_namestore_block_response (void *cls,
1617 const struct GNUNET_NAMESTORE_Block *block)
1619 struct GNS_ResolverHandle *rh = cls;
1620 struct GNS_ResolverHandle *rx;
1621 struct AuthorityChain *ac = rh->ac_tail;
1622 const char *label = ac->label;
1623 const struct GNUNET_CRYPTO_EccPublicSignKey *auth = &ac->authority_info.gns_authority;
1624 struct GNUNET_HashCode query;
1626 GNUNET_NAMESTORE_query_from_public_key (auth,
1629 rh->namestore_qe = NULL;
1630 if ( (GNUNET_NO == rh->only_cached) &&
1631 ( (NULL == block) ||
1632 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
1634 /* Namestore knows nothing; try DHT lookup */
1635 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1636 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1638 DHT_GNS_REPLICATION_LEVEL,
1639 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1641 &handle_dht_response, rh);
1642 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1644 GNUNET_TIME_absolute_get ().abs_value_us);
1645 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
1647 /* fail longest-standing DHT request */
1648 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
1649 GNUNET_assert (NULL != rx);
1650 rx->proc (rx->proc_cls, 0, NULL);
1651 GNS_resolver_lookup_cancel (rx);
1655 if ( (NULL == block) ||
1656 (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
1658 /* DHT not permitted and no local result, fail */
1659 rh->proc (rh->proc_cls, 0, NULL);
1660 GNS_resolver_lookup_cancel (rh);
1664 GNUNET_NAMESTORE_block_decrypt (block,
1667 &handle_gns_resolution_result,
1670 GNUNET_break_op (0); /* block was ill-formed */
1671 rh->proc (rh->proc_cls, 0, NULL);
1672 GNS_resolver_lookup_cancel (rh);
1679 * Lookup tail of our authority chain in the namestore.
1681 * @param rh query we are processing
1684 recursive_gns_resolution_namestore (struct GNS_ResolverHandle *rh)
1686 struct AuthorityChain *ac = rh->ac_tail;
1687 struct GNUNET_HashCode query;
1689 GNUNET_NAMESTORE_query_from_public_key (&ac->authority_info.gns_authority,
1692 rh->namestore_qe = GNUNET_NAMESTORE_lookup_block (namestore_handle,
1694 &handle_namestore_block_response,
1700 * Task scheduled to continue with the resolution process.
1702 * @param cls the `struct GNS_ResolverHandle` of the resolution
1703 * @param tc task context
1706 recursive_resolution (void *cls,
1707 const struct GNUNET_SCHEDULER_TaskContext *tc)
1709 struct GNS_ResolverHandle *rh = cls;
1711 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
1712 if (MAX_RECURSION < rh->loop_limiter++)
1714 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1715 "Encountered unbounded recursion resolving `%s'\n",
1717 rh->proc (rh->proc_cls, 0, NULL);
1718 GNS_resolver_lookup_cancel (rh);
1721 if (GNUNET_YES == rh->ac_tail->gns_authority)
1722 recursive_gns_resolution_namestore (rh);
1724 recursive_dns_resolution (rh);
1729 * Begin the resolution process from 'name', starting with
1730 * the identification of the zone specified by 'name'.
1732 * @param rh resolution to perform
1735 start_resolver_lookup (struct GNS_ResolverHandle *rh)
1737 struct AuthorityChain *ac;
1742 if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
1743 (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
1744 ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
1745 (GNUNET_YES != is_zkey_tld (rh->name)) ) )
1747 /* use standard DNS lookup */
1750 switch (rh->record_type)
1752 case GNUNET_DNSPARSER_TYPE_A:
1755 case GNUNET_DNSPARSER_TYPE_AAAA:
1762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1763 "Doing standard DNS lookup for `%s'\n",
1765 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
1772 if (is_zkey_tld (rh->name))
1774 /* Name ends with ".zkey", try to replace authority zone with zkey
1776 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
1777 x = resolver_lookup_get_next_label (rh); /* will return 'x' coordinate */
1778 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
1779 GNUNET_asprintf (&pkey,
1785 GNUNET_CRYPTO_ecc_public_sign_key_from_string (pkey,
1787 &rh->authority_zone)) )
1789 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1790 _("Hostname `%s' is not well-formed, resolution fails\n"),
1792 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
1794 GNUNET_free_non_null (x);
1795 GNUNET_free_non_null (y);
1800 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
1801 GNUNET_free (resolver_lookup_get_next_label (rh));
1803 ac = GNUNET_new (struct AuthorityChain);
1805 ac->label = resolver_lookup_get_next_label (rh);
1806 if (NULL == ac->label)
1807 /* name was just "gnu", so we default to label '+' */
1808 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
1809 ac->gns_authority = GNUNET_YES;
1810 ac->authority_info.gns_authority = rh->authority_zone;
1811 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1814 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1820 * Lookup of a record in a specific zone calls lookup result processor
1823 * @param zone the zone to perform the lookup in
1824 * @param record_type the record type to look up
1825 * @param name the name to look up
1826 * @param shorten_key a private key for use with PSEU import (can be NULL)
1827 * @param only_cached #GNUNET_NO to only check locally not DHT for performance
1828 * @param proc the processor to call on result
1829 * @param proc_cls the closure to pass to @a proc
1830 * @return handle to cancel operation
1832 struct GNS_ResolverHandle *
1833 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EccPublicSignKey *zone,
1834 uint32_t record_type,
1836 const struct GNUNET_CRYPTO_EccPrivateKey *shorten_key,
1838 GNS_ResultProcessor proc, void *proc_cls)
1840 struct GNS_ResolverHandle *rh;
1842 rh = GNUNET_new (struct GNS_ResolverHandle);
1843 GNUNET_CONTAINER_DLL_insert (rlh_head,
1846 rh->authority_zone = *zone;
1848 rh->proc_cls = proc_cls;
1849 rh->only_cached = only_cached;
1850 rh->record_type = record_type;
1851 rh->name = GNUNET_strdup (name);
1852 rh->name_resolution_pos = strlen (name);
1853 if (NULL != shorten_key)
1855 rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
1856 *rh->shorten_key = *shorten_key;
1858 start_resolver_lookup (rh);
1864 * Cancel active resolution (i.e. client disconnected).
1866 * @param rh resolution to abort
1869 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
1871 struct DnsResult *dr;
1872 struct AuthorityChain *ac;
1873 struct VpnContext *vpn_ctx;
1875 GNUNET_CONTAINER_DLL_remove (rlh_head,
1878 while (NULL != (ac = rh->ac_head))
1880 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
1883 GNUNET_free (ac->label);
1886 if (GNUNET_SCHEDULER_NO_TASK != rh->task_id)
1888 GNUNET_SCHEDULER_cancel (rh->task_id);
1889 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
1891 if (NULL != rh->get_handle)
1893 GNUNET_DHT_get_stop (rh->get_handle);
1894 rh->get_handle = NULL;
1896 if (NULL != rh->dht_heap_node)
1898 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1899 rh->dht_heap_node = NULL;
1901 if (NULL != (vpn_ctx = rh->vpn_ctx))
1903 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
1904 GNUNET_free (vpn_ctx->rd_data);
1905 GNUNET_free (vpn_ctx);
1907 if (NULL != rh->dns_request)
1909 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
1910 rh->dns_request = NULL;
1912 if (NULL != rh->namestore_qe)
1914 GNUNET_NAMESTORE_cancel (rh->namestore_qe);
1915 rh->namestore_qe = NULL;
1917 if (NULL != rh->std_resolve)
1919 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
1920 rh->std_resolve = NULL;
1922 while (NULL != (dr = rh->dns_result_head))
1924 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
1925 rh->dns_result_tail,
1929 GNUNET_free_non_null (rh->shorten_key);
1930 GNUNET_free (rh->name);
1935 /* ***************** Resolver initialization ********************* */
1939 * Initialize the resolver
1941 * @param nh the namestore handle
1942 * @param dht the dht handle
1943 * @param c configuration handle
1944 * @param max_bg_queries maximum number of parallel background queries in dht
1947 GNS_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
1948 struct GNUNET_DHT_Handle *dht,
1949 const struct GNUNET_CONFIGURATION_Handle *c,
1950 unsigned long long max_bg_queries)
1955 namestore_handle = nh;
1958 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1959 max_allowed_background_queries = max_bg_queries;
1961 GNUNET_CONFIGURATION_get_value_string (c,
1966 /* user did not specify DNS resolver, use 8.8.8.8 */
1967 dns_ip = GNUNET_strdup ("8.8.8.8");
1969 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
1970 GNUNET_free (dns_ip);
1971 vpn_handle = GNUNET_VPN_connect (cfg);
1979 GNS_resolver_done ()
1981 struct GNS_ResolverHandle *rh;
1983 /* abort active resolutions */
1984 while (NULL != (rh = rlh_head))
1986 rh->proc (rh->proc_cls, 0, NULL);
1987 GNS_resolver_lookup_cancel (rh);
1989 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
1990 dht_lookup_heap = NULL;
1991 GNUNET_DNSSTUB_stop (dns_handle);
1993 GNUNET_VPN_disconnect (vpn_handle);
1996 namestore_handle = NULL;
2000 /* *************** common helper functions (do not really belong here) *********** */
2003 * Checks if @a name ends in ".TLD"
2005 * @param name the name to check
2006 * @param tld the TLD to check for
2007 * @return GNUNET_YES or GNUNET_NO
2010 is_tld (const char* name, const char* tld)
2014 if (strlen (name) <= strlen (tld))
2016 offset = strlen (name) - strlen (tld);
2017 if (0 != strcmp (name + offset, tld))
2023 /* end of gnunet-service-gns_resolver.c */