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 "../dns/dnsparser.h"
37 #include "gnunet_gns_service.h"
38 #include "block_gns.h"
40 #include "gnunet-service-gns_resolver.h"
42 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
43 #define DHT_GNS_REPLICATION_LEVEL 5
44 #define MAX_DNS_LABEL_LENGTH 63
48 * Our handle to the namestore service
50 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
53 * Our handle to the vpn service
55 static struct GNUNET_VPN_Handle *vpn_handle;
58 * Resolver handle to the dht
60 static struct GNUNET_DHT_Handle *dht_handle;
63 * Heap for parallel DHT lookups
65 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
68 * Heap for namestore queues
70 static struct GNUNET_CONTAINER_Heap *ns_task_heap;
73 * Maximum amount of parallel queries in background
75 static unsigned long long max_allowed_background_queries;
78 * Maximum amount of parallel namestore tasks in background
80 static unsigned long long max_allowed_ns_tasks;
83 * Wheather or not to ignore pending records
85 static int ignore_pending_records;
90 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
93 * Background shortening handles
95 static struct GetPseuAuthorityHandle *gph_head;
98 * Background shortening handles
100 static struct GetPseuAuthorityHandle *gph_tail;
103 * Resolver lookup list
105 static struct ResolverHandle *rlh_head;
108 * Resolver lookup list
110 static struct ResolverHandle *rlh_tail;
113 * Resolver shorten list
115 static struct ResolverHandle *nsh_head;
118 * Resolver shorten list
120 static struct ResolverHandle *nsh_tail;
123 * Resolver get auth list
125 static struct ResolverHandle *nah_head;
128 * Resolver get auth list
130 static struct ResolverHandle *nah_tail;
133 * Global configuration.
135 static const struct GNUNET_CONFIGURATION_Handle *cfg;
138 * a resolution identifier pool variable
140 * This is a non critical identifier useful for debugging
142 static unsigned long long rid = 0;
145 * Check if name is in srv format (_x._y.xxx)
148 * @return GNUNET_YES if true
154 int ret = GNUNET_YES;
158 if (NULL == strstr (name, "._"))
161 ndup = GNUNET_strdup (name);
164 if (NULL == strtok (NULL, "."))
167 if (NULL == strtok (NULL, "."))
170 if (NULL != strtok (NULL, "."))
179 * Determine if this name is canonical.
181 * a.b.gnunet = not canonical
184 * @param name the name to test
185 * @return GNUNET_YES if canonical
188 is_canonical (char* name)
193 ndup = GNUNET_strdup (name);
196 for (tok = strtok (NULL, "."); tok != NULL; tok = strtok (NULL, "."))
212 * Callback that shortens authorities
214 * @param gph the handle containing the name to shorten
217 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
221 * Continueation for pkey record creation (shorten)
223 * @param cls a GetPseuAuthorityHandle
224 * @param success unused
228 create_pkey_cont (void* cls, int32_t success, const char* emsg)
230 //FIXME do sth with error
231 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
233 gph->namestore_task = NULL;
234 GNUNET_free (gph->auth);
235 GNUNET_CRYPTO_rsa_key_free (gph->key);
236 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
242 * Namestore calls this function if we have record for this name.
243 * (or with rd_count=0 to indicate no matches)
245 * @param cls the pending query
246 * @param key the key of the zone we did the lookup
247 * @param expiration expiration date of the namestore entry
248 * @param name the name for which we need an authority
249 * @param rd_count the number of records with 'name'
250 * @param rd the record data
251 * @param signature the signature of the authority for the record data
254 process_pseu_lookup_ns (void* cls,
255 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
256 struct GNUNET_TIME_Absolute expiration,
257 const char *name, unsigned int rd_count,
258 const struct GNUNET_NAMESTORE_RecordData *rd,
259 const struct GNUNET_CRYPTO_RsaSignature *signature)
261 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
262 struct GNUNET_NAMESTORE_RecordData new_pkey;
264 gph->namestore_task = NULL;
267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
268 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
269 GNUNET_free (gph->auth);
270 GNUNET_CRYPTO_rsa_key_free (gph->key);
271 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
280 new_pkey.expiration_time = UINT64_MAX;
281 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
282 new_pkey.data = &gph->auth->zone;
283 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
284 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
285 | GNUNET_NAMESTORE_RF_PRIVATE
286 | GNUNET_NAMESTORE_RF_PENDING;
287 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
291 &create_pkey_cont, //cont
296 * process result of a dht pseu lookup
298 * @param gph the handle
299 * @param name the pseu result or NULL
302 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "GNS_AUTO_PSEU: No PSEU, no shorten. Finished.\n");
308 GNUNET_free (gph->auth);
309 GNUNET_CRYPTO_rsa_key_free (gph->key);
310 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
314 memcpy (gph->test_name, name, strlen(name)+1);
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
320 * Check for collision
322 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
325 GNUNET_NAMESTORE_TYPE_ANY,
326 &process_pseu_lookup_ns,
331 * Handle timeout for dht request
333 * @param cls the request handle as closure
334 * @param tc the task context
337 handle_auth_discovery_timeout (void *cls,
338 const struct GNUNET_SCHEDULER_TaskContext *tc)
340 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
344 GNUNET_DHT_get_stop (gph->get_handle);
345 gph->get_handle = NULL;
346 process_pseu_result (gph, NULL);
350 * Function called when we find a PSEU entry in the DHT
352 * @param cls the request handle
353 * @param exp lifetime
354 * @param key the key the record was stored under
355 * @param get_path get path
356 * @param get_path_length get path length
357 * @param put_path put path
358 * @param put_path_length put path length
359 * @param type the block type
360 * @param size the size of the record
361 * @param data the record data
364 process_auth_discovery_dht_result (void* cls,
365 struct GNUNET_TIME_Absolute exp,
366 const struct GNUNET_HashCode * key,
367 const struct GNUNET_PeerIdentity *get_path,
368 unsigned int get_path_length,
369 const struct GNUNET_PeerIdentity *put_path,
370 unsigned int put_path_length,
371 enum GNUNET_BLOCK_Type type,
372 size_t size, const void *data)
374 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
375 struct GNSNameRecordBlock *nrb;
376 char* rd_data = (char*)data;
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
385 /* stop lookup and timeout task */
386 GNUNET_DHT_get_stop (gph->get_handle);
387 gph->get_handle = NULL;
388 GNUNET_SCHEDULER_cancel (gph->timeout);
392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
393 "GNS_GET_AUTH: got dht result null!\n", size);
394 GNUNET_free (gph->auth);
395 GNUNET_CRYPTO_rsa_key_free (gph->key);
396 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
401 nrb = (struct GNSNameRecordBlock*)data;
402 name = (char*)&nrb[1];
403 num_records = ntohl (nrb->rd_count);
405 struct GNUNET_NAMESTORE_RecordData rd[num_records];
407 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
408 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
410 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
416 "GNS_GET_AUTH: Error deserializing data!\n");
420 for (i=0; i < num_records; i++)
422 if ((strcmp (name, "+") == 0) &&
423 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
426 process_pseu_result (gph, (char*)rd[i].data);
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "GNS_GET_AUTH: finished shorten, no results!\n");
434 process_pseu_result (gph, NULL);
438 * Process PSEU discovery for shorten via namestore
440 * @param cls the GetPseuAuthorityHandle
441 * @param key the public key
442 * @param expiration recorddata expiration
443 * @param name the looked up name
444 * @param rd_count number of records in set
445 * @param rd record data
446 * @param signature the signature
449 process_auth_discovery_ns_result (void* cls,
450 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
451 struct GNUNET_TIME_Absolute expiration,
453 unsigned int rd_count,
454 const struct GNUNET_NAMESTORE_RecordData *rd,
455 const struct GNUNET_CRYPTO_RsaSignature *signature)
458 struct GNUNET_CRYPTO_ShortHashCode name_hash;
459 struct GNUNET_HashCode lookup_key;
460 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
461 struct GNUNET_HashCode name_hash_double;
462 struct GNUNET_HashCode zone_hash_double;
464 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
466 gph->namestore_task = NULL;
473 GNUNET_CRYPTO_short_hash ("+", strlen ("+"), &name_hash);
474 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
475 GNUNET_CRYPTO_short_hash_double (&gph->auth->zone, &zone_hash_double);
476 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
477 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
481 "+", (char*)&lookup_key_string);
483 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
484 &handle_auth_discovery_timeout, gph);
486 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
488 GNUNET_assert (gph->get_handle == NULL);
490 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
491 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
493 DHT_GNS_REPLICATION_LEVEL,
497 &process_auth_discovery_dht_result,
502 for (i=0; i < rd_count; i++)
504 if ((strcmp (name, "+") == 0) &&
505 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
508 process_pseu_result (gph, (char*)rd[i].data);
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
514 process_pseu_result (gph, NULL);
518 * Callback called by namestore for a zone to name
521 * @param cls the closure
522 * @param zone_key the zone we queried
523 * @param expire the expiration time of the name
524 * @param name the name found or NULL
525 * @param rd_len number of records for the name
526 * @param rd the record data (PKEY) for the name
527 * @param signature the signature for the record data
530 process_zone_to_name_discover (void *cls,
531 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
532 struct GNUNET_TIME_Absolute expire,
535 const struct GNUNET_NAMESTORE_RecordData *rd,
536 const struct GNUNET_CRYPTO_RsaSignature *signature)
538 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
540 gph->namestore_task = NULL;
541 /* we found a match in our own zone */
544 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
545 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
547 GNUNET_free (gph->auth);
548 GNUNET_CRYPTO_rsa_key_free (gph->key);
549 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
555 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
558 GNUNET_GNS_RECORD_PSEU,
559 &process_auth_discovery_ns_result,
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,
588 start_shorten (struct AuthorityChain *auth,
589 struct GNUNET_CRYPTO_RsaPrivateKey *key)
591 struct AuthorityChain *acopy;
592 struct GetPseuAuthorityHandle *gph;
593 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
594 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
597 acopy = GNUNET_malloc (sizeof (struct AuthorityChain));
598 memcpy (acopy, auth, sizeof (struct AuthorityChain));
600 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
602 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
603 pb_key = GNUNET_CRYPTO_rsa_encode_key (key);
604 gph->key = GNUNET_CRYPTO_rsa_decode_key ((char*)pb_key, ntohs (pb_key->len));
606 GNUNET_CRYPTO_short_hash (&pkey,
607 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
611 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
613 shorten_authority_chain (gph);
617 * Initialize the resolver
619 * @param nh the namestore handle
620 * @param dh the dht handle
621 * @param lz the local zone's hash
622 * @param c configuration handle
623 * @param max_bg_queries maximum number of parallel background queries in dht
624 * @param ignore_pending ignore records that still require user confirmation
626 * @return GNUNET_OK on success
629 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
630 struct GNUNET_DHT_Handle *dh,
631 struct GNUNET_CRYPTO_ShortHashCode lz,
632 const struct GNUNET_CONFIGURATION_Handle *c,
633 unsigned long long max_bg_queries,
637 namestore_handle = nh;
641 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
643 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
644 max_allowed_background_queries = max_bg_queries;
645 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
647 ignore_pending_records = ignore_pending;
658 GNUNET_RESOLVER_connect (cfg);
661 if ((namestore_handle != NULL) && (dht_handle != NULL))
666 return GNUNET_SYSERR;
673 * @param cls closure to iterator
674 * @param node heap nodes
675 * @param element the namestorebgtask
676 * @param cost heap cost
677 * @return always GNUNET_YES
680 cleanup_pending_ns_tasks (void* cls,
681 struct GNUNET_CONTAINER_HeapNode *node,
683 GNUNET_CONTAINER_HeapCostType cost)
685 struct NamestoreBGTask *nbg = (struct NamestoreBGTask *)element;
686 ResolverCleanupContinuation cont = cls;
688 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
689 "GNS_CLEANUP: Terminating ns task\n");
690 GNUNET_NAMESTORE_cancel (nbg->qe);
692 GNUNET_CONTAINER_heap_remove_node (node);
694 if (GNUNET_CONTAINER_heap_get_size (ns_task_heap) == 0)
704 * @param rh resolver handle
705 * @param rlh record lookup handle
706 * @param rd_count number of results
710 finish_lookup (struct ResolverHandle *rh,
711 struct RecordLookupHandle* rlh,
712 unsigned int rd_count,
713 const struct GNUNET_NAMESTORE_RecordData *rd);
717 * Cleanup background lookups FIXME get rid of this??
719 * @param cls closure to iterator
720 * @param node heap nodes
721 * @param element the resolver handle
722 * @param cost heap cost
723 * @return always GNUNET_YES
726 cleanup_pending_background_queries (void* cls,
727 struct GNUNET_CONTAINER_HeapNode *node,
729 GNUNET_CONTAINER_HeapCostType cost)
731 struct ResolverHandle *rh = (struct ResolverHandle *)element;
732 ResolverCleanupContinuation cont = cls;
734 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
735 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
738 GNUNET_CONTAINER_heap_remove_node (node);
740 if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) == 0)
742 if (GNUNET_CONTAINER_heap_get_size (ns_task_heap) == 0)
746 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
747 &cleanup_pending_ns_tasks,
758 * Helper function to free resolver handle
760 * @param rh the handle to free
763 free_resolver_handle (struct ResolverHandle* rh)
765 struct AuthorityChain *ac;
766 struct AuthorityChain *ac_next;
771 ac = rh->authority_chain_head;
780 if (NULL != rh->get_handle)
781 GNUNET_DHT_get_stop (rh->get_handle);
783 if (NULL != rh->dns_raw_packet)
784 GNUNET_free (rh->dns_raw_packet);
786 if (NULL != rh->namestore_task)
787 GNUNET_NAMESTORE_cancel (rh->namestore_task);
788 rh->namestore_task = NULL;
790 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
791 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
793 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
794 GNUNET_SCHEDULER_cancel (rh->timeout_task);
796 if (NULL != rh->dns_sock)
797 GNUNET_NETWORK_socket_close (rh->dns_sock);
798 if (NULL != rh->dns_resolver_handle)
799 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
801 if (NULL != rh->rd.data)
802 GNUNET_free ((void*)(rh->rd.data));
810 * @param rh resolver handle
811 * @param nsh name shorten handle
814 finish_shorten (struct ResolverHandle *rh,
815 struct NameShortenHandle *nsh);
819 * @param rh resolver handle
820 * @param nah get name authority handle
823 finish_get_auth (struct ResolverHandle *rh,
824 struct GetNameAuthorityHandle* rlh);
829 gns_resolver_cleanup (ResolverCleanupContinuation cont)
832 struct GetPseuAuthorityHandle *tmp;
836 for (tmp = gph_head; tmp != NULL; tmp = gph_head)
838 if (tmp->get_handle != NULL)
839 GNUNET_DHT_get_stop (tmp->get_handle);
840 tmp->get_handle = NULL;
841 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
842 GNUNET_SCHEDULER_cancel (tmp->timeout);
843 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
845 if (NULL != tmp->namestore_task)
846 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
847 tmp->namestore_task = NULL;
848 GNUNET_free (tmp->auth);
849 GNUNET_CRYPTO_rsa_key_free (tmp->key);
850 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, tmp);
854 while (NULL != rlh_head)
856 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
859 while (NULL != nsh_head)
861 finish_shorten (nsh_head, nsh_head->proc_cls);
864 while (NULL != nah_head)
866 finish_get_auth (nah_head, nah_head->proc_cls);
869 s = GNUNET_CONTAINER_heap_get_size (dht_lookup_heap);
870 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
871 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
873 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
874 &cleanup_pending_background_queries,
876 else if (0 != GNUNET_CONTAINER_heap_get_size (ns_task_heap))
878 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
879 &cleanup_pending_ns_tasks,
890 * Callback when record data is put into namestore
892 * @param cls the closure
893 * @param success GNUNET_OK on success
894 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
897 on_namestore_record_put_result (void *cls,
901 struct NamestoreBGTask *nbg = cls;
903 GNUNET_CONTAINER_heap_remove_node (nbg->node);
906 if (GNUNET_NO == success)
908 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
909 "GNS_NS: records already in namestore\n");
912 else if (GNUNET_YES == success)
914 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
915 "GNS_NS: records successfully put in namestore\n");
919 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
920 "GNS_NS: Error putting records into namestore: %s\n", emsg);
924 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
926 struct ResolverHandle *rh = cls;
928 if (rh->timeout_cont)
929 rh->timeout_cont (rh->timeout_cont_cls, tc);
933 * Processor for background lookups in the DHT
935 * @param cls closure (NULL)
936 * @param rd_count number of records found (not 0)
937 * @param rd record data
940 background_lookup_result_processor (void *cls,
942 const struct GNUNET_NAMESTORE_RecordData *rd)
944 //We could do sth verbose/more useful here but it doesn't make any difference
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
946 "GNS_BG: background dht lookup finished. (%d results)\n",
951 * Handle timeout for DHT requests
953 * @param cls the request handle as closure
954 * @param tc the task context
957 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
959 struct ResolverHandle *rh = cls;
960 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
961 char new_name[MAX_DNS_NAME_LENGTH];
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
965 rh->id, rh->name, rh->timeout.rel_value);
967 * Start resolution in bg
969 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
970 rh->name, GNUNET_GNS_TLD);
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
974 rh->id, new_name, rlh->record_type);
976 gns_resolver_lookup_record (rh->authority,
977 rh->private_local_zone,
981 GNUNET_TIME_UNIT_FOREVER_REL,
983 &background_lookup_result_processor,
985 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
987 GNUNET_DHT_get_stop (rh->get_handle);
988 rh->get_handle = NULL;
989 rh->proc (rh->proc_cls, rh, 0, NULL);
994 * Function called when we get a result from the dht
995 * for our record query
997 * @param cls the request handle
998 * @param exp lifetime
999 * @param key the key the record was stored under
1000 * @param get_path get path
1001 * @param get_path_length get path length
1002 * @param put_path put path
1003 * @param put_path_length put path length
1004 * @param type the block type
1005 * @param size the size of the record
1006 * @param data the record data
1009 process_record_result_dht (void* cls,
1010 struct GNUNET_TIME_Absolute exp,
1011 const struct GNUNET_HashCode * key,
1012 const struct GNUNET_PeerIdentity *get_path,
1013 unsigned int get_path_length,
1014 const struct GNUNET_PeerIdentity *put_path,
1015 unsigned int put_path_length,
1016 enum GNUNET_BLOCK_Type type,
1017 size_t size, const void *data)
1019 struct ResolverHandle *rh;
1020 struct RecordLookupHandle *rlh;
1021 struct GNSNameRecordBlock *nrb;
1022 uint32_t num_records;
1024 char* rd_data = (char*)data;
1028 rh = (struct ResolverHandle *)cls;
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1030 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
1032 rlh = (struct RecordLookupHandle *) rh->proc_cls;
1033 nrb = (struct GNSNameRecordBlock*)data;
1035 /* stop lookup and timeout task */
1036 GNUNET_DHT_get_stop (rh->get_handle);
1037 rh->get_handle = NULL;
1039 if (rh->dht_heap_node != NULL)
1041 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1042 rh->dht_heap_node = NULL;
1045 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1047 GNUNET_SCHEDULER_cancel (rh->timeout_task);
1048 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1051 rh->get_handle = NULL;
1052 name = (char*)&nrb[1];
1053 num_records = ntohl (nrb->rd_count);
1055 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1056 struct NamestoreBGTask *ns_heap_root;
1057 struct NamestoreBGTask *namestore_bg_task;
1059 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
1060 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
1062 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1067 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1068 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1072 for (i=0; i<num_records; i++)
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1075 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1076 rh->id, name, rh->name);
1077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1078 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1079 rh->id, rd[i].record_type, rlh->record_type);
1080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1082 rh->id, rd[i].data_size);
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "GNS_PHASE_REC-%llu: Got flag %d\n",
1085 rh->id, rd[i].flags);
1087 if ((strcmp (name, rh->name) == 0) &&
1088 (rd[i].record_type == rlh->record_type))
1096 * FIXME check pubkey against existing key in namestore?
1097 * https://gnunet.org/bugs/view.php?id=2179
1099 if (max_allowed_ns_tasks <=
1100 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1102 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1103 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1106 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1110 /* Save to namestore */
1111 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1112 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1119 &on_namestore_record_put_result, //cont
1122 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1124 GNUNET_TIME_absolute_get().abs_value);
1128 rh->proc (rh->proc_cls, rh, num_records, rd);
1130 rh->proc (rh->proc_cls, rh, 0, NULL);
1137 * Start DHT lookup for a (name -> query->record_type) record in
1138 * rh->authority's zone
1140 * @param rh the pending gns query context
1143 resolve_record_dht (struct ResolverHandle *rh)
1146 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1147 struct GNUNET_HashCode lookup_key;
1148 struct GNUNET_HashCode name_hash_double;
1149 struct GNUNET_HashCode zone_hash_double;
1150 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
1151 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1152 struct ResolverHandle *rh_heap_root;
1154 GNUNET_CRYPTO_short_hash (rh->name, strlen (rh->name), &name_hash);
1155 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
1156 GNUNET_CRYPTO_short_hash_double (&rh->authority, &zone_hash_double);
1157 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
1158 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
1160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1161 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1162 rh->id, rh->name, (char*)&lookup_key_string);
1164 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1165 rh->dht_heap_node = NULL;
1167 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1170 * Update timeout if necessary
1172 if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "GNS_PHASE_REC-%llu: Adjusting timeout\n", rh->id);
1178 * Set timeout for authority lookup phase to 1/2
1180 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1181 GNUNET_TIME_relative_divide (rh->timeout, 2),
1182 &handle_lookup_timeout,
1185 rh->timeout_cont = &dht_lookup_timeout;
1186 rh->timeout_cont_cls = rh;
1190 if (max_allowed_background_queries <=
1191 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1193 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1194 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1195 rh_heap_root->get_handle = NULL;
1196 rh_heap_root->dht_heap_node = NULL;
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1200 rh->id, rh_heap_root->name);
1201 rh_heap_root->proc (rh_heap_root->proc_cls,
1206 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1208 GNUNET_TIME_absolute_get ().abs_value);
1211 xquery = htonl (rlh->record_type);
1213 GNUNET_assert (rh->get_handle == NULL);
1214 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1215 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1217 DHT_GNS_REPLICATION_LEVEL,
1221 &process_record_result_dht,
1228 * Namestore calls this function if we have record for this name.
1229 * (or with rd_count=0 to indicate no matches)
1231 * @param cls the pending query
1232 * @param key the key of the zone we did the lookup
1233 * @param expiration expiration date of the namestore entry
1234 * @param name the name for which we need an authority
1235 * @param rd_count the number of records with 'name'
1236 * @param rd the record data
1237 * @param signature the signature of the authority for the record data
1240 process_record_result_ns (void* cls,
1241 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1242 struct GNUNET_TIME_Absolute expiration,
1243 const char *name, unsigned int rd_count,
1244 const struct GNUNET_NAMESTORE_RecordData *rd,
1245 const struct GNUNET_CRYPTO_RsaSignature *signature)
1247 struct ResolverHandle *rh;
1248 struct RecordLookupHandle *rlh;
1249 struct GNUNET_TIME_Relative remaining_time;
1250 struct GNUNET_CRYPTO_ShortHashCode zone;
1251 struct GNUNET_TIME_Absolute et;
1254 rh = (struct ResolverHandle *) cls;
1255 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1257 rh->namestore_task = NULL;
1258 GNUNET_CRYPTO_short_hash (key,
1259 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1261 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1269 rh->status |= RSL_RECORD_EXISTS;
1271 if (remaining_time.rel_value == 0)
1273 rh->status |= RSL_RECORD_EXPIRED;
1280 * Lookup terminated and no results
1282 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1283 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1286 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1287 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1290 * Our zone and no result? Cannot resolve TT
1292 rh->proc(rh->proc_cls, rh, 0, NULL);
1298 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1299 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1301 for (i=0; i<rd_count;i++)
1303 if (rd[i].record_type != rlh->record_type)
1306 if (ignore_pending_records &&
1307 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1309 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1310 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1315 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1316 et.abs_value = rd[i].expiration_time;
1317 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
1320 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1321 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1331 if (rh->answered == 0)
1333 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1334 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1335 rh->proc(rh->proc_cls, rh, 0, NULL);
1339 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1340 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1341 rh->id, rh->answered, rd_count);
1343 rh->proc(rh->proc_cls, rh, rd_count, rd);
1349 * VPN redirect result callback
1351 * @param cls the resolver handle
1352 * @param af the requested address family
1353 * @param address in_addr(6) respectively
1356 process_record_result_vpn (void* cls, int af, const void *address)
1358 struct ResolverHandle *rh = cls;
1359 struct RecordLookupHandle *rlh;
1360 struct GNUNET_NAMESTORE_RecordData rd;
1362 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1364 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1365 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1369 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1370 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1372 if (rlh->record_type != GNUNET_GNS_RECORD_A)
1374 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1375 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1377 rh->proc (rh->proc_cls, rh, 0, NULL);
1380 rd.record_type = GNUNET_GNS_RECORD_A;
1381 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1383 rd.data_size = sizeof (struct in_addr);
1385 rh->proc (rh->proc_cls, rh, 1, &rd);
1388 else if (af == AF_INET6)
1390 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1391 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1393 if (rlh->record_type != GNUNET_GNS_RECORD_AAAA)
1395 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1396 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1398 rh->proc (rh->proc_cls, rh, 0, NULL);
1401 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1402 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1404 rd.data_size = sizeof (struct in6_addr);
1406 rh->proc (rh->proc_cls, rh, 1, &rd);
1410 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1411 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1413 rh->proc (rh->proc_cls, rh, 0, NULL);
1420 * Process VPN lookup result for record
1422 * @param cls the record lookup handle
1423 * @param rh resolver handle
1424 * @param rd_count number of results (1)
1425 * @param rd record data containing the result
1428 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1429 unsigned int rd_count,
1430 const struct GNUNET_NAMESTORE_RecordData *rd)
1432 struct RecordLookupHandle* rlh = (struct RecordLookupHandle*) cls;
1436 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1437 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1440 /* give up, cannot resolve */
1441 finish_lookup(rh, rlh, 0, NULL);
1445 /* results found yay */
1446 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1447 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1449 finish_lookup(rh, rlh, rd_count, rd);
1454 * Sends a UDP dns query to a nameserver specified in the rh
1456 * @param rh the resolver handle
1459 send_dns_packet (struct ResolverHandle *rh);
1463 handle_dns_resolver (void *cls,
1464 const struct sockaddr *addr,
1467 struct ResolverHandle *rh = cls;
1468 struct RecordLookupHandle *rlh = rh->proc_cls;
1469 struct GNUNET_NAMESTORE_RecordData rd;
1470 struct sockaddr_in *sai;
1471 struct sockaddr_in6 *sai6;
1475 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1476 "No address found in DNS!\n");
1477 finish_lookup (rh, rlh, 0, NULL);
1481 if (addrlen == sizeof (struct sockaddr_in))
1483 sai = (struct sockaddr_in*) addr;
1484 rd.record_type = GNUNET_GNS_RECORD_A;
1485 rd.data_size = sizeof (struct in_addr);
1486 rd.data = &sai->sin_addr;
1490 sai6 = (struct sockaddr_in6*) addr;
1491 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1492 rd.data_size = sizeof (struct in6_addr);
1493 rd.data = &sai6->sin6_addr;
1496 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1499 finish_lookup (rh, rlh, 1, &rd);
1504 * Resolve DNS name via local stub resolver
1506 * @param rh the resolver handle
1509 resolve_dns_name (struct ResolverHandle *rh)
1511 struct RecordLookupHandle *rlh = rh->proc_cls;
1514 if ((rlh->record_type != GNUNET_GNS_RECORD_A) &&
1515 (rlh->record_type != GNUNET_GNS_RECORD_AAAA))
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1518 "Can only resolve A/AAAA via stub... abort\n");
1519 finish_lookup (rh, rlh, 0, NULL);
1523 if (rlh->record_type == GNUNET_GNS_RECORD_A)
1528 //GNUNET_RESOLVER_connect (cfg); FIXME into init
1530 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1533 &handle_dns_resolver,
1539 * Read DNS udp packet from socket
1541 * @param cls the resolver handle
1542 * @param tc task context
1545 read_dns_response (void *cls,
1546 const struct GNUNET_SCHEDULER_TaskContext *tc)
1548 struct ResolverHandle *rh = cls;
1549 struct RecordLookupHandle *rlh = rh->proc_cls;
1550 char buf[UINT16_MAX];
1552 struct sockaddr_in addr;
1554 struct GNUNET_DNSPARSER_Packet *packet;
1555 struct GNUNET_NAMESTORE_RecordData rd;
1556 int found_delegation = GNUNET_NO;
1557 int found_cname = GNUNET_NO;
1558 char* delegation_name = NULL;
1559 int zone_offset = 0;
1562 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1563 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1565 /* timeout or shutdown */
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1567 "Terminating DNS query %d\n", tc->reason);
1568 finish_lookup (rh, rlh, 0, NULL);
1572 addrlen = sizeof (addr);
1573 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1575 (struct sockaddr*) &addr,
1580 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1581 finish_lookup (rh, rlh, 0, NULL);
1585 packet = GNUNET_DNSPARSER_parse (buf, r);
1589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1590 "Failed to parse DNS reply!\n");
1591 finish_lookup (rh, rlh, 0, NULL);
1595 for (i = 0; i < packet->num_answers; i++)
1597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1598 "Got record type %d (want %d)\n",
1599 packet->answers[i].type,
1601 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1602 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1605 "CNAME... restarting query with %s\n",
1606 packet->answers[i].data.hostname
1608 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1609 found_cname = GNUNET_YES;
1610 //send_dns_packet (rh);
1611 //GNUNET_DNSPARSER_free_packet (packet);
1615 if ((packet->answers[i].type == rlh->record_type) &&
1616 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1620 rd.data = packet->answers[i].data.raw.data;
1621 rd.data_size = packet->answers[i].data.raw.data_len;
1622 rd.record_type = packet->answers[i].type;
1624 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1625 finish_lookup (rh, rlh, 1, &rd);
1626 GNUNET_DNSPARSER_free_packet (packet);
1631 if (GNUNET_YES == found_cname)
1633 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1635 if (0 > zone_offset)
1638 /* restart query with CNAME */
1639 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1642 "Asking same server for %s\n", rh->dns_name);
1643 send_dns_packet (rh);
1647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1648 "Trying system resolver for %s\n", rh->dns_name);
1649 resolve_dns_name (rh);
1652 GNUNET_DNSPARSER_free_packet (packet);
1656 for (i = 0; i < packet->num_authority_records; i++)
1659 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1662 "Found NS delegation!\n");
1663 found_delegation = GNUNET_YES;
1664 delegation_name = packet->authority_records[i].data.hostname;
1669 for (i = 0; i < packet->num_additional_records; i++)
1671 if (found_delegation == GNUNET_NO)
1674 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1675 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1677 GNUNET_assert (sizeof (struct in_addr) ==
1678 packet->authority_records[i].data.raw.data_len);
1680 rh->dns_addr.sin_addr =
1681 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1682 send_dns_packet (rh);
1683 GNUNET_DNSPARSER_free_packet (packet);
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1689 "Nothing useful in DNS reply!\n");
1690 finish_lookup (rh, rlh, 0, NULL);
1691 GNUNET_DNSPARSER_free_packet (packet);
1696 * Sends a UDP dns query to a nameserver specified in the rh
1698 * @param rh the request handle
1701 send_dns_packet (struct ResolverHandle *rh)
1703 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1704 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1707 "Sending %dbyte DNS query\n",
1708 rh->dns_raw_packet_size);
1710 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1712 rh->dns_raw_packet_size,
1713 (struct sockaddr*)&rh->dns_addr,
1714 sizeof (struct sockaddr_in));
1716 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1717 rh->timeout, //FIXME less?
1723 GNUNET_NETWORK_fdset_destroy (rset);
1728 * The final phase of resoution.
1729 * We found a NS RR and want to resolve via DNS
1731 * @param rh the pending lookup handle
1732 * @param rd_count length of record data
1733 * @param rd record data containing VPN RR
1736 resolve_record_dns (struct ResolverHandle *rh,
1738 const struct GNUNET_NAMESTORE_RecordData *rd)
1740 struct GNUNET_DNSPARSER_Query query;
1741 struct GNUNET_DNSPARSER_Packet packet;
1742 struct GNUNET_DNSPARSER_Flags flags;
1743 struct in_addr dnsip;
1744 struct sockaddr_in addr;
1745 struct sockaddr *sa;
1747 struct RecordLookupHandle *rlh = rh->proc_cls;
1749 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1751 /* We cancel here as to not include the ns lookup in the timeout */
1752 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1754 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1755 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1757 /* Start shortening */
1758 if ((rh->priv_key != NULL) &&
1759 (is_canonical (rh->name) == GNUNET_YES))
1761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1762 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1764 start_shorten (rh->authority_chain_head,
1768 for (i = 0; i < rd_count; i++)
1770 /* Synthesize dns name */
1771 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
1773 strcpy (rh->dns_zone, (char*)rd[i].data);
1774 if (0 == strcmp (rh->name, ""))
1775 strcpy (rh->dns_name, (char*)rd[i].data);
1777 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1780 if (rd[i].record_type == GNUNET_GNS_RECORD_A)
1781 dnsip = *((struct in_addr*)rd[i].data);
1784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1785 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1789 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1790 if (rh->dns_sock == NULL)
1792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1793 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1795 finish_lookup (rh, rlh, 0, NULL);
1799 memset (&addr, 0, sizeof (struct sockaddr_in));
1800 sa = (struct sockaddr *) &addr;
1801 sa->sa_family = AF_INET;
1802 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1804 sizeof (struct sockaddr_in)))
1806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1807 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1809 finish_lookup (rh, rlh, 0, NULL);
1813 /*TODO create dnsparser query, serialize, sendto, handle reply*/
1814 query.name = rh->dns_name;
1815 query.type = rlh->record_type;
1816 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1817 memset (&flags, 0, sizeof (flags));
1818 flags.recursion_desired = 1;
1819 flags.checking_disabled = 1;
1820 packet.queries = &query;
1821 packet.answers = NULL;
1822 packet.authority_records = NULL;
1823 packet.num_queries = 1;
1824 packet.num_answers = 0;
1825 packet.num_authority_records = 0;
1826 packet.num_additional_records = 0;
1827 packet.flags = flags;
1829 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1831 &rh->dns_raw_packet,
1832 &rh->dns_raw_packet_size))
1834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1835 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1837 GNUNET_NETWORK_socket_close (rh->dns_sock);
1838 finish_lookup (rh, rlh, 0, NULL);
1842 rh->dns_addr.sin_family = AF_INET;
1843 rh->dns_addr.sin_port = htons (53); //domain
1844 rh->dns_addr.sin_addr = dnsip;
1845 #if HAVE_SOCKADDR_IN_SIN_LEN
1846 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1849 send_dns_packet (rh);
1854 * The final phase of resoution.
1855 * We found a VPN RR and want to request an IPv4/6 address
1857 * @param rh the pending lookup handle
1858 * @param rd_count length of record data
1859 * @param rd record data containing VPN RR
1862 resolve_record_vpn (struct ResolverHandle *rh,
1864 const struct GNUNET_NAMESTORE_RecordData *rd)
1866 struct RecordLookupHandle *rlh = rh->proc_cls;
1867 struct GNUNET_HashCode serv_desc;
1868 struct vpn_data* vpn;
1871 /* We cancel here as to not include the ns lookup in the timeout */
1872 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1874 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1875 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1877 /* Start shortening */
1878 if ((rh->priv_key != NULL) &&
1879 (is_canonical (rh->name) == GNUNET_YES))
1881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1882 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1884 start_shorten (rh->authority_chain_head,
1888 vpn = (struct vpn_data*)rd->data;
1891 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1892 strlen ((char*)&vpn[1]) + 1,
1894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1895 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1898 GNUNET_h2s (&vpn->peer));
1900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1901 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1904 GNUNET_h2s (&serv_desc));
1905 rh->proc = &handle_record_vpn;
1909 if (rlh->record_type == GNUNET_GNS_RECORD_A)
1915 if (NULL == vpn_handle)
1917 vpn_handle = GNUNET_VPN_connect (cfg);
1918 if (NULL == vpn_handle)
1920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1921 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1922 finish_lookup (rh, rh->proc_cls, 0, NULL);
1927 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1928 af, ntohs (vpn->proto),
1929 (struct GNUNET_PeerIdentity *)&vpn->peer,
1932 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1933 &process_record_result_vpn,
1939 * The final phase of resolution.
1940 * rh->name is a name that is canonical and we do not have a delegation.
1941 * Query namestore for this record
1943 * @param rh the pending lookup handle
1946 resolve_record_ns(struct ResolverHandle *rh)
1948 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1950 /* We cancel here as to not include the ns lookup in the timeout */
1951 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1953 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1954 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1956 /* Start shortening */
1957 if ((rh->priv_key != NULL) &&
1958 (is_canonical (rh->name) == GNUNET_YES))
1960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1961 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1963 start_shorten (rh->authority_chain_head,
1968 * Try to resolve this record in our namestore.
1969 * The name to resolve is now in rh->authority_name
1970 * since we tried to resolve it to an authority
1973 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1977 &process_record_result_ns,
1984 * Handle timeout for DHT requests
1986 * @param cls the request handle as closure
1987 * @param tc the task context
1990 dht_authority_lookup_timeout(void *cls,
1991 const struct GNUNET_SCHEDULER_TaskContext *tc)
1993 struct ResolverHandle *rh = cls;
1994 struct RecordLookupHandle *rlh = rh->proc_cls;
1995 char new_name[MAX_DNS_NAME_LENGTH];
1997 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1998 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
1999 rh->id, rh->authority_name, rh->timeout.rel_value);
2001 rh->status |= RSL_TIMED_OUT;
2003 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2005 if (NULL != rh->get_handle)
2006 GNUNET_DHT_get_stop (rh->get_handle);
2008 rh->get_handle = NULL;
2010 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
2012 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2013 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
2015 rh->proc(rh->proc_cls, rh, 0, NULL);
2019 if (strcmp(rh->name, "") == 0)
2022 * promote authority back to name and try to resolve record
2024 strcpy(rh->name, rh->authority_name);
2025 rh->proc(rh->proc_cls, rh, 0, NULL);
2030 * Start resolution in bg
2032 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
2033 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
2034 //strcpy(new_name, rh->name);
2035 //strcpy(new_name+strlen(new_name), ".");
2036 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
2038 strcpy(rh->name, new_name);
2040 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2041 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
2042 rh->id, rh->name, rlh->record_type);
2044 gns_resolver_lookup_record(rh->authority,
2045 rh->private_local_zone,
2049 GNUNET_TIME_UNIT_FOREVER_REL,
2051 &background_lookup_result_processor,
2054 rh->proc(rh->proc_cls, rh, 0, NULL);
2058 static void resolve_delegation_dht(struct ResolverHandle *rh);
2061 static void resolve_delegation_ns(struct ResolverHandle *rh);
2065 * Namestore resolution for delegation finished. Processing result.
2067 * @param cls the closure
2068 * @param rh resolver handle
2069 * @param rd_count number of results (always 0)
2070 * @param rd record data (always NULL)
2073 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2074 unsigned int rd_count,
2075 const struct GNUNET_NAMESTORE_RecordData *rd);
2079 * This is a callback function that checks for key revocation
2081 * @param cls the pending query
2082 * @param key the key of the zone we did the lookup
2083 * @param expiration expiration date of the record data set in the namestore
2084 * @param name the name for which we need an authority
2085 * @param rd_count the number of records with 'name'
2086 * @param rd the record data
2087 * @param signature the signature of the authority for the record data
2090 process_pkey_revocation_result_ns (void *cls,
2091 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2092 struct GNUNET_TIME_Absolute expiration,
2094 unsigned int rd_count,
2095 const struct GNUNET_NAMESTORE_RecordData *rd,
2096 const struct GNUNET_CRYPTO_RsaSignature *signature)
2098 struct ResolverHandle *rh = cls;
2099 struct GNUNET_TIME_Relative remaining_time;
2102 rh->namestore_task = NULL;
2103 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2105 for (i = 0; i < rd_count; i++)
2107 if (rd[i].record_type == GNUNET_GNS_RECORD_REV)
2109 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2110 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2112 rh->status |= RSL_PKEY_REVOKED;
2113 rh->proc (rh->proc_cls, rh, 0, NULL);
2118 if ((name == NULL) ||
2119 (remaining_time.rel_value == 0))
2121 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2122 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2125 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2127 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2128 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2129 rh->id, "+.gnunet", GNUNET_GNS_RECORD_REV);
2131 gns_resolver_lookup_record(rh->authority,
2132 rh->private_local_zone,
2133 GNUNET_GNS_RECORD_REV,
2136 GNUNET_TIME_UNIT_FOREVER_REL,
2138 &background_lookup_result_processor,
2142 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2143 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2146 * We are done with PKEY resolution if name is empty
2147 * else resolve again with new authority
2149 if (strcmp (rh->name, "") == 0)
2150 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2152 resolve_delegation_ns (rh);
2159 * Callback when record data is put into namestore
2161 * @param cls the closure
2162 * @param success GNUNET_OK on success
2163 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2166 on_namestore_delegation_put_result(void *cls,
2170 struct NamestoreBGTask *nbg = cls;
2172 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2175 if (GNUNET_NO == success)
2177 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2178 "GNS_NS: records already in namestore\n");
2181 else if (GNUNET_YES == success)
2183 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2184 "GNS_NS: records successfully put in namestore\n");
2188 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2189 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2193 * Function called when we get a result from the dht
2194 * for our query. Recursively tries to resolve authorities
2197 * @param cls the request handle
2198 * @param exp lifetime
2199 * @param key the key the record was stored under
2200 * @param get_path get path
2201 * @param get_path_length get path length
2202 * @param put_path put path
2203 * @param put_path_length put path length
2204 * @param type the block type
2205 * @param size the size of the record
2206 * @param data the record data
2209 process_delegation_result_dht(void* cls,
2210 struct GNUNET_TIME_Absolute exp,
2211 const struct GNUNET_HashCode * key,
2212 const struct GNUNET_PeerIdentity *get_path,
2213 unsigned int get_path_length,
2214 const struct GNUNET_PeerIdentity *put_path,
2215 unsigned int put_path_length,
2216 enum GNUNET_BLOCK_Type type,
2217 size_t size, const void *data)
2219 struct ResolverHandle *rh;
2220 struct GNSNameRecordBlock *nrb;
2221 uint32_t num_records;
2223 char* rd_data = (char*) data;
2226 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
2227 struct GNUNET_HashCode zone_hash_double, name_hash_double;
2229 rh = (struct ResolverHandle *)cls;
2231 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2232 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2237 nrb = (struct GNSNameRecordBlock*)data;
2239 /* stop dht lookup and timeout task */
2240 GNUNET_DHT_get_stop (rh->get_handle);
2242 rh->get_handle = NULL;
2244 if (rh->dht_heap_node != NULL)
2246 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2247 rh->dht_heap_node = NULL;
2250 num_records = ntohl(nrb->rd_count);
2251 name = (char*)&nrb[1];
2253 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2254 struct NamestoreBGTask *ns_heap_root;
2255 struct NamestoreBGTask *namestore_bg_task;
2257 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2258 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2260 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2265 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2266 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2271 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2272 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2273 rh->id, name, rh->authority_name);
2274 for (i=0; i<num_records; i++)
2277 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2278 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2279 rh->id, name, rh->authority_name);
2280 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2281 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2282 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2283 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2284 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2285 rh->id, rd[i].data_size);
2286 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2287 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2288 rh->id, rd[i].flags);
2290 if ((rd[i].record_type == GNUNET_GNS_RECORD_VPN) ||
2291 (rd[i].record_type == GNUNET_GNS_RECORD_NS) ||
2292 (rd[i].record_type == GNUNET_GNS_RECORD_CNAME))
2295 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2297 if (strcmp(rh->name, "") == 0)
2298 strcpy(rh->name, rh->authority_name);
2300 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2301 rh->name, rh->authority_name); //FIXME ret
2306 if ((strcmp(name, rh->authority_name) == 0) &&
2307 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
2309 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2310 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2313 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2314 struct AuthorityChain *auth =
2315 GNUNET_malloc(sizeof(struct AuthorityChain));
2316 auth->zone = rh->authority;
2317 memset(auth->name, 0, strlen(rh->authority_name)+1);
2318 strcpy(auth->name, rh->authority_name);
2319 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2320 rh->authority_chain_tail,
2323 if (NULL != rh->rd.data)
2324 GNUNET_free ((void*)rh->rd.data);
2326 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2327 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2328 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2331 /** try to import pkey if private key available */
2332 //if (rh->priv_key && is_canonical (rh->name))
2333 // process_discovered_authority(name, auth->zone,
2334 // rh->authority_chain_tail->zone,
2341 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2342 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2343 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2344 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2346 /* Save to namestore */
2347 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
2350 if (max_allowed_ns_tasks <=
2351 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2353 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2354 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2356 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2357 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
2361 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2363 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2365 GNUNET_TIME_absolute_get().abs_value);
2366 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2373 &on_namestore_delegation_put_result, //cont
2374 namestore_bg_task); //cls
2383 * FIXME in this case. should we ask namestore again?
2385 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2386 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2387 rh->id, rh->authority_name, rh->name);
2389 if (strcmp(rh->name, "") == 0)
2391 /* Start shortening */
2392 if ((rh->priv_key != NULL) &&
2393 (is_canonical (rh->name) == GNUNET_YES))
2395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2396 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2398 start_shorten (rh->authority_chain_head,
2403 rh->proc = &handle_delegation_ns;
2406 /* Check for key revocation and delegate */
2407 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2410 GNUNET_GNS_RECORD_REV,
2411 &process_pkey_revocation_result_ns,
2418 * No pkey but name exists
2421 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2422 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2423 rh->id, rh->authority_name, rh->name);
2424 if (strcmp(rh->name, "") == 0)
2425 strcpy(rh->name, rh->authority_name);
2427 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2428 rh->name, rh->authority_name); //FIXME ret
2430 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2431 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2433 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2435 rh->proc(rh->proc_cls, rh, 0, NULL);
2438 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2439 +(MAX_DNS_NAME_LENGTH*2)
2440 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2441 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2445 expand_plus(char* dest, char* src, char* repl)
2448 unsigned int s_len = strlen(src)+1;
2450 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2451 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2455 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2456 "GNS_POSTPROCESS: %s to short\n", src);
2458 /* no postprocessing */
2459 memcpy(dest, src, s_len+1);
2463 if (0 == strcmp(src+s_len-3, ".+"))
2465 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2466 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2467 memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2471 pos += strlen(repl);
2472 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2473 "GNS_POSTPROCESS: Expanded to %s\n", dest);
2477 memcpy(dest, src, s_len+1);
2485 finish_lookup (struct ResolverHandle *rh,
2486 struct RecordLookupHandle* rlh,
2487 unsigned int rd_count,
2488 const struct GNUNET_NAMESTORE_RecordData *rd)
2491 char new_rr_data[MAX_DNS_NAME_LENGTH];
2492 char new_mx_data[MAX_MX_LENGTH];
2493 char new_soa_data[MAX_SOA_LENGTH];
2494 char new_srv_data[MAX_SRV_LENGTH];
2495 struct srv_data *old_srv;
2496 struct srv_data *new_srv;
2497 struct soa_data *old_soa;
2498 struct soa_data *new_soa;
2499 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2502 unsigned int offset;
2504 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2506 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2507 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2510 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2513 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2515 for (i = 0; i < rd_count; i++)
2518 if (rd[i].record_type != GNUNET_GNS_RECORD_NS &&
2519 rd[i].record_type != GNUNET_GNS_RECORD_PTR &&
2520 rd[i].record_type != GNUNET_GNS_RECORD_CNAME &&
2521 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
2522 rd[i].record_type != GNUNET_GNS_RECORD_SOA &&
2523 rd[i].record_type != GNUNET_GNS_RECORD_SRV)
2525 p_rd[i].data = rd[i].data;
2530 * for all those records we 'should'
2531 * also try to resolve the A/AAAA records (RFC1035)
2532 * This is a feature and not important
2535 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2536 "GNS_POSTPROCESS: Postprocessing\n");
2538 if (strcmp(rh->name, "+") == 0)
2539 repl_string = rlh->name;
2541 repl_string = rlh->name+strlen(rh->name)+1;
2544 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
2546 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2547 offset = sizeof(uint16_t);
2548 pos = new_mx_data+offset;
2549 expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2551 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2552 p_rd[i].data = new_mx_data;
2553 p_rd[i].data_size = offset;
2555 else if (rd[i].record_type == GNUNET_GNS_RECORD_SRV)
2558 * Prio, weight and port
2560 new_srv = (struct srv_data*)new_srv_data;
2561 old_srv = (struct srv_data*)rd[i].data;
2562 new_srv->prio = old_srv->prio;
2563 new_srv->weight = old_srv->weight;
2564 new_srv->port = old_srv->port;
2565 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2567 p_rd[i].data = new_srv_data;
2568 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2570 else if (rd[i].record_type == GNUNET_GNS_RECORD_SOA)
2572 /* expand mname and rname */
2573 old_soa = (struct soa_data*)rd[i].data;
2574 new_soa = (struct soa_data*)new_soa_data;
2575 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2576 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2577 offset = strlen ((char*)&new_soa[1]) + 1;
2578 expand_plus((char*)&new_soa[1] + offset,
2579 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2581 p_rd[i].data_size = sizeof (struct soa_data)
2583 + strlen ((char*)&new_soa[1] + offset);
2584 p_rd[i].data = new_soa_data;
2589 expand_plus(pos, (char*)rd[i].data, repl_string);
2590 p_rd[i].data_size = strlen(new_rr_data)+1;
2591 p_rd[i].data = new_rr_data;
2596 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2598 free_resolver_handle (rh);
2602 * Process DHT lookup result for record.
2604 * @param cls the closure
2605 * @param rh resolver handle
2606 * @param rd_count number of results
2607 * @param rd record data
2610 handle_record_dht(void* cls, struct ResolverHandle *rh,
2611 unsigned int rd_count,
2612 const struct GNUNET_NAMESTORE_RecordData *rd)
2614 struct RecordLookupHandle* rlh;
2616 rlh = (struct RecordLookupHandle*)cls;
2619 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2620 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2622 /* give up, cannot resolve */
2623 finish_lookup(rh, rlh, 0, NULL);
2627 /* results found yay */
2628 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2629 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2631 finish_lookup(rh, rlh, rd_count, rd);
2639 * Process namestore lookup result for record.
2641 * @param cls the closure
2642 * @param rh resolver handle
2643 * @param rd_count number of results
2644 * @param rd record data
2647 handle_record_ns (void* cls, struct ResolverHandle *rh,
2648 unsigned int rd_count,
2649 const struct GNUNET_NAMESTORE_RecordData *rd)
2651 struct RecordLookupHandle* rlh;
2652 rlh = (struct RecordLookupHandle*) cls;
2653 int check_dht = GNUNET_YES;
2657 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2658 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2663 * There are 5 conditions that have to met for us to consult the DHT:
2664 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2665 * 2. No entry in the NS existed AND
2666 * 3. The zone queried is not the local resolver's zone AND
2667 * 4. The name that was looked up is '+'
2668 * because if it was any other canonical name we either already queried
2669 * the DHT for the authority in the authority lookup phase (and thus
2670 * would already have an entry in the NS for the record)
2671 * 5. We are not in cache only mode
2673 if (((rh->status & RSL_RECORD_EXPIRED) != 0) &&
2674 ((rh->status & RSL_RECORD_EXISTS) == 0) )
2675 check_dht = GNUNET_NO;
2677 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2678 &rh->private_local_zone))
2679 check_dht = GNUNET_NO;
2681 if ((strcmp (rh->name, "+") != 0) && (is_srv (rh->name) == GNUNET_YES))
2682 check_dht = GNUNET_NO;
2685 if (rh->only_cached == GNUNET_YES)
2686 check_dht = GNUNET_NO;
2688 if (GNUNET_YES == check_dht)
2690 rh->proc = &handle_record_dht;
2691 resolve_record_dht(rh);
2694 /* give up, cannot resolve */
2695 finish_lookup(rh, rlh, 0, NULL);
2699 /* results found yay */
2700 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2701 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2703 finish_lookup(rh, rlh, rd_count, rd);
2710 * Move one level up in the domain hierarchy and return the
2711 * passed top level domain.
2713 * @param name the domain
2714 * @param dest the destination where the tld will be put
2717 pop_tld(char* name, char* dest)
2721 if (is_canonical (name) == GNUNET_YES)
2728 for (len = strlen(name); len > 0; len--)
2730 if (*(name+len) == '.')
2740 strcpy(dest, (name+len+1));
2744 * Checks if name is in tld
2746 * @param name the name to check
2747 * @param tld the TLD to check for
2748 * @return GNUNET_YES or GNUNET_NO
2751 is_tld(const char* name, const char* tld)
2755 if (strlen(name) <= strlen(tld))
2760 offset = strlen(name)-strlen(tld);
2761 if (strcmp(name+offset, tld) != 0)
2763 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2764 "%s is not in .%s TLD\n", name, tld);
2771 * DHT resolution for delegation finished. Processing result.
2773 * @param cls the closure
2774 * @param rh resolver handle
2775 * @param rd_count number of results (always 0)
2776 * @param rd record data (always NULL)
2779 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2780 unsigned int rd_count,
2781 const struct GNUNET_NAMESTORE_RecordData *rd)
2783 struct RecordLookupHandle* rlh;
2784 rlh = (struct RecordLookupHandle*) cls;
2787 if (strcmp(rh->name, "") == 0)
2789 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
2791 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2792 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2794 finish_lookup(rh, rlh, rd_count, rd);
2797 /* We resolved full name for delegation. resolving record */
2798 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2799 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2801 strcpy(rh->name, "+\0");
2802 rh->proc = &handle_record_ns;
2803 resolve_record_ns(rh);
2808 * we still have some left
2810 if (is_canonical (rh->name) == GNUNET_YES)
2812 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2813 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2816 rh->proc = &handle_record_ns;
2817 resolve_record_ns(rh);
2820 /* give up, cannot resolve */
2821 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2822 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2824 finish_lookup(rh, rlh, 0, NULL);
2829 * Start DHT lookup for a name -> PKEY (compare NS) record in
2830 * rh->authority's zone
2832 * @param rh the pending gns query
2835 resolve_delegation_dht(struct ResolverHandle *rh)
2838 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2839 struct GNUNET_HashCode name_hash_double;
2840 struct GNUNET_HashCode zone_hash_double;
2841 struct GNUNET_HashCode lookup_key;
2842 struct ResolverHandle *rh_heap_root;
2844 pop_tld(rh->name, rh->authority_name);
2845 GNUNET_CRYPTO_short_hash(rh->authority_name,
2846 strlen(rh->authority_name),
2848 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2849 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2850 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2852 rh->dht_heap_node = NULL;
2854 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2856 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
2857 // &dht_authority_lookup_timeout,
2859 rh->timeout_cont = &dht_authority_lookup_timeout;
2860 rh->timeout_cont_cls = rh;
2864 if (max_allowed_background_queries <=
2865 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2867 /* terminate oldest lookup */
2868 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2869 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2870 rh_heap_root->get_handle = NULL;
2871 rh_heap_root->dht_heap_node = NULL;
2873 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2874 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2875 rh->id, rh_heap_root->authority_name);
2877 rh_heap_root->proc(rh_heap_root->proc_cls,
2882 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2884 GNUNET_TIME_absolute_get().abs_value);
2887 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2889 GNUNET_assert(rh->get_handle == NULL);
2890 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2891 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2893 DHT_GNS_REPLICATION_LEVEL,
2897 &process_delegation_result_dht,
2904 * Namestore resolution for delegation finished. Processing result.
2906 * @param cls the closure
2907 * @param rh resolver handle
2908 * @param rd_count number of results (always 0)
2909 * @param rd record data (always NULL)
2912 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2913 unsigned int rd_count,
2914 const struct GNUNET_NAMESTORE_RecordData *rd)
2916 struct RecordLookupHandle* rlh;
2917 rlh = (struct RecordLookupHandle*) cls;
2918 int check_dht = GNUNET_YES;
2921 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2922 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2923 rh->id, rh->status);
2925 if (rh->status & RSL_PKEY_REVOKED)
2927 finish_lookup (rh, rlh, 0, NULL);
2931 if (strcmp(rh->name, "") == 0)
2934 /* We resolved full name for delegation. resolving record */
2935 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2936 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2938 if (rh->status & RSL_CNAME_FOUND)
2940 if (rlh->record_type == GNUNET_GNS_RECORD_CNAME)
2942 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2943 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2945 strcpy (rh->name, rh->authority_name);
2946 finish_lookup (rh, rlh, rd_count, rd);
2951 if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2953 s_len = strlen (rd->data) - 2;
2954 memcpy (rh->name, rd->data, s_len);
2955 rh->name[s_len] = '\0';
2956 resolve_delegation_ns (rh);
2959 else if (is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2961 gns_resolver_lookup_record (rh->authority,
2962 rh->private_local_zone,
2971 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2972 free_resolver_handle (rh);
2978 strcpy (rh->dns_name, (char*)rd->data);
2979 resolve_dns_name (rh);
2984 else if (rh->status & RSL_DELEGATE_VPN)
2986 if (rlh->record_type == GNUNET_GNS_RECORD_VPN)
2988 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2989 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2991 finish_lookup(rh, rlh, rd_count, rd);
2994 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2995 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2997 GNUNET_assert (NULL != rd);
2998 rh->proc = &handle_record_vpn;
2999 resolve_record_vpn (rh, rd_count, rd);
3002 else if (rh->status & RSL_DELEGATE_NS)
3004 if (rlh->record_type == GNUNET_GNS_RECORD_NS)
3006 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3007 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3009 finish_lookup(rh, rlh, rd_count, rd);
3013 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3014 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3016 GNUNET_assert (NULL != rd);
3017 rh->proc = &handle_record_ns;
3018 resolve_record_dns (rh, rd_count, rd);
3021 else if (rh->status & RSL_DELEGATE_PKEY)
3023 if (rh->status & RSL_PKEY_REVOKED)
3025 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3026 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
3028 finish_lookup (rh, rlh, 0, NULL);
3031 else if (rlh->record_type == GNUNET_GNS_RECORD_PKEY)
3033 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3034 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
3036 finish_lookup(rh, rlh, rd_count, rd);
3040 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3041 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
3043 strcpy(rh->name, "+\0");
3044 rh->proc = &handle_record_ns;
3045 resolve_record_ns(rh);
3049 if (rh->status & RSL_DELEGATE_NS)
3051 if (rlh->record_type == GNUNET_GNS_RECORD_NS)
3053 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3054 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
3056 finish_lookup(rh, rlh, rd_count, rd);
3060 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3061 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
3063 GNUNET_assert (NULL != rd);
3064 rh->proc = &handle_record_ns;
3065 resolve_record_dns (rh, rd_count, rd);
3070 * we still have some left
3071 * check if authority in ns is fresh
3073 * or we are authority
3076 if ((rh->status & RSL_RECORD_EXISTS) &&
3077 !(rh->status & RSL_RECORD_EXPIRED))
3078 check_dht = GNUNET_NO;
3080 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3081 &rh->private_local_zone))
3082 check_dht = GNUNET_NO;
3084 if (rh->only_cached == GNUNET_YES)
3085 check_dht = GNUNET_NO;
3087 if (check_dht == GNUNET_NO)
3089 if (is_canonical (rh->name) == GNUNET_YES)
3091 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3092 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3095 rh->proc = &handle_record_ns;
3096 resolve_record_ns(rh);
3100 /* give up, cannot resolve */
3101 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3102 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3105 finish_lookup(rh, rlh, rd_count, rd);
3106 //rlh->proc(rlh->proc_cls, 0, NULL);
3111 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3112 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3114 rh->proc = &handle_delegation_dht;
3115 resolve_delegation_dht(rh);
3120 * This is a callback function that should give us only PKEY
3121 * records. Used to query the namestore for the authority (PKEY)
3122 * for 'name'. It will recursively try to resolve the
3123 * authority for a given name from the namestore.
3125 * @param cls the pending query
3126 * @param key the key of the zone we did the lookup
3127 * @param expiration expiration date of the record data set in the namestore
3128 * @param name the name for which we need an authority
3129 * @param rd_count the number of records with 'name'
3130 * @param rd the record data
3131 * @param signature the signature of the authority for the record data
3134 process_delegation_result_ns (void* cls,
3135 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3136 struct GNUNET_TIME_Absolute expiration,
3138 unsigned int rd_count,
3139 const struct GNUNET_NAMESTORE_RecordData *rd,
3140 const struct GNUNET_CRYPTO_RsaSignature *signature)
3142 struct ResolverHandle *rh;
3143 struct GNUNET_TIME_Relative remaining_time;
3144 struct GNUNET_CRYPTO_ShortHashCode zone;
3145 char new_name[MAX_DNS_NAME_LENGTH];
3147 struct GNUNET_TIME_Absolute et;
3149 rh = (struct ResolverHandle *)cls;
3150 rh->namestore_task = NULL;
3151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3152 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3155 GNUNET_CRYPTO_short_hash (key,
3156 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3158 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3165 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3167 rh->status |= RSL_RECORD_EXISTS;
3169 if (remaining_time.rel_value == 0)
3171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3172 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3174 rh->status |= RSL_RECORD_EXPIRED;
3179 * No authority found in namestore.
3184 * We did not find an authority in the namestore
3189 * Promote this authority back to a name maybe it is
3192 if (strcmp (rh->name, "") == 0)
3194 /* simply promote back */
3195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3196 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3197 rh->id, rh->authority_name);
3198 strcpy (rh->name, rh->authority_name);
3202 /* add back to existing name */
3203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3204 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3205 rh->id, rh->authority_name, rh->name);
3206 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3207 rh->name, rh->authority_name);
3208 strcpy (rh->name, new_name);
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3210 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3214 rh->proc (rh->proc_cls, rh, 0, NULL);
3219 * We found an authority that may be able to help us
3220 * move on with query
3221 * Note only 1 pkey should have been returned.. anything else would be strange
3223 for (i=0; i < rd_count;i++)
3227 * A CNAME. Like regular DNS this means the is no other record for this
3230 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3232 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3233 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3236 rh->status |= RSL_CNAME_FOUND;
3237 rh->proc (rh->proc_cls, rh, rd_count, rd);
3244 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3246 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3247 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3249 rh->status |= RSL_DELEGATE_VPN;
3250 rh->proc (rh->proc_cls, rh, rd_count, rd);
3256 * FIXME make optional
3258 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3261 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3263 rh->status |= RSL_DELEGATE_NS;
3264 rh->proc (rh->proc_cls, rh, rd_count, rd);
3268 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3271 rh->status |= RSL_DELEGATE_PKEY;
3273 if ((ignore_pending_records != 0) &&
3274 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3277 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3283 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3284 et.abs_value = rd[i].expiration_time;
3285 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3289 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3291 if (remaining_time.rel_value == 0)
3293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3294 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3296 rh->authority_chain_head->fresh = 0;
3297 rh->proc (rh->proc_cls, rh, 0, NULL);
3305 * Resolve rest of query with new authority
3307 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3308 memcpy (&rh->authority, rd[i].data,
3309 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3310 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3311 auth->zone = rh->authority;
3312 memset (auth->name, 0, strlen (rh->authority_name)+1);
3313 strcpy (auth->name, rh->authority_name);
3314 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3315 rh->authority_chain_tail,
3317 if (NULL != rh->rd.data)
3318 GNUNET_free ((void*)(rh->rd.data));
3320 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3321 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3322 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3324 /* Check for key revocation and delegate */
3325 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3328 GNUNET_GNS_RECORD_REV,
3329 &process_pkey_revocation_result_ns,
3338 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3339 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3341 * If we have found some records for the LAST label
3342 * we return the results. Else null.
3344 if (strcmp (rh->name, "") == 0)
3346 /* Start shortening */
3347 if ((rh->priv_key != NULL) &&
3348 (is_canonical (rh->name) == GNUNET_YES))
3350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3351 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3353 start_shorten (rh->authority_chain_head,
3356 /* simply promote back */
3357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3358 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3359 rh->id, rh->authority_name);
3360 strcpy (rh->name, rh->authority_name);
3361 rh->proc (rh->proc_cls, rh, rd_count, rd);
3365 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
3366 "%s.%s", rh->name, rh->authority_name);
3367 strcpy (rh->name, new_name);
3368 rh->proc (rh->proc_cls, rh, 0, NULL);
3374 * Resolve the delegation chain for the request in our namestore
3376 * @param rh the resolver handle
3379 resolve_delegation_ns (struct ResolverHandle *rh)
3381 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3382 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3384 pop_tld(rh->name, rh->authority_name);
3385 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3388 GNUNET_GNS_RECORD_ANY,
3389 &process_delegation_result_ns,
3396 * Lookup of a record in a specific zone
3397 * calls lookup result processor on result
3399 * @param zone the root zone
3400 * @param pzone the private local zone
3401 * @param record_type the record type to look up
3402 * @param name the name to look up
3403 * @param key a private key for use with PSEU import (can be NULL)
3404 * @param timeout timeout for resolution
3405 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3406 * @param proc the processor to call on result
3407 * @param cls the closure to pass to proc
3410 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3411 struct GNUNET_CRYPTO_ShortHashCode pzone,
3412 uint32_t record_type,
3414 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3415 struct GNUNET_TIME_Relative timeout,
3417 RecordLookupProcessor proc,
3420 struct ResolverHandle *rh;
3421 struct RecordLookupHandle* rlh;
3422 char string_hash[MAX_DNS_LABEL_LENGTH];
3423 char nzkey[MAX_DNS_LABEL_LENGTH];
3424 char* nzkey_ptr = nzkey;
3426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3427 "Starting resolution for %s (type=%d)!\n",
3431 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3432 (strcmp(GNUNET_GNS_TLD, name) != 0))
3434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3435 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3440 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3441 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3443 memset (rh, 0, sizeof (struct ResolverHandle));
3444 rh->authority = zone;
3448 rh->timeout = timeout;
3449 rh->get_handle = NULL;
3450 rh->private_local_zone = pzone;
3451 rh->only_cached = only_cached;
3452 rh->namestore_task = NULL;
3455 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3460 "No shorten key for resolution\n");
3463 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3466 * Set timeout for authority lookup phase to 1/2
3468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3469 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3470 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3471 GNUNET_TIME_relative_divide(timeout, 2),
3472 &handle_lookup_timeout,
3474 rh->timeout_cont = &dht_authority_lookup_timeout;
3475 rh->timeout_cont_cls = rh;
3479 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3480 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3483 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3486 * Only 'gnunet' given
3488 strcpy(rh->name, "\0");
3492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3493 "Checking for TLD...\n");
3494 if (is_zkey_tld(name) == GNUNET_YES)
3496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3499 * This is a zkey tld
3500 * build hash and use as initial authority
3503 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3504 memcpy(rh->name, name,
3505 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3506 pop_tld(rh->name, string_hash);
3508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3509 "ZKEY is %s!\n", string_hash);
3511 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3513 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3516 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3517 "Cannot convert ZKEY %s to hash!\n", string_hash);
3519 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3522 proc (cls, 0, NULL);
3527 else if (is_gnunet_tld (name) == GNUNET_YES)
3529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3532 * Presumably GNUNET tld
3534 memset (rh->name, 0,
3535 strlen(name)-strlen(GNUNET_GNS_TLD));
3536 memcpy (rh->name, name,
3537 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3541 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3542 "Cannot handle this TLD %s\n", string_hash);
3544 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3545 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3546 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3549 proc (cls, 0, NULL);
3555 * Initialize authority chain
3557 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3558 rh->authority_chain_head->prev = NULL;
3559 rh->authority_chain_head->next = NULL;
3560 rh->authority_chain_tail = rh->authority_chain_head;
3561 rh->authority_chain_head->zone = rh->authority;
3562 strcpy (rh->authority_chain_head->name, "");
3565 * Copy original query into lookup handle
3567 rlh->record_type = record_type;
3568 memset(rlh->name, 0, strlen(name) + 1);
3569 strcpy(rlh->name, name);
3571 rlh->proc_cls = cls;
3573 rh->proc = &handle_delegation_ns;
3574 resolve_delegation_ns(rh);
3577 /******** END Record Resolver ***********/
3580 finish_shorten (struct ResolverHandle *rh,
3581 struct NameShortenHandle *nsh)
3583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3584 "Sending %s as shorten result\n", nsh->result);
3585 nsh->proc (nsh->proc_cls, nsh->result);
3586 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3588 free_resolver_handle (rh);
3593 * Callback calles by namestore for a zone to name
3596 * @param cls the closure
3597 * @param zone_key the zone we queried
3598 * @param expire the expiration time of the name
3599 * @param name the name found or NULL
3600 * @param rd_len number of records for the name
3601 * @param rd the record data (PKEY) for the name
3602 * @param signature the signature for the record data
3605 process_zone_to_name_shorten_root (void *cls,
3606 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3607 struct GNUNET_TIME_Absolute expire,
3609 unsigned int rd_len,
3610 const struct GNUNET_NAMESTORE_RecordData *rd,
3611 const struct GNUNET_CRYPTO_RsaSignature *signature);
3615 * Callback called by namestore for a zone to name
3618 * @param cls the closure
3619 * @param zone_key the zone we queried
3620 * @param expire the expiration time of the name
3621 * @param name the name found or NULL
3622 * @param rd_len number of records for the name
3623 * @param rd the record data (PKEY) for the name
3624 * @param signature the signature for the record data
3627 process_zone_to_name_shorten_shorten (void *cls,
3628 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3629 struct GNUNET_TIME_Absolute expire,
3631 unsigned int rd_len,
3632 const struct GNUNET_NAMESTORE_RecordData *rd,
3633 const struct GNUNET_CRYPTO_RsaSignature *signature)
3635 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3636 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3637 struct AuthorityChain *next_authority;
3639 char result[MAX_DNS_NAME_LENGTH];
3640 char tmp_name[MAX_DNS_NAME_LENGTH];
3643 rh->namestore_task = NULL;
3644 /* we found a match in our own root zone */
3647 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3648 memset(result, 0, answer_len);
3650 if (strlen(rh->name) > 0)
3652 sprintf (result, "%s.%s.%s.%s",
3654 nsh->shorten_zone_name,
3659 sprintf (result, "%s.%s.%s", name,
3660 nsh->shorten_zone_name,
3664 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3665 "Found shorten result %s\n", result);
3666 if (strlen (nsh->result) > strlen (result))
3667 strcpy (nsh->result, result);
3669 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3670 nsh->shorten_zone) == 0)
3673 * This is our zone append .gnunet unless name is empty
3674 * (it shouldn't be, usually FIXME what happens if we
3675 * shorten to our zone to a "" record??)
3678 sprintf (result, "%s.%s.%s",
3680 nsh->shorten_zone_name,
3682 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3683 "Our zone: Found %s as shorten result\n", result);
3685 if (strlen (nsh->result) > strlen (result))
3686 strcpy (nsh->result, result);
3687 //nsh->proc(nsh->proc_cls, result);
3689 //free_resolver_handle(rh);
3696 * continue with next authority if exists
3698 if ((rh->authority_chain_head->next == NULL))
3700 finish_shorten (rh, nsh);
3703 next_authority = rh->authority_chain_head;
3705 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3706 "%s.%s", rh->name, next_authority->name);
3708 strcpy(rh->name, tmp_name);
3709 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3710 "No PSEU found for authority %s. Promoting back: %s\n",
3711 next_authority->name, rh->name);
3713 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3714 rh->authority_chain_tail,
3717 GNUNET_free (next_authority);
3719 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3720 &rh->authority_chain_tail->zone,
3721 &rh->authority_chain_head->zone,
3722 &process_zone_to_name_shorten_root,
3727 * Callback calles by namestore for a zone to name
3730 * @param cls the closure
3731 * @param zone_key the zone we queried
3732 * @param expire the expiration time of the name
3733 * @param name the name found or NULL
3734 * @param rd_len number of records for the name
3735 * @param rd the record data (PKEY) for the name
3736 * @param signature the signature for the record data
3739 process_zone_to_name_shorten_private (void *cls,
3740 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3741 struct GNUNET_TIME_Absolute expire,
3743 unsigned int rd_len,
3744 const struct GNUNET_NAMESTORE_RecordData *rd,
3745 const struct GNUNET_CRYPTO_RsaSignature *signature)
3747 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3748 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3749 struct AuthorityChain *next_authority;
3751 char result[MAX_DNS_NAME_LENGTH];
3752 char tmp_name[MAX_DNS_NAME_LENGTH];
3755 rh->namestore_task = NULL;
3756 /* we found a match in our own root zone */
3759 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3760 memset(result, 0, answer_len);
3762 if (strlen(rh->name) > 0)
3764 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3768 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3771 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3772 "Found shorten result %s\n", result);
3773 if (strlen (nsh->result) > strlen (result))
3774 strcpy (nsh->result, result);
3776 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3777 nsh->private_zone) == 0)
3780 * This is our zone append .gnunet unless name is empty
3781 * (it shouldn't be, usually FIXME what happens if we
3782 * shorten to our zone to a "" record??)
3785 sprintf (result, "%s.%s.%s",
3786 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3787 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3788 "Our private zone: Found %s as shorten result %s\n", result);
3789 if (strlen (nsh->result) > strlen (result))
3790 strcpy (nsh->result, result);
3793 if (0 != strcmp (nsh->shorten_zone_name, ""))
3795 /* backtrack authorities for names in priv zone */
3796 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3798 &rh->authority_chain_head->zone,
3799 &process_zone_to_name_shorten_shorten,
3806 * continue with next authority if exists
3808 if ((rh->authority_chain_head->next == NULL))
3810 finish_shorten (rh, nsh);
3813 next_authority = rh->authority_chain_head;
3815 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3816 "%s.%s", rh->name, next_authority->name);
3818 strcpy(rh->name, tmp_name);
3819 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3820 "No PSEU found for authority %s. Promoting back: %s\n",
3821 next_authority->name, rh->name);
3823 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3824 rh->authority_chain_tail,
3827 GNUNET_free (next_authority);
3829 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3830 &rh->authority_chain_tail->zone,
3831 &rh->authority_chain_head->zone,
3832 &process_zone_to_name_shorten_root,
3838 * Callback calles by namestore for a zone to name
3841 * @param cls the closure
3842 * @param zone_key the zone we queried
3843 * @param expire the expiration time of the name
3844 * @param name the name found or NULL
3845 * @param rd_len number of records for the name
3846 * @param rd the record data (PKEY) for the name
3847 * @param signature the signature for the record data
3850 process_zone_to_name_shorten_root (void *cls,
3851 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3852 struct GNUNET_TIME_Absolute expire,
3854 unsigned int rd_len,
3855 const struct GNUNET_NAMESTORE_RecordData *rd,
3856 const struct GNUNET_CRYPTO_RsaSignature *signature)
3858 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3859 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3860 struct AuthorityChain *next_authority;
3862 char result[MAX_DNS_NAME_LENGTH];
3863 char tmp_name[MAX_DNS_NAME_LENGTH];
3866 rh->namestore_task = NULL;
3867 /* we found a match in our own root zone */
3870 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3871 memset(result, 0, answer_len);
3873 if (strlen(rh->name) > 0)
3875 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3879 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3882 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3883 "Found shorten result %s\n", result);
3884 if (strlen (nsh->result) > strlen (result))
3885 strcpy (nsh->result, result);
3887 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3888 nsh->root_zone) == 0)
3891 * This is our zone append .gnunet unless name is empty
3892 * (it shouldn't be, usually FIXME what happens if we
3893 * shorten to our zone to a "" record??)
3896 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3897 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3898 "Our zone: Found %s as shorten result\n", result);
3899 if (strlen (nsh->result) > strlen (result))
3900 strcpy (nsh->result, result);
3903 if (NULL != nsh->private_zone)
3905 /* backtrack authorities for names in priv zone */
3906 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3908 &rh->authority_chain_head->zone,
3909 &process_zone_to_name_shorten_private,
3912 else if (NULL != nsh->shorten_zone)
3914 /* backtrack authorities for names in shorten zone */
3915 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3917 &rh->authority_chain_head->zone,
3918 &process_zone_to_name_shorten_shorten,
3925 * continue with next authority if exists
3927 if ((rh->authority_chain_head->next == NULL))
3929 finish_shorten (rh, nsh);
3932 next_authority = rh->authority_chain_head;
3934 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3935 "%s.%s", rh->name, next_authority->name);
3937 strcpy(rh->name, tmp_name);
3938 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3939 "No PSEU found for authority %s. Promoting back: %s\n",
3940 next_authority->name, rh->name);
3942 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3943 rh->authority_chain_tail,
3946 GNUNET_free (next_authority);
3948 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3949 &rh->authority_chain_tail->zone,
3950 &rh->authority_chain_head->zone,
3951 &process_zone_to_name_shorten_root,
3958 * Process result from namestore delegation lookup
3959 * for shorten operation
3961 * @param cls the client shorten handle
3962 * @param rh the resolver handle
3963 * @param rd_count number of results (0)
3964 * @param rd data (NULL)
3967 handle_delegation_ns_shorten (void* cls,
3968 struct ResolverHandle *rh,
3970 const struct GNUNET_NAMESTORE_RecordData *rd)
3972 struct NameShortenHandle *nsh;
3973 char result[MAX_DNS_NAME_LENGTH];
3975 nsh = (struct NameShortenHandle *)cls;
3978 * At this point rh->name contains the part of the name
3979 * that we do not have a PKEY in our namestore to resolve.
3980 * The authority chain in the resolver handle is now
3981 * useful to backtrack if needed
3984 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3985 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3986 memset(result, 0, sizeof (result));
3988 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3989 nsh->root_zone) == 0)
3992 * This is our zone append .gnunet unless name is empty
3993 * (it shouldn't be, usually FIXME what happens if we
3994 * shorten to our zone to a "" record??)
3997 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3998 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3999 "Our zone: Found %s as shorten result\n", result);
4001 if (strlen (nsh->result) > strlen (result))
4002 strcpy (nsh->result, result);
4005 else if (NULL != nsh->private_zone)
4008 * This is our zone append .gnunet unless name is empty
4009 * (it shouldn't be, usually FIXME what happens if we
4010 * shorten to our zone to a "" record??)
4012 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4013 nsh->private_zone) == 0)
4016 sprintf (result, "%s.%s.%s",
4017 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4018 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4019 "Our zone: Found %s as shorten result in private zone %s\n",
4022 if (strlen (nsh->result) > strlen (result))
4023 strcpy (nsh->result, result);
4026 else if (NULL != nsh->shorten_zone)
4029 * This is our zone append .gnunet unless name is empty
4030 * (it shouldn't be, usually FIXME what happens if we
4031 * shorten to our zone to a "" record??)
4033 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
4034 nsh->shorten_zone) == 0)
4036 sprintf (result, "%s.%s.%s",
4037 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
4038 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4039 "Our zone: Found %s as shorten result in shorten zone\n",
4042 if (strlen (nsh->result) > strlen (result))
4043 strcpy (nsh->result, result);
4048 /* backtrack authorities for names */
4049 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4051 &rh->authority_chain_head->zone,
4052 &process_zone_to_name_shorten_root,
4059 * Callback calles by namestore for a zone to name
4062 * @param cls the closure
4063 * @param zone_key the zone we queried
4064 * @param expire the expiration time of the name
4065 * @param name the name found or NULL
4066 * @param rd_len number of records for the name
4067 * @param rd the record data (PKEY) for the name
4068 * @param signature the signature for the record data
4071 process_zone_to_name_zkey(void *cls,
4072 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4073 struct GNUNET_TIME_Absolute expire,
4075 unsigned int rd_len,
4076 const struct GNUNET_NAMESTORE_RecordData *rd,
4077 const struct GNUNET_CRYPTO_RsaSignature *signature)
4079 struct ResolverHandle *rh = cls;
4080 struct NameShortenHandle *nsh = rh->proc_cls;
4081 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
4082 char new_name[MAX_DNS_NAME_LENGTH];
4084 /* zkey not in our zone */
4088 * In this case we have not given this PKEY a name (yet)
4089 * It is either just not in our zone or not even cached
4090 * Since we do not know at this point we will not try to shorten
4091 * because PKEY import will happen if the user follows the zkey
4094 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
4096 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4097 "No name found for zkey %s returning verbatim!\n", enc);
4098 if (strcmp(rh->name, "") != 0)
4099 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4100 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4102 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4103 enc, GNUNET_GNS_TLD_ZKEY);
4105 strcpy (nsh->result, new_name);
4107 finish_shorten (rh, nsh);
4111 if (strcmp(rh->name, "") != 0)
4112 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4115 strcpy(new_name, name);
4117 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4118 "Continue shorten for %s!\n", new_name);
4120 strcpy(rh->name, new_name);
4122 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4123 rh->authority_chain_tail = rh->authority_chain_head;
4124 rh->authority_chain_head->zone = rh->authority;
4127 /* Start delegation resolution in our namestore */
4128 resolve_delegation_ns (rh);
4133 * Shorten api from resolver
4135 * @param zone the root zone to use
4136 * @param pzone the private zone to use
4137 * @param szone the shorten zone to use
4138 * @param name the name to shorten
4139 * @param private_zone_name name of the private zone
4140 * @param shorten_zone_name name of the shorten zone
4141 * @param proc the processor to call with result
4142 * @param proc_cls closure to pass to proc
4145 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4146 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4147 struct GNUNET_CRYPTO_ShortHashCode *szone,
4149 const char* private_zone_name,
4150 const char* shorten_zone_name,
4151 ShortenResultProcessor proc,
4154 struct ResolverHandle *rh;
4155 struct NameShortenHandle *nsh;
4156 char string_hash[MAX_DNS_LABEL_LENGTH];
4157 struct GNUNET_CRYPTO_ShortHashCode zkey;
4158 char nzkey[MAX_DNS_LABEL_LENGTH];
4159 char* nzkey_ptr = nzkey;
4162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4163 "Starting shorten for %s!\n", name);
4165 if (is_canonical ((char*)name) == GNUNET_YES)
4167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4168 "%s is canonical. Returning verbatim\n", name);
4169 proc (proc_cls, name);
4173 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4176 nsh->proc_cls = proc_cls;
4177 nsh->root_zone = zone;
4178 nsh->private_zone = pzone;
4179 nsh->shorten_zone = szone;
4180 strcpy (nsh->private_zone_name, private_zone_name);
4181 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4182 strcpy (nsh->result, name);
4185 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4186 rh->authority = *zone;
4188 rh->priv_key = NULL;
4189 rh->namestore_task = NULL;
4190 rh->proc = &handle_delegation_ns_shorten;
4193 rh->private_local_zone = *zone;
4195 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4198 "Checking for TLD...\n");
4199 if (is_zkey_tld (name) == GNUNET_YES)
4201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4204 * This is a zkey tld
4205 * build hash and use as initial authority
4208 memset (rh->name, 0,
4209 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4210 memcpy (rh->name, name,
4211 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4212 pop_tld (rh->name, string_hash);
4214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4215 "ZKEY is %s!\n", string_hash);
4217 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4219 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4223 "Cannot convert ZKEY %s to hash!\n", nzkey);
4226 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4227 proc (proc_cls, name);
4231 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4234 &process_zone_to_name_zkey,
4239 else if (is_gnunet_tld (name) == GNUNET_YES)
4241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4244 * Presumably GNUNET tld
4246 memset (rh->name, 0,
4247 strlen (name)-strlen (GNUNET_GNS_TLD));
4248 memcpy (rh->name, name,
4249 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4256 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4257 proc (proc_cls, name);
4261 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4262 rh->authority_chain_tail = rh->authority_chain_head;
4263 rh->authority_chain_head->zone = *zone;
4266 /* Start delegation resolution in our namestore */
4267 resolve_delegation_ns (rh);
4270 /*********** END NAME SHORTEN ********************/
4273 * Conclude get authority lookup
4275 * @param rh resolver handle
4276 * @param nah get authority lookup handle
4279 finish_get_auth (struct ResolverHandle *rh,
4280 struct GetNameAuthorityHandle *nah)
4282 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4283 "Got authority result %s\n", nah->result);
4285 nah->proc(nah->proc_cls, nah->result);
4286 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4288 free_resolver_handle(rh);
4292 * Process result from namestore delegation lookup
4293 * for get authority operation
4295 * @param cls the client get auth handle
4296 * @param rh the resolver handle
4297 * @param rd_count number of results (0)
4298 * @param rd data (NULL)
4301 handle_delegation_result_ns_get_auth(void* cls,
4302 struct ResolverHandle *rh,
4304 const struct GNUNET_NAMESTORE_RecordData *rd)
4306 struct GetNameAuthorityHandle* nah;
4309 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4312 * At this point rh->name contains the part of the name
4313 * that we do not have a PKEY in our namestore to resolve.
4314 * The authority chain in the resolver handle is now
4315 * useful to backtrack if needed
4318 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4319 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4321 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4322 "Building response!\n");
4323 if (is_canonical (rh->name) == GNUNET_YES)
4326 * We successfully resolved the authority in the ns
4327 * FIXME for our purposes this is fine
4328 * but maybe we want to have an api that also looks
4329 * into the dht (i.e. option in message)
4331 if (strlen(rh->name) > strlen(nah->name))
4333 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4334 "Record name longer than original lookup name... odd!\n");
4338 answer_len = strlen(nah->name) - strlen(rh->name)
4339 + strlen(GNUNET_GNS_TLD) + 1;
4340 memset(nah->result, 0, answer_len);
4341 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4343 finish_get_auth (rh, nah);
4347 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4348 "Unable to resolve authority for remaining %s!\n", rh->name);
4349 strcpy(nah->result, "");
4350 finish_get_auth (rh, nah);
4358 * Tries to resolve the authority for name
4361 * @param zone the root zone to look up for
4362 * @param pzone the private local zone
4363 * @param name the name to lookup up
4364 * @param proc the processor to call when finished
4365 * @param proc_cls the closure to pass to the processor
4368 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4369 struct GNUNET_CRYPTO_ShortHashCode pzone,
4371 GetAuthorityResultProcessor proc,
4374 struct ResolverHandle *rh;
4375 struct GetNameAuthorityHandle *nah;
4377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4378 "Starting authority resolution for %s!\n", name);
4380 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4381 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4382 rh->authority = zone;
4384 rh->private_local_zone = pzone;
4385 rh->namestore_task = NULL;
4387 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4389 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4391 strcpy(rh->name, "\0");
4396 strlen(name)-strlen(GNUNET_GNS_TLD));
4397 memcpy(rh->name, name,
4398 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4401 memset(nah->name, 0,
4403 strcpy(nah->name, name);
4405 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4406 rh->authority_chain_tail = rh->authority_chain_head;
4407 rh->authority_chain_head->zone = zone;
4408 rh->proc = &handle_delegation_result_ns_get_auth;
4409 rh->proc_cls = (void*)nah;
4412 nah->proc_cls = proc_cls;
4413 strcpy (nah->result, "");
4415 /* Start delegation resolution in our namestore */
4416 resolve_delegation_ns(rh);
4420 /******** END GET AUTHORITY *************/
4422 /* end of gnunet-service-gns_resolver.c */