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_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
42 #define DHT_GNS_REPLICATION_LEVEL 5
43 #define MAX_DNS_LABEL_LENGTH 63
47 * Our handle to the namestore service
49 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
52 * Resolver handle to the dht
54 static struct GNUNET_DHT_Handle *dht_handle;
57 * Heap for parallel DHT lookups
59 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
62 * Maximum amount of parallel queries in background
64 static unsigned long long max_allowed_background_queries;
67 * Wheather or not to ignore pending records
69 static int ignore_pending_records;
74 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
77 * a resolution identifier pool variable
79 * This is a non critical identifier useful for debugging
81 static unsigned long long rid = 0;
84 * Namestore calls this function if we have record for this name.
85 * (or with rd_count=0 to indicate no matches)
87 * @param cls the pending query
88 * @param key the key of the zone we did the lookup
89 * @param expiration expiration date of the namestore entry
90 * @param name the name for which we need an authority
91 * @param rd_count the number of records with 'name'
92 * @param rd the record data
93 * @param signature the signature of the authority for the record data
96 process_pseu_lookup_ns(void* cls,
97 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
98 struct GNUNET_TIME_Absolute expiration,
99 const char *name, unsigned int rd_count,
100 const struct GNUNET_NAMESTORE_RecordData *rd,
101 const struct GNUNET_CRYPTO_RsaSignature *signature)
103 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
104 struct GNUNET_NAMESTORE_RecordData new_pkey;
108 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
109 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
110 if (0 == strcmp(gph->name, name))
112 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
113 "GNS_AUTO_PSEU: Intelligent replacement not implemented\n",
119 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
120 "GNS_AUTO_PSEU: Trying delegated name %s\n", gph->name);
121 memcpy(gph->new_name, gph->name, strlen(gph->name)+1);
122 GNUNET_NAMESTORE_lookup_record(namestore_handle,
125 GNUNET_NAMESTORE_TYPE_ANY,
126 &process_pseu_lookup_ns,
132 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
133 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->new_name);
135 new_pkey.expiration = GNUNET_TIME_absolute_get_forever ();
136 new_pkey.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
137 new_pkey.data = &gph->new_zone;
138 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
139 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
140 | GNUNET_NAMESTORE_RF_PRIVATE
141 | GNUNET_NAMESTORE_RF_PENDING;
142 GNUNET_NAMESTORE_record_create (namestore_handle,
153 * process result of a dht pseu lookup
155 * @param gph the handle
156 * @param name the pseu result or NULL
159 process_pseu_result(struct GetPseuAuthorityHandle* gph, char* name)
163 memcpy(gph->new_name, gph->name, strlen(gph->name)+1);
167 memcpy(gph->new_name, name, strlen(name)+1);
170 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
171 "GNS_AUTO_PSEU: Checking %s for collision in NS\n", gph->new_name);
174 * Check for collision
176 GNUNET_NAMESTORE_lookup_record(namestore_handle,
179 GNUNET_NAMESTORE_TYPE_ANY,
180 &process_pseu_lookup_ns,
185 * Handle timeout for dht request
187 * @param cls the request handle as closure
188 * @param tc the task context
191 handle_auth_discovery_timeout(void *cls,
192 const struct GNUNET_SCHEDULER_TaskContext *tc)
194 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
196 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
197 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
198 GNUNET_DHT_get_stop (gph->get_handle);
199 gph->get_handle = NULL;
200 process_pseu_result(gph, NULL);
204 * Function called when we find a PSEU entry in the DHT
206 * @param cls the request handle
207 * @param exp lifetime
208 * @param key the key the record was stored under
209 * @param get_path get path
210 * @param get_path_length get path length
211 * @param put_path put path
212 * @param put_path_length put path length
213 * @param type the block type
214 * @param size the size of the record
215 * @param data the record data
218 process_auth_discovery_dht_result(void* cls,
219 struct GNUNET_TIME_Absolute exp,
220 const GNUNET_HashCode * key,
221 const struct GNUNET_PeerIdentity *get_path,
222 unsigned int get_path_length,
223 const struct GNUNET_PeerIdentity *put_path,
224 unsigned int put_path_length,
225 enum GNUNET_BLOCK_Type type,
226 size_t size, const void *data)
228 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
229 struct GNSNameRecordBlock *nrb;
230 char* rd_data = (char*)data;
236 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
237 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
241 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
242 "GNS_GET_AUTH: got dht result null!\n", size);
248 nrb = (struct GNSNameRecordBlock*)data;
250 /* stop lookup and timeout task */
251 GNUNET_DHT_get_stop (gph->get_handle);
252 gph->get_handle = NULL;
253 GNUNET_SCHEDULER_cancel(gph->timeout);
255 gph->get_handle = NULL;
257 nrb = (struct GNSNameRecordBlock*)data;
259 name = (char*)&nrb[1];
260 num_records = ntohl(nrb->rd_count);
262 struct GNUNET_NAMESTORE_RecordData rd[num_records];
264 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
265 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
267 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
272 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
273 "GNS_GET_AUTH: Error deserializing data!\n");
279 for (i=0; i<num_records; i++)
281 if ((strcmp(name, "+") == 0) &&
282 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
285 process_pseu_result(gph, (char*)rd[i].data);
291 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in dht!\n");
292 process_pseu_result(gph, NULL);
296 process_auth_discovery_ns_result(void* cls,
297 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
298 struct GNUNET_TIME_Absolute expiration,
299 const char *name, unsigned int rd_count,
300 const struct GNUNET_NAMESTORE_RecordData *rd,
301 const struct GNUNET_CRYPTO_RsaSignature *signature)
304 struct GNUNET_CRYPTO_ShortHashCode name_hash;
305 GNUNET_HashCode lookup_key;
306 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
307 GNUNET_HashCode name_hash_double;
308 GNUNET_HashCode zone_hash_double;
310 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
318 GNUNET_CRYPTO_short_hash("+", strlen("+"), &name_hash);
319 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
320 GNUNET_CRYPTO_short_hash_double (&gph->new_zone, &zone_hash_double);
321 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
322 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
324 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
325 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
326 "+", (char*)&lookup_key_string);
328 gph->timeout = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
329 &handle_auth_discovery_timeout, gph);
331 xquery = htonl(GNUNET_GNS_RECORD_PSEU);
333 GNUNET_assert(gph->get_handle == NULL);
334 gph->get_handle = GNUNET_DHT_get_start(dht_handle,
335 GNUNET_TIME_UNIT_FOREVER_REL,
336 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
338 DHT_GNS_REPLICATION_LEVEL,
342 &process_auth_discovery_dht_result,
346 for (i=0; i<rd_count; i++)
348 if ((strcmp(name, "+") == 0) &&
349 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
352 process_pseu_result(gph, (char*)rd[i].data);
359 * Callback called by namestore for a zone to name
362 * @param cls the closure
363 * @param zone_key the zone we queried
364 * @param expire the expiration time of the name
365 * @param name the name found or NULL
366 * @param rd_len number of records for the name
367 * @param rd the record data (PKEY) for the name
368 * @param signature the signature for the record data
371 process_zone_to_name_discover(void *cls,
372 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
373 struct GNUNET_TIME_Absolute expire,
376 const struct GNUNET_NAMESTORE_RecordData *rd,
377 const struct GNUNET_CRYPTO_RsaSignature *signature)
379 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
381 /* we found a match in our own zone */
384 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
385 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
391 GNUNET_NAMESTORE_lookup_record(namestore_handle,
394 GNUNET_GNS_RECORD_PSEU,
395 &process_auth_discovery_ns_result,
404 * Callback for new authories
406 * @param name the name given by delegation
407 * @param zone the authority
408 * @param our_zone our local zone
409 * @param key the private key of our authority
411 static void process_discovered_authority(char* name,
412 struct GNUNET_CRYPTO_ShortHashCode zone,
413 struct GNUNET_CRYPTO_ShortHashCode our_zone,
414 struct GNUNET_CRYPTO_RsaPrivateKey *key)
416 struct GetPseuAuthorityHandle *gph;
419 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
420 "GNS_AUTO_PSEU: New authority %s discovered\n",
423 gph = GNUNET_malloc(sizeof(struct GetPseuAuthorityHandle));
424 namelen = strlen(name) + 1;
425 memcpy(gph->name, name, namelen);
427 gph->new_zone = zone;
428 gph->zone = our_zone;
431 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
434 &process_zone_to_name_discover,
440 * Initialize the resolver
442 * @param nh the namestore handle
443 * @param dh the dht handle
444 * @param lz the local zone's hash
445 * @param max_bg_queries maximum number of parallel background queries in dht
446 * @param ignore_pending ignore records that still require user confirmation
448 * @return GNUNET_OK on success
451 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
452 struct GNUNET_DHT_Handle *dh,
453 struct GNUNET_CRYPTO_ShortHashCode lz,
454 unsigned long long max_bg_queries,
457 namestore_handle = nh;
461 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
462 max_allowed_background_queries = max_bg_queries;
463 ignore_pending_records = ignore_pending;
465 if ((namestore_handle != NULL) && (dht_handle != NULL))
469 return GNUNET_SYSERR;
473 * Cleanup background lookups
475 * @param cls closure to iterator
476 * @param node heap nodes
477 * @param element the resolver handle
478 * @param cost heap cost
479 * @return always GNUNET_YES
482 cleanup_pending_background_queries(void* cls,
483 struct GNUNET_CONTAINER_HeapNode *node,
485 GNUNET_CONTAINER_HeapCostType cost)
487 struct ResolverHandle *rh = (struct ResolverHandle *)element;
488 ResolverCleanupContinuation cont = cls;
490 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
491 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
493 GNUNET_DHT_get_stop(rh->get_handle);
494 rh->get_handle = NULL;
495 rh->proc(rh->proc_cls, rh, 0, NULL);
497 GNUNET_CONTAINER_heap_remove_node(node);
499 if (GNUNET_CONTAINER_heap_get_size(dht_lookup_heap) == 0)
511 gns_resolver_cleanup(ResolverCleanupContinuation cont)
513 unsigned int s = GNUNET_CONTAINER_heap_get_size(dht_lookup_heap);
514 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
515 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
518 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
519 &cleanup_pending_background_queries,
527 * Helper function to free resolver handle
529 * @param rh the handle to free
532 free_resolver_handle(struct ResolverHandle* rh)
534 struct AuthorityChain *ac;
535 struct AuthorityChain *ac_next;
540 ac = rh->authority_chain_head;
553 * Callback when record data is put into namestore
555 * @param cls the closure
556 * @param success GNUNET_OK on success
557 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
560 on_namestore_record_put_result(void *cls,
564 if (GNUNET_NO == success)
566 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
567 "GNS_NS: records already in namestore\n");
570 else if (GNUNET_YES == success)
572 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
573 "GNS_NS: records successfully put in namestore\n");
577 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
578 "GNS_NS: Error putting records into namestore: %s\n", emsg);
582 handle_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
584 struct ResolverHandle *rh = cls;
586 if (rh->timeout_cont)
587 rh->timeout_cont(rh->timeout_cont_cls, tc);
591 * Processor for background lookups in the DHT
593 * @param cls closure (NULL)
594 * @param rd_count number of records found (not 0)
595 * @param rd record data
598 background_lookup_result_processor(void *cls,
600 const struct GNUNET_NAMESTORE_RecordData *rd)
602 //We could do sth verbose/more useful here but it doesn't make any difference
603 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
604 "GNS_BG: background dht lookup for finished. (%d results)\n",
609 * Handle timeout for DHT requests
611 * @param cls the request handle as closure
612 * @param tc the task context
615 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
617 struct ResolverHandle *rh = cls;
618 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
619 char new_name[MAX_DNS_NAME_LENGTH];
621 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
622 "GNS_PHASE_REC-%d: dht lookup for query %s (%ds)timed out.\n",
623 rh->id, rh->name, rh->timeout.rel_value);
625 * Start resolution in bg
627 //strcpy(new_name, rh->name);
628 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
629 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
630 rh->name, GNUNET_GNS_TLD);
632 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
633 "GNS_PHASE_REC-%d: Starting background lookup for %s type %d\n",
634 rh->id, new_name, rlh->record_type);
636 gns_resolver_lookup_record(rh->authority,
640 GNUNET_TIME_UNIT_FOREVER_REL,
641 &background_lookup_result_processor,
643 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
645 GNUNET_DHT_get_stop (rh->get_handle);
646 rh->get_handle = NULL;
647 rh->proc(rh->proc_cls, rh, 0, NULL);
652 * Function called when we get a result from the dht
653 * for our record query
655 * @param cls the request handle
656 * @param exp lifetime
657 * @param key the key the record was stored under
658 * @param get_path get path
659 * @param get_path_length get path length
660 * @param put_path put path
661 * @param put_path_length put path length
662 * @param type the block type
663 * @param size the size of the record
664 * @param data the record data
667 process_record_result_dht(void* cls,
668 struct GNUNET_TIME_Absolute exp,
669 const GNUNET_HashCode * key,
670 const struct GNUNET_PeerIdentity *get_path,
671 unsigned int get_path_length,
672 const struct GNUNET_PeerIdentity *put_path,
673 unsigned int put_path_length,
674 enum GNUNET_BLOCK_Type type,
675 size_t size, const void *data)
677 struct ResolverHandle *rh;
678 struct RecordLookupHandle *rlh;
679 struct GNSNameRecordBlock *nrb;
680 uint32_t num_records;
682 char* rd_data = (char*)data;
686 rh = (struct ResolverHandle *)cls;
687 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
688 "GNS_PHASE_REC-%d: got dht result (size=%d)\n", rh->id, size);
693 //FIXME maybe check expiration here, check block type
696 rlh = (struct RecordLookupHandle *) rh->proc_cls;
697 nrb = (struct GNSNameRecordBlock*)data;
699 /* stop lookup and timeout task */
700 GNUNET_DHT_get_stop (rh->get_handle);
701 rh->get_handle = NULL;
703 if (rh->dht_heap_node != NULL)
705 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
706 rh->dht_heap_node = NULL;
709 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
711 GNUNET_SCHEDULER_cancel(rh->timeout_task);
712 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
715 rh->get_handle = NULL;
716 name = (char*)&nrb[1];
717 num_records = ntohl(nrb->rd_count);
719 struct GNUNET_NAMESTORE_RecordData rd[num_records];
721 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
722 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
724 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
729 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
730 "GNS_PHASE_REC-%d: Error deserializing data!\n", rh->id);
734 for (i=0; i<num_records; i++)
736 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
737 "GNS_PHASE_REC-%d: Got name: %s (wanted %s)\n",
738 rh->id, name, rh->name);
739 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
740 "GNS_PHASE_REC-%d: Got type: %d\n",
741 rh->id, rd[i].record_type);
742 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
743 "GNS_PHASE_REC-%d: Got data length: %d\n",
744 rh->id, rd[i].data_size);
745 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
746 "GNS_PHASE_REC-%d: Got flag %d\n",
747 rh->id, rd[i].flags);
749 if ((strcmp(name, rh->name) == 0) &&
750 (rd[i].record_type == rlh->record_type))
758 * FIXME check pubkey against existing key in namestore?
759 * https://gnunet.org/bugs/view.php?id=2179
762 /* Save to namestore */
763 GNUNET_NAMESTORE_record_put (namestore_handle,
770 &on_namestore_record_put_result, //cont
775 rh->proc(rh->proc_cls, rh, num_records, rd);
777 rh->proc(rh->proc_cls, rh, 0, NULL);
784 * Start DHT lookup for a (name -> query->record_type) record in
785 * rh->authority's zone
787 * @param rh the pending gns query context
790 resolve_record_dht(struct ResolverHandle *rh)
793 struct GNUNET_CRYPTO_ShortHashCode name_hash;
794 GNUNET_HashCode lookup_key;
795 GNUNET_HashCode name_hash_double;
796 GNUNET_HashCode zone_hash_double;
797 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
798 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
799 struct ResolverHandle *rh_heap_root;
801 GNUNET_CRYPTO_short_hash(rh->name, strlen(rh->name), &name_hash);
802 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
803 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
804 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
805 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
807 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
808 "GNS_PHASE_REC-%d: starting dht lookup for %s with key: %s\n",
809 rh->id, rh->name, (char*)&lookup_key_string);
811 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
812 rh->dht_heap_node = NULL;
814 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
817 * Update timeout if necessary
819 if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
822 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
823 "GNS_PHASE_REC-%d: Adjusting timeout\n", rh->id);
825 * Set timeout for authority lookup phase to 1/2
827 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
828 GNUNET_TIME_relative_divide(rh->timeout, 2),
829 &handle_lookup_timeout,
832 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
833 // &dht_lookup_timeout,
835 rh->timeout_cont = &dht_lookup_timeout;
836 rh->timeout_cont_cls = rh;
840 if (max_allowed_background_queries <=
841 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
843 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
844 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
845 rh_heap_root->get_handle = NULL;
846 rh_heap_root->dht_heap_node = NULL;
848 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
849 "GNS_PHASE_REC-%d: Replacing oldest background query for %s\n",
850 rh->id, rh_heap_root->name);
851 rh_heap_root->proc(rh_heap_root->proc_cls,
856 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
858 GNUNET_TIME_absolute_get().abs_value);
861 xquery = htonl(rlh->record_type);
863 GNUNET_assert(rh->get_handle == NULL);
864 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
865 GNUNET_TIME_UNIT_FOREVER_REL,
866 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
868 DHT_GNS_REPLICATION_LEVEL,
872 &process_record_result_dht,
879 * Namestore calls this function if we have record for this name.
880 * (or with rd_count=0 to indicate no matches)
882 * @param cls the pending query
883 * @param key the key of the zone we did the lookup
884 * @param expiration expiration date of the namestore entry
885 * @param name the name for which we need an authority
886 * @param rd_count the number of records with 'name'
887 * @param rd the record data
888 * @param signature the signature of the authority for the record data
891 process_record_result_ns(void* cls,
892 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
893 struct GNUNET_TIME_Absolute expiration,
894 const char *name, unsigned int rd_count,
895 const struct GNUNET_NAMESTORE_RecordData *rd,
896 const struct GNUNET_CRYPTO_RsaSignature *signature)
898 struct ResolverHandle *rh;
899 struct RecordLookupHandle *rlh;
900 struct GNUNET_TIME_Relative remaining_time;
901 struct GNUNET_CRYPTO_ShortHashCode zone;
903 rh = (struct ResolverHandle *) cls;
904 rlh = (struct RecordLookupHandle *)rh->proc_cls;
905 GNUNET_CRYPTO_short_hash(key,
906 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
908 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
916 rh->status |= RSL_RECORD_EXISTS;
919 if (remaining_time.rel_value == 0)
921 rh->status |= RSL_RECORD_EXPIRED;
927 * Lookup terminated and no results
929 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
930 "GNS_PHASE_REC-%d: Namestore lookup for %s terminated without results\n",
933 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
934 "GNS_PHASE_REC-%d: Record %s unknown in namestore\n",
937 * Our zone and no result? Cannot resolve TT
939 rh->proc(rh->proc_cls, rh, 0, NULL);
946 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
947 "GNS_PHASE_REC-%d: Processing additional result %s from namestore\n",
950 for (i=0; i<rd_count;i++)
953 if (rd[i].record_type != rlh->record_type)
956 if (ignore_pending_records &&
957 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
959 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
960 "GNS_PHASE_REC-%d: Record %s is awaiting user confirmation. Skipping\n",
965 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
968 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
969 "GNS_PHASE_REC-%d: This record is expired. Skipping\n",
981 if (rh->answered == 0)
983 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
984 "GNS_PHASE_REC-%d: No answers found. This is odd!\n", rh->id);
985 rh->proc(rh->proc_cls, rh, 0, NULL);
989 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
990 "GNS_PHASE_REC-%d: Found %d answer(s) to query in %d records!\n",
991 rh->id, rh->answered, rd_count);
993 rh->proc(rh->proc_cls, rh, rd_count, rd);
999 * The final phase of resolution.
1000 * rh->name is a name that is canonical and we do not have a delegation.
1001 * Query namestore for this record
1003 * @param rh the pending lookup
1006 resolve_record_ns(struct ResolverHandle *rh)
1008 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1010 /* We cancel here as to not include the ns lookup in the timeout */
1011 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1013 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1014 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1018 * Try to resolve this record in our namestore.
1019 * The name to resolve is now in rh->authority_name
1020 * since we tried to resolve it to an authority
1023 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1027 &process_record_result_ns,
1034 * Handle timeout for DHT requests
1036 * @param cls the request handle as closure
1037 * @param tc the task context
1040 dht_authority_lookup_timeout(void *cls,
1041 const struct GNUNET_SCHEDULER_TaskContext *tc)
1043 struct ResolverHandle *rh = cls;
1044 struct RecordLookupHandle *rlh = rh->proc_cls;
1045 char new_name[MAX_DNS_NAME_LENGTH];
1047 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1048 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%ds)timed out.\n",
1049 rh->id, rh->authority_name, rh->timeout.rel_value);
1051 rh->status |= RSL_TIMED_OUT;
1053 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1055 GNUNET_DHT_get_stop (rh->get_handle);
1056 rh->get_handle = NULL;
1058 if (strcmp(rh->name, "") == 0)
1061 * promote authority back to name and try to resolve record
1063 strcpy(rh->name, rh->authority_name);
1064 rh->proc(rh->proc_cls, rh, 0, NULL);
1069 * Start resolution in bg
1071 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
1072 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
1073 //strcpy(new_name, rh->name);
1074 //strcpy(new_name+strlen(new_name), ".");
1075 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1077 strcpy(rh->name, new_name);
1079 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1080 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1081 rh->id, rh->name, rlh->record_type);
1083 gns_resolver_lookup_record(rh->authority,
1087 GNUNET_TIME_UNIT_FOREVER_REL,
1088 &background_lookup_result_processor,
1091 rh->proc(rh->proc_cls, rh, 0, NULL);
1095 static void resolve_delegation_dht(struct ResolverHandle *rh);
1098 static void resolve_delegation_ns(struct ResolverHandle *rh);
1102 * Namestore resolution for delegation finished. Processing result.
1104 * @param cls the closure
1105 * @param rh resolver handle
1106 * @param rd_count number of results (always 0)
1107 * @param rd record data (always NULL)
1110 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1111 unsigned int rd_count,
1112 const struct GNUNET_NAMESTORE_RecordData *rd);
1116 * Function called when we get a result from the dht
1117 * for our query. Recursively tries to resolve authorities
1120 * @param cls the request handle
1121 * @param exp lifetime
1122 * @param key the key the record was stored under
1123 * @param get_path get path
1124 * @param get_path_length get path length
1125 * @param put_path put path
1126 * @param put_path_length put path length
1127 * @param type the block type
1128 * @param size the size of the record
1129 * @param data the record data
1132 process_delegation_result_dht(void* cls,
1133 struct GNUNET_TIME_Absolute exp,
1134 const GNUNET_HashCode * key,
1135 const struct GNUNET_PeerIdentity *get_path,
1136 unsigned int get_path_length,
1137 const struct GNUNET_PeerIdentity *put_path,
1138 unsigned int put_path_length,
1139 enum GNUNET_BLOCK_Type type,
1140 size_t size, const void *data)
1142 struct ResolverHandle *rh;
1143 struct GNSNameRecordBlock *nrb;
1144 uint32_t num_records;
1146 char* rd_data = (char*) data;
1149 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
1150 GNUNET_HashCode zone_hash_double, name_hash_double;
1152 rh = (struct ResolverHandle *)cls;
1154 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1155 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
1160 nrb = (struct GNSNameRecordBlock*)data;
1162 /* stop dht lookup and timeout task */
1163 GNUNET_DHT_get_stop (rh->get_handle);
1165 rh->get_handle = NULL;
1167 if (rh->dht_heap_node != NULL)
1169 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
1170 rh->dht_heap_node = NULL;
1173 num_records = ntohl(nrb->rd_count);
1174 name = (char*)&nrb[1];
1176 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1178 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1179 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
1181 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1186 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1187 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
1192 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1193 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
1194 rh->id, name, rh->authority_name);
1195 for (i=0; i<num_records; i++)
1198 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1199 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
1200 rh->id, name, rh->authority_name);
1201 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1202 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
1203 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
1204 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1205 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
1206 rh->id, rd[i].data_size);
1207 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1208 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
1209 rh->id, rd[i].flags);
1211 if ((strcmp(name, rh->authority_name) == 0) &&
1212 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
1214 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1215 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
1218 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
1219 struct AuthorityChain *auth =
1220 GNUNET_malloc(sizeof(struct AuthorityChain));
1221 auth->zone = rh->authority;
1222 memset(auth->name, 0, strlen(rh->authority_name)+1);
1223 strcpy(auth->name, rh->authority_name);
1224 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1225 rh->authority_chain_tail,
1228 /** try to import pkey if private key available */
1230 process_discovered_authority(name, auth->zone,
1231 rh->authority_chain_tail->zone,
1238 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
1239 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1240 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
1241 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
1243 /* Save to namestore */
1244 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
1247 GNUNET_NAMESTORE_record_put (namestore_handle,
1254 &on_namestore_record_put_result, //cont
1264 * FIXME in this case. should we ask namestore again?
1266 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1267 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
1268 rh->id, rh->authority_name, rh->name);
1269 if (strcmp(rh->name, "") == 0)
1271 rh->proc(rh->proc_cls, rh, 0, NULL);
1275 rh->proc = &handle_delegation_ns;
1276 resolve_delegation_ns(rh);
1282 * No pkey but name exists
1285 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1286 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
1287 rh->id, rh->authority_name, rh->name);
1288 if (strcmp(rh->name, "") == 0)
1289 strcpy(rh->name, rh->authority_name);
1291 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
1292 rh->name, rh->authority_name); //FIXME ret
1294 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1295 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
1296 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1297 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
1299 rh->proc(rh->proc_cls, rh, 0, NULL);
1302 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
1303 +(MAX_DNS_NAME_LENGTH*2)
1304 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
1308 expand_plus(char** dest, char* src, char* repl)
1311 unsigned int s_len = strlen(src)+1;
1313 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1314 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
1318 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1319 "GNS_POSTPROCESS: %s to short\n", src);
1321 /* no postprocessing */
1322 memcpy(*dest, src, s_len+1);
1326 if (0 == strcmp(src+s_len-3, ".+"))
1328 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1329 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
1330 memset(*dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
1332 pos = *dest+s_len-2;
1334 pos += strlen(repl);
1335 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1336 "GNS_POSTPROCESS: Expanded to %s\n", *dest);
1340 memcpy(*dest, src, s_len+1);
1348 finish_lookup(struct ResolverHandle *rh,
1349 struct RecordLookupHandle* rlh,
1350 unsigned int rd_count,
1351 const struct GNUNET_NAMESTORE_RecordData *rd)
1354 char new_rr_data[MAX_DNS_NAME_LENGTH];
1355 char new_mx_data[MAX_MX_LENGTH];
1356 char new_soa_data[MAX_SOA_LENGTH];
1357 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
1360 unsigned int offset;
1362 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1363 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1366 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
1368 for (i = 0; i < rd_count; i++)
1371 if (rd[i].record_type != GNUNET_GNS_RECORD_TYPE_NS &&
1372 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_CNAME &&
1373 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
1374 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_SOA)
1376 p_rd[i].data = rd[i].data;
1381 * for all those records we 'should'
1382 * also try to resolve the A/AAAA records (RFC1035)
1383 * This is a feature and not important
1386 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1387 "GNS_POSTPROCESS: Postprocessing\n");
1389 if (strcmp(rh->name, "+") == 0)
1390 repl_string = rlh->name;
1392 repl_string = rlh->name+strlen(rh->name)+1;
1395 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
1397 memcpy(new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
1398 offset = sizeof(uint16_t);
1399 pos = new_mx_data+offset;
1400 expand_plus(&pos, (char*)rd[i].data+sizeof(uint16_t),
1402 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
1403 p_rd[i].data = new_mx_data;
1404 p_rd[i].data_size = offset;
1406 else if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_SOA)
1408 /* expand mname and rname */
1410 expand_plus(&pos, (char*)rd[i].data, repl_string);
1411 offset = strlen(new_soa_data)+1;
1412 pos = new_soa_data+offset;
1413 expand_plus(&pos, (char*)rd[i].data+offset, repl_string);
1414 offset += strlen(new_soa_data+offset)+1;
1415 /* cpy the 4 numbers serial refresh retry and expire */
1416 memcpy(new_soa_data+offset, (char*)rd[i].data+offset, sizeof(uint32_t)*5);
1417 offset += sizeof(uint32_t)*5;
1418 p_rd[i].data_size = offset;
1419 p_rd[i].data = new_soa_data;
1424 expand_plus(&pos, (char*)rd[i].data, repl_string);
1425 p_rd[i].data_size = strlen(new_rr_data)+1;
1426 p_rd[i].data = new_rr_data;
1431 rlh->proc(rlh->proc_cls, rd_count, p_rd);
1437 * Process DHT lookup result for record.
1439 * @param cls the closure
1440 * @param rh resolver handle
1441 * @param rd_count number of results
1442 * @param rd record data
1445 handle_record_dht(void* cls, struct ResolverHandle *rh,
1446 unsigned int rd_count,
1447 const struct GNUNET_NAMESTORE_RecordData *rd)
1449 struct RecordLookupHandle* rlh;
1451 rlh = (struct RecordLookupHandle*)cls;
1454 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1455 "GNS_PHASE_REC-%d: No records for %s found in DHT. Aborting\n",
1457 /* give up, cannot resolve */
1458 finish_lookup(rh, rlh, 0, NULL);
1459 free_resolver_handle(rh);
1463 /* results found yay */
1464 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1465 "GNS_PHASE_REC-%d: Record resolved from DHT!", rh->id);
1467 finish_lookup(rh, rlh, rd_count, rd);
1468 free_resolver_handle(rh);
1474 * Process namestore lookup result for record.
1476 * @param cls the closure
1477 * @param rh resolver handle
1478 * @param rd_count number of results
1479 * @param rd record data
1482 handle_record_ns(void* cls, struct ResolverHandle *rh,
1483 unsigned int rd_count,
1484 const struct GNUNET_NAMESTORE_RecordData *rd)
1486 struct RecordLookupHandle* rlh;
1487 rlh = (struct RecordLookupHandle*) cls;
1490 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1491 "GNS_PHASE_REC-%d: NS returned no records. (status: %d)!\n",
1496 * There are 4 conditions that have to met for us to consult the DHT:
1497 * 1. The entry in the DHT is RSL_RECORD_EXPIRED AND
1498 * 2. No entry in the NS existed AND
1499 * 3. The zone queried is not the local resolver's zone AND
1500 * 4. The name that was looked up is '+'
1501 * because if it was any other canonical name we either already queried
1502 * the DHT for the authority in the authority lookup phase (and thus
1503 * would already have an entry in the NS for the record)
1505 if (rh->status & (RSL_RECORD_EXPIRED | !RSL_RECORD_EXISTS) &&
1506 GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1508 (strcmp(rh->name, "+") == 0))
1510 rh->proc = &handle_record_dht;
1511 resolve_record_dht(rh);
1514 /* give up, cannot resolve */
1515 finish_lookup(rh, rlh, 0, NULL);
1516 free_resolver_handle(rh);
1520 /* results found yay */
1521 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1522 "GNS_PHASE_REC-%d: Record resolved from namestore!", rh->id);
1524 finish_lookup(rh, rlh, rd_count, rd);
1526 free_resolver_handle(rh);
1532 * Determine if this name is canonical.
1534 * a.b.gnunet = not canonical
1537 * @param name the name to test
1538 * @return 1 if canonical
1541 is_canonical(char* name)
1543 uint32_t len = strlen(name);
1546 for (i=0; i<len; i++)
1548 if (*(name+i) == '.')
1555 * Move one level up in the domain hierarchy and return the
1556 * passed top level domain.
1558 * @param name the domain
1559 * @param dest the destination where the tld will be put
1562 pop_tld(char* name, char* dest)
1566 if (is_canonical(name))
1573 for (len = strlen(name); len > 0; len--)
1575 if (*(name+len) == '.')
1585 strcpy(dest, (name+len+1));
1589 * Checks if name is in tld
1591 * @param name the name to check
1592 * @param tld the TLD to check for
1593 * @return GNUNET_YES or GNUNET_NO
1596 is_tld(const char* name, const char* tld)
1600 if (strlen(name) <= strlen(tld))
1605 offset = strlen(name)-strlen(tld);
1606 if (strcmp(name+offset, tld) != 0)
1608 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1609 "%s is not in .%s TLD\n", name, tld);
1616 * DHT resolution for delegation finished. Processing result.
1618 * @param cls the closure
1619 * @param rh resolver handle
1620 * @param rd_count number of results (always 0)
1621 * @param rd record data (always NULL)
1624 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
1625 unsigned int rd_count,
1626 const struct GNUNET_NAMESTORE_RecordData *rd)
1628 struct RecordLookupHandle* rlh;
1629 rlh = (struct RecordLookupHandle*) cls;
1632 if (strcmp(rh->name, "") == 0)
1634 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
1636 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1637 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
1639 finish_lookup(rh, rlh, rd_count, rd);
1640 free_resolver_handle(rh);
1643 /* We resolved full name for delegation. resolving record */
1644 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1645 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
1647 strcpy(rh->name, "+\0");
1648 rh->proc = &handle_record_ns;
1649 resolve_record_ns(rh);
1654 * we still have some left
1656 if (is_canonical(rh->name))
1658 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1659 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
1662 rh->proc = &handle_record_ns;
1663 resolve_record_ns(rh);
1666 /* give up, cannot resolve */
1667 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1668 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
1670 finish_lookup(rh, rlh, 0, NULL);
1671 free_resolver_handle(rh);
1676 * Start DHT lookup for a name -> PKEY (compare NS) record in
1677 * rh->authority's zone
1679 * @param rh the pending gns query
1682 resolve_delegation_dht(struct ResolverHandle *rh)
1685 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1686 GNUNET_HashCode name_hash_double;
1687 GNUNET_HashCode zone_hash_double;
1688 GNUNET_HashCode lookup_key;
1689 struct ResolverHandle *rh_heap_root;
1691 pop_tld(rh->name, rh->authority_name);
1692 GNUNET_CRYPTO_short_hash(rh->authority_name,
1693 strlen(rh->authority_name),
1695 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1696 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
1697 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
1699 rh->dht_heap_node = NULL;
1701 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1703 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1704 // &dht_authority_lookup_timeout,
1706 rh->timeout_cont = &dht_authority_lookup_timeout;
1707 rh->timeout_cont_cls = rh;
1711 if (max_allowed_background_queries <=
1712 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1714 /* terminate oldest lookup */
1715 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1716 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
1717 rh_heap_root->dht_heap_node = NULL;
1719 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1720 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
1721 rh->id, rh_heap_root->authority_name);
1723 rh_heap_root->proc(rh_heap_root->proc_cls,
1728 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1730 GNUNET_TIME_absolute_get().abs_value);
1733 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1735 GNUNET_assert(rh->get_handle == NULL);
1736 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1737 GNUNET_TIME_UNIT_FOREVER_REL,
1738 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1740 DHT_GNS_REPLICATION_LEVEL,
1744 &process_delegation_result_dht,
1751 * Namestore resolution for delegation finished. Processing result.
1753 * @param cls the closure
1754 * @param rh resolver handle
1755 * @param rd_count number of results (always 0)
1756 * @param rd record data (always NULL)
1759 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1760 unsigned int rd_count,
1761 const struct GNUNET_NAMESTORE_RecordData *rd)
1763 struct RecordLookupHandle* rlh;
1764 rlh = (struct RecordLookupHandle*) cls;
1766 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1767 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
1768 rh->id, rh->status);
1770 if (strcmp(rh->name, "") == 0)
1772 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
1774 GNUNET_assert(rd_count == 1);
1775 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1776 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
1778 finish_lookup(rh, rlh, rd_count, rd);
1779 free_resolver_handle(rh);
1782 /* We resolved full name for delegation. resolving record */
1783 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1784 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
1786 strcpy(rh->name, "+\0");
1787 rh->proc = &handle_record_ns;
1788 resolve_record_ns(rh);
1793 * we still have some left
1794 * check if authority in ns is fresh
1796 * or we are authority
1798 if (((rh->status & RSL_RECORD_EXISTS) && (!(rh->status & RSL_RECORD_EXPIRED)))
1799 || !GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1802 if (is_canonical(rh->name))
1804 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1805 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
1808 rh->proc = &handle_record_ns;
1809 resolve_record_ns(rh);
1813 /* give up, cannot resolve */
1814 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1815 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
1818 finish_lookup(rh, rlh, rd_count, rd);
1819 //rlh->proc(rlh->proc_cls, 0, NULL);
1824 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1825 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
1827 rh->proc = &handle_delegation_dht;
1828 resolve_delegation_dht(rh);
1834 * This is a callback function that should give us only PKEY
1835 * records. Used to query the namestore for the authority (PKEY)
1836 * for 'name'. It will recursively try to resolve the
1837 * authority for a given name from the namestore.
1839 * @param cls the pending query
1840 * @param key the key of the zone we did the lookup
1841 * @param expiration expiration date of the record data set in the namestore
1842 * @param name the name for which we need an authority
1843 * @param rd_count the number of records with 'name'
1844 * @param rd the record data
1845 * @param signature the signature of the authority for the record data
1848 process_delegation_result_ns(void* cls,
1849 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1850 struct GNUNET_TIME_Absolute expiration,
1852 unsigned int rd_count,
1853 const struct GNUNET_NAMESTORE_RecordData *rd,
1854 const struct GNUNET_CRYPTO_RsaSignature *signature)
1856 struct ResolverHandle *rh;
1857 struct GNUNET_TIME_Relative remaining_time;
1858 struct GNUNET_CRYPTO_ShortHashCode zone;
1859 char new_name[MAX_DNS_NAME_LENGTH];
1861 rh = (struct ResolverHandle *)cls;
1862 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1863 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
1866 GNUNET_CRYPTO_short_hash(key,
1867 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1869 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1875 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1876 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
1878 rh->status |= RSL_RECORD_EXISTS;
1881 if (remaining_time.rel_value == 0)
1883 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1884 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
1886 rh->status |= RSL_RECORD_EXPIRED;
1890 * No authority found in namestore.
1895 * We did not find an authority in the namestore
1900 * Promote this authority back to a name maybe it is
1903 if (strcmp(rh->name, "") == 0)
1905 /* simply promote back */
1906 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1907 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
1908 rh->id, rh->authority_name);
1909 strcpy(rh->name, rh->authority_name);
1913 /* add back to existing name */
1914 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1915 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
1916 rh->id, rh->authority_name, rh->name);
1917 //memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1918 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
1919 rh->name, rh->authority_name);
1920 //strcpy(new_name, rh->name);
1921 //strcpy(new_name+strlen(new_name), ".");
1922 //strcpy(new_name+strlen(new_name), rh->authority_name);
1923 strcpy(rh->name, new_name);
1924 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1925 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n", rh->id, rh->name);
1927 rh->proc(rh->proc_cls, rh, 0, NULL);
1932 * We found an authority that may be able to help us
1933 * move on with query
1934 * Note only 1 pkey should have been returned.. anything else would be strange
1937 for (i=0; i<rd_count;i++)
1940 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1943 if (ignore_pending_records &&
1944 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1946 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1947 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
1953 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1956 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1957 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
1959 if (remaining_time.rel_value == 0)
1961 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1962 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
1964 rh->authority_chain_head->fresh = 0;
1965 rh->proc(rh->proc_cls, rh, 0, NULL);
1973 * Resolve rest of query with new authority
1975 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1976 memcpy(&rh->authority, rd[i].data,
1977 sizeof(struct GNUNET_CRYPTO_ShortHashCode));
1978 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1979 auth->zone = rh->authority;
1980 memset(auth->name, 0, strlen(rh->authority_name)+1);
1981 strcpy(auth->name, rh->authority_name);
1982 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1983 rh->authority_chain_tail,
1986 /** try to import pkey if private key available */
1988 process_discovered_authority((char*)name, auth->zone,
1989 rh->authority_chain_tail->zone,
1992 * We are done with PKEY resolution if name is empty
1993 * else resolve again with new authority
1995 if (strcmp(rh->name, "") == 0)
1996 rh->proc(rh->proc_cls, rh, rd_count, rd);
1998 resolve_delegation_ns(rh);
2005 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2006 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
2008 * If we have found some records for the LAST label
2009 * we return the results. Else null.
2011 if (strcmp(rh->name, "") == 0)
2013 /* simply promote back */
2014 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2015 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
2016 rh->id, rh->authority_name);
2017 strcpy(rh->name, rh->authority_name);
2018 rh->proc(rh->proc_cls, rh, rd_count, rd);
2022 rh->proc(rh->proc_cls, rh, 0, NULL);
2028 * Resolve the delegation chain for the request in our namestore
2030 * @param rh the resolver handle
2033 resolve_delegation_ns(struct ResolverHandle *rh)
2035 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2036 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
2038 pop_tld(rh->name, rh->authority_name);
2039 GNUNET_NAMESTORE_lookup_record(namestore_handle,
2042 GNUNET_GNS_RECORD_ANY,
2043 &process_delegation_result_ns,
2050 * Lookup of a record in a specific zone
2051 * calls lookup result processor on result
2053 * @param zone the root zone
2054 * @param record_type the record type to look up
2055 * @param name the name to look up
2056 * @param key a private key for use with PSEU import (can be NULL)
2057 * @param timeout timeout for resolution
2058 * @param proc the processor to call on result
2059 * @param cls the closure to pass to proc
2062 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
2063 uint32_t record_type,
2065 struct GNUNET_CRYPTO_RsaPrivateKey *key,
2066 struct GNUNET_TIME_Relative timeout,
2067 RecordLookupProcessor proc,
2070 struct ResolverHandle *rh;
2071 struct RecordLookupHandle* rlh;
2072 char string_hash[MAX_DNS_LABEL_LENGTH];
2073 char nzkey[MAX_DNS_LABEL_LENGTH];
2074 char* nzkey_ptr = nzkey;
2076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2077 "Starting resolution for %s (type=%d)!\n",
2081 if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2084 "%s is canonical and not gnunet -> cannot resolve!\n", name);
2089 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
2090 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2092 rh->authority = zone;
2096 rh->timeout = timeout;
2097 rh->get_handle = NULL;
2098 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2101 * Set timeout for authority lookup phase to 1/2
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2104 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
2105 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
2106 GNUNET_TIME_relative_divide(timeout, 2),
2107 &handle_lookup_timeout,
2109 rh->timeout_cont = &dht_authority_lookup_timeout;
2110 rh->timeout_cont_cls = rh;
2114 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
2115 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2118 if (strcmp(GNUNET_GNS_TLD, name) == 0)
2121 * Only 'gnunet' given
2123 strcpy(rh->name, "\0");
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2128 "Checking for TLD...\n");
2129 if (is_zkey_tld(name) == GNUNET_YES)
2131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2134 * This is a zkey tld
2135 * build hash and use as initial authority
2138 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
2139 memcpy(rh->name, name,
2140 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
2141 pop_tld(rh->name, string_hash);
2143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2144 "ZKEY is %s!\n", string_hash);
2146 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
2148 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
2151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2152 "Cannot convert ZKEY %s to hash!\n", string_hash);
2162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2165 * Presumably GNUNET tld
2168 strlen(name)-strlen(GNUNET_GNS_TLD));
2169 memcpy(rh->name, name,
2170 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2175 * Initialize authority chain
2177 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2178 rh->authority_chain_head->prev = NULL;
2179 rh->authority_chain_head->next = NULL;
2180 rh->authority_chain_tail = rh->authority_chain_head;
2181 rh->authority_chain_head->zone = rh->authority;
2184 * Copy original query into lookup handle
2186 rlh->record_type = record_type;
2187 memset(rlh->name, 0, strlen(name) + 1);
2188 strcpy(rlh->name, name);
2190 rlh->proc_cls = cls;
2192 rh->proc = &handle_delegation_ns;
2193 resolve_delegation_ns(rh);
2196 /******** END Record Resolver ***********/
2200 * Callback calles by namestore for a zone to name
2203 * @param cls the closure
2204 * @param zone_key the zone we queried
2205 * @param expire the expiration time of the name
2206 * @param name the name found or NULL
2207 * @param rd_len number of records for the name
2208 * @param rd the record data (PKEY) for the name
2209 * @param signature the signature for the record data
2212 process_zone_to_name_shorten(void *cls,
2213 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
2214 struct GNUNET_TIME_Absolute expire,
2216 unsigned int rd_len,
2217 const struct GNUNET_NAMESTORE_RecordData *rd,
2218 const struct GNUNET_CRYPTO_RsaSignature *signature)
2220 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
2221 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
2222 struct AuthorityChain *next_authority;
2224 char result[MAX_DNS_NAME_LENGTH];
2225 char next_authority_name[MAX_DNS_LABEL_LENGTH];
2228 /* we found a match in our own zone */
2231 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2232 "result strlen %d\n", strlen(name));
2233 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
2234 memset(result, 0, answer_len);
2235 if (strlen(rh->name) > 0)
2237 strcpy(result, rh->name);
2238 strcpy(result+strlen(rh->name), ".");
2241 strcpy(result+strlen(result), name);
2242 strcpy(result+strlen(result), ".");
2243 strcpy(result+strlen(result), GNUNET_GNS_TLD);
2245 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2246 "Sending shorten result %s\n", result);
2248 nsh->proc(nsh->proc_cls, result);
2250 free_resolver_handle(rh);
2252 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2255 /* our zone, just append .gnunet */
2256 answer_len = strlen(rh->name) + strlen(GNUNET_GNS_TLD) + 2;
2257 memset(result, 0, answer_len);
2258 strcpy(result, rh->name);
2259 strcpy(result+strlen(rh->name), ".");
2260 strcpy(result+strlen(rh->name)+1, GNUNET_GNS_TLD);
2262 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2263 "Our zone: Sending name as shorten result %s\n", rh->name);
2265 nsh->proc(nsh->proc_cls, result);
2267 free_resolver_handle(rh);
2273 * continue with next authority
2275 next_authority = rh->authority_chain_head;
2277 GNUNET_snprintf(next_authority_name, MAX_DNS_NAME_LENGTH,
2278 "%s.%s", rh->name, next_authority->name);
2280 strcpy(rh->name, next_authority_name);
2281 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2282 "No PSEU found for authority %s. Promoting back: %s\n",
2283 next_authority->name, rh->name);
2285 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
2286 rh->authority_chain_tail,
2289 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2290 &rh->authority_chain_tail->zone,
2291 &rh->authority_chain_head->zone,
2292 &process_zone_to_name_shorten,
2298 * DHT resolution for delegation. Processing result.
2300 * @param cls the closure
2301 * @param rh resolver handle
2302 * @param rd_count number of results
2303 * @param rd record data
2306 handle_delegation_dht_bg_shorten(void* cls, struct ResolverHandle *rh,
2307 unsigned int rd_count,
2308 const struct GNUNET_NAMESTORE_RecordData *rd)
2311 /* We resolved full name for delegation. resolving record */
2312 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2313 "GNS_SHORTEN: Resolved up to %s for delegation via DHT in background.\n",
2315 free_resolver_handle(rh);
2319 * Process result from namestore delegation lookup
2320 * for shorten operation
2322 * @param cls the client shorten handle
2323 * @param rh the resolver handle
2324 * @param rd_count number of results (0)
2325 * @param rd data (NULL)
2328 handle_delegation_ns_shorten(void* cls,
2329 struct ResolverHandle *rh,
2331 const struct GNUNET_NAMESTORE_RecordData *rd)
2333 struct NameShortenHandle *nsh;
2334 char result[MAX_DNS_NAME_LENGTH];
2336 struct ResolverHandle *rh_bg;
2338 nsh = (struct NameShortenHandle *)cls;
2341 * At this point rh->name contains the part of the name
2342 * that we do not have a PKEY in our namestore to resolve.
2343 * The authority chain in the resolver handle is now
2344 * useful to backtrack if needed
2347 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2348 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2350 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2354 * This is our zone append .gnunet unless name is empty
2355 * (it shouldn't be, usually FIXME what happens if we
2356 * shorten to our zone to a "" record??)
2359 answer_len = strlen(rh->name) + strlen(GNUNET_GNS_TLD) + 2;
2360 memset(result, 0, answer_len);
2361 strcpy(result, rh->name);
2362 strcpy(result+strlen(rh->name), ".");
2363 strcpy(result+strlen(rh->name)+1, GNUNET_GNS_TLD);
2365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2366 "Our zone: Sending name as shorten result %s\n", rh->name);
2368 nsh->proc(nsh->proc_cls, result);
2370 free_resolver_handle(rh);
2375 * we have to this before zone to name for rh might
2379 if (!is_canonical(rh->name))
2381 rh_bg = GNUNET_malloc(sizeof(struct ResolverHandle));
2382 memcpy(rh_bg, rh, sizeof(struct ResolverHandle));
2386 /* backtrack authorities for names */
2387 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2388 &rh->authority_chain_tail->zone, //ours
2389 &rh->authority_chain_head->zone,
2390 &process_zone_to_name_shorten,
2399 * If authority resolution is incomplete we can do a background lookup
2400 * of the full name so that next time we can (likely) fully or at least
2401 * further shorten the name
2403 rh_bg->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2404 rh_bg->authority_chain_tail = rh_bg->authority_chain_head;
2405 rh_bg->authority_chain_head->zone = rh_bg->authority;
2407 rh_bg->proc = &handle_delegation_dht_bg_shorten;
2408 rh_bg->proc_cls = NULL;
2410 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2411 "GNS_SHORTEN: Starting background lookup for %s\n",
2414 resolve_delegation_dht(rh_bg);
2420 * Callback calles by namestore for a zone to name
2423 * @param cls the closure
2424 * @param zone_key the zone we queried
2425 * @param expire the expiration time of the name
2426 * @param name the name found or NULL
2427 * @param rd_len number of records for the name
2428 * @param rd the record data (PKEY) for the name
2429 * @param signature the signature for the record data
2432 process_zone_to_name_zkey(void *cls,
2433 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
2434 struct GNUNET_TIME_Absolute expire,
2436 unsigned int rd_len,
2437 const struct GNUNET_NAMESTORE_RecordData *rd,
2438 const struct GNUNET_CRYPTO_RsaSignature *signature)
2440 struct ResolverHandle *rh = cls;
2441 struct NameShortenHandle *nsh = rh->proc_cls;
2442 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
2443 char new_name[MAX_DNS_NAME_LENGTH];
2445 /* zkey not in our zone */
2449 * In this case we have not given this PKEY a name (yet)
2450 * It is either just not in our zone or not even cached
2451 * Since we do not know at this point we will not try to shorten
2452 * because PKEY import will happen if the user follows the zkey
2455 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
2457 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2458 "No name found for zkey %s returning verbatim!\n", enc);
2459 if (strcmp(rh->name, "") != 0)
2460 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
2461 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
2463 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2464 enc, GNUNET_GNS_TLD_ZKEY);
2465 nsh->proc(nsh->proc_cls, new_name);
2467 free_resolver_handle(rh);
2471 if (strcmp(rh->name, "") != 0)
2472 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2475 strcpy(new_name, name);
2477 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2478 "Continue shorten for %s!\n", new_name);
2480 strcpy(rh->name, new_name);
2482 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2483 rh->authority_chain_tail = rh->authority_chain_head;
2484 rh->authority_chain_head->zone = rh->authority;
2487 /* Start delegation resolution in our namestore */
2488 resolve_delegation_ns(rh);
2493 * Shorten api from resolver
2495 * @param zone the zone to use
2496 * @param name the name to shorten
2497 * @param key optional private key for background lookups and PSEU import
2498 * @param proc the processor to call with result
2499 * @param proc_cls closure to pass to proc
2502 gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode zone,
2504 struct GNUNET_CRYPTO_RsaPrivateKey *key,
2505 ShortenResultProcessor proc,
2508 struct ResolverHandle *rh;
2509 struct NameShortenHandle *nsh;
2510 char string_hash[MAX_DNS_LABEL_LENGTH];
2511 struct GNUNET_CRYPTO_ShortHashCode zkey;
2512 char nzkey[MAX_DNS_LABEL_LENGTH];
2513 char* nzkey_ptr = nzkey;
2516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2517 "Starting shorten for %s!\n", name);
2519 if (is_canonical((char*)name))
2521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2522 "%s is canonical. Returning verbatim\n", name);
2523 proc(proc_cls, name);
2527 nsh = GNUNET_malloc(sizeof (struct NameShortenHandle));
2530 nsh->proc_cls = proc_cls;
2532 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2533 rh->authority = zone;
2536 rh->proc = &handle_delegation_ns_shorten;
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2541 "Checking for TLD...\n");
2542 if (is_zkey_tld(name) == GNUNET_YES)
2544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2547 * This is a zkey tld
2548 * build hash and use as initial authority
2552 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
2553 memcpy(rh->name, name,
2554 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
2555 pop_tld(rh->name, string_hash);
2557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2558 "ZKEY is %s!\n", string_hash);
2560 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
2562 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
2565 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2566 "Cannot convert ZKEY %s to hash!\n", nzkey);
2569 proc(proc_cls, name);
2573 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2576 &process_zone_to_name_zkey,
2583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2586 * Presumably GNUNET tld
2589 strlen(name)-strlen(GNUNET_GNS_TLD));
2590 memcpy(rh->name, name,
2591 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2594 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2595 rh->authority_chain_tail = rh->authority_chain_head;
2596 rh->authority_chain_head->zone = zone;
2599 /* Start delegation resolution in our namestore */
2600 resolve_delegation_ns(rh);
2603 /*********** END NAME SHORTEN ********************/
2607 * Process result from namestore delegation lookup
2608 * for get authority operation
2610 * @param cls the client get auth handle
2611 * @param rh the resolver handle
2612 * @param rd_count number of results (0)
2613 * @param rd data (NULL)
2616 handle_delegation_result_ns_get_auth(void* cls,
2617 struct ResolverHandle *rh,
2619 const struct GNUNET_NAMESTORE_RecordData *rd)
2621 struct GetNameAuthorityHandle* nah;
2622 char result[MAX_DNS_NAME_LENGTH];
2625 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
2628 * At this point rh->name contains the part of the name
2629 * that we do not have a PKEY in our namestore to resolve.
2630 * The authority chain in the resolver handle is now
2631 * useful to backtrack if needed
2634 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2635 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2637 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2638 "Building response!\n");
2639 if (is_canonical(rh->name))
2642 * We successfully resolved the authority in the ns
2643 * FIXME for our purposes this is fine
2644 * but maybe we want to have an api that also looks
2645 * into the dht (i.e. option in message)
2647 if (strlen(rh->name) > strlen(nah->name))
2649 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2650 "Record name longer than original lookup name... odd!\n");
2654 answer_len = strlen(nah->name) - strlen(rh->name)
2655 + strlen(GNUNET_GNS_TLD) + 1;
2656 memset(result, 0, answer_len);
2657 strcpy(result, nah->name + strlen(rh->name) + 1);
2659 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2660 "Got authority result %s\n", result);
2662 nah->proc(nah->proc_cls, result);
2664 free_resolver_handle(rh);
2668 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2669 "Unable to resolve authority for remaining %s!\n", rh->name);
2670 nah->proc(nah->proc_cls, "");
2672 free_resolver_handle(rh);
2680 * Tries to resolve the authority for name
2683 * @param zone the root zone to look up for
2684 * @param name the name to lookup up
2685 * @param proc the processor to call when finished
2686 * @param proc_cls the closure to pass to the processor
2689 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
2691 GetAuthorityResultProcessor proc,
2694 struct ResolverHandle *rh;
2695 struct GetNameAuthorityHandle *nah;
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2698 "Starting authority resolution for %s!\n", name);
2700 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
2701 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2702 rh->authority = zone;
2705 if (strcmp(GNUNET_GNS_TLD, name) == 0)
2707 strcpy(rh->name, "\0");
2712 strlen(name)-strlen(GNUNET_GNS_TLD));
2713 memcpy(rh->name, name,
2714 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2717 memset(nah->name, 0,
2719 strcpy(nah->name, name);
2721 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2722 rh->authority_chain_tail = rh->authority_chain_head;
2723 rh->authority_chain_head->zone = zone;
2724 rh->proc = &handle_delegation_result_ns_get_auth;
2725 rh->proc_cls = (void*)nah;
2728 nah->proc_cls = proc_cls;
2730 /* Start delegation resolution in our namestore */
2731 resolve_delegation_ns(rh);
2735 /******** END GET AUTHORITY *************/
2737 /* end of gnunet-service-gns_resolver.c */