2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file gns/gnunet-service-gns_resolver.c
23 * @brief GNUnet GNS resolver logic
24 * @author Martin Schanzenbach
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_vpn_service.h"
33 #include "gnunet_dns_service.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "gns_protocol.h"
37 #include "gnunet_gns_service.h"
38 #include "gns_common.h"
39 #include "block_gns.h"
41 #include "gnunet-service-gns_resolver.h"
46 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
49 * DHT replication level
51 #define DHT_GNS_REPLICATION_LEVEL 5
55 * Our handle to the namestore service
57 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
60 * Our handle to the vpn service
62 static struct GNUNET_VPN_Handle *vpn_handle;
65 * Resolver handle to the dht
67 static struct GNUNET_DHT_Handle *dht_handle;
70 * Heap for parallel DHT lookups
72 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
75 * Heap for namestore queues
77 static struct GNUNET_CONTAINER_Heap *ns_task_heap;
80 * Maximum amount of parallel queries in background
82 static unsigned long long max_allowed_background_queries;
85 * Maximum amount of parallel namestore tasks in background
87 static unsigned long long max_allowed_ns_tasks;
90 * Wheather or not to ignore pending records
92 static int ignore_pending_records;
97 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
100 * Background shortening handles
102 static struct GetPseuAuthorityHandle *gph_head;
105 * Background shortening handles
107 static struct GetPseuAuthorityHandle *gph_tail;
110 * Resolver lookup list
112 static struct ResolverHandle *rlh_head;
115 * Resolver lookup list
117 static struct ResolverHandle *rlh_tail;
120 * Resolver shorten list
122 static struct ResolverHandle *nsh_head;
125 * Resolver shorten list
127 static struct ResolverHandle *nsh_tail;
130 * Resolver get auth list
132 static struct ResolverHandle *nah_head;
135 * Resolver get auth list
137 static struct ResolverHandle *nah_tail;
140 * Global configuration.
142 static const struct GNUNET_CONFIGURATION_Handle *cfg;
145 * a resolution identifier pool variable
146 * This is a non critical identifier useful for debugging
148 static unsigned long long rid_gen;
152 * Check if name is in srv format (_x._y.xxx)
155 * @return GNUNET_YES if true
158 is_srv (const char *name)
165 if (NULL == strstr (name, "._"))
168 ndup = GNUNET_strdup (name);
170 if (NULL == strtok (NULL, "."))
172 if (NULL == strtok (NULL, "."))
174 if (NULL != strtok (NULL, "."))
182 * Determine if this name is canonical (is a legal name in a zone, without delegation);
183 * note that we do not test that the name does not contain illegal characters, we only
184 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
185 * even though they consist of multiple labels.
188 * a.b.gads = not canonical
190 * _foo._srv = canonical
191 * _f.bar = not canonical
193 * @param name the name to test
194 * @return GNUNET_YES if canonical
197 is_canonical (const char *name)
202 if (NULL == strchr (name, '.'))
207 while (NULL != (dot = strchr (pos, '.')))
217 free_get_pseu_authority_handle (struct GetPseuAuthorityHandle *gph)
219 gph->namestore_task = NULL;
220 GNUNET_free (gph->auth);
221 GNUNET_CRYPTO_rsa_key_free (gph->key);
222 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
228 * Callback that shortens authorities
230 * @param gph the handle containing the name to shorten
233 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
237 * Continuation for pkey record creation (shorten)
239 * @param cls a GetPseuAuthorityHandle
240 * @param success unused
244 create_pkey_cont (void* cls, int32_t success, const char* emsg)
246 //FIXME do sth with error
247 struct GetPseuAuthorityHandle* gph = cls;
249 free_get_pseu_authority_handle (gph);
254 * Namestore calls this function if we have record for this name.
255 * (or with rd_count=0 to indicate no matches)
257 * @param cls the pending query
258 * @param key the key of the zone we did the lookup
259 * @param expiration expiration date of the namestore entry
260 * @param name the name for which we need an authority
261 * @param rd_count the number of records with 'name'
262 * @param rd the record data
263 * @param signature the signature of the authority for the record data
266 process_pseu_lookup_ns (void* cls,
267 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
268 struct GNUNET_TIME_Absolute expiration,
269 const char *name, unsigned int rd_count,
270 const struct GNUNET_NAMESTORE_RecordData *rd,
271 const struct GNUNET_CRYPTO_RsaSignature *signature)
273 struct GetPseuAuthorityHandle* gph = cls;
274 struct GNUNET_NAMESTORE_RecordData new_pkey;
276 gph->namestore_task = NULL;
279 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
280 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
281 free_get_pseu_authority_handle (gph);
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
287 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n",
290 new_pkey.expiration_time = UINT64_MAX;
291 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
292 new_pkey.data = &gph->auth->zone;
293 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
294 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
295 | GNUNET_NAMESTORE_RF_PRIVATE
296 | GNUNET_NAMESTORE_RF_PENDING;
297 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
307 * process result of a dht pseu lookup
309 * @param gph the handle
310 * @param name the pseu result or NULL
313 process_pseu_result (struct GetPseuAuthorityHandle* gph,
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "GNS_AUTO_PSEU: No PSEU, no shorten. Finished.\n");
320 free_get_pseu_authority_handle (gph);
324 memcpy (gph->test_name, name, strlen(name) + 1);
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
329 * Check for collision
331 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
334 GNUNET_NAMESTORE_TYPE_ANY,
335 &process_pseu_lookup_ns,
341 * Handle timeout for dht request
343 * @param cls the request handle as closure
344 * @param tc the task context
347 handle_auth_discovery_timeout (void *cls,
348 const struct GNUNET_SCHEDULER_TaskContext *tc)
350 struct GetPseuAuthorityHandle* gph = cls;
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
354 GNUNET_DHT_get_stop (gph->get_handle);
355 gph->get_handle = NULL;
356 process_pseu_result (gph, NULL);
361 * Function called when we find a PSEU entry in the DHT
363 * @param cls the request handle
364 * @param exp lifetime
365 * @param key the key the record was stored under
366 * @param get_path get path
367 * @param get_path_length get path length
368 * @param put_path put path
369 * @param put_path_length put path length
370 * @param type the block type
371 * @param size the size of the record
372 * @param data the record data
375 process_auth_discovery_dht_result (void* cls,
376 struct GNUNET_TIME_Absolute exp,
377 const struct GNUNET_HashCode * key,
378 const struct GNUNET_PeerIdentity *get_path,
379 unsigned int get_path_length,
380 const struct GNUNET_PeerIdentity *put_path,
381 unsigned int put_path_length,
382 enum GNUNET_BLOCK_Type type,
386 struct GetPseuAuthorityHandle* gph = cls;
387 struct GNSNameRecordBlock *nrb;
388 const char* rd_data = data;
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
397 /* stop lookup and timeout task */
398 GNUNET_DHT_get_stop (gph->get_handle);
399 gph->get_handle = NULL;
400 GNUNET_SCHEDULER_cancel (gph->timeout);
404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
405 "GNS_GET_AUTH: got dht result null!\n", size);
406 free_get_pseu_authority_handle (gph);
410 nrb = (struct GNSNameRecordBlock*)data;
411 name = (char*)&nrb[1];
412 num_records = ntohl (nrb->rd_count);
414 struct GNUNET_NAMESTORE_RecordData rd[num_records];
416 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
417 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
419 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
424 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
425 "GNS_GET_AUTH: Error deserializing data!\n");
429 for (i=0; i < num_records; i++)
431 if ((strcmp (name, "+") == 0) &&
432 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
435 process_pseu_result (gph, (char*)rd[i].data);
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442 "GNS_GET_AUTH: finished shorten, no results!\n");
443 process_pseu_result (gph, NULL);
448 * Process PSEU discovery for shorten via namestore
450 * @param cls the GetPseuAuthorityHandle
451 * @param key the public key
452 * @param expiration recorddata expiration
453 * @param name the looked up name
454 * @param rd_count number of records in set
455 * @param rd record data
456 * @param signature the signature
459 process_auth_discovery_ns_result (void* cls,
460 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
461 struct GNUNET_TIME_Absolute expiration,
463 unsigned int rd_count,
464 const struct GNUNET_NAMESTORE_RecordData *rd,
465 const struct GNUNET_CRYPTO_RsaSignature *signature)
467 struct GetPseuAuthorityHandle* gph = cls;
468 struct GNUNET_HashCode lookup_key;
472 gph->namestore_task = NULL;
476 GNUNET_GNS_get_key_for_record (GNUNET_GNS_TLD_PLUS, &gph->auth->zone, &lookup_key);
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
478 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
480 GNUNET_h2s (&lookup_key));
482 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
483 &handle_auth_discovery_timeout, gph);
485 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
487 GNUNET_assert (gph->get_handle == NULL);
489 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
490 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
492 DHT_GNS_REPLICATION_LEVEL,
493 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
496 &process_auth_discovery_dht_result,
501 for (i=0; i < rd_count; i++)
503 if (0 != (strcmp (name, GNUNET_GNS_TLD_PLUS)))
506 if (rd[i].record_type != GNUNET_GNS_RECORD_PSEU)
510 process_pseu_result (gph, (char*)rd[i].data);
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
515 process_pseu_result (gph, NULL);
520 * Callback called by namestore for a zone to name
523 * @param cls the closure
524 * @param zone_key the zone we queried
525 * @param expire the expiration time of the name
526 * @param name the name found or NULL
527 * @param rd_len number of records for the name
528 * @param rd the record data (PKEY) for the name
529 * @param signature the signature for the record data
532 process_zone_to_name_discover (void *cls,
533 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
534 struct GNUNET_TIME_Absolute expire,
537 const struct GNUNET_NAMESTORE_RecordData *rd,
538 const struct GNUNET_CRYPTO_RsaSignature *signature)
540 struct GetPseuAuthorityHandle* gph = cls;
542 gph->namestore_task = NULL;
545 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
548 GNUNET_GNS_RECORD_PSEU,
549 &process_auth_discovery_ns_result,
553 /* we found a match in our own zone */
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
556 free_get_pseu_authority_handle (gph);
561 * Callback that shortens authorities
563 * @param gph the handle to the shorten request
566 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "GNS_AUTO_PSEU: New authority %s discovered\n",
572 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
575 &process_zone_to_name_discover,
581 * Start shortening algorithm using auth as
584 * @param auth the authorities that were resolved
585 * @param key the private key for PKEY import
588 start_shorten (struct AuthorityChain *auth,
589 const struct GNUNET_CRYPTO_RsaPrivateKey *key)
591 struct GetPseuAuthorityHandle *gph;
592 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
593 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
595 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
596 if (NULL == (pb_key = GNUNET_CRYPTO_rsa_encode_key (key)))
598 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
599 "Failed to encode RSA key on shorten\n");
602 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
603 gph->key = GNUNET_CRYPTO_rsa_decode_key ((const char*) pb_key, ntohs (pb_key->len));
604 GNUNET_free (pb_key);
605 if (NULL == gph->key)
607 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
608 "Failed to decode RSA key on shorten\n");
612 GNUNET_CRYPTO_short_hash (&pkey,
613 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
615 gph->auth = GNUNET_malloc (sizeof (struct AuthorityChain));
616 memcpy (gph->auth, auth, sizeof (struct AuthorityChain));
617 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
618 shorten_authority_chain (gph);
623 * Initialize the resolver
625 * @param nh the namestore handle
626 * @param dh the dht handle
627 * @param lz the local zone's hash
628 * @param c configuration handle
629 * @param max_bg_queries maximum number of parallel background queries in dht
630 * @param ignore_pending ignore records that still require user confirmation
632 * @return GNUNET_OK on success
635 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
636 struct GNUNET_DHT_Handle *dh,
637 struct GNUNET_CRYPTO_ShortHashCode lz,
638 const struct GNUNET_CONFIGURATION_Handle *c,
639 unsigned long long max_bg_queries,
644 return GNUNET_SYSERR;
647 namestore_handle = nh;
651 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
653 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
654 max_allowed_background_queries = max_bg_queries;
655 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
656 ignore_pending_records = ignore_pending;
657 GNUNET_RESOLVER_connect (cfg);
664 * @param rh resolver handle
665 * @param rlh record lookup handle
666 * @param rd_count number of results
670 finish_lookup (struct ResolverHandle *rh,
671 struct RecordLookupHandle* rlh,
672 unsigned int rd_count,
673 const struct GNUNET_NAMESTORE_RecordData *rd);
677 * Helper function to free resolver handle
679 * @param rh the handle to free
682 free_resolver_handle (struct ResolverHandle* rh)
684 struct AuthorityChain *ac;
685 struct AuthorityChain *ac_next;
690 ac_next = rh->authority_chain_head;
691 while (NULL != (ac = ac_next))
697 if (NULL != rh->get_handle)
698 GNUNET_DHT_get_stop (rh->get_handle);
699 if (NULL != rh->dns_raw_packet)
700 GNUNET_free (rh->dns_raw_packet);
701 if (NULL != rh->namestore_task)
703 GNUNET_NAMESTORE_cancel (rh->namestore_task);
704 rh->namestore_task = NULL;
706 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
707 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
708 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
709 GNUNET_SCHEDULER_cancel (rh->timeout_task);
710 if (NULL != rh->dns_sock)
711 GNUNET_NETWORK_socket_close (rh->dns_sock);
712 if (NULL != rh->dns_resolver_handle)
713 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
714 if (NULL != rh->rd.data)
715 GNUNET_free ((void*)(rh->rd.data));
716 if (NULL != rh->dht_heap_node)
717 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
725 * @param rh resolver handle
726 * @param nsh name shorten handle
729 finish_shorten (struct ResolverHandle *rh,
730 struct NameShortenHandle *nsh);
736 * @param rh resolver handle
737 * @param nah get name authority handle
740 finish_get_auth (struct ResolverHandle *rh,
741 struct GetNameAuthorityHandle* rlh);
748 gns_resolver_cleanup ()
750 struct GetPseuAuthorityHandle *tmp;
751 struct ResolverHandle *rh;
752 struct NamestoreBGTask *nbg;
754 while (NULL != (tmp = gph_head))
756 if (tmp->get_handle != NULL)
758 GNUNET_DHT_get_stop (tmp->get_handle);
759 tmp->get_handle = NULL;
761 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
763 GNUNET_SCHEDULER_cancel (tmp->timeout);
764 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
766 if (NULL != tmp->namestore_task)
768 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
769 tmp->namestore_task = NULL;
771 free_get_pseu_authority_handle (tmp);
774 while (NULL != rlh_head)
776 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
778 while (NULL != nsh_head)
780 finish_shorten (nsh_head, nsh_head->proc_cls);
782 while (NULL != nah_head)
784 finish_get_auth (nah_head, nah_head->proc_cls);
787 while (NULL != (rh = GNUNET_CONTAINER_heap_remove_root(dht_lookup_heap)))
791 GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
792 dht_lookup_heap = NULL;
794 while (NULL != (nbg = GNUNET_CONTAINER_heap_remove_root(ns_task_heap)))
796 GNUNET_NAMESTORE_cancel (nbg->qe);
799 GNUNET_CONTAINER_heap_destroy (ns_task_heap);
806 * Callback when record data is put into namestore
808 * @param cls the closure
809 * @param success GNUNET_OK on success
810 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
813 on_namestore_record_put_result (void *cls,
817 struct NamestoreBGTask *nbg = cls;
819 GNUNET_CONTAINER_heap_remove_node (nbg->node);
822 if (GNUNET_NO == success)
824 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
825 "GNS_NS: records already in namestore\n");
828 else if (GNUNET_YES == success)
830 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
831 "GNS_NS: records successfully put in namestore\n");
835 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
836 "GNS_NS: Error putting records into namestore: %s\n", emsg);
841 * Lookup timeout task
843 * @param cls the ResolverHandle for the task that timed out
844 * @param tc the task context
847 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
849 struct ResolverHandle *rh = cls;
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852 "Lookup timeout for request %llu triggered\n",
854 if (NULL != rh->timeout_cont)
855 rh->timeout_cont (rh->timeout_cont_cls, tc);
860 * Processor for background lookups in the DHT
862 * @param cls closure (NULL)
863 * @param rd_count number of records found (not 0)
864 * @param rd record data
867 background_lookup_result_processor (void *cls,
869 const struct GNUNET_NAMESTORE_RecordData *rd)
871 //We could do sth verbose/more useful here but it doesn't make any difference
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "GNS_BG: background dht lookup finished. (%d results)\n",
879 * Handle timeout for DHT requests
881 * @param cls the request handle as closure
882 * @param tc the task context
885 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
887 struct ResolverHandle *rh = cls;
888 struct RecordLookupHandle *rlh = rh->proc_cls;
889 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus) timed out.\n",
893 rh->id, rh->name, rh->timeout.rel_value);
895 * Start resolution in bg
897 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
898 rh->name, GNUNET_GNS_TLD);
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
902 rh->id, new_name, rlh->record_type);
904 gns_resolver_lookup_record (rh->authority,
905 rh->private_local_zone,
909 GNUNET_TIME_UNIT_FOREVER_REL,
911 &background_lookup_result_processor,
914 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
916 GNUNET_DHT_get_stop (rh->get_handle);
917 rh->get_handle = NULL;
918 rh->proc (rh->proc_cls, rh, 0, NULL);
923 * Function called when we get a result from the dht
924 * for our record query
926 * @param cls the request handle
927 * @param exp lifetime
928 * @param key the key the record was stored under
929 * @param get_path get path
930 * @param get_path_length get path length
931 * @param put_path put path
932 * @param put_path_length put path length
933 * @param type the block type
934 * @param size the size of the record
935 * @param data the record data
938 process_record_result_dht (void* cls,
939 struct GNUNET_TIME_Absolute exp,
940 const struct GNUNET_HashCode * key,
941 const struct GNUNET_PeerIdentity *get_path,
942 unsigned int get_path_length,
943 const struct GNUNET_PeerIdentity *put_path,
944 unsigned int put_path_length,
945 enum GNUNET_BLOCK_Type type,
946 size_t size, const void *data)
948 struct ResolverHandle *rh = cls;
949 struct RecordLookupHandle *rlh = rh->proc_cls;
950 const struct GNSNameRecordBlock *nrb = data;
951 uint32_t num_records;
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
959 /* stop lookup and timeout task */
960 GNUNET_DHT_get_stop (rh->get_handle);
961 rh->get_handle = NULL;
962 if (rh->dht_heap_node != NULL)
964 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
965 rh->dht_heap_node = NULL;
967 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
969 GNUNET_SCHEDULER_cancel (rh->timeout_task);
970 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
972 rh->get_handle = NULL;
973 name = (const char*) &nrb[1];
974 num_records = ntohl (nrb->rd_count);
976 struct GNUNET_NAMESTORE_RecordData rd[num_records];
977 struct NamestoreBGTask *ns_heap_root;
978 struct NamestoreBGTask *namestore_bg_task;
980 rd_data = &name[strlen (name) + 1];
981 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
983 GNUNET_NAMESTORE_records_deserialize (rd_size,
988 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
989 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
990 rh->proc (rh->proc_cls, rh, 0, NULL);
993 for (i = 0; i < num_records; i++)
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
996 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
997 rh->id, name, rh->name);
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1000 rh->id, rd[i].record_type, rlh->record_type);
1001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1002 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1003 rh->id, rd[i].data_size);
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "GNS_PHASE_REC-%llu: Got flag %d\n",
1006 rh->id, rd[i].flags);
1008 if ((strcmp (name, rh->name) == 0) &&
1009 (rd[i].record_type == rlh->record_type))
1015 * FIXME check pubkey against existing key in namestore?
1016 * https://gnunet.org/bugs/view.php?id=2179
1018 if (max_allowed_ns_tasks <=
1019 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1021 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1022 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1029 /* Save to namestore */
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031 "GNS_PHASE_REC-%llu: Caching record for %s\n",
1033 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1034 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1041 &on_namestore_record_put_result, //cont
1044 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1046 GNUNET_TIME_absolute_get().abs_value);
1047 if (0 < rh->answered)
1048 rh->proc (rh->proc_cls, rh, num_records, rd);
1050 rh->proc (rh->proc_cls, rh, 0, NULL);
1056 * Start DHT lookup for a (name -> query->record_type) record in
1057 * rh->authority's zone
1059 * @param rh the pending gns query context
1062 resolve_record_dht (struct ResolverHandle *rh)
1064 struct RecordLookupHandle *rlh = rh->proc_cls;
1066 struct GNUNET_HashCode lookup_key;
1067 struct ResolverHandle *rh_heap_root;
1069 GNUNET_GNS_get_key_for_record (rh->name, &rh->authority, &lookup_key);
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1072 rh->id, rh->name, GNUNET_h2s (&lookup_key));
1074 rh->dht_heap_node = NULL;
1076 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
1079 * Update timeout if necessary
1081 if (GNUNET_SCHEDULER_NO_TASK == rh->timeout_task)
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "GNS_PHASE_REC-%llu: Adjusting timeout to %s/2\n",
1086 GNUNET_STRINGS_relative_time_to_string (rh->timeout, GNUNET_YES));
1088 * Set timeout for authority lookup phase to 1/2
1090 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1091 GNUNET_TIME_relative_divide (rh->timeout, 2),
1092 &handle_lookup_timeout,
1095 rh->timeout_cont = &dht_lookup_timeout;
1096 rh->timeout_cont_cls = rh;
1100 if (max_allowed_background_queries <=
1101 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1103 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1104 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1105 rh_heap_root->get_handle = NULL;
1106 rh_heap_root->dht_heap_node = NULL;
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1109 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1110 rh->id, rh_heap_root->name);
1111 rh_heap_root->proc (rh_heap_root->proc_cls,
1116 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1118 GNUNET_TIME_absolute_get ().abs_value);
1121 xquery = htonl (rlh->record_type);
1123 GNUNET_assert (rh->get_handle == NULL);
1124 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1125 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1127 DHT_GNS_REPLICATION_LEVEL,
1128 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1131 &process_record_result_dht,
1138 * Namestore calls this function if we have record for this name.
1139 * (or with rd_count=0 to indicate no matches)
1141 * @param cls the pending query
1142 * @param key the key of the zone we did the lookup
1143 * @param expiration expiration date of the namestore entry
1144 * @param name the name for which we need an authority
1145 * @param rd_count the number of records with 'name'
1146 * @param rd the record data
1147 * @param signature the signature of the authority for the record data
1150 process_record_result_ns (void* cls,
1151 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1152 struct GNUNET_TIME_Absolute expiration,
1153 const char *name, unsigned int rd_count,
1154 const struct GNUNET_NAMESTORE_RecordData *rd,
1155 const struct GNUNET_CRYPTO_RsaSignature *signature)
1157 struct ResolverHandle *rh = cls;
1158 struct RecordLookupHandle *rlh = rh->proc_cls;
1159 struct GNUNET_TIME_Relative remaining_time;
1160 struct GNUNET_CRYPTO_ShortHashCode zone;
1161 struct GNUNET_TIME_Absolute et;
1164 rh->namestore_task = NULL;
1165 GNUNET_CRYPTO_short_hash (key,
1166 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1168 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1172 rh->status |= RSL_RECORD_EXISTS;
1173 if (remaining_time.rel_value == 0)
1174 rh->status |= RSL_RECORD_EXPIRED;
1179 * Lookup terminated and no results
1181 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1182 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1185 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1186 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1189 * Our zone and no result? Cannot resolve TT
1191 rh->proc(rh->proc_cls, rh, 0, NULL);
1195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1196 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1198 for (i = 0; i < rd_count;i++)
1200 if (rd[i].record_type != rlh->record_type)
1203 if (ignore_pending_records &&
1204 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1206 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1207 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1212 //FIXME: eh? do I have to handle this here?
1213 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1214 et.abs_value = rd[i].expiration_time;
1215 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
1217 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1218 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1228 if (0 == rh->answered)
1230 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1231 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1232 rh->proc(rh->proc_cls, rh, 0, NULL);
1236 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1237 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1238 rh->id, rh->answered, rd_count);
1239 rh->proc(rh->proc_cls, rh, rd_count, rd);
1244 * VPN redirect result callback
1246 * @param cls the resolver handle
1247 * @param af the requested address family
1248 * @param address in_addr(6) respectively
1251 process_record_result_vpn (void* cls, int af, const void *address)
1253 struct ResolverHandle *rh = cls;
1254 struct RecordLookupHandle *rlh = rh->proc_cls;
1255 struct GNUNET_NAMESTORE_RecordData rd;
1257 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1258 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1262 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1263 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1265 if (GNUNET_GNS_RECORD_A != rlh->record_type)
1267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1268 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1270 rh->proc (rh->proc_cls, rh, 0, NULL);
1273 rd.record_type = GNUNET_GNS_RECORD_A;
1274 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1276 rd.data_size = sizeof (struct in_addr);
1278 rh->proc (rh->proc_cls, rh, 1, &rd);
1281 else if (AF_INET6 == af)
1283 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1284 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1286 if (GNUNET_GNS_RECORD_AAAA != rlh->record_type)
1288 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1289 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1291 rh->proc (rh->proc_cls, rh, 0, NULL);
1294 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1295 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1297 rd.data_size = sizeof (struct in6_addr);
1299 rh->proc (rh->proc_cls, rh, 1, &rd);
1303 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1304 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1306 rh->proc (rh->proc_cls, rh, 0, NULL);
1311 * Process VPN lookup result for record
1313 * @param cls the record lookup handle
1314 * @param rh resolver handle
1315 * @param rd_count number of results (1)
1316 * @param rd record data containing the result
1319 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1320 unsigned int rd_count,
1321 const struct GNUNET_NAMESTORE_RecordData *rd)
1323 struct RecordLookupHandle* rlh = cls;
1327 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1328 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1331 /* give up, cannot resolve */
1332 finish_lookup(rh, rlh, 0, NULL);
1336 /* results found yay */
1337 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1338 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1340 finish_lookup(rh, rlh, rd_count, rd);
1345 * Sends a UDP dns query to a nameserver specified in the rh
1347 * @param rh the resolver handle
1350 send_dns_packet (struct ResolverHandle *rh);
1356 * @param cls the ResolverHandle for this lookup
1357 * @param addr the sockaddr
1358 * @param addrlen the socket address length
1361 handle_dns_resolver (void *cls,
1362 const struct sockaddr *addr,
1365 struct ResolverHandle *rh = cls;
1366 struct RecordLookupHandle *rlh = rh->proc_cls;
1367 struct GNUNET_NAMESTORE_RecordData rd;
1368 struct sockaddr_in *sai;
1369 struct sockaddr_in6 *sai6;
1373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1374 "No address found in DNS!\n");
1375 finish_lookup (rh, rlh, 0, NULL);
1379 if (sizeof (struct sockaddr_in) == addrlen)
1381 sai = (struct sockaddr_in*) addr;
1382 rd.record_type = GNUNET_GNS_RECORD_A;
1383 rd.data_size = sizeof (struct in_addr);
1384 rd.data = &sai->sin_addr;
1386 else if (sizeof (struct sockaddr_in6) == addrlen)
1388 sai6 = (struct sockaddr_in6*) addr;
1389 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1390 rd.data_size = sizeof (struct in6_addr);
1391 rd.data = &sai6->sin6_addr;
1395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1396 "Address length is garbage!\n");
1397 finish_lookup (rh, rlh, 0, NULL);
1400 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1402 finish_lookup (rh, rlh, 1, &rd);
1407 * Resolve DNS name via local stub resolver
1409 * @param rh the resolver handle
1412 resolve_dns_name (struct ResolverHandle *rh)
1414 struct RecordLookupHandle *rlh = rh->proc_cls;
1417 if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1418 (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1421 "Can only resolve A/AAAA via stub... abort\n");
1422 finish_lookup (rh, rlh, 0, NULL);
1426 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1431 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1434 &handle_dns_resolver,
1440 * Read DNS udp packet from socket
1442 * @param cls the resolver handle
1443 * @param tc task context
1446 read_dns_response (void *cls,
1447 const struct GNUNET_SCHEDULER_TaskContext *tc)
1449 struct ResolverHandle *rh = cls;
1450 struct RecordLookupHandle *rlh = rh->proc_cls;
1451 char buf[UINT16_MAX];
1453 struct sockaddr_in addr;
1455 struct GNUNET_DNSPARSER_Packet *packet;
1456 struct GNUNET_NAMESTORE_RecordData rd;
1457 int found_delegation = GNUNET_NO;
1458 int found_cname = GNUNET_NO;
1459 char* delegation_name = NULL;
1460 int zone_offset = 0;
1463 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1464 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1466 /* timeout or shutdown */
1467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1468 "Terminating DNS query %d\n", tc->reason);
1469 finish_lookup (rh, rlh, 0, NULL);
1473 addrlen = sizeof (addr);
1474 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1476 (struct sockaddr*) &addr,
1481 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1482 finish_lookup (rh, rlh, 0, NULL);
1485 if (NULL == (packet = GNUNET_DNSPARSER_parse (buf, r)))
1487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1488 "Failed to parse DNS reply!\n");
1489 finish_lookup (rh, rlh, 0, NULL);
1493 for (i = 0; i < packet->num_answers; i++)
1495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1496 "Got record type %d (want %d)\n",
1497 packet->answers[i].type,
1499 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1500 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503 "CNAME... restarting query with %s\n",
1504 packet->answers[i].data.hostname
1506 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1507 found_cname = GNUNET_YES;
1511 if ((packet->answers[i].type == rlh->record_type) &&
1512 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1516 rd.data = packet->answers[i].data.raw.data;
1517 rd.data_size = packet->answers[i].data.raw.data_len;
1518 rd.record_type = packet->answers[i].type;
1520 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1521 finish_lookup (rh, rlh, 1, &rd);
1522 GNUNET_DNSPARSER_free_packet (packet);
1527 if (GNUNET_YES == found_cname)
1529 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1531 if (0 > zone_offset)
1534 /* restart query with CNAME */
1535 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1538 "Asking same server for %s\n", rh->dns_name);
1539 send_dns_packet (rh);
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1544 "Trying system resolver for %s\n", rh->dns_name);
1545 resolve_dns_name (rh);
1548 GNUNET_DNSPARSER_free_packet (packet);
1552 for (i = 0; i < packet->num_authority_records; i++)
1554 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1557 "Found NS delegation!\n");
1558 found_delegation = GNUNET_YES;
1559 delegation_name = packet->authority_records[i].data.hostname;
1564 for (i = 0; i < packet->num_additional_records; i++)
1566 if (GNUNET_NO == found_delegation)
1569 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1570 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1572 GNUNET_assert (sizeof (struct in_addr) ==
1573 packet->authority_records[i].data.raw.data_len);
1575 rh->dns_addr.sin_addr =
1576 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1577 send_dns_packet (rh);
1578 GNUNET_DNSPARSER_free_packet (packet);
1583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1584 "Nothing useful in DNS reply!\n");
1585 finish_lookup (rh, rlh, 0, NULL);
1586 GNUNET_DNSPARSER_free_packet (packet);
1591 * Sends a UDP dns query to a nameserver specified in the rh
1593 * @param rh the request handle
1596 send_dns_packet (struct ResolverHandle *rh)
1598 struct GNUNET_NETWORK_FDSet *rset;
1600 rset = GNUNET_NETWORK_fdset_create ();
1601 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "Sending %d byte DNS query\n",
1604 (int) rh->dns_raw_packet_size);
1606 if (GNUNET_SYSERR ==
1607 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1609 rh->dns_raw_packet_size,
1610 (struct sockaddr*)&rh->dns_addr,
1611 sizeof (struct sockaddr_in)))
1612 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1613 _("Failed to send DNS request to %s\n"),
1614 GNUNET_a2s ((const struct sockaddr *)&rh->dns_addr,
1615 sizeof (struct sockaddr_in)));
1617 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1618 rh->timeout, //FIXME less?
1623 GNUNET_NETWORK_fdset_destroy (rset);
1628 * The final phase of resoution.
1629 * We found a NS RR and want to resolve via DNS
1631 * @param rh the pending lookup handle
1632 * @param rd_count length of record data
1633 * @param rd record data containing VPN RR
1636 resolve_record_dns (struct ResolverHandle *rh,
1637 unsigned int rd_count,
1638 const struct GNUNET_NAMESTORE_RecordData *rd)
1640 struct RecordLookupHandle *rlh = rh->proc_cls;
1641 struct GNUNET_DNSPARSER_Query query;
1642 struct GNUNET_DNSPARSER_Packet packet;
1643 struct GNUNET_DNSPARSER_Flags flags;
1644 struct in_addr dnsip;
1645 struct sockaddr_in addr;
1646 struct sockaddr *sa;
1649 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1650 memset (rh->dns_name, 0, sizeof (rh->dns_name));
1652 /* We cancel here as to not include the ns lookup in the timeout */
1653 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1655 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1656 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1658 /* Start shortening */
1659 if ((NULL != rh->priv_key) &&
1660 (GNUNET_YES == is_canonical (rh->name)))
1662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1663 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1665 start_shorten (rh->authority_chain_head,
1669 for (i = 0; i < rd_count; i++)
1671 /* Synthesize dns name */
1672 if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1674 strcpy (rh->dns_zone, (char*)rd[i].data);
1675 if (0 == strcmp (rh->name, ""))
1676 strcpy (rh->dns_name, (char*)rd[i].data);
1678 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1681 if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1682 /* need to use memcpy as .data may be unaligned */
1683 memcpy (&dnsip, rd[i].data, sizeof (dnsip));
1686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1687 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1691 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1692 if (NULL == rh->dns_sock)
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1697 finish_lookup (rh, rlh, 0, NULL);
1701 memset (&addr, 0, sizeof (struct sockaddr_in));
1702 sa = (struct sockaddr *) &addr;
1703 sa->sa_family = AF_INET;
1704 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1706 sizeof (struct sockaddr_in)))
1708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1709 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1711 finish_lookup (rh, rlh, 0, NULL);
1714 query.name = rh->dns_name;
1715 query.type = rlh->record_type;
1716 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1717 memset (&flags, 0, sizeof (flags));
1718 flags.recursion_desired = 1;
1719 flags.checking_disabled = 1;
1720 packet.queries = &query;
1721 packet.answers = NULL;
1722 packet.authority_records = NULL;
1723 packet.num_queries = 1;
1724 packet.num_answers = 0;
1725 packet.num_authority_records = 0;
1726 packet.num_additional_records = 0;
1727 packet.flags = flags;
1729 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1731 &rh->dns_raw_packet,
1732 &rh->dns_raw_packet_size))
1734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1735 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1737 GNUNET_NETWORK_socket_close (rh->dns_sock);
1738 finish_lookup (rh, rlh, 0, NULL);
1742 rh->dns_addr.sin_family = AF_INET;
1743 rh->dns_addr.sin_port = htons (53); //domain
1744 rh->dns_addr.sin_addr = dnsip;
1745 #if HAVE_SOCKADDR_IN_SIN_LEN
1746 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1748 send_dns_packet (rh);
1753 * The final phase of resoution.
1754 * We found a VPN RR and want to request an IPv4/6 address
1756 * @param rh the pending lookup handle
1757 * @param rd_count length of record data
1758 * @param rd record data containing VPN RR
1761 resolve_record_vpn (struct ResolverHandle *rh,
1762 unsigned int rd_count,
1763 const struct GNUNET_NAMESTORE_RecordData *rd)
1765 struct RecordLookupHandle *rlh = rh->proc_cls;
1766 struct GNUNET_HashCode serv_desc;
1767 struct vpn_data* vpn;
1770 /* We cancel here as to not include the ns lookup in the timeout */
1771 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1773 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1774 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1776 /* Start shortening */
1777 if ((NULL != rh->priv_key) &&
1778 (GNUNET_YES == is_canonical (rh->name)))
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1781 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1783 start_shorten (rh->authority_chain_head,
1787 vpn = (struct vpn_data*)rd->data;
1788 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1789 strlen ((char*)&vpn[1]) + 1,
1791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1792 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1795 GNUNET_h2s (&vpn->peer));
1797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1798 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1801 GNUNET_h2s (&serv_desc));
1802 rh->proc = &handle_record_vpn;
1803 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1807 if (NULL == vpn_handle)
1809 vpn_handle = GNUNET_VPN_connect (cfg);
1810 if (NULL == vpn_handle)
1812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1813 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1814 finish_lookup (rh, rh->proc_cls, 0, NULL);
1819 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1820 af, ntohs (vpn->proto),
1821 (struct GNUNET_PeerIdentity *)&vpn->peer,
1824 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1825 &process_record_result_vpn,
1831 * The final phase of resolution.
1832 * rh->name is a name that is canonical and we do not have a delegation.
1833 * Query namestore for this record
1835 * @param rh the pending lookup handle
1838 resolve_record_ns(struct ResolverHandle *rh)
1840 struct RecordLookupHandle *rlh = rh->proc_cls;
1842 /* We cancel here as to not include the ns lookup in the timeout */
1843 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1845 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1846 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1848 /* Start shortening */
1849 if ((NULL != rh->priv_key) &&
1850 (GNUNET_YES == is_canonical (rh->name)))
1852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1853 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1855 start_shorten (rh->authority_chain_head,
1860 * Try to resolve this record in our namestore.
1861 * The name to resolve is now in rh->authority_name
1862 * since we tried to resolve it to an authority
1865 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1869 &process_record_result_ns,
1875 * Handle timeout for DHT requests
1877 * @param cls the request handle as closure
1878 * @param tc the task context
1881 dht_authority_lookup_timeout (void *cls,
1882 const struct GNUNET_SCHEDULER_TaskContext *tc)
1884 struct ResolverHandle *rh = cls;
1885 struct RecordLookupHandle *rlh = rh->proc_cls;
1886 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
1888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1889 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus) timed out.\n",
1890 rh->id, rh->authority_name,
1891 rh->timeout.rel_value);
1893 rh->status |= RSL_TIMED_OUT;
1894 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1895 if (NULL != rh->get_handle)
1897 GNUNET_DHT_get_stop (rh->get_handle);
1898 rh->get_handle = NULL;
1900 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1903 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
1905 rh->proc (rh->proc_cls, rh, 0, NULL);
1908 if (0 == strcmp (rh->name, ""))
1911 * promote authority back to name and try to resolve record
1913 strcpy (rh->name, rh->authority_name);
1914 rh->proc (rh->proc_cls, rh, 0, NULL);
1919 * Start resolution in bg
1921 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
1923 rh->name, rh->authority_name, GNUNET_GNS_TLD);
1924 strcpy (rh->name, new_name);
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1926 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1929 gns_resolver_lookup_record (rh->authority,
1930 rh->private_local_zone,
1934 GNUNET_TIME_UNIT_FOREVER_REL,
1936 &background_lookup_result_processor,
1938 rh->proc (rh->proc_cls, rh, 0, NULL);
1943 * Start DHT lookup for a name -> PKEY (compare NS) record in
1944 * rh->authority's zone
1946 * @param rh the pending gns query
1949 resolve_delegation_dht (struct ResolverHandle *rh);
1953 * Resolve the delegation chain for the request in our namestore
1955 * @param rh the resolver handle
1958 resolve_delegation_ns (struct ResolverHandle *rh);
1962 * Namestore resolution for delegation finished. Processing result.
1964 * @param cls the closure
1965 * @param rh resolver handle
1966 * @param rd_count number of results (always 0)
1967 * @param rd record data (always NULL)
1970 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
1971 unsigned int rd_count,
1972 const struct GNUNET_NAMESTORE_RecordData *rd);
1976 * This is a callback function that checks for key revocation
1978 * @param cls the pending query
1979 * @param key the key of the zone we did the lookup
1980 * @param expiration expiration date of the record data set in the namestore
1981 * @param name the name for which we need an authority
1982 * @param rd_count the number of records with 'name'
1983 * @param rd the record data
1984 * @param signature the signature of the authority for the record data
1987 process_pkey_revocation_result_ns (void *cls,
1988 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1989 struct GNUNET_TIME_Absolute expiration,
1991 unsigned int rd_count,
1992 const struct GNUNET_NAMESTORE_RecordData *rd,
1993 const struct GNUNET_CRYPTO_RsaSignature *signature)
1995 struct ResolverHandle *rh = cls;
1996 struct GNUNET_TIME_Relative remaining_time;
1999 rh->namestore_task = NULL;
2000 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2002 for (i = 0; i < rd_count; i++)
2004 if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2006 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2007 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2009 rh->status |= RSL_PKEY_REVOKED;
2010 rh->proc (rh->proc_cls, rh, 0, NULL);
2015 if ((NULL == name) ||
2016 (0 == remaining_time.rel_value))
2018 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2019 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2022 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2024 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2025 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2026 rh->id, "+.gads", GNUNET_GNS_RECORD_REV);
2028 gns_resolver_lookup_record(rh->authority,
2029 rh->private_local_zone,
2030 GNUNET_GNS_RECORD_REV,
2033 GNUNET_TIME_UNIT_FOREVER_REL,
2035 &background_lookup_result_processor,
2039 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2040 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2043 * We are done with PKEY resolution if name is empty
2044 * else resolve again with new authority
2046 if (strcmp (rh->name, "") == 0)
2047 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2049 resolve_delegation_ns (rh);
2054 * Callback when record data is put into namestore
2056 * @param cls the closure
2057 * @param success GNUNET_OK on success
2058 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2061 on_namestore_delegation_put_result(void *cls,
2065 struct NamestoreBGTask *nbg = cls;
2067 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2070 if (GNUNET_NO == success)
2072 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2073 "GNS_NS: records already in namestore\n");
2076 else if (GNUNET_YES == success)
2078 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2079 "GNS_NS: records successfully put in namestore\n");
2083 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2084 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2089 * Function called when we get a result from the dht
2090 * for our query. Recursively tries to resolve authorities
2093 * @param cls the request handle
2094 * @param exp lifetime
2095 * @param key the key the record was stored under
2096 * @param get_path get path
2097 * @param get_path_length get path length
2098 * @param put_path put path
2099 * @param put_path_length put path length
2100 * @param type the block type
2101 * @param size the size of the record
2102 * @param data the record data
2105 process_delegation_result_dht (void* cls,
2106 struct GNUNET_TIME_Absolute exp,
2107 const struct GNUNET_HashCode * key,
2108 const struct GNUNET_PeerIdentity *get_path,
2109 unsigned int get_path_length,
2110 const struct GNUNET_PeerIdentity *put_path,
2111 unsigned int put_path_length,
2112 enum GNUNET_BLOCK_Type type,
2113 size_t size, const void *data)
2115 struct ResolverHandle *rh = cls;
2116 const struct GNSNameRecordBlock *nrb = data;
2117 const char* rd_data;
2118 uint32_t num_records;
2122 struct GNUNET_CRYPTO_ShortHashCode zone;
2124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2125 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n",
2129 /* stop dht lookup and timeout task */
2130 GNUNET_DHT_get_stop (rh->get_handle);
2131 rh->get_handle = NULL;
2132 if (rh->dht_heap_node != NULL)
2134 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2135 rh->dht_heap_node = NULL;
2138 num_records = ntohl(nrb->rd_count);
2139 name = (const char*) &nrb[1];
2141 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2142 struct NamestoreBGTask *ns_heap_root;
2143 struct NamestoreBGTask *namestore_bg_task;
2145 rd_data = name + strlen(name) + 1;
2146 rd_size = size - strlen(name) - 1 - sizeof (struct GNSNameRecordBlock);
2147 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2152 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2153 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2159 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2160 rh->id, name, rh->authority_name);
2161 for (i=0; i<num_records; i++)
2163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2164 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2165 rh->id, name, rh->authority_name);
2166 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2167 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2168 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2169 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2170 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2171 rh->id, rd[i].data_size);
2172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2173 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2174 rh->id, rd[i].flags);
2176 if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2177 (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2178 (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2181 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2183 if (0 == strcmp(rh->name, ""))
2184 strcpy(rh->name, rh->authority_name);
2186 GNUNET_snprintf(rh->name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
2187 rh->name, rh->authority_name); //FIXME ret
2192 if ((0 == strcmp(name, rh->authority_name)) &&
2193 (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2196 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2199 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2200 struct AuthorityChain *auth =
2201 GNUNET_malloc(sizeof(struct AuthorityChain));
2202 auth->zone = rh->authority;
2203 memset(auth->name, 0, strlen(rh->authority_name)+1);
2204 strcpy(auth->name, rh->authority_name);
2205 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2206 rh->authority_chain_tail,
2209 if (NULL != rh->rd.data)
2210 GNUNET_free ((void*)rh->rd.data);
2212 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2213 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2214 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2217 /** try to import pkey if private key available */
2218 //if (rh->priv_key && is_canonical (rh->name))
2219 // process_discovered_authority(name, auth->zone,
2220 // rh->authority_chain_tail->zone,
2225 GNUNET_GNS_get_zone_from_key (name, key, &zone);
2228 /* Save to namestore
2229 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2232 if (max_allowed_ns_tasks <=
2233 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2235 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2236 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2238 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2239 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background ns task\n",
2242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2243 "GNS_PHASE_DELEGATE_DHT-%llu: Caching record for %s\n",
2245 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2247 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2249 GNUNET_TIME_absolute_get().abs_value);
2250 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2257 &on_namestore_delegation_put_result, //cont
2258 namestore_bg_task); //cls
2262 if (0 != rh->answered)
2267 * FIXME in this case. should we ask namestore again?
2269 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2270 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2271 rh->id, rh->authority_name, rh->name);
2273 if (0 == strcmp(rh->name, ""))
2275 /* Start shortening */
2276 if ((NULL != rh->priv_key) &&
2277 (GNUNET_YES == is_canonical (rh->name)))
2279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2280 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2282 start_shorten (rh->authority_chain_head,
2287 rh->proc = &handle_delegation_ns;
2290 /* Check for key revocation and delegate */
2291 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2294 GNUNET_GNS_RECORD_REV,
2295 &process_pkey_revocation_result_ns,
2302 * No pkey but name exists
2305 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2306 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2307 rh->id, rh->authority_name, rh->name);
2308 if (0 == strcmp(rh->name, ""))
2309 strcpy(rh->name, rh->authority_name);
2311 GNUNET_snprintf(rh->name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
2312 rh->name, rh->authority_name); //FIXME ret
2314 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2315 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2316 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2317 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2319 rh->proc(rh->proc_cls, rh, 0, NULL);
2322 //FIXME maybe define somewhere else?
2323 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2324 +(GNUNET_DNSPARSER_MAX_NAME_LENGTH*2)
2325 #define MAX_MX_LENGTH sizeof(uint16_t)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
2326 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+GNUNET_DNSPARSER_MAX_NAME_LENGTH
2330 * Exands a name ending in .+ with the zone of origin.
2331 * FIXME: funky api: 'dest' must be large enough to hold
2332 * the result; this is a bit yucky...
2334 * @param dest destination buffer
2335 * @param src the .+ name
2336 * @param repl the string to replace the + with
2339 expand_plus (char* dest,
2344 size_t s_len = strlen (src) + 1;
2346 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2349 /* no postprocessing */
2350 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2351 "GNS_POSTPROCESS: %s too short\n", src);
2352 memcpy (dest, src, s_len);
2355 if (0 == strcmp (src + s_len - 3, ".+"))
2357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2358 "GNS_POSTPROCESS: Expanding .+ in %s\n",
2360 memset (dest, 0, s_len + strlen (repl) + strlen(GNUNET_GNS_TLD));
2362 pos = dest + s_len - 2;
2364 pos += strlen (repl);
2365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2366 "GNS_POSTPROCESS: Expanded to %s\n",
2371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2372 "GNS_POSTPROCESS: No postprocessing for %s\n", src);
2373 memcpy (dest, src, s_len);
2382 finish_lookup (struct ResolverHandle *rh,
2383 struct RecordLookupHandle* rlh,
2384 unsigned int rd_count,
2385 const struct GNUNET_NAMESTORE_RecordData *rd)
2388 char new_rr_data[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
2389 char new_mx_data[MAX_MX_LENGTH];
2390 char new_soa_data[MAX_SOA_LENGTH];
2391 char new_srv_data[MAX_SRV_LENGTH];
2392 struct srv_data *old_srv;
2393 struct srv_data *new_srv;
2394 struct soa_data *old_soa;
2395 struct soa_data *new_soa;
2396 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2399 unsigned int offset;
2401 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
2403 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2404 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2407 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2410 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2412 for (i = 0; i < rd_count; i++)
2415 if ((GNUNET_GNS_RECORD_NS != rd[i].record_type) &&
2416 (GNUNET_GNS_RECORD_PTR != rd[i].record_type) &&
2417 (GNUNET_GNS_RECORD_CNAME != rd[i].record_type) &&
2418 (GNUNET_GNS_RECORD_MX != rd[i].record_type) &&
2419 (GNUNET_GNS_RECORD_SOA != rd[i].record_type) &&
2420 (GNUNET_GNS_RECORD_SRV != rd[i].record_type))
2422 p_rd[i].data = rd[i].data;
2427 * for all those records we 'should'
2428 * also try to resolve the A/AAAA records (RFC1035)
2429 * This is a feature and not important
2432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2433 "GNS_POSTPROCESS: Postprocessing\n");
2434 if (0 == strcmp(rh->name, "+"))
2435 repl_string = rlh->name;
2437 repl_string = rlh->name+strlen(rh->name)+1;
2440 if (GNUNET_GNS_RECORD_MX == rd[i].record_type)
2442 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2443 offset = sizeof (uint16_t);
2444 pos = new_mx_data + offset;
2445 // FIXME: how do we know that 'pos' has enough space for the new name?
2446 expand_plus (pos, (char*)rd[i].data+sizeof(uint16_t),
2448 offset += strlen(new_mx_data+sizeof(uint16_t)) + 1;
2449 p_rd[i].data = new_mx_data;
2450 p_rd[i].data_size = offset;
2452 else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2455 * Prio, weight and port
2457 new_srv = (struct srv_data*)new_srv_data;
2458 old_srv = (struct srv_data*)rd[i].data;
2459 new_srv->prio = old_srv->prio;
2460 new_srv->weight = old_srv->weight;
2461 new_srv->port = old_srv->port;
2462 // FIXME: how do we know that '&new_srv[1]' has enough space for the new name?
2463 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2465 p_rd[i].data = new_srv_data;
2466 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2468 else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2470 /* expand mname and rname */
2471 old_soa = (struct soa_data*)rd[i].data;
2472 new_soa = (struct soa_data*)new_soa_data;
2473 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2474 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2475 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2476 offset = strlen ((char*)&new_soa[1]) + 1;
2477 // FIXME: how do we know that 'new_soa[1]' has enough space for the new name?
2478 expand_plus((char*)&new_soa[1] + offset,
2479 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2481 p_rd[i].data_size = sizeof (struct soa_data)
2483 + strlen ((char*)&new_soa[1] + offset);
2484 p_rd[i].data = new_soa_data;
2489 // FIXME: how do we know that 'rd[i].data' has enough space for the new name?
2490 expand_plus(pos, (char*)rd[i].data, repl_string);
2491 p_rd[i].data_size = strlen(new_rr_data)+1;
2492 p_rd[i].data = new_rr_data;
2497 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2499 free_resolver_handle (rh);
2504 * Process DHT lookup result for record.
2506 * @param cls the closure
2507 * @param rh resolver handle
2508 * @param rd_count number of results
2509 * @param rd record data
2512 handle_record_dht (void* cls, struct ResolverHandle *rh,
2513 unsigned int rd_count,
2514 const struct GNUNET_NAMESTORE_RecordData *rd)
2516 struct RecordLookupHandle* rlh = cls;
2520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2521 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2523 /* give up, cannot resolve */
2524 finish_lookup (rh, rlh, 0, NULL);
2527 /* results found yay */
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2529 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2530 finish_lookup (rh, rlh, rd_count, rd);
2535 * Process namestore lookup result for record.
2537 * @param cls the closure
2538 * @param rh resolver handle
2539 * @param rd_count number of results
2540 * @param rd record data
2543 handle_record_ns (void* cls, struct ResolverHandle *rh,
2544 unsigned int rd_count,
2545 const struct GNUNET_NAMESTORE_RecordData *rd)
2547 struct RecordLookupHandle* rlh = cls;
2548 int check_dht = GNUNET_YES;
2552 /* results found yay */
2553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2554 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2555 finish_lookup (rh, rlh, rd_count, rd);
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2560 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2564 * There are 5 conditions that have to met for us to consult the DHT:
2565 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2566 * 2. No entry in the NS existed AND
2567 * 3. The zone queried is not the local resolver's zone AND
2568 * 4. The name that was looked up is '+'
2569 * because if it was any other canonical name we either already queried
2570 * the DHT for the authority in the authority lookup phase (and thus
2571 * would already have an entry in the NS for the record)
2572 * 5. We are not in cache only mode
2574 if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2575 (0 == (rh->status & RSL_RECORD_EXISTS)) )
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2579 "GNS_PHASE_REC-%llu: Not expired and exists!\n",
2581 check_dht = GNUNET_NO;
2584 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2585 &rh->private_local_zone))
2588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2589 "GNS_PHASE_REC-%llu: Our zone!\n",
2591 check_dht = GNUNET_NO;
2594 if ((0 != strcmp (rh->name, "+")) && (GNUNET_YES == is_srv (rh->name)))
2595 check_dht = GNUNET_NO;
2597 if (GNUNET_YES == rh->only_cached)
2598 check_dht = GNUNET_NO;
2600 if (GNUNET_YES == check_dht)
2602 rh->proc = &handle_record_dht;
2603 resolve_record_dht(rh);
2606 /* give up, cannot resolve */
2607 finish_lookup (rh, rlh, 0, NULL);
2612 * Move one level up in the domain hierarchy and return the
2613 * passed top level domain.
2615 * FIXME: funky API: not only 'dest' is updated, so is 'name'!
2617 * @param name the domain
2618 * @param dest the destination where the tld will be put
2621 pop_tld (char* name, char* dest)
2625 if (GNUNET_YES == is_canonical (name))
2627 strcpy (dest, name);
2632 for (len = strlen(name); 0 < len; len--)
2634 if (*(name+len) == '.')
2644 strcpy(dest, (name+len+1));
2649 * DHT resolution for delegation finished. Processing result.
2651 * @param cls the closure
2652 * @param rh resolver handle
2653 * @param rd_count number of results (always 0)
2654 * @param rd record data (always NULL)
2657 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2658 unsigned int rd_count,
2659 const struct GNUNET_NAMESTORE_RecordData *rd)
2661 struct RecordLookupHandle* rlh = cls;
2663 if (0 == strcmp(rh->name, ""))
2665 if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2667 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2668 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2670 finish_lookup(rh, rlh, rd_count, rd);
2673 /* We resolved full name for delegation. resolving record */
2674 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2675 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2677 strcpy(rh->name, "+\0");
2678 rh->proc = &handle_record_ns;
2679 resolve_record_ns(rh);
2684 * we still have some left
2686 if (GNUNET_YES == is_canonical (rh->name))
2688 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2689 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2692 rh->proc = &handle_record_ns;
2693 resolve_record_ns(rh);
2696 /* give up, cannot resolve */
2697 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2698 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2700 finish_lookup(rh, rlh, 0, NULL);
2705 * Start DHT lookup for a name -> PKEY (compare NS) record in
2706 * rh->authority's zone
2708 * @param rh the pending gns query
2711 resolve_delegation_dht (struct ResolverHandle *rh)
2714 struct GNUNET_HashCode lookup_key;
2715 struct ResolverHandle *rh_heap_root;
2717 pop_tld (rh->name, rh->authority_name);
2718 GNUNET_GNS_get_key_for_record (rh->authority_name,
2721 rh->dht_heap_node = NULL;
2722 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2724 rh->timeout_cont = &dht_authority_lookup_timeout;
2725 rh->timeout_cont_cls = rh;
2729 if (max_allowed_background_queries <=
2730 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2732 /* terminate oldest lookup */
2733 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2734 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2735 rh_heap_root->get_handle = NULL;
2736 rh_heap_root->dht_heap_node = NULL;
2737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2738 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2740 rh_heap_root->authority_name);
2741 rh_heap_root->proc (rh_heap_root->proc_cls,
2746 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2748 GNUNET_TIME_absolute_get().abs_value);
2750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2751 "Beginning DHT lookup for %s in zone %s for request %llu\n",
2753 GNUNET_short_h2s (&rh->authority),
2755 xquery = htonl (GNUNET_GNS_RECORD_PKEY);
2756 GNUNET_assert (rh->get_handle == NULL);
2757 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
2758 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2760 DHT_GNS_REPLICATION_LEVEL,
2761 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2764 &process_delegation_result_dht,
2770 * Checks if name is in tld
2772 * @param name the name to check
2773 * @param tld the TLD to check for
2774 * @return GNUNET_YES or GNUNET_NO
2777 is_tld (const char* name, const char* tld)
2781 if (strlen (name) <= strlen (tld))
2784 offset = strlen (name) - strlen (tld);
2785 if (0 != strcmp (name + offset, tld))
2787 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2788 "%s is not in .%s TLD\n", name, tld);
2796 * Namestore resolution for delegation finished. Processing result.
2798 * @param cls the closure
2799 * @param rh resolver handle
2800 * @param rd_count number of results (always 0)
2801 * @param rd record data (always NULL)
2804 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2805 unsigned int rd_count,
2806 const struct GNUNET_NAMESTORE_RecordData *rd)
2808 struct RecordLookupHandle* rlh = cls;
2812 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2813 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2814 rh->id, rh->status);
2816 if (rh->status & RSL_PKEY_REVOKED)
2818 finish_lookup (rh, rlh, 0, NULL);
2822 if (0 == strcmp(rh->name, ""))
2825 /* We resolved full name for delegation. resolving record */
2826 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2827 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2829 if (rh->status & RSL_CNAME_FOUND)
2831 if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2833 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2834 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2836 strcpy (rh->name, rh->authority_name);
2837 finish_lookup (rh, rlh, rd_count, rd);
2842 if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2844 s_len = strlen (rd->data) - 2;
2845 memcpy (rh->name, rd->data, s_len);
2846 rh->name[s_len] = '\0';
2847 resolve_delegation_ns (rh);
2850 else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2852 gns_resolver_lookup_record (rh->authority,
2853 rh->private_local_zone,
2862 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2863 free_resolver_handle (rh);
2869 strcpy (rh->dns_name, (char*)rd->data);
2870 resolve_dns_name (rh);
2875 else if (rh->status & RSL_DELEGATE_VPN)
2877 if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2879 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2880 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2882 finish_lookup(rh, rlh, rd_count, rd);
2885 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2886 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2888 GNUNET_assert (NULL != rd);
2889 rh->proc = &handle_record_vpn;
2890 resolve_record_vpn (rh, rd_count, rd);
2893 else if (rh->status & RSL_DELEGATE_NS)
2895 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2897 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2898 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2900 finish_lookup(rh, rlh, rd_count, rd);
2904 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2905 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2907 GNUNET_assert (NULL != rd);
2908 rh->proc = &handle_record_ns;
2909 resolve_record_dns (rh, rd_count, rd);
2912 else if (rh->status & RSL_DELEGATE_PKEY)
2914 if (rh->status & RSL_PKEY_REVOKED)
2916 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2917 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
2919 finish_lookup (rh, rlh, 0, NULL);
2922 else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2924 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2925 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
2927 finish_lookup(rh, rlh, rd_count, rd);
2931 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2932 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
2934 strcpy(rh->name, "+\0");
2935 rh->proc = &handle_record_ns;
2936 resolve_record_ns(rh);
2940 if (rh->status & RSL_DELEGATE_NS)
2942 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2944 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2945 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2947 finish_lookup(rh, rlh, rd_count, rd);
2951 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2952 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2954 GNUNET_assert (NULL != rd);
2955 rh->proc = &handle_record_ns;
2956 resolve_record_dns (rh, rd_count, rd);
2961 * we still have some left
2962 * check if authority in ns is fresh
2964 * or we are authority
2967 check_dht = GNUNET_YES;
2968 if ((rh->status & RSL_RECORD_EXISTS) &&
2969 !(rh->status & RSL_RECORD_EXPIRED))
2970 check_dht = GNUNET_NO;
2972 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2973 &rh->private_local_zone))
2974 check_dht = GNUNET_NO;
2976 if (GNUNET_YES == rh->only_cached)
2977 check_dht = GNUNET_NO;
2979 if (GNUNET_YES == check_dht)
2982 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2983 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
2985 rh->proc = &handle_delegation_dht;
2986 resolve_delegation_dht(rh);
2990 if (GNUNET_NO == is_canonical (rh->name))
2992 /* give up, cannot resolve */
2993 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2994 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
2997 finish_lookup(rh, rlh, rd_count, rd);
3000 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3001 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3004 rh->proc = &handle_record_ns;
3005 resolve_record_ns(rh);
3010 * This is a callback function that should give us only PKEY
3011 * records. Used to query the namestore for the authority (PKEY)
3012 * for 'name'. It will recursively try to resolve the
3013 * authority for a given name from the namestore.
3015 * @param cls the pending query
3016 * @param key the key of the zone we did the lookup
3017 * @param expiration expiration date of the record data set in the namestore
3018 * @param name the name for which we need an authority
3019 * @param rd_count the number of records with 'name'
3020 * @param rd the record data
3021 * @param signature the signature of the authority for the record data
3024 process_delegation_result_ns (void* cls,
3025 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3026 struct GNUNET_TIME_Absolute expiration,
3028 unsigned int rd_count,
3029 const struct GNUNET_NAMESTORE_RecordData *rd,
3030 const struct GNUNET_CRYPTO_RsaSignature *signature)
3032 struct ResolverHandle *rh = cls;
3033 struct GNUNET_TIME_Relative remaining_time;
3034 struct GNUNET_CRYPTO_ShortHashCode zone;
3035 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3037 struct GNUNET_TIME_Absolute et;
3039 rh->namestore_task = NULL;
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3041 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3044 GNUNET_CRYPTO_short_hash (key,
3045 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3047 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3054 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3056 rh->status |= RSL_RECORD_EXISTS;
3058 if (remaining_time.rel_value == 0)
3060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3061 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3063 rh->status |= RSL_RECORD_EXPIRED;
3068 * No authority found in namestore.
3073 * We did not find an authority in the namestore
3078 * Promote this authority back to a name maybe it is
3081 if (strcmp (rh->name, "") == 0)
3083 /* simply promote back */
3084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3085 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3086 rh->id, rh->authority_name);
3087 strcpy (rh->name, rh->authority_name);
3091 /* add back to existing name */
3092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3093 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3094 rh->id, rh->authority_name, rh->name);
3095 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
3096 rh->name, rh->authority_name);
3097 strcpy (rh->name, new_name);
3098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3099 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3103 rh->proc (rh->proc_cls, rh, 0, NULL);
3108 * We found an authority that may be able to help us
3109 * move on with query
3110 * Note only 1 pkey should have been returned.. anything else would be strange
3112 for (i=0; i < rd_count;i++)
3116 * A CNAME. Like regular DNS this means the is no other record for this
3119 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3121 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3122 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3125 rh->status |= RSL_CNAME_FOUND;
3126 rh->proc (rh->proc_cls, rh, rd_count, rd);
3133 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3135 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3136 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3138 rh->status |= RSL_DELEGATE_VPN;
3139 rh->proc (rh->proc_cls, rh, rd_count, rd);
3145 * FIXME make optional
3147 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3149 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3150 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3152 rh->status |= RSL_DELEGATE_NS;
3153 rh->proc (rh->proc_cls, rh, rd_count, rd);
3157 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3160 rh->status |= RSL_DELEGATE_PKEY;
3162 if ((ignore_pending_records != 0) &&
3163 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3166 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3172 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3173 et.abs_value = rd[i].expiration_time;
3174 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3178 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3180 if (remaining_time.rel_value == 0)
3182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3183 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3185 rh->authority_chain_head->fresh = 0;
3186 rh->proc (rh->proc_cls, rh, 0, NULL);
3194 * Resolve rest of query with new authority
3196 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3197 memcpy (&rh->authority, rd[i].data,
3198 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3199 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3200 auth->zone = rh->authority;
3201 memset (auth->name, 0, strlen (rh->authority_name)+1);
3202 strcpy (auth->name, rh->authority_name);
3203 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3204 rh->authority_chain_tail,
3206 if (NULL != rh->rd.data)
3207 GNUNET_free ((void*)(rh->rd.data));
3209 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3210 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3211 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3213 /* Check for key revocation and delegate */
3214 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3217 GNUNET_GNS_RECORD_REV,
3218 &process_pkey_revocation_result_ns,
3226 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3227 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3229 * If we have found some records for the LAST label
3230 * we return the results. Else null.
3232 if (strcmp (rh->name, "") == 0)
3234 /* Start shortening */
3235 if ((rh->priv_key != NULL) &&
3236 (is_canonical (rh->name) == GNUNET_YES))
3238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3239 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3241 start_shorten (rh->authority_chain_head,
3244 /* simply promote back */
3245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3246 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3247 rh->id, rh->authority_name);
3248 strcpy (rh->name, rh->authority_name);
3249 rh->proc (rh->proc_cls, rh, rd_count, rd);
3253 GNUNET_snprintf (new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3254 "%s.%s", rh->name, rh->authority_name);
3255 strcpy (rh->name, new_name);
3256 rh->proc (rh->proc_cls, rh, 0, NULL);
3262 * Resolve the delegation chain for the request in our namestore
3264 * @param rh the resolver handle
3267 resolve_delegation_ns (struct ResolverHandle *rh)
3269 pop_tld (rh->name, rh->authority_name);
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3271 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s by looking up %s\n",
3273 rh->authority_name);
3274 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3277 GNUNET_GNS_RECORD_ANY,
3278 &process_delegation_result_ns,
3284 * Lookup of a record in a specific zone
3285 * calls lookup result processor on result
3287 * @param zone the root zone
3288 * @param pzone the private local zone
3289 * @param record_type the record type to look up
3290 * @param name the name to look up
3291 * @param key a private key for use with PSEU import (can be NULL)
3292 * @param timeout timeout for resolution
3293 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3294 * @param proc the processor to call on result
3295 * @param cls the closure to pass to proc
3298 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3299 struct GNUNET_CRYPTO_ShortHashCode pzone,
3300 uint32_t record_type,
3302 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3303 struct GNUNET_TIME_Relative timeout,
3305 RecordLookupProcessor proc,
3308 struct ResolverHandle *rh;
3309 struct RecordLookupHandle* rlh;
3310 char string_hash[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
3311 char nzkey[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
3312 char* nzkey_ptr = nzkey;
3314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3315 "Starting resolution for %s (type=%d) with timeout %s!\n",
3317 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
3319 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3320 (strcmp(GNUNET_GNS_TLD, name) != 0))
3322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3323 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3328 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3329 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3330 rh->authority = zone;
3334 rh->timeout = timeout;
3335 rh->private_local_zone = pzone;
3336 rh->only_cached = only_cached;
3338 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3342 "No shorten key for resolution\n");
3344 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3347 * Set timeout for authority lookup phase to 1/2
3349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3350 "Timeout for lookup set to %s/2\n",
3351 GNUNET_STRINGS_relative_time_to_string (rh->timeout, GNUNET_YES));
3352 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
3353 GNUNET_TIME_relative_divide(timeout, 2),
3354 &handle_lookup_timeout,
3356 rh->timeout_cont = &dht_authority_lookup_timeout;
3357 rh->timeout_cont_cls = rh;
3361 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3362 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3365 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3368 * Only 'gnunet' given
3370 strcpy(rh->name, "\0");
3374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3375 "Checking for TLD...\n");
3376 if (is_zkey_tld(name) == GNUNET_YES)
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3381 * This is a zkey tld
3382 * build hash and use as initial authority
3385 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3386 memcpy(rh->name, name,
3387 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3388 pop_tld(rh->name, string_hash);
3390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3391 "ZKEY is %s!\n", string_hash);
3393 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3395 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3398 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3399 "Cannot convert ZKEY `%s' to hash!\n", string_hash);
3401 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3402 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3403 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3406 proc (cls, 0, NULL);
3411 else if (is_gads_tld (name) == GNUNET_YES)
3413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3416 * Presumably GADS tld
3418 memcpy (rh->name, name,
3419 strlen (name) - strlen(GNUNET_GNS_TLD) - 1);
3420 rh->name[strlen (name) - strlen(GNUNET_GNS_TLD) - 1] = '\0';
3424 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3425 "Cannot handle this TLD %s\n", string_hash);
3427 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3428 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3429 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3432 proc (cls, 0, NULL);
3438 * Initialize authority chain
3440 rh->authority_chain_head = GNUNET_malloc (sizeof(struct AuthorityChain));
3441 rh->authority_chain_tail = rh->authority_chain_head;
3442 rh->authority_chain_head->zone = rh->authority;
3443 strcpy (rh->authority_chain_head->name, "");
3446 * Copy original query into lookup handle
3448 rlh->record_type = record_type;
3449 memset(rlh->name, 0, strlen(name) + 1);
3450 strcpy(rlh->name, name);
3452 rlh->proc_cls = cls;
3454 rh->proc = &handle_delegation_ns;
3455 resolve_delegation_ns(rh);
3458 /******** END Record Resolver ***********/
3461 finish_shorten (struct ResolverHandle *rh,
3462 struct NameShortenHandle *nsh)
3464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3465 "Sending %s as shorten result\n", nsh->result);
3466 nsh->proc (nsh->proc_cls, nsh->result);
3467 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3469 free_resolver_handle (rh);
3474 * Callback calles by namestore for a zone to name
3477 * @param cls the closure
3478 * @param zone_key the zone we queried
3479 * @param expire the expiration time of the name
3480 * @param name the name found or NULL
3481 * @param rd_len number of records for the name
3482 * @param rd the record data (PKEY) for the name
3483 * @param signature the signature for the record data
3486 process_zone_to_name_shorten_root (void *cls,
3487 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3488 struct GNUNET_TIME_Absolute expire,
3490 unsigned int rd_len,
3491 const struct GNUNET_NAMESTORE_RecordData *rd,
3492 const struct GNUNET_CRYPTO_RsaSignature *signature);
3496 * Callback called by namestore for a zone to name
3499 * @param cls the closure
3500 * @param zone_key the zone we queried
3501 * @param expire the expiration time of the name
3502 * @param name the name found or NULL
3503 * @param rd_len number of records for the name
3504 * @param rd the record data (PKEY) for the name
3505 * @param signature the signature for the record data
3508 process_zone_to_name_shorten_shorten (void *cls,
3509 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3510 struct GNUNET_TIME_Absolute expire,
3512 unsigned int rd_len,
3513 const struct GNUNET_NAMESTORE_RecordData *rd,
3514 const struct GNUNET_CRYPTO_RsaSignature *signature)
3516 struct ResolverHandle *rh = cls;
3517 struct NameShortenHandle* nsh = rh->proc_cls;
3518 struct AuthorityChain *next_authority;
3520 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3521 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3524 rh->namestore_task = NULL;
3525 /* we found a match in our own root zone */
3528 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3529 memset(result, 0, answer_len);
3531 if (strlen(rh->name) > 0)
3533 sprintf (result, "%s.%s.%s.%s",
3535 nsh->shorten_zone_name,
3540 sprintf (result, "%s.%s.%s", name,
3541 nsh->shorten_zone_name,
3545 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3546 "Found shorten result %s\n", result);
3547 if (strlen (nsh->result) > strlen (result))
3548 strcpy (nsh->result, result);
3550 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3551 nsh->shorten_zone) == 0)
3554 * This is our zone append .gads unless name is empty
3555 * (it shouldn't be, usually FIXME what happens if we
3556 * shorten to our zone to a "" record??)
3559 sprintf (result, "%s.%s.%s",
3561 nsh->shorten_zone_name,
3563 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3564 "Our zone: Found %s as shorten result\n", result);
3566 if (strlen (nsh->result) > strlen (result))
3567 strcpy (nsh->result, result);
3568 //nsh->proc(nsh->proc_cls, result);
3570 //free_resolver_handle(rh);
3577 * continue with next authority if exists
3579 if (NULL == rh->authority_chain_head->next)
3581 finish_shorten (rh, nsh);
3584 next_authority = rh->authority_chain_head;
3586 if (0 == strcmp (rh->name, ""))
3587 strcpy (tmp_name, next_authority->name);
3589 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3590 "%s.%s", rh->name, next_authority->name);
3592 strcpy(rh->name, tmp_name);
3593 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3594 "No PSEU found for authority %s. Promoting back: %s\n",
3595 next_authority->name, rh->name);
3597 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3598 rh->authority_chain_tail,
3601 GNUNET_free (next_authority);
3603 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3604 &rh->authority_chain_tail->zone,
3605 &rh->authority_chain_head->zone,
3606 &process_zone_to_name_shorten_root,
3612 * Callback calles by namestore for a zone to name
3615 * @param cls the closure
3616 * @param zone_key the zone we queried
3617 * @param expire the expiration time of the name
3618 * @param name the name found or NULL
3619 * @param rd_len number of records for the name
3620 * @param rd the record data (PKEY) for the name
3621 * @param signature the signature for the record data
3624 process_zone_to_name_shorten_private (void *cls,
3625 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3626 struct GNUNET_TIME_Absolute expire,
3628 unsigned int rd_len,
3629 const struct GNUNET_NAMESTORE_RecordData *rd,
3630 const struct GNUNET_CRYPTO_RsaSignature *signature)
3632 struct ResolverHandle *rh = cls;
3633 struct NameShortenHandle* nsh = rh->proc_cls;
3634 struct AuthorityChain *next_authority;
3636 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3637 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3640 rh->namestore_task = NULL;
3641 /* we found a match in our own root zone */
3644 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3645 memset(result, 0, answer_len);
3647 if (strlen(rh->name) > 0)
3649 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3653 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3656 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3657 "Found shorten result %s\n", result);
3658 if (strlen (nsh->result) > strlen (result))
3659 strcpy (nsh->result, result);
3661 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3662 nsh->private_zone) == 0)
3665 * This is our zone append .gads unless name is empty
3666 * (it shouldn't be, usually FIXME what happens if we
3667 * shorten to our zone to a "" record??)
3670 sprintf (result, "%s.%s.%s",
3671 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3672 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3673 "Our private zone: Found %s as shorten result %s\n", result);
3674 if (strlen (nsh->result) > strlen (result))
3675 strcpy (nsh->result, result);
3678 if (0 != strcmp (nsh->shorten_zone_name, ""))
3680 /* backtrack authorities for names in priv zone */
3681 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3683 &rh->authority_chain_head->zone,
3684 &process_zone_to_name_shorten_shorten,
3691 * continue with next authority if exists
3693 if (NULL == rh->authority_chain_head->next)
3695 finish_shorten (rh, nsh);
3698 next_authority = rh->authority_chain_head;
3700 if (0 == strcmp (rh->name, ""))
3701 strcpy (tmp_name, next_authority->name);
3703 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3704 "%s.%s", rh->name, next_authority->name);
3706 strcpy(rh->name, tmp_name);
3707 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3708 "No PSEU found for authority %s. Promoting back: %s\n",
3709 next_authority->name, rh->name);
3711 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3712 rh->authority_chain_tail,
3715 GNUNET_free (next_authority);
3717 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3718 &rh->authority_chain_tail->zone,
3719 &rh->authority_chain_head->zone,
3720 &process_zone_to_name_shorten_root,
3727 * Callback calles by namestore for a zone to name
3730 * @param cls the closure
3731 * @param zone_key the zone we queried
3732 * @param expire the expiration time of the name
3733 * @param name the name found or NULL
3734 * @param rd_len number of records for the name
3735 * @param rd the record data (PKEY) for the name
3736 * @param signature the signature for the record data
3739 process_zone_to_name_shorten_root (void *cls,
3740 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3741 struct GNUNET_TIME_Absolute expire,
3743 unsigned int rd_len,
3744 const struct GNUNET_NAMESTORE_RecordData *rd,
3745 const struct GNUNET_CRYPTO_RsaSignature *signature)
3747 struct ResolverHandle *rh = cls;
3748 struct NameShortenHandle* nsh = rh->proc_cls;
3749 struct AuthorityChain *next_authority;
3750 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3751 char tmp_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3754 rh->namestore_task = NULL;
3755 /* we found a match in our own root zone */
3758 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3759 memset(result, 0, answer_len);
3761 if (strlen(rh->name) > 0)
3763 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3767 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3770 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3771 "Found shorten result %s\n", result);
3772 if (strlen (nsh->result) > strlen (result))
3773 strcpy (nsh->result, result);
3775 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3776 nsh->root_zone) == 0)
3779 * This is our zone append .gads unless name is empty
3780 * (it shouldn't be, usually FIXME what happens if we
3781 * shorten to our zone to a "" record??)
3784 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3785 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3786 "Our zone: Found %s as shorten result\n", result);
3787 if (strlen (nsh->result) > strlen (result))
3788 strcpy (nsh->result, result);
3791 if (NULL != nsh->private_zone)
3793 /* backtrack authorities for names in priv zone */
3794 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3796 &rh->authority_chain_head->zone,
3797 &process_zone_to_name_shorten_private,
3800 else if (NULL != nsh->shorten_zone)
3802 /* backtrack authorities for names in shorten zone */
3803 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3805 &rh->authority_chain_head->zone,
3806 &process_zone_to_name_shorten_shorten,
3813 * continue with next authority if exists
3815 if (NULL == rh->authority_chain_head->next)
3817 finish_shorten (rh, nsh);
3820 next_authority = rh->authority_chain_head;
3822 if (0 == strcmp (rh->name, ""))
3823 strcpy (tmp_name, next_authority->name);
3825 GNUNET_snprintf(tmp_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH,
3826 "%s.%s", rh->name, next_authority->name);
3828 strcpy(rh->name, tmp_name);
3829 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3830 "No PSEU found for authority %s. Promoting back: %s\n",
3831 next_authority->name, rh->name);
3833 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3834 rh->authority_chain_tail,
3837 GNUNET_free (next_authority);
3839 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3840 &rh->authority_chain_tail->zone,
3841 &rh->authority_chain_head->zone,
3842 &process_zone_to_name_shorten_root,
3849 * Process result from namestore delegation lookup
3850 * for shorten operation
3852 * @param cls the client shorten handle
3853 * @param rh the resolver handle
3854 * @param rd_count number of results (0)
3855 * @param rd data (NULL)
3858 handle_delegation_ns_shorten (void* cls,
3859 struct ResolverHandle *rh,
3861 const struct GNUNET_NAMESTORE_RecordData *rd)
3863 struct NameShortenHandle *nsh;
3864 char result[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3866 nsh = (struct NameShortenHandle *)cls;
3867 rh->namestore_task = NULL;
3869 * At this point rh->name contains the part of the name
3870 * that we do not have a PKEY in our namestore to resolve.
3871 * The authority chain in the resolver handle is now
3872 * useful to backtrack if needed
3875 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3876 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3877 memset(result, 0, sizeof (result));
3879 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3883 * This is our zone append .gads unless name is empty
3884 * (it shouldn't be, usually FIXME what happens if we
3885 * shorten to our zone to a "" record??)
3888 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3889 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3890 "Our zone: Found %s as shorten result\n", result);
3892 if (strlen (nsh->result) > strlen (result))
3893 strcpy (nsh->result, result);
3896 else if (NULL != nsh->private_zone)
3899 * This is our zone append .gads unless name is empty
3900 * (it shouldn't be, usually FIXME what happens if we
3901 * shorten to our zone to a "" record??)
3903 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3904 nsh->private_zone) == 0)
3907 sprintf (result, "%s.%s.%s",
3908 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3909 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3910 "Our zone: Found %s as shorten result in private zone %s\n",
3913 if (strlen (nsh->result) > strlen (result))
3914 strcpy (nsh->result, result);
3917 else if (NULL != nsh->shorten_zone)
3920 * This is our zone append .gads unless name is empty
3921 * (it shouldn't be, usually FIXME what happens if we
3922 * shorten to our zone to a "" record??)
3924 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3925 nsh->shorten_zone) == 0)
3927 sprintf (result, "%s.%s.%s",
3928 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3929 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3930 "Our zone: Found %s as shorten result in shorten zone\n",
3933 if (strlen (nsh->result) > strlen (result))
3934 strcpy (nsh->result, result);
3939 /* backtrack authorities for names */
3940 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3942 &rh->authority_chain_head->zone,
3943 &process_zone_to_name_shorten_root,
3950 * Callback calles by namestore for a zone to name
3953 * @param cls the closure
3954 * @param zone_key the zone we queried
3955 * @param expire the expiration time of the name
3956 * @param name the name found or NULL
3957 * @param rd_len number of records for the name
3958 * @param rd the record data (PKEY) for the name
3959 * @param signature the signature for the record data
3962 process_zone_to_name_zkey(void *cls,
3963 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3964 struct GNUNET_TIME_Absolute expire,
3966 unsigned int rd_len,
3967 const struct GNUNET_NAMESTORE_RecordData *rd,
3968 const struct GNUNET_CRYPTO_RsaSignature *signature)
3970 struct ResolverHandle *rh = cls;
3971 struct NameShortenHandle *nsh = rh->proc_cls;
3972 char new_name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
3974 rh->namestore_task = NULL;
3976 /* zkey not in our zone */
3980 * In this case we have not given this PKEY a name (yet)
3981 * It is either just not in our zone or not even cached
3982 * Since we do not know at this point we will not try to shorten
3983 * because PKEY import will happen if the user follows the zkey
3986 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3987 "No name found for zkey %s returning verbatim!\n", nsh->result);
3988 /*if (strcmp(rh->name, "") != 0)
3989 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s.%s",
3990 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
3992 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
3993 enc, GNUNET_GNS_TLD_ZKEY);
3995 strcpy (nsh->result, new_name);*/
3997 finish_shorten (rh, nsh);
4001 if (strcmp(rh->name, "") != 0)
4002 GNUNET_snprintf(new_name, GNUNET_DNSPARSER_MAX_NAME_LENGTH, "%s.%s",
4005 strcpy(new_name, name);
4007 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4008 "Continue shorten for %s!\n", new_name);
4010 strcpy(rh->name, new_name);
4012 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4013 rh->authority_chain_tail = rh->authority_chain_head;
4014 rh->authority_chain_head->zone = rh->authority;
4017 /* Start delegation resolution in our namestore */
4018 resolve_delegation_ns (rh);
4023 * Shorten api from resolver
4025 * @param zone the root zone to use
4026 * @param pzone the private zone to use
4027 * @param szone the shorten zone to use
4028 * @param name the name to shorten
4029 * @param private_zone_name name of the private zone
4030 * @param shorten_zone_name name of the shorten zone
4031 * @param proc the processor to call with result
4032 * @param proc_cls closure to pass to proc
4035 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4036 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4037 struct GNUNET_CRYPTO_ShortHashCode *szone,
4039 const char* private_zone_name,
4040 const char* shorten_zone_name,
4041 ShortenResultProcessor proc,
4044 struct ResolverHandle *rh;
4045 struct NameShortenHandle *nsh;
4046 char string_hash[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
4047 struct GNUNET_CRYPTO_ShortHashCode zkey;
4048 char nzkey[GNUNET_DNSPARSER_MAX_LABEL_LENGTH];
4049 char* nzkey_ptr = nzkey;
4051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4052 "Starting shorten for %s!\n", name);
4054 if (is_canonical ((char*)name) == GNUNET_YES)
4056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4057 "%s is canonical. Returning verbatim\n", name);
4058 proc (proc_cls, name);
4062 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4064 nsh->proc_cls = proc_cls;
4065 nsh->root_zone = zone;
4066 nsh->private_zone = pzone;
4067 nsh->shorten_zone = szone;
4068 strcpy (nsh->private_zone_name, private_zone_name);
4069 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4070 strcpy (nsh->result, name);
4072 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4073 rh->authority = *zone;
4075 rh->proc = &handle_delegation_ns_shorten;
4077 rh->private_local_zone = *zone;
4079 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4081 "Checking for TLD...\n");
4082 if (is_zkey_tld (name) == GNUNET_YES)
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4087 * This is a zkey tld
4088 * build hash and use as initial authority
4091 memset (rh->name, 0,
4092 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4093 memcpy (rh->name, name,
4094 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4095 pop_tld (rh->name, string_hash);
4097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4098 "ZKEY is %s!\n", string_hash);
4100 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4102 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4106 "Cannot convert ZKEY %s to hash!\n", nzkey);
4107 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4110 proc (proc_cls, name);
4113 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4116 &process_zone_to_name_zkey,
4121 else if (is_gads_tld (name) == GNUNET_YES)
4123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4126 * Presumably GNUNET tld
4128 memset (rh->name, 0,
4129 strlen (name)-strlen (GNUNET_GNS_TLD));
4130 memcpy (rh->name, name,
4131 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4136 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4139 proc (proc_cls, name);
4143 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4144 rh->authority_chain_tail = rh->authority_chain_head;
4145 rh->authority_chain_head->zone = *zone;
4147 /* Start delegation resolution in our namestore */
4148 resolve_delegation_ns (rh);
4151 /*********** END NAME SHORTEN ********************/
4154 * Conclude get authority lookup
4156 * @param rh resolver handle
4157 * @param nah get authority lookup handle
4160 finish_get_auth (struct ResolverHandle *rh,
4161 struct GetNameAuthorityHandle *nah)
4163 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4164 "Got authority result %s\n", nah->result);
4166 nah->proc (nah->proc_cls, nah->result);
4167 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4169 free_resolver_handle (rh);
4174 * Process result from namestore delegation lookup
4175 * for get authority operation
4177 * @param cls the client get auth handle
4178 * @param rh the resolver handle
4179 * @param rd_count number of results (0)
4180 * @param rd data (NULL)
4183 handle_delegation_result_ns_get_auth(void* cls,
4184 struct ResolverHandle *rh,
4186 const struct GNUNET_NAMESTORE_RecordData *rd)
4188 struct GetNameAuthorityHandle* nah = rh->proc_cls;
4192 * At this point rh->name contains the part of the name
4193 * that we do not have a PKEY in our namestore to resolve.
4194 * The authority chain in the resolver handle is now
4195 * useful to backtrack if needed
4198 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4199 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4201 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4202 "Building response!\n");
4203 if (is_canonical (rh->name) == GNUNET_YES)
4206 * We successfully resolved the authority in the ns
4207 * FIXME for our purposes this is fine
4208 * but maybe we want to have an api that also looks
4209 * into the dht (i.e. option in message)
4211 if (strlen(rh->name) > strlen(nah->name))
4213 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4214 "Record name longer than original lookup name... odd!\n");
4218 answer_len = strlen(nah->name) - strlen(rh->name)
4219 + strlen(GNUNET_GNS_TLD) + 1;
4220 memset(nah->result, 0, answer_len);
4221 if (0 != strcmp (rh->name, ""))
4222 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4224 strcpy(nah->result, nah->name);
4226 finish_get_auth (rh, nah);
4230 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4231 "Unable to resolve authority for remaining %s!\n", rh->name);
4232 strcpy(nah->result, "");
4233 finish_get_auth (rh, nah);
4239 * Tries to resolve the authority for name
4242 * @param zone the root zone to look up for
4243 * @param pzone the private local zone
4244 * @param name the name to lookup up
4245 * @param proc the processor to call when finished
4246 * @param proc_cls the closure to pass to the processor
4249 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4250 struct GNUNET_CRYPTO_ShortHashCode pzone,
4252 GetAuthorityResultProcessor proc,
4255 struct ResolverHandle *rh;
4256 struct GetNameAuthorityHandle *nah;
4258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4259 "Starting authority resolution for %s!\n", name);
4261 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4262 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4263 rh->authority = zone;
4265 rh->private_local_zone = pzone;
4267 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4269 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4271 strcpy(rh->name, "\0");
4276 strlen(name)-strlen(GNUNET_GNS_TLD));
4277 memcpy(rh->name, name,
4278 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4281 memset(nah->name, 0,
4283 strcpy(nah->name, name);
4285 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4286 rh->authority_chain_tail = rh->authority_chain_head;
4287 rh->authority_chain_head->zone = zone;
4288 rh->proc = &handle_delegation_result_ns_get_auth;
4289 rh->proc_cls = (void*)nah;
4292 nah->proc_cls = proc_cls;
4293 strcpy (nah->result, "");
4295 /* Start delegation resolution in our namestore */
4296 resolve_delegation_ns(rh);
4300 /******** END GET AUTHORITY *************/
4302 /* end of gnunet-service-gns_resolver.c */