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"
41 #include "gnunet-service-gns_resolver.h"
42 #include "gnunet-service-gns_interceptor.h"
44 /* FIXME move to proper header in include */
45 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23
46 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24
47 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN 25
48 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT 26
49 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH 27
50 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT 28
54 * Handle to a shorten operation from api
56 struct ClientShortenHandle
58 /* the requesting client that */
59 struct GNUNET_SERVER_Client *client;
65 enum GNUNET_GNS_RecordType type;
74 * Handle to a get auhtority operation from api
76 struct ClientGetAuthHandle
78 /* the requesting client that */
79 struct GNUNET_SERVER_Client *client;
84 /* name to lookup authority */
91 * Handle to a lookup operation from api
93 struct ClientLookupHandle
95 /* the requesting client that */
96 struct GNUNET_SERVER_Client *client;
102 enum GNUNET_GNS_RecordType type;
104 /* the name to look up */
105 char* name; //Needed?
109 * Our handle to the DHT
111 struct GNUNET_DHT_Handle *dht_handle;
114 * Our zone's private key
116 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
119 * Our handle to the namestore service
120 * FIXME maybe need a second handle for iteration
122 struct GNUNET_NAMESTORE_Handle *namestore_handle;
125 * Handle to iterate over our authoritative zone in namestore
127 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
130 * The configuration the GNS service is running with
132 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
135 * Our notification context.
137 static struct GNUNET_SERVER_NotificationContext *nc;
142 GNUNET_HashCode zone_hash;
145 * Useful for zone update for DHT put
147 static int num_public_records = 3600;
149 /* dht update interval FIXME define? */
150 static struct GNUNET_TIME_Relative dht_update_interval;
152 /* zone update task */
153 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
156 * Task run during shutdown.
162 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
165 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
167 /* Kill zone task for it may make the scheduler hang */
168 if (zone_update_taskid)
169 GNUNET_SCHEDULER_cancel(zone_update_taskid);
171 GNUNET_SERVER_notification_context_destroy (nc);
173 gns_interceptor_stop();
175 GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
176 GNUNET_DHT_disconnect(dht_handle);
181 * Method called periodicattluy that triggers
182 * iteration over root zone
185 * @param tc task context
188 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
194 * Continuation for DHT put
197 * @param tc task context
200 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
202 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
207 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
210 * Function used to put all records successively into the DHT.
212 * @param cls the closure (NULL)
213 * @param key the public key of the authority (ours)
214 * @param expiration lifetime of the namestore entry
215 * @param name the name of the records
216 * @param rd_count the number of records in data
217 * @param rd the record data
218 * @param signature the signature for the record data
221 put_gns_record(void *cls,
222 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
223 struct GNUNET_TIME_Absolute expiration,
225 unsigned int rd_count,
226 const struct GNUNET_NAMESTORE_RecordData *rd,
227 const struct GNUNET_CRYPTO_RsaSignature *signature)
230 struct GNSNameRecordBlock *nrb;
231 GNUNET_HashCode name_hash;
232 GNUNET_HashCode xor_hash;
233 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
234 uint32_t rd_payload_length;
235 char* nrb_data = NULL;
241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
242 "Zone iteration finished. Rescheduling put in %ds\n",
243 GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL);
244 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
245 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (
246 GNUNET_TIME_relative_multiply(
247 GNUNET_TIME_UNIT_SECONDS,
248 GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL
250 &update_zone_dht_start,
255 namelen = strlen(name) + 1;
257 if (signature == NULL)
259 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
260 "No signature for %s record data provided! Skipping...\n",
262 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
268 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
269 "Putting records for %s into the DHT\n", name);
271 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
273 nrb = GNUNET_malloc(rd_payload_length + namelen
274 + sizeof(struct GNSNameRecordBlock));
276 nrb->signature = *signature;
278 nrb->public_key = *key;
280 nrb->rd_count = htonl(rd_count);
282 memcpy(&nrb[1], name, namelen);
284 nrb_data = (char*)&nrb[1];
287 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
289 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
294 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
295 "Record serialization failed! Skipping...\n");
297 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
304 * calculate DHT key: H(name) xor H(pubkey)
306 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
307 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
308 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
310 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
311 "putting records for %s under key: %s with size %d\n",
312 name, (char*)&xor_hash_string, rd_payload_length);
314 GNUNET_DHT_put (dht_handle, &xor_hash,
315 DHT_GNS_REPLICATION_LEVEL,
317 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
321 DHT_OPERATION_TIMEOUT,
323 NULL); //cls for cont
325 num_public_records++;
328 * Reschedule periodic put
330 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
331 &update_zone_dht_next,
339 * Periodically iterate over our zone and store everything in dht
342 * @param tc task context
345 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
347 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
348 if (0 == num_public_records)
350 dht_update_interval = GNUNET_TIME_relative_multiply(
351 GNUNET_TIME_UNIT_SECONDS,
352 GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL);
353 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
354 "No records in db. Adjusted DHT update interval to %ds\n",
355 GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL);
360 dht_update_interval = GNUNET_TIME_relative_multiply(
361 GNUNET_TIME_UNIT_SECONDS,
362 (3600/num_public_records));
363 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
364 "Adjusted DHT update interval to %ds!\n",
365 (3600/num_public_records));
368 /* start counting again */
369 num_public_records = 0;
370 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
372 GNUNET_NAMESTORE_RF_AUTHORITY,
373 GNUNET_NAMESTORE_RF_PRIVATE,
379 /* END DHT ZONE PROPAGATION */
382 * Send shorten response back to client
384 * @param name the shortened name result or NULL if cannot be shortened
385 * @param csh the handle to the shorten request
388 send_shorten_response(void* cls, const char* name)
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
391 "SHORTEN_RESULT", name);
392 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
393 struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
400 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
403 rmsg->id = csh->unique_id;
404 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
406 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
409 strcpy((char*)&rmsg[1], name);
411 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
412 (const struct GNUNET_MessageHeader *) rmsg,
414 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
417 GNUNET_free(csh->name);
423 * Handle a shorten message from the api
425 * @param cls the closure
426 * @param client the client
427 * @param message the message
429 static void handle_shorten(void *cls,
430 struct GNUNET_SERVER_Client * client,
431 const struct GNUNET_MessageHeader * message)
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
436 struct ClientShortenHandle *csh;
439 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
442 GNUNET_SERVER_receive_done (client, GNUNET_OK);
446 GNUNET_SERVER_notification_context_add (nc, client);
448 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
449 (struct GNUNET_GNS_ClientShortenMessage *) message;
451 msg_size = ntohs(message->size);
453 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
456 GNUNET_SERVER_receive_done (client, GNUNET_OK);
460 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
461 csh->client = client;
462 csh->unique_id = sh_msg->id;
464 name = (char*)&sh_msg[1];
465 csh->name = GNUNET_malloc(strlen(name)
466 - strlen(GNUNET_GNS_TLD) + 1);
468 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
469 memcpy(csh->name, name,
470 strlen(name)-strlen(GNUNET_GNS_TLD));
472 /* Start shortening */
473 gns_resolver_shorten_name(zone_hash, name, &send_shorten_response, csh);
478 * Send get authority response back to client
480 * @param name the shortened name result or NULL if cannot be shortened
481 * @param cah the handle to the get authority request
484 send_get_auth_response(void *cls, const char* name)
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
487 "GET_AUTH_RESULT", name);
488 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
489 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
496 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
499 rmsg->id = cah->unique_id;
500 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
502 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
505 strcpy((char*)&rmsg[1], name);
507 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
508 (const struct GNUNET_MessageHeader *) rmsg,
510 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up handles...\n");
515 GNUNET_free(cah->name);
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done.\n");
524 * Handle a get authority message from the api
526 * @param cls the closure
527 * @param client the client
528 * @param message the message
530 static void handle_get_authority(void *cls,
531 struct GNUNET_SERVER_Client * client,
532 const struct GNUNET_MessageHeader * message)
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
537 struct ClientGetAuthHandle *cah;
540 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
543 GNUNET_SERVER_receive_done (client, GNUNET_OK);
547 GNUNET_SERVER_notification_context_add (nc, client);
549 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
550 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
552 msg_size = ntohs(message->size);
554 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
557 GNUNET_SERVER_receive_done (client, GNUNET_OK);
561 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
562 cah->client = client;
563 cah->unique_id = sh_msg->id;
565 name = (char*)&sh_msg[1];
566 cah->name = GNUNET_malloc(strlen(name)
567 - strlen(GNUNET_GNS_TLD) + 1);
569 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
570 memcpy(cah->name, name,
571 strlen(name)-strlen(GNUNET_GNS_TLD));
573 /* Start delegation resolution in our namestore */
574 gns_resolver_get_authority(zone_hash, name, &send_get_auth_response, cah);
579 * Reply to client with the result from our lookup.
581 * @param cls the closure (our client lookup handle)
582 * @param rh the request handle of the lookup
583 * @param rd_count the number of records
584 * @param rd the record data
587 send_lookup_response(void* cls,
589 const struct GNUNET_NAMESTORE_RecordData *rd)
591 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
592 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
596 "LOOKUP_RESULT", rd_count);
598 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
599 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
601 rmsg->id = clh->unique_id;
602 rmsg->rd_count = htonl(rd_count);
603 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
605 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
607 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
609 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
610 (const struct GNUNET_MessageHeader *) rmsg,
612 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
615 GNUNET_free(clh->name);
622 * Handle lookup requests from client
624 * @param cls the closure
625 * @param client the client
626 * @param message the message
629 handle_lookup(void *cls,
630 struct GNUNET_SERVER_Client * client,
631 const struct GNUNET_MessageHeader * message)
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
638 struct ClientLookupHandle *clh;
640 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
643 GNUNET_SERVER_receive_done (client, GNUNET_OK);
647 GNUNET_SERVER_notification_context_add (nc, client);
649 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
650 (struct GNUNET_GNS_ClientLookupMessage *) message;
652 msg_size = ntohs(message->size);
654 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
657 GNUNET_SERVER_receive_done (client, GNUNET_OK);
661 name = (char*)&sh_msg[1];
662 namelen = strlen(name)+1;
663 clh = GNUNET_malloc(sizeof(struct ClientLookupHandle));
664 clh->client = client;
665 clh->name = GNUNET_malloc(namelen);
666 strcpy(clh->name, name);
667 clh->unique_id = sh_msg->id;
668 clh->type = ntohl(sh_msg->type);
670 gns_resolver_lookup_record(zone_hash, clh->type, name,
671 &send_lookup_response, clh);
677 * Process GNS requests.
679 * @param cls closure)
680 * @param server the initialized server
681 * @param c configuration to use
684 run (void *cls, struct GNUNET_SERVER_Handle *server,
685 const struct GNUNET_CONFIGURATION_Handle *c)
688 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
691 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
693 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
694 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
695 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
696 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
699 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
700 "ZONEKEY", &keyfile))
702 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
703 "No private key for root zone specified!\n");
704 GNUNET_SCHEDULER_shutdown(0);
708 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
709 "Using keyfile %s for root zone.\n", keyfile);
711 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
712 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
714 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
716 GNUNET_free(keyfile);
720 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
723 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
724 "DNS hijacking enabled... connecting to service.\n");
726 if (gns_interceptor_init(zone_hash, c) == GNUNET_SYSERR)
728 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
729 "Failed to enable the dns interceptor!\n");
736 * handle to our local namestore
738 namestore_handle = GNUNET_NAMESTORE_connect(c);
740 if (NULL == namestore_handle)
742 //FIXME do error handling;
743 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
744 "Failed to connect to the namestore!\n");
745 GNUNET_SCHEDULER_shutdown(0);
752 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
754 if (NULL == dht_handle)
756 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
759 //put_some_records(); //FIXME for testing
762 * Schedule periodic put
764 * We have roughly an hour for all records;
766 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
768 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
770 GNUNET_SERVER_add_handlers (server, handlers);
773 //GNUNET_SERVER_disconnect_notify (server,
774 // &client_disconnect_notification,
777 nc = GNUNET_SERVER_notification_context_create (server, 1);
779 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
785 * The main function for the GNS service.
787 * @param argc number of arguments from the command line
788 * @param argv command line arguments
789 * @return 0 ok, 1 on error
792 main (int argc, char *const *argv)
798 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
803 /* end of gnunet-service-gns.c */