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 * - Write xquery and block plugin
25 * - The smaller FIXME issues all around
27 * @file gns/gnunet-service-gns.c
28 * @brief GNUnet GNS service
29 * @author Martin Schanzenbach
32 #include "gnunet_util_lib.h"
33 #include "gnunet_transport_service.h"
34 #include "gnunet_dns_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_namestore_service.h"
38 #include "gnunet_gns_service.h"
39 #include "block_gns.h"
42 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
43 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
44 #define DHT_GNS_REPLICATION_LEVEL 5
45 #define MAX_DNS_LABEL_LENGTH 63
47 /* Ignore for now not used anyway and probably never will */
48 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23
49 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24
50 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN 25
51 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT 26
56 struct AuthorityChain *prev;
58 struct AuthorityChain *next;
62 /* (local) name of the authority */
65 /* was the ns entry fresh */
69 struct GNUNET_GNS_ResolverHandle;
71 typedef void (*ResolutionResultProcessor) (void *cls,
72 struct GNUNET_GNS_ResolverHandle *rh,
74 const struct GNUNET_NAMESTORE_RecordData *rd);
83 * Handle to a currenty pending resolution
85 struct GNUNET_GNS_ResolverHandle
87 /* The name to resolve */
92 /* has this query been answered? how many matches */
95 /* the authoritative zone to query */
96 GNUNET_HashCode authority;
98 /* the name of the authoritative zone to query */
102 * we have an authority in namestore that
103 * may be able to resolve
107 /* a handle for dht lookups. should be NULL if no lookups are in progress */
108 struct GNUNET_DHT_GetHandle *get_handle;
110 /* timeout task for dht lookups */
111 GNUNET_SCHEDULER_TaskIdentifier dht_timeout_task;
113 /* called when resolution phase finishes */
114 ResolutionResultProcessor proc;
116 /* closure passed to proc */
119 struct AuthorityChain *authority_chain_head;
120 struct AuthorityChain *authority_chain_tail;
122 enum ResolutionStatus status;
127 * Handle to a record lookup
129 struct RecordLookupHandle
131 /* the record type to look up */
132 enum GNUNET_GNS_RecordType record_type;
134 /* the name to look up */
137 /* Method to call on record resolution result */
138 ResolutionResultProcessor proc;
140 /* closure to pass to proc */
146 * Handle to a shorten operation from api
148 struct ClientShortenHandle
150 /* the requesting client that */
151 struct GNUNET_SERVER_Client *client;
159 /* name to shorten */
165 * Handle to a lookup operation from api
167 struct ClientLookupHandle
169 /* the requesting client that */
170 struct GNUNET_SERVER_Client *client;
178 /* the name to look up */
179 char* name; //Needed?
183 * Handle to a DNS intercepted
186 struct InterceptLookupHandle
188 /* the request handle to reply to */
189 struct GNUNET_DNS_RequestHandle *request_handle;
191 /* the dns parser packet received */
192 struct GNUNET_DNSPARSER_Packet *packet;
194 /* the query parsed from the packet */
195 struct GNUNET_DNSPARSER_Query *query;
199 * Our handle to the DNS handler library
201 struct GNUNET_DNS_Handle *dns_handle;
204 * Our handle to the DHT
206 struct GNUNET_DHT_Handle *dht_handle;
209 * Our zone's private key
211 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
214 * Our handle to the namestore service
215 * FIXME maybe need a second handle for iteration
217 struct GNUNET_NAMESTORE_Handle *namestore_handle;
220 * Handle to iterate over our authoritative zone in namestore
222 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
225 * The configuration the GNS service is running with
227 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
230 * Our notification context.
232 static struct GNUNET_SERVER_NotificationContext *nc;
237 GNUNET_HashCode zone_hash;
240 * Our tld. Maybe get from config file
242 const char* gnunet_tld = ".gnunet";
245 * Useful for zone update for DHT put
247 static int num_public_records = 3600;
248 struct GNUNET_TIME_Relative dht_update_interval;
249 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
252 * Helper function to free resolver handle
255 free_resolver_handle(struct GNUNET_GNS_ResolverHandle* rh)
257 struct AuthorityChain *ac;
262 GNUNET_free_non_null (rh->name);
263 GNUNET_free_non_null (rh->authority_name);
265 ac = rh->authority_chain_head;
267 for (; NULL != ac; ac = ac->next)
269 GNUNET_free_non_null (ac->name);
277 * Reply to client with the result from our lookup.
279 * @param rh the request handle of the lookup
280 * @param rd_count the number of records to return
281 * @param rd the record data
284 reply_to_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
285 const struct GNUNET_NAMESTORE_RecordData *rd)
291 struct InterceptLookupHandle* ilh = (struct InterceptLookupHandle*)cls;
292 struct GNUNET_DNSPARSER_Packet *packet = ilh->packet;
293 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
294 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
295 packet->answers = answer_records;
296 packet->additional_records = additional_records;
299 * Put records in the DNS packet and modify it
302 len = sizeof(struct GNUNET_DNSPARSER_Record*);
303 for (i=0; i < rd_count; i++)
306 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
307 "Adding type %d to DNS response\n", rd[i].record_type);
308 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", rh->name);
309 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "QName: %s\n", ilh->query->name);
310 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
311 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
313 if (rd[i].record_type == ilh->query->type)
315 answer_records[i].name = ilh->query->name;
316 answer_records[i].type = rd[i].record_type;
317 answer_records[i].data.raw.data_len = rd[i].data_size;
318 answer_records[i].data.raw.data = (char*)rd[i].data;
319 answer_records[i].expiration_time = rd[i].expiration;
320 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
324 additional_records[i].name = ilh->query->name;
325 additional_records[i].type = rd[i].record_type;
326 additional_records[i].data.raw.data_len = rd[i].data_size;
327 additional_records[i].data.raw.data = (char*)rd[i].data;
328 additional_records[i].expiration_time = rd[i].expiration;
329 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
333 packet->num_answers = rh->answered;
334 packet->num_additional_records = rd_count-(rh->answered);
336 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
337 packet->flags.authoritative_answer = 1;
339 packet->flags.authoritative_answer = 0;
342 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
344 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
346 packet->flags.query_or_response = 1;
352 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
353 "Building DNS response\n");
354 ret = GNUNET_DNSPARSER_pack (packet,
355 1024, /* FIXME magic from dns redirector */
358 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
359 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
360 if (ret == GNUNET_OK)
362 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
363 "Answering DNS request\n");
364 GNUNET_DNS_request_answer(ilh->request_handle,
369 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
373 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
374 "Error building DNS response! (ret=%d)", ret);
377 packet->num_answers = 0;
378 packet->answers = NULL;
379 packet->num_additional_records = 0;
380 packet->additional_records = NULL;
381 GNUNET_DNSPARSER_free_packet(packet);
383 GNUNET_free((struct RecordLookupHandle*)rh->proc_cls);
384 free_resolver_handle(rh);
390 * Task run during shutdown.
396 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
398 /* Kill zone task for it may make the scheduler hang */
399 if (zone_update_taskid)
400 GNUNET_SCHEDULER_cancel(zone_update_taskid);
402 GNUNET_DNS_disconnect(dns_handle);
403 GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
404 GNUNET_DHT_disconnect(dht_handle);
408 * Callback when record data is put into namestore
410 * @param cls the closure
411 * @param success GNUNET_OK on success
412 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
415 on_namestore_record_put_result(void *cls,
419 if (GNUNET_NO == success)
421 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
424 else if (GNUNET_YES == success)
426 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
427 "records successfully put in namestore\n");
431 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
432 "Error putting records into namestore: %s\n", emsg);
436 * Handle timeout for DHT requests
438 * @param cls the request handle as closure
439 * @param tc the task context
442 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
444 struct GNUNET_GNS_ResolverHandle *rh = cls;
446 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
447 "dht lookup for query %s timed out.\n",
450 GNUNET_DHT_get_stop (rh->get_handle);
451 rh->proc(rh->proc_cls, rh, 0, NULL);
457 * Function called when we get a result from the dht
460 * @param cls the request handle
461 * @param exp lifetime
462 * @param key the key the record was stored under
463 * @param get_path get path
464 * @param get_path_length get path length
465 * @param put_path put path
466 * @param put_path_length put path length
467 * @param type the block type
468 * @param size the size of the record
469 * @param data the record data
472 process_record_dht_result(void* cls,
473 struct GNUNET_TIME_Absolute exp,
474 const GNUNET_HashCode * key,
475 const struct GNUNET_PeerIdentity *get_path,
476 unsigned int get_path_length,
477 const struct GNUNET_PeerIdentity *put_path,
478 unsigned int put_path_length,
479 enum GNUNET_BLOCK_Type type,
480 size_t size, const void *data)
482 struct GNUNET_GNS_ResolverHandle *rh;
483 struct RecordLookupHandle *rlh;
484 struct GNSNameRecordBlock *nrb;
485 uint32_t num_records;
487 char* rd_data = (char*)data;
491 GNUNET_HashCode zone, name_hash;
492 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
497 //FIXME maybe check expiration here, check block type
499 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
500 rlh = (struct RecordLookupHandle *) rh->proc_cls;
501 nrb = (struct GNSNameRecordBlock*)data;
503 /* stop lookup and timeout task */
504 GNUNET_DHT_get_stop (rh->get_handle);
505 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
507 rh->get_handle = NULL;
508 name = (char*)&nrb[1];
509 num_records = ntohl(nrb->rd_count);
511 struct GNUNET_NAMESTORE_RecordData rd[num_records];
513 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
514 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
516 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
521 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
525 for (i=0; i<num_records; i++)
527 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
528 "Got name: %s (wanted %s)\n", name, rh->name);
529 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
532 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
533 "Got data length: %d\n", rd[i].data_size);
534 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
535 "Got flag %d\n", rd[i].flags);
537 if ((strcmp(name, rh->name) == 0) &&
538 (rd[i].record_type == rlh->record_type))
545 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
546 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
549 * FIXME check pubkey against existing key in namestore?
550 * https://gnunet.org/bugs/view.php?id=2179
553 /* Save to namestore */
554 GNUNET_NAMESTORE_record_put (namestore_handle,
561 &on_namestore_record_put_result, //cont
565 rh->proc(rh->proc_cls, rh, num_records, rd);
567 rh->proc(rh->proc_cls, rh, 0, NULL);
574 * Start DHT lookup for a (name -> query->record_type) record in
575 * rh->authority's zone
577 * @param rh the pending gns query context
578 * @param name the name to query record
581 resolve_record_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
584 GNUNET_HashCode name_hash;
585 GNUNET_HashCode lookup_key;
586 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
587 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
589 GNUNET_CRYPTO_hash(rh->name, strlen(rh->name), &name_hash);
590 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
591 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
593 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
594 "starting dht lookup for %s with key: %s\n",
595 rh->name, (char*)&lookup_key_string);
597 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
598 &dht_lookup_timeout, rh);
600 xquery = htonl(rlh->record_type);
601 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
602 DHT_OPERATION_TIMEOUT,
603 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
605 DHT_GNS_REPLICATION_LEVEL,
609 &process_record_dht_result,
616 * Namestore calls this function if we have record for this name.
617 * (or with rd_count=0 to indicate no matches)
619 * @param cls the pending query
620 * @param key the key of the zone we did the lookup
621 * @param expiration expiration date of the namestore entry
622 * @param name the name for which we need an authority
623 * @param rd_count the number of records with 'name'
624 * @param rd the record data
625 * @param signature the signature of the authority for the record data
628 process_record_lookup_ns(void* cls,
629 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
630 struct GNUNET_TIME_Absolute expiration,
631 const char *name, unsigned int rd_count,
632 const struct GNUNET_NAMESTORE_RecordData *rd,
633 const struct GNUNET_CRYPTO_RsaSignature *signature)
635 struct GNUNET_GNS_ResolverHandle *rh;
636 struct RecordLookupHandle *rlh;
637 struct GNUNET_TIME_Relative remaining_time;
638 GNUNET_HashCode zone;
640 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
641 rlh = (struct RecordLookupHandle *)rh->proc_cls;
642 GNUNET_CRYPTO_hash(key,
643 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
645 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
651 rh->status |= EXISTS;
654 if (remaining_time.rel_value == 0)
656 rh->status |= EXPIRED;
662 * Lookup terminated and no results
664 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
665 "Namestore lookup for %s terminated without results\n", name);
669 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
670 "Record %s unknown in namestore\n",
673 * Our zone and no result? Cannot resolve TT
675 rh->proc(rh->proc_cls, rh, 0, NULL);
682 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
683 "Processing additional result %s from namestore\n", name);
685 for (i=0; i<rd_count;i++)
688 if (rd[i].record_type != rlh->record_type)
691 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
694 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
705 if (rh->answered == 0)
707 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
708 "No answers found. This is odd!\n");
709 rh->proc(rh->proc_cls, rh, 0, NULL);
713 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
716 rh->proc(rh->proc_cls, rh, rd_count, rd);
721 * The final phase of resolution.
722 * This is a name that is canonical and we do not have a delegation.
724 * @param rh the pending lookup
727 resolve_record_from_ns(struct GNUNET_GNS_ResolverHandle *rh)
729 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
731 * Try to resolve this record in our namestore.
732 * The name to resolve is now in rh->authority_name
733 * since we tried to resolve it to an authority
736 GNUNET_NAMESTORE_lookup_record(namestore_handle,
740 &process_record_lookup_ns,
747 * Handle timeout for DHT requests
749 * @param cls the request handle as closure
750 * @param tc the task context
753 dht_authority_lookup_timeout(void *cls,
754 const struct GNUNET_SCHEDULER_TaskContext *tc)
756 struct GNUNET_GNS_ResolverHandle *rh = cls;
758 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
759 "dht lookup for query %s timed out.\n",
762 GNUNET_DHT_get_stop (rh->get_handle);
763 if (strcmp(rh->name, "") == 0)
766 * promote authority back to name and try to resolve record
768 strcpy(rh->name, rh->authority_name);
770 rh->proc(rh->proc_cls, rh, 0, NULL);
774 static void resolve_delegation_from_dht(struct GNUNET_GNS_ResolverHandle *rh);
777 * Function called when we get a result from the dht
778 * for our query. Recursively tries to resolve PKEYs
781 * @param cls the request handle
782 * @param exp lifetime
783 * @param key the key the record was stored under
784 * @param get_path get path
785 * @param get_path_length get path length
786 * @param put_path put path
787 * @param put_path_length put path length
788 * @param type the block type
789 * @param size the size of the record
790 * @param data the record data
793 process_authority_dht_result(void* cls,
794 struct GNUNET_TIME_Absolute exp,
795 const GNUNET_HashCode * key,
796 const struct GNUNET_PeerIdentity *get_path,
797 unsigned int get_path_length,
798 const struct GNUNET_PeerIdentity *put_path,
799 unsigned int put_path_length,
800 enum GNUNET_BLOCK_Type type,
801 size_t size, const void *data)
803 struct GNUNET_GNS_ResolverHandle *rh;
804 struct GNSNameRecordBlock *nrb;
805 uint32_t num_records;
807 char* rd_data = (char*) data;
810 GNUNET_HashCode zone, name_hash;
815 //FIXME check expiration?
817 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
818 nrb = (struct GNSNameRecordBlock*)data;
820 /* stop dht lookup and timeout task */
821 GNUNET_DHT_get_stop (rh->get_handle);
822 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
824 rh->get_handle = NULL;
825 num_records = ntohl(nrb->rd_count);
826 name = (char*)&nrb[1];
828 struct GNUNET_NAMESTORE_RecordData rd[num_records];
830 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
831 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
833 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
838 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
842 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
843 "Got name: %s (wanted %s)\n", name, rh->authority_name);
844 for (i=0; i<num_records; i++)
847 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
848 "Got name: %s (wanted %s)\n", name, rh->authority_name);
849 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
850 "Got type: %d (wanted %d)\n",
851 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
852 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
853 "Got data length: %d\n", rd[i].data_size);
854 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
855 "Got flag %d\n", rd[i].flags);
857 if ((strcmp(name, rh->authority_name) == 0) &&
858 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
860 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
862 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
863 struct AuthorityChain *auth =
864 GNUNET_malloc(sizeof(struct AuthorityChain));
865 auth->zone = rh->authority;
866 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
867 memset(auth->name, 0, strlen(rh->authority_name)+1);
868 strcpy(auth->name, rh->authority_name);
869 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
870 rh->authority_chain_tail,
877 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
878 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
880 /* Save to namestore */
881 if (0 != GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
883 GNUNET_NAMESTORE_record_put (namestore_handle,
890 &on_namestore_record_put_result, //cont
899 if (strcmp(rh->name, "") == 0)
900 rh->proc(rh->proc_cls, rh, 0, NULL);
902 resolve_delegation_from_dht(rh);
907 * should never get here unless false dht key/put
908 * block plugin should handle this
911 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DHT authority lookup error!\n");
917 * Process DHT lookup result for record.
919 * @param cls the closure
920 * @param rh resolver handle
921 * @param rd_count number of results (always 0)
922 * @param rd record data (always NULL)
925 process_record_result_dht(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
926 unsigned int rd_count,
927 const struct GNUNET_NAMESTORE_RecordData *rd)
929 struct RecordLookupHandle* rlh;
930 rlh = (struct RecordLookupHandle*)cls;
933 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
934 "No records for %s found in DHT. Aborting\n",
936 /* give up, cannot resolve */
937 rlh->proc(rlh->proc_cls, rh, 0, NULL);
938 //reply_to_dns(NULL, rh, 0, NULL);
942 /* results found yay */
943 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
944 "Record resolved from namestore!");
945 rlh->proc(rlh->proc_cls, rh, rd_count, rd);
946 //reply_to_dns(NULL, rh, rd_count, rd);
952 * Process namestore lookup result for record.
954 * @param cls the closure
955 * @param rh resolver handle
956 * @param rd_count number of results (always 0)
957 * @param rd record data (always NULL)
960 process_record_result_ns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
961 unsigned int rd_count,
962 const struct GNUNET_NAMESTORE_RecordData *rd)
964 struct RecordLookupHandle* rlh;
965 rlh = (struct RecordLookupHandle*) cls;
968 /* ns entry expired. try dht */
969 if (rh->status & (EXPIRED | !EXISTS))
971 rh->proc = &process_record_result_dht;
972 resolve_record_from_dht(rh);
975 /* give up, cannot resolve */
976 rlh->proc(rlh->proc_cls, rh, 0, NULL);
977 //reply_to_dns(NULL, rh, 0, NULL);
981 /* results found yay */
982 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
983 "Record resolved from namestore!");
984 rlh->proc(rlh->proc_cls, rh, rd_count, rd);
985 //reply_to_dns(NULL, rh, rd_count, rd);
991 * Determine if this name is canonical.
993 * a.b.gnunet = not canonical
996 * @param name the name to test
997 * @return 1 if canonical
1000 is_canonical(char* name)
1002 uint32_t len = strlen(name);
1005 for (i=0; i<len; i++)
1007 if (*(name+i) == '.')
1014 * Move one level up in the domain hierarchy and return the
1015 * passed top level domain.
1017 * @param name the domain
1018 * @param dest the destination where the tld will be put
1021 pop_tld(char* name, char* dest)
1025 if (is_canonical(name))
1032 for (len = strlen(name); len > 0; len--)
1034 if (*(name+len) == '.')
1044 strcpy(dest, (name+len+1));
1048 * DHT resolution for delegation finished. Processing result.
1050 * @param cls the closure
1051 * @param rh resolver handle
1052 * @param rd_count number of results (always 0)
1053 * @param rd record data (always NULL)
1056 process_dht_delegation_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1057 unsigned int rd_count,
1058 const struct GNUNET_NAMESTORE_RecordData *rd)
1060 struct RecordLookupHandle* rlh;
1061 rlh = (struct RecordLookupHandle*) cls;
1063 if (strcmp(rh->name, "") == 0)
1065 /* We resolved full name for delegation. resolving record */
1066 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1067 "Resolved full name for delegation via DHT. resolving record '' in ns\n");
1068 rh->proc = &process_record_result_ns;
1069 resolve_record_from_ns(rh);
1074 * we still have some left
1076 if (is_canonical(rh->name))
1078 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1079 "Resolving canonical record %s in ns\n", rh->name);
1080 rh->proc = &process_record_result_ns;
1081 resolve_record_from_ns(rh);
1084 /* give up, cannot resolve */
1085 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1086 "Cannot fully resolve delegation for %s via DHT!\n",
1088 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1089 //reply_to_dns(NULL, rh, 0, NULL);
1094 * Start DHT lookup for a name -> PKEY (compare NS) record in
1095 * rh->authority's zone
1097 * @param rh the pending gns query
1098 * @param name the name of the PKEY record
1101 resolve_delegation_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
1104 GNUNET_HashCode name_hash;
1105 GNUNET_HashCode lookup_key;
1107 GNUNET_CRYPTO_hash(rh->authority_name,
1108 strlen(rh->authority_name),
1110 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1112 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1113 &dht_authority_lookup_timeout,
1116 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1118 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1119 DHT_OPERATION_TIMEOUT,
1120 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1122 DHT_GNS_REPLICATION_LEVEL,
1126 &process_authority_dht_result,
1133 * Namestore resolution for delegation finished. Processing result.
1135 * @param cls the closure
1136 * @param rh resolver handle
1137 * @param rd_count number of results (always 0)
1138 * @param rd record data (always NULL)
1141 process_ns_delegation_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1142 unsigned int rd_count,
1143 const struct GNUNET_NAMESTORE_RecordData *rd)
1145 struct RecordLookupHandle* rlh;
1146 rlh = (struct RecordLookupHandle*) cls;
1148 if (strcmp(rh->name, "") == 0)
1150 /* We resolved full name for delegation. resolving record */
1151 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1152 "Resolved full name for delegation. resolving record ''\n");
1153 rh->proc = &process_record_result_ns;
1154 resolve_record_from_ns(rh);
1159 * we still have some left
1160 * check if ns entry is fresh
1162 if (rh->status & (EXISTS | !EXPIRED))
1164 if (is_canonical(rh->name))
1166 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1167 "Resolving canonical record %s\n", rh->name);
1168 rh->proc = &process_record_result_ns;
1169 resolve_record_from_ns(rh);
1173 /* give up, cannot resolve */
1174 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1175 "Cannot fully resolve delegation for %s!\n",
1177 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1178 //reply_to_dns(NULL, rh, 0, NULL);
1183 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1184 "Trying to resolve delegation for %s via DHT\n",
1186 rh->proc = &process_dht_delegation_dns;
1187 resolve_delegation_from_dht(rh);
1191 static void resolve_delegation_from_ns(struct GNUNET_GNS_ResolverHandle *rh);
1194 * This is a callback function that should give us only PKEY
1195 * records. Used to query the namestore for the authority (PKEY)
1196 * for 'name'. It will recursively try to resolve the
1197 * authority for a given name from the namestore.
1199 * @param cls the pending query
1200 * @param key the key of the zone we did the lookup
1201 * @param expiration expiration date of the record data set in the namestore
1202 * @param name the name for which we need an authority
1203 * @param rd_count the number of records with 'name'
1204 * @param rd the record data
1205 * @param signature the signature of the authority for the record data
1208 process_authority_lookup_ns(void* cls,
1209 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1210 struct GNUNET_TIME_Absolute expiration,
1212 unsigned int rd_count,
1213 const struct GNUNET_NAMESTORE_RecordData *rd,
1214 const struct GNUNET_CRYPTO_RsaSignature *signature)
1216 struct GNUNET_GNS_ResolverHandle *rh;
1217 struct GNUNET_TIME_Relative remaining_time;
1218 GNUNET_HashCode zone;
1221 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %d records from authority lookup\n",
1224 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
1225 GNUNET_CRYPTO_hash(key,
1226 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1228 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1234 rh->status |= EXISTS;
1237 if (remaining_time.rel_value == 0)
1239 rh->status |= EXPIRED;
1243 * No authority found in namestore.
1248 * We did not find an authority in the namestore
1253 * Promote this authority back to a name maybe it is
1256 if (strcmp(rh->name, "") == 0)
1258 /* simply promote back */
1259 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1260 "Promoting %s back to name\n", rh->authority_name);
1261 strcpy(rh->name, rh->authority_name);
1265 /* add back to existing name */
1266 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1267 "Adding %s back to %s\n",
1268 rh->authority_name, rh->name);
1269 new_name = GNUNET_malloc(strlen(rh->name)
1270 + strlen(rh->authority_name) + 2);
1271 memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1272 strcpy(new_name, rh->name);
1273 strcpy(new_name+strlen(new_name)+1, ".");
1274 strcpy(new_name+strlen(new_name)+2, rh->authority_name);
1275 GNUNET_free(rh->name);
1276 rh->name = new_name;
1278 rh->proc(rh->proc_cls, rh, 0, NULL);
1282 //Note only 1 pkey should have been returned.. anything else would be strange
1284 * We found an authority that may be able to help us
1285 * move on with query
1288 for (i=0; i<rd_count;i++)
1291 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1294 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1297 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
1298 if (remaining_time.rel_value == 0)
1300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1301 "This dht entry is expired.\n");
1302 rh->authority_chain_head->fresh = 0;
1303 rh->proc(rh->proc_cls, rh, 0, NULL);
1311 * Resolve rest of query with new authority
1313 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1314 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
1315 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1316 auth->zone = rh->authority;
1317 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
1318 memset(auth->name, 0, strlen(rh->authority_name)+1);
1319 strcpy(auth->name, rh->authority_name);
1320 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1321 rh->authority_chain_tail,
1325 * We are done with PKEY resolution if name is empty
1326 * else resolve again with new authority
1328 if (strcmp(rh->name, "") == 0)
1329 rh->proc(rh->proc_cls, rh, 0, NULL);
1331 resolve_delegation_from_ns(rh);
1338 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1339 "Authority lookup successful but no PKEY... never get here\n");
1340 rh->proc(rh->proc_cls, rh, 0, NULL);
1345 * Resolve the delegation chain for the request
1347 * @param rh the resolver handle
1350 resolve_delegation_from_ns(struct GNUNET_GNS_ResolverHandle *rh)
1353 pop_tld(rh->name, rh->authority_name);
1354 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1357 GNUNET_GNS_RECORD_PKEY,
1358 &process_authority_lookup_ns,
1364 * Entry point for name resolution
1365 * Setup a new query and try to resolve
1367 * @param request the request handle of the DNS request from a client
1368 * @param p the DNS query packet we received
1369 * @param q the DNS query we received parsed from p
1372 start_resolution_from_dns(struct GNUNET_DNS_RequestHandle *request,
1373 struct GNUNET_DNSPARSER_Packet *p,
1374 struct GNUNET_DNSPARSER_Query *q)
1376 struct GNUNET_GNS_ResolverHandle *rh;
1377 struct RecordLookupHandle* rlh;
1378 struct InterceptLookupHandle* ilh;
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1381 "Starting resolution for %s (type=%d)!\n",
1384 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
1385 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
1386 ilh = GNUNET_malloc(sizeof(struct InterceptLookupHandle));
1389 ilh->request_handle = request;
1391 rh->authority = zone_hash;
1393 rlh->record_type = q->type;
1394 rlh->name = q->name;
1395 rlh->proc = &reply_to_dns;
1396 rlh->proc_cls = ilh;
1400 rh->authority = zone_hash;
1401 rh->name = GNUNET_malloc(strlen(q->name)
1402 - strlen(gnunet_tld) + 1);
1404 strlen(q->name)-strlen(gnunet_tld) + 1);
1405 memcpy(rh->name, q->name,
1406 strlen(q->name)-strlen(gnunet_tld));
1408 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
1410 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
1411 rh->authority_chain_head->prev = NULL;
1412 rh->authority_chain_head->next = NULL;
1413 rh->authority_chain_tail = rh->authority_chain_head;
1414 rh->authority_chain_head->zone = zone_hash;
1416 /* Start resolution in our zone */
1417 rh->proc = &process_ns_delegation_dns;
1418 resolve_delegation_from_ns(rh);
1424 * The DNS request handler
1425 * Called for every incoming DNS request.
1427 * @param cls closure
1428 * @param rh request handle to user for reply
1429 * @param request_length number of bytes in request
1430 * @param request udp payload of the DNS request
1433 handle_dns_request(void *cls,
1434 struct GNUNET_DNS_RequestHandle *rh,
1435 size_t request_length,
1436 const char *request)
1438 struct GNUNET_DNSPARSER_Packet *p;
1442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
1443 p = GNUNET_DNSPARSER_parse (request, request_length);
1447 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1448 "Received malformed DNS packet, leaving it untouched\n");
1449 GNUNET_DNS_request_forward (rh);
1450 GNUNET_DNSPARSER_free_packet (p);
1455 * Check tld and decide if we or
1456 * legacy dns is responsible
1458 * FIXME now in theory there could be more than 1 query in the request
1459 * but if this is case we get into trouble:
1460 * either we query the GNS or the DNS. We cannot do both!
1461 * So I suggest to either only allow a single query per request or
1462 * only allow GNS or DNS requests.
1463 * The way it is implemented here now is buggy and will lead to erratic
1464 * behaviour (if multiple queries are present).
1466 if (p->num_queries == 0)
1468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1469 "No Queries in DNS packet... forwarding\n");
1470 GNUNET_DNS_request_forward (rh);
1473 if (p->num_queries > 1)
1475 /* Note: We could also look for .gnunet */
1476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1477 ">1 queriy in DNS packet... odd. We only process #1\n");
1484 tldoffset = p->queries[0].name + strlen(p->queries[0].name) - 1;
1486 for (i=0; i<strlen(p->queries[0].name); i++)
1488 if (*(tldoffset-i) == '.')
1492 if ((i==strlen(gnunet_tld)-1) && (0 == strcmp(tldoffset-i, gnunet_tld)))
1494 start_resolution_from_dns(rh, p, p->queries);
1499 * This request does not concern us. Forward to real DNS.
1501 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1502 "Request for %s is forwarded to DNS\n", p->queries[0].name);
1503 GNUNET_DNS_request_forward (rh);
1504 GNUNET_DNSPARSER_free_packet (p);
1510 * Method called periodicattluy that triggers
1511 * iteration over root zone
1513 * @param cls closure
1514 * @param tc task context
1517 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1519 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1523 * Continuation for DHT put
1525 * @param cls closure
1526 * @param tc task context
1529 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1531 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1536 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1539 * Function used to put all records successively into the DHT.
1541 * @param cls the closure (NULL)
1542 * @param key the public key of the authority (ours)
1543 * @param expiration lifetime of the namestore entry
1544 * @param name the name of the records
1545 * @param rd_count the number of records in data
1546 * @param rd the record data
1547 * @param signature the signature for the record data
1550 put_gns_record(void *cls,
1551 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1552 struct GNUNET_TIME_Absolute expiration,
1554 unsigned int rd_count,
1555 const struct GNUNET_NAMESTORE_RecordData *rd,
1556 const struct GNUNET_CRYPTO_RsaSignature *signature)
1559 struct GNSNameRecordBlock *nrb;
1560 GNUNET_HashCode name_hash;
1561 GNUNET_HashCode xor_hash;
1562 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1563 uint32_t rd_payload_length;
1564 char* nrb_data = NULL;
1569 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1570 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1571 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1576 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1577 "Putting records for %s into the DHT\n", name);
1579 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1581 nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1
1582 + sizeof(struct GNSNameRecordBlock));
1584 if (signature != NULL)
1585 nrb->signature = *signature;
1587 nrb->public_key = *key;
1589 nrb->rd_count = htonl(rd_count);
1591 memset(&nrb[1], 0, strlen(name) + 1);
1592 memcpy(&nrb[1], name, strlen(name));
1594 nrb_data = (char*)&nrb[1];
1595 nrb_data += strlen(name) + 1;
1597 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
1600 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
1605 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
1612 * calculate DHT key: H(name) xor H(pubkey)
1614 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1615 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1616 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1617 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1618 "putting records for %s under key: %s with size %d\n",
1619 name, (char*)&xor_hash_string, rd_payload_length);
1621 GNUNET_DHT_put (dht_handle, &xor_hash,
1622 DHT_GNS_REPLICATION_LEVEL,
1624 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1628 DHT_OPERATION_TIMEOUT,
1630 NULL); //cls for cont
1632 num_public_records++;
1635 * Reschedule periodic put
1637 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1638 &update_zone_dht_next,
1646 * Periodically iterate over our zone and store everything in dht
1649 * @param tc task context
1652 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1654 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1655 if (0 == num_public_records)
1657 dht_update_interval = GNUNET_TIME_relative_multiply(
1658 GNUNET_TIME_UNIT_SECONDS,
1663 dht_update_interval = GNUNET_TIME_relative_multiply(
1664 GNUNET_TIME_UNIT_SECONDS,
1665 (3600/num_public_records));
1667 num_public_records = 0; //start counting again
1668 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1670 GNUNET_NAMESTORE_RF_AUTHORITY,
1671 GNUNET_NAMESTORE_RF_PRIVATE,
1677 static void send_shorten_response(const char* name,
1678 struct ClientShortenHandle *csh);
1680 process_shorten_pseu_lookup_ns(void *cls,
1681 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1682 struct GNUNET_TIME_Absolute expire,
1684 unsigned int rd_len,
1685 const struct GNUNET_NAMESTORE_RecordData *rd,
1686 const struct GNUNET_CRYPTO_RsaSignature *signature)
1688 struct GNUNET_GNS_ResolverHandle *rh =
1689 (struct GNUNET_GNS_ResolverHandle *)cls;
1690 struct GNUNET_TIME_Relative remaining_time;
1697 rh->status |= EXISTS;
1700 if (remaining_time.rel_value == 0)
1702 rh->status |= EXPIRED;
1705 rh->proc(rh->proc_cls, rh, rd_len, rd);
1710 * Start DHT lookup for a PSEUdonym record in
1711 * rh->authority's zone
1713 * @param rh the pending gns query
1714 * @param name the name of the PKEY record
1717 resolve_pseu_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
1720 GNUNET_HashCode name_hash;
1721 GNUNET_HashCode lookup_key;
1724 GNUNET_CRYPTO_hash("",
1728 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1730 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1731 &dht_lookup_timeout,
1734 xquery = htonl(GNUNET_GNS_RECORD_PSEU);
1736 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1737 DHT_OPERATION_TIMEOUT,
1738 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1740 DHT_GNS_REPLICATION_LEVEL,
1744 &process_authority_dht_result,
1751 handle_shorten_pseu_ns_result(void* cls,
1752 struct GNUNET_GNS_ResolverHandle *rh,
1754 const struct GNUNET_NAMESTORE_RecordData *rd);
1757 handle_shorten_zone_to_name(void *cls,
1758 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1759 struct GNUNET_TIME_Absolute expire,
1761 unsigned int rd_len,
1762 const struct GNUNET_NAMESTORE_RecordData *rd,
1763 const struct GNUNET_CRYPTO_RsaSignature *signature)
1765 struct GNUNET_GNS_ResolverHandle *rh =
1766 (struct GNUNET_GNS_ResolverHandle *)cls;
1767 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) rh->proc_cls;
1772 /* we found a match in our own zone */
1775 answer_len = strlen(rh->name) + strlen(name) + strlen(gnunet_tld) + 2;
1776 result = GNUNET_malloc(answer_len);
1777 memset(result, 0, answer_len);
1778 strcpy(result, rh->name);
1779 strcpy(result+strlen(rh->name), ".");
1780 strcpy(result+strlen(rh->name)+1, name);
1781 strcpy(result+strlen(rh->name)+strlen(name), gnunet_tld);
1783 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1784 "Sending shorten result %s\n", result);
1786 send_shorten_response(result, csh);
1788 GNUNET_free(result);
1793 * Nothing in our zone
1794 * check PSEU for this authority in namestore
1796 rh->proc = &handle_shorten_pseu_ns_result;
1797 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1798 &rh->authority_chain_head->zone,
1800 GNUNET_GNS_RECORD_PSEU,
1801 &process_shorten_pseu_lookup_ns,
1807 * Process result from namestore delegation lookup
1808 * for shorten operation
1810 * @param cls the client shorten handle
1811 * @param rh the resolver handle
1812 * @param rd_count number of results (0)
1813 * @param rd data (NULL)
1816 handle_shorten_pseu_dht_result(void* cls,
1817 struct GNUNET_GNS_ResolverHandle *rh,
1819 const struct GNUNET_NAMESTORE_RecordData *rd)
1821 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1822 struct AuthorityChain *auth_chain;
1834 for (i=0; i < rd_len; i++)
1836 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1838 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1839 "Found PSEU %s\n", (char*) rd[i].data);
1844 pseu = (char*) rd[i].data;
1845 answer_len = strlen(rh->name) + strlen(pseu) + strlen(gnunet_tld) + 2;
1846 result = GNUNET_malloc(answer_len);
1847 memset(result, 0, answer_len);
1848 strcpy(result, rh->name);
1849 strcpy(result+strlen(rh->name), ".");
1850 strcpy(result+strlen(rh->name)+1, pseu);
1851 strcpy(result+strlen(rh->name)+strlen(pseu), gnunet_tld);
1853 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1854 "Sending pseudonym shorten result %s\n", result);
1856 send_shorten_response(result, csh);
1858 GNUNET_free(result);
1864 * continue with next authority
1867 auth_chain = rh->authority_chain_head;
1869 if ((auth_chain->next->next == NULL) &&
1870 GNUNET_CRYPTO_hash_cmp(&auth_chain->next->zone, &zone_hash) == 0)
1875 answer_len = strlen(rh->name) + strlen(auth_chain->name)
1876 + strlen(gnunet_tld) + 2;
1878 result = GNUNET_malloc(answer_len);
1879 memset(result, 0, answer_len);
1880 strcpy(result, rh->name);
1881 strcpy(result+strlen(rh->name), ".");
1882 strcpy(result+strlen(rh->name)+1, auth_chain->name);
1883 strcpy(result+strlen(rh->name)+strlen(auth_chain->name)+1, gnunet_tld);
1884 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1885 "Sending non pseudonym shorten result %s\n", result);
1887 send_shorten_response(result, csh);
1888 GNUNET_free(result);
1893 * Continue with next authority
1895 new_name = GNUNET_malloc(strlen(rh->name)+
1896 strlen(auth_chain->name) + 2);
1897 memset(new_name, 0, strlen(rh->name)+
1898 strlen(auth_chain->name) + 2);
1899 strcpy(new_name, rh->name);
1900 strcpy(new_name+strlen(rh->name)+1, ".");
1901 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1902 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1903 rh->authority_chain_tail,
1905 GNUNET_free(rh->name);
1906 rh->name = new_name;
1907 GNUNET_free(auth_chain->name);
1908 GNUNET_free(auth_chain);
1909 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1911 &rh->authority_chain_head->zone,
1912 &handle_shorten_zone_to_name,
1920 * Process result from namestore PSEU lookup
1921 * for shorten operation
1922 * FIXME do we need to check for own zone here?
1924 * @param cls the client shorten handle
1925 * @param rh the resolver handle
1926 * @param rd_count number of results (0 if none found)
1927 * @param rd data (NULL if none found)
1930 handle_shorten_pseu_ns_result(void* cls,
1931 struct GNUNET_GNS_ResolverHandle *rh,
1933 const struct GNUNET_NAMESTORE_RecordData *rd)
1935 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1936 struct AuthorityChain *auth_chain;
1948 for (i=0; i < rd_len; i++)
1950 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1952 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1953 "Found PSEU %s\n", (char*) rd[i].data);
1958 pseu = (char*) rd[i].data;
1959 answer_len = strlen(rh->name) + strlen(pseu) + strlen(gnunet_tld) + 2;
1960 result = GNUNET_malloc(answer_len);
1961 memset(result, 0, answer_len);
1962 strcpy(result, rh->name);
1963 strcpy(result+strlen(rh->name), ".");
1964 strcpy(result+strlen(rh->name)+1, pseu);
1965 strcpy(result+strlen(rh->name)+strlen(pseu)+1, gnunet_tld);
1967 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1968 "Sending shorten result %s\n", result);
1970 send_shorten_response(result, csh);
1972 GNUNET_free(result);
1977 * No PSEU found. Ask DHT if expired.
1978 * Else contunue with next authority
1980 if (rh->status & (EXISTS | !EXPIRED))
1985 auth_chain = rh->authority_chain_head;
1986 new_name = GNUNET_malloc(strlen(rh->name)+
1987 strlen(auth_chain->name) + 2);
1988 memset(new_name, 0, strlen(rh->name)+
1989 strlen(auth_chain->name) + 2);
1990 strcpy(new_name, rh->name);
1991 strcpy(new_name+strlen(rh->name)+1, ".");
1992 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1994 GNUNET_free(rh->name);
1995 rh->name = new_name;
1996 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1997 rh->authority_chain_tail,
2000 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2002 &rh->authority_chain_head->zone,
2003 &handle_shorten_zone_to_name,
2011 rh->authority = rh->authority_chain_head->zone;
2012 rh->proc = &handle_shorten_pseu_dht_result;
2013 resolve_pseu_from_dht(rh);
2020 * Process result from namestore delegation lookup
2021 * for shorten operation
2023 * @param cls the client shorten handle
2024 * @param rh the resolver handle
2025 * @param rd_count number of results (0)
2026 * @param rd data (NULL)
2029 handle_shorten_delegation_result(void* cls,
2030 struct GNUNET_GNS_ResolverHandle *rh,
2032 const struct GNUNET_NAMESTORE_RecordData *rd)
2034 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
2035 struct AuthorityChain *auth_chain;
2040 * At this point rh->name contains the part of the name
2041 * that we do not have a PKEY in our namestore to resolve.
2042 * The authority chain in the resolver handle is now
2043 * useful to backtrack if needed
2046 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2047 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2049 if (GNUNET_CRYPTO_hash_cmp(&rh->authority_chain_head->zone,
2053 * This is our zone append .gnunet unless name is empty
2054 * (it shouldn't be, usually FIXME what happens if we
2055 * shorten to our zone to a "" record??)
2058 answer_len = strlen(rh->name) + strlen(gnunet_tld) + 2;
2059 result = GNUNET_malloc(answer_len);
2060 memset(result, 0, answer_len);
2061 strcpy(result, rh->name);
2062 strcpy(result+strlen(rh->name), ".");
2063 strcpy(result+strlen(rh->name)+1, gnunet_tld);
2065 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2066 "Our zone: Sending name as shorten result %s\n", rh->name);
2068 send_shorten_response(rh->name, csh); //FIXME +.gnunet!
2069 free_resolver_handle(rh);
2070 GNUNET_free(csh->name);
2072 GNUNET_free(result);
2076 auth_chain = rh->authority_chain_head;
2077 /* backtrack authorities for pseu */
2078 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2081 &handle_shorten_zone_to_name,
2086 typedef void (*ShortenResponseProc) (void* cls, const char* name);
2089 * Shorten a given name
2091 * @param name the name to shorten
2092 * @param proc the processor to call when finished
2093 * @praram cls the closure to the processor
2096 shorten_name(char* name, struct ClientShortenHandle* csh)
2099 struct GNUNET_GNS_ResolverHandle *rh;
2101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2102 "Starting resolution for %s (type=%d)!\n",
2103 name, GNUNET_GNS_RECORD_PKEY);
2105 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
2106 rh->authority = zone_hash;
2108 rh->name = GNUNET_malloc(strlen(name)
2109 - strlen(gnunet_tld) + 1);
2111 strlen(name)-strlen(gnunet_tld) + 1);
2112 memcpy(rh->name, name,
2113 strlen(name)-strlen(gnunet_tld));
2115 csh->name = GNUNET_malloc(strlen(name)
2116 - strlen(gnunet_tld) + 1);
2118 strlen(name)-strlen(gnunet_tld) + 1);
2119 memcpy(rh->name, name,
2120 strlen(name)-strlen(gnunet_tld));
2122 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
2124 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2125 rh->authority_chain_tail = rh->authority_chain_head;
2126 rh->authority_chain_head->zone = zone_hash;
2127 rh->proc = &handle_shorten_delegation_result;
2128 rh->proc_cls = (void*)csh;
2130 /* Start delegation resolution in our namestore */
2131 resolve_delegation_from_ns(rh);
2136 * Send shorten response back to client
2137 * FIXME this is without .gnunet!
2139 * @param cls the client handle in closure
2140 * @param name the shortened name result or NULL if cannot be shortened
2143 send_shorten_response(const char* name, struct ClientShortenHandle *csh)
2145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
2146 "SHORTEN_RESULT", name);
2147 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
2154 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
2155 + strlen(name) + 1);
2157 rmsg->unique_id = csh->unique_id;
2158 rmsg->key = csh->key;
2159 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
2161 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
2164 strcpy((char*)&rmsg[1], name);
2166 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
2167 (const struct GNUNET_MessageHeader *) rmsg,
2170 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
2177 * Handle a shorten message from the api
2179 * @param cls the closure
2180 * @param client the client
2181 * @param message the message
2183 static void handle_shorten(void *cls,
2184 struct GNUNET_SERVER_Client * client,
2185 const struct GNUNET_MessageHeader * message)
2187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
2189 size_t msg_size = 0;
2190 struct ClientShortenHandle *csh;
2192 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
2194 GNUNET_break_op (0);
2195 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2199 GNUNET_SERVER_notification_context_add (nc, client);
2201 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
2202 (struct GNUNET_GNS_ClientShortenMessage *) message;
2204 msg_size = ntohs(message->size);
2206 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2208 GNUNET_break_op (0);
2209 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2213 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
2214 csh->client = client;
2215 csh->unique_id = sh_msg->unique_id;
2216 csh->key = sh_msg->key;
2218 shorten_name((char*)&sh_msg[1], csh);
2226 handle_lookup(void *cls,
2227 struct GNUNET_SERVER_Client * client,
2228 const struct GNUNET_MessageHeader * message)
2233 * Process GNS requests.
2235 * @param cls closure)
2236 * @param server the initialized server
2237 * @param c configuration to use
2240 run (void *cls, struct GNUNET_SERVER_Handle *server,
2241 const struct GNUNET_CONFIGURATION_Handle *c)
2244 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
2247 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2249 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2250 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
2251 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0}
2254 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
2255 "ZONEKEY", &keyfile))
2257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2258 "No private key for root zone specified%s!\n", keyfile);
2259 GNUNET_SCHEDULER_shutdown(0);
2263 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2264 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
2266 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2268 GNUNET_free(keyfile);
2271 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
2274 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2275 "DNS hijacking enabled... connecting to service.\n");
2277 * Do gnunet dns init here
2279 dns_handle = GNUNET_DNS_connect(c,
2280 GNUNET_DNS_FLAG_PRE_RESOLUTION,
2281 &handle_dns_request, /* rh */
2282 NULL); /* Closure */
2283 if (NULL == dns_handle)
2285 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2286 "Failed to connect to the dnsservice!\n");
2293 * handle to our local namestore
2295 namestore_handle = GNUNET_NAMESTORE_connect(c);
2297 if (NULL == namestore_handle)
2299 //FIXME do error handling;
2300 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2301 "Failed to connect to the namestore!\n");
2302 GNUNET_SCHEDULER_shutdown(0);
2309 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
2311 if (NULL == dht_handle)
2313 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
2316 //put_some_records(); //FIXME for testing
2319 * Schedule periodic put
2321 * We have roughly an hour for all records;
2323 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
2325 //zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
2327 GNUNET_SERVER_add_handlers (server, handlers);
2330 //GNUNET_SERVER_disconnect_notify (server,
2331 // &client_disconnect_notification,
2334 nc = GNUNET_SERVER_notification_context_create (server, 1);
2336 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
2342 * The main function for the GNS service.
2344 * @param argc number of arguments from the command line
2345 * @param argv command line arguments
2346 * @return 0 ok, 1 on error
2349 main (int argc, char *const *argv)
2355 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
2360 /* end of gnunet-service-gns.c */