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);
183 tok = strtok (ndup, ".");
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-%d: dht lookup for query %s (%ds)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-%d: 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-%d: 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-%d: 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-%d: Got name: %s (wanted %s)\n",
1201 rh->id, name, rh->name);
1202 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1203 "GNS_PHASE_REC-%d: 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-%d: Got data length: %d\n",
1207 rh->id, rd[i].data_size);
1208 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1209 "GNS_PHASE_REC-%d: 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-%d: 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-%d: 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-%d: 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-%d: 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-%d: Namestore lookup for %s terminated without results\n",
1414 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1415 "GNS_PHASE_REC-%d: 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-%d: 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-%d: 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-%d: This record is expired. Skipping\n",
1459 if (rh->answered == 0)
1461 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1462 "GNS_PHASE_REC-%d: 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-%d: 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-%d: Got answer from VPN to query!\n",
1497 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1498 "GNS_PHASE_REC-%d: Answer is IPv4!\n",
1500 if (rlh->record_type != GNUNET_GNS_RECORD_TYPE_A)
1502 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1503 "GNS_PHASE_REC-%d: Requested record is not IPv4!\n",
1505 rh->proc (rh->proc_cls, rh, 0, NULL);
1508 rd.record_type = GNUNET_GNS_RECORD_TYPE_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-%d: Answer is IPv6!\n",
1521 if (rlh->record_type != GNUNET_GNS_RECORD_AAAA)
1523 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1524 "GNS_PHASE_REC-%d: 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-%d: 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-%d: 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-%d: 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_TYPE_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_TYPE_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_TYPE_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_TYPE_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_TYPE_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_TYPE_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_TYPE_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_TYPE_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_TYPE_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 (%ds)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 GNUNET_DHT_get_stop (rh->get_handle);
2126 rh->get_handle = NULL;
2128 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
2130 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2131 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
2133 rh->proc(rh->proc_cls, rh, 0, NULL);
2137 if (strcmp(rh->name, "") == 0)
2140 * promote authority back to name and try to resolve record
2142 strcpy(rh->name, rh->authority_name);
2143 rh->proc(rh->proc_cls, rh, 0, NULL);
2148 * Start resolution in bg
2150 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
2151 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
2152 //strcpy(new_name, rh->name);
2153 //strcpy(new_name+strlen(new_name), ".");
2154 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
2156 strcpy(rh->name, new_name);
2158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2159 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2160 rh->id, rh->name, rlh->record_type);
2162 gns_resolver_lookup_record(rh->authority,
2163 rh->private_local_zone,
2167 GNUNET_TIME_UNIT_FOREVER_REL,
2169 &background_lookup_result_processor,
2172 rh->proc(rh->proc_cls, rh, 0, NULL);
2176 static void resolve_delegation_dht(struct ResolverHandle *rh);
2179 static void resolve_delegation_ns(struct ResolverHandle *rh);
2183 * Namestore resolution for delegation finished. Processing result.
2185 * @param cls the closure
2186 * @param rh resolver handle
2187 * @param rd_count number of results (always 0)
2188 * @param rd record data (always NULL)
2191 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2192 unsigned int rd_count,
2193 const struct GNUNET_NAMESTORE_RecordData *rd);
2197 * This is a callback function that checks for key revocation
2199 * @param cls the pending query
2200 * @param key the key of the zone we did the lookup
2201 * @param expiration expiration date of the record data set in the namestore
2202 * @param name the name for which we need an authority
2203 * @param rd_count the number of records with 'name'
2204 * @param rd the record data
2205 * @param signature the signature of the authority for the record data
2208 process_pkey_revocation_result_ns (void *cls,
2209 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2210 struct GNUNET_TIME_Absolute expiration,
2212 unsigned int rd_count,
2213 const struct GNUNET_NAMESTORE_RecordData *rd,
2214 const struct GNUNET_CRYPTO_RsaSignature *signature)
2216 struct ResolverHandle *rh = cls;
2217 struct GNUNET_TIME_Relative remaining_time;
2220 rh->namestore_task = NULL;
2221 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2223 for (i = 0; i < rd_count; i++)
2225 if (rd[i].record_type == GNUNET_GNS_RECORD_REV)
2227 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2228 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2230 rh->status |= RSL_PKEY_REVOKED;
2231 rh->proc (rh->proc_cls, rh, 0, NULL);
2236 if ((name == NULL) ||
2237 (remaining_time.rel_value == 0))
2239 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2240 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2243 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2245 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2246 "GNS_PHASE_DELEGATE_REV-%d: Starting background lookup for %s type %d\n",
2247 rh->id, "+.gnunet", GNUNET_GNS_RECORD_REV);
2249 gns_resolver_lookup_record(rh->authority,
2250 rh->private_local_zone,
2251 GNUNET_GNS_RECORD_REV,
2254 GNUNET_TIME_UNIT_FOREVER_REL,
2256 &background_lookup_result_processor,
2260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2261 "GNS_PHASE_DELEGATE_REV-%llu: Revocation checkpassed\n",
2264 * We are done with PKEY resolution if name is empty
2265 * else resolve again with new authority
2267 if (strcmp (rh->name, "") == 0)
2268 rh->proc (rh->proc_cls, rh, 0, NULL);
2270 resolve_delegation_ns (rh);
2276 * Callback when record data is put into namestore
2278 * @param cls the closure
2279 * @param success GNUNET_OK on success
2280 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2283 on_namestore_delegation_put_result(void *cls,
2287 struct NamestoreBGTask *nbg = cls;
2289 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2292 if (GNUNET_NO == success)
2294 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2295 "GNS_NS: records already in namestore\n");
2298 else if (GNUNET_YES == success)
2300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2301 "GNS_NS: records successfully put in namestore\n");
2305 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2306 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2310 * Function called when we get a result from the dht
2311 * for our query. Recursively tries to resolve authorities
2314 * @param cls the request handle
2315 * @param exp lifetime
2316 * @param key the key the record was stored under
2317 * @param get_path get path
2318 * @param get_path_length get path length
2319 * @param put_path put path
2320 * @param put_path_length put path length
2321 * @param type the block type
2322 * @param size the size of the record
2323 * @param data the record data
2326 process_delegation_result_dht(void* cls,
2327 struct GNUNET_TIME_Absolute exp,
2328 const struct GNUNET_HashCode * key,
2329 const struct GNUNET_PeerIdentity *get_path,
2330 unsigned int get_path_length,
2331 const struct GNUNET_PeerIdentity *put_path,
2332 unsigned int put_path_length,
2333 enum GNUNET_BLOCK_Type type,
2334 size_t size, const void *data)
2336 struct ResolverHandle *rh;
2337 struct GNSNameRecordBlock *nrb;
2338 uint32_t num_records;
2340 char* rd_data = (char*) data;
2343 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
2344 struct GNUNET_HashCode zone_hash_double, name_hash_double;
2346 rh = (struct ResolverHandle *)cls;
2348 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2349 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2354 nrb = (struct GNSNameRecordBlock*)data;
2356 /* stop dht lookup and timeout task */
2357 GNUNET_DHT_get_stop (rh->get_handle);
2359 rh->get_handle = NULL;
2361 if (rh->dht_heap_node != NULL)
2363 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2364 rh->dht_heap_node = NULL;
2367 num_records = ntohl(nrb->rd_count);
2368 name = (char*)&nrb[1];
2370 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2371 struct NamestoreBGTask *ns_heap_root;
2372 struct NamestoreBGTask *namestore_bg_task;
2374 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2375 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2377 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2382 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2383 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2388 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2389 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2390 rh->id, name, rh->authority_name);
2391 for (i=0; i<num_records; i++)
2394 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2395 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2396 rh->id, name, rh->authority_name);
2397 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2398 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2399 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2400 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2401 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2402 rh->id, rd[i].data_size);
2403 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2404 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2405 rh->id, rd[i].flags);
2407 if ((rd[i].record_type == GNUNET_GNS_RECORD_VPN) ||
2408 (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS) ||
2409 (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_CNAME))
2412 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2414 if (strcmp(rh->name, "") == 0)
2415 strcpy(rh->name, rh->authority_name);
2417 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2418 rh->name, rh->authority_name); //FIXME ret
2423 if ((strcmp(name, rh->authority_name) == 0) &&
2424 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
2426 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2427 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2430 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2431 struct AuthorityChain *auth =
2432 GNUNET_malloc(sizeof(struct AuthorityChain));
2433 auth->zone = rh->authority;
2434 memset(auth->name, 0, strlen(rh->authority_name)+1);
2435 strcpy(auth->name, rh->authority_name);
2436 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2437 rh->authority_chain_tail,
2440 /** try to import pkey if private key available */
2441 //if (rh->priv_key && is_canonical (rh->name))
2442 // process_discovered_authority(name, auth->zone,
2443 // rh->authority_chain_tail->zone,
2450 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2451 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2452 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2453 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2455 /* Save to namestore */
2456 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
2459 if (max_allowed_ns_tasks <=
2460 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2462 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2463 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2465 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2466 "GNS_PHASE_REC-%d: Replacing oldest background ns task\n",
2470 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2472 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2474 GNUNET_TIME_absolute_get().abs_value);
2475 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2482 &on_namestore_delegation_put_result, //cont
2483 namestore_bg_task); //cls
2492 * FIXME in this case. should we ask namestore again?
2494 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2495 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2496 rh->id, rh->authority_name, rh->name);
2498 if (strcmp(rh->name, "") == 0)
2500 /* Start shortening */
2501 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2504 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2506 start_shorten (rh->authority_chain_tail,
2511 rh->proc = &handle_delegation_ns;
2513 /* Check for key revocation and delegate */
2514 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2517 GNUNET_GNS_RECORD_REV,
2518 &process_pkey_revocation_result_ns,
2525 * No pkey but name exists
2528 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2529 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2530 rh->id, rh->authority_name, rh->name);
2531 if (strcmp(rh->name, "") == 0)
2532 strcpy(rh->name, rh->authority_name);
2534 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2535 rh->name, rh->authority_name); //FIXME ret
2537 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2538 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2539 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2540 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2542 rh->proc(rh->proc_cls, rh, 0, NULL);
2545 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2546 +(MAX_DNS_NAME_LENGTH*2)
2547 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2548 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2552 expand_plus(char* dest, char* src, char* repl)
2555 unsigned int s_len = strlen(src)+1;
2557 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2558 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2562 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2563 "GNS_POSTPROCESS: %s to short\n", src);
2565 /* no postprocessing */
2566 memcpy(dest, src, s_len+1);
2570 if (0 == strcmp(src+s_len-3, ".+"))
2572 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2573 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2574 memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2578 pos += strlen(repl);
2579 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2580 "GNS_POSTPROCESS: Expanded to %s\n", dest);
2584 memcpy(dest, src, s_len+1);
2592 finish_lookup (struct ResolverHandle *rh,
2593 struct RecordLookupHandle* rlh,
2594 unsigned int rd_count,
2595 const struct GNUNET_NAMESTORE_RecordData *rd)
2598 char new_rr_data[MAX_DNS_NAME_LENGTH];
2599 char new_mx_data[MAX_MX_LENGTH];
2600 char new_soa_data[MAX_SOA_LENGTH];
2601 char new_srv_data[MAX_SRV_LENGTH];
2602 struct srv_data *old_srv;
2603 struct srv_data *new_srv;
2604 struct soa_data *old_soa;
2605 struct soa_data *new_soa;
2606 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2609 unsigned int offset;
2611 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2613 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2614 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2617 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2620 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2622 for (i = 0; i < rd_count; i++)
2625 if (rd[i].record_type != GNUNET_GNS_RECORD_TYPE_NS &&
2626 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_CNAME &&
2627 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
2628 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_SOA &&
2629 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_SRV)
2631 p_rd[i].data = rd[i].data;
2636 * for all those records we 'should'
2637 * also try to resolve the A/AAAA records (RFC1035)
2638 * This is a feature and not important
2641 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2642 "GNS_POSTPROCESS: Postprocessing\n");
2644 if (strcmp(rh->name, "+") == 0)
2645 repl_string = rlh->name;
2647 repl_string = rlh->name+strlen(rh->name)+1;
2650 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
2652 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2653 offset = sizeof(uint16_t);
2654 pos = new_mx_data+offset;
2655 expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2657 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2658 p_rd[i].data = new_mx_data;
2659 p_rd[i].data_size = offset;
2661 else if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_SRV)
2664 * Prio, weight and port
2666 new_srv = (struct srv_data*)new_srv_data;
2667 old_srv = (struct srv_data*)rd[i].data;
2668 new_srv->prio = old_srv->prio;
2669 new_srv->weight = old_srv->weight;
2670 new_srv->port = old_srv->port;
2671 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2673 p_rd[i].data = new_srv_data;
2674 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2676 else if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_SOA)
2678 /* expand mname and rname */
2679 old_soa = (struct soa_data*)rd[i].data;
2680 new_soa = (struct soa_data*)new_soa_data;
2681 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2682 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2683 offset = strlen ((char*)&new_soa[1]) + 1;
2684 expand_plus((char*)&new_soa[1] + offset,
2685 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2687 p_rd[i].data_size = sizeof (struct soa_data)
2689 + strlen ((char*)&new_soa[1] + offset);
2690 p_rd[i].data = new_soa_data;
2695 expand_plus(pos, (char*)rd[i].data, repl_string);
2696 p_rd[i].data_size = strlen(new_rr_data)+1;
2697 p_rd[i].data = new_rr_data;
2702 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2704 free_resolver_handle (rh);
2708 * Process DHT lookup result for record.
2710 * @param cls the closure
2711 * @param rh resolver handle
2712 * @param rd_count number of results
2713 * @param rd record data
2716 handle_record_dht(void* cls, struct ResolverHandle *rh,
2717 unsigned int rd_count,
2718 const struct GNUNET_NAMESTORE_RecordData *rd)
2720 struct RecordLookupHandle* rlh;
2722 rlh = (struct RecordLookupHandle*)cls;
2725 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2726 "GNS_PHASE_REC-%d: No records for %s found in DHT. Aborting\n",
2728 /* give up, cannot resolve */
2729 finish_lookup(rh, rlh, 0, NULL);
2733 /* results found yay */
2734 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2735 "GNS_PHASE_REC-%d: Record resolved from DHT!", rh->id);
2737 finish_lookup(rh, rlh, rd_count, rd);
2745 * Process namestore lookup result for record.
2747 * @param cls the closure
2748 * @param rh resolver handle
2749 * @param rd_count number of results
2750 * @param rd record data
2753 handle_record_ns (void* cls, struct ResolverHandle *rh,
2754 unsigned int rd_count,
2755 const struct GNUNET_NAMESTORE_RecordData *rd)
2757 struct RecordLookupHandle* rlh;
2758 rlh = (struct RecordLookupHandle*) cls;
2759 int check_dht = GNUNET_YES;
2763 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2764 "GNS_PHASE_REC-%d: NS returned no records. (status: %d)!\n",
2769 * There are 5 conditions that have to met for us to consult the DHT:
2770 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2771 * 2. No entry in the NS existed AND
2772 * 3. The zone queried is not the local resolver's zone AND
2773 * 4. The name that was looked up is '+'
2774 * because if it was any other canonical name we either already queried
2775 * the DHT for the authority in the authority lookup phase (and thus
2776 * would already have an entry in the NS for the record)
2777 * 5. We are not in cache only mode
2779 if (((rh->status & RSL_RECORD_EXPIRED) != 0) &&
2780 ((rh->status & RSL_RECORD_EXISTS) == 0) )
2781 check_dht = GNUNET_NO;
2783 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2784 &rh->private_local_zone))
2785 check_dht = GNUNET_NO;
2787 if ((strcmp (rh->name, "+") != 0) && (is_srv (rh->name) != 0))
2788 check_dht = GNUNET_NO;
2791 if (rh->only_cached == GNUNET_YES)
2792 check_dht = GNUNET_NO;
2794 if (GNUNET_YES == check_dht)
2796 rh->proc = &handle_record_dht;
2797 resolve_record_dht(rh);
2800 /* give up, cannot resolve */
2801 finish_lookup(rh, rlh, 0, NULL);
2805 /* results found yay */
2806 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2807 "GNS_PHASE_REC-%d: Record resolved from namestore!\n", rh->id);
2809 finish_lookup(rh, rlh, rd_count, rd);
2816 * Move one level up in the domain hierarchy and return the
2817 * passed top level domain.
2819 * @param name the domain
2820 * @param dest the destination where the tld will be put
2823 pop_tld(char* name, char* dest)
2827 if (is_canonical (name))
2834 for (len = strlen(name); len > 0; len--)
2836 if (*(name+len) == '.')
2846 strcpy(dest, (name+len+1));
2850 * Checks if name is in tld
2852 * @param name the name to check
2853 * @param tld the TLD to check for
2854 * @return GNUNET_YES or GNUNET_NO
2857 is_tld(const char* name, const char* tld)
2861 if (strlen(name) <= strlen(tld))
2866 offset = strlen(name)-strlen(tld);
2867 if (strcmp(name+offset, tld) != 0)
2869 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2870 "%s is not in .%s TLD\n", name, tld);
2877 * DHT resolution for delegation finished. Processing result.
2879 * @param cls the closure
2880 * @param rh resolver handle
2881 * @param rd_count number of results (always 0)
2882 * @param rd record data (always NULL)
2885 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2886 unsigned int rd_count,
2887 const struct GNUNET_NAMESTORE_RecordData *rd)
2889 struct RecordLookupHandle* rlh;
2890 rlh = (struct RecordLookupHandle*) cls;
2893 if (strcmp(rh->name, "") == 0)
2895 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
2897 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2898 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2900 finish_lookup(rh, rlh, rd_count, rd);
2903 /* We resolved full name for delegation. resolving record */
2904 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2905 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2907 strcpy(rh->name, "+\0");
2908 rh->proc = &handle_record_ns;
2909 resolve_record_ns(rh);
2914 * we still have some left
2916 if (is_canonical(rh->name))
2918 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2919 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2922 rh->proc = &handle_record_ns;
2923 resolve_record_ns(rh);
2926 /* give up, cannot resolve */
2927 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2928 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2930 finish_lookup(rh, rlh, 0, NULL);
2935 * Start DHT lookup for a name -> PKEY (compare NS) record in
2936 * rh->authority's zone
2938 * @param rh the pending gns query
2941 resolve_delegation_dht(struct ResolverHandle *rh)
2944 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2945 struct GNUNET_HashCode name_hash_double;
2946 struct GNUNET_HashCode zone_hash_double;
2947 struct GNUNET_HashCode lookup_key;
2948 struct ResolverHandle *rh_heap_root;
2950 pop_tld(rh->name, rh->authority_name);
2951 GNUNET_CRYPTO_short_hash(rh->authority_name,
2952 strlen(rh->authority_name),
2954 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2955 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2956 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2958 rh->dht_heap_node = NULL;
2960 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2962 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
2963 // &dht_authority_lookup_timeout,
2965 rh->timeout_cont = &dht_authority_lookup_timeout;
2966 rh->timeout_cont_cls = rh;
2970 if (max_allowed_background_queries <=
2971 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2973 /* terminate oldest lookup */
2974 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2975 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2976 rh_heap_root->get_handle = NULL;
2977 rh_heap_root->dht_heap_node = NULL;
2979 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2980 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2981 rh->id, rh_heap_root->authority_name);
2983 rh_heap_root->proc(rh_heap_root->proc_cls,
2988 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2990 GNUNET_TIME_absolute_get().abs_value);
2993 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2995 GNUNET_assert(rh->get_handle == NULL);
2996 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2997 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2999 DHT_GNS_REPLICATION_LEVEL,
3003 &process_delegation_result_dht,
3010 * Namestore resolution for delegation finished. Processing result.
3012 * @param cls the closure
3013 * @param rh resolver handle
3014 * @param rd_count number of results (always 0)
3015 * @param rd record data (always NULL)
3018 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
3019 unsigned int rd_count,
3020 const struct GNUNET_NAMESTORE_RecordData *rd)
3022 struct RecordLookupHandle* rlh;
3023 rlh = (struct RecordLookupHandle*) cls;
3024 int check_dht = GNUNET_YES;
3027 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3028 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
3029 rh->id, rh->status);
3031 if (rh->status & RSL_PKEY_REVOKED)
3033 finish_lookup (rh, rlh, 0, NULL);
3037 if (strcmp(rh->name, "") == 0)
3040 /* We resolved full name for delegation. resolving record */
3041 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3042 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
3044 if (rh->status & RSL_CNAME_FOUND)
3046 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_CNAME)
3048 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3049 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
3051 strcpy (rh->name, rh->authority_name);
3052 finish_lookup (rh, rlh, rd_count, rd);
3057 if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
3059 s_len = strlen (rd->data) - 2;
3060 memcpy (rh->name, rd->data, s_len);
3061 rh->name[s_len] = '\0';
3062 resolve_delegation_ns (rh);
3065 else if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
3067 gns_resolver_lookup_record (rh->authority,
3068 rh->private_local_zone,
3077 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3078 free_resolver_handle (rh);
3084 strcpy (rh->dns_name, (char*)rd->data);
3085 resolve_dns_name (rh);
3090 else if (rh->status & RSL_DELEGATE_VPN)
3092 if (rlh->record_type == GNUNET_GNS_RECORD_VPN)
3094 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3095 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
3097 finish_lookup(rh, rlh, rd_count, rd);
3100 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3101 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
3103 GNUNET_assert (NULL != rd);
3104 rh->proc = &handle_record_vpn;
3105 resolve_record_vpn (rh, rd_count, rd);
3108 else if (rh->status & RSL_DELEGATE_NS)
3110 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_NS)
3112 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3113 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3115 finish_lookup(rh, rlh, rd_count, rd);
3119 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3120 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3122 GNUNET_assert (NULL != rd);
3123 rh->proc = &handle_record_ns;
3124 resolve_record_dns (rh, rd_count, rd);
3127 else if (rh->status & RSL_DELEGATE_PKEY)
3129 if (rh->status & RSL_PKEY_REVOKED)
3131 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3132 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
3134 finish_lookup (rh, rlh, 0, NULL);
3137 else if (rlh->record_type == GNUNET_GNS_RECORD_PKEY)
3139 GNUNET_assert(rd_count == 1);
3140 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3141 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
3143 finish_lookup(rh, rlh, rd_count, rd);
3147 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3148 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3150 strcpy(rh->name, "+\0");
3151 rh->proc = &handle_record_ns;
3152 resolve_record_ns(rh);
3156 if (rh->status & RSL_DELEGATE_NS)
3158 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_NS)
3160 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3161 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3163 finish_lookup(rh, rlh, rd_count, rd);
3167 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3168 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3170 GNUNET_assert (NULL != rd);
3171 rh->proc = &handle_record_ns;
3172 resolve_record_dns (rh, rd_count, rd);
3177 * we still have some left
3178 * check if authority in ns is fresh
3180 * or we are authority
3183 if ((rh->status & RSL_RECORD_EXISTS) &&
3184 !(rh->status & RSL_RECORD_EXPIRED))
3185 check_dht = GNUNET_NO;
3187 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3188 &rh->private_local_zone))
3189 check_dht = GNUNET_NO;
3191 if (rh->only_cached == GNUNET_YES)
3192 check_dht = GNUNET_NO;
3194 if (check_dht == GNUNET_NO)
3196 if (is_canonical(rh->name))
3198 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3199 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3202 rh->proc = &handle_record_ns;
3203 resolve_record_ns(rh);
3207 /* give up, cannot resolve */
3208 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3209 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3212 finish_lookup(rh, rlh, rd_count, rd);
3213 //rlh->proc(rlh->proc_cls, 0, NULL);
3218 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3219 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3221 rh->proc = &handle_delegation_dht;
3222 resolve_delegation_dht(rh);
3227 * This is a callback function that should give us only PKEY
3228 * records. Used to query the namestore for the authority (PKEY)
3229 * for 'name'. It will recursively try to resolve the
3230 * authority for a given name from the namestore.
3232 * @param cls the pending query
3233 * @param key the key of the zone we did the lookup
3234 * @param expiration expiration date of the record data set in the namestore
3235 * @param name the name for which we need an authority
3236 * @param rd_count the number of records with 'name'
3237 * @param rd the record data
3238 * @param signature the signature of the authority for the record data
3241 process_delegation_result_ns (void* cls,
3242 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3243 struct GNUNET_TIME_Absolute expiration,
3245 unsigned int rd_count,
3246 const struct GNUNET_NAMESTORE_RecordData *rd,
3247 const struct GNUNET_CRYPTO_RsaSignature *signature)
3249 struct ResolverHandle *rh;
3250 struct GNUNET_TIME_Relative remaining_time;
3251 struct GNUNET_CRYPTO_ShortHashCode zone;
3252 char new_name[MAX_DNS_NAME_LENGTH];
3254 struct GNUNET_TIME_Absolute et;
3256 rh = (struct ResolverHandle *)cls;
3257 rh->namestore_task = NULL;
3258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3259 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3262 GNUNET_CRYPTO_short_hash (key,
3263 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3265 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3272 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3274 rh->status |= RSL_RECORD_EXISTS;
3276 if (remaining_time.rel_value == 0)
3278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3279 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3281 rh->status |= RSL_RECORD_EXPIRED;
3286 * No authority found in namestore.
3291 * We did not find an authority in the namestore
3296 * Promote this authority back to a name maybe it is
3299 if (strcmp (rh->name, "") == 0)
3301 /* simply promote back */
3302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3303 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3304 rh->id, rh->authority_name);
3305 strcpy (rh->name, rh->authority_name);
3309 /* add back to existing name */
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3311 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3312 rh->id, rh->authority_name, rh->name);
3313 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3314 rh->name, rh->authority_name);
3315 strcpy (rh->name, new_name);
3316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3317 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3321 rh->proc (rh->proc_cls, rh, 0, NULL);
3326 * We found an authority that may be able to help us
3327 * move on with query
3328 * Note only 1 pkey should have been returned.. anything else would be strange
3330 for (i=0; i < rd_count;i++)
3334 * A CNAME. Like regular DNS this means the is no other record for this
3337 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_CNAME)
3339 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3340 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3343 rh->status |= RSL_CNAME_FOUND;
3344 rh->proc (rh->proc_cls, rh, rd_count, rd);
3351 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3353 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3354 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3356 rh->status |= RSL_DELEGATE_VPN;
3357 rh->proc (rh->proc_cls, rh, rd_count, rd);
3363 * FIXME make optional
3365 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS)
3367 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3368 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3370 rh->status |= RSL_DELEGATE_NS;
3371 rh->proc (rh->proc_cls, rh, rd_count, rd);
3375 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3378 rh->status |= RSL_DELEGATE_PKEY;
3380 if ((ignore_pending_records != 0) &&
3381 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3384 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3390 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3391 et.abs_value = rd[i].expiration_time;
3392 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3396 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3398 if (remaining_time.rel_value == 0)
3400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3401 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3403 rh->authority_chain_head->fresh = 0;
3404 rh->proc (rh->proc_cls, rh, 0, NULL);
3412 * Resolve rest of query with new authority
3414 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3415 memcpy (&rh->authority, rd[i].data,
3416 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3417 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3418 auth->zone = rh->authority;
3419 memset (auth->name, 0, strlen (rh->authority_name)+1);
3420 strcpy (auth->name, rh->authority_name);
3421 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3422 rh->authority_chain_tail,
3425 /* Check for key revocation and delegate */
3426 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3429 GNUNET_GNS_RECORD_REV,
3430 &process_pkey_revocation_result_ns,
3439 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3440 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3442 * If we have found some records for the LAST label
3443 * we return the results. Else null.
3445 if (strcmp(rh->name, "") == 0)
3447 /* Start shortening */
3448 if ((rh->priv_key != NULL) && is_canonical (rh->name))
3450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3451 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3453 start_shorten (rh->authority_chain_tail,
3456 /* simply promote back */
3457 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3458 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3459 rh->id, rh->authority_name);
3460 strcpy(rh->name, rh->authority_name);
3461 rh->proc(rh->proc_cls, rh, rd_count, rd);
3465 rh->proc(rh->proc_cls, rh, 0, NULL);
3471 * Resolve the delegation chain for the request in our namestore
3473 * @param rh the resolver handle
3476 resolve_delegation_ns (struct ResolverHandle *rh)
3478 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3479 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3481 pop_tld(rh->name, rh->authority_name);
3482 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3485 GNUNET_GNS_RECORD_ANY,
3486 &process_delegation_result_ns,
3493 * Lookup of a record in a specific zone
3494 * calls lookup result processor on result
3496 * @param zone the root zone
3497 * @param pzone the private local zone
3498 * @param record_type the record type to look up
3499 * @param name the name to look up
3500 * @param key a private key for use with PSEU import (can be NULL)
3501 * @param timeout timeout for resolution
3502 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3503 * @param proc the processor to call on result
3504 * @param cls the closure to pass to proc
3507 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3508 struct GNUNET_CRYPTO_ShortHashCode pzone,
3509 uint32_t record_type,
3511 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3512 struct GNUNET_TIME_Relative timeout,
3514 RecordLookupProcessor proc,
3517 struct ResolverHandle *rh;
3518 struct RecordLookupHandle* rlh;
3519 char string_hash[MAX_DNS_LABEL_LENGTH];
3520 char nzkey[MAX_DNS_LABEL_LENGTH];
3521 char* nzkey_ptr = nzkey;
3523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3524 "Starting resolution for %s (type=%d)!\n",
3528 if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
3530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3531 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3536 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3537 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3539 memset (rh, 0, sizeof (struct ResolverHandle));
3540 rh->authority = zone;
3544 rh->timeout = timeout;
3545 rh->get_handle = NULL;
3546 rh->private_local_zone = pzone;
3547 rh->only_cached = only_cached;
3548 rh->namestore_task = NULL;
3550 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3555 "No shorten key for resolution\n");
3558 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3561 * Set timeout for authority lookup phase to 1/2
3563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3564 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3565 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3566 GNUNET_TIME_relative_divide(timeout, 2),
3567 &handle_lookup_timeout,
3569 rh->timeout_cont = &dht_authority_lookup_timeout;
3570 rh->timeout_cont_cls = rh;
3574 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3575 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3578 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3581 * Only 'gnunet' given
3583 strcpy(rh->name, "\0");
3587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3588 "Checking for TLD...\n");
3589 if (is_zkey_tld(name) == GNUNET_YES)
3591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3594 * This is a zkey tld
3595 * build hash and use as initial authority
3598 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3599 memcpy(rh->name, name,
3600 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3601 pop_tld(rh->name, string_hash);
3603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3604 "ZKEY is %s!\n", string_hash);
3606 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3608 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3612 "Cannot convert ZKEY %s to hash!\n", string_hash);
3614 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3617 proc (cls, 0, NULL);
3624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3627 * Presumably GNUNET tld
3629 memset (rh->name, 0,
3630 strlen(name)-strlen(GNUNET_GNS_TLD));
3631 memcpy (rh->name, name,
3632 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3637 * Initialize authority chain
3639 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3640 rh->authority_chain_head->prev = NULL;
3641 rh->authority_chain_head->next = NULL;
3642 rh->authority_chain_tail = rh->authority_chain_head;
3643 rh->authority_chain_head->zone = rh->authority;
3646 * Copy original query into lookup handle
3648 rlh->record_type = record_type;
3649 memset(rlh->name, 0, strlen(name) + 1);
3650 strcpy(rlh->name, name);
3652 rlh->proc_cls = cls;
3654 rh->proc = &handle_delegation_ns;
3655 resolve_delegation_ns(rh);
3658 /******** END Record Resolver ***********/
3661 finish_shorten (struct ResolverHandle *rh,
3662 struct NameShortenHandle *nsh)
3664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3665 "Sending %s as shorten result\n", nsh->result);
3666 nsh->proc (nsh->proc_cls, nsh->result);
3667 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3669 free_resolver_handle (rh);
3674 * Callback calles by namestore for a zone to name
3677 * @param cls the closure
3678 * @param zone_key the zone we queried
3679 * @param expire the expiration time of the name
3680 * @param name the name found or NULL
3681 * @param rd_len number of records for the name
3682 * @param rd the record data (PKEY) for the name
3683 * @param signature the signature for the record data
3686 process_zone_to_name_shorten_root (void *cls,
3687 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3688 struct GNUNET_TIME_Absolute expire,
3690 unsigned int rd_len,
3691 const struct GNUNET_NAMESTORE_RecordData *rd,
3692 const struct GNUNET_CRYPTO_RsaSignature *signature);
3696 * Callback called by namestore for a zone to name
3699 * @param cls the closure
3700 * @param zone_key the zone we queried
3701 * @param expire the expiration time of the name
3702 * @param name the name found or NULL
3703 * @param rd_len number of records for the name
3704 * @param rd the record data (PKEY) for the name
3705 * @param signature the signature for the record data
3708 process_zone_to_name_shorten_shorten (void *cls,
3709 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3710 struct GNUNET_TIME_Absolute expire,
3712 unsigned int rd_len,
3713 const struct GNUNET_NAMESTORE_RecordData *rd,
3714 const struct GNUNET_CRYPTO_RsaSignature *signature)
3716 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3717 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3718 struct AuthorityChain *next_authority;
3720 char result[MAX_DNS_NAME_LENGTH];
3721 char tmp_name[MAX_DNS_NAME_LENGTH];
3724 rh->namestore_task = NULL;
3725 /* we found a match in our own root zone */
3728 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3729 "result strlen %d\n", strlen(name));
3730 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3731 memset(result, 0, answer_len);
3733 if (strlen(rh->name) > 0)
3735 sprintf (result, "%s.%s.%s.%s.%s",
3737 nsh->shorten_zone_name, nsh->private_zone_name,
3742 sprintf (result, "%s.%s.%s.%s", name,
3743 nsh->shorten_zone_name, nsh->private_zone_name,
3747 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3748 "Found shorten result %s\n", result);
3749 if (strlen (nsh->result) > strlen (result))
3750 strcpy (nsh->result, result);
3752 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3753 nsh->shorten_zone) == 0)
3756 * This is our zone append .gnunet unless name is empty
3757 * (it shouldn't be, usually FIXME what happens if we
3758 * shorten to our zone to a "" record??)
3761 sprintf (result, "%s.%s.%s.%s",
3763 nsh->shorten_zone_name, nsh->private_zone_name,
3765 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3766 "Our zone: Found %s as shorten result\n", result);
3768 if (strlen (nsh->result) > strlen (result))
3769 strcpy (nsh->result, result);
3770 //nsh->proc(nsh->proc_cls, result);
3772 //free_resolver_handle(rh);
3779 * continue with next authority if exists
3781 if ((rh->authority_chain_head->next == NULL))
3783 finish_shorten (rh, nsh);
3786 next_authority = rh->authority_chain_head;
3788 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3789 "%s.%s", rh->name, next_authority->name);
3791 strcpy(rh->name, tmp_name);
3792 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3793 "No PSEU found for authority %s. Promoting back: %s\n",
3794 next_authority->name, rh->name);
3796 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3797 rh->authority_chain_tail,
3800 GNUNET_free (next_authority);
3802 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3803 &rh->authority_chain_tail->zone,
3804 &rh->authority_chain_head->zone,
3805 &process_zone_to_name_shorten_root,
3810 * Callback calles by namestore for a zone to name
3813 * @param cls the closure
3814 * @param zone_key the zone we queried
3815 * @param expire the expiration time of the name
3816 * @param name the name found or NULL
3817 * @param rd_len number of records for the name
3818 * @param rd the record data (PKEY) for the name
3819 * @param signature the signature for the record data
3822 process_zone_to_name_shorten_private (void *cls,
3823 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3824 struct GNUNET_TIME_Absolute expire,
3826 unsigned int rd_len,
3827 const struct GNUNET_NAMESTORE_RecordData *rd,
3828 const struct GNUNET_CRYPTO_RsaSignature *signature)
3830 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3831 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3832 struct AuthorityChain *next_authority;
3834 char result[MAX_DNS_NAME_LENGTH];
3835 char tmp_name[MAX_DNS_NAME_LENGTH];
3838 rh->namestore_task = NULL;
3839 /* we found a match in our own root zone */
3842 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3843 "result strlen %d\n", strlen(name));
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 (nsh->shorten_zone != NULL)
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 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3956 "result strlen %d\n", strlen(name));
3957 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3958 memset(result, 0, answer_len);
3960 if (strlen(rh->name) > 0)
3962 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3966 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3969 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3970 "Found shorten result %s\n", result);
3971 if (strlen (nsh->result) > strlen (result))
3972 strcpy (nsh->result, result);
3974 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3975 nsh->root_zone) == 0)
3978 * This is our zone append .gnunet unless name is empty
3979 * (it shouldn't be, usually FIXME what happens if we
3980 * shorten to our zone to a "" record??)
3983 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3984 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3985 "Our zone: Found %s as shorten result\n", result);
3986 if (strlen (nsh->result) > strlen (result))
3987 strcpy (nsh->result, result);
3990 if (nsh->private_zone != NULL)
3992 /* backtrack authorities for names in priv zone */
3993 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3995 &rh->authority_chain_head->zone,
3996 &process_zone_to_name_shorten_private,
4003 * continue with next authority if exists
4005 if ((rh->authority_chain_head->next == NULL))
4007 finish_shorten (rh, nsh);
4010 next_authority = rh->authority_chain_head;
4012 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
4013 "%s.%s", rh->name, next_authority->name);
4015 strcpy(rh->name, tmp_name);
4016 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4017 "No PSEU found for authority %s. Promoting back: %s\n",
4018 next_authority->name, rh->name);
4020 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
4021 rh->authority_chain_tail,
4024 GNUNET_free (next_authority);
4026 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4027 &rh->authority_chain_tail->zone,
4028 &rh->authority_chain_head->zone,
4029 &process_zone_to_name_shorten_root,
4036 * Process result from namestore delegation lookup
4037 * for shorten operation
4039 * @param cls the client shorten handle
4040 * @param rh the resolver handle
4041 * @param rd_count number of results (0)
4042 * @param rd data (NULL)
4045 handle_delegation_ns_shorten (void* cls,
4046 struct ResolverHandle *rh,
4048 const struct GNUNET_NAMESTORE_RecordData *rd)
4050 struct NameShortenHandle *nsh;
4051 char result[MAX_DNS_NAME_LENGTH];
4053 nsh = (struct NameShortenHandle *)cls;
4056 * At this point rh->name contains the part of the name
4057 * that we do not have a PKEY in our namestore to resolve.
4058 * The authority chain in the resolver handle is now
4059 * useful to backtrack if needed
4062 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4063 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4064 memset(result, 0, sizeof (result));
4066 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4067 nsh->root_zone) == 0)
4070 * This is our zone append .gnunet unless name is empty
4071 * (it shouldn't be, usually FIXME what happens if we
4072 * shorten to our zone to a "" record??)
4075 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
4076 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4077 "Our zone: Found %s as shorten result\n", result);
4079 if (strlen (nsh->result) > strlen (result))
4080 strcpy (nsh->result, result);
4083 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4084 nsh->private_zone) == 0)
4087 * This is our zone append .gnunet unless name is empty
4088 * (it shouldn't be, usually FIXME what happens if we
4089 * shorten to our zone to a "" record??)
4092 sprintf (result, "%s.%s.%s",
4093 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4094 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4095 "Our zone: Found %s as shorten result %s\n", result);
4097 if (strlen (nsh->result) > strlen (result))
4098 strcpy (nsh->result, result);
4100 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4101 nsh->shorten_zone) == 0)
4104 * This is our zone append .gnunet unless name is empty
4105 * (it shouldn't be, usually FIXME what happens if we
4106 * shorten to our zone to a "" record??)
4109 sprintf (result, "%s.%s.%s",
4110 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4111 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4112 "Our zone: Found %s as shorten result\n", result);
4114 if (strlen (nsh->result) > strlen (result))
4115 strcpy (nsh->result, result);
4119 /* backtrack authorities for names */
4120 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4122 &rh->authority_chain_head->zone,
4123 &process_zone_to_name_shorten_root,
4130 * Callback calles by namestore for a zone to name
4133 * @param cls the closure
4134 * @param zone_key the zone we queried
4135 * @param expire the expiration time of the name
4136 * @param name the name found or NULL
4137 * @param rd_len number of records for the name
4138 * @param rd the record data (PKEY) for the name
4139 * @param signature the signature for the record data
4142 process_zone_to_name_zkey(void *cls,
4143 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4144 struct GNUNET_TIME_Absolute expire,
4146 unsigned int rd_len,
4147 const struct GNUNET_NAMESTORE_RecordData *rd,
4148 const struct GNUNET_CRYPTO_RsaSignature *signature)
4150 struct ResolverHandle *rh = cls;
4151 struct NameShortenHandle *nsh = rh->proc_cls;
4152 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
4153 char new_name[MAX_DNS_NAME_LENGTH];
4155 /* zkey not in our zone */
4159 * In this case we have not given this PKEY a name (yet)
4160 * It is either just not in our zone or not even cached
4161 * Since we do not know at this point we will not try to shorten
4162 * because PKEY import will happen if the user follows the zkey
4165 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
4167 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4168 "No name found for zkey %s returning verbatim!\n", enc);
4169 if (strcmp(rh->name, "") != 0)
4170 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4171 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4173 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4174 enc, GNUNET_GNS_TLD_ZKEY);
4176 strcpy (nsh->result, new_name);
4178 finish_shorten (rh, nsh);
4182 if (strcmp(rh->name, "") != 0)
4183 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4186 strcpy(new_name, name);
4188 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4189 "Continue shorten for %s!\n", new_name);
4191 strcpy(rh->name, new_name);
4193 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4194 rh->authority_chain_tail = rh->authority_chain_head;
4195 rh->authority_chain_head->zone = rh->authority;
4198 /* Start delegation resolution in our namestore */
4199 resolve_delegation_ns (rh);
4204 * Shorten api from resolver
4206 * @param zone the root zone to use
4207 * @param pzone the private zone to use
4208 * @param szone the shorten zone to use
4209 * @param name the name to shorten
4210 * @param private_zone_name name of the private zone
4211 * @param shorten_zone_name name of the shorten zone
4212 * @param proc the processor to call with result
4213 * @param proc_cls closure to pass to proc
4216 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4217 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4218 struct GNUNET_CRYPTO_ShortHashCode *szone,
4220 const char* private_zone_name,
4221 const char* shorten_zone_name,
4222 ShortenResultProcessor proc,
4225 struct ResolverHandle *rh;
4226 struct NameShortenHandle *nsh;
4227 char string_hash[MAX_DNS_LABEL_LENGTH];
4228 struct GNUNET_CRYPTO_ShortHashCode zkey;
4229 char nzkey[MAX_DNS_LABEL_LENGTH];
4230 char* nzkey_ptr = nzkey;
4233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4234 "Starting shorten for %s!\n", name);
4236 if (is_canonical ((char*)name))
4238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4239 "%s is canonical. Returning verbatim\n", name);
4240 proc (proc_cls, name);
4244 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4247 nsh->proc_cls = proc_cls;
4248 nsh->root_zone = zone;
4249 nsh->private_zone = pzone;
4250 nsh->shorten_zone = szone;
4251 strcpy (nsh->private_zone_name, private_zone_name);
4252 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4253 strcpy (nsh->result, name);
4256 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4257 rh->authority = *zone;
4259 rh->priv_key = NULL;
4260 rh->namestore_task = NULL;
4261 rh->proc = &handle_delegation_ns_shorten;
4264 rh->private_local_zone = *zone;
4266 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4269 "Checking for TLD...\n");
4270 if (is_zkey_tld (name) == GNUNET_YES)
4272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4275 * This is a zkey tld
4276 * build hash and use as initial authority
4279 memset (rh->name, 0,
4280 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4281 memcpy (rh->name, name,
4282 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4283 pop_tld (rh->name, string_hash);
4285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4286 "ZKEY is %s!\n", string_hash);
4288 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4290 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4294 "Cannot convert ZKEY %s to hash!\n", nzkey);
4297 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4298 proc (proc_cls, name);
4302 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4305 &process_zone_to_name_zkey,
4312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4315 * Presumably GNUNET tld
4317 memset (rh->name, 0,
4318 strlen (name)-strlen (GNUNET_GNS_TLD));
4319 memcpy (rh->name, name,
4320 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4323 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4324 rh->authority_chain_tail = rh->authority_chain_head;
4325 rh->authority_chain_head->zone = *zone;
4328 /* Start delegation resolution in our namestore */
4329 resolve_delegation_ns (rh);
4332 /*********** END NAME SHORTEN ********************/
4335 finish_get_auth (struct ResolverHandle *rh,
4336 struct GetNameAuthorityHandle *nah)
4338 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4339 "Got authority result %s\n", nah->result);
4341 nah->proc(nah->proc_cls, nah->result);
4342 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4344 free_resolver_handle(rh);
4348 * Process result from namestore delegation lookup
4349 * for get authority operation
4351 * @param cls the client get auth handle
4352 * @param rh the resolver handle
4353 * @param rd_count number of results (0)
4354 * @param rd data (NULL)
4357 handle_delegation_result_ns_get_auth(void* cls,
4358 struct ResolverHandle *rh,
4360 const struct GNUNET_NAMESTORE_RecordData *rd)
4362 struct GetNameAuthorityHandle* nah;
4365 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4368 * At this point rh->name contains the part of the name
4369 * that we do not have a PKEY in our namestore to resolve.
4370 * The authority chain in the resolver handle is now
4371 * useful to backtrack if needed
4374 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4375 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4377 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4378 "Building response!\n");
4379 if (is_canonical(rh->name))
4382 * We successfully resolved the authority in the ns
4383 * FIXME for our purposes this is fine
4384 * but maybe we want to have an api that also looks
4385 * into the dht (i.e. option in message)
4387 if (strlen(rh->name) > strlen(nah->name))
4389 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4390 "Record name longer than original lookup name... odd!\n");
4394 answer_len = strlen(nah->name) - strlen(rh->name)
4395 + strlen(GNUNET_GNS_TLD) + 1;
4396 memset(nah->result, 0, answer_len);
4397 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4399 finish_get_auth (rh, nah);
4403 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4404 "Unable to resolve authority for remaining %s!\n", rh->name);
4405 strcpy(nah->result, "");
4406 finish_get_auth (rh, nah);
4414 * Tries to resolve the authority for name
4417 * @param zone the root zone to look up for
4418 * @param pzone the private local zone
4419 * @param name the name to lookup up
4420 * @param proc the processor to call when finished
4421 * @param proc_cls the closure to pass to the processor
4424 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4425 struct GNUNET_CRYPTO_ShortHashCode pzone,
4427 GetAuthorityResultProcessor proc,
4430 struct ResolverHandle *rh;
4431 struct GetNameAuthorityHandle *nah;
4433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4434 "Starting authority resolution for %s!\n", name);
4436 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4437 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4438 rh->authority = zone;
4440 rh->private_local_zone = pzone;
4441 rh->namestore_task = NULL;
4443 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4445 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4447 strcpy(rh->name, "\0");
4452 strlen(name)-strlen(GNUNET_GNS_TLD));
4453 memcpy(rh->name, name,
4454 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4457 memset(nah->name, 0,
4459 strcpy(nah->name, name);
4461 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4462 rh->authority_chain_tail = rh->authority_chain_head;
4463 rh->authority_chain_head->zone = zone;
4464 rh->proc = &handle_delegation_result_ns_get_auth;
4465 rh->proc_cls = (void*)nah;
4468 nah->proc_cls = proc_cls;
4469 strcpy (nah->result, "");
4471 /* Start delegation resolution in our namestore */
4472 resolve_delegation_ns(rh);
4476 /******** END GET AUTHORITY *************/
4478 /* end of gnunet-service-gns_resolver.c */