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 CNAME records (idea: manipulate rh->name)
29 * - GNS: handle VPN records (easy)
30 * - GNS: handle special SRV names --- no delegation, direct lookup;
31 * can likely be done in 'resolver_lookup_get_next_label'.
32 * - recursive DNS resolution
33 * - shortening triggers
34 * - revocation checks (make optional: privacy!)
37 * - We currently go to the DHT simply if we find no local reply; this
38 * is incorrect; the correct rules for going to DHT are:
40 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
41 * 2. No entry in the NS existed AND
42 * 3. The zone queried is not the local resolver's zone AND
43 * 4. The name that was looked up is '+'
44 * because if it was any other canonical name we either already queried
45 * the DHT for the authority in the authority lookup phase (and thus
46 * would already have an entry in the NS for the record)
47 * 5. We are not in cache only mode
49 * - We currently never look "into" the records; for example,
50 * MX, SRV and SOA records may include ".+" that need to be
55 #include "gnunet_util_lib.h"
56 #include "gnunet_transport_service.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_vpn_service.h"
70 * Default DHT timeout for lookups.
72 #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
75 * Default timeout for DNS lookups.
77 #define DNS_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
80 * DHT replication level
82 #define DHT_GNS_REPLICATION_LEVEL 5
85 * How deep do we allow recursions to go before we abort?
87 #define MAX_RECURSION 256
91 * DLL to hold the authority chain we had to pass in the resolution
99 struct AuthorityChain *prev;
104 struct AuthorityChain *next;
107 * Resolver handle this entry in the chain belongs to.
109 struct GNS_ResolverHandle *rh;
112 * label/name corresponding to the authority
117 * #GNUNET_YES if the authority was a GNS authority,
118 * #GNUNET_NO if the authority was a DNS authority.
123 * Information about the resolver authority for this label.
129 * The zone of the GNS authority
131 struct GNUNET_CRYPTO_EccPublicKey gns_authority;
136 * Domain of the DNS resolver that is the authority.
137 * (appended to construct the DNS name to resolve;
138 * this is NOT the DNS name of the DNS server!).
140 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
143 * IP address of the DNS resolver that is authoritative.
144 * (this implementation currently only supports one
147 struct sockaddr_storage dns_ip;
157 * Resolution status indicator
159 enum ResolutionStatus
162 * the name to lookup exists
164 RSL_RECORD_EXISTS = 1,
167 * the name in the record expired
169 RSL_RECORD_EXPIRED = 2,
172 * resolution timed out
177 * Found VPN delegation
179 RSL_DELEGATE_VPN = 8,
182 * Found NS delegation
184 RSL_DELEGATE_NS = 16,
187 * Found PKEY delegation
189 RSL_DELEGATE_PKEY = 32,
194 RSL_CNAME_FOUND = 64,
197 * Found PKEY has been revoked
199 RSL_PKEY_REVOKED = 128
204 * A result we got from DNS.
212 struct DnsResult *next;
217 struct DnsResult *prev;
220 * Binary value stored in the DNS record (appended to this struct)
225 * Expiration time for the DNS record, 0 if we didn't
226 * get anything useful (i.e. 'gethostbyname' was used).
228 uint64_t expiration_time;
231 * Number of bytes in 'data'.
236 * Type of the GNS/DNS record.
238 uint32_t record_type;
244 * Handle to a currenty pending resolution. On result (positive or
245 * negative) the #GNS_ResultProcessor is called.
247 struct GNS_ResolverHandle
253 struct GNS_ResolverHandle *next;
258 struct GNS_ResolverHandle *prev;
261 * The top-level GNS authoritative zone to query
263 struct GNUNET_CRYPTO_EccPublicKey authority_zone;
266 * called when resolution phase finishes
268 GNS_ResultProcessor proc;
271 * closure passed to proc
276 * Handle for DHT lookups. should be NULL if no lookups are in progress
278 struct GNUNET_DHT_GetHandle *get_handle;
281 * Handle to a VPN request, NULL if none is active.
283 struct GNUNET_VPN_RedirectionRequest *vpn_handle;
286 * Socket for a DNS request, NULL if none is active.
288 struct GNUNET_DNSSTUB_RequestSocket *dns_request;
291 * Handle for standard DNS resolution, NULL if none is active.
293 struct GNUNET_RESOLVER_RequestHandle *std_resolve;
296 * Pending Namestore task
298 struct GNUNET_NAMESTORE_QueueEntry *namestore_qe;
301 * Heap node associated with this lookup. Used to limit number of
302 * concurrent requests.
304 struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
307 * DLL to store the authority chain
309 struct AuthorityChain *ac_head;
312 * DLL to store the authority chain
314 struct AuthorityChain *ac_tail;
317 * Private key of the shorten zone, NULL to not shorten.
319 struct GNUNET_CRYPTO_EccPrivateKey *shorten_key;
322 * ID of a task associated with the resolution process.
324 GNUNET_SCHEDULER_TaskIdentifier task_id;
327 * The name to resolve
332 * DLL of results we got from DNS.
334 struct DnsResult *dns_result_head;
337 * DLL of results we got from DNS.
339 struct DnsResult *dns_result_tail;
342 * Current offset in 'name' where we are resolving.
344 size_t name_resolution_pos;
352 * Desired type for the resolution.
357 * We increment the loop limiter for each step in a recursive
358 * resolution. If it passes our threshold (i.e. due to
359 * self-recursion in the resolution, i.e CNAME fun), we stop.
361 unsigned int loop_limiter;
367 * Handle for a PSEU lookup used to shorten names.
369 struct GetPseuAuthorityHandle
374 struct GetPseuAuthorityHandle *next;
379 struct GetPseuAuthorityHandle *prev;
382 * Private key of the (shorten) zone to store the resulting
385 struct GNUNET_CRYPTO_EccPrivateKey shorten_zone_key;
388 * Original label (used if no PSEU record is found).
390 char label[GNUNET_DNSPARSER_MAX_LABEL_LENGTH + 1];
393 * Label we are currently trying out (during #perform_pseu_lookup).
398 * The zone for which we are trying to find the PSEU record.
400 struct GNUNET_CRYPTO_EccPublicKey target_zone;
403 * Handle for DHT lookups. Should be NULL if no lookups are in progress
405 struct GNUNET_DHT_GetHandle *get_handle;
408 * Handle to namestore request
410 struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
413 * Task to abort DHT lookup operation.
415 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
421 * Our handle to the namestore service
423 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
426 * Our handle to the vpn service
428 static struct GNUNET_VPN_Handle *vpn_handle;
431 * Resolver handle to the dht
433 static struct GNUNET_DHT_Handle *dht_handle;
436 * Handle to perform DNS lookups.
438 static struct GNUNET_DNSSTUB_Context *dns_handle;
441 * Heap for limiting parallel DHT lookups
443 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
446 * Maximum amount of parallel queries to the DHT
448 static unsigned long long max_allowed_background_queries;
451 * Head of PSEU/shorten operations list.
453 struct GetPseuAuthorityHandle *gph_head;
456 * Tail of PSEU/shorten operations list.
458 struct GetPseuAuthorityHandle *gph_tail;
461 * Head of resolver lookup list
463 static struct GNS_ResolverHandle *rlh_head;
466 * Tail of resolver lookup list
468 static struct GNS_ResolverHandle *rlh_tail;
471 * Global configuration.
473 static const struct GNUNET_CONFIGURATION_Handle *cfg;
477 * Check if name is in srv format (_x._y.xxx)
480 * @return GNUNET_YES if true
483 is_srv (const char *name)
490 if (NULL == strstr (name, "._"))
493 ndup = GNUNET_strdup (name);
495 if (NULL == strtok (NULL, "."))
497 if (NULL == strtok (NULL, "."))
499 if (NULL != strtok (NULL, "."))
507 * Determine if this name is canonical (is a legal name in a zone, without delegation);
508 * note that we do not test that the name does not contain illegal characters, we only
509 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
510 * even though they consist of multiple labels.
513 * a.b.gnu = not canonical
515 * _foo._srv = canonical
516 * _f.bar = not canonical
518 * @param name the name to test
519 * @return GNUNET_YES if canonical
522 is_canonical (const char *name)
527 if (NULL == strchr (name, '.'))
532 while (NULL != (dot = strchr (pos, '.')))
541 /* ******************** Shortening logic ************************ */
545 * Cleanup a 'struct GetPseuAuthorityHandle', terminating all
546 * pending activities.
548 * @param gph handle to terminate
551 free_get_pseu_authority_handle (struct GetPseuAuthorityHandle *gph)
553 if (NULL != gph->get_handle)
555 GNUNET_DHT_get_stop (gph->get_handle);
556 gph->get_handle = NULL;
558 if (NULL != gph->namestore_task)
560 GNUNET_NAMESTORE_cancel (gph->namestore_task);
561 gph->namestore_task = NULL;
563 if (GNUNET_SCHEDULER_NO_TASK != gph->timeout_task)
565 GNUNET_SCHEDULER_cancel (gph->timeout_task);
566 gph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
568 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
569 GNUNET_free_non_null (gph->current_label);
575 * Continuation for pkey record creation (shorten)
577 * @param cls a GetPseuAuthorityHandle
578 * @param success unused
582 create_pkey_cont (void* cls,
586 struct GetPseuAuthorityHandle* gph = cls;
588 gph->namestore_task = NULL;
589 free_get_pseu_authority_handle (gph);
594 * Namestore calls this function if we have record for this name.
595 * (or with rd_count=0 to indicate no matches).
597 * @param cls the pending query
598 * @param rd_count the number of records with 'name'
599 * @param rd the record data
602 process_pseu_lookup_ns (void *cls,
603 unsigned int rd_count,
604 const struct GNUNET_NAMESTORE_RecordData *rd);
608 * We obtained a result for our query to the shorten zone from
609 * the namestore. Try to decrypt.
611 * @param cls the handle to our shorten operation
612 * @param block resulting encrypted block
615 process_pseu_block_ns (void *cls,
616 const struct GNUNET_NAMESTORE_Block *block)
618 struct GetPseuAuthorityHandle *gph = cls;
619 struct GNUNET_CRYPTO_EccPublicKey pub;
621 gph->namestore_task = NULL;
624 process_pseu_lookup_ns (gph, 0, NULL);
627 GNUNET_CRYPTO_ecc_key_get_public (&gph->shorten_zone_key,
630 GNUNET_NAMESTORE_block_decrypt (block,
633 &process_pseu_lookup_ns,
637 free_get_pseu_authority_handle (gph);
644 * Lookup in the namestore for the shorten zone the given label.
646 * @param gph the handle to our shorten operation
647 * @param label the label to lookup
650 perform_pseu_lookup (struct GetPseuAuthorityHandle *gph,
653 struct GNUNET_CRYPTO_EccPublicKey pub;
654 struct GNUNET_HashCode query;
656 GNUNET_CRYPTO_ecc_key_get_public (&gph->shorten_zone_key,
658 GNUNET_free_non_null (gph->current_label);
659 gph->current_label = GNUNET_strdup (label);
660 GNUNET_NAMESTORE_query_from_public_key (&pub,
663 gph->namestore_task = GNUNET_NAMESTORE_lookup_block (namestore_handle,
665 &process_pseu_block_ns,
671 * Namestore calls this function if we have record for this name.
672 * (or with rd_count=0 to indicate no matches).
674 * @param cls the pending query
675 * @param rd_count the number of records with 'name'
676 * @param rd the record data
679 process_pseu_lookup_ns (void *cls,
680 unsigned int rd_count,
681 const struct GNUNET_NAMESTORE_RecordData *rd)
683 struct GetPseuAuthorityHandle *gph = cls;
684 struct GNUNET_NAMESTORE_RecordData new_pkey;
686 gph->namestore_task = NULL;
689 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
690 "Name `%s' already taken, cannot shorten.\n",
692 /* if this was not yet the original label, try one more
693 time, this time not using PSEU but the original label */
694 if (0 == strcmp (gph->current_label,
697 free_get_pseu_authority_handle (gph);
701 perform_pseu_lookup (gph, gph->label);
705 /* name is available */
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
707 "Shortening `%s' to `%s'\n",
708 GNUNET_NAMESTORE_z2s (&gph->target_zone),
710 new_pkey.expiration_time = UINT64_MAX;
711 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_EccPublicKey);
712 new_pkey.data = &gph->target_zone;
713 new_pkey.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
714 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
715 | GNUNET_NAMESTORE_RF_PRIVATE
716 | GNUNET_NAMESTORE_RF_PENDING;
718 = GNUNET_NAMESTORE_records_store (namestore_handle,
719 &gph->shorten_zone_key,
722 &create_pkey_cont, gph);
727 * Process result of a DHT lookup for a PSEU record.
729 * @param gph the handle to our shorten operation
730 * @param pseu the pseu result or NULL
733 process_pseu_result (struct GetPseuAuthorityHandle* gph,
738 /* no PSEU found, try original label */
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "No PSEU found, trying original label `%s' instead.\n",
742 perform_pseu_lookup (gph, gph->label);
745 /* check if 'pseu' is taken */
746 perform_pseu_lookup (gph, pseu);
751 * Handle timeout for DHT request during shortening.
753 * @param cls the request handle as closure
754 * @param tc the task context
757 handle_auth_discovery_timeout (void *cls,
758 const struct GNUNET_SCHEDULER_TaskContext *tc)
760 struct GetPseuAuthorityHandle *gph = cls;
762 gph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
764 "DHT lookup for PSEU query timed out.\n");
765 GNUNET_DHT_get_stop (gph->get_handle);
766 gph->get_handle = NULL;
767 process_pseu_result (gph, NULL);
772 * Handle decrypted records from DHT result.
774 * @param cls closure with our 'struct GetPseuAuthorityHandle'
775 * @param rd_count number of entries in 'rd' array
776 * @param rd array of records with data to store
779 process_auth_records (void *cls,
780 unsigned int rd_count,
781 const struct GNUNET_NAMESTORE_RecordData *rd)
783 struct GetPseuAuthorityHandle *gph = cls;
786 for (i=0; i < rd_count; i++)
788 if (GNUNET_NAMESTORE_TYPE_PSEU == rd[i].record_type)
791 process_pseu_result (gph,
792 (const char *) rd[i].data);
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797 "No PSEU record found in DHT reply.\n");
798 process_pseu_result (gph, NULL);
803 * Function called when we find a PSEU entry in the DHT
805 * @param cls the request handle
806 * @param exp lifetime
807 * @param key the key the record was stored under
808 * @param get_path get path
809 * @param get_path_length get path length
810 * @param put_path put path
811 * @param put_path_length put path length
812 * @param type the block type
813 * @param size the size of the record
814 * @param data the record data
817 process_auth_discovery_dht_result (void* cls,
818 struct GNUNET_TIME_Absolute exp,
819 const struct GNUNET_HashCode *key,
820 const struct GNUNET_PeerIdentity *get_path,
821 unsigned int get_path_length,
822 const struct GNUNET_PeerIdentity *put_path,
823 unsigned int put_path_length,
824 enum GNUNET_BLOCK_Type type,
828 struct GetPseuAuthorityHandle *gph = cls;
829 const struct GNUNET_NAMESTORE_Block *block;
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832 "Got DHT result for PSEU request\n");
833 GNUNET_DHT_get_stop (gph->get_handle);
834 gph->get_handle = NULL;
835 GNUNET_SCHEDULER_cancel (gph->timeout_task);
836 gph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
840 /* is this allowed!? */
842 process_pseu_result (gph, NULL);
845 if (size < sizeof (struct GNUNET_NAMESTORE_Block))
847 /* how did this pass DHT block validation!? */
849 process_pseu_result (gph, NULL);
854 ntohs (block->purpose.size) +
855 sizeof (struct GNUNET_CRYPTO_EccPublicKey) +
856 sizeof (struct GNUNET_CRYPTO_EccSignature))
858 /* how did this pass DHT block validation!? */
860 process_pseu_result (gph, NULL);
864 GNUNET_NAMESTORE_block_decrypt (block,
867 &process_auth_records,
870 /* other peer encrypted invalid block, complain */
872 process_pseu_result (gph, NULL);
879 * Callback called by namestore for a zone to name result. We're
880 * trying to see if a short name for a given zone already exists.
882 * @param cls the closure
883 * @param zone_key the zone we queried
884 * @param name the name found or NULL
885 * @param rd_len number of records for the name
886 * @param rd the record data (PKEY) for the name
889 process_zone_to_name_discover (void *cls,
890 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
893 const struct GNUNET_NAMESTORE_RecordData *rd)
895 struct GetPseuAuthorityHandle* gph = cls;
896 struct GNUNET_HashCode lookup_key;
898 gph->namestore_task = NULL;
901 /* we found a match in our own zone */
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
903 "Shortening aborted, name `%s' already reserved for the zone\n",
905 free_get_pseu_authority_handle (gph);
908 /* record does not yet exist, go into DHT to find PSEU record */
909 GNUNET_NAMESTORE_query_from_public_key (&gph->target_zone,
912 gph->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
913 &handle_auth_discovery_timeout,
915 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
916 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
918 DHT_GNS_REPLICATION_LEVEL,
919 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
921 &process_auth_discovery_dht_result,
927 * Start shortening algorithm, try to allocate a nice short
928 * canonical name for @a pub in @a shorten_zone, using
929 * @a original_label as one possible suggestion.
931 * @param original_label original label for the zone
932 * @param pub public key of the zone to shorten
933 * @param shorten_zone private key of the target zone for the new record
936 start_shorten (const char *original_label,
937 const struct GNUNET_CRYPTO_EccPublicKey *pub,
938 const struct GNUNET_CRYPTO_EccPrivateKey *shorten_zone)
940 struct GetPseuAuthorityHandle *gph;
942 if (strlen (original_label) > GNUNET_DNSPARSER_MAX_LABEL_LENGTH)
947 gph = GNUNET_new (struct GetPseuAuthorityHandle);
948 gph->shorten_zone_key = *shorten_zone;
949 gph->target_zone = *pub;
950 strcpy (gph->label, original_label);
951 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
952 /* first, check if we *already* have a record for this zone */
953 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
956 &process_zone_to_name_discover,
961 /* ************************** Resolution **************************** */
967 * VPN redirect result callback
969 * @param cls the resolver handle
970 * @param af the requested address family
971 * @param address in_addr(6) respectively
974 process_record_result_vpn (void* cls, int af, const void *address)
976 struct ResolverHandle *rh = cls;
977 struct RecordLookupHandle *rlh = rh->proc_cls;
978 struct GNUNET_NAMESTORE_RecordData rd;
980 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
981 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
985 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
986 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
988 if (GNUNET_DNSPARSER_TYPE_A != rlh->record_type)
990 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
991 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
993 rh->proc (rh->proc_cls, rh, 0, NULL);
996 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
997 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
999 rd.data_size = sizeof (struct in_addr);
1001 rh->proc (rh->proc_cls, rh, 1, &rd);
1004 else if (AF_INET6 == af)
1006 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1007 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1009 if (GNUNET_DNSPARSER_TYPE_AAAA != rlh->record_type)
1011 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1012 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1014 rh->proc (rh->proc_cls, rh, 0, NULL);
1017 rd.record_type = GNUNET_DNSPARSER_TYPE_AAAA;
1018 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1020 rd.data_size = sizeof (struct in6_addr);
1022 rh->proc (rh->proc_cls, rh, 1, &rd);
1026 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1027 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1029 rh->proc (rh->proc_cls, rh, 0, NULL);
1037 * The final phase of resoution.
1038 * We found a VPN RR and want to request an IPv4/6 address
1040 * @param rh the pending lookup handle
1041 * @param rd_count length of record data
1042 * @param rd record data containing VPN RR
1045 resolve_record_vpn (struct ResolverHandle *rh,
1046 unsigned int rd_count,
1047 const struct GNUNET_NAMESTORE_RecordData *rd)
1049 struct RecordLookupHandle *rlh = rh->proc_cls;
1050 struct GNUNET_HashCode serv_desc;
1051 struct GNUNET_TUN_GnsVpnRecord* vpn;
1054 /* We cancel here as to not include the ns lookup in the timeout */
1055 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1057 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1058 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1060 /* Start shortening */
1061 if ((NULL != rh->priv_key) &&
1062 (GNUNET_YES == is_canonical (rh->name)))
1064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1065 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1067 start_shorten (rh->authority_chain_head,
1071 vpn = (struct GNUNET_TUN_GnsVpnRecord*)rd->data;
1072 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1073 strlen ((char*)&vpn[1]) + 1,
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1079 GNUNET_h2s (&vpn->peer));
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1082 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1085 GNUNET_h2s (&serv_desc));
1086 rh->proc = &handle_record_vpn;
1087 if (GNUNET_DNSPARSER_TYPE_A == rlh->record_type)
1092 if (NULL == vpn_handle)
1094 vpn_handle = GNUNET_VPN_connect (cfg);
1095 if (NULL == vpn_handle)
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1098 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1099 finish_lookup (rh, rh->proc_cls, 0, NULL);
1104 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1105 af, ntohs (vpn->proto),
1106 (struct GNUNET_PeerIdentity *)&vpn->peer,
1109 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1110 &process_record_result_vpn,
1113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1114 "Error connecting to VPN (not available on W32 yet)\n");
1115 finish_lookup (rh, rh->proc_cls, 0, NULL);
1120 //FIXME maybe define somewhere else?
1121 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
1122 +(GNUNET_DNSPARSER_MAX_NAME_LENGTH*2)
1123 #define MAX_MX_LENGTH sizeof(uint16_t)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
1124 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
1128 * Exands a name ending in .+ with the zone of origin.
1129 * FIXME: funky api: 'dest' must be large enough to hold
1130 * the result; this is a bit yucky...
1132 * @param dest destination buffer
1133 * @param src the .+ name
1134 * @param repl the string to replace the + with
1137 expand_plus (char* dest,
1142 size_t s_len = strlen (src) + 1;
1144 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
1147 /* no postprocessing */
1148 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1149 "GNS_POSTPROCESS: %s too short\n", src);
1150 memcpy (dest, src, s_len);
1153 if (0 == strcmp (src + s_len - 3, ".+"))
1155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1156 "GNS_POSTPROCESS: Expanding .+ in %s\n",
1158 memset (dest, 0, s_len + strlen (repl) + strlen(GNUNET_GNS_TLD));
1160 pos = dest + s_len - 2;
1162 pos += strlen (repl);
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 "GNS_POSTPROCESS: Expanded to %s\n",
1169 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1170 "GNS_POSTPROCESS: No postprocessing for %s\n", src);
1171 memcpy (dest, src, s_len);
1180 finish_lookup (struct ResolverHandle *rh,
1181 struct RecordLookupHandle* rlh,
1182 unsigned int rd_count,
1183 const struct GNUNET_NAMESTORE_RecordData *rd)
1186 char new_rr_data[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
1187 char new_mx_data[MAX_MX_LENGTH];
1188 char new_soa_data[MAX_SOA_LENGTH];
1189 char new_srv_data[MAX_SRV_LENGTH];
1190 struct GNUNET_TUN_DnsSrvRecord *old_srv;
1191 struct GNUNET_TUN_DnsSrvRecord *new_srv;
1192 struct GNUNET_TUN_DnsSoaRecord *old_soa;
1193 struct GNUNET_TUN_DnsSoaRecord *new_soa;
1194 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
1197 unsigned int offset;
1199 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1201 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1202 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1205 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
1208 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
1210 for (i = 0; i < rd_count; i++)
1213 if ((GNUNET_DNSPARSER_TYPE_NS != rd[i].record_type) &&
1214 (GNUNET_DNSPARSER_TYPE_PTR != rd[i].record_type) &&
1215 (GNUNET_DNSPARSER_TYPE_CNAME != rd[i].record_type) &&
1216 (GNUNET_DNSPARSER_TYPE_MX != rd[i].record_type) &&
1217 (GNUNET_DNSPARSER_TYPE_SOA != rd[i].record_type) &&
1218 (GNUNET_DNSPARSER_TYPE_SRV != rd[i].record_type))
1220 p_rd[i].data = rd[i].data;
1225 * for all those records we 'should'
1226 * also try to resolve the A/AAAA records (RFC1035)
1227 * This is a feature and not important
1230 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1231 "GNS_POSTPROCESS: Postprocessing\n");
1232 if (0 == strcmp(rh->name, GNUNET_GNS_MASTERZONE_STR))
1233 repl_string = rlh->name;
1235 repl_string = rlh->name+strlen(rh->name)+1;
1238 if (GNUNET_DNSPARSER_TYPE_MX == rd[i].record_type)
1240 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
1241 offset = sizeof (uint16_t);
1242 pos = new_mx_data + offset;
1243 // FIXME: how do we know that 'pos' has enough space for the new name?
1244 expand_plus (pos, (char*)rd[i].data+sizeof(uint16_t),
1246 offset += strlen(new_mx_data+sizeof(uint16_t)) + 1;
1247 p_rd[i].data = new_mx_data;
1248 p_rd[i].data_size = offset;
1250 else if (GNUNET_DNSPARSER_TYPE_SRV == rd[i].record_type)
1253 * Prio, weight and port
1255 new_srv = (struct GNUNET_TUN_DnsSrvRecord*)new_srv_data;
1256 old_srv = (struct GNUNET_TUN_DnsSrvRecord*)rd[i].data;
1257 new_srv->prio = old_srv->prio;
1258 new_srv->weight = old_srv->weight;
1259 new_srv->port = old_srv->port;
1260 // FIXME: how do we know that '&new_srv[1]' has enough space for the new name?
1261 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
1263 p_rd[i].data = new_srv_data;
1264 p_rd[i].data_size = sizeof (struct GNUNET_TUN_DnsSrvRecord) + strlen ((char*)&new_srv[1]) + 1;
1266 else if (GNUNET_DNSPARSER_TYPE_SOA == rd[i].record_type)
1268 /* expand mname and rname */
1269 old_soa = (struct GNUNET_TUN_DnsSoaRecord*)rd[i].data;
1270 new_soa = (struct GNUNET_TUN_DnsSoaRecord*)new_soa_data;
1271 memcpy (new_soa, old_soa, sizeof (struct GNUNET_TUN_DnsSoaRecord));
1272 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
1273 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
1274 offset = strlen ((char*)&new_soa[1]) + 1;
1275 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
1276 expand_plus((char*)&new_soa[1] + offset,
1277 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
1279 p_rd[i].data_size = sizeof (struct GNUNET_TUN_DnsSoaRecord)
1281 + strlen ((char*)&new_soa[1] + offset);
1282 p_rd[i].data = new_soa_data;
1287 // FIXME: how do we know that 'rd[i].data' has enough space for the new name?
1288 expand_plus(pos, (char*)rd[i].data, repl_string);
1289 p_rd[i].data_size = strlen(new_rr_data)+1;
1290 p_rd[i].data = new_rr_data;
1295 rlh->proc(rlh->proc_cls, rd_count, p_rd);
1297 free_resolver_handle (rh);
1303 ///////////////////////////////////////////////////////////////////////////////////////////////////
1304 ///////////////////////////////////////////////////////////////////////////////////////////////////
1305 ///////////////////////////////////////////////////////////////////////////////////////////////////
1306 ///////////////////////////////////////////////////////////////////////////////////////////////////
1307 ///////////////////////////////////////////////////////////////////////////////////////////////////
1311 * Task scheduled to asynchronously fail a resolution.
1313 * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
1314 * @param tc task context
1317 fail_resolution (void *cls,
1318 const struct GNUNET_SCHEDULER_TaskContext *tc)
1320 struct GNS_ResolverHandle *rh = cls;
1322 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
1323 rh->proc (rh->proc_cls, 0, NULL);
1324 GNS_resolver_lookup_cancel (rh);
1329 * Get the next, rightmost label from the name that we are trying to resolve,
1330 * and update the resolution position accordingly.
1332 * @param rh handle to the resolution operation to get the next label from
1333 * @return NULL if there are no more labels
1336 resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
1342 if (0 == rh->name_resolution_pos)
1344 dot = memrchr (rh->name, (int) '.', rh->name_resolution_pos);
1347 /* done, this was the last one */
1348 len = rh->name_resolution_pos;
1350 rh->name_resolution_pos = 0;
1354 /* advance by one label */
1355 len = rh->name_resolution_pos - (dot - rh->name) - 1;
1357 rh->name_resolution_pos = dot - rh->name;
1359 return GNUNET_strndup (rp, len);
1364 * Gives the cummulative result obtained to the callback and clean up the request.
1366 * @param rh resolution process that has culminated in a result
1369 transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
1371 struct DnsResult *pos;
1376 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
1379 struct GNUNET_NAMESTORE_RecordData rd[n];
1382 for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
1384 rd[i].data = pos->data;
1385 rd[i].data_size = pos->data_size;
1386 rd[i].record_type = pos->record_type;
1387 if (0 == pos->expiration_time)
1389 rd[i].flags = GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
1390 rd[i].expiration_time = 0;
1394 rd[i].flags = GNUNET_NAMESTORE_RF_NONE;
1395 rd[i].expiration_time = pos->expiration_time;
1398 rh->proc (rh->proc_cls,
1402 GNS_resolver_lookup_cancel (rh);
1407 * Add a result from DNS to the records to be returned to the application.
1409 * @param rh resolution request to extend with a result
1410 * @param expiration_time expiration time for the answer
1411 * @param record_type DNS record type of the answer
1412 * @param data_size number of bytes in @a data
1413 * @param data binary data to return in DNS record
1416 add_dns_result (struct GNS_ResolverHandle *rh,
1417 uint64_t expiration_time,
1418 uint32_t record_type,
1422 struct DnsResult *res;
1424 res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
1425 res->expiration_time = expiration_time;
1426 res->data_size = data_size;
1427 res->record_type = record_type;
1428 res->data = &res[1];
1429 memcpy (&res[1], data, data_size);
1430 GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
1431 rh->dns_result_tail,
1437 * We had to do a DNS lookup. Convert the result (if any) and return
1440 * @param cls closure with the 'struct GNS_ResolverHandle'
1441 * @param addr one of the addresses of the host, NULL for the last address
1442 * @param addrlen length of the address
1445 handle_dns_result (void *cls,
1446 const struct sockaddr *addr,
1449 struct GNS_ResolverHandle *rh = cls;
1450 const struct sockaddr_in *sa4;
1451 const struct sockaddr_in6 *sa6;
1453 rh->std_resolve = NULL;
1456 transmit_lookup_dns_result (rh);
1459 switch (addr->sa_family)
1462 sa4 = (const struct sockaddr_in *) addr;
1464 0 /* expiration time is unknown */,
1465 GNUNET_DNSPARSER_TYPE_A,
1466 sizeof (struct in_addr),
1470 sa6 = (const struct sockaddr_in6 *) addr;
1472 0 /* expiration time is unknown */,
1473 GNUNET_DNSPARSER_TYPE_AAAA,
1474 sizeof (struct in6_addr),
1485 * Task scheduled to continue with the resolution process.
1487 * @param cls the 'struct GNS_ResolverHandle' of the resolution
1488 * @param tc task context
1491 recursive_resolution (void *cls,
1492 const struct GNUNET_SCHEDULER_TaskContext *tc);
1496 * Function called with the result of a DNS resolution.
1498 * @param cls the request handle of the resolution that
1499 * we were attempting to make
1500 * @param rs socket that received the response
1501 * @param dns dns response, never NULL
1502 * @param dns_len number of bytes in 'dns'
1505 dns_result_parser (void *cls,
1506 struct GNUNET_DNSSTUB_RequestSocket *rs,
1507 const struct GNUNET_TUN_DnsHeader *dns,
1510 struct GNS_ResolverHandle *rh = cls;
1511 struct GNUNET_DNSPARSER_Packet *p;
1513 rh->dns_request = NULL;
1514 GNUNET_SCHEDULER_cancel (rh->task_id);
1515 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
1516 p = GNUNET_DNSPARSER_parse ((const char *) dns,
1520 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1521 _("Failed to parse DNS response\n"));
1522 rh->proc (rh->proc_cls, 0, NULL);
1523 GNS_resolver_lookup_cancel (rh);
1527 // Check if the packet is the final answer, or
1528 // just pointing us to another NS or another name (CNAME), or another domain (DNAME);
1529 // then do the right thing (TM) -- possibly using "recursive_dns_resolution".
1531 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1532 _("NOT IMPLEMENTED\n"));
1533 rh->proc (rh->proc_cls, 0, NULL);
1534 GNS_resolver_lookup_cancel (rh);
1537 GNUNET_DNSPARSER_free_packet (p);
1542 * Perform recursive DNS resolution. Asks the given DNS resolver to
1543 * resolve "rh->dns_name", possibly recursively proceeding following
1544 * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
1545 * we find the answer.
1547 * @param rh resolution information
1550 recursive_dns_resolution (struct GNS_ResolverHandle *rh)
1552 struct AuthorityChain *ac;
1554 struct GNUNET_DNSPARSER_Query *query;
1555 struct GNUNET_DNSPARSER_Packet *p;
1557 size_t dns_request_length;
1560 GNUNET_assert (NULL != ac);
1561 GNUNET_assert (GNUNET_NO == ac->gns_authority);
1562 switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
1565 sa_len = sizeof (struct sockaddr_in);
1568 sa_len = sizeof (struct sockaddr_in6);
1572 rh->proc (rh->proc_cls, 0, NULL);
1573 GNS_resolver_lookup_cancel (rh);
1576 query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
1577 query->name = GNUNET_strdup (ac->label);
1578 query->type = rh->record_type;
1579 query->class = GNUNET_TUN_DNS_CLASS_INTERNET;
1580 p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
1583 p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1585 p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
1586 p->flags.recursion_desired = 1;
1588 GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
1591 rh->proc (rh->proc_cls, 0, NULL);
1592 GNS_resolver_lookup_cancel (rh);
1596 rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
1597 (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
1603 rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
1607 GNUNET_free (dns_request);
1608 GNUNET_DNSPARSER_free_packet (p);
1613 * We encountered a CNAME record during our resolution.
1614 * Merge it into our chain.
1616 * @param rh resolution we are performing
1617 * @param cname value of the cname record we got for the current
1618 * authority chain tail
1621 handle_gns_cname_result (struct GNS_ResolverHandle *rh,
1624 // FIXME: not implemented
1626 rh->proc (rh->proc_cls, 0, NULL);
1627 GNS_resolver_lookup_cancel (rh);
1632 * Process a records that were decrypted from a block.
1634 * @param cls closure with the 'struct GNS_ResolverHandle'
1635 * @param rd_count number of entries in @a rd array
1636 * @param rd array of records with data to store
1639 handle_gns_resolution_result (void *cls,
1640 unsigned int rd_count,
1641 const struct GNUNET_NAMESTORE_RecordData *rd)
1643 struct GNS_ResolverHandle *rh = cls;
1644 struct AuthorityChain *ac;
1647 struct sockaddr *sa;
1648 struct sockaddr_in v4;
1649 struct sockaddr_in6 v6;
1653 if (0 == rh->name_resolution_pos)
1655 /* top-level match, are we done yet? */
1656 if ( (rd_count > 0) &&
1657 (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
1658 (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
1660 cname = GNUNET_strndup (rd[0].data,
1662 handle_gns_cname_result (rh,
1664 GNUNET_free (cname);
1667 /* FIXME: if A/AAAA was requested, but we got a VPN
1668 record, we should interact with GNUnet VPN here */
1670 /* yes, we are done, return result */
1671 rh->proc (rh->proc_cls, rd_count, rd);
1672 GNS_resolver_lookup_cancel (rh);
1675 /* need to recurse, check if we can */
1676 for (i=0;i<rd_count;i++)
1678 switch (rd[i].record_type)
1680 case GNUNET_NAMESTORE_TYPE_PKEY:
1681 /* delegation to another zone */
1682 if (sizeof (struct GNUNET_CRYPTO_EccPublicKey) !=
1685 GNUNET_break_op (0);
1686 rh->proc (rh->proc_cls, 0, NULL);
1687 GNS_resolver_lookup_cancel (rh);
1690 /* expand authority chain */
1691 ac = GNUNET_new (struct AuthorityChain);
1693 ac->gns_authority = GNUNET_YES;
1694 memcpy (&ac->authority_info.gns_authority,
1696 sizeof (struct GNUNET_CRYPTO_EccPublicKey));
1697 ac->label = resolver_lookup_get_next_label (rh);
1698 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1702 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1705 case GNUNET_DNSPARSER_TYPE_NS:
1706 /* resolution continues within DNS */
1707 if (GNUNET_DNSPARSER_MAX_NAME_LENGTH < rd[i].data_size)
1709 GNUNET_break_op (0);
1710 rh->proc (rh->proc_cls, 0, NULL);
1711 GNS_resolver_lookup_cancel (rh);
1714 /* find associated A/AAAA record */
1716 for (j=0;j<rd_count;j++)
1718 switch (rd[j].record_type)
1720 case GNUNET_DNSPARSER_TYPE_A:
1721 if (sizeof (struct in_addr) != rd[i].data_size)
1723 GNUNET_break_op (0);
1724 rh->proc (rh->proc_cls, 0, NULL);
1725 GNS_resolver_lookup_cancel (rh);
1728 /* FIXME: might want to check if we support IPv4 here,
1729 and otherwise skip this one and hope we find another */
1730 memset (&v4, 0, sizeof (v4));
1731 sa_len = sizeof (v4);
1732 v4.sin_family = AF_INET;
1733 v4.sin_port = htons (53);
1734 #if HAVE_SOCKADDR_IN_SIN_LEN
1735 v4.sin_len = (u_char) sa_len;
1737 memcpy (&v4.sin_addr,
1739 sizeof (struct in_addr));
1740 sa = (struct sockaddr *) &v4;
1742 case GNUNET_DNSPARSER_TYPE_AAAA:
1743 if (sizeof (struct in6_addr) != rd[i].data_size)
1745 GNUNET_break_op (0);
1746 rh->proc (rh->proc_cls, 0, NULL);
1747 GNS_resolver_lookup_cancel (rh);
1750 /* FIXME: might want to check if we support IPv6 here,
1751 and otherwise skip this one and hope we find another */
1752 memset (&v6, 0, sizeof (v6));
1753 sa_len = sizeof (v6);
1754 v6.sin6_family = AF_INET6;
1755 v6.sin6_port = htons (53);
1756 #if HAVE_SOCKADDR_IN_SIN_LEN
1757 v6.sin6_len = (u_char) sa_len;
1759 memcpy (&v6.sin6_addr,
1761 sizeof (struct in6_addr));
1762 sa = (struct sockaddr *) &v6;
1768 /* expand authority chain */
1769 ac = GNUNET_new (struct AuthorityChain);
1771 strncpy (ac->authority_info.dns_authority.name,
1774 ac->authority_info.dns_authority.name[rd[i].data_size] = '\0';
1775 memcpy (&ac->authority_info.dns_authority.dns_ip,
1778 /* for DNS recursion, the label is the full DNS name,
1779 created from the remainder of the GNS name and the
1780 name in the NS record */
1781 GNUNET_asprintf (&ac->label,
1783 (int) rh->name_resolution_pos,
1785 ac->authority_info.dns_authority.name);
1786 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
1789 if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
1791 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1792 _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
1794 rh->proc (rh->proc_cls, 0, NULL);
1795 GNS_resolver_lookup_cancel (rh);
1799 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
1802 case GNUNET_DNSPARSER_TYPE_CNAME:
1803 cname = GNUNET_strndup (rd[i].data,
1805 handle_gns_cname_result (rh,
1807 GNUNET_free (cname);
1809 /* FIXME: handle DNAME */
1815 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1816 _("GNS lookup recursion failed (no delegation record found)\n"));
1817 rh->proc (rh->proc_cls, 0, NULL);
1818 GNS_resolver_lookup_cancel (rh);
1823 * Function called once the namestore has completed the request for
1826 * @param cls closure with the 'struct GNS_ResolverHandle'
1827 * @param success #GNUNET_OK on success
1828 * @param emsg error message
1831 namestore_cache_continuation (void *cls,
1835 struct GNS_ResolverHandle *rh = cls;
1837 rh->namestore_qe = NULL;
1839 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1840 _("Failed to cache GNS resolution: %s\n"),
1846 * Iterator called on each result obtained for a DHT
1847 * operation that expects a reply
1849 * @param cls closure with the 'struct GNS_ResolverHandle'
1850 * @param exp when will this value expire
1851 * @param key key of the result
1852 * @param get_path peers on reply path (or NULL if not recorded)
1853 * [0] = datastore's first neighbor, [length - 1] = local peer
1854 * @param get_path_length number of entries in get_path
1855 * @param put_path peers on the PUT path (or NULL if not recorded)
1856 * [0] = origin, [length - 1] = datastore
1857 * @param put_path_length number of entries in get_path
1858 * @param type type of the result
1859 * @param size number of bytes in data
1860 * @param data pointer to the result data
1863 handle_dht_response (void *cls,
1864 struct GNUNET_TIME_Absolute exp,
1865 const struct GNUNET_HashCode * key,
1866 const struct GNUNET_PeerIdentity *get_path,
1867 unsigned int get_path_length,
1868 const struct GNUNET_PeerIdentity *put_path,
1869 unsigned int put_path_length,
1870 enum GNUNET_BLOCK_Type type,
1871 size_t size, const void *data)
1873 struct GNS_ResolverHandle *rh = cls;
1874 struct AuthorityChain *ac = rh->ac_tail;
1875 const struct GNUNET_NAMESTORE_Block *block;
1877 GNUNET_DHT_get_stop (rh->get_handle);
1878 rh->get_handle = NULL;
1879 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1880 rh->dht_heap_node = NULL;
1881 if (size < sizeof (struct GNUNET_NAMESTORE_Block))
1883 /* how did this pass DHT block validation!? */
1885 rh->proc (rh->proc_cls, 0, NULL);
1886 GNS_resolver_lookup_cancel (rh);
1891 ntohs (block->purpose.size) +
1892 sizeof (struct GNUNET_CRYPTO_EccPublicKey) +
1893 sizeof (struct GNUNET_CRYPTO_EccSignature))
1895 /* how did this pass DHT block validation!? */
1897 rh->proc (rh->proc_cls, 0, NULL);
1898 GNS_resolver_lookup_cancel (rh);
1902 GNUNET_NAMESTORE_block_decrypt (block,
1903 &ac->authority_info.gns_authority,
1905 &handle_gns_resolution_result,
1908 GNUNET_break_op (0); /* block was ill-formed */
1909 rh->proc (rh->proc_cls, 0, NULL);
1910 GNS_resolver_lookup_cancel (rh);
1913 /* Cache well-formed blocks */
1914 rh->namestore_qe = GNUNET_NAMESTORE_block_cache (namestore_handle,
1916 &namestore_cache_continuation,
1922 * Process a record that was stored in the namestore.
1924 * @param cls closure with the 'struct GNS_ResolverHandle'
1925 * @param block block that was stored in the namestore
1928 handle_namestore_block_response (void *cls,
1929 const struct GNUNET_NAMESTORE_Block *block)
1931 struct GNS_ResolverHandle *rh = cls;
1932 struct GNS_ResolverHandle *rx;
1933 struct AuthorityChain *ac = rh->ac_tail;
1934 const char *label = ac->label;
1935 const struct GNUNET_CRYPTO_EccPublicKey *auth = &ac->authority_info.gns_authority;
1936 struct GNUNET_HashCode query;
1938 GNUNET_NAMESTORE_query_from_public_key (auth,
1941 rh->namestore_qe = NULL;
1944 /* Namestore knows nothing; try DHT lookup */
1945 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1946 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1948 DHT_GNS_REPLICATION_LEVEL,
1949 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1951 &handle_dht_response, rh);
1952 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1954 GNUNET_TIME_absolute_get ().abs_value_us);
1955 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
1957 /* fail longest-standing DHT request */
1958 rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
1959 rx->proc (rx->proc_cls, 0, NULL);
1960 GNS_resolver_lookup_cancel (rx);
1965 GNUNET_NAMESTORE_block_decrypt (block,
1968 &handle_gns_resolution_result,
1971 GNUNET_break_op (0); /* block was ill-formed */
1972 rh->proc (rh->proc_cls, 0, NULL);
1973 GNS_resolver_lookup_cancel (rh);
1980 * Lookup tail of our authority chain in the namestore.
1982 * @param rh query we are processing
1985 recursive_gns_resolution_namestore (struct GNS_ResolverHandle *rh)
1987 struct AuthorityChain *ac = rh->ac_tail;
1988 struct GNUNET_HashCode query;
1990 GNUNET_NAMESTORE_query_from_public_key (&ac->authority_info.gns_authority,
1993 rh->namestore_qe = GNUNET_NAMESTORE_lookup_block (namestore_handle,
1995 &handle_namestore_block_response,
2001 * Task scheduled to continue with the resolution process.
2003 * @param cls the 'struct GNS_ResolverHandle' of the resolution
2004 * @param tc task context
2007 recursive_resolution (void *cls,
2008 const struct GNUNET_SCHEDULER_TaskContext *tc)
2010 struct GNS_ResolverHandle *rh = cls;
2012 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
2013 if (MAX_RECURSION < rh->loop_limiter++)
2015 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2016 "Encountered unbounded recursion resolving `%s'\n",
2018 rh->proc (rh->proc_cls, 0, NULL);
2019 GNS_resolver_lookup_cancel (rh);
2022 if (GNUNET_YES == rh->ac_tail->gns_authority)
2023 recursive_gns_resolution_namestore (rh);
2025 recursive_dns_resolution (rh);
2030 * Lookup of a record in a specific zone calls lookup result processor
2033 * @param zone the zone to perform the lookup in
2034 * @param record_type the record type to look up
2035 * @param name the name to look up
2036 * @param shorten_key a private key for use with PSEU import (can be NULL)
2037 * @param only_cached GNUNET_NO to only check locally not DHT for performance
2038 * @param proc the processor to call on result
2039 * @param proc_cls the closure to pass to @a proc
2040 * @return handle to cancel operation
2042 struct GNS_ResolverHandle *
2043 GNS_resolver_lookup (const struct GNUNET_CRYPTO_EccPublicKey *zone,
2044 uint32_t record_type,
2046 const struct GNUNET_CRYPTO_EccPrivateKey *shorten_key,
2048 GNS_ResultProcessor proc, void *proc_cls)
2050 struct GNS_ResolverHandle *rh;
2051 struct AuthorityChain *ac;
2056 rh = GNUNET_new (struct GNS_ResolverHandle);
2057 GNUNET_CONTAINER_DLL_insert (rlh_head,
2060 rh->authority_zone = *zone;
2062 rh->proc_cls = proc_cls;
2063 rh->only_cached = only_cached;
2064 rh->record_type = record_type;
2065 rh->name = GNUNET_strdup (name);
2066 rh->name_resolution_pos = strlen (name);
2067 if (NULL != shorten_key)
2069 rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
2070 *rh->shorten_key = *shorten_key;
2073 if ( ( (GNUNET_YES == is_canonical (name)) &&
2074 (0 != strcmp (GNUNET_GNS_TLD, name)) ) ||
2075 ( (GNUNET_YES != is_gnu_tld (name)) &&
2076 (GNUNET_YES != is_zkey_tld (name)) ) )
2078 /* use standard DNS lookup */
2081 switch (record_type)
2083 case GNUNET_DNSPARSER_TYPE_A:
2086 case GNUNET_DNSPARSER_TYPE_AAAA:
2093 rh->std_resolve = GNUNET_RESOLVER_ip_get (name,
2100 if (is_zkey_tld (name))
2102 /* Name ends with ".zkey", try to replace authority zone with zkey
2104 GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
2105 x = resolver_lookup_get_next_label (rh); /* will return 'x' coordinate */
2106 y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
2107 GNUNET_asprintf (&pkey,
2113 GNUNET_CRYPTO_ecc_public_key_from_string (pkey,
2115 &rh->authority_zone)) )
2117 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2118 _("Hostname `%s' is not well-formed, resolution fails\n"),
2120 rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
2122 GNUNET_free_non_null (x);
2123 GNUNET_free_non_null (y);
2128 /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
2129 GNUNET_free (resolver_lookup_get_next_label (rh));
2131 ac = GNUNET_new (struct AuthorityChain);
2133 ac->label = resolver_lookup_get_next_label (rh);
2134 if (NULL == ac->label)
2135 /* name was just "gnu", so we default to label '+' */
2136 ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
2137 ac->gns_authority = GNUNET_YES;
2138 ac->authority_info.gns_authority = rh->authority_zone;
2139 GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
2142 rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
2149 * Cancel active resolution (i.e. client disconnected).
2151 * @param rh resolution to abort
2154 GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
2156 struct DnsResult *dr;
2157 struct AuthorityChain *ac;
2159 GNUNET_CONTAINER_DLL_remove (rlh_head,
2162 while (NULL != (ac = rh->ac_head))
2164 GNUNET_CONTAINER_DLL_remove (rh->ac_head,
2167 GNUNET_free (ac->label);
2170 if (GNUNET_SCHEDULER_NO_TASK != rh->task_id)
2172 GNUNET_SCHEDULER_cancel (rh->task_id);
2173 rh->task_id = GNUNET_SCHEDULER_NO_TASK;
2175 if (NULL != rh->get_handle)
2177 GNUNET_DHT_get_stop (rh->get_handle);
2178 rh->get_handle = NULL;
2180 if (NULL != rh->dht_heap_node)
2182 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
2183 rh->dht_heap_node = NULL;
2185 if (NULL != rh->dns_request)
2187 GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
2188 rh->dns_request = NULL;
2190 if (NULL != rh->namestore_qe)
2192 GNUNET_NAMESTORE_cancel (rh->namestore_qe);
2193 rh->namestore_qe = NULL;
2195 if (NULL != rh->std_resolve)
2197 GNUNET_RESOLVER_request_cancel (rh->std_resolve);
2198 rh->std_resolve = NULL;
2200 while (NULL != (dr = rh->dns_result_head))
2202 GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
2203 rh->dns_result_tail,
2207 GNUNET_free_non_null (rh->shorten_key);
2208 GNUNET_free (rh->name);
2213 /* ***************** Resolver initialization ********************* */
2217 * Initialize the resolver
2219 * @param nh the namestore handle
2220 * @param dht the dht handle
2221 * @param c configuration handle
2222 * @param max_bg_queries maximum number of parallel background queries in dht
2225 GNS_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
2226 struct GNUNET_DHT_Handle *dht,
2227 const struct GNUNET_CONFIGURATION_Handle *c,
2228 unsigned long long max_bg_queries)
2233 namestore_handle = nh;
2236 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2237 max_allowed_background_queries = max_bg_queries;
2239 GNUNET_CONFIGURATION_get_value_string (c,
2244 /* user did not specify DNS resolver, use 8.8.8.8 */
2245 dns_ip = GNUNET_strdup ("8.8.8.8");
2247 dns_handle = GNUNET_DNSSTUB_start (dns_ip);
2248 GNUNET_free (dns_ip);
2256 GNS_resolver_done ()
2258 struct GNS_ResolverHandle *rh;
2260 /* abort active resolutions */
2261 while (NULL != (rh = rlh_head))
2263 rh->proc (rh->proc_cls, 0, NULL);
2264 GNS_resolver_lookup_cancel (rh);
2266 /* abort active shorten operations */
2267 while (NULL != gph_head)
2268 free_get_pseu_authority_handle (gph_head);
2269 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
2270 dht_lookup_heap = NULL;
2271 GNUNET_DNSSTUB_stop (dns_handle);
2276 /* *************** common helper functions (do not really belong here) *********** */
2279 * Checks if "name" ends in ".tld"
2281 * @param name the name to check
2282 * @param tld the TLD to check for
2283 * @return GNUNET_YES or GNUNET_NO
2286 is_tld (const char* name, const char* tld)
2290 if (strlen (name) <= strlen (tld))
2292 offset = strlen (name) - strlen (tld);
2293 if (0 != strcmp (name + offset, tld))
2299 /* end of gnunet-service-gns_resolver.c */