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);
276 * Reply to client with the result from our lookup.
278 * @param rh the request handle of the lookup
279 * @param rd_count the number of records to return
280 * @param rd the record data
283 reply_to_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
284 const struct GNUNET_NAMESTORE_RecordData *rd)
290 struct InterceptLookupHandle* ilh = (struct InterceptLookupHandle*)cls;
291 struct GNUNET_DNSPARSER_Packet *packet = ilh->packet;
292 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
293 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
294 packet->answers = answer_records;
295 packet->additional_records = additional_records;
298 * Put records in the DNS packet and modify it
301 len = sizeof(struct GNUNET_DNSPARSER_Record*);
302 for (i=0; i < rd_count; i++)
305 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
306 "Adding type %d to DNS response\n", rd[i].record_type);
307 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", rh->name);
308 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "QName: %s\n", ilh->query->name);
309 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
310 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
312 if (rd[i].record_type == ilh->query->type)
314 answer_records[i].name = ilh->query->name;
315 answer_records[i].type = rd[i].record_type;
316 answer_records[i].data.raw.data_len = rd[i].data_size;
317 answer_records[i].data.raw.data = (char*)rd[i].data;
318 answer_records[i].expiration_time = rd[i].expiration;
319 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
323 additional_records[i].name = ilh->query->name;
324 additional_records[i].type = rd[i].record_type;
325 additional_records[i].data.raw.data_len = rd[i].data_size;
326 additional_records[i].data.raw.data = (char*)rd[i].data;
327 additional_records[i].expiration_time = rd[i].expiration;
328 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
332 packet->num_answers = rh->answered;
333 packet->num_additional_records = rd_count-(rh->answered);
335 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
336 packet->flags.authoritative_answer = 1;
338 packet->flags.authoritative_answer = 0;
341 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
343 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
345 packet->flags.query_or_response = 1;
351 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
352 "Building DNS response\n");
353 ret = GNUNET_DNSPARSER_pack (packet,
354 1024, /* FIXME magic from dns redirector */
357 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
358 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
359 if (ret == GNUNET_OK)
361 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
362 "Answering DNS request\n");
363 GNUNET_DNS_request_answer(ilh->request_handle,
366 //GNUNET_free(answer);
367 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
371 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
372 "Error building DNS response! (ret=%d)", ret);
376 free_resolver_handle(rh);
377 GNUNET_free((struct RecordLookupHandle*)rh->proc_cls);
384 * Task run during shutdown.
390 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
392 /* Kill zone task for it may make the scheduler hang */
393 if (zone_update_taskid)
394 GNUNET_SCHEDULER_cancel(zone_update_taskid);
396 GNUNET_DNS_disconnect(dns_handle);
397 GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
398 GNUNET_DHT_disconnect(dht_handle);
402 * Callback when record data is put into namestore
404 * @param cls the closure
405 * @param success GNUNET_OK on success
406 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
409 on_namestore_record_put_result(void *cls,
413 if (GNUNET_NO == success)
415 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
418 else if (GNUNET_YES == success)
420 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
421 "records successfully put in namestore\n");
425 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
426 "Error putting records into namestore: %s\n", emsg);
430 * Handle timeout for DHT requests
432 * @param cls the request handle as closure
433 * @param tc the task context
436 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
438 struct GNUNET_GNS_ResolverHandle *rh = cls;
440 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
441 "dht lookup for query %s timed out.\n",
444 GNUNET_DHT_get_stop (rh->get_handle);
445 rh->proc(rh->proc_cls, rh, 0, NULL);
451 * Function called when we get a result from the dht
454 * @param cls the request handle
455 * @param exp lifetime
456 * @param key the key the record was stored under
457 * @param get_path get path
458 * @param get_path_length get path length
459 * @param put_path put path
460 * @param put_path_length put path length
461 * @param type the block type
462 * @param size the size of the record
463 * @param data the record data
466 process_record_dht_result(void* cls,
467 struct GNUNET_TIME_Absolute exp,
468 const GNUNET_HashCode * key,
469 const struct GNUNET_PeerIdentity *get_path,
470 unsigned int get_path_length,
471 const struct GNUNET_PeerIdentity *put_path,
472 unsigned int put_path_length,
473 enum GNUNET_BLOCK_Type type,
474 size_t size, const void *data)
476 struct GNUNET_GNS_ResolverHandle *rh;
477 struct RecordLookupHandle *rlh;
478 struct GNSNameRecordBlock *nrb;
479 uint32_t num_records;
481 char* rd_data = (char*)data;
485 GNUNET_HashCode zone, name_hash;
486 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
491 //FIXME maybe check expiration here, check block type
493 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
494 rlh = (struct RecordLookupHandle *) rh->proc_cls;
495 nrb = (struct GNSNameRecordBlock*)data;
497 /* stop lookup and timeout task */
498 GNUNET_DHT_get_stop (rh->get_handle);
499 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
501 rh->get_handle = NULL;
502 name = (char*)&nrb[1];
503 num_records = ntohl(nrb->rd_count);
505 struct GNUNET_NAMESTORE_RecordData rd[num_records];
507 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
508 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
510 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
515 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
519 for (i=0; i<num_records; i++)
521 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
522 "Got name: %s (wanted %s)\n", name, rh->name);
523 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
526 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
527 "Got data length: %d\n", rd[i].data_size);
528 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
529 "Got flag %d\n", rd[i].flags);
531 if ((strcmp(name, rh->name) == 0) &&
532 (rd[i].record_type == rlh->record_type))
539 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
540 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
543 * FIXME check pubkey against existing key in namestore?
544 * https://gnunet.org/bugs/view.php?id=2179
547 /* Save to namestore */
548 GNUNET_NAMESTORE_record_put (namestore_handle,
555 &on_namestore_record_put_result, //cont
559 rh->proc(rh->proc_cls, rh, num_records, rd);
561 rh->proc(rh->proc_cls, rh, 0, NULL);
568 * Start DHT lookup for a (name -> query->record_type) record in
569 * rh->authority's zone
571 * @param rh the pending gns query context
572 * @param name the name to query record
575 resolve_record_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
578 GNUNET_HashCode name_hash;
579 GNUNET_HashCode lookup_key;
580 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
581 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
583 GNUNET_CRYPTO_hash(rh->name, strlen(rh->name), &name_hash);
584 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
585 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
587 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
588 "starting dht lookup for %s with key: %s\n",
589 rh->name, (char*)&lookup_key_string);
591 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
592 &dht_lookup_timeout, rh);
594 xquery = htonl(rlh->record_type);
595 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
596 DHT_OPERATION_TIMEOUT,
597 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
599 DHT_GNS_REPLICATION_LEVEL,
603 &process_record_dht_result,
610 * Namestore calls this function if we have record for this name.
611 * (or with rd_count=0 to indicate no matches)
613 * @param cls the pending query
614 * @param key the key of the zone we did the lookup
615 * @param expiration expiration date of the namestore entry
616 * @param name the name for which we need an authority
617 * @param rd_count the number of records with 'name'
618 * @param rd the record data
619 * @param signature the signature of the authority for the record data
622 process_record_lookup_ns(void* cls,
623 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
624 struct GNUNET_TIME_Absolute expiration,
625 const char *name, unsigned int rd_count,
626 const struct GNUNET_NAMESTORE_RecordData *rd,
627 const struct GNUNET_CRYPTO_RsaSignature *signature)
629 struct GNUNET_GNS_ResolverHandle *rh;
630 struct RecordLookupHandle *rlh;
631 struct GNUNET_TIME_Relative remaining_time;
632 GNUNET_HashCode zone;
634 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
635 rlh = (struct RecordLookupHandle *)rh->proc_cls;
636 GNUNET_CRYPTO_hash(key,
637 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
639 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
645 rh->status |= EXISTS;
648 if (remaining_time.rel_value == 0)
650 rh->status |= EXPIRED;
656 * Lookup terminated and no results
658 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
659 "Namestore lookup for %s terminated without results\n", name);
663 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
664 "Record %s unknown in namestore\n",
667 * Our zone and no result? Cannot resolve TT
669 rh->proc(rh->proc_cls, rh, 0, NULL);
676 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
677 "Processing additional result %s from namestore\n", name);
679 for (i=0; i<rd_count;i++)
682 if (rd[i].record_type != rlh->record_type)
685 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
688 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
699 if (rh->answered == 0)
701 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
702 "No answers found. This is odd!\n");
703 rh->proc(rh->proc_cls, rh, 0, NULL);
707 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
710 rh->proc(rh->proc_cls, rh, rd_count, rd);
715 * The final phase of resolution.
716 * This is a name that is canonical and we do not have a delegation.
718 * @param rh the pending lookup
721 resolve_record_from_ns(struct GNUNET_GNS_ResolverHandle *rh)
723 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
725 * Try to resolve this record in our namestore.
726 * The name to resolve is now in rh->authority_name
727 * since we tried to resolve it to an authority
730 GNUNET_NAMESTORE_lookup_record(namestore_handle,
734 &process_record_lookup_ns,
741 * Handle timeout for DHT requests
743 * @param cls the request handle as closure
744 * @param tc the task context
747 dht_authority_lookup_timeout(void *cls,
748 const struct GNUNET_SCHEDULER_TaskContext *tc)
750 struct GNUNET_GNS_ResolverHandle *rh = cls;
752 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
753 "dht lookup for query %s timed out.\n",
756 GNUNET_DHT_get_stop (rh->get_handle);
757 if (strcmp(rh->name, "") == 0)
760 * promote authority back to name and try to resolve record
762 strcpy(rh->name, rh->authority_name);
764 rh->proc(rh->proc_cls, rh, 0, NULL);
768 static void resolve_delegation_from_dht(struct GNUNET_GNS_ResolverHandle *rh);
771 * Function called when we get a result from the dht
772 * for our query. Recursively tries to resolve PKEYs
775 * @param cls the request handle
776 * @param exp lifetime
777 * @param key the key the record was stored under
778 * @param get_path get path
779 * @param get_path_length get path length
780 * @param put_path put path
781 * @param put_path_length put path length
782 * @param type the block type
783 * @param size the size of the record
784 * @param data the record data
787 process_authority_dht_result(void* cls,
788 struct GNUNET_TIME_Absolute exp,
789 const GNUNET_HashCode * key,
790 const struct GNUNET_PeerIdentity *get_path,
791 unsigned int get_path_length,
792 const struct GNUNET_PeerIdentity *put_path,
793 unsigned int put_path_length,
794 enum GNUNET_BLOCK_Type type,
795 size_t size, const void *data)
797 struct GNUNET_GNS_ResolverHandle *rh;
798 struct GNSNameRecordBlock *nrb;
799 uint32_t num_records;
801 char* rd_data = (char*) data;
804 GNUNET_HashCode zone, name_hash;
809 //FIXME check expiration?
811 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
812 nrb = (struct GNSNameRecordBlock*)data;
814 /* stop dht lookup and timeout task */
815 GNUNET_DHT_get_stop (rh->get_handle);
816 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
818 rh->get_handle = NULL;
819 num_records = ntohl(nrb->rd_count);
820 name = (char*)&nrb[1];
822 struct GNUNET_NAMESTORE_RecordData rd[num_records];
824 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
825 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
827 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
832 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
836 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
837 "Got name: %s (wanted %s)\n", name, rh->authority_name);
838 for (i=0; i<num_records; i++)
841 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
842 "Got name: %s (wanted %s)\n", name, rh->authority_name);
843 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
844 "Got type: %d (wanted %d)\n",
845 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
846 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
847 "Got data length: %d\n", rd[i].data_size);
848 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
849 "Got flag %d\n", rd[i].flags);
851 if ((strcmp(name, rh->authority_name) == 0) &&
852 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
854 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
856 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
857 struct AuthorityChain *auth =
858 GNUNET_malloc(sizeof(struct AuthorityChain));
859 auth->zone = rh->authority;
860 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
861 memset(auth->name, 0, strlen(rh->authority_name)+1);
862 strcpy(auth->name, rh->authority_name);
863 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
864 rh->authority_chain_tail,
871 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
872 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
874 /* Save to namestore */
875 if (0 != GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
877 GNUNET_NAMESTORE_record_put (namestore_handle,
884 &on_namestore_record_put_result, //cont
893 if (strcmp(rh->name, "") == 0)
894 rh->proc(rh->proc_cls, rh, 0, NULL);
896 resolve_delegation_from_dht(rh);
901 * should never get here unless false dht key/put
902 * block plugin should handle this
905 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DHT authority lookup error!\n");
911 * Process DHT lookup result for record.
913 * @param cls the closure
914 * @param rh resolver handle
915 * @param rd_count number of results (always 0)
916 * @param rd record data (always NULL)
919 process_record_result_dht(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
920 unsigned int rd_count,
921 const struct GNUNET_NAMESTORE_RecordData *rd)
923 struct RecordLookupHandle* rlh;
924 rlh = (struct RecordLookupHandle*)cls;
927 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
928 "No records for %s found in DHT. Aborting\n",
930 /* give up, cannot resolve */
931 rlh->proc(rlh->proc_cls, rh, 0, NULL);
932 //reply_to_dns(NULL, rh, 0, NULL);
936 /* results found yay */
937 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
938 "Record resolved from namestore!");
939 rlh->proc(rlh->proc_cls, rh, rd_count, rd);
940 //reply_to_dns(NULL, rh, rd_count, rd);
946 * Process namestore lookup result for record.
948 * @param cls the closure
949 * @param rh resolver handle
950 * @param rd_count number of results (always 0)
951 * @param rd record data (always NULL)
954 process_record_result_ns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
955 unsigned int rd_count,
956 const struct GNUNET_NAMESTORE_RecordData *rd)
958 struct RecordLookupHandle* rlh;
959 rlh = (struct RecordLookupHandle*) cls;
962 /* ns entry expired. try dht */
963 if (rh->status & (EXPIRED | !EXISTS))
965 rh->proc = &process_record_result_dht;
966 resolve_record_from_dht(rh);
969 /* give up, cannot resolve */
970 rlh->proc(rlh->proc_cls, rh, 0, NULL);
971 //reply_to_dns(NULL, rh, 0, NULL);
975 /* results found yay */
976 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
977 "Record resolved from namestore!");
978 rlh->proc(rlh->proc_cls, rh, rd_count, rd);
979 //reply_to_dns(NULL, rh, rd_count, rd);
985 * Determine if this name is canonical.
987 * a.b.gnunet = not canonical
990 * @param name the name to test
991 * @return 1 if canonical
994 is_canonical(char* name)
996 uint32_t len = strlen(name);
999 for (i=0; i<len; i++)
1001 if (*(name+i) == '.')
1008 * Move one level up in the domain hierarchy and return the
1009 * passed top level domain.
1011 * @param name the domain
1012 * @param dest the destination where the tld will be put
1015 pop_tld(char* name, char* dest)
1019 if (is_canonical(name))
1026 for (len = strlen(name); len > 0; len--)
1028 if (*(name+len) == '.')
1038 strcpy(dest, (name+len+1));
1042 * DHT resolution for delegation finished. Processing result.
1044 * @param cls the closure
1045 * @param rh resolver handle
1046 * @param rd_count number of results (always 0)
1047 * @param rd record data (always NULL)
1050 process_dht_delegation_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1051 unsigned int rd_count,
1052 const struct GNUNET_NAMESTORE_RecordData *rd)
1054 struct RecordLookupHandle* rlh;
1055 rlh = (struct RecordLookupHandle*) cls;
1057 if (strcmp(rh->name, "") == 0)
1059 /* We resolved full name for delegation. resolving record */
1060 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1061 "Resolved full name for delegation via DHT. resolving record '' in ns\n");
1062 rh->proc = &process_record_result_ns;
1063 resolve_record_from_ns(rh);
1068 * we still have some left
1070 if (is_canonical(rh->name))
1072 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1073 "Resolving canonical record %s in ns\n", rh->name);
1074 rh->proc = &process_record_result_ns;
1075 resolve_record_from_ns(rh);
1078 /* give up, cannot resolve */
1079 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1080 "Cannot fully resolve delegation for %s via DHT!\n",
1082 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1083 //reply_to_dns(NULL, rh, 0, NULL);
1088 * Start DHT lookup for a name -> PKEY (compare NS) record in
1089 * rh->authority's zone
1091 * @param rh the pending gns query
1092 * @param name the name of the PKEY record
1095 resolve_delegation_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
1098 GNUNET_HashCode name_hash;
1099 GNUNET_HashCode lookup_key;
1101 GNUNET_CRYPTO_hash(rh->authority_name,
1102 strlen(rh->authority_name),
1104 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1106 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1107 &dht_authority_lookup_timeout,
1110 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1112 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1113 DHT_OPERATION_TIMEOUT,
1114 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1116 DHT_GNS_REPLICATION_LEVEL,
1120 &process_authority_dht_result,
1127 * Namestore resolution for delegation finished. Processing result.
1129 * @param cls the closure
1130 * @param rh resolver handle
1131 * @param rd_count number of results (always 0)
1132 * @param rd record data (always NULL)
1135 process_ns_delegation_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1136 unsigned int rd_count,
1137 const struct GNUNET_NAMESTORE_RecordData *rd)
1139 struct RecordLookupHandle* rlh;
1140 rlh = (struct RecordLookupHandle*) cls;
1142 if (strcmp(rh->name, "") == 0)
1144 /* We resolved full name for delegation. resolving record */
1145 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1146 "Resolved full name for delegation. resolving record ''\n");
1147 rh->proc = &process_record_result_ns;
1148 resolve_record_from_ns(rh);
1153 * we still have some left
1154 * check if ns entry is fresh
1156 if (rh->status & (EXISTS | !EXPIRED))
1158 if (is_canonical(rh->name))
1160 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1161 "Resolving canonical record %s\n", rh->name);
1162 rh->proc = &process_record_result_ns;
1163 resolve_record_from_ns(rh);
1167 /* give up, cannot resolve */
1168 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1169 "Cannot fully resolve delegation for %s!\n",
1171 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1172 //reply_to_dns(NULL, rh, 0, NULL);
1177 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1178 "Trying to resolve delegation for %s via DHT\n",
1180 rh->proc = &process_dht_delegation_dns;
1181 resolve_delegation_from_dht(rh);
1185 static void resolve_delegation_from_ns(struct GNUNET_GNS_ResolverHandle *rh);
1188 * This is a callback function that should give us only PKEY
1189 * records. Used to query the namestore for the authority (PKEY)
1190 * for 'name'. It will recursively try to resolve the
1191 * authority for a given name from the namestore.
1193 * @param cls the pending query
1194 * @param key the key of the zone we did the lookup
1195 * @param expiration expiration date of the record data set in the namestore
1196 * @param name the name for which we need an authority
1197 * @param rd_count the number of records with 'name'
1198 * @param rd the record data
1199 * @param signature the signature of the authority for the record data
1202 process_authority_lookup_ns(void* cls,
1203 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1204 struct GNUNET_TIME_Absolute expiration,
1206 unsigned int rd_count,
1207 const struct GNUNET_NAMESTORE_RecordData *rd,
1208 const struct GNUNET_CRYPTO_RsaSignature *signature)
1210 struct GNUNET_GNS_ResolverHandle *rh;
1211 struct GNUNET_TIME_Relative remaining_time;
1212 GNUNET_HashCode zone;
1215 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %d records from authority lookup\n",
1218 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
1219 GNUNET_CRYPTO_hash(key,
1220 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1222 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1228 rh->status |= EXISTS;
1231 if (remaining_time.rel_value == 0)
1233 rh->status |= EXPIRED;
1237 * No authority found in namestore.
1242 * We did not find an authority in the namestore
1247 * Promote this authority back to a name maybe it is
1250 if (strcmp(rh->name, "") == 0)
1252 /* simply promote back */
1253 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1254 "Promoting %s back to name\n", rh->authority_name);
1255 strcpy(rh->name, rh->authority_name);
1259 /* add back to existing name */
1260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1261 "Adding %s back to %s\n",
1262 rh->authority_name, rh->name);
1263 new_name = GNUNET_malloc(strlen(rh->name)
1264 + strlen(rh->authority_name) + 2);
1265 memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1266 strcpy(new_name, rh->name);
1267 strcpy(new_name+strlen(new_name)+1, ".");
1268 strcpy(new_name+strlen(new_name)+2, rh->authority_name);
1269 GNUNET_free(rh->name);
1270 rh->name = new_name;
1272 rh->proc(rh->proc_cls, rh, 0, NULL);
1276 //Note only 1 pkey should have been returned.. anything else would be strange
1278 * We found an authority that may be able to help us
1279 * move on with query
1282 for (i=0; i<rd_count;i++)
1285 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1288 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1291 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
1292 if (remaining_time.rel_value == 0)
1294 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1295 "This dht entry is expired.\n");
1296 rh->authority_chain_head->fresh = 0;
1297 rh->proc(rh->proc_cls, rh, 0, NULL);
1305 * Resolve rest of query with new authority
1307 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1308 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
1309 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1310 auth->zone = rh->authority;
1311 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
1312 memset(auth->name, 0, strlen(rh->authority_name)+1);
1313 strcpy(auth->name, rh->authority_name);
1314 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1315 rh->authority_chain_tail,
1319 * We are done with PKEY resolution if name is empty
1320 * else resolve again with new authority
1322 if (strcmp(rh->name, "") == 0)
1323 rh->proc(rh->proc_cls, rh, 0, NULL);
1325 resolve_delegation_from_ns(rh);
1332 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1333 "Authority lookup successful but no PKEY... never get here\n");
1334 rh->proc(rh->proc_cls, rh, 0, NULL);
1339 * Resolve the delegation chain for the request
1341 * @param rh the resolver handle
1344 resolve_delegation_from_ns(struct GNUNET_GNS_ResolverHandle *rh)
1347 pop_tld(rh->name, rh->authority_name);
1348 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1351 GNUNET_GNS_RECORD_PKEY,
1352 &process_authority_lookup_ns,
1358 * Entry point for name resolution
1359 * Setup a new query and try to resolve
1361 * @param request the request handle of the DNS request from a client
1362 * @param p the DNS query packet we received
1363 * @param q the DNS query we received parsed from p
1366 start_resolution_from_dns(struct GNUNET_DNS_RequestHandle *request,
1367 struct GNUNET_DNSPARSER_Packet *p,
1368 struct GNUNET_DNSPARSER_Query *q)
1370 struct GNUNET_GNS_ResolverHandle *rh;
1371 struct RecordLookupHandle* rlh;
1372 struct InterceptLookupHandle* ilh;
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1375 "Starting resolution for %s (type=%d)!\n",
1378 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
1379 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
1380 ilh = GNUNET_malloc(sizeof(struct InterceptLookupHandle));
1383 ilh->request_handle = request;
1385 rh->authority = zone_hash;
1387 rlh->record_type = q->type;
1388 rlh->name = q->name;
1389 rlh->proc = &reply_to_dns;
1390 rlh->proc_cls = ilh;
1394 rh->authority = zone_hash;
1395 rh->name = GNUNET_malloc(strlen(q->name)
1396 - strlen(gnunet_tld) + 1);
1398 strlen(q->name)-strlen(gnunet_tld) + 1);
1399 memcpy(rh->name, q->name,
1400 strlen(q->name)-strlen(gnunet_tld));
1402 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
1404 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
1405 rh->authority_chain_tail = rh->authority_chain_head;
1406 rh->authority_chain_head->zone = zone_hash;
1408 /* Start resolution in our zone */
1409 rh->proc = &process_ns_delegation_dns;
1410 resolve_delegation_from_ns(rh);
1416 * The DNS request handler
1417 * Called for every incoming DNS request.
1419 * @param cls closure
1420 * @param rh request handle to user for reply
1421 * @param request_length number of bytes in request
1422 * @param request udp payload of the DNS request
1425 handle_dns_request(void *cls,
1426 struct GNUNET_DNS_RequestHandle *rh,
1427 size_t request_length,
1428 const char *request)
1430 struct GNUNET_DNSPARSER_Packet *p;
1433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
1434 p = GNUNET_DNSPARSER_parse (request, request_length);
1438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1439 "Received malformed DNS packet, leaving it untouched\n");
1440 GNUNET_DNS_request_forward (rh);
1445 * Check tld and decide if we or
1446 * legacy dns is responsible
1448 * FIXME now in theory there could be more than 1 query in the request
1449 * but if this is case we get into trouble:
1450 * either we query the GNS or the DNS. We cannot do both!
1451 * So I suggest to either only allow a single query per request or
1452 * only allow GNS or DNS requests.
1453 * The way it is implemented here now is buggy and will lead to erratic
1454 * behaviour (if multiple queries are present).
1456 if (p->num_queries == 0)
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1459 "No Queries in DNS packet... forwarding\n");
1460 GNUNET_DNS_request_forward (rh);
1463 if (p->num_queries > 1)
1465 /* Note: We could also look for .gnunet */
1466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1467 ">1 queriy in DNS packet... odd. We only process #1\n");
1474 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
1476 while ((*tldoffset) != '.')
1479 if (0 == strcmp(tldoffset, gnunet_tld))
1481 start_resolution_from_dns(rh, p, p->queries);
1486 * This request does not concern us. Forward to real DNS.
1488 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1489 "Request for %s is forwarded to DNS\n", p->queries[0].name);
1490 GNUNET_DNS_request_forward (rh);
1496 * Method called periodicattluy that triggers
1497 * iteration over root zone
1499 * @param cls closure
1500 * @param tc task context
1503 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1505 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1509 * Continuation for DHT put
1511 * @param cls closure
1512 * @param tc task context
1515 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1517 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1522 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1525 * Function used to put all records successively into the DHT.
1527 * @param cls the closure (NULL)
1528 * @param key the public key of the authority (ours)
1529 * @param expiration lifetime of the namestore entry
1530 * @param name the name of the records
1531 * @param rd_count the number of records in data
1532 * @param rd the record data
1533 * @param signature the signature for the record data
1536 put_gns_record(void *cls,
1537 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1538 struct GNUNET_TIME_Absolute expiration,
1540 unsigned int rd_count,
1541 const struct GNUNET_NAMESTORE_RecordData *rd,
1542 const struct GNUNET_CRYPTO_RsaSignature *signature)
1545 struct GNSNameRecordBlock *nrb;
1546 GNUNET_HashCode name_hash;
1547 GNUNET_HashCode xor_hash;
1548 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1549 uint32_t rd_payload_length;
1550 char* nrb_data = NULL;
1555 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1556 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1557 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1562 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1563 "Putting records for %s into the DHT\n", name);
1565 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1567 nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1
1568 + sizeof(struct GNSNameRecordBlock));
1570 if (signature != NULL)
1571 nrb->signature = *signature;
1573 nrb->public_key = *key;
1575 nrb->rd_count = htonl(rd_count);
1577 memset(&nrb[1], 0, strlen(name) + 1);
1578 memcpy(&nrb[1], name, strlen(name));
1580 nrb_data = (char*)&nrb[1];
1581 nrb_data += strlen(name) + 1;
1583 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
1586 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
1591 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
1598 * calculate DHT key: H(name) xor H(pubkey)
1600 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1601 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1602 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1603 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1604 "putting records for %s under key: %s with size %d\n",
1605 name, (char*)&xor_hash_string, rd_payload_length);
1607 GNUNET_DHT_put (dht_handle, &xor_hash,
1608 DHT_GNS_REPLICATION_LEVEL,
1610 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1614 DHT_OPERATION_TIMEOUT,
1616 NULL); //cls for cont
1618 num_public_records++;
1621 * Reschedule periodic put
1623 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1624 &update_zone_dht_next,
1630 * Periodically iterate over our zone and store everything in dht
1633 * @param tc task context
1636 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1638 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1639 if (0 == num_public_records)
1641 dht_update_interval = GNUNET_TIME_relative_multiply(
1642 GNUNET_TIME_UNIT_SECONDS,
1647 dht_update_interval = GNUNET_TIME_relative_multiply(
1648 GNUNET_TIME_UNIT_SECONDS,
1649 (3600/num_public_records));
1651 num_public_records = 0; //start counting again
1652 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1654 GNUNET_NAMESTORE_RF_AUTHORITY,
1655 GNUNET_NAMESTORE_RF_PRIVATE,
1661 static void send_shorten_response(const char* name,
1662 struct ClientShortenHandle *csh);
1664 process_shorten_pseu_lookup_ns(void *cls,
1665 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1666 struct GNUNET_TIME_Absolute expire,
1668 unsigned int rd_len,
1669 const struct GNUNET_NAMESTORE_RecordData *rd,
1670 const struct GNUNET_CRYPTO_RsaSignature *signature)
1672 struct GNUNET_GNS_ResolverHandle *rh =
1673 (struct GNUNET_GNS_ResolverHandle *)cls;
1674 struct GNUNET_TIME_Relative remaining_time;
1681 rh->status |= EXISTS;
1684 if (remaining_time.rel_value == 0)
1686 rh->status |= EXPIRED;
1689 rh->proc(cls, rh, rd_len, rd);
1694 * Start DHT lookup for a PSEUdonym record in
1695 * rh->authority's zone
1697 * @param rh the pending gns query
1698 * @param name the name of the PKEY record
1701 resolve_pseu_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
1704 GNUNET_HashCode name_hash;
1705 GNUNET_HashCode lookup_key;
1708 GNUNET_CRYPTO_hash("",
1712 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1714 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1715 &dht_lookup_timeout,
1718 xquery = htonl(GNUNET_GNS_RECORD_PSEU);
1720 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1721 DHT_OPERATION_TIMEOUT,
1722 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1724 DHT_GNS_REPLICATION_LEVEL,
1728 &process_authority_dht_result,
1735 handle_shorten_pseu_ns_result(void* cls,
1736 struct GNUNET_GNS_ResolverHandle *rh,
1738 const struct GNUNET_NAMESTORE_RecordData *rd);
1741 handle_shorten_zone_to_name(void *cls,
1742 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1743 struct GNUNET_TIME_Absolute expire,
1745 unsigned int rd_len,
1746 const struct GNUNET_NAMESTORE_RecordData *rd,
1747 const struct GNUNET_CRYPTO_RsaSignature *signature)
1749 struct GNUNET_GNS_ResolverHandle *rh =
1750 (struct GNUNET_GNS_ResolverHandle *)cls;
1751 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) rh->proc_cls;
1755 /* we found a match in our own zone */
1758 result = GNUNET_malloc(strlen(rh->name) + strlen(name) + 1);
1759 memset(result, 0, strlen(rh->name) + strlen(name) + 1);
1760 memcpy(result, rh->name, strlen(rh->name));
1761 memcpy(result+strlen(rh->name)+1, name, strlen(name));
1763 send_shorten_response(result, csh);
1765 GNUNET_free(result);
1770 * Nothing in our zone
1771 * check PSEU for this authority in namestore
1773 rh->proc = &handle_shorten_pseu_ns_result;
1774 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1775 &rh->authority_chain_head->zone,
1777 GNUNET_GNS_RECORD_PSEU,
1778 &process_shorten_pseu_lookup_ns,
1784 * Process result from namestore delegation lookup
1785 * for shorten operation
1787 * @param cls the client shorten handle
1788 * @param rh the resolver handle
1789 * @param rd_count number of results (0)
1790 * @param rd data (NULL)
1793 handle_shorten_pseu_dht_result(void* cls,
1794 struct GNUNET_GNS_ResolverHandle *rh,
1796 const struct GNUNET_NAMESTORE_RecordData *rd)
1798 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1799 struct AuthorityChain *auth_chain;
1810 for (i=0; i < rd_len; i++)
1812 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1816 pseu = (char*) rd[i].data;
1817 result = GNUNET_malloc(strlen(rh->name) + strlen(pseu) + 1);
1818 memset(result, 0, strlen(rh->name) + strlen(pseu) + 1);
1819 memcpy(result, rh->name, strlen(rh->name));
1820 memcpy(result+strlen(rh->name)+1, pseu, strlen(pseu));
1822 send_shorten_response(result, csh);
1824 GNUNET_free(result);
1830 * continue with next authority
1833 auth_chain = rh->authority_chain_head;
1835 if ((auth_chain->next->next == NULL) &&
1836 GNUNET_CRYPTO_hash_cmp(&auth_chain->next->zone, &zone_hash) == 0)
1841 result = GNUNET_malloc(strlen(rh->name) + strlen(auth_chain->name) + 2);
1842 memset(result, 0, strlen(rh->name) + strlen(auth_chain->name) + 2);
1843 strcpy(result, rh->name);
1844 strcpy(result+strlen(rh->name)+1, ".");
1845 strcpy(result+strlen(rh->name)+2, auth_chain->name);
1846 send_shorten_response(result, csh);
1852 * Continue with next authority
1854 new_name = GNUNET_malloc(strlen(rh->name)+
1855 strlen(auth_chain->name) + 2);
1856 memset(new_name, 0, strlen(rh->name)+
1857 strlen(auth_chain->name) + 2);
1858 strcpy(new_name, rh->name);
1859 strcpy(new_name+strlen(rh->name)+1, ".");
1860 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1861 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1862 rh->authority_chain_tail,
1864 GNUNET_free(auth_chain->name);
1865 GNUNET_free(auth_chain);
1866 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1868 &rh->authority_chain_head->zone,
1869 &handle_shorten_zone_to_name,
1877 * Process result from namestore PSEU lookup
1878 * for shorten operation
1879 * FIXME do we need to check for own zone here?
1881 * @param cls the client shorten handle
1882 * @param rh the resolver handle
1883 * @param rd_count number of results (0 if none found)
1884 * @param rd data (NULL if none found)
1887 handle_shorten_pseu_ns_result(void* cls,
1888 struct GNUNET_GNS_ResolverHandle *rh,
1890 const struct GNUNET_NAMESTORE_RecordData *rd)
1892 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1893 struct AuthorityChain *auth_chain;
1904 for (i=0; i < rd_len; i++)
1906 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1910 pseu = (char*) rd[i].data;
1911 result = GNUNET_malloc(strlen(rh->name) + strlen(pseu) + 1);
1912 memset(result, 0, strlen(rh->name) + strlen(pseu) + 1);
1913 memcpy(result, rh->name, strlen(rh->name));
1914 memcpy(result+strlen(rh->name)+1, pseu, strlen(pseu));
1916 send_shorten_response(result, csh);
1918 GNUNET_free(result);
1923 * No PSEU found. Ask DHT if expired.
1924 * Else contunue with next authority
1926 if (rh->status & (EXISTS | !EXPIRED))
1931 auth_chain = rh->authority_chain_head;
1932 new_name = GNUNET_malloc(strlen(rh->name)+
1933 strlen(auth_chain->name) + 2);
1934 memset(new_name, 0, strlen(rh->name)+
1935 strlen(auth_chain->name) + 2);
1936 strcpy(new_name, rh->name);
1937 strcpy(new_name+strlen(rh->name)+1, ".");
1938 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1940 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1941 rh->authority_chain_tail,
1944 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1946 &rh->authority_chain_head->zone,
1947 &handle_shorten_zone_to_name,
1955 rh->authority = rh->authority_chain_head->zone;
1956 rh->proc = &handle_shorten_pseu_dht_result;
1957 resolve_pseu_from_dht(rh);
1964 * Process result from namestore delegation lookup
1965 * for shorten operation
1967 * @param cls the client shorten handle
1968 * @param rh the resolver handle
1969 * @param rd_count number of results (0)
1970 * @param rd data (NULL)
1973 handle_shorten_delegation_result(void* cls,
1974 struct GNUNET_GNS_ResolverHandle *rh,
1976 const struct GNUNET_NAMESTORE_RecordData *rd)
1978 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1979 struct AuthorityChain *auth_chain;
1982 * At this point rh->name contains the part of the name
1983 * that we do not have a PKEY in our namestore to resolve.
1984 * The authority chain in the resolver handle is now
1985 * useful to backtrack if needed
1988 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1989 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
1991 if (GNUNET_CRYPTO_hash_cmp(&rh->authority_chain_head->zone,
1995 * This is our zone append .gnunet unless name is empty
1996 * (it shouldn't be, usually FIXME what happens if we
1997 * shorten to our zone to a "" record??)
1999 send_shorten_response(rh->name, csh); //FIXME +.gnunet!
2000 free_resolver_handle(rh);
2001 GNUNET_free(csh->name);
2006 auth_chain = rh->authority_chain_head;
2007 /* backtrack authorities for pseu */
2008 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2011 &handle_shorten_zone_to_name,
2016 typedef void (*ShortenResponseProc) (void* cls, const char* name);
2019 * Shorten a given name
2021 * @param name the name to shorten
2022 * @param proc the processor to call when finished
2023 * @praram cls the closure to the processor
2026 shorten_name(char* name, struct ClientShortenHandle* csh)
2029 struct GNUNET_GNS_ResolverHandle *rh;
2031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2032 "Starting resolution for %s (type=%d)!\n",
2033 name, GNUNET_GNS_RECORD_PKEY);
2035 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
2036 rh->authority = zone_hash;
2038 rh->name = GNUNET_malloc(strlen(name)
2039 - strlen(gnunet_tld) + 1);
2041 strlen(name)-strlen(gnunet_tld) + 1);
2042 memcpy(rh->name, name,
2043 strlen(name)-strlen(gnunet_tld));
2045 csh->name = GNUNET_malloc(strlen(name)
2046 - strlen(gnunet_tld) + 1);
2048 strlen(name)-strlen(gnunet_tld) + 1);
2049 memcpy(rh->name, name,
2050 strlen(name)-strlen(gnunet_tld));
2052 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
2054 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2055 rh->authority_chain_tail = rh->authority_chain_head;
2056 rh->authority_chain_head->zone = zone_hash;
2057 rh->proc = &handle_shorten_delegation_result;
2058 rh->proc_cls = (void*)csh;
2060 /* Start delegation resolution in our namestore */
2061 resolve_delegation_from_ns(rh);
2066 * Send shorten response back to client
2067 * FIXME this is without .gnunet!
2069 * @param cls the client handle in closure
2070 * @param name the shortened name result or NULL if cannot be shortened
2073 send_shorten_response(const char* name, struct ClientShortenHandle *csh)
2075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n",
2077 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
2084 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage *)
2087 rmsg->unique_id = csh->unique_id;
2088 rmsg->key = csh->key;
2089 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
2091 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
2094 strcpy((char*)&rmsg[1], name);
2096 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
2097 (const struct GNUNET_MessageHeader *) rmsg,
2100 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
2107 * Handle a shorten message from the api
2109 * @param cls the closure
2110 * @param client the client
2111 * @param message the message
2113 static void handle_shorten(void *cls,
2114 struct GNUNET_SERVER_Client * client,
2115 const struct GNUNET_MessageHeader * message)
2117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
2119 size_t msg_size = 0;
2120 struct ClientShortenHandle *csh;
2122 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
2124 GNUNET_break_op (0);
2125 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2129 GNUNET_SERVER_notification_context_add (nc, client);
2130 GNUNET_SERVER_client_keep (client);
2132 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
2133 (struct GNUNET_GNS_ClientShortenMessage *) message;
2135 msg_size = ntohs(message->size);
2137 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2139 GNUNET_break_op (0);
2140 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2144 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
2145 csh->client = client;
2146 csh->unique_id = sh_msg->unique_id;
2147 csh->key = sh_msg->key;
2149 shorten_name((char*)&sh_msg[1], csh);
2157 handle_lookup(void *cls,
2158 struct GNUNET_SERVER_Client * client,
2159 const struct GNUNET_MessageHeader * message)
2164 * Process GNS requests.
2166 * @param cls closure)
2167 * @param server the initialized server
2168 * @param c configuration to use
2171 run (void *cls, struct GNUNET_SERVER_Handle *server,
2172 const struct GNUNET_CONFIGURATION_Handle *c)
2175 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
2178 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2180 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2181 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
2182 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0}
2185 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
2186 "ZONEKEY", &keyfile))
2188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2189 "No private key for root zone specified%s!\n", keyfile);
2190 GNUNET_SCHEDULER_shutdown(0);
2194 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2195 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
2197 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2202 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
2205 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2206 "DNS hijacking enabled... connecting to service.\n");
2208 * Do gnunet dns init here
2210 dns_handle = GNUNET_DNS_connect(c,
2211 GNUNET_DNS_FLAG_PRE_RESOLUTION,
2212 &handle_dns_request, /* rh */
2213 NULL); /* Closure */
2214 if (NULL == dns_handle)
2216 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2217 "Failed to connect to the dnsservice!\n");
2224 * handle to our local namestore
2226 namestore_handle = GNUNET_NAMESTORE_connect(c);
2228 if (NULL == namestore_handle)
2230 //FIXME do error handling;
2231 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2232 "Failed to connect to the namestore!\n");
2233 GNUNET_SCHEDULER_shutdown(0);
2240 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
2242 if (NULL == dht_handle)
2244 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
2247 //put_some_records(); //FIXME for testing
2250 * Schedule periodic put
2252 * We have roughly an hour for all records;
2254 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
2256 //zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
2258 GNUNET_SERVER_add_handlers (server, handlers);
2261 //GNUNET_SERVER_disconnect_notify (server,
2262 // &client_disconnect_notification,
2265 nc = GNUNET_SERVER_notification_context_create (server, 1);
2267 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
2273 * The main function for the GNS service.
2275 * @param argc number of arguments from the command line
2276 * @param argv command line arguments
2277 * @return 0 ok, 1 on error
2280 main (int argc, char *const *argv)
2286 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
2291 /* end of gnunet-service-gns.c */