2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file gns/gnunet-service-gns_resolver.c
23 * @brief GNUnet GNS resolver logic
24 * @author Martin Schanzenbach
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_vpn_service.h"
33 #include "gnunet_dns_service.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "../dns/dnsparser.h"
37 #include "gnunet_gns_service.h"
38 #include "block_gns.h"
40 #include "gnunet-service-gns_resolver.h"
42 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
43 #define DHT_GNS_REPLICATION_LEVEL 5
44 #define MAX_DNS_LABEL_LENGTH 63
48 * Our handle to the namestore service
50 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
53 * Our handle to the vpn service
55 static struct GNUNET_VPN_Handle *vpn_handle;
58 * Resolver handle to the dht
60 static struct GNUNET_DHT_Handle *dht_handle;
63 * Heap for parallel DHT lookups
65 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
68 * Heap for namestore queues
70 static struct GNUNET_CONTAINER_Heap *ns_task_heap;
73 * Maximum amount of parallel queries in background
75 static unsigned long long max_allowed_background_queries;
78 * Maximum amount of parallel namestore tasks in background
80 static unsigned long long max_allowed_ns_tasks;
83 * Wheather or not to ignore pending records
85 static int ignore_pending_records;
90 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
93 * Background shortening handles
95 static struct GetPseuAuthorityHandle *gph_head;
98 * Background shortening handles
100 static struct GetPseuAuthorityHandle *gph_tail;
103 * Resolver lookup list
105 static struct ResolverHandle *rlh_head;
108 * Resolver lookup list
110 static struct ResolverHandle *rlh_tail;
113 * Resolver shorten list
115 static struct ResolverHandle *nsh_head;
118 * Resolver shorten list
120 static struct ResolverHandle *nsh_tail;
123 * Resolver get auth list
125 static struct ResolverHandle *nah_head;
128 * Resolver get auth list
130 static struct ResolverHandle *nah_tail;
133 * Global configuration.
135 static const struct GNUNET_CONFIGURATION_Handle *cfg;
138 * a resolution identifier pool variable
140 * This is a non critical identifier useful for debugging
142 static unsigned long long rid = 0;
145 * Check if name is in srv format (_x._y.xxx)
148 * @return GNUNET_YES if true
154 int ret = GNUNET_YES;
158 if (NULL == strstr (name, "._"))
161 ndup = GNUNET_strdup (name);
164 if (NULL == strtok (NULL, "."))
167 if (NULL == strtok (NULL, "."))
170 if (NULL != strtok (NULL, "."))
179 * Determine if this name is canonical.
181 * a.b.gnunet = not canonical
184 * @param name the name to test
185 * @return GNUNET_YES if canonical
188 is_canonical (char* name)
193 ndup = GNUNET_strdup (name);
196 for (tok = strtok (NULL, "."); tok != NULL; tok = strtok (NULL, "."))
212 * Callback that shortens authorities
214 * @param gph the handle containing the name to shorten
217 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
221 create_pseu_cont (void* cls, int32_t success, const char* emsg)
223 //FIXME do sth with error
224 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
225 struct AuthorityChain *iter;
227 gph->namestore_task = NULL;
230 iter = gph->ahead->next;
231 GNUNET_free (gph->ahead);
233 } while (iter != NULL);
234 GNUNET_CRYPTO_rsa_key_free (gph->key);
235 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
241 * Namestore calls this function if we have record for this name.
242 * (or with rd_count=0 to indicate no matches)
244 * @param cls the pending query
245 * @param key the key of the zone we did the lookup
246 * @param expiration expiration date of the namestore entry
247 * @param name the name for which we need an authority
248 * @param rd_count the number of records with 'name'
249 * @param rd the record data
250 * @param signature the signature of the authority for the record data
253 process_pseu_lookup_ns (void* cls,
254 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
255 struct GNUNET_TIME_Absolute expiration,
256 const char *name, unsigned int rd_count,
257 const struct GNUNET_NAMESTORE_RecordData *rd,
258 const struct GNUNET_CRYPTO_RsaSignature *signature)
260 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
261 struct GNUNET_NAMESTORE_RecordData new_pkey;
262 struct AuthorityChain *iter;
264 gph->namestore_task = NULL;
267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
268 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
269 if (0 == strcmp (gph->name, name))
271 if (gph->ahead->next != NULL)
273 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
277 iter = gph->ahead->next;
278 GNUNET_free (gph->ahead);
280 shorten_authority_chain (gph);
288 iter = gph->ahead->next;
289 GNUNET_free (gph->ahead);
291 } while (iter != NULL);
292 GNUNET_CRYPTO_rsa_key_free (gph->key);
293 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "GNS_AUTO_PSEU: Trying delegated name %s\n", gph->name);
300 memcpy (gph->test_name, gph->name, strlen (gph->name)+1);
301 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
304 GNUNET_NAMESTORE_TYPE_ANY,
305 &process_pseu_lookup_ns,
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
314 new_pkey.expiration_time = UINT64_MAX;
315 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
316 new_pkey.data = &gph->ahead->zone;
317 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
318 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
319 | GNUNET_NAMESTORE_RF_PRIVATE
320 | GNUNET_NAMESTORE_RF_PENDING;
321 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
325 &create_pseu_cont, //cont
330 * process result of a dht pseu lookup
332 * @param gph the handle
333 * @param name the pseu result or NULL
336 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
340 memcpy (gph->test_name, gph->ahead->name, strlen (gph->ahead->name)+1);
344 memcpy (gph->test_name, name, strlen(name)+1);
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
352 * Check for collision
354 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
357 GNUNET_NAMESTORE_TYPE_ANY,
358 &process_pseu_lookup_ns,
363 * Handle timeout for dht request
365 * @param cls the request handle as closure
366 * @param tc the task context
369 handle_auth_discovery_timeout(void *cls,
370 const struct GNUNET_SCHEDULER_TaskContext *tc)
372 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
373 struct AuthorityChain *iter;
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
377 GNUNET_DHT_get_stop (gph->get_handle);
378 gph->get_handle = NULL;
380 if (gph->ahead->next != NULL)
382 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
386 iter = gph->ahead->next;
387 GNUNET_free (gph->ahead);
389 shorten_authority_chain (gph);
394 process_pseu_result (gph, NULL);
398 * Function called when we find a PSEU entry in the DHT
400 * @param cls the request handle
401 * @param exp lifetime
402 * @param key the key the record was stored under
403 * @param get_path get path
404 * @param get_path_length get path length
405 * @param put_path put path
406 * @param put_path_length put path length
407 * @param type the block type
408 * @param size the size of the record
409 * @param data the record data
412 process_auth_discovery_dht_result(void* cls,
413 struct GNUNET_TIME_Absolute exp,
414 const struct GNUNET_HashCode * key,
415 const struct GNUNET_PeerIdentity *get_path,
416 unsigned int get_path_length,
417 const struct GNUNET_PeerIdentity *put_path,
418 unsigned int put_path_length,
419 enum GNUNET_BLOCK_Type type,
420 size_t size, const void *data)
422 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
423 struct AuthorityChain *iter;
424 struct GNSNameRecordBlock *nrb;
425 char* rd_data = (char*)data;
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
435 /* stop lookup and timeout task */
436 GNUNET_DHT_get_stop (gph->get_handle);
437 gph->get_handle = NULL;
438 GNUNET_SCHEDULER_cancel (gph->timeout);
442 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
443 "GNS_GET_AUTH: got dht result null!\n", size);
447 iter = gph->ahead->next;
448 GNUNET_free (gph->ahead);
450 } while (iter != NULL);
451 GNUNET_CRYPTO_rsa_key_free (gph->key);
452 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
457 nrb = (struct GNSNameRecordBlock*)data;
461 nrb = (struct GNSNameRecordBlock*)data;
463 name = (char*)&nrb[1];
464 num_records = ntohl (nrb->rd_count);
466 struct GNUNET_NAMESTORE_RecordData rd[num_records];
468 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
469 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
471 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
477 "GNS_GET_AUTH: Error deserializing data!\n");
481 for (i=0; i < num_records; i++)
483 if ((strcmp (name, "+") == 0) &&
484 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
487 process_pseu_result (gph, (char*)rd[i].data);
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in dht!\n");
496 if (gph->ahead->next != NULL)
498 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
502 iter = gph->ahead->next;
503 GNUNET_free (gph->ahead);
505 shorten_authority_chain (gph);
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "GNS_GET_AUTH: finished shorten, no results!\n");
511 process_pseu_result (gph, NULL);
515 * Process PSEU discovery for shorten via namestore
517 * @param cls the GetPseuAuthorityHandle
518 * @param key the public key
519 * @param expiration recorddata expiration
520 * @param name the looked up name
521 * @param rd_count number of records in set
522 * @param rd record data
523 * @param signature the signature
526 process_auth_discovery_ns_result(void* cls,
527 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
528 struct GNUNET_TIME_Absolute expiration,
530 unsigned int rd_count,
531 const struct GNUNET_NAMESTORE_RecordData *rd,
532 const struct GNUNET_CRYPTO_RsaSignature *signature)
535 struct GNUNET_CRYPTO_ShortHashCode name_hash;
536 struct GNUNET_HashCode lookup_key;
537 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
538 struct GNUNET_HashCode name_hash_double;
539 struct GNUNET_HashCode zone_hash_double;
541 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
542 struct AuthorityChain *iter;
544 gph->namestore_task = NULL;
551 GNUNET_CRYPTO_short_hash ("+", strlen ("+"), &name_hash);
552 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
553 GNUNET_CRYPTO_short_hash_double (&gph->ahead->zone, &zone_hash_double);
554 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
555 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
559 "+", (char*)&lookup_key_string);
561 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
562 &handle_auth_discovery_timeout, gph);
564 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
566 GNUNET_assert (gph->get_handle == NULL);
568 gph->get_handle = GNUNET_DHT_get_start(dht_handle,
569 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
571 DHT_GNS_REPLICATION_LEVEL,
575 &process_auth_discovery_dht_result,
580 for (i=0; i < rd_count; i++)
582 if ((strcmp (name, "+") == 0) &&
583 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
586 process_pseu_result (gph, (char*)rd[i].data);
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
593 if (gph->ahead->next != NULL)
595 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
599 iter = gph->ahead->next;
600 GNUNET_free (gph->ahead);
602 shorten_authority_chain (gph);
607 process_pseu_result (gph, NULL);
611 * Callback called by namestore for a zone to name
614 * @param cls the closure
615 * @param zone_key the zone we queried
616 * @param expire the expiration time of the name
617 * @param name the name found or NULL
618 * @param rd_len number of records for the name
619 * @param rd the record data (PKEY) for the name
620 * @param signature the signature for the record data
623 process_zone_to_name_discover (void *cls,
624 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
625 struct GNUNET_TIME_Absolute expire,
628 const struct GNUNET_NAMESTORE_RecordData *rd,
629 const struct GNUNET_CRYPTO_RsaSignature *signature)
631 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
632 struct AuthorityChain *iter;
634 gph->namestore_task = NULL;
635 /* we found a match in our own zone */
638 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
639 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
644 iter = gph->ahead->next;
645 GNUNET_free (gph->ahead);
647 } while (iter != NULL);
648 GNUNET_CRYPTO_rsa_key_free (gph->key);
649 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
655 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
658 GNUNET_GNS_RECORD_PSEU,
659 &process_auth_discovery_ns_result,
667 * Callback that shortens authorities
669 * @param gph the handle to the shorten request
672 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "GNS_AUTO_PSEU: New authority %s discovered\n",
679 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
682 &process_zone_to_name_discover,
688 start_shorten (struct AuthorityChain *atail,
689 struct GNUNET_CRYPTO_RsaPrivateKey *key)
691 struct AuthorityChain *new_head = NULL;
692 struct AuthorityChain *new_tail = NULL;
693 struct AuthorityChain *iter;
694 struct AuthorityChain *acopy;
695 struct GetPseuAuthorityHandle *gph;
696 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
697 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
699 /* First copy the authority chain in reverse order */
700 for (iter = atail; iter != NULL; iter = iter->prev)
702 acopy = GNUNET_malloc (sizeof (struct AuthorityChain));
703 memcpy (acopy, iter, sizeof (struct AuthorityChain));
706 GNUNET_CONTAINER_DLL_insert (new_head, new_tail, acopy);
709 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
711 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
712 pb_key = GNUNET_CRYPTO_rsa_encode_key (key);
713 gph->key = GNUNET_CRYPTO_rsa_decode_key ((char*)pb_key, ntohs (pb_key->len));
714 //gph->key = key;//GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
715 //memcpy (gph->key, key, sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
717 GNUNET_CRYPTO_short_hash (&pkey,
718 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
720 gph->ahead = new_head;
722 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
724 shorten_authority_chain (gph);
728 * Initialize the resolver
730 * @param nh the namestore handle
731 * @param dh the dht handle
732 * @param lz the local zone's hash
733 * @param c configuration handle
734 * @param max_bg_queries maximum number of parallel background queries in dht
735 * @param ignore_pending ignore records that still require user confirmation
737 * @return GNUNET_OK on success
740 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
741 struct GNUNET_DHT_Handle *dh,
742 struct GNUNET_CRYPTO_ShortHashCode lz,
743 const struct GNUNET_CONFIGURATION_Handle *c,
744 unsigned long long max_bg_queries,
748 namestore_handle = nh;
752 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
754 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
755 max_allowed_background_queries = max_bg_queries;
756 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
758 ignore_pending_records = ignore_pending;
769 GNUNET_RESOLVER_connect (cfg);
772 if ((namestore_handle != NULL) && (dht_handle != NULL))
777 return GNUNET_SYSERR;
784 * @param cls closure to iterator
785 * @param node heap nodes
786 * @param element the namestorebgtask
787 * @param cost heap cost
788 * @return always GNUNET_YES
791 cleanup_pending_ns_tasks(void* cls,
792 struct GNUNET_CONTAINER_HeapNode *node,
794 GNUNET_CONTAINER_HeapCostType cost)
796 struct NamestoreBGTask *nbg = (struct NamestoreBGTask *)element;
797 ResolverCleanupContinuation cont = cls;
799 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
800 "GNS_CLEANUP: Terminating ns task\n");
801 GNUNET_NAMESTORE_cancel (nbg->qe);
803 GNUNET_CONTAINER_heap_remove_node(node);
805 if (GNUNET_CONTAINER_heap_get_size(ns_task_heap) == 0)
815 * @param rh resolver handle
816 * @param rlh record lookup handle
817 * @param rd_count number of results
821 finish_lookup (struct ResolverHandle *rh,
822 struct RecordLookupHandle* rlh,
823 unsigned int rd_count,
824 const struct GNUNET_NAMESTORE_RecordData *rd);
828 * Cleanup background lookups FIXME get rid of this??
830 * @param cls closure to iterator
831 * @param node heap nodes
832 * @param element the resolver handle
833 * @param cost heap cost
834 * @return always GNUNET_YES
837 cleanup_pending_background_queries (void* cls,
838 struct GNUNET_CONTAINER_HeapNode *node,
840 GNUNET_CONTAINER_HeapCostType cost)
842 struct ResolverHandle *rh = (struct ResolverHandle *)element;
843 ResolverCleanupContinuation cont = cls;
845 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
846 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
849 //finish_lookup (rh, rh->proc_cls, 0, NULL);
850 //rh->get_handle = NULL;
851 //rh->proc(rh->proc_cls, rh, 0, NULL);
853 GNUNET_CONTAINER_heap_remove_node(node);
855 if (GNUNET_CONTAINER_heap_get_size(dht_lookup_heap) == 0)
857 if (GNUNET_CONTAINER_heap_get_size(ns_task_heap) == 0)
861 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
862 &cleanup_pending_ns_tasks,
873 * Helper function to free resolver handle
875 * @param rh the handle to free
878 free_resolver_handle (struct ResolverHandle* rh)
880 struct AuthorityChain *ac;
881 struct AuthorityChain *ac_next;
886 ac = rh->authority_chain_head;
895 if (NULL != rh->get_handle)
896 GNUNET_DHT_get_stop (rh->get_handle);
898 if (NULL != rh->dns_raw_packet)
899 GNUNET_free (rh->dns_raw_packet);
901 if (NULL != rh->namestore_task)
902 GNUNET_NAMESTORE_cancel (rh->namestore_task);
903 rh->namestore_task = NULL;
905 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
906 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
908 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
909 GNUNET_SCHEDULER_cancel (rh->timeout_task);
911 if (NULL != rh->dns_sock)
912 GNUNET_NETWORK_socket_close (rh->dns_sock);
913 if (NULL != rh->dns_resolver_handle)
914 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
916 if (NULL != rh->rd.data)
917 GNUNET_free ((void*)(rh->rd.data));
925 * @param rh resolver handle
926 * @param nsh name shorten handle
929 finish_shorten (struct ResolverHandle *rh,
930 struct NameShortenHandle *nsh);
934 * @param rh resolver handle
935 * @param nah get name authority handle
938 finish_get_auth (struct ResolverHandle *rh,
939 struct GetNameAuthorityHandle* rlh);
944 gns_resolver_cleanup (ResolverCleanupContinuation cont)
947 struct GetPseuAuthorityHandle *tmp;
948 struct AuthorityChain *iter;
952 for (tmp = gph_head; tmp != NULL; tmp = gph_head)
954 if (tmp->get_handle != NULL)
955 GNUNET_DHT_get_stop (tmp->get_handle);
956 tmp->get_handle = NULL;
957 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
958 GNUNET_SCHEDULER_cancel (tmp->timeout);
959 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
961 if (NULL != tmp->namestore_task)
962 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
963 tmp->namestore_task = NULL;
968 iter = tmp->ahead->next;
969 GNUNET_free (tmp->ahead);
971 } while (iter != NULL);
973 GNUNET_CRYPTO_rsa_key_free (tmp->key);
974 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, tmp);
978 while (NULL != rlh_head)
980 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
983 while (NULL != nsh_head)
985 finish_shorten (nsh_head, nsh_head->proc_cls);
988 while (NULL != nah_head)
990 finish_get_auth (nah_head, nah_head->proc_cls);
993 s = GNUNET_CONTAINER_heap_get_size(dht_lookup_heap);
994 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
995 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
997 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
998 &cleanup_pending_background_queries,
1000 else if (0 != GNUNET_CONTAINER_heap_get_size(ns_task_heap))
1002 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
1003 &cleanup_pending_ns_tasks,
1014 * Callback when record data is put into namestore
1016 * @param cls the closure
1017 * @param success GNUNET_OK on success
1018 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
1021 on_namestore_record_put_result(void *cls,
1025 struct NamestoreBGTask *nbg = cls;
1027 GNUNET_CONTAINER_heap_remove_node (nbg->node);
1030 if (GNUNET_NO == success)
1032 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1033 "GNS_NS: records already in namestore\n");
1036 else if (GNUNET_YES == success)
1038 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1039 "GNS_NS: records successfully put in namestore\n");
1043 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1044 "GNS_NS: Error putting records into namestore: %s\n", emsg);
1048 handle_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1050 struct ResolverHandle *rh = cls;
1052 if (rh->timeout_cont)
1053 rh->timeout_cont(rh->timeout_cont_cls, tc);
1057 * Processor for background lookups in the DHT
1059 * @param cls closure (NULL)
1060 * @param rd_count number of records found (not 0)
1061 * @param rd record data
1064 background_lookup_result_processor(void *cls,
1066 const struct GNUNET_NAMESTORE_RecordData *rd)
1068 //We could do sth verbose/more useful here but it doesn't make any difference
1069 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1070 "GNS_BG: background dht lookup finished. (%d results)\n",
1075 * Handle timeout for DHT requests
1077 * @param cls the request handle as closure
1078 * @param tc the task context
1081 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1083 struct ResolverHandle *rh = cls;
1084 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1085 char new_name[MAX_DNS_NAME_LENGTH];
1087 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1088 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
1089 rh->id, rh->name, rh->timeout.rel_value);
1091 * Start resolution in bg
1093 //strcpy(new_name, rh->name);
1094 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1095 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
1096 rh->name, GNUNET_GNS_TLD);
1098 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1099 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
1100 rh->id, new_name, rlh->record_type);
1102 gns_resolver_lookup_record(rh->authority,
1103 rh->private_local_zone,
1107 GNUNET_TIME_UNIT_FOREVER_REL,
1109 &background_lookup_result_processor,
1111 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1113 GNUNET_DHT_get_stop (rh->get_handle);
1114 rh->get_handle = NULL;
1115 rh->proc(rh->proc_cls, rh, 0, NULL);
1120 * Function called when we get a result from the dht
1121 * for our record query
1123 * @param cls the request handle
1124 * @param exp lifetime
1125 * @param key the key the record was stored under
1126 * @param get_path get path
1127 * @param get_path_length get path length
1128 * @param put_path put path
1129 * @param put_path_length put path length
1130 * @param type the block type
1131 * @param size the size of the record
1132 * @param data the record data
1135 process_record_result_dht(void* cls,
1136 struct GNUNET_TIME_Absolute exp,
1137 const struct GNUNET_HashCode * key,
1138 const struct GNUNET_PeerIdentity *get_path,
1139 unsigned int get_path_length,
1140 const struct GNUNET_PeerIdentity *put_path,
1141 unsigned int put_path_length,
1142 enum GNUNET_BLOCK_Type type,
1143 size_t size, const void *data)
1145 struct ResolverHandle *rh;
1146 struct RecordLookupHandle *rlh;
1147 struct GNSNameRecordBlock *nrb;
1148 uint32_t num_records;
1150 char* rd_data = (char*)data;
1154 rh = (struct ResolverHandle *)cls;
1155 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1156 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
1158 //FIXME maybe check expiration here, check block type
1161 rlh = (struct RecordLookupHandle *) rh->proc_cls;
1162 nrb = (struct GNSNameRecordBlock*)data;
1164 /* stop lookup and timeout task */
1165 GNUNET_DHT_get_stop (rh->get_handle);
1166 rh->get_handle = NULL;
1168 if (rh->dht_heap_node != NULL)
1170 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
1171 rh->dht_heap_node = NULL;
1174 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1176 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1177 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1180 rh->get_handle = NULL;
1181 name = (char*)&nrb[1];
1182 num_records = ntohl(nrb->rd_count);
1184 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1185 struct NamestoreBGTask *ns_heap_root;
1186 struct NamestoreBGTask *namestore_bg_task;
1188 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1189 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
1191 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1196 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1197 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1201 for (i=0; i<num_records; i++)
1203 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1204 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1205 rh->id, name, rh->name);
1206 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1207 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1208 rh->id, rd[i].record_type, rlh->record_type);
1209 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1210 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1211 rh->id, rd[i].data_size);
1212 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1213 "GNS_PHASE_REC-%llu: Got flag %d\n",
1214 rh->id, rd[i].flags);
1216 if ((strcmp(name, rh->name) == 0) &&
1217 (rd[i].record_type == rlh->record_type))
1225 * FIXME check pubkey against existing key in namestore?
1226 * https://gnunet.org/bugs/view.php?id=2179
1228 if (max_allowed_ns_tasks <=
1229 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1231 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1232 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1234 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1235 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1239 /* Save to namestore */
1240 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1241 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1248 &on_namestore_record_put_result, //cont
1251 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1253 GNUNET_TIME_absolute_get().abs_value);
1257 rh->proc(rh->proc_cls, rh, num_records, rd);
1259 rh->proc(rh->proc_cls, rh, 0, NULL);
1266 * Start DHT lookup for a (name -> query->record_type) record in
1267 * rh->authority's zone
1269 * @param rh the pending gns query context
1272 resolve_record_dht (struct ResolverHandle *rh)
1275 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1276 struct GNUNET_HashCode lookup_key;
1277 struct GNUNET_HashCode name_hash_double;
1278 struct GNUNET_HashCode zone_hash_double;
1279 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
1280 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1281 struct ResolverHandle *rh_heap_root;
1283 GNUNET_CRYPTO_short_hash(rh->name, strlen(rh->name), &name_hash);
1284 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1285 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
1286 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
1287 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
1289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1290 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1291 rh->id, rh->name, (char*)&lookup_key_string);
1293 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1294 rh->dht_heap_node = NULL;
1296 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1299 * Update timeout if necessary
1301 if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
1304 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1305 "GNS_PHASE_REC-%llu: Adjusting timeout\n", rh->id);
1307 * Set timeout for authority lookup phase to 1/2
1309 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
1310 GNUNET_TIME_relative_divide(rh->timeout, 2),
1311 &handle_lookup_timeout,
1314 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1315 // &dht_lookup_timeout,
1317 rh->timeout_cont = &dht_lookup_timeout;
1318 rh->timeout_cont_cls = rh;
1322 if (max_allowed_background_queries <=
1323 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1325 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1326 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
1327 rh_heap_root->get_handle = NULL;
1328 rh_heap_root->dht_heap_node = NULL;
1330 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1331 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1332 rh->id, rh_heap_root->name);
1333 rh_heap_root->proc(rh_heap_root->proc_cls,
1338 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1340 GNUNET_TIME_absolute_get().abs_value);
1343 xquery = htonl(rlh->record_type);
1345 GNUNET_assert(rh->get_handle == NULL);
1346 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1347 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1349 DHT_GNS_REPLICATION_LEVEL,
1353 &process_record_result_dht,
1360 * Namestore calls this function if we have record for this name.
1361 * (or with rd_count=0 to indicate no matches)
1363 * @param cls the pending query
1364 * @param key the key of the zone we did the lookup
1365 * @param expiration expiration date of the namestore entry
1366 * @param name the name for which we need an authority
1367 * @param rd_count the number of records with 'name'
1368 * @param rd the record data
1369 * @param signature the signature of the authority for the record data
1372 process_record_result_ns (void* cls,
1373 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1374 struct GNUNET_TIME_Absolute expiration,
1375 const char *name, unsigned int rd_count,
1376 const struct GNUNET_NAMESTORE_RecordData *rd,
1377 const struct GNUNET_CRYPTO_RsaSignature *signature)
1379 struct ResolverHandle *rh;
1380 struct RecordLookupHandle *rlh;
1381 struct GNUNET_TIME_Relative remaining_time;
1382 struct GNUNET_CRYPTO_ShortHashCode zone;
1383 struct GNUNET_TIME_Absolute et;
1386 rh = (struct ResolverHandle *) cls;
1387 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1389 rh->namestore_task = NULL;
1390 GNUNET_CRYPTO_short_hash(key,
1391 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1393 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1401 rh->status |= RSL_RECORD_EXISTS;
1403 if (remaining_time.rel_value == 0)
1405 rh->status |= RSL_RECORD_EXPIRED;
1412 * Lookup terminated and no results
1414 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1415 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1418 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1419 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1422 * Our zone and no result? Cannot resolve TT
1424 rh->proc(rh->proc_cls, rh, 0, NULL);
1430 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1431 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1433 for (i=0; i<rd_count;i++)
1435 if (rd[i].record_type != rlh->record_type)
1438 if (ignore_pending_records &&
1439 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1441 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1442 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1447 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1448 et.abs_value = rd[i].expiration_time;
1449 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
1452 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1453 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1463 if (rh->answered == 0)
1465 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1466 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1467 rh->proc(rh->proc_cls, rh, 0, NULL);
1471 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1472 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1473 rh->id, rh->answered, rd_count);
1475 rh->proc(rh->proc_cls, rh, rd_count, rd);
1481 * VPN redirect result callback
1483 * @param cls the resolver handle
1484 * @param af the requested address family
1485 * @param address in_addr(6) respectively
1488 process_record_result_vpn (void* cls, int af, const void *address)
1490 struct ResolverHandle *rh = cls;
1491 struct RecordLookupHandle *rlh;
1492 struct GNUNET_NAMESTORE_RecordData rd;
1494 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1496 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1497 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1501 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1502 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1504 if (rlh->record_type != GNUNET_GNS_RECORD_A)
1506 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1507 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1509 rh->proc (rh->proc_cls, rh, 0, NULL);
1512 rd.record_type = GNUNET_GNS_RECORD_A;
1513 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1515 rd.data_size = sizeof (struct in_addr);
1517 rh->proc (rh->proc_cls, rh, 1, &rd);
1520 else if (af == AF_INET6)
1522 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1523 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1525 if (rlh->record_type != GNUNET_GNS_RECORD_AAAA)
1527 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1528 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1530 rh->proc (rh->proc_cls, rh, 0, NULL);
1533 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1534 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1536 rd.data_size = sizeof (struct in6_addr);
1538 rh->proc (rh->proc_cls, rh, 1, &rd);
1542 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1543 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1545 rh->proc (rh->proc_cls, rh, 0, NULL);
1552 * Process VPN lookup result for record
1554 * @param cls the record lookup handle
1555 * @param rh resolver handle
1556 * @param rd_count number of results (1)
1557 * @param rd record data containing the result
1560 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1561 unsigned int rd_count,
1562 const struct GNUNET_NAMESTORE_RecordData *rd)
1564 struct RecordLookupHandle* rlh = (struct RecordLookupHandle*) cls;
1568 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1569 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1572 /* give up, cannot resolve */
1573 finish_lookup(rh, rlh, 0, NULL);
1577 /* results found yay */
1578 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1579 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1581 finish_lookup(rh, rlh, rd_count, rd);
1586 * Sends a UDP dns query to a nameserver specified in the rh
1588 * @param rh the resolver handle
1591 send_dns_packet (struct ResolverHandle *rh);
1595 handle_dns_resolver (void *cls,
1596 const struct sockaddr *addr,
1599 struct ResolverHandle *rh = cls;
1600 struct RecordLookupHandle *rlh = rh->proc_cls;
1601 struct GNUNET_NAMESTORE_RecordData rd;
1602 struct sockaddr_in *sai;
1603 struct sockaddr_in6 *sai6;
1607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1608 "No address found in DNS!\n");
1609 finish_lookup (rh, rlh, 0, NULL);
1613 if (addrlen == sizeof (struct sockaddr_in))
1615 sai = (struct sockaddr_in*) addr;
1616 rd.record_type = GNUNET_GNS_RECORD_A;
1617 rd.data_size = sizeof (struct in_addr);
1618 rd.data = &sai->sin_addr;
1622 sai6 = (struct sockaddr_in6*) addr;
1623 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1624 rd.data_size = sizeof (struct in6_addr);
1625 rd.data = &sai6->sin6_addr;
1628 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1631 finish_lookup (rh, rlh, 1, &rd);
1636 * Resolve DNS name via local stub resolver
1638 * @param rh the resolver handle
1641 resolve_dns_name (struct ResolverHandle *rh)
1643 struct RecordLookupHandle *rlh = rh->proc_cls;
1646 if ((rlh->record_type != GNUNET_GNS_RECORD_A) &&
1647 (rlh->record_type != GNUNET_GNS_RECORD_AAAA))
1649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1650 "Can only resolve A/AAAA via stub... abort\n");
1651 finish_lookup (rh, rlh, 0, NULL);
1655 if (rlh->record_type == GNUNET_GNS_RECORD_A)
1660 //GNUNET_RESOLVER_connect (cfg); FIXME into init
1662 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1665 &handle_dns_resolver,
1671 * Read DNS udp packet from socket
1673 * @param cls the resolver handle
1674 * @param tc task context
1677 read_dns_response (void *cls,
1678 const struct GNUNET_SCHEDULER_TaskContext *tc)
1680 struct ResolverHandle *rh = cls;
1681 struct RecordLookupHandle *rlh = rh->proc_cls;
1682 char buf[UINT16_MAX];
1684 struct sockaddr_in addr;
1686 struct GNUNET_DNSPARSER_Packet *packet;
1687 struct GNUNET_NAMESTORE_RecordData rd;
1688 int found_delegation = GNUNET_NO;
1689 int found_cname = GNUNET_NO;
1690 char* delegation_name = NULL;
1691 int zone_offset = 0;
1694 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1695 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1697 /* timeout or shutdown */
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1699 "Terminating DNS query %d\n", tc->reason);
1700 finish_lookup (rh, rlh, 0, NULL);
1704 addrlen = sizeof (addr);
1705 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1707 (struct sockaddr*) &addr,
1712 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1713 finish_lookup (rh, rlh, 0, NULL);
1717 packet = GNUNET_DNSPARSER_parse (buf, r);
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1722 "Failed to parse DNS reply!\n");
1723 finish_lookup (rh, rlh, 0, NULL);
1727 for (i = 0; i < packet->num_answers; i++)
1729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1730 "Got record type %d (want %d)\n",
1731 packet->answers[i].type,
1733 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1734 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1737 "CNAME... restarting query with %s\n",
1738 packet->answers[i].data.hostname
1740 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1741 found_cname = GNUNET_YES;
1742 //send_dns_packet (rh);
1743 //GNUNET_DNSPARSER_free_packet (packet);
1747 if ((packet->answers[i].type == rlh->record_type) &&
1748 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1752 rd.data = packet->answers[i].data.raw.data;
1753 rd.data_size = packet->answers[i].data.raw.data_len;
1754 rd.record_type = packet->answers[i].type;
1756 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1757 finish_lookup (rh, rlh, 1, &rd);
1758 GNUNET_DNSPARSER_free_packet (packet);
1763 if (GNUNET_YES == found_cname)
1765 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1767 if (0 > zone_offset)
1770 /* restart query with CNAME */
1771 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1774 "Asking same server for %s\n", rh->dns_name);
1775 send_dns_packet (rh);
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1780 "Trying system resolver for %s\n", rh->dns_name);
1781 resolve_dns_name (rh);
1784 GNUNET_DNSPARSER_free_packet (packet);
1788 for (i = 0; i < packet->num_authority_records; i++)
1791 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1794 "Found NS delegation!\n");
1795 found_delegation = GNUNET_YES;
1796 delegation_name = packet->authority_records[i].data.hostname;
1801 for (i = 0; i < packet->num_additional_records; i++)
1803 if (found_delegation == GNUNET_NO)
1806 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1807 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1809 GNUNET_assert (sizeof (struct in_addr) ==
1810 packet->authority_records[i].data.raw.data_len);
1812 rh->dns_addr.sin_addr =
1813 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1814 send_dns_packet (rh);
1815 GNUNET_DNSPARSER_free_packet (packet);
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1821 "Nothing useful in DNS reply!\n");
1822 finish_lookup (rh, rlh, 0, NULL);
1823 GNUNET_DNSPARSER_free_packet (packet);
1828 * Sends a UDP dns query to a nameserver specified in the rh
1830 * @param rh the request handle
1833 send_dns_packet (struct ResolverHandle *rh)
1835 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1836 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1839 "Sending %dbyte DNS query\n",
1840 rh->dns_raw_packet_size);
1842 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1844 rh->dns_raw_packet_size,
1845 (struct sockaddr*)&rh->dns_addr,
1846 sizeof (struct sockaddr_in));
1848 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1849 rh->timeout, //FIXME less?
1855 GNUNET_NETWORK_fdset_destroy (rset);
1860 * The final phase of resoution.
1861 * We found a NS RR and want to resolve via DNS
1863 * @param rh the pending lookup handle
1864 * @param rd_count length of record data
1865 * @param rd record data containing VPN RR
1868 resolve_record_dns (struct ResolverHandle *rh,
1870 const struct GNUNET_NAMESTORE_RecordData *rd)
1872 struct GNUNET_DNSPARSER_Query query;
1873 struct GNUNET_DNSPARSER_Packet packet;
1874 struct GNUNET_DNSPARSER_Flags flags;
1875 struct in_addr dnsip;
1876 struct sockaddr_in addr;
1877 struct sockaddr *sa;
1879 struct RecordLookupHandle *rlh = rh->proc_cls;
1881 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1883 /* We cancel here as to not include the ns lookup in the timeout */
1884 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1886 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1887 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1889 /* Start shortening */
1890 if ((rh->priv_key != NULL) &&
1891 (is_canonical (rh->name) == GNUNET_YES))
1893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1894 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1896 start_shorten (rh->authority_chain_tail,
1900 for (i = 0; i < rd_count; i++)
1902 /* Synthesize dns name */
1903 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
1905 strcpy (rh->dns_zone, (char*)rd[i].data);
1906 if (0 == strcmp (rh->name, ""))
1907 strcpy (rh->dns_name, (char*)rd[i].data);
1909 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1912 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
1913 dnsip = *((struct in_addr*)rd[i].data);
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1921 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1922 if (rh->dns_sock == NULL)
1924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1925 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1927 finish_lookup (rh, rlh, 0, NULL);
1931 memset (&addr, 0, sizeof (struct sockaddr_in));
1932 sa = (struct sockaddr *) &addr;
1933 sa->sa_family = AF_INET;
1934 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1936 sizeof (struct sockaddr_in)))
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1939 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1941 finish_lookup (rh, rlh, 0, NULL);
1945 /*TODO create dnsparser query, serialize, sendto, handle reply*/
1946 query.name = rh->dns_name;
1947 query.type = rlh->record_type;
1948 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1949 memset (&flags, 0, sizeof (flags));
1950 flags.recursion_desired = 1;
1951 flags.checking_disabled = 1;
1952 packet.queries = &query;
1953 packet.answers = NULL;
1954 packet.authority_records = NULL;
1955 packet.num_queries = 1;
1956 packet.num_answers = 0;
1957 packet.num_authority_records = 0;
1958 packet.num_additional_records = 0;
1959 packet.flags = flags;
1961 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1963 &rh->dns_raw_packet,
1964 &rh->dns_raw_packet_size))
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1969 GNUNET_NETWORK_socket_close (rh->dns_sock);
1970 finish_lookup (rh, rlh, 0, NULL);
1974 rh->dns_addr.sin_family = AF_INET;
1975 rh->dns_addr.sin_port = htons (53); //domain
1976 rh->dns_addr.sin_addr = dnsip;
1977 #if HAVE_SOCKADDR_IN_SIN_LEN
1978 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1981 send_dns_packet (rh);
1986 * The final phase of resoution.
1987 * We found a VPN RR and want to request an IPv4/6 address
1989 * @param rh the pending lookup handle
1990 * @param rd_count length of record data
1991 * @param rd record data containing VPN RR
1994 resolve_record_vpn (struct ResolverHandle *rh,
1996 const struct GNUNET_NAMESTORE_RecordData *rd)
1998 struct RecordLookupHandle *rlh = rh->proc_cls;
1999 struct GNUNET_HashCode serv_desc;
2000 struct vpn_data* vpn;
2003 /* We cancel here as to not include the ns lookup in the timeout */
2004 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2006 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2007 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2009 /* Start shortening */
2010 if ((rh->priv_key != NULL) &&
2011 (is_canonical (rh->name) == GNUNET_YES))
2013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2014 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
2016 start_shorten (rh->authority_chain_tail,
2020 vpn = (struct vpn_data*)rd->data;
2023 GNUNET_CRYPTO_hash ((char*)&vpn[1],
2024 strlen ((char*)&vpn[1]) + 1,
2026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2027 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
2030 GNUNET_h2s (&vpn->peer));
2032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2033 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
2036 GNUNET_h2s (&serv_desc));
2037 rh->proc = &handle_record_vpn;
2039 if (NULL == vpn_handle)
2041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2042 "GNS_PHASE_REC_VPN-%llu: VPN not connected!\n",
2044 finish_lookup (rh, rh->proc_cls, 0, NULL);
2048 if (rlh->record_type == GNUNET_GNS_RECORD_A)
2054 if (NULL == vpn_handle)
2056 vpn_handle = GNUNET_VPN_connect (cfg);
2057 if (NULL == vpn_handle)
2059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2060 "GNS_PHASE_INIT: Error connecting to VPN!\n");
2061 finish_lookup (rh, rh->proc_cls, 0, NULL);
2067 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
2068 af, ntohs (vpn->proto),
2069 (struct GNUNET_PeerIdentity *)&vpn->peer,
2072 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
2073 &process_record_result_vpn,
2079 * The final phase of resolution.
2080 * rh->name is a name that is canonical and we do not have a delegation.
2081 * Query namestore for this record
2083 * @param rh the pending lookup handle
2086 resolve_record_ns(struct ResolverHandle *rh)
2088 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
2090 /* We cancel here as to not include the ns lookup in the timeout */
2091 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2093 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2094 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2096 /* Start shortening */
2097 if ((rh->priv_key != NULL) &&
2098 (is_canonical (rh->name) == GNUNET_YES))
2100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2101 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
2103 start_shorten (rh->authority_chain_tail,
2108 * Try to resolve this record in our namestore.
2109 * The name to resolve is now in rh->authority_name
2110 * since we tried to resolve it to an authority
2113 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
2117 &process_record_result_ns,
2124 * Handle timeout for DHT requests
2126 * @param cls the request handle as closure
2127 * @param tc the task context
2130 dht_authority_lookup_timeout(void *cls,
2131 const struct GNUNET_SCHEDULER_TaskContext *tc)
2133 struct ResolverHandle *rh = cls;
2134 struct RecordLookupHandle *rlh = rh->proc_cls;
2135 char new_name[MAX_DNS_NAME_LENGTH];
2137 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2138 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
2139 rh->id, rh->authority_name, rh->timeout.rel_value);
2141 rh->status |= RSL_TIMED_OUT;
2143 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2145 if (NULL != rh->get_handle)
2146 GNUNET_DHT_get_stop (rh->get_handle);
2148 rh->get_handle = NULL;
2150 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
2152 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2153 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
2155 rh->proc(rh->proc_cls, rh, 0, NULL);
2159 if (strcmp(rh->name, "") == 0)
2162 * promote authority back to name and try to resolve record
2164 strcpy(rh->name, rh->authority_name);
2165 rh->proc(rh->proc_cls, rh, 0, NULL);
2170 * Start resolution in bg
2172 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
2173 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
2174 //strcpy(new_name, rh->name);
2175 //strcpy(new_name+strlen(new_name), ".");
2176 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
2178 strcpy(rh->name, new_name);
2180 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2181 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2182 rh->id, rh->name, rlh->record_type);
2184 gns_resolver_lookup_record(rh->authority,
2185 rh->private_local_zone,
2189 GNUNET_TIME_UNIT_FOREVER_REL,
2191 &background_lookup_result_processor,
2194 rh->proc(rh->proc_cls, rh, 0, NULL);
2198 static void resolve_delegation_dht(struct ResolverHandle *rh);
2201 static void resolve_delegation_ns(struct ResolverHandle *rh);
2205 * Namestore resolution for delegation finished. Processing result.
2207 * @param cls the closure
2208 * @param rh resolver handle
2209 * @param rd_count number of results (always 0)
2210 * @param rd record data (always NULL)
2213 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2214 unsigned int rd_count,
2215 const struct GNUNET_NAMESTORE_RecordData *rd);
2219 * This is a callback function that checks for key revocation
2221 * @param cls the pending query
2222 * @param key the key of the zone we did the lookup
2223 * @param expiration expiration date of the record data set in the namestore
2224 * @param name the name for which we need an authority
2225 * @param rd_count the number of records with 'name'
2226 * @param rd the record data
2227 * @param signature the signature of the authority for the record data
2230 process_pkey_revocation_result_ns (void *cls,
2231 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2232 struct GNUNET_TIME_Absolute expiration,
2234 unsigned int rd_count,
2235 const struct GNUNET_NAMESTORE_RecordData *rd,
2236 const struct GNUNET_CRYPTO_RsaSignature *signature)
2238 struct ResolverHandle *rh = cls;
2239 struct GNUNET_TIME_Relative remaining_time;
2242 rh->namestore_task = NULL;
2243 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2245 for (i = 0; i < rd_count; i++)
2247 if (rd[i].record_type == GNUNET_GNS_RECORD_REV)
2249 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2250 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2252 rh->status |= RSL_PKEY_REVOKED;
2253 rh->proc (rh->proc_cls, rh, 0, NULL);
2258 if ((name == NULL) ||
2259 (remaining_time.rel_value == 0))
2261 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2262 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2265 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2268 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2269 rh->id, "+.gnunet", GNUNET_GNS_RECORD_REV);
2271 gns_resolver_lookup_record(rh->authority,
2272 rh->private_local_zone,
2273 GNUNET_GNS_RECORD_REV,
2276 GNUNET_TIME_UNIT_FOREVER_REL,
2278 &background_lookup_result_processor,
2282 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2283 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2286 * We are done with PKEY resolution if name is empty
2287 * else resolve again with new authority
2289 if (strcmp (rh->name, "") == 0)
2290 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2292 resolve_delegation_ns (rh);
2299 * Callback when record data is put into namestore
2301 * @param cls the closure
2302 * @param success GNUNET_OK on success
2303 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2306 on_namestore_delegation_put_result(void *cls,
2310 struct NamestoreBGTask *nbg = cls;
2312 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2315 if (GNUNET_NO == success)
2317 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2318 "GNS_NS: records already in namestore\n");
2321 else if (GNUNET_YES == success)
2323 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2324 "GNS_NS: records successfully put in namestore\n");
2328 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2329 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2333 * Function called when we get a result from the dht
2334 * for our query. Recursively tries to resolve authorities
2337 * @param cls the request handle
2338 * @param exp lifetime
2339 * @param key the key the record was stored under
2340 * @param get_path get path
2341 * @param get_path_length get path length
2342 * @param put_path put path
2343 * @param put_path_length put path length
2344 * @param type the block type
2345 * @param size the size of the record
2346 * @param data the record data
2349 process_delegation_result_dht(void* cls,
2350 struct GNUNET_TIME_Absolute exp,
2351 const struct GNUNET_HashCode * key,
2352 const struct GNUNET_PeerIdentity *get_path,
2353 unsigned int get_path_length,
2354 const struct GNUNET_PeerIdentity *put_path,
2355 unsigned int put_path_length,
2356 enum GNUNET_BLOCK_Type type,
2357 size_t size, const void *data)
2359 struct ResolverHandle *rh;
2360 struct GNSNameRecordBlock *nrb;
2361 uint32_t num_records;
2363 char* rd_data = (char*) data;
2366 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
2367 struct GNUNET_HashCode zone_hash_double, name_hash_double;
2369 rh = (struct ResolverHandle *)cls;
2371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2372 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2377 nrb = (struct GNSNameRecordBlock*)data;
2379 /* stop dht lookup and timeout task */
2380 GNUNET_DHT_get_stop (rh->get_handle);
2382 rh->get_handle = NULL;
2384 if (rh->dht_heap_node != NULL)
2386 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2387 rh->dht_heap_node = NULL;
2390 num_records = ntohl(nrb->rd_count);
2391 name = (char*)&nrb[1];
2393 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2394 struct NamestoreBGTask *ns_heap_root;
2395 struct NamestoreBGTask *namestore_bg_task;
2397 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2398 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2400 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2405 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2406 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2411 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2412 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2413 rh->id, name, rh->authority_name);
2414 for (i=0; i<num_records; i++)
2417 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2418 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2419 rh->id, name, rh->authority_name);
2420 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2421 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2422 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2423 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2424 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2425 rh->id, rd[i].data_size);
2426 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2427 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2428 rh->id, rd[i].flags);
2430 if ((rd[i].record_type == GNUNET_GNS_RECORD_VPN) ||
2431 (rd[i].record_type == GNUNET_GNS_RECORD_NS) ||
2432 (rd[i].record_type == GNUNET_GNS_RECORD_CNAME))
2435 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2437 if (strcmp(rh->name, "") == 0)
2438 strcpy(rh->name, rh->authority_name);
2440 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2441 rh->name, rh->authority_name); //FIXME ret
2446 if ((strcmp(name, rh->authority_name) == 0) &&
2447 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
2449 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2450 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2453 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2454 struct AuthorityChain *auth =
2455 GNUNET_malloc(sizeof(struct AuthorityChain));
2456 auth->zone = rh->authority;
2457 memset(auth->name, 0, strlen(rh->authority_name)+1);
2458 strcpy(auth->name, rh->authority_name);
2459 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2460 rh->authority_chain_tail,
2463 if (NULL != rh->rd.data)
2464 GNUNET_free ((void*)rh->rd.data);
2466 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2467 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2468 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2471 /** try to import pkey if private key available */
2472 //if (rh->priv_key && is_canonical (rh->name))
2473 // process_discovered_authority(name, auth->zone,
2474 // rh->authority_chain_tail->zone,
2481 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2482 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2483 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2484 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2486 /* Save to namestore */
2487 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
2490 if (max_allowed_ns_tasks <=
2491 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2493 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2494 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2496 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2497 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
2501 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2503 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2505 GNUNET_TIME_absolute_get().abs_value);
2506 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2513 &on_namestore_delegation_put_result, //cont
2514 namestore_bg_task); //cls
2523 * FIXME in this case. should we ask namestore again?
2525 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2526 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2527 rh->id, rh->authority_name, rh->name);
2529 if (strcmp(rh->name, "") == 0)
2531 /* Start shortening */
2532 if ((rh->priv_key != NULL) &&
2533 (is_canonical (rh->name) == GNUNET_YES))
2535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2536 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2538 start_shorten (rh->authority_chain_tail,
2543 rh->proc = &handle_delegation_ns;
2546 /* Check for key revocation and delegate */
2547 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2550 GNUNET_GNS_RECORD_REV,
2551 &process_pkey_revocation_result_ns,
2558 * No pkey but name exists
2561 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2562 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2563 rh->id, rh->authority_name, rh->name);
2564 if (strcmp(rh->name, "") == 0)
2565 strcpy(rh->name, rh->authority_name);
2567 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2568 rh->name, rh->authority_name); //FIXME ret
2570 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2571 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2572 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2573 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2575 rh->proc(rh->proc_cls, rh, 0, NULL);
2578 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2579 +(MAX_DNS_NAME_LENGTH*2)
2580 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2581 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2585 expand_plus(char* dest, char* src, char* repl)
2588 unsigned int s_len = strlen(src)+1;
2590 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2591 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2595 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2596 "GNS_POSTPROCESS: %s to short\n", src);
2598 /* no postprocessing */
2599 memcpy(dest, src, s_len+1);
2603 if (0 == strcmp(src+s_len-3, ".+"))
2605 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2606 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2607 memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2611 pos += strlen(repl);
2612 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2613 "GNS_POSTPROCESS: Expanded to %s\n", dest);
2617 memcpy(dest, src, s_len+1);
2625 finish_lookup (struct ResolverHandle *rh,
2626 struct RecordLookupHandle* rlh,
2627 unsigned int rd_count,
2628 const struct GNUNET_NAMESTORE_RecordData *rd)
2631 char new_rr_data[MAX_DNS_NAME_LENGTH];
2632 char new_mx_data[MAX_MX_LENGTH];
2633 char new_soa_data[MAX_SOA_LENGTH];
2634 char new_srv_data[MAX_SRV_LENGTH];
2635 struct srv_data *old_srv;
2636 struct srv_data *new_srv;
2637 struct soa_data *old_soa;
2638 struct soa_data *new_soa;
2639 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2642 unsigned int offset;
2644 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2646 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2647 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2650 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2653 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2655 for (i = 0; i < rd_count; i++)
2658 if (rd[i].record_type != GNUNET_GNS_RECORD_NS &&
2659 rd[i].record_type != GNUNET_GNS_RECORD_PTR &&
2660 rd[i].record_type != GNUNET_GNS_RECORD_CNAME &&
2661 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
2662 rd[i].record_type != GNUNET_GNS_RECORD_SOA &&
2663 rd[i].record_type != GNUNET_GNS_RECORD_SRV)
2665 p_rd[i].data = rd[i].data;
2670 * for all those records we 'should'
2671 * also try to resolve the A/AAAA records (RFC1035)
2672 * This is a feature and not important
2675 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2676 "GNS_POSTPROCESS: Postprocessing\n");
2678 if (strcmp(rh->name, "+") == 0)
2679 repl_string = rlh->name;
2681 repl_string = rlh->name+strlen(rh->name)+1;
2684 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
2686 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2687 offset = sizeof(uint16_t);
2688 pos = new_mx_data+offset;
2689 expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2691 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2692 p_rd[i].data = new_mx_data;
2693 p_rd[i].data_size = offset;
2695 else if (rd[i].record_type == GNUNET_GNS_RECORD_SRV)
2698 * Prio, weight and port
2700 new_srv = (struct srv_data*)new_srv_data;
2701 old_srv = (struct srv_data*)rd[i].data;
2702 new_srv->prio = old_srv->prio;
2703 new_srv->weight = old_srv->weight;
2704 new_srv->port = old_srv->port;
2705 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2707 p_rd[i].data = new_srv_data;
2708 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2710 else if (rd[i].record_type == GNUNET_GNS_RECORD_SOA)
2712 /* expand mname and rname */
2713 old_soa = (struct soa_data*)rd[i].data;
2714 new_soa = (struct soa_data*)new_soa_data;
2715 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2716 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2717 offset = strlen ((char*)&new_soa[1]) + 1;
2718 expand_plus((char*)&new_soa[1] + offset,
2719 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2721 p_rd[i].data_size = sizeof (struct soa_data)
2723 + strlen ((char*)&new_soa[1] + offset);
2724 p_rd[i].data = new_soa_data;
2729 expand_plus(pos, (char*)rd[i].data, repl_string);
2730 p_rd[i].data_size = strlen(new_rr_data)+1;
2731 p_rd[i].data = new_rr_data;
2736 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2738 free_resolver_handle (rh);
2742 * Process DHT lookup result for record.
2744 * @param cls the closure
2745 * @param rh resolver handle
2746 * @param rd_count number of results
2747 * @param rd record data
2750 handle_record_dht(void* cls, struct ResolverHandle *rh,
2751 unsigned int rd_count,
2752 const struct GNUNET_NAMESTORE_RecordData *rd)
2754 struct RecordLookupHandle* rlh;
2756 rlh = (struct RecordLookupHandle*)cls;
2759 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2760 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2762 /* give up, cannot resolve */
2763 finish_lookup(rh, rlh, 0, NULL);
2767 /* results found yay */
2768 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2769 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2771 finish_lookup(rh, rlh, rd_count, rd);
2779 * Process namestore lookup result for record.
2781 * @param cls the closure
2782 * @param rh resolver handle
2783 * @param rd_count number of results
2784 * @param rd record data
2787 handle_record_ns (void* cls, struct ResolverHandle *rh,
2788 unsigned int rd_count,
2789 const struct GNUNET_NAMESTORE_RecordData *rd)
2791 struct RecordLookupHandle* rlh;
2792 rlh = (struct RecordLookupHandle*) cls;
2793 int check_dht = GNUNET_YES;
2797 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2798 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2803 * There are 5 conditions that have to met for us to consult the DHT:
2804 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2805 * 2. No entry in the NS existed AND
2806 * 3. The zone queried is not the local resolver's zone AND
2807 * 4. The name that was looked up is '+'
2808 * because if it was any other canonical name we either already queried
2809 * the DHT for the authority in the authority lookup phase (and thus
2810 * would already have an entry in the NS for the record)
2811 * 5. We are not in cache only mode
2813 if (((rh->status & RSL_RECORD_EXPIRED) != 0) &&
2814 ((rh->status & RSL_RECORD_EXISTS) == 0) )
2815 check_dht = GNUNET_NO;
2817 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2818 &rh->private_local_zone))
2819 check_dht = GNUNET_NO;
2821 if ((strcmp (rh->name, "+") != 0) && (is_srv (rh->name) == GNUNET_YES))
2822 check_dht = GNUNET_NO;
2825 if (rh->only_cached == GNUNET_YES)
2826 check_dht = GNUNET_NO;
2828 if (GNUNET_YES == check_dht)
2830 rh->proc = &handle_record_dht;
2831 resolve_record_dht(rh);
2834 /* give up, cannot resolve */
2835 finish_lookup(rh, rlh, 0, NULL);
2839 /* results found yay */
2840 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2841 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2843 finish_lookup(rh, rlh, rd_count, rd);
2850 * Move one level up in the domain hierarchy and return the
2851 * passed top level domain.
2853 * @param name the domain
2854 * @param dest the destination where the tld will be put
2857 pop_tld(char* name, char* dest)
2861 if (is_canonical (name) == GNUNET_YES)
2868 for (len = strlen(name); len > 0; len--)
2870 if (*(name+len) == '.')
2880 strcpy(dest, (name+len+1));
2884 * Checks if name is in tld
2886 * @param name the name to check
2887 * @param tld the TLD to check for
2888 * @return GNUNET_YES or GNUNET_NO
2891 is_tld(const char* name, const char* tld)
2895 if (strlen(name) <= strlen(tld))
2900 offset = strlen(name)-strlen(tld);
2901 if (strcmp(name+offset, tld) != 0)
2903 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2904 "%s is not in .%s TLD\n", name, tld);
2911 * DHT resolution for delegation finished. Processing result.
2913 * @param cls the closure
2914 * @param rh resolver handle
2915 * @param rd_count number of results (always 0)
2916 * @param rd record data (always NULL)
2919 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2920 unsigned int rd_count,
2921 const struct GNUNET_NAMESTORE_RecordData *rd)
2923 struct RecordLookupHandle* rlh;
2924 rlh = (struct RecordLookupHandle*) cls;
2927 if (strcmp(rh->name, "") == 0)
2929 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
2931 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2932 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2934 finish_lookup(rh, rlh, rd_count, rd);
2937 /* We resolved full name for delegation. resolving record */
2938 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2939 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2941 strcpy(rh->name, "+\0");
2942 rh->proc = &handle_record_ns;
2943 resolve_record_ns(rh);
2948 * we still have some left
2950 if (is_canonical (rh->name) == GNUNET_YES)
2952 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2953 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2956 rh->proc = &handle_record_ns;
2957 resolve_record_ns(rh);
2960 /* give up, cannot resolve */
2961 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2962 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2964 finish_lookup(rh, rlh, 0, NULL);
2969 * Start DHT lookup for a name -> PKEY (compare NS) record in
2970 * rh->authority's zone
2972 * @param rh the pending gns query
2975 resolve_delegation_dht(struct ResolverHandle *rh)
2978 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2979 struct GNUNET_HashCode name_hash_double;
2980 struct GNUNET_HashCode zone_hash_double;
2981 struct GNUNET_HashCode lookup_key;
2982 struct ResolverHandle *rh_heap_root;
2984 pop_tld(rh->name, rh->authority_name);
2985 GNUNET_CRYPTO_short_hash(rh->authority_name,
2986 strlen(rh->authority_name),
2988 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2989 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2990 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2992 rh->dht_heap_node = NULL;
2994 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2996 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
2997 // &dht_authority_lookup_timeout,
2999 rh->timeout_cont = &dht_authority_lookup_timeout;
3000 rh->timeout_cont_cls = rh;
3004 if (max_allowed_background_queries <=
3005 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
3007 /* terminate oldest lookup */
3008 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
3009 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
3010 rh_heap_root->get_handle = NULL;
3011 rh_heap_root->dht_heap_node = NULL;
3013 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3014 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
3015 rh->id, rh_heap_root->authority_name);
3017 rh_heap_root->proc(rh_heap_root->proc_cls,
3022 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
3024 GNUNET_TIME_absolute_get().abs_value);
3027 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
3029 GNUNET_assert(rh->get_handle == NULL);
3030 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
3031 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
3033 DHT_GNS_REPLICATION_LEVEL,
3037 &process_delegation_result_dht,
3044 * Namestore resolution for delegation finished. Processing result.
3046 * @param cls the closure
3047 * @param rh resolver handle
3048 * @param rd_count number of results (always 0)
3049 * @param rd record data (always NULL)
3052 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
3053 unsigned int rd_count,
3054 const struct GNUNET_NAMESTORE_RecordData *rd)
3056 struct RecordLookupHandle* rlh;
3057 rlh = (struct RecordLookupHandle*) cls;
3058 int check_dht = GNUNET_YES;
3061 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3062 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
3063 rh->id, rh->status);
3065 if (rh->status & RSL_PKEY_REVOKED)
3067 finish_lookup (rh, rlh, 0, NULL);
3071 if (strcmp(rh->name, "") == 0)
3074 /* We resolved full name for delegation. resolving record */
3075 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3076 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
3078 if (rh->status & RSL_CNAME_FOUND)
3080 if (rlh->record_type == GNUNET_GNS_RECORD_CNAME)
3082 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3083 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
3085 strcpy (rh->name, rh->authority_name);
3086 finish_lookup (rh, rlh, rd_count, rd);
3091 if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
3093 s_len = strlen (rd->data) - 2;
3094 memcpy (rh->name, rd->data, s_len);
3095 rh->name[s_len] = '\0';
3096 resolve_delegation_ns (rh);
3099 else if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
3101 gns_resolver_lookup_record (rh->authority,
3102 rh->private_local_zone,
3111 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3112 free_resolver_handle (rh);
3118 strcpy (rh->dns_name, (char*)rd->data);
3119 resolve_dns_name (rh);
3124 else if (rh->status & RSL_DELEGATE_VPN)
3126 if (rlh->record_type == GNUNET_GNS_RECORD_VPN)
3128 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3129 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
3131 finish_lookup(rh, rlh, rd_count, rd);
3134 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3135 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
3137 GNUNET_assert (NULL != rd);
3138 rh->proc = &handle_record_vpn;
3139 resolve_record_vpn (rh, rd_count, rd);
3142 else if (rh->status & RSL_DELEGATE_NS)
3144 if (rlh->record_type == GNUNET_GNS_RECORD_NS)
3146 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3147 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3149 finish_lookup(rh, rlh, rd_count, rd);
3153 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3154 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3156 GNUNET_assert (NULL != rd);
3157 rh->proc = &handle_record_ns;
3158 resolve_record_dns (rh, rd_count, rd);
3161 else if (rh->status & RSL_DELEGATE_PKEY)
3163 if (rh->status & RSL_PKEY_REVOKED)
3165 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3166 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
3168 finish_lookup (rh, rlh, 0, NULL);
3171 else if (rlh->record_type == GNUNET_GNS_RECORD_PKEY)
3173 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3174 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
3176 finish_lookup(rh, rlh, rd_count, rd);
3180 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3181 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3183 strcpy(rh->name, "+\0");
3184 rh->proc = &handle_record_ns;
3185 resolve_record_ns(rh);
3189 if (rh->status & RSL_DELEGATE_NS)
3191 if (rlh->record_type == GNUNET_GNS_RECORD_NS)
3193 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3194 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3196 finish_lookup(rh, rlh, rd_count, rd);
3200 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3201 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3203 GNUNET_assert (NULL != rd);
3204 rh->proc = &handle_record_ns;
3205 resolve_record_dns (rh, rd_count, rd);
3210 * we still have some left
3211 * check if authority in ns is fresh
3213 * or we are authority
3216 if ((rh->status & RSL_RECORD_EXISTS) &&
3217 !(rh->status & RSL_RECORD_EXPIRED))
3218 check_dht = GNUNET_NO;
3220 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3221 &rh->private_local_zone))
3222 check_dht = GNUNET_NO;
3224 if (rh->only_cached == GNUNET_YES)
3225 check_dht = GNUNET_NO;
3227 if (check_dht == GNUNET_NO)
3229 if (is_canonical (rh->name) == GNUNET_YES)
3231 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3232 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3235 rh->proc = &handle_record_ns;
3236 resolve_record_ns(rh);
3240 /* give up, cannot resolve */
3241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3242 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3245 finish_lookup(rh, rlh, rd_count, rd);
3246 //rlh->proc(rlh->proc_cls, 0, NULL);
3251 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3252 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3254 rh->proc = &handle_delegation_dht;
3255 resolve_delegation_dht(rh);
3260 * This is a callback function that should give us only PKEY
3261 * records. Used to query the namestore for the authority (PKEY)
3262 * for 'name'. It will recursively try to resolve the
3263 * authority for a given name from the namestore.
3265 * @param cls the pending query
3266 * @param key the key of the zone we did the lookup
3267 * @param expiration expiration date of the record data set in the namestore
3268 * @param name the name for which we need an authority
3269 * @param rd_count the number of records with 'name'
3270 * @param rd the record data
3271 * @param signature the signature of the authority for the record data
3274 process_delegation_result_ns (void* cls,
3275 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3276 struct GNUNET_TIME_Absolute expiration,
3278 unsigned int rd_count,
3279 const struct GNUNET_NAMESTORE_RecordData *rd,
3280 const struct GNUNET_CRYPTO_RsaSignature *signature)
3282 struct ResolverHandle *rh;
3283 struct GNUNET_TIME_Relative remaining_time;
3284 struct GNUNET_CRYPTO_ShortHashCode zone;
3285 char new_name[MAX_DNS_NAME_LENGTH];
3287 struct GNUNET_TIME_Absolute et;
3289 rh = (struct ResolverHandle *)cls;
3290 rh->namestore_task = NULL;
3291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3292 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3295 GNUNET_CRYPTO_short_hash (key,
3296 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3298 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3305 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3307 rh->status |= RSL_RECORD_EXISTS;
3309 if (remaining_time.rel_value == 0)
3311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3312 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3314 rh->status |= RSL_RECORD_EXPIRED;
3319 * No authority found in namestore.
3324 * We did not find an authority in the namestore
3329 * Promote this authority back to a name maybe it is
3332 if (strcmp (rh->name, "") == 0)
3334 /* simply promote back */
3335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3336 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3337 rh->id, rh->authority_name);
3338 strcpy (rh->name, rh->authority_name);
3342 /* add back to existing name */
3343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3344 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3345 rh->id, rh->authority_name, rh->name);
3346 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3347 rh->name, rh->authority_name);
3348 strcpy (rh->name, new_name);
3349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3350 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3354 rh->proc (rh->proc_cls, rh, 0, NULL);
3359 * We found an authority that may be able to help us
3360 * move on with query
3361 * Note only 1 pkey should have been returned.. anything else would be strange
3363 for (i=0; i < rd_count;i++)
3367 * A CNAME. Like regular DNS this means the is no other record for this
3370 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3372 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3373 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3376 rh->status |= RSL_CNAME_FOUND;
3377 rh->proc (rh->proc_cls, rh, rd_count, rd);
3384 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3386 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3387 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3389 rh->status |= RSL_DELEGATE_VPN;
3390 rh->proc (rh->proc_cls, rh, rd_count, rd);
3396 * FIXME make optional
3398 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3400 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3401 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3403 rh->status |= RSL_DELEGATE_NS;
3404 rh->proc (rh->proc_cls, rh, rd_count, rd);
3408 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3411 rh->status |= RSL_DELEGATE_PKEY;
3413 if ((ignore_pending_records != 0) &&
3414 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3417 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3423 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3424 et.abs_value = rd[i].expiration_time;
3425 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3429 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3431 if (remaining_time.rel_value == 0)
3433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3434 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3436 rh->authority_chain_head->fresh = 0;
3437 rh->proc (rh->proc_cls, rh, 0, NULL);
3445 * Resolve rest of query with new authority
3447 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3448 memcpy (&rh->authority, rd[i].data,
3449 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3450 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3451 auth->zone = rh->authority;
3452 memset (auth->name, 0, strlen (rh->authority_name)+1);
3453 strcpy (auth->name, rh->authority_name);
3454 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3455 rh->authority_chain_tail,
3457 if (NULL != rh->rd.data)
3458 GNUNET_free ((void*)(rh->rd.data));
3460 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3461 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3462 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3464 /* Check for key revocation and delegate */
3465 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3468 GNUNET_GNS_RECORD_REV,
3469 &process_pkey_revocation_result_ns,
3478 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3479 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3481 * If we have found some records for the LAST label
3482 * we return the results. Else null.
3484 if (strcmp (rh->name, "") == 0)
3486 /* Start shortening */
3487 if ((rh->priv_key != NULL) &&
3488 (is_canonical (rh->name) == GNUNET_YES))
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3491 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3493 start_shorten (rh->authority_chain_tail,
3496 /* simply promote back */
3497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3498 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3499 rh->id, rh->authority_name);
3500 strcpy (rh->name, rh->authority_name);
3501 rh->proc (rh->proc_cls, rh, rd_count, rd);
3505 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
3506 "%s.%s", rh->name, rh->authority_name);
3507 strcpy (rh->name, new_name);
3508 rh->proc (rh->proc_cls, rh, 0, NULL);
3514 * Resolve the delegation chain for the request in our namestore
3516 * @param rh the resolver handle
3519 resolve_delegation_ns (struct ResolverHandle *rh)
3521 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3522 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3524 pop_tld(rh->name, rh->authority_name);
3525 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3528 GNUNET_GNS_RECORD_ANY,
3529 &process_delegation_result_ns,
3536 * Lookup of a record in a specific zone
3537 * calls lookup result processor on result
3539 * @param zone the root zone
3540 * @param pzone the private local zone
3541 * @param record_type the record type to look up
3542 * @param name the name to look up
3543 * @param key a private key for use with PSEU import (can be NULL)
3544 * @param timeout timeout for resolution
3545 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3546 * @param proc the processor to call on result
3547 * @param cls the closure to pass to proc
3550 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3551 struct GNUNET_CRYPTO_ShortHashCode pzone,
3552 uint32_t record_type,
3554 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3555 struct GNUNET_TIME_Relative timeout,
3557 RecordLookupProcessor proc,
3560 struct ResolverHandle *rh;
3561 struct RecordLookupHandle* rlh;
3562 char string_hash[MAX_DNS_LABEL_LENGTH];
3563 char nzkey[MAX_DNS_LABEL_LENGTH];
3564 char* nzkey_ptr = nzkey;
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3567 "Starting resolution for %s (type=%d)!\n",
3571 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3572 (strcmp(GNUNET_GNS_TLD, name) != 0))
3574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3575 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3580 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3581 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3583 memset (rh, 0, sizeof (struct ResolverHandle));
3584 rh->authority = zone;
3588 rh->timeout = timeout;
3589 rh->get_handle = NULL;
3590 rh->private_local_zone = pzone;
3591 rh->only_cached = only_cached;
3592 rh->namestore_task = NULL;
3595 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3600 "No shorten key for resolution\n");
3603 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3606 * Set timeout for authority lookup phase to 1/2
3608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3609 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3610 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3611 GNUNET_TIME_relative_divide(timeout, 2),
3612 &handle_lookup_timeout,
3614 rh->timeout_cont = &dht_authority_lookup_timeout;
3615 rh->timeout_cont_cls = rh;
3619 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3620 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3623 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3626 * Only 'gnunet' given
3628 strcpy(rh->name, "\0");
3632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3633 "Checking for TLD...\n");
3634 if (is_zkey_tld(name) == GNUNET_YES)
3636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3639 * This is a zkey tld
3640 * build hash and use as initial authority
3643 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3644 memcpy(rh->name, name,
3645 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3646 pop_tld(rh->name, string_hash);
3648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3649 "ZKEY is %s!\n", string_hash);
3651 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3653 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3656 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3657 "Cannot convert ZKEY %s to hash!\n", string_hash);
3659 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3662 proc (cls, 0, NULL);
3667 else if (is_gnunet_tld (name) == GNUNET_YES)
3669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3672 * Presumably GNUNET tld
3674 memset (rh->name, 0,
3675 strlen(name)-strlen(GNUNET_GNS_TLD));
3676 memcpy (rh->name, name,
3677 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3681 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3682 "Cannot handle this TLD %s\n", string_hash);
3684 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3685 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3686 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3689 proc (cls, 0, NULL);
3695 * Initialize authority chain
3697 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3698 rh->authority_chain_head->prev = NULL;
3699 rh->authority_chain_head->next = NULL;
3700 rh->authority_chain_tail = rh->authority_chain_head;
3701 rh->authority_chain_head->zone = rh->authority;
3702 strcpy (rh->authority_chain_head->name, "");
3705 * Copy original query into lookup handle
3707 rlh->record_type = record_type;
3708 memset(rlh->name, 0, strlen(name) + 1);
3709 strcpy(rlh->name, name);
3711 rlh->proc_cls = cls;
3713 rh->proc = &handle_delegation_ns;
3714 resolve_delegation_ns(rh);
3717 /******** END Record Resolver ***********/
3720 finish_shorten (struct ResolverHandle *rh,
3721 struct NameShortenHandle *nsh)
3723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3724 "Sending %s as shorten result\n", nsh->result);
3725 nsh->proc (nsh->proc_cls, nsh->result);
3726 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3728 free_resolver_handle (rh);
3733 * Callback calles by namestore for a zone to name
3736 * @param cls the closure
3737 * @param zone_key the zone we queried
3738 * @param expire the expiration time of the name
3739 * @param name the name found or NULL
3740 * @param rd_len number of records for the name
3741 * @param rd the record data (PKEY) for the name
3742 * @param signature the signature for the record data
3745 process_zone_to_name_shorten_root (void *cls,
3746 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3747 struct GNUNET_TIME_Absolute expire,
3749 unsigned int rd_len,
3750 const struct GNUNET_NAMESTORE_RecordData *rd,
3751 const struct GNUNET_CRYPTO_RsaSignature *signature);
3755 * Callback called by namestore for a zone to name
3758 * @param cls the closure
3759 * @param zone_key the zone we queried
3760 * @param expire the expiration time of the name
3761 * @param name the name found or NULL
3762 * @param rd_len number of records for the name
3763 * @param rd the record data (PKEY) for the name
3764 * @param signature the signature for the record data
3767 process_zone_to_name_shorten_shorten (void *cls,
3768 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3769 struct GNUNET_TIME_Absolute expire,
3771 unsigned int rd_len,
3772 const struct GNUNET_NAMESTORE_RecordData *rd,
3773 const struct GNUNET_CRYPTO_RsaSignature *signature)
3775 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3776 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3777 struct AuthorityChain *next_authority;
3779 char result[MAX_DNS_NAME_LENGTH];
3780 char tmp_name[MAX_DNS_NAME_LENGTH];
3783 rh->namestore_task = NULL;
3784 /* we found a match in our own root zone */
3787 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3788 memset(result, 0, answer_len);
3790 if (strlen(rh->name) > 0)
3792 sprintf (result, "%s.%s.%s.%s",
3794 nsh->shorten_zone_name,
3799 sprintf (result, "%s.%s.%s", name,
3800 nsh->shorten_zone_name,
3804 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3805 "Found shorten result %s\n", result);
3806 if (strlen (nsh->result) > strlen (result))
3807 strcpy (nsh->result, result);
3809 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3810 nsh->shorten_zone) == 0)
3813 * This is our zone append .gnunet unless name is empty
3814 * (it shouldn't be, usually FIXME what happens if we
3815 * shorten to our zone to a "" record??)
3818 sprintf (result, "%s.%s.%s",
3820 nsh->shorten_zone_name,
3822 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3823 "Our zone: Found %s as shorten result\n", result);
3825 if (strlen (nsh->result) > strlen (result))
3826 strcpy (nsh->result, result);
3827 //nsh->proc(nsh->proc_cls, result);
3829 //free_resolver_handle(rh);
3836 * continue with next authority if exists
3838 if ((rh->authority_chain_head->next == NULL))
3840 finish_shorten (rh, nsh);
3843 next_authority = rh->authority_chain_head;
3845 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3846 "%s.%s", rh->name, next_authority->name);
3848 strcpy(rh->name, tmp_name);
3849 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3850 "No PSEU found for authority %s. Promoting back: %s\n",
3851 next_authority->name, rh->name);
3853 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3854 rh->authority_chain_tail,
3857 GNUNET_free (next_authority);
3859 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3860 &rh->authority_chain_tail->zone,
3861 &rh->authority_chain_head->zone,
3862 &process_zone_to_name_shorten_root,
3867 * Callback calles by namestore for a zone to name
3870 * @param cls the closure
3871 * @param zone_key the zone we queried
3872 * @param expire the expiration time of the name
3873 * @param name the name found or NULL
3874 * @param rd_len number of records for the name
3875 * @param rd the record data (PKEY) for the name
3876 * @param signature the signature for the record data
3879 process_zone_to_name_shorten_private (void *cls,
3880 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3881 struct GNUNET_TIME_Absolute expire,
3883 unsigned int rd_len,
3884 const struct GNUNET_NAMESTORE_RecordData *rd,
3885 const struct GNUNET_CRYPTO_RsaSignature *signature)
3887 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3888 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3889 struct AuthorityChain *next_authority;
3891 char result[MAX_DNS_NAME_LENGTH];
3892 char tmp_name[MAX_DNS_NAME_LENGTH];
3895 rh->namestore_task = NULL;
3896 /* we found a match in our own root zone */
3899 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3900 memset(result, 0, answer_len);
3902 if (strlen(rh->name) > 0)
3904 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3908 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3911 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3912 "Found shorten result %s\n", result);
3913 if (strlen (nsh->result) > strlen (result))
3914 strcpy (nsh->result, result);
3916 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3917 nsh->private_zone) == 0)
3920 * This is our zone append .gnunet unless name is empty
3921 * (it shouldn't be, usually FIXME what happens if we
3922 * shorten to our zone to a "" record??)
3925 sprintf (result, "%s.%s.%s",
3926 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3927 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3928 "Our private zone: Found %s as shorten result %s\n", result);
3929 if (strlen (nsh->result) > strlen (result))
3930 strcpy (nsh->result, result);
3933 if (0 != strcmp (nsh->shorten_zone_name, ""))
3935 /* backtrack authorities for names in priv zone */
3936 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3938 &rh->authority_chain_head->zone,
3939 &process_zone_to_name_shorten_shorten,
3946 * continue with next authority if exists
3948 if ((rh->authority_chain_head->next == NULL))
3950 finish_shorten (rh, nsh);
3953 next_authority = rh->authority_chain_head;
3955 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3956 "%s.%s", rh->name, next_authority->name);
3958 strcpy(rh->name, tmp_name);
3959 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3960 "No PSEU found for authority %s. Promoting back: %s\n",
3961 next_authority->name, rh->name);
3963 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3964 rh->authority_chain_tail,
3967 GNUNET_free (next_authority);
3969 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3970 &rh->authority_chain_tail->zone,
3971 &rh->authority_chain_head->zone,
3972 &process_zone_to_name_shorten_root,
3978 * Callback calles by namestore for a zone to name
3981 * @param cls the closure
3982 * @param zone_key the zone we queried
3983 * @param expire the expiration time of the name
3984 * @param name the name found or NULL
3985 * @param rd_len number of records for the name
3986 * @param rd the record data (PKEY) for the name
3987 * @param signature the signature for the record data
3990 process_zone_to_name_shorten_root (void *cls,
3991 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3992 struct GNUNET_TIME_Absolute expire,
3994 unsigned int rd_len,
3995 const struct GNUNET_NAMESTORE_RecordData *rd,
3996 const struct GNUNET_CRYPTO_RsaSignature *signature)
3998 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3999 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
4000 struct AuthorityChain *next_authority;
4002 char result[MAX_DNS_NAME_LENGTH];
4003 char tmp_name[MAX_DNS_NAME_LENGTH];
4006 rh->namestore_task = NULL;
4007 /* we found a match in our own root zone */
4010 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
4011 memset(result, 0, answer_len);
4013 if (strlen(rh->name) > 0)
4015 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
4019 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
4022 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4023 "Found shorten result %s\n", result);
4024 if (strlen (nsh->result) > strlen (result))
4025 strcpy (nsh->result, result);
4027 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4028 nsh->root_zone) == 0)
4031 * This is our zone append .gnunet unless name is empty
4032 * (it shouldn't be, usually FIXME what happens if we
4033 * shorten to our zone to a "" record??)
4036 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
4037 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4038 "Our zone: Found %s as shorten result\n", result);
4039 if (strlen (nsh->result) > strlen (result))
4040 strcpy (nsh->result, result);
4043 if (NULL != nsh->private_zone)
4045 /* backtrack authorities for names in priv zone */
4046 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4048 &rh->authority_chain_head->zone,
4049 &process_zone_to_name_shorten_private,
4052 else if (NULL != nsh->shorten_zone)
4054 /* backtrack authorities for names in shorten zone */
4055 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4057 &rh->authority_chain_head->zone,
4058 &process_zone_to_name_shorten_shorten,
4065 * continue with next authority if exists
4067 if ((rh->authority_chain_head->next == NULL))
4069 finish_shorten (rh, nsh);
4072 next_authority = rh->authority_chain_head;
4074 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
4075 "%s.%s", rh->name, next_authority->name);
4077 strcpy(rh->name, tmp_name);
4078 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4079 "No PSEU found for authority %s. Promoting back: %s\n",
4080 next_authority->name, rh->name);
4082 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
4083 rh->authority_chain_tail,
4086 GNUNET_free (next_authority);
4088 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4089 &rh->authority_chain_tail->zone,
4090 &rh->authority_chain_head->zone,
4091 &process_zone_to_name_shorten_root,
4098 * Process result from namestore delegation lookup
4099 * for shorten operation
4101 * @param cls the client shorten handle
4102 * @param rh the resolver handle
4103 * @param rd_count number of results (0)
4104 * @param rd data (NULL)
4107 handle_delegation_ns_shorten (void* cls,
4108 struct ResolverHandle *rh,
4110 const struct GNUNET_NAMESTORE_RecordData *rd)
4112 struct NameShortenHandle *nsh;
4113 char result[MAX_DNS_NAME_LENGTH];
4115 nsh = (struct NameShortenHandle *)cls;
4118 * At this point rh->name contains the part of the name
4119 * that we do not have a PKEY in our namestore to resolve.
4120 * The authority chain in the resolver handle is now
4121 * useful to backtrack if needed
4124 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4125 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4126 memset(result, 0, sizeof (result));
4128 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4129 nsh->root_zone) == 0)
4132 * This is our zone append .gnunet unless name is empty
4133 * (it shouldn't be, usually FIXME what happens if we
4134 * shorten to our zone to a "" record??)
4137 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
4138 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4139 "Our zone: Found %s as shorten result\n", result);
4141 if (strlen (nsh->result) > strlen (result))
4142 strcpy (nsh->result, result);
4145 else if (NULL != nsh->private_zone)
4148 * This is our zone append .gnunet unless name is empty
4149 * (it shouldn't be, usually FIXME what happens if we
4150 * shorten to our zone to a "" record??)
4152 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4153 nsh->private_zone) == 0)
4156 sprintf (result, "%s.%s.%s",
4157 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4159 "Our zone: Found %s as shorten result in private zone %s\n",
4162 if (strlen (nsh->result) > strlen (result))
4163 strcpy (nsh->result, result);
4166 else if (NULL != nsh->shorten_zone)
4169 * This is our zone append .gnunet unless name is empty
4170 * (it shouldn't be, usually FIXME what happens if we
4171 * shorten to our zone to a "" record??)
4173 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4174 nsh->shorten_zone) == 0)
4176 sprintf (result, "%s.%s.%s",
4177 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4178 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4179 "Our zone: Found %s as shorten result in shorten zone\n",
4182 if (strlen (nsh->result) > strlen (result))
4183 strcpy (nsh->result, result);
4188 /* backtrack authorities for names */
4189 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4191 &rh->authority_chain_head->zone,
4192 &process_zone_to_name_shorten_root,
4199 * Callback calles by namestore for a zone to name
4202 * @param cls the closure
4203 * @param zone_key the zone we queried
4204 * @param expire the expiration time of the name
4205 * @param name the name found or NULL
4206 * @param rd_len number of records for the name
4207 * @param rd the record data (PKEY) for the name
4208 * @param signature the signature for the record data
4211 process_zone_to_name_zkey(void *cls,
4212 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4213 struct GNUNET_TIME_Absolute expire,
4215 unsigned int rd_len,
4216 const struct GNUNET_NAMESTORE_RecordData *rd,
4217 const struct GNUNET_CRYPTO_RsaSignature *signature)
4219 struct ResolverHandle *rh = cls;
4220 struct NameShortenHandle *nsh = rh->proc_cls;
4221 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
4222 char new_name[MAX_DNS_NAME_LENGTH];
4224 /* zkey not in our zone */
4228 * In this case we have not given this PKEY a name (yet)
4229 * It is either just not in our zone or not even cached
4230 * Since we do not know at this point we will not try to shorten
4231 * because PKEY import will happen if the user follows the zkey
4234 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
4236 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4237 "No name found for zkey %s returning verbatim!\n", enc);
4238 if (strcmp(rh->name, "") != 0)
4239 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4240 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4242 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4243 enc, GNUNET_GNS_TLD_ZKEY);
4245 strcpy (nsh->result, new_name);
4247 finish_shorten (rh, nsh);
4251 if (strcmp(rh->name, "") != 0)
4252 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4255 strcpy(new_name, name);
4257 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4258 "Continue shorten for %s!\n", new_name);
4260 strcpy(rh->name, new_name);
4262 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4263 rh->authority_chain_tail = rh->authority_chain_head;
4264 rh->authority_chain_head->zone = rh->authority;
4267 /* Start delegation resolution in our namestore */
4268 resolve_delegation_ns (rh);
4273 * Shorten api from resolver
4275 * @param zone the root zone to use
4276 * @param pzone the private zone to use
4277 * @param szone the shorten zone to use
4278 * @param name the name to shorten
4279 * @param private_zone_name name of the private zone
4280 * @param shorten_zone_name name of the shorten zone
4281 * @param proc the processor to call with result
4282 * @param proc_cls closure to pass to proc
4285 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4286 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4287 struct GNUNET_CRYPTO_ShortHashCode *szone,
4289 const char* private_zone_name,
4290 const char* shorten_zone_name,
4291 ShortenResultProcessor proc,
4294 struct ResolverHandle *rh;
4295 struct NameShortenHandle *nsh;
4296 char string_hash[MAX_DNS_LABEL_LENGTH];
4297 struct GNUNET_CRYPTO_ShortHashCode zkey;
4298 char nzkey[MAX_DNS_LABEL_LENGTH];
4299 char* nzkey_ptr = nzkey;
4302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4303 "Starting shorten for %s!\n", name);
4305 if (is_canonical ((char*)name) == GNUNET_YES)
4307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4308 "%s is canonical. Returning verbatim\n", name);
4309 proc (proc_cls, name);
4313 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4316 nsh->proc_cls = proc_cls;
4317 nsh->root_zone = zone;
4318 nsh->private_zone = pzone;
4319 nsh->shorten_zone = szone;
4320 strcpy (nsh->private_zone_name, private_zone_name);
4321 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4322 strcpy (nsh->result, name);
4325 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4326 rh->authority = *zone;
4328 rh->priv_key = NULL;
4329 rh->namestore_task = NULL;
4330 rh->proc = &handle_delegation_ns_shorten;
4333 rh->private_local_zone = *zone;
4335 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4338 "Checking for TLD...\n");
4339 if (is_zkey_tld (name) == GNUNET_YES)
4341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4344 * This is a zkey tld
4345 * build hash and use as initial authority
4348 memset (rh->name, 0,
4349 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4350 memcpy (rh->name, name,
4351 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4352 pop_tld (rh->name, string_hash);
4354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4355 "ZKEY is %s!\n", string_hash);
4357 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4359 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4363 "Cannot convert ZKEY %s to hash!\n", nzkey);
4366 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4367 proc (proc_cls, name);
4371 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4374 &process_zone_to_name_zkey,
4379 else if (is_gnunet_tld (name) == GNUNET_YES)
4381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4384 * Presumably GNUNET tld
4386 memset (rh->name, 0,
4387 strlen (name)-strlen (GNUNET_GNS_TLD));
4388 memcpy (rh->name, name,
4389 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4393 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4396 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4397 proc (proc_cls, name);
4401 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4402 rh->authority_chain_tail = rh->authority_chain_head;
4403 rh->authority_chain_head->zone = *zone;
4406 /* Start delegation resolution in our namestore */
4407 resolve_delegation_ns (rh);
4410 /*********** END NAME SHORTEN ********************/
4413 * Conclude get authority lookup
4415 * @param rh resolver handle
4416 * @param nah get authority lookup handle
4419 finish_get_auth (struct ResolverHandle *rh,
4420 struct GetNameAuthorityHandle *nah)
4422 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4423 "Got authority result %s\n", nah->result);
4425 nah->proc(nah->proc_cls, nah->result);
4426 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4428 free_resolver_handle(rh);
4432 * Process result from namestore delegation lookup
4433 * for get authority operation
4435 * @param cls the client get auth handle
4436 * @param rh the resolver handle
4437 * @param rd_count number of results (0)
4438 * @param rd data (NULL)
4441 handle_delegation_result_ns_get_auth(void* cls,
4442 struct ResolverHandle *rh,
4444 const struct GNUNET_NAMESTORE_RecordData *rd)
4446 struct GetNameAuthorityHandle* nah;
4449 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4452 * At this point rh->name contains the part of the name
4453 * that we do not have a PKEY in our namestore to resolve.
4454 * The authority chain in the resolver handle is now
4455 * useful to backtrack if needed
4458 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4459 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4461 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4462 "Building response!\n");
4463 if (is_canonical (rh->name) == GNUNET_YES)
4466 * We successfully resolved the authority in the ns
4467 * FIXME for our purposes this is fine
4468 * but maybe we want to have an api that also looks
4469 * into the dht (i.e. option in message)
4471 if (strlen(rh->name) > strlen(nah->name))
4473 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4474 "Record name longer than original lookup name... odd!\n");
4478 answer_len = strlen(nah->name) - strlen(rh->name)
4479 + strlen(GNUNET_GNS_TLD) + 1;
4480 memset(nah->result, 0, answer_len);
4481 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4483 finish_get_auth (rh, nah);
4487 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4488 "Unable to resolve authority for remaining %s!\n", rh->name);
4489 strcpy(nah->result, "");
4490 finish_get_auth (rh, nah);
4498 * Tries to resolve the authority for name
4501 * @param zone the root zone to look up for
4502 * @param pzone the private local zone
4503 * @param name the name to lookup up
4504 * @param proc the processor to call when finished
4505 * @param proc_cls the closure to pass to the processor
4508 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4509 struct GNUNET_CRYPTO_ShortHashCode pzone,
4511 GetAuthorityResultProcessor proc,
4514 struct ResolverHandle *rh;
4515 struct GetNameAuthorityHandle *nah;
4517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4518 "Starting authority resolution for %s!\n", name);
4520 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4521 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4522 rh->authority = zone;
4524 rh->private_local_zone = pzone;
4525 rh->namestore_task = NULL;
4527 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4529 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4531 strcpy(rh->name, "\0");
4536 strlen(name)-strlen(GNUNET_GNS_TLD));
4537 memcpy(rh->name, name,
4538 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4541 memset(nah->name, 0,
4543 strcpy(nah->name, name);
4545 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4546 rh->authority_chain_tail = rh->authority_chain_head;
4547 rh->authority_chain_head->zone = zone;
4548 rh->proc = &handle_delegation_result_ns_get_auth;
4549 rh->proc_cls = (void*)nah;
4552 nah->proc_cls = proc_cls;
4553 strcpy (nah->result, "");
4555 /* Start delegation resolution in our namestore */
4556 resolve_delegation_ns(rh);
4560 /******** END GET AUTHORITY *************/
4562 /* end of gnunet-service-gns_resolver.c */