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.
22 * @file gns/gnunet-service-gns_resolver.c
23 * @brief GNUnet GNS resolver logic
24 * @author Martin Schanzenbach
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_vpn_service.h"
33 #include "gnunet_dns_service.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "../dns/dnsparser.h"
37 #include "gnunet_gns_service.h"
38 #include "block_gns.h"
40 #include "gnunet-service-gns_resolver.h"
42 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
43 #define DHT_GNS_REPLICATION_LEVEL 5
44 #define MAX_DNS_LABEL_LENGTH 63
48 * Our handle to the namestore service
50 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
53 * Our handle to the vpn service
55 static struct GNUNET_VPN_Handle *vpn_handle;
58 * Resolver handle to the dht
60 static struct GNUNET_DHT_Handle *dht_handle;
63 * Heap for parallel DHT lookups
65 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
68 * Heap for namestore queues
70 static struct GNUNET_CONTAINER_Heap *ns_task_heap;
73 * Maximum amount of parallel queries in background
75 static unsigned long long max_allowed_background_queries;
78 * Maximum amount of parallel namestore tasks in background
80 static unsigned long long max_allowed_ns_tasks;
83 * Wheather or not to ignore pending records
85 static int ignore_pending_records;
90 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
93 * Background shortening handles
95 static struct GetPseuAuthorityHandle *gph_head;
98 * Background shortening handles
100 static struct GetPseuAuthorityHandle *gph_tail;
103 * Resolver lookup list
105 static struct ResolverHandle *rlh_head;
108 * Resolver lookup list
110 static struct ResolverHandle *rlh_tail;
113 * Resolver shorten list
115 static struct ResolverHandle *nsh_head;
118 * Resolver shorten list
120 static struct ResolverHandle *nsh_tail;
123 * Resolver get auth list
125 static struct ResolverHandle *nah_head;
128 * Resolver get auth list
130 static struct ResolverHandle *nah_tail;
133 * Global configuration.
135 static const struct GNUNET_CONFIGURATION_Handle *cfg;
138 * a resolution identifier pool variable
140 * This is a non critical identifier useful for debugging
142 static unsigned long long rid = 0;
145 * Check if name is in srv format (_x._y.xxx)
148 * @return GNUNET_YES if true
154 int ret = GNUNET_YES;
158 if (NULL == strstr (name, "._"))
161 ndup = GNUNET_strdup (name);
164 if (NULL == strtok (NULL, "."))
167 if (NULL == strtok (NULL, "."))
170 if (NULL != strtok (NULL, "."))
179 * Determine if this name is canonical.
181 * a.b.gnunet = not canonical
184 * @param name the name to test
185 * @return GNUNET_YES if canonical
188 is_canonical (char* name)
193 ndup = GNUNET_strdup (name);
196 for (tok = strtok (NULL, "."); tok != NULL; tok = strtok (NULL, "."))
212 * Callback that shortens authorities
214 * @param gph the handle containing the name to shorten
217 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
221 * Continueation for pkey record creation (shorten)
223 * @param cls a GetPseuAuthorityHandle
224 * @param success unused
228 create_pkey_cont (void* cls, int32_t success, const char* emsg)
230 //FIXME do sth with error
231 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
232 struct AuthorityChain *iter;
234 gph->namestore_task = NULL;
237 iter = gph->ahead->next;
238 GNUNET_free (gph->ahead);
240 } while (iter != NULL);
241 GNUNET_CRYPTO_rsa_key_free (gph->key);
242 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
248 * Namestore calls this function if we have record for this name.
249 * (or with rd_count=0 to indicate no matches)
251 * @param cls the pending query
252 * @param key the key of the zone we did the lookup
253 * @param expiration expiration date of the namestore entry
254 * @param name the name for which we need an authority
255 * @param rd_count the number of records with 'name'
256 * @param rd the record data
257 * @param signature the signature of the authority for the record data
260 process_pseu_lookup_ns (void* cls,
261 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
262 struct GNUNET_TIME_Absolute expiration,
263 const char *name, unsigned int rd_count,
264 const struct GNUNET_NAMESTORE_RecordData *rd,
265 const struct GNUNET_CRYPTO_RsaSignature *signature)
267 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
268 struct GNUNET_NAMESTORE_RecordData new_pkey;
269 struct AuthorityChain *iter;
271 gph->namestore_task = NULL;
274 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
275 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
276 if (0 == strcmp (gph->name, name))
278 if (gph->ahead->next != NULL)
280 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
284 iter = gph->ahead->next;
285 GNUNET_free (gph->ahead);
287 shorten_authority_chain (gph);
295 iter = gph->ahead->next;
296 GNUNET_free (gph->ahead);
298 } while (iter != NULL);
299 GNUNET_CRYPTO_rsa_key_free (gph->key);
300 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "GNS_AUTO_PSEU: Trying delegated name %s\n", gph->name);
307 memcpy (gph->test_name, gph->name, strlen (gph->name)+1);
308 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
311 GNUNET_NAMESTORE_TYPE_ANY,
312 &process_pseu_lookup_ns,
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
321 new_pkey.expiration_time = UINT64_MAX;
322 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
323 new_pkey.data = &gph->ahead->zone;
324 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
325 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
326 | GNUNET_NAMESTORE_RF_PRIVATE
327 | GNUNET_NAMESTORE_RF_PENDING;
328 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
332 &create_pkey_cont, //cont
337 * process result of a dht pseu lookup
339 * @param gph the handle
340 * @param name the pseu result or NULL
343 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
347 memcpy (gph->test_name, gph->ahead->name, strlen (gph->ahead->name)+1);
351 memcpy (gph->test_name, name, strlen(name)+1);
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
359 * Check for collision
361 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
364 GNUNET_NAMESTORE_TYPE_ANY,
365 &process_pseu_lookup_ns,
370 * Handle timeout for dht request
372 * @param cls the request handle as closure
373 * @param tc the task context
376 handle_auth_discovery_timeout (void *cls,
377 const struct GNUNET_SCHEDULER_TaskContext *tc)
379 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
380 struct AuthorityChain *iter;
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
384 GNUNET_DHT_get_stop (gph->get_handle);
385 gph->get_handle = NULL;
387 if (gph->ahead->next != NULL)
389 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
393 iter = gph->ahead->next;
394 GNUNET_free (gph->ahead);
396 shorten_authority_chain (gph);
401 process_pseu_result (gph, NULL);
405 * Function called when we find a PSEU entry in the DHT
407 * @param cls the request handle
408 * @param exp lifetime
409 * @param key the key the record was stored under
410 * @param get_path get path
411 * @param get_path_length get path length
412 * @param put_path put path
413 * @param put_path_length put path length
414 * @param type the block type
415 * @param size the size of the record
416 * @param data the record data
419 process_auth_discovery_dht_result (void* cls,
420 struct GNUNET_TIME_Absolute exp,
421 const struct GNUNET_HashCode * key,
422 const struct GNUNET_PeerIdentity *get_path,
423 unsigned int get_path_length,
424 const struct GNUNET_PeerIdentity *put_path,
425 unsigned int put_path_length,
426 enum GNUNET_BLOCK_Type type,
427 size_t size, const void *data)
429 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
430 struct AuthorityChain *iter;
431 struct GNSNameRecordBlock *nrb;
432 char* rd_data = (char*)data;
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
442 /* stop lookup and timeout task */
443 GNUNET_DHT_get_stop (gph->get_handle);
444 gph->get_handle = NULL;
445 GNUNET_SCHEDULER_cancel (gph->timeout);
449 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
450 "GNS_GET_AUTH: got dht result null!\n", size);
454 iter = gph->ahead->next;
455 GNUNET_free (gph->ahead);
457 } while (iter != NULL);
458 GNUNET_CRYPTO_rsa_key_free (gph->key);
459 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
464 nrb = (struct GNSNameRecordBlock*)data;
468 nrb = (struct GNSNameRecordBlock*)data;
470 name = (char*)&nrb[1];
471 num_records = ntohl (nrb->rd_count);
473 struct GNUNET_NAMESTORE_RecordData rd[num_records];
475 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
476 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
478 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
484 "GNS_GET_AUTH: Error deserializing data!\n");
488 for (i=0; i < num_records; i++)
490 if ((strcmp (name, "+") == 0) &&
491 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
494 process_pseu_result (gph, (char*)rd[i].data);
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in dht!\n");
503 if (gph->ahead->next != NULL)
505 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
509 iter = gph->ahead->next;
510 GNUNET_free (gph->ahead);
512 shorten_authority_chain (gph);
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517 "GNS_GET_AUTH: finished shorten, no results!\n");
518 process_pseu_result (gph, NULL);
522 * Process PSEU discovery for shorten via namestore
524 * @param cls the GetPseuAuthorityHandle
525 * @param key the public key
526 * @param expiration recorddata expiration
527 * @param name the looked up name
528 * @param rd_count number of records in set
529 * @param rd record data
530 * @param signature the signature
533 process_auth_discovery_ns_result (void* cls,
534 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
535 struct GNUNET_TIME_Absolute expiration,
537 unsigned int rd_count,
538 const struct GNUNET_NAMESTORE_RecordData *rd,
539 const struct GNUNET_CRYPTO_RsaSignature *signature)
542 struct GNUNET_CRYPTO_ShortHashCode name_hash;
543 struct GNUNET_HashCode lookup_key;
544 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
545 struct GNUNET_HashCode name_hash_double;
546 struct GNUNET_HashCode zone_hash_double;
548 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
549 struct AuthorityChain *iter;
551 gph->namestore_task = NULL;
558 GNUNET_CRYPTO_short_hash ("+", strlen ("+"), &name_hash);
559 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
560 GNUNET_CRYPTO_short_hash_double (&gph->ahead->zone, &zone_hash_double);
561 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
562 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
565 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
566 "+", (char*)&lookup_key_string);
568 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
569 &handle_auth_discovery_timeout, gph);
571 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
573 GNUNET_assert (gph->get_handle == NULL);
575 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
576 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
578 DHT_GNS_REPLICATION_LEVEL,
582 &process_auth_discovery_dht_result,
587 for (i=0; i < rd_count; i++)
589 if ((strcmp (name, "+") == 0) &&
590 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
593 process_pseu_result (gph, (char*)rd[i].data);
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
600 if (gph->ahead->next != NULL)
602 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
606 iter = gph->ahead->next;
607 GNUNET_free (gph->ahead);
609 shorten_authority_chain (gph);
614 process_pseu_result (gph, NULL);
618 * Callback called by namestore for a zone to name
621 * @param cls the closure
622 * @param zone_key the zone we queried
623 * @param expire the expiration time of the name
624 * @param name the name found or NULL
625 * @param rd_len number of records for the name
626 * @param rd the record data (PKEY) for the name
627 * @param signature the signature for the record data
630 process_zone_to_name_discover (void *cls,
631 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
632 struct GNUNET_TIME_Absolute expire,
635 const struct GNUNET_NAMESTORE_RecordData *rd,
636 const struct GNUNET_CRYPTO_RsaSignature *signature)
638 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
639 struct AuthorityChain *iter;
641 gph->namestore_task = NULL;
642 /* we found a match in our own zone */
645 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
646 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
651 iter = gph->ahead->next;
652 GNUNET_free (gph->ahead);
654 } while (iter != NULL);
655 GNUNET_CRYPTO_rsa_key_free (gph->key);
656 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
662 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
665 GNUNET_GNS_RECORD_PSEU,
666 &process_auth_discovery_ns_result,
674 * Callback that shortens authorities
676 * @param gph the handle to the shorten request
679 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
683 "GNS_AUTO_PSEU: New authority %s discovered\n",
686 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
689 &process_zone_to_name_discover,
695 start_shorten (struct AuthorityChain *atail,
696 struct GNUNET_CRYPTO_RsaPrivateKey *key)
698 struct AuthorityChain *new_head = NULL;
699 struct AuthorityChain *new_tail = NULL;
700 struct AuthorityChain *iter;
701 struct AuthorityChain *acopy;
702 struct GetPseuAuthorityHandle *gph;
703 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
704 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
706 /* First copy the authority chain in reverse order */
707 for (iter = atail; iter != NULL; iter = iter->prev)
709 acopy = GNUNET_malloc (sizeof (struct AuthorityChain));
710 memcpy (acopy, iter, sizeof (struct AuthorityChain));
713 GNUNET_CONTAINER_DLL_insert (new_head, new_tail, acopy);
716 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
718 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
719 pb_key = GNUNET_CRYPTO_rsa_encode_key (key);
720 gph->key = GNUNET_CRYPTO_rsa_decode_key ((char*)pb_key, ntohs (pb_key->len));
722 GNUNET_CRYPTO_short_hash (&pkey,
723 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
725 gph->ahead = new_head;
727 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
729 shorten_authority_chain (gph);
733 * Initialize the resolver
735 * @param nh the namestore handle
736 * @param dh the dht handle
737 * @param lz the local zone's hash
738 * @param c configuration handle
739 * @param max_bg_queries maximum number of parallel background queries in dht
740 * @param ignore_pending ignore records that still require user confirmation
742 * @return GNUNET_OK on success
745 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
746 struct GNUNET_DHT_Handle *dh,
747 struct GNUNET_CRYPTO_ShortHashCode lz,
748 const struct GNUNET_CONFIGURATION_Handle *c,
749 unsigned long long max_bg_queries,
753 namestore_handle = nh;
757 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
759 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
760 max_allowed_background_queries = max_bg_queries;
761 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
763 ignore_pending_records = ignore_pending;
774 GNUNET_RESOLVER_connect (cfg);
777 if ((namestore_handle != NULL) && (dht_handle != NULL))
782 return GNUNET_SYSERR;
789 * @param cls closure to iterator
790 * @param node heap nodes
791 * @param element the namestorebgtask
792 * @param cost heap cost
793 * @return always GNUNET_YES
796 cleanup_pending_ns_tasks (void* cls,
797 struct GNUNET_CONTAINER_HeapNode *node,
799 GNUNET_CONTAINER_HeapCostType cost)
801 struct NamestoreBGTask *nbg = (struct NamestoreBGTask *)element;
802 ResolverCleanupContinuation cont = cls;
804 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
805 "GNS_CLEANUP: Terminating ns task\n");
806 GNUNET_NAMESTORE_cancel (nbg->qe);
808 GNUNET_CONTAINER_heap_remove_node (node);
810 if (GNUNET_CONTAINER_heap_get_size (ns_task_heap) == 0)
820 * @param rh resolver handle
821 * @param rlh record lookup handle
822 * @param rd_count number of results
826 finish_lookup (struct ResolverHandle *rh,
827 struct RecordLookupHandle* rlh,
828 unsigned int rd_count,
829 const struct GNUNET_NAMESTORE_RecordData *rd);
833 * Cleanup background lookups FIXME get rid of this??
835 * @param cls closure to iterator
836 * @param node heap nodes
837 * @param element the resolver handle
838 * @param cost heap cost
839 * @return always GNUNET_YES
842 cleanup_pending_background_queries (void* cls,
843 struct GNUNET_CONTAINER_HeapNode *node,
845 GNUNET_CONTAINER_HeapCostType cost)
847 struct ResolverHandle *rh = (struct ResolverHandle *)element;
848 ResolverCleanupContinuation cont = cls;
850 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
851 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
854 GNUNET_CONTAINER_heap_remove_node (node);
856 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) == 0)
858 if (GNUNET_CONTAINER_heap_get_size (ns_task_heap) == 0)
862 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
863 &cleanup_pending_ns_tasks,
874 * Helper function to free resolver handle
876 * @param rh the handle to free
879 free_resolver_handle (struct ResolverHandle* rh)
881 struct AuthorityChain *ac;
882 struct AuthorityChain *ac_next;
887 ac = rh->authority_chain_head;
896 if (NULL != rh->get_handle)
897 GNUNET_DHT_get_stop (rh->get_handle);
899 if (NULL != rh->dns_raw_packet)
900 GNUNET_free (rh->dns_raw_packet);
902 if (NULL != rh->namestore_task)
903 GNUNET_NAMESTORE_cancel (rh->namestore_task);
904 rh->namestore_task = NULL;
906 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
907 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
909 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
910 GNUNET_SCHEDULER_cancel (rh->timeout_task);
912 if (NULL != rh->dns_sock)
913 GNUNET_NETWORK_socket_close (rh->dns_sock);
914 if (NULL != rh->dns_resolver_handle)
915 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
917 if (NULL != rh->rd.data)
918 GNUNET_free ((void*)(rh->rd.data));
926 * @param rh resolver handle
927 * @param nsh name shorten handle
930 finish_shorten (struct ResolverHandle *rh,
931 struct NameShortenHandle *nsh);
935 * @param rh resolver handle
936 * @param nah get name authority handle
939 finish_get_auth (struct ResolverHandle *rh,
940 struct GetNameAuthorityHandle* rlh);
945 gns_resolver_cleanup (ResolverCleanupContinuation cont)
948 struct GetPseuAuthorityHandle *tmp;
949 struct AuthorityChain *iter;
953 for (tmp = gph_head; tmp != NULL; tmp = gph_head)
955 if (tmp->get_handle != NULL)
956 GNUNET_DHT_get_stop (tmp->get_handle);
957 tmp->get_handle = NULL;
958 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
959 GNUNET_SCHEDULER_cancel (tmp->timeout);
960 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
962 if (NULL != tmp->namestore_task)
963 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
964 tmp->namestore_task = NULL;
969 iter = tmp->ahead->next;
970 GNUNET_free (tmp->ahead);
972 } while (iter != NULL);
974 GNUNET_CRYPTO_rsa_key_free (tmp->key);
975 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, tmp);
979 while (NULL != rlh_head)
981 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
984 while (NULL != nsh_head)
986 finish_shorten (nsh_head, nsh_head->proc_cls);
989 while (NULL != nah_head)
991 finish_get_auth (nah_head, nah_head->proc_cls);
994 s = GNUNET_CONTAINER_heap_get_size (dht_lookup_heap);
995 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
996 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
998 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
999 &cleanup_pending_background_queries,
1001 else if (0 != GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1003 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
1004 &cleanup_pending_ns_tasks,
1015 * Callback when record data is put into namestore
1017 * @param cls the closure
1018 * @param success GNUNET_OK on success
1019 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
1022 on_namestore_record_put_result (void *cls,
1026 struct NamestoreBGTask *nbg = cls;
1028 GNUNET_CONTAINER_heap_remove_node (nbg->node);
1031 if (GNUNET_NO == success)
1033 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1034 "GNS_NS: records already in namestore\n");
1037 else if (GNUNET_YES == success)
1039 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1040 "GNS_NS: records successfully put in namestore\n");
1044 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1045 "GNS_NS: Error putting records into namestore: %s\n", emsg);
1049 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1051 struct ResolverHandle *rh = cls;
1053 if (rh->timeout_cont)
1054 rh->timeout_cont (rh->timeout_cont_cls, tc);
1058 * Processor for background lookups in the DHT
1060 * @param cls closure (NULL)
1061 * @param rd_count number of records found (not 0)
1062 * @param rd record data
1065 background_lookup_result_processor (void *cls,
1067 const struct GNUNET_NAMESTORE_RecordData *rd)
1069 //We could do sth verbose/more useful here but it doesn't make any difference
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "GNS_BG: background dht lookup finished. (%d results)\n",
1076 * Handle timeout for DHT requests
1078 * @param cls the request handle as closure
1079 * @param tc the task context
1082 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1084 struct ResolverHandle *rh = cls;
1085 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1086 char new_name[MAX_DNS_NAME_LENGTH];
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1089 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
1090 rh->id, rh->name, rh->timeout.rel_value);
1092 * Start resolution in bg
1094 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
1095 rh->name, GNUNET_GNS_TLD);
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1098 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
1099 rh->id, new_name, rlh->record_type);
1101 gns_resolver_lookup_record (rh->authority,
1102 rh->private_local_zone,
1106 GNUNET_TIME_UNIT_FOREVER_REL,
1108 &background_lookup_result_processor,
1110 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1112 GNUNET_DHT_get_stop (rh->get_handle);
1113 rh->get_handle = NULL;
1114 rh->proc (rh->proc_cls, rh, 0, NULL);
1119 * Function called when we get a result from the dht
1120 * for our record query
1122 * @param cls the request handle
1123 * @param exp lifetime
1124 * @param key the key the record was stored under
1125 * @param get_path get path
1126 * @param get_path_length get path length
1127 * @param put_path put path
1128 * @param put_path_length put path length
1129 * @param type the block type
1130 * @param size the size of the record
1131 * @param data the record data
1134 process_record_result_dht (void* cls,
1135 struct GNUNET_TIME_Absolute exp,
1136 const struct GNUNET_HashCode * key,
1137 const struct GNUNET_PeerIdentity *get_path,
1138 unsigned int get_path_length,
1139 const struct GNUNET_PeerIdentity *put_path,
1140 unsigned int put_path_length,
1141 enum GNUNET_BLOCK_Type type,
1142 size_t size, const void *data)
1144 struct ResolverHandle *rh;
1145 struct RecordLookupHandle *rlh;
1146 struct GNSNameRecordBlock *nrb;
1147 uint32_t num_records;
1149 char* rd_data = (char*)data;
1153 rh = (struct ResolverHandle *)cls;
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1155 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
1157 rlh = (struct RecordLookupHandle *) rh->proc_cls;
1158 nrb = (struct GNSNameRecordBlock*)data;
1160 /* stop lookup and timeout task */
1161 GNUNET_DHT_get_stop (rh->get_handle);
1162 rh->get_handle = NULL;
1164 if (rh->dht_heap_node != NULL)
1166 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1167 rh->dht_heap_node = NULL;
1170 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1172 GNUNET_SCHEDULER_cancel (rh->timeout_task);
1173 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1176 rh->get_handle = NULL;
1177 name = (char*)&nrb[1];
1178 num_records = ntohl (nrb->rd_count);
1180 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1181 struct NamestoreBGTask *ns_heap_root;
1182 struct NamestoreBGTask *namestore_bg_task;
1184 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
1185 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
1187 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1193 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1197 for (i=0; i<num_records; i++)
1199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1200 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1201 rh->id, name, rh->name);
1202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1203 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1204 rh->id, rd[i].record_type, rlh->record_type);
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1206 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1207 rh->id, rd[i].data_size);
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1209 "GNS_PHASE_REC-%llu: Got flag %d\n",
1210 rh->id, rd[i].flags);
1212 if ((strcmp (name, rh->name) == 0) &&
1213 (rd[i].record_type == rlh->record_type))
1221 * FIXME check pubkey against existing key in namestore?
1222 * https://gnunet.org/bugs/view.php?id=2179
1224 if (max_allowed_ns_tasks <=
1225 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1227 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1228 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1231 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1235 /* Save to namestore */
1236 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1237 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1244 &on_namestore_record_put_result, //cont
1247 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1249 GNUNET_TIME_absolute_get().abs_value);
1253 rh->proc (rh->proc_cls, rh, num_records, rd);
1255 rh->proc (rh->proc_cls, rh, 0, NULL);
1262 * Start DHT lookup for a (name -> query->record_type) record in
1263 * rh->authority's zone
1265 * @param rh the pending gns query context
1268 resolve_record_dht (struct ResolverHandle *rh)
1271 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1272 struct GNUNET_HashCode lookup_key;
1273 struct GNUNET_HashCode name_hash_double;
1274 struct GNUNET_HashCode zone_hash_double;
1275 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
1276 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1277 struct ResolverHandle *rh_heap_root;
1279 GNUNET_CRYPTO_short_hash (rh->name, strlen (rh->name), &name_hash);
1280 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
1281 GNUNET_CRYPTO_short_hash_double (&rh->authority, &zone_hash_double);
1282 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
1283 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
1285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1286 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1287 rh->id, rh->name, (char*)&lookup_key_string);
1289 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1290 rh->dht_heap_node = NULL;
1292 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1295 * Update timeout if necessary
1297 if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 "GNS_PHASE_REC-%llu: Adjusting timeout\n", rh->id);
1303 * Set timeout for authority lookup phase to 1/2
1305 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1306 GNUNET_TIME_relative_divide (rh->timeout, 2),
1307 &handle_lookup_timeout,
1310 rh->timeout_cont = &dht_lookup_timeout;
1311 rh->timeout_cont_cls = rh;
1315 if (max_allowed_background_queries <=
1316 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1318 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1319 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1320 rh_heap_root->get_handle = NULL;
1321 rh_heap_root->dht_heap_node = NULL;
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1325 rh->id, rh_heap_root->name);
1326 rh_heap_root->proc (rh_heap_root->proc_cls,
1331 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1333 GNUNET_TIME_absolute_get ().abs_value);
1336 xquery = htonl (rlh->record_type);
1338 GNUNET_assert (rh->get_handle == NULL);
1339 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1340 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1342 DHT_GNS_REPLICATION_LEVEL,
1346 &process_record_result_dht,
1353 * Namestore calls this function if we have record for this name.
1354 * (or with rd_count=0 to indicate no matches)
1356 * @param cls the pending query
1357 * @param key the key of the zone we did the lookup
1358 * @param expiration expiration date of the namestore entry
1359 * @param name the name for which we need an authority
1360 * @param rd_count the number of records with 'name'
1361 * @param rd the record data
1362 * @param signature the signature of the authority for the record data
1365 process_record_result_ns (void* cls,
1366 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1367 struct GNUNET_TIME_Absolute expiration,
1368 const char *name, unsigned int rd_count,
1369 const struct GNUNET_NAMESTORE_RecordData *rd,
1370 const struct GNUNET_CRYPTO_RsaSignature *signature)
1372 struct ResolverHandle *rh;
1373 struct RecordLookupHandle *rlh;
1374 struct GNUNET_TIME_Relative remaining_time;
1375 struct GNUNET_CRYPTO_ShortHashCode zone;
1376 struct GNUNET_TIME_Absolute et;
1379 rh = (struct ResolverHandle *) cls;
1380 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1382 rh->namestore_task = NULL;
1383 GNUNET_CRYPTO_short_hash (key,
1384 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1386 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1394 rh->status |= RSL_RECORD_EXISTS;
1396 if (remaining_time.rel_value == 0)
1398 rh->status |= RSL_RECORD_EXPIRED;
1405 * Lookup terminated and no results
1407 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1408 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1411 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1412 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1415 * Our zone and no result? Cannot resolve TT
1417 rh->proc(rh->proc_cls, rh, 0, NULL);
1423 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1424 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1426 for (i=0; i<rd_count;i++)
1428 if (rd[i].record_type != rlh->record_type)
1431 if (ignore_pending_records &&
1432 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1434 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1435 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1440 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1441 et.abs_value = rd[i].expiration_time;
1442 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
1445 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1446 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1456 if (rh->answered == 0)
1458 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1459 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1460 rh->proc(rh->proc_cls, rh, 0, NULL);
1464 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1465 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1466 rh->id, rh->answered, rd_count);
1468 rh->proc(rh->proc_cls, rh, rd_count, rd);
1474 * VPN redirect result callback
1476 * @param cls the resolver handle
1477 * @param af the requested address family
1478 * @param address in_addr(6) respectively
1481 process_record_result_vpn (void* cls, int af, const void *address)
1483 struct ResolverHandle *rh = cls;
1484 struct RecordLookupHandle *rlh;
1485 struct GNUNET_NAMESTORE_RecordData rd;
1487 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1489 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1490 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1494 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1495 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1497 if (rlh->record_type != GNUNET_GNS_RECORD_A)
1499 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1500 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1502 rh->proc (rh->proc_cls, rh, 0, NULL);
1505 rd.record_type = GNUNET_GNS_RECORD_A;
1506 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1508 rd.data_size = sizeof (struct in_addr);
1510 rh->proc (rh->proc_cls, rh, 1, &rd);
1513 else if (af == AF_INET6)
1515 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1516 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1518 if (rlh->record_type != GNUNET_GNS_RECORD_AAAA)
1520 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1521 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1523 rh->proc (rh->proc_cls, rh, 0, NULL);
1526 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1527 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1529 rd.data_size = sizeof (struct in6_addr);
1531 rh->proc (rh->proc_cls, rh, 1, &rd);
1535 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1536 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1538 rh->proc (rh->proc_cls, rh, 0, NULL);
1545 * Process VPN lookup result for record
1547 * @param cls the record lookup handle
1548 * @param rh resolver handle
1549 * @param rd_count number of results (1)
1550 * @param rd record data containing the result
1553 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1554 unsigned int rd_count,
1555 const struct GNUNET_NAMESTORE_RecordData *rd)
1557 struct RecordLookupHandle* rlh = (struct RecordLookupHandle*) cls;
1561 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1562 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1565 /* give up, cannot resolve */
1566 finish_lookup(rh, rlh, 0, NULL);
1570 /* results found yay */
1571 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1572 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1574 finish_lookup(rh, rlh, rd_count, rd);
1579 * Sends a UDP dns query to a nameserver specified in the rh
1581 * @param rh the resolver handle
1584 send_dns_packet (struct ResolverHandle *rh);
1588 handle_dns_resolver (void *cls,
1589 const struct sockaddr *addr,
1592 struct ResolverHandle *rh = cls;
1593 struct RecordLookupHandle *rlh = rh->proc_cls;
1594 struct GNUNET_NAMESTORE_RecordData rd;
1595 struct sockaddr_in *sai;
1596 struct sockaddr_in6 *sai6;
1600 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1601 "No address found in DNS!\n");
1602 finish_lookup (rh, rlh, 0, NULL);
1606 if (addrlen == sizeof (struct sockaddr_in))
1608 sai = (struct sockaddr_in*) addr;
1609 rd.record_type = GNUNET_GNS_RECORD_A;
1610 rd.data_size = sizeof (struct in_addr);
1611 rd.data = &sai->sin_addr;
1615 sai6 = (struct sockaddr_in6*) addr;
1616 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1617 rd.data_size = sizeof (struct in6_addr);
1618 rd.data = &sai6->sin6_addr;
1621 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1624 finish_lookup (rh, rlh, 1, &rd);
1629 * Resolve DNS name via local stub resolver
1631 * @param rh the resolver handle
1634 resolve_dns_name (struct ResolverHandle *rh)
1636 struct RecordLookupHandle *rlh = rh->proc_cls;
1639 if ((rlh->record_type != GNUNET_GNS_RECORD_A) &&
1640 (rlh->record_type != GNUNET_GNS_RECORD_AAAA))
1642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1643 "Can only resolve A/AAAA via stub... abort\n");
1644 finish_lookup (rh, rlh, 0, NULL);
1648 if (rlh->record_type == GNUNET_GNS_RECORD_A)
1653 //GNUNET_RESOLVER_connect (cfg); FIXME into init
1655 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1658 &handle_dns_resolver,
1664 * Read DNS udp packet from socket
1666 * @param cls the resolver handle
1667 * @param tc task context
1670 read_dns_response (void *cls,
1671 const struct GNUNET_SCHEDULER_TaskContext *tc)
1673 struct ResolverHandle *rh = cls;
1674 struct RecordLookupHandle *rlh = rh->proc_cls;
1675 char buf[UINT16_MAX];
1677 struct sockaddr_in addr;
1679 struct GNUNET_DNSPARSER_Packet *packet;
1680 struct GNUNET_NAMESTORE_RecordData rd;
1681 int found_delegation = GNUNET_NO;
1682 int found_cname = GNUNET_NO;
1683 char* delegation_name = NULL;
1684 int zone_offset = 0;
1687 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1688 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1690 /* timeout or shutdown */
1691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1692 "Terminating DNS query %d\n", tc->reason);
1693 finish_lookup (rh, rlh, 0, NULL);
1697 addrlen = sizeof (addr);
1698 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1700 (struct sockaddr*) &addr,
1705 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1706 finish_lookup (rh, rlh, 0, NULL);
1710 packet = GNUNET_DNSPARSER_parse (buf, r);
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1715 "Failed to parse DNS reply!\n");
1716 finish_lookup (rh, rlh, 0, NULL);
1720 for (i = 0; i < packet->num_answers; i++)
1722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1723 "Got record type %d (want %d)\n",
1724 packet->answers[i].type,
1726 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1727 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1730 "CNAME... restarting query with %s\n",
1731 packet->answers[i].data.hostname
1733 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1734 found_cname = GNUNET_YES;
1735 //send_dns_packet (rh);
1736 //GNUNET_DNSPARSER_free_packet (packet);
1740 if ((packet->answers[i].type == rlh->record_type) &&
1741 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1745 rd.data = packet->answers[i].data.raw.data;
1746 rd.data_size = packet->answers[i].data.raw.data_len;
1747 rd.record_type = packet->answers[i].type;
1749 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1750 finish_lookup (rh, rlh, 1, &rd);
1751 GNUNET_DNSPARSER_free_packet (packet);
1756 if (GNUNET_YES == found_cname)
1758 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1760 if (0 > zone_offset)
1763 /* restart query with CNAME */
1764 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1767 "Asking same server for %s\n", rh->dns_name);
1768 send_dns_packet (rh);
1772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1773 "Trying system resolver for %s\n", rh->dns_name);
1774 resolve_dns_name (rh);
1777 GNUNET_DNSPARSER_free_packet (packet);
1781 for (i = 0; i < packet->num_authority_records; i++)
1784 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1787 "Found NS delegation!\n");
1788 found_delegation = GNUNET_YES;
1789 delegation_name = packet->authority_records[i].data.hostname;
1794 for (i = 0; i < packet->num_additional_records; i++)
1796 if (found_delegation == GNUNET_NO)
1799 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1800 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1802 GNUNET_assert (sizeof (struct in_addr) ==
1803 packet->authority_records[i].data.raw.data_len);
1805 rh->dns_addr.sin_addr =
1806 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1807 send_dns_packet (rh);
1808 GNUNET_DNSPARSER_free_packet (packet);
1813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1814 "Nothing useful in DNS reply!\n");
1815 finish_lookup (rh, rlh, 0, NULL);
1816 GNUNET_DNSPARSER_free_packet (packet);
1821 * Sends a UDP dns query to a nameserver specified in the rh
1823 * @param rh the request handle
1826 send_dns_packet (struct ResolverHandle *rh)
1828 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1829 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1832 "Sending %dbyte DNS query\n",
1833 rh->dns_raw_packet_size);
1835 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1837 rh->dns_raw_packet_size,
1838 (struct sockaddr*)&rh->dns_addr,
1839 sizeof (struct sockaddr_in));
1841 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1842 rh->timeout, //FIXME less?
1848 GNUNET_NETWORK_fdset_destroy (rset);
1853 * The final phase of resoution.
1854 * We found a NS RR and want to resolve via DNS
1856 * @param rh the pending lookup handle
1857 * @param rd_count length of record data
1858 * @param rd record data containing VPN RR
1861 resolve_record_dns (struct ResolverHandle *rh,
1863 const struct GNUNET_NAMESTORE_RecordData *rd)
1865 struct GNUNET_DNSPARSER_Query query;
1866 struct GNUNET_DNSPARSER_Packet packet;
1867 struct GNUNET_DNSPARSER_Flags flags;
1868 struct in_addr dnsip;
1869 struct sockaddr_in addr;
1870 struct sockaddr *sa;
1872 struct RecordLookupHandle *rlh = rh->proc_cls;
1874 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1876 /* We cancel here as to not include the ns lookup in the timeout */
1877 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1879 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1880 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1882 /* Start shortening */
1883 if ((rh->priv_key != NULL) &&
1884 (is_canonical (rh->name) == GNUNET_YES))
1886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1887 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1889 start_shorten (rh->authority_chain_tail,
1893 for (i = 0; i < rd_count; i++)
1895 /* Synthesize dns name */
1896 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
1898 strcpy (rh->dns_zone, (char*)rd[i].data);
1899 if (0 == strcmp (rh->name, ""))
1900 strcpy (rh->dns_name, (char*)rd[i].data);
1902 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1905 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
1906 dnsip = *((struct in_addr*)rd[i].data);
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1910 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1914 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1915 if (rh->dns_sock == NULL)
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1918 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1920 finish_lookup (rh, rlh, 0, NULL);
1924 memset (&addr, 0, sizeof (struct sockaddr_in));
1925 sa = (struct sockaddr *) &addr;
1926 sa->sa_family = AF_INET;
1927 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1929 sizeof (struct sockaddr_in)))
1931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1932 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1934 finish_lookup (rh, rlh, 0, NULL);
1938 /*TODO create dnsparser query, serialize, sendto, handle reply*/
1939 query.name = rh->dns_name;
1940 query.type = rlh->record_type;
1941 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1942 memset (&flags, 0, sizeof (flags));
1943 flags.recursion_desired = 1;
1944 flags.checking_disabled = 1;
1945 packet.queries = &query;
1946 packet.answers = NULL;
1947 packet.authority_records = NULL;
1948 packet.num_queries = 1;
1949 packet.num_answers = 0;
1950 packet.num_authority_records = 0;
1951 packet.num_additional_records = 0;
1952 packet.flags = flags;
1954 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1956 &rh->dns_raw_packet,
1957 &rh->dns_raw_packet_size))
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1960 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1962 GNUNET_NETWORK_socket_close (rh->dns_sock);
1963 finish_lookup (rh, rlh, 0, NULL);
1967 rh->dns_addr.sin_family = AF_INET;
1968 rh->dns_addr.sin_port = htons (53); //domain
1969 rh->dns_addr.sin_addr = dnsip;
1970 #if HAVE_SOCKADDR_IN_SIN_LEN
1971 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1974 send_dns_packet (rh);
1979 * The final phase of resoution.
1980 * We found a VPN RR and want to request an IPv4/6 address
1982 * @param rh the pending lookup handle
1983 * @param rd_count length of record data
1984 * @param rd record data containing VPN RR
1987 resolve_record_vpn (struct ResolverHandle *rh,
1989 const struct GNUNET_NAMESTORE_RecordData *rd)
1991 struct RecordLookupHandle *rlh = rh->proc_cls;
1992 struct GNUNET_HashCode serv_desc;
1993 struct vpn_data* vpn;
1996 /* We cancel here as to not include the ns lookup in the timeout */
1997 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1999 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2000 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2002 /* Start shortening */
2003 if ((rh->priv_key != NULL) &&
2004 (is_canonical (rh->name) == GNUNET_YES))
2006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2007 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
2009 start_shorten (rh->authority_chain_tail,
2013 vpn = (struct vpn_data*)rd->data;
2016 GNUNET_CRYPTO_hash ((char*)&vpn[1],
2017 strlen ((char*)&vpn[1]) + 1,
2019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2020 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
2023 GNUNET_h2s (&vpn->peer));
2025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2026 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
2029 GNUNET_h2s (&serv_desc));
2030 rh->proc = &handle_record_vpn;
2032 if (NULL == vpn_handle)
2034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2035 "GNS_PHASE_REC_VPN-%llu: VPN not connected!\n",
2037 finish_lookup (rh, rh->proc_cls, 0, NULL);
2041 if (rlh->record_type == GNUNET_GNS_RECORD_A)
2047 if (NULL == vpn_handle)
2049 vpn_handle = GNUNET_VPN_connect (cfg);
2050 if (NULL == vpn_handle)
2052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2053 "GNS_PHASE_INIT: Error connecting to VPN!\n");
2054 finish_lookup (rh, rh->proc_cls, 0, NULL);
2060 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
2061 af, ntohs (vpn->proto),
2062 (struct GNUNET_PeerIdentity *)&vpn->peer,
2065 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
2066 &process_record_result_vpn,
2072 * The final phase of resolution.
2073 * rh->name is a name that is canonical and we do not have a delegation.
2074 * Query namestore for this record
2076 * @param rh the pending lookup handle
2079 resolve_record_ns(struct ResolverHandle *rh)
2081 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
2083 /* We cancel here as to not include the ns lookup in the timeout */
2084 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2086 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2087 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2089 /* Start shortening */
2090 if ((rh->priv_key != NULL) &&
2091 (is_canonical (rh->name) == GNUNET_YES))
2093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2094 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
2096 start_shorten (rh->authority_chain_tail,
2101 * Try to resolve this record in our namestore.
2102 * The name to resolve is now in rh->authority_name
2103 * since we tried to resolve it to an authority
2106 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
2110 &process_record_result_ns,
2117 * Handle timeout for DHT requests
2119 * @param cls the request handle as closure
2120 * @param tc the task context
2123 dht_authority_lookup_timeout(void *cls,
2124 const struct GNUNET_SCHEDULER_TaskContext *tc)
2126 struct ResolverHandle *rh = cls;
2127 struct RecordLookupHandle *rlh = rh->proc_cls;
2128 char new_name[MAX_DNS_NAME_LENGTH];
2130 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2131 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
2132 rh->id, rh->authority_name, rh->timeout.rel_value);
2134 rh->status |= RSL_TIMED_OUT;
2136 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2138 if (NULL != rh->get_handle)
2139 GNUNET_DHT_get_stop (rh->get_handle);
2141 rh->get_handle = NULL;
2143 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
2145 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2146 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
2148 rh->proc(rh->proc_cls, rh, 0, NULL);
2152 if (strcmp(rh->name, "") == 0)
2155 * promote authority back to name and try to resolve record
2157 strcpy(rh->name, rh->authority_name);
2158 rh->proc(rh->proc_cls, rh, 0, NULL);
2163 * Start resolution in bg
2165 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
2166 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
2167 //strcpy(new_name, rh->name);
2168 //strcpy(new_name+strlen(new_name), ".");
2169 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
2171 strcpy(rh->name, new_name);
2173 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2174 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2175 rh->id, rh->name, rlh->record_type);
2177 gns_resolver_lookup_record(rh->authority,
2178 rh->private_local_zone,
2182 GNUNET_TIME_UNIT_FOREVER_REL,
2184 &background_lookup_result_processor,
2187 rh->proc(rh->proc_cls, rh, 0, NULL);
2191 static void resolve_delegation_dht(struct ResolverHandle *rh);
2194 static void resolve_delegation_ns(struct ResolverHandle *rh);
2198 * Namestore resolution for delegation finished. Processing result.
2200 * @param cls the closure
2201 * @param rh resolver handle
2202 * @param rd_count number of results (always 0)
2203 * @param rd record data (always NULL)
2206 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2207 unsigned int rd_count,
2208 const struct GNUNET_NAMESTORE_RecordData *rd);
2212 * This is a callback function that checks for key revocation
2214 * @param cls the pending query
2215 * @param key the key of the zone we did the lookup
2216 * @param expiration expiration date of the record data set in the namestore
2217 * @param name the name for which we need an authority
2218 * @param rd_count the number of records with 'name'
2219 * @param rd the record data
2220 * @param signature the signature of the authority for the record data
2223 process_pkey_revocation_result_ns (void *cls,
2224 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2225 struct GNUNET_TIME_Absolute expiration,
2227 unsigned int rd_count,
2228 const struct GNUNET_NAMESTORE_RecordData *rd,
2229 const struct GNUNET_CRYPTO_RsaSignature *signature)
2231 struct ResolverHandle *rh = cls;
2232 struct GNUNET_TIME_Relative remaining_time;
2235 rh->namestore_task = NULL;
2236 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2238 for (i = 0; i < rd_count; i++)
2240 if (rd[i].record_type == GNUNET_GNS_RECORD_REV)
2242 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2243 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2245 rh->status |= RSL_PKEY_REVOKED;
2246 rh->proc (rh->proc_cls, rh, 0, NULL);
2251 if ((name == NULL) ||
2252 (remaining_time.rel_value == 0))
2254 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2255 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2258 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2261 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2262 rh->id, "+.gnunet", GNUNET_GNS_RECORD_REV);
2264 gns_resolver_lookup_record(rh->authority,
2265 rh->private_local_zone,
2266 GNUNET_GNS_RECORD_REV,
2269 GNUNET_TIME_UNIT_FOREVER_REL,
2271 &background_lookup_result_processor,
2275 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2276 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2279 * We are done with PKEY resolution if name is empty
2280 * else resolve again with new authority
2282 if (strcmp (rh->name, "") == 0)
2283 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2285 resolve_delegation_ns (rh);
2292 * Callback when record data is put into namestore
2294 * @param cls the closure
2295 * @param success GNUNET_OK on success
2296 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2299 on_namestore_delegation_put_result(void *cls,
2303 struct NamestoreBGTask *nbg = cls;
2305 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2308 if (GNUNET_NO == success)
2310 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2311 "GNS_NS: records already in namestore\n");
2314 else if (GNUNET_YES == success)
2316 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2317 "GNS_NS: records successfully put in namestore\n");
2321 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2322 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2326 * Function called when we get a result from the dht
2327 * for our query. Recursively tries to resolve authorities
2330 * @param cls the request handle
2331 * @param exp lifetime
2332 * @param key the key the record was stored under
2333 * @param get_path get path
2334 * @param get_path_length get path length
2335 * @param put_path put path
2336 * @param put_path_length put path length
2337 * @param type the block type
2338 * @param size the size of the record
2339 * @param data the record data
2342 process_delegation_result_dht(void* cls,
2343 struct GNUNET_TIME_Absolute exp,
2344 const struct GNUNET_HashCode * key,
2345 const struct GNUNET_PeerIdentity *get_path,
2346 unsigned int get_path_length,
2347 const struct GNUNET_PeerIdentity *put_path,
2348 unsigned int put_path_length,
2349 enum GNUNET_BLOCK_Type type,
2350 size_t size, const void *data)
2352 struct ResolverHandle *rh;
2353 struct GNSNameRecordBlock *nrb;
2354 uint32_t num_records;
2356 char* rd_data = (char*) data;
2359 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
2360 struct GNUNET_HashCode zone_hash_double, name_hash_double;
2362 rh = (struct ResolverHandle *)cls;
2364 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2365 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2370 nrb = (struct GNSNameRecordBlock*)data;
2372 /* stop dht lookup and timeout task */
2373 GNUNET_DHT_get_stop (rh->get_handle);
2375 rh->get_handle = NULL;
2377 if (rh->dht_heap_node != NULL)
2379 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2380 rh->dht_heap_node = NULL;
2383 num_records = ntohl(nrb->rd_count);
2384 name = (char*)&nrb[1];
2386 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2387 struct NamestoreBGTask *ns_heap_root;
2388 struct NamestoreBGTask *namestore_bg_task;
2390 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2391 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2393 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2398 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2399 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2404 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2405 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2406 rh->id, name, rh->authority_name);
2407 for (i=0; i<num_records; i++)
2410 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2411 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2412 rh->id, name, rh->authority_name);
2413 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2414 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2415 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2416 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2417 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2418 rh->id, rd[i].data_size);
2419 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2420 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2421 rh->id, rd[i].flags);
2423 if ((rd[i].record_type == GNUNET_GNS_RECORD_VPN) ||
2424 (rd[i].record_type == GNUNET_GNS_RECORD_NS) ||
2425 (rd[i].record_type == GNUNET_GNS_RECORD_CNAME))
2428 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2430 if (strcmp(rh->name, "") == 0)
2431 strcpy(rh->name, rh->authority_name);
2433 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2434 rh->name, rh->authority_name); //FIXME ret
2439 if ((strcmp(name, rh->authority_name) == 0) &&
2440 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
2442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2443 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2446 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2447 struct AuthorityChain *auth =
2448 GNUNET_malloc(sizeof(struct AuthorityChain));
2449 auth->zone = rh->authority;
2450 memset(auth->name, 0, strlen(rh->authority_name)+1);
2451 strcpy(auth->name, rh->authority_name);
2452 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2453 rh->authority_chain_tail,
2456 if (NULL != rh->rd.data)
2457 GNUNET_free ((void*)rh->rd.data);
2459 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2460 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2461 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2464 /** try to import pkey if private key available */
2465 //if (rh->priv_key && is_canonical (rh->name))
2466 // process_discovered_authority(name, auth->zone,
2467 // rh->authority_chain_tail->zone,
2474 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2475 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2476 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2477 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2479 /* Save to namestore */
2480 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
2483 if (max_allowed_ns_tasks <=
2484 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2486 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2487 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2489 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2490 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
2494 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2496 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2498 GNUNET_TIME_absolute_get().abs_value);
2499 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2506 &on_namestore_delegation_put_result, //cont
2507 namestore_bg_task); //cls
2516 * FIXME in this case. should we ask namestore again?
2518 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2519 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2520 rh->id, rh->authority_name, rh->name);
2522 if (strcmp(rh->name, "") == 0)
2524 /* Start shortening */
2525 if ((rh->priv_key != NULL) &&
2526 (is_canonical (rh->name) == GNUNET_YES))
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2529 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2531 start_shorten (rh->authority_chain_tail,
2536 rh->proc = &handle_delegation_ns;
2539 /* Check for key revocation and delegate */
2540 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2543 GNUNET_GNS_RECORD_REV,
2544 &process_pkey_revocation_result_ns,
2551 * No pkey but name exists
2554 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2555 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2556 rh->id, rh->authority_name, rh->name);
2557 if (strcmp(rh->name, "") == 0)
2558 strcpy(rh->name, rh->authority_name);
2560 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2561 rh->name, rh->authority_name); //FIXME ret
2563 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2564 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2565 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2566 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2568 rh->proc(rh->proc_cls, rh, 0, NULL);
2571 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2572 +(MAX_DNS_NAME_LENGTH*2)
2573 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2574 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2578 expand_plus(char* dest, char* src, char* repl)
2581 unsigned int s_len = strlen(src)+1;
2583 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2584 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2588 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2589 "GNS_POSTPROCESS: %s to short\n", src);
2591 /* no postprocessing */
2592 memcpy(dest, src, s_len+1);
2596 if (0 == strcmp(src+s_len-3, ".+"))
2598 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2599 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2600 memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2604 pos += strlen(repl);
2605 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2606 "GNS_POSTPROCESS: Expanded to %s\n", dest);
2610 memcpy(dest, src, s_len+1);
2618 finish_lookup (struct ResolverHandle *rh,
2619 struct RecordLookupHandle* rlh,
2620 unsigned int rd_count,
2621 const struct GNUNET_NAMESTORE_RecordData *rd)
2624 char new_rr_data[MAX_DNS_NAME_LENGTH];
2625 char new_mx_data[MAX_MX_LENGTH];
2626 char new_soa_data[MAX_SOA_LENGTH];
2627 char new_srv_data[MAX_SRV_LENGTH];
2628 struct srv_data *old_srv;
2629 struct srv_data *new_srv;
2630 struct soa_data *old_soa;
2631 struct soa_data *new_soa;
2632 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2635 unsigned int offset;
2637 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2639 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2640 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2643 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2646 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2648 for (i = 0; i < rd_count; i++)
2651 if (rd[i].record_type != GNUNET_GNS_RECORD_NS &&
2652 rd[i].record_type != GNUNET_GNS_RECORD_PTR &&
2653 rd[i].record_type != GNUNET_GNS_RECORD_CNAME &&
2654 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
2655 rd[i].record_type != GNUNET_GNS_RECORD_SOA &&
2656 rd[i].record_type != GNUNET_GNS_RECORD_SRV)
2658 p_rd[i].data = rd[i].data;
2663 * for all those records we 'should'
2664 * also try to resolve the A/AAAA records (RFC1035)
2665 * This is a feature and not important
2668 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2669 "GNS_POSTPROCESS: Postprocessing\n");
2671 if (strcmp(rh->name, "+") == 0)
2672 repl_string = rlh->name;
2674 repl_string = rlh->name+strlen(rh->name)+1;
2677 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
2679 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2680 offset = sizeof(uint16_t);
2681 pos = new_mx_data+offset;
2682 expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2684 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2685 p_rd[i].data = new_mx_data;
2686 p_rd[i].data_size = offset;
2688 else if (rd[i].record_type == GNUNET_GNS_RECORD_SRV)
2691 * Prio, weight and port
2693 new_srv = (struct srv_data*)new_srv_data;
2694 old_srv = (struct srv_data*)rd[i].data;
2695 new_srv->prio = old_srv->prio;
2696 new_srv->weight = old_srv->weight;
2697 new_srv->port = old_srv->port;
2698 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2700 p_rd[i].data = new_srv_data;
2701 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2703 else if (rd[i].record_type == GNUNET_GNS_RECORD_SOA)
2705 /* expand mname and rname */
2706 old_soa = (struct soa_data*)rd[i].data;
2707 new_soa = (struct soa_data*)new_soa_data;
2708 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2709 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2710 offset = strlen ((char*)&new_soa[1]) + 1;
2711 expand_plus((char*)&new_soa[1] + offset,
2712 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2714 p_rd[i].data_size = sizeof (struct soa_data)
2716 + strlen ((char*)&new_soa[1] + offset);
2717 p_rd[i].data = new_soa_data;
2722 expand_plus(pos, (char*)rd[i].data, repl_string);
2723 p_rd[i].data_size = strlen(new_rr_data)+1;
2724 p_rd[i].data = new_rr_data;
2729 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2731 free_resolver_handle (rh);
2735 * Process DHT lookup result for record.
2737 * @param cls the closure
2738 * @param rh resolver handle
2739 * @param rd_count number of results
2740 * @param rd record data
2743 handle_record_dht(void* cls, struct ResolverHandle *rh,
2744 unsigned int rd_count,
2745 const struct GNUNET_NAMESTORE_RecordData *rd)
2747 struct RecordLookupHandle* rlh;
2749 rlh = (struct RecordLookupHandle*)cls;
2752 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2753 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2755 /* give up, cannot resolve */
2756 finish_lookup(rh, rlh, 0, NULL);
2760 /* results found yay */
2761 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2762 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2764 finish_lookup(rh, rlh, rd_count, rd);
2772 * Process namestore lookup result for record.
2774 * @param cls the closure
2775 * @param rh resolver handle
2776 * @param rd_count number of results
2777 * @param rd record data
2780 handle_record_ns (void* cls, struct ResolverHandle *rh,
2781 unsigned int rd_count,
2782 const struct GNUNET_NAMESTORE_RecordData *rd)
2784 struct RecordLookupHandle* rlh;
2785 rlh = (struct RecordLookupHandle*) cls;
2786 int check_dht = GNUNET_YES;
2790 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2791 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2796 * There are 5 conditions that have to met for us to consult the DHT:
2797 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2798 * 2. No entry in the NS existed AND
2799 * 3. The zone queried is not the local resolver's zone AND
2800 * 4. The name that was looked up is '+'
2801 * because if it was any other canonical name we either already queried
2802 * the DHT for the authority in the authority lookup phase (and thus
2803 * would already have an entry in the NS for the record)
2804 * 5. We are not in cache only mode
2806 if (((rh->status & RSL_RECORD_EXPIRED) != 0) &&
2807 ((rh->status & RSL_RECORD_EXISTS) == 0) )
2808 check_dht = GNUNET_NO;
2810 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2811 &rh->private_local_zone))
2812 check_dht = GNUNET_NO;
2814 if ((strcmp (rh->name, "+") != 0) && (is_srv (rh->name) == GNUNET_YES))
2815 check_dht = GNUNET_NO;
2818 if (rh->only_cached == GNUNET_YES)
2819 check_dht = GNUNET_NO;
2821 if (GNUNET_YES == check_dht)
2823 rh->proc = &handle_record_dht;
2824 resolve_record_dht(rh);
2827 /* give up, cannot resolve */
2828 finish_lookup(rh, rlh, 0, NULL);
2832 /* results found yay */
2833 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2834 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2836 finish_lookup(rh, rlh, rd_count, rd);
2843 * Move one level up in the domain hierarchy and return the
2844 * passed top level domain.
2846 * @param name the domain
2847 * @param dest the destination where the tld will be put
2850 pop_tld(char* name, char* dest)
2854 if (is_canonical (name) == GNUNET_YES)
2861 for (len = strlen(name); len > 0; len--)
2863 if (*(name+len) == '.')
2873 strcpy(dest, (name+len+1));
2877 * Checks if name is in tld
2879 * @param name the name to check
2880 * @param tld the TLD to check for
2881 * @return GNUNET_YES or GNUNET_NO
2884 is_tld(const char* name, const char* tld)
2888 if (strlen(name) <= strlen(tld))
2893 offset = strlen(name)-strlen(tld);
2894 if (strcmp(name+offset, tld) != 0)
2896 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2897 "%s is not in .%s TLD\n", name, tld);
2904 * DHT resolution for delegation finished. Processing result.
2906 * @param cls the closure
2907 * @param rh resolver handle
2908 * @param rd_count number of results (always 0)
2909 * @param rd record data (always NULL)
2912 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2913 unsigned int rd_count,
2914 const struct GNUNET_NAMESTORE_RecordData *rd)
2916 struct RecordLookupHandle* rlh;
2917 rlh = (struct RecordLookupHandle*) cls;
2920 if (strcmp(rh->name, "") == 0)
2922 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
2924 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2925 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2927 finish_lookup(rh, rlh, rd_count, rd);
2930 /* We resolved full name for delegation. resolving record */
2931 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2932 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2934 strcpy(rh->name, "+\0");
2935 rh->proc = &handle_record_ns;
2936 resolve_record_ns(rh);
2941 * we still have some left
2943 if (is_canonical (rh->name) == GNUNET_YES)
2945 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2946 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2949 rh->proc = &handle_record_ns;
2950 resolve_record_ns(rh);
2953 /* give up, cannot resolve */
2954 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2955 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2957 finish_lookup(rh, rlh, 0, NULL);
2962 * Start DHT lookup for a name -> PKEY (compare NS) record in
2963 * rh->authority's zone
2965 * @param rh the pending gns query
2968 resolve_delegation_dht(struct ResolverHandle *rh)
2971 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2972 struct GNUNET_HashCode name_hash_double;
2973 struct GNUNET_HashCode zone_hash_double;
2974 struct GNUNET_HashCode lookup_key;
2975 struct ResolverHandle *rh_heap_root;
2977 pop_tld(rh->name, rh->authority_name);
2978 GNUNET_CRYPTO_short_hash(rh->authority_name,
2979 strlen(rh->authority_name),
2981 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2982 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2983 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2985 rh->dht_heap_node = NULL;
2987 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2989 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
2990 // &dht_authority_lookup_timeout,
2992 rh->timeout_cont = &dht_authority_lookup_timeout;
2993 rh->timeout_cont_cls = rh;
2997 if (max_allowed_background_queries <=
2998 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
3000 /* terminate oldest lookup */
3001 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
3002 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
3003 rh_heap_root->get_handle = NULL;
3004 rh_heap_root->dht_heap_node = NULL;
3006 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3007 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
3008 rh->id, rh_heap_root->authority_name);
3010 rh_heap_root->proc(rh_heap_root->proc_cls,
3015 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
3017 GNUNET_TIME_absolute_get().abs_value);
3020 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
3022 GNUNET_assert(rh->get_handle == NULL);
3023 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
3024 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
3026 DHT_GNS_REPLICATION_LEVEL,
3030 &process_delegation_result_dht,
3037 * Namestore resolution for delegation finished. Processing result.
3039 * @param cls the closure
3040 * @param rh resolver handle
3041 * @param rd_count number of results (always 0)
3042 * @param rd record data (always NULL)
3045 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
3046 unsigned int rd_count,
3047 const struct GNUNET_NAMESTORE_RecordData *rd)
3049 struct RecordLookupHandle* rlh;
3050 rlh = (struct RecordLookupHandle*) cls;
3051 int check_dht = GNUNET_YES;
3054 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3055 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
3056 rh->id, rh->status);
3058 if (rh->status & RSL_PKEY_REVOKED)
3060 finish_lookup (rh, rlh, 0, NULL);
3064 if (strcmp(rh->name, "") == 0)
3067 /* We resolved full name for delegation. resolving record */
3068 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3069 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
3071 if (rh->status & RSL_CNAME_FOUND)
3073 if (rlh->record_type == GNUNET_GNS_RECORD_CNAME)
3075 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3076 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
3078 strcpy (rh->name, rh->authority_name);
3079 finish_lookup (rh, rlh, rd_count, rd);
3084 if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
3086 s_len = strlen (rd->data) - 2;
3087 memcpy (rh->name, rd->data, s_len);
3088 rh->name[s_len] = '\0';
3089 resolve_delegation_ns (rh);
3092 else if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
3094 gns_resolver_lookup_record (rh->authority,
3095 rh->private_local_zone,
3104 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3105 free_resolver_handle (rh);
3111 strcpy (rh->dns_name, (char*)rd->data);
3112 resolve_dns_name (rh);
3117 else if (rh->status & RSL_DELEGATE_VPN)
3119 if (rlh->record_type == GNUNET_GNS_RECORD_VPN)
3121 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3122 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
3124 finish_lookup(rh, rlh, rd_count, rd);
3127 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3128 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
3130 GNUNET_assert (NULL != rd);
3131 rh->proc = &handle_record_vpn;
3132 resolve_record_vpn (rh, rd_count, rd);
3135 else if (rh->status & RSL_DELEGATE_NS)
3137 if (rlh->record_type == GNUNET_GNS_RECORD_NS)
3139 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3140 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3142 finish_lookup(rh, rlh, rd_count, rd);
3146 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3147 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3149 GNUNET_assert (NULL != rd);
3150 rh->proc = &handle_record_ns;
3151 resolve_record_dns (rh, rd_count, rd);
3154 else if (rh->status & RSL_DELEGATE_PKEY)
3156 if (rh->status & RSL_PKEY_REVOKED)
3158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3159 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
3161 finish_lookup (rh, rlh, 0, NULL);
3164 else if (rlh->record_type == GNUNET_GNS_RECORD_PKEY)
3166 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3167 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
3169 finish_lookup(rh, rlh, rd_count, rd);
3173 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3174 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3176 strcpy(rh->name, "+\0");
3177 rh->proc = &handle_record_ns;
3178 resolve_record_ns(rh);
3182 if (rh->status & RSL_DELEGATE_NS)
3184 if (rlh->record_type == GNUNET_GNS_RECORD_NS)
3186 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3187 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3189 finish_lookup(rh, rlh, rd_count, rd);
3193 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3194 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3196 GNUNET_assert (NULL != rd);
3197 rh->proc = &handle_record_ns;
3198 resolve_record_dns (rh, rd_count, rd);
3203 * we still have some left
3204 * check if authority in ns is fresh
3206 * or we are authority
3209 if ((rh->status & RSL_RECORD_EXISTS) &&
3210 !(rh->status & RSL_RECORD_EXPIRED))
3211 check_dht = GNUNET_NO;
3213 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3214 &rh->private_local_zone))
3215 check_dht = GNUNET_NO;
3217 if (rh->only_cached == GNUNET_YES)
3218 check_dht = GNUNET_NO;
3220 if (check_dht == GNUNET_NO)
3222 if (is_canonical (rh->name) == GNUNET_YES)
3224 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3225 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3228 rh->proc = &handle_record_ns;
3229 resolve_record_ns(rh);
3233 /* give up, cannot resolve */
3234 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3235 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3238 finish_lookup(rh, rlh, rd_count, rd);
3239 //rlh->proc(rlh->proc_cls, 0, NULL);
3244 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3245 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3247 rh->proc = &handle_delegation_dht;
3248 resolve_delegation_dht(rh);
3253 * This is a callback function that should give us only PKEY
3254 * records. Used to query the namestore for the authority (PKEY)
3255 * for 'name'. It will recursively try to resolve the
3256 * authority for a given name from the namestore.
3258 * @param cls the pending query
3259 * @param key the key of the zone we did the lookup
3260 * @param expiration expiration date of the record data set in the namestore
3261 * @param name the name for which we need an authority
3262 * @param rd_count the number of records with 'name'
3263 * @param rd the record data
3264 * @param signature the signature of the authority for the record data
3267 process_delegation_result_ns (void* cls,
3268 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3269 struct GNUNET_TIME_Absolute expiration,
3271 unsigned int rd_count,
3272 const struct GNUNET_NAMESTORE_RecordData *rd,
3273 const struct GNUNET_CRYPTO_RsaSignature *signature)
3275 struct ResolverHandle *rh;
3276 struct GNUNET_TIME_Relative remaining_time;
3277 struct GNUNET_CRYPTO_ShortHashCode zone;
3278 char new_name[MAX_DNS_NAME_LENGTH];
3280 struct GNUNET_TIME_Absolute et;
3282 rh = (struct ResolverHandle *)cls;
3283 rh->namestore_task = NULL;
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3285 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3288 GNUNET_CRYPTO_short_hash (key,
3289 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3291 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3298 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3300 rh->status |= RSL_RECORD_EXISTS;
3302 if (remaining_time.rel_value == 0)
3304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3305 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3307 rh->status |= RSL_RECORD_EXPIRED;
3312 * No authority found in namestore.
3317 * We did not find an authority in the namestore
3322 * Promote this authority back to a name maybe it is
3325 if (strcmp (rh->name, "") == 0)
3327 /* simply promote back */
3328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3329 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3330 rh->id, rh->authority_name);
3331 strcpy (rh->name, rh->authority_name);
3335 /* add back to existing name */
3336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3337 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3338 rh->id, rh->authority_name, rh->name);
3339 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3340 rh->name, rh->authority_name);
3341 strcpy (rh->name, new_name);
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3343 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3347 rh->proc (rh->proc_cls, rh, 0, NULL);
3352 * We found an authority that may be able to help us
3353 * move on with query
3354 * Note only 1 pkey should have been returned.. anything else would be strange
3356 for (i=0; i < rd_count;i++)
3360 * A CNAME. Like regular DNS this means the is no other record for this
3363 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3366 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3369 rh->status |= RSL_CNAME_FOUND;
3370 rh->proc (rh->proc_cls, rh, rd_count, rd);
3377 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3379 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3380 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3382 rh->status |= RSL_DELEGATE_VPN;
3383 rh->proc (rh->proc_cls, rh, rd_count, rd);
3389 * FIXME make optional
3391 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3393 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3394 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3396 rh->status |= RSL_DELEGATE_NS;
3397 rh->proc (rh->proc_cls, rh, rd_count, rd);
3401 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3404 rh->status |= RSL_DELEGATE_PKEY;
3406 if ((ignore_pending_records != 0) &&
3407 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3410 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3416 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3417 et.abs_value = rd[i].expiration_time;
3418 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3422 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3424 if (remaining_time.rel_value == 0)
3426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3427 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3429 rh->authority_chain_head->fresh = 0;
3430 rh->proc (rh->proc_cls, rh, 0, NULL);
3438 * Resolve rest of query with new authority
3440 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3441 memcpy (&rh->authority, rd[i].data,
3442 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3443 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3444 auth->zone = rh->authority;
3445 memset (auth->name, 0, strlen (rh->authority_name)+1);
3446 strcpy (auth->name, rh->authority_name);
3447 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3448 rh->authority_chain_tail,
3450 if (NULL != rh->rd.data)
3451 GNUNET_free ((void*)(rh->rd.data));
3453 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3454 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3455 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3457 /* Check for key revocation and delegate */
3458 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3461 GNUNET_GNS_RECORD_REV,
3462 &process_pkey_revocation_result_ns,
3471 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3472 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3474 * If we have found some records for the LAST label
3475 * we return the results. Else null.
3477 if (strcmp (rh->name, "") == 0)
3479 /* Start shortening */
3480 if ((rh->priv_key != NULL) &&
3481 (is_canonical (rh->name) == GNUNET_YES))
3483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3484 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3486 start_shorten (rh->authority_chain_tail,
3489 /* simply promote back */
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3491 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3492 rh->id, rh->authority_name);
3493 strcpy (rh->name, rh->authority_name);
3494 rh->proc (rh->proc_cls, rh, rd_count, rd);
3498 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
3499 "%s.%s", rh->name, rh->authority_name);
3500 strcpy (rh->name, new_name);
3501 rh->proc (rh->proc_cls, rh, 0, NULL);
3507 * Resolve the delegation chain for the request in our namestore
3509 * @param rh the resolver handle
3512 resolve_delegation_ns (struct ResolverHandle *rh)
3514 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3515 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3517 pop_tld(rh->name, rh->authority_name);
3518 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3521 GNUNET_GNS_RECORD_ANY,
3522 &process_delegation_result_ns,
3529 * Lookup of a record in a specific zone
3530 * calls lookup result processor on result
3532 * @param zone the root zone
3533 * @param pzone the private local zone
3534 * @param record_type the record type to look up
3535 * @param name the name to look up
3536 * @param key a private key for use with PSEU import (can be NULL)
3537 * @param timeout timeout for resolution
3538 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3539 * @param proc the processor to call on result
3540 * @param cls the closure to pass to proc
3543 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3544 struct GNUNET_CRYPTO_ShortHashCode pzone,
3545 uint32_t record_type,
3547 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3548 struct GNUNET_TIME_Relative timeout,
3550 RecordLookupProcessor proc,
3553 struct ResolverHandle *rh;
3554 struct RecordLookupHandle* rlh;
3555 char string_hash[MAX_DNS_LABEL_LENGTH];
3556 char nzkey[MAX_DNS_LABEL_LENGTH];
3557 char* nzkey_ptr = nzkey;
3559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3560 "Starting resolution for %s (type=%d)!\n",
3564 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3565 (strcmp(GNUNET_GNS_TLD, name) != 0))
3567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3568 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3573 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3574 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3576 memset (rh, 0, sizeof (struct ResolverHandle));
3577 rh->authority = zone;
3581 rh->timeout = timeout;
3582 rh->get_handle = NULL;
3583 rh->private_local_zone = pzone;
3584 rh->only_cached = only_cached;
3585 rh->namestore_task = NULL;
3588 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3593 "No shorten key for resolution\n");
3596 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3599 * Set timeout for authority lookup phase to 1/2
3601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3602 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3603 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3604 GNUNET_TIME_relative_divide(timeout, 2),
3605 &handle_lookup_timeout,
3607 rh->timeout_cont = &dht_authority_lookup_timeout;
3608 rh->timeout_cont_cls = rh;
3612 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3613 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3616 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3619 * Only 'gnunet' given
3621 strcpy(rh->name, "\0");
3625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3626 "Checking for TLD...\n");
3627 if (is_zkey_tld(name) == GNUNET_YES)
3629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3632 * This is a zkey tld
3633 * build hash and use as initial authority
3636 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3637 memcpy(rh->name, name,
3638 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3639 pop_tld(rh->name, string_hash);
3641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3642 "ZKEY is %s!\n", string_hash);
3644 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3646 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3649 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3650 "Cannot convert ZKEY %s to hash!\n", string_hash);
3652 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3655 proc (cls, 0, NULL);
3660 else if (is_gnunet_tld (name) == GNUNET_YES)
3662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3665 * Presumably GNUNET tld
3667 memset (rh->name, 0,
3668 strlen(name)-strlen(GNUNET_GNS_TLD));
3669 memcpy (rh->name, name,
3670 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3674 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3675 "Cannot handle this TLD %s\n", string_hash);
3677 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3678 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3679 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3682 proc (cls, 0, NULL);
3688 * Initialize authority chain
3690 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3691 rh->authority_chain_head->prev = NULL;
3692 rh->authority_chain_head->next = NULL;
3693 rh->authority_chain_tail = rh->authority_chain_head;
3694 rh->authority_chain_head->zone = rh->authority;
3695 strcpy (rh->authority_chain_head->name, "");
3698 * Copy original query into lookup handle
3700 rlh->record_type = record_type;
3701 memset(rlh->name, 0, strlen(name) + 1);
3702 strcpy(rlh->name, name);
3704 rlh->proc_cls = cls;
3706 rh->proc = &handle_delegation_ns;
3707 resolve_delegation_ns(rh);
3710 /******** END Record Resolver ***********/
3713 finish_shorten (struct ResolverHandle *rh,
3714 struct NameShortenHandle *nsh)
3716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3717 "Sending %s as shorten result\n", nsh->result);
3718 nsh->proc (nsh->proc_cls, nsh->result);
3719 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3721 free_resolver_handle (rh);
3726 * Callback calles by namestore for a zone to name
3729 * @param cls the closure
3730 * @param zone_key the zone we queried
3731 * @param expire the expiration time of the name
3732 * @param name the name found or NULL
3733 * @param rd_len number of records for the name
3734 * @param rd the record data (PKEY) for the name
3735 * @param signature the signature for the record data
3738 process_zone_to_name_shorten_root (void *cls,
3739 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3740 struct GNUNET_TIME_Absolute expire,
3742 unsigned int rd_len,
3743 const struct GNUNET_NAMESTORE_RecordData *rd,
3744 const struct GNUNET_CRYPTO_RsaSignature *signature);
3748 * Callback called by namestore for a zone to name
3751 * @param cls the closure
3752 * @param zone_key the zone we queried
3753 * @param expire the expiration time of the name
3754 * @param name the name found or NULL
3755 * @param rd_len number of records for the name
3756 * @param rd the record data (PKEY) for the name
3757 * @param signature the signature for the record data
3760 process_zone_to_name_shorten_shorten (void *cls,
3761 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3762 struct GNUNET_TIME_Absolute expire,
3764 unsigned int rd_len,
3765 const struct GNUNET_NAMESTORE_RecordData *rd,
3766 const struct GNUNET_CRYPTO_RsaSignature *signature)
3768 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3769 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3770 struct AuthorityChain *next_authority;
3772 char result[MAX_DNS_NAME_LENGTH];
3773 char tmp_name[MAX_DNS_NAME_LENGTH];
3776 rh->namestore_task = NULL;
3777 /* we found a match in our own root zone */
3780 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3781 memset(result, 0, answer_len);
3783 if (strlen(rh->name) > 0)
3785 sprintf (result, "%s.%s.%s.%s",
3787 nsh->shorten_zone_name,
3792 sprintf (result, "%s.%s.%s", name,
3793 nsh->shorten_zone_name,
3797 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3798 "Found shorten result %s\n", result);
3799 if (strlen (nsh->result) > strlen (result))
3800 strcpy (nsh->result, result);
3802 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3803 nsh->shorten_zone) == 0)
3806 * This is our zone append .gnunet unless name is empty
3807 * (it shouldn't be, usually FIXME what happens if we
3808 * shorten to our zone to a "" record??)
3811 sprintf (result, "%s.%s.%s",
3813 nsh->shorten_zone_name,
3815 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3816 "Our zone: Found %s as shorten result\n", result);
3818 if (strlen (nsh->result) > strlen (result))
3819 strcpy (nsh->result, result);
3820 //nsh->proc(nsh->proc_cls, result);
3822 //free_resolver_handle(rh);
3829 * continue with next authority if exists
3831 if ((rh->authority_chain_head->next == NULL))
3833 finish_shorten (rh, nsh);
3836 next_authority = rh->authority_chain_head;
3838 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3839 "%s.%s", rh->name, next_authority->name);
3841 strcpy(rh->name, tmp_name);
3842 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3843 "No PSEU found for authority %s. Promoting back: %s\n",
3844 next_authority->name, rh->name);
3846 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3847 rh->authority_chain_tail,
3850 GNUNET_free (next_authority);
3852 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3853 &rh->authority_chain_tail->zone,
3854 &rh->authority_chain_head->zone,
3855 &process_zone_to_name_shorten_root,
3860 * Callback calles by namestore for a zone to name
3863 * @param cls the closure
3864 * @param zone_key the zone we queried
3865 * @param expire the expiration time of the name
3866 * @param name the name found or NULL
3867 * @param rd_len number of records for the name
3868 * @param rd the record data (PKEY) for the name
3869 * @param signature the signature for the record data
3872 process_zone_to_name_shorten_private (void *cls,
3873 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3874 struct GNUNET_TIME_Absolute expire,
3876 unsigned int rd_len,
3877 const struct GNUNET_NAMESTORE_RecordData *rd,
3878 const struct GNUNET_CRYPTO_RsaSignature *signature)
3880 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3881 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3882 struct AuthorityChain *next_authority;
3884 char result[MAX_DNS_NAME_LENGTH];
3885 char tmp_name[MAX_DNS_NAME_LENGTH];
3888 rh->namestore_task = NULL;
3889 /* we found a match in our own root zone */
3892 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3893 memset(result, 0, answer_len);
3895 if (strlen(rh->name) > 0)
3897 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3901 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3904 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3905 "Found shorten result %s\n", result);
3906 if (strlen (nsh->result) > strlen (result))
3907 strcpy (nsh->result, result);
3909 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3910 nsh->private_zone) == 0)
3913 * This is our zone append .gnunet unless name is empty
3914 * (it shouldn't be, usually FIXME what happens if we
3915 * shorten to our zone to a "" record??)
3918 sprintf (result, "%s.%s.%s",
3919 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3920 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3921 "Our private zone: Found %s as shorten result %s\n", result);
3922 if (strlen (nsh->result) > strlen (result))
3923 strcpy (nsh->result, result);
3926 if (0 != strcmp (nsh->shorten_zone_name, ""))
3928 /* backtrack authorities for names in priv zone */
3929 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3931 &rh->authority_chain_head->zone,
3932 &process_zone_to_name_shorten_shorten,
3939 * continue with next authority if exists
3941 if ((rh->authority_chain_head->next == NULL))
3943 finish_shorten (rh, nsh);
3946 next_authority = rh->authority_chain_head;
3948 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3949 "%s.%s", rh->name, next_authority->name);
3951 strcpy(rh->name, tmp_name);
3952 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3953 "No PSEU found for authority %s. Promoting back: %s\n",
3954 next_authority->name, rh->name);
3956 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3957 rh->authority_chain_tail,
3960 GNUNET_free (next_authority);
3962 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3963 &rh->authority_chain_tail->zone,
3964 &rh->authority_chain_head->zone,
3965 &process_zone_to_name_shorten_root,
3971 * Callback calles by namestore for a zone to name
3974 * @param cls the closure
3975 * @param zone_key the zone we queried
3976 * @param expire the expiration time of the name
3977 * @param name the name found or NULL
3978 * @param rd_len number of records for the name
3979 * @param rd the record data (PKEY) for the name
3980 * @param signature the signature for the record data
3983 process_zone_to_name_shorten_root (void *cls,
3984 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3985 struct GNUNET_TIME_Absolute expire,
3987 unsigned int rd_len,
3988 const struct GNUNET_NAMESTORE_RecordData *rd,
3989 const struct GNUNET_CRYPTO_RsaSignature *signature)
3991 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3992 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3993 struct AuthorityChain *next_authority;
3995 char result[MAX_DNS_NAME_LENGTH];
3996 char tmp_name[MAX_DNS_NAME_LENGTH];
3999 rh->namestore_task = NULL;
4000 /* we found a match in our own root zone */
4003 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
4004 memset(result, 0, answer_len);
4006 if (strlen(rh->name) > 0)
4008 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
4012 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
4015 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4016 "Found shorten result %s\n", result);
4017 if (strlen (nsh->result) > strlen (result))
4018 strcpy (nsh->result, result);
4020 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4021 nsh->root_zone) == 0)
4024 * This is our zone append .gnunet unless name is empty
4025 * (it shouldn't be, usually FIXME what happens if we
4026 * shorten to our zone to a "" record??)
4029 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
4030 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4031 "Our zone: Found %s as shorten result\n", result);
4032 if (strlen (nsh->result) > strlen (result))
4033 strcpy (nsh->result, result);
4036 if (NULL != nsh->private_zone)
4038 /* backtrack authorities for names in priv zone */
4039 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4041 &rh->authority_chain_head->zone,
4042 &process_zone_to_name_shorten_private,
4045 else if (NULL != nsh->shorten_zone)
4047 /* backtrack authorities for names in shorten zone */
4048 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4050 &rh->authority_chain_head->zone,
4051 &process_zone_to_name_shorten_shorten,
4058 * continue with next authority if exists
4060 if ((rh->authority_chain_head->next == NULL))
4062 finish_shorten (rh, nsh);
4065 next_authority = rh->authority_chain_head;
4067 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
4068 "%s.%s", rh->name, next_authority->name);
4070 strcpy(rh->name, tmp_name);
4071 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4072 "No PSEU found for authority %s. Promoting back: %s\n",
4073 next_authority->name, rh->name);
4075 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
4076 rh->authority_chain_tail,
4079 GNUNET_free (next_authority);
4081 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4082 &rh->authority_chain_tail->zone,
4083 &rh->authority_chain_head->zone,
4084 &process_zone_to_name_shorten_root,
4091 * Process result from namestore delegation lookup
4092 * for shorten operation
4094 * @param cls the client shorten handle
4095 * @param rh the resolver handle
4096 * @param rd_count number of results (0)
4097 * @param rd data (NULL)
4100 handle_delegation_ns_shorten (void* cls,
4101 struct ResolverHandle *rh,
4103 const struct GNUNET_NAMESTORE_RecordData *rd)
4105 struct NameShortenHandle *nsh;
4106 char result[MAX_DNS_NAME_LENGTH];
4108 nsh = (struct NameShortenHandle *)cls;
4111 * At this point rh->name contains the part of the name
4112 * that we do not have a PKEY in our namestore to resolve.
4113 * The authority chain in the resolver handle is now
4114 * useful to backtrack if needed
4117 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4118 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4119 memset(result, 0, sizeof (result));
4121 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4122 nsh->root_zone) == 0)
4125 * This is our zone append .gnunet unless name is empty
4126 * (it shouldn't be, usually FIXME what happens if we
4127 * shorten to our zone to a "" record??)
4130 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
4131 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4132 "Our zone: Found %s as shorten result\n", result);
4134 if (strlen (nsh->result) > strlen (result))
4135 strcpy (nsh->result, result);
4138 else if (NULL != nsh->private_zone)
4141 * This is our zone append .gnunet unless name is empty
4142 * (it shouldn't be, usually FIXME what happens if we
4143 * shorten to our zone to a "" record??)
4145 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4146 nsh->private_zone) == 0)
4149 sprintf (result, "%s.%s.%s",
4150 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4151 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4152 "Our zone: Found %s as shorten result in private zone %s\n",
4155 if (strlen (nsh->result) > strlen (result))
4156 strcpy (nsh->result, result);
4159 else if (NULL != nsh->shorten_zone)
4162 * This is our zone append .gnunet unless name is empty
4163 * (it shouldn't be, usually FIXME what happens if we
4164 * shorten to our zone to a "" record??)
4166 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4167 nsh->shorten_zone) == 0)
4169 sprintf (result, "%s.%s.%s",
4170 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4171 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4172 "Our zone: Found %s as shorten result in shorten zone\n",
4175 if (strlen (nsh->result) > strlen (result))
4176 strcpy (nsh->result, result);
4181 /* backtrack authorities for names */
4182 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4184 &rh->authority_chain_head->zone,
4185 &process_zone_to_name_shorten_root,
4192 * Callback calles by namestore for a zone to name
4195 * @param cls the closure
4196 * @param zone_key the zone we queried
4197 * @param expire the expiration time of the name
4198 * @param name the name found or NULL
4199 * @param rd_len number of records for the name
4200 * @param rd the record data (PKEY) for the name
4201 * @param signature the signature for the record data
4204 process_zone_to_name_zkey(void *cls,
4205 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4206 struct GNUNET_TIME_Absolute expire,
4208 unsigned int rd_len,
4209 const struct GNUNET_NAMESTORE_RecordData *rd,
4210 const struct GNUNET_CRYPTO_RsaSignature *signature)
4212 struct ResolverHandle *rh = cls;
4213 struct NameShortenHandle *nsh = rh->proc_cls;
4214 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
4215 char new_name[MAX_DNS_NAME_LENGTH];
4217 /* zkey not in our zone */
4221 * In this case we have not given this PKEY a name (yet)
4222 * It is either just not in our zone or not even cached
4223 * Since we do not know at this point we will not try to shorten
4224 * because PKEY import will happen if the user follows the zkey
4227 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
4229 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4230 "No name found for zkey %s returning verbatim!\n", enc);
4231 if (strcmp(rh->name, "") != 0)
4232 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4233 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4235 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4236 enc, GNUNET_GNS_TLD_ZKEY);
4238 strcpy (nsh->result, new_name);
4240 finish_shorten (rh, nsh);
4244 if (strcmp(rh->name, "") != 0)
4245 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4248 strcpy(new_name, name);
4250 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4251 "Continue shorten for %s!\n", new_name);
4253 strcpy(rh->name, new_name);
4255 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4256 rh->authority_chain_tail = rh->authority_chain_head;
4257 rh->authority_chain_head->zone = rh->authority;
4260 /* Start delegation resolution in our namestore */
4261 resolve_delegation_ns (rh);
4266 * Shorten api from resolver
4268 * @param zone the root zone to use
4269 * @param pzone the private zone to use
4270 * @param szone the shorten zone to use
4271 * @param name the name to shorten
4272 * @param private_zone_name name of the private zone
4273 * @param shorten_zone_name name of the shorten zone
4274 * @param proc the processor to call with result
4275 * @param proc_cls closure to pass to proc
4278 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4279 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4280 struct GNUNET_CRYPTO_ShortHashCode *szone,
4282 const char* private_zone_name,
4283 const char* shorten_zone_name,
4284 ShortenResultProcessor proc,
4287 struct ResolverHandle *rh;
4288 struct NameShortenHandle *nsh;
4289 char string_hash[MAX_DNS_LABEL_LENGTH];
4290 struct GNUNET_CRYPTO_ShortHashCode zkey;
4291 char nzkey[MAX_DNS_LABEL_LENGTH];
4292 char* nzkey_ptr = nzkey;
4295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4296 "Starting shorten for %s!\n", name);
4298 if (is_canonical ((char*)name) == GNUNET_YES)
4300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4301 "%s is canonical. Returning verbatim\n", name);
4302 proc (proc_cls, name);
4306 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4309 nsh->proc_cls = proc_cls;
4310 nsh->root_zone = zone;
4311 nsh->private_zone = pzone;
4312 nsh->shorten_zone = szone;
4313 strcpy (nsh->private_zone_name, private_zone_name);
4314 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4315 strcpy (nsh->result, name);
4318 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4319 rh->authority = *zone;
4321 rh->priv_key = NULL;
4322 rh->namestore_task = NULL;
4323 rh->proc = &handle_delegation_ns_shorten;
4326 rh->private_local_zone = *zone;
4328 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4331 "Checking for TLD...\n");
4332 if (is_zkey_tld (name) == GNUNET_YES)
4334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4337 * This is a zkey tld
4338 * build hash and use as initial authority
4341 memset (rh->name, 0,
4342 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4343 memcpy (rh->name, name,
4344 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4345 pop_tld (rh->name, string_hash);
4347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4348 "ZKEY is %s!\n", string_hash);
4350 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4352 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4356 "Cannot convert ZKEY %s to hash!\n", nzkey);
4359 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4360 proc (proc_cls, name);
4364 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4367 &process_zone_to_name_zkey,
4372 else if (is_gnunet_tld (name) == GNUNET_YES)
4374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4377 * Presumably GNUNET tld
4379 memset (rh->name, 0,
4380 strlen (name)-strlen (GNUNET_GNS_TLD));
4381 memcpy (rh->name, name,
4382 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4386 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4389 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4390 proc (proc_cls, name);
4394 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4395 rh->authority_chain_tail = rh->authority_chain_head;
4396 rh->authority_chain_head->zone = *zone;
4399 /* Start delegation resolution in our namestore */
4400 resolve_delegation_ns (rh);
4403 /*********** END NAME SHORTEN ********************/
4406 * Conclude get authority lookup
4408 * @param rh resolver handle
4409 * @param nah get authority lookup handle
4412 finish_get_auth (struct ResolverHandle *rh,
4413 struct GetNameAuthorityHandle *nah)
4415 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4416 "Got authority result %s\n", nah->result);
4418 nah->proc(nah->proc_cls, nah->result);
4419 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4421 free_resolver_handle(rh);
4425 * Process result from namestore delegation lookup
4426 * for get authority operation
4428 * @param cls the client get auth handle
4429 * @param rh the resolver handle
4430 * @param rd_count number of results (0)
4431 * @param rd data (NULL)
4434 handle_delegation_result_ns_get_auth(void* cls,
4435 struct ResolverHandle *rh,
4437 const struct GNUNET_NAMESTORE_RecordData *rd)
4439 struct GetNameAuthorityHandle* nah;
4442 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4445 * At this point rh->name contains the part of the name
4446 * that we do not have a PKEY in our namestore to resolve.
4447 * The authority chain in the resolver handle is now
4448 * useful to backtrack if needed
4451 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4452 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4454 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4455 "Building response!\n");
4456 if (is_canonical (rh->name) == GNUNET_YES)
4459 * We successfully resolved the authority in the ns
4460 * FIXME for our purposes this is fine
4461 * but maybe we want to have an api that also looks
4462 * into the dht (i.e. option in message)
4464 if (strlen(rh->name) > strlen(nah->name))
4466 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4467 "Record name longer than original lookup name... odd!\n");
4471 answer_len = strlen(nah->name) - strlen(rh->name)
4472 + strlen(GNUNET_GNS_TLD) + 1;
4473 memset(nah->result, 0, answer_len);
4474 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4476 finish_get_auth (rh, nah);
4480 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4481 "Unable to resolve authority for remaining %s!\n", rh->name);
4482 strcpy(nah->result, "");
4483 finish_get_auth (rh, nah);
4491 * Tries to resolve the authority for name
4494 * @param zone the root zone to look up for
4495 * @param pzone the private local zone
4496 * @param name the name to lookup up
4497 * @param proc the processor to call when finished
4498 * @param proc_cls the closure to pass to the processor
4501 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4502 struct GNUNET_CRYPTO_ShortHashCode pzone,
4504 GetAuthorityResultProcessor proc,
4507 struct ResolverHandle *rh;
4508 struct GetNameAuthorityHandle *nah;
4510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4511 "Starting authority resolution for %s!\n", name);
4513 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4514 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4515 rh->authority = zone;
4517 rh->private_local_zone = pzone;
4518 rh->namestore_task = NULL;
4520 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4522 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4524 strcpy(rh->name, "\0");
4529 strlen(name)-strlen(GNUNET_GNS_TLD));
4530 memcpy(rh->name, name,
4531 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4534 memset(nah->name, 0,
4536 strcpy(nah->name, name);
4538 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4539 rh->authority_chain_tail = rh->authority_chain_head;
4540 rh->authority_chain_head->zone = zone;
4541 rh->proc = &handle_delegation_result_ns_get_auth;
4542 rh->proc_cls = (void*)nah;
4545 nah->proc_cls = proc_cls;
4546 strcpy (nah->result, "");
4548 /* Start delegation resolution in our namestore */
4549 resolve_delegation_ns(rh);
4553 /******** END GET AUTHORITY *************/
4555 /* end of gnunet-service-gns_resolver.c */