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(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;
1771 /* we found a match in our own zone */
1774 result = GNUNET_malloc(strlen(rh->name) + strlen(name) + 1);
1775 memset(result, 0, strlen(rh->name) + strlen(name) + 1);
1776 memcpy(result, rh->name, strlen(rh->name));
1777 memcpy(result+strlen(rh->name)+1, name, strlen(name));
1779 send_shorten_response(result, csh);
1781 GNUNET_free(result);
1786 * Nothing in our zone
1787 * check PSEU for this authority in namestore
1789 rh->proc = &handle_shorten_pseu_ns_result;
1790 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1791 &rh->authority_chain_head->zone,
1793 GNUNET_GNS_RECORD_PSEU,
1794 &process_shorten_pseu_lookup_ns,
1800 * Process result from namestore delegation lookup
1801 * for shorten operation
1803 * @param cls the client shorten handle
1804 * @param rh the resolver handle
1805 * @param rd_count number of results (0)
1806 * @param rd data (NULL)
1809 handle_shorten_pseu_dht_result(void* cls,
1810 struct GNUNET_GNS_ResolverHandle *rh,
1812 const struct GNUNET_NAMESTORE_RecordData *rd)
1814 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1815 struct AuthorityChain *auth_chain;
1826 for (i=0; i < rd_len; i++)
1828 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1832 pseu = (char*) rd[i].data;
1833 result = GNUNET_malloc(strlen(rh->name) + strlen(pseu) + 1);
1834 memset(result, 0, strlen(rh->name) + strlen(pseu) + 1);
1835 memcpy(result, rh->name, strlen(rh->name));
1836 memcpy(result+strlen(rh->name)+1, pseu, strlen(pseu));
1838 send_shorten_response(result, csh);
1840 GNUNET_free(result);
1846 * continue with next authority
1849 auth_chain = rh->authority_chain_head;
1851 if ((auth_chain->next->next == NULL) &&
1852 GNUNET_CRYPTO_hash_cmp(&auth_chain->next->zone, &zone_hash) == 0)
1857 result = GNUNET_malloc(strlen(rh->name) + strlen(auth_chain->name) + 2);
1858 memset(result, 0, strlen(rh->name) + strlen(auth_chain->name) + 2);
1859 strcpy(result, rh->name);
1860 strcpy(result+strlen(rh->name)+1, ".");
1861 strcpy(result+strlen(rh->name)+2, auth_chain->name);
1862 send_shorten_response(result, csh);
1863 GNUNET_free(result);
1868 * Continue with next authority
1870 new_name = GNUNET_malloc(strlen(rh->name)+
1871 strlen(auth_chain->name) + 2);
1872 memset(new_name, 0, strlen(rh->name)+
1873 strlen(auth_chain->name) + 2);
1874 strcpy(new_name, rh->name);
1875 strcpy(new_name+strlen(rh->name)+1, ".");
1876 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1877 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1878 rh->authority_chain_tail,
1880 GNUNET_free(rh->name);
1881 rh->name = new_name;
1882 GNUNET_free(auth_chain->name);
1883 GNUNET_free(auth_chain);
1884 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1886 &rh->authority_chain_head->zone,
1887 &handle_shorten_zone_to_name,
1895 * Process result from namestore PSEU lookup
1896 * for shorten operation
1897 * FIXME do we need to check for own zone here?
1899 * @param cls the client shorten handle
1900 * @param rh the resolver handle
1901 * @param rd_count number of results (0 if none found)
1902 * @param rd data (NULL if none found)
1905 handle_shorten_pseu_ns_result(void* cls,
1906 struct GNUNET_GNS_ResolverHandle *rh,
1908 const struct GNUNET_NAMESTORE_RecordData *rd)
1910 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1911 struct AuthorityChain *auth_chain;
1922 for (i=0; i < rd_len; i++)
1924 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1928 pseu = (char*) rd[i].data;
1929 result = GNUNET_malloc(strlen(rh->name) + strlen(pseu) + 1);
1930 memset(result, 0, strlen(rh->name) + strlen(pseu) + 1);
1931 memcpy(result, rh->name, strlen(rh->name));
1932 memcpy(result+strlen(rh->name)+1, pseu, strlen(pseu));
1934 send_shorten_response(result, csh);
1936 GNUNET_free(result);
1941 * No PSEU found. Ask DHT if expired.
1942 * Else contunue with next authority
1944 if (rh->status & (EXISTS | !EXPIRED))
1949 auth_chain = rh->authority_chain_head;
1950 new_name = GNUNET_malloc(strlen(rh->name)+
1951 strlen(auth_chain->name) + 2);
1952 memset(new_name, 0, strlen(rh->name)+
1953 strlen(auth_chain->name) + 2);
1954 strcpy(new_name, rh->name);
1955 strcpy(new_name+strlen(rh->name)+1, ".");
1956 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1958 GNUNET_free(rh->name);
1959 rh->name = new_name;
1960 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1961 rh->authority_chain_tail,
1964 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1966 &rh->authority_chain_head->zone,
1967 &handle_shorten_zone_to_name,
1975 rh->authority = rh->authority_chain_head->zone;
1976 rh->proc = &handle_shorten_pseu_dht_result;
1977 resolve_pseu_from_dht(rh);
1984 * Process result from namestore delegation lookup
1985 * for shorten operation
1987 * @param cls the client shorten handle
1988 * @param rh the resolver handle
1989 * @param rd_count number of results (0)
1990 * @param rd data (NULL)
1993 handle_shorten_delegation_result(void* cls,
1994 struct GNUNET_GNS_ResolverHandle *rh,
1996 const struct GNUNET_NAMESTORE_RecordData *rd)
1998 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1999 struct AuthorityChain *auth_chain;
2002 * At this point rh->name contains the part of the name
2003 * that we do not have a PKEY in our namestore to resolve.
2004 * The authority chain in the resolver handle is now
2005 * useful to backtrack if needed
2008 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2009 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2011 if (GNUNET_CRYPTO_hash_cmp(&rh->authority_chain_head->zone,
2015 * This is our zone append .gnunet unless name is empty
2016 * (it shouldn't be, usually FIXME what happens if we
2017 * shorten to our zone to a "" record??)
2019 send_shorten_response(rh->name, csh); //FIXME +.gnunet!
2020 free_resolver_handle(rh);
2021 GNUNET_free(csh->name);
2026 auth_chain = rh->authority_chain_head;
2027 /* backtrack authorities for pseu */
2028 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2031 &handle_shorten_zone_to_name,
2036 typedef void (*ShortenResponseProc) (void* cls, const char* name);
2039 * Shorten a given name
2041 * @param name the name to shorten
2042 * @param proc the processor to call when finished
2043 * @praram cls the closure to the processor
2046 shorten_name(char* name, struct ClientShortenHandle* csh)
2049 struct GNUNET_GNS_ResolverHandle *rh;
2051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2052 "Starting resolution for %s (type=%d)!\n",
2053 name, GNUNET_GNS_RECORD_PKEY);
2055 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
2056 rh->authority = zone_hash;
2058 rh->name = GNUNET_malloc(strlen(name)
2059 - strlen(gnunet_tld) + 1);
2061 strlen(name)-strlen(gnunet_tld) + 1);
2062 memcpy(rh->name, name,
2063 strlen(name)-strlen(gnunet_tld));
2065 csh->name = GNUNET_malloc(strlen(name)
2066 - strlen(gnunet_tld) + 1);
2068 strlen(name)-strlen(gnunet_tld) + 1);
2069 memcpy(rh->name, name,
2070 strlen(name)-strlen(gnunet_tld));
2072 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
2074 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2075 rh->authority_chain_tail = rh->authority_chain_head;
2076 rh->authority_chain_head->zone = zone_hash;
2077 rh->proc = &handle_shorten_delegation_result;
2078 rh->proc_cls = (void*)csh;
2080 /* Start delegation resolution in our namestore */
2081 resolve_delegation_from_ns(rh);
2086 * Send shorten response back to client
2087 * FIXME this is without .gnunet!
2089 * @param cls the client handle in closure
2090 * @param name the shortened name result or NULL if cannot be shortened
2093 send_shorten_response(const char* name, struct ClientShortenHandle *csh)
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n",
2097 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
2104 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage *)
2107 rmsg->unique_id = csh->unique_id;
2108 rmsg->key = csh->key;
2109 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
2111 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
2114 strcpy((char*)&rmsg[1], name);
2116 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
2117 (const struct GNUNET_MessageHeader *) rmsg,
2120 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
2127 * Handle a shorten message from the api
2129 * @param cls the closure
2130 * @param client the client
2131 * @param message the message
2133 static void handle_shorten(void *cls,
2134 struct GNUNET_SERVER_Client * client,
2135 const struct GNUNET_MessageHeader * message)
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
2139 size_t msg_size = 0;
2140 struct ClientShortenHandle *csh;
2142 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
2144 GNUNET_break_op (0);
2145 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2149 GNUNET_SERVER_notification_context_add (nc, client);
2150 GNUNET_SERVER_client_keep (client);
2152 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
2153 (struct GNUNET_GNS_ClientShortenMessage *) message;
2155 msg_size = ntohs(message->size);
2157 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2159 GNUNET_break_op (0);
2160 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2164 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
2165 csh->client = client;
2166 csh->unique_id = sh_msg->unique_id;
2167 csh->key = sh_msg->key;
2169 shorten_name((char*)&sh_msg[1], csh);
2177 handle_lookup(void *cls,
2178 struct GNUNET_SERVER_Client * client,
2179 const struct GNUNET_MessageHeader * message)
2184 * Process GNS requests.
2186 * @param cls closure)
2187 * @param server the initialized server
2188 * @param c configuration to use
2191 run (void *cls, struct GNUNET_SERVER_Handle *server,
2192 const struct GNUNET_CONFIGURATION_Handle *c)
2195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
2198 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2200 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2201 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
2202 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0}
2205 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
2206 "ZONEKEY", &keyfile))
2208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2209 "No private key for root zone specified%s!\n", keyfile);
2210 GNUNET_SCHEDULER_shutdown(0);
2214 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2215 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
2217 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2219 GNUNET_free(keyfile);
2222 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
2225 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2226 "DNS hijacking enabled... connecting to service.\n");
2228 * Do gnunet dns init here
2230 dns_handle = GNUNET_DNS_connect(c,
2231 GNUNET_DNS_FLAG_PRE_RESOLUTION,
2232 &handle_dns_request, /* rh */
2233 NULL); /* Closure */
2234 if (NULL == dns_handle)
2236 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2237 "Failed to connect to the dnsservice!\n");
2244 * handle to our local namestore
2246 namestore_handle = GNUNET_NAMESTORE_connect(c);
2248 if (NULL == namestore_handle)
2250 //FIXME do error handling;
2251 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2252 "Failed to connect to the namestore!\n");
2253 GNUNET_SCHEDULER_shutdown(0);
2260 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
2262 if (NULL == dht_handle)
2264 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
2267 //put_some_records(); //FIXME for testing
2270 * Schedule periodic put
2272 * We have roughly an hour for all records;
2274 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
2276 //zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
2278 GNUNET_SERVER_add_handlers (server, handlers);
2281 //GNUNET_SERVER_disconnect_notify (server,
2282 // &client_disconnect_notification,
2285 nc = GNUNET_SERVER_notification_context_create (server, 1);
2287 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
2293 * The main function for the GNS service.
2295 * @param argc number of arguments from the command line
2296 * @param argv command line arguments
2297 * @return 0 ok, 1 on error
2300 main (int argc, char *const *argv)
2306 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
2311 /* end of gnunet-service-gns.c */