2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file gns/gnunet-service-gns_resolver.c
23 * @brief GNUnet GNS resolver logic
24 * @author Martin Schanzenbach
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_vpn_service.h"
33 #include "gnunet_dns_service.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "gns_protocol.h"
37 #include "gnunet_gns_service.h"
38 #include "gns_common.h"
39 #include "block_gns.h"
41 #include "gnunet-service-gns_resolver.h"
46 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
49 * DHT replication level
51 #define DHT_GNS_REPLICATION_LEVEL 5
54 * Maximum label length of DNS names
56 #define MAX_DNS_LABEL_LENGTH 63
60 * Our handle to the namestore service
62 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
65 * Our handle to the vpn service
67 static struct GNUNET_VPN_Handle *vpn_handle;
70 * Resolver handle to the dht
72 static struct GNUNET_DHT_Handle *dht_handle;
75 * Heap for parallel DHT lookups
77 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
80 * Heap for namestore queues
82 static struct GNUNET_CONTAINER_Heap *ns_task_heap;
85 * Maximum amount of parallel queries in background
87 static unsigned long long max_allowed_background_queries;
90 * Maximum amount of parallel namestore tasks in background
92 static unsigned long long max_allowed_ns_tasks;
95 * Wheather or not to ignore pending records
97 static int ignore_pending_records;
102 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
105 * Background shortening handles
107 static struct GetPseuAuthorityHandle *gph_head;
110 * Background shortening handles
112 static struct GetPseuAuthorityHandle *gph_tail;
115 * Resolver lookup list
117 static struct ResolverHandle *rlh_head;
120 * Resolver lookup list
122 static struct ResolverHandle *rlh_tail;
125 * Resolver shorten list
127 static struct ResolverHandle *nsh_head;
130 * Resolver shorten list
132 static struct ResolverHandle *nsh_tail;
135 * Resolver get auth list
137 static struct ResolverHandle *nah_head;
140 * Resolver get auth list
142 static struct ResolverHandle *nah_tail;
145 * Global configuration.
147 static const struct GNUNET_CONFIGURATION_Handle *cfg;
150 * a resolution identifier pool variable
151 * This is a non critical identifier useful for debugging
153 static unsigned long long rid = 0;
156 * Check if name is in srv format (_x._y.xxx)
159 * @return GNUNET_YES if true
165 int ret = GNUNET_YES;
169 if (NULL == strstr (name, "._"))
172 ndup = GNUNET_strdup (name);
175 if (NULL == strtok (NULL, "."))
178 if (NULL == strtok (NULL, "."))
181 if (NULL != strtok (NULL, "."))
191 * Determine if this name is canonical (is a legal name in a zone, without delegation);
192 * note that we do not test that the name does not contain illegal characters, we only
193 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
194 * even though they consist of multiple labels.
197 * a.b.gads = not canonical
199 * _foo._srv = canonical
200 * _f.bar = not canonical
202 * @param name the name to test
203 * @return GNUNET_YES if canonical
206 is_canonical (const char* name)
211 if (NULL == strchr (name, '.'))
216 while (NULL != (dot = strchr (pos, '.')))
226 free_get_pseu_authority_handle (struct GetPseuAuthorityHandle *gph)
228 gph->namestore_task = NULL;
229 GNUNET_free (gph->auth);
230 GNUNET_CRYPTO_rsa_key_free (gph->key);
231 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
237 * Callback that shortens authorities
239 * @param gph the handle containing the name to shorten
242 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
246 * Continueation for pkey record creation (shorten)
248 * @param cls a GetPseuAuthorityHandle
249 * @param success unused
253 create_pkey_cont (void* cls, int32_t success, const char* emsg)
255 //FIXME do sth with error
256 struct GetPseuAuthorityHandle* gph = cls;
258 free_get_pseu_authority_handle (gph);
263 * Namestore calls this function if we have record for this name.
264 * (or with rd_count=0 to indicate no matches)
266 * @param cls the pending query
267 * @param key the key of the zone we did the lookup
268 * @param expiration expiration date of the namestore entry
269 * @param name the name for which we need an authority
270 * @param rd_count the number of records with 'name'
271 * @param rd the record data
272 * @param signature the signature of the authority for the record data
275 process_pseu_lookup_ns (void* cls,
276 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
277 struct GNUNET_TIME_Absolute expiration,
278 const char *name, unsigned int rd_count,
279 const struct GNUNET_NAMESTORE_RecordData *rd,
280 const struct GNUNET_CRYPTO_RsaSignature *signature)
282 struct GetPseuAuthorityHandle* gph = cls;
283 struct GNUNET_NAMESTORE_RecordData new_pkey;
285 gph->namestore_task = NULL;
288 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
289 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
290 free_get_pseu_authority_handle (gph);
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
298 new_pkey.expiration_time = UINT64_MAX;
299 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
300 new_pkey.data = &gph->auth->zone;
301 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
302 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
303 | GNUNET_NAMESTORE_RF_PRIVATE
304 | GNUNET_NAMESTORE_RF_PENDING;
305 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
314 * process result of a dht pseu lookup
316 * @param gph the handle
317 * @param name the pseu result or NULL
320 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 "GNS_AUTO_PSEU: No PSEU, no shorten. Finished.\n");
326 free_get_pseu_authority_handle (gph);
330 memcpy (gph->test_name, name, strlen(name) + 1);
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
335 * Check for collision
337 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
340 GNUNET_NAMESTORE_TYPE_ANY,
341 &process_pseu_lookup_ns,
347 * Handle timeout for dht request
349 * @param cls the request handle as closure
350 * @param tc the task context
353 handle_auth_discovery_timeout (void *cls,
354 const struct GNUNET_SCHEDULER_TaskContext *tc)
356 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
360 GNUNET_DHT_get_stop (gph->get_handle);
361 gph->get_handle = NULL;
362 process_pseu_result (gph, NULL);
367 * Function called when we find a PSEU entry in the DHT
369 * @param cls the request handle
370 * @param exp lifetime
371 * @param key the key the record was stored under
372 * @param get_path get path
373 * @param get_path_length get path length
374 * @param put_path put path
375 * @param put_path_length put path length
376 * @param type the block type
377 * @param size the size of the record
378 * @param data the record data
381 process_auth_discovery_dht_result (void* cls,
382 struct GNUNET_TIME_Absolute exp,
383 const struct GNUNET_HashCode * key,
384 const struct GNUNET_PeerIdentity *get_path,
385 unsigned int get_path_length,
386 const struct GNUNET_PeerIdentity *put_path,
387 unsigned int put_path_length,
388 enum GNUNET_BLOCK_Type type,
392 struct GetPseuAuthorityHandle* gph = cls;
393 struct GNSNameRecordBlock *nrb;
394 const char* rd_data = data;
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
403 /* stop lookup and timeout task */
404 GNUNET_DHT_get_stop (gph->get_handle);
405 gph->get_handle = NULL;
406 GNUNET_SCHEDULER_cancel (gph->timeout);
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
411 "GNS_GET_AUTH: got dht result null!\n", size);
412 free_get_pseu_authority_handle (gph);
416 nrb = (struct GNSNameRecordBlock*)data;
417 name = (char*)&nrb[1];
418 num_records = ntohl (nrb->rd_count);
420 struct GNUNET_NAMESTORE_RecordData rd[num_records];
422 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
423 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
425 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
430 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
431 "GNS_GET_AUTH: Error deserializing data!\n");
435 for (i=0; i < num_records; i++)
437 if ((strcmp (name, "+") == 0) &&
438 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
441 process_pseu_result (gph, (char*)rd[i].data);
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 "GNS_GET_AUTH: finished shorten, no results!\n");
449 process_pseu_result (gph, NULL);
454 * Process PSEU discovery for shorten via namestore
456 * @param cls the GetPseuAuthorityHandle
457 * @param key the public key
458 * @param expiration recorddata expiration
459 * @param name the looked up name
460 * @param rd_count number of records in set
461 * @param rd record data
462 * @param signature the signature
465 process_auth_discovery_ns_result (void* cls,
466 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
467 struct GNUNET_TIME_Absolute expiration,
469 unsigned int rd_count,
470 const struct GNUNET_NAMESTORE_RecordData *rd,
471 const struct GNUNET_CRYPTO_RsaSignature *signature)
473 struct GetPseuAuthorityHandle* gph = cls;
474 struct GNUNET_HashCode lookup_key;
478 gph->namestore_task = NULL;
482 GNUNET_GNS_get_key_for_record (GNUNET_GNS_TLD_PLUS, &gph->auth->zone, &lookup_key);
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
486 GNUNET_h2s (&lookup_key));
488 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
489 &handle_auth_discovery_timeout, gph);
491 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
493 GNUNET_assert (gph->get_handle == NULL);
495 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
496 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
498 DHT_GNS_REPLICATION_LEVEL,
499 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
502 &process_auth_discovery_dht_result,
507 for (i=0; i < rd_count; i++)
509 if (0 != (strcmp (name, GNUNET_GNS_TLD_PLUS)))
512 if (rd[i].record_type != GNUNET_GNS_RECORD_PSEU)
516 process_pseu_result (gph, (char*)rd[i].data);
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
521 process_pseu_result (gph, NULL);
526 * Callback called by namestore for a zone to name
529 * @param cls the closure
530 * @param zone_key the zone we queried
531 * @param expire the expiration time of the name
532 * @param name the name found or NULL
533 * @param rd_len number of records for the name
534 * @param rd the record data (PKEY) for the name
535 * @param signature the signature for the record data
538 process_zone_to_name_discover (void *cls,
539 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
540 struct GNUNET_TIME_Absolute expire,
543 const struct GNUNET_NAMESTORE_RecordData *rd,
544 const struct GNUNET_CRYPTO_RsaSignature *signature)
546 struct GetPseuAuthorityHandle* gph = cls;
548 gph->namestore_task = NULL;
551 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
554 GNUNET_GNS_RECORD_PSEU,
555 &process_auth_discovery_ns_result,
559 /* we found a match in our own zone */
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
562 free_get_pseu_authority_handle (gph);
567 * Callback that shortens authorities
569 * @param gph the handle to the shorten request
572 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576 "GNS_AUTO_PSEU: New authority %s discovered\n",
579 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
582 &process_zone_to_name_discover,
589 * Start shortening algorithm using auth as
592 * @param auth the authorities that were resolved
593 * @param key the private key for PKEY import
596 start_shorten (struct AuthorityChain *auth,
597 const struct GNUNET_CRYPTO_RsaPrivateKey *key)
599 struct GetPseuAuthorityHandle *gph;
600 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
601 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
603 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
604 if (NULL == (pb_key = GNUNET_CRYPTO_rsa_encode_key (key)))
606 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
607 "Failed to encode RSA key on shorten\n");
610 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
611 gph->key = GNUNET_CRYPTO_rsa_decode_key ((const char*) pb_key, ntohs (pb_key->len));
612 GNUNET_free (pb_key);
613 if (NULL == gph->key)
615 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
616 "Failed to decode RSA key on shorten\n");
620 GNUNET_CRYPTO_short_hash (&pkey,
621 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
623 gph->auth = GNUNET_malloc (sizeof (struct AuthorityChain));
624 memcpy (gph->auth, auth, sizeof (struct AuthorityChain));
625 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
626 shorten_authority_chain (gph);
630 * Initialize the resolver
632 * @param nh the namestore handle
633 * @param dh the dht handle
634 * @param lz the local zone's hash
635 * @param c configuration handle
636 * @param max_bg_queries maximum number of parallel background queries in dht
637 * @param ignore_pending ignore records that still require user confirmation
639 * @return GNUNET_OK on success
642 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
643 struct GNUNET_DHT_Handle *dh,
644 struct GNUNET_CRYPTO_ShortHashCode lz,
645 const struct GNUNET_CONFIGURATION_Handle *c,
646 unsigned long long max_bg_queries,
651 return GNUNET_SYSERR;
654 namestore_handle = nh;
658 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
660 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
661 max_allowed_background_queries = max_bg_queries;
662 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
663 ignore_pending_records = ignore_pending;
664 GNUNET_RESOLVER_connect (cfg);
672 * @param cls closure to iterator
673 * @param node heap nodes
674 * @param element the namestorebgtask
675 * @param cost heap cost
676 * @return always GNUNET_YES
679 cleanup_pending_ns_tasks (void* cls,
680 struct GNUNET_CONTAINER_HeapNode *node,
682 GNUNET_CONTAINER_HeapCostType cost)
684 struct NamestoreBGTask *nbg = element;
686 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
687 "GNS_CLEANUP: Terminating ns task\n");
688 GNUNET_NAMESTORE_cancel (nbg->qe);
690 GNUNET_CONTAINER_heap_remove_node (node);
698 * @param rh resolver handle
699 * @param rlh record lookup handle
700 * @param rd_count number of results
704 finish_lookup (struct ResolverHandle *rh,
705 struct RecordLookupHandle* rlh,
706 unsigned int rd_count,
707 const struct GNUNET_NAMESTORE_RecordData *rd);
711 * Cleanup background lookups FIXME get rid of this?? YES this doesn't do
712 * anything! => find in code and remove all references to the heap
714 * @param cls closure to iterator
715 * @param node heap nodes
716 * @param element the resolver handle
717 * @param cost heap cost
718 * @return always GNUNET_YES
721 cleanup_pending_background_queries (void* cls,
722 struct GNUNET_CONTAINER_HeapNode *node,
724 GNUNET_CONTAINER_HeapCostType cost)
726 struct ResolverHandle *rh = element;
728 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
729 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
731 GNUNET_CONTAINER_heap_remove_node (node);
732 if (0 == GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
734 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
735 &cleanup_pending_ns_tasks,
743 * Helper function to free resolver handle
745 * @param rh the handle to free
748 free_resolver_handle (struct ResolverHandle* rh)
750 struct AuthorityChain *ac;
751 struct AuthorityChain *ac_next;
756 ac = rh->authority_chain_head;
765 if (NULL != rh->get_handle)
766 GNUNET_DHT_get_stop (rh->get_handle);
768 if (NULL != rh->dns_raw_packet)
769 GNUNET_free (rh->dns_raw_packet);
771 if (NULL != rh->namestore_task)
772 GNUNET_NAMESTORE_cancel (rh->namestore_task);
773 rh->namestore_task = NULL;
775 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
776 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
778 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
779 GNUNET_SCHEDULER_cancel (rh->timeout_task);
781 if (NULL != rh->dns_sock)
782 GNUNET_NETWORK_socket_close (rh->dns_sock);
783 if (NULL != rh->dns_resolver_handle)
784 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
786 if (NULL != rh->rd.data)
787 GNUNET_free ((void*)(rh->rd.data));
795 * @param rh resolver handle
796 * @param nsh name shorten handle
799 finish_shorten (struct ResolverHandle *rh,
800 struct NameShortenHandle *nsh);
806 * @param rh resolver handle
807 * @param nah get name authority handle
810 finish_get_auth (struct ResolverHandle *rh,
811 struct GetNameAuthorityHandle* rlh);
818 gns_resolver_cleanup ()
821 struct GetPseuAuthorityHandle *tmp;
823 while (NULL != (tmp = gph_head))
825 if (tmp->get_handle != NULL)
827 GNUNET_DHT_get_stop (tmp->get_handle);
828 tmp->get_handle = NULL;
830 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
832 GNUNET_SCHEDULER_cancel (tmp->timeout);
833 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
835 if (NULL != tmp->namestore_task)
837 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
838 tmp->namestore_task = NULL;
840 free_get_pseu_authority_handle (tmp);
843 while (NULL != rlh_head)
845 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
847 while (NULL != nsh_head)
849 finish_shorten (nsh_head, nsh_head->proc_cls);
851 while (NULL != nah_head)
853 finish_get_auth (nah_head, nah_head->proc_cls);
856 s = GNUNET_CONTAINER_heap_get_size (dht_lookup_heap);
857 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
858 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
860 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
861 &cleanup_pending_background_queries,
863 else if (0 != GNUNET_CONTAINER_heap_get_size (ns_task_heap))
865 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
866 &cleanup_pending_ns_tasks,
873 * Callback when record data is put into namestore
875 * @param cls the closure
876 * @param success GNUNET_OK on success
877 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
880 on_namestore_record_put_result (void *cls,
884 struct NamestoreBGTask *nbg = cls;
886 GNUNET_CONTAINER_heap_remove_node (nbg->node);
889 if (GNUNET_NO == success)
891 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
892 "GNS_NS: records already in namestore\n");
895 else if (GNUNET_YES == success)
897 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
898 "GNS_NS: records successfully put in namestore\n");
902 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
903 "GNS_NS: Error putting records into namestore: %s\n", emsg);
908 * Lookup timeout task
910 * @param cls the ResolverHandle for the task that timed out
911 * @param tc the task context
914 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
916 struct ResolverHandle *rh = cls;
918 if (NULL != rh->timeout_cont)
919 rh->timeout_cont (rh->timeout_cont_cls, tc);
924 * Processor for background lookups in the DHT
926 * @param cls closure (NULL)
927 * @param rd_count number of records found (not 0)
928 * @param rd record data
931 background_lookup_result_processor (void *cls,
933 const struct GNUNET_NAMESTORE_RecordData *rd)
935 //We could do sth verbose/more useful here but it doesn't make any difference
936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
937 "GNS_BG: background dht lookup finished. (%d results)\n",
943 * Handle timeout for DHT requests
945 * @param cls the request handle as closure
946 * @param tc the task context
949 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
951 struct ResolverHandle *rh = cls;
952 struct RecordLookupHandle *rlh = rh->proc_cls;
953 char new_name[MAX_DNS_NAME_LENGTH];
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
956 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
957 rh->id, rh->name, rh->timeout.rel_value);
959 * Start resolution in bg
961 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
962 rh->name, GNUNET_GNS_TLD);
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
966 rh->id, new_name, rlh->record_type);
968 gns_resolver_lookup_record (rh->authority,
969 rh->private_local_zone,
973 GNUNET_TIME_UNIT_FOREVER_REL,
975 &background_lookup_result_processor,
978 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
980 GNUNET_DHT_get_stop (rh->get_handle);
981 rh->get_handle = NULL;
982 rh->proc (rh->proc_cls, rh, 0, NULL);
987 * Function called when we get a result from the dht
988 * for our record query
990 * @param cls the request handle
991 * @param exp lifetime
992 * @param key the key the record was stored under
993 * @param get_path get path
994 * @param get_path_length get path length
995 * @param put_path put path
996 * @param put_path_length put path length
997 * @param type the block type
998 * @param size the size of the record
999 * @param data the record data
1002 process_record_result_dht (void* cls,
1003 struct GNUNET_TIME_Absolute exp,
1004 const struct GNUNET_HashCode * key,
1005 const struct GNUNET_PeerIdentity *get_path,
1006 unsigned int get_path_length,
1007 const struct GNUNET_PeerIdentity *put_path,
1008 unsigned int put_path_length,
1009 enum GNUNET_BLOCK_Type type,
1010 size_t size, const void *data)
1012 struct ResolverHandle *rh;
1013 struct RecordLookupHandle *rlh;
1014 struct GNSNameRecordBlock *nrb;
1015 uint32_t num_records;
1017 char* rd_data = (char*)data;
1021 rh = (struct ResolverHandle *)cls;
1022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1023 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
1025 rlh = (struct RecordLookupHandle *) rh->proc_cls;
1026 nrb = (struct GNSNameRecordBlock*)data;
1028 /* stop lookup and timeout task */
1029 GNUNET_DHT_get_stop (rh->get_handle);
1030 rh->get_handle = NULL;
1032 if (rh->dht_heap_node != NULL)
1034 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1035 rh->dht_heap_node = NULL;
1038 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1040 GNUNET_SCHEDULER_cancel (rh->timeout_task);
1041 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1044 rh->get_handle = NULL;
1045 name = (char*)&nrb[1];
1046 num_records = ntohl (nrb->rd_count);
1048 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1049 struct NamestoreBGTask *ns_heap_root;
1050 struct NamestoreBGTask *namestore_bg_task;
1052 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
1053 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
1055 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1060 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1061 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1062 rh->proc (rh->proc_cls, rh, 0, NULL);
1066 for (i = 0; i < num_records; i++)
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1070 rh->id, name, rh->name);
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1073 rh->id, rd[i].record_type, rlh->record_type);
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1075 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1076 rh->id, rd[i].data_size);
1077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1078 "GNS_PHASE_REC-%llu: Got flag %d\n",
1079 rh->id, rd[i].flags);
1081 if ((strcmp (name, rh->name) == 0) &&
1082 (rd[i].record_type == rlh->record_type))
1088 * FIXME check pubkey against existing key in namestore?
1089 * https://gnunet.org/bugs/view.php?id=2179
1091 if (max_allowed_ns_tasks <=
1092 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1094 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1095 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1098 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1102 /* Save to namestore */
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 "GNS_PHASE_REC-%llu: Caching record for %s\n",
1106 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1107 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1114 &on_namestore_record_put_result, //cont
1117 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1119 GNUNET_TIME_absolute_get().abs_value);
1122 if (0 < rh->answered)
1123 rh->proc (rh->proc_cls, rh, num_records, rd);
1125 rh->proc (rh->proc_cls, rh, 0, NULL);
1132 * Start DHT lookup for a (name -> query->record_type) record in
1133 * rh->authority's zone
1135 * @param rh the pending gns query context
1138 resolve_record_dht (struct ResolverHandle *rh)
1141 struct GNUNET_HashCode lookup_key;
1142 struct RecordLookupHandle *rlh = rh->proc_cls;
1143 struct ResolverHandle *rh_heap_root;
1145 GNUNET_GNS_get_key_for_record (rh->name, &rh->authority, &lookup_key);
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1148 rh->id, rh->name, GNUNET_h2s (&lookup_key));
1150 rh->dht_heap_node = NULL;
1152 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
1155 * Update timeout if necessary
1157 if (GNUNET_SCHEDULER_NO_TASK == rh->timeout_task)
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 "GNS_PHASE_REC-%llu: Adjusting timeout\n", rh->id);
1162 * Set timeout for authority lookup phase to 1/2
1164 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1165 GNUNET_TIME_relative_divide (rh->timeout, 2),
1166 &handle_lookup_timeout,
1169 rh->timeout_cont = &dht_lookup_timeout;
1170 rh->timeout_cont_cls = rh;
1174 if (max_allowed_background_queries <=
1175 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1177 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1178 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1179 rh_heap_root->get_handle = NULL;
1180 rh_heap_root->dht_heap_node = NULL;
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1184 rh->id, rh_heap_root->name);
1185 rh_heap_root->proc (rh_heap_root->proc_cls,
1190 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1192 GNUNET_TIME_absolute_get ().abs_value);
1195 xquery = htonl (rlh->record_type);
1197 GNUNET_assert (rh->get_handle == NULL);
1198 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1199 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1201 DHT_GNS_REPLICATION_LEVEL,
1202 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1205 &process_record_result_dht,
1212 * Namestore calls this function if we have record for this name.
1213 * (or with rd_count=0 to indicate no matches)
1215 * @param cls the pending query
1216 * @param key the key of the zone we did the lookup
1217 * @param expiration expiration date of the namestore entry
1218 * @param name the name for which we need an authority
1219 * @param rd_count the number of records with 'name'
1220 * @param rd the record data
1221 * @param signature the signature of the authority for the record data
1224 process_record_result_ns (void* cls,
1225 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1226 struct GNUNET_TIME_Absolute expiration,
1227 const char *name, unsigned int rd_count,
1228 const struct GNUNET_NAMESTORE_RecordData *rd,
1229 const struct GNUNET_CRYPTO_RsaSignature *signature)
1231 struct ResolverHandle *rh;
1232 struct RecordLookupHandle *rlh;
1233 struct GNUNET_TIME_Relative remaining_time;
1234 struct GNUNET_CRYPTO_ShortHashCode zone;
1235 struct GNUNET_TIME_Absolute et;
1241 rh->namestore_task = NULL;
1242 GNUNET_CRYPTO_short_hash (key,
1243 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1245 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1253 rh->status |= RSL_RECORD_EXISTS;
1255 if (remaining_time.rel_value == 0)
1256 rh->status |= RSL_RECORD_EXPIRED;
1262 * Lookup terminated and no results
1264 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1265 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1268 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1269 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1272 * Our zone and no result? Cannot resolve TT
1274 rh->proc(rh->proc_cls, rh, 0, NULL);
1278 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1279 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1281 for (i = 0; i < rd_count;i++)
1283 if (rd[i].record_type != rlh->record_type)
1286 if (ignore_pending_records &&
1287 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1290 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1295 //FIXME: eh? do I have to handle this here?
1296 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1297 et.abs_value = rd[i].expiration_time;
1298 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
1300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1301 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1311 if (0 == rh->answered)
1313 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1314 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1315 rh->proc(rh->proc_cls, rh, 0, NULL);
1319 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1320 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1321 rh->id, rh->answered, rd_count);
1322 rh->proc(rh->proc_cls, rh, rd_count, rd);
1327 * VPN redirect result callback
1329 * @param cls the resolver handle
1330 * @param af the requested address family
1331 * @param address in_addr(6) respectively
1334 process_record_result_vpn (void* cls, int af, const void *address)
1336 struct ResolverHandle *rh = cls;
1337 struct RecordLookupHandle *rlh = rh->proc_cls;
1338 struct GNUNET_NAMESTORE_RecordData rd;
1340 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1341 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1345 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1346 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1348 if (GNUNET_GNS_RECORD_A != rlh->record_type)
1350 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1351 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1353 rh->proc (rh->proc_cls, rh, 0, NULL);
1356 rd.record_type = GNUNET_GNS_RECORD_A;
1357 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1359 rd.data_size = sizeof (struct in_addr);
1361 rh->proc (rh->proc_cls, rh, 1, &rd);
1364 else if (AF_INET6 == af)
1366 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1367 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1369 if (GNUNET_GNS_RECORD_AAAA != rlh->record_type)
1371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1372 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1374 rh->proc (rh->proc_cls, rh, 0, NULL);
1377 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1378 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1380 rd.data_size = sizeof (struct in6_addr);
1382 rh->proc (rh->proc_cls, rh, 1, &rd);
1386 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1387 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1389 rh->proc (rh->proc_cls, rh, 0, NULL);
1394 * Process VPN lookup result for record
1396 * @param cls the record lookup handle
1397 * @param rh resolver handle
1398 * @param rd_count number of results (1)
1399 * @param rd record data containing the result
1402 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1403 unsigned int rd_count,
1404 const struct GNUNET_NAMESTORE_RecordData *rd)
1406 struct RecordLookupHandle* rlh = cls;
1410 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1411 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1414 /* give up, cannot resolve */
1415 finish_lookup(rh, rlh, 0, NULL);
1419 /* results found yay */
1420 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1421 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1423 finish_lookup(rh, rlh, rd_count, rd);
1428 * Sends a UDP dns query to a nameserver specified in the rh
1430 * @param rh the resolver handle
1433 send_dns_packet (struct ResolverHandle *rh);
1439 * @param cls the ResolverHandle for this lookup
1440 * @param addr the sockaddr
1441 * @param addrlen the socket address length
1444 handle_dns_resolver (void *cls,
1445 const struct sockaddr *addr,
1448 struct ResolverHandle *rh = cls;
1449 struct RecordLookupHandle *rlh = rh->proc_cls;
1450 struct GNUNET_NAMESTORE_RecordData rd;
1451 struct sockaddr_in *sai;
1452 struct sockaddr_in6 *sai6;
1456 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1457 "No address found in DNS!\n");
1458 finish_lookup (rh, rlh, 0, NULL);
1462 if (sizeof (struct sockaddr_in) == addrlen)
1464 sai = (struct sockaddr_in*) addr;
1465 rd.record_type = GNUNET_GNS_RECORD_A;
1466 rd.data_size = sizeof (struct in_addr);
1467 rd.data = &sai->sin_addr;
1469 else if (sizeof (struct sockaddr_in6) == addrlen)
1471 sai6 = (struct sockaddr_in6*) addr;
1472 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1473 rd.data_size = sizeof (struct in6_addr);
1474 rd.data = &sai6->sin6_addr;
1478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1479 "Address length is garbage!\n");
1480 finish_lookup (rh, rlh, 0, NULL);
1483 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1485 finish_lookup (rh, rlh, 1, &rd);
1490 * Resolve DNS name via local stub resolver
1492 * @param rh the resolver handle
1495 resolve_dns_name (struct ResolverHandle *rh)
1497 struct RecordLookupHandle *rlh = rh->proc_cls;
1500 if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1501 (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 "Can only resolve A/AAAA via stub... abort\n");
1505 finish_lookup (rh, rlh, 0, NULL);
1509 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1514 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1517 &handle_dns_resolver,
1523 * Read DNS udp packet from socket
1525 * @param cls the resolver handle
1526 * @param tc task context
1529 read_dns_response (void *cls,
1530 const struct GNUNET_SCHEDULER_TaskContext *tc)
1532 struct ResolverHandle *rh = cls;
1533 struct RecordLookupHandle *rlh = rh->proc_cls;
1534 char buf[UINT16_MAX];
1536 struct sockaddr_in addr;
1538 struct GNUNET_DNSPARSER_Packet *packet;
1539 struct GNUNET_NAMESTORE_RecordData rd;
1540 int found_delegation = GNUNET_NO;
1541 int found_cname = GNUNET_NO;
1542 char* delegation_name = NULL;
1543 int zone_offset = 0;
1546 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1547 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1549 /* timeout or shutdown */
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1551 "Terminating DNS query %d\n", tc->reason);
1552 finish_lookup (rh, rlh, 0, NULL);
1556 addrlen = sizeof (addr);
1557 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1559 (struct sockaddr*) &addr,
1564 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1565 finish_lookup (rh, rlh, 0, NULL);
1569 packet = GNUNET_DNSPARSER_parse (buf, r);
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1574 "Failed to parse DNS reply!\n");
1575 finish_lookup (rh, rlh, 0, NULL);
1579 for (i = 0; i < packet->num_answers; i++)
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1582 "Got record type %d (want %d)\n",
1583 packet->answers[i].type,
1585 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1586 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1589 "CNAME... restarting query with %s\n",
1590 packet->answers[i].data.hostname
1592 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1593 found_cname = GNUNET_YES;
1597 if ((packet->answers[i].type == rlh->record_type) &&
1598 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 rd.data = packet->answers[i].data.raw.data;
1603 rd.data_size = packet->answers[i].data.raw.data_len;
1604 rd.record_type = packet->answers[i].type;
1606 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1607 finish_lookup (rh, rlh, 1, &rd);
1608 GNUNET_DNSPARSER_free_packet (packet);
1613 if (GNUNET_YES == found_cname)
1615 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1617 if (0 > zone_offset)
1620 /* restart query with CNAME */
1621 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1624 "Asking same server for %s\n", rh->dns_name);
1625 send_dns_packet (rh);
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1630 "Trying system resolver for %s\n", rh->dns_name);
1631 resolve_dns_name (rh);
1634 GNUNET_DNSPARSER_free_packet (packet);
1638 for (i = 0; i < packet->num_authority_records; i++)
1641 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1644 "Found NS delegation!\n");
1645 found_delegation = GNUNET_YES;
1646 delegation_name = packet->authority_records[i].data.hostname;
1651 for (i = 0; i < packet->num_additional_records; i++)
1653 if (GNUNET_NO == found_delegation)
1656 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1657 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1659 GNUNET_assert (sizeof (struct in_addr) ==
1660 packet->authority_records[i].data.raw.data_len);
1662 rh->dns_addr.sin_addr =
1663 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1664 send_dns_packet (rh);
1665 GNUNET_DNSPARSER_free_packet (packet);
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1671 "Nothing useful in DNS reply!\n");
1672 finish_lookup (rh, rlh, 0, NULL);
1673 GNUNET_DNSPARSER_free_packet (packet);
1679 * Sends a UDP dns query to a nameserver specified in the rh
1681 * @param rh the request handle
1684 send_dns_packet (struct ResolverHandle *rh)
1686 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1687 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1690 "Sending %dbyte DNS query\n",
1691 rh->dns_raw_packet_size);
1693 if (GNUNET_SYSERR ==
1694 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1696 rh->dns_raw_packet_size,
1697 (struct sockaddr*)&rh->dns_addr,
1698 sizeof (struct sockaddr_in)))
1699 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1700 _("Failed to send DNS request to %s\n"),
1701 GNUNET_a2s ((const struct sockaddr *)&rh->dns_addr,
1702 sizeof (struct sockaddr_in)));
1704 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1705 rh->timeout, //FIXME less?
1711 GNUNET_NETWORK_fdset_destroy (rset);
1717 * The final phase of resoution.
1718 * We found a NS RR and want to resolve via DNS
1720 * @param rh the pending lookup handle
1721 * @param rd_count length of record data
1722 * @param rd record data containing VPN RR
1725 resolve_record_dns (struct ResolverHandle *rh,
1727 const struct GNUNET_NAMESTORE_RecordData *rd)
1729 struct GNUNET_DNSPARSER_Query query;
1730 struct GNUNET_DNSPARSER_Packet packet;
1731 struct GNUNET_DNSPARSER_Flags flags;
1732 struct in_addr dnsip;
1733 struct sockaddr_in addr;
1734 struct sockaddr *sa;
1736 struct RecordLookupHandle *rlh = rh->proc_cls;
1738 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1739 memset (rh->dns_name, 0, sizeof (rh->dns_name));
1741 /* We cancel here as to not include the ns lookup in the timeout */
1742 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1744 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1745 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1747 /* Start shortening */
1748 if ((NULL != rh->priv_key) &&
1749 (GNUNET_YES == is_canonical (rh->name)))
1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1752 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1754 start_shorten (rh->authority_chain_head,
1758 for (i = 0; i < rd_count; i++)
1760 /* Synthesize dns name */
1761 if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1763 strcpy (rh->dns_zone, (char*)rd[i].data);
1764 if (0 == strcmp (rh->name, ""))
1765 strcpy (rh->dns_name, (char*)rd[i].data);
1767 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1770 if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1771 /* need to use memcpy as .data may be unaligned */
1772 memcpy (&dnsip, rd[i].data, sizeof (dnsip));
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1780 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1781 if (NULL == rh->dns_sock)
1783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1784 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1786 finish_lookup (rh, rlh, 0, NULL);
1790 memset (&addr, 0, sizeof (struct sockaddr_in));
1791 sa = (struct sockaddr *) &addr;
1792 sa->sa_family = AF_INET;
1793 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1795 sizeof (struct sockaddr_in)))
1797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1798 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1800 finish_lookup (rh, rlh, 0, NULL);
1803 query.name = rh->dns_name;
1804 query.type = rlh->record_type;
1805 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1806 memset (&flags, 0, sizeof (flags));
1807 flags.recursion_desired = 1;
1808 flags.checking_disabled = 1;
1809 packet.queries = &query;
1810 packet.answers = NULL;
1811 packet.authority_records = NULL;
1812 packet.num_queries = 1;
1813 packet.num_answers = 0;
1814 packet.num_authority_records = 0;
1815 packet.num_additional_records = 0;
1816 packet.flags = flags;
1818 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1820 &rh->dns_raw_packet,
1821 &rh->dns_raw_packet_size))
1823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1824 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1826 GNUNET_NETWORK_socket_close (rh->dns_sock);
1827 finish_lookup (rh, rlh, 0, NULL);
1831 rh->dns_addr.sin_family = AF_INET;
1832 rh->dns_addr.sin_port = htons (53); //domain
1833 rh->dns_addr.sin_addr = dnsip;
1834 #if HAVE_SOCKADDR_IN_SIN_LEN
1835 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1838 send_dns_packet (rh);
1843 * The final phase of resoution.
1844 * We found a VPN RR and want to request an IPv4/6 address
1846 * @param rh the pending lookup handle
1847 * @param rd_count length of record data
1848 * @param rd record data containing VPN RR
1851 resolve_record_vpn (struct ResolverHandle *rh,
1853 const struct GNUNET_NAMESTORE_RecordData *rd)
1855 struct RecordLookupHandle *rlh = rh->proc_cls;
1856 struct GNUNET_HashCode serv_desc;
1857 struct vpn_data* vpn;
1860 /* We cancel here as to not include the ns lookup in the timeout */
1861 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1863 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1864 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1866 /* Start shortening */
1867 if ((NULL != rh->priv_key) &&
1868 (GNUNET_YES == is_canonical (rh->name)))
1870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1871 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1873 start_shorten (rh->authority_chain_head,
1877 vpn = (struct vpn_data*)rd->data;
1880 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1881 strlen ((char*)&vpn[1]) + 1,
1883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1884 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1887 GNUNET_h2s (&vpn->peer));
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1890 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1893 GNUNET_h2s (&serv_desc));
1894 rh->proc = &handle_record_vpn;
1895 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1899 if (NULL == vpn_handle)
1901 vpn_handle = GNUNET_VPN_connect (cfg);
1902 if (NULL == vpn_handle)
1904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1905 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1906 finish_lookup (rh, rh->proc_cls, 0, NULL);
1911 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1912 af, ntohs (vpn->proto),
1913 (struct GNUNET_PeerIdentity *)&vpn->peer,
1916 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1917 &process_record_result_vpn,
1923 * The final phase of resolution.
1924 * rh->name is a name that is canonical and we do not have a delegation.
1925 * Query namestore for this record
1927 * @param rh the pending lookup handle
1930 resolve_record_ns(struct ResolverHandle *rh)
1932 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1934 /* We cancel here as to not include the ns lookup in the timeout */
1935 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1937 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1938 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1940 /* Start shortening */
1941 if ((NULL != rh->priv_key) &&
1942 (GNUNET_YES == is_canonical (rh->name)))
1944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1945 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1947 start_shorten (rh->authority_chain_head,
1952 * Try to resolve this record in our namestore.
1953 * The name to resolve is now in rh->authority_name
1954 * since we tried to resolve it to an authority
1957 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1961 &process_record_result_ns,
1967 * Handle timeout for DHT requests
1969 * @param cls the request handle as closure
1970 * @param tc the task context
1973 dht_authority_lookup_timeout(void *cls,
1974 const struct GNUNET_SCHEDULER_TaskContext *tc)
1976 struct ResolverHandle *rh = cls;
1977 struct RecordLookupHandle *rlh = rh->proc_cls;
1978 char new_name[MAX_DNS_NAME_LENGTH];
1980 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1981 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
1982 rh->id, rh->authority_name, rh->timeout.rel_value);
1984 rh->status |= RSL_TIMED_OUT;
1985 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1986 if (NULL != rh->get_handle)
1987 GNUNET_DHT_get_stop (rh->get_handle);
1989 rh->get_handle = NULL;
1990 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
1992 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1993 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
1995 rh->proc(rh->proc_cls, rh, 0, NULL);
1999 if (0 == strcmp(rh->name, ""))
2002 * promote authority back to name and try to resolve record
2004 strcpy(rh->name, rh->authority_name);
2005 rh->proc(rh->proc_cls, rh, 0, NULL);
2010 * Start resolution in bg
2012 GNUNET_assert (0 < GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
2013 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD));
2015 strcpy(rh->name, new_name);
2017 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2018 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2019 rh->id, rh->name, rlh->record_type);
2021 gns_resolver_lookup_record (rh->authority,
2022 rh->private_local_zone,
2026 GNUNET_TIME_UNIT_FOREVER_REL,
2028 &background_lookup_result_processor,
2030 rh->proc(rh->proc_cls, rh, 0, NULL);
2035 * Start DHT lookup for a name -> PKEY (compare NS) record in
2036 * rh->authority's zone
2038 * @param rh the pending gns query
2040 static void resolve_delegation_dht(struct ResolverHandle *rh);
2044 * Resolve the delegation chain for the request in our namestore
2046 * @param rh the resolver handle
2048 static void resolve_delegation_ns(struct ResolverHandle *rh);
2052 * Namestore resolution for delegation finished. Processing result.
2054 * @param cls the closure
2055 * @param rh resolver handle
2056 * @param rd_count number of results (always 0)
2057 * @param rd record data (always NULL)
2060 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2061 unsigned int rd_count,
2062 const struct GNUNET_NAMESTORE_RecordData *rd);
2066 * This is a callback function that checks for key revocation
2068 * @param cls the pending query
2069 * @param key the key of the zone we did the lookup
2070 * @param expiration expiration date of the record data set in the namestore
2071 * @param name the name for which we need an authority
2072 * @param rd_count the number of records with 'name'
2073 * @param rd the record data
2074 * @param signature the signature of the authority for the record data
2077 process_pkey_revocation_result_ns (void *cls,
2078 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2079 struct GNUNET_TIME_Absolute expiration,
2081 unsigned int rd_count,
2082 const struct GNUNET_NAMESTORE_RecordData *rd,
2083 const struct GNUNET_CRYPTO_RsaSignature *signature)
2085 struct ResolverHandle *rh = cls;
2086 struct GNUNET_TIME_Relative remaining_time;
2089 rh->namestore_task = NULL;
2090 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2092 for (i = 0; i < rd_count; i++)
2094 if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2096 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2097 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2099 rh->status |= RSL_PKEY_REVOKED;
2100 rh->proc (rh->proc_cls, rh, 0, NULL);
2105 if ((NULL == name) ||
2106 (0 == remaining_time.rel_value))
2108 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2109 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2112 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2114 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2115 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2116 rh->id, "+.gads", GNUNET_GNS_RECORD_REV);
2118 gns_resolver_lookup_record(rh->authority,
2119 rh->private_local_zone,
2120 GNUNET_GNS_RECORD_REV,
2123 GNUNET_TIME_UNIT_FOREVER_REL,
2125 &background_lookup_result_processor,
2129 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2130 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2133 * We are done with PKEY resolution if name is empty
2134 * else resolve again with new authority
2136 if (strcmp (rh->name, "") == 0)
2137 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2139 resolve_delegation_ns (rh);
2144 * Callback when record data is put into namestore
2146 * @param cls the closure
2147 * @param success GNUNET_OK on success
2148 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2151 on_namestore_delegation_put_result(void *cls,
2155 struct NamestoreBGTask *nbg = cls;
2157 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2160 if (GNUNET_NO == success)
2162 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2163 "GNS_NS: records already in namestore\n");
2166 else if (GNUNET_YES == success)
2168 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2169 "GNS_NS: records successfully put in namestore\n");
2173 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2174 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2179 * Function called when we get a result from the dht
2180 * for our query. Recursively tries to resolve authorities
2183 * @param cls the request handle
2184 * @param exp lifetime
2185 * @param key the key the record was stored under
2186 * @param get_path get path
2187 * @param get_path_length get path length
2188 * @param put_path put path
2189 * @param put_path_length put path length
2190 * @param type the block type
2191 * @param size the size of the record
2192 * @param data the record data
2195 process_delegation_result_dht(void* cls,
2196 struct GNUNET_TIME_Absolute exp,
2197 const struct GNUNET_HashCode * key,
2198 const struct GNUNET_PeerIdentity *get_path,
2199 unsigned int get_path_length,
2200 const struct GNUNET_PeerIdentity *put_path,
2201 unsigned int put_path_length,
2202 enum GNUNET_BLOCK_Type type,
2203 size_t size, const void *data)
2205 struct ResolverHandle *rh = cls;
2206 struct GNSNameRecordBlock *nrb;
2207 uint32_t num_records;
2209 char* rd_data = (char*) data;
2212 struct GNUNET_CRYPTO_ShortHashCode zone;
2214 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2215 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2220 nrb = (struct GNSNameRecordBlock*)data;
2222 /* stop dht lookup and timeout task */
2223 GNUNET_DHT_get_stop (rh->get_handle);
2224 rh->get_handle = NULL;
2225 if (rh->dht_heap_node != NULL)
2227 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2228 rh->dht_heap_node = NULL;
2231 num_records = ntohl(nrb->rd_count);
2232 name = (char*)&nrb[1];
2234 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2235 struct NamestoreBGTask *ns_heap_root;
2236 struct NamestoreBGTask *namestore_bg_task;
2238 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2239 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2240 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2245 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2246 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2251 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2252 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2253 rh->id, name, rh->authority_name);
2254 for (i=0; i<num_records; i++)
2257 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2258 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2259 rh->id, name, rh->authority_name);
2260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2261 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2262 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2263 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2264 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2265 rh->id, rd[i].data_size);
2266 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2267 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2268 rh->id, rd[i].flags);
2270 if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2271 (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2272 (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2275 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2277 if (0 == strcmp(rh->name, ""))
2278 strcpy(rh->name, rh->authority_name);
2280 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2281 rh->name, rh->authority_name); //FIXME ret
2286 if ((0 == strcmp(name, rh->authority_name)) &&
2287 (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2290 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2293 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2294 struct AuthorityChain *auth =
2295 GNUNET_malloc(sizeof(struct AuthorityChain));
2296 auth->zone = rh->authority;
2297 memset(auth->name, 0, strlen(rh->authority_name)+1);
2298 strcpy(auth->name, rh->authority_name);
2299 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2300 rh->authority_chain_tail,
2303 if (NULL != rh->rd.data)
2304 GNUNET_free ((void*)rh->rd.data);
2306 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2307 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2308 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2311 /** try to import pkey if private key available */
2312 //if (rh->priv_key && is_canonical (rh->name))
2313 // process_discovered_authority(name, auth->zone,
2314 // rh->authority_chain_tail->zone,
2319 GNUNET_GNS_get_zone_from_key (name, key, &zone);
2322 /* Save to namestore
2323 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2326 if (max_allowed_ns_tasks <=
2327 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2329 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2330 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2332 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2333 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background ns task\n",
2336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2337 "GNS_PHASE_DELEGATE_DHT-%llu: Caching record for %s\n",
2339 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2341 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2343 GNUNET_TIME_absolute_get().abs_value);
2344 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2351 &on_namestore_delegation_put_result, //cont
2352 namestore_bg_task); //cls
2356 if (0 != rh->answered)
2361 * FIXME in this case. should we ask namestore again?
2363 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2364 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2365 rh->id, rh->authority_name, rh->name);
2367 if (0 == strcmp(rh->name, ""))
2369 /* Start shortening */
2370 if ((NULL != rh->priv_key) &&
2371 (GNUNET_YES == is_canonical (rh->name)))
2373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2374 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2376 start_shorten (rh->authority_chain_head,
2381 rh->proc = &handle_delegation_ns;
2384 /* Check for key revocation and delegate */
2385 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2388 GNUNET_GNS_RECORD_REV,
2389 &process_pkey_revocation_result_ns,
2396 * No pkey but name exists
2399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2400 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2401 rh->id, rh->authority_name, rh->name);
2402 if (0 == strcmp(rh->name, ""))
2403 strcpy(rh->name, rh->authority_name);
2405 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2406 rh->name, rh->authority_name); //FIXME ret
2408 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2409 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2410 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2411 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2413 rh->proc(rh->proc_cls, rh, 0, NULL);
2416 //FIXME maybe define somewhere else?
2417 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2418 +(MAX_DNS_NAME_LENGTH*2)
2419 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2420 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2424 * Exands a name ending in .+ with the zone of origin.
2425 * FIXME: funky api: 'dest' must be large enough to hold
2426 * the result; this is a bit yucky...
2428 * @param dest destination buffer
2429 * @param src the .+ name
2430 * @param repl the string to replace the + with
2433 expand_plus (char* dest,
2438 size_t s_len = strlen (src) + 1;
2440 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2443 /* no postprocessing */
2444 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2445 "GNS_POSTPROCESS: %s too short\n", src);
2446 memcpy (dest, src, s_len);
2449 if (0 == strcmp (src + s_len - 3, ".+"))
2451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2452 "GNS_POSTPROCESS: Expanding .+ in %s\n",
2454 memset (dest, 0, s_len + strlen (repl) + strlen(GNUNET_GNS_TLD));
2456 pos = dest + s_len - 2;
2458 pos += strlen (repl);
2459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2460 "GNS_POSTPROCESS: Expanded to %s\n",
2465 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2466 "GNS_POSTPROCESS: No postprocessing for %s\n", src);
2467 memcpy (dest, src, s_len);
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 // FIXME: how do we know that 'pos' has enough space for the new name?
2540 expand_plus (pos, (char*)rd[i].data+sizeof(uint16_t),
2542 offset += strlen(new_mx_data+sizeof(uint16_t)) + 1;
2543 p_rd[i].data = new_mx_data;
2544 p_rd[i].data_size = offset;
2546 else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2549 * Prio, weight and port
2551 new_srv = (struct srv_data*)new_srv_data;
2552 old_srv = (struct srv_data*)rd[i].data;
2553 new_srv->prio = old_srv->prio;
2554 new_srv->weight = old_srv->weight;
2555 new_srv->port = old_srv->port;
2556 // FIXME: how do we know that '&new_srv[1]' has enough space for the new name?
2557 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2559 p_rd[i].data = new_srv_data;
2560 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2562 else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2564 /* expand mname and rname */
2565 old_soa = (struct soa_data*)rd[i].data;
2566 new_soa = (struct soa_data*)new_soa_data;
2567 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2568 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2569 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2570 offset = strlen ((char*)&new_soa[1]) + 1;
2571 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2572 expand_plus((char*)&new_soa[1] + offset,
2573 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2575 p_rd[i].data_size = sizeof (struct soa_data)
2577 + strlen ((char*)&new_soa[1] + offset);
2578 p_rd[i].data = new_soa_data;
2583 // FIXME: how do we know that 'rd[i].data' has enough space for the new name?
2584 expand_plus(pos, (char*)rd[i].data, repl_string);
2585 p_rd[i].data_size = strlen(new_rr_data)+1;
2586 p_rd[i].data = new_rr_data;
2591 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2593 free_resolver_handle (rh);
2598 * Process DHT lookup result for record.
2600 * @param cls the closure
2601 * @param rh resolver handle
2602 * @param rd_count number of results
2603 * @param rd record data
2606 handle_record_dht(void* cls, struct ResolverHandle *rh,
2607 unsigned int rd_count,
2608 const struct GNUNET_NAMESTORE_RecordData *rd)
2610 struct RecordLookupHandle* rlh = cls;
2614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2615 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2617 /* give up, cannot resolve */
2618 finish_lookup (rh, rlh, 0, NULL);
2621 /* results found yay */
2622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2623 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2624 finish_lookup (rh, rlh, rd_count, rd);
2629 * Process namestore lookup result for record.
2631 * @param cls the closure
2632 * @param rh resolver handle
2633 * @param rd_count number of results
2634 * @param rd record data
2637 handle_record_ns (void* cls, struct ResolverHandle *rh,
2638 unsigned int rd_count,
2639 const struct GNUNET_NAMESTORE_RecordData *rd)
2641 struct RecordLookupHandle* rlh = cls;
2642 int check_dht = GNUNET_YES;
2646 /* results found yay */
2647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2648 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2649 finish_lookup (rh, rlh, rd_count, rd);
2653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2654 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2658 * There are 5 conditions that have to met for us to consult the DHT:
2659 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2660 * 2. No entry in the NS existed AND
2661 * 3. The zone queried is not the local resolver's zone AND
2662 * 4. The name that was looked up is '+'
2663 * because if it was any other canonical name we either already queried
2664 * the DHT for the authority in the authority lookup phase (and thus
2665 * would already have an entry in the NS for the record)
2666 * 5. We are not in cache only mode
2668 if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2669 (0 == (rh->status & RSL_RECORD_EXISTS)) )
2672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2673 "GNS_PHASE_REC-%llu: Not expired and exists!\n",
2675 check_dht = GNUNET_NO;
2678 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2679 &rh->private_local_zone))
2682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2683 "GNS_PHASE_REC-%llu: Our zone!\n",
2685 check_dht = GNUNET_NO;
2688 if ((0 != strcmp (rh->name, "+")) && (GNUNET_YES == is_srv (rh->name)))
2689 check_dht = GNUNET_NO;
2691 if (GNUNET_YES == rh->only_cached)
2692 check_dht = GNUNET_NO;
2694 if (GNUNET_YES == check_dht)
2696 rh->proc = &handle_record_dht;
2697 resolve_record_dht(rh);
2700 /* give up, cannot resolve */
2701 finish_lookup (rh, rlh, 0, NULL);
2706 * Move one level up in the domain hierarchy and return the
2707 * passed top level domain.
2709 * @param name the domain
2710 * @param dest the destination where the tld will be put
2713 pop_tld(char* name, char* dest)
2717 if (GNUNET_YES == is_canonical (name))
2724 for (len = strlen(name); 0 < len; len--)
2726 if (*(name+len) == '.')
2736 strcpy(dest, (name+len+1));
2741 * Checks if name is in tld
2743 * @param name the name to check
2744 * @param tld the TLD to check for
2745 * @return GNUNET_YES or GNUNET_NO
2748 is_tld(const char* name, const char* tld)
2752 if (strlen(name) <= strlen(tld))
2755 offset = strlen(name)-strlen(tld);
2756 if (0 != strcmp(name+offset, tld))
2758 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2759 "%s is not in .%s TLD\n", name, tld);
2767 * DHT resolution for delegation finished. Processing result.
2769 * @param cls the closure
2770 * @param rh resolver handle
2771 * @param rd_count number of results (always 0)
2772 * @param rd record data (always NULL)
2775 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2776 unsigned int rd_count,
2777 const struct GNUNET_NAMESTORE_RecordData *rd)
2779 struct RecordLookupHandle* rlh;
2783 if (0 == strcmp(rh->name, ""))
2785 if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2787 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2788 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2790 finish_lookup(rh, rlh, rd_count, rd);
2793 /* We resolved full name for delegation. resolving record */
2794 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2795 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2797 strcpy(rh->name, "+\0");
2798 rh->proc = &handle_record_ns;
2799 resolve_record_ns(rh);
2804 * we still have some left
2806 if (GNUNET_YES == is_canonical (rh->name))
2808 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2809 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2812 rh->proc = &handle_record_ns;
2813 resolve_record_ns(rh);
2816 /* give up, cannot resolve */
2817 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2818 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2820 finish_lookup(rh, rlh, 0, NULL);
2825 * Start DHT lookup for a name -> PKEY (compare NS) record in
2826 * rh->authority's zone
2828 * @param rh the pending gns query
2831 resolve_delegation_dht(struct ResolverHandle *rh)
2834 struct GNUNET_HashCode lookup_key;
2835 struct ResolverHandle *rh_heap_root;
2837 pop_tld(rh->name, rh->authority_name);
2839 GNUNET_GNS_get_key_for_record (rh->authority_name, &rh->authority, &lookup_key);
2841 rh->dht_heap_node = NULL;
2843 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2845 rh->timeout_cont = &dht_authority_lookup_timeout;
2846 rh->timeout_cont_cls = rh;
2850 if (max_allowed_background_queries <=
2851 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2853 /* terminate oldest lookup */
2854 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2855 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2856 rh_heap_root->get_handle = NULL;
2857 rh_heap_root->dht_heap_node = NULL;
2859 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2860 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2861 rh->id, rh_heap_root->authority_name);
2863 rh_heap_root->proc(rh_heap_root->proc_cls,
2868 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2870 GNUNET_TIME_absolute_get().abs_value);
2873 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2874 GNUNET_assert(rh->get_handle == NULL);
2875 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2876 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2878 DHT_GNS_REPLICATION_LEVEL,
2879 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2882 &process_delegation_result_dht,
2888 * Namestore resolution for delegation finished. Processing result.
2890 * @param cls the closure
2891 * @param rh resolver handle
2892 * @param rd_count number of results (always 0)
2893 * @param rd record data (always NULL)
2896 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2897 unsigned int rd_count,
2898 const struct GNUNET_NAMESTORE_RecordData *rd)
2900 struct RecordLookupHandle* rlh;
2902 int check_dht = GNUNET_YES;
2905 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2906 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2907 rh->id, rh->status);
2909 if (rh->status & RSL_PKEY_REVOKED)
2911 finish_lookup (rh, rlh, 0, NULL);
2915 if (0 == strcmp(rh->name, ""))
2918 /* We resolved full name for delegation. resolving record */
2919 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2920 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2922 if (rh->status & RSL_CNAME_FOUND)
2924 if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2926 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2927 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2929 strcpy (rh->name, rh->authority_name);
2930 finish_lookup (rh, rlh, rd_count, rd);
2935 if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2937 s_len = strlen (rd->data) - 2;
2938 memcpy (rh->name, rd->data, s_len);
2939 rh->name[s_len] = '\0';
2940 resolve_delegation_ns (rh);
2943 else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2945 gns_resolver_lookup_record (rh->authority,
2946 rh->private_local_zone,
2955 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2956 free_resolver_handle (rh);
2962 strcpy (rh->dns_name, (char*)rd->data);
2963 resolve_dns_name (rh);
2968 else if (rh->status & RSL_DELEGATE_VPN)
2970 if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2972 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2973 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2975 finish_lookup(rh, rlh, rd_count, rd);
2978 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2979 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2981 GNUNET_assert (NULL != rd);
2982 rh->proc = &handle_record_vpn;
2983 resolve_record_vpn (rh, rd_count, rd);
2986 else if (rh->status & RSL_DELEGATE_NS)
2988 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2990 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2991 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2993 finish_lookup(rh, rlh, rd_count, rd);
2997 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2998 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3000 GNUNET_assert (NULL != rd);
3001 rh->proc = &handle_record_ns;
3002 resolve_record_dns (rh, rd_count, rd);
3005 else if (rh->status & RSL_DELEGATE_PKEY)
3007 if (rh->status & RSL_PKEY_REVOKED)
3009 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3010 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
3012 finish_lookup (rh, rlh, 0, NULL);
3015 else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
3017 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3018 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
3020 finish_lookup(rh, rlh, rd_count, rd);
3024 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3025 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3027 strcpy(rh->name, "+\0");
3028 rh->proc = &handle_record_ns;
3029 resolve_record_ns(rh);
3033 if (rh->status & RSL_DELEGATE_NS)
3035 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
3037 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3038 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3040 finish_lookup(rh, rlh, rd_count, rd);
3044 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3045 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3047 GNUNET_assert (NULL != rd);
3048 rh->proc = &handle_record_ns;
3049 resolve_record_dns (rh, rd_count, rd);
3054 * we still have some left
3055 * check if authority in ns is fresh
3057 * or we are authority
3060 if ((rh->status & RSL_RECORD_EXISTS) &&
3061 !(rh->status & RSL_RECORD_EXPIRED))
3062 check_dht = GNUNET_NO;
3064 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3065 &rh->private_local_zone))
3066 check_dht = GNUNET_NO;
3068 if (GNUNET_YES == rh->only_cached)
3069 check_dht = GNUNET_NO;
3071 if (GNUNET_YES == check_dht)
3074 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3075 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3077 rh->proc = &handle_delegation_dht;
3078 resolve_delegation_dht(rh);
3082 if (GNUNET_NO == is_canonical (rh->name))
3084 /* give up, cannot resolve */
3085 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3086 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3089 finish_lookup(rh, rlh, rd_count, rd);
3092 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3093 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3096 rh->proc = &handle_record_ns;
3097 resolve_record_ns(rh);
3102 * This is a callback function that should give us only PKEY
3103 * records. Used to query the namestore for the authority (PKEY)
3104 * for 'name'. It will recursively try to resolve the
3105 * authority for a given name from the namestore.
3107 * @param cls the pending query
3108 * @param key the key of the zone we did the lookup
3109 * @param expiration expiration date of the record data set in the namestore
3110 * @param name the name for which we need an authority
3111 * @param rd_count the number of records with 'name'
3112 * @param rd the record data
3113 * @param signature the signature of the authority for the record data
3116 process_delegation_result_ns (void* cls,
3117 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3118 struct GNUNET_TIME_Absolute expiration,
3120 unsigned int rd_count,
3121 const struct GNUNET_NAMESTORE_RecordData *rd,
3122 const struct GNUNET_CRYPTO_RsaSignature *signature)
3124 struct ResolverHandle *rh;
3125 struct GNUNET_TIME_Relative remaining_time;
3126 struct GNUNET_CRYPTO_ShortHashCode zone;
3127 char new_name[MAX_DNS_NAME_LENGTH];
3129 struct GNUNET_TIME_Absolute et;
3131 rh = (struct ResolverHandle *)cls;
3132 rh->namestore_task = NULL;
3133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3134 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3137 GNUNET_CRYPTO_short_hash (key,
3138 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3140 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3147 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3149 rh->status |= RSL_RECORD_EXISTS;
3151 if (remaining_time.rel_value == 0)
3153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3154 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3156 rh->status |= RSL_RECORD_EXPIRED;
3161 * No authority found in namestore.
3166 * We did not find an authority in the namestore
3171 * Promote this authority back to a name maybe it is
3174 if (strcmp (rh->name, "") == 0)
3176 /* simply promote back */
3177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3178 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3179 rh->id, rh->authority_name);
3180 strcpy (rh->name, rh->authority_name);
3184 /* add back to existing name */
3185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3186 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3187 rh->id, rh->authority_name, rh->name);
3188 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3189 rh->name, rh->authority_name);
3190 strcpy (rh->name, new_name);
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3192 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3196 rh->proc (rh->proc_cls, rh, 0, NULL);
3201 * We found an authority that may be able to help us
3202 * move on with query
3203 * Note only 1 pkey should have been returned.. anything else would be strange
3205 for (i=0; i < rd_count;i++)
3209 * A CNAME. Like regular DNS this means the is no other record for this
3212 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3214 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3215 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3218 rh->status |= RSL_CNAME_FOUND;
3219 rh->proc (rh->proc_cls, rh, rd_count, rd);
3226 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3228 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3229 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3231 rh->status |= RSL_DELEGATE_VPN;
3232 rh->proc (rh->proc_cls, rh, rd_count, rd);
3238 * FIXME make optional
3240 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3242 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3243 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3245 rh->status |= RSL_DELEGATE_NS;
3246 rh->proc (rh->proc_cls, rh, rd_count, rd);
3250 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3253 rh->status |= RSL_DELEGATE_PKEY;
3255 if ((ignore_pending_records != 0) &&
3256 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3259 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3265 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3266 et.abs_value = rd[i].expiration_time;
3267 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3271 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3273 if (remaining_time.rel_value == 0)
3275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3276 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3278 rh->authority_chain_head->fresh = 0;
3279 rh->proc (rh->proc_cls, rh, 0, NULL);
3287 * Resolve rest of query with new authority
3289 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3290 memcpy (&rh->authority, rd[i].data,
3291 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3292 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3293 auth->zone = rh->authority;
3294 memset (auth->name, 0, strlen (rh->authority_name)+1);
3295 strcpy (auth->name, rh->authority_name);
3296 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3297 rh->authority_chain_tail,
3299 if (NULL != rh->rd.data)
3300 GNUNET_free ((void*)(rh->rd.data));
3302 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3303 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3304 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3306 /* Check for key revocation and delegate */
3307 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3310 GNUNET_GNS_RECORD_REV,
3311 &process_pkey_revocation_result_ns,
3320 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3321 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3323 * If we have found some records for the LAST label
3324 * we return the results. Else null.
3326 if (strcmp (rh->name, "") == 0)
3328 /* Start shortening */
3329 if ((rh->priv_key != NULL) &&
3330 (is_canonical (rh->name) == GNUNET_YES))
3332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3333 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3335 start_shorten (rh->authority_chain_head,
3338 /* simply promote back */
3339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3340 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3341 rh->id, rh->authority_name);
3342 strcpy (rh->name, rh->authority_name);
3343 rh->proc (rh->proc_cls, rh, rd_count, rd);
3347 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
3348 "%s.%s", rh->name, rh->authority_name);
3349 strcpy (rh->name, new_name);
3350 rh->proc (rh->proc_cls, rh, 0, NULL);
3356 * Resolve the delegation chain for the request in our namestore
3358 * @param rh the resolver handle
3361 resolve_delegation_ns (struct ResolverHandle *rh)
3363 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3364 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3366 pop_tld(rh->name, rh->authority_name);
3367 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3370 GNUNET_GNS_RECORD_ANY,
3371 &process_delegation_result_ns,
3378 * Lookup of a record in a specific zone
3379 * calls lookup result processor on result
3381 * @param zone the root zone
3382 * @param pzone the private local zone
3383 * @param record_type the record type to look up
3384 * @param name the name to look up
3385 * @param key a private key for use with PSEU import (can be NULL)
3386 * @param timeout timeout for resolution
3387 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3388 * @param proc the processor to call on result
3389 * @param cls the closure to pass to proc
3392 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3393 struct GNUNET_CRYPTO_ShortHashCode pzone,
3394 uint32_t record_type,
3396 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3397 struct GNUNET_TIME_Relative timeout,
3399 RecordLookupProcessor proc,
3402 struct ResolverHandle *rh;
3403 struct RecordLookupHandle* rlh;
3404 char string_hash[MAX_DNS_LABEL_LENGTH];
3405 char nzkey[MAX_DNS_LABEL_LENGTH];
3406 char* nzkey_ptr = nzkey;
3408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3409 "Starting resolution for %s (type=%d)!\n",
3413 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3414 (strcmp(GNUNET_GNS_TLD, name) != 0))
3416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3417 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3422 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3423 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3425 memset (rh, 0, sizeof (struct ResolverHandle));
3426 rh->authority = zone;
3430 rh->timeout = timeout;
3431 rh->get_handle = NULL;
3432 rh->private_local_zone = pzone;
3433 rh->only_cached = only_cached;
3434 rh->namestore_task = NULL;
3437 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3441 "No shorten key for resolution\n");
3443 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3446 * Set timeout for authority lookup phase to 1/2
3448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3449 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3450 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3451 GNUNET_TIME_relative_divide(timeout, 2),
3452 &handle_lookup_timeout,
3454 rh->timeout_cont = &dht_authority_lookup_timeout;
3455 rh->timeout_cont_cls = rh;
3459 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3460 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3463 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3466 * Only 'gnunet' given
3468 strcpy(rh->name, "\0");
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3473 "Checking for TLD...\n");
3474 if (is_zkey_tld(name) == GNUNET_YES)
3476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3479 * This is a zkey tld
3480 * build hash and use as initial authority
3483 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3484 memcpy(rh->name, name,
3485 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3486 pop_tld(rh->name, string_hash);
3488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3489 "ZKEY is %s!\n", string_hash);
3491 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3493 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3497 "Cannot convert ZKEY `%s' to hash!\n", string_hash);
3499 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3500 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3501 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3504 proc (cls, 0, NULL);
3509 else if (is_gnunet_tld (name) == GNUNET_YES)
3511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3514 * Presumably GNUNET tld
3516 memset (rh->name, 0,
3517 strlen(name)-strlen(GNUNET_GNS_TLD));
3518 memcpy (rh->name, name,
3519 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3523 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3524 "Cannot handle this TLD %s\n", string_hash);
3526 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3527 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3528 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3531 proc (cls, 0, NULL);
3537 * Initialize authority chain
3539 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3540 rh->authority_chain_head->prev = NULL;
3541 rh->authority_chain_head->next = NULL;
3542 rh->authority_chain_tail = rh->authority_chain_head;
3543 rh->authority_chain_head->zone = rh->authority;
3544 strcpy (rh->authority_chain_head->name, "");
3547 * Copy original query into lookup handle
3549 rlh->record_type = record_type;
3550 memset(rlh->name, 0, strlen(name) + 1);
3551 strcpy(rlh->name, name);
3553 rlh->proc_cls = cls;
3555 rh->proc = &handle_delegation_ns;
3556 resolve_delegation_ns(rh);
3559 /******** END Record Resolver ***********/
3562 finish_shorten (struct ResolverHandle *rh,
3563 struct NameShortenHandle *nsh)
3565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3566 "Sending %s as shorten result\n", nsh->result);
3567 nsh->proc (nsh->proc_cls, nsh->result);
3568 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3570 free_resolver_handle (rh);
3575 * Callback calles by namestore for a zone to name
3578 * @param cls the closure
3579 * @param zone_key the zone we queried
3580 * @param expire the expiration time of the name
3581 * @param name the name found or NULL
3582 * @param rd_len number of records for the name
3583 * @param rd the record data (PKEY) for the name
3584 * @param signature the signature for the record data
3587 process_zone_to_name_shorten_root (void *cls,
3588 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3589 struct GNUNET_TIME_Absolute expire,
3591 unsigned int rd_len,
3592 const struct GNUNET_NAMESTORE_RecordData *rd,
3593 const struct GNUNET_CRYPTO_RsaSignature *signature);
3597 * Callback called by namestore for a zone to name
3600 * @param cls the closure
3601 * @param zone_key the zone we queried
3602 * @param expire the expiration time of the name
3603 * @param name the name found or NULL
3604 * @param rd_len number of records for the name
3605 * @param rd the record data (PKEY) for the name
3606 * @param signature the signature for the record data
3609 process_zone_to_name_shorten_shorten (void *cls,
3610 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3611 struct GNUNET_TIME_Absolute expire,
3613 unsigned int rd_len,
3614 const struct GNUNET_NAMESTORE_RecordData *rd,
3615 const struct GNUNET_CRYPTO_RsaSignature *signature)
3617 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3618 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3619 struct AuthorityChain *next_authority;
3621 char result[MAX_DNS_NAME_LENGTH];
3622 char tmp_name[MAX_DNS_NAME_LENGTH];
3625 rh->namestore_task = NULL;
3626 /* we found a match in our own root zone */
3629 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3630 memset(result, 0, answer_len);
3632 if (strlen(rh->name) > 0)
3634 sprintf (result, "%s.%s.%s.%s",
3636 nsh->shorten_zone_name,
3641 sprintf (result, "%s.%s.%s", name,
3642 nsh->shorten_zone_name,
3646 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3647 "Found shorten result %s\n", result);
3648 if (strlen (nsh->result) > strlen (result))
3649 strcpy (nsh->result, result);
3651 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3652 nsh->shorten_zone) == 0)
3655 * This is our zone append .gads unless name is empty
3656 * (it shouldn't be, usually FIXME what happens if we
3657 * shorten to our zone to a "" record??)
3660 sprintf (result, "%s.%s.%s",
3662 nsh->shorten_zone_name,
3664 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3665 "Our zone: Found %s as shorten result\n", result);
3667 if (strlen (nsh->result) > strlen (result))
3668 strcpy (nsh->result, result);
3669 //nsh->proc(nsh->proc_cls, result);
3671 //free_resolver_handle(rh);
3678 * continue with next authority if exists
3680 if (NULL == rh->authority_chain_head->next)
3682 finish_shorten (rh, nsh);
3685 next_authority = rh->authority_chain_head;
3687 if (0 == strcmp (rh->name, ""))
3688 strcpy (tmp_name, next_authority->name);
3690 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3691 "%s.%s", rh->name, next_authority->name);
3693 strcpy(rh->name, tmp_name);
3694 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3695 "No PSEU found for authority %s. Promoting back: %s\n",
3696 next_authority->name, rh->name);
3698 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3699 rh->authority_chain_tail,
3702 GNUNET_free (next_authority);
3704 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3705 &rh->authority_chain_tail->zone,
3706 &rh->authority_chain_head->zone,
3707 &process_zone_to_name_shorten_root,
3713 * Callback calles by namestore for a zone to name
3716 * @param cls the closure
3717 * @param zone_key the zone we queried
3718 * @param expire the expiration time of the name
3719 * @param name the name found or NULL
3720 * @param rd_len number of records for the name
3721 * @param rd the record data (PKEY) for the name
3722 * @param signature the signature for the record data
3725 process_zone_to_name_shorten_private (void *cls,
3726 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3727 struct GNUNET_TIME_Absolute expire,
3729 unsigned int rd_len,
3730 const struct GNUNET_NAMESTORE_RecordData *rd,
3731 const struct GNUNET_CRYPTO_RsaSignature *signature)
3733 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3734 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3735 struct AuthorityChain *next_authority;
3737 char result[MAX_DNS_NAME_LENGTH];
3738 char tmp_name[MAX_DNS_NAME_LENGTH];
3741 rh->namestore_task = NULL;
3742 /* we found a match in our own root zone */
3745 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3746 memset(result, 0, answer_len);
3748 if (strlen(rh->name) > 0)
3750 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3754 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3757 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3758 "Found shorten result %s\n", result);
3759 if (strlen (nsh->result) > strlen (result))
3760 strcpy (nsh->result, result);
3762 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3763 nsh->private_zone) == 0)
3766 * This is our zone append .gads unless name is empty
3767 * (it shouldn't be, usually FIXME what happens if we
3768 * shorten to our zone to a "" record??)
3771 sprintf (result, "%s.%s.%s",
3772 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3773 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3774 "Our private zone: Found %s as shorten result %s\n", result);
3775 if (strlen (nsh->result) > strlen (result))
3776 strcpy (nsh->result, result);
3779 if (0 != strcmp (nsh->shorten_zone_name, ""))
3781 /* backtrack authorities for names in priv zone */
3782 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3784 &rh->authority_chain_head->zone,
3785 &process_zone_to_name_shorten_shorten,
3792 * continue with next authority if exists
3794 if (NULL == rh->authority_chain_head->next)
3796 finish_shorten (rh, nsh);
3799 next_authority = rh->authority_chain_head;
3801 if (0 == strcmp (rh->name, ""))
3802 strcpy (tmp_name, next_authority->name);
3804 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3805 "%s.%s", rh->name, next_authority->name);
3807 strcpy(rh->name, tmp_name);
3808 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3809 "No PSEU found for authority %s. Promoting back: %s\n",
3810 next_authority->name, rh->name);
3812 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3813 rh->authority_chain_tail,
3816 GNUNET_free (next_authority);
3818 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3819 &rh->authority_chain_tail->zone,
3820 &rh->authority_chain_head->zone,
3821 &process_zone_to_name_shorten_root,
3828 * Callback calles by namestore for a zone to name
3831 * @param cls the closure
3832 * @param zone_key the zone we queried
3833 * @param expire the expiration time of the name
3834 * @param name the name found or NULL
3835 * @param rd_len number of records for the name
3836 * @param rd the record data (PKEY) for the name
3837 * @param signature the signature for the record data
3840 process_zone_to_name_shorten_root (void *cls,
3841 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3842 struct GNUNET_TIME_Absolute expire,
3844 unsigned int rd_len,
3845 const struct GNUNET_NAMESTORE_RecordData *rd,
3846 const struct GNUNET_CRYPTO_RsaSignature *signature)
3848 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3849 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3850 struct AuthorityChain *next_authority;
3852 char result[MAX_DNS_NAME_LENGTH];
3853 char tmp_name[MAX_DNS_NAME_LENGTH];
3856 rh->namestore_task = NULL;
3857 /* we found a match in our own root zone */
3860 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3861 memset(result, 0, answer_len);
3863 if (strlen(rh->name) > 0)
3865 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3869 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3872 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3873 "Found shorten result %s\n", result);
3874 if (strlen (nsh->result) > strlen (result))
3875 strcpy (nsh->result, result);
3877 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3878 nsh->root_zone) == 0)
3881 * This is our zone append .gads unless name is empty
3882 * (it shouldn't be, usually FIXME what happens if we
3883 * shorten to our zone to a "" record??)
3886 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3887 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3888 "Our zone: Found %s as shorten result\n", result);
3889 if (strlen (nsh->result) > strlen (result))
3890 strcpy (nsh->result, result);
3893 if (NULL != nsh->private_zone)
3895 /* backtrack authorities for names in priv zone */
3896 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3898 &rh->authority_chain_head->zone,
3899 &process_zone_to_name_shorten_private,
3902 else if (NULL != nsh->shorten_zone)
3904 /* backtrack authorities for names in shorten zone */
3905 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3907 &rh->authority_chain_head->zone,
3908 &process_zone_to_name_shorten_shorten,
3915 * continue with next authority if exists
3917 if (NULL == rh->authority_chain_head->next)
3919 finish_shorten (rh, nsh);
3922 next_authority = rh->authority_chain_head;
3924 if (0 == strcmp (rh->name, ""))
3925 strcpy (tmp_name, next_authority->name);
3927 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3928 "%s.%s", rh->name, next_authority->name);
3930 strcpy(rh->name, tmp_name);
3931 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3932 "No PSEU found for authority %s. Promoting back: %s\n",
3933 next_authority->name, rh->name);
3935 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3936 rh->authority_chain_tail,
3939 GNUNET_free (next_authority);
3941 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3942 &rh->authority_chain_tail->zone,
3943 &rh->authority_chain_head->zone,
3944 &process_zone_to_name_shorten_root,
3951 * Process result from namestore delegation lookup
3952 * for shorten operation
3954 * @param cls the client shorten handle
3955 * @param rh the resolver handle
3956 * @param rd_count number of results (0)
3957 * @param rd data (NULL)
3960 handle_delegation_ns_shorten (void* cls,
3961 struct ResolverHandle *rh,
3963 const struct GNUNET_NAMESTORE_RecordData *rd)
3965 struct NameShortenHandle *nsh;
3966 char result[MAX_DNS_NAME_LENGTH];
3968 nsh = (struct NameShortenHandle *)cls;
3969 rh->namestore_task = NULL;
3971 * At this point rh->name contains the part of the name
3972 * that we do not have a PKEY in our namestore to resolve.
3973 * The authority chain in the resolver handle is now
3974 * useful to backtrack if needed
3977 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3978 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3979 memset(result, 0, sizeof (result));
3981 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3985 * This is our zone append .gads unless name is empty
3986 * (it shouldn't be, usually FIXME what happens if we
3987 * shorten to our zone to a "" record??)
3990 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3991 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3992 "Our zone: Found %s as shorten result\n", result);
3994 if (strlen (nsh->result) > strlen (result))
3995 strcpy (nsh->result, result);
3998 else if (NULL != nsh->private_zone)
4001 * This is our zone append .gads unless name is empty
4002 * (it shouldn't be, usually FIXME what happens if we
4003 * shorten to our zone to a "" record??)
4005 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4006 nsh->private_zone) == 0)
4009 sprintf (result, "%s.%s.%s",
4010 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4011 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4012 "Our zone: Found %s as shorten result in private zone %s\n",
4015 if (strlen (nsh->result) > strlen (result))
4016 strcpy (nsh->result, result);
4019 else if (NULL != nsh->shorten_zone)
4022 * This is our zone append .gads unless name is empty
4023 * (it shouldn't be, usually FIXME what happens if we
4024 * shorten to our zone to a "" record??)
4026 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4027 nsh->shorten_zone) == 0)
4029 sprintf (result, "%s.%s.%s",
4030 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4031 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4032 "Our zone: Found %s as shorten result in shorten zone\n",
4035 if (strlen (nsh->result) > strlen (result))
4036 strcpy (nsh->result, result);
4041 /* backtrack authorities for names */
4042 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4044 &rh->authority_chain_head->zone,
4045 &process_zone_to_name_shorten_root,
4052 * Callback calles by namestore for a zone to name
4055 * @param cls the closure
4056 * @param zone_key the zone we queried
4057 * @param expire the expiration time of the name
4058 * @param name the name found or NULL
4059 * @param rd_len number of records for the name
4060 * @param rd the record data (PKEY) for the name
4061 * @param signature the signature for the record data
4064 process_zone_to_name_zkey(void *cls,
4065 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4066 struct GNUNET_TIME_Absolute expire,
4068 unsigned int rd_len,
4069 const struct GNUNET_NAMESTORE_RecordData *rd,
4070 const struct GNUNET_CRYPTO_RsaSignature *signature)
4072 struct ResolverHandle *rh = cls;
4073 struct NameShortenHandle *nsh = rh->proc_cls;
4074 char new_name[MAX_DNS_NAME_LENGTH];
4076 rh->namestore_task = NULL;
4078 /* zkey not in our zone */
4082 * In this case we have not given this PKEY a name (yet)
4083 * It is either just not in our zone or not even cached
4084 * Since we do not know at this point we will not try to shorten
4085 * because PKEY import will happen if the user follows the zkey
4088 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4089 "No name found for zkey %s returning verbatim!\n", nsh->result);
4090 /*if (strcmp(rh->name, "") != 0)
4091 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4092 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4094 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4095 enc, GNUNET_GNS_TLD_ZKEY);
4097 strcpy (nsh->result, new_name);*/
4099 finish_shorten (rh, nsh);
4103 if (strcmp(rh->name, "") != 0)
4104 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4107 strcpy(new_name, name);
4109 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4110 "Continue shorten for %s!\n", new_name);
4112 strcpy(rh->name, new_name);
4114 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4115 rh->authority_chain_tail = rh->authority_chain_head;
4116 rh->authority_chain_head->zone = rh->authority;
4119 /* Start delegation resolution in our namestore */
4120 resolve_delegation_ns (rh);
4125 * Shorten api from resolver
4127 * @param zone the root zone to use
4128 * @param pzone the private zone to use
4129 * @param szone the shorten zone to use
4130 * @param name the name to shorten
4131 * @param private_zone_name name of the private zone
4132 * @param shorten_zone_name name of the shorten zone
4133 * @param proc the processor to call with result
4134 * @param proc_cls closure to pass to proc
4137 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4138 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4139 struct GNUNET_CRYPTO_ShortHashCode *szone,
4141 const char* private_zone_name,
4142 const char* shorten_zone_name,
4143 ShortenResultProcessor proc,
4146 struct ResolverHandle *rh;
4147 struct NameShortenHandle *nsh;
4148 char string_hash[MAX_DNS_LABEL_LENGTH];
4149 struct GNUNET_CRYPTO_ShortHashCode zkey;
4150 char nzkey[MAX_DNS_LABEL_LENGTH];
4151 char* nzkey_ptr = nzkey;
4154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4155 "Starting shorten for %s!\n", name);
4157 if (is_canonical ((char*)name) == GNUNET_YES)
4159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4160 "%s is canonical. Returning verbatim\n", name);
4161 proc (proc_cls, name);
4165 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4167 nsh->proc_cls = proc_cls;
4168 nsh->root_zone = zone;
4169 nsh->private_zone = pzone;
4170 nsh->shorten_zone = szone;
4171 strcpy (nsh->private_zone_name, private_zone_name);
4172 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4173 strcpy (nsh->result, name);
4175 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4176 rh->authority = *zone;
4178 rh->priv_key = NULL;
4179 rh->namestore_task = NULL;
4180 rh->proc = &handle_delegation_ns_shorten;
4183 rh->private_local_zone = *zone;
4185 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4187 "Checking for TLD...\n");
4188 if (is_zkey_tld (name) == GNUNET_YES)
4190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4193 * This is a zkey tld
4194 * build hash and use as initial authority
4197 memset (rh->name, 0,
4198 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4199 memcpy (rh->name, name,
4200 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4201 pop_tld (rh->name, string_hash);
4203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4204 "ZKEY is %s!\n", string_hash);
4206 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4208 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4212 "Cannot convert ZKEY %s to hash!\n", nzkey);
4213 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4216 proc (proc_cls, name);
4219 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4222 &process_zone_to_name_zkey,
4227 else if (is_gnunet_tld (name) == GNUNET_YES)
4229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4232 * Presumably GNUNET tld
4234 memset (rh->name, 0,
4235 strlen (name)-strlen (GNUNET_GNS_TLD));
4236 memcpy (rh->name, name,
4237 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4242 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4245 proc (proc_cls, name);
4249 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4250 rh->authority_chain_tail = rh->authority_chain_head;
4251 rh->authority_chain_head->zone = *zone;
4254 /* Start delegation resolution in our namestore */
4255 resolve_delegation_ns (rh);
4258 /*********** END NAME SHORTEN ********************/
4261 * Conclude get authority lookup
4263 * @param rh resolver handle
4264 * @param nah get authority lookup handle
4267 finish_get_auth (struct ResolverHandle *rh,
4268 struct GetNameAuthorityHandle *nah)
4270 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4271 "Got authority result %s\n", nah->result);
4273 nah->proc (nah->proc_cls, nah->result);
4274 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4276 free_resolver_handle (rh);
4280 * Process result from namestore delegation lookup
4281 * for get authority operation
4283 * @param cls the client get auth handle
4284 * @param rh the resolver handle
4285 * @param rd_count number of results (0)
4286 * @param rd data (NULL)
4289 handle_delegation_result_ns_get_auth(void* cls,
4290 struct ResolverHandle *rh,
4292 const struct GNUNET_NAMESTORE_RecordData *rd)
4294 struct GetNameAuthorityHandle* nah = rh->proc_cls;
4298 * At this point rh->name contains the part of the name
4299 * that we do not have a PKEY in our namestore to resolve.
4300 * The authority chain in the resolver handle is now
4301 * useful to backtrack if needed
4304 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4305 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4307 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4308 "Building response!\n");
4309 if (is_canonical (rh->name) == GNUNET_YES)
4312 * We successfully resolved the authority in the ns
4313 * FIXME for our purposes this is fine
4314 * but maybe we want to have an api that also looks
4315 * into the dht (i.e. option in message)
4317 if (strlen(rh->name) > strlen(nah->name))
4319 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4320 "Record name longer than original lookup name... odd!\n");
4324 answer_len = strlen(nah->name) - strlen(rh->name)
4325 + strlen(GNUNET_GNS_TLD) + 1;
4326 memset(nah->result, 0, answer_len);
4327 if (0 != strcmp (rh->name, ""))
4328 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4330 strcpy(nah->result, nah->name);
4332 finish_get_auth (rh, nah);
4336 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4337 "Unable to resolve authority for remaining %s!\n", rh->name);
4338 strcpy(nah->result, "");
4339 finish_get_auth (rh, nah);
4347 * Tries to resolve the authority for name
4350 * @param zone the root zone to look up for
4351 * @param pzone the private local zone
4352 * @param name the name to lookup up
4353 * @param proc the processor to call when finished
4354 * @param proc_cls the closure to pass to the processor
4357 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4358 struct GNUNET_CRYPTO_ShortHashCode pzone,
4360 GetAuthorityResultProcessor proc,
4363 struct ResolverHandle *rh;
4364 struct GetNameAuthorityHandle *nah;
4366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4367 "Starting authority resolution for %s!\n", name);
4369 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4370 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4371 rh->authority = zone;
4373 rh->private_local_zone = pzone;
4374 rh->namestore_task = NULL;
4376 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4378 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4380 strcpy(rh->name, "\0");
4385 strlen(name)-strlen(GNUNET_GNS_TLD));
4386 memcpy(rh->name, name,
4387 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4390 memset(nah->name, 0,
4392 strcpy(nah->name, name);
4394 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4395 rh->authority_chain_tail = rh->authority_chain_head;
4396 rh->authority_chain_head->zone = zone;
4397 rh->proc = &handle_delegation_result_ns_get_auth;
4398 rh->proc_cls = (void*)nah;
4401 nah->proc_cls = proc_cls;
4402 strcpy (nah->result, "");
4404 /* Start delegation resolution in our namestore */
4405 resolve_delegation_ns(rh);
4409 /******** END GET AUTHORITY *************/
4411 /* end of gnunet-service-gns_resolver.c */