2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 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.
24 * @file gns/gnunet-service-gns_resolver.c
25 * @brief GNUnet GNS resolver logic
26 * @author Martin Schanzenbach
29 #include "gnunet_util_lib.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_dns_service.h"
32 #include "gnunet_dht_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_vpn_service.h"
35 #include "gnunet_dns_service.h"
36 #include "gnunet_resolver_service.h"
37 #include "gnunet_dnsparser_lib.h"
38 #include "../dns/dnsparser.h"
39 #include "gnunet_gns_service.h"
40 #include "block_gns.h"
42 #include "gnunet-service-gns_resolver.h"
44 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
45 #define DHT_GNS_REPLICATION_LEVEL 5
46 #define MAX_DNS_LABEL_LENGTH 63
50 * Our handle to the namestore service
52 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
55 * Our handle to the vpn service
57 static struct GNUNET_VPN_Handle *vpn_handle;
60 * Resolver handle to the dht
62 static struct GNUNET_DHT_Handle *dht_handle;
65 * Heap for parallel DHT lookups
67 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
70 * Heap for namestore queues
72 static struct GNUNET_CONTAINER_Heap *ns_task_heap;
75 * Maximum amount of parallel queries in background
77 static unsigned long long max_allowed_background_queries;
80 * Maximum amount of parallel namestore tasks in background
82 static unsigned long long max_allowed_ns_tasks;
85 * Wheather or not to ignore pending records
87 static int ignore_pending_records;
92 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
95 * Background shortening handles
97 static struct GetPseuAuthorityHandle *gph_head;
100 * Background shortening handles
102 static struct GetPseuAuthorityHandle *gph_tail;
105 * Resolver lookup list
107 static struct ResolverHandle *rlh_head;
110 * Resolver lookup list
112 static struct ResolverHandle *rlh_tail;
115 * Resolver shorten list
117 static struct ResolverHandle *nsh_head;
120 * Resolver shorten list
122 static struct ResolverHandle *nsh_tail;
125 * Resolver get auth list
127 static struct ResolverHandle *nah_head;
130 * Resolver get auth list
132 static struct ResolverHandle *nah_tail;
135 * Global configuration.
137 static const struct GNUNET_CONFIGURATION_Handle *cfg;
140 * a resolution identifier pool variable
142 * This is a non critical identifier useful for debugging
144 static unsigned long long rid = 0;
154 if (NULL == strstr (name, "._"))
157 ndup = GNUNET_strdup (name);
160 if (NULL == strtok (NULL, "."))
163 if (NULL == strtok (NULL, "."))
166 if (NULL != strtok (NULL, "."))
175 * Determine if this name is canonical.
177 * a.b.gnunet = not canonical
180 * @param name the name to test
181 * @return GNUNET_YES if canonical
184 is_canonical (char* name)
189 ndup = GNUNET_strdup (name);
192 for (tok = strtok (NULL, "."); tok != NULL; tok = strtok (NULL, "."))
208 * Callback that shortens authorities
210 * @param gph the handle containing the name to shorten
213 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
217 create_pseu_cont (void* cls, int32_t success, const char* emsg)
219 //FIXME do sth with error
220 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
221 struct AuthorityChain *iter;
223 gph->namestore_task = NULL;
226 iter = gph->ahead->next;
227 GNUNET_free (gph->ahead);
229 } while (iter != NULL);
230 GNUNET_CRYPTO_rsa_key_free (gph->key);
231 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
237 * Namestore calls this function if we have record for this name.
238 * (or with rd_count=0 to indicate no matches)
240 * @param cls the pending query
241 * @param key the key of the zone we did the lookup
242 * @param expiration expiration date of the namestore entry
243 * @param name the name for which we need an authority
244 * @param rd_count the number of records with 'name'
245 * @param rd the record data
246 * @param signature the signature of the authority for the record data
249 process_pseu_lookup_ns (void* cls,
250 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
251 struct GNUNET_TIME_Absolute expiration,
252 const char *name, unsigned int rd_count,
253 const struct GNUNET_NAMESTORE_RecordData *rd,
254 const struct GNUNET_CRYPTO_RsaSignature *signature)
256 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
257 struct GNUNET_NAMESTORE_RecordData new_pkey;
258 struct AuthorityChain *iter;
260 gph->namestore_task = NULL;
263 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
264 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
265 if (0 == strcmp (gph->name, name))
267 if (gph->ahead->next != NULL)
269 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
273 iter = gph->ahead->next;
274 GNUNET_free (gph->ahead);
276 shorten_authority_chain (gph);
284 iter = gph->ahead->next;
285 GNUNET_free (gph->ahead);
287 } while (iter != NULL);
288 GNUNET_CRYPTO_rsa_key_free (gph->key);
289 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "GNS_AUTO_PSEU: Trying delegated name %s\n", gph->name);
296 memcpy (gph->test_name, gph->name, strlen (gph->name)+1);
297 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
300 GNUNET_NAMESTORE_TYPE_ANY,
301 &process_pseu_lookup_ns,
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
310 new_pkey.expiration_time = UINT64_MAX;
311 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
312 new_pkey.data = &gph->ahead->zone;
313 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
314 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
315 | GNUNET_NAMESTORE_RF_PRIVATE
316 | GNUNET_NAMESTORE_RF_PENDING;
317 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
321 &create_pseu_cont, //cont
326 * process result of a dht pseu lookup
328 * @param gph the handle
329 * @param name the pseu result or NULL
332 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
336 memcpy (gph->test_name, gph->ahead->name, strlen (gph->ahead->name)+1);
340 memcpy (gph->test_name, name, strlen(name)+1);
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
348 * Check for collision
350 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
353 GNUNET_NAMESTORE_TYPE_ANY,
354 &process_pseu_lookup_ns,
359 * Handle timeout for dht request
361 * @param cls the request handle as closure
362 * @param tc the task context
365 handle_auth_discovery_timeout(void *cls,
366 const struct GNUNET_SCHEDULER_TaskContext *tc)
368 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
369 struct AuthorityChain *iter;
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
373 GNUNET_DHT_get_stop (gph->get_handle);
374 gph->get_handle = NULL;
376 if (gph->ahead->next != NULL)
378 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
382 iter = gph->ahead->next;
383 GNUNET_free (gph->ahead);
385 shorten_authority_chain (gph);
390 process_pseu_result (gph, NULL);
394 * Function called when we find a PSEU entry in the DHT
396 * @param cls the request handle
397 * @param exp lifetime
398 * @param key the key the record was stored under
399 * @param get_path get path
400 * @param get_path_length get path length
401 * @param put_path put path
402 * @param put_path_length put path length
403 * @param type the block type
404 * @param size the size of the record
405 * @param data the record data
408 process_auth_discovery_dht_result(void* cls,
409 struct GNUNET_TIME_Absolute exp,
410 const struct GNUNET_HashCode * key,
411 const struct GNUNET_PeerIdentity *get_path,
412 unsigned int get_path_length,
413 const struct GNUNET_PeerIdentity *put_path,
414 unsigned int put_path_length,
415 enum GNUNET_BLOCK_Type type,
416 size_t size, const void *data)
418 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
419 struct AuthorityChain *iter;
420 struct GNSNameRecordBlock *nrb;
421 char* rd_data = (char*)data;
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
431 /* stop lookup and timeout task */
432 GNUNET_DHT_get_stop (gph->get_handle);
433 gph->get_handle = NULL;
434 GNUNET_SCHEDULER_cancel (gph->timeout);
438 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
439 "GNS_GET_AUTH: got dht result null!\n", size);
443 iter = gph->ahead->next;
444 GNUNET_free (gph->ahead);
446 } while (iter != NULL);
447 GNUNET_CRYPTO_rsa_key_free (gph->key);
448 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
453 nrb = (struct GNSNameRecordBlock*)data;
457 nrb = (struct GNSNameRecordBlock*)data;
459 name = (char*)&nrb[1];
460 num_records = ntohl (nrb->rd_count);
462 struct GNUNET_NAMESTORE_RecordData rd[num_records];
464 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
465 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
467 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
473 "GNS_GET_AUTH: Error deserializing data!\n");
477 for (i=0; i < num_records; i++)
479 if ((strcmp (name, "+") == 0) &&
480 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
483 process_pseu_result (gph, (char*)rd[i].data);
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in dht!\n");
492 if (gph->ahead->next != NULL)
494 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
498 iter = gph->ahead->next;
499 GNUNET_free (gph->ahead);
501 shorten_authority_chain (gph);
505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
506 "GNS_GET_AUTH: finished shorten, no results!\n");
507 process_pseu_result (gph, NULL);
511 * Process PSEU discovery for shorten via namestore
513 * @param cls the GetPseuAuthorityHandle
514 * @param key the public key
515 * @param expiration recorddata expiration
516 * @param name the looked up name
517 * @param rd_count number of records in set
518 * @param rd record data
519 * @param signature the signature
522 process_auth_discovery_ns_result(void* cls,
523 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
524 struct GNUNET_TIME_Absolute expiration,
526 unsigned int rd_count,
527 const struct GNUNET_NAMESTORE_RecordData *rd,
528 const struct GNUNET_CRYPTO_RsaSignature *signature)
531 struct GNUNET_CRYPTO_ShortHashCode name_hash;
532 struct GNUNET_HashCode lookup_key;
533 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
534 struct GNUNET_HashCode name_hash_double;
535 struct GNUNET_HashCode zone_hash_double;
537 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
538 struct AuthorityChain *iter;
540 gph->namestore_task = NULL;
547 GNUNET_CRYPTO_short_hash ("+", strlen ("+"), &name_hash);
548 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
549 GNUNET_CRYPTO_short_hash_double (&gph->ahead->zone, &zone_hash_double);
550 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
551 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
555 "+", (char*)&lookup_key_string);
557 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
558 &handle_auth_discovery_timeout, gph);
560 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
562 GNUNET_assert (gph->get_handle == NULL);
564 gph->get_handle = GNUNET_DHT_get_start(dht_handle,
565 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
567 DHT_GNS_REPLICATION_LEVEL,
571 &process_auth_discovery_dht_result,
576 for (i=0; i < rd_count; i++)
578 if ((strcmp (name, "+") == 0) &&
579 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
582 process_pseu_result (gph, (char*)rd[i].data);
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
589 if (gph->ahead->next != NULL)
591 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
595 iter = gph->ahead->next;
596 GNUNET_free (gph->ahead);
598 shorten_authority_chain (gph);
603 process_pseu_result (gph, NULL);
607 * Callback called by namestore for a zone to name
610 * @param cls the closure
611 * @param zone_key the zone we queried
612 * @param expire the expiration time of the name
613 * @param name the name found or NULL
614 * @param rd_len number of records for the name
615 * @param rd the record data (PKEY) for the name
616 * @param signature the signature for the record data
619 process_zone_to_name_discover (void *cls,
620 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
621 struct GNUNET_TIME_Absolute expire,
624 const struct GNUNET_NAMESTORE_RecordData *rd,
625 const struct GNUNET_CRYPTO_RsaSignature *signature)
627 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
628 struct AuthorityChain *iter;
630 gph->namestore_task = NULL;
631 /* we found a match in our own zone */
634 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
635 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
640 iter = gph->ahead->next;
641 GNUNET_free (gph->ahead);
643 } while (iter != NULL);
644 GNUNET_CRYPTO_rsa_key_free (gph->key);
645 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
651 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
654 GNUNET_GNS_RECORD_PSEU,
655 &process_auth_discovery_ns_result,
663 * Callback that shortens authorities
665 * @param gph the handle to the shorten request
668 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
672 "GNS_AUTO_PSEU: New authority %s discovered\n",
675 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
678 &process_zone_to_name_discover,
684 start_shorten (struct AuthorityChain *atail,
685 struct GNUNET_CRYPTO_RsaPrivateKey *key)
687 struct AuthorityChain *new_head = NULL;
688 struct AuthorityChain *new_tail = NULL;
689 struct AuthorityChain *iter;
690 struct AuthorityChain *acopy;
691 struct GetPseuAuthorityHandle *gph;
692 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
693 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
695 /* First copy the authority chain in reverse order */
696 for (iter = atail; iter != NULL; iter = iter->prev)
698 acopy = GNUNET_malloc (sizeof (struct AuthorityChain));
699 memcpy (acopy, iter, sizeof (struct AuthorityChain));
702 GNUNET_CONTAINER_DLL_insert (new_head, new_tail, acopy);
705 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
707 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
708 pb_key = GNUNET_CRYPTO_rsa_encode_key (key);
709 gph->key = GNUNET_CRYPTO_rsa_decode_key ((char*)pb_key, ntohs (pb_key->len));
710 //gph->key = key;//GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
711 //memcpy (gph->key, key, sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
713 GNUNET_CRYPTO_short_hash (&pkey,
714 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
716 gph->ahead = new_head;
718 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
720 shorten_authority_chain (gph);
724 * Initialize the resolver
726 * @param nh the namestore handle
727 * @param dh the dht handle
728 * @param lz the local zone's hash
729 * @param c configuration handle
730 * @param max_bg_queries maximum number of parallel background queries in dht
731 * @param ignore_pending ignore records that still require user confirmation
733 * @return GNUNET_OK on success
736 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
737 struct GNUNET_DHT_Handle *dh,
738 struct GNUNET_CRYPTO_ShortHashCode lz,
739 const struct GNUNET_CONFIGURATION_Handle *c,
740 unsigned long long max_bg_queries,
744 namestore_handle = nh;
748 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
750 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
751 max_allowed_background_queries = max_bg_queries;
752 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
754 ignore_pending_records = ignore_pending;
765 GNUNET_RESOLVER_connect (cfg);
768 if ((namestore_handle != NULL) && (dht_handle != NULL))
773 return GNUNET_SYSERR;
780 * @param cls closure to iterator
781 * @param node heap nodes
782 * @param element the namestorebgtask
783 * @param cost heap cost
784 * @return always GNUNET_YES
787 cleanup_pending_ns_tasks(void* cls,
788 struct GNUNET_CONTAINER_HeapNode *node,
790 GNUNET_CONTAINER_HeapCostType cost)
792 struct NamestoreBGTask *nbg = (struct NamestoreBGTask *)element;
793 ResolverCleanupContinuation cont = cls;
795 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
796 "GNS_CLEANUP: Terminating ns task\n");
797 GNUNET_NAMESTORE_cancel (nbg->qe);
799 GNUNET_CONTAINER_heap_remove_node(node);
801 if (GNUNET_CONTAINER_heap_get_size(ns_task_heap) == 0)
811 * @param rh resolver handle
812 * @param rlh record lookup handle
813 * @param rd_count number of results
817 finish_lookup (struct ResolverHandle *rh,
818 struct RecordLookupHandle* rlh,
819 unsigned int rd_count,
820 const struct GNUNET_NAMESTORE_RecordData *rd);
824 * Cleanup background lookups FIXME get rid of this??
826 * @param cls closure to iterator
827 * @param node heap nodes
828 * @param element the resolver handle
829 * @param cost heap cost
830 * @return always GNUNET_YES
833 cleanup_pending_background_queries (void* cls,
834 struct GNUNET_CONTAINER_HeapNode *node,
836 GNUNET_CONTAINER_HeapCostType cost)
838 struct ResolverHandle *rh = (struct ResolverHandle *)element;
839 ResolverCleanupContinuation cont = cls;
841 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
842 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
845 //finish_lookup (rh, rh->proc_cls, 0, NULL);
846 //rh->get_handle = NULL;
847 //rh->proc(rh->proc_cls, rh, 0, NULL);
849 GNUNET_CONTAINER_heap_remove_node(node);
851 if (GNUNET_CONTAINER_heap_get_size(dht_lookup_heap) == 0)
853 if (GNUNET_CONTAINER_heap_get_size(ns_task_heap) == 0)
857 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
858 &cleanup_pending_ns_tasks,
869 * Helper function to free resolver handle
871 * @param rh the handle to free
874 free_resolver_handle (struct ResolverHandle* rh)
876 struct AuthorityChain *ac;
877 struct AuthorityChain *ac_next;
882 ac = rh->authority_chain_head;
891 if (NULL != rh->get_handle)
892 GNUNET_DHT_get_stop (rh->get_handle);
894 if (NULL != rh->dns_raw_packet)
895 GNUNET_free (rh->dns_raw_packet);
897 if (NULL != rh->namestore_task)
898 GNUNET_NAMESTORE_cancel (rh->namestore_task);
899 rh->namestore_task = NULL;
901 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
902 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
904 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
905 GNUNET_SCHEDULER_cancel (rh->timeout_task);
907 if (NULL != rh->dns_sock)
908 GNUNET_NETWORK_socket_close (rh->dns_sock);
909 if (NULL != rh->dns_resolver_handle)
910 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
912 if (NULL != rh->rd.data)
913 GNUNET_free ((void*)(rh->rd.data));
921 * @param rh resolver handle
922 * @param nsh name shorten handle
925 finish_shorten (struct ResolverHandle *rh,
926 struct NameShortenHandle *nsh);
930 * @param rh resolver handle
931 * @param nah get name authority handle
934 finish_get_auth (struct ResolverHandle *rh,
935 struct GetNameAuthorityHandle* rlh);
940 gns_resolver_cleanup (ResolverCleanupContinuation cont)
943 struct GetPseuAuthorityHandle *tmp;
944 struct AuthorityChain *iter;
948 for (tmp = gph_head; tmp != NULL; tmp = gph_head)
950 if (tmp->get_handle != NULL)
951 GNUNET_DHT_get_stop (tmp->get_handle);
952 tmp->get_handle = NULL;
953 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
954 GNUNET_SCHEDULER_cancel (tmp->timeout);
955 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
957 if (NULL != tmp->namestore_task)
958 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
959 tmp->namestore_task = NULL;
964 iter = tmp->ahead->next;
965 GNUNET_free (tmp->ahead);
967 } while (iter != NULL);
969 GNUNET_CRYPTO_rsa_key_free (tmp->key);
970 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, tmp);
974 while (NULL != rlh_head)
976 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
979 while (NULL != nsh_head)
981 finish_shorten (nsh_head, nsh_head->proc_cls);
984 while (NULL != nah_head)
986 finish_get_auth (nah_head, nah_head->proc_cls);
989 s = GNUNET_CONTAINER_heap_get_size(dht_lookup_heap);
990 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
991 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
993 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
994 &cleanup_pending_background_queries,
996 else if (0 != GNUNET_CONTAINER_heap_get_size(ns_task_heap))
998 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
999 &cleanup_pending_ns_tasks,
1010 * Callback when record data is put into namestore
1012 * @param cls the closure
1013 * @param success GNUNET_OK on success
1014 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
1017 on_namestore_record_put_result(void *cls,
1021 struct NamestoreBGTask *nbg = cls;
1023 GNUNET_CONTAINER_heap_remove_node (nbg->node);
1026 if (GNUNET_NO == success)
1028 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1029 "GNS_NS: records already in namestore\n");
1032 else if (GNUNET_YES == success)
1034 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1035 "GNS_NS: records successfully put in namestore\n");
1039 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1040 "GNS_NS: Error putting records into namestore: %s\n", emsg);
1044 handle_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1046 struct ResolverHandle *rh = cls;
1048 if (rh->timeout_cont)
1049 rh->timeout_cont(rh->timeout_cont_cls, tc);
1053 * Processor for background lookups in the DHT
1055 * @param cls closure (NULL)
1056 * @param rd_count number of records found (not 0)
1057 * @param rd record data
1060 background_lookup_result_processor(void *cls,
1062 const struct GNUNET_NAMESTORE_RecordData *rd)
1064 //We could do sth verbose/more useful here but it doesn't make any difference
1065 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1066 "GNS_BG: background dht lookup finished. (%d results)\n",
1071 * Handle timeout for DHT requests
1073 * @param cls the request handle as closure
1074 * @param tc the task context
1077 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1079 struct ResolverHandle *rh = cls;
1080 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1081 char new_name[MAX_DNS_NAME_LENGTH];
1083 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1084 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
1085 rh->id, rh->name, rh->timeout.rel_value);
1087 * Start resolution in bg
1089 //strcpy(new_name, rh->name);
1090 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1091 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
1092 rh->name, GNUNET_GNS_TLD);
1094 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1095 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
1096 rh->id, new_name, rlh->record_type);
1098 gns_resolver_lookup_record(rh->authority,
1099 rh->private_local_zone,
1103 GNUNET_TIME_UNIT_FOREVER_REL,
1105 &background_lookup_result_processor,
1107 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1109 GNUNET_DHT_get_stop (rh->get_handle);
1110 rh->get_handle = NULL;
1111 rh->proc(rh->proc_cls, rh, 0, NULL);
1116 * Function called when we get a result from the dht
1117 * for our record query
1119 * @param cls the request handle
1120 * @param exp lifetime
1121 * @param key the key the record was stored under
1122 * @param get_path get path
1123 * @param get_path_length get path length
1124 * @param put_path put path
1125 * @param put_path_length put path length
1126 * @param type the block type
1127 * @param size the size of the record
1128 * @param data the record data
1131 process_record_result_dht(void* cls,
1132 struct GNUNET_TIME_Absolute exp,
1133 const struct GNUNET_HashCode * key,
1134 const struct GNUNET_PeerIdentity *get_path,
1135 unsigned int get_path_length,
1136 const struct GNUNET_PeerIdentity *put_path,
1137 unsigned int put_path_length,
1138 enum GNUNET_BLOCK_Type type,
1139 size_t size, const void *data)
1141 struct ResolverHandle *rh;
1142 struct RecordLookupHandle *rlh;
1143 struct GNSNameRecordBlock *nrb;
1144 uint32_t num_records;
1146 char* rd_data = (char*)data;
1150 rh = (struct ResolverHandle *)cls;
1151 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1152 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
1154 //FIXME maybe check expiration here, check block type
1157 rlh = (struct RecordLookupHandle *) rh->proc_cls;
1158 nrb = (struct GNSNameRecordBlock*)data;
1160 /* stop lookup and timeout task */
1161 GNUNET_DHT_get_stop (rh->get_handle);
1162 rh->get_handle = NULL;
1164 if (rh->dht_heap_node != NULL)
1166 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
1167 rh->dht_heap_node = NULL;
1170 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1172 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1173 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1176 rh->get_handle = NULL;
1177 name = (char*)&nrb[1];
1178 num_records = ntohl(nrb->rd_count);
1180 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1181 struct NamestoreBGTask *ns_heap_root;
1182 struct NamestoreBGTask *namestore_bg_task;
1184 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1185 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
1187 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1192 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1193 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1197 for (i=0; i<num_records; i++)
1199 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1200 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1201 rh->id, name, rh->name);
1202 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1203 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1204 rh->id, rd[i].record_type, rlh->record_type);
1205 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1206 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1207 rh->id, rd[i].data_size);
1208 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1209 "GNS_PHASE_REC-%llu: Got flag %d\n",
1210 rh->id, rd[i].flags);
1212 if ((strcmp(name, rh->name) == 0) &&
1213 (rd[i].record_type == rlh->record_type))
1221 * FIXME check pubkey against existing key in namestore?
1222 * https://gnunet.org/bugs/view.php?id=2179
1224 if (max_allowed_ns_tasks <=
1225 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1227 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1228 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1230 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1231 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1235 /* Save to namestore */
1236 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1237 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1244 &on_namestore_record_put_result, //cont
1247 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1249 GNUNET_TIME_absolute_get().abs_value);
1253 rh->proc(rh->proc_cls, rh, num_records, rd);
1255 rh->proc(rh->proc_cls, rh, 0, NULL);
1262 * Start DHT lookup for a (name -> query->record_type) record in
1263 * rh->authority's zone
1265 * @param rh the pending gns query context
1268 resolve_record_dht (struct ResolverHandle *rh)
1271 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1272 struct GNUNET_HashCode lookup_key;
1273 struct GNUNET_HashCode name_hash_double;
1274 struct GNUNET_HashCode zone_hash_double;
1275 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
1276 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1277 struct ResolverHandle *rh_heap_root;
1279 GNUNET_CRYPTO_short_hash(rh->name, strlen(rh->name), &name_hash);
1280 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1281 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
1282 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
1283 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
1285 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1286 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1287 rh->id, rh->name, (char*)&lookup_key_string);
1289 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1290 rh->dht_heap_node = NULL;
1292 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1295 * Update timeout if necessary
1297 if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
1300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1301 "GNS_PHASE_REC-%llu: Adjusting timeout\n", rh->id);
1303 * Set timeout for authority lookup phase to 1/2
1305 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
1306 GNUNET_TIME_relative_divide(rh->timeout, 2),
1307 &handle_lookup_timeout,
1310 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1311 // &dht_lookup_timeout,
1313 rh->timeout_cont = &dht_lookup_timeout;
1314 rh->timeout_cont_cls = rh;
1318 if (max_allowed_background_queries <=
1319 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1321 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1322 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
1323 rh_heap_root->get_handle = NULL;
1324 rh_heap_root->dht_heap_node = NULL;
1326 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1327 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1328 rh->id, rh_heap_root->name);
1329 rh_heap_root->proc(rh_heap_root->proc_cls,
1334 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1336 GNUNET_TIME_absolute_get().abs_value);
1339 xquery = htonl(rlh->record_type);
1341 GNUNET_assert(rh->get_handle == NULL);
1342 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1343 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1345 DHT_GNS_REPLICATION_LEVEL,
1349 &process_record_result_dht,
1356 * Namestore calls this function if we have record for this name.
1357 * (or with rd_count=0 to indicate no matches)
1359 * @param cls the pending query
1360 * @param key the key of the zone we did the lookup
1361 * @param expiration expiration date of the namestore entry
1362 * @param name the name for which we need an authority
1363 * @param rd_count the number of records with 'name'
1364 * @param rd the record data
1365 * @param signature the signature of the authority for the record data
1368 process_record_result_ns (void* cls,
1369 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1370 struct GNUNET_TIME_Absolute expiration,
1371 const char *name, unsigned int rd_count,
1372 const struct GNUNET_NAMESTORE_RecordData *rd,
1373 const struct GNUNET_CRYPTO_RsaSignature *signature)
1375 struct ResolverHandle *rh;
1376 struct RecordLookupHandle *rlh;
1377 struct GNUNET_TIME_Relative remaining_time;
1378 struct GNUNET_CRYPTO_ShortHashCode zone;
1379 struct GNUNET_TIME_Absolute et;
1382 rh = (struct ResolverHandle *) cls;
1383 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1385 rh->namestore_task = NULL;
1386 GNUNET_CRYPTO_short_hash(key,
1387 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1389 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1397 rh->status |= RSL_RECORD_EXISTS;
1399 if (remaining_time.rel_value == 0)
1401 rh->status |= RSL_RECORD_EXPIRED;
1408 * Lookup terminated and no results
1410 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1411 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1414 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1415 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1418 * Our zone and no result? Cannot resolve TT
1420 rh->proc(rh->proc_cls, rh, 0, NULL);
1426 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1427 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1429 for (i=0; i<rd_count;i++)
1431 if (rd[i].record_type != rlh->record_type)
1434 if (ignore_pending_records &&
1435 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1437 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1438 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1443 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1444 et.abs_value = rd[i].expiration_time;
1445 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
1448 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1449 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1459 if (rh->answered == 0)
1461 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1462 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1463 rh->proc(rh->proc_cls, rh, 0, NULL);
1467 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1468 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1469 rh->id, rh->answered, rd_count);
1471 rh->proc(rh->proc_cls, rh, rd_count, rd);
1477 * VPN redirect result callback
1479 * @param cls the resolver handle
1480 * @param af the requested address family
1481 * @param address in_addr(6) respectively
1484 process_record_result_vpn (void* cls, int af, const void *address)
1486 struct ResolverHandle *rh = cls;
1487 struct RecordLookupHandle *rlh;
1488 struct GNUNET_NAMESTORE_RecordData rd;
1490 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1492 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1493 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1497 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1498 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1500 if (rlh->record_type != GNUNET_GNS_RECORD_A)
1502 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1503 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1505 rh->proc (rh->proc_cls, rh, 0, NULL);
1508 rd.record_type = GNUNET_GNS_RECORD_A;
1509 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1511 rd.data_size = sizeof (struct in_addr);
1513 rh->proc (rh->proc_cls, rh, 1, &rd);
1516 else if (af == AF_INET6)
1518 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1519 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1521 if (rlh->record_type != GNUNET_GNS_RECORD_AAAA)
1523 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1524 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1526 rh->proc (rh->proc_cls, rh, 0, NULL);
1529 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1530 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1532 rd.data_size = sizeof (struct in6_addr);
1534 rh->proc (rh->proc_cls, rh, 1, &rd);
1538 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1539 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1541 rh->proc (rh->proc_cls, rh, 0, NULL);
1548 * Process VPN lookup result for record
1550 * @param cls the record lookup handle
1551 * @param rh resolver handle
1552 * @param rd_count number of results (1)
1553 * @param rd record data containing the result
1556 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1557 unsigned int rd_count,
1558 const struct GNUNET_NAMESTORE_RecordData *rd)
1560 struct RecordLookupHandle* rlh = (struct RecordLookupHandle*) cls;
1564 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1565 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1568 /* give up, cannot resolve */
1569 finish_lookup(rh, rlh, 0, NULL);
1573 /* results found yay */
1574 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1575 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1577 finish_lookup(rh, rlh, rd_count, rd);
1582 * Sends a UDP dns query to a nameserver specified in the rh
1584 * @param rh the resolver handle
1587 send_dns_packet (struct ResolverHandle *rh);
1591 handle_dns_resolver (void *cls,
1592 const struct sockaddr *addr,
1595 struct ResolverHandle *rh = cls;
1596 struct RecordLookupHandle *rlh = rh->proc_cls;
1597 struct GNUNET_NAMESTORE_RecordData rd;
1598 struct sockaddr_in *sai;
1599 struct sockaddr_in6 *sai6;
1603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1604 "No address found in DNS!\n");
1605 finish_lookup (rh, rlh, 0, NULL);
1609 if (addrlen == sizeof (struct sockaddr_in))
1611 sai = (struct sockaddr_in*) addr;
1612 rd.record_type = GNUNET_GNS_RECORD_A;
1613 rd.data_size = sizeof (struct in_addr);
1614 rd.data = &sai->sin_addr;
1618 sai6 = (struct sockaddr_in6*) addr;
1619 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1620 rd.data_size = sizeof (struct in6_addr);
1621 rd.data = &sai6->sin6_addr;
1624 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1627 finish_lookup (rh, rlh, 1, &rd);
1632 * Resolve DNS name via local stub resolver
1634 * @param rh the resolver handle
1637 resolve_dns_name (struct ResolverHandle *rh)
1639 struct RecordLookupHandle *rlh = rh->proc_cls;
1642 if ((rlh->record_type != GNUNET_GNS_RECORD_A) &&
1643 (rlh->record_type != GNUNET_GNS_RECORD_AAAA))
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1646 "Can only resolve A/AAAA via stub... abort\n");
1647 finish_lookup (rh, rlh, 0, NULL);
1651 if (rlh->record_type == GNUNET_GNS_RECORD_A)
1656 //GNUNET_RESOLVER_connect (cfg); FIXME into init
1658 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1661 &handle_dns_resolver,
1667 * Read DNS udp packet from socket
1669 * @param cls the resolver handle
1670 * @param tc task context
1673 read_dns_response (void *cls,
1674 const struct GNUNET_SCHEDULER_TaskContext *tc)
1676 struct ResolverHandle *rh = cls;
1677 struct RecordLookupHandle *rlh = rh->proc_cls;
1678 char buf[UINT16_MAX];
1680 struct sockaddr_in addr;
1682 struct GNUNET_DNSPARSER_Packet *packet;
1683 struct GNUNET_NAMESTORE_RecordData rd;
1684 int found_delegation = GNUNET_NO;
1685 int found_cname = GNUNET_NO;
1686 char* delegation_name = NULL;
1687 int zone_offset = 0;
1690 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1691 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1693 /* timeout or shutdown */
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "Terminating DNS query %d\n", tc->reason);
1696 finish_lookup (rh, rlh, 0, NULL);
1700 addrlen = sizeof (addr);
1701 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1703 (struct sockaddr*) &addr,
1708 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1709 finish_lookup (rh, rlh, 0, NULL);
1713 packet = GNUNET_DNSPARSER_parse (buf, r);
1717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1718 "Failed to parse DNS reply!\n");
1719 finish_lookup (rh, rlh, 0, NULL);
1723 for (i = 0; i < packet->num_answers; i++)
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1726 "Got record type %d (want %d)\n",
1727 packet->answers[i].type,
1729 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1730 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1733 "CNAME... restarting query with %s\n",
1734 packet->answers[i].data.hostname
1736 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1737 found_cname = GNUNET_YES;
1738 //send_dns_packet (rh);
1739 //GNUNET_DNSPARSER_free_packet (packet);
1743 if ((packet->answers[i].type == rlh->record_type) &&
1744 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1748 rd.data = packet->answers[i].data.raw.data;
1749 rd.data_size = packet->answers[i].data.raw.data_len;
1750 rd.record_type = packet->answers[i].type;
1752 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1753 finish_lookup (rh, rlh, 1, &rd);
1754 GNUNET_DNSPARSER_free_packet (packet);
1759 if (GNUNET_YES == found_cname)
1761 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1763 if (0 > zone_offset)
1766 /* restart query with CNAME */
1767 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770 "Asking same server for %s\n", rh->dns_name);
1771 send_dns_packet (rh);
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 "Trying system resolver for %s\n", rh->dns_name);
1777 resolve_dns_name (rh);
1780 GNUNET_DNSPARSER_free_packet (packet);
1784 for (i = 0; i < packet->num_authority_records; i++)
1787 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1790 "Found NS delegation!\n");
1791 found_delegation = GNUNET_YES;
1792 delegation_name = packet->authority_records[i].data.hostname;
1797 for (i = 0; i < packet->num_additional_records; i++)
1799 if (found_delegation == GNUNET_NO)
1802 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1803 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1805 GNUNET_assert (sizeof (struct in_addr) ==
1806 packet->authority_records[i].data.raw.data_len);
1808 rh->dns_addr.sin_addr =
1809 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1810 send_dns_packet (rh);
1811 GNUNET_DNSPARSER_free_packet (packet);
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1817 "Nothing useful in DNS reply!\n");
1818 finish_lookup (rh, rlh, 0, NULL);
1819 GNUNET_DNSPARSER_free_packet (packet);
1824 * Sends a UDP dns query to a nameserver specified in the rh
1826 * @param rh the request handle
1829 send_dns_packet (struct ResolverHandle *rh)
1831 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1832 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1835 "Sending %dbyte DNS query\n",
1836 rh->dns_raw_packet_size);
1838 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1840 rh->dns_raw_packet_size,
1841 (struct sockaddr*)&rh->dns_addr,
1842 sizeof (struct sockaddr_in));
1844 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1845 rh->timeout, //FIXME less?
1851 GNUNET_NETWORK_fdset_destroy (rset);
1856 * The final phase of resoution.
1857 * We found a NS RR and want to resolve via DNS
1859 * @param rh the pending lookup handle
1860 * @param rd_count length of record data
1861 * @param rd record data containing VPN RR
1864 resolve_record_dns (struct ResolverHandle *rh,
1866 const struct GNUNET_NAMESTORE_RecordData *rd)
1868 struct GNUNET_DNSPARSER_Query query;
1869 struct GNUNET_DNSPARSER_Packet packet;
1870 struct GNUNET_DNSPARSER_Flags flags;
1871 struct in_addr dnsip;
1872 struct sockaddr_in addr;
1873 struct sockaddr *sa;
1875 struct RecordLookupHandle *rlh = rh->proc_cls;
1877 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1879 /* We cancel here as to not include the ns lookup in the timeout */
1880 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1882 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1883 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1885 /* Start shortening */
1886 if ((rh->priv_key != NULL) &&
1887 (is_canonical (rh->name) == GNUNET_YES))
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1890 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1892 start_shorten (rh->authority_chain_tail,
1896 for (i = 0; i < rd_count; i++)
1898 /* Synthesize dns name */
1899 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
1901 strcpy (rh->dns_zone, (char*)rd[i].data);
1902 if (0 == strcmp (rh->name, ""))
1903 strcpy (rh->dns_name, (char*)rd[i].data);
1905 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1908 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
1909 dnsip = *((struct in_addr*)rd[i].data);
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1913 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1917 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1918 if (rh->dns_sock == NULL)
1920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1921 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1923 finish_lookup (rh, rlh, 0, NULL);
1927 memset (&addr, 0, sizeof (struct sockaddr_in));
1928 sa = (struct sockaddr *) &addr;
1929 sa->sa_family = AF_INET;
1930 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1932 sizeof (struct sockaddr_in)))
1934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1935 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1937 finish_lookup (rh, rlh, 0, NULL);
1941 /*TODO create dnsparser query, serialize, sendto, handle reply*/
1942 query.name = rh->dns_name;
1943 query.type = rlh->record_type;
1944 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1945 memset (&flags, 0, sizeof (flags));
1946 flags.recursion_desired = 1;
1947 flags.checking_disabled = 1;
1948 packet.queries = &query;
1949 packet.answers = NULL;
1950 packet.authority_records = NULL;
1951 packet.num_queries = 1;
1952 packet.num_answers = 0;
1953 packet.num_authority_records = 0;
1954 packet.num_additional_records = 0;
1955 packet.flags = flags;
1957 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1959 &rh->dns_raw_packet,
1960 &rh->dns_raw_packet_size))
1962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1963 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1965 GNUNET_NETWORK_socket_close (rh->dns_sock);
1966 finish_lookup (rh, rlh, 0, NULL);
1970 rh->dns_addr.sin_family = AF_INET;
1971 rh->dns_addr.sin_port = htons (53); //domain
1972 rh->dns_addr.sin_addr = dnsip;
1973 #if HAVE_SOCKADDR_IN_SIN_LEN
1974 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1977 send_dns_packet (rh);
1982 * The final phase of resoution.
1983 * We found a VPN RR and want to request an IPv4/6 address
1985 * @param rh the pending lookup handle
1986 * @param rd_count length of record data
1987 * @param rd record data containing VPN RR
1990 resolve_record_vpn (struct ResolverHandle *rh,
1992 const struct GNUNET_NAMESTORE_RecordData *rd)
1994 struct RecordLookupHandle *rlh = rh->proc_cls;
1995 struct GNUNET_HashCode serv_desc;
1996 struct vpn_data* vpn;
1999 /* We cancel here as to not include the ns lookup in the timeout */
2000 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2002 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2003 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2005 /* Start shortening */
2006 if ((rh->priv_key != NULL) &&
2007 (is_canonical (rh->name) == GNUNET_YES))
2009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2010 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
2012 start_shorten (rh->authority_chain_tail,
2016 vpn = (struct vpn_data*)rd->data;
2019 GNUNET_CRYPTO_hash ((char*)&vpn[1],
2020 strlen ((char*)&vpn[1]) + 1,
2022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2023 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
2026 GNUNET_h2s (&vpn->peer));
2028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2029 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
2032 GNUNET_h2s (&serv_desc));
2033 rh->proc = &handle_record_vpn;
2035 if (NULL == vpn_handle)
2037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2038 "GNS_PHASE_REC_VPN-%llu: VPN not connected!\n",
2040 finish_lookup (rh, rh->proc_cls, 0, NULL);
2044 if (rlh->record_type == GNUNET_GNS_RECORD_A)
2050 if (NULL == vpn_handle)
2052 vpn_handle = GNUNET_VPN_connect (cfg);
2053 if (NULL == vpn_handle)
2055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2056 "GNS_PHASE_INIT: Error connecting to VPN!\n");
2057 finish_lookup (rh, rh->proc_cls, 0, NULL);
2063 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
2064 af, ntohs (vpn->proto),
2065 (struct GNUNET_PeerIdentity *)&vpn->peer,
2068 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
2069 &process_record_result_vpn,
2075 * The final phase of resolution.
2076 * rh->name is a name that is canonical and we do not have a delegation.
2077 * Query namestore for this record
2079 * @param rh the pending lookup handle
2082 resolve_record_ns(struct ResolverHandle *rh)
2084 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
2086 /* We cancel here as to not include the ns lookup in the timeout */
2087 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2089 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2090 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2092 /* Start shortening */
2093 if ((rh->priv_key != NULL) &&
2094 (is_canonical (rh->name) == GNUNET_YES))
2096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2097 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
2099 start_shorten (rh->authority_chain_tail,
2104 * Try to resolve this record in our namestore.
2105 * The name to resolve is now in rh->authority_name
2106 * since we tried to resolve it to an authority
2109 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
2113 &process_record_result_ns,
2120 * Handle timeout for DHT requests
2122 * @param cls the request handle as closure
2123 * @param tc the task context
2126 dht_authority_lookup_timeout(void *cls,
2127 const struct GNUNET_SCHEDULER_TaskContext *tc)
2129 struct ResolverHandle *rh = cls;
2130 struct RecordLookupHandle *rlh = rh->proc_cls;
2131 char new_name[MAX_DNS_NAME_LENGTH];
2133 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2134 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
2135 rh->id, rh->authority_name, rh->timeout.rel_value);
2137 rh->status |= RSL_TIMED_OUT;
2139 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2141 if (NULL != rh->get_handle)
2142 GNUNET_DHT_get_stop (rh->get_handle);
2144 rh->get_handle = NULL;
2146 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
2148 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2149 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
2151 rh->proc(rh->proc_cls, rh, 0, NULL);
2155 if (strcmp(rh->name, "") == 0)
2158 * promote authority back to name and try to resolve record
2160 strcpy(rh->name, rh->authority_name);
2161 rh->proc(rh->proc_cls, rh, 0, NULL);
2166 * Start resolution in bg
2168 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
2169 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
2170 //strcpy(new_name, rh->name);
2171 //strcpy(new_name+strlen(new_name), ".");
2172 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
2174 strcpy(rh->name, new_name);
2176 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2177 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2178 rh->id, rh->name, rlh->record_type);
2180 gns_resolver_lookup_record(rh->authority,
2181 rh->private_local_zone,
2185 GNUNET_TIME_UNIT_FOREVER_REL,
2187 &background_lookup_result_processor,
2190 rh->proc(rh->proc_cls, rh, 0, NULL);
2194 static void resolve_delegation_dht(struct ResolverHandle *rh);
2197 static void resolve_delegation_ns(struct ResolverHandle *rh);
2201 * Namestore resolution for delegation finished. Processing result.
2203 * @param cls the closure
2204 * @param rh resolver handle
2205 * @param rd_count number of results (always 0)
2206 * @param rd record data (always NULL)
2209 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2210 unsigned int rd_count,
2211 const struct GNUNET_NAMESTORE_RecordData *rd);
2215 * This is a callback function that checks for key revocation
2217 * @param cls the pending query
2218 * @param key the key of the zone we did the lookup
2219 * @param expiration expiration date of the record data set in the namestore
2220 * @param name the name for which we need an authority
2221 * @param rd_count the number of records with 'name'
2222 * @param rd the record data
2223 * @param signature the signature of the authority for the record data
2226 process_pkey_revocation_result_ns (void *cls,
2227 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2228 struct GNUNET_TIME_Absolute expiration,
2230 unsigned int rd_count,
2231 const struct GNUNET_NAMESTORE_RecordData *rd,
2232 const struct GNUNET_CRYPTO_RsaSignature *signature)
2234 struct ResolverHandle *rh = cls;
2235 struct GNUNET_TIME_Relative remaining_time;
2238 rh->namestore_task = NULL;
2239 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2241 for (i = 0; i < rd_count; i++)
2243 if (rd[i].record_type == GNUNET_GNS_RECORD_REV)
2245 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2246 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2248 rh->status |= RSL_PKEY_REVOKED;
2249 rh->proc (rh->proc_cls, rh, 0, NULL);
2254 if ((name == NULL) ||
2255 (remaining_time.rel_value == 0))
2257 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2258 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2261 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2263 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2264 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2265 rh->id, "+.gnunet", GNUNET_GNS_RECORD_REV);
2267 gns_resolver_lookup_record(rh->authority,
2268 rh->private_local_zone,
2269 GNUNET_GNS_RECORD_REV,
2272 GNUNET_TIME_UNIT_FOREVER_REL,
2274 &background_lookup_result_processor,
2278 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2279 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2282 * We are done with PKEY resolution if name is empty
2283 * else resolve again with new authority
2285 if (strcmp (rh->name, "") == 0)
2286 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2288 resolve_delegation_ns (rh);
2295 * Callback when record data is put into namestore
2297 * @param cls the closure
2298 * @param success GNUNET_OK on success
2299 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2302 on_namestore_delegation_put_result(void *cls,
2306 struct NamestoreBGTask *nbg = cls;
2308 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2311 if (GNUNET_NO == success)
2313 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2314 "GNS_NS: records already in namestore\n");
2317 else if (GNUNET_YES == success)
2319 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2320 "GNS_NS: records successfully put in namestore\n");
2324 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2325 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2329 * Function called when we get a result from the dht
2330 * for our query. Recursively tries to resolve authorities
2333 * @param cls the request handle
2334 * @param exp lifetime
2335 * @param key the key the record was stored under
2336 * @param get_path get path
2337 * @param get_path_length get path length
2338 * @param put_path put path
2339 * @param put_path_length put path length
2340 * @param type the block type
2341 * @param size the size of the record
2342 * @param data the record data
2345 process_delegation_result_dht(void* cls,
2346 struct GNUNET_TIME_Absolute exp,
2347 const struct GNUNET_HashCode * key,
2348 const struct GNUNET_PeerIdentity *get_path,
2349 unsigned int get_path_length,
2350 const struct GNUNET_PeerIdentity *put_path,
2351 unsigned int put_path_length,
2352 enum GNUNET_BLOCK_Type type,
2353 size_t size, const void *data)
2355 struct ResolverHandle *rh;
2356 struct GNSNameRecordBlock *nrb;
2357 uint32_t num_records;
2359 char* rd_data = (char*) data;
2362 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
2363 struct GNUNET_HashCode zone_hash_double, name_hash_double;
2365 rh = (struct ResolverHandle *)cls;
2367 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2368 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2373 nrb = (struct GNSNameRecordBlock*)data;
2375 /* stop dht lookup and timeout task */
2376 GNUNET_DHT_get_stop (rh->get_handle);
2378 rh->get_handle = NULL;
2380 if (rh->dht_heap_node != NULL)
2382 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2383 rh->dht_heap_node = NULL;
2386 num_records = ntohl(nrb->rd_count);
2387 name = (char*)&nrb[1];
2389 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2390 struct NamestoreBGTask *ns_heap_root;
2391 struct NamestoreBGTask *namestore_bg_task;
2393 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2394 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2396 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2401 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2402 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2407 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2408 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2409 rh->id, name, rh->authority_name);
2410 for (i=0; i<num_records; i++)
2413 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2414 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2415 rh->id, name, rh->authority_name);
2416 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2417 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2418 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2419 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2420 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2421 rh->id, rd[i].data_size);
2422 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2423 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2424 rh->id, rd[i].flags);
2426 if ((rd[i].record_type == GNUNET_GNS_RECORD_VPN) ||
2427 (rd[i].record_type == GNUNET_GNS_RECORD_NS) ||
2428 (rd[i].record_type == GNUNET_GNS_RECORD_CNAME))
2431 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2433 if (strcmp(rh->name, "") == 0)
2434 strcpy(rh->name, rh->authority_name);
2436 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2437 rh->name, rh->authority_name); //FIXME ret
2442 if ((strcmp(name, rh->authority_name) == 0) &&
2443 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
2445 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2446 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2449 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2450 struct AuthorityChain *auth =
2451 GNUNET_malloc(sizeof(struct AuthorityChain));
2452 auth->zone = rh->authority;
2453 memset(auth->name, 0, strlen(rh->authority_name)+1);
2454 strcpy(auth->name, rh->authority_name);
2455 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2456 rh->authority_chain_tail,
2459 if (NULL != rh->rd.data)
2460 GNUNET_free ((void*)rh->rd.data);
2462 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2463 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2464 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2467 /** try to import pkey if private key available */
2468 //if (rh->priv_key && is_canonical (rh->name))
2469 // process_discovered_authority(name, auth->zone,
2470 // rh->authority_chain_tail->zone,
2477 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2478 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2479 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2480 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2482 /* Save to namestore */
2483 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
2486 if (max_allowed_ns_tasks <=
2487 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2489 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2490 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2492 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2493 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
2497 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2499 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2501 GNUNET_TIME_absolute_get().abs_value);
2502 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2509 &on_namestore_delegation_put_result, //cont
2510 namestore_bg_task); //cls
2519 * FIXME in this case. should we ask namestore again?
2521 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2522 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2523 rh->id, rh->authority_name, rh->name);
2525 if (strcmp(rh->name, "") == 0)
2527 /* Start shortening */
2528 if ((rh->priv_key != NULL) &&
2529 (is_canonical (rh->name) == GNUNET_YES))
2531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2532 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2534 start_shorten (rh->authority_chain_tail,
2539 rh->proc = &handle_delegation_ns;
2542 /* Check for key revocation and delegate */
2543 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2546 GNUNET_GNS_RECORD_REV,
2547 &process_pkey_revocation_result_ns,
2554 * No pkey but name exists
2557 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2558 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2559 rh->id, rh->authority_name, rh->name);
2560 if (strcmp(rh->name, "") == 0)
2561 strcpy(rh->name, rh->authority_name);
2563 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2564 rh->name, rh->authority_name); //FIXME ret
2566 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2567 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2568 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2569 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2571 rh->proc(rh->proc_cls, rh, 0, NULL);
2574 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2575 +(MAX_DNS_NAME_LENGTH*2)
2576 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2577 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2581 expand_plus(char* dest, char* src, char* repl)
2584 unsigned int s_len = strlen(src)+1;
2586 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2587 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2591 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2592 "GNS_POSTPROCESS: %s to short\n", src);
2594 /* no postprocessing */
2595 memcpy(dest, src, s_len+1);
2599 if (0 == strcmp(src+s_len-3, ".+"))
2601 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2602 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2603 memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2607 pos += strlen(repl);
2608 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2609 "GNS_POSTPROCESS: Expanded to %s\n", dest);
2613 memcpy(dest, src, s_len+1);
2621 finish_lookup (struct ResolverHandle *rh,
2622 struct RecordLookupHandle* rlh,
2623 unsigned int rd_count,
2624 const struct GNUNET_NAMESTORE_RecordData *rd)
2627 char new_rr_data[MAX_DNS_NAME_LENGTH];
2628 char new_mx_data[MAX_MX_LENGTH];
2629 char new_soa_data[MAX_SOA_LENGTH];
2630 char new_srv_data[MAX_SRV_LENGTH];
2631 struct srv_data *old_srv;
2632 struct srv_data *new_srv;
2633 struct soa_data *old_soa;
2634 struct soa_data *new_soa;
2635 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2638 unsigned int offset;
2640 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2642 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2643 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2646 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2649 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2651 for (i = 0; i < rd_count; i++)
2654 if (rd[i].record_type != GNUNET_GNS_RECORD_NS &&
2655 rd[i].record_type != GNUNET_GNS_RECORD_PTR &&
2656 rd[i].record_type != GNUNET_GNS_RECORD_CNAME &&
2657 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
2658 rd[i].record_type != GNUNET_GNS_RECORD_SOA &&
2659 rd[i].record_type != GNUNET_GNS_RECORD_SRV)
2661 p_rd[i].data = rd[i].data;
2666 * for all those records we 'should'
2667 * also try to resolve the A/AAAA records (RFC1035)
2668 * This is a feature and not important
2671 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2672 "GNS_POSTPROCESS: Postprocessing\n");
2674 if (strcmp(rh->name, "+") == 0)
2675 repl_string = rlh->name;
2677 repl_string = rlh->name+strlen(rh->name)+1;
2680 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
2682 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2683 offset = sizeof(uint16_t);
2684 pos = new_mx_data+offset;
2685 expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2687 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2688 p_rd[i].data = new_mx_data;
2689 p_rd[i].data_size = offset;
2691 else if (rd[i].record_type == GNUNET_GNS_RECORD_SRV)
2694 * Prio, weight and port
2696 new_srv = (struct srv_data*)new_srv_data;
2697 old_srv = (struct srv_data*)rd[i].data;
2698 new_srv->prio = old_srv->prio;
2699 new_srv->weight = old_srv->weight;
2700 new_srv->port = old_srv->port;
2701 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2703 p_rd[i].data = new_srv_data;
2704 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2706 else if (rd[i].record_type == GNUNET_GNS_RECORD_SOA)
2708 /* expand mname and rname */
2709 old_soa = (struct soa_data*)rd[i].data;
2710 new_soa = (struct soa_data*)new_soa_data;
2711 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2712 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2713 offset = strlen ((char*)&new_soa[1]) + 1;
2714 expand_plus((char*)&new_soa[1] + offset,
2715 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2717 p_rd[i].data_size = sizeof (struct soa_data)
2719 + strlen ((char*)&new_soa[1] + offset);
2720 p_rd[i].data = new_soa_data;
2725 expand_plus(pos, (char*)rd[i].data, repl_string);
2726 p_rd[i].data_size = strlen(new_rr_data)+1;
2727 p_rd[i].data = new_rr_data;
2732 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2734 free_resolver_handle (rh);
2738 * Process DHT lookup result for record.
2740 * @param cls the closure
2741 * @param rh resolver handle
2742 * @param rd_count number of results
2743 * @param rd record data
2746 handle_record_dht(void* cls, struct ResolverHandle *rh,
2747 unsigned int rd_count,
2748 const struct GNUNET_NAMESTORE_RecordData *rd)
2750 struct RecordLookupHandle* rlh;
2752 rlh = (struct RecordLookupHandle*)cls;
2755 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2756 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2758 /* give up, cannot resolve */
2759 finish_lookup(rh, rlh, 0, NULL);
2763 /* results found yay */
2764 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2765 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2767 finish_lookup(rh, rlh, rd_count, rd);
2775 * Process namestore lookup result for record.
2777 * @param cls the closure
2778 * @param rh resolver handle
2779 * @param rd_count number of results
2780 * @param rd record data
2783 handle_record_ns (void* cls, struct ResolverHandle *rh,
2784 unsigned int rd_count,
2785 const struct GNUNET_NAMESTORE_RecordData *rd)
2787 struct RecordLookupHandle* rlh;
2788 rlh = (struct RecordLookupHandle*) cls;
2789 int check_dht = GNUNET_YES;
2793 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2794 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2799 * There are 5 conditions that have to met for us to consult the DHT:
2800 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2801 * 2. No entry in the NS existed AND
2802 * 3. The zone queried is not the local resolver's zone AND
2803 * 4. The name that was looked up is '+'
2804 * because if it was any other canonical name we either already queried
2805 * the DHT for the authority in the authority lookup phase (and thus
2806 * would already have an entry in the NS for the record)
2807 * 5. We are not in cache only mode
2809 if (((rh->status & RSL_RECORD_EXPIRED) != 0) &&
2810 ((rh->status & RSL_RECORD_EXISTS) == 0) )
2811 check_dht = GNUNET_NO;
2813 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2814 &rh->private_local_zone))
2815 check_dht = GNUNET_NO;
2817 if ((strcmp (rh->name, "+") != 0) && (is_srv (rh->name) != 0))
2818 check_dht = GNUNET_NO;
2821 if (rh->only_cached == GNUNET_YES)
2822 check_dht = GNUNET_NO;
2824 if (GNUNET_YES == check_dht)
2826 rh->proc = &handle_record_dht;
2827 resolve_record_dht(rh);
2830 /* give up, cannot resolve */
2831 finish_lookup(rh, rlh, 0, NULL);
2835 /* results found yay */
2836 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2837 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2839 finish_lookup(rh, rlh, rd_count, rd);
2846 * Move one level up in the domain hierarchy and return the
2847 * passed top level domain.
2849 * @param name the domain
2850 * @param dest the destination where the tld will be put
2853 pop_tld(char* name, char* dest)
2857 if (is_canonical (name) == GNUNET_YES)
2864 for (len = strlen(name); len > 0; len--)
2866 if (*(name+len) == '.')
2876 strcpy(dest, (name+len+1));
2880 * Checks if name is in tld
2882 * @param name the name to check
2883 * @param tld the TLD to check for
2884 * @return GNUNET_YES or GNUNET_NO
2887 is_tld(const char* name, const char* tld)
2891 if (strlen(name) <= strlen(tld))
2896 offset = strlen(name)-strlen(tld);
2897 if (strcmp(name+offset, tld) != 0)
2899 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2900 "%s is not in .%s TLD\n", name, tld);
2907 * DHT resolution for delegation finished. Processing result.
2909 * @param cls the closure
2910 * @param rh resolver handle
2911 * @param rd_count number of results (always 0)
2912 * @param rd record data (always NULL)
2915 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2916 unsigned int rd_count,
2917 const struct GNUNET_NAMESTORE_RecordData *rd)
2919 struct RecordLookupHandle* rlh;
2920 rlh = (struct RecordLookupHandle*) cls;
2923 if (strcmp(rh->name, "") == 0)
2925 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
2927 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2928 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2930 finish_lookup(rh, rlh, rd_count, rd);
2933 /* We resolved full name for delegation. resolving record */
2934 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2935 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2937 strcpy(rh->name, "+\0");
2938 rh->proc = &handle_record_ns;
2939 resolve_record_ns(rh);
2944 * we still have some left
2946 if (is_canonical (rh->name) == GNUNET_YES)
2948 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2949 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2952 rh->proc = &handle_record_ns;
2953 resolve_record_ns(rh);
2956 /* give up, cannot resolve */
2957 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2958 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2960 finish_lookup(rh, rlh, 0, NULL);
2965 * Start DHT lookup for a name -> PKEY (compare NS) record in
2966 * rh->authority's zone
2968 * @param rh the pending gns query
2971 resolve_delegation_dht(struct ResolverHandle *rh)
2974 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2975 struct GNUNET_HashCode name_hash_double;
2976 struct GNUNET_HashCode zone_hash_double;
2977 struct GNUNET_HashCode lookup_key;
2978 struct ResolverHandle *rh_heap_root;
2980 pop_tld(rh->name, rh->authority_name);
2981 GNUNET_CRYPTO_short_hash(rh->authority_name,
2982 strlen(rh->authority_name),
2984 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2985 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2986 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2988 rh->dht_heap_node = NULL;
2990 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2992 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
2993 // &dht_authority_lookup_timeout,
2995 rh->timeout_cont = &dht_authority_lookup_timeout;
2996 rh->timeout_cont_cls = rh;
3000 if (max_allowed_background_queries <=
3001 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
3003 /* terminate oldest lookup */
3004 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
3005 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
3006 rh_heap_root->get_handle = NULL;
3007 rh_heap_root->dht_heap_node = NULL;
3009 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3010 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
3011 rh->id, rh_heap_root->authority_name);
3013 rh_heap_root->proc(rh_heap_root->proc_cls,
3018 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
3020 GNUNET_TIME_absolute_get().abs_value);
3023 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
3025 GNUNET_assert(rh->get_handle == NULL);
3026 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
3027 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
3029 DHT_GNS_REPLICATION_LEVEL,
3033 &process_delegation_result_dht,
3040 * Namestore resolution for delegation finished. Processing result.
3042 * @param cls the closure
3043 * @param rh resolver handle
3044 * @param rd_count number of results (always 0)
3045 * @param rd record data (always NULL)
3048 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
3049 unsigned int rd_count,
3050 const struct GNUNET_NAMESTORE_RecordData *rd)
3052 struct RecordLookupHandle* rlh;
3053 rlh = (struct RecordLookupHandle*) cls;
3054 int check_dht = GNUNET_YES;
3057 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3058 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
3059 rh->id, rh->status);
3061 if (rh->status & RSL_PKEY_REVOKED)
3063 finish_lookup (rh, rlh, 0, NULL);
3067 if (strcmp(rh->name, "") == 0)
3070 /* We resolved full name for delegation. resolving record */
3071 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3072 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
3074 if (rh->status & RSL_CNAME_FOUND)
3076 if (rlh->record_type == GNUNET_GNS_RECORD_CNAME)
3078 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3079 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
3081 strcpy (rh->name, rh->authority_name);
3082 finish_lookup (rh, rlh, rd_count, rd);
3087 if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
3089 s_len = strlen (rd->data) - 2;
3090 memcpy (rh->name, rd->data, s_len);
3091 rh->name[s_len] = '\0';
3092 resolve_delegation_ns (rh);
3095 else if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
3097 gns_resolver_lookup_record (rh->authority,
3098 rh->private_local_zone,
3107 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3108 free_resolver_handle (rh);
3114 strcpy (rh->dns_name, (char*)rd->data);
3115 resolve_dns_name (rh);
3120 else if (rh->status & RSL_DELEGATE_VPN)
3122 if (rlh->record_type == GNUNET_GNS_RECORD_VPN)
3124 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3125 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
3127 finish_lookup(rh, rlh, rd_count, rd);
3130 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3131 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
3133 GNUNET_assert (NULL != rd);
3134 rh->proc = &handle_record_vpn;
3135 resolve_record_vpn (rh, rd_count, rd);
3138 else if (rh->status & RSL_DELEGATE_NS)
3140 if (rlh->record_type == GNUNET_GNS_RECORD_NS)
3142 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3143 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3145 finish_lookup(rh, rlh, rd_count, rd);
3149 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3150 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3152 GNUNET_assert (NULL != rd);
3153 rh->proc = &handle_record_ns;
3154 resolve_record_dns (rh, rd_count, rd);
3157 else if (rh->status & RSL_DELEGATE_PKEY)
3159 if (rh->status & RSL_PKEY_REVOKED)
3161 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3162 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
3164 finish_lookup (rh, rlh, 0, NULL);
3167 else if (rlh->record_type == GNUNET_GNS_RECORD_PKEY)
3169 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3170 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
3172 finish_lookup(rh, rlh, rd_count, rd);
3176 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3177 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3179 strcpy(rh->name, "+\0");
3180 rh->proc = &handle_record_ns;
3181 resolve_record_ns(rh);
3185 if (rh->status & RSL_DELEGATE_NS)
3187 if (rlh->record_type == GNUNET_GNS_RECORD_NS)
3189 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3190 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3192 finish_lookup(rh, rlh, rd_count, rd);
3196 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3197 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3199 GNUNET_assert (NULL != rd);
3200 rh->proc = &handle_record_ns;
3201 resolve_record_dns (rh, rd_count, rd);
3206 * we still have some left
3207 * check if authority in ns is fresh
3209 * or we are authority
3212 if ((rh->status & RSL_RECORD_EXISTS) &&
3213 !(rh->status & RSL_RECORD_EXPIRED))
3214 check_dht = GNUNET_NO;
3216 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3217 &rh->private_local_zone))
3218 check_dht = GNUNET_NO;
3220 if (rh->only_cached == GNUNET_YES)
3221 check_dht = GNUNET_NO;
3223 if (check_dht == GNUNET_NO)
3225 if (is_canonical (rh->name) == GNUNET_YES)
3227 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3228 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3231 rh->proc = &handle_record_ns;
3232 resolve_record_ns(rh);
3236 /* give up, cannot resolve */
3237 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3238 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3241 finish_lookup(rh, rlh, rd_count, rd);
3242 //rlh->proc(rlh->proc_cls, 0, NULL);
3247 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3248 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3250 rh->proc = &handle_delegation_dht;
3251 resolve_delegation_dht(rh);
3256 * This is a callback function that should give us only PKEY
3257 * records. Used to query the namestore for the authority (PKEY)
3258 * for 'name'. It will recursively try to resolve the
3259 * authority for a given name from the namestore.
3261 * @param cls the pending query
3262 * @param key the key of the zone we did the lookup
3263 * @param expiration expiration date of the record data set in the namestore
3264 * @param name the name for which we need an authority
3265 * @param rd_count the number of records with 'name'
3266 * @param rd the record data
3267 * @param signature the signature of the authority for the record data
3270 process_delegation_result_ns (void* cls,
3271 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3272 struct GNUNET_TIME_Absolute expiration,
3274 unsigned int rd_count,
3275 const struct GNUNET_NAMESTORE_RecordData *rd,
3276 const struct GNUNET_CRYPTO_RsaSignature *signature)
3278 struct ResolverHandle *rh;
3279 struct GNUNET_TIME_Relative remaining_time;
3280 struct GNUNET_CRYPTO_ShortHashCode zone;
3281 char new_name[MAX_DNS_NAME_LENGTH];
3283 struct GNUNET_TIME_Absolute et;
3285 rh = (struct ResolverHandle *)cls;
3286 rh->namestore_task = NULL;
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3288 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3291 GNUNET_CRYPTO_short_hash (key,
3292 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3294 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3301 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3303 rh->status |= RSL_RECORD_EXISTS;
3305 if (remaining_time.rel_value == 0)
3307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3308 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3310 rh->status |= RSL_RECORD_EXPIRED;
3315 * No authority found in namestore.
3320 * We did not find an authority in the namestore
3325 * Promote this authority back to a name maybe it is
3328 if (strcmp (rh->name, "") == 0)
3330 /* simply promote back */
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3332 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3333 rh->id, rh->authority_name);
3334 strcpy (rh->name, rh->authority_name);
3338 /* add back to existing name */
3339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3340 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3341 rh->id, rh->authority_name, rh->name);
3342 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3343 rh->name, rh->authority_name);
3344 strcpy (rh->name, new_name);
3345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3346 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3350 rh->proc (rh->proc_cls, rh, 0, NULL);
3355 * We found an authority that may be able to help us
3356 * move on with query
3357 * Note only 1 pkey should have been returned.. anything else would be strange
3359 for (i=0; i < rd_count;i++)
3363 * A CNAME. Like regular DNS this means the is no other record for this
3366 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3368 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3369 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3372 rh->status |= RSL_CNAME_FOUND;
3373 rh->proc (rh->proc_cls, rh, rd_count, rd);
3380 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3382 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3383 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3385 rh->status |= RSL_DELEGATE_VPN;
3386 rh->proc (rh->proc_cls, rh, rd_count, rd);
3392 * FIXME make optional
3394 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3396 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3397 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3399 rh->status |= RSL_DELEGATE_NS;
3400 rh->proc (rh->proc_cls, rh, rd_count, rd);
3404 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3407 rh->status |= RSL_DELEGATE_PKEY;
3409 if ((ignore_pending_records != 0) &&
3410 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3413 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3419 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3420 et.abs_value = rd[i].expiration_time;
3421 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3425 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3427 if (remaining_time.rel_value == 0)
3429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3430 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3432 rh->authority_chain_head->fresh = 0;
3433 rh->proc (rh->proc_cls, rh, 0, NULL);
3441 * Resolve rest of query with new authority
3443 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3444 memcpy (&rh->authority, rd[i].data,
3445 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3446 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3447 auth->zone = rh->authority;
3448 memset (auth->name, 0, strlen (rh->authority_name)+1);
3449 strcpy (auth->name, rh->authority_name);
3450 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3451 rh->authority_chain_tail,
3453 if (NULL != rh->rd.data)
3454 GNUNET_free ((void*)(rh->rd.data));
3456 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3457 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3458 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3460 /* Check for key revocation and delegate */
3461 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3464 GNUNET_GNS_RECORD_REV,
3465 &process_pkey_revocation_result_ns,
3474 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3475 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3477 * If we have found some records for the LAST label
3478 * we return the results. Else null.
3480 if (strcmp (rh->name, "") == 0)
3482 /* Start shortening */
3483 if ((rh->priv_key != NULL) &&
3484 (is_canonical (rh->name) == GNUNET_YES))
3486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3487 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3489 start_shorten (rh->authority_chain_tail,
3492 /* simply promote back */
3493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3494 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3495 rh->id, rh->authority_name);
3496 strcpy (rh->name, rh->authority_name);
3497 rh->proc (rh->proc_cls, rh, rd_count, rd);
3501 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
3502 "%s.%s", rh->name, rh->authority_name);
3503 strcpy (rh->name, new_name);
3504 rh->proc (rh->proc_cls, rh, 0, NULL);
3510 * Resolve the delegation chain for the request in our namestore
3512 * @param rh the resolver handle
3515 resolve_delegation_ns (struct ResolverHandle *rh)
3517 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3518 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3520 pop_tld(rh->name, rh->authority_name);
3521 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3524 GNUNET_GNS_RECORD_ANY,
3525 &process_delegation_result_ns,
3532 * Lookup of a record in a specific zone
3533 * calls lookup result processor on result
3535 * @param zone the root zone
3536 * @param pzone the private local zone
3537 * @param record_type the record type to look up
3538 * @param name the name to look up
3539 * @param key a private key for use with PSEU import (can be NULL)
3540 * @param timeout timeout for resolution
3541 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3542 * @param proc the processor to call on result
3543 * @param cls the closure to pass to proc
3546 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3547 struct GNUNET_CRYPTO_ShortHashCode pzone,
3548 uint32_t record_type,
3550 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3551 struct GNUNET_TIME_Relative timeout,
3553 RecordLookupProcessor proc,
3556 struct ResolverHandle *rh;
3557 struct RecordLookupHandle* rlh;
3558 char string_hash[MAX_DNS_LABEL_LENGTH];
3559 char nzkey[MAX_DNS_LABEL_LENGTH];
3560 char* nzkey_ptr = nzkey;
3562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3563 "Starting resolution for %s (type=%d)!\n",
3567 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3568 (strcmp(GNUNET_GNS_TLD, name) != 0))
3570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3571 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3576 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3577 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3579 memset (rh, 0, sizeof (struct ResolverHandle));
3580 rh->authority = zone;
3584 rh->timeout = timeout;
3585 rh->get_handle = NULL;
3586 rh->private_local_zone = pzone;
3587 rh->only_cached = only_cached;
3588 rh->namestore_task = NULL;
3591 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3596 "No shorten key for resolution\n");
3599 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3602 * Set timeout for authority lookup phase to 1/2
3604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3605 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3606 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3607 GNUNET_TIME_relative_divide(timeout, 2),
3608 &handle_lookup_timeout,
3610 rh->timeout_cont = &dht_authority_lookup_timeout;
3611 rh->timeout_cont_cls = rh;
3615 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3616 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3619 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3622 * Only 'gnunet' given
3624 strcpy(rh->name, "\0");
3628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3629 "Checking for TLD...\n");
3630 if (is_zkey_tld(name) == GNUNET_YES)
3632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3635 * This is a zkey tld
3636 * build hash and use as initial authority
3639 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3640 memcpy(rh->name, name,
3641 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3642 pop_tld(rh->name, string_hash);
3644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3645 "ZKEY is %s!\n", string_hash);
3647 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3649 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3652 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3653 "Cannot convert ZKEY %s to hash!\n", string_hash);
3655 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3658 proc (cls, 0, NULL);
3663 else if (is_gnunet_tld (name) == GNUNET_YES)
3665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3668 * Presumably GNUNET tld
3670 memset (rh->name, 0,
3671 strlen(name)-strlen(GNUNET_GNS_TLD));
3672 memcpy (rh->name, name,
3673 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3677 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3678 "Cannot handle this TLD %s\n", string_hash);
3680 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3681 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3682 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3685 proc (cls, 0, NULL);
3691 * Initialize authority chain
3693 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3694 rh->authority_chain_head->prev = NULL;
3695 rh->authority_chain_head->next = NULL;
3696 rh->authority_chain_tail = rh->authority_chain_head;
3697 rh->authority_chain_head->zone = rh->authority;
3698 strcpy (rh->authority_chain_head->name, "");
3701 * Copy original query into lookup handle
3703 rlh->record_type = record_type;
3704 memset(rlh->name, 0, strlen(name) + 1);
3705 strcpy(rlh->name, name);
3707 rlh->proc_cls = cls;
3709 rh->proc = &handle_delegation_ns;
3710 resolve_delegation_ns(rh);
3713 /******** END Record Resolver ***********/
3716 finish_shorten (struct ResolverHandle *rh,
3717 struct NameShortenHandle *nsh)
3719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3720 "Sending %s as shorten result\n", nsh->result);
3721 nsh->proc (nsh->proc_cls, nsh->result);
3722 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3724 free_resolver_handle (rh);
3729 * Callback calles by namestore for a zone to name
3732 * @param cls the closure
3733 * @param zone_key the zone we queried
3734 * @param expire the expiration time of the name
3735 * @param name the name found or NULL
3736 * @param rd_len number of records for the name
3737 * @param rd the record data (PKEY) for the name
3738 * @param signature the signature for the record data
3741 process_zone_to_name_shorten_root (void *cls,
3742 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3743 struct GNUNET_TIME_Absolute expire,
3745 unsigned int rd_len,
3746 const struct GNUNET_NAMESTORE_RecordData *rd,
3747 const struct GNUNET_CRYPTO_RsaSignature *signature);
3751 * Callback called by namestore for a zone to name
3754 * @param cls the closure
3755 * @param zone_key the zone we queried
3756 * @param expire the expiration time of the name
3757 * @param name the name found or NULL
3758 * @param rd_len number of records for the name
3759 * @param rd the record data (PKEY) for the name
3760 * @param signature the signature for the record data
3763 process_zone_to_name_shorten_shorten (void *cls,
3764 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3765 struct GNUNET_TIME_Absolute expire,
3767 unsigned int rd_len,
3768 const struct GNUNET_NAMESTORE_RecordData *rd,
3769 const struct GNUNET_CRYPTO_RsaSignature *signature)
3771 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3772 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3773 struct AuthorityChain *next_authority;
3775 char result[MAX_DNS_NAME_LENGTH];
3776 char tmp_name[MAX_DNS_NAME_LENGTH];
3779 rh->namestore_task = NULL;
3780 /* we found a match in our own root zone */
3783 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3784 memset(result, 0, answer_len);
3786 if (strlen(rh->name) > 0)
3788 sprintf (result, "%s.%s.%s.%s",
3790 nsh->shorten_zone_name,
3795 sprintf (result, "%s.%s.%s", name,
3796 nsh->shorten_zone_name,
3800 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3801 "Found shorten result %s\n", result);
3802 if (strlen (nsh->result) > strlen (result))
3803 strcpy (nsh->result, result);
3805 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3806 nsh->shorten_zone) == 0)
3809 * This is our zone append .gnunet unless name is empty
3810 * (it shouldn't be, usually FIXME what happens if we
3811 * shorten to our zone to a "" record??)
3814 sprintf (result, "%s.%s.%s",
3816 nsh->shorten_zone_name,
3818 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3819 "Our zone: Found %s as shorten result\n", result);
3821 if (strlen (nsh->result) > strlen (result))
3822 strcpy (nsh->result, result);
3823 //nsh->proc(nsh->proc_cls, result);
3825 //free_resolver_handle(rh);
3832 * continue with next authority if exists
3834 if ((rh->authority_chain_head->next == NULL))
3836 finish_shorten (rh, nsh);
3839 next_authority = rh->authority_chain_head;
3841 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3842 "%s.%s", rh->name, next_authority->name);
3844 strcpy(rh->name, tmp_name);
3845 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3846 "No PSEU found for authority %s. Promoting back: %s\n",
3847 next_authority->name, rh->name);
3849 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3850 rh->authority_chain_tail,
3853 GNUNET_free (next_authority);
3855 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3856 &rh->authority_chain_tail->zone,
3857 &rh->authority_chain_head->zone,
3858 &process_zone_to_name_shorten_root,
3863 * Callback calles by namestore for a zone to name
3866 * @param cls the closure
3867 * @param zone_key the zone we queried
3868 * @param expire the expiration time of the name
3869 * @param name the name found or NULL
3870 * @param rd_len number of records for the name
3871 * @param rd the record data (PKEY) for the name
3872 * @param signature the signature for the record data
3875 process_zone_to_name_shorten_private (void *cls,
3876 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3877 struct GNUNET_TIME_Absolute expire,
3879 unsigned int rd_len,
3880 const struct GNUNET_NAMESTORE_RecordData *rd,
3881 const struct GNUNET_CRYPTO_RsaSignature *signature)
3883 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3884 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3885 struct AuthorityChain *next_authority;
3887 char result[MAX_DNS_NAME_LENGTH];
3888 char tmp_name[MAX_DNS_NAME_LENGTH];
3891 rh->namestore_task = NULL;
3892 /* we found a match in our own root zone */
3895 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3896 memset(result, 0, answer_len);
3898 if (strlen(rh->name) > 0)
3900 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3904 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3907 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3908 "Found shorten result %s\n", result);
3909 if (strlen (nsh->result) > strlen (result))
3910 strcpy (nsh->result, result);
3912 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3913 nsh->private_zone) == 0)
3916 * This is our zone append .gnunet unless name is empty
3917 * (it shouldn't be, usually FIXME what happens if we
3918 * shorten to our zone to a "" record??)
3921 sprintf (result, "%s.%s.%s",
3922 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3923 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3924 "Our private zone: Found %s as shorten result %s\n", result);
3925 if (strlen (nsh->result) > strlen (result))
3926 strcpy (nsh->result, result);
3929 if (0 != strcmp (nsh->shorten_zone_name, ""))
3931 /* backtrack authorities for names in priv zone */
3932 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3934 &rh->authority_chain_head->zone,
3935 &process_zone_to_name_shorten_shorten,
3942 * continue with next authority if exists
3944 if ((rh->authority_chain_head->next == NULL))
3946 finish_shorten (rh, nsh);
3949 next_authority = rh->authority_chain_head;
3951 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3952 "%s.%s", rh->name, next_authority->name);
3954 strcpy(rh->name, tmp_name);
3955 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3956 "No PSEU found for authority %s. Promoting back: %s\n",
3957 next_authority->name, rh->name);
3959 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3960 rh->authority_chain_tail,
3963 GNUNET_free (next_authority);
3965 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3966 &rh->authority_chain_tail->zone,
3967 &rh->authority_chain_head->zone,
3968 &process_zone_to_name_shorten_root,
3974 * Callback calles by namestore for a zone to name
3977 * @param cls the closure
3978 * @param zone_key the zone we queried
3979 * @param expire the expiration time of the name
3980 * @param name the name found or NULL
3981 * @param rd_len number of records for the name
3982 * @param rd the record data (PKEY) for the name
3983 * @param signature the signature for the record data
3986 process_zone_to_name_shorten_root (void *cls,
3987 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3988 struct GNUNET_TIME_Absolute expire,
3990 unsigned int rd_len,
3991 const struct GNUNET_NAMESTORE_RecordData *rd,
3992 const struct GNUNET_CRYPTO_RsaSignature *signature)
3994 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3995 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3996 struct AuthorityChain *next_authority;
3998 char result[MAX_DNS_NAME_LENGTH];
3999 char tmp_name[MAX_DNS_NAME_LENGTH];
4002 rh->namestore_task = NULL;
4003 /* we found a match in our own root zone */
4006 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
4007 memset(result, 0, answer_len);
4009 if (strlen(rh->name) > 0)
4011 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
4015 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
4018 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4019 "Found shorten result %s\n", result);
4020 if (strlen (nsh->result) > strlen (result))
4021 strcpy (nsh->result, result);
4023 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4024 nsh->root_zone) == 0)
4027 * This is our zone append .gnunet unless name is empty
4028 * (it shouldn't be, usually FIXME what happens if we
4029 * shorten to our zone to a "" record??)
4032 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
4033 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4034 "Our zone: Found %s as shorten result\n", result);
4035 if (strlen (nsh->result) > strlen (result))
4036 strcpy (nsh->result, result);
4039 if (NULL != nsh->private_zone)
4041 /* backtrack authorities for names in priv zone */
4042 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4044 &rh->authority_chain_head->zone,
4045 &process_zone_to_name_shorten_private,
4048 else if (NULL != nsh->shorten_zone)
4050 /* backtrack authorities for names in shorten zone */
4051 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4053 &rh->authority_chain_head->zone,
4054 &process_zone_to_name_shorten_shorten,
4061 * continue with next authority if exists
4063 if ((rh->authority_chain_head->next == NULL))
4065 finish_shorten (rh, nsh);
4068 next_authority = rh->authority_chain_head;
4070 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
4071 "%s.%s", rh->name, next_authority->name);
4073 strcpy(rh->name, tmp_name);
4074 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4075 "No PSEU found for authority %s. Promoting back: %s\n",
4076 next_authority->name, rh->name);
4078 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
4079 rh->authority_chain_tail,
4082 GNUNET_free (next_authority);
4084 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4085 &rh->authority_chain_tail->zone,
4086 &rh->authority_chain_head->zone,
4087 &process_zone_to_name_shorten_root,
4094 * Process result from namestore delegation lookup
4095 * for shorten operation
4097 * @param cls the client shorten handle
4098 * @param rh the resolver handle
4099 * @param rd_count number of results (0)
4100 * @param rd data (NULL)
4103 handle_delegation_ns_shorten (void* cls,
4104 struct ResolverHandle *rh,
4106 const struct GNUNET_NAMESTORE_RecordData *rd)
4108 struct NameShortenHandle *nsh;
4109 char result[MAX_DNS_NAME_LENGTH];
4111 nsh = (struct NameShortenHandle *)cls;
4114 * At this point rh->name contains the part of the name
4115 * that we do not have a PKEY in our namestore to resolve.
4116 * The authority chain in the resolver handle is now
4117 * useful to backtrack if needed
4120 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4121 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4122 memset(result, 0, sizeof (result));
4124 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4125 nsh->root_zone) == 0)
4128 * This is our zone append .gnunet unless name is empty
4129 * (it shouldn't be, usually FIXME what happens if we
4130 * shorten to our zone to a "" record??)
4133 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
4134 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4135 "Our zone: Found %s as shorten result\n", result);
4137 if (strlen (nsh->result) > strlen (result))
4138 strcpy (nsh->result, result);
4141 else if (NULL != nsh->private_zone)
4144 * This is our zone append .gnunet unless name is empty
4145 * (it shouldn't be, usually FIXME what happens if we
4146 * shorten to our zone to a "" record??)
4148 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4149 nsh->private_zone) == 0)
4152 sprintf (result, "%s.%s.%s",
4153 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4154 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4155 "Our zone: Found %s as shorten result in private zone %s\n",
4158 if (strlen (nsh->result) > strlen (result))
4159 strcpy (nsh->result, result);
4162 else if (NULL != nsh->shorten_zone)
4165 * This is our zone append .gnunet unless name is empty
4166 * (it shouldn't be, usually FIXME what happens if we
4167 * shorten to our zone to a "" record??)
4169 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4170 nsh->shorten_zone) == 0)
4172 sprintf (result, "%s.%s.%s",
4173 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4174 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4175 "Our zone: Found %s as shorten result in shorten zone\n",
4178 if (strlen (nsh->result) > strlen (result))
4179 strcpy (nsh->result, result);
4184 /* backtrack authorities for names */
4185 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4187 &rh->authority_chain_head->zone,
4188 &process_zone_to_name_shorten_root,
4195 * Callback calles by namestore for a zone to name
4198 * @param cls the closure
4199 * @param zone_key the zone we queried
4200 * @param expire the expiration time of the name
4201 * @param name the name found or NULL
4202 * @param rd_len number of records for the name
4203 * @param rd the record data (PKEY) for the name
4204 * @param signature the signature for the record data
4207 process_zone_to_name_zkey(void *cls,
4208 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4209 struct GNUNET_TIME_Absolute expire,
4211 unsigned int rd_len,
4212 const struct GNUNET_NAMESTORE_RecordData *rd,
4213 const struct GNUNET_CRYPTO_RsaSignature *signature)
4215 struct ResolverHandle *rh = cls;
4216 struct NameShortenHandle *nsh = rh->proc_cls;
4217 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
4218 char new_name[MAX_DNS_NAME_LENGTH];
4220 /* zkey not in our zone */
4224 * In this case we have not given this PKEY a name (yet)
4225 * It is either just not in our zone or not even cached
4226 * Since we do not know at this point we will not try to shorten
4227 * because PKEY import will happen if the user follows the zkey
4230 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
4232 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4233 "No name found for zkey %s returning verbatim!\n", enc);
4234 if (strcmp(rh->name, "") != 0)
4235 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4236 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4238 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4239 enc, GNUNET_GNS_TLD_ZKEY);
4241 strcpy (nsh->result, new_name);
4243 finish_shorten (rh, nsh);
4247 if (strcmp(rh->name, "") != 0)
4248 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4251 strcpy(new_name, name);
4253 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4254 "Continue shorten for %s!\n", new_name);
4256 strcpy(rh->name, new_name);
4258 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4259 rh->authority_chain_tail = rh->authority_chain_head;
4260 rh->authority_chain_head->zone = rh->authority;
4263 /* Start delegation resolution in our namestore */
4264 resolve_delegation_ns (rh);
4269 * Shorten api from resolver
4271 * @param zone the root zone to use
4272 * @param pzone the private zone to use
4273 * @param szone the shorten zone to use
4274 * @param name the name to shorten
4275 * @param private_zone_name name of the private zone
4276 * @param shorten_zone_name name of the shorten zone
4277 * @param proc the processor to call with result
4278 * @param proc_cls closure to pass to proc
4281 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4282 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4283 struct GNUNET_CRYPTO_ShortHashCode *szone,
4285 const char* private_zone_name,
4286 const char* shorten_zone_name,
4287 ShortenResultProcessor proc,
4290 struct ResolverHandle *rh;
4291 struct NameShortenHandle *nsh;
4292 char string_hash[MAX_DNS_LABEL_LENGTH];
4293 struct GNUNET_CRYPTO_ShortHashCode zkey;
4294 char nzkey[MAX_DNS_LABEL_LENGTH];
4295 char* nzkey_ptr = nzkey;
4298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4299 "Starting shorten for %s!\n", name);
4301 if (is_canonical ((char*)name) == GNUNET_YES)
4303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4304 "%s is canonical. Returning verbatim\n", name);
4305 proc (proc_cls, name);
4309 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4312 nsh->proc_cls = proc_cls;
4313 nsh->root_zone = zone;
4314 nsh->private_zone = pzone;
4315 nsh->shorten_zone = szone;
4316 strcpy (nsh->private_zone_name, private_zone_name);
4317 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4318 strcpy (nsh->result, name);
4321 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4322 rh->authority = *zone;
4324 rh->priv_key = NULL;
4325 rh->namestore_task = NULL;
4326 rh->proc = &handle_delegation_ns_shorten;
4329 rh->private_local_zone = *zone;
4331 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4334 "Checking for TLD...\n");
4335 if (is_zkey_tld (name) == GNUNET_YES)
4337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4340 * This is a zkey tld
4341 * build hash and use as initial authority
4344 memset (rh->name, 0,
4345 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4346 memcpy (rh->name, name,
4347 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4348 pop_tld (rh->name, string_hash);
4350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4351 "ZKEY is %s!\n", string_hash);
4353 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4355 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4358 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4359 "Cannot convert ZKEY %s to hash!\n", nzkey);
4362 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4363 proc (proc_cls, name);
4367 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4370 &process_zone_to_name_zkey,
4375 else if (is_gnunet_tld (name) == GNUNET_YES)
4377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4380 * Presumably GNUNET tld
4382 memset (rh->name, 0,
4383 strlen (name)-strlen (GNUNET_GNS_TLD));
4384 memcpy (rh->name, name,
4385 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4392 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4393 proc (proc_cls, name);
4397 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4398 rh->authority_chain_tail = rh->authority_chain_head;
4399 rh->authority_chain_head->zone = *zone;
4402 /* Start delegation resolution in our namestore */
4403 resolve_delegation_ns (rh);
4406 /*********** END NAME SHORTEN ********************/
4409 * Conclude get authority lookup
4411 * @param rh resolver handle
4412 * @param nah get authority lookup handle
4415 finish_get_auth (struct ResolverHandle *rh,
4416 struct GetNameAuthorityHandle *nah)
4418 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4419 "Got authority result %s\n", nah->result);
4421 nah->proc(nah->proc_cls, nah->result);
4422 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4424 free_resolver_handle(rh);
4428 * Process result from namestore delegation lookup
4429 * for get authority operation
4431 * @param cls the client get auth handle
4432 * @param rh the resolver handle
4433 * @param rd_count number of results (0)
4434 * @param rd data (NULL)
4437 handle_delegation_result_ns_get_auth(void* cls,
4438 struct ResolverHandle *rh,
4440 const struct GNUNET_NAMESTORE_RecordData *rd)
4442 struct GetNameAuthorityHandle* nah;
4445 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4448 * At this point rh->name contains the part of the name
4449 * that we do not have a PKEY in our namestore to resolve.
4450 * The authority chain in the resolver handle is now
4451 * useful to backtrack if needed
4454 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4455 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4457 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4458 "Building response!\n");
4459 if (is_canonical (rh->name) == GNUNET_YES)
4462 * We successfully resolved the authority in the ns
4463 * FIXME for our purposes this is fine
4464 * but maybe we want to have an api that also looks
4465 * into the dht (i.e. option in message)
4467 if (strlen(rh->name) > strlen(nah->name))
4469 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4470 "Record name longer than original lookup name... odd!\n");
4474 answer_len = strlen(nah->name) - strlen(rh->name)
4475 + strlen(GNUNET_GNS_TLD) + 1;
4476 memset(nah->result, 0, answer_len);
4477 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4479 finish_get_auth (rh, nah);
4483 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4484 "Unable to resolve authority for remaining %s!\n", rh->name);
4485 strcpy(nah->result, "");
4486 finish_get_auth (rh, nah);
4494 * Tries to resolve the authority for name
4497 * @param zone the root zone to look up for
4498 * @param pzone the private local zone
4499 * @param name the name to lookup up
4500 * @param proc the processor to call when finished
4501 * @param proc_cls the closure to pass to the processor
4504 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4505 struct GNUNET_CRYPTO_ShortHashCode pzone,
4507 GetAuthorityResultProcessor proc,
4510 struct ResolverHandle *rh;
4511 struct GetNameAuthorityHandle *nah;
4513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4514 "Starting authority resolution for %s!\n", name);
4516 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4517 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4518 rh->authority = zone;
4520 rh->private_local_zone = pzone;
4521 rh->namestore_task = NULL;
4523 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4525 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4527 strcpy(rh->name, "\0");
4532 strlen(name)-strlen(GNUNET_GNS_TLD));
4533 memcpy(rh->name, name,
4534 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4537 memset(nah->name, 0,
4539 strcpy(nah->name, name);
4541 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4542 rh->authority_chain_tail = rh->authority_chain_head;
4543 rh->authority_chain_head->zone = zone;
4544 rh->proc = &handle_delegation_result_ns_get_auth;
4545 rh->proc_cls = (void*)nah;
4548 nah->proc_cls = proc_cls;
4549 strcpy (nah->result, "");
4551 /* Start delegation resolution in our namestore */
4552 resolve_delegation_ns(rh);
4556 /******** END GET AUTHORITY *************/
4558 /* end of gnunet-service-gns_resolver.c */