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, "."))
190 * Determine if this name is canonical (is a legal name in a zone, without delegation);
191 * note that we do not test that the name does not contain illegal characters, we only
192 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
193 * even though they consist of multiple labels.
196 * a.b.gads = not canonical
198 * _foo._srv = canonical
199 * _f.bar = not canonical
201 * @param name the name to test
202 * @return GNUNET_YES if canonical
205 is_canonical (const char* name)
210 if (NULL == strchr (name, '.'))
215 while (NULL != (dot = strchr (pos, '.')))
225 free_get_pseu_authority_handle (struct GetPseuAuthorityHandle *gph)
227 gph->namestore_task = NULL;
228 GNUNET_free (gph->auth);
229 GNUNET_CRYPTO_rsa_key_free (gph->key);
230 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
236 * Callback that shortens authorities
238 * @param gph the handle containing the name to shorten
241 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
245 * Continueation for pkey record creation (shorten)
247 * @param cls a GetPseuAuthorityHandle
248 * @param success unused
252 create_pkey_cont (void* cls, int32_t success, const char* emsg)
254 //FIXME do sth with error
255 struct GetPseuAuthorityHandle* gph = cls;
257 free_get_pseu_authority_handle (gph);
262 * Namestore calls this function if we have record for this name.
263 * (or with rd_count=0 to indicate no matches)
265 * @param cls the pending query
266 * @param key the key of the zone we did the lookup
267 * @param expiration expiration date of the namestore entry
268 * @param name the name for which we need an authority
269 * @param rd_count the number of records with 'name'
270 * @param rd the record data
271 * @param signature the signature of the authority for the record data
274 process_pseu_lookup_ns (void* cls,
275 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
276 struct GNUNET_TIME_Absolute expiration,
277 const char *name, unsigned int rd_count,
278 const struct GNUNET_NAMESTORE_RecordData *rd,
279 const struct GNUNET_CRYPTO_RsaSignature *signature)
281 struct GetPseuAuthorityHandle* gph = cls;
282 struct GNUNET_NAMESTORE_RecordData new_pkey;
284 gph->namestore_task = NULL;
287 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
288 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
289 free_get_pseu_authority_handle (gph);
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
297 new_pkey.expiration_time = UINT64_MAX;
298 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
299 new_pkey.data = &gph->auth->zone;
300 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
301 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
302 | GNUNET_NAMESTORE_RF_PRIVATE
303 | GNUNET_NAMESTORE_RF_PENDING;
304 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
313 * process result of a dht pseu lookup
315 * @param gph the handle
316 * @param name the pseu result or NULL
319 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 "GNS_AUTO_PSEU: No PSEU, no shorten. Finished.\n");
325 free_get_pseu_authority_handle (gph);
329 memcpy (gph->test_name, name, strlen(name) + 1);
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
334 * Check for collision
336 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
339 GNUNET_NAMESTORE_TYPE_ANY,
340 &process_pseu_lookup_ns,
346 * Handle timeout for dht request
348 * @param cls the request handle as closure
349 * @param tc the task context
352 handle_auth_discovery_timeout (void *cls,
353 const struct GNUNET_SCHEDULER_TaskContext *tc)
355 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
359 GNUNET_DHT_get_stop (gph->get_handle);
360 gph->get_handle = NULL;
361 process_pseu_result (gph, NULL);
366 * Function called when we find a PSEU entry in the DHT
368 * @param cls the request handle
369 * @param exp lifetime
370 * @param key the key the record was stored under
371 * @param get_path get path
372 * @param get_path_length get path length
373 * @param put_path put path
374 * @param put_path_length put path length
375 * @param type the block type
376 * @param size the size of the record
377 * @param data the record data
380 process_auth_discovery_dht_result (void* cls,
381 struct GNUNET_TIME_Absolute exp,
382 const struct GNUNET_HashCode * key,
383 const struct GNUNET_PeerIdentity *get_path,
384 unsigned int get_path_length,
385 const struct GNUNET_PeerIdentity *put_path,
386 unsigned int put_path_length,
387 enum GNUNET_BLOCK_Type type,
391 struct GetPseuAuthorityHandle* gph = cls;
392 struct GNSNameRecordBlock *nrb;
393 const char* rd_data = data;
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
402 /* stop lookup and timeout task */
403 GNUNET_DHT_get_stop (gph->get_handle);
404 gph->get_handle = NULL;
405 GNUNET_SCHEDULER_cancel (gph->timeout);
409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
410 "GNS_GET_AUTH: got dht result null!\n", size);
411 free_get_pseu_authority_handle (gph);
415 nrb = (struct GNSNameRecordBlock*)data;
416 name = (char*)&nrb[1];
417 num_records = ntohl (nrb->rd_count);
419 struct GNUNET_NAMESTORE_RecordData rd[num_records];
421 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
422 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
424 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
430 "GNS_GET_AUTH: Error deserializing data!\n");
434 for (i=0; i < num_records; i++)
436 if ((strcmp (name, "+") == 0) &&
437 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
440 process_pseu_result (gph, (char*)rd[i].data);
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "GNS_GET_AUTH: finished shorten, no results!\n");
448 process_pseu_result (gph, NULL);
453 * Process PSEU discovery for shorten via namestore
455 * @param cls the GetPseuAuthorityHandle
456 * @param key the public key
457 * @param expiration recorddata expiration
458 * @param name the looked up name
459 * @param rd_count number of records in set
460 * @param rd record data
461 * @param signature the signature
464 process_auth_discovery_ns_result (void* cls,
465 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
466 struct GNUNET_TIME_Absolute expiration,
468 unsigned int rd_count,
469 const struct GNUNET_NAMESTORE_RecordData *rd,
470 const struct GNUNET_CRYPTO_RsaSignature *signature)
472 struct GetPseuAuthorityHandle* gph = cls;
473 struct GNUNET_HashCode lookup_key;
477 gph->namestore_task = NULL;
481 GNUNET_GNS_get_key_for_record (GNUNET_GNS_TLD_PLUS, &gph->auth->zone, &lookup_key);
482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
483 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
485 GNUNET_h2s (&lookup_key));
487 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
488 &handle_auth_discovery_timeout, gph);
490 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
492 GNUNET_assert (gph->get_handle == NULL);
494 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
495 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
497 DHT_GNS_REPLICATION_LEVEL,
498 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
501 &process_auth_discovery_dht_result,
506 for (i=0; i < rd_count; i++)
508 if (0 != (strcmp (name, GNUNET_GNS_TLD_PLUS)))
511 if (rd[i].record_type != GNUNET_GNS_RECORD_PSEU)
515 process_pseu_result (gph, (char*)rd[i].data);
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
520 process_pseu_result (gph, NULL);
525 * Callback called by namestore for a zone to name
528 * @param cls the closure
529 * @param zone_key the zone we queried
530 * @param expire the expiration time of the name
531 * @param name the name found or NULL
532 * @param rd_len number of records for the name
533 * @param rd the record data (PKEY) for the name
534 * @param signature the signature for the record data
537 process_zone_to_name_discover (void *cls,
538 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
539 struct GNUNET_TIME_Absolute expire,
542 const struct GNUNET_NAMESTORE_RecordData *rd,
543 const struct GNUNET_CRYPTO_RsaSignature *signature)
545 struct GetPseuAuthorityHandle* gph = cls;
547 gph->namestore_task = NULL;
550 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
553 GNUNET_GNS_RECORD_PSEU,
554 &process_auth_discovery_ns_result,
558 /* we found a match in our own zone */
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
561 free_get_pseu_authority_handle (gph);
566 * Callback that shortens authorities
568 * @param gph the handle to the shorten request
571 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "GNS_AUTO_PSEU: New authority %s discovered\n",
578 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
581 &process_zone_to_name_discover,
588 * Start shortening algorithm using auth as
591 * @param auth the authorities that were resolved
592 * @param key the private key for PKEY import
595 start_shorten (struct AuthorityChain *auth,
596 const struct GNUNET_CRYPTO_RsaPrivateKey *key)
598 struct GetPseuAuthorityHandle *gph;
599 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
600 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
602 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
603 if (NULL == (pb_key = GNUNET_CRYPTO_rsa_encode_key (key)))
605 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
606 "Failed to encode RSA key on shorten\n");
609 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
610 gph->key = GNUNET_CRYPTO_rsa_decode_key ((const char*) pb_key, ntohs (pb_key->len));
611 GNUNET_free (pb_key);
612 if (NULL == gph->key)
614 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
615 "Failed to decode RSA key on shorten\n");
619 GNUNET_CRYPTO_short_hash (&pkey,
620 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
622 gph->auth = GNUNET_malloc (sizeof (struct AuthorityChain));
623 memcpy (gph->auth, auth, sizeof (struct AuthorityChain));
624 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
625 shorten_authority_chain (gph);
629 * Initialize the resolver
631 * @param nh the namestore handle
632 * @param dh the dht handle
633 * @param lz the local zone's hash
634 * @param c configuration handle
635 * @param max_bg_queries maximum number of parallel background queries in dht
636 * @param ignore_pending ignore records that still require user confirmation
638 * @return GNUNET_OK on success
641 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
642 struct GNUNET_DHT_Handle *dh,
643 struct GNUNET_CRYPTO_ShortHashCode lz,
644 const struct GNUNET_CONFIGURATION_Handle *c,
645 unsigned long long max_bg_queries,
650 return GNUNET_SYSERR;
653 namestore_handle = nh;
657 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
659 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
660 max_allowed_background_queries = max_bg_queries;
661 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
662 ignore_pending_records = ignore_pending;
663 GNUNET_RESOLVER_connect (cfg);
671 * @param cls closure to iterator
672 * @param node heap nodes
673 * @param element the namestorebgtask
674 * @param cost heap cost
675 * @return always GNUNET_YES
678 cleanup_pending_ns_tasks (void* cls,
679 struct GNUNET_CONTAINER_HeapNode *node,
681 GNUNET_CONTAINER_HeapCostType cost)
683 struct NamestoreBGTask *nbg = element;
685 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
686 "GNS_CLEANUP: Terminating ns task\n");
687 GNUNET_NAMESTORE_cancel (nbg->qe);
689 GNUNET_CONTAINER_heap_remove_node (node);
697 * @param rh resolver handle
698 * @param rlh record lookup handle
699 * @param rd_count number of results
703 finish_lookup (struct ResolverHandle *rh,
704 struct RecordLookupHandle* rlh,
705 unsigned int rd_count,
706 const struct GNUNET_NAMESTORE_RecordData *rd);
710 * Cleanup background lookups FIXME get rid of this?? YES this doesn't do
711 * anything! => find in code and remove all references to the heap
713 * @param cls closure to iterator
714 * @param node heap nodes
715 * @param element the resolver handle
716 * @param cost heap cost
717 * @return always GNUNET_YES
720 cleanup_pending_background_queries (void* cls,
721 struct GNUNET_CONTAINER_HeapNode *node,
723 GNUNET_CONTAINER_HeapCostType cost)
725 struct ResolverHandle *rh = element;
727 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
728 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
730 GNUNET_CONTAINER_heap_remove_node (node);
731 if (0 == GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
733 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
734 &cleanup_pending_ns_tasks,
742 * Helper function to free resolver handle
744 * @param rh the handle to free
747 free_resolver_handle (struct ResolverHandle* rh)
749 struct AuthorityChain *ac;
750 struct AuthorityChain *ac_next;
755 ac = rh->authority_chain_head;
764 if (NULL != rh->get_handle)
765 GNUNET_DHT_get_stop (rh->get_handle);
767 if (NULL != rh->dns_raw_packet)
768 GNUNET_free (rh->dns_raw_packet);
770 if (NULL != rh->namestore_task)
771 GNUNET_NAMESTORE_cancel (rh->namestore_task);
772 rh->namestore_task = NULL;
774 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
775 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
777 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
778 GNUNET_SCHEDULER_cancel (rh->timeout_task);
780 if (NULL != rh->dns_sock)
781 GNUNET_NETWORK_socket_close (rh->dns_sock);
782 if (NULL != rh->dns_resolver_handle)
783 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
785 if (NULL != rh->rd.data)
786 GNUNET_free ((void*)(rh->rd.data));
794 * @param rh resolver handle
795 * @param nsh name shorten handle
798 finish_shorten (struct ResolverHandle *rh,
799 struct NameShortenHandle *nsh);
805 * @param rh resolver handle
806 * @param nah get name authority handle
809 finish_get_auth (struct ResolverHandle *rh,
810 struct GetNameAuthorityHandle* rlh);
817 gns_resolver_cleanup ()
820 struct GetPseuAuthorityHandle *tmp;
822 while (NULL != (tmp = gph_head))
824 if (tmp->get_handle != NULL)
826 GNUNET_DHT_get_stop (tmp->get_handle);
827 tmp->get_handle = NULL;
829 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
831 GNUNET_SCHEDULER_cancel (tmp->timeout);
832 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
834 if (NULL != tmp->namestore_task)
836 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
837 tmp->namestore_task = NULL;
839 free_get_pseu_authority_handle (tmp);
842 while (NULL != rlh_head)
844 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
846 while (NULL != nsh_head)
848 finish_shorten (nsh_head, nsh_head->proc_cls);
850 while (NULL != nah_head)
852 finish_get_auth (nah_head, nah_head->proc_cls);
855 s = GNUNET_CONTAINER_heap_get_size (dht_lookup_heap);
856 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
857 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
859 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
860 &cleanup_pending_background_queries,
862 else if (0 != GNUNET_CONTAINER_heap_get_size (ns_task_heap))
864 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
865 &cleanup_pending_ns_tasks,
872 * Callback when record data is put into namestore
874 * @param cls the closure
875 * @param success GNUNET_OK on success
876 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
879 on_namestore_record_put_result (void *cls,
883 struct NamestoreBGTask *nbg = cls;
885 GNUNET_CONTAINER_heap_remove_node (nbg->node);
888 if (GNUNET_NO == success)
890 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
891 "GNS_NS: records already in namestore\n");
894 else if (GNUNET_YES == success)
896 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
897 "GNS_NS: records successfully put in namestore\n");
901 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
902 "GNS_NS: Error putting records into namestore: %s\n", emsg);
907 * Lookup timeout task
909 * @param cls the ResolverHandle for the task that timed out
910 * @param tc the task context
913 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
915 struct ResolverHandle *rh = cls;
917 if (NULL != rh->timeout_cont)
918 rh->timeout_cont (rh->timeout_cont_cls, tc);
923 * Processor for background lookups in the DHT
925 * @param cls closure (NULL)
926 * @param rd_count number of records found (not 0)
927 * @param rd record data
930 background_lookup_result_processor (void *cls,
932 const struct GNUNET_NAMESTORE_RecordData *rd)
934 //We could do sth verbose/more useful here but it doesn't make any difference
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
936 "GNS_BG: background dht lookup finished. (%d results)\n",
942 * Handle timeout for DHT requests
944 * @param cls the request handle as closure
945 * @param tc the task context
948 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
950 struct ResolverHandle *rh = cls;
951 struct RecordLookupHandle *rlh = rh->proc_cls;
952 char new_name[MAX_DNS_NAME_LENGTH];
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
955 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
956 rh->id, rh->name, rh->timeout.rel_value);
958 * Start resolution in bg
960 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
961 rh->name, GNUNET_GNS_TLD);
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
965 rh->id, new_name, rlh->record_type);
967 gns_resolver_lookup_record (rh->authority,
968 rh->private_local_zone,
972 GNUNET_TIME_UNIT_FOREVER_REL,
974 &background_lookup_result_processor,
977 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
979 GNUNET_DHT_get_stop (rh->get_handle);
980 rh->get_handle = NULL;
981 rh->proc (rh->proc_cls, rh, 0, NULL);
986 * Function called when we get a result from the dht
987 * for our record query
989 * @param cls the request handle
990 * @param exp lifetime
991 * @param key the key the record was stored under
992 * @param get_path get path
993 * @param get_path_length get path length
994 * @param put_path put path
995 * @param put_path_length put path length
996 * @param type the block type
997 * @param size the size of the record
998 * @param data the record data
1001 process_record_result_dht (void* cls,
1002 struct GNUNET_TIME_Absolute exp,
1003 const struct GNUNET_HashCode * key,
1004 const struct GNUNET_PeerIdentity *get_path,
1005 unsigned int get_path_length,
1006 const struct GNUNET_PeerIdentity *put_path,
1007 unsigned int put_path_length,
1008 enum GNUNET_BLOCK_Type type,
1009 size_t size, const void *data)
1011 struct ResolverHandle *rh;
1012 struct RecordLookupHandle *rlh;
1013 struct GNSNameRecordBlock *nrb;
1014 uint32_t num_records;
1016 char* rd_data = (char*)data;
1020 rh = (struct ResolverHandle *)cls;
1021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1022 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
1024 rlh = (struct RecordLookupHandle *) rh->proc_cls;
1025 nrb = (struct GNSNameRecordBlock*)data;
1027 /* stop lookup and timeout task */
1028 GNUNET_DHT_get_stop (rh->get_handle);
1029 rh->get_handle = NULL;
1031 if (rh->dht_heap_node != NULL)
1033 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1034 rh->dht_heap_node = NULL;
1037 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1039 GNUNET_SCHEDULER_cancel (rh->timeout_task);
1040 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1043 rh->get_handle = NULL;
1044 name = (char*)&nrb[1];
1045 num_records = ntohl (nrb->rd_count);
1047 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1048 struct NamestoreBGTask *ns_heap_root;
1049 struct NamestoreBGTask *namestore_bg_task;
1051 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
1052 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
1054 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1059 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1060 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1061 rh->proc (rh->proc_cls, rh, 0, NULL);
1065 for (i=0; i<num_records; i++)
1067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1068 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1069 rh->id, name, rh->name);
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1072 rh->id, rd[i].record_type, rlh->record_type);
1073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1074 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1075 rh->id, rd[i].data_size);
1076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1077 "GNS_PHASE_REC-%llu: Got flag %d\n",
1078 rh->id, rd[i].flags);
1080 if ((strcmp (name, rh->name) == 0) &&
1081 (rd[i].record_type == rlh->record_type))
1089 * FIXME check pubkey against existing key in namestore?
1090 * https://gnunet.org/bugs/view.php?id=2179
1092 if (max_allowed_ns_tasks <=
1093 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1095 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1096 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1099 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1103 /* Save to namestore */
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1105 "GNS_PHASE_REC-%llu: Caching record for %s\n",
1107 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1108 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1115 &on_namestore_record_put_result, //cont
1118 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1120 GNUNET_TIME_absolute_get().abs_value);
1123 if (0 < rh->answered)
1124 rh->proc (rh->proc_cls, rh, num_records, rd);
1126 rh->proc (rh->proc_cls, rh, 0, NULL);
1133 * Start DHT lookup for a (name -> query->record_type) record in
1134 * rh->authority's zone
1136 * @param rh the pending gns query context
1139 resolve_record_dht (struct ResolverHandle *rh)
1142 struct GNUNET_HashCode lookup_key;
1143 struct RecordLookupHandle *rlh = rh->proc_cls;
1144 struct ResolverHandle *rh_heap_root;
1146 GNUNET_GNS_get_key_for_record (rh->name, &rh->authority, &lookup_key);
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1148 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1149 rh->id, rh->name, GNUNET_h2s (&lookup_key));
1151 rh->dht_heap_node = NULL;
1153 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
1156 * Update timeout if necessary
1158 if (GNUNET_SCHEDULER_NO_TASK == rh->timeout_task)
1160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1161 "GNS_PHASE_REC-%llu: Adjusting timeout\n", rh->id);
1163 * Set timeout for authority lookup phase to 1/2
1165 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1166 GNUNET_TIME_relative_divide (rh->timeout, 2),
1167 &handle_lookup_timeout,
1170 rh->timeout_cont = &dht_lookup_timeout;
1171 rh->timeout_cont_cls = rh;
1175 if (max_allowed_background_queries <=
1176 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1178 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1179 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1180 rh_heap_root->get_handle = NULL;
1181 rh_heap_root->dht_heap_node = NULL;
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1185 rh->id, rh_heap_root->name);
1186 rh_heap_root->proc (rh_heap_root->proc_cls,
1191 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1193 GNUNET_TIME_absolute_get ().abs_value);
1196 xquery = htonl (rlh->record_type);
1198 GNUNET_assert (rh->get_handle == NULL);
1199 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1200 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1202 DHT_GNS_REPLICATION_LEVEL,
1203 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1206 &process_record_result_dht,
1213 * Namestore calls this function if we have record for this name.
1214 * (or with rd_count=0 to indicate no matches)
1216 * @param cls the pending query
1217 * @param key the key of the zone we did the lookup
1218 * @param expiration expiration date of the namestore entry
1219 * @param name the name for which we need an authority
1220 * @param rd_count the number of records with 'name'
1221 * @param rd the record data
1222 * @param signature the signature of the authority for the record data
1225 process_record_result_ns (void* cls,
1226 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1227 struct GNUNET_TIME_Absolute expiration,
1228 const char *name, unsigned int rd_count,
1229 const struct GNUNET_NAMESTORE_RecordData *rd,
1230 const struct GNUNET_CRYPTO_RsaSignature *signature)
1232 struct ResolverHandle *rh;
1233 struct RecordLookupHandle *rlh;
1234 struct GNUNET_TIME_Relative remaining_time;
1235 struct GNUNET_CRYPTO_ShortHashCode zone;
1236 struct GNUNET_TIME_Absolute et;
1242 rh->namestore_task = NULL;
1243 GNUNET_CRYPTO_short_hash (key,
1244 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1246 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1254 rh->status |= RSL_RECORD_EXISTS;
1256 if (remaining_time.rel_value == 0)
1258 rh->status |= RSL_RECORD_EXPIRED;
1265 * Lookup terminated and no results
1267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1268 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1271 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1272 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1275 * Our zone and no result? Cannot resolve TT
1277 rh->proc(rh->proc_cls, rh, 0, NULL);
1281 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1282 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1284 for (i=0; i<rd_count;i++)
1286 if (rd[i].record_type != rlh->record_type)
1289 if (ignore_pending_records &&
1290 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1292 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1293 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1298 //FIXME: eh? do I have to handle this here?
1299 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1300 et.abs_value = rd[i].expiration_time;
1301 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
1303 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1304 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1314 if (0 == rh->answered)
1316 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1317 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1318 rh->proc(rh->proc_cls, rh, 0, NULL);
1322 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1323 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1324 rh->id, rh->answered, rd_count);
1325 rh->proc(rh->proc_cls, rh, rd_count, rd);
1330 * VPN redirect result callback
1332 * @param cls the resolver handle
1333 * @param af the requested address family
1334 * @param address in_addr(6) respectively
1337 process_record_result_vpn (void* cls, int af, const void *address)
1339 struct ResolverHandle *rh = cls;
1340 struct RecordLookupHandle *rlh;
1341 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);
1494 * Resolve DNS name via local stub resolver
1496 * @param rh the resolver handle
1499 resolve_dns_name (struct ResolverHandle *rh)
1501 struct RecordLookupHandle *rlh = rh->proc_cls;
1504 if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1505 (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "Can only resolve A/AAAA via stub... abort\n");
1509 finish_lookup (rh, rlh, 0, NULL);
1513 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1518 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1521 &handle_dns_resolver,
1527 * Read DNS udp packet from socket
1529 * @param cls the resolver handle
1530 * @param tc task context
1533 read_dns_response (void *cls,
1534 const struct GNUNET_SCHEDULER_TaskContext *tc)
1536 struct ResolverHandle *rh = cls;
1537 struct RecordLookupHandle *rlh = rh->proc_cls;
1538 char buf[UINT16_MAX];
1540 struct sockaddr_in addr;
1542 struct GNUNET_DNSPARSER_Packet *packet;
1543 struct GNUNET_NAMESTORE_RecordData rd;
1544 int found_delegation = GNUNET_NO;
1545 int found_cname = GNUNET_NO;
1546 char* delegation_name = NULL;
1547 int zone_offset = 0;
1550 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1551 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1553 /* timeout or shutdown */
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1555 "Terminating DNS query %d\n", tc->reason);
1556 finish_lookup (rh, rlh, 0, NULL);
1560 addrlen = sizeof (addr);
1561 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1563 (struct sockaddr*) &addr,
1568 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1569 finish_lookup (rh, rlh, 0, NULL);
1573 packet = GNUNET_DNSPARSER_parse (buf, r);
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1578 "Failed to parse DNS reply!\n");
1579 finish_lookup (rh, rlh, 0, NULL);
1583 for (i = 0; i < packet->num_answers; i++)
1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1586 "Got record type %d (want %d)\n",
1587 packet->answers[i].type,
1589 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1590 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1593 "CNAME... restarting query with %s\n",
1594 packet->answers[i].data.hostname
1596 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1597 found_cname = GNUNET_YES;
1601 if ((packet->answers[i].type == rlh->record_type) &&
1602 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1606 rd.data = packet->answers[i].data.raw.data;
1607 rd.data_size = packet->answers[i].data.raw.data_len;
1608 rd.record_type = packet->answers[i].type;
1610 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1611 finish_lookup (rh, rlh, 1, &rd);
1612 GNUNET_DNSPARSER_free_packet (packet);
1617 if (GNUNET_YES == found_cname)
1619 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1621 if (0 > zone_offset)
1624 /* restart query with CNAME */
1625 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1628 "Asking same server for %s\n", rh->dns_name);
1629 send_dns_packet (rh);
1633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1634 "Trying system resolver for %s\n", rh->dns_name);
1635 resolve_dns_name (rh);
1638 GNUNET_DNSPARSER_free_packet (packet);
1642 for (i = 0; i < packet->num_authority_records; i++)
1645 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1648 "Found NS delegation!\n");
1649 found_delegation = GNUNET_YES;
1650 delegation_name = packet->authority_records[i].data.hostname;
1655 for (i = 0; i < packet->num_additional_records; i++)
1657 if (GNUNET_NO == found_delegation)
1660 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1661 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1663 GNUNET_assert (sizeof (struct in_addr) ==
1664 packet->authority_records[i].data.raw.data_len);
1666 rh->dns_addr.sin_addr =
1667 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1668 send_dns_packet (rh);
1669 GNUNET_DNSPARSER_free_packet (packet);
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1675 "Nothing useful in DNS reply!\n");
1676 finish_lookup (rh, rlh, 0, NULL);
1677 GNUNET_DNSPARSER_free_packet (packet);
1683 * Sends a UDP dns query to a nameserver specified in the rh
1685 * @param rh the request handle
1688 send_dns_packet (struct ResolverHandle *rh)
1690 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1691 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1694 "Sending %dbyte DNS query\n",
1695 rh->dns_raw_packet_size);
1697 if (GNUNET_SYSERR ==
1698 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1700 rh->dns_raw_packet_size,
1701 (struct sockaddr*)&rh->dns_addr,
1702 sizeof (struct sockaddr_in)))
1703 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1704 _("Failed to send DNS request to %s\n"),
1705 GNUNET_a2s ((const struct sockaddr *)&rh->dns_addr,
1706 sizeof (struct sockaddr_in)));
1708 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1709 rh->timeout, //FIXME less?
1715 GNUNET_NETWORK_fdset_destroy (rset);
1721 * The final phase of resoution.
1722 * We found a NS RR and want to resolve via DNS
1724 * @param rh the pending lookup handle
1725 * @param rd_count length of record data
1726 * @param rd record data containing VPN RR
1729 resolve_record_dns (struct ResolverHandle *rh,
1731 const struct GNUNET_NAMESTORE_RecordData *rd)
1733 struct GNUNET_DNSPARSER_Query query;
1734 struct GNUNET_DNSPARSER_Packet packet;
1735 struct GNUNET_DNSPARSER_Flags flags;
1736 struct in_addr dnsip;
1737 struct sockaddr_in addr;
1738 struct sockaddr *sa;
1740 struct RecordLookupHandle *rlh = rh->proc_cls;
1742 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1743 memset (rh->dns_name, 0, sizeof (rh->dns_name));
1745 /* We cancel here as to not include the ns lookup in the timeout */
1746 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1748 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1749 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1751 /* Start shortening */
1752 if ((NULL != rh->priv_key) &&
1753 (GNUNET_YES == is_canonical (rh->name)))
1755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1756 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1758 start_shorten (rh->authority_chain_head,
1762 for (i = 0; i < rd_count; i++)
1764 /* Synthesize dns name */
1765 if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1767 strcpy (rh->dns_zone, (char*)rd[i].data);
1768 if (0 == strcmp (rh->name, ""))
1769 strcpy (rh->dns_name, (char*)rd[i].data);
1771 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1774 if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1775 /* need to use memcpy as .data may be unaligned */
1776 memcpy (&dnsip, rd[i].data, sizeof (dnsip));
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1780 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1784 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1785 if (NULL == rh->dns_sock)
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1788 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1790 finish_lookup (rh, rlh, 0, NULL);
1794 memset (&addr, 0, sizeof (struct sockaddr_in));
1795 sa = (struct sockaddr *) &addr;
1796 sa->sa_family = AF_INET;
1797 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1799 sizeof (struct sockaddr_in)))
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1802 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1804 finish_lookup (rh, rlh, 0, NULL);
1807 query.name = rh->dns_name;
1808 query.type = rlh->record_type;
1809 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1810 memset (&flags, 0, sizeof (flags));
1811 flags.recursion_desired = 1;
1812 flags.checking_disabled = 1;
1813 packet.queries = &query;
1814 packet.answers = NULL;
1815 packet.authority_records = NULL;
1816 packet.num_queries = 1;
1817 packet.num_answers = 0;
1818 packet.num_authority_records = 0;
1819 packet.num_additional_records = 0;
1820 packet.flags = flags;
1822 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1824 &rh->dns_raw_packet,
1825 &rh->dns_raw_packet_size))
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1828 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1830 GNUNET_NETWORK_socket_close (rh->dns_sock);
1831 finish_lookup (rh, rlh, 0, NULL);
1835 rh->dns_addr.sin_family = AF_INET;
1836 rh->dns_addr.sin_port = htons (53); //domain
1837 rh->dns_addr.sin_addr = dnsip;
1838 #if HAVE_SOCKADDR_IN_SIN_LEN
1839 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1842 send_dns_packet (rh);
1847 * The final phase of resoution.
1848 * We found a VPN RR and want to request an IPv4/6 address
1850 * @param rh the pending lookup handle
1851 * @param rd_count length of record data
1852 * @param rd record data containing VPN RR
1855 resolve_record_vpn (struct ResolverHandle *rh,
1857 const struct GNUNET_NAMESTORE_RecordData *rd)
1859 struct RecordLookupHandle *rlh = rh->proc_cls;
1860 struct GNUNET_HashCode serv_desc;
1861 struct vpn_data* vpn;
1864 /* We cancel here as to not include the ns lookup in the timeout */
1865 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1867 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1868 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1870 /* Start shortening */
1871 if ((NULL != rh->priv_key) &&
1872 (GNUNET_YES == is_canonical (rh->name)))
1874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1875 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1877 start_shorten (rh->authority_chain_head,
1881 vpn = (struct vpn_data*)rd->data;
1884 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1885 strlen ((char*)&vpn[1]) + 1,
1887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1888 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1891 GNUNET_h2s (&vpn->peer));
1893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1894 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1897 GNUNET_h2s (&serv_desc));
1898 rh->proc = &handle_record_vpn;
1899 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1903 if (NULL == vpn_handle)
1905 vpn_handle = GNUNET_VPN_connect (cfg);
1906 if (NULL == vpn_handle)
1908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1909 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1910 finish_lookup (rh, rh->proc_cls, 0, NULL);
1915 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1916 af, ntohs (vpn->proto),
1917 (struct GNUNET_PeerIdentity *)&vpn->peer,
1920 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1921 &process_record_result_vpn,
1927 * The final phase of resolution.
1928 * rh->name is a name that is canonical and we do not have a delegation.
1929 * Query namestore for this record
1931 * @param rh the pending lookup handle
1934 resolve_record_ns(struct ResolverHandle *rh)
1936 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1938 /* We cancel here as to not include the ns lookup in the timeout */
1939 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1941 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1942 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1944 /* Start shortening */
1945 if ((NULL != rh->priv_key) &&
1946 (GNUNET_YES == is_canonical (rh->name)))
1948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1949 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1951 start_shorten (rh->authority_chain_head,
1956 * Try to resolve this record in our namestore.
1957 * The name to resolve is now in rh->authority_name
1958 * since we tried to resolve it to an authority
1961 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1965 &process_record_result_ns,
1971 * Handle timeout for DHT requests
1973 * @param cls the request handle as closure
1974 * @param tc the task context
1977 dht_authority_lookup_timeout(void *cls,
1978 const struct GNUNET_SCHEDULER_TaskContext *tc)
1980 struct ResolverHandle *rh = cls;
1981 struct RecordLookupHandle *rlh = rh->proc_cls;
1982 char new_name[MAX_DNS_NAME_LENGTH];
1984 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1985 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
1986 rh->id, rh->authority_name, rh->timeout.rel_value);
1988 rh->status |= RSL_TIMED_OUT;
1989 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1990 if (NULL != rh->get_handle)
1991 GNUNET_DHT_get_stop (rh->get_handle);
1993 rh->get_handle = NULL;
1994 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
1996 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1997 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
1999 rh->proc(rh->proc_cls, rh, 0, NULL);
2003 if (0 == strcmp(rh->name, ""))
2006 * promote authority back to name and try to resolve record
2008 strcpy(rh->name, rh->authority_name);
2009 rh->proc(rh->proc_cls, rh, 0, NULL);
2014 * Start resolution in bg
2016 GNUNET_assert (0 < GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
2017 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD));
2019 strcpy(rh->name, new_name);
2021 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2022 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2023 rh->id, rh->name, rlh->record_type);
2025 gns_resolver_lookup_record (rh->authority,
2026 rh->private_local_zone,
2030 GNUNET_TIME_UNIT_FOREVER_REL,
2032 &background_lookup_result_processor,
2034 rh->proc(rh->proc_cls, rh, 0, NULL);
2039 * Start DHT lookup for a name -> PKEY (compare NS) record in
2040 * rh->authority's zone
2042 * @param rh the pending gns query
2044 static void resolve_delegation_dht(struct ResolverHandle *rh);
2048 * Resolve the delegation chain for the request in our namestore
2050 * @param rh the resolver handle
2052 static void resolve_delegation_ns(struct ResolverHandle *rh);
2056 * Namestore resolution for delegation finished. Processing result.
2058 * @param cls the closure
2059 * @param rh resolver handle
2060 * @param rd_count number of results (always 0)
2061 * @param rd record data (always NULL)
2064 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2065 unsigned int rd_count,
2066 const struct GNUNET_NAMESTORE_RecordData *rd);
2070 * This is a callback function that checks for key revocation
2072 * @param cls the pending query
2073 * @param key the key of the zone we did the lookup
2074 * @param expiration expiration date of the record data set in the namestore
2075 * @param name the name for which we need an authority
2076 * @param rd_count the number of records with 'name'
2077 * @param rd the record data
2078 * @param signature the signature of the authority for the record data
2081 process_pkey_revocation_result_ns (void *cls,
2082 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2083 struct GNUNET_TIME_Absolute expiration,
2085 unsigned int rd_count,
2086 const struct GNUNET_NAMESTORE_RecordData *rd,
2087 const struct GNUNET_CRYPTO_RsaSignature *signature)
2089 struct ResolverHandle *rh = cls;
2090 struct GNUNET_TIME_Relative remaining_time;
2093 rh->namestore_task = NULL;
2094 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2096 for (i = 0; i < rd_count; i++)
2098 if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2100 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2101 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2103 rh->status |= RSL_PKEY_REVOKED;
2104 rh->proc (rh->proc_cls, rh, 0, NULL);
2109 if ((NULL == name) ||
2110 (0 == remaining_time.rel_value))
2112 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2113 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2116 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2118 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2119 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2120 rh->id, "+.gads", GNUNET_GNS_RECORD_REV);
2122 gns_resolver_lookup_record(rh->authority,
2123 rh->private_local_zone,
2124 GNUNET_GNS_RECORD_REV,
2127 GNUNET_TIME_UNIT_FOREVER_REL,
2129 &background_lookup_result_processor,
2133 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2134 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2137 * We are done with PKEY resolution if name is empty
2138 * else resolve again with new authority
2140 if (strcmp (rh->name, "") == 0)
2141 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2143 resolve_delegation_ns (rh);
2148 * Callback when record data is put into namestore
2150 * @param cls the closure
2151 * @param success GNUNET_OK on success
2152 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2155 on_namestore_delegation_put_result(void *cls,
2159 struct NamestoreBGTask *nbg = cls;
2161 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2164 if (GNUNET_NO == success)
2166 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2167 "GNS_NS: records already in namestore\n");
2170 else if (GNUNET_YES == success)
2172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2173 "GNS_NS: records successfully put in namestore\n");
2177 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2178 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2183 * Function called when we get a result from the dht
2184 * for our query. Recursively tries to resolve authorities
2187 * @param cls the request handle
2188 * @param exp lifetime
2189 * @param key the key the record was stored under
2190 * @param get_path get path
2191 * @param get_path_length get path length
2192 * @param put_path put path
2193 * @param put_path_length put path length
2194 * @param type the block type
2195 * @param size the size of the record
2196 * @param data the record data
2199 process_delegation_result_dht(void* cls,
2200 struct GNUNET_TIME_Absolute exp,
2201 const struct GNUNET_HashCode * key,
2202 const struct GNUNET_PeerIdentity *get_path,
2203 unsigned int get_path_length,
2204 const struct GNUNET_PeerIdentity *put_path,
2205 unsigned int put_path_length,
2206 enum GNUNET_BLOCK_Type type,
2207 size_t size, const void *data)
2209 struct ResolverHandle *rh = cls;
2210 struct GNSNameRecordBlock *nrb;
2211 uint32_t num_records;
2213 char* rd_data = (char*) data;
2216 struct GNUNET_CRYPTO_ShortHashCode zone;
2218 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2219 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2224 nrb = (struct GNSNameRecordBlock*)data;
2226 /* stop dht lookup and timeout task */
2227 GNUNET_DHT_get_stop (rh->get_handle);
2228 rh->get_handle = NULL;
2229 if (rh->dht_heap_node != NULL)
2231 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2232 rh->dht_heap_node = NULL;
2235 num_records = ntohl(nrb->rd_count);
2236 name = (char*)&nrb[1];
2238 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2239 struct NamestoreBGTask *ns_heap_root;
2240 struct NamestoreBGTask *namestore_bg_task;
2242 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2243 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2244 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2249 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2250 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2255 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2256 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2257 rh->id, name, rh->authority_name);
2258 for (i=0; i<num_records; i++)
2261 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2262 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2263 rh->id, name, rh->authority_name);
2264 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2265 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2266 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2268 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2269 rh->id, rd[i].data_size);
2270 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2271 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2272 rh->id, rd[i].flags);
2274 if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2275 (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2276 (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2279 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2281 if (0 == strcmp(rh->name, ""))
2282 strcpy(rh->name, rh->authority_name);
2284 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2285 rh->name, rh->authority_name); //FIXME ret
2290 if ((0 == strcmp(name, rh->authority_name)) &&
2291 (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2293 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2294 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2297 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2298 struct AuthorityChain *auth =
2299 GNUNET_malloc(sizeof(struct AuthorityChain));
2300 auth->zone = rh->authority;
2301 memset(auth->name, 0, strlen(rh->authority_name)+1);
2302 strcpy(auth->name, rh->authority_name);
2303 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2304 rh->authority_chain_tail,
2307 if (NULL != rh->rd.data)
2308 GNUNET_free ((void*)rh->rd.data);
2310 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2311 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2312 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2315 /** try to import pkey if private key available */
2316 //if (rh->priv_key && is_canonical (rh->name))
2317 // process_discovered_authority(name, auth->zone,
2318 // rh->authority_chain_tail->zone,
2323 GNUNET_GNS_get_zone_from_key (name, key, &zone);
2326 /* Save to namestore
2327 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2330 if (max_allowed_ns_tasks <=
2331 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2333 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2334 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2336 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2337 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background ns task\n",
2340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2341 "GNS_PHASE_DELEGATE_DHT-%llu: Caching record for %s\n",
2343 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2345 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2347 GNUNET_TIME_absolute_get().abs_value);
2348 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2355 &on_namestore_delegation_put_result, //cont
2356 namestore_bg_task); //cls
2360 if (0 != rh->answered)
2365 * FIXME in this case. should we ask namestore again?
2367 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2368 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2369 rh->id, rh->authority_name, rh->name);
2371 if (0 == strcmp(rh->name, ""))
2373 /* Start shortening */
2374 if ((NULL != rh->priv_key) &&
2375 (GNUNET_YES == is_canonical (rh->name)))
2377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2378 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2380 start_shorten (rh->authority_chain_head,
2385 rh->proc = &handle_delegation_ns;
2388 /* Check for key revocation and delegate */
2389 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2392 GNUNET_GNS_RECORD_REV,
2393 &process_pkey_revocation_result_ns,
2400 * No pkey but name exists
2403 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2404 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2405 rh->id, rh->authority_name, rh->name);
2406 if (0 == strcmp(rh->name, ""))
2407 strcpy(rh->name, rh->authority_name);
2409 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2410 rh->name, rh->authority_name); //FIXME ret
2412 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2413 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2414 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2415 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2417 rh->proc(rh->proc_cls, rh, 0, NULL);
2420 //FIXME maybe define somewhere else?
2421 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2422 +(MAX_DNS_NAME_LENGTH*2)
2423 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2424 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2428 * Exands a name ending in .+ with the zone of origin
2430 * @param dest destination buffer
2431 * @param sec the .+ name
2432 * @param repl the string to replace the + with
2435 expand_plus(char* dest, char* src, char* repl)
2438 unsigned int s_len = strlen(src)+1;
2440 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2443 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2444 "GNS_POSTPROCESS: %s too short\n", src);
2446 /* no postprocessing */
2447 memcpy(dest, src, s_len+1);
2451 if (0 == strcmp(src+s_len-3, ".+"))
2453 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2454 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2455 memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2459 pos += strlen(repl);
2460 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2461 "GNS_POSTPROCESS: Expanded to %s\n", dest);
2465 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2466 "GNS_POSTPROCESS: No postprocessing for %s\n", src);
2467 memcpy(dest, src, s_len+1);
2476 finish_lookup (struct ResolverHandle *rh,
2477 struct RecordLookupHandle* rlh,
2478 unsigned int rd_count,
2479 const struct GNUNET_NAMESTORE_RecordData *rd)
2482 char new_rr_data[MAX_DNS_NAME_LENGTH];
2483 char new_mx_data[MAX_MX_LENGTH];
2484 char new_soa_data[MAX_SOA_LENGTH];
2485 char new_srv_data[MAX_SRV_LENGTH];
2486 struct srv_data *old_srv;
2487 struct srv_data *new_srv;
2488 struct soa_data *old_soa;
2489 struct soa_data *new_soa;
2490 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2493 unsigned int offset;
2495 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
2497 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2498 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2501 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2504 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2506 for (i = 0; i < rd_count; i++)
2509 if ((GNUNET_GNS_RECORD_NS != rd[i].record_type) &&
2510 (GNUNET_GNS_RECORD_PTR != rd[i].record_type) &&
2511 (GNUNET_GNS_RECORD_CNAME != rd[i].record_type) &&
2512 (GNUNET_GNS_RECORD_MX != rd[i].record_type) &&
2513 (GNUNET_GNS_RECORD_SOA != rd[i].record_type) &&
2514 (GNUNET_GNS_RECORD_SRV != rd[i].record_type))
2516 p_rd[i].data = rd[i].data;
2521 * for all those records we 'should'
2522 * also try to resolve the A/AAAA records (RFC1035)
2523 * This is a feature and not important
2526 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2527 "GNS_POSTPROCESS: Postprocessing\n");
2528 if (0 == strcmp(rh->name, "+"))
2529 repl_string = rlh->name;
2531 repl_string = rlh->name+strlen(rh->name)+1;
2534 if (GNUNET_GNS_RECORD_MX == rd[i].record_type)
2536 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2537 offset = sizeof(uint16_t);
2538 pos = new_mx_data+offset;
2539 expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2541 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2542 p_rd[i].data = new_mx_data;
2543 p_rd[i].data_size = offset;
2545 else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2548 * Prio, weight and port
2550 new_srv = (struct srv_data*)new_srv_data;
2551 old_srv = (struct srv_data*)rd[i].data;
2552 new_srv->prio = old_srv->prio;
2553 new_srv->weight = old_srv->weight;
2554 new_srv->port = old_srv->port;
2555 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2557 p_rd[i].data = new_srv_data;
2558 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2560 else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2562 /* expand mname and rname */
2563 old_soa = (struct soa_data*)rd[i].data;
2564 new_soa = (struct soa_data*)new_soa_data;
2565 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2566 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2567 offset = strlen ((char*)&new_soa[1]) + 1;
2568 expand_plus((char*)&new_soa[1] + offset,
2569 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2571 p_rd[i].data_size = sizeof (struct soa_data)
2573 + strlen ((char*)&new_soa[1] + offset);
2574 p_rd[i].data = new_soa_data;
2579 expand_plus(pos, (char*)rd[i].data, repl_string);
2580 p_rd[i].data_size = strlen(new_rr_data)+1;
2581 p_rd[i].data = new_rr_data;
2586 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2588 free_resolver_handle (rh);
2593 * Process DHT lookup result for record.
2595 * @param cls the closure
2596 * @param rh resolver handle
2597 * @param rd_count number of results
2598 * @param rd record data
2601 handle_record_dht(void* cls, struct ResolverHandle *rh,
2602 unsigned int rd_count,
2603 const struct GNUNET_NAMESTORE_RecordData *rd)
2605 struct RecordLookupHandle* rlh = cls;
2609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2610 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2612 /* give up, cannot resolve */
2613 finish_lookup (rh, rlh, 0, NULL);
2616 /* results found yay */
2617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2618 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2619 finish_lookup (rh, rlh, rd_count, rd);
2624 * Process namestore lookup result for record.
2626 * @param cls the closure
2627 * @param rh resolver handle
2628 * @param rd_count number of results
2629 * @param rd record data
2632 handle_record_ns (void* cls, struct ResolverHandle *rh,
2633 unsigned int rd_count,
2634 const struct GNUNET_NAMESTORE_RecordData *rd)
2636 struct RecordLookupHandle* rlh = cls;
2637 int check_dht = GNUNET_YES;
2641 /* results found yay */
2642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2643 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2644 finish_lookup (rh, rlh, rd_count, rd);
2648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2649 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2653 * There are 5 conditions that have to met for us to consult the DHT:
2654 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2655 * 2. No entry in the NS existed AND
2656 * 3. The zone queried is not the local resolver's zone AND
2657 * 4. The name that was looked up is '+'
2658 * because if it was any other canonical name we either already queried
2659 * the DHT for the authority in the authority lookup phase (and thus
2660 * would already have an entry in the NS for the record)
2661 * 5. We are not in cache only mode
2663 if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2664 (0 == (rh->status & RSL_RECORD_EXISTS)) )
2667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2668 "GNS_PHASE_REC-%llu: Not expired and exists!\n",
2670 check_dht = GNUNET_NO;
2673 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2674 &rh->private_local_zone))
2677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2678 "GNS_PHASE_REC-%llu: Our zone!\n",
2680 check_dht = GNUNET_NO;
2683 if ((0 != strcmp (rh->name, "+")) && (GNUNET_YES == is_srv (rh->name)))
2684 check_dht = GNUNET_NO;
2686 if (GNUNET_YES == rh->only_cached)
2687 check_dht = GNUNET_NO;
2689 if (GNUNET_YES == check_dht)
2691 rh->proc = &handle_record_dht;
2692 resolve_record_dht(rh);
2695 /* give up, cannot resolve */
2696 finish_lookup (rh, rlh, 0, NULL);
2701 * Move one level up in the domain hierarchy and return the
2702 * passed top level domain.
2704 * @param name the domain
2705 * @param dest the destination where the tld will be put
2708 pop_tld(char* name, char* dest)
2712 if (GNUNET_YES == is_canonical (name))
2719 for (len = strlen(name); 0 < len; len--)
2721 if (*(name+len) == '.')
2731 strcpy(dest, (name+len+1));
2736 * Checks if name is in tld
2738 * @param name the name to check
2739 * @param tld the TLD to check for
2740 * @return GNUNET_YES or GNUNET_NO
2743 is_tld(const char* name, const char* tld)
2747 if (strlen(name) <= strlen(tld))
2750 offset = strlen(name)-strlen(tld);
2751 if (0 != strcmp(name+offset, tld))
2753 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2754 "%s is not in .%s TLD\n", name, tld);
2762 * DHT resolution for delegation finished. Processing result.
2764 * @param cls the closure
2765 * @param rh resolver handle
2766 * @param rd_count number of results (always 0)
2767 * @param rd record data (always NULL)
2770 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2771 unsigned int rd_count,
2772 const struct GNUNET_NAMESTORE_RecordData *rd)
2774 struct RecordLookupHandle* rlh;
2778 if (0 == strcmp(rh->name, ""))
2780 if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2782 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2783 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2785 finish_lookup(rh, rlh, rd_count, rd);
2788 /* We resolved full name for delegation. resolving record */
2789 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2790 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2792 strcpy(rh->name, "+\0");
2793 rh->proc = &handle_record_ns;
2794 resolve_record_ns(rh);
2799 * we still have some left
2801 if (GNUNET_YES == is_canonical (rh->name))
2803 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2804 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2807 rh->proc = &handle_record_ns;
2808 resolve_record_ns(rh);
2811 /* give up, cannot resolve */
2812 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2813 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2815 finish_lookup(rh, rlh, 0, NULL);
2820 * Start DHT lookup for a name -> PKEY (compare NS) record in
2821 * rh->authority's zone
2823 * @param rh the pending gns query
2826 resolve_delegation_dht(struct ResolverHandle *rh)
2829 struct GNUNET_HashCode lookup_key;
2830 struct ResolverHandle *rh_heap_root;
2832 pop_tld(rh->name, rh->authority_name);
2834 GNUNET_GNS_get_key_for_record (rh->authority_name, &rh->authority, &lookup_key);
2836 rh->dht_heap_node = NULL;
2838 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2840 rh->timeout_cont = &dht_authority_lookup_timeout;
2841 rh->timeout_cont_cls = rh;
2845 if (max_allowed_background_queries <=
2846 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2848 /* terminate oldest lookup */
2849 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2850 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2851 rh_heap_root->get_handle = NULL;
2852 rh_heap_root->dht_heap_node = NULL;
2854 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2855 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2856 rh->id, rh_heap_root->authority_name);
2858 rh_heap_root->proc(rh_heap_root->proc_cls,
2863 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2865 GNUNET_TIME_absolute_get().abs_value);
2868 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2869 GNUNET_assert(rh->get_handle == NULL);
2870 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2871 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2873 DHT_GNS_REPLICATION_LEVEL,
2874 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2877 &process_delegation_result_dht,
2883 * Namestore resolution for delegation finished. Processing result.
2885 * @param cls the closure
2886 * @param rh resolver handle
2887 * @param rd_count number of results (always 0)
2888 * @param rd record data (always NULL)
2891 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2892 unsigned int rd_count,
2893 const struct GNUNET_NAMESTORE_RecordData *rd)
2895 struct RecordLookupHandle* rlh;
2897 int check_dht = GNUNET_YES;
2900 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2901 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2902 rh->id, rh->status);
2904 if (rh->status & RSL_PKEY_REVOKED)
2906 finish_lookup (rh, rlh, 0, NULL);
2910 if (0 == strcmp(rh->name, ""))
2913 /* We resolved full name for delegation. resolving record */
2914 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2915 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2917 if (rh->status & RSL_CNAME_FOUND)
2919 if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2921 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2922 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2924 strcpy (rh->name, rh->authority_name);
2925 finish_lookup (rh, rlh, rd_count, rd);
2930 if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2932 s_len = strlen (rd->data) - 2;
2933 memcpy (rh->name, rd->data, s_len);
2934 rh->name[s_len] = '\0';
2935 resolve_delegation_ns (rh);
2938 else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2940 gns_resolver_lookup_record (rh->authority,
2941 rh->private_local_zone,
2950 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2951 free_resolver_handle (rh);
2957 strcpy (rh->dns_name, (char*)rd->data);
2958 resolve_dns_name (rh);
2963 else if (rh->status & RSL_DELEGATE_VPN)
2965 if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2967 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2968 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2970 finish_lookup(rh, rlh, rd_count, rd);
2973 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2974 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2976 GNUNET_assert (NULL != rd);
2977 rh->proc = &handle_record_vpn;
2978 resolve_record_vpn (rh, rd_count, rd);
2981 else if (rh->status & RSL_DELEGATE_NS)
2983 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2985 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2986 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2988 finish_lookup(rh, rlh, rd_count, rd);
2992 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2993 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2995 GNUNET_assert (NULL != rd);
2996 rh->proc = &handle_record_ns;
2997 resolve_record_dns (rh, rd_count, rd);
3000 else if (rh->status & RSL_DELEGATE_PKEY)
3002 if (rh->status & RSL_PKEY_REVOKED)
3004 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3005 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
3007 finish_lookup (rh, rlh, 0, NULL);
3010 else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
3012 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3013 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
3015 finish_lookup(rh, rlh, rd_count, rd);
3019 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3020 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3022 strcpy(rh->name, "+\0");
3023 rh->proc = &handle_record_ns;
3024 resolve_record_ns(rh);
3028 if (rh->status & RSL_DELEGATE_NS)
3030 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
3032 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3033 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3035 finish_lookup(rh, rlh, rd_count, rd);
3039 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3040 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3042 GNUNET_assert (NULL != rd);
3043 rh->proc = &handle_record_ns;
3044 resolve_record_dns (rh, rd_count, rd);
3049 * we still have some left
3050 * check if authority in ns is fresh
3052 * or we are authority
3055 if ((rh->status & RSL_RECORD_EXISTS) &&
3056 !(rh->status & RSL_RECORD_EXPIRED))
3057 check_dht = GNUNET_NO;
3059 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3060 &rh->private_local_zone))
3061 check_dht = GNUNET_NO;
3063 if (GNUNET_YES == rh->only_cached)
3064 check_dht = GNUNET_NO;
3066 if (GNUNET_YES == check_dht)
3069 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3070 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3072 rh->proc = &handle_delegation_dht;
3073 resolve_delegation_dht(rh);
3077 if (GNUNET_NO == is_canonical (rh->name))
3079 /* give up, cannot resolve */
3080 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3081 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3084 finish_lookup(rh, rlh, rd_count, rd);
3087 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3088 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3091 rh->proc = &handle_record_ns;
3092 resolve_record_ns(rh);
3097 * This is a callback function that should give us only PKEY
3098 * records. Used to query the namestore for the authority (PKEY)
3099 * for 'name'. It will recursively try to resolve the
3100 * authority for a given name from the namestore.
3102 * @param cls the pending query
3103 * @param key the key of the zone we did the lookup
3104 * @param expiration expiration date of the record data set in the namestore
3105 * @param name the name for which we need an authority
3106 * @param rd_count the number of records with 'name'
3107 * @param rd the record data
3108 * @param signature the signature of the authority for the record data
3111 process_delegation_result_ns (void* cls,
3112 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3113 struct GNUNET_TIME_Absolute expiration,
3115 unsigned int rd_count,
3116 const struct GNUNET_NAMESTORE_RecordData *rd,
3117 const struct GNUNET_CRYPTO_RsaSignature *signature)
3119 struct ResolverHandle *rh;
3120 struct GNUNET_TIME_Relative remaining_time;
3121 struct GNUNET_CRYPTO_ShortHashCode zone;
3122 char new_name[MAX_DNS_NAME_LENGTH];
3124 struct GNUNET_TIME_Absolute et;
3126 rh = (struct ResolverHandle *)cls;
3127 rh->namestore_task = NULL;
3128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3129 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3132 GNUNET_CRYPTO_short_hash (key,
3133 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3135 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3142 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3144 rh->status |= RSL_RECORD_EXISTS;
3146 if (remaining_time.rel_value == 0)
3148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3149 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3151 rh->status |= RSL_RECORD_EXPIRED;
3156 * No authority found in namestore.
3161 * We did not find an authority in the namestore
3166 * Promote this authority back to a name maybe it is
3169 if (strcmp (rh->name, "") == 0)
3171 /* simply promote back */
3172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3173 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3174 rh->id, rh->authority_name);
3175 strcpy (rh->name, rh->authority_name);
3179 /* add back to existing name */
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3181 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3182 rh->id, rh->authority_name, rh->name);
3183 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3184 rh->name, rh->authority_name);
3185 strcpy (rh->name, new_name);
3186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3187 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3191 rh->proc (rh->proc_cls, rh, 0, NULL);
3196 * We found an authority that may be able to help us
3197 * move on with query
3198 * Note only 1 pkey should have been returned.. anything else would be strange
3200 for (i=0; i < rd_count;i++)
3204 * A CNAME. Like regular DNS this means the is no other record for this
3207 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3209 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3210 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3213 rh->status |= RSL_CNAME_FOUND;
3214 rh->proc (rh->proc_cls, rh, rd_count, rd);
3221 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3223 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3224 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3226 rh->status |= RSL_DELEGATE_VPN;
3227 rh->proc (rh->proc_cls, rh, rd_count, rd);
3233 * FIXME make optional
3235 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3237 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3238 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3240 rh->status |= RSL_DELEGATE_NS;
3241 rh->proc (rh->proc_cls, rh, rd_count, rd);
3245 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3248 rh->status |= RSL_DELEGATE_PKEY;
3250 if ((ignore_pending_records != 0) &&
3251 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3254 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3260 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3261 et.abs_value = rd[i].expiration_time;
3262 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3266 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3268 if (remaining_time.rel_value == 0)
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3271 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3273 rh->authority_chain_head->fresh = 0;
3274 rh->proc (rh->proc_cls, rh, 0, NULL);
3282 * Resolve rest of query with new authority
3284 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3285 memcpy (&rh->authority, rd[i].data,
3286 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3287 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3288 auth->zone = rh->authority;
3289 memset (auth->name, 0, strlen (rh->authority_name)+1);
3290 strcpy (auth->name, rh->authority_name);
3291 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3292 rh->authority_chain_tail,
3294 if (NULL != rh->rd.data)
3295 GNUNET_free ((void*)(rh->rd.data));
3297 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3298 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3299 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3301 /* Check for key revocation and delegate */
3302 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3305 GNUNET_GNS_RECORD_REV,
3306 &process_pkey_revocation_result_ns,
3315 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3316 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3318 * If we have found some records for the LAST label
3319 * we return the results. Else null.
3321 if (strcmp (rh->name, "") == 0)
3323 /* Start shortening */
3324 if ((rh->priv_key != NULL) &&
3325 (is_canonical (rh->name) == GNUNET_YES))
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3328 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3330 start_shorten (rh->authority_chain_head,
3333 /* simply promote back */
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3335 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3336 rh->id, rh->authority_name);
3337 strcpy (rh->name, rh->authority_name);
3338 rh->proc (rh->proc_cls, rh, rd_count, rd);
3342 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
3343 "%s.%s", rh->name, rh->authority_name);
3344 strcpy (rh->name, new_name);
3345 rh->proc (rh->proc_cls, rh, 0, NULL);
3351 * Resolve the delegation chain for the request in our namestore
3353 * @param rh the resolver handle
3356 resolve_delegation_ns (struct ResolverHandle *rh)
3358 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3359 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3361 pop_tld(rh->name, rh->authority_name);
3362 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3365 GNUNET_GNS_RECORD_ANY,
3366 &process_delegation_result_ns,
3373 * Lookup of a record in a specific zone
3374 * calls lookup result processor on result
3376 * @param zone the root zone
3377 * @param pzone the private local zone
3378 * @param record_type the record type to look up
3379 * @param name the name to look up
3380 * @param key a private key for use with PSEU import (can be NULL)
3381 * @param timeout timeout for resolution
3382 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3383 * @param proc the processor to call on result
3384 * @param cls the closure to pass to proc
3387 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3388 struct GNUNET_CRYPTO_ShortHashCode pzone,
3389 uint32_t record_type,
3391 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3392 struct GNUNET_TIME_Relative timeout,
3394 RecordLookupProcessor proc,
3397 struct ResolverHandle *rh;
3398 struct RecordLookupHandle* rlh;
3399 char string_hash[MAX_DNS_LABEL_LENGTH];
3400 char nzkey[MAX_DNS_LABEL_LENGTH];
3401 char* nzkey_ptr = nzkey;
3403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3404 "Starting resolution for %s (type=%d)!\n",
3408 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3409 (strcmp(GNUNET_GNS_TLD, name) != 0))
3411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3412 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3417 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3418 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3420 memset (rh, 0, sizeof (struct ResolverHandle));
3421 rh->authority = zone;
3425 rh->timeout = timeout;
3426 rh->get_handle = NULL;
3427 rh->private_local_zone = pzone;
3428 rh->only_cached = only_cached;
3429 rh->namestore_task = NULL;
3432 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3436 "No shorten key for resolution\n");
3438 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3441 * Set timeout for authority lookup phase to 1/2
3443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3444 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3445 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3446 GNUNET_TIME_relative_divide(timeout, 2),
3447 &handle_lookup_timeout,
3449 rh->timeout_cont = &dht_authority_lookup_timeout;
3450 rh->timeout_cont_cls = rh;
3454 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3455 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3458 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3461 * Only 'gnunet' given
3463 strcpy(rh->name, "\0");
3467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3468 "Checking for TLD...\n");
3469 if (is_zkey_tld(name) == GNUNET_YES)
3471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3474 * This is a zkey tld
3475 * build hash and use as initial authority
3478 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3479 memcpy(rh->name, name,
3480 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3481 pop_tld(rh->name, string_hash);
3483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3484 "ZKEY is %s!\n", string_hash);
3486 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3488 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3492 "Cannot convert ZKEY `%s' to hash!\n", string_hash);
3494 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3495 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3496 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3499 proc (cls, 0, NULL);
3504 else if (is_gnunet_tld (name) == GNUNET_YES)
3506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3509 * Presumably GNUNET tld
3511 memset (rh->name, 0,
3512 strlen(name)-strlen(GNUNET_GNS_TLD));
3513 memcpy (rh->name, name,
3514 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3518 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3519 "Cannot handle this TLD %s\n", string_hash);
3521 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3522 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3523 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3526 proc (cls, 0, NULL);
3532 * Initialize authority chain
3534 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3535 rh->authority_chain_head->prev = NULL;
3536 rh->authority_chain_head->next = NULL;
3537 rh->authority_chain_tail = rh->authority_chain_head;
3538 rh->authority_chain_head->zone = rh->authority;
3539 strcpy (rh->authority_chain_head->name, "");
3542 * Copy original query into lookup handle
3544 rlh->record_type = record_type;
3545 memset(rlh->name, 0, strlen(name) + 1);
3546 strcpy(rlh->name, name);
3548 rlh->proc_cls = cls;
3550 rh->proc = &handle_delegation_ns;
3551 resolve_delegation_ns(rh);
3554 /******** END Record Resolver ***********/
3557 finish_shorten (struct ResolverHandle *rh,
3558 struct NameShortenHandle *nsh)
3560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3561 "Sending %s as shorten result\n", nsh->result);
3562 nsh->proc (nsh->proc_cls, nsh->result);
3563 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3565 free_resolver_handle (rh);
3570 * Callback calles by namestore for a zone to name
3573 * @param cls the closure
3574 * @param zone_key the zone we queried
3575 * @param expire the expiration time of the name
3576 * @param name the name found or NULL
3577 * @param rd_len number of records for the name
3578 * @param rd the record data (PKEY) for the name
3579 * @param signature the signature for the record data
3582 process_zone_to_name_shorten_root (void *cls,
3583 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3584 struct GNUNET_TIME_Absolute expire,
3586 unsigned int rd_len,
3587 const struct GNUNET_NAMESTORE_RecordData *rd,
3588 const struct GNUNET_CRYPTO_RsaSignature *signature);
3592 * Callback called by namestore for a zone to name
3595 * @param cls the closure
3596 * @param zone_key the zone we queried
3597 * @param expire the expiration time of the name
3598 * @param name the name found or NULL
3599 * @param rd_len number of records for the name
3600 * @param rd the record data (PKEY) for the name
3601 * @param signature the signature for the record data
3604 process_zone_to_name_shorten_shorten (void *cls,
3605 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3606 struct GNUNET_TIME_Absolute expire,
3608 unsigned int rd_len,
3609 const struct GNUNET_NAMESTORE_RecordData *rd,
3610 const struct GNUNET_CRYPTO_RsaSignature *signature)
3612 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3613 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3614 struct AuthorityChain *next_authority;
3616 char result[MAX_DNS_NAME_LENGTH];
3617 char tmp_name[MAX_DNS_NAME_LENGTH];
3620 rh->namestore_task = NULL;
3621 /* we found a match in our own root zone */
3624 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3625 memset(result, 0, answer_len);
3627 if (strlen(rh->name) > 0)
3629 sprintf (result, "%s.%s.%s.%s",
3631 nsh->shorten_zone_name,
3636 sprintf (result, "%s.%s.%s", name,
3637 nsh->shorten_zone_name,
3641 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3642 "Found shorten result %s\n", result);
3643 if (strlen (nsh->result) > strlen (result))
3644 strcpy (nsh->result, result);
3646 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3647 nsh->shorten_zone) == 0)
3650 * This is our zone append .gads unless name is empty
3651 * (it shouldn't be, usually FIXME what happens if we
3652 * shorten to our zone to a "" record??)
3655 sprintf (result, "%s.%s.%s",
3657 nsh->shorten_zone_name,
3659 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3660 "Our zone: Found %s as shorten result\n", result);
3662 if (strlen (nsh->result) > strlen (result))
3663 strcpy (nsh->result, result);
3664 //nsh->proc(nsh->proc_cls, result);
3666 //free_resolver_handle(rh);
3673 * continue with next authority if exists
3675 if (NULL == rh->authority_chain_head->next)
3677 finish_shorten (rh, nsh);
3680 next_authority = rh->authority_chain_head;
3682 if (0 == strcmp (rh->name, ""))
3683 strcpy (tmp_name, next_authority->name);
3685 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3686 "%s.%s", rh->name, next_authority->name);
3688 strcpy(rh->name, tmp_name);
3689 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3690 "No PSEU found for authority %s. Promoting back: %s\n",
3691 next_authority->name, rh->name);
3693 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3694 rh->authority_chain_tail,
3697 GNUNET_free (next_authority);
3699 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3700 &rh->authority_chain_tail->zone,
3701 &rh->authority_chain_head->zone,
3702 &process_zone_to_name_shorten_root,
3708 * Callback calles by namestore for a zone to name
3711 * @param cls the closure
3712 * @param zone_key the zone we queried
3713 * @param expire the expiration time of the name
3714 * @param name the name found or NULL
3715 * @param rd_len number of records for the name
3716 * @param rd the record data (PKEY) for the name
3717 * @param signature the signature for the record data
3720 process_zone_to_name_shorten_private (void *cls,
3721 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3722 struct GNUNET_TIME_Absolute expire,
3724 unsigned int rd_len,
3725 const struct GNUNET_NAMESTORE_RecordData *rd,
3726 const struct GNUNET_CRYPTO_RsaSignature *signature)
3728 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3729 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3730 struct AuthorityChain *next_authority;
3732 char result[MAX_DNS_NAME_LENGTH];
3733 char tmp_name[MAX_DNS_NAME_LENGTH];
3736 rh->namestore_task = NULL;
3737 /* we found a match in our own root zone */
3740 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3741 memset(result, 0, answer_len);
3743 if (strlen(rh->name) > 0)
3745 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3749 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3752 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3753 "Found shorten result %s\n", result);
3754 if (strlen (nsh->result) > strlen (result))
3755 strcpy (nsh->result, result);
3757 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3758 nsh->private_zone) == 0)
3761 * This is our zone append .gads unless name is empty
3762 * (it shouldn't be, usually FIXME what happens if we
3763 * shorten to our zone to a "" record??)
3766 sprintf (result, "%s.%s.%s",
3767 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3768 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3769 "Our private zone: Found %s as shorten result %s\n", result);
3770 if (strlen (nsh->result) > strlen (result))
3771 strcpy (nsh->result, result);
3774 if (0 != strcmp (nsh->shorten_zone_name, ""))
3776 /* backtrack authorities for names in priv zone */
3777 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3779 &rh->authority_chain_head->zone,
3780 &process_zone_to_name_shorten_shorten,
3787 * continue with next authority if exists
3789 if (NULL == rh->authority_chain_head->next)
3791 finish_shorten (rh, nsh);
3794 next_authority = rh->authority_chain_head;
3796 if (0 == strcmp (rh->name, ""))
3797 strcpy (tmp_name, next_authority->name);
3799 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3800 "%s.%s", rh->name, next_authority->name);
3802 strcpy(rh->name, tmp_name);
3803 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3804 "No PSEU found for authority %s. Promoting back: %s\n",
3805 next_authority->name, rh->name);
3807 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3808 rh->authority_chain_tail,
3811 GNUNET_free (next_authority);
3813 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3814 &rh->authority_chain_tail->zone,
3815 &rh->authority_chain_head->zone,
3816 &process_zone_to_name_shorten_root,
3823 * Callback calles by namestore for a zone to name
3826 * @param cls the closure
3827 * @param zone_key the zone we queried
3828 * @param expire the expiration time of the name
3829 * @param name the name found or NULL
3830 * @param rd_len number of records for the name
3831 * @param rd the record data (PKEY) for the name
3832 * @param signature the signature for the record data
3835 process_zone_to_name_shorten_root (void *cls,
3836 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3837 struct GNUNET_TIME_Absolute expire,
3839 unsigned int rd_len,
3840 const struct GNUNET_NAMESTORE_RecordData *rd,
3841 const struct GNUNET_CRYPTO_RsaSignature *signature)
3843 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3844 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3845 struct AuthorityChain *next_authority;
3847 char result[MAX_DNS_NAME_LENGTH];
3848 char tmp_name[MAX_DNS_NAME_LENGTH];
3851 rh->namestore_task = NULL;
3852 /* we found a match in our own root zone */
3855 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3856 memset(result, 0, answer_len);
3858 if (strlen(rh->name) > 0)
3860 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3864 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3867 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3868 "Found shorten result %s\n", result);
3869 if (strlen (nsh->result) > strlen (result))
3870 strcpy (nsh->result, result);
3872 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3873 nsh->root_zone) == 0)
3876 * This is our zone append .gads unless name is empty
3877 * (it shouldn't be, usually FIXME what happens if we
3878 * shorten to our zone to a "" record??)
3881 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3882 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3883 "Our zone: Found %s as shorten result\n", result);
3884 if (strlen (nsh->result) > strlen (result))
3885 strcpy (nsh->result, result);
3888 if (NULL != nsh->private_zone)
3890 /* backtrack authorities for names in priv zone */
3891 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3893 &rh->authority_chain_head->zone,
3894 &process_zone_to_name_shorten_private,
3897 else if (NULL != nsh->shorten_zone)
3899 /* backtrack authorities for names in shorten zone */
3900 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3902 &rh->authority_chain_head->zone,
3903 &process_zone_to_name_shorten_shorten,
3910 * continue with next authority if exists
3912 if (NULL == rh->authority_chain_head->next)
3914 finish_shorten (rh, nsh);
3917 next_authority = rh->authority_chain_head;
3919 if (0 == strcmp (rh->name, ""))
3920 strcpy (tmp_name, next_authority->name);
3922 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3923 "%s.%s", rh->name, next_authority->name);
3925 strcpy(rh->name, tmp_name);
3926 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3927 "No PSEU found for authority %s. Promoting back: %s\n",
3928 next_authority->name, rh->name);
3930 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3931 rh->authority_chain_tail,
3934 GNUNET_free (next_authority);
3936 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3937 &rh->authority_chain_tail->zone,
3938 &rh->authority_chain_head->zone,
3939 &process_zone_to_name_shorten_root,
3946 * Process result from namestore delegation lookup
3947 * for shorten operation
3949 * @param cls the client shorten handle
3950 * @param rh the resolver handle
3951 * @param rd_count number of results (0)
3952 * @param rd data (NULL)
3955 handle_delegation_ns_shorten (void* cls,
3956 struct ResolverHandle *rh,
3958 const struct GNUNET_NAMESTORE_RecordData *rd)
3960 struct NameShortenHandle *nsh;
3961 char result[MAX_DNS_NAME_LENGTH];
3963 nsh = (struct NameShortenHandle *)cls;
3964 rh->namestore_task = NULL;
3966 * At this point rh->name contains the part of the name
3967 * that we do not have a PKEY in our namestore to resolve.
3968 * The authority chain in the resolver handle is now
3969 * useful to backtrack if needed
3972 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3973 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3974 memset(result, 0, sizeof (result));
3976 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3980 * This is our zone append .gads unless name is empty
3981 * (it shouldn't be, usually FIXME what happens if we
3982 * shorten to our zone to a "" record??)
3985 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3986 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3987 "Our zone: Found %s as shorten result\n", result);
3989 if (strlen (nsh->result) > strlen (result))
3990 strcpy (nsh->result, result);
3993 else if (NULL != nsh->private_zone)
3996 * This is our zone append .gads unless name is empty
3997 * (it shouldn't be, usually FIXME what happens if we
3998 * shorten to our zone to a "" record??)
4000 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4001 nsh->private_zone) == 0)
4004 sprintf (result, "%s.%s.%s",
4005 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4006 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4007 "Our zone: Found %s as shorten result in private zone %s\n",
4010 if (strlen (nsh->result) > strlen (result))
4011 strcpy (nsh->result, result);
4014 else if (NULL != nsh->shorten_zone)
4017 * This is our zone append .gads unless name is empty
4018 * (it shouldn't be, usually FIXME what happens if we
4019 * shorten to our zone to a "" record??)
4021 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4022 nsh->shorten_zone) == 0)
4024 sprintf (result, "%s.%s.%s",
4025 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4026 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4027 "Our zone: Found %s as shorten result in shorten zone\n",
4030 if (strlen (nsh->result) > strlen (result))
4031 strcpy (nsh->result, result);
4036 /* backtrack authorities for names */
4037 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4039 &rh->authority_chain_head->zone,
4040 &process_zone_to_name_shorten_root,
4047 * Callback calles by namestore for a zone to name
4050 * @param cls the closure
4051 * @param zone_key the zone we queried
4052 * @param expire the expiration time of the name
4053 * @param name the name found or NULL
4054 * @param rd_len number of records for the name
4055 * @param rd the record data (PKEY) for the name
4056 * @param signature the signature for the record data
4059 process_zone_to_name_zkey(void *cls,
4060 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4061 struct GNUNET_TIME_Absolute expire,
4063 unsigned int rd_len,
4064 const struct GNUNET_NAMESTORE_RecordData *rd,
4065 const struct GNUNET_CRYPTO_RsaSignature *signature)
4067 struct ResolverHandle *rh = cls;
4068 struct NameShortenHandle *nsh = rh->proc_cls;
4069 char new_name[MAX_DNS_NAME_LENGTH];
4071 rh->namestore_task = NULL;
4073 /* zkey not in our zone */
4077 * In this case we have not given this PKEY a name (yet)
4078 * It is either just not in our zone or not even cached
4079 * Since we do not know at this point we will not try to shorten
4080 * because PKEY import will happen if the user follows the zkey
4083 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4084 "No name found for zkey %s returning verbatim!\n", nsh->result);
4085 /*if (strcmp(rh->name, "") != 0)
4086 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4087 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4089 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4090 enc, GNUNET_GNS_TLD_ZKEY);
4092 strcpy (nsh->result, new_name);*/
4094 finish_shorten (rh, nsh);
4098 if (strcmp(rh->name, "") != 0)
4099 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4102 strcpy(new_name, name);
4104 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4105 "Continue shorten for %s!\n", new_name);
4107 strcpy(rh->name, new_name);
4109 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4110 rh->authority_chain_tail = rh->authority_chain_head;
4111 rh->authority_chain_head->zone = rh->authority;
4114 /* Start delegation resolution in our namestore */
4115 resolve_delegation_ns (rh);
4120 * Shorten api from resolver
4122 * @param zone the root zone to use
4123 * @param pzone the private zone to use
4124 * @param szone the shorten zone to use
4125 * @param name the name to shorten
4126 * @param private_zone_name name of the private zone
4127 * @param shorten_zone_name name of the shorten zone
4128 * @param proc the processor to call with result
4129 * @param proc_cls closure to pass to proc
4132 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4133 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4134 struct GNUNET_CRYPTO_ShortHashCode *szone,
4136 const char* private_zone_name,
4137 const char* shorten_zone_name,
4138 ShortenResultProcessor proc,
4141 struct ResolverHandle *rh;
4142 struct NameShortenHandle *nsh;
4143 char string_hash[MAX_DNS_LABEL_LENGTH];
4144 struct GNUNET_CRYPTO_ShortHashCode zkey;
4145 char nzkey[MAX_DNS_LABEL_LENGTH];
4146 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_gnunet_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);
4275 * Process result from namestore delegation lookup
4276 * for get authority operation
4278 * @param cls the client get auth handle
4279 * @param rh the resolver handle
4280 * @param rd_count number of results (0)
4281 * @param rd data (NULL)
4284 handle_delegation_result_ns_get_auth(void* cls,
4285 struct ResolverHandle *rh,
4287 const struct GNUNET_NAMESTORE_RecordData *rd)
4289 struct GetNameAuthorityHandle* nah = rh->proc_cls;
4293 * At this point rh->name contains the part of the name
4294 * that we do not have a PKEY in our namestore to resolve.
4295 * The authority chain in the resolver handle is now
4296 * useful to backtrack if needed
4299 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4300 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4302 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4303 "Building response!\n");
4304 if (is_canonical (rh->name) == GNUNET_YES)
4307 * We successfully resolved the authority in the ns
4308 * FIXME for our purposes this is fine
4309 * but maybe we want to have an api that also looks
4310 * into the dht (i.e. option in message)
4312 if (strlen(rh->name) > strlen(nah->name))
4314 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4315 "Record name longer than original lookup name... odd!\n");
4319 answer_len = strlen(nah->name) - strlen(rh->name)
4320 + strlen(GNUNET_GNS_TLD) + 1;
4321 memset(nah->result, 0, answer_len);
4322 if (0 != strcmp (rh->name, ""))
4323 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4325 strcpy(nah->result, nah->name);
4327 finish_get_auth (rh, nah);
4331 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4332 "Unable to resolve authority for remaining %s!\n", rh->name);
4333 strcpy(nah->result, "");
4334 finish_get_auth (rh, nah);
4342 * Tries to resolve the authority for name
4345 * @param zone the root zone to look up for
4346 * @param pzone the private local zone
4347 * @param name the name to lookup up
4348 * @param proc the processor to call when finished
4349 * @param proc_cls the closure to pass to the processor
4352 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4353 struct GNUNET_CRYPTO_ShortHashCode pzone,
4355 GetAuthorityResultProcessor proc,
4358 struct ResolverHandle *rh;
4359 struct GetNameAuthorityHandle *nah;
4361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4362 "Starting authority resolution for %s!\n", name);
4364 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4365 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4366 rh->authority = zone;
4368 rh->private_local_zone = pzone;
4369 rh->namestore_task = NULL;
4371 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4373 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4375 strcpy(rh->name, "\0");
4380 strlen(name)-strlen(GNUNET_GNS_TLD));
4381 memcpy(rh->name, name,
4382 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4385 memset(nah->name, 0,
4387 strcpy(nah->name, name);
4389 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4390 rh->authority_chain_tail = rh->authority_chain_head;
4391 rh->authority_chain_head->zone = zone;
4392 rh->proc = &handle_delegation_result_ns_get_auth;
4393 rh->proc_cls = (void*)nah;
4396 nah->proc_cls = proc_cls;
4397 strcpy (nah->result, "");
4399 /* Start delegation resolution in our namestore */
4400 resolve_delegation_ns(rh);
4404 /******** END GET AUTHORITY *************/
4406 /* end of gnunet-service-gns_resolver.c */