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_vpn_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_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 * Our handle to the vpn service
55 static struct GNUNET_VPN_Handle *vpn_handle;
58 * Resolver handle to the dht
60 static struct GNUNET_DHT_Handle *dht_handle;
63 * Heap for parallel DHT lookups
65 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
68 * Maximum amount of parallel queries in background
70 static unsigned long long max_allowed_background_queries;
73 * Wheather or not to ignore pending records
75 static int ignore_pending_records;
80 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
83 * a resolution identifier pool variable
85 * This is a non critical identifier useful for debugging
87 static unsigned long long rid = 0;
91 * Determine if this name is canonical.
93 * a.b.gnunet = not canonical
96 * @param name the name to test
97 * @return 1 if canonical
100 is_canonical(char* name)
102 uint32_t len = strlen(name);
105 for (i=0; i<len; i++)
107 if (*(name+i) == '.')
115 * Callback that shortens authorities
117 * @param gph the handle containing the name to shorten
120 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
124 * Namestore calls this function if we have record for this name.
125 * (or with rd_count=0 to indicate no matches)
127 * @param cls the pending query
128 * @param key the key of the zone we did the lookup
129 * @param expiration expiration date of the namestore entry
130 * @param name the name for which we need an authority
131 * @param rd_count the number of records with 'name'
132 * @param rd the record data
133 * @param signature the signature of the authority for the record data
136 process_pseu_lookup_ns (void* cls,
137 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
138 struct GNUNET_TIME_Absolute expiration,
139 const char *name, unsigned int rd_count,
140 const struct GNUNET_NAMESTORE_RecordData *rd,
141 const struct GNUNET_CRYPTO_RsaSignature *signature)
143 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
144 struct GNUNET_NAMESTORE_RecordData new_pkey;
145 struct AuthorityChain *iter;
149 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
150 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
151 if (0 == strcmp (gph->name, name))
153 if (gph->ahead->next != NULL)
155 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
159 iter = gph->ahead->next;
160 GNUNET_free (gph->ahead);
162 shorten_authority_chain (gph);
170 iter = gph->ahead->next;
171 GNUNET_free (gph->ahead);
173 } while (iter != NULL);
174 GNUNET_CRYPTO_rsa_key_free (gph->key);
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 "GNS_AUTO_PSEU: Trying delegated name %s\n", gph->name);
182 memcpy (gph->test_name, gph->name, strlen (gph->name)+1);
183 GNUNET_NAMESTORE_lookup_record (namestore_handle,
186 GNUNET_NAMESTORE_TYPE_ANY,
187 &process_pseu_lookup_ns,
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
196 new_pkey.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
197 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
198 new_pkey.data = &gph->ahead->zone;
199 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
200 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
201 | GNUNET_NAMESTORE_RF_PRIVATE
202 | GNUNET_NAMESTORE_RF_PENDING;
203 GNUNET_NAMESTORE_record_create (namestore_handle,
211 iter = gph->ahead->next;
212 GNUNET_free (gph->ahead);
214 } while (iter != NULL);
215 GNUNET_CRYPTO_rsa_key_free (gph->key);
221 * process result of a dht pseu lookup
223 * @param gph the handle
224 * @param name the pseu result or NULL
227 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
231 memcpy (gph->test_name, gph->ahead->name, strlen (gph->ahead->name)+1);
235 memcpy (gph->test_name, name, strlen(name)+1);
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
243 * Check for collision
245 GNUNET_NAMESTORE_lookup_record (namestore_handle,
248 GNUNET_NAMESTORE_TYPE_ANY,
249 &process_pseu_lookup_ns,
254 * Handle timeout for dht request
256 * @param cls the request handle as closure
257 * @param tc the task context
260 handle_auth_discovery_timeout(void *cls,
261 const struct GNUNET_SCHEDULER_TaskContext *tc)
263 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
264 struct AuthorityChain *iter;
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
268 GNUNET_DHT_get_stop (gph->get_handle);
269 gph->get_handle = NULL;
271 if (gph->ahead->next != NULL)
273 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
277 iter = gph->ahead->next;
278 GNUNET_free (gph->ahead);
280 shorten_authority_chain (gph);
285 process_pseu_result (gph, NULL);
289 * Function called when we find a PSEU entry in the DHT
291 * @param cls the request handle
292 * @param exp lifetime
293 * @param key the key the record was stored under
294 * @param get_path get path
295 * @param get_path_length get path length
296 * @param put_path put path
297 * @param put_path_length put path length
298 * @param type the block type
299 * @param size the size of the record
300 * @param data the record data
303 process_auth_discovery_dht_result(void* cls,
304 struct GNUNET_TIME_Absolute exp,
305 const struct GNUNET_HashCode * key,
306 const struct GNUNET_PeerIdentity *get_path,
307 unsigned int get_path_length,
308 const struct GNUNET_PeerIdentity *put_path,
309 unsigned int put_path_length,
310 enum GNUNET_BLOCK_Type type,
311 size_t size, const void *data)
313 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
314 struct AuthorityChain *iter;
315 struct GNSNameRecordBlock *nrb;
316 char* rd_data = (char*)data;
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
328 "GNS_GET_AUTH: got dht result null!\n", size);
332 iter = gph->ahead->next;
333 GNUNET_free (gph->ahead);
335 } while (iter != NULL);
336 GNUNET_CRYPTO_rsa_key_free (gph->key);
341 nrb = (struct GNSNameRecordBlock*)data;
343 /* stop lookup and timeout task */
344 GNUNET_DHT_get_stop (gph->get_handle);
345 gph->get_handle = NULL;
346 GNUNET_SCHEDULER_cancel (gph->timeout);
348 gph->get_handle = NULL;
350 nrb = (struct GNSNameRecordBlock*)data;
352 name = (char*)&nrb[1];
353 num_records = ntohl (nrb->rd_count);
355 struct GNUNET_NAMESTORE_RecordData rd[num_records];
357 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
358 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
360 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
365 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
366 "GNS_GET_AUTH: Error deserializing data!\n");
370 for (i=0; i < num_records; i++)
372 if ((strcmp (name, "+") == 0) &&
373 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
376 process_pseu_result (gph, (char*)rd[i].data);
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in dht!\n");
385 if (gph->ahead->next != NULL)
387 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
391 iter = gph->ahead->next;
392 GNUNET_free (gph->ahead);
394 shorten_authority_chain (gph);
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "GNS_GET_AUTH: finished shorten, no results!\n");
400 process_pseu_result (gph, NULL);
404 * Process PSEU discovery for shorten via namestore
406 * @param cls the GetPseuAuthorityHandle
407 * @param key the public key
408 * @param expiration recorddata expiration
409 * @param name the looked up name
410 * @param rd_count number of records in set
411 * @param rd record data
412 * @param signature the signature
415 process_auth_discovery_ns_result(void* cls,
416 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
417 struct GNUNET_TIME_Absolute expiration,
419 unsigned int rd_count,
420 const struct GNUNET_NAMESTORE_RecordData *rd,
421 const struct GNUNET_CRYPTO_RsaSignature *signature)
424 struct GNUNET_CRYPTO_ShortHashCode name_hash;
425 struct GNUNET_HashCode lookup_key;
426 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
427 struct GNUNET_HashCode name_hash_double;
428 struct GNUNET_HashCode zone_hash_double;
430 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
431 struct AuthorityChain *iter;
439 GNUNET_CRYPTO_short_hash ("+", strlen ("+"), &name_hash);
440 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
441 GNUNET_CRYPTO_short_hash_double (&gph->ahead->zone, &zone_hash_double);
442 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
443 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
446 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
447 "+", (char*)&lookup_key_string);
449 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
450 &handle_auth_discovery_timeout, gph);
452 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
454 GNUNET_assert (gph->get_handle == NULL);
456 gph->get_handle = GNUNET_DHT_get_start(dht_handle,
457 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
459 DHT_GNS_REPLICATION_LEVEL,
463 &process_auth_discovery_dht_result,
468 for (i=0; i < rd_count; i++)
470 if ((strcmp (name, "+") == 0) &&
471 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
474 process_pseu_result (gph, (char*)rd[i].data);
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
481 if (gph->ahead->next != NULL)
483 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
487 iter = gph->ahead->next;
488 GNUNET_free (gph->ahead);
490 shorten_authority_chain (gph);
495 process_pseu_result (gph, NULL);
499 * Callback called by namestore for a zone to name
502 * @param cls the closure
503 * @param zone_key the zone we queried
504 * @param expire the expiration time of the name
505 * @param name the name found or NULL
506 * @param rd_len number of records for the name
507 * @param rd the record data (PKEY) for the name
508 * @param signature the signature for the record data
511 process_zone_to_name_discover (void *cls,
512 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
513 struct GNUNET_TIME_Absolute expire,
516 const struct GNUNET_NAMESTORE_RecordData *rd,
517 const struct GNUNET_CRYPTO_RsaSignature *signature)
519 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
520 struct AuthorityChain *iter;
522 /* we found a match in our own zone */
525 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
526 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
531 iter = gph->ahead->next;
532 GNUNET_free (gph->ahead);
534 } while (iter != NULL);
535 GNUNET_CRYPTO_rsa_key_free (gph->key);
541 GNUNET_NAMESTORE_lookup_record (namestore_handle,
544 GNUNET_GNS_RECORD_PSEU,
545 &process_auth_discovery_ns_result,
553 * Callback that shortens authorities
555 * @param name the name given by delegation
556 * @param zone the authority
557 * @param our_zone our local zone
558 * @param key the private key of our authority
561 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
565 "GNS_AUTO_PSEU: New authority %s discovered\n",
568 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
571 &process_zone_to_name_discover,
577 start_shorten (struct AuthorityChain *atail,
578 struct GNUNET_CRYPTO_RsaPrivateKey *key)
580 struct AuthorityChain *new_head = NULL;
581 struct AuthorityChain *new_tail = NULL;
582 struct AuthorityChain *iter;
583 struct AuthorityChain *acopy;
584 struct GetPseuAuthorityHandle *gph;
585 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
586 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
588 /* First copy the authority chain in reverse order */
589 for (iter = atail; iter != NULL; iter = iter->prev)
591 acopy = GNUNET_malloc (sizeof (struct AuthorityChain));
592 memcpy (acopy, iter, sizeof (struct AuthorityChain));
595 GNUNET_CONTAINER_DLL_insert (new_head, new_tail, acopy);
598 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
600 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
601 pb_key = GNUNET_CRYPTO_rsa_encode_key (key);
602 gph->key = GNUNET_CRYPTO_rsa_decode_key ((char*)pb_key, ntohs (pb_key->len));
603 //gph->key = key;//GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
604 //memcpy (gph->key, key, sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
606 GNUNET_CRYPTO_short_hash (&pkey,
607 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
609 gph->ahead = new_head;
611 shorten_authority_chain (gph);
615 * Initialize the resolver
617 * @param nh the namestore handle
618 * @param dh the dht handle
619 * @param lz the local zone's hash
620 * @param max_bg_queries maximum number of parallel background queries in dht
621 * @param ignore_pending ignore records that still require user confirmation
623 * @return GNUNET_OK on success
626 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
627 struct GNUNET_DHT_Handle *dh,
628 struct GNUNET_CRYPTO_ShortHashCode lz,
629 unsigned long long max_bg_queries,
632 namestore_handle = nh;
636 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
637 max_allowed_background_queries = max_bg_queries;
638 ignore_pending_records = ignore_pending;
640 if ((namestore_handle != NULL) && (dht_handle != NULL))
644 return GNUNET_SYSERR;
648 * Cleanup background lookups
650 * @param cls closure to iterator
651 * @param node heap nodes
652 * @param element the resolver handle
653 * @param cost heap cost
654 * @return always GNUNET_YES
657 cleanup_pending_background_queries(void* cls,
658 struct GNUNET_CONTAINER_HeapNode *node,
660 GNUNET_CONTAINER_HeapCostType cost)
662 struct ResolverHandle *rh = (struct ResolverHandle *)element;
663 ResolverCleanupContinuation cont = cls;
665 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
666 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
668 GNUNET_DHT_get_stop(rh->get_handle);
669 rh->get_handle = NULL;
670 rh->proc(rh->proc_cls, rh, 0, NULL);
672 GNUNET_CONTAINER_heap_remove_node(node);
674 if (GNUNET_CONTAINER_heap_get_size(dht_lookup_heap) == 0)
686 gns_resolver_cleanup(ResolverCleanupContinuation cont)
688 unsigned int s = GNUNET_CONTAINER_heap_get_size(dht_lookup_heap);
689 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
690 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
693 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
694 &cleanup_pending_background_queries,
702 * Helper function to free resolver handle
704 * @param rh the handle to free
707 free_resolver_handle(struct ResolverHandle* rh)
709 struct AuthorityChain *ac;
710 struct AuthorityChain *ac_next;
715 ac = rh->authority_chain_head;
724 if (NULL != rh->dns_raw_packet)
725 GNUNET_free (rh->dns_raw_packet);
732 * Callback when record data is put into namestore
734 * @param cls the closure
735 * @param success GNUNET_OK on success
736 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
739 on_namestore_record_put_result(void *cls,
743 if (GNUNET_NO == success)
745 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
746 "GNS_NS: records already in namestore\n");
749 else if (GNUNET_YES == success)
751 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
752 "GNS_NS: records successfully put in namestore\n");
756 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
757 "GNS_NS: Error putting records into namestore: %s\n", emsg);
761 handle_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
763 struct ResolverHandle *rh = cls;
765 if (rh->timeout_cont)
766 rh->timeout_cont(rh->timeout_cont_cls, tc);
770 * Processor for background lookups in the DHT
772 * @param cls closure (NULL)
773 * @param rd_count number of records found (not 0)
774 * @param rd record data
777 background_lookup_result_processor(void *cls,
779 const struct GNUNET_NAMESTORE_RecordData *rd)
781 //We could do sth verbose/more useful here but it doesn't make any difference
782 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
783 "GNS_BG: background dht lookup for finished. (%d results)\n",
788 * Handle timeout for DHT requests
790 * @param cls the request handle as closure
791 * @param tc the task context
794 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
796 struct ResolverHandle *rh = cls;
797 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
798 char new_name[MAX_DNS_NAME_LENGTH];
800 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
801 "GNS_PHASE_REC-%d: dht lookup for query %s (%ds)timed out.\n",
802 rh->id, rh->name, rh->timeout.rel_value);
804 * Start resolution in bg
806 //strcpy(new_name, rh->name);
807 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
808 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
809 rh->name, GNUNET_GNS_TLD);
811 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
812 "GNS_PHASE_REC-%d: Starting background lookup for %s type %d\n",
813 rh->id, new_name, rlh->record_type);
815 gns_resolver_lookup_record(rh->authority,
816 rh->private_local_zone,
820 GNUNET_TIME_UNIT_FOREVER_REL,
822 &background_lookup_result_processor,
824 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
826 GNUNET_DHT_get_stop (rh->get_handle);
827 rh->get_handle = NULL;
828 rh->proc(rh->proc_cls, rh, 0, NULL);
833 * Function called when we get a result from the dht
834 * for our record query
836 * @param cls the request handle
837 * @param exp lifetime
838 * @param key the key the record was stored under
839 * @param get_path get path
840 * @param get_path_length get path length
841 * @param put_path put path
842 * @param put_path_length put path length
843 * @param type the block type
844 * @param size the size of the record
845 * @param data the record data
848 process_record_result_dht(void* cls,
849 struct GNUNET_TIME_Absolute exp,
850 const struct GNUNET_HashCode * key,
851 const struct GNUNET_PeerIdentity *get_path,
852 unsigned int get_path_length,
853 const struct GNUNET_PeerIdentity *put_path,
854 unsigned int put_path_length,
855 enum GNUNET_BLOCK_Type type,
856 size_t size, const void *data)
858 struct ResolverHandle *rh;
859 struct RecordLookupHandle *rlh;
860 struct GNSNameRecordBlock *nrb;
861 uint32_t num_records;
863 char* rd_data = (char*)data;
867 rh = (struct ResolverHandle *)cls;
868 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
869 "GNS_PHASE_REC-%d: got dht result (size=%d)\n", rh->id, size);
874 //FIXME maybe check expiration here, check block type
877 rlh = (struct RecordLookupHandle *) rh->proc_cls;
878 nrb = (struct GNSNameRecordBlock*)data;
880 /* stop lookup and timeout task */
881 GNUNET_DHT_get_stop (rh->get_handle);
882 rh->get_handle = NULL;
884 if (rh->dht_heap_node != NULL)
886 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
887 rh->dht_heap_node = NULL;
890 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
892 GNUNET_SCHEDULER_cancel(rh->timeout_task);
893 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
896 rh->get_handle = NULL;
897 name = (char*)&nrb[1];
898 num_records = ntohl(nrb->rd_count);
900 struct GNUNET_NAMESTORE_RecordData rd[num_records];
902 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
903 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
905 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
910 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
911 "GNS_PHASE_REC-%d: Error deserializing data!\n", rh->id);
915 for (i=0; i<num_records; i++)
917 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
918 "GNS_PHASE_REC-%d: Got name: %s (wanted %s)\n",
919 rh->id, name, rh->name);
920 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
921 "GNS_PHASE_REC-%d: Got type: %d\n",
922 rh->id, rd[i].record_type);
923 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
924 "GNS_PHASE_REC-%d: Got data length: %d\n",
925 rh->id, rd[i].data_size);
926 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
927 "GNS_PHASE_REC-%d: Got flag %d\n",
928 rh->id, rd[i].flags);
930 if ((strcmp(name, rh->name) == 0) &&
931 (rd[i].record_type == rlh->record_type))
939 * FIXME check pubkey against existing key in namestore?
940 * https://gnunet.org/bugs/view.php?id=2179
943 /* Save to namestore */
944 GNUNET_NAMESTORE_record_put (namestore_handle,
951 &on_namestore_record_put_result, //cont
956 rh->proc(rh->proc_cls, rh, num_records, rd);
958 rh->proc(rh->proc_cls, rh, 0, NULL);
965 * Start DHT lookup for a (name -> query->record_type) record in
966 * rh->authority's zone
968 * @param rh the pending gns query context
971 resolve_record_dht(struct ResolverHandle *rh)
974 struct GNUNET_CRYPTO_ShortHashCode name_hash;
975 struct GNUNET_HashCode lookup_key;
976 struct GNUNET_HashCode name_hash_double;
977 struct GNUNET_HashCode zone_hash_double;
978 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
979 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
980 struct ResolverHandle *rh_heap_root;
982 GNUNET_CRYPTO_short_hash(rh->name, strlen(rh->name), &name_hash);
983 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
984 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
985 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
986 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
988 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
989 "GNS_PHASE_REC-%d: starting dht lookup for %s with key: %s\n",
990 rh->id, rh->name, (char*)&lookup_key_string);
992 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
993 rh->dht_heap_node = NULL;
995 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
998 * Update timeout if necessary
1000 if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
1003 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1004 "GNS_PHASE_REC-%d: Adjusting timeout\n", rh->id);
1006 * Set timeout for authority lookup phase to 1/2
1008 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
1009 GNUNET_TIME_relative_divide(rh->timeout, 2),
1010 &handle_lookup_timeout,
1013 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1014 // &dht_lookup_timeout,
1016 rh->timeout_cont = &dht_lookup_timeout;
1017 rh->timeout_cont_cls = rh;
1021 if (max_allowed_background_queries <=
1022 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1024 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1025 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
1026 rh_heap_root->get_handle = NULL;
1027 rh_heap_root->dht_heap_node = NULL;
1029 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1030 "GNS_PHASE_REC-%d: Replacing oldest background query for %s\n",
1031 rh->id, rh_heap_root->name);
1032 rh_heap_root->proc(rh_heap_root->proc_cls,
1037 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1039 GNUNET_TIME_absolute_get().abs_value);
1042 xquery = htonl(rlh->record_type);
1044 GNUNET_assert(rh->get_handle == NULL);
1045 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1046 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1048 DHT_GNS_REPLICATION_LEVEL,
1052 &process_record_result_dht,
1059 * Namestore calls this function if we have record for this name.
1060 * (or with rd_count=0 to indicate no matches)
1062 * @param cls the pending query
1063 * @param key the key of the zone we did the lookup
1064 * @param expiration expiration date of the namestore entry
1065 * @param name the name for which we need an authority
1066 * @param rd_count the number of records with 'name'
1067 * @param rd the record data
1068 * @param signature the signature of the authority for the record data
1071 process_record_result_ns(void* cls,
1072 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1073 struct GNUNET_TIME_Absolute expiration,
1074 const char *name, unsigned int rd_count,
1075 const struct GNUNET_NAMESTORE_RecordData *rd,
1076 const struct GNUNET_CRYPTO_RsaSignature *signature)
1078 struct ResolverHandle *rh;
1079 struct RecordLookupHandle *rlh;
1080 struct GNUNET_TIME_Relative remaining_time;
1081 struct GNUNET_CRYPTO_ShortHashCode zone;
1083 rh = (struct ResolverHandle *) cls;
1084 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1085 GNUNET_CRYPTO_short_hash(key,
1086 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1088 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1096 rh->status |= RSL_RECORD_EXISTS;
1099 if (remaining_time.rel_value == 0)
1101 rh->status |= RSL_RECORD_EXPIRED;
1107 * Lookup terminated and no results
1109 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1110 "GNS_PHASE_REC-%d: Namestore lookup for %s terminated without results\n",
1113 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1114 "GNS_PHASE_REC-%d: Record %s unknown in namestore\n",
1117 * Our zone and no result? Cannot resolve TT
1119 rh->proc(rh->proc_cls, rh, 0, NULL);
1126 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1127 "GNS_PHASE_REC-%d: Processing additional result %s from namestore\n",
1130 for (i=0; i<rd_count;i++)
1133 if (rd[i].record_type != rlh->record_type)
1136 if (ignore_pending_records &&
1137 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1139 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1140 "GNS_PHASE_REC-%d: Record %s is awaiting user confirmation. Skipping\n",
1145 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1148 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1149 "GNS_PHASE_REC-%d: This record is expired. Skipping\n",
1161 if (rh->answered == 0)
1163 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1164 "GNS_PHASE_REC-%d: No answers found. This is odd!\n", rh->id);
1165 rh->proc(rh->proc_cls, rh, 0, NULL);
1169 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1170 "GNS_PHASE_REC-%d: Found %d answer(s) to query in %d records!\n",
1171 rh->id, rh->answered, rd_count);
1173 rh->proc(rh->proc_cls, rh, rd_count, rd);
1179 * VPN redirect result callback
1181 * @param cls the resolver handle
1182 * @param af the requested address family
1183 * @param address in_addr(6) respectively
1186 process_record_result_vpn (void* cls, int af, const void *address)
1188 struct ResolverHandle *rh = cls;
1189 struct RecordLookupHandle *rlh;
1190 struct GNUNET_NAMESTORE_RecordData rd;
1192 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1194 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1195 "GNS_PHASE_REC_VPN-%d: Got answer from VPN to query!\n",
1199 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1200 "GNS_PHASE_REC-%d: Answer is IPv4!\n",
1202 if (rlh->record_type != GNUNET_GNS_RECORD_TYPE_A)
1204 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1205 "GNS_PHASE_REC-%d: Requested record is not IPv4!\n",
1207 rh->proc (rh->proc_cls, rh, 0, NULL);
1210 rd.record_type = GNUNET_GNS_RECORD_TYPE_A;
1211 rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
1213 rd.data_size = sizeof (struct in_addr);
1215 rh->proc (rh->proc_cls, rh, 1, &rd);
1218 else if (af == AF_INET6)
1220 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1221 "GNS_PHASE_REC-%d: Answer is IPv6!\n",
1223 if (rlh->record_type != GNUNET_GNS_RECORD_AAAA)
1225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1226 "GNS_PHASE_REC-%d: Requested record is not IPv6!\n",
1228 rh->proc (rh->proc_cls, rh, 0, NULL);
1231 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1232 rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
1234 rd.data_size = sizeof (struct in6_addr);
1236 rh->proc (rh->proc_cls, rh, 1, &rd);
1240 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1241 "GNS_PHASE_REC-%d: Got garbage from VPN!\n",
1243 rh->proc (rh->proc_cls, rh, 0, NULL);
1250 * @param rh resolver handle
1251 * @param rlh record lookup handle
1252 * @param rd_cound number of results
1256 finish_lookup(struct ResolverHandle *rh,
1257 struct RecordLookupHandle* rlh,
1258 unsigned int rd_count,
1259 const struct GNUNET_NAMESTORE_RecordData *rd);
1262 * Process VPN lookup result for record
1264 * @param cls the record lookup handle
1265 * @param rh resolver handle
1266 * @param rd_count number of results (1)
1267 * @param rd record data containing the result
1270 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1271 unsigned int rd_count,
1272 const struct GNUNET_NAMESTORE_RecordData *rd)
1274 struct RecordLookupHandle* rlh = (struct RecordLookupHandle*) cls;
1278 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1279 "GNS_PHASE_REC_VPN-%d: VPN returned no records. (status: %d)!\n",
1282 /* give up, cannot resolve */
1283 finish_lookup(rh, rlh, 0, NULL);
1284 free_resolver_handle(rh);
1288 /* results found yay */
1289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1290 "GNS_PHASE_REC_VPN-%d: Record resolved from VPN!", rh->id);
1292 finish_lookup(rh, rlh, rd_count, rd);
1294 free_resolver_handle(rh);
1299 * Sends a UDP dns query to a nameserver specified in the rh
1301 * @param rh the request handle
1304 send_dns_packet (struct ResolverHandle *rh);
1308 read_dns_response (void *cls,
1309 const struct GNUNET_SCHEDULER_TaskContext *tc)
1311 struct ResolverHandle *rh = cls;
1312 struct RecordLookupHandle *rlh = rh->proc_cls;
1313 char buf[UINT16_MAX];
1315 struct sockaddr_in addr;
1317 struct GNUNET_DNSPARSER_Packet *packet;
1318 struct GNUNET_NAMESTORE_RecordData rd;
1319 int found_delegation = GNUNET_NO;
1320 char* delegation_name = NULL;
1323 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1324 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1326 /* timeout or shutdown */
1327 rh->proc (rh->proc_cls, rh, 0, NULL);
1328 GNUNET_NETWORK_socket_close (rh->dns_sock);
1329 free_resolver_handle (rh);
1333 addrlen = sizeof (addr);
1334 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1336 (struct sockaddr*) &addr,
1341 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1342 rh->proc (rh->proc_cls, rh, 0, NULL);
1343 GNUNET_NETWORK_socket_close (rh->dns_sock);
1344 free_resolver_handle (rh);
1348 packet = GNUNET_DNSPARSER_parse (buf, r);
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Failed to parse DNS reply!\n");
1354 rh->proc (rh->proc_cls, rh, 0, NULL);
1355 GNUNET_NETWORK_socket_close (rh->dns_sock);
1356 free_resolver_handle (rh);
1360 for (i = 0; i < packet->num_answers; i++)
1362 if ((packet->answers[i].type == rlh->record_type) &&
1363 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367 rd.data = packet->answers[i].data.raw.data;
1368 rd.data_size = packet->answers[i].data.raw.data_len;
1369 rd.record_type = packet->answers[i].type;
1371 rd.expiration = packet->answers[i].expiration_time;
1372 rh->proc (rh->proc_cls, rh, 1, &rd);
1373 GNUNET_NETWORK_socket_close (rh->dns_sock);
1374 GNUNET_DNSPARSER_free_packet (packet);
1375 free_resolver_handle (rh);
1380 for (i = 0; i < packet->num_authority_records; i++)
1382 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_TYPE_NS)
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1385 "Found NS delegation!\n");
1386 found_delegation = GNUNET_YES;
1387 delegation_name = packet->authority_records[i].data.hostname;
1392 for (i = 0; i < packet->num_additional_records; i++)
1394 if (found_delegation == GNUNET_NO)
1397 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_TYPE_A) &&
1398 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1400 GNUNET_assert (sizeof (struct in_addr) ==
1401 packet->authority_records[i].data.raw.data_len);
1403 rh->dns_addr.sin_addr =
1404 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1405 send_dns_packet (rh);
1406 GNUNET_DNSPARSER_free_packet (packet);
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 "Failed to parse DNS reply!\n");
1413 rh->proc (rh->proc_cls, rh, 0, NULL);
1414 GNUNET_NETWORK_socket_close (rh->dns_sock);
1415 free_resolver_handle (rh);
1416 GNUNET_DNSPARSER_free_packet (packet);
1421 * Sends a UDP dns query to a nameserver specified in the rh
1423 * @param rh the request handle
1426 send_dns_packet (struct ResolverHandle *rh)
1428 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1429 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1431 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1433 rh->dns_raw_packet_size,
1434 (struct sockaddr*)&rh->dns_addr,
1435 sizeof (struct sockaddr_in));
1437 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1438 rh->timeout, //FIXME less?
1444 GNUNET_NETWORK_fdset_destroy (rset);
1449 * The final phase of resoution.
1450 * We found a NS RR and want to resolve via DNS
1452 * @param rh the pending lookup handle
1453 * @param rd_count length of record data
1454 * @param rd record data containing VPN RR
1457 resolve_record_dns (struct ResolverHandle *rh,
1459 const struct GNUNET_NAMESTORE_RecordData *rd)
1461 struct GNUNET_DNSPARSER_Query query;
1462 struct GNUNET_DNSPARSER_Packet packet;
1463 struct GNUNET_DNSPARSER_Flags flags;
1464 char dns_name[MAX_DNS_NAME_LENGTH];
1465 struct in_addr dnsip;
1466 struct sockaddr_in addr;
1467 struct sockaddr *sa;
1469 struct RecordLookupHandle *rlh = rh->proc_cls;
1471 /* We cancel here as to not include the ns lookup in the timeout */
1472 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1474 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1475 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1477 /* Start shortening */
1478 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1481 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1483 start_shorten (rh->authority_chain_tail,
1487 for (i = 0; i < rd_count; i++)
1489 /* Synthesize dns name */
1490 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS)
1491 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1493 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
1494 dnsip = *((struct in_addr*)rd[i].data);
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1498 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1501 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1502 if (rh->dns_sock == NULL)
1504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1505 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1507 rh->proc(rh->proc_cls, rh, 0, NULL);
1511 memset (&addr, 0, sizeof (struct sockaddr_in));
1512 sa = (struct sockaddr *) &addr;
1513 sa->sa_family = AF_INET;
1514 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1516 sizeof (struct sockaddr_in)))
1518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1519 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1521 GNUNET_NETWORK_socket_close (rh->dns_sock);
1522 rh->proc(rh->proc_cls, rh, 0, NULL);
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526 "GNS_PHASE_REC_DNS-%llu: NOT IMPLEMENTED!\n",
1528 GNUNET_NETWORK_socket_close (rh->dns_sock);
1529 rh->proc(rh->proc_cls, rh, 0, NULL);
1530 /*TODO create dnsparser query, serialize, sendto, handle reply*/
1531 query.name = dns_name;
1532 query.type = rlh->record_type;
1533 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1534 memset (&flags, 0, sizeof (flags));
1535 flags.recursion_desired = 1;
1536 flags.checking_disabled = 1;
1537 packet.queries = &query;
1538 packet.answers = NULL;
1539 packet.authority_records = NULL;
1540 packet.num_queries = 1;
1541 packet.num_answers = 0;
1542 packet.num_authority_records = 0;
1543 packet.num_additional_records = 0;
1544 packet.flags = flags;
1546 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1548 &rh->dns_raw_packet,
1549 &rh->dns_raw_packet_size))
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1554 GNUNET_NETWORK_socket_close (rh->dns_sock);
1555 rh->proc(rh->proc_cls, rh, 0, NULL);
1559 rh->dns_addr.sin_family = AF_INET;
1560 rh->dns_addr.sin_port = htons (53); //domain
1561 rh->dns_addr.sin_addr = dnsip;
1562 #if HAVE_SOCKADDR_IN_SIN_LEN
1563 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1566 send_dns_packet (rh);
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 "GNS_PHASE_REC_DNS-%llu: NOT IMPLEMENTED!\n",
1571 GNUNET_free (rh->dns_raw_packet);
1572 GNUNET_NETWORK_socket_close (rh->dns_sock);
1573 rh->proc(rh->proc_cls, rh, 0, NULL);
1578 * The final phase of resoution.
1579 * We found a VPN RR and want to request an IPv4/6 address
1581 * @param rh the pending lookup handle
1582 * @param rd_count length of record data
1583 * @param rd record data containing VPN RR
1586 resolve_record_vpn (struct ResolverHandle *rh,
1588 const struct GNUNET_NAMESTORE_RecordData *rd)
1592 struct GNUNET_HashCode peer_id;
1593 struct GNUNET_CRYPTO_HashAsciiEncoded s_pid;
1594 struct GNUNET_HashCode serv_desc;
1595 struct GNUNET_CRYPTO_HashAsciiEncoded s_sd;
1597 size_t len = (sizeof (uint32_t) * 2) + (sizeof (struct GNUNET_HashCode) * 2);
1599 /* We cancel here as to not include the ns lookup in the timeout */
1600 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1602 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1603 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1605 /* Start shortening */
1606 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1609 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1611 start_shorten (rh->authority_chain_tail,
1615 /* Extracting VPN information FIXME rd parsing with NS API?*/
1616 if (len != rd->data_size)
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1619 "GNS_PHASE_REC_VPN-%llu: Error parsing VPN RR!\n",
1621 rh->proc(rh->proc_cls, rh, 0, NULL);
1626 memcpy (&af, pos, sizeof (uint32_t));
1627 pos += sizeof (uint32_t);
1628 memcpy (&proto, pos, sizeof (uint32_t));
1629 pos += sizeof (uint32_t);
1630 memcpy (&s_pid, pos, sizeof (struct GNUNET_HashCode));
1631 pos += sizeof (struct GNUNET_HashCode);
1632 memcpy (&s_sd, pos, sizeof (struct GNUNET_HashCode));
1635 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_pid, &peer_id)) ||
1636 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_sd, &serv_desc)))
1638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1639 "GNS_PHASE_REC_VPN-%llu: Error parsing VPN RR hashes!\n",
1641 rh->proc(rh->proc_cls, rh, 0, NULL);
1645 rh->proc = &handle_record_vpn;
1648 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1650 (struct GNUNET_PeerIdentity*)&peer_id,
1653 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1654 &process_record_result_vpn,
1660 * The final phase of resolution.
1661 * rh->name is a name that is canonical and we do not have a delegation.
1662 * Query namestore for this record
1664 * @param rh the pending lookup handle
1667 resolve_record_ns(struct ResolverHandle *rh)
1669 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1671 /* We cancel here as to not include the ns lookup in the timeout */
1672 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1674 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1675 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1677 /* Start shortening */
1678 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1681 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1683 start_shorten (rh->authority_chain_tail,
1688 * Try to resolve this record in our namestore.
1689 * The name to resolve is now in rh->authority_name
1690 * since we tried to resolve it to an authority
1693 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1697 &process_record_result_ns,
1704 * Handle timeout for DHT requests
1706 * @param cls the request handle as closure
1707 * @param tc the task context
1710 dht_authority_lookup_timeout(void *cls,
1711 const struct GNUNET_SCHEDULER_TaskContext *tc)
1713 struct ResolverHandle *rh = cls;
1714 struct RecordLookupHandle *rlh = rh->proc_cls;
1715 char new_name[MAX_DNS_NAME_LENGTH];
1717 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1718 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%ds)timed out.\n",
1719 rh->id, rh->authority_name, rh->timeout.rel_value);
1721 rh->status |= RSL_TIMED_OUT;
1723 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1725 GNUNET_DHT_get_stop (rh->get_handle);
1726 rh->get_handle = NULL;
1728 if (strcmp(rh->name, "") == 0)
1731 * promote authority back to name and try to resolve record
1733 strcpy(rh->name, rh->authority_name);
1734 rh->proc(rh->proc_cls, rh, 0, NULL);
1739 * Start resolution in bg
1741 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
1742 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
1743 //strcpy(new_name, rh->name);
1744 //strcpy(new_name+strlen(new_name), ".");
1745 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1747 strcpy(rh->name, new_name);
1749 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1750 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1751 rh->id, rh->name, rlh->record_type);
1753 gns_resolver_lookup_record(rh->authority,
1754 rh->private_local_zone,
1758 GNUNET_TIME_UNIT_FOREVER_REL,
1760 &background_lookup_result_processor,
1763 rh->proc(rh->proc_cls, rh, 0, NULL);
1767 static void resolve_delegation_dht(struct ResolverHandle *rh);
1770 static void resolve_delegation_ns(struct ResolverHandle *rh);
1774 * Namestore resolution for delegation finished. Processing result.
1776 * @param cls the closure
1777 * @param rh resolver handle
1778 * @param rd_count number of results (always 0)
1779 * @param rd record data (always NULL)
1782 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1783 unsigned int rd_count,
1784 const struct GNUNET_NAMESTORE_RecordData *rd);
1788 * Function called when we get a result from the dht
1789 * for our query. Recursively tries to resolve authorities
1792 * @param cls the request handle
1793 * @param exp lifetime
1794 * @param key the key the record was stored under
1795 * @param get_path get path
1796 * @param get_path_length get path length
1797 * @param put_path put path
1798 * @param put_path_length put path length
1799 * @param type the block type
1800 * @param size the size of the record
1801 * @param data the record data
1804 process_delegation_result_dht(void* cls,
1805 struct GNUNET_TIME_Absolute exp,
1806 const struct GNUNET_HashCode * key,
1807 const struct GNUNET_PeerIdentity *get_path,
1808 unsigned int get_path_length,
1809 const struct GNUNET_PeerIdentity *put_path,
1810 unsigned int put_path_length,
1811 enum GNUNET_BLOCK_Type type,
1812 size_t size, const void *data)
1814 struct ResolverHandle *rh;
1815 struct GNSNameRecordBlock *nrb;
1816 uint32_t num_records;
1818 char* rd_data = (char*) data;
1821 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
1822 struct GNUNET_HashCode zone_hash_double, name_hash_double;
1824 rh = (struct ResolverHandle *)cls;
1826 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1827 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
1832 nrb = (struct GNSNameRecordBlock*)data;
1834 /* stop dht lookup and timeout task */
1835 GNUNET_DHT_get_stop (rh->get_handle);
1837 rh->get_handle = NULL;
1839 if (rh->dht_heap_node != NULL)
1841 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
1842 rh->dht_heap_node = NULL;
1845 num_records = ntohl(nrb->rd_count);
1846 name = (char*)&nrb[1];
1848 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1850 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1851 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
1853 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1858 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1859 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
1864 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1865 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
1866 rh->id, name, rh->authority_name);
1867 for (i=0; i<num_records; i++)
1870 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1871 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
1872 rh->id, name, rh->authority_name);
1873 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1874 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
1875 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
1876 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1877 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
1878 rh->id, rd[i].data_size);
1879 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1880 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
1881 rh->id, rd[i].flags);
1883 if ((strcmp(name, rh->authority_name) == 0) &&
1884 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
1886 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1887 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
1890 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
1891 struct AuthorityChain *auth =
1892 GNUNET_malloc(sizeof(struct AuthorityChain));
1893 auth->zone = rh->authority;
1894 memset(auth->name, 0, strlen(rh->authority_name)+1);
1895 strcpy(auth->name, rh->authority_name);
1896 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1897 rh->authority_chain_tail,
1900 /** try to import pkey if private key available */
1901 //if (rh->priv_key && is_canonical (rh->name))
1902 // process_discovered_authority(name, auth->zone,
1903 // rh->authority_chain_tail->zone,
1910 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
1911 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1912 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
1913 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
1915 /* Save to namestore */
1916 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
1919 GNUNET_NAMESTORE_record_put (namestore_handle,
1926 &on_namestore_record_put_result, //cont
1936 * FIXME in this case. should we ask namestore again?
1938 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1939 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
1940 rh->id, rh->authority_name, rh->name);
1942 if (strcmp(rh->name, "") == 0)
1944 /* Start shortening */
1945 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1948 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
1950 start_shorten (rh->authority_chain_tail,
1954 rh->proc(rh->proc_cls, rh, 0, NULL);
1958 rh->proc = &handle_delegation_ns;
1959 resolve_delegation_ns(rh);
1965 * No pkey but name exists
1968 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1969 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
1970 rh->id, rh->authority_name, rh->name);
1971 if (strcmp(rh->name, "") == 0)
1972 strcpy(rh->name, rh->authority_name);
1974 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
1975 rh->name, rh->authority_name); //FIXME ret
1977 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1978 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
1979 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1980 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
1982 rh->proc(rh->proc_cls, rh, 0, NULL);
1985 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
1986 +(MAX_DNS_NAME_LENGTH*2)
1987 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
1991 expand_plus(char** dest, char* src, char* repl)
1994 unsigned int s_len = strlen(src)+1;
1996 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1997 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2001 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2002 "GNS_POSTPROCESS: %s to short\n", src);
2004 /* no postprocessing */
2005 memcpy(*dest, src, s_len+1);
2009 if (0 == strcmp(src+s_len-3, ".+"))
2011 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2012 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2013 memset(*dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2015 pos = *dest+s_len-2;
2017 pos += strlen(repl);
2018 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2019 "GNS_POSTPROCESS: Expanded to %s\n", *dest);
2023 memcpy(*dest, src, s_len+1);
2031 finish_lookup(struct ResolverHandle *rh,
2032 struct RecordLookupHandle* rlh,
2033 unsigned int rd_count,
2034 const struct GNUNET_NAMESTORE_RecordData *rd)
2037 char new_rr_data[MAX_DNS_NAME_LENGTH];
2038 char new_mx_data[MAX_MX_LENGTH];
2039 char new_soa_data[MAX_SOA_LENGTH];
2040 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2043 unsigned int offset;
2045 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2047 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2048 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2052 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2054 for (i = 0; i < rd_count; i++)
2057 if (rd[i].record_type != GNUNET_GNS_RECORD_TYPE_NS &&
2058 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_CNAME &&
2059 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
2060 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_SOA)
2062 p_rd[i].data = rd[i].data;
2067 * for all those records we 'should'
2068 * also try to resolve the A/AAAA records (RFC1035)
2069 * This is a feature and not important
2072 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2073 "GNS_POSTPROCESS: Postprocessing\n");
2075 if (strcmp(rh->name, "+") == 0)
2076 repl_string = rlh->name;
2078 repl_string = rlh->name+strlen(rh->name)+1;
2081 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
2083 memcpy(new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2084 offset = sizeof(uint16_t);
2085 pos = new_mx_data+offset;
2086 expand_plus(&pos, (char*)rd[i].data+sizeof(uint16_t),
2088 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2089 p_rd[i].data = new_mx_data;
2090 p_rd[i].data_size = offset;
2092 else if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_SOA)
2094 /* expand mname and rname */
2096 expand_plus(&pos, (char*)rd[i].data, repl_string);
2097 offset = strlen(new_soa_data)+1;
2098 pos = new_soa_data+offset;
2099 expand_plus(&pos, (char*)rd[i].data+offset, repl_string);
2100 offset += strlen(new_soa_data+offset)+1;
2101 /* cpy the 4 numbers serial refresh retry and expire */
2102 memcpy(new_soa_data+offset, (char*)rd[i].data+offset, sizeof(uint32_t)*5);
2103 offset += sizeof(uint32_t)*5;
2104 p_rd[i].data_size = offset;
2105 p_rd[i].data = new_soa_data;
2110 expand_plus(&pos, (char*)rd[i].data, repl_string);
2111 p_rd[i].data_size = strlen(new_rr_data)+1;
2112 p_rd[i].data = new_rr_data;
2117 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2123 * Process DHT lookup result for record.
2125 * @param cls the closure
2126 * @param rh resolver handle
2127 * @param rd_count number of results
2128 * @param rd record data
2131 handle_record_dht(void* cls, struct ResolverHandle *rh,
2132 unsigned int rd_count,
2133 const struct GNUNET_NAMESTORE_RecordData *rd)
2135 struct RecordLookupHandle* rlh;
2137 rlh = (struct RecordLookupHandle*)cls;
2140 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2141 "GNS_PHASE_REC-%d: No records for %s found in DHT. Aborting\n",
2143 /* give up, cannot resolve */
2144 finish_lookup(rh, rlh, 0, NULL);
2145 free_resolver_handle(rh);
2149 /* results found yay */
2150 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2151 "GNS_PHASE_REC-%d: Record resolved from DHT!", rh->id);
2153 finish_lookup(rh, rlh, rd_count, rd);
2154 free_resolver_handle(rh);
2162 * Process namestore lookup result for record.
2164 * @param cls the closure
2165 * @param rh resolver handle
2166 * @param rd_count number of results
2167 * @param rd record data
2170 handle_record_ns (void* cls, struct ResolverHandle *rh,
2171 unsigned int rd_count,
2172 const struct GNUNET_NAMESTORE_RecordData *rd)
2174 struct RecordLookupHandle* rlh;
2175 rlh = (struct RecordLookupHandle*) cls;
2178 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2179 "GNS_PHASE_REC-%d: NS returned no records. (status: %d)!\n",
2184 * There are 5 conditions that have to met for us to consult the DHT:
2185 * 1. The entry in the DHT is RSL_RECORD_EXPIRED AND
2186 * 2. No entry in the NS existed AND
2187 * 3. The zone queried is not the local resolver's zone AND
2188 * 4. The name that was looked up is '+'
2189 * because if it was any other canonical name we either already queried
2190 * the DHT for the authority in the authority lookup phase (and thus
2191 * would already have an entry in the NS for the record)
2192 * 5. We are not in cache only mode
2194 if (rh->status & (RSL_RECORD_EXPIRED | !RSL_RECORD_EXISTS) &&
2195 GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2196 &rh->private_local_zone) &&
2197 (strcmp(rh->name, "+") == 0) &&
2198 (rh->only_cached == GNUNET_NO))
2200 rh->proc = &handle_record_dht;
2201 resolve_record_dht(rh);
2204 /* give up, cannot resolve */
2205 finish_lookup(rh, rlh, 0, NULL);
2206 free_resolver_handle(rh);
2210 /* results found yay */
2211 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2212 "GNS_PHASE_REC-%d: Record resolved from namestore!", rh->id);
2214 finish_lookup(rh, rlh, rd_count, rd);
2216 free_resolver_handle(rh);
2222 * Move one level up in the domain hierarchy and return the
2223 * passed top level domain.
2225 * @param name the domain
2226 * @param dest the destination where the tld will be put
2229 pop_tld(char* name, char* dest)
2233 if (is_canonical(name))
2240 for (len = strlen(name); len > 0; len--)
2242 if (*(name+len) == '.')
2252 strcpy(dest, (name+len+1));
2256 * Checks if name is in tld
2258 * @param name the name to check
2259 * @param tld the TLD to check for
2260 * @return GNUNET_YES or GNUNET_NO
2263 is_tld(const char* name, const char* tld)
2267 if (strlen(name) <= strlen(tld))
2272 offset = strlen(name)-strlen(tld);
2273 if (strcmp(name+offset, tld) != 0)
2275 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2276 "%s is not in .%s TLD\n", name, tld);
2283 * DHT resolution for delegation finished. Processing result.
2285 * @param cls the closure
2286 * @param rh resolver handle
2287 * @param rd_count number of results (always 0)
2288 * @param rd record data (always NULL)
2291 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2292 unsigned int rd_count,
2293 const struct GNUNET_NAMESTORE_RecordData *rd)
2295 struct RecordLookupHandle* rlh;
2296 rlh = (struct RecordLookupHandle*) cls;
2299 if (strcmp(rh->name, "") == 0)
2301 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
2303 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2304 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2306 finish_lookup(rh, rlh, rd_count, rd);
2307 free_resolver_handle(rh);
2310 /* We resolved full name for delegation. resolving record */
2311 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2312 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2314 strcpy(rh->name, "+\0");
2315 rh->proc = &handle_record_ns;
2316 resolve_record_ns(rh);
2321 * we still have some left
2323 if (is_canonical(rh->name))
2325 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2326 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2329 rh->proc = &handle_record_ns;
2330 resolve_record_ns(rh);
2333 /* give up, cannot resolve */
2334 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2335 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2337 finish_lookup(rh, rlh, 0, NULL);
2338 free_resolver_handle(rh);
2343 * Start DHT lookup for a name -> PKEY (compare NS) record in
2344 * rh->authority's zone
2346 * @param rh the pending gns query
2349 resolve_delegation_dht(struct ResolverHandle *rh)
2352 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2353 struct GNUNET_HashCode name_hash_double;
2354 struct GNUNET_HashCode zone_hash_double;
2355 struct GNUNET_HashCode lookup_key;
2356 struct ResolverHandle *rh_heap_root;
2358 pop_tld(rh->name, rh->authority_name);
2359 GNUNET_CRYPTO_short_hash(rh->authority_name,
2360 strlen(rh->authority_name),
2362 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2363 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2364 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2366 rh->dht_heap_node = NULL;
2368 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2370 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
2371 // &dht_authority_lookup_timeout,
2373 rh->timeout_cont = &dht_authority_lookup_timeout;
2374 rh->timeout_cont_cls = rh;
2378 if (max_allowed_background_queries <=
2379 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2381 /* terminate oldest lookup */
2382 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2383 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
2384 rh_heap_root->dht_heap_node = NULL;
2386 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2387 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2388 rh->id, rh_heap_root->authority_name);
2390 rh_heap_root->proc(rh_heap_root->proc_cls,
2395 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2397 GNUNET_TIME_absolute_get().abs_value);
2400 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2402 GNUNET_assert(rh->get_handle == NULL);
2403 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2404 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2406 DHT_GNS_REPLICATION_LEVEL,
2410 &process_delegation_result_dht,
2417 * Namestore resolution for delegation finished. Processing result.
2419 * @param cls the closure
2420 * @param rh resolver handle
2421 * @param rd_count number of results (always 0)
2422 * @param rd record data (always NULL)
2425 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2426 unsigned int rd_count,
2427 const struct GNUNET_NAMESTORE_RecordData *rd)
2429 struct RecordLookupHandle* rlh;
2430 rlh = (struct RecordLookupHandle*) cls;
2432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2433 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2434 rh->id, rh->status);
2436 if (strcmp(rh->name, "") == 0)
2438 if (rlh->record_type == GNUNET_GNS_RECORD_PKEY)
2440 GNUNET_assert(rd_count == 1);
2441 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2442 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
2444 finish_lookup(rh, rlh, rd_count, rd);
2445 free_resolver_handle(rh);
2448 /* We resolved full name for delegation. resolving record */
2449 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2450 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2453 if (rh->status & RSL_DELEGATE_VPN)
2455 if (rlh->record_type == GNUNET_GNS_RECORD_VPN)
2457 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2458 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2460 finish_lookup(rh, rlh, rd_count, rd);
2461 free_resolver_handle(rh);
2464 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2465 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2467 GNUNET_assert (NULL != rd);
2468 rh->proc = &handle_record_vpn;
2469 resolve_record_vpn (rh, rd_count, rd);
2471 else if (rh->status & RSL_DELEGATE_NS)
2473 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_NS)
2475 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2476 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2478 finish_lookup(rh, rlh, rd_count, rd);
2479 free_resolver_handle(rh);
2482 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2483 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2485 GNUNET_assert (NULL != rd);
2486 rh->proc = &handle_record_ns;
2487 resolve_record_dns (rh, rd_count, rd);
2491 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2492 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
2494 strcpy(rh->name, "+\0");
2495 rh->proc = &handle_record_ns;
2496 resolve_record_ns(rh);
2502 * we still have some left
2503 * check if authority in ns is fresh
2505 * or we are authority
2507 if (((rh->status & RSL_RECORD_EXISTS) && (!(rh->status & RSL_RECORD_EXPIRED)))
2508 || !GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2509 &rh->private_local_zone))
2511 if (is_canonical(rh->name))
2513 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2514 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
2517 rh->proc = &handle_record_ns;
2518 resolve_record_ns(rh);
2522 /* give up, cannot resolve */
2523 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2524 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
2527 finish_lookup(rh, rlh, rd_count, rd);
2528 //rlh->proc(rlh->proc_cls, 0, NULL);
2533 if (rh->only_cached == GNUNET_YES)
2535 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2536 "GNS_PHASE_DELEGATE_NS-%llu: Only cache resolution, no result\n",
2538 finish_lookup(rh, rlh, rd_count, rd);
2542 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2543 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
2545 rh->proc = &handle_delegation_dht;
2546 resolve_delegation_dht(rh);
2552 * This is a callback function that should give us only PKEY
2553 * records. Used to query the namestore for the authority (PKEY)
2554 * for 'name'. It will recursively try to resolve the
2555 * authority for a given name from the namestore.
2557 * @param cls the pending query
2558 * @param key the key of the zone we did the lookup
2559 * @param expiration expiration date of the record data set in the namestore
2560 * @param name the name for which we need an authority
2561 * @param rd_count the number of records with 'name'
2562 * @param rd the record data
2563 * @param signature the signature of the authority for the record data
2566 process_delegation_result_ns(void* cls,
2567 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2568 struct GNUNET_TIME_Absolute expiration,
2570 unsigned int rd_count,
2571 const struct GNUNET_NAMESTORE_RecordData *rd,
2572 const struct GNUNET_CRYPTO_RsaSignature *signature)
2574 struct ResolverHandle *rh;
2575 struct GNUNET_TIME_Relative remaining_time;
2576 struct GNUNET_CRYPTO_ShortHashCode zone;
2577 char new_name[MAX_DNS_NAME_LENGTH];
2579 rh = (struct ResolverHandle *)cls;
2580 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2581 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
2584 GNUNET_CRYPTO_short_hash(key,
2585 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2587 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2593 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2594 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
2596 rh->status |= RSL_RECORD_EXISTS;
2599 if (remaining_time.rel_value == 0)
2601 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2602 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
2604 rh->status |= RSL_RECORD_EXPIRED;
2608 * No authority found in namestore.
2613 * We did not find an authority in the namestore
2618 * Promote this authority back to a name maybe it is
2621 if (strcmp(rh->name, "") == 0)
2623 /* simply promote back */
2624 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2625 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
2626 rh->id, rh->authority_name);
2627 strcpy(rh->name, rh->authority_name);
2631 /* add back to existing name */
2632 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2633 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
2634 rh->id, rh->authority_name, rh->name);
2635 //memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
2636 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2637 rh->name, rh->authority_name);
2638 //strcpy(new_name, rh->name);
2639 //strcpy(new_name+strlen(new_name), ".");
2640 //strcpy(new_name+strlen(new_name), rh->authority_name);
2641 strcpy(rh->name, new_name);
2642 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2643 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n", rh->id, rh->name);
2645 rh->proc(rh->proc_cls, rh, 0, NULL);
2650 * We found an authority that may be able to help us
2651 * move on with query
2652 * Note only 1 pkey should have been returned.. anything else would be strange
2655 for (i=0; i<rd_count;i++)
2660 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
2662 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2663 "GNS_PHASE_DELEGATE_NS-%llu: VPNRR found.\n",
2665 rh->status |= RSL_DELEGATE_VPN;
2666 rh->proc (rh->proc_cls, rh, rd_count, rd);
2672 * FIXME make optional
2674 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS)
2676 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2677 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
2679 rh->status |= RSL_DELEGATE_NS;
2680 rh->proc (rh->proc_cls, rh, rd_count, rd);
2683 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
2686 if (ignore_pending_records &&
2687 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
2689 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2690 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
2696 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
2699 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2700 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
2702 if (remaining_time.rel_value == 0)
2704 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2705 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
2707 rh->authority_chain_head->fresh = 0;
2708 rh->proc(rh->proc_cls, rh, 0, NULL);
2716 * Resolve rest of query with new authority
2718 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
2719 memcpy(&rh->authority, rd[i].data,
2720 sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2721 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
2722 auth->zone = rh->authority;
2723 memset(auth->name, 0, strlen(rh->authority_name)+1);
2724 strcpy(auth->name, rh->authority_name);
2725 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2726 rh->authority_chain_tail,
2729 /** try to import pkey if private key available
2730 * TODO: Only import last one?
2732 //if (rh->priv_key && (name != NULL) && is_canonical (rh->name))
2733 // process_discovered_authority((char*)name, auth->zone,
2734 // rh->authority_chain_tail->zone,
2737 * We are done with PKEY resolution if name is empty
2738 * else resolve again with new authority
2740 if (strcmp(rh->name, "") == 0)
2741 rh->proc(rh->proc_cls, rh, rd_count, rd);
2743 resolve_delegation_ns(rh);
2750 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2751 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
2753 * If we have found some records for the LAST label
2754 * we return the results. Else null.
2756 if (strcmp(rh->name, "") == 0)
2758 /* Start shortening */
2759 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2762 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
2764 start_shorten (rh->authority_chain_tail,
2767 /* simply promote back */
2768 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2769 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
2770 rh->id, rh->authority_name);
2771 strcpy(rh->name, rh->authority_name);
2772 rh->proc(rh->proc_cls, rh, rd_count, rd);
2776 rh->proc(rh->proc_cls, rh, 0, NULL);
2782 * Resolve the delegation chain for the request in our namestore
2784 * @param rh the resolver handle
2787 resolve_delegation_ns(struct ResolverHandle *rh)
2789 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2790 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
2792 pop_tld(rh->name, rh->authority_name);
2793 GNUNET_NAMESTORE_lookup_record(namestore_handle,
2796 GNUNET_GNS_RECORD_ANY,
2797 &process_delegation_result_ns,
2804 * Lookup of a record in a specific zone
2805 * calls lookup result processor on result
2807 * @param zone the root zone
2808 * @param pzone the private local zone
2809 * @param record_type the record type to look up
2810 * @param name the name to look up
2811 * @param key a private key for use with PSEU import (can be NULL)
2812 * @param timeout timeout for resolution
2813 * @param only_cached GNUNET_NO to only check locally not DHT for performance
2814 * @param proc the processor to call on result
2815 * @param cls the closure to pass to proc
2818 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
2819 struct GNUNET_CRYPTO_ShortHashCode pzone,
2820 uint32_t record_type,
2822 struct GNUNET_CRYPTO_RsaPrivateKey *key,
2823 struct GNUNET_TIME_Relative timeout,
2825 RecordLookupProcessor proc,
2828 struct ResolverHandle *rh;
2829 struct RecordLookupHandle* rlh;
2830 char string_hash[MAX_DNS_LABEL_LENGTH];
2831 char nzkey[MAX_DNS_LABEL_LENGTH];
2832 char* nzkey_ptr = nzkey;
2834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2835 "Starting resolution for %s (type=%d)!\n",
2839 if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
2841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2842 "%s is canonical and not gnunet -> cannot resolve!\n", name);
2847 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
2848 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2850 rh->authority = zone;
2854 rh->timeout = timeout;
2855 rh->get_handle = NULL;
2856 rh->private_local_zone = pzone;
2857 rh->only_cached = only_cached;
2861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2862 "No shorten key for resolution\n");
2865 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2868 * Set timeout for authority lookup phase to 1/2
2870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2871 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
2872 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
2873 GNUNET_TIME_relative_divide(timeout, 2),
2874 &handle_lookup_timeout,
2876 rh->timeout_cont = &dht_authority_lookup_timeout;
2877 rh->timeout_cont_cls = rh;
2881 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
2882 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2885 if (strcmp(GNUNET_GNS_TLD, name) == 0)
2888 * Only 'gnunet' given
2890 strcpy(rh->name, "\0");
2894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2895 "Checking for TLD...\n");
2896 if (is_zkey_tld(name) == GNUNET_YES)
2898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2901 * This is a zkey tld
2902 * build hash and use as initial authority
2905 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
2906 memcpy(rh->name, name,
2907 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
2908 pop_tld(rh->name, string_hash);
2910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2911 "ZKEY is %s!\n", string_hash);
2913 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
2915 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
2918 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2919 "Cannot convert ZKEY %s to hash!\n", string_hash);
2929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2932 * Presumably GNUNET tld
2935 strlen(name)-strlen(GNUNET_GNS_TLD));
2936 memcpy(rh->name, name,
2937 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
2942 * Initialize authority chain
2944 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2945 rh->authority_chain_head->prev = NULL;
2946 rh->authority_chain_head->next = NULL;
2947 rh->authority_chain_tail = rh->authority_chain_head;
2948 rh->authority_chain_head->zone = rh->authority;
2951 * Copy original query into lookup handle
2953 rlh->record_type = record_type;
2954 memset(rlh->name, 0, strlen(name) + 1);
2955 strcpy(rlh->name, name);
2957 rlh->proc_cls = cls;
2959 rh->proc = &handle_delegation_ns;
2960 resolve_delegation_ns(rh);
2963 /******** END Record Resolver ***********/
2966 * Callback calles by namestore for a zone to name
2969 * @param cls the closure
2970 * @param zone_key the zone we queried
2971 * @param expire the expiration time of the name
2972 * @param name the name found or NULL
2973 * @param rd_len number of records for the name
2974 * @param rd the record data (PKEY) for the name
2975 * @param signature the signature for the record data
2978 process_zone_to_name_shorten_root (void *cls,
2979 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
2980 struct GNUNET_TIME_Absolute expire,
2982 unsigned int rd_len,
2983 const struct GNUNET_NAMESTORE_RecordData *rd,
2984 const struct GNUNET_CRYPTO_RsaSignature *signature);
2988 * Callback called by namestore for a zone to name
2991 * @param cls the closure
2992 * @param zone_key the zone we queried
2993 * @param expire the expiration time of the name
2994 * @param name the name found or NULL
2995 * @param rd_len number of records for the name
2996 * @param rd the record data (PKEY) for the name
2997 * @param signature the signature for the record data
3000 process_zone_to_name_shorten_shorten (void *cls,
3001 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3002 struct GNUNET_TIME_Absolute expire,
3004 unsigned int rd_len,
3005 const struct GNUNET_NAMESTORE_RecordData *rd,
3006 const struct GNUNET_CRYPTO_RsaSignature *signature)
3008 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3009 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3010 struct AuthorityChain *next_authority;
3012 char result[MAX_DNS_NAME_LENGTH];
3013 char tmp_name[MAX_DNS_NAME_LENGTH];
3016 /* we found a match in our own root zone */
3019 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3020 "result strlen %d\n", strlen(name));
3021 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3022 memset(result, 0, answer_len);
3024 if (strlen(rh->name) > 0)
3026 sprintf (result, "%s.%s.%s.%s.%s",
3028 nsh->shorten_zone_name, nsh->private_zone_name,
3033 sprintf (result, "%s.%s.%s.%s", name,
3034 nsh->shorten_zone_name, nsh->private_zone_name,
3038 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3039 "Found shorten result %s\n", result);
3040 if (strlen (nsh->result) > strlen (result))
3041 strcpy (nsh->result, result);
3043 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3044 nsh->shorten_zone) == 0)
3047 * This is our zone append .gnunet unless name is empty
3048 * (it shouldn't be, usually FIXME what happens if we
3049 * shorten to our zone to a "" record??)
3052 sprintf (result, "%s.%s.%s.%s",
3054 nsh->shorten_zone_name, nsh->private_zone_name,
3056 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3057 "Our zone: Found %s as shorten result\n", result);
3059 if (strlen (nsh->result) > strlen (result))
3060 strcpy (nsh->result, result);
3061 //nsh->proc(nsh->proc_cls, result);
3063 //free_resolver_handle(rh);
3070 * continue with next authority if exists
3072 if ((rh->authority_chain_head->next == NULL))
3074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3075 "Sending %s as shorten result\n", nsh->result);
3076 nsh->proc(nsh->proc_cls, nsh->result);
3078 free_resolver_handle (rh);
3081 next_authority = rh->authority_chain_head;
3083 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3084 "%s.%s", rh->name, next_authority->name);
3086 strcpy(rh->name, tmp_name);
3087 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3088 "No PSEU found for authority %s. Promoting back: %s\n",
3089 next_authority->name, rh->name);
3091 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3092 rh->authority_chain_tail,
3095 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3096 &rh->authority_chain_tail->zone,
3097 &rh->authority_chain_head->zone,
3098 &process_zone_to_name_shorten_root,
3103 * Callback calles by namestore for a zone to name
3106 * @param cls the closure
3107 * @param zone_key the zone we queried
3108 * @param expire the expiration time of the name
3109 * @param name the name found or NULL
3110 * @param rd_len number of records for the name
3111 * @param rd the record data (PKEY) for the name
3112 * @param signature the signature for the record data
3115 process_zone_to_name_shorten_private (void *cls,
3116 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3117 struct GNUNET_TIME_Absolute expire,
3119 unsigned int rd_len,
3120 const struct GNUNET_NAMESTORE_RecordData *rd,
3121 const struct GNUNET_CRYPTO_RsaSignature *signature)
3123 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3124 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3125 struct AuthorityChain *next_authority;
3127 char result[MAX_DNS_NAME_LENGTH];
3128 char tmp_name[MAX_DNS_NAME_LENGTH];
3131 /* we found a match in our own root zone */
3134 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3135 "result strlen %d\n", strlen(name));
3136 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3137 memset(result, 0, answer_len);
3139 if (strlen(rh->name) > 0)
3141 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3145 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3148 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3149 "Found shorten result %s\n", result);
3150 if (strlen (nsh->result) > strlen (result))
3151 strcpy (nsh->result, result);
3153 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3154 nsh->private_zone) == 0)
3157 * This is our zone append .gnunet unless name is empty
3158 * (it shouldn't be, usually FIXME what happens if we
3159 * shorten to our zone to a "" record??)
3162 sprintf (result, "%s.%s.%s",
3163 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3164 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3165 "Our private zone: Found %s as shorten result %s\n", result);
3166 if (strlen (nsh->result) > strlen (result))
3167 strcpy (nsh->result, result);
3170 if (nsh->shorten_zone != NULL)
3172 /* backtrack authorities for names in priv zone */
3173 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3175 &rh->authority_chain_head->zone,
3176 &process_zone_to_name_shorten_shorten,
3183 * continue with next authority if exists
3185 if ((rh->authority_chain_head->next == NULL))
3187 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3188 "Sending %s as shorten result\n", nsh->result);
3189 nsh->proc(nsh->proc_cls, nsh->result);
3191 free_resolver_handle(rh);
3194 next_authority = rh->authority_chain_head;
3196 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3197 "%s.%s", rh->name, next_authority->name);
3199 strcpy(rh->name, tmp_name);
3200 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3201 "No PSEU found for authority %s. Promoting back: %s\n",
3202 next_authority->name, rh->name);
3204 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3205 rh->authority_chain_tail,
3208 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3209 &rh->authority_chain_tail->zone,
3210 &rh->authority_chain_head->zone,
3211 &process_zone_to_name_shorten_root,
3217 * Callback calles by namestore for a zone to name
3220 * @param cls the closure
3221 * @param zone_key the zone we queried
3222 * @param expire the expiration time of the name
3223 * @param name the name found or NULL
3224 * @param rd_len number of records for the name
3225 * @param rd the record data (PKEY) for the name
3226 * @param signature the signature for the record data
3229 process_zone_to_name_shorten_root (void *cls,
3230 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3231 struct GNUNET_TIME_Absolute expire,
3233 unsigned int rd_len,
3234 const struct GNUNET_NAMESTORE_RecordData *rd,
3235 const struct GNUNET_CRYPTO_RsaSignature *signature)
3237 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3238 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3239 struct AuthorityChain *next_authority;
3241 char result[MAX_DNS_NAME_LENGTH];
3242 char tmp_name[MAX_DNS_NAME_LENGTH];
3245 /* we found a match in our own root zone */
3248 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3249 "result strlen %d\n", strlen(name));
3250 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3251 memset(result, 0, answer_len);
3253 if (strlen(rh->name) > 0)
3255 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3259 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3262 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3263 "Found shorten result %s\n", result);
3264 if (strlen (nsh->result) > strlen (result))
3265 strcpy (nsh->result, result);
3267 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3268 nsh->root_zone) == 0)
3271 * This is our zone append .gnunet unless name is empty
3272 * (it shouldn't be, usually FIXME what happens if we
3273 * shorten to our zone to a "" record??)
3276 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3277 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3278 "Our zone: Found %s as shorten result\n", result);
3279 if (strlen (nsh->result) > strlen (result))
3280 strcpy (nsh->result, result);
3283 if (nsh->private_zone != NULL)
3285 /* backtrack authorities for names in priv zone */
3286 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3288 &rh->authority_chain_head->zone,
3289 &process_zone_to_name_shorten_private,
3296 * continue with next authority if exists
3298 if ((rh->authority_chain_head->next == NULL))
3300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3301 "Sending %s as shorten result\n", nsh->result);
3302 nsh->proc(nsh->proc_cls, nsh->result);
3304 free_resolver_handle(rh);
3307 next_authority = rh->authority_chain_head;
3309 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3310 "%s.%s", rh->name, next_authority->name);
3312 strcpy(rh->name, tmp_name);
3313 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3314 "No PSEU found for authority %s. Promoting back: %s\n",
3315 next_authority->name, rh->name);
3317 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3318 rh->authority_chain_tail,
3321 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3322 &rh->authority_chain_tail->zone,
3323 &rh->authority_chain_head->zone,
3324 &process_zone_to_name_shorten_root,
3331 * Process result from namestore delegation lookup
3332 * for shorten operation
3334 * @param cls the client shorten handle
3335 * @param rh the resolver handle
3336 * @param rd_count number of results (0)
3337 * @param rd data (NULL)
3340 handle_delegation_ns_shorten(void* cls,
3341 struct ResolverHandle *rh,
3343 const struct GNUNET_NAMESTORE_RecordData *rd)
3345 struct NameShortenHandle *nsh;
3346 char result[MAX_DNS_NAME_LENGTH];
3348 nsh = (struct NameShortenHandle *)cls;
3351 * At this point rh->name contains the part of the name
3352 * that we do not have a PKEY in our namestore to resolve.
3353 * The authority chain in the resolver handle is now
3354 * useful to backtrack if needed
3357 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3358 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3359 memset(result, 0, sizeof (result));
3361 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3362 nsh->root_zone) == 0)
3365 * This is our zone append .gnunet unless name is empty
3366 * (it shouldn't be, usually FIXME what happens if we
3367 * shorten to our zone to a "" record??)
3370 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3372 "Our zone: Found %s as shorten result\n", result);
3374 if (strlen (nsh->result) > strlen (result))
3375 strcpy (nsh->result, result);
3378 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3379 nsh->private_zone) == 0)
3382 * This is our zone append .gnunet unless name is empty
3383 * (it shouldn't be, usually FIXME what happens if we
3384 * shorten to our zone to a "" record??)
3387 sprintf (result, "%s.%s.%s",
3388 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3389 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3390 "Our zone: Found %s as shorten result %s\n", result);
3392 if (strlen (nsh->result) > strlen (result))
3393 strcpy (nsh->result, result);
3395 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3396 nsh->shorten_zone) == 0)
3399 * This is our zone append .gnunet unless name is empty
3400 * (it shouldn't be, usually FIXME what happens if we
3401 * shorten to our zone to a "" record??)
3404 sprintf (result, "%s.%s.%s",
3405 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3406 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3407 "Our zone: Found %s as shorten result\n", result);
3409 if (strlen (nsh->result) > strlen (result))
3410 strcpy (nsh->result, result);
3414 /* backtrack authorities for names */
3415 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3417 &rh->authority_chain_head->zone,
3418 &process_zone_to_name_shorten_root,
3425 * Callback calles by namestore for a zone to name
3428 * @param cls the closure
3429 * @param zone_key the zone we queried
3430 * @param expire the expiration time of the name
3431 * @param name the name found or NULL
3432 * @param rd_len number of records for the name
3433 * @param rd the record data (PKEY) for the name
3434 * @param signature the signature for the record data
3437 process_zone_to_name_zkey(void *cls,
3438 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3439 struct GNUNET_TIME_Absolute expire,
3441 unsigned int rd_len,
3442 const struct GNUNET_NAMESTORE_RecordData *rd,
3443 const struct GNUNET_CRYPTO_RsaSignature *signature)
3445 struct ResolverHandle *rh = cls;
3446 struct NameShortenHandle *nsh = rh->proc_cls;
3447 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
3448 char new_name[MAX_DNS_NAME_LENGTH];
3450 /* zkey not in our zone */
3454 * In this case we have not given this PKEY a name (yet)
3455 * It is either just not in our zone or not even cached
3456 * Since we do not know at this point we will not try to shorten
3457 * because PKEY import will happen if the user follows the zkey
3460 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
3462 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3463 "No name found for zkey %s returning verbatim!\n", enc);
3464 if (strcmp(rh->name, "") != 0)
3465 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
3466 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
3468 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3469 enc, GNUNET_GNS_TLD_ZKEY);
3471 strcpy (nsh->result, new_name);
3473 nsh->proc(nsh->proc_cls, new_name);
3475 free_resolver_handle(rh);
3479 if (strcmp(rh->name, "") != 0)
3480 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3483 strcpy(new_name, name);
3485 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3486 "Continue shorten for %s!\n", new_name);
3488 strcpy(rh->name, new_name);
3490 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3491 rh->authority_chain_tail = rh->authority_chain_head;
3492 rh->authority_chain_head->zone = rh->authority;
3495 /* Start delegation resolution in our namestore */
3496 resolve_delegation_ns(rh);
3501 * Shorten api from resolver
3503 * @param zone the root zone to use
3504 * @param pzone the private zone to use
3505 * @param szone the shorten zone to use
3506 * @param name the name to shorten
3507 * @param private_zone_name name of the private zone
3508 * @param shorten_zone_name name of the shorten zone
3509 * @param proc the processor to call with result
3510 * @param proc_cls closure to pass to proc
3513 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
3514 struct GNUNET_CRYPTO_ShortHashCode *pzone,
3515 struct GNUNET_CRYPTO_ShortHashCode *szone,
3517 const char* private_zone_name,
3518 const char* shorten_zone_name,
3519 ShortenResultProcessor proc,
3522 struct ResolverHandle *rh;
3523 struct NameShortenHandle *nsh;
3524 char string_hash[MAX_DNS_LABEL_LENGTH];
3525 struct GNUNET_CRYPTO_ShortHashCode zkey;
3526 char nzkey[MAX_DNS_LABEL_LENGTH];
3527 char* nzkey_ptr = nzkey;
3530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3531 "Starting shorten for %s!\n", name);
3533 if (is_canonical ((char*)name))
3535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3536 "%s is canonical. Returning verbatim\n", name);
3537 proc (proc_cls, name);
3541 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
3544 nsh->proc_cls = proc_cls;
3545 nsh->root_zone = zone;
3546 nsh->private_zone = pzone;
3547 nsh->shorten_zone = szone;
3548 strcpy (nsh->private_zone_name, private_zone_name);
3549 strcpy (nsh->shorten_zone_name, shorten_zone_name);
3550 strcpy (nsh->result, name);
3552 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3553 rh->authority = *zone;
3555 rh->priv_key = NULL;
3556 rh->proc = &handle_delegation_ns_shorten;
3559 rh->private_local_zone = *zone;
3561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3562 "Checking for TLD...\n");
3563 if (is_zkey_tld (name) == GNUNET_YES)
3565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3568 * This is a zkey tld
3569 * build hash and use as initial authority
3572 memset (rh->name, 0,
3573 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
3574 memcpy (rh->name, name,
3575 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
3576 pop_tld (rh->name, string_hash);
3578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3579 "ZKEY is %s!\n", string_hash);
3581 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
3583 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
3586 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3587 "Cannot convert ZKEY %s to hash!\n", nzkey);
3590 proc (proc_cls, name);
3594 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3597 &process_zone_to_name_zkey,
3604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3607 * Presumably GNUNET tld
3609 memset (rh->name, 0,
3610 strlen (name)-strlen (GNUNET_GNS_TLD));
3611 memcpy (rh->name, name,
3612 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
3615 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
3616 rh->authority_chain_tail = rh->authority_chain_head;
3617 rh->authority_chain_head->zone = *zone;
3620 /* Start delegation resolution in our namestore */
3621 resolve_delegation_ns (rh);
3624 /*********** END NAME SHORTEN ********************/
3628 * Process result from namestore delegation lookup
3629 * for get authority operation
3631 * @param cls the client get auth handle
3632 * @param rh the resolver handle
3633 * @param rd_count number of results (0)
3634 * @param rd data (NULL)
3637 handle_delegation_result_ns_get_auth(void* cls,
3638 struct ResolverHandle *rh,
3640 const struct GNUNET_NAMESTORE_RecordData *rd)
3642 struct GetNameAuthorityHandle* nah;
3643 char result[MAX_DNS_NAME_LENGTH];
3646 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
3649 * At this point rh->name contains the part of the name
3650 * that we do not have a PKEY in our namestore to resolve.
3651 * The authority chain in the resolver handle is now
3652 * useful to backtrack if needed
3655 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3656 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3658 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3659 "Building response!\n");
3660 if (is_canonical(rh->name))
3663 * We successfully resolved the authority in the ns
3664 * FIXME for our purposes this is fine
3665 * but maybe we want to have an api that also looks
3666 * into the dht (i.e. option in message)
3668 if (strlen(rh->name) > strlen(nah->name))
3670 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3671 "Record name longer than original lookup name... odd!\n");
3675 answer_len = strlen(nah->name) - strlen(rh->name)
3676 + strlen(GNUNET_GNS_TLD) + 1;
3677 memset(result, 0, answer_len);
3678 strcpy(result, nah->name + strlen(rh->name) + 1);
3680 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3681 "Got authority result %s\n", result);
3683 nah->proc(nah->proc_cls, result);
3685 free_resolver_handle(rh);
3689 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3690 "Unable to resolve authority for remaining %s!\n", rh->name);
3691 nah->proc(nah->proc_cls, "");
3693 free_resolver_handle(rh);
3701 * Tries to resolve the authority for name
3704 * @param zone the root zone to look up for
3705 * @param pzone the private local zone
3706 * @param name the name to lookup up
3707 * @param proc the processor to call when finished
3708 * @param proc_cls the closure to pass to the processor
3711 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
3712 struct GNUNET_CRYPTO_ShortHashCode pzone,
3714 GetAuthorityResultProcessor proc,
3717 struct ResolverHandle *rh;
3718 struct GetNameAuthorityHandle *nah;
3720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3721 "Starting authority resolution for %s!\n", name);
3723 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
3724 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
3725 rh->authority = zone;
3727 rh->private_local_zone = pzone;
3729 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3731 strcpy(rh->name, "\0");
3736 strlen(name)-strlen(GNUNET_GNS_TLD));
3737 memcpy(rh->name, name,
3738 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3741 memset(nah->name, 0,
3743 strcpy(nah->name, name);
3745 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3746 rh->authority_chain_tail = rh->authority_chain_head;
3747 rh->authority_chain_head->zone = zone;
3748 rh->proc = &handle_delegation_result_ns_get_auth;
3749 rh->proc_cls = (void*)nah;
3752 nah->proc_cls = proc_cls;
3754 /* Start delegation resolution in our namestore */
3755 resolve_delegation_ns(rh);
3759 /******** END GET AUTHORITY *************/
3761 /* end of gnunet-service-gns_resolver.c */