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.gnunet = not canonical
184 * @param name the name to test
185 * @return GNUNET_YES if canonical
188 is_canonical (char* name)
193 ndup = GNUNET_strdup (name);
196 for (tok = strtok (NULL, "."); tok != NULL; tok = strtok (NULL, "."))
212 * Callback that shortens authorities
214 * @param gph the handle containing the name to shorten
217 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
221 * 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,
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);
924 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
926 struct ResolverHandle *rh = cls;
928 if (rh->timeout_cont)
929 rh->timeout_cont (rh->timeout_cont_cls, tc);
933 * Processor for background lookups in the DHT
935 * @param cls closure (NULL)
936 * @param rd_count number of records found (not 0)
937 * @param rd record data
940 background_lookup_result_processor (void *cls,
942 const struct GNUNET_NAMESTORE_RecordData *rd)
944 //We could do sth verbose/more useful here but it doesn't make any difference
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
946 "GNS_BG: background dht lookup finished. (%d results)\n",
951 * Handle timeout for DHT requests
953 * @param cls the request handle as closure
954 * @param tc the task context
957 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
959 struct ResolverHandle *rh = cls;
960 struct RecordLookupHandle *rlh = rh->proc_cls;
961 char new_name[MAX_DNS_NAME_LENGTH];
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
965 rh->id, rh->name, rh->timeout.rel_value);
967 * Start resolution in bg
969 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
970 rh->name, GNUNET_GNS_TLD);
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
974 rh->id, new_name, rlh->record_type);
976 gns_resolver_lookup_record (rh->authority,
977 rh->private_local_zone,
981 GNUNET_TIME_UNIT_FOREVER_REL,
983 &background_lookup_result_processor,
985 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
987 GNUNET_DHT_get_stop (rh->get_handle);
988 rh->get_handle = NULL;
989 rh->proc (rh->proc_cls, rh, 0, NULL);
994 * Function called when we get a result from the dht
995 * for our record query
997 * @param cls the request handle
998 * @param exp lifetime
999 * @param key the key the record was stored under
1000 * @param get_path get path
1001 * @param get_path_length get path length
1002 * @param put_path put path
1003 * @param put_path_length put path length
1004 * @param type the block type
1005 * @param size the size of the record
1006 * @param data the record data
1009 process_record_result_dht (void* cls,
1010 struct GNUNET_TIME_Absolute exp,
1011 const struct GNUNET_HashCode * key,
1012 const struct GNUNET_PeerIdentity *get_path,
1013 unsigned int get_path_length,
1014 const struct GNUNET_PeerIdentity *put_path,
1015 unsigned int put_path_length,
1016 enum GNUNET_BLOCK_Type type,
1017 size_t size, const void *data)
1019 struct ResolverHandle *rh;
1020 struct RecordLookupHandle *rlh;
1021 struct GNSNameRecordBlock *nrb;
1022 uint32_t num_records;
1024 char* rd_data = (char*)data;
1028 rh = (struct ResolverHandle *)cls;
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1030 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
1032 rlh = (struct RecordLookupHandle *) rh->proc_cls;
1033 nrb = (struct GNSNameRecordBlock*)data;
1035 /* stop lookup and timeout task */
1036 GNUNET_DHT_get_stop (rh->get_handle);
1037 rh->get_handle = NULL;
1039 if (rh->dht_heap_node != NULL)
1041 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1042 rh->dht_heap_node = NULL;
1045 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1047 GNUNET_SCHEDULER_cancel (rh->timeout_task);
1048 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1051 rh->get_handle = NULL;
1052 name = (char*)&nrb[1];
1053 num_records = ntohl (nrb->rd_count);
1055 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1056 struct NamestoreBGTask *ns_heap_root;
1057 struct NamestoreBGTask *namestore_bg_task;
1059 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
1060 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
1062 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1067 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1068 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1069 rh->proc (rh->proc_cls, rh, 0, NULL);
1073 for (i=0; i<num_records; i++)
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1077 rh->id, name, rh->name);
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1080 rh->id, rd[i].record_type, rlh->record_type);
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1082 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1083 rh->id, rd[i].data_size);
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085 "GNS_PHASE_REC-%llu: Got flag %d\n",
1086 rh->id, rd[i].flags);
1088 if ((strcmp (name, rh->name) == 0) &&
1089 (rd[i].record_type == rlh->record_type))
1097 * FIXME check pubkey against existing key in namestore?
1098 * https://gnunet.org/bugs/view.php?id=2179
1100 if (max_allowed_ns_tasks <=
1101 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1103 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1104 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1107 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1111 /* Save to namestore */
1112 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1113 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1120 &on_namestore_record_put_result, //cont
1123 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1125 GNUNET_TIME_absolute_get().abs_value);
1128 if (0 < rh->answered)
1129 rh->proc (rh->proc_cls, rh, num_records, rd);
1131 rh->proc (rh->proc_cls, rh, 0, NULL);
1138 * Start DHT lookup for a (name -> query->record_type) record in
1139 * rh->authority's zone
1141 * @param rh the pending gns query context
1144 resolve_record_dht (struct ResolverHandle *rh)
1147 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1148 struct GNUNET_HashCode lookup_key;
1149 struct GNUNET_HashCode name_hash_double;
1150 struct GNUNET_HashCode zone_hash_double;
1151 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
1152 struct RecordLookupHandle *rlh = rh->proc_cls;
1153 struct ResolverHandle *rh_heap_root;
1155 GNUNET_CRYPTO_short_hash (rh->name, strlen (rh->name), &name_hash);
1156 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
1157 GNUNET_CRYPTO_short_hash_double (&rh->authority, &zone_hash_double);
1158 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
1159 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1162 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1163 rh->id, rh->name, (char*)&lookup_key_string);
1165 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1166 rh->dht_heap_node = NULL;
1168 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
1171 * Update timeout if necessary
1173 if (GNUNET_SCHEDULER_NO_TASK == rh->timeout_task)
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "GNS_PHASE_REC-%llu: Adjusting timeout\n", rh->id);
1178 * Set timeout for authority lookup phase to 1/2
1180 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1181 GNUNET_TIME_relative_divide (rh->timeout, 2),
1182 &handle_lookup_timeout,
1185 rh->timeout_cont = &dht_lookup_timeout;
1186 rh->timeout_cont_cls = rh;
1190 if (max_allowed_background_queries <=
1191 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1193 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1194 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1195 rh_heap_root->get_handle = NULL;
1196 rh_heap_root->dht_heap_node = NULL;
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1200 rh->id, rh_heap_root->name);
1201 rh_heap_root->proc (rh_heap_root->proc_cls,
1206 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1208 GNUNET_TIME_absolute_get ().abs_value);
1211 xquery = htonl (rlh->record_type);
1213 GNUNET_assert (rh->get_handle == NULL);
1214 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1215 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1217 DHT_GNS_REPLICATION_LEVEL,
1221 &process_record_result_dht,
1228 * Namestore calls this function if we have record for this name.
1229 * (or with rd_count=0 to indicate no matches)
1231 * @param cls the pending query
1232 * @param key the key of the zone we did the lookup
1233 * @param expiration expiration date of the namestore entry
1234 * @param name the name for which we need an authority
1235 * @param rd_count the number of records with 'name'
1236 * @param rd the record data
1237 * @param signature the signature of the authority for the record data
1240 process_record_result_ns (void* cls,
1241 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1242 struct GNUNET_TIME_Absolute expiration,
1243 const char *name, unsigned int rd_count,
1244 const struct GNUNET_NAMESTORE_RecordData *rd,
1245 const struct GNUNET_CRYPTO_RsaSignature *signature)
1247 struct ResolverHandle *rh;
1248 struct RecordLookupHandle *rlh;
1249 struct GNUNET_TIME_Relative remaining_time;
1250 struct GNUNET_CRYPTO_ShortHashCode zone;
1251 struct GNUNET_TIME_Absolute et;
1257 rh->namestore_task = NULL;
1258 GNUNET_CRYPTO_short_hash (key,
1259 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1261 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1269 rh->status |= RSL_RECORD_EXISTS;
1271 if (remaining_time.rel_value == 0)
1273 rh->status |= RSL_RECORD_EXPIRED;
1280 * Lookup terminated and no results
1282 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1283 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1286 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1287 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1290 * Our zone and no result? Cannot resolve TT
1292 rh->proc(rh->proc_cls, rh, 0, NULL);
1296 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1297 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1299 for (i=0; i<rd_count;i++)
1301 if (rd[i].record_type != rlh->record_type)
1304 if (ignore_pending_records &&
1305 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1307 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1308 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1313 //FIXME: eh? do I have to handle this here?
1314 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1315 et.abs_value = rd[i].expiration_time;
1316 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
1318 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1319 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1329 if (0 == rh->answered)
1331 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1332 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1333 rh->proc(rh->proc_cls, rh, 0, NULL);
1337 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1338 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1339 rh->id, rh->answered, rd_count);
1340 rh->proc(rh->proc_cls, rh, rd_count, rd);
1345 * VPN redirect result callback
1347 * @param cls the resolver handle
1348 * @param af the requested address family
1349 * @param address in_addr(6) respectively
1352 process_record_result_vpn (void* cls, int af, const void *address)
1354 struct ResolverHandle *rh = cls;
1355 struct RecordLookupHandle *rlh;
1356 struct GNUNET_NAMESTORE_RecordData rd;
1360 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1361 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1366 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1368 if (GNUNET_GNS_RECORD_A != rlh->record_type)
1370 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1371 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1373 rh->proc (rh->proc_cls, rh, 0, NULL);
1376 rd.record_type = GNUNET_GNS_RECORD_A;
1377 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1379 rd.data_size = sizeof (struct in_addr);
1381 rh->proc (rh->proc_cls, rh, 1, &rd);
1384 else if (AF_INET6 == af)
1386 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1387 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1389 if (GNUNET_GNS_RECORD_AAAA != rlh->record_type)
1391 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1392 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1394 rh->proc (rh->proc_cls, rh, 0, NULL);
1397 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1398 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1400 rd.data_size = sizeof (struct in6_addr);
1402 rh->proc (rh->proc_cls, rh, 1, &rd);
1406 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1407 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1409 rh->proc (rh->proc_cls, rh, 0, NULL);
1416 * Process VPN lookup result for record
1418 * @param cls the record lookup handle
1419 * @param rh resolver handle
1420 * @param rd_count number of results (1)
1421 * @param rd record data containing the result
1424 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1425 unsigned int rd_count,
1426 const struct GNUNET_NAMESTORE_RecordData *rd)
1428 struct RecordLookupHandle* rlh = cls;
1432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1433 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1436 /* give up, cannot resolve */
1437 finish_lookup(rh, rlh, 0, NULL);
1441 /* results found yay */
1442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1443 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1445 finish_lookup(rh, rlh, rd_count, rd);
1450 * Sends a UDP dns query to a nameserver specified in the rh
1452 * @param rh the resolver handle
1455 send_dns_packet (struct ResolverHandle *rh);
1459 handle_dns_resolver (void *cls,
1460 const struct sockaddr *addr,
1463 struct ResolverHandle *rh = cls;
1464 struct RecordLookupHandle *rlh = rh->proc_cls;
1465 struct GNUNET_NAMESTORE_RecordData rd;
1466 struct sockaddr_in *sai;
1467 struct sockaddr_in6 *sai6;
1471 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1472 "No address found in DNS!\n");
1473 finish_lookup (rh, rlh, 0, NULL);
1477 if (sizeof (struct sockaddr_in) == addrlen)
1479 sai = (struct sockaddr_in*) addr;
1480 rd.record_type = GNUNET_GNS_RECORD_A;
1481 rd.data_size = sizeof (struct in_addr);
1482 rd.data = &sai->sin_addr;
1484 else if (sizeof (struct sockaddr_in6) == addrlen)
1486 sai6 = (struct sockaddr_in6*) addr;
1487 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1488 rd.data_size = sizeof (struct in6_addr);
1489 rd.data = &sai6->sin6_addr;
1493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1494 "Address length is garbage!\n");
1495 finish_lookup (rh, rlh, 0, NULL);
1498 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1500 finish_lookup (rh, rlh, 1, &rd);
1504 * Resolve DNS name via local stub resolver
1506 * @param rh the resolver handle
1509 resolve_dns_name (struct ResolverHandle *rh)
1511 struct RecordLookupHandle *rlh = rh->proc_cls;
1514 if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1515 (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1518 "Can only resolve A/AAAA via stub... abort\n");
1519 finish_lookup (rh, rlh, 0, NULL);
1523 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1528 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1531 &handle_dns_resolver,
1537 * Read DNS udp packet from socket
1539 * @param cls the resolver handle
1540 * @param tc task context
1543 read_dns_response (void *cls,
1544 const struct GNUNET_SCHEDULER_TaskContext *tc)
1546 struct ResolverHandle *rh = cls;
1547 struct RecordLookupHandle *rlh = rh->proc_cls;
1548 char buf[UINT16_MAX];
1550 struct sockaddr_in addr;
1552 struct GNUNET_DNSPARSER_Packet *packet;
1553 struct GNUNET_NAMESTORE_RecordData rd;
1554 int found_delegation = GNUNET_NO;
1555 int found_cname = GNUNET_NO;
1556 char* delegation_name = NULL;
1557 int zone_offset = 0;
1560 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1561 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1563 /* timeout or shutdown */
1564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1565 "Terminating DNS query %d\n", tc->reason);
1566 finish_lookup (rh, rlh, 0, NULL);
1570 addrlen = sizeof (addr);
1571 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1573 (struct sockaddr*) &addr,
1578 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1579 finish_lookup (rh, rlh, 0, NULL);
1583 packet = GNUNET_DNSPARSER_parse (buf, r);
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1588 "Failed to parse DNS reply!\n");
1589 finish_lookup (rh, rlh, 0, NULL);
1593 for (i = 0; i < packet->num_answers; i++)
1595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1596 "Got record type %d (want %d)\n",
1597 packet->answers[i].type,
1599 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1600 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "CNAME... restarting query with %s\n",
1604 packet->answers[i].data.hostname
1606 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1607 found_cname = GNUNET_YES;
1611 if ((packet->answers[i].type == rlh->record_type) &&
1612 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1616 rd.data = packet->answers[i].data.raw.data;
1617 rd.data_size = packet->answers[i].data.raw.data_len;
1618 rd.record_type = packet->answers[i].type;
1620 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1621 finish_lookup (rh, rlh, 1, &rd);
1622 GNUNET_DNSPARSER_free_packet (packet);
1627 if (GNUNET_YES == found_cname)
1629 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1631 if (0 > zone_offset)
1634 /* restart query with CNAME */
1635 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1638 "Asking same server for %s\n", rh->dns_name);
1639 send_dns_packet (rh);
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1644 "Trying system resolver for %s\n", rh->dns_name);
1645 resolve_dns_name (rh);
1648 GNUNET_DNSPARSER_free_packet (packet);
1652 for (i = 0; i < packet->num_authority_records; i++)
1655 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1658 "Found NS delegation!\n");
1659 found_delegation = GNUNET_YES;
1660 delegation_name = packet->authority_records[i].data.hostname;
1665 for (i = 0; i < packet->num_additional_records; i++)
1667 if (GNUNET_NO == found_delegation)
1670 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1671 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1673 GNUNET_assert (sizeof (struct in_addr) ==
1674 packet->authority_records[i].data.raw.data_len);
1676 rh->dns_addr.sin_addr =
1677 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1678 send_dns_packet (rh);
1679 GNUNET_DNSPARSER_free_packet (packet);
1684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1685 "Nothing useful in DNS reply!\n");
1686 finish_lookup (rh, rlh, 0, NULL);
1687 GNUNET_DNSPARSER_free_packet (packet);
1692 * Sends a UDP dns query to a nameserver specified in the rh
1694 * @param rh the request handle
1697 send_dns_packet (struct ResolverHandle *rh)
1699 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1700 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1703 "Sending %dbyte DNS query\n",
1704 rh->dns_raw_packet_size);
1706 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1708 rh->dns_raw_packet_size,
1709 (struct sockaddr*)&rh->dns_addr,
1710 sizeof (struct sockaddr_in));
1712 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1713 rh->timeout, //FIXME less?
1719 GNUNET_NETWORK_fdset_destroy (rset);
1724 * The final phase of resoution.
1725 * We found a NS RR and want to resolve via DNS
1727 * @param rh the pending lookup handle
1728 * @param rd_count length of record data
1729 * @param rd record data containing VPN RR
1732 resolve_record_dns (struct ResolverHandle *rh,
1734 const struct GNUNET_NAMESTORE_RecordData *rd)
1736 struct GNUNET_DNSPARSER_Query query;
1737 struct GNUNET_DNSPARSER_Packet packet;
1738 struct GNUNET_DNSPARSER_Flags flags;
1739 struct in_addr dnsip;
1740 struct sockaddr_in addr;
1741 struct sockaddr *sa;
1743 struct RecordLookupHandle *rlh = rh->proc_cls;
1745 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1747 /* We cancel here as to not include the ns lookup in the timeout */
1748 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1750 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1751 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1753 /* Start shortening */
1754 if ((NULL != rh->priv_key) &&
1755 (GNUNET_YES == is_canonical (rh->name)))
1757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1758 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1760 start_shorten (rh->authority_chain_head,
1764 for (i = 0; i < rd_count; i++)
1766 /* Synthesize dns name */
1767 if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1769 strcpy (rh->dns_zone, (char*)rd[i].data);
1770 if (0 == strcmp (rh->name, ""))
1771 strcpy (rh->dns_name, (char*)rd[i].data);
1773 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1776 if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1777 dnsip = *((struct in_addr*)rd[i].data);
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1781 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1785 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1786 if (NULL == rh->dns_sock)
1788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1789 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1791 finish_lookup (rh, rlh, 0, NULL);
1795 memset (&addr, 0, sizeof (struct sockaddr_in));
1796 sa = (struct sockaddr *) &addr;
1797 sa->sa_family = AF_INET;
1798 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1800 sizeof (struct sockaddr_in)))
1802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1803 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1805 finish_lookup (rh, rlh, 0, NULL);
1808 query.name = rh->dns_name;
1809 query.type = rlh->record_type;
1810 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1811 memset (&flags, 0, sizeof (flags));
1812 flags.recursion_desired = 1;
1813 flags.checking_disabled = 1;
1814 packet.queries = &query;
1815 packet.answers = NULL;
1816 packet.authority_records = NULL;
1817 packet.num_queries = 1;
1818 packet.num_answers = 0;
1819 packet.num_authority_records = 0;
1820 packet.num_additional_records = 0;
1821 packet.flags = flags;
1823 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1825 &rh->dns_raw_packet,
1826 &rh->dns_raw_packet_size))
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1829 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1831 GNUNET_NETWORK_socket_close (rh->dns_sock);
1832 finish_lookup (rh, rlh, 0, NULL);
1836 rh->dns_addr.sin_family = AF_INET;
1837 rh->dns_addr.sin_port = htons (53); //domain
1838 rh->dns_addr.sin_addr = dnsip;
1839 #if HAVE_SOCKADDR_IN_SIN_LEN
1840 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1843 send_dns_packet (rh);
1848 * The final phase of resoution.
1849 * We found a VPN RR and want to request an IPv4/6 address
1851 * @param rh the pending lookup handle
1852 * @param rd_count length of record data
1853 * @param rd record data containing VPN RR
1856 resolve_record_vpn (struct ResolverHandle *rh,
1858 const struct GNUNET_NAMESTORE_RecordData *rd)
1860 struct RecordLookupHandle *rlh = rh->proc_cls;
1861 struct GNUNET_HashCode serv_desc;
1862 struct vpn_data* vpn;
1865 /* We cancel here as to not include the ns lookup in the timeout */
1866 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1868 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1869 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1871 /* Start shortening */
1872 if ((NULL != rh->priv_key) &&
1873 (GNUNET_YES == is_canonical (rh->name)))
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1878 start_shorten (rh->authority_chain_head,
1882 vpn = (struct vpn_data*)rd->data;
1885 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1886 strlen ((char*)&vpn[1]) + 1,
1888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1889 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1892 GNUNET_h2s (&vpn->peer));
1894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1895 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1898 GNUNET_h2s (&serv_desc));
1899 rh->proc = &handle_record_vpn;
1900 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1904 if (NULL == vpn_handle)
1906 vpn_handle = GNUNET_VPN_connect (cfg);
1907 if (NULL == vpn_handle)
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1910 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1911 finish_lookup (rh, rh->proc_cls, 0, NULL);
1916 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1917 af, ntohs (vpn->proto),
1918 (struct GNUNET_PeerIdentity *)&vpn->peer,
1921 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1922 &process_record_result_vpn,
1928 * The final phase of resolution.
1929 * rh->name is a name that is canonical and we do not have a delegation.
1930 * Query namestore for this record
1932 * @param rh the pending lookup handle
1935 resolve_record_ns(struct ResolverHandle *rh)
1937 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1939 /* We cancel here as to not include the ns lookup in the timeout */
1940 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1942 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1943 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1945 /* Start shortening */
1946 if ((NULL != rh->priv_key) &&
1947 (GNUNET_YES == is_canonical (rh->name)))
1949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1950 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1952 start_shorten (rh->authority_chain_head,
1957 * Try to resolve this record in our namestore.
1958 * The name to resolve is now in rh->authority_name
1959 * since we tried to resolve it to an authority
1962 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1966 &process_record_result_ns,
1973 * Handle timeout for DHT requests
1975 * @param cls the request handle as closure
1976 * @param tc the task context
1979 dht_authority_lookup_timeout(void *cls,
1980 const struct GNUNET_SCHEDULER_TaskContext *tc)
1982 struct ResolverHandle *rh = cls;
1983 struct RecordLookupHandle *rlh = rh->proc_cls;
1984 char new_name[MAX_DNS_NAME_LENGTH];
1986 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1987 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
1988 rh->id, rh->authority_name, rh->timeout.rel_value);
1990 rh->status |= RSL_TIMED_OUT;
1991 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1992 if (NULL != rh->get_handle)
1993 GNUNET_DHT_get_stop (rh->get_handle);
1995 rh->get_handle = NULL;
1996 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
1998 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1999 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
2001 rh->proc(rh->proc_cls, rh, 0, NULL);
2005 if (0 == strcmp(rh->name, ""))
2008 * promote authority back to name and try to resolve record
2010 strcpy(rh->name, rh->authority_name);
2011 rh->proc(rh->proc_cls, rh, 0, NULL);
2016 * Start resolution in bg
2018 GNUNET_assert (0 < GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
2019 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD));
2021 strcpy(rh->name, new_name);
2023 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2024 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2025 rh->id, rh->name, rlh->record_type);
2027 gns_resolver_lookup_record(rh->authority,
2028 rh->private_local_zone,
2032 GNUNET_TIME_UNIT_FOREVER_REL,
2034 &background_lookup_result_processor,
2037 rh->proc(rh->proc_cls, rh, 0, NULL);
2041 static void resolve_delegation_dht(struct ResolverHandle *rh);
2044 static void resolve_delegation_ns(struct ResolverHandle *rh);
2048 * Namestore resolution for delegation finished. Processing result.
2050 * @param cls the closure
2051 * @param rh resolver handle
2052 * @param rd_count number of results (always 0)
2053 * @param rd record data (always NULL)
2056 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2057 unsigned int rd_count,
2058 const struct GNUNET_NAMESTORE_RecordData *rd);
2062 * This is a callback function that checks for key revocation
2064 * @param cls the pending query
2065 * @param key the key of the zone we did the lookup
2066 * @param expiration expiration date of the record data set in the namestore
2067 * @param name the name for which we need an authority
2068 * @param rd_count the number of records with 'name'
2069 * @param rd the record data
2070 * @param signature the signature of the authority for the record data
2073 process_pkey_revocation_result_ns (void *cls,
2074 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2075 struct GNUNET_TIME_Absolute expiration,
2077 unsigned int rd_count,
2078 const struct GNUNET_NAMESTORE_RecordData *rd,
2079 const struct GNUNET_CRYPTO_RsaSignature *signature)
2081 struct ResolverHandle *rh = cls;
2082 struct GNUNET_TIME_Relative remaining_time;
2085 rh->namestore_task = NULL;
2086 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2088 for (i = 0; i < rd_count; i++)
2090 if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2092 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2093 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2095 rh->status |= RSL_PKEY_REVOKED;
2096 rh->proc (rh->proc_cls, rh, 0, NULL);
2101 if ((NULL == name) ||
2102 (0 == remaining_time.rel_value))
2104 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2105 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2108 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2110 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2111 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2112 rh->id, "+.gnunet", GNUNET_GNS_RECORD_REV);
2114 gns_resolver_lookup_record(rh->authority,
2115 rh->private_local_zone,
2116 GNUNET_GNS_RECORD_REV,
2119 GNUNET_TIME_UNIT_FOREVER_REL,
2121 &background_lookup_result_processor,
2125 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2126 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2129 * We are done with PKEY resolution if name is empty
2130 * else resolve again with new authority
2132 if (strcmp (rh->name, "") == 0)
2133 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2135 resolve_delegation_ns (rh);
2140 * Callback when record data is put into namestore
2142 * @param cls the closure
2143 * @param success GNUNET_OK on success
2144 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2147 on_namestore_delegation_put_result(void *cls,
2151 struct NamestoreBGTask *nbg = cls;
2153 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2156 if (GNUNET_NO == success)
2158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2159 "GNS_NS: records already in namestore\n");
2162 else if (GNUNET_YES == success)
2164 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2165 "GNS_NS: records successfully put in namestore\n");
2169 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2170 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2174 * Function called when we get a result from the dht
2175 * for our query. Recursively tries to resolve authorities
2178 * @param cls the request handle
2179 * @param exp lifetime
2180 * @param key the key the record was stored under
2181 * @param get_path get path
2182 * @param get_path_length get path length
2183 * @param put_path put path
2184 * @param put_path_length put path length
2185 * @param type the block type
2186 * @param size the size of the record
2187 * @param data the record data
2190 process_delegation_result_dht(void* cls,
2191 struct GNUNET_TIME_Absolute exp,
2192 const struct GNUNET_HashCode * key,
2193 const struct GNUNET_PeerIdentity *get_path,
2194 unsigned int get_path_length,
2195 const struct GNUNET_PeerIdentity *put_path,
2196 unsigned int put_path_length,
2197 enum GNUNET_BLOCK_Type type,
2198 size_t size, const void *data)
2200 struct ResolverHandle *rh = cls;
2201 struct GNSNameRecordBlock *nrb;
2202 uint32_t num_records;
2204 char* rd_data = (char*) data;
2207 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
2208 struct GNUNET_HashCode zone_hash_double, name_hash_double;
2210 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2211 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2216 nrb = (struct GNSNameRecordBlock*)data;
2218 /* stop dht lookup and timeout task */
2219 GNUNET_DHT_get_stop (rh->get_handle);
2220 rh->get_handle = NULL;
2221 if (rh->dht_heap_node != NULL)
2223 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2224 rh->dht_heap_node = NULL;
2227 num_records = ntohl(nrb->rd_count);
2228 name = (char*)&nrb[1];
2230 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2231 struct NamestoreBGTask *ns_heap_root;
2232 struct NamestoreBGTask *namestore_bg_task;
2234 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2235 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2236 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2241 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2242 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2247 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2248 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2249 rh->id, name, rh->authority_name);
2250 for (i=0; i<num_records; i++)
2253 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2254 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2255 rh->id, name, rh->authority_name);
2256 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2257 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2258 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2259 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2260 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2261 rh->id, rd[i].data_size);
2262 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2263 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2264 rh->id, rd[i].flags);
2266 if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2267 (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2268 (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2271 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2273 if (0 == strcmp(rh->name, ""))
2274 strcpy(rh->name, rh->authority_name);
2276 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2277 rh->name, rh->authority_name); //FIXME ret
2282 if ((0 == strcmp(name, rh->authority_name)) &&
2283 (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2285 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2286 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2289 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2290 struct AuthorityChain *auth =
2291 GNUNET_malloc(sizeof(struct AuthorityChain));
2292 auth->zone = rh->authority;
2293 memset(auth->name, 0, strlen(rh->authority_name)+1);
2294 strcpy(auth->name, rh->authority_name);
2295 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2296 rh->authority_chain_tail,
2299 if (NULL != rh->rd.data)
2300 GNUNET_free ((void*)rh->rd.data);
2302 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2303 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2304 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2307 /** try to import pkey if private key available */
2308 //if (rh->priv_key && is_canonical (rh->name))
2309 // process_discovered_authority(name, auth->zone,
2310 // rh->authority_chain_tail->zone,
2317 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2318 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2319 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2320 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2322 /* Save to namestore */
2323 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
2326 if (max_allowed_ns_tasks <=
2327 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2329 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2330 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2332 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2333 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
2337 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2339 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2341 GNUNET_TIME_absolute_get().abs_value);
2342 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2349 &on_namestore_delegation_put_result, //cont
2350 namestore_bg_task); //cls
2354 if (0 != rh->answered)
2359 * FIXME in this case. should we ask namestore again?
2361 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2362 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2363 rh->id, rh->authority_name, rh->name);
2365 if (0 == strcmp(rh->name, ""))
2367 /* Start shortening */
2368 if ((NULL != rh->priv_key) &&
2369 (GNUNET_YES == is_canonical (rh->name)))
2371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2372 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2374 start_shorten (rh->authority_chain_head,
2379 rh->proc = &handle_delegation_ns;
2382 /* Check for key revocation and delegate */
2383 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2386 GNUNET_GNS_RECORD_REV,
2387 &process_pkey_revocation_result_ns,
2394 * No pkey but name exists
2397 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2398 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2399 rh->id, rh->authority_name, rh->name);
2400 if (0 == strcmp(rh->name, ""))
2401 strcpy(rh->name, rh->authority_name);
2403 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2404 rh->name, rh->authority_name); //FIXME ret
2406 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2407 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2408 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2409 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2411 rh->proc(rh->proc_cls, rh, 0, NULL);
2414 //FIXME maybe define somewhere else?
2415 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2416 +(MAX_DNS_NAME_LENGTH*2)
2417 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2418 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2422 expand_plus(char* dest, char* src, char* repl)
2425 unsigned int s_len = strlen(src)+1;
2427 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2428 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2429 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2433 "GNS_POSTPROCESS: %s too short\n", src);
2435 /* no postprocessing */
2436 memcpy(dest, src, s_len+1);
2440 if (0 == strcmp(src+s_len-3, ".+"))
2442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2443 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2444 memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2448 pos += strlen(repl);
2449 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2450 "GNS_POSTPROCESS: Expanded to %s\n", dest);
2454 memcpy(dest, src, s_len+1);
2462 finish_lookup (struct ResolverHandle *rh,
2463 struct RecordLookupHandle* rlh,
2464 unsigned int rd_count,
2465 const struct GNUNET_NAMESTORE_RecordData *rd)
2468 char new_rr_data[MAX_DNS_NAME_LENGTH];
2469 char new_mx_data[MAX_MX_LENGTH];
2470 char new_soa_data[MAX_SOA_LENGTH];
2471 char new_srv_data[MAX_SRV_LENGTH];
2472 struct srv_data *old_srv;
2473 struct srv_data *new_srv;
2474 struct soa_data *old_soa;
2475 struct soa_data *new_soa;
2476 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2479 unsigned int offset;
2481 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
2483 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2484 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2487 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2490 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2492 for (i = 0; i < rd_count; i++)
2495 if ((GNUNET_GNS_RECORD_NS != rd[i].record_type) &&
2496 (GNUNET_GNS_RECORD_PTR != rd[i].record_type) &&
2497 (GNUNET_GNS_RECORD_CNAME != rd[i].record_type) &&
2498 (GNUNET_GNS_RECORD_MX != rd[i].record_type) &&
2499 (GNUNET_GNS_RECORD_SOA != rd[i].record_type) &&
2500 (GNUNET_GNS_RECORD_SRV != rd[i].record_type))
2502 p_rd[i].data = rd[i].data;
2507 * for all those records we 'should'
2508 * also try to resolve the A/AAAA records (RFC1035)
2509 * This is a feature and not important
2512 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2513 "GNS_POSTPROCESS: Postprocessing\n");
2514 if (0 == strcmp(rh->name, "+"))
2515 repl_string = rlh->name;
2517 repl_string = rlh->name+strlen(rh->name)+1;
2520 if (GNUNET_GNS_RECORD_MX == rd[i].record_type)
2522 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2523 offset = sizeof(uint16_t);
2524 pos = new_mx_data+offset;
2525 expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2527 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2528 p_rd[i].data = new_mx_data;
2529 p_rd[i].data_size = offset;
2531 else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2534 * Prio, weight and port
2536 new_srv = (struct srv_data*)new_srv_data;
2537 old_srv = (struct srv_data*)rd[i].data;
2538 new_srv->prio = old_srv->prio;
2539 new_srv->weight = old_srv->weight;
2540 new_srv->port = old_srv->port;
2541 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2543 p_rd[i].data = new_srv_data;
2544 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2546 else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2548 /* expand mname and rname */
2549 old_soa = (struct soa_data*)rd[i].data;
2550 new_soa = (struct soa_data*)new_soa_data;
2551 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2552 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2553 offset = strlen ((char*)&new_soa[1]) + 1;
2554 expand_plus((char*)&new_soa[1] + offset,
2555 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2557 p_rd[i].data_size = sizeof (struct soa_data)
2559 + strlen ((char*)&new_soa[1] + offset);
2560 p_rd[i].data = new_soa_data;
2565 expand_plus(pos, (char*)rd[i].data, repl_string);
2566 p_rd[i].data_size = strlen(new_rr_data)+1;
2567 p_rd[i].data = new_rr_data;
2572 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2574 free_resolver_handle (rh);
2578 * Process DHT lookup result for record.
2580 * @param cls the closure
2581 * @param rh resolver handle
2582 * @param rd_count number of results
2583 * @param rd record data
2586 handle_record_dht(void* cls, struct ResolverHandle *rh,
2587 unsigned int rd_count,
2588 const struct GNUNET_NAMESTORE_RecordData *rd)
2590 struct RecordLookupHandle* rlh = cls;
2594 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2595 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2597 /* give up, cannot resolve */
2598 finish_lookup(rh, rlh, 0, NULL);
2601 /* results found yay */
2602 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2603 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2604 finish_lookup(rh, rlh, rd_count, rd);
2611 * Process namestore lookup result for record.
2613 * @param cls the closure
2614 * @param rh resolver handle
2615 * @param rd_count number of results
2616 * @param rd record data
2619 handle_record_ns (void* cls, struct ResolverHandle *rh,
2620 unsigned int rd_count,
2621 const struct GNUNET_NAMESTORE_RecordData *rd)
2623 struct RecordLookupHandle* rlh = cls;
2624 int check_dht = GNUNET_YES;
2628 /* results found yay */
2629 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2630 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2631 finish_lookup(rh, rlh, rd_count, rd);
2635 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2636 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2640 * There are 5 conditions that have to met for us to consult the DHT:
2641 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2642 * 2. No entry in the NS existed AND
2643 * 3. The zone queried is not the local resolver's zone AND
2644 * 4. The name that was looked up is '+'
2645 * because if it was any other canonical name we either already queried
2646 * the DHT for the authority in the authority lookup phase (and thus
2647 * would already have an entry in the NS for the record)
2648 * 5. We are not in cache only mode
2650 if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2651 (0 == (rh->status & RSL_RECORD_EXISTS)) )
2652 check_dht = GNUNET_NO;
2654 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2655 &rh->private_local_zone))
2656 check_dht = GNUNET_NO;
2658 if ((0 != strcmp (rh->name, "+")) && (GNUNET_YES == is_srv (rh->name)))
2659 check_dht = GNUNET_NO;
2661 if (GNUNET_YES == rh->only_cached)
2662 check_dht = GNUNET_NO;
2664 if (GNUNET_YES == check_dht)
2666 rh->proc = &handle_record_dht;
2667 resolve_record_dht(rh);
2670 /* give up, cannot resolve */
2671 finish_lookup(rh, rlh, 0, NULL);
2676 * Move one level up in the domain hierarchy and return the
2677 * passed top level domain.
2679 * @param name the domain
2680 * @param dest the destination where the tld will be put
2683 pop_tld(char* name, char* dest)
2687 if (GNUNET_YES == is_canonical (name))
2694 for (len = strlen(name); 0 < len; len--)
2696 if (*(name+len) == '.')
2706 strcpy(dest, (name+len+1));
2710 * Checks if name is in tld
2712 * @param name the name to check
2713 * @param tld the TLD to check for
2714 * @return GNUNET_YES or GNUNET_NO
2717 is_tld(const char* name, const char* tld)
2721 if (strlen(name) <= strlen(tld))
2724 offset = strlen(name)-strlen(tld);
2725 if (0 != strcmp(name+offset, tld))
2727 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2728 "%s is not in .%s TLD\n", name, tld);
2735 * DHT resolution for delegation finished. Processing result.
2737 * @param cls the closure
2738 * @param rh resolver handle
2739 * @param rd_count number of results (always 0)
2740 * @param rd record data (always NULL)
2743 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2744 unsigned int rd_count,
2745 const struct GNUNET_NAMESTORE_RecordData *rd)
2747 struct RecordLookupHandle* rlh;
2751 if (0 == strcmp(rh->name, ""))
2753 if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2755 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2756 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2758 finish_lookup(rh, rlh, rd_count, rd);
2761 /* We resolved full name for delegation. resolving record */
2762 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2763 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2765 strcpy(rh->name, "+\0");
2766 rh->proc = &handle_record_ns;
2767 resolve_record_ns(rh);
2772 * we still have some left
2774 if (GNUNET_YES == is_canonical (rh->name))
2776 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2777 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2780 rh->proc = &handle_record_ns;
2781 resolve_record_ns(rh);
2784 /* give up, cannot resolve */
2785 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2786 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2788 finish_lookup(rh, rlh, 0, NULL);
2793 * Start DHT lookup for a name -> PKEY (compare NS) record in
2794 * rh->authority's zone
2796 * @param rh the pending gns query
2799 resolve_delegation_dht(struct ResolverHandle *rh)
2802 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2803 struct GNUNET_HashCode name_hash_double;
2804 struct GNUNET_HashCode zone_hash_double;
2805 struct GNUNET_HashCode lookup_key;
2806 struct ResolverHandle *rh_heap_root;
2808 pop_tld(rh->name, rh->authority_name);
2810 //FIXME handle return values here
2811 GNUNET_CRYPTO_short_hash(rh->authority_name,
2812 strlen(rh->authority_name),
2814 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2815 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2816 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2818 rh->dht_heap_node = NULL;
2820 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2822 rh->timeout_cont = &dht_authority_lookup_timeout;
2823 rh->timeout_cont_cls = rh;
2827 if (max_allowed_background_queries <=
2828 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2830 /* terminate oldest lookup */
2831 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2832 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2833 rh_heap_root->get_handle = NULL;
2834 rh_heap_root->dht_heap_node = NULL;
2836 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2837 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2838 rh->id, rh_heap_root->authority_name);
2840 rh_heap_root->proc(rh_heap_root->proc_cls,
2845 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2847 GNUNET_TIME_absolute_get().abs_value);
2850 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2851 GNUNET_assert(rh->get_handle == NULL);
2852 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2853 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2855 DHT_GNS_REPLICATION_LEVEL,
2859 &process_delegation_result_dht,
2865 * Namestore resolution for delegation finished. Processing result.
2867 * @param cls the closure
2868 * @param rh resolver handle
2869 * @param rd_count number of results (always 0)
2870 * @param rd record data (always NULL)
2873 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2874 unsigned int rd_count,
2875 const struct GNUNET_NAMESTORE_RecordData *rd)
2877 struct RecordLookupHandle* rlh;
2879 int check_dht = GNUNET_YES;
2882 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2883 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2884 rh->id, rh->status);
2886 if (rh->status & RSL_PKEY_REVOKED)
2888 finish_lookup (rh, rlh, 0, NULL);
2892 if (0 == strcmp(rh->name, ""))
2895 /* We resolved full name for delegation. resolving record */
2896 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2897 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2899 if (rh->status & RSL_CNAME_FOUND)
2901 if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2903 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2904 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2906 strcpy (rh->name, rh->authority_name);
2907 finish_lookup (rh, rlh, rd_count, rd);
2912 if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2914 s_len = strlen (rd->data) - 2;
2915 memcpy (rh->name, rd->data, s_len);
2916 rh->name[s_len] = '\0';
2917 resolve_delegation_ns (rh);
2920 else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2922 gns_resolver_lookup_record (rh->authority,
2923 rh->private_local_zone,
2932 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2933 free_resolver_handle (rh);
2939 strcpy (rh->dns_name, (char*)rd->data);
2940 resolve_dns_name (rh);
2945 else if (rh->status & RSL_DELEGATE_VPN)
2947 if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2949 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2950 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2952 finish_lookup(rh, rlh, rd_count, rd);
2955 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2956 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2958 GNUNET_assert (NULL != rd);
2959 rh->proc = &handle_record_vpn;
2960 resolve_record_vpn (rh, rd_count, rd);
2963 else if (rh->status & RSL_DELEGATE_NS)
2965 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2967 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2968 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2970 finish_lookup(rh, rlh, rd_count, rd);
2974 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2975 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2977 GNUNET_assert (NULL != rd);
2978 rh->proc = &handle_record_ns;
2979 resolve_record_dns (rh, rd_count, rd);
2982 else if (rh->status & RSL_DELEGATE_PKEY)
2984 if (rh->status & RSL_PKEY_REVOKED)
2986 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2987 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
2989 finish_lookup (rh, rlh, 0, NULL);
2992 else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2994 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2995 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
2997 finish_lookup(rh, rlh, rd_count, rd);
3001 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3002 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3004 strcpy(rh->name, "+\0");
3005 rh->proc = &handle_record_ns;
3006 resolve_record_ns(rh);
3010 if (rh->status & RSL_DELEGATE_NS)
3012 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
3014 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3015 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3017 finish_lookup(rh, rlh, rd_count, rd);
3021 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3022 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3024 GNUNET_assert (NULL != rd);
3025 rh->proc = &handle_record_ns;
3026 resolve_record_dns (rh, rd_count, rd);
3031 * we still have some left
3032 * check if authority in ns is fresh
3034 * or we are authority
3037 if ((rh->status & RSL_RECORD_EXISTS) &&
3038 !(rh->status & RSL_RECORD_EXPIRED))
3039 check_dht = GNUNET_NO;
3041 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3042 &rh->private_local_zone))
3043 check_dht = GNUNET_NO;
3045 if (GNUNET_YES == rh->only_cached)
3046 check_dht = GNUNET_NO;
3048 if (GNUNET_YES == check_dht)
3051 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3052 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3054 rh->proc = &handle_delegation_dht;
3055 resolve_delegation_dht(rh);
3059 if (GNUNET_NO == is_canonical (rh->name))
3061 /* give up, cannot resolve */
3062 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3063 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3066 finish_lookup(rh, rlh, rd_count, rd);
3069 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3070 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3073 rh->proc = &handle_record_ns;
3074 resolve_record_ns(rh);
3079 * This is a callback function that should give us only PKEY
3080 * records. Used to query the namestore for the authority (PKEY)
3081 * for 'name'. It will recursively try to resolve the
3082 * authority for a given name from the namestore.
3084 * @param cls the pending query
3085 * @param key the key of the zone we did the lookup
3086 * @param expiration expiration date of the record data set in the namestore
3087 * @param name the name for which we need an authority
3088 * @param rd_count the number of records with 'name'
3089 * @param rd the record data
3090 * @param signature the signature of the authority for the record data
3093 process_delegation_result_ns (void* cls,
3094 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3095 struct GNUNET_TIME_Absolute expiration,
3097 unsigned int rd_count,
3098 const struct GNUNET_NAMESTORE_RecordData *rd,
3099 const struct GNUNET_CRYPTO_RsaSignature *signature)
3101 struct ResolverHandle *rh;
3102 struct GNUNET_TIME_Relative remaining_time;
3103 struct GNUNET_CRYPTO_ShortHashCode zone;
3104 char new_name[MAX_DNS_NAME_LENGTH];
3106 struct GNUNET_TIME_Absolute et;
3108 rh = (struct ResolverHandle *)cls;
3109 rh->namestore_task = NULL;
3110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3111 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3114 GNUNET_CRYPTO_short_hash (key,
3115 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3117 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3124 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3126 rh->status |= RSL_RECORD_EXISTS;
3128 if (remaining_time.rel_value == 0)
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3131 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3133 rh->status |= RSL_RECORD_EXPIRED;
3138 * No authority found in namestore.
3143 * We did not find an authority in the namestore
3148 * Promote this authority back to a name maybe it is
3151 if (strcmp (rh->name, "") == 0)
3153 /* simply promote back */
3154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3155 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3156 rh->id, rh->authority_name);
3157 strcpy (rh->name, rh->authority_name);
3161 /* add back to existing name */
3162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3163 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3164 rh->id, rh->authority_name, rh->name);
3165 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3166 rh->name, rh->authority_name);
3167 strcpy (rh->name, new_name);
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3169 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3173 rh->proc (rh->proc_cls, rh, 0, NULL);
3178 * We found an authority that may be able to help us
3179 * move on with query
3180 * Note only 1 pkey should have been returned.. anything else would be strange
3182 for (i=0; i < rd_count;i++)
3186 * A CNAME. Like regular DNS this means the is no other record for this
3189 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3191 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3192 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3195 rh->status |= RSL_CNAME_FOUND;
3196 rh->proc (rh->proc_cls, rh, rd_count, rd);
3203 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3205 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3206 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3208 rh->status |= RSL_DELEGATE_VPN;
3209 rh->proc (rh->proc_cls, rh, rd_count, rd);
3215 * FIXME make optional
3217 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3219 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3220 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3222 rh->status |= RSL_DELEGATE_NS;
3223 rh->proc (rh->proc_cls, rh, rd_count, rd);
3227 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3230 rh->status |= RSL_DELEGATE_PKEY;
3232 if ((ignore_pending_records != 0) &&
3233 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3236 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3242 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3243 et.abs_value = rd[i].expiration_time;
3244 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3248 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3250 if (remaining_time.rel_value == 0)
3252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3253 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3255 rh->authority_chain_head->fresh = 0;
3256 rh->proc (rh->proc_cls, rh, 0, NULL);
3264 * Resolve rest of query with new authority
3266 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3267 memcpy (&rh->authority, rd[i].data,
3268 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3269 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3270 auth->zone = rh->authority;
3271 memset (auth->name, 0, strlen (rh->authority_name)+1);
3272 strcpy (auth->name, rh->authority_name);
3273 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3274 rh->authority_chain_tail,
3276 if (NULL != rh->rd.data)
3277 GNUNET_free ((void*)(rh->rd.data));
3279 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3280 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3281 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3283 /* Check for key revocation and delegate */
3284 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3287 GNUNET_GNS_RECORD_REV,
3288 &process_pkey_revocation_result_ns,
3297 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3298 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3300 * If we have found some records for the LAST label
3301 * we return the results. Else null.
3303 if (strcmp (rh->name, "") == 0)
3305 /* Start shortening */
3306 if ((rh->priv_key != NULL) &&
3307 (is_canonical (rh->name) == GNUNET_YES))
3309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3310 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3312 start_shorten (rh->authority_chain_head,
3315 /* simply promote back */
3316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3317 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3318 rh->id, rh->authority_name);
3319 strcpy (rh->name, rh->authority_name);
3320 rh->proc (rh->proc_cls, rh, rd_count, rd);
3324 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
3325 "%s.%s", rh->name, rh->authority_name);
3326 strcpy (rh->name, new_name);
3327 rh->proc (rh->proc_cls, rh, 0, NULL);
3333 * Resolve the delegation chain for the request in our namestore
3335 * @param rh the resolver handle
3338 resolve_delegation_ns (struct ResolverHandle *rh)
3340 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3341 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3343 pop_tld(rh->name, rh->authority_name);
3344 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3347 GNUNET_GNS_RECORD_ANY,
3348 &process_delegation_result_ns,
3355 * Lookup of a record in a specific zone
3356 * calls lookup result processor on result
3358 * @param zone the root zone
3359 * @param pzone the private local zone
3360 * @param record_type the record type to look up
3361 * @param name the name to look up
3362 * @param key a private key for use with PSEU import (can be NULL)
3363 * @param timeout timeout for resolution
3364 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3365 * @param proc the processor to call on result
3366 * @param cls the closure to pass to proc
3369 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3370 struct GNUNET_CRYPTO_ShortHashCode pzone,
3371 uint32_t record_type,
3373 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3374 struct GNUNET_TIME_Relative timeout,
3376 RecordLookupProcessor proc,
3379 struct ResolverHandle *rh;
3380 struct RecordLookupHandle* rlh;
3381 char string_hash[MAX_DNS_LABEL_LENGTH];
3382 char nzkey[MAX_DNS_LABEL_LENGTH];
3383 char* nzkey_ptr = nzkey;
3385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3386 "Starting resolution for %s (type=%d)!\n",
3390 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3391 (strcmp(GNUNET_GNS_TLD, name) != 0))
3393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3394 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3399 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3400 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3402 memset (rh, 0, sizeof (struct ResolverHandle));
3403 rh->authority = zone;
3407 rh->timeout = timeout;
3408 rh->get_handle = NULL;
3409 rh->private_local_zone = pzone;
3410 rh->only_cached = only_cached;
3411 rh->namestore_task = NULL;
3414 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3419 "No shorten key for resolution\n");
3422 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3425 * Set timeout for authority lookup phase to 1/2
3427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3428 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3429 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3430 GNUNET_TIME_relative_divide(timeout, 2),
3431 &handle_lookup_timeout,
3433 rh->timeout_cont = &dht_authority_lookup_timeout;
3434 rh->timeout_cont_cls = rh;
3438 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3439 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3442 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3445 * Only 'gnunet' given
3447 strcpy(rh->name, "\0");
3451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3452 "Checking for TLD...\n");
3453 if (is_zkey_tld(name) == GNUNET_YES)
3455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3458 * This is a zkey tld
3459 * build hash and use as initial authority
3462 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3463 memcpy(rh->name, name,
3464 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3465 pop_tld(rh->name, string_hash);
3467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3468 "ZKEY is %s!\n", string_hash);
3470 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3472 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3475 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3476 "Cannot convert ZKEY %s to hash!\n", string_hash);
3478 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3481 proc (cls, 0, NULL);
3486 else if (is_gnunet_tld (name) == GNUNET_YES)
3488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3491 * Presumably GNUNET tld
3493 memset (rh->name, 0,
3494 strlen(name)-strlen(GNUNET_GNS_TLD));
3495 memcpy (rh->name, name,
3496 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3501 "Cannot handle this TLD %s\n", string_hash);
3503 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3504 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3505 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3508 proc (cls, 0, NULL);
3514 * Initialize authority chain
3516 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3517 rh->authority_chain_head->prev = NULL;
3518 rh->authority_chain_head->next = NULL;
3519 rh->authority_chain_tail = rh->authority_chain_head;
3520 rh->authority_chain_head->zone = rh->authority;
3521 strcpy (rh->authority_chain_head->name, "");
3524 * Copy original query into lookup handle
3526 rlh->record_type = record_type;
3527 memset(rlh->name, 0, strlen(name) + 1);
3528 strcpy(rlh->name, name);
3530 rlh->proc_cls = cls;
3532 rh->proc = &handle_delegation_ns;
3533 resolve_delegation_ns(rh);
3536 /******** END Record Resolver ***********/
3539 finish_shorten (struct ResolverHandle *rh,
3540 struct NameShortenHandle *nsh)
3542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3543 "Sending %s as shorten result\n", nsh->result);
3544 nsh->proc (nsh->proc_cls, nsh->result);
3545 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3547 free_resolver_handle (rh);
3552 * Callback calles by namestore for a zone to name
3555 * @param cls the closure
3556 * @param zone_key the zone we queried
3557 * @param expire the expiration time of the name
3558 * @param name the name found or NULL
3559 * @param rd_len number of records for the name
3560 * @param rd the record data (PKEY) for the name
3561 * @param signature the signature for the record data
3564 process_zone_to_name_shorten_root (void *cls,
3565 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3566 struct GNUNET_TIME_Absolute expire,
3568 unsigned int rd_len,
3569 const struct GNUNET_NAMESTORE_RecordData *rd,
3570 const struct GNUNET_CRYPTO_RsaSignature *signature);
3574 * Callback called by namestore for a zone to name
3577 * @param cls the closure
3578 * @param zone_key the zone we queried
3579 * @param expire the expiration time of the name
3580 * @param name the name found or NULL
3581 * @param rd_len number of records for the name
3582 * @param rd the record data (PKEY) for the name
3583 * @param signature the signature for the record data
3586 process_zone_to_name_shorten_shorten (void *cls,
3587 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3588 struct GNUNET_TIME_Absolute expire,
3590 unsigned int rd_len,
3591 const struct GNUNET_NAMESTORE_RecordData *rd,
3592 const struct GNUNET_CRYPTO_RsaSignature *signature)
3594 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3595 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3596 struct AuthorityChain *next_authority;
3598 char result[MAX_DNS_NAME_LENGTH];
3599 char tmp_name[MAX_DNS_NAME_LENGTH];
3602 rh->namestore_task = NULL;
3603 /* we found a match in our own root zone */
3606 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3607 memset(result, 0, answer_len);
3609 if (strlen(rh->name) > 0)
3611 sprintf (result, "%s.%s.%s.%s",
3613 nsh->shorten_zone_name,
3618 sprintf (result, "%s.%s.%s", name,
3619 nsh->shorten_zone_name,
3623 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3624 "Found shorten result %s\n", result);
3625 if (strlen (nsh->result) > strlen (result))
3626 strcpy (nsh->result, result);
3628 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3629 nsh->shorten_zone) == 0)
3632 * This is our zone append .gnunet unless name is empty
3633 * (it shouldn't be, usually FIXME what happens if we
3634 * shorten to our zone to a "" record??)
3637 sprintf (result, "%s.%s.%s",
3639 nsh->shorten_zone_name,
3641 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3642 "Our zone: Found %s as shorten result\n", result);
3644 if (strlen (nsh->result) > strlen (result))
3645 strcpy (nsh->result, result);
3646 //nsh->proc(nsh->proc_cls, result);
3648 //free_resolver_handle(rh);
3655 * continue with next authority if exists
3657 if ((rh->authority_chain_head->next == NULL))
3659 finish_shorten (rh, nsh);
3662 next_authority = rh->authority_chain_head;
3664 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3665 "%s.%s", rh->name, next_authority->name);
3667 strcpy(rh->name, tmp_name);
3668 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3669 "No PSEU found for authority %s. Promoting back: %s\n",
3670 next_authority->name, rh->name);
3672 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3673 rh->authority_chain_tail,
3676 GNUNET_free (next_authority);
3678 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3679 &rh->authority_chain_tail->zone,
3680 &rh->authority_chain_head->zone,
3681 &process_zone_to_name_shorten_root,
3686 * Callback calles by namestore for a zone to name
3689 * @param cls the closure
3690 * @param zone_key the zone we queried
3691 * @param expire the expiration time of the name
3692 * @param name the name found or NULL
3693 * @param rd_len number of records for the name
3694 * @param rd the record data (PKEY) for the name
3695 * @param signature the signature for the record data
3698 process_zone_to_name_shorten_private (void *cls,
3699 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3700 struct GNUNET_TIME_Absolute expire,
3702 unsigned int rd_len,
3703 const struct GNUNET_NAMESTORE_RecordData *rd,
3704 const struct GNUNET_CRYPTO_RsaSignature *signature)
3706 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3707 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3708 struct AuthorityChain *next_authority;
3710 char result[MAX_DNS_NAME_LENGTH];
3711 char tmp_name[MAX_DNS_NAME_LENGTH];
3714 rh->namestore_task = NULL;
3715 /* we found a match in our own root zone */
3718 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3719 memset(result, 0, answer_len);
3721 if (strlen(rh->name) > 0)
3723 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3727 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3730 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3731 "Found shorten result %s\n", result);
3732 if (strlen (nsh->result) > strlen (result))
3733 strcpy (nsh->result, result);
3735 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3736 nsh->private_zone) == 0)
3739 * This is our zone append .gnunet unless name is empty
3740 * (it shouldn't be, usually FIXME what happens if we
3741 * shorten to our zone to a "" record??)
3744 sprintf (result, "%s.%s.%s",
3745 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3746 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3747 "Our private zone: Found %s as shorten result %s\n", result);
3748 if (strlen (nsh->result) > strlen (result))
3749 strcpy (nsh->result, result);
3752 if (0 != strcmp (nsh->shorten_zone_name, ""))
3754 /* backtrack authorities for names in priv zone */
3755 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3757 &rh->authority_chain_head->zone,
3758 &process_zone_to_name_shorten_shorten,
3765 * continue with next authority if exists
3767 if ((rh->authority_chain_head->next == NULL))
3769 finish_shorten (rh, nsh);
3772 next_authority = rh->authority_chain_head;
3774 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3775 "%s.%s", rh->name, next_authority->name);
3777 strcpy(rh->name, tmp_name);
3778 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3779 "No PSEU found for authority %s. Promoting back: %s\n",
3780 next_authority->name, rh->name);
3782 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3783 rh->authority_chain_tail,
3786 GNUNET_free (next_authority);
3788 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3789 &rh->authority_chain_tail->zone,
3790 &rh->authority_chain_head->zone,
3791 &process_zone_to_name_shorten_root,
3797 * Callback calles by namestore for a zone to name
3800 * @param cls the closure
3801 * @param zone_key the zone we queried
3802 * @param expire the expiration time of the name
3803 * @param name the name found or NULL
3804 * @param rd_len number of records for the name
3805 * @param rd the record data (PKEY) for the name
3806 * @param signature the signature for the record data
3809 process_zone_to_name_shorten_root (void *cls,
3810 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3811 struct GNUNET_TIME_Absolute expire,
3813 unsigned int rd_len,
3814 const struct GNUNET_NAMESTORE_RecordData *rd,
3815 const struct GNUNET_CRYPTO_RsaSignature *signature)
3817 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3818 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3819 struct AuthorityChain *next_authority;
3821 char result[MAX_DNS_NAME_LENGTH];
3822 char tmp_name[MAX_DNS_NAME_LENGTH];
3825 rh->namestore_task = NULL;
3826 /* we found a match in our own root zone */
3829 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3830 memset(result, 0, answer_len);
3832 if (strlen(rh->name) > 0)
3834 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3838 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3841 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3842 "Found shorten result %s\n", result);
3843 if (strlen (nsh->result) > strlen (result))
3844 strcpy (nsh->result, result);
3846 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3847 nsh->root_zone) == 0)
3850 * This is our zone append .gnunet unless name is empty
3851 * (it shouldn't be, usually FIXME what happens if we
3852 * shorten to our zone to a "" record??)
3855 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3856 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3857 "Our zone: Found %s as shorten result\n", result);
3858 if (strlen (nsh->result) > strlen (result))
3859 strcpy (nsh->result, result);
3862 if (NULL != nsh->private_zone)
3864 /* backtrack authorities for names in priv zone */
3865 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3867 &rh->authority_chain_head->zone,
3868 &process_zone_to_name_shorten_private,
3871 else if (NULL != nsh->shorten_zone)
3873 /* backtrack authorities for names in shorten zone */
3874 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3876 &rh->authority_chain_head->zone,
3877 &process_zone_to_name_shorten_shorten,
3884 * continue with next authority if exists
3886 if ((rh->authority_chain_head->next == NULL))
3888 finish_shorten (rh, nsh);
3891 next_authority = rh->authority_chain_head;
3893 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3894 "%s.%s", rh->name, next_authority->name);
3896 strcpy(rh->name, tmp_name);
3897 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3898 "No PSEU found for authority %s. Promoting back: %s\n",
3899 next_authority->name, rh->name);
3901 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3902 rh->authority_chain_tail,
3905 GNUNET_free (next_authority);
3907 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3908 &rh->authority_chain_tail->zone,
3909 &rh->authority_chain_head->zone,
3910 &process_zone_to_name_shorten_root,
3917 * Process result from namestore delegation lookup
3918 * for shorten operation
3920 * @param cls the client shorten handle
3921 * @param rh the resolver handle
3922 * @param rd_count number of results (0)
3923 * @param rd data (NULL)
3926 handle_delegation_ns_shorten (void* cls,
3927 struct ResolverHandle *rh,
3929 const struct GNUNET_NAMESTORE_RecordData *rd)
3931 struct NameShortenHandle *nsh;
3932 char result[MAX_DNS_NAME_LENGTH];
3934 nsh = (struct NameShortenHandle *)cls;
3937 * At this point rh->name contains the part of the name
3938 * that we do not have a PKEY in our namestore to resolve.
3939 * The authority chain in the resolver handle is now
3940 * useful to backtrack if needed
3943 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3944 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3945 memset(result, 0, sizeof (result));
3947 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3948 nsh->root_zone) == 0)
3951 * This is our zone append .gnunet unless name is empty
3952 * (it shouldn't be, usually FIXME what happens if we
3953 * shorten to our zone to a "" record??)
3956 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3957 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3958 "Our zone: Found %s as shorten result\n", result);
3960 if (strlen (nsh->result) > strlen (result))
3961 strcpy (nsh->result, result);
3964 else if (NULL != nsh->private_zone)
3967 * This is our zone append .gnunet unless name is empty
3968 * (it shouldn't be, usually FIXME what happens if we
3969 * shorten to our zone to a "" record??)
3971 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3972 nsh->private_zone) == 0)
3975 sprintf (result, "%s.%s.%s",
3976 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3977 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3978 "Our zone: Found %s as shorten result in private zone %s\n",
3981 if (strlen (nsh->result) > strlen (result))
3982 strcpy (nsh->result, result);
3985 else if (NULL != nsh->shorten_zone)
3988 * This is our zone append .gnunet unless name is empty
3989 * (it shouldn't be, usually FIXME what happens if we
3990 * shorten to our zone to a "" record??)
3992 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3993 nsh->shorten_zone) == 0)
3995 sprintf (result, "%s.%s.%s",
3996 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3997 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3998 "Our zone: Found %s as shorten result in shorten zone\n",
4001 if (strlen (nsh->result) > strlen (result))
4002 strcpy (nsh->result, result);
4007 /* backtrack authorities for names */
4008 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4010 &rh->authority_chain_head->zone,
4011 &process_zone_to_name_shorten_root,
4018 * Callback calles by namestore for a zone to name
4021 * @param cls the closure
4022 * @param zone_key the zone we queried
4023 * @param expire the expiration time of the name
4024 * @param name the name found or NULL
4025 * @param rd_len number of records for the name
4026 * @param rd the record data (PKEY) for the name
4027 * @param signature the signature for the record data
4030 process_zone_to_name_zkey(void *cls,
4031 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4032 struct GNUNET_TIME_Absolute expire,
4034 unsigned int rd_len,
4035 const struct GNUNET_NAMESTORE_RecordData *rd,
4036 const struct GNUNET_CRYPTO_RsaSignature *signature)
4038 struct ResolverHandle *rh = cls;
4039 struct NameShortenHandle *nsh = rh->proc_cls;
4040 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
4041 char new_name[MAX_DNS_NAME_LENGTH];
4043 /* zkey not in our zone */
4047 * In this case we have not given this PKEY a name (yet)
4048 * It is either just not in our zone or not even cached
4049 * Since we do not know at this point we will not try to shorten
4050 * because PKEY import will happen if the user follows the zkey
4053 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
4055 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4056 "No name found for zkey %s returning verbatim!\n", enc);
4057 if (strcmp(rh->name, "") != 0)
4058 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4059 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4061 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4062 enc, GNUNET_GNS_TLD_ZKEY);
4064 strcpy (nsh->result, new_name);
4066 finish_shorten (rh, nsh);
4070 if (strcmp(rh->name, "") != 0)
4071 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4074 strcpy(new_name, name);
4076 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4077 "Continue shorten for %s!\n", new_name);
4079 strcpy(rh->name, new_name);
4081 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4082 rh->authority_chain_tail = rh->authority_chain_head;
4083 rh->authority_chain_head->zone = rh->authority;
4086 /* Start delegation resolution in our namestore */
4087 resolve_delegation_ns (rh);
4092 * Shorten api from resolver
4094 * @param zone the root zone to use
4095 * @param pzone the private zone to use
4096 * @param szone the shorten zone to use
4097 * @param name the name to shorten
4098 * @param private_zone_name name of the private zone
4099 * @param shorten_zone_name name of the shorten zone
4100 * @param proc the processor to call with result
4101 * @param proc_cls closure to pass to proc
4104 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4105 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4106 struct GNUNET_CRYPTO_ShortHashCode *szone,
4108 const char* private_zone_name,
4109 const char* shorten_zone_name,
4110 ShortenResultProcessor proc,
4113 struct ResolverHandle *rh;
4114 struct NameShortenHandle *nsh;
4115 char string_hash[MAX_DNS_LABEL_LENGTH];
4116 struct GNUNET_CRYPTO_ShortHashCode zkey;
4117 char nzkey[MAX_DNS_LABEL_LENGTH];
4118 char* nzkey_ptr = nzkey;
4121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4122 "Starting shorten for %s!\n", name);
4124 if (is_canonical ((char*)name) == GNUNET_YES)
4126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4127 "%s is canonical. Returning verbatim\n", name);
4128 proc (proc_cls, name);
4132 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4135 nsh->proc_cls = proc_cls;
4136 nsh->root_zone = zone;
4137 nsh->private_zone = pzone;
4138 nsh->shorten_zone = szone;
4139 strcpy (nsh->private_zone_name, private_zone_name);
4140 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4141 strcpy (nsh->result, name);
4144 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4145 rh->authority = *zone;
4147 rh->priv_key = NULL;
4148 rh->namestore_task = NULL;
4149 rh->proc = &handle_delegation_ns_shorten;
4152 rh->private_local_zone = *zone;
4154 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4157 "Checking for TLD...\n");
4158 if (is_zkey_tld (name) == GNUNET_YES)
4160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4163 * This is a zkey tld
4164 * build hash and use as initial authority
4167 memset (rh->name, 0,
4168 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4169 memcpy (rh->name, name,
4170 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4171 pop_tld (rh->name, string_hash);
4173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4174 "ZKEY is %s!\n", string_hash);
4176 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4178 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4182 "Cannot convert ZKEY %s to hash!\n", nzkey);
4185 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4186 proc (proc_cls, name);
4190 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4193 &process_zone_to_name_zkey,
4198 else if (is_gnunet_tld (name) == GNUNET_YES)
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4203 * Presumably GNUNET tld
4205 memset (rh->name, 0,
4206 strlen (name)-strlen (GNUNET_GNS_TLD));
4207 memcpy (rh->name, name,
4208 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4215 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4216 proc (proc_cls, name);
4220 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4221 rh->authority_chain_tail = rh->authority_chain_head;
4222 rh->authority_chain_head->zone = *zone;
4225 /* Start delegation resolution in our namestore */
4226 resolve_delegation_ns (rh);
4229 /*********** END NAME SHORTEN ********************/
4232 * Conclude get authority lookup
4234 * @param rh resolver handle
4235 * @param nah get authority lookup handle
4238 finish_get_auth (struct ResolverHandle *rh,
4239 struct GetNameAuthorityHandle *nah)
4241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4242 "Got authority result %s\n", nah->result);
4244 nah->proc(nah->proc_cls, nah->result);
4245 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4247 free_resolver_handle(rh);
4251 * Process result from namestore delegation lookup
4252 * for get authority operation
4254 * @param cls the client get auth handle
4255 * @param rh the resolver handle
4256 * @param rd_count number of results (0)
4257 * @param rd data (NULL)
4260 handle_delegation_result_ns_get_auth(void* cls,
4261 struct ResolverHandle *rh,
4263 const struct GNUNET_NAMESTORE_RecordData *rd)
4265 struct GetNameAuthorityHandle* nah;
4268 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4271 * At this point rh->name contains the part of the name
4272 * that we do not have a PKEY in our namestore to resolve.
4273 * The authority chain in the resolver handle is now
4274 * useful to backtrack if needed
4277 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4278 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4280 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4281 "Building response!\n");
4282 if (is_canonical (rh->name) == GNUNET_YES)
4285 * We successfully resolved the authority in the ns
4286 * FIXME for our purposes this is fine
4287 * but maybe we want to have an api that also looks
4288 * into the dht (i.e. option in message)
4290 if (strlen(rh->name) > strlen(nah->name))
4292 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4293 "Record name longer than original lookup name... odd!\n");
4297 answer_len = strlen(nah->name) - strlen(rh->name)
4298 + strlen(GNUNET_GNS_TLD) + 1;
4299 memset(nah->result, 0, answer_len);
4300 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4302 finish_get_auth (rh, nah);
4306 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4307 "Unable to resolve authority for remaining %s!\n", rh->name);
4308 strcpy(nah->result, "");
4309 finish_get_auth (rh, nah);
4317 * Tries to resolve the authority for name
4320 * @param zone the root zone to look up for
4321 * @param pzone the private local zone
4322 * @param name the name to lookup up
4323 * @param proc the processor to call when finished
4324 * @param proc_cls the closure to pass to the processor
4327 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4328 struct GNUNET_CRYPTO_ShortHashCode pzone,
4330 GetAuthorityResultProcessor proc,
4333 struct ResolverHandle *rh;
4334 struct GetNameAuthorityHandle *nah;
4336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4337 "Starting authority resolution for %s!\n", name);
4339 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4340 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4341 rh->authority = zone;
4343 rh->private_local_zone = pzone;
4344 rh->namestore_task = NULL;
4346 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4348 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4350 strcpy(rh->name, "\0");
4355 strlen(name)-strlen(GNUNET_GNS_TLD));
4356 memcpy(rh->name, name,
4357 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4360 memset(nah->name, 0,
4362 strcpy(nah->name, name);
4364 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4365 rh->authority_chain_tail = rh->authority_chain_head;
4366 rh->authority_chain_head->zone = zone;
4367 rh->proc = &handle_delegation_result_ns_get_auth;
4368 rh->proc_cls = (void*)nah;
4371 nah->proc_cls = proc_cls;
4372 strcpy (nah->result, "");
4374 /* Start delegation resolution in our namestore */
4375 resolve_delegation_ns(rh);
4379 /******** END GET AUTHORITY *************/
4381 /* end of gnunet-service-gns_resolver.c */