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;
258 struct AuthorityChain *ac_next;
263 GNUNET_free_non_null (rh->name);
264 GNUNET_free_non_null (rh->authority_name);
266 ac = rh->authority_chain_head;
271 GNUNET_free_non_null (ac->name);
280 * Reply to client with the result from our lookup.
282 * @param rh the request handle of the lookup
283 * @param rd_count the number of records to return
284 * @param rd the record data
287 reply_to_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
288 const struct GNUNET_NAMESTORE_RecordData *rd)
294 struct InterceptLookupHandle* ilh = (struct InterceptLookupHandle*)cls;
295 struct GNUNET_DNSPARSER_Packet *packet = ilh->packet;
296 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
297 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
298 packet->answers = answer_records;
299 packet->additional_records = additional_records;
302 * Put records in the DNS packet and modify it
305 len = sizeof(struct GNUNET_DNSPARSER_Record*);
306 for (i=0; i < rd_count; i++)
309 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
310 "Adding type %d to DNS response\n", rd[i].record_type);
311 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", rh->name);
312 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "QName: %s\n", ilh->query->name);
313 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
314 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
316 if (rd[i].record_type == ilh->query->type)
318 answer_records[i].name = ilh->query->name;
319 answer_records[i].type = rd[i].record_type;
320 answer_records[i].data.raw.data_len = rd[i].data_size;
321 answer_records[i].data.raw.data = (char*)rd[i].data;
322 answer_records[i].expiration_time = rd[i].expiration;
323 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
327 additional_records[i].name = ilh->query->name;
328 additional_records[i].type = rd[i].record_type;
329 additional_records[i].data.raw.data_len = rd[i].data_size;
330 additional_records[i].data.raw.data = (char*)rd[i].data;
331 additional_records[i].expiration_time = rd[i].expiration;
332 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
336 packet->num_answers = rh->answered;
337 packet->num_additional_records = rd_count-(rh->answered);
339 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
340 packet->flags.authoritative_answer = 1;
342 packet->flags.authoritative_answer = 0;
345 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
347 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
349 packet->flags.query_or_response = 1;
355 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
356 "Building DNS response\n");
357 ret = GNUNET_DNSPARSER_pack (packet,
358 1024, /* FIXME magic from dns redirector */
361 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
362 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
363 if (ret == GNUNET_OK)
365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
366 "Answering DNS request\n");
367 GNUNET_DNS_request_answer(ilh->request_handle,
372 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
376 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
377 "Error building DNS response! (ret=%d)", ret);
380 packet->num_answers = 0;
381 packet->answers = NULL;
382 packet->num_additional_records = 0;
383 packet->additional_records = NULL;
384 GNUNET_DNSPARSER_free_packet(packet);
386 GNUNET_free((struct RecordLookupHandle*)rh->proc_cls);
387 free_resolver_handle(rh);
393 * Task run during shutdown.
399 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
401 /* Kill zone task for it may make the scheduler hang */
402 if (zone_update_taskid)
403 GNUNET_SCHEDULER_cancel(zone_update_taskid);
405 GNUNET_SERVER_notification_context_destroy (nc);
406 GNUNET_DNS_disconnect(dns_handle);
407 GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
408 GNUNET_DHT_disconnect(dht_handle);
412 * Callback when record data is put into namestore
414 * @param cls the closure
415 * @param success GNUNET_OK on success
416 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
419 on_namestore_record_put_result(void *cls,
423 if (GNUNET_NO == success)
425 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
428 else if (GNUNET_YES == success)
430 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
431 "records successfully put in namestore\n");
435 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
436 "Error putting records into namestore: %s\n", emsg);
440 * Handle timeout for DHT requests
442 * @param cls the request handle as closure
443 * @param tc the task context
446 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
448 struct GNUNET_GNS_ResolverHandle *rh = cls;
450 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
451 "dht lookup for query %s timed out.\n",
454 GNUNET_DHT_get_stop (rh->get_handle);
455 rh->proc(rh->proc_cls, rh, 0, NULL);
461 * Function called when we get a result from the dht
464 * @param cls the request handle
465 * @param exp lifetime
466 * @param key the key the record was stored under
467 * @param get_path get path
468 * @param get_path_length get path length
469 * @param put_path put path
470 * @param put_path_length put path length
471 * @param type the block type
472 * @param size the size of the record
473 * @param data the record data
476 process_record_dht_result(void* cls,
477 struct GNUNET_TIME_Absolute exp,
478 const GNUNET_HashCode * key,
479 const struct GNUNET_PeerIdentity *get_path,
480 unsigned int get_path_length,
481 const struct GNUNET_PeerIdentity *put_path,
482 unsigned int put_path_length,
483 enum GNUNET_BLOCK_Type type,
484 size_t size, const void *data)
486 struct GNUNET_GNS_ResolverHandle *rh;
487 struct RecordLookupHandle *rlh;
488 struct GNSNameRecordBlock *nrb;
489 uint32_t num_records;
491 char* rd_data = (char*)data;
495 GNUNET_HashCode zone, name_hash;
496 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
501 //FIXME maybe check expiration here, check block type
503 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
504 rlh = (struct RecordLookupHandle *) rh->proc_cls;
505 nrb = (struct GNSNameRecordBlock*)data;
507 /* stop lookup and timeout task */
508 GNUNET_DHT_get_stop (rh->get_handle);
509 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
511 rh->get_handle = NULL;
512 name = (char*)&nrb[1];
513 num_records = ntohl(nrb->rd_count);
515 struct GNUNET_NAMESTORE_RecordData rd[num_records];
517 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
518 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
520 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
525 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
529 for (i=0; i<num_records; i++)
531 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
532 "Got name: %s (wanted %s)\n", name, rh->name);
533 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
536 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
537 "Got data length: %d\n", rd[i].data_size);
538 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
539 "Got flag %d\n", rd[i].flags);
541 if ((strcmp(name, rh->name) == 0) &&
542 (rd[i].record_type == rlh->record_type))
549 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
550 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
553 * FIXME check pubkey against existing key in namestore?
554 * https://gnunet.org/bugs/view.php?id=2179
557 /* Save to namestore */
558 GNUNET_NAMESTORE_record_put (namestore_handle,
565 &on_namestore_record_put_result, //cont
569 rh->proc(rh->proc_cls, rh, num_records, rd);
571 rh->proc(rh->proc_cls, rh, 0, NULL);
578 * Start DHT lookup for a (name -> query->record_type) record in
579 * rh->authority's zone
581 * @param rh the pending gns query context
582 * @param name the name to query record
585 resolve_record_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
588 GNUNET_HashCode name_hash;
589 GNUNET_HashCode lookup_key;
590 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
591 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
593 GNUNET_CRYPTO_hash(rh->name, strlen(rh->name), &name_hash);
594 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
595 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
597 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
598 "starting dht lookup for %s with key: %s\n",
599 rh->name, (char*)&lookup_key_string);
601 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
602 &dht_lookup_timeout, rh);
604 xquery = htonl(rlh->record_type);
605 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
606 DHT_OPERATION_TIMEOUT,
607 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
609 DHT_GNS_REPLICATION_LEVEL,
613 &process_record_dht_result,
620 * Namestore calls this function if we have record for this name.
621 * (or with rd_count=0 to indicate no matches)
623 * @param cls the pending query
624 * @param key the key of the zone we did the lookup
625 * @param expiration expiration date of the namestore entry
626 * @param name the name for which we need an authority
627 * @param rd_count the number of records with 'name'
628 * @param rd the record data
629 * @param signature the signature of the authority for the record data
632 process_record_lookup_ns(void* cls,
633 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
634 struct GNUNET_TIME_Absolute expiration,
635 const char *name, unsigned int rd_count,
636 const struct GNUNET_NAMESTORE_RecordData *rd,
637 const struct GNUNET_CRYPTO_RsaSignature *signature)
639 struct GNUNET_GNS_ResolverHandle *rh;
640 struct RecordLookupHandle *rlh;
641 struct GNUNET_TIME_Relative remaining_time;
642 GNUNET_HashCode zone;
644 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
645 rlh = (struct RecordLookupHandle *)rh->proc_cls;
646 GNUNET_CRYPTO_hash(key,
647 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
649 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
655 rh->status |= EXISTS;
658 if (remaining_time.rel_value == 0)
660 rh->status |= EXPIRED;
666 * Lookup terminated and no results
668 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
669 "Namestore lookup for %s terminated without results\n", name);
673 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
674 "Record %s unknown in namestore\n",
677 * Our zone and no result? Cannot resolve TT
679 rh->proc(rh->proc_cls, rh, 0, NULL);
686 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
687 "Processing additional result %s from namestore\n", name);
689 for (i=0; i<rd_count;i++)
692 if (rd[i].record_type != rlh->record_type)
695 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
698 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
709 if (rh->answered == 0)
711 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
712 "No answers found. This is odd!\n");
713 rh->proc(rh->proc_cls, rh, 0, NULL);
717 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
720 rh->proc(rh->proc_cls, rh, rd_count, rd);
725 * The final phase of resolution.
726 * This is a name that is canonical and we do not have a delegation.
728 * @param rh the pending lookup
731 resolve_record_from_ns(struct GNUNET_GNS_ResolverHandle *rh)
733 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
735 * Try to resolve this record in our namestore.
736 * The name to resolve is now in rh->authority_name
737 * since we tried to resolve it to an authority
740 GNUNET_NAMESTORE_lookup_record(namestore_handle,
744 &process_record_lookup_ns,
751 * Handle timeout for DHT requests
753 * @param cls the request handle as closure
754 * @param tc the task context
757 dht_authority_lookup_timeout(void *cls,
758 const struct GNUNET_SCHEDULER_TaskContext *tc)
760 struct GNUNET_GNS_ResolverHandle *rh = cls;
762 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
763 "dht lookup for query %s timed out.\n",
766 GNUNET_DHT_get_stop (rh->get_handle);
767 if (strcmp(rh->name, "") == 0)
770 * promote authority back to name and try to resolve record
772 strcpy(rh->name, rh->authority_name);
774 rh->proc(rh->proc_cls, rh, 0, NULL);
778 static void resolve_delegation_from_dht(struct GNUNET_GNS_ResolverHandle *rh);
781 * Function called when we get a result from the dht
782 * for our query. Recursively tries to resolve PKEYs
785 * @param cls the request handle
786 * @param exp lifetime
787 * @param key the key the record was stored under
788 * @param get_path get path
789 * @param get_path_length get path length
790 * @param put_path put path
791 * @param put_path_length put path length
792 * @param type the block type
793 * @param size the size of the record
794 * @param data the record data
797 process_authority_dht_result(void* cls,
798 struct GNUNET_TIME_Absolute exp,
799 const GNUNET_HashCode * key,
800 const struct GNUNET_PeerIdentity *get_path,
801 unsigned int get_path_length,
802 const struct GNUNET_PeerIdentity *put_path,
803 unsigned int put_path_length,
804 enum GNUNET_BLOCK_Type type,
805 size_t size, const void *data)
807 struct GNUNET_GNS_ResolverHandle *rh;
808 struct GNSNameRecordBlock *nrb;
809 uint32_t num_records;
811 char* rd_data = (char*) data;
814 GNUNET_HashCode zone, name_hash;
819 //FIXME check expiration?
821 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
822 nrb = (struct GNSNameRecordBlock*)data;
824 /* stop dht lookup and timeout task */
825 GNUNET_DHT_get_stop (rh->get_handle);
826 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
828 rh->get_handle = NULL;
829 num_records = ntohl(nrb->rd_count);
830 name = (char*)&nrb[1];
832 struct GNUNET_NAMESTORE_RecordData rd[num_records];
834 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
835 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
837 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
842 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
846 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
847 "Got name: %s (wanted %s)\n", name, rh->authority_name);
848 for (i=0; i<num_records; i++)
851 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
852 "Got name: %s (wanted %s)\n", name, rh->authority_name);
853 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
854 "Got type: %d (wanted %d)\n",
855 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
856 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
857 "Got data length: %d\n", rd[i].data_size);
858 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
859 "Got flag %d\n", rd[i].flags);
861 if ((strcmp(name, rh->authority_name) == 0) &&
862 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
864 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
866 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
867 struct AuthorityChain *auth =
868 GNUNET_malloc(sizeof(struct AuthorityChain));
869 auth->zone = rh->authority;
870 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
871 memset(auth->name, 0, strlen(rh->authority_name)+1);
872 strcpy(auth->name, rh->authority_name);
873 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
874 rh->authority_chain_tail,
881 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
882 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
884 /* Save to namestore */
885 if (0 != GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
887 GNUNET_NAMESTORE_record_put (namestore_handle,
894 &on_namestore_record_put_result, //cont
903 if (strcmp(rh->name, "") == 0)
904 rh->proc(rh->proc_cls, rh, 0, NULL);
906 resolve_delegation_from_dht(rh);
911 * should never get here unless false dht key/put
912 * block plugin should handle this
915 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DHT authority lookup error!\n");
921 * Process DHT lookup result for record.
923 * @param cls the closure
924 * @param rh resolver handle
925 * @param rd_count number of results (always 0)
926 * @param rd record data (always NULL)
929 process_record_result_dht(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
930 unsigned int rd_count,
931 const struct GNUNET_NAMESTORE_RecordData *rd)
933 struct RecordLookupHandle* rlh;
934 rlh = (struct RecordLookupHandle*)cls;
937 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
938 "No records for %s found in DHT. Aborting\n",
940 /* give up, cannot resolve */
941 rlh->proc(rlh->proc_cls, rh, 0, NULL);
942 //reply_to_dns(NULL, rh, 0, NULL);
946 /* results found yay */
947 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
948 "Record resolved from namestore!");
949 rlh->proc(rlh->proc_cls, rh, rd_count, rd);
950 //reply_to_dns(NULL, rh, rd_count, rd);
956 * Process namestore lookup result for record.
958 * @param cls the closure
959 * @param rh resolver handle
960 * @param rd_count number of results (always 0)
961 * @param rd record data (always NULL)
964 process_record_result_ns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
965 unsigned int rd_count,
966 const struct GNUNET_NAMESTORE_RecordData *rd)
968 struct RecordLookupHandle* rlh;
969 rlh = (struct RecordLookupHandle*) cls;
972 /* ns entry expired. try dht */
973 if (rh->status & (EXPIRED | !EXISTS))
975 rh->proc = &process_record_result_dht;
976 resolve_record_from_dht(rh);
979 /* give up, cannot resolve */
980 rlh->proc(rlh->proc_cls, rh, 0, NULL);
981 //reply_to_dns(NULL, rh, 0, NULL);
985 /* results found yay */
986 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
987 "Record resolved from namestore!");
988 rlh->proc(rlh->proc_cls, rh, rd_count, rd);
989 //reply_to_dns(NULL, rh, rd_count, rd);
995 * Determine if this name is canonical.
997 * a.b.gnunet = not canonical
1000 * @param name the name to test
1001 * @return 1 if canonical
1004 is_canonical(char* name)
1006 uint32_t len = strlen(name);
1009 for (i=0; i<len; i++)
1011 if (*(name+i) == '.')
1018 * Move one level up in the domain hierarchy and return the
1019 * passed top level domain.
1021 * @param name the domain
1022 * @param dest the destination where the tld will be put
1025 pop_tld(char* name, char* dest)
1029 if (is_canonical(name))
1036 for (len = strlen(name); len > 0; len--)
1038 if (*(name+len) == '.')
1048 strcpy(dest, (name+len+1));
1052 * DHT resolution for delegation finished. Processing result.
1054 * @param cls the closure
1055 * @param rh resolver handle
1056 * @param rd_count number of results (always 0)
1057 * @param rd record data (always NULL)
1060 process_dht_delegation_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1061 unsigned int rd_count,
1062 const struct GNUNET_NAMESTORE_RecordData *rd)
1064 struct RecordLookupHandle* rlh;
1065 rlh = (struct RecordLookupHandle*) cls;
1067 if (strcmp(rh->name, "") == 0)
1069 /* We resolved full name for delegation. resolving record */
1070 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1071 "Resolved full name for delegation via DHT. resolving record '' in ns\n");
1072 rh->proc = &process_record_result_ns;
1073 resolve_record_from_ns(rh);
1078 * we still have some left
1080 if (is_canonical(rh->name))
1082 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1083 "Resolving canonical record %s in ns\n", rh->name);
1084 rh->proc = &process_record_result_ns;
1085 resolve_record_from_ns(rh);
1088 /* give up, cannot resolve */
1089 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1090 "Cannot fully resolve delegation for %s via DHT!\n",
1092 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1093 //reply_to_dns(NULL, rh, 0, NULL);
1098 * Start DHT lookup for a name -> PKEY (compare NS) record in
1099 * rh->authority's zone
1101 * @param rh the pending gns query
1102 * @param name the name of the PKEY record
1105 resolve_delegation_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
1108 GNUNET_HashCode name_hash;
1109 GNUNET_HashCode lookup_key;
1111 GNUNET_CRYPTO_hash(rh->authority_name,
1112 strlen(rh->authority_name),
1114 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1116 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1117 &dht_authority_lookup_timeout,
1120 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1122 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1123 DHT_OPERATION_TIMEOUT,
1124 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1126 DHT_GNS_REPLICATION_LEVEL,
1130 &process_authority_dht_result,
1137 * Namestore resolution for delegation finished. Processing result.
1139 * @param cls the closure
1140 * @param rh resolver handle
1141 * @param rd_count number of results (always 0)
1142 * @param rd record data (always NULL)
1145 process_ns_delegation_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1146 unsigned int rd_count,
1147 const struct GNUNET_NAMESTORE_RecordData *rd)
1149 struct RecordLookupHandle* rlh;
1150 rlh = (struct RecordLookupHandle*) cls;
1152 if (strcmp(rh->name, "") == 0)
1154 /* We resolved full name for delegation. resolving record */
1155 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1156 "Resolved full name for delegation. resolving record ''\n");
1157 rh->proc = &process_record_result_ns;
1158 resolve_record_from_ns(rh);
1163 * we still have some left
1164 * check if ns entry is fresh
1166 if (rh->status & (EXISTS | !EXPIRED))
1168 if (is_canonical(rh->name))
1170 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1171 "Resolving canonical record %s\n", rh->name);
1172 rh->proc = &process_record_result_ns;
1173 resolve_record_from_ns(rh);
1177 /* give up, cannot resolve */
1178 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1179 "Cannot fully resolve delegation for %s!\n",
1181 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1182 //reply_to_dns(NULL, rh, 0, NULL);
1187 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1188 "Trying to resolve delegation for %s via DHT\n",
1190 rh->proc = &process_dht_delegation_dns;
1191 resolve_delegation_from_dht(rh);
1195 static void resolve_delegation_from_ns(struct GNUNET_GNS_ResolverHandle *rh);
1198 * This is a callback function that should give us only PKEY
1199 * records. Used to query the namestore for the authority (PKEY)
1200 * for 'name'. It will recursively try to resolve the
1201 * authority for a given name from the namestore.
1203 * @param cls the pending query
1204 * @param key the key of the zone we did the lookup
1205 * @param expiration expiration date of the record data set in the namestore
1206 * @param name the name for which we need an authority
1207 * @param rd_count the number of records with 'name'
1208 * @param rd the record data
1209 * @param signature the signature of the authority for the record data
1212 process_authority_lookup_ns(void* cls,
1213 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1214 struct GNUNET_TIME_Absolute expiration,
1216 unsigned int rd_count,
1217 const struct GNUNET_NAMESTORE_RecordData *rd,
1218 const struct GNUNET_CRYPTO_RsaSignature *signature)
1220 struct GNUNET_GNS_ResolverHandle *rh;
1221 struct GNUNET_TIME_Relative remaining_time;
1222 GNUNET_HashCode zone;
1225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %d records from authority lookup\n",
1228 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
1229 GNUNET_CRYPTO_hash(key,
1230 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1232 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1238 rh->status |= EXISTS;
1241 if (remaining_time.rel_value == 0)
1243 rh->status |= EXPIRED;
1247 * No authority found in namestore.
1252 * We did not find an authority in the namestore
1257 * Promote this authority back to a name maybe it is
1260 if (strcmp(rh->name, "") == 0)
1262 /* simply promote back */
1263 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1264 "Promoting %s back to name\n", rh->authority_name);
1265 strcpy(rh->name, rh->authority_name);
1269 /* add back to existing name */
1270 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1271 "Adding %s back to %s\n",
1272 rh->authority_name, rh->name);
1273 new_name = GNUNET_malloc(strlen(rh->name)
1274 + strlen(rh->authority_name) + 2);
1275 memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1276 strcpy(new_name, rh->name);
1277 strcpy(new_name+strlen(new_name)+1, ".");
1278 strcpy(new_name+strlen(new_name)+2, rh->authority_name);
1279 GNUNET_free(rh->name);
1280 rh->name = new_name;
1282 rh->proc(rh->proc_cls, rh, 0, NULL);
1286 //Note only 1 pkey should have been returned.. anything else would be strange
1288 * We found an authority that may be able to help us
1289 * move on with query
1292 for (i=0; i<rd_count;i++)
1295 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1298 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1301 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
1302 if (remaining_time.rel_value == 0)
1304 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1305 "This dht entry is expired.\n");
1306 rh->authority_chain_head->fresh = 0;
1307 rh->proc(rh->proc_cls, rh, 0, NULL);
1315 * Resolve rest of query with new authority
1317 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1318 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
1319 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1320 auth->zone = rh->authority;
1321 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
1322 memset(auth->name, 0, strlen(rh->authority_name)+1);
1323 strcpy(auth->name, rh->authority_name);
1324 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1325 rh->authority_chain_tail,
1329 * We are done with PKEY resolution if name is empty
1330 * else resolve again with new authority
1332 if (strcmp(rh->name, "") == 0)
1333 rh->proc(rh->proc_cls, rh, 0, NULL);
1335 resolve_delegation_from_ns(rh);
1342 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1343 "Authority lookup successful but no PKEY... never get here\n");
1344 rh->proc(rh->proc_cls, rh, 0, NULL);
1349 * Resolve the delegation chain for the request
1351 * @param rh the resolver handle
1354 resolve_delegation_from_ns(struct GNUNET_GNS_ResolverHandle *rh)
1357 pop_tld(rh->name, rh->authority_name);
1358 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1361 GNUNET_GNS_RECORD_PKEY,
1362 &process_authority_lookup_ns,
1368 * Entry point for name resolution
1369 * Setup a new query and try to resolve
1371 * @param request the request handle of the DNS request from a client
1372 * @param p the DNS query packet we received
1373 * @param q the DNS query we received parsed from p
1376 start_resolution_from_dns(struct GNUNET_DNS_RequestHandle *request,
1377 struct GNUNET_DNSPARSER_Packet *p,
1378 struct GNUNET_DNSPARSER_Query *q)
1380 struct GNUNET_GNS_ResolverHandle *rh;
1381 struct RecordLookupHandle* rlh;
1382 struct InterceptLookupHandle* ilh;
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1385 "Starting resolution for %s (type=%d)!\n",
1388 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
1389 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
1390 ilh = GNUNET_malloc(sizeof(struct InterceptLookupHandle));
1393 ilh->request_handle = request;
1395 rh->authority = zone_hash;
1397 rlh->record_type = q->type;
1398 rlh->name = q->name;
1399 rlh->proc = &reply_to_dns;
1400 rlh->proc_cls = ilh;
1404 rh->authority = zone_hash;
1405 rh->name = GNUNET_malloc(strlen(q->name)
1406 - strlen(gnunet_tld) + 1);
1408 strlen(q->name)-strlen(gnunet_tld) + 1);
1409 memcpy(rh->name, q->name,
1410 strlen(q->name)-strlen(gnunet_tld));
1412 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
1414 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
1415 rh->authority_chain_head->prev = NULL;
1416 rh->authority_chain_head->next = NULL;
1417 rh->authority_chain_tail = rh->authority_chain_head;
1418 rh->authority_chain_head->zone = zone_hash;
1420 /* Start resolution in our zone */
1421 rh->proc = &process_ns_delegation_dns;
1422 resolve_delegation_from_ns(rh);
1428 * The DNS request handler
1429 * Called for every incoming DNS request.
1431 * @param cls closure
1432 * @param rh request handle to user for reply
1433 * @param request_length number of bytes in request
1434 * @param request udp payload of the DNS request
1437 handle_dns_request(void *cls,
1438 struct GNUNET_DNS_RequestHandle *rh,
1439 size_t request_length,
1440 const char *request)
1442 struct GNUNET_DNSPARSER_Packet *p;
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
1447 p = GNUNET_DNSPARSER_parse (request, request_length);
1451 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1452 "Received malformed DNS packet, leaving it untouched\n");
1453 GNUNET_DNS_request_forward (rh);
1454 GNUNET_DNSPARSER_free_packet (p);
1459 * Check tld and decide if we or
1460 * legacy dns is responsible
1462 * FIXME now in theory there could be more than 1 query in the request
1463 * but if this is case we get into trouble:
1464 * either we query the GNS or the DNS. We cannot do both!
1465 * So I suggest to either only allow a single query per request or
1466 * only allow GNS or DNS requests.
1467 * The way it is implemented here now is buggy and will lead to erratic
1468 * behaviour (if multiple queries are present).
1470 if (p->num_queries == 0)
1472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1473 "No Queries in DNS packet... forwarding\n");
1474 GNUNET_DNS_request_forward (rh);
1475 GNUNET_DNSPARSER_free_packet(p);
1479 if (p->num_queries > 1)
1481 /* Note: We could also look for .gnunet */
1482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1483 ">1 queriy in DNS packet... odd. We only process #1\n");
1490 tldoffset = p->queries[0].name + strlen(p->queries[0].name) - 1;
1492 for (i=0; i<strlen(p->queries[0].name); i++)
1494 if (*(tldoffset-i) == '.')
1498 if ((i==strlen(gnunet_tld)-1) && (0 == strcmp(tldoffset-i, gnunet_tld)))
1500 start_resolution_from_dns(rh, p, p->queries);
1505 * This request does not concern us. Forward to real DNS.
1507 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1508 "Request for %s is forwarded to DNS\n", p->queries[0].name);
1509 GNUNET_DNS_request_forward (rh);
1510 GNUNET_DNSPARSER_free_packet (p);
1516 * Method called periodicattluy that triggers
1517 * iteration over root zone
1519 * @param cls closure
1520 * @param tc task context
1523 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1525 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1529 * Continuation for DHT put
1531 * @param cls closure
1532 * @param tc task context
1535 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1537 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1542 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1545 * Function used to put all records successively into the DHT.
1547 * @param cls the closure (NULL)
1548 * @param key the public key of the authority (ours)
1549 * @param expiration lifetime of the namestore entry
1550 * @param name the name of the records
1551 * @param rd_count the number of records in data
1552 * @param rd the record data
1553 * @param signature the signature for the record data
1556 put_gns_record(void *cls,
1557 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1558 struct GNUNET_TIME_Absolute expiration,
1560 unsigned int rd_count,
1561 const struct GNUNET_NAMESTORE_RecordData *rd,
1562 const struct GNUNET_CRYPTO_RsaSignature *signature)
1565 struct GNSNameRecordBlock *nrb;
1566 GNUNET_HashCode name_hash;
1567 GNUNET_HashCode xor_hash;
1568 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1569 uint32_t rd_payload_length;
1570 char* nrb_data = NULL;
1575 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1576 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1577 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1582 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1583 "Putting records for %s into the DHT\n", name);
1585 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1587 nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1
1588 + sizeof(struct GNSNameRecordBlock));
1590 if (signature != NULL)
1591 nrb->signature = *signature;
1593 nrb->public_key = *key;
1595 nrb->rd_count = htonl(rd_count);
1597 memset(&nrb[1], 0, strlen(name) + 1);
1598 memcpy(&nrb[1], name, strlen(name));
1600 nrb_data = (char*)&nrb[1];
1601 nrb_data += strlen(name) + 1;
1603 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
1606 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
1611 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
1618 * calculate DHT key: H(name) xor H(pubkey)
1620 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1621 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1622 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1623 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1624 "putting records for %s under key: %s with size %d\n",
1625 name, (char*)&xor_hash_string, rd_payload_length);
1627 GNUNET_DHT_put (dht_handle, &xor_hash,
1628 DHT_GNS_REPLICATION_LEVEL,
1630 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1634 DHT_OPERATION_TIMEOUT,
1636 NULL); //cls for cont
1638 num_public_records++;
1641 * Reschedule periodic put
1643 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1644 &update_zone_dht_next,
1652 * Periodically iterate over our zone and store everything in dht
1655 * @param tc task context
1658 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1660 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1661 if (0 == num_public_records)
1663 dht_update_interval = GNUNET_TIME_relative_multiply(
1664 GNUNET_TIME_UNIT_SECONDS,
1669 dht_update_interval = GNUNET_TIME_relative_multiply(
1670 GNUNET_TIME_UNIT_SECONDS,
1671 (3600/num_public_records));
1673 num_public_records = 0; //start counting again
1674 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1676 GNUNET_NAMESTORE_RF_AUTHORITY,
1677 GNUNET_NAMESTORE_RF_PRIVATE,
1683 static void send_shorten_response(const char* name,
1684 struct ClientShortenHandle *csh);
1686 process_shorten_pseu_lookup_ns(void *cls,
1687 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1688 struct GNUNET_TIME_Absolute expire,
1690 unsigned int rd_len,
1691 const struct GNUNET_NAMESTORE_RecordData *rd,
1692 const struct GNUNET_CRYPTO_RsaSignature *signature)
1694 struct GNUNET_GNS_ResolverHandle *rh =
1695 (struct GNUNET_GNS_ResolverHandle *)cls;
1696 struct GNUNET_TIME_Relative remaining_time;
1703 rh->status |= EXISTS;
1706 if (remaining_time.rel_value == 0)
1708 rh->status |= EXPIRED;
1711 rh->proc(rh->proc_cls, rh, rd_len, rd);
1716 * Start DHT lookup for a PSEUdonym record in
1717 * rh->authority's zone
1719 * @param rh the pending gns query
1720 * @param name the name of the PKEY record
1723 resolve_pseu_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
1726 GNUNET_HashCode name_hash;
1727 GNUNET_HashCode lookup_key;
1730 GNUNET_CRYPTO_hash("",
1734 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1736 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1737 &dht_lookup_timeout,
1740 xquery = htonl(GNUNET_GNS_RECORD_PSEU);
1742 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1743 DHT_OPERATION_TIMEOUT,
1744 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1746 DHT_GNS_REPLICATION_LEVEL,
1750 &process_authority_dht_result,
1757 handle_shorten_pseu_ns_result(void* cls,
1758 struct GNUNET_GNS_ResolverHandle *rh,
1760 const struct GNUNET_NAMESTORE_RecordData *rd);
1763 handle_shorten_zone_to_name(void *cls,
1764 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1765 struct GNUNET_TIME_Absolute expire,
1767 unsigned int rd_len,
1768 const struct GNUNET_NAMESTORE_RecordData *rd,
1769 const struct GNUNET_CRYPTO_RsaSignature *signature)
1771 struct GNUNET_GNS_ResolverHandle *rh =
1772 (struct GNUNET_GNS_ResolverHandle *)cls;
1773 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) rh->proc_cls;
1778 /* we found a match in our own zone */
1781 answer_len = strlen(rh->name) + strlen(name) + strlen(gnunet_tld) + 2;
1782 result = GNUNET_malloc(answer_len);
1783 memset(result, 0, answer_len);
1784 strcpy(result, rh->name);
1785 strcpy(result+strlen(rh->name), ".");
1786 strcpy(result+strlen(rh->name)+1, name);
1787 strcpy(result+strlen(rh->name)+strlen(name), gnunet_tld);
1789 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1790 "Sending shorten result %s\n", result);
1792 send_shorten_response(result, csh);
1793 free_resolver_handle(rh);
1794 GNUNET_free(result);
1799 * Nothing in our zone
1800 * check PSEU for this authority in namestore
1802 rh->proc = &handle_shorten_pseu_ns_result;
1803 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1804 &rh->authority_chain_head->zone,
1806 GNUNET_GNS_RECORD_PSEU,
1807 &process_shorten_pseu_lookup_ns,
1813 * Process result from namestore delegation lookup
1814 * for shorten operation
1816 * @param cls the client shorten handle
1817 * @param rh the resolver handle
1818 * @param rd_count number of results (0)
1819 * @param rd data (NULL)
1822 handle_shorten_pseu_dht_result(void* cls,
1823 struct GNUNET_GNS_ResolverHandle *rh,
1825 const struct GNUNET_NAMESTORE_RecordData *rd)
1827 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1828 struct AuthorityChain *auth_chain;
1840 for (i=0; i < rd_len; i++)
1842 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1844 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1845 "Found PSEU %s\n", (char*) rd[i].data);
1850 pseu = (char*) rd[i].data;
1851 answer_len = strlen(rh->name) + strlen(pseu) + strlen(gnunet_tld) + 2;
1852 result = GNUNET_malloc(answer_len);
1853 memset(result, 0, answer_len);
1854 strcpy(result, rh->name);
1855 strcpy(result+strlen(rh->name), ".");
1856 strcpy(result+strlen(rh->name)+1, pseu);
1857 strcpy(result+strlen(rh->name)+strlen(pseu), gnunet_tld);
1859 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1860 "Sending pseudonym shorten result %s\n", result);
1862 send_shorten_response(result, csh);
1863 free_resolver_handle(rh);
1864 GNUNET_free(result);
1870 * continue with next authority
1873 auth_chain = rh->authority_chain_head;
1875 if ((auth_chain->next->next == NULL) &&
1876 GNUNET_CRYPTO_hash_cmp(&auth_chain->next->zone, &zone_hash) == 0)
1881 answer_len = strlen(rh->name) + strlen(auth_chain->name)
1882 + strlen(gnunet_tld) + 2;
1884 result = GNUNET_malloc(answer_len);
1885 memset(result, 0, answer_len);
1886 strcpy(result, rh->name);
1887 strcpy(result+strlen(rh->name), ".");
1888 strcpy(result+strlen(rh->name)+1, auth_chain->name);
1889 strcpy(result+strlen(rh->name)+strlen(auth_chain->name)+1, gnunet_tld);
1890 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1891 "Sending non pseudonym shorten result %s\n", result);
1893 send_shorten_response(result, csh);
1894 free_resolver_handle(rh);
1895 GNUNET_free(result);
1900 * Continue with next authority
1902 new_name = GNUNET_malloc(strlen(rh->name)+
1903 strlen(auth_chain->name) + 2);
1904 memset(new_name, 0, strlen(rh->name)+
1905 strlen(auth_chain->name) + 2);
1906 strcpy(new_name, rh->name);
1907 strcpy(new_name+strlen(rh->name)+1, ".");
1908 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1909 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1910 rh->authority_chain_tail,
1912 GNUNET_free(rh->name);
1913 rh->name = new_name;
1914 GNUNET_free(auth_chain->name);
1915 GNUNET_free(auth_chain);
1916 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1918 &rh->authority_chain_head->zone,
1919 &handle_shorten_zone_to_name,
1927 * Process result from namestore PSEU lookup
1928 * for shorten operation
1929 * FIXME do we need to check for own zone here?
1931 * @param cls the client shorten handle
1932 * @param rh the resolver handle
1933 * @param rd_count number of results (0 if none found)
1934 * @param rd data (NULL if none found)
1937 handle_shorten_pseu_ns_result(void* cls,
1938 struct GNUNET_GNS_ResolverHandle *rh,
1940 const struct GNUNET_NAMESTORE_RecordData *rd)
1942 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1943 struct AuthorityChain *auth_chain;
1955 for (i=0; i < rd_len; i++)
1957 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1959 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1960 "Found PSEU %s\n", (char*) rd[i].data);
1965 pseu = (char*) rd[i].data;
1966 answer_len = strlen(rh->name) + strlen(pseu) + strlen(gnunet_tld) + 2;
1967 result = GNUNET_malloc(answer_len);
1968 memset(result, 0, answer_len);
1969 strcpy(result, rh->name);
1970 strcpy(result+strlen(rh->name), ".");
1971 strcpy(result+strlen(rh->name)+1, pseu);
1972 strcpy(result+strlen(rh->name)+strlen(pseu)+1, gnunet_tld);
1974 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1975 "Sending shorten result %s\n", result);
1977 send_shorten_response(result, csh);
1978 free_resolver_handle(rh);
1979 GNUNET_free(result);
1984 * No PSEU found. Ask DHT if expired.
1985 * Else contunue with next authority
1987 if (rh->status & (EXISTS | !EXPIRED))
1992 auth_chain = rh->authority_chain_head;
1993 new_name = GNUNET_malloc(strlen(rh->name)+
1994 strlen(auth_chain->name) + 2);
1995 memset(new_name, 0, strlen(rh->name)+
1996 strlen(auth_chain->name) + 2);
1997 strcpy(new_name, rh->name);
1998 strcpy(new_name+strlen(rh->name)+1, ".");
1999 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
2001 GNUNET_free(rh->name);
2002 rh->name = new_name;
2003 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
2004 rh->authority_chain_tail,
2007 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2009 &rh->authority_chain_head->zone,
2010 &handle_shorten_zone_to_name,
2018 rh->authority = rh->authority_chain_head->zone;
2019 rh->proc = &handle_shorten_pseu_dht_result;
2020 resolve_pseu_from_dht(rh);
2027 * Process result from namestore delegation lookup
2028 * for shorten operation
2030 * @param cls the client shorten handle
2031 * @param rh the resolver handle
2032 * @param rd_count number of results (0)
2033 * @param rd data (NULL)
2036 handle_shorten_delegation_result(void* cls,
2037 struct GNUNET_GNS_ResolverHandle *rh,
2039 const struct GNUNET_NAMESTORE_RecordData *rd)
2041 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
2042 struct AuthorityChain *auth_chain;
2047 * At this point rh->name contains the part of the name
2048 * that we do not have a PKEY in our namestore to resolve.
2049 * The authority chain in the resolver handle is now
2050 * useful to backtrack if needed
2053 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2054 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2056 if (GNUNET_CRYPTO_hash_cmp(&rh->authority_chain_head->zone,
2060 * This is our zone append .gnunet unless name is empty
2061 * (it shouldn't be, usually FIXME what happens if we
2062 * shorten to our zone to a "" record??)
2065 answer_len = strlen(rh->name) + strlen(gnunet_tld) + 2;
2066 result = GNUNET_malloc(answer_len);
2067 memset(result, 0, answer_len);
2068 strcpy(result, rh->name);
2069 strcpy(result+strlen(rh->name), ".");
2070 strcpy(result+strlen(rh->name)+1, gnunet_tld);
2072 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2073 "Our zone: Sending name as shorten result %s\n", rh->name);
2075 send_shorten_response(rh->name, csh); //FIXME +.gnunet!
2076 free_resolver_handle(rh);
2077 GNUNET_free(csh->name);
2079 GNUNET_free(result);
2083 auth_chain = rh->authority_chain_head;
2084 /* backtrack authorities for pseu */
2085 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2088 &handle_shorten_zone_to_name,
2093 typedef void (*ShortenResponseProc) (void* cls, const char* name);
2096 * Shorten a given name
2098 * @param name the name to shorten
2099 * @param proc the processor to call when finished
2100 * @praram cls the closure to the processor
2103 shorten_name(char* name, struct ClientShortenHandle* csh)
2106 struct GNUNET_GNS_ResolverHandle *rh;
2108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2109 "Starting resolution for %s (type=%d)!\n",
2110 name, GNUNET_GNS_RECORD_PKEY);
2112 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
2113 rh->authority = zone_hash;
2115 rh->name = GNUNET_malloc(strlen(name)
2116 - strlen(gnunet_tld) + 1);
2118 strlen(name)-strlen(gnunet_tld) + 1);
2119 memcpy(rh->name, name,
2120 strlen(name)-strlen(gnunet_tld));
2122 csh->name = GNUNET_malloc(strlen(name)
2123 - strlen(gnunet_tld) + 1);
2125 strlen(name)-strlen(gnunet_tld) + 1);
2126 memcpy(rh->name, name,
2127 strlen(name)-strlen(gnunet_tld));
2129 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
2131 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2132 rh->authority_chain_tail = rh->authority_chain_head;
2133 rh->authority_chain_head->zone = zone_hash;
2134 rh->proc = &handle_shorten_delegation_result;
2135 rh->proc_cls = (void*)csh;
2137 /* Start delegation resolution in our namestore */
2138 resolve_delegation_from_ns(rh);
2143 * Send shorten response back to client
2144 * FIXME this is without .gnunet!
2146 * @param cls the client handle in closure
2147 * @param name the shortened name result or NULL if cannot be shortened
2150 send_shorten_response(const char* name, struct ClientShortenHandle *csh)
2152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
2153 "SHORTEN_RESULT", name);
2154 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
2161 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
2162 + strlen(name) + 1);
2164 rmsg->id = csh->unique_id;
2165 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
2167 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
2170 strcpy((char*)&rmsg[1], name);
2172 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
2173 (const struct GNUNET_MessageHeader *) rmsg,
2175 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
2183 * Handle a shorten message from the api
2185 * @param cls the closure
2186 * @param client the client
2187 * @param message the message
2189 static void handle_shorten(void *cls,
2190 struct GNUNET_SERVER_Client * client,
2191 const struct GNUNET_MessageHeader * message)
2193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
2195 size_t msg_size = 0;
2196 struct ClientShortenHandle *csh;
2198 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
2200 GNUNET_break_op (0);
2201 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2205 GNUNET_SERVER_notification_context_add (nc, client);
2207 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
2208 (struct GNUNET_GNS_ClientShortenMessage *) message;
2210 msg_size = ntohs(message->size);
2212 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2214 GNUNET_break_op (0);
2215 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2219 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
2220 csh->client = client;
2221 csh->unique_id = sh_msg->id;
2223 shorten_name((char*)&sh_msg[1], csh);
2231 handle_lookup(void *cls,
2232 struct GNUNET_SERVER_Client * client,
2233 const struct GNUNET_MessageHeader * message)
2238 * Process GNS requests.
2240 * @param cls closure)
2241 * @param server the initialized server
2242 * @param c configuration to use
2245 run (void *cls, struct GNUNET_SERVER_Handle *server,
2246 const struct GNUNET_CONFIGURATION_Handle *c)
2249 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
2252 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2254 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2255 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
2256 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0}
2259 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
2260 "ZONEKEY", &keyfile))
2262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2263 "No private key for root zone specified%s!\n", keyfile);
2264 GNUNET_SCHEDULER_shutdown(0);
2268 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2269 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
2271 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2273 GNUNET_free(keyfile);
2276 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
2279 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2280 "DNS hijacking enabled... connecting to service.\n");
2282 * Do gnunet dns init here
2284 dns_handle = GNUNET_DNS_connect(c,
2285 GNUNET_DNS_FLAG_PRE_RESOLUTION,
2286 &handle_dns_request, /* rh */
2287 NULL); /* Closure */
2288 if (NULL == dns_handle)
2290 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2291 "Failed to connect to the dnsservice!\n");
2298 * handle to our local namestore
2300 namestore_handle = GNUNET_NAMESTORE_connect(c);
2302 if (NULL == namestore_handle)
2304 //FIXME do error handling;
2305 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2306 "Failed to connect to the namestore!\n");
2307 GNUNET_SCHEDULER_shutdown(0);
2314 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
2316 if (NULL == dht_handle)
2318 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
2321 //put_some_records(); //FIXME for testing
2324 * Schedule periodic put
2326 * We have roughly an hour for all records;
2328 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
2330 //zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
2332 GNUNET_SERVER_add_handlers (server, handlers);
2335 //GNUNET_SERVER_disconnect_notify (server,
2336 // &client_disconnect_notification,
2339 nc = GNUNET_SERVER_notification_context_create (server, 1);
2341 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
2347 * The main function for the GNS service.
2349 * @param argc number of arguments from the command line
2350 * @param argv command line arguments
2351 * @return 0 ok, 1 on error
2354 main (int argc, char *const *argv)
2360 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
2365 /* end of gnunet-service-gns.c */