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;
1331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1332 "No address found in DNS!\n");
1333 finish_lookup (rh, rlh, 0, NULL);
1334 free_resolver_handle (rh);
1338 if (addrlen == sizeof (struct sockaddr_in))
1339 rd.record_type = GNUNET_GNS_RECORD_TYPE_A;
1341 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1343 rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
1344 rd.data_size = addrlen;
1347 finish_lookup (rh, rlh, 1, &rd);
1348 free_resolver_handle (rh);
1352 * Resolve DNS name via local stub resolver
1354 * @param rh the resolver handle
1357 resolve_dns_name (struct ResolverHandle *rh)
1359 struct RecordLookupHandle *rlh = rh->proc_cls;
1362 if ((rlh->record_type != GNUNET_GNS_RECORD_TYPE_A) &&
1363 (rlh->record_type != GNUNET_GNS_RECORD_AAAA))
1365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1366 "Can only resolve A/AAAA via stub... abort\n");
1367 finish_lookup (rh, rlh, 0, NULL);
1368 free_resolver_handle (rh);
1372 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_A)
1377 //GNUNET_RESOLVER_connect (cfg); FIXME into init
1379 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1382 &handle_dns_resolver,
1388 * Read DNS udp packet from socket
1390 * @param cls the resolver handle
1391 * @param tc task context
1394 read_dns_response (void *cls,
1395 const struct GNUNET_SCHEDULER_TaskContext *tc)
1397 struct ResolverHandle *rh = cls;
1398 struct RecordLookupHandle *rlh = rh->proc_cls;
1399 char buf[UINT16_MAX];
1401 struct sockaddr_in addr;
1403 struct GNUNET_DNSPARSER_Packet *packet;
1404 struct GNUNET_NAMESTORE_RecordData rd;
1405 int found_delegation = GNUNET_NO;
1406 int found_cname = GNUNET_NO;
1407 char* delegation_name = NULL;
1408 int zone_offset = 0;
1411 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1412 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1414 /* timeout or shutdown */
1415 rh->proc (rh->proc_cls, rh, 0, NULL);
1416 GNUNET_NETWORK_socket_close (rh->dns_sock);
1417 free_resolver_handle (rh);
1421 addrlen = sizeof (addr);
1422 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1424 (struct sockaddr*) &addr,
1429 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1430 rh->proc (rh->proc_cls, rh, 0, NULL);
1431 GNUNET_NETWORK_socket_close (rh->dns_sock);
1432 free_resolver_handle (rh);
1436 packet = GNUNET_DNSPARSER_parse (buf, r);
1440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1441 "Failed to parse DNS reply!\n");
1442 rh->proc (rh->proc_cls, rh, 0, NULL);
1443 GNUNET_NETWORK_socket_close (rh->dns_sock);
1444 free_resolver_handle (rh);
1448 for (i = 0; i < packet->num_answers; i++)
1450 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1451 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_TYPE_CNAME)
1453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1454 "CNAME... restarting query with %s\n",
1455 packet->answers[i].data.hostname
1457 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1458 found_cname = GNUNET_YES;
1459 //send_dns_packet (rh);
1460 //GNUNET_DNSPARSER_free_packet (packet);
1464 if ((packet->answers[i].type == rlh->record_type) &&
1465 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1469 rd.data = packet->answers[i].data.raw.data;
1470 rd.data_size = packet->answers[i].data.raw.data_len;
1471 rd.record_type = packet->answers[i].type;
1473 rd.expiration = packet->answers[i].expiration_time;
1474 rh->proc (rh->proc_cls, rh, 1, &rd);
1475 GNUNET_NETWORK_socket_close (rh->dns_sock);
1476 GNUNET_DNSPARSER_free_packet (packet);
1477 free_resolver_handle (rh);
1482 if (GNUNET_YES == found_cname)
1484 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1486 if (0 > zone_offset)
1489 /* restart query with CNAME */
1490 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1491 send_dns_packet (rh);
1493 resolve_dns_name (rh);
1495 GNUNET_DNSPARSER_free_packet (packet);
1499 for (i = 0; i < packet->num_authority_records; i++)
1502 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_TYPE_NS)
1504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1505 "Found NS delegation!\n");
1506 found_delegation = GNUNET_YES;
1507 delegation_name = packet->authority_records[i].data.hostname;
1512 for (i = 0; i < packet->num_additional_records; i++)
1514 if (found_delegation == GNUNET_NO)
1517 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_TYPE_A) &&
1518 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1520 GNUNET_assert (sizeof (struct in_addr) ==
1521 packet->authority_records[i].data.raw.data_len);
1523 rh->dns_addr.sin_addr =
1524 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1525 send_dns_packet (rh);
1526 GNUNET_DNSPARSER_free_packet (packet);
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532 "Failed to parse DNS reply!\n");
1533 rh->proc (rh->proc_cls, rh, 0, NULL);
1534 GNUNET_NETWORK_socket_close (rh->dns_sock);
1535 free_resolver_handle (rh);
1536 GNUNET_DNSPARSER_free_packet (packet);
1541 * Sends a UDP dns query to a nameserver specified in the rh
1543 * @param rh the request handle
1546 send_dns_packet (struct ResolverHandle *rh)
1548 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1549 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1551 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1553 rh->dns_raw_packet_size,
1554 (struct sockaddr*)&rh->dns_addr,
1555 sizeof (struct sockaddr_in));
1557 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1558 rh->timeout, //FIXME less?
1564 GNUNET_NETWORK_fdset_destroy (rset);
1569 * The final phase of resoution.
1570 * We found a NS RR and want to resolve via DNS
1572 * @param rh the pending lookup handle
1573 * @param rd_count length of record data
1574 * @param rd record data containing VPN RR
1577 resolve_record_dns (struct ResolverHandle *rh,
1579 const struct GNUNET_NAMESTORE_RecordData *rd)
1581 struct GNUNET_DNSPARSER_Query query;
1582 struct GNUNET_DNSPARSER_Packet packet;
1583 struct GNUNET_DNSPARSER_Flags flags;
1584 char dns_name[MAX_DNS_NAME_LENGTH];
1585 struct in_addr dnsip;
1586 struct sockaddr_in addr;
1587 struct sockaddr *sa;
1589 struct RecordLookupHandle *rlh = rh->proc_cls;
1591 /* We cancel here as to not include the ns lookup in the timeout */
1592 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1594 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1595 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1597 /* Start shortening */
1598 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1601 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1603 start_shorten (rh->authority_chain_tail,
1607 for (i = 0; i < rd_count; i++)
1609 /* Synthesize dns name */
1610 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS)
1612 strcpy (rh->dns_zone, (char*)rd[i].data);
1613 if (0 == strcmp (rh->name, ""))
1614 strcpy (rh->dns_name, (char*)rd[i].data);
1616 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1619 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
1620 dnsip = *((struct in_addr*)rd[i].data);
1623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1624 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1627 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1628 if (rh->dns_sock == NULL)
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1633 rh->proc(rh->proc_cls, rh, 0, NULL);
1637 memset (&addr, 0, sizeof (struct sockaddr_in));
1638 sa = (struct sockaddr *) &addr;
1639 sa->sa_family = AF_INET;
1640 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1642 sizeof (struct sockaddr_in)))
1644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1645 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1647 GNUNET_NETWORK_socket_close (rh->dns_sock);
1648 rh->proc(rh->proc_cls, rh, 0, NULL);
1651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1652 "GNS_PHASE_REC_DNS-%llu: NOT IMPLEMENTED!\n",
1654 GNUNET_NETWORK_socket_close (rh->dns_sock);
1655 rh->proc(rh->proc_cls, rh, 0, NULL);
1656 /*TODO create dnsparser query, serialize, sendto, handle reply*/
1657 query.name = dns_name;
1658 query.type = rlh->record_type;
1659 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1660 memset (&flags, 0, sizeof (flags));
1661 flags.recursion_desired = 1;
1662 flags.checking_disabled = 1;
1663 packet.queries = &query;
1664 packet.answers = NULL;
1665 packet.authority_records = NULL;
1666 packet.num_queries = 1;
1667 packet.num_answers = 0;
1668 packet.num_authority_records = 0;
1669 packet.num_additional_records = 0;
1670 packet.flags = flags;
1672 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1674 &rh->dns_raw_packet,
1675 &rh->dns_raw_packet_size))
1677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1678 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1680 GNUNET_NETWORK_socket_close (rh->dns_sock);
1681 rh->proc(rh->proc_cls, rh, 0, NULL);
1685 rh->dns_addr.sin_family = AF_INET;
1686 rh->dns_addr.sin_port = htons (53); //domain
1687 rh->dns_addr.sin_addr = dnsip;
1688 #if HAVE_SOCKADDR_IN_SIN_LEN
1689 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1692 send_dns_packet (rh);
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "GNS_PHASE_REC_DNS-%llu: NOT IMPLEMENTED!\n",
1697 GNUNET_free (rh->dns_raw_packet);
1698 GNUNET_NETWORK_socket_close (rh->dns_sock);
1699 rh->proc(rh->proc_cls, rh, 0, NULL);
1704 * The final phase of resoution.
1705 * We found a VPN RR and want to request an IPv4/6 address
1707 * @param rh the pending lookup handle
1708 * @param rd_count length of record data
1709 * @param rd record data containing VPN RR
1712 resolve_record_vpn (struct ResolverHandle *rh,
1714 const struct GNUNET_NAMESTORE_RecordData *rd)
1718 struct GNUNET_HashCode peer_id;
1719 struct GNUNET_CRYPTO_HashAsciiEncoded s_pid;
1720 struct GNUNET_HashCode serv_desc;
1721 struct GNUNET_CRYPTO_HashAsciiEncoded s_sd;
1723 size_t len = (sizeof (uint32_t) * 2) + (sizeof (struct GNUNET_HashCode) * 2);
1725 /* We cancel here as to not include the ns lookup in the timeout */
1726 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1728 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1729 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1731 /* Start shortening */
1732 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1735 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1737 start_shorten (rh->authority_chain_tail,
1741 /* Extracting VPN information FIXME rd parsing with NS API?*/
1742 if (len != rd->data_size)
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1745 "GNS_PHASE_REC_VPN-%llu: Error parsing VPN RR!\n",
1747 finish_lookup (rh, rh->proc_cls, 0, NULL);
1748 free_resolver_handle (rh);
1753 memcpy (&af, pos, sizeof (uint32_t));
1754 pos += sizeof (uint32_t);
1755 memcpy (&proto, pos, sizeof (uint32_t));
1756 pos += sizeof (uint32_t);
1757 memcpy (&s_pid, pos, sizeof (struct GNUNET_HashCode));
1758 pos += sizeof (struct GNUNET_HashCode);
1759 memcpy (&s_sd, pos, sizeof (struct GNUNET_HashCode));
1762 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_pid, &peer_id)) ||
1763 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_sd, &serv_desc)))
1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1766 "GNS_PHASE_REC_VPN-%llu: Error parsing VPN RR hashes!\n",
1768 finish_lookup (rh, rh->proc_cls, 0, NULL);
1769 free_resolver_handle (rh);
1773 rh->proc = &handle_record_vpn;
1775 if (NULL == vpn_handle)
1777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1778 "GNS_PHASE_REC_VPN-%llu: VPN not connected!\n",
1780 finish_lookup (rh, rh->proc_cls, 0, NULL);
1781 free_resolver_handle (rh);
1786 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1788 (struct GNUNET_PeerIdentity*)&peer_id,
1791 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1792 &process_record_result_vpn,
1798 * The final phase of resolution.
1799 * rh->name is a name that is canonical and we do not have a delegation.
1800 * Query namestore for this record
1802 * @param rh the pending lookup handle
1805 resolve_record_ns(struct ResolverHandle *rh)
1807 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1809 /* We cancel here as to not include the ns lookup in the timeout */
1810 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1812 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1813 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1815 /* Start shortening */
1816 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1819 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1821 start_shorten (rh->authority_chain_tail,
1826 * Try to resolve this record in our namestore.
1827 * The name to resolve is now in rh->authority_name
1828 * since we tried to resolve it to an authority
1831 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1835 &process_record_result_ns,
1842 * Handle timeout for DHT requests
1844 * @param cls the request handle as closure
1845 * @param tc the task context
1848 dht_authority_lookup_timeout(void *cls,
1849 const struct GNUNET_SCHEDULER_TaskContext *tc)
1851 struct ResolverHandle *rh = cls;
1852 struct RecordLookupHandle *rlh = rh->proc_cls;
1853 char new_name[MAX_DNS_NAME_LENGTH];
1855 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1856 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%ds)timed out.\n",
1857 rh->id, rh->authority_name, rh->timeout.rel_value);
1859 rh->status |= RSL_TIMED_OUT;
1861 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1863 GNUNET_DHT_get_stop (rh->get_handle);
1864 rh->get_handle = NULL;
1866 if (strcmp(rh->name, "") == 0)
1869 * promote authority back to name and try to resolve record
1871 strcpy(rh->name, rh->authority_name);
1872 rh->proc(rh->proc_cls, rh, 0, NULL);
1877 * Start resolution in bg
1879 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
1880 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
1881 //strcpy(new_name, rh->name);
1882 //strcpy(new_name+strlen(new_name), ".");
1883 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1885 strcpy(rh->name, new_name);
1887 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1888 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1889 rh->id, rh->name, rlh->record_type);
1891 gns_resolver_lookup_record(rh->authority,
1892 rh->private_local_zone,
1896 GNUNET_TIME_UNIT_FOREVER_REL,
1898 &background_lookup_result_processor,
1901 rh->proc(rh->proc_cls, rh, 0, NULL);
1905 static void resolve_delegation_dht(struct ResolverHandle *rh);
1908 static void resolve_delegation_ns(struct ResolverHandle *rh);
1912 * Namestore resolution for delegation finished. Processing result.
1914 * @param cls the closure
1915 * @param rh resolver handle
1916 * @param rd_count number of results (always 0)
1917 * @param rd record data (always NULL)
1920 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1921 unsigned int rd_count,
1922 const struct GNUNET_NAMESTORE_RecordData *rd);
1926 * Function called when we get a result from the dht
1927 * for our query. Recursively tries to resolve authorities
1930 * @param cls the request handle
1931 * @param exp lifetime
1932 * @param key the key the record was stored under
1933 * @param get_path get path
1934 * @param get_path_length get path length
1935 * @param put_path put path
1936 * @param put_path_length put path length
1937 * @param type the block type
1938 * @param size the size of the record
1939 * @param data the record data
1942 process_delegation_result_dht(void* cls,
1943 struct GNUNET_TIME_Absolute exp,
1944 const struct GNUNET_HashCode * key,
1945 const struct GNUNET_PeerIdentity *get_path,
1946 unsigned int get_path_length,
1947 const struct GNUNET_PeerIdentity *put_path,
1948 unsigned int put_path_length,
1949 enum GNUNET_BLOCK_Type type,
1950 size_t size, const void *data)
1952 struct ResolverHandle *rh;
1953 struct GNSNameRecordBlock *nrb;
1954 uint32_t num_records;
1956 char* rd_data = (char*) data;
1959 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
1960 struct GNUNET_HashCode zone_hash_double, name_hash_double;
1962 rh = (struct ResolverHandle *)cls;
1964 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1965 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
1970 nrb = (struct GNSNameRecordBlock*)data;
1972 /* stop dht lookup and timeout task */
1973 GNUNET_DHT_get_stop (rh->get_handle);
1975 rh->get_handle = NULL;
1977 if (rh->dht_heap_node != NULL)
1979 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
1980 rh->dht_heap_node = NULL;
1983 num_records = ntohl(nrb->rd_count);
1984 name = (char*)&nrb[1];
1986 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1988 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1989 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
1991 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1996 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1997 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2002 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2003 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2004 rh->id, name, rh->authority_name);
2005 for (i=0; i<num_records; i++)
2008 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2009 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2010 rh->id, name, rh->authority_name);
2011 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2012 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2013 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2014 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2015 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2016 rh->id, rd[i].data_size);
2017 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2018 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2019 rh->id, rd[i].flags);
2021 if ((rd[i].record_type == GNUNET_GNS_RECORD_VPN) ||
2022 (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS) ||
2023 (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_CNAME))
2026 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2028 if (strcmp(rh->name, "") == 0)
2029 strcpy(rh->name, rh->authority_name);
2031 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2032 rh->name, rh->authority_name); //FIXME ret
2037 if ((strcmp(name, rh->authority_name) == 0) &&
2038 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
2040 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2041 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2044 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2045 struct AuthorityChain *auth =
2046 GNUNET_malloc(sizeof(struct AuthorityChain));
2047 auth->zone = rh->authority;
2048 memset(auth->name, 0, strlen(rh->authority_name)+1);
2049 strcpy(auth->name, rh->authority_name);
2050 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2051 rh->authority_chain_tail,
2054 /** try to import pkey if private key available */
2055 //if (rh->priv_key && is_canonical (rh->name))
2056 // process_discovered_authority(name, auth->zone,
2057 // rh->authority_chain_tail->zone,
2064 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2065 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2066 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2067 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2069 /* Save to namestore */
2070 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
2073 GNUNET_NAMESTORE_record_put (namestore_handle,
2080 &on_namestore_record_put_result, //cont
2090 * FIXME in this case. should we ask namestore again?
2092 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2093 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2094 rh->id, rh->authority_name, rh->name);
2096 if (strcmp(rh->name, "") == 0)
2098 /* Start shortening */
2099 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2102 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2104 start_shorten (rh->authority_chain_tail,
2108 rh->proc(rh->proc_cls, rh, 0, NULL);
2112 rh->proc = &handle_delegation_ns;
2113 resolve_delegation_ns (rh);
2119 * No pkey but name exists
2122 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2123 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2124 rh->id, rh->authority_name, rh->name);
2125 if (strcmp(rh->name, "") == 0)
2126 strcpy(rh->name, rh->authority_name);
2128 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2129 rh->name, rh->authority_name); //FIXME ret
2131 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2132 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2133 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2134 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2136 rh->proc(rh->proc_cls, rh, 0, NULL);
2139 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2140 +(MAX_DNS_NAME_LENGTH*2)
2141 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2145 expand_plus(char** dest, char* src, char* repl)
2148 unsigned int s_len = strlen(src)+1;
2150 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2151 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2155 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2156 "GNS_POSTPROCESS: %s to short\n", src);
2158 /* no postprocessing */
2159 memcpy(*dest, src, s_len+1);
2163 if (0 == strcmp(src+s_len-3, ".+"))
2165 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2166 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2167 memset(*dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2169 pos = *dest+s_len-2;
2171 pos += strlen(repl);
2172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2173 "GNS_POSTPROCESS: Expanded to %s\n", *dest);
2177 memcpy(*dest, src, s_len+1);
2185 finish_lookup(struct ResolverHandle *rh,
2186 struct RecordLookupHandle* rlh,
2187 unsigned int rd_count,
2188 const struct GNUNET_NAMESTORE_RecordData *rd)
2191 char new_rr_data[MAX_DNS_NAME_LENGTH];
2192 char new_mx_data[MAX_MX_LENGTH];
2193 char new_soa_data[MAX_SOA_LENGTH];
2194 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2197 unsigned int offset;
2199 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2201 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2202 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2206 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2208 for (i = 0; i < rd_count; i++)
2211 if (rd[i].record_type != GNUNET_GNS_RECORD_TYPE_NS &&
2212 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_CNAME &&
2213 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
2214 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_SOA)
2216 p_rd[i].data = rd[i].data;
2221 * for all those records we 'should'
2222 * also try to resolve the A/AAAA records (RFC1035)
2223 * This is a feature and not important
2226 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2227 "GNS_POSTPROCESS: Postprocessing\n");
2229 if (strcmp(rh->name, "+") == 0)
2230 repl_string = rlh->name;
2232 repl_string = rlh->name+strlen(rh->name)+1;
2235 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
2237 memcpy(new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2238 offset = sizeof(uint16_t);
2239 pos = new_mx_data+offset;
2240 expand_plus(&pos, (char*)rd[i].data+sizeof(uint16_t),
2242 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2243 p_rd[i].data = new_mx_data;
2244 p_rd[i].data_size = offset;
2246 else if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_SOA)
2248 /* expand mname and rname */
2250 expand_plus(&pos, (char*)rd[i].data, repl_string);
2251 offset = strlen(new_soa_data)+1;
2252 pos = new_soa_data+offset;
2253 expand_plus(&pos, (char*)rd[i].data+offset, repl_string);
2254 offset += strlen(new_soa_data+offset)+1;
2255 /* cpy the 4 numbers serial refresh retry and expire */
2256 memcpy(new_soa_data+offset, (char*)rd[i].data+offset, sizeof(uint32_t)*5);
2257 offset += sizeof(uint32_t)*5;
2258 p_rd[i].data_size = offset;
2259 p_rd[i].data = new_soa_data;
2264 expand_plus(&pos, (char*)rd[i].data, repl_string);
2265 p_rd[i].data_size = strlen(new_rr_data)+1;
2266 p_rd[i].data = new_rr_data;
2271 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2277 * Process DHT lookup result for record.
2279 * @param cls the closure
2280 * @param rh resolver handle
2281 * @param rd_count number of results
2282 * @param rd record data
2285 handle_record_dht(void* cls, struct ResolverHandle *rh,
2286 unsigned int rd_count,
2287 const struct GNUNET_NAMESTORE_RecordData *rd)
2289 struct RecordLookupHandle* rlh;
2291 rlh = (struct RecordLookupHandle*)cls;
2294 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2295 "GNS_PHASE_REC-%d: No records for %s found in DHT. Aborting\n",
2297 /* give up, cannot resolve */
2298 finish_lookup(rh, rlh, 0, NULL);
2299 free_resolver_handle(rh);
2303 /* results found yay */
2304 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2305 "GNS_PHASE_REC-%d: Record resolved from DHT!", rh->id);
2307 finish_lookup(rh, rlh, rd_count, rd);
2308 free_resolver_handle(rh);
2316 * Process namestore lookup result for record.
2318 * @param cls the closure
2319 * @param rh resolver handle
2320 * @param rd_count number of results
2321 * @param rd record data
2324 handle_record_ns (void* cls, struct ResolverHandle *rh,
2325 unsigned int rd_count,
2326 const struct GNUNET_NAMESTORE_RecordData *rd)
2328 struct RecordLookupHandle* rlh;
2329 rlh = (struct RecordLookupHandle*) cls;
2332 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2333 "GNS_PHASE_REC-%d: NS returned no records. (status: %d)!\n",
2338 * There are 5 conditions that have to met for us to consult the DHT:
2339 * 1. The entry in the DHT is RSL_RECORD_EXPIRED AND
2340 * 2. No entry in the NS existed AND
2341 * 3. The zone queried is not the local resolver's zone AND
2342 * 4. The name that was looked up is '+'
2343 * because if it was any other canonical name we either already queried
2344 * the DHT for the authority in the authority lookup phase (and thus
2345 * would already have an entry in the NS for the record)
2346 * 5. We are not in cache only mode
2348 if (rh->status & (RSL_RECORD_EXPIRED | !RSL_RECORD_EXISTS) &&
2349 GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2350 &rh->private_local_zone) &&
2351 (strcmp(rh->name, "+") == 0) &&
2352 (rh->only_cached == GNUNET_NO))
2354 rh->proc = &handle_record_dht;
2355 resolve_record_dht(rh);
2358 /* give up, cannot resolve */
2359 finish_lookup(rh, rlh, 0, NULL);
2360 free_resolver_handle(rh);
2364 /* results found yay */
2365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2366 "GNS_PHASE_REC-%d: Record resolved from namestore!", rh->id);
2368 finish_lookup(rh, rlh, rd_count, rd);
2370 free_resolver_handle(rh);
2376 * Move one level up in the domain hierarchy and return the
2377 * passed top level domain.
2379 * @param name the domain
2380 * @param dest the destination where the tld will be put
2383 pop_tld(char* name, char* dest)
2387 if (is_canonical(name))
2394 for (len = strlen(name); len > 0; len--)
2396 if (*(name+len) == '.')
2406 strcpy(dest, (name+len+1));
2410 * Checks if name is in tld
2412 * @param name the name to check
2413 * @param tld the TLD to check for
2414 * @return GNUNET_YES or GNUNET_NO
2417 is_tld(const char* name, const char* tld)
2421 if (strlen(name) <= strlen(tld))
2426 offset = strlen(name)-strlen(tld);
2427 if (strcmp(name+offset, tld) != 0)
2429 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2430 "%s is not in .%s TLD\n", name, tld);
2437 * DHT resolution for delegation finished. Processing result.
2439 * @param cls the closure
2440 * @param rh resolver handle
2441 * @param rd_count number of results (always 0)
2442 * @param rd record data (always NULL)
2445 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2446 unsigned int rd_count,
2447 const struct GNUNET_NAMESTORE_RecordData *rd)
2449 struct RecordLookupHandle* rlh;
2450 rlh = (struct RecordLookupHandle*) cls;
2453 if (strcmp(rh->name, "") == 0)
2455 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
2457 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2458 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2460 finish_lookup(rh, rlh, rd_count, rd);
2461 free_resolver_handle(rh);
2464 /* We resolved full name for delegation. resolving record */
2465 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2466 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2468 strcpy(rh->name, "+\0");
2469 rh->proc = &handle_record_ns;
2470 resolve_record_ns(rh);
2475 * we still have some left
2477 if (is_canonical(rh->name))
2479 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2480 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2483 rh->proc = &handle_record_ns;
2484 resolve_record_ns(rh);
2487 /* give up, cannot resolve */
2488 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2489 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2491 finish_lookup(rh, rlh, 0, NULL);
2492 free_resolver_handle(rh);
2497 * Start DHT lookup for a name -> PKEY (compare NS) record in
2498 * rh->authority's zone
2500 * @param rh the pending gns query
2503 resolve_delegation_dht(struct ResolverHandle *rh)
2506 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2507 struct GNUNET_HashCode name_hash_double;
2508 struct GNUNET_HashCode zone_hash_double;
2509 struct GNUNET_HashCode lookup_key;
2510 struct ResolverHandle *rh_heap_root;
2512 pop_tld(rh->name, rh->authority_name);
2513 GNUNET_CRYPTO_short_hash(rh->authority_name,
2514 strlen(rh->authority_name),
2516 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2517 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2518 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2520 rh->dht_heap_node = NULL;
2522 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2524 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
2525 // &dht_authority_lookup_timeout,
2527 rh->timeout_cont = &dht_authority_lookup_timeout;
2528 rh->timeout_cont_cls = rh;
2532 if (max_allowed_background_queries <=
2533 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2535 /* terminate oldest lookup */
2536 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2537 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
2538 rh_heap_root->dht_heap_node = NULL;
2540 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2541 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2542 rh->id, rh_heap_root->authority_name);
2544 rh_heap_root->proc(rh_heap_root->proc_cls,
2549 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2551 GNUNET_TIME_absolute_get().abs_value);
2554 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2556 GNUNET_assert(rh->get_handle == NULL);
2557 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2558 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2560 DHT_GNS_REPLICATION_LEVEL,
2564 &process_delegation_result_dht,
2571 * Namestore resolution for delegation finished. Processing result.
2573 * @param cls the closure
2574 * @param rh resolver handle
2575 * @param rd_count number of results (always 0)
2576 * @param rd record data (always NULL)
2579 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2580 unsigned int rd_count,
2581 const struct GNUNET_NAMESTORE_RecordData *rd)
2583 struct RecordLookupHandle* rlh;
2584 rlh = (struct RecordLookupHandle*) cls;
2586 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2587 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2588 rh->id, rh->status);
2590 if (strcmp(rh->name, "") == 0)
2593 /* We resolved full name for delegation. resolving record */
2594 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2595 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2597 if (rh->status & RSL_CNAME_FOUND)
2599 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_CNAME)
2601 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2602 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2604 finish_lookup(rh, rlh, rd_count, rd);
2605 free_resolver_handle(rh);
2609 /* A CNAME can only occur alone */
2610 GNUNET_assert (is_canonical ((char*)rd->data));
2611 strcpy (rh->name, rd->data);
2612 resolve_delegation_ns (rh);
2615 else if (rh->status & RSL_DELEGATE_VPN)
2617 if (rlh->record_type == GNUNET_GNS_RECORD_VPN)
2619 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2620 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2622 finish_lookup(rh, rlh, rd_count, rd);
2623 free_resolver_handle(rh);
2626 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2627 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2629 GNUNET_assert (NULL != rd);
2630 rh->proc = &handle_record_vpn;
2631 resolve_record_vpn (rh, rd_count, rd);
2633 else if (rh->status & RSL_DELEGATE_NS)
2635 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_NS)
2637 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2638 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2640 finish_lookup(rh, rlh, rd_count, rd);
2641 free_resolver_handle(rh);
2645 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2646 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2648 GNUNET_assert (NULL != rd);
2649 rh->proc = &handle_record_ns;
2650 resolve_record_dns (rh, rd_count, rd);
2652 else if (rh->status & RSL_DELEGATE_PKEY)
2654 if (rlh->record_type == GNUNET_GNS_RECORD_PKEY)
2656 GNUNET_assert(rd_count == 1);
2657 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2658 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
2660 finish_lookup(rh, rlh, rd_count, rd);
2661 free_resolver_handle(rh);
2667 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2668 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
2670 strcpy(rh->name, "+\0");
2671 rh->proc = &handle_record_ns;
2672 resolve_record_ns(rh);
2678 * we still have some left
2679 * check if authority in ns is fresh
2681 * or we are authority
2683 if (((rh->status & RSL_RECORD_EXISTS) && (!(rh->status & RSL_RECORD_EXPIRED)))
2684 || !GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2685 &rh->private_local_zone))
2687 if (is_canonical(rh->name))
2689 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2690 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
2693 rh->proc = &handle_record_ns;
2694 resolve_record_ns(rh);
2698 /* give up, cannot resolve */
2699 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2700 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
2703 finish_lookup(rh, rlh, rd_count, rd);
2704 //rlh->proc(rlh->proc_cls, 0, NULL);
2709 if (rh->only_cached == GNUNET_YES)
2711 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2712 "GNS_PHASE_DELEGATE_NS-%llu: Only cache resolution, no result\n",
2714 finish_lookup(rh, rlh, rd_count, rd);
2718 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2719 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
2721 rh->proc = &handle_delegation_dht;
2722 resolve_delegation_dht(rh);
2728 * This is a callback function that should give us only PKEY
2729 * records. Used to query the namestore for the authority (PKEY)
2730 * for 'name'. It will recursively try to resolve the
2731 * authority for a given name from the namestore.
2733 * @param cls the pending query
2734 * @param key the key of the zone we did the lookup
2735 * @param expiration expiration date of the record data set in the namestore
2736 * @param name the name for which we need an authority
2737 * @param rd_count the number of records with 'name'
2738 * @param rd the record data
2739 * @param signature the signature of the authority for the record data
2742 process_delegation_result_ns(void* cls,
2743 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2744 struct GNUNET_TIME_Absolute expiration,
2746 unsigned int rd_count,
2747 const struct GNUNET_NAMESTORE_RecordData *rd,
2748 const struct GNUNET_CRYPTO_RsaSignature *signature)
2750 struct ResolverHandle *rh;
2751 struct GNUNET_TIME_Relative remaining_time;
2752 struct GNUNET_CRYPTO_ShortHashCode zone;
2753 char new_name[MAX_DNS_NAME_LENGTH];
2755 rh = (struct ResolverHandle *)cls;
2756 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2757 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
2760 GNUNET_CRYPTO_short_hash(key,
2761 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2763 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2769 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2770 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
2772 rh->status |= RSL_RECORD_EXISTS;
2775 if (remaining_time.rel_value == 0)
2777 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2778 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
2780 rh->status |= RSL_RECORD_EXPIRED;
2784 * No authority found in namestore.
2789 * We did not find an authority in the namestore
2794 * Promote this authority back to a name maybe it is
2797 if (strcmp(rh->name, "") == 0)
2799 /* simply promote back */
2800 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2801 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
2802 rh->id, rh->authority_name);
2803 strcpy(rh->name, rh->authority_name);
2807 /* add back to existing name */
2808 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2809 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
2810 rh->id, rh->authority_name, rh->name);
2811 //memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
2812 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2813 rh->name, rh->authority_name);
2814 //strcpy(new_name, rh->name);
2815 //strcpy(new_name+strlen(new_name), ".");
2816 //strcpy(new_name+strlen(new_name), rh->authority_name);
2817 strcpy(rh->name, new_name);
2818 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2819 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n", rh->id, rh->name);
2821 rh->proc(rh->proc_cls, rh, 0, NULL);
2826 * We found an authority that may be able to help us
2827 * move on with query
2828 * Note only 1 pkey should have been returned.. anything else would be strange
2831 for (i=0; i<rd_count;i++)
2835 * A CNAME. Like regular DNS this means the is no other record for this
2838 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_CNAME)
2840 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2841 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
2843 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2844 "GNS_PHASE_DELEGATE_NS-%llu: new name to resolve: %s.\n",
2847 rh->status |= RSL_CNAME_FOUND;
2848 rh->proc (rh->proc_cls, rh, rd_count, rd);
2855 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
2857 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2858 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
2860 rh->status |= RSL_DELEGATE_VPN;
2861 rh->proc (rh->proc_cls, rh, rd_count, rd);
2867 * FIXME make optional
2869 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS)
2871 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2872 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
2874 rh->status |= RSL_DELEGATE_NS;
2875 rh->proc (rh->proc_cls, rh, rd_count, rd);
2878 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
2881 rh->status |= RSL_DELEGATE_PKEY;
2883 if (ignore_pending_records &&
2884 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
2886 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2887 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
2893 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
2896 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2897 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
2899 if (remaining_time.rel_value == 0)
2901 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2902 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
2904 rh->authority_chain_head->fresh = 0;
2905 rh->proc(rh->proc_cls, rh, 0, NULL);
2913 * Resolve rest of query with new authority
2915 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
2916 memcpy(&rh->authority, rd[i].data,
2917 sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2918 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
2919 auth->zone = rh->authority;
2920 memset(auth->name, 0, strlen(rh->authority_name)+1);
2921 strcpy(auth->name, rh->authority_name);
2922 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2923 rh->authority_chain_tail,
2926 /** try to import pkey if private key available
2927 * TODO: Only import last one?
2929 //if (rh->priv_key && (name != NULL) && is_canonical (rh->name))
2930 // process_discovered_authority((char*)name, auth->zone,
2931 // rh->authority_chain_tail->zone,
2934 * We are done with PKEY resolution if name is empty
2935 * else resolve again with new authority
2937 if (strcmp (rh->name, "") == 0)
2938 rh->proc (rh->proc_cls, rh, rd_count, rd);
2940 resolve_delegation_ns (rh);
2947 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2948 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
2950 * If we have found some records for the LAST label
2951 * we return the results. Else null.
2953 if (strcmp(rh->name, "") == 0)
2955 /* Start shortening */
2956 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2959 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
2961 start_shorten (rh->authority_chain_tail,
2964 /* simply promote back */
2965 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2966 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
2967 rh->id, rh->authority_name);
2968 strcpy(rh->name, rh->authority_name);
2969 rh->proc(rh->proc_cls, rh, rd_count, rd);
2973 rh->proc(rh->proc_cls, rh, 0, NULL);
2979 * Resolve the delegation chain for the request in our namestore
2981 * @param rh the resolver handle
2984 resolve_delegation_ns (struct ResolverHandle *rh)
2986 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2987 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
2989 pop_tld(rh->name, rh->authority_name);
2990 GNUNET_NAMESTORE_lookup_record(namestore_handle,
2993 GNUNET_GNS_RECORD_ANY,
2994 &process_delegation_result_ns,
3001 * Lookup of a record in a specific zone
3002 * calls lookup result processor on result
3004 * @param zone the root zone
3005 * @param pzone the private local zone
3006 * @param record_type the record type to look up
3007 * @param name the name to look up
3008 * @param key a private key for use with PSEU import (can be NULL)
3009 * @param timeout timeout for resolution
3010 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3011 * @param proc the processor to call on result
3012 * @param cls the closure to pass to proc
3015 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
3016 struct GNUNET_CRYPTO_ShortHashCode pzone,
3017 uint32_t record_type,
3019 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3020 struct GNUNET_TIME_Relative timeout,
3022 RecordLookupProcessor proc,
3025 struct ResolverHandle *rh;
3026 struct RecordLookupHandle* rlh;
3027 char string_hash[MAX_DNS_LABEL_LENGTH];
3028 char nzkey[MAX_DNS_LABEL_LENGTH];
3029 char* nzkey_ptr = nzkey;
3031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3032 "Starting resolution for %s (type=%d)!\n",
3036 if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
3038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3039 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3044 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
3045 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
3047 rh->authority = zone;
3051 rh->timeout = timeout;
3052 rh->get_handle = NULL;
3053 rh->private_local_zone = pzone;
3054 rh->only_cached = only_cached;
3058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3059 "No shorten key for resolution\n");
3062 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3065 * Set timeout for authority lookup phase to 1/2
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3068 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3069 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3070 GNUNET_TIME_relative_divide(timeout, 2),
3071 &handle_lookup_timeout,
3073 rh->timeout_cont = &dht_authority_lookup_timeout;
3074 rh->timeout_cont_cls = rh;
3078 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3079 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3082 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3085 * Only 'gnunet' given
3087 strcpy(rh->name, "\0");
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3092 "Checking for TLD...\n");
3093 if (is_zkey_tld(name) == GNUNET_YES)
3095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3098 * This is a zkey tld
3099 * build hash and use as initial authority
3102 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3103 memcpy(rh->name, name,
3104 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3105 pop_tld(rh->name, string_hash);
3107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3108 "ZKEY is %s!\n", string_hash);
3110 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3112 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3116 "Cannot convert ZKEY %s to hash!\n", string_hash);
3126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3129 * Presumably GNUNET tld
3132 strlen(name)-strlen(GNUNET_GNS_TLD));
3133 memcpy(rh->name, name,
3134 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3139 * Initialize authority chain
3141 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3142 rh->authority_chain_head->prev = NULL;
3143 rh->authority_chain_head->next = NULL;
3144 rh->authority_chain_tail = rh->authority_chain_head;
3145 rh->authority_chain_head->zone = rh->authority;
3148 * Copy original query into lookup handle
3150 rlh->record_type = record_type;
3151 memset(rlh->name, 0, strlen(name) + 1);
3152 strcpy(rlh->name, name);
3154 rlh->proc_cls = cls;
3156 rh->proc = &handle_delegation_ns;
3157 resolve_delegation_ns(rh);
3160 /******** END Record Resolver ***********/
3163 * Callback calles by namestore for a zone to name
3166 * @param cls the closure
3167 * @param zone_key the zone we queried
3168 * @param expire the expiration time of the name
3169 * @param name the name found or NULL
3170 * @param rd_len number of records for the name
3171 * @param rd the record data (PKEY) for the name
3172 * @param signature the signature for the record data
3175 process_zone_to_name_shorten_root (void *cls,
3176 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3177 struct GNUNET_TIME_Absolute expire,
3179 unsigned int rd_len,
3180 const struct GNUNET_NAMESTORE_RecordData *rd,
3181 const struct GNUNET_CRYPTO_RsaSignature *signature);
3185 * Callback called by namestore for a zone to name
3188 * @param cls the closure
3189 * @param zone_key the zone we queried
3190 * @param expire the expiration time of the name
3191 * @param name the name found or NULL
3192 * @param rd_len number of records for the name
3193 * @param rd the record data (PKEY) for the name
3194 * @param signature the signature for the record data
3197 process_zone_to_name_shorten_shorten (void *cls,
3198 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3199 struct GNUNET_TIME_Absolute expire,
3201 unsigned int rd_len,
3202 const struct GNUNET_NAMESTORE_RecordData *rd,
3203 const struct GNUNET_CRYPTO_RsaSignature *signature)
3205 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3206 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3207 struct AuthorityChain *next_authority;
3209 char result[MAX_DNS_NAME_LENGTH];
3210 char tmp_name[MAX_DNS_NAME_LENGTH];
3213 /* we found a match in our own root zone */
3216 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3217 "result strlen %d\n", strlen(name));
3218 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3219 memset(result, 0, answer_len);
3221 if (strlen(rh->name) > 0)
3223 sprintf (result, "%s.%s.%s.%s.%s",
3225 nsh->shorten_zone_name, nsh->private_zone_name,
3230 sprintf (result, "%s.%s.%s.%s", name,
3231 nsh->shorten_zone_name, nsh->private_zone_name,
3235 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3236 "Found shorten result %s\n", result);
3237 if (strlen (nsh->result) > strlen (result))
3238 strcpy (nsh->result, result);
3240 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3241 nsh->shorten_zone) == 0)
3244 * This is our zone append .gnunet unless name is empty
3245 * (it shouldn't be, usually FIXME what happens if we
3246 * shorten to our zone to a "" record??)
3249 sprintf (result, "%s.%s.%s.%s",
3251 nsh->shorten_zone_name, nsh->private_zone_name,
3253 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3254 "Our zone: Found %s as shorten result\n", result);
3256 if (strlen (nsh->result) > strlen (result))
3257 strcpy (nsh->result, result);
3258 //nsh->proc(nsh->proc_cls, result);
3260 //free_resolver_handle(rh);
3267 * continue with next authority if exists
3269 if ((rh->authority_chain_head->next == NULL))
3271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3272 "Sending %s as shorten result\n", nsh->result);
3273 nsh->proc(nsh->proc_cls, nsh->result);
3275 free_resolver_handle (rh);
3278 next_authority = rh->authority_chain_head;
3280 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3281 "%s.%s", rh->name, next_authority->name);
3283 strcpy(rh->name, tmp_name);
3284 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3285 "No PSEU found for authority %s. Promoting back: %s\n",
3286 next_authority->name, rh->name);
3288 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3289 rh->authority_chain_tail,
3292 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3293 &rh->authority_chain_tail->zone,
3294 &rh->authority_chain_head->zone,
3295 &process_zone_to_name_shorten_root,
3300 * Callback calles by namestore for a zone to name
3303 * @param cls the closure
3304 * @param zone_key the zone we queried
3305 * @param expire the expiration time of the name
3306 * @param name the name found or NULL
3307 * @param rd_len number of records for the name
3308 * @param rd the record data (PKEY) for the name
3309 * @param signature the signature for the record data
3312 process_zone_to_name_shorten_private (void *cls,
3313 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3314 struct GNUNET_TIME_Absolute expire,
3316 unsigned int rd_len,
3317 const struct GNUNET_NAMESTORE_RecordData *rd,
3318 const struct GNUNET_CRYPTO_RsaSignature *signature)
3320 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3321 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3322 struct AuthorityChain *next_authority;
3324 char result[MAX_DNS_NAME_LENGTH];
3325 char tmp_name[MAX_DNS_NAME_LENGTH];
3328 /* we found a match in our own root zone */
3331 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3332 "result strlen %d\n", strlen(name));
3333 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3334 memset(result, 0, answer_len);
3336 if (strlen(rh->name) > 0)
3338 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3342 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3345 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3346 "Found shorten result %s\n", result);
3347 if (strlen (nsh->result) > strlen (result))
3348 strcpy (nsh->result, result);
3350 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3351 nsh->private_zone) == 0)
3354 * This is our zone append .gnunet unless name is empty
3355 * (it shouldn't be, usually FIXME what happens if we
3356 * shorten to our zone to a "" record??)
3359 sprintf (result, "%s.%s.%s",
3360 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3361 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3362 "Our private zone: Found %s as shorten result %s\n", result);
3363 if (strlen (nsh->result) > strlen (result))
3364 strcpy (nsh->result, result);
3367 if (nsh->shorten_zone != NULL)
3369 /* backtrack authorities for names in priv zone */
3370 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3372 &rh->authority_chain_head->zone,
3373 &process_zone_to_name_shorten_shorten,
3380 * continue with next authority if exists
3382 if ((rh->authority_chain_head->next == NULL))
3384 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3385 "Sending %s as shorten result\n", nsh->result);
3386 nsh->proc(nsh->proc_cls, nsh->result);
3388 free_resolver_handle(rh);
3391 next_authority = rh->authority_chain_head;
3393 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3394 "%s.%s", rh->name, next_authority->name);
3396 strcpy(rh->name, tmp_name);
3397 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3398 "No PSEU found for authority %s. Promoting back: %s\n",
3399 next_authority->name, rh->name);
3401 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3402 rh->authority_chain_tail,
3405 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3406 &rh->authority_chain_tail->zone,
3407 &rh->authority_chain_head->zone,
3408 &process_zone_to_name_shorten_root,
3414 * Callback calles by namestore for a zone to name
3417 * @param cls the closure
3418 * @param zone_key the zone we queried
3419 * @param expire the expiration time of the name
3420 * @param name the name found or NULL
3421 * @param rd_len number of records for the name
3422 * @param rd the record data (PKEY) for the name
3423 * @param signature the signature for the record data
3426 process_zone_to_name_shorten_root (void *cls,
3427 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3428 struct GNUNET_TIME_Absolute expire,
3430 unsigned int rd_len,
3431 const struct GNUNET_NAMESTORE_RecordData *rd,
3432 const struct GNUNET_CRYPTO_RsaSignature *signature)
3434 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3435 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3436 struct AuthorityChain *next_authority;
3438 char result[MAX_DNS_NAME_LENGTH];
3439 char tmp_name[MAX_DNS_NAME_LENGTH];
3442 /* we found a match in our own root zone */
3445 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3446 "result strlen %d\n", strlen(name));
3447 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3448 memset(result, 0, answer_len);
3450 if (strlen(rh->name) > 0)
3452 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3456 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3459 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3460 "Found shorten result %s\n", result);
3461 if (strlen (nsh->result) > strlen (result))
3462 strcpy (nsh->result, result);
3464 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3465 nsh->root_zone) == 0)
3468 * This is our zone append .gnunet unless name is empty
3469 * (it shouldn't be, usually FIXME what happens if we
3470 * shorten to our zone to a "" record??)
3473 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3474 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3475 "Our zone: Found %s as shorten result\n", result);
3476 if (strlen (nsh->result) > strlen (result))
3477 strcpy (nsh->result, result);
3480 if (nsh->private_zone != NULL)
3482 /* backtrack authorities for names in priv zone */
3483 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3485 &rh->authority_chain_head->zone,
3486 &process_zone_to_name_shorten_private,
3493 * continue with next authority if exists
3495 if ((rh->authority_chain_head->next == NULL))
3497 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3498 "Sending %s as shorten result\n", nsh->result);
3499 nsh->proc(nsh->proc_cls, nsh->result);
3501 free_resolver_handle(rh);
3504 next_authority = rh->authority_chain_head;
3506 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3507 "%s.%s", rh->name, next_authority->name);
3509 strcpy(rh->name, tmp_name);
3510 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3511 "No PSEU found for authority %s. Promoting back: %s\n",
3512 next_authority->name, rh->name);
3514 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3515 rh->authority_chain_tail,
3518 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3519 &rh->authority_chain_tail->zone,
3520 &rh->authority_chain_head->zone,
3521 &process_zone_to_name_shorten_root,
3528 * Process result from namestore delegation lookup
3529 * for shorten operation
3531 * @param cls the client shorten handle
3532 * @param rh the resolver handle
3533 * @param rd_count number of results (0)
3534 * @param rd data (NULL)
3537 handle_delegation_ns_shorten (void* cls,
3538 struct ResolverHandle *rh,
3540 const struct GNUNET_NAMESTORE_RecordData *rd)
3542 struct NameShortenHandle *nsh;
3543 char result[MAX_DNS_NAME_LENGTH];
3545 nsh = (struct NameShortenHandle *)cls;
3548 * At this point rh->name contains the part of the name
3549 * that we do not have a PKEY in our namestore to resolve.
3550 * The authority chain in the resolver handle is now
3551 * useful to backtrack if needed
3554 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3555 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3556 memset(result, 0, sizeof (result));
3558 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3559 nsh->root_zone) == 0)
3562 * This is our zone append .gnunet unless name is empty
3563 * (it shouldn't be, usually FIXME what happens if we
3564 * shorten to our zone to a "" record??)
3567 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3568 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3569 "Our zone: Found %s as shorten result\n", result);
3571 if (strlen (nsh->result) > strlen (result))
3572 strcpy (nsh->result, result);
3575 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3576 nsh->private_zone) == 0)
3579 * This is our zone append .gnunet unless name is empty
3580 * (it shouldn't be, usually FIXME what happens if we
3581 * shorten to our zone to a "" record??)
3584 sprintf (result, "%s.%s.%s",
3585 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3586 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3587 "Our zone: Found %s as shorten result %s\n", result);
3589 if (strlen (nsh->result) > strlen (result))
3590 strcpy (nsh->result, result);
3592 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3593 nsh->shorten_zone) == 0)
3596 * This is our zone append .gnunet unless name is empty
3597 * (it shouldn't be, usually FIXME what happens if we
3598 * shorten to our zone to a "" record??)
3601 sprintf (result, "%s.%s.%s",
3602 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3603 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3604 "Our zone: Found %s as shorten result\n", result);
3606 if (strlen (nsh->result) > strlen (result))
3607 strcpy (nsh->result, result);
3611 /* backtrack authorities for names */
3612 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3614 &rh->authority_chain_head->zone,
3615 &process_zone_to_name_shorten_root,
3622 * Callback calles by namestore for a zone to name
3625 * @param cls the closure
3626 * @param zone_key the zone we queried
3627 * @param expire the expiration time of the name
3628 * @param name the name found or NULL
3629 * @param rd_len number of records for the name
3630 * @param rd the record data (PKEY) for the name
3631 * @param signature the signature for the record data
3634 process_zone_to_name_zkey(void *cls,
3635 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3636 struct GNUNET_TIME_Absolute expire,
3638 unsigned int rd_len,
3639 const struct GNUNET_NAMESTORE_RecordData *rd,
3640 const struct GNUNET_CRYPTO_RsaSignature *signature)
3642 struct ResolverHandle *rh = cls;
3643 struct NameShortenHandle *nsh = rh->proc_cls;
3644 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
3645 char new_name[MAX_DNS_NAME_LENGTH];
3647 /* zkey not in our zone */
3651 * In this case we have not given this PKEY a name (yet)
3652 * It is either just not in our zone or not even cached
3653 * Since we do not know at this point we will not try to shorten
3654 * because PKEY import will happen if the user follows the zkey
3657 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
3659 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3660 "No name found for zkey %s returning verbatim!\n", enc);
3661 if (strcmp(rh->name, "") != 0)
3662 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
3663 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
3665 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3666 enc, GNUNET_GNS_TLD_ZKEY);
3668 strcpy (nsh->result, new_name);
3670 nsh->proc(nsh->proc_cls, new_name);
3672 free_resolver_handle(rh);
3676 if (strcmp(rh->name, "") != 0)
3677 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3680 strcpy(new_name, name);
3682 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3683 "Continue shorten for %s!\n", new_name);
3685 strcpy(rh->name, new_name);
3687 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3688 rh->authority_chain_tail = rh->authority_chain_head;
3689 rh->authority_chain_head->zone = rh->authority;
3692 /* Start delegation resolution in our namestore */
3693 resolve_delegation_ns(rh);
3698 * Shorten api from resolver
3700 * @param zone the root zone to use
3701 * @param pzone the private zone to use
3702 * @param szone the shorten zone to use
3703 * @param name the name to shorten
3704 * @param private_zone_name name of the private zone
3705 * @param shorten_zone_name name of the shorten zone
3706 * @param proc the processor to call with result
3707 * @param proc_cls closure to pass to proc
3710 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
3711 struct GNUNET_CRYPTO_ShortHashCode *pzone,
3712 struct GNUNET_CRYPTO_ShortHashCode *szone,
3714 const char* private_zone_name,
3715 const char* shorten_zone_name,
3716 ShortenResultProcessor proc,
3719 struct ResolverHandle *rh;
3720 struct NameShortenHandle *nsh;
3721 char string_hash[MAX_DNS_LABEL_LENGTH];
3722 struct GNUNET_CRYPTO_ShortHashCode zkey;
3723 char nzkey[MAX_DNS_LABEL_LENGTH];
3724 char* nzkey_ptr = nzkey;
3727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3728 "Starting shorten for %s!\n", name);
3730 if (is_canonical ((char*)name))
3732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3733 "%s is canonical. Returning verbatim\n", name);
3734 proc (proc_cls, name);
3738 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
3741 nsh->proc_cls = proc_cls;
3742 nsh->root_zone = zone;
3743 nsh->private_zone = pzone;
3744 nsh->shorten_zone = szone;
3745 strcpy (nsh->private_zone_name, private_zone_name);
3746 strcpy (nsh->shorten_zone_name, shorten_zone_name);
3747 strcpy (nsh->result, name);
3749 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3750 rh->authority = *zone;
3752 rh->priv_key = NULL;
3753 rh->proc = &handle_delegation_ns_shorten;
3756 rh->private_local_zone = *zone;
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3759 "Checking for TLD...\n");
3760 if (is_zkey_tld (name) == GNUNET_YES)
3762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3765 * This is a zkey tld
3766 * build hash and use as initial authority
3769 memset (rh->name, 0,
3770 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
3771 memcpy (rh->name, name,
3772 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
3773 pop_tld (rh->name, string_hash);
3775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3776 "ZKEY is %s!\n", string_hash);
3778 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
3780 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
3783 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3784 "Cannot convert ZKEY %s to hash!\n", nzkey);
3787 proc (proc_cls, name);
3791 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3794 &process_zone_to_name_zkey,
3801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3804 * Presumably GNUNET tld
3806 memset (rh->name, 0,
3807 strlen (name)-strlen (GNUNET_GNS_TLD));
3808 memcpy (rh->name, name,
3809 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
3812 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
3813 rh->authority_chain_tail = rh->authority_chain_head;
3814 rh->authority_chain_head->zone = *zone;
3817 /* Start delegation resolution in our namestore */
3818 resolve_delegation_ns (rh);
3821 /*********** END NAME SHORTEN ********************/
3825 * Process result from namestore delegation lookup
3826 * for get authority operation
3828 * @param cls the client get auth handle
3829 * @param rh the resolver handle
3830 * @param rd_count number of results (0)
3831 * @param rd data (NULL)
3834 handle_delegation_result_ns_get_auth(void* cls,
3835 struct ResolverHandle *rh,
3837 const struct GNUNET_NAMESTORE_RecordData *rd)
3839 struct GetNameAuthorityHandle* nah;
3840 char result[MAX_DNS_NAME_LENGTH];
3843 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
3846 * At this point rh->name contains the part of the name
3847 * that we do not have a PKEY in our namestore to resolve.
3848 * The authority chain in the resolver handle is now
3849 * useful to backtrack if needed
3852 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3853 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3855 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3856 "Building response!\n");
3857 if (is_canonical(rh->name))
3860 * We successfully resolved the authority in the ns
3861 * FIXME for our purposes this is fine
3862 * but maybe we want to have an api that also looks
3863 * into the dht (i.e. option in message)
3865 if (strlen(rh->name) > strlen(nah->name))
3867 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3868 "Record name longer than original lookup name... odd!\n");
3872 answer_len = strlen(nah->name) - strlen(rh->name)
3873 + strlen(GNUNET_GNS_TLD) + 1;
3874 memset(result, 0, answer_len);
3875 strcpy(result, nah->name + strlen(rh->name) + 1);
3877 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3878 "Got authority result %s\n", result);
3880 nah->proc(nah->proc_cls, result);
3882 free_resolver_handle(rh);
3886 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3887 "Unable to resolve authority for remaining %s!\n", rh->name);
3888 nah->proc(nah->proc_cls, "");
3890 free_resolver_handle(rh);
3898 * Tries to resolve the authority for name
3901 * @param zone the root zone to look up for
3902 * @param pzone the private local zone
3903 * @param name the name to lookup up
3904 * @param proc the processor to call when finished
3905 * @param proc_cls the closure to pass to the processor
3908 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
3909 struct GNUNET_CRYPTO_ShortHashCode pzone,
3911 GetAuthorityResultProcessor proc,
3914 struct ResolverHandle *rh;
3915 struct GetNameAuthorityHandle *nah;
3917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3918 "Starting authority resolution for %s!\n", name);
3920 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
3921 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
3922 rh->authority = zone;
3924 rh->private_local_zone = pzone;
3926 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3928 strcpy(rh->name, "\0");
3933 strlen(name)-strlen(GNUNET_GNS_TLD));
3934 memcpy(rh->name, name,
3935 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3938 memset(nah->name, 0,
3940 strcpy(nah->name, name);
3942 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3943 rh->authority_chain_tail = rh->authority_chain_head;
3944 rh->authority_chain_head->zone = zone;
3945 rh->proc = &handle_delegation_result_ns_get_auth;
3946 rh->proc_cls = (void*)nah;
3949 nah->proc_cls = proc_cls;
3951 /* Start delegation resolution in our namestore */
3952 resolve_delegation_ns(rh);
3956 /******** END GET AUTHORITY *************/
3958 /* end of gnunet-service-gns_resolver.c */