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_resolver_service.h"
37 #include "gnunet_dnsparser_lib.h"
38 #include "gnunet_gns_service.h"
39 #include "block_gns.h"
41 #include "gnunet-service-gns_resolver.h"
43 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
44 #define DHT_GNS_REPLICATION_LEVEL 5
45 #define MAX_DNS_LABEL_LENGTH 63
49 * Our handle to the namestore service
51 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
54 * Our handle to the vpn service
56 static struct GNUNET_VPN_Handle *vpn_handle;
59 * Resolver handle to the dht
61 static struct GNUNET_DHT_Handle *dht_handle;
64 * Heap for parallel DHT lookups
66 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
69 * Maximum amount of parallel queries in background
71 static unsigned long long max_allowed_background_queries;
74 * Wheather or not to ignore pending records
76 static int ignore_pending_records;
81 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
84 * a resolution identifier pool variable
86 * This is a non critical identifier useful for debugging
88 static unsigned long long rid = 0;
92 * Determine if this name is canonical.
94 * a.b.gnunet = not canonical
97 * @param name the name to test
98 * @return 1 if canonical
101 is_canonical(char* name)
103 uint32_t len = strlen(name);
106 for (i=0; i<len; i++)
108 if (*(name+i) == '.')
116 * Callback that shortens authorities
118 * @param gph the handle containing the name to shorten
121 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
125 * Namestore calls this function if we have record for this name.
126 * (or with rd_count=0 to indicate no matches)
128 * @param cls the pending query
129 * @param key the key of the zone we did the lookup
130 * @param expiration expiration date of the namestore entry
131 * @param name the name for which we need an authority
132 * @param rd_count the number of records with 'name'
133 * @param rd the record data
134 * @param signature the signature of the authority for the record data
137 process_pseu_lookup_ns (void* cls,
138 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
139 struct GNUNET_TIME_Absolute expiration,
140 const char *name, unsigned int rd_count,
141 const struct GNUNET_NAMESTORE_RecordData *rd,
142 const struct GNUNET_CRYPTO_RsaSignature *signature)
144 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
145 struct GNUNET_NAMESTORE_RecordData new_pkey;
146 struct AuthorityChain *iter;
150 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
151 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
152 if (0 == strcmp (gph->name, name))
154 if (gph->ahead->next != NULL)
156 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
160 iter = gph->ahead->next;
161 GNUNET_free (gph->ahead);
163 shorten_authority_chain (gph);
171 iter = gph->ahead->next;
172 GNUNET_free (gph->ahead);
174 } while (iter != NULL);
175 GNUNET_CRYPTO_rsa_key_free (gph->key);
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
182 "GNS_AUTO_PSEU: Trying delegated name %s\n", gph->name);
183 memcpy (gph->test_name, gph->name, strlen (gph->name)+1);
184 GNUNET_NAMESTORE_lookup_record (namestore_handle,
187 GNUNET_NAMESTORE_TYPE_ANY,
188 &process_pseu_lookup_ns,
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
197 new_pkey.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
198 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
199 new_pkey.data = &gph->ahead->zone;
200 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
201 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
202 | GNUNET_NAMESTORE_RF_PRIVATE
203 | GNUNET_NAMESTORE_RF_PENDING;
204 GNUNET_NAMESTORE_record_create (namestore_handle,
212 iter = gph->ahead->next;
213 GNUNET_free (gph->ahead);
215 } while (iter != NULL);
216 GNUNET_CRYPTO_rsa_key_free (gph->key);
222 * process result of a dht pseu lookup
224 * @param gph the handle
225 * @param name the pseu result or NULL
228 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
232 memcpy (gph->test_name, gph->ahead->name, strlen (gph->ahead->name)+1);
236 memcpy (gph->test_name, name, strlen(name)+1);
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
244 * Check for collision
246 GNUNET_NAMESTORE_lookup_record (namestore_handle,
249 GNUNET_NAMESTORE_TYPE_ANY,
250 &process_pseu_lookup_ns,
255 * Handle timeout for dht request
257 * @param cls the request handle as closure
258 * @param tc the task context
261 handle_auth_discovery_timeout(void *cls,
262 const struct GNUNET_SCHEDULER_TaskContext *tc)
264 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
265 struct AuthorityChain *iter;
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
269 GNUNET_DHT_get_stop (gph->get_handle);
270 gph->get_handle = NULL;
272 if (gph->ahead->next != NULL)
274 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
278 iter = gph->ahead->next;
279 GNUNET_free (gph->ahead);
281 shorten_authority_chain (gph);
286 process_pseu_result (gph, NULL);
290 * Function called when we find a PSEU entry in the DHT
292 * @param cls the request handle
293 * @param exp lifetime
294 * @param key the key the record was stored under
295 * @param get_path get path
296 * @param get_path_length get path length
297 * @param put_path put path
298 * @param put_path_length put path length
299 * @param type the block type
300 * @param size the size of the record
301 * @param data the record data
304 process_auth_discovery_dht_result(void* cls,
305 struct GNUNET_TIME_Absolute exp,
306 const struct GNUNET_HashCode * key,
307 const struct GNUNET_PeerIdentity *get_path,
308 unsigned int get_path_length,
309 const struct GNUNET_PeerIdentity *put_path,
310 unsigned int put_path_length,
311 enum GNUNET_BLOCK_Type type,
312 size_t size, const void *data)
314 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
315 struct AuthorityChain *iter;
316 struct GNSNameRecordBlock *nrb;
317 char* rd_data = (char*)data;
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
329 "GNS_GET_AUTH: got dht result null!\n", size);
333 iter = gph->ahead->next;
334 GNUNET_free (gph->ahead);
336 } while (iter != NULL);
337 GNUNET_CRYPTO_rsa_key_free (gph->key);
342 nrb = (struct GNSNameRecordBlock*)data;
344 /* stop lookup and timeout task */
345 GNUNET_DHT_get_stop (gph->get_handle);
346 gph->get_handle = NULL;
347 GNUNET_SCHEDULER_cancel (gph->timeout);
349 gph->get_handle = NULL;
351 nrb = (struct GNSNameRecordBlock*)data;
353 name = (char*)&nrb[1];
354 num_records = ntohl (nrb->rd_count);
356 struct GNUNET_NAMESTORE_RecordData rd[num_records];
358 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
359 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
361 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
367 "GNS_GET_AUTH: Error deserializing data!\n");
371 for (i=0; i < num_records; i++)
373 if ((strcmp (name, "+") == 0) &&
374 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
377 process_pseu_result (gph, (char*)rd[i].data);
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in dht!\n");
386 if (gph->ahead->next != NULL)
388 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
392 iter = gph->ahead->next;
393 GNUNET_free (gph->ahead);
395 shorten_authority_chain (gph);
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "GNS_GET_AUTH: finished shorten, no results!\n");
401 process_pseu_result (gph, NULL);
405 * Process PSEU discovery for shorten via namestore
407 * @param cls the GetPseuAuthorityHandle
408 * @param key the public key
409 * @param expiration recorddata expiration
410 * @param name the looked up name
411 * @param rd_count number of records in set
412 * @param rd record data
413 * @param signature the signature
416 process_auth_discovery_ns_result(void* cls,
417 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
418 struct GNUNET_TIME_Absolute expiration,
420 unsigned int rd_count,
421 const struct GNUNET_NAMESTORE_RecordData *rd,
422 const struct GNUNET_CRYPTO_RsaSignature *signature)
425 struct GNUNET_CRYPTO_ShortHashCode name_hash;
426 struct GNUNET_HashCode lookup_key;
427 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
428 struct GNUNET_HashCode name_hash_double;
429 struct GNUNET_HashCode zone_hash_double;
431 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
432 struct AuthorityChain *iter;
440 GNUNET_CRYPTO_short_hash ("+", strlen ("+"), &name_hash);
441 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
442 GNUNET_CRYPTO_short_hash_double (&gph->ahead->zone, &zone_hash_double);
443 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
444 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
448 "+", (char*)&lookup_key_string);
450 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
451 &handle_auth_discovery_timeout, gph);
453 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
455 GNUNET_assert (gph->get_handle == NULL);
457 gph->get_handle = GNUNET_DHT_get_start(dht_handle,
458 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
460 DHT_GNS_REPLICATION_LEVEL,
464 &process_auth_discovery_dht_result,
469 for (i=0; i < rd_count; i++)
471 if ((strcmp (name, "+") == 0) &&
472 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
475 process_pseu_result (gph, (char*)rd[i].data);
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
482 if (gph->ahead->next != NULL)
484 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
488 iter = gph->ahead->next;
489 GNUNET_free (gph->ahead);
491 shorten_authority_chain (gph);
496 process_pseu_result (gph, NULL);
500 * Callback called by namestore for a zone to name
503 * @param cls the closure
504 * @param zone_key the zone we queried
505 * @param expire the expiration time of the name
506 * @param name the name found or NULL
507 * @param rd_len number of records for the name
508 * @param rd the record data (PKEY) for the name
509 * @param signature the signature for the record data
512 process_zone_to_name_discover (void *cls,
513 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
514 struct GNUNET_TIME_Absolute expire,
517 const struct GNUNET_NAMESTORE_RecordData *rd,
518 const struct GNUNET_CRYPTO_RsaSignature *signature)
520 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
521 struct AuthorityChain *iter;
523 /* we found a match in our own zone */
526 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
527 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
532 iter = gph->ahead->next;
533 GNUNET_free (gph->ahead);
535 } while (iter != NULL);
536 GNUNET_CRYPTO_rsa_key_free (gph->key);
542 GNUNET_NAMESTORE_lookup_record (namestore_handle,
545 GNUNET_GNS_RECORD_PSEU,
546 &process_auth_discovery_ns_result,
554 * Callback that shortens authorities
556 * @param gph the handle to the shorten request
559 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
563 "GNS_AUTO_PSEU: New authority %s discovered\n",
566 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
569 &process_zone_to_name_discover,
575 start_shorten (struct AuthorityChain *atail,
576 struct GNUNET_CRYPTO_RsaPrivateKey *key)
578 struct AuthorityChain *new_head = NULL;
579 struct AuthorityChain *new_tail = NULL;
580 struct AuthorityChain *iter;
581 struct AuthorityChain *acopy;
582 struct GetPseuAuthorityHandle *gph;
583 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
584 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
586 /* First copy the authority chain in reverse order */
587 for (iter = atail; iter != NULL; iter = iter->prev)
589 acopy = GNUNET_malloc (sizeof (struct AuthorityChain));
590 memcpy (acopy, iter, sizeof (struct AuthorityChain));
593 GNUNET_CONTAINER_DLL_insert (new_head, new_tail, acopy);
596 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
598 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
599 pb_key = GNUNET_CRYPTO_rsa_encode_key (key);
600 gph->key = GNUNET_CRYPTO_rsa_decode_key ((char*)pb_key, ntohs (pb_key->len));
601 //gph->key = key;//GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
602 //memcpy (gph->key, key, sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
604 GNUNET_CRYPTO_short_hash (&pkey,
605 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
607 gph->ahead = new_head;
609 shorten_authority_chain (gph);
613 * Initialize the resolver
615 * @param nh the namestore handle
616 * @param dh the dht handle
617 * @param lz the local zone's hash
618 * @param max_bg_queries maximum number of parallel background queries in dht
619 * @param ignore_pending ignore records that still require user confirmation
621 * @return GNUNET_OK on success
624 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
625 struct GNUNET_DHT_Handle *dh,
626 struct GNUNET_CRYPTO_ShortHashCode lz,
627 const struct GNUNET_CONFIGURATION_Handle *cfg,
628 unsigned long long max_bg_queries,
631 namestore_handle = nh;
635 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
636 max_allowed_background_queries = max_bg_queries;
637 ignore_pending_records = ignore_pending;
639 GNUNET_RESOLVER_connect (cfg);
641 if (NULL == vpn_handle)
643 vpn_handle = GNUNET_VPN_connect (cfg);
644 if (NULL == vpn_handle)
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
647 "GNS_PHASE_INIT: Error connecting to VPN!\n");
649 return GNUNET_SYSERR;
653 if ((namestore_handle != NULL) && (dht_handle != NULL))
658 return GNUNET_SYSERR;
662 * Cleanup background lookups
664 * @param cls closure to iterator
665 * @param node heap nodes
666 * @param element the resolver handle
667 * @param cost heap cost
668 * @return always GNUNET_YES
671 cleanup_pending_background_queries(void* cls,
672 struct GNUNET_CONTAINER_HeapNode *node,
674 GNUNET_CONTAINER_HeapCostType cost)
676 struct ResolverHandle *rh = (struct ResolverHandle *)element;
677 ResolverCleanupContinuation cont = cls;
679 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
680 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
682 GNUNET_DHT_get_stop(rh->get_handle);
683 rh->get_handle = NULL;
684 rh->proc(rh->proc_cls, rh, 0, NULL);
686 GNUNET_CONTAINER_heap_remove_node(node);
688 if (GNUNET_CONTAINER_heap_get_size(dht_lookup_heap) == 0)
700 gns_resolver_cleanup(ResolverCleanupContinuation cont)
702 unsigned int s = GNUNET_CONTAINER_heap_get_size(dht_lookup_heap);
703 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
704 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
707 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
708 &cleanup_pending_background_queries,
716 * Helper function to free resolver handle
718 * @param rh the handle to free
721 free_resolver_handle(struct ResolverHandle* rh)
723 struct AuthorityChain *ac;
724 struct AuthorityChain *ac_next;
729 ac = rh->authority_chain_head;
738 if (NULL != rh->dns_raw_packet)
739 GNUNET_free (rh->dns_raw_packet);
746 * Callback when record data is put into namestore
748 * @param cls the closure
749 * @param success GNUNET_OK on success
750 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
753 on_namestore_record_put_result(void *cls,
757 if (GNUNET_NO == success)
759 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
760 "GNS_NS: records already in namestore\n");
763 else if (GNUNET_YES == success)
765 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
766 "GNS_NS: records successfully put in namestore\n");
770 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
771 "GNS_NS: Error putting records into namestore: %s\n", emsg);
775 handle_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
777 struct ResolverHandle *rh = cls;
779 if (rh->timeout_cont)
780 rh->timeout_cont(rh->timeout_cont_cls, tc);
784 * Processor for background lookups in the DHT
786 * @param cls closure (NULL)
787 * @param rd_count number of records found (not 0)
788 * @param rd record data
791 background_lookup_result_processor(void *cls,
793 const struct GNUNET_NAMESTORE_RecordData *rd)
795 //We could do sth verbose/more useful here but it doesn't make any difference
796 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
797 "GNS_BG: background dht lookup for finished. (%d results)\n",
802 * Handle timeout for DHT requests
804 * @param cls the request handle as closure
805 * @param tc the task context
808 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
810 struct ResolverHandle *rh = cls;
811 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
812 char new_name[MAX_DNS_NAME_LENGTH];
814 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
815 "GNS_PHASE_REC-%d: dht lookup for query %s (%ds)timed out.\n",
816 rh->id, rh->name, rh->timeout.rel_value);
818 * Start resolution in bg
820 //strcpy(new_name, rh->name);
821 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
822 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
823 rh->name, GNUNET_GNS_TLD);
825 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
826 "GNS_PHASE_REC-%d: Starting background lookup for %s type %d\n",
827 rh->id, new_name, rlh->record_type);
829 gns_resolver_lookup_record(rh->authority,
830 rh->private_local_zone,
834 GNUNET_TIME_UNIT_FOREVER_REL,
836 &background_lookup_result_processor,
838 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
840 GNUNET_DHT_get_stop (rh->get_handle);
841 rh->get_handle = NULL;
842 rh->proc(rh->proc_cls, rh, 0, NULL);
847 * Function called when we get a result from the dht
848 * for our record query
850 * @param cls the request handle
851 * @param exp lifetime
852 * @param key the key the record was stored under
853 * @param get_path get path
854 * @param get_path_length get path length
855 * @param put_path put path
856 * @param put_path_length put path length
857 * @param type the block type
858 * @param size the size of the record
859 * @param data the record data
862 process_record_result_dht(void* cls,
863 struct GNUNET_TIME_Absolute exp,
864 const struct GNUNET_HashCode * key,
865 const struct GNUNET_PeerIdentity *get_path,
866 unsigned int get_path_length,
867 const struct GNUNET_PeerIdentity *put_path,
868 unsigned int put_path_length,
869 enum GNUNET_BLOCK_Type type,
870 size_t size, const void *data)
872 struct ResolverHandle *rh;
873 struct RecordLookupHandle *rlh;
874 struct GNSNameRecordBlock *nrb;
875 uint32_t num_records;
877 char* rd_data = (char*)data;
881 rh = (struct ResolverHandle *)cls;
882 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
883 "GNS_PHASE_REC-%d: got dht result (size=%d)\n", rh->id, size);
888 //FIXME maybe check expiration here, check block type
891 rlh = (struct RecordLookupHandle *) rh->proc_cls;
892 nrb = (struct GNSNameRecordBlock*)data;
894 /* stop lookup and timeout task */
895 GNUNET_DHT_get_stop (rh->get_handle);
896 rh->get_handle = NULL;
898 if (rh->dht_heap_node != NULL)
900 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
901 rh->dht_heap_node = NULL;
904 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
906 GNUNET_SCHEDULER_cancel(rh->timeout_task);
907 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
910 rh->get_handle = NULL;
911 name = (char*)&nrb[1];
912 num_records = ntohl(nrb->rd_count);
914 struct GNUNET_NAMESTORE_RecordData rd[num_records];
916 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
917 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
919 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
924 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
925 "GNS_PHASE_REC-%d: Error deserializing data!\n", rh->id);
929 for (i=0; i<num_records; i++)
931 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
932 "GNS_PHASE_REC-%d: Got name: %s (wanted %s)\n",
933 rh->id, name, rh->name);
934 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
935 "GNS_PHASE_REC-%d: Got type: %d\n",
936 rh->id, rd[i].record_type);
937 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
938 "GNS_PHASE_REC-%d: Got data length: %d\n",
939 rh->id, rd[i].data_size);
940 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
941 "GNS_PHASE_REC-%d: Got flag %d\n",
942 rh->id, rd[i].flags);
944 if ((strcmp(name, rh->name) == 0) &&
945 (rd[i].record_type == rlh->record_type))
953 * FIXME check pubkey against existing key in namestore?
954 * https://gnunet.org/bugs/view.php?id=2179
957 /* Save to namestore */
958 GNUNET_NAMESTORE_record_put (namestore_handle,
965 &on_namestore_record_put_result, //cont
970 rh->proc(rh->proc_cls, rh, num_records, rd);
972 rh->proc(rh->proc_cls, rh, 0, NULL);
979 * Start DHT lookup for a (name -> query->record_type) record in
980 * rh->authority's zone
982 * @param rh the pending gns query context
985 resolve_record_dht(struct ResolverHandle *rh)
988 struct GNUNET_CRYPTO_ShortHashCode name_hash;
989 struct GNUNET_HashCode lookup_key;
990 struct GNUNET_HashCode name_hash_double;
991 struct GNUNET_HashCode zone_hash_double;
992 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
993 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
994 struct ResolverHandle *rh_heap_root;
996 GNUNET_CRYPTO_short_hash(rh->name, strlen(rh->name), &name_hash);
997 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
998 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
999 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
1000 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
1002 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1003 "GNS_PHASE_REC-%d: starting dht lookup for %s with key: %s\n",
1004 rh->id, rh->name, (char*)&lookup_key_string);
1006 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1007 rh->dht_heap_node = NULL;
1009 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1012 * Update timeout if necessary
1014 if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
1017 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1018 "GNS_PHASE_REC-%d: Adjusting timeout\n", rh->id);
1020 * Set timeout for authority lookup phase to 1/2
1022 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
1023 GNUNET_TIME_relative_divide(rh->timeout, 2),
1024 &handle_lookup_timeout,
1027 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1028 // &dht_lookup_timeout,
1030 rh->timeout_cont = &dht_lookup_timeout;
1031 rh->timeout_cont_cls = rh;
1035 if (max_allowed_background_queries <=
1036 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1038 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1039 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
1040 rh_heap_root->get_handle = NULL;
1041 rh_heap_root->dht_heap_node = NULL;
1043 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1044 "GNS_PHASE_REC-%d: Replacing oldest background query for %s\n",
1045 rh->id, rh_heap_root->name);
1046 rh_heap_root->proc(rh_heap_root->proc_cls,
1051 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1053 GNUNET_TIME_absolute_get().abs_value);
1056 xquery = htonl(rlh->record_type);
1058 GNUNET_assert(rh->get_handle == NULL);
1059 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1060 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1062 DHT_GNS_REPLICATION_LEVEL,
1066 &process_record_result_dht,
1073 * Namestore calls this function if we have record for this name.
1074 * (or with rd_count=0 to indicate no matches)
1076 * @param cls the pending query
1077 * @param key the key of the zone we did the lookup
1078 * @param expiration expiration date of the namestore entry
1079 * @param name the name for which we need an authority
1080 * @param rd_count the number of records with 'name'
1081 * @param rd the record data
1082 * @param signature the signature of the authority for the record data
1085 process_record_result_ns(void* cls,
1086 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1087 struct GNUNET_TIME_Absolute expiration,
1088 const char *name, unsigned int rd_count,
1089 const struct GNUNET_NAMESTORE_RecordData *rd,
1090 const struct GNUNET_CRYPTO_RsaSignature *signature)
1092 struct ResolverHandle *rh;
1093 struct RecordLookupHandle *rlh;
1094 struct GNUNET_TIME_Relative remaining_time;
1095 struct GNUNET_CRYPTO_ShortHashCode zone;
1097 rh = (struct ResolverHandle *) cls;
1098 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1099 GNUNET_CRYPTO_short_hash(key,
1100 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1102 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1110 rh->status |= RSL_RECORD_EXISTS;
1113 if (remaining_time.rel_value == 0)
1115 rh->status |= RSL_RECORD_EXPIRED;
1121 * Lookup terminated and no results
1123 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1124 "GNS_PHASE_REC-%d: Namestore lookup for %s terminated without results\n",
1127 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1128 "GNS_PHASE_REC-%d: Record %s unknown in namestore\n",
1131 * Our zone and no result? Cannot resolve TT
1133 rh->proc(rh->proc_cls, rh, 0, NULL);
1140 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1141 "GNS_PHASE_REC-%d: Processing additional result %s from namestore\n",
1144 for (i=0; i<rd_count;i++)
1147 if (rd[i].record_type != rlh->record_type)
1150 if (ignore_pending_records &&
1151 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1153 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1154 "GNS_PHASE_REC-%d: Record %s is awaiting user confirmation. Skipping\n",
1159 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1162 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1163 "GNS_PHASE_REC-%d: This record is expired. Skipping\n",
1175 if (rh->answered == 0)
1177 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1178 "GNS_PHASE_REC-%d: No answers found. This is odd!\n", rh->id);
1179 rh->proc(rh->proc_cls, rh, 0, NULL);
1183 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1184 "GNS_PHASE_REC-%d: Found %d answer(s) to query in %d records!\n",
1185 rh->id, rh->answered, rd_count);
1187 rh->proc(rh->proc_cls, rh, rd_count, rd);
1193 * VPN redirect result callback
1195 * @param cls the resolver handle
1196 * @param af the requested address family
1197 * @param address in_addr(6) respectively
1200 process_record_result_vpn (void* cls, int af, const void *address)
1202 struct ResolverHandle *rh = cls;
1203 struct RecordLookupHandle *rlh;
1204 struct GNUNET_NAMESTORE_RecordData rd;
1206 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1208 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1209 "GNS_PHASE_REC_VPN-%d: Got answer from VPN to query!\n",
1213 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1214 "GNS_PHASE_REC-%d: Answer is IPv4!\n",
1216 if (rlh->record_type != GNUNET_GNS_RECORD_TYPE_A)
1218 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1219 "GNS_PHASE_REC-%d: Requested record is not IPv4!\n",
1221 rh->proc (rh->proc_cls, rh, 0, NULL);
1224 rd.record_type = GNUNET_GNS_RECORD_TYPE_A;
1225 rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
1227 rd.data_size = sizeof (struct in_addr);
1229 rh->proc (rh->proc_cls, rh, 1, &rd);
1232 else if (af == AF_INET6)
1234 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1235 "GNS_PHASE_REC-%d: Answer is IPv6!\n",
1237 if (rlh->record_type != GNUNET_GNS_RECORD_AAAA)
1239 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1240 "GNS_PHASE_REC-%d: Requested record is not IPv6!\n",
1242 rh->proc (rh->proc_cls, rh, 0, NULL);
1245 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1246 rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
1248 rd.data_size = sizeof (struct in6_addr);
1250 rh->proc (rh->proc_cls, rh, 1, &rd);
1254 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1255 "GNS_PHASE_REC-%d: Got garbage from VPN!\n",
1257 rh->proc (rh->proc_cls, rh, 0, NULL);
1264 * @param rh resolver handle
1265 * @param rlh record lookup handle
1266 * @param rd_count number of results
1270 finish_lookup(struct ResolverHandle *rh,
1271 struct RecordLookupHandle* rlh,
1272 unsigned int rd_count,
1273 const struct GNUNET_NAMESTORE_RecordData *rd);
1276 * Process VPN lookup result for record
1278 * @param cls the record lookup handle
1279 * @param rh resolver handle
1280 * @param rd_count number of results (1)
1281 * @param rd record data containing the result
1284 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1285 unsigned int rd_count,
1286 const struct GNUNET_NAMESTORE_RecordData *rd)
1288 struct RecordLookupHandle* rlh = (struct RecordLookupHandle*) cls;
1292 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1293 "GNS_PHASE_REC_VPN-%d: VPN returned no records. (status: %d)!\n",
1296 /* give up, cannot resolve */
1297 finish_lookup(rh, rlh, 0, NULL);
1298 free_resolver_handle(rh);
1302 /* results found yay */
1303 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1304 "GNS_PHASE_REC_VPN-%d: Record resolved from VPN!", rh->id);
1306 finish_lookup(rh, rlh, rd_count, rd);
1308 free_resolver_handle(rh);
1313 * Sends a UDP dns query to a nameserver specified in the rh
1315 * @param rh the resolver handle
1318 send_dns_packet (struct ResolverHandle *rh);
1321 handle_dns_resolver (void *cls,
1322 const struct sockaddr *addr,
1325 struct ResolverHandle *rh = cls;
1326 struct RecordLookupHandle *rlh = rh->proc_cls;
1327 struct GNUNET_NAMESTORE_RecordData rd;
1328 struct sockaddr_in *sai;
1329 struct sockaddr_in6 *sai6;
1333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1334 "No address found in DNS!\n");
1335 finish_lookup (rh, rlh, 0, NULL);
1336 free_resolver_handle (rh);
1340 if (addrlen == sizeof (struct sockaddr_in))
1342 sai = (struct sockaddr_in*) addr;
1343 rd.record_type = GNUNET_GNS_RECORD_TYPE_A;
1344 rd.data_size = sizeof (struct in_addr);
1345 rd.data = &sai->sin_addr;
1349 sai6 = (struct sockaddr_in6*) addr;
1350 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1351 rd.data_size = sizeof (struct in6_addr);
1352 rd.data = &sai6->sin6_addr;
1355 rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
1357 finish_lookup (rh, rlh, 1, &rd);
1358 free_resolver_handle (rh);
1359 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
1363 * Resolve DNS name via local stub resolver
1365 * @param rh the resolver handle
1368 resolve_dns_name (struct ResolverHandle *rh)
1370 struct RecordLookupHandle *rlh = rh->proc_cls;
1373 if ((rlh->record_type != GNUNET_GNS_RECORD_TYPE_A) &&
1374 (rlh->record_type != GNUNET_GNS_RECORD_AAAA))
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1377 "Can only resolve A/AAAA via stub... abort\n");
1378 finish_lookup (rh, rlh, 0, NULL);
1379 free_resolver_handle (rh);
1383 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_A)
1388 //GNUNET_RESOLVER_connect (cfg); FIXME into init
1390 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1393 &handle_dns_resolver,
1399 * Read DNS udp packet from socket
1401 * @param cls the resolver handle
1402 * @param tc task context
1405 read_dns_response (void *cls,
1406 const struct GNUNET_SCHEDULER_TaskContext *tc)
1408 struct ResolverHandle *rh = cls;
1409 struct RecordLookupHandle *rlh = rh->proc_cls;
1410 char buf[UINT16_MAX];
1412 struct sockaddr_in addr;
1414 struct GNUNET_DNSPARSER_Packet *packet;
1415 struct GNUNET_NAMESTORE_RecordData rd;
1416 int found_delegation = GNUNET_NO;
1417 int found_cname = GNUNET_NO;
1418 char* delegation_name = NULL;
1419 int zone_offset = 0;
1422 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1423 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1425 /* timeout or shutdown */
1426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1427 "Terminating DNS query\n");
1428 finish_lookup (rh, rlh, 0, NULL);
1429 GNUNET_NETWORK_socket_close (rh->dns_sock);
1430 free_resolver_handle (rh);
1434 addrlen = sizeof (addr);
1435 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1437 (struct sockaddr*) &addr,
1442 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1443 finish_lookup (rh, rlh, 0, NULL);
1444 GNUNET_NETWORK_socket_close (rh->dns_sock);
1445 free_resolver_handle (rh);
1449 packet = GNUNET_DNSPARSER_parse (buf, r);
1453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1454 "Failed to parse DNS reply!\n");
1455 finish_lookup (rh, rlh, 0, NULL);
1456 GNUNET_NETWORK_socket_close (rh->dns_sock);
1457 free_resolver_handle (rh);
1461 for (i = 0; i < packet->num_answers; i++)
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464 "Got record type %d (want %d)\n",
1465 packet->answers[i].type,
1467 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1468 if (packet->answers[i].type == GNUNET_GNS_RECORD_TYPE_CNAME)
1470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1471 "CNAME... restarting query with %s\n",
1472 packet->answers[i].data.hostname
1474 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1475 found_cname = GNUNET_YES;
1476 //send_dns_packet (rh);
1477 //GNUNET_DNSPARSER_free_packet (packet);
1481 if ((packet->answers[i].type == rlh->record_type) &&
1482 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1486 rd.data = packet->answers[i].data.raw.data;
1487 rd.data_size = packet->answers[i].data.raw.data_len;
1488 rd.record_type = packet->answers[i].type;
1490 rd.expiration = packet->answers[i].expiration_time;
1491 finish_lookup (rh, rlh, 1, &rd);
1492 GNUNET_NETWORK_socket_close (rh->dns_sock);
1493 GNUNET_DNSPARSER_free_packet (packet);
1494 free_resolver_handle (rh);
1499 if (GNUNET_YES == found_cname)
1501 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1503 if (0 > zone_offset)
1506 /* restart query with CNAME */
1507 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1510 "Asking same server for %s\n", rh->dns_name);
1511 send_dns_packet (rh);
1515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1516 "Trying system resolver for %s\n", rh->dns_name);
1517 resolve_dns_name (rh);
1520 GNUNET_DNSPARSER_free_packet (packet);
1524 for (i = 0; i < packet->num_authority_records; i++)
1527 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_TYPE_NS)
1529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1530 "Found NS delegation!\n");
1531 found_delegation = GNUNET_YES;
1532 delegation_name = packet->authority_records[i].data.hostname;
1537 for (i = 0; i < packet->num_additional_records; i++)
1539 if (found_delegation == GNUNET_NO)
1542 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_TYPE_A) &&
1543 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1545 GNUNET_assert (sizeof (struct in_addr) ==
1546 packet->authority_records[i].data.raw.data_len);
1548 rh->dns_addr.sin_addr =
1549 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1550 send_dns_packet (rh);
1551 GNUNET_DNSPARSER_free_packet (packet);
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1557 "Nothing useful in DNS reply!\n");
1558 finish_lookup (rh, rlh, 0, NULL);
1559 GNUNET_NETWORK_socket_close (rh->dns_sock);
1560 free_resolver_handle (rh);
1561 GNUNET_DNSPARSER_free_packet (packet);
1566 * Sends a UDP dns query to a nameserver specified in the rh
1568 * @param rh the request handle
1571 send_dns_packet (struct ResolverHandle *rh)
1573 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1574 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1576 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1578 rh->dns_raw_packet_size,
1579 (struct sockaddr*)&rh->dns_addr,
1580 sizeof (struct sockaddr_in));
1582 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1583 rh->timeout, //FIXME less?
1589 GNUNET_NETWORK_fdset_destroy (rset);
1594 * The final phase of resoution.
1595 * We found a NS RR and want to resolve via DNS
1597 * @param rh the pending lookup handle
1598 * @param rd_count length of record data
1599 * @param rd record data containing VPN RR
1602 resolve_record_dns (struct ResolverHandle *rh,
1604 const struct GNUNET_NAMESTORE_RecordData *rd)
1606 struct GNUNET_DNSPARSER_Query query;
1607 struct GNUNET_DNSPARSER_Packet packet;
1608 struct GNUNET_DNSPARSER_Flags flags;
1609 struct in_addr dnsip;
1610 struct sockaddr_in addr;
1611 struct sockaddr *sa;
1613 struct RecordLookupHandle *rlh = rh->proc_cls;
1615 /* We cancel here as to not include the ns lookup in the timeout */
1616 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1618 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1619 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1621 /* Start shortening */
1622 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1625 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1627 start_shorten (rh->authority_chain_tail,
1631 for (i = 0; i < rd_count; i++)
1633 /* Synthesize dns name */
1634 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS)
1636 strcpy (rh->dns_zone, (char*)rd[i].data);
1637 if (0 == strcmp (rh->name, ""))
1638 strcpy (rh->dns_name, (char*)rd[i].data);
1640 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1643 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
1644 dnsip = *((struct in_addr*)rd[i].data);
1647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1648 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1652 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1653 if (rh->dns_sock == NULL)
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1656 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1658 finish_lookup (rh, rlh, 0, NULL);
1659 free_resolver_handle (rh);
1663 memset (&addr, 0, sizeof (struct sockaddr_in));
1664 sa = (struct sockaddr *) &addr;
1665 sa->sa_family = AF_INET;
1666 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1668 sizeof (struct sockaddr_in)))
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1671 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1673 GNUNET_NETWORK_socket_close (rh->dns_sock);
1674 finish_lookup (rh, rlh, 0, NULL);
1675 free_resolver_handle (rh);
1679 /*TODO create dnsparser query, serialize, sendto, handle reply*/
1680 query.name = rh->dns_name;
1681 query.type = rlh->record_type;
1682 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1683 memset (&flags, 0, sizeof (flags));
1684 flags.recursion_desired = 1;
1685 flags.checking_disabled = 1;
1686 packet.queries = &query;
1687 packet.answers = NULL;
1688 packet.authority_records = NULL;
1689 packet.num_queries = 1;
1690 packet.num_answers = 0;
1691 packet.num_authority_records = 0;
1692 packet.num_additional_records = 0;
1693 packet.flags = flags;
1695 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1697 &rh->dns_raw_packet,
1698 &rh->dns_raw_packet_size))
1700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1701 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1703 GNUNET_NETWORK_socket_close (rh->dns_sock);
1704 finish_lookup (rh, rlh, 0, NULL);
1705 free_resolver_handle (rh);
1709 rh->dns_addr.sin_family = AF_INET;
1710 rh->dns_addr.sin_port = htons (53); //domain
1711 rh->dns_addr.sin_addr = dnsip;
1712 #if HAVE_SOCKADDR_IN_SIN_LEN
1713 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1716 send_dns_packet (rh);
1721 * The final phase of resoution.
1722 * We found a VPN RR and want to request an IPv4/6 address
1724 * @param rh the pending lookup handle
1725 * @param rd_count length of record data
1726 * @param rd record data containing VPN RR
1729 resolve_record_vpn (struct ResolverHandle *rh,
1731 const struct GNUNET_NAMESTORE_RecordData *rd)
1735 struct GNUNET_HashCode peer_id;
1736 struct GNUNET_CRYPTO_HashAsciiEncoded s_pid;
1737 struct GNUNET_HashCode serv_desc;
1738 struct GNUNET_CRYPTO_HashAsciiEncoded s_sd;
1740 size_t len = (sizeof (uint32_t) * 2) + (sizeof (struct GNUNET_HashCode) * 2);
1742 /* We cancel here as to not include the ns lookup in the timeout */
1743 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1745 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1746 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1748 /* Start shortening */
1749 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1752 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1754 start_shorten (rh->authority_chain_tail,
1758 /* Extracting VPN information FIXME rd parsing with NS API?*/
1759 if (len != rd->data_size)
1761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1762 "GNS_PHASE_REC_VPN-%llu: Error parsing VPN RR!\n",
1764 finish_lookup (rh, rh->proc_cls, 0, NULL);
1765 free_resolver_handle (rh);
1770 memcpy (&af, pos, sizeof (uint32_t));
1771 pos += sizeof (uint32_t);
1772 memcpy (&proto, pos, sizeof (uint32_t));
1773 pos += sizeof (uint32_t);
1774 memcpy (&s_pid, pos, sizeof (struct GNUNET_HashCode));
1775 pos += sizeof (struct GNUNET_HashCode);
1776 memcpy (&s_sd, pos, sizeof (struct GNUNET_HashCode));
1779 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_pid, &peer_id)) ||
1780 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_sd, &serv_desc)))
1782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1783 "GNS_PHASE_REC_VPN-%llu: Error parsing VPN RR hashes!\n",
1785 finish_lookup (rh, rh->proc_cls, 0, NULL);
1786 free_resolver_handle (rh);
1790 rh->proc = &handle_record_vpn;
1792 if (NULL == vpn_handle)
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1795 "GNS_PHASE_REC_VPN-%llu: VPN not connected!\n",
1797 finish_lookup (rh, rh->proc_cls, 0, NULL);
1798 free_resolver_handle (rh);
1803 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1805 (struct GNUNET_PeerIdentity*)&peer_id,
1808 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1809 &process_record_result_vpn,
1815 * The final phase of resolution.
1816 * rh->name is a name that is canonical and we do not have a delegation.
1817 * Query namestore for this record
1819 * @param rh the pending lookup handle
1822 resolve_record_ns(struct ResolverHandle *rh)
1824 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1826 /* We cancel here as to not include the ns lookup in the timeout */
1827 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1829 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1830 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1832 /* Start shortening */
1833 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1836 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1838 start_shorten (rh->authority_chain_tail,
1843 * Try to resolve this record in our namestore.
1844 * The name to resolve is now in rh->authority_name
1845 * since we tried to resolve it to an authority
1848 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1852 &process_record_result_ns,
1859 * Handle timeout for DHT requests
1861 * @param cls the request handle as closure
1862 * @param tc the task context
1865 dht_authority_lookup_timeout(void *cls,
1866 const struct GNUNET_SCHEDULER_TaskContext *tc)
1868 struct ResolverHandle *rh = cls;
1869 struct RecordLookupHandle *rlh = rh->proc_cls;
1870 char new_name[MAX_DNS_NAME_LENGTH];
1872 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1873 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%ds)timed out.\n",
1874 rh->id, rh->authority_name, rh->timeout.rel_value);
1876 rh->status |= RSL_TIMED_OUT;
1878 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1880 GNUNET_DHT_get_stop (rh->get_handle);
1881 rh->get_handle = NULL;
1883 if (strcmp(rh->name, "") == 0)
1886 * promote authority back to name and try to resolve record
1888 strcpy(rh->name, rh->authority_name);
1889 rh->proc(rh->proc_cls, rh, 0, NULL);
1894 * Start resolution in bg
1896 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
1897 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
1898 //strcpy(new_name, rh->name);
1899 //strcpy(new_name+strlen(new_name), ".");
1900 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1902 strcpy(rh->name, new_name);
1904 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1905 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1906 rh->id, rh->name, rlh->record_type);
1908 gns_resolver_lookup_record(rh->authority,
1909 rh->private_local_zone,
1913 GNUNET_TIME_UNIT_FOREVER_REL,
1915 &background_lookup_result_processor,
1918 rh->proc(rh->proc_cls, rh, 0, NULL);
1922 static void resolve_delegation_dht(struct ResolverHandle *rh);
1925 static void resolve_delegation_ns(struct ResolverHandle *rh);
1929 * Namestore resolution for delegation finished. Processing result.
1931 * @param cls the closure
1932 * @param rh resolver handle
1933 * @param rd_count number of results (always 0)
1934 * @param rd record data (always NULL)
1937 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1938 unsigned int rd_count,
1939 const struct GNUNET_NAMESTORE_RecordData *rd);
1943 * Function called when we get a result from the dht
1944 * for our query. Recursively tries to resolve authorities
1947 * @param cls the request handle
1948 * @param exp lifetime
1949 * @param key the key the record was stored under
1950 * @param get_path get path
1951 * @param get_path_length get path length
1952 * @param put_path put path
1953 * @param put_path_length put path length
1954 * @param type the block type
1955 * @param size the size of the record
1956 * @param data the record data
1959 process_delegation_result_dht(void* cls,
1960 struct GNUNET_TIME_Absolute exp,
1961 const struct GNUNET_HashCode * key,
1962 const struct GNUNET_PeerIdentity *get_path,
1963 unsigned int get_path_length,
1964 const struct GNUNET_PeerIdentity *put_path,
1965 unsigned int put_path_length,
1966 enum GNUNET_BLOCK_Type type,
1967 size_t size, const void *data)
1969 struct ResolverHandle *rh;
1970 struct GNSNameRecordBlock *nrb;
1971 uint32_t num_records;
1973 char* rd_data = (char*) data;
1976 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
1977 struct GNUNET_HashCode zone_hash_double, name_hash_double;
1979 rh = (struct ResolverHandle *)cls;
1981 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1982 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
1987 nrb = (struct GNSNameRecordBlock*)data;
1989 /* stop dht lookup and timeout task */
1990 GNUNET_DHT_get_stop (rh->get_handle);
1992 rh->get_handle = NULL;
1994 if (rh->dht_heap_node != NULL)
1996 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
1997 rh->dht_heap_node = NULL;
2000 num_records = ntohl(nrb->rd_count);
2001 name = (char*)&nrb[1];
2003 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2005 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2006 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2008 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2013 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2014 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2019 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2020 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2021 rh->id, name, rh->authority_name);
2022 for (i=0; i<num_records; i++)
2025 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2026 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2027 rh->id, name, rh->authority_name);
2028 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2029 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2030 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2031 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2032 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2033 rh->id, rd[i].data_size);
2034 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2035 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2036 rh->id, rd[i].flags);
2038 if ((rd[i].record_type == GNUNET_GNS_RECORD_VPN) ||
2039 (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS) ||
2040 (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_CNAME))
2043 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2045 if (strcmp(rh->name, "") == 0)
2046 strcpy(rh->name, rh->authority_name);
2048 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2049 rh->name, rh->authority_name); //FIXME ret
2054 if ((strcmp(name, rh->authority_name) == 0) &&
2055 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
2057 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2058 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2061 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2062 struct AuthorityChain *auth =
2063 GNUNET_malloc(sizeof(struct AuthorityChain));
2064 auth->zone = rh->authority;
2065 memset(auth->name, 0, strlen(rh->authority_name)+1);
2066 strcpy(auth->name, rh->authority_name);
2067 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2068 rh->authority_chain_tail,
2071 /** try to import pkey if private key available */
2072 //if (rh->priv_key && is_canonical (rh->name))
2073 // process_discovered_authority(name, auth->zone,
2074 // rh->authority_chain_tail->zone,
2081 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2082 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2083 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2084 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2086 /* Save to namestore */
2087 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
2090 GNUNET_NAMESTORE_record_put (namestore_handle,
2097 &on_namestore_record_put_result, //cont
2107 * FIXME in this case. should we ask namestore again?
2109 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2110 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2111 rh->id, rh->authority_name, rh->name);
2113 if (strcmp(rh->name, "") == 0)
2115 /* Start shortening */
2116 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2119 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2121 start_shorten (rh->authority_chain_tail,
2125 rh->proc(rh->proc_cls, rh, 0, NULL);
2129 rh->proc = &handle_delegation_ns;
2130 resolve_delegation_ns (rh);
2136 * No pkey but name exists
2139 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2140 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2141 rh->id, rh->authority_name, rh->name);
2142 if (strcmp(rh->name, "") == 0)
2143 strcpy(rh->name, rh->authority_name);
2145 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2146 rh->name, rh->authority_name); //FIXME ret
2148 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2149 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2150 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2151 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2153 rh->proc(rh->proc_cls, rh, 0, NULL);
2156 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2157 +(MAX_DNS_NAME_LENGTH*2)
2158 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2162 expand_plus(char** dest, char* src, char* repl)
2165 unsigned int s_len = strlen(src)+1;
2167 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2168 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2173 "GNS_POSTPROCESS: %s to short\n", src);
2175 /* no postprocessing */
2176 memcpy(*dest, src, s_len+1);
2180 if (0 == strcmp(src+s_len-3, ".+"))
2182 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2183 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2184 memset(*dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2186 pos = *dest+s_len-2;
2188 pos += strlen(repl);
2189 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2190 "GNS_POSTPROCESS: Expanded to %s\n", *dest);
2194 memcpy(*dest, src, s_len+1);
2202 finish_lookup(struct ResolverHandle *rh,
2203 struct RecordLookupHandle* rlh,
2204 unsigned int rd_count,
2205 const struct GNUNET_NAMESTORE_RecordData *rd)
2208 char new_rr_data[MAX_DNS_NAME_LENGTH];
2209 char new_mx_data[MAX_MX_LENGTH];
2210 char new_soa_data[MAX_SOA_LENGTH];
2211 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2214 unsigned int offset;
2216 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2218 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2219 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2223 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2225 for (i = 0; i < rd_count; i++)
2228 if (rd[i].record_type != GNUNET_GNS_RECORD_TYPE_NS &&
2229 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_CNAME &&
2230 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
2231 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_SOA)
2233 p_rd[i].data = rd[i].data;
2238 * for all those records we 'should'
2239 * also try to resolve the A/AAAA records (RFC1035)
2240 * This is a feature and not important
2243 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2244 "GNS_POSTPROCESS: Postprocessing\n");
2246 if (strcmp(rh->name, "+") == 0)
2247 repl_string = rlh->name;
2249 repl_string = rlh->name+strlen(rh->name)+1;
2252 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
2254 memcpy(new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2255 offset = sizeof(uint16_t);
2256 pos = new_mx_data+offset;
2257 expand_plus(&pos, (char*)rd[i].data+sizeof(uint16_t),
2259 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2260 p_rd[i].data = new_mx_data;
2261 p_rd[i].data_size = offset;
2263 else if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_SOA)
2265 /* expand mname and rname */
2267 expand_plus(&pos, (char*)rd[i].data, repl_string);
2268 offset = strlen(new_soa_data)+1;
2269 pos = new_soa_data+offset;
2270 expand_plus(&pos, (char*)rd[i].data+offset, repl_string);
2271 offset += strlen(new_soa_data+offset)+1;
2272 /* cpy the 4 numbers serial refresh retry and expire */
2273 memcpy(new_soa_data+offset, (char*)rd[i].data+offset, sizeof(uint32_t)*5);
2274 offset += sizeof(uint32_t)*5;
2275 p_rd[i].data_size = offset;
2276 p_rd[i].data = new_soa_data;
2281 expand_plus(&pos, (char*)rd[i].data, repl_string);
2282 p_rd[i].data_size = strlen(new_rr_data)+1;
2283 p_rd[i].data = new_rr_data;
2288 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2294 * Process DHT lookup result for record.
2296 * @param cls the closure
2297 * @param rh resolver handle
2298 * @param rd_count number of results
2299 * @param rd record data
2302 handle_record_dht(void* cls, struct ResolverHandle *rh,
2303 unsigned int rd_count,
2304 const struct GNUNET_NAMESTORE_RecordData *rd)
2306 struct RecordLookupHandle* rlh;
2308 rlh = (struct RecordLookupHandle*)cls;
2311 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2312 "GNS_PHASE_REC-%d: No records for %s found in DHT. Aborting\n",
2314 /* give up, cannot resolve */
2315 finish_lookup(rh, rlh, 0, NULL);
2316 free_resolver_handle(rh);
2320 /* results found yay */
2321 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2322 "GNS_PHASE_REC-%d: Record resolved from DHT!", rh->id);
2324 finish_lookup(rh, rlh, rd_count, rd);
2325 free_resolver_handle(rh);
2333 * Process namestore lookup result for record.
2335 * @param cls the closure
2336 * @param rh resolver handle
2337 * @param rd_count number of results
2338 * @param rd record data
2341 handle_record_ns (void* cls, struct ResolverHandle *rh,
2342 unsigned int rd_count,
2343 const struct GNUNET_NAMESTORE_RecordData *rd)
2345 struct RecordLookupHandle* rlh;
2346 rlh = (struct RecordLookupHandle*) cls;
2349 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2350 "GNS_PHASE_REC-%d: NS returned no records. (status: %d)!\n",
2355 * There are 5 conditions that have to met for us to consult the DHT:
2356 * 1. The entry in the DHT is RSL_RECORD_EXPIRED AND
2357 * 2. No entry in the NS existed AND
2358 * 3. The zone queried is not the local resolver's zone AND
2359 * 4. The name that was looked up is '+'
2360 * because if it was any other canonical name we either already queried
2361 * the DHT for the authority in the authority lookup phase (and thus
2362 * would already have an entry in the NS for the record)
2363 * 5. We are not in cache only mode
2365 if (rh->status & (RSL_RECORD_EXPIRED | !RSL_RECORD_EXISTS) &&
2366 GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2367 &rh->private_local_zone) &&
2368 (strcmp(rh->name, "+") == 0) &&
2369 (rh->only_cached == GNUNET_NO))
2371 rh->proc = &handle_record_dht;
2372 resolve_record_dht(rh);
2375 /* give up, cannot resolve */
2376 finish_lookup(rh, rlh, 0, NULL);
2377 free_resolver_handle(rh);
2381 /* results found yay */
2382 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2383 "GNS_PHASE_REC-%d: Record resolved from namestore!", rh->id);
2385 finish_lookup(rh, rlh, rd_count, rd);
2387 free_resolver_handle(rh);
2393 * Move one level up in the domain hierarchy and return the
2394 * passed top level domain.
2396 * @param name the domain
2397 * @param dest the destination where the tld will be put
2400 pop_tld(char* name, char* dest)
2404 if (is_canonical(name))
2411 for (len = strlen(name); len > 0; len--)
2413 if (*(name+len) == '.')
2423 strcpy(dest, (name+len+1));
2427 * Checks if name is in tld
2429 * @param name the name to check
2430 * @param tld the TLD to check for
2431 * @return GNUNET_YES or GNUNET_NO
2434 is_tld(const char* name, const char* tld)
2438 if (strlen(name) <= strlen(tld))
2443 offset = strlen(name)-strlen(tld);
2444 if (strcmp(name+offset, tld) != 0)
2446 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2447 "%s is not in .%s TLD\n", name, tld);
2454 * DHT resolution for delegation finished. Processing result.
2456 * @param cls the closure
2457 * @param rh resolver handle
2458 * @param rd_count number of results (always 0)
2459 * @param rd record data (always NULL)
2462 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2463 unsigned int rd_count,
2464 const struct GNUNET_NAMESTORE_RecordData *rd)
2466 struct RecordLookupHandle* rlh;
2467 rlh = (struct RecordLookupHandle*) cls;
2470 if (strcmp(rh->name, "") == 0)
2472 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
2474 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2475 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2477 finish_lookup(rh, rlh, rd_count, rd);
2478 free_resolver_handle(rh);
2481 /* We resolved full name for delegation. resolving record */
2482 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2483 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2485 strcpy(rh->name, "+\0");
2486 rh->proc = &handle_record_ns;
2487 resolve_record_ns(rh);
2492 * we still have some left
2494 if (is_canonical(rh->name))
2496 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2497 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2500 rh->proc = &handle_record_ns;
2501 resolve_record_ns(rh);
2504 /* give up, cannot resolve */
2505 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2506 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2508 finish_lookup(rh, rlh, 0, NULL);
2509 free_resolver_handle(rh);
2514 * Start DHT lookup for a name -> PKEY (compare NS) record in
2515 * rh->authority's zone
2517 * @param rh the pending gns query
2520 resolve_delegation_dht(struct ResolverHandle *rh)
2523 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2524 struct GNUNET_HashCode name_hash_double;
2525 struct GNUNET_HashCode zone_hash_double;
2526 struct GNUNET_HashCode lookup_key;
2527 struct ResolverHandle *rh_heap_root;
2529 pop_tld(rh->name, rh->authority_name);
2530 GNUNET_CRYPTO_short_hash(rh->authority_name,
2531 strlen(rh->authority_name),
2533 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2534 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2535 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2537 rh->dht_heap_node = NULL;
2539 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2541 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
2542 // &dht_authority_lookup_timeout,
2544 rh->timeout_cont = &dht_authority_lookup_timeout;
2545 rh->timeout_cont_cls = rh;
2549 if (max_allowed_background_queries <=
2550 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2552 /* terminate oldest lookup */
2553 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2554 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
2555 rh_heap_root->dht_heap_node = NULL;
2557 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2558 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2559 rh->id, rh_heap_root->authority_name);
2561 rh_heap_root->proc(rh_heap_root->proc_cls,
2566 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2568 GNUNET_TIME_absolute_get().abs_value);
2571 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2573 GNUNET_assert(rh->get_handle == NULL);
2574 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2575 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2577 DHT_GNS_REPLICATION_LEVEL,
2581 &process_delegation_result_dht,
2588 * Namestore resolution for delegation finished. Processing result.
2590 * @param cls the closure
2591 * @param rh resolver handle
2592 * @param rd_count number of results (always 0)
2593 * @param rd record data (always NULL)
2596 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2597 unsigned int rd_count,
2598 const struct GNUNET_NAMESTORE_RecordData *rd)
2600 struct RecordLookupHandle* rlh;
2601 rlh = (struct RecordLookupHandle*) cls;
2603 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2604 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2605 rh->id, rh->status);
2607 if (strcmp(rh->name, "") == 0)
2610 /* We resolved full name for delegation. resolving record */
2611 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2612 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2614 if (rh->status & RSL_CNAME_FOUND)
2616 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_CNAME)
2618 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2619 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2621 finish_lookup(rh, rlh, rd_count, rd);
2622 free_resolver_handle(rh);
2626 /* A CNAME can only occur alone */
2627 GNUNET_assert (is_canonical ((char*)rd->data));
2628 strcpy (rh->name, rd->data);
2629 resolve_delegation_ns (rh);
2632 else if (rh->status & RSL_DELEGATE_VPN)
2634 if (rlh->record_type == GNUNET_GNS_RECORD_VPN)
2636 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2637 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2639 finish_lookup(rh, rlh, rd_count, rd);
2640 free_resolver_handle(rh);
2643 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2644 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2646 GNUNET_assert (NULL != rd);
2647 rh->proc = &handle_record_vpn;
2648 resolve_record_vpn (rh, rd_count, rd);
2650 else if (rh->status & RSL_DELEGATE_NS)
2652 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_NS)
2654 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2655 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2657 finish_lookup(rh, rlh, rd_count, rd);
2658 free_resolver_handle(rh);
2662 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2663 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2665 GNUNET_assert (NULL != rd);
2666 rh->proc = &handle_record_ns;
2667 resolve_record_dns (rh, rd_count, rd);
2669 else if (rh->status & RSL_DELEGATE_PKEY)
2671 if (rlh->record_type == GNUNET_GNS_RECORD_PKEY)
2673 GNUNET_assert(rd_count == 1);
2674 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2675 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
2677 finish_lookup(rh, rlh, rd_count, rd);
2678 free_resolver_handle(rh);
2684 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2685 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
2687 strcpy(rh->name, "+\0");
2688 rh->proc = &handle_record_ns;
2689 resolve_record_ns(rh);
2694 if (rh->status & RSL_DELEGATE_NS)
2696 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_NS)
2698 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2699 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2701 finish_lookup(rh, rlh, rd_count, rd);
2702 free_resolver_handle(rh);
2706 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2707 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2709 GNUNET_assert (NULL != rd);
2710 rh->proc = &handle_record_ns;
2711 resolve_record_dns (rh, rd_count, rd);
2716 * we still have some left
2717 * check if authority in ns is fresh
2719 * or we are authority
2721 if (((rh->status & RSL_RECORD_EXISTS) && (!(rh->status & RSL_RECORD_EXPIRED)))
2722 || !GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2723 &rh->private_local_zone))
2725 if (is_canonical(rh->name))
2727 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2728 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
2731 rh->proc = &handle_record_ns;
2732 resolve_record_ns(rh);
2736 /* give up, cannot resolve */
2737 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2738 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
2741 finish_lookup(rh, rlh, rd_count, rd);
2742 //rlh->proc(rlh->proc_cls, 0, NULL);
2747 if (rh->only_cached == GNUNET_YES)
2749 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2750 "GNS_PHASE_DELEGATE_NS-%llu: Only cache resolution, no result\n",
2752 finish_lookup(rh, rlh, rd_count, rd);
2756 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2757 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
2759 rh->proc = &handle_delegation_dht;
2760 resolve_delegation_dht(rh);
2766 * This is a callback function that should give us only PKEY
2767 * records. Used to query the namestore for the authority (PKEY)
2768 * for 'name'. It will recursively try to resolve the
2769 * authority for a given name from the namestore.
2771 * @param cls the pending query
2772 * @param key the key of the zone we did the lookup
2773 * @param expiration expiration date of the record data set in the namestore
2774 * @param name the name for which we need an authority
2775 * @param rd_count the number of records with 'name'
2776 * @param rd the record data
2777 * @param signature the signature of the authority for the record data
2780 process_delegation_result_ns(void* cls,
2781 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2782 struct GNUNET_TIME_Absolute expiration,
2784 unsigned int rd_count,
2785 const struct GNUNET_NAMESTORE_RecordData *rd,
2786 const struct GNUNET_CRYPTO_RsaSignature *signature)
2788 struct ResolverHandle *rh;
2789 struct GNUNET_TIME_Relative remaining_time;
2790 struct GNUNET_CRYPTO_ShortHashCode zone;
2791 char new_name[MAX_DNS_NAME_LENGTH];
2793 rh = (struct ResolverHandle *)cls;
2794 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2795 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
2798 GNUNET_CRYPTO_short_hash(key,
2799 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2801 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2807 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2808 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
2810 rh->status |= RSL_RECORD_EXISTS;
2813 if (remaining_time.rel_value == 0)
2815 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2816 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
2818 rh->status |= RSL_RECORD_EXPIRED;
2822 * No authority found in namestore.
2827 * We did not find an authority in the namestore
2832 * Promote this authority back to a name maybe it is
2835 if (strcmp(rh->name, "") == 0)
2837 /* simply promote back */
2838 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2839 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
2840 rh->id, rh->authority_name);
2841 strcpy(rh->name, rh->authority_name);
2845 /* add back to existing name */
2846 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2847 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
2848 rh->id, rh->authority_name, rh->name);
2849 //memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
2850 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2851 rh->name, rh->authority_name);
2852 //strcpy(new_name, rh->name);
2853 //strcpy(new_name+strlen(new_name), ".");
2854 //strcpy(new_name+strlen(new_name), rh->authority_name);
2855 strcpy(rh->name, new_name);
2856 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2857 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n", rh->id, rh->name);
2859 rh->proc(rh->proc_cls, rh, 0, NULL);
2864 * We found an authority that may be able to help us
2865 * move on with query
2866 * Note only 1 pkey should have been returned.. anything else would be strange
2869 for (i=0; i<rd_count;i++)
2873 * A CNAME. Like regular DNS this means the is no other record for this
2876 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_CNAME)
2878 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2879 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
2881 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2882 "GNS_PHASE_DELEGATE_NS-%llu: new name to resolve: %s.\n",
2885 rh->status |= RSL_CNAME_FOUND;
2886 rh->proc (rh->proc_cls, rh, rd_count, rd);
2893 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
2895 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2896 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
2898 rh->status |= RSL_DELEGATE_VPN;
2899 rh->proc (rh->proc_cls, rh, rd_count, rd);
2905 * FIXME make optional
2907 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS)
2909 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2910 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
2912 rh->status |= RSL_DELEGATE_NS;
2913 rh->proc (rh->proc_cls, rh, rd_count, rd);
2917 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
2920 rh->status |= RSL_DELEGATE_PKEY;
2922 if (ignore_pending_records &&
2923 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
2925 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2926 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
2932 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
2935 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2936 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
2938 if (remaining_time.rel_value == 0)
2940 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2941 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
2943 rh->authority_chain_head->fresh = 0;
2944 rh->proc(rh->proc_cls, rh, 0, NULL);
2952 * Resolve rest of query with new authority
2954 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
2955 memcpy(&rh->authority, rd[i].data,
2956 sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2957 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
2958 auth->zone = rh->authority;
2959 memset(auth->name, 0, strlen(rh->authority_name)+1);
2960 strcpy(auth->name, rh->authority_name);
2961 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2962 rh->authority_chain_tail,
2965 /** try to import pkey if private key available
2966 * TODO: Only import last one?
2968 //if (rh->priv_key && (name != NULL) && is_canonical (rh->name))
2969 // process_discovered_authority((char*)name, auth->zone,
2970 // rh->authority_chain_tail->zone,
2973 * We are done with PKEY resolution if name is empty
2974 * else resolve again with new authority
2976 if (strcmp (rh->name, "") == 0)
2977 rh->proc (rh->proc_cls, rh, rd_count, rd);
2979 resolve_delegation_ns (rh);
2986 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2987 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
2989 * If we have found some records for the LAST label
2990 * we return the results. Else null.
2992 if (strcmp(rh->name, "") == 0)
2994 /* Start shortening */
2995 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2998 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3000 start_shorten (rh->authority_chain_tail,
3003 /* simply promote back */
3004 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3005 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3006 rh->id, rh->authority_name);
3007 strcpy(rh->name, rh->authority_name);
3008 rh->proc(rh->proc_cls, rh, rd_count, rd);
3012 rh->proc(rh->proc_cls, rh, 0, NULL);
3018 * Resolve the delegation chain for the request in our namestore
3020 * @param rh the resolver handle
3023 resolve_delegation_ns (struct ResolverHandle *rh)
3025 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3026 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3028 pop_tld(rh->name, rh->authority_name);
3029 GNUNET_NAMESTORE_lookup_record(namestore_handle,
3032 GNUNET_GNS_RECORD_ANY,
3033 &process_delegation_result_ns,
3040 * Lookup of a record in a specific zone
3041 * calls lookup result processor on result
3043 * @param zone the root zone
3044 * @param pzone the private local zone
3045 * @param record_type the record type to look up
3046 * @param name the name to look up
3047 * @param key a private key for use with PSEU import (can be NULL)
3048 * @param timeout timeout for resolution
3049 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3050 * @param proc the processor to call on result
3051 * @param cls the closure to pass to proc
3054 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
3055 struct GNUNET_CRYPTO_ShortHashCode pzone,
3056 uint32_t record_type,
3058 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3059 struct GNUNET_TIME_Relative timeout,
3061 RecordLookupProcessor proc,
3064 struct ResolverHandle *rh;
3065 struct RecordLookupHandle* rlh;
3066 char string_hash[MAX_DNS_LABEL_LENGTH];
3067 char nzkey[MAX_DNS_LABEL_LENGTH];
3068 char* nzkey_ptr = nzkey;
3070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3071 "Starting resolution for %s (type=%d)!\n",
3075 if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
3077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3078 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3083 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
3084 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
3086 rh->authority = zone;
3090 rh->timeout = timeout;
3091 rh->get_handle = NULL;
3092 rh->private_local_zone = pzone;
3093 rh->only_cached = only_cached;
3097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3098 "No shorten key for resolution\n");
3101 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3104 * Set timeout for authority lookup phase to 1/2
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3107 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3108 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3109 GNUNET_TIME_relative_divide(timeout, 2),
3110 &handle_lookup_timeout,
3112 rh->timeout_cont = &dht_authority_lookup_timeout;
3113 rh->timeout_cont_cls = rh;
3117 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3118 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3121 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3124 * Only 'gnunet' given
3126 strcpy(rh->name, "\0");
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3131 "Checking for TLD...\n");
3132 if (is_zkey_tld(name) == GNUNET_YES)
3134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3137 * This is a zkey tld
3138 * build hash and use as initial authority
3141 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3142 memcpy(rh->name, name,
3143 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3144 pop_tld(rh->name, string_hash);
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3147 "ZKEY is %s!\n", string_hash);
3149 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3151 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3155 "Cannot convert ZKEY %s to hash!\n", string_hash);
3165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3168 * Presumably GNUNET tld
3171 strlen(name)-strlen(GNUNET_GNS_TLD));
3172 memcpy(rh->name, name,
3173 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3178 * Initialize authority chain
3180 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3181 rh->authority_chain_head->prev = NULL;
3182 rh->authority_chain_head->next = NULL;
3183 rh->authority_chain_tail = rh->authority_chain_head;
3184 rh->authority_chain_head->zone = rh->authority;
3187 * Copy original query into lookup handle
3189 rlh->record_type = record_type;
3190 memset(rlh->name, 0, strlen(name) + 1);
3191 strcpy(rlh->name, name);
3193 rlh->proc_cls = cls;
3195 rh->proc = &handle_delegation_ns;
3196 resolve_delegation_ns(rh);
3199 /******** END Record Resolver ***********/
3202 * Callback calles by namestore for a zone to name
3205 * @param cls the closure
3206 * @param zone_key the zone we queried
3207 * @param expire the expiration time of the name
3208 * @param name the name found or NULL
3209 * @param rd_len number of records for the name
3210 * @param rd the record data (PKEY) for the name
3211 * @param signature the signature for the record data
3214 process_zone_to_name_shorten_root (void *cls,
3215 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3216 struct GNUNET_TIME_Absolute expire,
3218 unsigned int rd_len,
3219 const struct GNUNET_NAMESTORE_RecordData *rd,
3220 const struct GNUNET_CRYPTO_RsaSignature *signature);
3224 * Callback called by namestore for a zone to name
3227 * @param cls the closure
3228 * @param zone_key the zone we queried
3229 * @param expire the expiration time of the name
3230 * @param name the name found or NULL
3231 * @param rd_len number of records for the name
3232 * @param rd the record data (PKEY) for the name
3233 * @param signature the signature for the record data
3236 process_zone_to_name_shorten_shorten (void *cls,
3237 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3238 struct GNUNET_TIME_Absolute expire,
3240 unsigned int rd_len,
3241 const struct GNUNET_NAMESTORE_RecordData *rd,
3242 const struct GNUNET_CRYPTO_RsaSignature *signature)
3244 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3245 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3246 struct AuthorityChain *next_authority;
3248 char result[MAX_DNS_NAME_LENGTH];
3249 char tmp_name[MAX_DNS_NAME_LENGTH];
3252 /* we found a match in our own root zone */
3255 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3256 "result strlen %d\n", strlen(name));
3257 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3258 memset(result, 0, answer_len);
3260 if (strlen(rh->name) > 0)
3262 sprintf (result, "%s.%s.%s.%s.%s",
3264 nsh->shorten_zone_name, nsh->private_zone_name,
3269 sprintf (result, "%s.%s.%s.%s", name,
3270 nsh->shorten_zone_name, nsh->private_zone_name,
3274 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3275 "Found shorten result %s\n", result);
3276 if (strlen (nsh->result) > strlen (result))
3277 strcpy (nsh->result, result);
3279 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3280 nsh->shorten_zone) == 0)
3283 * This is our zone append .gnunet unless name is empty
3284 * (it shouldn't be, usually FIXME what happens if we
3285 * shorten to our zone to a "" record??)
3288 sprintf (result, "%s.%s.%s.%s",
3290 nsh->shorten_zone_name, nsh->private_zone_name,
3292 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3293 "Our zone: Found %s as shorten result\n", result);
3295 if (strlen (nsh->result) > strlen (result))
3296 strcpy (nsh->result, result);
3297 //nsh->proc(nsh->proc_cls, result);
3299 //free_resolver_handle(rh);
3306 * continue with next authority if exists
3308 if ((rh->authority_chain_head->next == NULL))
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3311 "Sending %s as shorten result\n", nsh->result);
3312 nsh->proc(nsh->proc_cls, nsh->result);
3314 free_resolver_handle (rh);
3317 next_authority = rh->authority_chain_head;
3319 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3320 "%s.%s", rh->name, next_authority->name);
3322 strcpy(rh->name, tmp_name);
3323 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3324 "No PSEU found for authority %s. Promoting back: %s\n",
3325 next_authority->name, rh->name);
3327 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3328 rh->authority_chain_tail,
3331 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3332 &rh->authority_chain_tail->zone,
3333 &rh->authority_chain_head->zone,
3334 &process_zone_to_name_shorten_root,
3339 * Callback calles by namestore for a zone to name
3342 * @param cls the closure
3343 * @param zone_key the zone we queried
3344 * @param expire the expiration time of the name
3345 * @param name the name found or NULL
3346 * @param rd_len number of records for the name
3347 * @param rd the record data (PKEY) for the name
3348 * @param signature the signature for the record data
3351 process_zone_to_name_shorten_private (void *cls,
3352 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3353 struct GNUNET_TIME_Absolute expire,
3355 unsigned int rd_len,
3356 const struct GNUNET_NAMESTORE_RecordData *rd,
3357 const struct GNUNET_CRYPTO_RsaSignature *signature)
3359 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3360 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3361 struct AuthorityChain *next_authority;
3363 char result[MAX_DNS_NAME_LENGTH];
3364 char tmp_name[MAX_DNS_NAME_LENGTH];
3367 /* we found a match in our own root zone */
3370 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3371 "result strlen %d\n", strlen(name));
3372 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3373 memset(result, 0, answer_len);
3375 if (strlen(rh->name) > 0)
3377 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3381 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3384 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3385 "Found shorten result %s\n", result);
3386 if (strlen (nsh->result) > strlen (result))
3387 strcpy (nsh->result, result);
3389 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3390 nsh->private_zone) == 0)
3393 * This is our zone append .gnunet unless name is empty
3394 * (it shouldn't be, usually FIXME what happens if we
3395 * shorten to our zone to a "" record??)
3398 sprintf (result, "%s.%s.%s",
3399 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3400 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3401 "Our private zone: Found %s as shorten result %s\n", result);
3402 if (strlen (nsh->result) > strlen (result))
3403 strcpy (nsh->result, result);
3406 if (nsh->shorten_zone != NULL)
3408 /* backtrack authorities for names in priv zone */
3409 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3411 &rh->authority_chain_head->zone,
3412 &process_zone_to_name_shorten_shorten,
3419 * continue with next authority if exists
3421 if ((rh->authority_chain_head->next == NULL))
3423 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3424 "Sending %s as shorten result\n", nsh->result);
3425 nsh->proc(nsh->proc_cls, nsh->result);
3427 free_resolver_handle(rh);
3430 next_authority = rh->authority_chain_head;
3432 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3433 "%s.%s", rh->name, next_authority->name);
3435 strcpy(rh->name, tmp_name);
3436 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3437 "No PSEU found for authority %s. Promoting back: %s\n",
3438 next_authority->name, rh->name);
3440 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3441 rh->authority_chain_tail,
3444 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3445 &rh->authority_chain_tail->zone,
3446 &rh->authority_chain_head->zone,
3447 &process_zone_to_name_shorten_root,
3453 * Callback calles by namestore for a zone to name
3456 * @param cls the closure
3457 * @param zone_key the zone we queried
3458 * @param expire the expiration time of the name
3459 * @param name the name found or NULL
3460 * @param rd_len number of records for the name
3461 * @param rd the record data (PKEY) for the name
3462 * @param signature the signature for the record data
3465 process_zone_to_name_shorten_root (void *cls,
3466 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3467 struct GNUNET_TIME_Absolute expire,
3469 unsigned int rd_len,
3470 const struct GNUNET_NAMESTORE_RecordData *rd,
3471 const struct GNUNET_CRYPTO_RsaSignature *signature)
3473 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3474 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3475 struct AuthorityChain *next_authority;
3477 char result[MAX_DNS_NAME_LENGTH];
3478 char tmp_name[MAX_DNS_NAME_LENGTH];
3481 /* we found a match in our own root zone */
3484 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3485 "result strlen %d\n", strlen(name));
3486 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3487 memset(result, 0, answer_len);
3489 if (strlen(rh->name) > 0)
3491 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3495 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3498 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3499 "Found shorten result %s\n", result);
3500 if (strlen (nsh->result) > strlen (result))
3501 strcpy (nsh->result, result);
3503 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3504 nsh->root_zone) == 0)
3507 * This is our zone append .gnunet unless name is empty
3508 * (it shouldn't be, usually FIXME what happens if we
3509 * shorten to our zone to a "" record??)
3512 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3513 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3514 "Our zone: Found %s as shorten result\n", result);
3515 if (strlen (nsh->result) > strlen (result))
3516 strcpy (nsh->result, result);
3519 if (nsh->private_zone != NULL)
3521 /* backtrack authorities for names in priv zone */
3522 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3524 &rh->authority_chain_head->zone,
3525 &process_zone_to_name_shorten_private,
3532 * continue with next authority if exists
3534 if ((rh->authority_chain_head->next == NULL))
3536 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3537 "Sending %s as shorten result\n", nsh->result);
3538 nsh->proc(nsh->proc_cls, nsh->result);
3540 free_resolver_handle(rh);
3543 next_authority = rh->authority_chain_head;
3545 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3546 "%s.%s", rh->name, next_authority->name);
3548 strcpy(rh->name, tmp_name);
3549 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3550 "No PSEU found for authority %s. Promoting back: %s\n",
3551 next_authority->name, rh->name);
3553 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3554 rh->authority_chain_tail,
3557 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3558 &rh->authority_chain_tail->zone,
3559 &rh->authority_chain_head->zone,
3560 &process_zone_to_name_shorten_root,
3567 * Process result from namestore delegation lookup
3568 * for shorten operation
3570 * @param cls the client shorten handle
3571 * @param rh the resolver handle
3572 * @param rd_count number of results (0)
3573 * @param rd data (NULL)
3576 handle_delegation_ns_shorten (void* cls,
3577 struct ResolverHandle *rh,
3579 const struct GNUNET_NAMESTORE_RecordData *rd)
3581 struct NameShortenHandle *nsh;
3582 char result[MAX_DNS_NAME_LENGTH];
3584 nsh = (struct NameShortenHandle *)cls;
3587 * At this point rh->name contains the part of the name
3588 * that we do not have a PKEY in our namestore to resolve.
3589 * The authority chain in the resolver handle is now
3590 * useful to backtrack if needed
3593 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3594 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3595 memset(result, 0, sizeof (result));
3597 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3598 nsh->root_zone) == 0)
3601 * This is our zone append .gnunet unless name is empty
3602 * (it shouldn't be, usually FIXME what happens if we
3603 * shorten to our zone to a "" record??)
3606 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3607 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3608 "Our zone: Found %s as shorten result\n", result);
3610 if (strlen (nsh->result) > strlen (result))
3611 strcpy (nsh->result, result);
3614 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3615 nsh->private_zone) == 0)
3618 * This is our zone append .gnunet unless name is empty
3619 * (it shouldn't be, usually FIXME what happens if we
3620 * shorten to our zone to a "" record??)
3623 sprintf (result, "%s.%s.%s",
3624 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3625 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3626 "Our zone: Found %s as shorten result %s\n", result);
3628 if (strlen (nsh->result) > strlen (result))
3629 strcpy (nsh->result, result);
3631 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3632 nsh->shorten_zone) == 0)
3635 * This is our zone append .gnunet unless name is empty
3636 * (it shouldn't be, usually FIXME what happens if we
3637 * shorten to our zone to a "" record??)
3640 sprintf (result, "%s.%s.%s",
3641 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3642 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3643 "Our zone: Found %s as shorten result\n", result);
3645 if (strlen (nsh->result) > strlen (result))
3646 strcpy (nsh->result, result);
3650 /* backtrack authorities for names */
3651 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3653 &rh->authority_chain_head->zone,
3654 &process_zone_to_name_shorten_root,
3661 * Callback calles by namestore for a zone to name
3664 * @param cls the closure
3665 * @param zone_key the zone we queried
3666 * @param expire the expiration time of the name
3667 * @param name the name found or NULL
3668 * @param rd_len number of records for the name
3669 * @param rd the record data (PKEY) for the name
3670 * @param signature the signature for the record data
3673 process_zone_to_name_zkey(void *cls,
3674 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3675 struct GNUNET_TIME_Absolute expire,
3677 unsigned int rd_len,
3678 const struct GNUNET_NAMESTORE_RecordData *rd,
3679 const struct GNUNET_CRYPTO_RsaSignature *signature)
3681 struct ResolverHandle *rh = cls;
3682 struct NameShortenHandle *nsh = rh->proc_cls;
3683 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
3684 char new_name[MAX_DNS_NAME_LENGTH];
3686 /* zkey not in our zone */
3690 * In this case we have not given this PKEY a name (yet)
3691 * It is either just not in our zone or not even cached
3692 * Since we do not know at this point we will not try to shorten
3693 * because PKEY import will happen if the user follows the zkey
3696 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
3698 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3699 "No name found for zkey %s returning verbatim!\n", enc);
3700 if (strcmp(rh->name, "") != 0)
3701 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
3702 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
3704 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3705 enc, GNUNET_GNS_TLD_ZKEY);
3707 strcpy (nsh->result, new_name);
3709 nsh->proc(nsh->proc_cls, new_name);
3711 free_resolver_handle(rh);
3715 if (strcmp(rh->name, "") != 0)
3716 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3719 strcpy(new_name, name);
3721 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3722 "Continue shorten for %s!\n", new_name);
3724 strcpy(rh->name, new_name);
3726 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3727 rh->authority_chain_tail = rh->authority_chain_head;
3728 rh->authority_chain_head->zone = rh->authority;
3731 /* Start delegation resolution in our namestore */
3732 resolve_delegation_ns(rh);
3737 * Shorten api from resolver
3739 * @param zone the root zone to use
3740 * @param pzone the private zone to use
3741 * @param szone the shorten zone to use
3742 * @param name the name to shorten
3743 * @param private_zone_name name of the private zone
3744 * @param shorten_zone_name name of the shorten zone
3745 * @param proc the processor to call with result
3746 * @param proc_cls closure to pass to proc
3749 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
3750 struct GNUNET_CRYPTO_ShortHashCode *pzone,
3751 struct GNUNET_CRYPTO_ShortHashCode *szone,
3753 const char* private_zone_name,
3754 const char* shorten_zone_name,
3755 ShortenResultProcessor proc,
3758 struct ResolverHandle *rh;
3759 struct NameShortenHandle *nsh;
3760 char string_hash[MAX_DNS_LABEL_LENGTH];
3761 struct GNUNET_CRYPTO_ShortHashCode zkey;
3762 char nzkey[MAX_DNS_LABEL_LENGTH];
3763 char* nzkey_ptr = nzkey;
3766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3767 "Starting shorten for %s!\n", name);
3769 if (is_canonical ((char*)name))
3771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3772 "%s is canonical. Returning verbatim\n", name);
3773 proc (proc_cls, name);
3777 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
3780 nsh->proc_cls = proc_cls;
3781 nsh->root_zone = zone;
3782 nsh->private_zone = pzone;
3783 nsh->shorten_zone = szone;
3784 strcpy (nsh->private_zone_name, private_zone_name);
3785 strcpy (nsh->shorten_zone_name, shorten_zone_name);
3786 strcpy (nsh->result, name);
3788 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3789 rh->authority = *zone;
3791 rh->priv_key = NULL;
3792 rh->proc = &handle_delegation_ns_shorten;
3795 rh->private_local_zone = *zone;
3797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3798 "Checking for TLD...\n");
3799 if (is_zkey_tld (name) == GNUNET_YES)
3801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3804 * This is a zkey tld
3805 * build hash and use as initial authority
3808 memset (rh->name, 0,
3809 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
3810 memcpy (rh->name, name,
3811 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
3812 pop_tld (rh->name, string_hash);
3814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3815 "ZKEY is %s!\n", string_hash);
3817 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
3819 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
3822 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3823 "Cannot convert ZKEY %s to hash!\n", nzkey);
3826 proc (proc_cls, name);
3830 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3833 &process_zone_to_name_zkey,
3840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3843 * Presumably GNUNET tld
3845 memset (rh->name, 0,
3846 strlen (name)-strlen (GNUNET_GNS_TLD));
3847 memcpy (rh->name, name,
3848 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
3851 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
3852 rh->authority_chain_tail = rh->authority_chain_head;
3853 rh->authority_chain_head->zone = *zone;
3856 /* Start delegation resolution in our namestore */
3857 resolve_delegation_ns (rh);
3860 /*********** END NAME SHORTEN ********************/
3864 * Process result from namestore delegation lookup
3865 * for get authority operation
3867 * @param cls the client get auth handle
3868 * @param rh the resolver handle
3869 * @param rd_count number of results (0)
3870 * @param rd data (NULL)
3873 handle_delegation_result_ns_get_auth(void* cls,
3874 struct ResolverHandle *rh,
3876 const struct GNUNET_NAMESTORE_RecordData *rd)
3878 struct GetNameAuthorityHandle* nah;
3879 char result[MAX_DNS_NAME_LENGTH];
3882 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
3885 * At this point rh->name contains the part of the name
3886 * that we do not have a PKEY in our namestore to resolve.
3887 * The authority chain in the resolver handle is now
3888 * useful to backtrack if needed
3891 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3892 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3894 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3895 "Building response!\n");
3896 if (is_canonical(rh->name))
3899 * We successfully resolved the authority in the ns
3900 * FIXME for our purposes this is fine
3901 * but maybe we want to have an api that also looks
3902 * into the dht (i.e. option in message)
3904 if (strlen(rh->name) > strlen(nah->name))
3906 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3907 "Record name longer than original lookup name... odd!\n");
3911 answer_len = strlen(nah->name) - strlen(rh->name)
3912 + strlen(GNUNET_GNS_TLD) + 1;
3913 memset(result, 0, answer_len);
3914 strcpy(result, nah->name + strlen(rh->name) + 1);
3916 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3917 "Got authority result %s\n", result);
3919 nah->proc(nah->proc_cls, result);
3921 free_resolver_handle(rh);
3925 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3926 "Unable to resolve authority for remaining %s!\n", rh->name);
3927 nah->proc(nah->proc_cls, "");
3929 free_resolver_handle(rh);
3937 * Tries to resolve the authority for name
3940 * @param zone the root zone to look up for
3941 * @param pzone the private local zone
3942 * @param name the name to lookup up
3943 * @param proc the processor to call when finished
3944 * @param proc_cls the closure to pass to the processor
3947 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
3948 struct GNUNET_CRYPTO_ShortHashCode pzone,
3950 GetAuthorityResultProcessor proc,
3953 struct ResolverHandle *rh;
3954 struct GetNameAuthorityHandle *nah;
3956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3957 "Starting authority resolution for %s!\n", name);
3959 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
3960 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
3961 rh->authority = zone;
3963 rh->private_local_zone = pzone;
3965 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3967 strcpy(rh->name, "\0");
3972 strlen(name)-strlen(GNUNET_GNS_TLD));
3973 memcpy(rh->name, name,
3974 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3977 memset(nah->name, 0,
3979 strcpy(nah->name, name);
3981 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3982 rh->authority_chain_tail = rh->authority_chain_head;
3983 rh->authority_chain_head->zone = zone;
3984 rh->proc = &handle_delegation_result_ns_get_auth;
3985 rh->proc_cls = (void*)nah;
3988 nah->proc_cls = proc_cls;
3990 /* Start delegation resolution in our namestore */
3991 resolve_delegation_ns(rh);
3995 /******** END GET AUTHORITY *************/
3997 /* end of gnunet-service-gns_resolver.c */