2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file gns/gnunet-service-gns_resolver.c
23 * @brief GNUnet GNS resolver logic
24 * @author Martin Schanzenbach
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_vpn_service.h"
33 #include "gnunet_dns_service.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "gns_protocol.h"
37 #include "gnunet_gns_service.h"
38 #include "gns_common.h"
39 #include "block_gns.h"
41 #include "gnunet-service-gns_resolver.h"
46 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
49 * DHT replication level
51 #define DHT_GNS_REPLICATION_LEVEL 5
54 * Maximum label length of DNS names
56 #define MAX_DNS_LABEL_LENGTH 63
60 * Our handle to the namestore service
62 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
65 * Our handle to the vpn service
67 static struct GNUNET_VPN_Handle *vpn_handle;
70 * Resolver handle to the dht
72 static struct GNUNET_DHT_Handle *dht_handle;
75 * Heap for parallel DHT lookups
77 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
80 * Heap for namestore queues
82 static struct GNUNET_CONTAINER_Heap *ns_task_heap;
85 * Maximum amount of parallel queries in background
87 static unsigned long long max_allowed_background_queries;
90 * Maximum amount of parallel namestore tasks in background
92 static unsigned long long max_allowed_ns_tasks;
95 * Wheather or not to ignore pending records
97 static int ignore_pending_records;
102 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
105 * Background shortening handles
107 static struct GetPseuAuthorityHandle *gph_head;
110 * Background shortening handles
112 static struct GetPseuAuthorityHandle *gph_tail;
115 * Resolver lookup list
117 static struct ResolverHandle *rlh_head;
120 * Resolver lookup list
122 static struct ResolverHandle *rlh_tail;
125 * Resolver shorten list
127 static struct ResolverHandle *nsh_head;
130 * Resolver shorten list
132 static struct ResolverHandle *nsh_tail;
135 * Resolver get auth list
137 static struct ResolverHandle *nah_head;
140 * Resolver get auth list
142 static struct ResolverHandle *nah_tail;
145 * Global configuration.
147 static const struct GNUNET_CONFIGURATION_Handle *cfg;
150 * a resolution identifier pool variable
151 * This is a non critical identifier useful for debugging
153 static unsigned long long rid = 0;
156 * Check if name is in srv format (_x._y.xxx)
159 * @return GNUNET_YES if true
165 int ret = GNUNET_YES;
169 if (NULL == strstr (name, "._"))
172 ndup = GNUNET_strdup (name);
175 if (NULL == strtok (NULL, "."))
178 if (NULL == strtok (NULL, "."))
181 if (NULL != strtok (NULL, "."))
191 * Determine if this name is canonical (is a legal name in a zone, without delegation);
192 * note that we do not test that the name does not contain illegal characters, we only
193 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
194 * even though they consist of multiple labels.
197 * a.b.gads = not canonical
199 * _foo._srv = canonical
200 * _f.bar = not canonical
202 * @param name the name to test
203 * @return GNUNET_YES if canonical
206 is_canonical (const char* name)
211 if (NULL == strchr (name, '.'))
216 while (NULL != (dot = strchr (pos, '.')))
226 free_get_pseu_authority_handle (struct GetPseuAuthorityHandle *gph)
228 gph->namestore_task = NULL;
229 GNUNET_free (gph->auth);
230 GNUNET_CRYPTO_rsa_key_free (gph->key);
231 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
237 * Callback that shortens authorities
239 * @param gph the handle containing the name to shorten
242 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
246 * Continueation for pkey record creation (shorten)
248 * @param cls a GetPseuAuthorityHandle
249 * @param success unused
253 create_pkey_cont (void* cls, int32_t success, const char* emsg)
255 //FIXME do sth with error
256 struct GetPseuAuthorityHandle* gph = cls;
258 free_get_pseu_authority_handle (gph);
263 * Namestore calls this function if we have record for this name.
264 * (or with rd_count=0 to indicate no matches)
266 * @param cls the pending query
267 * @param key the key of the zone we did the lookup
268 * @param expiration expiration date of the namestore entry
269 * @param name the name for which we need an authority
270 * @param rd_count the number of records with 'name'
271 * @param rd the record data
272 * @param signature the signature of the authority for the record data
275 process_pseu_lookup_ns (void* cls,
276 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
277 struct GNUNET_TIME_Absolute expiration,
278 const char *name, unsigned int rd_count,
279 const struct GNUNET_NAMESTORE_RecordData *rd,
280 const struct GNUNET_CRYPTO_RsaSignature *signature)
282 struct GetPseuAuthorityHandle* gph = cls;
283 struct GNUNET_NAMESTORE_RecordData new_pkey;
285 gph->namestore_task = NULL;
288 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
289 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
290 free_get_pseu_authority_handle (gph);
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
298 new_pkey.expiration_time = UINT64_MAX;
299 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
300 new_pkey.data = &gph->auth->zone;
301 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
302 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
303 | GNUNET_NAMESTORE_RF_PRIVATE
304 | GNUNET_NAMESTORE_RF_PENDING;
305 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
314 * process result of a dht pseu lookup
316 * @param gph the handle
317 * @param name the pseu result or NULL
320 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
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 = (struct GetPseuAuthorityHandle*)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, "+") == 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_RsaPublicKeyBinaryEncoded *key,
467 struct GNUNET_TIME_Absolute expiration,
469 unsigned int rd_count,
470 const struct GNUNET_NAMESTORE_RecordData *rd,
471 const struct GNUNET_CRYPTO_RsaSignature *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_RsaPublicKeyBinaryEncoded *zone_key,
540 struct GNUNET_TIME_Absolute expire,
543 const struct GNUNET_NAMESTORE_RecordData *rd,
544 const struct GNUNET_CRYPTO_RsaSignature *signature)
546 struct GetPseuAuthorityHandle* gph = cls;
548 gph->namestore_task = NULL;
551 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
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)
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576 "GNS_AUTO_PSEU: New authority %s discovered\n",
579 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
582 &process_zone_to_name_discover,
589 * Start shortening algorithm using auth as
592 * @param auth the authorities that were resolved
593 * @param key the private key for PKEY import
596 start_shorten (struct AuthorityChain *auth,
597 const struct GNUNET_CRYPTO_RsaPrivateKey *key)
599 struct GetPseuAuthorityHandle *gph;
600 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
601 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
603 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
604 if (NULL == (pb_key = GNUNET_CRYPTO_rsa_encode_key (key)))
606 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
607 "Failed to encode RSA key on shorten\n");
610 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
611 gph->key = GNUNET_CRYPTO_rsa_decode_key ((const char*) pb_key, ntohs (pb_key->len));
612 GNUNET_free (pb_key);
613 if (NULL == gph->key)
615 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
616 "Failed to decode RSA key on shorten\n");
620 GNUNET_CRYPTO_short_hash (&pkey,
621 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
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);
630 * Initialize the resolver
632 * @param nh the namestore handle
633 * @param dh the dht handle
634 * @param lz the local zone's hash
635 * @param c configuration handle
636 * @param max_bg_queries maximum number of parallel background queries in dht
637 * @param ignore_pending ignore records that still require user confirmation
639 * @return GNUNET_OK on success
642 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
643 struct GNUNET_DHT_Handle *dh,
644 struct GNUNET_CRYPTO_ShortHashCode lz,
645 const struct GNUNET_CONFIGURATION_Handle *c,
646 unsigned long long max_bg_queries,
651 return GNUNET_SYSERR;
654 namestore_handle = nh;
658 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
660 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
661 max_allowed_background_queries = max_bg_queries;
662 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
663 ignore_pending_records = ignore_pending;
664 GNUNET_RESOLVER_connect (cfg);
672 * @param cls closure to iterator
673 * @param node heap nodes
674 * @param element the namestorebgtask
675 * @param cost heap cost
676 * @return always GNUNET_YES
679 cleanup_pending_ns_tasks (void* cls,
680 struct GNUNET_CONTAINER_HeapNode *node,
682 GNUNET_CONTAINER_HeapCostType cost)
684 struct NamestoreBGTask *nbg = element;
686 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
687 "GNS_CLEANUP: Terminating ns task\n");
688 GNUNET_NAMESTORE_cancel (nbg->qe);
690 GNUNET_CONTAINER_heap_remove_node (node);
698 * @param rh resolver handle
699 * @param rlh record lookup handle
700 * @param rd_count number of results
704 finish_lookup (struct ResolverHandle *rh,
705 struct RecordLookupHandle* rlh,
706 unsigned int rd_count,
707 const struct GNUNET_NAMESTORE_RecordData *rd);
711 * Cleanup background lookups FIXME get rid of this?? YES this doesn't do
712 * anything! => find in code and remove all references to the heap
714 * @param cls closure to iterator
715 * @param node heap nodes
716 * @param element the resolver handle
717 * @param cost heap cost
718 * @return always GNUNET_YES
721 cleanup_pending_background_queries (void* cls,
722 struct GNUNET_CONTAINER_HeapNode *node,
724 GNUNET_CONTAINER_HeapCostType cost)
726 struct ResolverHandle *rh = element;
728 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
729 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
731 GNUNET_CONTAINER_heap_remove_node (node);
732 if (0 == GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
734 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
735 &cleanup_pending_ns_tasks,
743 * Helper function to free resolver handle
745 * @param rh the handle to free
748 free_resolver_handle (struct ResolverHandle* rh)
750 struct AuthorityChain *ac;
751 struct AuthorityChain *ac_next;
756 ac = rh->authority_chain_head;
765 if (NULL != rh->get_handle)
766 GNUNET_DHT_get_stop (rh->get_handle);
768 if (NULL != rh->dns_raw_packet)
769 GNUNET_free (rh->dns_raw_packet);
771 if (NULL != rh->namestore_task)
772 GNUNET_NAMESTORE_cancel (rh->namestore_task);
773 rh->namestore_task = NULL;
775 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
776 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
778 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
779 GNUNET_SCHEDULER_cancel (rh->timeout_task);
781 if (NULL != rh->dns_sock)
782 GNUNET_NETWORK_socket_close (rh->dns_sock);
783 if (NULL != rh->dns_resolver_handle)
784 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
786 if (NULL != rh->rd.data)
787 GNUNET_free ((void*)(rh->rd.data));
795 * @param rh resolver handle
796 * @param nsh name shorten handle
799 finish_shorten (struct ResolverHandle *rh,
800 struct NameShortenHandle *nsh);
806 * @param rh resolver handle
807 * @param nah get name authority handle
810 finish_get_auth (struct ResolverHandle *rh,
811 struct GetNameAuthorityHandle* rlh);
818 gns_resolver_cleanup ()
821 struct GetPseuAuthorityHandle *tmp;
823 while (NULL != (tmp = gph_head))
825 if (tmp->get_handle != NULL)
827 GNUNET_DHT_get_stop (tmp->get_handle);
828 tmp->get_handle = NULL;
830 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
832 GNUNET_SCHEDULER_cancel (tmp->timeout);
833 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
835 if (NULL != tmp->namestore_task)
837 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
838 tmp->namestore_task = NULL;
840 free_get_pseu_authority_handle (tmp);
843 while (NULL != rlh_head)
845 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
847 while (NULL != nsh_head)
849 finish_shorten (nsh_head, nsh_head->proc_cls);
851 while (NULL != nah_head)
853 finish_get_auth (nah_head, nah_head->proc_cls);
856 s = GNUNET_CONTAINER_heap_get_size (dht_lookup_heap);
857 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
858 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
860 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
861 &cleanup_pending_background_queries,
863 else if (0 != GNUNET_CONTAINER_heap_get_size (ns_task_heap))
865 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
866 &cleanup_pending_ns_tasks,
873 * Callback when record data is put into namestore
875 * @param cls the closure
876 * @param success GNUNET_OK on success
877 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
880 on_namestore_record_put_result (void *cls,
884 struct NamestoreBGTask *nbg = cls;
886 GNUNET_CONTAINER_heap_remove_node (nbg->node);
889 if (GNUNET_NO == success)
891 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
892 "GNS_NS: records already in namestore\n");
895 else if (GNUNET_YES == success)
897 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
898 "GNS_NS: records successfully put in namestore\n");
902 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
903 "GNS_NS: Error putting records into namestore: %s\n", emsg);
908 * Lookup timeout task
910 * @param cls the ResolverHandle for the task that timed out
911 * @param tc the task context
914 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
916 struct ResolverHandle *rh = cls;
918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
919 "Lookup timeout for request %llu triggered\n",
921 if (NULL != rh->timeout_cont)
922 rh->timeout_cont (rh->timeout_cont_cls, tc);
927 * Processor for background lookups in the DHT
929 * @param cls closure (NULL)
930 * @param rd_count number of records found (not 0)
931 * @param rd record data
934 background_lookup_result_processor (void *cls,
936 const struct GNUNET_NAMESTORE_RecordData *rd)
938 //We could do sth verbose/more useful here but it doesn't make any difference
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
940 "GNS_BG: background dht lookup finished. (%d results)\n",
946 * Handle timeout for DHT requests
948 * @param cls the request handle as closure
949 * @param tc the task context
952 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
954 struct ResolverHandle *rh = cls;
955 struct RecordLookupHandle *rlh = rh->proc_cls;
956 char new_name[MAX_DNS_NAME_LENGTH];
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus) timed out.\n",
960 rh->id, rh->name, rh->timeout.rel_value);
962 * Start resolution in bg
964 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
965 rh->name, GNUNET_GNS_TLD);
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
969 rh->id, new_name, rlh->record_type);
971 gns_resolver_lookup_record (rh->authority,
972 rh->private_local_zone,
976 GNUNET_TIME_UNIT_FOREVER_REL,
978 &background_lookup_result_processor,
981 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
983 GNUNET_DHT_get_stop (rh->get_handle);
984 rh->get_handle = NULL;
985 rh->proc (rh->proc_cls, rh, 0, NULL);
990 * Function called when we get a result from the dht
991 * for our record query
993 * @param cls the request handle
994 * @param exp lifetime
995 * @param key the key the record was stored under
996 * @param get_path get path
997 * @param get_path_length get path length
998 * @param put_path put path
999 * @param put_path_length put path length
1000 * @param type the block type
1001 * @param size the size of the record
1002 * @param data the record data
1005 process_record_result_dht (void* cls,
1006 struct GNUNET_TIME_Absolute exp,
1007 const struct GNUNET_HashCode * key,
1008 const struct GNUNET_PeerIdentity *get_path,
1009 unsigned int get_path_length,
1010 const struct GNUNET_PeerIdentity *put_path,
1011 unsigned int put_path_length,
1012 enum GNUNET_BLOCK_Type type,
1013 size_t size, const void *data)
1015 struct ResolverHandle *rh;
1016 struct RecordLookupHandle *rlh;
1017 struct GNSNameRecordBlock *nrb;
1018 uint32_t num_records;
1020 char* rd_data = (char*)data;
1024 rh = (struct ResolverHandle *)cls;
1025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1026 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
1028 rlh = (struct RecordLookupHandle *) rh->proc_cls;
1029 nrb = (struct GNSNameRecordBlock*)data;
1031 /* stop lookup and timeout task */
1032 GNUNET_DHT_get_stop (rh->get_handle);
1033 rh->get_handle = NULL;
1035 if (rh->dht_heap_node != NULL)
1037 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1038 rh->dht_heap_node = NULL;
1041 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1043 GNUNET_SCHEDULER_cancel (rh->timeout_task);
1044 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1047 rh->get_handle = NULL;
1048 name = (char*)&nrb[1];
1049 num_records = ntohl (nrb->rd_count);
1051 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1052 struct NamestoreBGTask *ns_heap_root;
1053 struct NamestoreBGTask *namestore_bg_task;
1055 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
1056 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
1058 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1063 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1064 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1065 rh->proc (rh->proc_cls, rh, 0, NULL);
1069 for (i = 0; i < num_records; i++)
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1073 rh->id, name, rh->name);
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1075 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1076 rh->id, rd[i].record_type, rlh->record_type);
1077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1078 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1079 rh->id, rd[i].data_size);
1080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081 "GNS_PHASE_REC-%llu: Got flag %d\n",
1082 rh->id, rd[i].flags);
1084 if ((strcmp (name, rh->name) == 0) &&
1085 (rd[i].record_type == rlh->record_type))
1091 * FIXME check pubkey against existing key in namestore?
1092 * https://gnunet.org/bugs/view.php?id=2179
1094 if (max_allowed_ns_tasks <=
1095 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1097 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1098 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1101 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1105 /* Save to namestore */
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1107 "GNS_PHASE_REC-%llu: Caching record for %s\n",
1109 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1110 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1117 &on_namestore_record_put_result, //cont
1120 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1122 GNUNET_TIME_absolute_get().abs_value);
1125 if (0 < rh->answered)
1126 rh->proc (rh->proc_cls, rh, num_records, rd);
1128 rh->proc (rh->proc_cls, rh, 0, NULL);
1135 * Start DHT lookup for a (name -> query->record_type) record in
1136 * rh->authority's zone
1138 * @param rh the pending gns query context
1141 resolve_record_dht (struct ResolverHandle *rh)
1144 struct GNUNET_HashCode lookup_key;
1145 struct RecordLookupHandle *rlh = rh->proc_cls;
1146 struct ResolverHandle *rh_heap_root;
1148 GNUNET_GNS_get_key_for_record (rh->name, &rh->authority, &lookup_key);
1149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1150 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1151 rh->id, rh->name, GNUNET_h2s (&lookup_key));
1153 rh->dht_heap_node = NULL;
1155 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
1158 * Update timeout if necessary
1160 if (GNUNET_SCHEDULER_NO_TASK == rh->timeout_task)
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1163 "GNS_PHASE_REC-%llu: Adjusting timeout to %s/2\n",
1165 GNUNET_STRINGS_relative_time_to_string (rh->timeout, GNUNET_YES));
1167 * Set timeout for authority lookup phase to 1/2
1169 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1170 GNUNET_TIME_relative_divide (rh->timeout, 2),
1171 &handle_lookup_timeout,
1174 rh->timeout_cont = &dht_lookup_timeout;
1175 rh->timeout_cont_cls = rh;
1179 if (max_allowed_background_queries <=
1180 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1182 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1183 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1184 rh_heap_root->get_handle = NULL;
1185 rh_heap_root->dht_heap_node = NULL;
1187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1188 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1189 rh->id, rh_heap_root->name);
1190 rh_heap_root->proc (rh_heap_root->proc_cls,
1195 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1197 GNUNET_TIME_absolute_get ().abs_value);
1200 xquery = htonl (rlh->record_type);
1202 GNUNET_assert (rh->get_handle == NULL);
1203 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1204 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1206 DHT_GNS_REPLICATION_LEVEL,
1207 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1210 &process_record_result_dht,
1217 * Namestore calls this function if we have record for this name.
1218 * (or with rd_count=0 to indicate no matches)
1220 * @param cls the pending query
1221 * @param key the key of the zone we did the lookup
1222 * @param expiration expiration date of the namestore entry
1223 * @param name the name for which we need an authority
1224 * @param rd_count the number of records with 'name'
1225 * @param rd the record data
1226 * @param signature the signature of the authority for the record data
1229 process_record_result_ns (void* cls,
1230 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1231 struct GNUNET_TIME_Absolute expiration,
1232 const char *name, unsigned int rd_count,
1233 const struct GNUNET_NAMESTORE_RecordData *rd,
1234 const struct GNUNET_CRYPTO_RsaSignature *signature)
1236 struct ResolverHandle *rh;
1237 struct RecordLookupHandle *rlh;
1238 struct GNUNET_TIME_Relative remaining_time;
1239 struct GNUNET_CRYPTO_ShortHashCode zone;
1240 struct GNUNET_TIME_Absolute et;
1246 rh->namestore_task = NULL;
1247 GNUNET_CRYPTO_short_hash (key,
1248 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1250 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1258 rh->status |= RSL_RECORD_EXISTS;
1260 if (remaining_time.rel_value == 0)
1261 rh->status |= RSL_RECORD_EXPIRED;
1267 * Lookup terminated and no results
1269 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1270 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1273 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1274 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1277 * Our zone and no result? Cannot resolve TT
1279 rh->proc(rh->proc_cls, rh, 0, NULL);
1283 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1284 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1286 for (i = 0; i < rd_count;i++)
1288 if (rd[i].record_type != rlh->record_type)
1291 if (ignore_pending_records &&
1292 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1294 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1295 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1300 //FIXME: eh? do I have to handle this here?
1301 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1302 et.abs_value = rd[i].expiration_time;
1303 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
1305 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1306 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1316 if (0 == rh->answered)
1318 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1319 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1320 rh->proc(rh->proc_cls, rh, 0, NULL);
1324 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1325 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1326 rh->id, rh->answered, rd_count);
1327 rh->proc(rh->proc_cls, rh, rd_count, rd);
1332 * VPN redirect result callback
1334 * @param cls the resolver handle
1335 * @param af the requested address family
1336 * @param address in_addr(6) respectively
1339 process_record_result_vpn (void* cls, int af, const void *address)
1341 struct ResolverHandle *rh = cls;
1342 struct RecordLookupHandle *rlh = rh->proc_cls;
1343 struct GNUNET_NAMESTORE_RecordData rd;
1345 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1346 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1350 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1351 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1353 if (GNUNET_GNS_RECORD_A != rlh->record_type)
1355 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1356 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1358 rh->proc (rh->proc_cls, rh, 0, NULL);
1361 rd.record_type = GNUNET_GNS_RECORD_A;
1362 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1364 rd.data_size = sizeof (struct in_addr);
1366 rh->proc (rh->proc_cls, rh, 1, &rd);
1369 else if (AF_INET6 == af)
1371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1372 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1374 if (GNUNET_GNS_RECORD_AAAA != rlh->record_type)
1376 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1377 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1379 rh->proc (rh->proc_cls, rh, 0, NULL);
1382 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1383 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1385 rd.data_size = sizeof (struct in6_addr);
1387 rh->proc (rh->proc_cls, rh, 1, &rd);
1391 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1392 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1394 rh->proc (rh->proc_cls, rh, 0, NULL);
1399 * Process VPN lookup result for record
1401 * @param cls the record lookup handle
1402 * @param rh resolver handle
1403 * @param rd_count number of results (1)
1404 * @param rd record data containing the result
1407 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1408 unsigned int rd_count,
1409 const struct GNUNET_NAMESTORE_RecordData *rd)
1411 struct RecordLookupHandle* rlh = cls;
1415 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1416 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1419 /* give up, cannot resolve */
1420 finish_lookup(rh, rlh, 0, NULL);
1424 /* results found yay */
1425 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1426 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1428 finish_lookup(rh, rlh, rd_count, rd);
1433 * Sends a UDP dns query to a nameserver specified in the rh
1435 * @param rh the resolver handle
1438 send_dns_packet (struct ResolverHandle *rh);
1444 * @param cls the ResolverHandle for this lookup
1445 * @param addr the sockaddr
1446 * @param addrlen the socket address length
1449 handle_dns_resolver (void *cls,
1450 const struct sockaddr *addr,
1453 struct ResolverHandle *rh = cls;
1454 struct RecordLookupHandle *rlh = rh->proc_cls;
1455 struct GNUNET_NAMESTORE_RecordData rd;
1456 struct sockaddr_in *sai;
1457 struct sockaddr_in6 *sai6;
1461 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1462 "No address found in DNS!\n");
1463 finish_lookup (rh, rlh, 0, NULL);
1467 if (sizeof (struct sockaddr_in) == addrlen)
1469 sai = (struct sockaddr_in*) addr;
1470 rd.record_type = GNUNET_GNS_RECORD_A;
1471 rd.data_size = sizeof (struct in_addr);
1472 rd.data = &sai->sin_addr;
1474 else if (sizeof (struct sockaddr_in6) == addrlen)
1476 sai6 = (struct sockaddr_in6*) addr;
1477 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1478 rd.data_size = sizeof (struct in6_addr);
1479 rd.data = &sai6->sin6_addr;
1483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1484 "Address length is garbage!\n");
1485 finish_lookup (rh, rlh, 0, NULL);
1488 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1490 finish_lookup (rh, rlh, 1, &rd);
1495 * Resolve DNS name via local stub resolver
1497 * @param rh the resolver handle
1500 resolve_dns_name (struct ResolverHandle *rh)
1502 struct RecordLookupHandle *rlh = rh->proc_cls;
1505 if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1506 (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1509 "Can only resolve A/AAAA via stub... abort\n");
1510 finish_lookup (rh, rlh, 0, NULL);
1514 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1519 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1522 &handle_dns_resolver,
1528 * Read DNS udp packet from socket
1530 * @param cls the resolver handle
1531 * @param tc task context
1534 read_dns_response (void *cls,
1535 const struct GNUNET_SCHEDULER_TaskContext *tc)
1537 struct ResolverHandle *rh = cls;
1538 struct RecordLookupHandle *rlh = rh->proc_cls;
1539 char buf[UINT16_MAX];
1541 struct sockaddr_in addr;
1543 struct GNUNET_DNSPARSER_Packet *packet;
1544 struct GNUNET_NAMESTORE_RecordData rd;
1545 int found_delegation = GNUNET_NO;
1546 int found_cname = GNUNET_NO;
1547 char* delegation_name = NULL;
1548 int zone_offset = 0;
1551 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1552 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1554 /* timeout or shutdown */
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556 "Terminating DNS query %d\n", tc->reason);
1557 finish_lookup (rh, rlh, 0, NULL);
1561 addrlen = sizeof (addr);
1562 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1564 (struct sockaddr*) &addr,
1569 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1570 finish_lookup (rh, rlh, 0, NULL);
1574 packet = GNUNET_DNSPARSER_parse (buf, r);
1578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1579 "Failed to parse DNS reply!\n");
1580 finish_lookup (rh, rlh, 0, NULL);
1584 for (i = 0; i < packet->num_answers; i++)
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1587 "Got record type %d (want %d)\n",
1588 packet->answers[i].type,
1590 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1591 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1594 "CNAME... restarting query with %s\n",
1595 packet->answers[i].data.hostname
1597 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1598 found_cname = GNUNET_YES;
1602 if ((packet->answers[i].type == rlh->record_type) &&
1603 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1607 rd.data = packet->answers[i].data.raw.data;
1608 rd.data_size = packet->answers[i].data.raw.data_len;
1609 rd.record_type = packet->answers[i].type;
1611 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1612 finish_lookup (rh, rlh, 1, &rd);
1613 GNUNET_DNSPARSER_free_packet (packet);
1618 if (GNUNET_YES == found_cname)
1620 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1622 if (0 > zone_offset)
1625 /* restart query with CNAME */
1626 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1629 "Asking same server for %s\n", rh->dns_name);
1630 send_dns_packet (rh);
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1635 "Trying system resolver for %s\n", rh->dns_name);
1636 resolve_dns_name (rh);
1639 GNUNET_DNSPARSER_free_packet (packet);
1643 for (i = 0; i < packet->num_authority_records; i++)
1646 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1649 "Found NS delegation!\n");
1650 found_delegation = GNUNET_YES;
1651 delegation_name = packet->authority_records[i].data.hostname;
1656 for (i = 0; i < packet->num_additional_records; i++)
1658 if (GNUNET_NO == found_delegation)
1661 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1662 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1664 GNUNET_assert (sizeof (struct in_addr) ==
1665 packet->authority_records[i].data.raw.data_len);
1667 rh->dns_addr.sin_addr =
1668 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1669 send_dns_packet (rh);
1670 GNUNET_DNSPARSER_free_packet (packet);
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "Nothing useful in DNS reply!\n");
1677 finish_lookup (rh, rlh, 0, NULL);
1678 GNUNET_DNSPARSER_free_packet (packet);
1684 * Sends a UDP dns query to a nameserver specified in the rh
1686 * @param rh the request handle
1689 send_dns_packet (struct ResolverHandle *rh)
1691 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1692 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "Sending %dbyte DNS query\n",
1696 rh->dns_raw_packet_size);
1698 if (GNUNET_SYSERR ==
1699 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1701 rh->dns_raw_packet_size,
1702 (struct sockaddr*)&rh->dns_addr,
1703 sizeof (struct sockaddr_in)))
1704 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1705 _("Failed to send DNS request to %s\n"),
1706 GNUNET_a2s ((const struct sockaddr *)&rh->dns_addr,
1707 sizeof (struct sockaddr_in)));
1709 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1710 rh->timeout, //FIXME less?
1716 GNUNET_NETWORK_fdset_destroy (rset);
1722 * The final phase of resoution.
1723 * We found a NS RR and want to resolve via DNS
1725 * @param rh the pending lookup handle
1726 * @param rd_count length of record data
1727 * @param rd record data containing VPN RR
1730 resolve_record_dns (struct ResolverHandle *rh,
1732 const struct GNUNET_NAMESTORE_RecordData *rd)
1734 struct GNUNET_DNSPARSER_Query query;
1735 struct GNUNET_DNSPARSER_Packet packet;
1736 struct GNUNET_DNSPARSER_Flags flags;
1737 struct in_addr dnsip;
1738 struct sockaddr_in addr;
1739 struct sockaddr *sa;
1741 struct RecordLookupHandle *rlh = rh->proc_cls;
1743 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1744 memset (rh->dns_name, 0, sizeof (rh->dns_name));
1746 /* We cancel here as to not include the ns lookup in the timeout */
1747 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1749 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1750 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1752 /* Start shortening */
1753 if ((NULL != rh->priv_key) &&
1754 (GNUNET_YES == is_canonical (rh->name)))
1756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1757 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1759 start_shorten (rh->authority_chain_head,
1763 for (i = 0; i < rd_count; i++)
1765 /* Synthesize dns name */
1766 if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1768 strcpy (rh->dns_zone, (char*)rd[i].data);
1769 if (0 == strcmp (rh->name, ""))
1770 strcpy (rh->dns_name, (char*)rd[i].data);
1772 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1775 if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1776 /* need to use memcpy as .data may be unaligned */
1777 memcpy (&dnsip, rd[i].data, sizeof (dnsip));
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1781 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1785 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1786 if (NULL == rh->dns_sock)
1788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1789 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1791 finish_lookup (rh, rlh, 0, NULL);
1795 memset (&addr, 0, sizeof (struct sockaddr_in));
1796 sa = (struct sockaddr *) &addr;
1797 sa->sa_family = AF_INET;
1798 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1800 sizeof (struct sockaddr_in)))
1802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1803 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1805 finish_lookup (rh, rlh, 0, NULL);
1808 query.name = rh->dns_name;
1809 query.type = rlh->record_type;
1810 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1811 memset (&flags, 0, sizeof (flags));
1812 flags.recursion_desired = 1;
1813 flags.checking_disabled = 1;
1814 packet.queries = &query;
1815 packet.answers = NULL;
1816 packet.authority_records = NULL;
1817 packet.num_queries = 1;
1818 packet.num_answers = 0;
1819 packet.num_authority_records = 0;
1820 packet.num_additional_records = 0;
1821 packet.flags = flags;
1823 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1825 &rh->dns_raw_packet,
1826 &rh->dns_raw_packet_size))
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1829 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1831 GNUNET_NETWORK_socket_close (rh->dns_sock);
1832 finish_lookup (rh, rlh, 0, NULL);
1836 rh->dns_addr.sin_family = AF_INET;
1837 rh->dns_addr.sin_port = htons (53); //domain
1838 rh->dns_addr.sin_addr = dnsip;
1839 #if HAVE_SOCKADDR_IN_SIN_LEN
1840 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1843 send_dns_packet (rh);
1848 * The final phase of resoution.
1849 * We found a VPN RR and want to request an IPv4/6 address
1851 * @param rh the pending lookup handle
1852 * @param rd_count length of record data
1853 * @param rd record data containing VPN RR
1856 resolve_record_vpn (struct ResolverHandle *rh,
1858 const struct GNUNET_NAMESTORE_RecordData *rd)
1860 struct RecordLookupHandle *rlh = rh->proc_cls;
1861 struct GNUNET_HashCode serv_desc;
1862 struct vpn_data* vpn;
1865 /* We cancel here as to not include the ns lookup in the timeout */
1866 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1868 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1869 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1871 /* Start shortening */
1872 if ((NULL != rh->priv_key) &&
1873 (GNUNET_YES == is_canonical (rh->name)))
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1878 start_shorten (rh->authority_chain_head,
1882 vpn = (struct vpn_data*)rd->data;
1885 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1886 strlen ((char*)&vpn[1]) + 1,
1888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1889 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1892 GNUNET_h2s (&vpn->peer));
1894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1895 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1898 GNUNET_h2s (&serv_desc));
1899 rh->proc = &handle_record_vpn;
1900 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1904 if (NULL == vpn_handle)
1906 vpn_handle = GNUNET_VPN_connect (cfg);
1907 if (NULL == vpn_handle)
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1910 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1911 finish_lookup (rh, rh->proc_cls, 0, NULL);
1916 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1917 af, ntohs (vpn->proto),
1918 (struct GNUNET_PeerIdentity *)&vpn->peer,
1921 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1922 &process_record_result_vpn,
1928 * The final phase of resolution.
1929 * rh->name is a name that is canonical and we do not have a delegation.
1930 * Query namestore for this record
1932 * @param rh the pending lookup handle
1935 resolve_record_ns(struct ResolverHandle *rh)
1937 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1939 /* We cancel here as to not include the ns lookup in the timeout */
1940 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1942 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1943 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1945 /* Start shortening */
1946 if ((NULL != rh->priv_key) &&
1947 (GNUNET_YES == is_canonical (rh->name)))
1949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1950 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1952 start_shorten (rh->authority_chain_head,
1957 * Try to resolve this record in our namestore.
1958 * The name to resolve is now in rh->authority_name
1959 * since we tried to resolve it to an authority
1962 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1966 &process_record_result_ns,
1972 * Handle timeout for DHT requests
1974 * @param cls the request handle as closure
1975 * @param tc the task context
1978 dht_authority_lookup_timeout (void *cls,
1979 const struct GNUNET_SCHEDULER_TaskContext *tc)
1981 struct ResolverHandle *rh = cls;
1982 struct RecordLookupHandle *rlh = rh->proc_cls;
1983 char new_name[MAX_DNS_NAME_LENGTH];
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1986 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus) timed out.\n",
1987 rh->id, rh->authority_name,
1988 rh->timeout.rel_value);
1990 rh->status |= RSL_TIMED_OUT;
1991 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1992 if (NULL != rh->get_handle)
1994 GNUNET_DHT_get_stop (rh->get_handle);
1995 rh->get_handle = NULL;
1997 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2000 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
2002 rh->proc (rh->proc_cls, rh, 0, NULL);
2005 if (0 == strcmp (rh->name, ""))
2008 * promote authority back to name and try to resolve record
2010 strcpy (rh->name, rh->authority_name);
2011 rh->proc (rh->proc_cls, rh, 0, NULL);
2016 * Start resolution in bg
2018 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
2020 rh->name, rh->authority_name, GNUNET_GNS_TLD);
2021 strcpy (rh->name, new_name);
2022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2023 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2026 gns_resolver_lookup_record (rh->authority,
2027 rh->private_local_zone,
2031 GNUNET_TIME_UNIT_FOREVER_REL,
2033 &background_lookup_result_processor,
2035 rh->proc (rh->proc_cls, rh, 0, NULL);
2040 * Start DHT lookup for a name -> PKEY (compare NS) record in
2041 * rh->authority's zone
2043 * @param rh the pending gns query
2046 resolve_delegation_dht(struct ResolverHandle *rh);
2050 * Resolve the delegation chain for the request in our namestore
2052 * @param rh the resolver handle
2055 resolve_delegation_ns(struct ResolverHandle *rh);
2059 * Namestore resolution for delegation finished. Processing result.
2061 * @param cls the closure
2062 * @param rh resolver handle
2063 * @param rd_count number of results (always 0)
2064 * @param rd record data (always NULL)
2067 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2068 unsigned int rd_count,
2069 const struct GNUNET_NAMESTORE_RecordData *rd);
2073 * This is a callback function that checks for key revocation
2075 * @param cls the pending query
2076 * @param key the key of the zone we did the lookup
2077 * @param expiration expiration date of the record data set in the namestore
2078 * @param name the name for which we need an authority
2079 * @param rd_count the number of records with 'name'
2080 * @param rd the record data
2081 * @param signature the signature of the authority for the record data
2084 process_pkey_revocation_result_ns (void *cls,
2085 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2086 struct GNUNET_TIME_Absolute expiration,
2088 unsigned int rd_count,
2089 const struct GNUNET_NAMESTORE_RecordData *rd,
2090 const struct GNUNET_CRYPTO_RsaSignature *signature)
2092 struct ResolverHandle *rh = cls;
2093 struct GNUNET_TIME_Relative remaining_time;
2096 rh->namestore_task = NULL;
2097 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2099 for (i = 0; i < rd_count; i++)
2101 if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2103 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2104 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2106 rh->status |= RSL_PKEY_REVOKED;
2107 rh->proc (rh->proc_cls, rh, 0, NULL);
2112 if ((NULL == name) ||
2113 (0 == remaining_time.rel_value))
2115 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2116 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2119 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2121 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2122 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2123 rh->id, "+.gads", GNUNET_GNS_RECORD_REV);
2125 gns_resolver_lookup_record(rh->authority,
2126 rh->private_local_zone,
2127 GNUNET_GNS_RECORD_REV,
2130 GNUNET_TIME_UNIT_FOREVER_REL,
2132 &background_lookup_result_processor,
2136 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2137 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2140 * We are done with PKEY resolution if name is empty
2141 * else resolve again with new authority
2143 if (strcmp (rh->name, "") == 0)
2144 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2146 resolve_delegation_ns (rh);
2151 * Callback when record data is put into namestore
2153 * @param cls the closure
2154 * @param success GNUNET_OK on success
2155 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2158 on_namestore_delegation_put_result(void *cls,
2162 struct NamestoreBGTask *nbg = cls;
2164 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2167 if (GNUNET_NO == success)
2169 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2170 "GNS_NS: records already in namestore\n");
2173 else if (GNUNET_YES == success)
2175 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2176 "GNS_NS: records successfully put in namestore\n");
2180 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2181 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2186 * Function called when we get a result from the dht
2187 * for our query. Recursively tries to resolve authorities
2190 * @param cls the request handle
2191 * @param exp lifetime
2192 * @param key the key the record was stored under
2193 * @param get_path get path
2194 * @param get_path_length get path length
2195 * @param put_path put path
2196 * @param put_path_length put path length
2197 * @param type the block type
2198 * @param size the size of the record
2199 * @param data the record data
2202 process_delegation_result_dht (void* cls,
2203 struct GNUNET_TIME_Absolute exp,
2204 const struct GNUNET_HashCode * key,
2205 const struct GNUNET_PeerIdentity *get_path,
2206 unsigned int get_path_length,
2207 const struct GNUNET_PeerIdentity *put_path,
2208 unsigned int put_path_length,
2209 enum GNUNET_BLOCK_Type type,
2210 size_t size, const void *data)
2212 struct ResolverHandle *rh = cls;
2213 const struct GNSNameRecordBlock *nrb = data;
2214 const char* rd_data;
2215 uint32_t num_records;
2219 struct GNUNET_CRYPTO_ShortHashCode zone;
2221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2222 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n",
2227 nrb = (struct GNSNameRecordBlock*)data;
2229 /* stop dht lookup and timeout task */
2230 GNUNET_DHT_get_stop (rh->get_handle);
2231 rh->get_handle = NULL;
2232 if (rh->dht_heap_node != NULL)
2234 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2235 rh->dht_heap_node = NULL;
2238 num_records = ntohl(nrb->rd_count);
2239 name = (const char*) &nrb[1];
2241 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2242 struct NamestoreBGTask *ns_heap_root;
2243 struct NamestoreBGTask *namestore_bg_task;
2245 rd_data = name + strlen(name) + 1;
2246 rd_size = size - strlen(name) - 1 - sizeof (struct GNSNameRecordBlock);
2247 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2252 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2253 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2258 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2259 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2260 rh->id, name, rh->authority_name);
2261 for (i=0; i<num_records; i++)
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2264 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2265 rh->id, name, rh->authority_name);
2266 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2267 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2268 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2269 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2270 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2271 rh->id, rd[i].data_size);
2272 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2273 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2274 rh->id, rd[i].flags);
2276 if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2277 (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2278 (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2281 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2283 if (0 == strcmp(rh->name, ""))
2284 strcpy(rh->name, rh->authority_name);
2286 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2287 rh->name, rh->authority_name); //FIXME ret
2292 if ((0 == strcmp(name, rh->authority_name)) &&
2293 (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2295 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2296 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2299 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2300 struct AuthorityChain *auth =
2301 GNUNET_malloc(sizeof(struct AuthorityChain));
2302 auth->zone = rh->authority;
2303 memset(auth->name, 0, strlen(rh->authority_name)+1);
2304 strcpy(auth->name, rh->authority_name);
2305 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2306 rh->authority_chain_tail,
2309 if (NULL != rh->rd.data)
2310 GNUNET_free ((void*)rh->rd.data);
2312 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2313 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2314 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2317 /** try to import pkey if private key available */
2318 //if (rh->priv_key && is_canonical (rh->name))
2319 // process_discovered_authority(name, auth->zone,
2320 // rh->authority_chain_tail->zone,
2325 GNUNET_GNS_get_zone_from_key (name, key, &zone);
2328 /* Save to namestore
2329 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2332 if (max_allowed_ns_tasks <=
2333 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2335 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2336 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2338 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2339 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background ns task\n",
2342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2343 "GNS_PHASE_DELEGATE_DHT-%llu: Caching record for %s\n",
2345 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2347 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2349 GNUNET_TIME_absolute_get().abs_value);
2350 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2357 &on_namestore_delegation_put_result, //cont
2358 namestore_bg_task); //cls
2362 if (0 != rh->answered)
2367 * FIXME in this case. should we ask namestore again?
2369 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2370 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2371 rh->id, rh->authority_name, rh->name);
2373 if (0 == strcmp(rh->name, ""))
2375 /* Start shortening */
2376 if ((NULL != rh->priv_key) &&
2377 (GNUNET_YES == is_canonical (rh->name)))
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2380 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2382 start_shorten (rh->authority_chain_head,
2387 rh->proc = &handle_delegation_ns;
2390 /* Check for key revocation and delegate */
2391 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2394 GNUNET_GNS_RECORD_REV,
2395 &process_pkey_revocation_result_ns,
2402 * No pkey but name exists
2405 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2406 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2407 rh->id, rh->authority_name, rh->name);
2408 if (0 == strcmp(rh->name, ""))
2409 strcpy(rh->name, rh->authority_name);
2411 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2412 rh->name, rh->authority_name); //FIXME ret
2414 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2415 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2416 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2417 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2419 rh->proc(rh->proc_cls, rh, 0, NULL);
2422 //FIXME maybe define somewhere else?
2423 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2424 +(MAX_DNS_NAME_LENGTH*2)
2425 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2426 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2430 * Exands a name ending in .+ with the zone of origin.
2431 * FIXME: funky api: 'dest' must be large enough to hold
2432 * the result; this is a bit yucky...
2434 * @param dest destination buffer
2435 * @param src the .+ name
2436 * @param repl the string to replace the + with
2439 expand_plus (char* dest,
2444 size_t s_len = strlen (src) + 1;
2446 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2449 /* no postprocessing */
2450 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2451 "GNS_POSTPROCESS: %s too short\n", src);
2452 memcpy (dest, src, s_len);
2455 if (0 == strcmp (src + s_len - 3, ".+"))
2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2458 "GNS_POSTPROCESS: Expanding .+ in %s\n",
2460 memset (dest, 0, s_len + strlen (repl) + strlen(GNUNET_GNS_TLD));
2462 pos = dest + s_len - 2;
2464 pos += strlen (repl);
2465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2466 "GNS_POSTPROCESS: Expanded to %s\n",
2471 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2472 "GNS_POSTPROCESS: No postprocessing for %s\n", src);
2473 memcpy (dest, src, s_len);
2482 finish_lookup (struct ResolverHandle *rh,
2483 struct RecordLookupHandle* rlh,
2484 unsigned int rd_count,
2485 const struct GNUNET_NAMESTORE_RecordData *rd)
2488 char new_rr_data[MAX_DNS_NAME_LENGTH];
2489 char new_mx_data[MAX_MX_LENGTH];
2490 char new_soa_data[MAX_SOA_LENGTH];
2491 char new_srv_data[MAX_SRV_LENGTH];
2492 struct srv_data *old_srv;
2493 struct srv_data *new_srv;
2494 struct soa_data *old_soa;
2495 struct soa_data *new_soa;
2496 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2499 unsigned int offset;
2501 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
2503 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2504 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2507 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2510 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2512 for (i = 0; i < rd_count; i++)
2515 if ((GNUNET_GNS_RECORD_NS != rd[i].record_type) &&
2516 (GNUNET_GNS_RECORD_PTR != rd[i].record_type) &&
2517 (GNUNET_GNS_RECORD_CNAME != rd[i].record_type) &&
2518 (GNUNET_GNS_RECORD_MX != rd[i].record_type) &&
2519 (GNUNET_GNS_RECORD_SOA != rd[i].record_type) &&
2520 (GNUNET_GNS_RECORD_SRV != rd[i].record_type))
2522 p_rd[i].data = rd[i].data;
2527 * for all those records we 'should'
2528 * also try to resolve the A/AAAA records (RFC1035)
2529 * This is a feature and not important
2532 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2533 "GNS_POSTPROCESS: Postprocessing\n");
2534 if (0 == strcmp(rh->name, "+"))
2535 repl_string = rlh->name;
2537 repl_string = rlh->name+strlen(rh->name)+1;
2540 if (GNUNET_GNS_RECORD_MX == rd[i].record_type)
2542 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2543 offset = sizeof (uint16_t);
2544 pos = new_mx_data + offset;
2545 // FIXME: how do we know that 'pos' has enough space for the new name?
2546 expand_plus (pos, (char*)rd[i].data+sizeof(uint16_t),
2548 offset += strlen(new_mx_data+sizeof(uint16_t)) + 1;
2549 p_rd[i].data = new_mx_data;
2550 p_rd[i].data_size = offset;
2552 else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2555 * Prio, weight and port
2557 new_srv = (struct srv_data*)new_srv_data;
2558 old_srv = (struct srv_data*)rd[i].data;
2559 new_srv->prio = old_srv->prio;
2560 new_srv->weight = old_srv->weight;
2561 new_srv->port = old_srv->port;
2562 // FIXME: how do we know that '&new_srv[1]' has enough space for the new name?
2563 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2565 p_rd[i].data = new_srv_data;
2566 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2568 else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2570 /* expand mname and rname */
2571 old_soa = (struct soa_data*)rd[i].data;
2572 new_soa = (struct soa_data*)new_soa_data;
2573 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2574 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2575 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2576 offset = strlen ((char*)&new_soa[1]) + 1;
2577 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2578 expand_plus((char*)&new_soa[1] + offset,
2579 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2581 p_rd[i].data_size = sizeof (struct soa_data)
2583 + strlen ((char*)&new_soa[1] + offset);
2584 p_rd[i].data = new_soa_data;
2589 // FIXME: how do we know that 'rd[i].data' has enough space for the new name?
2590 expand_plus(pos, (char*)rd[i].data, repl_string);
2591 p_rd[i].data_size = strlen(new_rr_data)+1;
2592 p_rd[i].data = new_rr_data;
2597 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2599 free_resolver_handle (rh);
2604 * Process DHT lookup result for record.
2606 * @param cls the closure
2607 * @param rh resolver handle
2608 * @param rd_count number of results
2609 * @param rd record data
2612 handle_record_dht(void* cls, struct ResolverHandle *rh,
2613 unsigned int rd_count,
2614 const struct GNUNET_NAMESTORE_RecordData *rd)
2616 struct RecordLookupHandle* rlh = cls;
2620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2621 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2623 /* give up, cannot resolve */
2624 finish_lookup (rh, rlh, 0, NULL);
2627 /* results found yay */
2628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2629 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2630 finish_lookup (rh, rlh, rd_count, rd);
2635 * Process namestore lookup result for record.
2637 * @param cls the closure
2638 * @param rh resolver handle
2639 * @param rd_count number of results
2640 * @param rd record data
2643 handle_record_ns (void* cls, struct ResolverHandle *rh,
2644 unsigned int rd_count,
2645 const struct GNUNET_NAMESTORE_RecordData *rd)
2647 struct RecordLookupHandle* rlh = cls;
2648 int check_dht = GNUNET_YES;
2652 /* results found yay */
2653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2654 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2655 finish_lookup (rh, rlh, rd_count, rd);
2659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2660 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2664 * There are 5 conditions that have to met for us to consult the DHT:
2665 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2666 * 2. No entry in the NS existed AND
2667 * 3. The zone queried is not the local resolver's zone AND
2668 * 4. The name that was looked up is '+'
2669 * because if it was any other canonical name we either already queried
2670 * the DHT for the authority in the authority lookup phase (and thus
2671 * would already have an entry in the NS for the record)
2672 * 5. We are not in cache only mode
2674 if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2675 (0 == (rh->status & RSL_RECORD_EXISTS)) )
2678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2679 "GNS_PHASE_REC-%llu: Not expired and exists!\n",
2681 check_dht = GNUNET_NO;
2684 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2685 &rh->private_local_zone))
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2689 "GNS_PHASE_REC-%llu: Our zone!\n",
2691 check_dht = GNUNET_NO;
2694 if ((0 != strcmp (rh->name, "+")) && (GNUNET_YES == is_srv (rh->name)))
2695 check_dht = GNUNET_NO;
2697 if (GNUNET_YES == rh->only_cached)
2698 check_dht = GNUNET_NO;
2700 if (GNUNET_YES == check_dht)
2702 rh->proc = &handle_record_dht;
2703 resolve_record_dht(rh);
2706 /* give up, cannot resolve */
2707 finish_lookup (rh, rlh, 0, NULL);
2712 * Move one level up in the domain hierarchy and return the
2713 * passed top level domain.
2715 * @param name the domain
2716 * @param dest the destination where the tld will be put
2719 pop_tld (char* name, char* dest)
2723 if (GNUNET_YES == is_canonical (name))
2730 for (len = strlen(name); 0 < len; len--)
2732 if (*(name+len) == '.')
2742 strcpy(dest, (name+len+1));
2747 * Checks if name is in tld
2749 * @param name the name to check
2750 * @param tld the TLD to check for
2751 * @return GNUNET_YES or GNUNET_NO
2754 is_tld(const char* name, const char* tld)
2758 if (strlen(name) <= strlen(tld))
2761 offset = strlen(name)-strlen(tld);
2762 if (0 != strcmp(name+offset, tld))
2764 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2765 "%s is not in .%s TLD\n", name, tld);
2773 * DHT resolution for delegation finished. Processing result.
2775 * @param cls the closure
2776 * @param rh resolver handle
2777 * @param rd_count number of results (always 0)
2778 * @param rd record data (always NULL)
2781 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2782 unsigned int rd_count,
2783 const struct GNUNET_NAMESTORE_RecordData *rd)
2785 struct RecordLookupHandle* rlh = cls;
2787 if (0 == strcmp(rh->name, ""))
2789 if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2791 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2792 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2794 finish_lookup(rh, rlh, rd_count, rd);
2797 /* We resolved full name for delegation. resolving record */
2798 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2799 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2801 strcpy(rh->name, "+\0");
2802 rh->proc = &handle_record_ns;
2803 resolve_record_ns(rh);
2808 * we still have some left
2810 if (GNUNET_YES == is_canonical (rh->name))
2812 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2813 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2816 rh->proc = &handle_record_ns;
2817 resolve_record_ns(rh);
2820 /* give up, cannot resolve */
2821 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2822 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2824 finish_lookup(rh, rlh, 0, NULL);
2829 * Start DHT lookup for a name -> PKEY (compare NS) record in
2830 * rh->authority's zone
2832 * @param rh the pending gns query
2835 resolve_delegation_dht (struct ResolverHandle *rh)
2838 struct GNUNET_HashCode lookup_key;
2839 struct ResolverHandle *rh_heap_root;
2841 pop_tld (rh->name, rh->authority_name);
2842 GNUNET_GNS_get_key_for_record (rh->authority_name,
2845 rh->dht_heap_node = NULL;
2846 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2848 rh->timeout_cont = &dht_authority_lookup_timeout;
2849 rh->timeout_cont_cls = rh;
2853 if (max_allowed_background_queries <=
2854 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2856 /* terminate oldest lookup */
2857 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2858 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2859 rh_heap_root->get_handle = NULL;
2860 rh_heap_root->dht_heap_node = NULL;
2861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2862 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2864 rh_heap_root->authority_name);
2865 rh_heap_root->proc (rh_heap_root->proc_cls,
2870 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2872 GNUNET_TIME_absolute_get().abs_value);
2874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2875 "Beginning DHT lookup for %s in zone %s for request %llu\n",
2877 GNUNET_short_h2s (&rh->authority),
2879 xquery = htonl (GNUNET_GNS_RECORD_PKEY);
2880 GNUNET_assert (rh->get_handle == NULL);
2881 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
2882 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2884 DHT_GNS_REPLICATION_LEVEL,
2885 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2888 &process_delegation_result_dht,
2894 * Namestore resolution for delegation finished. Processing result.
2896 * @param cls the closure
2897 * @param rh resolver handle
2898 * @param rd_count number of results (always 0)
2899 * @param rd record data (always NULL)
2902 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2903 unsigned int rd_count,
2904 const struct GNUNET_NAMESTORE_RecordData *rd)
2906 struct RecordLookupHandle* rlh;
2908 int check_dht = GNUNET_YES;
2911 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2912 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2913 rh->id, rh->status);
2915 if (rh->status & RSL_PKEY_REVOKED)
2917 finish_lookup (rh, rlh, 0, NULL);
2921 if (0 == strcmp(rh->name, ""))
2924 /* We resolved full name for delegation. resolving record */
2925 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2926 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2928 if (rh->status & RSL_CNAME_FOUND)
2930 if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2932 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2933 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2935 strcpy (rh->name, rh->authority_name);
2936 finish_lookup (rh, rlh, rd_count, rd);
2941 if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2943 s_len = strlen (rd->data) - 2;
2944 memcpy (rh->name, rd->data, s_len);
2945 rh->name[s_len] = '\0';
2946 resolve_delegation_ns (rh);
2949 else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2951 gns_resolver_lookup_record (rh->authority,
2952 rh->private_local_zone,
2961 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2962 free_resolver_handle (rh);
2968 strcpy (rh->dns_name, (char*)rd->data);
2969 resolve_dns_name (rh);
2974 else if (rh->status & RSL_DELEGATE_VPN)
2976 if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2978 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2979 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2981 finish_lookup(rh, rlh, rd_count, rd);
2984 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2985 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2987 GNUNET_assert (NULL != rd);
2988 rh->proc = &handle_record_vpn;
2989 resolve_record_vpn (rh, rd_count, rd);
2992 else if (rh->status & RSL_DELEGATE_NS)
2994 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2996 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2997 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2999 finish_lookup(rh, rlh, rd_count, rd);
3003 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3004 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3006 GNUNET_assert (NULL != rd);
3007 rh->proc = &handle_record_ns;
3008 resolve_record_dns (rh, rd_count, rd);
3011 else if (rh->status & RSL_DELEGATE_PKEY)
3013 if (rh->status & RSL_PKEY_REVOKED)
3015 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3016 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
3018 finish_lookup (rh, rlh, 0, NULL);
3021 else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
3023 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3024 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
3026 finish_lookup(rh, rlh, rd_count, rd);
3030 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3031 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3033 strcpy(rh->name, "+\0");
3034 rh->proc = &handle_record_ns;
3035 resolve_record_ns(rh);
3039 if (rh->status & RSL_DELEGATE_NS)
3041 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
3043 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3044 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3046 finish_lookup(rh, rlh, rd_count, rd);
3050 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3051 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3053 GNUNET_assert (NULL != rd);
3054 rh->proc = &handle_record_ns;
3055 resolve_record_dns (rh, rd_count, rd);
3060 * we still have some left
3061 * check if authority in ns is fresh
3063 * or we are authority
3066 if ((rh->status & RSL_RECORD_EXISTS) &&
3067 !(rh->status & RSL_RECORD_EXPIRED))
3068 check_dht = GNUNET_NO;
3070 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3071 &rh->private_local_zone))
3072 check_dht = GNUNET_NO;
3074 if (GNUNET_YES == rh->only_cached)
3075 check_dht = GNUNET_NO;
3077 if (GNUNET_YES == check_dht)
3080 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3081 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3083 rh->proc = &handle_delegation_dht;
3084 resolve_delegation_dht(rh);
3088 if (GNUNET_NO == is_canonical (rh->name))
3090 /* give up, cannot resolve */
3091 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3092 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3095 finish_lookup(rh, rlh, rd_count, rd);
3098 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3099 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3102 rh->proc = &handle_record_ns;
3103 resolve_record_ns(rh);
3108 * This is a callback function that should give us only PKEY
3109 * records. Used to query the namestore for the authority (PKEY)
3110 * for 'name'. It will recursively try to resolve the
3111 * authority for a given name from the namestore.
3113 * @param cls the pending query
3114 * @param key the key of the zone we did the lookup
3115 * @param expiration expiration date of the record data set in the namestore
3116 * @param name the name for which we need an authority
3117 * @param rd_count the number of records with 'name'
3118 * @param rd the record data
3119 * @param signature the signature of the authority for the record data
3122 process_delegation_result_ns (void* cls,
3123 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3124 struct GNUNET_TIME_Absolute expiration,
3126 unsigned int rd_count,
3127 const struct GNUNET_NAMESTORE_RecordData *rd,
3128 const struct GNUNET_CRYPTO_RsaSignature *signature)
3130 struct ResolverHandle *rh = cls;
3131 struct GNUNET_TIME_Relative remaining_time;
3132 struct GNUNET_CRYPTO_ShortHashCode zone;
3133 char new_name[MAX_DNS_NAME_LENGTH];
3135 struct GNUNET_TIME_Absolute et;
3137 rh->namestore_task = NULL;
3138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3139 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3142 GNUNET_CRYPTO_short_hash (key,
3143 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3145 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3152 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3154 rh->status |= RSL_RECORD_EXISTS;
3156 if (remaining_time.rel_value == 0)
3158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3159 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3161 rh->status |= RSL_RECORD_EXPIRED;
3166 * No authority found in namestore.
3171 * We did not find an authority in the namestore
3176 * Promote this authority back to a name maybe it is
3179 if (strcmp (rh->name, "") == 0)
3181 /* simply promote back */
3182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3183 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3184 rh->id, rh->authority_name);
3185 strcpy (rh->name, rh->authority_name);
3189 /* add back to existing name */
3190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3191 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3192 rh->id, rh->authority_name, rh->name);
3193 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3194 rh->name, rh->authority_name);
3195 strcpy (rh->name, new_name);
3196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3197 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3201 rh->proc (rh->proc_cls, rh, 0, NULL);
3206 * We found an authority that may be able to help us
3207 * move on with query
3208 * Note only 1 pkey should have been returned.. anything else would be strange
3210 for (i=0; i < rd_count;i++)
3214 * A CNAME. Like regular DNS this means the is no other record for this
3217 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3219 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3220 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3223 rh->status |= RSL_CNAME_FOUND;
3224 rh->proc (rh->proc_cls, rh, rd_count, rd);
3231 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3233 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3234 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3236 rh->status |= RSL_DELEGATE_VPN;
3237 rh->proc (rh->proc_cls, rh, rd_count, rd);
3243 * FIXME make optional
3245 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3247 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3248 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3250 rh->status |= RSL_DELEGATE_NS;
3251 rh->proc (rh->proc_cls, rh, rd_count, rd);
3255 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3258 rh->status |= RSL_DELEGATE_PKEY;
3260 if ((ignore_pending_records != 0) &&
3261 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3264 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3270 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3271 et.abs_value = rd[i].expiration_time;
3272 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3276 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3278 if (remaining_time.rel_value == 0)
3280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3281 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3283 rh->authority_chain_head->fresh = 0;
3284 rh->proc (rh->proc_cls, rh, 0, NULL);
3292 * Resolve rest of query with new authority
3294 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3295 memcpy (&rh->authority, rd[i].data,
3296 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3297 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3298 auth->zone = rh->authority;
3299 memset (auth->name, 0, strlen (rh->authority_name)+1);
3300 strcpy (auth->name, rh->authority_name);
3301 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3302 rh->authority_chain_tail,
3304 if (NULL != rh->rd.data)
3305 GNUNET_free ((void*)(rh->rd.data));
3307 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3308 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3309 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3311 /* Check for key revocation and delegate */
3312 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3315 GNUNET_GNS_RECORD_REV,
3316 &process_pkey_revocation_result_ns,
3324 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3325 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3327 * If we have found some records for the LAST label
3328 * we return the results. Else null.
3330 if (strcmp (rh->name, "") == 0)
3332 /* Start shortening */
3333 if ((rh->priv_key != NULL) &&
3334 (is_canonical (rh->name) == GNUNET_YES))
3336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3337 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3339 start_shorten (rh->authority_chain_head,
3342 /* simply promote back */
3343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3344 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3345 rh->id, rh->authority_name);
3346 strcpy (rh->name, rh->authority_name);
3347 rh->proc (rh->proc_cls, rh, rd_count, rd);
3351 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
3352 "%s.%s", rh->name, rh->authority_name);
3353 strcpy (rh->name, new_name);
3354 rh->proc (rh->proc_cls, rh, 0, NULL);
3360 * Resolve the delegation chain for the request in our namestore
3362 * @param rh the resolver handle
3365 resolve_delegation_ns (struct ResolverHandle *rh)
3367 pop_tld (rh->name, rh->authority_name);
3368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3369 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s by looking up %s\n",
3371 rh->authority_name);
3372 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3375 GNUNET_GNS_RECORD_ANY,
3376 &process_delegation_result_ns,
3382 * Lookup of a record in a specific zone
3383 * calls lookup result processor on result
3385 * @param zone the root zone
3386 * @param pzone the private local zone
3387 * @param record_type the record type to look up
3388 * @param name the name to look up
3389 * @param key a private key for use with PSEU import (can be NULL)
3390 * @param timeout timeout for resolution
3391 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3392 * @param proc the processor to call on result
3393 * @param cls the closure to pass to proc
3396 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3397 struct GNUNET_CRYPTO_ShortHashCode pzone,
3398 uint32_t record_type,
3400 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3401 struct GNUNET_TIME_Relative timeout,
3403 RecordLookupProcessor proc,
3406 struct ResolverHandle *rh;
3407 struct RecordLookupHandle* rlh;
3408 char string_hash[MAX_DNS_LABEL_LENGTH];
3409 char nzkey[MAX_DNS_LABEL_LENGTH];
3410 char* nzkey_ptr = nzkey;
3412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3413 "Starting resolution for %s (type=%d) with timeout %s!\n",
3415 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
3417 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3418 (strcmp(GNUNET_GNS_TLD, name) != 0))
3420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3421 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3426 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3427 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3428 rh->authority = zone;
3432 rh->timeout = timeout;
3433 rh->private_local_zone = pzone;
3434 rh->only_cached = only_cached;
3436 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3440 "No shorten key for resolution\n");
3442 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3445 * Set timeout for authority lookup phase to 1/2
3447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3448 "Timeout for lookup set to %s/2\n",
3449 GNUNET_STRINGS_relative_time_to_string (rh->timeout, GNUNET_YES));
3450 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
3451 GNUNET_TIME_relative_divide(timeout, 2),
3452 &handle_lookup_timeout,
3454 rh->timeout_cont = &dht_authority_lookup_timeout;
3455 rh->timeout_cont_cls = rh;
3459 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3460 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3463 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3466 * Only 'gnunet' given
3468 strcpy(rh->name, "\0");
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3473 "Checking for TLD...\n");
3474 if (is_zkey_tld(name) == GNUNET_YES)
3476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3479 * This is a zkey tld
3480 * build hash and use as initial authority
3483 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3484 memcpy(rh->name, name,
3485 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3486 pop_tld(rh->name, string_hash);
3488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3489 "ZKEY is %s!\n", string_hash);
3491 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3493 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3497 "Cannot convert ZKEY `%s' to hash!\n", string_hash);
3499 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3500 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3501 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3504 proc (cls, 0, NULL);
3509 else if (is_gads_tld (name) == GNUNET_YES)
3511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3514 * Presumably GADS tld
3516 memcpy (rh->name, name,
3517 strlen (name) - strlen(GNUNET_GNS_TLD) - 1);
3518 rh->name[strlen (name) - strlen(GNUNET_GNS_TLD) - 1] = '\0';
3522 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3523 "Cannot handle this TLD %s\n", string_hash);
3525 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3526 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3527 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3530 proc (cls, 0, NULL);
3536 * Initialize authority chain
3538 rh->authority_chain_head = GNUNET_malloc (sizeof(struct AuthorityChain));
3539 rh->authority_chain_tail = rh->authority_chain_head;
3540 rh->authority_chain_head->zone = rh->authority;
3541 strcpy (rh->authority_chain_head->name, "");
3544 * Copy original query into lookup handle
3546 rlh->record_type = record_type;
3547 memset(rlh->name, 0, strlen(name) + 1);
3548 strcpy(rlh->name, name);
3550 rlh->proc_cls = cls;
3552 rh->proc = &handle_delegation_ns;
3553 resolve_delegation_ns(rh);
3556 /******** END Record Resolver ***********/
3559 finish_shorten (struct ResolverHandle *rh,
3560 struct NameShortenHandle *nsh)
3562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3563 "Sending %s as shorten result\n", nsh->result);
3564 nsh->proc (nsh->proc_cls, nsh->result);
3565 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3567 free_resolver_handle (rh);
3572 * Callback calles by namestore for a zone to name
3575 * @param cls the closure
3576 * @param zone_key the zone we queried
3577 * @param expire the expiration time of the name
3578 * @param name the name found or NULL
3579 * @param rd_len number of records for the name
3580 * @param rd the record data (PKEY) for the name
3581 * @param signature the signature for the record data
3584 process_zone_to_name_shorten_root (void *cls,
3585 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3586 struct GNUNET_TIME_Absolute expire,
3588 unsigned int rd_len,
3589 const struct GNUNET_NAMESTORE_RecordData *rd,
3590 const struct GNUNET_CRYPTO_RsaSignature *signature);
3594 * Callback called by namestore for a zone to name
3597 * @param cls the closure
3598 * @param zone_key the zone we queried
3599 * @param expire the expiration time of the name
3600 * @param name the name found or NULL
3601 * @param rd_len number of records for the name
3602 * @param rd the record data (PKEY) for the name
3603 * @param signature the signature for the record data
3606 process_zone_to_name_shorten_shorten (void *cls,
3607 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3608 struct GNUNET_TIME_Absolute expire,
3610 unsigned int rd_len,
3611 const struct GNUNET_NAMESTORE_RecordData *rd,
3612 const struct GNUNET_CRYPTO_RsaSignature *signature)
3614 struct ResolverHandle *rh = cls;
3615 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3616 struct AuthorityChain *next_authority;
3618 char result[MAX_DNS_NAME_LENGTH];
3619 char tmp_name[MAX_DNS_NAME_LENGTH];
3622 rh->namestore_task = NULL;
3623 /* we found a match in our own root zone */
3626 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3627 memset(result, 0, answer_len);
3629 if (strlen(rh->name) > 0)
3631 sprintf (result, "%s.%s.%s.%s",
3633 nsh->shorten_zone_name,
3638 sprintf (result, "%s.%s.%s", name,
3639 nsh->shorten_zone_name,
3643 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3644 "Found shorten result %s\n", result);
3645 if (strlen (nsh->result) > strlen (result))
3646 strcpy (nsh->result, result);
3648 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3649 nsh->shorten_zone) == 0)
3652 * This is our zone append .gads unless name is empty
3653 * (it shouldn't be, usually FIXME what happens if we
3654 * shorten to our zone to a "" record??)
3657 sprintf (result, "%s.%s.%s",
3659 nsh->shorten_zone_name,
3661 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3662 "Our zone: Found %s as shorten result\n", result);
3664 if (strlen (nsh->result) > strlen (result))
3665 strcpy (nsh->result, result);
3666 //nsh->proc(nsh->proc_cls, result);
3668 //free_resolver_handle(rh);
3675 * continue with next authority if exists
3677 if (NULL == rh->authority_chain_head->next)
3679 finish_shorten (rh, nsh);
3682 next_authority = rh->authority_chain_head;
3684 if (0 == strcmp (rh->name, ""))
3685 strcpy (tmp_name, next_authority->name);
3687 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3688 "%s.%s", rh->name, next_authority->name);
3690 strcpy(rh->name, tmp_name);
3691 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3692 "No PSEU found for authority %s. Promoting back: %s\n",
3693 next_authority->name, rh->name);
3695 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3696 rh->authority_chain_tail,
3699 GNUNET_free (next_authority);
3701 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3702 &rh->authority_chain_tail->zone,
3703 &rh->authority_chain_head->zone,
3704 &process_zone_to_name_shorten_root,
3710 * Callback calles by namestore for a zone to name
3713 * @param cls the closure
3714 * @param zone_key the zone we queried
3715 * @param expire the expiration time of the name
3716 * @param name the name found or NULL
3717 * @param rd_len number of records for the name
3718 * @param rd the record data (PKEY) for the name
3719 * @param signature the signature for the record data
3722 process_zone_to_name_shorten_private (void *cls,
3723 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3724 struct GNUNET_TIME_Absolute expire,
3726 unsigned int rd_len,
3727 const struct GNUNET_NAMESTORE_RecordData *rd,
3728 const struct GNUNET_CRYPTO_RsaSignature *signature)
3730 struct ResolverHandle *rh = cls;
3731 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3732 struct AuthorityChain *next_authority;
3734 char result[MAX_DNS_NAME_LENGTH];
3735 char tmp_name[MAX_DNS_NAME_LENGTH];
3738 rh->namestore_task = NULL;
3739 /* we found a match in our own root zone */
3742 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3743 memset(result, 0, answer_len);
3745 if (strlen(rh->name) > 0)
3747 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3751 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3754 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3755 "Found shorten result %s\n", result);
3756 if (strlen (nsh->result) > strlen (result))
3757 strcpy (nsh->result, result);
3759 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3760 nsh->private_zone) == 0)
3763 * This is our zone append .gads unless name is empty
3764 * (it shouldn't be, usually FIXME what happens if we
3765 * shorten to our zone to a "" record??)
3768 sprintf (result, "%s.%s.%s",
3769 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3770 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3771 "Our private zone: Found %s as shorten result %s\n", result);
3772 if (strlen (nsh->result) > strlen (result))
3773 strcpy (nsh->result, result);
3776 if (0 != strcmp (nsh->shorten_zone_name, ""))
3778 /* backtrack authorities for names in priv zone */
3779 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3781 &rh->authority_chain_head->zone,
3782 &process_zone_to_name_shorten_shorten,
3789 * continue with next authority if exists
3791 if (NULL == rh->authority_chain_head->next)
3793 finish_shorten (rh, nsh);
3796 next_authority = rh->authority_chain_head;
3798 if (0 == strcmp (rh->name, ""))
3799 strcpy (tmp_name, next_authority->name);
3801 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3802 "%s.%s", rh->name, next_authority->name);
3804 strcpy(rh->name, tmp_name);
3805 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3806 "No PSEU found for authority %s. Promoting back: %s\n",
3807 next_authority->name, rh->name);
3809 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3810 rh->authority_chain_tail,
3813 GNUNET_free (next_authority);
3815 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3816 &rh->authority_chain_tail->zone,
3817 &rh->authority_chain_head->zone,
3818 &process_zone_to_name_shorten_root,
3825 * Callback calles by namestore for a zone to name
3828 * @param cls the closure
3829 * @param zone_key the zone we queried
3830 * @param expire the expiration time of the name
3831 * @param name the name found or NULL
3832 * @param rd_len number of records for the name
3833 * @param rd the record data (PKEY) for the name
3834 * @param signature the signature for the record data
3837 process_zone_to_name_shorten_root (void *cls,
3838 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3839 struct GNUNET_TIME_Absolute expire,
3841 unsigned int rd_len,
3842 const struct GNUNET_NAMESTORE_RecordData *rd,
3843 const struct GNUNET_CRYPTO_RsaSignature *signature)
3845 struct ResolverHandle *rh = cls;
3846 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3847 struct AuthorityChain *next_authority;
3848 char result[MAX_DNS_NAME_LENGTH];
3849 char tmp_name[MAX_DNS_NAME_LENGTH];
3852 rh->namestore_task = NULL;
3853 /* we found a match in our own root zone */
3856 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3857 memset(result, 0, answer_len);
3859 if (strlen(rh->name) > 0)
3861 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3865 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3868 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3869 "Found shorten result %s\n", result);
3870 if (strlen (nsh->result) > strlen (result))
3871 strcpy (nsh->result, result);
3873 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3874 nsh->root_zone) == 0)
3877 * This is our zone append .gads unless name is empty
3878 * (it shouldn't be, usually FIXME what happens if we
3879 * shorten to our zone to a "" record??)
3882 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3883 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3884 "Our zone: Found %s as shorten result\n", result);
3885 if (strlen (nsh->result) > strlen (result))
3886 strcpy (nsh->result, result);
3889 if (NULL != nsh->private_zone)
3891 /* backtrack authorities for names in priv zone */
3892 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3894 &rh->authority_chain_head->zone,
3895 &process_zone_to_name_shorten_private,
3898 else if (NULL != nsh->shorten_zone)
3900 /* backtrack authorities for names in shorten zone */
3901 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3903 &rh->authority_chain_head->zone,
3904 &process_zone_to_name_shorten_shorten,
3911 * continue with next authority if exists
3913 if (NULL == rh->authority_chain_head->next)
3915 finish_shorten (rh, nsh);
3918 next_authority = rh->authority_chain_head;
3920 if (0 == strcmp (rh->name, ""))
3921 strcpy (tmp_name, next_authority->name);
3923 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3924 "%s.%s", rh->name, next_authority->name);
3926 strcpy(rh->name, tmp_name);
3927 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3928 "No PSEU found for authority %s. Promoting back: %s\n",
3929 next_authority->name, rh->name);
3931 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3932 rh->authority_chain_tail,
3935 GNUNET_free (next_authority);
3937 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3938 &rh->authority_chain_tail->zone,
3939 &rh->authority_chain_head->zone,
3940 &process_zone_to_name_shorten_root,
3947 * Process result from namestore delegation lookup
3948 * for shorten operation
3950 * @param cls the client shorten handle
3951 * @param rh the resolver handle
3952 * @param rd_count number of results (0)
3953 * @param rd data (NULL)
3956 handle_delegation_ns_shorten (void* cls,
3957 struct ResolverHandle *rh,
3959 const struct GNUNET_NAMESTORE_RecordData *rd)
3961 struct NameShortenHandle *nsh;
3962 char result[MAX_DNS_NAME_LENGTH];
3964 nsh = (struct NameShortenHandle *)cls;
3965 rh->namestore_task = NULL;
3967 * At this point rh->name contains the part of the name
3968 * that we do not have a PKEY in our namestore to resolve.
3969 * The authority chain in the resolver handle is now
3970 * useful to backtrack if needed
3973 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3974 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3975 memset(result, 0, sizeof (result));
3977 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3981 * This is our zone append .gads unless name is empty
3982 * (it shouldn't be, usually FIXME what happens if we
3983 * shorten to our zone to a "" record??)
3986 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3987 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3988 "Our zone: Found %s as shorten result\n", result);
3990 if (strlen (nsh->result) > strlen (result))
3991 strcpy (nsh->result, result);
3994 else if (NULL != nsh->private_zone)
3997 * This is our zone append .gads unless name is empty
3998 * (it shouldn't be, usually FIXME what happens if we
3999 * shorten to our zone to a "" record??)
4001 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4002 nsh->private_zone) == 0)
4005 sprintf (result, "%s.%s.%s",
4006 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4007 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4008 "Our zone: Found %s as shorten result in private zone %s\n",
4011 if (strlen (nsh->result) > strlen (result))
4012 strcpy (nsh->result, result);
4015 else if (NULL != nsh->shorten_zone)
4018 * This is our zone append .gads unless name is empty
4019 * (it shouldn't be, usually FIXME what happens if we
4020 * shorten to our zone to a "" record??)
4022 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4023 nsh->shorten_zone) == 0)
4025 sprintf (result, "%s.%s.%s",
4026 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4027 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4028 "Our zone: Found %s as shorten result in shorten zone\n",
4031 if (strlen (nsh->result) > strlen (result))
4032 strcpy (nsh->result, result);
4037 /* backtrack authorities for names */
4038 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4040 &rh->authority_chain_head->zone,
4041 &process_zone_to_name_shorten_root,
4048 * Callback calles by namestore for a zone to name
4051 * @param cls the closure
4052 * @param zone_key the zone we queried
4053 * @param expire the expiration time of the name
4054 * @param name the name found or NULL
4055 * @param rd_len number of records for the name
4056 * @param rd the record data (PKEY) for the name
4057 * @param signature the signature for the record data
4060 process_zone_to_name_zkey(void *cls,
4061 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4062 struct GNUNET_TIME_Absolute expire,
4064 unsigned int rd_len,
4065 const struct GNUNET_NAMESTORE_RecordData *rd,
4066 const struct GNUNET_CRYPTO_RsaSignature *signature)
4068 struct ResolverHandle *rh = cls;
4069 struct NameShortenHandle *nsh = rh->proc_cls;
4070 char new_name[MAX_DNS_NAME_LENGTH];
4072 rh->namestore_task = NULL;
4074 /* zkey not in our zone */
4078 * In this case we have not given this PKEY a name (yet)
4079 * It is either just not in our zone or not even cached
4080 * Since we do not know at this point we will not try to shorten
4081 * because PKEY import will happen if the user follows the zkey
4084 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4085 "No name found for zkey %s returning verbatim!\n", nsh->result);
4086 /*if (strcmp(rh->name, "") != 0)
4087 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4088 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4090 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4091 enc, GNUNET_GNS_TLD_ZKEY);
4093 strcpy (nsh->result, new_name);*/
4095 finish_shorten (rh, nsh);
4099 if (strcmp(rh->name, "") != 0)
4100 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4103 strcpy(new_name, name);
4105 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4106 "Continue shorten for %s!\n", new_name);
4108 strcpy(rh->name, new_name);
4110 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4111 rh->authority_chain_tail = rh->authority_chain_head;
4112 rh->authority_chain_head->zone = rh->authority;
4115 /* Start delegation resolution in our namestore */
4116 resolve_delegation_ns (rh);
4121 * Shorten api from resolver
4123 * @param zone the root zone to use
4124 * @param pzone the private zone to use
4125 * @param szone the shorten zone to use
4126 * @param name the name to shorten
4127 * @param private_zone_name name of the private zone
4128 * @param shorten_zone_name name of the shorten zone
4129 * @param proc the processor to call with result
4130 * @param proc_cls closure to pass to proc
4133 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4134 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4135 struct GNUNET_CRYPTO_ShortHashCode *szone,
4137 const char* private_zone_name,
4138 const char* shorten_zone_name,
4139 ShortenResultProcessor proc,
4142 struct ResolverHandle *rh;
4143 struct NameShortenHandle *nsh;
4144 char string_hash[MAX_DNS_LABEL_LENGTH];
4145 struct GNUNET_CRYPTO_ShortHashCode zkey;
4146 char nzkey[MAX_DNS_LABEL_LENGTH];
4147 char* nzkey_ptr = nzkey;
4149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4150 "Starting shorten for %s!\n", name);
4152 if (is_canonical ((char*)name) == GNUNET_YES)
4154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4155 "%s is canonical. Returning verbatim\n", name);
4156 proc (proc_cls, name);
4160 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4162 nsh->proc_cls = proc_cls;
4163 nsh->root_zone = zone;
4164 nsh->private_zone = pzone;
4165 nsh->shorten_zone = szone;
4166 strcpy (nsh->private_zone_name, private_zone_name);
4167 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4168 strcpy (nsh->result, name);
4170 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4171 rh->authority = *zone;
4173 rh->priv_key = NULL;
4174 rh->namestore_task = NULL;
4175 rh->proc = &handle_delegation_ns_shorten;
4178 rh->private_local_zone = *zone;
4180 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4182 "Checking for TLD...\n");
4183 if (is_zkey_tld (name) == GNUNET_YES)
4185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4188 * This is a zkey tld
4189 * build hash and use as initial authority
4192 memset (rh->name, 0,
4193 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4194 memcpy (rh->name, name,
4195 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4196 pop_tld (rh->name, string_hash);
4198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4199 "ZKEY is %s!\n", string_hash);
4201 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4203 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4207 "Cannot convert ZKEY %s to hash!\n", nzkey);
4208 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4211 proc (proc_cls, name);
4214 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4217 &process_zone_to_name_zkey,
4222 else if (is_gads_tld (name) == GNUNET_YES)
4224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4227 * Presumably GNUNET tld
4229 memset (rh->name, 0,
4230 strlen (name)-strlen (GNUNET_GNS_TLD));
4231 memcpy (rh->name, name,
4232 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4237 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4240 proc (proc_cls, name);
4244 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4245 rh->authority_chain_tail = rh->authority_chain_head;
4246 rh->authority_chain_head->zone = *zone;
4249 /* Start delegation resolution in our namestore */
4250 resolve_delegation_ns (rh);
4253 /*********** END NAME SHORTEN ********************/
4256 * Conclude get authority lookup
4258 * @param rh resolver handle
4259 * @param nah get authority lookup handle
4262 finish_get_auth (struct ResolverHandle *rh,
4263 struct GetNameAuthorityHandle *nah)
4265 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4266 "Got authority result %s\n", nah->result);
4268 nah->proc (nah->proc_cls, nah->result);
4269 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4271 free_resolver_handle (rh);
4276 * Process result from namestore delegation lookup
4277 * for get authority operation
4279 * @param cls the client get auth handle
4280 * @param rh the resolver handle
4281 * @param rd_count number of results (0)
4282 * @param rd data (NULL)
4285 handle_delegation_result_ns_get_auth(void* cls,
4286 struct ResolverHandle *rh,
4288 const struct GNUNET_NAMESTORE_RecordData *rd)
4290 struct GetNameAuthorityHandle* nah = rh->proc_cls;
4294 * At this point rh->name contains the part of the name
4295 * that we do not have a PKEY in our namestore to resolve.
4296 * The authority chain in the resolver handle is now
4297 * useful to backtrack if needed
4300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4301 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4303 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4304 "Building response!\n");
4305 if (is_canonical (rh->name) == GNUNET_YES)
4308 * We successfully resolved the authority in the ns
4309 * FIXME for our purposes this is fine
4310 * but maybe we want to have an api that also looks
4311 * into the dht (i.e. option in message)
4313 if (strlen(rh->name) > strlen(nah->name))
4315 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4316 "Record name longer than original lookup name... odd!\n");
4320 answer_len = strlen(nah->name) - strlen(rh->name)
4321 + strlen(GNUNET_GNS_TLD) + 1;
4322 memset(nah->result, 0, answer_len);
4323 if (0 != strcmp (rh->name, ""))
4324 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4326 strcpy(nah->result, nah->name);
4328 finish_get_auth (rh, nah);
4332 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4333 "Unable to resolve authority for remaining %s!\n", rh->name);
4334 strcpy(nah->result, "");
4335 finish_get_auth (rh, nah);
4341 * Tries to resolve the authority for name
4344 * @param zone the root zone to look up for
4345 * @param pzone the private local zone
4346 * @param name the name to lookup up
4347 * @param proc the processor to call when finished
4348 * @param proc_cls the closure to pass to the processor
4351 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4352 struct GNUNET_CRYPTO_ShortHashCode pzone,
4354 GetAuthorityResultProcessor proc,
4357 struct ResolverHandle *rh;
4358 struct GetNameAuthorityHandle *nah;
4360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4361 "Starting authority resolution for %s!\n", name);
4363 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4364 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4365 rh->authority = zone;
4367 rh->private_local_zone = pzone;
4369 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4371 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4373 strcpy(rh->name, "\0");
4378 strlen(name)-strlen(GNUNET_GNS_TLD));
4379 memcpy(rh->name, name,
4380 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4383 memset(nah->name, 0,
4385 strcpy(nah->name, name);
4387 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4388 rh->authority_chain_tail = rh->authority_chain_head;
4389 rh->authority_chain_head->zone = zone;
4390 rh->proc = &handle_delegation_result_ns_get_auth;
4391 rh->proc_cls = (void*)nah;
4394 nah->proc_cls = proc_cls;
4395 strcpy (nah->result, "");
4397 /* Start delegation resolution in our namestore */
4398 resolve_delegation_ns(rh);
4402 /******** END GET AUTHORITY *************/
4404 /* end of gnunet-service-gns_resolver.c */