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_dns_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "gnunet_gns_service.h"
37 #include "block_gns.h"
39 #include "gnunet-service-gns_resolver.h"
41 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
42 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
43 #define DHT_GNS_REPLICATION_LEVEL 5
44 #define MAX_DNS_LABEL_LENGTH 63
48 * Our handle to the namestore service
50 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
53 * Resolver handle to the dht
55 static struct GNUNET_DHT_Handle *dht_handle;
58 * Heap for parallel DHT lookups
60 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
63 * Maximum amount of parallel queries in background
65 static unsigned long long max_allowed_background_queries;
70 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
73 * Namestore calls this function if we have record for this name.
74 * (or with rd_count=0 to indicate no matches)
76 * @param cls the pending query
77 * @param key the key of the zone we did the lookup
78 * @param expiration expiration date of the namestore entry
79 * @param name the name for which we need an authority
80 * @param rd_count the number of records with 'name'
81 * @param rd the record data
82 * @param signature the signature of the authority for the record data
85 process_pseu_lookup_ns(void* cls,
86 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
87 struct GNUNET_TIME_Absolute expiration,
88 const char *name, unsigned int rd_count,
89 const struct GNUNET_NAMESTORE_RecordData *rd,
90 const struct GNUNET_CRYPTO_RsaSignature *signature)
92 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
93 struct GNUNET_NAMESTORE_RecordData new_pkey;
97 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
98 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
99 if (0 == strcmp(gph->name, name))
101 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
102 "GNS_AUTO_PSEU: Intelligent replacement not implemented\n",
108 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
109 "GNS_AUTO_PSEU: Trying delegated name %s\n", gph->name);
110 memcpy(gph->new_name, gph->name, strlen(gph->name)+1);
111 GNUNET_NAMESTORE_lookup_record(namestore_handle,
114 GNUNET_GNS_RECORD_PSEU,
115 &process_pseu_lookup_ns,
121 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
122 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->new_name);
124 new_pkey.expiration = GNUNET_TIME_absolute_get_forever ();
125 new_pkey.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
126 new_pkey.data = &gph->new_zone;
127 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
128 GNUNET_NAMESTORE_record_create (namestore_handle,
139 * process result of a dht pseu lookup
141 * @param gph the handle
142 * @param name the pseu result or NULL
145 process_pseu_result(struct GetPseuAuthorityHandle* gph, char* name)
149 memcpy(gph->new_name, gph->name, strlen(gph->name)+1);
153 memcpy(gph->new_name, name, strlen(name)+1);
156 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
157 "GNS_AUTO_PSEU: Checking %s for collision in NS\n", gph->new_name);
160 * Check for collision
162 GNUNET_NAMESTORE_lookup_record(namestore_handle,
165 GNUNET_GNS_RECORD_PSEU,
166 &process_pseu_lookup_ns,
171 * Handle timeout for dht request
173 * @param cls the request handle as closure
174 * @param tc the task context
177 handle_auth_discovery_timeout(void *cls,
178 const struct GNUNET_SCHEDULER_TaskContext *tc)
180 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
182 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
183 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
184 GNUNET_DHT_get_stop (gph->get_handle);
185 gph->get_handle = NULL;
186 process_pseu_result(gph, NULL);
190 * Function called when we find a PSEU entry in the DHT
192 * @param cls the request handle
193 * @param exp lifetime
194 * @param key the key the record was stored under
195 * @param get_path get path
196 * @param get_path_length get path length
197 * @param put_path put path
198 * @param put_path_length put path length
199 * @param type the block type
200 * @param size the size of the record
201 * @param data the record data
204 process_auth_discovery_dht_result(void* cls,
205 struct GNUNET_TIME_Absolute exp,
206 const GNUNET_HashCode * key,
207 const struct GNUNET_PeerIdentity *get_path,
208 unsigned int get_path_length,
209 const struct GNUNET_PeerIdentity *put_path,
210 unsigned int put_path_length,
211 enum GNUNET_BLOCK_Type type,
212 size_t size, const void *data)
214 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
215 struct GNSNameRecordBlock *nrb;
216 char* rd_data = (char*)data;
222 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
223 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
227 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
228 "GNS_GET_AUTH: got dht result null!\n", size);
234 nrb = (struct GNSNameRecordBlock*)data;
236 /* stop lookup and timeout task */
237 GNUNET_DHT_get_stop (gph->get_handle);
238 gph->get_handle = NULL;
239 GNUNET_SCHEDULER_cancel(gph->timeout);
241 gph->get_handle = NULL;
243 nrb = (struct GNSNameRecordBlock*)data;
245 name = (char*)&nrb[1];
246 num_records = ntohl(nrb->rd_count);
248 struct GNUNET_NAMESTORE_RecordData rd[num_records];
250 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
251 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
253 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
258 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
259 "GNS_GET_AUTH: Error deserializing data!\n");
265 for (i=0; i<num_records; i++)
267 if ((strcmp(name, "+") == 0) &&
268 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
271 process_pseu_result(gph, (char*)rd[i].data);
277 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in dht!\n");
278 process_pseu_result(gph, NULL);
282 process_auth_discovery_ns_result(void* cls,
283 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
284 struct GNUNET_TIME_Absolute expiration,
285 const char *name, unsigned int rd_count,
286 const struct GNUNET_NAMESTORE_RecordData *rd,
287 const struct GNUNET_CRYPTO_RsaSignature *signature)
290 struct GNUNET_CRYPTO_ShortHashCode name_hash;
291 GNUNET_HashCode lookup_key;
292 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
293 GNUNET_HashCode name_hash_double;
294 GNUNET_HashCode zone_hash_double;
296 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
304 GNUNET_CRYPTO_short_hash("+", strlen("+"), &name_hash);
305 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
306 GNUNET_CRYPTO_short_hash_double (&gph->new_zone, &zone_hash_double);
307 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
308 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
310 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
311 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
312 "+", (char*)&lookup_key_string);
314 gph->timeout = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
315 &handle_auth_discovery_timeout, gph);
317 xquery = htonl(GNUNET_GNS_RECORD_PSEU);
319 GNUNET_assert(gph->get_handle == NULL);
320 gph->get_handle = GNUNET_DHT_get_start(dht_handle,
321 GNUNET_TIME_UNIT_FOREVER_REL,
322 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
324 DHT_GNS_REPLICATION_LEVEL,
328 &process_auth_discovery_dht_result,
332 for (i=0; i<rd_count; i++)
334 if ((strcmp(name, "+") == 0) &&
335 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
338 process_pseu_result(gph, (char*)rd[i].data);
345 * Callback called by namestore for a zone to name
348 * @param cls the closure
349 * @param zone_key the zone we queried
350 * @param expire the expiration time of the name
351 * @param name the name found or NULL
352 * @param rd_len number of records for the name
353 * @param rd the record data (PKEY) for the name
354 * @param signature the signature for the record data
357 process_zone_to_name_discover(void *cls,
358 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
359 struct GNUNET_TIME_Absolute expire,
362 const struct GNUNET_NAMESTORE_RecordData *rd,
363 const struct GNUNET_CRYPTO_RsaSignature *signature)
365 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
367 /* we found a match in our own zone */
370 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
371 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
377 GNUNET_NAMESTORE_lookup_record(namestore_handle,
380 GNUNET_GNS_RECORD_PSEU,
381 &process_auth_discovery_ns_result,
390 * Callback for new authories
392 * @param name the name given by delegation
393 * @param zone the authority
394 * @param our_zone our local zone
395 * @param key the private key of our authority
397 static void process_discovered_authority(char* name,
398 struct GNUNET_CRYPTO_ShortHashCode zone,
399 struct GNUNET_CRYPTO_ShortHashCode our_zone,
400 struct GNUNET_CRYPTO_RsaPrivateKey *key)
402 struct GetPseuAuthorityHandle *gph;
405 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
406 "GNS_AUTO_PSEU: New authority %s discovered\n",
409 gph = GNUNET_malloc(sizeof(struct GetPseuAuthorityHandle));
410 namelen = strlen(name) + 1;
411 memcpy(gph->name, name, namelen);
413 gph->new_zone = zone;
414 gph->zone = our_zone;
417 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
420 &process_zone_to_name_discover,
426 * Initialize the resolver
428 * @param nh the namestore handle
429 * @param dh the dht handle
430 * @param lz the local zone's hash
431 * @param max_bg_queries maximum number of parallel background queries in dht
432 * @return GNUNET_OK on success
435 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
436 struct GNUNET_DHT_Handle *dh,
437 struct GNUNET_CRYPTO_ShortHashCode lz,
438 unsigned long long max_bg_queries)
440 namestore_handle = nh;
444 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
445 max_allowed_background_queries = max_bg_queries;
447 if ((namestore_handle != NULL) && (dht_handle != NULL))
451 return GNUNET_SYSERR;
455 * Cleanup background lookups
457 * @param cls closure to iterator
458 * @param node heap nodes
459 * @param element the resolver handle
460 * @param cost heap cost
461 * @return always GNUNET_YES
464 cleanup_pending_background_queries(void* cls,
465 struct GNUNET_CONTAINER_HeapNode *node,
467 GNUNET_CONTAINER_HeapCostType cost)
469 struct ResolverHandle *rh = (struct ResolverHandle *)element;
470 ResolverCleanupContinuation cont = cls;
472 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
473 "GNS_CLEANUP: Terminating background lookup for %s\n",
475 GNUNET_DHT_get_stop(rh->get_handle);
476 rh->get_handle = NULL;
477 rh->proc(rh->proc_cls, rh, 0, NULL);
479 GNUNET_CONTAINER_heap_remove_node(node);
481 if (GNUNET_CONTAINER_heap_get_size(dht_lookup_heap) == 0)
493 gns_resolver_cleanup(ResolverCleanupContinuation cont)
495 unsigned int s = GNUNET_CONTAINER_heap_get_size(dht_lookup_heap);
496 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
497 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
500 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
501 &cleanup_pending_background_queries,
509 * Helper function to free resolver handle
511 * @param rh the handle to free
514 free_resolver_handle(struct ResolverHandle* rh)
516 struct AuthorityChain *ac;
517 struct AuthorityChain *ac_next;
522 ac = rh->authority_chain_head;
535 * Callback when record data is put into namestore
537 * @param cls the closure
538 * @param success GNUNET_OK on success
539 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
542 on_namestore_record_put_result(void *cls,
546 if (GNUNET_NO == success)
548 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
549 "GNS_NS: records already in namestore\n");
552 else if (GNUNET_YES == success)
554 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
555 "GNS_NS: records successfully put in namestore\n");
559 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
560 "GNS_NS: Error putting records into namestore: %s\n", emsg);
564 handle_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
566 struct ResolverHandle *rh = cls;
568 if (rh->timeout_cont)
569 rh->timeout_cont(rh->timeout_cont_cls, tc);
573 * Processor for background lookups in the DHT
575 * @param cls closure (NULL)
576 * @param rd_count number of records found (not 0)
577 * @param rd record data
580 background_lookup_result_processor(void *cls,
582 const struct GNUNET_NAMESTORE_RecordData *rd)
584 //We could do sth verbose/more useful here but it doesn't make any difference
585 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
586 "GNS_BG: background dht lookup for finished. (%d results)\n",
591 * Handle timeout for DHT requests
593 * @param cls the request handle as closure
594 * @param tc the task context
597 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
599 struct ResolverHandle *rh = cls;
600 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
601 char new_name[MAX_DNS_NAME_LENGTH];
603 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
604 "GNS_PHASE_REC: dht lookup for query %s timed out.\n",
607 * Start resolution in bg
609 //strcpy(new_name, rh->name);
610 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
611 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
612 rh->name, GNUNET_GNS_TLD);
614 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
615 "GNS_PHASE_REC: Starting background lookup for %s type %d\n",
616 new_name, rlh->record_type);
618 gns_resolver_lookup_record(rh->authority,
622 GNUNET_TIME_UNIT_FOREVER_REL,
623 &background_lookup_result_processor,
625 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
627 GNUNET_DHT_get_stop (rh->get_handle);
628 rh->get_handle = NULL;
629 rh->proc(rh->proc_cls, rh, 0, NULL);
634 * Function called when we get a result from the dht
635 * for our record query
637 * @param cls the request handle
638 * @param exp lifetime
639 * @param key the key the record was stored under
640 * @param get_path get path
641 * @param get_path_length get path length
642 * @param put_path put path
643 * @param put_path_length put path length
644 * @param type the block type
645 * @param size the size of the record
646 * @param data the record data
649 process_record_result_dht(void* cls,
650 struct GNUNET_TIME_Absolute exp,
651 const GNUNET_HashCode * key,
652 const struct GNUNET_PeerIdentity *get_path,
653 unsigned int get_path_length,
654 const struct GNUNET_PeerIdentity *put_path,
655 unsigned int put_path_length,
656 enum GNUNET_BLOCK_Type type,
657 size_t size, const void *data)
659 struct ResolverHandle *rh;
660 struct RecordLookupHandle *rlh;
661 struct GNSNameRecordBlock *nrb;
662 uint32_t num_records;
664 char* rd_data = (char*)data;
668 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
669 "GNS_PHASE_REC: got dht result (size=%d)\n", size);
674 //FIXME maybe check expiration here, check block type
676 rh = (struct ResolverHandle *)cls;
677 rlh = (struct RecordLookupHandle *) rh->proc_cls;
678 nrb = (struct GNSNameRecordBlock*)data;
680 /* stop lookup and timeout task */
681 GNUNET_DHT_get_stop (rh->get_handle);
682 rh->get_handle = NULL;
684 if (rh->dht_heap_node != NULL)
686 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
687 rh->dht_heap_node = NULL;
690 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
692 GNUNET_SCHEDULER_cancel(rh->timeout_task);
693 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
696 rh->get_handle = NULL;
697 name = (char*)&nrb[1];
698 num_records = ntohl(nrb->rd_count);
700 struct GNUNET_NAMESTORE_RecordData rd[num_records];
702 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
703 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
705 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
710 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
714 for (i=0; i<num_records; i++)
716 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
717 "GNS_PHASE_REC: Got name: %s (wanted %s)\n", name, rh->name);
718 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
719 "GNS_PHASE_REC: Got type: %d\n",
721 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
722 "GNS_PHASE_REC: Got data length: %d\n", rd[i].data_size);
723 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
724 "GNS_PHASE_REC: Got flag %d\n", rd[i].flags);
726 if ((strcmp(name, rh->name) == 0) &&
727 (rd[i].record_type == rlh->record_type))
735 * FIXME check pubkey against existing key in namestore?
736 * https://gnunet.org/bugs/view.php?id=2179
739 /* Save to namestore */
740 GNUNET_NAMESTORE_record_put (namestore_handle,
747 &on_namestore_record_put_result, //cont
752 rh->proc(rh->proc_cls, rh, num_records, rd);
754 rh->proc(rh->proc_cls, rh, 0, NULL);
761 * Start DHT lookup for a (name -> query->record_type) record in
762 * rh->authority's zone
764 * @param rh the pending gns query context
767 resolve_record_dht(struct ResolverHandle *rh)
770 struct GNUNET_CRYPTO_ShortHashCode name_hash;
771 GNUNET_HashCode lookup_key;
772 GNUNET_HashCode name_hash_double;
773 GNUNET_HashCode zone_hash_double;
774 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
775 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
776 struct ResolverHandle *rh_heap_root;
778 GNUNET_CRYPTO_short_hash(rh->name, strlen(rh->name), &name_hash);
779 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
780 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
781 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
782 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
784 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
785 "GNS_PHASE_REC: starting dht lookup for %s with key: %s\n",
786 rh->name, (char*)&lookup_key_string);
788 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
789 rh->dht_heap_node = NULL;
791 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
794 * Update timeout if necessary
796 if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
799 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
800 "GNS_PHASE_REC: Adjusting timeout\n");
802 * Set timeout for authority lookup phase to 1/2
804 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
805 GNUNET_TIME_relative_divide(rh->timeout, 2),
806 &handle_lookup_timeout,
809 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
810 // &dht_lookup_timeout,
812 rh->timeout_cont = &dht_lookup_timeout;
813 rh->timeout_cont_cls = rh;
817 if (max_allowed_background_queries <=
818 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
820 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
821 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
822 rh_heap_root->get_handle = NULL;
823 rh_heap_root->dht_heap_node = NULL;
825 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
826 "GNS_PHASE_REC: Replacing oldest background query for %s\n",
828 rh_heap_root->proc(rh_heap_root->proc_cls,
833 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
835 GNUNET_TIME_absolute_get().abs_value);
838 xquery = htonl(rlh->record_type);
840 GNUNET_assert(rh->get_handle == NULL);
841 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
842 GNUNET_TIME_UNIT_FOREVER_REL,
843 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
845 DHT_GNS_REPLICATION_LEVEL,
849 &process_record_result_dht,
856 * Namestore calls this function if we have record for this name.
857 * (or with rd_count=0 to indicate no matches)
859 * @param cls the pending query
860 * @param key the key of the zone we did the lookup
861 * @param expiration expiration date of the namestore entry
862 * @param name the name for which we need an authority
863 * @param rd_count the number of records with 'name'
864 * @param rd the record data
865 * @param signature the signature of the authority for the record data
868 process_record_result_ns(void* cls,
869 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
870 struct GNUNET_TIME_Absolute expiration,
871 const char *name, unsigned int rd_count,
872 const struct GNUNET_NAMESTORE_RecordData *rd,
873 const struct GNUNET_CRYPTO_RsaSignature *signature)
875 struct ResolverHandle *rh;
876 struct RecordLookupHandle *rlh;
877 struct GNUNET_TIME_Relative remaining_time;
878 struct GNUNET_CRYPTO_ShortHashCode zone;
880 rh = (struct ResolverHandle *) cls;
881 rlh = (struct RecordLookupHandle *)rh->proc_cls;
882 GNUNET_CRYPTO_short_hash(key,
883 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
885 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
893 rh->status |= EXISTS;
896 if (remaining_time.rel_value == 0)
898 rh->status |= EXPIRED;
904 * Lookup terminated and no results
906 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
907 "GNS_PHASE_REC: Namestore lookup for %s terminated without results\n",
910 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
911 "GNS_PHASE_REC: Record %s unknown in namestore\n",
914 * Our zone and no result? Cannot resolve TT
916 rh->proc(rh->proc_cls, rh, 0, NULL);
923 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
924 "GNS_PHASE_REC: Processing additional result %s from namestore\n",
927 for (i=0; i<rd_count;i++)
930 if (rd[i].record_type != rlh->record_type)
933 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
936 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
937 "GNS_PHASE_REC: This record is expired. Skipping\n");
948 if (rh->answered == 0)
950 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
951 "GNS_PHASE_REC: No answers found. This is odd!\n");
952 rh->proc(rh->proc_cls, rh, 0, NULL);
956 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
957 "GNS_PHASE_REC: Found %d answer(s) to query in %d records!\n",
958 rh->answered, rd_count);
960 rh->proc(rh->proc_cls, rh, rd_count, rd);
966 * The final phase of resolution.
967 * rh->name is a name that is canonical and we do not have a delegation.
968 * Query namestore for this record
970 * @param rh the pending lookup
973 resolve_record_ns(struct ResolverHandle *rh)
975 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
977 /* We cancel here as to not include the ns lookup in the timeout */
978 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
980 GNUNET_SCHEDULER_cancel(rh->timeout_task);
981 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
985 * Try to resolve this record in our namestore.
986 * The name to resolve is now in rh->authority_name
987 * since we tried to resolve it to an authority
990 GNUNET_NAMESTORE_lookup_record(namestore_handle,
994 &process_record_result_ns,
1001 * Handle timeout for DHT requests
1003 * @param cls the request handle as closure
1004 * @param tc the task context
1007 dht_authority_lookup_timeout(void *cls,
1008 const struct GNUNET_SCHEDULER_TaskContext *tc)
1010 struct ResolverHandle *rh = cls;
1011 struct RecordLookupHandle *rlh = rh->proc_cls;
1012 char new_name[MAX_DNS_NAME_LENGTH];
1014 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1015 "GNS_PHASE_DELEGATE_DHT: dht lookup for query %s timed out.\n",
1016 rh->authority_name);
1018 rh->status |= TIMED_OUT;
1020 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1022 GNUNET_DHT_get_stop (rh->get_handle);
1023 rh->get_handle = NULL;
1025 if (strcmp(rh->name, "") == 0)
1028 * promote authority back to name and try to resolve record
1030 strcpy(rh->name, rh->authority_name);
1031 rh->proc(rh->proc_cls, rh, 0, NULL);
1036 * Start resolution in bg
1038 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
1039 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
1040 //strcpy(new_name, rh->name);
1041 //strcpy(new_name+strlen(new_name), ".");
1042 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1044 strcpy(rh->name, new_name);
1046 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1047 "GNS_PHASE_DELEGATE_DHT: Starting background query for %s type %d\n",
1048 rh->name, rlh->record_type);
1050 gns_resolver_lookup_record(rh->authority,
1054 GNUNET_TIME_UNIT_FOREVER_REL,
1055 &background_lookup_result_processor,
1058 rh->proc(rh->proc_cls, rh, 0, NULL);
1062 static void resolve_delegation_dht(struct ResolverHandle *rh);
1065 static void resolve_delegation_ns(struct ResolverHandle *rh);
1069 * Namestore resolution for delegation finished. Processing result.
1071 * @param cls the closure
1072 * @param rh resolver handle
1073 * @param rd_count number of results (always 0)
1074 * @param rd record data (always NULL)
1077 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1078 unsigned int rd_count,
1079 const struct GNUNET_NAMESTORE_RecordData *rd);
1083 * Function called when we get a result from the dht
1084 * for our query. Recursively tries to resolve authorities
1087 * @param cls the request handle
1088 * @param exp lifetime
1089 * @param key the key the record was stored under
1090 * @param get_path get path
1091 * @param get_path_length get path length
1092 * @param put_path put path
1093 * @param put_path_length put path length
1094 * @param type the block type
1095 * @param size the size of the record
1096 * @param data the record data
1099 process_delegation_result_dht(void* cls,
1100 struct GNUNET_TIME_Absolute exp,
1101 const GNUNET_HashCode * key,
1102 const struct GNUNET_PeerIdentity *get_path,
1103 unsigned int get_path_length,
1104 const struct GNUNET_PeerIdentity *put_path,
1105 unsigned int put_path_length,
1106 enum GNUNET_BLOCK_Type type,
1107 size_t size, const void *data)
1109 struct ResolverHandle *rh;
1110 struct GNSNameRecordBlock *nrb;
1111 uint32_t num_records;
1113 char* rd_data = (char*) data;
1116 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
1117 GNUNET_HashCode zone_hash_double, name_hash_double;
1119 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1120 "GNS_PHASE_DELEGATE_DHT: Got DHT result\n");
1125 //FIXME check expiration?
1127 rh = (struct ResolverHandle *)cls;
1128 nrb = (struct GNSNameRecordBlock*)data;
1130 /* stop dht lookup and timeout task */
1131 GNUNET_DHT_get_stop (rh->get_handle);
1133 rh->get_handle = NULL;
1135 if (rh->dht_heap_node != NULL)
1137 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
1138 rh->dht_heap_node = NULL;
1141 num_records = ntohl(nrb->rd_count);
1142 name = (char*)&nrb[1];
1144 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1146 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1147 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
1149 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1154 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1155 "GNS_PHASE_DELEGATE_DHT: Error deserializing data!\n");
1159 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1160 "GNS_PHASE_DELEGATE_DHT: Got name: %s (wanted %s)\n",
1161 name, rh->authority_name);
1162 for (i=0; i<num_records; i++)
1165 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1166 "GNS_PHASE_DELEGATE_DHT: Got name: %s (wanted %s)\n",
1167 name, rh->authority_name);
1168 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1169 "GNS_PHASE_DELEGATE_DHT: Got type: %d (wanted %d)\n",
1170 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
1171 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1172 "GNS_PHASE_DELEGATE_DHT: Got data length: %d\n",
1174 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1175 "GNS_PHASE_DELEGATE_DHT: Got flag %d\n", rd[i].flags);
1177 if ((strcmp(name, rh->authority_name) == 0) &&
1178 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
1180 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1181 "GNS_PHASE_DELEGATE_DHT: Authority found in DHT\n");
1183 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
1184 struct AuthorityChain *auth =
1185 GNUNET_malloc(sizeof(struct AuthorityChain));
1186 auth->zone = rh->authority;
1187 memset(auth->name, 0, strlen(rh->authority_name)+1);
1188 strcpy(auth->name, rh->authority_name);
1189 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1190 rh->authority_chain_tail,
1193 /** try to import pkey if private key available */
1195 process_discovered_authority(name, auth->zone,
1196 rh->authority_chain_tail->zone,
1203 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
1204 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1205 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
1206 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
1208 /* Save to namestore */
1209 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
1212 GNUNET_NAMESTORE_record_put (namestore_handle,
1219 &on_namestore_record_put_result, //cont
1229 * FIXME in this case. should we ask namestore again?
1231 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1232 "GNS_PHASE_DELEGATE_DHT: Answer from DHT for %s. Yet to resolve: %s\n",
1233 rh->authority_name, rh->name);
1234 if (strcmp(rh->name, "") == 0)
1236 rh->proc(rh->proc_cls, rh, 0, NULL);
1240 rh->proc = &handle_delegation_ns;
1241 resolve_delegation_ns(rh);
1247 * No pkey but name exists
1250 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1251 "GNS_PHASE_DELEGATE_DHT: Adding %s back to %s\n",
1252 rh->authority_name, rh->name);
1253 if (strcmp(rh->name, "") == 0)
1254 strcpy(rh->name, rh->authority_name);
1256 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
1257 rh->name, rh->authority_name); //FIXME ret
1259 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1260 "GNS_PHASE_DELEGATE_DHT: %s restored\n", rh->name);
1261 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1262 "GNS_PHASE_DELEGATE_DHT: DHT authority lookup found no match!\n");
1263 rh->proc(rh->proc_cls, rh, 0, NULL);
1266 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
1267 +(MAX_DNS_NAME_LENGTH*2)
1268 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
1272 expand_plus(char** dest, char* src, char* repl)
1275 unsigned int s_len = strlen(src)+1;
1277 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1278 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
1282 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1283 "GNS_POSTPROCESS: %s to short\n", src);
1285 /* no postprocessing */
1286 memcpy(*dest, src, s_len+1);
1290 if (0 == strcmp(src+s_len-3, ".+"))
1292 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1293 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
1294 memset(*dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
1296 pos = *dest+s_len-2;
1298 pos += strlen(repl);
1299 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1300 "GNS_POSTPROCESS: Expanded to %s\n", *dest);
1304 memcpy(*dest, src, s_len+1);
1312 finish_lookup(struct ResolverHandle *rh,
1313 struct RecordLookupHandle* rlh,
1314 unsigned int rd_count,
1315 const struct GNUNET_NAMESTORE_RecordData *rd)
1318 char new_rr_data[MAX_DNS_NAME_LENGTH];
1319 char new_mx_data[MAX_MX_LENGTH];
1320 char new_soa_data[MAX_SOA_LENGTH];
1321 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
1324 unsigned int offset;
1326 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1327 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1330 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
1332 for (i = 0; i < rd_count; i++)
1335 if (rd[i].record_type != GNUNET_GNS_RECORD_TYPE_NS &&
1336 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_CNAME &&
1337 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
1338 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_SOA)
1340 p_rd[i].data = rd[i].data;
1345 * for all those records we 'should'
1346 * also try to resolve the A/AAAA records (RFC1035)
1347 * This is a feature and not important
1350 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1351 "GNS_POSTPROCESS: Postprocessing\n");
1353 if (strcmp(rh->name, "+") == 0)
1354 repl_string = rlh->name;
1356 repl_string = rlh->name+strlen(rh->name)+1;
1359 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
1361 memcpy(new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
1362 offset = sizeof(uint16_t);
1363 pos = new_mx_data+offset;
1364 expand_plus(&pos, (char*)rd[i].data+sizeof(uint16_t),
1366 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
1367 p_rd[i].data = new_mx_data;
1368 p_rd[i].data_size = offset;
1370 else if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_SOA)
1372 /* expand mname and rname */
1374 expand_plus(&pos, (char*)rd[i].data, repl_string);
1375 offset = strlen(new_soa_data)+1;
1376 pos = new_soa_data+offset;
1377 expand_plus(&pos, (char*)rd[i].data+offset, repl_string);
1378 offset += strlen(new_soa_data+offset)+1;
1379 /* cpy the 4 numbers serial refresh retry and expire */
1380 memcpy(new_soa_data+offset, (char*)rd[i].data+offset, sizeof(uint32_t)*5);
1381 offset += sizeof(uint32_t)*5;
1382 p_rd[i].data_size = offset;
1383 p_rd[i].data = new_soa_data;
1388 expand_plus(&pos, (char*)rd[i].data, repl_string);
1389 p_rd[i].data_size = strlen(new_rr_data)+1;
1390 p_rd[i].data = new_rr_data;
1395 rlh->proc(rlh->proc_cls, rd_count, p_rd);
1401 * Process DHT lookup result for record.
1403 * @param cls the closure
1404 * @param rh resolver handle
1405 * @param rd_count number of results
1406 * @param rd record data
1409 handle_record_dht(void* cls, struct ResolverHandle *rh,
1410 unsigned int rd_count,
1411 const struct GNUNET_NAMESTORE_RecordData *rd)
1413 struct RecordLookupHandle* rlh;
1415 rlh = (struct RecordLookupHandle*)cls;
1418 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1419 "GNS_PHASE_REC: No records for %s found in DHT. Aborting\n",
1421 /* give up, cannot resolve */
1422 finish_lookup(rh, rlh, 0, NULL);
1423 free_resolver_handle(rh);
1427 /* results found yay */
1428 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1429 "GNS_PHASE_REC: Record resolved from DHT!");
1431 finish_lookup(rh, rlh, rd_count, rd);
1432 free_resolver_handle(rh);
1438 * Process namestore lookup result for record.
1440 * @param cls the closure
1441 * @param rh resolver handle
1442 * @param rd_count number of results
1443 * @param rd record data
1446 handle_record_ns(void* cls, struct ResolverHandle *rh,
1447 unsigned int rd_count,
1448 const struct GNUNET_NAMESTORE_RecordData *rd)
1450 struct RecordLookupHandle* rlh;
1451 rlh = (struct RecordLookupHandle*) cls;
1454 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1455 "GNS_PHASE_REC: NS returned no records. (status: %d)!\n",
1459 * There are 4 conditions that have to met for us to consult the DHT:
1460 * 1. The entry in the DHT is EXPIRED AND
1461 * 2. No entry in the NS existed AND
1462 * 3. The zone queried is not the local resolver's zone AND
1463 * 4. The name that was looked up is '+'
1464 * because if it was any other canonical name we either already queried
1465 * the DHT for the authority in the authority lookup phase (and thus
1466 * would already have an entry in the NS for the record)
1468 if (rh->status & (EXPIRED | !EXISTS) &&
1469 GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1471 (strcmp(rh->name, "+") == 0))
1473 rh->proc = &handle_record_dht;
1474 resolve_record_dht(rh);
1477 /* give up, cannot resolve */
1478 finish_lookup(rh, rlh, 0, NULL);
1479 free_resolver_handle(rh);
1483 /* results found yay */
1484 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1485 "GNS_PHASE_REC: Record resolved from namestore!");
1487 finish_lookup(rh, rlh, rd_count, rd);
1489 free_resolver_handle(rh);
1495 * Determine if this name is canonical.
1497 * a.b.gnunet = not canonical
1500 * @param name the name to test
1501 * @return 1 if canonical
1504 is_canonical(char* name)
1506 uint32_t len = strlen(name);
1509 for (i=0; i<len; i++)
1511 if (*(name+i) == '.')
1518 * Move one level up in the domain hierarchy and return the
1519 * passed top level domain.
1521 * @param name the domain
1522 * @param dest the destination where the tld will be put
1525 pop_tld(char* name, char* dest)
1529 if (is_canonical(name))
1536 for (len = strlen(name); len > 0; len--)
1538 if (*(name+len) == '.')
1548 strcpy(dest, (name+len+1));
1552 * Checks if name is in tld
1554 * @param name the name to check
1555 * @param tld the TLD to check for
1556 * @return GNUNET_YES or GNUNET_NO
1559 is_tld(const char* name, const char* tld)
1563 if (strlen(name) <= strlen(tld))
1568 offset = strlen(name)-strlen(tld);
1569 if (strcmp(name+offset, tld) != 0)
1571 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1572 "%s is not in .%s TLD\n", name, tld);
1579 * DHT resolution for delegation finished. Processing result.
1581 * @param cls the closure
1582 * @param rh resolver handle
1583 * @param rd_count number of results (always 0)
1584 * @param rd record data (always NULL)
1587 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
1588 unsigned int rd_count,
1589 const struct GNUNET_NAMESTORE_RecordData *rd)
1591 struct RecordLookupHandle* rlh;
1592 rlh = (struct RecordLookupHandle*) cls;
1595 if (strcmp(rh->name, "") == 0)
1597 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
1599 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1600 "GNS_PHASE_DELEGATE_DHT: Resolved queried PKEY via DHT.\n");
1601 finish_lookup(rh, rlh, rd_count, rd);
1602 free_resolver_handle(rh);
1605 /* We resolved full name for delegation. resolving record */
1606 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1607 "GNS_PHASE_DELEGATE_DHT: Resolved full name for delegation via DHT.\n");
1608 strcpy(rh->name, "+\0");
1609 rh->proc = &handle_record_ns;
1610 resolve_record_ns(rh);
1615 * we still have some left
1617 if (is_canonical(rh->name))
1619 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1620 "GNS_PHASE_DELEGATE_DHT: Resolving canonical record %s in ns\n",
1622 rh->proc = &handle_record_ns;
1623 resolve_record_ns(rh);
1626 /* give up, cannot resolve */
1627 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1628 "GNS_PHASE_DELEGATE_DHT: Cannot fully resolve delegation for %s via DHT!\n",
1630 finish_lookup(rh, rlh, 0, NULL);
1631 free_resolver_handle(rh);
1636 * Start DHT lookup for a name -> PKEY (compare NS) record in
1637 * rh->authority's zone
1639 * @param rh the pending gns query
1642 resolve_delegation_dht(struct ResolverHandle *rh)
1645 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1646 GNUNET_HashCode name_hash_double;
1647 GNUNET_HashCode zone_hash_double;
1648 GNUNET_HashCode lookup_key;
1649 struct ResolverHandle *rh_heap_root;
1651 pop_tld(rh->name, rh->authority_name);
1652 GNUNET_CRYPTO_short_hash(rh->authority_name,
1653 strlen(rh->authority_name),
1655 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1656 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
1657 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
1659 rh->dht_heap_node = NULL;
1661 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1663 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1664 // &dht_authority_lookup_timeout,
1666 rh->timeout_cont = &dht_authority_lookup_timeout;
1667 rh->timeout_cont_cls = rh;
1671 if (max_allowed_background_queries <=
1672 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1674 /* terminate oldest lookup */
1675 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1676 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
1677 rh_heap_root->dht_heap_node = NULL;
1679 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1680 "GNS_PHASE_DELEGATE_DHT: Replacing oldest background query for %s\n",
1681 rh_heap_root->authority_name);
1683 rh_heap_root->proc(rh_heap_root->proc_cls,
1688 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1690 GNUNET_TIME_absolute_get().abs_value);
1693 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1695 GNUNET_assert(rh->get_handle == NULL);
1696 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1697 GNUNET_TIME_UNIT_FOREVER_REL,
1698 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1700 DHT_GNS_REPLICATION_LEVEL,
1704 &process_delegation_result_dht,
1711 * Namestore resolution for delegation finished. Processing result.
1713 * @param cls the closure
1714 * @param rh resolver handle
1715 * @param rd_count number of results (always 0)
1716 * @param rd record data (always NULL)
1719 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1720 unsigned int rd_count,
1721 const struct GNUNET_NAMESTORE_RecordData *rd)
1723 struct RecordLookupHandle* rlh;
1724 rlh = (struct RecordLookupHandle*) cls;
1726 if (strcmp(rh->name, "") == 0)
1728 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
1730 GNUNET_assert(rd_count == 1);
1731 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1732 "GNS_PHASE_DELEGATE_NS: Resolved queried PKEY in NS.\n");
1733 finish_lookup(rh, rlh, rd_count, rd);
1734 free_resolver_handle(rh);
1737 /* We resolved full name for delegation. resolving record */
1738 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1739 "GNS_PHASE_DELEGATE_NS: Resolved full name for delegation.\n");
1740 strcpy(rh->name, "+\0");
1741 rh->proc = &handle_record_ns;
1742 resolve_record_ns(rh);
1747 * we still have some left
1748 * check if authority in ns is fresh
1750 * or we are authority
1752 if ((rh->status & (EXISTS | !EXPIRED)) ||
1753 !GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1754 &rh->authority_chain_tail->zone))
1756 if (is_canonical(rh->name))
1758 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1759 "GNS_PHASE_DELEGATE_NS: Resolving canonical record %s\n",
1761 rh->proc = &handle_record_ns;
1762 resolve_record_ns(rh);
1766 /* give up, cannot resolve */
1767 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1768 "GNS_PHASE_DELEGATE_NS: Cannot fully resolve delegation for %s!\n",
1770 finish_lookup(rh, rlh, rd_count, rd);
1771 //rlh->proc(rlh->proc_cls, 0, NULL);
1776 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1777 "GNS_PHASE_DELEGATE_NS: Trying to resolve delegation for %s via DHT\n",
1779 rh->proc = &handle_delegation_dht;
1780 resolve_delegation_dht(rh);
1786 * This is a callback function that should give us only PKEY
1787 * records. Used to query the namestore for the authority (PKEY)
1788 * for 'name'. It will recursively try to resolve the
1789 * authority for a given name from the namestore.
1791 * @param cls the pending query
1792 * @param key the key of the zone we did the lookup
1793 * @param expiration expiration date of the record data set in the namestore
1794 * @param name the name for which we need an authority
1795 * @param rd_count the number of records with 'name'
1796 * @param rd the record data
1797 * @param signature the signature of the authority for the record data
1800 process_delegation_result_ns(void* cls,
1801 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1802 struct GNUNET_TIME_Absolute expiration,
1804 unsigned int rd_count,
1805 const struct GNUNET_NAMESTORE_RecordData *rd,
1806 const struct GNUNET_CRYPTO_RsaSignature *signature)
1808 struct ResolverHandle *rh;
1809 struct GNUNET_TIME_Relative remaining_time;
1810 struct GNUNET_CRYPTO_ShortHashCode zone;
1811 char new_name[MAX_DNS_NAME_LENGTH];
1813 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1814 "GNS_PHASE_DELEGATE_NS: Got %d records from authority lookup\n",
1817 rh = (struct ResolverHandle *)cls;
1818 GNUNET_CRYPTO_short_hash(key,
1819 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1821 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1827 rh->status |= EXISTS;
1830 if (remaining_time.rel_value == 0)
1832 rh->status |= EXPIRED;
1836 * No authority found in namestore.
1841 * We did not find an authority in the namestore
1846 * Promote this authority back to a name maybe it is
1849 if (strcmp(rh->name, "") == 0)
1851 /* simply promote back */
1852 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1853 "GNS_PHASE_DELEGATE_NS: Promoting %s back to name\n",
1854 rh->authority_name);
1855 strcpy(rh->name, rh->authority_name);
1859 /* add back to existing name */
1860 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1861 "GNS_PHASE_DELEGATE_NS: Adding %s back to %s\n",
1862 rh->authority_name, rh->name);
1863 //memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1864 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
1865 rh->name, rh->authority_name);
1866 //strcpy(new_name, rh->name);
1867 //strcpy(new_name+strlen(new_name), ".");
1868 //strcpy(new_name+strlen(new_name), rh->authority_name);
1869 strcpy(rh->name, new_name);
1870 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1871 "GNS_PHASE_DELEGATE_NS: %s restored\n", rh->name);
1873 rh->proc(rh->proc_cls, rh, 0, NULL);
1878 * We found an authority that may be able to help us
1879 * move on with query
1880 * Note only 1 pkey should have been returned.. anything else would be strange
1883 for (i=0; i<rd_count;i++)
1886 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1889 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1892 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1893 "GNS_PHASE_DELEGATE_NS: This pkey is expired.\n");
1894 if (remaining_time.rel_value == 0)
1896 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1897 "GNS_PHASE_DELEGATE_NS: This dht entry is expired.\n");
1898 rh->authority_chain_head->fresh = 0;
1899 rh->proc(rh->proc_cls, rh, 0, NULL);
1907 * Resolve rest of query with new authority
1909 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1910 memcpy(&rh->authority, rd[i].data,
1911 sizeof(struct GNUNET_CRYPTO_ShortHashCode));
1912 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1913 auth->zone = rh->authority;
1914 memset(auth->name, 0, strlen(rh->authority_name)+1);
1915 strcpy(auth->name, rh->authority_name);
1916 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1917 rh->authority_chain_tail,
1920 /** try to import pkey if private key available */
1922 process_discovered_authority((char*)name, auth->zone,
1923 rh->authority_chain_tail->zone,
1926 * We are done with PKEY resolution if name is empty
1927 * else resolve again with new authority
1929 if (strcmp(rh->name, "") == 0)
1930 rh->proc(rh->proc_cls, rh, rd_count, rd);
1932 resolve_delegation_ns(rh);
1939 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1940 "GNS_PHASE_DELEGATE_NS: Authority lookup and no PKEY...\n");
1941 rh->proc(rh->proc_cls, rh, 0, NULL);
1946 * Resolve the delegation chain for the request in our namestore
1948 * @param rh the resolver handle
1951 resolve_delegation_ns(struct ResolverHandle *rh)
1953 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1954 "GNS_PHASE_DELEGATE_NS: Resolving delegation for %s\n", rh->name);
1955 pop_tld(rh->name, rh->authority_name);
1956 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1959 GNUNET_GNS_RECORD_PKEY,
1960 &process_delegation_result_ns,
1967 * Lookup of a record in a specific zone
1968 * calls lookup result processor on result
1970 * @param zone the root zone
1971 * @param record_type the record type to look up
1972 * @param name the name to look up
1973 * @param key a private key for use with PSEU import (can be NULL)
1974 * @param timeout timeout for resolution
1975 * @param proc the processor to call on result
1976 * @param cls the closure to pass to proc
1979 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
1980 uint32_t record_type,
1982 struct GNUNET_CRYPTO_RsaPrivateKey *key,
1983 struct GNUNET_TIME_Relative timeout,
1984 RecordLookupProcessor proc,
1987 struct ResolverHandle *rh;
1988 struct RecordLookupHandle* rlh;
1989 char string_hash[MAX_DNS_LABEL_LENGTH];
1990 char nzkey[MAX_DNS_LABEL_LENGTH];
1991 char* nzkey_ptr = nzkey;
1993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1994 "Starting resolution for %s (type=%d)!\n",
1998 if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
2000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2001 "%s is canonical and not gnunet -> cannot resolve!\n", name);
2006 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
2007 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2009 rh->authority = zone;
2012 rh->timeout = timeout;
2013 rh->get_handle = NULL;
2014 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2017 * Set timeout for authority lookup phase to 1/2
2019 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
2020 GNUNET_TIME_relative_divide(timeout, 2),
2021 &handle_lookup_timeout,
2023 rh->timeout_cont = &dht_authority_lookup_timeout;
2024 rh->timeout_cont_cls = rh;
2028 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
2029 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2032 if (strcmp(GNUNET_GNS_TLD, name) == 0)
2035 * Only 'gnunet' given
2037 strcpy(rh->name, "\0");
2041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2042 "Checking for TLD...\n");
2043 if (is_zkey_tld(name) == GNUNET_YES)
2045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2048 * This is a zkey tld
2049 * build hash and use as initial authority
2052 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
2053 memcpy(rh->name, name,
2054 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
2055 pop_tld(rh->name, string_hash);
2057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2058 "ZKEY is %s!\n", string_hash);
2060 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
2062 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
2065 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2066 "Cannot convert ZKEY %s to hash!\n", string_hash);
2076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2079 * Presumably GNUNET tld
2082 strlen(name)-strlen(GNUNET_GNS_TLD));
2083 memcpy(rh->name, name,
2084 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2089 * Initialize authority chain
2091 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2092 rh->authority_chain_head->prev = NULL;
2093 rh->authority_chain_head->next = NULL;
2094 rh->authority_chain_tail = rh->authority_chain_head;
2095 rh->authority_chain_head->zone = rh->authority;
2098 * Copy original query into lookup handle
2100 rlh->record_type = record_type;
2101 memset(rlh->name, 0, strlen(name) + 1);
2102 strcpy(rlh->name, name);
2104 rlh->proc_cls = cls;
2106 rh->proc = &handle_delegation_ns;
2107 resolve_delegation_ns(rh);
2110 /******** END Record Resolver ***********/
2114 * Callback calles by namestore for a zone to name
2117 * @param cls the closure
2118 * @param zone_key the zone we queried
2119 * @param expire the expiration time of the name
2120 * @param name the name found or NULL
2121 * @param rd_len number of records for the name
2122 * @param rd the record data (PKEY) for the name
2123 * @param signature the signature for the record data
2126 process_zone_to_name_shorten(void *cls,
2127 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
2128 struct GNUNET_TIME_Absolute expire,
2130 unsigned int rd_len,
2131 const struct GNUNET_NAMESTORE_RecordData *rd,
2132 const struct GNUNET_CRYPTO_RsaSignature *signature)
2134 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
2135 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
2136 struct AuthorityChain *next_authority;
2138 char result[MAX_DNS_NAME_LENGTH];
2139 char next_authority_name[MAX_DNS_LABEL_LENGTH];
2142 /* we found a match in our own zone */
2145 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2146 "result strlen %d\n", strlen(name));
2147 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
2148 memset(result, 0, answer_len);
2149 if (strlen(rh->name) > 0)
2151 strcpy(result, rh->name);
2152 strcpy(result+strlen(rh->name), ".");
2155 strcpy(result+strlen(result), name);
2156 strcpy(result+strlen(result), ".");
2157 strcpy(result+strlen(result), GNUNET_GNS_TLD);
2159 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2160 "Sending shorten result %s\n", result);
2162 nsh->proc(nsh->proc_cls, result);
2164 free_resolver_handle(rh);
2166 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2169 /* our zone, just append .gnunet */
2170 answer_len = strlen(rh->name) + strlen(GNUNET_GNS_TLD) + 2;
2171 memset(result, 0, answer_len);
2172 strcpy(result, rh->name);
2173 strcpy(result+strlen(rh->name), ".");
2174 strcpy(result+strlen(rh->name)+1, GNUNET_GNS_TLD);
2176 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2177 "Our zone: Sending name as shorten result %s\n", rh->name);
2179 nsh->proc(nsh->proc_cls, result);
2181 free_resolver_handle(rh);
2187 * continue with next authority
2189 next_authority = rh->authority_chain_head;
2190 // strlen(next_authority->name) + 2);
2191 memset(next_authority_name, 0, strlen(rh->name)+
2192 strlen(next_authority->name) + 2);
2193 strcpy(next_authority_name, rh->name);
2194 strcpy(next_authority_name+strlen(rh->name)+1, ".");
2195 strcpy(next_authority_name+strlen(rh->name)+2, next_authority->name);
2197 strcpy(rh->name, next_authority_name);
2198 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
2199 rh->authority_chain_tail,
2202 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2203 &rh->authority_chain_tail->zone,
2204 &rh->authority_chain_head->zone,
2205 &process_zone_to_name_shorten,
2211 * DHT resolution for delegation finished. Processing result.
2213 * @param cls the closure
2214 * @param rh resolver handle
2215 * @param rd_count number of results (always 0)
2216 * @param rd record data (always NULL)
2219 handle_delegation_dht_bg_shorten(void* cls, struct ResolverHandle *rh,
2220 unsigned int rd_count,
2221 const struct GNUNET_NAMESTORE_RecordData *rd)
2224 /* We resolved full name for delegation. resolving record */
2225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2226 "GNS_SHORTEN: Resolved up to %s for delegation via DHT in background.\n",
2228 free_resolver_handle(rh);
2232 * Process result from namestore delegation lookup
2233 * for shorten operation
2235 * @param cls the client shorten handle
2236 * @param rh the resolver handle
2237 * @param rd_count number of results (0)
2238 * @param rd data (NULL)
2241 handle_delegation_ns_shorten(void* cls,
2242 struct ResolverHandle *rh,
2244 const struct GNUNET_NAMESTORE_RecordData *rd)
2246 struct NameShortenHandle *nsh;
2247 char result[MAX_DNS_NAME_LENGTH];
2249 struct ResolverHandle *rh_bg;
2251 nsh = (struct NameShortenHandle *)cls;
2254 * At this point rh->name contains the part of the name
2255 * that we do not have a PKEY in our namestore to resolve.
2256 * The authority chain in the resolver handle is now
2257 * useful to backtrack if needed
2260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2261 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2263 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2267 * This is our zone append .gnunet unless name is empty
2268 * (it shouldn't be, usually FIXME what happens if we
2269 * shorten to our zone to a "" record??)
2272 answer_len = strlen(rh->name) + strlen(GNUNET_GNS_TLD) + 2;
2273 memset(result, 0, answer_len);
2274 strcpy(result, rh->name);
2275 strcpy(result+strlen(rh->name), ".");
2276 strcpy(result+strlen(rh->name)+1, GNUNET_GNS_TLD);
2278 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2279 "Our zone: Sending name as shorten result %s\n", rh->name);
2281 nsh->proc(nsh->proc_cls, result);
2283 free_resolver_handle(rh);
2288 * we have to this before zone to name for rh might
2292 if (!is_canonical(rh->name))
2294 rh_bg = GNUNET_malloc(sizeof(struct ResolverHandle));
2295 memcpy(rh_bg, rh, sizeof(struct ResolverHandle));
2298 /* backtrack authorities for names */
2299 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2300 &rh->authority_chain_tail->zone, //ours
2301 &rh->authority_chain_head->zone,
2302 &process_zone_to_name_shorten,
2311 * If authority resolution is incomplete we can do a background lookup
2312 * of the full name so that next time we can (likely) fully or at least
2313 * further shorten the name
2315 rh_bg->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2316 rh_bg->authority_chain_tail = rh_bg->authority_chain_head;
2317 rh_bg->authority_chain_head->zone = rh_bg->authority;
2319 rh_bg->proc = &handle_delegation_dht_bg_shorten;
2320 rh_bg->proc_cls = NULL;
2322 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2323 "GNS_SHORTEN: Starting background lookup for %s\n",
2326 resolve_delegation_dht(rh_bg);
2332 * Callback calles by namestore for a zone to name
2335 * @param cls the closure
2336 * @param zone_key the zone we queried
2337 * @param expire the expiration time of the name
2338 * @param name the name found or NULL
2339 * @param rd_len number of records for the name
2340 * @param rd the record data (PKEY) for the name
2341 * @param signature the signature for the record data
2344 process_zone_to_name_zkey(void *cls,
2345 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
2346 struct GNUNET_TIME_Absolute expire,
2348 unsigned int rd_len,
2349 const struct GNUNET_NAMESTORE_RecordData *rd,
2350 const struct GNUNET_CRYPTO_RsaSignature *signature)
2352 struct ResolverHandle *rh = cls;
2353 struct NameShortenHandle *nsh = rh->proc_cls;
2354 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
2355 char new_name[MAX_DNS_NAME_LENGTH];
2357 /* zkey not in our zone */
2361 * In this case we have not given this PKEY a name (yet)
2362 * It is either just not in our zone or not even cached
2363 * Since we do not know at this point we will not try to shorten
2364 * because PKEY import will happen if the user follows the zkey
2367 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
2369 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2370 "No name found for zkey %s returning verbatim!\n", enc);
2371 if (strcmp(rh->name, "") != 0)
2372 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
2373 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
2375 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2376 enc, GNUNET_GNS_TLD_ZKEY);
2377 nsh->proc(nsh->proc_cls, new_name);
2379 free_resolver_handle(rh);
2383 if (strcmp(rh->name, "") != 0)
2384 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2387 strcpy(new_name, name);
2389 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2390 "Continue shorten for %s!\n", new_name);
2392 strcpy(rh->name, new_name);
2394 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2395 rh->authority_chain_tail = rh->authority_chain_head;
2396 rh->authority_chain_head->zone = rh->authority;
2399 /* Start delegation resolution in our namestore */
2400 resolve_delegation_ns(rh);
2404 * Shorten api from resolver
2406 * @param zone the zone to use
2407 * @param name the name to shorten
2408 * @param key optional private key for background lookups and PSEU import
2409 * @param proc the processor to call with result
2410 * @param proc_cls closure to pass to proc
2413 gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode zone,
2415 struct GNUNET_CRYPTO_RsaPrivateKey *key,
2416 ShortenResultProcessor proc,
2419 struct ResolverHandle *rh;
2420 struct NameShortenHandle *nsh;
2421 char string_hash[MAX_DNS_LABEL_LENGTH];
2422 struct GNUNET_CRYPTO_ShortHashCode zkey;
2423 char nzkey[MAX_DNS_LABEL_LENGTH];
2424 char* nzkey_ptr = nzkey;
2427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2428 "Starting shorten for %s!\n", name);
2430 if (is_canonical((char*)name))
2432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2433 "%s is canonical. Returning verbatim\n", name);
2434 proc(proc_cls, name);
2438 nsh = GNUNET_malloc(sizeof (struct NameShortenHandle));
2441 nsh->proc_cls = proc_cls;
2443 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2444 rh->authority = zone;
2446 rh->proc = &handle_delegation_ns_shorten;
2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2450 "Checking for TLD...\n");
2451 if (is_zkey_tld(name) == GNUNET_YES)
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2456 * This is a zkey tld
2457 * build hash and use as initial authority
2461 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
2462 memcpy(rh->name, name,
2463 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
2464 pop_tld(rh->name, string_hash);
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 "ZKEY is %s!\n", string_hash);
2469 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
2471 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
2474 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2475 "Cannot convert ZKEY %s to hash!\n", nzkey);
2478 proc(proc_cls, name);
2482 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2485 &process_zone_to_name_zkey,
2492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 * Presumably GNUNET tld
2498 strlen(name)-strlen(GNUNET_GNS_TLD));
2499 memcpy(rh->name, name,
2500 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2503 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2504 rh->authority_chain_tail = rh->authority_chain_head;
2505 rh->authority_chain_head->zone = zone;
2508 /* Start delegation resolution in our namestore */
2509 resolve_delegation_ns(rh);
2512 /*********** END NAME SHORTEN ********************/
2516 * Process result from namestore delegation lookup
2517 * for get authority operation
2519 * @param cls the client get auth handle
2520 * @param rh the resolver handle
2521 * @param rd_count number of results (0)
2522 * @param rd data (NULL)
2525 handle_delegation_result_ns_get_auth(void* cls,
2526 struct ResolverHandle *rh,
2528 const struct GNUNET_NAMESTORE_RecordData *rd)
2530 struct GetNameAuthorityHandle* nah;
2531 char result[MAX_DNS_NAME_LENGTH];
2534 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
2537 * At this point rh->name contains the part of the name
2538 * that we do not have a PKEY in our namestore to resolve.
2539 * The authority chain in the resolver handle is now
2540 * useful to backtrack if needed
2543 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2544 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2546 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2547 "Building response!\n");
2548 if (is_canonical(rh->name))
2551 * We successfully resolved the authority in the ns
2552 * FIXME for our purposes this is fine
2553 * but maybe we want to have an api that also looks
2554 * into the dht (i.e. option in message)
2556 if (strlen(rh->name) > strlen(nah->name))
2558 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2559 "Record name longer than original lookup name... odd!\n");
2563 answer_len = strlen(nah->name) - strlen(rh->name)
2564 + strlen(GNUNET_GNS_TLD) + 1;
2565 memset(result, 0, answer_len);
2566 strcpy(result, nah->name + strlen(rh->name) + 1);
2568 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2569 "Got authority result %s\n", result);
2571 nah->proc(nah->proc_cls, result);
2573 free_resolver_handle(rh);
2577 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2578 "Unable to resolve authority for remaining %s!\n", rh->name);
2579 nah->proc(nah->proc_cls, "");
2581 free_resolver_handle(rh);
2589 * Tries to resolve the authority for name
2592 * @param zone the root zone to look up for
2593 * @param name the name to lookup up
2594 * @param proc the processor to call when finished
2595 * @param proc_cls the closure to pass to the processor
2598 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
2600 GetAuthorityResultProcessor proc,
2603 struct ResolverHandle *rh;
2604 struct GetNameAuthorityHandle *nah;
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2607 "Starting authority resolution for %s!\n", name);
2609 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
2610 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2611 rh->authority = zone;
2613 if (strcmp(GNUNET_GNS_TLD, name) == 0)
2615 strcpy(rh->name, "\0");
2620 strlen(name)-strlen(GNUNET_GNS_TLD));
2621 memcpy(rh->name, name,
2622 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2625 memset(nah->name, 0,
2627 strcpy(nah->name, name);
2629 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2630 rh->authority_chain_tail = rh->authority_chain_head;
2631 rh->authority_chain_head->zone = zone;
2632 rh->proc = &handle_delegation_result_ns_get_auth;
2633 rh->proc_cls = (void*)nah;
2636 nah->proc_cls = proc_cls;
2638 /* Start delegation resolution in our namestore */
2639 resolve_delegation_ns(rh);
2643 /******** END GET AUTHORITY *************/
2645 /* end of gnunet-service-gns_resolver.c */