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)
402 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
404 /* Kill zone task for it may make the scheduler hang */
405 if (zone_update_taskid)
406 GNUNET_SCHEDULER_cancel(zone_update_taskid);
408 GNUNET_SERVER_notification_context_destroy (nc);
409 GNUNET_DNS_disconnect(dns_handle);
410 GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
411 GNUNET_DHT_disconnect(dht_handle);
415 * Callback when record data is put into namestore
417 * @param cls the closure
418 * @param success GNUNET_OK on success
419 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
422 on_namestore_record_put_result(void *cls,
426 if (GNUNET_NO == success)
428 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
431 else if (GNUNET_YES == success)
433 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
434 "records successfully put in namestore\n");
438 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
439 "Error putting records into namestore: %s\n", emsg);
443 * Handle timeout for DHT requests
445 * @param cls the request handle as closure
446 * @param tc the task context
449 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
451 struct GNUNET_GNS_ResolverHandle *rh = cls;
453 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
454 "dht lookup for query %s timed out.\n",
457 GNUNET_DHT_get_stop (rh->get_handle);
458 rh->proc(rh->proc_cls, rh, 0, NULL);
464 * Function called when we get a result from the dht
467 * @param cls the request handle
468 * @param exp lifetime
469 * @param key the key the record was stored under
470 * @param get_path get path
471 * @param get_path_length get path length
472 * @param put_path put path
473 * @param put_path_length put path length
474 * @param type the block type
475 * @param size the size of the record
476 * @param data the record data
479 process_record_dht_result(void* cls,
480 struct GNUNET_TIME_Absolute exp,
481 const GNUNET_HashCode * key,
482 const struct GNUNET_PeerIdentity *get_path,
483 unsigned int get_path_length,
484 const struct GNUNET_PeerIdentity *put_path,
485 unsigned int put_path_length,
486 enum GNUNET_BLOCK_Type type,
487 size_t size, const void *data)
489 struct GNUNET_GNS_ResolverHandle *rh;
490 struct RecordLookupHandle *rlh;
491 struct GNSNameRecordBlock *nrb;
492 uint32_t num_records;
494 char* rd_data = (char*)data;
498 GNUNET_HashCode zone, name_hash;
499 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
504 //FIXME maybe check expiration here, check block type
506 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
507 rlh = (struct RecordLookupHandle *) rh->proc_cls;
508 nrb = (struct GNSNameRecordBlock*)data;
510 /* stop lookup and timeout task */
511 GNUNET_DHT_get_stop (rh->get_handle);
512 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
514 rh->get_handle = NULL;
515 name = (char*)&nrb[1];
516 num_records = ntohl(nrb->rd_count);
518 struct GNUNET_NAMESTORE_RecordData rd[num_records];
520 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
521 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
523 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
528 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
532 for (i=0; i<num_records; i++)
534 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
535 "Got name: %s (wanted %s)\n", name, rh->name);
536 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
539 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
540 "Got data length: %d\n", rd[i].data_size);
541 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
542 "Got flag %d\n", rd[i].flags);
544 if ((strcmp(name, rh->name) == 0) &&
545 (rd[i].record_type == rlh->record_type))
552 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
553 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
556 * FIXME check pubkey against existing key in namestore?
557 * https://gnunet.org/bugs/view.php?id=2179
560 /* Save to namestore */
561 GNUNET_NAMESTORE_record_put (namestore_handle,
568 &on_namestore_record_put_result, //cont
572 rh->proc(rh->proc_cls, rh, num_records, rd);
574 rh->proc(rh->proc_cls, rh, 0, NULL);
581 * Start DHT lookup for a (name -> query->record_type) record in
582 * rh->authority's zone
584 * @param rh the pending gns query context
585 * @param name the name to query record
588 resolve_record_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
591 GNUNET_HashCode name_hash;
592 GNUNET_HashCode lookup_key;
593 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
594 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
596 GNUNET_CRYPTO_hash(rh->name, strlen(rh->name), &name_hash);
597 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
598 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
600 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
601 "starting dht lookup for %s with key: %s\n",
602 rh->name, (char*)&lookup_key_string);
604 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
605 &dht_lookup_timeout, rh);
607 xquery = htonl(rlh->record_type);
608 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
609 DHT_OPERATION_TIMEOUT,
610 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
612 DHT_GNS_REPLICATION_LEVEL,
616 &process_record_dht_result,
623 * Namestore calls this function if we have record for this name.
624 * (or with rd_count=0 to indicate no matches)
626 * @param cls the pending query
627 * @param key the key of the zone we did the lookup
628 * @param expiration expiration date of the namestore entry
629 * @param name the name for which we need an authority
630 * @param rd_count the number of records with 'name'
631 * @param rd the record data
632 * @param signature the signature of the authority for the record data
635 process_record_lookup_ns(void* cls,
636 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
637 struct GNUNET_TIME_Absolute expiration,
638 const char *name, unsigned int rd_count,
639 const struct GNUNET_NAMESTORE_RecordData *rd,
640 const struct GNUNET_CRYPTO_RsaSignature *signature)
642 struct GNUNET_GNS_ResolverHandle *rh;
643 struct RecordLookupHandle *rlh;
644 struct GNUNET_TIME_Relative remaining_time;
645 GNUNET_HashCode zone;
647 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
648 rlh = (struct RecordLookupHandle *)rh->proc_cls;
649 GNUNET_CRYPTO_hash(key,
650 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
652 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
658 rh->status |= EXISTS;
661 if (remaining_time.rel_value == 0)
663 rh->status |= EXPIRED;
669 * Lookup terminated and no results
671 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
672 "Namestore lookup for %s terminated without results\n", name);
676 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
677 "Record %s unknown in namestore\n",
680 * Our zone and no result? Cannot resolve TT
682 rh->proc(rh->proc_cls, rh, 0, NULL);
689 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
690 "Processing additional result %s from namestore\n", name);
692 for (i=0; i<rd_count;i++)
695 if (rd[i].record_type != rlh->record_type)
698 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
701 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
712 if (rh->answered == 0)
714 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
715 "No answers found. This is odd!\n");
716 rh->proc(rh->proc_cls, rh, 0, NULL);
720 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
723 rh->proc(rh->proc_cls, rh, rd_count, rd);
728 * The final phase of resolution.
729 * This is a name that is canonical and we do not have a delegation.
731 * @param rh the pending lookup
734 resolve_record_from_ns(struct GNUNET_GNS_ResolverHandle *rh)
736 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
738 * Try to resolve this record in our namestore.
739 * The name to resolve is now in rh->authority_name
740 * since we tried to resolve it to an authority
743 GNUNET_NAMESTORE_lookup_record(namestore_handle,
747 &process_record_lookup_ns,
754 * Handle timeout for DHT requests
756 * @param cls the request handle as closure
757 * @param tc the task context
760 dht_authority_lookup_timeout(void *cls,
761 const struct GNUNET_SCHEDULER_TaskContext *tc)
763 struct GNUNET_GNS_ResolverHandle *rh = cls;
765 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
766 "dht lookup for query %s timed out.\n",
769 GNUNET_DHT_get_stop (rh->get_handle);
770 if (strcmp(rh->name, "") == 0)
773 * promote authority back to name and try to resolve record
775 strcpy(rh->name, rh->authority_name);
777 rh->proc(rh->proc_cls, rh, 0, NULL);
781 static void resolve_delegation_from_dht(struct GNUNET_GNS_ResolverHandle *rh);
784 * Function called when we get a result from the dht
785 * for our query. Recursively tries to resolve PKEYs
788 * @param cls the request handle
789 * @param exp lifetime
790 * @param key the key the record was stored under
791 * @param get_path get path
792 * @param get_path_length get path length
793 * @param put_path put path
794 * @param put_path_length put path length
795 * @param type the block type
796 * @param size the size of the record
797 * @param data the record data
800 process_authority_dht_result(void* cls,
801 struct GNUNET_TIME_Absolute exp,
802 const GNUNET_HashCode * key,
803 const struct GNUNET_PeerIdentity *get_path,
804 unsigned int get_path_length,
805 const struct GNUNET_PeerIdentity *put_path,
806 unsigned int put_path_length,
807 enum GNUNET_BLOCK_Type type,
808 size_t size, const void *data)
810 struct GNUNET_GNS_ResolverHandle *rh;
811 struct GNSNameRecordBlock *nrb;
812 uint32_t num_records;
814 char* rd_data = (char*) data;
817 GNUNET_HashCode zone, name_hash;
822 //FIXME check expiration?
824 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
825 nrb = (struct GNSNameRecordBlock*)data;
827 /* stop dht lookup and timeout task */
828 GNUNET_DHT_get_stop (rh->get_handle);
829 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
831 rh->get_handle = NULL;
832 num_records = ntohl(nrb->rd_count);
833 name = (char*)&nrb[1];
835 struct GNUNET_NAMESTORE_RecordData rd[num_records];
837 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
838 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
840 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
845 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
849 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
850 "Got name: %s (wanted %s)\n", name, rh->authority_name);
851 for (i=0; i<num_records; i++)
854 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
855 "Got name: %s (wanted %s)\n", name, rh->authority_name);
856 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
857 "Got type: %d (wanted %d)\n",
858 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
859 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
860 "Got data length: %d\n", rd[i].data_size);
861 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
862 "Got flag %d\n", rd[i].flags);
864 if ((strcmp(name, rh->authority_name) == 0) &&
865 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
867 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
869 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
870 struct AuthorityChain *auth =
871 GNUNET_malloc(sizeof(struct AuthorityChain));
872 auth->zone = rh->authority;
873 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
874 memset(auth->name, 0, strlen(rh->authority_name)+1);
875 strcpy(auth->name, rh->authority_name);
876 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
877 rh->authority_chain_tail,
884 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
885 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
887 /* Save to namestore */
888 if (0 != GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
890 GNUNET_NAMESTORE_record_put (namestore_handle,
897 &on_namestore_record_put_result, //cont
906 if (strcmp(rh->name, "") == 0)
907 rh->proc(rh->proc_cls, rh, 0, NULL);
909 resolve_delegation_from_dht(rh);
914 * should never get here unless false dht key/put
915 * block plugin should handle this
918 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DHT authority lookup error!\n");
924 * Process DHT lookup result for record.
926 * @param cls the closure
927 * @param rh resolver handle
928 * @param rd_count number of results (always 0)
929 * @param rd record data (always NULL)
932 process_record_result_dht(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
933 unsigned int rd_count,
934 const struct GNUNET_NAMESTORE_RecordData *rd)
936 struct RecordLookupHandle* rlh;
937 rlh = (struct RecordLookupHandle*)cls;
940 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
941 "No records for %s found in DHT. Aborting\n",
943 /* give up, cannot resolve */
944 rlh->proc(rlh->proc_cls, rh, 0, NULL);
945 //reply_to_dns(NULL, rh, 0, NULL);
949 /* results found yay */
950 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
951 "Record resolved from namestore!");
952 rlh->proc(rlh->proc_cls, rh, rd_count, rd);
953 //reply_to_dns(NULL, rh, rd_count, rd);
959 * Process namestore lookup result for record.
961 * @param cls the closure
962 * @param rh resolver handle
963 * @param rd_count number of results (always 0)
964 * @param rd record data (always NULL)
967 process_record_result_ns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
968 unsigned int rd_count,
969 const struct GNUNET_NAMESTORE_RecordData *rd)
971 struct RecordLookupHandle* rlh;
972 rlh = (struct RecordLookupHandle*) cls;
975 /* ns entry expired. try dht */
976 if (rh->status & (EXPIRED | !EXISTS))
978 rh->proc = &process_record_result_dht;
979 resolve_record_from_dht(rh);
982 /* give up, cannot resolve */
983 rlh->proc(rlh->proc_cls, rh, 0, NULL);
984 //reply_to_dns(NULL, rh, 0, NULL);
988 /* results found yay */
989 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
990 "Record resolved from namestore!");
991 rlh->proc(rlh->proc_cls, rh, rd_count, rd);
992 //reply_to_dns(NULL, rh, rd_count, rd);
998 * Determine if this name is canonical.
1000 * a.b.gnunet = not canonical
1003 * @param name the name to test
1004 * @return 1 if canonical
1007 is_canonical(char* name)
1009 uint32_t len = strlen(name);
1012 for (i=0; i<len; i++)
1014 if (*(name+i) == '.')
1021 * Move one level up in the domain hierarchy and return the
1022 * passed top level domain.
1024 * @param name the domain
1025 * @param dest the destination where the tld will be put
1028 pop_tld(char* name, char* dest)
1032 if (is_canonical(name))
1039 for (len = strlen(name); len > 0; len--)
1041 if (*(name+len) == '.')
1051 strcpy(dest, (name+len+1));
1055 * DHT resolution for delegation finished. Processing result.
1057 * @param cls the closure
1058 * @param rh resolver handle
1059 * @param rd_count number of results (always 0)
1060 * @param rd record data (always NULL)
1063 process_dht_delegation_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1064 unsigned int rd_count,
1065 const struct GNUNET_NAMESTORE_RecordData *rd)
1067 struct RecordLookupHandle* rlh;
1068 rlh = (struct RecordLookupHandle*) cls;
1070 if (strcmp(rh->name, "") == 0)
1072 /* We resolved full name for delegation. resolving record */
1073 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1074 "Resolved full name for delegation via DHT. resolving record '' in ns\n");
1075 rh->proc = &process_record_result_ns;
1076 resolve_record_from_ns(rh);
1081 * we still have some left
1083 if (is_canonical(rh->name))
1085 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1086 "Resolving canonical record %s in ns\n", rh->name);
1087 rh->proc = &process_record_result_ns;
1088 resolve_record_from_ns(rh);
1091 /* give up, cannot resolve */
1092 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1093 "Cannot fully resolve delegation for %s via DHT!\n",
1095 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1096 //reply_to_dns(NULL, rh, 0, NULL);
1101 * Start DHT lookup for a name -> PKEY (compare NS) record in
1102 * rh->authority's zone
1104 * @param rh the pending gns query
1105 * @param name the name of the PKEY record
1108 resolve_delegation_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
1111 GNUNET_HashCode name_hash;
1112 GNUNET_HashCode lookup_key;
1114 GNUNET_CRYPTO_hash(rh->authority_name,
1115 strlen(rh->authority_name),
1117 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1119 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1120 &dht_authority_lookup_timeout,
1123 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1125 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1126 DHT_OPERATION_TIMEOUT,
1127 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1129 DHT_GNS_REPLICATION_LEVEL,
1133 &process_authority_dht_result,
1140 * Namestore resolution for delegation finished. Processing result.
1142 * @param cls the closure
1143 * @param rh resolver handle
1144 * @param rd_count number of results (always 0)
1145 * @param rd record data (always NULL)
1148 process_ns_delegation_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1149 unsigned int rd_count,
1150 const struct GNUNET_NAMESTORE_RecordData *rd)
1152 struct RecordLookupHandle* rlh;
1153 rlh = (struct RecordLookupHandle*) cls;
1155 if (strcmp(rh->name, "") == 0)
1157 /* We resolved full name for delegation. resolving record */
1158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1159 "Resolved full name for delegation. resolving record ''\n");
1160 rh->proc = &process_record_result_ns;
1161 resolve_record_from_ns(rh);
1166 * we still have some left
1167 * check if ns entry is fresh
1169 if (rh->status & (EXISTS | !EXPIRED))
1171 if (is_canonical(rh->name))
1173 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1174 "Resolving canonical record %s\n", rh->name);
1175 rh->proc = &process_record_result_ns;
1176 resolve_record_from_ns(rh);
1180 /* give up, cannot resolve */
1181 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1182 "Cannot fully resolve delegation for %s!\n",
1184 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1185 //reply_to_dns(NULL, rh, 0, NULL);
1190 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1191 "Trying to resolve delegation for %s via DHT\n",
1193 rh->proc = &process_dht_delegation_dns;
1194 resolve_delegation_from_dht(rh);
1198 static void resolve_delegation_from_ns(struct GNUNET_GNS_ResolverHandle *rh);
1201 * This is a callback function that should give us only PKEY
1202 * records. Used to query the namestore for the authority (PKEY)
1203 * for 'name'. It will recursively try to resolve the
1204 * authority for a given name from the namestore.
1206 * @param cls the pending query
1207 * @param key the key of the zone we did the lookup
1208 * @param expiration expiration date of the record data set in the namestore
1209 * @param name the name for which we need an authority
1210 * @param rd_count the number of records with 'name'
1211 * @param rd the record data
1212 * @param signature the signature of the authority for the record data
1215 process_authority_lookup_ns(void* cls,
1216 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1217 struct GNUNET_TIME_Absolute expiration,
1219 unsigned int rd_count,
1220 const struct GNUNET_NAMESTORE_RecordData *rd,
1221 const struct GNUNET_CRYPTO_RsaSignature *signature)
1223 struct GNUNET_GNS_ResolverHandle *rh;
1224 struct GNUNET_TIME_Relative remaining_time;
1225 GNUNET_HashCode zone;
1228 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %d records from authority lookup\n",
1231 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
1232 GNUNET_CRYPTO_hash(key,
1233 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1235 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1241 rh->status |= EXISTS;
1244 if (remaining_time.rel_value == 0)
1246 rh->status |= EXPIRED;
1250 * No authority found in namestore.
1255 * We did not find an authority in the namestore
1260 * Promote this authority back to a name maybe it is
1263 if (strcmp(rh->name, "") == 0)
1265 /* simply promote back */
1266 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1267 "Promoting %s back to name\n", rh->authority_name);
1268 strcpy(rh->name, rh->authority_name);
1272 /* add back to existing name */
1273 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1274 "Adding %s back to %s\n",
1275 rh->authority_name, rh->name);
1276 new_name = GNUNET_malloc(strlen(rh->name)
1277 + strlen(rh->authority_name) + 2);
1278 memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1279 strcpy(new_name, rh->name);
1280 strcpy(new_name+strlen(new_name)+1, ".");
1281 strcpy(new_name+strlen(new_name)+2, rh->authority_name);
1282 GNUNET_free(rh->name);
1283 rh->name = new_name;
1285 rh->proc(rh->proc_cls, rh, 0, NULL);
1289 //Note only 1 pkey should have been returned.. anything else would be strange
1291 * We found an authority that may be able to help us
1292 * move on with query
1295 for (i=0; i<rd_count;i++)
1298 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1301 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1304 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
1305 if (remaining_time.rel_value == 0)
1307 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1308 "This dht entry is expired.\n");
1309 rh->authority_chain_head->fresh = 0;
1310 rh->proc(rh->proc_cls, rh, 0, NULL);
1318 * Resolve rest of query with new authority
1320 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1321 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
1322 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1323 auth->zone = rh->authority;
1324 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
1325 memset(auth->name, 0, strlen(rh->authority_name)+1);
1326 strcpy(auth->name, rh->authority_name);
1327 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1328 rh->authority_chain_tail,
1332 * We are done with PKEY resolution if name is empty
1333 * else resolve again with new authority
1335 if (strcmp(rh->name, "") == 0)
1336 rh->proc(rh->proc_cls, rh, 0, NULL);
1338 resolve_delegation_from_ns(rh);
1345 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1346 "Authority lookup successful but no PKEY... never get here\n");
1347 rh->proc(rh->proc_cls, rh, 0, NULL);
1352 * Resolve the delegation chain for the request
1354 * @param rh the resolver handle
1357 resolve_delegation_from_ns(struct GNUNET_GNS_ResolverHandle *rh)
1360 pop_tld(rh->name, rh->authority_name);
1361 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1364 GNUNET_GNS_RECORD_PKEY,
1365 &process_authority_lookup_ns,
1371 * Entry point for name resolution
1372 * Setup a new query and try to resolve
1374 * @param request the request handle of the DNS request from a client
1375 * @param p the DNS query packet we received
1376 * @param q the DNS query we received parsed from p
1379 start_resolution_from_dns(struct GNUNET_DNS_RequestHandle *request,
1380 struct GNUNET_DNSPARSER_Packet *p,
1381 struct GNUNET_DNSPARSER_Query *q)
1383 struct GNUNET_GNS_ResolverHandle *rh;
1384 struct RecordLookupHandle* rlh;
1385 struct InterceptLookupHandle* ilh;
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "Starting resolution for %s (type=%d)!\n",
1391 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
1392 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
1393 ilh = GNUNET_malloc(sizeof(struct InterceptLookupHandle));
1396 ilh->request_handle = request;
1398 rh->authority = zone_hash;
1400 rlh->record_type = q->type;
1401 rlh->name = q->name;
1402 rlh->proc = &reply_to_dns;
1403 rlh->proc_cls = ilh;
1407 rh->authority = zone_hash;
1408 rh->name = GNUNET_malloc(strlen(q->name)
1409 - strlen(gnunet_tld) + 1);
1411 strlen(q->name)-strlen(gnunet_tld) + 1);
1412 memcpy(rh->name, q->name,
1413 strlen(q->name)-strlen(gnunet_tld));
1415 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
1417 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
1418 rh->authority_chain_head->prev = NULL;
1419 rh->authority_chain_head->next = NULL;
1420 rh->authority_chain_tail = rh->authority_chain_head;
1421 rh->authority_chain_head->zone = zone_hash;
1423 /* Start resolution in our zone */
1424 rh->proc = &process_ns_delegation_dns;
1425 resolve_delegation_from_ns(rh);
1431 * The DNS request handler
1432 * Called for every incoming DNS request.
1434 * @param cls closure
1435 * @param rh request handle to user for reply
1436 * @param request_length number of bytes in request
1437 * @param request udp payload of the DNS request
1440 handle_dns_request(void *cls,
1441 struct GNUNET_DNS_RequestHandle *rh,
1442 size_t request_length,
1443 const char *request)
1445 struct GNUNET_DNSPARSER_Packet *p;
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
1450 p = GNUNET_DNSPARSER_parse (request, request_length);
1454 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1455 "Received malformed DNS packet, leaving it untouched\n");
1456 GNUNET_DNS_request_forward (rh);
1457 GNUNET_DNSPARSER_free_packet (p);
1462 * Check tld and decide if we or
1463 * legacy dns is responsible
1465 * FIXME now in theory there could be more than 1 query in the request
1466 * but if this is case we get into trouble:
1467 * either we query the GNS or the DNS. We cannot do both!
1468 * So I suggest to either only allow a single query per request or
1469 * only allow GNS or DNS requests.
1470 * The way it is implemented here now is buggy and will lead to erratic
1471 * behaviour (if multiple queries are present).
1473 if (p->num_queries == 0)
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476 "No Queries in DNS packet... forwarding\n");
1477 GNUNET_DNS_request_forward (rh);
1478 GNUNET_DNSPARSER_free_packet(p);
1482 if (p->num_queries > 1)
1484 /* Note: We could also look for .gnunet */
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1486 ">1 queriy in DNS packet... odd. We only process #1\n");
1493 tldoffset = p->queries[0].name + strlen(p->queries[0].name) - 1;
1495 for (i=0; i<strlen(p->queries[0].name); i++)
1497 if (*(tldoffset-i) == '.')
1501 if ((i==strlen(gnunet_tld)-1) && (0 == strcmp(tldoffset-i, gnunet_tld)))
1503 start_resolution_from_dns(rh, p, p->queries);
1508 * This request does not concern us. Forward to real DNS.
1510 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1511 "Request for %s is forwarded to DNS\n", p->queries[0].name);
1512 GNUNET_DNS_request_forward (rh);
1513 GNUNET_DNSPARSER_free_packet (p);
1519 * Method called periodicattluy that triggers
1520 * iteration over root zone
1522 * @param cls closure
1523 * @param tc task context
1526 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1528 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1532 * Continuation for DHT put
1534 * @param cls closure
1535 * @param tc task context
1538 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1540 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1545 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1548 * Function used to put all records successively into the DHT.
1550 * @param cls the closure (NULL)
1551 * @param key the public key of the authority (ours)
1552 * @param expiration lifetime of the namestore entry
1553 * @param name the name of the records
1554 * @param rd_count the number of records in data
1555 * @param rd the record data
1556 * @param signature the signature for the record data
1559 put_gns_record(void *cls,
1560 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1561 struct GNUNET_TIME_Absolute expiration,
1563 unsigned int rd_count,
1564 const struct GNUNET_NAMESTORE_RecordData *rd,
1565 const struct GNUNET_CRYPTO_RsaSignature *signature)
1568 struct GNSNameRecordBlock *nrb;
1569 GNUNET_HashCode name_hash;
1570 GNUNET_HashCode xor_hash;
1571 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1572 uint32_t rd_payload_length;
1573 char* nrb_data = NULL;
1578 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1579 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1580 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1585 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1586 "Putting records for %s into the DHT\n", name);
1588 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1590 nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1
1591 + sizeof(struct GNSNameRecordBlock));
1593 if (signature != NULL)
1594 nrb->signature = *signature;
1596 nrb->public_key = *key;
1598 nrb->rd_count = htonl(rd_count);
1600 memset(&nrb[1], 0, strlen(name) + 1);
1601 memcpy(&nrb[1], name, strlen(name));
1603 nrb_data = (char*)&nrb[1];
1604 nrb_data += strlen(name) + 1;
1606 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
1609 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
1614 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
1621 * calculate DHT key: H(name) xor H(pubkey)
1623 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1624 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1625 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1626 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1627 "putting records for %s under key: %s with size %d\n",
1628 name, (char*)&xor_hash_string, rd_payload_length);
1630 GNUNET_DHT_put (dht_handle, &xor_hash,
1631 DHT_GNS_REPLICATION_LEVEL,
1633 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1637 DHT_OPERATION_TIMEOUT,
1639 NULL); //cls for cont
1641 num_public_records++;
1644 * Reschedule periodic put
1646 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1647 &update_zone_dht_next,
1655 * Periodically iterate over our zone and store everything in dht
1658 * @param tc task context
1661 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1663 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1664 if (0 == num_public_records)
1666 dht_update_interval = GNUNET_TIME_relative_multiply(
1667 GNUNET_TIME_UNIT_SECONDS,
1672 dht_update_interval = GNUNET_TIME_relative_multiply(
1673 GNUNET_TIME_UNIT_SECONDS,
1674 (3600/num_public_records));
1676 num_public_records = 0; //start counting again
1677 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1679 GNUNET_NAMESTORE_RF_AUTHORITY,
1680 GNUNET_NAMESTORE_RF_PRIVATE,
1686 static void send_shorten_response(const char* name,
1687 struct ClientShortenHandle *csh);
1689 process_shorten_pseu_lookup_ns(void *cls,
1690 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1691 struct GNUNET_TIME_Absolute expire,
1693 unsigned int rd_len,
1694 const struct GNUNET_NAMESTORE_RecordData *rd,
1695 const struct GNUNET_CRYPTO_RsaSignature *signature)
1697 struct GNUNET_GNS_ResolverHandle *rh =
1698 (struct GNUNET_GNS_ResolverHandle *)cls;
1699 struct GNUNET_TIME_Relative remaining_time;
1706 rh->status |= EXISTS;
1709 if (remaining_time.rel_value == 0)
1711 rh->status |= EXPIRED;
1714 rh->proc(rh->proc_cls, rh, rd_len, rd);
1719 * Start DHT lookup for a PSEUdonym record in
1720 * rh->authority's zone
1722 * @param rh the pending gns query
1723 * @param name the name of the PKEY record
1726 resolve_pseu_from_dht(struct GNUNET_GNS_ResolverHandle *rh)
1729 GNUNET_HashCode name_hash;
1730 GNUNET_HashCode lookup_key;
1733 GNUNET_CRYPTO_hash("",
1737 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1739 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1740 &dht_lookup_timeout,
1743 xquery = htonl(GNUNET_GNS_RECORD_PSEU);
1745 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1746 DHT_OPERATION_TIMEOUT,
1747 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1749 DHT_GNS_REPLICATION_LEVEL,
1753 &process_authority_dht_result,
1760 handle_shorten_pseu_ns_result(void* cls,
1761 struct GNUNET_GNS_ResolverHandle *rh,
1763 const struct GNUNET_NAMESTORE_RecordData *rd);
1766 handle_shorten_zone_to_name(void *cls,
1767 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1768 struct GNUNET_TIME_Absolute expire,
1770 unsigned int rd_len,
1771 const struct GNUNET_NAMESTORE_RecordData *rd,
1772 const struct GNUNET_CRYPTO_RsaSignature *signature)
1774 struct GNUNET_GNS_ResolverHandle *rh =
1775 (struct GNUNET_GNS_ResolverHandle *)cls;
1776 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) rh->proc_cls;
1781 /* we found a match in our own zone */
1784 answer_len = strlen(rh->name) + strlen(name) + strlen(gnunet_tld) + 2;
1785 result = GNUNET_malloc(answer_len);
1786 memset(result, 0, answer_len);
1787 strcpy(result, rh->name);
1788 strcpy(result+strlen(rh->name), ".");
1789 strcpy(result+strlen(rh->name)+1, name);
1790 strcpy(result+strlen(rh->name)+strlen(name), gnunet_tld);
1792 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1793 "Sending shorten result %s\n", result);
1795 send_shorten_response(result, csh);
1796 free_resolver_handle(rh);
1797 GNUNET_free(result);
1802 * Nothing in our zone
1803 * check PSEU for this authority in namestore
1805 rh->proc = &handle_shorten_pseu_ns_result;
1806 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1807 &rh->authority_chain_head->zone,
1809 GNUNET_GNS_RECORD_PSEU,
1810 &process_shorten_pseu_lookup_ns,
1816 * Process result from namestore delegation lookup
1817 * for shorten operation
1819 * @param cls the client shorten handle
1820 * @param rh the resolver handle
1821 * @param rd_count number of results (0)
1822 * @param rd data (NULL)
1825 handle_shorten_pseu_dht_result(void* cls,
1826 struct GNUNET_GNS_ResolverHandle *rh,
1828 const struct GNUNET_NAMESTORE_RecordData *rd)
1830 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1831 struct AuthorityChain *auth_chain;
1843 for (i=0; i < rd_len; i++)
1845 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1847 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1848 "Found PSEU %s\n", (char*) rd[i].data);
1853 pseu = (char*) rd[i].data;
1854 answer_len = strlen(rh->name) + strlen(pseu) + strlen(gnunet_tld) + 2;
1855 result = GNUNET_malloc(answer_len);
1856 memset(result, 0, answer_len);
1857 strcpy(result, rh->name);
1858 strcpy(result+strlen(rh->name), ".");
1859 strcpy(result+strlen(rh->name)+1, pseu);
1860 strcpy(result+strlen(rh->name)+strlen(pseu), gnunet_tld);
1862 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1863 "Sending pseudonym shorten result %s\n", result);
1865 send_shorten_response(result, csh);
1866 free_resolver_handle(rh);
1867 GNUNET_free(result);
1873 * continue with next authority
1876 auth_chain = rh->authority_chain_head;
1878 if ((auth_chain->next->next == NULL) &&
1879 GNUNET_CRYPTO_hash_cmp(&auth_chain->next->zone, &zone_hash) == 0)
1884 answer_len = strlen(rh->name) + strlen(auth_chain->name)
1885 + strlen(gnunet_tld) + 2;
1887 result = GNUNET_malloc(answer_len);
1888 memset(result, 0, answer_len);
1889 strcpy(result, rh->name);
1890 strcpy(result+strlen(rh->name), ".");
1891 strcpy(result+strlen(rh->name)+1, auth_chain->name);
1892 strcpy(result+strlen(rh->name)+strlen(auth_chain->name)+1, gnunet_tld);
1893 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1894 "Sending non pseudonym shorten result %s\n", result);
1896 send_shorten_response(result, csh);
1897 free_resolver_handle(rh);
1898 GNUNET_free(result);
1903 * Continue with next authority
1905 new_name = GNUNET_malloc(strlen(rh->name)+
1906 strlen(auth_chain->name) + 2);
1907 memset(new_name, 0, strlen(rh->name)+
1908 strlen(auth_chain->name) + 2);
1909 strcpy(new_name, rh->name);
1910 strcpy(new_name+strlen(rh->name)+1, ".");
1911 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1912 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1913 rh->authority_chain_tail,
1915 GNUNET_free(rh->name);
1916 rh->name = new_name;
1917 GNUNET_free(auth_chain->name);
1918 GNUNET_free(auth_chain);
1919 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1921 &rh->authority_chain_head->zone,
1922 &handle_shorten_zone_to_name,
1930 * Process result from namestore PSEU lookup
1931 * for shorten operation
1932 * FIXME do we need to check for own zone here?
1934 * @param cls the client shorten handle
1935 * @param rh the resolver handle
1936 * @param rd_count number of results (0 if none found)
1937 * @param rd data (NULL if none found)
1940 handle_shorten_pseu_ns_result(void* cls,
1941 struct GNUNET_GNS_ResolverHandle *rh,
1943 const struct GNUNET_NAMESTORE_RecordData *rd)
1945 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1946 struct AuthorityChain *auth_chain;
1958 for (i=0; i < rd_len; i++)
1960 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1962 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1963 "Found PSEU %s\n", (char*) rd[i].data);
1968 pseu = (char*) rd[i].data;
1969 answer_len = strlen(rh->name) + strlen(pseu) + strlen(gnunet_tld) + 2;
1970 result = GNUNET_malloc(answer_len);
1971 memset(result, 0, answer_len);
1972 strcpy(result, rh->name);
1973 strcpy(result+strlen(rh->name), ".");
1974 strcpy(result+strlen(rh->name)+1, pseu);
1975 strcpy(result+strlen(rh->name)+strlen(pseu)+1, gnunet_tld);
1977 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1978 "Sending shorten result %s\n", result);
1980 send_shorten_response(result, csh);
1981 free_resolver_handle(rh);
1982 GNUNET_free(result);
1987 * No PSEU found. Ask DHT if expired.
1988 * Else contunue with next authority
1990 if (rh->status & (EXISTS | !EXPIRED))
1995 auth_chain = rh->authority_chain_head;
1996 new_name = GNUNET_malloc(strlen(rh->name)+
1997 strlen(auth_chain->name) + 2);
1998 memset(new_name, 0, strlen(rh->name)+
1999 strlen(auth_chain->name) + 2);
2000 strcpy(new_name, rh->name);
2001 strcpy(new_name+strlen(rh->name)+1, ".");
2002 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
2004 GNUNET_free(rh->name);
2005 rh->name = new_name;
2006 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
2007 rh->authority_chain_tail,
2010 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2012 &rh->authority_chain_head->zone,
2013 &handle_shorten_zone_to_name,
2021 rh->authority = rh->authority_chain_head->zone;
2022 rh->proc = &handle_shorten_pseu_dht_result;
2023 resolve_pseu_from_dht(rh);
2030 * Process result from namestore delegation lookup
2031 * for shorten operation
2033 * @param cls the client shorten handle
2034 * @param rh the resolver handle
2035 * @param rd_count number of results (0)
2036 * @param rd data (NULL)
2039 handle_shorten_delegation_result(void* cls,
2040 struct GNUNET_GNS_ResolverHandle *rh,
2042 const struct GNUNET_NAMESTORE_RecordData *rd)
2044 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
2045 struct AuthorityChain *auth_chain;
2050 * At this point rh->name contains the part of the name
2051 * that we do not have a PKEY in our namestore to resolve.
2052 * The authority chain in the resolver handle is now
2053 * useful to backtrack if needed
2056 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2057 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2059 if (GNUNET_CRYPTO_hash_cmp(&rh->authority_chain_head->zone,
2063 * This is our zone append .gnunet unless name is empty
2064 * (it shouldn't be, usually FIXME what happens if we
2065 * shorten to our zone to a "" record??)
2068 answer_len = strlen(rh->name) + strlen(gnunet_tld) + 2;
2069 result = GNUNET_malloc(answer_len);
2070 memset(result, 0, answer_len);
2071 strcpy(result, rh->name);
2072 strcpy(result+strlen(rh->name), ".");
2073 strcpy(result+strlen(rh->name)+1, gnunet_tld);
2075 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2076 "Our zone: Sending name as shorten result %s\n", rh->name);
2078 send_shorten_response(rh->name, csh); //FIXME +.gnunet!
2079 free_resolver_handle(rh);
2080 GNUNET_free(csh->name);
2082 GNUNET_free(result);
2086 auth_chain = rh->authority_chain_head;
2087 /* backtrack authorities for pseu */
2088 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2091 &handle_shorten_zone_to_name,
2096 typedef void (*ShortenResponseProc) (void* cls, const char* name);
2099 * Shorten a given name
2101 * @param name the name to shorten
2102 * @param proc the processor to call when finished
2103 * @praram cls the closure to the processor
2106 shorten_name(char* name, struct ClientShortenHandle* csh)
2109 struct GNUNET_GNS_ResolverHandle *rh;
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2112 "Starting resolution for %s (type=%d)!\n",
2113 name, GNUNET_GNS_RECORD_PKEY);
2115 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
2116 rh->authority = zone_hash;
2118 rh->name = GNUNET_malloc(strlen(name)
2119 - strlen(gnunet_tld) + 1);
2121 strlen(name)-strlen(gnunet_tld) + 1);
2122 memcpy(rh->name, name,
2123 strlen(name)-strlen(gnunet_tld));
2125 csh->name = GNUNET_malloc(strlen(name)
2126 - strlen(gnunet_tld) + 1);
2128 strlen(name)-strlen(gnunet_tld) + 1);
2129 memcpy(rh->name, name,
2130 strlen(name)-strlen(gnunet_tld));
2132 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
2134 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2135 rh->authority_chain_tail = rh->authority_chain_head;
2136 rh->authority_chain_head->zone = zone_hash;
2137 rh->proc = &handle_shorten_delegation_result;
2138 rh->proc_cls = (void*)csh;
2140 /* Start delegation resolution in our namestore */
2141 resolve_delegation_from_ns(rh);
2146 * Send shorten response back to client
2147 * FIXME this is without .gnunet!
2149 * @param cls the client handle in closure
2150 * @param name the shortened name result or NULL if cannot be shortened
2153 send_shorten_response(const char* name, struct ClientShortenHandle *csh)
2155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
2156 "SHORTEN_RESULT", name);
2157 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
2164 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
2165 + strlen(name) + 1);
2167 rmsg->id = csh->unique_id;
2168 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
2170 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
2173 strcpy((char*)&rmsg[1], name);
2175 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
2176 (const struct GNUNET_MessageHeader *) rmsg,
2178 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
2181 GNUNET_free(csh->name);
2187 * Handle a shorten message from the api
2189 * @param cls the closure
2190 * @param client the client
2191 * @param message the message
2193 static void handle_shorten(void *cls,
2194 struct GNUNET_SERVER_Client * client,
2195 const struct GNUNET_MessageHeader * message)
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
2199 size_t msg_size = 0;
2200 struct ClientShortenHandle *csh;
2202 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
2204 GNUNET_break_op (0);
2205 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2209 GNUNET_SERVER_notification_context_add (nc, client);
2211 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
2212 (struct GNUNET_GNS_ClientShortenMessage *) message;
2214 msg_size = ntohs(message->size);
2216 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2218 GNUNET_break_op (0);
2219 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2223 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
2224 csh->client = client;
2225 csh->unique_id = sh_msg->id;
2227 shorten_name((char*)&sh_msg[1], csh);
2235 handle_lookup(void *cls,
2236 struct GNUNET_SERVER_Client * client,
2237 const struct GNUNET_MessageHeader * message)
2242 * Process GNS requests.
2244 * @param cls closure)
2245 * @param server the initialized server
2246 * @param c configuration to use
2249 run (void *cls, struct GNUNET_SERVER_Handle *server,
2250 const struct GNUNET_CONFIGURATION_Handle *c)
2253 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
2256 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2258 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2259 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
2260 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0}
2263 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
2264 "ZONEKEY", &keyfile))
2266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2267 "No private key for root zone specified%s!\n", keyfile);
2268 GNUNET_SCHEDULER_shutdown(0);
2272 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2273 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
2275 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2277 GNUNET_free(keyfile);
2280 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
2283 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2284 "DNS hijacking enabled... connecting to service.\n");
2286 * Do gnunet dns init here
2288 dns_handle = GNUNET_DNS_connect(c,
2289 GNUNET_DNS_FLAG_PRE_RESOLUTION,
2290 &handle_dns_request, /* rh */
2291 NULL); /* Closure */
2292 if (NULL == dns_handle)
2294 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2295 "Failed to connect to the dnsservice!\n");
2302 * handle to our local namestore
2304 namestore_handle = GNUNET_NAMESTORE_connect(c);
2306 if (NULL == namestore_handle)
2308 //FIXME do error handling;
2309 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2310 "Failed to connect to the namestore!\n");
2311 GNUNET_SCHEDULER_shutdown(0);
2318 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
2320 if (NULL == dht_handle)
2322 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
2325 //put_some_records(); //FIXME for testing
2328 * Schedule periodic put
2330 * We have roughly an hour for all records;
2332 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
2334 //zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
2336 GNUNET_SERVER_add_handlers (server, handlers);
2339 //GNUNET_SERVER_disconnect_notify (server,
2340 // &client_disconnect_notification,
2343 nc = GNUNET_SERVER_notification_context_create (server, 1);
2345 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
2351 * The main function for the GNS service.
2353 * @param argc number of arguments from the command line
2354 * @param argv command line arguments
2355 * @return 0 ok, 1 on error
2358 main (int argc, char *const *argv)
2364 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
2369 /* end of gnunet-service-gns.c */