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, "."))
173 * Determine if this name is canonical.
175 * a.b.gnunet = not canonical
178 * @param name the name to test
179 * @return 1 if canonical
182 is_canonical(char* name)
187 ndup = GNUNET_strdup (name);
190 for (tok = strtok (NULL, "."); tok != NULL; tok = strtok (NULL, "."))
206 * Callback that shortens authorities
208 * @param gph the handle containing the name to shorten
211 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
215 create_pseu_cont (void* cls, int32_t success, const char* emsg)
217 //FIXME do sth with error
218 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
219 struct AuthorityChain *iter;
221 gph->namestore_task = NULL;
224 iter = gph->ahead->next;
225 GNUNET_free (gph->ahead);
227 } while (iter != NULL);
228 GNUNET_CRYPTO_rsa_key_free (gph->key);
229 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
235 * Namestore calls this function if we have record for this name.
236 * (or with rd_count=0 to indicate no matches)
238 * @param cls the pending query
239 * @param key the key of the zone we did the lookup
240 * @param expiration expiration date of the namestore entry
241 * @param name the name for which we need an authority
242 * @param rd_count the number of records with 'name'
243 * @param rd the record data
244 * @param signature the signature of the authority for the record data
247 process_pseu_lookup_ns (void* cls,
248 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
249 struct GNUNET_TIME_Absolute expiration,
250 const char *name, unsigned int rd_count,
251 const struct GNUNET_NAMESTORE_RecordData *rd,
252 const struct GNUNET_CRYPTO_RsaSignature *signature)
254 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
255 struct GNUNET_NAMESTORE_RecordData new_pkey;
256 struct AuthorityChain *iter;
258 gph->namestore_task = NULL;
261 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
262 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
263 if (0 == strcmp (gph->name, name))
265 if (gph->ahead->next != NULL)
267 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
271 iter = gph->ahead->next;
272 GNUNET_free (gph->ahead);
274 shorten_authority_chain (gph);
282 iter = gph->ahead->next;
283 GNUNET_free (gph->ahead);
285 } while (iter != NULL);
286 GNUNET_CRYPTO_rsa_key_free (gph->key);
287 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "GNS_AUTO_PSEU: Trying delegated name %s\n", gph->name);
294 memcpy (gph->test_name, gph->name, strlen (gph->name)+1);
295 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
298 GNUNET_NAMESTORE_TYPE_ANY,
299 &process_pseu_lookup_ns,
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
308 new_pkey.expiration_time = UINT64_MAX;
309 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
310 new_pkey.data = &gph->ahead->zone;
311 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
312 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
313 | GNUNET_NAMESTORE_RF_PRIVATE
314 | GNUNET_NAMESTORE_RF_PENDING;
315 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
319 &create_pseu_cont, //cont
324 * process result of a dht pseu lookup
326 * @param gph the handle
327 * @param name the pseu result or NULL
330 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
334 memcpy (gph->test_name, gph->ahead->name, strlen (gph->ahead->name)+1);
338 memcpy (gph->test_name, name, strlen(name)+1);
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
346 * Check for collision
348 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
351 GNUNET_NAMESTORE_TYPE_ANY,
352 &process_pseu_lookup_ns,
357 * Handle timeout for dht request
359 * @param cls the request handle as closure
360 * @param tc the task context
363 handle_auth_discovery_timeout(void *cls,
364 const struct GNUNET_SCHEDULER_TaskContext *tc)
366 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
367 struct AuthorityChain *iter;
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
371 GNUNET_DHT_get_stop (gph->get_handle);
372 gph->get_handle = NULL;
374 if (gph->ahead->next != NULL)
376 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
380 iter = gph->ahead->next;
381 GNUNET_free (gph->ahead);
383 shorten_authority_chain (gph);
388 process_pseu_result (gph, NULL);
392 * Function called when we find a PSEU entry in the DHT
394 * @param cls the request handle
395 * @param exp lifetime
396 * @param key the key the record was stored under
397 * @param get_path get path
398 * @param get_path_length get path length
399 * @param put_path put path
400 * @param put_path_length put path length
401 * @param type the block type
402 * @param size the size of the record
403 * @param data the record data
406 process_auth_discovery_dht_result(void* cls,
407 struct GNUNET_TIME_Absolute exp,
408 const struct GNUNET_HashCode * key,
409 const struct GNUNET_PeerIdentity *get_path,
410 unsigned int get_path_length,
411 const struct GNUNET_PeerIdentity *put_path,
412 unsigned int put_path_length,
413 enum GNUNET_BLOCK_Type type,
414 size_t size, const void *data)
416 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
417 struct AuthorityChain *iter;
418 struct GNSNameRecordBlock *nrb;
419 char* rd_data = (char*)data;
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
429 /* stop lookup and timeout task */
430 GNUNET_DHT_get_stop (gph->get_handle);
431 gph->get_handle = NULL;
432 GNUNET_SCHEDULER_cancel (gph->timeout);
436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
437 "GNS_GET_AUTH: got dht result null!\n", size);
441 iter = gph->ahead->next;
442 GNUNET_free (gph->ahead);
444 } while (iter != NULL);
445 GNUNET_CRYPTO_rsa_key_free (gph->key);
446 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
451 nrb = (struct GNSNameRecordBlock*)data;
455 nrb = (struct GNSNameRecordBlock*)data;
457 name = (char*)&nrb[1];
458 num_records = ntohl (nrb->rd_count);
460 struct GNUNET_NAMESTORE_RecordData rd[num_records];
462 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
463 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
465 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
470 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
471 "GNS_GET_AUTH: Error deserializing data!\n");
475 for (i=0; i < num_records; i++)
477 if ((strcmp (name, "+") == 0) &&
478 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
481 process_pseu_result (gph, (char*)rd[i].data);
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in dht!\n");
490 if (gph->ahead->next != NULL)
492 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
496 iter = gph->ahead->next;
497 GNUNET_free (gph->ahead);
499 shorten_authority_chain (gph);
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
504 "GNS_GET_AUTH: finished shorten, no results!\n");
505 process_pseu_result (gph, NULL);
509 * Process PSEU discovery for shorten via namestore
511 * @param cls the GetPseuAuthorityHandle
512 * @param key the public key
513 * @param expiration recorddata expiration
514 * @param name the looked up name
515 * @param rd_count number of records in set
516 * @param rd record data
517 * @param signature the signature
520 process_auth_discovery_ns_result(void* cls,
521 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
522 struct GNUNET_TIME_Absolute expiration,
524 unsigned int rd_count,
525 const struct GNUNET_NAMESTORE_RecordData *rd,
526 const struct GNUNET_CRYPTO_RsaSignature *signature)
529 struct GNUNET_CRYPTO_ShortHashCode name_hash;
530 struct GNUNET_HashCode lookup_key;
531 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
532 struct GNUNET_HashCode name_hash_double;
533 struct GNUNET_HashCode zone_hash_double;
535 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
536 struct AuthorityChain *iter;
538 gph->namestore_task = NULL;
545 GNUNET_CRYPTO_short_hash ("+", strlen ("+"), &name_hash);
546 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
547 GNUNET_CRYPTO_short_hash_double (&gph->ahead->zone, &zone_hash_double);
548 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
549 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
553 "+", (char*)&lookup_key_string);
555 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
556 &handle_auth_discovery_timeout, gph);
558 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
560 GNUNET_assert (gph->get_handle == NULL);
562 gph->get_handle = GNUNET_DHT_get_start(dht_handle,
563 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
565 DHT_GNS_REPLICATION_LEVEL,
569 &process_auth_discovery_dht_result,
574 for (i=0; i < rd_count; i++)
576 if ((strcmp (name, "+") == 0) &&
577 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
580 process_pseu_result (gph, (char*)rd[i].data);
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
587 if (gph->ahead->next != NULL)
589 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
593 iter = gph->ahead->next;
594 GNUNET_free (gph->ahead);
596 shorten_authority_chain (gph);
601 process_pseu_result (gph, NULL);
605 * Callback called by namestore for a zone to name
608 * @param cls the closure
609 * @param zone_key the zone we queried
610 * @param expire the expiration time of the name
611 * @param name the name found or NULL
612 * @param rd_len number of records for the name
613 * @param rd the record data (PKEY) for the name
614 * @param signature the signature for the record data
617 process_zone_to_name_discover (void *cls,
618 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
619 struct GNUNET_TIME_Absolute expire,
622 const struct GNUNET_NAMESTORE_RecordData *rd,
623 const struct GNUNET_CRYPTO_RsaSignature *signature)
625 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
626 struct AuthorityChain *iter;
628 gph->namestore_task = NULL;
629 /* we found a match in our own zone */
632 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
633 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
638 iter = gph->ahead->next;
639 GNUNET_free (gph->ahead);
641 } while (iter != NULL);
642 GNUNET_CRYPTO_rsa_key_free (gph->key);
643 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
649 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
652 GNUNET_GNS_RECORD_PSEU,
653 &process_auth_discovery_ns_result,
661 * Callback that shortens authorities
663 * @param gph the handle to the shorten request
666 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "GNS_AUTO_PSEU: New authority %s discovered\n",
673 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
676 &process_zone_to_name_discover,
682 start_shorten (struct AuthorityChain *atail,
683 struct GNUNET_CRYPTO_RsaPrivateKey *key)
685 struct AuthorityChain *new_head = NULL;
686 struct AuthorityChain *new_tail = NULL;
687 struct AuthorityChain *iter;
688 struct AuthorityChain *acopy;
689 struct GetPseuAuthorityHandle *gph;
690 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
691 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
693 /* First copy the authority chain in reverse order */
694 for (iter = atail; iter != NULL; iter = iter->prev)
696 acopy = GNUNET_malloc (sizeof (struct AuthorityChain));
697 memcpy (acopy, iter, sizeof (struct AuthorityChain));
700 GNUNET_CONTAINER_DLL_insert (new_head, new_tail, acopy);
703 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
705 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
706 pb_key = GNUNET_CRYPTO_rsa_encode_key (key);
707 gph->key = GNUNET_CRYPTO_rsa_decode_key ((char*)pb_key, ntohs (pb_key->len));
708 //gph->key = key;//GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
709 //memcpy (gph->key, key, sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
711 GNUNET_CRYPTO_short_hash (&pkey,
712 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
714 gph->ahead = new_head;
716 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
718 shorten_authority_chain (gph);
722 * Initialize the resolver
724 * @param nh the namestore handle
725 * @param dh the dht handle
726 * @param lz the local zone's hash
727 * @param c configuration handle
728 * @param max_bg_queries maximum number of parallel background queries in dht
729 * @param ignore_pending ignore records that still require user confirmation
731 * @return GNUNET_OK on success
734 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
735 struct GNUNET_DHT_Handle *dh,
736 struct GNUNET_CRYPTO_ShortHashCode lz,
737 const struct GNUNET_CONFIGURATION_Handle *c,
738 unsigned long long max_bg_queries,
742 namestore_handle = nh;
746 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
748 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
749 max_allowed_background_queries = max_bg_queries;
750 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
752 ignore_pending_records = ignore_pending;
763 GNUNET_RESOLVER_connect (cfg);
766 if ((namestore_handle != NULL) && (dht_handle != NULL))
771 return GNUNET_SYSERR;
778 * @param cls closure to iterator
779 * @param node heap nodes
780 * @param element the namestorebgtask
781 * @param cost heap cost
782 * @return always GNUNET_YES
785 cleanup_pending_ns_tasks(void* cls,
786 struct GNUNET_CONTAINER_HeapNode *node,
788 GNUNET_CONTAINER_HeapCostType cost)
790 struct NamestoreBGTask *nbg = (struct NamestoreBGTask *)element;
791 ResolverCleanupContinuation cont = cls;
793 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
794 "GNS_CLEANUP: Terminating ns task\n");
795 GNUNET_NAMESTORE_cancel (nbg->qe);
797 GNUNET_CONTAINER_heap_remove_node(node);
799 if (GNUNET_CONTAINER_heap_get_size(ns_task_heap) == 0)
809 * @param rh resolver handle
810 * @param rlh record lookup handle
811 * @param rd_count number of results
815 finish_lookup (struct ResolverHandle *rh,
816 struct RecordLookupHandle* rlh,
817 unsigned int rd_count,
818 const struct GNUNET_NAMESTORE_RecordData *rd);
822 * Cleanup background lookups FIXME get rid of this??
824 * @param cls closure to iterator
825 * @param node heap nodes
826 * @param element the resolver handle
827 * @param cost heap cost
828 * @return always GNUNET_YES
831 cleanup_pending_background_queries (void* cls,
832 struct GNUNET_CONTAINER_HeapNode *node,
834 GNUNET_CONTAINER_HeapCostType cost)
836 struct ResolverHandle *rh = (struct ResolverHandle *)element;
837 ResolverCleanupContinuation cont = cls;
839 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
840 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
843 //finish_lookup (rh, rh->proc_cls, 0, NULL);
844 //rh->get_handle = NULL;
845 //rh->proc(rh->proc_cls, rh, 0, NULL);
847 GNUNET_CONTAINER_heap_remove_node(node);
849 if (GNUNET_CONTAINER_heap_get_size(dht_lookup_heap) == 0)
851 if (GNUNET_CONTAINER_heap_get_size(ns_task_heap) == 0)
855 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
856 &cleanup_pending_ns_tasks,
867 * Helper function to free resolver handle
869 * @param rh the handle to free
872 free_resolver_handle (struct ResolverHandle* rh)
874 struct AuthorityChain *ac;
875 struct AuthorityChain *ac_next;
880 ac = rh->authority_chain_head;
889 if (NULL != rh->get_handle)
890 GNUNET_DHT_get_stop (rh->get_handle);
892 if (NULL != rh->dns_raw_packet)
893 GNUNET_free (rh->dns_raw_packet);
895 if (NULL != rh->namestore_task)
896 GNUNET_NAMESTORE_cancel (rh->namestore_task);
897 rh->namestore_task = NULL;
899 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
900 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
902 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
903 GNUNET_SCHEDULER_cancel (rh->timeout_task);
905 if (NULL != rh->dns_sock)
906 GNUNET_NETWORK_socket_close (rh->dns_sock);
907 if (NULL != rh->dns_resolver_handle)
908 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
910 if (NULL != rh->rd.data)
911 GNUNET_free ((void*)(rh->rd.data));
919 * @param rh resolver handle
920 * @param nsh name shorten handle
923 finish_shorten (struct ResolverHandle *rh,
924 struct NameShortenHandle *nsh);
928 * @param rh resolver handle
929 * @param nah get name authority handle
932 finish_get_auth (struct ResolverHandle *rh,
933 struct GetNameAuthorityHandle* rlh);
938 gns_resolver_cleanup (ResolverCleanupContinuation cont)
941 struct GetPseuAuthorityHandle *tmp;
942 struct AuthorityChain *iter;
946 for (tmp = gph_head; tmp != NULL; tmp = gph_head)
948 if (tmp->get_handle != NULL)
949 GNUNET_DHT_get_stop (tmp->get_handle);
950 tmp->get_handle = NULL;
951 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
952 GNUNET_SCHEDULER_cancel (tmp->timeout);
953 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
955 if (NULL != tmp->namestore_task)
956 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
957 tmp->namestore_task = NULL;
962 iter = tmp->ahead->next;
963 GNUNET_free (tmp->ahead);
965 } while (iter != NULL);
967 GNUNET_CRYPTO_rsa_key_free (tmp->key);
968 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, tmp);
972 while (NULL != rlh_head)
974 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
977 while (NULL != nsh_head)
979 finish_shorten (nsh_head, nsh_head->proc_cls);
982 while (NULL != nah_head)
984 finish_get_auth (nah_head, nah_head->proc_cls);
987 s = GNUNET_CONTAINER_heap_get_size(dht_lookup_heap);
988 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
989 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
991 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
992 &cleanup_pending_background_queries,
994 else if (0 != GNUNET_CONTAINER_heap_get_size(ns_task_heap))
996 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
997 &cleanup_pending_ns_tasks,
1008 * Callback when record data is put into namestore
1010 * @param cls the closure
1011 * @param success GNUNET_OK on success
1012 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
1015 on_namestore_record_put_result(void *cls,
1019 struct NamestoreBGTask *nbg = cls;
1021 GNUNET_CONTAINER_heap_remove_node (nbg->node);
1024 if (GNUNET_NO == success)
1026 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1027 "GNS_NS: records already in namestore\n");
1030 else if (GNUNET_YES == success)
1032 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1033 "GNS_NS: records successfully put in namestore\n");
1037 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1038 "GNS_NS: Error putting records into namestore: %s\n", emsg);
1042 handle_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1044 struct ResolverHandle *rh = cls;
1046 if (rh->timeout_cont)
1047 rh->timeout_cont(rh->timeout_cont_cls, tc);
1051 * Processor for background lookups in the DHT
1053 * @param cls closure (NULL)
1054 * @param rd_count number of records found (not 0)
1055 * @param rd record data
1058 background_lookup_result_processor(void *cls,
1060 const struct GNUNET_NAMESTORE_RecordData *rd)
1062 //We could do sth verbose/more useful here but it doesn't make any difference
1063 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1064 "GNS_BG: background dht lookup finished. (%d results)\n",
1069 * Handle timeout for DHT requests
1071 * @param cls the request handle as closure
1072 * @param tc the task context
1075 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1077 struct ResolverHandle *rh = cls;
1078 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1079 char new_name[MAX_DNS_NAME_LENGTH];
1081 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1082 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
1083 rh->id, rh->name, rh->timeout.rel_value);
1085 * Start resolution in bg
1087 //strcpy(new_name, rh->name);
1088 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1089 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
1090 rh->name, GNUNET_GNS_TLD);
1092 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1093 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
1094 rh->id, new_name, rlh->record_type);
1096 gns_resolver_lookup_record(rh->authority,
1097 rh->private_local_zone,
1101 GNUNET_TIME_UNIT_FOREVER_REL,
1103 &background_lookup_result_processor,
1105 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1107 GNUNET_DHT_get_stop (rh->get_handle);
1108 rh->get_handle = NULL;
1109 rh->proc(rh->proc_cls, rh, 0, NULL);
1114 * Function called when we get a result from the dht
1115 * for our record query
1117 * @param cls the request handle
1118 * @param exp lifetime
1119 * @param key the key the record was stored under
1120 * @param get_path get path
1121 * @param get_path_length get path length
1122 * @param put_path put path
1123 * @param put_path_length put path length
1124 * @param type the block type
1125 * @param size the size of the record
1126 * @param data the record data
1129 process_record_result_dht(void* cls,
1130 struct GNUNET_TIME_Absolute exp,
1131 const struct GNUNET_HashCode * key,
1132 const struct GNUNET_PeerIdentity *get_path,
1133 unsigned int get_path_length,
1134 const struct GNUNET_PeerIdentity *put_path,
1135 unsigned int put_path_length,
1136 enum GNUNET_BLOCK_Type type,
1137 size_t size, const void *data)
1139 struct ResolverHandle *rh;
1140 struct RecordLookupHandle *rlh;
1141 struct GNSNameRecordBlock *nrb;
1142 uint32_t num_records;
1144 char* rd_data = (char*)data;
1148 rh = (struct ResolverHandle *)cls;
1149 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1150 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
1152 //FIXME maybe check expiration here, check block type
1155 rlh = (struct RecordLookupHandle *) rh->proc_cls;
1156 nrb = (struct GNSNameRecordBlock*)data;
1158 /* stop lookup and timeout task */
1159 GNUNET_DHT_get_stop (rh->get_handle);
1160 rh->get_handle = NULL;
1162 if (rh->dht_heap_node != NULL)
1164 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
1165 rh->dht_heap_node = NULL;
1168 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1170 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1171 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1174 rh->get_handle = NULL;
1175 name = (char*)&nrb[1];
1176 num_records = ntohl(nrb->rd_count);
1178 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1179 struct NamestoreBGTask *ns_heap_root;
1180 struct NamestoreBGTask *namestore_bg_task;
1182 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1183 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
1185 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1190 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1191 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1195 for (i=0; i<num_records; i++)
1197 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1198 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1199 rh->id, name, rh->name);
1200 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1201 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1202 rh->id, rd[i].record_type, rlh->record_type);
1203 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1204 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1205 rh->id, rd[i].data_size);
1206 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1207 "GNS_PHASE_REC-%llu: Got flag %d\n",
1208 rh->id, rd[i].flags);
1210 if ((strcmp(name, rh->name) == 0) &&
1211 (rd[i].record_type == rlh->record_type))
1219 * FIXME check pubkey against existing key in namestore?
1220 * https://gnunet.org/bugs/view.php?id=2179
1222 if (max_allowed_ns_tasks <=
1223 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1225 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1226 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1228 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1229 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1233 /* Save to namestore */
1234 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1235 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1242 &on_namestore_record_put_result, //cont
1245 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1247 GNUNET_TIME_absolute_get().abs_value);
1251 rh->proc(rh->proc_cls, rh, num_records, rd);
1253 rh->proc(rh->proc_cls, rh, 0, NULL);
1260 * Start DHT lookup for a (name -> query->record_type) record in
1261 * rh->authority's zone
1263 * @param rh the pending gns query context
1266 resolve_record_dht (struct ResolverHandle *rh)
1269 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1270 struct GNUNET_HashCode lookup_key;
1271 struct GNUNET_HashCode name_hash_double;
1272 struct GNUNET_HashCode zone_hash_double;
1273 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
1274 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1275 struct ResolverHandle *rh_heap_root;
1277 GNUNET_CRYPTO_short_hash(rh->name, strlen(rh->name), &name_hash);
1278 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1279 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
1280 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
1281 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
1283 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1284 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1285 rh->id, rh->name, (char*)&lookup_key_string);
1287 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1288 rh->dht_heap_node = NULL;
1290 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1293 * Update timeout if necessary
1295 if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
1298 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1299 "GNS_PHASE_REC-%llu: Adjusting timeout\n", rh->id);
1301 * Set timeout for authority lookup phase to 1/2
1303 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
1304 GNUNET_TIME_relative_divide(rh->timeout, 2),
1305 &handle_lookup_timeout,
1308 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1309 // &dht_lookup_timeout,
1311 rh->timeout_cont = &dht_lookup_timeout;
1312 rh->timeout_cont_cls = rh;
1316 if (max_allowed_background_queries <=
1317 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1319 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1320 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
1321 rh_heap_root->get_handle = NULL;
1322 rh_heap_root->dht_heap_node = NULL;
1324 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1325 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1326 rh->id, rh_heap_root->name);
1327 rh_heap_root->proc(rh_heap_root->proc_cls,
1332 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1334 GNUNET_TIME_absolute_get().abs_value);
1337 xquery = htonl(rlh->record_type);
1339 GNUNET_assert(rh->get_handle == NULL);
1340 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1341 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1343 DHT_GNS_REPLICATION_LEVEL,
1347 &process_record_result_dht,
1354 * Namestore calls this function if we have record for this name.
1355 * (or with rd_count=0 to indicate no matches)
1357 * @param cls the pending query
1358 * @param key the key of the zone we did the lookup
1359 * @param expiration expiration date of the namestore entry
1360 * @param name the name for which we need an authority
1361 * @param rd_count the number of records with 'name'
1362 * @param rd the record data
1363 * @param signature the signature of the authority for the record data
1366 process_record_result_ns (void* cls,
1367 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1368 struct GNUNET_TIME_Absolute expiration,
1369 const char *name, unsigned int rd_count,
1370 const struct GNUNET_NAMESTORE_RecordData *rd,
1371 const struct GNUNET_CRYPTO_RsaSignature *signature)
1373 struct ResolverHandle *rh;
1374 struct RecordLookupHandle *rlh;
1375 struct GNUNET_TIME_Relative remaining_time;
1376 struct GNUNET_CRYPTO_ShortHashCode zone;
1377 struct GNUNET_TIME_Absolute et;
1380 rh = (struct ResolverHandle *) cls;
1381 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1383 rh->namestore_task = NULL;
1384 GNUNET_CRYPTO_short_hash(key,
1385 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1387 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1395 rh->status |= RSL_RECORD_EXISTS;
1397 if (remaining_time.rel_value == 0)
1399 rh->status |= RSL_RECORD_EXPIRED;
1406 * Lookup terminated and no results
1408 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1409 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1412 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1413 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1416 * Our zone and no result? Cannot resolve TT
1418 rh->proc(rh->proc_cls, rh, 0, NULL);
1424 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1425 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1427 for (i=0; i<rd_count;i++)
1429 if (rd[i].record_type != rlh->record_type)
1432 if (ignore_pending_records &&
1433 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1435 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1436 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1441 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1442 et.abs_value = rd[i].expiration_time;
1443 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
1446 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1447 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1457 if (rh->answered == 0)
1459 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1460 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1461 rh->proc(rh->proc_cls, rh, 0, NULL);
1465 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1466 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1467 rh->id, rh->answered, rd_count);
1469 rh->proc(rh->proc_cls, rh, rd_count, rd);
1475 * VPN redirect result callback
1477 * @param cls the resolver handle
1478 * @param af the requested address family
1479 * @param address in_addr(6) respectively
1482 process_record_result_vpn (void* cls, int af, const void *address)
1484 struct ResolverHandle *rh = cls;
1485 struct RecordLookupHandle *rlh;
1486 struct GNUNET_NAMESTORE_RecordData rd;
1488 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1490 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1491 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1495 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1496 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1498 if (rlh->record_type != GNUNET_GNS_RECORD_A)
1500 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1501 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1503 rh->proc (rh->proc_cls, rh, 0, NULL);
1506 rd.record_type = GNUNET_GNS_RECORD_A;
1507 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1509 rd.data_size = sizeof (struct in_addr);
1511 rh->proc (rh->proc_cls, rh, 1, &rd);
1514 else if (af == AF_INET6)
1516 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1517 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1519 if (rlh->record_type != GNUNET_GNS_RECORD_AAAA)
1521 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1522 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1524 rh->proc (rh->proc_cls, rh, 0, NULL);
1527 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1528 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1530 rd.data_size = sizeof (struct in6_addr);
1532 rh->proc (rh->proc_cls, rh, 1, &rd);
1536 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1537 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1539 rh->proc (rh->proc_cls, rh, 0, NULL);
1546 * Process VPN lookup result for record
1548 * @param cls the record lookup handle
1549 * @param rh resolver handle
1550 * @param rd_count number of results (1)
1551 * @param rd record data containing the result
1554 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1555 unsigned int rd_count,
1556 const struct GNUNET_NAMESTORE_RecordData *rd)
1558 struct RecordLookupHandle* rlh = (struct RecordLookupHandle*) cls;
1562 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1563 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1566 /* give up, cannot resolve */
1567 finish_lookup(rh, rlh, 0, NULL);
1571 /* results found yay */
1572 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1573 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1575 finish_lookup(rh, rlh, rd_count, rd);
1580 * Sends a UDP dns query to a nameserver specified in the rh
1582 * @param rh the resolver handle
1585 send_dns_packet (struct ResolverHandle *rh);
1589 handle_dns_resolver (void *cls,
1590 const struct sockaddr *addr,
1593 struct ResolverHandle *rh = cls;
1594 struct RecordLookupHandle *rlh = rh->proc_cls;
1595 struct GNUNET_NAMESTORE_RecordData rd;
1596 struct sockaddr_in *sai;
1597 struct sockaddr_in6 *sai6;
1601 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1602 "No address found in DNS!\n");
1603 finish_lookup (rh, rlh, 0, NULL);
1607 if (addrlen == sizeof (struct sockaddr_in))
1609 sai = (struct sockaddr_in*) addr;
1610 rd.record_type = GNUNET_GNS_RECORD_A;
1611 rd.data_size = sizeof (struct in_addr);
1612 rd.data = &sai->sin_addr;
1616 sai6 = (struct sockaddr_in6*) addr;
1617 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1618 rd.data_size = sizeof (struct in6_addr);
1619 rd.data = &sai6->sin6_addr;
1622 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1625 finish_lookup (rh, rlh, 1, &rd);
1630 * Resolve DNS name via local stub resolver
1632 * @param rh the resolver handle
1635 resolve_dns_name (struct ResolverHandle *rh)
1637 struct RecordLookupHandle *rlh = rh->proc_cls;
1640 if ((rlh->record_type != GNUNET_GNS_RECORD_A) &&
1641 (rlh->record_type != GNUNET_GNS_RECORD_AAAA))
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1644 "Can only resolve A/AAAA via stub... abort\n");
1645 finish_lookup (rh, rlh, 0, NULL);
1649 if (rlh->record_type == GNUNET_GNS_RECORD_A)
1654 //GNUNET_RESOLVER_connect (cfg); FIXME into init
1656 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1659 &handle_dns_resolver,
1665 * Read DNS udp packet from socket
1667 * @param cls the resolver handle
1668 * @param tc task context
1671 read_dns_response (void *cls,
1672 const struct GNUNET_SCHEDULER_TaskContext *tc)
1674 struct ResolverHandle *rh = cls;
1675 struct RecordLookupHandle *rlh = rh->proc_cls;
1676 char buf[UINT16_MAX];
1678 struct sockaddr_in addr;
1680 struct GNUNET_DNSPARSER_Packet *packet;
1681 struct GNUNET_NAMESTORE_RecordData rd;
1682 int found_delegation = GNUNET_NO;
1683 int found_cname = GNUNET_NO;
1684 char* delegation_name = NULL;
1685 int zone_offset = 0;
1688 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1689 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1691 /* timeout or shutdown */
1692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1693 "Terminating DNS query %d\n", tc->reason);
1694 finish_lookup (rh, rlh, 0, NULL);
1698 addrlen = sizeof (addr);
1699 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1701 (struct sockaddr*) &addr,
1706 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1707 finish_lookup (rh, rlh, 0, NULL);
1711 packet = GNUNET_DNSPARSER_parse (buf, r);
1715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1716 "Failed to parse DNS reply!\n");
1717 finish_lookup (rh, rlh, 0, NULL);
1721 for (i = 0; i < packet->num_answers; i++)
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1724 "Got record type %d (want %d)\n",
1725 packet->answers[i].type,
1727 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1728 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1731 "CNAME... restarting query with %s\n",
1732 packet->answers[i].data.hostname
1734 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1735 found_cname = GNUNET_YES;
1736 //send_dns_packet (rh);
1737 //GNUNET_DNSPARSER_free_packet (packet);
1741 if ((packet->answers[i].type == rlh->record_type) &&
1742 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1746 rd.data = packet->answers[i].data.raw.data;
1747 rd.data_size = packet->answers[i].data.raw.data_len;
1748 rd.record_type = packet->answers[i].type;
1750 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1751 finish_lookup (rh, rlh, 1, &rd);
1752 GNUNET_DNSPARSER_free_packet (packet);
1757 if (GNUNET_YES == found_cname)
1759 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1761 if (0 > zone_offset)
1764 /* restart query with CNAME */
1765 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1768 "Asking same server for %s\n", rh->dns_name);
1769 send_dns_packet (rh);
1773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1774 "Trying system resolver for %s\n", rh->dns_name);
1775 resolve_dns_name (rh);
1778 GNUNET_DNSPARSER_free_packet (packet);
1782 for (i = 0; i < packet->num_authority_records; i++)
1785 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1788 "Found NS delegation!\n");
1789 found_delegation = GNUNET_YES;
1790 delegation_name = packet->authority_records[i].data.hostname;
1795 for (i = 0; i < packet->num_additional_records; i++)
1797 if (found_delegation == GNUNET_NO)
1800 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1801 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1803 GNUNET_assert (sizeof (struct in_addr) ==
1804 packet->authority_records[i].data.raw.data_len);
1806 rh->dns_addr.sin_addr =
1807 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1808 send_dns_packet (rh);
1809 GNUNET_DNSPARSER_free_packet (packet);
1814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1815 "Nothing useful in DNS reply!\n");
1816 finish_lookup (rh, rlh, 0, NULL);
1817 GNUNET_DNSPARSER_free_packet (packet);
1822 * Sends a UDP dns query to a nameserver specified in the rh
1824 * @param rh the request handle
1827 send_dns_packet (struct ResolverHandle *rh)
1829 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1830 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1833 "Sending %dbyte DNS query\n",
1834 rh->dns_raw_packet_size);
1836 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1838 rh->dns_raw_packet_size,
1839 (struct sockaddr*)&rh->dns_addr,
1840 sizeof (struct sockaddr_in));
1842 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1843 rh->timeout, //FIXME less?
1849 GNUNET_NETWORK_fdset_destroy (rset);
1854 * The final phase of resoution.
1855 * We found a NS RR and want to resolve via DNS
1857 * @param rh the pending lookup handle
1858 * @param rd_count length of record data
1859 * @param rd record data containing VPN RR
1862 resolve_record_dns (struct ResolverHandle *rh,
1864 const struct GNUNET_NAMESTORE_RecordData *rd)
1866 struct GNUNET_DNSPARSER_Query query;
1867 struct GNUNET_DNSPARSER_Packet packet;
1868 struct GNUNET_DNSPARSER_Flags flags;
1869 struct in_addr dnsip;
1870 struct sockaddr_in addr;
1871 struct sockaddr *sa;
1873 struct RecordLookupHandle *rlh = rh->proc_cls;
1875 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1877 /* We cancel here as to not include the ns lookup in the timeout */
1878 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1880 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1881 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1883 /* Start shortening */
1884 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1887 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1889 start_shorten (rh->authority_chain_tail,
1893 for (i = 0; i < rd_count; i++)
1895 /* Synthesize dns name */
1896 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
1898 strcpy (rh->dns_zone, (char*)rd[i].data);
1899 if (0 == strcmp (rh->name, ""))
1900 strcpy (rh->dns_name, (char*)rd[i].data);
1902 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1905 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
1906 dnsip = *((struct in_addr*)rd[i].data);
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1910 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1914 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1915 if (rh->dns_sock == NULL)
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1918 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1920 finish_lookup (rh, rlh, 0, NULL);
1924 memset (&addr, 0, sizeof (struct sockaddr_in));
1925 sa = (struct sockaddr *) &addr;
1926 sa->sa_family = AF_INET;
1927 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1929 sizeof (struct sockaddr_in)))
1931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1932 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1934 finish_lookup (rh, rlh, 0, NULL);
1938 /*TODO create dnsparser query, serialize, sendto, handle reply*/
1939 query.name = rh->dns_name;
1940 query.type = rlh->record_type;
1941 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1942 memset (&flags, 0, sizeof (flags));
1943 flags.recursion_desired = 1;
1944 flags.checking_disabled = 1;
1945 packet.queries = &query;
1946 packet.answers = NULL;
1947 packet.authority_records = NULL;
1948 packet.num_queries = 1;
1949 packet.num_answers = 0;
1950 packet.num_authority_records = 0;
1951 packet.num_additional_records = 0;
1952 packet.flags = flags;
1954 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1956 &rh->dns_raw_packet,
1957 &rh->dns_raw_packet_size))
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1960 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1962 GNUNET_NETWORK_socket_close (rh->dns_sock);
1963 finish_lookup (rh, rlh, 0, NULL);
1967 rh->dns_addr.sin_family = AF_INET;
1968 rh->dns_addr.sin_port = htons (53); //domain
1969 rh->dns_addr.sin_addr = dnsip;
1970 #if HAVE_SOCKADDR_IN_SIN_LEN
1971 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1974 send_dns_packet (rh);
1979 * The final phase of resoution.
1980 * We found a VPN RR and want to request an IPv4/6 address
1982 * @param rh the pending lookup handle
1983 * @param rd_count length of record data
1984 * @param rd record data containing VPN RR
1987 resolve_record_vpn (struct ResolverHandle *rh,
1989 const struct GNUNET_NAMESTORE_RecordData *rd)
1991 struct RecordLookupHandle *rlh = rh->proc_cls;
1992 struct GNUNET_HashCode serv_desc;
1993 struct vpn_data* vpn;
1996 /* We cancel here as to not include the ns lookup in the timeout */
1997 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1999 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2000 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2002 /* Start shortening */
2003 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2006 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
2008 start_shorten (rh->authority_chain_tail,
2012 vpn = (struct vpn_data*)rd->data;
2015 GNUNET_CRYPTO_hash ((char*)&vpn[1],
2016 strlen ((char*)&vpn[1]) + 1,
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2019 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
2022 GNUNET_h2s (&vpn->peer));
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2025 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
2028 GNUNET_h2s (&serv_desc));
2029 rh->proc = &handle_record_vpn;
2031 if (NULL == vpn_handle)
2033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2034 "GNS_PHASE_REC_VPN-%llu: VPN not connected!\n",
2036 finish_lookup (rh, rh->proc_cls, 0, NULL);
2040 if (rlh->record_type == GNUNET_GNS_RECORD_A)
2046 if (NULL == vpn_handle)
2048 vpn_handle = GNUNET_VPN_connect (cfg);
2049 if (NULL == vpn_handle)
2051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2052 "GNS_PHASE_INIT: Error connecting to VPN!\n");
2053 finish_lookup (rh, rh->proc_cls, 0, NULL);
2059 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
2060 af, ntohs (vpn->proto),
2061 (struct GNUNET_PeerIdentity *)&vpn->peer,
2064 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
2065 &process_record_result_vpn,
2071 * The final phase of resolution.
2072 * rh->name is a name that is canonical and we do not have a delegation.
2073 * Query namestore for this record
2075 * @param rh the pending lookup handle
2078 resolve_record_ns(struct ResolverHandle *rh)
2080 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
2082 /* We cancel here as to not include the ns lookup in the timeout */
2083 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2085 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2086 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2088 /* Start shortening */
2089 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2092 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
2094 start_shorten (rh->authority_chain_tail,
2099 * Try to resolve this record in our namestore.
2100 * The name to resolve is now in rh->authority_name
2101 * since we tried to resolve it to an authority
2104 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
2108 &process_record_result_ns,
2115 * Handle timeout for DHT requests
2117 * @param cls the request handle as closure
2118 * @param tc the task context
2121 dht_authority_lookup_timeout(void *cls,
2122 const struct GNUNET_SCHEDULER_TaskContext *tc)
2124 struct ResolverHandle *rh = cls;
2125 struct RecordLookupHandle *rlh = rh->proc_cls;
2126 char new_name[MAX_DNS_NAME_LENGTH];
2128 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2129 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
2130 rh->id, rh->authority_name, rh->timeout.rel_value);
2132 rh->status |= RSL_TIMED_OUT;
2134 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2136 if (NULL != rh->get_handle)
2137 GNUNET_DHT_get_stop (rh->get_handle);
2139 rh->get_handle = NULL;
2141 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
2143 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2144 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
2146 rh->proc(rh->proc_cls, rh, 0, NULL);
2150 if (strcmp(rh->name, "") == 0)
2153 * promote authority back to name and try to resolve record
2155 strcpy(rh->name, rh->authority_name);
2156 rh->proc(rh->proc_cls, rh, 0, NULL);
2161 * Start resolution in bg
2163 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
2164 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
2165 //strcpy(new_name, rh->name);
2166 //strcpy(new_name+strlen(new_name), ".");
2167 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
2169 strcpy(rh->name, new_name);
2171 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2172 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2173 rh->id, rh->name, rlh->record_type);
2175 gns_resolver_lookup_record(rh->authority,
2176 rh->private_local_zone,
2180 GNUNET_TIME_UNIT_FOREVER_REL,
2182 &background_lookup_result_processor,
2185 rh->proc(rh->proc_cls, rh, 0, NULL);
2189 static void resolve_delegation_dht(struct ResolverHandle *rh);
2192 static void resolve_delegation_ns(struct ResolverHandle *rh);
2196 * Namestore resolution for delegation finished. Processing result.
2198 * @param cls the closure
2199 * @param rh resolver handle
2200 * @param rd_count number of results (always 0)
2201 * @param rd record data (always NULL)
2204 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2205 unsigned int rd_count,
2206 const struct GNUNET_NAMESTORE_RecordData *rd);
2210 * This is a callback function that checks for key revocation
2212 * @param cls the pending query
2213 * @param key the key of the zone we did the lookup
2214 * @param expiration expiration date of the record data set in the namestore
2215 * @param name the name for which we need an authority
2216 * @param rd_count the number of records with 'name'
2217 * @param rd the record data
2218 * @param signature the signature of the authority for the record data
2221 process_pkey_revocation_result_ns (void *cls,
2222 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2223 struct GNUNET_TIME_Absolute expiration,
2225 unsigned int rd_count,
2226 const struct GNUNET_NAMESTORE_RecordData *rd,
2227 const struct GNUNET_CRYPTO_RsaSignature *signature)
2229 struct ResolverHandle *rh = cls;
2230 struct GNUNET_TIME_Relative remaining_time;
2233 rh->namestore_task = NULL;
2234 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2236 for (i = 0; i < rd_count; i++)
2238 if (rd[i].record_type == GNUNET_GNS_RECORD_REV)
2240 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2241 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2243 rh->status |= RSL_PKEY_REVOKED;
2244 rh->proc (rh->proc_cls, rh, 0, NULL);
2249 if ((name == NULL) ||
2250 (remaining_time.rel_value == 0))
2252 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2253 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2256 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2258 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2259 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2260 rh->id, "+.gnunet", GNUNET_GNS_RECORD_REV);
2262 gns_resolver_lookup_record(rh->authority,
2263 rh->private_local_zone,
2264 GNUNET_GNS_RECORD_REV,
2267 GNUNET_TIME_UNIT_FOREVER_REL,
2269 &background_lookup_result_processor,
2273 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2274 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2277 * We are done with PKEY resolution if name is empty
2278 * else resolve again with new authority
2280 if (strcmp (rh->name, "") == 0)
2281 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2283 resolve_delegation_ns (rh);
2290 * Callback when record data is put into namestore
2292 * @param cls the closure
2293 * @param success GNUNET_OK on success
2294 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2297 on_namestore_delegation_put_result(void *cls,
2301 struct NamestoreBGTask *nbg = cls;
2303 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2306 if (GNUNET_NO == success)
2308 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2309 "GNS_NS: records already in namestore\n");
2312 else if (GNUNET_YES == success)
2314 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2315 "GNS_NS: records successfully put in namestore\n");
2319 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2320 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2324 * Function called when we get a result from the dht
2325 * for our query. Recursively tries to resolve authorities
2328 * @param cls the request handle
2329 * @param exp lifetime
2330 * @param key the key the record was stored under
2331 * @param get_path get path
2332 * @param get_path_length get path length
2333 * @param put_path put path
2334 * @param put_path_length put path length
2335 * @param type the block type
2336 * @param size the size of the record
2337 * @param data the record data
2340 process_delegation_result_dht(void* cls,
2341 struct GNUNET_TIME_Absolute exp,
2342 const struct GNUNET_HashCode * key,
2343 const struct GNUNET_PeerIdentity *get_path,
2344 unsigned int get_path_length,
2345 const struct GNUNET_PeerIdentity *put_path,
2346 unsigned int put_path_length,
2347 enum GNUNET_BLOCK_Type type,
2348 size_t size, const void *data)
2350 struct ResolverHandle *rh;
2351 struct GNSNameRecordBlock *nrb;
2352 uint32_t num_records;
2354 char* rd_data = (char*) data;
2357 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
2358 struct GNUNET_HashCode zone_hash_double, name_hash_double;
2360 rh = (struct ResolverHandle *)cls;
2362 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2363 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2368 nrb = (struct GNSNameRecordBlock*)data;
2370 /* stop dht lookup and timeout task */
2371 GNUNET_DHT_get_stop (rh->get_handle);
2373 rh->get_handle = NULL;
2375 if (rh->dht_heap_node != NULL)
2377 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2378 rh->dht_heap_node = NULL;
2381 num_records = ntohl(nrb->rd_count);
2382 name = (char*)&nrb[1];
2384 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2385 struct NamestoreBGTask *ns_heap_root;
2386 struct NamestoreBGTask *namestore_bg_task;
2388 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2389 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2391 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2396 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2397 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2402 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2403 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2404 rh->id, name, rh->authority_name);
2405 for (i=0; i<num_records; i++)
2408 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2409 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2410 rh->id, name, rh->authority_name);
2411 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2412 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2413 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2414 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2415 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2416 rh->id, rd[i].data_size);
2417 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2418 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2419 rh->id, rd[i].flags);
2421 if ((rd[i].record_type == GNUNET_GNS_RECORD_VPN) ||
2422 (rd[i].record_type == GNUNET_GNS_RECORD_NS) ||
2423 (rd[i].record_type == GNUNET_GNS_RECORD_CNAME))
2426 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2428 if (strcmp(rh->name, "") == 0)
2429 strcpy(rh->name, rh->authority_name);
2431 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2432 rh->name, rh->authority_name); //FIXME ret
2437 if ((strcmp(name, rh->authority_name) == 0) &&
2438 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
2440 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2441 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2444 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2445 struct AuthorityChain *auth =
2446 GNUNET_malloc(sizeof(struct AuthorityChain));
2447 auth->zone = rh->authority;
2448 memset(auth->name, 0, strlen(rh->authority_name)+1);
2449 strcpy(auth->name, rh->authority_name);
2450 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2451 rh->authority_chain_tail,
2454 if (NULL != rh->rd.data)
2455 GNUNET_free ((void*)rh->rd.data);
2457 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2458 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2459 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2462 /** try to import pkey if private key available */
2463 //if (rh->priv_key && is_canonical (rh->name))
2464 // process_discovered_authority(name, auth->zone,
2465 // rh->authority_chain_tail->zone,
2472 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2473 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2474 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2475 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2477 /* Save to namestore */
2478 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
2481 if (max_allowed_ns_tasks <=
2482 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2484 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2485 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2487 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2488 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
2492 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2494 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2496 GNUNET_TIME_absolute_get().abs_value);
2497 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2504 &on_namestore_delegation_put_result, //cont
2505 namestore_bg_task); //cls
2514 * FIXME in this case. should we ask namestore again?
2516 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2517 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2518 rh->id, rh->authority_name, rh->name);
2520 if (strcmp(rh->name, "") == 0)
2522 /* Start shortening */
2523 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2526 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2528 start_shorten (rh->authority_chain_tail,
2533 rh->proc = &handle_delegation_ns;
2536 /* Check for key revocation and delegate */
2537 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2540 GNUNET_GNS_RECORD_REV,
2541 &process_pkey_revocation_result_ns,
2548 * No pkey but name exists
2551 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2552 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2553 rh->id, rh->authority_name, rh->name);
2554 if (strcmp(rh->name, "") == 0)
2555 strcpy(rh->name, rh->authority_name);
2557 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2558 rh->name, rh->authority_name); //FIXME ret
2560 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2561 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2562 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2563 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2565 rh->proc(rh->proc_cls, rh, 0, NULL);
2568 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2569 +(MAX_DNS_NAME_LENGTH*2)
2570 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2571 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2575 expand_plus(char* dest, char* src, char* repl)
2578 unsigned int s_len = strlen(src)+1;
2580 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2581 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2585 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2586 "GNS_POSTPROCESS: %s to short\n", src);
2588 /* no postprocessing */
2589 memcpy(dest, src, s_len+1);
2593 if (0 == strcmp(src+s_len-3, ".+"))
2595 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2596 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2597 memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2601 pos += strlen(repl);
2602 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2603 "GNS_POSTPROCESS: Expanded to %s\n", dest);
2607 memcpy(dest, src, s_len+1);
2615 finish_lookup (struct ResolverHandle *rh,
2616 struct RecordLookupHandle* rlh,
2617 unsigned int rd_count,
2618 const struct GNUNET_NAMESTORE_RecordData *rd)
2621 char new_rr_data[MAX_DNS_NAME_LENGTH];
2622 char new_mx_data[MAX_MX_LENGTH];
2623 char new_soa_data[MAX_SOA_LENGTH];
2624 char new_srv_data[MAX_SRV_LENGTH];
2625 struct srv_data *old_srv;
2626 struct srv_data *new_srv;
2627 struct soa_data *old_soa;
2628 struct soa_data *new_soa;
2629 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2632 unsigned int offset;
2634 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2636 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2637 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2640 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2643 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2645 for (i = 0; i < rd_count; i++)
2648 if (rd[i].record_type != GNUNET_GNS_RECORD_NS &&
2649 rd[i].record_type != GNUNET_GNS_RECORD_PTR &&
2650 rd[i].record_type != GNUNET_GNS_RECORD_CNAME &&
2651 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
2652 rd[i].record_type != GNUNET_GNS_RECORD_SOA &&
2653 rd[i].record_type != GNUNET_GNS_RECORD_SRV)
2655 p_rd[i].data = rd[i].data;
2660 * for all those records we 'should'
2661 * also try to resolve the A/AAAA records (RFC1035)
2662 * This is a feature and not important
2665 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2666 "GNS_POSTPROCESS: Postprocessing\n");
2668 if (strcmp(rh->name, "+") == 0)
2669 repl_string = rlh->name;
2671 repl_string = rlh->name+strlen(rh->name)+1;
2674 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
2676 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2677 offset = sizeof(uint16_t);
2678 pos = new_mx_data+offset;
2679 expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2681 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2682 p_rd[i].data = new_mx_data;
2683 p_rd[i].data_size = offset;
2685 else if (rd[i].record_type == GNUNET_GNS_RECORD_SRV)
2688 * Prio, weight and port
2690 new_srv = (struct srv_data*)new_srv_data;
2691 old_srv = (struct srv_data*)rd[i].data;
2692 new_srv->prio = old_srv->prio;
2693 new_srv->weight = old_srv->weight;
2694 new_srv->port = old_srv->port;
2695 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2697 p_rd[i].data = new_srv_data;
2698 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2700 else if (rd[i].record_type == GNUNET_GNS_RECORD_SOA)
2702 /* expand mname and rname */
2703 old_soa = (struct soa_data*)rd[i].data;
2704 new_soa = (struct soa_data*)new_soa_data;
2705 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2706 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2707 offset = strlen ((char*)&new_soa[1]) + 1;
2708 expand_plus((char*)&new_soa[1] + offset,
2709 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2711 p_rd[i].data_size = sizeof (struct soa_data)
2713 + strlen ((char*)&new_soa[1] + offset);
2714 p_rd[i].data = new_soa_data;
2719 expand_plus(pos, (char*)rd[i].data, repl_string);
2720 p_rd[i].data_size = strlen(new_rr_data)+1;
2721 p_rd[i].data = new_rr_data;
2726 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2728 free_resolver_handle (rh);
2732 * Process DHT lookup result for record.
2734 * @param cls the closure
2735 * @param rh resolver handle
2736 * @param rd_count number of results
2737 * @param rd record data
2740 handle_record_dht(void* cls, struct ResolverHandle *rh,
2741 unsigned int rd_count,
2742 const struct GNUNET_NAMESTORE_RecordData *rd)
2744 struct RecordLookupHandle* rlh;
2746 rlh = (struct RecordLookupHandle*)cls;
2749 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2750 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2752 /* give up, cannot resolve */
2753 finish_lookup(rh, rlh, 0, NULL);
2757 /* results found yay */
2758 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2759 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2761 finish_lookup(rh, rlh, rd_count, rd);
2769 * Process namestore lookup result for record.
2771 * @param cls the closure
2772 * @param rh resolver handle
2773 * @param rd_count number of results
2774 * @param rd record data
2777 handle_record_ns (void* cls, struct ResolverHandle *rh,
2778 unsigned int rd_count,
2779 const struct GNUNET_NAMESTORE_RecordData *rd)
2781 struct RecordLookupHandle* rlh;
2782 rlh = (struct RecordLookupHandle*) cls;
2783 int check_dht = GNUNET_YES;
2787 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2788 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2793 * There are 5 conditions that have to met for us to consult the DHT:
2794 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2795 * 2. No entry in the NS existed AND
2796 * 3. The zone queried is not the local resolver's zone AND
2797 * 4. The name that was looked up is '+'
2798 * because if it was any other canonical name we either already queried
2799 * the DHT for the authority in the authority lookup phase (and thus
2800 * would already have an entry in the NS for the record)
2801 * 5. We are not in cache only mode
2803 if (((rh->status & RSL_RECORD_EXPIRED) != 0) &&
2804 ((rh->status & RSL_RECORD_EXISTS) == 0) )
2805 check_dht = GNUNET_NO;
2807 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2808 &rh->private_local_zone))
2809 check_dht = GNUNET_NO;
2811 if ((strcmp (rh->name, "+") != 0) && (is_srv (rh->name) != 0))
2812 check_dht = GNUNET_NO;
2815 if (rh->only_cached == GNUNET_YES)
2816 check_dht = GNUNET_NO;
2818 if (GNUNET_YES == check_dht)
2820 rh->proc = &handle_record_dht;
2821 resolve_record_dht(rh);
2824 /* give up, cannot resolve */
2825 finish_lookup(rh, rlh, 0, NULL);
2829 /* results found yay */
2830 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2831 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2833 finish_lookup(rh, rlh, rd_count, rd);
2840 * Move one level up in the domain hierarchy and return the
2841 * passed top level domain.
2843 * @param name the domain
2844 * @param dest the destination where the tld will be put
2847 pop_tld(char* name, char* dest)
2851 if (is_canonical (name))
2858 for (len = strlen(name); len > 0; len--)
2860 if (*(name+len) == '.')
2870 strcpy(dest, (name+len+1));
2874 * Checks if name is in tld
2876 * @param name the name to check
2877 * @param tld the TLD to check for
2878 * @return GNUNET_YES or GNUNET_NO
2881 is_tld(const char* name, const char* tld)
2885 if (strlen(name) <= strlen(tld))
2890 offset = strlen(name)-strlen(tld);
2891 if (strcmp(name+offset, tld) != 0)
2893 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2894 "%s is not in .%s TLD\n", name, tld);
2901 * DHT resolution for delegation finished. Processing result.
2903 * @param cls the closure
2904 * @param rh resolver handle
2905 * @param rd_count number of results (always 0)
2906 * @param rd record data (always NULL)
2909 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2910 unsigned int rd_count,
2911 const struct GNUNET_NAMESTORE_RecordData *rd)
2913 struct RecordLookupHandle* rlh;
2914 rlh = (struct RecordLookupHandle*) cls;
2917 if (strcmp(rh->name, "") == 0)
2919 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
2921 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2922 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2924 finish_lookup(rh, rlh, rd_count, rd);
2927 /* We resolved full name for delegation. resolving record */
2928 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2929 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2931 strcpy(rh->name, "+\0");
2932 rh->proc = &handle_record_ns;
2933 resolve_record_ns(rh);
2938 * we still have some left
2940 if (is_canonical(rh->name))
2942 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2943 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2946 rh->proc = &handle_record_ns;
2947 resolve_record_ns(rh);
2950 /* give up, cannot resolve */
2951 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2952 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2954 finish_lookup(rh, rlh, 0, NULL);
2959 * Start DHT lookup for a name -> PKEY (compare NS) record in
2960 * rh->authority's zone
2962 * @param rh the pending gns query
2965 resolve_delegation_dht(struct ResolverHandle *rh)
2968 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2969 struct GNUNET_HashCode name_hash_double;
2970 struct GNUNET_HashCode zone_hash_double;
2971 struct GNUNET_HashCode lookup_key;
2972 struct ResolverHandle *rh_heap_root;
2974 pop_tld(rh->name, rh->authority_name);
2975 GNUNET_CRYPTO_short_hash(rh->authority_name,
2976 strlen(rh->authority_name),
2978 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2979 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2980 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2982 rh->dht_heap_node = NULL;
2984 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2986 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
2987 // &dht_authority_lookup_timeout,
2989 rh->timeout_cont = &dht_authority_lookup_timeout;
2990 rh->timeout_cont_cls = rh;
2994 if (max_allowed_background_queries <=
2995 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2997 /* terminate oldest lookup */
2998 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2999 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
3000 rh_heap_root->get_handle = NULL;
3001 rh_heap_root->dht_heap_node = NULL;
3003 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3004 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
3005 rh->id, rh_heap_root->authority_name);
3007 rh_heap_root->proc(rh_heap_root->proc_cls,
3012 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
3014 GNUNET_TIME_absolute_get().abs_value);
3017 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
3019 GNUNET_assert(rh->get_handle == NULL);
3020 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
3021 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
3023 DHT_GNS_REPLICATION_LEVEL,
3027 &process_delegation_result_dht,
3034 * Namestore resolution for delegation finished. Processing result.
3036 * @param cls the closure
3037 * @param rh resolver handle
3038 * @param rd_count number of results (always 0)
3039 * @param rd record data (always NULL)
3042 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
3043 unsigned int rd_count,
3044 const struct GNUNET_NAMESTORE_RecordData *rd)
3046 struct RecordLookupHandle* rlh;
3047 rlh = (struct RecordLookupHandle*) cls;
3048 int check_dht = GNUNET_YES;
3051 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3052 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
3053 rh->id, rh->status);
3055 if (rh->status & RSL_PKEY_REVOKED)
3057 finish_lookup (rh, rlh, 0, NULL);
3061 if (strcmp(rh->name, "") == 0)
3064 /* We resolved full name for delegation. resolving record */
3065 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3066 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
3068 if (rh->status & RSL_CNAME_FOUND)
3070 if (rlh->record_type == GNUNET_GNS_RECORD_CNAME)
3072 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3073 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
3075 strcpy (rh->name, rh->authority_name);
3076 finish_lookup (rh, rlh, rd_count, rd);
3081 if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
3083 s_len = strlen (rd->data) - 2;
3084 memcpy (rh->name, rd->data, s_len);
3085 rh->name[s_len] = '\0';
3086 resolve_delegation_ns (rh);
3089 else if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
3091 gns_resolver_lookup_record (rh->authority,
3092 rh->private_local_zone,
3101 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3102 free_resolver_handle (rh);
3108 strcpy (rh->dns_name, (char*)rd->data);
3109 resolve_dns_name (rh);
3114 else if (rh->status & RSL_DELEGATE_VPN)
3116 if (rlh->record_type == GNUNET_GNS_RECORD_VPN)
3118 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3119 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
3121 finish_lookup(rh, rlh, rd_count, rd);
3124 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3125 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
3127 GNUNET_assert (NULL != rd);
3128 rh->proc = &handle_record_vpn;
3129 resolve_record_vpn (rh, rd_count, rd);
3132 else if (rh->status & RSL_DELEGATE_NS)
3134 if (rlh->record_type == GNUNET_GNS_RECORD_NS)
3136 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3137 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3139 finish_lookup(rh, rlh, rd_count, rd);
3143 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3144 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3146 GNUNET_assert (NULL != rd);
3147 rh->proc = &handle_record_ns;
3148 resolve_record_dns (rh, rd_count, rd);
3151 else if (rh->status & RSL_DELEGATE_PKEY)
3153 if (rh->status & RSL_PKEY_REVOKED)
3155 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3156 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
3158 finish_lookup (rh, rlh, 0, NULL);
3161 else if (rlh->record_type == GNUNET_GNS_RECORD_PKEY)
3163 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3164 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
3166 finish_lookup(rh, rlh, rd_count, rd);
3170 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3171 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3173 strcpy(rh->name, "+\0");
3174 rh->proc = &handle_record_ns;
3175 resolve_record_ns(rh);
3179 if (rh->status & RSL_DELEGATE_NS)
3181 if (rlh->record_type == GNUNET_GNS_RECORD_NS)
3183 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3184 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3186 finish_lookup(rh, rlh, rd_count, rd);
3190 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3191 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3193 GNUNET_assert (NULL != rd);
3194 rh->proc = &handle_record_ns;
3195 resolve_record_dns (rh, rd_count, rd);
3200 * we still have some left
3201 * check if authority in ns is fresh
3203 * or we are authority
3206 if ((rh->status & RSL_RECORD_EXISTS) &&
3207 !(rh->status & RSL_RECORD_EXPIRED))
3208 check_dht = GNUNET_NO;
3210 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3211 &rh->private_local_zone))
3212 check_dht = GNUNET_NO;
3214 if (rh->only_cached == GNUNET_YES)
3215 check_dht = GNUNET_NO;
3217 if (check_dht == GNUNET_NO)
3219 if (is_canonical(rh->name))
3221 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3222 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3225 rh->proc = &handle_record_ns;
3226 resolve_record_ns(rh);
3230 /* give up, cannot resolve */
3231 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3232 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3235 finish_lookup(rh, rlh, rd_count, rd);
3236 //rlh->proc(rlh->proc_cls, 0, NULL);
3241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3242 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3244 rh->proc = &handle_delegation_dht;
3245 resolve_delegation_dht(rh);
3250 * This is a callback function that should give us only PKEY
3251 * records. Used to query the namestore for the authority (PKEY)
3252 * for 'name'. It will recursively try to resolve the
3253 * authority for a given name from the namestore.
3255 * @param cls the pending query
3256 * @param key the key of the zone we did the lookup
3257 * @param expiration expiration date of the record data set in the namestore
3258 * @param name the name for which we need an authority
3259 * @param rd_count the number of records with 'name'
3260 * @param rd the record data
3261 * @param signature the signature of the authority for the record data
3264 process_delegation_result_ns (void* cls,
3265 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3266 struct GNUNET_TIME_Absolute expiration,
3268 unsigned int rd_count,
3269 const struct GNUNET_NAMESTORE_RecordData *rd,
3270 const struct GNUNET_CRYPTO_RsaSignature *signature)
3272 struct ResolverHandle *rh;
3273 struct GNUNET_TIME_Relative remaining_time;
3274 struct GNUNET_CRYPTO_ShortHashCode zone;
3275 char new_name[MAX_DNS_NAME_LENGTH];
3277 struct GNUNET_TIME_Absolute et;
3279 rh = (struct ResolverHandle *)cls;
3280 rh->namestore_task = NULL;
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3282 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3285 GNUNET_CRYPTO_short_hash (key,
3286 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3288 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3295 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3297 rh->status |= RSL_RECORD_EXISTS;
3299 if (remaining_time.rel_value == 0)
3301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3302 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3304 rh->status |= RSL_RECORD_EXPIRED;
3309 * No authority found in namestore.
3314 * We did not find an authority in the namestore
3319 * Promote this authority back to a name maybe it is
3322 if (strcmp (rh->name, "") == 0)
3324 /* simply promote back */
3325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3326 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3327 rh->id, rh->authority_name);
3328 strcpy (rh->name, rh->authority_name);
3332 /* add back to existing name */
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3334 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3335 rh->id, rh->authority_name, rh->name);
3336 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3337 rh->name, rh->authority_name);
3338 strcpy (rh->name, new_name);
3339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3340 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3344 rh->proc (rh->proc_cls, rh, 0, NULL);
3349 * We found an authority that may be able to help us
3350 * move on with query
3351 * Note only 1 pkey should have been returned.. anything else would be strange
3353 for (i=0; i < rd_count;i++)
3357 * A CNAME. Like regular DNS this means the is no other record for this
3360 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3362 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3363 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3366 rh->status |= RSL_CNAME_FOUND;
3367 rh->proc (rh->proc_cls, rh, rd_count, rd);
3374 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3376 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3377 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3379 rh->status |= RSL_DELEGATE_VPN;
3380 rh->proc (rh->proc_cls, rh, rd_count, rd);
3386 * FIXME make optional
3388 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3390 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3391 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3393 rh->status |= RSL_DELEGATE_NS;
3394 rh->proc (rh->proc_cls, rh, rd_count, rd);
3398 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3401 rh->status |= RSL_DELEGATE_PKEY;
3403 if ((ignore_pending_records != 0) &&
3404 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3407 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3413 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3414 et.abs_value = rd[i].expiration_time;
3415 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3419 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3421 if (remaining_time.rel_value == 0)
3423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3424 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3426 rh->authority_chain_head->fresh = 0;
3427 rh->proc (rh->proc_cls, rh, 0, NULL);
3435 * Resolve rest of query with new authority
3437 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3438 memcpy (&rh->authority, rd[i].data,
3439 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3440 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3441 auth->zone = rh->authority;
3442 memset (auth->name, 0, strlen (rh->authority_name)+1);
3443 strcpy (auth->name, rh->authority_name);
3444 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3445 rh->authority_chain_tail,
3447 if (NULL != rh->rd.data)
3448 GNUNET_free ((void*)(rh->rd.data));
3450 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3451 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3452 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3454 /* Check for key revocation and delegate */
3455 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3458 GNUNET_GNS_RECORD_REV,
3459 &process_pkey_revocation_result_ns,
3468 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3469 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3471 * If we have found some records for the LAST label
3472 * we return the results. Else null.
3474 if (strcmp(rh->name, "") == 0)
3476 /* Start shortening */
3477 if ((rh->priv_key != NULL) && is_canonical (rh->name))
3479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3480 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3482 start_shorten (rh->authority_chain_tail,
3485 /* simply promote back */
3486 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3487 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3488 rh->id, rh->authority_name);
3489 strcpy(rh->name, rh->authority_name);
3490 rh->proc(rh->proc_cls, rh, rd_count, rd);
3494 rh->proc(rh->proc_cls, rh, 0, NULL);
3500 * Resolve the delegation chain for the request in our namestore
3502 * @param rh the resolver handle
3505 resolve_delegation_ns (struct ResolverHandle *rh)
3507 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3508 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3510 pop_tld(rh->name, rh->authority_name);
3511 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3514 GNUNET_GNS_RECORD_ANY,
3515 &process_delegation_result_ns,
3522 * Lookup of a record in a specific zone
3523 * calls lookup result processor on result
3525 * @param zone the root zone
3526 * @param pzone the private local zone
3527 * @param record_type the record type to look up
3528 * @param name the name to look up
3529 * @param key a private key for use with PSEU import (can be NULL)
3530 * @param timeout timeout for resolution
3531 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3532 * @param proc the processor to call on result
3533 * @param cls the closure to pass to proc
3536 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3537 struct GNUNET_CRYPTO_ShortHashCode pzone,
3538 uint32_t record_type,
3540 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3541 struct GNUNET_TIME_Relative timeout,
3543 RecordLookupProcessor proc,
3546 struct ResolverHandle *rh;
3547 struct RecordLookupHandle* rlh;
3548 char string_hash[MAX_DNS_LABEL_LENGTH];
3549 char nzkey[MAX_DNS_LABEL_LENGTH];
3550 char* nzkey_ptr = nzkey;
3552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3553 "Starting resolution for %s (type=%d)!\n",
3557 if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
3559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3560 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3565 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3566 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3568 memset (rh, 0, sizeof (struct ResolverHandle));
3569 rh->authority = zone;
3573 rh->timeout = timeout;
3574 rh->get_handle = NULL;
3575 rh->private_local_zone = pzone;
3576 rh->only_cached = only_cached;
3577 rh->namestore_task = NULL;
3580 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3585 "No shorten key for resolution\n");
3588 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3591 * Set timeout for authority lookup phase to 1/2
3593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3594 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3595 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3596 GNUNET_TIME_relative_divide(timeout, 2),
3597 &handle_lookup_timeout,
3599 rh->timeout_cont = &dht_authority_lookup_timeout;
3600 rh->timeout_cont_cls = rh;
3604 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3605 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3608 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3611 * Only 'gnunet' given
3613 strcpy(rh->name, "\0");
3617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3618 "Checking for TLD...\n");
3619 if (is_zkey_tld(name) == GNUNET_YES)
3621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3624 * This is a zkey tld
3625 * build hash and use as initial authority
3628 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3629 memcpy(rh->name, name,
3630 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3631 pop_tld(rh->name, string_hash);
3633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3634 "ZKEY is %s!\n", string_hash);
3636 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3638 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3642 "Cannot convert ZKEY %s to hash!\n", string_hash);
3644 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3647 proc (cls, 0, NULL);
3652 else if (is_gnunet_tld (name) == GNUNET_YES)
3654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3657 * Presumably GNUNET tld
3659 memset (rh->name, 0,
3660 strlen(name)-strlen(GNUNET_GNS_TLD));
3661 memcpy (rh->name, name,
3662 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3666 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3667 "Cannot handle this TLD %s\n", string_hash);
3669 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3670 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3671 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3674 proc (cls, 0, NULL);
3680 * Initialize authority chain
3682 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3683 rh->authority_chain_head->prev = NULL;
3684 rh->authority_chain_head->next = NULL;
3685 rh->authority_chain_tail = rh->authority_chain_head;
3686 rh->authority_chain_head->zone = rh->authority;
3687 strcpy (rh->authority_chain_head->name, "");
3690 * Copy original query into lookup handle
3692 rlh->record_type = record_type;
3693 memset(rlh->name, 0, strlen(name) + 1);
3694 strcpy(rlh->name, name);
3696 rlh->proc_cls = cls;
3698 rh->proc = &handle_delegation_ns;
3699 resolve_delegation_ns(rh);
3702 /******** END Record Resolver ***********/
3705 finish_shorten (struct ResolverHandle *rh,
3706 struct NameShortenHandle *nsh)
3708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3709 "Sending %s as shorten result\n", nsh->result);
3710 nsh->proc (nsh->proc_cls, nsh->result);
3711 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3713 free_resolver_handle (rh);
3718 * Callback calles by namestore for a zone to name
3721 * @param cls the closure
3722 * @param zone_key the zone we queried
3723 * @param expire the expiration time of the name
3724 * @param name the name found or NULL
3725 * @param rd_len number of records for the name
3726 * @param rd the record data (PKEY) for the name
3727 * @param signature the signature for the record data
3730 process_zone_to_name_shorten_root (void *cls,
3731 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3732 struct GNUNET_TIME_Absolute expire,
3734 unsigned int rd_len,
3735 const struct GNUNET_NAMESTORE_RecordData *rd,
3736 const struct GNUNET_CRYPTO_RsaSignature *signature);
3740 * Callback called by namestore for a zone to name
3743 * @param cls the closure
3744 * @param zone_key the zone we queried
3745 * @param expire the expiration time of the name
3746 * @param name the name found or NULL
3747 * @param rd_len number of records for the name
3748 * @param rd the record data (PKEY) for the name
3749 * @param signature the signature for the record data
3752 process_zone_to_name_shorten_shorten (void *cls,
3753 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3754 struct GNUNET_TIME_Absolute expire,
3756 unsigned int rd_len,
3757 const struct GNUNET_NAMESTORE_RecordData *rd,
3758 const struct GNUNET_CRYPTO_RsaSignature *signature)
3760 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3761 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3762 struct AuthorityChain *next_authority;
3764 char result[MAX_DNS_NAME_LENGTH];
3765 char tmp_name[MAX_DNS_NAME_LENGTH];
3768 rh->namestore_task = NULL;
3769 /* we found a match in our own root zone */
3772 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3773 memset(result, 0, answer_len);
3775 if (strlen(rh->name) > 0)
3777 sprintf (result, "%s.%s.%s.%s",
3779 nsh->shorten_zone_name,
3784 sprintf (result, "%s.%s.%s", name,
3785 nsh->shorten_zone_name,
3789 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3790 "Found shorten result %s\n", result);
3791 if (strlen (nsh->result) > strlen (result))
3792 strcpy (nsh->result, result);
3794 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3795 nsh->shorten_zone) == 0)
3798 * This is our zone append .gnunet unless name is empty
3799 * (it shouldn't be, usually FIXME what happens if we
3800 * shorten to our zone to a "" record??)
3803 sprintf (result, "%s.%s.%s",
3805 nsh->shorten_zone_name,
3807 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3808 "Our zone: Found %s as shorten result\n", result);
3810 if (strlen (nsh->result) > strlen (result))
3811 strcpy (nsh->result, result);
3812 //nsh->proc(nsh->proc_cls, result);
3814 //free_resolver_handle(rh);
3821 * continue with next authority if exists
3823 if ((rh->authority_chain_head->next == NULL))
3825 finish_shorten (rh, nsh);
3828 next_authority = rh->authority_chain_head;
3830 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3831 "%s.%s", rh->name, next_authority->name);
3833 strcpy(rh->name, tmp_name);
3834 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3835 "No PSEU found for authority %s. Promoting back: %s\n",
3836 next_authority->name, rh->name);
3838 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3839 rh->authority_chain_tail,
3842 GNUNET_free (next_authority);
3844 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3845 &rh->authority_chain_tail->zone,
3846 &rh->authority_chain_head->zone,
3847 &process_zone_to_name_shorten_root,
3852 * Callback calles by namestore for a zone to name
3855 * @param cls the closure
3856 * @param zone_key the zone we queried
3857 * @param expire the expiration time of the name
3858 * @param name the name found or NULL
3859 * @param rd_len number of records for the name
3860 * @param rd the record data (PKEY) for the name
3861 * @param signature the signature for the record data
3864 process_zone_to_name_shorten_private (void *cls,
3865 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3866 struct GNUNET_TIME_Absolute expire,
3868 unsigned int rd_len,
3869 const struct GNUNET_NAMESTORE_RecordData *rd,
3870 const struct GNUNET_CRYPTO_RsaSignature *signature)
3872 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3873 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3874 struct AuthorityChain *next_authority;
3876 char result[MAX_DNS_NAME_LENGTH];
3877 char tmp_name[MAX_DNS_NAME_LENGTH];
3880 rh->namestore_task = NULL;
3881 /* we found a match in our own root zone */
3884 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3885 memset(result, 0, answer_len);
3887 if (strlen(rh->name) > 0)
3889 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3893 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3896 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3897 "Found shorten result %s\n", result);
3898 if (strlen (nsh->result) > strlen (result))
3899 strcpy (nsh->result, result);
3901 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3902 nsh->private_zone) == 0)
3905 * This is our zone append .gnunet unless name is empty
3906 * (it shouldn't be, usually FIXME what happens if we
3907 * shorten to our zone to a "" record??)
3910 sprintf (result, "%s.%s.%s",
3911 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3912 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3913 "Our private zone: Found %s as shorten result %s\n", result);
3914 if (strlen (nsh->result) > strlen (result))
3915 strcpy (nsh->result, result);
3918 if (0 != strcmp (nsh->shorten_zone_name, ""))
3920 /* backtrack authorities for names in priv zone */
3921 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3923 &rh->authority_chain_head->zone,
3924 &process_zone_to_name_shorten_shorten,
3931 * continue with next authority if exists
3933 if ((rh->authority_chain_head->next == NULL))
3935 finish_shorten (rh, nsh);
3938 next_authority = rh->authority_chain_head;
3940 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3941 "%s.%s", rh->name, next_authority->name);
3943 strcpy(rh->name, tmp_name);
3944 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3945 "No PSEU found for authority %s. Promoting back: %s\n",
3946 next_authority->name, rh->name);
3948 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3949 rh->authority_chain_tail,
3952 GNUNET_free (next_authority);
3954 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3955 &rh->authority_chain_tail->zone,
3956 &rh->authority_chain_head->zone,
3957 &process_zone_to_name_shorten_root,
3963 * Callback calles by namestore for a zone to name
3966 * @param cls the closure
3967 * @param zone_key the zone we queried
3968 * @param expire the expiration time of the name
3969 * @param name the name found or NULL
3970 * @param rd_len number of records for the name
3971 * @param rd the record data (PKEY) for the name
3972 * @param signature the signature for the record data
3975 process_zone_to_name_shorten_root (void *cls,
3976 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3977 struct GNUNET_TIME_Absolute expire,
3979 unsigned int rd_len,
3980 const struct GNUNET_NAMESTORE_RecordData *rd,
3981 const struct GNUNET_CRYPTO_RsaSignature *signature)
3983 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3984 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3985 struct AuthorityChain *next_authority;
3987 char result[MAX_DNS_NAME_LENGTH];
3988 char tmp_name[MAX_DNS_NAME_LENGTH];
3991 rh->namestore_task = NULL;
3992 /* we found a match in our own root zone */
3995 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3996 memset(result, 0, answer_len);
3998 if (strlen(rh->name) > 0)
4000 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
4004 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
4007 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4008 "Found shorten result %s\n", result);
4009 if (strlen (nsh->result) > strlen (result))
4010 strcpy (nsh->result, result);
4012 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4013 nsh->root_zone) == 0)
4016 * This is our zone append .gnunet unless name is empty
4017 * (it shouldn't be, usually FIXME what happens if we
4018 * shorten to our zone to a "" record??)
4021 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
4022 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4023 "Our zone: Found %s as shorten result\n", result);
4024 if (strlen (nsh->result) > strlen (result))
4025 strcpy (nsh->result, result);
4028 if (NULL != nsh->private_zone)
4030 /* backtrack authorities for names in priv zone */
4031 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4033 &rh->authority_chain_head->zone,
4034 &process_zone_to_name_shorten_private,
4037 else if (NULL != nsh->shorten_zone)
4039 /* backtrack authorities for names in shorten zone */
4040 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4042 &rh->authority_chain_head->zone,
4043 &process_zone_to_name_shorten_shorten,
4050 * continue with next authority if exists
4052 if ((rh->authority_chain_head->next == NULL))
4054 finish_shorten (rh, nsh);
4057 next_authority = rh->authority_chain_head;
4059 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
4060 "%s.%s", rh->name, next_authority->name);
4062 strcpy(rh->name, tmp_name);
4063 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4064 "No PSEU found for authority %s. Promoting back: %s\n",
4065 next_authority->name, rh->name);
4067 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
4068 rh->authority_chain_tail,
4071 GNUNET_free (next_authority);
4073 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4074 &rh->authority_chain_tail->zone,
4075 &rh->authority_chain_head->zone,
4076 &process_zone_to_name_shorten_root,
4083 * Process result from namestore delegation lookup
4084 * for shorten operation
4086 * @param cls the client shorten handle
4087 * @param rh the resolver handle
4088 * @param rd_count number of results (0)
4089 * @param rd data (NULL)
4092 handle_delegation_ns_shorten (void* cls,
4093 struct ResolverHandle *rh,
4095 const struct GNUNET_NAMESTORE_RecordData *rd)
4097 struct NameShortenHandle *nsh;
4098 char result[MAX_DNS_NAME_LENGTH];
4100 nsh = (struct NameShortenHandle *)cls;
4103 * At this point rh->name contains the part of the name
4104 * that we do not have a PKEY in our namestore to resolve.
4105 * The authority chain in the resolver handle is now
4106 * useful to backtrack if needed
4109 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4110 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4111 memset(result, 0, sizeof (result));
4113 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4114 nsh->root_zone) == 0)
4117 * This is our zone append .gnunet unless name is empty
4118 * (it shouldn't be, usually FIXME what happens if we
4119 * shorten to our zone to a "" record??)
4122 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
4123 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4124 "Our zone: Found %s as shorten result\n", result);
4126 if (strlen (nsh->result) > strlen (result))
4127 strcpy (nsh->result, result);
4130 else if (NULL != nsh->private_zone)
4133 * This is our zone append .gnunet unless name is empty
4134 * (it shouldn't be, usually FIXME what happens if we
4135 * shorten to our zone to a "" record??)
4137 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4138 nsh->private_zone) == 0)
4141 sprintf (result, "%s.%s.%s",
4142 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4143 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4144 "Our zone: Found %s as shorten result in private zone %s\n",
4147 if (strlen (nsh->result) > strlen (result))
4148 strcpy (nsh->result, result);
4151 else if (NULL != nsh->shorten_zone)
4154 * This is our zone append .gnunet unless name is empty
4155 * (it shouldn't be, usually FIXME what happens if we
4156 * shorten to our zone to a "" record??)
4158 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4159 nsh->shorten_zone) == 0)
4161 sprintf (result, "%s.%s.%s",
4162 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4163 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4164 "Our zone: Found %s as shorten result in shorten zone\n",
4167 if (strlen (nsh->result) > strlen (result))
4168 strcpy (nsh->result, result);
4173 /* backtrack authorities for names */
4174 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4176 &rh->authority_chain_head->zone,
4177 &process_zone_to_name_shorten_root,
4184 * Callback calles by namestore for a zone to name
4187 * @param cls the closure
4188 * @param zone_key the zone we queried
4189 * @param expire the expiration time of the name
4190 * @param name the name found or NULL
4191 * @param rd_len number of records for the name
4192 * @param rd the record data (PKEY) for the name
4193 * @param signature the signature for the record data
4196 process_zone_to_name_zkey(void *cls,
4197 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4198 struct GNUNET_TIME_Absolute expire,
4200 unsigned int rd_len,
4201 const struct GNUNET_NAMESTORE_RecordData *rd,
4202 const struct GNUNET_CRYPTO_RsaSignature *signature)
4204 struct ResolverHandle *rh = cls;
4205 struct NameShortenHandle *nsh = rh->proc_cls;
4206 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
4207 char new_name[MAX_DNS_NAME_LENGTH];
4209 /* zkey not in our zone */
4213 * In this case we have not given this PKEY a name (yet)
4214 * It is either just not in our zone or not even cached
4215 * Since we do not know at this point we will not try to shorten
4216 * because PKEY import will happen if the user follows the zkey
4219 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
4221 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4222 "No name found for zkey %s returning verbatim!\n", enc);
4223 if (strcmp(rh->name, "") != 0)
4224 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4225 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4227 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4228 enc, GNUNET_GNS_TLD_ZKEY);
4230 strcpy (nsh->result, new_name);
4232 finish_shorten (rh, nsh);
4236 if (strcmp(rh->name, "") != 0)
4237 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4240 strcpy(new_name, name);
4242 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4243 "Continue shorten for %s!\n", new_name);
4245 strcpy(rh->name, new_name);
4247 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4248 rh->authority_chain_tail = rh->authority_chain_head;
4249 rh->authority_chain_head->zone = rh->authority;
4252 /* Start delegation resolution in our namestore */
4253 resolve_delegation_ns (rh);
4258 * Shorten api from resolver
4260 * @param zone the root zone to use
4261 * @param pzone the private zone to use
4262 * @param szone the shorten zone to use
4263 * @param name the name to shorten
4264 * @param private_zone_name name of the private zone
4265 * @param shorten_zone_name name of the shorten zone
4266 * @param proc the processor to call with result
4267 * @param proc_cls closure to pass to proc
4270 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4271 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4272 struct GNUNET_CRYPTO_ShortHashCode *szone,
4274 const char* private_zone_name,
4275 const char* shorten_zone_name,
4276 ShortenResultProcessor proc,
4279 struct ResolverHandle *rh;
4280 struct NameShortenHandle *nsh;
4281 char string_hash[MAX_DNS_LABEL_LENGTH];
4282 struct GNUNET_CRYPTO_ShortHashCode zkey;
4283 char nzkey[MAX_DNS_LABEL_LENGTH];
4284 char* nzkey_ptr = nzkey;
4287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4288 "Starting shorten for %s!\n", name);
4290 if (is_canonical ((char*)name))
4292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4293 "%s is canonical. Returning verbatim\n", name);
4294 proc (proc_cls, name);
4298 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4301 nsh->proc_cls = proc_cls;
4302 nsh->root_zone = zone;
4303 nsh->private_zone = pzone;
4304 nsh->shorten_zone = szone;
4305 strcpy (nsh->private_zone_name, private_zone_name);
4306 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4307 strcpy (nsh->result, name);
4310 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4311 rh->authority = *zone;
4313 rh->priv_key = NULL;
4314 rh->namestore_task = NULL;
4315 rh->proc = &handle_delegation_ns_shorten;
4318 rh->private_local_zone = *zone;
4320 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4323 "Checking for TLD...\n");
4324 if (is_zkey_tld (name) == GNUNET_YES)
4326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4329 * This is a zkey tld
4330 * build hash and use as initial authority
4333 memset (rh->name, 0,
4334 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4335 memcpy (rh->name, name,
4336 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4337 pop_tld (rh->name, string_hash);
4339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4340 "ZKEY is %s!\n", string_hash);
4342 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4344 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4347 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4348 "Cannot convert ZKEY %s to hash!\n", nzkey);
4351 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4352 proc (proc_cls, name);
4356 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4359 &process_zone_to_name_zkey,
4364 else if (is_gnunet_tld (name) == GNUNET_YES)
4366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4369 * Presumably GNUNET tld
4371 memset (rh->name, 0,
4372 strlen (name)-strlen (GNUNET_GNS_TLD));
4373 memcpy (rh->name, name,
4374 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4378 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4381 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4382 proc (proc_cls, name);
4386 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4387 rh->authority_chain_tail = rh->authority_chain_head;
4388 rh->authority_chain_head->zone = *zone;
4391 /* Start delegation resolution in our namestore */
4392 resolve_delegation_ns (rh);
4395 /*********** END NAME SHORTEN ********************/
4398 * Conclude get authority lookup
4400 * @param rh resolver handle
4401 * @param nah get authority lookup handle
4404 finish_get_auth (struct ResolverHandle *rh,
4405 struct GetNameAuthorityHandle *nah)
4407 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4408 "Got authority result %s\n", nah->result);
4410 nah->proc(nah->proc_cls, nah->result);
4411 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4413 free_resolver_handle(rh);
4417 * Process result from namestore delegation lookup
4418 * for get authority operation
4420 * @param cls the client get auth handle
4421 * @param rh the resolver handle
4422 * @param rd_count number of results (0)
4423 * @param rd data (NULL)
4426 handle_delegation_result_ns_get_auth(void* cls,
4427 struct ResolverHandle *rh,
4429 const struct GNUNET_NAMESTORE_RecordData *rd)
4431 struct GetNameAuthorityHandle* nah;
4434 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4437 * At this point rh->name contains the part of the name
4438 * that we do not have a PKEY in our namestore to resolve.
4439 * The authority chain in the resolver handle is now
4440 * useful to backtrack if needed
4443 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4444 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4446 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4447 "Building response!\n");
4448 if (is_canonical(rh->name))
4451 * We successfully resolved the authority in the ns
4452 * FIXME for our purposes this is fine
4453 * but maybe we want to have an api that also looks
4454 * into the dht (i.e. option in message)
4456 if (strlen(rh->name) > strlen(nah->name))
4458 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4459 "Record name longer than original lookup name... odd!\n");
4463 answer_len = strlen(nah->name) - strlen(rh->name)
4464 + strlen(GNUNET_GNS_TLD) + 1;
4465 memset(nah->result, 0, answer_len);
4466 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4468 finish_get_auth (rh, nah);
4472 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4473 "Unable to resolve authority for remaining %s!\n", rh->name);
4474 strcpy(nah->result, "");
4475 finish_get_auth (rh, nah);
4483 * Tries to resolve the authority for name
4486 * @param zone the root zone to look up for
4487 * @param pzone the private local zone
4488 * @param name the name to lookup up
4489 * @param proc the processor to call when finished
4490 * @param proc_cls the closure to pass to the processor
4493 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4494 struct GNUNET_CRYPTO_ShortHashCode pzone,
4496 GetAuthorityResultProcessor proc,
4499 struct ResolverHandle *rh;
4500 struct GetNameAuthorityHandle *nah;
4502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4503 "Starting authority resolution for %s!\n", name);
4505 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4506 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4507 rh->authority = zone;
4509 rh->private_local_zone = pzone;
4510 rh->namestore_task = NULL;
4512 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4514 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4516 strcpy(rh->name, "\0");
4521 strlen(name)-strlen(GNUNET_GNS_TLD));
4522 memcpy(rh->name, name,
4523 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4526 memset(nah->name, 0,
4528 strcpy(nah->name, name);
4530 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4531 rh->authority_chain_tail = rh->authority_chain_head;
4532 rh->authority_chain_head->zone = zone;
4533 rh->proc = &handle_delegation_result_ns_get_auth;
4534 rh->proc_cls = (void*)nah;
4537 nah->proc_cls = proc_cls;
4538 strcpy (nah->result, "");
4540 /* Start delegation resolution in our namestore */
4541 resolve_delegation_ns(rh);
4545 /******** END GET AUTHORITY *************/
4547 /* end of gnunet-service-gns_resolver.c */