2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
24 * @file gns/gnunet-service-gns_resolver.c
25 * @brief GNUnet GNS resolver logic
26 * @author Martin Schanzenbach
29 #include "gnunet_util_lib.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_dns_service.h"
32 #include "gnunet_dht_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_dns_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "gnunet_gns_service.h"
37 #include "block_gns.h"
39 #include "gnunet-service-gns_resolver.h"
41 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
42 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
43 #define DHT_GNS_REPLICATION_LEVEL 5
44 #define MAX_DNS_LABEL_LENGTH 63
48 * Our handle to the namestore service
50 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
53 * Resolver handle to the dht
55 static struct GNUNET_DHT_Handle *dht_handle;
58 * Namestore calls this function if we have record for this name.
59 * (or with rd_count=0 to indicate no matches)
61 * @param cls the pending query
62 * @param key the key of the zone we did the lookup
63 * @param expiration expiration date of the namestore entry
64 * @param name the name for which we need an authority
65 * @param rd_count the number of records with 'name'
66 * @param rd the record data
67 * @param signature the signature of the authority for the record data
70 process_pseu_lookup_ns(void* cls,
71 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
72 struct GNUNET_TIME_Absolute expiration,
73 const char *name, unsigned int rd_count,
74 const struct GNUNET_NAMESTORE_RecordData *rd,
75 const struct GNUNET_CRYPTO_RsaSignature *signature)
77 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
78 struct GNUNET_NAMESTORE_RecordData new_pkey;
82 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
83 "Name %s already taken in NS!\n", name);
84 if (0 == strcmp(gph->name, name))
86 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
87 "Intelligent replacement not implemented\n", name);
92 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
93 "Trying delegated name %s\n", gph->name);
94 memcpy(gph->new_name, gph->name, strlen(gph->name)+1);
95 GNUNET_NAMESTORE_lookup_record(namestore_handle,
98 GNUNET_GNS_RECORD_PSEU,
99 &process_pseu_lookup_ns,
105 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
106 "Name %s not taken in NS! Adding\n", gph->new_name);
108 new_pkey.expiration = GNUNET_TIME_absolute_get_forever ();
109 new_pkey.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
110 new_pkey.data = &gph->new_zone;
111 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
112 GNUNET_NAMESTORE_record_create (namestore_handle,
123 * process result of a dht pseu lookup
125 * @param gph the handle
126 * @param name the pseu result or NULL
129 process_pseu_result(struct GetPseuAuthorityHandle* gph, char* name)
133 memcpy(gph->new_name, name, strlen(gph->name)+1);
137 memcpy(gph->new_name, name, strlen(name)+1);
140 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
141 "Checking %s for collision in NS\n", gph->new_name);
144 * Check for collision
146 GNUNET_NAMESTORE_lookup_record(namestore_handle,
149 GNUNET_GNS_RECORD_PSEU,
150 &process_pseu_lookup_ns,
155 * Handle timeout for dht request
157 * @param cls the request handle as closure
158 * @param tc the task context
161 handle_auth_discovery_timeout(void *cls,
162 const struct GNUNET_SCHEDULER_TaskContext *tc)
164 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
166 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
167 "dht lookup for query PSEU timed out.\n");
168 GNUNET_DHT_get_stop (gph->get_handle);
169 process_pseu_result(gph, NULL);
173 * Function called when we find a PSEU entry in the DHT
175 * @param cls the request handle
176 * @param exp lifetime
177 * @param key the key the record was stored under
178 * @param get_path get path
179 * @param get_path_length get path length
180 * @param put_path put path
181 * @param put_path_length put path length
182 * @param type the block type
183 * @param size the size of the record
184 * @param data the record data
187 process_auth_discovery_dht_result(void* cls,
188 struct GNUNET_TIME_Absolute exp,
189 const GNUNET_HashCode * key,
190 const struct GNUNET_PeerIdentity *get_path,
191 unsigned int get_path_length,
192 const struct GNUNET_PeerIdentity *put_path,
193 unsigned int put_path_length,
194 enum GNUNET_BLOCK_Type type,
195 size_t size, const void *data)
197 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
198 struct GNSNameRecordBlock *nrb;
199 char* rd_data = (char*)data;
205 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
209 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "got dht result null!\n", size);
215 nrb = (struct GNSNameRecordBlock*)data;
217 /* stop lookup and timeout task */
218 GNUNET_DHT_get_stop (gph->get_handle);
219 GNUNET_SCHEDULER_cancel(gph->dht_timeout);
221 gph->get_handle = NULL;
223 nrb = (struct GNSNameRecordBlock*)data;
225 name = (char*)&nrb[1];
226 num_records = ntohl(nrb->rd_count);
228 struct GNUNET_NAMESTORE_RecordData rd[num_records];
230 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
231 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
233 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
238 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
244 for (i=0; i<num_records; i++)
246 if ((strcmp(name, "+") == 0) &&
247 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
250 process_pseu_result(gph, (char*)rd[i].data);
256 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "no pseu in dht!\n");
257 process_pseu_result(gph, NULL);
261 * Callback called by namestore for a zone to name
264 * @param cls the closure
265 * @param zone_key the zone we queried
266 * @param expire the expiration time of the name
267 * @param name the name found or NULL
268 * @param rd_len number of records for the name
269 * @param rd the record data (PKEY) for the name
270 * @param signature the signature for the record data
273 process_zone_to_name_discover(void *cls,
274 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
275 struct GNUNET_TIME_Absolute expire,
278 const struct GNUNET_NAMESTORE_RecordData *rd,
279 const struct GNUNET_CRYPTO_RsaSignature *signature)
281 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
283 /* we found a match in our own zone */
286 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
287 "name for zone in our root %d\n", strlen(name));
297 struct GNUNET_CRYPTO_ShortHashCode name_hash;
298 GNUNET_HashCode lookup_key;
299 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
300 GNUNET_HashCode name_hash_double;
301 GNUNET_HashCode zone_hash_double;
303 GNUNET_CRYPTO_short_hash("+", strlen("+"), &name_hash);
304 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
305 GNUNET_CRYPTO_short_hash_double (&gph->new_zone, &zone_hash_double);
306 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
307 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
309 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
310 "starting dht lookup for %s with key: %s\n",
311 "+", (char*)&lookup_key_string);
313 gph->dht_timeout = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
314 &handle_auth_discovery_timeout, gph);
316 xquery = htonl(GNUNET_GNS_RECORD_PSEU);
318 gph->get_handle = GNUNET_DHT_get_start(dht_handle,
319 DHT_OPERATION_TIMEOUT,
320 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
322 DHT_GNS_REPLICATION_LEVEL,
326 &process_auth_discovery_dht_result,
334 * Callback for new authories
336 * @param name the name given by delegation
337 * @param zone the authority
338 * @param the private key of our authority
340 static void process_discovered_authority(char* name, struct GNUNET_CRYPTO_ShortHashCode zone,
341 struct GNUNET_CRYPTO_ShortHashCode our_zone,
342 struct GNUNET_CRYPTO_RsaPrivateKey *key)
344 struct GetPseuAuthorityHandle *gph;
347 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New authority %s discovered\n",
350 gph = GNUNET_malloc(sizeof(struct GetPseuAuthorityHandle));
351 namelen = strlen(name) + 1;
352 memcpy(gph->name, name, namelen);
354 gph->new_zone = zone;
355 gph->zone = our_zone;
358 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
361 &process_zone_to_name_discover,
367 * Initialize the resolver
369 * @param nh the namestore handle
370 * @param dh the dht handle
371 * @return GNUNET_OK on success
374 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
375 struct GNUNET_DHT_Handle *dh)
377 namestore_handle = nh;
379 if ((namestore_handle != NULL) && (dht_handle != NULL))
383 return GNUNET_SYSERR;
388 * Helper function to free resolver handle
390 * @rh the handle to free
393 free_resolver_handle(struct ResolverHandle* rh)
395 struct AuthorityChain *ac;
396 struct AuthorityChain *ac_next;
401 ac = rh->authority_chain_head;
414 * Callback when record data is put into namestore
416 * @param cls the closure
417 * @param success GNUNET_OK on success
418 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
421 on_namestore_record_put_result(void *cls,
425 if (GNUNET_NO == success)
427 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
430 else if (GNUNET_YES == success)
432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
433 "records successfully put in namestore\n");
437 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
438 "Error putting records into namestore: %s\n", emsg);
443 * Handle timeout for DHT requests
445 * @param cls the request handle as closure
446 * @param tc the task context
449 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
451 struct ResolverHandle *rh = cls;
453 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
454 "dht lookup for query %s timed out.\n",
457 GNUNET_DHT_get_stop (rh->get_handle);
458 rh->proc(rh->proc_cls, rh, 0, NULL);
463 * Function called when we get a result from the dht
464 * for our record query
466 * @param cls the request handle
467 * @param exp lifetime
468 * @param key the key the record was stored under
469 * @param get_path get path
470 * @param get_path_length get path length
471 * @param put_path put path
472 * @param put_path_length put path length
473 * @param type the block type
474 * @param size the size of the record
475 * @param data the record data
478 process_record_result_dht(void* cls,
479 struct GNUNET_TIME_Absolute exp,
480 const GNUNET_HashCode * key,
481 const struct GNUNET_PeerIdentity *get_path,
482 unsigned int get_path_length,
483 const struct GNUNET_PeerIdentity *put_path,
484 unsigned int put_path_length,
485 enum GNUNET_BLOCK_Type type,
486 size_t size, const void *data)
488 struct ResolverHandle *rh;
489 struct RecordLookupHandle *rlh;
490 struct GNSNameRecordBlock *nrb;
491 uint32_t num_records;
493 char* rd_data = (char*)data;
497 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
502 //FIXME maybe check expiration here, check block type
504 rh = (struct ResolverHandle *)cls;
505 rlh = (struct RecordLookupHandle *) rh->proc_cls;
506 nrb = (struct GNSNameRecordBlock*)data;
508 /* stop lookup and timeout task */
509 GNUNET_DHT_get_stop (rh->get_handle);
510 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
512 rh->get_handle = NULL;
513 name = (char*)&nrb[1];
514 num_records = ntohl(nrb->rd_count);
516 struct GNUNET_NAMESTORE_RecordData rd[num_records];
518 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
519 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
521 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
526 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
530 for (i=0; i<num_records; i++)
532 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
533 "Got name: %s (wanted %s)\n", name, rh->name);
534 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
537 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
538 "Got data length: %d\n", rd[i].data_size);
539 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
540 "Got flag %d\n", rd[i].flags);
542 if ((strcmp(name, rh->name) == 0) &&
543 (rd[i].record_type == rlh->record_type))
551 * FIXME check pubkey against existing key in namestore?
552 * https://gnunet.org/bugs/view.php?id=2179
555 /* Save to namestore */
556 GNUNET_NAMESTORE_record_put (namestore_handle,
563 &on_namestore_record_put_result, //cont
567 rh->proc(rh->proc_cls, rh, num_records, rd);
569 rh->proc(rh->proc_cls, rh, 0, NULL);
576 * Start DHT lookup for a (name -> query->record_type) record in
577 * rh->authority's zone
579 * @param rh the pending gns query context
582 resolve_record_dht(struct ResolverHandle *rh)
585 struct GNUNET_CRYPTO_ShortHashCode name_hash;
586 GNUNET_HashCode lookup_key;
587 GNUNET_HashCode name_hash_double;
588 GNUNET_HashCode zone_hash_double;
589 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
590 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
592 GNUNET_CRYPTO_short_hash(rh->name, strlen(rh->name), &name_hash);
593 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
594 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
595 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
596 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
598 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
599 "starting dht lookup for %s with key: %s\n",
600 rh->name, (char*)&lookup_key_string);
602 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
603 &dht_lookup_timeout, rh);
605 xquery = htonl(rlh->record_type);
606 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
607 DHT_OPERATION_TIMEOUT,
608 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
610 DHT_GNS_REPLICATION_LEVEL,
614 &process_record_result_dht,
621 * Namestore calls this function if we have record for this name.
622 * (or with rd_count=0 to indicate no matches)
624 * @param cls the pending query
625 * @param key the key of the zone we did the lookup
626 * @param expiration expiration date of the namestore entry
627 * @param name the name for which we need an authority
628 * @param rd_count the number of records with 'name'
629 * @param rd the record data
630 * @param signature the signature of the authority for the record data
633 process_record_result_ns(void* cls,
634 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
635 struct GNUNET_TIME_Absolute expiration,
636 const char *name, unsigned int rd_count,
637 const struct GNUNET_NAMESTORE_RecordData *rd,
638 const struct GNUNET_CRYPTO_RsaSignature *signature)
640 struct ResolverHandle *rh;
641 struct RecordLookupHandle *rlh;
642 struct GNUNET_TIME_Relative remaining_time;
643 struct GNUNET_CRYPTO_ShortHashCode zone;
645 rh = (struct ResolverHandle *) cls;
646 rlh = (struct RecordLookupHandle *)rh->proc_cls;
647 GNUNET_CRYPTO_short_hash(key,
648 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
650 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
656 rh->status |= EXISTS;
659 if (remaining_time.rel_value == 0)
661 rh->status |= EXPIRED;
667 * Lookup terminated and no results
669 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
670 "Namestore lookup for %s terminated without results\n", name);
672 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
673 "Record %s unknown in namestore\n",
676 * Our zone and no result? Cannot resolve TT
678 rh->proc(rh->proc_cls, rh, 0, NULL);
685 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
686 "Processing additional result %s from namestore\n", name);
688 for (i=0; i<rd_count;i++)
691 if (rd[i].record_type != rlh->record_type)
694 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
697 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
698 "This record is expired. Skipping\n");
709 if (rh->answered == 0)
711 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
712 "No answers found. This is odd!\n");
713 rh->proc(rh->proc_cls, rh, 0, NULL);
717 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
720 rh->proc(rh->proc_cls, rh, rd_count, rd);
726 * The final phase of resolution.
727 * rh->name is a name that is canonical and we do not have a delegation.
728 * Query namestore for this record
730 * @param rh the pending lookup
733 resolve_record_ns(struct ResolverHandle *rh)
735 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
738 * Try to resolve this record in our namestore.
739 * The name to resolve is now in rh->authority_name
740 * since we tried to resolve it to an authority
743 GNUNET_NAMESTORE_lookup_record(namestore_handle,
747 &process_record_result_ns,
753 * Handle timeout for DHT requests
755 * @param cls the request handle as closure
756 * @param tc the task context
759 dht_authority_lookup_timeout(void *cls,
760 const struct GNUNET_SCHEDULER_TaskContext *tc)
762 struct ResolverHandle *rh = cls;
764 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
765 "dht lookup for query %s timed out.\n",
768 GNUNET_DHT_get_stop (rh->get_handle);
769 if (strcmp(rh->name, "") == 0)
772 * promote authority back to name and try to resolve record
774 strcpy(rh->name, rh->authority_name);
776 rh->proc(rh->proc_cls, rh, 0, NULL);
780 static void resolve_delegation_dht(struct ResolverHandle *rh);
783 * Function called when we get a result from the dht
784 * for our query. Recursively tries to resolve authorities
787 * @param cls the request handle
788 * @param exp lifetime
789 * @param key the key the record was stored under
790 * @param get_path get path
791 * @param get_path_length get path length
792 * @param put_path put path
793 * @param put_path_length put path length
794 * @param type the block type
795 * @param size the size of the record
796 * @param data the record data
799 process_delegation_result_dht(void* cls,
800 struct GNUNET_TIME_Absolute exp,
801 const GNUNET_HashCode * key,
802 const struct GNUNET_PeerIdentity *get_path,
803 unsigned int get_path_length,
804 const struct GNUNET_PeerIdentity *put_path,
805 unsigned int put_path_length,
806 enum GNUNET_BLOCK_Type type,
807 size_t size, const void *data)
809 struct ResolverHandle *rh;
810 struct GNSNameRecordBlock *nrb;
811 uint32_t num_records;
813 char* rd_data = (char*) data;
816 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
817 GNUNET_HashCode zone_hash_double, name_hash_double;
819 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got DHT result\n");
824 //FIXME check expiration?
826 rh = (struct ResolverHandle *)cls;
827 nrb = (struct GNSNameRecordBlock*)data;
829 /* stop dht lookup and timeout task */
830 GNUNET_DHT_get_stop (rh->get_handle);
831 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
833 rh->get_handle = NULL;
834 num_records = ntohl(nrb->rd_count);
835 name = (char*)&nrb[1];
837 struct GNUNET_NAMESTORE_RecordData rd[num_records];
839 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
840 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
842 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
847 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
851 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
852 "Got name: %s (wanted %s)\n", name, rh->authority_name);
853 for (i=0; i<num_records; i++)
856 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
857 "Got name: %s (wanted %s)\n", name, rh->authority_name);
858 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
859 "Got type: %d (wanted %d)\n",
860 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
861 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
862 "Got data length: %d\n", rd[i].data_size);
863 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
864 "Got flag %d\n", rd[i].flags);
866 if ((strcmp(name, rh->authority_name) == 0) &&
867 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
869 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
871 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
872 struct AuthorityChain *auth =
873 GNUNET_malloc(sizeof(struct AuthorityChain));
874 auth->zone = rh->authority;
875 memset(auth->name, 0, strlen(rh->authority_name)+1);
876 strcpy(auth->name, rh->authority_name);
877 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
878 rh->authority_chain_tail,
881 /** call process new authority */
883 process_discovered_authority(name, auth->zone,
884 rh->authority_chain_tail->zone,
891 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
892 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
893 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
894 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
896 /* Save to namestore */
897 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
900 GNUNET_NAMESTORE_record_put (namestore_handle,
907 &on_namestore_record_put_result, //cont
917 * FIXME in this case. should we ask namestore again?
919 if (strcmp(rh->name, "") == 0)
920 rh->proc(rh->proc_cls, rh, 0, NULL);
922 resolve_delegation_dht(rh);
927 * No pkey but name exists
929 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DHT authority lookup found no match!\n");
930 rh->proc(rh->proc_cls, rh, 0, NULL);
937 finish_lookup(struct ResolverHandle *rh,
938 struct RecordLookupHandle* rlh,
939 unsigned int rd_count,
940 const struct GNUNET_NAMESTORE_RecordData *rd)
944 char new_s_value[256];
945 char new_mx_value[sizeof(struct GNUNET_DNSPARSER_MxRecord)+256];
947 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
950 struct GNUNET_DNSPARSER_MxRecord *mx;
953 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
955 for (i = 0; i < rd_count; i++)
958 if (rd[i].record_type != GNUNET_GNS_RECORD_TYPE_NS &&
959 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_CNAME &&
960 rd[i].record_type != GNUNET_GNS_RECORD_MX)
962 p_rd[i].data = rd[i].data;
967 * for all those records we 'should'
968 * also try to resolve the A/AAAA records (RFC1035)
971 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
973 mx = (struct GNUNET_DNSPARSER_MxRecord*)rd[i].data;
974 s_value = (char*)&mx[1];
978 s_value = (char*)rd[i].data;
981 s_len = strlen(s_value)+1;
985 /* no postprocessing */
986 p_rd[i].data = rd[i].data;
990 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
991 "Postprocessing %s\n", s_value);
993 if (0 == strcmp(s_value+s_len-3, ".+"))
995 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
996 "Expanding .+ in %s\n", s_value);
997 if (strcmp(rh->name, "+") == 0)
1003 trailer = rlh->name+strlen(rh->name)+1;
1005 memset(new_s_value, 0, s_len+strlen(trailer)+strlen(GNUNET_GNS_TLD));
1006 strcpy(new_s_value, s_value);
1007 pos = new_s_value+s_len-2;
1008 strcpy(pos, trailer);
1009 pos += strlen(trailer);
1010 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
1013 p_rd[i].data_size = sizeof(struct GNUNET_DNSPARSER_MxRecord)
1014 +strlen(new_s_value)+1;
1016 p_rd[i].data = new_mx_value;
1017 mx = (struct GNUNET_DNSPARSER_MxRecord*)p_rd[i].data;
1019 ((struct GNUNET_DNSPARSER_MxRecord*)rd[i].data)->preference;
1020 memcpy((char*)&mx[1], new_s_value, strlen(new_s_value)+1);
1021 mx->mxhost = (char*)&mx[1];
1025 p_rd[i].data = new_s_value;
1026 p_rd[i].data_size = strlen(new_s_value)+1;
1028 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1029 "Expanded to %s\n", new_s_value);
1033 rlh->proc(rlh->proc_cls, rd_count, p_rd);
1039 * Process DHT lookup result for record.
1041 * @param cls the closure
1042 * @param rh resolver handle
1043 * @param rd_count number of results
1044 * @param rd record data
1047 handle_record_dht(void* cls, struct ResolverHandle *rh,
1048 unsigned int rd_count,
1049 const struct GNUNET_NAMESTORE_RecordData *rd)
1051 struct RecordLookupHandle* rlh;
1053 rlh = (struct RecordLookupHandle*)cls;
1056 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1057 "No records for %s found in DHT. Aborting\n",
1059 /* give up, cannot resolve */
1060 finish_lookup(rh, rlh, 0, NULL);
1061 free_resolver_handle(rh);
1065 /* results found yay */
1066 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1067 "Record resolved from DHT!");
1069 finish_lookup(rh, rlh, rd_count, rd);
1070 free_resolver_handle(rh);
1076 * Process namestore lookup result for record.
1078 * @param cls the closure
1079 * @param rh resolver handle
1080 * @param rd_count number of results
1081 * @param rd record data
1084 handle_record_ns(void* cls, struct ResolverHandle *rh,
1085 unsigned int rd_count,
1086 const struct GNUNET_NAMESTORE_RecordData *rd)
1088 struct RecordLookupHandle* rlh;
1089 rlh = (struct RecordLookupHandle*) cls;
1092 /* ns entry expired and not ours. try dht */
1093 if (rh->status & (EXPIRED | !EXISTS) &&
1094 GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1095 &rh->authority_chain_tail->zone))
1097 rh->proc = &handle_record_dht;
1098 resolve_record_dht(rh);
1101 /* give up, cannot resolve */
1102 finish_lookup(rh, rlh, 0, NULL);
1103 free_resolver_handle(rh);
1107 /* results found yay */
1108 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1109 "Record resolved from namestore!");
1111 finish_lookup(rh, rlh, rd_count, rd);
1112 free_resolver_handle(rh);
1118 * Determine if this name is canonical.
1120 * a.b.gnunet = not canonical
1123 * @param name the name to test
1124 * @return 1 if canonical
1127 is_canonical(char* name)
1129 uint32_t len = strlen(name);
1132 for (i=0; i<len; i++)
1134 if (*(name+i) == '.')
1141 * Move one level up in the domain hierarchy and return the
1142 * passed top level domain.
1144 * @param name the domain
1145 * @param dest the destination where the tld will be put
1148 pop_tld(char* name, char* dest)
1152 if (is_canonical(name))
1159 for (len = strlen(name); len > 0; len--)
1161 if (*(name+len) == '.')
1171 strcpy(dest, (name+len+1));
1175 * Checks if name is in tld
1177 * @param name the name to check
1178 * @return GNUNET_YES or GNUNET_NO
1181 is_tld(const char* name, const char* tld)
1185 if (strlen(name) <= strlen(tld))
1190 offset = strlen(name)-strlen(tld);
1191 if (strcmp(name+offset, tld) != 0)
1193 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1194 "%s is not in .%s TLD\n", name, tld);
1201 * DHT resolution for delegation finished. Processing result.
1203 * @param cls the closure
1204 * @param rh resolver handle
1205 * @param rd_count number of results (always 0)
1206 * @param rd record data (always NULL)
1209 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
1210 unsigned int rd_count,
1211 const struct GNUNET_NAMESTORE_RecordData *rd)
1213 struct RecordLookupHandle* rlh;
1214 rlh = (struct RecordLookupHandle*) cls;
1216 if (strcmp(rh->name, "") == 0)
1218 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
1220 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1221 "Resolved queried PKEY via DHT.\n");
1222 finish_lookup(rh, rlh, rd_count, rd);
1223 free_resolver_handle(rh);
1226 /* We resolved full name for delegation. resolving record */
1227 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1228 "Resolved full name for delegation via DHT. resolving record '' in ns\n");
1229 strcpy(rh->name, "+\0");
1230 rh->proc = &handle_record_ns;
1231 resolve_record_ns(rh);
1236 * we still have some left
1238 if (is_canonical(rh->name))
1240 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1241 "Resolving canonical record %s in ns\n", rh->name);
1242 rh->proc = &handle_record_ns;
1243 resolve_record_ns(rh);
1246 /* give up, cannot resolve */
1247 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1248 "Cannot fully resolve delegation for %s via DHT!\n",
1250 finish_lookup(rh, rlh, 0, NULL);
1251 free_resolver_handle(rh);
1256 * Start DHT lookup for a name -> PKEY (compare NS) record in
1257 * rh->authority's zone
1259 * @param rh the pending gns query
1262 resolve_delegation_dht(struct ResolverHandle *rh)
1265 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1266 GNUNET_HashCode name_hash_double;
1267 GNUNET_HashCode zone_hash_double;
1268 GNUNET_HashCode lookup_key;
1270 GNUNET_CRYPTO_short_hash(rh->authority_name,
1271 strlen(rh->authority_name),
1273 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1274 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
1275 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
1277 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1278 &dht_authority_lookup_timeout,
1281 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1283 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1284 DHT_OPERATION_TIMEOUT,
1285 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1287 DHT_GNS_REPLICATION_LEVEL,
1291 &process_delegation_result_dht,
1298 * Namestore resolution for delegation finished. Processing result.
1300 * @param cls the closure
1301 * @param rh resolver handle
1302 * @param rd_count number of results (always 0)
1303 * @param rd record data (always NULL)
1306 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1307 unsigned int rd_count,
1308 const struct GNUNET_NAMESTORE_RecordData *rd)
1310 struct RecordLookupHandle* rlh;
1311 rlh = (struct RecordLookupHandle*) cls;
1313 if (strcmp(rh->name, "") == 0)
1315 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
1317 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1318 "Resolved queried PKEY in NS.\n");
1319 finish_lookup(rh, rlh, rd_count, rd);
1320 free_resolver_handle(rh);
1323 /* We resolved full name for delegation. resolving record */
1324 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1325 "Resolved full name for delegation. resolving record '+'\n");
1326 strcpy(rh->name, "+\0");
1327 rh->proc = &handle_record_ns;
1328 resolve_record_ns(rh);
1333 * we still have some left
1334 * check if authority in ns is fresh
1336 * or we are authority
1338 if ((rh->status & (EXISTS | !EXPIRED)) ||
1339 !GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1340 &rh->authority_chain_tail->zone))
1342 if (is_canonical(rh->name))
1344 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1345 "Resolving canonical record %s\n", rh->name);
1346 rh->proc = &handle_record_ns;
1347 resolve_record_ns(rh);
1351 /* give up, cannot resolve */
1352 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1353 "Cannot fully resolve delegation for %s!\n",
1355 rlh->proc(rlh->proc_cls, 0, NULL);
1360 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1361 "Trying to resolve delegation for %s via DHT\n",
1363 rh->proc = &handle_delegation_dht;
1364 resolve_delegation_dht(rh);
1368 static void resolve_delegation_ns(struct ResolverHandle *rh);
1372 * This is a callback function that should give us only PKEY
1373 * records. Used to query the namestore for the authority (PKEY)
1374 * for 'name'. It will recursively try to resolve the
1375 * authority for a given name from the namestore.
1377 * @param cls the pending query
1378 * @param key the key of the zone we did the lookup
1379 * @param expiration expiration date of the record data set in the namestore
1380 * @param name the name for which we need an authority
1381 * @param rd_count the number of records with 'name'
1382 * @param rd the record data
1383 * @param signature the signature of the authority for the record data
1386 process_delegation_result_ns(void* cls,
1387 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1388 struct GNUNET_TIME_Absolute expiration,
1390 unsigned int rd_count,
1391 const struct GNUNET_NAMESTORE_RecordData *rd,
1392 const struct GNUNET_CRYPTO_RsaSignature *signature)
1394 struct ResolverHandle *rh;
1395 struct GNUNET_TIME_Relative remaining_time;
1396 struct GNUNET_CRYPTO_ShortHashCode zone;
1397 char new_name[MAX_DNS_NAME_LENGTH];
1399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %d records from authority lookup\n",
1402 rh = (struct ResolverHandle *)cls;
1403 GNUNET_CRYPTO_short_hash(key,
1404 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1406 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1412 rh->status |= EXISTS;
1415 if (remaining_time.rel_value == 0)
1417 rh->status |= EXPIRED;
1421 * No authority found in namestore.
1426 * We did not find an authority in the namestore
1431 * Promote this authority back to a name maybe it is
1434 if (strcmp(rh->name, "") == 0)
1436 /* simply promote back */
1437 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1438 "Promoting %s back to name\n", rh->authority_name);
1439 strcpy(rh->name, rh->authority_name);
1443 /* add back to existing name */
1444 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1445 "Adding %s back to %s\n",
1446 rh->authority_name, rh->name);
1447 memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1448 strcpy(new_name, rh->name);
1449 strcpy(new_name+strlen(new_name)+1, ".");
1450 strcpy(new_name+strlen(new_name)+2, rh->authority_name);
1451 strcpy(rh->name, new_name);
1453 rh->proc(rh->proc_cls, rh, 0, NULL);
1458 * We found an authority that may be able to help us
1459 * move on with query
1460 * Note only 1 pkey should have been returned.. anything else would be strange
1463 for (i=0; i<rd_count;i++)
1466 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1469 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1472 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
1473 if (remaining_time.rel_value == 0)
1475 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1476 "This dht entry is expired.\n");
1477 rh->authority_chain_head->fresh = 0;
1478 rh->proc(rh->proc_cls, rh, 0, NULL);
1486 * Resolve rest of query with new authority
1488 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1489 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
1490 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1491 auth->zone = rh->authority;
1492 memset(auth->name, 0, strlen(rh->authority_name)+1);
1493 strcpy(auth->name, rh->authority_name);
1494 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1495 rh->authority_chain_tail,
1499 * We are done with PKEY resolution if name is empty
1500 * else resolve again with new authority
1502 if (strcmp(rh->name, "") == 0)
1503 rh->proc(rh->proc_cls, rh, 0, NULL);
1505 resolve_delegation_ns(rh);
1512 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1513 "Authority lookup successful but no PKEY... never get here\n");
1514 rh->proc(rh->proc_cls, rh, 0, NULL);
1519 * Resolve the delegation chain for the request in our namestore
1521 * @param rh the resolver handle
1524 resolve_delegation_ns(struct ResolverHandle *rh)
1526 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1527 "Resolving delegation for %s\n", rh->name);
1528 pop_tld(rh->name, rh->authority_name);
1529 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1532 GNUNET_GNS_RECORD_PKEY,
1533 &process_delegation_result_ns,
1540 * Lookup of a record in a specific zone
1541 * calls lookup result processor on result
1543 * @param zone the root zone
1544 * @param record_type the record type to look up
1545 * @param name the name to look up
1546 * @param proc the processor to call on result
1547 * @param cls the closure to pass to proc
1550 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
1551 uint32_t record_type,
1553 struct GNUNET_CRYPTO_RsaPrivateKey *key,
1554 RecordLookupProcessor proc,
1557 struct ResolverHandle *rh;
1558 struct RecordLookupHandle* rlh;
1559 char string_hash[MAX_DNS_NAME_LENGTH]; //FIXME name len as soon as shorthash
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1562 "Starting resolution for %s (type=%d)!\n",
1566 if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 "%s is canonical and not gnunet -> cannot resolve!\n", name);
1574 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
1575 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
1577 rh->authority = zone;
1581 if (strcmp(GNUNET_GNS_TLD, name) == 0)
1584 * Only 'gnunet' given
1586 strcpy(rh->name, "\0");
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 "Checking for TLD...\n");
1592 if (is_zkey_tld(name) == GNUNET_YES)
1594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1597 * This is a zkey tld
1598 * build hash and use as initial authority
1601 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
1602 memcpy(rh->name, name,
1603 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
1604 pop_tld(rh->name, string_hash);
1606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1607 "ZKEY is %s!\n", string_hash);
1609 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(string_hash,
1612 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1613 "Cannot convert ZKEY %s to hash!\n", string_hash);
1623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1626 * Presumably GNUNET tld
1629 strlen(name)-strlen(GNUNET_GNS_TLD));
1630 memcpy(rh->name, name,
1631 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
1636 * Initialize authority chain
1638 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
1639 rh->authority_chain_head->prev = NULL;
1640 rh->authority_chain_head->next = NULL;
1641 rh->authority_chain_tail = rh->authority_chain_head;
1642 rh->authority_chain_head->zone = rh->authority;
1645 * Copy original query into lookup handle
1647 rlh->record_type = record_type;
1648 memset(rlh->name, 0, strlen(name) + 1);
1649 strcpy(rlh->name, name);
1651 rlh->proc_cls = cls;
1653 rh->proc = &handle_delegation_ns;
1654 resolve_delegation_ns(rh);
1657 /******** END Record Resolver ***********/
1661 * Callback calles by namestore for a zone to name
1664 * @param cls the closure
1665 * @param zone_key the zone we queried
1666 * @param expire the expiration time of the name
1667 * @param name the name found or NULL
1668 * @param rd_len number of records for the name
1669 * @param rd the record data (PKEY) for the name
1670 * @param signature the signature for the record data
1673 process_zone_to_name_shorten(void *cls,
1674 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1675 struct GNUNET_TIME_Absolute expire,
1677 unsigned int rd_len,
1678 const struct GNUNET_NAMESTORE_RecordData *rd,
1679 const struct GNUNET_CRYPTO_RsaSignature *signature)
1681 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
1682 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
1683 struct AuthorityChain *next_authority;
1685 char result[MAX_DNS_NAME_LENGTH];
1686 char next_authority_name[MAX_DNS_LABEL_LENGTH];
1689 /* we found a match in our own zone */
1692 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1693 "result strlen %d\n", strlen(name));
1694 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
1695 memset(result, 0, answer_len);
1696 if (strlen(rh->name) > 0)
1698 strcpy(result, rh->name);
1699 strcpy(result+strlen(rh->name), ".");
1702 strcpy(result+strlen(result), name);
1703 strcpy(result+strlen(result), ".");
1704 strcpy(result+strlen(result), GNUNET_GNS_TLD);
1706 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1707 "Sending shorten result %s\n", result);
1709 nsh->proc(nsh->proc_cls, result);
1711 free_resolver_handle(rh);
1713 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1714 &rh->authority_chain_tail->zone))
1716 /* our zone, just append .gnunet */
1717 answer_len = strlen(rh->name) + strlen(GNUNET_GNS_TLD) + 2;
1718 memset(result, 0, answer_len);
1719 strcpy(result, rh->name);
1720 strcpy(result+strlen(rh->name), ".");
1721 strcpy(result+strlen(rh->name)+1, GNUNET_GNS_TLD);
1723 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1724 "Our zone: Sending name as shorten result %s\n", rh->name);
1726 nsh->proc(nsh->proc_cls, result);
1728 free_resolver_handle(rh);
1734 * continue with next authority
1736 next_authority = rh->authority_chain_head;
1737 // strlen(next_authority->name) + 2);
1738 memset(next_authority_name, 0, strlen(rh->name)+
1739 strlen(next_authority->name) + 2);
1740 strcpy(next_authority_name, rh->name);
1741 strcpy(next_authority_name+strlen(rh->name)+1, ".");
1742 strcpy(next_authority_name+strlen(rh->name)+2, next_authority->name);
1744 strcpy(rh->name, next_authority_name);
1745 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1746 rh->authority_chain_tail,
1749 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1750 &rh->authority_chain_tail->zone,
1751 &rh->authority_chain_head->zone,
1752 &process_zone_to_name_shorten,
1759 * Process result from namestore delegation lookup
1760 * for shorten operation
1762 * @param cls the client shorten handle
1763 * @param rh the resolver handle
1764 * @param rd_count number of results (0)
1765 * @param rd data (NULL)
1768 handle_delegation_ns_shorten(void* cls,
1769 struct ResolverHandle *rh,
1771 const struct GNUNET_NAMESTORE_RecordData *rd)
1773 struct NameShortenHandle *nsh;
1774 char result[MAX_DNS_NAME_LENGTH];
1777 nsh = (struct NameShortenHandle *)cls;
1780 * At this point rh->name contains the part of the name
1781 * that we do not have a PKEY in our namestore to resolve.
1782 * The authority chain in the resolver handle is now
1783 * useful to backtrack if needed
1786 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1787 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
1789 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
1790 &rh->authority_chain_tail->zone) == 0)
1793 * This is our zone append .gnunet unless name is empty
1794 * (it shouldn't be, usually FIXME what happens if we
1795 * shorten to our zone to a "" record??)
1798 answer_len = strlen(rh->name) + strlen(GNUNET_GNS_TLD) + 2;
1799 memset(result, 0, answer_len);
1800 strcpy(result, rh->name);
1801 strcpy(result+strlen(rh->name), ".");
1802 strcpy(result+strlen(rh->name)+1, GNUNET_GNS_TLD);
1804 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1805 "Our zone: Sending name as shorten result %s\n", rh->name);
1807 nsh->proc(nsh->proc_cls, result);
1809 free_resolver_handle(rh);
1813 /* backtrack authorities for names */
1814 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1815 &rh->authority_chain_tail->zone, //ours
1816 &rh->authority_chain_head->zone,
1817 &process_zone_to_name_shorten,
1823 * Shorten api from resolver
1825 * @param zone the zone to use
1826 * @param name the name to shorten
1827 * @param proc the processor to call with result
1828 * @param cls closure to pass to proc
1831 gns_resolver_shorten_name(struct GNUNET_CRYPTO_ShortHashCode zone,
1833 ShortenResultProcessor proc,
1836 struct ResolverHandle *rh;
1837 struct NameShortenHandle *nsh;
1838 char string_hash[MAX_DNS_NAME_LENGTH]; //FIXME LABEL length when shorthash
1840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1841 "Starting shorten for %s!\n", name);
1843 if (is_canonical((char*)name))
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1846 "%s is canonical. Returning verbatim\n", name);
1851 nsh = GNUNET_malloc(sizeof (struct NameShortenHandle));
1855 nsh->proc_cls = cls;
1857 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
1858 rh->authority = zone;
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1861 "Checking for TLD...\n");
1862 if (is_zkey_tld(name) == GNUNET_YES)
1864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1867 * This is a zkey tld
1868 * build hash and use as initial authority
1871 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
1872 memcpy(rh->name, name,
1873 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
1874 pop_tld(rh->name, string_hash);
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1877 "ZKEY is %s!\n", string_hash);
1879 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(string_hash,
1882 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1883 "Cannot convert ZKEY %s to hash!\n", string_hash);
1893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1896 * Presumably GNUNET tld
1899 strlen(name)-strlen(GNUNET_GNS_TLD));
1900 memcpy(rh->name, name,
1901 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
1904 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
1905 rh->authority_chain_tail = rh->authority_chain_head;
1906 rh->authority_chain_head->zone = zone;
1907 rh->proc = &handle_delegation_ns_shorten;
1910 /* Start delegation resolution in our namestore */
1911 resolve_delegation_ns(rh);
1914 /*********** END NAME SHORTEN ********************/
1918 * Process result from namestore delegation lookup
1919 * for get authority operation
1921 * @param cls the client get auth handle
1922 * @param rh the resolver handle
1923 * @param rd_count number of results (0)
1924 * @param rd data (NULL)
1927 handle_delegation_result_ns_get_auth(void* cls,
1928 struct ResolverHandle *rh,
1930 const struct GNUNET_NAMESTORE_RecordData *rd)
1932 struct GetNameAuthorityHandle* nah;
1933 char result[MAX_DNS_NAME_LENGTH];
1936 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
1939 * At this point rh->name contains the part of the name
1940 * that we do not have a PKEY in our namestore to resolve.
1941 * The authority chain in the resolver handle is now
1942 * useful to backtrack if needed
1945 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1946 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
1948 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1949 "Building response!\n");
1950 if (is_canonical(rh->name))
1953 * We successfully resolved the authority in the ns
1954 * FIXME for our purposes this is fine
1955 * but maybe we want to have an api that also looks
1956 * into the dht (i.e. option in message)
1958 if (strlen(rh->name) > strlen(nah->name))
1960 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1961 "Record name longer than original lookup name... odd!\n");
1965 answer_len = strlen(nah->name) - strlen(rh->name)
1966 + strlen(GNUNET_GNS_TLD) + 1;
1967 memset(result, 0, answer_len);
1968 strcpy(result, nah->name + strlen(rh->name) + 1);
1970 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1971 "Got authority result %s\n", result);
1973 nah->proc(nah->proc_cls, result);
1975 free_resolver_handle(rh);
1979 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1980 "Unable to resolve authority for remaining %s!\n", rh->name);
1981 nah->proc(nah->proc_cls, "");
1983 free_resolver_handle(rh);
1991 * Tries to resolve the authority for name
1994 * @param zone the root zone to look up for
1995 * @param name the name to lookup up
1996 * @param proc the processor to call when finished
1997 * @param cls the closure to pass to the processor
2000 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
2002 GetAuthorityResultProcessor proc,
2005 struct ResolverHandle *rh;
2006 struct GetNameAuthorityHandle *nah;
2008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2009 "Starting authority resolution for %s!\n", name);
2011 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
2012 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2013 rh->authority = zone;
2015 if (strcmp(GNUNET_GNS_TLD, name) == 0)
2017 strcpy(rh->name, "\0");
2022 strlen(name)-strlen(GNUNET_GNS_TLD));
2023 memcpy(rh->name, name,
2024 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2027 memset(nah->name, 0,
2029 strcpy(nah->name, name);
2031 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2032 rh->authority_chain_tail = rh->authority_chain_head;
2033 rh->authority_chain_head->zone = zone;
2034 rh->proc = &handle_delegation_result_ns_get_auth;
2035 rh->proc_cls = (void*)nah;
2038 nah->proc_cls = cls;
2040 /* Start delegation resolution in our namestore */
2041 resolve_delegation_ns(rh);
2045 /******** END GET AUTHORITY *************/
2047 /* end of gnunet-service-gns_resolver.c */