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 (is a legal name in a zone, without delegation);
180 * note that we do not test that the name does not contain illegal characters, we only
181 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
182 * even though they consist of multiple labels.
185 * a.b.gads = not canonical
187 * _foo._srv = canonical
188 * _f.bar = not canonical
190 * @param name the name to test
191 * @return GNUNET_YES if canonical
194 is_canonical (const char* name)
199 if (NULL == strchr (name, '.'))
204 while (NULL != (dot = strchr (pos, '.')))
214 * Callback that shortens authorities
216 * @param gph the handle containing the name to shorten
219 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
223 * Continueation for pkey record creation (shorten)
225 * @param cls a GetPseuAuthorityHandle
226 * @param success unused
230 create_pkey_cont (void* cls, int32_t success, const char* emsg)
232 //FIXME do sth with error
233 struct GetPseuAuthorityHandle* gph = cls;
235 gph->namestore_task = NULL;
236 GNUNET_free (gph->auth);
237 GNUNET_CRYPTO_rsa_key_free (gph->key);
238 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
244 * Namestore calls this function if we have record for this name.
245 * (or with rd_count=0 to indicate no matches)
247 * @param cls the pending query
248 * @param key the key of the zone we did the lookup
249 * @param expiration expiration date of the namestore entry
250 * @param name the name for which we need an authority
251 * @param rd_count the number of records with 'name'
252 * @param rd the record data
253 * @param signature the signature of the authority for the record data
256 process_pseu_lookup_ns (void* cls,
257 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
258 struct GNUNET_TIME_Absolute expiration,
259 const char *name, unsigned int rd_count,
260 const struct GNUNET_NAMESTORE_RecordData *rd,
261 const struct GNUNET_CRYPTO_RsaSignature *signature)
263 struct GetPseuAuthorityHandle* gph = cls;
264 struct GNUNET_NAMESTORE_RecordData new_pkey;
266 gph->namestore_task = NULL;
269 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
270 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
271 GNUNET_free (gph->auth);
272 GNUNET_CRYPTO_rsa_key_free (gph->key);
273 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
282 new_pkey.expiration_time = UINT64_MAX;
283 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
284 new_pkey.data = &gph->auth->zone;
285 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
286 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
287 | GNUNET_NAMESTORE_RF_PRIVATE
288 | GNUNET_NAMESTORE_RF_PENDING;
289 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
293 &create_pkey_cont, //cont
298 * process result of a dht pseu lookup
300 * @param gph the handle
301 * @param name the pseu result or NULL
304 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309 "GNS_AUTO_PSEU: No PSEU, no shorten. Finished.\n");
310 GNUNET_free (gph->auth);
311 GNUNET_CRYPTO_rsa_key_free (gph->key);
312 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
317 memcpy (gph->test_name, name, strlen(name)+1);
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
323 * Check for collision
325 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
328 GNUNET_NAMESTORE_TYPE_ANY,
329 &process_pseu_lookup_ns,
334 * Handle timeout for dht request
336 * @param cls the request handle as closure
337 * @param tc the task context
340 handle_auth_discovery_timeout (void *cls,
341 const struct GNUNET_SCHEDULER_TaskContext *tc)
343 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
347 GNUNET_DHT_get_stop (gph->get_handle);
348 gph->get_handle = NULL;
349 process_pseu_result (gph, NULL);
353 * Function called when we find a PSEU entry in the DHT
355 * @param cls the request handle
356 * @param exp lifetime
357 * @param key the key the record was stored under
358 * @param get_path get path
359 * @param get_path_length get path length
360 * @param put_path put path
361 * @param put_path_length put path length
362 * @param type the block type
363 * @param size the size of the record
364 * @param data the record data
367 process_auth_discovery_dht_result (void* cls,
368 struct GNUNET_TIME_Absolute exp,
369 const struct GNUNET_HashCode * key,
370 const struct GNUNET_PeerIdentity *get_path,
371 unsigned int get_path_length,
372 const struct GNUNET_PeerIdentity *put_path,
373 unsigned int put_path_length,
374 enum GNUNET_BLOCK_Type type,
378 struct GetPseuAuthorityHandle* gph = cls;
379 struct GNSNameRecordBlock *nrb;
380 const char* rd_data = data;
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
389 /* stop lookup and timeout task */
390 GNUNET_DHT_get_stop (gph->get_handle);
391 gph->get_handle = NULL;
392 GNUNET_SCHEDULER_cancel (gph->timeout);
396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
397 "GNS_GET_AUTH: got dht result null!\n", size);
398 GNUNET_free (gph->auth);
399 GNUNET_CRYPTO_rsa_key_free (gph->key);
400 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
405 nrb = (struct GNSNameRecordBlock*)data;
406 name = (char*)&nrb[1];
407 num_records = ntohl (nrb->rd_count);
409 struct GNUNET_NAMESTORE_RecordData rd[num_records];
411 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
412 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
414 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
420 "GNS_GET_AUTH: Error deserializing data!\n");
424 for (i=0; i < num_records; i++)
426 if ((strcmp (name, "+") == 0) &&
427 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
430 process_pseu_result (gph, (char*)rd[i].data);
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437 "GNS_GET_AUTH: finished shorten, no results!\n");
438 process_pseu_result (gph, NULL);
442 * Process PSEU discovery for shorten via namestore
444 * @param cls the GetPseuAuthorityHandle
445 * @param key the public key
446 * @param expiration recorddata expiration
447 * @param name the looked up name
448 * @param rd_count number of records in set
449 * @param rd record data
450 * @param signature the signature
453 process_auth_discovery_ns_result (void* cls,
454 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
455 struct GNUNET_TIME_Absolute expiration,
457 unsigned int rd_count,
458 const struct GNUNET_NAMESTORE_RecordData *rd,
459 const struct GNUNET_CRYPTO_RsaSignature *signature)
461 struct GetPseuAuthorityHandle* gph = cls;
462 struct GNUNET_CRYPTO_ShortHashCode name_hash;
463 struct GNUNET_HashCode lookup_key;
464 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
465 struct GNUNET_HashCode name_hash_double;
466 struct GNUNET_HashCode zone_hash_double;
470 gph->namestore_task = NULL;
477 GNUNET_CRYPTO_short_hash ("+", strlen ("+"), &name_hash);
478 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
479 GNUNET_CRYPTO_short_hash_double (&gph->auth->zone, &zone_hash_double);
480 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
481 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
485 "+", (char*)&lookup_key_string);
487 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
488 &handle_auth_discovery_timeout, gph);
490 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
492 GNUNET_assert (gph->get_handle == NULL);
494 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
495 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
497 DHT_GNS_REPLICATION_LEVEL,
498 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
501 &process_auth_discovery_dht_result,
506 for (i=0; i < rd_count; i++)
508 if (0 != (strcmp (name, "+")))
511 if (rd[i].record_type != GNUNET_GNS_RECORD_PSEU)
515 process_pseu_result (gph, (char*)rd[i].data);
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
520 process_pseu_result (gph, NULL);
524 * Callback called by namestore for a zone to name
527 * @param cls the closure
528 * @param zone_key the zone we queried
529 * @param expire the expiration time of the name
530 * @param name the name found or NULL
531 * @param rd_len number of records for the name
532 * @param rd the record data (PKEY) for the name
533 * @param signature the signature for the record data
536 process_zone_to_name_discover (void *cls,
537 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
538 struct GNUNET_TIME_Absolute expire,
541 const struct GNUNET_NAMESTORE_RecordData *rd,
542 const struct GNUNET_CRYPTO_RsaSignature *signature)
544 struct GetPseuAuthorityHandle* gph = cls;
546 gph->namestore_task = NULL;
549 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
552 GNUNET_GNS_RECORD_PSEU,
553 &process_auth_discovery_ns_result,
557 /* we found a match in our own zone */
558 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
559 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
560 GNUNET_free (gph->auth);
561 GNUNET_CRYPTO_rsa_key_free (gph->key);
562 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
568 * Callback that shortens authorities
570 * @param gph the handle to the shorten request
573 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577 "GNS_AUTO_PSEU: New authority %s discovered\n",
580 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
583 &process_zone_to_name_discover,
590 start_shorten (struct AuthorityChain *auth,
591 const struct GNUNET_CRYPTO_RsaPrivateKey *key)
593 struct GetPseuAuthorityHandle *gph;
594 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
595 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
597 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
598 if (NULL == (pb_key = GNUNET_CRYPTO_rsa_encode_key (key)))
600 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
601 "Failed to encode RSA key on shorten\n");
605 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
606 gph->key = GNUNET_CRYPTO_rsa_decode_key ((const char*) pb_key, ntohs (pb_key->len));
608 if (NULL == gph->key)
610 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
611 "Failed to decode RSA key on shorten\n");
615 GNUNET_CRYPTO_short_hash (&pkey,
616 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
618 gph->auth = GNUNET_malloc (sizeof (struct AuthorityChain));
619 memcpy (gph->auth, auth, sizeof (struct AuthorityChain));
620 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
621 shorten_authority_chain (gph);
625 * Initialize the resolver
627 * @param nh the namestore handle
628 * @param dh the dht handle
629 * @param lz the local zone's hash
630 * @param c configuration handle
631 * @param max_bg_queries maximum number of parallel background queries in dht
632 * @param ignore_pending ignore records that still require user confirmation
634 * @return GNUNET_OK on success
637 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
638 struct GNUNET_DHT_Handle *dh,
639 struct GNUNET_CRYPTO_ShortHashCode lz,
640 const struct GNUNET_CONFIGURATION_Handle *c,
641 unsigned long long max_bg_queries,
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;
659 GNUNET_RESOLVER_connect (cfg);
667 * @param cls closure to iterator
668 * @param node heap nodes
669 * @param element the namestorebgtask
670 * @param cost heap cost
671 * @return always GNUNET_YES
674 cleanup_pending_ns_tasks (void* cls,
675 struct GNUNET_CONTAINER_HeapNode *node,
677 GNUNET_CONTAINER_HeapCostType cost)
679 struct NamestoreBGTask *nbg = element;
680 ResolverCleanupContinuation cont = cls;
682 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
683 "GNS_CLEANUP: Terminating ns task\n");
684 GNUNET_NAMESTORE_cancel (nbg->qe);
686 GNUNET_CONTAINER_heap_remove_node (node);
688 if (0 == GNUNET_CONTAINER_heap_get_size (ns_task_heap))
698 * @param rh resolver handle
699 * @param rlh record lookup handle
700 * @param rd_count number of results
704 finish_lookup (struct ResolverHandle *rh,
705 struct RecordLookupHandle* rlh,
706 unsigned int rd_count,
707 const struct GNUNET_NAMESTORE_RecordData *rd);
711 * Cleanup background lookups FIXME get rid of this?? YES this doesn't do
712 * anything! => find in code and remove all references to the heap
714 * @param cls closure to iterator
715 * @param node heap nodes
716 * @param element the resolver handle
717 * @param cost heap cost
718 * @return always GNUNET_YES
721 cleanup_pending_background_queries (void* cls,
722 struct GNUNET_CONTAINER_HeapNode *node,
724 GNUNET_CONTAINER_HeapCostType cost)
726 struct ResolverHandle *rh = element;
727 ResolverCleanupContinuation cont = cls;
729 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
730 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
732 GNUNET_CONTAINER_heap_remove_node (node);
733 if (0 == GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
735 if (GNUNET_CONTAINER_heap_get_size (ns_task_heap) == 0)
739 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
740 &cleanup_pending_ns_tasks,
751 * Helper function to free resolver handle
753 * @param rh the handle to free
756 free_resolver_handle (struct ResolverHandle* rh)
758 struct AuthorityChain *ac;
759 struct AuthorityChain *ac_next;
764 ac = rh->authority_chain_head;
773 if (NULL != rh->get_handle)
774 GNUNET_DHT_get_stop (rh->get_handle);
776 if (NULL != rh->dns_raw_packet)
777 GNUNET_free (rh->dns_raw_packet);
779 if (NULL != rh->namestore_task)
780 GNUNET_NAMESTORE_cancel (rh->namestore_task);
781 rh->namestore_task = NULL;
783 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
784 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
786 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
787 GNUNET_SCHEDULER_cancel (rh->timeout_task);
789 if (NULL != rh->dns_sock)
790 GNUNET_NETWORK_socket_close (rh->dns_sock);
791 if (NULL != rh->dns_resolver_handle)
792 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
794 if (NULL != rh->rd.data)
795 GNUNET_free ((void*)(rh->rd.data));
803 * @param rh resolver handle
804 * @param nsh name shorten handle
807 finish_shorten (struct ResolverHandle *rh,
808 struct NameShortenHandle *nsh);
812 * @param rh resolver handle
813 * @param nah get name authority handle
816 finish_get_auth (struct ResolverHandle *rh,
817 struct GetNameAuthorityHandle* rlh);
822 gns_resolver_cleanup (ResolverCleanupContinuation cont)
825 struct GetPseuAuthorityHandle *tmp;
829 for (tmp = gph_head; tmp != NULL; tmp = gph_head)
831 if (tmp->get_handle != NULL)
832 GNUNET_DHT_get_stop (tmp->get_handle);
833 tmp->get_handle = NULL;
834 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
835 GNUNET_SCHEDULER_cancel (tmp->timeout);
836 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
838 if (NULL != tmp->namestore_task)
839 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
840 tmp->namestore_task = NULL;
841 GNUNET_free (tmp->auth);
842 GNUNET_CRYPTO_rsa_key_free (tmp->key);
843 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, tmp);
847 while (NULL != rlh_head)
849 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
851 while (NULL != nsh_head)
853 finish_shorten (nsh_head, nsh_head->proc_cls);
855 while (NULL != nah_head)
857 finish_get_auth (nah_head, nah_head->proc_cls);
860 s = GNUNET_CONTAINER_heap_get_size (dht_lookup_heap);
861 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
862 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
864 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
865 &cleanup_pending_background_queries,
867 else if (0 != GNUNET_CONTAINER_heap_get_size (ns_task_heap))
869 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
870 &cleanup_pending_ns_tasks,
881 * Callback when record data is put into namestore
883 * @param cls the closure
884 * @param success GNUNET_OK on success
885 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
888 on_namestore_record_put_result (void *cls,
892 struct NamestoreBGTask *nbg = cls;
894 GNUNET_CONTAINER_heap_remove_node (nbg->node);
897 if (GNUNET_NO == success)
899 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
900 "GNS_NS: records already in namestore\n");
903 else if (GNUNET_YES == success)
905 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
906 "GNS_NS: records successfully put in namestore\n");
910 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
911 "GNS_NS: Error putting records into namestore: %s\n", emsg);
916 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
918 struct ResolverHandle *rh = cls;
920 if (NULL != rh->timeout_cont)
921 rh->timeout_cont (rh->timeout_cont_cls, tc);
922 /* FIXME: does this leak memory? */
927 * Processor for background lookups in the DHT
929 * @param cls closure (NULL)
930 * @param rd_count number of records found (not 0)
931 * @param rd record data
934 background_lookup_result_processor (void *cls,
936 const struct GNUNET_NAMESTORE_RecordData *rd)
938 //We could do sth verbose/more useful here but it doesn't make any difference
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
940 "GNS_BG: background dht lookup finished. (%d results)\n",
945 * Handle timeout for DHT requests
947 * @param cls the request handle as closure
948 * @param tc the task context
951 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
953 struct ResolverHandle *rh = cls;
954 struct RecordLookupHandle *rlh = rh->proc_cls;
955 char new_name[MAX_DNS_NAME_LENGTH];
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
959 rh->id, rh->name, rh->timeout.rel_value);
961 * Start resolution in bg
963 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
964 rh->name, GNUNET_GNS_TLD);
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
968 rh->id, new_name, rlh->record_type);
970 gns_resolver_lookup_record (rh->authority,
971 rh->private_local_zone,
975 GNUNET_TIME_UNIT_FOREVER_REL,
977 &background_lookup_result_processor,
980 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
982 GNUNET_DHT_get_stop (rh->get_handle);
983 rh->get_handle = NULL;
984 rh->proc (rh->proc_cls, rh, 0, NULL);
989 * Function called when we get a result from the dht
990 * for our record query
992 * @param cls the request handle
993 * @param exp lifetime
994 * @param key the key the record was stored under
995 * @param get_path get path
996 * @param get_path_length get path length
997 * @param put_path put path
998 * @param put_path_length put path length
999 * @param type the block type
1000 * @param size the size of the record
1001 * @param data the record data
1004 process_record_result_dht (void* cls,
1005 struct GNUNET_TIME_Absolute exp,
1006 const struct GNUNET_HashCode * key,
1007 const struct GNUNET_PeerIdentity *get_path,
1008 unsigned int get_path_length,
1009 const struct GNUNET_PeerIdentity *put_path,
1010 unsigned int put_path_length,
1011 enum GNUNET_BLOCK_Type type,
1012 size_t size, const void *data)
1014 struct ResolverHandle *rh;
1015 struct RecordLookupHandle *rlh;
1016 struct GNSNameRecordBlock *nrb;
1017 uint32_t num_records;
1019 char* rd_data = (char*)data;
1023 rh = (struct ResolverHandle *)cls;
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
1027 rlh = (struct RecordLookupHandle *) rh->proc_cls;
1028 nrb = (struct GNSNameRecordBlock*)data;
1030 /* stop lookup and timeout task */
1031 GNUNET_DHT_get_stop (rh->get_handle);
1032 rh->get_handle = NULL;
1034 if (rh->dht_heap_node != NULL)
1036 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1037 rh->dht_heap_node = NULL;
1040 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1042 GNUNET_SCHEDULER_cancel (rh->timeout_task);
1043 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1046 rh->get_handle = NULL;
1047 name = (char*)&nrb[1];
1048 num_records = ntohl (nrb->rd_count);
1050 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1051 struct NamestoreBGTask *ns_heap_root;
1052 struct NamestoreBGTask *namestore_bg_task;
1054 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
1055 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
1057 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1062 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1063 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1064 rh->proc (rh->proc_cls, rh, 0, NULL);
1068 for (i=0; i<num_records; i++)
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1072 rh->id, name, rh->name);
1073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1074 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1075 rh->id, rd[i].record_type, rlh->record_type);
1076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1077 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1078 rh->id, rd[i].data_size);
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 "GNS_PHASE_REC-%llu: Got flag %d\n",
1081 rh->id, rd[i].flags);
1083 if ((strcmp (name, rh->name) == 0) &&
1084 (rd[i].record_type == rlh->record_type))
1092 * FIXME check pubkey against existing key in namestore?
1093 * https://gnunet.org/bugs/view.php?id=2179
1095 if (max_allowed_ns_tasks <=
1096 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1098 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1099 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1106 /* Save to namestore */
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1108 "GNS_PHASE_REC-%llu: Caching record for %s\n",
1110 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1111 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1118 &on_namestore_record_put_result, //cont
1121 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1123 GNUNET_TIME_absolute_get().abs_value);
1126 if (0 < rh->answered)
1127 rh->proc (rh->proc_cls, rh, num_records, rd);
1129 rh->proc (rh->proc_cls, rh, 0, NULL);
1136 * Start DHT lookup for a (name -> query->record_type) record in
1137 * rh->authority's zone
1139 * @param rh the pending gns query context
1142 resolve_record_dht (struct ResolverHandle *rh)
1145 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1146 struct GNUNET_HashCode lookup_key;
1147 struct GNUNET_HashCode name_hash_double;
1148 struct GNUNET_HashCode zone_hash_double;
1149 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
1150 struct RecordLookupHandle *rlh = rh->proc_cls;
1151 struct ResolverHandle *rh_heap_root;
1153 GNUNET_CRYPTO_short_hash (rh->name, strlen (rh->name), &name_hash);
1154 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
1155 GNUNET_CRYPTO_short_hash_double (&rh->authority, &zone_hash_double);
1156 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
1157 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1161 rh->id, rh->name, (char*)&lookup_key_string);
1163 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1164 rh->dht_heap_node = NULL;
1166 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
1169 * Update timeout if necessary
1171 if (GNUNET_SCHEDULER_NO_TASK == rh->timeout_task)
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1174 "GNS_PHASE_REC-%llu: Adjusting timeout\n", rh->id);
1176 * Set timeout for authority lookup phase to 1/2
1178 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1179 GNUNET_TIME_relative_divide (rh->timeout, 2),
1180 &handle_lookup_timeout,
1183 rh->timeout_cont = &dht_lookup_timeout;
1184 rh->timeout_cont_cls = rh;
1188 if (max_allowed_background_queries <=
1189 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1191 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1192 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1193 rh_heap_root->get_handle = NULL;
1194 rh_heap_root->dht_heap_node = NULL;
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1198 rh->id, rh_heap_root->name);
1199 rh_heap_root->proc (rh_heap_root->proc_cls,
1204 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1206 GNUNET_TIME_absolute_get ().abs_value);
1209 xquery = htonl (rlh->record_type);
1211 GNUNET_assert (rh->get_handle == NULL);
1212 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1213 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1215 DHT_GNS_REPLICATION_LEVEL,
1216 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1219 &process_record_result_dht,
1226 * Namestore calls this function if we have record for this name.
1227 * (or with rd_count=0 to indicate no matches)
1229 * @param cls the pending query
1230 * @param key the key of the zone we did the lookup
1231 * @param expiration expiration date of the namestore entry
1232 * @param name the name for which we need an authority
1233 * @param rd_count the number of records with 'name'
1234 * @param rd the record data
1235 * @param signature the signature of the authority for the record data
1238 process_record_result_ns (void* cls,
1239 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1240 struct GNUNET_TIME_Absolute expiration,
1241 const char *name, unsigned int rd_count,
1242 const struct GNUNET_NAMESTORE_RecordData *rd,
1243 const struct GNUNET_CRYPTO_RsaSignature *signature)
1245 struct ResolverHandle *rh;
1246 struct RecordLookupHandle *rlh;
1247 struct GNUNET_TIME_Relative remaining_time;
1248 struct GNUNET_CRYPTO_ShortHashCode zone;
1249 struct GNUNET_TIME_Absolute et;
1255 rh->namestore_task = NULL;
1256 GNUNET_CRYPTO_short_hash (key,
1257 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1259 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1267 rh->status |= RSL_RECORD_EXISTS;
1269 if (remaining_time.rel_value == 0)
1271 rh->status |= RSL_RECORD_EXPIRED;
1278 * Lookup terminated and no results
1280 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1281 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1284 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1285 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1288 * Our zone and no result? Cannot resolve TT
1290 rh->proc(rh->proc_cls, rh, 0, NULL);
1294 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1295 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1297 for (i=0; i<rd_count;i++)
1299 if (rd[i].record_type != rlh->record_type)
1302 if (ignore_pending_records &&
1303 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1305 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1306 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1311 //FIXME: eh? do I have to handle this here?
1312 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1313 et.abs_value = rd[i].expiration_time;
1314 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
1316 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1317 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1327 if (0 == rh->answered)
1329 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1330 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1331 rh->proc(rh->proc_cls, rh, 0, NULL);
1335 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1336 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1337 rh->id, rh->answered, rd_count);
1338 rh->proc(rh->proc_cls, rh, rd_count, rd);
1343 * VPN redirect result callback
1345 * @param cls the resolver handle
1346 * @param af the requested address family
1347 * @param address in_addr(6) respectively
1350 process_record_result_vpn (void* cls, int af, const void *address)
1352 struct ResolverHandle *rh = cls;
1353 struct RecordLookupHandle *rlh;
1354 struct GNUNET_NAMESTORE_RecordData rd;
1358 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1359 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1363 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1364 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1366 if (GNUNET_GNS_RECORD_A != rlh->record_type)
1368 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1369 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1371 rh->proc (rh->proc_cls, rh, 0, NULL);
1374 rd.record_type = GNUNET_GNS_RECORD_A;
1375 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1377 rd.data_size = sizeof (struct in_addr);
1379 rh->proc (rh->proc_cls, rh, 1, &rd);
1382 else if (AF_INET6 == af)
1384 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1385 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1387 if (GNUNET_GNS_RECORD_AAAA != rlh->record_type)
1389 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1390 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1392 rh->proc (rh->proc_cls, rh, 0, NULL);
1395 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1396 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1398 rd.data_size = sizeof (struct in6_addr);
1400 rh->proc (rh->proc_cls, rh, 1, &rd);
1404 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1405 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1407 rh->proc (rh->proc_cls, rh, 0, NULL);
1414 * Process VPN lookup result for record
1416 * @param cls the record lookup handle
1417 * @param rh resolver handle
1418 * @param rd_count number of results (1)
1419 * @param rd record data containing the result
1422 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1423 unsigned int rd_count,
1424 const struct GNUNET_NAMESTORE_RecordData *rd)
1426 struct RecordLookupHandle* rlh = cls;
1430 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1431 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1434 /* give up, cannot resolve */
1435 finish_lookup(rh, rlh, 0, NULL);
1439 /* results found yay */
1440 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1441 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1443 finish_lookup(rh, rlh, rd_count, rd);
1448 * Sends a UDP dns query to a nameserver specified in the rh
1450 * @param rh the resolver handle
1453 send_dns_packet (struct ResolverHandle *rh);
1457 handle_dns_resolver (void *cls,
1458 const struct sockaddr *addr,
1461 struct ResolverHandle *rh = cls;
1462 struct RecordLookupHandle *rlh = rh->proc_cls;
1463 struct GNUNET_NAMESTORE_RecordData rd;
1464 struct sockaddr_in *sai;
1465 struct sockaddr_in6 *sai6;
1469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1470 "No address found in DNS!\n");
1471 finish_lookup (rh, rlh, 0, NULL);
1475 if (sizeof (struct sockaddr_in) == addrlen)
1477 sai = (struct sockaddr_in*) addr;
1478 rd.record_type = GNUNET_GNS_RECORD_A;
1479 rd.data_size = sizeof (struct in_addr);
1480 rd.data = &sai->sin_addr;
1482 else if (sizeof (struct sockaddr_in6) == addrlen)
1484 sai6 = (struct sockaddr_in6*) addr;
1485 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1486 rd.data_size = sizeof (struct in6_addr);
1487 rd.data = &sai6->sin6_addr;
1491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1492 "Address length is garbage!\n");
1493 finish_lookup (rh, rlh, 0, NULL);
1496 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1498 finish_lookup (rh, rlh, 1, &rd);
1502 * Resolve DNS name via local stub resolver
1504 * @param rh the resolver handle
1507 resolve_dns_name (struct ResolverHandle *rh)
1509 struct RecordLookupHandle *rlh = rh->proc_cls;
1512 if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1513 (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1516 "Can only resolve A/AAAA via stub... abort\n");
1517 finish_lookup (rh, rlh, 0, NULL);
1521 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1526 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1529 &handle_dns_resolver,
1535 * Read DNS udp packet from socket
1537 * @param cls the resolver handle
1538 * @param tc task context
1541 read_dns_response (void *cls,
1542 const struct GNUNET_SCHEDULER_TaskContext *tc)
1544 struct ResolverHandle *rh = cls;
1545 struct RecordLookupHandle *rlh = rh->proc_cls;
1546 char buf[UINT16_MAX];
1548 struct sockaddr_in addr;
1550 struct GNUNET_DNSPARSER_Packet *packet;
1551 struct GNUNET_NAMESTORE_RecordData rd;
1552 int found_delegation = GNUNET_NO;
1553 int found_cname = GNUNET_NO;
1554 char* delegation_name = NULL;
1555 int zone_offset = 0;
1558 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1559 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1561 /* timeout or shutdown */
1562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1563 "Terminating DNS query %d\n", tc->reason);
1564 finish_lookup (rh, rlh, 0, NULL);
1568 addrlen = sizeof (addr);
1569 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1571 (struct sockaddr*) &addr,
1576 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1577 finish_lookup (rh, rlh, 0, NULL);
1581 packet = GNUNET_DNSPARSER_parse (buf, r);
1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1586 "Failed to parse DNS reply!\n");
1587 finish_lookup (rh, rlh, 0, NULL);
1591 for (i = 0; i < packet->num_answers; i++)
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1594 "Got record type %d (want %d)\n",
1595 packet->answers[i].type,
1597 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1598 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1601 "CNAME... restarting query with %s\n",
1602 packet->answers[i].data.hostname
1604 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1605 found_cname = GNUNET_YES;
1609 if ((packet->answers[i].type == rlh->record_type) &&
1610 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1614 rd.data = packet->answers[i].data.raw.data;
1615 rd.data_size = packet->answers[i].data.raw.data_len;
1616 rd.record_type = packet->answers[i].type;
1618 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1619 finish_lookup (rh, rlh, 1, &rd);
1620 GNUNET_DNSPARSER_free_packet (packet);
1625 if (GNUNET_YES == found_cname)
1627 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1629 if (0 > zone_offset)
1632 /* restart query with CNAME */
1633 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1636 "Asking same server for %s\n", rh->dns_name);
1637 send_dns_packet (rh);
1641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1642 "Trying system resolver for %s\n", rh->dns_name);
1643 resolve_dns_name (rh);
1646 GNUNET_DNSPARSER_free_packet (packet);
1650 for (i = 0; i < packet->num_authority_records; i++)
1653 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1656 "Found NS delegation!\n");
1657 found_delegation = GNUNET_YES;
1658 delegation_name = packet->authority_records[i].data.hostname;
1663 for (i = 0; i < packet->num_additional_records; i++)
1665 if (GNUNET_NO == found_delegation)
1668 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1669 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1671 GNUNET_assert (sizeof (struct in_addr) ==
1672 packet->authority_records[i].data.raw.data_len);
1674 rh->dns_addr.sin_addr =
1675 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1676 send_dns_packet (rh);
1677 GNUNET_DNSPARSER_free_packet (packet);
1682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1683 "Nothing useful in DNS reply!\n");
1684 finish_lookup (rh, rlh, 0, NULL);
1685 GNUNET_DNSPARSER_free_packet (packet);
1690 * Sends a UDP dns query to a nameserver specified in the rh
1692 * @param rh the request handle
1695 send_dns_packet (struct ResolverHandle *rh)
1697 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1698 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1701 "Sending %dbyte DNS query\n",
1702 rh->dns_raw_packet_size);
1704 if (GNUNET_SYSERR ==
1705 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1707 rh->dns_raw_packet_size,
1708 (struct sockaddr*)&rh->dns_addr,
1709 sizeof (struct sockaddr_in)))
1710 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1711 _("Failed to send DNS request to %s\n"),
1712 GNUNET_a2s ((const struct sockaddr *)&rh->dns_addr,
1713 sizeof (struct sockaddr_in)));
1715 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1716 rh->timeout, //FIXME less?
1722 GNUNET_NETWORK_fdset_destroy (rset);
1727 * The final phase of resoution.
1728 * We found a NS RR and want to resolve via DNS
1730 * @param rh the pending lookup handle
1731 * @param rd_count length of record data
1732 * @param rd record data containing VPN RR
1735 resolve_record_dns (struct ResolverHandle *rh,
1737 const struct GNUNET_NAMESTORE_RecordData *rd)
1739 struct GNUNET_DNSPARSER_Query query;
1740 struct GNUNET_DNSPARSER_Packet packet;
1741 struct GNUNET_DNSPARSER_Flags flags;
1742 struct in_addr dnsip;
1743 struct sockaddr_in addr;
1744 struct sockaddr *sa;
1746 struct RecordLookupHandle *rlh = rh->proc_cls;
1748 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1749 memset (rh->dns_name, 0, sizeof (rh->dns_name));
1751 /* We cancel here as to not include the ns lookup in the timeout */
1752 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1754 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1755 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1757 /* Start shortening */
1758 if ((NULL != rh->priv_key) &&
1759 (GNUNET_YES == is_canonical (rh->name)))
1761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1762 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1764 start_shorten (rh->authority_chain_head,
1768 for (i = 0; i < rd_count; i++)
1770 /* Synthesize dns name */
1771 if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1773 strcpy (rh->dns_zone, (char*)rd[i].data);
1774 if (0 == strcmp (rh->name, ""))
1775 strcpy (rh->dns_name, (char*)rd[i].data);
1777 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1780 if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1781 /* need to use memcpy as .data may be unaligned */
1782 memcpy (&dnsip, rd[i].data, sizeof (dnsip));
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1790 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1791 if (NULL == rh->dns_sock)
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1794 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1796 finish_lookup (rh, rlh, 0, NULL);
1800 memset (&addr, 0, sizeof (struct sockaddr_in));
1801 sa = (struct sockaddr *) &addr;
1802 sa->sa_family = AF_INET;
1803 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1805 sizeof (struct sockaddr_in)))
1807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1808 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1810 finish_lookup (rh, rlh, 0, NULL);
1813 query.name = rh->dns_name;
1814 query.type = rlh->record_type;
1815 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1816 memset (&flags, 0, sizeof (flags));
1817 flags.recursion_desired = 1;
1818 flags.checking_disabled = 1;
1819 packet.queries = &query;
1820 packet.answers = NULL;
1821 packet.authority_records = NULL;
1822 packet.num_queries = 1;
1823 packet.num_answers = 0;
1824 packet.num_authority_records = 0;
1825 packet.num_additional_records = 0;
1826 packet.flags = flags;
1828 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1830 &rh->dns_raw_packet,
1831 &rh->dns_raw_packet_size))
1833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1834 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1836 GNUNET_NETWORK_socket_close (rh->dns_sock);
1837 finish_lookup (rh, rlh, 0, NULL);
1841 rh->dns_addr.sin_family = AF_INET;
1842 rh->dns_addr.sin_port = htons (53); //domain
1843 rh->dns_addr.sin_addr = dnsip;
1844 #if HAVE_SOCKADDR_IN_SIN_LEN
1845 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1848 send_dns_packet (rh);
1853 * The final phase of resoution.
1854 * We found a VPN RR and want to request an IPv4/6 address
1856 * @param rh the pending lookup handle
1857 * @param rd_count length of record data
1858 * @param rd record data containing VPN RR
1861 resolve_record_vpn (struct ResolverHandle *rh,
1863 const struct GNUNET_NAMESTORE_RecordData *rd)
1865 struct RecordLookupHandle *rlh = rh->proc_cls;
1866 struct GNUNET_HashCode serv_desc;
1867 struct vpn_data* vpn;
1870 /* We cancel here as to not include the ns lookup in the timeout */
1871 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1873 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1874 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1876 /* Start shortening */
1877 if ((NULL != rh->priv_key) &&
1878 (GNUNET_YES == is_canonical (rh->name)))
1880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1881 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1883 start_shorten (rh->authority_chain_head,
1887 vpn = (struct vpn_data*)rd->data;
1890 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1891 strlen ((char*)&vpn[1]) + 1,
1893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1894 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1897 GNUNET_h2s (&vpn->peer));
1899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1900 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1903 GNUNET_h2s (&serv_desc));
1904 rh->proc = &handle_record_vpn;
1905 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1909 if (NULL == vpn_handle)
1911 vpn_handle = GNUNET_VPN_connect (cfg);
1912 if (NULL == vpn_handle)
1914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1915 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1916 finish_lookup (rh, rh->proc_cls, 0, NULL);
1921 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1922 af, ntohs (vpn->proto),
1923 (struct GNUNET_PeerIdentity *)&vpn->peer,
1926 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1927 &process_record_result_vpn,
1933 * The final phase of resolution.
1934 * rh->name is a name that is canonical and we do not have a delegation.
1935 * Query namestore for this record
1937 * @param rh the pending lookup handle
1940 resolve_record_ns(struct ResolverHandle *rh)
1942 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1944 /* We cancel here as to not include the ns lookup in the timeout */
1945 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1947 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1948 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1950 /* Start shortening */
1951 if ((NULL != rh->priv_key) &&
1952 (GNUNET_YES == is_canonical (rh->name)))
1954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1955 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1957 start_shorten (rh->authority_chain_head,
1962 * Try to resolve this record in our namestore.
1963 * The name to resolve is now in rh->authority_name
1964 * since we tried to resolve it to an authority
1967 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1971 &process_record_result_ns,
1978 * Handle timeout for DHT requests
1980 * @param cls the request handle as closure
1981 * @param tc the task context
1984 dht_authority_lookup_timeout(void *cls,
1985 const struct GNUNET_SCHEDULER_TaskContext *tc)
1987 struct ResolverHandle *rh = cls;
1988 struct RecordLookupHandle *rlh = rh->proc_cls;
1989 char new_name[MAX_DNS_NAME_LENGTH];
1991 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1992 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
1993 rh->id, rh->authority_name, rh->timeout.rel_value);
1995 rh->status |= RSL_TIMED_OUT;
1996 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1997 if (NULL != rh->get_handle)
1998 GNUNET_DHT_get_stop (rh->get_handle);
2000 rh->get_handle = NULL;
2001 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
2003 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2004 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
2006 rh->proc(rh->proc_cls, rh, 0, NULL);
2010 if (0 == strcmp(rh->name, ""))
2013 * promote authority back to name and try to resolve record
2015 strcpy(rh->name, rh->authority_name);
2016 rh->proc(rh->proc_cls, rh, 0, NULL);
2021 * Start resolution in bg
2023 GNUNET_assert (0 < GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
2024 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD));
2026 strcpy(rh->name, new_name);
2028 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2029 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2030 rh->id, rh->name, rlh->record_type);
2032 gns_resolver_lookup_record (rh->authority,
2033 rh->private_local_zone,
2037 GNUNET_TIME_UNIT_FOREVER_REL,
2039 &background_lookup_result_processor,
2041 rh->proc(rh->proc_cls, rh, 0, NULL);
2045 static void resolve_delegation_dht(struct ResolverHandle *rh);
2048 static void resolve_delegation_ns(struct ResolverHandle *rh);
2052 * Namestore resolution for delegation finished. Processing result.
2054 * @param cls the closure
2055 * @param rh resolver handle
2056 * @param rd_count number of results (always 0)
2057 * @param rd record data (always NULL)
2060 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2061 unsigned int rd_count,
2062 const struct GNUNET_NAMESTORE_RecordData *rd);
2066 * This is a callback function that checks for key revocation
2068 * @param cls the pending query
2069 * @param key the key of the zone we did the lookup
2070 * @param expiration expiration date of the record data set in the namestore
2071 * @param name the name for which we need an authority
2072 * @param rd_count the number of records with 'name'
2073 * @param rd the record data
2074 * @param signature the signature of the authority for the record data
2077 process_pkey_revocation_result_ns (void *cls,
2078 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2079 struct GNUNET_TIME_Absolute expiration,
2081 unsigned int rd_count,
2082 const struct GNUNET_NAMESTORE_RecordData *rd,
2083 const struct GNUNET_CRYPTO_RsaSignature *signature)
2085 struct ResolverHandle *rh = cls;
2086 struct GNUNET_TIME_Relative remaining_time;
2089 rh->namestore_task = NULL;
2090 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2092 for (i = 0; i < rd_count; i++)
2094 if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2096 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2097 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2099 rh->status |= RSL_PKEY_REVOKED;
2100 rh->proc (rh->proc_cls, rh, 0, NULL);
2105 if ((NULL == name) ||
2106 (0 == remaining_time.rel_value))
2108 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2109 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2112 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2114 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2115 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2116 rh->id, "+.gads", GNUNET_GNS_RECORD_REV);
2118 gns_resolver_lookup_record(rh->authority,
2119 rh->private_local_zone,
2120 GNUNET_GNS_RECORD_REV,
2123 GNUNET_TIME_UNIT_FOREVER_REL,
2125 &background_lookup_result_processor,
2129 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2130 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2133 * We are done with PKEY resolution if name is empty
2134 * else resolve again with new authority
2136 if (strcmp (rh->name, "") == 0)
2137 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2139 resolve_delegation_ns (rh);
2144 * Callback when record data is put into namestore
2146 * @param cls the closure
2147 * @param success GNUNET_OK on success
2148 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2151 on_namestore_delegation_put_result(void *cls,
2155 struct NamestoreBGTask *nbg = cls;
2157 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2160 if (GNUNET_NO == success)
2162 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2163 "GNS_NS: records already in namestore\n");
2166 else if (GNUNET_YES == success)
2168 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2169 "GNS_NS: records successfully put in namestore\n");
2173 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2174 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2178 * Function called when we get a result from the dht
2179 * for our query. Recursively tries to resolve authorities
2182 * @param cls the request handle
2183 * @param exp lifetime
2184 * @param key the key the record was stored under
2185 * @param get_path get path
2186 * @param get_path_length get path length
2187 * @param put_path put path
2188 * @param put_path_length put path length
2189 * @param type the block type
2190 * @param size the size of the record
2191 * @param data the record data
2194 process_delegation_result_dht(void* cls,
2195 struct GNUNET_TIME_Absolute exp,
2196 const struct GNUNET_HashCode * key,
2197 const struct GNUNET_PeerIdentity *get_path,
2198 unsigned int get_path_length,
2199 const struct GNUNET_PeerIdentity *put_path,
2200 unsigned int put_path_length,
2201 enum GNUNET_BLOCK_Type type,
2202 size_t size, const void *data)
2204 struct ResolverHandle *rh = cls;
2205 struct GNSNameRecordBlock *nrb;
2206 uint32_t num_records;
2208 char* rd_data = (char*) data;
2211 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
2212 struct GNUNET_HashCode zone_hash_double, name_hash_double;
2214 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2215 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2220 nrb = (struct GNSNameRecordBlock*)data;
2222 /* stop dht lookup and timeout task */
2223 GNUNET_DHT_get_stop (rh->get_handle);
2224 rh->get_handle = NULL;
2225 if (rh->dht_heap_node != NULL)
2227 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2228 rh->dht_heap_node = NULL;
2231 num_records = ntohl(nrb->rd_count);
2232 name = (char*)&nrb[1];
2234 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2235 struct NamestoreBGTask *ns_heap_root;
2236 struct NamestoreBGTask *namestore_bg_task;
2238 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2239 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2240 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2245 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2246 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2251 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2252 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2253 rh->id, name, rh->authority_name);
2254 for (i=0; i<num_records; i++)
2257 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2258 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2259 rh->id, name, rh->authority_name);
2260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2261 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2262 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2263 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2264 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2265 rh->id, rd[i].data_size);
2266 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2267 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2268 rh->id, rd[i].flags);
2270 if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2271 (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2272 (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2275 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2277 if (0 == strcmp(rh->name, ""))
2278 strcpy(rh->name, rh->authority_name);
2280 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2281 rh->name, rh->authority_name); //FIXME ret
2286 if ((0 == strcmp(name, rh->authority_name)) &&
2287 (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2290 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2293 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2294 struct AuthorityChain *auth =
2295 GNUNET_malloc(sizeof(struct AuthorityChain));
2296 auth->zone = rh->authority;
2297 memset(auth->name, 0, strlen(rh->authority_name)+1);
2298 strcpy(auth->name, rh->authority_name);
2299 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2300 rh->authority_chain_tail,
2303 if (NULL != rh->rd.data)
2304 GNUNET_free ((void*)rh->rd.data);
2306 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2307 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2308 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2311 /** try to import pkey if private key available */
2312 //if (rh->priv_key && is_canonical (rh->name))
2313 // process_discovered_authority(name, auth->zone,
2314 // rh->authority_chain_tail->zone,
2321 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2322 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2323 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2324 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2326 /* Save to namestore
2327 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2330 if (max_allowed_ns_tasks <=
2331 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2333 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2334 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2336 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2337 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background ns task\n",
2340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2341 "GNS_PHASE_DELEGATE_DHT-%llu: Caching record for %s\n",
2343 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2345 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2347 GNUNET_TIME_absolute_get().abs_value);
2348 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2355 &on_namestore_delegation_put_result, //cont
2356 namestore_bg_task); //cls
2360 if (0 != rh->answered)
2365 * FIXME in this case. should we ask namestore again?
2367 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2368 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2369 rh->id, rh->authority_name, rh->name);
2371 if (0 == strcmp(rh->name, ""))
2373 /* Start shortening */
2374 if ((NULL != rh->priv_key) &&
2375 (GNUNET_YES == is_canonical (rh->name)))
2377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2378 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2380 start_shorten (rh->authority_chain_head,
2385 rh->proc = &handle_delegation_ns;
2388 /* Check for key revocation and delegate */
2389 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2392 GNUNET_GNS_RECORD_REV,
2393 &process_pkey_revocation_result_ns,
2400 * No pkey but name exists
2403 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2404 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2405 rh->id, rh->authority_name, rh->name);
2406 if (0 == strcmp(rh->name, ""))
2407 strcpy(rh->name, rh->authority_name);
2409 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2410 rh->name, rh->authority_name); //FIXME ret
2412 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2413 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2414 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2415 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2417 rh->proc(rh->proc_cls, rh, 0, NULL);
2420 //FIXME maybe define somewhere else?
2421 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2422 +(MAX_DNS_NAME_LENGTH*2)
2423 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2424 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2428 expand_plus(char* dest, char* src, char* repl)
2431 unsigned int s_len = strlen(src)+1;
2433 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2434 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2435 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2438 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2439 "GNS_POSTPROCESS: %s too short\n", src);
2441 /* no postprocessing */
2442 memcpy(dest, src, s_len+1);
2446 if (0 == strcmp(src+s_len-3, ".+"))
2448 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2449 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2450 memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2454 pos += strlen(repl);
2455 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2456 "GNS_POSTPROCESS: Expanded to %s\n", dest);
2460 memcpy(dest, src, s_len+1);
2468 finish_lookup (struct ResolverHandle *rh,
2469 struct RecordLookupHandle* rlh,
2470 unsigned int rd_count,
2471 const struct GNUNET_NAMESTORE_RecordData *rd)
2474 char new_rr_data[MAX_DNS_NAME_LENGTH];
2475 char new_mx_data[MAX_MX_LENGTH];
2476 char new_soa_data[MAX_SOA_LENGTH];
2477 char new_srv_data[MAX_SRV_LENGTH];
2478 struct srv_data *old_srv;
2479 struct srv_data *new_srv;
2480 struct soa_data *old_soa;
2481 struct soa_data *new_soa;
2482 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2485 unsigned int offset;
2487 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
2489 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2490 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2493 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2496 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2498 for (i = 0; i < rd_count; i++)
2501 if ((GNUNET_GNS_RECORD_NS != rd[i].record_type) &&
2502 (GNUNET_GNS_RECORD_PTR != rd[i].record_type) &&
2503 (GNUNET_GNS_RECORD_CNAME != rd[i].record_type) &&
2504 (GNUNET_GNS_RECORD_MX != rd[i].record_type) &&
2505 (GNUNET_GNS_RECORD_SOA != rd[i].record_type) &&
2506 (GNUNET_GNS_RECORD_SRV != rd[i].record_type))
2508 p_rd[i].data = rd[i].data;
2513 * for all those records we 'should'
2514 * also try to resolve the A/AAAA records (RFC1035)
2515 * This is a feature and not important
2518 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2519 "GNS_POSTPROCESS: Postprocessing\n");
2520 if (0 == strcmp(rh->name, "+"))
2521 repl_string = rlh->name;
2523 repl_string = rlh->name+strlen(rh->name)+1;
2526 if (GNUNET_GNS_RECORD_MX == rd[i].record_type)
2528 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2529 offset = sizeof(uint16_t);
2530 pos = new_mx_data+offset;
2531 expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2533 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2534 p_rd[i].data = new_mx_data;
2535 p_rd[i].data_size = offset;
2537 else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2540 * Prio, weight and port
2542 new_srv = (struct srv_data*)new_srv_data;
2543 old_srv = (struct srv_data*)rd[i].data;
2544 new_srv->prio = old_srv->prio;
2545 new_srv->weight = old_srv->weight;
2546 new_srv->port = old_srv->port;
2547 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2549 p_rd[i].data = new_srv_data;
2550 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2552 else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2554 /* expand mname and rname */
2555 old_soa = (struct soa_data*)rd[i].data;
2556 new_soa = (struct soa_data*)new_soa_data;
2557 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2558 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2559 offset = strlen ((char*)&new_soa[1]) + 1;
2560 expand_plus((char*)&new_soa[1] + offset,
2561 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2563 p_rd[i].data_size = sizeof (struct soa_data)
2565 + strlen ((char*)&new_soa[1] + offset);
2566 p_rd[i].data = new_soa_data;
2571 expand_plus(pos, (char*)rd[i].data, repl_string);
2572 p_rd[i].data_size = strlen(new_rr_data)+1;
2573 p_rd[i].data = new_rr_data;
2578 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2580 free_resolver_handle (rh);
2584 * Process DHT lookup result for record.
2586 * @param cls the closure
2587 * @param rh resolver handle
2588 * @param rd_count number of results
2589 * @param rd record data
2592 handle_record_dht(void* cls, struct ResolverHandle *rh,
2593 unsigned int rd_count,
2594 const struct GNUNET_NAMESTORE_RecordData *rd)
2596 struct RecordLookupHandle* rlh = cls;
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2601 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2603 /* give up, cannot resolve */
2604 finish_lookup (rh, rlh, 0, NULL);
2607 /* results found yay */
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2609 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2610 finish_lookup (rh, rlh, rd_count, rd);
2617 * Process namestore lookup result for record.
2619 * @param cls the closure
2620 * @param rh resolver handle
2621 * @param rd_count number of results
2622 * @param rd record data
2625 handle_record_ns (void* cls, struct ResolverHandle *rh,
2626 unsigned int rd_count,
2627 const struct GNUNET_NAMESTORE_RecordData *rd)
2629 struct RecordLookupHandle* rlh = cls;
2630 int check_dht = GNUNET_YES;
2634 /* results found yay */
2635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2636 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2637 finish_lookup (rh, rlh, rd_count, rd);
2641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2642 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2646 * There are 5 conditions that have to met for us to consult the DHT:
2647 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2648 * 2. No entry in the NS existed AND
2649 * 3. The zone queried is not the local resolver's zone AND
2650 * 4. The name that was looked up is '+'
2651 * because if it was any other canonical name we either already queried
2652 * the DHT for the authority in the authority lookup phase (and thus
2653 * would already have an entry in the NS for the record)
2654 * 5. We are not in cache only mode
2656 if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2657 (0 == (rh->status & RSL_RECORD_EXISTS)) )
2660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2661 "GNS_PHASE_REC-%llu: Not expired and exists!\n",
2663 check_dht = GNUNET_NO;
2666 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2667 &rh->private_local_zone))
2670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2671 "GNS_PHASE_REC-%llu: Our zone!\n",
2673 check_dht = GNUNET_NO;
2676 if ((0 != strcmp (rh->name, "+")) && (GNUNET_YES == is_srv (rh->name)))
2677 check_dht = GNUNET_NO;
2679 if (GNUNET_YES == rh->only_cached)
2680 check_dht = GNUNET_NO;
2682 if (GNUNET_YES == check_dht)
2684 rh->proc = &handle_record_dht;
2685 resolve_record_dht(rh);
2688 /* give up, cannot resolve */
2689 finish_lookup (rh, rlh, 0, NULL);
2694 * Move one level up in the domain hierarchy and return the
2695 * passed top level domain.
2697 * @param name the domain
2698 * @param dest the destination where the tld will be put
2701 pop_tld(char* name, char* dest)
2705 if (GNUNET_YES == is_canonical (name))
2712 for (len = strlen(name); 0 < len; len--)
2714 if (*(name+len) == '.')
2724 strcpy(dest, (name+len+1));
2728 * Checks if name is in tld
2730 * @param name the name to check
2731 * @param tld the TLD to check for
2732 * @return GNUNET_YES or GNUNET_NO
2735 is_tld(const char* name, const char* tld)
2739 if (strlen(name) <= strlen(tld))
2742 offset = strlen(name)-strlen(tld);
2743 if (0 != strcmp(name+offset, tld))
2745 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2746 "%s is not in .%s TLD\n", name, tld);
2753 * DHT resolution for delegation finished. Processing result.
2755 * @param cls the closure
2756 * @param rh resolver handle
2757 * @param rd_count number of results (always 0)
2758 * @param rd record data (always NULL)
2761 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2762 unsigned int rd_count,
2763 const struct GNUNET_NAMESTORE_RecordData *rd)
2765 struct RecordLookupHandle* rlh;
2769 if (0 == strcmp(rh->name, ""))
2771 if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2773 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2774 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2776 finish_lookup(rh, rlh, rd_count, rd);
2779 /* We resolved full name for delegation. resolving record */
2780 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2781 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2783 strcpy(rh->name, "+\0");
2784 rh->proc = &handle_record_ns;
2785 resolve_record_ns(rh);
2790 * we still have some left
2792 if (GNUNET_YES == is_canonical (rh->name))
2794 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2795 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2798 rh->proc = &handle_record_ns;
2799 resolve_record_ns(rh);
2802 /* give up, cannot resolve */
2803 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2804 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2806 finish_lookup(rh, rlh, 0, NULL);
2811 * Start DHT lookup for a name -> PKEY (compare NS) record in
2812 * rh->authority's zone
2814 * @param rh the pending gns query
2817 resolve_delegation_dht(struct ResolverHandle *rh)
2820 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2821 struct GNUNET_HashCode name_hash_double;
2822 struct GNUNET_HashCode zone_hash_double;
2823 struct GNUNET_HashCode lookup_key;
2824 struct ResolverHandle *rh_heap_root;
2826 pop_tld(rh->name, rh->authority_name);
2828 //FIXME handle return values here
2829 GNUNET_CRYPTO_short_hash(rh->authority_name,
2830 strlen(rh->authority_name),
2832 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2833 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2834 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2836 rh->dht_heap_node = NULL;
2838 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2840 rh->timeout_cont = &dht_authority_lookup_timeout;
2841 rh->timeout_cont_cls = rh;
2845 if (max_allowed_background_queries <=
2846 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2848 /* terminate oldest lookup */
2849 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2850 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2851 rh_heap_root->get_handle = NULL;
2852 rh_heap_root->dht_heap_node = NULL;
2854 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2855 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2856 rh->id, rh_heap_root->authority_name);
2858 rh_heap_root->proc(rh_heap_root->proc_cls,
2863 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2865 GNUNET_TIME_absolute_get().abs_value);
2868 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2869 GNUNET_assert(rh->get_handle == NULL);
2870 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2871 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2873 DHT_GNS_REPLICATION_LEVEL,
2874 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2877 &process_delegation_result_dht,
2883 * Namestore resolution for delegation finished. Processing result.
2885 * @param cls the closure
2886 * @param rh resolver handle
2887 * @param rd_count number of results (always 0)
2888 * @param rd record data (always NULL)
2891 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2892 unsigned int rd_count,
2893 const struct GNUNET_NAMESTORE_RecordData *rd)
2895 struct RecordLookupHandle* rlh;
2897 int check_dht = GNUNET_YES;
2900 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2901 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2902 rh->id, rh->status);
2904 if (rh->status & RSL_PKEY_REVOKED)
2906 finish_lookup (rh, rlh, 0, NULL);
2910 if (0 == strcmp(rh->name, ""))
2913 /* We resolved full name for delegation. resolving record */
2914 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2915 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2917 if (rh->status & RSL_CNAME_FOUND)
2919 if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2921 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2922 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2924 strcpy (rh->name, rh->authority_name);
2925 finish_lookup (rh, rlh, rd_count, rd);
2930 if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2932 s_len = strlen (rd->data) - 2;
2933 memcpy (rh->name, rd->data, s_len);
2934 rh->name[s_len] = '\0';
2935 resolve_delegation_ns (rh);
2938 else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2940 gns_resolver_lookup_record (rh->authority,
2941 rh->private_local_zone,
2950 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2951 free_resolver_handle (rh);
2957 strcpy (rh->dns_name, (char*)rd->data);
2958 resolve_dns_name (rh);
2963 else if (rh->status & RSL_DELEGATE_VPN)
2965 if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2967 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2968 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2970 finish_lookup(rh, rlh, rd_count, rd);
2973 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2974 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2976 GNUNET_assert (NULL != rd);
2977 rh->proc = &handle_record_vpn;
2978 resolve_record_vpn (rh, rd_count, rd);
2981 else if (rh->status & RSL_DELEGATE_NS)
2983 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2985 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2986 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2988 finish_lookup(rh, rlh, rd_count, rd);
2992 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2993 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2995 GNUNET_assert (NULL != rd);
2996 rh->proc = &handle_record_ns;
2997 resolve_record_dns (rh, rd_count, rd);
3000 else if (rh->status & RSL_DELEGATE_PKEY)
3002 if (rh->status & RSL_PKEY_REVOKED)
3004 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3005 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
3007 finish_lookup (rh, rlh, 0, NULL);
3010 else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
3012 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3013 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
3015 finish_lookup(rh, rlh, rd_count, rd);
3019 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3020 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3022 strcpy(rh->name, "+\0");
3023 rh->proc = &handle_record_ns;
3024 resolve_record_ns(rh);
3028 if (rh->status & RSL_DELEGATE_NS)
3030 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
3032 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3033 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3035 finish_lookup(rh, rlh, rd_count, rd);
3039 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3040 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3042 GNUNET_assert (NULL != rd);
3043 rh->proc = &handle_record_ns;
3044 resolve_record_dns (rh, rd_count, rd);
3049 * we still have some left
3050 * check if authority in ns is fresh
3052 * or we are authority
3055 if ((rh->status & RSL_RECORD_EXISTS) &&
3056 !(rh->status & RSL_RECORD_EXPIRED))
3057 check_dht = GNUNET_NO;
3059 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3060 &rh->private_local_zone))
3061 check_dht = GNUNET_NO;
3063 if (GNUNET_YES == rh->only_cached)
3064 check_dht = GNUNET_NO;
3066 if (GNUNET_YES == check_dht)
3069 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3070 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3072 rh->proc = &handle_delegation_dht;
3073 resolve_delegation_dht(rh);
3077 if (GNUNET_NO == is_canonical (rh->name))
3079 /* give up, cannot resolve */
3080 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3081 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3084 finish_lookup(rh, rlh, rd_count, rd);
3087 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3088 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3091 rh->proc = &handle_record_ns;
3092 resolve_record_ns(rh);
3097 * This is a callback function that should give us only PKEY
3098 * records. Used to query the namestore for the authority (PKEY)
3099 * for 'name'. It will recursively try to resolve the
3100 * authority for a given name from the namestore.
3102 * @param cls the pending query
3103 * @param key the key of the zone we did the lookup
3104 * @param expiration expiration date of the record data set in the namestore
3105 * @param name the name for which we need an authority
3106 * @param rd_count the number of records with 'name'
3107 * @param rd the record data
3108 * @param signature the signature of the authority for the record data
3111 process_delegation_result_ns (void* cls,
3112 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3113 struct GNUNET_TIME_Absolute expiration,
3115 unsigned int rd_count,
3116 const struct GNUNET_NAMESTORE_RecordData *rd,
3117 const struct GNUNET_CRYPTO_RsaSignature *signature)
3119 struct ResolverHandle *rh;
3120 struct GNUNET_TIME_Relative remaining_time;
3121 struct GNUNET_CRYPTO_ShortHashCode zone;
3122 char new_name[MAX_DNS_NAME_LENGTH];
3124 struct GNUNET_TIME_Absolute et;
3126 rh = (struct ResolverHandle *)cls;
3127 rh->namestore_task = NULL;
3128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3129 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3132 GNUNET_CRYPTO_short_hash (key,
3133 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3135 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3142 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3144 rh->status |= RSL_RECORD_EXISTS;
3146 if (remaining_time.rel_value == 0)
3148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3149 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3151 rh->status |= RSL_RECORD_EXPIRED;
3156 * No authority found in namestore.
3161 * We did not find an authority in the namestore
3166 * Promote this authority back to a name maybe it is
3169 if (strcmp (rh->name, "") == 0)
3171 /* simply promote back */
3172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3173 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3174 rh->id, rh->authority_name);
3175 strcpy (rh->name, rh->authority_name);
3179 /* add back to existing name */
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3181 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3182 rh->id, rh->authority_name, rh->name);
3183 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3184 rh->name, rh->authority_name);
3185 strcpy (rh->name, new_name);
3186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3187 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3191 rh->proc (rh->proc_cls, rh, 0, NULL);
3196 * We found an authority that may be able to help us
3197 * move on with query
3198 * Note only 1 pkey should have been returned.. anything else would be strange
3200 for (i=0; i < rd_count;i++)
3204 * A CNAME. Like regular DNS this means the is no other record for this
3207 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3209 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3210 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3213 rh->status |= RSL_CNAME_FOUND;
3214 rh->proc (rh->proc_cls, rh, rd_count, rd);
3221 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3223 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3224 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3226 rh->status |= RSL_DELEGATE_VPN;
3227 rh->proc (rh->proc_cls, rh, rd_count, rd);
3233 * FIXME make optional
3235 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3237 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3238 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3240 rh->status |= RSL_DELEGATE_NS;
3241 rh->proc (rh->proc_cls, rh, rd_count, rd);
3245 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3248 rh->status |= RSL_DELEGATE_PKEY;
3250 if ((ignore_pending_records != 0) &&
3251 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3254 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3260 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3261 et.abs_value = rd[i].expiration_time;
3262 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3266 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3268 if (remaining_time.rel_value == 0)
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3271 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3273 rh->authority_chain_head->fresh = 0;
3274 rh->proc (rh->proc_cls, rh, 0, NULL);
3282 * Resolve rest of query with new authority
3284 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3285 memcpy (&rh->authority, rd[i].data,
3286 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3287 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3288 auth->zone = rh->authority;
3289 memset (auth->name, 0, strlen (rh->authority_name)+1);
3290 strcpy (auth->name, rh->authority_name);
3291 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3292 rh->authority_chain_tail,
3294 if (NULL != rh->rd.data)
3295 GNUNET_free ((void*)(rh->rd.data));
3297 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3298 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3299 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3301 /* Check for key revocation and delegate */
3302 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3305 GNUNET_GNS_RECORD_REV,
3306 &process_pkey_revocation_result_ns,
3315 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3316 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3318 * If we have found some records for the LAST label
3319 * we return the results. Else null.
3321 if (strcmp (rh->name, "") == 0)
3323 /* Start shortening */
3324 if ((rh->priv_key != NULL) &&
3325 (is_canonical (rh->name) == GNUNET_YES))
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3328 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3330 start_shorten (rh->authority_chain_head,
3333 /* simply promote back */
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3335 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3336 rh->id, rh->authority_name);
3337 strcpy (rh->name, rh->authority_name);
3338 rh->proc (rh->proc_cls, rh, rd_count, rd);
3342 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
3343 "%s.%s", rh->name, rh->authority_name);
3344 strcpy (rh->name, new_name);
3345 rh->proc (rh->proc_cls, rh, 0, NULL);
3351 * Resolve the delegation chain for the request in our namestore
3353 * @param rh the resolver handle
3356 resolve_delegation_ns (struct ResolverHandle *rh)
3358 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3359 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3361 pop_tld(rh->name, rh->authority_name);
3362 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3365 GNUNET_GNS_RECORD_ANY,
3366 &process_delegation_result_ns,
3373 * Lookup of a record in a specific zone
3374 * calls lookup result processor on result
3376 * @param zone the root zone
3377 * @param pzone the private local zone
3378 * @param record_type the record type to look up
3379 * @param name the name to look up
3380 * @param key a private key for use with PSEU import (can be NULL)
3381 * @param timeout timeout for resolution
3382 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3383 * @param proc the processor to call on result
3384 * @param cls the closure to pass to proc
3387 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3388 struct GNUNET_CRYPTO_ShortHashCode pzone,
3389 uint32_t record_type,
3391 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3392 struct GNUNET_TIME_Relative timeout,
3394 RecordLookupProcessor proc,
3397 struct ResolverHandle *rh;
3398 struct RecordLookupHandle* rlh;
3399 char string_hash[MAX_DNS_LABEL_LENGTH];
3400 char nzkey[MAX_DNS_LABEL_LENGTH];
3401 char* nzkey_ptr = nzkey;
3403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3404 "Starting resolution for %s (type=%d)!\n",
3408 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3409 (strcmp(GNUNET_GNS_TLD, name) != 0))
3411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3412 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3417 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3418 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3420 memset (rh, 0, sizeof (struct ResolverHandle));
3421 rh->authority = zone;
3425 rh->timeout = timeout;
3426 rh->get_handle = NULL;
3427 rh->private_local_zone = pzone;
3428 rh->only_cached = only_cached;
3429 rh->namestore_task = NULL;
3432 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3437 "No shorten key for resolution\n");
3440 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3443 * Set timeout for authority lookup phase to 1/2
3445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3446 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3447 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3448 GNUNET_TIME_relative_divide(timeout, 2),
3449 &handle_lookup_timeout,
3451 rh->timeout_cont = &dht_authority_lookup_timeout;
3452 rh->timeout_cont_cls = rh;
3456 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3457 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3460 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3463 * Only 'gnunet' given
3465 strcpy(rh->name, "\0");
3469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3470 "Checking for TLD...\n");
3471 if (is_zkey_tld(name) == GNUNET_YES)
3473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3476 * This is a zkey tld
3477 * build hash and use as initial authority
3480 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3481 memcpy(rh->name, name,
3482 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3483 pop_tld(rh->name, string_hash);
3485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3486 "ZKEY is %s!\n", string_hash);
3488 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3490 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3494 "Cannot convert ZKEY `%s' to hash!\n", string_hash);
3496 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3497 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3498 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3501 proc (cls, 0, NULL);
3506 else if (is_gnunet_tld (name) == GNUNET_YES)
3508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3511 * Presumably GNUNET tld
3513 memset (rh->name, 0,
3514 strlen(name)-strlen(GNUNET_GNS_TLD));
3515 memcpy (rh->name, name,
3516 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3520 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3521 "Cannot handle this TLD %s\n", string_hash);
3523 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3524 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3525 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3528 proc (cls, 0, NULL);
3534 * Initialize authority chain
3536 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3537 rh->authority_chain_head->prev = NULL;
3538 rh->authority_chain_head->next = NULL;
3539 rh->authority_chain_tail = rh->authority_chain_head;
3540 rh->authority_chain_head->zone = rh->authority;
3541 strcpy (rh->authority_chain_head->name, "");
3544 * Copy original query into lookup handle
3546 rlh->record_type = record_type;
3547 memset(rlh->name, 0, strlen(name) + 1);
3548 strcpy(rlh->name, name);
3550 rlh->proc_cls = cls;
3552 rh->proc = &handle_delegation_ns;
3553 resolve_delegation_ns(rh);
3556 /******** END Record Resolver ***********/
3559 finish_shorten (struct ResolverHandle *rh,
3560 struct NameShortenHandle *nsh)
3562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3563 "Sending %s as shorten result\n", nsh->result);
3564 nsh->proc (nsh->proc_cls, nsh->result);
3565 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3567 free_resolver_handle (rh);
3572 * Callback calles by namestore for a zone to name
3575 * @param cls the closure
3576 * @param zone_key the zone we queried
3577 * @param expire the expiration time of the name
3578 * @param name the name found or NULL
3579 * @param rd_len number of records for the name
3580 * @param rd the record data (PKEY) for the name
3581 * @param signature the signature for the record data
3584 process_zone_to_name_shorten_root (void *cls,
3585 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3586 struct GNUNET_TIME_Absolute expire,
3588 unsigned int rd_len,
3589 const struct GNUNET_NAMESTORE_RecordData *rd,
3590 const struct GNUNET_CRYPTO_RsaSignature *signature);
3594 * Callback called by namestore for a zone to name
3597 * @param cls the closure
3598 * @param zone_key the zone we queried
3599 * @param expire the expiration time of the name
3600 * @param name the name found or NULL
3601 * @param rd_len number of records for the name
3602 * @param rd the record data (PKEY) for the name
3603 * @param signature the signature for the record data
3606 process_zone_to_name_shorten_shorten (void *cls,
3607 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3608 struct GNUNET_TIME_Absolute expire,
3610 unsigned int rd_len,
3611 const struct GNUNET_NAMESTORE_RecordData *rd,
3612 const struct GNUNET_CRYPTO_RsaSignature *signature)
3614 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3615 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3616 struct AuthorityChain *next_authority;
3618 char result[MAX_DNS_NAME_LENGTH];
3619 char tmp_name[MAX_DNS_NAME_LENGTH];
3622 rh->namestore_task = NULL;
3623 /* we found a match in our own root zone */
3626 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3627 memset(result, 0, answer_len);
3629 if (strlen(rh->name) > 0)
3631 sprintf (result, "%s.%s.%s.%s",
3633 nsh->shorten_zone_name,
3638 sprintf (result, "%s.%s.%s", name,
3639 nsh->shorten_zone_name,
3643 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3644 "Found shorten result %s\n", result);
3645 if (strlen (nsh->result) > strlen (result))
3646 strcpy (nsh->result, result);
3648 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3649 nsh->shorten_zone) == 0)
3652 * This is our zone append .gads unless name is empty
3653 * (it shouldn't be, usually FIXME what happens if we
3654 * shorten to our zone to a "" record??)
3657 sprintf (result, "%s.%s.%s",
3659 nsh->shorten_zone_name,
3661 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3662 "Our zone: Found %s as shorten result\n", result);
3664 if (strlen (nsh->result) > strlen (result))
3665 strcpy (nsh->result, result);
3666 //nsh->proc(nsh->proc_cls, result);
3668 //free_resolver_handle(rh);
3675 * continue with next authority if exists
3677 if ((rh->authority_chain_head->next == NULL))
3679 finish_shorten (rh, nsh);
3682 next_authority = rh->authority_chain_head;
3684 if (0 == strcmp (rh->name, ""))
3685 strcpy (tmp_name, next_authority->name);
3687 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3688 "%s.%s", rh->name, next_authority->name);
3690 strcpy(rh->name, tmp_name);
3691 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3692 "No PSEU found for authority %s. Promoting back: %s\n",
3693 next_authority->name, rh->name);
3695 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3696 rh->authority_chain_tail,
3699 GNUNET_free (next_authority);
3701 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3702 &rh->authority_chain_tail->zone,
3703 &rh->authority_chain_head->zone,
3704 &process_zone_to_name_shorten_root,
3709 * Callback calles by namestore for a zone to name
3712 * @param cls the closure
3713 * @param zone_key the zone we queried
3714 * @param expire the expiration time of the name
3715 * @param name the name found or NULL
3716 * @param rd_len number of records for the name
3717 * @param rd the record data (PKEY) for the name
3718 * @param signature the signature for the record data
3721 process_zone_to_name_shorten_private (void *cls,
3722 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3723 struct GNUNET_TIME_Absolute expire,
3725 unsigned int rd_len,
3726 const struct GNUNET_NAMESTORE_RecordData *rd,
3727 const struct GNUNET_CRYPTO_RsaSignature *signature)
3729 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3730 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3731 struct AuthorityChain *next_authority;
3733 char result[MAX_DNS_NAME_LENGTH];
3734 char tmp_name[MAX_DNS_NAME_LENGTH];
3737 rh->namestore_task = NULL;
3738 /* we found a match in our own root zone */
3741 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3742 memset(result, 0, answer_len);
3744 if (strlen(rh->name) > 0)
3746 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3750 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3753 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3754 "Found shorten result %s\n", result);
3755 if (strlen (nsh->result) > strlen (result))
3756 strcpy (nsh->result, result);
3758 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3759 nsh->private_zone) == 0)
3762 * This is our zone append .gads unless name is empty
3763 * (it shouldn't be, usually FIXME what happens if we
3764 * shorten to our zone to a "" record??)
3767 sprintf (result, "%s.%s.%s",
3768 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3769 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3770 "Our private zone: Found %s as shorten result %s\n", result);
3771 if (strlen (nsh->result) > strlen (result))
3772 strcpy (nsh->result, result);
3775 if (0 != strcmp (nsh->shorten_zone_name, ""))
3777 /* backtrack authorities for names in priv zone */
3778 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3780 &rh->authority_chain_head->zone,
3781 &process_zone_to_name_shorten_shorten,
3788 * continue with next authority if exists
3790 if ((rh->authority_chain_head->next == NULL))
3792 finish_shorten (rh, nsh);
3795 next_authority = rh->authority_chain_head;
3797 if (0 == strcmp (rh->name, ""))
3798 strcpy (tmp_name, next_authority->name);
3800 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3801 "%s.%s", rh->name, next_authority->name);
3803 strcpy(rh->name, tmp_name);
3804 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3805 "No PSEU found for authority %s. Promoting back: %s\n",
3806 next_authority->name, rh->name);
3808 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3809 rh->authority_chain_tail,
3812 GNUNET_free (next_authority);
3814 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3815 &rh->authority_chain_tail->zone,
3816 &rh->authority_chain_head->zone,
3817 &process_zone_to_name_shorten_root,
3823 * Callback calles by namestore for a zone to name
3826 * @param cls the closure
3827 * @param zone_key the zone we queried
3828 * @param expire the expiration time of the name
3829 * @param name the name found or NULL
3830 * @param rd_len number of records for the name
3831 * @param rd the record data (PKEY) for the name
3832 * @param signature the signature for the record data
3835 process_zone_to_name_shorten_root (void *cls,
3836 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3837 struct GNUNET_TIME_Absolute expire,
3839 unsigned int rd_len,
3840 const struct GNUNET_NAMESTORE_RecordData *rd,
3841 const struct GNUNET_CRYPTO_RsaSignature *signature)
3843 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3844 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3845 struct AuthorityChain *next_authority;
3847 char result[MAX_DNS_NAME_LENGTH];
3848 char tmp_name[MAX_DNS_NAME_LENGTH];
3851 rh->namestore_task = NULL;
3852 /* we found a match in our own root zone */
3855 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3856 memset(result, 0, answer_len);
3858 if (strlen(rh->name) > 0)
3860 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3864 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3867 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3868 "Found shorten result %s\n", result);
3869 if (strlen (nsh->result) > strlen (result))
3870 strcpy (nsh->result, result);
3872 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3873 nsh->root_zone) == 0)
3876 * This is our zone append .gads unless name is empty
3877 * (it shouldn't be, usually FIXME what happens if we
3878 * shorten to our zone to a "" record??)
3881 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3882 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3883 "Our zone: Found %s as shorten result\n", result);
3884 if (strlen (nsh->result) > strlen (result))
3885 strcpy (nsh->result, result);
3888 if (NULL != nsh->private_zone)
3890 /* backtrack authorities for names in priv zone */
3891 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3893 &rh->authority_chain_head->zone,
3894 &process_zone_to_name_shorten_private,
3897 else if (NULL != nsh->shorten_zone)
3899 /* backtrack authorities for names in shorten zone */
3900 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3902 &rh->authority_chain_head->zone,
3903 &process_zone_to_name_shorten_shorten,
3910 * continue with next authority if exists
3912 if ((rh->authority_chain_head->next == NULL))
3914 finish_shorten (rh, nsh);
3917 next_authority = rh->authority_chain_head;
3919 if (0 == strcmp (rh->name, ""))
3920 strcpy (tmp_name, next_authority->name);
3922 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3923 "%s.%s", rh->name, next_authority->name);
3925 strcpy(rh->name, tmp_name);
3926 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3927 "No PSEU found for authority %s. Promoting back: %s\n",
3928 next_authority->name, rh->name);
3930 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3931 rh->authority_chain_tail,
3934 GNUNET_free (next_authority);
3936 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3937 &rh->authority_chain_tail->zone,
3938 &rh->authority_chain_head->zone,
3939 &process_zone_to_name_shorten_root,
3946 * Process result from namestore delegation lookup
3947 * for shorten operation
3949 * @param cls the client shorten handle
3950 * @param rh the resolver handle
3951 * @param rd_count number of results (0)
3952 * @param rd data (NULL)
3955 handle_delegation_ns_shorten (void* cls,
3956 struct ResolverHandle *rh,
3958 const struct GNUNET_NAMESTORE_RecordData *rd)
3960 struct NameShortenHandle *nsh;
3961 char result[MAX_DNS_NAME_LENGTH];
3963 nsh = (struct NameShortenHandle *)cls;
3964 rh->namestore_task = NULL;
3966 * At this point rh->name contains the part of the name
3967 * that we do not have a PKEY in our namestore to resolve.
3968 * The authority chain in the resolver handle is now
3969 * useful to backtrack if needed
3972 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3973 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3974 memset(result, 0, sizeof (result));
3976 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3980 * This is our zone append .gads unless name is empty
3981 * (it shouldn't be, usually FIXME what happens if we
3982 * shorten to our zone to a "" record??)
3985 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3986 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3987 "Our zone: Found %s as shorten result\n", result);
3989 if (strlen (nsh->result) > strlen (result))
3990 strcpy (nsh->result, result);
3993 else if (NULL != nsh->private_zone)
3996 * This is our zone append .gads unless name is empty
3997 * (it shouldn't be, usually FIXME what happens if we
3998 * shorten to our zone to a "" record??)
4000 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4001 nsh->private_zone) == 0)
4004 sprintf (result, "%s.%s.%s",
4005 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4006 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4007 "Our zone: Found %s as shorten result in private zone %s\n",
4010 if (strlen (nsh->result) > strlen (result))
4011 strcpy (nsh->result, result);
4014 else if (NULL != nsh->shorten_zone)
4017 * This is our zone append .gads unless name is empty
4018 * (it shouldn't be, usually FIXME what happens if we
4019 * shorten to our zone to a "" record??)
4021 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4022 nsh->shorten_zone) == 0)
4024 sprintf (result, "%s.%s.%s",
4025 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4026 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4027 "Our zone: Found %s as shorten result in shorten zone\n",
4030 if (strlen (nsh->result) > strlen (result))
4031 strcpy (nsh->result, result);
4036 /* backtrack authorities for names */
4037 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4039 &rh->authority_chain_head->zone,
4040 &process_zone_to_name_shorten_root,
4047 * Callback calles by namestore for a zone to name
4050 * @param cls the closure
4051 * @param zone_key the zone we queried
4052 * @param expire the expiration time of the name
4053 * @param name the name found or NULL
4054 * @param rd_len number of records for the name
4055 * @param rd the record data (PKEY) for the name
4056 * @param signature the signature for the record data
4059 process_zone_to_name_zkey(void *cls,
4060 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4061 struct GNUNET_TIME_Absolute expire,
4063 unsigned int rd_len,
4064 const struct GNUNET_NAMESTORE_RecordData *rd,
4065 const struct GNUNET_CRYPTO_RsaSignature *signature)
4067 struct ResolverHandle *rh = cls;
4068 struct NameShortenHandle *nsh = rh->proc_cls;
4069 char new_name[MAX_DNS_NAME_LENGTH];
4071 rh->namestore_task = NULL;
4073 /* zkey not in our zone */
4077 * In this case we have not given this PKEY a name (yet)
4078 * It is either just not in our zone or not even cached
4079 * Since we do not know at this point we will not try to shorten
4080 * because PKEY import will happen if the user follows the zkey
4083 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4084 "No name found for zkey %s returning verbatim!\n", nsh->result);
4085 /*if (strcmp(rh->name, "") != 0)
4086 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4087 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4089 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4090 enc, GNUNET_GNS_TLD_ZKEY);
4092 strcpy (nsh->result, new_name);*/
4094 finish_shorten (rh, nsh);
4098 if (strcmp(rh->name, "") != 0)
4099 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4102 strcpy(new_name, name);
4104 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4105 "Continue shorten for %s!\n", new_name);
4107 strcpy(rh->name, new_name);
4109 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4110 rh->authority_chain_tail = rh->authority_chain_head;
4111 rh->authority_chain_head->zone = rh->authority;
4114 /* Start delegation resolution in our namestore */
4115 resolve_delegation_ns (rh);
4120 * Shorten api from resolver
4122 * @param zone the root zone to use
4123 * @param pzone the private zone to use
4124 * @param szone the shorten zone to use
4125 * @param name the name to shorten
4126 * @param private_zone_name name of the private zone
4127 * @param shorten_zone_name name of the shorten zone
4128 * @param proc the processor to call with result
4129 * @param proc_cls closure to pass to proc
4132 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4133 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4134 struct GNUNET_CRYPTO_ShortHashCode *szone,
4136 const char* private_zone_name,
4137 const char* shorten_zone_name,
4138 ShortenResultProcessor proc,
4141 struct ResolverHandle *rh;
4142 struct NameShortenHandle *nsh;
4143 char string_hash[MAX_DNS_LABEL_LENGTH];
4144 struct GNUNET_CRYPTO_ShortHashCode zkey;
4145 char nzkey[MAX_DNS_LABEL_LENGTH];
4146 char* nzkey_ptr = nzkey;
4149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4150 "Starting shorten for %s!\n", name);
4152 if (is_canonical ((char*)name) == GNUNET_YES)
4154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4155 "%s is canonical. Returning verbatim\n", name);
4156 proc (proc_cls, name);
4160 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4163 nsh->proc_cls = proc_cls;
4164 nsh->root_zone = zone;
4165 nsh->private_zone = pzone;
4166 nsh->shorten_zone = szone;
4167 strcpy (nsh->private_zone_name, private_zone_name);
4168 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4169 strcpy (nsh->result, name);
4172 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4173 rh->authority = *zone;
4175 rh->priv_key = NULL;
4176 rh->namestore_task = NULL;
4177 rh->proc = &handle_delegation_ns_shorten;
4180 rh->private_local_zone = *zone;
4182 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4185 "Checking for TLD...\n");
4186 if (is_zkey_tld (name) == GNUNET_YES)
4188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4191 * This is a zkey tld
4192 * build hash and use as initial authority
4195 memset (rh->name, 0,
4196 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4197 memcpy (rh->name, name,
4198 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4199 pop_tld (rh->name, string_hash);
4201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4202 "ZKEY is %s!\n", string_hash);
4204 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4206 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4210 "Cannot convert ZKEY %s to hash!\n", nzkey);
4213 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4214 proc (proc_cls, name);
4218 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4221 &process_zone_to_name_zkey,
4226 else if (is_gnunet_tld (name) == GNUNET_YES)
4228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4231 * Presumably GNUNET tld
4233 memset (rh->name, 0,
4234 strlen (name)-strlen (GNUNET_GNS_TLD));
4235 memcpy (rh->name, name,
4236 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4243 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4244 proc (proc_cls, name);
4248 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4249 rh->authority_chain_tail = rh->authority_chain_head;
4250 rh->authority_chain_head->zone = *zone;
4253 /* Start delegation resolution in our namestore */
4254 resolve_delegation_ns (rh);
4257 /*********** END NAME SHORTEN ********************/
4260 * Conclude get authority lookup
4262 * @param rh resolver handle
4263 * @param nah get authority lookup handle
4266 finish_get_auth (struct ResolverHandle *rh,
4267 struct GetNameAuthorityHandle *nah)
4269 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4270 "Got authority result %s\n", nah->result);
4272 nah->proc (nah->proc_cls, nah->result);
4273 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4275 free_resolver_handle (rh);
4279 * Process result from namestore delegation lookup
4280 * for get authority operation
4282 * @param cls the client get auth handle
4283 * @param rh the resolver handle
4284 * @param rd_count number of results (0)
4285 * @param rd data (NULL)
4288 handle_delegation_result_ns_get_auth(void* cls,
4289 struct ResolverHandle *rh,
4291 const struct GNUNET_NAMESTORE_RecordData *rd)
4293 struct GetNameAuthorityHandle* nah;
4296 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4299 * At this point rh->name contains the part of the name
4300 * that we do not have a PKEY in our namestore to resolve.
4301 * The authority chain in the resolver handle is now
4302 * useful to backtrack if needed
4305 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4306 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4308 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4309 "Building response!\n");
4310 if (is_canonical (rh->name) == GNUNET_YES)
4313 * We successfully resolved the authority in the ns
4314 * FIXME for our purposes this is fine
4315 * but maybe we want to have an api that also looks
4316 * into the dht (i.e. option in message)
4318 if (strlen(rh->name) > strlen(nah->name))
4320 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4321 "Record name longer than original lookup name... odd!\n");
4325 answer_len = strlen(nah->name) - strlen(rh->name)
4326 + strlen(GNUNET_GNS_TLD) + 1;
4327 memset(nah->result, 0, answer_len);
4328 if (0 != strcmp (rh->name, ""))
4329 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4331 strcpy(nah->result, nah->name);
4333 finish_get_auth (rh, nah);
4337 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4338 "Unable to resolve authority for remaining %s!\n", rh->name);
4339 strcpy(nah->result, "");
4340 finish_get_auth (rh, nah);
4348 * Tries to resolve the authority for name
4351 * @param zone the root zone to look up for
4352 * @param pzone the private local zone
4353 * @param name the name to lookup up
4354 * @param proc the processor to call when finished
4355 * @param proc_cls the closure to pass to the processor
4358 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4359 struct GNUNET_CRYPTO_ShortHashCode pzone,
4361 GetAuthorityResultProcessor proc,
4364 struct ResolverHandle *rh;
4365 struct GetNameAuthorityHandle *nah;
4367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4368 "Starting authority resolution for %s!\n", name);
4370 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4371 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4372 rh->authority = zone;
4374 rh->private_local_zone = pzone;
4375 rh->namestore_task = NULL;
4377 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4379 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4381 strcpy(rh->name, "\0");
4386 strlen(name)-strlen(GNUNET_GNS_TLD));
4387 memcpy(rh->name, name,
4388 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4391 memset(nah->name, 0,
4393 strcpy(nah->name, name);
4395 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4396 rh->authority_chain_tail = rh->authority_chain_head;
4397 rh->authority_chain_head->zone = zone;
4398 rh->proc = &handle_delegation_result_ns_get_auth;
4399 rh->proc_cls = (void*)nah;
4402 nah->proc_cls = proc_cls;
4403 strcpy (nah->result, "");
4405 /* Start delegation resolution in our namestore */
4406 resolve_delegation_ns(rh);
4410 /******** END GET AUTHORITY *************/
4412 /* end of gnunet-service-gns_resolver.c */