2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file gns/gnunet-service-gns_resolver.c
23 * @brief GNUnet GNS resolver logic
24 * @author Martin Schanzenbach
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_vpn_service.h"
33 #include "gnunet_dns_service.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "gns_protocol.h"
37 #include "gnunet_gns_service.h"
38 #include "block_gns.h"
40 #include "gnunet-service-gns_resolver.h"
42 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
43 #define DHT_GNS_REPLICATION_LEVEL 5
44 #define MAX_DNS_LABEL_LENGTH 63
48 * Our handle to the namestore service
50 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
53 * Our handle to the vpn service
55 static struct GNUNET_VPN_Handle *vpn_handle;
58 * Resolver handle to the dht
60 static struct GNUNET_DHT_Handle *dht_handle;
63 * Heap for parallel DHT lookups
65 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
68 * Heap for namestore queues
70 static struct GNUNET_CONTAINER_Heap *ns_task_heap;
73 * Maximum amount of parallel queries in background
75 static unsigned long long max_allowed_background_queries;
78 * Maximum amount of parallel namestore tasks in background
80 static unsigned long long max_allowed_ns_tasks;
83 * Wheather or not to ignore pending records
85 static int ignore_pending_records;
90 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
93 * Background shortening handles
95 static struct GetPseuAuthorityHandle *gph_head;
98 * Background shortening handles
100 static struct GetPseuAuthorityHandle *gph_tail;
103 * Resolver lookup list
105 static struct ResolverHandle *rlh_head;
108 * Resolver lookup list
110 static struct ResolverHandle *rlh_tail;
113 * Resolver shorten list
115 static struct ResolverHandle *nsh_head;
118 * Resolver shorten list
120 static struct ResolverHandle *nsh_tail;
123 * Resolver get auth list
125 static struct ResolverHandle *nah_head;
128 * Resolver get auth list
130 static struct ResolverHandle *nah_tail;
133 * Global configuration.
135 static const struct GNUNET_CONFIGURATION_Handle *cfg;
138 * a resolution identifier pool variable
140 * This is a non critical identifier useful for debugging
142 static unsigned long long rid = 0;
145 * Check if name is in srv format (_x._y.xxx)
148 * @return GNUNET_YES if true
154 int ret = GNUNET_YES;
158 if (NULL == strstr (name, "._"))
161 ndup = GNUNET_strdup (name);
164 if (NULL == strtok (NULL, "."))
167 if (NULL == strtok (NULL, "."))
170 if (NULL != strtok (NULL, "."))
179 * Determine if this name is canonical.
181 * a.b.gads = not canonical
184 * @param name the name to test
185 * @return GNUNET_YES if canonical
188 is_canonical (char* name)
193 ndup = GNUNET_strdup (name);
196 for (tok = strtok (NULL, "."); tok != NULL; tok = strtok (NULL, "."))
212 * Callback that shortens authorities
214 * @param gph the handle containing the name to shorten
217 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
221 * Continueation for pkey record creation (shorten)
223 * @param cls a GetPseuAuthorityHandle
224 * @param success unused
228 create_pkey_cont (void* cls, int32_t success, const char* emsg)
230 //FIXME do sth with error
231 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
233 gph->namestore_task = NULL;
234 GNUNET_free (gph->auth);
235 GNUNET_CRYPTO_rsa_key_free (gph->key);
236 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
242 * Namestore calls this function if we have record for this name.
243 * (or with rd_count=0 to indicate no matches)
245 * @param cls the pending query
246 * @param key the key of the zone we did the lookup
247 * @param expiration expiration date of the namestore entry
248 * @param name the name for which we need an authority
249 * @param rd_count the number of records with 'name'
250 * @param rd the record data
251 * @param signature the signature of the authority for the record data
254 process_pseu_lookup_ns (void* cls,
255 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
256 struct GNUNET_TIME_Absolute expiration,
257 const char *name, unsigned int rd_count,
258 const struct GNUNET_NAMESTORE_RecordData *rd,
259 const struct GNUNET_CRYPTO_RsaSignature *signature)
261 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
262 struct GNUNET_NAMESTORE_RecordData new_pkey;
264 gph->namestore_task = NULL;
267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
268 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
269 GNUNET_free (gph->auth);
270 GNUNET_CRYPTO_rsa_key_free (gph->key);
271 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
280 new_pkey.expiration_time = UINT64_MAX;
281 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
282 new_pkey.data = &gph->auth->zone;
283 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
284 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
285 | GNUNET_NAMESTORE_RF_PRIVATE
286 | GNUNET_NAMESTORE_RF_PENDING;
287 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
291 &create_pkey_cont, //cont
296 * process result of a dht pseu lookup
298 * @param gph the handle
299 * @param name the pseu result or NULL
302 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "GNS_AUTO_PSEU: No PSEU, no shorten. Finished.\n");
308 GNUNET_free (gph->auth);
309 GNUNET_CRYPTO_rsa_key_free (gph->key);
310 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
315 memcpy (gph->test_name, name, strlen(name)+1);
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
321 * Check for collision
323 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
326 GNUNET_NAMESTORE_TYPE_ANY,
327 &process_pseu_lookup_ns,
332 * Handle timeout for dht request
334 * @param cls the request handle as closure
335 * @param tc the task context
338 handle_auth_discovery_timeout (void *cls,
339 const struct GNUNET_SCHEDULER_TaskContext *tc)
341 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
345 GNUNET_DHT_get_stop (gph->get_handle);
346 gph->get_handle = NULL;
347 process_pseu_result (gph, NULL);
351 * Function called when we find a PSEU entry in the DHT
353 * @param cls the request handle
354 * @param exp lifetime
355 * @param key the key the record was stored under
356 * @param get_path get path
357 * @param get_path_length get path length
358 * @param put_path put path
359 * @param put_path_length put path length
360 * @param type the block type
361 * @param size the size of the record
362 * @param data the record data
365 process_auth_discovery_dht_result (void* cls,
366 struct GNUNET_TIME_Absolute exp,
367 const struct GNUNET_HashCode * key,
368 const struct GNUNET_PeerIdentity *get_path,
369 unsigned int get_path_length,
370 const struct GNUNET_PeerIdentity *put_path,
371 unsigned int put_path_length,
372 enum GNUNET_BLOCK_Type type,
376 struct GetPseuAuthorityHandle* gph = cls;
377 struct GNSNameRecordBlock *nrb;
378 char* rd_data = (char*)data;
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
387 /* stop lookup and timeout task */
388 GNUNET_DHT_get_stop (gph->get_handle);
389 gph->get_handle = NULL;
390 GNUNET_SCHEDULER_cancel (gph->timeout);
394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
395 "GNS_GET_AUTH: got dht result null!\n", size);
396 GNUNET_free (gph->auth);
397 GNUNET_CRYPTO_rsa_key_free (gph->key);
398 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
403 nrb = (struct GNSNameRecordBlock*)data;
404 name = (char*)&nrb[1];
405 num_records = ntohl (nrb->rd_count);
407 struct GNUNET_NAMESTORE_RecordData rd[num_records];
409 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
410 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
412 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
418 "GNS_GET_AUTH: Error deserializing data!\n");
422 for (i=0; i < num_records; i++)
424 if ((strcmp (name, "+") == 0) &&
425 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
428 process_pseu_result (gph, (char*)rd[i].data);
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "GNS_GET_AUTH: finished shorten, no results!\n");
436 process_pseu_result (gph, NULL);
440 * Process PSEU discovery for shorten via namestore
442 * @param cls the GetPseuAuthorityHandle
443 * @param key the public key
444 * @param expiration recorddata expiration
445 * @param name the looked up name
446 * @param rd_count number of records in set
447 * @param rd record data
448 * @param signature the signature
451 process_auth_discovery_ns_result (void* cls,
452 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
453 struct GNUNET_TIME_Absolute expiration,
455 unsigned int rd_count,
456 const struct GNUNET_NAMESTORE_RecordData *rd,
457 const struct GNUNET_CRYPTO_RsaSignature *signature)
459 struct GetPseuAuthorityHandle* gph = cls;
460 struct GNUNET_CRYPTO_ShortHashCode name_hash;
461 struct GNUNET_HashCode lookup_key;
462 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
463 struct GNUNET_HashCode name_hash_double;
464 struct GNUNET_HashCode zone_hash_double;
468 gph->namestore_task = NULL;
475 GNUNET_CRYPTO_short_hash ("+", strlen ("+"), &name_hash);
476 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
477 GNUNET_CRYPTO_short_hash_double (&gph->auth->zone, &zone_hash_double);
478 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
479 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
483 "+", (char*)&lookup_key_string);
485 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
486 &handle_auth_discovery_timeout, gph);
488 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
490 GNUNET_assert (gph->get_handle == NULL);
492 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
493 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
495 DHT_GNS_REPLICATION_LEVEL,
496 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
499 &process_auth_discovery_dht_result,
504 for (i=0; i < rd_count; i++)
506 if (0 != (strcmp (name, "+")))
509 if (rd[i].record_type != GNUNET_GNS_RECORD_PSEU)
513 process_pseu_result (gph, (char*)rd[i].data);
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
518 process_pseu_result (gph, NULL);
522 * Callback called by namestore for a zone to name
525 * @param cls the closure
526 * @param zone_key the zone we queried
527 * @param expire the expiration time of the name
528 * @param name the name found or NULL
529 * @param rd_len number of records for the name
530 * @param rd the record data (PKEY) for the name
531 * @param signature the signature for the record data
534 process_zone_to_name_discover (void *cls,
535 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
536 struct GNUNET_TIME_Absolute expire,
539 const struct GNUNET_NAMESTORE_RecordData *rd,
540 const struct GNUNET_CRYPTO_RsaSignature *signature)
542 struct GetPseuAuthorityHandle* gph = cls;
544 gph->namestore_task = NULL;
547 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
550 GNUNET_GNS_RECORD_PSEU,
551 &process_auth_discovery_ns_result,
555 /* we found a match in our own zone */
556 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
557 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
558 GNUNET_free (gph->auth);
559 GNUNET_CRYPTO_rsa_key_free (gph->key);
560 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
566 * Callback that shortens authorities
568 * @param gph the handle to the shorten request
571 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "GNS_AUTO_PSEU: New authority %s discovered\n",
578 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
581 &process_zone_to_name_discover,
587 start_shorten (struct AuthorityChain *auth,
588 struct GNUNET_CRYPTO_RsaPrivateKey *key)
590 struct GetPseuAuthorityHandle *gph;
591 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
592 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
594 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
595 pb_key = GNUNET_CRYPTO_rsa_encode_key (key);
599 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
600 "Failed to encode RSA key on shorten\n");
604 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
605 gph->key = GNUNET_CRYPTO_rsa_decode_key ((char*)pb_key, ntohs (pb_key->len));
607 if (NULL == gph->key)
609 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
610 "Failed to decode RSA key on shorten\n");
614 GNUNET_CRYPTO_short_hash (&pkey,
615 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
617 gph->auth = GNUNET_malloc (sizeof (struct AuthorityChain));
618 memcpy (gph->auth, auth, sizeof (struct AuthorityChain));
619 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
620 shorten_authority_chain (gph);
624 * Initialize the resolver
626 * @param nh the namestore handle
627 * @param dh the dht handle
628 * @param lz the local zone's hash
629 * @param c configuration handle
630 * @param max_bg_queries maximum number of parallel background queries in dht
631 * @param ignore_pending ignore records that still require user confirmation
633 * @return GNUNET_OK on success
636 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
637 struct GNUNET_DHT_Handle *dh,
638 struct GNUNET_CRYPTO_ShortHashCode lz,
639 const struct GNUNET_CONFIGURATION_Handle *c,
640 unsigned long long max_bg_queries,
644 return GNUNET_SYSERR;
646 return GNUNET_SYSERR;
649 namestore_handle = nh;
653 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
655 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
656 max_allowed_background_queries = max_bg_queries;
657 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
658 ignore_pending_records = ignore_pending;
668 GNUNET_RESOLVER_connect (cfg);
676 * @param cls closure to iterator
677 * @param node heap nodes
678 * @param element the namestorebgtask
679 * @param cost heap cost
680 * @return always GNUNET_YES
683 cleanup_pending_ns_tasks (void* cls,
684 struct GNUNET_CONTAINER_HeapNode *node,
686 GNUNET_CONTAINER_HeapCostType cost)
688 struct NamestoreBGTask *nbg = element;
689 ResolverCleanupContinuation cont = cls;
691 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
692 "GNS_CLEANUP: Terminating ns task\n");
693 GNUNET_NAMESTORE_cancel (nbg->qe);
695 GNUNET_CONTAINER_heap_remove_node (node);
697 if (0 == GNUNET_CONTAINER_heap_get_size (ns_task_heap))
707 * @param rh resolver handle
708 * @param rlh record lookup handle
709 * @param rd_count number of results
713 finish_lookup (struct ResolverHandle *rh,
714 struct RecordLookupHandle* rlh,
715 unsigned int rd_count,
716 const struct GNUNET_NAMESTORE_RecordData *rd);
720 * Cleanup background lookups FIXME get rid of this?? YES this doesn't do
721 * anything! => find in code and remove all references to the heap
723 * @param cls closure to iterator
724 * @param node heap nodes
725 * @param element the resolver handle
726 * @param cost heap cost
727 * @return always GNUNET_YES
730 cleanup_pending_background_queries (void* cls,
731 struct GNUNET_CONTAINER_HeapNode *node,
733 GNUNET_CONTAINER_HeapCostType cost)
735 struct ResolverHandle *rh = element;
736 ResolverCleanupContinuation cont = cls;
738 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
739 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
741 GNUNET_CONTAINER_heap_remove_node (node);
742 if (0 == GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
744 if (GNUNET_CONTAINER_heap_get_size (ns_task_heap) == 0)
748 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
749 &cleanup_pending_ns_tasks,
760 * Helper function to free resolver handle
762 * @param rh the handle to free
765 free_resolver_handle (struct ResolverHandle* rh)
767 struct AuthorityChain *ac;
768 struct AuthorityChain *ac_next;
773 ac = rh->authority_chain_head;
782 if (NULL != rh->get_handle)
783 GNUNET_DHT_get_stop (rh->get_handle);
785 if (NULL != rh->dns_raw_packet)
786 GNUNET_free (rh->dns_raw_packet);
788 if (NULL != rh->namestore_task)
789 GNUNET_NAMESTORE_cancel (rh->namestore_task);
790 rh->namestore_task = NULL;
792 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
793 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
795 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
796 GNUNET_SCHEDULER_cancel (rh->timeout_task);
798 if (NULL != rh->dns_sock)
799 GNUNET_NETWORK_socket_close (rh->dns_sock);
800 if (NULL != rh->dns_resolver_handle)
801 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
803 if (NULL != rh->rd.data)
804 GNUNET_free ((void*)(rh->rd.data));
812 * @param rh resolver handle
813 * @param nsh name shorten handle
816 finish_shorten (struct ResolverHandle *rh,
817 struct NameShortenHandle *nsh);
821 * @param rh resolver handle
822 * @param nah get name authority handle
825 finish_get_auth (struct ResolverHandle *rh,
826 struct GetNameAuthorityHandle* rlh);
831 gns_resolver_cleanup (ResolverCleanupContinuation cont)
834 struct GetPseuAuthorityHandle *tmp;
838 for (tmp = gph_head; tmp != NULL; tmp = gph_head)
840 if (tmp->get_handle != NULL)
841 GNUNET_DHT_get_stop (tmp->get_handle);
842 tmp->get_handle = NULL;
843 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
844 GNUNET_SCHEDULER_cancel (tmp->timeout);
845 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
847 if (NULL != tmp->namestore_task)
848 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
849 tmp->namestore_task = NULL;
850 GNUNET_free (tmp->auth);
851 GNUNET_CRYPTO_rsa_key_free (tmp->key);
852 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, tmp);
856 while (NULL != rlh_head)
858 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
860 while (NULL != nsh_head)
862 finish_shorten (nsh_head, nsh_head->proc_cls);
864 while (NULL != nah_head)
866 finish_get_auth (nah_head, nah_head->proc_cls);
869 s = GNUNET_CONTAINER_heap_get_size (dht_lookup_heap);
870 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
871 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
873 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
874 &cleanup_pending_background_queries,
876 else if (0 != GNUNET_CONTAINER_heap_get_size (ns_task_heap))
878 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
879 &cleanup_pending_ns_tasks,
890 * Callback when record data is put into namestore
892 * @param cls the closure
893 * @param success GNUNET_OK on success
894 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
897 on_namestore_record_put_result (void *cls,
901 struct NamestoreBGTask *nbg = cls;
903 GNUNET_CONTAINER_heap_remove_node (nbg->node);
906 if (GNUNET_NO == success)
908 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
909 "GNS_NS: records already in namestore\n");
912 else if (GNUNET_YES == success)
914 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
915 "GNS_NS: records successfully put in namestore\n");
919 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
920 "GNS_NS: Error putting records into namestore: %s\n", emsg);
925 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
927 struct ResolverHandle *rh = cls;
929 if (NULL != rh->timeout_cont)
930 rh->timeout_cont (rh->timeout_cont_cls, tc);
931 /* FIXME: does this leak memory? */
936 * Processor for background lookups in the DHT
938 * @param cls closure (NULL)
939 * @param rd_count number of records found (not 0)
940 * @param rd record data
943 background_lookup_result_processor (void *cls,
945 const struct GNUNET_NAMESTORE_RecordData *rd)
947 //We could do sth verbose/more useful here but it doesn't make any difference
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
949 "GNS_BG: background dht lookup finished. (%d results)\n",
954 * Handle timeout for DHT requests
956 * @param cls the request handle as closure
957 * @param tc the task context
960 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
962 struct ResolverHandle *rh = cls;
963 struct RecordLookupHandle *rlh = rh->proc_cls;
964 char new_name[MAX_DNS_NAME_LENGTH];
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
968 rh->id, rh->name, rh->timeout.rel_value);
970 * Start resolution in bg
972 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
973 rh->name, GNUNET_GNS_TLD);
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
976 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
977 rh->id, new_name, rlh->record_type);
979 gns_resolver_lookup_record (rh->authority,
980 rh->private_local_zone,
984 GNUNET_TIME_UNIT_FOREVER_REL,
986 &background_lookup_result_processor,
989 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
991 GNUNET_DHT_get_stop (rh->get_handle);
992 rh->get_handle = NULL;
993 rh->proc (rh->proc_cls, rh, 0, NULL);
998 * Function called when we get a result from the dht
999 * for our record query
1001 * @param cls the request handle
1002 * @param exp lifetime
1003 * @param key the key the record was stored under
1004 * @param get_path get path
1005 * @param get_path_length get path length
1006 * @param put_path put path
1007 * @param put_path_length put path length
1008 * @param type the block type
1009 * @param size the size of the record
1010 * @param data the record data
1013 process_record_result_dht (void* cls,
1014 struct GNUNET_TIME_Absolute exp,
1015 const struct GNUNET_HashCode * key,
1016 const struct GNUNET_PeerIdentity *get_path,
1017 unsigned int get_path_length,
1018 const struct GNUNET_PeerIdentity *put_path,
1019 unsigned int put_path_length,
1020 enum GNUNET_BLOCK_Type type,
1021 size_t size, const void *data)
1023 struct ResolverHandle *rh;
1024 struct RecordLookupHandle *rlh;
1025 struct GNSNameRecordBlock *nrb;
1026 uint32_t num_records;
1028 char* rd_data = (char*)data;
1032 rh = (struct ResolverHandle *)cls;
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1034 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
1036 rlh = (struct RecordLookupHandle *) rh->proc_cls;
1037 nrb = (struct GNSNameRecordBlock*)data;
1039 /* stop lookup and timeout task */
1040 GNUNET_DHT_get_stop (rh->get_handle);
1041 rh->get_handle = NULL;
1043 if (rh->dht_heap_node != NULL)
1045 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1046 rh->dht_heap_node = NULL;
1049 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1051 GNUNET_SCHEDULER_cancel (rh->timeout_task);
1052 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1055 rh->get_handle = NULL;
1056 name = (char*)&nrb[1];
1057 num_records = ntohl (nrb->rd_count);
1059 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1060 struct NamestoreBGTask *ns_heap_root;
1061 struct NamestoreBGTask *namestore_bg_task;
1063 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
1064 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
1066 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1071 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1072 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1073 rh->proc (rh->proc_cls, rh, 0, NULL);
1077 for (i=0; i<num_records; i++)
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1081 rh->id, name, rh->name);
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1083 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1084 rh->id, rd[i].record_type, rlh->record_type);
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1087 rh->id, rd[i].data_size);
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1089 "GNS_PHASE_REC-%llu: Got flag %d\n",
1090 rh->id, rd[i].flags);
1092 if ((strcmp (name, rh->name) == 0) &&
1093 (rd[i].record_type == rlh->record_type))
1101 * FIXME check pubkey against existing key in namestore?
1102 * https://gnunet.org/bugs/view.php?id=2179
1104 if (max_allowed_ns_tasks <=
1105 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1107 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1108 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1115 /* Save to namestore */
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 "GNS_PHASE_REC-%llu: Caching record for %s\n",
1119 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1120 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1127 &on_namestore_record_put_result, //cont
1130 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1132 GNUNET_TIME_absolute_get().abs_value);
1135 if (0 < rh->answered)
1136 rh->proc (rh->proc_cls, rh, num_records, rd);
1138 rh->proc (rh->proc_cls, rh, 0, NULL);
1145 * Start DHT lookup for a (name -> query->record_type) record in
1146 * rh->authority's zone
1148 * @param rh the pending gns query context
1151 resolve_record_dht (struct ResolverHandle *rh)
1154 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1155 struct GNUNET_HashCode lookup_key;
1156 struct GNUNET_HashCode name_hash_double;
1157 struct GNUNET_HashCode zone_hash_double;
1158 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
1159 struct RecordLookupHandle *rlh = rh->proc_cls;
1160 struct ResolverHandle *rh_heap_root;
1162 GNUNET_CRYPTO_short_hash (rh->name, strlen (rh->name), &name_hash);
1163 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
1164 GNUNET_CRYPTO_short_hash_double (&rh->authority, &zone_hash_double);
1165 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
1166 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
1168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1169 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1170 rh->id, rh->name, (char*)&lookup_key_string);
1172 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1173 rh->dht_heap_node = NULL;
1175 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
1178 * Update timeout if necessary
1180 if (GNUNET_SCHEDULER_NO_TASK == rh->timeout_task)
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "GNS_PHASE_REC-%llu: Adjusting timeout\n", rh->id);
1185 * Set timeout for authority lookup phase to 1/2
1187 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1188 GNUNET_TIME_relative_divide (rh->timeout, 2),
1189 &handle_lookup_timeout,
1192 rh->timeout_cont = &dht_lookup_timeout;
1193 rh->timeout_cont_cls = rh;
1197 if (max_allowed_background_queries <=
1198 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1200 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1201 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1202 rh_heap_root->get_handle = NULL;
1203 rh_heap_root->dht_heap_node = NULL;
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1206 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1207 rh->id, rh_heap_root->name);
1208 rh_heap_root->proc (rh_heap_root->proc_cls,
1213 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1215 GNUNET_TIME_absolute_get ().abs_value);
1218 xquery = htonl (rlh->record_type);
1220 GNUNET_assert (rh->get_handle == NULL);
1221 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1222 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1224 DHT_GNS_REPLICATION_LEVEL,
1225 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1228 &process_record_result_dht,
1235 * Namestore calls this function if we have record for this name.
1236 * (or with rd_count=0 to indicate no matches)
1238 * @param cls the pending query
1239 * @param key the key of the zone we did the lookup
1240 * @param expiration expiration date of the namestore entry
1241 * @param name the name for which we need an authority
1242 * @param rd_count the number of records with 'name'
1243 * @param rd the record data
1244 * @param signature the signature of the authority for the record data
1247 process_record_result_ns (void* cls,
1248 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1249 struct GNUNET_TIME_Absolute expiration,
1250 const char *name, unsigned int rd_count,
1251 const struct GNUNET_NAMESTORE_RecordData *rd,
1252 const struct GNUNET_CRYPTO_RsaSignature *signature)
1254 struct ResolverHandle *rh;
1255 struct RecordLookupHandle *rlh;
1256 struct GNUNET_TIME_Relative remaining_time;
1257 struct GNUNET_CRYPTO_ShortHashCode zone;
1258 struct GNUNET_TIME_Absolute et;
1264 rh->namestore_task = NULL;
1265 GNUNET_CRYPTO_short_hash (key,
1266 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1268 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1276 rh->status |= RSL_RECORD_EXISTS;
1278 if (remaining_time.rel_value == 0)
1280 rh->status |= RSL_RECORD_EXPIRED;
1287 * Lookup terminated and no results
1289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1290 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1293 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1294 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1297 * Our zone and no result? Cannot resolve TT
1299 rh->proc(rh->proc_cls, rh, 0, NULL);
1303 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1304 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1306 for (i=0; i<rd_count;i++)
1308 if (rd[i].record_type != rlh->record_type)
1311 if (ignore_pending_records &&
1312 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1314 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1315 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1320 //FIXME: eh? do I have to handle this here?
1321 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1322 et.abs_value = rd[i].expiration_time;
1323 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
1325 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1326 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1336 if (0 == rh->answered)
1338 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1339 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1340 rh->proc(rh->proc_cls, rh, 0, NULL);
1344 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1345 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1346 rh->id, rh->answered, rd_count);
1347 rh->proc(rh->proc_cls, rh, rd_count, rd);
1352 * VPN redirect result callback
1354 * @param cls the resolver handle
1355 * @param af the requested address family
1356 * @param address in_addr(6) respectively
1359 process_record_result_vpn (void* cls, int af, const void *address)
1361 struct ResolverHandle *rh = cls;
1362 struct RecordLookupHandle *rlh;
1363 struct GNUNET_NAMESTORE_RecordData rd;
1367 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1368 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1372 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1373 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1375 if (GNUNET_GNS_RECORD_A != rlh->record_type)
1377 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1378 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1380 rh->proc (rh->proc_cls, rh, 0, NULL);
1383 rd.record_type = GNUNET_GNS_RECORD_A;
1384 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1386 rd.data_size = sizeof (struct in_addr);
1388 rh->proc (rh->proc_cls, rh, 1, &rd);
1391 else if (AF_INET6 == af)
1393 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1394 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1396 if (GNUNET_GNS_RECORD_AAAA != rlh->record_type)
1398 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1399 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1401 rh->proc (rh->proc_cls, rh, 0, NULL);
1404 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1405 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1407 rd.data_size = sizeof (struct in6_addr);
1409 rh->proc (rh->proc_cls, rh, 1, &rd);
1413 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1414 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1416 rh->proc (rh->proc_cls, rh, 0, NULL);
1423 * Process VPN lookup result for record
1425 * @param cls the record lookup handle
1426 * @param rh resolver handle
1427 * @param rd_count number of results (1)
1428 * @param rd record data containing the result
1431 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1432 unsigned int rd_count,
1433 const struct GNUNET_NAMESTORE_RecordData *rd)
1435 struct RecordLookupHandle* rlh = cls;
1439 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1440 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1443 /* give up, cannot resolve */
1444 finish_lookup(rh, rlh, 0, NULL);
1448 /* results found yay */
1449 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1450 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1452 finish_lookup(rh, rlh, rd_count, rd);
1457 * Sends a UDP dns query to a nameserver specified in the rh
1459 * @param rh the resolver handle
1462 send_dns_packet (struct ResolverHandle *rh);
1466 handle_dns_resolver (void *cls,
1467 const struct sockaddr *addr,
1470 struct ResolverHandle *rh = cls;
1471 struct RecordLookupHandle *rlh = rh->proc_cls;
1472 struct GNUNET_NAMESTORE_RecordData rd;
1473 struct sockaddr_in *sai;
1474 struct sockaddr_in6 *sai6;
1478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1479 "No address found in DNS!\n");
1480 finish_lookup (rh, rlh, 0, NULL);
1484 if (sizeof (struct sockaddr_in) == addrlen)
1486 sai = (struct sockaddr_in*) addr;
1487 rd.record_type = GNUNET_GNS_RECORD_A;
1488 rd.data_size = sizeof (struct in_addr);
1489 rd.data = &sai->sin_addr;
1491 else if (sizeof (struct sockaddr_in6) == addrlen)
1493 sai6 = (struct sockaddr_in6*) addr;
1494 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1495 rd.data_size = sizeof (struct in6_addr);
1496 rd.data = &sai6->sin6_addr;
1500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1501 "Address length is garbage!\n");
1502 finish_lookup (rh, rlh, 0, NULL);
1505 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1507 finish_lookup (rh, rlh, 1, &rd);
1511 * Resolve DNS name via local stub resolver
1513 * @param rh the resolver handle
1516 resolve_dns_name (struct ResolverHandle *rh)
1518 struct RecordLookupHandle *rlh = rh->proc_cls;
1521 if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1522 (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525 "Can only resolve A/AAAA via stub... abort\n");
1526 finish_lookup (rh, rlh, 0, NULL);
1530 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1535 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1538 &handle_dns_resolver,
1544 * Read DNS udp packet from socket
1546 * @param cls the resolver handle
1547 * @param tc task context
1550 read_dns_response (void *cls,
1551 const struct GNUNET_SCHEDULER_TaskContext *tc)
1553 struct ResolverHandle *rh = cls;
1554 struct RecordLookupHandle *rlh = rh->proc_cls;
1555 char buf[UINT16_MAX];
1557 struct sockaddr_in addr;
1559 struct GNUNET_DNSPARSER_Packet *packet;
1560 struct GNUNET_NAMESTORE_RecordData rd;
1561 int found_delegation = GNUNET_NO;
1562 int found_cname = GNUNET_NO;
1563 char* delegation_name = NULL;
1564 int zone_offset = 0;
1567 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1568 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1570 /* timeout or shutdown */
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1572 "Terminating DNS query %d\n", tc->reason);
1573 finish_lookup (rh, rlh, 0, NULL);
1577 addrlen = sizeof (addr);
1578 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1580 (struct sockaddr*) &addr,
1585 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1586 finish_lookup (rh, rlh, 0, NULL);
1590 packet = GNUNET_DNSPARSER_parse (buf, r);
1594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1595 "Failed to parse DNS reply!\n");
1596 finish_lookup (rh, rlh, 0, NULL);
1600 for (i = 0; i < packet->num_answers; i++)
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "Got record type %d (want %d)\n",
1604 packet->answers[i].type,
1606 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1607 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1610 "CNAME... restarting query with %s\n",
1611 packet->answers[i].data.hostname
1613 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1614 found_cname = GNUNET_YES;
1618 if ((packet->answers[i].type == rlh->record_type) &&
1619 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1623 rd.data = packet->answers[i].data.raw.data;
1624 rd.data_size = packet->answers[i].data.raw.data_len;
1625 rd.record_type = packet->answers[i].type;
1627 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1628 finish_lookup (rh, rlh, 1, &rd);
1629 GNUNET_DNSPARSER_free_packet (packet);
1634 if (GNUNET_YES == found_cname)
1636 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1638 if (0 > zone_offset)
1641 /* restart query with CNAME */
1642 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1645 "Asking same server for %s\n", rh->dns_name);
1646 send_dns_packet (rh);
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1651 "Trying system resolver for %s\n", rh->dns_name);
1652 resolve_dns_name (rh);
1655 GNUNET_DNSPARSER_free_packet (packet);
1659 for (i = 0; i < packet->num_authority_records; i++)
1662 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1665 "Found NS delegation!\n");
1666 found_delegation = GNUNET_YES;
1667 delegation_name = packet->authority_records[i].data.hostname;
1672 for (i = 0; i < packet->num_additional_records; i++)
1674 if (GNUNET_NO == found_delegation)
1677 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1678 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1680 GNUNET_assert (sizeof (struct in_addr) ==
1681 packet->authority_records[i].data.raw.data_len);
1683 rh->dns_addr.sin_addr =
1684 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1685 send_dns_packet (rh);
1686 GNUNET_DNSPARSER_free_packet (packet);
1691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1692 "Nothing useful in DNS reply!\n");
1693 finish_lookup (rh, rlh, 0, NULL);
1694 GNUNET_DNSPARSER_free_packet (packet);
1699 * Sends a UDP dns query to a nameserver specified in the rh
1701 * @param rh the request handle
1704 send_dns_packet (struct ResolverHandle *rh)
1706 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1707 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1710 "Sending %dbyte DNS query\n",
1711 rh->dns_raw_packet_size);
1713 if (GNUNET_SYSERR ==
1714 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1716 rh->dns_raw_packet_size,
1717 (struct sockaddr*)&rh->dns_addr,
1718 sizeof (struct sockaddr_in)))
1719 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1720 _("Failed to send DNS request to %s\n"),
1721 GNUNET_a2s ((const struct sockaddr *)&rh->dns_addr,
1722 sizeof (struct sockaddr_in)));
1724 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1725 rh->timeout, //FIXME less?
1731 GNUNET_NETWORK_fdset_destroy (rset);
1736 * The final phase of resoution.
1737 * We found a NS RR and want to resolve via DNS
1739 * @param rh the pending lookup handle
1740 * @param rd_count length of record data
1741 * @param rd record data containing VPN RR
1744 resolve_record_dns (struct ResolverHandle *rh,
1746 const struct GNUNET_NAMESTORE_RecordData *rd)
1748 struct GNUNET_DNSPARSER_Query query;
1749 struct GNUNET_DNSPARSER_Packet packet;
1750 struct GNUNET_DNSPARSER_Flags flags;
1751 struct in_addr dnsip;
1752 struct sockaddr_in addr;
1753 struct sockaddr *sa;
1755 struct RecordLookupHandle *rlh = rh->proc_cls;
1757 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1759 /* We cancel here as to not include the ns lookup in the timeout */
1760 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1762 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1763 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1765 /* Start shortening */
1766 if ((NULL != rh->priv_key) &&
1767 (GNUNET_YES == is_canonical (rh->name)))
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1772 start_shorten (rh->authority_chain_head,
1776 for (i = 0; i < rd_count; i++)
1778 /* Synthesize dns name */
1779 if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1781 strcpy (rh->dns_zone, (char*)rd[i].data);
1782 if (0 == strcmp (rh->name, ""))
1783 strcpy (rh->dns_name, (char*)rd[i].data);
1785 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1788 if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1789 /* need to use memcpy as .data may be unaligned */
1790 memcpy (&dnsip, rd[i].data, sizeof (dnsip));
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1794 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1798 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1799 if (NULL == rh->dns_sock)
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1802 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1804 finish_lookup (rh, rlh, 0, NULL);
1808 memset (&addr, 0, sizeof (struct sockaddr_in));
1809 sa = (struct sockaddr *) &addr;
1810 sa->sa_family = AF_INET;
1811 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1813 sizeof (struct sockaddr_in)))
1815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1816 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1818 finish_lookup (rh, rlh, 0, NULL);
1821 query.name = rh->dns_name;
1822 query.type = rlh->record_type;
1823 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1824 memset (&flags, 0, sizeof (flags));
1825 flags.recursion_desired = 1;
1826 flags.checking_disabled = 1;
1827 packet.queries = &query;
1828 packet.answers = NULL;
1829 packet.authority_records = NULL;
1830 packet.num_queries = 1;
1831 packet.num_answers = 0;
1832 packet.num_authority_records = 0;
1833 packet.num_additional_records = 0;
1834 packet.flags = flags;
1836 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1838 &rh->dns_raw_packet,
1839 &rh->dns_raw_packet_size))
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1842 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1844 GNUNET_NETWORK_socket_close (rh->dns_sock);
1845 finish_lookup (rh, rlh, 0, NULL);
1849 rh->dns_addr.sin_family = AF_INET;
1850 rh->dns_addr.sin_port = htons (53); //domain
1851 rh->dns_addr.sin_addr = dnsip;
1852 #if HAVE_SOCKADDR_IN_SIN_LEN
1853 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1856 send_dns_packet (rh);
1861 * The final phase of resoution.
1862 * We found a VPN RR and want to request an IPv4/6 address
1864 * @param rh the pending lookup handle
1865 * @param rd_count length of record data
1866 * @param rd record data containing VPN RR
1869 resolve_record_vpn (struct ResolverHandle *rh,
1871 const struct GNUNET_NAMESTORE_RecordData *rd)
1873 struct RecordLookupHandle *rlh = rh->proc_cls;
1874 struct GNUNET_HashCode serv_desc;
1875 struct vpn_data* vpn;
1878 /* We cancel here as to not include the ns lookup in the timeout */
1879 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1881 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1882 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1884 /* Start shortening */
1885 if ((NULL != rh->priv_key) &&
1886 (GNUNET_YES == is_canonical (rh->name)))
1888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1889 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1891 start_shorten (rh->authority_chain_head,
1895 vpn = (struct vpn_data*)rd->data;
1898 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1899 strlen ((char*)&vpn[1]) + 1,
1901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1902 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1905 GNUNET_h2s (&vpn->peer));
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1908 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1911 GNUNET_h2s (&serv_desc));
1912 rh->proc = &handle_record_vpn;
1913 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1917 if (NULL == vpn_handle)
1919 vpn_handle = GNUNET_VPN_connect (cfg);
1920 if (NULL == vpn_handle)
1922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1923 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1924 finish_lookup (rh, rh->proc_cls, 0, NULL);
1929 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1930 af, ntohs (vpn->proto),
1931 (struct GNUNET_PeerIdentity *)&vpn->peer,
1934 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1935 &process_record_result_vpn,
1941 * The final phase of resolution.
1942 * rh->name is a name that is canonical and we do not have a delegation.
1943 * Query namestore for this record
1945 * @param rh the pending lookup handle
1948 resolve_record_ns(struct ResolverHandle *rh)
1950 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1952 /* We cancel here as to not include the ns lookup in the timeout */
1953 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1955 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1956 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1958 /* Start shortening */
1959 if ((NULL != rh->priv_key) &&
1960 (GNUNET_YES == is_canonical (rh->name)))
1962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1963 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1965 start_shorten (rh->authority_chain_head,
1970 * Try to resolve this record in our namestore.
1971 * The name to resolve is now in rh->authority_name
1972 * since we tried to resolve it to an authority
1975 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1979 &process_record_result_ns,
1986 * Handle timeout for DHT requests
1988 * @param cls the request handle as closure
1989 * @param tc the task context
1992 dht_authority_lookup_timeout(void *cls,
1993 const struct GNUNET_SCHEDULER_TaskContext *tc)
1995 struct ResolverHandle *rh = cls;
1996 struct RecordLookupHandle *rlh = rh->proc_cls;
1997 char new_name[MAX_DNS_NAME_LENGTH];
1999 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2000 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
2001 rh->id, rh->authority_name, rh->timeout.rel_value);
2003 rh->status |= RSL_TIMED_OUT;
2004 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2005 if (NULL != rh->get_handle)
2006 GNUNET_DHT_get_stop (rh->get_handle);
2008 rh->get_handle = NULL;
2009 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
2011 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2012 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
2014 rh->proc(rh->proc_cls, rh, 0, NULL);
2018 if (0 == strcmp(rh->name, ""))
2021 * promote authority back to name and try to resolve record
2023 strcpy(rh->name, rh->authority_name);
2024 rh->proc(rh->proc_cls, rh, 0, NULL);
2029 * Start resolution in bg
2031 GNUNET_assert (0 < GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
2032 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD));
2034 strcpy(rh->name, new_name);
2036 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2037 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2038 rh->id, rh->name, rlh->record_type);
2040 gns_resolver_lookup_record (rh->authority,
2041 rh->private_local_zone,
2045 GNUNET_TIME_UNIT_FOREVER_REL,
2047 &background_lookup_result_processor,
2049 rh->proc(rh->proc_cls, rh, 0, NULL);
2053 static void resolve_delegation_dht(struct ResolverHandle *rh);
2056 static void resolve_delegation_ns(struct ResolverHandle *rh);
2060 * Namestore resolution for delegation finished. Processing result.
2062 * @param cls the closure
2063 * @param rh resolver handle
2064 * @param rd_count number of results (always 0)
2065 * @param rd record data (always NULL)
2068 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2069 unsigned int rd_count,
2070 const struct GNUNET_NAMESTORE_RecordData *rd);
2074 * This is a callback function that checks for key revocation
2076 * @param cls the pending query
2077 * @param key the key of the zone we did the lookup
2078 * @param expiration expiration date of the record data set in the namestore
2079 * @param name the name for which we need an authority
2080 * @param rd_count the number of records with 'name'
2081 * @param rd the record data
2082 * @param signature the signature of the authority for the record data
2085 process_pkey_revocation_result_ns (void *cls,
2086 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2087 struct GNUNET_TIME_Absolute expiration,
2089 unsigned int rd_count,
2090 const struct GNUNET_NAMESTORE_RecordData *rd,
2091 const struct GNUNET_CRYPTO_RsaSignature *signature)
2093 struct ResolverHandle *rh = cls;
2094 struct GNUNET_TIME_Relative remaining_time;
2097 rh->namestore_task = NULL;
2098 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2100 for (i = 0; i < rd_count; i++)
2102 if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2104 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2105 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2107 rh->status |= RSL_PKEY_REVOKED;
2108 rh->proc (rh->proc_cls, rh, 0, NULL);
2113 if ((NULL == name) ||
2114 (0 == remaining_time.rel_value))
2116 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2117 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2120 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2122 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2123 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2124 rh->id, "+.gads", GNUNET_GNS_RECORD_REV);
2126 gns_resolver_lookup_record(rh->authority,
2127 rh->private_local_zone,
2128 GNUNET_GNS_RECORD_REV,
2131 GNUNET_TIME_UNIT_FOREVER_REL,
2133 &background_lookup_result_processor,
2137 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2138 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2141 * We are done with PKEY resolution if name is empty
2142 * else resolve again with new authority
2144 if (strcmp (rh->name, "") == 0)
2145 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2147 resolve_delegation_ns (rh);
2152 * Callback when record data is put into namestore
2154 * @param cls the closure
2155 * @param success GNUNET_OK on success
2156 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2159 on_namestore_delegation_put_result(void *cls,
2163 struct NamestoreBGTask *nbg = cls;
2165 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2168 if (GNUNET_NO == success)
2170 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2171 "GNS_NS: records already in namestore\n");
2174 else if (GNUNET_YES == success)
2176 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2177 "GNS_NS: records successfully put in namestore\n");
2181 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2182 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2186 * Function called when we get a result from the dht
2187 * for our query. Recursively tries to resolve authorities
2190 * @param cls the request handle
2191 * @param exp lifetime
2192 * @param key the key the record was stored under
2193 * @param get_path get path
2194 * @param get_path_length get path length
2195 * @param put_path put path
2196 * @param put_path_length put path length
2197 * @param type the block type
2198 * @param size the size of the record
2199 * @param data the record data
2202 process_delegation_result_dht(void* cls,
2203 struct GNUNET_TIME_Absolute exp,
2204 const struct GNUNET_HashCode * key,
2205 const struct GNUNET_PeerIdentity *get_path,
2206 unsigned int get_path_length,
2207 const struct GNUNET_PeerIdentity *put_path,
2208 unsigned int put_path_length,
2209 enum GNUNET_BLOCK_Type type,
2210 size_t size, const void *data)
2212 struct ResolverHandle *rh = cls;
2213 struct GNSNameRecordBlock *nrb;
2214 uint32_t num_records;
2216 char* rd_data = (char*) data;
2219 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
2220 struct GNUNET_HashCode zone_hash_double, name_hash_double;
2222 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2223 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2228 nrb = (struct GNSNameRecordBlock*)data;
2230 /* stop dht lookup and timeout task */
2231 GNUNET_DHT_get_stop (rh->get_handle);
2232 rh->get_handle = NULL;
2233 if (rh->dht_heap_node != NULL)
2235 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2236 rh->dht_heap_node = NULL;
2239 num_records = ntohl(nrb->rd_count);
2240 name = (char*)&nrb[1];
2242 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2243 struct NamestoreBGTask *ns_heap_root;
2244 struct NamestoreBGTask *namestore_bg_task;
2246 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2247 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2248 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2253 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2254 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2259 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2260 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2261 rh->id, name, rh->authority_name);
2262 for (i=0; i<num_records; i++)
2265 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2266 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2267 rh->id, name, rh->authority_name);
2268 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2269 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2270 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2271 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2272 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2273 rh->id, rd[i].data_size);
2274 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2275 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2276 rh->id, rd[i].flags);
2278 if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2279 (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2280 (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2283 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2285 if (0 == strcmp(rh->name, ""))
2286 strcpy(rh->name, rh->authority_name);
2288 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2289 rh->name, rh->authority_name); //FIXME ret
2294 if ((0 == strcmp(name, rh->authority_name)) &&
2295 (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2297 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2298 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2301 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2302 struct AuthorityChain *auth =
2303 GNUNET_malloc(sizeof(struct AuthorityChain));
2304 auth->zone = rh->authority;
2305 memset(auth->name, 0, strlen(rh->authority_name)+1);
2306 strcpy(auth->name, rh->authority_name);
2307 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2308 rh->authority_chain_tail,
2311 if (NULL != rh->rd.data)
2312 GNUNET_free ((void*)rh->rd.data);
2314 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2315 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2316 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2319 /** try to import pkey if private key available */
2320 //if (rh->priv_key && is_canonical (rh->name))
2321 // process_discovered_authority(name, auth->zone,
2322 // rh->authority_chain_tail->zone,
2329 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2330 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2331 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2332 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2334 /* Save to namestore
2335 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2338 if (max_allowed_ns_tasks <=
2339 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2341 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2342 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2344 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2345 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background ns task\n",
2348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2349 "GNS_PHASE_DELEGATE_DHT-%llu: Caching record for %s\n",
2351 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2353 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2355 GNUNET_TIME_absolute_get().abs_value);
2356 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2363 &on_namestore_delegation_put_result, //cont
2364 namestore_bg_task); //cls
2368 if (0 != rh->answered)
2373 * FIXME in this case. should we ask namestore again?
2375 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2376 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2377 rh->id, rh->authority_name, rh->name);
2379 if (0 == strcmp(rh->name, ""))
2381 /* Start shortening */
2382 if ((NULL != rh->priv_key) &&
2383 (GNUNET_YES == is_canonical (rh->name)))
2385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2386 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2388 start_shorten (rh->authority_chain_head,
2393 rh->proc = &handle_delegation_ns;
2396 /* Check for key revocation and delegate */
2397 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2400 GNUNET_GNS_RECORD_REV,
2401 &process_pkey_revocation_result_ns,
2408 * No pkey but name exists
2411 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2412 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2413 rh->id, rh->authority_name, rh->name);
2414 if (0 == strcmp(rh->name, ""))
2415 strcpy(rh->name, rh->authority_name);
2417 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2418 rh->name, rh->authority_name); //FIXME ret
2420 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2421 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2422 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2423 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2425 rh->proc(rh->proc_cls, rh, 0, NULL);
2428 //FIXME maybe define somewhere else?
2429 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2430 +(MAX_DNS_NAME_LENGTH*2)
2431 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2432 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2436 expand_plus(char* dest, char* src, char* repl)
2439 unsigned int s_len = strlen(src)+1;
2441 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2442 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2443 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2446 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2447 "GNS_POSTPROCESS: %s too short\n", src);
2449 /* no postprocessing */
2450 memcpy(dest, src, s_len+1);
2454 if (0 == strcmp(src+s_len-3, ".+"))
2456 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2457 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2458 memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2462 pos += strlen(repl);
2463 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2464 "GNS_POSTPROCESS: Expanded to %s\n", dest);
2468 memcpy(dest, src, s_len+1);
2476 finish_lookup (struct ResolverHandle *rh,
2477 struct RecordLookupHandle* rlh,
2478 unsigned int rd_count,
2479 const struct GNUNET_NAMESTORE_RecordData *rd)
2482 char new_rr_data[MAX_DNS_NAME_LENGTH];
2483 char new_mx_data[MAX_MX_LENGTH];
2484 char new_soa_data[MAX_SOA_LENGTH];
2485 char new_srv_data[MAX_SRV_LENGTH];
2486 struct srv_data *old_srv;
2487 struct srv_data *new_srv;
2488 struct soa_data *old_soa;
2489 struct soa_data *new_soa;
2490 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2493 unsigned int offset;
2495 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
2497 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2498 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2501 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2504 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2506 for (i = 0; i < rd_count; i++)
2509 if ((GNUNET_GNS_RECORD_NS != rd[i].record_type) &&
2510 (GNUNET_GNS_RECORD_PTR != rd[i].record_type) &&
2511 (GNUNET_GNS_RECORD_CNAME != rd[i].record_type) &&
2512 (GNUNET_GNS_RECORD_MX != rd[i].record_type) &&
2513 (GNUNET_GNS_RECORD_SOA != rd[i].record_type) &&
2514 (GNUNET_GNS_RECORD_SRV != rd[i].record_type))
2516 p_rd[i].data = rd[i].data;
2521 * for all those records we 'should'
2522 * also try to resolve the A/AAAA records (RFC1035)
2523 * This is a feature and not important
2526 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2527 "GNS_POSTPROCESS: Postprocessing\n");
2528 if (0 == strcmp(rh->name, "+"))
2529 repl_string = rlh->name;
2531 repl_string = rlh->name+strlen(rh->name)+1;
2534 if (GNUNET_GNS_RECORD_MX == rd[i].record_type)
2536 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2537 offset = sizeof(uint16_t);
2538 pos = new_mx_data+offset;
2539 expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2541 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2542 p_rd[i].data = new_mx_data;
2543 p_rd[i].data_size = offset;
2545 else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2548 * Prio, weight and port
2550 new_srv = (struct srv_data*)new_srv_data;
2551 old_srv = (struct srv_data*)rd[i].data;
2552 new_srv->prio = old_srv->prio;
2553 new_srv->weight = old_srv->weight;
2554 new_srv->port = old_srv->port;
2555 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2557 p_rd[i].data = new_srv_data;
2558 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2560 else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2562 /* expand mname and rname */
2563 old_soa = (struct soa_data*)rd[i].data;
2564 new_soa = (struct soa_data*)new_soa_data;
2565 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2566 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2567 offset = strlen ((char*)&new_soa[1]) + 1;
2568 expand_plus((char*)&new_soa[1] + offset,
2569 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2571 p_rd[i].data_size = sizeof (struct soa_data)
2573 + strlen ((char*)&new_soa[1] + offset);
2574 p_rd[i].data = new_soa_data;
2579 expand_plus(pos, (char*)rd[i].data, repl_string);
2580 p_rd[i].data_size = strlen(new_rr_data)+1;
2581 p_rd[i].data = new_rr_data;
2586 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2588 free_resolver_handle (rh);
2592 * Process DHT lookup result for record.
2594 * @param cls the closure
2595 * @param rh resolver handle
2596 * @param rd_count number of results
2597 * @param rd record data
2600 handle_record_dht(void* cls, struct ResolverHandle *rh,
2601 unsigned int rd_count,
2602 const struct GNUNET_NAMESTORE_RecordData *rd)
2604 struct RecordLookupHandle* rlh = cls;
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2609 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2611 /* give up, cannot resolve */
2612 finish_lookup (rh, rlh, 0, NULL);
2615 /* results found yay */
2616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2617 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2618 finish_lookup (rh, rlh, rd_count, rd);
2625 * Process namestore lookup result for record.
2627 * @param cls the closure
2628 * @param rh resolver handle
2629 * @param rd_count number of results
2630 * @param rd record data
2633 handle_record_ns (void* cls, struct ResolverHandle *rh,
2634 unsigned int rd_count,
2635 const struct GNUNET_NAMESTORE_RecordData *rd)
2637 struct RecordLookupHandle* rlh = cls;
2638 int check_dht = GNUNET_YES;
2642 /* results found yay */
2643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2644 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2645 finish_lookup (rh, rlh, rd_count, rd);
2649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2650 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2654 * There are 5 conditions that have to met for us to consult the DHT:
2655 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2656 * 2. No entry in the NS existed AND
2657 * 3. The zone queried is not the local resolver's zone AND
2658 * 4. The name that was looked up is '+'
2659 * because if it was any other canonical name we either already queried
2660 * the DHT for the authority in the authority lookup phase (and thus
2661 * would already have an entry in the NS for the record)
2662 * 5. We are not in cache only mode
2664 if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2665 (0 == (rh->status & RSL_RECORD_EXISTS)) )
2668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2669 "GNS_PHASE_REC-%llu: Not expired and exists!\n",
2671 check_dht = GNUNET_NO;
2674 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2675 &rh->private_local_zone))
2678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2679 "GNS_PHASE_REC-%llu: Our zone!\n",
2681 check_dht = GNUNET_NO;
2684 if ((0 != strcmp (rh->name, "+")) && (GNUNET_YES == is_srv (rh->name)))
2685 check_dht = GNUNET_NO;
2687 if (GNUNET_YES == rh->only_cached)
2688 check_dht = GNUNET_NO;
2690 if (GNUNET_YES == check_dht)
2692 rh->proc = &handle_record_dht;
2693 resolve_record_dht(rh);
2696 /* give up, cannot resolve */
2697 finish_lookup (rh, rlh, 0, NULL);
2702 * Move one level up in the domain hierarchy and return the
2703 * passed top level domain.
2705 * @param name the domain
2706 * @param dest the destination where the tld will be put
2709 pop_tld(char* name, char* dest)
2713 if (GNUNET_YES == is_canonical (name))
2720 for (len = strlen(name); 0 < len; len--)
2722 if (*(name+len) == '.')
2732 strcpy(dest, (name+len+1));
2736 * Checks if name is in tld
2738 * @param name the name to check
2739 * @param tld the TLD to check for
2740 * @return GNUNET_YES or GNUNET_NO
2743 is_tld(const char* name, const char* tld)
2747 if (strlen(name) <= strlen(tld))
2750 offset = strlen(name)-strlen(tld);
2751 if (0 != strcmp(name+offset, tld))
2753 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2754 "%s is not in .%s TLD\n", name, tld);
2761 * DHT resolution for delegation finished. Processing result.
2763 * @param cls the closure
2764 * @param rh resolver handle
2765 * @param rd_count number of results (always 0)
2766 * @param rd record data (always NULL)
2769 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2770 unsigned int rd_count,
2771 const struct GNUNET_NAMESTORE_RecordData *rd)
2773 struct RecordLookupHandle* rlh;
2777 if (0 == strcmp(rh->name, ""))
2779 if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2781 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2782 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2784 finish_lookup(rh, rlh, rd_count, rd);
2787 /* We resolved full name for delegation. resolving record */
2788 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2789 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2791 strcpy(rh->name, "+\0");
2792 rh->proc = &handle_record_ns;
2793 resolve_record_ns(rh);
2798 * we still have some left
2800 if (GNUNET_YES == is_canonical (rh->name))
2802 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2803 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2806 rh->proc = &handle_record_ns;
2807 resolve_record_ns(rh);
2810 /* give up, cannot resolve */
2811 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2812 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2814 finish_lookup(rh, rlh, 0, NULL);
2819 * Start DHT lookup for a name -> PKEY (compare NS) record in
2820 * rh->authority's zone
2822 * @param rh the pending gns query
2825 resolve_delegation_dht(struct ResolverHandle *rh)
2828 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2829 struct GNUNET_HashCode name_hash_double;
2830 struct GNUNET_HashCode zone_hash_double;
2831 struct GNUNET_HashCode lookup_key;
2832 struct ResolverHandle *rh_heap_root;
2834 pop_tld(rh->name, rh->authority_name);
2836 //FIXME handle return values here
2837 GNUNET_CRYPTO_short_hash(rh->authority_name,
2838 strlen(rh->authority_name),
2840 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2841 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2842 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2844 rh->dht_heap_node = NULL;
2846 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2848 rh->timeout_cont = &dht_authority_lookup_timeout;
2849 rh->timeout_cont_cls = rh;
2853 if (max_allowed_background_queries <=
2854 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2856 /* terminate oldest lookup */
2857 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2858 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2859 rh_heap_root->get_handle = NULL;
2860 rh_heap_root->dht_heap_node = NULL;
2862 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2863 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2864 rh->id, rh_heap_root->authority_name);
2866 rh_heap_root->proc(rh_heap_root->proc_cls,
2871 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2873 GNUNET_TIME_absolute_get().abs_value);
2876 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2877 GNUNET_assert(rh->get_handle == NULL);
2878 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2879 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2881 DHT_GNS_REPLICATION_LEVEL,
2882 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2885 &process_delegation_result_dht,
2891 * Namestore resolution for delegation finished. Processing result.
2893 * @param cls the closure
2894 * @param rh resolver handle
2895 * @param rd_count number of results (always 0)
2896 * @param rd record data (always NULL)
2899 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2900 unsigned int rd_count,
2901 const struct GNUNET_NAMESTORE_RecordData *rd)
2903 struct RecordLookupHandle* rlh;
2905 int check_dht = GNUNET_YES;
2908 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2909 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2910 rh->id, rh->status);
2912 if (rh->status & RSL_PKEY_REVOKED)
2914 finish_lookup (rh, rlh, 0, NULL);
2918 if (0 == strcmp(rh->name, ""))
2921 /* We resolved full name for delegation. resolving record */
2922 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2923 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2925 if (rh->status & RSL_CNAME_FOUND)
2927 if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2929 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2930 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2932 strcpy (rh->name, rh->authority_name);
2933 finish_lookup (rh, rlh, rd_count, rd);
2938 if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2940 s_len = strlen (rd->data) - 2;
2941 memcpy (rh->name, rd->data, s_len);
2942 rh->name[s_len] = '\0';
2943 resolve_delegation_ns (rh);
2946 else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2948 gns_resolver_lookup_record (rh->authority,
2949 rh->private_local_zone,
2958 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2959 free_resolver_handle (rh);
2965 strcpy (rh->dns_name, (char*)rd->data);
2966 resolve_dns_name (rh);
2971 else if (rh->status & RSL_DELEGATE_VPN)
2973 if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2975 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2976 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2978 finish_lookup(rh, rlh, rd_count, rd);
2981 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2982 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2984 GNUNET_assert (NULL != rd);
2985 rh->proc = &handle_record_vpn;
2986 resolve_record_vpn (rh, rd_count, rd);
2989 else if (rh->status & RSL_DELEGATE_NS)
2991 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2993 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2994 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2996 finish_lookup(rh, rlh, rd_count, rd);
3000 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3001 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3003 GNUNET_assert (NULL != rd);
3004 rh->proc = &handle_record_ns;
3005 resolve_record_dns (rh, rd_count, rd);
3008 else if (rh->status & RSL_DELEGATE_PKEY)
3010 if (rh->status & RSL_PKEY_REVOKED)
3012 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3013 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
3015 finish_lookup (rh, rlh, 0, NULL);
3018 else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
3020 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3021 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
3023 finish_lookup(rh, rlh, rd_count, rd);
3027 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3028 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3030 strcpy(rh->name, "+\0");
3031 rh->proc = &handle_record_ns;
3032 resolve_record_ns(rh);
3036 if (rh->status & RSL_DELEGATE_NS)
3038 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
3040 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3041 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3043 finish_lookup(rh, rlh, rd_count, rd);
3047 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3048 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3050 GNUNET_assert (NULL != rd);
3051 rh->proc = &handle_record_ns;
3052 resolve_record_dns (rh, rd_count, rd);
3057 * we still have some left
3058 * check if authority in ns is fresh
3060 * or we are authority
3063 if ((rh->status & RSL_RECORD_EXISTS) &&
3064 !(rh->status & RSL_RECORD_EXPIRED))
3065 check_dht = GNUNET_NO;
3067 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3068 &rh->private_local_zone))
3069 check_dht = GNUNET_NO;
3071 if (GNUNET_YES == rh->only_cached)
3072 check_dht = GNUNET_NO;
3074 if (GNUNET_YES == check_dht)
3077 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3078 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3080 rh->proc = &handle_delegation_dht;
3081 resolve_delegation_dht(rh);
3085 if (GNUNET_NO == is_canonical (rh->name))
3087 /* give up, cannot resolve */
3088 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3089 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3092 finish_lookup(rh, rlh, rd_count, rd);
3095 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3096 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3099 rh->proc = &handle_record_ns;
3100 resolve_record_ns(rh);
3105 * This is a callback function that should give us only PKEY
3106 * records. Used to query the namestore for the authority (PKEY)
3107 * for 'name'. It will recursively try to resolve the
3108 * authority for a given name from the namestore.
3110 * @param cls the pending query
3111 * @param key the key of the zone we did the lookup
3112 * @param expiration expiration date of the record data set in the namestore
3113 * @param name the name for which we need an authority
3114 * @param rd_count the number of records with 'name'
3115 * @param rd the record data
3116 * @param signature the signature of the authority for the record data
3119 process_delegation_result_ns (void* cls,
3120 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3121 struct GNUNET_TIME_Absolute expiration,
3123 unsigned int rd_count,
3124 const struct GNUNET_NAMESTORE_RecordData *rd,
3125 const struct GNUNET_CRYPTO_RsaSignature *signature)
3127 struct ResolverHandle *rh;
3128 struct GNUNET_TIME_Relative remaining_time;
3129 struct GNUNET_CRYPTO_ShortHashCode zone;
3130 char new_name[MAX_DNS_NAME_LENGTH];
3132 struct GNUNET_TIME_Absolute et;
3134 rh = (struct ResolverHandle *)cls;
3135 rh->namestore_task = NULL;
3136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3137 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3140 GNUNET_CRYPTO_short_hash (key,
3141 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3143 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3150 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3152 rh->status |= RSL_RECORD_EXISTS;
3154 if (remaining_time.rel_value == 0)
3156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3157 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3159 rh->status |= RSL_RECORD_EXPIRED;
3164 * No authority found in namestore.
3169 * We did not find an authority in the namestore
3174 * Promote this authority back to a name maybe it is
3177 if (strcmp (rh->name, "") == 0)
3179 /* simply promote back */
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3181 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3182 rh->id, rh->authority_name);
3183 strcpy (rh->name, rh->authority_name);
3187 /* add back to existing name */
3188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3189 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3190 rh->id, rh->authority_name, rh->name);
3191 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3192 rh->name, rh->authority_name);
3193 strcpy (rh->name, new_name);
3194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3195 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3199 rh->proc (rh->proc_cls, rh, 0, NULL);
3204 * We found an authority that may be able to help us
3205 * move on with query
3206 * Note only 1 pkey should have been returned.. anything else would be strange
3208 for (i=0; i < rd_count;i++)
3212 * A CNAME. Like regular DNS this means the is no other record for this
3215 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3217 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3218 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3221 rh->status |= RSL_CNAME_FOUND;
3222 rh->proc (rh->proc_cls, rh, rd_count, rd);
3229 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3231 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3232 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3234 rh->status |= RSL_DELEGATE_VPN;
3235 rh->proc (rh->proc_cls, rh, rd_count, rd);
3241 * FIXME make optional
3243 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3245 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3246 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3248 rh->status |= RSL_DELEGATE_NS;
3249 rh->proc (rh->proc_cls, rh, rd_count, rd);
3253 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3256 rh->status |= RSL_DELEGATE_PKEY;
3258 if ((ignore_pending_records != 0) &&
3259 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3262 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3268 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3269 et.abs_value = rd[i].expiration_time;
3270 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3274 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3276 if (remaining_time.rel_value == 0)
3278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3279 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3281 rh->authority_chain_head->fresh = 0;
3282 rh->proc (rh->proc_cls, rh, 0, NULL);
3290 * Resolve rest of query with new authority
3292 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3293 memcpy (&rh->authority, rd[i].data,
3294 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3295 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3296 auth->zone = rh->authority;
3297 memset (auth->name, 0, strlen (rh->authority_name)+1);
3298 strcpy (auth->name, rh->authority_name);
3299 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3300 rh->authority_chain_tail,
3302 if (NULL != rh->rd.data)
3303 GNUNET_free ((void*)(rh->rd.data));
3305 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3306 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3307 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3309 /* Check for key revocation and delegate */
3310 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3313 GNUNET_GNS_RECORD_REV,
3314 &process_pkey_revocation_result_ns,
3323 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3324 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3326 * If we have found some records for the LAST label
3327 * we return the results. Else null.
3329 if (strcmp (rh->name, "") == 0)
3331 /* Start shortening */
3332 if ((rh->priv_key != NULL) &&
3333 (is_canonical (rh->name) == GNUNET_YES))
3335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3336 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3338 start_shorten (rh->authority_chain_head,
3341 /* simply promote back */
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3343 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3344 rh->id, rh->authority_name);
3345 strcpy (rh->name, rh->authority_name);
3346 rh->proc (rh->proc_cls, rh, rd_count, rd);
3350 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
3351 "%s.%s", rh->name, rh->authority_name);
3352 strcpy (rh->name, new_name);
3353 rh->proc (rh->proc_cls, rh, 0, NULL);
3359 * Resolve the delegation chain for the request in our namestore
3361 * @param rh the resolver handle
3364 resolve_delegation_ns (struct ResolverHandle *rh)
3366 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3367 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3369 pop_tld(rh->name, rh->authority_name);
3370 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3373 GNUNET_GNS_RECORD_ANY,
3374 &process_delegation_result_ns,
3381 * Lookup of a record in a specific zone
3382 * calls lookup result processor on result
3384 * @param zone the root zone
3385 * @param pzone the private local zone
3386 * @param record_type the record type to look up
3387 * @param name the name to look up
3388 * @param key a private key for use with PSEU import (can be NULL)
3389 * @param timeout timeout for resolution
3390 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3391 * @param proc the processor to call on result
3392 * @param cls the closure to pass to proc
3395 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3396 struct GNUNET_CRYPTO_ShortHashCode pzone,
3397 uint32_t record_type,
3399 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3400 struct GNUNET_TIME_Relative timeout,
3402 RecordLookupProcessor proc,
3405 struct ResolverHandle *rh;
3406 struct RecordLookupHandle* rlh;
3407 char string_hash[MAX_DNS_LABEL_LENGTH];
3408 char nzkey[MAX_DNS_LABEL_LENGTH];
3409 char* nzkey_ptr = nzkey;
3411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3412 "Starting resolution for %s (type=%d)!\n",
3416 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3417 (strcmp(GNUNET_GNS_TLD, name) != 0))
3419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3420 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3425 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3426 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3428 memset (rh, 0, sizeof (struct ResolverHandle));
3429 rh->authority = zone;
3433 rh->timeout = timeout;
3434 rh->get_handle = NULL;
3435 rh->private_local_zone = pzone;
3436 rh->only_cached = only_cached;
3437 rh->namestore_task = NULL;
3440 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3445 "No shorten key for resolution\n");
3448 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3451 * Set timeout for authority lookup phase to 1/2
3453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3454 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3455 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3456 GNUNET_TIME_relative_divide(timeout, 2),
3457 &handle_lookup_timeout,
3459 rh->timeout_cont = &dht_authority_lookup_timeout;
3460 rh->timeout_cont_cls = rh;
3464 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3465 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3468 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3471 * Only 'gnunet' given
3473 strcpy(rh->name, "\0");
3477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3478 "Checking for TLD...\n");
3479 if (is_zkey_tld(name) == GNUNET_YES)
3481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3484 * This is a zkey tld
3485 * build hash and use as initial authority
3488 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3489 memcpy(rh->name, name,
3490 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3491 pop_tld(rh->name, string_hash);
3493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3494 "ZKEY is %s!\n", string_hash);
3496 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3498 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3502 "Cannot convert ZKEY `%s' to hash!\n", string_hash);
3504 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3505 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3506 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3509 proc (cls, 0, NULL);
3514 else if (is_gnunet_tld (name) == GNUNET_YES)
3516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3519 * Presumably GNUNET tld
3521 memset (rh->name, 0,
3522 strlen(name)-strlen(GNUNET_GNS_TLD));
3523 memcpy (rh->name, name,
3524 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3528 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3529 "Cannot handle this TLD %s\n", string_hash);
3531 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3532 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3533 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3536 proc (cls, 0, NULL);
3542 * Initialize authority chain
3544 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3545 rh->authority_chain_head->prev = NULL;
3546 rh->authority_chain_head->next = NULL;
3547 rh->authority_chain_tail = rh->authority_chain_head;
3548 rh->authority_chain_head->zone = rh->authority;
3549 strcpy (rh->authority_chain_head->name, "");
3552 * Copy original query into lookup handle
3554 rlh->record_type = record_type;
3555 memset(rlh->name, 0, strlen(name) + 1);
3556 strcpy(rlh->name, name);
3558 rlh->proc_cls = cls;
3560 rh->proc = &handle_delegation_ns;
3561 resolve_delegation_ns(rh);
3564 /******** END Record Resolver ***********/
3567 finish_shorten (struct ResolverHandle *rh,
3568 struct NameShortenHandle *nsh)
3570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3571 "Sending %s as shorten result\n", nsh->result);
3572 nsh->proc (nsh->proc_cls, nsh->result);
3573 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3575 free_resolver_handle (rh);
3580 * Callback calles by namestore for a zone to name
3583 * @param cls the closure
3584 * @param zone_key the zone we queried
3585 * @param expire the expiration time of the name
3586 * @param name the name found or NULL
3587 * @param rd_len number of records for the name
3588 * @param rd the record data (PKEY) for the name
3589 * @param signature the signature for the record data
3592 process_zone_to_name_shorten_root (void *cls,
3593 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3594 struct GNUNET_TIME_Absolute expire,
3596 unsigned int rd_len,
3597 const struct GNUNET_NAMESTORE_RecordData *rd,
3598 const struct GNUNET_CRYPTO_RsaSignature *signature);
3602 * Callback called by namestore for a zone to name
3605 * @param cls the closure
3606 * @param zone_key the zone we queried
3607 * @param expire the expiration time of the name
3608 * @param name the name found or NULL
3609 * @param rd_len number of records for the name
3610 * @param rd the record data (PKEY) for the name
3611 * @param signature the signature for the record data
3614 process_zone_to_name_shorten_shorten (void *cls,
3615 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3616 struct GNUNET_TIME_Absolute expire,
3618 unsigned int rd_len,
3619 const struct GNUNET_NAMESTORE_RecordData *rd,
3620 const struct GNUNET_CRYPTO_RsaSignature *signature)
3622 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3623 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3624 struct AuthorityChain *next_authority;
3626 char result[MAX_DNS_NAME_LENGTH];
3627 char tmp_name[MAX_DNS_NAME_LENGTH];
3630 rh->namestore_task = NULL;
3631 /* we found a match in our own root zone */
3634 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3635 memset(result, 0, answer_len);
3637 if (strlen(rh->name) > 0)
3639 sprintf (result, "%s.%s.%s.%s",
3641 nsh->shorten_zone_name,
3646 sprintf (result, "%s.%s.%s", name,
3647 nsh->shorten_zone_name,
3651 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3652 "Found shorten result %s\n", result);
3653 if (strlen (nsh->result) > strlen (result))
3654 strcpy (nsh->result, result);
3656 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3657 nsh->shorten_zone) == 0)
3660 * This is our zone append .gads unless name is empty
3661 * (it shouldn't be, usually FIXME what happens if we
3662 * shorten to our zone to a "" record??)
3665 sprintf (result, "%s.%s.%s",
3667 nsh->shorten_zone_name,
3669 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3670 "Our zone: Found %s as shorten result\n", result);
3672 if (strlen (nsh->result) > strlen (result))
3673 strcpy (nsh->result, result);
3674 //nsh->proc(nsh->proc_cls, result);
3676 //free_resolver_handle(rh);
3683 * continue with next authority if exists
3685 if ((rh->authority_chain_head->next == NULL))
3687 finish_shorten (rh, nsh);
3690 next_authority = rh->authority_chain_head;
3692 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3693 "%s.%s", rh->name, next_authority->name);
3695 strcpy(rh->name, tmp_name);
3696 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3697 "No PSEU found for authority %s. Promoting back: %s\n",
3698 next_authority->name, rh->name);
3700 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3701 rh->authority_chain_tail,
3704 GNUNET_free (next_authority);
3706 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3707 &rh->authority_chain_tail->zone,
3708 &rh->authority_chain_head->zone,
3709 &process_zone_to_name_shorten_root,
3714 * Callback calles by namestore for a zone to name
3717 * @param cls the closure
3718 * @param zone_key the zone we queried
3719 * @param expire the expiration time of the name
3720 * @param name the name found or NULL
3721 * @param rd_len number of records for the name
3722 * @param rd the record data (PKEY) for the name
3723 * @param signature the signature for the record data
3726 process_zone_to_name_shorten_private (void *cls,
3727 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3728 struct GNUNET_TIME_Absolute expire,
3730 unsigned int rd_len,
3731 const struct GNUNET_NAMESTORE_RecordData *rd,
3732 const struct GNUNET_CRYPTO_RsaSignature *signature)
3734 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3735 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3736 struct AuthorityChain *next_authority;
3738 char result[MAX_DNS_NAME_LENGTH];
3739 char tmp_name[MAX_DNS_NAME_LENGTH];
3742 rh->namestore_task = NULL;
3743 /* we found a match in our own root zone */
3746 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3747 memset(result, 0, answer_len);
3749 if (strlen(rh->name) > 0)
3751 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3755 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3758 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3759 "Found shorten result %s\n", result);
3760 if (strlen (nsh->result) > strlen (result))
3761 strcpy (nsh->result, result);
3763 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3764 nsh->private_zone) == 0)
3767 * This is our zone append .gads unless name is empty
3768 * (it shouldn't be, usually FIXME what happens if we
3769 * shorten to our zone to a "" record??)
3772 sprintf (result, "%s.%s.%s",
3773 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3774 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3775 "Our private zone: Found %s as shorten result %s\n", result);
3776 if (strlen (nsh->result) > strlen (result))
3777 strcpy (nsh->result, result);
3780 if (0 != strcmp (nsh->shorten_zone_name, ""))
3782 /* backtrack authorities for names in priv zone */
3783 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3785 &rh->authority_chain_head->zone,
3786 &process_zone_to_name_shorten_shorten,
3793 * continue with next authority if exists
3795 if ((rh->authority_chain_head->next == NULL))
3797 finish_shorten (rh, nsh);
3800 next_authority = rh->authority_chain_head;
3802 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3803 "%s.%s", rh->name, next_authority->name);
3805 strcpy(rh->name, tmp_name);
3806 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3807 "No PSEU found for authority %s. Promoting back: %s\n",
3808 next_authority->name, rh->name);
3810 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3811 rh->authority_chain_tail,
3814 GNUNET_free (next_authority);
3816 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3817 &rh->authority_chain_tail->zone,
3818 &rh->authority_chain_head->zone,
3819 &process_zone_to_name_shorten_root,
3825 * Callback calles by namestore for a zone to name
3828 * @param cls the closure
3829 * @param zone_key the zone we queried
3830 * @param expire the expiration time of the name
3831 * @param name the name found or NULL
3832 * @param rd_len number of records for the name
3833 * @param rd the record data (PKEY) for the name
3834 * @param signature the signature for the record data
3837 process_zone_to_name_shorten_root (void *cls,
3838 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3839 struct GNUNET_TIME_Absolute expire,
3841 unsigned int rd_len,
3842 const struct GNUNET_NAMESTORE_RecordData *rd,
3843 const struct GNUNET_CRYPTO_RsaSignature *signature)
3845 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3846 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3847 struct AuthorityChain *next_authority;
3849 char result[MAX_DNS_NAME_LENGTH];
3850 char tmp_name[MAX_DNS_NAME_LENGTH];
3853 rh->namestore_task = NULL;
3854 /* we found a match in our own root zone */
3857 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3858 memset(result, 0, answer_len);
3860 if (strlen(rh->name) > 0)
3862 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3866 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3869 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3870 "Found shorten result %s\n", result);
3871 if (strlen (nsh->result) > strlen (result))
3872 strcpy (nsh->result, result);
3874 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3875 nsh->root_zone) == 0)
3878 * This is our zone append .gads unless name is empty
3879 * (it shouldn't be, usually FIXME what happens if we
3880 * shorten to our zone to a "" record??)
3883 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3884 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3885 "Our zone: Found %s as shorten result\n", result);
3886 if (strlen (nsh->result) > strlen (result))
3887 strcpy (nsh->result, result);
3890 if (NULL != nsh->private_zone)
3892 /* backtrack authorities for names in priv zone */
3893 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3895 &rh->authority_chain_head->zone,
3896 &process_zone_to_name_shorten_private,
3899 else if (NULL != nsh->shorten_zone)
3901 /* backtrack authorities for names in shorten zone */
3902 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3904 &rh->authority_chain_head->zone,
3905 &process_zone_to_name_shorten_shorten,
3912 * continue with next authority if exists
3914 if ((rh->authority_chain_head->next == NULL))
3916 finish_shorten (rh, nsh);
3919 next_authority = rh->authority_chain_head;
3921 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3922 "%s.%s", rh->name, next_authority->name);
3924 strcpy(rh->name, tmp_name);
3925 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3926 "No PSEU found for authority %s. Promoting back: %s\n",
3927 next_authority->name, rh->name);
3929 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3930 rh->authority_chain_tail,
3933 GNUNET_free (next_authority);
3935 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3936 &rh->authority_chain_tail->zone,
3937 &rh->authority_chain_head->zone,
3938 &process_zone_to_name_shorten_root,
3945 * Process result from namestore delegation lookup
3946 * for shorten operation
3948 * @param cls the client shorten handle
3949 * @param rh the resolver handle
3950 * @param rd_count number of results (0)
3951 * @param rd data (NULL)
3954 handle_delegation_ns_shorten (void* cls,
3955 struct ResolverHandle *rh,
3957 const struct GNUNET_NAMESTORE_RecordData *rd)
3959 struct NameShortenHandle *nsh;
3960 char result[MAX_DNS_NAME_LENGTH];
3962 nsh = (struct NameShortenHandle *)cls;
3963 rh->namestore_task = NULL;
3965 * At this point rh->name contains the part of the name
3966 * that we do not have a PKEY in our namestore to resolve.
3967 * The authority chain in the resolver handle is now
3968 * useful to backtrack if needed
3971 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3972 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3973 memset(result, 0, sizeof (result));
3975 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3979 * This is our zone append .gads unless name is empty
3980 * (it shouldn't be, usually FIXME what happens if we
3981 * shorten to our zone to a "" record??)
3984 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3985 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3986 "Our zone: Found %s as shorten result\n", result);
3988 if (strlen (nsh->result) > strlen (result))
3989 strcpy (nsh->result, result);
3992 else if (NULL != nsh->private_zone)
3995 * This is our zone append .gads unless name is empty
3996 * (it shouldn't be, usually FIXME what happens if we
3997 * shorten to our zone to a "" record??)
3999 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4000 nsh->private_zone) == 0)
4003 sprintf (result, "%s.%s.%s",
4004 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4005 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4006 "Our zone: Found %s as shorten result in private zone %s\n",
4009 if (strlen (nsh->result) > strlen (result))
4010 strcpy (nsh->result, result);
4013 else if (NULL != nsh->shorten_zone)
4016 * This is our zone append .gads unless name is empty
4017 * (it shouldn't be, usually FIXME what happens if we
4018 * shorten to our zone to a "" record??)
4020 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4021 nsh->shorten_zone) == 0)
4023 sprintf (result, "%s.%s.%s",
4024 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4025 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4026 "Our zone: Found %s as shorten result in shorten zone\n",
4029 if (strlen (nsh->result) > strlen (result))
4030 strcpy (nsh->result, result);
4035 /* backtrack authorities for names */
4036 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4038 &rh->authority_chain_head->zone,
4039 &process_zone_to_name_shorten_root,
4046 * Callback calles by namestore for a zone to name
4049 * @param cls the closure
4050 * @param zone_key the zone we queried
4051 * @param expire the expiration time of the name
4052 * @param name the name found or NULL
4053 * @param rd_len number of records for the name
4054 * @param rd the record data (PKEY) for the name
4055 * @param signature the signature for the record data
4058 process_zone_to_name_zkey(void *cls,
4059 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4060 struct GNUNET_TIME_Absolute expire,
4062 unsigned int rd_len,
4063 const struct GNUNET_NAMESTORE_RecordData *rd,
4064 const struct GNUNET_CRYPTO_RsaSignature *signature)
4066 struct ResolverHandle *rh = cls;
4067 struct NameShortenHandle *nsh = rh->proc_cls;
4068 char new_name[MAX_DNS_NAME_LENGTH];
4070 rh->namestore_task = NULL;
4072 /* zkey not in our zone */
4076 * In this case we have not given this PKEY a name (yet)
4077 * It is either just not in our zone or not even cached
4078 * Since we do not know at this point we will not try to shorten
4079 * because PKEY import will happen if the user follows the zkey
4082 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4083 "No name found for zkey %s returning verbatim!\n", nsh->result);
4084 /*if (strcmp(rh->name, "") != 0)
4085 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4086 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4088 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4089 enc, GNUNET_GNS_TLD_ZKEY);
4091 strcpy (nsh->result, new_name);*/
4093 finish_shorten (rh, nsh);
4097 if (strcmp(rh->name, "") != 0)
4098 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4101 strcpy(new_name, name);
4103 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4104 "Continue shorten for %s!\n", new_name);
4106 strcpy(rh->name, new_name);
4108 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4109 rh->authority_chain_tail = rh->authority_chain_head;
4110 rh->authority_chain_head->zone = rh->authority;
4113 /* Start delegation resolution in our namestore */
4114 resolve_delegation_ns (rh);
4119 * Shorten api from resolver
4121 * @param zone the root zone to use
4122 * @param pzone the private zone to use
4123 * @param szone the shorten zone to use
4124 * @param name the name to shorten
4125 * @param private_zone_name name of the private zone
4126 * @param shorten_zone_name name of the shorten zone
4127 * @param proc the processor to call with result
4128 * @param proc_cls closure to pass to proc
4131 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4132 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4133 struct GNUNET_CRYPTO_ShortHashCode *szone,
4135 const char* private_zone_name,
4136 const char* shorten_zone_name,
4137 ShortenResultProcessor proc,
4140 struct ResolverHandle *rh;
4141 struct NameShortenHandle *nsh;
4142 char string_hash[MAX_DNS_LABEL_LENGTH];
4143 struct GNUNET_CRYPTO_ShortHashCode zkey;
4144 char nzkey[MAX_DNS_LABEL_LENGTH];
4145 char* nzkey_ptr = nzkey;
4148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4149 "Starting shorten for %s!\n", name);
4151 if (is_canonical ((char*)name) == GNUNET_YES)
4153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4154 "%s is canonical. Returning verbatim\n", name);
4155 proc (proc_cls, name);
4159 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4162 nsh->proc_cls = proc_cls;
4163 nsh->root_zone = zone;
4164 nsh->private_zone = pzone;
4165 nsh->shorten_zone = szone;
4166 strcpy (nsh->private_zone_name, private_zone_name);
4167 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4168 strcpy (nsh->result, name);
4171 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4172 rh->authority = *zone;
4174 rh->priv_key = NULL;
4175 rh->namestore_task = NULL;
4176 rh->proc = &handle_delegation_ns_shorten;
4179 rh->private_local_zone = *zone;
4181 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4184 "Checking for TLD...\n");
4185 if (is_zkey_tld (name) == GNUNET_YES)
4187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4190 * This is a zkey tld
4191 * build hash and use as initial authority
4194 memset (rh->name, 0,
4195 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4196 memcpy (rh->name, name,
4197 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4198 pop_tld (rh->name, string_hash);
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4201 "ZKEY is %s!\n", string_hash);
4203 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4205 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4209 "Cannot convert ZKEY %s to hash!\n", nzkey);
4212 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4213 proc (proc_cls, name);
4217 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4220 &process_zone_to_name_zkey,
4225 else if (is_gnunet_tld (name) == GNUNET_YES)
4227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4230 * Presumably GNUNET tld
4232 memset (rh->name, 0,
4233 strlen (name)-strlen (GNUNET_GNS_TLD));
4234 memcpy (rh->name, name,
4235 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4242 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4243 proc (proc_cls, name);
4247 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4248 rh->authority_chain_tail = rh->authority_chain_head;
4249 rh->authority_chain_head->zone = *zone;
4252 /* Start delegation resolution in our namestore */
4253 resolve_delegation_ns (rh);
4256 /*********** END NAME SHORTEN ********************/
4259 * Conclude get authority lookup
4261 * @param rh resolver handle
4262 * @param nah get authority lookup handle
4265 finish_get_auth (struct ResolverHandle *rh,
4266 struct GetNameAuthorityHandle *nah)
4268 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4269 "Got authority result %s\n", nah->result);
4271 nah->proc (nah->proc_cls, nah->result);
4272 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4274 free_resolver_handle (rh);
4278 * Process result from namestore delegation lookup
4279 * for get authority operation
4281 * @param cls the client get auth handle
4282 * @param rh the resolver handle
4283 * @param rd_count number of results (0)
4284 * @param rd data (NULL)
4287 handle_delegation_result_ns_get_auth(void* cls,
4288 struct ResolverHandle *rh,
4290 const struct GNUNET_NAMESTORE_RecordData *rd)
4292 struct GetNameAuthorityHandle* nah;
4295 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4298 * At this point rh->name contains the part of the name
4299 * that we do not have a PKEY in our namestore to resolve.
4300 * The authority chain in the resolver handle is now
4301 * useful to backtrack if needed
4304 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4305 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4307 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4308 "Building response!\n");
4309 if (is_canonical (rh->name) == GNUNET_YES)
4312 * We successfully resolved the authority in the ns
4313 * FIXME for our purposes this is fine
4314 * but maybe we want to have an api that also looks
4315 * into the dht (i.e. option in message)
4317 if (strlen(rh->name) > strlen(nah->name))
4319 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4320 "Record name longer than original lookup name... odd!\n");
4324 answer_len = strlen(nah->name) - strlen(rh->name)
4325 + strlen(GNUNET_GNS_TLD) + 1;
4326 memset(nah->result, 0, answer_len);
4327 if (0 != strcmp (rh->name, ""))
4328 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4330 strcpy(nah->result, nah->name);
4332 finish_get_auth (rh, nah);
4336 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4337 "Unable to resolve authority for remaining %s!\n", rh->name);
4338 strcpy(nah->result, "");
4339 finish_get_auth (rh, nah);
4347 * Tries to resolve the authority for name
4350 * @param zone the root zone to look up for
4351 * @param pzone the private local zone
4352 * @param name the name to lookup up
4353 * @param proc the processor to call when finished
4354 * @param proc_cls the closure to pass to the processor
4357 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4358 struct GNUNET_CRYPTO_ShortHashCode pzone,
4360 GetAuthorityResultProcessor proc,
4363 struct ResolverHandle *rh;
4364 struct GetNameAuthorityHandle *nah;
4366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4367 "Starting authority resolution for %s!\n", name);
4369 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4370 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4371 rh->authority = zone;
4373 rh->private_local_zone = pzone;
4374 rh->namestore_task = NULL;
4376 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4378 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4380 strcpy(rh->name, "\0");
4385 strlen(name)-strlen(GNUNET_GNS_TLD));
4386 memcpy(rh->name, name,
4387 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4390 memset(nah->name, 0,
4392 strcpy(nah->name, name);
4394 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4395 rh->authority_chain_tail = rh->authority_chain_head;
4396 rh->authority_chain_head->zone = zone;
4397 rh->proc = &handle_delegation_result_ns_get_auth;
4398 rh->proc_cls = (void*)nah;
4401 nah->proc_cls = proc_cls;
4402 strcpy (nah->result, "");
4404 /* Start delegation resolution in our namestore */
4405 resolve_delegation_ns(rh);
4409 /******** END GET AUTHORITY *************/
4411 /* end of gnunet-service-gns_resolver.c */