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 /* handle to a resolution process */
70 struct GNUNET_GNS_ResolverHandle;
73 * processor for a resultion result
75 * @param cls the closure
76 * @param rh the resolution handle
77 * @param rd_count number of results
78 * @pram rd resukt data
80 typedef void (*ResolutionResultProcessor) (void *cls,
81 struct GNUNET_GNS_ResolverHandle *rh,
83 const struct GNUNET_NAMESTORE_RecordData *rd);
86 * Resoltion status indicator
87 * EXISTS: the name to lookup exists
88 * EXPIRED: the name in the record expired
97 * Handle to a currenty pending resolution
99 struct GNUNET_GNS_ResolverHandle
101 /* The name to resolve */
104 /* has this query been answered? how many matches */
107 /* the authoritative zone to query */
108 GNUNET_HashCode authority;
110 /* the name of the authoritative zone to query */
111 char *authority_name;
114 * we have an authority in namestore that
115 * may be able to resolve
119 /* a handle for dht lookups. should be NULL if no lookups are in progress */
120 struct GNUNET_DHT_GetHandle *get_handle;
122 /* timeout task for dht lookups */
123 GNUNET_SCHEDULER_TaskIdentifier dht_timeout_task;
125 /* called when resolution phase finishes */
126 ResolutionResultProcessor proc;
128 /* closure passed to proc */
131 /* DLL to store the authority chain */
132 struct AuthorityChain *authority_chain_head;
134 /* DLL to store the authority chain */
135 struct AuthorityChain *authority_chain_tail;
137 /* status of the resolution result */
138 enum ResolutionStatus status;
143 * Handle to a record lookup
145 struct RecordLookupHandle
147 /* the record type to look up */
148 enum GNUNET_GNS_RecordType record_type;
150 /* the name to look up */
153 /* Method to call on record resolution result */
154 ResolutionResultProcessor proc;
156 /* closure to pass to proc */
162 * Handle to a shorten operation from api
164 struct ClientShortenHandle
166 /* the requesting client that */
167 struct GNUNET_SERVER_Client *client;
173 enum GNUNET_GNS_RecordType type;
175 /* name to shorten */
181 * Handle to a lookup operation from api
183 struct ClientLookupHandle
185 /* the requesting client that */
186 struct GNUNET_SERVER_Client *client;
192 enum GNUNET_GNS_RecordType type;
194 /* the name to look up */
195 char* name; //Needed?
199 * Handle to a DNS intercepted
202 struct InterceptLookupHandle
204 /* the request handle to reply to */
205 struct GNUNET_DNS_RequestHandle *request_handle;
207 /* the dns parser packet received */
208 struct GNUNET_DNSPARSER_Packet *packet;
210 /* the query parsed from the packet */
211 struct GNUNET_DNSPARSER_Query *query;
216 * Our handle to the DNS handler library
218 struct GNUNET_DNS_Handle *dns_handle;
221 * Our handle to the DHT
223 struct GNUNET_DHT_Handle *dht_handle;
226 * Our zone's private key
228 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
231 * Our handle to the namestore service
232 * FIXME maybe need a second handle for iteration
234 struct GNUNET_NAMESTORE_Handle *namestore_handle;
237 * Handle to iterate over our authoritative zone in namestore
239 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
242 * The configuration the GNS service is running with
244 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
247 * Our notification context.
249 static struct GNUNET_SERVER_NotificationContext *nc;
254 GNUNET_HashCode zone_hash;
257 * Our tld. Maybe get from config file
259 const char* gnunet_tld = ".gnunet";
262 * Useful for zone update for DHT put
264 static int num_public_records = 3600;
266 /* dht update interval FIXME define? */
267 static struct GNUNET_TIME_Relative dht_update_interval;
269 /* zone update task */
270 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
273 * Helper function to free resolver handle
275 * @rh the handle to free
278 free_resolver_handle(struct GNUNET_GNS_ResolverHandle* rh)
280 struct AuthorityChain *ac;
281 struct AuthorityChain *ac_next;
286 GNUNET_free_non_null (rh->name);
287 GNUNET_free_non_null (rh->authority_name);
289 ac = rh->authority_chain_head;
294 GNUNET_free_non_null (ac->name);
303 * Reply to dns request with the result from our lookup.
305 * @param cls the closure to the request (an InterceptLookupHandle)
306 * @param rh the request handle of the lookup
307 * @param rd_count the number of records to return
308 * @param rd the record data
311 reply_to_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
312 const struct GNUNET_NAMESTORE_RecordData *rd)
318 struct InterceptLookupHandle* ilh = (struct InterceptLookupHandle*)cls;
319 struct GNUNET_DNSPARSER_Packet *packet = ilh->packet;
320 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
321 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
322 packet->answers = answer_records;
323 packet->additional_records = additional_records;
326 * Put records in the DNS packet and modify it
329 len = sizeof(struct GNUNET_DNSPARSER_Record*);
330 for (i=0; i < rd_count; i++)
333 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
334 "Adding type %d to DNS response\n", rd[i].record_type);
335 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", rh->name);
336 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "QName: %s\n", ilh->query->name);
337 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
338 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
340 if (rd[i].record_type == ilh->query->type)
342 answer_records[i].name = ilh->query->name;
343 answer_records[i].type = rd[i].record_type;
344 answer_records[i].data.raw.data_len = rd[i].data_size;
345 answer_records[i].data.raw.data = (char*)rd[i].data;
346 answer_records[i].expiration_time = rd[i].expiration;
347 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
351 additional_records[i].name = ilh->query->name;
352 additional_records[i].type = rd[i].record_type;
353 additional_records[i].data.raw.data_len = rd[i].data_size;
354 additional_records[i].data.raw.data = (char*)rd[i].data;
355 additional_records[i].expiration_time = rd[i].expiration;
356 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
360 packet->num_answers = rh->answered;
361 packet->num_additional_records = rd_count-(rh->answered);
363 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
364 packet->flags.authoritative_answer = 1;
366 packet->flags.authoritative_answer = 0;
369 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
371 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
373 packet->flags.query_or_response = 1;
379 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
380 "Building DNS response\n");
381 ret = GNUNET_DNSPARSER_pack (packet,
382 1024, /* FIXME magic from dns redirector */
385 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
386 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
387 if (ret == GNUNET_OK)
389 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
390 "Answering DNS request\n");
391 GNUNET_DNS_request_answer(ilh->request_handle,
396 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
400 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
401 "Error building DNS response! (ret=%d)", ret);
404 packet->num_answers = 0;
405 packet->answers = NULL;
406 packet->num_additional_records = 0;
407 packet->additional_records = NULL;
408 GNUNET_DNSPARSER_free_packet(packet);
410 GNUNET_free((struct RecordLookupHandle*)rh->proc_cls);
411 free_resolver_handle(rh);
417 * Task run during shutdown.
423 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
426 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
428 /* Kill zone task for it may make the scheduler hang */
429 if (zone_update_taskid)
430 GNUNET_SCHEDULER_cancel(zone_update_taskid);
432 GNUNET_SERVER_notification_context_destroy (nc);
435 GNUNET_DNS_disconnect(dns_handle);
437 GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
438 GNUNET_DHT_disconnect(dht_handle);
443 * Callback when record data is put into namestore
445 * @param cls the closure
446 * @param success GNUNET_OK on success
447 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
450 on_namestore_record_put_result(void *cls,
454 if (GNUNET_NO == success)
456 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
459 else if (GNUNET_YES == success)
461 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
462 "records successfully put in namestore\n");
466 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
467 "Error putting records into namestore: %s\n", emsg);
472 * Handle timeout for DHT requests
474 * @param cls the request handle as closure
475 * @param tc the task context
478 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
480 struct GNUNET_GNS_ResolverHandle *rh = cls;
482 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
483 "dht lookup for query %s timed out.\n",
486 GNUNET_DHT_get_stop (rh->get_handle);
487 rh->proc(rh->proc_cls, rh, 0, NULL);
492 * Function called when we get a result from the dht
493 * for our record query
495 * @param cls the request handle
496 * @param exp lifetime
497 * @param key the key the record was stored under
498 * @param get_path get path
499 * @param get_path_length get path length
500 * @param put_path put path
501 * @param put_path_length put path length
502 * @param type the block type
503 * @param size the size of the record
504 * @param data the record data
507 process_record_dht_result(void* cls,
508 struct GNUNET_TIME_Absolute exp,
509 const GNUNET_HashCode * key,
510 const struct GNUNET_PeerIdentity *get_path,
511 unsigned int get_path_length,
512 const struct GNUNET_PeerIdentity *put_path,
513 unsigned int put_path_length,
514 enum GNUNET_BLOCK_Type type,
515 size_t size, const void *data)
517 struct GNUNET_GNS_ResolverHandle *rh;
518 struct RecordLookupHandle *rlh;
519 struct GNSNameRecordBlock *nrb;
520 uint32_t num_records;
522 char* rd_data = (char*)data;
526 GNUNET_HashCode zone, name_hash;
527 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
532 //FIXME maybe check expiration here, check block type
534 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
535 rlh = (struct RecordLookupHandle *) rh->proc_cls;
536 nrb = (struct GNSNameRecordBlock*)data;
538 /* stop lookup and timeout task */
539 GNUNET_DHT_get_stop (rh->get_handle);
540 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
542 rh->get_handle = NULL;
543 name = (char*)&nrb[1];
544 num_records = ntohl(nrb->rd_count);
546 struct GNUNET_NAMESTORE_RecordData rd[num_records];
548 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
549 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
551 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
556 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
560 for (i=0; i<num_records; i++)
562 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
563 "Got name: %s (wanted %s)\n", name, rh->name);
564 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
567 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
568 "Got data length: %d\n", rd[i].data_size);
569 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
570 "Got flag %d\n", rd[i].flags);
572 if ((strcmp(name, rh->name) == 0) &&
573 (rd[i].record_type == rlh->record_type))
580 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
581 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
584 * FIXME check pubkey against existing key in namestore?
585 * https://gnunet.org/bugs/view.php?id=2179
588 /* Save to namestore */
589 GNUNET_NAMESTORE_record_put (namestore_handle,
596 &on_namestore_record_put_result, //cont
600 rh->proc(rh->proc_cls, rh, num_records, rd);
602 rh->proc(rh->proc_cls, rh, 0, NULL);
609 * Start DHT lookup for a (name -> query->record_type) record in
610 * rh->authority's zone
612 * @param rh the pending gns query context
615 resolve_record_dht(struct GNUNET_GNS_ResolverHandle *rh)
618 GNUNET_HashCode name_hash;
619 GNUNET_HashCode lookup_key;
620 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
621 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
623 GNUNET_CRYPTO_hash(rh->name, strlen(rh->name), &name_hash);
624 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
625 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
627 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
628 "starting dht lookup for %s with key: %s\n",
629 rh->name, (char*)&lookup_key_string);
631 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
632 &dht_lookup_timeout, rh);
634 xquery = htonl(rlh->record_type);
635 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
636 DHT_OPERATION_TIMEOUT,
637 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
639 DHT_GNS_REPLICATION_LEVEL,
643 &process_record_dht_result,
650 * Namestore calls this function if we have record for this name.
651 * (or with rd_count=0 to indicate no matches)
653 * @param cls the pending query
654 * @param key the key of the zone we did the lookup
655 * @param expiration expiration date of the namestore entry
656 * @param name the name for which we need an authority
657 * @param rd_count the number of records with 'name'
658 * @param rd the record data
659 * @param signature the signature of the authority for the record data
662 process_record_lookup_ns(void* cls,
663 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
664 struct GNUNET_TIME_Absolute expiration,
665 const char *name, unsigned int rd_count,
666 const struct GNUNET_NAMESTORE_RecordData *rd,
667 const struct GNUNET_CRYPTO_RsaSignature *signature)
669 struct GNUNET_GNS_ResolverHandle *rh;
670 struct RecordLookupHandle *rlh;
671 struct GNUNET_TIME_Relative remaining_time;
672 GNUNET_HashCode zone;
674 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
675 rlh = (struct RecordLookupHandle *)rh->proc_cls;
676 GNUNET_CRYPTO_hash(key,
677 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
679 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
685 rh->status |= EXISTS;
688 if (remaining_time.rel_value == 0)
690 rh->status |= EXPIRED;
696 * Lookup terminated and no results
698 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
699 "Namestore lookup for %s terminated without results\n", name);
701 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
702 "Record %s unknown in namestore\n",
705 * Our zone and no result? Cannot resolve TT
707 rh->proc(rh->proc_cls, rh, 0, NULL);
714 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
715 "Processing additional result %s from namestore\n", name);
717 for (i=0; i<rd_count;i++)
720 if (rd[i].record_type != rlh->record_type)
723 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
726 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
737 if (rh->answered == 0)
739 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
740 "No answers found. This is odd!\n");
741 rh->proc(rh->proc_cls, rh, 0, NULL);
745 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
748 rh->proc(rh->proc_cls, rh, rd_count, rd);
754 * The final phase of resolution.
755 * rh->name is a name that is canonical and we do not have a delegation.
756 * Query namestore for this record
758 * @param rh the pending lookup
761 resolve_record_ns(struct GNUNET_GNS_ResolverHandle *rh)
763 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
766 * Try to resolve this record in our namestore.
767 * The name to resolve is now in rh->authority_name
768 * since we tried to resolve it to an authority
771 GNUNET_NAMESTORE_lookup_record(namestore_handle,
775 &process_record_lookup_ns,
781 * Handle timeout for DHT requests
783 * @param cls the request handle as closure
784 * @param tc the task context
787 dht_authority_lookup_timeout(void *cls,
788 const struct GNUNET_SCHEDULER_TaskContext *tc)
790 struct GNUNET_GNS_ResolverHandle *rh = cls;
792 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
793 "dht lookup for query %s timed out.\n",
796 GNUNET_DHT_get_stop (rh->get_handle);
797 if (strcmp(rh->name, "") == 0)
800 * promote authority back to name and try to resolve record
802 strcpy(rh->name, rh->authority_name);
804 rh->proc(rh->proc_cls, rh, 0, NULL);
808 static void resolve_delegation_dht(struct GNUNET_GNS_ResolverHandle *rh);
811 * Function called when we get a result from the dht
812 * for our query. Recursively tries to resolve authorities
815 * @param cls the request handle
816 * @param exp lifetime
817 * @param key the key the record was stored under
818 * @param get_path get path
819 * @param get_path_length get path length
820 * @param put_path put path
821 * @param put_path_length put path length
822 * @param type the block type
823 * @param size the size of the record
824 * @param data the record data
827 process_delegation_result_dht(void* cls,
828 struct GNUNET_TIME_Absolute exp,
829 const GNUNET_HashCode * key,
830 const struct GNUNET_PeerIdentity *get_path,
831 unsigned int get_path_length,
832 const struct GNUNET_PeerIdentity *put_path,
833 unsigned int put_path_length,
834 enum GNUNET_BLOCK_Type type,
835 size_t size, const void *data)
837 struct GNUNET_GNS_ResolverHandle *rh;
838 struct GNSNameRecordBlock *nrb;
839 uint32_t num_records;
841 char* rd_data = (char*) data;
844 GNUNET_HashCode zone, name_hash;
849 //FIXME check expiration?
851 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
852 nrb = (struct GNSNameRecordBlock*)data;
854 /* stop dht lookup and timeout task */
855 GNUNET_DHT_get_stop (rh->get_handle);
856 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
858 rh->get_handle = NULL;
859 num_records = ntohl(nrb->rd_count);
860 name = (char*)&nrb[1];
862 struct GNUNET_NAMESTORE_RecordData rd[num_records];
864 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
865 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
867 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
872 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
876 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
877 "Got name: %s (wanted %s)\n", name, rh->authority_name);
878 for (i=0; i<num_records; i++)
881 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
882 "Got name: %s (wanted %s)\n", name, rh->authority_name);
883 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
884 "Got type: %d (wanted %d)\n",
885 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
886 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
887 "Got data length: %d\n", rd[i].data_size);
888 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
889 "Got flag %d\n", rd[i].flags);
891 if ((strcmp(name, rh->authority_name) == 0) &&
892 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
894 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
896 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
897 struct AuthorityChain *auth =
898 GNUNET_malloc(sizeof(struct AuthorityChain));
899 auth->zone = rh->authority;
900 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
901 memset(auth->name, 0, strlen(rh->authority_name)+1);
902 strcpy(auth->name, rh->authority_name);
903 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
904 rh->authority_chain_tail,
911 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
912 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
914 /* Save to namestore */
915 if (0 != GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
917 GNUNET_NAMESTORE_record_put (namestore_handle,
924 &on_namestore_record_put_result, //cont
934 * FIXME in this case. should we ask namestore again?
936 if (strcmp(rh->name, "") == 0)
937 rh->proc(rh->proc_cls, rh, 0, NULL);
939 resolve_delegation_dht(rh);
944 * should never get here unless false dht key/put
945 * block plugin should handle this
947 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DHT authority lookup error!\n");
953 * Process DHT lookup result for record.
955 * @param cls the closure
956 * @param rh resolver handle
957 * @param rd_count number of results (always 0)
958 * @param rd record data (always NULL)
961 process_record_result_dht(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
962 unsigned int rd_count,
963 const struct GNUNET_NAMESTORE_RecordData *rd)
965 struct RecordLookupHandle* rlh;
966 rlh = (struct RecordLookupHandle*)cls;
969 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
970 "No records for %s found in DHT. Aborting\n",
972 /* give up, cannot resolve */
973 rlh->proc(rlh->proc_cls, rh, 0, NULL);
977 /* results found yay */
978 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
979 "Record resolved from namestore!");
980 rlh->proc(rlh->proc_cls, rh, rd_count, rd);
986 * Process namestore lookup result for record.
988 * @param cls the closure
989 * @param rh resolver handle
990 * @param rd_count number of results (always 0)
991 * @param rd record data (always NULL)
994 process_record_result_ns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
995 unsigned int rd_count,
996 const struct GNUNET_NAMESTORE_RecordData *rd)
998 struct RecordLookupHandle* rlh;
999 rlh = (struct RecordLookupHandle*) cls;
1002 /* ns entry expired. try dht */
1003 if (rh->status & (EXPIRED | !EXISTS))
1005 rh->proc = &process_record_result_dht;
1006 resolve_record_dht(rh);
1009 /* give up, cannot resolve */
1010 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1014 /* results found yay */
1015 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1016 "Record resolved from namestore!");
1017 rlh->proc(rlh->proc_cls, rh, rd_count, rd);
1023 * Determine if this name is canonical.
1025 * a.b.gnunet = not canonical
1028 * @param name the name to test
1029 * @return 1 if canonical
1032 is_canonical(char* name)
1034 uint32_t len = strlen(name);
1037 for (i=0; i<len; i++)
1039 if (*(name+i) == '.')
1046 * Move one level up in the domain hierarchy and return the
1047 * passed top level domain.
1049 * @param name the domain
1050 * @param dest the destination where the tld will be put
1053 pop_tld(char* name, char* dest)
1057 if (is_canonical(name))
1064 for (len = strlen(name); len > 0; len--)
1066 if (*(name+len) == '.')
1076 strcpy(dest, (name+len+1));
1080 * DHT resolution for delegation finished. Processing result.
1082 * @param cls the closure
1083 * @param rh resolver handle
1084 * @param rd_count number of results (always 0)
1085 * @param rd record data (always NULL)
1088 process_delegation_dht(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1089 unsigned int rd_count,
1090 const struct GNUNET_NAMESTORE_RecordData *rd)
1092 struct RecordLookupHandle* rlh;
1093 rlh = (struct RecordLookupHandle*) cls;
1095 if (strcmp(rh->name, "") == 0)
1097 /* We resolved full name for delegation. resolving record */
1098 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1099 "Resolved full name for delegation via DHT. resolving record '' in ns\n");
1100 rh->proc = &process_record_result_ns;
1101 resolve_record_ns(rh);
1106 * we still have some left
1108 if (is_canonical(rh->name))
1110 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1111 "Resolving canonical record %s in ns\n", rh->name);
1112 rh->proc = &process_record_result_ns;
1113 resolve_record_ns(rh);
1116 /* give up, cannot resolve */
1117 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1118 "Cannot fully resolve delegation for %s via DHT!\n",
1120 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1125 * Start DHT lookup for a name -> PKEY (compare NS) record in
1126 * rh->authority's zone
1128 * @param rh the pending gns query
1129 * @param name the name of the PKEY record
1132 resolve_delegation_dht(struct GNUNET_GNS_ResolverHandle *rh)
1135 GNUNET_HashCode name_hash;
1136 GNUNET_HashCode lookup_key;
1138 GNUNET_CRYPTO_hash(rh->authority_name,
1139 strlen(rh->authority_name),
1141 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1143 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1144 &dht_authority_lookup_timeout,
1147 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1149 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1150 DHT_OPERATION_TIMEOUT,
1151 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1153 DHT_GNS_REPLICATION_LEVEL,
1157 &process_delegation_result_dht,
1164 * Namestore resolution for delegation finished. Processing result.
1166 * @param cls the closure
1167 * @param rh resolver handle
1168 * @param rd_count number of results (always 0)
1169 * @param rd record data (always NULL)
1172 process_delegation_ns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1173 unsigned int rd_count,
1174 const struct GNUNET_NAMESTORE_RecordData *rd)
1176 struct RecordLookupHandle* rlh;
1177 rlh = (struct RecordLookupHandle*) cls;
1179 if (strcmp(rh->name, "") == 0)
1181 /* We resolved full name for delegation. resolving record */
1182 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1183 "Resolved full name for delegation. resolving record ''\n");
1184 rh->proc = &process_record_result_ns;
1185 resolve_record_ns(rh);
1190 * we still have some left
1191 * check if ns entry is fresh
1193 if (rh->status & (EXISTS | !EXPIRED))
1195 if (is_canonical(rh->name))
1197 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1198 "Resolving canonical record %s\n", rh->name);
1199 rh->proc = &process_record_result_ns;
1200 resolve_record_ns(rh);
1204 /* give up, cannot resolve */
1205 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1206 "Cannot fully resolve delegation for %s!\n",
1208 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1213 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1214 "Trying to resolve delegation for %s via DHT\n",
1216 rh->proc = &process_delegation_dht;
1217 resolve_delegation_dht(rh);
1221 static void resolve_delegation_ns(struct GNUNET_GNS_ResolverHandle *rh);
1224 * This is a callback function that should give us only PKEY
1225 * records. Used to query the namestore for the authority (PKEY)
1226 * for 'name'. It will recursively try to resolve the
1227 * authority for a given name from the namestore.
1229 * @param cls the pending query
1230 * @param key the key of the zone we did the lookup
1231 * @param expiration expiration date of the record data set in the namestore
1232 * @param name the name for which we need an authority
1233 * @param rd_count the number of records with 'name'
1234 * @param rd the record data
1235 * @param signature the signature of the authority for the record data
1238 process_delegation_result_ns(void* cls,
1239 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1240 struct GNUNET_TIME_Absolute expiration,
1242 unsigned int rd_count,
1243 const struct GNUNET_NAMESTORE_RecordData *rd,
1244 const struct GNUNET_CRYPTO_RsaSignature *signature)
1246 struct GNUNET_GNS_ResolverHandle *rh;
1247 struct GNUNET_TIME_Relative remaining_time;
1248 GNUNET_HashCode zone;
1251 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %d records from authority lookup\n",
1254 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
1255 GNUNET_CRYPTO_hash(key,
1256 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1258 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1264 rh->status |= EXISTS;
1267 if (remaining_time.rel_value == 0)
1269 rh->status |= EXPIRED;
1273 * No authority found in namestore.
1278 * We did not find an authority in the namestore
1283 * Promote this authority back to a name maybe it is
1286 if (strcmp(rh->name, "") == 0)
1288 /* simply promote back */
1289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1290 "Promoting %s back to name\n", rh->authority_name);
1291 strcpy(rh->name, rh->authority_name);
1295 /* add back to existing name */
1296 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1297 "Adding %s back to %s\n",
1298 rh->authority_name, rh->name);
1299 new_name = GNUNET_malloc(strlen(rh->name)
1300 + strlen(rh->authority_name) + 2);
1301 memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1302 strcpy(new_name, rh->name);
1303 strcpy(new_name+strlen(new_name)+1, ".");
1304 strcpy(new_name+strlen(new_name)+2, rh->authority_name);
1305 GNUNET_free(rh->name);
1306 rh->name = new_name;
1308 rh->proc(rh->proc_cls, rh, 0, NULL);
1312 //Note only 1 pkey should have been returned.. anything else would be strange
1314 * We found an authority that may be able to help us
1315 * move on with query
1318 for (i=0; i<rd_count;i++)
1321 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1324 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1327 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
1328 if (remaining_time.rel_value == 0)
1330 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1331 "This dht entry is expired.\n");
1332 rh->authority_chain_head->fresh = 0;
1333 rh->proc(rh->proc_cls, rh, 0, NULL);
1341 * Resolve rest of query with new authority
1343 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1344 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
1345 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1346 auth->zone = rh->authority;
1347 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
1348 memset(auth->name, 0, strlen(rh->authority_name)+1);
1349 strcpy(auth->name, rh->authority_name);
1350 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1351 rh->authority_chain_tail,
1355 * We are done with PKEY resolution if name is empty
1356 * else resolve again with new authority
1358 if (strcmp(rh->name, "") == 0)
1359 rh->proc(rh->proc_cls, rh, 0, NULL);
1361 resolve_delegation_ns(rh);
1368 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1369 "Authority lookup successful but no PKEY... never get here\n");
1370 rh->proc(rh->proc_cls, rh, 0, NULL);
1375 * Resolve the delegation chain for the request in our namestore
1377 * @param rh the resolver handle
1380 resolve_delegation_ns(struct GNUNET_GNS_ResolverHandle *rh)
1383 pop_tld(rh->name, rh->authority_name);
1384 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1387 GNUNET_GNS_RECORD_PKEY,
1388 &process_delegation_result_ns,
1394 * Entry point for name resolution
1395 * Setup a new query and try to resolve
1397 * @param request the request handle of the DNS request from a client
1398 * @param p the DNS query packet we received
1399 * @param q the DNS query we received parsed from p
1402 start_resolution_for_dns(struct GNUNET_DNS_RequestHandle *request,
1403 struct GNUNET_DNSPARSER_Packet *p,
1404 struct GNUNET_DNSPARSER_Query *q)
1406 struct GNUNET_GNS_ResolverHandle *rh;
1407 struct RecordLookupHandle* rlh;
1408 struct InterceptLookupHandle* ilh;
1410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1411 "Starting resolution for %s (type=%d)!\n",
1414 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
1415 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
1416 ilh = GNUNET_malloc(sizeof(struct InterceptLookupHandle));
1419 ilh->request_handle = request;
1421 rh->authority = zone_hash;
1423 rlh->record_type = q->type;
1424 rlh->name = q->name;
1425 rlh->proc = &reply_to_dns;
1426 rlh->proc_cls = ilh;
1430 rh->name = GNUNET_malloc(strlen(q->name)
1431 - strlen(gnunet_tld) + 1);
1433 strlen(q->name)-strlen(gnunet_tld) + 1);
1434 memcpy(rh->name, q->name,
1435 strlen(q->name)-strlen(gnunet_tld));
1437 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
1439 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
1440 rh->authority_chain_head->prev = NULL;
1441 rh->authority_chain_head->next = NULL;
1442 rh->authority_chain_tail = rh->authority_chain_head;
1443 rh->authority_chain_head->zone = zone_hash;
1445 /* Start resolution in our zone */
1446 rh->proc = &process_delegation_ns;
1447 resolve_delegation_ns(rh);
1453 * The DNS request handler
1454 * Called for every incoming DNS request.
1456 * @param cls closure
1457 * @param rh request handle to user for reply
1458 * @param request_length number of bytes in request
1459 * @param request udp payload of the DNS request
1462 handle_dns_request(void *cls,
1463 struct GNUNET_DNS_RequestHandle *rh,
1464 size_t request_length,
1465 const char *request)
1467 struct GNUNET_DNSPARSER_Packet *p;
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
1472 p = GNUNET_DNSPARSER_parse (request, request_length);
1476 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1477 "Received malformed DNS packet, leaving it untouched\n");
1478 GNUNET_DNS_request_forward (rh);
1479 GNUNET_DNSPARSER_free_packet (p);
1484 * Check tld and decide if we or
1485 * legacy dns is responsible
1487 * FIXME now in theory there could be more than 1 query in the request
1488 * but if this is case we get into trouble:
1489 * either we query the GNS or the DNS. We cannot do both!
1490 * So I suggest to either only allow a single query per request or
1491 * only allow GNS or DNS requests.
1492 * The way it is implemented here now is buggy and will lead to erratic
1493 * behaviour (if multiple queries are present).
1495 if (p->num_queries == 0)
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1498 "No Queries in DNS packet... forwarding\n");
1499 GNUNET_DNS_request_forward (rh);
1500 GNUNET_DNSPARSER_free_packet(p);
1504 if (p->num_queries > 1)
1506 /* Note: We could also look for .gnunet */
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 ">1 queriy in DNS packet... odd. We only process #1\n");
1515 tldoffset = p->queries[0].name + strlen(p->queries[0].name) - 1;
1517 for (i=0; i<strlen(p->queries[0].name); i++)
1519 if (*(tldoffset-i) == '.')
1523 if ((i==strlen(gnunet_tld)-1) && (0 == strcmp(tldoffset-i, gnunet_tld)))
1525 start_resolution_for_dns(rh, p, p->queries);
1530 * This request does not concern us. Forward to real DNS.
1532 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1533 "Request for %s is forwarded to DNS\n", p->queries[0].name);
1534 GNUNET_DNS_request_forward (rh);
1535 GNUNET_DNSPARSER_free_packet (p);
1541 * Method called periodicattluy that triggers
1542 * iteration over root zone
1544 * @param cls closure
1545 * @param tc task context
1548 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1550 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1554 * Continuation for DHT put
1556 * @param cls closure
1557 * @param tc task context
1560 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1562 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1567 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1570 * Function used to put all records successively into the DHT.
1572 * @param cls the closure (NULL)
1573 * @param key the public key of the authority (ours)
1574 * @param expiration lifetime of the namestore entry
1575 * @param name the name of the records
1576 * @param rd_count the number of records in data
1577 * @param rd the record data
1578 * @param signature the signature for the record data
1581 put_gns_record(void *cls,
1582 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1583 struct GNUNET_TIME_Absolute expiration,
1585 unsigned int rd_count,
1586 const struct GNUNET_NAMESTORE_RecordData *rd,
1587 const struct GNUNET_CRYPTO_RsaSignature *signature)
1590 struct GNSNameRecordBlock *nrb;
1591 GNUNET_HashCode name_hash;
1592 GNUNET_HashCode xor_hash;
1593 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1594 uint32_t rd_payload_length;
1595 char* nrb_data = NULL;
1600 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1601 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1602 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1607 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1608 "Putting records for %s into the DHT\n", name);
1610 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1612 nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1
1613 + sizeof(struct GNSNameRecordBlock));
1615 if (signature != NULL)
1616 nrb->signature = *signature;
1618 nrb->public_key = *key;
1620 nrb->rd_count = htonl(rd_count);
1622 memset(&nrb[1], 0, strlen(name) + 1);
1623 memcpy(&nrb[1], name, strlen(name));
1625 nrb_data = (char*)&nrb[1];
1626 nrb_data += strlen(name) + 1;
1628 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
1631 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
1636 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
1644 * calculate DHT key: H(name) xor H(pubkey)
1646 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1647 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1648 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1649 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1650 "putting records for %s under key: %s with size %d\n",
1651 name, (char*)&xor_hash_string, rd_payload_length);
1653 GNUNET_DHT_put (dht_handle, &xor_hash,
1654 DHT_GNS_REPLICATION_LEVEL,
1656 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1660 DHT_OPERATION_TIMEOUT,
1662 NULL); //cls for cont
1664 num_public_records++;
1667 * Reschedule periodic put
1669 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1670 &update_zone_dht_next,
1678 * Periodically iterate over our zone and store everything in dht
1681 * @param tc task context
1684 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1686 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1687 if (0 == num_public_records)
1689 dht_update_interval = GNUNET_TIME_relative_multiply(
1690 GNUNET_TIME_UNIT_SECONDS,
1695 dht_update_interval = GNUNET_TIME_relative_multiply(
1696 GNUNET_TIME_UNIT_SECONDS,
1697 (3600/num_public_records));
1699 num_public_records = 0; //start counting again
1700 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1702 GNUNET_NAMESTORE_RF_AUTHORITY,
1703 GNUNET_NAMESTORE_RF_PRIVATE,
1709 static void send_shorten_response(const char* name,
1710 struct ClientShortenHandle *csh);
1712 process_shorten_pseu_lookup_ns(void *cls,
1713 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1714 struct GNUNET_TIME_Absolute expire,
1716 unsigned int rd_len,
1717 const struct GNUNET_NAMESTORE_RecordData *rd,
1718 const struct GNUNET_CRYPTO_RsaSignature *signature)
1720 struct GNUNET_GNS_ResolverHandle *rh =
1721 (struct GNUNET_GNS_ResolverHandle *)cls;
1722 struct GNUNET_TIME_Relative remaining_time;
1724 GNUNET_TIME_absolute_get_remaining (expire);
1729 rh->status |= EXISTS;
1732 if (remaining_time.rel_value == 0)
1734 rh->status |= EXPIRED;
1737 rh->proc(rh->proc_cls, rh, rd_len, rd);
1742 * Start DHT lookup for a PSEUdonym record in
1743 * rh->authority's zone
1745 * @param rh the pending gns query
1748 resolve_pseu_dht(struct GNUNET_GNS_ResolverHandle *rh)
1751 GNUNET_HashCode name_hash;
1752 GNUNET_HashCode lookup_key;
1755 GNUNET_CRYPTO_hash("",
1759 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1761 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1762 &dht_lookup_timeout,
1765 xquery = htonl(GNUNET_GNS_RECORD_PSEU);
1767 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1768 DHT_OPERATION_TIMEOUT,
1769 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1771 DHT_GNS_REPLICATION_LEVEL,
1775 &process_delegation_result_dht,
1782 handle_shorten_pseu_ns_result(void* cls,
1783 struct GNUNET_GNS_ResolverHandle *rh,
1785 const struct GNUNET_NAMESTORE_RecordData *rd);
1788 handle_shorten_zone_to_name(void *cls,
1789 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1790 struct GNUNET_TIME_Absolute expire,
1792 unsigned int rd_len,
1793 const struct GNUNET_NAMESTORE_RecordData *rd,
1794 const struct GNUNET_CRYPTO_RsaSignature *signature)
1796 struct GNUNET_GNS_ResolverHandle *rh =
1797 (struct GNUNET_GNS_ResolverHandle *)cls;
1798 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) rh->proc_cls;
1803 /* we found a match in our own zone */
1806 answer_len = strlen(rh->name) + strlen(name) + strlen(gnunet_tld) + 2;
1807 result = GNUNET_malloc(answer_len);
1808 memset(result, 0, answer_len);
1809 strcpy(result, rh->name);
1810 strcpy(result+strlen(rh->name), ".");
1811 strcpy(result+strlen(rh->name)+1, name);
1812 strcpy(result+strlen(rh->name)+strlen(name), gnunet_tld);
1814 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1815 "Sending shorten result %s\n", result);
1817 send_shorten_response(result, csh);
1818 free_resolver_handle(rh);
1819 GNUNET_free(result);
1824 * Nothing in our zone
1825 * check PSEU for this authority in namestore
1827 rh->proc = &handle_shorten_pseu_ns_result;
1828 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1829 &rh->authority_chain_head->zone,
1831 GNUNET_GNS_RECORD_PSEU,
1832 &process_shorten_pseu_lookup_ns,
1838 * Process result from namestore delegation lookup
1839 * for shorten operation
1841 * @param cls the client shorten handle
1842 * @param rh the resolver handle
1843 * @param rd_count number of results (0)
1844 * @param rd data (NULL)
1847 handle_shorten_pseu_dht_result(void* cls,
1848 struct GNUNET_GNS_ResolverHandle *rh,
1850 const struct GNUNET_NAMESTORE_RecordData *rd)
1852 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1853 struct AuthorityChain *auth_chain;
1865 for (i=0; i < rd_len; i++)
1867 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1869 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1870 "Found PSEU %s\n", (char*) rd[i].data);
1875 pseu = (char*) rd[i].data;
1876 answer_len = strlen(rh->name) + strlen(pseu) + strlen(gnunet_tld) + 2;
1877 result = GNUNET_malloc(answer_len);
1878 memset(result, 0, answer_len);
1879 strcpy(result, rh->name);
1880 strcpy(result+strlen(rh->name), ".");
1881 strcpy(result+strlen(rh->name)+1, pseu);
1882 strcpy(result+strlen(rh->name)+strlen(pseu), gnunet_tld);
1884 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1885 "Sending pseudonym shorten result %s\n", result);
1887 send_shorten_response(result, csh);
1888 free_resolver_handle(rh);
1889 GNUNET_free(result);
1895 * continue with next authority
1898 auth_chain = rh->authority_chain_head;
1900 if ((auth_chain->next->next == NULL) &&
1901 GNUNET_CRYPTO_hash_cmp(&auth_chain->next->zone, &zone_hash) == 0)
1906 answer_len = strlen(rh->name) + strlen(auth_chain->name)
1907 + strlen(gnunet_tld) + 2;
1909 result = GNUNET_malloc(answer_len);
1910 memset(result, 0, answer_len);
1911 strcpy(result, rh->name);
1912 strcpy(result+strlen(rh->name), ".");
1913 strcpy(result+strlen(rh->name)+1, auth_chain->name);
1914 strcpy(result+strlen(rh->name)+strlen(auth_chain->name)+1, gnunet_tld);
1915 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1916 "Sending non pseudonym shorten result %s\n", result);
1918 send_shorten_response(result, csh);
1919 free_resolver_handle(rh);
1920 GNUNET_free(result);
1925 * Continue with next authority
1927 new_name = GNUNET_malloc(strlen(rh->name)+
1928 strlen(auth_chain->name) + 2);
1929 memset(new_name, 0, strlen(rh->name)+
1930 strlen(auth_chain->name) + 2);
1931 strcpy(new_name, rh->name);
1932 strcpy(new_name+strlen(rh->name)+1, ".");
1933 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1934 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1935 rh->authority_chain_tail,
1937 GNUNET_free(rh->name);
1938 rh->name = new_name;
1939 GNUNET_free(auth_chain->name);
1940 GNUNET_free(auth_chain);
1941 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1943 &rh->authority_chain_head->zone,
1944 &handle_shorten_zone_to_name,
1952 * Process result from namestore PSEU lookup
1953 * for shorten operation
1954 * FIXME do we need to check for own zone here?
1956 * @param cls the client shorten handle
1957 * @param rh the resolver handle
1958 * @param rd_count number of results (0 if none found)
1959 * @param rd data (NULL if none found)
1962 handle_shorten_pseu_ns_result(void* cls,
1963 struct GNUNET_GNS_ResolverHandle *rh,
1965 const struct GNUNET_NAMESTORE_RecordData *rd)
1967 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1968 struct AuthorityChain *auth_chain;
1980 for (i=0; i < rd_len; i++)
1982 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1984 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1985 "Found PSEU %s\n", (char*) rd[i].data);
1990 pseu = (char*) rd[i].data;
1991 answer_len = strlen(rh->name) + strlen(pseu) + strlen(gnunet_tld) + 2;
1992 result = GNUNET_malloc(answer_len);
1993 memset(result, 0, answer_len);
1994 strcpy(result, rh->name);
1995 strcpy(result+strlen(rh->name), ".");
1996 strcpy(result+strlen(rh->name)+1, pseu);
1997 strcpy(result+strlen(rh->name)+strlen(pseu)+1, gnunet_tld);
1999 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2000 "Sending shorten result %s\n", result);
2002 send_shorten_response(result, csh);
2003 free_resolver_handle(rh);
2004 GNUNET_free(result);
2009 * No PSEU found. Ask DHT if expired.
2010 * Else contunue with next authority
2012 if (rh->status & (EXISTS | !EXPIRED))
2017 auth_chain = rh->authority_chain_head;
2018 new_name = GNUNET_malloc(strlen(rh->name)+
2019 strlen(auth_chain->name) + 2);
2020 memset(new_name, 0, strlen(rh->name)+
2021 strlen(auth_chain->name) + 2);
2022 strcpy(new_name, rh->name);
2023 strcpy(new_name+strlen(rh->name)+1, ".");
2024 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
2026 GNUNET_free(rh->name);
2027 rh->name = new_name;
2028 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
2029 rh->authority_chain_tail,
2032 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2034 &rh->authority_chain_head->zone,
2035 &handle_shorten_zone_to_name,
2043 rh->authority = rh->authority_chain_head->zone;
2044 rh->proc = &handle_shorten_pseu_dht_result;
2045 resolve_pseu_dht(rh);
2052 * Process result from namestore delegation lookup
2053 * for shorten operation
2055 * @param cls the client shorten handle
2056 * @param rh the resolver handle
2057 * @param rd_count number of results (0)
2058 * @param rd data (NULL)
2061 handle_shorten_delegation_result(void* cls,
2062 struct GNUNET_GNS_ResolverHandle *rh,
2064 const struct GNUNET_NAMESTORE_RecordData *rd)
2066 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
2067 struct AuthorityChain *auth_chain;
2072 * At this point rh->name contains the part of the name
2073 * that we do not have a PKEY in our namestore to resolve.
2074 * The authority chain in the resolver handle is now
2075 * useful to backtrack if needed
2078 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2079 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2081 if (GNUNET_CRYPTO_hash_cmp(&rh->authority_chain_head->zone,
2085 * This is our zone append .gnunet unless name is empty
2086 * (it shouldn't be, usually FIXME what happens if we
2087 * shorten to our zone to a "" record??)
2090 answer_len = strlen(rh->name) + strlen(gnunet_tld) + 2;
2091 result = GNUNET_malloc(answer_len);
2092 memset(result, 0, answer_len);
2093 strcpy(result, rh->name);
2094 strcpy(result+strlen(rh->name), ".");
2095 strcpy(result+strlen(rh->name)+1, gnunet_tld);
2097 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2098 "Our zone: Sending name as shorten result %s\n", rh->name);
2100 send_shorten_response(rh->name, csh); //FIXME +.gnunet!
2101 free_resolver_handle(rh);
2102 GNUNET_free(csh->name);
2104 GNUNET_free(result);
2108 auth_chain = rh->authority_chain_head;
2109 /* backtrack authorities for pseu */
2110 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2113 &handle_shorten_zone_to_name,
2118 typedef void (*ShortenResponseProc) (void* cls, const char* name);
2121 * Shorten a given name
2123 * @param name the name to shorten
2124 * @param csh the shorten handle of the request
2127 shorten_name(char* name, struct ClientShortenHandle* csh)
2130 struct GNUNET_GNS_ResolverHandle *rh;
2132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2133 "Starting resolution for %s (type=%d)!\n",
2134 name, GNUNET_GNS_RECORD_PKEY);
2136 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
2137 rh->authority = zone_hash;
2139 rh->name = GNUNET_malloc(strlen(name)
2140 - strlen(gnunet_tld) + 1);
2142 strlen(name)-strlen(gnunet_tld) + 1);
2143 memcpy(rh->name, name,
2144 strlen(name)-strlen(gnunet_tld));
2146 csh->name = GNUNET_malloc(strlen(name)
2147 - strlen(gnunet_tld) + 1);
2149 strlen(name)-strlen(gnunet_tld) + 1);
2150 memcpy(rh->name, name,
2151 strlen(name)-strlen(gnunet_tld));
2153 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
2155 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2156 rh->authority_chain_tail = rh->authority_chain_head;
2157 rh->authority_chain_head->zone = zone_hash;
2158 rh->proc = &handle_shorten_delegation_result;
2159 rh->proc_cls = (void*)csh;
2161 /* Start delegation resolution in our namestore */
2162 resolve_delegation_ns(rh);
2167 * Send shorten response back to client
2169 * @param name the shortened name result or NULL if cannot be shortened
2170 * @param csh the handle to the shorten request
2173 send_shorten_response(const char* name, struct ClientShortenHandle *csh)
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
2176 "SHORTEN_RESULT", name);
2177 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
2184 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
2185 + strlen(name) + 1);
2187 rmsg->id = csh->unique_id;
2188 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
2190 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
2193 strcpy((char*)&rmsg[1], name);
2195 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
2196 (const struct GNUNET_MessageHeader *) rmsg,
2198 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
2201 GNUNET_free(csh->name);
2207 * Handle a shorten message from the api
2209 * @param cls the closure
2210 * @param client the client
2211 * @param message the message
2213 static void handle_shorten(void *cls,
2214 struct GNUNET_SERVER_Client * client,
2215 const struct GNUNET_MessageHeader * message)
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
2219 size_t msg_size = 0;
2220 struct ClientShortenHandle *csh;
2222 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
2224 GNUNET_break_op (0);
2225 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2229 GNUNET_SERVER_notification_context_add (nc, client);
2231 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
2232 (struct GNUNET_GNS_ClientShortenMessage *) message;
2234 msg_size = ntohs(message->size);
2236 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2238 GNUNET_break_op (0);
2239 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2243 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
2244 csh->client = client;
2245 csh->unique_id = sh_msg->id;
2247 shorten_name((char*)&sh_msg[1], csh);
2252 * Reply to client with the result from our lookup.
2254 * @param cls the closure (our client lookup handle)
2255 * @param rh the request handle of the lookup
2256 * @param rd_count the number of records
2257 * @param rd the record data
2260 reply_to_client(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
2262 const struct GNUNET_NAMESTORE_RecordData *rd)
2264 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
2265 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
2268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
2269 "LOOKUP_RESULT", rd_count);
2271 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
2272 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
2274 rmsg->id = clh->unique_id;
2275 rmsg->rd_count = htonl(rd_count);
2276 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
2278 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
2280 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
2282 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
2283 (const struct GNUNET_MessageHeader *) rmsg,
2285 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
2287 GNUNET_free(rh->proc_cls);
2288 free_resolver_handle(rh);
2290 GNUNET_free(clh->name);
2296 * Lookup a given name
2298 * @param name the name to looku[
2299 * @param clh the client lookup handle
2302 lookup_name(char* name, struct ClientLookupHandle* clh)
2305 struct GNUNET_GNS_ResolverHandle *rh;
2306 struct RecordLookupHandle* rlh;
2308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2309 "Starting resolution for %s (type=%d)!\n",
2312 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
2313 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
2315 rh->authority = zone_hash;
2317 rlh->record_type = clh->type;
2318 rlh->name = clh->name;
2319 rlh->proc = &reply_to_client;
2320 rlh->proc_cls = clh;
2324 rh->name = GNUNET_malloc(strlen(name)
2325 - strlen(gnunet_tld) + 1);
2327 strlen(name)-strlen(gnunet_tld) + 1);
2328 memcpy(rh->name, name,
2329 strlen(name)-strlen(gnunet_tld));
2331 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
2333 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2334 rh->authority_chain_head->prev = NULL;
2335 rh->authority_chain_head->next = NULL;
2336 rh->authority_chain_tail = rh->authority_chain_head;
2337 rh->authority_chain_head->zone = zone_hash;
2339 /* Start resolution in our zone */
2340 rh->proc = &process_delegation_ns;
2341 resolve_delegation_ns(rh);
2346 * Handle lookup requests from client
2348 * @param cls the closure
2349 * @param client the client
2350 * @param message the message
2353 handle_lookup(void *cls,
2354 struct GNUNET_SERVER_Client * client,
2355 const struct GNUNET_MessageHeader * message)
2357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
2359 size_t msg_size = 0;
2360 struct ClientLookupHandle *clh;
2362 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
2364 GNUNET_break_op (0);
2365 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2369 GNUNET_SERVER_notification_context_add (nc, client);
2371 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
2372 (struct GNUNET_GNS_ClientLookupMessage *) message;
2374 msg_size = ntohs(message->size);
2376 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2378 GNUNET_break_op (0);
2379 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2383 clh = GNUNET_malloc(sizeof(struct ClientLookupHandle));
2384 clh->client = client;
2385 clh->name = GNUNET_malloc(strlen((char*)&sh_msg[1]) + 1);
2386 strcpy(clh->name, (char*)&sh_msg[1]);
2387 clh->unique_id = sh_msg->id;
2388 clh->type = ntohl(sh_msg->type);
2390 lookup_name((char*)&sh_msg[1], clh);
2394 * Process GNS requests.
2396 * @param cls closure)
2397 * @param server the initialized server
2398 * @param c configuration to use
2401 run (void *cls, struct GNUNET_SERVER_Handle *server,
2402 const struct GNUNET_CONFIGURATION_Handle *c)
2405 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
2408 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2410 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2411 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
2412 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0}
2415 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
2416 "ZONEKEY", &keyfile))
2418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2419 "No private key for root zone specified%s!\n", keyfile);
2420 GNUNET_SCHEDULER_shutdown(0);
2424 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2425 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
2427 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2429 GNUNET_free(keyfile);
2434 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
2437 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2438 "DNS hijacking enabled... connecting to service.\n");
2440 * Do gnunet dns init here
2442 dns_handle = GNUNET_DNS_connect(c,
2443 GNUNET_DNS_FLAG_PRE_RESOLUTION,
2444 &handle_dns_request, /* rh */
2445 NULL); /* Closure */
2446 if (NULL == dns_handle)
2448 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2449 "Failed to connect to the dnsservice!\n");
2456 * handle to our local namestore
2458 namestore_handle = GNUNET_NAMESTORE_connect(c);
2460 if (NULL == namestore_handle)
2462 //FIXME do error handling;
2463 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2464 "Failed to connect to the namestore!\n");
2465 GNUNET_SCHEDULER_shutdown(0);
2472 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
2474 if (NULL == dht_handle)
2476 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
2479 //put_some_records(); //FIXME for testing
2482 * Schedule periodic put
2484 * We have roughly an hour for all records;
2486 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
2488 //zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
2490 GNUNET_SERVER_add_handlers (server, handlers);
2493 //GNUNET_SERVER_disconnect_notify (server,
2494 // &client_disconnect_notification,
2497 nc = GNUNET_SERVER_notification_context_create (server, 1);
2499 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
2505 * The main function for the GNS service.
2507 * @param argc number of arguments from the command line
2508 * @param argv command line arguments
2509 * @return 0 ok, 1 on error
2512 main (int argc, char *const *argv)
2518 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
2523 /* end of gnunet-service-gns.c */