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 * a resolution identifier pool variable
86 * This is a non critical identifier useful for debugging
88 static unsigned long long rid = 0;
92 * Determine if this name is canonical.
94 * a.b.gnunet = not canonical
97 * @param name the name to test
98 * @return 1 if canonical
101 is_canonical(char* name)
103 uint32_t len = strlen(name);
106 for (i=0; i<len; i++)
108 if (*(name+i) == '.')
116 * Callback that shortens authorities
118 * @param gph the handle containing the name to shorten
121 shorten_authority_chain (struct GetPseuAuthorityHandle *gph);
125 * Namestore calls this function if we have record for this name.
126 * (or with rd_count=0 to indicate no matches)
128 * @param cls the pending query
129 * @param key the key of the zone we did the lookup
130 * @param expiration expiration date of the namestore entry
131 * @param name the name for which we need an authority
132 * @param rd_count the number of records with 'name'
133 * @param rd the record data
134 * @param signature the signature of the authority for the record data
137 process_pseu_lookup_ns (void* cls,
138 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
139 struct GNUNET_TIME_Absolute expiration,
140 const char *name, unsigned int rd_count,
141 const struct GNUNET_NAMESTORE_RecordData *rd,
142 const struct GNUNET_CRYPTO_RsaSignature *signature)
144 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
145 struct GNUNET_NAMESTORE_RecordData new_pkey;
146 struct AuthorityChain *iter;
150 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
151 "GNS_AUTO_PSEU: Name %s already taken in NS!\n", name);
152 if (0 == strcmp (gph->name, name))
154 if (gph->ahead->next != NULL)
156 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
160 iter = gph->ahead->next;
161 GNUNET_free (gph->ahead);
163 shorten_authority_chain (gph);
171 iter = gph->ahead->next;
172 GNUNET_free (gph->ahead);
174 } while (iter != NULL);
175 GNUNET_CRYPTO_rsa_key_free (gph->key);
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
182 "GNS_AUTO_PSEU: Trying delegated name %s\n", gph->name);
183 memcpy (gph->test_name, gph->name, strlen (gph->name)+1);
184 GNUNET_NAMESTORE_lookup_record (namestore_handle,
187 GNUNET_NAMESTORE_TYPE_ANY,
188 &process_pseu_lookup_ns,
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 "GNS_AUTO_PSEU: Name %s not taken in NS! Adding\n", gph->test_name);
197 new_pkey.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
198 new_pkey.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
199 new_pkey.data = &gph->ahead->zone;
200 new_pkey.record_type = GNUNET_GNS_RECORD_PKEY;
201 new_pkey.flags = GNUNET_NAMESTORE_RF_AUTHORITY
202 | GNUNET_NAMESTORE_RF_PRIVATE
203 | GNUNET_NAMESTORE_RF_PENDING;
204 GNUNET_NAMESTORE_record_create (namestore_handle,
212 iter = gph->ahead->next;
213 GNUNET_free (gph->ahead);
215 } while (iter != NULL);
216 GNUNET_CRYPTO_rsa_key_free (gph->key);
222 * process result of a dht pseu lookup
224 * @param gph the handle
225 * @param name the pseu result or NULL
228 process_pseu_result (struct GetPseuAuthorityHandle* gph, char* name)
232 memcpy (gph->test_name, gph->ahead->name, strlen (gph->ahead->name)+1);
236 memcpy (gph->test_name, name, strlen(name)+1);
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "GNS_AUTO_PSEU: Checking %s for collision in NS\n",
244 * Check for collision
246 GNUNET_NAMESTORE_lookup_record (namestore_handle,
249 GNUNET_NAMESTORE_TYPE_ANY,
250 &process_pseu_lookup_ns,
255 * Handle timeout for dht request
257 * @param cls the request handle as closure
258 * @param tc the task context
261 handle_auth_discovery_timeout(void *cls,
262 const struct GNUNET_SCHEDULER_TaskContext *tc)
264 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
265 struct AuthorityChain *iter;
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "GNS_GET_AUTH: dht lookup for query PSEU timed out.\n");
269 GNUNET_DHT_get_stop (gph->get_handle);
270 gph->get_handle = NULL;
272 if (gph->ahead->next != NULL)
274 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
278 iter = gph->ahead->next;
279 GNUNET_free (gph->ahead);
281 shorten_authority_chain (gph);
286 process_pseu_result (gph, NULL);
290 * Function called when we find a PSEU entry in the DHT
292 * @param cls the request handle
293 * @param exp lifetime
294 * @param key the key the record was stored under
295 * @param get_path get path
296 * @param get_path_length get path length
297 * @param put_path put path
298 * @param put_path_length put path length
299 * @param type the block type
300 * @param size the size of the record
301 * @param data the record data
304 process_auth_discovery_dht_result(void* cls,
305 struct GNUNET_TIME_Absolute exp,
306 const struct GNUNET_HashCode * key,
307 const struct GNUNET_PeerIdentity *get_path,
308 unsigned int get_path_length,
309 const struct GNUNET_PeerIdentity *put_path,
310 unsigned int put_path_length,
311 enum GNUNET_BLOCK_Type type,
312 size_t size, const void *data)
314 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
315 struct AuthorityChain *iter;
316 struct GNSNameRecordBlock *nrb;
317 char* rd_data = (char*)data;
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 "GNS_GET_AUTH: got dht result (size=%d)\n", size);
328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
329 "GNS_GET_AUTH: got dht result null!\n", size);
333 iter = gph->ahead->next;
334 GNUNET_free (gph->ahead);
336 } while (iter != NULL);
337 GNUNET_CRYPTO_rsa_key_free (gph->key);
342 nrb = (struct GNSNameRecordBlock*)data;
344 /* stop lookup and timeout task */
345 GNUNET_DHT_get_stop (gph->get_handle);
346 gph->get_handle = NULL;
347 GNUNET_SCHEDULER_cancel (gph->timeout);
349 gph->get_handle = NULL;
351 nrb = (struct GNSNameRecordBlock*)data;
353 name = (char*)&nrb[1];
354 num_records = ntohl (nrb->rd_count);
356 struct GNUNET_NAMESTORE_RecordData rd[num_records];
358 rd_data += strlen (name) + 1 + sizeof (struct GNSNameRecordBlock);
359 rd_size = size - strlen (name) - 1 - sizeof (struct GNSNameRecordBlock);
361 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
367 "GNS_GET_AUTH: Error deserializing data!\n");
371 for (i=0; i < num_records; i++)
373 if ((strcmp (name, "+") == 0) &&
374 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
377 process_pseu_result (gph, (char*)rd[i].data);
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in dht!\n");
386 if (gph->ahead->next != NULL)
388 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
392 iter = gph->ahead->next;
393 GNUNET_free (gph->ahead);
395 shorten_authority_chain (gph);
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "GNS_GET_AUTH: finished shorten, no results!\n");
401 process_pseu_result (gph, NULL);
405 * Process PSEU discovery for shorten via namestore
407 * @param cls the GetPseuAuthorityHandle
408 * @param key the public key
409 * @param expiration recorddata expiration
410 * @param name the looked up name
411 * @param rd_count number of records in set
412 * @param rd record data
413 * @param signature the signature
416 process_auth_discovery_ns_result(void* cls,
417 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
418 struct GNUNET_TIME_Absolute expiration,
420 unsigned int rd_count,
421 const struct GNUNET_NAMESTORE_RecordData *rd,
422 const struct GNUNET_CRYPTO_RsaSignature *signature)
425 struct GNUNET_CRYPTO_ShortHashCode name_hash;
426 struct GNUNET_HashCode lookup_key;
427 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
428 struct GNUNET_HashCode name_hash_double;
429 struct GNUNET_HashCode zone_hash_double;
431 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
432 struct AuthorityChain *iter;
440 GNUNET_CRYPTO_short_hash ("+", strlen ("+"), &name_hash);
441 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
442 GNUNET_CRYPTO_short_hash_double (&gph->ahead->zone, &zone_hash_double);
443 GNUNET_CRYPTO_hash_xor (&name_hash_double, &zone_hash_double, &lookup_key);
444 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "GNS_AUTO_PSEU: starting dht lookup for %s with key: %s\n",
448 "+", (char*)&lookup_key_string);
450 gph->timeout = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
451 &handle_auth_discovery_timeout, gph);
453 xquery = htonl (GNUNET_GNS_RECORD_PSEU);
455 GNUNET_assert (gph->get_handle == NULL);
457 gph->get_handle = GNUNET_DHT_get_start(dht_handle,
458 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
460 DHT_GNS_REPLICATION_LEVEL,
464 &process_auth_discovery_dht_result,
469 for (i=0; i < rd_count; i++)
471 if ((strcmp (name, "+") == 0) &&
472 (rd[i].record_type == GNUNET_GNS_RECORD_PSEU))
475 process_pseu_result (gph, (char*)rd[i].data);
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: no pseu in namestore!\n");
482 if (gph->ahead->next != NULL)
484 if (GNUNET_CRYPTO_short_hash_cmp (&gph->ahead->next->zone,
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS_GET_AUTH: trying next!\n");
488 iter = gph->ahead->next;
489 GNUNET_free (gph->ahead);
491 shorten_authority_chain (gph);
496 process_pseu_result (gph, NULL);
500 * Callback called by namestore for a zone to name
503 * @param cls the closure
504 * @param zone_key the zone we queried
505 * @param expire the expiration time of the name
506 * @param name the name found or NULL
507 * @param rd_len number of records for the name
508 * @param rd the record data (PKEY) for the name
509 * @param signature the signature for the record data
512 process_zone_to_name_discover (void *cls,
513 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
514 struct GNUNET_TIME_Absolute expire,
517 const struct GNUNET_NAMESTORE_RecordData *rd,
518 const struct GNUNET_CRYPTO_RsaSignature *signature)
520 struct GetPseuAuthorityHandle* gph = (struct GetPseuAuthorityHandle*)cls;
521 struct AuthorityChain *iter;
523 /* we found a match in our own zone */
526 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
527 "GNS_AUTO_PSEU: name for zone in our root %s\n", name);
532 iter = gph->ahead->next;
533 GNUNET_free (gph->ahead);
535 } while (iter != NULL);
536 GNUNET_CRYPTO_rsa_key_free (gph->key);
542 GNUNET_NAMESTORE_lookup_record (namestore_handle,
545 GNUNET_GNS_RECORD_PSEU,
546 &process_auth_discovery_ns_result,
554 * Callback that shortens authorities
556 * @param gph the handle to the shorten request
559 shorten_authority_chain (struct GetPseuAuthorityHandle *gph)
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
563 "GNS_AUTO_PSEU: New authority %s discovered\n",
566 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
569 &process_zone_to_name_discover,
575 start_shorten (struct AuthorityChain *atail,
576 struct GNUNET_CRYPTO_RsaPrivateKey *key)
578 struct AuthorityChain *new_head = NULL;
579 struct AuthorityChain *new_tail = NULL;
580 struct AuthorityChain *iter;
581 struct AuthorityChain *acopy;
582 struct GetPseuAuthorityHandle *gph;
583 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
584 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pb_key;
586 /* First copy the authority chain in reverse order */
587 for (iter = atail; iter != NULL; iter = iter->prev)
589 acopy = GNUNET_malloc (sizeof (struct AuthorityChain));
590 memcpy (acopy, iter, sizeof (struct AuthorityChain));
593 GNUNET_CONTAINER_DLL_insert (new_head, new_tail, acopy);
596 gph = GNUNET_malloc (sizeof (struct GetPseuAuthorityHandle));
598 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
599 pb_key = GNUNET_CRYPTO_rsa_encode_key (key);
600 gph->key = GNUNET_CRYPTO_rsa_decode_key ((char*)pb_key, ntohs (pb_key->len));
601 //gph->key = key;//GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
602 //memcpy (gph->key, key, sizeof (struct GNUNET_CRYPTO_RsaPrivateKey));
604 GNUNET_CRYPTO_short_hash (&pkey,
605 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
607 gph->ahead = new_head;
609 shorten_authority_chain (gph);
613 * Initialize the resolver
615 * @param nh the namestore handle
616 * @param dh the dht handle
617 * @param lz the local zone's hash
618 * @param max_bg_queries maximum number of parallel background queries in dht
619 * @param ignore_pending ignore records that still require user confirmation
621 * @return GNUNET_OK on success
624 gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
625 struct GNUNET_DHT_Handle *dh,
626 struct GNUNET_CRYPTO_ShortHashCode lz,
627 const struct GNUNET_CONFIGURATION_Handle *cfg,
628 unsigned long long max_bg_queries,
631 namestore_handle = nh;
635 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
636 max_allowed_background_queries = max_bg_queries;
637 ignore_pending_records = ignore_pending;
639 if (NULL == vpn_handle)
641 vpn_handle = GNUNET_VPN_connect (cfg);
642 if (NULL == vpn_handle)
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
645 "GNS_PHASE_INIT: Error connecting to VPN!\n");
647 return GNUNET_SYSERR;
651 if ((namestore_handle != NULL) && (dht_handle != NULL))
656 return GNUNET_SYSERR;
660 * Cleanup background lookups
662 * @param cls closure to iterator
663 * @param node heap nodes
664 * @param element the resolver handle
665 * @param cost heap cost
666 * @return always GNUNET_YES
669 cleanup_pending_background_queries(void* cls,
670 struct GNUNET_CONTAINER_HeapNode *node,
672 GNUNET_CONTAINER_HeapCostType cost)
674 struct ResolverHandle *rh = (struct ResolverHandle *)element;
675 ResolverCleanupContinuation cont = cls;
677 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
678 "GNS_CLEANUP-%llu: Terminating background lookup for %s\n",
680 GNUNET_DHT_get_stop(rh->get_handle);
681 rh->get_handle = NULL;
682 rh->proc(rh->proc_cls, rh, 0, NULL);
684 GNUNET_CONTAINER_heap_remove_node(node);
686 if (GNUNET_CONTAINER_heap_get_size(dht_lookup_heap) == 0)
698 gns_resolver_cleanup(ResolverCleanupContinuation cont)
700 unsigned int s = GNUNET_CONTAINER_heap_get_size(dht_lookup_heap);
701 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
702 "GNS_CLEANUP: %d pending background queries to terminate\n", s);
705 GNUNET_CONTAINER_heap_iterate (dht_lookup_heap,
706 &cleanup_pending_background_queries,
714 * Helper function to free resolver handle
716 * @param rh the handle to free
719 free_resolver_handle(struct ResolverHandle* rh)
721 struct AuthorityChain *ac;
722 struct AuthorityChain *ac_next;
727 ac = rh->authority_chain_head;
736 if (NULL != rh->dns_raw_packet)
737 GNUNET_free (rh->dns_raw_packet);
744 * Callback when record data is put into namestore
746 * @param cls the closure
747 * @param success GNUNET_OK on success
748 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
751 on_namestore_record_put_result(void *cls,
755 if (GNUNET_NO == success)
757 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
758 "GNS_NS: records already in namestore\n");
761 else if (GNUNET_YES == success)
763 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
764 "GNS_NS: records successfully put in namestore\n");
768 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
769 "GNS_NS: Error putting records into namestore: %s\n", emsg);
773 handle_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
775 struct ResolverHandle *rh = cls;
777 if (rh->timeout_cont)
778 rh->timeout_cont(rh->timeout_cont_cls, tc);
782 * Processor for background lookups in the DHT
784 * @param cls closure (NULL)
785 * @param rd_count number of records found (not 0)
786 * @param rd record data
789 background_lookup_result_processor(void *cls,
791 const struct GNUNET_NAMESTORE_RecordData *rd)
793 //We could do sth verbose/more useful here but it doesn't make any difference
794 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
795 "GNS_BG: background dht lookup for finished. (%d results)\n",
800 * Handle timeout for DHT requests
802 * @param cls the request handle as closure
803 * @param tc the task context
806 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
808 struct ResolverHandle *rh = cls;
809 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
810 char new_name[MAX_DNS_NAME_LENGTH];
812 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
813 "GNS_PHASE_REC-%d: dht lookup for query %s (%ds)timed out.\n",
814 rh->id, rh->name, rh->timeout.rel_value);
816 * Start resolution in bg
818 //strcpy(new_name, rh->name);
819 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
820 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
821 rh->name, GNUNET_GNS_TLD);
823 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
824 "GNS_PHASE_REC-%d: Starting background lookup for %s type %d\n",
825 rh->id, new_name, rlh->record_type);
827 gns_resolver_lookup_record(rh->authority,
828 rh->private_local_zone,
832 GNUNET_TIME_UNIT_FOREVER_REL,
834 &background_lookup_result_processor,
836 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
838 GNUNET_DHT_get_stop (rh->get_handle);
839 rh->get_handle = NULL;
840 rh->proc(rh->proc_cls, rh, 0, NULL);
845 * Function called when we get a result from the dht
846 * for our record query
848 * @param cls the request handle
849 * @param exp lifetime
850 * @param key the key the record was stored under
851 * @param get_path get path
852 * @param get_path_length get path length
853 * @param put_path put path
854 * @param put_path_length put path length
855 * @param type the block type
856 * @param size the size of the record
857 * @param data the record data
860 process_record_result_dht(void* cls,
861 struct GNUNET_TIME_Absolute exp,
862 const struct GNUNET_HashCode * key,
863 const struct GNUNET_PeerIdentity *get_path,
864 unsigned int get_path_length,
865 const struct GNUNET_PeerIdentity *put_path,
866 unsigned int put_path_length,
867 enum GNUNET_BLOCK_Type type,
868 size_t size, const void *data)
870 struct ResolverHandle *rh;
871 struct RecordLookupHandle *rlh;
872 struct GNSNameRecordBlock *nrb;
873 uint32_t num_records;
875 char* rd_data = (char*)data;
879 rh = (struct ResolverHandle *)cls;
880 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
881 "GNS_PHASE_REC-%d: got dht result (size=%d)\n", rh->id, size);
886 //FIXME maybe check expiration here, check block type
889 rlh = (struct RecordLookupHandle *) rh->proc_cls;
890 nrb = (struct GNSNameRecordBlock*)data;
892 /* stop lookup and timeout task */
893 GNUNET_DHT_get_stop (rh->get_handle);
894 rh->get_handle = NULL;
896 if (rh->dht_heap_node != NULL)
898 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
899 rh->dht_heap_node = NULL;
902 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
904 GNUNET_SCHEDULER_cancel(rh->timeout_task);
905 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
908 rh->get_handle = NULL;
909 name = (char*)&nrb[1];
910 num_records = ntohl(nrb->rd_count);
912 struct GNUNET_NAMESTORE_RecordData rd[num_records];
914 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
915 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
917 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
922 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
923 "GNS_PHASE_REC-%d: Error deserializing data!\n", rh->id);
927 for (i=0; i<num_records; i++)
929 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
930 "GNS_PHASE_REC-%d: Got name: %s (wanted %s)\n",
931 rh->id, name, rh->name);
932 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
933 "GNS_PHASE_REC-%d: Got type: %d\n",
934 rh->id, rd[i].record_type);
935 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
936 "GNS_PHASE_REC-%d: Got data length: %d\n",
937 rh->id, rd[i].data_size);
938 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
939 "GNS_PHASE_REC-%d: Got flag %d\n",
940 rh->id, rd[i].flags);
942 if ((strcmp(name, rh->name) == 0) &&
943 (rd[i].record_type == rlh->record_type))
951 * FIXME check pubkey against existing key in namestore?
952 * https://gnunet.org/bugs/view.php?id=2179
955 /* Save to namestore */
956 GNUNET_NAMESTORE_record_put (namestore_handle,
963 &on_namestore_record_put_result, //cont
968 rh->proc(rh->proc_cls, rh, num_records, rd);
970 rh->proc(rh->proc_cls, rh, 0, NULL);
977 * Start DHT lookup for a (name -> query->record_type) record in
978 * rh->authority's zone
980 * @param rh the pending gns query context
983 resolve_record_dht(struct ResolverHandle *rh)
986 struct GNUNET_CRYPTO_ShortHashCode name_hash;
987 struct GNUNET_HashCode lookup_key;
988 struct GNUNET_HashCode name_hash_double;
989 struct GNUNET_HashCode zone_hash_double;
990 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
991 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
992 struct ResolverHandle *rh_heap_root;
994 GNUNET_CRYPTO_short_hash(rh->name, strlen(rh->name), &name_hash);
995 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
996 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
997 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
998 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
1000 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1001 "GNS_PHASE_REC-%d: starting dht lookup for %s with key: %s\n",
1002 rh->id, rh->name, (char*)&lookup_key_string);
1004 //rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1005 rh->dht_heap_node = NULL;
1007 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1010 * Update timeout if necessary
1012 if (rh->timeout_task == GNUNET_SCHEDULER_NO_TASK)
1015 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1016 "GNS_PHASE_REC-%d: Adjusting timeout\n", rh->id);
1018 * Set timeout for authority lookup phase to 1/2
1020 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
1021 GNUNET_TIME_relative_divide(rh->timeout, 2),
1022 &handle_lookup_timeout,
1025 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1026 // &dht_lookup_timeout,
1028 rh->timeout_cont = &dht_lookup_timeout;
1029 rh->timeout_cont_cls = rh;
1033 if (max_allowed_background_queries <=
1034 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
1036 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
1037 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
1038 rh_heap_root->get_handle = NULL;
1039 rh_heap_root->dht_heap_node = NULL;
1041 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1042 "GNS_PHASE_REC-%d: Replacing oldest background query for %s\n",
1043 rh->id, rh_heap_root->name);
1044 rh_heap_root->proc(rh_heap_root->proc_cls,
1049 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
1051 GNUNET_TIME_absolute_get().abs_value);
1054 xquery = htonl(rlh->record_type);
1056 GNUNET_assert(rh->get_handle == NULL);
1057 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1058 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1060 DHT_GNS_REPLICATION_LEVEL,
1064 &process_record_result_dht,
1071 * Namestore calls this function if we have record for this name.
1072 * (or with rd_count=0 to indicate no matches)
1074 * @param cls the pending query
1075 * @param key the key of the zone we did the lookup
1076 * @param expiration expiration date of the namestore entry
1077 * @param name the name for which we need an authority
1078 * @param rd_count the number of records with 'name'
1079 * @param rd the record data
1080 * @param signature the signature of the authority for the record data
1083 process_record_result_ns(void* cls,
1084 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1085 struct GNUNET_TIME_Absolute expiration,
1086 const char *name, unsigned int rd_count,
1087 const struct GNUNET_NAMESTORE_RecordData *rd,
1088 const struct GNUNET_CRYPTO_RsaSignature *signature)
1090 struct ResolverHandle *rh;
1091 struct RecordLookupHandle *rlh;
1092 struct GNUNET_TIME_Relative remaining_time;
1093 struct GNUNET_CRYPTO_ShortHashCode zone;
1095 rh = (struct ResolverHandle *) cls;
1096 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1097 GNUNET_CRYPTO_short_hash(key,
1098 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1100 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1108 rh->status |= RSL_RECORD_EXISTS;
1111 if (remaining_time.rel_value == 0)
1113 rh->status |= RSL_RECORD_EXPIRED;
1119 * Lookup terminated and no results
1121 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1122 "GNS_PHASE_REC-%d: Namestore lookup for %s terminated without results\n",
1125 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1126 "GNS_PHASE_REC-%d: Record %s unknown in namestore\n",
1129 * Our zone and no result? Cannot resolve TT
1131 rh->proc(rh->proc_cls, rh, 0, NULL);
1138 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1139 "GNS_PHASE_REC-%d: Processing additional result %s from namestore\n",
1142 for (i=0; i<rd_count;i++)
1145 if (rd[i].record_type != rlh->record_type)
1148 if (ignore_pending_records &&
1149 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
1151 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1152 "GNS_PHASE_REC-%d: Record %s is awaiting user confirmation. Skipping\n",
1157 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1160 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1161 "GNS_PHASE_REC-%d: This record is expired. Skipping\n",
1173 if (rh->answered == 0)
1175 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1176 "GNS_PHASE_REC-%d: No answers found. This is odd!\n", rh->id);
1177 rh->proc(rh->proc_cls, rh, 0, NULL);
1181 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1182 "GNS_PHASE_REC-%d: Found %d answer(s) to query in %d records!\n",
1183 rh->id, rh->answered, rd_count);
1185 rh->proc(rh->proc_cls, rh, rd_count, rd);
1191 * VPN redirect result callback
1193 * @param cls the resolver handle
1194 * @param af the requested address family
1195 * @param address in_addr(6) respectively
1198 process_record_result_vpn (void* cls, int af, const void *address)
1200 struct ResolverHandle *rh = cls;
1201 struct RecordLookupHandle *rlh;
1202 struct GNUNET_NAMESTORE_RecordData rd;
1204 rlh = (struct RecordLookupHandle *)rh->proc_cls;
1206 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1207 "GNS_PHASE_REC_VPN-%d: Got answer from VPN to query!\n",
1211 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1212 "GNS_PHASE_REC-%d: Answer is IPv4!\n",
1214 if (rlh->record_type != GNUNET_GNS_RECORD_TYPE_A)
1216 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1217 "GNS_PHASE_REC-%d: Requested record is not IPv4!\n",
1219 rh->proc (rh->proc_cls, rh, 0, NULL);
1222 rd.record_type = GNUNET_GNS_RECORD_TYPE_A;
1223 rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
1225 rd.data_size = sizeof (struct in_addr);
1227 rh->proc (rh->proc_cls, rh, 1, &rd);
1230 else if (af == AF_INET6)
1232 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1233 "GNS_PHASE_REC-%d: Answer is IPv6!\n",
1235 if (rlh->record_type != GNUNET_GNS_RECORD_AAAA)
1237 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1238 "GNS_PHASE_REC-%d: Requested record is not IPv6!\n",
1240 rh->proc (rh->proc_cls, rh, 0, NULL);
1243 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1244 rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
1246 rd.data_size = sizeof (struct in6_addr);
1248 rh->proc (rh->proc_cls, rh, 1, &rd);
1252 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1253 "GNS_PHASE_REC-%d: Got garbage from VPN!\n",
1255 rh->proc (rh->proc_cls, rh, 0, NULL);
1262 * @param rh resolver handle
1263 * @param rlh record lookup handle
1264 * @param rd_count number of results
1268 finish_lookup(struct ResolverHandle *rh,
1269 struct RecordLookupHandle* rlh,
1270 unsigned int rd_count,
1271 const struct GNUNET_NAMESTORE_RecordData *rd);
1274 * Process VPN lookup result for record
1276 * @param cls the record lookup handle
1277 * @param rh resolver handle
1278 * @param rd_count number of results (1)
1279 * @param rd record data containing the result
1282 handle_record_vpn (void* cls, struct ResolverHandle *rh,
1283 unsigned int rd_count,
1284 const struct GNUNET_NAMESTORE_RecordData *rd)
1286 struct RecordLookupHandle* rlh = (struct RecordLookupHandle*) cls;
1290 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1291 "GNS_PHASE_REC_VPN-%d: VPN returned no records. (status: %d)!\n",
1294 /* give up, cannot resolve */
1295 finish_lookup(rh, rlh, 0, NULL);
1296 free_resolver_handle(rh);
1300 /* results found yay */
1301 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1302 "GNS_PHASE_REC_VPN-%d: Record resolved from VPN!", rh->id);
1304 finish_lookup(rh, rlh, rd_count, rd);
1306 free_resolver_handle(rh);
1311 * Sends a UDP dns query to a nameserver specified in the rh
1313 * @param rh the resolver handle
1316 send_dns_packet (struct ResolverHandle *rh);
1319 handle_dns_resolver (void *cls,
1320 const struct sockaddr *addr,
1323 struct ResolverHandle *rh = cls;
1324 struct RecordLookupHandle *rlh = rh->proc_cls;
1325 struct GNUNET_NAMESTORE_RecordData rd;
1329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1330 "No address found in DNS!\n");
1331 finish_lookup (rh, rlh, 0, NULL);
1332 free_resolver_handle (rh);
1336 if (addrlen == sizeof (struct sockaddr_in))
1337 rd.record_type = GNUNET_GNS_RECORD_TYPE_A;
1339 rd.record_type = GNUNET_GNS_RECORD_AAAA;
1341 rd.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
1342 rd.data_size = addrlen;
1345 finish_lookup (rh, rlh, 1, &rd);
1346 free_resolver_handle (rh);
1350 * Resolve DNS name via local stub resolver
1352 * @param rh the resolver handle
1355 resolve_dns_name (struct ResolverHandle *rh)
1357 struct RecordLookupHandle *rlh = rh->proc_cls;
1360 if ((rlh->record_type != GNUNET_GNS_RECORD_TYPE_A) &&
1361 (rlh->record_type != GNUNET_GNS_RECORD_AAAA))
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "Can only resolve A/AAAA via stub... abort\n");
1365 finish_lookup (rh, rlh, 0, NULL);
1366 free_resolver_handle (rh);
1370 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_A)
1375 //GNUNET_RESOLVER_connect (cfg); FIXME into init
1377 rh->dns_resolver_handle = GNUNET_RESOLVER_ip_get (rh->dns_name,
1380 &handle_dns_resolver,
1386 * Read DNS udp packet from socket
1388 * @param cls the resolver handle
1389 * @param tc task context
1392 read_dns_response (void *cls,
1393 const struct GNUNET_SCHEDULER_TaskContext *tc)
1395 struct ResolverHandle *rh = cls;
1396 struct RecordLookupHandle *rlh = rh->proc_cls;
1397 char buf[UINT16_MAX];
1399 struct sockaddr_in addr;
1401 struct GNUNET_DNSPARSER_Packet *packet;
1402 struct GNUNET_NAMESTORE_RecordData rd;
1403 int found_delegation = GNUNET_NO;
1404 int found_cname = GNUNET_NO;
1405 char* delegation_name = NULL;
1406 int zone_offset = 0;
1409 rh->dns_read_task = GNUNET_SCHEDULER_NO_TASK;
1410 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1412 /* timeout or shutdown */
1413 rh->proc (rh->proc_cls, rh, 0, NULL);
1414 GNUNET_NETWORK_socket_close (rh->dns_sock);
1415 free_resolver_handle (rh);
1419 addrlen = sizeof (addr);
1420 r = GNUNET_NETWORK_socket_recvfrom (rh->dns_sock,
1422 (struct sockaddr*) &addr,
1427 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "recvfrom");
1428 rh->proc (rh->proc_cls, rh, 0, NULL);
1429 GNUNET_NETWORK_socket_close (rh->dns_sock);
1430 free_resolver_handle (rh);
1434 packet = GNUNET_DNSPARSER_parse (buf, r);
1438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1439 "Failed to parse DNS reply!\n");
1440 rh->proc (rh->proc_cls, rh, 0, NULL);
1441 GNUNET_NETWORK_socket_close (rh->dns_sock);
1442 free_resolver_handle (rh);
1446 for (i = 0; i < packet->num_answers; i++)
1448 /* http://tools.ietf.org/html/rfc1034#section-3.6.2 */
1449 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_TYPE_CNAME)
1451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1452 "CNAME... restarting query with %s\n",
1453 packet->answers[i].data.hostname
1455 strcpy (rh->dns_name, packet->answers[i].data.hostname);
1456 found_cname = GNUNET_YES;
1457 //send_dns_packet (rh);
1458 //GNUNET_DNSPARSER_free_packet (packet);
1462 if ((packet->answers[i].type == rlh->record_type) &&
1463 (0 == strcmp (packet->answers[i].name, rh->dns_name)))
1465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1467 rd.data = packet->answers[i].data.raw.data;
1468 rd.data_size = packet->answers[i].data.raw.data_len;
1469 rd.record_type = packet->answers[i].type;
1471 rd.expiration = packet->answers[i].expiration_time;
1472 rh->proc (rh->proc_cls, rh, 1, &rd);
1473 GNUNET_NETWORK_socket_close (rh->dns_sock);
1474 GNUNET_DNSPARSER_free_packet (packet);
1475 free_resolver_handle (rh);
1480 if (GNUNET_YES == found_cname)
1482 zone_offset = strlen (rh->dns_name) - strlen (rh->dns_zone) - 1;
1484 if (0 > zone_offset)
1487 /* restart query with CNAME */
1488 if (0 == strcmp (rh->dns_name+zone_offset, rh->dns_zone))
1489 send_dns_packet (rh);
1491 resolve_dns_name (rh);
1493 GNUNET_DNSPARSER_free_packet (packet);
1497 for (i = 0; i < packet->num_authority_records; i++)
1500 if (packet->authority_records[i].type == GNUNET_GNS_RECORD_TYPE_NS)
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503 "Found NS delegation!\n");
1504 found_delegation = GNUNET_YES;
1505 delegation_name = packet->authority_records[i].data.hostname;
1510 for (i = 0; i < packet->num_additional_records; i++)
1512 if (found_delegation == GNUNET_NO)
1515 if ((packet->additional_records[i].type == GNUNET_GNS_RECORD_TYPE_A) &&
1516 (0 == strcmp (packet->additional_records[i].name, delegation_name)))
1518 GNUNET_assert (sizeof (struct in_addr) ==
1519 packet->authority_records[i].data.raw.data_len);
1521 rh->dns_addr.sin_addr =
1522 *((struct in_addr*)packet->authority_records[i].data.raw.data);
1523 send_dns_packet (rh);
1524 GNUNET_DNSPARSER_free_packet (packet);
1529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1530 "Failed to parse DNS reply!\n");
1531 rh->proc (rh->proc_cls, rh, 0, NULL);
1532 GNUNET_NETWORK_socket_close (rh->dns_sock);
1533 free_resolver_handle (rh);
1534 GNUNET_DNSPARSER_free_packet (packet);
1539 * Sends a UDP dns query to a nameserver specified in the rh
1541 * @param rh the request handle
1544 send_dns_packet (struct ResolverHandle *rh)
1546 struct GNUNET_NETWORK_FDSet *rset = GNUNET_NETWORK_fdset_create ();
1547 GNUNET_NETWORK_fdset_set (rset, rh->dns_sock);
1549 GNUNET_NETWORK_socket_sendto (rh->dns_sock,
1551 rh->dns_raw_packet_size,
1552 (struct sockaddr*)&rh->dns_addr,
1553 sizeof (struct sockaddr_in));
1555 rh->dns_read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1556 rh->timeout, //FIXME less?
1562 GNUNET_NETWORK_fdset_destroy (rset);
1567 * The final phase of resoution.
1568 * We found a NS RR and want to resolve via DNS
1570 * @param rh the pending lookup handle
1571 * @param rd_count length of record data
1572 * @param rd record data containing VPN RR
1575 resolve_record_dns (struct ResolverHandle *rh,
1577 const struct GNUNET_NAMESTORE_RecordData *rd)
1579 struct GNUNET_DNSPARSER_Query query;
1580 struct GNUNET_DNSPARSER_Packet packet;
1581 struct GNUNET_DNSPARSER_Flags flags;
1582 char dns_name[MAX_DNS_NAME_LENGTH];
1583 struct in_addr dnsip;
1584 struct sockaddr_in addr;
1585 struct sockaddr *sa;
1587 struct RecordLookupHandle *rlh = rh->proc_cls;
1589 /* We cancel here as to not include the ns lookup in the timeout */
1590 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1592 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1593 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1595 /* Start shortening */
1596 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1599 "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
1601 start_shorten (rh->authority_chain_tail,
1605 for (i = 0; i < rd_count; i++)
1607 /* Synthesize dns name */
1608 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS)
1610 strcpy (rh->dns_zone, (char*)rd[i].data);
1611 if (0 == strcmp (rh->name, ""))
1612 strcpy (rh->dns_name, (char*)rd[i].data);
1614 sprintf (rh->dns_name, "%s.%s", rh->name, (char*)rd[i].data);
1617 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_A)
1618 dnsip = *((struct in_addr*)rd[i].data);
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1622 "GNS_PHASE_REC_DNS-%llu: Looking up %s from %s\n",
1625 rh->dns_sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1626 if (rh->dns_sock == NULL)
1628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1629 "GNS_PHASE_REC_DNS-%llu: Error creating udp socket for dns!\n",
1631 rh->proc(rh->proc_cls, rh, 0, NULL);
1635 memset (&addr, 0, sizeof (struct sockaddr_in));
1636 sa = (struct sockaddr *) &addr;
1637 sa->sa_family = AF_INET;
1638 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (rh->dns_sock,
1640 sizeof (struct sockaddr_in)))
1642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1643 "GNS_PHASE_REC_DNS-%llu: Error binding udp socket for dns!\n",
1645 GNUNET_NETWORK_socket_close (rh->dns_sock);
1646 rh->proc(rh->proc_cls, rh, 0, NULL);
1649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1650 "GNS_PHASE_REC_DNS-%llu: NOT IMPLEMENTED!\n",
1652 GNUNET_NETWORK_socket_close (rh->dns_sock);
1653 rh->proc(rh->proc_cls, rh, 0, NULL);
1654 /*TODO create dnsparser query, serialize, sendto, handle reply*/
1655 query.name = dns_name;
1656 query.type = rlh->record_type;
1657 query.class = GNUNET_DNSPARSER_CLASS_INTERNET;
1658 memset (&flags, 0, sizeof (flags));
1659 flags.recursion_desired = 1;
1660 flags.checking_disabled = 1;
1661 packet.queries = &query;
1662 packet.answers = NULL;
1663 packet.authority_records = NULL;
1664 packet.num_queries = 1;
1665 packet.num_answers = 0;
1666 packet.num_authority_records = 0;
1667 packet.num_additional_records = 0;
1668 packet.flags = flags;
1670 if (GNUNET_OK != GNUNET_DNSPARSER_pack (&packet,
1672 &rh->dns_raw_packet,
1673 &rh->dns_raw_packet_size))
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "GNS_PHASE_REC_DNS-%llu: Creating raw dns packet!\n",
1678 GNUNET_NETWORK_socket_close (rh->dns_sock);
1679 rh->proc(rh->proc_cls, rh, 0, NULL);
1683 rh->dns_addr.sin_family = AF_INET;
1684 rh->dns_addr.sin_port = htons (53); //domain
1685 rh->dns_addr.sin_addr = dnsip;
1686 #if HAVE_SOCKADDR_IN_SIN_LEN
1687 rh->dns_addr.sin_len = (u_char) sizeof (struct sockaddr_in);
1690 send_dns_packet (rh);
1692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1693 "GNS_PHASE_REC_DNS-%llu: NOT IMPLEMENTED!\n",
1695 GNUNET_free (rh->dns_raw_packet);
1696 GNUNET_NETWORK_socket_close (rh->dns_sock);
1697 rh->proc(rh->proc_cls, rh, 0, NULL);
1702 * The final phase of resoution.
1703 * We found a VPN RR and want to request an IPv4/6 address
1705 * @param rh the pending lookup handle
1706 * @param rd_count length of record data
1707 * @param rd record data containing VPN RR
1710 resolve_record_vpn (struct ResolverHandle *rh,
1712 const struct GNUNET_NAMESTORE_RecordData *rd)
1716 struct GNUNET_HashCode peer_id;
1717 struct GNUNET_CRYPTO_HashAsciiEncoded s_pid;
1718 struct GNUNET_HashCode serv_desc;
1719 struct GNUNET_CRYPTO_HashAsciiEncoded s_sd;
1721 size_t len = (sizeof (uint32_t) * 2) + (sizeof (struct GNUNET_HashCode) * 2);
1723 /* We cancel here as to not include the ns lookup in the timeout */
1724 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1726 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1727 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1729 /* Start shortening */
1730 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1733 "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
1735 start_shorten (rh->authority_chain_tail,
1739 /* Extracting VPN information FIXME rd parsing with NS API?*/
1740 if (len != rd->data_size)
1742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1743 "GNS_PHASE_REC_VPN-%llu: Error parsing VPN RR!\n",
1745 finish_lookup (rh, rh->proc_cls, 0, NULL);
1746 free_resolver_handle (rh);
1751 memcpy (&af, pos, sizeof (uint32_t));
1752 pos += sizeof (uint32_t);
1753 memcpy (&proto, pos, sizeof (uint32_t));
1754 pos += sizeof (uint32_t);
1755 memcpy (&s_pid, pos, sizeof (struct GNUNET_HashCode));
1756 pos += sizeof (struct GNUNET_HashCode);
1757 memcpy (&s_sd, pos, sizeof (struct GNUNET_HashCode));
1760 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_pid, &peer_id)) ||
1761 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char*)&s_sd, &serv_desc)))
1763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1764 "GNS_PHASE_REC_VPN-%llu: Error parsing VPN RR hashes!\n",
1766 finish_lookup (rh, rh->proc_cls, 0, NULL);
1767 free_resolver_handle (rh);
1771 rh->proc = &handle_record_vpn;
1773 if (NULL == vpn_handle)
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 "GNS_PHASE_REC_VPN-%llu: VPN not connected!\n",
1778 finish_lookup (rh, rh->proc_cls, 0, NULL);
1779 free_resolver_handle (rh);
1784 rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
1786 (struct GNUNET_PeerIdentity*)&peer_id,
1789 GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
1790 &process_record_result_vpn,
1796 * The final phase of resolution.
1797 * rh->name is a name that is canonical and we do not have a delegation.
1798 * Query namestore for this record
1800 * @param rh the pending lookup handle
1803 resolve_record_ns(struct ResolverHandle *rh)
1805 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
1807 /* We cancel here as to not include the ns lookup in the timeout */
1808 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1810 GNUNET_SCHEDULER_cancel(rh->timeout_task);
1811 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1813 /* Start shortening */
1814 if ((rh->priv_key != NULL) && is_canonical (rh->name))
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1817 "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
1819 start_shorten (rh->authority_chain_tail,
1824 * Try to resolve this record in our namestore.
1825 * The name to resolve is now in rh->authority_name
1826 * since we tried to resolve it to an authority
1829 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1833 &process_record_result_ns,
1840 * Handle timeout for DHT requests
1842 * @param cls the request handle as closure
1843 * @param tc the task context
1846 dht_authority_lookup_timeout(void *cls,
1847 const struct GNUNET_SCHEDULER_TaskContext *tc)
1849 struct ResolverHandle *rh = cls;
1850 struct RecordLookupHandle *rlh = rh->proc_cls;
1851 char new_name[MAX_DNS_NAME_LENGTH];
1853 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1854 "GNS_PHASE_DELEGATE_DHT-%llu: dht lookup for query %s (%ds)timed out.\n",
1855 rh->id, rh->authority_name, rh->timeout.rel_value);
1857 rh->status |= RSL_TIMED_OUT;
1859 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1861 GNUNET_DHT_get_stop (rh->get_handle);
1862 rh->get_handle = NULL;
1864 if (strcmp(rh->name, "") == 0)
1867 * promote authority back to name and try to resolve record
1869 strcpy(rh->name, rh->authority_name);
1870 rh->proc(rh->proc_cls, rh, 0, NULL);
1875 * Start resolution in bg
1877 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH,
1878 "%s.%s.%s", rh->name, rh->authority_name, GNUNET_GNS_TLD);
1879 //strcpy(new_name, rh->name);
1880 //strcpy(new_name+strlen(new_name), ".");
1881 //memcpy(new_name+strlen(new_name), GNUNET_GNS_TLD, strlen(GNUNET_GNS_TLD));
1883 strcpy(rh->name, new_name);
1885 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1886 "GNS_PHASE_DELEGATE_DHT-%llu: Starting background query for %s type %d\n",
1887 rh->id, rh->name, rlh->record_type);
1889 gns_resolver_lookup_record(rh->authority,
1890 rh->private_local_zone,
1894 GNUNET_TIME_UNIT_FOREVER_REL,
1896 &background_lookup_result_processor,
1899 rh->proc(rh->proc_cls, rh, 0, NULL);
1903 static void resolve_delegation_dht(struct ResolverHandle *rh);
1906 static void resolve_delegation_ns(struct ResolverHandle *rh);
1910 * Namestore resolution for delegation finished. Processing result.
1912 * @param cls the closure
1913 * @param rh resolver handle
1914 * @param rd_count number of results (always 0)
1915 * @param rd record data (always NULL)
1918 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
1919 unsigned int rd_count,
1920 const struct GNUNET_NAMESTORE_RecordData *rd);
1924 * Function called when we get a result from the dht
1925 * for our query. Recursively tries to resolve authorities
1928 * @param cls the request handle
1929 * @param exp lifetime
1930 * @param key the key the record was stored under
1931 * @param get_path get path
1932 * @param get_path_length get path length
1933 * @param put_path put path
1934 * @param put_path_length put path length
1935 * @param type the block type
1936 * @param size the size of the record
1937 * @param data the record data
1940 process_delegation_result_dht(void* cls,
1941 struct GNUNET_TIME_Absolute exp,
1942 const struct GNUNET_HashCode * key,
1943 const struct GNUNET_PeerIdentity *get_path,
1944 unsigned int get_path_length,
1945 const struct GNUNET_PeerIdentity *put_path,
1946 unsigned int put_path_length,
1947 enum GNUNET_BLOCK_Type type,
1948 size_t size, const void *data)
1950 struct ResolverHandle *rh;
1951 struct GNSNameRecordBlock *nrb;
1952 uint32_t num_records;
1954 char* rd_data = (char*) data;
1957 struct GNUNET_CRYPTO_ShortHashCode zone, name_hash;
1958 struct GNUNET_HashCode zone_hash_double, name_hash_double;
1960 rh = (struct ResolverHandle *)cls;
1962 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1963 "GNS_PHASE_DELEGATE_DHT-%llu: Got DHT result\n", rh->id);
1968 nrb = (struct GNSNameRecordBlock*)data;
1970 /* stop dht lookup and timeout task */
1971 GNUNET_DHT_get_stop (rh->get_handle);
1973 rh->get_handle = NULL;
1975 if (rh->dht_heap_node != NULL)
1977 GNUNET_CONTAINER_heap_remove_node(rh->dht_heap_node);
1978 rh->dht_heap_node = NULL;
1981 num_records = ntohl(nrb->rd_count);
1982 name = (char*)&nrb[1];
1984 struct GNUNET_NAMESTORE_RecordData rd[num_records];
1986 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1987 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
1989 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
1994 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1995 "GNS_PHASE_DELEGATE_DHT-%llu: Error deserializing data!\n",
2000 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2001 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2002 rh->id, name, rh->authority_name);
2003 for (i=0; i<num_records; i++)
2006 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2007 "GNS_PHASE_DELEGATE_DHT-%llu: Got name: %s (wanted %s)\n",
2008 rh->id, name, rh->authority_name);
2009 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2010 "GNS_PHASE_DELEGATE_DHT-%llu: Got type: %d (wanted %d)\n",
2011 rh->id, rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
2012 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2013 "GNS_PHASE_DELEGATE_DHT-%llu: Got data length: %d\n",
2014 rh->id, rd[i].data_size);
2015 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2016 "GNS_PHASE_DELEGATE_DHT-%llu: Got flag %d\n",
2017 rh->id, rd[i].flags);
2019 if ((strcmp(name, rh->authority_name) == 0) &&
2020 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
2022 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2023 "GNS_PHASE_DELEGATE_DHT-%llu: Authority found in DHT\n",
2026 memcpy(&rh->authority, rd[i].data, sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2027 struct AuthorityChain *auth =
2028 GNUNET_malloc(sizeof(struct AuthorityChain));
2029 auth->zone = rh->authority;
2030 memset(auth->name, 0, strlen(rh->authority_name)+1);
2031 strcpy(auth->name, rh->authority_name);
2032 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2033 rh->authority_chain_tail,
2036 /** try to import pkey if private key available */
2037 //if (rh->priv_key && is_canonical (rh->name))
2038 // process_discovered_authority(name, auth->zone,
2039 // rh->authority_chain_tail->zone,
2046 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
2047 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2048 GNUNET_CRYPTO_hash_xor(key, &name_hash_double, &zone_hash_double);
2049 GNUNET_CRYPTO_short_hash_from_truncation (&zone_hash_double, &zone);
2051 /* Save to namestore */
2052 if (0 != GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_tail->zone,
2055 GNUNET_NAMESTORE_record_put (namestore_handle,
2062 &on_namestore_record_put_result, //cont
2072 * FIXME in this case. should we ask namestore again?
2074 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2075 "GNS_PHASE_DELEGATE_DHT-%llu: Answer from DHT for %s. Yet to resolve: %s\n",
2076 rh->id, rh->authority_name, rh->name);
2078 if (strcmp(rh->name, "") == 0)
2080 /* Start shortening */
2081 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2084 "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
2086 start_shorten (rh->authority_chain_tail,
2090 rh->proc(rh->proc_cls, rh, 0, NULL);
2094 rh->proc = &handle_delegation_ns;
2095 resolve_delegation_ns(rh);
2101 * No pkey but name exists
2104 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2105 "GNS_PHASE_DELEGATE_DHT-%llu: Adding %s back to %s\n",
2106 rh->id, rh->authority_name, rh->name);
2107 if (strcmp(rh->name, "") == 0)
2108 strcpy(rh->name, rh->authority_name);
2110 GNUNET_snprintf(rh->name, MAX_DNS_NAME_LENGTH, "%s.%s",
2111 rh->name, rh->authority_name); //FIXME ret
2113 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2114 "GNS_PHASE_DELEGATE_DHT-%llu: %s restored\n", rh->id, rh->name);
2115 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2116 "GNS_PHASE_DELEGATE_DHT-%llu: DHT authority lookup found no match!\n",
2118 rh->proc(rh->proc_cls, rh, 0, NULL);
2121 #define MAX_SOA_LENGTH sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint32_t)\
2122 +(MAX_DNS_NAME_LENGTH*2)
2123 #define MAX_MX_LENGTH sizeof(uint16_t)+MAX_DNS_NAME_LENGTH
2127 expand_plus(char** dest, char* src, char* repl)
2130 unsigned int s_len = strlen(src)+1;
2132 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2133 "GNS_POSTPROCESS: Got %s to expand with %s\n", src, repl);
2137 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2138 "GNS_POSTPROCESS: %s to short\n", src);
2140 /* no postprocessing */
2141 memcpy(*dest, src, s_len+1);
2145 if (0 == strcmp(src+s_len-3, ".+"))
2147 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2148 "GNS_POSTPROCESS: Expanding .+ in %s\n", src);
2149 memset(*dest, 0, s_len+strlen(repl)+strlen(GNUNET_GNS_TLD));
2151 pos = *dest+s_len-2;
2153 pos += strlen(repl);
2154 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2155 "GNS_POSTPROCESS: Expanded to %s\n", *dest);
2159 memcpy(*dest, src, s_len+1);
2167 finish_lookup(struct ResolverHandle *rh,
2168 struct RecordLookupHandle* rlh,
2169 unsigned int rd_count,
2170 const struct GNUNET_NAMESTORE_RecordData *rd)
2173 char new_rr_data[MAX_DNS_NAME_LENGTH];
2174 char new_mx_data[MAX_MX_LENGTH];
2175 char new_soa_data[MAX_SOA_LENGTH];
2176 struct GNUNET_NAMESTORE_RecordData p_rd[rd_count];
2179 unsigned int offset;
2181 if (rh->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2183 GNUNET_SCHEDULER_cancel(rh->timeout_task);
2184 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2188 memcpy(p_rd, rd, rd_count*sizeof(struct GNUNET_NAMESTORE_RecordData));
2190 for (i = 0; i < rd_count; i++)
2193 if (rd[i].record_type != GNUNET_GNS_RECORD_TYPE_NS &&
2194 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_CNAME &&
2195 rd[i].record_type != GNUNET_GNS_RECORD_MX &&
2196 rd[i].record_type != GNUNET_GNS_RECORD_TYPE_SOA)
2198 p_rd[i].data = rd[i].data;
2203 * for all those records we 'should'
2204 * also try to resolve the A/AAAA records (RFC1035)
2205 * This is a feature and not important
2208 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2209 "GNS_POSTPROCESS: Postprocessing\n");
2211 if (strcmp(rh->name, "+") == 0)
2212 repl_string = rlh->name;
2214 repl_string = rlh->name+strlen(rh->name)+1;
2217 if (rd[i].record_type == GNUNET_GNS_RECORD_MX)
2219 memcpy(new_mx_data, (char*)rd[i].data, sizeof(uint16_t));
2220 offset = sizeof(uint16_t);
2221 pos = new_mx_data+offset;
2222 expand_plus(&pos, (char*)rd[i].data+sizeof(uint16_t),
2224 offset += strlen(new_mx_data+sizeof(uint16_t))+1;
2225 p_rd[i].data = new_mx_data;
2226 p_rd[i].data_size = offset;
2228 else if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_SOA)
2230 /* expand mname and rname */
2232 expand_plus(&pos, (char*)rd[i].data, repl_string);
2233 offset = strlen(new_soa_data)+1;
2234 pos = new_soa_data+offset;
2235 expand_plus(&pos, (char*)rd[i].data+offset, repl_string);
2236 offset += strlen(new_soa_data+offset)+1;
2237 /* cpy the 4 numbers serial refresh retry and expire */
2238 memcpy(new_soa_data+offset, (char*)rd[i].data+offset, sizeof(uint32_t)*5);
2239 offset += sizeof(uint32_t)*5;
2240 p_rd[i].data_size = offset;
2241 p_rd[i].data = new_soa_data;
2246 expand_plus(&pos, (char*)rd[i].data, repl_string);
2247 p_rd[i].data_size = strlen(new_rr_data)+1;
2248 p_rd[i].data = new_rr_data;
2253 rlh->proc(rlh->proc_cls, rd_count, p_rd);
2259 * Process DHT lookup result for record.
2261 * @param cls the closure
2262 * @param rh resolver handle
2263 * @param rd_count number of results
2264 * @param rd record data
2267 handle_record_dht(void* cls, struct ResolverHandle *rh,
2268 unsigned int rd_count,
2269 const struct GNUNET_NAMESTORE_RecordData *rd)
2271 struct RecordLookupHandle* rlh;
2273 rlh = (struct RecordLookupHandle*)cls;
2276 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2277 "GNS_PHASE_REC-%d: No records for %s found in DHT. Aborting\n",
2279 /* give up, cannot resolve */
2280 finish_lookup(rh, rlh, 0, NULL);
2281 free_resolver_handle(rh);
2285 /* results found yay */
2286 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2287 "GNS_PHASE_REC-%d: Record resolved from DHT!", rh->id);
2289 finish_lookup(rh, rlh, rd_count, rd);
2290 free_resolver_handle(rh);
2298 * Process namestore lookup result for record.
2300 * @param cls the closure
2301 * @param rh resolver handle
2302 * @param rd_count number of results
2303 * @param rd record data
2306 handle_record_ns (void* cls, struct ResolverHandle *rh,
2307 unsigned int rd_count,
2308 const struct GNUNET_NAMESTORE_RecordData *rd)
2310 struct RecordLookupHandle* rlh;
2311 rlh = (struct RecordLookupHandle*) cls;
2314 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2315 "GNS_PHASE_REC-%d: NS returned no records. (status: %d)!\n",
2320 * There are 5 conditions that have to met for us to consult the DHT:
2321 * 1. The entry in the DHT is RSL_RECORD_EXPIRED AND
2322 * 2. No entry in the NS existed AND
2323 * 3. The zone queried is not the local resolver's zone AND
2324 * 4. The name that was looked up is '+'
2325 * because if it was any other canonical name we either already queried
2326 * the DHT for the authority in the authority lookup phase (and thus
2327 * would already have an entry in the NS for the record)
2328 * 5. We are not in cache only mode
2330 if (rh->status & (RSL_RECORD_EXPIRED | !RSL_RECORD_EXISTS) &&
2331 GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2332 &rh->private_local_zone) &&
2333 (strcmp(rh->name, "+") == 0) &&
2334 (rh->only_cached == GNUNET_NO))
2336 rh->proc = &handle_record_dht;
2337 resolve_record_dht(rh);
2340 /* give up, cannot resolve */
2341 finish_lookup(rh, rlh, 0, NULL);
2342 free_resolver_handle(rh);
2346 /* results found yay */
2347 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2348 "GNS_PHASE_REC-%d: Record resolved from namestore!", rh->id);
2350 finish_lookup(rh, rlh, rd_count, rd);
2352 free_resolver_handle(rh);
2358 * Move one level up in the domain hierarchy and return the
2359 * passed top level domain.
2361 * @param name the domain
2362 * @param dest the destination where the tld will be put
2365 pop_tld(char* name, char* dest)
2369 if (is_canonical(name))
2376 for (len = strlen(name); len > 0; len--)
2378 if (*(name+len) == '.')
2388 strcpy(dest, (name+len+1));
2392 * Checks if name is in tld
2394 * @param name the name to check
2395 * @param tld the TLD to check for
2396 * @return GNUNET_YES or GNUNET_NO
2399 is_tld(const char* name, const char* tld)
2403 if (strlen(name) <= strlen(tld))
2408 offset = strlen(name)-strlen(tld);
2409 if (strcmp(name+offset, tld) != 0)
2411 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2412 "%s is not in .%s TLD\n", name, tld);
2419 * DHT resolution for delegation finished. Processing result.
2421 * @param cls the closure
2422 * @param rh resolver handle
2423 * @param rd_count number of results (always 0)
2424 * @param rd record data (always NULL)
2427 handle_delegation_dht(void* cls, struct ResolverHandle *rh,
2428 unsigned int rd_count,
2429 const struct GNUNET_NAMESTORE_RecordData *rd)
2431 struct RecordLookupHandle* rlh;
2432 rlh = (struct RecordLookupHandle*) cls;
2435 if (strcmp(rh->name, "") == 0)
2437 if ((rlh->record_type == GNUNET_GNS_RECORD_PKEY))
2439 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2440 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved queried PKEY via DHT.\n",
2442 finish_lookup(rh, rlh, rd_count, rd);
2443 free_resolver_handle(rh);
2446 /* We resolved full name for delegation. resolving record */
2447 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2448 "GNS_PHASE_DELEGATE_DHT-%llu: Resolved full name for delegation via DHT.\n",
2450 strcpy(rh->name, "+\0");
2451 rh->proc = &handle_record_ns;
2452 resolve_record_ns(rh);
2457 * we still have some left
2459 if (is_canonical(rh->name))
2461 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2462 "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
2465 rh->proc = &handle_record_ns;
2466 resolve_record_ns(rh);
2469 /* give up, cannot resolve */
2470 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2471 "GNS_PHASE_DELEGATE_DHT-%llu: Cannot fully resolve delegation for %s via DHT!\n",
2473 finish_lookup(rh, rlh, 0, NULL);
2474 free_resolver_handle(rh);
2479 * Start DHT lookup for a name -> PKEY (compare NS) record in
2480 * rh->authority's zone
2482 * @param rh the pending gns query
2485 resolve_delegation_dht(struct ResolverHandle *rh)
2488 struct GNUNET_CRYPTO_ShortHashCode name_hash;
2489 struct GNUNET_HashCode name_hash_double;
2490 struct GNUNET_HashCode zone_hash_double;
2491 struct GNUNET_HashCode lookup_key;
2492 struct ResolverHandle *rh_heap_root;
2494 pop_tld(rh->name, rh->authority_name);
2495 GNUNET_CRYPTO_short_hash(rh->authority_name,
2496 strlen(rh->authority_name),
2498 GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
2499 GNUNET_CRYPTO_short_hash_double(&rh->authority, &zone_hash_double);
2500 GNUNET_CRYPTO_hash_xor(&name_hash_double, &zone_hash_double, &lookup_key);
2502 rh->dht_heap_node = NULL;
2504 if (rh->timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
2506 //rh->timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
2507 // &dht_authority_lookup_timeout,
2509 rh->timeout_cont = &dht_authority_lookup_timeout;
2510 rh->timeout_cont_cls = rh;
2514 if (max_allowed_background_queries <=
2515 GNUNET_CONTAINER_heap_get_size (dht_lookup_heap))
2517 /* terminate oldest lookup */
2518 rh_heap_root = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
2519 GNUNET_DHT_get_stop(rh_heap_root->get_handle);
2520 rh_heap_root->dht_heap_node = NULL;
2522 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2523 "GNS_PHASE_DELEGATE_DHT-%llu: Replacing oldest background query for %s\n",
2524 rh->id, rh_heap_root->authority_name);
2526 rh_heap_root->proc(rh_heap_root->proc_cls,
2531 rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
2533 GNUNET_TIME_absolute_get().abs_value);
2536 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
2538 GNUNET_assert(rh->get_handle == NULL);
2539 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
2540 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
2542 DHT_GNS_REPLICATION_LEVEL,
2546 &process_delegation_result_dht,
2553 * Namestore resolution for delegation finished. Processing result.
2555 * @param cls the closure
2556 * @param rh resolver handle
2557 * @param rd_count number of results (always 0)
2558 * @param rd record data (always NULL)
2561 handle_delegation_ns(void* cls, struct ResolverHandle *rh,
2562 unsigned int rd_count,
2563 const struct GNUNET_NAMESTORE_RecordData *rd)
2565 struct RecordLookupHandle* rlh;
2566 rlh = (struct RecordLookupHandle*) cls;
2568 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2569 "GNS_PHASE_DELEGATE_NS-%llu: Resolution status: %d.\n",
2570 rh->id, rh->status);
2572 if (strcmp(rh->name, "") == 0)
2574 if (rlh->record_type == GNUNET_GNS_RECORD_PKEY)
2576 GNUNET_assert(rd_count == 1);
2577 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2578 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
2580 finish_lookup(rh, rlh, rd_count, rd);
2581 free_resolver_handle(rh);
2584 /* We resolved full name for delegation. resolving record */
2585 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2586 "GNS_PHASE_DELEGATE_NS-%llu: Resolved full name for delegation.\n",
2589 if (rh->status & RSL_DELEGATE_VPN)
2591 if (rlh->record_type == GNUNET_GNS_RECORD_VPN)
2593 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2594 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried VPNRR in NS.\n",
2596 finish_lookup(rh, rlh, rd_count, rd);
2597 free_resolver_handle(rh);
2600 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2601 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2603 GNUNET_assert (NULL != rd);
2604 rh->proc = &handle_record_vpn;
2605 resolve_record_vpn (rh, rd_count, rd);
2607 else if (rh->status & RSL_DELEGATE_NS)
2609 if (rlh->record_type == GNUNET_GNS_RECORD_TYPE_NS)
2611 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2612 "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried NSRR in NS.\n",
2614 finish_lookup(rh, rlh, rd_count, rd);
2615 free_resolver_handle(rh);
2618 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2619 "GNS_PHASE_DELEGATE_NS-%llu: VPN delegation starting.\n",
2621 GNUNET_assert (NULL != rd);
2622 rh->proc = &handle_record_ns;
2623 resolve_record_dns (rh, rd_count, rd);
2627 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2628 "GNS_PHASE_DELEGATE_NS-%llu: Resolving record +\n",
2630 strcpy(rh->name, "+\0");
2631 rh->proc = &handle_record_ns;
2632 resolve_record_ns(rh);
2638 * we still have some left
2639 * check if authority in ns is fresh
2641 * or we are authority
2643 if (((rh->status & RSL_RECORD_EXISTS) && (!(rh->status & RSL_RECORD_EXPIRED)))
2644 || !GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
2645 &rh->private_local_zone))
2647 if (is_canonical(rh->name))
2649 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2650 "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
2653 rh->proc = &handle_record_ns;
2654 resolve_record_ns(rh);
2658 /* give up, cannot resolve */
2659 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2660 "GNS_PHASE_DELEGATE_NS-%llu: Cannot fully resolve delegation for %s!\n",
2663 finish_lookup(rh, rlh, rd_count, rd);
2664 //rlh->proc(rlh->proc_cls, 0, NULL);
2669 if (rh->only_cached == GNUNET_YES)
2671 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2672 "GNS_PHASE_DELEGATE_NS-%llu: Only cache resolution, no result\n",
2674 finish_lookup(rh, rlh, rd_count, rd);
2678 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2679 "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
2681 rh->proc = &handle_delegation_dht;
2682 resolve_delegation_dht(rh);
2688 * This is a callback function that should give us only PKEY
2689 * records. Used to query the namestore for the authority (PKEY)
2690 * for 'name'. It will recursively try to resolve the
2691 * authority for a given name from the namestore.
2693 * @param cls the pending query
2694 * @param key the key of the zone we did the lookup
2695 * @param expiration expiration date of the record data set in the namestore
2696 * @param name the name for which we need an authority
2697 * @param rd_count the number of records with 'name'
2698 * @param rd the record data
2699 * @param signature the signature of the authority for the record data
2702 process_delegation_result_ns(void* cls,
2703 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
2704 struct GNUNET_TIME_Absolute expiration,
2706 unsigned int rd_count,
2707 const struct GNUNET_NAMESTORE_RecordData *rd,
2708 const struct GNUNET_CRYPTO_RsaSignature *signature)
2710 struct ResolverHandle *rh;
2711 struct GNUNET_TIME_Relative remaining_time;
2712 struct GNUNET_CRYPTO_ShortHashCode zone;
2713 char new_name[MAX_DNS_NAME_LENGTH];
2715 rh = (struct ResolverHandle *)cls;
2716 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2717 "GNS_PHASE_DELEGATE_NS-%llu: Got %d records from authority lookup\n",
2720 GNUNET_CRYPTO_short_hash(key,
2721 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2723 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
2729 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2730 "GNS_PHASE_DELEGATE_NS-%llu: Records with name %s exist.\n",
2732 rh->status |= RSL_RECORD_EXISTS;
2735 if (remaining_time.rel_value == 0)
2737 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2738 "GNS_PHASE_DELEGATE_NS-%llu: Record set %s expired.\n",
2740 rh->status |= RSL_RECORD_EXPIRED;
2744 * No authority found in namestore.
2749 * We did not find an authority in the namestore
2754 * Promote this authority back to a name maybe it is
2757 if (strcmp(rh->name, "") == 0)
2759 /* simply promote back */
2760 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2761 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
2762 rh->id, rh->authority_name);
2763 strcpy(rh->name, rh->authority_name);
2767 /* add back to existing name */
2768 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2769 "GNS_PHASE_DELEGATE_NS-%llu: Adding %s back to %s\n",
2770 rh->id, rh->authority_name, rh->name);
2771 //memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
2772 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
2773 rh->name, rh->authority_name);
2774 //strcpy(new_name, rh->name);
2775 //strcpy(new_name+strlen(new_name), ".");
2776 //strcpy(new_name+strlen(new_name), rh->authority_name);
2777 strcpy(rh->name, new_name);
2778 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2779 "GNS_PHASE_DELEGATE_NS-%llu: %s restored\n", rh->id, rh->name);
2781 rh->proc(rh->proc_cls, rh, 0, NULL);
2786 * We found an authority that may be able to help us
2787 * move on with query
2788 * Note only 1 pkey should have been returned.. anything else would be strange
2791 for (i=0; i<rd_count;i++)
2796 if (rd[i].record_type == GNUNET_GNS_RECORD_VPN)
2798 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2799 "GNS_PHASE_DELEGATE_NS-%llu: VPNRR found.\n",
2801 rh->status |= RSL_DELEGATE_VPN;
2802 rh->proc (rh->proc_cls, rh, rd_count, rd);
2808 * FIXME make optional
2810 if (rd[i].record_type == GNUNET_GNS_RECORD_TYPE_NS)
2812 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2813 "GNS_PHASE_DELEGATE_NS-%llu: NS found.\n",
2815 rh->status |= RSL_DELEGATE_NS;
2816 rh->proc (rh->proc_cls, rh, rd_count, rd);
2819 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
2822 if (ignore_pending_records &&
2823 (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING))
2825 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2826 "GNS_PHASE_DELEGATE_NS-%llu: PKEY for %s is pending user confirmation.\n",
2832 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
2835 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2836 "GNS_PHASE_DELEGATE_NS-%llu: This pkey is expired.\n",
2838 if (remaining_time.rel_value == 0)
2840 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2841 "GNS_PHASE_DELEGATE_NS-%llu: This dht entry is expired.\n",
2843 rh->authority_chain_head->fresh = 0;
2844 rh->proc(rh->proc_cls, rh, 0, NULL);
2852 * Resolve rest of query with new authority
2854 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
2855 memcpy(&rh->authority, rd[i].data,
2856 sizeof(struct GNUNET_CRYPTO_ShortHashCode));
2857 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
2858 auth->zone = rh->authority;
2859 memset(auth->name, 0, strlen(rh->authority_name)+1);
2860 strcpy(auth->name, rh->authority_name);
2861 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
2862 rh->authority_chain_tail,
2865 /** try to import pkey if private key available
2866 * TODO: Only import last one?
2868 //if (rh->priv_key && (name != NULL) && is_canonical (rh->name))
2869 // process_discovered_authority((char*)name, auth->zone,
2870 // rh->authority_chain_tail->zone,
2873 * We are done with PKEY resolution if name is empty
2874 * else resolve again with new authority
2876 if (strcmp(rh->name, "") == 0)
2877 rh->proc(rh->proc_cls, rh, rd_count, rd);
2879 resolve_delegation_ns(rh);
2886 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2887 "GNS_PHASE_DELEGATE_NS-%llu: Authority lookup and no PKEY...\n", rh->id);
2889 * If we have found some records for the LAST label
2890 * we return the results. Else null.
2892 if (strcmp(rh->name, "") == 0)
2894 /* Start shortening */
2895 if ((rh->priv_key != NULL) && is_canonical (rh->name))
2897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2898 "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
2900 start_shorten (rh->authority_chain_tail,
2903 /* simply promote back */
2904 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2905 "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
2906 rh->id, rh->authority_name);
2907 strcpy(rh->name, rh->authority_name);
2908 rh->proc(rh->proc_cls, rh, rd_count, rd);
2912 rh->proc(rh->proc_cls, rh, 0, NULL);
2918 * Resolve the delegation chain for the request in our namestore
2920 * @param rh the resolver handle
2923 resolve_delegation_ns(struct ResolverHandle *rh)
2925 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2926 "GNS_PHASE_DELEGATE_NS-%llu: Resolving delegation for %s\n",
2928 pop_tld(rh->name, rh->authority_name);
2929 GNUNET_NAMESTORE_lookup_record(namestore_handle,
2932 GNUNET_GNS_RECORD_ANY,
2933 &process_delegation_result_ns,
2940 * Lookup of a record in a specific zone
2941 * calls lookup result processor on result
2943 * @param zone the root zone
2944 * @param pzone the private local zone
2945 * @param record_type the record type to look up
2946 * @param name the name to look up
2947 * @param key a private key for use with PSEU import (can be NULL)
2948 * @param timeout timeout for resolution
2949 * @param only_cached GNUNET_NO to only check locally not DHT for performance
2950 * @param proc the processor to call on result
2951 * @param cls the closure to pass to proc
2954 gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
2955 struct GNUNET_CRYPTO_ShortHashCode pzone,
2956 uint32_t record_type,
2958 struct GNUNET_CRYPTO_RsaPrivateKey *key,
2959 struct GNUNET_TIME_Relative timeout,
2961 RecordLookupProcessor proc,
2964 struct ResolverHandle *rh;
2965 struct RecordLookupHandle* rlh;
2966 char string_hash[MAX_DNS_LABEL_LENGTH];
2967 char nzkey[MAX_DNS_LABEL_LENGTH];
2968 char* nzkey_ptr = nzkey;
2970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2971 "Starting resolution for %s (type=%d)!\n",
2975 if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
2977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2978 "%s is canonical and not gnunet -> cannot resolve!\n", name);
2983 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
2984 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
2986 rh->authority = zone;
2990 rh->timeout = timeout;
2991 rh->get_handle = NULL;
2992 rh->private_local_zone = pzone;
2993 rh->only_cached = only_cached;
2997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2998 "No shorten key for resolution\n");
3001 if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
3004 * Set timeout for authority lookup phase to 1/2
3006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3007 "Timeout for lookup set to %ds\n", rh->timeout.rel_value);
3008 rh->timeout_task = GNUNET_SCHEDULER_add_delayed(
3009 GNUNET_TIME_relative_divide(timeout, 2),
3010 &handle_lookup_timeout,
3012 rh->timeout_cont = &dht_authority_lookup_timeout;
3013 rh->timeout_cont_cls = rh;
3017 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No timeout for query!\n");
3018 rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3021 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3024 * Only 'gnunet' given
3026 strcpy(rh->name, "\0");
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3031 "Checking for TLD...\n");
3032 if (is_zkey_tld(name) == GNUNET_YES)
3034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3037 * This is a zkey tld
3038 * build hash and use as initial authority
3041 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY));
3042 memcpy(rh->name, name,
3043 strlen(name)-strlen(GNUNET_GNS_TLD_ZKEY) - 1);
3044 pop_tld(rh->name, string_hash);
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3047 "ZKEY is %s!\n", string_hash);
3049 GNUNET_STRINGS_utf8_toupper(string_hash, &nzkey_ptr);
3051 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string(nzkey,
3054 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3055 "Cannot convert ZKEY %s to hash!\n", string_hash);
3065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3068 * Presumably GNUNET tld
3071 strlen(name)-strlen(GNUNET_GNS_TLD));
3072 memcpy(rh->name, name,
3073 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3078 * Initialize authority chain
3080 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3081 rh->authority_chain_head->prev = NULL;
3082 rh->authority_chain_head->next = NULL;
3083 rh->authority_chain_tail = rh->authority_chain_head;
3084 rh->authority_chain_head->zone = rh->authority;
3087 * Copy original query into lookup handle
3089 rlh->record_type = record_type;
3090 memset(rlh->name, 0, strlen(name) + 1);
3091 strcpy(rlh->name, name);
3093 rlh->proc_cls = cls;
3095 rh->proc = &handle_delegation_ns;
3096 resolve_delegation_ns(rh);
3099 /******** END Record Resolver ***********/
3102 * Callback calles by namestore for a zone to name
3105 * @param cls the closure
3106 * @param zone_key the zone we queried
3107 * @param expire the expiration time of the name
3108 * @param name the name found or NULL
3109 * @param rd_len number of records for the name
3110 * @param rd the record data (PKEY) for the name
3111 * @param signature the signature for the record data
3114 process_zone_to_name_shorten_root (void *cls,
3115 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3116 struct GNUNET_TIME_Absolute expire,
3118 unsigned int rd_len,
3119 const struct GNUNET_NAMESTORE_RecordData *rd,
3120 const struct GNUNET_CRYPTO_RsaSignature *signature);
3124 * Callback called by namestore for a zone to name
3127 * @param cls the closure
3128 * @param zone_key the zone we queried
3129 * @param expire the expiration time of the name
3130 * @param name the name found or NULL
3131 * @param rd_len number of records for the name
3132 * @param rd the record data (PKEY) for the name
3133 * @param signature the signature for the record data
3136 process_zone_to_name_shorten_shorten (void *cls,
3137 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3138 struct GNUNET_TIME_Absolute expire,
3140 unsigned int rd_len,
3141 const struct GNUNET_NAMESTORE_RecordData *rd,
3142 const struct GNUNET_CRYPTO_RsaSignature *signature)
3144 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3145 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3146 struct AuthorityChain *next_authority;
3148 char result[MAX_DNS_NAME_LENGTH];
3149 char tmp_name[MAX_DNS_NAME_LENGTH];
3152 /* we found a match in our own root zone */
3155 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3156 "result strlen %d\n", strlen(name));
3157 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3158 memset(result, 0, answer_len);
3160 if (strlen(rh->name) > 0)
3162 sprintf (result, "%s.%s.%s.%s.%s",
3164 nsh->shorten_zone_name, nsh->private_zone_name,
3169 sprintf (result, "%s.%s.%s.%s", name,
3170 nsh->shorten_zone_name, nsh->private_zone_name,
3174 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3175 "Found shorten result %s\n", result);
3176 if (strlen (nsh->result) > strlen (result))
3177 strcpy (nsh->result, result);
3179 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3180 nsh->shorten_zone) == 0)
3183 * This is our zone append .gnunet unless name is empty
3184 * (it shouldn't be, usually FIXME what happens if we
3185 * shorten to our zone to a "" record??)
3188 sprintf (result, "%s.%s.%s.%s",
3190 nsh->shorten_zone_name, nsh->private_zone_name,
3192 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3193 "Our zone: Found %s as shorten result\n", result);
3195 if (strlen (nsh->result) > strlen (result))
3196 strcpy (nsh->result, result);
3197 //nsh->proc(nsh->proc_cls, result);
3199 //free_resolver_handle(rh);
3206 * continue with next authority if exists
3208 if ((rh->authority_chain_head->next == NULL))
3210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3211 "Sending %s as shorten result\n", nsh->result);
3212 nsh->proc(nsh->proc_cls, nsh->result);
3214 free_resolver_handle (rh);
3217 next_authority = rh->authority_chain_head;
3219 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3220 "%s.%s", rh->name, next_authority->name);
3222 strcpy(rh->name, tmp_name);
3223 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3224 "No PSEU found for authority %s. Promoting back: %s\n",
3225 next_authority->name, rh->name);
3227 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3228 rh->authority_chain_tail,
3231 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3232 &rh->authority_chain_tail->zone,
3233 &rh->authority_chain_head->zone,
3234 &process_zone_to_name_shorten_root,
3239 * Callback calles by namestore for a zone to name
3242 * @param cls the closure
3243 * @param zone_key the zone we queried
3244 * @param expire the expiration time of the name
3245 * @param name the name found or NULL
3246 * @param rd_len number of records for the name
3247 * @param rd the record data (PKEY) for the name
3248 * @param signature the signature for the record data
3251 process_zone_to_name_shorten_private (void *cls,
3252 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3253 struct GNUNET_TIME_Absolute expire,
3255 unsigned int rd_len,
3256 const struct GNUNET_NAMESTORE_RecordData *rd,
3257 const struct GNUNET_CRYPTO_RsaSignature *signature)
3259 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3260 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3261 struct AuthorityChain *next_authority;
3263 char result[MAX_DNS_NAME_LENGTH];
3264 char tmp_name[MAX_DNS_NAME_LENGTH];
3267 /* we found a match in our own root zone */
3270 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3271 "result strlen %d\n", strlen(name));
3272 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3273 memset(result, 0, answer_len);
3275 if (strlen(rh->name) > 0)
3277 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3281 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3284 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3285 "Found shorten result %s\n", result);
3286 if (strlen (nsh->result) > strlen (result))
3287 strcpy (nsh->result, result);
3289 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3290 nsh->private_zone) == 0)
3293 * This is our zone append .gnunet unless name is empty
3294 * (it shouldn't be, usually FIXME what happens if we
3295 * shorten to our zone to a "" record??)
3298 sprintf (result, "%s.%s.%s",
3299 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3301 "Our private zone: Found %s as shorten result %s\n", result);
3302 if (strlen (nsh->result) > strlen (result))
3303 strcpy (nsh->result, result);
3306 if (nsh->shorten_zone != NULL)
3308 /* backtrack authorities for names in priv zone */
3309 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3311 &rh->authority_chain_head->zone,
3312 &process_zone_to_name_shorten_shorten,
3319 * continue with next authority if exists
3321 if ((rh->authority_chain_head->next == NULL))
3323 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3324 "Sending %s as shorten result\n", nsh->result);
3325 nsh->proc(nsh->proc_cls, nsh->result);
3327 free_resolver_handle(rh);
3330 next_authority = rh->authority_chain_head;
3332 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3333 "%s.%s", rh->name, next_authority->name);
3335 strcpy(rh->name, tmp_name);
3336 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3337 "No PSEU found for authority %s. Promoting back: %s\n",
3338 next_authority->name, rh->name);
3340 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3341 rh->authority_chain_tail,
3344 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3345 &rh->authority_chain_tail->zone,
3346 &rh->authority_chain_head->zone,
3347 &process_zone_to_name_shorten_root,
3353 * Callback calles by namestore for a zone to name
3356 * @param cls the closure
3357 * @param zone_key the zone we queried
3358 * @param expire the expiration time of the name
3359 * @param name the name found or NULL
3360 * @param rd_len number of records for the name
3361 * @param rd the record data (PKEY) for the name
3362 * @param signature the signature for the record data
3365 process_zone_to_name_shorten_root (void *cls,
3366 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3367 struct GNUNET_TIME_Absolute expire,
3369 unsigned int rd_len,
3370 const struct GNUNET_NAMESTORE_RecordData *rd,
3371 const struct GNUNET_CRYPTO_RsaSignature *signature)
3373 struct ResolverHandle *rh = (struct ResolverHandle *)cls;
3374 struct NameShortenHandle* nsh = (struct NameShortenHandle*)rh->proc_cls;
3375 struct AuthorityChain *next_authority;
3377 char result[MAX_DNS_NAME_LENGTH];
3378 char tmp_name[MAX_DNS_NAME_LENGTH];
3381 /* we found a match in our own root zone */
3384 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3385 "result strlen %d\n", strlen(name));
3386 answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3;
3387 memset(result, 0, answer_len);
3389 if (strlen(rh->name) > 0)
3391 sprintf (result, "%s.%s.%s", rh->name, name, GNUNET_GNS_TLD);
3395 sprintf (result, "%s.%s", name, GNUNET_GNS_TLD);
3398 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3399 "Found shorten result %s\n", result);
3400 if (strlen (nsh->result) > strlen (result))
3401 strcpy (nsh->result, result);
3403 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3404 nsh->root_zone) == 0)
3407 * This is our zone append .gnunet unless name is empty
3408 * (it shouldn't be, usually FIXME what happens if we
3409 * shorten to our zone to a "" record??)
3412 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3413 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3414 "Our zone: Found %s as shorten result\n", result);
3415 if (strlen (nsh->result) > strlen (result))
3416 strcpy (nsh->result, result);
3419 if (nsh->private_zone != NULL)
3421 /* backtrack authorities for names in priv zone */
3422 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3424 &rh->authority_chain_head->zone,
3425 &process_zone_to_name_shorten_private,
3432 * continue with next authority if exists
3434 if ((rh->authority_chain_head->next == NULL))
3436 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3437 "Sending %s as shorten result\n", nsh->result);
3438 nsh->proc(nsh->proc_cls, nsh->result);
3440 free_resolver_handle(rh);
3443 next_authority = rh->authority_chain_head;
3445 GNUNET_snprintf(tmp_name, MAX_DNS_NAME_LENGTH,
3446 "%s.%s", rh->name, next_authority->name);
3448 strcpy(rh->name, tmp_name);
3449 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3450 "No PSEU found for authority %s. Promoting back: %s\n",
3451 next_authority->name, rh->name);
3453 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
3454 rh->authority_chain_tail,
3457 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3458 &rh->authority_chain_tail->zone,
3459 &rh->authority_chain_head->zone,
3460 &process_zone_to_name_shorten_root,
3467 * Process result from namestore delegation lookup
3468 * for shorten operation
3470 * @param cls the client shorten handle
3471 * @param rh the resolver handle
3472 * @param rd_count number of results (0)
3473 * @param rd data (NULL)
3476 handle_delegation_ns_shorten(void* cls,
3477 struct ResolverHandle *rh,
3479 const struct GNUNET_NAMESTORE_RecordData *rd)
3481 struct NameShortenHandle *nsh;
3482 char result[MAX_DNS_NAME_LENGTH];
3484 nsh = (struct NameShortenHandle *)cls;
3487 * At this point rh->name contains the part of the name
3488 * that we do not have a PKEY in our namestore to resolve.
3489 * The authority chain in the resolver handle is now
3490 * useful to backtrack if needed
3493 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3494 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3495 memset(result, 0, sizeof (result));
3497 if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3498 nsh->root_zone) == 0)
3501 * This is our zone append .gnunet unless name is empty
3502 * (it shouldn't be, usually FIXME what happens if we
3503 * shorten to our zone to a "" record??)
3506 sprintf (result, "%s.%s", rh->name, GNUNET_GNS_TLD);
3507 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3508 "Our zone: Found %s as shorten result\n", result);
3510 if (strlen (nsh->result) > strlen (result))
3511 strcpy (nsh->result, result);
3514 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3515 nsh->private_zone) == 0)
3518 * This is our zone append .gnunet unless name is empty
3519 * (it shouldn't be, usually FIXME what happens if we
3520 * shorten to our zone to a "" record??)
3523 sprintf (result, "%s.%s.%s",
3524 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3525 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3526 "Our zone: Found %s as shorten result %s\n", result);
3528 if (strlen (nsh->result) > strlen (result))
3529 strcpy (nsh->result, result);
3531 else if (GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
3532 nsh->shorten_zone) == 0)
3535 * This is our zone append .gnunet unless name is empty
3536 * (it shouldn't be, usually FIXME what happens if we
3537 * shorten to our zone to a "" record??)
3540 sprintf (result, "%s.%s.%s",
3541 rh->name, nsh->private_zone_name, GNUNET_GNS_TLD);
3542 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3543 "Our zone: Found %s as shorten result\n", result);
3545 if (strlen (nsh->result) > strlen (result))
3546 strcpy (nsh->result, result);
3550 /* backtrack authorities for names */
3551 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3553 &rh->authority_chain_head->zone,
3554 &process_zone_to_name_shorten_root,
3561 * Callback calles by namestore for a zone to name
3564 * @param cls the closure
3565 * @param zone_key the zone we queried
3566 * @param expire the expiration time of the name
3567 * @param name the name found or NULL
3568 * @param rd_len number of records for the name
3569 * @param rd the record data (PKEY) for the name
3570 * @param signature the signature for the record data
3573 process_zone_to_name_zkey(void *cls,
3574 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
3575 struct GNUNET_TIME_Absolute expire,
3577 unsigned int rd_len,
3578 const struct GNUNET_NAMESTORE_RecordData *rd,
3579 const struct GNUNET_CRYPTO_RsaSignature *signature)
3581 struct ResolverHandle *rh = cls;
3582 struct NameShortenHandle *nsh = rh->proc_cls;
3583 struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
3584 char new_name[MAX_DNS_NAME_LENGTH];
3586 /* zkey not in our zone */
3590 * In this case we have not given this PKEY a name (yet)
3591 * It is either just not in our zone or not even cached
3592 * Since we do not know at this point we will not try to shorten
3593 * because PKEY import will happen if the user follows the zkey
3596 GNUNET_CRYPTO_short_hash_to_enc ((struct GNUNET_CRYPTO_ShortHashCode*)rd,
3598 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3599 "No name found for zkey %s returning verbatim!\n", enc);
3600 if (strcmp(rh->name, "") != 0)
3601 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s.%s",
3602 rh->name, enc, GNUNET_GNS_TLD_ZKEY);
3604 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3605 enc, GNUNET_GNS_TLD_ZKEY);
3607 strcpy (nsh->result, new_name);
3609 nsh->proc(nsh->proc_cls, new_name);
3611 free_resolver_handle(rh);
3615 if (strcmp(rh->name, "") != 0)
3616 GNUNET_snprintf(new_name, MAX_DNS_NAME_LENGTH, "%s.%s",
3619 strcpy(new_name, name);
3621 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3622 "Continue shorten for %s!\n", new_name);
3624 strcpy(rh->name, new_name);
3626 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3627 rh->authority_chain_tail = rh->authority_chain_head;
3628 rh->authority_chain_head->zone = rh->authority;
3631 /* Start delegation resolution in our namestore */
3632 resolve_delegation_ns(rh);
3637 * Shorten api from resolver
3639 * @param zone the root zone to use
3640 * @param pzone the private zone to use
3641 * @param szone the shorten zone to use
3642 * @param name the name to shorten
3643 * @param private_zone_name name of the private zone
3644 * @param shorten_zone_name name of the shorten zone
3645 * @param proc the processor to call with result
3646 * @param proc_cls closure to pass to proc
3649 gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
3650 struct GNUNET_CRYPTO_ShortHashCode *pzone,
3651 struct GNUNET_CRYPTO_ShortHashCode *szone,
3653 const char* private_zone_name,
3654 const char* shorten_zone_name,
3655 ShortenResultProcessor proc,
3658 struct ResolverHandle *rh;
3659 struct NameShortenHandle *nsh;
3660 char string_hash[MAX_DNS_LABEL_LENGTH];
3661 struct GNUNET_CRYPTO_ShortHashCode zkey;
3662 char nzkey[MAX_DNS_LABEL_LENGTH];
3663 char* nzkey_ptr = nzkey;
3666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3667 "Starting shorten for %s!\n", name);
3669 if (is_canonical ((char*)name))
3671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3672 "%s is canonical. Returning verbatim\n", name);
3673 proc (proc_cls, name);
3677 nsh = GNUNET_malloc (sizeof (struct NameShortenHandle));
3680 nsh->proc_cls = proc_cls;
3681 nsh->root_zone = zone;
3682 nsh->private_zone = pzone;
3683 nsh->shorten_zone = szone;
3684 strcpy (nsh->private_zone_name, private_zone_name);
3685 strcpy (nsh->shorten_zone_name, shorten_zone_name);
3686 strcpy (nsh->result, name);
3688 rh = GNUNET_malloc (sizeof (struct ResolverHandle));
3689 rh->authority = *zone;
3691 rh->priv_key = NULL;
3692 rh->proc = &handle_delegation_ns_shorten;
3695 rh->private_local_zone = *zone;
3697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3698 "Checking for TLD...\n");
3699 if (is_zkey_tld (name) == GNUNET_YES)
3701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3704 * This is a zkey tld
3705 * build hash and use as initial authority
3708 memset (rh->name, 0,
3709 strlen (name)-strlen (GNUNET_GNS_TLD_ZKEY));
3710 memcpy (rh->name, name,
3711 strlen(name)-strlen (GNUNET_GNS_TLD_ZKEY) - 1);
3712 pop_tld (rh->name, string_hash);
3714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3715 "ZKEY is %s!\n", string_hash);
3717 GNUNET_STRINGS_utf8_toupper (string_hash, &nzkey_ptr);
3719 if (GNUNET_OK != GNUNET_CRYPTO_short_hash_from_string (nzkey,
3722 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3723 "Cannot convert ZKEY %s to hash!\n", nzkey);
3726 proc (proc_cls, name);
3730 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
3733 &process_zone_to_name_zkey,
3740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3743 * Presumably GNUNET tld
3745 memset (rh->name, 0,
3746 strlen (name)-strlen (GNUNET_GNS_TLD));
3747 memcpy (rh->name, name,
3748 strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
3751 rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
3752 rh->authority_chain_tail = rh->authority_chain_head;
3753 rh->authority_chain_head->zone = *zone;
3756 /* Start delegation resolution in our namestore */
3757 resolve_delegation_ns (rh);
3760 /*********** END NAME SHORTEN ********************/
3764 * Process result from namestore delegation lookup
3765 * for get authority operation
3767 * @param cls the client get auth handle
3768 * @param rh the resolver handle
3769 * @param rd_count number of results (0)
3770 * @param rd data (NULL)
3773 handle_delegation_result_ns_get_auth(void* cls,
3774 struct ResolverHandle *rh,
3776 const struct GNUNET_NAMESTORE_RecordData *rd)
3778 struct GetNameAuthorityHandle* nah;
3779 char result[MAX_DNS_NAME_LENGTH];
3782 nah = (struct GetNameAuthorityHandle*) rh->proc_cls;
3785 * At this point rh->name contains the part of the name
3786 * that we do not have a PKEY in our namestore to resolve.
3787 * The authority chain in the resolver handle is now
3788 * useful to backtrack if needed
3791 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3792 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
3794 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3795 "Building response!\n");
3796 if (is_canonical(rh->name))
3799 * We successfully resolved the authority in the ns
3800 * FIXME for our purposes this is fine
3801 * but maybe we want to have an api that also looks
3802 * into the dht (i.e. option in message)
3804 if (strlen(rh->name) > strlen(nah->name))
3806 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3807 "Record name longer than original lookup name... odd!\n");
3811 answer_len = strlen(nah->name) - strlen(rh->name)
3812 + strlen(GNUNET_GNS_TLD) + 1;
3813 memset(result, 0, answer_len);
3814 strcpy(result, nah->name + strlen(rh->name) + 1);
3816 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3817 "Got authority result %s\n", result);
3819 nah->proc(nah->proc_cls, result);
3821 free_resolver_handle(rh);
3825 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3826 "Unable to resolve authority for remaining %s!\n", rh->name);
3827 nah->proc(nah->proc_cls, "");
3829 free_resolver_handle(rh);
3837 * Tries to resolve the authority for name
3840 * @param zone the root zone to look up for
3841 * @param pzone the private local zone
3842 * @param name the name to lookup up
3843 * @param proc the processor to call when finished
3844 * @param proc_cls the closure to pass to the processor
3847 gns_resolver_get_authority(struct GNUNET_CRYPTO_ShortHashCode zone,
3848 struct GNUNET_CRYPTO_ShortHashCode pzone,
3850 GetAuthorityResultProcessor proc,
3853 struct ResolverHandle *rh;
3854 struct GetNameAuthorityHandle *nah;
3856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3857 "Starting authority resolution for %s!\n", name);
3859 nah = GNUNET_malloc(sizeof (struct GetNameAuthorityHandle));
3860 rh = GNUNET_malloc(sizeof (struct ResolverHandle));
3861 rh->authority = zone;
3863 rh->private_local_zone = pzone;
3865 if (strcmp(GNUNET_GNS_TLD, name) == 0)
3867 strcpy(rh->name, "\0");
3872 strlen(name)-strlen(GNUNET_GNS_TLD));
3873 memcpy(rh->name, name,
3874 strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
3877 memset(nah->name, 0,
3879 strcpy(nah->name, name);
3881 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
3882 rh->authority_chain_tail = rh->authority_chain_head;
3883 rh->authority_chain_head->zone = zone;
3884 rh->proc = &handle_delegation_result_ns_get_auth;
3885 rh->proc_cls = (void*)nah;
3888 nah->proc_cls = proc_cls;
3890 /* Start delegation resolution in our namestore */
3891 resolve_delegation_ns(rh);
3895 /******** END GET AUTHORITY *************/
3897 /* end of gnunet-service-gns_resolver.c */