2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
24 * @file gns/gnunet-service-gns_resolver.c
25 * @brief GNUnet GNS resolver logic
26 * @author Martin Schanzenbach
29 #include "gnunet_util_lib.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_dns_service.h"
32 #include "gnunet_dht_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_vpn_service.h"
35 #include "gnunet_dns_service.h"
36 #include "gnunet_resolver_service.h"
37 #include "gnunet_dnsparser_lib.h"
38 #include "../dns/dnsparser.h"
39 #include "gnunet_gns_service.h"
40 #include "block_gns.h"
42 #include "gnunet-service-gns_resolver.h"
44 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
45 #define DHT_GNS_REPLICATION_LEVEL 5
46 #define MAX_DNS_LABEL_LENGTH 63
50 * Our handle to the namestore service
52 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
55 * Our handle to the vpn service
57 static struct GNUNET_VPN_Handle *vpn_handle;
60 * Resolver handle to the dht
62 static struct GNUNET_DHT_Handle *dht_handle;
65 * Heap for parallel DHT lookups
67 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
70 * Heap for namestore queues
72 static struct GNUNET_CONTAINER_Heap *ns_task_heap;
75 * Maximum amount of parallel queries in background
77 static unsigned long long max_allowed_background_queries;
80 * Maximum amount of parallel namestore tasks in background
82 static unsigned long long max_allowed_ns_tasks;
85 * Wheather or not to ignore pending records
87 static int ignore_pending_records;
92 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
95 * Background shortening handles
97 static struct GetPseuAuthorityHandle *gph_head;
100 * Background shortening handles
102 static struct GetPseuAuthorityHandle *gph_tail;
105 * Resolver lookup list
107 static struct ResolverHandle *rlh_head;
110 * Resolver lookup list
112 static struct ResolverHandle *rlh_tail;
115 * Resolver shorten list
117 static struct ResolverHandle *nsh_head;
120 * Resolver shorten list
122 static struct ResolverHandle *nsh_tail;
125 * Resolver get auth list
127 static struct ResolverHandle *nah_head;
130 * Resolver get auth list
132 static struct ResolverHandle *nah_tail;
135 * a resolution identifier pool variable
137 * This is a non critical identifier useful for debugging
139 static unsigned long long rid = 0;
149 if (NULL == strstr (name, "._"))
152 ndup = GNUNET_strdup (name);
155 if (NULL == strtok (NULL, "."))
158 if (NULL == strtok (NULL, "."))
161 if (NULL != strtok (NULL, "."))
168 * Determine if this name is canonical.
170 * a.b.gnunet = not canonical
173 * @param name the name to test
174 * @return 1 if canonical
177 is_canonical(char* name)
182 ndup = GNUNET_strdup (name);
185 for (tok = strtok (NULL, "."); tok != NULL; tok = strtok (NULL, "."))
201 * Callback that shortens authorities
203 * @param gph the handle containing the name to shorten
206 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
210 create_pseu_cont (void* cls, int32_t success, const char* emsg)
212 //FIXME do sth with error
213 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
214 struct AuthorityChain *iter;
216 gph->namestore_task = NULL;
219 iter = gph->ahead->next;
220 GNUNET_free (gph->ahead);
222 } while (iter != NULL);
223 GNUNET_CRYPTO_rsa_key_free (gph->key);
224 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
230 * Namestore calls this function if we have record for this name.
231 * (or with rd_count=0 to indicate no matches)
233 * @param cls the pending query
234 * @param key the key of the zone we did the lookup
235 * @param expiration expiration date of the namestore entry
236 * @param name the name for which we need an authority
237 * @param rd_count the number of records with 'name'
238 * @param rd the record data
239 * @param signature the signature of the authority for the record data
242 process_pseu_lookup_ns (void* cls,
243 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
244 struct GNUNET_TIME_Absolute expiration,
245 const char *name, unsigned int rd_count,
246 const struct GNUNET_NAMESTORE_RecordData *rd,
247 const struct GNUNET_CRYPTO_RsaSignature *signature)
249 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
250 struct GNUNET_NAMESTORE_RecordData new_pkey;
251 struct AuthorityChain *iter;
253 gph->namestore_task = NULL;
256 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
257 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
258 if (0 == strcmp (gph->name, name))
260 if (gph->ahead->next != NULL)
262 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
266 iter = gph->ahead->next;
267 GNUNET_free (gph->ahead);
269 shorten_authority_chain (gph);
277 iter = gph->ahead->next;
278 GNUNET_free (gph->ahead);
280 } while (iter != NULL);
281 GNUNET_CRYPTO_rsa_key_free (gph->key);
282 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "GNS_AUTO_PSEU: Trying delegated name %s\n", gph->name);
289 memcpy (gph->test_name, gph->name, strlen (gph->name)+1);
290 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
293 GNUNET_NAMESTORE_TYPE_ANY,
294 &process_pseu_lookup_ns,
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
303 new_pkey.expiration_time = UINT64_MAX;
304 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
305 new_pkey.data = &gph->ahead->zone;
306 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
307 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
308 | GNUNET_NAMESTORE_RF_PRIVATE
309 | GNUNET_NAMESTORE_RF_PENDING;
310 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
314 &create_pseu_cont, //cont
319 * process result of a dht pseu lookup
321 * @param gph the handle
322 * @param name the pseu result or NULL
325 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
329 memcpy (gph->test_name, gph->ahead->name, strlen (gph->ahead->name)+1);
333 memcpy (gph->test_name, name, strlen(name)+1);
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
341 * Check for collision
343 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
346 GNUNET_NAMESTORE_TYPE_ANY,
347 &process_pseu_lookup_ns,
352 * Handle timeout for dht request
354 * @param cls the request handle as closure
355 * @param tc the task context
358 handle_auth_discovery_timeout(void *cls,
359 const struct GNUNET_SCHEDULER_TaskContext *tc)
361 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
362 struct AuthorityChain *iter;
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
366 GNUNET_DHT_get_stop (gph->get_handle);
367 gph->get_handle = NULL;
369 if (gph->ahead->next != NULL)
371 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
375 iter = gph->ahead->next;
376 GNUNET_free (gph->ahead);
378 shorten_authority_chain (gph);
383 process_pseu_result (gph, NULL);
387 * Function called when we find a PSEU entry in the DHT
389 * @param cls the request handle
390 * @param exp lifetime
391 * @param key the key the record was stored under
392 * @param get_path get path
393 * @param get_path_length get path length
394 * @param put_path put path
395 * @param put_path_length put path length
396 * @param type the block type
397 * @param size the size of the record
398 * @param data the record data
401 process_auth_discovery_dht_result(void* cls,
402 struct GNUNET_TIME_Absolute exp,
403 const struct GNUNET_HashCode * key,
404 const struct GNUNET_PeerIdentity *get_path,
405 unsigned int get_path_length,
406 const struct GNUNET_PeerIdentity *put_path,
407 unsigned int put_path_length,
408 enum GNUNET_BLOCK_Type type,
409 size_t size, const void *data)
411 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
412 struct AuthorityChain *iter;
413 struct GNSNameRecordBlock *nrb;
414 char* rd_data = (char*)data;
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
424 /* stop lookup and timeout task */
425 GNUNET_DHT_get_stop (gph->get_handle);
426 gph->get_handle = NULL;
427 GNUNET_SCHEDULER_cancel (gph->timeout);
431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
432 "GNS_GET_AUTH: got dht result null!\n", size);
436 iter = gph->ahead->next;
437 GNUNET_free (gph->ahead);
439 } while (iter != NULL);
440 GNUNET_CRYPTO_rsa_key_free (gph->key);
441 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
446 nrb = (struct GNSNameRecordBlock*)data;
450 nrb = (struct GNSNameRecordBlock*)data;
452 name = (char*)&nrb[1];
453 num_records = ntohl (nrb->rd_count);
455 struct GNUNET_NAMESTORE_RecordData rd[num_records];
457 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
458 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
460 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
465 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
466 "GNS_GET_AUTH: Error deserializing data!\n");
470 for (i=0; i < num_records; i++)
472 if ((strcmp (name, "+") == 0) &&
473 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
476 process_pseu_result (gph, (char*)rd[i].data);
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in dht!\n");
485 if (gph->ahead->next != NULL)
487 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
491 iter = gph->ahead->next;
492 GNUNET_free (gph->ahead);
494 shorten_authority_chain (gph);
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499 "GNS_GET_AUTH: finished shorten, no results!\n");
500 process_pseu_result (gph, NULL);
504 * Process PSEU discovery for shorten via namestore
506 * @param cls the GetPseuAuthorityHandle
507 * @param key the public key
508 * @param expiration recorddata expiration
509 * @param name the looked up name
510 * @param rd_count number of records in set
511 * @param rd record data
512 * @param signature the signature
515 process_auth_discovery_ns_result(void* cls,
516 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
517 struct GNUNET_TIME_Absolute expiration,
519 unsigned int rd_count,
520 const struct GNUNET_NAMESTORE_RecordData *rd,
521 const struct GNUNET_CRYPTO_RsaSignature *signature)
524 struct GNUNET_CRYPTO_ShortHashCode name_hash;
525 struct GNUNET_HashCode lookup_key;
526 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
527 struct GNUNET_HashCode name_hash_double;
528 struct GNUNET_HashCode zone_hash_double;
530 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
531 struct AuthorityChain *iter;
533 gph->namestore_task = NULL;
540 GNUNET_CRYPTO_short_hash ("+", strlen ("+"), &name_hash);
541 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
542 GNUNET_CRYPTO_short_hash_double (&gph->ahead->zone, &zone_hash_double);
543 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
544 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
548 "+", (char*)&lookup_key_string);
550 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
551 &handle_auth_discovery_timeout, gph);
553 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
555 GNUNET_assert (gph->get_handle == NULL);
557 gph->get_handle = GNUNET_DHT_get_start(dht_handle,
558 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
560 DHT_GNS_REPLICATION_LEVEL,
564 &process_auth_discovery_dht_result,
569 for (i=0; i < rd_count; i++)
571 if ((strcmp (name, "+") == 0) &&
572 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
575 process_pseu_result (gph, (char*)rd[i].data);
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
582 if (gph->ahead->next != NULL)
584 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
588 iter = gph->ahead->next;
589 GNUNET_free (gph->ahead);
591 shorten_authority_chain (gph);
596 process_pseu_result (gph, NULL);
600 * Callback called by namestore for a zone to name
603 * @param cls the closure
604 * @param zone_key the zone we queried
605 * @param expire the expiration time of the name
606 * @param name the name found or NULL
607 * @param rd_len number of records for the name
608 * @param rd the record data (PKEY) for the name
609 * @param signature the signature for the record data
612 process_zone_to_name_discover (void *cls,
613 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
614 struct GNUNET_TIME_Absolute expire,
617 const struct GNUNET_NAMESTORE_RecordData *rd,
618 const struct GNUNET_CRYPTO_RsaSignature *signature)
620 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
621 struct AuthorityChain *iter;
623 gph->namestore_task = NULL;
624 /* we found a match in our own zone */
627 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
628 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
633 iter = gph->ahead->next;
634 GNUNET_free (gph->ahead);
636 } while (iter != NULL);
637 GNUNET_CRYPTO_rsa_key_free (gph->key);
638 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
644 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
647 GNUNET_GNS_RECORD_PSEU,
648 &process_auth_discovery_ns_result,
656 * Callback that shortens authorities
658 * @param gph the handle to the shorten request
661 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665 "GNS_AUTO_PSEU: New authority %s discovered\n",
668 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
671 &process_zone_to_name_discover,
677 start_shorten (struct AuthorityChain *atail,
678 struct GNUNET_CRYPTO_RsaPrivateKey *key)
680 struct AuthorityChain *new_head = NULL;
681 struct AuthorityChain *new_tail = NULL;
682 struct AuthorityChain *iter;
683 struct AuthorityChain *acopy;
684 struct GetPseuAuthorityHandle *gph;
685 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
686 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
688 /* First copy the authority chain in reverse order */
689 for (iter = atail; iter != NULL; iter = iter->prev)
691 acopy = GNUNET_malloc (sizeof (struct AuthorityChain));
692 memcpy (acopy, iter, sizeof (struct AuthorityChain));
695 GNUNET_CONTAINER_DLL_insert (new_head, new_tail, acopy);
698 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
700 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
701 pb_key = GNUNET_CRYPTO_rsa_encode_key (key);
702 gph->key = GNUNET_CRYPTO_rsa_decode_key ((char*)pb_key, ntohs (pb_key->len));
703 //gph->key = key;//GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
704 //memcpy (gph->key, key, sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
706 GNUNET_CRYPTO_short_hash (&pkey,
707 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
709 gph->ahead = new_head;
711 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
713 shorten_authority_chain (gph);
717 * Initialize the resolver
719 * @param nh the namestore handle
720 * @param dh the dht handle
721 * @param lz the local zone's hash
722 * @param cfg configuration handle
723 * @param max_bg_queries maximum number of parallel background queries in dht
724 * @param ignore_pending ignore records that still require user confirmation
726 * @return GNUNET_OK on success
729 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
730 struct GNUNET_DHT_Handle *dh,
731 struct GNUNET_CRYPTO_ShortHashCode lz,
732 const struct GNUNET_CONFIGURATION_Handle *cfg,
733 unsigned long long max_bg_queries,
736 namestore_handle = nh;
740 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
742 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
743 max_allowed_background_queries = max_bg_queries;
744 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
746 ignore_pending_records = ignore_pending;
757 GNUNET_RESOLVER_connect (cfg);
759 if (NULL == vpn_handle)
761 vpn_handle = GNUNET_VPN_connect (cfg);
762 if (NULL == vpn_handle)
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "GNS_PHASE_INIT: Error connecting to VPN!\n");
767 return GNUNET_SYSERR;
771 if ((namestore_handle != NULL) && (dht_handle != NULL))
776 return GNUNET_SYSERR;
783 * @param cls closure to iterator
784 * @param node heap nodes
785 * @param element the namestorebgtask
786 * @param cost heap cost
787 * @return always GNUNET_YES
790 cleanup_pending_ns_tasks(void* cls,
791 struct GNUNET_CONTAINER_HeapNode *node,
793 GNUNET_CONTAINER_HeapCostType cost)
795 struct NamestoreBGTask *nbg = (struct NamestoreBGTask *)element;
796 ResolverCleanupContinuation cont = cls;
798 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
799 "GNS_CLEANUP: Terminating ns task\n");
800 GNUNET_NAMESTORE_cancel (nbg->qe);
802 GNUNET_CONTAINER_heap_remove_node(node);
804 if (GNUNET_CONTAINER_heap_get_size(ns_task_heap) == 0)
814 * @param rh resolver handle
815 * @param rlh record lookup handle
816 * @param rd_count number of results
820 finish_lookup (struct ResolverHandle *rh,
821 struct RecordLookupHandle* rlh,
822 unsigned int rd_count,
823 const struct GNUNET_NAMESTORE_RecordData *rd);
827 * Cleanup background lookups FIXME get rid of this??
829 * @param cls closure to iterator
830 * @param node heap nodes
831 * @param element the resolver handle
832 * @param cost heap cost
833 * @return always GNUNET_YES
836 cleanup_pending_background_queries (void* cls,
837 struct GNUNET_CONTAINER_HeapNode *node,
839 GNUNET_CONTAINER_HeapCostType cost)
841 struct ResolverHandle *rh = (struct ResolverHandle *)element;
842 ResolverCleanupContinuation cont = cls;
844 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
845 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
848 //finish_lookup (rh, rh->proc_cls, 0, NULL);
849 //rh->get_handle = NULL;
850 //rh->proc(rh->proc_cls, rh, 0, NULL);
852 GNUNET_CONTAINER_heap_remove_node(node);
854 if (GNUNET_CONTAINER_heap_get_size(dht_lookup_heap) == 0)
856 if (GNUNET_CONTAINER_heap_get_size(ns_task_heap) == 0)
860 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
861 &cleanup_pending_ns_tasks,
872 * Helper function to free resolver handle
874 * @param rh the handle to free
877 free_resolver_handle (struct ResolverHandle* rh)
879 struct AuthorityChain *ac;
880 struct AuthorityChain *ac_next;
885 ac = rh->authority_chain_head;
894 if (NULL != rh->get_handle)
895 GNUNET_DHT_get_stop (rh->get_handle);
897 if (NULL != rh->dns_raw_packet)
898 GNUNET_free (rh->dns_raw_packet);
900 if (NULL != rh->namestore_task)
901 GNUNET_NAMESTORE_cancel (rh->namestore_task);
902 rh->namestore_task = NULL;
904 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
905 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
907 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
908 GNUNET_SCHEDULER_cancel (rh->timeout_task);
910 if (NULL != rh->dns_sock)
911 GNUNET_NETWORK_socket_close (rh->dns_sock);
912 if (NULL != rh->dns_resolver_handle)
913 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
921 * @param rh resolver handle
922 * @param nsh name shorten handle
925 finish_shorten (struct ResolverHandle *rh,
926 struct NameShortenHandle *nsh);
930 * @param rh resolver handle
931 * @param nah get name authority handle
934 finish_get_auth (struct ResolverHandle *rh,
935 struct GetNameAuthorityHandle* rlh);
940 gns_resolver_cleanup (ResolverCleanupContinuation cont)
943 struct GetPseuAuthorityHandle *tmp;
944 struct AuthorityChain *iter;
948 for (tmp = gph_head; tmp != NULL; tmp = gph_head)
950 if (tmp->get_handle != NULL)
951 GNUNET_DHT_get_stop (tmp->get_handle);
952 tmp->get_handle = NULL;
953 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
954 GNUNET_SCHEDULER_cancel (tmp->timeout);
955 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
957 if (NULL != tmp->namestore_task)
958 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
959 tmp->namestore_task = NULL;
964 iter = tmp->ahead->next;
965 GNUNET_free (tmp->ahead);
967 } while (iter != NULL);
969 GNUNET_CRYPTO_rsa_key_free (tmp->key);
970 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, tmp);
974 while (NULL != rlh_head)
976 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
979 while (NULL != nsh_head)
981 finish_shorten (nsh_head, nsh_head->proc_cls);
984 while (NULL != nah_head)
986 finish_get_auth (nah_head, nah_head->proc_cls);
989 s = GNUNET_CONTAINER_heap_get_size(dht_lookup_heap);
990 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
991 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
993 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
994 &cleanup_pending_background_queries,
996 else if (0 != GNUNET_CONTAINER_heap_get_size(ns_task_heap))
998 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
999 &cleanup_pending_ns_tasks,
1010 * Callback when record data is put into namestore
1012 * @param cls the closure
1013 * @param success GNUNET_OK on success
1014 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
1017 on_namestore_record_put_result(void *cls,
1021 struct NamestoreBGTask *nbg = cls;
1023 GNUNET_CONTAINER_heap_remove_node (nbg->node);
1026 if (GNUNET_NO == success)
1028 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1029 "GNS_NS: records already in namestore\n");
1032 else if (GNUNET_YES == success)
1034 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1035 "GNS_NS: records successfully put in namestore\n");
1039 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1040 "GNS_NS: Error putting records into namestore: %s\n", emsg);
1044 handle_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1046 struct ResolverHandle *rh = cls;
1048 if (rh->timeout_cont)
1049 rh->timeout_cont(rh->timeout_cont_cls, tc);
1053 * Processor for background lookups in the DHT
1055 * @param cls closure (NULL)
1056 * @param rd_count number of records found (not 0)
1057 * @param rd record data
1060 background_lookup_result_processor(void *cls,
1062 const struct GNUNET_NAMESTORE_RecordData *rd)
1064 //We could do sth verbose/more useful here but it doesn't make any difference
1065 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1066 "GNS_BG: background dht lookup finished. (%d results)\n",
1071 * Handle timeout for DHT requests
1073 * @param cls the request handle as closure
1074 * @param tc the task context
1077 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1079 struct ResolverHandle *rh = cls;
1080 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1081 char new_name[MAX_DNS_NAME_LENGTH];
1083 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1084 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
1085 rh->id, rh->name, rh->timeout.rel_value);
1087 * Start resolution in bg
1089 //strcpy(new_name, rh->name);
1090 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1091 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
1092 rh->name, GNUNET_GNS_TLD);
1094 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1095 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
1096 rh->id, new_name, rlh->record_type);
1098 gns_resolver_lookup_record(rh->authority,
1099 rh->private_local_zone,
1103 GNUNET_TIME_UNIT_FOREVER_REL,
1105 &background_lookup_result_processor,
1107 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1109 GNUNET_DHT_get_stop (rh->get_handle);
1110 rh->get_handle = NULL;
1111 rh->proc(rh->proc_cls, rh, 0, NULL);
1116 * Function called when we get a result from the dht
1117 * for our record query
1119 * @param cls the request handle
1120 * @param exp lifetime
1121 * @param key the key the record was stored under
1122 * @param get_path get path
1123 * @param get_path_length get path length
1124 * @param put_path put path
1125 * @param put_path_length put path length
1126 * @param type the block type
1127 * @param size the size of the record
1128 * @param data the record data
1131 process_record_result_dht(void* cls,
1132 struct GNUNET_TIME_Absolute exp,
1133 const struct GNUNET_HashCode * key,
1134 const struct GNUNET_PeerIdentity *get_path,
1135 unsigned int get_path_length,
1136 const struct GNUNET_PeerIdentity *put_path,
1137 unsigned int put_path_length,
1138 enum GNUNET_BLOCK_Type type,
1139 size_t size, const void *data)
1141 struct ResolverHandle *rh;
1142 struct RecordLookupHandle *rlh;
1143 struct GNSNameRecordBlock *nrb;
1144 uint32_t num_records;
1146 char* rd_data = (char*)data;
1150 rh = (struct ResolverHandle *)cls;
1151 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1152 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
1154 //FIXME maybe check expiration here, check block type
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_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1311 // &dht_lookup_timeout,
1313 rh->timeout_cont = &dht_lookup_timeout;
1314 rh->timeout_cont_cls = rh;
1318 if (max_allowed_background_queries <=
1319 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1321 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1322 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
1323 rh_heap_root->get_handle = NULL;
1324 rh_heap_root->dht_heap_node = NULL;
1326 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1327 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1328 rh->id, rh_heap_root->name);
1329 rh_heap_root->proc(rh_heap_root->proc_cls,
1334 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1336 GNUNET_TIME_absolute_get().abs_value);
1339 xquery = htonl(rlh->record_type);
1341 GNUNET_assert(rh->get_handle == NULL);
1342 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1343 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1345 DHT_GNS_REPLICATION_LEVEL,
1349 &process_record_result_dht,
1356 * Namestore calls this function if we have record for this name.
1357 * (or with rd_count=0 to indicate no matches)
1359 * @param cls the pending query
1360 * @param key the key of the zone we did the lookup
1361 * @param expiration expiration date of the namestore entry
1362 * @param name the name for which we need an authority
1363 * @param rd_count the number of records with 'name'
1364 * @param rd the record data
1365 * @param signature the signature of the authority for the record data
1368 process_record_result_ns (void* cls,
1369 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1370 struct GNUNET_TIME_Absolute expiration,
1371 const char *name, unsigned int rd_count,
1372 const struct GNUNET_NAMESTORE_RecordData *rd,
1373 const struct GNUNET_CRYPTO_RsaSignature *signature)
1375 struct ResolverHandle *rh;
1376 struct RecordLookupHandle *rlh;
1377 struct GNUNET_TIME_Relative remaining_time;
1378 struct GNUNET_CRYPTO_ShortHashCode zone;
1379 struct GNUNET_TIME_Absolute et;
1382 rh = (struct ResolverHandle *) cls;
1383 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1385 rh->namestore_task = NULL;
1386 GNUNET_CRYPTO_short_hash(key,
1387 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1389 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1397 rh->status |= RSL_RECORD_EXISTS;
1399 if (remaining_time.rel_value == 0)
1401 rh->status |= RSL_RECORD_EXPIRED;
1408 * Lookup terminated and no results
1410 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1411 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1414 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1415 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1418 * Our zone and no result? Cannot resolve TT
1420 rh->proc(rh->proc_cls, rh, 0, NULL);
1426 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1427 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1429 for (i=0; i<rd_count;i++)
1431 if (rd[i].record_type != rlh->record_type)
1434 if (ignore_pending_records &&
1435 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1437 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1438 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1443 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1444 et.abs_value = rd[i].expiration_time;
1445 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
1448 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1449 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1459 if (rh->answered == 0)
1461 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1462 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1463 rh->proc(rh->proc_cls, rh, 0, NULL);
1467 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1468 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1469 rh->id, rh->answered, rd_count);
1471 rh->proc(rh->proc_cls, rh, rd_count, rd);
1477 * VPN redirect result callback
1479 * @param cls the resolver handle
1480 * @param af the requested address family
1481 * @param address in_addr(6) respectively
1484 process_record_result_vpn (void* cls, int af, const void *address)
1486 struct ResolverHandle *rh = cls;
1487 struct RecordLookupHandle *rlh;
1488 struct GNUNET_NAMESTORE_RecordData rd;
1490 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1492 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1493 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1497 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1498 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1500 if (rlh->record_type != GNUNET_GNS_RECORD_A)
1502 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1503 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1505 rh->proc (rh->proc_cls, rh, 0, NULL);
1508 rd.record_type = GNUNET_GNS_RECORD_A;
1509 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1511 rd.data_size = sizeof (struct in_addr);
1513 rh->proc (rh->proc_cls, rh, 1, &rd);
1516 else if (af == AF_INET6)
1518 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1519 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1521 if (rlh->record_type != GNUNET_GNS_RECORD_AAAA)
1523 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1524 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1526 rh->proc (rh->proc_cls, rh, 0, NULL);
1529 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1530 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1532 rd.data_size = sizeof (struct in6_addr);
1534 rh->proc (rh->proc_cls, rh, 1, &rd);
1538 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1539 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1541 rh->proc (rh->proc_cls, rh, 0, NULL);
1548 * Process VPN lookup result for record
1550 * @param cls the record lookup handle
1551 * @param rh resolver handle
1552 * @param rd_count number of results (1)
1553 * @param rd record data containing the result
1556 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1557 unsigned int rd_count,
1558 const struct GNUNET_NAMESTORE_RecordData *rd)
1560 struct RecordLookupHandle* rlh = (struct RecordLookupHandle*) cls;
1564 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1565 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1568 /* give up, cannot resolve */
1569 finish_lookup(rh, rlh, 0, NULL);
1573 /* results found yay */
1574 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1575 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1577 finish_lookup(rh, rlh, rd_count, rd);
1582 * Sends a UDP dns query to a nameserver specified in the rh
1584 * @param rh the resolver handle
1587 send_dns_packet (struct ResolverHandle *rh);
1591 handle_dns_resolver (void *cls,
1592 const struct sockaddr *addr,
1595 struct ResolverHandle *rh = cls;
1596 struct RecordLookupHandle *rlh = rh->proc_cls;
1597 struct GNUNET_NAMESTORE_RecordData rd;
1598 struct sockaddr_in *sai;
1599 struct sockaddr_in6 *sai6;
1603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1604 "No address found in DNS!\n");
1605 finish_lookup (rh, rlh, 0, NULL);
1609 if (addrlen == sizeof (struct sockaddr_in))
1611 sai = (struct sockaddr_in*) addr;
1612 rd.record_type = GNUNET_GNS_RECORD_A;
1613 rd.data_size = sizeof (struct in_addr);
1614 rd.data = &sai->sin_addr;
1618 sai6 = (struct sockaddr_in6*) addr;
1619 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1620 rd.data_size = sizeof (struct in6_addr);
1621 rd.data = &sai6->sin6_addr;
1624 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1627 finish_lookup (rh, rlh, 1, &rd);
1632 * Resolve DNS name via local stub resolver
1634 * @param rh the resolver handle
1637 resolve_dns_name (struct ResolverHandle *rh)
1639 struct RecordLookupHandle *rlh = rh->proc_cls;
1642 if ((rlh->record_type != GNUNET_GNS_RECORD_A) &&
1643 (rlh->record_type != GNUNET_GNS_RECORD_AAAA))
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1646 "Can only resolve A/AAAA via stub... abort\n");
1647 finish_lookup (rh, rlh, 0, NULL);
1651 if (rlh->record_type == GNUNET_GNS_RECORD_A)
1656 //GNUNET_RESOLVER_connect (cfg); FIXME into init
1658 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1661 &handle_dns_resolver,
1667 * Read DNS udp packet from socket
1669 * @param cls the resolver handle
1670 * @param tc task context
1673 read_dns_response (void *cls,
1674 const struct GNUNET_SCHEDULER_TaskContext *tc)
1676 struct ResolverHandle *rh = cls;
1677 struct RecordLookupHandle *rlh = rh->proc_cls;
1678 char buf[UINT16_MAX];
1680 struct sockaddr_in addr;
1682 struct GNUNET_DNSPARSER_Packet *packet;
1683 struct GNUNET_NAMESTORE_RecordData rd;
1684 int found_delegation = GNUNET_NO;
1685 int found_cname = GNUNET_NO;
1686 char* delegation_name = NULL;
1687 int zone_offset = 0;
1690 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1691 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1693 /* timeout or shutdown */
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "Terminating DNS query %d\n", tc->reason);
1696 finish_lookup (rh, rlh, 0, NULL);
1700 addrlen = sizeof (addr);
1701 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1703 (struct sockaddr*) &addr,
1708 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1709 finish_lookup (rh, rlh, 0, NULL);
1713 packet = GNUNET_DNSPARSER_parse (buf, r);
1717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1718 "Failed to parse DNS reply!\n");
1719 finish_lookup (rh, rlh, 0, NULL);
1723 for (i = 0; i < packet->num_answers; i++)
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1726 "Got record type %d (want %d)\n",
1727 packet->answers[i].type,
1729 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1730 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1733 "CNAME... restarting query with %s\n",
1734 packet->answers[i].data.hostname
1736 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1737 found_cname = GNUNET_YES;
1738 //send_dns_packet (rh);
1739 //GNUNET_DNSPARSER_free_packet (packet);
1743 if ((packet->answers[i].type == rlh->record_type) &&
1744 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1748 rd.data = packet->answers[i].data.raw.data;
1749 rd.data_size = packet->answers[i].data.raw.data_len;
1750 rd.record_type = packet->answers[i].type;
1752 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1753 finish_lookup (rh, rlh, 1, &rd);
1754 GNUNET_DNSPARSER_free_packet (packet);
1759 if (GNUNET_YES == found_cname)
1761 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1763 if (0 > zone_offset)
1766 /* restart query with CNAME */
1767 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770 "Asking same server for %s\n", rh->dns_name);
1771 send_dns_packet (rh);
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 "Trying system resolver for %s\n", rh->dns_name);
1777 resolve_dns_name (rh);
1780 GNUNET_DNSPARSER_free_packet (packet);
1784 for (i = 0; i < packet->num_authority_records; i++)
1787 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1790 "Found NS delegation!\n");
1791 found_delegation = GNUNET_YES;
1792 delegation_name = packet->authority_records[i].data.hostname;
1797 for (i = 0; i < packet->num_additional_records; i++)
1799 if (found_delegation == GNUNET_NO)
1802 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1803 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1805 GNUNET_assert (sizeof (struct in_addr) ==
1806 packet->authority_records[i].data.raw.data_len);
1808 rh->dns_addr.sin_addr =
1809 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1810 send_dns_packet (rh);
1811 GNUNET_DNSPARSER_free_packet (packet);
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1817 "Nothing useful in DNS reply!\n");
1818 finish_lookup (rh, rlh, 0, NULL);
1819 GNUNET_DNSPARSER_free_packet (packet);
1824 * Sends a UDP dns query to a nameserver specified in the rh
1826 * @param rh the request handle
1829 send_dns_packet (struct ResolverHandle *rh)
1831 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1832 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1835 "Sending %dbyte DNS query\n",
1836 rh->dns_raw_packet_size);
1838 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1840 rh->dns_raw_packet_size,
1841 (struct sockaddr*)&rh->dns_addr,
1842 sizeof (struct sockaddr_in));
1844 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1845 rh->timeout, //FIXME less?
1851 GNUNET_NETWORK_fdset_destroy (rset);
1856 * The final phase of resoution.
1857 * We found a NS RR and want to resolve via DNS
1859 * @param rh the pending lookup handle
1860 * @param rd_count length of record data
1861 * @param rd record data containing VPN RR
1864 resolve_record_dns (struct ResolverHandle *rh,
1866 const struct GNUNET_NAMESTORE_RecordData *rd)
1868 struct GNUNET_DNSPARSER_Query query;
1869 struct GNUNET_DNSPARSER_Packet packet;
1870 struct GNUNET_DNSPARSER_Flags flags;
1871 struct in_addr dnsip;
1872 struct sockaddr_in addr;
1873 struct sockaddr *sa;
1875 struct RecordLookupHandle *rlh = rh->proc_cls;
1877 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1879 /* We cancel here as to not include the ns lookup in the timeout */
1880 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1882 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1883 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1885 /* Start shortening */
1886 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1889 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1891 start_shorten (rh->authority_chain_tail,
1895 for (i = 0; i < rd_count; i++)
1897 /* Synthesize dns name */
1898 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
1900 strcpy (rh->dns_zone, (char*)rd[i].data);
1901 if (0 == strcmp (rh->name, ""))
1902 strcpy (rh->dns_name, (char*)rd[i].data);
1904 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1907 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
1908 dnsip = *((struct in_addr*)rd[i].data);
1911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1912 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1916 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1917 if (rh->dns_sock == NULL)
1919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1920 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1922 finish_lookup (rh, rlh, 0, NULL);
1926 memset (&addr, 0, sizeof (struct sockaddr_in));
1927 sa = (struct sockaddr *) &addr;
1928 sa->sa_family = AF_INET;
1929 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1931 sizeof (struct sockaddr_in)))
1933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1934 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1936 finish_lookup (rh, rlh, 0, NULL);
1940 /*TODO create dnsparser query, serialize, sendto, handle reply*/
1941 query.name = rh->dns_name;
1942 query.type = rlh->record_type;
1943 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1944 memset (&flags, 0, sizeof (flags));
1945 flags.recursion_desired = 1;
1946 flags.checking_disabled = 1;
1947 packet.queries = &query;
1948 packet.answers = NULL;
1949 packet.authority_records = NULL;
1950 packet.num_queries = 1;
1951 packet.num_answers = 0;
1952 packet.num_authority_records = 0;
1953 packet.num_additional_records = 0;
1954 packet.flags = flags;
1956 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1958 &rh->dns_raw_packet,
1959 &rh->dns_raw_packet_size))
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1962 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1964 GNUNET_NETWORK_socket_close (rh->dns_sock);
1965 finish_lookup (rh, rlh, 0, NULL);
1969 rh->dns_addr.sin_family = AF_INET;
1970 rh->dns_addr.sin_port = htons (53); //domain
1971 rh->dns_addr.sin_addr = dnsip;
1972 #if HAVE_SOCKADDR_IN_SIN_LEN
1973 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1976 send_dns_packet (rh);
1981 * The final phase of resoution.
1982 * We found a VPN RR and want to request an IPv4/6 address
1984 * @param rh the pending lookup handle
1985 * @param rd_count length of record data
1986 * @param rd record data containing VPN RR
1989 resolve_record_vpn (struct ResolverHandle *rh,
1991 const struct GNUNET_NAMESTORE_RecordData *rd)
1993 struct RecordLookupHandle *rlh = rh->proc_cls;
1994 struct GNUNET_HashCode serv_desc;
1995 struct vpn_data* vpn;
1998 /* We cancel here as to not include the ns lookup in the timeout */
1999 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2001 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2002 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2004 /* Start shortening */
2005 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2008 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
2010 start_shorten (rh->authority_chain_tail,
2014 vpn = (struct vpn_data*)rd->data;
2017 GNUNET_CRYPTO_hash ((char*)&vpn[1],
2018 strlen ((char*)&vpn[1]) + 1,
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2021 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
2024 GNUNET_h2s (&vpn->peer));
2026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2027 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
2030 GNUNET_h2s (&serv_desc));
2031 rh->proc = &handle_record_vpn;
2033 if (NULL == vpn_handle)
2035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2036 "GNS_PHASE_REC_VPN-%llu: VPN not connected!\n",
2038 finish_lookup (rh, rh->proc_cls, 0, NULL);
2042 if (rlh->record_type == GNUNET_GNS_RECORD_A)
2048 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
2049 af, ntohs (vpn->proto),
2050 (struct GNUNET_PeerIdentity *)&vpn->peer,
2053 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
2054 &process_record_result_vpn,
2060 * The final phase of resolution.
2061 * rh->name is a name that is canonical and we do not have a delegation.
2062 * Query namestore for this record
2064 * @param rh the pending lookup handle
2067 resolve_record_ns(struct ResolverHandle *rh)
2069 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
2071 /* We cancel here as to not include the ns lookup in the timeout */
2072 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2074 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2075 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2077 /* Start shortening */
2078 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2081 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
2083 start_shorten (rh->authority_chain_tail,
2088 * Try to resolve this record in our namestore.
2089 * The name to resolve is now in rh->authority_name
2090 * since we tried to resolve it to an authority
2093 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
2097 &process_record_result_ns,
2104 * Handle timeout for DHT requests
2106 * @param cls the request handle as closure
2107 * @param tc the task context
2110 dht_authority_lookup_timeout(void *cls,
2111 const struct GNUNET_SCHEDULER_TaskContext *tc)
2113 struct ResolverHandle *rh = cls;
2114 struct RecordLookupHandle *rlh = rh->proc_cls;
2115 char new_name[MAX_DNS_NAME_LENGTH];
2117 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2118 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
2119 rh->id, rh->authority_name, rh->timeout.rel_value);
2121 rh->status |= RSL_TIMED_OUT;
2123 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2125 if (NULL != rh->get_handle)
2126 GNUNET_DHT_get_stop (rh->get_handle);
2128 rh->get_handle = NULL;
2130 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
2132 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2133 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
2135 rh->proc(rh->proc_cls, rh, 0, NULL);
2139 if (strcmp(rh->name, "") == 0)
2142 * promote authority back to name and try to resolve record
2144 strcpy(rh->name, rh->authority_name);
2145 rh->proc(rh->proc_cls, rh, 0, NULL);
2150 * Start resolution in bg
2152 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
2153 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
2154 //strcpy(new_name, rh->name);
2155 //strcpy(new_name+strlen(new_name), ".");
2156 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
2158 strcpy(rh->name, new_name);
2160 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2161 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2162 rh->id, rh->name, rlh->record_type);
2164 gns_resolver_lookup_record(rh->authority,
2165 rh->private_local_zone,
2169 GNUNET_TIME_UNIT_FOREVER_REL,
2171 &background_lookup_result_processor,
2174 rh->proc(rh->proc_cls, rh, 0, NULL);
2178 static void resolve_delegation_dht(struct ResolverHandle *rh);
2181 static void resolve_delegation_ns(struct ResolverHandle *rh);
2185 * Namestore resolution for delegation finished. Processing result.
2187 * @param cls the closure
2188 * @param rh resolver handle
2189 * @param rd_count number of results (always 0)
2190 * @param rd record data (always NULL)
2193 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2194 unsigned int rd_count,
2195 const struct GNUNET_NAMESTORE_RecordData *rd);
2199 * This is a callback function that checks for key revocation
2201 * @param cls the pending query
2202 * @param key the key of the zone we did the lookup
2203 * @param expiration expiration date of the record data set in the namestore
2204 * @param name the name for which we need an authority
2205 * @param rd_count the number of records with 'name'
2206 * @param rd the record data
2207 * @param signature the signature of the authority for the record data
2210 process_pkey_revocation_result_ns (void *cls,
2211 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2212 struct GNUNET_TIME_Absolute expiration,
2214 unsigned int rd_count,
2215 const struct GNUNET_NAMESTORE_RecordData *rd,
2216 const struct GNUNET_CRYPTO_RsaSignature *signature)
2218 struct ResolverHandle *rh = cls;
2219 struct GNUNET_TIME_Relative remaining_time;
2222 rh->namestore_task = NULL;
2223 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2225 for (i = 0; i < rd_count; i++)
2227 if (rd[i].record_type == GNUNET_GNS_RECORD_REV)
2229 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2230 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2232 rh->status |= RSL_PKEY_REVOKED;
2233 rh->proc (rh->proc_cls, rh, 0, NULL);
2238 if ((name == NULL) ||
2239 (remaining_time.rel_value == 0))
2241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2242 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2245 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2247 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2248 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2249 rh->id, "+.gnunet", GNUNET_GNS_RECORD_REV);
2251 gns_resolver_lookup_record(rh->authority,
2252 rh->private_local_zone,
2253 GNUNET_GNS_RECORD_REV,
2256 GNUNET_TIME_UNIT_FOREVER_REL,
2258 &background_lookup_result_processor,
2262 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2263 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2266 * We are done with PKEY resolution if name is empty
2267 * else resolve again with new authority
2269 if (strcmp (rh->name, "") == 0)
2270 rh->proc (rh->proc_cls, rh, 0, NULL);
2272 resolve_delegation_ns (rh);
2278 * Callback when record data is put into namestore
2280 * @param cls the closure
2281 * @param success GNUNET_OK on success
2282 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2285 on_namestore_delegation_put_result(void *cls,
2289 struct NamestoreBGTask *nbg = cls;
2291 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2294 if (GNUNET_NO == success)
2296 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2297 "GNS_NS: records already in namestore\n");
2300 else if (GNUNET_YES == success)
2302 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2303 "GNS_NS: records successfully put in namestore\n");
2307 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2308 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2312 * Function called when we get a result from the dht
2313 * for our query. Recursively tries to resolve authorities
2316 * @param cls the request handle
2317 * @param exp lifetime
2318 * @param key the key the record was stored under
2319 * @param get_path get path
2320 * @param get_path_length get path length
2321 * @param put_path put path
2322 * @param put_path_length put path length
2323 * @param type the block type
2324 * @param size the size of the record
2325 * @param data the record data
2328 process_delegation_result_dht(void* cls,
2329 struct GNUNET_TIME_Absolute exp,
2330 const struct GNUNET_HashCode * key,
2331 const struct GNUNET_PeerIdentity *get_path,
2332 unsigned int get_path_length,
2333 const struct GNUNET_PeerIdentity *put_path,
2334 unsigned int put_path_length,
2335 enum GNUNET_BLOCK_Type type,
2336 size_t size, const void *data)
2338 struct ResolverHandle *rh;
2339 struct GNSNameRecordBlock *nrb;
2340 uint32_t num_records;
2342 char* rd_data = (char*) data;
2345 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
2346 struct GNUNET_HashCode zone_hash_double, name_hash_double;
2348 rh = (struct ResolverHandle *)cls;
2350 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2351 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2356 nrb = (struct GNSNameRecordBlock*)data;
2358 /* stop dht lookup and timeout task */
2359 GNUNET_DHT_get_stop (rh->get_handle);
2361 rh->get_handle = NULL;
2363 if (rh->dht_heap_node != NULL)
2365 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2366 rh->dht_heap_node = NULL;
2369 num_records = ntohl(nrb->rd_count);
2370 name = (char*)&nrb[1];
2372 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2373 struct NamestoreBGTask *ns_heap_root;
2374 struct NamestoreBGTask *namestore_bg_task;
2376 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2377 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2379 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2384 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2385 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2390 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2391 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2392 rh->id, name, rh->authority_name);
2393 for (i=0; i<num_records; i++)
2396 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2397 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2398 rh->id, name, rh->authority_name);
2399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2400 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2401 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2402 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2403 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2404 rh->id, rd[i].data_size);
2405 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2406 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2407 rh->id, rd[i].flags);
2409 if ((rd[i].record_type == GNUNET_GNS_RECORD_VPN) ||
2410 (rd[i].record_type == GNUNET_GNS_RECORD_NS) ||
2411 (rd[i].record_type == GNUNET_GNS_RECORD_CNAME))
2414 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2416 if (strcmp(rh->name, "") == 0)
2417 strcpy(rh->name, rh->authority_name);
2419 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2420 rh->name, rh->authority_name); //FIXME ret
2425 if ((strcmp(name, rh->authority_name) == 0) &&
2426 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
2428 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2429 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2432 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2433 struct AuthorityChain *auth =
2434 GNUNET_malloc(sizeof(struct AuthorityChain));
2435 auth->zone = rh->authority;
2436 memset(auth->name, 0, strlen(rh->authority_name)+1);
2437 strcpy(auth->name, rh->authority_name);
2438 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2439 rh->authority_chain_tail,
2442 /** try to import pkey if private key available */
2443 //if (rh->priv_key && is_canonical (rh->name))
2444 // process_discovered_authority(name, auth->zone,
2445 // rh->authority_chain_tail->zone,
2452 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2453 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2454 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2455 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2457 /* Save to namestore */
2458 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
2461 if (max_allowed_ns_tasks <=
2462 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2464 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2465 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2467 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2468 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
2472 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2474 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2476 GNUNET_TIME_absolute_get().abs_value);
2477 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2484 &on_namestore_delegation_put_result, //cont
2485 namestore_bg_task); //cls
2494 * FIXME in this case. should we ask namestore again?
2496 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2497 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2498 rh->id, rh->authority_name, rh->name);
2500 if (strcmp(rh->name, "") == 0)
2502 /* Start shortening */
2503 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2506 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2508 start_shorten (rh->authority_chain_tail,
2513 rh->proc = &handle_delegation_ns;
2515 /* Check for key revocation and delegate */
2516 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2519 GNUNET_GNS_RECORD_REV,
2520 &process_pkey_revocation_result_ns,
2527 * No pkey but name exists
2530 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2531 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2532 rh->id, rh->authority_name, rh->name);
2533 if (strcmp(rh->name, "") == 0)
2534 strcpy(rh->name, rh->authority_name);
2536 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2537 rh->name, rh->authority_name); //FIXME ret
2539 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2540 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2541 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2542 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2544 rh->proc(rh->proc_cls, rh, 0, NULL);
2547 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2548 +(MAX_DNS_NAME_LENGTH*2)
2549 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2550 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2554 expand_plus(char* dest, char* src, char* repl)
2557 unsigned int s_len = strlen(src)+1;
2559 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2560 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2564 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2565 "GNS_POSTPROCESS: %s to short\n", src);
2567 /* no postprocessing */
2568 memcpy(dest, src, s_len+1);
2572 if (0 == strcmp(src+s_len-3, ".+"))
2574 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2575 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2576 memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2580 pos += strlen(repl);
2581 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2582 "GNS_POSTPROCESS: Expanded to %s\n", dest);
2586 memcpy(dest, src, s_len+1);
2594 finish_lookup (struct ResolverHandle *rh,
2595 struct RecordLookupHandle* rlh,
2596 unsigned int rd_count,
2597 const struct GNUNET_NAMESTORE_RecordData *rd)
2600 char new_rr_data[MAX_DNS_NAME_LENGTH];
2601 char new_mx_data[MAX_MX_LENGTH];
2602 char new_soa_data[MAX_SOA_LENGTH];
2603 char new_srv_data[MAX_SRV_LENGTH];
2604 struct srv_data *old_srv;
2605 struct srv_data *new_srv;
2606 struct soa_data *old_soa;
2607 struct soa_data *new_soa;
2608 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2611 unsigned int offset;
2613 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2615 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2616 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2619 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2622 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2624 for (i = 0; i < rd_count; i++)
2627 if (rd[i].record_type != GNUNET_GNS_RECORD_NS &&
2628 rd[i].record_type != GNUNET_GNS_RECORD_PTR &&
2629 rd[i].record_type != GNUNET_GNS_RECORD_CNAME &&
2630 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
2631 rd[i].record_type != GNUNET_GNS_RECORD_SOA &&
2632 rd[i].record_type != GNUNET_GNS_RECORD_SRV)
2634 p_rd[i].data = rd[i].data;
2639 * for all those records we 'should'
2640 * also try to resolve the A/AAAA records (RFC1035)
2641 * This is a feature and not important
2644 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2645 "GNS_POSTPROCESS: Postprocessing\n");
2647 if (strcmp(rh->name, "+") == 0)
2648 repl_string = rlh->name;
2650 repl_string = rlh->name+strlen(rh->name)+1;
2653 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
2655 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2656 offset = sizeof(uint16_t);
2657 pos = new_mx_data+offset;
2658 expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2660 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2661 p_rd[i].data = new_mx_data;
2662 p_rd[i].data_size = offset;
2664 else if (rd[i].record_type == GNUNET_GNS_RECORD_SRV)
2667 * Prio, weight and port
2669 new_srv = (struct srv_data*)new_srv_data;
2670 old_srv = (struct srv_data*)rd[i].data;
2671 new_srv->prio = old_srv->prio;
2672 new_srv->weight = old_srv->weight;
2673 new_srv->port = old_srv->port;
2674 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2676 p_rd[i].data = new_srv_data;
2677 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2679 else if (rd[i].record_type == GNUNET_GNS_RECORD_SOA)
2681 /* expand mname and rname */
2682 old_soa = (struct soa_data*)rd[i].data;
2683 new_soa = (struct soa_data*)new_soa_data;
2684 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2685 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2686 offset = strlen ((char*)&new_soa[1]) + 1;
2687 expand_plus((char*)&new_soa[1] + offset,
2688 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2690 p_rd[i].data_size = sizeof (struct soa_data)
2692 + strlen ((char*)&new_soa[1] + offset);
2693 p_rd[i].data = new_soa_data;
2698 expand_plus(pos, (char*)rd[i].data, repl_string);
2699 p_rd[i].data_size = strlen(new_rr_data)+1;
2700 p_rd[i].data = new_rr_data;
2705 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2707 free_resolver_handle (rh);
2711 * Process DHT lookup result for record.
2713 * @param cls the closure
2714 * @param rh resolver handle
2715 * @param rd_count number of results
2716 * @param rd record data
2719 handle_record_dht(void* cls, struct ResolverHandle *rh,
2720 unsigned int rd_count,
2721 const struct GNUNET_NAMESTORE_RecordData *rd)
2723 struct RecordLookupHandle* rlh;
2725 rlh = (struct RecordLookupHandle*)cls;
2728 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2729 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2731 /* give up, cannot resolve */
2732 finish_lookup(rh, rlh, 0, NULL);
2736 /* results found yay */
2737 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2738 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2740 finish_lookup(rh, rlh, rd_count, rd);
2748 * Process namestore lookup result for record.
2750 * @param cls the closure
2751 * @param rh resolver handle
2752 * @param rd_count number of results
2753 * @param rd record data
2756 handle_record_ns (void* cls, struct ResolverHandle *rh,
2757 unsigned int rd_count,
2758 const struct GNUNET_NAMESTORE_RecordData *rd)
2760 struct RecordLookupHandle* rlh;
2761 rlh = (struct RecordLookupHandle*) cls;
2762 int check_dht = GNUNET_YES;
2766 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2767 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2772 * There are 5 conditions that have to met for us to consult the DHT:
2773 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2774 * 2. No entry in the NS existed AND
2775 * 3. The zone queried is not the local resolver's zone AND
2776 * 4. The name that was looked up is '+'
2777 * because if it was any other canonical name we either already queried
2778 * the DHT for the authority in the authority lookup phase (and thus
2779 * would already have an entry in the NS for the record)
2780 * 5. We are not in cache only mode
2782 if (((rh->status & RSL_RECORD_EXPIRED) != 0) &&
2783 ((rh->status & RSL_RECORD_EXISTS) == 0) )
2784 check_dht = GNUNET_NO;
2786 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2787 &rh->private_local_zone))
2788 check_dht = GNUNET_NO;
2790 if ((strcmp (rh->name, "+") != 0) && (is_srv (rh->name) != 0))
2791 check_dht = GNUNET_NO;
2794 if (rh->only_cached == GNUNET_YES)
2795 check_dht = GNUNET_NO;
2797 if (GNUNET_YES == check_dht)
2799 rh->proc = &handle_record_dht;
2800 resolve_record_dht(rh);
2803 /* give up, cannot resolve */
2804 finish_lookup(rh, rlh, 0, NULL);
2808 /* results found yay */
2809 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2810 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2812 finish_lookup(rh, rlh, rd_count, rd);
2819 * Move one level up in the domain hierarchy and return the
2820 * passed top level domain.
2822 * @param name the domain
2823 * @param dest the destination where the tld will be put
2826 pop_tld(char* name, char* dest)
2830 if (is_canonical (name))
2837 for (len = strlen(name); len > 0; len--)
2839 if (*(name+len) == '.')
2849 strcpy(dest, (name+len+1));
2853 * Checks if name is in tld
2855 * @param name the name to check
2856 * @param tld the TLD to check for
2857 * @return GNUNET_YES or GNUNET_NO
2860 is_tld(const char* name, const char* tld)
2864 if (strlen(name) <= strlen(tld))
2869 offset = strlen(name)-strlen(tld);
2870 if (strcmp(name+offset, tld) != 0)
2872 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2873 "%s is not in .%s TLD\n", name, tld);
2880 * DHT resolution for delegation finished. Processing result.
2882 * @param cls the closure
2883 * @param rh resolver handle
2884 * @param rd_count number of results (always 0)
2885 * @param rd record data (always NULL)
2888 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2889 unsigned int rd_count,
2890 const struct GNUNET_NAMESTORE_RecordData *rd)
2892 struct RecordLookupHandle* rlh;
2893 rlh = (struct RecordLookupHandle*) cls;
2896 if (strcmp(rh->name, "") == 0)
2898 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
2900 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2901 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2903 finish_lookup(rh, rlh, rd_count, rd);
2906 /* We resolved full name for delegation. resolving record */
2907 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2908 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2910 strcpy(rh->name, "+\0");
2911 rh->proc = &handle_record_ns;
2912 resolve_record_ns(rh);
2917 * we still have some left
2919 if (is_canonical(rh->name))
2921 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2922 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2925 rh->proc = &handle_record_ns;
2926 resolve_record_ns(rh);
2929 /* give up, cannot resolve */
2930 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2931 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2933 finish_lookup(rh, rlh, 0, NULL);
2938 * Start DHT lookup for a name -> PKEY (compare NS) record in
2939 * rh->authority's zone
2941 * @param rh the pending gns query
2944 resolve_delegation_dht(struct ResolverHandle *rh)
2947 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2948 struct GNUNET_HashCode name_hash_double;
2949 struct GNUNET_HashCode zone_hash_double;
2950 struct GNUNET_HashCode lookup_key;
2951 struct ResolverHandle *rh_heap_root;
2953 pop_tld(rh->name, rh->authority_name);
2954 GNUNET_CRYPTO_short_hash(rh->authority_name,
2955 strlen(rh->authority_name),
2957 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2958 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2959 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2961 rh->dht_heap_node = NULL;
2963 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2965 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
2966 // &dht_authority_lookup_timeout,
2968 rh->timeout_cont = &dht_authority_lookup_timeout;
2969 rh->timeout_cont_cls = rh;
2973 if (max_allowed_background_queries <=
2974 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2976 /* terminate oldest lookup */
2977 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2978 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2979 rh_heap_root->get_handle = NULL;
2980 rh_heap_root->dht_heap_node = NULL;
2982 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2983 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2984 rh->id, rh_heap_root->authority_name);
2986 rh_heap_root->proc(rh_heap_root->proc_cls,
2991 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2993 GNUNET_TIME_absolute_get().abs_value);
2996 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2998 GNUNET_assert(rh->get_handle == NULL);
2999 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
3000 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
3002 DHT_GNS_REPLICATION_LEVEL,
3006 &process_delegation_result_dht,
3013 * Namestore resolution for delegation finished. Processing result.
3015 * @param cls the closure
3016 * @param rh resolver handle
3017 * @param rd_count number of results (always 0)
3018 * @param rd record data (always NULL)
3021 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
3022 unsigned int rd_count,
3023 const struct GNUNET_NAMESTORE_RecordData *rd)
3025 struct RecordLookupHandle* rlh;
3026 rlh = (struct RecordLookupHandle*) cls;
3027 int check_dht = GNUNET_YES;
3030 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3031 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
3032 rh->id, rh->status);
3034 if (rh->status & RSL_PKEY_REVOKED)
3036 finish_lookup (rh, rlh, 0, NULL);
3040 if (strcmp(rh->name, "") == 0)
3043 /* We resolved full name for delegation. resolving record */
3044 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3045 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
3047 if (rh->status & RSL_CNAME_FOUND)
3049 if (rlh->record_type == GNUNET_GNS_RECORD_CNAME)
3051 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3052 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
3054 strcpy (rh->name, rh->authority_name);
3055 finish_lookup (rh, rlh, rd_count, rd);
3060 if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
3062 s_len = strlen (rd->data) - 2;
3063 memcpy (rh->name, rd->data, s_len);
3064 rh->name[s_len] = '\0';
3065 resolve_delegation_ns (rh);
3068 else if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
3070 gns_resolver_lookup_record (rh->authority,
3071 rh->private_local_zone,
3080 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3081 free_resolver_handle (rh);
3087 strcpy (rh->dns_name, (char*)rd->data);
3088 resolve_dns_name (rh);
3093 else if (rh->status & RSL_DELEGATE_VPN)
3095 if (rlh->record_type == GNUNET_GNS_RECORD_VPN)
3097 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3098 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
3100 finish_lookup(rh, rlh, rd_count, rd);
3103 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3104 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
3106 GNUNET_assert (NULL != rd);
3107 rh->proc = &handle_record_vpn;
3108 resolve_record_vpn (rh, rd_count, rd);
3111 else if (rh->status & RSL_DELEGATE_NS)
3113 if (rlh->record_type == GNUNET_GNS_RECORD_NS)
3115 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3116 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3118 finish_lookup(rh, rlh, rd_count, rd);
3122 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3123 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3125 GNUNET_assert (NULL != rd);
3126 rh->proc = &handle_record_ns;
3127 resolve_record_dns (rh, rd_count, rd);
3130 else if (rh->status & RSL_DELEGATE_PKEY)
3132 if (rh->status & RSL_PKEY_REVOKED)
3134 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3135 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
3137 finish_lookup (rh, rlh, 0, NULL);
3140 else if (rlh->record_type == GNUNET_GNS_RECORD_PKEY)
3142 GNUNET_assert(rd_count == 1);
3143 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3144 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
3146 finish_lookup(rh, rlh, rd_count, rd);
3150 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3151 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3153 strcpy(rh->name, "+\0");
3154 rh->proc = &handle_record_ns;
3155 resolve_record_ns(rh);
3159 if (rh->status & RSL_DELEGATE_NS)
3161 if (rlh->record_type == GNUNET_GNS_RECORD_NS)
3163 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3164 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3166 finish_lookup(rh, rlh, rd_count, rd);
3170 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3171 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3173 GNUNET_assert (NULL != rd);
3174 rh->proc = &handle_record_ns;
3175 resolve_record_dns (rh, rd_count, rd);
3180 * we still have some left
3181 * check if authority in ns is fresh
3183 * or we are authority
3186 if ((rh->status & RSL_RECORD_EXISTS) &&
3187 !(rh->status & RSL_RECORD_EXPIRED))
3188 check_dht = GNUNET_NO;
3190 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3191 &rh->private_local_zone))
3192 check_dht = GNUNET_NO;
3194 if (rh->only_cached == GNUNET_YES)
3195 check_dht = GNUNET_NO;
3197 if (check_dht == GNUNET_NO)
3199 if (is_canonical(rh->name))
3201 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3202 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3205 rh->proc = &handle_record_ns;
3206 resolve_record_ns(rh);
3210 /* give up, cannot resolve */
3211 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3212 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3215 finish_lookup(rh, rlh, rd_count, rd);
3216 //rlh->proc(rlh->proc_cls, 0, NULL);
3221 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3222 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3224 rh->proc = &handle_delegation_dht;
3225 resolve_delegation_dht(rh);
3230 * This is a callback function that should give us only PKEY
3231 * records. Used to query the namestore for the authority (PKEY)
3232 * for 'name'. It will recursively try to resolve the
3233 * authority for a given name from the namestore.
3235 * @param cls the pending query
3236 * @param key the key of the zone we did the lookup
3237 * @param expiration expiration date of the record data set in the namestore
3238 * @param name the name for which we need an authority
3239 * @param rd_count the number of records with 'name'
3240 * @param rd the record data
3241 * @param signature the signature of the authority for the record data
3244 process_delegation_result_ns (void* cls,
3245 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3246 struct GNUNET_TIME_Absolute expiration,
3248 unsigned int rd_count,
3249 const struct GNUNET_NAMESTORE_RecordData *rd,
3250 const struct GNUNET_CRYPTO_RsaSignature *signature)
3252 struct ResolverHandle *rh;
3253 struct GNUNET_TIME_Relative remaining_time;
3254 struct GNUNET_CRYPTO_ShortHashCode zone;
3255 char new_name[MAX_DNS_NAME_LENGTH];
3257 struct GNUNET_TIME_Absolute et;
3259 rh = (struct ResolverHandle *)cls;
3260 rh->namestore_task = NULL;
3261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3262 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3265 GNUNET_CRYPTO_short_hash (key,
3266 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3268 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3275 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3277 rh->status |= RSL_RECORD_EXISTS;
3279 if (remaining_time.rel_value == 0)
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3282 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3284 rh->status |= RSL_RECORD_EXPIRED;
3289 * No authority found in namestore.
3294 * We did not find an authority in the namestore
3299 * Promote this authority back to a name maybe it is
3302 if (strcmp (rh->name, "") == 0)
3304 /* simply promote back */
3305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3306 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3307 rh->id, rh->authority_name);
3308 strcpy (rh->name, rh->authority_name);
3312 /* add back to existing name */
3313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3314 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3315 rh->id, rh->authority_name, rh->name);
3316 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3317 rh->name, rh->authority_name);
3318 strcpy (rh->name, new_name);
3319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3320 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3324 rh->proc (rh->proc_cls, rh, 0, NULL);
3329 * We found an authority that may be able to help us
3330 * move on with query
3331 * Note only 1 pkey should have been returned.. anything else would be strange
3333 for (i=0; i < rd_count;i++)
3337 * A CNAME. Like regular DNS this means the is no other record for this
3340 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3342 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3343 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3346 rh->status |= RSL_CNAME_FOUND;
3347 rh->proc (rh->proc_cls, rh, rd_count, rd);
3354 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3356 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3357 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3359 rh->status |= RSL_DELEGATE_VPN;
3360 rh->proc (rh->proc_cls, rh, rd_count, rd);
3366 * FIXME make optional
3368 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3370 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3371 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3373 rh->status |= RSL_DELEGATE_NS;
3374 rh->proc (rh->proc_cls, rh, rd_count, rd);
3378 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3381 rh->status |= RSL_DELEGATE_PKEY;
3383 if ((ignore_pending_records != 0) &&
3384 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3387 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3393 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3394 et.abs_value = rd[i].expiration_time;
3395 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3399 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3401 if (remaining_time.rel_value == 0)
3403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3404 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3406 rh->authority_chain_head->fresh = 0;
3407 rh->proc (rh->proc_cls, rh, 0, NULL);
3415 * Resolve rest of query with new authority
3417 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3418 memcpy (&rh->authority, rd[i].data,
3419 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3420 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3421 auth->zone = rh->authority;
3422 memset (auth->name, 0, strlen (rh->authority_name)+1);
3423 strcpy (auth->name, rh->authority_name);
3424 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3425 rh->authority_chain_tail,
3428 /* Check for key revocation and delegate */
3429 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3432 GNUNET_GNS_RECORD_REV,
3433 &process_pkey_revocation_result_ns,
3442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3443 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3445 * If we have found some records for the LAST label
3446 * we return the results. Else null.
3448 if (strcmp(rh->name, "") == 0)
3450 /* Start shortening */
3451 if ((rh->priv_key != NULL) && is_canonical (rh->name))
3453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3454 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3456 start_shorten (rh->authority_chain_tail,
3459 /* simply promote back */
3460 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3461 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3462 rh->id, rh->authority_name);
3463 strcpy(rh->name, rh->authority_name);
3464 rh->proc(rh->proc_cls, rh, rd_count, rd);
3468 rh->proc(rh->proc_cls, rh, 0, NULL);
3474 * Resolve the delegation chain for the request in our namestore
3476 * @param rh the resolver handle
3479 resolve_delegation_ns (struct ResolverHandle *rh)
3481 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3482 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3484 pop_tld(rh->name, rh->authority_name);
3485 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3488 GNUNET_GNS_RECORD_ANY,
3489 &process_delegation_result_ns,
3496 * Lookup of a record in a specific zone
3497 * calls lookup result processor on result
3499 * @param zone the root zone
3500 * @param pzone the private local zone
3501 * @param record_type the record type to look up
3502 * @param name the name to look up
3503 * @param key a private key for use with PSEU import (can be NULL)
3504 * @param timeout timeout for resolution
3505 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3506 * @param proc the processor to call on result
3507 * @param cls the closure to pass to proc
3510 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3511 struct GNUNET_CRYPTO_ShortHashCode pzone,
3512 uint32_t record_type,
3514 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3515 struct GNUNET_TIME_Relative timeout,
3517 RecordLookupProcessor proc,
3520 struct ResolverHandle *rh;
3521 struct RecordLookupHandle* rlh;
3522 char string_hash[MAX_DNS_LABEL_LENGTH];
3523 char nzkey[MAX_DNS_LABEL_LENGTH];
3524 char* nzkey_ptr = nzkey;
3526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3527 "Starting resolution for %s (type=%d)!\n",
3531 if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
3533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3534 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3539 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3540 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3542 memset (rh, 0, sizeof (struct ResolverHandle));
3543 rh->authority = zone;
3547 rh->timeout = timeout;
3548 rh->get_handle = NULL;
3549 rh->private_local_zone = pzone;
3550 rh->only_cached = only_cached;
3551 rh->namestore_task = NULL;
3553 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3558 "No shorten key for resolution\n");
3561 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3564 * Set timeout for authority lookup phase to 1/2
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3567 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3568 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3569 GNUNET_TIME_relative_divide(timeout, 2),
3570 &handle_lookup_timeout,
3572 rh->timeout_cont = &dht_authority_lookup_timeout;
3573 rh->timeout_cont_cls = rh;
3577 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3578 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3581 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3584 * Only 'gnunet' given
3586 strcpy(rh->name, "\0");
3590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3591 "Checking for TLD...\n");
3592 if (is_zkey_tld(name) == GNUNET_YES)
3594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3597 * This is a zkey tld
3598 * build hash and use as initial authority
3601 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3602 memcpy(rh->name, name,
3603 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3604 pop_tld(rh->name, string_hash);
3606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3607 "ZKEY is %s!\n", string_hash);
3609 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3611 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3614 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3615 "Cannot convert ZKEY %s to hash!\n", string_hash);
3617 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3620 proc (cls, 0, NULL);
3627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3630 * Presumably GNUNET tld
3632 memset (rh->name, 0,
3633 strlen(name)-strlen(GNUNET_GNS_TLD));
3634 memcpy (rh->name, name,
3635 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3640 * Initialize authority chain
3642 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3643 rh->authority_chain_head->prev = NULL;
3644 rh->authority_chain_head->next = NULL;
3645 rh->authority_chain_tail = rh->authority_chain_head;
3646 rh->authority_chain_head->zone = rh->authority;
3647 strcpy (rh->authority_chain_head->name, "");
3650 * Copy original query into lookup handle
3652 rlh->record_type = record_type;
3653 memset(rlh->name, 0, strlen(name) + 1);
3654 strcpy(rlh->name, name);
3656 rlh->proc_cls = cls;
3658 rh->proc = &handle_delegation_ns;
3659 resolve_delegation_ns(rh);
3662 /******** END Record Resolver ***********/
3665 finish_shorten (struct ResolverHandle *rh,
3666 struct NameShortenHandle *nsh)
3668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3669 "Sending %s as shorten result\n", nsh->result);
3670 nsh->proc (nsh->proc_cls, nsh->result);
3671 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3673 free_resolver_handle (rh);
3678 * Callback calles by namestore for a zone to name
3681 * @param cls the closure
3682 * @param zone_key the zone we queried
3683 * @param expire the expiration time of the name
3684 * @param name the name found or NULL
3685 * @param rd_len number of records for the name
3686 * @param rd the record data (PKEY) for the name
3687 * @param signature the signature for the record data
3690 process_zone_to_name_shorten_root (void *cls,
3691 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3692 struct GNUNET_TIME_Absolute expire,
3694 unsigned int rd_len,
3695 const struct GNUNET_NAMESTORE_RecordData *rd,
3696 const struct GNUNET_CRYPTO_RsaSignature *signature);
3700 * Callback called by namestore for a zone to name
3703 * @param cls the closure
3704 * @param zone_key the zone we queried
3705 * @param expire the expiration time of the name
3706 * @param name the name found or NULL
3707 * @param rd_len number of records for the name
3708 * @param rd the record data (PKEY) for the name
3709 * @param signature the signature for the record data
3712 process_zone_to_name_shorten_shorten (void *cls,
3713 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3714 struct GNUNET_TIME_Absolute expire,
3716 unsigned int rd_len,
3717 const struct GNUNET_NAMESTORE_RecordData *rd,
3718 const struct GNUNET_CRYPTO_RsaSignature *signature)
3720 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3721 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3722 struct AuthorityChain *next_authority;
3724 char result[MAX_DNS_NAME_LENGTH];
3725 char tmp_name[MAX_DNS_NAME_LENGTH];
3728 rh->namestore_task = NULL;
3729 /* we found a match in our own root zone */
3732 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3733 memset(result, 0, answer_len);
3735 if (strlen(rh->name) > 0)
3737 sprintf (result, "%s.%s.%s.%s",
3739 nsh->shorten_zone_name,
3744 sprintf (result, "%s.%s.%s", name,
3745 nsh->shorten_zone_name,
3749 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3750 "Found shorten result %s\n", result);
3751 if (strlen (nsh->result) > strlen (result))
3752 strcpy (nsh->result, result);
3754 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3755 nsh->shorten_zone) == 0)
3758 * This is our zone append .gnunet unless name is empty
3759 * (it shouldn't be, usually FIXME what happens if we
3760 * shorten to our zone to a "" record??)
3763 sprintf (result, "%s.%s.%s",
3765 nsh->shorten_zone_name,
3767 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3768 "Our zone: Found %s as shorten result\n", result);
3770 if (strlen (nsh->result) > strlen (result))
3771 strcpy (nsh->result, result);
3772 //nsh->proc(nsh->proc_cls, result);
3774 //free_resolver_handle(rh);
3781 * continue with next authority if exists
3783 if ((rh->authority_chain_head->next == NULL))
3785 finish_shorten (rh, nsh);
3788 next_authority = rh->authority_chain_head;
3790 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3791 "%s.%s", rh->name, next_authority->name);
3793 strcpy(rh->name, tmp_name);
3794 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3795 "No PSEU found for authority %s. Promoting back: %s\n",
3796 next_authority->name, rh->name);
3798 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3799 rh->authority_chain_tail,
3802 GNUNET_free (next_authority);
3804 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3805 &rh->authority_chain_tail->zone,
3806 &rh->authority_chain_head->zone,
3807 &process_zone_to_name_shorten_root,
3812 * Callback calles by namestore for a zone to name
3815 * @param cls the closure
3816 * @param zone_key the zone we queried
3817 * @param expire the expiration time of the name
3818 * @param name the name found or NULL
3819 * @param rd_len number of records for the name
3820 * @param rd the record data (PKEY) for the name
3821 * @param signature the signature for the record data
3824 process_zone_to_name_shorten_private (void *cls,
3825 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3826 struct GNUNET_TIME_Absolute expire,
3828 unsigned int rd_len,
3829 const struct GNUNET_NAMESTORE_RecordData *rd,
3830 const struct GNUNET_CRYPTO_RsaSignature *signature)
3832 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3833 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3834 struct AuthorityChain *next_authority;
3836 char result[MAX_DNS_NAME_LENGTH];
3837 char tmp_name[MAX_DNS_NAME_LENGTH];
3840 rh->namestore_task = NULL;
3841 /* we found a match in our own root zone */
3844 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3845 memset(result, 0, answer_len);
3847 if (strlen(rh->name) > 0)
3849 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3853 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3856 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3857 "Found shorten result %s\n", result);
3858 if (strlen (nsh->result) > strlen (result))
3859 strcpy (nsh->result, result);
3861 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3862 nsh->private_zone) == 0)
3865 * This is our zone append .gnunet unless name is empty
3866 * (it shouldn't be, usually FIXME what happens if we
3867 * shorten to our zone to a "" record??)
3870 sprintf (result, "%s.%s.%s",
3871 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3872 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3873 "Our private zone: Found %s as shorten result %s\n", result);
3874 if (strlen (nsh->result) > strlen (result))
3875 strcpy (nsh->result, result);
3878 if (0 != strcmp (nsh->shorten_zone_name, ""))
3880 /* backtrack authorities for names in priv zone */
3881 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3883 &rh->authority_chain_head->zone,
3884 &process_zone_to_name_shorten_shorten,
3891 * continue with next authority if exists
3893 if ((rh->authority_chain_head->next == NULL))
3895 finish_shorten (rh, nsh);
3898 next_authority = rh->authority_chain_head;
3900 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3901 "%s.%s", rh->name, next_authority->name);
3903 strcpy(rh->name, tmp_name);
3904 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3905 "No PSEU found for authority %s. Promoting back: %s\n",
3906 next_authority->name, rh->name);
3908 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3909 rh->authority_chain_tail,
3912 GNUNET_free (next_authority);
3914 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3915 &rh->authority_chain_tail->zone,
3916 &rh->authority_chain_head->zone,
3917 &process_zone_to_name_shorten_root,
3923 * Callback calles by namestore for a zone to name
3926 * @param cls the closure
3927 * @param zone_key the zone we queried
3928 * @param expire the expiration time of the name
3929 * @param name the name found or NULL
3930 * @param rd_len number of records for the name
3931 * @param rd the record data (PKEY) for the name
3932 * @param signature the signature for the record data
3935 process_zone_to_name_shorten_root (void *cls,
3936 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3937 struct GNUNET_TIME_Absolute expire,
3939 unsigned int rd_len,
3940 const struct GNUNET_NAMESTORE_RecordData *rd,
3941 const struct GNUNET_CRYPTO_RsaSignature *signature)
3943 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3944 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3945 struct AuthorityChain *next_authority;
3947 char result[MAX_DNS_NAME_LENGTH];
3948 char tmp_name[MAX_DNS_NAME_LENGTH];
3951 rh->namestore_task = NULL;
3952 /* we found a match in our own root zone */
3955 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3956 memset(result, 0, answer_len);
3958 if (strlen(rh->name) > 0)
3960 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3964 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3967 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3968 "Found shorten result %s\n", result);
3969 if (strlen (nsh->result) > strlen (result))
3970 strcpy (nsh->result, result);
3972 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3973 nsh->root_zone) == 0)
3976 * This is our zone append .gnunet unless name is empty
3977 * (it shouldn't be, usually FIXME what happens if we
3978 * shorten to our zone to a "" record??)
3981 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3982 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3983 "Our zone: Found %s as shorten result\n", result);
3984 if (strlen (nsh->result) > strlen (result))
3985 strcpy (nsh->result, result);
3988 if (NULL != nsh->private_zone)
3990 /* backtrack authorities for names in priv zone */
3991 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3993 &rh->authority_chain_head->zone,
3994 &process_zone_to_name_shorten_private,
3997 else if (NULL != nsh->shorten_zone)
3999 /* backtrack authorities for names in shorten zone */
4000 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4002 &rh->authority_chain_head->zone,
4003 &process_zone_to_name_shorten_shorten,
4010 * continue with next authority if exists
4012 if ((rh->authority_chain_head->next == NULL))
4014 finish_shorten (rh, nsh);
4017 next_authority = rh->authority_chain_head;
4019 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
4020 "%s.%s", rh->name, next_authority->name);
4022 strcpy(rh->name, tmp_name);
4023 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4024 "No PSEU found for authority %s. Promoting back: %s\n",
4025 next_authority->name, rh->name);
4027 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
4028 rh->authority_chain_tail,
4031 GNUNET_free (next_authority);
4033 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4034 &rh->authority_chain_tail->zone,
4035 &rh->authority_chain_head->zone,
4036 &process_zone_to_name_shorten_root,
4043 * Process result from namestore delegation lookup
4044 * for shorten operation
4046 * @param cls the client shorten handle
4047 * @param rh the resolver handle
4048 * @param rd_count number of results (0)
4049 * @param rd data (NULL)
4052 handle_delegation_ns_shorten (void* cls,
4053 struct ResolverHandle *rh,
4055 const struct GNUNET_NAMESTORE_RecordData *rd)
4057 struct NameShortenHandle *nsh;
4058 char result[MAX_DNS_NAME_LENGTH];
4060 nsh = (struct NameShortenHandle *)cls;
4063 * At this point rh->name contains the part of the name
4064 * that we do not have a PKEY in our namestore to resolve.
4065 * The authority chain in the resolver handle is now
4066 * useful to backtrack if needed
4069 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4070 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4071 memset(result, 0, sizeof (result));
4073 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4074 nsh->root_zone) == 0)
4077 * This is our zone append .gnunet unless name is empty
4078 * (it shouldn't be, usually FIXME what happens if we
4079 * shorten to our zone to a "" record??)
4082 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
4083 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4084 "Our zone: Found %s as shorten result\n", result);
4086 if (strlen (nsh->result) > strlen (result))
4087 strcpy (nsh->result, result);
4090 else if (NULL != nsh->private_zone)
4093 * This is our zone append .gnunet unless name is empty
4094 * (it shouldn't be, usually FIXME what happens if we
4095 * shorten to our zone to a "" record??)
4097 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4098 nsh->private_zone) == 0)
4101 sprintf (result, "%s.%s.%s",
4102 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4103 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4104 "Our zone: Found %s as shorten result in private zone %s\n",
4107 if (strlen (nsh->result) > strlen (result))
4108 strcpy (nsh->result, result);
4111 else if (NULL != nsh->shorten_zone)
4114 * This is our zone append .gnunet unless name is empty
4115 * (it shouldn't be, usually FIXME what happens if we
4116 * shorten to our zone to a "" record??)
4118 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4119 nsh->shorten_zone) == 0)
4121 sprintf (result, "%s.%s.%s",
4122 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4123 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4124 "Our zone: Found %s as shorten result in shorten zone\n",
4127 if (strlen (nsh->result) > strlen (result))
4128 strcpy (nsh->result, result);
4133 /* backtrack authorities for names */
4134 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4136 &rh->authority_chain_head->zone,
4137 &process_zone_to_name_shorten_root,
4144 * Callback calles by namestore for a zone to name
4147 * @param cls the closure
4148 * @param zone_key the zone we queried
4149 * @param expire the expiration time of the name
4150 * @param name the name found or NULL
4151 * @param rd_len number of records for the name
4152 * @param rd the record data (PKEY) for the name
4153 * @param signature the signature for the record data
4156 process_zone_to_name_zkey(void *cls,
4157 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4158 struct GNUNET_TIME_Absolute expire,
4160 unsigned int rd_len,
4161 const struct GNUNET_NAMESTORE_RecordData *rd,
4162 const struct GNUNET_CRYPTO_RsaSignature *signature)
4164 struct ResolverHandle *rh = cls;
4165 struct NameShortenHandle *nsh = rh->proc_cls;
4166 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
4167 char new_name[MAX_DNS_NAME_LENGTH];
4169 /* zkey not in our zone */
4173 * In this case we have not given this PKEY a name (yet)
4174 * It is either just not in our zone or not even cached
4175 * Since we do not know at this point we will not try to shorten
4176 * because PKEY import will happen if the user follows the zkey
4179 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
4181 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4182 "No name found for zkey %s returning verbatim!\n", enc);
4183 if (strcmp(rh->name, "") != 0)
4184 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4185 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4187 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4188 enc, GNUNET_GNS_TLD_ZKEY);
4190 strcpy (nsh->result, new_name);
4192 finish_shorten (rh, nsh);
4196 if (strcmp(rh->name, "") != 0)
4197 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4200 strcpy(new_name, name);
4202 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4203 "Continue shorten for %s!\n", new_name);
4205 strcpy(rh->name, new_name);
4207 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4208 rh->authority_chain_tail = rh->authority_chain_head;
4209 rh->authority_chain_head->zone = rh->authority;
4212 /* Start delegation resolution in our namestore */
4213 resolve_delegation_ns (rh);
4218 * Shorten api from resolver
4220 * @param zone the root zone to use
4221 * @param pzone the private zone to use
4222 * @param szone the shorten zone to use
4223 * @param name the name to shorten
4224 * @param private_zone_name name of the private zone
4225 * @param shorten_zone_name name of the shorten zone
4226 * @param proc the processor to call with result
4227 * @param proc_cls closure to pass to proc
4230 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4231 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4232 struct GNUNET_CRYPTO_ShortHashCode *szone,
4234 const char* private_zone_name,
4235 const char* shorten_zone_name,
4236 ShortenResultProcessor proc,
4239 struct ResolverHandle *rh;
4240 struct NameShortenHandle *nsh;
4241 char string_hash[MAX_DNS_LABEL_LENGTH];
4242 struct GNUNET_CRYPTO_ShortHashCode zkey;
4243 char nzkey[MAX_DNS_LABEL_LENGTH];
4244 char* nzkey_ptr = nzkey;
4247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4248 "Starting shorten for %s!\n", name);
4250 if (is_canonical ((char*)name))
4252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4253 "%s is canonical. Returning verbatim\n", name);
4254 proc (proc_cls, name);
4258 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4261 nsh->proc_cls = proc_cls;
4262 nsh->root_zone = zone;
4263 nsh->private_zone = pzone;
4264 nsh->shorten_zone = szone;
4265 strcpy (nsh->private_zone_name, private_zone_name);
4266 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4267 strcpy (nsh->result, name);
4270 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4271 rh->authority = *zone;
4273 rh->priv_key = NULL;
4274 rh->namestore_task = NULL;
4275 rh->proc = &handle_delegation_ns_shorten;
4278 rh->private_local_zone = *zone;
4280 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4283 "Checking for TLD...\n");
4284 if (is_zkey_tld (name) == GNUNET_YES)
4286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4289 * This is a zkey tld
4290 * build hash and use as initial authority
4293 memset (rh->name, 0,
4294 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4295 memcpy (rh->name, name,
4296 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4297 pop_tld (rh->name, string_hash);
4299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4300 "ZKEY is %s!\n", string_hash);
4302 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4304 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4308 "Cannot convert ZKEY %s to hash!\n", nzkey);
4311 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4312 proc (proc_cls, name);
4316 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4319 &process_zone_to_name_zkey,
4326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4329 * Presumably GNUNET tld
4331 memset (rh->name, 0,
4332 strlen (name)-strlen (GNUNET_GNS_TLD));
4333 memcpy (rh->name, name,
4334 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4337 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4338 rh->authority_chain_tail = rh->authority_chain_head;
4339 rh->authority_chain_head->zone = *zone;
4342 /* Start delegation resolution in our namestore */
4343 resolve_delegation_ns (rh);
4346 /*********** END NAME SHORTEN ********************/
4349 * Conclude get authority lookup
4351 * @param rh resolver handle
4352 * @param nah get authority lookup handle
4355 finish_get_auth (struct ResolverHandle *rh,
4356 struct GetNameAuthorityHandle *nah)
4358 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4359 "Got authority result %s\n", nah->result);
4361 nah->proc(nah->proc_cls, nah->result);
4362 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4364 free_resolver_handle(rh);
4368 * Process result from namestore delegation lookup
4369 * for get authority operation
4371 * @param cls the client get auth handle
4372 * @param rh the resolver handle
4373 * @param rd_count number of results (0)
4374 * @param rd data (NULL)
4377 handle_delegation_result_ns_get_auth(void* cls,
4378 struct ResolverHandle *rh,
4380 const struct GNUNET_NAMESTORE_RecordData *rd)
4382 struct GetNameAuthorityHandle* nah;
4385 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4388 * At this point rh->name contains the part of the name
4389 * that we do not have a PKEY in our namestore to resolve.
4390 * The authority chain in the resolver handle is now
4391 * useful to backtrack if needed
4394 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4395 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4397 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4398 "Building response!\n");
4399 if (is_canonical(rh->name))
4402 * We successfully resolved the authority in the ns
4403 * FIXME for our purposes this is fine
4404 * but maybe we want to have an api that also looks
4405 * into the dht (i.e. option in message)
4407 if (strlen(rh->name) > strlen(nah->name))
4409 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4410 "Record name longer than original lookup name... odd!\n");
4414 answer_len = strlen(nah->name) - strlen(rh->name)
4415 + strlen(GNUNET_GNS_TLD) + 1;
4416 memset(nah->result, 0, answer_len);
4417 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4419 finish_get_auth (rh, nah);
4423 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4424 "Unable to resolve authority for remaining %s!\n", rh->name);
4425 strcpy(nah->result, "");
4426 finish_get_auth (rh, nah);
4434 * Tries to resolve the authority for name
4437 * @param zone the root zone to look up for
4438 * @param pzone the private local zone
4439 * @param name the name to lookup up
4440 * @param proc the processor to call when finished
4441 * @param proc_cls the closure to pass to the processor
4444 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4445 struct GNUNET_CRYPTO_ShortHashCode pzone,
4447 GetAuthorityResultProcessor proc,
4450 struct ResolverHandle *rh;
4451 struct GetNameAuthorityHandle *nah;
4453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4454 "Starting authority resolution for %s!\n", name);
4456 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4457 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4458 rh->authority = zone;
4460 rh->private_local_zone = pzone;
4461 rh->namestore_task = NULL;
4463 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4465 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4467 strcpy(rh->name, "\0");
4472 strlen(name)-strlen(GNUNET_GNS_TLD));
4473 memcpy(rh->name, name,
4474 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4477 memset(nah->name, 0,
4479 strcpy(nah->name, name);
4481 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4482 rh->authority_chain_tail = rh->authority_chain_head;
4483 rh->authority_chain_head->zone = zone;
4484 rh->proc = &handle_delegation_result_ns_get_auth;
4485 rh->proc_cls = (void*)nah;
4488 nah->proc_cls = proc_cls;
4489 strcpy (nah->result, "");
4491 /* Start delegation resolution in our namestore */
4492 resolve_delegation_ns(rh);
4496 /******** END GET AUTHORITY *************/
4498 /* end of gnunet-service-gns_resolver.c */