2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011, 2012, 2013 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
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_dns_service.h"
31 #include "gnunet_dht_service.h"
32 #include "gnunet_namestore_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 "gns_common.h"
39 #include "block_gns.h"
41 #include "gnunet-service-gns_resolver.h"
43 #include "gnunet_vpn_service.h"
50 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
53 * DHT replication level
55 #define DHT_GNS_REPLICATION_LEVEL 5
59 * Our handle to the namestore service
61 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
65 * Our handle to the vpn service
67 static struct GNUNET_VPN_Handle *vpn_handle;
71 * Resolver handle to the dht
73 static struct GNUNET_DHT_Handle *dht_handle;
76 * Heap for parallel DHT lookups
78 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
81 * Heap for namestore queues
83 static struct GNUNET_CONTAINER_Heap *ns_task_heap;
86 * Maximum amount of parallel queries in background
88 static unsigned long long max_allowed_background_queries;
91 * Maximum amount of parallel namestore tasks in background
93 static unsigned long long max_allowed_ns_tasks;
96 * Wheather or not to ignore pending records
98 static int ignore_pending_records;
103 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
106 * Background shortening handles
108 static struct GetPseuAuthorityHandle *gph_head;
111 * Background shortening handles
113 static struct GetPseuAuthorityHandle *gph_tail;
116 * Resolver lookup list
118 static struct ResolverHandle *rlh_head;
121 * Resolver lookup list
123 static struct ResolverHandle *rlh_tail;
126 * Resolver shorten list
128 static struct ResolverHandle *nsh_head;
131 * Resolver shorten list
133 static struct ResolverHandle *nsh_tail;
136 * Resolver get auth list
138 static struct ResolverHandle *nah_head;
141 * Resolver get auth list
143 static struct ResolverHandle *nah_tail;
146 * Global configuration.
148 static const struct GNUNET_CONFIGURATION_Handle *cfg;
151 * a resolution identifier pool variable
152 * This is a non critical identifier useful for debugging
154 static unsigned long long rid_gen;
158 * Check if name is in srv format (_x._y.xxx)
161 * @return GNUNET_YES if true
164 is_srv (const char *name)
171 if (NULL == strstr (name, "._"))
174 ndup = GNUNET_strdup (name);
176 if (NULL == strtok (NULL, "."))
178 if (NULL == strtok (NULL, "."))
180 if (NULL != strtok (NULL, "."))
188 * Determine if this name is canonical (is a legal name in a zone, without delegation);
189 * note that we do not test that the name does not contain illegal characters, we only
190 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
191 * even though they consist of multiple labels.
194 * a.b.gads = not canonical
196 * _foo._srv = canonical
197 * _f.bar = not canonical
199 * @param name the name to test
200 * @return GNUNET_YES if canonical
203 is_canonical (const char *name)
208 if (NULL == strchr (name, '.'))
213 while (NULL != (dot = strchr (pos, '.')))
223 free_get_pseu_authority_handle (struct GetPseuAuthorityHandle *gph)
225 gph->namestore_task = NULL;
226 GNUNET_free (gph->auth);
227 GNUNET_CRYPTO_ecc_key_free (gph->key);
228 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
234 * Callback that shortens authorities
236 * @param gph the handle containing the name to shorten
239 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
243 * Continuation for pkey record creation (shorten)
245 * @param cls a GetPseuAuthorityHandle
246 * @param success unused
250 create_pkey_cont (void* cls, int32_t success, const char* emsg)
252 //FIXME do sth with error
253 struct GetPseuAuthorityHandle* gph = cls;
255 free_get_pseu_authority_handle (gph);
260 * Namestore calls this function if we have record for this name.
261 * (or with rd_count=0 to indicate no matches)
263 * @param cls the pending query
264 * @param key the key of the zone we did the lookup
265 * @param expiration expiration date of the namestore entry
266 * @param name the name for which we need an authority
267 * @param rd_count the number of records with 'name'
268 * @param rd the record data
269 * @param signature the signature of the authority for the record data
272 process_pseu_lookup_ns (void* cls,
273 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *key,
274 struct GNUNET_TIME_Absolute expiration,
275 const char *name, unsigned int rd_count,
276 const struct GNUNET_NAMESTORE_RecordData *rd,
277 const struct GNUNET_CRYPTO_EccSignature *signature)
279 struct GetPseuAuthorityHandle* gph = cls;
280 struct GNUNET_NAMESTORE_RecordData new_pkey;
282 gph->namestore_task = NULL;
285 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
286 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
287 free_get_pseu_authority_handle (gph);
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n",
296 new_pkey.expiration_time = UINT64_MAX;
297 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
298 new_pkey.data = &gph->auth->zone;
299 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
300 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
301 | GNUNET_NAMESTORE_RF_PRIVATE
302 | GNUNET_NAMESTORE_RF_PENDING;
303 gph->namestore_task = GNUNET_NAMESTORE_record_put_by_authority (namestore_handle,
314 * process result of a dht pseu lookup
316 * @param gph the handle
317 * @param name the pseu result or NULL
320 process_pseu_result (struct GetPseuAuthorityHandle* gph,
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "GNS_AUTO_PSEU: No PSEU, no shorten. Finished.\n");
327 free_get_pseu_authority_handle (gph);
331 memcpy (gph->test_name, name, strlen(name) + 1);
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
336 * Check for collision
338 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
341 GNUNET_NAMESTORE_TYPE_ANY,
342 &process_pseu_lookup_ns,
348 * Handle timeout for dht request
350 * @param cls the request handle as closure
351 * @param tc the task context
354 handle_auth_discovery_timeout (void *cls,
355 const struct GNUNET_SCHEDULER_TaskContext *tc)
357 struct GetPseuAuthorityHandle* gph = cls;
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
361 GNUNET_DHT_get_stop (gph->get_handle);
362 gph->get_handle = NULL;
363 process_pseu_result (gph, NULL);
368 * Function called when we find a PSEU entry in the DHT
370 * @param cls the request handle
371 * @param exp lifetime
372 * @param key the key the record was stored under
373 * @param get_path get path
374 * @param get_path_length get path length
375 * @param put_path put path
376 * @param put_path_length put path length
377 * @param type the block type
378 * @param size the size of the record
379 * @param data the record data
382 process_auth_discovery_dht_result (void* cls,
383 struct GNUNET_TIME_Absolute exp,
384 const struct GNUNET_HashCode * key,
385 const struct GNUNET_PeerIdentity *get_path,
386 unsigned int get_path_length,
387 const struct GNUNET_PeerIdentity *put_path,
388 unsigned int put_path_length,
389 enum GNUNET_BLOCK_Type type,
393 struct GetPseuAuthorityHandle* gph = cls;
394 struct GNSNameRecordBlock *nrb;
395 const char* rd_data = data;
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
404 /* stop lookup and timeout task */
405 GNUNET_DHT_get_stop (gph->get_handle);
406 gph->get_handle = NULL;
407 GNUNET_SCHEDULER_cancel (gph->timeout);
411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
412 "GNS_GET_AUTH: got dht result null!\n", size);
413 free_get_pseu_authority_handle (gph);
417 nrb = (struct GNSNameRecordBlock*)data;
418 name = (char*)&nrb[1];
419 num_records = ntohl (nrb->rd_count);
421 struct GNUNET_NAMESTORE_RecordData rd[num_records];
423 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
424 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
426 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
432 "GNS_GET_AUTH: Error deserializing data!\n");
436 for (i=0; i < num_records; i++)
438 if ((strcmp (name, GNUNET_GNS_MASTERZONE_STR) == 0) &&
439 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
442 process_pseu_result (gph, (char*)rd[i].data);
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
449 "GNS_GET_AUTH: finished shorten, no results!\n");
450 process_pseu_result (gph, NULL);
455 * Process PSEU discovery for shorten via namestore
457 * @param cls the GetPseuAuthorityHandle
458 * @param key the public key
459 * @param expiration recorddata expiration
460 * @param name the looked up name
461 * @param rd_count number of records in set
462 * @param rd record data
463 * @param signature the signature
466 process_auth_discovery_ns_result (void* cls,
467 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *key,
468 struct GNUNET_TIME_Absolute expiration,
470 unsigned int rd_count,
471 const struct GNUNET_NAMESTORE_RecordData *rd,
472 const struct GNUNET_CRYPTO_EccSignature *signature)
474 struct GetPseuAuthorityHandle* gph = cls;
475 struct GNUNET_HashCode lookup_key;
479 gph->namestore_task = NULL;
483 GNUNET_GNS_get_key_for_record (GNUNET_GNS_TLD_PLUS, &gph->auth->zone, &lookup_key);
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
487 GNUNET_h2s (&lookup_key));
489 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
490 &handle_auth_discovery_timeout, gph);
492 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
494 GNUNET_assert (gph->get_handle == NULL);
496 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
497 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
499 DHT_GNS_REPLICATION_LEVEL,
500 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
503 &process_auth_discovery_dht_result,
508 for (i=0; i < rd_count; i++)
510 if (0 != (strcmp (name, GNUNET_GNS_TLD_PLUS)))
513 if (rd[i].record_type != GNUNET_GNS_RECORD_PSEU)
517 process_pseu_result (gph, (char*)rd[i].data);
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
522 process_pseu_result (gph, NULL);
527 * Callback called by namestore for a zone to name
530 * @param cls the closure
531 * @param zone_key the zone we queried
532 * @param expire the expiration time of the name
533 * @param name the name found or NULL
534 * @param rd_len number of records for the name
535 * @param rd the record data (PKEY) for the name
536 * @param signature the signature for the record data
539 process_zone_to_name_discover (void *cls,
540 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
541 struct GNUNET_TIME_Absolute expire,
544 const struct GNUNET_NAMESTORE_RecordData *rd,
545 const struct GNUNET_CRYPTO_EccSignature *signature)
547 struct GetPseuAuthorityHandle* gph = cls;
549 gph->namestore_task = NULL;
552 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
554 GNUNET_GNS_MASTERZONE_STR,
555 GNUNET_GNS_RECORD_PSEU,
556 &process_auth_discovery_ns_result,
560 /* we found a match in our own zone */
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
563 free_get_pseu_authority_handle (gph);
568 * Callback that shortens authorities
570 * @param gph the handle to the shorten request
573 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576 "GNS_AUTO_PSEU: New authority %s discovered\n",
579 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
582 &process_zone_to_name_discover,
588 * Start shortening algorithm using auth as
591 * @param auth the authorities that were resolved
592 * @param key the private key for PKEY import
595 start_shorten (struct AuthorityChain *auth,
596 const struct GNUNET_CRYPTO_EccPrivateKey *key)
598 struct GetPseuAuthorityHandle *gph;
599 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pkey;
600 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *pb_key;
602 GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
603 if (NULL == (pb_key = GNUNET_CRYPTO_ecc_encode_key (key)))
605 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
606 "Failed to encode ECC key on shorten\n");
609 gph = GNUNET_new (struct GetPseuAuthorityHandle);
610 gph->key = GNUNET_CRYPTO_ecc_decode_key ((const char*) pb_key,
611 ntohs (pb_key->size),
613 GNUNET_free (pb_key);
614 if (NULL == gph->key)
616 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
617 "Failed to decode ECC key on shorten\n");
621 GNUNET_CRYPTO_short_hash (&pkey,
622 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
624 gph->auth = GNUNET_malloc (sizeof (struct AuthorityChain));
625 memcpy (gph->auth, auth, sizeof (struct AuthorityChain));
626 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
627 shorten_authority_chain (gph);
632 * Initialize the resolver
634 * @param nh the namestore handle
635 * @param dh the dht handle
636 * @param lz the local zone's hash
637 * @param c configuration handle
638 * @param max_bg_queries maximum number of parallel background queries in dht
639 * @param ignore_pending ignore records that still require user confirmation
641 * @return GNUNET_OK on success
644 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
645 struct GNUNET_DHT_Handle *dh,
646 struct GNUNET_CRYPTO_ShortHashCode lz,
647 const struct GNUNET_CONFIGURATION_Handle *c,
648 unsigned long long max_bg_queries,
653 return GNUNET_SYSERR;
656 namestore_handle = nh;
660 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
662 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
663 max_allowed_background_queries = max_bg_queries;
664 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
665 ignore_pending_records = ignore_pending;
666 GNUNET_RESOLVER_connect (cfg);
673 * @param rh resolver handle
674 * @param rlh record lookup handle
675 * @param rd_count number of results
679 finish_lookup (struct ResolverHandle *rh,
680 struct RecordLookupHandle* rlh,
681 unsigned int rd_count,
682 const struct GNUNET_NAMESTORE_RecordData *rd);
686 * Helper function to free resolver handle
688 * @param rh the handle to free
691 free_resolver_handle (struct ResolverHandle* rh)
693 struct AuthorityChain *ac;
694 struct AuthorityChain *ac_next;
699 ac_next = rh->authority_chain_head;
700 while (NULL != (ac = ac_next))
706 if (NULL != rh->get_handle)
707 GNUNET_DHT_get_stop (rh->get_handle);
708 if (NULL != rh->dns_raw_packet)
709 GNUNET_free (rh->dns_raw_packet);
710 if (NULL != rh->namestore_task)
712 GNUNET_NAMESTORE_cancel (rh->namestore_task);
713 rh->namestore_task = NULL;
715 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
716 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
717 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
718 GNUNET_SCHEDULER_cancel (rh->timeout_task);
719 if (NULL != rh->dns_sock)
720 GNUNET_NETWORK_socket_close (rh->dns_sock);
721 if (NULL != rh->dns_resolver_handle)
722 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
723 if (NULL != rh->rd.data)
724 GNUNET_free ((void*)(rh->rd.data));
725 if (NULL != rh->dht_heap_node)
726 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
734 * @param rh resolver handle
735 * @param nsh name shorten handle
738 finish_shorten (struct ResolverHandle *rh,
739 struct NameShortenHandle *nsh);
745 * @param rh resolver handle
746 * @param nah get name authority handle
749 finish_get_auth (struct ResolverHandle *rh,
750 struct GetNameAuthorityHandle* rlh);
757 gns_resolver_cleanup ()
759 struct GetPseuAuthorityHandle *tmp;
760 struct ResolverHandle *rh;
761 struct NamestoreBGTask *nbg;
763 while (NULL != (tmp = gph_head))
765 if (tmp->get_handle != NULL)
767 GNUNET_DHT_get_stop (tmp->get_handle);
768 tmp->get_handle = NULL;
770 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
772 GNUNET_SCHEDULER_cancel (tmp->timeout);
773 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
775 if (NULL != tmp->namestore_task)
777 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
778 tmp->namestore_task = NULL;
780 free_get_pseu_authority_handle (tmp);
783 while (NULL != rlh_head)
785 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
787 while (NULL != nsh_head)
789 finish_shorten (nsh_head, nsh_head->proc_cls);
791 while (NULL != nah_head)
793 finish_get_auth (nah_head, nah_head->proc_cls);
796 while (NULL != (rh = GNUNET_CONTAINER_heap_remove_root(dht_lookup_heap)))
800 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
801 dht_lookup_heap = NULL;
803 while (NULL != (nbg = GNUNET_CONTAINER_heap_remove_root(ns_task_heap)))
805 GNUNET_NAMESTORE_cancel (nbg->qe);
808 GNUNET_CONTAINER_heap_destroy (ns_task_heap);
815 * Callback when record data is put into namestore
817 * @param cls the closure
818 * @param success GNUNET_OK on success
819 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
822 on_namestore_record_put_result (void *cls,
826 struct NamestoreBGTask *nbg = cls;
828 GNUNET_CONTAINER_heap_remove_node (nbg->node);
831 if (GNUNET_NO == success)
833 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
834 "GNS_NS: records already in namestore\n");
837 else if (GNUNET_YES == success)
839 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
840 "GNS_NS: records successfully put in namestore\n");
844 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
845 "GNS_NS: Error putting records into namestore: %s\n", emsg);
850 * Lookup timeout task
852 * @param cls the ResolverHandle for the task that timed out
853 * @param tc the task context
856 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
858 struct ResolverHandle *rh = cls;
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "Lookup timeout for request %llu triggered\n",
863 if (NULL != rh->timeout_cont)
864 rh->timeout_cont (rh->timeout_cont_cls, tc);
869 * Processor for background lookups in the DHT
871 * @param cls closure (NULL)
872 * @param rd_count number of records found (not 0)
873 * @param rd record data
876 background_lookup_result_processor (void *cls,
878 const struct GNUNET_NAMESTORE_RecordData *rd)
880 //We could do sth verbose/more useful here but it doesn't make any difference
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
882 "GNS_BG: background dht lookup finished. (%d results)\n",
888 * Handle timeout for DHT requests
890 * @param cls the request handle as closure
891 * @param tc the task context
894 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
896 struct ResolverHandle *rh = cls;
897 struct RecordLookupHandle *rlh = rh->proc_cls;
898 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus) timed out.\n",
902 rh->id, rh->name, rh->timeout.rel_value);
904 * Start resolution in bg
906 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
907 rh->name, GNUNET_GNS_TLD);
909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
910 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
911 rh->id, new_name, rlh->record_type);
913 gns_resolver_lookup_record (rh->authority,
914 rh->private_local_zone,
918 GNUNET_TIME_UNIT_FOREVER_REL,
920 &background_lookup_result_processor,
923 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
925 GNUNET_DHT_get_stop (rh->get_handle);
926 rh->get_handle = NULL;
927 rh->proc (rh->proc_cls, rh, 0, NULL);
932 * Function called when we get a result from the dht
933 * for our record query
935 * @param cls the request handle
936 * @param exp lifetime
937 * @param key the key the record was stored under
938 * @param get_path get path
939 * @param get_path_length get path length
940 * @param put_path put path
941 * @param put_path_length put path length
942 * @param type the block type
943 * @param size the size of the record
944 * @param data the record data
947 process_record_result_dht (void* cls,
948 struct GNUNET_TIME_Absolute exp,
949 const struct GNUNET_HashCode * key,
950 const struct GNUNET_PeerIdentity *get_path,
951 unsigned int get_path_length,
952 const struct GNUNET_PeerIdentity *put_path,
953 unsigned int put_path_length,
954 enum GNUNET_BLOCK_Type type,
955 size_t size, const void *data)
957 struct ResolverHandle *rh = cls;
958 struct RecordLookupHandle *rlh = rh->proc_cls;
959 const struct GNSNameRecordBlock *nrb = data;
960 uint32_t num_records;
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
968 /* stop lookup and timeout task */
969 GNUNET_DHT_get_stop (rh->get_handle);
970 rh->get_handle = NULL;
971 if (rh->dht_heap_node != NULL)
973 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
974 rh->dht_heap_node = NULL;
976 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
978 GNUNET_SCHEDULER_cancel (rh->timeout_task);
979 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
981 rh->get_handle = NULL;
982 name = (const char*) &nrb[1];
983 num_records = ntohl (nrb->rd_count);
985 struct GNUNET_NAMESTORE_RecordData rd[num_records];
986 struct NamestoreBGTask *ns_heap_root;
987 struct NamestoreBGTask *namestore_bg_task;
989 rd_data = &name[strlen (name) + 1];
990 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
992 GNUNET_NAMESTORE_records_deserialize (rd_size,
997 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
998 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
999 rh->proc (rh->proc_cls, rh, 0, NULL);
1002 for (i = 0; i < num_records; i++)
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1006 rh->id, name, rh->name);
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1009 rh->id, rd[i].record_type, rlh->record_type);
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1011 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1012 rh->id, rd[i].data_size);
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1014 "GNS_PHASE_REC-%llu: Got flag %d\n",
1015 rh->id, rd[i].flags);
1017 if ((strcmp (name, rh->name) == 0) &&
1018 (rd[i].record_type == rlh->record_type))
1024 * FIXME check pubkey against existing key in namestore?
1025 * https://gnunet.org/bugs/view.php?id=2179
1027 if (max_allowed_ns_tasks <=
1028 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1030 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1031 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1034 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1038 /* Save to namestore */
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1040 "GNS_PHASE_REC-%llu: Caching record for %s\n",
1042 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1043 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1050 &on_namestore_record_put_result, //cont
1053 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1055 GNUNET_TIME_absolute_get().abs_value);
1056 if (0 < rh->answered)
1057 rh->proc (rh->proc_cls, rh, num_records, rd);
1059 rh->proc (rh->proc_cls, rh, 0, NULL);
1065 * Start DHT lookup for a (name -> query->record_type) record in
1066 * rh->authority's zone
1068 * @param rh the pending gns query context
1071 resolve_record_dht (struct ResolverHandle *rh)
1073 struct RecordLookupHandle *rlh = rh->proc_cls;
1075 struct GNUNET_HashCode lookup_key;
1076 struct ResolverHandle *rh_heap_root;
1078 GNUNET_GNS_get_key_for_record (rh->name, &rh->authority, &lookup_key);
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1081 rh->id, rh->name, GNUNET_h2s (&lookup_key));
1083 rh->dht_heap_node = NULL;
1085 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
1088 * Update timeout if necessary
1090 if (GNUNET_SCHEDULER_NO_TASK == rh->timeout_task)
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1093 "GNS_PHASE_REC-%llu: Adjusting timeout to %s/2\n",
1095 GNUNET_STRINGS_relative_time_to_string (rh->timeout, GNUNET_YES));
1097 * Set timeout for authority lookup phase to 1/2
1099 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1100 GNUNET_TIME_relative_divide (rh->timeout, 2),
1101 &handle_lookup_timeout,
1104 rh->timeout_cont = &dht_lookup_timeout;
1105 rh->timeout_cont_cls = rh;
1109 if (max_allowed_background_queries <=
1110 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1112 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1113 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1114 rh_heap_root->get_handle = NULL;
1115 rh_heap_root->dht_heap_node = NULL;
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1118 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1119 rh->id, rh_heap_root->name);
1120 rh_heap_root->proc (rh_heap_root->proc_cls,
1125 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1127 GNUNET_TIME_absolute_get ().abs_value);
1130 xquery = htonl (rlh->record_type);
1132 GNUNET_assert (rh->get_handle == NULL);
1133 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1134 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1136 DHT_GNS_REPLICATION_LEVEL,
1137 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1140 &process_record_result_dht,
1147 * Namestore calls this function if we have record for this name.
1148 * (or with rd_count=0 to indicate no matches)
1150 * @param cls the pending query
1151 * @param key the key of the zone we did the lookup
1152 * @param expiration expiration date of the namestore entry
1153 * @param name the name for which we need an authority
1154 * @param rd_count the number of records with 'name'
1155 * @param rd the record data
1156 * @param signature the signature of the authority for the record data
1159 process_record_result_ns (void* cls,
1160 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *key,
1161 struct GNUNET_TIME_Absolute expiration,
1162 const char *name, unsigned int rd_count,
1163 const struct GNUNET_NAMESTORE_RecordData *rd,
1164 const struct GNUNET_CRYPTO_EccSignature *signature)
1166 struct ResolverHandle *rh = cls;
1167 struct RecordLookupHandle *rlh = rh->proc_cls;
1168 struct GNUNET_TIME_Relative remaining_time;
1169 struct GNUNET_CRYPTO_ShortHashCode zone;
1170 struct GNUNET_TIME_Absolute et;
1173 rh->namestore_task = NULL;
1174 GNUNET_CRYPTO_short_hash (key,
1175 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
1177 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1181 rh->status |= RSL_RECORD_EXISTS;
1182 if (remaining_time.rel_value == 0)
1183 rh->status |= RSL_RECORD_EXPIRED;
1188 * Lookup terminated and no results
1190 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1191 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1194 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1195 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1198 * Our zone and no result? Cannot resolve TT
1200 rh->proc(rh->proc_cls, rh, 0, NULL);
1204 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1205 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1207 for (i = 0; i < rd_count;i++)
1209 if (rd[i].record_type != rlh->record_type)
1212 if (ignore_pending_records &&
1213 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1215 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1216 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1221 //FIXME: eh? do I have to handle this here?
1222 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1223 et.abs_value = rd[i].expiration_time;
1224 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
1226 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1227 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1237 if (0 == rh->answered)
1239 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1240 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1241 rh->proc(rh->proc_cls, rh, 0, NULL);
1245 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1246 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1247 rh->id, rh->answered, rd_count);
1248 rh->proc(rh->proc_cls, rh, rd_count, rd);
1254 * VPN redirect result callback
1256 * @param cls the resolver handle
1257 * @param af the requested address family
1258 * @param address in_addr(6) respectively
1261 process_record_result_vpn (void* cls, int af, const void *address)
1263 struct ResolverHandle *rh = cls;
1264 struct RecordLookupHandle *rlh = rh->proc_cls;
1265 struct GNUNET_NAMESTORE_RecordData rd;
1267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1268 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1272 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1273 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1275 if (GNUNET_GNS_RECORD_A != rlh->record_type)
1277 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1278 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1280 rh->proc (rh->proc_cls, rh, 0, NULL);
1283 rd.record_type = GNUNET_GNS_RECORD_A;
1284 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1286 rd.data_size = sizeof (struct in_addr);
1288 rh->proc (rh->proc_cls, rh, 1, &rd);
1291 else if (AF_INET6 == af)
1293 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1294 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1296 if (GNUNET_GNS_RECORD_AAAA != rlh->record_type)
1298 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1299 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1301 rh->proc (rh->proc_cls, rh, 0, NULL);
1304 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1305 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1307 rd.data_size = sizeof (struct in6_addr);
1309 rh->proc (rh->proc_cls, rh, 1, &rd);
1313 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1314 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1316 rh->proc (rh->proc_cls, rh, 0, NULL);
1322 * Process VPN lookup result for record
1324 * @param cls the record lookup handle
1325 * @param rh resolver handle
1326 * @param rd_count number of results (1)
1327 * @param rd record data containing the result
1330 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1331 unsigned int rd_count,
1332 const struct GNUNET_NAMESTORE_RecordData *rd)
1334 struct RecordLookupHandle* rlh = cls;
1338 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1339 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1342 /* give up, cannot resolve */
1343 finish_lookup(rh, rlh, 0, NULL);
1347 /* results found yay */
1348 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1349 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!\n",
1352 finish_lookup(rh, rlh, rd_count, rd);
1357 * Sends a UDP dns query to a nameserver specified in the rh
1359 * @param rh the resolver handle
1362 send_dns_packet (struct ResolverHandle *rh);
1368 * @param cls the ResolverHandle for this lookup
1369 * @param addr the sockaddr
1370 * @param addrlen the socket address length
1373 handle_dns_resolver (void *cls,
1374 const struct sockaddr *addr,
1377 struct ResolverHandle *rh = cls;
1378 struct RecordLookupHandle *rlh = rh->proc_cls;
1379 struct GNUNET_NAMESTORE_RecordData rd;
1380 struct sockaddr_in *sai;
1381 struct sockaddr_in6 *sai6;
1385 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1386 "No address found in DNS!\n");
1387 finish_lookup (rh, rlh, 0, NULL);
1391 if (sizeof (struct sockaddr_in) == addrlen)
1393 sai = (struct sockaddr_in*) addr;
1394 rd.record_type = GNUNET_GNS_RECORD_A;
1395 rd.data_size = sizeof (struct in_addr);
1396 rd.data = &sai->sin_addr;
1398 else if (sizeof (struct sockaddr_in6) == addrlen)
1400 sai6 = (struct sockaddr_in6*) addr;
1401 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1402 rd.data_size = sizeof (struct in6_addr);
1403 rd.data = &sai6->sin6_addr;
1407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1408 "Address length is garbage!\n");
1409 finish_lookup (rh, rlh, 0, NULL);
1412 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1414 finish_lookup (rh, rlh, 1, &rd);
1419 * Resolve DNS name via local stub resolver
1421 * @param rh the resolver handle
1424 resolve_dns_name (struct ResolverHandle *rh)
1426 struct RecordLookupHandle *rlh = rh->proc_cls;
1429 if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1430 (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1433 "Can only resolve A/AAAA via stub... abort\n");
1434 finish_lookup (rh, rlh, 0, NULL);
1438 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1443 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1446 &handle_dns_resolver,
1452 * Read DNS packet from UDP socket and process the reply.
1454 * @param cls the resolver handle
1455 * @param tc task context
1458 read_dns_response (void *cls,
1459 const struct GNUNET_SCHEDULER_TaskContext *tc)
1461 struct ResolverHandle *rh = cls;
1462 struct RecordLookupHandle *rlh = rh->proc_cls;
1463 char buf[UINT16_MAX];
1465 struct sockaddr_in addr;
1467 struct GNUNET_DNSPARSER_Packet *packet;
1468 struct GNUNET_NAMESTORE_RecordData rd;
1469 int found_delegation = GNUNET_NO;
1470 int found_cname = GNUNET_NO;
1471 char* delegation_name = NULL;
1472 int zone_offset = 0;
1475 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1476 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1478 /* timeout or shutdown */
1479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1480 "Terminating DNS query %d\n", tc->reason);
1481 finish_lookup (rh, rlh, 0, NULL);
1485 addrlen = sizeof (addr);
1486 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1488 (struct sockaddr*) &addr,
1493 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1494 finish_lookup (rh, rlh, 0, NULL);
1497 if (NULL == (packet = GNUNET_DNSPARSER_parse (buf, r)))
1499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1500 "Failed to parse DNS reply!\n");
1501 finish_lookup (rh, rlh, 0, NULL);
1505 for (i = 0; i < packet->num_answers; i++)
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "Got record type %d (want %d)\n",
1509 packet->answers[i].type,
1511 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1512 if (GNUNET_GNS_RECORD_CNAME == packet->answers[i].type)
1514 struct GNUNET_DNSPARSER_Query query;
1515 struct GNUNET_DNSPARSER_Packet npacket;
1516 struct GNUNET_DNSPARSER_Flags flags;
1518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1519 "CNAME record, restarting query with `%s'\n",
1520 packet->answers[i].data.hostname);
1521 strcpy (rh->dns_name,
1522 packet->answers[i].data.hostname);
1523 found_cname = GNUNET_YES;
1524 query.name = rh->dns_name;
1525 query.type = rlh->record_type;
1526 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1527 memset (&flags, 0, sizeof (flags));
1528 flags.recursion_desired = 1;
1529 flags.checking_disabled = 1;
1530 npacket.queries = &query;
1531 npacket.answers = NULL;
1532 npacket.authority_records = NULL;
1533 npacket.num_queries = 1;
1534 npacket.num_answers = 0;
1535 npacket.num_authority_records = 0;
1536 npacket.num_additional_records = 0;
1537 npacket.flags = flags;
1538 npacket.id = rh->id;
1539 GNUNET_free_non_null (rh->dns_raw_packet);
1540 rh->dns_raw_packet = NULL;
1541 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&npacket,
1543 &rh->dns_raw_packet,
1544 &rh->dns_raw_packet_size))
1546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1547 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1549 GNUNET_NETWORK_socket_close (rh->dns_sock);
1550 finish_lookup (rh, rlh, 0, NULL);
1556 if ((packet->answers[i].type == rlh->record_type) &&
1557 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1561 rd.data = packet->answers[i].data.raw.data;
1562 rd.data_size = packet->answers[i].data.raw.data_len;
1563 rd.record_type = packet->answers[i].type;
1565 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1566 finish_lookup (rh, rlh, 1, &rd);
1567 GNUNET_DNSPARSER_free_packet (packet);
1572 if (GNUNET_YES == found_cname)
1574 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1575 if (0 > zone_offset)
1578 /* restart query with CNAME */
1579 if (0 == strcmp (rh->dns_name + zone_offset, rh->dns_zone))
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1582 "Asking DNS server for `%s'\n",
1585 send_dns_packet (rh);
1589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1590 "Trying system resolver for `%s'\n", rh->dns_name);
1591 resolve_dns_name (rh);
1594 GNUNET_DNSPARSER_free_packet (packet);
1598 for (i = 0; i < packet->num_authority_records; i++)
1600 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "Found NS delegation!\n");
1604 found_delegation = GNUNET_YES;
1605 delegation_name = packet->authority_records[i].data.hostname;
1610 for (i = 0; i < packet->num_additional_records; i++)
1612 if (GNUNET_NO == found_delegation)
1615 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1616 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1618 GNUNET_assert (sizeof (struct in_addr) ==
1619 packet->authority_records[i].data.raw.data_len);
1621 rh->dns_addr.sin_addr =
1622 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1623 send_dns_packet (rh);
1624 GNUNET_DNSPARSER_free_packet (packet);
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1630 "Nothing useful in DNS reply!\n");
1631 finish_lookup (rh, rlh, 0, NULL);
1632 GNUNET_DNSPARSER_free_packet (packet);
1637 * Sends a UDP dns query to a nameserver specified in the rh
1639 * @param rh the request handle
1642 send_dns_packet (struct ResolverHandle *rh)
1644 struct GNUNET_NETWORK_FDSet *rset;
1646 rset = GNUNET_NETWORK_fdset_create ();
1647 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1649 "Sending %d byte DNS query\n",
1650 (int) rh->dns_raw_packet_size);
1652 if (GNUNET_SYSERR ==
1653 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1655 rh->dns_raw_packet_size,
1656 (struct sockaddr*)&rh->dns_addr,
1657 sizeof (struct sockaddr_in)))
1658 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1659 _("Failed to send DNS request to %s\n"),
1660 GNUNET_a2s ((const struct sockaddr *)&rh->dns_addr,
1661 sizeof (struct sockaddr_in)));
1663 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1664 rh->timeout, //FIXME less?
1669 GNUNET_NETWORK_fdset_destroy (rset);
1674 * The final phase of resoution.
1675 * We found a NS RR and want to resolve via DNS
1677 * @param rh the pending lookup handle
1678 * @param rd_count length of record data
1679 * @param rd record data containing VPN RR
1682 resolve_record_dns (struct ResolverHandle *rh,
1683 unsigned int rd_count,
1684 const struct GNUNET_NAMESTORE_RecordData *rd)
1686 struct RecordLookupHandle *rlh = rh->proc_cls;
1687 struct GNUNET_DNSPARSER_Query query;
1688 struct GNUNET_DNSPARSER_Packet packet;
1689 struct GNUNET_DNSPARSER_Flags flags;
1690 struct in_addr dnsip;
1691 struct sockaddr_in addr;
1692 struct sockaddr *sa;
1695 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1696 memset (rh->dns_name, 0, sizeof (rh->dns_name));
1698 /* We cancel here as to not include the ns lookup in the timeout */
1699 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1701 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1702 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1704 /* Start shortening */
1705 if ((NULL != rh->priv_key) &&
1706 (GNUNET_YES == is_canonical (rh->name)))
1708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1709 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1711 start_shorten (rh->authority_chain_head,
1715 for (i = 0; i < rd_count; i++)
1717 /* Synthesize dns name */
1718 if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1720 strcpy (rh->dns_zone, (char*)rd[i].data);
1721 if (0 == strcmp (rh->name, ""))
1722 strcpy (rh->dns_name, (char*)rd[i].data);
1724 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1727 if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1728 /* need to use memcpy as .data may be unaligned */
1729 memcpy (&dnsip, rd[i].data, sizeof (dnsip));
1732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1733 "GNS_PHASE_REC_DNS-%llu: Looking up `%s' from `%s'\n",
1737 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1738 if (NULL == rh->dns_sock)
1740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1741 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1743 finish_lookup (rh, rlh, 0, NULL);
1747 memset (&addr, 0, sizeof (struct sockaddr_in));
1748 sa = (struct sockaddr *) &addr;
1749 sa->sa_family = AF_INET;
1750 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1752 sizeof (struct sockaddr_in),
1755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1756 "GNS_PHASE_REC_DNS-%llu: Error binding UDP socket for DNS lookup!\n",
1758 finish_lookup (rh, rlh, 0, NULL);
1761 query.name = rh->dns_name;
1762 query.type = rlh->record_type;
1763 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1764 memset (&flags, 0, sizeof (flags));
1765 flags.recursion_desired = 1;
1766 flags.checking_disabled = 1;
1767 packet.queries = &query;
1768 packet.answers = NULL;
1769 packet.authority_records = NULL;
1770 packet.num_queries = 1;
1771 packet.num_answers = 0;
1772 packet.num_authority_records = 0;
1773 packet.num_additional_records = 0;
1774 packet.flags = flags;
1776 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1778 &rh->dns_raw_packet,
1779 &rh->dns_raw_packet_size))
1781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1782 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1784 GNUNET_NETWORK_socket_close (rh->dns_sock);
1785 finish_lookup (rh, rlh, 0, NULL);
1789 rh->dns_addr.sin_family = AF_INET;
1790 rh->dns_addr.sin_port = htons (53); //domain
1791 rh->dns_addr.sin_addr = dnsip;
1792 #if HAVE_SOCKADDR_IN_SIN_LEN
1793 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1795 send_dns_packet (rh);
1800 * The final phase of resoution.
1801 * We found a VPN RR and want to request an IPv4/6 address
1803 * @param rh the pending lookup handle
1804 * @param rd_count length of record data
1805 * @param rd record data containing VPN RR
1808 resolve_record_vpn (struct ResolverHandle *rh,
1809 unsigned int rd_count,
1810 const struct GNUNET_NAMESTORE_RecordData *rd)
1812 struct RecordLookupHandle *rlh = rh->proc_cls;
1813 struct GNUNET_HashCode serv_desc;
1814 struct vpn_data* vpn;
1817 /* We cancel here as to not include the ns lookup in the timeout */
1818 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1820 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1821 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1823 /* Start shortening */
1824 if ((NULL != rh->priv_key) &&
1825 (GNUNET_YES == is_canonical (rh->name)))
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1828 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1830 start_shorten (rh->authority_chain_head,
1834 vpn = (struct vpn_data*)rd->data;
1835 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1836 strlen ((char*)&vpn[1]) + 1,
1838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1839 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1842 GNUNET_h2s (&vpn->peer));
1844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1845 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1848 GNUNET_h2s (&serv_desc));
1849 rh->proc = &handle_record_vpn;
1850 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1855 if (NULL == vpn_handle)
1857 vpn_handle = GNUNET_VPN_connect (cfg);
1858 if (NULL == vpn_handle)
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1861 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1862 finish_lookup (rh, rh->proc_cls, 0, NULL);
1867 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1868 af, ntohs (vpn->proto),
1869 (struct GNUNET_PeerIdentity *)&vpn->peer,
1872 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1873 &process_record_result_vpn,
1876 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1877 "Error connecting to VPN (not available on W32 yet)\n");
1878 finish_lookup (rh, rh->proc_cls, 0, NULL);
1884 * The final phase of resolution.
1885 * rh->name is a name that is canonical and we do not have a delegation.
1886 * Query namestore for this record
1888 * @param rh the pending lookup handle
1891 resolve_record_ns(struct ResolverHandle *rh)
1893 struct RecordLookupHandle *rlh = rh->proc_cls;
1895 /* We cancel here as to not include the ns lookup in the timeout */
1896 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1898 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1899 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1901 /* Start shortening */
1902 if ((NULL != rh->priv_key) &&
1903 (GNUNET_YES == is_canonical (rh->name)))
1905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1906 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1908 start_shorten (rh->authority_chain_head,
1913 * Try to resolve this record in our namestore.
1914 * The name to resolve is now in rh->authority_name
1915 * since we tried to resolve it to an authority
1918 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1922 &process_record_result_ns,
1928 * Handle timeout for DHT requests
1930 * @param cls the request handle as closure
1931 * @param tc the task context
1934 dht_authority_lookup_timeout (void *cls,
1935 const struct GNUNET_SCHEDULER_TaskContext *tc)
1937 struct ResolverHandle *rh = cls;
1938 struct RecordLookupHandle *rlh = rh->proc_cls;
1939 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
1941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1942 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus) timed out.\n",
1943 rh->id, rh->authority_name,
1944 rh->timeout.rel_value);
1946 rh->status |= RSL_TIMED_OUT;
1947 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1948 if (NULL != rh->get_handle)
1950 GNUNET_DHT_get_stop (rh->get_handle);
1951 rh->get_handle = NULL;
1953 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1956 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
1958 rh->proc (rh->proc_cls, rh, 0, NULL);
1961 if (0 == strcmp (rh->name, ""))
1964 * promote authority back to name and try to resolve record
1966 strcpy (rh->name, rh->authority_name);
1967 rh->proc (rh->proc_cls, rh, 0, NULL);
1972 * Start resolution in bg
1974 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
1976 rh->name, rh->authority_name, GNUNET_GNS_TLD);
1977 strcpy (rh->name, new_name);
1978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1979 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1982 gns_resolver_lookup_record (rh->authority,
1983 rh->private_local_zone,
1987 GNUNET_TIME_UNIT_FOREVER_REL,
1989 &background_lookup_result_processor,
1991 rh->proc (rh->proc_cls, rh, 0, NULL);
1996 * Start DHT lookup for a name -> PKEY (compare NS) record in
1997 * rh->authority's zone
1999 * @param rh the pending gns query
2002 resolve_delegation_dht (struct ResolverHandle *rh);
2006 * Resolve the delegation chain for the request in our namestore
2008 * @param rh the resolver handle
2011 resolve_delegation_ns (struct ResolverHandle *rh);
2015 * Namestore resolution for delegation finished. Processing result.
2017 * @param cls the closure
2018 * @param rh resolver handle
2019 * @param rd_count number of results (always 0)
2020 * @param rd record data (always NULL)
2023 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2024 unsigned int rd_count,
2025 const struct GNUNET_NAMESTORE_RecordData *rd);
2029 * This is a callback function that checks for key revocation
2031 * @param cls the pending query
2032 * @param key the key of the zone we did the lookup
2033 * @param expiration expiration date of the record data set in the namestore
2034 * @param name the name for which we need an authority
2035 * @param rd_count the number of records with 'name'
2036 * @param rd the record data
2037 * @param signature the signature of the authority for the record data
2040 process_pkey_revocation_result_ns (void *cls,
2041 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *key,
2042 struct GNUNET_TIME_Absolute expiration,
2044 unsigned int rd_count,
2045 const struct GNUNET_NAMESTORE_RecordData *rd,
2046 const struct GNUNET_CRYPTO_EccSignature *signature)
2048 struct ResolverHandle *rh = cls;
2049 struct GNUNET_TIME_Relative remaining_time;
2052 rh->namestore_task = NULL;
2053 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2055 for (i = 0; i < rd_count; i++)
2057 if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2059 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2060 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2062 rh->status |= RSL_PKEY_REVOKED;
2063 rh->proc (rh->proc_cls, rh, 0, NULL);
2068 if ((NULL == name) ||
2069 (0 == remaining_time.rel_value))
2071 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2072 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2075 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2077 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2078 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2079 rh->id, "+.gads", GNUNET_GNS_RECORD_REV);
2081 gns_resolver_lookup_record(rh->authority,
2082 rh->private_local_zone,
2083 GNUNET_GNS_RECORD_REV,
2086 GNUNET_TIME_UNIT_FOREVER_REL,
2088 &background_lookup_result_processor,
2092 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2093 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2096 * We are done with PKEY resolution if name is empty
2097 * else resolve again with new authority
2099 if (strcmp (rh->name, "") == 0)
2100 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2102 resolve_delegation_ns (rh);
2107 * Callback when record data is put into namestore
2109 * @param cls the closure
2110 * @param success GNUNET_OK on success
2111 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2114 on_namestore_delegation_put_result(void *cls,
2118 struct NamestoreBGTask *nbg = cls;
2120 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2123 if (GNUNET_NO == success)
2125 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2126 "GNS_NS: records already in namestore\n");
2129 else if (GNUNET_YES == success)
2131 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2132 "GNS_NS: records successfully put in namestore\n");
2136 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2137 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2142 * Function called when we get a result from the dht
2143 * for our query. Recursively tries to resolve authorities
2146 * @param cls the request handle
2147 * @param exp lifetime
2148 * @param key the key the record was stored under
2149 * @param get_path get path
2150 * @param get_path_length get path length
2151 * @param put_path put path
2152 * @param put_path_length put path length
2153 * @param type the block type
2154 * @param size the size of the record
2155 * @param data the record data
2158 process_delegation_result_dht (void* cls,
2159 struct GNUNET_TIME_Absolute exp,
2160 const struct GNUNET_HashCode * key,
2161 const struct GNUNET_PeerIdentity *get_path,
2162 unsigned int get_path_length,
2163 const struct GNUNET_PeerIdentity *put_path,
2164 unsigned int put_path_length,
2165 enum GNUNET_BLOCK_Type type,
2166 size_t size, const void *data)
2168 struct ResolverHandle *rh = cls;
2169 const struct GNSNameRecordBlock *nrb = data;
2170 const char* rd_data;
2171 uint32_t num_records;
2175 struct GNUNET_CRYPTO_ShortHashCode zone;
2177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2178 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n",
2182 /* stop dht lookup and timeout task */
2183 GNUNET_DHT_get_stop (rh->get_handle);
2184 rh->get_handle = NULL;
2185 if (rh->dht_heap_node != NULL)
2187 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2188 rh->dht_heap_node = NULL;
2191 num_records = ntohl(nrb->rd_count);
2192 name = (const char*) &nrb[1];
2194 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2195 struct NamestoreBGTask *ns_heap_root;
2196 struct NamestoreBGTask *namestore_bg_task;
2198 rd_data = name + strlen(name) + 1;
2199 rd_size = size - strlen(name) - 1 - sizeof (struct GNSNameRecordBlock);
2200 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2205 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2206 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2211 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2212 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2213 rh->id, name, rh->authority_name);
2214 for (i=0; i<num_records; i++)
2216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2217 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2218 rh->id, name, rh->authority_name);
2219 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2220 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2221 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2222 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2223 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2224 rh->id, rd[i].data_size);
2225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2226 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2227 rh->id, rd[i].flags);
2229 if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2230 (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2231 (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2234 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2236 if (0 == strcmp(rh->name, ""))
2237 strcpy(rh->name, rh->authority_name);
2239 GNUNET_snprintf(rh->name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
2240 rh->name, rh->authority_name); //FIXME ret
2245 if ((0 == strcmp(name, rh->authority_name)) &&
2246 (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2248 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2249 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2252 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2253 struct AuthorityChain *auth =
2254 GNUNET_malloc(sizeof(struct AuthorityChain));
2255 auth->zone = rh->authority;
2256 memset(auth->name, 0, strlen(rh->authority_name)+1);
2257 strcpy(auth->name, rh->authority_name);
2258 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2259 rh->authority_chain_tail,
2262 if (NULL != rh->rd.data)
2263 GNUNET_free ((void*)rh->rd.data);
2265 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2266 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2267 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2270 /** try to import pkey if private key available */
2271 //if (rh->priv_key && is_canonical (rh->name))
2272 // process_discovered_authority(name, auth->zone,
2273 // rh->authority_chain_tail->zone,
2278 GNUNET_GNS_get_zone_from_key (name, key, &zone);
2281 /* Save to namestore
2282 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2285 if (max_allowed_ns_tasks <=
2286 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2288 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2289 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2291 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2292 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background ns task\n",
2295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2296 "GNS_PHASE_DELEGATE_DHT-%llu: Caching record for %s\n",
2298 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2300 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2302 GNUNET_TIME_absolute_get().abs_value);
2303 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2310 &on_namestore_delegation_put_result, //cont
2311 namestore_bg_task); //cls
2315 if (0 != rh->answered)
2320 * FIXME in this case. should we ask namestore again?
2322 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2323 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2324 rh->id, rh->authority_name, rh->name);
2326 if (0 == strcmp(rh->name, ""))
2328 /* Start shortening */
2329 if ((NULL != rh->priv_key) &&
2330 (GNUNET_YES == is_canonical (rh->name)))
2332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2333 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2335 start_shorten (rh->authority_chain_head,
2340 rh->proc = &handle_delegation_ns;
2343 /* Check for key revocation and delegate */
2344 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2346 GNUNET_GNS_MASTERZONE_STR,
2347 GNUNET_GNS_RECORD_REV,
2348 &process_pkey_revocation_result_ns,
2355 * No pkey but name exists
2358 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2359 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2360 rh->id, rh->authority_name, rh->name);
2361 if (0 == strcmp(rh->name, ""))
2362 strcpy(rh->name, rh->authority_name);
2364 GNUNET_snprintf(rh->name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
2365 rh->name, rh->authority_name); //FIXME ret
2367 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2368 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2369 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2370 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2372 rh->proc(rh->proc_cls, rh, 0, NULL);
2375 //FIXME maybe define somewhere else?
2376 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2377 +(GNUNET_DNSPARSER_MAX_NAME_LENGTH*2)
2378 #define MAX_MX_LENGTH sizeof(uint16_t)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
2379 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
2383 * Exands a name ending in .+ with the zone of origin.
2384 * FIXME: funky api: 'dest' must be large enough to hold
2385 * the result; this is a bit yucky...
2387 * @param dest destination buffer
2388 * @param src the .+ name
2389 * @param repl the string to replace the + with
2392 expand_plus (char* dest,
2397 size_t s_len = strlen (src) + 1;
2399 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2402 /* no postprocessing */
2403 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2404 "GNS_POSTPROCESS: %s too short\n", src);
2405 memcpy (dest, src, s_len);
2408 if (0 == strcmp (src + s_len - 3, ".+"))
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2411 "GNS_POSTPROCESS: Expanding .+ in %s\n",
2413 memset (dest, 0, s_len + strlen (repl) + strlen(GNUNET_GNS_TLD));
2415 pos = dest + s_len - 2;
2417 pos += strlen (repl);
2418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2419 "GNS_POSTPROCESS: Expanded to %s\n",
2424 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2425 "GNS_POSTPROCESS: No postprocessing for %s\n", src);
2426 memcpy (dest, src, s_len);
2435 finish_lookup (struct ResolverHandle *rh,
2436 struct RecordLookupHandle* rlh,
2437 unsigned int rd_count,
2438 const struct GNUNET_NAMESTORE_RecordData *rd)
2441 char new_rr_data[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
2442 char new_mx_data[MAX_MX_LENGTH];
2443 char new_soa_data[MAX_SOA_LENGTH];
2444 char new_srv_data[MAX_SRV_LENGTH];
2445 struct srv_data *old_srv;
2446 struct srv_data *new_srv;
2447 struct soa_data *old_soa;
2448 struct soa_data *new_soa;
2449 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2452 unsigned int offset;
2454 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
2456 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2457 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2460 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2463 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2465 for (i = 0; i < rd_count; i++)
2468 if ((GNUNET_GNS_RECORD_NS != rd[i].record_type) &&
2469 (GNUNET_GNS_RECORD_PTR != rd[i].record_type) &&
2470 (GNUNET_GNS_RECORD_CNAME != rd[i].record_type) &&
2471 (GNUNET_GNS_RECORD_MX != rd[i].record_type) &&
2472 (GNUNET_GNS_RECORD_SOA != rd[i].record_type) &&
2473 (GNUNET_GNS_RECORD_SRV != rd[i].record_type))
2475 p_rd[i].data = rd[i].data;
2480 * for all those records we 'should'
2481 * also try to resolve the A/AAAA records (RFC1035)
2482 * This is a feature and not important
2485 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2486 "GNS_POSTPROCESS: Postprocessing\n");
2487 if (0 == strcmp(rh->name, GNUNET_GNS_MASTERZONE_STR))
2488 repl_string = rlh->name;
2490 repl_string = rlh->name+strlen(rh->name)+1;
2493 if (GNUNET_GNS_RECORD_MX == rd[i].record_type)
2495 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2496 offset = sizeof (uint16_t);
2497 pos = new_mx_data + offset;
2498 // FIXME: how do we know that 'pos' has enough space for the new name?
2499 expand_plus (pos, (char*)rd[i].data+sizeof(uint16_t),
2501 offset += strlen(new_mx_data+sizeof(uint16_t)) + 1;
2502 p_rd[i].data = new_mx_data;
2503 p_rd[i].data_size = offset;
2505 else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2508 * Prio, weight and port
2510 new_srv = (struct srv_data*)new_srv_data;
2511 old_srv = (struct srv_data*)rd[i].data;
2512 new_srv->prio = old_srv->prio;
2513 new_srv->weight = old_srv->weight;
2514 new_srv->port = old_srv->port;
2515 // FIXME: how do we know that '&new_srv[1]' has enough space for the new name?
2516 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2518 p_rd[i].data = new_srv_data;
2519 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2521 else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2523 /* expand mname and rname */
2524 old_soa = (struct soa_data*)rd[i].data;
2525 new_soa = (struct soa_data*)new_soa_data;
2526 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2527 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2528 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2529 offset = strlen ((char*)&new_soa[1]) + 1;
2530 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2531 expand_plus((char*)&new_soa[1] + offset,
2532 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2534 p_rd[i].data_size = sizeof (struct soa_data)
2536 + strlen ((char*)&new_soa[1] + offset);
2537 p_rd[i].data = new_soa_data;
2542 // FIXME: how do we know that 'rd[i].data' has enough space for the new name?
2543 expand_plus(pos, (char*)rd[i].data, repl_string);
2544 p_rd[i].data_size = strlen(new_rr_data)+1;
2545 p_rd[i].data = new_rr_data;
2550 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2552 free_resolver_handle (rh);
2557 * Process DHT lookup result for record.
2559 * @param cls the closure
2560 * @param rh resolver handle
2561 * @param rd_count number of results
2562 * @param rd record data
2565 handle_record_dht (void* cls, struct ResolverHandle *rh,
2566 unsigned int rd_count,
2567 const struct GNUNET_NAMESTORE_RecordData *rd)
2569 struct RecordLookupHandle* rlh = cls;
2573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2574 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2576 /* give up, cannot resolve */
2577 finish_lookup (rh, rlh, 0, NULL);
2580 /* results found yay */
2581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2582 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2583 finish_lookup (rh, rlh, rd_count, rd);
2588 * Process namestore lookup result for record.
2590 * @param cls the closure
2591 * @param rh resolver handle
2592 * @param rd_count number of results
2593 * @param rd record data
2596 handle_record_ns (void* cls, struct ResolverHandle *rh,
2597 unsigned int rd_count,
2598 const struct GNUNET_NAMESTORE_RecordData *rd)
2600 struct RecordLookupHandle* rlh = cls;
2601 int check_dht = GNUNET_YES;
2605 /* results found yay */
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2607 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2608 finish_lookup (rh, rlh, rd_count, rd);
2612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2613 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2617 * There are 5 conditions that have to met for us to consult the DHT:
2618 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2619 * 2. No entry in the NS existed AND
2620 * 3. The zone queried is not the local resolver's zone AND
2621 * 4. The name that was looked up is '+'
2622 * because if it was any other canonical name we either already queried
2623 * the DHT for the authority in the authority lookup phase (and thus
2624 * would already have an entry in the NS for the record)
2625 * 5. We are not in cache only mode
2627 if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2628 (0 == (rh->status & RSL_RECORD_EXISTS)) )
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2632 "GNS_PHASE_REC-%llu: Not expired and exists!\n",
2634 check_dht = GNUNET_NO;
2637 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2638 &rh->private_local_zone))
2641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2642 "GNS_PHASE_REC-%llu: Our zone!\n",
2644 check_dht = GNUNET_NO;
2647 if ((0 != strcmp (rh->name, GNUNET_GNS_MASTERZONE_STR)) && (GNUNET_YES == is_srv (rh->name)))
2648 check_dht = GNUNET_NO;
2650 if (GNUNET_YES == rh->only_cached)
2651 check_dht = GNUNET_NO;
2653 if (GNUNET_YES == check_dht)
2655 rh->proc = &handle_record_dht;
2656 resolve_record_dht(rh);
2659 /* give up, cannot resolve */
2660 finish_lookup (rh, rlh, 0, NULL);
2665 * Move one level up in the domain hierarchy and return the
2666 * passed top level domain.
2668 * FIXME: funky API: not only 'dest' is updated, so is 'name'!
2670 * @param name the domain
2671 * @param dest the destination where the tld will be put
2674 pop_tld (char* name, char* dest)
2678 if (GNUNET_YES == is_canonical (name))
2680 strcpy (dest, name);
2685 for (len = strlen(name); 0 < len; len--)
2687 if (*(name+len) == '.')
2695 strcpy (dest, (name+len+1));
2700 * DHT resolution for delegation finished. Processing result.
2702 * @param cls the closure
2703 * @param rh resolver handle
2704 * @param rd_count number of results (always 0)
2705 * @param rd record data (always NULL)
2708 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2709 unsigned int rd_count,
2710 const struct GNUNET_NAMESTORE_RecordData *rd)
2712 struct RecordLookupHandle* rlh = cls;
2714 if (0 == strcmp(rh->name, ""))
2716 if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2718 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2719 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2721 finish_lookup(rh, rlh, rd_count, rd);
2724 /* We resolved full name for delegation. resolving record */
2725 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2726 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2728 strcpy(rh->name, "+\0");
2729 rh->proc = &handle_record_ns;
2730 resolve_record_ns(rh);
2735 * we still have some left
2737 if (GNUNET_YES == is_canonical (rh->name))
2739 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2740 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2743 rh->proc = &handle_record_ns;
2744 resolve_record_ns(rh);
2747 /* give up, cannot resolve */
2748 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2749 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2751 finish_lookup(rh, rlh, 0, NULL);
2756 * Start DHT lookup for a name -> PKEY (compare NS) record in
2757 * rh->authority's zone
2759 * @param rh the pending gns query
2762 resolve_delegation_dht (struct ResolverHandle *rh)
2765 struct GNUNET_HashCode lookup_key;
2766 struct ResolverHandle *rh_heap_root;
2768 pop_tld (rh->name, rh->authority_name);
2769 GNUNET_GNS_get_key_for_record (rh->authority_name,
2772 rh->dht_heap_node = NULL;
2773 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2775 rh->timeout_cont = &dht_authority_lookup_timeout;
2776 rh->timeout_cont_cls = rh;
2780 if (max_allowed_background_queries <=
2781 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2783 /* terminate oldest lookup */
2784 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2785 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2786 rh_heap_root->get_handle = NULL;
2787 rh_heap_root->dht_heap_node = NULL;
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2789 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2791 rh_heap_root->authority_name);
2792 rh_heap_root->proc (rh_heap_root->proc_cls,
2797 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2799 GNUNET_TIME_absolute_get().abs_value);
2801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2802 "Beginning DHT lookup for %s in zone %s for request %llu\n",
2804 GNUNET_short_h2s (&rh->authority),
2806 xquery = htonl (GNUNET_GNS_RECORD_PKEY);
2807 GNUNET_assert (rh->get_handle == NULL);
2808 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
2809 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2811 DHT_GNS_REPLICATION_LEVEL,
2812 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2815 &process_delegation_result_dht,
2821 * Checks if "name" ends in ".tld"
2823 * @param name the name to check
2824 * @param tld the TLD to check for
2825 * @return GNUNET_YES or GNUNET_NO
2828 is_tld (const char* name, const char* tld)
2832 if (strlen (name) <= strlen (tld))
2834 offset = strlen (name) - strlen (tld);
2835 if (0 != strcmp (name + offset, tld))
2842 * Namestore resolution for delegation finished. Processing result.
2844 * @param cls the closure
2845 * @param rh resolver handle
2846 * @param rd_count number of results (always 0)
2847 * @param rd record data (always NULL)
2850 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2851 unsigned int rd_count,
2852 const struct GNUNET_NAMESTORE_RecordData *rd)
2854 struct RecordLookupHandle* rlh = cls;
2858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2859 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2860 rh->id, rh->status);
2862 if (rh->status & RSL_PKEY_REVOKED)
2864 finish_lookup (rh, rlh, 0, NULL);
2868 if (0 == strcmp(rh->name, ""))
2871 /* We resolved full name for delegation. resolving record */
2872 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2873 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2875 if (rh->status & RSL_CNAME_FOUND)
2877 if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2879 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2880 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2882 strcpy (rh->name, rh->authority_name);
2883 finish_lookup (rh, rlh, rd_count, rd);
2888 if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2890 s_len = strlen (rd->data) - 2;
2891 memcpy (rh->name, rd->data, s_len);
2892 rh->name[s_len] = '\0';
2893 resolve_delegation_ns (rh);
2896 else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2898 gns_resolver_lookup_record (rh->authority,
2899 rh->private_local_zone,
2908 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2909 free_resolver_handle (rh);
2915 strcpy (rh->dns_name, (char*)rd->data);
2916 resolve_dns_name (rh);
2921 else if (rh->status & RSL_DELEGATE_VPN)
2923 if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2925 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2926 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2928 finish_lookup(rh, rlh, rd_count, rd);
2931 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2932 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2934 GNUNET_assert (NULL != rd);
2935 rh->proc = &handle_record_vpn;
2936 resolve_record_vpn (rh, rd_count, rd);
2939 else if (rh->status & RSL_DELEGATE_NS)
2941 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2944 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2946 finish_lookup (rh, rlh, rd_count, rd);
2949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2950 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2952 GNUNET_assert (NULL != rd);
2953 rh->proc = &handle_record_ns;
2954 resolve_record_dns (rh, rd_count, rd);
2957 else if (rh->status & RSL_DELEGATE_PKEY)
2959 if (rh->status & RSL_PKEY_REVOKED)
2961 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2962 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
2964 finish_lookup (rh, rlh, 0, NULL);
2967 else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2969 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2970 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
2972 finish_lookup(rh, rlh, rd_count, rd);
2976 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2977 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
2979 strcpy(rh->name, "+\0");
2980 rh->proc = &handle_record_ns;
2981 resolve_record_ns(rh);
2985 if (rh->status & RSL_DELEGATE_NS)
2987 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2989 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2990 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2992 finish_lookup(rh, rlh, rd_count, rd);
2996 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2997 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2999 GNUNET_assert (NULL != rd);
3000 rh->proc = &handle_record_ns;
3001 resolve_record_dns (rh, rd_count, rd);
3006 * we still have some left
3007 * check if authority in ns is fresh
3009 * or we are authority
3012 check_dht = GNUNET_YES;
3013 if ((rh->status & RSL_RECORD_EXISTS) &&
3014 !(rh->status & RSL_RECORD_EXPIRED))
3015 check_dht = GNUNET_NO;
3017 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3018 &rh->private_local_zone))
3019 check_dht = GNUNET_NO;
3021 if (GNUNET_YES == rh->only_cached)
3022 check_dht = GNUNET_NO;
3024 if (GNUNET_YES == check_dht)
3027 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3028 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3030 rh->proc = &handle_delegation_dht;
3031 resolve_delegation_dht(rh);
3035 if (GNUNET_NO == is_canonical (rh->name))
3037 /* give up, cannot resolve */
3038 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3039 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3042 finish_lookup(rh, rlh, rd_count, rd);
3045 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3046 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3049 rh->proc = &handle_record_ns;
3050 resolve_record_ns(rh);
3055 * This is a callback function that should give us only PKEY
3056 * records. Used to query the namestore for the authority (PKEY)
3057 * for 'name'. It will recursively try to resolve the
3058 * authority for a given name from the namestore.
3060 * @param cls the pending query
3061 * @param key the key of the zone we did the lookup
3062 * @param expiration expiration date of the record data set in the namestore
3063 * @param name the name for which we need an authority
3064 * @param rd_count the number of records with 'name'
3065 * @param rd the record data
3066 * @param signature the signature of the authority for the record data
3069 process_delegation_result_ns (void* cls,
3070 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *key,
3071 struct GNUNET_TIME_Absolute expiration,
3073 unsigned int rd_count,
3074 const struct GNUNET_NAMESTORE_RecordData *rd,
3075 const struct GNUNET_CRYPTO_EccSignature *signature)
3077 struct ResolverHandle *rh = cls;
3078 struct GNUNET_TIME_Relative remaining_time;
3079 struct GNUNET_CRYPTO_ShortHashCode zone;
3080 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3082 struct GNUNET_TIME_Absolute et;
3083 struct AuthorityChain *auth;
3085 rh->namestore_task = NULL;
3086 GNUNET_CRYPTO_short_hash (key,
3087 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3090 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup for `%s' in zone %s\n",
3093 GNUNET_short_h2s (&zone));
3095 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3102 "GNS_PHASE_DELEGATE_NS-%llu: Records with name `%s' exist in zone %s.\n",
3104 GNUNET_short_h2s (&zone));
3105 rh->status |= RSL_RECORD_EXISTS;
3107 if (0 == remaining_time.rel_value)
3109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3110 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3112 rh->status |= RSL_RECORD_EXPIRED;
3117 * No authority found in namestore.
3122 * We did not find an authority in the namestore
3127 * Promote this authority back to a name maybe it is
3130 if (strcmp (rh->name, "") == 0)
3132 /* simply promote back */
3133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3134 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3135 rh->id, rh->authority_name);
3136 strcpy (rh->name, rh->authority_name);
3140 /* add back to existing name */
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3142 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3143 rh->id, rh->authority_name, rh->name);
3144 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
3145 rh->name, rh->authority_name);
3146 strcpy (rh->name, new_name);
3147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3148 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3152 rh->proc (rh->proc_cls, rh, 0, NULL);
3157 * We found an authority that may be able to help us
3158 * move on with query
3159 * Note only 1 pkey should have been returned.. anything else would be strange
3161 for (i=0; i < rd_count;i++)
3163 switch (rd[i].record_type)
3165 case GNUNET_GNS_RECORD_CNAME:
3166 /* Like in regular DNS this should mean that there is no other
3167 * record for this name. */
3169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3170 "GNS_PHASE_DELEGATE_NS-%llu: CNAME `%.*s' found.\n",
3172 (int) rd[i].data_size,
3174 rh->status |= RSL_CNAME_FOUND;
3175 rh->proc (rh->proc_cls, rh, rd_count, rd);
3177 case GNUNET_GNS_RECORD_VPN:
3178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3179 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3181 rh->status |= RSL_DELEGATE_VPN;
3182 rh->proc (rh->proc_cls, rh, rd_count, rd);
3184 case GNUNET_GNS_RECORD_NS:
3185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3186 "GNS_PHASE_DELEGATE_NS-%llu: NS `%.*s' found.\n",
3188 (int) rd[i].data_size,
3190 rh->status |= RSL_DELEGATE_NS;
3191 rh->proc (rh->proc_cls, rh, rd_count, rd);
3193 case GNUNET_GNS_RECORD_PKEY:
3194 rh->status |= RSL_DELEGATE_PKEY;
3195 if ((ignore_pending_records != 0) &&
3196 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3199 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3204 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3205 et.abs_value = rd[i].expiration_time;
3206 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
3208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3209 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3211 if (remaining_time.rel_value == 0)
3213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3214 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3216 rh->authority_chain_head->fresh = 0;
3217 rh->proc (rh->proc_cls, rh, 0, NULL);
3222 /* Resolve rest of query with new authority */
3223 memcpy (&rh->authority, rd[i].data,
3224 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3225 auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3226 auth->zone = rh->authority;
3227 memset (auth->name, 0, strlen (rh->authority_name)+1);
3228 strcpy (auth->name, rh->authority_name);
3229 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3230 rh->authority_chain_tail,
3232 if (NULL != rh->rd.data)
3233 GNUNET_free ((void*)(rh->rd.data));
3234 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3235 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3236 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3238 /* Check for key revocation and delegate */
3239 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3241 GNUNET_GNS_MASTERZONE_STR,
3242 GNUNET_GNS_RECORD_REV,
3243 &process_pkey_revocation_result_ns,
3247 /* ignore, move to next result */
3252 /* no answers that would cause delegation were found */
3253 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3254 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup failed (no PKEY record)\n",
3257 * If we have found some records for the LAST label
3258 * we return the results. Else NULL.
3260 if (0 == strcmp (rh->name, ""))
3262 /* Start shortening */
3263 if ((rh->priv_key != NULL) &&
3264 (is_canonical (rh->name) == GNUNET_YES))
3266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3267 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3269 start_shorten (rh->authority_chain_head,
3272 /* simply promote back */
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3274 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3275 rh->id, rh->authority_name);
3276 strcpy (rh->name, rh->authority_name);
3277 rh->proc (rh->proc_cls, rh, rd_count, rd);
3281 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3282 "%s.%s", rh->name, rh->authority_name);
3283 strcpy (rh->name, new_name);
3284 rh->proc (rh->proc_cls, rh, 0, NULL);
3290 * Resolve the delegation chain for the request in our namestore
3291 * (as in, find the respective authority for the leftmost label).
3293 * @param rh the resolver handle
3296 resolve_delegation_ns (struct ResolverHandle *rh)
3298 pop_tld (rh->name, rh->authority_name);
3299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3300 "GNS_PHASE_DELEGATE_NS-%llu: Finding authority for `%s' by looking up `%s' in GADS zone `%s'\n",
3304 GNUNET_short_h2s (&rh->authority));
3305 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3308 GNUNET_GNS_RECORD_ANY,
3309 &process_delegation_result_ns,
3315 * Lookup of a record in a specific zone
3316 * calls lookup result processor on result
3318 * @param zone the root zone
3319 * @param pzone the private local zone
3320 * @param record_type the record type to look up
3321 * @param name the name to look up
3322 * @param key a private key for use with PSEU import (can be NULL)
3323 * @param timeout timeout for resolution
3324 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3325 * @param proc the processor to call on result
3326 * @param cls the closure to pass to proc
3329 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3330 struct GNUNET_CRYPTO_ShortHashCode pzone,
3331 uint32_t record_type,
3333 struct GNUNET_CRYPTO_EccPrivateKey *key,
3334 struct GNUNET_TIME_Relative timeout,
3336 RecordLookupProcessor proc,
3339 struct ResolverHandle *rh;
3340 struct RecordLookupHandle* rlh;
3341 char string_hash[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
3342 char nzkey[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
3343 char* nzkey_ptr = nzkey;
3345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3346 "Starting resolution for `%s' (type=%d) with timeout %s!\n",
3348 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
3350 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3351 (strcmp(GNUNET_GNS_TLD, name) != 0))
3353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3354 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3359 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3360 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3361 rh->authority = zone;
3365 rh->timeout = timeout;
3366 rh->private_local_zone = pzone;
3367 rh->only_cached = only_cached;
3369 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3373 "No shorten key for resolution\n");
3375 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3378 * Set timeout for authority lookup phase to 1/2
3380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3381 "Timeout for lookup set to %s/2\n",
3382 GNUNET_STRINGS_relative_time_to_string (rh->timeout, GNUNET_YES));
3383 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
3384 GNUNET_TIME_relative_divide(timeout, 2),
3385 &handle_lookup_timeout,
3387 rh->timeout_cont = &dht_authority_lookup_timeout;
3388 rh->timeout_cont_cls = rh;
3392 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3393 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3396 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3399 * Only '.gads' given
3401 strcpy (rh->name, "\0");
3405 if (is_zkey_tld(name) == GNUNET_YES)
3407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3410 * This is a zkey tld
3411 * build hash and use as initial authority
3414 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3415 memcpy(rh->name, name,
3416 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3417 pop_tld (rh->name, string_hash);
3419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3420 "ZKEY is %s!\n", string_hash);
3422 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3424 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3427 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3428 "Cannot convert ZKEY `%s' to hash!\n", string_hash);
3430 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3431 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3432 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3435 proc (cls, 0, NULL);
3440 else if (is_gads_tld (name) == GNUNET_YES)
3442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3445 * Presumably GADS tld
3447 memcpy (rh->name, name,
3448 strlen (name) - strlen(GNUNET_GNS_TLD) - 1);
3449 rh->name[strlen (name) - strlen(GNUNET_GNS_TLD) - 1] = '\0';
3453 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3454 _("Not a GADS TLD: `%s'\n"),
3456 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3457 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3458 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3461 proc (cls, 0, NULL);
3467 * Initialize authority chain
3469 rh->authority_chain_head = GNUNET_malloc (sizeof(struct AuthorityChain));
3470 rh->authority_chain_tail = rh->authority_chain_head;
3471 rh->authority_chain_head->zone = rh->authority;
3472 strcpy (rh->authority_chain_head->name, "");
3475 * Copy original query into lookup handle
3477 rlh->record_type = record_type;
3478 memset(rlh->name, 0, strlen(name) + 1);
3479 strcpy(rlh->name, name);
3481 rlh->proc_cls = cls;
3483 rh->proc = &handle_delegation_ns;
3484 resolve_delegation_ns (rh);
3487 /******** END Record Resolver ***********/
3490 finish_shorten (struct ResolverHandle *rh,
3491 struct NameShortenHandle *nsh)
3493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3494 "Sending %s as shorten result\n", nsh->result);
3495 nsh->proc (nsh->proc_cls, nsh->result);
3496 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3498 free_resolver_handle (rh);
3503 * Callback calles by namestore for a zone to name
3506 * @param cls the closure
3507 * @param zone_key the zone we queried
3508 * @param expire the expiration time of the name
3509 * @param name the name found or NULL
3510 * @param rd_len number of records for the name
3511 * @param rd the record data (PKEY) for the name
3512 * @param signature the signature for the record data
3515 process_zone_to_name_shorten_root (void *cls,
3516 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
3517 struct GNUNET_TIME_Absolute expire,
3519 unsigned int rd_len,
3520 const struct GNUNET_NAMESTORE_RecordData *rd,
3521 const struct GNUNET_CRYPTO_EccSignature *signature);
3525 * Callback called by namestore for a zone to name
3528 * @param cls the closure
3529 * @param zone_key the zone we queried
3530 * @param expire the expiration time of the name
3531 * @param name the name found or NULL
3532 * @param rd_len number of records for the name
3533 * @param rd the record data (PKEY) for the name
3534 * @param signature the signature for the record data
3537 process_zone_to_name_shorten_shorten (void *cls,
3538 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
3539 struct GNUNET_TIME_Absolute expire,
3541 unsigned int rd_len,
3542 const struct GNUNET_NAMESTORE_RecordData *rd,
3543 const struct GNUNET_CRYPTO_EccSignature *signature)
3545 struct ResolverHandle *rh = cls;
3546 struct NameShortenHandle* nsh = rh->proc_cls;
3547 struct AuthorityChain *next_authority;
3549 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3550 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3553 rh->namestore_task = NULL;
3554 /* we found a match in our own root zone */
3557 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3558 memset(result, 0, answer_len);
3560 if (strlen(rh->name) > 0)
3562 sprintf (result, "%s.%s.%s.%s",
3564 nsh->shorten_zone_name,
3569 sprintf (result, "%s.%s.%s", name,
3570 nsh->shorten_zone_name,
3574 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3575 "Found shorten result %s\n", result);
3576 if (strlen (nsh->result) > strlen (result))
3577 strcpy (nsh->result, result);
3579 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3580 nsh->shorten_zone) == 0)
3583 * This is our zone append .gads unless name is empty
3584 * (it shouldn't be, usually FIXME what happens if we
3585 * shorten to our zone to a "" record??)
3588 sprintf (result, "%s.%s.%s",
3590 nsh->shorten_zone_name,
3592 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3593 "Our zone: Found %s as shorten result\n", result);
3595 if (strlen (nsh->result) > strlen (result))
3596 strcpy (nsh->result, result);
3597 //nsh->proc(nsh->proc_cls, result);
3599 //free_resolver_handle(rh);
3606 * continue with next authority if exists
3608 if (NULL == rh->authority_chain_head->next)
3610 finish_shorten (rh, nsh);
3613 next_authority = rh->authority_chain_head;
3615 if (0 == strcmp (rh->name, ""))
3616 strcpy (tmp_name, next_authority->name);
3618 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3619 "%s.%s", rh->name, next_authority->name);
3621 strcpy(rh->name, tmp_name);
3622 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3623 "No PSEU found for authority %s. Promoting back: %s\n",
3624 next_authority->name, rh->name);
3626 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3627 rh->authority_chain_tail,
3630 GNUNET_free (next_authority);
3632 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3633 &rh->authority_chain_tail->zone,
3634 &rh->authority_chain_head->zone,
3635 &process_zone_to_name_shorten_root,
3641 * Callback calles by namestore for a zone to name
3644 * @param cls the closure
3645 * @param zone_key the zone we queried
3646 * @param expire the expiration time of the name
3647 * @param name the name found or NULL
3648 * @param rd_len number of records for the name
3649 * @param rd the record data (PKEY) for the name
3650 * @param signature the signature for the record data
3653 process_zone_to_name_shorten_private (void *cls,
3654 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
3655 struct GNUNET_TIME_Absolute expire,
3657 unsigned int rd_len,
3658 const struct GNUNET_NAMESTORE_RecordData *rd,
3659 const struct GNUNET_CRYPTO_EccSignature *signature)
3661 struct ResolverHandle *rh = cls;
3662 struct NameShortenHandle* nsh = rh->proc_cls;
3663 struct AuthorityChain *next_authority;
3665 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3666 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3669 rh->namestore_task = NULL;
3670 /* we found a match in our own root zone */
3673 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3674 memset(result, 0, answer_len);
3676 if (strlen(rh->name) > 0)
3678 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3682 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3685 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3686 "Found shorten result %s\n", result);
3687 if (strlen (nsh->result) > strlen (result))
3688 strcpy (nsh->result, result);
3690 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3691 nsh->private_zone) == 0)
3694 * This is our zone append .gads unless name is empty
3695 * (it shouldn't be, usually FIXME what happens if we
3696 * shorten to our zone to a "" record??)
3699 sprintf (result, "%s.%s.%s",
3700 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3701 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3702 "Our private zone: Found %s as shorten result %s\n", result);
3703 if (strlen (nsh->result) > strlen (result))
3704 strcpy (nsh->result, result);
3707 if (0 != strcmp (nsh->shorten_zone_name, ""))
3709 /* backtrack authorities for names in priv zone */
3710 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3712 &rh->authority_chain_head->zone,
3713 &process_zone_to_name_shorten_shorten,
3720 * continue with next authority if exists
3722 if (NULL == rh->authority_chain_head->next)
3724 finish_shorten (rh, nsh);
3727 next_authority = rh->authority_chain_head;
3729 if (0 == strcmp (rh->name, ""))
3730 strcpy (tmp_name, next_authority->name);
3732 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3733 "%s.%s", rh->name, next_authority->name);
3735 strcpy(rh->name, tmp_name);
3736 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3737 "No PSEU found for authority %s. Promoting back: %s\n",
3738 next_authority->name, rh->name);
3740 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3741 rh->authority_chain_tail,
3744 GNUNET_free (next_authority);
3746 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3747 &rh->authority_chain_tail->zone,
3748 &rh->authority_chain_head->zone,
3749 &process_zone_to_name_shorten_root,
3756 * Callback calles by namestore for a zone to name
3759 * @param cls the closure
3760 * @param zone_key the zone we queried
3761 * @param expire the expiration time of the name
3762 * @param name the name found or NULL
3763 * @param rd_len number of records for the name
3764 * @param rd the record data (PKEY) for the name
3765 * @param signature the signature for the record data
3768 process_zone_to_name_shorten_root (void *cls,
3769 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
3770 struct GNUNET_TIME_Absolute expire,
3772 unsigned int rd_len,
3773 const struct GNUNET_NAMESTORE_RecordData *rd,
3774 const struct GNUNET_CRYPTO_EccSignature *signature)
3776 struct ResolverHandle *rh = cls;
3777 struct NameShortenHandle* nsh = rh->proc_cls;
3778 struct AuthorityChain *next_authority;
3779 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3780 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3783 rh->namestore_task = NULL;
3784 /* we found a match in our own root zone */
3787 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3788 memset(result, 0, answer_len);
3790 if (strlen(rh->name) > 0)
3792 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3796 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3799 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3800 "Found shorten result %s\n", result);
3801 if (strlen (nsh->result) > strlen (result))
3802 strcpy (nsh->result, result);
3804 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3805 nsh->root_zone) == 0)
3808 * This is our zone append .gads unless name is empty
3809 * (it shouldn't be, usually FIXME what happens if we
3810 * shorten to our zone to a "" record??)
3813 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3814 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3815 "Our zone: Found %s as shorten result\n", result);
3816 if (strlen (nsh->result) > strlen (result))
3817 strcpy (nsh->result, result);
3820 if (NULL != nsh->private_zone)
3822 /* backtrack authorities for names in priv zone */
3823 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3825 &rh->authority_chain_head->zone,
3826 &process_zone_to_name_shorten_private,
3829 else if (NULL != nsh->shorten_zone)
3831 /* backtrack authorities for names in shorten zone */
3832 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3834 &rh->authority_chain_head->zone,
3835 &process_zone_to_name_shorten_shorten,
3842 * continue with next authority if exists
3844 if (NULL == rh->authority_chain_head->next)
3846 finish_shorten (rh, nsh);
3849 next_authority = rh->authority_chain_head;
3851 if (0 == strcmp (rh->name, ""))
3852 strcpy (tmp_name, next_authority->name);
3854 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3855 "%s.%s", rh->name, next_authority->name);
3857 strcpy(rh->name, tmp_name);
3858 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3859 "No PSEU found for authority %s. Promoting back: %s\n",
3860 next_authority->name, rh->name);
3862 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3863 rh->authority_chain_tail,
3866 GNUNET_free (next_authority);
3868 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3869 &rh->authority_chain_tail->zone,
3870 &rh->authority_chain_head->zone,
3871 &process_zone_to_name_shorten_root,
3878 * Process result from namestore delegation lookup
3879 * for shorten operation
3881 * @param cls the client shorten handle
3882 * @param rh the resolver handle
3883 * @param rd_count number of results (0)
3884 * @param rd data (NULL)
3887 handle_delegation_ns_shorten (void* cls,
3888 struct ResolverHandle *rh,
3890 const struct GNUNET_NAMESTORE_RecordData *rd)
3892 struct NameShortenHandle *nsh;
3893 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3895 nsh = (struct NameShortenHandle *)cls;
3896 rh->namestore_task = NULL;
3898 * At this point rh->name contains the part of the name
3899 * that we do not have a PKEY in our namestore to resolve.
3900 * The authority chain in the resolver handle is now
3901 * useful to backtrack if needed
3904 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3905 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3906 memset(result, 0, sizeof (result));
3908 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3912 * This is our zone append .gads unless name is empty
3913 * (it shouldn't be, usually FIXME what happens if we
3914 * shorten to our zone to a "" record??)
3917 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3918 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3919 "Our zone: Found %s as shorten result\n", result);
3921 if (strlen (nsh->result) > strlen (result))
3922 strcpy (nsh->result, result);
3925 else if (NULL != nsh->private_zone)
3928 * This is our zone append .gads unless name is empty
3929 * (it shouldn't be, usually FIXME what happens if we
3930 * shorten to our zone to a "" record??)
3932 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3933 nsh->private_zone) == 0)
3936 sprintf (result, "%s.%s.%s",
3937 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3938 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3939 "Our zone: Found %s as shorten result in private zone %s\n",
3942 if (strlen (nsh->result) > strlen (result))
3943 strcpy (nsh->result, result);
3946 else if (NULL != nsh->shorten_zone)
3949 * This is our zone append .gads unless name is empty
3950 * (it shouldn't be, usually FIXME what happens if we
3951 * shorten to our zone to a "" record??)
3953 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3954 nsh->shorten_zone) == 0)
3956 sprintf (result, "%s.%s.%s",
3957 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3958 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3959 "Our zone: Found %s as shorten result in shorten zone\n",
3962 if (strlen (nsh->result) > strlen (result))
3963 strcpy (nsh->result, result);
3968 /* backtrack authorities for names */
3969 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3971 &rh->authority_chain_head->zone,
3972 &process_zone_to_name_shorten_root,
3979 * Callback calles by namestore for a zone to name
3982 * @param cls the closure
3983 * @param zone_key the zone we queried
3984 * @param expire the expiration time of the name
3985 * @param name the name found or NULL
3986 * @param rd_len number of records for the name
3987 * @param rd the record data (PKEY) for the name
3988 * @param signature the signature for the record data
3991 process_zone_to_name_zkey(void *cls,
3992 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
3993 struct GNUNET_TIME_Absolute expire,
3995 unsigned int rd_len,
3996 const struct GNUNET_NAMESTORE_RecordData *rd,
3997 const struct GNUNET_CRYPTO_EccSignature *signature)
3999 struct ResolverHandle *rh = cls;
4000 struct NameShortenHandle *nsh = rh->proc_cls;
4001 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
4003 rh->namestore_task = NULL;
4005 /* zkey not in our zone */
4009 * In this case we have not given this PKEY a name (yet)
4010 * It is either just not in our zone or not even cached
4011 * Since we do not know at this point we will not try to shorten
4012 * because PKEY import will happen if the user follows the zkey
4015 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4016 "No name found for zkey %s returning verbatim!\n", nsh->result);
4017 /*if (strcmp(rh->name, "") != 0)
4018 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s.%s",
4019 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4021 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
4022 enc, GNUNET_GNS_TLD_ZKEY);
4024 strcpy (nsh->result, new_name);*/
4026 finish_shorten (rh, nsh);
4030 if (strcmp(rh->name, "") != 0)
4031 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
4034 strcpy(new_name, name);
4036 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4037 "Continue shorten for %s!\n", new_name);
4039 strcpy(rh->name, new_name);
4041 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4042 rh->authority_chain_tail = rh->authority_chain_head;
4043 rh->authority_chain_head->zone = rh->authority;
4046 /* Start delegation resolution in our namestore */
4047 resolve_delegation_ns (rh);
4052 * Shorten api from resolver
4054 * @param zone the root zone to use
4055 * @param pzone the private zone to use
4056 * @param szone the shorten zone to use
4057 * @param name the name to shorten
4058 * @param private_zone_name name of the private zone
4059 * @param shorten_zone_name name of the shorten zone
4060 * @param proc the processor to call with result
4061 * @param proc_cls closure to pass to proc
4064 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4065 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4066 struct GNUNET_CRYPTO_ShortHashCode *szone,
4068 const char* private_zone_name,
4069 const char* shorten_zone_name,
4070 ShortenResultProcessor proc,
4073 struct ResolverHandle *rh;
4074 struct NameShortenHandle *nsh;
4075 char string_hash[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
4076 struct GNUNET_CRYPTO_ShortHashCode zkey;
4077 char nzkey[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
4078 char* nzkey_ptr = nzkey;
4080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4081 "Starting shorten for %s!\n", name);
4083 if (is_canonical ((char*)name) == GNUNET_YES)
4085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4086 "%s is canonical. Returning verbatim\n", name);
4087 proc (proc_cls, name);
4091 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4093 nsh->proc_cls = proc_cls;
4094 nsh->root_zone = zone;
4095 nsh->private_zone = pzone;
4096 nsh->shorten_zone = szone;
4097 strcpy (nsh->private_zone_name, private_zone_name);
4098 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4099 strcpy (nsh->result, name);
4101 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4102 rh->authority = *zone;
4104 rh->proc = &handle_delegation_ns_shorten;
4106 rh->private_local_zone = *zone;
4108 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4110 "Checking for TLD...\n");
4111 if (is_zkey_tld (name) == GNUNET_YES)
4113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4116 * This is a zkey tld
4117 * build hash and use as initial authority
4120 memset (rh->name, 0,
4121 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4122 memcpy (rh->name, name,
4123 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4124 pop_tld (rh->name, string_hash);
4126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4127 "ZKEY is %s!\n", string_hash);
4129 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4131 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4135 "Cannot convert ZKEY %s to hash!\n", nzkey);
4136 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4139 proc (proc_cls, name);
4142 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4145 &process_zone_to_name_zkey,
4150 else if (is_gads_tld (name) == GNUNET_YES)
4152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4155 * Presumably GNUNET tld
4157 memset (rh->name, 0,
4158 strlen (name)-strlen (GNUNET_GNS_TLD));
4159 memcpy (rh->name, name,
4160 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4165 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4168 proc (proc_cls, name);
4172 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4173 rh->authority_chain_tail = rh->authority_chain_head;
4174 rh->authority_chain_head->zone = *zone;
4176 /* Start delegation resolution in our namestore */
4177 resolve_delegation_ns (rh);
4180 /*********** END NAME SHORTEN ********************/
4183 * Conclude get authority lookup
4185 * @param rh resolver handle
4186 * @param nah get authority lookup handle
4189 finish_get_auth (struct ResolverHandle *rh,
4190 struct GetNameAuthorityHandle *nah)
4192 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4193 "Got authority result %s\n", nah->result);
4195 nah->proc (nah->proc_cls, nah->result);
4196 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4198 free_resolver_handle (rh);
4203 * Process result from namestore delegation lookup
4204 * for get authority operation
4206 * @param cls the client get auth handle
4207 * @param rh the resolver handle
4208 * @param rd_count number of results (0)
4209 * @param rd data (NULL)
4212 handle_delegation_result_ns_get_auth(void* cls,
4213 struct ResolverHandle *rh,
4215 const struct GNUNET_NAMESTORE_RecordData *rd)
4217 struct GetNameAuthorityHandle* nah = rh->proc_cls;
4221 * At this point rh->name contains the part of the name
4222 * that we do not have a PKEY in our namestore to resolve.
4223 * The authority chain in the resolver handle is now
4224 * useful to backtrack if needed
4227 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4228 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4230 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4231 "Building response!\n");
4232 if (is_canonical (rh->name) == GNUNET_YES)
4235 * We successfully resolved the authority in the ns
4236 * FIXME for our purposes this is fine
4237 * but maybe we want to have an api that also looks
4238 * into the dht (i.e. option in message)
4240 if (strlen(rh->name) > strlen(nah->name))
4242 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4243 "Record name longer than original lookup name... odd!\n");
4247 answer_len = strlen(nah->name) - strlen(rh->name)
4248 + strlen(GNUNET_GNS_TLD) + 1;
4249 memset(nah->result, 0, answer_len);
4250 if (0 != strcmp (rh->name, ""))
4251 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4253 strcpy(nah->result, nah->name);
4255 finish_get_auth (rh, nah);
4259 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4260 "Unable to resolve authority for remaining %s!\n", rh->name);
4261 strcpy(nah->result, "");
4262 finish_get_auth (rh, nah);
4268 * Tries to resolve the authority for name
4271 * @param zone the root zone to look up for
4272 * @param pzone the private local zone
4273 * @param name the name to lookup up
4274 * @param proc the processor to call when finished
4275 * @param proc_cls the closure to pass to the processor
4278 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4279 struct GNUNET_CRYPTO_ShortHashCode pzone,
4281 GetAuthorityResultProcessor proc,
4284 struct ResolverHandle *rh;
4285 struct GetNameAuthorityHandle *nah;
4287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4288 "Starting authority resolution for %s!\n", name);
4290 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4291 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4292 rh->authority = zone;
4294 rh->private_local_zone = pzone;
4296 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4298 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4300 strcpy(rh->name, "\0");
4305 strlen(name)-strlen(GNUNET_GNS_TLD));
4306 memcpy(rh->name, name,
4307 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4310 memset(nah->name, 0,
4312 strcpy(nah->name, name);
4314 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4315 rh->authority_chain_tail = rh->authority_chain_head;
4316 rh->authority_chain_head->zone = zone;
4317 rh->proc = &handle_delegation_result_ns_get_auth;
4318 rh->proc_cls = (void*)nah;
4321 nah->proc_cls = proc_cls;
4322 strcpy (nah->result, "");
4324 /* Start delegation resolution in our namestore */
4325 resolve_delegation_ns(rh);
4329 /******** END GET AUTHORITY *************/
4331 /* end of gnunet-service-gns_resolver.c */