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 GNUnet GNS 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'.
30 * - GNS: expand ".+" in returned values to the respective absolute
32 * - DNS: convert additional record types to GNS
33 * - shortening triggers
34 * - revocation checks (make optional: privacy!)
38 * - We currently go to the DHT simply if we find no local reply; this
39 * is incorrect; the correct rules for going to DHT are:
41 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
42 * 2. No entry in the NS existed AND
43 * 3. The zone queried is not the local resolver's zone AND
44 * 4. The name that was looked up is '+'
45 * because if it was any other canonical name we either already queried
46 * the DHT for the authority in the authority lookup phase (and thus
47 * would already have an entry in the NS for the record)
48 * 5. We are not in cache only mode
50 * - We currently never look "into" the records; for example,
51 * MX, SRV and SOA records may include ".+" that need to be
56 #include "gnunet_util_lib.h"
57 #include "gnunet_dnsstub_lib.h"
58 #include "gnunet_dht_service.h"
59 #include "gnunet_namestore_service.h"
60 #include "gnunet_dns_service.h"
61 #include "gnunet_resolver_service.h"
62 #include "gnunet_dnsparser_lib.h"
63 #include "gnunet_gns_service.h"
65 #include "gnunet-service-gns_resolver.h"
66 #include "gnunet-service-gns_shorten.h"
67 #include "gnunet_vpn_service.h"
71 * Default DHT timeout for lookups.
73 #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
76 * Default timeout for DNS lookups.
78 #define DNS_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
81 * Default timeout for VPN redirections.
83 #define VPN_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
86 * DHT replication level
88 #define DHT_GNS_REPLICATION_LEVEL 5
91 * How deep do we allow recursions to go before we abort?
93 #define MAX_RECURSION 256
97 * DLL to hold the authority chain we had to pass in the resolution
100 struct AuthorityChain
105 struct AuthorityChain *prev;
110 struct AuthorityChain *next;
113 * Resolver handle this entry in the chain belongs to.
115 struct GNS_ResolverHandle *rh;
118 * label/name corresponding to the authority
123 * #GNUNET_YES if the authority was a GNS authority,
124 * #GNUNET_NO if the authority was a DNS authority.
129 * Information about the resolver authority for this label.
135 * The zone of the GNS authority
137 struct GNUNET_CRYPTO_EccPublicKey gns_authority;
142 * Domain of the DNS resolver that is the authority.
143 * (appended to construct the DNS name to resolve;
144 * this is NOT the DNS name of the DNS server!).
146 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
149 * IP address of the DNS resolver that is authoritative.
150 * (this implementation currently only supports one
153 struct sockaddr_storage dns_ip;
163 * A result we got from DNS.
171 struct DnsResult *next;
176 struct DnsResult *prev;
179 * Binary value stored in the DNS record (appended to this struct)
184 * Expiration time for the DNS record, 0 if we didn't
185 * get anything useful (i.e. 'gethostbyname' was used).
187 uint64_t expiration_time;
190 * Number of bytes in 'data'.
195 * Type of the GNS/DNS record.
197 uint32_t record_type;
203 * Closure for #vpn_allocation_cb.
209 * Which resolution process are we processing.
211 struct GNS_ResolverHandle *rh;
214 * Handle to the VPN request that we were performing.
216 struct GNUNET_VPN_RedirectionRequest *vpn_request;
219 * Number of records serialized in 'rd_data'.
221 unsigned int rd_count;
224 * Serialized records.
229 * Number of bytes in 'rd_data'.
236 * Handle to a currenty pending resolution. On result (positive or
237 * negative) the #GNS_ResultProcessor is called.
239 struct GNS_ResolverHandle
245 struct GNS_ResolverHandle *next;
250 struct GNS_ResolverHandle *prev;
253 * The top-level GNS authoritative zone to query
255 struct GNUNET_CRYPTO_EccPublicKey authority_zone;
258 * called when resolution phase finishes
260 GNS_ResultProcessor proc;
263 * closure passed to proc
268 * Handle for DHT lookups. should be NULL if no lookups are in progress
270 struct GNUNET_DHT_GetHandle *get_handle;
273 * Handle to a VPN request, NULL if none is active.
275 struct VpnContext *vpn_ctx;
278 * Socket for a DNS request, NULL if none is active.
280 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
283 * Handle for standard DNS resolution, NULL if none is active.
285 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
288 * Pending Namestore task
290 struct GNUNET_NAMESTORE_QueueEntry *namestore_qe;
293 * Heap node associated with this lookup. Used to limit number of
294 * concurrent requests.
296 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
299 * DLL to store the authority chain
301 struct AuthorityChain *ac_head;
304 * DLL to store the authority chain
306 struct AuthorityChain *ac_tail;
309 * Private key of the shorten zone, NULL to not shorten.
311 struct GNUNET_CRYPTO_EccPrivateKey *shorten_key;
314 * ID of a task associated with the resolution process.
316 GNUNET_SCHEDULER_TaskIdentifier task_id;
319 * The name to resolve
324 * DLL of results we got from DNS.
326 struct DnsResult *dns_result_head;
329 * DLL of results we got from DNS.
331 struct DnsResult *dns_result_tail;
334 * Current offset in 'name' where we are resolving.
336 size_t name_resolution_pos;
344 * Desired type for the resolution.
349 * We increment the loop limiter for each step in a recursive
350 * resolution. If it passes our threshold (i.e. due to
351 * self-recursion in the resolution, i.e CNAME fun), we stop.
353 unsigned int loop_limiter;
359 * Our handle to the namestore service
361 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
364 * Our handle to the vpn service
366 static struct GNUNET_VPN_Handle *vpn_handle;
369 * Resolver handle to the dht
371 static struct GNUNET_DHT_Handle *dht_handle;
374 * Handle to perform DNS lookups.
376 static struct GNUNET_DNSSTUB_Context *dns_handle;
379 * Heap for limiting parallel DHT lookups
381 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
384 * Maximum amount of parallel queries to the DHT
386 static unsigned long long max_allowed_background_queries;
389 * Head of resolver lookup list
391 static struct GNS_ResolverHandle *rlh_head;
394 * Tail of resolver lookup list
396 static struct GNS_ResolverHandle *rlh_tail;
399 * Global configuration.
401 static const struct GNUNET_CONFIGURATION_Handle *cfg;
405 * Check if name is in srv format (_x._y.xxx)
408 * @return GNUNET_YES if true
411 is_srv (const char *name)
418 if (NULL == strstr (name, "._"))
421 ndup = GNUNET_strdup (name);
423 if (NULL == strtok (NULL, "."))
425 if (NULL == strtok (NULL, "."))
427 if (NULL != strtok (NULL, "."))
435 * Determine if this name is canonical (is a legal name in a zone, without delegation);
436 * note that we do not test that the name does not contain illegal characters, we only
437 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
438 * even though they consist of multiple labels.
441 * a.b.gnu = not canonical
443 * _foo._srv = canonical
444 * _f.bar = not canonical
446 * @param name the name to test
447 * @return GNUNET_YES if canonical
450 is_canonical (const char *name)
455 if (NULL == strchr (name, '.'))
460 while (NULL != (dot = strchr (pos, '.')))
470 /* ************************** Resolution **************************** */
475 //FIXME maybe define somewhere else?
476 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
477 +(GNUNET_DNSPARSER_MAX_NAME_LENGTH*2)
478 #define MAX_MX_LENGTH sizeof(uint16_t)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
479 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
483 * Exands a name ending in .+ with the zone of origin.
484 * FIXME: funky api: 'dest' must be large enough to hold
485 * the result; this is a bit yucky...
487 * @param dest destination buffer
488 * @param src the .+ name
489 * @param repl the string to replace the + with
492 expand_plus (char* dest,
497 size_t s_len = strlen (src) + 1;
499 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
502 /* no postprocessing */
503 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
504 "GNS_POSTPROCESS: %s too short\n", src);
505 memcpy (dest, src, s_len);
508 if (0 == strcmp (src + s_len - 3, ".+"))
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "GNS_POSTPROCESS: Expanding .+ in %s\n",
513 memset (dest, 0, s_len + strlen (repl) + strlen(GNUNET_GNS_TLD));
515 pos = dest + s_len - 2;
517 pos += strlen (repl);
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "GNS_POSTPROCESS: Expanded to %s\n",
524 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
525 "GNS_POSTPROCESS: No postprocessing for %s\n", src);
526 memcpy (dest, src, s_len);
535 finish_lookup (struct ResolverHandle *rh,
536 struct RecordLookupHandle* rlh,
537 unsigned int rd_count,
538 const struct GNUNET_NAMESTORE_RecordData *rd)
541 char new_rr_data[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
542 char new_mx_data[MAX_MX_LENGTH];
543 char new_soa_data[MAX_SOA_LENGTH];
544 char new_srv_data[MAX_SRV_LENGTH];
545 struct GNUNET_TUN_DnsSrvRecord *old_srv;
546 struct GNUNET_TUN_DnsSrvRecord *new_srv;
547 struct GNUNET_TUN_DnsSoaRecord *old_soa;
548 struct GNUNET_TUN_DnsSoaRecord *new_soa;
549 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
554 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
556 GNUNET_SCHEDULER_cancel(rh->timeout_task);
557 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
560 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
563 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
565 for (i = 0; i < rd_count; i++)
568 if ((GNUNET_DNSPARSER_TYPE_NS != rd[i].record_type) &&
569 (GNUNET_DNSPARSER_TYPE_PTR != rd[i].record_type) &&
570 (GNUNET_DNSPARSER_TYPE_CNAME != rd[i].record_type) &&
571 (GNUNET_DNSPARSER_TYPE_MX != rd[i].record_type) &&
572 (GNUNET_DNSPARSER_TYPE_SOA != rd[i].record_type) &&
573 (GNUNET_DNSPARSER_TYPE_SRV != rd[i].record_type))
575 p_rd[i].data = rd[i].data;
580 * for all those records we 'should'
581 * also try to resolve the A/AAAA records (RFC1035)
582 * This is a feature and not important
585 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
586 "GNS_POSTPROCESS: Postprocessing\n");
587 if (0 == strcmp(rh->name, GNUNET_GNS_MASTERZONE_STR))
588 repl_string = rlh->name;
590 repl_string = rlh->name+strlen(rh->name)+1;
593 if (GNUNET_DNSPARSER_TYPE_MX == rd[i].record_type)
595 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
596 offset = sizeof (uint16_t);
597 pos = new_mx_data + offset;
598 // FIXME: how do we know that 'pos' has enough space for the new name?
599 expand_plus (pos, (char*)rd[i].data+sizeof(uint16_t),
601 offset += strlen(new_mx_data+sizeof(uint16_t)) + 1;
602 p_rd[i].data = new_mx_data;
603 p_rd[i].data_size = offset;
605 else if (GNUNET_DNSPARSER_TYPE_SRV == rd[i].record_type)
608 * Prio, weight and port
610 new_srv = (struct GNUNET_TUN_DnsSrvRecord*)new_srv_data;
611 old_srv = (struct GNUNET_TUN_DnsSrvRecord*)rd[i].data;
612 new_srv->prio = old_srv->prio;
613 new_srv->weight = old_srv->weight;
614 new_srv->port = old_srv->port;
615 // FIXME: how do we know that '&new_srv[1]' has enough space for the new name?
616 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
618 p_rd[i].data = new_srv_data;
619 p_rd[i].data_size = sizeof (struct GNUNET_TUN_DnsSrvRecord) + strlen ((char*)&new_srv[1]) + 1;
621 else if (GNUNET_DNSPARSER_TYPE_SOA == rd[i].record_type)
623 /* expand mname and rname */
624 old_soa = (struct GNUNET_TUN_DnsSoaRecord*)rd[i].data;
625 new_soa = (struct GNUNET_TUN_DnsSoaRecord*)new_soa_data;
626 memcpy (new_soa, old_soa, sizeof (struct GNUNET_TUN_DnsSoaRecord));
627 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
628 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
629 offset = strlen ((char*)&new_soa[1]) + 1;
630 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
631 expand_plus((char*)&new_soa[1] + offset,
632 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
634 p_rd[i].data_size = sizeof (struct GNUNET_TUN_DnsSoaRecord)
636 + strlen ((char*)&new_soa[1] + offset);
637 p_rd[i].data = new_soa_data;
642 // FIXME: how do we know that 'rd[i].data' has enough space for the new name?
643 expand_plus(pos, (char*)rd[i].data, repl_string);
644 p_rd[i].data_size = strlen(new_rr_data)+1;
645 p_rd[i].data = new_rr_data;
650 rlh->proc(rlh->proc_cls, rd_count, p_rd);
652 free_resolver_handle (rh);
658 ///////////////////////////////////////////////////////////////////////////////////////////////////
659 ///////////////////////////////////////////////////////////////////////////////////////////////////
660 ///////////////////////////////////////////////////////////////////////////////////////////////////
661 ///////////////////////////////////////////////////////////////////////////////////////////////////
662 ///////////////////////////////////////////////////////////////////////////////////////////////////
666 * Task scheduled to asynchronously fail a resolution.
668 * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
669 * @param tc task context
672 fail_resolution (void *cls,
673 const struct GNUNET_SCHEDULER_TaskContext *tc)
675 struct GNS_ResolverHandle *rh = cls;
677 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
678 rh->proc (rh->proc_cls, 0, NULL);
679 GNS_resolver_lookup_cancel (rh);
682 #if (defined WINDOWS) || (defined DARWIN)
683 /* Don't have this on W32, here's a naive implementation
684 * Was somehow removed on OS X ... */
686 memrchr (const void *s, int c, size_t n)
689 unsigned char *ucs = (unsigned char *) s;
691 for (i = n - 1; i >= 0; i--)
693 return (void *) &ucs[i];
699 * Get the next, rightmost label from the name that we are trying to resolve,
700 * and update the resolution position accordingly.
702 * @param rh handle to the resolution operation to get the next label from
703 * @return NULL if there are no more labels
706 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
712 if (0 == rh->name_resolution_pos)
714 dot = memrchr (rh->name, (int) '.', rh->name_resolution_pos);
717 /* done, this was the last one */
718 len = rh->name_resolution_pos;
720 rh->name_resolution_pos = 0;
724 /* advance by one label */
725 len = rh->name_resolution_pos - (dot - rh->name) - 1;
727 rh->name_resolution_pos = dot - rh->name;
729 return GNUNET_strndup (rp, len);
734 * Gives the cummulative result obtained to the callback and clean up the request.
736 * @param rh resolution process that has culminated in a result
739 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
741 struct DnsResult *pos;
746 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
749 struct GNUNET_NAMESTORE_RecordData rd[n];
752 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
754 rd[i].data = pos->data;
755 rd[i].data_size = pos->data_size;
756 rd[i].record_type = pos->record_type;
757 if (0 == pos->expiration_time)
759 rd[i].flags = GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
760 rd[i].expiration_time = 0;
764 rd[i].flags = GNUNET_NAMESTORE_RF_NONE;
765 rd[i].expiration_time = pos->expiration_time;
768 rh->proc (rh->proc_cls,
772 GNS_resolver_lookup_cancel (rh);
777 * Add a result from DNS to the records to be returned to the application.
779 * @param rh resolution request to extend with a result
780 * @param expiration_time expiration time for the answer
781 * @param record_type DNS record type of the answer
782 * @param data_size number of bytes in @a data
783 * @param data binary data to return in DNS record
786 add_dns_result (struct GNS_ResolverHandle *rh,
787 uint64_t expiration_time,
788 uint32_t record_type,
792 struct DnsResult *res;
794 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
795 res->expiration_time = expiration_time;
796 res->data_size = data_size;
797 res->record_type = record_type;
799 memcpy (&res[1], data, data_size);
800 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
807 * We had to do a DNS lookup. Convert the result (if any) and return
810 * @param cls closure with the 'struct GNS_ResolverHandle'
811 * @param addr one of the addresses of the host, NULL for the last address
812 * @param addrlen length of the address
815 handle_dns_result (void *cls,
816 const struct sockaddr *addr,
819 struct GNS_ResolverHandle *rh = cls;
820 const struct sockaddr_in *sa4;
821 const struct sockaddr_in6 *sa6;
823 rh->std_resolve = NULL;
826 transmit_lookup_dns_result (rh);
829 switch (addr->sa_family)
832 sa4 = (const struct sockaddr_in *) addr;
834 0 /* expiration time is unknown */,
835 GNUNET_DNSPARSER_TYPE_A,
836 sizeof (struct in_addr),
840 sa6 = (const struct sockaddr_in6 *) addr;
842 0 /* expiration time is unknown */,
843 GNUNET_DNSPARSER_TYPE_AAAA,
844 sizeof (struct in6_addr),
855 * Task scheduled to continue with the resolution process.
857 * @param cls the 'struct GNS_ResolverHandle' of the resolution
858 * @param tc task context
861 recursive_resolution (void *cls,
862 const struct GNUNET_SCHEDULER_TaskContext *tc);
866 * Begin the resolution process from 'name', starting with
867 * the identification of the zone specified by 'name'.
869 * @param rh resolution to perform
872 start_resolver_lookup (struct GNS_ResolverHandle *rh);
876 * Function called with the result of a DNS resolution.
878 * @param cls the request handle of the resolution that
879 * we were attempting to make
880 * @param rs socket that received the response
881 * @param dns dns response, never NULL
882 * @param dns_len number of bytes in 'dns'
885 dns_result_parser (void *cls,
886 struct GNUNET_DNSSTUB_RequestSocket *rs,
887 const struct GNUNET_TUN_DnsHeader *dns,
890 struct GNS_ResolverHandle *rh = cls;
891 struct GNUNET_DNSPARSER_Packet *p;
892 const struct GNUNET_DNSPARSER_Record *rec;
893 unsigned int rd_count;
896 rh->dns_request = NULL;
897 GNUNET_SCHEDULER_cancel (rh->task_id);
898 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
899 p = GNUNET_DNSPARSER_parse ((const char *) dns,
903 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
904 _("Failed to parse DNS response\n"));
905 rh->proc (rh->proc_cls, 0, NULL);
906 GNS_resolver_lookup_cancel (rh);
910 if ( (p->num_answers > 0) &&
911 (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
912 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
914 GNUNET_free (rh->name);
915 rh->name = GNUNET_strdup (p->answers[0].data.hostname);
916 start_resolver_lookup (rh);
919 /* FIXME: add DNAME support */
921 /* convert from DNS to GNS format! */
922 rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
924 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
928 memset (rd, 0, sizeof (rd));
929 for (i=0;i<rd_count;i++)
931 if (i < p->num_answers)
932 rec = &p->answers[i];
933 else if (i < p->num_answers + p->num_authority_records)
934 rec = &p->authority_records[i - p->num_answers];
936 rec = &p->authority_records[i - p->num_answers - p->num_authority_records];
937 /* As we copied the full DNS name to 'rh->ac_tail->label', this
938 should be the correct check to see if this record is actually
939 a record for our label... */
940 if (0 != strcmp (rec->name,
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 "Dropping record `%s', does not match desired name `%s'\n",
950 rd[i - skip].record_type = rec->type;
951 rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
954 case GNUNET_DNSPARSER_TYPE_A:
955 if (rec->data.raw.data_len != sizeof (struct in_addr))
961 rd[i - skip].data_size = rec->data.raw.data_len;
962 rd[i - skip].data = rec->data.raw.data;
964 case GNUNET_DNSPARSER_TYPE_AAAA:
965 if (rec->data.raw.data_len != sizeof (struct in6_addr))
971 rd[i - skip].data_size = rec->data.raw.data_len;
972 rd[i - skip].data = rec->data.raw.data;
974 case GNUNET_DNSPARSER_TYPE_CNAME:
975 case GNUNET_DNSPARSER_TYPE_PTR:
976 case GNUNET_DNSPARSER_TYPE_NS:
977 rd[i - skip].data_size = strlen (rec->data.hostname) + 1;
978 rd[i - skip].data = rec->data.hostname;
980 case GNUNET_DNSPARSER_TYPE_SOA:
981 // FIXME: not implemented
982 // NOTE: consider exporting implementation in DNSPARSER lib!
986 case GNUNET_DNSPARSER_TYPE_MX:
987 // FIXME: not implemented
988 // NOTE: consider exporting implementation in DNSPARSER lib!
992 case GNUNET_DNSPARSER_TYPE_SRV:
993 // FIXME: not implemented
994 // NOTE: consider exporting implementation in DNSPARSER lib!
999 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1000 _("Skipping record of unsupported type %d\n"),
1006 rh->proc (rh->proc_cls, rd_count - skip, rd);
1007 GNS_resolver_lookup_cancel (rh);
1009 GNUNET_DNSPARSER_free_packet (p);
1014 * Perform recursive DNS resolution. Asks the given DNS resolver to
1015 * resolve "rh->dns_name", possibly recursively proceeding following
1016 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
1017 * we find the answer.
1019 * @param rh resolution information
1022 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
1024 struct AuthorityChain *ac;
1026 struct GNUNET_DNSPARSER_Query *query;
1027 struct GNUNET_DNSPARSER_Packet *p;
1029 size_t dns_request_length;
1032 GNUNET_assert (NULL != ac);
1033 GNUNET_assert (GNUNET_NO == ac->gns_authority);
1034 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
1037 sa_len = sizeof (struct sockaddr_in);
1040 sa_len = sizeof (struct sockaddr_in6);
1044 rh->proc (rh->proc_cls, 0, NULL);
1045 GNS_resolver_lookup_cancel (rh);
1048 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
1049 query->name = GNUNET_strdup (ac->label);
1050 query->type = rh->record_type;
1051 query->class = GNUNET_TUN_DNS_CLASS_INTERNET;
1052 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
1055 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1057 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
1058 p->flags.recursion_desired = 1;
1060 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
1063 rh->proc (rh->proc_cls, 0, NULL);
1064 GNS_resolver_lookup_cancel (rh);
1068 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
1069 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
1075 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
1079 GNUNET_free (dns_request);
1080 GNUNET_DNSPARSER_free_packet (p);
1085 * We encountered a CNAME record during our resolution.
1086 * Merge it into our chain.
1088 * @param rh resolution we are performing
1089 * @param cname value of the cname record we got for the current
1090 * authority chain tail
1093 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
1099 nlen = strlen (cname);
1102 &cname[nlen - 2])) )
1104 /* CNAME resolution continues relative to current domain */
1105 if (0 == rh->name_resolution_pos)
1107 res = GNUNET_strndup (cname, nlen - 2);
1108 rh->name_resolution_pos = nlen - 2;
1112 GNUNET_asprintf (&res,
1114 (int) rh->name_resolution_pos,
1118 rh->name_resolution_pos = strlen (res);
1120 GNUNET_free (rh->name);
1122 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1126 /* name is absolute, start from the beginning */
1127 GNUNET_free (rh->name);
1128 rh->name = GNUNET_strdup (cname);
1129 start_resolver_lookup (rh);
1134 * Process a records that were decrypted from a block.
1136 * @param cls closure with the 'struct GNS_ResolverHandle'
1137 * @param rd_count number of entries in @a rd array
1138 * @param rd array of records with data to store
1141 handle_gns_resolution_result (void *cls,
1142 unsigned int rd_count,
1143 const struct GNUNET_NAMESTORE_RecordData *rd);
1147 * Callback invoked from the VPN service once a redirection is
1148 * available. Provides the IP address that can now be used to
1149 * reach the requested destination. Replaces the "VPN" record
1150 * with the respective A/AAAA record and continues processing.
1152 * @param cls closure
1153 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
1154 * will match 'result_af' from the request
1155 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
1156 * that the VPN allocated for the redirection;
1157 * traffic to this IP will now be redirected to the
1158 * specified target peer; NULL on error
1161 vpn_allocation_cb (void *cls,
1163 const void *address)
1165 struct VpnContext *vpn_ctx = cls;
1166 struct GNS_ResolverHandle *rh = vpn_ctx->rh;
1167 struct GNUNET_NAMESTORE_RecordData rd[vpn_ctx->rd_count];
1170 vpn_ctx->vpn_request = NULL;
1172 GNUNET_assert (GNUNET_OK ==
1173 GNUNET_NAMESTORE_records_deserialize (vpn_ctx->rd_data_size,
1177 for (i=0;i<vpn_ctx->rd_count;i++)
1179 if (GNUNET_NAMESTORE_TYPE_VPN == rd[i].record_type)
1184 rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
1185 rd[i].data_size = sizeof (struct in_addr);
1186 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1188 rd[i].data = address;
1191 rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1192 rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
1194 rd[i].data = address;
1195 rd[i].data_size = sizeof (struct in6_addr);
1203 GNUNET_assert (i < vpn_ctx->rd_count);
1204 handle_gns_resolution_result (rh,
1207 GNUNET_free (vpn_ctx->rd_data);
1208 GNUNET_free (vpn_ctx);
1213 * Process a records that were decrypted from a block.
1215 * @param cls closure with the 'struct GNS_ResolverHandle'
1216 * @param rd_count number of entries in @a rd array
1217 * @param rd array of records with data to store
1220 handle_gns_resolution_result (void *cls,
1221 unsigned int rd_count,
1222 const struct GNUNET_NAMESTORE_RecordData *rd)
1224 struct GNS_ResolverHandle *rh = cls;
1225 struct AuthorityChain *ac;
1228 struct sockaddr *sa;
1229 struct sockaddr_in v4;
1230 struct sockaddr_in6 v6;
1233 struct VpnContext *vpn_ctx;
1234 const struct GNUNET_TUN_GnsVpnRecord *vpn;
1236 struct GNUNET_HashCode vhash;
1239 unsigned int scratch_len;
1241 if (0 == rh->name_resolution_pos)
1243 /* top-level match, are we done yet? */
1244 if ( (rd_count > 0) &&
1245 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1246 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1248 cname = GNUNET_strndup (rd[0].data,
1250 handle_gns_cname_result (rh,
1252 GNUNET_free (cname);
1255 /* If A/AAAA was requested, but we got a VPN
1256 record, we convert it to A/AAAA using GNUnet VPN */
1257 if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
1258 (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
1260 for (i=0;i<rd_count;i++)
1262 if (GNUNET_NAMESTORE_TYPE_VPN == rd[i].record_type)
1264 af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
1265 if (sizeof (struct GNUNET_TUN_GnsVpnRecord) <
1268 GNUNET_break_op (0);
1269 rh->proc (rh->proc_cls, 0, NULL);
1270 GNS_resolver_lookup_cancel (rh);
1273 vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
1274 vname = (const char *) &vpn[1];
1275 if ('\0' != vname[rd[i].data_size - 1 - sizeof (struct GNUNET_TUN_GnsVpnRecord)])
1277 GNUNET_break_op (0);
1278 rh->proc (rh->proc_cls, 0, NULL);
1279 GNS_resolver_lookup_cancel (rh);
1282 GNUNET_CRYPTO_hash (vname,
1283 strlen (vname), // FIXME: +1?
1285 vpn_ctx = GNUNET_new (struct VpnContext);
1286 rh->vpn_ctx = vpn_ctx;
1288 vpn_ctx->rd_data_size = GNUNET_NAMESTORE_records_get_size (rd_count,
1290 vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
1291 (void) GNUNET_NAMESTORE_records_serialize (rd_count,
1293 vpn_ctx->rd_data_size,
1295 vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
1301 GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
1308 /* convert relative names in record values to absolute names,
1309 using 'scratch' array for memory allocations */
1312 for (i=0;i<rd_count;i++)
1314 /* Strategy: dnsparser should expose API to make it easier
1315 for us to parse all of these binary record values individually;
1316 then, we check if the embedded name(s) end in "+", and if so,
1317 replace the "+" with the zone at "ac_tail", changing the name
1318 to a ".zkey". The name is allocated on the 'scratch' array,
1319 so we can free it afterwards. */
1320 switch (rd[i].record_type)
1322 case GNUNET_DNSPARSER_TYPE_CNAME:
1323 GNUNET_break (0); // FIXME: not implemented
1325 case GNUNET_DNSPARSER_TYPE_SOA:
1326 GNUNET_break (0); // FIXME: not implemented
1328 case GNUNET_DNSPARSER_TYPE_MX:
1329 GNUNET_break (0); // FIXME: not implemented
1331 case GNUNET_DNSPARSER_TYPE_SRV:
1332 GNUNET_break (0); // FIXME: not implemented
1339 /* yes, we are done, return result */
1340 rh->proc (rh->proc_cls, rd_count, rd);
1341 GNS_resolver_lookup_cancel (rh);
1342 for (i=0;i<scratch_len;i++)
1343 GNUNET_free (scratch[i]);
1344 GNUNET_array_grow (scratch,
1349 /* need to recurse, check if we can */
1350 for (i=0;i<rd_count;i++)
1352 switch (rd[i].record_type)
1354 case GNUNET_NAMESTORE_TYPE_PKEY:
1355 /* delegation to another zone */
1356 if (sizeof (struct GNUNET_CRYPTO_EccPublicKey) !=
1359 GNUNET_break_op (0);
1360 rh->proc (rh->proc_cls, 0, NULL);
1361 GNS_resolver_lookup_cancel (rh);
1364 /* expand authority chain */
1365 ac = GNUNET_new (struct AuthorityChain);
1367 ac->gns_authority = GNUNET_YES;
1368 memcpy (&ac->authority_info.gns_authority,
1370 sizeof (struct GNUNET_CRYPTO_EccPublicKey));
1371 ac->label = resolver_lookup_get_next_label (rh);
1372 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1376 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1379 case GNUNET_DNSPARSER_TYPE_NS:
1380 /* resolution continues within DNS */
1381 if (GNUNET_DNSPARSER_MAX_NAME_LENGTH < rd[i].data_size)
1383 GNUNET_break_op (0);
1384 rh->proc (rh->proc_cls, 0, NULL);
1385 GNS_resolver_lookup_cancel (rh);
1388 /* find associated A/AAAA record */
1390 for (j=0;j<rd_count;j++)
1392 switch (rd[j].record_type)
1394 case GNUNET_DNSPARSER_TYPE_A:
1395 if (sizeof (struct in_addr) != rd[i].data_size)
1397 GNUNET_break_op (0);
1398 rh->proc (rh->proc_cls, 0, NULL);
1399 GNS_resolver_lookup_cancel (rh);
1402 /* FIXME: might want to check if we support IPv4 here,
1403 and otherwise skip this one and hope we find another */
1404 memset (&v4, 0, sizeof (v4));
1405 sa_len = sizeof (v4);
1406 v4.sin_family = AF_INET;
1407 v4.sin_port = htons (53);
1408 #if HAVE_SOCKADDR_IN_SIN_LEN
1409 v4.sin_len = (u_char) sa_len;
1411 memcpy (&v4.sin_addr,
1413 sizeof (struct in_addr));
1414 sa = (struct sockaddr *) &v4;
1416 case GNUNET_DNSPARSER_TYPE_AAAA:
1417 if (sizeof (struct in6_addr) != rd[i].data_size)
1419 GNUNET_break_op (0);
1420 rh->proc (rh->proc_cls, 0, NULL);
1421 GNS_resolver_lookup_cancel (rh);
1424 /* FIXME: might want to check if we support IPv6 here,
1425 and otherwise skip this one and hope we find another */
1426 memset (&v6, 0, sizeof (v6));
1427 sa_len = sizeof (v6);
1428 v6.sin6_family = AF_INET6;
1429 v6.sin6_port = htons (53);
1430 #if HAVE_SOCKADDR_IN_SIN_LEN
1431 v6.sin6_len = (u_char) sa_len;
1433 memcpy (&v6.sin6_addr,
1435 sizeof (struct in6_addr));
1436 sa = (struct sockaddr *) &v6;
1442 /* expand authority chain */
1443 ac = GNUNET_new (struct AuthorityChain);
1445 strncpy (ac->authority_info.dns_authority.name,
1448 ac->authority_info.dns_authority.name[rd[i].data_size] = '\0';
1449 memcpy (&ac->authority_info.dns_authority.dns_ip,
1452 /* for DNS recursion, the label is the full DNS name,
1453 created from the remainder of the GNS name and the
1454 name in the NS record */
1455 GNUNET_asprintf (&ac->label,
1457 (int) rh->name_resolution_pos,
1459 ac->authority_info.dns_authority.name);
1460 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1463 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1465 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1466 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1468 rh->proc (rh->proc_cls, 0, NULL);
1469 GNS_resolver_lookup_cancel (rh);
1473 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1476 case GNUNET_DNSPARSER_TYPE_CNAME:
1477 cname = GNUNET_strndup (rd[i].data,
1479 handle_gns_cname_result (rh,
1481 GNUNET_free (cname);
1483 /* FIXME: handle DNAME */
1489 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1490 _("GNS lookup recursion failed (no delegation record found)\n"));
1491 rh->proc (rh->proc_cls, 0, NULL);
1492 GNS_resolver_lookup_cancel (rh);
1497 * Function called once the namestore has completed the request for
1500 * @param cls closure with the 'struct GNS_ResolverHandle'
1501 * @param success #GNUNET_OK on success
1502 * @param emsg error message
1505 namestore_cache_continuation (void *cls,
1509 struct GNS_ResolverHandle *rh = cls;
1511 rh->namestore_qe = NULL;
1513 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1514 _("Failed to cache GNS resolution: %s\n"),
1520 * Iterator called on each result obtained for a DHT
1521 * operation that expects a reply
1523 * @param cls closure with the 'struct GNS_ResolverHandle'
1524 * @param exp when will this value expire
1525 * @param key key of the result
1526 * @param get_path peers on reply path (or NULL if not recorded)
1527 * [0] = datastore's first neighbor, [length - 1] = local peer
1528 * @param get_path_length number of entries in get_path
1529 * @param put_path peers on the PUT path (or NULL if not recorded)
1530 * [0] = origin, [length - 1] = datastore
1531 * @param put_path_length number of entries in get_path
1532 * @param type type of the result
1533 * @param size number of bytes in data
1534 * @param data pointer to the result data
1537 handle_dht_response (void *cls,
1538 struct GNUNET_TIME_Absolute exp,
1539 const struct GNUNET_HashCode * key,
1540 const struct GNUNET_PeerIdentity *get_path,
1541 unsigned int get_path_length,
1542 const struct GNUNET_PeerIdentity *put_path,
1543 unsigned int put_path_length,
1544 enum GNUNET_BLOCK_Type type,
1545 size_t size, const void *data)
1547 struct GNS_ResolverHandle *rh = cls;
1548 struct AuthorityChain *ac = rh->ac_tail;
1549 const struct GNUNET_NAMESTORE_Block *block;
1551 GNUNET_DHT_get_stop (rh->get_handle);
1552 rh->get_handle = NULL;
1553 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1554 rh->dht_heap_node = NULL;
1555 if (size < sizeof (struct GNUNET_NAMESTORE_Block))
1557 /* how did this pass DHT block validation!? */
1559 rh->proc (rh->proc_cls, 0, NULL);
1560 GNS_resolver_lookup_cancel (rh);
1565 ntohs (block->purpose.size) +
1566 sizeof (struct GNUNET_CRYPTO_EccPublicKey) +
1567 sizeof (struct GNUNET_CRYPTO_EccSignature))
1569 /* how did this pass DHT block validation!? */
1571 rh->proc (rh->proc_cls, 0, NULL);
1572 GNS_resolver_lookup_cancel (rh);
1576 GNUNET_NAMESTORE_block_decrypt (block,
1577 &ac->authority_info.gns_authority,
1579 &handle_gns_resolution_result,
1582 GNUNET_break_op (0); /* block was ill-formed */
1583 rh->proc (rh->proc_cls, 0, NULL);
1584 GNS_resolver_lookup_cancel (rh);
1587 /* Cache well-formed blocks */
1588 rh->namestore_qe = GNUNET_NAMESTORE_block_cache (namestore_handle,
1590 &namestore_cache_continuation,
1596 * Process a record that was stored in the namestore.
1598 * @param cls closure with the `struct GNS_ResolverHandle`
1599 * @param block block that was stored in the namestore
1602 handle_namestore_block_response (void *cls,
1603 const struct GNUNET_NAMESTORE_Block *block)
1605 struct GNS_ResolverHandle *rh = cls;
1606 struct GNS_ResolverHandle *rx;
1607 struct AuthorityChain *ac = rh->ac_tail;
1608 const char *label = ac->label;
1609 const struct GNUNET_CRYPTO_EccPublicKey *auth = &ac->authority_info.gns_authority;
1610 struct GNUNET_HashCode query;
1612 GNUNET_NAMESTORE_query_from_public_key (auth,
1615 rh->namestore_qe = NULL;
1618 /* Namestore knows nothing; try DHT lookup */
1619 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1620 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1622 DHT_GNS_REPLICATION_LEVEL,
1623 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1625 &handle_dht_response, rh);
1626 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1628 GNUNET_TIME_absolute_get ().abs_value_us);
1629 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
1631 /* fail longest-standing DHT request */
1632 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
1633 rx->proc (rx->proc_cls, 0, NULL);
1634 GNS_resolver_lookup_cancel (rx);
1639 GNUNET_NAMESTORE_block_decrypt (block,
1642 &handle_gns_resolution_result,
1645 GNUNET_break_op (0); /* block was ill-formed */
1646 rh->proc (rh->proc_cls, 0, NULL);
1647 GNS_resolver_lookup_cancel (rh);
1654 * Lookup tail of our authority chain in the namestore.
1656 * @param rh query we are processing
1659 recursive_gns_resolution_namestore (struct GNS_ResolverHandle *rh)
1661 struct AuthorityChain *ac = rh->ac_tail;
1662 struct GNUNET_HashCode query;
1664 GNUNET_NAMESTORE_query_from_public_key (&ac->authority_info.gns_authority,
1667 rh->namestore_qe = GNUNET_NAMESTORE_lookup_block (namestore_handle,
1669 &handle_namestore_block_response,
1675 * Task scheduled to continue with the resolution process.
1677 * @param cls the `struct GNS_ResolverHandle` of the resolution
1678 * @param tc task context
1681 recursive_resolution (void *cls,
1682 const struct GNUNET_SCHEDULER_TaskContext *tc)
1684 struct GNS_ResolverHandle *rh = cls;
1686 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
1687 if (MAX_RECURSION < rh->loop_limiter++)
1689 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1690 "Encountered unbounded recursion resolving `%s'\n",
1692 rh->proc (rh->proc_cls, 0, NULL);
1693 GNS_resolver_lookup_cancel (rh);
1696 if (GNUNET_YES == rh->ac_tail->gns_authority)
1697 recursive_gns_resolution_namestore (rh);
1699 recursive_dns_resolution (rh);
1704 * Begin the resolution process from 'name', starting with
1705 * the identification of the zone specified by 'name'.
1707 * @param rh resolution to perform
1710 start_resolver_lookup (struct GNS_ResolverHandle *rh)
1712 struct AuthorityChain *ac;
1717 if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
1718 (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
1719 ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
1720 (GNUNET_YES != is_zkey_tld (rh->name)) ) )
1722 /* use standard DNS lookup */
1725 switch (rh->record_type)
1727 case GNUNET_DNSPARSER_TYPE_A:
1730 case GNUNET_DNSPARSER_TYPE_AAAA:
1737 rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
1744 if (is_zkey_tld (rh->name))
1746 /* Name ends with ".zkey", try to replace authority zone with zkey
1748 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
1749 x = resolver_lookup_get_next_label (rh); /* will return 'x' coordinate */
1750 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
1751 GNUNET_asprintf (&pkey,
1757 GNUNET_CRYPTO_ecc_public_key_from_string (pkey,
1759 &rh->authority_zone)) )
1761 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1762 _("Hostname `%s' is not well-formed, resolution fails\n"),
1764 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
1766 GNUNET_free_non_null (x);
1767 GNUNET_free_non_null (y);
1772 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
1773 GNUNET_free (resolver_lookup_get_next_label (rh));
1775 ac = GNUNET_new (struct AuthorityChain);
1777 ac->label = resolver_lookup_get_next_label (rh);
1778 if (NULL == ac->label)
1779 /* name was just "gnu", so we default to label '+' */
1780 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
1781 ac->gns_authority = GNUNET_YES;
1782 ac->authority_info.gns_authority = rh->authority_zone;
1783 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1786 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1792 * Lookup of a record in a specific zone calls lookup result processor
1795 * @param zone the zone to perform the lookup in
1796 * @param record_type the record type to look up
1797 * @param name the name to look up
1798 * @param shorten_key a private key for use with PSEU import (can be NULL)
1799 * @param only_cached #GNUNET_NO to only check locally not DHT for performance
1800 * @param proc the processor to call on result
1801 * @param proc_cls the closure to pass to @a proc
1802 * @return handle to cancel operation
1804 struct GNS_ResolverHandle *
1805 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EccPublicKey *zone,
1806 uint32_t record_type,
1808 const struct GNUNET_CRYPTO_EccPrivateKey *shorten_key,
1810 GNS_ResultProcessor proc, void *proc_cls)
1812 struct GNS_ResolverHandle *rh;
1814 rh = GNUNET_new (struct GNS_ResolverHandle);
1815 GNUNET_CONTAINER_DLL_insert (rlh_head,
1818 rh->authority_zone = *zone;
1820 rh->proc_cls = proc_cls;
1821 rh->only_cached = only_cached;
1822 rh->record_type = record_type;
1823 rh->name = GNUNET_strdup (name);
1824 rh->name_resolution_pos = strlen (name);
1825 if (NULL != shorten_key)
1827 rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
1828 *rh->shorten_key = *shorten_key;
1830 start_resolver_lookup (rh);
1836 * Cancel active resolution (i.e. client disconnected).
1838 * @param rh resolution to abort
1841 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
1843 struct DnsResult *dr;
1844 struct AuthorityChain *ac;
1845 struct VpnContext *vpn_ctx;
1847 GNUNET_CONTAINER_DLL_remove (rlh_head,
1850 while (NULL != (ac = rh->ac_head))
1852 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
1855 GNUNET_free (ac->label);
1858 if (GNUNET_SCHEDULER_NO_TASK != rh->task_id)
1860 GNUNET_SCHEDULER_cancel (rh->task_id);
1861 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
1863 if (NULL != rh->get_handle)
1865 GNUNET_DHT_get_stop (rh->get_handle);
1866 rh->get_handle = NULL;
1868 if (NULL != rh->dht_heap_node)
1870 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1871 rh->dht_heap_node = NULL;
1873 if (NULL != (vpn_ctx = rh->vpn_ctx))
1875 GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
1876 GNUNET_free (vpn_ctx->rd_data);
1877 GNUNET_free (vpn_ctx);
1879 if (NULL != rh->dns_request)
1881 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
1882 rh->dns_request = NULL;
1884 if (NULL != rh->namestore_qe)
1886 GNUNET_NAMESTORE_cancel (rh->namestore_qe);
1887 rh->namestore_qe = NULL;
1889 if (NULL != rh->std_resolve)
1891 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
1892 rh->std_resolve = NULL;
1894 while (NULL != (dr = rh->dns_result_head))
1896 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
1897 rh->dns_result_tail,
1901 GNUNET_free_non_null (rh->shorten_key);
1902 GNUNET_free (rh->name);
1907 /* ***************** Resolver initialization ********************* */
1911 * Initialize the resolver
1913 * @param nh the namestore handle
1914 * @param dht the dht handle
1915 * @param c configuration handle
1916 * @param max_bg_queries maximum number of parallel background queries in dht
1919 GNS_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
1920 struct GNUNET_DHT_Handle *dht,
1921 const struct GNUNET_CONFIGURATION_Handle *c,
1922 unsigned long long max_bg_queries)
1927 namestore_handle = nh;
1930 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1931 max_allowed_background_queries = max_bg_queries;
1933 GNUNET_CONFIGURATION_get_value_string (c,
1938 /* user did not specify DNS resolver, use 8.8.8.8 */
1939 dns_ip = GNUNET_strdup ("8.8.8.8");
1941 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
1942 GNUNET_free (dns_ip);
1943 vpn_handle = GNUNET_VPN_connect (cfg);
1951 GNS_resolver_done ()
1953 struct GNS_ResolverHandle *rh;
1955 /* abort active resolutions */
1956 while (NULL != (rh = rlh_head))
1958 rh->proc (rh->proc_cls, 0, NULL);
1959 GNS_resolver_lookup_cancel (rh);
1961 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
1962 dht_lookup_heap = NULL;
1963 GNUNET_DNSSTUB_stop (dns_handle);
1965 GNUNET_VPN_disconnect (vpn_handle);
1968 namestore_handle = NULL;
1972 /* *************** common helper functions (do not really belong here) *********** */
1975 * Checks if @a name ends in ".TLD"
1977 * @param name the name to check
1978 * @param tld the TLD to check for
1979 * @return GNUNET_YES or GNUNET_NO
1982 is_tld (const char* name, const char* tld)
1986 if (strlen (name) <= strlen (tld))
1988 offset = strlen (name) - strlen (tld);
1989 if (0 != strcmp (name + offset, tld))
1995 /* end of gnunet-service-gns_resolver.c */