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 /* FIXME move to proper header in include */
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
52 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH 27
53 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT 28
58 struct AuthorityChain *prev;
60 struct AuthorityChain *next;
64 /* (local) name of the authority */
67 /* was the ns entry fresh */
71 /* handle to a resolution process */
72 struct GNUNET_GNS_ResolverHandle;
75 * processor for a resultion result
77 * @param cls the closure
78 * @param rh the resolution handle
79 * @param rd_count number of results
80 * @pram rd resukt data
82 typedef void (*ResolutionResultProcessor) (void *cls,
83 struct GNUNET_GNS_ResolverHandle *rh,
85 const struct GNUNET_NAMESTORE_RecordData *rd);
88 * Resoltion status indicator
89 * EXISTS: the name to lookup exists
90 * EXPIRED: the name in the record expired
99 * Handle to a currenty pending resolution
101 struct GNUNET_GNS_ResolverHandle
103 /* The name to resolve */
106 /* has this query been answered? how many matches */
109 /* the authoritative zone to query */
110 GNUNET_HashCode authority;
112 /* the name of the authoritative zone to query */
113 char *authority_name;
116 * we have an authority in namestore that
117 * may be able to resolve
121 /* a handle for dht lookups. should be NULL if no lookups are in progress */
122 struct GNUNET_DHT_GetHandle *get_handle;
124 /* timeout task for dht lookups */
125 GNUNET_SCHEDULER_TaskIdentifier dht_timeout_task;
127 /* called when resolution phase finishes */
128 ResolutionResultProcessor proc;
130 /* closure passed to proc */
133 /* DLL to store the authority chain */
134 struct AuthorityChain *authority_chain_head;
136 /* DLL to store the authority chain */
137 struct AuthorityChain *authority_chain_tail;
139 /* status of the resolution result */
140 enum ResolutionStatus status;
145 * Handle to a record lookup
147 struct RecordLookupHandle
149 /* the record type to look up */
150 enum GNUNET_GNS_RecordType record_type;
152 /* the name to look up */
155 /* Method to call on record resolution result */
156 ResolutionResultProcessor proc;
158 /* closure to pass to proc */
164 * Handle to a shorten operation from api
166 struct ClientShortenHandle
168 /* the requesting client that */
169 struct GNUNET_SERVER_Client *client;
175 enum GNUNET_GNS_RecordType type;
177 /* name to shorten */
184 * Handle to a get auhtority operation from api
186 struct ClientGetAuthHandle
188 /* the requesting client that */
189 struct GNUNET_SERVER_Client *client;
194 /* name to lookup authority */
201 * Handle to a lookup operation from api
203 struct ClientLookupHandle
205 /* the requesting client that */
206 struct GNUNET_SERVER_Client *client;
212 enum GNUNET_GNS_RecordType type;
214 /* the name to look up */
215 char* name; //Needed?
219 * Handle to a DNS intercepted
222 struct InterceptLookupHandle
224 /* the request handle to reply to */
225 struct GNUNET_DNS_RequestHandle *request_handle;
227 /* the dns parser packet received */
228 struct GNUNET_DNSPARSER_Packet *packet;
230 /* the query parsed from the packet */
231 struct GNUNET_DNSPARSER_Query *query;
236 * Our handle to the DNS handler library
238 struct GNUNET_DNS_Handle *dns_handle;
241 * Our handle to the DHT
243 struct GNUNET_DHT_Handle *dht_handle;
246 * Our zone's private key
248 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
251 * Our handle to the namestore service
252 * FIXME maybe need a second handle for iteration
254 struct GNUNET_NAMESTORE_Handle *namestore_handle;
257 * Handle to iterate over our authoritative zone in namestore
259 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
262 * The configuration the GNS service is running with
264 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
267 * Our notification context.
269 static struct GNUNET_SERVER_NotificationContext *nc;
274 GNUNET_HashCode zone_hash;
277 * Our tld. Maybe get from config file
279 const char* gnunet_tld = ".gnunet";
282 * Useful for zone update for DHT put
284 static int num_public_records = 3600;
286 /* dht update interval FIXME define? */
287 static struct GNUNET_TIME_Relative dht_update_interval;
289 /* zone update task */
290 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
293 * Helper function to free resolver handle
295 * @rh the handle to free
298 free_resolver_handle(struct GNUNET_GNS_ResolverHandle* rh)
300 struct AuthorityChain *ac;
301 struct AuthorityChain *ac_next;
306 GNUNET_free_non_null (rh->name);
307 GNUNET_free_non_null (rh->authority_name);
309 ac = rh->authority_chain_head;
314 GNUNET_free_non_null (ac->name);
323 * Reply to dns request with the result from our lookup.
325 * @param cls the closure to the request (an InterceptLookupHandle)
326 * @param rh the request handle of the lookup
327 * @param rd_count the number of records to return
328 * @param rd the record data
331 reply_to_dns(void* cls, struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
332 const struct GNUNET_NAMESTORE_RecordData *rd)
338 struct InterceptLookupHandle* ilh = (struct InterceptLookupHandle*)cls;
339 struct GNUNET_DNSPARSER_Packet *packet = ilh->packet;
340 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
341 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
342 packet->answers = answer_records;
343 packet->additional_records = additional_records;
346 * Put records in the DNS packet and modify it
349 len = sizeof(struct GNUNET_DNSPARSER_Record*);
350 for (i=0; i < rd_count; i++)
353 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
354 "Adding type %d to DNS response\n", rd[i].record_type);
355 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", rh->name);
356 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "QName: %s\n", ilh->query->name);
357 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
358 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
360 if (rd[i].record_type == ilh->query->type)
362 answer_records[i].name = ilh->query->name;
363 answer_records[i].type = rd[i].record_type;
364 answer_records[i].data.raw.data_len = rd[i].data_size;
365 answer_records[i].data.raw.data = (char*)rd[i].data;
366 answer_records[i].expiration_time = rd[i].expiration;
367 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
371 additional_records[i].name = ilh->query->name;
372 additional_records[i].type = rd[i].record_type;
373 additional_records[i].data.raw.data_len = rd[i].data_size;
374 additional_records[i].data.raw.data = (char*)rd[i].data;
375 additional_records[i].expiration_time = rd[i].expiration;
376 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
380 packet->num_answers = rh->answered;
381 packet->num_additional_records = rd_count-(rh->answered);
383 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
384 packet->flags.authoritative_answer = 1;
386 packet->flags.authoritative_answer = 0;
389 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
391 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
393 packet->flags.query_or_response = 1;
399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
400 "Building DNS response\n");
401 ret = GNUNET_DNSPARSER_pack (packet,
402 1024, /* FIXME magic from dns redirector */
405 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
406 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
407 if (ret == GNUNET_OK)
409 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
410 "Answering DNS request\n");
411 GNUNET_DNS_request_answer(ilh->request_handle,
416 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
420 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
421 "Error building DNS response! (ret=%d)", ret);
424 packet->num_answers = 0;
425 packet->answers = NULL;
426 packet->num_additional_records = 0;
427 packet->additional_records = NULL;
428 GNUNET_DNSPARSER_free_packet(packet);
430 GNUNET_free((struct RecordLookupHandle*)rh->proc_cls);
431 free_resolver_handle(rh);
437 * Task run during shutdown.
443 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
446 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
448 /* Kill zone task for it may make the scheduler hang */
449 if (zone_update_taskid)
450 GNUNET_SCHEDULER_cancel(zone_update_taskid);
452 GNUNET_SERVER_notification_context_destroy (nc);
455 GNUNET_DNS_disconnect(dns_handle);
457 GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
458 GNUNET_DHT_disconnect(dht_handle);
463 * Callback when record data is put into namestore
465 * @param cls the closure
466 * @param success GNUNET_OK on success
467 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
470 on_namestore_record_put_result(void *cls,
474 if (GNUNET_NO == success)
476 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
479 else if (GNUNET_YES == success)
481 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
482 "records successfully put in namestore\n");
486 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
487 "Error putting records into namestore: %s\n", emsg);
492 * Handle timeout for DHT requests
494 * @param cls the request handle as closure
495 * @param tc the task context
498 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
500 struct GNUNET_GNS_ResolverHandle *rh = cls;
502 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
503 "dht lookup for query %s timed out.\n",
506 GNUNET_DHT_get_stop (rh->get_handle);
507 rh->proc(rh->proc_cls, rh, 0, NULL);
512 * Function called when we get a result from the dht
513 * for our record query
515 * @param cls the request handle
516 * @param exp lifetime
517 * @param key the key the record was stored under
518 * @param get_path get path
519 * @param get_path_length get path length
520 * @param put_path put path
521 * @param put_path_length put path length
522 * @param type the block type
523 * @param size the size of the record
524 * @param data the record data
527 process_record_dht_result(void* cls,
528 struct GNUNET_TIME_Absolute exp,
529 const GNUNET_HashCode * key,
530 const struct GNUNET_PeerIdentity *get_path,
531 unsigned int get_path_length,
532 const struct GNUNET_PeerIdentity *put_path,
533 unsigned int put_path_length,
534 enum GNUNET_BLOCK_Type type,
535 size_t size, const void *data)
537 struct GNUNET_GNS_ResolverHandle *rh;
538 struct RecordLookupHandle *rlh;
539 struct GNSNameRecordBlock *nrb;
540 uint32_t num_records;
542 char* rd_data = (char*)data;
546 GNUNET_HashCode zone, name_hash;
547 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
552 //FIXME maybe check expiration here, check block type
554 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
555 rlh = (struct RecordLookupHandle *) rh->proc_cls;
556 nrb = (struct GNSNameRecordBlock*)data;
558 /* stop lookup and timeout task */
559 GNUNET_DHT_get_stop (rh->get_handle);
560 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
562 rh->get_handle = NULL;
563 name = (char*)&nrb[1];
564 num_records = ntohl(nrb->rd_count);
566 struct GNUNET_NAMESTORE_RecordData rd[num_records];
568 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
569 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
571 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
576 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
580 for (i=0; i<num_records; i++)
582 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
583 "Got name: %s (wanted %s)\n", name, rh->name);
584 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
587 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
588 "Got data length: %d\n", rd[i].data_size);
589 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
590 "Got flag %d\n", rd[i].flags);
592 if ((strcmp(name, rh->name) == 0) &&
593 (rd[i].record_type == rlh->record_type))
600 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
601 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
604 * FIXME check pubkey against existing key in namestore?
605 * https://gnunet.org/bugs/view.php?id=2179
608 /* Save to namestore */
609 GNUNET_NAMESTORE_record_put (namestore_handle,
616 &on_namestore_record_put_result, //cont
620 rh->proc(rh->proc_cls, rh, num_records, rd);
622 rh->proc(rh->proc_cls, rh, 0, NULL);
629 * Start DHT lookup for a (name -> query->record_type) record in
630 * rh->authority's zone
632 * @param rh the pending gns query context
635 resolve_record_dht(struct GNUNET_GNS_ResolverHandle *rh)
638 GNUNET_HashCode name_hash;
639 GNUNET_HashCode lookup_key;
640 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
641 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
643 GNUNET_CRYPTO_hash(rh->name, strlen(rh->name), &name_hash);
644 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
645 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
647 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
648 "starting dht lookup for %s with key: %s\n",
649 rh->name, (char*)&lookup_key_string);
651 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
652 &dht_lookup_timeout, rh);
654 xquery = htonl(rlh->record_type);
655 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
656 DHT_OPERATION_TIMEOUT,
657 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
659 DHT_GNS_REPLICATION_LEVEL,
663 &process_record_dht_result,
670 * Namestore calls this function if we have record for this name.
671 * (or with rd_count=0 to indicate no matches)
673 * @param cls the pending query
674 * @param key the key of the zone we did the lookup
675 * @param expiration expiration date of the namestore entry
676 * @param name the name for which we need an authority
677 * @param rd_count the number of records with 'name'
678 * @param rd the record data
679 * @param signature the signature of the authority for the record data
682 process_record_lookup_ns(void* cls,
683 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
684 struct GNUNET_TIME_Absolute expiration,
685 const char *name, unsigned int rd_count,
686 const struct GNUNET_NAMESTORE_RecordData *rd,
687 const struct GNUNET_CRYPTO_RsaSignature *signature)
689 struct GNUNET_GNS_ResolverHandle *rh;
690 struct RecordLookupHandle *rlh;
691 struct GNUNET_TIME_Relative remaining_time;
692 GNUNET_HashCode zone;
694 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
695 rlh = (struct RecordLookupHandle *)rh->proc_cls;
696 GNUNET_CRYPTO_hash(key,
697 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
699 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
705 rh->status |= EXISTS;
708 if (remaining_time.rel_value == 0)
710 rh->status |= EXPIRED;
716 * Lookup terminated and no results
718 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
719 "Namestore lookup for %s terminated without results\n", name);
721 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
722 "Record %s unknown in namestore\n",
725 * Our zone and no result? Cannot resolve TT
727 rh->proc(rh->proc_cls, rh, 0, NULL);
734 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
735 "Processing additional result %s from namestore\n", name);
737 for (i=0; i<rd_count;i++)
740 if (rd[i].record_type != rlh->record_type)
743 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
746 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
757 if (rh->answered == 0)
759 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
760 "No answers found. This is odd!\n");
761 rh->proc(rh->proc_cls, rh, 0, NULL);
765 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
768 rh->proc(rh->proc_cls, rh, rd_count, rd);
774 * The final phase of resolution.
775 * rh->name is a name that is canonical and we do not have a delegation.
776 * Query namestore for this record
778 * @param rh the pending lookup
781 resolve_record_ns(struct GNUNET_GNS_ResolverHandle *rh)
783 struct RecordLookupHandle *rlh = (struct RecordLookupHandle *)rh->proc_cls;
786 * Try to resolve this record in our namestore.
787 * The name to resolve is now in rh->authority_name
788 * since we tried to resolve it to an authority
791 GNUNET_NAMESTORE_lookup_record(namestore_handle,
795 &process_record_lookup_ns,
801 * Handle timeout for DHT requests
803 * @param cls the request handle as closure
804 * @param tc the task context
807 dht_authority_lookup_timeout(void *cls,
808 const struct GNUNET_SCHEDULER_TaskContext *tc)
810 struct GNUNET_GNS_ResolverHandle *rh = cls;
812 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
813 "dht lookup for query %s timed out.\n",
816 GNUNET_DHT_get_stop (rh->get_handle);
817 if (strcmp(rh->name, "") == 0)
820 * promote authority back to name and try to resolve record
822 strcpy(rh->name, rh->authority_name);
824 rh->proc(rh->proc_cls, rh, 0, NULL);
828 static void resolve_delegation_dht(struct GNUNET_GNS_ResolverHandle *rh);
831 * Function called when we get a result from the dht
832 * for our query. Recursively tries to resolve authorities
835 * @param cls the request handle
836 * @param exp lifetime
837 * @param key the key the record was stored under
838 * @param get_path get path
839 * @param get_path_length get path length
840 * @param put_path put path
841 * @param put_path_length put path length
842 * @param type the block type
843 * @param size the size of the record
844 * @param data the record data
847 process_delegation_result_dht(void* cls,
848 struct GNUNET_TIME_Absolute exp,
849 const GNUNET_HashCode * key,
850 const struct GNUNET_PeerIdentity *get_path,
851 unsigned int get_path_length,
852 const struct GNUNET_PeerIdentity *put_path,
853 unsigned int put_path_length,
854 enum GNUNET_BLOCK_Type type,
855 size_t size, const void *data)
857 struct GNUNET_GNS_ResolverHandle *rh;
858 struct GNSNameRecordBlock *nrb;
859 uint32_t num_records;
861 char* rd_data = (char*) data;
864 GNUNET_HashCode zone, name_hash;
869 //FIXME check expiration?
871 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
872 nrb = (struct GNSNameRecordBlock*)data;
874 /* stop dht lookup and timeout task */
875 GNUNET_DHT_get_stop (rh->get_handle);
876 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
878 rh->get_handle = NULL;
879 num_records = ntohl(nrb->rd_count);
880 name = (char*)&nrb[1];
882 struct GNUNET_NAMESTORE_RecordData rd[num_records];
884 rd_data += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
885 rd_size = size - strlen(name) - 1 - sizeof(struct GNSNameRecordBlock);
887 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
892 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
896 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
897 "Got name: %s (wanted %s)\n", name, rh->authority_name);
898 for (i=0; i<num_records; i++)
901 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
902 "Got name: %s (wanted %s)\n", name, rh->authority_name);
903 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
904 "Got type: %d (wanted %d)\n",
905 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
906 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
907 "Got data length: %d\n", rd[i].data_size);
908 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
909 "Got flag %d\n", rd[i].flags);
911 if ((strcmp(name, rh->authority_name) == 0) &&
912 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
914 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
916 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
917 struct AuthorityChain *auth =
918 GNUNET_malloc(sizeof(struct AuthorityChain));
919 auth->zone = rh->authority;
920 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
921 memset(auth->name, 0, strlen(rh->authority_name)+1);
922 strcpy(auth->name, rh->authority_name);
923 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
924 rh->authority_chain_tail,
931 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
932 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
934 /* Save to namestore */
935 if (0 != GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
937 GNUNET_NAMESTORE_record_put (namestore_handle,
944 &on_namestore_record_put_result, //cont
954 * FIXME in this case. should we ask namestore again?
956 if (strcmp(rh->name, "") == 0)
957 rh->proc(rh->proc_cls, rh, 0, NULL);
959 resolve_delegation_dht(rh);
964 * should never get here unless false dht key/put
965 * block plugin should handle this
967 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DHT authority lookup error!\n");
973 * Process DHT lookup result for record.
975 * @param cls the closure
976 * @param rh resolver handle
977 * @param rd_count number of results (always 0)
978 * @param rd record data (always NULL)
981 process_record_result_dht(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
982 unsigned int rd_count,
983 const struct GNUNET_NAMESTORE_RecordData *rd)
985 struct RecordLookupHandle* rlh;
986 rlh = (struct RecordLookupHandle*)cls;
989 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
990 "No records for %s found in DHT. Aborting\n",
992 /* give up, cannot resolve */
993 rlh->proc(rlh->proc_cls, rh, 0, NULL);
997 /* results found yay */
998 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
999 "Record resolved from namestore!");
1000 rlh->proc(rlh->proc_cls, rh, rd_count, rd);
1006 * Process namestore lookup result for record.
1008 * @param cls the closure
1009 * @param rh resolver handle
1010 * @param rd_count number of results (always 0)
1011 * @param rd record data (always NULL)
1014 process_record_result_ns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1015 unsigned int rd_count,
1016 const struct GNUNET_NAMESTORE_RecordData *rd)
1018 struct RecordLookupHandle* rlh;
1019 rlh = (struct RecordLookupHandle*) cls;
1022 /* ns entry expired. try dht */
1023 if (rh->status & (EXPIRED | !EXISTS))
1025 rh->proc = &process_record_result_dht;
1026 resolve_record_dht(rh);
1029 /* give up, cannot resolve */
1030 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1034 /* results found yay */
1035 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1036 "Record resolved from namestore!");
1037 rlh->proc(rlh->proc_cls, rh, rd_count, rd);
1043 * Determine if this name is canonical.
1045 * a.b.gnunet = not canonical
1048 * @param name the name to test
1049 * @return 1 if canonical
1052 is_canonical(char* name)
1054 uint32_t len = strlen(name);
1057 for (i=0; i<len; i++)
1059 if (*(name+i) == '.')
1066 * Move one level up in the domain hierarchy and return the
1067 * passed top level domain.
1069 * @param name the domain
1070 * @param dest the destination where the tld will be put
1073 pop_tld(char* name, char* dest)
1077 if (is_canonical(name))
1084 for (len = strlen(name); len > 0; len--)
1086 if (*(name+len) == '.')
1096 strcpy(dest, (name+len+1));
1100 * DHT resolution for delegation finished. Processing result.
1102 * @param cls the closure
1103 * @param rh resolver handle
1104 * @param rd_count number of results (always 0)
1105 * @param rd record data (always NULL)
1108 process_delegation_dht(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1109 unsigned int rd_count,
1110 const struct GNUNET_NAMESTORE_RecordData *rd)
1112 struct RecordLookupHandle* rlh;
1113 rlh = (struct RecordLookupHandle*) cls;
1115 if (strcmp(rh->name, "") == 0)
1117 /* We resolved full name for delegation. resolving record */
1118 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1119 "Resolved full name for delegation via DHT. resolving record '' in ns\n");
1120 rh->proc = &process_record_result_ns;
1121 resolve_record_ns(rh);
1126 * we still have some left
1128 if (is_canonical(rh->name))
1130 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1131 "Resolving canonical record %s in ns\n", rh->name);
1132 rh->proc = &process_record_result_ns;
1133 resolve_record_ns(rh);
1136 /* give up, cannot resolve */
1137 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1138 "Cannot fully resolve delegation for %s via DHT!\n",
1140 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1145 * Start DHT lookup for a name -> PKEY (compare NS) record in
1146 * rh->authority's zone
1148 * @param rh the pending gns query
1149 * @param name the name of the PKEY record
1152 resolve_delegation_dht(struct GNUNET_GNS_ResolverHandle *rh)
1155 GNUNET_HashCode name_hash;
1156 GNUNET_HashCode lookup_key;
1158 GNUNET_CRYPTO_hash(rh->authority_name,
1159 strlen(rh->authority_name),
1161 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1163 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1164 &dht_authority_lookup_timeout,
1167 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
1169 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1170 DHT_OPERATION_TIMEOUT,
1171 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1173 DHT_GNS_REPLICATION_LEVEL,
1177 &process_delegation_result_dht,
1184 * Namestore resolution for delegation finished. Processing result.
1186 * @param cls the closure
1187 * @param rh resolver handle
1188 * @param rd_count number of results (always 0)
1189 * @param rd record data (always NULL)
1192 process_delegation_ns(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
1193 unsigned int rd_count,
1194 const struct GNUNET_NAMESTORE_RecordData *rd)
1196 struct RecordLookupHandle* rlh;
1197 rlh = (struct RecordLookupHandle*) cls;
1199 if (strcmp(rh->name, "") == 0)
1201 /* We resolved full name for delegation. resolving record */
1202 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1203 "Resolved full name for delegation. resolving record ''\n");
1204 rh->proc = &process_record_result_ns;
1205 resolve_record_ns(rh);
1210 * we still have some left
1211 * check if ns entry is fresh
1213 if (rh->status & (EXISTS | !EXPIRED))
1215 if (is_canonical(rh->name))
1217 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1218 "Resolving canonical record %s\n", rh->name);
1219 rh->proc = &process_record_result_ns;
1220 resolve_record_ns(rh);
1224 /* give up, cannot resolve */
1225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1226 "Cannot fully resolve delegation for %s!\n",
1228 rlh->proc(rlh->proc_cls, rh, 0, NULL);
1233 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1234 "Trying to resolve delegation for %s via DHT\n",
1236 rh->proc = &process_delegation_dht;
1237 resolve_delegation_dht(rh);
1241 static void resolve_delegation_ns(struct GNUNET_GNS_ResolverHandle *rh);
1244 * This is a callback function that should give us only PKEY
1245 * records. Used to query the namestore for the authority (PKEY)
1246 * for 'name'. It will recursively try to resolve the
1247 * authority for a given name from the namestore.
1249 * @param cls the pending query
1250 * @param key the key of the zone we did the lookup
1251 * @param expiration expiration date of the record data set in the namestore
1252 * @param name the name for which we need an authority
1253 * @param rd_count the number of records with 'name'
1254 * @param rd the record data
1255 * @param signature the signature of the authority for the record data
1258 process_delegation_result_ns(void* cls,
1259 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1260 struct GNUNET_TIME_Absolute expiration,
1262 unsigned int rd_count,
1263 const struct GNUNET_NAMESTORE_RecordData *rd,
1264 const struct GNUNET_CRYPTO_RsaSignature *signature)
1266 struct GNUNET_GNS_ResolverHandle *rh;
1267 struct GNUNET_TIME_Relative remaining_time;
1268 GNUNET_HashCode zone;
1271 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %d records from authority lookup\n",
1274 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
1275 GNUNET_CRYPTO_hash(key,
1276 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1278 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
1284 rh->status |= EXISTS;
1287 if (remaining_time.rel_value == 0)
1289 rh->status |= EXPIRED;
1293 * No authority found in namestore.
1298 * We did not find an authority in the namestore
1303 * Promote this authority back to a name maybe it is
1306 if (strcmp(rh->name, "") == 0)
1308 /* simply promote back */
1309 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1310 "Promoting %s back to name\n", rh->authority_name);
1311 strcpy(rh->name, rh->authority_name);
1315 /* add back to existing name */
1316 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1317 "Adding %s back to %s\n",
1318 rh->authority_name, rh->name);
1319 new_name = GNUNET_malloc(strlen(rh->name)
1320 + strlen(rh->authority_name) + 2);
1321 memset(new_name, 0, strlen(rh->name) + strlen(rh->authority_name) + 2);
1322 strcpy(new_name, rh->name);
1323 strcpy(new_name+strlen(new_name)+1, ".");
1324 strcpy(new_name+strlen(new_name)+2, rh->authority_name);
1325 GNUNET_free(rh->name);
1326 rh->name = new_name;
1328 rh->proc(rh->proc_cls, rh, 0, NULL);
1332 //Note only 1 pkey should have been returned.. anything else would be strange
1334 * We found an authority that may be able to help us
1335 * move on with query
1338 for (i=0; i<rd_count;i++)
1341 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
1344 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
1347 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
1348 if (remaining_time.rel_value == 0)
1350 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1351 "This dht entry is expired.\n");
1352 rh->authority_chain_head->fresh = 0;
1353 rh->proc(rh->proc_cls, rh, 0, NULL);
1361 * Resolve rest of query with new authority
1363 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
1364 memcpy(&rh->authority, rd[i].data, sizeof(GNUNET_HashCode));
1365 struct AuthorityChain *auth = GNUNET_malloc(sizeof(struct AuthorityChain));
1366 auth->zone = rh->authority;
1367 auth->name = GNUNET_malloc(strlen(rh->authority_name)+1);
1368 memset(auth->name, 0, strlen(rh->authority_name)+1);
1369 strcpy(auth->name, rh->authority_name);
1370 GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
1371 rh->authority_chain_tail,
1375 * We are done with PKEY resolution if name is empty
1376 * else resolve again with new authority
1378 if (strcmp(rh->name, "") == 0)
1379 rh->proc(rh->proc_cls, rh, 0, NULL);
1381 resolve_delegation_ns(rh);
1388 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1389 "Authority lookup successful but no PKEY... never get here\n");
1390 rh->proc(rh->proc_cls, rh, 0, NULL);
1395 * Resolve the delegation chain for the request in our namestore
1397 * @param rh the resolver handle
1400 resolve_delegation_ns(struct GNUNET_GNS_ResolverHandle *rh)
1403 pop_tld(rh->name, rh->authority_name);
1404 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1407 GNUNET_GNS_RECORD_PKEY,
1408 &process_delegation_result_ns,
1414 * Entry point for name resolution
1415 * Setup a new query and try to resolve
1417 * @param request the request handle of the DNS request from a client
1418 * @param p the DNS query packet we received
1419 * @param q the DNS query we received parsed from p
1422 start_resolution_for_dns(struct GNUNET_DNS_RequestHandle *request,
1423 struct GNUNET_DNSPARSER_Packet *p,
1424 struct GNUNET_DNSPARSER_Query *q)
1426 struct GNUNET_GNS_ResolverHandle *rh;
1427 struct RecordLookupHandle* rlh;
1428 struct InterceptLookupHandle* ilh;
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1431 "Starting resolution for %s (type=%d)!\n",
1434 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
1435 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
1436 ilh = GNUNET_malloc(sizeof(struct InterceptLookupHandle));
1439 ilh->request_handle = request;
1441 rh->authority = zone_hash;
1443 rlh->record_type = q->type;
1444 rlh->name = q->name;
1445 rlh->proc = &reply_to_dns;
1446 rlh->proc_cls = ilh;
1450 rh->name = GNUNET_malloc(strlen(q->name)
1451 - strlen(gnunet_tld) + 1);
1453 strlen(q->name)-strlen(gnunet_tld) + 1);
1454 memcpy(rh->name, q->name,
1455 strlen(q->name)-strlen(gnunet_tld));
1457 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
1459 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
1460 rh->authority_chain_head->prev = NULL;
1461 rh->authority_chain_head->next = NULL;
1462 rh->authority_chain_tail = rh->authority_chain_head;
1463 rh->authority_chain_head->zone = zone_hash;
1465 /* Start resolution in our zone */
1466 rh->proc = &process_delegation_ns;
1467 resolve_delegation_ns(rh);
1473 * The DNS request handler
1474 * Called for every incoming DNS request.
1476 * @param cls closure
1477 * @param rh request handle to user for reply
1478 * @param request_length number of bytes in request
1479 * @param request udp payload of the DNS request
1482 handle_dns_request(void *cls,
1483 struct GNUNET_DNS_RequestHandle *rh,
1484 size_t request_length,
1485 const char *request)
1487 struct GNUNET_DNSPARSER_Packet *p;
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
1492 p = GNUNET_DNSPARSER_parse (request, request_length);
1496 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1497 "Received malformed DNS packet, leaving it untouched\n");
1498 GNUNET_DNS_request_forward (rh);
1499 GNUNET_DNSPARSER_free_packet (p);
1504 * Check tld and decide if we or
1505 * legacy dns is responsible
1507 * FIXME now in theory there could be more than 1 query in the request
1508 * but if this is case we get into trouble:
1509 * either we query the GNS or the DNS. We cannot do both!
1510 * So I suggest to either only allow a single query per request or
1511 * only allow GNS or DNS requests.
1512 * The way it is implemented here now is buggy and will lead to erratic
1513 * behaviour (if multiple queries are present).
1515 if (p->num_queries == 0)
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1518 "No Queries in DNS packet... forwarding\n");
1519 GNUNET_DNS_request_forward (rh);
1520 GNUNET_DNSPARSER_free_packet(p);
1524 if (p->num_queries > 1)
1526 /* Note: We could also look for .gnunet */
1527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1528 ">1 queriy in DNS packet... odd. We only process #1\n");
1535 tldoffset = p->queries[0].name + strlen(p->queries[0].name) - 1;
1537 for (i=0; i<strlen(p->queries[0].name); i++)
1539 if (*(tldoffset-i) == '.')
1543 if ((i==strlen(gnunet_tld)-1) && (0 == strcmp(tldoffset-i, gnunet_tld)))
1545 start_resolution_for_dns(rh, p, p->queries);
1550 * This request does not concern us. Forward to real DNS.
1552 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1553 "Request for %s is forwarded to DNS\n", p->queries[0].name);
1554 GNUNET_DNS_request_forward (rh);
1555 GNUNET_DNSPARSER_free_packet (p);
1561 * Method called periodicattluy that triggers
1562 * iteration over root zone
1564 * @param cls closure
1565 * @param tc task context
1568 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1570 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1574 * Continuation for DHT put
1576 * @param cls closure
1577 * @param tc task context
1580 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1582 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1587 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1590 * Function used to put all records successively into the DHT.
1592 * @param cls the closure (NULL)
1593 * @param key the public key of the authority (ours)
1594 * @param expiration lifetime of the namestore entry
1595 * @param name the name of the records
1596 * @param rd_count the number of records in data
1597 * @param rd the record data
1598 * @param signature the signature for the record data
1601 put_gns_record(void *cls,
1602 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1603 struct GNUNET_TIME_Absolute expiration,
1605 unsigned int rd_count,
1606 const struct GNUNET_NAMESTORE_RecordData *rd,
1607 const struct GNUNET_CRYPTO_RsaSignature *signature)
1610 struct GNSNameRecordBlock *nrb;
1611 GNUNET_HashCode name_hash;
1612 GNUNET_HashCode xor_hash;
1613 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1614 uint32_t rd_payload_length;
1615 char* nrb_data = NULL;
1620 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1621 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1622 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1627 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1628 "Putting records for %s into the DHT\n", name);
1630 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1632 nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1
1633 + sizeof(struct GNSNameRecordBlock));
1635 if (signature != NULL)
1636 nrb->signature = *signature;
1638 nrb->public_key = *key;
1640 nrb->rd_count = htonl(rd_count);
1642 memset(&nrb[1], 0, strlen(name) + 1);
1643 memcpy(&nrb[1], name, strlen(name));
1645 nrb_data = (char*)&nrb[1];
1646 nrb_data += strlen(name) + 1;
1648 rd_payload_length += sizeof(struct GNSNameRecordBlock) +
1651 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
1656 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
1664 * calculate DHT key: H(name) xor H(pubkey)
1666 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1667 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1668 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1669 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1670 "putting records for %s under key: %s with size %d\n",
1671 name, (char*)&xor_hash_string, rd_payload_length);
1673 GNUNET_DHT_put (dht_handle, &xor_hash,
1674 DHT_GNS_REPLICATION_LEVEL,
1676 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1680 DHT_OPERATION_TIMEOUT,
1682 NULL); //cls for cont
1684 num_public_records++;
1687 * Reschedule periodic put
1689 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1690 &update_zone_dht_next,
1698 * Periodically iterate over our zone and store everything in dht
1701 * @param tc task context
1704 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1706 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1707 if (0 == num_public_records)
1709 dht_update_interval = GNUNET_TIME_relative_multiply(
1710 GNUNET_TIME_UNIT_SECONDS,
1715 dht_update_interval = GNUNET_TIME_relative_multiply(
1716 GNUNET_TIME_UNIT_SECONDS,
1717 (3600/num_public_records));
1719 num_public_records = 0; //start counting again
1720 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1722 GNUNET_NAMESTORE_RF_AUTHORITY,
1723 GNUNET_NAMESTORE_RF_PRIVATE,
1729 static void send_shorten_response(const char* name,
1730 struct ClientShortenHandle *csh);
1732 process_shorten_pseu_lookup_ns(void *cls,
1733 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1734 struct GNUNET_TIME_Absolute expire,
1736 unsigned int rd_len,
1737 const struct GNUNET_NAMESTORE_RecordData *rd,
1738 const struct GNUNET_CRYPTO_RsaSignature *signature)
1740 struct GNUNET_GNS_ResolverHandle *rh =
1741 (struct GNUNET_GNS_ResolverHandle *)cls;
1742 struct GNUNET_TIME_Relative remaining_time;
1744 GNUNET_TIME_absolute_get_remaining (expire);
1749 rh->status |= EXISTS;
1752 if (remaining_time.rel_value == 0)
1754 rh->status |= EXPIRED;
1757 rh->proc(rh->proc_cls, rh, rd_len, rd);
1762 * Start DHT lookup for a PSEUdonym record in
1763 * rh->authority's zone
1765 * @param rh the pending gns query
1768 resolve_pseu_dht(struct GNUNET_GNS_ResolverHandle *rh)
1771 GNUNET_HashCode name_hash;
1772 GNUNET_HashCode lookup_key;
1775 GNUNET_CRYPTO_hash("",
1779 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
1781 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
1782 &dht_lookup_timeout,
1785 xquery = htonl(GNUNET_GNS_RECORD_PSEU);
1787 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
1788 DHT_OPERATION_TIMEOUT,
1789 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1791 DHT_GNS_REPLICATION_LEVEL,
1795 &process_delegation_result_dht,
1802 handle_shorten_pseu_ns_result(void* cls,
1803 struct GNUNET_GNS_ResolverHandle *rh,
1805 const struct GNUNET_NAMESTORE_RecordData *rd);
1808 handle_shorten_zone_to_name(void *cls,
1809 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
1810 struct GNUNET_TIME_Absolute expire,
1812 unsigned int rd_len,
1813 const struct GNUNET_NAMESTORE_RecordData *rd,
1814 const struct GNUNET_CRYPTO_RsaSignature *signature)
1816 struct GNUNET_GNS_ResolverHandle *rh =
1817 (struct GNUNET_GNS_ResolverHandle *)cls;
1818 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) rh->proc_cls;
1823 /* we found a match in our own zone */
1826 answer_len = strlen(rh->name) + strlen(name) + strlen(gnunet_tld) + 2;
1827 result = GNUNET_malloc(answer_len);
1828 memset(result, 0, answer_len);
1829 strcpy(result, rh->name);
1830 strcpy(result+strlen(rh->name), ".");
1831 strcpy(result+strlen(rh->name)+1, name);
1832 strcpy(result+strlen(rh->name)+strlen(name), gnunet_tld);
1834 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1835 "Sending shorten result %s\n", result);
1837 send_shorten_response(result, csh);
1838 free_resolver_handle(rh);
1839 GNUNET_free(result);
1844 * Nothing in our zone
1845 * check PSEU for this authority in namestore
1847 rh->proc = &handle_shorten_pseu_ns_result;
1848 GNUNET_NAMESTORE_lookup_record(namestore_handle,
1849 &rh->authority_chain_head->zone,
1851 GNUNET_GNS_RECORD_PSEU,
1852 &process_shorten_pseu_lookup_ns,
1858 * Process result from namestore delegation lookup
1859 * for shorten operation
1861 * @param cls the client shorten handle
1862 * @param rh the resolver handle
1863 * @param rd_count number of results (0)
1864 * @param rd data (NULL)
1867 handle_shorten_pseu_dht_result(void* cls,
1868 struct GNUNET_GNS_ResolverHandle *rh,
1870 const struct GNUNET_NAMESTORE_RecordData *rd)
1872 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1873 struct AuthorityChain *auth_chain;
1885 for (i=0; i < rd_len; i++)
1887 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
1889 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1890 "Found PSEU %s\n", (char*) rd[i].data);
1895 pseu = (char*) rd[i].data;
1896 answer_len = strlen(rh->name) + strlen(pseu) + strlen(gnunet_tld) + 2;
1897 result = GNUNET_malloc(answer_len);
1898 memset(result, 0, answer_len);
1899 strcpy(result, rh->name);
1900 strcpy(result+strlen(rh->name), ".");
1901 strcpy(result+strlen(rh->name)+1, pseu);
1902 strcpy(result+strlen(rh->name)+strlen(pseu), gnunet_tld);
1904 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1905 "Sending pseudonym shorten result %s\n", result);
1907 send_shorten_response(result, csh);
1908 free_resolver_handle(rh);
1909 GNUNET_free(result);
1915 * continue with next authority
1918 auth_chain = rh->authority_chain_head;
1920 if ((auth_chain->next->next == NULL) &&
1921 GNUNET_CRYPTO_hash_cmp(&auth_chain->next->zone, &zone_hash) == 0)
1926 answer_len = strlen(rh->name) + strlen(auth_chain->name)
1927 + strlen(gnunet_tld) + 2;
1929 result = GNUNET_malloc(answer_len);
1930 memset(result, 0, answer_len);
1931 strcpy(result, rh->name);
1932 strcpy(result+strlen(rh->name), ".");
1933 strcpy(result+strlen(rh->name)+1, auth_chain->name);
1934 strcpy(result+strlen(rh->name)+strlen(auth_chain->name)+1, gnunet_tld);
1935 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1936 "Sending non pseudonym shorten result %s\n", result);
1938 send_shorten_response(result, csh);
1939 free_resolver_handle(rh);
1940 GNUNET_free(result);
1945 * Continue with next authority
1947 new_name = GNUNET_malloc(strlen(rh->name)+
1948 strlen(auth_chain->name) + 2);
1949 memset(new_name, 0, strlen(rh->name)+
1950 strlen(auth_chain->name) + 2);
1951 strcpy(new_name, rh->name);
1952 strcpy(new_name+strlen(rh->name)+1, ".");
1953 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
1954 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
1955 rh->authority_chain_tail,
1957 GNUNET_free(rh->name);
1958 rh->name = new_name;
1959 GNUNET_free(auth_chain->name);
1960 GNUNET_free(auth_chain);
1961 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
1963 &rh->authority_chain_head->zone,
1964 &handle_shorten_zone_to_name,
1972 * Process result from namestore PSEU lookup
1973 * for shorten operation
1974 * FIXME do we need to check for own zone here?
1976 * @param cls the client shorten handle
1977 * @param rh the resolver handle
1978 * @param rd_count number of results (0 if none found)
1979 * @param rd data (NULL if none found)
1982 handle_shorten_pseu_ns_result(void* cls,
1983 struct GNUNET_GNS_ResolverHandle *rh,
1985 const struct GNUNET_NAMESTORE_RecordData *rd)
1987 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
1988 struct AuthorityChain *auth_chain;
2000 for (i=0; i < rd_len; i++)
2002 if (rd[i].record_type == GNUNET_GNS_RECORD_PSEU)
2004 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2005 "Found PSEU %s\n", (char*) rd[i].data);
2010 pseu = (char*) rd[i].data;
2011 answer_len = strlen(rh->name) + strlen(pseu) + strlen(gnunet_tld) + 2;
2012 result = GNUNET_malloc(answer_len);
2013 memset(result, 0, answer_len);
2014 strcpy(result, rh->name);
2015 strcpy(result+strlen(rh->name), ".");
2016 strcpy(result+strlen(rh->name)+1, pseu);
2017 strcpy(result+strlen(rh->name)+strlen(pseu)+1, gnunet_tld);
2019 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2020 "Sending shorten result %s\n", result);
2022 send_shorten_response(result, csh);
2023 free_resolver_handle(rh);
2024 GNUNET_free(result);
2029 * No PSEU found. Ask DHT if expired.
2030 * Else contunue with next authority
2032 if (rh->status & (EXISTS | !EXPIRED))
2037 auth_chain = rh->authority_chain_head;
2038 new_name = GNUNET_malloc(strlen(rh->name)+
2039 strlen(auth_chain->name) + 2);
2040 memset(new_name, 0, strlen(rh->name)+
2041 strlen(auth_chain->name) + 2);
2042 strcpy(new_name, rh->name);
2043 strcpy(new_name+strlen(rh->name)+1, ".");
2044 strcpy(new_name+strlen(rh->name)+2, auth_chain->name);
2046 GNUNET_free(rh->name);
2047 rh->name = new_name;
2048 GNUNET_CONTAINER_DLL_remove(rh->authority_chain_head,
2049 rh->authority_chain_tail,
2052 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2054 &rh->authority_chain_head->zone,
2055 &handle_shorten_zone_to_name,
2063 rh->authority = rh->authority_chain_head->zone;
2064 rh->proc = &handle_shorten_pseu_dht_result;
2065 resolve_pseu_dht(rh);
2072 * Process result from namestore delegation lookup
2073 * for shorten operation
2075 * @param cls the client shorten handle
2076 * @param rh the resolver handle
2077 * @param rd_count number of results (0)
2078 * @param rd data (NULL)
2081 handle_shorten_delegation_result(void* cls,
2082 struct GNUNET_GNS_ResolverHandle *rh,
2084 const struct GNUNET_NAMESTORE_RecordData *rd)
2086 struct ClientShortenHandle* csh = (struct ClientShortenHandle*) cls;
2087 struct AuthorityChain *auth_chain;
2092 * At this point rh->name contains the part of the name
2093 * that we do not have a PKEY in our namestore to resolve.
2094 * The authority chain in the resolver handle is now
2095 * useful to backtrack if needed
2098 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2099 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2101 if (GNUNET_CRYPTO_hash_cmp(&rh->authority_chain_head->zone,
2105 * This is our zone append .gnunet unless name is empty
2106 * (it shouldn't be, usually FIXME what happens if we
2107 * shorten to our zone to a "" record??)
2110 answer_len = strlen(rh->name) + strlen(gnunet_tld) + 2;
2111 result = GNUNET_malloc(answer_len);
2112 memset(result, 0, answer_len);
2113 strcpy(result, rh->name);
2114 strcpy(result+strlen(rh->name), ".");
2115 strcpy(result+strlen(rh->name)+1, gnunet_tld);
2117 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2118 "Our zone: Sending name as shorten result %s\n", rh->name);
2120 send_shorten_response(result, csh); //FIXME +.gnunet!
2121 free_resolver_handle(rh);
2122 GNUNET_free(result);
2126 auth_chain = rh->authority_chain_head;
2127 /* backtrack authorities for pseu */
2128 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2131 &handle_shorten_zone_to_name,
2136 typedef void (*ShortenResponseProc) (void* cls, const char* name);
2139 * Shorten a given name
2141 * @param name the name to shorten
2142 * @param csh the shorten handle of the request
2145 shorten_name(char* name, struct ClientShortenHandle* csh)
2148 struct GNUNET_GNS_ResolverHandle *rh;
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2151 "Starting resolution for %s (type=%d)!\n",
2152 name, GNUNET_GNS_RECORD_PKEY);
2154 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
2155 rh->authority = zone_hash;
2157 rh->name = GNUNET_malloc(strlen(name)
2158 - strlen(gnunet_tld) + 1);
2160 strlen(name)-strlen(gnunet_tld) + 1);
2161 memcpy(rh->name, name,
2162 strlen(name)-strlen(gnunet_tld));
2164 csh->name = GNUNET_malloc(strlen(name)
2165 - strlen(gnunet_tld) + 1);
2166 memset(csh->name, 0,
2167 strlen(name)-strlen(gnunet_tld) + 1);
2168 memcpy(csh->name, name,
2169 strlen(name)-strlen(gnunet_tld));
2171 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
2173 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2174 rh->authority_chain_tail = rh->authority_chain_head;
2175 rh->authority_chain_head->zone = zone_hash;
2176 rh->proc = &handle_shorten_delegation_result;
2177 rh->proc_cls = (void*)csh;
2179 /* Start delegation resolution in our namestore */
2180 resolve_delegation_ns(rh);
2185 * Send shorten response back to client
2187 * @param name the shortened name result or NULL if cannot be shortened
2188 * @param csh the handle to the shorten request
2191 send_shorten_response(const char* name, struct ClientShortenHandle *csh)
2193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
2194 "SHORTEN_RESULT", name);
2195 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
2202 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
2203 + strlen(name) + 1);
2205 rmsg->id = csh->unique_id;
2206 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
2208 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
2211 strcpy((char*)&rmsg[1], name);
2213 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
2214 (const struct GNUNET_MessageHeader *) rmsg,
2216 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
2219 GNUNET_free(csh->name);
2225 * Handle a shorten message from the api
2227 * @param cls the closure
2228 * @param client the client
2229 * @param message the message
2231 static void handle_shorten(void *cls,
2232 struct GNUNET_SERVER_Client * client,
2233 const struct GNUNET_MessageHeader * message)
2235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
2237 size_t msg_size = 0;
2238 struct ClientShortenHandle *csh;
2240 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
2242 GNUNET_break_op (0);
2243 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2247 GNUNET_SERVER_notification_context_add (nc, client);
2249 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
2250 (struct GNUNET_GNS_ClientShortenMessage *) message;
2252 msg_size = ntohs(message->size);
2254 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2256 GNUNET_break_op (0);
2257 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2261 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
2262 csh->client = client;
2263 csh->unique_id = sh_msg->id;
2265 shorten_name((char*)&sh_msg[1], csh);
2271 * Send get authority response back to client
2273 * @param name the shortened name result or NULL if cannot be shortened
2274 * @param cah the handle to the get authority request
2277 send_get_auth_response(const char* name, struct ClientGetAuthHandle *cah)
2279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
2280 "GET_AUTH_RESULT", name);
2281 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
2288 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
2289 + strlen(name) + 1);
2291 rmsg->id = cah->unique_id;
2292 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
2294 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
2297 strcpy((char*)&rmsg[1], name);
2299 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
2300 (const struct GNUNET_MessageHeader *) rmsg,
2302 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
2305 GNUNET_free(cah->name);
2311 * Process result from namestore delegation lookup
2312 * for get authority operation
2314 * @param cls the client get auth handle
2315 * @param rh the resolver handle
2316 * @param rd_count number of results (0)
2317 * @param rd data (NULL)
2320 handle_get_auth_delegation_result(void* cls,
2321 struct GNUNET_GNS_ResolverHandle *rh,
2323 const struct GNUNET_NAMESTORE_RecordData *rd)
2325 struct ClientGetAuthHandle* cah = (struct ClientGetAuthHandle*) cls;
2326 struct AuthorityChain *auth_chain;
2331 * At this point rh->name contains the part of the name
2332 * that we do not have a PKEY in our namestore to resolve.
2333 * The authority chain in the resolver handle is now
2334 * useful to backtrack if needed
2337 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2338 "PKEY resolved as far as possible in ns up to %s!\n", rh->name);
2340 if (is_canonical(rh->name))
2343 * We successfully resolved the authority in the ns
2344 * FIXME for our purposes this is fine
2345 * but maybe we want to have an api that also looks
2346 * into the dht (i.e. option in message)
2348 if (strlen(rh->name) > strlen(cah->name))
2350 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2351 "Record name longer than original lookup name... odd!\n");
2356 answer_len = strlen(cah->name) - strlen(rh->name);
2357 result = GNUNET_malloc(answer_len);
2358 memset(result, 0, answer_len);
2359 strcpy(result, cah->name + strlen(rh->name) + 1);
2361 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2362 "Our zone: Sending authority result %s\n", result);
2364 send_get_auth_response(result, cah);
2365 free_resolver_handle(rh);
2366 GNUNET_free(result);
2370 auth_chain = rh->authority_chain_head;
2371 /* backtrack authorities for pseu */
2372 GNUNET_NAMESTORE_zone_to_name (namestore_handle,
2375 &handle_shorten_zone_to_name,
2382 * Get authority for a given name
2384 * @param name the name to shorten
2385 * @param csh the shorten handle of the request
2388 get_authority(char* name, struct ClientGetAuthHandle* cah)
2391 struct GNUNET_GNS_ResolverHandle *rh;
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2394 "Starting authority resolution for %s (type=%d)!\n",
2395 name, GNUNET_GNS_RECORD_PKEY);
2397 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
2398 rh->authority = zone_hash;
2400 rh->name = GNUNET_malloc(strlen(name)
2401 - strlen(gnunet_tld) + 1);
2403 strlen(name)-strlen(gnunet_tld) + 1);
2404 memcpy(rh->name, name,
2405 strlen(name)-strlen(gnunet_tld));
2407 cah->name = GNUNET_malloc(strlen(name)
2408 - strlen(gnunet_tld) + 1);
2409 memset(cah->name, 0,
2410 strlen(name)-strlen(gnunet_tld) + 1);
2411 memcpy(cah->name, name,
2412 strlen(name)-strlen(gnunet_tld));
2414 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
2416 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2417 rh->authority_chain_tail = rh->authority_chain_head;
2418 rh->authority_chain_head->zone = zone_hash;
2419 rh->proc = &handle_get_auth_delegation_result;
2420 rh->proc_cls = (void*)cah;
2422 /* Start delegation resolution in our namestore */
2423 resolve_delegation_ns(rh);
2429 * Handle a get authority message from the api
2431 * @param cls the closure
2432 * @param client the client
2433 * @param message the message
2435 static void handle_get_authority(void *cls,
2436 struct GNUNET_SERVER_Client * client,
2437 const struct GNUNET_MessageHeader * message)
2439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
2441 size_t msg_size = 0;
2442 struct ClientGetAuthHandle *cah;
2444 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
2446 GNUNET_break_op (0);
2447 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2451 GNUNET_SERVER_notification_context_add (nc, client);
2453 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
2454 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
2456 msg_size = ntohs(message->size);
2458 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2460 GNUNET_break_op (0);
2461 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2465 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
2466 cah->client = client;
2467 cah->unique_id = sh_msg->id;
2469 get_authority((char*)&sh_msg[1], cah);
2475 * Reply to client with the result from our lookup.
2477 * @param cls the closure (our client lookup handle)
2478 * @param rh the request handle of the lookup
2479 * @param rd_count the number of records
2480 * @param rd the record data
2483 reply_to_client(void* cls, struct GNUNET_GNS_ResolverHandle *rh,
2485 const struct GNUNET_NAMESTORE_RecordData *rd)
2487 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
2488 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
2491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
2492 "LOOKUP_RESULT", rd_count);
2494 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
2495 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
2497 rmsg->id = clh->unique_id;
2498 rmsg->rd_count = htonl(rd_count);
2499 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
2501 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
2503 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
2505 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
2506 (const struct GNUNET_MessageHeader *) rmsg,
2508 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
2510 GNUNET_free(rh->proc_cls);
2511 free_resolver_handle(rh);
2513 GNUNET_free(clh->name);
2519 * Lookup a given name
2521 * @param name the name to looku[
2522 * @param clh the client lookup handle
2525 lookup_name(char* name, struct ClientLookupHandle* clh)
2528 struct GNUNET_GNS_ResolverHandle *rh;
2529 struct RecordLookupHandle* rlh;
2531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2532 "Starting resolution for %s (type=%d)!\n",
2535 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
2536 rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle));
2538 rh->authority = zone_hash;
2540 rlh->record_type = clh->type;
2541 rlh->name = clh->name;
2542 rlh->proc = &reply_to_client;
2543 rlh->proc_cls = clh;
2547 rh->name = GNUNET_malloc(strlen(name)
2548 - strlen(gnunet_tld) + 1);
2550 strlen(name)-strlen(gnunet_tld) + 1);
2551 memcpy(rh->name, name,
2552 strlen(name)-strlen(gnunet_tld));
2554 rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH);
2556 rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain));
2557 rh->authority_chain_head->prev = NULL;
2558 rh->authority_chain_head->next = NULL;
2559 rh->authority_chain_tail = rh->authority_chain_head;
2560 rh->authority_chain_head->zone = zone_hash;
2562 /* Start resolution in our zone */
2563 rh->proc = &process_delegation_ns;
2564 resolve_delegation_ns(rh);
2569 * Handle lookup requests from client
2571 * @param cls the closure
2572 * @param client the client
2573 * @param message the message
2576 handle_lookup(void *cls,
2577 struct GNUNET_SERVER_Client * client,
2578 const struct GNUNET_MessageHeader * message)
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
2582 size_t msg_size = 0;
2583 struct ClientLookupHandle *clh;
2585 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
2587 GNUNET_break_op (0);
2588 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2592 GNUNET_SERVER_notification_context_add (nc, client);
2594 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
2595 (struct GNUNET_GNS_ClientLookupMessage *) message;
2597 msg_size = ntohs(message->size);
2599 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2601 GNUNET_break_op (0);
2602 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2606 clh = GNUNET_malloc(sizeof(struct ClientLookupHandle));
2607 clh->client = client;
2608 clh->name = GNUNET_malloc(strlen((char*)&sh_msg[1]) + 1);
2609 strcpy(clh->name, (char*)&sh_msg[1]);
2610 clh->unique_id = sh_msg->id;
2611 clh->type = ntohl(sh_msg->type);
2613 lookup_name((char*)&sh_msg[1], clh);
2619 * Process GNS requests.
2621 * @param cls closure)
2622 * @param server the initialized server
2623 * @param c configuration to use
2626 run (void *cls, struct GNUNET_SERVER_Handle *server,
2627 const struct GNUNET_CONFIGURATION_Handle *c)
2630 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
2633 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2635 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2636 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
2637 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
2638 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
2641 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
2642 "ZONEKEY", &keyfile))
2644 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2645 "No private key for root zone specified%s!\n", keyfile);
2646 GNUNET_SCHEDULER_shutdown(0);
2650 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2651 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
2653 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2655 GNUNET_free(keyfile);
2660 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
2663 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2664 "DNS hijacking enabled... connecting to service.\n");
2666 * Do gnunet dns init here
2668 dns_handle = GNUNET_DNS_connect(c,
2669 GNUNET_DNS_FLAG_PRE_RESOLUTION,
2670 &handle_dns_request, /* rh */
2671 NULL); /* Closure */
2672 if (NULL == dns_handle)
2674 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2675 "Failed to connect to the dnsservice!\n");
2682 * handle to our local namestore
2684 namestore_handle = GNUNET_NAMESTORE_connect(c);
2686 if (NULL == namestore_handle)
2688 //FIXME do error handling;
2689 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2690 "Failed to connect to the namestore!\n");
2691 GNUNET_SCHEDULER_shutdown(0);
2698 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
2700 if (NULL == dht_handle)
2702 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
2705 //put_some_records(); //FIXME for testing
2708 * Schedule periodic put
2710 * We have roughly an hour for all records;
2712 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
2714 //zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
2716 GNUNET_SERVER_add_handlers (server, handlers);
2719 //GNUNET_SERVER_disconnect_notify (server,
2720 // &client_disconnect_notification,
2723 nc = GNUNET_SERVER_notification_context_create (server, 1);
2725 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
2731 * The main function for the GNS service.
2733 * @param argc number of arguments from the command line
2734 * @param argv command line arguments
2735 * @return 0 ok, 1 on error
2738 main (int argc, char *const *argv)
2744 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
2749 /* end of gnunet-service-gns.c */