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.
24 * @file gns/gnunet-service-gns_resolver.c
25 * @brief GNUnet GNS resolver logic
26 * @author Martin Schanzenbach
29 #include "gnunet_util_lib.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_dns_service.h"
32 #include "gnunet_dht_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_vpn_service.h"
35 #include "gnunet_dns_service.h"
36 #include "gnunet_resolver_service.h"
37 #include "gnunet_dnsparser_lib.h"
38 #include "gnunet_gns_service.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 * Maximum amount of parallel queries in background
71 static unsigned long long max_allowed_background_queries;
74 * Wheather or not to ignore pending records
76 static int ignore_pending_records;
81 static struct GNUNET_CRYPTO_ShortHashCode local_zone;
84 * Background shortening handles
86 static struct GetPseuAuthorityHandle *gph_head;
89 * Background shortening handles
91 static struct GetPseuAuthorityHandle *gph_tail;
94 * a resolution identifier pool variable
96 * This is a non critical identifier useful for debugging
98 static unsigned long long rid = 0;
102 * Determine if this name is canonical.
104 * a.b.gnunet = not canonical
107 * @param name the name to test
108 * @return 1 if canonical
111 is_canonical(char* name)
113 uint32_t len = strlen(name);
116 for (i=0; i<len; i++)
118 if (*(name+i) == '.')
126 * Callback that shortens authorities
128 * @param gph the handle containing the name to shorten
131 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
135 * Namestore calls this function if we have record for this name.
136 * (or with rd_count=0 to indicate no matches)
138 * @param cls the pending query
139 * @param key the key of the zone we did the lookup
140 * @param expiration expiration date of the namestore entry
141 * @param name the name for which we need an authority
142 * @param rd_count the number of records with 'name'
143 * @param rd the record data
144 * @param signature the signature of the authority for the record data
147 process_pseu_lookup_ns (void* cls,
148 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
149 struct GNUNET_TIME_Absolute expiration,
150 const char *name, unsigned int rd_count,
151 const struct GNUNET_NAMESTORE_RecordData *rd,
152 const struct GNUNET_CRYPTO_RsaSignature *signature)
154 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
155 struct GNUNET_NAMESTORE_RecordData new_pkey;
156 struct AuthorityChain *iter;
160 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
161 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
162 if (0 == strcmp (gph->name, name))
164 if (gph->ahead->next != NULL)
166 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
170 iter = gph->ahead->next;
171 GNUNET_free (gph->ahead);
173 shorten_authority_chain (gph);
181 iter = gph->ahead->next;
182 GNUNET_free (gph->ahead);
184 } while (iter != NULL);
185 GNUNET_CRYPTO_rsa_key_free (gph->key);
186 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 "GNS_AUTO_PSEU: Trying delegated name %s\n", gph->name);
193 memcpy (gph->test_name, gph->name, strlen (gph->name)+1);
194 GNUNET_NAMESTORE_lookup_record (namestore_handle,
197 GNUNET_NAMESTORE_TYPE_ANY,
198 &process_pseu_lookup_ns,
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
207 new_pkey.expiration_time = UINT64_MAX;
208 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
209 new_pkey.data = &gph->ahead->zone;
210 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
211 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
212 | GNUNET_NAMESTORE_RF_PRIVATE
213 | GNUNET_NAMESTORE_RF_PENDING;
214 GNUNET_NAMESTORE_record_create (namestore_handle,
222 iter = gph->ahead->next;
223 GNUNET_free (gph->ahead);
225 } while (iter != NULL);
226 GNUNET_CRYPTO_rsa_key_free (gph->key);
227 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
233 * process result of a dht pseu lookup
235 * @param gph the handle
236 * @param name the pseu result or NULL
239 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
243 memcpy (gph->test_name, gph->ahead->name, strlen (gph->ahead->name)+1);
247 memcpy (gph->test_name, name, strlen(name)+1);
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
255 * Check for collision
257 GNUNET_NAMESTORE_lookup_record (namestore_handle,
260 GNUNET_NAMESTORE_TYPE_ANY,
261 &process_pseu_lookup_ns,
266 * Handle timeout for dht request
268 * @param cls the request handle as closure
269 * @param tc the task context
272 handle_auth_discovery_timeout(void *cls,
273 const struct GNUNET_SCHEDULER_TaskContext *tc)
275 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
276 struct AuthorityChain *iter;
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
280 GNUNET_DHT_get_stop (gph->get_handle);
281 gph->get_handle = NULL;
283 if (gph->ahead->next != NULL)
285 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
289 iter = gph->ahead->next;
290 GNUNET_free (gph->ahead);
292 shorten_authority_chain (gph);
297 process_pseu_result (gph, NULL);
301 * Function called when we find a PSEU entry in the DHT
303 * @param cls the request handle
304 * @param exp lifetime
305 * @param key the key the record was stored under
306 * @param get_path get path
307 * @param get_path_length get path length
308 * @param put_path put path
309 * @param put_path_length put path length
310 * @param type the block type
311 * @param size the size of the record
312 * @param data the record data
315 process_auth_discovery_dht_result(void* cls,
316 struct GNUNET_TIME_Absolute exp,
317 const struct GNUNET_HashCode * key,
318 const struct GNUNET_PeerIdentity *get_path,
319 unsigned int get_path_length,
320 const struct GNUNET_PeerIdentity *put_path,
321 unsigned int put_path_length,
322 enum GNUNET_BLOCK_Type type,
323 size_t size, const void *data)
325 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
326 struct AuthorityChain *iter;
327 struct GNSNameRecordBlock *nrb;
328 char* rd_data = (char*)data;
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
338 /* stop lookup and timeout task */
339 GNUNET_DHT_get_stop (gph->get_handle);
340 gph->get_handle = NULL;
341 GNUNET_SCHEDULER_cancel (gph->timeout);
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346 "GNS_GET_AUTH: got dht result null!\n", size);
350 iter = gph->ahead->next;
351 GNUNET_free (gph->ahead);
353 } while (iter != NULL);
354 GNUNET_CRYPTO_rsa_key_free (gph->key);
355 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
360 nrb = (struct GNSNameRecordBlock*)data;
364 nrb = (struct GNSNameRecordBlock*)data;
366 name = (char*)&nrb[1];
367 num_records = ntohl (nrb->rd_count);
369 struct GNUNET_NAMESTORE_RecordData rd[num_records];
371 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
372 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
374 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
380 "GNS_GET_AUTH: Error deserializing data!\n");
384 for (i=0; i < num_records; i++)
386 if ((strcmp (name, "+") == 0) &&
387 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
390 process_pseu_result (gph, (char*)rd[i].data);
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in dht!\n");
399 if (gph->ahead->next != NULL)
401 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
405 iter = gph->ahead->next;
406 GNUNET_free (gph->ahead);
408 shorten_authority_chain (gph);
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "GNS_GET_AUTH: finished shorten, no results!\n");
414 process_pseu_result (gph, NULL);
418 * Process PSEU discovery for shorten via namestore
420 * @param cls the GetPseuAuthorityHandle
421 * @param key the public key
422 * @param expiration recorddata expiration
423 * @param name the looked up name
424 * @param rd_count number of records in set
425 * @param rd record data
426 * @param signature the signature
429 process_auth_discovery_ns_result(void* cls,
430 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
431 struct GNUNET_TIME_Absolute expiration,
433 unsigned int rd_count,
434 const struct GNUNET_NAMESTORE_RecordData *rd,
435 const struct GNUNET_CRYPTO_RsaSignature *signature)
438 struct GNUNET_CRYPTO_ShortHashCode name_hash;
439 struct GNUNET_HashCode lookup_key;
440 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
441 struct GNUNET_HashCode name_hash_double;
442 struct GNUNET_HashCode zone_hash_double;
444 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
445 struct AuthorityChain *iter;
453 GNUNET_CRYPTO_short_hash ("+", strlen ("+"), &name_hash);
454 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
455 GNUNET_CRYPTO_short_hash_double (&gph->ahead->zone, &zone_hash_double);
456 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
457 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
461 "+", (char*)&lookup_key_string);
463 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
464 &handle_auth_discovery_timeout, gph);
466 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
468 GNUNET_assert (gph->get_handle == NULL);
470 gph->get_handle = GNUNET_DHT_get_start(dht_handle,
471 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
473 DHT_GNS_REPLICATION_LEVEL,
477 &process_auth_discovery_dht_result,
482 for (i=0; i < rd_count; i++)
484 if ((strcmp (name, "+") == 0) &&
485 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
488 process_pseu_result (gph, (char*)rd[i].data);
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
495 if (gph->ahead->next != NULL)
497 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
501 iter = gph->ahead->next;
502 GNUNET_free (gph->ahead);
504 shorten_authority_chain (gph);
509 process_pseu_result (gph, NULL);
513 * Callback called by namestore for a zone to name
516 * @param cls the closure
517 * @param zone_key the zone we queried
518 * @param expire the expiration time of the name
519 * @param name the name found or NULL
520 * @param rd_len number of records for the name
521 * @param rd the record data (PKEY) for the name
522 * @param signature the signature for the record data
525 process_zone_to_name_discover (void *cls,
526 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
527 struct GNUNET_TIME_Absolute expire,
530 const struct GNUNET_NAMESTORE_RecordData *rd,
531 const struct GNUNET_CRYPTO_RsaSignature *signature)
533 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
534 struct AuthorityChain *iter;
536 /* we found a match in our own zone */
539 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
540 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
545 iter = gph->ahead->next;
546 GNUNET_free (gph->ahead);
548 } while (iter != NULL);
549 GNUNET_CRYPTO_rsa_key_free (gph->key);
550 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, gph);
556 GNUNET_NAMESTORE_lookup_record (namestore_handle,
559 GNUNET_GNS_RECORD_PSEU,
560 &process_auth_discovery_ns_result,
568 * Callback that shortens authorities
570 * @param gph the handle to the shorten request
573 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577 "GNS_AUTO_PSEU: New authority %s discovered\n",
580 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
583 &process_zone_to_name_discover,
589 start_shorten (struct AuthorityChain *atail,
590 struct GNUNET_CRYPTO_RsaPrivateKey *key)
592 struct AuthorityChain *new_head = NULL;
593 struct AuthorityChain *new_tail = NULL;
594 struct AuthorityChain *iter;
595 struct AuthorityChain *acopy;
596 struct GetPseuAuthorityHandle *gph;
597 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
598 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
600 /* First copy the authority chain in reverse order */
601 for (iter = atail; iter != NULL; iter = iter->prev)
603 acopy = GNUNET_malloc (sizeof (struct AuthorityChain));
604 memcpy (acopy, iter, sizeof (struct AuthorityChain));
607 GNUNET_CONTAINER_DLL_insert (new_head, new_tail, acopy);
610 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
612 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
613 pb_key = GNUNET_CRYPTO_rsa_encode_key (key);
614 gph->key = GNUNET_CRYPTO_rsa_decode_key ((char*)pb_key, ntohs (pb_key->len));
615 //gph->key = key;//GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
616 //memcpy (gph->key, key, sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
618 GNUNET_CRYPTO_short_hash (&pkey,
619 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
621 gph->ahead = new_head;
623 GNUNET_CONTAINER_DLL_insert (gph_head, gph_tail, gph);
625 shorten_authority_chain (gph);
629 * Initialize the resolver
631 * @param nh the namestore handle
632 * @param dh the dht handle
633 * @param lz the local zone's hash
634 * @param max_bg_queries maximum number of parallel background queries in dht
635 * @param ignore_pending ignore records that still require user confirmation
637 * @return GNUNET_OK on success
640 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
641 struct GNUNET_DHT_Handle *dh,
642 struct GNUNET_CRYPTO_ShortHashCode lz,
643 const struct GNUNET_CONFIGURATION_Handle *cfg,
644 unsigned long long max_bg_queries,
647 namestore_handle = nh;
651 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
652 max_allowed_background_queries = max_bg_queries;
653 ignore_pending_records = ignore_pending;
658 GNUNET_RESOLVER_connect (cfg);
660 if (NULL == vpn_handle)
662 vpn_handle = GNUNET_VPN_connect (cfg);
663 if (NULL == vpn_handle)
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "GNS_PHASE_INIT: Error connecting to VPN!\n");
668 return GNUNET_SYSERR;
672 if ((namestore_handle != NULL) && (dht_handle != NULL))
677 return GNUNET_SYSERR;
681 * Cleanup background lookups
683 * @param cls closure to iterator
684 * @param node heap nodes
685 * @param element the resolver handle
686 * @param cost heap cost
687 * @return always GNUNET_YES
690 cleanup_pending_background_queries(void* cls,
691 struct GNUNET_CONTAINER_HeapNode *node,
693 GNUNET_CONTAINER_HeapCostType cost)
695 struct ResolverHandle *rh = (struct ResolverHandle *)element;
696 ResolverCleanupContinuation cont = cls;
698 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
699 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
701 GNUNET_DHT_get_stop(rh->get_handle);
702 rh->get_handle = NULL;
703 rh->proc(rh->proc_cls, rh, 0, NULL);
705 GNUNET_CONTAINER_heap_remove_node(node);
707 if (GNUNET_CONTAINER_heap_get_size(dht_lookup_heap) == 0)
719 gns_resolver_cleanup(ResolverCleanupContinuation cont)
721 unsigned int s = GNUNET_CONTAINER_heap_get_size(dht_lookup_heap);
722 struct GetPseuAuthorityHandle *tmp;
723 struct AuthorityChain *iter;
725 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
726 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
729 for (tmp = gph_head; tmp != NULL; tmp = gph_head)
731 if (tmp->get_handle != NULL)
732 GNUNET_DHT_get_stop (tmp->get_handle);
733 tmp->get_handle = NULL;
734 if (tmp->timeout != GNUNET_SCHEDULER_NO_TASK)
735 GNUNET_SCHEDULER_cancel (tmp->timeout);
736 tmp->timeout = GNUNET_SCHEDULER_NO_TASK;
741 iter = tmp->ahead->next;
742 GNUNET_free (tmp->ahead);
744 } while (iter != NULL);
746 GNUNET_CRYPTO_rsa_key_free (tmp->key);
747 GNUNET_CONTAINER_DLL_remove (gph_head, gph_tail, tmp);
753 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
754 &cleanup_pending_background_queries,
762 * Helper function to free resolver handle
764 * @param rh the handle to free
767 free_resolver_handle(struct ResolverHandle* rh)
769 struct AuthorityChain *ac;
770 struct AuthorityChain *ac_next;
775 ac = rh->authority_chain_head;
784 if (NULL != rh->dns_raw_packet)
785 GNUNET_free (rh->dns_raw_packet);
792 * Callback when record data is put into namestore
794 * @param cls the closure
795 * @param success GNUNET_OK on success
796 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
799 on_namestore_record_put_result(void *cls,
803 if (GNUNET_NO == success)
805 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
806 "GNS_NS: records already in namestore\n");
809 else if (GNUNET_YES == success)
811 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
812 "GNS_NS: records successfully put in namestore\n");
816 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
817 "GNS_NS: Error putting records into namestore: %s\n", emsg);
821 handle_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
823 struct ResolverHandle *rh = cls;
825 if (rh->timeout_cont)
826 rh->timeout_cont(rh->timeout_cont_cls, tc);
830 * Processor for background lookups in the DHT
832 * @param cls closure (NULL)
833 * @param rd_count number of records found (not 0)
834 * @param rd record data
837 background_lookup_result_processor(void *cls,
839 const struct GNUNET_NAMESTORE_RecordData *rd)
841 //We could do sth verbose/more useful here but it doesn't make any difference
842 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
843 "GNS_BG: background dht lookup finished. (%d results)\n",
848 * Handle timeout for DHT requests
850 * @param cls the request handle as closure
851 * @param tc the task context
854 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
856 struct ResolverHandle *rh = cls;
857 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
858 char new_name[MAX_DNS_NAME_LENGTH];
860 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
861 "GNS_PHASE_REC-%d: dht lookup for query %s (%ds)timed out.\n",
862 rh->id, rh->name, rh->timeout.rel_value);
864 * Start resolution in bg
866 //strcpy(new_name, rh->name);
867 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
868 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
869 rh->name, GNUNET_GNS_TLD);
871 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
872 "GNS_PHASE_REC-%d: Starting background lookup for %s type %d\n",
873 rh->id, new_name, rlh->record_type);
875 gns_resolver_lookup_record(rh->authority,
876 rh->private_local_zone,
880 GNUNET_TIME_UNIT_FOREVER_REL,
882 &background_lookup_result_processor,
884 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
886 GNUNET_DHT_get_stop (rh->get_handle);
887 rh->get_handle = NULL;
888 rh->proc(rh->proc_cls, rh, 0, NULL);
893 * Function called when we get a result from the dht
894 * for our record query
896 * @param cls the request handle
897 * @param exp lifetime
898 * @param key the key the record was stored under
899 * @param get_path get path
900 * @param get_path_length get path length
901 * @param put_path put path
902 * @param put_path_length put path length
903 * @param type the block type
904 * @param size the size of the record
905 * @param data the record data
908 process_record_result_dht(void* cls,
909 struct GNUNET_TIME_Absolute exp,
910 const struct GNUNET_HashCode * key,
911 const struct GNUNET_PeerIdentity *get_path,
912 unsigned int get_path_length,
913 const struct GNUNET_PeerIdentity *put_path,
914 unsigned int put_path_length,
915 enum GNUNET_BLOCK_Type type,
916 size_t size, const void *data)
918 struct ResolverHandle *rh;
919 struct RecordLookupHandle *rlh;
920 struct GNSNameRecordBlock *nrb;
921 uint32_t num_records;
923 char* rd_data = (char*)data;
927 rh = (struct ResolverHandle *)cls;
928 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
929 "GNS_PHASE_REC-%d: got dht result (size=%d)\n", rh->id, size);
931 //FIXME maybe check expiration here, check block type
934 rlh = (struct RecordLookupHandle *) rh->proc_cls;
935 nrb = (struct GNSNameRecordBlock*)data;
937 /* stop lookup and timeout task */
938 GNUNET_DHT_get_stop (rh->get_handle);
939 rh->get_handle = NULL;
941 if (rh->dht_heap_node != NULL)
943 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
944 rh->dht_heap_node = NULL;
947 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
949 GNUNET_SCHEDULER_cancel(rh->timeout_task);
950 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
953 rh->get_handle = NULL;
954 name = (char*)&nrb[1];
955 num_records = ntohl(nrb->rd_count);
957 struct GNUNET_NAMESTORE_RecordData rd[num_records];
959 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
960 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
962 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
967 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
968 "GNS_PHASE_REC-%d: Error deserializing data!\n", rh->id);
972 for (i=0; i<num_records; i++)
974 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
975 "GNS_PHASE_REC-%d: Got name: %s (wanted %s)\n",
976 rh->id, name, rh->name);
977 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
978 "GNS_PHASE_REC-%d: Got type: %d (wanted %d)\n",
979 rh->id, rd[i].record_type, rlh->record_type);
980 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
981 "GNS_PHASE_REC-%d: Got data length: %d\n",
982 rh->id, rd[i].data_size);
983 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
984 "GNS_PHASE_REC-%d: Got flag %d\n",
985 rh->id, rd[i].flags);
987 if ((strcmp(name, rh->name) == 0) &&
988 (rd[i].record_type == rlh->record_type))
996 * FIXME check pubkey against existing key in namestore?
997 * https://gnunet.org/bugs/view.php?id=2179
1000 /* Save to namestore */
1001 GNUNET_NAMESTORE_record_put (namestore_handle,
1008 &on_namestore_record_put_result, //cont
1013 rh->proc(rh->proc_cls, rh, num_records, rd);
1015 rh->proc(rh->proc_cls, rh, 0, NULL);
1022 * Start DHT lookup for a (name -> query->record_type) record in
1023 * rh->authority's zone
1025 * @param rh the pending gns query context
1028 resolve_record_dht (struct ResolverHandle *rh)
1031 struct GNUNET_CRYPTO_ShortHashCode name_hash;
1032 struct GNUNET_HashCode lookup_key;
1033 struct GNUNET_HashCode name_hash_double;
1034 struct GNUNET_HashCode zone_hash_double;
1035 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
1036 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1037 struct ResolverHandle *rh_heap_root;
1039 GNUNET_CRYPTO_short_hash(rh->name, strlen(rh->name), &name_hash);
1040 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
1041 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
1042 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
1043 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
1045 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1046 "GNS_PHASE_REC-%d: starting dht lookup for %s with key: %s\n",
1047 rh->id, rh->name, (char*)&lookup_key_string);
1049 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1050 rh->dht_heap_node = NULL;
1052 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1055 * Update timeout if necessary
1057 if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
1060 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1061 "GNS_PHASE_REC-%d: Adjusting timeout\n", rh->id);
1063 * Set timeout for authority lookup phase to 1/2
1065 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
1066 GNUNET_TIME_relative_divide(rh->timeout, 2),
1067 &handle_lookup_timeout,
1070 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1071 // &dht_lookup_timeout,
1073 rh->timeout_cont = &dht_lookup_timeout;
1074 rh->timeout_cont_cls = rh;
1078 if (max_allowed_background_queries <=
1079 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1081 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1082 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
1083 rh_heap_root->get_handle = NULL;
1084 rh_heap_root->dht_heap_node = NULL;
1086 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1087 "GNS_PHASE_REC-%d: Replacing oldest background query for %s\n",
1088 rh->id, rh_heap_root->name);
1089 rh_heap_root->proc(rh_heap_root->proc_cls,
1094 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1096 GNUNET_TIME_absolute_get().abs_value);
1099 xquery = htonl(rlh->record_type);
1101 GNUNET_assert(rh->get_handle == NULL);
1102 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1103 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1105 DHT_GNS_REPLICATION_LEVEL,
1109 &process_record_result_dht,
1116 * Namestore calls this function if we have record for this name.
1117 * (or with rd_count=0 to indicate no matches)
1119 * @param cls the pending query
1120 * @param key the key of the zone we did the lookup
1121 * @param expiration expiration date of the namestore entry
1122 * @param name the name for which we need an authority
1123 * @param rd_count the number of records with 'name'
1124 * @param rd the record data
1125 * @param signature the signature of the authority for the record data
1128 process_record_result_ns(void* cls,
1129 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1130 struct GNUNET_TIME_Absolute expiration,
1131 const char *name, unsigned int rd_count,
1132 const struct GNUNET_NAMESTORE_RecordData *rd,
1133 const struct GNUNET_CRYPTO_RsaSignature *signature)
1135 struct ResolverHandle *rh;
1136 struct RecordLookupHandle *rlh;
1137 struct GNUNET_TIME_Relative remaining_time;
1138 struct GNUNET_CRYPTO_ShortHashCode zone;
1139 struct GNUNET_TIME_Absolute et;
1142 rh = (struct ResolverHandle *) cls;
1143 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1144 GNUNET_CRYPTO_short_hash(key,
1145 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1147 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1155 rh->status |= RSL_RECORD_EXISTS;
1157 if (remaining_time.rel_value == 0)
1159 rh->status |= RSL_RECORD_EXPIRED;
1166 * Lookup terminated and no results
1168 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1169 "GNS_PHASE_REC-%d: Namestore lookup for %s terminated without results\n",
1172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1173 "GNS_PHASE_REC-%d: Record %s unknown in namestore\n",
1176 * Our zone and no result? Cannot resolve TT
1178 rh->proc(rh->proc_cls, rh, 0, NULL);
1184 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1185 "GNS_PHASE_REC-%d: Processing additional result %s from namestore\n",
1187 for (i=0; i<rd_count;i++)
1189 if (rd[i].record_type != rlh->record_type)
1192 if (ignore_pending_records &&
1193 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1196 "GNS_PHASE_REC-%d: Record %s is awaiting user confirmation. Skipping\n",
1201 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
1202 et.abs_value = rd[i].expiration_time;
1203 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
1206 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1207 "GNS_PHASE_REC-%d: This record is expired. Skipping\n",
1217 if (rh->answered == 0)
1219 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1220 "GNS_PHASE_REC-%d: No answers found. This is odd!\n", rh->id);
1221 rh->proc(rh->proc_cls, rh, 0, NULL);
1225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1226 "GNS_PHASE_REC-%d: Found %d answer(s) to query in %d records!\n",
1227 rh->id, rh->answered, rd_count);
1229 rh->proc(rh->proc_cls, rh, rd_count, rd);
1235 * VPN redirect result callback
1237 * @param cls the resolver handle
1238 * @param af the requested address family
1239 * @param address in_addr(6) respectively
1242 process_record_result_vpn (void* cls, int af, const void *address)
1244 struct ResolverHandle *rh = cls;
1245 struct RecordLookupHandle *rlh;
1246 struct GNUNET_NAMESTORE_RecordData rd;
1248 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1250 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1251 "GNS_PHASE_REC_VPN-%d: Got answer from VPN to query!\n",
1255 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1256 "GNS_PHASE_REC-%d: Answer is IPv4!\n",
1258 if (rlh->record_type != GNUNET_GNS_RECORD_TYPE_A)
1260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1261 "GNS_PHASE_REC-%d: Requested record is not IPv4!\n",
1263 rh->proc (rh->proc_cls, rh, 0, NULL);
1266 rd.record_type = GNUNET_GNS_RECORD_TYPE_A;
1267 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1269 rd.data_size = sizeof (struct in_addr);
1271 rh->proc (rh->proc_cls, rh, 1, &rd);
1274 else if (af == AF_INET6)
1276 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1277 "GNS_PHASE_REC-%d: Answer is IPv6!\n",
1279 if (rlh->record_type != GNUNET_GNS_RECORD_AAAA)
1281 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1282 "GNS_PHASE_REC-%d: Requested record is not IPv6!\n",
1284 rh->proc (rh->proc_cls, rh, 0, NULL);
1287 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1288 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter... */
1290 rd.data_size = sizeof (struct in6_addr);
1292 rh->proc (rh->proc_cls, rh, 1, &rd);
1296 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1297 "GNS_PHASE_REC-%d: Got garbage from VPN!\n",
1299 rh->proc (rh->proc_cls, rh, 0, NULL);
1306 * @param rh resolver handle
1307 * @param rlh record lookup handle
1308 * @param rd_count number of results
1312 finish_lookup(struct ResolverHandle *rh,
1313 struct RecordLookupHandle* rlh,
1314 unsigned int rd_count,
1315 const struct GNUNET_NAMESTORE_RecordData *rd);
1318 * Process VPN lookup result for record
1320 * @param cls the record lookup handle
1321 * @param rh resolver handle
1322 * @param rd_count number of results (1)
1323 * @param rd record data containing the result
1326 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1327 unsigned int rd_count,
1328 const struct GNUNET_NAMESTORE_RecordData *rd)
1330 struct RecordLookupHandle* rlh = (struct RecordLookupHandle*) cls;
1334 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1335 "GNS_PHASE_REC_VPN-%d: VPN returned no records. (status: %d)!\n",
1338 /* give up, cannot resolve */
1339 finish_lookup(rh, rlh, 0, NULL);
1340 free_resolver_handle(rh);
1344 /* results found yay */
1345 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1346 "GNS_PHASE_REC_VPN-%d: Record resolved from VPN!", rh->id);
1348 finish_lookup(rh, rlh, rd_count, rd);
1350 free_resolver_handle(rh);
1355 * Sends a UDP dns query to a nameserver specified in the rh
1357 * @param rh the resolver handle
1360 send_dns_packet (struct ResolverHandle *rh);
1364 handle_dns_resolver (void *cls,
1365 const struct sockaddr *addr,
1368 struct ResolverHandle *rh = cls;
1369 struct RecordLookupHandle *rlh = rh->proc_cls;
1370 struct GNUNET_NAMESTORE_RecordData rd;
1371 struct sockaddr_in *sai;
1372 struct sockaddr_in6 *sai6;
1376 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1377 "No address found in DNS!\n");
1378 finish_lookup (rh, rlh, 0, NULL);
1379 free_resolver_handle (rh);
1383 if (addrlen == sizeof (struct sockaddr_in))
1385 sai = (struct sockaddr_in*) addr;
1386 rd.record_type = GNUNET_GNS_RECORD_TYPE_A;
1387 rd.data_size = sizeof (struct in_addr);
1388 rd.data = &sai->sin_addr;
1392 sai6 = (struct sockaddr_in6*) addr;
1393 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1394 rd.data_size = sizeof (struct in6_addr);
1395 rd.data = &sai6->sin6_addr;
1398 rd.expiration_time = UINT64_MAX; /* FIXME: should probably pick something shorter */
1400 finish_lookup (rh, rlh, 1, &rd);
1401 free_resolver_handle (rh);
1402 GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
1406 * Resolve DNS name via local stub resolver
1408 * @param rh the resolver handle
1411 resolve_dns_name (struct ResolverHandle *rh)
1413 struct RecordLookupHandle *rlh = rh->proc_cls;
1416 if ((rlh->record_type != GNUNET_GNS_RECORD_TYPE_A) &&
1417 (rlh->record_type != GNUNET_GNS_RECORD_AAAA))
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420 "Can only resolve A/AAAA via stub... abort\n");
1421 finish_lookup (rh, rlh, 0, NULL);
1422 free_resolver_handle (rh);
1426 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_A)
1431 //GNUNET_RESOLVER_connect (cfg); FIXME into init
1433 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1436 &handle_dns_resolver,
1442 * Read DNS udp packet from socket
1444 * @param cls the resolver handle
1445 * @param tc task context
1448 read_dns_response (void *cls,
1449 const struct GNUNET_SCHEDULER_TaskContext *tc)
1451 struct ResolverHandle *rh = cls;
1452 struct RecordLookupHandle *rlh = rh->proc_cls;
1453 char buf[UINT16_MAX];
1455 struct sockaddr_in addr;
1457 struct GNUNET_DNSPARSER_Packet *packet;
1458 struct GNUNET_NAMESTORE_RecordData rd;
1459 int found_delegation = GNUNET_NO;
1460 int found_cname = GNUNET_NO;
1461 char* delegation_name = NULL;
1462 int zone_offset = 0;
1465 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1466 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1468 /* timeout or shutdown */
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470 "Terminating DNS query\n");
1471 finish_lookup (rh, rlh, 0, NULL);
1472 GNUNET_NETWORK_socket_close (rh->dns_sock);
1473 free_resolver_handle (rh);
1477 addrlen = sizeof (addr);
1478 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1480 (struct sockaddr*) &addr,
1485 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1486 finish_lookup (rh, rlh, 0, NULL);
1487 GNUNET_NETWORK_socket_close (rh->dns_sock);
1488 free_resolver_handle (rh);
1492 packet = GNUNET_DNSPARSER_parse (buf, r);
1496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1497 "Failed to parse DNS reply!\n");
1498 finish_lookup (rh, rlh, 0, NULL);
1499 GNUNET_NETWORK_socket_close (rh->dns_sock);
1500 free_resolver_handle (rh);
1504 for (i = 0; i < packet->num_answers; i++)
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507 "Got record type %d (want %d)\n",
1508 packet->answers[i].type,
1510 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1511 if (packet->answers[i].type == GNUNET_GNS_RECORD_TYPE_CNAME)
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 "CNAME... restarting query with %s\n",
1515 packet->answers[i].data.hostname
1517 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1518 found_cname = GNUNET_YES;
1519 //send_dns_packet (rh);
1520 //GNUNET_DNSPARSER_free_packet (packet);
1524 if ((packet->answers[i].type == rlh->record_type) &&
1525 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1529 rd.data = packet->answers[i].data.raw.data;
1530 rd.data_size = packet->answers[i].data.raw.data_len;
1531 rd.record_type = packet->answers[i].type;
1533 rd.expiration_time = packet->answers[i].expiration_time.abs_value;
1534 finish_lookup (rh, rlh, 1, &rd);
1535 GNUNET_NETWORK_socket_close (rh->dns_sock);
1536 GNUNET_DNSPARSER_free_packet (packet);
1537 free_resolver_handle (rh);
1542 if (GNUNET_YES == found_cname)
1544 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1546 if (0 > zone_offset)
1549 /* restart query with CNAME */
1550 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1553 "Asking same server for %s\n", rh->dns_name);
1554 send_dns_packet (rh);
1558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1559 "Trying system resolver for %s\n", rh->dns_name);
1560 resolve_dns_name (rh);
1563 GNUNET_DNSPARSER_free_packet (packet);
1567 for (i = 0; i < packet->num_authority_records; i++)
1570 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_TYPE_NS)
1572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1573 "Found NS delegation!\n");
1574 found_delegation = GNUNET_YES;
1575 delegation_name = packet->authority_records[i].data.hostname;
1580 for (i = 0; i < packet->num_additional_records; i++)
1582 if (found_delegation == GNUNET_NO)
1585 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_TYPE_A) &&
1586 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1588 GNUNET_assert (sizeof (struct in_addr) ==
1589 packet->authority_records[i].data.raw.data_len);
1591 rh->dns_addr.sin_addr =
1592 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1593 send_dns_packet (rh);
1594 GNUNET_DNSPARSER_free_packet (packet);
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1600 "Nothing useful in DNS reply!\n");
1601 finish_lookup (rh, rlh, 0, NULL);
1602 GNUNET_NETWORK_socket_close (rh->dns_sock);
1603 free_resolver_handle (rh);
1604 GNUNET_DNSPARSER_free_packet (packet);
1609 * Sends a UDP dns query to a nameserver specified in the rh
1611 * @param rh the request handle
1614 send_dns_packet (struct ResolverHandle *rh)
1616 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1617 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1619 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1621 rh->dns_raw_packet_size,
1622 (struct sockaddr*)&rh->dns_addr,
1623 sizeof (struct sockaddr_in));
1625 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1626 rh->timeout, //FIXME less?
1632 GNUNET_NETWORK_fdset_destroy (rset);
1637 * The final phase of resoution.
1638 * We found a NS RR and want to resolve via DNS
1640 * @param rh the pending lookup handle
1641 * @param rd_count length of record data
1642 * @param rd record data containing VPN RR
1645 resolve_record_dns (struct ResolverHandle *rh,
1647 const struct GNUNET_NAMESTORE_RecordData *rd)
1649 struct GNUNET_DNSPARSER_Query query;
1650 struct GNUNET_DNSPARSER_Packet packet;
1651 struct GNUNET_DNSPARSER_Flags flags;
1652 struct in_addr dnsip;
1653 struct sockaddr_in addr;
1654 struct sockaddr *sa;
1656 struct RecordLookupHandle *rlh = rh->proc_cls;
1658 /* We cancel here as to not include the ns lookup in the timeout */
1659 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1661 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1662 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1664 /* Start shortening */
1665 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1668 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1670 start_shorten (rh->authority_chain_tail,
1674 for (i = 0; i < rd_count; i++)
1676 /* Synthesize dns name */
1677 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS)
1679 strcpy (rh->dns_zone, (char*)rd[i].data);
1680 if (0 == strcmp (rh->name, ""))
1681 strcpy (rh->dns_name, (char*)rd[i].data);
1683 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1686 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
1687 dnsip = *((struct in_addr*)rd[i].data);
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1691 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1695 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1696 if (rh->dns_sock == NULL)
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1699 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1701 finish_lookup (rh, rlh, 0, NULL);
1702 free_resolver_handle (rh);
1706 memset (&addr, 0, sizeof (struct sockaddr_in));
1707 sa = (struct sockaddr *) &addr;
1708 sa->sa_family = AF_INET;
1709 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1711 sizeof (struct sockaddr_in)))
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1714 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1716 GNUNET_NETWORK_socket_close (rh->dns_sock);
1717 finish_lookup (rh, rlh, 0, NULL);
1718 free_resolver_handle (rh);
1722 /*TODO create dnsparser query, serialize, sendto, handle reply*/
1723 query.name = rh->dns_name;
1724 query.type = rlh->record_type;
1725 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1726 memset (&flags, 0, sizeof (flags));
1727 flags.recursion_desired = 1;
1728 flags.checking_disabled = 1;
1729 packet.queries = &query;
1730 packet.answers = NULL;
1731 packet.authority_records = NULL;
1732 packet.num_queries = 1;
1733 packet.num_answers = 0;
1734 packet.num_authority_records = 0;
1735 packet.num_additional_records = 0;
1736 packet.flags = flags;
1738 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1740 &rh->dns_raw_packet,
1741 &rh->dns_raw_packet_size))
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1744 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1746 GNUNET_NETWORK_socket_close (rh->dns_sock);
1747 finish_lookup (rh, rlh, 0, NULL);
1748 free_resolver_handle (rh);
1752 rh->dns_addr.sin_family = AF_INET;
1753 rh->dns_addr.sin_port = htons (53); //domain
1754 rh->dns_addr.sin_addr = dnsip;
1755 #if HAVE_SOCKADDR_IN_SIN_LEN
1756 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1759 send_dns_packet (rh);
1764 * The final phase of resoution.
1765 * We found a VPN RR and want to request an IPv4/6 address
1767 * @param rh the pending lookup handle
1768 * @param rd_count length of record data
1769 * @param rd record data containing VPN RR
1772 resolve_record_vpn (struct ResolverHandle *rh,
1774 const struct GNUNET_NAMESTORE_RecordData *rd)
1778 struct GNUNET_HashCode peer_id;
1779 struct GNUNET_CRYPTO_HashAsciiEncoded s_pid;
1780 struct GNUNET_HashCode serv_desc;
1781 struct GNUNET_CRYPTO_HashAsciiEncoded s_sd;
1783 size_t len = (sizeof (uint32_t) * 2) + (sizeof (struct GNUNET_HashCode) * 2);
1785 /* We cancel here as to not include the ns lookup in the timeout */
1786 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1788 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1789 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1791 /* Start shortening */
1792 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1795 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1797 start_shorten (rh->authority_chain_tail,
1801 /* Extracting VPN information FIXME rd parsing with NS API?*/
1802 if (len != rd->data_size)
1804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1805 "GNS_PHASE_REC_VPN-%llu: Error parsing VPN RR!\n",
1807 finish_lookup (rh, rh->proc_cls, 0, NULL);
1808 free_resolver_handle (rh);
1813 memcpy (&af, pos, sizeof (uint32_t));
1814 pos += sizeof (uint32_t);
1815 memcpy (&proto, pos, sizeof (uint32_t));
1816 pos += sizeof (uint32_t);
1817 memcpy (&s_pid, pos, sizeof (struct GNUNET_HashCode));
1818 pos += sizeof (struct GNUNET_HashCode);
1819 memcpy (&s_sd, pos, sizeof (struct GNUNET_HashCode));
1822 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_pid, &peer_id)) ||
1823 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_sd, &serv_desc)))
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1826 "GNS_PHASE_REC_VPN-%llu: Error parsing VPN RR hashes!\n",
1828 finish_lookup (rh, rh->proc_cls, 0, NULL);
1829 free_resolver_handle (rh);
1833 rh->proc = &handle_record_vpn;
1835 if (NULL == vpn_handle)
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1838 "GNS_PHASE_REC_VPN-%llu: VPN not connected!\n",
1840 finish_lookup (rh, rh->proc_cls, 0, NULL);
1841 free_resolver_handle (rh);
1846 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1848 (struct GNUNET_PeerIdentity*)&peer_id,
1851 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1852 &process_record_result_vpn,
1858 * The final phase of resolution.
1859 * rh->name is a name that is canonical and we do not have a delegation.
1860 * Query namestore for this record
1862 * @param rh the pending lookup handle
1865 resolve_record_ns(struct ResolverHandle *rh)
1867 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1869 /* We cancel here as to not include the ns lookup in the timeout */
1870 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1872 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1873 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1875 /* Start shortening */
1876 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1879 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1881 start_shorten (rh->authority_chain_tail,
1886 * Try to resolve this record in our namestore.
1887 * The name to resolve is now in rh->authority_name
1888 * since we tried to resolve it to an authority
1891 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1895 &process_record_result_ns,
1902 * Handle timeout for DHT requests
1904 * @param cls the request handle as closure
1905 * @param tc the task context
1908 dht_authority_lookup_timeout(void *cls,
1909 const struct GNUNET_SCHEDULER_TaskContext *tc)
1911 struct ResolverHandle *rh = cls;
1912 struct RecordLookupHandle *rlh = rh->proc_cls;
1913 char new_name[MAX_DNS_NAME_LENGTH];
1915 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1916 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%ds)timed out.\n",
1917 rh->id, rh->authority_name, rh->timeout.rel_value);
1919 rh->status |= RSL_TIMED_OUT;
1921 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1923 GNUNET_DHT_get_stop (rh->get_handle);
1924 rh->get_handle = NULL;
1926 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
1928 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1929 "GNS_PHASE_DELEGATE_DHT-%llu: Got shutdown\n",
1931 rh->proc(rh->proc_cls, rh, 0, NULL);
1935 if (strcmp(rh->name, "") == 0)
1938 * promote authority back to name and try to resolve record
1940 strcpy(rh->name, rh->authority_name);
1941 rh->proc(rh->proc_cls, rh, 0, NULL);
1946 * Start resolution in bg
1948 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
1949 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
1950 //strcpy(new_name, rh->name);
1951 //strcpy(new_name+strlen(new_name), ".");
1952 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1954 strcpy(rh->name, new_name);
1956 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1957 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1958 rh->id, rh->name, rlh->record_type);
1960 gns_resolver_lookup_record(rh->authority,
1961 rh->private_local_zone,
1965 GNUNET_TIME_UNIT_FOREVER_REL,
1967 &background_lookup_result_processor,
1970 rh->proc(rh->proc_cls, rh, 0, NULL);
1974 static void resolve_delegation_dht(struct ResolverHandle *rh);
1977 static void resolve_delegation_ns(struct ResolverHandle *rh);
1981 * Namestore resolution for delegation finished. Processing result.
1983 * @param cls the closure
1984 * @param rh resolver handle
1985 * @param rd_count number of results (always 0)
1986 * @param rd record data (always NULL)
1989 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1990 unsigned int rd_count,
1991 const struct GNUNET_NAMESTORE_RecordData *rd);
1995 * This is a callback function that checks for key revocation
1997 * @param cls the pending query
1998 * @param key the key of the zone we did the lookup
1999 * @param expiration expiration date of the record data set in the namestore
2000 * @param name the name for which we need an authority
2001 * @param rd_count the number of records with 'name'
2002 * @param rd the record data
2003 * @param signature the signature of the authority for the record data
2006 process_pkey_revocation_result_ns (void *cls,
2007 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2008 struct GNUNET_TIME_Absolute expiration,
2010 unsigned int rd_count,
2011 const struct GNUNET_NAMESTORE_RecordData *rd,
2012 const struct GNUNET_CRYPTO_RsaSignature *signature)
2014 struct ResolverHandle *rh = cls;
2015 struct GNUNET_TIME_Relative remaining_time;
2018 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2020 for (i = 0; i < rd_count; i++)
2022 if (rd[i].record_type == GNUNET_GNS_RECORD_REV)
2024 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2025 "GNS_PHASE_DELEGATE_REV-%llu: Zone has been revoked.\n",
2027 rh->status |= RSL_PKEY_REVOKED;
2028 rh->proc (rh->proc_cls, rh, 0, NULL);
2033 if ((name == NULL) ||
2034 (remaining_time.rel_value == 0))
2036 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2037 "GNS_PHASE_DELEGATE_REV-%llu: + Records don't exist or are expired.\n",
2040 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2042 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2043 "GNS_PHASE_DELEGATE_REV-%d: Starting background lookup for %s type %d\n",
2044 rh->id, "+.gnunet", GNUNET_GNS_RECORD_REV);
2046 gns_resolver_lookup_record(rh->authority,
2047 rh->private_local_zone,
2048 GNUNET_GNS_RECORD_REV,
2051 GNUNET_TIME_UNIT_FOREVER_REL,
2053 &background_lookup_result_processor,
2057 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2058 "GNS_PHASE_DELEGATE_REV-%llu: Revocation checkpassed\n",
2061 * We are done with PKEY resolution if name is empty
2062 * else resolve again with new authority
2064 if (strcmp (rh->name, "") == 0)
2065 rh->proc (rh->proc_cls, rh, 0, NULL);
2067 resolve_delegation_ns (rh);
2073 * Function called when we get a result from the dht
2074 * for our query. Recursively tries to resolve authorities
2077 * @param cls the request handle
2078 * @param exp lifetime
2079 * @param key the key the record was stored under
2080 * @param get_path get path
2081 * @param get_path_length get path length
2082 * @param put_path put path
2083 * @param put_path_length put path length
2084 * @param type the block type
2085 * @param size the size of the record
2086 * @param data the record data
2089 process_delegation_result_dht(void* cls,
2090 struct GNUNET_TIME_Absolute exp,
2091 const struct GNUNET_HashCode * key,
2092 const struct GNUNET_PeerIdentity *get_path,
2093 unsigned int get_path_length,
2094 const struct GNUNET_PeerIdentity *put_path,
2095 unsigned int put_path_length,
2096 enum GNUNET_BLOCK_Type type,
2097 size_t size, const void *data)
2099 struct ResolverHandle *rh;
2100 struct GNSNameRecordBlock *nrb;
2101 uint32_t num_records;
2103 char* rd_data = (char*) data;
2106 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
2107 struct GNUNET_HashCode zone_hash_double, name_hash_double;
2109 rh = (struct ResolverHandle *)cls;
2111 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2112 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
2117 nrb = (struct GNSNameRecordBlock*)data;
2119 /* stop dht lookup and timeout task */
2120 GNUNET_DHT_get_stop (rh->get_handle);
2122 rh->get_handle = NULL;
2124 if (rh->dht_heap_node != NULL)
2126 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
2127 rh->dht_heap_node = NULL;
2130 num_records = ntohl(nrb->rd_count);
2131 name = (char*)&nrb[1];
2133 struct GNUNET_NAMESTORE_RecordData rd[num_records];
2135 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
2136 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
2138 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
2143 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2144 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2149 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2150 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2151 rh->id, name, rh->authority_name);
2152 for (i=0; i<num_records; i++)
2155 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2156 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2157 rh->id, name, rh->authority_name);
2158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2159 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2160 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2161 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2162 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2163 rh->id, rd[i].data_size);
2164 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2165 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2166 rh->id, rd[i].flags);
2168 if ((rd[i].record_type == GNUNET_GNS_RECORD_VPN) ||
2169 (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS) ||
2170 (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_CNAME))
2173 * This is a VPN,NS,CNAME entry. Let namestore handle this after caching
2175 if (strcmp(rh->name, "") == 0)
2176 strcpy(rh->name, rh->authority_name);
2178 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2179 rh->name, rh->authority_name); //FIXME ret
2184 if ((strcmp(name, rh->authority_name) == 0) &&
2185 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
2187 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2188 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2191 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2192 struct AuthorityChain *auth =
2193 GNUNET_malloc(sizeof(struct AuthorityChain));
2194 auth->zone = rh->authority;
2195 memset(auth->name, 0, strlen(rh->authority_name)+1);
2196 strcpy(auth->name, rh->authority_name);
2197 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2198 rh->authority_chain_tail,
2201 /** try to import pkey if private key available */
2202 //if (rh->priv_key && is_canonical (rh->name))
2203 // process_discovered_authority(name, auth->zone,
2204 // rh->authority_chain_tail->zone,
2211 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2212 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2213 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2214 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2216 /* Save to namestore */
2217 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
2220 GNUNET_NAMESTORE_record_put (namestore_handle,
2227 &on_namestore_record_put_result, //cont
2237 * FIXME in this case. should we ask namestore again?
2239 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2240 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2241 rh->id, rh->authority_name, rh->name);
2243 if (strcmp(rh->name, "") == 0)
2245 /* Start shortening */
2246 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2249 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2251 start_shorten (rh->authority_chain_tail,
2256 rh->proc = &handle_delegation_ns;
2258 /* Check for key revocation and delegate */
2259 GNUNET_NAMESTORE_lookup_record (namestore_handle,
2262 GNUNET_GNS_RECORD_REV,
2263 &process_pkey_revocation_result_ns,
2266 /*if (strcmp(rh->name, "") == 0)
2268 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2271 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2273 start_shorten (rh->authority_chain_tail,
2277 rh->proc(rh->proc_cls, rh, 0, NULL);
2281 rh->proc = &handle_delegation_ns;
2282 resolve_delegation_ns (rh);
2289 * No pkey but name exists
2292 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2293 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2294 rh->id, rh->authority_name, rh->name);
2295 if (strcmp(rh->name, "") == 0)
2296 strcpy(rh->name, rh->authority_name);
2298 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2299 rh->name, rh->authority_name); //FIXME ret
2301 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2302 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2303 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2304 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2306 rh->proc(rh->proc_cls, rh, 0, NULL);
2309 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2310 +(MAX_DNS_NAME_LENGTH*2)
2311 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2315 expand_plus(char** dest, char* src, char* repl)
2318 unsigned int s_len = strlen(src)+1;
2320 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2321 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2325 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2326 "GNS_POSTPROCESS: %s to short\n", src);
2328 /* no postprocessing */
2329 memcpy(*dest, src, s_len+1);
2333 if (0 == strcmp(src+s_len-3, ".+"))
2335 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2336 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2337 memset(*dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2339 pos = *dest+s_len-2;
2341 pos += strlen(repl);
2342 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2343 "GNS_POSTPROCESS: Expanded to %s\n", *dest);
2347 memcpy(*dest, src, s_len+1);
2355 finish_lookup(struct ResolverHandle *rh,
2356 struct RecordLookupHandle* rlh,
2357 unsigned int rd_count,
2358 const struct GNUNET_NAMESTORE_RecordData *rd)
2361 char new_rr_data[MAX_DNS_NAME_LENGTH];
2362 char new_mx_data[MAX_MX_LENGTH];
2363 char new_soa_data[MAX_SOA_LENGTH];
2364 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2367 unsigned int offset;
2369 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2371 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2372 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2376 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2378 for (i = 0; i < rd_count; i++)
2381 if (rd[i].record_type != GNUNET_GNS_RECORD_TYPE_NS &&
2382 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_CNAME &&
2383 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
2384 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_SOA)
2386 p_rd[i].data = rd[i].data;
2391 * for all those records we 'should'
2392 * also try to resolve the A/AAAA records (RFC1035)
2393 * This is a feature and not important
2396 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2397 "GNS_POSTPROCESS: Postprocessing\n");
2399 if (strcmp(rh->name, "+") == 0)
2400 repl_string = rlh->name;
2402 repl_string = rlh->name+strlen(rh->name)+1;
2405 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
2407 memcpy(new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2408 offset = sizeof(uint16_t);
2409 pos = new_mx_data+offset;
2410 expand_plus(&pos, (char*)rd[i].data+sizeof(uint16_t),
2412 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2413 p_rd[i].data = new_mx_data;
2414 p_rd[i].data_size = offset;
2416 else if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_SOA)
2418 /* expand mname and rname */
2420 expand_plus(&pos, (char*)rd[i].data, repl_string);
2421 offset = strlen(new_soa_data)+1;
2422 pos = new_soa_data+offset;
2423 expand_plus(&pos, (char*)rd[i].data+offset, repl_string);
2424 offset += strlen(new_soa_data+offset)+1;
2425 /* cpy the 4 numbers serial refresh retry and expire */
2426 memcpy(new_soa_data+offset, (char*)rd[i].data+offset, sizeof(uint32_t)*5);
2427 offset += sizeof(uint32_t)*5;
2428 p_rd[i].data_size = offset;
2429 p_rd[i].data = new_soa_data;
2434 expand_plus(&pos, (char*)rd[i].data, repl_string);
2435 p_rd[i].data_size = strlen(new_rr_data)+1;
2436 p_rd[i].data = new_rr_data;
2441 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2447 * Process DHT lookup result for record.
2449 * @param cls the closure
2450 * @param rh resolver handle
2451 * @param rd_count number of results
2452 * @param rd record data
2455 handle_record_dht(void* cls, struct ResolverHandle *rh,
2456 unsigned int rd_count,
2457 const struct GNUNET_NAMESTORE_RecordData *rd)
2459 struct RecordLookupHandle* rlh;
2461 rlh = (struct RecordLookupHandle*)cls;
2464 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2465 "GNS_PHASE_REC-%d: No records for %s found in DHT. Aborting\n",
2467 /* give up, cannot resolve */
2468 finish_lookup(rh, rlh, 0, NULL);
2469 free_resolver_handle(rh);
2473 /* results found yay */
2474 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2475 "GNS_PHASE_REC-%d: Record resolved from DHT!", rh->id);
2477 finish_lookup(rh, rlh, rd_count, rd);
2478 free_resolver_handle(rh);
2486 * Process namestore lookup result for record.
2488 * @param cls the closure
2489 * @param rh resolver handle
2490 * @param rd_count number of results
2491 * @param rd record data
2494 handle_record_ns (void* cls, struct ResolverHandle *rh,
2495 unsigned int rd_count,
2496 const struct GNUNET_NAMESTORE_RecordData *rd)
2498 struct RecordLookupHandle* rlh;
2499 rlh = (struct RecordLookupHandle*) cls;
2502 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2503 "GNS_PHASE_REC-%d: NS returned no records. (status: %d)!\n",
2508 * There are 5 conditions that have to met for us to consult the DHT:
2509 * 1. The entry in the DHT is RSL_RECORD_EXPIRED OR
2510 * 2. No entry in the NS existed AND
2511 * 3. The zone queried is not the local resolver's zone AND
2512 * 4. The name that was looked up is '+'
2513 * because if it was any other canonical name we either already queried
2514 * the DHT for the authority in the authority lookup phase (and thus
2515 * would already have an entry in the NS for the record)
2516 * 5. We are not in cache only mode
2518 if (((rh->status & RSL_RECORD_EXPIRED) || (rh->status &!RSL_RECORD_EXISTS))
2519 && GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2520 &rh->private_local_zone) &&
2521 (strcmp(rh->name, "+") == 0) &&
2522 (rh->only_cached == GNUNET_NO))
2524 rh->proc = &handle_record_dht;
2525 resolve_record_dht(rh);
2528 /* give up, cannot resolve */
2529 finish_lookup(rh, rlh, 0, NULL);
2530 free_resolver_handle(rh);
2534 /* results found yay */
2535 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2536 "GNS_PHASE_REC-%d: Record resolved from namestore!", rh->id);
2538 finish_lookup(rh, rlh, rd_count, rd);
2540 free_resolver_handle(rh);
2546 * Move one level up in the domain hierarchy and return the
2547 * passed top level domain.
2549 * @param name the domain
2550 * @param dest the destination where the tld will be put
2553 pop_tld(char* name, char* dest)
2557 if (is_canonical(name))
2564 for (len = strlen(name); len > 0; len--)
2566 if (*(name+len) == '.')
2576 strcpy(dest, (name+len+1));
2580 * Checks if name is in tld
2582 * @param name the name to check
2583 * @param tld the TLD to check for
2584 * @return GNUNET_YES or GNUNET_NO
2587 is_tld(const char* name, const char* tld)
2591 if (strlen(name) <= strlen(tld))
2596 offset = strlen(name)-strlen(tld);
2597 if (strcmp(name+offset, tld) != 0)
2599 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2600 "%s is not in .%s TLD\n", name, tld);
2607 * DHT resolution for delegation finished. Processing result.
2609 * @param cls the closure
2610 * @param rh resolver handle
2611 * @param rd_count number of results (always 0)
2612 * @param rd record data (always NULL)
2615 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2616 unsigned int rd_count,
2617 const struct GNUNET_NAMESTORE_RecordData *rd)
2619 struct RecordLookupHandle* rlh;
2620 rlh = (struct RecordLookupHandle*) cls;
2623 if (strcmp(rh->name, "") == 0)
2625 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
2627 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2628 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2630 finish_lookup(rh, rlh, rd_count, rd);
2631 free_resolver_handle(rh);
2634 /* We resolved full name for delegation. resolving record */
2635 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2636 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2638 strcpy(rh->name, "+\0");
2639 rh->proc = &handle_record_ns;
2640 resolve_record_ns(rh);
2645 * we still have some left
2647 if (is_canonical(rh->name))
2649 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2650 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2653 rh->proc = &handle_record_ns;
2654 resolve_record_ns(rh);
2657 /* give up, cannot resolve */
2658 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2659 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2661 finish_lookup(rh, rlh, 0, NULL);
2662 free_resolver_handle(rh);
2667 * Start DHT lookup for a name -> PKEY (compare NS) record in
2668 * rh->authority's zone
2670 * @param rh the pending gns query
2673 resolve_delegation_dht(struct ResolverHandle *rh)
2676 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2677 struct GNUNET_HashCode name_hash_double;
2678 struct GNUNET_HashCode zone_hash_double;
2679 struct GNUNET_HashCode lookup_key;
2680 struct ResolverHandle *rh_heap_root;
2682 pop_tld(rh->name, rh->authority_name);
2683 GNUNET_CRYPTO_short_hash(rh->authority_name,
2684 strlen(rh->authority_name),
2686 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2687 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2688 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2690 rh->dht_heap_node = NULL;
2692 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2694 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
2695 // &dht_authority_lookup_timeout,
2697 rh->timeout_cont = &dht_authority_lookup_timeout;
2698 rh->timeout_cont_cls = rh;
2702 if (max_allowed_background_queries <=
2703 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2705 /* terminate oldest lookup */
2706 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2707 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
2708 rh_heap_root->dht_heap_node = NULL;
2710 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2711 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2712 rh->id, rh_heap_root->authority_name);
2714 rh_heap_root->proc(rh_heap_root->proc_cls,
2719 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2721 GNUNET_TIME_absolute_get().abs_value);
2724 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2726 GNUNET_assert(rh->get_handle == NULL);
2727 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2728 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2730 DHT_GNS_REPLICATION_LEVEL,
2734 &process_delegation_result_dht,
2741 * Namestore resolution for delegation finished. Processing result.
2743 * @param cls the closure
2744 * @param rh resolver handle
2745 * @param rd_count number of results (always 0)
2746 * @param rd record data (always NULL)
2749 handle_delegation_ns (void* cls, struct ResolverHandle *rh,
2750 unsigned int rd_count,
2751 const struct GNUNET_NAMESTORE_RecordData *rd)
2753 struct RecordLookupHandle* rlh;
2754 rlh = (struct RecordLookupHandle*) cls;
2756 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2757 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2758 rh->id, rh->status);
2760 if (rh->status & RSL_PKEY_REVOKED)
2762 finish_lookup (rh, rlh, 0, NULL);
2763 free_resolver_handle (rh);
2767 if (strcmp(rh->name, "") == 0)
2770 /* We resolved full name for delegation. resolving record */
2771 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2772 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2774 if (rh->status & RSL_CNAME_FOUND)
2776 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_CNAME)
2778 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2779 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried CNAME in NS.\n",
2781 finish_lookup(rh, rlh, rd_count, rd);
2782 free_resolver_handle(rh);
2786 /* A CNAME can only occur alone */
2787 GNUNET_assert (is_canonical ((char*)rd->data));
2788 strcpy (rh->name, rd->data);
2789 resolve_delegation_ns (rh);
2792 else if (rh->status & RSL_DELEGATE_VPN)
2794 if (rlh->record_type == GNUNET_GNS_RECORD_VPN)
2796 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2797 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2799 finish_lookup(rh, rlh, rd_count, rd);
2800 free_resolver_handle(rh);
2803 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2804 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2806 GNUNET_assert (NULL != rd);
2807 rh->proc = &handle_record_vpn;
2808 resolve_record_vpn (rh, rd_count, rd);
2811 else if (rh->status & RSL_DELEGATE_NS)
2813 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_NS)
2815 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2816 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2818 finish_lookup(rh, rlh, rd_count, rd);
2819 free_resolver_handle(rh);
2823 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2824 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2826 GNUNET_assert (NULL != rd);
2827 rh->proc = &handle_record_ns;
2828 resolve_record_dns (rh, rd_count, rd);
2831 else if (rh->status & RSL_DELEGATE_PKEY)
2833 if (rh->status & RSL_PKEY_REVOKED)
2835 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2836 "GNS_PHASE_DELEGATE_NS-%llu: Resolved PKEY is revoked.\n",
2838 finish_lookup (rh, rlh, 0, NULL);
2839 free_resolver_handle (rh);
2842 else if (rlh->record_type == GNUNET_GNS_RECORD_PKEY)
2844 GNUNET_assert(rd_count == 1);
2845 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2846 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
2848 finish_lookup(rh, rlh, rd_count, rd);
2849 free_resolver_handle(rh);
2853 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2854 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
2856 strcpy(rh->name, "+\0");
2857 rh->proc = &handle_record_ns;
2858 resolve_record_ns(rh);
2862 if (rh->status & RSL_DELEGATE_NS)
2864 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_NS)
2866 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2867 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2869 finish_lookup(rh, rlh, rd_count, rd);
2870 free_resolver_handle(rh);
2874 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2875 "GNS_PHASE_DELEGATE_NS-%llu: NS delegation starting.\n",
2877 GNUNET_assert (NULL != rd);
2878 rh->proc = &handle_record_ns;
2879 resolve_record_dns (rh, rd_count, rd);
2884 * we still have some left
2885 * check if authority in ns is fresh
2887 * or we are authority
2889 if (((rh->status & RSL_RECORD_EXISTS) && (!(rh->status & RSL_RECORD_EXPIRED)))
2890 || !GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2891 &rh->private_local_zone))
2893 if (is_canonical(rh->name))
2895 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2896 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
2899 rh->proc = &handle_record_ns;
2900 resolve_record_ns(rh);
2904 /* give up, cannot resolve */
2905 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2906 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
2909 finish_lookup(rh, rlh, rd_count, rd);
2910 //rlh->proc(rlh->proc_cls, 0, NULL);
2915 if (rh->only_cached == GNUNET_YES)
2917 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2918 "GNS_PHASE_DELEGATE_NS-%llu: Only cache resolution, no result\n",
2920 finish_lookup(rh, rlh, rd_count, rd);
2924 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2925 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
2927 rh->proc = &handle_delegation_dht;
2928 resolve_delegation_dht(rh);
2933 * This is a callback function that should give us only PKEY
2934 * records. Used to query the namestore for the authority (PKEY)
2935 * for 'name'. It will recursively try to resolve the
2936 * authority for a given name from the namestore.
2938 * @param cls the pending query
2939 * @param key the key of the zone we did the lookup
2940 * @param expiration expiration date of the record data set in the namestore
2941 * @param name the name for which we need an authority
2942 * @param rd_count the number of records with 'name'
2943 * @param rd the record data
2944 * @param signature the signature of the authority for the record data
2947 process_delegation_result_ns (void* cls,
2948 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2949 struct GNUNET_TIME_Absolute expiration,
2951 unsigned int rd_count,
2952 const struct GNUNET_NAMESTORE_RecordData *rd,
2953 const struct GNUNET_CRYPTO_RsaSignature *signature)
2955 struct ResolverHandle *rh;
2956 struct GNUNET_TIME_Relative remaining_time;
2957 struct GNUNET_CRYPTO_ShortHashCode zone;
2958 char new_name[MAX_DNS_NAME_LENGTH];
2960 struct GNUNET_TIME_Absolute et;
2962 rh = (struct ResolverHandle *)cls;
2963 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2964 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
2967 GNUNET_CRYPTO_short_hash(key,
2968 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2970 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2976 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2977 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
2979 rh->status |= RSL_RECORD_EXISTS;
2981 if (remaining_time.rel_value == 0)
2983 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2984 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
2986 rh->status |= RSL_RECORD_EXPIRED;
2991 * No authority found in namestore.
2996 * We did not find an authority in the namestore
3001 * Promote this authority back to a name maybe it is
3004 if (strcmp(rh->name, "") == 0)
3006 /* simply promote back */
3007 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3008 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3009 rh->id, rh->authority_name);
3010 strcpy(rh->name, rh->authority_name);
3014 /* add back to existing name */
3015 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3016 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
3017 rh->id, rh->authority_name, rh->name);
3018 //memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
3019 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3020 rh->name, rh->authority_name);
3021 //strcpy(new_name, rh->name);
3022 //strcpy(new_name+strlen(new_name), ".");
3023 //strcpy(new_name+strlen(new_name), rh->authority_name);
3024 strcpy(rh->name, new_name);
3025 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3026 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n", rh->id, rh->name);
3028 rh->proc(rh->proc_cls, rh, 0, NULL);
3033 * We found an authority that may be able to help us
3034 * move on with query
3035 * Note only 1 pkey should have been returned.. anything else would be strange
3037 for (i=0; i<rd_count;i++)
3041 * A CNAME. Like regular DNS this means the is no other record for this
3044 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_CNAME)
3046 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3047 "GNS_PHASE_DELEGATE_NS-%llu: CNAME found.\n",
3049 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3050 "GNS_PHASE_DELEGATE_NS-%llu: new name to resolve: %s.\n",
3053 rh->status |= RSL_CNAME_FOUND;
3054 rh->proc (rh->proc_cls, rh, rd_count, rd);
3061 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
3063 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3064 "GNS_PHASE_DELEGATE_NS-%llu: VPN found.\n",
3066 rh->status |= RSL_DELEGATE_VPN;
3067 rh->proc (rh->proc_cls, rh, rd_count, rd);
3073 * FIXME make optional
3075 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS)
3077 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3078 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
3080 rh->status |= RSL_DELEGATE_NS;
3081 rh->proc (rh->proc_cls, rh, rd_count, rd);
3085 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
3088 rh->status |= RSL_DELEGATE_PKEY;
3090 if (ignore_pending_records &&
3091 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
3093 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3094 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
3100 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
3101 et.abs_value = rd[i].expiration_time;
3102 if ((GNUNET_TIME_absolute_get_remaining (et)).rel_value
3105 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3106 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
3108 if (remaining_time.rel_value == 0)
3110 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3111 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
3113 rh->authority_chain_head->fresh = 0;
3114 rh->proc(rh->proc_cls, rh, 0, NULL);
3122 * Resolve rest of query with new authority
3124 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
3125 memcpy(&rh->authority, rd[i].data,
3126 sizeof(struct GNUNET_CRYPTO_ShortHashCode));
3127 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
3128 auth->zone = rh->authority;
3129 memset(auth->name, 0, strlen(rh->authority_name)+1);
3130 strcpy(auth->name, rh->authority_name);
3131 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
3132 rh->authority_chain_tail,
3135 /* Check for key revocation and delegate */
3136 GNUNET_NAMESTORE_lookup_record (namestore_handle,
3139 GNUNET_GNS_RECORD_REV,
3140 &process_pkey_revocation_result_ns,
3149 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3150 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
3152 * If we have found some records for the LAST label
3153 * we return the results. Else null.
3155 if (strcmp(rh->name, "") == 0)
3157 /* Start shortening */
3158 if ((rh->priv_key != NULL) && is_canonical (rh->name))
3160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3161 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
3163 start_shorten (rh->authority_chain_tail,
3166 /* simply promote back */
3167 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3168 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
3169 rh->id, rh->authority_name);
3170 strcpy(rh->name, rh->authority_name);
3171 rh->proc(rh->proc_cls, rh, rd_count, rd);
3175 rh->proc(rh->proc_cls, rh, 0, NULL);
3181 * Resolve the delegation chain for the request in our namestore
3183 * @param rh the resolver handle
3186 resolve_delegation_ns (struct ResolverHandle *rh)
3188 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3189 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
3191 pop_tld(rh->name, rh->authority_name);
3192 GNUNET_NAMESTORE_lookup_record(namestore_handle,
3195 GNUNET_GNS_RECORD_ANY,
3196 &process_delegation_result_ns,
3203 * Lookup of a record in a specific zone
3204 * calls lookup result processor on result
3206 * @param zone the root zone
3207 * @param pzone the private local zone
3208 * @param record_type the record type to look up
3209 * @param name the name to look up
3210 * @param key a private key for use with PSEU import (can be NULL)
3211 * @param timeout timeout for resolution
3212 * @param only_cached GNUNET_NO to only check locally not DHT for performance
3213 * @param proc the processor to call on result
3214 * @param cls the closure to pass to proc
3217 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
3218 struct GNUNET_CRYPTO_ShortHashCode pzone,
3219 uint32_t record_type,
3221 struct GNUNET_CRYPTO_RsaPrivateKey *key,
3222 struct GNUNET_TIME_Relative timeout,
3224 RecordLookupProcessor proc,
3227 struct ResolverHandle *rh;
3228 struct RecordLookupHandle* rlh;
3229 char string_hash[MAX_DNS_LABEL_LENGTH];
3230 char nzkey[MAX_DNS_LABEL_LENGTH];
3231 char* nzkey_ptr = nzkey;
3233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3234 "Starting resolution for %s (type=%d)!\n",
3238 if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
3240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3241 "%s is canonical and not gnunet -> cannot resolve!\n", name);
3246 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
3247 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
3249 rh->authority = zone;
3253 rh->timeout = timeout;
3254 rh->get_handle = NULL;
3255 rh->private_local_zone = pzone;
3256 rh->only_cached = only_cached;
3260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3261 "No shorten key for resolution\n");
3264 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3267 * Set timeout for authority lookup phase to 1/2
3269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3270 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3271 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3272 GNUNET_TIME_relative_divide(timeout, 2),
3273 &handle_lookup_timeout,
3275 rh->timeout_cont = &dht_authority_lookup_timeout;
3276 rh->timeout_cont_cls = rh;
3280 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3281 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3284 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3287 * Only 'gnunet' given
3289 strcpy(rh->name, "\0");
3293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3294 "Checking for TLD...\n");
3295 if (is_zkey_tld(name) == GNUNET_YES)
3297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3300 * This is a zkey tld
3301 * build hash and use as initial authority
3304 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3305 memcpy(rh->name, name,
3306 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3307 pop_tld(rh->name, string_hash);
3309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3310 "ZKEY is %s!\n", string_hash);
3312 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3314 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3318 "Cannot convert ZKEY %s to hash!\n", string_hash);
3328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3331 * Presumably GNUNET tld
3334 strlen(name)-strlen(GNUNET_GNS_TLD));
3335 memcpy(rh->name, name,
3336 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3341 * Initialize authority chain
3343 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3344 rh->authority_chain_head->prev = NULL;
3345 rh->authority_chain_head->next = NULL;
3346 rh->authority_chain_tail = rh->authority_chain_head;
3347 rh->authority_chain_head->zone = rh->authority;
3350 * Copy original query into lookup handle
3352 rlh->record_type = record_type;
3353 memset(rlh->name, 0, strlen(name) + 1);
3354 strcpy(rlh->name, name);
3356 rlh->proc_cls = cls;
3358 rh->proc = &handle_delegation_ns;
3359 resolve_delegation_ns(rh);
3362 /******** END Record Resolver ***********/
3365 * Callback calles by namestore for a zone to name
3368 * @param cls the closure
3369 * @param zone_key the zone we queried
3370 * @param expire the expiration time of the name
3371 * @param name the name found or NULL
3372 * @param rd_len number of records for the name
3373 * @param rd the record data (PKEY) for the name
3374 * @param signature the signature for the record data
3377 process_zone_to_name_shorten_root (void *cls,
3378 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3379 struct GNUNET_TIME_Absolute expire,
3381 unsigned int rd_len,
3382 const struct GNUNET_NAMESTORE_RecordData *rd,
3383 const struct GNUNET_CRYPTO_RsaSignature *signature);
3387 * Callback called by namestore for a zone to name
3390 * @param cls the closure
3391 * @param zone_key the zone we queried
3392 * @param expire the expiration time of the name
3393 * @param name the name found or NULL
3394 * @param rd_len number of records for the name
3395 * @param rd the record data (PKEY) for the name
3396 * @param signature the signature for the record data
3399 process_zone_to_name_shorten_shorten (void *cls,
3400 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3401 struct GNUNET_TIME_Absolute expire,
3403 unsigned int rd_len,
3404 const struct GNUNET_NAMESTORE_RecordData *rd,
3405 const struct GNUNET_CRYPTO_RsaSignature *signature)
3407 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3408 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3409 struct AuthorityChain *next_authority;
3411 char result[MAX_DNS_NAME_LENGTH];
3412 char tmp_name[MAX_DNS_NAME_LENGTH];
3415 /* we found a match in our own root zone */
3418 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3419 "result strlen %d\n", strlen(name));
3420 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3421 memset(result, 0, answer_len);
3423 if (strlen(rh->name) > 0)
3425 sprintf (result, "%s.%s.%s.%s.%s",
3427 nsh->shorten_zone_name, nsh->private_zone_name,
3432 sprintf (result, "%s.%s.%s.%s", name,
3433 nsh->shorten_zone_name, nsh->private_zone_name,
3437 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3438 "Found shorten result %s\n", result);
3439 if (strlen (nsh->result) > strlen (result))
3440 strcpy (nsh->result, result);
3442 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3443 nsh->shorten_zone) == 0)
3446 * This is our zone append .gnunet unless name is empty
3447 * (it shouldn't be, usually FIXME what happens if we
3448 * shorten to our zone to a "" record??)
3451 sprintf (result, "%s.%s.%s.%s",
3453 nsh->shorten_zone_name, nsh->private_zone_name,
3455 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3456 "Our zone: Found %s as shorten result\n", result);
3458 if (strlen (nsh->result) > strlen (result))
3459 strcpy (nsh->result, result);
3460 //nsh->proc(nsh->proc_cls, result);
3462 //free_resolver_handle(rh);
3469 * continue with next authority if exists
3471 if ((rh->authority_chain_head->next == NULL))
3473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3474 "Sending %s as shorten result\n", nsh->result);
3475 nsh->proc(nsh->proc_cls, nsh->result);
3477 free_resolver_handle (rh);
3480 next_authority = rh->authority_chain_head;
3482 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3483 "%s.%s", rh->name, next_authority->name);
3485 strcpy(rh->name, tmp_name);
3486 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3487 "No PSEU found for authority %s. Promoting back: %s\n",
3488 next_authority->name, rh->name);
3490 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3491 rh->authority_chain_tail,
3494 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3495 &rh->authority_chain_tail->zone,
3496 &rh->authority_chain_head->zone,
3497 &process_zone_to_name_shorten_root,
3502 * Callback calles by namestore for a zone to name
3505 * @param cls the closure
3506 * @param zone_key the zone we queried
3507 * @param expire the expiration time of the name
3508 * @param name the name found or NULL
3509 * @param rd_len number of records for the name
3510 * @param rd the record data (PKEY) for the name
3511 * @param signature the signature for the record data
3514 process_zone_to_name_shorten_private (void *cls,
3515 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3516 struct GNUNET_TIME_Absolute expire,
3518 unsigned int rd_len,
3519 const struct GNUNET_NAMESTORE_RecordData *rd,
3520 const struct GNUNET_CRYPTO_RsaSignature *signature)
3522 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3523 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3524 struct AuthorityChain *next_authority;
3526 char result[MAX_DNS_NAME_LENGTH];
3527 char tmp_name[MAX_DNS_NAME_LENGTH];
3530 /* we found a match in our own root zone */
3533 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3534 "result strlen %d\n", strlen(name));
3535 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3536 memset(result, 0, answer_len);
3538 if (strlen(rh->name) > 0)
3540 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3544 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3547 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3548 "Found shorten result %s\n", result);
3549 if (strlen (nsh->result) > strlen (result))
3550 strcpy (nsh->result, result);
3552 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3553 nsh->private_zone) == 0)
3556 * This is our zone append .gnunet unless name is empty
3557 * (it shouldn't be, usually FIXME what happens if we
3558 * shorten to our zone to a "" record??)
3561 sprintf (result, "%s.%s.%s",
3562 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3563 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3564 "Our private zone: Found %s as shorten result %s\n", result);
3565 if (strlen (nsh->result) > strlen (result))
3566 strcpy (nsh->result, result);
3569 if (nsh->shorten_zone != NULL)
3571 /* backtrack authorities for names in priv zone */
3572 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3574 &rh->authority_chain_head->zone,
3575 &process_zone_to_name_shorten_shorten,
3582 * continue with next authority if exists
3584 if ((rh->authority_chain_head->next == NULL))
3586 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3587 "Sending %s as shorten result\n", nsh->result);
3588 nsh->proc(nsh->proc_cls, nsh->result);
3590 free_resolver_handle(rh);
3593 next_authority = rh->authority_chain_head;
3595 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3596 "%s.%s", rh->name, next_authority->name);
3598 strcpy(rh->name, tmp_name);
3599 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3600 "No PSEU found for authority %s. Promoting back: %s\n",
3601 next_authority->name, rh->name);
3603 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3604 rh->authority_chain_tail,
3607 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3608 &rh->authority_chain_tail->zone,
3609 &rh->authority_chain_head->zone,
3610 &process_zone_to_name_shorten_root,
3616 * Callback calles by namestore for a zone to name
3619 * @param cls the closure
3620 * @param zone_key the zone we queried
3621 * @param expire the expiration time of the name
3622 * @param name the name found or NULL
3623 * @param rd_len number of records for the name
3624 * @param rd the record data (PKEY) for the name
3625 * @param signature the signature for the record data
3628 process_zone_to_name_shorten_root (void *cls,
3629 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3630 struct GNUNET_TIME_Absolute expire,
3632 unsigned int rd_len,
3633 const struct GNUNET_NAMESTORE_RecordData *rd,
3634 const struct GNUNET_CRYPTO_RsaSignature *signature)
3636 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3637 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3638 struct AuthorityChain *next_authority;
3640 char result[MAX_DNS_NAME_LENGTH];
3641 char tmp_name[MAX_DNS_NAME_LENGTH];
3644 /* we found a match in our own root zone */
3647 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3648 "result strlen %d\n", strlen(name));
3649 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3650 memset(result, 0, answer_len);
3652 if (strlen(rh->name) > 0)
3654 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3658 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3661 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3662 "Found shorten result %s\n", result);
3663 if (strlen (nsh->result) > strlen (result))
3664 strcpy (nsh->result, result);
3666 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3667 nsh->root_zone) == 0)
3670 * This is our zone append .gnunet unless name is empty
3671 * (it shouldn't be, usually FIXME what happens if we
3672 * shorten to our zone to a "" record??)
3675 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3676 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3677 "Our zone: Found %s as shorten result\n", result);
3678 if (strlen (nsh->result) > strlen (result))
3679 strcpy (nsh->result, result);
3682 if (nsh->private_zone != NULL)
3684 /* backtrack authorities for names in priv zone */
3685 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3687 &rh->authority_chain_head->zone,
3688 &process_zone_to_name_shorten_private,
3695 * continue with next authority if exists
3697 if ((rh->authority_chain_head->next == NULL))
3699 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3700 "Sending %s as shorten result\n", nsh->result);
3701 nsh->proc(nsh->proc_cls, nsh->result);
3703 free_resolver_handle(rh);
3706 next_authority = rh->authority_chain_head;
3708 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3709 "%s.%s", rh->name, next_authority->name);
3711 strcpy(rh->name, tmp_name);
3712 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3713 "No PSEU found for authority %s. Promoting back: %s\n",
3714 next_authority->name, rh->name);
3716 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3717 rh->authority_chain_tail,
3720 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3721 &rh->authority_chain_tail->zone,
3722 &rh->authority_chain_head->zone,
3723 &process_zone_to_name_shorten_root,
3730 * Process result from namestore delegation lookup
3731 * for shorten operation
3733 * @param cls the client shorten handle
3734 * @param rh the resolver handle
3735 * @param rd_count number of results (0)
3736 * @param rd data (NULL)
3739 handle_delegation_ns_shorten (void* cls,
3740 struct ResolverHandle *rh,
3742 const struct GNUNET_NAMESTORE_RecordData *rd)
3744 struct NameShortenHandle *nsh;
3745 char result[MAX_DNS_NAME_LENGTH];
3747 nsh = (struct NameShortenHandle *)cls;
3750 * At this point rh->name contains the part of the name
3751 * that we do not have a PKEY in our namestore to resolve.
3752 * The authority chain in the resolver handle is now
3753 * useful to backtrack if needed
3756 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3757 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3758 memset(result, 0, sizeof (result));
3760 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3761 nsh->root_zone) == 0)
3764 * This is our zone append .gnunet unless name is empty
3765 * (it shouldn't be, usually FIXME what happens if we
3766 * shorten to our zone to a "" record??)
3769 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3770 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3771 "Our zone: Found %s as shorten result\n", result);
3773 if (strlen (nsh->result) > strlen (result))
3774 strcpy (nsh->result, result);
3777 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3778 nsh->private_zone) == 0)
3781 * This is our zone append .gnunet unless name is empty
3782 * (it shouldn't be, usually FIXME what happens if we
3783 * shorten to our zone to a "" record??)
3786 sprintf (result, "%s.%s.%s",
3787 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3788 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3789 "Our zone: Found %s as shorten result %s\n", result);
3791 if (strlen (nsh->result) > strlen (result))
3792 strcpy (nsh->result, result);
3794 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3795 nsh->shorten_zone) == 0)
3798 * This is our zone append .gnunet unless name is empty
3799 * (it shouldn't be, usually FIXME what happens if we
3800 * shorten to our zone to a "" record??)
3803 sprintf (result, "%s.%s.%s",
3804 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3805 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3806 "Our zone: Found %s as shorten result\n", result);
3808 if (strlen (nsh->result) > strlen (result))
3809 strcpy (nsh->result, result);
3813 /* backtrack authorities for names */
3814 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3816 &rh->authority_chain_head->zone,
3817 &process_zone_to_name_shorten_root,
3824 * Callback calles by namestore for a zone to name
3827 * @param cls the closure
3828 * @param zone_key the zone we queried
3829 * @param expire the expiration time of the name
3830 * @param name the name found or NULL
3831 * @param rd_len number of records for the name
3832 * @param rd the record data (PKEY) for the name
3833 * @param signature the signature for the record data
3836 process_zone_to_name_zkey(void *cls,
3837 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3838 struct GNUNET_TIME_Absolute expire,
3840 unsigned int rd_len,
3841 const struct GNUNET_NAMESTORE_RecordData *rd,
3842 const struct GNUNET_CRYPTO_RsaSignature *signature)
3844 struct ResolverHandle *rh = cls;
3845 struct NameShortenHandle *nsh = rh->proc_cls;
3846 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
3847 char new_name[MAX_DNS_NAME_LENGTH];
3849 /* zkey not in our zone */
3853 * In this case we have not given this PKEY a name (yet)
3854 * It is either just not in our zone or not even cached
3855 * Since we do not know at this point we will not try to shorten
3856 * because PKEY import will happen if the user follows the zkey
3859 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
3861 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3862 "No name found for zkey %s returning verbatim!\n", enc);
3863 if (strcmp(rh->name, "") != 0)
3864 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
3865 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
3867 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3868 enc, GNUNET_GNS_TLD_ZKEY);
3870 strcpy (nsh->result, new_name);
3872 nsh->proc(nsh->proc_cls, new_name);
3874 free_resolver_handle(rh);
3878 if (strcmp(rh->name, "") != 0)
3879 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3882 strcpy(new_name, name);
3884 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3885 "Continue shorten for %s!\n", new_name);
3887 strcpy(rh->name, new_name);
3889 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3890 rh->authority_chain_tail = rh->authority_chain_head;
3891 rh->authority_chain_head->zone = rh->authority;
3894 /* Start delegation resolution in our namestore */
3895 resolve_delegation_ns(rh);
3900 * Shorten api from resolver
3902 * @param zone the root zone to use
3903 * @param pzone the private zone to use
3904 * @param szone the shorten zone to use
3905 * @param name the name to shorten
3906 * @param private_zone_name name of the private zone
3907 * @param shorten_zone_name name of the shorten zone
3908 * @param proc the processor to call with result
3909 * @param proc_cls closure to pass to proc
3912 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
3913 struct GNUNET_CRYPTO_ShortHashCode *pzone,
3914 struct GNUNET_CRYPTO_ShortHashCode *szone,
3916 const char* private_zone_name,
3917 const char* shorten_zone_name,
3918 ShortenResultProcessor proc,
3921 struct ResolverHandle *rh;
3922 struct NameShortenHandle *nsh;
3923 char string_hash[MAX_DNS_LABEL_LENGTH];
3924 struct GNUNET_CRYPTO_ShortHashCode zkey;
3925 char nzkey[MAX_DNS_LABEL_LENGTH];
3926 char* nzkey_ptr = nzkey;
3929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3930 "Starting shorten for %s!\n", name);
3932 if (is_canonical ((char*)name))
3934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3935 "%s is canonical. Returning verbatim\n", name);
3936 proc (proc_cls, name);
3940 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
3943 nsh->proc_cls = proc_cls;
3944 nsh->root_zone = zone;
3945 nsh->private_zone = pzone;
3946 nsh->shorten_zone = szone;
3947 strcpy (nsh->private_zone_name, private_zone_name);
3948 strcpy (nsh->shorten_zone_name, shorten_zone_name);
3949 strcpy (nsh->result, name);
3951 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3952 rh->authority = *zone;
3954 rh->priv_key = NULL;
3955 rh->proc = &handle_delegation_ns_shorten;
3958 rh->private_local_zone = *zone;
3960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3961 "Checking for TLD...\n");
3962 if (is_zkey_tld (name) == GNUNET_YES)
3964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3967 * This is a zkey tld
3968 * build hash and use as initial authority
3971 memset (rh->name, 0,
3972 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
3973 memcpy (rh->name, name,
3974 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
3975 pop_tld (rh->name, string_hash);
3977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3978 "ZKEY is %s!\n", string_hash);
3980 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
3982 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
3985 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3986 "Cannot convert ZKEY %s to hash!\n", nzkey);
3989 proc (proc_cls, name);
3993 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3996 &process_zone_to_name_zkey,
4003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4006 * Presumably GNUNET tld
4008 memset (rh->name, 0,
4009 strlen (name)-strlen (GNUNET_GNS_TLD));
4010 memcpy (rh->name, name,
4011 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
4014 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
4015 rh->authority_chain_tail = rh->authority_chain_head;
4016 rh->authority_chain_head->zone = *zone;
4019 /* Start delegation resolution in our namestore */
4020 resolve_delegation_ns (rh);
4023 /*********** END NAME SHORTEN ********************/
4027 * Process result from namestore delegation lookup
4028 * for get authority operation
4030 * @param cls the client get auth handle
4031 * @param rh the resolver handle
4032 * @param rd_count number of results (0)
4033 * @param rd data (NULL)
4036 handle_delegation_result_ns_get_auth(void* cls,
4037 struct ResolverHandle *rh,
4039 const struct GNUNET_NAMESTORE_RecordData *rd)
4041 struct GetNameAuthorityHandle* nah;
4042 char result[MAX_DNS_NAME_LENGTH];
4045 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
4048 * At this point rh->name contains the part of the name
4049 * that we do not have a PKEY in our namestore to resolve.
4050 * The authority chain in the resolver handle is now
4051 * useful to backtrack if needed
4054 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4055 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
4057 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4058 "Building response!\n");
4059 if (is_canonical(rh->name))
4062 * We successfully resolved the authority in the ns
4063 * FIXME for our purposes this is fine
4064 * but maybe we want to have an api that also looks
4065 * into the dht (i.e. option in message)
4067 if (strlen(rh->name) > strlen(nah->name))
4069 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4070 "Record name longer than original lookup name... odd!\n");
4074 answer_len = strlen(nah->name) - strlen(rh->name)
4075 + strlen(GNUNET_GNS_TLD) + 1;
4076 memset(result, 0, answer_len);
4077 strcpy(result, nah->name + strlen(rh->name) + 1);
4079 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4080 "Got authority result %s\n", result);
4082 nah->proc(nah->proc_cls, result);
4084 free_resolver_handle(rh);
4088 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4089 "Unable to resolve authority for remaining %s!\n", rh->name);
4090 nah->proc(nah->proc_cls, "");
4092 free_resolver_handle(rh);
4100 * Tries to resolve the authority for name
4103 * @param zone the root zone to look up for
4104 * @param pzone the private local zone
4105 * @param name the name to lookup up
4106 * @param proc the processor to call when finished
4107 * @param proc_cls the closure to pass to the processor
4110 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
4111 struct GNUNET_CRYPTO_ShortHashCode pzone,
4113 GetAuthorityResultProcessor proc,
4116 struct ResolverHandle *rh;
4117 struct GetNameAuthorityHandle *nah;
4119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4120 "Starting authority resolution for %s!\n", name);
4122 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
4123 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
4124 rh->authority = zone;
4126 rh->private_local_zone = pzone;
4128 if (strcmp(GNUNET_GNS_TLD, name) == 0)
4130 strcpy(rh->name, "\0");
4135 strlen(name)-strlen(GNUNET_GNS_TLD));
4136 memcpy(rh->name, name,
4137 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
4140 memset(nah->name, 0,
4142 strcpy(nah->name, name);
4144 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
4145 rh->authority_chain_tail = rh->authority_chain_head;
4146 rh->authority_chain_head->zone = zone;
4147 rh->proc = &handle_delegation_result_ns_get_auth;
4148 rh->proc_cls = (void*)nah;
4151 nah->proc_cls = proc_cls;
4153 /* Start delegation resolution in our namestore */
4154 resolve_delegation_ns(rh);
4158 /******** END GET AUTHORITY *************/
4160 /* end of gnunet-service-gns_resolver.c */