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,
637 rh->private_local_zone,
641 GNUNET_TIME_UNIT_FOREVER_REL,
642 &background_lookup_result_processor,
644 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
646 GNUNET_DHT_get_stop (rh->get_handle);
647 rh->get_handle = NULL;
648 rh->proc(rh->proc_cls, rh, 0, NULL);
653 * Function called when we get a result from the dht
654 * for our record query
656 * @param cls the request handle
657 * @param exp lifetime
658 * @param key the key the record was stored under
659 * @param get_path get path
660 * @param get_path_length get path length
661 * @param put_path put path
662 * @param put_path_length put path length
663 * @param type the block type
664 * @param size the size of the record
665 * @param data the record data
668 process_record_result_dht(void* cls,
669 struct GNUNET_TIME_Absolute exp,
670 const GNUNET_HashCode * key,
671 const struct GNUNET_PeerIdentity *get_path,
672 unsigned int get_path_length,
673 const struct GNUNET_PeerIdentity *put_path,
674 unsigned int put_path_length,
675 enum GNUNET_BLOCK_Type type,
676 size_t size, const void *data)
678 struct ResolverHandle *rh;
679 struct RecordLookupHandle *rlh;
680 struct GNSNameRecordBlock *nrb;
681 uint32_t num_records;
683 char* rd_data = (char*)data;
687 rh = (struct ResolverHandle *)cls;
688 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
689 "GNS_PHASE_REC-%d: got dht result (size=%d)\n", rh->id, size);
694 //FIXME maybe check expiration here, check block type
697 rlh = (struct RecordLookupHandle *) rh->proc_cls;
698 nrb = (struct GNSNameRecordBlock*)data;
700 /* stop lookup and timeout task */
701 GNUNET_DHT_get_stop (rh->get_handle);
702 rh->get_handle = NULL;
704 if (rh->dht_heap_node != NULL)
706 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
707 rh->dht_heap_node = NULL;
710 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
712 GNUNET_SCHEDULER_cancel(rh->timeout_task);
713 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
716 rh->get_handle = NULL;
717 name = (char*)&nrb[1];
718 num_records = ntohl(nrb->rd_count);
720 struct GNUNET_NAMESTORE_RecordData rd[num_records];
722 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
723 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
725 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
730 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
731 "GNS_PHASE_REC-%d: Error deserializing data!\n", rh->id);
735 for (i=0; i<num_records; i++)
737 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
738 "GNS_PHASE_REC-%d: Got name: %s (wanted %s)\n",
739 rh->id, name, rh->name);
740 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
741 "GNS_PHASE_REC-%d: Got type: %d\n",
742 rh->id, rd[i].record_type);
743 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
744 "GNS_PHASE_REC-%d: Got data length: %d\n",
745 rh->id, rd[i].data_size);
746 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
747 "GNS_PHASE_REC-%d: Got flag %d\n",
748 rh->id, rd[i].flags);
750 if ((strcmp(name, rh->name) == 0) &&
751 (rd[i].record_type == rlh->record_type))
759 * FIXME check pubkey against existing key in namestore?
760 * https://gnunet.org/bugs/view.php?id=2179
763 /* Save to namestore */
764 GNUNET_NAMESTORE_record_put (namestore_handle,
771 &on_namestore_record_put_result, //cont
776 rh->proc(rh->proc_cls, rh, num_records, rd);
778 rh->proc(rh->proc_cls, rh, 0, NULL);
785 * Start DHT lookup for a (name -> query->record_type) record in
786 * rh->authority's zone
788 * @param rh the pending gns query context
791 resolve_record_dht(struct ResolverHandle *rh)
794 struct GNUNET_CRYPTO_ShortHashCode name_hash;
795 GNUNET_HashCode lookup_key;
796 GNUNET_HashCode name_hash_double;
797 GNUNET_HashCode zone_hash_double;
798 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
799 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
800 struct ResolverHandle *rh_heap_root;
802 GNUNET_CRYPTO_short_hash(rh->name, strlen(rh->name), &name_hash);
803 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
804 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
805 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
806 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
808 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
809 "GNS_PHASE_REC-%d: starting dht lookup for %s with key: %s\n",
810 rh->id, rh->name, (char*)&lookup_key_string);
812 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
813 rh->dht_heap_node = NULL;
815 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
818 * Update timeout if necessary
820 if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
823 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
824 "GNS_PHASE_REC-%d: Adjusting timeout\n", rh->id);
826 * Set timeout for authority lookup phase to 1/2
828 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
829 GNUNET_TIME_relative_divide(rh->timeout, 2),
830 &handle_lookup_timeout,
833 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
834 // &dht_lookup_timeout,
836 rh->timeout_cont = &dht_lookup_timeout;
837 rh->timeout_cont_cls = rh;
841 if (max_allowed_background_queries <=
842 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
844 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
845 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
846 rh_heap_root->get_handle = NULL;
847 rh_heap_root->dht_heap_node = NULL;
849 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
850 "GNS_PHASE_REC-%d: Replacing oldest background query for %s\n",
851 rh->id, rh_heap_root->name);
852 rh_heap_root->proc(rh_heap_root->proc_cls,
857 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
859 GNUNET_TIME_absolute_get().abs_value);
862 xquery = htonl(rlh->record_type);
864 GNUNET_assert(rh->get_handle == NULL);
865 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
866 GNUNET_TIME_UNIT_FOREVER_REL,
867 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
869 DHT_GNS_REPLICATION_LEVEL,
873 &process_record_result_dht,
880 * Namestore calls this function if we have record for this name.
881 * (or with rd_count=0 to indicate no matches)
883 * @param cls the pending query
884 * @param key the key of the zone we did the lookup
885 * @param expiration expiration date of the namestore entry
886 * @param name the name for which we need an authority
887 * @param rd_count the number of records with 'name'
888 * @param rd the record data
889 * @param signature the signature of the authority for the record data
892 process_record_result_ns(void* cls,
893 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
894 struct GNUNET_TIME_Absolute expiration,
895 const char *name, unsigned int rd_count,
896 const struct GNUNET_NAMESTORE_RecordData *rd,
897 const struct GNUNET_CRYPTO_RsaSignature *signature)
899 struct ResolverHandle *rh;
900 struct RecordLookupHandle *rlh;
901 struct GNUNET_TIME_Relative remaining_time;
902 struct GNUNET_CRYPTO_ShortHashCode zone;
904 rh = (struct ResolverHandle *) cls;
905 rlh = (struct RecordLookupHandle *)rh->proc_cls;
906 GNUNET_CRYPTO_short_hash(key,
907 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
909 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
917 rh->status |= RSL_RECORD_EXISTS;
920 if (remaining_time.rel_value == 0)
922 rh->status |= RSL_RECORD_EXPIRED;
928 * Lookup terminated and no results
930 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
931 "GNS_PHASE_REC-%d: Namestore lookup for %s terminated without results\n",
934 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
935 "GNS_PHASE_REC-%d: Record %s unknown in namestore\n",
938 * Our zone and no result? Cannot resolve TT
940 rh->proc(rh->proc_cls, rh, 0, NULL);
947 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
948 "GNS_PHASE_REC-%d: Processing additional result %s from namestore\n",
951 for (i=0; i<rd_count;i++)
954 if (rd[i].record_type != rlh->record_type)
957 if (ignore_pending_records &&
958 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
960 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
961 "GNS_PHASE_REC-%d: Record %s is awaiting user confirmation. Skipping\n",
966 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
969 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
970 "GNS_PHASE_REC-%d: This record is expired. Skipping\n",
982 if (rh->answered == 0)
984 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
985 "GNS_PHASE_REC-%d: No answers found. This is odd!\n", rh->id);
986 rh->proc(rh->proc_cls, rh, 0, NULL);
990 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
991 "GNS_PHASE_REC-%d: Found %d answer(s) to query in %d records!\n",
992 rh->id, rh->answered, rd_count);
994 rh->proc(rh->proc_cls, rh, rd_count, rd);
1000 * The final phase of resolution.
1001 * rh->name is a name that is canonical and we do not have a delegation.
1002 * Query namestore for this record
1004 * @param rh the pending lookup
1007 resolve_record_ns(struct ResolverHandle *rh)
1009 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1011 /* We cancel here as to not include the ns lookup in the timeout */
1012 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1014 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1015 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1019 * Try to resolve this record in our namestore.
1020 * The name to resolve is now in rh->authority_name
1021 * since we tried to resolve it to an authority
1024 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1028 &process_record_result_ns,
1035 * Handle timeout for DHT requests
1037 * @param cls the request handle as closure
1038 * @param tc the task context
1041 dht_authority_lookup_timeout(void *cls,
1042 const struct GNUNET_SCHEDULER_TaskContext *tc)
1044 struct ResolverHandle *rh = cls;
1045 struct RecordLookupHandle *rlh = rh->proc_cls;
1046 char new_name[MAX_DNS_NAME_LENGTH];
1048 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1049 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%ds)timed out.\n",
1050 rh->id, rh->authority_name, rh->timeout.rel_value);
1052 rh->status |= RSL_TIMED_OUT;
1054 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1056 GNUNET_DHT_get_stop (rh->get_handle);
1057 rh->get_handle = NULL;
1059 if (strcmp(rh->name, "") == 0)
1062 * promote authority back to name and try to resolve record
1064 strcpy(rh->name, rh->authority_name);
1065 rh->proc(rh->proc_cls, rh, 0, NULL);
1070 * Start resolution in bg
1072 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
1073 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
1074 //strcpy(new_name, rh->name);
1075 //strcpy(new_name+strlen(new_name), ".");
1076 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1078 strcpy(rh->name, new_name);
1080 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1081 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1082 rh->id, rh->name, rlh->record_type);
1084 gns_resolver_lookup_record(rh->authority,
1085 rh->private_local_zone,
1089 GNUNET_TIME_UNIT_FOREVER_REL,
1090 &background_lookup_result_processor,
1093 rh->proc(rh->proc_cls, rh, 0, NULL);
1097 static void resolve_delegation_dht(struct ResolverHandle *rh);
1100 static void resolve_delegation_ns(struct ResolverHandle *rh);
1104 * Namestore resolution for delegation finished. Processing result.
1106 * @param cls the closure
1107 * @param rh resolver handle
1108 * @param rd_count number of results (always 0)
1109 * @param rd record data (always NULL)
1112 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1113 unsigned int rd_count,
1114 const struct GNUNET_NAMESTORE_RecordData *rd);
1118 * Function called when we get a result from the dht
1119 * for our query. Recursively tries to resolve authorities
1122 * @param cls the request handle
1123 * @param exp lifetime
1124 * @param key the key the record was stored under
1125 * @param get_path get path
1126 * @param get_path_length get path length
1127 * @param put_path put path
1128 * @param put_path_length put path length
1129 * @param type the block type
1130 * @param size the size of the record
1131 * @param data the record data
1134 process_delegation_result_dht(void* cls,
1135 struct GNUNET_TIME_Absolute exp,
1136 const GNUNET_HashCode * key,
1137 const struct GNUNET_PeerIdentity *get_path,
1138 unsigned int get_path_length,
1139 const struct GNUNET_PeerIdentity *put_path,
1140 unsigned int put_path_length,
1141 enum GNUNET_BLOCK_Type type,
1142 size_t size, const void *data)
1144 struct ResolverHandle *rh;
1145 struct GNSNameRecordBlock *nrb;
1146 uint32_t num_records;
1148 char* rd_data = (char*) data;
1151 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
1152 GNUNET_HashCode zone_hash_double, name_hash_double;
1154 rh = (struct ResolverHandle *)cls;
1156 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1157 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
1162 nrb = (struct GNSNameRecordBlock*)data;
1164 /* stop dht lookup and timeout task */
1165 GNUNET_DHT_get_stop (rh->get_handle);
1167 rh->get_handle = NULL;
1169 if (rh->dht_heap_node != NULL)
1171 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
1172 rh->dht_heap_node = NULL;
1175 num_records = ntohl(nrb->rd_count);
1176 name = (char*)&nrb[1];
1178 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1180 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1181 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
1183 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1188 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1189 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
1194 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1195 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
1196 rh->id, name, rh->authority_name);
1197 for (i=0; i<num_records; i++)
1200 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1201 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
1202 rh->id, name, rh->authority_name);
1203 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1204 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
1205 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
1206 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1207 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
1208 rh->id, rd[i].data_size);
1209 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1210 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
1211 rh->id, rd[i].flags);
1213 if ((strcmp(name, rh->authority_name) == 0) &&
1214 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
1216 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1217 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
1220 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
1221 struct AuthorityChain *auth =
1222 GNUNET_malloc(sizeof(struct AuthorityChain));
1223 auth->zone = rh->authority;
1224 memset(auth->name, 0, strlen(rh->authority_name)+1);
1225 strcpy(auth->name, rh->authority_name);
1226 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1227 rh->authority_chain_tail,
1230 /** try to import pkey if private key available */
1232 process_discovered_authority(name, auth->zone,
1233 rh->authority_chain_tail->zone,
1240 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
1241 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1242 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
1243 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
1245 /* Save to namestore */
1246 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
1249 GNUNET_NAMESTORE_record_put (namestore_handle,
1256 &on_namestore_record_put_result, //cont
1266 * FIXME in this case. should we ask namestore again?
1268 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1269 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
1270 rh->id, rh->authority_name, rh->name);
1271 if (strcmp(rh->name, "") == 0)
1273 rh->proc(rh->proc_cls, rh, 0, NULL);
1277 rh->proc = &handle_delegation_ns;
1278 resolve_delegation_ns(rh);
1284 * No pkey but name exists
1287 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1288 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
1289 rh->id, rh->authority_name, rh->name);
1290 if (strcmp(rh->name, "") == 0)
1291 strcpy(rh->name, rh->authority_name);
1293 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
1294 rh->name, rh->authority_name); //FIXME ret
1296 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1297 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
1298 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1299 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
1301 rh->proc(rh->proc_cls, rh, 0, NULL);
1304 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
1305 +(MAX_DNS_NAME_LENGTH*2)
1306 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
1310 expand_plus(char** dest, char* src, char* repl)
1313 unsigned int s_len = strlen(src)+1;
1315 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1316 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
1320 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1321 "GNS_POSTPROCESS: %s to short\n", src);
1323 /* no postprocessing */
1324 memcpy(*dest, src, s_len+1);
1328 if (0 == strcmp(src+s_len-3, ".+"))
1330 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1331 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
1332 memset(*dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
1334 pos = *dest+s_len-2;
1336 pos += strlen(repl);
1337 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1338 "GNS_POSTPROCESS: Expanded to %s\n", *dest);
1342 memcpy(*dest, src, s_len+1);
1350 finish_lookup(struct ResolverHandle *rh,
1351 struct RecordLookupHandle* rlh,
1352 unsigned int rd_count,
1353 const struct GNUNET_NAMESTORE_RecordData *rd)
1356 char new_rr_data[MAX_DNS_NAME_LENGTH];
1357 char new_mx_data[MAX_MX_LENGTH];
1358 char new_soa_data[MAX_SOA_LENGTH];
1359 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
1362 unsigned int offset;
1364 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1366 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1367 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1371 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
1373 for (i = 0; i < rd_count; i++)
1376 if (rd[i].record_type != GNUNET_GNS_RECORD_TYPE_NS &&
1377 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_CNAME &&
1378 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
1379 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_SOA)
1381 p_rd[i].data = rd[i].data;
1386 * for all those records we 'should'
1387 * also try to resolve the A/AAAA records (RFC1035)
1388 * This is a feature and not important
1391 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1392 "GNS_POSTPROCESS: Postprocessing\n");
1394 if (strcmp(rh->name, "+") == 0)
1395 repl_string = rlh->name;
1397 repl_string = rlh->name+strlen(rh->name)+1;
1400 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
1402 memcpy(new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
1403 offset = sizeof(uint16_t);
1404 pos = new_mx_data+offset;
1405 expand_plus(&pos, (char*)rd[i].data+sizeof(uint16_t),
1407 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
1408 p_rd[i].data = new_mx_data;
1409 p_rd[i].data_size = offset;
1411 else if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_SOA)
1413 /* expand mname and rname */
1415 expand_plus(&pos, (char*)rd[i].data, repl_string);
1416 offset = strlen(new_soa_data)+1;
1417 pos = new_soa_data+offset;
1418 expand_plus(&pos, (char*)rd[i].data+offset, repl_string);
1419 offset += strlen(new_soa_data+offset)+1;
1420 /* cpy the 4 numbers serial refresh retry and expire */
1421 memcpy(new_soa_data+offset, (char*)rd[i].data+offset, sizeof(uint32_t)*5);
1422 offset += sizeof(uint32_t)*5;
1423 p_rd[i].data_size = offset;
1424 p_rd[i].data = new_soa_data;
1429 expand_plus(&pos, (char*)rd[i].data, repl_string);
1430 p_rd[i].data_size = strlen(new_rr_data)+1;
1431 p_rd[i].data = new_rr_data;
1436 rlh->proc(rlh->proc_cls, rd_count, p_rd);
1442 * Process DHT lookup result for record.
1444 * @param cls the closure
1445 * @param rh resolver handle
1446 * @param rd_count number of results
1447 * @param rd record data
1450 handle_record_dht(void* cls, struct ResolverHandle *rh,
1451 unsigned int rd_count,
1452 const struct GNUNET_NAMESTORE_RecordData *rd)
1454 struct RecordLookupHandle* rlh;
1456 rlh = (struct RecordLookupHandle*)cls;
1459 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1460 "GNS_PHASE_REC-%d: No records for %s found in DHT. Aborting\n",
1462 /* give up, cannot resolve */
1463 finish_lookup(rh, rlh, 0, NULL);
1464 free_resolver_handle(rh);
1468 /* results found yay */
1469 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1470 "GNS_PHASE_REC-%d: Record resolved from DHT!", rh->id);
1472 finish_lookup(rh, rlh, rd_count, rd);
1473 free_resolver_handle(rh);
1479 * Process namestore lookup result for record.
1481 * @param cls the closure
1482 * @param rh resolver handle
1483 * @param rd_count number of results
1484 * @param rd record data
1487 handle_record_ns(void* cls, struct ResolverHandle *rh,
1488 unsigned int rd_count,
1489 const struct GNUNET_NAMESTORE_RecordData *rd)
1491 struct RecordLookupHandle* rlh;
1492 rlh = (struct RecordLookupHandle*) cls;
1495 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1496 "GNS_PHASE_REC-%d: NS returned no records. (status: %d)!\n",
1501 * There are 4 conditions that have to met for us to consult the DHT:
1502 * 1. The entry in the DHT is RSL_RECORD_EXPIRED AND
1503 * 2. No entry in the NS existed AND
1504 * 3. The zone queried is not the local resolver's zone AND
1505 * 4. The name that was looked up is '+'
1506 * because if it was any other canonical name we either already queried
1507 * the DHT for the authority in the authority lookup phase (and thus
1508 * would already have an entry in the NS for the record)
1510 if (rh->status & (RSL_RECORD_EXPIRED | !RSL_RECORD_EXISTS) &&
1511 GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1512 &rh->private_local_zone) &&
1513 (strcmp(rh->name, "+") == 0))
1515 rh->proc = &handle_record_dht;
1516 resolve_record_dht(rh);
1519 /* give up, cannot resolve */
1520 finish_lookup(rh, rlh, 0, NULL);
1521 free_resolver_handle(rh);
1525 /* results found yay */
1526 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1527 "GNS_PHASE_REC-%d: Record resolved from namestore!", rh->id);
1529 finish_lookup(rh, rlh, rd_count, rd);
1531 free_resolver_handle(rh);
1537 * Determine if this name is canonical.
1539 * a.b.gnunet = not canonical
1542 * @param name the name to test
1543 * @return 1 if canonical
1546 is_canonical(char* name)
1548 uint32_t len = strlen(name);
1551 for (i=0; i<len; i++)
1553 if (*(name+i) == '.')
1560 * Move one level up in the domain hierarchy and return the
1561 * passed top level domain.
1563 * @param name the domain
1564 * @param dest the destination where the tld will be put
1567 pop_tld(char* name, char* dest)
1571 if (is_canonical(name))
1578 for (len = strlen(name); len > 0; len--)
1580 if (*(name+len) == '.')
1590 strcpy(dest, (name+len+1));
1594 * Checks if name is in tld
1596 * @param name the name to check
1597 * @param tld the TLD to check for
1598 * @return GNUNET_YES or GNUNET_NO
1601 is_tld(const char* name, const char* tld)
1605 if (strlen(name) <= strlen(tld))
1610 offset = strlen(name)-strlen(tld);
1611 if (strcmp(name+offset, tld) != 0)
1613 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1614 "%s is not in .%s TLD\n", name, tld);
1621 * DHT resolution for delegation finished. Processing result.
1623 * @param cls the closure
1624 * @param rh resolver handle
1625 * @param rd_count number of results (always 0)
1626 * @param rd record data (always NULL)
1629 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
1630 unsigned int rd_count,
1631 const struct GNUNET_NAMESTORE_RecordData *rd)
1633 struct RecordLookupHandle* rlh;
1634 rlh = (struct RecordLookupHandle*) cls;
1637 if (strcmp(rh->name, "") == 0)
1639 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
1641 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1642 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
1644 finish_lookup(rh, rlh, rd_count, rd);
1645 free_resolver_handle(rh);
1648 /* We resolved full name for delegation. resolving record */
1649 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1650 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
1652 strcpy(rh->name, "+\0");
1653 rh->proc = &handle_record_ns;
1654 resolve_record_ns(rh);
1659 * we still have some left
1661 if (is_canonical(rh->name))
1663 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1664 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
1667 rh->proc = &handle_record_ns;
1668 resolve_record_ns(rh);
1671 /* give up, cannot resolve */
1672 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1673 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
1675 finish_lookup(rh, rlh, 0, NULL);
1676 free_resolver_handle(rh);
1681 * Start DHT lookup for a name -> PKEY (compare NS) record in
1682 * rh->authority's zone
1684 * @param rh the pending gns query
1687 resolve_delegation_dht(struct ResolverHandle *rh)
1690 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1691 GNUNET_HashCode name_hash_double;
1692 GNUNET_HashCode zone_hash_double;
1693 GNUNET_HashCode lookup_key;
1694 struct ResolverHandle *rh_heap_root;
1696 pop_tld(rh->name, rh->authority_name);
1697 GNUNET_CRYPTO_short_hash(rh->authority_name,
1698 strlen(rh->authority_name),
1700 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1701 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
1702 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
1704 rh->dht_heap_node = NULL;
1706 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1708 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1709 // &dht_authority_lookup_timeout,
1711 rh->timeout_cont = &dht_authority_lookup_timeout;
1712 rh->timeout_cont_cls = rh;
1716 if (max_allowed_background_queries <=
1717 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1719 /* terminate oldest lookup */
1720 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1721 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
1722 rh_heap_root->dht_heap_node = NULL;
1724 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1725 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
1726 rh->id, rh_heap_root->authority_name);
1728 rh_heap_root->proc(rh_heap_root->proc_cls,
1733 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1735 GNUNET_TIME_absolute_get().abs_value);
1738 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1740 GNUNET_assert(rh->get_handle == NULL);
1741 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1742 GNUNET_TIME_UNIT_FOREVER_REL,
1743 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1745 DHT_GNS_REPLICATION_LEVEL,
1749 &process_delegation_result_dht,
1756 * Namestore resolution for delegation finished. Processing result.
1758 * @param cls the closure
1759 * @param rh resolver handle
1760 * @param rd_count number of results (always 0)
1761 * @param rd record data (always NULL)
1764 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1765 unsigned int rd_count,
1766 const struct GNUNET_NAMESTORE_RecordData *rd)
1768 struct RecordLookupHandle* rlh;
1769 rlh = (struct RecordLookupHandle*) cls;
1771 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1772 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
1773 rh->id, rh->status);
1775 if (strcmp(rh->name, "") == 0)
1777 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
1779 GNUNET_assert(rd_count == 1);
1780 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1781 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
1783 finish_lookup(rh, rlh, rd_count, rd);
1784 free_resolver_handle(rh);
1787 /* We resolved full name for delegation. resolving record */
1788 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1789 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
1791 strcpy(rh->name, "+\0");
1792 rh->proc = &handle_record_ns;
1793 resolve_record_ns(rh);
1798 * we still have some left
1799 * check if authority in ns is fresh
1801 * or we are authority
1803 if (((rh->status & RSL_RECORD_EXISTS) && (!(rh->status & RSL_RECORD_EXPIRED)))
1804 || !GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1805 &rh->private_local_zone))
1807 if (is_canonical(rh->name))
1809 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1810 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
1813 rh->proc = &handle_record_ns;
1814 resolve_record_ns(rh);
1818 /* give up, cannot resolve */
1819 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1820 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
1823 finish_lookup(rh, rlh, rd_count, rd);
1824 //rlh->proc(rlh->proc_cls, 0, NULL);
1829 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1830 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
1832 rh->proc = &handle_delegation_dht;
1833 resolve_delegation_dht(rh);
1839 * This is a callback function that should give us only PKEY
1840 * records. Used to query the namestore for the authority (PKEY)
1841 * for 'name'. It will recursively try to resolve the
1842 * authority for a given name from the namestore.
1844 * @param cls the pending query
1845 * @param key the key of the zone we did the lookup
1846 * @param expiration expiration date of the record data set in the namestore
1847 * @param name the name for which we need an authority
1848 * @param rd_count the number of records with 'name'
1849 * @param rd the record data
1850 * @param signature the signature of the authority for the record data
1853 process_delegation_result_ns(void* cls,
1854 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1855 struct GNUNET_TIME_Absolute expiration,
1857 unsigned int rd_count,
1858 const struct GNUNET_NAMESTORE_RecordData *rd,
1859 const struct GNUNET_CRYPTO_RsaSignature *signature)
1861 struct ResolverHandle *rh;
1862 struct GNUNET_TIME_Relative remaining_time;
1863 struct GNUNET_CRYPTO_ShortHashCode zone;
1864 char new_name[MAX_DNS_NAME_LENGTH];
1866 rh = (struct ResolverHandle *)cls;
1867 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1868 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
1871 GNUNET_CRYPTO_short_hash(key,
1872 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1874 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1880 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1881 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
1883 rh->status |= RSL_RECORD_EXISTS;
1886 if (remaining_time.rel_value == 0)
1888 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1889 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
1891 rh->status |= RSL_RECORD_EXPIRED;
1895 * No authority found in namestore.
1900 * We did not find an authority in the namestore
1905 * Promote this authority back to a name maybe it is
1908 if (strcmp(rh->name, "") == 0)
1910 /* simply promote back */
1911 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1912 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
1913 rh->id, rh->authority_name);
1914 strcpy(rh->name, rh->authority_name);
1918 /* add back to existing name */
1919 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1920 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
1921 rh->id, rh->authority_name, rh->name);
1922 //memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1923 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
1924 rh->name, rh->authority_name);
1925 //strcpy(new_name, rh->name);
1926 //strcpy(new_name+strlen(new_name), ".");
1927 //strcpy(new_name+strlen(new_name), rh->authority_name);
1928 strcpy(rh->name, new_name);
1929 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1930 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n", rh->id, rh->name);
1932 rh->proc(rh->proc_cls, rh, 0, NULL);
1937 * We found an authority that may be able to help us
1938 * move on with query
1939 * Note only 1 pkey should have been returned.. anything else would be strange
1942 for (i=0; i<rd_count;i++)
1945 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1948 if (ignore_pending_records &&
1949 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1951 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1952 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
1958 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1961 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1962 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
1964 if (remaining_time.rel_value == 0)
1966 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1967 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
1969 rh->authority_chain_head->fresh = 0;
1970 rh->proc(rh->proc_cls, rh, 0, NULL);
1978 * Resolve rest of query with new authority
1980 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1981 memcpy(&rh->authority, rd[i].data,
1982 sizeof(struct GNUNET_CRYPTO_ShortHashCode));
1983 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1984 auth->zone = rh->authority;
1985 memset(auth->name, 0, strlen(rh->authority_name)+1);
1986 strcpy(auth->name, rh->authority_name);
1987 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1988 rh->authority_chain_tail,
1991 /** try to import pkey if private key available
1992 * TODO: Only import last one?
1994 if (rh->priv_key && (name != NULL))
1995 process_discovered_authority((char*)name, auth->zone,
1996 rh->authority_chain_tail->zone,
1999 * We are done with PKEY resolution if name is empty
2000 * else resolve again with new authority
2002 if (strcmp(rh->name, "") == 0)
2003 rh->proc(rh->proc_cls, rh, rd_count, rd);
2005 resolve_delegation_ns(rh);
2012 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2013 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
2015 * If we have found some records for the LAST label
2016 * we return the results. Else null.
2018 if (strcmp(rh->name, "") == 0)
2020 /* simply promote back */
2021 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2022 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
2023 rh->id, rh->authority_name);
2024 strcpy(rh->name, rh->authority_name);
2025 rh->proc(rh->proc_cls, rh, rd_count, rd);
2029 rh->proc(rh->proc_cls, rh, 0, NULL);
2035 * Resolve the delegation chain for the request in our namestore
2037 * @param rh the resolver handle
2040 resolve_delegation_ns(struct ResolverHandle *rh)
2042 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2043 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
2045 pop_tld(rh->name, rh->authority_name);
2046 GNUNET_NAMESTORE_lookup_record(namestore_handle,
2049 GNUNET_GNS_RECORD_ANY,
2050 &process_delegation_result_ns,
2057 * Lookup of a record in a specific zone
2058 * calls lookup result processor on result
2060 * @param zone the root zone
2061 * @param pzone the private local zone
2062 * @param record_type the record type to look up
2063 * @param name the name to look up
2064 * @param key a private key for use with PSEU import (can be NULL)
2065 * @param timeout timeout for resolution
2066 * @param proc the processor to call on result
2067 * @param cls the closure to pass to proc
2070 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
2071 struct GNUNET_CRYPTO_ShortHashCode pzone,
2072 uint32_t record_type,
2074 struct GNUNET_CRYPTO_RsaPrivateKey *key,
2075 struct GNUNET_TIME_Relative timeout,
2076 RecordLookupProcessor proc,
2079 struct ResolverHandle *rh;
2080 struct RecordLookupHandle* rlh;
2081 char string_hash[MAX_DNS_LABEL_LENGTH];
2082 char nzkey[MAX_DNS_LABEL_LENGTH];
2083 char* nzkey_ptr = nzkey;
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2086 "Starting resolution for %s (type=%d)!\n",
2090 if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2093 "%s is canonical and not gnunet -> cannot resolve!\n", name);
2098 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
2099 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2101 rh->authority = zone;
2105 rh->timeout = timeout;
2106 rh->get_handle = NULL;
2107 rh->private_local_zone = pzone;
2109 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2112 * Set timeout for authority lookup phase to 1/2
2114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2115 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
2116 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
2117 GNUNET_TIME_relative_divide(timeout, 2),
2118 &handle_lookup_timeout,
2120 rh->timeout_cont = &dht_authority_lookup_timeout;
2121 rh->timeout_cont_cls = rh;
2125 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
2126 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2129 if (strcmp(GNUNET_GNS_TLD, name) == 0)
2132 * Only 'gnunet' given
2134 strcpy(rh->name, "\0");
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2139 "Checking for TLD...\n");
2140 if (is_zkey_tld(name) == GNUNET_YES)
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2145 * This is a zkey tld
2146 * build hash and use as initial authority
2149 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
2150 memcpy(rh->name, name,
2151 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
2152 pop_tld(rh->name, string_hash);
2154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2155 "ZKEY is %s!\n", string_hash);
2157 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
2159 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
2162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2163 "Cannot convert ZKEY %s to hash!\n", string_hash);
2173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2176 * Presumably GNUNET tld
2179 strlen(name)-strlen(GNUNET_GNS_TLD));
2180 memcpy(rh->name, name,
2181 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2186 * Initialize authority chain
2188 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2189 rh->authority_chain_head->prev = NULL;
2190 rh->authority_chain_head->next = NULL;
2191 rh->authority_chain_tail = rh->authority_chain_head;
2192 rh->authority_chain_head->zone = rh->authority;
2195 * Copy original query into lookup handle
2197 rlh->record_type = record_type;
2198 memset(rlh->name, 0, strlen(name) + 1);
2199 strcpy(rlh->name, name);
2201 rlh->proc_cls = cls;
2203 rh->proc = &handle_delegation_ns;
2204 resolve_delegation_ns(rh);
2207 /******** END Record Resolver ***********/
2211 * Callback calles by namestore for a zone to name
2214 * @param cls the closure
2215 * @param zone_key the zone we queried
2216 * @param expire the expiration time of the name
2217 * @param name the name found or NULL
2218 * @param rd_len number of records for the name
2219 * @param rd the record data (PKEY) for the name
2220 * @param signature the signature for the record data
2223 process_zone_to_name_shorten(void *cls,
2224 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
2225 struct GNUNET_TIME_Absolute expire,
2227 unsigned int rd_len,
2228 const struct GNUNET_NAMESTORE_RecordData *rd,
2229 const struct GNUNET_CRYPTO_RsaSignature *signature)
2231 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
2232 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
2233 struct AuthorityChain *next_authority;
2235 char result[MAX_DNS_NAME_LENGTH];
2236 char tmp_name[MAX_DNS_NAME_LENGTH];
2239 /* we found a match in our own zone */
2242 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2243 "result strlen %d\n", strlen(name));
2244 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
2245 memset(result, 0, answer_len);
2246 if (strlen(rh->name) > 0)
2248 strcpy(result, rh->name);
2249 strcpy(result+strlen(rh->name), ".");
2252 strcpy(result+strlen(result), name);
2253 strcpy(result+strlen(result), ".");
2254 strcpy(result+strlen(result), GNUNET_GNS_TLD);
2256 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2257 "Sending shorten result %s\n", result);
2259 nsh->proc(nsh->proc_cls, result);
2261 free_resolver_handle(rh);
2263 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2264 &rh->private_local_zone) == 0)
2266 /* our zone, just append .gnunet */
2267 answer_len = strlen(rh->name) + strlen(GNUNET_GNS_TLD) + 2;
2268 memset(result, 0, answer_len);
2269 strcpy(result, rh->name);
2270 strcpy(result+strlen(rh->name), ".");
2271 strcpy(result+strlen(rh->name)+1, GNUNET_GNS_TLD);
2273 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2274 "Our zone: Sending name as shorten result %s\n", rh->name);
2276 nsh->proc(nsh->proc_cls, result);
2278 free_resolver_handle(rh);
2284 * continue with next authority
2286 next_authority = rh->authority_chain_head;
2288 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
2289 "%s.%s", rh->name, next_authority->name);
2291 strcpy(rh->name, tmp_name);
2292 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2293 "No PSEU found for authority %s. Promoting back: %s\n",
2294 next_authority->name, rh->name);
2296 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
2297 rh->authority_chain_tail,
2300 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2301 &rh->authority_chain_tail->zone,
2302 &rh->authority_chain_head->zone,
2303 &process_zone_to_name_shorten,
2309 * DHT resolution for delegation. Processing result.
2311 * @param cls the closure
2312 * @param rh resolver handle
2313 * @param rd_count number of results
2314 * @param rd record data
2317 handle_delegation_dht_bg_shorten(void* cls, struct ResolverHandle *rh,
2318 unsigned int rd_count,
2319 const struct GNUNET_NAMESTORE_RecordData *rd)
2322 /* We resolved full name for delegation. resolving record */
2323 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2324 "GNS_SHORTEN: Resolved up to %s for delegation via DHT in background.\n",
2326 free_resolver_handle(rh);
2330 * Process result from namestore delegation lookup
2331 * for shorten operation
2333 * @param cls the client shorten handle
2334 * @param rh the resolver handle
2335 * @param rd_count number of results (0)
2336 * @param rd data (NULL)
2339 handle_delegation_ns_shorten(void* cls,
2340 struct ResolverHandle *rh,
2342 const struct GNUNET_NAMESTORE_RecordData *rd)
2344 struct NameShortenHandle *nsh;
2345 char result[MAX_DNS_NAME_LENGTH];
2347 struct ResolverHandle *rh_bg;
2349 nsh = (struct NameShortenHandle *)cls;
2352 * At this point rh->name contains the part of the name
2353 * that we do not have a PKEY in our namestore to resolve.
2354 * The authority chain in the resolver handle is now
2355 * useful to backtrack if needed
2358 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2359 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2361 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2362 &rh->private_local_zone) == 0)
2365 * This is our zone append .gnunet unless name is empty
2366 * (it shouldn't be, usually FIXME what happens if we
2367 * shorten to our zone to a "" record??)
2370 answer_len = strlen(rh->name) + strlen(GNUNET_GNS_TLD) + 2;
2371 memset(result, 0, answer_len);
2372 strcpy(result, rh->name);
2373 strcpy(result+strlen(rh->name), ".");
2374 strcpy(result+strlen(rh->name)+1, GNUNET_GNS_TLD);
2376 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2377 "Our zone: Sending name as shorten result %s\n", rh->name);
2379 nsh->proc(nsh->proc_cls, result);
2381 free_resolver_handle(rh);
2386 * we have to this before zone to name for rh might
2390 if (!is_canonical(rh->name))
2392 rh_bg = GNUNET_malloc(sizeof(struct ResolverHandle));
2393 memcpy(rh_bg, rh, sizeof(struct ResolverHandle));
2397 /* backtrack authorities for names */
2398 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2399 &rh->authority_chain_tail->zone, //ours
2400 &rh->authority_chain_head->zone,
2401 &process_zone_to_name_shorten,
2410 * If authority resolution is incomplete we can do a background lookup
2411 * of the full name so that next time we can (likely) fully or at least
2412 * further shorten the name
2414 rh_bg->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2415 rh_bg->authority_chain_tail = rh_bg->authority_chain_head;
2416 rh_bg->authority_chain_head->zone = rh_bg->authority;
2418 rh_bg->proc = &handle_delegation_dht_bg_shorten;
2419 rh_bg->proc_cls = NULL;
2421 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2422 "GNS_SHORTEN: Starting background lookup for %s\n",
2425 resolve_delegation_dht(rh_bg);
2431 * Callback calles by namestore for a zone to name
2434 * @param cls the closure
2435 * @param zone_key the zone we queried
2436 * @param expire the expiration time of the name
2437 * @param name the name found or NULL
2438 * @param rd_len number of records for the name
2439 * @param rd the record data (PKEY) for the name
2440 * @param signature the signature for the record data
2443 process_zone_to_name_zkey(void *cls,
2444 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
2445 struct GNUNET_TIME_Absolute expire,
2447 unsigned int rd_len,
2448 const struct GNUNET_NAMESTORE_RecordData *rd,
2449 const struct GNUNET_CRYPTO_RsaSignature *signature)
2451 struct ResolverHandle *rh = cls;
2452 struct NameShortenHandle *nsh = rh->proc_cls;
2453 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
2454 char new_name[MAX_DNS_NAME_LENGTH];
2456 /* zkey not in our zone */
2460 * In this case we have not given this PKEY a name (yet)
2461 * It is either just not in our zone or not even cached
2462 * Since we do not know at this point we will not try to shorten
2463 * because PKEY import will happen if the user follows the zkey
2466 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
2468 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2469 "No name found for zkey %s returning verbatim!\n", enc);
2470 if (strcmp(rh->name, "") != 0)
2471 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
2472 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
2474 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2475 enc, GNUNET_GNS_TLD_ZKEY);
2476 nsh->proc(nsh->proc_cls, new_name);
2478 free_resolver_handle(rh);
2482 if (strcmp(rh->name, "") != 0)
2483 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2486 strcpy(new_name, name);
2488 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2489 "Continue shorten for %s!\n", new_name);
2491 strcpy(rh->name, new_name);
2493 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2494 rh->authority_chain_tail = rh->authority_chain_head;
2495 rh->authority_chain_head->zone = rh->authority;
2498 /* Start delegation resolution in our namestore */
2499 resolve_delegation_ns(rh);
2504 * Shorten api from resolver
2506 * @param zone the zone to use
2507 * @param pzone the private local zone
2508 * @param name the name to shorten
2509 * @param key optional private key for background lookups and PSEU import
2510 * @param proc the processor to call with result
2511 * @param proc_cls closure to pass to proc
2514 gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode zone,
2515 struct GNUNET_CRYPTO_ShortHashCode pzone,
2517 struct GNUNET_CRYPTO_RsaPrivateKey *key,
2518 ShortenResultProcessor proc,
2521 struct ResolverHandle *rh;
2522 struct NameShortenHandle *nsh;
2523 char string_hash[MAX_DNS_LABEL_LENGTH];
2524 struct GNUNET_CRYPTO_ShortHashCode zkey;
2525 char nzkey[MAX_DNS_LABEL_LENGTH];
2526 char* nzkey_ptr = nzkey;
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 "Starting shorten for %s!\n", name);
2532 if (is_canonical((char*)name))
2534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2535 "%s is canonical. Returning verbatim\n", name);
2536 proc(proc_cls, name);
2540 nsh = GNUNET_malloc(sizeof (struct NameShortenHandle));
2543 nsh->proc_cls = proc_cls;
2545 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2546 rh->authority = zone;
2549 rh->proc = &handle_delegation_ns_shorten;
2552 rh->private_local_zone = pzone;
2554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2555 "Checking for TLD...\n");
2556 if (is_zkey_tld(name) == GNUNET_YES)
2558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2561 * This is a zkey tld
2562 * build hash and use as initial authority
2566 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
2567 memcpy(rh->name, name,
2568 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
2569 pop_tld(rh->name, string_hash);
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2572 "ZKEY is %s!\n", string_hash);
2574 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
2576 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
2579 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2580 "Cannot convert ZKEY %s to hash!\n", nzkey);
2583 proc(proc_cls, name);
2587 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2590 &process_zone_to_name_zkey,
2597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2600 * Presumably GNUNET tld
2603 strlen(name)-strlen(GNUNET_GNS_TLD));
2604 memcpy(rh->name, name,
2605 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2608 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2609 rh->authority_chain_tail = rh->authority_chain_head;
2610 rh->authority_chain_head->zone = zone;
2613 /* Start delegation resolution in our namestore */
2614 resolve_delegation_ns(rh);
2617 /*********** END NAME SHORTEN ********************/
2621 * Process result from namestore delegation lookup
2622 * for get authority operation
2624 * @param cls the client get auth handle
2625 * @param rh the resolver handle
2626 * @param rd_count number of results (0)
2627 * @param rd data (NULL)
2630 handle_delegation_result_ns_get_auth(void* cls,
2631 struct ResolverHandle *rh,
2633 const struct GNUNET_NAMESTORE_RecordData *rd)
2635 struct GetNameAuthorityHandle* nah;
2636 char result[MAX_DNS_NAME_LENGTH];
2639 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
2642 * At this point rh->name contains the part of the name
2643 * that we do not have a PKEY in our namestore to resolve.
2644 * The authority chain in the resolver handle is now
2645 * useful to backtrack if needed
2648 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2649 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2651 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2652 "Building response!\n");
2653 if (is_canonical(rh->name))
2656 * We successfully resolved the authority in the ns
2657 * FIXME for our purposes this is fine
2658 * but maybe we want to have an api that also looks
2659 * into the dht (i.e. option in message)
2661 if (strlen(rh->name) > strlen(nah->name))
2663 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2664 "Record name longer than original lookup name... odd!\n");
2668 answer_len = strlen(nah->name) - strlen(rh->name)
2669 + strlen(GNUNET_GNS_TLD) + 1;
2670 memset(result, 0, answer_len);
2671 strcpy(result, nah->name + strlen(rh->name) + 1);
2673 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2674 "Got authority result %s\n", result);
2676 nah->proc(nah->proc_cls, result);
2678 free_resolver_handle(rh);
2682 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2683 "Unable to resolve authority for remaining %s!\n", rh->name);
2684 nah->proc(nah->proc_cls, "");
2686 free_resolver_handle(rh);
2694 * Tries to resolve the authority for name
2697 * @param zone the root zone to look up for
2698 * @param pzone the private local zone
2699 * @param name the name to lookup up
2700 * @param proc the processor to call when finished
2701 * @param proc_cls the closure to pass to the processor
2704 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
2705 struct GNUNET_CRYPTO_ShortHashCode pzone,
2707 GetAuthorityResultProcessor proc,
2710 struct ResolverHandle *rh;
2711 struct GetNameAuthorityHandle *nah;
2713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2714 "Starting authority resolution for %s!\n", name);
2716 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
2717 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2718 rh->authority = zone;
2720 rh->private_local_zone = pzone;
2722 if (strcmp(GNUNET_GNS_TLD, name) == 0)
2724 strcpy(rh->name, "\0");
2729 strlen(name)-strlen(GNUNET_GNS_TLD));
2730 memcpy(rh->name, name,
2731 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2734 memset(nah->name, 0,
2736 strcpy(nah->name, name);
2738 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2739 rh->authority_chain_tail = rh->authority_chain_head;
2740 rh->authority_chain_head->zone = zone;
2741 rh->proc = &handle_delegation_result_ns_get_auth;
2742 rh->proc_cls = (void*)nah;
2745 nah->proc_cls = proc_cls;
2747 /* Start delegation resolution in our namestore */
2748 resolve_delegation_ns(rh);
2752 /******** END GET AUTHORITY *************/
2754 /* end of gnunet-service-gns_resolver.c */