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
30 #include "gnunet_util_lib.h"
31 #include "gnunet_transport_service.h"
32 #include "gnunet_dns_service.h"
33 #include "gnunet_dht_service.h"
34 #include "gnunet_namestore_service.h"
35 #include "gnunet_dns_service.h"
36 #include "gnunet_dnsparser_lib.h"
37 #include "gnunet_gns_service.h"
38 #include "block_gns.h"
40 #include "gnunet-service-gns_resolver.h"
42 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
43 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
44 #define DHT_GNS_REPLICATION_LEVEL 5
45 #define MAX_DNS_LABEL_LENGTH 63
49 * Our handle to the namestore service
51 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
54 * Resolver handle to the dht
56 static struct GNUNET_DHT_Handle *dht_handle;
59 * Heap for parallel DHT lookups
61 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
64 * Maximum amount of parallel queries in background
66 static unsigned long long max_allowed_background_queries;
71 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
74 * Namestore calls this function if we have record for this name.
75 * (or with rd_count=0 to indicate no matches)
77 * @param cls the pending query
78 * @param key the key of the zone we did the lookup
79 * @param expiration expiration date of the namestore entry
80 * @param name the name for which we need an authority
81 * @param rd_count the number of records with 'name'
82 * @param rd the record data
83 * @param signature the signature of the authority for the record data
86 process_pseu_lookup_ns(void* cls,
87 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
88 struct GNUNET_TIME_Absolute expiration,
89 const char *name, unsigned int rd_count,
90 const struct GNUNET_NAMESTORE_RecordData *rd,
91 const struct GNUNET_CRYPTO_RsaSignature *signature)
93 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
94 struct GNUNET_NAMESTORE_RecordData new_pkey;
98 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
99 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
100 if (0 == strcmp(gph->name, name))
102 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
103 "GNS_AUTO_PSEU: Intelligent replacement not implemented\n",
109 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
110 "GNS_AUTO_PSEU: Trying delegated name %s\n", gph->name);
111 memcpy(gph->new_name, gph->name, strlen(gph->name)+1);
112 GNUNET_NAMESTORE_lookup_record(namestore_handle,
115 GNUNET_GNS_RECORD_PSEU,
116 &process_pseu_lookup_ns,
122 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
123 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->new_name);
125 new_pkey.expiration = GNUNET_TIME_absolute_get_forever ();
126 new_pkey.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
127 new_pkey.data = &gph->new_zone;
128 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
129 GNUNET_NAMESTORE_record_create (namestore_handle,
140 * process result of a dht pseu lookup
142 * @param gph the handle
143 * @param name the pseu result or NULL
146 process_pseu_result(struct GetPseuAuthorityHandle* gph, char* name)
150 memcpy(gph->new_name, gph->name, strlen(gph->name)+1);
154 memcpy(gph->new_name, name, strlen(name)+1);
157 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
158 "GNS_AUTO_PSEU: Checking %s for collision in NS\n", gph->new_name);
161 * Check for collision
163 GNUNET_NAMESTORE_lookup_record(namestore_handle,
166 GNUNET_GNS_RECORD_PSEU,
167 &process_pseu_lookup_ns,
172 * Handle timeout for dht request
174 * @param cls the request handle as closure
175 * @param tc the task context
178 handle_auth_discovery_timeout(void *cls,
179 const struct GNUNET_SCHEDULER_TaskContext *tc)
181 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
183 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
184 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
185 GNUNET_DHT_get_stop (gph->get_handle);
186 gph->get_handle = NULL;
187 process_pseu_result(gph, NULL);
191 * Function called when we find a PSEU entry in the DHT
193 * @param cls the request handle
194 * @param exp lifetime
195 * @param key the key the record was stored under
196 * @param get_path get path
197 * @param get_path_length get path length
198 * @param put_path put path
199 * @param put_path_length put path length
200 * @param type the block type
201 * @param size the size of the record
202 * @param data the record data
205 process_auth_discovery_dht_result(void* cls,
206 struct GNUNET_TIME_Absolute exp,
207 const GNUNET_HashCode * key,
208 const struct GNUNET_PeerIdentity *get_path,
209 unsigned int get_path_length,
210 const struct GNUNET_PeerIdentity *put_path,
211 unsigned int put_path_length,
212 enum GNUNET_BLOCK_Type type,
213 size_t size, const void *data)
215 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
216 struct GNSNameRecordBlock *nrb;
217 char* rd_data = (char*)data;
223 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
224 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
228 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
229 "GNS_GET_AUTH: got dht result null!\n", size);
235 nrb = (struct GNSNameRecordBlock*)data;
237 /* stop lookup and timeout task */
238 GNUNET_DHT_get_stop (gph->get_handle);
239 gph->get_handle = NULL;
240 GNUNET_SCHEDULER_cancel(gph->timeout);
242 gph->get_handle = NULL;
244 nrb = (struct GNSNameRecordBlock*)data;
246 name = (char*)&nrb[1];
247 num_records = ntohl(nrb->rd_count);
249 struct GNUNET_NAMESTORE_RecordData rd[num_records];
251 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
252 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
254 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
259 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
260 "GNS_GET_AUTH: Error deserializing data!\n");
266 for (i=0; i<num_records; i++)
268 if ((strcmp(name, "+") == 0) &&
269 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
272 process_pseu_result(gph, (char*)rd[i].data);
278 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in dht!\n");
279 process_pseu_result(gph, NULL);
283 process_auth_discovery_ns_result(void* cls,
284 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
285 struct GNUNET_TIME_Absolute expiration,
286 const char *name, unsigned int rd_count,
287 const struct GNUNET_NAMESTORE_RecordData *rd,
288 const struct GNUNET_CRYPTO_RsaSignature *signature)
291 struct GNUNET_CRYPTO_ShortHashCode name_hash;
292 GNUNET_HashCode lookup_key;
293 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
294 GNUNET_HashCode name_hash_double;
295 GNUNET_HashCode zone_hash_double;
297 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
305 GNUNET_CRYPTO_short_hash("+", strlen("+"), &name_hash);
306 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
307 GNUNET_CRYPTO_short_hash_double (&gph->new_zone, &zone_hash_double);
308 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
309 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
311 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
312 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
313 "+", (char*)&lookup_key_string);
315 gph->timeout = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
316 &handle_auth_discovery_timeout, gph);
318 xquery = htonl(GNUNET_GNS_RECORD_PSEU);
320 GNUNET_assert(gph->get_handle == NULL);
321 gph->get_handle = GNUNET_DHT_get_start(dht_handle,
322 GNUNET_TIME_UNIT_FOREVER_REL,
323 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
325 DHT_GNS_REPLICATION_LEVEL,
329 &process_auth_discovery_dht_result,
333 for (i=0; i<rd_count; i++)
335 if ((strcmp(name, "+") == 0) &&
336 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
339 process_pseu_result(gph, (char*)rd[i].data);
346 * Callback called by namestore for a zone to name
349 * @param cls the closure
350 * @param zone_key the zone we queried
351 * @param expire the expiration time of the name
352 * @param name the name found or NULL
353 * @param rd_len number of records for the name
354 * @param rd the record data (PKEY) for the name
355 * @param signature the signature for the record data
358 process_zone_to_name_discover(void *cls,
359 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
360 struct GNUNET_TIME_Absolute expire,
363 const struct GNUNET_NAMESTORE_RecordData *rd,
364 const struct GNUNET_CRYPTO_RsaSignature *signature)
366 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
368 /* we found a match in our own zone */
371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
372 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
378 GNUNET_NAMESTORE_lookup_record(namestore_handle,
381 GNUNET_GNS_RECORD_PSEU,
382 &process_auth_discovery_ns_result,
391 * Callback for new authories
393 * @param name the name given by delegation
394 * @param zone the authority
395 * @param our_zone our local zone
396 * @param key the private key of our authority
398 static void process_discovered_authority(char* name,
399 struct GNUNET_CRYPTO_ShortHashCode zone,
400 struct GNUNET_CRYPTO_ShortHashCode our_zone,
401 struct GNUNET_CRYPTO_RsaPrivateKey *key)
403 struct GetPseuAuthorityHandle *gph;
406 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
407 "GNS_AUTO_PSEU: New authority %s discovered\n",
410 gph = GNUNET_malloc(sizeof(struct GetPseuAuthorityHandle));
411 namelen = strlen(name) + 1;
412 memcpy(gph->name, name, namelen);
414 gph->new_zone = zone;
415 gph->zone = our_zone;
418 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
421 &process_zone_to_name_discover,
427 * Initialize the resolver
429 * @param nh the namestore handle
430 * @param dh the dht handle
431 * @param lz the local zone's hash
432 * @param max_bg_queries maximum number of parallel background queries in dht
433 * @return GNUNET_OK on success
436 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
437 struct GNUNET_DHT_Handle *dh,
438 struct GNUNET_CRYPTO_ShortHashCode lz,
439 unsigned long long max_bg_queries)
441 namestore_handle = nh;
445 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
446 max_allowed_background_queries = max_bg_queries;
448 if ((namestore_handle != NULL) && (dht_handle != NULL))
452 return GNUNET_SYSERR;
456 * Cleanup background lookups
458 * @param cls closure to iterator
459 * @param node heap nodes
460 * @param element the resolver handle
461 * @param cost heap cost
462 * @return always GNUNET_YES
465 cleanup_pending_background_queries(void* cls,
466 struct GNUNET_CONTAINER_HeapNode *node,
468 GNUNET_CONTAINER_HeapCostType cost)
470 struct ResolverHandle *rh = (struct ResolverHandle *)element;
471 ResolverCleanupContinuation cont = cls;
473 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
474 "GNS_CLEANUP: Terminating background lookup for %s\n",
476 GNUNET_DHT_get_stop(rh->get_handle);
477 rh->get_handle = NULL;
478 rh->proc(rh->proc_cls, rh, 0, NULL);
480 GNUNET_CONTAINER_heap_remove_node(node);
482 if (GNUNET_CONTAINER_heap_get_size(dht_lookup_heap) == 0)
494 gns_resolver_cleanup(ResolverCleanupContinuation cont)
496 unsigned int s = GNUNET_CONTAINER_heap_get_size(dht_lookup_heap);
497 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
498 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
501 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
502 &cleanup_pending_background_queries,
510 * Helper function to free resolver handle
512 * @param rh the handle to free
515 free_resolver_handle(struct ResolverHandle* rh)
517 struct AuthorityChain *ac;
518 struct AuthorityChain *ac_next;
523 ac = rh->authority_chain_head;
536 * Callback when record data is put into namestore
538 * @param cls the closure
539 * @param success GNUNET_OK on success
540 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
543 on_namestore_record_put_result(void *cls,
547 if (GNUNET_NO == success)
549 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
550 "GNS_NS: records already in namestore\n");
553 else if (GNUNET_YES == success)
555 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
556 "GNS_NS: records successfully put in namestore\n");
560 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
561 "GNS_NS: Error putting records into namestore: %s\n", emsg);
565 handle_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
567 struct ResolverHandle *rh = cls;
569 if (rh->timeout_cont)
570 rh->timeout_cont(rh->timeout_cont_cls, tc);
574 * Processor for background lookups in the DHT
576 * @param cls closure (NULL)
577 * @param rd_count number of records found (not 0)
578 * @param rd record data
581 background_lookup_result_processor(void *cls,
583 const struct GNUNET_NAMESTORE_RecordData *rd)
585 //We could do sth verbose/more useful here but it doesn't make any difference
586 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
587 "GNS_BG: background dht lookup for finished. (%d results)\n",
592 * Handle timeout for DHT requests
594 * @param cls the request handle as closure
595 * @param tc the task context
598 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
600 struct ResolverHandle *rh = cls;
601 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
602 char new_name[MAX_DNS_NAME_LENGTH];
604 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
605 "GNS_PHASE_REC: dht lookup for query %s timed out.\n",
608 * Start resolution in bg
610 //strcpy(new_name, rh->name);
611 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
612 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
613 rh->name, GNUNET_GNS_TLD);
615 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
616 "GNS_PHASE_REC: Starting background lookup for %s type %d\n",
617 new_name, rlh->record_type);
619 gns_resolver_lookup_record(rh->authority,
623 GNUNET_TIME_UNIT_FOREVER_REL,
624 &background_lookup_result_processor,
626 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
628 GNUNET_DHT_get_stop (rh->get_handle);
629 rh->get_handle = NULL;
630 rh->proc(rh->proc_cls, rh, 0, NULL);
635 * Function called when we get a result from the dht
636 * for our record query
638 * @param cls the request handle
639 * @param exp lifetime
640 * @param key the key the record was stored under
641 * @param get_path get path
642 * @param get_path_length get path length
643 * @param put_path put path
644 * @param put_path_length put path length
645 * @param type the block type
646 * @param size the size of the record
647 * @param data the record data
650 process_record_result_dht(void* cls,
651 struct GNUNET_TIME_Absolute exp,
652 const GNUNET_HashCode * key,
653 const struct GNUNET_PeerIdentity *get_path,
654 unsigned int get_path_length,
655 const struct GNUNET_PeerIdentity *put_path,
656 unsigned int put_path_length,
657 enum GNUNET_BLOCK_Type type,
658 size_t size, const void *data)
660 struct ResolverHandle *rh;
661 struct RecordLookupHandle *rlh;
662 struct GNSNameRecordBlock *nrb;
663 uint32_t num_records;
665 char* rd_data = (char*)data;
669 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
670 "GNS_PHASE_REC: got dht result (size=%d)\n", size);
675 //FIXME maybe check expiration here, check block type
677 rh = (struct ResolverHandle *)cls;
678 rlh = (struct RecordLookupHandle *) rh->proc_cls;
679 nrb = (struct GNSNameRecordBlock*)data;
681 /* stop lookup and timeout task */
682 GNUNET_DHT_get_stop (rh->get_handle);
683 rh->get_handle = NULL;
685 if (rh->dht_heap_node != NULL)
687 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
688 rh->dht_heap_node = NULL;
691 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
693 GNUNET_SCHEDULER_cancel(rh->timeout_task);
694 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
697 rh->get_handle = NULL;
698 name = (char*)&nrb[1];
699 num_records = ntohl(nrb->rd_count);
701 struct GNUNET_NAMESTORE_RecordData rd[num_records];
703 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
704 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
706 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
711 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
715 for (i=0; i<num_records; i++)
717 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
718 "GNS_PHASE_REC: Got name: %s (wanted %s)\n", name, rh->name);
719 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
720 "GNS_PHASE_REC: Got type: %d\n",
722 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
723 "GNS_PHASE_REC: Got data length: %d\n", rd[i].data_size);
724 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
725 "GNS_PHASE_REC: Got flag %d\n", rd[i].flags);
727 if ((strcmp(name, rh->name) == 0) &&
728 (rd[i].record_type == rlh->record_type))
736 * FIXME check pubkey against existing key in namestore?
737 * https://gnunet.org/bugs/view.php?id=2179
740 /* Save to namestore */
741 GNUNET_NAMESTORE_record_put (namestore_handle,
748 &on_namestore_record_put_result, //cont
753 rh->proc(rh->proc_cls, rh, num_records, rd);
755 rh->proc(rh->proc_cls, rh, 0, NULL);
762 * Start DHT lookup for a (name -> query->record_type) record in
763 * rh->authority's zone
765 * @param rh the pending gns query context
768 resolve_record_dht(struct ResolverHandle *rh)
771 struct GNUNET_CRYPTO_ShortHashCode name_hash;
772 GNUNET_HashCode lookup_key;
773 GNUNET_HashCode name_hash_double;
774 GNUNET_HashCode zone_hash_double;
775 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
776 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
777 struct ResolverHandle *rh_heap_root;
779 GNUNET_CRYPTO_short_hash(rh->name, strlen(rh->name), &name_hash);
780 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
781 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
782 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
783 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
785 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
786 "GNS_PHASE_REC: starting dht lookup for %s with key: %s\n",
787 rh->name, (char*)&lookup_key_string);
789 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
790 rh->dht_heap_node = NULL;
792 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
795 * Update timeout if necessary
797 if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
800 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
801 "GNS_PHASE_REC: Adjusting timeout\n");
803 * Set timeout for authority lookup phase to 1/2
805 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
806 GNUNET_TIME_relative_divide(rh->timeout, 2),
807 &handle_lookup_timeout,
810 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
811 // &dht_lookup_timeout,
813 rh->timeout_cont = &dht_lookup_timeout;
814 rh->timeout_cont_cls = rh;
818 if (max_allowed_background_queries <=
819 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
821 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
822 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
823 rh_heap_root->get_handle = NULL;
824 rh_heap_root->dht_heap_node = NULL;
826 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
827 "GNS_PHASE_REC: Replacing oldest background query for %s\n",
829 rh_heap_root->proc(rh_heap_root->proc_cls,
834 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
836 GNUNET_TIME_absolute_get().abs_value);
839 xquery = htonl(rlh->record_type);
841 GNUNET_assert(rh->get_handle == NULL);
842 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
843 GNUNET_TIME_UNIT_FOREVER_REL,
844 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
846 DHT_GNS_REPLICATION_LEVEL,
850 &process_record_result_dht,
857 * Namestore calls this function if we have record for this name.
858 * (or with rd_count=0 to indicate no matches)
860 * @param cls the pending query
861 * @param key the key of the zone we did the lookup
862 * @param expiration expiration date of the namestore entry
863 * @param name the name for which we need an authority
864 * @param rd_count the number of records with 'name'
865 * @param rd the record data
866 * @param signature the signature of the authority for the record data
869 process_record_result_ns(void* cls,
870 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
871 struct GNUNET_TIME_Absolute expiration,
872 const char *name, unsigned int rd_count,
873 const struct GNUNET_NAMESTORE_RecordData *rd,
874 const struct GNUNET_CRYPTO_RsaSignature *signature)
876 struct ResolverHandle *rh;
877 struct RecordLookupHandle *rlh;
878 struct GNUNET_TIME_Relative remaining_time;
879 struct GNUNET_CRYPTO_ShortHashCode zone;
881 rh = (struct ResolverHandle *) cls;
882 rlh = (struct RecordLookupHandle *)rh->proc_cls;
883 GNUNET_CRYPTO_short_hash(key,
884 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
886 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
894 rh->status |= EXISTS;
897 if (remaining_time.rel_value == 0)
899 rh->status |= EXPIRED;
905 * Lookup terminated and no results
907 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
908 "GNS_PHASE_REC: Namestore lookup for %s terminated without results\n",
911 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
912 "GNS_PHASE_REC: Record %s unknown in namestore\n",
915 * Our zone and no result? Cannot resolve TT
917 rh->proc(rh->proc_cls, rh, 0, NULL);
924 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
925 "GNS_PHASE_REC: Processing additional result %s from namestore\n",
928 for (i=0; i<rd_count;i++)
931 if (rd[i].record_type != rlh->record_type)
934 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
937 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
938 "GNS_PHASE_REC: This record is expired. Skipping\n");
949 if (rh->answered == 0)
951 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
952 "GNS_PHASE_REC: No answers found. This is odd!\n");
953 rh->proc(rh->proc_cls, rh, 0, NULL);
957 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
958 "GNS_PHASE_REC: Found %d answer(s) to query in %d records!\n",
959 rh->answered, rd_count);
961 rh->proc(rh->proc_cls, rh, rd_count, rd);
967 * The final phase of resolution.
968 * rh->name is a name that is canonical and we do not have a delegation.
969 * Query namestore for this record
971 * @param rh the pending lookup
974 resolve_record_ns(struct ResolverHandle *rh)
976 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
978 /* We cancel here as to not include the ns lookup in the timeout */
979 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
981 GNUNET_SCHEDULER_cancel(rh->timeout_task);
982 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
986 * Try to resolve this record in our namestore.
987 * The name to resolve is now in rh->authority_name
988 * since we tried to resolve it to an authority
991 GNUNET_NAMESTORE_lookup_record(namestore_handle,
995 &process_record_result_ns,
1002 * Handle timeout for DHT requests
1004 * @param cls the request handle as closure
1005 * @param tc the task context
1008 dht_authority_lookup_timeout(void *cls,
1009 const struct GNUNET_SCHEDULER_TaskContext *tc)
1011 struct ResolverHandle *rh = cls;
1012 struct RecordLookupHandle *rlh = rh->proc_cls;
1013 char new_name[MAX_DNS_NAME_LENGTH];
1015 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1016 "GNS_PHASE_DELEGATE: dht lookup for query %s timed out.\n",
1017 rh->authority_name);
1019 rh->status |= TIMED_OUT;
1021 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1023 GNUNET_DHT_get_stop (rh->get_handle);
1024 rh->get_handle = NULL;
1026 if (strcmp(rh->name, "") == 0)
1029 * promote authority back to name and try to resolve record
1031 strcpy(rh->name, rh->authority_name);
1032 rh->proc(rh->proc_cls, rh, 0, NULL);
1037 * Start resolution in bg
1039 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
1040 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
1041 //strcpy(new_name, rh->name);
1042 //strcpy(new_name+strlen(new_name), ".");
1043 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1045 strcpy(rh->name, new_name);
1047 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1048 "GNS_PHASE_DELEGATE: Starting background query for %s type %d\n",
1049 rh->name, rlh->record_type);
1051 gns_resolver_lookup_record(rh->authority,
1055 GNUNET_TIME_UNIT_FOREVER_REL,
1056 &background_lookup_result_processor,
1059 rh->proc(rh->proc_cls, rh, 0, NULL);
1063 static void resolve_delegation_dht(struct ResolverHandle *rh);
1066 static void resolve_delegation_ns(struct ResolverHandle *rh);
1069 * Function called when we get a result from the dht
1070 * for our query. Recursively tries to resolve authorities
1073 * @param cls the request handle
1074 * @param exp lifetime
1075 * @param key the key the record was stored under
1076 * @param get_path get path
1077 * @param get_path_length get path length
1078 * @param put_path put path
1079 * @param put_path_length put path length
1080 * @param type the block type
1081 * @param size the size of the record
1082 * @param data the record data
1085 process_delegation_result_dht(void* cls,
1086 struct GNUNET_TIME_Absolute exp,
1087 const GNUNET_HashCode * key,
1088 const struct GNUNET_PeerIdentity *get_path,
1089 unsigned int get_path_length,
1090 const struct GNUNET_PeerIdentity *put_path,
1091 unsigned int put_path_length,
1092 enum GNUNET_BLOCK_Type type,
1093 size_t size, const void *data)
1095 struct ResolverHandle *rh;
1096 struct GNSNameRecordBlock *nrb;
1097 uint32_t num_records;
1099 char* rd_data = (char*) data;
1102 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
1103 GNUNET_HashCode zone_hash_double, name_hash_double;
1105 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "GNS_PHASE_DELEGATE: Got DHT result\n");
1110 //FIXME check expiration?
1112 rh = (struct ResolverHandle *)cls;
1113 nrb = (struct GNSNameRecordBlock*)data;
1115 /* stop dht lookup and timeout task */
1116 GNUNET_DHT_get_stop (rh->get_handle);
1118 rh->get_handle = NULL;
1120 if (rh->dht_heap_node != NULL)
1122 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
1123 rh->dht_heap_node = NULL;
1126 num_records = ntohl(nrb->rd_count);
1127 name = (char*)&nrb[1];
1129 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1131 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1132 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
1134 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1139 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1140 "GNS_PHASE_DELEGATE: Error deserializing data!\n");
1144 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1145 "GNS_PHASE_DELEGATE: Got name: %s (wanted %s)\n",
1146 name, rh->authority_name);
1147 for (i=0; i<num_records; i++)
1150 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1151 "GNS_PHASE_DELEGATE: Got name: %s (wanted %s)\n",
1152 name, rh->authority_name);
1153 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1154 "GNS_PHASE_DELEGATE: Got type: %d (wanted %d)\n",
1155 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
1156 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1157 "GNS_PHASE_DELEGATE: Got data length: %d\n",
1159 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1160 "GNS_PHASE_DELEGATE: Got flag %d\n", rd[i].flags);
1162 if ((strcmp(name, rh->authority_name) == 0) &&
1163 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
1165 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1166 "GNS_PHASE_DELEGATE: Authority found in DHT\n");
1168 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
1169 struct AuthorityChain *auth =
1170 GNUNET_malloc(sizeof(struct AuthorityChain));
1171 auth->zone = rh->authority;
1172 memset(auth->name, 0, strlen(rh->authority_name)+1);
1173 strcpy(auth->name, rh->authority_name);
1174 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1175 rh->authority_chain_tail,
1178 /** try to import pkey if private key available */
1180 process_discovered_authority(name, auth->zone,
1181 rh->authority_chain_tail->zone,
1188 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
1189 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1190 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
1191 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
1193 /* Save to namestore */
1194 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
1197 GNUNET_NAMESTORE_record_put (namestore_handle,
1204 &on_namestore_record_put_result, //cont
1214 * FIXME in this case. should we ask namestore again?
1216 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1217 "GNS_PHASE_DELEGATE: Answer from DHT for %s to resolve: %s\n",
1218 rh->authority_name, rh->name);
1219 if (strcmp(rh->name, "") == 0)
1220 rh->proc(rh->proc_cls, rh, 0, NULL);
1222 resolve_delegation_ns(rh);
1227 * No pkey but name exists
1230 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1231 "GNS_PHASE_DELEGATE: Adding %s back to %s\n",
1232 rh->authority_name, rh->name);
1233 if (strcmp(rh->name, "") == 0)
1234 strcpy(rh->name, rh->authority_name);
1236 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
1237 rh->name, rh->authority_name); //FIXME ret
1239 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1240 "GNS_PHASE_DELEGATE: %s restored\n", rh->name);
1241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1242 "GNS_PHASE_DELEGATE: DHT authority lookup found no match!\n");
1243 rh->proc(rh->proc_cls, rh, 0, NULL);
1246 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
1247 +(MAX_DNS_NAME_LENGTH*2)
1248 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
1252 expand_plus(char** dest, char* src, char* repl)
1255 unsigned int s_len = strlen(src)+1;
1257 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1258 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
1262 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1263 "GNS_POSTPROCESS: %s to short\n", src);
1265 /* no postprocessing */
1266 memcpy(*dest, src, s_len+1);
1270 if (0 == strcmp(src+s_len-3, ".+"))
1272 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1273 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
1274 memset(*dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
1276 pos = *dest+s_len-2;
1278 pos += strlen(repl);
1279 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1280 "GNS_POSTPROCESS: Expanded to %s\n", *dest);
1284 memcpy(*dest, src, s_len+1);
1292 finish_lookup(struct ResolverHandle *rh,
1293 struct RecordLookupHandle* rlh,
1294 unsigned int rd_count,
1295 const struct GNUNET_NAMESTORE_RecordData *rd)
1298 char new_rr_data[MAX_DNS_NAME_LENGTH];
1299 char new_mx_data[MAX_MX_LENGTH];
1300 char new_soa_data[MAX_SOA_LENGTH];
1301 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
1304 unsigned int offset;
1306 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1307 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1310 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
1312 for (i = 0; i < rd_count; i++)
1315 if (rd[i].record_type != GNUNET_GNS_RECORD_TYPE_NS &&
1316 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_CNAME &&
1317 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
1318 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_SOA)
1320 p_rd[i].data = rd[i].data;
1325 * for all those records we 'should'
1326 * also try to resolve the A/AAAA records (RFC1035)
1327 * This is a feature and not important
1330 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1331 "GNS_POSTPROCESS: Postprocessing\n");
1333 if (strcmp(rh->name, "+") == 0)
1334 repl_string = rlh->name;
1336 repl_string = rlh->name+strlen(rh->name)+1;
1339 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
1341 memcpy(new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
1342 offset = sizeof(uint16_t);
1343 pos = new_mx_data+offset;
1344 expand_plus(&pos, (char*)rd[i].data+sizeof(uint16_t),
1346 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
1347 p_rd[i].data = new_mx_data;
1348 p_rd[i].data_size = offset;
1350 else if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_SOA)
1352 /* expand mname and rname */
1354 expand_plus(&pos, (char*)rd[i].data, repl_string);
1355 offset = strlen(new_soa_data)+1;
1356 pos = new_soa_data+offset;
1357 expand_plus(&pos, (char*)rd[i].data+offset, repl_string);
1358 offset += strlen(new_soa_data+offset)+1;
1359 /* cpy the 4 numbers serial refresh retry and expire */
1360 memcpy(new_soa_data+offset, (char*)rd[i].data+offset, sizeof(uint32_t)*5);
1361 offset += sizeof(uint32_t)*5;
1362 p_rd[i].data_size = offset;
1363 p_rd[i].data = new_soa_data;
1368 expand_plus(&pos, (char*)rd[i].data, repl_string);
1369 p_rd[i].data_size = strlen(new_rr_data)+1;
1370 p_rd[i].data = new_rr_data;
1375 rlh->proc(rlh->proc_cls, rd_count, p_rd);
1381 * Process DHT lookup result for record.
1383 * @param cls the closure
1384 * @param rh resolver handle
1385 * @param rd_count number of results
1386 * @param rd record data
1389 handle_record_dht(void* cls, struct ResolverHandle *rh,
1390 unsigned int rd_count,
1391 const struct GNUNET_NAMESTORE_RecordData *rd)
1393 struct RecordLookupHandle* rlh;
1395 rlh = (struct RecordLookupHandle*)cls;
1398 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1399 "GNS_PHASE_REC: No records for %s found in DHT. Aborting\n",
1401 /* give up, cannot resolve */
1402 finish_lookup(rh, rlh, 0, NULL);
1403 free_resolver_handle(rh);
1407 /* results found yay */
1408 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1409 "GNS_PHASE_REC: Record resolved from DHT!");
1411 finish_lookup(rh, rlh, rd_count, rd);
1412 free_resolver_handle(rh);
1418 * Process namestore lookup result for record.
1420 * @param cls the closure
1421 * @param rh resolver handle
1422 * @param rd_count number of results
1423 * @param rd record data
1426 handle_record_ns(void* cls, struct ResolverHandle *rh,
1427 unsigned int rd_count,
1428 const struct GNUNET_NAMESTORE_RecordData *rd)
1430 struct RecordLookupHandle* rlh;
1431 rlh = (struct RecordLookupHandle*) cls;
1434 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1435 "GNS_PHASE_REC: NS returned no records. (status: %d)!\n",
1439 * There are 4 conditions that have to met for us to consult the DHT:
1440 * 1. The entry in the DHT is EXPIRED AND
1441 * 2. No entry in the NS existed AND
1442 * 3. The zone queried is not the local resolver's zone AND
1443 * 4. The name that was looked up is '+'
1444 * because if it was any other canonical name we either already queried
1445 * the DHT for the authority in the authority lookup phase (and thus
1446 * would already have an entry in the NS for the record)
1448 if (rh->status & (EXPIRED | !EXISTS) &&
1449 GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1451 (strcmp(rh->name, "+") == 0))
1453 rh->proc = &handle_record_dht;
1454 resolve_record_dht(rh);
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: Record resolved from namestore!");
1467 finish_lookup(rh, rlh, rd_count, rd);
1469 free_resolver_handle(rh);
1475 * Determine if this name is canonical.
1477 * a.b.gnunet = not canonical
1480 * @param name the name to test
1481 * @return 1 if canonical
1484 is_canonical(char* name)
1486 uint32_t len = strlen(name);
1489 for (i=0; i<len; i++)
1491 if (*(name+i) == '.')
1498 * Move one level up in the domain hierarchy and return the
1499 * passed top level domain.
1501 * @param name the domain
1502 * @param dest the destination where the tld will be put
1505 pop_tld(char* name, char* dest)
1509 if (is_canonical(name))
1516 for (len = strlen(name); len > 0; len--)
1518 if (*(name+len) == '.')
1528 strcpy(dest, (name+len+1));
1532 * Checks if name is in tld
1534 * @param name the name to check
1535 * @param tld the TLD to check for
1536 * @return GNUNET_YES or GNUNET_NO
1539 is_tld(const char* name, const char* tld)
1543 if (strlen(name) <= strlen(tld))
1548 offset = strlen(name)-strlen(tld);
1549 if (strcmp(name+offset, tld) != 0)
1551 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1552 "%s is not in .%s TLD\n", name, tld);
1559 * DHT resolution for delegation finished. Processing result.
1561 * @param cls the closure
1562 * @param rh resolver handle
1563 * @param rd_count number of results (always 0)
1564 * @param rd record data (always NULL)
1567 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
1568 unsigned int rd_count,
1569 const struct GNUNET_NAMESTORE_RecordData *rd)
1571 struct RecordLookupHandle* rlh;
1572 rlh = (struct RecordLookupHandle*) cls;
1575 if (strcmp(rh->name, "") == 0)
1577 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
1579 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1580 "GNS_PHASE_DELEGATE_DHT: Resolved queried PKEY via DHT.\n");
1581 finish_lookup(rh, rlh, rd_count, rd);
1582 free_resolver_handle(rh);
1585 /* We resolved full name for delegation. resolving record */
1586 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1587 "GNS_PHASE_DELEGATE_DHT: Resolved full name for delegation via DHT.\n");
1588 strcpy(rh->name, "+\0");
1589 rh->proc = &handle_record_ns;
1590 resolve_record_ns(rh);
1595 * we still have some left
1597 if (is_canonical(rh->name))
1599 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1600 "GNS_PHASE_DELEGATE_DHT: Resolving canonical record %s in ns\n",
1602 rh->proc = &handle_record_ns;
1603 resolve_record_ns(rh);
1606 /* give up, cannot resolve */
1607 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1608 "GNS_PHASE_DELEGATE_DHT: Cannot fully resolve delegation for %s via DHT!\n",
1610 finish_lookup(rh, rlh, 0, NULL);
1611 free_resolver_handle(rh);
1616 * Start DHT lookup for a name -> PKEY (compare NS) record in
1617 * rh->authority's zone
1619 * @param rh the pending gns query
1622 resolve_delegation_dht(struct ResolverHandle *rh)
1625 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1626 GNUNET_HashCode name_hash_double;
1627 GNUNET_HashCode zone_hash_double;
1628 GNUNET_HashCode lookup_key;
1629 struct ResolverHandle *rh_heap_root;
1631 pop_tld(rh->name, rh->authority_name);
1632 GNUNET_CRYPTO_short_hash(rh->authority_name,
1633 strlen(rh->authority_name),
1635 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1636 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
1637 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
1639 rh->dht_heap_node = NULL;
1641 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1643 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1644 // &dht_authority_lookup_timeout,
1646 rh->timeout_cont = &dht_authority_lookup_timeout;
1647 rh->timeout_cont_cls = rh;
1651 if (max_allowed_background_queries <=
1652 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1654 /* terminate oldest lookup */
1655 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1656 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
1657 rh_heap_root->dht_heap_node = NULL;
1659 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1660 "GNS_PHASE_DELEGATE: Replacing oldest background query for %s\n",
1661 rh_heap_root->authority_name);
1663 rh_heap_root->proc(rh_heap_root->proc_cls,
1668 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1670 GNUNET_TIME_absolute_get().abs_value);
1673 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1675 GNUNET_assert(rh->get_handle == NULL);
1676 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1677 GNUNET_TIME_UNIT_FOREVER_REL,
1678 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1680 DHT_GNS_REPLICATION_LEVEL,
1684 &process_delegation_result_dht,
1691 * Namestore resolution for delegation finished. Processing result.
1693 * @param cls the closure
1694 * @param rh resolver handle
1695 * @param rd_count number of results (always 0)
1696 * @param rd record data (always NULL)
1699 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1700 unsigned int rd_count,
1701 const struct GNUNET_NAMESTORE_RecordData *rd)
1703 struct RecordLookupHandle* rlh;
1704 rlh = (struct RecordLookupHandle*) cls;
1706 if (strcmp(rh->name, "") == 0)
1708 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
1710 GNUNET_assert(rd_count == 1);
1711 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1712 "GNS_PHASE_DELEGATE_NS: Resolved queried PKEY in NS.\n");
1713 finish_lookup(rh, rlh, rd_count, rd);
1714 free_resolver_handle(rh);
1717 /* We resolved full name for delegation. resolving record */
1718 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1719 "GNS_PHASE_DELEGATE_NS: Resolved full name for delegation.\n");
1720 strcpy(rh->name, "+\0");
1721 rh->proc = &handle_record_ns;
1722 resolve_record_ns(rh);
1727 * we still have some left
1728 * check if authority in ns is fresh
1730 * or we are authority
1732 if ((rh->status & (EXISTS | !EXPIRED)) ||
1733 !GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1734 &rh->authority_chain_tail->zone))
1736 if (is_canonical(rh->name))
1738 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1739 "GNS_PHASE_DELEGATE_NS: Resolving canonical record %s\n",
1741 rh->proc = &handle_record_ns;
1742 resolve_record_ns(rh);
1746 /* give up, cannot resolve */
1747 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1748 "GNS_PHASE_DELEGATE_NS: Cannot fully resolve delegation for %s!\n",
1750 finish_lookup(rh, rlh, rd_count, rd);
1751 //rlh->proc(rlh->proc_cls, 0, NULL);
1756 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1757 "GNS_PHASE_DELEGATE_NS: Trying to resolve delegation for %s via DHT\n",
1759 rh->proc = &handle_delegation_dht;
1760 resolve_delegation_dht(rh);
1766 * This is a callback function that should give us only PKEY
1767 * records. Used to query the namestore for the authority (PKEY)
1768 * for 'name'. It will recursively try to resolve the
1769 * authority for a given name from the namestore.
1771 * @param cls the pending query
1772 * @param key the key of the zone we did the lookup
1773 * @param expiration expiration date of the record data set in the namestore
1774 * @param name the name for which we need an authority
1775 * @param rd_count the number of records with 'name'
1776 * @param rd the record data
1777 * @param signature the signature of the authority for the record data
1780 process_delegation_result_ns(void* cls,
1781 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1782 struct GNUNET_TIME_Absolute expiration,
1784 unsigned int rd_count,
1785 const struct GNUNET_NAMESTORE_RecordData *rd,
1786 const struct GNUNET_CRYPTO_RsaSignature *signature)
1788 struct ResolverHandle *rh;
1789 struct GNUNET_TIME_Relative remaining_time;
1790 struct GNUNET_CRYPTO_ShortHashCode zone;
1791 char new_name[MAX_DNS_NAME_LENGTH];
1793 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1794 "GNS_PHASE_DELEGATE: Got %d records from authority lookup\n",
1797 rh = (struct ResolverHandle *)cls;
1798 GNUNET_CRYPTO_short_hash(key,
1799 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1801 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1807 rh->status |= EXISTS;
1810 if (remaining_time.rel_value == 0)
1812 rh->status |= EXPIRED;
1816 * No authority found in namestore.
1821 * We did not find an authority in the namestore
1826 * Promote this authority back to a name maybe it is
1829 if (strcmp(rh->name, "") == 0)
1831 /* simply promote back */
1832 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1833 "GNS_PHASE_DELEGATE: Promoting %s back to name\n",
1834 rh->authority_name);
1835 strcpy(rh->name, rh->authority_name);
1839 /* add back to existing name */
1840 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1841 "GNS_PHASE_DELEGATE: Adding %s back to %s\n",
1842 rh->authority_name, rh->name);
1843 //memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1844 strcpy(new_name, rh->name);
1845 strcpy(new_name+strlen(new_name), ".");
1846 strcpy(new_name+strlen(new_name), rh->authority_name);
1847 strcpy(rh->name, new_name);
1848 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1849 "GNS_PHASE_DELEGATE: %s restored\n", rh->name);
1851 rh->proc(rh->proc_cls, rh, 0, NULL);
1856 * We found an authority that may be able to help us
1857 * move on with query
1858 * Note only 1 pkey should have been returned.. anything else would be strange
1861 for (i=0; i<rd_count;i++)
1864 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1867 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1870 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1871 "GNS_PHASE_DELEGATE: This pkey is expired.\n");
1872 if (remaining_time.rel_value == 0)
1874 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1875 "GNS_PHASE_DELEGATE: This dht entry is expired.\n");
1876 rh->authority_chain_head->fresh = 0;
1877 rh->proc(rh->proc_cls, rh, 0, NULL);
1885 * Resolve rest of query with new authority
1887 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1888 memcpy(&rh->authority, rd[i].data,
1889 sizeof(struct GNUNET_CRYPTO_ShortHashCode));
1890 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1891 auth->zone = rh->authority;
1892 memset(auth->name, 0, strlen(rh->authority_name)+1);
1893 strcpy(auth->name, rh->authority_name);
1894 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1895 rh->authority_chain_tail,
1898 /** try to import pkey if private key available */
1900 process_discovered_authority((char*)name, auth->zone,
1901 rh->authority_chain_tail->zone,
1904 * We are done with PKEY resolution if name is empty
1905 * else resolve again with new authority
1907 if (strcmp(rh->name, "") == 0)
1908 rh->proc(rh->proc_cls, rh, rd_count, rd);
1910 resolve_delegation_ns(rh);
1917 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1918 "GNS_PHASE_DELEGATE: Authority lookup and but no PKEY... never get here\n");
1919 rh->proc(rh->proc_cls, rh, 0, NULL);
1924 * Resolve the delegation chain for the request in our namestore
1926 * @param rh the resolver handle
1929 resolve_delegation_ns(struct ResolverHandle *rh)
1931 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1932 "GNS_PHASE_DELEGATE: Resolving delegation for %s\n", rh->name);
1933 pop_tld(rh->name, rh->authority_name);
1934 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1937 GNUNET_GNS_RECORD_PKEY,
1938 &process_delegation_result_ns,
1945 * Lookup of a record in a specific zone
1946 * calls lookup result processor on result
1948 * @param zone the root zone
1949 * @param record_type the record type to look up
1950 * @param name the name to look up
1951 * @param key a private key for use with PSEU import (can be NULL)
1952 * @param timeout timeout for resolution
1953 * @param proc the processor to call on result
1954 * @param cls the closure to pass to proc
1957 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
1958 uint32_t record_type,
1960 struct GNUNET_CRYPTO_RsaPrivateKey *key,
1961 struct GNUNET_TIME_Relative timeout,
1962 RecordLookupProcessor proc,
1965 struct ResolverHandle *rh;
1966 struct RecordLookupHandle* rlh;
1967 char string_hash[MAX_DNS_LABEL_LENGTH];
1968 uint8_t* normalized_zkey;
1969 char nzkey[MAX_DNS_LABEL_LENGTH];
1972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1973 "Starting resolution for %s (type=%d)!\n",
1977 if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
1979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1980 "%s is canonical and not gnunet -> cannot resolve!\n", name);
1985 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
1986 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
1988 rh->authority = zone;
1991 rh->timeout = timeout;
1992 rh->get_handle = NULL;
1993 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1996 * Set timeout for authority lookup phase to 1/2
1998 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
1999 GNUNET_TIME_relative_divide(timeout, 2),
2000 &handle_lookup_timeout,
2002 rh->timeout_cont = &dht_authority_lookup_timeout;
2003 rh->timeout_cont_cls = rh;
2007 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
2008 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2011 if (strcmp(GNUNET_GNS_TLD, name) == 0)
2014 * Only 'gnunet' given
2016 strcpy(rh->name, "\0");
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2021 "Checking for TLD...\n");
2022 if (is_zkey_tld(name) == GNUNET_YES)
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2027 * This is a zkey tld
2028 * build hash and use as initial authority
2031 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
2032 memcpy(rh->name, name,
2033 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
2034 pop_tld(rh->name, string_hash);
2036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2037 "ZKEY is %s!\n", string_hash);
2039 normalized_zkey = u8_toupper ((uint8_t*)string_hash,
2040 strlen ((char *) string_hash),
2041 NULL, UNINORM_NFD, NULL, &normal_len);
2044 memcpy(nzkey, normalized_zkey, normal_len);
2045 nzkey[normal_len] = '\0';
2046 free(normalized_zkey);
2048 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
2051 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2052 "Cannot convert ZKEY %s to hash!\n", string_hash);
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2065 * Presumably GNUNET tld
2068 strlen(name)-strlen(GNUNET_GNS_TLD));
2069 memcpy(rh->name, name,
2070 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2075 * Initialize authority chain
2077 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2078 rh->authority_chain_head->prev = NULL;
2079 rh->authority_chain_head->next = NULL;
2080 rh->authority_chain_tail = rh->authority_chain_head;
2081 rh->authority_chain_head->zone = rh->authority;
2084 * Copy original query into lookup handle
2086 rlh->record_type = record_type;
2087 memset(rlh->name, 0, strlen(name) + 1);
2088 strcpy(rlh->name, name);
2090 rlh->proc_cls = cls;
2092 rh->proc = &handle_delegation_ns;
2093 resolve_delegation_ns(rh);
2096 /******** END Record Resolver ***********/
2100 * Callback calles by namestore for a zone to name
2103 * @param cls the closure
2104 * @param zone_key the zone we queried
2105 * @param expire the expiration time of the name
2106 * @param name the name found or NULL
2107 * @param rd_len number of records for the name
2108 * @param rd the record data (PKEY) for the name
2109 * @param signature the signature for the record data
2112 process_zone_to_name_shorten(void *cls,
2113 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
2114 struct GNUNET_TIME_Absolute expire,
2116 unsigned int rd_len,
2117 const struct GNUNET_NAMESTORE_RecordData *rd,
2118 const struct GNUNET_CRYPTO_RsaSignature *signature)
2120 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
2121 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
2122 struct AuthorityChain *next_authority;
2124 char result[MAX_DNS_NAME_LENGTH];
2125 char next_authority_name[MAX_DNS_LABEL_LENGTH];
2128 /* we found a match in our own zone */
2131 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2132 "result strlen %d\n", strlen(name));
2133 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
2134 memset(result, 0, answer_len);
2135 if (strlen(rh->name) > 0)
2137 strcpy(result, rh->name);
2138 strcpy(result+strlen(rh->name), ".");
2141 strcpy(result+strlen(result), name);
2142 strcpy(result+strlen(result), ".");
2143 strcpy(result+strlen(result), GNUNET_GNS_TLD);
2145 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2146 "Sending shorten result %s\n", result);
2148 nsh->proc(nsh->proc_cls, result);
2150 free_resolver_handle(rh);
2152 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2155 /* our zone, just append .gnunet */
2156 answer_len = strlen(rh->name) + strlen(GNUNET_GNS_TLD) + 2;
2157 memset(result, 0, answer_len);
2158 strcpy(result, rh->name);
2159 strcpy(result+strlen(rh->name), ".");
2160 strcpy(result+strlen(rh->name)+1, GNUNET_GNS_TLD);
2162 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2163 "Our zone: Sending name as shorten result %s\n", rh->name);
2165 nsh->proc(nsh->proc_cls, result);
2167 free_resolver_handle(rh);
2173 * continue with next authority
2175 next_authority = rh->authority_chain_head;
2176 // strlen(next_authority->name) + 2);
2177 memset(next_authority_name, 0, strlen(rh->name)+
2178 strlen(next_authority->name) + 2);
2179 strcpy(next_authority_name, rh->name);
2180 strcpy(next_authority_name+strlen(rh->name)+1, ".");
2181 strcpy(next_authority_name+strlen(rh->name)+2, next_authority->name);
2183 strcpy(rh->name, next_authority_name);
2184 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
2185 rh->authority_chain_tail,
2188 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2189 &rh->authority_chain_tail->zone,
2190 &rh->authority_chain_head->zone,
2191 &process_zone_to_name_shorten,
2197 * DHT resolution for delegation finished. Processing result.
2199 * @param cls the closure
2200 * @param rh resolver handle
2201 * @param rd_count number of results (always 0)
2202 * @param rd record data (always NULL)
2205 handle_delegation_dht_bg_shorten(void* cls, struct ResolverHandle *rh,
2206 unsigned int rd_count,
2207 const struct GNUNET_NAMESTORE_RecordData *rd)
2210 /* We resolved full name for delegation. resolving record */
2211 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2212 "GNS_SHORTEN: Resolved up to %s for delegation via DHT in background.\n",
2214 free_resolver_handle(rh);
2218 * Process result from namestore delegation lookup
2219 * for shorten operation
2221 * @param cls the client shorten handle
2222 * @param rh the resolver handle
2223 * @param rd_count number of results (0)
2224 * @param rd data (NULL)
2227 handle_delegation_ns_shorten(void* cls,
2228 struct ResolverHandle *rh,
2230 const struct GNUNET_NAMESTORE_RecordData *rd)
2232 struct NameShortenHandle *nsh;
2233 char result[MAX_DNS_NAME_LENGTH];
2235 struct ResolverHandle *rh_bg;
2237 nsh = (struct NameShortenHandle *)cls;
2240 * At this point rh->name contains the part of the name
2241 * that we do not have a PKEY in our namestore to resolve.
2242 * The authority chain in the resolver handle is now
2243 * useful to backtrack if needed
2246 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2247 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2249 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2253 * This is our zone append .gnunet unless name is empty
2254 * (it shouldn't be, usually FIXME what happens if we
2255 * shorten to our zone to a "" record??)
2258 answer_len = strlen(rh->name) + strlen(GNUNET_GNS_TLD) + 2;
2259 memset(result, 0, answer_len);
2260 strcpy(result, rh->name);
2261 strcpy(result+strlen(rh->name), ".");
2262 strcpy(result+strlen(rh->name)+1, GNUNET_GNS_TLD);
2264 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2265 "Our zone: Sending name as shorten result %s\n", rh->name);
2267 nsh->proc(nsh->proc_cls, result);
2269 free_resolver_handle(rh);
2274 * we have to this before zone to name for rh might
2278 if (!is_canonical(rh->name))
2280 rh_bg = GNUNET_malloc(sizeof(struct ResolverHandle));
2281 memcpy(rh_bg, rh, sizeof(struct ResolverHandle));
2284 /* backtrack authorities for names */
2285 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2286 &rh->authority_chain_tail->zone, //ours
2287 &rh->authority_chain_head->zone,
2288 &process_zone_to_name_shorten,
2297 * If authority resolution is incomplete we can do a background lookup
2298 * of the full name so that next time we can (likely) fully or at least
2299 * further shorten the name
2301 rh_bg->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2302 rh_bg->authority_chain_tail = rh_bg->authority_chain_head;
2303 rh_bg->authority_chain_head->zone = rh_bg->authority;
2305 rh_bg->proc = &handle_delegation_dht_bg_shorten;
2306 rh_bg->proc_cls = NULL;
2308 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2309 "GNS_SHORTEN: Starting background lookup for %s\n",
2312 resolve_delegation_dht(rh_bg);
2318 * Callback calles by namestore for a zone to name
2321 * @param cls the closure
2322 * @param zone_key the zone we queried
2323 * @param expire the expiration time of the name
2324 * @param name the name found or NULL
2325 * @param rd_len number of records for the name
2326 * @param rd the record data (PKEY) for the name
2327 * @param signature the signature for the record data
2330 process_zone_to_name_zkey(void *cls,
2331 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
2332 struct GNUNET_TIME_Absolute expire,
2334 unsigned int rd_len,
2335 const struct GNUNET_NAMESTORE_RecordData *rd,
2336 const struct GNUNET_CRYPTO_RsaSignature *signature)
2338 struct ResolverHandle *rh = cls;
2339 struct NameShortenHandle *nsh = rh->proc_cls;
2340 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
2341 char new_name[MAX_DNS_NAME_LENGTH];
2343 /* zkey not in our zone */
2347 * In this case we have not given this PKEY a name (yet)
2348 * It is either just not in our zone or not even cached
2349 * Since we do not know at this point we will not try to shorten
2350 * because PKEY import will happen if the user follows the zkey
2353 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
2355 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2356 "No name found for zkey %s returning verbatim!\n", enc);
2357 if (strcmp(rh->name, "") != 0)
2358 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
2359 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
2361 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2362 enc, GNUNET_GNS_TLD_ZKEY);
2363 nsh->proc(nsh->proc_cls, new_name);
2365 free_resolver_handle(rh);
2369 if (strcmp(rh->name, "") != 0)
2370 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2373 strcpy(new_name, name);
2375 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2376 "Continue shorten for %s!\n", new_name);
2378 strcpy(rh->name, new_name);
2380 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2381 rh->authority_chain_tail = rh->authority_chain_head;
2382 rh->authority_chain_head->zone = rh->authority;
2385 /* Start delegation resolution in our namestore */
2386 resolve_delegation_ns(rh);
2390 * Shorten api from resolver
2392 * @param zone the zone to use
2393 * @param name the name to shorten
2394 * @param key optional private key for background lookups and PSEU import
2395 * @param proc the processor to call with result
2396 * @param proc_cls closure to pass to proc
2399 gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode zone,
2401 struct GNUNET_CRYPTO_RsaPrivateKey *key,
2402 ShortenResultProcessor proc,
2405 struct ResolverHandle *rh;
2406 struct NameShortenHandle *nsh;
2407 char string_hash[MAX_DNS_LABEL_LENGTH];
2408 struct GNUNET_CRYPTO_ShortHashCode zkey;
2409 uint8_t* normalized_zkey;
2411 char nzkey[MAX_DNS_LABEL_LENGTH];
2414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2415 "Starting shorten for %s!\n", name);
2417 if (is_canonical((char*)name))
2419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2420 "%s is canonical. Returning verbatim\n", name);
2421 proc(proc_cls, name);
2425 nsh = GNUNET_malloc(sizeof (struct NameShortenHandle));
2428 nsh->proc_cls = proc_cls;
2430 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2431 rh->authority = zone;
2433 rh->proc = &handle_delegation_ns_shorten;
2436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2437 "Checking for TLD...\n");
2438 if (is_zkey_tld(name) == GNUNET_YES)
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2443 * This is a zkey tld
2444 * build hash and use as initial authority
2448 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
2449 memcpy(rh->name, name,
2450 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
2451 pop_tld(rh->name, string_hash);
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2454 "ZKEY is %s!\n", string_hash);
2456 normalized_zkey = u8_toupper ((uint8_t*)string_hash, strlen ((char *) string_hash),
2457 NULL, UNINORM_NFD, NULL, &normal_len);
2459 memcpy(nzkey, normalized_zkey, normal_len);
2460 nzkey[normal_len] = '\0';
2461 free(normalized_zkey);
2463 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
2466 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2467 "Cannot convert ZKEY %s to hash!\n", nzkey);
2470 proc(proc_cls, name);
2474 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2477 &process_zone_to_name_zkey,
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2487 * Presumably GNUNET tld
2490 strlen(name)-strlen(GNUNET_GNS_TLD));
2491 memcpy(rh->name, name,
2492 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2495 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2496 rh->authority_chain_tail = rh->authority_chain_head;
2497 rh->authority_chain_head->zone = zone;
2500 /* Start delegation resolution in our namestore */
2501 resolve_delegation_ns(rh);
2504 /*********** END NAME SHORTEN ********************/
2508 * Process result from namestore delegation lookup
2509 * for get authority operation
2511 * @param cls the client get auth handle
2512 * @param rh the resolver handle
2513 * @param rd_count number of results (0)
2514 * @param rd data (NULL)
2517 handle_delegation_result_ns_get_auth(void* cls,
2518 struct ResolverHandle *rh,
2520 const struct GNUNET_NAMESTORE_RecordData *rd)
2522 struct GetNameAuthorityHandle* nah;
2523 char result[MAX_DNS_NAME_LENGTH];
2526 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
2529 * At this point rh->name contains the part of the name
2530 * that we do not have a PKEY in our namestore to resolve.
2531 * The authority chain in the resolver handle is now
2532 * useful to backtrack if needed
2535 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2536 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2538 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2539 "Building response!\n");
2540 if (is_canonical(rh->name))
2543 * We successfully resolved the authority in the ns
2544 * FIXME for our purposes this is fine
2545 * but maybe we want to have an api that also looks
2546 * into the dht (i.e. option in message)
2548 if (strlen(rh->name) > strlen(nah->name))
2550 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2551 "Record name longer than original lookup name... odd!\n");
2555 answer_len = strlen(nah->name) - strlen(rh->name)
2556 + strlen(GNUNET_GNS_TLD) + 1;
2557 memset(result, 0, answer_len);
2558 strcpy(result, nah->name + strlen(rh->name) + 1);
2560 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2561 "Got authority result %s\n", result);
2563 nah->proc(nah->proc_cls, result);
2565 free_resolver_handle(rh);
2569 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2570 "Unable to resolve authority for remaining %s!\n", rh->name);
2571 nah->proc(nah->proc_cls, "");
2573 free_resolver_handle(rh);
2581 * Tries to resolve the authority for name
2584 * @param zone the root zone to look up for
2585 * @param name the name to lookup up
2586 * @param proc the processor to call when finished
2587 * @param proc_cls the closure to pass to the processor
2590 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
2592 GetAuthorityResultProcessor proc,
2595 struct ResolverHandle *rh;
2596 struct GetNameAuthorityHandle *nah;
2598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2599 "Starting authority resolution for %s!\n", name);
2601 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
2602 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2603 rh->authority = zone;
2605 if (strcmp(GNUNET_GNS_TLD, name) == 0)
2607 strcpy(rh->name, "\0");
2612 strlen(name)-strlen(GNUNET_GNS_TLD));
2613 memcpy(rh->name, name,
2614 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2617 memset(nah->name, 0,
2619 strcpy(nah->name, name);
2621 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2622 rh->authority_chain_tail = rh->authority_chain_head;
2623 rh->authority_chain_head->zone = zone;
2624 rh->proc = &handle_delegation_result_ns_get_auth;
2625 rh->proc_cls = (void*)nah;
2628 nah->proc_cls = proc_cls;
2630 /* Start delegation resolution in our namestore */
2631 resolve_delegation_ns(rh);
2635 /******** END GET AUTHORITY *************/
2637 /* end of gnunet-service-gns_resolver.c */