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!\n",
1348 finish_lookup(rh, rlh, rd_count, rd);
1353 * Sends a UDP dns query to a nameserver specified in the rh
1355 * @param rh the resolver handle
1358 send_dns_packet (struct ResolverHandle *rh);
1364 * @param cls the ResolverHandle for this lookup
1365 * @param addr the sockaddr
1366 * @param addrlen the socket address length
1369 handle_dns_resolver (void *cls,
1370 const struct sockaddr *addr,
1373 struct ResolverHandle *rh = cls;
1374 struct RecordLookupHandle *rlh = rh->proc_cls;
1375 struct GNUNET_NAMESTORE_RecordData rd;
1376 struct sockaddr_in *sai;
1377 struct sockaddr_in6 *sai6;
1381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1382 "No address found in DNS!\n");
1383 finish_lookup (rh, rlh, 0, NULL);
1387 if (sizeof (struct sockaddr_in) == addrlen)
1389 sai = (struct sockaddr_in*) addr;
1390 rd.record_type = GNUNET_GNS_RECORD_A;
1391 rd.data_size = sizeof (struct in_addr);
1392 rd.data = &sai->sin_addr;
1394 else if (sizeof (struct sockaddr_in6) == addrlen)
1396 sai6 = (struct sockaddr_in6*) addr;
1397 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1398 rd.data_size = sizeof (struct in6_addr);
1399 rd.data = &sai6->sin6_addr;
1403 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1404 "Address length is garbage!\n");
1405 finish_lookup (rh, rlh, 0, NULL);
1408 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1410 finish_lookup (rh, rlh, 1, &rd);
1415 * Resolve DNS name via local stub resolver
1417 * @param rh the resolver handle
1420 resolve_dns_name (struct ResolverHandle *rh)
1422 struct RecordLookupHandle *rlh = rh->proc_cls;
1425 if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1426 (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Can only resolve A/AAAA via stub... abort\n");
1430 finish_lookup (rh, rlh, 0, NULL);
1434 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1439 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1442 &handle_dns_resolver,
1448 * Read DNS udp packet from socket
1450 * @param cls the resolver handle
1451 * @param tc task context
1454 read_dns_response (void *cls,
1455 const struct GNUNET_SCHEDULER_TaskContext *tc)
1457 struct ResolverHandle *rh = cls;
1458 struct RecordLookupHandle *rlh = rh->proc_cls;
1459 char buf[UINT16_MAX];
1461 struct sockaddr_in addr;
1463 struct GNUNET_DNSPARSER_Packet *packet;
1464 struct GNUNET_NAMESTORE_RecordData rd;
1465 int found_delegation = GNUNET_NO;
1466 int found_cname = GNUNET_NO;
1467 char* delegation_name = NULL;
1468 int zone_offset = 0;
1471 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1472 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1474 /* timeout or shutdown */
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476 "Terminating DNS query %d\n", tc->reason);
1477 finish_lookup (rh, rlh, 0, NULL);
1481 addrlen = sizeof (addr);
1482 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1484 (struct sockaddr*) &addr,
1489 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1490 finish_lookup (rh, rlh, 0, NULL);
1493 if (NULL == (packet = GNUNET_DNSPARSER_parse (buf, r)))
1495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1496 "Failed to parse DNS reply!\n");
1497 finish_lookup (rh, rlh, 0, NULL);
1501 for (i = 0; i < packet->num_answers; i++)
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 "Got record type %d (want %d)\n",
1505 packet->answers[i].type,
1507 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1508 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 "CNAME... restarting query with %s\n",
1512 packet->answers[i].data.hostname
1514 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1515 found_cname = GNUNET_YES;
1519 if ((packet->answers[i].type == rlh->record_type) &&
1520 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1524 rd.data = packet->answers[i].data.raw.data;
1525 rd.data_size = packet->answers[i].data.raw.data_len;
1526 rd.record_type = packet->answers[i].type;
1528 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1529 finish_lookup (rh, rlh, 1, &rd);
1530 GNUNET_DNSPARSER_free_packet (packet);
1535 if (GNUNET_YES == found_cname)
1537 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1539 if (0 > zone_offset)
1542 /* restart query with CNAME */
1543 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546 "Asking same server for %s\n", rh->dns_name);
1547 send_dns_packet (rh);
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "Trying system resolver for %s\n", rh->dns_name);
1553 resolve_dns_name (rh);
1556 GNUNET_DNSPARSER_free_packet (packet);
1560 for (i = 0; i < packet->num_authority_records; i++)
1562 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1565 "Found NS delegation!\n");
1566 found_delegation = GNUNET_YES;
1567 delegation_name = packet->authority_records[i].data.hostname;
1572 for (i = 0; i < packet->num_additional_records; i++)
1574 if (GNUNET_NO == found_delegation)
1577 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1578 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1580 GNUNET_assert (sizeof (struct in_addr) ==
1581 packet->authority_records[i].data.raw.data_len);
1583 rh->dns_addr.sin_addr =
1584 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1585 send_dns_packet (rh);
1586 GNUNET_DNSPARSER_free_packet (packet);
1591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1592 "Nothing useful in DNS reply!\n");
1593 finish_lookup (rh, rlh, 0, NULL);
1594 GNUNET_DNSPARSER_free_packet (packet);
1599 * Sends a UDP dns query to a nameserver specified in the rh
1601 * @param rh the request handle
1604 send_dns_packet (struct ResolverHandle *rh)
1606 struct GNUNET_NETWORK_FDSet *rset;
1608 rset = GNUNET_NETWORK_fdset_create ();
1609 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1611 "Sending %d byte DNS query\n",
1612 (int) rh->dns_raw_packet_size);
1614 if (GNUNET_SYSERR ==
1615 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1617 rh->dns_raw_packet_size,
1618 (struct sockaddr*)&rh->dns_addr,
1619 sizeof (struct sockaddr_in)))
1620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1621 _("Failed to send DNS request to %s\n"),
1622 GNUNET_a2s ((const struct sockaddr *)&rh->dns_addr,
1623 sizeof (struct sockaddr_in)));
1625 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1626 rh->timeout, //FIXME less?
1631 GNUNET_NETWORK_fdset_destroy (rset);
1636 * The final phase of resoution.
1637 * We found a NS RR and want to resolve via DNS
1639 * @param rh the pending lookup handle
1640 * @param rd_count length of record data
1641 * @param rd record data containing VPN RR
1644 resolve_record_dns (struct ResolverHandle *rh,
1645 unsigned int rd_count,
1646 const struct GNUNET_NAMESTORE_RecordData *rd)
1648 struct RecordLookupHandle *rlh = rh->proc_cls;
1649 struct GNUNET_DNSPARSER_Query query;
1650 struct GNUNET_DNSPARSER_Packet packet;
1651 struct GNUNET_DNSPARSER_Flags flags;
1652 struct in_addr dnsip;
1653 struct sockaddr_in addr;
1654 struct sockaddr *sa;
1657 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1658 memset (rh->dns_name, 0, sizeof (rh->dns_name));
1660 /* We cancel here as to not include the ns lookup in the timeout */
1661 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1663 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1664 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1666 /* Start shortening */
1667 if ((NULL != rh->priv_key) &&
1668 (GNUNET_YES == is_canonical (rh->name)))
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1671 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1673 start_shorten (rh->authority_chain_head,
1677 for (i = 0; i < rd_count; i++)
1679 /* Synthesize dns name */
1680 if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1682 strcpy (rh->dns_zone, (char*)rd[i].data);
1683 if (0 == strcmp (rh->name, ""))
1684 strcpy (rh->dns_name, (char*)rd[i].data);
1686 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1689 if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1690 /* need to use memcpy as .data may be unaligned */
1691 memcpy (&dnsip, rd[i].data, sizeof (dnsip));
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1699 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1700 if (NULL == rh->dns_sock)
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1703 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1705 finish_lookup (rh, rlh, 0, NULL);
1709 memset (&addr, 0, sizeof (struct sockaddr_in));
1710 sa = (struct sockaddr *) &addr;
1711 sa->sa_family = AF_INET;
1712 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1714 sizeof (struct sockaddr_in)))
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1717 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1719 finish_lookup (rh, rlh, 0, NULL);
1722 query.name = rh->dns_name;
1723 query.type = rlh->record_type;
1724 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1725 memset (&flags, 0, sizeof (flags));
1726 flags.recursion_desired = 1;
1727 flags.checking_disabled = 1;
1728 packet.queries = &query;
1729 packet.answers = NULL;
1730 packet.authority_records = NULL;
1731 packet.num_queries = 1;
1732 packet.num_answers = 0;
1733 packet.num_authority_records = 0;
1734 packet.num_additional_records = 0;
1735 packet.flags = flags;
1737 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1739 &rh->dns_raw_packet,
1740 &rh->dns_raw_packet_size))
1742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1743 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1745 GNUNET_NETWORK_socket_close (rh->dns_sock);
1746 finish_lookup (rh, rlh, 0, NULL);
1750 rh->dns_addr.sin_family = AF_INET;
1751 rh->dns_addr.sin_port = htons (53); //domain
1752 rh->dns_addr.sin_addr = dnsip;
1753 #if HAVE_SOCKADDR_IN_SIN_LEN
1754 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1756 send_dns_packet (rh);
1761 * The final phase of resoution.
1762 * We found a VPN RR and want to request an IPv4/6 address
1764 * @param rh the pending lookup handle
1765 * @param rd_count length of record data
1766 * @param rd record data containing VPN RR
1769 resolve_record_vpn (struct ResolverHandle *rh,
1770 unsigned int rd_count,
1771 const struct GNUNET_NAMESTORE_RecordData *rd)
1773 struct RecordLookupHandle *rlh = rh->proc_cls;
1774 struct GNUNET_HashCode serv_desc;
1775 struct vpn_data* vpn;
1778 /* We cancel here as to not include the ns lookup in the timeout */
1779 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1781 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1782 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1784 /* Start shortening */
1785 if ((NULL != rh->priv_key) &&
1786 (GNUNET_YES == is_canonical (rh->name)))
1788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1789 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1791 start_shorten (rh->authority_chain_head,
1795 vpn = (struct vpn_data*)rd->data;
1796 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1797 strlen ((char*)&vpn[1]) + 1,
1799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1800 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1803 GNUNET_h2s (&vpn->peer));
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1806 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1809 GNUNET_h2s (&serv_desc));
1810 rh->proc = &handle_record_vpn;
1811 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1816 if (NULL == vpn_handle)
1818 vpn_handle = GNUNET_VPN_connect (cfg);
1819 if (NULL == vpn_handle)
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1822 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1823 finish_lookup (rh, rh->proc_cls, 0, NULL);
1828 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1829 af, ntohs (vpn->proto),
1830 (struct GNUNET_PeerIdentity *)&vpn->peer,
1833 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1834 &process_record_result_vpn,
1837 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1838 "Error connecting to VPN (not available on W32 yet)\n");
1839 finish_lookup (rh, rh->proc_cls, 0, NULL);
1845 * The final phase of resolution.
1846 * rh->name is a name that is canonical and we do not have a delegation.
1847 * Query namestore for this record
1849 * @param rh the pending lookup handle
1852 resolve_record_ns(struct ResolverHandle *rh)
1854 struct RecordLookupHandle *rlh = rh->proc_cls;
1856 /* We cancel here as to not include the ns lookup in the timeout */
1857 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1859 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1860 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1862 /* Start shortening */
1863 if ((NULL != rh->priv_key) &&
1864 (GNUNET_YES == is_canonical (rh->name)))
1866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1867 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1869 start_shorten (rh->authority_chain_head,
1874 * Try to resolve this record in our namestore.
1875 * The name to resolve is now in rh->authority_name
1876 * since we tried to resolve it to an authority
1879 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1883 &process_record_result_ns,
1889 * Handle timeout for DHT requests
1891 * @param cls the request handle as closure
1892 * @param tc the task context
1895 dht_authority_lookup_timeout (void *cls,
1896 const struct GNUNET_SCHEDULER_TaskContext *tc)
1898 struct ResolverHandle *rh = cls;
1899 struct RecordLookupHandle *rlh = rh->proc_cls;
1900 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
1902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1903 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus) timed out.\n",
1904 rh->id, rh->authority_name,
1905 rh->timeout.rel_value);
1907 rh->status |= RSL_TIMED_OUT;
1908 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1909 if (NULL != rh->get_handle)
1911 GNUNET_DHT_get_stop (rh->get_handle);
1912 rh->get_handle = NULL;
1914 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
1919 rh->proc (rh->proc_cls, rh, 0, NULL);
1922 if (0 == strcmp (rh->name, ""))
1925 * promote authority back to name and try to resolve record
1927 strcpy (rh->name, rh->authority_name);
1928 rh->proc (rh->proc_cls, rh, 0, NULL);
1933 * Start resolution in bg
1935 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
1937 rh->name, rh->authority_name, GNUNET_GNS_TLD);
1938 strcpy (rh->name, new_name);
1939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1940 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1943 gns_resolver_lookup_record (rh->authority,
1944 rh->private_local_zone,
1948 GNUNET_TIME_UNIT_FOREVER_REL,
1950 &background_lookup_result_processor,
1952 rh->proc (rh->proc_cls, rh, 0, NULL);
1957 * Start DHT lookup for a name -> PKEY (compare NS) record in
1958 * rh->authority's zone
1960 * @param rh the pending gns query
1963 resolve_delegation_dht (struct ResolverHandle *rh);
1967 * Resolve the delegation chain for the request in our namestore
1969 * @param rh the resolver handle
1972 resolve_delegation_ns (struct ResolverHandle *rh);
1976 * Namestore resolution for delegation finished. Processing result.
1978 * @param cls the closure
1979 * @param rh resolver handle
1980 * @param rd_count number of results (always 0)
1981 * @param rd record data (always NULL)
1984 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
1985 unsigned int rd_count,
1986 const struct GNUNET_NAMESTORE_RecordData *rd);
1990 * This is a callback function that checks for key revocation
1992 * @param cls the pending query
1993 * @param key the key of the zone we did the lookup
1994 * @param expiration expiration date of the record data set in the namestore
1995 * @param name the name for which we need an authority
1996 * @param rd_count the number of records with 'name'
1997 * @param rd the record data
1998 * @param signature the signature of the authority for the record data
2001 process_pkey_revocation_result_ns (void *cls,
2002 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2003 struct GNUNET_TIME_Absolute expiration,
2005 unsigned int rd_count,
2006 const struct GNUNET_NAMESTORE_RecordData *rd,
2007 const struct GNUNET_CRYPTO_RsaSignature *signature)
2009 struct ResolverHandle *rh = cls;
2010 struct GNUNET_TIME_Relative remaining_time;
2013 rh->namestore_task = NULL;
2014 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2016 for (i = 0; i < rd_count; i++)
2018 if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2020 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2021 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2023 rh->status |= RSL_PKEY_REVOKED;
2024 rh->proc (rh->proc_cls, rh, 0, NULL);
2029 if ((NULL == name) ||
2030 (0 == remaining_time.rel_value))
2032 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2033 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2036 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2038 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2039 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2040 rh->id, "+.gads", GNUNET_GNS_RECORD_REV);
2042 gns_resolver_lookup_record(rh->authority,
2043 rh->private_local_zone,
2044 GNUNET_GNS_RECORD_REV,
2047 GNUNET_TIME_UNIT_FOREVER_REL,
2049 &background_lookup_result_processor,
2053 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2054 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2057 * We are done with PKEY resolution if name is empty
2058 * else resolve again with new authority
2060 if (strcmp (rh->name, "") == 0)
2061 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2063 resolve_delegation_ns (rh);
2068 * Callback when record data is put into namestore
2070 * @param cls the closure
2071 * @param success GNUNET_OK on success
2072 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2075 on_namestore_delegation_put_result(void *cls,
2079 struct NamestoreBGTask *nbg = cls;
2081 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2084 if (GNUNET_NO == success)
2086 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2087 "GNS_NS: records already in namestore\n");
2090 else if (GNUNET_YES == success)
2092 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2093 "GNS_NS: records successfully put in namestore\n");
2097 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2098 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2103 * Function called when we get a result from the dht
2104 * for our query. Recursively tries to resolve authorities
2107 * @param cls the request handle
2108 * @param exp lifetime
2109 * @param key the key the record was stored under
2110 * @param get_path get path
2111 * @param get_path_length get path length
2112 * @param put_path put path
2113 * @param put_path_length put path length
2114 * @param type the block type
2115 * @param size the size of the record
2116 * @param data the record data
2119 process_delegation_result_dht (void* cls,
2120 struct GNUNET_TIME_Absolute exp,
2121 const struct GNUNET_HashCode * key,
2122 const struct GNUNET_PeerIdentity *get_path,
2123 unsigned int get_path_length,
2124 const struct GNUNET_PeerIdentity *put_path,
2125 unsigned int put_path_length,
2126 enum GNUNET_BLOCK_Type type,
2127 size_t size, const void *data)
2129 struct ResolverHandle *rh = cls;
2130 const struct GNSNameRecordBlock *nrb = data;
2131 const char* rd_data;
2132 uint32_t num_records;
2136 struct GNUNET_CRYPTO_ShortHashCode zone;
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2139 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n",
2143 /* stop dht lookup and timeout task */
2144 GNUNET_DHT_get_stop (rh->get_handle);
2145 rh->get_handle = NULL;
2146 if (rh->dht_heap_node != NULL)
2148 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2149 rh->dht_heap_node = NULL;
2152 num_records = ntohl(nrb->rd_count);
2153 name = (const char*) &nrb[1];
2155 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2156 struct NamestoreBGTask *ns_heap_root;
2157 struct NamestoreBGTask *namestore_bg_task;
2159 rd_data = name + strlen(name) + 1;
2160 rd_size = size - strlen(name) - 1 - sizeof (struct GNSNameRecordBlock);
2161 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2166 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2167 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2173 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2174 rh->id, name, rh->authority_name);
2175 for (i=0; i<num_records; i++)
2177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2178 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2179 rh->id, name, rh->authority_name);
2180 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2181 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2182 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2183 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2184 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2185 rh->id, rd[i].data_size);
2186 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2187 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2188 rh->id, rd[i].flags);
2190 if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2191 (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2192 (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2195 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2197 if (0 == strcmp(rh->name, ""))
2198 strcpy(rh->name, rh->authority_name);
2200 GNUNET_snprintf(rh->name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
2201 rh->name, rh->authority_name); //FIXME ret
2206 if ((0 == strcmp(name, rh->authority_name)) &&
2207 (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2209 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2210 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2213 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2214 struct AuthorityChain *auth =
2215 GNUNET_malloc(sizeof(struct AuthorityChain));
2216 auth->zone = rh->authority;
2217 memset(auth->name, 0, strlen(rh->authority_name)+1);
2218 strcpy(auth->name, rh->authority_name);
2219 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2220 rh->authority_chain_tail,
2223 if (NULL != rh->rd.data)
2224 GNUNET_free ((void*)rh->rd.data);
2226 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2227 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2228 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2231 /** try to import pkey if private key available */
2232 //if (rh->priv_key && is_canonical (rh->name))
2233 // process_discovered_authority(name, auth->zone,
2234 // rh->authority_chain_tail->zone,
2239 GNUNET_GNS_get_zone_from_key (name, key, &zone);
2242 /* Save to namestore
2243 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2246 if (max_allowed_ns_tasks <=
2247 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2249 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2250 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2252 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2253 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background ns task\n",
2256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2257 "GNS_PHASE_DELEGATE_DHT-%llu: Caching record for %s\n",
2259 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2261 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2263 GNUNET_TIME_absolute_get().abs_value);
2264 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2271 &on_namestore_delegation_put_result, //cont
2272 namestore_bg_task); //cls
2276 if (0 != rh->answered)
2281 * FIXME in this case. should we ask namestore again?
2283 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2284 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2285 rh->id, rh->authority_name, rh->name);
2287 if (0 == strcmp(rh->name, ""))
2289 /* Start shortening */
2290 if ((NULL != rh->priv_key) &&
2291 (GNUNET_YES == is_canonical (rh->name)))
2293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2294 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2296 start_shorten (rh->authority_chain_head,
2301 rh->proc = &handle_delegation_ns;
2304 /* Check for key revocation and delegate */
2305 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2307 GNUNET_GNS_MASTERZONE_STR,
2308 GNUNET_GNS_RECORD_REV,
2309 &process_pkey_revocation_result_ns,
2316 * No pkey but name exists
2319 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2320 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2321 rh->id, rh->authority_name, rh->name);
2322 if (0 == strcmp(rh->name, ""))
2323 strcpy(rh->name, rh->authority_name);
2325 GNUNET_snprintf(rh->name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
2326 rh->name, rh->authority_name); //FIXME ret
2328 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2329 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2330 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2331 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2333 rh->proc(rh->proc_cls, rh, 0, NULL);
2336 //FIXME maybe define somewhere else?
2337 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2338 +(GNUNET_DNSPARSER_MAX_NAME_LENGTH*2)
2339 #define MAX_MX_LENGTH sizeof(uint16_t)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
2340 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
2344 * Exands a name ending in .+ with the zone of origin.
2345 * FIXME: funky api: 'dest' must be large enough to hold
2346 * the result; this is a bit yucky...
2348 * @param dest destination buffer
2349 * @param src the .+ name
2350 * @param repl the string to replace the + with
2353 expand_plus (char* dest,
2358 size_t s_len = strlen (src) + 1;
2360 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2363 /* no postprocessing */
2364 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2365 "GNS_POSTPROCESS: %s too short\n", src);
2366 memcpy (dest, src, s_len);
2369 if (0 == strcmp (src + s_len - 3, ".+"))
2371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2372 "GNS_POSTPROCESS: Expanding .+ in %s\n",
2374 memset (dest, 0, s_len + strlen (repl) + strlen(GNUNET_GNS_TLD));
2376 pos = dest + s_len - 2;
2378 pos += strlen (repl);
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2380 "GNS_POSTPROCESS: Expanded to %s\n",
2385 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2386 "GNS_POSTPROCESS: No postprocessing for %s\n", src);
2387 memcpy (dest, src, s_len);
2396 finish_lookup (struct ResolverHandle *rh,
2397 struct RecordLookupHandle* rlh,
2398 unsigned int rd_count,
2399 const struct GNUNET_NAMESTORE_RecordData *rd)
2402 char new_rr_data[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
2403 char new_mx_data[MAX_MX_LENGTH];
2404 char new_soa_data[MAX_SOA_LENGTH];
2405 char new_srv_data[MAX_SRV_LENGTH];
2406 struct srv_data *old_srv;
2407 struct srv_data *new_srv;
2408 struct soa_data *old_soa;
2409 struct soa_data *new_soa;
2410 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2413 unsigned int offset;
2415 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
2417 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2418 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2421 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2424 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2426 for (i = 0; i < rd_count; i++)
2429 if ((GNUNET_GNS_RECORD_NS != rd[i].record_type) &&
2430 (GNUNET_GNS_RECORD_PTR != rd[i].record_type) &&
2431 (GNUNET_GNS_RECORD_CNAME != rd[i].record_type) &&
2432 (GNUNET_GNS_RECORD_MX != rd[i].record_type) &&
2433 (GNUNET_GNS_RECORD_SOA != rd[i].record_type) &&
2434 (GNUNET_GNS_RECORD_SRV != rd[i].record_type))
2436 p_rd[i].data = rd[i].data;
2441 * for all those records we 'should'
2442 * also try to resolve the A/AAAA records (RFC1035)
2443 * This is a feature and not important
2446 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2447 "GNS_POSTPROCESS: Postprocessing\n");
2448 if (0 == strcmp(rh->name, GNUNET_GNS_MASTERZONE_STR))
2449 repl_string = rlh->name;
2451 repl_string = rlh->name+strlen(rh->name)+1;
2454 if (GNUNET_GNS_RECORD_MX == rd[i].record_type)
2456 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2457 offset = sizeof (uint16_t);
2458 pos = new_mx_data + offset;
2459 // FIXME: how do we know that 'pos' has enough space for the new name?
2460 expand_plus (pos, (char*)rd[i].data+sizeof(uint16_t),
2462 offset += strlen(new_mx_data+sizeof(uint16_t)) + 1;
2463 p_rd[i].data = new_mx_data;
2464 p_rd[i].data_size = offset;
2466 else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2469 * Prio, weight and port
2471 new_srv = (struct srv_data*)new_srv_data;
2472 old_srv = (struct srv_data*)rd[i].data;
2473 new_srv->prio = old_srv->prio;
2474 new_srv->weight = old_srv->weight;
2475 new_srv->port = old_srv->port;
2476 // FIXME: how do we know that '&new_srv[1]' has enough space for the new name?
2477 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2479 p_rd[i].data = new_srv_data;
2480 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2482 else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2484 /* expand mname and rname */
2485 old_soa = (struct soa_data*)rd[i].data;
2486 new_soa = (struct soa_data*)new_soa_data;
2487 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2488 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2489 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2490 offset = strlen ((char*)&new_soa[1]) + 1;
2491 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2492 expand_plus((char*)&new_soa[1] + offset,
2493 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2495 p_rd[i].data_size = sizeof (struct soa_data)
2497 + strlen ((char*)&new_soa[1] + offset);
2498 p_rd[i].data = new_soa_data;
2503 // FIXME: how do we know that 'rd[i].data' has enough space for the new name?
2504 expand_plus(pos, (char*)rd[i].data, repl_string);
2505 p_rd[i].data_size = strlen(new_rr_data)+1;
2506 p_rd[i].data = new_rr_data;
2511 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2513 free_resolver_handle (rh);
2518 * Process DHT lookup result for record.
2520 * @param cls the closure
2521 * @param rh resolver handle
2522 * @param rd_count number of results
2523 * @param rd record data
2526 handle_record_dht (void* cls, struct ResolverHandle *rh,
2527 unsigned int rd_count,
2528 const struct GNUNET_NAMESTORE_RecordData *rd)
2530 struct RecordLookupHandle* rlh = cls;
2534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2535 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2537 /* give up, cannot resolve */
2538 finish_lookup (rh, rlh, 0, NULL);
2541 /* results found yay */
2542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2543 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2544 finish_lookup (rh, rlh, rd_count, rd);
2549 * Process namestore lookup result for record.
2551 * @param cls the closure
2552 * @param rh resolver handle
2553 * @param rd_count number of results
2554 * @param rd record data
2557 handle_record_ns (void* cls, struct ResolverHandle *rh,
2558 unsigned int rd_count,
2559 const struct GNUNET_NAMESTORE_RecordData *rd)
2561 struct RecordLookupHandle* rlh = cls;
2562 int check_dht = GNUNET_YES;
2566 /* results found yay */
2567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2568 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2569 finish_lookup (rh, rlh, rd_count, rd);
2573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2574 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2578 * There are 5 conditions that have to met for us to consult the DHT:
2579 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2580 * 2. No entry in the NS existed AND
2581 * 3. The zone queried is not the local resolver's zone AND
2582 * 4. The name that was looked up is '+'
2583 * because if it was any other canonical name we either already queried
2584 * the DHT for the authority in the authority lookup phase (and thus
2585 * would already have an entry in the NS for the record)
2586 * 5. We are not in cache only mode
2588 if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2589 (0 == (rh->status & RSL_RECORD_EXISTS)) )
2592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2593 "GNS_PHASE_REC-%llu: Not expired and exists!\n",
2595 check_dht = GNUNET_NO;
2598 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2599 &rh->private_local_zone))
2602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2603 "GNS_PHASE_REC-%llu: Our zone!\n",
2605 check_dht = GNUNET_NO;
2608 if ((0 != strcmp (rh->name, GNUNET_GNS_MASTERZONE_STR)) && (GNUNET_YES == is_srv (rh->name)))
2609 check_dht = GNUNET_NO;
2611 if (GNUNET_YES == rh->only_cached)
2612 check_dht = GNUNET_NO;
2614 if (GNUNET_YES == check_dht)
2616 rh->proc = &handle_record_dht;
2617 resolve_record_dht(rh);
2620 /* give up, cannot resolve */
2621 finish_lookup (rh, rlh, 0, NULL);
2626 * Move one level up in the domain hierarchy and return the
2627 * passed top level domain.
2629 * FIXME: funky API: not only 'dest' is updated, so is 'name'!
2631 * @param name the domain
2632 * @param dest the destination where the tld will be put
2635 pop_tld (char* name, char* dest)
2639 if (GNUNET_YES == is_canonical (name))
2641 strcpy (dest, name);
2646 for (len = strlen(name); 0 < len; len--)
2648 if (*(name+len) == '.')
2658 strcpy(dest, (name+len+1));
2663 * DHT resolution for delegation finished. Processing result.
2665 * @param cls the closure
2666 * @param rh resolver handle
2667 * @param rd_count number of results (always 0)
2668 * @param rd record data (always NULL)
2671 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2672 unsigned int rd_count,
2673 const struct GNUNET_NAMESTORE_RecordData *rd)
2675 struct RecordLookupHandle* rlh = cls;
2677 if (0 == strcmp(rh->name, ""))
2679 if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2681 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2682 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2684 finish_lookup(rh, rlh, rd_count, rd);
2687 /* We resolved full name for delegation. resolving record */
2688 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2689 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2691 strcpy(rh->name, "+\0");
2692 rh->proc = &handle_record_ns;
2693 resolve_record_ns(rh);
2698 * we still have some left
2700 if (GNUNET_YES == is_canonical (rh->name))
2702 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2703 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2706 rh->proc = &handle_record_ns;
2707 resolve_record_ns(rh);
2710 /* give up, cannot resolve */
2711 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2712 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2714 finish_lookup(rh, rlh, 0, NULL);
2719 * Start DHT lookup for a name -> PKEY (compare NS) record in
2720 * rh->authority's zone
2722 * @param rh the pending gns query
2725 resolve_delegation_dht (struct ResolverHandle *rh)
2728 struct GNUNET_HashCode lookup_key;
2729 struct ResolverHandle *rh_heap_root;
2731 pop_tld (rh->name, rh->authority_name);
2732 GNUNET_GNS_get_key_for_record (rh->authority_name,
2735 rh->dht_heap_node = NULL;
2736 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2738 rh->timeout_cont = &dht_authority_lookup_timeout;
2739 rh->timeout_cont_cls = rh;
2743 if (max_allowed_background_queries <=
2744 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2746 /* terminate oldest lookup */
2747 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2748 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2749 rh_heap_root->get_handle = NULL;
2750 rh_heap_root->dht_heap_node = NULL;
2751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2752 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2754 rh_heap_root->authority_name);
2755 rh_heap_root->proc (rh_heap_root->proc_cls,
2760 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2762 GNUNET_TIME_absolute_get().abs_value);
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2765 "Beginning DHT lookup for %s in zone %s for request %llu\n",
2767 GNUNET_short_h2s (&rh->authority),
2769 xquery = htonl (GNUNET_GNS_RECORD_PKEY);
2770 GNUNET_assert (rh->get_handle == NULL);
2771 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
2772 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2774 DHT_GNS_REPLICATION_LEVEL,
2775 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2778 &process_delegation_result_dht,
2784 * Checks if name is in tld
2786 * @param name the name to check
2787 * @param tld the TLD to check for
2788 * @return GNUNET_YES or GNUNET_NO
2791 is_tld (const char* name, const char* tld)
2795 if (strlen (name) <= strlen (tld))
2798 offset = strlen (name) - strlen (tld);
2799 if (0 != strcmp (name + offset, tld))
2801 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2802 "%s is not in .%s TLD\n", name, tld);
2810 * Namestore resolution for delegation finished. Processing result.
2812 * @param cls the closure
2813 * @param rh resolver handle
2814 * @param rd_count number of results (always 0)
2815 * @param rd record data (always NULL)
2818 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2819 unsigned int rd_count,
2820 const struct GNUNET_NAMESTORE_RecordData *rd)
2822 struct RecordLookupHandle* rlh = cls;
2826 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2827 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2828 rh->id, rh->status);
2830 if (rh->status & RSL_PKEY_REVOKED)
2832 finish_lookup (rh, rlh, 0, NULL);
2836 if (0 == strcmp(rh->name, ""))
2839 /* We resolved full name for delegation. resolving record */
2840 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2841 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2843 if (rh->status & RSL_CNAME_FOUND)
2845 if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2847 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2848 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2850 strcpy (rh->name, rh->authority_name);
2851 finish_lookup (rh, rlh, rd_count, rd);
2856 if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2858 s_len = strlen (rd->data) - 2;
2859 memcpy (rh->name, rd->data, s_len);
2860 rh->name[s_len] = '\0';
2861 resolve_delegation_ns (rh);
2864 else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2866 gns_resolver_lookup_record (rh->authority,
2867 rh->private_local_zone,
2876 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2877 free_resolver_handle (rh);
2883 strcpy (rh->dns_name, (char*)rd->data);
2884 resolve_dns_name (rh);
2889 else if (rh->status & RSL_DELEGATE_VPN)
2891 if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2893 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2894 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2896 finish_lookup(rh, rlh, rd_count, rd);
2899 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2900 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2902 GNUNET_assert (NULL != rd);
2903 rh->proc = &handle_record_vpn;
2904 resolve_record_vpn (rh, rd_count, rd);
2907 else if (rh->status & RSL_DELEGATE_NS)
2909 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2911 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2912 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2914 finish_lookup(rh, rlh, rd_count, rd);
2918 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2919 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2921 GNUNET_assert (NULL != rd);
2922 rh->proc = &handle_record_ns;
2923 resolve_record_dns (rh, rd_count, rd);
2926 else if (rh->status & RSL_DELEGATE_PKEY)
2928 if (rh->status & RSL_PKEY_REVOKED)
2930 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2931 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
2933 finish_lookup (rh, rlh, 0, NULL);
2936 else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2938 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2939 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
2941 finish_lookup(rh, rlh, rd_count, rd);
2945 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2946 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
2948 strcpy(rh->name, "+\0");
2949 rh->proc = &handle_record_ns;
2950 resolve_record_ns(rh);
2954 if (rh->status & RSL_DELEGATE_NS)
2956 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2958 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2959 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2961 finish_lookup(rh, rlh, rd_count, rd);
2965 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2966 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2968 GNUNET_assert (NULL != rd);
2969 rh->proc = &handle_record_ns;
2970 resolve_record_dns (rh, rd_count, rd);
2975 * we still have some left
2976 * check if authority in ns is fresh
2978 * or we are authority
2981 check_dht = GNUNET_YES;
2982 if ((rh->status & RSL_RECORD_EXISTS) &&
2983 !(rh->status & RSL_RECORD_EXPIRED))
2984 check_dht = GNUNET_NO;
2986 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2987 &rh->private_local_zone))
2988 check_dht = GNUNET_NO;
2990 if (GNUNET_YES == rh->only_cached)
2991 check_dht = GNUNET_NO;
2993 if (GNUNET_YES == check_dht)
2996 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2997 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
2999 rh->proc = &handle_delegation_dht;
3000 resolve_delegation_dht(rh);
3004 if (GNUNET_NO == is_canonical (rh->name))
3006 /* give up, cannot resolve */
3007 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3008 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3011 finish_lookup(rh, rlh, rd_count, rd);
3014 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3015 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3018 rh->proc = &handle_record_ns;
3019 resolve_record_ns(rh);
3024 * This is a callback function that should give us only PKEY
3025 * records. Used to query the namestore for the authority (PKEY)
3026 * for 'name'. It will recursively try to resolve the
3027 * authority for a given name from the namestore.
3029 * @param cls the pending query
3030 * @param key the key of the zone we did the lookup
3031 * @param expiration expiration date of the record data set in the namestore
3032 * @param name the name for which we need an authority
3033 * @param rd_count the number of records with 'name'
3034 * @param rd the record data
3035 * @param signature the signature of the authority for the record data
3038 process_delegation_result_ns (void* cls,
3039 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3040 struct GNUNET_TIME_Absolute expiration,
3042 unsigned int rd_count,
3043 const struct GNUNET_NAMESTORE_RecordData *rd,
3044 const struct GNUNET_CRYPTO_RsaSignature *signature)
3046 struct ResolverHandle *rh = cls;
3047 struct GNUNET_TIME_Relative remaining_time;
3048 struct GNUNET_CRYPTO_ShortHashCode zone;
3049 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3051 struct GNUNET_TIME_Absolute et;
3053 rh->namestore_task = NULL;
3054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3055 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3058 GNUNET_CRYPTO_short_hash (key,
3059 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3061 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3068 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3070 rh->status |= RSL_RECORD_EXISTS;
3072 if (remaining_time.rel_value == 0)
3074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3075 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3077 rh->status |= RSL_RECORD_EXPIRED;
3082 * No authority found in namestore.
3087 * We did not find an authority in the namestore
3092 * Promote this authority back to a name maybe it is
3095 if (strcmp (rh->name, "") == 0)
3097 /* simply promote back */
3098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3099 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3100 rh->id, rh->authority_name);
3101 strcpy (rh->name, rh->authority_name);
3105 /* add back to existing name */
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3107 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3108 rh->id, rh->authority_name, rh->name);
3109 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
3110 rh->name, rh->authority_name);
3111 strcpy (rh->name, new_name);
3112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3113 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3117 rh->proc (rh->proc_cls, rh, 0, NULL);
3122 * We found an authority that may be able to help us
3123 * move on with query
3124 * Note only 1 pkey should have been returned.. anything else would be strange
3126 for (i=0; i < rd_count;i++)
3130 * A CNAME. Like regular DNS this means the is no other record for this
3133 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3135 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3136 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3139 rh->status |= RSL_CNAME_FOUND;
3140 rh->proc (rh->proc_cls, rh, rd_count, rd);
3147 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3149 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3150 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3152 rh->status |= RSL_DELEGATE_VPN;
3153 rh->proc (rh->proc_cls, rh, rd_count, rd);
3159 * FIXME make optional
3161 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3163 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3164 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3166 rh->status |= RSL_DELEGATE_NS;
3167 rh->proc (rh->proc_cls, rh, rd_count, rd);
3171 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3174 rh->status |= RSL_DELEGATE_PKEY;
3176 if ((ignore_pending_records != 0) &&
3177 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3180 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3186 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3187 et.abs_value = rd[i].expiration_time;
3188 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3192 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3194 if (remaining_time.rel_value == 0)
3196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3197 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3199 rh->authority_chain_head->fresh = 0;
3200 rh->proc (rh->proc_cls, rh, 0, NULL);
3208 * Resolve rest of query with new authority
3210 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3211 memcpy (&rh->authority, rd[i].data,
3212 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3213 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3214 auth->zone = rh->authority;
3215 memset (auth->name, 0, strlen (rh->authority_name)+1);
3216 strcpy (auth->name, rh->authority_name);
3217 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3218 rh->authority_chain_tail,
3220 if (NULL != rh->rd.data)
3221 GNUNET_free ((void*)(rh->rd.data));
3223 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3224 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3225 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3227 /* Check for key revocation and delegate */
3228 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3230 GNUNET_GNS_MASTERZONE_STR,
3231 GNUNET_GNS_RECORD_REV,
3232 &process_pkey_revocation_result_ns,
3240 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3241 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3243 * If we have found some records for the LAST label
3244 * we return the results. Else null.
3246 if (strcmp (rh->name, "") == 0)
3248 /* Start shortening */
3249 if ((rh->priv_key != NULL) &&
3250 (is_canonical (rh->name) == GNUNET_YES))
3252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3253 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3255 start_shorten (rh->authority_chain_head,
3258 /* simply promote back */
3259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3260 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3261 rh->id, rh->authority_name);
3262 strcpy (rh->name, rh->authority_name);
3263 rh->proc (rh->proc_cls, rh, rd_count, rd);
3267 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3268 "%s.%s", rh->name, rh->authority_name);
3269 strcpy (rh->name, new_name);
3270 rh->proc (rh->proc_cls, rh, 0, NULL);
3276 * Resolve the delegation chain for the request in our namestore
3278 * @param rh the resolver handle
3281 resolve_delegation_ns (struct ResolverHandle *rh)
3283 pop_tld (rh->name, rh->authority_name);
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3285 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s by looking up %s\n",
3287 rh->authority_name);
3288 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3291 GNUNET_GNS_RECORD_ANY,
3292 &process_delegation_result_ns,
3298 * Lookup of a record in a specific zone
3299 * calls lookup result processor on result
3301 * @param zone the root zone
3302 * @param pzone the private local zone
3303 * @param record_type the record type to look up
3304 * @param name the name to look up
3305 * @param key a private key for use with PSEU import (can be NULL)
3306 * @param timeout timeout for resolution
3307 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3308 * @param proc the processor to call on result
3309 * @param cls the closure to pass to proc
3312 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3313 struct GNUNET_CRYPTO_ShortHashCode pzone,
3314 uint32_t record_type,
3316 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3317 struct GNUNET_TIME_Relative timeout,
3319 RecordLookupProcessor proc,
3322 struct ResolverHandle *rh;
3323 struct RecordLookupHandle* rlh;
3324 char string_hash[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
3325 char nzkey[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
3326 char* nzkey_ptr = nzkey;
3328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3329 "Starting resolution for %s (type=%d) with timeout %s!\n",
3331 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
3333 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3334 (strcmp(GNUNET_GNS_TLD, name) != 0))
3336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3337 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3342 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3343 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3344 rh->authority = zone;
3348 rh->timeout = timeout;
3349 rh->private_local_zone = pzone;
3350 rh->only_cached = only_cached;
3352 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3356 "No shorten key for resolution\n");
3358 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3361 * Set timeout for authority lookup phase to 1/2
3363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3364 "Timeout for lookup set to %s/2\n",
3365 GNUNET_STRINGS_relative_time_to_string (rh->timeout, GNUNET_YES));
3366 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
3367 GNUNET_TIME_relative_divide(timeout, 2),
3368 &handle_lookup_timeout,
3370 rh->timeout_cont = &dht_authority_lookup_timeout;
3371 rh->timeout_cont_cls = rh;
3375 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3376 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3379 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3382 * Only 'gnunet' given
3384 strcpy(rh->name, "\0");
3388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3389 "Checking for TLD...\n");
3390 if (is_zkey_tld(name) == GNUNET_YES)
3392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3395 * This is a zkey tld
3396 * build hash and use as initial authority
3399 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3400 memcpy(rh->name, name,
3401 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3402 pop_tld(rh->name, string_hash);
3404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3405 "ZKEY is %s!\n", string_hash);
3407 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3409 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3412 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3413 "Cannot convert ZKEY `%s' to hash!\n", string_hash);
3415 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3416 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3417 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3420 proc (cls, 0, NULL);
3425 else if (is_gads_tld (name) == GNUNET_YES)
3427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3430 * Presumably GADS tld
3432 memcpy (rh->name, name,
3433 strlen (name) - strlen(GNUNET_GNS_TLD) - 1);
3434 rh->name[strlen (name) - strlen(GNUNET_GNS_TLD) - 1] = '\0';
3438 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3439 _("Not a GADS TLD: `%s'\n"),
3441 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3442 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3443 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3446 proc (cls, 0, NULL);
3452 * Initialize authority chain
3454 rh->authority_chain_head = GNUNET_malloc (sizeof(struct AuthorityChain));
3455 rh->authority_chain_tail = rh->authority_chain_head;
3456 rh->authority_chain_head->zone = rh->authority;
3457 strcpy (rh->authority_chain_head->name, "");
3460 * Copy original query into lookup handle
3462 rlh->record_type = record_type;
3463 memset(rlh->name, 0, strlen(name) + 1);
3464 strcpy(rlh->name, name);
3466 rlh->proc_cls = cls;
3468 rh->proc = &handle_delegation_ns;
3469 resolve_delegation_ns(rh);
3472 /******** END Record Resolver ***********/
3475 finish_shorten (struct ResolverHandle *rh,
3476 struct NameShortenHandle *nsh)
3478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3479 "Sending %s as shorten result\n", nsh->result);
3480 nsh->proc (nsh->proc_cls, nsh->result);
3481 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3483 free_resolver_handle (rh);
3488 * Callback calles by namestore for a zone to name
3491 * @param cls the closure
3492 * @param zone_key the zone we queried
3493 * @param expire the expiration time of the name
3494 * @param name the name found or NULL
3495 * @param rd_len number of records for the name
3496 * @param rd the record data (PKEY) for the name
3497 * @param signature the signature for the record data
3500 process_zone_to_name_shorten_root (void *cls,
3501 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3502 struct GNUNET_TIME_Absolute expire,
3504 unsigned int rd_len,
3505 const struct GNUNET_NAMESTORE_RecordData *rd,
3506 const struct GNUNET_CRYPTO_RsaSignature *signature);
3510 * Callback called by namestore for a zone to name
3513 * @param cls the closure
3514 * @param zone_key the zone we queried
3515 * @param expire the expiration time of the name
3516 * @param name the name found or NULL
3517 * @param rd_len number of records for the name
3518 * @param rd the record data (PKEY) for the name
3519 * @param signature the signature for the record data
3522 process_zone_to_name_shorten_shorten (void *cls,
3523 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3524 struct GNUNET_TIME_Absolute expire,
3526 unsigned int rd_len,
3527 const struct GNUNET_NAMESTORE_RecordData *rd,
3528 const struct GNUNET_CRYPTO_RsaSignature *signature)
3530 struct ResolverHandle *rh = cls;
3531 struct NameShortenHandle* nsh = rh->proc_cls;
3532 struct AuthorityChain *next_authority;
3534 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3535 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3538 rh->namestore_task = NULL;
3539 /* we found a match in our own root zone */
3542 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3543 memset(result, 0, answer_len);
3545 if (strlen(rh->name) > 0)
3547 sprintf (result, "%s.%s.%s.%s",
3549 nsh->shorten_zone_name,
3554 sprintf (result, "%s.%s.%s", name,
3555 nsh->shorten_zone_name,
3559 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3560 "Found shorten result %s\n", result);
3561 if (strlen (nsh->result) > strlen (result))
3562 strcpy (nsh->result, result);
3564 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3565 nsh->shorten_zone) == 0)
3568 * This is our zone append .gads unless name is empty
3569 * (it shouldn't be, usually FIXME what happens if we
3570 * shorten to our zone to a "" record??)
3573 sprintf (result, "%s.%s.%s",
3575 nsh->shorten_zone_name,
3577 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3578 "Our zone: Found %s as shorten result\n", result);
3580 if (strlen (nsh->result) > strlen (result))
3581 strcpy (nsh->result, result);
3582 //nsh->proc(nsh->proc_cls, result);
3584 //free_resolver_handle(rh);
3591 * continue with next authority if exists
3593 if (NULL == rh->authority_chain_head->next)
3595 finish_shorten (rh, nsh);
3598 next_authority = rh->authority_chain_head;
3600 if (0 == strcmp (rh->name, ""))
3601 strcpy (tmp_name, next_authority->name);
3603 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3604 "%s.%s", rh->name, next_authority->name);
3606 strcpy(rh->name, tmp_name);
3607 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3608 "No PSEU found for authority %s. Promoting back: %s\n",
3609 next_authority->name, rh->name);
3611 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3612 rh->authority_chain_tail,
3615 GNUNET_free (next_authority);
3617 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3618 &rh->authority_chain_tail->zone,
3619 &rh->authority_chain_head->zone,
3620 &process_zone_to_name_shorten_root,
3626 * Callback calles by namestore for a zone to name
3629 * @param cls the closure
3630 * @param zone_key the zone we queried
3631 * @param expire the expiration time of the name
3632 * @param name the name found or NULL
3633 * @param rd_len number of records for the name
3634 * @param rd the record data (PKEY) for the name
3635 * @param signature the signature for the record data
3638 process_zone_to_name_shorten_private (void *cls,
3639 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3640 struct GNUNET_TIME_Absolute expire,
3642 unsigned int rd_len,
3643 const struct GNUNET_NAMESTORE_RecordData *rd,
3644 const struct GNUNET_CRYPTO_RsaSignature *signature)
3646 struct ResolverHandle *rh = cls;
3647 struct NameShortenHandle* nsh = rh->proc_cls;
3648 struct AuthorityChain *next_authority;
3650 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3651 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3654 rh->namestore_task = NULL;
3655 /* we found a match in our own root zone */
3658 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3659 memset(result, 0, answer_len);
3661 if (strlen(rh->name) > 0)
3663 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3667 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3670 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3671 "Found shorten result %s\n", result);
3672 if (strlen (nsh->result) > strlen (result))
3673 strcpy (nsh->result, result);
3675 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3676 nsh->private_zone) == 0)
3679 * This is our zone append .gads unless name is empty
3680 * (it shouldn't be, usually FIXME what happens if we
3681 * shorten to our zone to a "" record??)
3684 sprintf (result, "%s.%s.%s",
3685 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3686 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3687 "Our private zone: Found %s as shorten result %s\n", result);
3688 if (strlen (nsh->result) > strlen (result))
3689 strcpy (nsh->result, result);
3692 if (0 != strcmp (nsh->shorten_zone_name, ""))
3694 /* backtrack authorities for names in priv zone */
3695 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3697 &rh->authority_chain_head->zone,
3698 &process_zone_to_name_shorten_shorten,
3705 * continue with next authority if exists
3707 if (NULL == rh->authority_chain_head->next)
3709 finish_shorten (rh, nsh);
3712 next_authority = rh->authority_chain_head;
3714 if (0 == strcmp (rh->name, ""))
3715 strcpy (tmp_name, next_authority->name);
3717 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3718 "%s.%s", rh->name, next_authority->name);
3720 strcpy(rh->name, tmp_name);
3721 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3722 "No PSEU found for authority %s. Promoting back: %s\n",
3723 next_authority->name, rh->name);
3725 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3726 rh->authority_chain_tail,
3729 GNUNET_free (next_authority);
3731 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3732 &rh->authority_chain_tail->zone,
3733 &rh->authority_chain_head->zone,
3734 &process_zone_to_name_shorten_root,
3741 * Callback calles by namestore for a zone to name
3744 * @param cls the closure
3745 * @param zone_key the zone we queried
3746 * @param expire the expiration time of the name
3747 * @param name the name found or NULL
3748 * @param rd_len number of records for the name
3749 * @param rd the record data (PKEY) for the name
3750 * @param signature the signature for the record data
3753 process_zone_to_name_shorten_root (void *cls,
3754 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3755 struct GNUNET_TIME_Absolute expire,
3757 unsigned int rd_len,
3758 const struct GNUNET_NAMESTORE_RecordData *rd,
3759 const struct GNUNET_CRYPTO_RsaSignature *signature)
3761 struct ResolverHandle *rh = cls;
3762 struct NameShortenHandle* nsh = rh->proc_cls;
3763 struct AuthorityChain *next_authority;
3764 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3765 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3768 rh->namestore_task = NULL;
3769 /* we found a match in our own root zone */
3772 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3773 memset(result, 0, answer_len);
3775 if (strlen(rh->name) > 0)
3777 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3781 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3784 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3785 "Found shorten result %s\n", result);
3786 if (strlen (nsh->result) > strlen (result))
3787 strcpy (nsh->result, result);
3789 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3790 nsh->root_zone) == 0)
3793 * This is our zone append .gads unless name is empty
3794 * (it shouldn't be, usually FIXME what happens if we
3795 * shorten to our zone to a "" record??)
3798 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3799 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3800 "Our zone: Found %s as shorten result\n", result);
3801 if (strlen (nsh->result) > strlen (result))
3802 strcpy (nsh->result, result);
3805 if (NULL != nsh->private_zone)
3807 /* backtrack authorities for names in priv zone */
3808 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3810 &rh->authority_chain_head->zone,
3811 &process_zone_to_name_shorten_private,
3814 else if (NULL != nsh->shorten_zone)
3816 /* backtrack authorities for names in shorten zone */
3817 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3819 &rh->authority_chain_head->zone,
3820 &process_zone_to_name_shorten_shorten,
3827 * continue with next authority if exists
3829 if (NULL == rh->authority_chain_head->next)
3831 finish_shorten (rh, nsh);
3834 next_authority = rh->authority_chain_head;
3836 if (0 == strcmp (rh->name, ""))
3837 strcpy (tmp_name, next_authority->name);
3839 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3840 "%s.%s", rh->name, next_authority->name);
3842 strcpy(rh->name, tmp_name);
3843 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3844 "No PSEU found for authority %s. Promoting back: %s\n",
3845 next_authority->name, rh->name);
3847 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3848 rh->authority_chain_tail,
3851 GNUNET_free (next_authority);
3853 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3854 &rh->authority_chain_tail->zone,
3855 &rh->authority_chain_head->zone,
3856 &process_zone_to_name_shorten_root,
3863 * Process result from namestore delegation lookup
3864 * for shorten operation
3866 * @param cls the client shorten handle
3867 * @param rh the resolver handle
3868 * @param rd_count number of results (0)
3869 * @param rd data (NULL)
3872 handle_delegation_ns_shorten (void* cls,
3873 struct ResolverHandle *rh,
3875 const struct GNUNET_NAMESTORE_RecordData *rd)
3877 struct NameShortenHandle *nsh;
3878 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3880 nsh = (struct NameShortenHandle *)cls;
3881 rh->namestore_task = NULL;
3883 * At this point rh->name contains the part of the name
3884 * that we do not have a PKEY in our namestore to resolve.
3885 * The authority chain in the resolver handle is now
3886 * useful to backtrack if needed
3889 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3890 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3891 memset(result, 0, sizeof (result));
3893 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3897 * This is our zone append .gads unless name is empty
3898 * (it shouldn't be, usually FIXME what happens if we
3899 * shorten to our zone to a "" record??)
3902 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3903 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3904 "Our zone: Found %s as shorten result\n", result);
3906 if (strlen (nsh->result) > strlen (result))
3907 strcpy (nsh->result, result);
3910 else if (NULL != nsh->private_zone)
3913 * This is our zone append .gads unless name is empty
3914 * (it shouldn't be, usually FIXME what happens if we
3915 * shorten to our zone to a "" record??)
3917 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3918 nsh->private_zone) == 0)
3921 sprintf (result, "%s.%s.%s",
3922 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3923 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3924 "Our zone: Found %s as shorten result in private zone %s\n",
3927 if (strlen (nsh->result) > strlen (result))
3928 strcpy (nsh->result, result);
3931 else if (NULL != nsh->shorten_zone)
3934 * This is our zone append .gads unless name is empty
3935 * (it shouldn't be, usually FIXME what happens if we
3936 * shorten to our zone to a "" record??)
3938 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3939 nsh->shorten_zone) == 0)
3941 sprintf (result, "%s.%s.%s",
3942 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3943 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3944 "Our zone: Found %s as shorten result in shorten zone\n",
3947 if (strlen (nsh->result) > strlen (result))
3948 strcpy (nsh->result, result);
3953 /* backtrack authorities for names */
3954 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3956 &rh->authority_chain_head->zone,
3957 &process_zone_to_name_shorten_root,
3964 * Callback calles by namestore for a zone to name
3967 * @param cls the closure
3968 * @param zone_key the zone we queried
3969 * @param expire the expiration time of the name
3970 * @param name the name found or NULL
3971 * @param rd_len number of records for the name
3972 * @param rd the record data (PKEY) for the name
3973 * @param signature the signature for the record data
3976 process_zone_to_name_zkey(void *cls,
3977 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3978 struct GNUNET_TIME_Absolute expire,
3980 unsigned int rd_len,
3981 const struct GNUNET_NAMESTORE_RecordData *rd,
3982 const struct GNUNET_CRYPTO_RsaSignature *signature)
3984 struct ResolverHandle *rh = cls;
3985 struct NameShortenHandle *nsh = rh->proc_cls;
3986 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3988 rh->namestore_task = NULL;
3990 /* zkey not in our zone */
3994 * In this case we have not given this PKEY a name (yet)
3995 * It is either just not in our zone or not even cached
3996 * Since we do not know at this point we will not try to shorten
3997 * because PKEY import will happen if the user follows the zkey
4000 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4001 "No name found for zkey %s returning verbatim!\n", nsh->result);
4002 /*if (strcmp(rh->name, "") != 0)
4003 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s.%s",
4004 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4006 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
4007 enc, GNUNET_GNS_TLD_ZKEY);
4009 strcpy (nsh->result, new_name);*/
4011 finish_shorten (rh, nsh);
4015 if (strcmp(rh->name, "") != 0)
4016 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
4019 strcpy(new_name, name);
4021 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4022 "Continue shorten for %s!\n", new_name);
4024 strcpy(rh->name, new_name);
4026 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4027 rh->authority_chain_tail = rh->authority_chain_head;
4028 rh->authority_chain_head->zone = rh->authority;
4031 /* Start delegation resolution in our namestore */
4032 resolve_delegation_ns (rh);
4037 * Shorten api from resolver
4039 * @param zone the root zone to use
4040 * @param pzone the private zone to use
4041 * @param szone the shorten zone to use
4042 * @param name the name to shorten
4043 * @param private_zone_name name of the private zone
4044 * @param shorten_zone_name name of the shorten zone
4045 * @param proc the processor to call with result
4046 * @param proc_cls closure to pass to proc
4049 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4050 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4051 struct GNUNET_CRYPTO_ShortHashCode *szone,
4053 const char* private_zone_name,
4054 const char* shorten_zone_name,
4055 ShortenResultProcessor proc,
4058 struct ResolverHandle *rh;
4059 struct NameShortenHandle *nsh;
4060 char string_hash[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
4061 struct GNUNET_CRYPTO_ShortHashCode zkey;
4062 char nzkey[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
4063 char* nzkey_ptr = nzkey;
4065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4066 "Starting shorten for %s!\n", name);
4068 if (is_canonical ((char*)name) == GNUNET_YES)
4070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4071 "%s is canonical. Returning verbatim\n", name);
4072 proc (proc_cls, name);
4076 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4078 nsh->proc_cls = proc_cls;
4079 nsh->root_zone = zone;
4080 nsh->private_zone = pzone;
4081 nsh->shorten_zone = szone;
4082 strcpy (nsh->private_zone_name, private_zone_name);
4083 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4084 strcpy (nsh->result, name);
4086 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4087 rh->authority = *zone;
4089 rh->proc = &handle_delegation_ns_shorten;
4091 rh->private_local_zone = *zone;
4093 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4095 "Checking for TLD...\n");
4096 if (is_zkey_tld (name) == GNUNET_YES)
4098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4101 * This is a zkey tld
4102 * build hash and use as initial authority
4105 memset (rh->name, 0,
4106 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4107 memcpy (rh->name, name,
4108 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4109 pop_tld (rh->name, string_hash);
4111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4112 "ZKEY is %s!\n", string_hash);
4114 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4116 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4120 "Cannot convert ZKEY %s to hash!\n", nzkey);
4121 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4124 proc (proc_cls, name);
4127 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4130 &process_zone_to_name_zkey,
4135 else if (is_gads_tld (name) == GNUNET_YES)
4137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4140 * Presumably GNUNET tld
4142 memset (rh->name, 0,
4143 strlen (name)-strlen (GNUNET_GNS_TLD));
4144 memcpy (rh->name, name,
4145 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4150 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4153 proc (proc_cls, name);
4157 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4158 rh->authority_chain_tail = rh->authority_chain_head;
4159 rh->authority_chain_head->zone = *zone;
4161 /* Start delegation resolution in our namestore */
4162 resolve_delegation_ns (rh);
4165 /*********** END NAME SHORTEN ********************/
4168 * Conclude get authority lookup
4170 * @param rh resolver handle
4171 * @param nah get authority lookup handle
4174 finish_get_auth (struct ResolverHandle *rh,
4175 struct GetNameAuthorityHandle *nah)
4177 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4178 "Got authority result %s\n", nah->result);
4180 nah->proc (nah->proc_cls, nah->result);
4181 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4183 free_resolver_handle (rh);
4188 * Process result from namestore delegation lookup
4189 * for get authority operation
4191 * @param cls the client get auth handle
4192 * @param rh the resolver handle
4193 * @param rd_count number of results (0)
4194 * @param rd data (NULL)
4197 handle_delegation_result_ns_get_auth(void* cls,
4198 struct ResolverHandle *rh,
4200 const struct GNUNET_NAMESTORE_RecordData *rd)
4202 struct GetNameAuthorityHandle* nah = rh->proc_cls;
4206 * At this point rh->name contains the part of the name
4207 * that we do not have a PKEY in our namestore to resolve.
4208 * The authority chain in the resolver handle is now
4209 * useful to backtrack if needed
4212 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4213 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4215 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4216 "Building response!\n");
4217 if (is_canonical (rh->name) == GNUNET_YES)
4220 * We successfully resolved the authority in the ns
4221 * FIXME for our purposes this is fine
4222 * but maybe we want to have an api that also looks
4223 * into the dht (i.e. option in message)
4225 if (strlen(rh->name) > strlen(nah->name))
4227 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4228 "Record name longer than original lookup name... odd!\n");
4232 answer_len = strlen(nah->name) - strlen(rh->name)
4233 + strlen(GNUNET_GNS_TLD) + 1;
4234 memset(nah->result, 0, answer_len);
4235 if (0 != strcmp (rh->name, ""))
4236 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4238 strcpy(nah->result, nah->name);
4240 finish_get_auth (rh, nah);
4244 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4245 "Unable to resolve authority for remaining %s!\n", rh->name);
4246 strcpy(nah->result, "");
4247 finish_get_auth (rh, nah);
4253 * Tries to resolve the authority for name
4256 * @param zone the root zone to look up for
4257 * @param pzone the private local zone
4258 * @param name the name to lookup up
4259 * @param proc the processor to call when finished
4260 * @param proc_cls the closure to pass to the processor
4263 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4264 struct GNUNET_CRYPTO_ShortHashCode pzone,
4266 GetAuthorityResultProcessor proc,
4269 struct ResolverHandle *rh;
4270 struct GetNameAuthorityHandle *nah;
4272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4273 "Starting authority resolution for %s!\n", name);
4275 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4276 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4277 rh->authority = zone;
4279 rh->private_local_zone = pzone;
4281 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4283 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4285 strcpy(rh->name, "\0");
4290 strlen(name)-strlen(GNUNET_GNS_TLD));
4291 memcpy(rh->name, name,
4292 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4295 memset(nah->name, 0,
4297 strcpy(nah->name, name);
4299 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4300 rh->authority_chain_tail = rh->authority_chain_head;
4301 rh->authority_chain_head->zone = zone;
4302 rh->proc = &handle_delegation_result_ns_get_auth;
4303 rh->proc_cls = (void*)nah;
4306 nah->proc_cls = proc_cls;
4307 strcpy (nah->result, "");
4309 /* Start delegation resolution in our namestore */
4310 resolve_delegation_ns(rh);
4314 /******** END GET AUTHORITY *************/
4316 /* end of gnunet-service-gns_resolver.c */