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"
43 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
44 #define DHT_GNS_REPLICATION_LEVEL 5
45 #define MAX_DNS_LABEL_LENGTH 63
49 * Our handle to the namestore service
51 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
54 * Our handle to the vpn service
56 static struct GNUNET_VPN_Handle *vpn_handle;
59 * Resolver handle to the dht
61 static struct GNUNET_DHT_Handle *dht_handle;
64 * Heap for parallel DHT lookups
66 static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
69 * Heap for namestore queues
71 static struct GNUNET_CONTAINER_Heap *ns_task_heap;
74 * Maximum amount of parallel queries in background
76 static unsigned long long max_allowed_background_queries;
79 * Maximum amount of parallel namestore tasks in background
81 static unsigned long long max_allowed_ns_tasks;
84 * Wheather or not to ignore pending records
86 static int ignore_pending_records;
91 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
94 * Background shortening handles
96 static struct GetPseuAuthorityHandle *gph_head;
99 * Background shortening handles
101 static struct GetPseuAuthorityHandle *gph_tail;
104 * Resolver lookup list
106 static struct ResolverHandle *rlh_head;
109 * Resolver lookup list
111 static struct ResolverHandle *rlh_tail;
114 * Resolver shorten list
116 static struct ResolverHandle *nsh_head;
119 * Resolver shorten list
121 static struct ResolverHandle *nsh_tail;
124 * Resolver get auth list
126 static struct ResolverHandle *nah_head;
129 * Resolver get auth list
131 static struct ResolverHandle *nah_tail;
134 * Global configuration.
136 static const struct GNUNET_CONFIGURATION_Handle *cfg;
139 * a resolution identifier pool variable
141 * This is a non critical identifier useful for debugging
143 static unsigned long long rid = 0;
146 * Check if name is in srv format (_x._y.xxx)
149 * @return GNUNET_YES if true
155 int ret = GNUNET_YES;
159 if (NULL == strstr (name, "._"))
162 ndup = GNUNET_strdup (name);
165 if (NULL == strtok (NULL, "."))
168 if (NULL == strtok (NULL, "."))
171 if (NULL != strtok (NULL, "."))
180 * Determine if this name is canonical (is a legal name in a zone, without delegation);
181 * note that we do not test that the name does not contain illegal characters, we only
182 * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
183 * even though they consist of multiple labels.
186 * a.b.gads = not canonical
188 * _foo._srv = canonical
189 * _f.bar = not canonical
191 * @param name the name to test
192 * @return GNUNET_YES if canonical
195 is_canonical (const char* name)
200 if (NULL == strchr (name, '.'))
205 while (NULL != (dot = strchr (pos, '.')))
215 free_get_pseu_authority_handle (struct GetPseuAuthorityHandle *gph)
217 gph->namestore_task = NULL;
218 GNUNET_free (gph->auth);
219 GNUNET_CRYPTO_rsa_key_free (gph->key);
220 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
226 * Callback that shortens authorities
228 * @param gph the handle containing the name to shorten
231 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
235 * Continueation for pkey record creation (shorten)
237 * @param cls a GetPseuAuthorityHandle
238 * @param success unused
242 create_pkey_cont (void* cls, int32_t success, const char* emsg)
244 //FIXME do sth with error
245 struct GetPseuAuthorityHandle* gph = cls;
247 free_get_pseu_authority_handle (gph);
252 * Namestore calls this function if we have record for this name.
253 * (or with rd_count=0 to indicate no matches)
255 * @param cls the pending query
256 * @param key the key of the zone we did the lookup
257 * @param expiration expiration date of the namestore entry
258 * @param name the name for which we need an authority
259 * @param rd_count the number of records with 'name'
260 * @param rd the record data
261 * @param signature the signature of the authority for the record data
264 process_pseu_lookup_ns (void* cls,
265 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
266 struct GNUNET_TIME_Absolute expiration,
267 const char *name, unsigned int rd_count,
268 const struct GNUNET_NAMESTORE_RecordData *rd,
269 const struct GNUNET_CRYPTO_RsaSignature *signature)
271 struct GetPseuAuthorityHandle* gph = cls;
272 struct GNUNET_NAMESTORE_RecordData new_pkey;
274 gph->namestore_task = NULL;
277 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
278 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
279 free_get_pseu_authority_handle (gph);
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
287 new_pkey.expiration_time = UINT64_MAX;
288 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
289 new_pkey.data = &gph->auth->zone;
290 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
291 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
292 | GNUNET_NAMESTORE_RF_PRIVATE
293 | GNUNET_NAMESTORE_RF_PENDING;
294 gph->namestore_task = GNUNET_NAMESTORE_record_create (namestore_handle,
303 * process result of a dht pseu lookup
305 * @param gph the handle
306 * @param name the pseu result or NULL
309 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "GNS_AUTO_PSEU: No PSEU, no shorten. Finished.\n");
315 free_get_pseu_authority_handle (gph);
319 memcpy (gph->test_name, name, strlen(name) + 1);
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
324 * Check for collision
326 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
329 GNUNET_NAMESTORE_TYPE_ANY,
330 &process_pseu_lookup_ns,
336 * Handle timeout for dht request
338 * @param cls the request handle as closure
339 * @param tc the task context
342 handle_auth_discovery_timeout (void *cls,
343 const struct GNUNET_SCHEDULER_TaskContext *tc)
345 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
349 GNUNET_DHT_get_stop (gph->get_handle);
350 gph->get_handle = NULL;
351 process_pseu_result (gph, NULL);
355 * Function called when we find a PSEU entry in the DHT
357 * @param cls the request handle
358 * @param exp lifetime
359 * @param key the key the record was stored under
360 * @param get_path get path
361 * @param get_path_length get path length
362 * @param put_path put path
363 * @param put_path_length put path length
364 * @param type the block type
365 * @param size the size of the record
366 * @param data the record data
369 process_auth_discovery_dht_result (void* cls,
370 struct GNUNET_TIME_Absolute exp,
371 const struct GNUNET_HashCode * key,
372 const struct GNUNET_PeerIdentity *get_path,
373 unsigned int get_path_length,
374 const struct GNUNET_PeerIdentity *put_path,
375 unsigned int put_path_length,
376 enum GNUNET_BLOCK_Type type,
380 struct GetPseuAuthorityHandle* gph = cls;
381 struct GNSNameRecordBlock *nrb;
382 const char* rd_data = data;
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
391 /* stop lookup and timeout task */
392 GNUNET_DHT_get_stop (gph->get_handle);
393 gph->get_handle = NULL;
394 GNUNET_SCHEDULER_cancel (gph->timeout);
398 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
399 "GNS_GET_AUTH: got dht result null!\n", size);
400 free_get_pseu_authority_handle (gph);
404 nrb = (struct GNSNameRecordBlock*)data;
405 name = (char*)&nrb[1];
406 num_records = ntohl (nrb->rd_count);
408 struct GNUNET_NAMESTORE_RecordData rd[num_records];
410 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
411 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
413 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
419 "GNS_GET_AUTH: Error deserializing data!\n");
423 for (i=0; i < num_records; i++)
425 if ((strcmp (name, "+") == 0) &&
426 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
429 process_pseu_result (gph, (char*)rd[i].data);
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "GNS_GET_AUTH: finished shorten, no results!\n");
437 process_pseu_result (gph, NULL);
441 * Process PSEU discovery for shorten via namestore
443 * @param cls the GetPseuAuthorityHandle
444 * @param key the public key
445 * @param expiration recorddata expiration
446 * @param name the looked up name
447 * @param rd_count number of records in set
448 * @param rd record data
449 * @param signature the signature
452 process_auth_discovery_ns_result (void* cls,
453 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
454 struct GNUNET_TIME_Absolute expiration,
456 unsigned int rd_count,
457 const struct GNUNET_NAMESTORE_RecordData *rd,
458 const struct GNUNET_CRYPTO_RsaSignature *signature)
460 struct GetPseuAuthorityHandle* gph = cls;
461 struct GNUNET_HashCode lookup_key;
465 gph->namestore_task = NULL;
469 GNUNET_GNS_get_key_for_record (GNUNET_GNS_TLD_PLUS, &gph->auth->zone, &lookup_key);
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
473 GNUNET_h2s (&lookup_key));
475 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
476 &handle_auth_discovery_timeout, gph);
478 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
480 GNUNET_assert (gph->get_handle == NULL);
482 gph->get_handle = GNUNET_DHT_get_start (dht_handle,
483 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
485 DHT_GNS_REPLICATION_LEVEL,
486 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
489 &process_auth_discovery_dht_result,
494 for (i=0; i < rd_count; i++)
496 if (0 != (strcmp (name, GNUNET_GNS_TLD_PLUS)))
499 if (rd[i].record_type != GNUNET_GNS_RECORD_PSEU)
503 process_pseu_result (gph, (char*)rd[i].data);
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
508 process_pseu_result (gph, NULL);
512 * Callback called by namestore for a zone to name
515 * @param cls the closure
516 * @param zone_key the zone we queried
517 * @param expire the expiration time of the name
518 * @param name the name found or NULL
519 * @param rd_len number of records for the name
520 * @param rd the record data (PKEY) for the name
521 * @param signature the signature for the record data
524 process_zone_to_name_discover (void *cls,
525 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
526 struct GNUNET_TIME_Absolute expire,
529 const struct GNUNET_NAMESTORE_RecordData *rd,
530 const struct GNUNET_CRYPTO_RsaSignature *signature)
532 struct GetPseuAuthorityHandle* gph = cls;
534 gph->namestore_task = NULL;
537 gph->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
540 GNUNET_GNS_RECORD_PSEU,
541 &process_auth_discovery_ns_result,
545 /* we found a match in our own zone */
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
548 free_get_pseu_authority_handle (gph);
553 * Callback that shortens authorities
555 * @param gph the handle to the shorten request
558 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 "GNS_AUTO_PSEU: New authority %s discovered\n",
565 gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
568 &process_zone_to_name_discover,
575 start_shorten (struct AuthorityChain *auth,
576 const struct GNUNET_CRYPTO_RsaPrivateKey *key)
578 struct GetPseuAuthorityHandle *gph;
579 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
580 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
582 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
583 if (NULL == (pb_key = GNUNET_CRYPTO_rsa_encode_key (key)))
585 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
586 "Failed to encode RSA key on shorten\n");
589 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
590 gph->key = GNUNET_CRYPTO_rsa_decode_key ((const char*) pb_key, ntohs (pb_key->len));
591 GNUNET_free (pb_key);
592 if (NULL == gph->key)
594 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
595 "Failed to decode RSA key on shorten\n");
599 GNUNET_CRYPTO_short_hash (&pkey,
600 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
602 gph->auth = GNUNET_malloc (sizeof (struct AuthorityChain));
603 memcpy (gph->auth, auth, sizeof (struct AuthorityChain));
604 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
605 shorten_authority_chain (gph);
609 * Initialize the resolver
611 * @param nh the namestore handle
612 * @param dh the dht handle
613 * @param lz the local zone's hash
614 * @param c configuration handle
615 * @param max_bg_queries maximum number of parallel background queries in dht
616 * @param ignore_pending ignore records that still require user confirmation
618 * @return GNUNET_OK on success
621 gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
622 struct GNUNET_DHT_Handle *dh,
623 struct GNUNET_CRYPTO_ShortHashCode lz,
624 const struct GNUNET_CONFIGURATION_Handle *c,
625 unsigned long long max_bg_queries,
630 return GNUNET_SYSERR;
633 namestore_handle = nh;
637 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
639 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
640 max_allowed_background_queries = max_bg_queries;
641 max_allowed_ns_tasks = GNUNET_GNS_MAX_NS_TASKS;
642 ignore_pending_records = ignore_pending;
643 GNUNET_RESOLVER_connect (cfg);
651 * @param cls closure to iterator
652 * @param node heap nodes
653 * @param element the namestorebgtask
654 * @param cost heap cost
655 * @return always GNUNET_YES
658 cleanup_pending_ns_tasks (void* cls,
659 struct GNUNET_CONTAINER_HeapNode *node,
661 GNUNET_CONTAINER_HeapCostType cost)
663 struct NamestoreBGTask *nbg = element;
665 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
666 "GNS_CLEANUP: Terminating ns task\n");
667 GNUNET_NAMESTORE_cancel (nbg->qe);
669 GNUNET_CONTAINER_heap_remove_node (node);
677 * @param rh resolver handle
678 * @param rlh record lookup handle
679 * @param rd_count number of results
683 finish_lookup (struct ResolverHandle *rh,
684 struct RecordLookupHandle* rlh,
685 unsigned int rd_count,
686 const struct GNUNET_NAMESTORE_RecordData *rd);
690 * Cleanup background lookups FIXME get rid of this?? YES this doesn't do
691 * anything! => find in code and remove all references to the heap
693 * @param cls closure to iterator
694 * @param node heap nodes
695 * @param element the resolver handle
696 * @param cost heap cost
697 * @return always GNUNET_YES
700 cleanup_pending_background_queries (void* cls,
701 struct GNUNET_CONTAINER_HeapNode *node,
703 GNUNET_CONTAINER_HeapCostType cost)
705 struct ResolverHandle *rh = element;
707 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
708 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
710 GNUNET_CONTAINER_heap_remove_node (node);
711 if (0 == GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
713 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
714 &cleanup_pending_ns_tasks,
722 * Helper function to free resolver handle
724 * @param rh the handle to free
727 free_resolver_handle (struct ResolverHandle* rh)
729 struct AuthorityChain *ac;
730 struct AuthorityChain *ac_next;
735 ac = rh->authority_chain_head;
744 if (NULL != rh->get_handle)
745 GNUNET_DHT_get_stop (rh->get_handle);
747 if (NULL != rh->dns_raw_packet)
748 GNUNET_free (rh->dns_raw_packet);
750 if (NULL != rh->namestore_task)
751 GNUNET_NAMESTORE_cancel (rh->namestore_task);
752 rh->namestore_task = NULL;
754 if (GNUNET_SCHEDULER_NO_TASK != rh->dns_read_task)
755 GNUNET_SCHEDULER_cancel (rh->dns_read_task);
757 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
758 GNUNET_SCHEDULER_cancel (rh->timeout_task);
760 if (NULL != rh->dns_sock)
761 GNUNET_NETWORK_socket_close (rh->dns_sock);
762 if (NULL != rh->dns_resolver_handle)
763 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
765 if (NULL != rh->rd.data)
766 GNUNET_free ((void*)(rh->rd.data));
774 * @param rh resolver handle
775 * @param nsh name shorten handle
778 finish_shorten (struct ResolverHandle *rh,
779 struct NameShortenHandle *nsh);
783 * @param rh resolver handle
784 * @param nah get name authority handle
787 finish_get_auth (struct ResolverHandle *rh,
788 struct GetNameAuthorityHandle* rlh);
795 gns_resolver_cleanup ()
798 struct GetPseuAuthorityHandle *tmp;
800 while (NULL != (tmp = gph_head))
802 if (tmp->get_handle != NULL)
804 GNUNET_DHT_get_stop (tmp->get_handle);
805 tmp->get_handle = NULL;
807 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
809 GNUNET_SCHEDULER_cancel (tmp->timeout);
810 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
812 if (NULL != tmp->namestore_task)
814 GNUNET_NAMESTORE_cancel (tmp->namestore_task);
815 tmp->namestore_task = NULL;
817 free_get_pseu_authority_handle (tmp);
820 while (NULL != rlh_head)
822 finish_lookup (rlh_head, rlh_head->proc_cls, 0, NULL);
824 while (NULL != nsh_head)
826 finish_shorten (nsh_head, nsh_head->proc_cls);
828 while (NULL != nah_head)
830 finish_get_auth (nah_head, nah_head->proc_cls);
833 s = GNUNET_CONTAINER_heap_get_size (dht_lookup_heap);
834 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
835 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
837 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
838 &cleanup_pending_background_queries,
840 else if (0 != GNUNET_CONTAINER_heap_get_size (ns_task_heap))
842 GNUNET_CONTAINER_heap_iterate (ns_task_heap,
843 &cleanup_pending_ns_tasks,
852 * Callback when record data is put into namestore
854 * @param cls the closure
855 * @param success GNUNET_OK on success
856 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
859 on_namestore_record_put_result (void *cls,
863 struct NamestoreBGTask *nbg = cls;
865 GNUNET_CONTAINER_heap_remove_node (nbg->node);
868 if (GNUNET_NO == success)
870 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
871 "GNS_NS: records already in namestore\n");
874 else if (GNUNET_YES == success)
876 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
877 "GNS_NS: records successfully put in namestore\n");
881 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
882 "GNS_NS: Error putting records into namestore: %s\n", emsg);
887 handle_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
889 struct ResolverHandle *rh = cls;
891 if (NULL != rh->timeout_cont)
892 rh->timeout_cont (rh->timeout_cont_cls, tc);
893 /* FIXME: does this leak memory? */
898 * Processor for background lookups in the DHT
900 * @param cls closure (NULL)
901 * @param rd_count number of records found (not 0)
902 * @param rd record data
905 background_lookup_result_processor (void *cls,
907 const struct GNUNET_NAMESTORE_RecordData *rd)
909 //We could do sth verbose/more useful here but it doesn't make any difference
910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
911 "GNS_BG: background dht lookup finished. (%d results)\n",
916 * Handle timeout for DHT requests
918 * @param cls the request handle as closure
919 * @param tc the task context
922 dht_lookup_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
924 struct ResolverHandle *rh = cls;
925 struct RecordLookupHandle *rlh = rh->proc_cls;
926 char new_name[MAX_DNS_NAME_LENGTH];
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 "GNS_PHASE_REC-%llu: dht lookup for query %s (%llus)timed out.\n",
930 rh->id, rh->name, rh->timeout.rel_value);
932 * Start resolution in bg
934 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
935 rh->name, GNUNET_GNS_TLD);
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 "GNS_PHASE_REC-%llu: Starting background lookup for %s type %d\n",
939 rh->id, new_name, rlh->record_type);
941 gns_resolver_lookup_record (rh->authority,
942 rh->private_local_zone,
946 GNUNET_TIME_UNIT_FOREVER_REL,
948 &background_lookup_result_processor,
951 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
953 GNUNET_DHT_get_stop (rh->get_handle);
954 rh->get_handle = NULL;
955 rh->proc (rh->proc_cls, rh, 0, NULL);
960 * Function called when we get a result from the dht
961 * for our record query
963 * @param cls the request handle
964 * @param exp lifetime
965 * @param key the key the record was stored under
966 * @param get_path get path
967 * @param get_path_length get path length
968 * @param put_path put path
969 * @param put_path_length put path length
970 * @param type the block type
971 * @param size the size of the record
972 * @param data the record data
975 process_record_result_dht (void* cls,
976 struct GNUNET_TIME_Absolute exp,
977 const struct GNUNET_HashCode * key,
978 const struct GNUNET_PeerIdentity *get_path,
979 unsigned int get_path_length,
980 const struct GNUNET_PeerIdentity *put_path,
981 unsigned int put_path_length,
982 enum GNUNET_BLOCK_Type type,
983 size_t size, const void *data)
985 struct ResolverHandle *rh;
986 struct RecordLookupHandle *rlh;
987 struct GNSNameRecordBlock *nrb;
988 uint32_t num_records;
990 char* rd_data = (char*)data;
994 rh = (struct ResolverHandle *)cls;
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
996 "GNS_PHASE_REC-%llu: got dht result (size=%d)\n", rh->id, size);
998 rlh = (struct RecordLookupHandle *) rh->proc_cls;
999 nrb = (struct GNSNameRecordBlock*)data;
1001 /* stop lookup and timeout task */
1002 GNUNET_DHT_get_stop (rh->get_handle);
1003 rh->get_handle = NULL;
1005 if (rh->dht_heap_node != NULL)
1007 GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
1008 rh->dht_heap_node = NULL;
1011 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1013 GNUNET_SCHEDULER_cancel (rh->timeout_task);
1014 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1017 rh->get_handle = NULL;
1018 name = (char*)&nrb[1];
1019 num_records = ntohl (nrb->rd_count);
1021 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1022 struct NamestoreBGTask *ns_heap_root;
1023 struct NamestoreBGTask *namestore_bg_task;
1025 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
1026 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
1028 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1033 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1034 "GNS_PHASE_REC-%llu: Error deserializing data!\n", rh->id);
1035 rh->proc (rh->proc_cls, rh, 0, NULL);
1039 for (i=0; i<num_records; i++)
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042 "GNS_PHASE_REC-%llu: Got name: %s (wanted %s)\n",
1043 rh->id, name, rh->name);
1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1045 "GNS_PHASE_REC-%llu: Got type: %d (wanted %d)\n",
1046 rh->id, rd[i].record_type, rlh->record_type);
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "GNS_PHASE_REC-%llu: Got data length: %d\n",
1049 rh->id, rd[i].data_size);
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "GNS_PHASE_REC-%llu: Got flag %d\n",
1052 rh->id, rd[i].flags);
1054 if ((strcmp (name, rh->name) == 0) &&
1055 (rd[i].record_type == rlh->record_type))
1063 * FIXME check pubkey against existing key in namestore?
1064 * https://gnunet.org/bugs/view.php?id=2179
1066 if (max_allowed_ns_tasks <=
1067 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
1069 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
1070 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073 "GNS_PHASE_REC-%llu: Replacing oldest background ns task\n",
1077 /* Save to namestore */
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "GNS_PHASE_REC-%llu: Caching record for %s\n",
1081 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
1082 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
1089 &on_namestore_record_put_result, //cont
1092 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
1094 GNUNET_TIME_absolute_get().abs_value);
1097 if (0 < rh->answered)
1098 rh->proc (rh->proc_cls, rh, num_records, rd);
1100 rh->proc (rh->proc_cls, rh, 0, NULL);
1107 * Start DHT lookup for a (name -> query->record_type) record in
1108 * rh->authority's zone
1110 * @param rh the pending gns query context
1113 resolve_record_dht (struct ResolverHandle *rh)
1116 struct GNUNET_HashCode lookup_key;
1117 struct RecordLookupHandle *rlh = rh->proc_cls;
1118 struct ResolverHandle *rh_heap_root;
1120 GNUNET_GNS_get_key_for_record (rh->name, &rh->authority, &lookup_key);
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "GNS_PHASE_REC-%llu: starting dht lookup for %s with key: %s\n",
1123 rh->id, rh->name, GNUNET_h2s (&lookup_key));
1125 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1126 rh->dht_heap_node = NULL;
1128 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
1131 * Update timeout if necessary
1133 if (GNUNET_SCHEDULER_NO_TASK == rh->timeout_task)
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136 "GNS_PHASE_REC-%llu: Adjusting timeout\n", rh->id);
1138 * Set timeout for authority lookup phase to 1/2
1140 rh->timeout_task = GNUNET_SCHEDULER_add_delayed (
1141 GNUNET_TIME_relative_divide (rh->timeout, 2),
1142 &handle_lookup_timeout,
1145 rh->timeout_cont = &dht_lookup_timeout;
1146 rh->timeout_cont_cls = rh;
1150 if (max_allowed_background_queries <=
1151 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1153 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1154 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
1155 rh_heap_root->get_handle = NULL;
1156 rh_heap_root->dht_heap_node = NULL;
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1159 "GNS_PHASE_REC-%llu: Replacing oldest background query for %s\n",
1160 rh->id, rh_heap_root->name);
1161 rh_heap_root->proc (rh_heap_root->proc_cls,
1166 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1168 GNUNET_TIME_absolute_get ().abs_value);
1171 xquery = htonl (rlh->record_type);
1173 GNUNET_assert (rh->get_handle == NULL);
1174 rh->get_handle = GNUNET_DHT_get_start (dht_handle,
1175 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1177 DHT_GNS_REPLICATION_LEVEL,
1178 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1181 &process_record_result_dht,
1188 * Namestore calls this function if we have record for this name.
1189 * (or with rd_count=0 to indicate no matches)
1191 * @param cls the pending query
1192 * @param key the key of the zone we did the lookup
1193 * @param expiration expiration date of the namestore entry
1194 * @param name the name for which we need an authority
1195 * @param rd_count the number of records with 'name'
1196 * @param rd the record data
1197 * @param signature the signature of the authority for the record data
1200 process_record_result_ns (void* cls,
1201 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1202 struct GNUNET_TIME_Absolute expiration,
1203 const char *name, unsigned int rd_count,
1204 const struct GNUNET_NAMESTORE_RecordData *rd,
1205 const struct GNUNET_CRYPTO_RsaSignature *signature)
1207 struct ResolverHandle *rh;
1208 struct RecordLookupHandle *rlh;
1209 struct GNUNET_TIME_Relative remaining_time;
1210 struct GNUNET_CRYPTO_ShortHashCode zone;
1211 struct GNUNET_TIME_Absolute et;
1217 rh->namestore_task = NULL;
1218 GNUNET_CRYPTO_short_hash (key,
1219 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1221 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1229 rh->status |= RSL_RECORD_EXISTS;
1231 if (remaining_time.rel_value == 0)
1233 rh->status |= RSL_RECORD_EXPIRED;
1240 * Lookup terminated and no results
1242 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1243 "GNS_PHASE_REC-%llu: Namestore lookup for %s terminated without results\n",
1246 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1247 "GNS_PHASE_REC-%llu: Record %s unknown in namestore\n",
1250 * Our zone and no result? Cannot resolve TT
1252 rh->proc(rh->proc_cls, rh, 0, NULL);
1256 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1257 "GNS_PHASE_REC-%llu: Processing additional result %s from namestore\n",
1259 for (i=0; i<rd_count;i++)
1261 if (rd[i].record_type != rlh->record_type)
1264 if (ignore_pending_records &&
1265 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1268 "GNS_PHASE_REC-%llu: Record %s is awaiting user confirmation. Skipping\n",
1273 //FIXME: eh? do I have to handle this here?
1274 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1275 et.abs_value = rd[i].expiration_time;
1276 if (0 == (GNUNET_TIME_absolute_get_remaining (et)).rel_value)
1278 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1279 "GNS_PHASE_REC-%llu: This record is expired. Skipping\n",
1289 if (0 == rh->answered)
1291 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1292 "GNS_PHASE_REC-%llu: No answers found. This is odd!\n", rh->id);
1293 rh->proc(rh->proc_cls, rh, 0, NULL);
1297 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1298 "GNS_PHASE_REC-%llu: Found %d answer(s) to query in %d records!\n",
1299 rh->id, rh->answered, rd_count);
1300 rh->proc(rh->proc_cls, rh, rd_count, rd);
1305 * VPN redirect result callback
1307 * @param cls the resolver handle
1308 * @param af the requested address family
1309 * @param address in_addr(6) respectively
1312 process_record_result_vpn (void* cls, int af, const void *address)
1314 struct ResolverHandle *rh = cls;
1315 struct RecordLookupHandle *rlh;
1316 struct GNUNET_NAMESTORE_RecordData rd;
1320 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1321 "GNS_PHASE_REC_VPN-%llu: Got answer from VPN to query!\n",
1325 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1326 "GNS_PHASE_REC-%llu: Answer is IPv4!\n",
1328 if (GNUNET_GNS_RECORD_A != rlh->record_type)
1330 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1331 "GNS_PHASE_REC-%llu: Requested record is not IPv4!\n",
1333 rh->proc (rh->proc_cls, rh, 0, NULL);
1336 rd.record_type = GNUNET_GNS_RECORD_A;
1337 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1339 rd.data_size = sizeof (struct in_addr);
1341 rh->proc (rh->proc_cls, rh, 1, &rd);
1344 else if (AF_INET6 == af)
1346 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1347 "GNS_PHASE_REC-%llu: Answer is IPv6!\n",
1349 if (GNUNET_GNS_RECORD_AAAA != rlh->record_type)
1351 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1352 "GNS_PHASE_REC-%llu: Requested record is not IPv6!\n",
1354 rh->proc (rh->proc_cls, rh, 0, NULL);
1357 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1358 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1360 rd.data_size = sizeof (struct in6_addr);
1362 rh->proc (rh->proc_cls, rh, 1, &rd);
1366 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1367 "GNS_PHASE_REC-%llu: Got garbage from VPN!\n",
1369 rh->proc (rh->proc_cls, rh, 0, NULL);
1376 * Process VPN lookup result for record
1378 * @param cls the record lookup handle
1379 * @param rh resolver handle
1380 * @param rd_count number of results (1)
1381 * @param rd record data containing the result
1384 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1385 unsigned int rd_count,
1386 const struct GNUNET_NAMESTORE_RecordData *rd)
1388 struct RecordLookupHandle* rlh = cls;
1392 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1393 "GNS_PHASE_REC_VPN-%llu: VPN returned no records. (status: %d)!\n",
1396 /* give up, cannot resolve */
1397 finish_lookup(rh, rlh, 0, NULL);
1401 /* results found yay */
1402 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1403 "GNS_PHASE_REC_VPN-%llu: Record resolved from VPN!", rh->id);
1405 finish_lookup(rh, rlh, rd_count, rd);
1410 * Sends a UDP dns query to a nameserver specified in the rh
1412 * @param rh the resolver handle
1415 send_dns_packet (struct ResolverHandle *rh);
1419 handle_dns_resolver (void *cls,
1420 const struct sockaddr *addr,
1423 struct ResolverHandle *rh = cls;
1424 struct RecordLookupHandle *rlh = rh->proc_cls;
1425 struct GNUNET_NAMESTORE_RecordData rd;
1426 struct sockaddr_in *sai;
1427 struct sockaddr_in6 *sai6;
1431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1432 "No address found in DNS!\n");
1433 finish_lookup (rh, rlh, 0, NULL);
1437 if (sizeof (struct sockaddr_in) == addrlen)
1439 sai = (struct sockaddr_in*) addr;
1440 rd.record_type = GNUNET_GNS_RECORD_A;
1441 rd.data_size = sizeof (struct in_addr);
1442 rd.data = &sai->sin_addr;
1444 else if (sizeof (struct sockaddr_in6) == addrlen)
1446 sai6 = (struct sockaddr_in6*) addr;
1447 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1448 rd.data_size = sizeof (struct in6_addr);
1449 rd.data = &sai6->sin6_addr;
1453 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1454 "Address length is garbage!\n");
1455 finish_lookup (rh, rlh, 0, NULL);
1458 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1460 finish_lookup (rh, rlh, 1, &rd);
1464 * Resolve DNS name via local stub resolver
1466 * @param rh the resolver handle
1469 resolve_dns_name (struct ResolverHandle *rh)
1471 struct RecordLookupHandle *rlh = rh->proc_cls;
1474 if ((GNUNET_GNS_RECORD_A != rlh->record_type) &&
1475 (GNUNET_GNS_RECORD_AAAA != rlh->record_type))
1477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1478 "Can only resolve A/AAAA via stub... abort\n");
1479 finish_lookup (rh, rlh, 0, NULL);
1483 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1488 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1491 &handle_dns_resolver,
1497 * Read DNS udp packet from socket
1499 * @param cls the resolver handle
1500 * @param tc task context
1503 read_dns_response (void *cls,
1504 const struct GNUNET_SCHEDULER_TaskContext *tc)
1506 struct ResolverHandle *rh = cls;
1507 struct RecordLookupHandle *rlh = rh->proc_cls;
1508 char buf[UINT16_MAX];
1510 struct sockaddr_in addr;
1512 struct GNUNET_DNSPARSER_Packet *packet;
1513 struct GNUNET_NAMESTORE_RecordData rd;
1514 int found_delegation = GNUNET_NO;
1515 int found_cname = GNUNET_NO;
1516 char* delegation_name = NULL;
1517 int zone_offset = 0;
1520 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1521 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1523 /* timeout or shutdown */
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525 "Terminating DNS query %d\n", tc->reason);
1526 finish_lookup (rh, rlh, 0, NULL);
1530 addrlen = sizeof (addr);
1531 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1533 (struct sockaddr*) &addr,
1538 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1539 finish_lookup (rh, rlh, 0, NULL);
1543 packet = GNUNET_DNSPARSER_parse (buf, r);
1547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1548 "Failed to parse DNS reply!\n");
1549 finish_lookup (rh, rlh, 0, NULL);
1553 for (i = 0; i < packet->num_answers; i++)
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556 "Got record type %d (want %d)\n",
1557 packet->answers[i].type,
1559 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1560 if (packet->answers[i].type == GNUNET_GNS_RECORD_CNAME)
1562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1563 "CNAME... restarting query with %s\n",
1564 packet->answers[i].data.hostname
1566 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1567 found_cname = GNUNET_YES;
1571 if ((packet->answers[i].type == rlh->record_type) &&
1572 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1576 rd.data = packet->answers[i].data.raw.data;
1577 rd.data_size = packet->answers[i].data.raw.data_len;
1578 rd.record_type = packet->answers[i].type;
1580 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1581 finish_lookup (rh, rlh, 1, &rd);
1582 GNUNET_DNSPARSER_free_packet (packet);
1587 if (GNUNET_YES == found_cname)
1589 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1591 if (0 > zone_offset)
1594 /* restart query with CNAME */
1595 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1598 "Asking same server for %s\n", rh->dns_name);
1599 send_dns_packet (rh);
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 "Trying system resolver for %s\n", rh->dns_name);
1605 resolve_dns_name (rh);
1608 GNUNET_DNSPARSER_free_packet (packet);
1612 for (i = 0; i < packet->num_authority_records; i++)
1615 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_NS)
1617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1618 "Found NS delegation!\n");
1619 found_delegation = GNUNET_YES;
1620 delegation_name = packet->authority_records[i].data.hostname;
1625 for (i = 0; i < packet->num_additional_records; i++)
1627 if (GNUNET_NO == found_delegation)
1630 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_A) &&
1631 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1633 GNUNET_assert (sizeof (struct in_addr) ==
1634 packet->authority_records[i].data.raw.data_len);
1636 rh->dns_addr.sin_addr =
1637 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1638 send_dns_packet (rh);
1639 GNUNET_DNSPARSER_free_packet (packet);
1644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1645 "Nothing useful in DNS reply!\n");
1646 finish_lookup (rh, rlh, 0, NULL);
1647 GNUNET_DNSPARSER_free_packet (packet);
1652 * Sends a UDP dns query to a nameserver specified in the rh
1654 * @param rh the request handle
1657 send_dns_packet (struct ResolverHandle *rh)
1659 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1660 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1663 "Sending %dbyte DNS query\n",
1664 rh->dns_raw_packet_size);
1666 if (GNUNET_SYSERR ==
1667 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1669 rh->dns_raw_packet_size,
1670 (struct sockaddr*)&rh->dns_addr,
1671 sizeof (struct sockaddr_in)))
1672 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1673 _("Failed to send DNS request to %s\n"),
1674 GNUNET_a2s ((const struct sockaddr *)&rh->dns_addr,
1675 sizeof (struct sockaddr_in)));
1677 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1678 rh->timeout, //FIXME less?
1684 GNUNET_NETWORK_fdset_destroy (rset);
1689 * The final phase of resoution.
1690 * We found a NS RR and want to resolve via DNS
1692 * @param rh the pending lookup handle
1693 * @param rd_count length of record data
1694 * @param rd record data containing VPN RR
1697 resolve_record_dns (struct ResolverHandle *rh,
1699 const struct GNUNET_NAMESTORE_RecordData *rd)
1701 struct GNUNET_DNSPARSER_Query query;
1702 struct GNUNET_DNSPARSER_Packet packet;
1703 struct GNUNET_DNSPARSER_Flags flags;
1704 struct in_addr dnsip;
1705 struct sockaddr_in addr;
1706 struct sockaddr *sa;
1708 struct RecordLookupHandle *rlh = rh->proc_cls;
1710 memset (&packet, 0, sizeof (struct GNUNET_DNSPARSER_Packet));
1711 memset (rh->dns_name, 0, sizeof (rh->dns_name));
1713 /* We cancel here as to not include the ns lookup in the timeout */
1714 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1716 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1717 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1719 /* Start shortening */
1720 if ((NULL != rh->priv_key) &&
1721 (GNUNET_YES == is_canonical (rh->name)))
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1724 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1726 start_shorten (rh->authority_chain_head,
1730 for (i = 0; i < rd_count; i++)
1732 /* Synthesize dns name */
1733 if (GNUNET_GNS_RECORD_NS == rd[i].record_type)
1735 strcpy (rh->dns_zone, (char*)rd[i].data);
1736 if (0 == strcmp (rh->name, ""))
1737 strcpy (rh->dns_name, (char*)rd[i].data);
1739 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1742 if (GNUNET_GNS_RECORD_A == rd[i].record_type)
1743 /* need to use memcpy as .data may be unaligned */
1744 memcpy (&dnsip, rd[i].data, sizeof (dnsip));
1747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1748 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1752 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1753 if (NULL == rh->dns_sock)
1755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1756 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1758 finish_lookup (rh, rlh, 0, NULL);
1762 memset (&addr, 0, sizeof (struct sockaddr_in));
1763 sa = (struct sockaddr *) &addr;
1764 sa->sa_family = AF_INET;
1765 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1767 sizeof (struct sockaddr_in)))
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1772 finish_lookup (rh, rlh, 0, NULL);
1775 query.name = rh->dns_name;
1776 query.type = rlh->record_type;
1777 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1778 memset (&flags, 0, sizeof (flags));
1779 flags.recursion_desired = 1;
1780 flags.checking_disabled = 1;
1781 packet.queries = &query;
1782 packet.answers = NULL;
1783 packet.authority_records = NULL;
1784 packet.num_queries = 1;
1785 packet.num_answers = 0;
1786 packet.num_authority_records = 0;
1787 packet.num_additional_records = 0;
1788 packet.flags = flags;
1790 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1792 &rh->dns_raw_packet,
1793 &rh->dns_raw_packet_size))
1795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1796 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1798 GNUNET_NETWORK_socket_close (rh->dns_sock);
1799 finish_lookup (rh, rlh, 0, NULL);
1803 rh->dns_addr.sin_family = AF_INET;
1804 rh->dns_addr.sin_port = htons (53); //domain
1805 rh->dns_addr.sin_addr = dnsip;
1806 #if HAVE_SOCKADDR_IN_SIN_LEN
1807 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1810 send_dns_packet (rh);
1815 * The final phase of resoution.
1816 * We found a VPN RR and want to request an IPv4/6 address
1818 * @param rh the pending lookup handle
1819 * @param rd_count length of record data
1820 * @param rd record data containing VPN RR
1823 resolve_record_vpn (struct ResolverHandle *rh,
1825 const struct GNUNET_NAMESTORE_RecordData *rd)
1827 struct RecordLookupHandle *rlh = rh->proc_cls;
1828 struct GNUNET_HashCode serv_desc;
1829 struct vpn_data* vpn;
1832 /* We cancel here as to not include the ns lookup in the timeout */
1833 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1835 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1836 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1838 /* Start shortening */
1839 if ((NULL != rh->priv_key) &&
1840 (GNUNET_YES == is_canonical (rh->name)))
1842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1843 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1845 start_shorten (rh->authority_chain_head,
1849 vpn = (struct vpn_data*)rd->data;
1852 GNUNET_CRYPTO_hash ((char*)&vpn[1],
1853 strlen ((char*)&vpn[1]) + 1,
1855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1856 "GNS_PHASE_REC_VPN-%llu: proto %hu peer %s!\n",
1859 GNUNET_h2s (&vpn->peer));
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1862 "GNS_PHASE_REC_VPN-%llu: service %s -> %s!\n",
1865 GNUNET_h2s (&serv_desc));
1866 rh->proc = &handle_record_vpn;
1867 if (GNUNET_GNS_RECORD_A == rlh->record_type)
1871 if (NULL == vpn_handle)
1873 vpn_handle = GNUNET_VPN_connect (cfg);
1874 if (NULL == vpn_handle)
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1877 "GNS_PHASE_INIT: Error connecting to VPN!\n");
1878 finish_lookup (rh, rh->proc_cls, 0, NULL);
1883 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1884 af, ntohs (vpn->proto),
1885 (struct GNUNET_PeerIdentity *)&vpn->peer,
1888 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1889 &process_record_result_vpn,
1895 * The final phase of resolution.
1896 * rh->name is a name that is canonical and we do not have a delegation.
1897 * Query namestore for this record
1899 * @param rh the pending lookup handle
1902 resolve_record_ns(struct ResolverHandle *rh)
1904 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1906 /* We cancel here as to not include the ns lookup in the timeout */
1907 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
1909 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1910 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1912 /* Start shortening */
1913 if ((NULL != rh->priv_key) &&
1914 (GNUNET_YES == is_canonical (rh->name)))
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1919 start_shorten (rh->authority_chain_head,
1924 * Try to resolve this record in our namestore.
1925 * The name to resolve is now in rh->authority_name
1926 * since we tried to resolve it to an authority
1929 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
1933 &process_record_result_ns,
1940 * Handle timeout for DHT requests
1942 * @param cls the request handle as closure
1943 * @param tc the task context
1946 dht_authority_lookup_timeout(void *cls,
1947 const struct GNUNET_SCHEDULER_TaskContext *tc)
1949 struct ResolverHandle *rh = cls;
1950 struct RecordLookupHandle *rlh = rh->proc_cls;
1951 char new_name[MAX_DNS_NAME_LENGTH];
1953 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1954 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%llus)timed out.\n",
1955 rh->id, rh->authority_name, rh->timeout.rel_value);
1957 rh->status |= RSL_TIMED_OUT;
1958 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1959 if (NULL != rh->get_handle)
1960 GNUNET_DHT_get_stop (rh->get_handle);
1962 rh->get_handle = NULL;
1963 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
1965 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1966 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
1968 rh->proc(rh->proc_cls, rh, 0, NULL);
1972 if (0 == strcmp(rh->name, ""))
1975 * promote authority back to name and try to resolve record
1977 strcpy(rh->name, rh->authority_name);
1978 rh->proc(rh->proc_cls, rh, 0, NULL);
1983 * Start resolution in bg
1985 GNUNET_assert (0 < GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
1986 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD));
1988 strcpy(rh->name, new_name);
1990 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1991 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1992 rh->id, rh->name, rlh->record_type);
1994 gns_resolver_lookup_record (rh->authority,
1995 rh->private_local_zone,
1999 GNUNET_TIME_UNIT_FOREVER_REL,
2001 &background_lookup_result_processor,
2003 rh->proc(rh->proc_cls, rh, 0, NULL);
2007 static void resolve_delegation_dht(struct ResolverHandle *rh);
2010 static void resolve_delegation_ns(struct ResolverHandle *rh);
2014 * Namestore resolution for delegation finished. Processing result.
2016 * @param cls the closure
2017 * @param rh resolver handle
2018 * @param rd_count number of results (always 0)
2019 * @param rd record data (always NULL)
2022 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2023 unsigned int rd_count,
2024 const struct GNUNET_NAMESTORE_RecordData *rd);
2028 * This is a callback function that checks for key revocation
2030 * @param cls the pending query
2031 * @param key the key of the zone we did the lookup
2032 * @param expiration expiration date of the record data set in the namestore
2033 * @param name the name for which we need an authority
2034 * @param rd_count the number of records with 'name'
2035 * @param rd the record data
2036 * @param signature the signature of the authority for the record data
2039 process_pkey_revocation_result_ns (void *cls,
2040 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2041 struct GNUNET_TIME_Absolute expiration,
2043 unsigned int rd_count,
2044 const struct GNUNET_NAMESTORE_RecordData *rd,
2045 const struct GNUNET_CRYPTO_RsaSignature *signature)
2047 struct ResolverHandle *rh = cls;
2048 struct GNUNET_TIME_Relative remaining_time;
2051 rh->namestore_task = NULL;
2052 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2054 for (i = 0; i < rd_count; i++)
2056 if (GNUNET_GNS_RECORD_REV == rd[i].record_type)
2058 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2059 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2061 rh->status |= RSL_PKEY_REVOKED;
2062 rh->proc (rh->proc_cls, rh, 0, NULL);
2067 if ((NULL == name) ||
2068 (0 == remaining_time.rel_value))
2070 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2071 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2074 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != rh->timeout.rel_value)
2076 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2077 "GNS_PHASE_DELEGATE_REV-%llu: Starting background lookup for %s type %d\n",
2078 rh->id, "+.gads", GNUNET_GNS_RECORD_REV);
2080 gns_resolver_lookup_record(rh->authority,
2081 rh->private_local_zone,
2082 GNUNET_GNS_RECORD_REV,
2085 GNUNET_TIME_UNIT_FOREVER_REL,
2087 &background_lookup_result_processor,
2091 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2092 "GNS_PHASE_DELEGATE_REV-%llu: Revocation check passed\n",
2095 * We are done with PKEY resolution if name is empty
2096 * else resolve again with new authority
2098 if (strcmp (rh->name, "") == 0)
2099 rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
2101 resolve_delegation_ns (rh);
2106 * Callback when record data is put into namestore
2108 * @param cls the closure
2109 * @param success GNUNET_OK on success
2110 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
2113 on_namestore_delegation_put_result(void *cls,
2117 struct NamestoreBGTask *nbg = cls;
2119 GNUNET_CONTAINER_heap_remove_node (nbg->node);
2122 if (GNUNET_NO == success)
2124 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2125 "GNS_NS: records already in namestore\n");
2128 else if (GNUNET_YES == success)
2130 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2131 "GNS_NS: records successfully put in namestore\n");
2135 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2136 "GNS_NS: Error putting records into namestore: %s\n", emsg);
2140 * Function called when we get a result from the dht
2141 * for our query. Recursively tries to resolve authorities
2144 * @param cls the request handle
2145 * @param exp lifetime
2146 * @param key the key the record was stored under
2147 * @param get_path get path
2148 * @param get_path_length get path length
2149 * @param put_path put path
2150 * @param put_path_length put path length
2151 * @param type the block type
2152 * @param size the size of the record
2153 * @param data the record data
2156 process_delegation_result_dht(void* cls,
2157 struct GNUNET_TIME_Absolute exp,
2158 const struct GNUNET_HashCode * key,
2159 const struct GNUNET_PeerIdentity *get_path,
2160 unsigned int get_path_length,
2161 const struct GNUNET_PeerIdentity *put_path,
2162 unsigned int put_path_length,
2163 enum GNUNET_BLOCK_Type type,
2164 size_t size, const void *data)
2166 struct ResolverHandle *rh = cls;
2167 struct GNSNameRecordBlock *nrb;
2168 uint32_t num_records;
2170 char* rd_data = (char*) data;
2173 struct GNUNET_CRYPTO_ShortHashCode zone;
2175 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2176 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2181 nrb = (struct GNSNameRecordBlock*)data;
2183 /* stop dht lookup and timeout task */
2184 GNUNET_DHT_get_stop (rh->get_handle);
2185 rh->get_handle = NULL;
2186 if (rh->dht_heap_node != NULL)
2188 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2189 rh->dht_heap_node = NULL;
2192 num_records = ntohl(nrb->rd_count);
2193 name = (char*)&nrb[1];
2195 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2196 struct NamestoreBGTask *ns_heap_root;
2197 struct NamestoreBGTask *namestore_bg_task;
2199 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2200 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2201 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2206 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2207 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2212 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2213 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2214 rh->id, name, rh->authority_name);
2215 for (i=0; i<num_records; i++)
2218 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2219 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2220 rh->id, name, rh->authority_name);
2221 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2222 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2223 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2224 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2225 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2226 rh->id, rd[i].data_size);
2227 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2228 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2229 rh->id, rd[i].flags);
2231 if ((GNUNET_GNS_RECORD_VPN == rd[i].record_type) ||
2232 (GNUNET_GNS_RECORD_NS == rd[i].record_type) ||
2233 (GNUNET_GNS_RECORD_CNAME == rd[i].record_type))
2236 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2238 if (0 == strcmp(rh->name, ""))
2239 strcpy(rh->name, rh->authority_name);
2241 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2242 rh->name, rh->authority_name); //FIXME ret
2247 if ((0 == strcmp(name, rh->authority_name)) &&
2248 (GNUNET_GNS_RECORD_PKEY == rd[i].record_type))
2250 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2251 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2254 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2255 struct AuthorityChain *auth =
2256 GNUNET_malloc(sizeof(struct AuthorityChain));
2257 auth->zone = rh->authority;
2258 memset(auth->name, 0, strlen(rh->authority_name)+1);
2259 strcpy(auth->name, rh->authority_name);
2260 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2261 rh->authority_chain_tail,
2264 if (NULL != rh->rd.data)
2265 GNUNET_free ((void*)rh->rd.data);
2267 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
2268 rh->rd.data = GNUNET_malloc (rd[i].data_size);
2269 memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
2272 /** try to import pkey if private key available */
2273 //if (rh->priv_key && is_canonical (rh->name))
2274 // process_discovered_authority(name, auth->zone,
2275 // rh->authority_chain_tail->zone,
2280 GNUNET_GNS_get_zone_from_key (name, key, &zone);
2283 /* Save to namestore
2284 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2287 if (max_allowed_ns_tasks <=
2288 GNUNET_CONTAINER_heap_get_size (ns_task_heap))
2290 ns_heap_root = GNUNET_CONTAINER_heap_remove_root (ns_task_heap);
2291 GNUNET_NAMESTORE_cancel (ns_heap_root->qe);
2293 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2294 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background ns task\n",
2297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2298 "GNS_PHASE_DELEGATE_DHT-%llu: Caching record for %s\n",
2300 namestore_bg_task = GNUNET_malloc (sizeof (struct NamestoreBGTask));
2302 namestore_bg_task->node = GNUNET_CONTAINER_heap_insert (ns_task_heap,
2304 GNUNET_TIME_absolute_get().abs_value);
2305 namestore_bg_task->qe = GNUNET_NAMESTORE_record_put (namestore_handle,
2312 &on_namestore_delegation_put_result, //cont
2313 namestore_bg_task); //cls
2317 if (0 != rh->answered)
2322 * FIXME in this case. should we ask namestore again?
2324 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2325 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2326 rh->id, rh->authority_name, rh->name);
2328 if (0 == strcmp(rh->name, ""))
2330 /* Start shortening */
2331 if ((NULL != rh->priv_key) &&
2332 (GNUNET_YES == is_canonical (rh->name)))
2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2335 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2337 start_shorten (rh->authority_chain_head,
2342 rh->proc = &handle_delegation_ns;
2345 /* Check for key revocation and delegate */
2346 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
2349 GNUNET_GNS_RECORD_REV,
2350 &process_pkey_revocation_result_ns,
2357 * No pkey but name exists
2360 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2361 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2362 rh->id, rh->authority_name, rh->name);
2363 if (0 == strcmp(rh->name, ""))
2364 strcpy(rh->name, rh->authority_name);
2366 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2367 rh->name, rh->authority_name); //FIXME ret
2369 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2370 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2372 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2374 rh->proc(rh->proc_cls, rh, 0, NULL);
2377 //FIXME maybe define somewhere else?
2378 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2379 +(MAX_DNS_NAME_LENGTH*2)
2380 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2381 #define MAX_SRV_LENGTH (sizeof(uint16_t)*3)+MAX_DNS_NAME_LENGTH
2385 expand_plus(char* dest, char* src, char* repl)
2388 unsigned int s_len = strlen(src)+1;
2390 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2391 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2392 //Eh? I guess this is at least strlen ('x.+') == 3 FIXME
2395 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2396 "GNS_POSTPROCESS: %s too short\n", src);
2398 /* no postprocessing */
2399 memcpy(dest, src, s_len+1);
2403 if (0 == strcmp(src+s_len-3, ".+"))
2405 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2406 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2407 memset(dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2411 pos += strlen(repl);
2412 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2413 "GNS_POSTPROCESS: Expanded to %s\n", dest);
2417 memcpy(dest, src, s_len+1);
2425 finish_lookup (struct ResolverHandle *rh,
2426 struct RecordLookupHandle* rlh,
2427 unsigned int rd_count,
2428 const struct GNUNET_NAMESTORE_RecordData *rd)
2431 char new_rr_data[MAX_DNS_NAME_LENGTH];
2432 char new_mx_data[MAX_MX_LENGTH];
2433 char new_soa_data[MAX_SOA_LENGTH];
2434 char new_srv_data[MAX_SRV_LENGTH];
2435 struct srv_data *old_srv;
2436 struct srv_data *new_srv;
2437 struct soa_data *old_soa;
2438 struct soa_data *new_soa;
2439 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2442 unsigned int offset;
2444 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
2446 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2447 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2450 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2453 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2455 for (i = 0; i < rd_count; i++)
2458 if ((GNUNET_GNS_RECORD_NS != rd[i].record_type) &&
2459 (GNUNET_GNS_RECORD_PTR != rd[i].record_type) &&
2460 (GNUNET_GNS_RECORD_CNAME != rd[i].record_type) &&
2461 (GNUNET_GNS_RECORD_MX != rd[i].record_type) &&
2462 (GNUNET_GNS_RECORD_SOA != rd[i].record_type) &&
2463 (GNUNET_GNS_RECORD_SRV != rd[i].record_type))
2465 p_rd[i].data = rd[i].data;
2470 * for all those records we 'should'
2471 * also try to resolve the A/AAAA records (RFC1035)
2472 * This is a feature and not important
2475 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2476 "GNS_POSTPROCESS: Postprocessing\n");
2477 if (0 == strcmp(rh->name, "+"))
2478 repl_string = rlh->name;
2480 repl_string = rlh->name+strlen(rh->name)+1;
2483 if (GNUNET_GNS_RECORD_MX == rd[i].record_type)
2485 memcpy (new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2486 offset = sizeof(uint16_t);
2487 pos = new_mx_data+offset;
2488 expand_plus(pos, (char*)rd[i].data+sizeof(uint16_t),
2490 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2491 p_rd[i].data = new_mx_data;
2492 p_rd[i].data_size = offset;
2494 else if (GNUNET_GNS_RECORD_SRV == rd[i].record_type)
2497 * Prio, weight and port
2499 new_srv = (struct srv_data*)new_srv_data;
2500 old_srv = (struct srv_data*)rd[i].data;
2501 new_srv->prio = old_srv->prio;
2502 new_srv->weight = old_srv->weight;
2503 new_srv->port = old_srv->port;
2504 expand_plus((char*)&new_srv[1], (char*)&old_srv[1],
2506 p_rd[i].data = new_srv_data;
2507 p_rd[i].data_size = sizeof (struct srv_data) + strlen ((char*)&new_srv[1]) + 1;
2509 else if (GNUNET_GNS_RECORD_SOA == rd[i].record_type)
2511 /* expand mname and rname */
2512 old_soa = (struct soa_data*)rd[i].data;
2513 new_soa = (struct soa_data*)new_soa_data;
2514 memcpy (new_soa, old_soa, sizeof (struct soa_data));
2515 expand_plus((char*)&new_soa[1], (char*)&old_soa[1], repl_string);
2516 offset = strlen ((char*)&new_soa[1]) + 1;
2517 expand_plus((char*)&new_soa[1] + offset,
2518 (char*)&old_soa[1] + strlen ((char*)&old_soa[1]) + 1,
2520 p_rd[i].data_size = sizeof (struct soa_data)
2522 + strlen ((char*)&new_soa[1] + offset);
2523 p_rd[i].data = new_soa_data;
2528 expand_plus(pos, (char*)rd[i].data, repl_string);
2529 p_rd[i].data_size = strlen(new_rr_data)+1;
2530 p_rd[i].data = new_rr_data;
2535 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2537 free_resolver_handle (rh);
2541 * Process DHT lookup result for record.
2543 * @param cls the closure
2544 * @param rh resolver handle
2545 * @param rd_count number of results
2546 * @param rd record data
2549 handle_record_dht(void* cls, struct ResolverHandle *rh,
2550 unsigned int rd_count,
2551 const struct GNUNET_NAMESTORE_RecordData *rd)
2553 struct RecordLookupHandle* rlh = cls;
2557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2558 "GNS_PHASE_REC-%llu: No records for %s found in DHT. Aborting\n",
2560 /* give up, cannot resolve */
2561 finish_lookup (rh, rlh, 0, NULL);
2564 /* results found yay */
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2566 "GNS_PHASE_REC-%llu: Record resolved from DHT!", rh->id);
2567 finish_lookup (rh, rlh, rd_count, rd);
2574 * Process namestore lookup result for record.
2576 * @param cls the closure
2577 * @param rh resolver handle
2578 * @param rd_count number of results
2579 * @param rd record data
2582 handle_record_ns (void* cls, struct ResolverHandle *rh,
2583 unsigned int rd_count,
2584 const struct GNUNET_NAMESTORE_RecordData *rd)
2586 struct RecordLookupHandle* rlh = cls;
2587 int check_dht = GNUNET_YES;
2591 /* results found yay */
2592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2593 "GNS_PHASE_REC-%llu: Record resolved from namestore!\n", rh->id);
2594 finish_lookup (rh, rlh, rd_count, rd);
2598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2599 "GNS_PHASE_REC-%llu: NS returned no records. (status: %d)!\n",
2603 * There are 5 conditions that have to met for us to consult the DHT:
2604 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2605 * 2. No entry in the NS existed AND
2606 * 3. The zone queried is not the local resolver's zone AND
2607 * 4. The name that was looked up is '+'
2608 * because if it was any other canonical name we either already queried
2609 * the DHT for the authority in the authority lookup phase (and thus
2610 * would already have an entry in the NS for the record)
2611 * 5. We are not in cache only mode
2613 if ((0 != (rh->status & RSL_RECORD_EXPIRED)) &&
2614 (0 == (rh->status & RSL_RECORD_EXISTS)) )
2617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2618 "GNS_PHASE_REC-%llu: Not expired and exists!\n",
2620 check_dht = GNUNET_NO;
2623 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2624 &rh->private_local_zone))
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628 "GNS_PHASE_REC-%llu: Our zone!\n",
2630 check_dht = GNUNET_NO;
2633 if ((0 != strcmp (rh->name, "+")) && (GNUNET_YES == is_srv (rh->name)))
2634 check_dht = GNUNET_NO;
2636 if (GNUNET_YES == rh->only_cached)
2637 check_dht = GNUNET_NO;
2639 if (GNUNET_YES == check_dht)
2641 rh->proc = &handle_record_dht;
2642 resolve_record_dht(rh);
2645 /* give up, cannot resolve */
2646 finish_lookup (rh, rlh, 0, NULL);
2651 * Move one level up in the domain hierarchy and return the
2652 * passed top level domain.
2654 * @param name the domain
2655 * @param dest the destination where the tld will be put
2658 pop_tld(char* name, char* dest)
2662 if (GNUNET_YES == is_canonical (name))
2669 for (len = strlen(name); 0 < len; len--)
2671 if (*(name+len) == '.')
2681 strcpy(dest, (name+len+1));
2685 * Checks if name is in tld
2687 * @param name the name to check
2688 * @param tld the TLD to check for
2689 * @return GNUNET_YES or GNUNET_NO
2692 is_tld(const char* name, const char* tld)
2696 if (strlen(name) <= strlen(tld))
2699 offset = strlen(name)-strlen(tld);
2700 if (0 != strcmp(name+offset, tld))
2702 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2703 "%s is not in .%s TLD\n", name, tld);
2710 * DHT resolution for delegation finished. Processing result.
2712 * @param cls the closure
2713 * @param rh resolver handle
2714 * @param rd_count number of results (always 0)
2715 * @param rd record data (always NULL)
2718 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2719 unsigned int rd_count,
2720 const struct GNUNET_NAMESTORE_RecordData *rd)
2722 struct RecordLookupHandle* rlh;
2726 if (0 == strcmp(rh->name, ""))
2728 if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2730 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2731 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2733 finish_lookup(rh, rlh, rd_count, rd);
2736 /* We resolved full name for delegation. resolving record */
2737 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2738 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2740 strcpy(rh->name, "+\0");
2741 rh->proc = &handle_record_ns;
2742 resolve_record_ns(rh);
2747 * we still have some left
2749 if (GNUNET_YES == is_canonical (rh->name))
2751 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2752 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2755 rh->proc = &handle_record_ns;
2756 resolve_record_ns(rh);
2759 /* give up, cannot resolve */
2760 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2761 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2763 finish_lookup(rh, rlh, 0, NULL);
2768 * Start DHT lookup for a name -> PKEY (compare NS) record in
2769 * rh->authority's zone
2771 * @param rh the pending gns query
2774 resolve_delegation_dht(struct ResolverHandle *rh)
2777 struct GNUNET_HashCode lookup_key;
2778 struct ResolverHandle *rh_heap_root;
2780 pop_tld(rh->name, rh->authority_name);
2782 GNUNET_GNS_get_key_for_record (rh->authority_name, &rh->authority, &lookup_key);
2784 rh->dht_heap_node = NULL;
2786 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2788 rh->timeout_cont = &dht_authority_lookup_timeout;
2789 rh->timeout_cont_cls = rh;
2793 if (max_allowed_background_queries <=
2794 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2796 /* terminate oldest lookup */
2797 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2798 GNUNET_DHT_get_stop (rh_heap_root->get_handle);
2799 rh_heap_root->get_handle = NULL;
2800 rh_heap_root->dht_heap_node = NULL;
2802 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2803 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2804 rh->id, rh_heap_root->authority_name);
2806 rh_heap_root->proc(rh_heap_root->proc_cls,
2811 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2813 GNUNET_TIME_absolute_get().abs_value);
2816 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2817 GNUNET_assert(rh->get_handle == NULL);
2818 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2819 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2821 DHT_GNS_REPLICATION_LEVEL,
2822 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2825 &process_delegation_result_dht,
2831 * Namestore resolution for delegation finished. Processing result.
2833 * @param cls the closure
2834 * @param rh resolver handle
2835 * @param rd_count number of results (always 0)
2836 * @param rd record data (always NULL)
2839 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2840 unsigned int rd_count,
2841 const struct GNUNET_NAMESTORE_RecordData *rd)
2843 struct RecordLookupHandle* rlh;
2845 int check_dht = GNUNET_YES;
2848 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2849 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2850 rh->id, rh->status);
2852 if (rh->status & RSL_PKEY_REVOKED)
2854 finish_lookup (rh, rlh, 0, NULL);
2858 if (0 == strcmp(rh->name, ""))
2861 /* We resolved full name for delegation. resolving record */
2862 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2863 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2865 if (rh->status & RSL_CNAME_FOUND)
2867 if (GNUNET_GNS_RECORD_CNAME == rlh->record_type)
2869 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2870 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2872 strcpy (rh->name, rh->authority_name);
2873 finish_lookup (rh, rlh, rd_count, rd);
2878 if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_PLUS))
2880 s_len = strlen (rd->data) - 2;
2881 memcpy (rh->name, rd->data, s_len);
2882 rh->name[s_len] = '\0';
2883 resolve_delegation_ns (rh);
2886 else if (GNUNET_YES == is_tld ((char*)rd->data, GNUNET_GNS_TLD_ZKEY))
2888 gns_resolver_lookup_record (rh->authority,
2889 rh->private_local_zone,
2898 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
2899 free_resolver_handle (rh);
2905 strcpy (rh->dns_name, (char*)rd->data);
2906 resolve_dns_name (rh);
2911 else if (rh->status & RSL_DELEGATE_VPN)
2913 if (GNUNET_GNS_RECORD_VPN == rlh->record_type)
2915 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2916 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2918 finish_lookup(rh, rlh, rd_count, rd);
2921 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2922 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2924 GNUNET_assert (NULL != rd);
2925 rh->proc = &handle_record_vpn;
2926 resolve_record_vpn (rh, rd_count, rd);
2929 else if (rh->status & RSL_DELEGATE_NS)
2931 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2933 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2934 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2936 finish_lookup(rh, rlh, rd_count, rd);
2940 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2941 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2943 GNUNET_assert (NULL != rd);
2944 rh->proc = &handle_record_ns;
2945 resolve_record_dns (rh, rd_count, rd);
2948 else if (rh->status & RSL_DELEGATE_PKEY)
2950 if (rh->status & RSL_PKEY_REVOKED)
2952 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2953 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
2955 finish_lookup (rh, rlh, 0, NULL);
2958 else if (GNUNET_GNS_RECORD_PKEY == rlh->record_type)
2960 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2961 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
2963 finish_lookup(rh, rlh, rd_count, rd);
2967 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2968 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
2970 strcpy(rh->name, "+\0");
2971 rh->proc = &handle_record_ns;
2972 resolve_record_ns(rh);
2976 if (rh->status & RSL_DELEGATE_NS)
2978 if (GNUNET_GNS_RECORD_NS == rlh->record_type)
2980 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2981 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2983 finish_lookup(rh, rlh, rd_count, rd);
2987 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2988 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2990 GNUNET_assert (NULL != rd);
2991 rh->proc = &handle_record_ns;
2992 resolve_record_dns (rh, rd_count, rd);
2997 * we still have some left
2998 * check if authority in ns is fresh
3000 * or we are authority
3003 if ((rh->status & RSL_RECORD_EXISTS) &&
3004 !(rh->status & RSL_RECORD_EXPIRED))
3005 check_dht = GNUNET_NO;
3007 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3008 &rh->private_local_zone))
3009 check_dht = GNUNET_NO;
3011 if (GNUNET_YES == rh->only_cached)
3012 check_dht = GNUNET_NO;
3014 if (GNUNET_YES == check_dht)
3017 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3018 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
3020 rh->proc = &handle_delegation_dht;
3021 resolve_delegation_dht(rh);
3025 if (GNUNET_NO == is_canonical (rh->name))
3027 /* give up, cannot resolve */
3028 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3029 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
3032 finish_lookup(rh, rlh, rd_count, rd);
3035 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3036 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
3039 rh->proc = &handle_record_ns;
3040 resolve_record_ns(rh);
3045 * This is a callback function that should give us only PKEY
3046 * records. Used to query the namestore for the authority (PKEY)
3047 * for 'name'. It will recursively try to resolve the
3048 * authority for a given name from the namestore.
3050 * @param cls the pending query
3051 * @param key the key of the zone we did the lookup
3052 * @param expiration expiration date of the record data set in the namestore
3053 * @param name the name for which we need an authority
3054 * @param rd_count the number of records with 'name'
3055 * @param rd the record data
3056 * @param signature the signature of the authority for the record data
3059 process_delegation_result_ns (void* cls,
3060 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
3061 struct GNUNET_TIME_Absolute expiration,
3063 unsigned int rd_count,
3064 const struct GNUNET_NAMESTORE_RecordData *rd,
3065 const struct GNUNET_CRYPTO_RsaSignature *signature)
3067 struct ResolverHandle *rh;
3068 struct GNUNET_TIME_Relative remaining_time;
3069 struct GNUNET_CRYPTO_ShortHashCode zone;
3070 char new_name[MAX_DNS_NAME_LENGTH];
3072 struct GNUNET_TIME_Absolute et;
3074 rh = (struct ResolverHandle *)cls;
3075 rh->namestore_task = NULL;
3076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3077 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
3080 GNUNET_CRYPTO_short_hash (key,
3081 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3083 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3090 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
3092 rh->status |= RSL_RECORD_EXISTS;
3094 if (remaining_time.rel_value == 0)
3096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3097 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
3099 rh->status |= RSL_RECORD_EXPIRED;
3104 * No authority found in namestore.
3109 * We did not find an authority in the namestore
3114 * Promote this authority back to a name maybe it is
3117 if (strcmp (rh->name, "") == 0)
3119 /* simply promote back */
3120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3121 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3122 rh->id, rh->authority_name);
3123 strcpy (rh->name, rh->authority_name);
3127 /* add back to existing name */
3128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3129 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3130 rh->id, rh->authority_name, rh->name);
3131 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3132 rh->name, rh->authority_name);
3133 strcpy (rh->name, new_name);
3134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3135 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n",
3139 rh->proc (rh->proc_cls, rh, 0, NULL);
3144 * We found an authority that may be able to help us
3145 * move on with query
3146 * Note only 1 pkey should have been returned.. anything else would be strange
3148 for (i=0; i < rd_count;i++)
3152 * A CNAME. Like regular DNS this means the is no other record for this
3155 if (rd[i].record_type == GNUNET_GNS_RECORD_CNAME)
3157 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3158 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3161 rh->status |= RSL_CNAME_FOUND;
3162 rh->proc (rh->proc_cls, rh, rd_count, rd);
3169 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3171 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3172 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3174 rh->status |= RSL_DELEGATE_VPN;
3175 rh->proc (rh->proc_cls, rh, rd_count, rd);
3181 * FIXME make optional
3183 if (rd[i].record_type == GNUNET_GNS_RECORD_NS)
3185 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3186 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3188 rh->status |= RSL_DELEGATE_NS;
3189 rh->proc (rh->proc_cls, rh, rd_count, rd);
3193 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3196 rh->status |= RSL_DELEGATE_PKEY;
3198 if ((ignore_pending_records != 0) &&
3199 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3202 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3208 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3209 et.abs_value = rd[i].expiration_time;
3210 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3214 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3216 if (remaining_time.rel_value == 0)
3218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3219 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3221 rh->authority_chain_head->fresh = 0;
3222 rh->proc (rh->proc_cls, rh, 0, NULL);
3230 * Resolve rest of query with new authority
3232 GNUNET_assert (rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3233 memcpy (&rh->authority, rd[i].data,
3234 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3235 struct AuthorityChain *auth = GNUNET_malloc(sizeof (struct AuthorityChain));
3236 auth->zone = rh->authority;
3237 memset (auth->name, 0, strlen (rh->authority_name)+1);
3238 strcpy (auth->name, rh->authority_name);
3239 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3240 rh->authority_chain_tail,
3242 if (NULL != rh->rd.data)
3243 GNUNET_free ((void*)(rh->rd.data));
3245 memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
3246 rh->rd.data = GNUNET_malloc (rd[i].data_size);
3247 memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
3249 /* Check for key revocation and delegate */
3250 rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
3253 GNUNET_GNS_RECORD_REV,
3254 &process_pkey_revocation_result_ns,
3263 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3264 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3266 * If we have found some records for the LAST label
3267 * we return the results. Else null.
3269 if (strcmp (rh->name, "") == 0)
3271 /* Start shortening */
3272 if ((rh->priv_key != NULL) &&
3273 (is_canonical (rh->name) == GNUNET_YES))
3275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3276 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3278 start_shorten (rh->authority_chain_head,
3281 /* simply promote back */
3282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3283 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3284 rh->id, rh->authority_name);
3285 strcpy (rh->name, rh->authority_name);
3286 rh->proc (rh->proc_cls, rh, rd_count, rd);
3290 GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
3291 "%s.%s", rh->name, rh->authority_name);
3292 strcpy (rh->name, new_name);
3293 rh->proc (rh->proc_cls, rh, 0, NULL);
3299 * Resolve the delegation chain for the request in our namestore
3301 * @param rh the resolver handle
3304 resolve_delegation_ns (struct ResolverHandle *rh)
3306 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3307 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3309 pop_tld(rh->name, rh->authority_name);
3310 rh->namestore_task = GNUNET_NAMESTORE_lookup_record(namestore_handle,
3313 GNUNET_GNS_RECORD_ANY,
3314 &process_delegation_result_ns,
3321 * Lookup of a record in a specific zone
3322 * calls lookup result processor on result
3324 * @param zone the root zone
3325 * @param pzone the private local zone
3326 * @param record_type the record type to look up
3327 * @param name the name to look up
3328 * @param key a private key for use with PSEU import (can be NULL)
3329 * @param timeout timeout for resolution
3330 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3331 * @param proc the processor to call on result
3332 * @param cls the closure to pass to proc
3335 gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
3336 struct GNUNET_CRYPTO_ShortHashCode pzone,
3337 uint32_t record_type,
3339 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3340 struct GNUNET_TIME_Relative timeout,
3342 RecordLookupProcessor proc,
3345 struct ResolverHandle *rh;
3346 struct RecordLookupHandle* rlh;
3347 char string_hash[MAX_DNS_LABEL_LENGTH];
3348 char nzkey[MAX_DNS_LABEL_LENGTH];
3349 char* nzkey_ptr = nzkey;
3351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3352 "Starting resolution for %s (type=%d)!\n",
3356 if ((is_canonical ((char*)name) == GNUNET_YES) &&
3357 (strcmp(GNUNET_GNS_TLD, name) != 0))
3359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3360 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3365 rlh = GNUNET_malloc (sizeof(struct RecordLookupHandle));
3366 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3368 memset (rh, 0, sizeof (struct ResolverHandle));
3369 rh->authority = zone;
3373 rh->timeout = timeout;
3374 rh->get_handle = NULL;
3375 rh->private_local_zone = pzone;
3376 rh->only_cached = only_cached;
3377 rh->namestore_task = NULL;
3380 GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
3384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3385 "No shorten key for resolution\n");
3388 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3391 * Set timeout for authority lookup phase to 1/2
3393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3394 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3395 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3396 GNUNET_TIME_relative_divide(timeout, 2),
3397 &handle_lookup_timeout,
3399 rh->timeout_cont = &dht_authority_lookup_timeout;
3400 rh->timeout_cont_cls = rh;
3404 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3405 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3408 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3411 * Only 'gnunet' given
3413 strcpy(rh->name, "\0");
3417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3418 "Checking for TLD...\n");
3419 if (is_zkey_tld(name) == GNUNET_YES)
3421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3424 * This is a zkey tld
3425 * build hash and use as initial authority
3428 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3429 memcpy(rh->name, name,
3430 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3431 pop_tld(rh->name, string_hash);
3433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3434 "ZKEY is %s!\n", string_hash);
3436 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3438 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3442 "Cannot convert ZKEY `%s' to hash!\n", string_hash);
3444 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3445 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3446 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3449 proc (cls, 0, NULL);
3454 else if (is_gnunet_tld (name) == GNUNET_YES)
3456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3459 * Presumably GNUNET tld
3461 memset (rh->name, 0,
3462 strlen(name)-strlen(GNUNET_GNS_TLD));
3463 memcpy (rh->name, name,
3464 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3469 "Cannot handle this TLD %s\n", string_hash);
3471 if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
3472 GNUNET_SCHEDULER_cancel (rh->timeout_task);
3473 GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
3476 proc (cls, 0, NULL);
3482 * Initialize authority chain
3484 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3485 rh->authority_chain_head->prev = NULL;
3486 rh->authority_chain_head->next = NULL;
3487 rh->authority_chain_tail = rh->authority_chain_head;
3488 rh->authority_chain_head->zone = rh->authority;
3489 strcpy (rh->authority_chain_head->name, "");
3492 * Copy original query into lookup handle
3494 rlh->record_type = record_type;
3495 memset(rlh->name, 0, strlen(name) + 1);
3496 strcpy(rlh->name, name);
3498 rlh->proc_cls = cls;
3500 rh->proc = &handle_delegation_ns;
3501 resolve_delegation_ns(rh);
3504 /******** END Record Resolver ***********/
3507 finish_shorten (struct ResolverHandle *rh,
3508 struct NameShortenHandle *nsh)
3510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3511 "Sending %s as shorten result\n", nsh->result);
3512 nsh->proc (nsh->proc_cls, nsh->result);
3513 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
3515 free_resolver_handle (rh);
3520 * Callback calles by namestore for a zone to name
3523 * @param cls the closure
3524 * @param zone_key the zone we queried
3525 * @param expire the expiration time of the name
3526 * @param name the name found or NULL
3527 * @param rd_len number of records for the name
3528 * @param rd the record data (PKEY) for the name
3529 * @param signature the signature for the record data
3532 process_zone_to_name_shorten_root (void *cls,
3533 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3534 struct GNUNET_TIME_Absolute expire,
3536 unsigned int rd_len,
3537 const struct GNUNET_NAMESTORE_RecordData *rd,
3538 const struct GNUNET_CRYPTO_RsaSignature *signature);
3542 * Callback called by namestore for a zone to name
3545 * @param cls the closure
3546 * @param zone_key the zone we queried
3547 * @param expire the expiration time of the name
3548 * @param name the name found or NULL
3549 * @param rd_len number of records for the name
3550 * @param rd the record data (PKEY) for the name
3551 * @param signature the signature for the record data
3554 process_zone_to_name_shorten_shorten (void *cls,
3555 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3556 struct GNUNET_TIME_Absolute expire,
3558 unsigned int rd_len,
3559 const struct GNUNET_NAMESTORE_RecordData *rd,
3560 const struct GNUNET_CRYPTO_RsaSignature *signature)
3562 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3563 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3564 struct AuthorityChain *next_authority;
3566 char result[MAX_DNS_NAME_LENGTH];
3567 char tmp_name[MAX_DNS_NAME_LENGTH];
3570 rh->namestore_task = NULL;
3571 /* we found a match in our own root zone */
3574 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3575 memset(result, 0, answer_len);
3577 if (strlen(rh->name) > 0)
3579 sprintf (result, "%s.%s.%s.%s",
3581 nsh->shorten_zone_name,
3586 sprintf (result, "%s.%s.%s", name,
3587 nsh->shorten_zone_name,
3591 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3592 "Found shorten result %s\n", result);
3593 if (strlen (nsh->result) > strlen (result))
3594 strcpy (nsh->result, result);
3596 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3597 nsh->shorten_zone) == 0)
3600 * This is our zone append .gads unless name is empty
3601 * (it shouldn't be, usually FIXME what happens if we
3602 * shorten to our zone to a "" record??)
3605 sprintf (result, "%s.%s.%s",
3607 nsh->shorten_zone_name,
3609 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3610 "Our zone: Found %s as shorten result\n", result);
3612 if (strlen (nsh->result) > strlen (result))
3613 strcpy (nsh->result, result);
3614 //nsh->proc(nsh->proc_cls, result);
3616 //free_resolver_handle(rh);
3623 * continue with next authority if exists
3625 if ((rh->authority_chain_head->next == NULL))
3627 finish_shorten (rh, nsh);
3630 next_authority = rh->authority_chain_head;
3632 if (0 == strcmp (rh->name, ""))
3633 strcpy (tmp_name, next_authority->name);
3635 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3636 "%s.%s", rh->name, next_authority->name);
3638 strcpy(rh->name, tmp_name);
3639 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3640 "No PSEU found for authority %s. Promoting back: %s\n",
3641 next_authority->name, rh->name);
3643 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3644 rh->authority_chain_tail,
3647 GNUNET_free (next_authority);
3649 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3650 &rh->authority_chain_tail->zone,
3651 &rh->authority_chain_head->zone,
3652 &process_zone_to_name_shorten_root,
3657 * Callback calles by namestore for a zone to name
3660 * @param cls the closure
3661 * @param zone_key the zone we queried
3662 * @param expire the expiration time of the name
3663 * @param name the name found or NULL
3664 * @param rd_len number of records for the name
3665 * @param rd the record data (PKEY) for the name
3666 * @param signature the signature for the record data
3669 process_zone_to_name_shorten_private (void *cls,
3670 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3671 struct GNUNET_TIME_Absolute expire,
3673 unsigned int rd_len,
3674 const struct GNUNET_NAMESTORE_RecordData *rd,
3675 const struct GNUNET_CRYPTO_RsaSignature *signature)
3677 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3678 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3679 struct AuthorityChain *next_authority;
3681 char result[MAX_DNS_NAME_LENGTH];
3682 char tmp_name[MAX_DNS_NAME_LENGTH];
3685 rh->namestore_task = NULL;
3686 /* we found a match in our own root zone */
3689 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3690 memset(result, 0, answer_len);
3692 if (strlen(rh->name) > 0)
3694 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3698 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3701 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3702 "Found shorten result %s\n", result);
3703 if (strlen (nsh->result) > strlen (result))
3704 strcpy (nsh->result, result);
3706 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3707 nsh->private_zone) == 0)
3710 * This is our zone append .gads unless name is empty
3711 * (it shouldn't be, usually FIXME what happens if we
3712 * shorten to our zone to a "" record??)
3715 sprintf (result, "%s.%s.%s",
3716 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3717 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3718 "Our private zone: Found %s as shorten result %s\n", result);
3719 if (strlen (nsh->result) > strlen (result))
3720 strcpy (nsh->result, result);
3723 if (0 != strcmp (nsh->shorten_zone_name, ""))
3725 /* backtrack authorities for names in priv zone */
3726 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3728 &rh->authority_chain_head->zone,
3729 &process_zone_to_name_shorten_shorten,
3736 * continue with next authority if exists
3738 if ((rh->authority_chain_head->next == NULL))
3740 finish_shorten (rh, nsh);
3743 next_authority = rh->authority_chain_head;
3745 if (0 == strcmp (rh->name, ""))
3746 strcpy (tmp_name, next_authority->name);
3748 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3749 "%s.%s", rh->name, next_authority->name);
3751 strcpy(rh->name, tmp_name);
3752 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3753 "No PSEU found for authority %s. Promoting back: %s\n",
3754 next_authority->name, rh->name);
3756 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3757 rh->authority_chain_tail,
3760 GNUNET_free (next_authority);
3762 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3763 &rh->authority_chain_tail->zone,
3764 &rh->authority_chain_head->zone,
3765 &process_zone_to_name_shorten_root,
3771 * Callback calles by namestore for a zone to name
3774 * @param cls the closure
3775 * @param zone_key the zone we queried
3776 * @param expire the expiration time of the name
3777 * @param name the name found or NULL
3778 * @param rd_len number of records for the name
3779 * @param rd the record data (PKEY) for the name
3780 * @param signature the signature for the record data
3783 process_zone_to_name_shorten_root (void *cls,
3784 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3785 struct GNUNET_TIME_Absolute expire,
3787 unsigned int rd_len,
3788 const struct GNUNET_NAMESTORE_RecordData *rd,
3789 const struct GNUNET_CRYPTO_RsaSignature *signature)
3791 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3792 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3793 struct AuthorityChain *next_authority;
3795 char result[MAX_DNS_NAME_LENGTH];
3796 char tmp_name[MAX_DNS_NAME_LENGTH];
3799 rh->namestore_task = NULL;
3800 /* we found a match in our own root zone */
3803 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3804 memset(result, 0, answer_len);
3806 if (strlen(rh->name) > 0)
3808 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3812 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3815 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3816 "Found shorten result %s\n", result);
3817 if (strlen (nsh->result) > strlen (result))
3818 strcpy (nsh->result, result);
3820 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3821 nsh->root_zone) == 0)
3824 * This is our zone append .gads unless name is empty
3825 * (it shouldn't be, usually FIXME what happens if we
3826 * shorten to our zone to a "" record??)
3829 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3830 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3831 "Our zone: Found %s as shorten result\n", result);
3832 if (strlen (nsh->result) > strlen (result))
3833 strcpy (nsh->result, result);
3836 if (NULL != nsh->private_zone)
3838 /* backtrack authorities for names in priv zone */
3839 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3841 &rh->authority_chain_head->zone,
3842 &process_zone_to_name_shorten_private,
3845 else if (NULL != nsh->shorten_zone)
3847 /* backtrack authorities for names in shorten zone */
3848 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3850 &rh->authority_chain_head->zone,
3851 &process_zone_to_name_shorten_shorten,
3858 * continue with next authority if exists
3860 if ((rh->authority_chain_head->next == NULL))
3862 finish_shorten (rh, nsh);
3865 next_authority = rh->authority_chain_head;
3867 if (0 == strcmp (rh->name, ""))
3868 strcpy (tmp_name, next_authority->name);
3870 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3871 "%s.%s", rh->name, next_authority->name);
3873 strcpy(rh->name, tmp_name);
3874 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3875 "No PSEU found for authority %s. Promoting back: %s\n",
3876 next_authority->name, rh->name);
3878 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3879 rh->authority_chain_tail,
3882 GNUNET_free (next_authority);
3884 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3885 &rh->authority_chain_tail->zone,
3886 &rh->authority_chain_head->zone,
3887 &process_zone_to_name_shorten_root,
3894 * Process result from namestore delegation lookup
3895 * for shorten operation
3897 * @param cls the client shorten handle
3898 * @param rh the resolver handle
3899 * @param rd_count number of results (0)
3900 * @param rd data (NULL)
3903 handle_delegation_ns_shorten (void* cls,
3904 struct ResolverHandle *rh,
3906 const struct GNUNET_NAMESTORE_RecordData *rd)
3908 struct NameShortenHandle *nsh;
3909 char result[MAX_DNS_NAME_LENGTH];
3911 nsh = (struct NameShortenHandle *)cls;
3912 rh->namestore_task = NULL;
3914 * At this point rh->name contains the part of the name
3915 * that we do not have a PKEY in our namestore to resolve.
3916 * The authority chain in the resolver handle is now
3917 * useful to backtrack if needed
3920 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3921 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3922 memset(result, 0, sizeof (result));
3924 if (0 == GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3928 * This is our zone append .gads unless name is empty
3929 * (it shouldn't be, usually FIXME what happens if we
3930 * shorten to our zone to a "" record??)
3933 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3934 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3935 "Our zone: Found %s as shorten result\n", result);
3937 if (strlen (nsh->result) > strlen (result))
3938 strcpy (nsh->result, result);
3941 else if (NULL != nsh->private_zone)
3944 * This is our zone append .gads unless name is empty
3945 * (it shouldn't be, usually FIXME what happens if we
3946 * shorten to our zone to a "" record??)
3948 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3949 nsh->private_zone) == 0)
3952 sprintf (result, "%s.%s.%s",
3953 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3954 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3955 "Our zone: Found %s as shorten result in private zone %s\n",
3958 if (strlen (nsh->result) > strlen (result))
3959 strcpy (nsh->result, result);
3962 else if (NULL != nsh->shorten_zone)
3965 * This is our zone append .gads unless name is empty
3966 * (it shouldn't be, usually FIXME what happens if we
3967 * shorten to our zone to a "" record??)
3969 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3970 nsh->shorten_zone) == 0)
3972 sprintf (result, "%s.%s.%s",
3973 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3974 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3975 "Our zone: Found %s as shorten result in shorten zone\n",
3978 if (strlen (nsh->result) > strlen (result))
3979 strcpy (nsh->result, result);
3984 /* backtrack authorities for names */
3985 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3987 &rh->authority_chain_head->zone,
3988 &process_zone_to_name_shorten_root,
3995 * Callback calles by namestore for a zone to name
3998 * @param cls the closure
3999 * @param zone_key the zone we queried
4000 * @param expire the expiration time of the name
4001 * @param name the name found or NULL
4002 * @param rd_len number of records for the name
4003 * @param rd the record data (PKEY) for the name
4004 * @param signature the signature for the record data
4007 process_zone_to_name_zkey(void *cls,
4008 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
4009 struct GNUNET_TIME_Absolute expire,
4011 unsigned int rd_len,
4012 const struct GNUNET_NAMESTORE_RecordData *rd,
4013 const struct GNUNET_CRYPTO_RsaSignature *signature)
4015 struct ResolverHandle *rh = cls;
4016 struct NameShortenHandle *nsh = rh->proc_cls;
4017 char new_name[MAX_DNS_NAME_LENGTH];
4019 rh->namestore_task = NULL;
4021 /* zkey not in our zone */
4025 * In this case we have not given this PKEY a name (yet)
4026 * It is either just not in our zone or not even cached
4027 * Since we do not know at this point we will not try to shorten
4028 * because PKEY import will happen if the user follows the zkey
4031 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4032 "No name found for zkey %s returning verbatim!\n", nsh->result);
4033 /*if (strcmp(rh->name, "") != 0)
4034 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
4035 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
4037 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4038 enc, GNUNET_GNS_TLD_ZKEY);
4040 strcpy (nsh->result, new_name);*/
4042 finish_shorten (rh, nsh);
4046 if (strcmp(rh->name, "") != 0)
4047 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
4050 strcpy(new_name, name);
4052 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4053 "Continue shorten for %s!\n", new_name);
4055 strcpy(rh->name, new_name);
4057 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4058 rh->authority_chain_tail = rh->authority_chain_head;
4059 rh->authority_chain_head->zone = rh->authority;
4062 /* Start delegation resolution in our namestore */
4063 resolve_delegation_ns (rh);
4068 * Shorten api from resolver
4070 * @param zone the root zone to use
4071 * @param pzone the private zone to use
4072 * @param szone the shorten zone to use
4073 * @param name the name to shorten
4074 * @param private_zone_name name of the private zone
4075 * @param shorten_zone_name name of the shorten zone
4076 * @param proc the processor to call with result
4077 * @param proc_cls closure to pass to proc
4080 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
4081 struct GNUNET_CRYPTO_ShortHashCode *pzone,
4082 struct GNUNET_CRYPTO_ShortHashCode *szone,
4084 const char* private_zone_name,
4085 const char* shorten_zone_name,
4086 ShortenResultProcessor proc,
4089 struct ResolverHandle *rh;
4090 struct NameShortenHandle *nsh;
4091 char string_hash[MAX_DNS_LABEL_LENGTH];
4092 struct GNUNET_CRYPTO_ShortHashCode zkey;
4093 char nzkey[MAX_DNS_LABEL_LENGTH];
4094 char* nzkey_ptr = nzkey;
4097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4098 "Starting shorten for %s!\n", name);
4100 if (is_canonical ((char*)name) == GNUNET_YES)
4102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4103 "%s is canonical. Returning verbatim\n", name);
4104 proc (proc_cls, name);
4108 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
4111 nsh->proc_cls = proc_cls;
4112 nsh->root_zone = zone;
4113 nsh->private_zone = pzone;
4114 nsh->shorten_zone = szone;
4115 strcpy (nsh->private_zone_name, private_zone_name);
4116 strcpy (nsh->shorten_zone_name, shorten_zone_name);
4117 strcpy (nsh->result, name);
4120 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
4121 rh->authority = *zone;
4123 rh->priv_key = NULL;
4124 rh->namestore_task = NULL;
4125 rh->proc = &handle_delegation_ns_shorten;
4128 rh->private_local_zone = *zone;
4130 GNUNET_CONTAINER_DLL_insert (nsh_head, nsh_tail, rh);
4132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4133 "Checking for TLD...\n");
4134 if (is_zkey_tld (name) == GNUNET_YES)
4136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4139 * This is a zkey tld
4140 * build hash and use as initial authority
4143 memset (rh->name, 0,
4144 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
4145 memcpy (rh->name, name,
4146 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
4147 pop_tld (rh->name, string_hash);
4149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4150 "ZKEY is %s!\n", string_hash);
4152 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
4154 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
4157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4158 "Cannot convert ZKEY %s to hash!\n", nzkey);
4159 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4162 proc (proc_cls, name);
4166 rh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
4169 &process_zone_to_name_zkey,
4174 else if (is_gnunet_tld (name) == GNUNET_YES)
4176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4179 * Presumably GNUNET tld
4181 memset (rh->name, 0,
4182 strlen (name)-strlen (GNUNET_GNS_TLD));
4183 memcpy (rh->name, name,
4184 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
4189 GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
4192 proc (proc_cls, name);
4196 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4197 rh->authority_chain_tail = rh->authority_chain_head;
4198 rh->authority_chain_head->zone = *zone;
4201 /* Start delegation resolution in our namestore */
4202 resolve_delegation_ns (rh);
4205 /*********** END NAME SHORTEN ********************/
4208 * Conclude get authority lookup
4210 * @param rh resolver handle
4211 * @param nah get authority lookup handle
4214 finish_get_auth (struct ResolverHandle *rh,
4215 struct GetNameAuthorityHandle *nah)
4217 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4218 "Got authority result %s\n", nah->result);
4220 nah->proc (nah->proc_cls, nah->result);
4221 GNUNET_CONTAINER_DLL_remove (nah_head, nah_tail, rh);
4223 free_resolver_handle (rh);
4227 * Process result from namestore delegation lookup
4228 * for get authority operation
4230 * @param cls the client get auth handle
4231 * @param rh the resolver handle
4232 * @param rd_count number of results (0)
4233 * @param rd data (NULL)
4236 handle_delegation_result_ns_get_auth(void* cls,
4237 struct ResolverHandle *rh,
4239 const struct GNUNET_NAMESTORE_RecordData *rd)
4241 struct GetNameAuthorityHandle* nah;
4244 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4247 * At this point rh->name contains the part of the name
4248 * that we do not have a PKEY in our namestore to resolve.
4249 * The authority chain in the resolver handle is now
4250 * useful to backtrack if needed
4253 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4254 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4256 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4257 "Building response!\n");
4258 if (is_canonical (rh->name) == GNUNET_YES)
4261 * We successfully resolved the authority in the ns
4262 * FIXME for our purposes this is fine
4263 * but maybe we want to have an api that also looks
4264 * into the dht (i.e. option in message)
4266 if (strlen(rh->name) > strlen(nah->name))
4268 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4269 "Record name longer than original lookup name... odd!\n");
4273 answer_len = strlen(nah->name) - strlen(rh->name)
4274 + strlen(GNUNET_GNS_TLD) + 1;
4275 memset(nah->result, 0, answer_len);
4276 if (0 != strcmp (rh->name, ""))
4277 strcpy(nah->result, nah->name + strlen(rh->name) + 1);
4279 strcpy(nah->result, nah->name);
4281 finish_get_auth (rh, nah);
4285 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4286 "Unable to resolve authority for remaining %s!\n", rh->name);
4287 strcpy(nah->result, "");
4288 finish_get_auth (rh, nah);
4296 * Tries to resolve the authority for name
4299 * @param zone the root zone to look up for
4300 * @param pzone the private local zone
4301 * @param name the name to lookup up
4302 * @param proc the processor to call when finished
4303 * @param proc_cls the closure to pass to the processor
4306 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4307 struct GNUNET_CRYPTO_ShortHashCode pzone,
4309 GetAuthorityResultProcessor proc,
4312 struct ResolverHandle *rh;
4313 struct GetNameAuthorityHandle *nah;
4315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4316 "Starting authority resolution for %s!\n", name);
4318 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4319 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4320 rh->authority = zone;
4322 rh->private_local_zone = pzone;
4323 rh->namestore_task = NULL;
4325 GNUNET_CONTAINER_DLL_insert (nah_head, nah_tail, rh);
4327 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4329 strcpy(rh->name, "\0");
4334 strlen(name)-strlen(GNUNET_GNS_TLD));
4335 memcpy(rh->name, name,
4336 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4339 memset(nah->name, 0,
4341 strcpy(nah->name, name);
4343 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4344 rh->authority_chain_tail = rh->authority_chain_head;
4345 rh->authority_chain_head->zone = zone;
4346 rh->proc = &handle_delegation_result_ns_get_auth;
4347 rh->proc_cls = (void*)nah;
4350 nah->proc_cls = proc_cls;
4351 strcpy (nah->result, "");
4353 /* Start delegation resolution in our namestore */
4354 resolve_delegation_ns(rh);
4358 /******** END GET AUTHORITY *************/
4360 /* end of gnunet-service-gns_resolver.c */