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_dns_service.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_dnsparser_lib.h"
35 #include "gns_protocol.h"
36 #include "gnunet_gns_service.h"
37 #include "gns_common.h"
38 #include "block_gns.h"
40 #include "gnunet-service-gns_resolver.h"
42 #include "gnunet_vpn_service.h"
49 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
52 * DHT replication level
54 #define DHT_GNS_REPLICATION_LEVEL 5
58 * Our handle to the namestore service
60 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
64 * Our handle to the vpn service
66 static struct GNUNET_VPN_Handle *vpn_handle;
70 * Resolver handle to the dht
72 static struct GNUNET_DHT_Handle *dht_handle;
75 * Heap for parallel DHT lookups
77 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
80 * Heap for namestore queues
82 static struct GNUNET_CONTAINER_Heap *ns_task_heap;
85 * Maximum amount of parallel queries in background
87 static unsigned long long max_allowed_background_queries;
90 * Maximum amount of parallel namestore tasks in background
92 static unsigned long long max_allowed_ns_tasks;
95 * Wheather or not to ignore pending records
97 static int ignore_pending_records;
102 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
105 * Background shortening handles
107 static struct GetPseuAuthorityHandle *gph_head;
110 * Background shortening handles
112 static struct GetPseuAuthorityHandle *gph_tail;
115 * Resolver lookup list
117 static struct ResolverHandle *rlh_head;
120 * Resolver lookup list
122 static struct ResolverHandle *rlh_tail;
125 * Resolver shorten list
127 static struct ResolverHandle *nsh_head;
130 * Resolver shorten list
132 static struct ResolverHandle *nsh_tail;
135 * Resolver get auth list
137 static struct ResolverHandle *nah_head;
140 * Resolver get auth list
142 static struct ResolverHandle *nah_tail;
145 * Global configuration.
147 static const struct GNUNET_CONFIGURATION_Handle *cfg;
150 * a resolution identifier pool variable
151 * This is a non critical identifier useful for debugging
153 static unsigned long long rid_gen;
157 * Check if name is in srv format (_x._y.xxx)
160 * @return GNUNET_YES if true
163 is_srv (const char *name)
170 if (NULL == strstr (name, "._"))
173 ndup = GNUNET_strdup (name);
175 if (NULL == strtok (NULL, "."))
177 if (NULL == strtok (NULL, "."))
179 if (NULL != strtok (NULL, "."))
187 * Determine if this name is canonical (is a legal name in a zone, without delegation);
188 * note that we do not test that the name does not contain illegal characters, we only
189 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
190 * even though they consist of multiple labels.
193 * a.b.gads = not canonical
195 * _foo._srv = canonical
196 * _f.bar = not canonical
198 * @param name the name to test
199 * @return GNUNET_YES if canonical
202 is_canonical (const char *name)
207 if (NULL == strchr (name, '.'))
212 while (NULL != (dot = strchr (pos, '.')))
222 free_get_pseu_authority_handle (struct GetPseuAuthorityHandle *gph)
224 gph->namestore_task = NULL;
225 GNUNET_free (gph->auth);
226 GNUNET_CRYPTO_rsa_key_free (gph->key);
227 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
233 * Callback that shortens authorities
235 * @param gph the handle containing the name to shorten
238 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
242 * Continuation for pkey record creation (shorten)
244 * @param cls a GetPseuAuthorityHandle
245 * @param success unused
249 create_pkey_cont (void* cls, int32_t success, const char* emsg)
251 //FIXME do sth with error
252 struct GetPseuAuthorityHandle* gph = cls;
254 free_get_pseu_authority_handle (gph);
259 * Namestore calls this function if we have record for this name.
260 * (or with rd_count=0 to indicate no matches)
262 * @param cls the pending query
263 * @param key the key of the zone we did the lookup
264 * @param expiration expiration date of the namestore entry
265 * @param name the name for which we need an authority
266 * @param rd_count the number of records with 'name'
267 * @param rd the record data
268 * @param signature the signature of the authority for the record data
271 process_pseu_lookup_ns (void* cls,
272 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
273 struct GNUNET_TIME_Absolute expiration,
274 const char *name, unsigned int rd_count,
275 const struct GNUNET_NAMESTORE_RecordData *rd,
276 const struct GNUNET_CRYPTO_RsaSignature *signature)
278 struct GetPseuAuthorityHandle* gph = cls;
279 struct GNUNET_NAMESTORE_RecordData new_pkey;
281 gph->namestore_task = NULL;
284 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
285 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
286 free_get_pseu_authority_handle (gph);
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n",
295 new_pkey.expiration_time = UINT64_MAX;
296 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
297 new_pkey.data = &gph->auth->zone;
298 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
299 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
300 | GNUNET_NAMESTORE_RF_PRIVATE
301 | GNUNET_NAMESTORE_RF_PENDING;
302 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
312 * process result of a dht pseu lookup
314 * @param gph the handle
315 * @param name the pseu result or NULL
318 process_pseu_result (struct GetPseuAuthorityHandle* gph,
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 "GNS_AUTO_PSEU: No PSEU, no shorten. Finished.\n");
325 free_get_pseu_authority_handle (gph);
329 memcpy (gph->test_name, name, strlen(name) + 1);
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
334 * Check for collision
336 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
339 GNUNET_NAMESTORE_TYPE_ANY,
340 &process_pseu_lookup_ns,
346 * Handle timeout for dht request
348 * @param cls the request handle as closure
349 * @param tc the task context
352 handle_auth_discovery_timeout (void *cls,
353 const struct GNUNET_SCHEDULER_TaskContext *tc)
355 struct GetPseuAuthorityHandle* gph = cls;
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
359 GNUNET_DHT_get_stop (gph->get_handle);
360 gph->get_handle = NULL;
361 process_pseu_result (gph, NULL);
366 * Function called when we find a PSEU entry in the DHT
368 * @param cls the request handle
369 * @param exp lifetime
370 * @param key the key the record was stored under
371 * @param get_path get path
372 * @param get_path_length get path length
373 * @param put_path put path
374 * @param put_path_length put path length
375 * @param type the block type
376 * @param size the size of the record
377 * @param data the record data
380 process_auth_discovery_dht_result (void* cls,
381 struct GNUNET_TIME_Absolute exp,
382 const struct GNUNET_HashCode * key,
383 const struct GNUNET_PeerIdentity *get_path,
384 unsigned int get_path_length,
385 const struct GNUNET_PeerIdentity *put_path,
386 unsigned int put_path_length,
387 enum GNUNET_BLOCK_Type type,
391 struct GetPseuAuthorityHandle* gph = cls;
392 struct GNSNameRecordBlock *nrb;
393 const char* rd_data = data;
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
402 /* stop lookup and timeout task */
403 GNUNET_DHT_get_stop (gph->get_handle);
404 gph->get_handle = NULL;
405 GNUNET_SCHEDULER_cancel (gph->timeout);
409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
410 "GNS_GET_AUTH: got dht result null!\n", size);
411 free_get_pseu_authority_handle (gph);
415 nrb = (struct GNSNameRecordBlock*)data;
416 name = (char*)&nrb[1];
417 num_records = ntohl (nrb->rd_count);
419 struct GNUNET_NAMESTORE_RecordData rd[num_records];
421 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
422 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
424 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
430 "GNS_GET_AUTH: Error deserializing data!\n");
434 for (i=0; i < num_records; i++)
436 if ((strcmp (name, GNUNET_GNS_MASTERZONE_STR) == 0) &&
437 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
440 process_pseu_result (gph, (char*)rd[i].data);
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "GNS_GET_AUTH: finished shorten, no results!\n");
448 process_pseu_result (gph, NULL);
453 * Process PSEU discovery for shorten via namestore
455 * @param cls the GetPseuAuthorityHandle
456 * @param key the public key
457 * @param expiration recorddata expiration
458 * @param name the looked up name
459 * @param rd_count number of records in set
460 * @param rd record data
461 * @param signature the signature
464 process_auth_discovery_ns_result (void* cls,
465 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
466 struct GNUNET_TIME_Absolute expiration,
468 unsigned int rd_count,
469 const struct GNUNET_NAMESTORE_RecordData *rd,
470 const struct GNUNET_CRYPTO_RsaSignature *signature)
472 struct GetPseuAuthorityHandle* gph = cls;
473 struct GNUNET_HashCode lookup_key;
477 gph->namestore_task = NULL;
481 GNUNET_GNS_get_key_for_record (GNUNET_GNS_TLD_PLUS, &gph->auth->zone, &lookup_key);
482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
483 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
485 GNUNET_h2s (&lookup_key));
487 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
488 &handle_auth_discovery_timeout, gph);
490 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
492 GNUNET_assert (gph->get_handle == NULL);
494 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
495 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
497 DHT_GNS_REPLICATION_LEVEL,
498 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
501 &process_auth_discovery_dht_result,
506 for (i=0; i < rd_count; i++)
508 if (0 != (strcmp (name, GNUNET_GNS_TLD_PLUS)))
511 if (rd[i].record_type != GNUNET_GNS_RECORD_PSEU)
515 process_pseu_result (gph, (char*)rd[i].data);
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
520 process_pseu_result (gph, NULL);
525 * Callback called by namestore for a zone to name
528 * @param cls the closure
529 * @param zone_key the zone we queried
530 * @param expire the expiration time of the name
531 * @param name the name found or NULL
532 * @param rd_len number of records for the name
533 * @param rd the record data (PKEY) for the name
534 * @param signature the signature for the record data
537 process_zone_to_name_discover (void *cls,
538 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
539 struct GNUNET_TIME_Absolute expire,
542 const struct GNUNET_NAMESTORE_RecordData *rd,
543 const struct GNUNET_CRYPTO_RsaSignature *signature)
545 struct GetPseuAuthorityHandle* gph = cls;
547 gph->namestore_task = NULL;
550 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
552 GNUNET_GNS_MASTERZONE_STR,
553 GNUNET_GNS_RECORD_PSEU,
554 &process_auth_discovery_ns_result,
558 /* we found a match in our own zone */
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
561 free_get_pseu_authority_handle (gph);
566 * Callback that shortens authorities
568 * @param gph the handle to the shorten request
571 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 "GNS_AUTO_PSEU: New authority %s discovered\n",
577 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
580 &process_zone_to_name_discover,
586 * Start shortening algorithm using auth as
589 * @param auth the authorities that were resolved
590 * @param key the private key for PKEY import
593 start_shorten (struct AuthorityChain *auth,
594 const struct GNUNET_CRYPTO_RsaPrivateKey *key)
596 struct GetPseuAuthorityHandle *gph;
597 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
598 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
600 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
601 if (NULL == (pb_key = GNUNET_CRYPTO_rsa_encode_key (key)))
603 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
604 "Failed to encode RSA key on shorten\n");
607 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
608 gph->key = GNUNET_CRYPTO_rsa_decode_key ((const char*) pb_key, ntohs (pb_key->len));
609 GNUNET_free (pb_key);
610 if (NULL == gph->key)
612 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
613 "Failed to decode RSA key on shorten\n");
617 GNUNET_CRYPTO_short_hash (&pkey,
618 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
620 gph->auth = GNUNET_malloc (sizeof (struct AuthorityChain));
621 memcpy (gph->auth, auth, sizeof (struct AuthorityChain));
622 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
623 shorten_authority_chain (gph);
628 * Initialize the resolver
630 * @param nh the namestore handle
631 * @param dh the dht handle
632 * @param lz the local zone's hash
633 * @param c configuration handle
634 * @param max_bg_queries maximum number of parallel background queries in dht
635 * @param ignore_pending ignore records that still require user confirmation
637 * @return GNUNET_OK on success
640 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
641 struct GNUNET_DHT_Handle *dh,
642 struct GNUNET_CRYPTO_ShortHashCode lz,
643 const struct GNUNET_CONFIGURATION_Handle *c,
644 unsigned long long max_bg_queries,
649 return GNUNET_SYSERR;
652 namestore_handle = nh;
656 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
658 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
659 max_allowed_background_queries = max_bg_queries;
660 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
661 ignore_pending_records = ignore_pending;
662 GNUNET_RESOLVER_connect (cfg);
669 * @param rh resolver handle
670 * @param rlh record lookup handle
671 * @param rd_count number of results
675 finish_lookup (struct ResolverHandle *rh,
676 struct RecordLookupHandle* rlh,
677 unsigned int rd_count,
678 const struct GNUNET_NAMESTORE_RecordData *rd);
682 * Helper function to free resolver handle
684 * @param rh the handle to free
687 free_resolver_handle (struct ResolverHandle* rh)
689 struct AuthorityChain *ac;
690 struct AuthorityChain *ac_next;
695 ac_next = rh->authority_chain_head;
696 while (NULL != (ac = ac_next))
702 if (NULL != rh->get_handle)
703 GNUNET_DHT_get_stop (rh->get_handle);
704 if (NULL != rh->dns_raw_packet)
705 GNUNET_free (rh->dns_raw_packet);
706 if (NULL != rh->namestore_task)
708 GNUNET_NAMESTORE_cancel (rh->namestore_task);
709 rh->namestore_task = NULL;
711 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
712 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
713 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
714 GNUNET_SCHEDULER_cancel (rh->timeout_task);
715 if (NULL != rh->dns_sock)
716 GNUNET_NETWORK_socket_close (rh->dns_sock);
717 if (NULL != rh->dns_resolver_handle)
718 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
719 if (NULL != rh->rd.data)
720 GNUNET_free ((void*)(rh->rd.data));
721 if (NULL != rh->dht_heap_node)
722 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
730 * @param rh resolver handle
731 * @param nsh name shorten handle
734 finish_shorten (struct ResolverHandle *rh,
735 struct NameShortenHandle *nsh);
741 * @param rh resolver handle
742 * @param nah get name authority handle
745 finish_get_auth (struct ResolverHandle *rh,
746 struct GetNameAuthorityHandle* rlh);
753 gns_resolver_cleanup ()
755 struct GetPseuAuthorityHandle *tmp;
756 struct ResolverHandle *rh;
757 struct NamestoreBGTask *nbg;
759 while (NULL != (tmp = gph_head))
761 if (tmp->get_handle != NULL)
763 GNUNET_DHT_get_stop (tmp->get_handle);
764 tmp->get_handle = NULL;
766 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
768 GNUNET_SCHEDULER_cancel (tmp->timeout);
769 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
771 if (NULL != tmp->namestore_task)
773 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
774 tmp->namestore_task = NULL;
776 free_get_pseu_authority_handle (tmp);
779 while (NULL != rlh_head)
781 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
783 while (NULL != nsh_head)
785 finish_shorten (nsh_head, nsh_head->proc_cls);
787 while (NULL != nah_head)
789 finish_get_auth (nah_head, nah_head->proc_cls);
792 while (NULL != (rh = GNUNET_CONTAINER_heap_remove_root(dht_lookup_heap)))
796 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
797 dht_lookup_heap = NULL;
799 while (NULL != (nbg = GNUNET_CONTAINER_heap_remove_root(ns_task_heap)))
801 GNUNET_NAMESTORE_cancel (nbg->qe);
804 GNUNET_CONTAINER_heap_destroy (ns_task_heap);
811 * Callback when record data is put into namestore
813 * @param cls the closure
814 * @param success GNUNET_OK on success
815 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
818 on_namestore_record_put_result (void *cls,
822 struct NamestoreBGTask *nbg = cls;
824 GNUNET_CONTAINER_heap_remove_node (nbg->node);
827 if (GNUNET_NO == success)
829 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
830 "GNS_NS: records already in namestore\n");
833 else if (GNUNET_YES == success)
835 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
836 "GNS_NS: records successfully put in namestore\n");
840 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
841 "GNS_NS: Error putting records into namestore: %s\n", emsg);
846 * Lookup timeout task
848 * @param cls the ResolverHandle for the task that timed out
849 * @param tc the task context
852 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
854 struct ResolverHandle *rh = cls;
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 "Lookup timeout for request %llu triggered\n",
859 if (NULL != rh->timeout_cont)
860 rh->timeout_cont (rh->timeout_cont_cls, tc);
865 * Processor for background lookups in the DHT
867 * @param cls closure (NULL)
868 * @param rd_count number of records found (not 0)
869 * @param rd record data
872 background_lookup_result_processor (void *cls,
874 const struct GNUNET_NAMESTORE_RecordData *rd)
876 //We could do sth verbose/more useful here but it doesn't make any difference
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "GNS_BG: background dht lookup finished. (%d results)\n",
884 * Handle timeout for DHT requests
886 * @param cls the request handle as closure
887 * @param tc the task context
890 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
892 struct ResolverHandle *rh = cls;
893 struct RecordLookupHandle *rlh = rh->proc_cls;
894 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
897 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus) timed out.\n",
898 rh->id, rh->name, rh->timeout.rel_value);
900 * Start resolution in bg
902 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
903 rh->name, GNUNET_GNS_TLD);
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
907 rh->id, new_name, rlh->record_type);
909 gns_resolver_lookup_record (rh->authority,
910 rh->private_local_zone,
914 GNUNET_TIME_UNIT_FOREVER_REL,
916 &background_lookup_result_processor,
919 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
921 GNUNET_DHT_get_stop (rh->get_handle);
922 rh->get_handle = NULL;
923 rh->proc (rh->proc_cls, rh, 0, NULL);
928 * Function called when we get a result from the dht
929 * for our record query
931 * @param cls the request handle
932 * @param exp lifetime
933 * @param key the key the record was stored under
934 * @param get_path get path
935 * @param get_path_length get path length
936 * @param put_path put path
937 * @param put_path_length put path length
938 * @param type the block type
939 * @param size the size of the record
940 * @param data the record data
943 process_record_result_dht (void* cls,
944 struct GNUNET_TIME_Absolute exp,
945 const struct GNUNET_HashCode * key,
946 const struct GNUNET_PeerIdentity *get_path,
947 unsigned int get_path_length,
948 const struct GNUNET_PeerIdentity *put_path,
949 unsigned int put_path_length,
950 enum GNUNET_BLOCK_Type type,
951 size_t size, const void *data)
953 struct ResolverHandle *rh = cls;
954 struct RecordLookupHandle *rlh = rh->proc_cls;
955 const struct GNSNameRecordBlock *nrb = data;
956 uint32_t num_records;
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
963 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
964 /* stop lookup and timeout task */
965 GNUNET_DHT_get_stop (rh->get_handle);
966 rh->get_handle = NULL;
967 if (rh->dht_heap_node != NULL)
969 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
970 rh->dht_heap_node = NULL;
972 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
974 GNUNET_SCHEDULER_cancel (rh->timeout_task);
975 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
977 rh->get_handle = NULL;
978 name = (const char*) &nrb[1];
979 num_records = ntohl (nrb->rd_count);
981 struct GNUNET_NAMESTORE_RecordData rd[num_records];
982 struct NamestoreBGTask *ns_heap_root;
983 struct NamestoreBGTask *namestore_bg_task;
985 rd_data = &name[strlen (name) + 1];
986 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
988 GNUNET_NAMESTORE_records_deserialize (rd_size,
993 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
994 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
995 rh->proc (rh->proc_cls, rh, 0, NULL);
998 for (i = 0; i < num_records; i++)
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1001 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1002 rh->id, name, rh->name);
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1004 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1005 rh->id, rd[i].record_type, rlh->record_type);
1006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1007 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1008 rh->id, rd[i].data_size);
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "GNS_PHASE_REC-%llu: Got flag %d\n",
1011 rh->id, rd[i].flags);
1013 if ((strcmp (name, rh->name) == 0) &&
1014 (rd[i].record_type == rlh->record_type))
1020 * FIXME check pubkey against existing key in namestore?
1021 * https://gnunet.org/bugs/view.php?id=2179
1023 if (max_allowed_ns_tasks <=
1024 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1026 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1027 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1030 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1034 /* Save to namestore */
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "GNS_PHASE_REC-%llu: Caching record for %s\n",
1038 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1039 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1046 &on_namestore_record_put_result, //cont
1049 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1051 GNUNET_TIME_absolute_get().abs_value);
1052 if (0 < rh->answered)
1053 rh->proc (rh->proc_cls, rh, num_records, rd);
1055 rh->proc (rh->proc_cls, rh, 0, NULL);
1061 * Start DHT lookup for a (name -> query->record_type) record in
1062 * rh->authority's zone
1064 * @param rh the pending gns query context
1067 resolve_record_dht (struct ResolverHandle *rh)
1069 struct RecordLookupHandle *rlh = rh->proc_cls;
1071 struct GNUNET_HashCode lookup_key;
1072 struct ResolverHandle *rh_heap_root;
1074 GNUNET_GNS_get_key_for_record (rh->name, &rh->authority, &lookup_key);
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1077 rh->id, rh->name, GNUNET_h2s (&lookup_key));
1079 rh->dht_heap_node = NULL;
1081 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
1084 * Update timeout if necessary
1086 if (GNUNET_SCHEDULER_NO_TASK == rh->timeout_task)
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1089 "GNS_PHASE_REC-%llu: Adjusting timeout to %s/2\n",
1091 GNUNET_STRINGS_relative_time_to_string (rh->timeout, GNUNET_YES));
1093 * Set timeout for authority lookup phase to 1/2
1095 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1096 GNUNET_TIME_relative_divide (rh->timeout, 2),
1097 &handle_lookup_timeout,
1100 rh->timeout_cont = &dht_lookup_timeout;
1101 rh->timeout_cont_cls = rh;
1105 if (max_allowed_background_queries <=
1106 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1108 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1109 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1110 rh_heap_root->get_handle = NULL;
1111 rh_heap_root->dht_heap_node = NULL;
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1114 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1115 rh->id, rh_heap_root->name);
1116 rh_heap_root->proc (rh_heap_root->proc_cls,
1121 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1123 GNUNET_TIME_absolute_get ().abs_value);
1126 xquery = htonl (rlh->record_type);
1128 GNUNET_assert (rh->get_handle == NULL);
1129 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1130 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1132 DHT_GNS_REPLICATION_LEVEL,
1133 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1136 &process_record_result_dht,
1143 * Namestore calls this function if we have record for this name.
1144 * (or with rd_count=0 to indicate no matches)
1146 * @param cls the pending query
1147 * @param key the key of the zone we did the lookup
1148 * @param expiration expiration date of the namestore entry
1149 * @param name the name for which we need an authority
1150 * @param rd_count the number of records with 'name'
1151 * @param rd the record data
1152 * @param signature the signature of the authority for the record data
1155 process_record_result_ns (void* cls,
1156 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1157 struct GNUNET_TIME_Absolute expiration,
1158 const char *name, unsigned int rd_count,
1159 const struct GNUNET_NAMESTORE_RecordData *rd,
1160 const struct GNUNET_CRYPTO_RsaSignature *signature)
1162 struct ResolverHandle *rh = cls;
1163 struct RecordLookupHandle *rlh = rh->proc_cls;
1164 struct GNUNET_TIME_Relative remaining_time;
1165 struct GNUNET_CRYPTO_ShortHashCode zone;
1166 struct GNUNET_TIME_Absolute et;
1169 rh->namestore_task = NULL;
1170 GNUNET_CRYPTO_short_hash (key,
1171 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1173 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1177 rh->status |= RSL_RECORD_EXISTS;
1178 if (remaining_time.rel_value == 0)
1179 rh->status |= RSL_RECORD_EXPIRED;
1184 * Lookup terminated and no results
1186 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1187 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1190 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1191 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1194 * Our zone and no result? Cannot resolve TT
1196 rh->proc(rh->proc_cls, rh, 0, NULL);
1200 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1201 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1203 for (i = 0; i < rd_count;i++)
1205 if (rd[i].record_type != rlh->record_type)
1208 if (ignore_pending_records &&
1209 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1211 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1212 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1217 //FIXME: eh? do I have to handle this here?
1218 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1219 et.abs_value = rd[i].expiration_time;
1220 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
1222 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1223 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1233 if (0 == rh->answered)
1235 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1236 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1237 rh->proc(rh->proc_cls, rh, 0, NULL);
1241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1242 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1243 rh->id, rh->answered, rd_count);
1244 rh->proc(rh->proc_cls, rh, rd_count, rd);
1250 * VPN redirect result callback
1252 * @param cls the resolver handle
1253 * @param af the requested address family
1254 * @param address in_addr(6) respectively
1257 process_record_result_vpn (void* cls, int af, const void *address)
1259 struct ResolverHandle *rh = cls;
1260 struct RecordLookupHandle *rlh = rh->proc_cls;
1261 struct GNUNET_NAMESTORE_RecordData rd;
1263 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1264 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1268 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1269 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1271 if (GNUNET_GNS_RECORD_A != rlh->record_type)
1273 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1274 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1276 rh->proc (rh->proc_cls, rh, 0, NULL);
1279 rd.record_type = GNUNET_GNS_RECORD_A;
1280 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1282 rd.data_size = sizeof (struct in_addr);
1284 rh->proc (rh->proc_cls, rh, 1, &rd);
1287 else if (AF_INET6 == af)
1289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1290 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1292 if (GNUNET_GNS_RECORD_AAAA != rlh->record_type)
1294 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1295 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1297 rh->proc (rh->proc_cls, rh, 0, NULL);
1300 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1301 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1303 rd.data_size = sizeof (struct in6_addr);
1305 rh->proc (rh->proc_cls, rh, 1, &rd);
1309 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1310 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1312 rh->proc (rh->proc_cls, rh, 0, NULL);
1318 * Process VPN lookup result for record
1320 * @param cls the record lookup handle
1321 * @param rh resolver handle
1322 * @param rd_count number of results (1)
1323 * @param rd record data containing the result
1326 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1327 unsigned int rd_count,
1328 const struct GNUNET_NAMESTORE_RecordData *rd)
1330 struct RecordLookupHandle* rlh = cls;
1334 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1335 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1338 /* give up, cannot resolve */
1339 finish_lookup(rh, rlh, 0, NULL);
1343 /* results found yay */
1344 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1345 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1347 finish_lookup(rh, rlh, rd_count, rd);
1352 * Sends a UDP dns query to a nameserver specified in the rh
1354 * @param rh the resolver handle
1357 send_dns_packet (struct ResolverHandle *rh);
1363 * @param cls the ResolverHandle for this lookup
1364 * @param addr the sockaddr
1365 * @param addrlen the socket address length
1368 handle_dns_resolver (void *cls,
1369 const struct sockaddr *addr,
1372 struct ResolverHandle *rh = cls;
1373 struct RecordLookupHandle *rlh = rh->proc_cls;
1374 struct GNUNET_NAMESTORE_RecordData rd;
1375 struct sockaddr_in *sai;
1376 struct sockaddr_in6 *sai6;
1380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1381 "No address found in DNS!\n");
1382 finish_lookup (rh, rlh, 0, NULL);
1386 if (sizeof (struct sockaddr_in) == addrlen)
1388 sai = (struct sockaddr_in*) addr;
1389 rd.record_type = GNUNET_GNS_RECORD_A;
1390 rd.data_size = sizeof (struct in_addr);
1391 rd.data = &sai->sin_addr;
1393 else if (sizeof (struct sockaddr_in6) == addrlen)
1395 sai6 = (struct sockaddr_in6*) addr;
1396 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1397 rd.data_size = sizeof (struct in6_addr);
1398 rd.data = &sai6->sin6_addr;
1402 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1403 "Address length is garbage!\n");
1404 finish_lookup (rh, rlh, 0, NULL);
1407 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1409 finish_lookup (rh, rlh, 1, &rd);
1414 * Resolve DNS name via local stub resolver
1416 * @param rh the resolver handle
1419 resolve_dns_name (struct ResolverHandle *rh)
1421 struct RecordLookupHandle *rlh = rh->proc_cls;
1424 if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1425 (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1428 "Can only resolve A/AAAA via stub... abort\n");
1429 finish_lookup (rh, rlh, 0, NULL);
1433 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1438 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1441 &handle_dns_resolver,
1447 * Read DNS udp packet from socket
1449 * @param cls the resolver handle
1450 * @param tc task context
1453 read_dns_response (void *cls,
1454 const struct GNUNET_SCHEDULER_TaskContext *tc)
1456 struct ResolverHandle *rh = cls;
1457 struct RecordLookupHandle *rlh = rh->proc_cls;
1458 char buf[UINT16_MAX];
1460 struct sockaddr_in addr;
1462 struct GNUNET_DNSPARSER_Packet *packet;
1463 struct GNUNET_NAMESTORE_RecordData rd;
1464 int found_delegation = GNUNET_NO;
1465 int found_cname = GNUNET_NO;
1466 char* delegation_name = NULL;
1467 int zone_offset = 0;
1470 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1471 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1473 /* timeout or shutdown */
1474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1475 "Terminating DNS query %d\n", tc->reason);
1476 finish_lookup (rh, rlh, 0, NULL);
1480 addrlen = sizeof (addr);
1481 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1483 (struct sockaddr*) &addr,
1488 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1489 finish_lookup (rh, rlh, 0, NULL);
1492 if (NULL == (packet = GNUNET_DNSPARSER_parse (buf, r)))
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1495 "Failed to parse DNS reply!\n");
1496 finish_lookup (rh, rlh, 0, NULL);
1500 for (i = 0; i < packet->num_answers; i++)
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503 "Got record type %d (want %d)\n",
1504 packet->answers[i].type,
1506 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1507 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1510 "CNAME... restarting query with %s\n",
1511 packet->answers[i].data.hostname
1513 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1514 found_cname = GNUNET_YES;
1518 if ((packet->answers[i].type == rlh->record_type) &&
1519 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1523 rd.data = packet->answers[i].data.raw.data;
1524 rd.data_size = packet->answers[i].data.raw.data_len;
1525 rd.record_type = packet->answers[i].type;
1527 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1528 finish_lookup (rh, rlh, 1, &rd);
1529 GNUNET_DNSPARSER_free_packet (packet);
1534 if (GNUNET_YES == found_cname)
1536 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1538 if (0 > zone_offset)
1541 /* restart query with CNAME */
1542 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1545 "Asking same server for %s\n", rh->dns_name);
1546 send_dns_packet (rh);
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1551 "Trying system resolver for %s\n", rh->dns_name);
1552 resolve_dns_name (rh);
1555 GNUNET_DNSPARSER_free_packet (packet);
1559 for (i = 0; i < packet->num_authority_records; i++)
1561 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1564 "Found NS delegation!\n");
1565 found_delegation = GNUNET_YES;
1566 delegation_name = packet->authority_records[i].data.hostname;
1571 for (i = 0; i < packet->num_additional_records; i++)
1573 if (GNUNET_NO == found_delegation)
1576 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1577 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1579 GNUNET_assert (sizeof (struct in_addr) ==
1580 packet->authority_records[i].data.raw.data_len);
1582 rh->dns_addr.sin_addr =
1583 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1584 send_dns_packet (rh);
1585 GNUNET_DNSPARSER_free_packet (packet);
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 "Nothing useful in DNS reply!\n");
1592 finish_lookup (rh, rlh, 0, NULL);
1593 GNUNET_DNSPARSER_free_packet (packet);
1598 * Sends a UDP dns query to a nameserver specified in the rh
1600 * @param rh the request handle
1603 send_dns_packet (struct ResolverHandle *rh)
1605 struct GNUNET_NETWORK_FDSet *rset;
1607 rset = GNUNET_NETWORK_fdset_create ();
1608 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1610 "Sending %d byte DNS query\n",
1611 (int) rh->dns_raw_packet_size);
1613 if (GNUNET_SYSERR ==
1614 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1616 rh->dns_raw_packet_size,
1617 (struct sockaddr*)&rh->dns_addr,
1618 sizeof (struct sockaddr_in)))
1619 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1620 _("Failed to send DNS request to %s\n"),
1621 GNUNET_a2s ((const struct sockaddr *)&rh->dns_addr,
1622 sizeof (struct sockaddr_in)));
1624 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1625 rh->timeout, //FIXME less?
1630 GNUNET_NETWORK_fdset_destroy (rset);
1635 * The final phase of resoution.
1636 * We found a NS RR and want to resolve via DNS
1638 * @param rh the pending lookup handle
1639 * @param rd_count length of record data
1640 * @param rd record data containing VPN RR
1643 resolve_record_dns (struct ResolverHandle *rh,
1644 unsigned int rd_count,
1645 const struct GNUNET_NAMESTORE_RecordData *rd)
1647 struct RecordLookupHandle *rlh = rh->proc_cls;
1648 struct GNUNET_DNSPARSER_Query query;
1649 struct GNUNET_DNSPARSER_Packet packet;
1650 struct GNUNET_DNSPARSER_Flags flags;
1651 struct in_addr dnsip;
1652 struct sockaddr_in addr;
1653 struct sockaddr *sa;
1656 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1657 memset (rh->dns_name, 0, sizeof (rh->dns_name));
1659 /* We cancel here as to not include the ns lookup in the timeout */
1660 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1662 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1663 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1665 /* Start shortening */
1666 if ((NULL != rh->priv_key) &&
1667 (GNUNET_YES == is_canonical (rh->name)))
1669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1670 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1672 start_shorten (rh->authority_chain_head,
1676 for (i = 0; i < rd_count; i++)
1678 /* Synthesize dns name */
1679 if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1681 strcpy (rh->dns_zone, (char*)rd[i].data);
1682 if (0 == strcmp (rh->name, ""))
1683 strcpy (rh->dns_name, (char*)rd[i].data);
1685 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1688 if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1689 /* need to use memcpy as .data may be unaligned */
1690 memcpy (&dnsip, rd[i].data, sizeof (dnsip));
1693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1694 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1698 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1699 if (NULL == rh->dns_sock)
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1702 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1704 finish_lookup (rh, rlh, 0, NULL);
1708 memset (&addr, 0, sizeof (struct sockaddr_in));
1709 sa = (struct sockaddr *) &addr;
1710 sa->sa_family = AF_INET;
1711 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1713 sizeof (struct sockaddr_in)))
1715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1716 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1718 finish_lookup (rh, rlh, 0, NULL);
1721 query.name = rh->dns_name;
1722 query.type = rlh->record_type;
1723 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1724 memset (&flags, 0, sizeof (flags));
1725 flags.recursion_desired = 1;
1726 flags.checking_disabled = 1;
1727 packet.queries = &query;
1728 packet.answers = NULL;
1729 packet.authority_records = NULL;
1730 packet.num_queries = 1;
1731 packet.num_answers = 0;
1732 packet.num_authority_records = 0;
1733 packet.num_additional_records = 0;
1734 packet.flags = flags;
1736 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1738 &rh->dns_raw_packet,
1739 &rh->dns_raw_packet_size))
1741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1742 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1744 GNUNET_NETWORK_socket_close (rh->dns_sock);
1745 finish_lookup (rh, rlh, 0, NULL);
1749 rh->dns_addr.sin_family = AF_INET;
1750 rh->dns_addr.sin_port = htons (53); //domain
1751 rh->dns_addr.sin_addr = dnsip;
1752 #if HAVE_SOCKADDR_IN_SIN_LEN
1753 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1755 send_dns_packet (rh);
1760 * The final phase of resoution.
1761 * We found a VPN RR and want to request an IPv4/6 address
1763 * @param rh the pending lookup handle
1764 * @param rd_count length of record data
1765 * @param rd record data containing VPN RR
1768 resolve_record_vpn (struct ResolverHandle *rh,
1769 unsigned int rd_count,
1770 const struct GNUNET_NAMESTORE_RecordData *rd)
1772 struct RecordLookupHandle *rlh = rh->proc_cls;
1773 struct GNUNET_HashCode serv_desc;
1774 struct vpn_data* vpn;
1777 /* We cancel here as to not include the ns lookup in the timeout */
1778 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1780 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1781 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1783 /* Start shortening */
1784 if ((NULL != rh->priv_key) &&
1785 (GNUNET_YES == is_canonical (rh->name)))
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1788 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1790 start_shorten (rh->authority_chain_head,
1794 vpn = (struct vpn_data*)rd->data;
1795 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1796 strlen ((char*)&vpn[1]) + 1,
1798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1799 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1802 GNUNET_h2s (&vpn->peer));
1804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1805 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1808 GNUNET_h2s (&serv_desc));
1809 rh->proc = &handle_record_vpn;
1810 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1815 if (NULL == vpn_handle)
1817 vpn_handle = GNUNET_VPN_connect (cfg);
1818 if (NULL == vpn_handle)
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1821 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1822 finish_lookup (rh, rh->proc_cls, 0, NULL);
1827 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1828 af, ntohs (vpn->proto),
1829 (struct GNUNET_PeerIdentity *)&vpn->peer,
1832 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1833 &process_record_result_vpn,
1836 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1837 "Error connecting to VPN (not available on W32 yet)\n");
1838 finish_lookup (rh, rh->proc_cls, 0, NULL);
1844 * The final phase of resolution.
1845 * rh->name is a name that is canonical and we do not have a delegation.
1846 * Query namestore for this record
1848 * @param rh the pending lookup handle
1851 resolve_record_ns(struct ResolverHandle *rh)
1853 struct RecordLookupHandle *rlh = rh->proc_cls;
1855 /* We cancel here as to not include the ns lookup in the timeout */
1856 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1858 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1859 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1861 /* Start shortening */
1862 if ((NULL != rh->priv_key) &&
1863 (GNUNET_YES == is_canonical (rh->name)))
1865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1866 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1868 start_shorten (rh->authority_chain_head,
1873 * Try to resolve this record in our namestore.
1874 * The name to resolve is now in rh->authority_name
1875 * since we tried to resolve it to an authority
1878 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1882 &process_record_result_ns,
1888 * Handle timeout for DHT requests
1890 * @param cls the request handle as closure
1891 * @param tc the task context
1894 dht_authority_lookup_timeout (void *cls,
1895 const struct GNUNET_SCHEDULER_TaskContext *tc)
1897 struct ResolverHandle *rh = cls;
1898 struct RecordLookupHandle *rlh = rh->proc_cls;
1899 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
1901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1902 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus) timed out.\n",
1903 rh->id, rh->authority_name,
1904 rh->timeout.rel_value);
1906 rh->status |= RSL_TIMED_OUT;
1907 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1908 if (NULL != rh->get_handle)
1910 GNUNET_DHT_get_stop (rh->get_handle);
1911 rh->get_handle = NULL;
1913 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1916 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
1918 rh->proc (rh->proc_cls, rh, 0, NULL);
1921 if (0 == strcmp (rh->name, ""))
1924 * promote authority back to name and try to resolve record
1926 strcpy (rh->name, rh->authority_name);
1927 rh->proc (rh->proc_cls, rh, 0, NULL);
1932 * Start resolution in bg
1934 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
1936 rh->name, rh->authority_name, GNUNET_GNS_TLD);
1937 strcpy (rh->name, new_name);
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1939 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1942 gns_resolver_lookup_record (rh->authority,
1943 rh->private_local_zone,
1947 GNUNET_TIME_UNIT_FOREVER_REL,
1949 &background_lookup_result_processor,
1951 rh->proc (rh->proc_cls, rh, 0, NULL);
1956 * Start DHT lookup for a name -> PKEY (compare NS) record in
1957 * rh->authority's zone
1959 * @param rh the pending gns query
1962 resolve_delegation_dht (struct ResolverHandle *rh);
1966 * Resolve the delegation chain for the request in our namestore
1968 * @param rh the resolver handle
1971 resolve_delegation_ns (struct ResolverHandle *rh);
1975 * Namestore resolution for delegation finished. Processing result.
1977 * @param cls the closure
1978 * @param rh resolver handle
1979 * @param rd_count number of results (always 0)
1980 * @param rd record data (always NULL)
1983 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
1984 unsigned int rd_count,
1985 const struct GNUNET_NAMESTORE_RecordData *rd);
1989 * This is a callback function that checks for key revocation
1991 * @param cls the pending query
1992 * @param key the key of the zone we did the lookup
1993 * @param expiration expiration date of the record data set in the namestore
1994 * @param name the name for which we need an authority
1995 * @param rd_count the number of records with 'name'
1996 * @param rd the record data
1997 * @param signature the signature of the authority for the record data
2000 process_pkey_revocation_result_ns (void *cls,
2001 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2002 struct GNUNET_TIME_Absolute expiration,
2004 unsigned int rd_count,
2005 const struct GNUNET_NAMESTORE_RecordData *rd,
2006 const struct GNUNET_CRYPTO_RsaSignature *signature)
2008 struct ResolverHandle *rh = cls;
2009 struct GNUNET_TIME_Relative remaining_time;
2012 rh->namestore_task = NULL;
2013 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2015 for (i = 0; i < rd_count; i++)
2017 if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2019 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2020 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2022 rh->status |= RSL_PKEY_REVOKED;
2023 rh->proc (rh->proc_cls, rh, 0, NULL);
2028 if ((NULL == name) ||
2029 (0 == remaining_time.rel_value))
2031 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2032 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2035 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2037 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2038 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2039 rh->id, "+.gads", GNUNET_GNS_RECORD_REV);
2041 gns_resolver_lookup_record(rh->authority,
2042 rh->private_local_zone,
2043 GNUNET_GNS_RECORD_REV,
2046 GNUNET_TIME_UNIT_FOREVER_REL,
2048 &background_lookup_result_processor,
2052 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2053 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2056 * We are done with PKEY resolution if name is empty
2057 * else resolve again with new authority
2059 if (strcmp (rh->name, "") == 0)
2060 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2062 resolve_delegation_ns (rh);
2067 * Callback when record data is put into namestore
2069 * @param cls the closure
2070 * @param success GNUNET_OK on success
2071 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2074 on_namestore_delegation_put_result(void *cls,
2078 struct NamestoreBGTask *nbg = cls;
2080 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2083 if (GNUNET_NO == success)
2085 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2086 "GNS_NS: records already in namestore\n");
2089 else if (GNUNET_YES == success)
2091 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2092 "GNS_NS: records successfully put in namestore\n");
2096 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2097 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2102 * Function called when we get a result from the dht
2103 * for our query. Recursively tries to resolve authorities
2106 * @param cls the request handle
2107 * @param exp lifetime
2108 * @param key the key the record was stored under
2109 * @param get_path get path
2110 * @param get_path_length get path length
2111 * @param put_path put path
2112 * @param put_path_length put path length
2113 * @param type the block type
2114 * @param size the size of the record
2115 * @param data the record data
2118 process_delegation_result_dht (void* cls,
2119 struct GNUNET_TIME_Absolute exp,
2120 const struct GNUNET_HashCode * key,
2121 const struct GNUNET_PeerIdentity *get_path,
2122 unsigned int get_path_length,
2123 const struct GNUNET_PeerIdentity *put_path,
2124 unsigned int put_path_length,
2125 enum GNUNET_BLOCK_Type type,
2126 size_t size, const void *data)
2128 struct ResolverHandle *rh = cls;
2129 const struct GNSNameRecordBlock *nrb = data;
2130 const char* rd_data;
2131 uint32_t num_records;
2135 struct GNUNET_CRYPTO_ShortHashCode zone;
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2138 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n",
2142 /* stop dht lookup and timeout task */
2143 GNUNET_DHT_get_stop (rh->get_handle);
2144 rh->get_handle = NULL;
2145 if (rh->dht_heap_node != NULL)
2147 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2148 rh->dht_heap_node = NULL;
2151 num_records = ntohl(nrb->rd_count);
2152 name = (const char*) &nrb[1];
2154 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2155 struct NamestoreBGTask *ns_heap_root;
2156 struct NamestoreBGTask *namestore_bg_task;
2158 rd_data = name + strlen(name) + 1;
2159 rd_size = size - strlen(name) - 1 - sizeof (struct GNSNameRecordBlock);
2160 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2165 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2166 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2171 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2172 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2173 rh->id, name, rh->authority_name);
2174 for (i=0; i<num_records; i++)
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2177 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2178 rh->id, name, rh->authority_name);
2179 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2180 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2181 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2182 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2183 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2184 rh->id, rd[i].data_size);
2185 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2186 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2187 rh->id, rd[i].flags);
2189 if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2190 (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2191 (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2194 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2196 if (0 == strcmp(rh->name, ""))
2197 strcpy(rh->name, rh->authority_name);
2199 GNUNET_snprintf(rh->name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
2200 rh->name, rh->authority_name); //FIXME ret
2205 if ((0 == strcmp(name, rh->authority_name)) &&
2206 (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2208 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2209 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2212 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2213 struct AuthorityChain *auth =
2214 GNUNET_malloc(sizeof(struct AuthorityChain));
2215 auth->zone = rh->authority;
2216 memset(auth->name, 0, strlen(rh->authority_name)+1);
2217 strcpy(auth->name, rh->authority_name);
2218 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2219 rh->authority_chain_tail,
2222 if (NULL != rh->rd.data)
2223 GNUNET_free ((void*)rh->rd.data);
2225 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2226 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2227 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2230 /** try to import pkey if private key available */
2231 //if (rh->priv_key && is_canonical (rh->name))
2232 // process_discovered_authority(name, auth->zone,
2233 // rh->authority_chain_tail->zone,
2238 GNUNET_GNS_get_zone_from_key (name, key, &zone);
2241 /* Save to namestore
2242 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2245 if (max_allowed_ns_tasks <=
2246 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2248 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2249 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2251 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2252 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background ns task\n",
2255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2256 "GNS_PHASE_DELEGATE_DHT-%llu: Caching record for %s\n",
2258 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2260 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2262 GNUNET_TIME_absolute_get().abs_value);
2263 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2270 &on_namestore_delegation_put_result, //cont
2271 namestore_bg_task); //cls
2275 if (0 != rh->answered)
2280 * FIXME in this case. should we ask namestore again?
2282 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2283 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2284 rh->id, rh->authority_name, rh->name);
2286 if (0 == strcmp(rh->name, ""))
2288 /* Start shortening */
2289 if ((NULL != rh->priv_key) &&
2290 (GNUNET_YES == is_canonical (rh->name)))
2292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2293 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2295 start_shorten (rh->authority_chain_head,
2300 rh->proc = &handle_delegation_ns;
2303 /* Check for key revocation and delegate */
2304 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2306 GNUNET_GNS_MASTERZONE_STR,
2307 GNUNET_GNS_RECORD_REV,
2308 &process_pkey_revocation_result_ns,
2315 * No pkey but name exists
2318 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2319 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2320 rh->id, rh->authority_name, rh->name);
2321 if (0 == strcmp(rh->name, ""))
2322 strcpy(rh->name, rh->authority_name);
2324 GNUNET_snprintf(rh->name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
2325 rh->name, rh->authority_name); //FIXME ret
2327 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2328 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2329 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2330 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2332 rh->proc(rh->proc_cls, rh, 0, NULL);
2335 //FIXME maybe define somewhere else?
2336 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2337 +(GNUNET_DNSPARSER_MAX_NAME_LENGTH*2)
2338 #define MAX_MX_LENGTH sizeof(uint16_t)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
2339 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
2343 * Exands a name ending in .+ with the zone of origin.
2344 * FIXME: funky api: 'dest' must be large enough to hold
2345 * the result; this is a bit yucky...
2347 * @param dest destination buffer
2348 * @param src the .+ name
2349 * @param repl the string to replace the + with
2352 expand_plus (char* dest,
2357 size_t s_len = strlen (src) + 1;
2359 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2362 /* no postprocessing */
2363 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2364 "GNS_POSTPROCESS: %s too short\n", src);
2365 memcpy (dest, src, s_len);
2368 if (0 == strcmp (src + s_len - 3, ".+"))
2370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2371 "GNS_POSTPROCESS: Expanding .+ in %s\n",
2373 memset (dest, 0, s_len + strlen (repl) + strlen(GNUNET_GNS_TLD));
2375 pos = dest + s_len - 2;
2377 pos += strlen (repl);
2378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2379 "GNS_POSTPROCESS: Expanded to %s\n",
2384 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2385 "GNS_POSTPROCESS: No postprocessing for %s\n", src);
2386 memcpy (dest, src, s_len);
2395 finish_lookup (struct ResolverHandle *rh,
2396 struct RecordLookupHandle* rlh,
2397 unsigned int rd_count,
2398 const struct GNUNET_NAMESTORE_RecordData *rd)
2401 char new_rr_data[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
2402 char new_mx_data[MAX_MX_LENGTH];
2403 char new_soa_data[MAX_SOA_LENGTH];
2404 char new_srv_data[MAX_SRV_LENGTH];
2405 struct srv_data *old_srv;
2406 struct srv_data *new_srv;
2407 struct soa_data *old_soa;
2408 struct soa_data *new_soa;
2409 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2412 unsigned int offset;
2414 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
2416 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2417 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2420 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2423 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2425 for (i = 0; i < rd_count; i++)
2428 if ((GNUNET_GNS_RECORD_NS != rd[i].record_type) &&
2429 (GNUNET_GNS_RECORD_PTR != rd[i].record_type) &&
2430 (GNUNET_GNS_RECORD_CNAME != rd[i].record_type) &&
2431 (GNUNET_GNS_RECORD_MX != rd[i].record_type) &&
2432 (GNUNET_GNS_RECORD_SOA != rd[i].record_type) &&
2433 (GNUNET_GNS_RECORD_SRV != rd[i].record_type))
2435 p_rd[i].data = rd[i].data;
2440 * for all those records we 'should'
2441 * also try to resolve the A/AAAA records (RFC1035)
2442 * This is a feature and not important
2445 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2446 "GNS_POSTPROCESS: Postprocessing\n");
2447 if (0 == strcmp(rh->name, GNUNET_GNS_MASTERZONE_STR))
2448 repl_string = rlh->name;
2450 repl_string = rlh->name+strlen(rh->name)+1;
2453 if (GNUNET_GNS_RECORD_MX == rd[i].record_type)
2455 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2456 offset = sizeof (uint16_t);
2457 pos = new_mx_data + offset;
2458 // FIXME: how do we know that 'pos' has enough space for the new name?
2459 expand_plus (pos, (char*)rd[i].data+sizeof(uint16_t),
2461 offset += strlen(new_mx_data+sizeof(uint16_t)) + 1;
2462 p_rd[i].data = new_mx_data;
2463 p_rd[i].data_size = offset;
2465 else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2468 * Prio, weight and port
2470 new_srv = (struct srv_data*)new_srv_data;
2471 old_srv = (struct srv_data*)rd[i].data;
2472 new_srv->prio = old_srv->prio;
2473 new_srv->weight = old_srv->weight;
2474 new_srv->port = old_srv->port;
2475 // FIXME: how do we know that '&new_srv[1]' has enough space for the new name?
2476 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2478 p_rd[i].data = new_srv_data;
2479 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2481 else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2483 /* expand mname and rname */
2484 old_soa = (struct soa_data*)rd[i].data;
2485 new_soa = (struct soa_data*)new_soa_data;
2486 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2487 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2488 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2489 offset = strlen ((char*)&new_soa[1]) + 1;
2490 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2491 expand_plus((char*)&new_soa[1] + offset,
2492 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2494 p_rd[i].data_size = sizeof (struct soa_data)
2496 + strlen ((char*)&new_soa[1] + offset);
2497 p_rd[i].data = new_soa_data;
2502 // FIXME: how do we know that 'rd[i].data' has enough space for the new name?
2503 expand_plus(pos, (char*)rd[i].data, repl_string);
2504 p_rd[i].data_size = strlen(new_rr_data)+1;
2505 p_rd[i].data = new_rr_data;
2510 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2512 free_resolver_handle (rh);
2517 * Process DHT lookup result for record.
2519 * @param cls the closure
2520 * @param rh resolver handle
2521 * @param rd_count number of results
2522 * @param rd record data
2525 handle_record_dht (void* cls, struct ResolverHandle *rh,
2526 unsigned int rd_count,
2527 const struct GNUNET_NAMESTORE_RecordData *rd)
2529 struct RecordLookupHandle* rlh = cls;
2533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2534 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2536 /* give up, cannot resolve */
2537 finish_lookup (rh, rlh, 0, NULL);
2540 /* results found yay */
2541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2542 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2543 finish_lookup (rh, rlh, rd_count, rd);
2548 * Process namestore lookup result for record.
2550 * @param cls the closure
2551 * @param rh resolver handle
2552 * @param rd_count number of results
2553 * @param rd record data
2556 handle_record_ns (void* cls, struct ResolverHandle *rh,
2557 unsigned int rd_count,
2558 const struct GNUNET_NAMESTORE_RecordData *rd)
2560 struct RecordLookupHandle* rlh = cls;
2561 int check_dht = GNUNET_YES;
2565 /* results found yay */
2566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2567 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2568 finish_lookup (rh, rlh, rd_count, rd);
2572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2573 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2577 * There are 5 conditions that have to met for us to consult the DHT:
2578 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2579 * 2. No entry in the NS existed AND
2580 * 3. The zone queried is not the local resolver's zone AND
2581 * 4. The name that was looked up is '+'
2582 * because if it was any other canonical name we either already queried
2583 * the DHT for the authority in the authority lookup phase (and thus
2584 * would already have an entry in the NS for the record)
2585 * 5. We are not in cache only mode
2587 if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2588 (0 == (rh->status & RSL_RECORD_EXISTS)) )
2591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2592 "GNS_PHASE_REC-%llu: Not expired and exists!\n",
2594 check_dht = GNUNET_NO;
2597 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2598 &rh->private_local_zone))
2601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2602 "GNS_PHASE_REC-%llu: Our zone!\n",
2604 check_dht = GNUNET_NO;
2607 if ((0 != strcmp (rh->name, GNUNET_GNS_MASTERZONE_STR)) && (GNUNET_YES == is_srv (rh->name)))
2608 check_dht = GNUNET_NO;
2610 if (GNUNET_YES == rh->only_cached)
2611 check_dht = GNUNET_NO;
2613 if (GNUNET_YES == check_dht)
2615 rh->proc = &handle_record_dht;
2616 resolve_record_dht(rh);
2619 /* give up, cannot resolve */
2620 finish_lookup (rh, rlh, 0, NULL);
2625 * Move one level up in the domain hierarchy and return the
2626 * passed top level domain.
2628 * FIXME: funky API: not only 'dest' is updated, so is 'name'!
2630 * @param name the domain
2631 * @param dest the destination where the tld will be put
2634 pop_tld (char* name, char* dest)
2638 if (GNUNET_YES == is_canonical (name))
2640 strcpy (dest, name);
2645 for (len = strlen(name); 0 < len; len--)
2647 if (*(name+len) == '.')
2657 strcpy(dest, (name+len+1));
2662 * DHT resolution for delegation finished. Processing result.
2664 * @param cls the closure
2665 * @param rh resolver handle
2666 * @param rd_count number of results (always 0)
2667 * @param rd record data (always NULL)
2670 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2671 unsigned int rd_count,
2672 const struct GNUNET_NAMESTORE_RecordData *rd)
2674 struct RecordLookupHandle* rlh = cls;
2676 if (0 == strcmp(rh->name, ""))
2678 if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2680 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2681 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2683 finish_lookup(rh, rlh, rd_count, rd);
2686 /* We resolved full name for delegation. resolving record */
2687 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2688 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2690 strcpy(rh->name, "+\0");
2691 rh->proc = &handle_record_ns;
2692 resolve_record_ns(rh);
2697 * we still have some left
2699 if (GNUNET_YES == is_canonical (rh->name))
2701 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2702 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2705 rh->proc = &handle_record_ns;
2706 resolve_record_ns(rh);
2709 /* give up, cannot resolve */
2710 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2711 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2713 finish_lookup(rh, rlh, 0, NULL);
2718 * Start DHT lookup for a name -> PKEY (compare NS) record in
2719 * rh->authority's zone
2721 * @param rh the pending gns query
2724 resolve_delegation_dht (struct ResolverHandle *rh)
2727 struct GNUNET_HashCode lookup_key;
2728 struct ResolverHandle *rh_heap_root;
2730 pop_tld (rh->name, rh->authority_name);
2731 GNUNET_GNS_get_key_for_record (rh->authority_name,
2734 rh->dht_heap_node = NULL;
2735 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2737 rh->timeout_cont = &dht_authority_lookup_timeout;
2738 rh->timeout_cont_cls = rh;
2742 if (max_allowed_background_queries <=
2743 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2745 /* terminate oldest lookup */
2746 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2747 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2748 rh_heap_root->get_handle = NULL;
2749 rh_heap_root->dht_heap_node = NULL;
2750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2751 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2753 rh_heap_root->authority_name);
2754 rh_heap_root->proc (rh_heap_root->proc_cls,
2759 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2761 GNUNET_TIME_absolute_get().abs_value);
2763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2764 "Beginning DHT lookup for %s in zone %s for request %llu\n",
2766 GNUNET_short_h2s (&rh->authority),
2768 xquery = htonl (GNUNET_GNS_RECORD_PKEY);
2769 GNUNET_assert (rh->get_handle == NULL);
2770 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
2771 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2773 DHT_GNS_REPLICATION_LEVEL,
2774 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2777 &process_delegation_result_dht,
2783 * Checks if name is in tld
2785 * @param name the name to check
2786 * @param tld the TLD to check for
2787 * @return GNUNET_YES or GNUNET_NO
2790 is_tld (const char* name, const char* tld)
2794 if (strlen (name) <= strlen (tld))
2797 offset = strlen (name) - strlen (tld);
2798 if (0 != strcmp (name + offset, tld))
2800 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2801 "%s is not in .%s TLD\n", name, tld);
2809 * Namestore resolution for delegation finished. Processing result.
2811 * @param cls the closure
2812 * @param rh resolver handle
2813 * @param rd_count number of results (always 0)
2814 * @param rd record data (always NULL)
2817 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2818 unsigned int rd_count,
2819 const struct GNUNET_NAMESTORE_RecordData *rd)
2821 struct RecordLookupHandle* rlh = cls;
2825 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2826 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2827 rh->id, rh->status);
2829 if (rh->status & RSL_PKEY_REVOKED)
2831 finish_lookup (rh, rlh, 0, NULL);
2835 if (0 == strcmp(rh->name, ""))
2838 /* We resolved full name for delegation. resolving record */
2839 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2840 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2842 if (rh->status & RSL_CNAME_FOUND)
2844 if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2846 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2847 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2849 strcpy (rh->name, rh->authority_name);
2850 finish_lookup (rh, rlh, rd_count, rd);
2855 if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2857 s_len = strlen (rd->data) - 2;
2858 memcpy (rh->name, rd->data, s_len);
2859 rh->name[s_len] = '\0';
2860 resolve_delegation_ns (rh);
2863 else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2865 gns_resolver_lookup_record (rh->authority,
2866 rh->private_local_zone,
2875 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2876 free_resolver_handle (rh);
2882 strcpy (rh->dns_name, (char*)rd->data);
2883 resolve_dns_name (rh);
2888 else if (rh->status & RSL_DELEGATE_VPN)
2890 if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2892 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2893 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2895 finish_lookup(rh, rlh, rd_count, rd);
2898 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2899 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2901 GNUNET_assert (NULL != rd);
2902 rh->proc = &handle_record_vpn;
2903 resolve_record_vpn (rh, rd_count, rd);
2906 else if (rh->status & RSL_DELEGATE_NS)
2908 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2910 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2911 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2913 finish_lookup(rh, rlh, rd_count, rd);
2917 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2918 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2920 GNUNET_assert (NULL != rd);
2921 rh->proc = &handle_record_ns;
2922 resolve_record_dns (rh, rd_count, rd);
2925 else if (rh->status & RSL_DELEGATE_PKEY)
2927 if (rh->status & RSL_PKEY_REVOKED)
2929 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2930 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
2932 finish_lookup (rh, rlh, 0, NULL);
2935 else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2937 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2938 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
2940 finish_lookup(rh, rlh, rd_count, rd);
2944 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2945 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
2947 strcpy(rh->name, "+\0");
2948 rh->proc = &handle_record_ns;
2949 resolve_record_ns(rh);
2953 if (rh->status & RSL_DELEGATE_NS)
2955 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2957 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2958 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2960 finish_lookup(rh, rlh, rd_count, rd);
2964 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2965 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2967 GNUNET_assert (NULL != rd);
2968 rh->proc = &handle_record_ns;
2969 resolve_record_dns (rh, rd_count, rd);
2974 * we still have some left
2975 * check if authority in ns is fresh
2977 * or we are authority
2980 check_dht = GNUNET_YES;
2981 if ((rh->status & RSL_RECORD_EXISTS) &&
2982 !(rh->status & RSL_RECORD_EXPIRED))
2983 check_dht = GNUNET_NO;
2985 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2986 &rh->private_local_zone))
2987 check_dht = GNUNET_NO;
2989 if (GNUNET_YES == rh->only_cached)
2990 check_dht = GNUNET_NO;
2992 if (GNUNET_YES == check_dht)
2995 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2996 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
2998 rh->proc = &handle_delegation_dht;
2999 resolve_delegation_dht(rh);
3003 if (GNUNET_NO == is_canonical (rh->name))
3005 /* give up, cannot resolve */
3006 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3007 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3010 finish_lookup(rh, rlh, rd_count, rd);
3013 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3014 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3017 rh->proc = &handle_record_ns;
3018 resolve_record_ns(rh);
3023 * This is a callback function that should give us only PKEY
3024 * records. Used to query the namestore for the authority (PKEY)
3025 * for 'name'. It will recursively try to resolve the
3026 * authority for a given name from the namestore.
3028 * @param cls the pending query
3029 * @param key the key of the zone we did the lookup
3030 * @param expiration expiration date of the record data set in the namestore
3031 * @param name the name for which we need an authority
3032 * @param rd_count the number of records with 'name'
3033 * @param rd the record data
3034 * @param signature the signature of the authority for the record data
3037 process_delegation_result_ns (void* cls,
3038 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3039 struct GNUNET_TIME_Absolute expiration,
3041 unsigned int rd_count,
3042 const struct GNUNET_NAMESTORE_RecordData *rd,
3043 const struct GNUNET_CRYPTO_RsaSignature *signature)
3045 struct ResolverHandle *rh = cls;
3046 struct GNUNET_TIME_Relative remaining_time;
3047 struct GNUNET_CRYPTO_ShortHashCode zone;
3048 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3050 struct GNUNET_TIME_Absolute et;
3052 rh->namestore_task = NULL;
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3054 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3057 GNUNET_CRYPTO_short_hash (key,
3058 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3060 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3067 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3069 rh->status |= RSL_RECORD_EXISTS;
3071 if (remaining_time.rel_value == 0)
3073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3074 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3076 rh->status |= RSL_RECORD_EXPIRED;
3081 * No authority found in namestore.
3086 * We did not find an authority in the namestore
3091 * Promote this authority back to a name maybe it is
3094 if (strcmp (rh->name, "") == 0)
3096 /* simply promote back */
3097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3098 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3099 rh->id, rh->authority_name);
3100 strcpy (rh->name, rh->authority_name);
3104 /* add back to existing name */
3105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3106 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3107 rh->id, rh->authority_name, rh->name);
3108 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
3109 rh->name, rh->authority_name);
3110 strcpy (rh->name, new_name);
3111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3112 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3116 rh->proc (rh->proc_cls, rh, 0, NULL);
3121 * We found an authority that may be able to help us
3122 * move on with query
3123 * Note only 1 pkey should have been returned.. anything else would be strange
3125 for (i=0; i < rd_count;i++)
3129 * A CNAME. Like regular DNS this means the is no other record for this
3132 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3134 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3135 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3138 rh->status |= RSL_CNAME_FOUND;
3139 rh->proc (rh->proc_cls, rh, rd_count, rd);
3146 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3148 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3149 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3151 rh->status |= RSL_DELEGATE_VPN;
3152 rh->proc (rh->proc_cls, rh, rd_count, rd);
3158 * FIXME make optional
3160 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3162 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3163 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3165 rh->status |= RSL_DELEGATE_NS;
3166 rh->proc (rh->proc_cls, rh, rd_count, rd);
3170 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3173 rh->status |= RSL_DELEGATE_PKEY;
3175 if ((ignore_pending_records != 0) &&
3176 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3179 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3185 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3186 et.abs_value = rd[i].expiration_time;
3187 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3191 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3193 if (remaining_time.rel_value == 0)
3195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3196 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3198 rh->authority_chain_head->fresh = 0;
3199 rh->proc (rh->proc_cls, rh, 0, NULL);
3207 * Resolve rest of query with new authority
3209 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3210 memcpy (&rh->authority, rd[i].data,
3211 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3212 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3213 auth->zone = rh->authority;
3214 memset (auth->name, 0, strlen (rh->authority_name)+1);
3215 strcpy (auth->name, rh->authority_name);
3216 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3217 rh->authority_chain_tail,
3219 if (NULL != rh->rd.data)
3220 GNUNET_free ((void*)(rh->rd.data));
3222 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3223 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3224 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3226 /* Check for key revocation and delegate */
3227 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3229 GNUNET_GNS_MASTERZONE_STR,
3230 GNUNET_GNS_RECORD_REV,
3231 &process_pkey_revocation_result_ns,
3239 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3240 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3242 * If we have found some records for the LAST label
3243 * we return the results. Else null.
3245 if (strcmp (rh->name, "") == 0)
3247 /* Start shortening */
3248 if ((rh->priv_key != NULL) &&
3249 (is_canonical (rh->name) == GNUNET_YES))
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3252 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3254 start_shorten (rh->authority_chain_head,
3257 /* simply promote back */
3258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3259 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3260 rh->id, rh->authority_name);
3261 strcpy (rh->name, rh->authority_name);
3262 rh->proc (rh->proc_cls, rh, rd_count, rd);
3266 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3267 "%s.%s", rh->name, rh->authority_name);
3268 strcpy (rh->name, new_name);
3269 rh->proc (rh->proc_cls, rh, 0, NULL);
3275 * Resolve the delegation chain for the request in our namestore
3277 * @param rh the resolver handle
3280 resolve_delegation_ns (struct ResolverHandle *rh)
3282 pop_tld (rh->name, rh->authority_name);
3283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3284 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s by looking up %s\n",
3286 rh->authority_name);
3287 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3290 GNUNET_GNS_RECORD_ANY,
3291 &process_delegation_result_ns,
3297 * Lookup of a record in a specific zone
3298 * calls lookup result processor on result
3300 * @param zone the root zone
3301 * @param pzone the private local zone
3302 * @param record_type the record type to look up
3303 * @param name the name to look up
3304 * @param key a private key for use with PSEU import (can be NULL)
3305 * @param timeout timeout for resolution
3306 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3307 * @param proc the processor to call on result
3308 * @param cls the closure to pass to proc
3311 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3312 struct GNUNET_CRYPTO_ShortHashCode pzone,
3313 uint32_t record_type,
3315 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3316 struct GNUNET_TIME_Relative timeout,
3318 RecordLookupProcessor proc,
3321 struct ResolverHandle *rh;
3322 struct RecordLookupHandle* rlh;
3323 char string_hash[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
3324 char nzkey[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
3325 char* nzkey_ptr = nzkey;
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3328 "Starting resolution for %s (type=%d) with timeout %s!\n",
3330 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
3332 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3333 (strcmp(GNUNET_GNS_TLD, name) != 0))
3335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3336 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3341 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3342 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3343 rh->authority = zone;
3347 rh->timeout = timeout;
3348 rh->private_local_zone = pzone;
3349 rh->only_cached = only_cached;
3351 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3355 "No shorten key for resolution\n");
3357 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3360 * Set timeout for authority lookup phase to 1/2
3362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3363 "Timeout for lookup set to %s/2\n",
3364 GNUNET_STRINGS_relative_time_to_string (rh->timeout, GNUNET_YES));
3365 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
3366 GNUNET_TIME_relative_divide(timeout, 2),
3367 &handle_lookup_timeout,
3369 rh->timeout_cont = &dht_authority_lookup_timeout;
3370 rh->timeout_cont_cls = rh;
3374 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3375 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3378 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3381 * Only 'gnunet' given
3383 strcpy(rh->name, "\0");
3387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3388 "Checking for TLD...\n");
3389 if (is_zkey_tld(name) == GNUNET_YES)
3391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3394 * This is a zkey tld
3395 * build hash and use as initial authority
3398 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3399 memcpy(rh->name, name,
3400 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3401 pop_tld(rh->name, string_hash);
3403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3404 "ZKEY is %s!\n", string_hash);
3406 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3408 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3412 "Cannot convert ZKEY `%s' to hash!\n", string_hash);
3414 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3415 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3416 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3419 proc (cls, 0, NULL);
3424 else if (is_gads_tld (name) == GNUNET_YES)
3426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3429 * Presumably GADS tld
3431 memcpy (rh->name, name,
3432 strlen (name) - strlen(GNUNET_GNS_TLD) - 1);
3433 rh->name[strlen (name) - strlen(GNUNET_GNS_TLD) - 1] = '\0';
3437 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3438 "Cannot handle this TLD %s\n", string_hash);
3440 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3441 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3442 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3445 proc (cls, 0, NULL);
3451 * Initialize authority chain
3453 rh->authority_chain_head = GNUNET_malloc (sizeof(struct AuthorityChain));
3454 rh->authority_chain_tail = rh->authority_chain_head;
3455 rh->authority_chain_head->zone = rh->authority;
3456 strcpy (rh->authority_chain_head->name, "");
3459 * Copy original query into lookup handle
3461 rlh->record_type = record_type;
3462 memset(rlh->name, 0, strlen(name) + 1);
3463 strcpy(rlh->name, name);
3465 rlh->proc_cls = cls;
3467 rh->proc = &handle_delegation_ns;
3468 resolve_delegation_ns(rh);
3471 /******** END Record Resolver ***********/
3474 finish_shorten (struct ResolverHandle *rh,
3475 struct NameShortenHandle *nsh)
3477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3478 "Sending %s as shorten result\n", nsh->result);
3479 nsh->proc (nsh->proc_cls, nsh->result);
3480 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3482 free_resolver_handle (rh);
3487 * Callback calles by namestore for a zone to name
3490 * @param cls the closure
3491 * @param zone_key the zone we queried
3492 * @param expire the expiration time of the name
3493 * @param name the name found or NULL
3494 * @param rd_len number of records for the name
3495 * @param rd the record data (PKEY) for the name
3496 * @param signature the signature for the record data
3499 process_zone_to_name_shorten_root (void *cls,
3500 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3501 struct GNUNET_TIME_Absolute expire,
3503 unsigned int rd_len,
3504 const struct GNUNET_NAMESTORE_RecordData *rd,
3505 const struct GNUNET_CRYPTO_RsaSignature *signature);
3509 * Callback called by namestore for a zone to name
3512 * @param cls the closure
3513 * @param zone_key the zone we queried
3514 * @param expire the expiration time of the name
3515 * @param name the name found or NULL
3516 * @param rd_len number of records for the name
3517 * @param rd the record data (PKEY) for the name
3518 * @param signature the signature for the record data
3521 process_zone_to_name_shorten_shorten (void *cls,
3522 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3523 struct GNUNET_TIME_Absolute expire,
3525 unsigned int rd_len,
3526 const struct GNUNET_NAMESTORE_RecordData *rd,
3527 const struct GNUNET_CRYPTO_RsaSignature *signature)
3529 struct ResolverHandle *rh = cls;
3530 struct NameShortenHandle* nsh = rh->proc_cls;
3531 struct AuthorityChain *next_authority;
3533 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3534 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3537 rh->namestore_task = NULL;
3538 /* we found a match in our own root zone */
3541 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3542 memset(result, 0, answer_len);
3544 if (strlen(rh->name) > 0)
3546 sprintf (result, "%s.%s.%s.%s",
3548 nsh->shorten_zone_name,
3553 sprintf (result, "%s.%s.%s", name,
3554 nsh->shorten_zone_name,
3558 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3559 "Found shorten result %s\n", result);
3560 if (strlen (nsh->result) > strlen (result))
3561 strcpy (nsh->result, result);
3563 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3564 nsh->shorten_zone) == 0)
3567 * This is our zone append .gads unless name is empty
3568 * (it shouldn't be, usually FIXME what happens if we
3569 * shorten to our zone to a "" record??)
3572 sprintf (result, "%s.%s.%s",
3574 nsh->shorten_zone_name,
3576 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3577 "Our zone: Found %s as shorten result\n", result);
3579 if (strlen (nsh->result) > strlen (result))
3580 strcpy (nsh->result, result);
3581 //nsh->proc(nsh->proc_cls, result);
3583 //free_resolver_handle(rh);
3590 * continue with next authority if exists
3592 if (NULL == rh->authority_chain_head->next)
3594 finish_shorten (rh, nsh);
3597 next_authority = rh->authority_chain_head;
3599 if (0 == strcmp (rh->name, ""))
3600 strcpy (tmp_name, next_authority->name);
3602 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3603 "%s.%s", rh->name, next_authority->name);
3605 strcpy(rh->name, tmp_name);
3606 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3607 "No PSEU found for authority %s. Promoting back: %s\n",
3608 next_authority->name, rh->name);
3610 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3611 rh->authority_chain_tail,
3614 GNUNET_free (next_authority);
3616 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3617 &rh->authority_chain_tail->zone,
3618 &rh->authority_chain_head->zone,
3619 &process_zone_to_name_shorten_root,
3625 * Callback calles by namestore for a zone to name
3628 * @param cls the closure
3629 * @param zone_key the zone we queried
3630 * @param expire the expiration time of the name
3631 * @param name the name found or NULL
3632 * @param rd_len number of records for the name
3633 * @param rd the record data (PKEY) for the name
3634 * @param signature the signature for the record data
3637 process_zone_to_name_shorten_private (void *cls,
3638 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3639 struct GNUNET_TIME_Absolute expire,
3641 unsigned int rd_len,
3642 const struct GNUNET_NAMESTORE_RecordData *rd,
3643 const struct GNUNET_CRYPTO_RsaSignature *signature)
3645 struct ResolverHandle *rh = cls;
3646 struct NameShortenHandle* nsh = rh->proc_cls;
3647 struct AuthorityChain *next_authority;
3649 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3650 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3653 rh->namestore_task = NULL;
3654 /* we found a match in our own root zone */
3657 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3658 memset(result, 0, answer_len);
3660 if (strlen(rh->name) > 0)
3662 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3666 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3669 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3670 "Found shorten result %s\n", result);
3671 if (strlen (nsh->result) > strlen (result))
3672 strcpy (nsh->result, result);
3674 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3675 nsh->private_zone) == 0)
3678 * This is our zone append .gads unless name is empty
3679 * (it shouldn't be, usually FIXME what happens if we
3680 * shorten to our zone to a "" record??)
3683 sprintf (result, "%s.%s.%s",
3684 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3685 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3686 "Our private zone: Found %s as shorten result %s\n", result);
3687 if (strlen (nsh->result) > strlen (result))
3688 strcpy (nsh->result, result);
3691 if (0 != strcmp (nsh->shorten_zone_name, ""))
3693 /* backtrack authorities for names in priv zone */
3694 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3696 &rh->authority_chain_head->zone,
3697 &process_zone_to_name_shorten_shorten,
3704 * continue with next authority if exists
3706 if (NULL == rh->authority_chain_head->next)
3708 finish_shorten (rh, nsh);
3711 next_authority = rh->authority_chain_head;
3713 if (0 == strcmp (rh->name, ""))
3714 strcpy (tmp_name, next_authority->name);
3716 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3717 "%s.%s", rh->name, next_authority->name);
3719 strcpy(rh->name, tmp_name);
3720 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3721 "No PSEU found for authority %s. Promoting back: %s\n",
3722 next_authority->name, rh->name);
3724 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3725 rh->authority_chain_tail,
3728 GNUNET_free (next_authority);
3730 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3731 &rh->authority_chain_tail->zone,
3732 &rh->authority_chain_head->zone,
3733 &process_zone_to_name_shorten_root,
3740 * Callback calles by namestore for a zone to name
3743 * @param cls the closure
3744 * @param zone_key the zone we queried
3745 * @param expire the expiration time of the name
3746 * @param name the name found or NULL
3747 * @param rd_len number of records for the name
3748 * @param rd the record data (PKEY) for the name
3749 * @param signature the signature for the record data
3752 process_zone_to_name_shorten_root (void *cls,
3753 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3754 struct GNUNET_TIME_Absolute expire,
3756 unsigned int rd_len,
3757 const struct GNUNET_NAMESTORE_RecordData *rd,
3758 const struct GNUNET_CRYPTO_RsaSignature *signature)
3760 struct ResolverHandle *rh = cls;
3761 struct NameShortenHandle* nsh = rh->proc_cls;
3762 struct AuthorityChain *next_authority;
3763 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3764 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3767 rh->namestore_task = NULL;
3768 /* we found a match in our own root zone */
3771 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3772 memset(result, 0, answer_len);
3774 if (strlen(rh->name) > 0)
3776 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3780 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3783 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3784 "Found shorten result %s\n", result);
3785 if (strlen (nsh->result) > strlen (result))
3786 strcpy (nsh->result, result);
3788 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3789 nsh->root_zone) == 0)
3792 * This is our zone append .gads unless name is empty
3793 * (it shouldn't be, usually FIXME what happens if we
3794 * shorten to our zone to a "" record??)
3797 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3798 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3799 "Our zone: Found %s as shorten result\n", result);
3800 if (strlen (nsh->result) > strlen (result))
3801 strcpy (nsh->result, result);
3804 if (NULL != nsh->private_zone)
3806 /* backtrack authorities for names in priv zone */
3807 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3809 &rh->authority_chain_head->zone,
3810 &process_zone_to_name_shorten_private,
3813 else if (NULL != nsh->shorten_zone)
3815 /* backtrack authorities for names in shorten zone */
3816 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3818 &rh->authority_chain_head->zone,
3819 &process_zone_to_name_shorten_shorten,
3826 * continue with next authority if exists
3828 if (NULL == rh->authority_chain_head->next)
3830 finish_shorten (rh, nsh);
3833 next_authority = rh->authority_chain_head;
3835 if (0 == strcmp (rh->name, ""))
3836 strcpy (tmp_name, next_authority->name);
3838 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3839 "%s.%s", rh->name, next_authority->name);
3841 strcpy(rh->name, tmp_name);
3842 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3843 "No PSEU found for authority %s. Promoting back: %s\n",
3844 next_authority->name, rh->name);
3846 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3847 rh->authority_chain_tail,
3850 GNUNET_free (next_authority);
3852 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3853 &rh->authority_chain_tail->zone,
3854 &rh->authority_chain_head->zone,
3855 &process_zone_to_name_shorten_root,
3862 * Process result from namestore delegation lookup
3863 * for shorten operation
3865 * @param cls the client shorten handle
3866 * @param rh the resolver handle
3867 * @param rd_count number of results (0)
3868 * @param rd data (NULL)
3871 handle_delegation_ns_shorten (void* cls,
3872 struct ResolverHandle *rh,
3874 const struct GNUNET_NAMESTORE_RecordData *rd)
3876 struct NameShortenHandle *nsh;
3877 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3879 nsh = (struct NameShortenHandle *)cls;
3880 rh->namestore_task = NULL;
3882 * At this point rh->name contains the part of the name
3883 * that we do not have a PKEY in our namestore to resolve.
3884 * The authority chain in the resolver handle is now
3885 * useful to backtrack if needed
3888 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3889 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3890 memset(result, 0, sizeof (result));
3892 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3896 * This is our zone append .gads unless name is empty
3897 * (it shouldn't be, usually FIXME what happens if we
3898 * shorten to our zone to a "" record??)
3901 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3902 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3903 "Our zone: Found %s as shorten result\n", result);
3905 if (strlen (nsh->result) > strlen (result))
3906 strcpy (nsh->result, result);
3909 else if (NULL != nsh->private_zone)
3912 * This is our zone append .gads unless name is empty
3913 * (it shouldn't be, usually FIXME what happens if we
3914 * shorten to our zone to a "" record??)
3916 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3917 nsh->private_zone) == 0)
3920 sprintf (result, "%s.%s.%s",
3921 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3922 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3923 "Our zone: Found %s as shorten result in private zone %s\n",
3926 if (strlen (nsh->result) > strlen (result))
3927 strcpy (nsh->result, result);
3930 else if (NULL != nsh->shorten_zone)
3933 * This is our zone append .gads unless name is empty
3934 * (it shouldn't be, usually FIXME what happens if we
3935 * shorten to our zone to a "" record??)
3937 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3938 nsh->shorten_zone) == 0)
3940 sprintf (result, "%s.%s.%s",
3941 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3942 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3943 "Our zone: Found %s as shorten result in shorten zone\n",
3946 if (strlen (nsh->result) > strlen (result))
3947 strcpy (nsh->result, result);
3952 /* backtrack authorities for names */
3953 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3955 &rh->authority_chain_head->zone,
3956 &process_zone_to_name_shorten_root,
3963 * Callback calles by namestore for a zone to name
3966 * @param cls the closure
3967 * @param zone_key the zone we queried
3968 * @param expire the expiration time of the name
3969 * @param name the name found or NULL
3970 * @param rd_len number of records for the name
3971 * @param rd the record data (PKEY) for the name
3972 * @param signature the signature for the record data
3975 process_zone_to_name_zkey(void *cls,
3976 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3977 struct GNUNET_TIME_Absolute expire,
3979 unsigned int rd_len,
3980 const struct GNUNET_NAMESTORE_RecordData *rd,
3981 const struct GNUNET_CRYPTO_RsaSignature *signature)
3983 struct ResolverHandle *rh = cls;
3984 struct NameShortenHandle *nsh = rh->proc_cls;
3985 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3987 rh->namestore_task = NULL;
3989 /* zkey not in our zone */
3993 * In this case we have not given this PKEY a name (yet)
3994 * It is either just not in our zone or not even cached
3995 * Since we do not know at this point we will not try to shorten
3996 * because PKEY import will happen if the user follows the zkey
3999 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4000 "No name found for zkey %s returning verbatim!\n", nsh->result);
4001 /*if (strcmp(rh->name, "") != 0)
4002 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s.%s",
4003 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4005 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
4006 enc, GNUNET_GNS_TLD_ZKEY);
4008 strcpy (nsh->result, new_name);*/
4010 finish_shorten (rh, nsh);
4014 if (strcmp(rh->name, "") != 0)
4015 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
4018 strcpy(new_name, name);
4020 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4021 "Continue shorten for %s!\n", new_name);
4023 strcpy(rh->name, new_name);
4025 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4026 rh->authority_chain_tail = rh->authority_chain_head;
4027 rh->authority_chain_head->zone = rh->authority;
4030 /* Start delegation resolution in our namestore */
4031 resolve_delegation_ns (rh);
4036 * Shorten api from resolver
4038 * @param zone the root zone to use
4039 * @param pzone the private zone to use
4040 * @param szone the shorten zone to use
4041 * @param name the name to shorten
4042 * @param private_zone_name name of the private zone
4043 * @param shorten_zone_name name of the shorten zone
4044 * @param proc the processor to call with result
4045 * @param proc_cls closure to pass to proc
4048 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4049 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4050 struct GNUNET_CRYPTO_ShortHashCode *szone,
4052 const char* private_zone_name,
4053 const char* shorten_zone_name,
4054 ShortenResultProcessor proc,
4057 struct ResolverHandle *rh;
4058 struct NameShortenHandle *nsh;
4059 char string_hash[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
4060 struct GNUNET_CRYPTO_ShortHashCode zkey;
4061 char nzkey[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
4062 char* nzkey_ptr = nzkey;
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4065 "Starting shorten for %s!\n", name);
4067 if (is_canonical ((char*)name) == GNUNET_YES)
4069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4070 "%s is canonical. Returning verbatim\n", name);
4071 proc (proc_cls, name);
4075 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4077 nsh->proc_cls = proc_cls;
4078 nsh->root_zone = zone;
4079 nsh->private_zone = pzone;
4080 nsh->shorten_zone = szone;
4081 strcpy (nsh->private_zone_name, private_zone_name);
4082 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4083 strcpy (nsh->result, name);
4085 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4086 rh->authority = *zone;
4088 rh->proc = &handle_delegation_ns_shorten;
4090 rh->private_local_zone = *zone;
4092 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4094 "Checking for TLD...\n");
4095 if (is_zkey_tld (name) == GNUNET_YES)
4097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4100 * This is a zkey tld
4101 * build hash and use as initial authority
4104 memset (rh->name, 0,
4105 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4106 memcpy (rh->name, name,
4107 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4108 pop_tld (rh->name, string_hash);
4110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4111 "ZKEY is %s!\n", string_hash);
4113 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4115 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4119 "Cannot convert ZKEY %s to hash!\n", nzkey);
4120 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4123 proc (proc_cls, name);
4126 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4129 &process_zone_to_name_zkey,
4134 else if (is_gads_tld (name) == GNUNET_YES)
4136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4139 * Presumably GNUNET tld
4141 memset (rh->name, 0,
4142 strlen (name)-strlen (GNUNET_GNS_TLD));
4143 memcpy (rh->name, name,
4144 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4149 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4152 proc (proc_cls, name);
4156 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4157 rh->authority_chain_tail = rh->authority_chain_head;
4158 rh->authority_chain_head->zone = *zone;
4160 /* Start delegation resolution in our namestore */
4161 resolve_delegation_ns (rh);
4164 /*********** END NAME SHORTEN ********************/
4167 * Conclude get authority lookup
4169 * @param rh resolver handle
4170 * @param nah get authority lookup handle
4173 finish_get_auth (struct ResolverHandle *rh,
4174 struct GetNameAuthorityHandle *nah)
4176 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4177 "Got authority result %s\n", nah->result);
4179 nah->proc (nah->proc_cls, nah->result);
4180 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4182 free_resolver_handle (rh);
4187 * Process result from namestore delegation lookup
4188 * for get authority operation
4190 * @param cls the client get auth handle
4191 * @param rh the resolver handle
4192 * @param rd_count number of results (0)
4193 * @param rd data (NULL)
4196 handle_delegation_result_ns_get_auth(void* cls,
4197 struct ResolverHandle *rh,
4199 const struct GNUNET_NAMESTORE_RecordData *rd)
4201 struct GetNameAuthorityHandle* nah = rh->proc_cls;
4205 * At this point rh->name contains the part of the name
4206 * that we do not have a PKEY in our namestore to resolve.
4207 * The authority chain in the resolver handle is now
4208 * useful to backtrack if needed
4211 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4212 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4214 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4215 "Building response!\n");
4216 if (is_canonical (rh->name) == GNUNET_YES)
4219 * We successfully resolved the authority in the ns
4220 * FIXME for our purposes this is fine
4221 * but maybe we want to have an api that also looks
4222 * into the dht (i.e. option in message)
4224 if (strlen(rh->name) > strlen(nah->name))
4226 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4227 "Record name longer than original lookup name... odd!\n");
4231 answer_len = strlen(nah->name) - strlen(rh->name)
4232 + strlen(GNUNET_GNS_TLD) + 1;
4233 memset(nah->result, 0, answer_len);
4234 if (0 != strcmp (rh->name, ""))
4235 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4237 strcpy(nah->result, nah->name);
4239 finish_get_auth (rh, nah);
4243 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4244 "Unable to resolve authority for remaining %s!\n", rh->name);
4245 strcpy(nah->result, "");
4246 finish_get_auth (rh, nah);
4252 * Tries to resolve the authority for name
4255 * @param zone the root zone to look up for
4256 * @param pzone the private local zone
4257 * @param name the name to lookup up
4258 * @param proc the processor to call when finished
4259 * @param proc_cls the closure to pass to the processor
4262 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4263 struct GNUNET_CRYPTO_ShortHashCode pzone,
4265 GetAuthorityResultProcessor proc,
4268 struct ResolverHandle *rh;
4269 struct GetNameAuthorityHandle *nah;
4271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4272 "Starting authority resolution for %s!\n", name);
4274 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4275 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4276 rh->authority = zone;
4278 rh->private_local_zone = pzone;
4280 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4282 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4284 strcpy(rh->name, "\0");
4289 strlen(name)-strlen(GNUNET_GNS_TLD));
4290 memcpy(rh->name, name,
4291 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4294 memset(nah->name, 0,
4296 strcpy(nah->name, name);
4298 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4299 rh->authority_chain_tail = rh->authority_chain_head;
4300 rh->authority_chain_head->zone = zone;
4301 rh->proc = &handle_delegation_result_ns_get_auth;
4302 rh->proc_cls = (void*)nah;
4305 nah->proc_cls = proc_cls;
4306 strcpy (nah->result, "");
4308 /* Start delegation resolution in our namestore */
4309 resolve_delegation_ns(rh);
4313 /******** END GET AUTHORITY *************/
4315 /* end of gnunet-service-gns_resolver.c */