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 "gns_protocol.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.gads = not canonical
184 * @param name the name to test
185 * @return GNUNET_YES if canonical
188 is_canonical (char* name)
193 ndup = GNUNET_strdup (name);
196 for (tok = strtok (NULL, "."); tok != NULL; tok = strtok (NULL, "."))
212 * Callback that shortens authorities
214 * @param gph the handle containing the name to shorten
217 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
221 * Continueation for pkey record creation (shorten)
223 * @param cls a GetPseuAuthorityHandle
224 * @param success unused
228 create_pkey_cont (void* cls, int32_t success, const char* emsg)
230 //FIXME do sth with error
231 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
233 gph->namestore_task = NULL;
234 GNUNET_free (gph->auth);
235 GNUNET_CRYPTO_rsa_key_free (gph->key);
236 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
242 * Namestore calls this function if we have record for this name.
243 * (or with rd_count=0 to indicate no matches)
245 * @param cls the pending query
246 * @param key the key of the zone we did the lookup
247 * @param expiration expiration date of the namestore entry
248 * @param name the name for which we need an authority
249 * @param rd_count the number of records with 'name'
250 * @param rd the record data
251 * @param signature the signature of the authority for the record data
254 process_pseu_lookup_ns (void* cls,
255 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
256 struct GNUNET_TIME_Absolute expiration,
257 const char *name, unsigned int rd_count,
258 const struct GNUNET_NAMESTORE_RecordData *rd,
259 const struct GNUNET_CRYPTO_RsaSignature *signature)
261 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
262 struct GNUNET_NAMESTORE_RecordData new_pkey;
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 GNUNET_free (gph->auth);
270 GNUNET_CRYPTO_rsa_key_free (gph->key);
271 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
280 new_pkey.expiration_time = UINT64_MAX;
281 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
282 new_pkey.data = &gph->auth->zone;
283 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
284 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
285 | GNUNET_NAMESTORE_RF_PRIVATE
286 | GNUNET_NAMESTORE_RF_PENDING;
287 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
291 &create_pkey_cont, //cont
296 * process result of a dht pseu lookup
298 * @param gph the handle
299 * @param name the pseu result or NULL
302 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "GNS_AUTO_PSEU: No PSEU, no shorten. Finished.\n");
308 GNUNET_free (gph->auth);
309 GNUNET_CRYPTO_rsa_key_free (gph->key);
310 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
315 memcpy (gph->test_name, name, strlen(name)+1);
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
321 * Check for collision
323 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
326 GNUNET_NAMESTORE_TYPE_ANY,
327 &process_pseu_lookup_ns,
332 * Handle timeout for dht request
334 * @param cls the request handle as closure
335 * @param tc the task context
338 handle_auth_discovery_timeout (void *cls,
339 const struct GNUNET_SCHEDULER_TaskContext *tc)
341 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
345 GNUNET_DHT_get_stop (gph->get_handle);
346 gph->get_handle = NULL;
347 process_pseu_result (gph, NULL);
351 * Function called when we find a PSEU entry in the DHT
353 * @param cls the request handle
354 * @param exp lifetime
355 * @param key the key the record was stored under
356 * @param get_path get path
357 * @param get_path_length get path length
358 * @param put_path put path
359 * @param put_path_length put path length
360 * @param type the block type
361 * @param size the size of the record
362 * @param data the record data
365 process_auth_discovery_dht_result (void* cls,
366 struct GNUNET_TIME_Absolute exp,
367 const struct GNUNET_HashCode * key,
368 const struct GNUNET_PeerIdentity *get_path,
369 unsigned int get_path_length,
370 const struct GNUNET_PeerIdentity *put_path,
371 unsigned int put_path_length,
372 enum GNUNET_BLOCK_Type type,
376 struct GetPseuAuthorityHandle* gph = cls;
377 struct GNSNameRecordBlock *nrb;
378 char* rd_data = (char*)data;
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
387 /* stop lookup and timeout task */
388 GNUNET_DHT_get_stop (gph->get_handle);
389 gph->get_handle = NULL;
390 GNUNET_SCHEDULER_cancel (gph->timeout);
394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
395 "GNS_GET_AUTH: got dht result null!\n", size);
396 GNUNET_free (gph->auth);
397 GNUNET_CRYPTO_rsa_key_free (gph->key);
398 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
403 nrb = (struct GNSNameRecordBlock*)data;
404 name = (char*)&nrb[1];
405 num_records = ntohl (nrb->rd_count);
407 struct GNUNET_NAMESTORE_RecordData rd[num_records];
409 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
410 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
412 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
418 "GNS_GET_AUTH: Error deserializing data!\n");
422 for (i=0; i < num_records; i++)
424 if ((strcmp (name, "+") == 0) &&
425 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
428 process_pseu_result (gph, (char*)rd[i].data);
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "GNS_GET_AUTH: finished shorten, no results!\n");
436 process_pseu_result (gph, NULL);
440 * Process PSEU discovery for shorten via namestore
442 * @param cls the GetPseuAuthorityHandle
443 * @param key the public key
444 * @param expiration recorddata expiration
445 * @param name the looked up name
446 * @param rd_count number of records in set
447 * @param rd record data
448 * @param signature the signature
451 process_auth_discovery_ns_result (void* cls,
452 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
453 struct GNUNET_TIME_Absolute expiration,
455 unsigned int rd_count,
456 const struct GNUNET_NAMESTORE_RecordData *rd,
457 const struct GNUNET_CRYPTO_RsaSignature *signature)
459 struct GetPseuAuthorityHandle* gph = cls;
460 struct GNUNET_CRYPTO_ShortHashCode name_hash;
461 struct GNUNET_HashCode lookup_key;
462 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
463 struct GNUNET_HashCode name_hash_double;
464 struct GNUNET_HashCode zone_hash_double;
468 gph->namestore_task = NULL;
475 GNUNET_CRYPTO_short_hash ("+", strlen ("+"), &name_hash);
476 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
477 GNUNET_CRYPTO_short_hash_double (&gph->auth->zone, &zone_hash_double);
478 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
479 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
483 "+", (char*)&lookup_key_string);
485 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
486 &handle_auth_discovery_timeout, gph);
488 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
490 GNUNET_assert (gph->get_handle == NULL);
492 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
493 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
495 DHT_GNS_REPLICATION_LEVEL,
496 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
499 &process_auth_discovery_dht_result,
504 for (i=0; i < rd_count; i++)
506 if (0 != (strcmp (name, "+")))
509 if (rd[i].record_type != GNUNET_GNS_RECORD_PSEU)
513 process_pseu_result (gph, (char*)rd[i].data);
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
518 process_pseu_result (gph, NULL);
522 * Callback called by namestore for a zone to name
525 * @param cls the closure
526 * @param zone_key the zone we queried
527 * @param expire the expiration time of the name
528 * @param name the name found or NULL
529 * @param rd_len number of records for the name
530 * @param rd the record data (PKEY) for the name
531 * @param signature the signature for the record data
534 process_zone_to_name_discover (void *cls,
535 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
536 struct GNUNET_TIME_Absolute expire,
539 const struct GNUNET_NAMESTORE_RecordData *rd,
540 const struct GNUNET_CRYPTO_RsaSignature *signature)
542 struct GetPseuAuthorityHandle* gph = cls;
544 gph->namestore_task = NULL;
547 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
550 GNUNET_GNS_RECORD_PSEU,
551 &process_auth_discovery_ns_result,
555 /* we found a match in our own zone */
556 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
557 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
558 GNUNET_free (gph->auth);
559 GNUNET_CRYPTO_rsa_key_free (gph->key);
560 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
566 * Callback that shortens authorities
568 * @param gph the handle to the shorten request
571 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "GNS_AUTO_PSEU: New authority %s discovered\n",
578 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
581 &process_zone_to_name_discover,
587 start_shorten (struct AuthorityChain *auth,
588 struct GNUNET_CRYPTO_RsaPrivateKey *key)
590 struct GetPseuAuthorityHandle *gph;
591 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
592 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
594 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
595 pb_key = GNUNET_CRYPTO_rsa_encode_key (key);
599 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
600 "Failed to encode RSA key on shorten\n");
604 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
605 gph->key = GNUNET_CRYPTO_rsa_decode_key ((char*)pb_key, ntohs (pb_key->len));
607 if (NULL == gph->key)
609 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
610 "Failed to decode RSA key on shorten\n");
614 GNUNET_CRYPTO_short_hash (&pkey,
615 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
617 gph->auth = GNUNET_malloc (sizeof (struct AuthorityChain));
618 memcpy (gph->auth, auth, sizeof (struct AuthorityChain));
619 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
620 shorten_authority_chain (gph);
624 * Initialize the resolver
626 * @param nh the namestore handle
627 * @param dh the dht handle
628 * @param lz the local zone's hash
629 * @param c configuration handle
630 * @param max_bg_queries maximum number of parallel background queries in dht
631 * @param ignore_pending ignore records that still require user confirmation
633 * @return GNUNET_OK on success
636 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
637 struct GNUNET_DHT_Handle *dh,
638 struct GNUNET_CRYPTO_ShortHashCode lz,
639 const struct GNUNET_CONFIGURATION_Handle *c,
640 unsigned long long max_bg_queries,
644 return GNUNET_SYSERR;
646 return GNUNET_SYSERR;
649 namestore_handle = nh;
653 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
655 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
656 max_allowed_background_queries = max_bg_queries;
657 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
658 ignore_pending_records = ignore_pending;
668 GNUNET_RESOLVER_connect (cfg);
676 * @param cls closure to iterator
677 * @param node heap nodes
678 * @param element the namestorebgtask
679 * @param cost heap cost
680 * @return always GNUNET_YES
683 cleanup_pending_ns_tasks (void* cls,
684 struct GNUNET_CONTAINER_HeapNode *node,
686 GNUNET_CONTAINER_HeapCostType cost)
688 struct NamestoreBGTask *nbg = element;
689 ResolverCleanupContinuation cont = cls;
691 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
692 "GNS_CLEANUP: Terminating ns task\n");
693 GNUNET_NAMESTORE_cancel (nbg->qe);
695 GNUNET_CONTAINER_heap_remove_node (node);
697 if (0 == GNUNET_CONTAINER_heap_get_size (ns_task_heap))
707 * @param rh resolver handle
708 * @param rlh record lookup handle
709 * @param rd_count number of results
713 finish_lookup (struct ResolverHandle *rh,
714 struct RecordLookupHandle* rlh,
715 unsigned int rd_count,
716 const struct GNUNET_NAMESTORE_RecordData *rd);
720 * Cleanup background lookups FIXME get rid of this?? YES this doesn't do
721 * anything! => find in code and remove all references to the heap
723 * @param cls closure to iterator
724 * @param node heap nodes
725 * @param element the resolver handle
726 * @param cost heap cost
727 * @return always GNUNET_YES
730 cleanup_pending_background_queries (void* cls,
731 struct GNUNET_CONTAINER_HeapNode *node,
733 GNUNET_CONTAINER_HeapCostType cost)
735 struct ResolverHandle *rh = element;
736 ResolverCleanupContinuation cont = cls;
738 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
739 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
741 GNUNET_CONTAINER_heap_remove_node (node);
742 if (0 == GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
744 if (GNUNET_CONTAINER_heap_get_size (ns_task_heap) == 0)
748 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
749 &cleanup_pending_ns_tasks,
760 * Helper function to free resolver handle
762 * @param rh the handle to free
765 free_resolver_handle (struct ResolverHandle* rh)
767 struct AuthorityChain *ac;
768 struct AuthorityChain *ac_next;
773 ac = rh->authority_chain_head;
782 if (NULL != rh->get_handle)
783 GNUNET_DHT_get_stop (rh->get_handle);
785 if (NULL != rh->dns_raw_packet)
786 GNUNET_free (rh->dns_raw_packet);
788 if (NULL != rh->namestore_task)
789 GNUNET_NAMESTORE_cancel (rh->namestore_task);
790 rh->namestore_task = NULL;
792 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
793 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
795 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
796 GNUNET_SCHEDULER_cancel (rh->timeout_task);
798 if (NULL != rh->dns_sock)
799 GNUNET_NETWORK_socket_close (rh->dns_sock);
800 if (NULL != rh->dns_resolver_handle)
801 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
803 if (NULL != rh->rd.data)
804 GNUNET_free ((void*)(rh->rd.data));
812 * @param rh resolver handle
813 * @param nsh name shorten handle
816 finish_shorten (struct ResolverHandle *rh,
817 struct NameShortenHandle *nsh);
821 * @param rh resolver handle
822 * @param nah get name authority handle
825 finish_get_auth (struct ResolverHandle *rh,
826 struct GetNameAuthorityHandle* rlh);
831 gns_resolver_cleanup (ResolverCleanupContinuation cont)
834 struct GetPseuAuthorityHandle *tmp;
838 for (tmp = gph_head; tmp != NULL; tmp = gph_head)
840 if (tmp->get_handle != NULL)
841 GNUNET_DHT_get_stop (tmp->get_handle);
842 tmp->get_handle = NULL;
843 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
844 GNUNET_SCHEDULER_cancel (tmp->timeout);
845 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
847 if (NULL != tmp->namestore_task)
848 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
849 tmp->namestore_task = NULL;
850 GNUNET_free (tmp->auth);
851 GNUNET_CRYPTO_rsa_key_free (tmp->key);
852 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, tmp);
856 while (NULL != rlh_head)
858 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
860 while (NULL != nsh_head)
862 finish_shorten (nsh_head, nsh_head->proc_cls);
864 while (NULL != nah_head)
866 finish_get_auth (nah_head, nah_head->proc_cls);
869 s = GNUNET_CONTAINER_heap_get_size (dht_lookup_heap);
870 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
871 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
873 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
874 &cleanup_pending_background_queries,
876 else if (0 != GNUNET_CONTAINER_heap_get_size (ns_task_heap))
878 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
879 &cleanup_pending_ns_tasks,
890 * Callback when record data is put into namestore
892 * @param cls the closure
893 * @param success GNUNET_OK on success
894 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
897 on_namestore_record_put_result (void *cls,
901 struct NamestoreBGTask *nbg = cls;
903 GNUNET_CONTAINER_heap_remove_node (nbg->node);
906 if (GNUNET_NO == success)
908 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
909 "GNS_NS: records already in namestore\n");
912 else if (GNUNET_YES == success)
914 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
915 "GNS_NS: records successfully put in namestore\n");
919 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
920 "GNS_NS: Error putting records into namestore: %s\n", emsg);
925 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
927 struct ResolverHandle *rh = cls;
929 if (NULL != rh->timeout_cont)
930 rh->timeout_cont (rh->timeout_cont_cls, tc);
931 /* FIXME: does this leak memory? */
936 * Processor for background lookups in the DHT
938 * @param cls closure (NULL)
939 * @param rd_count number of records found (not 0)
940 * @param rd record data
943 background_lookup_result_processor (void *cls,
945 const struct GNUNET_NAMESTORE_RecordData *rd)
947 //We could do sth verbose/more useful here but it doesn't make any difference
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
949 "GNS_BG: background dht lookup finished. (%d results)\n",
954 * Handle timeout for DHT requests
956 * @param cls the request handle as closure
957 * @param tc the task context
960 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
962 struct ResolverHandle *rh = cls;
963 struct RecordLookupHandle *rlh = rh->proc_cls;
964 char new_name[MAX_DNS_NAME_LENGTH];
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
968 rh->id, rh->name, rh->timeout.rel_value);
970 * Start resolution in bg
972 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
973 rh->name, GNUNET_GNS_TLD);
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
976 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
977 rh->id, new_name, rlh->record_type);
979 gns_resolver_lookup_record (rh->authority,
980 rh->private_local_zone,
984 GNUNET_TIME_UNIT_FOREVER_REL,
986 &background_lookup_result_processor,
989 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
991 GNUNET_DHT_get_stop (rh->get_handle);
992 rh->get_handle = NULL;
993 rh->proc (rh->proc_cls, rh, 0, NULL);
998 * Function called when we get a result from the dht
999 * for our record query
1001 * @param cls the request handle
1002 * @param exp lifetime
1003 * @param key the key the record was stored under
1004 * @param get_path get path
1005 * @param get_path_length get path length
1006 * @param put_path put path
1007 * @param put_path_length put path length
1008 * @param type the block type
1009 * @param size the size of the record
1010 * @param data the record data
1013 process_record_result_dht (void* cls,
1014 struct GNUNET_TIME_Absolute exp,
1015 const struct GNUNET_HashCode * key,
1016 const struct GNUNET_PeerIdentity *get_path,
1017 unsigned int get_path_length,
1018 const struct GNUNET_PeerIdentity *put_path,
1019 unsigned int put_path_length,
1020 enum GNUNET_BLOCK_Type type,
1021 size_t size, const void *data)
1023 struct ResolverHandle *rh;
1024 struct RecordLookupHandle *rlh;
1025 struct GNSNameRecordBlock *nrb;
1026 uint32_t num_records;
1028 char* rd_data = (char*)data;
1032 rh = (struct ResolverHandle *)cls;
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1034 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
1036 rlh = (struct RecordLookupHandle *) rh->proc_cls;
1037 nrb = (struct GNSNameRecordBlock*)data;
1039 /* stop lookup and timeout task */
1040 GNUNET_DHT_get_stop (rh->get_handle);
1041 rh->get_handle = NULL;
1043 if (rh->dht_heap_node != NULL)
1045 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1046 rh->dht_heap_node = NULL;
1049 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1051 GNUNET_SCHEDULER_cancel (rh->timeout_task);
1052 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1055 rh->get_handle = NULL;
1056 name = (char*)&nrb[1];
1057 num_records = ntohl (nrb->rd_count);
1059 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1060 struct NamestoreBGTask *ns_heap_root;
1061 struct NamestoreBGTask *namestore_bg_task;
1063 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
1064 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
1066 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1071 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1072 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1073 rh->proc (rh->proc_cls, rh, 0, NULL);
1077 for (i=0; i<num_records; i++)
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1081 rh->id, name, rh->name);
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1083 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1084 rh->id, rd[i].record_type, rlh->record_type);
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1087 rh->id, rd[i].data_size);
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1089 "GNS_PHASE_REC-%llu: Got flag %d\n",
1090 rh->id, rd[i].flags);
1092 if ((strcmp (name, rh->name) == 0) &&
1093 (rd[i].record_type == rlh->record_type))
1101 * FIXME check pubkey against existing key in namestore?
1102 * https://gnunet.org/bugs/view.php?id=2179
1104 if (max_allowed_ns_tasks <=
1105 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1107 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1108 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1115 /* Save to namestore */
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 "GNS_PHASE_REC-%llu: Caching record for %s\n",
1119 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1120 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1127 &on_namestore_record_put_result, //cont
1130 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1132 GNUNET_TIME_absolute_get().abs_value);
1135 if (0 < rh->answered)
1136 rh->proc (rh->proc_cls, rh, num_records, rd);
1138 rh->proc (rh->proc_cls, rh, 0, NULL);
1145 * Start DHT lookup for a (name -> query->record_type) record in
1146 * rh->authority's zone
1148 * @param rh the pending gns query context
1151 resolve_record_dht (struct ResolverHandle *rh)
1154 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1155 struct GNUNET_HashCode lookup_key;
1156 struct GNUNET_HashCode name_hash_double;
1157 struct GNUNET_HashCode zone_hash_double;
1158 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
1159 struct RecordLookupHandle *rlh = rh->proc_cls;
1160 struct ResolverHandle *rh_heap_root;
1162 GNUNET_CRYPTO_short_hash (rh->name, strlen (rh->name), &name_hash);
1163 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
1164 GNUNET_CRYPTO_short_hash_double (&rh->authority, &zone_hash_double);
1165 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
1166 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
1168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1169 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1170 rh->id, rh->name, (char*)&lookup_key_string);
1172 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1173 rh->dht_heap_node = NULL;
1175 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
1178 * Update timeout if necessary
1180 if (GNUNET_SCHEDULER_NO_TASK == rh->timeout_task)
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "GNS_PHASE_REC-%llu: Adjusting timeout\n", rh->id);
1185 * Set timeout for authority lookup phase to 1/2
1187 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1188 GNUNET_TIME_relative_divide (rh->timeout, 2),
1189 &handle_lookup_timeout,
1192 rh->timeout_cont = &dht_lookup_timeout;
1193 rh->timeout_cont_cls = rh;
1197 if (max_allowed_background_queries <=
1198 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1200 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1201 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1202 rh_heap_root->get_handle = NULL;
1203 rh_heap_root->dht_heap_node = NULL;
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1206 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1207 rh->id, rh_heap_root->name);
1208 rh_heap_root->proc (rh_heap_root->proc_cls,
1213 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1215 GNUNET_TIME_absolute_get ().abs_value);
1218 xquery = htonl (rlh->record_type);
1220 GNUNET_assert (rh->get_handle == NULL);
1221 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1222 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1224 DHT_GNS_REPLICATION_LEVEL,
1225 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1228 &process_record_result_dht,
1235 * Namestore calls this function if we have record for this name.
1236 * (or with rd_count=0 to indicate no matches)
1238 * @param cls the pending query
1239 * @param key the key of the zone we did the lookup
1240 * @param expiration expiration date of the namestore entry
1241 * @param name the name for which we need an authority
1242 * @param rd_count the number of records with 'name'
1243 * @param rd the record data
1244 * @param signature the signature of the authority for the record data
1247 process_record_result_ns (void* cls,
1248 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1249 struct GNUNET_TIME_Absolute expiration,
1250 const char *name, unsigned int rd_count,
1251 const struct GNUNET_NAMESTORE_RecordData *rd,
1252 const struct GNUNET_CRYPTO_RsaSignature *signature)
1254 struct ResolverHandle *rh;
1255 struct RecordLookupHandle *rlh;
1256 struct GNUNET_TIME_Relative remaining_time;
1257 struct GNUNET_CRYPTO_ShortHashCode zone;
1258 struct GNUNET_TIME_Absolute et;
1264 rh->namestore_task = NULL;
1265 GNUNET_CRYPTO_short_hash (key,
1266 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1268 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1276 rh->status |= RSL_RECORD_EXISTS;
1278 if (remaining_time.rel_value == 0)
1280 rh->status |= RSL_RECORD_EXPIRED;
1287 * Lookup terminated and no results
1289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1290 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1293 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1294 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1297 * Our zone and no result? Cannot resolve TT
1299 rh->proc(rh->proc_cls, rh, 0, NULL);
1303 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1304 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1306 for (i=0; i<rd_count;i++)
1308 if (rd[i].record_type != rlh->record_type)
1311 if (ignore_pending_records &&
1312 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1314 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1315 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1320 //FIXME: eh? do I have to handle this here?
1321 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1322 et.abs_value = rd[i].expiration_time;
1323 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
1325 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1326 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1336 if (0 == rh->answered)
1338 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1339 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1340 rh->proc(rh->proc_cls, rh, 0, NULL);
1344 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1345 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1346 rh->id, rh->answered, rd_count);
1347 rh->proc(rh->proc_cls, rh, rd_count, rd);
1352 * VPN redirect result callback
1354 * @param cls the resolver handle
1355 * @param af the requested address family
1356 * @param address in_addr(6) respectively
1359 process_record_result_vpn (void* cls, int af, const void *address)
1361 struct ResolverHandle *rh = cls;
1362 struct RecordLookupHandle *rlh;
1363 struct GNUNET_NAMESTORE_RecordData rd;
1367 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1368 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1372 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1373 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1375 if (GNUNET_GNS_RECORD_A != rlh->record_type)
1377 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1378 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1380 rh->proc (rh->proc_cls, rh, 0, NULL);
1383 rd.record_type = GNUNET_GNS_RECORD_A;
1384 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1386 rd.data_size = sizeof (struct in_addr);
1388 rh->proc (rh->proc_cls, rh, 1, &rd);
1391 else if (AF_INET6 == af)
1393 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1394 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1396 if (GNUNET_GNS_RECORD_AAAA != rlh->record_type)
1398 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1399 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1401 rh->proc (rh->proc_cls, rh, 0, NULL);
1404 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1405 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1407 rd.data_size = sizeof (struct in6_addr);
1409 rh->proc (rh->proc_cls, rh, 1, &rd);
1413 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1414 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1416 rh->proc (rh->proc_cls, rh, 0, NULL);
1423 * Process VPN lookup result for record
1425 * @param cls the record lookup handle
1426 * @param rh resolver handle
1427 * @param rd_count number of results (1)
1428 * @param rd record data containing the result
1431 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1432 unsigned int rd_count,
1433 const struct GNUNET_NAMESTORE_RecordData *rd)
1435 struct RecordLookupHandle* rlh = cls;
1439 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1440 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1443 /* give up, cannot resolve */
1444 finish_lookup(rh, rlh, 0, NULL);
1448 /* results found yay */
1449 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1450 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1452 finish_lookup(rh, rlh, rd_count, rd);
1457 * Sends a UDP dns query to a nameserver specified in the rh
1459 * @param rh the resolver handle
1462 send_dns_packet (struct ResolverHandle *rh);
1466 handle_dns_resolver (void *cls,
1467 const struct sockaddr *addr,
1470 struct ResolverHandle *rh = cls;
1471 struct RecordLookupHandle *rlh = rh->proc_cls;
1472 struct GNUNET_NAMESTORE_RecordData rd;
1473 struct sockaddr_in *sai;
1474 struct sockaddr_in6 *sai6;
1478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1479 "No address found in DNS!\n");
1480 finish_lookup (rh, rlh, 0, NULL);
1484 if (sizeof (struct sockaddr_in) == addrlen)
1486 sai = (struct sockaddr_in*) addr;
1487 rd.record_type = GNUNET_GNS_RECORD_A;
1488 rd.data_size = sizeof (struct in_addr);
1489 rd.data = &sai->sin_addr;
1491 else if (sizeof (struct sockaddr_in6) == addrlen)
1493 sai6 = (struct sockaddr_in6*) addr;
1494 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1495 rd.data_size = sizeof (struct in6_addr);
1496 rd.data = &sai6->sin6_addr;
1500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1501 "Address length is garbage!\n");
1502 finish_lookup (rh, rlh, 0, NULL);
1505 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1507 finish_lookup (rh, rlh, 1, &rd);
1511 * Resolve DNS name via local stub resolver
1513 * @param rh the resolver handle
1516 resolve_dns_name (struct ResolverHandle *rh)
1518 struct RecordLookupHandle *rlh = rh->proc_cls;
1521 if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1522 (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525 "Can only resolve A/AAAA via stub... abort\n");
1526 finish_lookup (rh, rlh, 0, NULL);
1530 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1535 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1538 &handle_dns_resolver,
1544 * Read DNS udp packet from socket
1546 * @param cls the resolver handle
1547 * @param tc task context
1550 read_dns_response (void *cls,
1551 const struct GNUNET_SCHEDULER_TaskContext *tc)
1553 struct ResolverHandle *rh = cls;
1554 struct RecordLookupHandle *rlh = rh->proc_cls;
1555 char buf[UINT16_MAX];
1557 struct sockaddr_in addr;
1559 struct GNUNET_DNSPARSER_Packet *packet;
1560 struct GNUNET_NAMESTORE_RecordData rd;
1561 int found_delegation = GNUNET_NO;
1562 int found_cname = GNUNET_NO;
1563 char* delegation_name = NULL;
1564 int zone_offset = 0;
1567 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1568 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1570 /* timeout or shutdown */
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1572 "Terminating DNS query %d\n", tc->reason);
1573 finish_lookup (rh, rlh, 0, NULL);
1577 addrlen = sizeof (addr);
1578 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1580 (struct sockaddr*) &addr,
1585 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1586 finish_lookup (rh, rlh, 0, NULL);
1590 packet = GNUNET_DNSPARSER_parse (buf, r);
1594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1595 "Failed to parse DNS reply!\n");
1596 finish_lookup (rh, rlh, 0, NULL);
1600 for (i = 0; i < packet->num_answers; i++)
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "Got record type %d (want %d)\n",
1604 packet->answers[i].type,
1606 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1607 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1610 "CNAME... restarting query with %s\n",
1611 packet->answers[i].data.hostname
1613 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1614 found_cname = GNUNET_YES;
1618 if ((packet->answers[i].type == rlh->record_type) &&
1619 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1623 rd.data = packet->answers[i].data.raw.data;
1624 rd.data_size = packet->answers[i].data.raw.data_len;
1625 rd.record_type = packet->answers[i].type;
1627 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1628 finish_lookup (rh, rlh, 1, &rd);
1629 GNUNET_DNSPARSER_free_packet (packet);
1634 if (GNUNET_YES == found_cname)
1636 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1638 if (0 > zone_offset)
1641 /* restart query with CNAME */
1642 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1645 "Asking same server for %s\n", rh->dns_name);
1646 send_dns_packet (rh);
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1651 "Trying system resolver for %s\n", rh->dns_name);
1652 resolve_dns_name (rh);
1655 GNUNET_DNSPARSER_free_packet (packet);
1659 for (i = 0; i < packet->num_authority_records; i++)
1662 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1665 "Found NS delegation!\n");
1666 found_delegation = GNUNET_YES;
1667 delegation_name = packet->authority_records[i].data.hostname;
1672 for (i = 0; i < packet->num_additional_records; i++)
1674 if (GNUNET_NO == found_delegation)
1677 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1678 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1680 GNUNET_assert (sizeof (struct in_addr) ==
1681 packet->authority_records[i].data.raw.data_len);
1683 rh->dns_addr.sin_addr =
1684 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1685 send_dns_packet (rh);
1686 GNUNET_DNSPARSER_free_packet (packet);
1691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1692 "Nothing useful in DNS reply!\n");
1693 finish_lookup (rh, rlh, 0, NULL);
1694 GNUNET_DNSPARSER_free_packet (packet);
1699 * Sends a UDP dns query to a nameserver specified in the rh
1701 * @param rh the request handle
1704 send_dns_packet (struct ResolverHandle *rh)
1706 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1707 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1710 "Sending %dbyte DNS query\n",
1711 rh->dns_raw_packet_size);
1713 if (GNUNET_SYSERR ==
1714 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1716 rh->dns_raw_packet_size,
1717 (struct sockaddr*)&rh->dns_addr,
1718 sizeof (struct sockaddr_in)))
1719 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1720 _("Failed to send DNS request to %s\n"),
1721 GNUNET_a2s ((const struct sockaddr *)&rh->dns_addr,
1722 sizeof (struct sockaddr_in)));
1724 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1725 rh->timeout, //FIXME less?
1731 GNUNET_NETWORK_fdset_destroy (rset);
1736 * The final phase of resoution.
1737 * We found a NS RR and want to resolve via DNS
1739 * @param rh the pending lookup handle
1740 * @param rd_count length of record data
1741 * @param rd record data containing VPN RR
1744 resolve_record_dns (struct ResolverHandle *rh,
1746 const struct GNUNET_NAMESTORE_RecordData *rd)
1748 struct GNUNET_DNSPARSER_Query query;
1749 struct GNUNET_DNSPARSER_Packet packet;
1750 struct GNUNET_DNSPARSER_Flags flags;
1751 struct in_addr dnsip;
1752 struct sockaddr_in addr;
1753 struct sockaddr *sa;
1755 struct RecordLookupHandle *rlh = rh->proc_cls;
1757 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1758 memset (rh->dns_name, 0, sizeof (rh->dns_name));
1760 /* We cancel here as to not include the ns lookup in the timeout */
1761 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1763 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1764 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1766 /* Start shortening */
1767 if ((NULL != rh->priv_key) &&
1768 (GNUNET_YES == is_canonical (rh->name)))
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1771 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1773 start_shorten (rh->authority_chain_head,
1777 for (i = 0; i < rd_count; i++)
1779 /* Synthesize dns name */
1780 if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1782 strcpy (rh->dns_zone, (char*)rd[i].data);
1783 if (0 == strcmp (rh->name, ""))
1784 strcpy (rh->dns_name, (char*)rd[i].data);
1786 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1789 if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1790 /* need to use memcpy as .data may be unaligned */
1791 memcpy (&dnsip, rd[i].data, sizeof (dnsip));
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1795 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1799 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1800 if (NULL == rh->dns_sock)
1802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1803 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1805 finish_lookup (rh, rlh, 0, NULL);
1809 memset (&addr, 0, sizeof (struct sockaddr_in));
1810 sa = (struct sockaddr *) &addr;
1811 sa->sa_family = AF_INET;
1812 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1814 sizeof (struct sockaddr_in)))
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1817 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1819 finish_lookup (rh, rlh, 0, NULL);
1822 query.name = rh->dns_name;
1823 query.type = rlh->record_type;
1824 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1825 memset (&flags, 0, sizeof (flags));
1826 flags.recursion_desired = 1;
1827 flags.checking_disabled = 1;
1828 packet.queries = &query;
1829 packet.answers = NULL;
1830 packet.authority_records = NULL;
1831 packet.num_queries = 1;
1832 packet.num_answers = 0;
1833 packet.num_authority_records = 0;
1834 packet.num_additional_records = 0;
1835 packet.flags = flags;
1837 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1839 &rh->dns_raw_packet,
1840 &rh->dns_raw_packet_size))
1842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1843 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1845 GNUNET_NETWORK_socket_close (rh->dns_sock);
1846 finish_lookup (rh, rlh, 0, NULL);
1850 rh->dns_addr.sin_family = AF_INET;
1851 rh->dns_addr.sin_port = htons (53); //domain
1852 rh->dns_addr.sin_addr = dnsip;
1853 #if HAVE_SOCKADDR_IN_SIN_LEN
1854 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1857 send_dns_packet (rh);
1862 * The final phase of resoution.
1863 * We found a VPN RR and want to request an IPv4/6 address
1865 * @param rh the pending lookup handle
1866 * @param rd_count length of record data
1867 * @param rd record data containing VPN RR
1870 resolve_record_vpn (struct ResolverHandle *rh,
1872 const struct GNUNET_NAMESTORE_RecordData *rd)
1874 struct RecordLookupHandle *rlh = rh->proc_cls;
1875 struct GNUNET_HashCode serv_desc;
1876 struct vpn_data* vpn;
1879 /* We cancel here as to not include the ns lookup in the timeout */
1880 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1882 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1883 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1885 /* Start shortening */
1886 if ((NULL != rh->priv_key) &&
1887 (GNUNET_YES == is_canonical (rh->name)))
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1890 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1892 start_shorten (rh->authority_chain_head,
1896 vpn = (struct vpn_data*)rd->data;
1899 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1900 strlen ((char*)&vpn[1]) + 1,
1902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1903 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1906 GNUNET_h2s (&vpn->peer));
1908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1909 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1912 GNUNET_h2s (&serv_desc));
1913 rh->proc = &handle_record_vpn;
1914 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1918 if (NULL == vpn_handle)
1920 vpn_handle = GNUNET_VPN_connect (cfg);
1921 if (NULL == vpn_handle)
1923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1924 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1925 finish_lookup (rh, rh->proc_cls, 0, NULL);
1930 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1931 af, ntohs (vpn->proto),
1932 (struct GNUNET_PeerIdentity *)&vpn->peer,
1935 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1936 &process_record_result_vpn,
1942 * The final phase of resolution.
1943 * rh->name is a name that is canonical and we do not have a delegation.
1944 * Query namestore for this record
1946 * @param rh the pending lookup handle
1949 resolve_record_ns(struct ResolverHandle *rh)
1951 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1953 /* We cancel here as to not include the ns lookup in the timeout */
1954 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1956 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1957 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1959 /* Start shortening */
1960 if ((NULL != rh->priv_key) &&
1961 (GNUNET_YES == is_canonical (rh->name)))
1963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1964 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1966 start_shorten (rh->authority_chain_head,
1971 * Try to resolve this record in our namestore.
1972 * The name to resolve is now in rh->authority_name
1973 * since we tried to resolve it to an authority
1976 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1980 &process_record_result_ns,
1987 * Handle timeout for DHT requests
1989 * @param cls the request handle as closure
1990 * @param tc the task context
1993 dht_authority_lookup_timeout(void *cls,
1994 const struct GNUNET_SCHEDULER_TaskContext *tc)
1996 struct ResolverHandle *rh = cls;
1997 struct RecordLookupHandle *rlh = rh->proc_cls;
1998 char new_name[MAX_DNS_NAME_LENGTH];
2000 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2001 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
2002 rh->id, rh->authority_name, rh->timeout.rel_value);
2004 rh->status |= RSL_TIMED_OUT;
2005 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2006 if (NULL != rh->get_handle)
2007 GNUNET_DHT_get_stop (rh->get_handle);
2009 rh->get_handle = NULL;
2010 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
2012 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2013 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
2015 rh->proc(rh->proc_cls, rh, 0, NULL);
2019 if (0 == strcmp(rh->name, ""))
2022 * promote authority back to name and try to resolve record
2024 strcpy(rh->name, rh->authority_name);
2025 rh->proc(rh->proc_cls, rh, 0, NULL);
2030 * Start resolution in bg
2032 GNUNET_assert (0 < GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
2033 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD));
2035 strcpy(rh->name, new_name);
2037 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2038 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2039 rh->id, rh->name, rlh->record_type);
2041 gns_resolver_lookup_record (rh->authority,
2042 rh->private_local_zone,
2046 GNUNET_TIME_UNIT_FOREVER_REL,
2048 &background_lookup_result_processor,
2050 rh->proc(rh->proc_cls, rh, 0, NULL);
2054 static void resolve_delegation_dht(struct ResolverHandle *rh);
2057 static void resolve_delegation_ns(struct ResolverHandle *rh);
2061 * Namestore resolution for delegation finished. Processing result.
2063 * @param cls the closure
2064 * @param rh resolver handle
2065 * @param rd_count number of results (always 0)
2066 * @param rd record data (always NULL)
2069 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2070 unsigned int rd_count,
2071 const struct GNUNET_NAMESTORE_RecordData *rd);
2075 * This is a callback function that checks for key revocation
2077 * @param cls the pending query
2078 * @param key the key of the zone we did the lookup
2079 * @param expiration expiration date of the record data set in the namestore
2080 * @param name the name for which we need an authority
2081 * @param rd_count the number of records with 'name'
2082 * @param rd the record data
2083 * @param signature the signature of the authority for the record data
2086 process_pkey_revocation_result_ns (void *cls,
2087 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2088 struct GNUNET_TIME_Absolute expiration,
2090 unsigned int rd_count,
2091 const struct GNUNET_NAMESTORE_RecordData *rd,
2092 const struct GNUNET_CRYPTO_RsaSignature *signature)
2094 struct ResolverHandle *rh = cls;
2095 struct GNUNET_TIME_Relative remaining_time;
2098 rh->namestore_task = NULL;
2099 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2101 for (i = 0; i < rd_count; i++)
2103 if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2105 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2106 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2108 rh->status |= RSL_PKEY_REVOKED;
2109 rh->proc (rh->proc_cls, rh, 0, NULL);
2114 if ((NULL == name) ||
2115 (0 == remaining_time.rel_value))
2117 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2118 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2121 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2123 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2124 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2125 rh->id, "+.gads", GNUNET_GNS_RECORD_REV);
2127 gns_resolver_lookup_record(rh->authority,
2128 rh->private_local_zone,
2129 GNUNET_GNS_RECORD_REV,
2132 GNUNET_TIME_UNIT_FOREVER_REL,
2134 &background_lookup_result_processor,
2138 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2139 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2142 * We are done with PKEY resolution if name is empty
2143 * else resolve again with new authority
2145 if (strcmp (rh->name, "") == 0)
2146 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2148 resolve_delegation_ns (rh);
2153 * Callback when record data is put into namestore
2155 * @param cls the closure
2156 * @param success GNUNET_OK on success
2157 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2160 on_namestore_delegation_put_result(void *cls,
2164 struct NamestoreBGTask *nbg = cls;
2166 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2169 if (GNUNET_NO == success)
2171 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2172 "GNS_NS: records already in namestore\n");
2175 else if (GNUNET_YES == success)
2177 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2178 "GNS_NS: records successfully put in namestore\n");
2182 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2183 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2187 * Function called when we get a result from the dht
2188 * for our query. Recursively tries to resolve authorities
2191 * @param cls the request handle
2192 * @param exp lifetime
2193 * @param key the key the record was stored under
2194 * @param get_path get path
2195 * @param get_path_length get path length
2196 * @param put_path put path
2197 * @param put_path_length put path length
2198 * @param type the block type
2199 * @param size the size of the record
2200 * @param data the record data
2203 process_delegation_result_dht(void* cls,
2204 struct GNUNET_TIME_Absolute exp,
2205 const struct GNUNET_HashCode * key,
2206 const struct GNUNET_PeerIdentity *get_path,
2207 unsigned int get_path_length,
2208 const struct GNUNET_PeerIdentity *put_path,
2209 unsigned int put_path_length,
2210 enum GNUNET_BLOCK_Type type,
2211 size_t size, const void *data)
2213 struct ResolverHandle *rh = cls;
2214 struct GNSNameRecordBlock *nrb;
2215 uint32_t num_records;
2217 char* rd_data = (char*) data;
2220 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
2221 struct GNUNET_HashCode zone_hash_double, name_hash_double;
2223 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2224 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2229 nrb = (struct GNSNameRecordBlock*)data;
2231 /* stop dht lookup and timeout task */
2232 GNUNET_DHT_get_stop (rh->get_handle);
2233 rh->get_handle = NULL;
2234 if (rh->dht_heap_node != NULL)
2236 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2237 rh->dht_heap_node = NULL;
2240 num_records = ntohl(nrb->rd_count);
2241 name = (char*)&nrb[1];
2243 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2244 struct NamestoreBGTask *ns_heap_root;
2245 struct NamestoreBGTask *namestore_bg_task;
2247 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2248 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2249 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2254 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2255 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2261 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2262 rh->id, name, rh->authority_name);
2263 for (i=0; i<num_records; i++)
2266 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2267 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2268 rh->id, name, rh->authority_name);
2269 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2270 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2271 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2272 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2273 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2274 rh->id, rd[i].data_size);
2275 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2276 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2277 rh->id, rd[i].flags);
2279 if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2280 (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2281 (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2284 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2286 if (0 == strcmp(rh->name, ""))
2287 strcpy(rh->name, rh->authority_name);
2289 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2290 rh->name, rh->authority_name); //FIXME ret
2295 if ((0 == strcmp(name, rh->authority_name)) &&
2296 (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2298 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2299 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2302 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2303 struct AuthorityChain *auth =
2304 GNUNET_malloc(sizeof(struct AuthorityChain));
2305 auth->zone = rh->authority;
2306 memset(auth->name, 0, strlen(rh->authority_name)+1);
2307 strcpy(auth->name, rh->authority_name);
2308 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2309 rh->authority_chain_tail,
2312 if (NULL != rh->rd.data)
2313 GNUNET_free ((void*)rh->rd.data);
2315 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2316 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2317 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2320 /** try to import pkey if private key available */
2321 //if (rh->priv_key && is_canonical (rh->name))
2322 // process_discovered_authority(name, auth->zone,
2323 // rh->authority_chain_tail->zone,
2330 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2331 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2332 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2333 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2335 /* Save to namestore
2336 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2339 if (max_allowed_ns_tasks <=
2340 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2342 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2343 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2345 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2346 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background ns task\n",
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 "GNS_PHASE_DELEGATE_DHT-%llu: Caching record for %s\n",
2352 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2354 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2356 GNUNET_TIME_absolute_get().abs_value);
2357 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2364 &on_namestore_delegation_put_result, //cont
2365 namestore_bg_task); //cls
2369 if (0 != rh->answered)
2374 * FIXME in this case. should we ask namestore again?
2376 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2377 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2378 rh->id, rh->authority_name, rh->name);
2380 if (0 == strcmp(rh->name, ""))
2382 /* Start shortening */
2383 if ((NULL != rh->priv_key) &&
2384 (GNUNET_YES == is_canonical (rh->name)))
2386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2387 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2389 start_shorten (rh->authority_chain_head,
2394 rh->proc = &handle_delegation_ns;
2397 /* Check for key revocation and delegate */
2398 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2401 GNUNET_GNS_RECORD_REV,
2402 &process_pkey_revocation_result_ns,
2409 * No pkey but name exists
2412 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2413 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2414 rh->id, rh->authority_name, rh->name);
2415 if (0 == strcmp(rh->name, ""))
2416 strcpy(rh->name, rh->authority_name);
2418 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2419 rh->name, rh->authority_name); //FIXME ret
2421 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2422 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2423 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2424 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2426 rh->proc(rh->proc_cls, rh, 0, NULL);
2429 //FIXME maybe define somewhere else?
2430 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2431 +(MAX_DNS_NAME_LENGTH*2)
2432 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2433 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2437 expand_plus(char* dest, char* src, char* repl)
2440 unsigned int s_len = strlen(src)+1;
2442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2443 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2444 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2447 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2448 "GNS_POSTPROCESS: %s too short\n", src);
2450 /* no postprocessing */
2451 memcpy(dest, src, s_len+1);
2455 if (0 == strcmp(src+s_len-3, ".+"))
2457 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2458 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2459 memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2463 pos += strlen(repl);
2464 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2465 "GNS_POSTPROCESS: Expanded to %s\n", dest);
2469 memcpy(dest, src, s_len+1);
2477 finish_lookup (struct ResolverHandle *rh,
2478 struct RecordLookupHandle* rlh,
2479 unsigned int rd_count,
2480 const struct GNUNET_NAMESTORE_RecordData *rd)
2483 char new_rr_data[MAX_DNS_NAME_LENGTH];
2484 char new_mx_data[MAX_MX_LENGTH];
2485 char new_soa_data[MAX_SOA_LENGTH];
2486 char new_srv_data[MAX_SRV_LENGTH];
2487 struct srv_data *old_srv;
2488 struct srv_data *new_srv;
2489 struct soa_data *old_soa;
2490 struct soa_data *new_soa;
2491 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2494 unsigned int offset;
2496 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
2498 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2499 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2502 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2505 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2507 for (i = 0; i < rd_count; i++)
2510 if ((GNUNET_GNS_RECORD_NS != rd[i].record_type) &&
2511 (GNUNET_GNS_RECORD_PTR != rd[i].record_type) &&
2512 (GNUNET_GNS_RECORD_CNAME != rd[i].record_type) &&
2513 (GNUNET_GNS_RECORD_MX != rd[i].record_type) &&
2514 (GNUNET_GNS_RECORD_SOA != rd[i].record_type) &&
2515 (GNUNET_GNS_RECORD_SRV != rd[i].record_type))
2517 p_rd[i].data = rd[i].data;
2522 * for all those records we 'should'
2523 * also try to resolve the A/AAAA records (RFC1035)
2524 * This is a feature and not important
2527 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2528 "GNS_POSTPROCESS: Postprocessing\n");
2529 if (0 == strcmp(rh->name, "+"))
2530 repl_string = rlh->name;
2532 repl_string = rlh->name+strlen(rh->name)+1;
2535 if (GNUNET_GNS_RECORD_MX == rd[i].record_type)
2537 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2538 offset = sizeof(uint16_t);
2539 pos = new_mx_data+offset;
2540 expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2542 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2543 p_rd[i].data = new_mx_data;
2544 p_rd[i].data_size = offset;
2546 else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2549 * Prio, weight and port
2551 new_srv = (struct srv_data*)new_srv_data;
2552 old_srv = (struct srv_data*)rd[i].data;
2553 new_srv->prio = old_srv->prio;
2554 new_srv->weight = old_srv->weight;
2555 new_srv->port = old_srv->port;
2556 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2558 p_rd[i].data = new_srv_data;
2559 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2561 else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2563 /* expand mname and rname */
2564 old_soa = (struct soa_data*)rd[i].data;
2565 new_soa = (struct soa_data*)new_soa_data;
2566 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2567 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2568 offset = strlen ((char*)&new_soa[1]) + 1;
2569 expand_plus((char*)&new_soa[1] + offset,
2570 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2572 p_rd[i].data_size = sizeof (struct soa_data)
2574 + strlen ((char*)&new_soa[1] + offset);
2575 p_rd[i].data = new_soa_data;
2580 expand_plus(pos, (char*)rd[i].data, repl_string);
2581 p_rd[i].data_size = strlen(new_rr_data)+1;
2582 p_rd[i].data = new_rr_data;
2587 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2589 free_resolver_handle (rh);
2593 * Process DHT lookup result for record.
2595 * @param cls the closure
2596 * @param rh resolver handle
2597 * @param rd_count number of results
2598 * @param rd record data
2601 handle_record_dht(void* cls, struct ResolverHandle *rh,
2602 unsigned int rd_count,
2603 const struct GNUNET_NAMESTORE_RecordData *rd)
2605 struct RecordLookupHandle* rlh = cls;
2609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2610 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2612 /* give up, cannot resolve */
2613 finish_lookup (rh, rlh, 0, NULL);
2616 /* results found yay */
2617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2618 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2619 finish_lookup (rh, rlh, rd_count, rd);
2626 * Process namestore lookup result for record.
2628 * @param cls the closure
2629 * @param rh resolver handle
2630 * @param rd_count number of results
2631 * @param rd record data
2634 handle_record_ns (void* cls, struct ResolverHandle *rh,
2635 unsigned int rd_count,
2636 const struct GNUNET_NAMESTORE_RecordData *rd)
2638 struct RecordLookupHandle* rlh = cls;
2639 int check_dht = GNUNET_YES;
2643 /* results found yay */
2644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2645 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2646 finish_lookup (rh, rlh, rd_count, rd);
2650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2651 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2655 * There are 5 conditions that have to met for us to consult the DHT:
2656 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2657 * 2. No entry in the NS existed AND
2658 * 3. The zone queried is not the local resolver's zone AND
2659 * 4. The name that was looked up is '+'
2660 * because if it was any other canonical name we either already queried
2661 * the DHT for the authority in the authority lookup phase (and thus
2662 * would already have an entry in the NS for the record)
2663 * 5. We are not in cache only mode
2665 if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2666 (0 == (rh->status & RSL_RECORD_EXISTS)) )
2669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2670 "GNS_PHASE_REC-%llu: Not expired and exists!\n",
2672 check_dht = GNUNET_NO;
2675 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2676 &rh->private_local_zone))
2679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2680 "GNS_PHASE_REC-%llu: Our zone!\n",
2682 check_dht = GNUNET_NO;
2685 if ((0 != strcmp (rh->name, "+")) && (GNUNET_YES == is_srv (rh->name)))
2686 check_dht = GNUNET_NO;
2688 if (GNUNET_YES == rh->only_cached)
2689 check_dht = GNUNET_NO;
2691 if (GNUNET_YES == check_dht)
2693 rh->proc = &handle_record_dht;
2694 resolve_record_dht(rh);
2697 /* give up, cannot resolve */
2698 finish_lookup (rh, rlh, 0, NULL);
2703 * Move one level up in the domain hierarchy and return the
2704 * passed top level domain.
2706 * @param name the domain
2707 * @param dest the destination where the tld will be put
2710 pop_tld(char* name, char* dest)
2714 if (GNUNET_YES == is_canonical (name))
2721 for (len = strlen(name); 0 < len; len--)
2723 if (*(name+len) == '.')
2733 strcpy(dest, (name+len+1));
2737 * Checks if name is in tld
2739 * @param name the name to check
2740 * @param tld the TLD to check for
2741 * @return GNUNET_YES or GNUNET_NO
2744 is_tld(const char* name, const char* tld)
2748 if (strlen(name) <= strlen(tld))
2751 offset = strlen(name)-strlen(tld);
2752 if (0 != strcmp(name+offset, tld))
2754 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2755 "%s is not in .%s TLD\n", name, tld);
2762 * DHT resolution for delegation finished. Processing result.
2764 * @param cls the closure
2765 * @param rh resolver handle
2766 * @param rd_count number of results (always 0)
2767 * @param rd record data (always NULL)
2770 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2771 unsigned int rd_count,
2772 const struct GNUNET_NAMESTORE_RecordData *rd)
2774 struct RecordLookupHandle* rlh;
2778 if (0 == strcmp(rh->name, ""))
2780 if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2782 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2783 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2785 finish_lookup(rh, rlh, rd_count, rd);
2788 /* We resolved full name for delegation. resolving record */
2789 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2790 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2792 strcpy(rh->name, "+\0");
2793 rh->proc = &handle_record_ns;
2794 resolve_record_ns(rh);
2799 * we still have some left
2801 if (GNUNET_YES == is_canonical (rh->name))
2803 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2804 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2807 rh->proc = &handle_record_ns;
2808 resolve_record_ns(rh);
2811 /* give up, cannot resolve */
2812 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2813 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2815 finish_lookup(rh, rlh, 0, NULL);
2820 * Start DHT lookup for a name -> PKEY (compare NS) record in
2821 * rh->authority's zone
2823 * @param rh the pending gns query
2826 resolve_delegation_dht(struct ResolverHandle *rh)
2829 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2830 struct GNUNET_HashCode name_hash_double;
2831 struct GNUNET_HashCode zone_hash_double;
2832 struct GNUNET_HashCode lookup_key;
2833 struct ResolverHandle *rh_heap_root;
2835 pop_tld(rh->name, rh->authority_name);
2837 //FIXME handle return values here
2838 GNUNET_CRYPTO_short_hash(rh->authority_name,
2839 strlen(rh->authority_name),
2841 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2842 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2843 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2845 rh->dht_heap_node = NULL;
2847 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2849 rh->timeout_cont = &dht_authority_lookup_timeout;
2850 rh->timeout_cont_cls = rh;
2854 if (max_allowed_background_queries <=
2855 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2857 /* terminate oldest lookup */
2858 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2859 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2860 rh_heap_root->get_handle = NULL;
2861 rh_heap_root->dht_heap_node = NULL;
2863 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2864 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2865 rh->id, rh_heap_root->authority_name);
2867 rh_heap_root->proc(rh_heap_root->proc_cls,
2872 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2874 GNUNET_TIME_absolute_get().abs_value);
2877 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2878 GNUNET_assert(rh->get_handle == NULL);
2879 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2880 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2882 DHT_GNS_REPLICATION_LEVEL,
2883 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2886 &process_delegation_result_dht,
2892 * Namestore resolution for delegation finished. Processing result.
2894 * @param cls the closure
2895 * @param rh resolver handle
2896 * @param rd_count number of results (always 0)
2897 * @param rd record data (always NULL)
2900 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2901 unsigned int rd_count,
2902 const struct GNUNET_NAMESTORE_RecordData *rd)
2904 struct RecordLookupHandle* rlh;
2906 int check_dht = GNUNET_YES;
2909 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2910 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2911 rh->id, rh->status);
2913 if (rh->status & RSL_PKEY_REVOKED)
2915 finish_lookup (rh, rlh, 0, NULL);
2919 if (0 == strcmp(rh->name, ""))
2922 /* We resolved full name for delegation. resolving record */
2923 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2924 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2926 if (rh->status & RSL_CNAME_FOUND)
2928 if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2930 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2931 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2933 strcpy (rh->name, rh->authority_name);
2934 finish_lookup (rh, rlh, rd_count, rd);
2939 if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2941 s_len = strlen (rd->data) - 2;
2942 memcpy (rh->name, rd->data, s_len);
2943 rh->name[s_len] = '\0';
2944 resolve_delegation_ns (rh);
2947 else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2949 gns_resolver_lookup_record (rh->authority,
2950 rh->private_local_zone,
2959 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2960 free_resolver_handle (rh);
2966 strcpy (rh->dns_name, (char*)rd->data);
2967 resolve_dns_name (rh);
2972 else if (rh->status & RSL_DELEGATE_VPN)
2974 if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2976 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2977 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2979 finish_lookup(rh, rlh, rd_count, rd);
2982 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2983 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2985 GNUNET_assert (NULL != rd);
2986 rh->proc = &handle_record_vpn;
2987 resolve_record_vpn (rh, rd_count, rd);
2990 else if (rh->status & RSL_DELEGATE_NS)
2992 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2994 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2995 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2997 finish_lookup(rh, rlh, rd_count, rd);
3001 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3002 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3004 GNUNET_assert (NULL != rd);
3005 rh->proc = &handle_record_ns;
3006 resolve_record_dns (rh, rd_count, rd);
3009 else if (rh->status & RSL_DELEGATE_PKEY)
3011 if (rh->status & RSL_PKEY_REVOKED)
3013 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3014 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
3016 finish_lookup (rh, rlh, 0, NULL);
3019 else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
3021 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3022 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
3024 finish_lookup(rh, rlh, rd_count, rd);
3028 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3029 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3031 strcpy(rh->name, "+\0");
3032 rh->proc = &handle_record_ns;
3033 resolve_record_ns(rh);
3037 if (rh->status & RSL_DELEGATE_NS)
3039 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
3041 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3042 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3044 finish_lookup(rh, rlh, rd_count, rd);
3048 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3049 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3051 GNUNET_assert (NULL != rd);
3052 rh->proc = &handle_record_ns;
3053 resolve_record_dns (rh, rd_count, rd);
3058 * we still have some left
3059 * check if authority in ns is fresh
3061 * or we are authority
3064 if ((rh->status & RSL_RECORD_EXISTS) &&
3065 !(rh->status & RSL_RECORD_EXPIRED))
3066 check_dht = GNUNET_NO;
3068 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3069 &rh->private_local_zone))
3070 check_dht = GNUNET_NO;
3072 if (GNUNET_YES == rh->only_cached)
3073 check_dht = GNUNET_NO;
3075 if (GNUNET_YES == check_dht)
3078 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3079 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3081 rh->proc = &handle_delegation_dht;
3082 resolve_delegation_dht(rh);
3086 if (GNUNET_NO == is_canonical (rh->name))
3088 /* give up, cannot resolve */
3089 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3090 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3093 finish_lookup(rh, rlh, rd_count, rd);
3096 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3097 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3100 rh->proc = &handle_record_ns;
3101 resolve_record_ns(rh);
3106 * This is a callback function that should give us only PKEY
3107 * records. Used to query the namestore for the authority (PKEY)
3108 * for 'name'. It will recursively try to resolve the
3109 * authority for a given name from the namestore.
3111 * @param cls the pending query
3112 * @param key the key of the zone we did the lookup
3113 * @param expiration expiration date of the record data set in the namestore
3114 * @param name the name for which we need an authority
3115 * @param rd_count the number of records with 'name'
3116 * @param rd the record data
3117 * @param signature the signature of the authority for the record data
3120 process_delegation_result_ns (void* cls,
3121 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3122 struct GNUNET_TIME_Absolute expiration,
3124 unsigned int rd_count,
3125 const struct GNUNET_NAMESTORE_RecordData *rd,
3126 const struct GNUNET_CRYPTO_RsaSignature *signature)
3128 struct ResolverHandle *rh;
3129 struct GNUNET_TIME_Relative remaining_time;
3130 struct GNUNET_CRYPTO_ShortHashCode zone;
3131 char new_name[MAX_DNS_NAME_LENGTH];
3133 struct GNUNET_TIME_Absolute et;
3135 rh = (struct ResolverHandle *)cls;
3136 rh->namestore_task = NULL;
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3138 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3141 GNUNET_CRYPTO_short_hash (key,
3142 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3144 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3151 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3153 rh->status |= RSL_RECORD_EXISTS;
3155 if (remaining_time.rel_value == 0)
3157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3158 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3160 rh->status |= RSL_RECORD_EXPIRED;
3165 * No authority found in namestore.
3170 * We did not find an authority in the namestore
3175 * Promote this authority back to a name maybe it is
3178 if (strcmp (rh->name, "") == 0)
3180 /* simply promote back */
3181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3182 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3183 rh->id, rh->authority_name);
3184 strcpy (rh->name, rh->authority_name);
3188 /* add back to existing name */
3189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3190 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3191 rh->id, rh->authority_name, rh->name);
3192 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3193 rh->name, rh->authority_name);
3194 strcpy (rh->name, new_name);
3195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3196 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3200 rh->proc (rh->proc_cls, rh, 0, NULL);
3205 * We found an authority that may be able to help us
3206 * move on with query
3207 * Note only 1 pkey should have been returned.. anything else would be strange
3209 for (i=0; i < rd_count;i++)
3213 * A CNAME. Like regular DNS this means the is no other record for this
3216 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3218 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3219 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3222 rh->status |= RSL_CNAME_FOUND;
3223 rh->proc (rh->proc_cls, rh, rd_count, rd);
3230 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3232 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3233 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3235 rh->status |= RSL_DELEGATE_VPN;
3236 rh->proc (rh->proc_cls, rh, rd_count, rd);
3242 * FIXME make optional
3244 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3246 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3247 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3249 rh->status |= RSL_DELEGATE_NS;
3250 rh->proc (rh->proc_cls, rh, rd_count, rd);
3254 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3257 rh->status |= RSL_DELEGATE_PKEY;
3259 if ((ignore_pending_records != 0) &&
3260 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3263 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3269 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3270 et.abs_value = rd[i].expiration_time;
3271 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3275 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3277 if (remaining_time.rel_value == 0)
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3280 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3282 rh->authority_chain_head->fresh = 0;
3283 rh->proc (rh->proc_cls, rh, 0, NULL);
3291 * Resolve rest of query with new authority
3293 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3294 memcpy (&rh->authority, rd[i].data,
3295 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3296 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3297 auth->zone = rh->authority;
3298 memset (auth->name, 0, strlen (rh->authority_name)+1);
3299 strcpy (auth->name, rh->authority_name);
3300 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3301 rh->authority_chain_tail,
3303 if (NULL != rh->rd.data)
3304 GNUNET_free ((void*)(rh->rd.data));
3306 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3307 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3308 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3310 /* Check for key revocation and delegate */
3311 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3314 GNUNET_GNS_RECORD_REV,
3315 &process_pkey_revocation_result_ns,
3324 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3325 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3327 * If we have found some records for the LAST label
3328 * we return the results. Else null.
3330 if (strcmp (rh->name, "") == 0)
3332 /* Start shortening */
3333 if ((rh->priv_key != NULL) &&
3334 (is_canonical (rh->name) == GNUNET_YES))
3336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3337 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3339 start_shorten (rh->authority_chain_head,
3342 /* simply promote back */
3343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3344 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3345 rh->id, rh->authority_name);
3346 strcpy (rh->name, rh->authority_name);
3347 rh->proc (rh->proc_cls, rh, rd_count, rd);
3351 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
3352 "%s.%s", rh->name, rh->authority_name);
3353 strcpy (rh->name, new_name);
3354 rh->proc (rh->proc_cls, rh, 0, NULL);
3360 * Resolve the delegation chain for the request in our namestore
3362 * @param rh the resolver handle
3365 resolve_delegation_ns (struct ResolverHandle *rh)
3367 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3368 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3370 pop_tld(rh->name, rh->authority_name);
3371 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3374 GNUNET_GNS_RECORD_ANY,
3375 &process_delegation_result_ns,
3382 * Lookup of a record in a specific zone
3383 * calls lookup result processor on result
3385 * @param zone the root zone
3386 * @param pzone the private local zone
3387 * @param record_type the record type to look up
3388 * @param name the name to look up
3389 * @param key a private key for use with PSEU import (can be NULL)
3390 * @param timeout timeout for resolution
3391 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3392 * @param proc the processor to call on result
3393 * @param cls the closure to pass to proc
3396 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3397 struct GNUNET_CRYPTO_ShortHashCode pzone,
3398 uint32_t record_type,
3400 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3401 struct GNUNET_TIME_Relative timeout,
3403 RecordLookupProcessor proc,
3406 struct ResolverHandle *rh;
3407 struct RecordLookupHandle* rlh;
3408 char string_hash[MAX_DNS_LABEL_LENGTH];
3409 char nzkey[MAX_DNS_LABEL_LENGTH];
3410 char* nzkey_ptr = nzkey;
3412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3413 "Starting resolution for %s (type=%d)!\n",
3417 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3418 (strcmp(GNUNET_GNS_TLD, name) != 0))
3420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3421 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3426 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3427 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3429 memset (rh, 0, sizeof (struct ResolverHandle));
3430 rh->authority = zone;
3434 rh->timeout = timeout;
3435 rh->get_handle = NULL;
3436 rh->private_local_zone = pzone;
3437 rh->only_cached = only_cached;
3438 rh->namestore_task = NULL;
3441 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3446 "No shorten key for resolution\n");
3449 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3452 * Set timeout for authority lookup phase to 1/2
3454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3455 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3456 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3457 GNUNET_TIME_relative_divide(timeout, 2),
3458 &handle_lookup_timeout,
3460 rh->timeout_cont = &dht_authority_lookup_timeout;
3461 rh->timeout_cont_cls = rh;
3465 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3466 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3469 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3472 * Only 'gnunet' given
3474 strcpy(rh->name, "\0");
3478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3479 "Checking for TLD...\n");
3480 if (is_zkey_tld(name) == GNUNET_YES)
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3485 * This is a zkey tld
3486 * build hash and use as initial authority
3489 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3490 memcpy(rh->name, name,
3491 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3492 pop_tld(rh->name, string_hash);
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3495 "ZKEY is %s!\n", string_hash);
3497 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3499 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3502 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3503 "Cannot convert ZKEY `%s' to hash!\n", string_hash);
3505 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3506 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3507 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3510 proc (cls, 0, NULL);
3515 else if (is_gnunet_tld (name) == GNUNET_YES)
3517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3520 * Presumably GNUNET tld
3522 memset (rh->name, 0,
3523 strlen(name)-strlen(GNUNET_GNS_TLD));
3524 memcpy (rh->name, name,
3525 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3529 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3530 "Cannot handle this TLD %s\n", string_hash);
3532 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3533 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3534 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3537 proc (cls, 0, NULL);
3543 * Initialize authority chain
3545 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3546 rh->authority_chain_head->prev = NULL;
3547 rh->authority_chain_head->next = NULL;
3548 rh->authority_chain_tail = rh->authority_chain_head;
3549 rh->authority_chain_head->zone = rh->authority;
3550 strcpy (rh->authority_chain_head->name, "");
3553 * Copy original query into lookup handle
3555 rlh->record_type = record_type;
3556 memset(rlh->name, 0, strlen(name) + 1);
3557 strcpy(rlh->name, name);
3559 rlh->proc_cls = cls;
3561 rh->proc = &handle_delegation_ns;
3562 resolve_delegation_ns(rh);
3565 /******** END Record Resolver ***********/
3568 finish_shorten (struct ResolverHandle *rh,
3569 struct NameShortenHandle *nsh)
3571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3572 "Sending %s as shorten result\n", nsh->result);
3573 nsh->proc (nsh->proc_cls, nsh->result);
3574 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3576 free_resolver_handle (rh);
3581 * Callback calles by namestore for a zone to name
3584 * @param cls the closure
3585 * @param zone_key the zone we queried
3586 * @param expire the expiration time of the name
3587 * @param name the name found or NULL
3588 * @param rd_len number of records for the name
3589 * @param rd the record data (PKEY) for the name
3590 * @param signature the signature for the record data
3593 process_zone_to_name_shorten_root (void *cls,
3594 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3595 struct GNUNET_TIME_Absolute expire,
3597 unsigned int rd_len,
3598 const struct GNUNET_NAMESTORE_RecordData *rd,
3599 const struct GNUNET_CRYPTO_RsaSignature *signature);
3603 * Callback called by namestore for a zone to name
3606 * @param cls the closure
3607 * @param zone_key the zone we queried
3608 * @param expire the expiration time of the name
3609 * @param name the name found or NULL
3610 * @param rd_len number of records for the name
3611 * @param rd the record data (PKEY) for the name
3612 * @param signature the signature for the record data
3615 process_zone_to_name_shorten_shorten (void *cls,
3616 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3617 struct GNUNET_TIME_Absolute expire,
3619 unsigned int rd_len,
3620 const struct GNUNET_NAMESTORE_RecordData *rd,
3621 const struct GNUNET_CRYPTO_RsaSignature *signature)
3623 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3624 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3625 struct AuthorityChain *next_authority;
3627 char result[MAX_DNS_NAME_LENGTH];
3628 char tmp_name[MAX_DNS_NAME_LENGTH];
3631 rh->namestore_task = NULL;
3632 /* we found a match in our own root zone */
3635 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3636 memset(result, 0, answer_len);
3638 if (strlen(rh->name) > 0)
3640 sprintf (result, "%s.%s.%s.%s",
3642 nsh->shorten_zone_name,
3647 sprintf (result, "%s.%s.%s", name,
3648 nsh->shorten_zone_name,
3652 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3653 "Found shorten result %s\n", result);
3654 if (strlen (nsh->result) > strlen (result))
3655 strcpy (nsh->result, result);
3657 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3658 nsh->shorten_zone) == 0)
3661 * This is our zone append .gads unless name is empty
3662 * (it shouldn't be, usually FIXME what happens if we
3663 * shorten to our zone to a "" record??)
3666 sprintf (result, "%s.%s.%s",
3668 nsh->shorten_zone_name,
3670 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3671 "Our zone: Found %s as shorten result\n", result);
3673 if (strlen (nsh->result) > strlen (result))
3674 strcpy (nsh->result, result);
3675 //nsh->proc(nsh->proc_cls, result);
3677 //free_resolver_handle(rh);
3684 * continue with next authority if exists
3686 if ((rh->authority_chain_head->next == NULL))
3688 finish_shorten (rh, nsh);
3691 next_authority = rh->authority_chain_head;
3693 if (0 == strcmp (rh->name, ""))
3694 strcpy (tmp_name, next_authority->name);
3696 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3697 "%s.%s", rh->name, next_authority->name);
3699 strcpy(rh->name, tmp_name);
3700 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3701 "No PSEU found for authority %s. Promoting back: %s\n",
3702 next_authority->name, rh->name);
3704 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3705 rh->authority_chain_tail,
3708 GNUNET_free (next_authority);
3710 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3711 &rh->authority_chain_tail->zone,
3712 &rh->authority_chain_head->zone,
3713 &process_zone_to_name_shorten_root,
3718 * Callback calles by namestore for a zone to name
3721 * @param cls the closure
3722 * @param zone_key the zone we queried
3723 * @param expire the expiration time of the name
3724 * @param name the name found or NULL
3725 * @param rd_len number of records for the name
3726 * @param rd the record data (PKEY) for the name
3727 * @param signature the signature for the record data
3730 process_zone_to_name_shorten_private (void *cls,
3731 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3732 struct GNUNET_TIME_Absolute expire,
3734 unsigned int rd_len,
3735 const struct GNUNET_NAMESTORE_RecordData *rd,
3736 const struct GNUNET_CRYPTO_RsaSignature *signature)
3738 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3739 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3740 struct AuthorityChain *next_authority;
3742 char result[MAX_DNS_NAME_LENGTH];
3743 char tmp_name[MAX_DNS_NAME_LENGTH];
3746 rh->namestore_task = NULL;
3747 /* we found a match in our own root zone */
3750 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3751 memset(result, 0, answer_len);
3753 if (strlen(rh->name) > 0)
3755 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3759 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3762 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3763 "Found shorten result %s\n", result);
3764 if (strlen (nsh->result) > strlen (result))
3765 strcpy (nsh->result, result);
3767 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3768 nsh->private_zone) == 0)
3771 * This is our zone append .gads unless name is empty
3772 * (it shouldn't be, usually FIXME what happens if we
3773 * shorten to our zone to a "" record??)
3776 sprintf (result, "%s.%s.%s",
3777 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3778 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3779 "Our private zone: Found %s as shorten result %s\n", result);
3780 if (strlen (nsh->result) > strlen (result))
3781 strcpy (nsh->result, result);
3784 if (0 != strcmp (nsh->shorten_zone_name, ""))
3786 /* backtrack authorities for names in priv zone */
3787 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3789 &rh->authority_chain_head->zone,
3790 &process_zone_to_name_shorten_shorten,
3797 * continue with next authority if exists
3799 if ((rh->authority_chain_head->next == NULL))
3801 finish_shorten (rh, nsh);
3804 next_authority = rh->authority_chain_head;
3806 if (0 == strcmp (rh->name, ""))
3807 strcpy (tmp_name, next_authority->name);
3809 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3810 "%s.%s", rh->name, next_authority->name);
3812 strcpy(rh->name, tmp_name);
3813 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3814 "No PSEU found for authority %s. Promoting back: %s\n",
3815 next_authority->name, rh->name);
3817 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3818 rh->authority_chain_tail,
3821 GNUNET_free (next_authority);
3823 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3824 &rh->authority_chain_tail->zone,
3825 &rh->authority_chain_head->zone,
3826 &process_zone_to_name_shorten_root,
3832 * Callback calles by namestore for a zone to name
3835 * @param cls the closure
3836 * @param zone_key the zone we queried
3837 * @param expire the expiration time of the name
3838 * @param name the name found or NULL
3839 * @param rd_len number of records for the name
3840 * @param rd the record data (PKEY) for the name
3841 * @param signature the signature for the record data
3844 process_zone_to_name_shorten_root (void *cls,
3845 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3846 struct GNUNET_TIME_Absolute expire,
3848 unsigned int rd_len,
3849 const struct GNUNET_NAMESTORE_RecordData *rd,
3850 const struct GNUNET_CRYPTO_RsaSignature *signature)
3852 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3853 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3854 struct AuthorityChain *next_authority;
3856 char result[MAX_DNS_NAME_LENGTH];
3857 char tmp_name[MAX_DNS_NAME_LENGTH];
3860 rh->namestore_task = NULL;
3861 /* we found a match in our own root zone */
3864 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3865 memset(result, 0, answer_len);
3867 if (strlen(rh->name) > 0)
3869 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3873 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3876 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3877 "Found shorten result %s\n", result);
3878 if (strlen (nsh->result) > strlen (result))
3879 strcpy (nsh->result, result);
3881 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3882 nsh->root_zone) == 0)
3885 * This is our zone append .gads unless name is empty
3886 * (it shouldn't be, usually FIXME what happens if we
3887 * shorten to our zone to a "" record??)
3890 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3891 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3892 "Our zone: Found %s as shorten result\n", result);
3893 if (strlen (nsh->result) > strlen (result))
3894 strcpy (nsh->result, result);
3897 if (NULL != nsh->private_zone)
3899 /* backtrack authorities for names in priv zone */
3900 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3902 &rh->authority_chain_head->zone,
3903 &process_zone_to_name_shorten_private,
3906 else if (NULL != nsh->shorten_zone)
3908 /* backtrack authorities for names in shorten zone */
3909 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3911 &rh->authority_chain_head->zone,
3912 &process_zone_to_name_shorten_shorten,
3919 * continue with next authority if exists
3921 if ((rh->authority_chain_head->next == NULL))
3923 finish_shorten (rh, nsh);
3926 next_authority = rh->authority_chain_head;
3928 if (0 == strcmp (rh->name, ""))
3929 strcpy (tmp_name, next_authority->name);
3931 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3932 "%s.%s", rh->name, next_authority->name);
3934 strcpy(rh->name, tmp_name);
3935 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3936 "No PSEU found for authority %s. Promoting back: %s\n",
3937 next_authority->name, rh->name);
3939 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3940 rh->authority_chain_tail,
3943 GNUNET_free (next_authority);
3945 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3946 &rh->authority_chain_tail->zone,
3947 &rh->authority_chain_head->zone,
3948 &process_zone_to_name_shorten_root,
3955 * Process result from namestore delegation lookup
3956 * for shorten operation
3958 * @param cls the client shorten handle
3959 * @param rh the resolver handle
3960 * @param rd_count number of results (0)
3961 * @param rd data (NULL)
3964 handle_delegation_ns_shorten (void* cls,
3965 struct ResolverHandle *rh,
3967 const struct GNUNET_NAMESTORE_RecordData *rd)
3969 struct NameShortenHandle *nsh;
3970 char result[MAX_DNS_NAME_LENGTH];
3972 nsh = (struct NameShortenHandle *)cls;
3973 rh->namestore_task = NULL;
3975 * At this point rh->name contains the part of the name
3976 * that we do not have a PKEY in our namestore to resolve.
3977 * The authority chain in the resolver handle is now
3978 * useful to backtrack if needed
3981 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3982 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3983 memset(result, 0, sizeof (result));
3985 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3989 * This is our zone append .gads unless name is empty
3990 * (it shouldn't be, usually FIXME what happens if we
3991 * shorten to our zone to a "" record??)
3994 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3995 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3996 "Our zone: Found %s as shorten result\n", result);
3998 if (strlen (nsh->result) > strlen (result))
3999 strcpy (nsh->result, result);
4002 else if (NULL != nsh->private_zone)
4005 * This is our zone append .gads unless name is empty
4006 * (it shouldn't be, usually FIXME what happens if we
4007 * shorten to our zone to a "" record??)
4009 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4010 nsh->private_zone) == 0)
4013 sprintf (result, "%s.%s.%s",
4014 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4015 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4016 "Our zone: Found %s as shorten result in private zone %s\n",
4019 if (strlen (nsh->result) > strlen (result))
4020 strcpy (nsh->result, result);
4023 else if (NULL != nsh->shorten_zone)
4026 * This is our zone append .gads unless name is empty
4027 * (it shouldn't be, usually FIXME what happens if we
4028 * shorten to our zone to a "" record??)
4030 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4031 nsh->shorten_zone) == 0)
4033 sprintf (result, "%s.%s.%s",
4034 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4035 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4036 "Our zone: Found %s as shorten result in shorten zone\n",
4039 if (strlen (nsh->result) > strlen (result))
4040 strcpy (nsh->result, result);
4045 /* backtrack authorities for names */
4046 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4048 &rh->authority_chain_head->zone,
4049 &process_zone_to_name_shorten_root,
4056 * Callback calles by namestore for a zone to name
4059 * @param cls the closure
4060 * @param zone_key the zone we queried
4061 * @param expire the expiration time of the name
4062 * @param name the name found or NULL
4063 * @param rd_len number of records for the name
4064 * @param rd the record data (PKEY) for the name
4065 * @param signature the signature for the record data
4068 process_zone_to_name_zkey(void *cls,
4069 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4070 struct GNUNET_TIME_Absolute expire,
4072 unsigned int rd_len,
4073 const struct GNUNET_NAMESTORE_RecordData *rd,
4074 const struct GNUNET_CRYPTO_RsaSignature *signature)
4076 struct ResolverHandle *rh = cls;
4077 struct NameShortenHandle *nsh = rh->proc_cls;
4078 char new_name[MAX_DNS_NAME_LENGTH];
4080 rh->namestore_task = NULL;
4082 /* zkey not in our zone */
4086 * In this case we have not given this PKEY a name (yet)
4087 * It is either just not in our zone or not even cached
4088 * Since we do not know at this point we will not try to shorten
4089 * because PKEY import will happen if the user follows the zkey
4092 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4093 "No name found for zkey %s returning verbatim!\n", nsh->result);
4094 /*if (strcmp(rh->name, "") != 0)
4095 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4096 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4098 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4099 enc, GNUNET_GNS_TLD_ZKEY);
4101 strcpy (nsh->result, new_name);*/
4103 finish_shorten (rh, nsh);
4107 if (strcmp(rh->name, "") != 0)
4108 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4111 strcpy(new_name, name);
4113 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4114 "Continue shorten for %s!\n", new_name);
4116 strcpy(rh->name, new_name);
4118 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4119 rh->authority_chain_tail = rh->authority_chain_head;
4120 rh->authority_chain_head->zone = rh->authority;
4123 /* Start delegation resolution in our namestore */
4124 resolve_delegation_ns (rh);
4129 * Shorten api from resolver
4131 * @param zone the root zone to use
4132 * @param pzone the private zone to use
4133 * @param szone the shorten zone to use
4134 * @param name the name to shorten
4135 * @param private_zone_name name of the private zone
4136 * @param shorten_zone_name name of the shorten zone
4137 * @param proc the processor to call with result
4138 * @param proc_cls closure to pass to proc
4141 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4142 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4143 struct GNUNET_CRYPTO_ShortHashCode *szone,
4145 const char* private_zone_name,
4146 const char* shorten_zone_name,
4147 ShortenResultProcessor proc,
4150 struct ResolverHandle *rh;
4151 struct NameShortenHandle *nsh;
4152 char string_hash[MAX_DNS_LABEL_LENGTH];
4153 struct GNUNET_CRYPTO_ShortHashCode zkey;
4154 char nzkey[MAX_DNS_LABEL_LENGTH];
4155 char* nzkey_ptr = nzkey;
4158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4159 "Starting shorten for %s!\n", name);
4161 if (is_canonical ((char*)name) == GNUNET_YES)
4163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4164 "%s is canonical. Returning verbatim\n", name);
4165 proc (proc_cls, name);
4169 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4172 nsh->proc_cls = proc_cls;
4173 nsh->root_zone = zone;
4174 nsh->private_zone = pzone;
4175 nsh->shorten_zone = szone;
4176 strcpy (nsh->private_zone_name, private_zone_name);
4177 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4178 strcpy (nsh->result, name);
4181 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4182 rh->authority = *zone;
4184 rh->priv_key = NULL;
4185 rh->namestore_task = NULL;
4186 rh->proc = &handle_delegation_ns_shorten;
4189 rh->private_local_zone = *zone;
4191 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4194 "Checking for TLD...\n");
4195 if (is_zkey_tld (name) == GNUNET_YES)
4197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4200 * This is a zkey tld
4201 * build hash and use as initial authority
4204 memset (rh->name, 0,
4205 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4206 memcpy (rh->name, name,
4207 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4208 pop_tld (rh->name, string_hash);
4210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4211 "ZKEY is %s!\n", string_hash);
4213 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4215 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4219 "Cannot convert ZKEY %s to hash!\n", nzkey);
4222 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4223 proc (proc_cls, name);
4227 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4230 &process_zone_to_name_zkey,
4235 else if (is_gnunet_tld (name) == GNUNET_YES)
4237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4240 * Presumably GNUNET tld
4242 memset (rh->name, 0,
4243 strlen (name)-strlen (GNUNET_GNS_TLD));
4244 memcpy (rh->name, name,
4245 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4252 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4253 proc (proc_cls, name);
4257 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4258 rh->authority_chain_tail = rh->authority_chain_head;
4259 rh->authority_chain_head->zone = *zone;
4262 /* Start delegation resolution in our namestore */
4263 resolve_delegation_ns (rh);
4266 /*********** END NAME SHORTEN ********************/
4269 * Conclude get authority lookup
4271 * @param rh resolver handle
4272 * @param nah get authority lookup handle
4275 finish_get_auth (struct ResolverHandle *rh,
4276 struct GetNameAuthorityHandle *nah)
4278 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4279 "Got authority result %s\n", nah->result);
4281 nah->proc (nah->proc_cls, nah->result);
4282 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4284 free_resolver_handle (rh);
4288 * Process result from namestore delegation lookup
4289 * for get authority operation
4291 * @param cls the client get auth handle
4292 * @param rh the resolver handle
4293 * @param rd_count number of results (0)
4294 * @param rd data (NULL)
4297 handle_delegation_result_ns_get_auth(void* cls,
4298 struct ResolverHandle *rh,
4300 const struct GNUNET_NAMESTORE_RecordData *rd)
4302 struct GetNameAuthorityHandle* nah;
4305 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4308 * At this point rh->name contains the part of the name
4309 * that we do not have a PKEY in our namestore to resolve.
4310 * The authority chain in the resolver handle is now
4311 * useful to backtrack if needed
4314 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4315 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4317 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4318 "Building response!\n");
4319 if (is_canonical (rh->name) == GNUNET_YES)
4322 * We successfully resolved the authority in the ns
4323 * FIXME for our purposes this is fine
4324 * but maybe we want to have an api that also looks
4325 * into the dht (i.e. option in message)
4327 if (strlen(rh->name) > strlen(nah->name))
4329 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4330 "Record name longer than original lookup name... odd!\n");
4334 answer_len = strlen(nah->name) - strlen(rh->name)
4335 + strlen(GNUNET_GNS_TLD) + 1;
4336 memset(nah->result, 0, answer_len);
4337 if (0 != strcmp (rh->name, ""))
4338 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4340 strcpy(nah->result, nah->name);
4342 finish_get_auth (rh, nah);
4346 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4347 "Unable to resolve authority for remaining %s!\n", rh->name);
4348 strcpy(nah->result, "");
4349 finish_get_auth (rh, nah);
4357 * Tries to resolve the authority for name
4360 * @param zone the root zone to look up for
4361 * @param pzone the private local zone
4362 * @param name the name to lookup up
4363 * @param proc the processor to call when finished
4364 * @param proc_cls the closure to pass to the processor
4367 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4368 struct GNUNET_CRYPTO_ShortHashCode pzone,
4370 GetAuthorityResultProcessor proc,
4373 struct ResolverHandle *rh;
4374 struct GetNameAuthorityHandle *nah;
4376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4377 "Starting authority resolution for %s!\n", name);
4379 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4380 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4381 rh->authority = zone;
4383 rh->private_local_zone = pzone;
4384 rh->namestore_task = NULL;
4386 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4388 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4390 strcpy(rh->name, "\0");
4395 strlen(name)-strlen(GNUNET_GNS_TLD));
4396 memcpy(rh->name, name,
4397 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4400 memset(nah->name, 0,
4402 strcpy(nah->name, name);
4404 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4405 rh->authority_chain_tail = rh->authority_chain_head;
4406 rh->authority_chain_head->zone = zone;
4407 rh->proc = &handle_delegation_result_ns_get_auth;
4408 rh->proc_cls = (void*)nah;
4411 nah->proc_cls = proc_cls;
4412 strcpy (nah->result, "");
4414 /* Start delegation resolution in our namestore */
4415 resolve_delegation_ns(rh);
4419 /******** END GET AUTHORITY *************/
4421 /* end of gnunet-service-gns_resolver.c */