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_create (namestore_handle,
313 * process result of a dht pseu lookup
315 * @param gph the handle
316 * @param name the pseu result or NULL
319 process_pseu_result (struct GetPseuAuthorityHandle* gph,
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 "GNS_AUTO_PSEU: No PSEU, no shorten. Finished.\n");
326 free_get_pseu_authority_handle (gph);
330 memcpy (gph->test_name, name, strlen(name) + 1);
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
335 * Check for collision
337 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
340 GNUNET_NAMESTORE_TYPE_ANY,
341 &process_pseu_lookup_ns,
347 * Handle timeout for dht request
349 * @param cls the request handle as closure
350 * @param tc the task context
353 handle_auth_discovery_timeout (void *cls,
354 const struct GNUNET_SCHEDULER_TaskContext *tc)
356 struct GetPseuAuthorityHandle* gph = cls;
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
360 GNUNET_DHT_get_stop (gph->get_handle);
361 gph->get_handle = NULL;
362 process_pseu_result (gph, NULL);
367 * Function called when we find a PSEU entry in the DHT
369 * @param cls the request handle
370 * @param exp lifetime
371 * @param key the key the record was stored under
372 * @param get_path get path
373 * @param get_path_length get path length
374 * @param put_path put path
375 * @param put_path_length put path length
376 * @param type the block type
377 * @param size the size of the record
378 * @param data the record data
381 process_auth_discovery_dht_result (void* cls,
382 struct GNUNET_TIME_Absolute exp,
383 const struct GNUNET_HashCode * key,
384 const struct GNUNET_PeerIdentity *get_path,
385 unsigned int get_path_length,
386 const struct GNUNET_PeerIdentity *put_path,
387 unsigned int put_path_length,
388 enum GNUNET_BLOCK_Type type,
392 struct GetPseuAuthorityHandle* gph = cls;
393 struct GNSNameRecordBlock *nrb;
394 const char* rd_data = data;
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
403 /* stop lookup and timeout task */
404 GNUNET_DHT_get_stop (gph->get_handle);
405 gph->get_handle = NULL;
406 GNUNET_SCHEDULER_cancel (gph->timeout);
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
411 "GNS_GET_AUTH: got dht result null!\n", size);
412 free_get_pseu_authority_handle (gph);
416 nrb = (struct GNSNameRecordBlock*)data;
417 name = (char*)&nrb[1];
418 num_records = ntohl (nrb->rd_count);
420 struct GNUNET_NAMESTORE_RecordData rd[num_records];
422 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
423 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
425 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
430 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
431 "GNS_GET_AUTH: Error deserializing data!\n");
435 for (i=0; i < num_records; i++)
437 if ((strcmp (name, GNUNET_GNS_MASTERZONE_STR) == 0) &&
438 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
441 process_pseu_result (gph, (char*)rd[i].data);
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 "GNS_GET_AUTH: finished shorten, no results!\n");
449 process_pseu_result (gph, NULL);
454 * Process PSEU discovery for shorten via namestore
456 * @param cls the GetPseuAuthorityHandle
457 * @param key the public key
458 * @param expiration recorddata expiration
459 * @param name the looked up name
460 * @param rd_count number of records in set
461 * @param rd record data
462 * @param signature the signature
465 process_auth_discovery_ns_result (void* cls,
466 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *key,
467 struct GNUNET_TIME_Absolute expiration,
469 unsigned int rd_count,
470 const struct GNUNET_NAMESTORE_RecordData *rd,
471 const struct GNUNET_CRYPTO_EccSignature *signature)
473 struct GetPseuAuthorityHandle* gph = cls;
474 struct GNUNET_HashCode lookup_key;
478 gph->namestore_task = NULL;
482 GNUNET_GNS_get_key_for_record (GNUNET_GNS_TLD_PLUS, &gph->auth->zone, &lookup_key);
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
486 GNUNET_h2s (&lookup_key));
488 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
489 &handle_auth_discovery_timeout, gph);
491 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
493 GNUNET_assert (gph->get_handle == NULL);
495 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
496 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
498 DHT_GNS_REPLICATION_LEVEL,
499 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
502 &process_auth_discovery_dht_result,
507 for (i=0; i < rd_count; i++)
509 if (0 != (strcmp (name, GNUNET_GNS_TLD_PLUS)))
512 if (rd[i].record_type != GNUNET_GNS_RECORD_PSEU)
516 process_pseu_result (gph, (char*)rd[i].data);
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
521 process_pseu_result (gph, NULL);
526 * Callback called by namestore for a zone to name
529 * @param cls the closure
530 * @param zone_key the zone we queried
531 * @param expire the expiration time of the name
532 * @param name the name found or NULL
533 * @param rd_len number of records for the name
534 * @param rd the record data (PKEY) for the name
535 * @param signature the signature for the record data
538 process_zone_to_name_discover (void *cls,
539 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
540 struct GNUNET_TIME_Absolute expire,
543 const struct GNUNET_NAMESTORE_RecordData *rd,
544 const struct GNUNET_CRYPTO_EccSignature *signature)
546 struct GetPseuAuthorityHandle* gph = cls;
548 gph->namestore_task = NULL;
551 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
553 GNUNET_GNS_MASTERZONE_STR,
554 GNUNET_GNS_RECORD_PSEU,
555 &process_auth_discovery_ns_result,
559 /* we found a match in our own zone */
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
562 free_get_pseu_authority_handle (gph);
567 * Callback that shortens authorities
569 * @param gph the handle to the shorten request
572 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "GNS_AUTO_PSEU: New authority %s discovered\n",
578 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
581 &process_zone_to_name_discover,
587 * Start shortening algorithm using auth as
590 * @param auth the authorities that were resolved
591 * @param key the private key for PKEY import
594 start_shorten (struct AuthorityChain *auth,
595 const struct GNUNET_CRYPTO_EccPrivateKey *key)
597 struct GetPseuAuthorityHandle *gph;
598 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pkey;
599 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *pb_key;
601 GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
602 if (NULL == (pb_key = GNUNET_CRYPTO_ecc_encode_key (key)))
604 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
605 "Failed to encode ECC key on shorten\n");
608 gph = GNUNET_new (struct GetPseuAuthorityHandle);
609 gph->key = GNUNET_CRYPTO_ecc_decode_key ((const char*) pb_key,
610 ntohs (pb_key->size),
612 GNUNET_free (pb_key);
613 if (NULL == gph->key)
615 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
616 "Failed to decode ECC key on shorten\n");
620 GNUNET_CRYPTO_short_hash (&pkey,
621 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
623 gph->auth = GNUNET_malloc (sizeof (struct AuthorityChain));
624 memcpy (gph->auth, auth, sizeof (struct AuthorityChain));
625 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
626 shorten_authority_chain (gph);
631 * Initialize the resolver
633 * @param nh the namestore handle
634 * @param dh the dht handle
635 * @param lz the local zone's hash
636 * @param c configuration handle
637 * @param max_bg_queries maximum number of parallel background queries in dht
638 * @param ignore_pending ignore records that still require user confirmation
640 * @return GNUNET_OK on success
643 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
644 struct GNUNET_DHT_Handle *dh,
645 struct GNUNET_CRYPTO_ShortHashCode lz,
646 const struct GNUNET_CONFIGURATION_Handle *c,
647 unsigned long long max_bg_queries,
652 return GNUNET_SYSERR;
655 namestore_handle = nh;
659 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
661 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
662 max_allowed_background_queries = max_bg_queries;
663 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
664 ignore_pending_records = ignore_pending;
665 GNUNET_RESOLVER_connect (cfg);
672 * @param rh resolver handle
673 * @param rlh record lookup handle
674 * @param rd_count number of results
678 finish_lookup (struct ResolverHandle *rh,
679 struct RecordLookupHandle* rlh,
680 unsigned int rd_count,
681 const struct GNUNET_NAMESTORE_RecordData *rd);
685 * Helper function to free resolver handle
687 * @param rh the handle to free
690 free_resolver_handle (struct ResolverHandle* rh)
692 struct AuthorityChain *ac;
693 struct AuthorityChain *ac_next;
698 ac_next = rh->authority_chain_head;
699 while (NULL != (ac = ac_next))
705 if (NULL != rh->get_handle)
706 GNUNET_DHT_get_stop (rh->get_handle);
707 if (NULL != rh->dns_raw_packet)
708 GNUNET_free (rh->dns_raw_packet);
709 if (NULL != rh->namestore_task)
711 GNUNET_NAMESTORE_cancel (rh->namestore_task);
712 rh->namestore_task = NULL;
714 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
715 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
716 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
717 GNUNET_SCHEDULER_cancel (rh->timeout_task);
718 if (NULL != rh->dns_sock)
719 GNUNET_NETWORK_socket_close (rh->dns_sock);
720 if (NULL != rh->dns_resolver_handle)
721 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
722 if (NULL != rh->rd.data)
723 GNUNET_free ((void*)(rh->rd.data));
724 if (NULL != rh->dht_heap_node)
725 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
733 * @param rh resolver handle
734 * @param nsh name shorten handle
737 finish_shorten (struct ResolverHandle *rh,
738 struct NameShortenHandle *nsh);
744 * @param rh resolver handle
745 * @param nah get name authority handle
748 finish_get_auth (struct ResolverHandle *rh,
749 struct GetNameAuthorityHandle* rlh);
756 gns_resolver_cleanup ()
758 struct GetPseuAuthorityHandle *tmp;
759 struct ResolverHandle *rh;
760 struct NamestoreBGTask *nbg;
762 while (NULL != (tmp = gph_head))
764 if (tmp->get_handle != NULL)
766 GNUNET_DHT_get_stop (tmp->get_handle);
767 tmp->get_handle = NULL;
769 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
771 GNUNET_SCHEDULER_cancel (tmp->timeout);
772 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
774 if (NULL != tmp->namestore_task)
776 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
777 tmp->namestore_task = NULL;
779 free_get_pseu_authority_handle (tmp);
782 while (NULL != rlh_head)
784 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
786 while (NULL != nsh_head)
788 finish_shorten (nsh_head, nsh_head->proc_cls);
790 while (NULL != nah_head)
792 finish_get_auth (nah_head, nah_head->proc_cls);
795 while (NULL != (rh = GNUNET_CONTAINER_heap_remove_root(dht_lookup_heap)))
799 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
800 dht_lookup_heap = NULL;
802 while (NULL != (nbg = GNUNET_CONTAINER_heap_remove_root(ns_task_heap)))
804 GNUNET_NAMESTORE_cancel (nbg->qe);
807 GNUNET_CONTAINER_heap_destroy (ns_task_heap);
814 * Callback when record data is put into namestore
816 * @param cls the closure
817 * @param success GNUNET_OK on success
818 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
821 on_namestore_record_put_result (void *cls,
825 struct NamestoreBGTask *nbg = cls;
827 GNUNET_CONTAINER_heap_remove_node (nbg->node);
830 if (GNUNET_NO == success)
832 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
833 "GNS_NS: records already in namestore\n");
836 else if (GNUNET_YES == success)
838 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
839 "GNS_NS: records successfully put in namestore\n");
843 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
844 "GNS_NS: Error putting records into namestore: %s\n", emsg);
849 * Lookup timeout task
851 * @param cls the ResolverHandle for the task that timed out
852 * @param tc the task context
855 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
857 struct ResolverHandle *rh = cls;
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "Lookup timeout for request %llu triggered\n",
862 if (NULL != rh->timeout_cont)
863 rh->timeout_cont (rh->timeout_cont_cls, tc);
868 * Processor for background lookups in the DHT
870 * @param cls closure (NULL)
871 * @param rd_count number of records found (not 0)
872 * @param rd record data
875 background_lookup_result_processor (void *cls,
877 const struct GNUNET_NAMESTORE_RecordData *rd)
879 //We could do sth verbose/more useful here but it doesn't make any difference
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "GNS_BG: background dht lookup finished. (%d results)\n",
887 * Handle timeout for DHT requests
889 * @param cls the request handle as closure
890 * @param tc the task context
893 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
895 struct ResolverHandle *rh = cls;
896 struct RecordLookupHandle *rlh = rh->proc_cls;
897 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus) timed out.\n",
901 rh->id, rh->name, rh->timeout.rel_value);
903 * Start resolution in bg
905 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
906 rh->name, GNUNET_GNS_TLD);
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
909 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
910 rh->id, new_name, rlh->record_type);
912 gns_resolver_lookup_record (rh->authority,
913 rh->private_local_zone,
917 GNUNET_TIME_UNIT_FOREVER_REL,
919 &background_lookup_result_processor,
922 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
924 GNUNET_DHT_get_stop (rh->get_handle);
925 rh->get_handle = NULL;
926 rh->proc (rh->proc_cls, rh, 0, NULL);
931 * Function called when we get a result from the dht
932 * for our record query
934 * @param cls the request handle
935 * @param exp lifetime
936 * @param key the key the record was stored under
937 * @param get_path get path
938 * @param get_path_length get path length
939 * @param put_path put path
940 * @param put_path_length put path length
941 * @param type the block type
942 * @param size the size of the record
943 * @param data the record data
946 process_record_result_dht (void* cls,
947 struct GNUNET_TIME_Absolute exp,
948 const struct GNUNET_HashCode * key,
949 const struct GNUNET_PeerIdentity *get_path,
950 unsigned int get_path_length,
951 const struct GNUNET_PeerIdentity *put_path,
952 unsigned int put_path_length,
953 enum GNUNET_BLOCK_Type type,
954 size_t size, const void *data)
956 struct ResolverHandle *rh = cls;
957 struct RecordLookupHandle *rlh = rh->proc_cls;
958 const struct GNSNameRecordBlock *nrb = data;
959 uint32_t num_records;
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
967 /* stop lookup and timeout task */
968 GNUNET_DHT_get_stop (rh->get_handle);
969 rh->get_handle = NULL;
970 if (rh->dht_heap_node != NULL)
972 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
973 rh->dht_heap_node = NULL;
975 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
977 GNUNET_SCHEDULER_cancel (rh->timeout_task);
978 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
980 rh->get_handle = NULL;
981 name = (const char*) &nrb[1];
982 num_records = ntohl (nrb->rd_count);
984 struct GNUNET_NAMESTORE_RecordData rd[num_records];
985 struct NamestoreBGTask *ns_heap_root;
986 struct NamestoreBGTask *namestore_bg_task;
988 rd_data = &name[strlen (name) + 1];
989 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
991 GNUNET_NAMESTORE_records_deserialize (rd_size,
996 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
997 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
998 rh->proc (rh->proc_cls, rh, 0, NULL);
1001 for (i = 0; i < num_records; i++)
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1004 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1005 rh->id, name, rh->name);
1006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1007 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1008 rh->id, rd[i].record_type, rlh->record_type);
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1011 rh->id, rd[i].data_size);
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1013 "GNS_PHASE_REC-%llu: Got flag %d\n",
1014 rh->id, rd[i].flags);
1016 if ((strcmp (name, rh->name) == 0) &&
1017 (rd[i].record_type == rlh->record_type))
1023 * FIXME check pubkey against existing key in namestore?
1024 * https://gnunet.org/bugs/view.php?id=2179
1026 if (max_allowed_ns_tasks <=
1027 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1029 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1030 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1037 /* Save to namestore */
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1039 "GNS_PHASE_REC-%llu: Caching record for %s\n",
1041 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1042 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1049 &on_namestore_record_put_result, //cont
1052 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1054 GNUNET_TIME_absolute_get().abs_value);
1055 if (0 < rh->answered)
1056 rh->proc (rh->proc_cls, rh, num_records, rd);
1058 rh->proc (rh->proc_cls, rh, 0, NULL);
1064 * Start DHT lookup for a (name -> query->record_type) record in
1065 * rh->authority's zone
1067 * @param rh the pending gns query context
1070 resolve_record_dht (struct ResolverHandle *rh)
1072 struct RecordLookupHandle *rlh = rh->proc_cls;
1074 struct GNUNET_HashCode lookup_key;
1075 struct ResolverHandle *rh_heap_root;
1077 GNUNET_GNS_get_key_for_record (rh->name, &rh->authority, &lookup_key);
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1080 rh->id, rh->name, GNUNET_h2s (&lookup_key));
1082 rh->dht_heap_node = NULL;
1084 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
1087 * Update timeout if necessary
1089 if (GNUNET_SCHEDULER_NO_TASK == rh->timeout_task)
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1092 "GNS_PHASE_REC-%llu: Adjusting timeout to %s/2\n",
1094 GNUNET_STRINGS_relative_time_to_string (rh->timeout, GNUNET_YES));
1096 * Set timeout for authority lookup phase to 1/2
1098 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1099 GNUNET_TIME_relative_divide (rh->timeout, 2),
1100 &handle_lookup_timeout,
1103 rh->timeout_cont = &dht_lookup_timeout;
1104 rh->timeout_cont_cls = rh;
1108 if (max_allowed_background_queries <=
1109 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1111 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1112 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1113 rh_heap_root->get_handle = NULL;
1114 rh_heap_root->dht_heap_node = NULL;
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1118 rh->id, rh_heap_root->name);
1119 rh_heap_root->proc (rh_heap_root->proc_cls,
1124 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1126 GNUNET_TIME_absolute_get ().abs_value);
1129 xquery = htonl (rlh->record_type);
1131 GNUNET_assert (rh->get_handle == NULL);
1132 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1133 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1135 DHT_GNS_REPLICATION_LEVEL,
1136 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1139 &process_record_result_dht,
1146 * Namestore calls this function if we have record for this name.
1147 * (or with rd_count=0 to indicate no matches)
1149 * @param cls the pending query
1150 * @param key the key of the zone we did the lookup
1151 * @param expiration expiration date of the namestore entry
1152 * @param name the name for which we need an authority
1153 * @param rd_count the number of records with 'name'
1154 * @param rd the record data
1155 * @param signature the signature of the authority for the record data
1158 process_record_result_ns (void* cls,
1159 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *key,
1160 struct GNUNET_TIME_Absolute expiration,
1161 const char *name, unsigned int rd_count,
1162 const struct GNUNET_NAMESTORE_RecordData *rd,
1163 const struct GNUNET_CRYPTO_EccSignature *signature)
1165 struct ResolverHandle *rh = cls;
1166 struct RecordLookupHandle *rlh = rh->proc_cls;
1167 struct GNUNET_TIME_Relative remaining_time;
1168 struct GNUNET_CRYPTO_ShortHashCode zone;
1169 struct GNUNET_TIME_Absolute et;
1172 rh->namestore_task = NULL;
1173 GNUNET_CRYPTO_short_hash (key,
1174 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
1176 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1180 rh->status |= RSL_RECORD_EXISTS;
1181 if (remaining_time.rel_value == 0)
1182 rh->status |= RSL_RECORD_EXPIRED;
1187 * Lookup terminated and no results
1189 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1190 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1193 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1194 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1197 * Our zone and no result? Cannot resolve TT
1199 rh->proc(rh->proc_cls, rh, 0, NULL);
1203 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1204 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1206 for (i = 0; i < rd_count;i++)
1208 if (rd[i].record_type != rlh->record_type)
1211 if (ignore_pending_records &&
1212 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1214 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1215 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1220 //FIXME: eh? do I have to handle this here?
1221 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1222 et.abs_value = rd[i].expiration_time;
1223 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
1225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1226 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1236 if (0 == rh->answered)
1238 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1239 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1240 rh->proc(rh->proc_cls, rh, 0, NULL);
1244 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1245 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1246 rh->id, rh->answered, rd_count);
1247 rh->proc(rh->proc_cls, rh, rd_count, rd);
1253 * VPN redirect result callback
1255 * @param cls the resolver handle
1256 * @param af the requested address family
1257 * @param address in_addr(6) respectively
1260 process_record_result_vpn (void* cls, int af, const void *address)
1262 struct ResolverHandle *rh = cls;
1263 struct RecordLookupHandle *rlh = rh->proc_cls;
1264 struct GNUNET_NAMESTORE_RecordData rd;
1266 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1267 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1271 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1272 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1274 if (GNUNET_GNS_RECORD_A != rlh->record_type)
1276 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1277 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1279 rh->proc (rh->proc_cls, rh, 0, NULL);
1282 rd.record_type = GNUNET_GNS_RECORD_A;
1283 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1285 rd.data_size = sizeof (struct in_addr);
1287 rh->proc (rh->proc_cls, rh, 1, &rd);
1290 else if (AF_INET6 == af)
1292 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1293 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1295 if (GNUNET_GNS_RECORD_AAAA != rlh->record_type)
1297 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1298 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1300 rh->proc (rh->proc_cls, rh, 0, NULL);
1303 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1304 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1306 rd.data_size = sizeof (struct in6_addr);
1308 rh->proc (rh->proc_cls, rh, 1, &rd);
1312 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1313 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1315 rh->proc (rh->proc_cls, rh, 0, NULL);
1321 * Process VPN lookup result for record
1323 * @param cls the record lookup handle
1324 * @param rh resolver handle
1325 * @param rd_count number of results (1)
1326 * @param rd record data containing the result
1329 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1330 unsigned int rd_count,
1331 const struct GNUNET_NAMESTORE_RecordData *rd)
1333 struct RecordLookupHandle* rlh = cls;
1337 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1338 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1341 /* give up, cannot resolve */
1342 finish_lookup(rh, rlh, 0, NULL);
1346 /* results found yay */
1347 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1348 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!\n",
1351 finish_lookup(rh, rlh, rd_count, rd);
1356 * Sends a UDP dns query to a nameserver specified in the rh
1358 * @param rh the resolver handle
1361 send_dns_packet (struct ResolverHandle *rh);
1367 * @param cls the ResolverHandle for this lookup
1368 * @param addr the sockaddr
1369 * @param addrlen the socket address length
1372 handle_dns_resolver (void *cls,
1373 const struct sockaddr *addr,
1376 struct ResolverHandle *rh = cls;
1377 struct RecordLookupHandle *rlh = rh->proc_cls;
1378 struct GNUNET_NAMESTORE_RecordData rd;
1379 struct sockaddr_in *sai;
1380 struct sockaddr_in6 *sai6;
1384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1385 "No address found in DNS!\n");
1386 finish_lookup (rh, rlh, 0, NULL);
1390 if (sizeof (struct sockaddr_in) == addrlen)
1392 sai = (struct sockaddr_in*) addr;
1393 rd.record_type = GNUNET_GNS_RECORD_A;
1394 rd.data_size = sizeof (struct in_addr);
1395 rd.data = &sai->sin_addr;
1397 else if (sizeof (struct sockaddr_in6) == addrlen)
1399 sai6 = (struct sockaddr_in6*) addr;
1400 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1401 rd.data_size = sizeof (struct in6_addr);
1402 rd.data = &sai6->sin6_addr;
1406 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1407 "Address length is garbage!\n");
1408 finish_lookup (rh, rlh, 0, NULL);
1411 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1413 finish_lookup (rh, rlh, 1, &rd);
1418 * Resolve DNS name via local stub resolver
1420 * @param rh the resolver handle
1423 resolve_dns_name (struct ResolverHandle *rh)
1425 struct RecordLookupHandle *rlh = rh->proc_cls;
1428 if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1429 (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1432 "Can only resolve A/AAAA via stub... abort\n");
1433 finish_lookup (rh, rlh, 0, NULL);
1437 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1442 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1445 &handle_dns_resolver,
1451 * Read DNS packet from UDP socket and process the reply.
1453 * @param cls the resolver handle
1454 * @param tc task context
1457 read_dns_response (void *cls,
1458 const struct GNUNET_SCHEDULER_TaskContext *tc)
1460 struct ResolverHandle *rh = cls;
1461 struct RecordLookupHandle *rlh = rh->proc_cls;
1462 char buf[UINT16_MAX];
1464 struct sockaddr_in addr;
1466 struct GNUNET_DNSPARSER_Packet *packet;
1467 struct GNUNET_NAMESTORE_RecordData rd;
1468 int found_delegation = GNUNET_NO;
1469 int found_cname = GNUNET_NO;
1470 char* delegation_name = NULL;
1471 int zone_offset = 0;
1474 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1475 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1477 /* timeout or shutdown */
1478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1479 "Terminating DNS query %d\n", tc->reason);
1480 finish_lookup (rh, rlh, 0, NULL);
1484 addrlen = sizeof (addr);
1485 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1487 (struct sockaddr*) &addr,
1492 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1493 finish_lookup (rh, rlh, 0, NULL);
1496 if (NULL == (packet = GNUNET_DNSPARSER_parse (buf, r)))
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1499 "Failed to parse DNS reply!\n");
1500 finish_lookup (rh, rlh, 0, NULL);
1504 for (i = 0; i < packet->num_answers; i++)
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507 "Got record type %d (want %d)\n",
1508 packet->answers[i].type,
1510 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1511 if (GNUNET_GNS_RECORD_CNAME == packet->answers[i].type)
1513 struct GNUNET_DNSPARSER_Query query;
1514 struct GNUNET_DNSPARSER_Packet npacket;
1515 struct GNUNET_DNSPARSER_Flags flags;
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1518 "CNAME record, restarting query with `%s'\n",
1519 packet->answers[i].data.hostname);
1520 strcpy (rh->dns_name,
1521 packet->answers[i].data.hostname);
1522 found_cname = GNUNET_YES;
1523 query.name = rh->dns_name;
1524 query.type = rlh->record_type;
1525 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1526 memset (&flags, 0, sizeof (flags));
1527 flags.recursion_desired = 1;
1528 flags.checking_disabled = 1;
1529 npacket.queries = &query;
1530 npacket.answers = NULL;
1531 npacket.authority_records = NULL;
1532 npacket.num_queries = 1;
1533 npacket.num_answers = 0;
1534 npacket.num_authority_records = 0;
1535 npacket.num_additional_records = 0;
1536 npacket.flags = flags;
1537 npacket.id = rh->id;
1538 GNUNET_free_non_null (rh->dns_raw_packet);
1539 rh->dns_raw_packet = NULL;
1540 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&npacket,
1542 &rh->dns_raw_packet,
1543 &rh->dns_raw_packet_size))
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1548 GNUNET_NETWORK_socket_close (rh->dns_sock);
1549 finish_lookup (rh, rlh, 0, NULL);
1555 if ((packet->answers[i].type == rlh->record_type) &&
1556 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 rd.data = packet->answers[i].data.raw.data;
1561 rd.data_size = packet->answers[i].data.raw.data_len;
1562 rd.record_type = packet->answers[i].type;
1564 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1565 finish_lookup (rh, rlh, 1, &rd);
1566 GNUNET_DNSPARSER_free_packet (packet);
1571 if (GNUNET_YES == found_cname)
1573 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1574 if (0 > zone_offset)
1577 /* restart query with CNAME */
1578 if (0 == strcmp (rh->dns_name + zone_offset, rh->dns_zone))
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1581 "Asking DNS server for `%s'\n",
1584 send_dns_packet (rh);
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1589 "Trying system resolver for `%s'\n", rh->dns_name);
1590 resolve_dns_name (rh);
1593 GNUNET_DNSPARSER_free_packet (packet);
1597 for (i = 0; i < packet->num_authority_records; i++)
1599 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 "Found NS delegation!\n");
1603 found_delegation = GNUNET_YES;
1604 delegation_name = packet->authority_records[i].data.hostname;
1609 for (i = 0; i < packet->num_additional_records; i++)
1611 if (GNUNET_NO == found_delegation)
1614 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1615 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1617 GNUNET_assert (sizeof (struct in_addr) ==
1618 packet->authority_records[i].data.raw.data_len);
1620 rh->dns_addr.sin_addr =
1621 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1622 send_dns_packet (rh);
1623 GNUNET_DNSPARSER_free_packet (packet);
1628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1629 "Nothing useful in DNS reply!\n");
1630 finish_lookup (rh, rlh, 0, NULL);
1631 GNUNET_DNSPARSER_free_packet (packet);
1636 * Sends a UDP dns query to a nameserver specified in the rh
1638 * @param rh the request handle
1641 send_dns_packet (struct ResolverHandle *rh)
1643 struct GNUNET_NETWORK_FDSet *rset;
1645 rset = GNUNET_NETWORK_fdset_create ();
1646 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1648 "Sending %d byte DNS query\n",
1649 (int) rh->dns_raw_packet_size);
1651 if (GNUNET_SYSERR ==
1652 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1654 rh->dns_raw_packet_size,
1655 (struct sockaddr*)&rh->dns_addr,
1656 sizeof (struct sockaddr_in)))
1657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1658 _("Failed to send DNS request to %s\n"),
1659 GNUNET_a2s ((const struct sockaddr *)&rh->dns_addr,
1660 sizeof (struct sockaddr_in)));
1662 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1663 rh->timeout, //FIXME less?
1668 GNUNET_NETWORK_fdset_destroy (rset);
1673 * The final phase of resoution.
1674 * We found a NS RR and want to resolve via DNS
1676 * @param rh the pending lookup handle
1677 * @param rd_count length of record data
1678 * @param rd record data containing VPN RR
1681 resolve_record_dns (struct ResolverHandle *rh,
1682 unsigned int rd_count,
1683 const struct GNUNET_NAMESTORE_RecordData *rd)
1685 struct RecordLookupHandle *rlh = rh->proc_cls;
1686 struct GNUNET_DNSPARSER_Query query;
1687 struct GNUNET_DNSPARSER_Packet packet;
1688 struct GNUNET_DNSPARSER_Flags flags;
1689 struct in_addr dnsip;
1690 struct sockaddr_in addr;
1691 struct sockaddr *sa;
1694 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1695 memset (rh->dns_name, 0, sizeof (rh->dns_name));
1697 /* We cancel here as to not include the ns lookup in the timeout */
1698 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1700 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1701 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1703 /* Start shortening */
1704 if ((NULL != rh->priv_key) &&
1705 (GNUNET_YES == is_canonical (rh->name)))
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1708 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1710 start_shorten (rh->authority_chain_head,
1714 for (i = 0; i < rd_count; i++)
1716 /* Synthesize dns name */
1717 if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1719 strcpy (rh->dns_zone, (char*)rd[i].data);
1720 if (0 == strcmp (rh->name, ""))
1721 strcpy (rh->dns_name, (char*)rd[i].data);
1723 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1726 if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1727 /* need to use memcpy as .data may be unaligned */
1728 memcpy (&dnsip, rd[i].data, sizeof (dnsip));
1731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1732 "GNS_PHASE_REC_DNS-%llu: Looking up `%s' from `%s'\n",
1736 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1737 if (NULL == rh->dns_sock)
1739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1740 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1742 finish_lookup (rh, rlh, 0, NULL);
1746 memset (&addr, 0, sizeof (struct sockaddr_in));
1747 sa = (struct sockaddr *) &addr;
1748 sa->sa_family = AF_INET;
1749 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1751 sizeof (struct sockaddr_in),
1754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1755 "GNS_PHASE_REC_DNS-%llu: Error binding UDP socket for DNS lookup!\n",
1757 finish_lookup (rh, rlh, 0, NULL);
1760 query.name = rh->dns_name;
1761 query.type = rlh->record_type;
1762 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1763 memset (&flags, 0, sizeof (flags));
1764 flags.recursion_desired = 1;
1765 flags.checking_disabled = 1;
1766 packet.queries = &query;
1767 packet.answers = NULL;
1768 packet.authority_records = NULL;
1769 packet.num_queries = 1;
1770 packet.num_answers = 0;
1771 packet.num_authority_records = 0;
1772 packet.num_additional_records = 0;
1773 packet.flags = flags;
1775 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1777 &rh->dns_raw_packet,
1778 &rh->dns_raw_packet_size))
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1781 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1783 GNUNET_NETWORK_socket_close (rh->dns_sock);
1784 finish_lookup (rh, rlh, 0, NULL);
1788 rh->dns_addr.sin_family = AF_INET;
1789 rh->dns_addr.sin_port = htons (53); //domain
1790 rh->dns_addr.sin_addr = dnsip;
1791 #if HAVE_SOCKADDR_IN_SIN_LEN
1792 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1794 send_dns_packet (rh);
1799 * The final phase of resoution.
1800 * We found a VPN RR and want to request an IPv4/6 address
1802 * @param rh the pending lookup handle
1803 * @param rd_count length of record data
1804 * @param rd record data containing VPN RR
1807 resolve_record_vpn (struct ResolverHandle *rh,
1808 unsigned int rd_count,
1809 const struct GNUNET_NAMESTORE_RecordData *rd)
1811 struct RecordLookupHandle *rlh = rh->proc_cls;
1812 struct GNUNET_HashCode serv_desc;
1813 struct vpn_data* vpn;
1816 /* We cancel here as to not include the ns lookup in the timeout */
1817 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1819 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1820 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1822 /* Start shortening */
1823 if ((NULL != rh->priv_key) &&
1824 (GNUNET_YES == is_canonical (rh->name)))
1826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1827 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1829 start_shorten (rh->authority_chain_head,
1833 vpn = (struct vpn_data*)rd->data;
1834 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1835 strlen ((char*)&vpn[1]) + 1,
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1838 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1841 GNUNET_h2s (&vpn->peer));
1843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1844 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1847 GNUNET_h2s (&serv_desc));
1848 rh->proc = &handle_record_vpn;
1849 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1854 if (NULL == vpn_handle)
1856 vpn_handle = GNUNET_VPN_connect (cfg);
1857 if (NULL == vpn_handle)
1859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1860 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1861 finish_lookup (rh, rh->proc_cls, 0, NULL);
1866 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1867 af, ntohs (vpn->proto),
1868 (struct GNUNET_PeerIdentity *)&vpn->peer,
1871 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1872 &process_record_result_vpn,
1875 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1876 "Error connecting to VPN (not available on W32 yet)\n");
1877 finish_lookup (rh, rh->proc_cls, 0, NULL);
1883 * The final phase of resolution.
1884 * rh->name is a name that is canonical and we do not have a delegation.
1885 * Query namestore for this record
1887 * @param rh the pending lookup handle
1890 resolve_record_ns(struct ResolverHandle *rh)
1892 struct RecordLookupHandle *rlh = rh->proc_cls;
1894 /* We cancel here as to not include the ns lookup in the timeout */
1895 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1897 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1898 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1900 /* Start shortening */
1901 if ((NULL != rh->priv_key) &&
1902 (GNUNET_YES == is_canonical (rh->name)))
1904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1905 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1907 start_shorten (rh->authority_chain_head,
1912 * Try to resolve this record in our namestore.
1913 * The name to resolve is now in rh->authority_name
1914 * since we tried to resolve it to an authority
1917 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1921 &process_record_result_ns,
1927 * Handle timeout for DHT requests
1929 * @param cls the request handle as closure
1930 * @param tc the task context
1933 dht_authority_lookup_timeout (void *cls,
1934 const struct GNUNET_SCHEDULER_TaskContext *tc)
1936 struct ResolverHandle *rh = cls;
1937 struct RecordLookupHandle *rlh = rh->proc_cls;
1938 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1941 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus) timed out.\n",
1942 rh->id, rh->authority_name,
1943 rh->timeout.rel_value);
1945 rh->status |= RSL_TIMED_OUT;
1946 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1947 if (NULL != rh->get_handle)
1949 GNUNET_DHT_get_stop (rh->get_handle);
1950 rh->get_handle = NULL;
1952 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1955 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
1957 rh->proc (rh->proc_cls, rh, 0, NULL);
1960 if (0 == strcmp (rh->name, ""))
1963 * promote authority back to name and try to resolve record
1965 strcpy (rh->name, rh->authority_name);
1966 rh->proc (rh->proc_cls, rh, 0, NULL);
1971 * Start resolution in bg
1973 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
1975 rh->name, rh->authority_name, GNUNET_GNS_TLD);
1976 strcpy (rh->name, new_name);
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1978 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1981 gns_resolver_lookup_record (rh->authority,
1982 rh->private_local_zone,
1986 GNUNET_TIME_UNIT_FOREVER_REL,
1988 &background_lookup_result_processor,
1990 rh->proc (rh->proc_cls, rh, 0, NULL);
1995 * Start DHT lookup for a name -> PKEY (compare NS) record in
1996 * rh->authority's zone
1998 * @param rh the pending gns query
2001 resolve_delegation_dht (struct ResolverHandle *rh);
2005 * Resolve the delegation chain for the request in our namestore
2007 * @param rh the resolver handle
2010 resolve_delegation_ns (struct ResolverHandle *rh);
2014 * Namestore resolution for delegation finished. Processing result.
2016 * @param cls the closure
2017 * @param rh resolver handle
2018 * @param rd_count number of results (always 0)
2019 * @param rd record data (always NULL)
2022 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2023 unsigned int rd_count,
2024 const struct GNUNET_NAMESTORE_RecordData *rd);
2028 * This is a callback function that checks for key revocation
2030 * @param cls the pending query
2031 * @param key the key of the zone we did the lookup
2032 * @param expiration expiration date of the record data set in the namestore
2033 * @param name the name for which we need an authority
2034 * @param rd_count the number of records with 'name'
2035 * @param rd the record data
2036 * @param signature the signature of the authority for the record data
2039 process_pkey_revocation_result_ns (void *cls,
2040 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *key,
2041 struct GNUNET_TIME_Absolute expiration,
2043 unsigned int rd_count,
2044 const struct GNUNET_NAMESTORE_RecordData *rd,
2045 const struct GNUNET_CRYPTO_EccSignature *signature)
2047 struct ResolverHandle *rh = cls;
2048 struct GNUNET_TIME_Relative remaining_time;
2051 rh->namestore_task = NULL;
2052 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2054 for (i = 0; i < rd_count; i++)
2056 if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2058 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2059 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2061 rh->status |= RSL_PKEY_REVOKED;
2062 rh->proc (rh->proc_cls, rh, 0, NULL);
2067 if ((NULL == name) ||
2068 (0 == remaining_time.rel_value))
2070 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2071 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2074 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2076 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2077 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2078 rh->id, "+.gads", GNUNET_GNS_RECORD_REV);
2080 gns_resolver_lookup_record(rh->authority,
2081 rh->private_local_zone,
2082 GNUNET_GNS_RECORD_REV,
2085 GNUNET_TIME_UNIT_FOREVER_REL,
2087 &background_lookup_result_processor,
2091 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2092 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2095 * We are done with PKEY resolution if name is empty
2096 * else resolve again with new authority
2098 if (strcmp (rh->name, "") == 0)
2099 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2101 resolve_delegation_ns (rh);
2106 * Callback when record data is put into namestore
2108 * @param cls the closure
2109 * @param success GNUNET_OK on success
2110 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2113 on_namestore_delegation_put_result(void *cls,
2117 struct NamestoreBGTask *nbg = cls;
2119 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2122 if (GNUNET_NO == success)
2124 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2125 "GNS_NS: records already in namestore\n");
2128 else if (GNUNET_YES == success)
2130 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2131 "GNS_NS: records successfully put in namestore\n");
2135 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2136 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2141 * Function called when we get a result from the dht
2142 * for our query. Recursively tries to resolve authorities
2145 * @param cls the request handle
2146 * @param exp lifetime
2147 * @param key the key the record was stored under
2148 * @param get_path get path
2149 * @param get_path_length get path length
2150 * @param put_path put path
2151 * @param put_path_length put path length
2152 * @param type the block type
2153 * @param size the size of the record
2154 * @param data the record data
2157 process_delegation_result_dht (void* cls,
2158 struct GNUNET_TIME_Absolute exp,
2159 const struct GNUNET_HashCode * key,
2160 const struct GNUNET_PeerIdentity *get_path,
2161 unsigned int get_path_length,
2162 const struct GNUNET_PeerIdentity *put_path,
2163 unsigned int put_path_length,
2164 enum GNUNET_BLOCK_Type type,
2165 size_t size, const void *data)
2167 struct ResolverHandle *rh = cls;
2168 const struct GNSNameRecordBlock *nrb = data;
2169 const char* rd_data;
2170 uint32_t num_records;
2174 struct GNUNET_CRYPTO_ShortHashCode zone;
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2177 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n",
2181 /* stop dht lookup and timeout task */
2182 GNUNET_DHT_get_stop (rh->get_handle);
2183 rh->get_handle = NULL;
2184 if (rh->dht_heap_node != NULL)
2186 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2187 rh->dht_heap_node = NULL;
2190 num_records = ntohl(nrb->rd_count);
2191 name = (const char*) &nrb[1];
2193 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2194 struct NamestoreBGTask *ns_heap_root;
2195 struct NamestoreBGTask *namestore_bg_task;
2197 rd_data = name + strlen(name) + 1;
2198 rd_size = size - strlen(name) - 1 - sizeof (struct GNSNameRecordBlock);
2199 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2204 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2205 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2210 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2211 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2212 rh->id, name, rh->authority_name);
2213 for (i=0; i<num_records; i++)
2215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2216 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2217 rh->id, name, rh->authority_name);
2218 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2219 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2220 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2221 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2222 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2223 rh->id, rd[i].data_size);
2224 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2225 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2226 rh->id, rd[i].flags);
2228 if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2229 (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2230 (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2233 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2235 if (0 == strcmp(rh->name, ""))
2236 strcpy(rh->name, rh->authority_name);
2238 GNUNET_snprintf(rh->name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
2239 rh->name, rh->authority_name); //FIXME ret
2244 if ((0 == strcmp(name, rh->authority_name)) &&
2245 (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2247 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2248 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2251 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2252 struct AuthorityChain *auth =
2253 GNUNET_malloc(sizeof(struct AuthorityChain));
2254 auth->zone = rh->authority;
2255 memset(auth->name, 0, strlen(rh->authority_name)+1);
2256 strcpy(auth->name, rh->authority_name);
2257 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2258 rh->authority_chain_tail,
2261 if (NULL != rh->rd.data)
2262 GNUNET_free ((void*)rh->rd.data);
2264 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2265 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2266 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2269 /** try to import pkey if private key available */
2270 //if (rh->priv_key && is_canonical (rh->name))
2271 // process_discovered_authority(name, auth->zone,
2272 // rh->authority_chain_tail->zone,
2277 GNUNET_GNS_get_zone_from_key (name, key, &zone);
2280 /* Save to namestore
2281 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2284 if (max_allowed_ns_tasks <=
2285 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2287 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2288 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2290 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2291 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background ns task\n",
2294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2295 "GNS_PHASE_DELEGATE_DHT-%llu: Caching record for %s\n",
2297 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2299 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2301 GNUNET_TIME_absolute_get().abs_value);
2302 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2309 &on_namestore_delegation_put_result, //cont
2310 namestore_bg_task); //cls
2314 if (0 != rh->answered)
2319 * FIXME in this case. should we ask namestore again?
2321 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2322 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2323 rh->id, rh->authority_name, rh->name);
2325 if (0 == strcmp(rh->name, ""))
2327 /* Start shortening */
2328 if ((NULL != rh->priv_key) &&
2329 (GNUNET_YES == is_canonical (rh->name)))
2331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2332 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2334 start_shorten (rh->authority_chain_head,
2339 rh->proc = &handle_delegation_ns;
2342 /* Check for key revocation and delegate */
2343 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2345 GNUNET_GNS_MASTERZONE_STR,
2346 GNUNET_GNS_RECORD_REV,
2347 &process_pkey_revocation_result_ns,
2354 * No pkey but name exists
2357 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2358 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2359 rh->id, rh->authority_name, rh->name);
2360 if (0 == strcmp(rh->name, ""))
2361 strcpy(rh->name, rh->authority_name);
2363 GNUNET_snprintf(rh->name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
2364 rh->name, rh->authority_name); //FIXME ret
2366 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2367 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2368 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2369 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2371 rh->proc(rh->proc_cls, rh, 0, NULL);
2374 //FIXME maybe define somewhere else?
2375 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2376 +(GNUNET_DNSPARSER_MAX_NAME_LENGTH*2)
2377 #define MAX_MX_LENGTH sizeof(uint16_t)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
2378 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
2382 * Exands a name ending in .+ with the zone of origin.
2383 * FIXME: funky api: 'dest' must be large enough to hold
2384 * the result; this is a bit yucky...
2386 * @param dest destination buffer
2387 * @param src the .+ name
2388 * @param repl the string to replace the + with
2391 expand_plus (char* dest,
2396 size_t s_len = strlen (src) + 1;
2398 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2401 /* no postprocessing */
2402 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2403 "GNS_POSTPROCESS: %s too short\n", src);
2404 memcpy (dest, src, s_len);
2407 if (0 == strcmp (src + s_len - 3, ".+"))
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 "GNS_POSTPROCESS: Expanding .+ in %s\n",
2412 memset (dest, 0, s_len + strlen (repl) + strlen(GNUNET_GNS_TLD));
2414 pos = dest + s_len - 2;
2416 pos += strlen (repl);
2417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2418 "GNS_POSTPROCESS: Expanded to %s\n",
2423 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2424 "GNS_POSTPROCESS: No postprocessing for %s\n", src);
2425 memcpy (dest, src, s_len);
2434 finish_lookup (struct ResolverHandle *rh,
2435 struct RecordLookupHandle* rlh,
2436 unsigned int rd_count,
2437 const struct GNUNET_NAMESTORE_RecordData *rd)
2440 char new_rr_data[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
2441 char new_mx_data[MAX_MX_LENGTH];
2442 char new_soa_data[MAX_SOA_LENGTH];
2443 char new_srv_data[MAX_SRV_LENGTH];
2444 struct srv_data *old_srv;
2445 struct srv_data *new_srv;
2446 struct soa_data *old_soa;
2447 struct soa_data *new_soa;
2448 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2451 unsigned int offset;
2453 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
2455 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2456 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2459 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2462 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2464 for (i = 0; i < rd_count; i++)
2467 if ((GNUNET_GNS_RECORD_NS != rd[i].record_type) &&
2468 (GNUNET_GNS_RECORD_PTR != rd[i].record_type) &&
2469 (GNUNET_GNS_RECORD_CNAME != rd[i].record_type) &&
2470 (GNUNET_GNS_RECORD_MX != rd[i].record_type) &&
2471 (GNUNET_GNS_RECORD_SOA != rd[i].record_type) &&
2472 (GNUNET_GNS_RECORD_SRV != rd[i].record_type))
2474 p_rd[i].data = rd[i].data;
2479 * for all those records we 'should'
2480 * also try to resolve the A/AAAA records (RFC1035)
2481 * This is a feature and not important
2484 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2485 "GNS_POSTPROCESS: Postprocessing\n");
2486 if (0 == strcmp(rh->name, GNUNET_GNS_MASTERZONE_STR))
2487 repl_string = rlh->name;
2489 repl_string = rlh->name+strlen(rh->name)+1;
2492 if (GNUNET_GNS_RECORD_MX == rd[i].record_type)
2494 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2495 offset = sizeof (uint16_t);
2496 pos = new_mx_data + offset;
2497 // FIXME: how do we know that 'pos' has enough space for the new name?
2498 expand_plus (pos, (char*)rd[i].data+sizeof(uint16_t),
2500 offset += strlen(new_mx_data+sizeof(uint16_t)) + 1;
2501 p_rd[i].data = new_mx_data;
2502 p_rd[i].data_size = offset;
2504 else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2507 * Prio, weight and port
2509 new_srv = (struct srv_data*)new_srv_data;
2510 old_srv = (struct srv_data*)rd[i].data;
2511 new_srv->prio = old_srv->prio;
2512 new_srv->weight = old_srv->weight;
2513 new_srv->port = old_srv->port;
2514 // FIXME: how do we know that '&new_srv[1]' has enough space for the new name?
2515 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2517 p_rd[i].data = new_srv_data;
2518 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2520 else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2522 /* expand mname and rname */
2523 old_soa = (struct soa_data*)rd[i].data;
2524 new_soa = (struct soa_data*)new_soa_data;
2525 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2526 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2527 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2528 offset = strlen ((char*)&new_soa[1]) + 1;
2529 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2530 expand_plus((char*)&new_soa[1] + offset,
2531 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2533 p_rd[i].data_size = sizeof (struct soa_data)
2535 + strlen ((char*)&new_soa[1] + offset);
2536 p_rd[i].data = new_soa_data;
2541 // FIXME: how do we know that 'rd[i].data' has enough space for the new name?
2542 expand_plus(pos, (char*)rd[i].data, repl_string);
2543 p_rd[i].data_size = strlen(new_rr_data)+1;
2544 p_rd[i].data = new_rr_data;
2549 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2551 free_resolver_handle (rh);
2556 * Process DHT lookup result for record.
2558 * @param cls the closure
2559 * @param rh resolver handle
2560 * @param rd_count number of results
2561 * @param rd record data
2564 handle_record_dht (void* cls, struct ResolverHandle *rh,
2565 unsigned int rd_count,
2566 const struct GNUNET_NAMESTORE_RecordData *rd)
2568 struct RecordLookupHandle* rlh = cls;
2572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2573 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2575 /* give up, cannot resolve */
2576 finish_lookup (rh, rlh, 0, NULL);
2579 /* results found yay */
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2581 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2582 finish_lookup (rh, rlh, rd_count, rd);
2587 * Process namestore lookup result for record.
2589 * @param cls the closure
2590 * @param rh resolver handle
2591 * @param rd_count number of results
2592 * @param rd record data
2595 handle_record_ns (void* cls, struct ResolverHandle *rh,
2596 unsigned int rd_count,
2597 const struct GNUNET_NAMESTORE_RecordData *rd)
2599 struct RecordLookupHandle* rlh = cls;
2600 int check_dht = GNUNET_YES;
2604 /* results found yay */
2605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2606 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2607 finish_lookup (rh, rlh, rd_count, rd);
2611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2612 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2616 * There are 5 conditions that have to met for us to consult the DHT:
2617 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2618 * 2. No entry in the NS existed AND
2619 * 3. The zone queried is not the local resolver's zone AND
2620 * 4. The name that was looked up is '+'
2621 * because if it was any other canonical name we either already queried
2622 * the DHT for the authority in the authority lookup phase (and thus
2623 * would already have an entry in the NS for the record)
2624 * 5. We are not in cache only mode
2626 if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2627 (0 == (rh->status & RSL_RECORD_EXISTS)) )
2630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2631 "GNS_PHASE_REC-%llu: Not expired and exists!\n",
2633 check_dht = GNUNET_NO;
2636 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2637 &rh->private_local_zone))
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2641 "GNS_PHASE_REC-%llu: Our zone!\n",
2643 check_dht = GNUNET_NO;
2646 if ((0 != strcmp (rh->name, GNUNET_GNS_MASTERZONE_STR)) && (GNUNET_YES == is_srv (rh->name)))
2647 check_dht = GNUNET_NO;
2649 if (GNUNET_YES == rh->only_cached)
2650 check_dht = GNUNET_NO;
2652 if (GNUNET_YES == check_dht)
2654 rh->proc = &handle_record_dht;
2655 resolve_record_dht(rh);
2658 /* give up, cannot resolve */
2659 finish_lookup (rh, rlh, 0, NULL);
2664 * Move one level up in the domain hierarchy and return the
2665 * passed top level domain.
2667 * FIXME: funky API: not only 'dest' is updated, so is 'name'!
2669 * @param name the domain
2670 * @param dest the destination where the tld will be put
2673 pop_tld (char* name, char* dest)
2677 if (GNUNET_YES == is_canonical (name))
2679 strcpy (dest, name);
2684 for (len = strlen(name); 0 < len; len--)
2686 if (*(name+len) == '.')
2694 strcpy (dest, (name+len+1));
2699 * DHT resolution for delegation finished. Processing result.
2701 * @param cls the closure
2702 * @param rh resolver handle
2703 * @param rd_count number of results (always 0)
2704 * @param rd record data (always NULL)
2707 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2708 unsigned int rd_count,
2709 const struct GNUNET_NAMESTORE_RecordData *rd)
2711 struct RecordLookupHandle* rlh = cls;
2713 if (0 == strcmp(rh->name, ""))
2715 if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2717 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2718 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2720 finish_lookup(rh, rlh, rd_count, rd);
2723 /* We resolved full name for delegation. resolving record */
2724 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2725 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2727 strcpy(rh->name, "+\0");
2728 rh->proc = &handle_record_ns;
2729 resolve_record_ns(rh);
2734 * we still have some left
2736 if (GNUNET_YES == is_canonical (rh->name))
2738 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2739 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2742 rh->proc = &handle_record_ns;
2743 resolve_record_ns(rh);
2746 /* give up, cannot resolve */
2747 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2748 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2750 finish_lookup(rh, rlh, 0, NULL);
2755 * Start DHT lookup for a name -> PKEY (compare NS) record in
2756 * rh->authority's zone
2758 * @param rh the pending gns query
2761 resolve_delegation_dht (struct ResolverHandle *rh)
2764 struct GNUNET_HashCode lookup_key;
2765 struct ResolverHandle *rh_heap_root;
2767 pop_tld (rh->name, rh->authority_name);
2768 GNUNET_GNS_get_key_for_record (rh->authority_name,
2771 rh->dht_heap_node = NULL;
2772 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2774 rh->timeout_cont = &dht_authority_lookup_timeout;
2775 rh->timeout_cont_cls = rh;
2779 if (max_allowed_background_queries <=
2780 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2782 /* terminate oldest lookup */
2783 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2784 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2785 rh_heap_root->get_handle = NULL;
2786 rh_heap_root->dht_heap_node = NULL;
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2788 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2790 rh_heap_root->authority_name);
2791 rh_heap_root->proc (rh_heap_root->proc_cls,
2796 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2798 GNUNET_TIME_absolute_get().abs_value);
2800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2801 "Beginning DHT lookup for %s in zone %s for request %llu\n",
2803 GNUNET_short_h2s (&rh->authority),
2805 xquery = htonl (GNUNET_GNS_RECORD_PKEY);
2806 GNUNET_assert (rh->get_handle == NULL);
2807 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
2808 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2810 DHT_GNS_REPLICATION_LEVEL,
2811 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2814 &process_delegation_result_dht,
2820 * Checks if "name" ends in ".tld"
2822 * @param name the name to check
2823 * @param tld the TLD to check for
2824 * @return GNUNET_YES or GNUNET_NO
2827 is_tld (const char* name, const char* tld)
2831 if (strlen (name) <= strlen (tld))
2833 offset = strlen (name) - strlen (tld);
2834 if (0 != strcmp (name + offset, tld))
2841 * Namestore resolution for delegation finished. Processing result.
2843 * @param cls the closure
2844 * @param rh resolver handle
2845 * @param rd_count number of results (always 0)
2846 * @param rd record data (always NULL)
2849 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2850 unsigned int rd_count,
2851 const struct GNUNET_NAMESTORE_RecordData *rd)
2853 struct RecordLookupHandle* rlh = cls;
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2858 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2859 rh->id, rh->status);
2861 if (rh->status & RSL_PKEY_REVOKED)
2863 finish_lookup (rh, rlh, 0, NULL);
2867 if (0 == strcmp(rh->name, ""))
2870 /* We resolved full name for delegation. resolving record */
2871 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2872 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2874 if (rh->status & RSL_CNAME_FOUND)
2876 if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2878 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2879 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2881 strcpy (rh->name, rh->authority_name);
2882 finish_lookup (rh, rlh, rd_count, rd);
2887 if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2889 s_len = strlen (rd->data) - 2;
2890 memcpy (rh->name, rd->data, s_len);
2891 rh->name[s_len] = '\0';
2892 resolve_delegation_ns (rh);
2895 else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2897 gns_resolver_lookup_record (rh->authority,
2898 rh->private_local_zone,
2907 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2908 free_resolver_handle (rh);
2914 strcpy (rh->dns_name, (char*)rd->data);
2915 resolve_dns_name (rh);
2920 else if (rh->status & RSL_DELEGATE_VPN)
2922 if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2924 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2925 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2927 finish_lookup(rh, rlh, rd_count, rd);
2930 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2931 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2933 GNUNET_assert (NULL != rd);
2934 rh->proc = &handle_record_vpn;
2935 resolve_record_vpn (rh, rd_count, rd);
2938 else if (rh->status & RSL_DELEGATE_NS)
2940 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2943 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2945 finish_lookup (rh, rlh, rd_count, rd);
2948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2949 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2951 GNUNET_assert (NULL != rd);
2952 rh->proc = &handle_record_ns;
2953 resolve_record_dns (rh, rd_count, rd);
2956 else if (rh->status & RSL_DELEGATE_PKEY)
2958 if (rh->status & RSL_PKEY_REVOKED)
2960 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2961 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
2963 finish_lookup (rh, rlh, 0, NULL);
2966 else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2968 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2969 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
2971 finish_lookup(rh, rlh, rd_count, rd);
2975 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2976 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
2978 strcpy(rh->name, "+\0");
2979 rh->proc = &handle_record_ns;
2980 resolve_record_ns(rh);
2984 if (rh->status & RSL_DELEGATE_NS)
2986 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2988 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2989 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2991 finish_lookup(rh, rlh, rd_count, rd);
2995 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2996 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2998 GNUNET_assert (NULL != rd);
2999 rh->proc = &handle_record_ns;
3000 resolve_record_dns (rh, rd_count, rd);
3005 * we still have some left
3006 * check if authority in ns is fresh
3008 * or we are authority
3011 check_dht = GNUNET_YES;
3012 if ((rh->status & RSL_RECORD_EXISTS) &&
3013 !(rh->status & RSL_RECORD_EXPIRED))
3014 check_dht = GNUNET_NO;
3016 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3017 &rh->private_local_zone))
3018 check_dht = GNUNET_NO;
3020 if (GNUNET_YES == rh->only_cached)
3021 check_dht = GNUNET_NO;
3023 if (GNUNET_YES == check_dht)
3026 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3027 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3029 rh->proc = &handle_delegation_dht;
3030 resolve_delegation_dht(rh);
3034 if (GNUNET_NO == is_canonical (rh->name))
3036 /* give up, cannot resolve */
3037 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3038 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3041 finish_lookup(rh, rlh, rd_count, rd);
3044 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3045 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3048 rh->proc = &handle_record_ns;
3049 resolve_record_ns(rh);
3054 * This is a callback function that should give us only PKEY
3055 * records. Used to query the namestore for the authority (PKEY)
3056 * for 'name'. It will recursively try to resolve the
3057 * authority for a given name from the namestore.
3059 * @param cls the pending query
3060 * @param key the key of the zone we did the lookup
3061 * @param expiration expiration date of the record data set in the namestore
3062 * @param name the name for which we need an authority
3063 * @param rd_count the number of records with 'name'
3064 * @param rd the record data
3065 * @param signature the signature of the authority for the record data
3068 process_delegation_result_ns (void* cls,
3069 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *key,
3070 struct GNUNET_TIME_Absolute expiration,
3072 unsigned int rd_count,
3073 const struct GNUNET_NAMESTORE_RecordData *rd,
3074 const struct GNUNET_CRYPTO_EccSignature *signature)
3076 struct ResolverHandle *rh = cls;
3077 struct GNUNET_TIME_Relative remaining_time;
3078 struct GNUNET_CRYPTO_ShortHashCode zone;
3079 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3081 struct GNUNET_TIME_Absolute et;
3082 struct AuthorityChain *auth;
3084 rh->namestore_task = NULL;
3085 GNUNET_CRYPTO_short_hash (key,
3086 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3089 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup for `%s' in zone %s\n",
3092 GNUNET_short_h2s (&zone));
3094 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3101 "GNS_PHASE_DELEGATE_NS-%llu: Records with name `%s' exist in zone %s.\n",
3103 GNUNET_short_h2s (&zone));
3104 rh->status |= RSL_RECORD_EXISTS;
3106 if (0 == remaining_time.rel_value)
3108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3109 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3111 rh->status |= RSL_RECORD_EXPIRED;
3116 * No authority found in namestore.
3121 * We did not find an authority in the namestore
3126 * Promote this authority back to a name maybe it is
3129 if (strcmp (rh->name, "") == 0)
3131 /* simply promote back */
3132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3133 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3134 rh->id, rh->authority_name);
3135 strcpy (rh->name, rh->authority_name);
3139 /* add back to existing name */
3140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3141 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3142 rh->id, rh->authority_name, rh->name);
3143 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
3144 rh->name, rh->authority_name);
3145 strcpy (rh->name, new_name);
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3147 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3151 rh->proc (rh->proc_cls, rh, 0, NULL);
3156 * We found an authority that may be able to help us
3157 * move on with query
3158 * Note only 1 pkey should have been returned.. anything else would be strange
3160 for (i=0; i < rd_count;i++)
3162 switch (rd[i].record_type)
3164 case GNUNET_GNS_RECORD_CNAME:
3165 /* Like in regular DNS this should mean that there is no other
3166 * record for this name. */
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3169 "GNS_PHASE_DELEGATE_NS-%llu: CNAME `%.*s' found.\n",
3171 (int) rd[i].data_size,
3173 rh->status |= RSL_CNAME_FOUND;
3174 rh->proc (rh->proc_cls, rh, rd_count, rd);
3176 case GNUNET_GNS_RECORD_VPN:
3177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3178 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3180 rh->status |= RSL_DELEGATE_VPN;
3181 rh->proc (rh->proc_cls, rh, rd_count, rd);
3183 case GNUNET_GNS_RECORD_NS:
3184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3185 "GNS_PHASE_DELEGATE_NS-%llu: NS `%.*s' found.\n",
3187 (int) rd[i].data_size,
3189 rh->status |= RSL_DELEGATE_NS;
3190 rh->proc (rh->proc_cls, rh, rd_count, rd);
3192 case GNUNET_GNS_RECORD_PKEY:
3193 rh->status |= RSL_DELEGATE_PKEY;
3194 if ((ignore_pending_records != 0) &&
3195 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3198 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3203 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3204 et.abs_value = rd[i].expiration_time;
3205 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
3207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3208 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3210 if (remaining_time.rel_value == 0)
3212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3213 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3215 rh->authority_chain_head->fresh = 0;
3216 rh->proc (rh->proc_cls, rh, 0, NULL);
3221 /* Resolve rest of query with new authority */
3222 memcpy (&rh->authority, rd[i].data,
3223 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3224 auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3225 auth->zone = rh->authority;
3226 memset (auth->name, 0, strlen (rh->authority_name)+1);
3227 strcpy (auth->name, rh->authority_name);
3228 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3229 rh->authority_chain_tail,
3231 if (NULL != rh->rd.data)
3232 GNUNET_free ((void*)(rh->rd.data));
3233 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3234 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3235 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3237 /* Check for key revocation and delegate */
3238 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3240 GNUNET_GNS_MASTERZONE_STR,
3241 GNUNET_GNS_RECORD_REV,
3242 &process_pkey_revocation_result_ns,
3246 /* ignore, move to next result */
3251 /* no answers that would cause delegation were found */
3252 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3253 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup failed (no PKEY record)\n",
3256 * If we have found some records for the LAST label
3257 * we return the results. Else NULL.
3259 if (0 == strcmp (rh->name, ""))
3261 /* Start shortening */
3262 if ((rh->priv_key != NULL) &&
3263 (is_canonical (rh->name) == GNUNET_YES))
3265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3266 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3268 start_shorten (rh->authority_chain_head,
3271 /* simply promote back */
3272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3273 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3274 rh->id, rh->authority_name);
3275 strcpy (rh->name, rh->authority_name);
3276 rh->proc (rh->proc_cls, rh, rd_count, rd);
3280 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3281 "%s.%s", rh->name, rh->authority_name);
3282 strcpy (rh->name, new_name);
3283 rh->proc (rh->proc_cls, rh, 0, NULL);
3289 * Resolve the delegation chain for the request in our namestore
3290 * (as in, find the respective authority for the leftmost label).
3292 * @param rh the resolver handle
3295 resolve_delegation_ns (struct ResolverHandle *rh)
3297 pop_tld (rh->name, rh->authority_name);
3298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3299 "GNS_PHASE_DELEGATE_NS-%llu: Finding authority for `%s' by looking up `%s' in GADS zone `%s'\n",
3303 GNUNET_short_h2s (&rh->authority));
3304 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3307 GNUNET_GNS_RECORD_ANY,
3308 &process_delegation_result_ns,
3314 * Lookup of a record in a specific zone
3315 * calls lookup result processor on result
3317 * @param zone the root zone
3318 * @param pzone the private local zone
3319 * @param record_type the record type to look up
3320 * @param name the name to look up
3321 * @param key a private key for use with PSEU import (can be NULL)
3322 * @param timeout timeout for resolution
3323 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3324 * @param proc the processor to call on result
3325 * @param cls the closure to pass to proc
3328 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3329 struct GNUNET_CRYPTO_ShortHashCode pzone,
3330 uint32_t record_type,
3332 struct GNUNET_CRYPTO_EccPrivateKey *key,
3333 struct GNUNET_TIME_Relative timeout,
3335 RecordLookupProcessor proc,
3338 struct ResolverHandle *rh;
3339 struct RecordLookupHandle* rlh;
3340 char string_hash[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
3341 char nzkey[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
3342 char* nzkey_ptr = nzkey;
3344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3345 "Starting resolution for `%s' (type=%d) with timeout %s!\n",
3347 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
3349 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3350 (strcmp(GNUNET_GNS_TLD, name) != 0))
3352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3353 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3358 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3359 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3360 rh->authority = zone;
3364 rh->timeout = timeout;
3365 rh->private_local_zone = pzone;
3366 rh->only_cached = only_cached;
3368 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3372 "No shorten key for resolution\n");
3374 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3377 * Set timeout for authority lookup phase to 1/2
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3380 "Timeout for lookup set to %s/2\n",
3381 GNUNET_STRINGS_relative_time_to_string (rh->timeout, GNUNET_YES));
3382 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
3383 GNUNET_TIME_relative_divide(timeout, 2),
3384 &handle_lookup_timeout,
3386 rh->timeout_cont = &dht_authority_lookup_timeout;
3387 rh->timeout_cont_cls = rh;
3391 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3392 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3395 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3398 * Only '.gads' given
3400 strcpy (rh->name, "\0");
3404 if (is_zkey_tld(name) == GNUNET_YES)
3406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3409 * This is a zkey tld
3410 * build hash and use as initial authority
3413 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3414 memcpy(rh->name, name,
3415 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3416 pop_tld (rh->name, string_hash);
3418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3419 "ZKEY is %s!\n", string_hash);
3421 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3423 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3426 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3427 "Cannot convert ZKEY `%s' to hash!\n", string_hash);
3429 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3430 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3431 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3434 proc (cls, 0, NULL);
3439 else if (is_gads_tld (name) == GNUNET_YES)
3441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3444 * Presumably GADS tld
3446 memcpy (rh->name, name,
3447 strlen (name) - strlen(GNUNET_GNS_TLD) - 1);
3448 rh->name[strlen (name) - strlen(GNUNET_GNS_TLD) - 1] = '\0';
3452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3453 _("Not a GADS TLD: `%s'\n"),
3455 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3456 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3457 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3460 proc (cls, 0, NULL);
3466 * Initialize authority chain
3468 rh->authority_chain_head = GNUNET_malloc (sizeof(struct AuthorityChain));
3469 rh->authority_chain_tail = rh->authority_chain_head;
3470 rh->authority_chain_head->zone = rh->authority;
3471 strcpy (rh->authority_chain_head->name, "");
3474 * Copy original query into lookup handle
3476 rlh->record_type = record_type;
3477 memset(rlh->name, 0, strlen(name) + 1);
3478 strcpy(rlh->name, name);
3480 rlh->proc_cls = cls;
3482 rh->proc = &handle_delegation_ns;
3483 resolve_delegation_ns (rh);
3486 /******** END Record Resolver ***********/
3489 finish_shorten (struct ResolverHandle *rh,
3490 struct NameShortenHandle *nsh)
3492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3493 "Sending %s as shorten result\n", nsh->result);
3494 nsh->proc (nsh->proc_cls, nsh->result);
3495 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3497 free_resolver_handle (rh);
3502 * Callback calles by namestore for a zone to name
3505 * @param cls the closure
3506 * @param zone_key the zone we queried
3507 * @param expire the expiration time of the name
3508 * @param name the name found or NULL
3509 * @param rd_len number of records for the name
3510 * @param rd the record data (PKEY) for the name
3511 * @param signature the signature for the record data
3514 process_zone_to_name_shorten_root (void *cls,
3515 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
3516 struct GNUNET_TIME_Absolute expire,
3518 unsigned int rd_len,
3519 const struct GNUNET_NAMESTORE_RecordData *rd,
3520 const struct GNUNET_CRYPTO_EccSignature *signature);
3524 * Callback called by namestore for a zone to name
3527 * @param cls the closure
3528 * @param zone_key the zone we queried
3529 * @param expire the expiration time of the name
3530 * @param name the name found or NULL
3531 * @param rd_len number of records for the name
3532 * @param rd the record data (PKEY) for the name
3533 * @param signature the signature for the record data
3536 process_zone_to_name_shorten_shorten (void *cls,
3537 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
3538 struct GNUNET_TIME_Absolute expire,
3540 unsigned int rd_len,
3541 const struct GNUNET_NAMESTORE_RecordData *rd,
3542 const struct GNUNET_CRYPTO_EccSignature *signature)
3544 struct ResolverHandle *rh = cls;
3545 struct NameShortenHandle* nsh = rh->proc_cls;
3546 struct AuthorityChain *next_authority;
3548 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3549 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3552 rh->namestore_task = NULL;
3553 /* we found a match in our own root zone */
3556 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3557 memset(result, 0, answer_len);
3559 if (strlen(rh->name) > 0)
3561 sprintf (result, "%s.%s.%s.%s",
3563 nsh->shorten_zone_name,
3568 sprintf (result, "%s.%s.%s", name,
3569 nsh->shorten_zone_name,
3573 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3574 "Found shorten result %s\n", result);
3575 if (strlen (nsh->result) > strlen (result))
3576 strcpy (nsh->result, result);
3578 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3579 nsh->shorten_zone) == 0)
3582 * This is our zone append .gads unless name is empty
3583 * (it shouldn't be, usually FIXME what happens if we
3584 * shorten to our zone to a "" record??)
3587 sprintf (result, "%s.%s.%s",
3589 nsh->shorten_zone_name,
3591 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3592 "Our zone: Found %s as shorten result\n", result);
3594 if (strlen (nsh->result) > strlen (result))
3595 strcpy (nsh->result, result);
3596 //nsh->proc(nsh->proc_cls, result);
3598 //free_resolver_handle(rh);
3605 * continue with next authority if exists
3607 if (NULL == rh->authority_chain_head->next)
3609 finish_shorten (rh, nsh);
3612 next_authority = rh->authority_chain_head;
3614 if (0 == strcmp (rh->name, ""))
3615 strcpy (tmp_name, next_authority->name);
3617 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3618 "%s.%s", rh->name, next_authority->name);
3620 strcpy(rh->name, tmp_name);
3621 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3622 "No PSEU found for authority %s. Promoting back: %s\n",
3623 next_authority->name, rh->name);
3625 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3626 rh->authority_chain_tail,
3629 GNUNET_free (next_authority);
3631 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3632 &rh->authority_chain_tail->zone,
3633 &rh->authority_chain_head->zone,
3634 &process_zone_to_name_shorten_root,
3640 * Callback calles by namestore for a zone to name
3643 * @param cls the closure
3644 * @param zone_key the zone we queried
3645 * @param expire the expiration time of the name
3646 * @param name the name found or NULL
3647 * @param rd_len number of records for the name
3648 * @param rd the record data (PKEY) for the name
3649 * @param signature the signature for the record data
3652 process_zone_to_name_shorten_private (void *cls,
3653 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
3654 struct GNUNET_TIME_Absolute expire,
3656 unsigned int rd_len,
3657 const struct GNUNET_NAMESTORE_RecordData *rd,
3658 const struct GNUNET_CRYPTO_EccSignature *signature)
3660 struct ResolverHandle *rh = cls;
3661 struct NameShortenHandle* nsh = rh->proc_cls;
3662 struct AuthorityChain *next_authority;
3664 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3665 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3668 rh->namestore_task = NULL;
3669 /* we found a match in our own root zone */
3672 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3673 memset(result, 0, answer_len);
3675 if (strlen(rh->name) > 0)
3677 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3681 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3684 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3685 "Found shorten result %s\n", result);
3686 if (strlen (nsh->result) > strlen (result))
3687 strcpy (nsh->result, result);
3689 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3690 nsh->private_zone) == 0)
3693 * This is our zone append .gads unless name is empty
3694 * (it shouldn't be, usually FIXME what happens if we
3695 * shorten to our zone to a "" record??)
3698 sprintf (result, "%s.%s.%s",
3699 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3700 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3701 "Our private zone: Found %s as shorten result %s\n", result);
3702 if (strlen (nsh->result) > strlen (result))
3703 strcpy (nsh->result, result);
3706 if (0 != strcmp (nsh->shorten_zone_name, ""))
3708 /* backtrack authorities for names in priv zone */
3709 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3711 &rh->authority_chain_head->zone,
3712 &process_zone_to_name_shorten_shorten,
3719 * continue with next authority if exists
3721 if (NULL == rh->authority_chain_head->next)
3723 finish_shorten (rh, nsh);
3726 next_authority = rh->authority_chain_head;
3728 if (0 == strcmp (rh->name, ""))
3729 strcpy (tmp_name, next_authority->name);
3731 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3732 "%s.%s", rh->name, next_authority->name);
3734 strcpy(rh->name, tmp_name);
3735 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3736 "No PSEU found for authority %s. Promoting back: %s\n",
3737 next_authority->name, rh->name);
3739 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3740 rh->authority_chain_tail,
3743 GNUNET_free (next_authority);
3745 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3746 &rh->authority_chain_tail->zone,
3747 &rh->authority_chain_head->zone,
3748 &process_zone_to_name_shorten_root,
3755 * Callback calles by namestore for a zone to name
3758 * @param cls the closure
3759 * @param zone_key the zone we queried
3760 * @param expire the expiration time of the name
3761 * @param name the name found or NULL
3762 * @param rd_len number of records for the name
3763 * @param rd the record data (PKEY) for the name
3764 * @param signature the signature for the record data
3767 process_zone_to_name_shorten_root (void *cls,
3768 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
3769 struct GNUNET_TIME_Absolute expire,
3771 unsigned int rd_len,
3772 const struct GNUNET_NAMESTORE_RecordData *rd,
3773 const struct GNUNET_CRYPTO_EccSignature *signature)
3775 struct ResolverHandle *rh = cls;
3776 struct NameShortenHandle* nsh = rh->proc_cls;
3777 struct AuthorityChain *next_authority;
3778 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3779 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3782 rh->namestore_task = NULL;
3783 /* we found a match in our own root zone */
3786 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3787 memset(result, 0, answer_len);
3789 if (strlen(rh->name) > 0)
3791 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3795 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3798 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3799 "Found shorten result %s\n", result);
3800 if (strlen (nsh->result) > strlen (result))
3801 strcpy (nsh->result, result);
3803 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3804 nsh->root_zone) == 0)
3807 * This is our zone append .gads unless name is empty
3808 * (it shouldn't be, usually FIXME what happens if we
3809 * shorten to our zone to a "" record??)
3812 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3813 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3814 "Our zone: Found %s as shorten result\n", result);
3815 if (strlen (nsh->result) > strlen (result))
3816 strcpy (nsh->result, result);
3819 if (NULL != nsh->private_zone)
3821 /* backtrack authorities for names in priv zone */
3822 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3824 &rh->authority_chain_head->zone,
3825 &process_zone_to_name_shorten_private,
3828 else if (NULL != nsh->shorten_zone)
3830 /* backtrack authorities for names in shorten zone */
3831 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3833 &rh->authority_chain_head->zone,
3834 &process_zone_to_name_shorten_shorten,
3841 * continue with next authority if exists
3843 if (NULL == rh->authority_chain_head->next)
3845 finish_shorten (rh, nsh);
3848 next_authority = rh->authority_chain_head;
3850 if (0 == strcmp (rh->name, ""))
3851 strcpy (tmp_name, next_authority->name);
3853 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3854 "%s.%s", rh->name, next_authority->name);
3856 strcpy(rh->name, tmp_name);
3857 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3858 "No PSEU found for authority %s. Promoting back: %s\n",
3859 next_authority->name, rh->name);
3861 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3862 rh->authority_chain_tail,
3865 GNUNET_free (next_authority);
3867 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3868 &rh->authority_chain_tail->zone,
3869 &rh->authority_chain_head->zone,
3870 &process_zone_to_name_shorten_root,
3877 * Process result from namestore delegation lookup
3878 * for shorten operation
3880 * @param cls the client shorten handle
3881 * @param rh the resolver handle
3882 * @param rd_count number of results (0)
3883 * @param rd data (NULL)
3886 handle_delegation_ns_shorten (void* cls,
3887 struct ResolverHandle *rh,
3889 const struct GNUNET_NAMESTORE_RecordData *rd)
3891 struct NameShortenHandle *nsh;
3892 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3894 nsh = (struct NameShortenHandle *)cls;
3895 rh->namestore_task = NULL;
3897 * At this point rh->name contains the part of the name
3898 * that we do not have a PKEY in our namestore to resolve.
3899 * The authority chain in the resolver handle is now
3900 * useful to backtrack if needed
3903 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3904 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3905 memset(result, 0, sizeof (result));
3907 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3911 * This is our zone append .gads unless name is empty
3912 * (it shouldn't be, usually FIXME what happens if we
3913 * shorten to our zone to a "" record??)
3916 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3917 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3918 "Our zone: Found %s as shorten result\n", result);
3920 if (strlen (nsh->result) > strlen (result))
3921 strcpy (nsh->result, result);
3924 else if (NULL != nsh->private_zone)
3927 * This is our zone append .gads unless name is empty
3928 * (it shouldn't be, usually FIXME what happens if we
3929 * shorten to our zone to a "" record??)
3931 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3932 nsh->private_zone) == 0)
3935 sprintf (result, "%s.%s.%s",
3936 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3937 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3938 "Our zone: Found %s as shorten result in private zone %s\n",
3941 if (strlen (nsh->result) > strlen (result))
3942 strcpy (nsh->result, result);
3945 else if (NULL != nsh->shorten_zone)
3948 * This is our zone append .gads unless name is empty
3949 * (it shouldn't be, usually FIXME what happens if we
3950 * shorten to our zone to a "" record??)
3952 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3953 nsh->shorten_zone) == 0)
3955 sprintf (result, "%s.%s.%s",
3956 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3957 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3958 "Our zone: Found %s as shorten result in shorten zone\n",
3961 if (strlen (nsh->result) > strlen (result))
3962 strcpy (nsh->result, result);
3967 /* backtrack authorities for names */
3968 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3970 &rh->authority_chain_head->zone,
3971 &process_zone_to_name_shorten_root,
3978 * Callback calles by namestore for a zone to name
3981 * @param cls the closure
3982 * @param zone_key the zone we queried
3983 * @param expire the expiration time of the name
3984 * @param name the name found or NULL
3985 * @param rd_len number of records for the name
3986 * @param rd the record data (PKEY) for the name
3987 * @param signature the signature for the record data
3990 process_zone_to_name_zkey(void *cls,
3991 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *zone_key,
3992 struct GNUNET_TIME_Absolute expire,
3994 unsigned int rd_len,
3995 const struct GNUNET_NAMESTORE_RecordData *rd,
3996 const struct GNUNET_CRYPTO_EccSignature *signature)
3998 struct ResolverHandle *rh = cls;
3999 struct NameShortenHandle *nsh = rh->proc_cls;
4000 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
4002 rh->namestore_task = NULL;
4004 /* zkey not in our zone */
4008 * In this case we have not given this PKEY a name (yet)
4009 * It is either just not in our zone or not even cached
4010 * Since we do not know at this point we will not try to shorten
4011 * because PKEY import will happen if the user follows the zkey
4014 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4015 "No name found for zkey %s returning verbatim!\n", nsh->result);
4016 /*if (strcmp(rh->name, "") != 0)
4017 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s.%s",
4018 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4020 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
4021 enc, GNUNET_GNS_TLD_ZKEY);
4023 strcpy (nsh->result, new_name);*/
4025 finish_shorten (rh, nsh);
4029 if (strcmp(rh->name, "") != 0)
4030 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
4033 strcpy(new_name, name);
4035 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4036 "Continue shorten for %s!\n", new_name);
4038 strcpy(rh->name, new_name);
4040 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4041 rh->authority_chain_tail = rh->authority_chain_head;
4042 rh->authority_chain_head->zone = rh->authority;
4045 /* Start delegation resolution in our namestore */
4046 resolve_delegation_ns (rh);
4051 * Shorten api from resolver
4053 * @param zone the root zone to use
4054 * @param pzone the private zone to use
4055 * @param szone the shorten zone to use
4056 * @param name the name to shorten
4057 * @param private_zone_name name of the private zone
4058 * @param shorten_zone_name name of the shorten zone
4059 * @param proc the processor to call with result
4060 * @param proc_cls closure to pass to proc
4063 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4064 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4065 struct GNUNET_CRYPTO_ShortHashCode *szone,
4067 const char* private_zone_name,
4068 const char* shorten_zone_name,
4069 ShortenResultProcessor proc,
4072 struct ResolverHandle *rh;
4073 struct NameShortenHandle *nsh;
4074 char string_hash[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
4075 struct GNUNET_CRYPTO_ShortHashCode zkey;
4076 char nzkey[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
4077 char* nzkey_ptr = nzkey;
4079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4080 "Starting shorten for %s!\n", name);
4082 if (is_canonical ((char*)name) == GNUNET_YES)
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4085 "%s is canonical. Returning verbatim\n", name);
4086 proc (proc_cls, name);
4090 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4092 nsh->proc_cls = proc_cls;
4093 nsh->root_zone = zone;
4094 nsh->private_zone = pzone;
4095 nsh->shorten_zone = szone;
4096 strcpy (nsh->private_zone_name, private_zone_name);
4097 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4098 strcpy (nsh->result, name);
4100 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4101 rh->authority = *zone;
4103 rh->proc = &handle_delegation_ns_shorten;
4105 rh->private_local_zone = *zone;
4107 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4109 "Checking for TLD...\n");
4110 if (is_zkey_tld (name) == GNUNET_YES)
4112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4115 * This is a zkey tld
4116 * build hash and use as initial authority
4119 memset (rh->name, 0,
4120 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4121 memcpy (rh->name, name,
4122 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4123 pop_tld (rh->name, string_hash);
4125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4126 "ZKEY is %s!\n", string_hash);
4128 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4130 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4134 "Cannot convert ZKEY %s to hash!\n", nzkey);
4135 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4138 proc (proc_cls, name);
4141 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4144 &process_zone_to_name_zkey,
4149 else if (is_gads_tld (name) == GNUNET_YES)
4151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4154 * Presumably GNUNET tld
4156 memset (rh->name, 0,
4157 strlen (name)-strlen (GNUNET_GNS_TLD));
4158 memcpy (rh->name, name,
4159 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4164 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4167 proc (proc_cls, name);
4171 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4172 rh->authority_chain_tail = rh->authority_chain_head;
4173 rh->authority_chain_head->zone = *zone;
4175 /* Start delegation resolution in our namestore */
4176 resolve_delegation_ns (rh);
4179 /*********** END NAME SHORTEN ********************/
4182 * Conclude get authority lookup
4184 * @param rh resolver handle
4185 * @param nah get authority lookup handle
4188 finish_get_auth (struct ResolverHandle *rh,
4189 struct GetNameAuthorityHandle *nah)
4191 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4192 "Got authority result %s\n", nah->result);
4194 nah->proc (nah->proc_cls, nah->result);
4195 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4197 free_resolver_handle (rh);
4202 * Process result from namestore delegation lookup
4203 * for get authority operation
4205 * @param cls the client get auth handle
4206 * @param rh the resolver handle
4207 * @param rd_count number of results (0)
4208 * @param rd data (NULL)
4211 handle_delegation_result_ns_get_auth(void* cls,
4212 struct ResolverHandle *rh,
4214 const struct GNUNET_NAMESTORE_RecordData *rd)
4216 struct GetNameAuthorityHandle* nah = rh->proc_cls;
4220 * At this point rh->name contains the part of the name
4221 * that we do not have a PKEY in our namestore to resolve.
4222 * The authority chain in the resolver handle is now
4223 * useful to backtrack if needed
4226 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4227 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4229 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4230 "Building response!\n");
4231 if (is_canonical (rh->name) == GNUNET_YES)
4234 * We successfully resolved the authority in the ns
4235 * FIXME for our purposes this is fine
4236 * but maybe we want to have an api that also looks
4237 * into the dht (i.e. option in message)
4239 if (strlen(rh->name) > strlen(nah->name))
4241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4242 "Record name longer than original lookup name... odd!\n");
4246 answer_len = strlen(nah->name) - strlen(rh->name)
4247 + strlen(GNUNET_GNS_TLD) + 1;
4248 memset(nah->result, 0, answer_len);
4249 if (0 != strcmp (rh->name, ""))
4250 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4252 strcpy(nah->result, nah->name);
4254 finish_get_auth (rh, nah);
4258 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4259 "Unable to resolve authority for remaining %s!\n", rh->name);
4260 strcpy(nah->result, "");
4261 finish_get_auth (rh, nah);
4267 * Tries to resolve the authority for name
4270 * @param zone the root zone to look up for
4271 * @param pzone the private local zone
4272 * @param name the name to lookup up
4273 * @param proc the processor to call when finished
4274 * @param proc_cls the closure to pass to the processor
4277 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4278 struct GNUNET_CRYPTO_ShortHashCode pzone,
4280 GetAuthorityResultProcessor proc,
4283 struct ResolverHandle *rh;
4284 struct GetNameAuthorityHandle *nah;
4286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4287 "Starting authority resolution for %s!\n", name);
4289 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4290 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4291 rh->authority = zone;
4293 rh->private_local_zone = pzone;
4295 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4297 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4299 strcpy(rh->name, "\0");
4304 strlen(name)-strlen(GNUNET_GNS_TLD));
4305 memcpy(rh->name, name,
4306 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4309 memset(nah->name, 0,
4311 strcpy(nah->name, name);
4313 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4314 rh->authority_chain_tail = rh->authority_chain_head;
4315 rh->authority_chain_head->zone = zone;
4316 rh->proc = &handle_delegation_result_ns_get_auth;
4317 rh->proc_cls = (void*)nah;
4320 nah->proc_cls = proc_cls;
4321 strcpy (nah->result, "");
4323 /* Start delegation resolution in our namestore */
4324 resolve_delegation_ns(rh);
4328 /******** END GET AUTHORITY *************/
4330 /* end of gnunet-service-gns_resolver.c */