2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011, 2012 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.
21 * @file gns/gnunet-service-gns.c
22 * @brief GNUnet GNS service
23 * @author Martin Schanzenbach
26 #include "gnunet_util_lib.h"
27 #include "gnunet_transport_service.h"
28 #include "gnunet_dns_service.h"
29 #include "gnunet_dnsparser_lib.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_gns_service.h"
33 #include "gnunet_statistics_service.h"
35 #include "gns_common.h"
36 #include "gnunet-service-gns_resolver.h"
37 #include "gnunet-service-gns_interceptor.h"
38 #include "gnunet_protocols.h"
41 * The initial interval in milliseconds btween puts in
44 #define INITIAL_PUT_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
47 * The upper bound for the zone iteration interval in milliseconds
49 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
52 * The default put interval for the zone iteration. In case
55 #define DEFAULT_ZONE_PUBLISH_TIME_WINDOW GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
58 * The factor the current zone iteration interval is divided by for each
59 * additional new record
61 #define LATE_ITERATION_SPEEDUP_FACTOR 2
65 * Handle to a lookup operation from api
67 struct ClientLookupHandle
71 * Handle to the requesting client
73 struct GNUNET_SERVER_Client *client;
76 * optional zone private key used for shorten
78 struct GNUNET_CRYPTO_EccPrivateKey *shorten_key;
86 * The zone we look up in
88 struct GNUNET_CRYPTO_ShortHashCode zone;
96 * GNUNET_YES if we only want to lookup from local cache
108 * Our handle to the DHT
110 static struct GNUNET_DHT_Handle *dht_handle;
113 * Our zone's private key
115 static struct GNUNET_CRYPTO_EccPrivateKey *zone_key;
118 * Our handle to the namestore service
120 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
123 * Handle to iterate over our authoritative zone in namestore
125 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
128 * Our notification context.
130 static struct GNUNET_SERVER_NotificationContext *nc;
135 static struct GNUNET_CRYPTO_ShortHashCode zone_hash;
138 * Useful for zone update for DHT put
140 static unsigned long long num_public_records;
143 * Last seen record count
145 static unsigned long long last_num_public_records;
148 * Zone iteration PUT interval.
150 static struct GNUNET_TIME_Relative put_interval;
153 * Time window for zone iteration
155 static struct GNUNET_TIME_Relative zone_publish_time_window;
160 static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
163 * GNUNET_YES if automatic pkey import for name shortening
166 static int auto_import_pkey;
169 * GNUNET_YES if zone has never been published before
171 static int first_zone_iteration;
176 static struct GNUNET_TIME_Relative default_lookup_timeout;
179 * GNUNET_YES if ipv6 is supported
181 static int v6_enabled;
184 * GNUNET_YES if ipv4 is supported
186 static int v4_enabled;
189 * Handle to the statistics service
191 static struct GNUNET_STATISTICS_Handle *statistics;
195 * Task run during shutdown.
201 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
203 struct ClientShortenHandle *csh_tmp;
205 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
207 GNUNET_SERVER_notification_context_destroy (nc);
208 gns_interceptor_stop ();
209 gns_resolver_cleanup ();
210 if (NULL != statistics)
212 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
215 if (GNUNET_SCHEDULER_NO_TASK != zone_publish_task)
217 GNUNET_SCHEDULER_cancel (zone_publish_task);
218 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
220 if (NULL != namestore_iter)
222 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
223 namestore_iter = NULL;
225 if (NULL != namestore_handle)
227 GNUNET_NAMESTORE_disconnect (namestore_handle);
228 namestore_handle = NULL;
230 if (NULL != dht_handle)
232 GNUNET_DHT_disconnect (dht_handle);
239 * Method called periodically that triggers iteration over authoritative records
242 * @param tc task context
245 publish_zone_dht_next (void *cls,
246 const struct GNUNET_SCHEDULER_TaskContext *tc)
248 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
249 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
254 * Periodically iterate over our zone and store everything in dht
257 * @param tc task context
260 publish_zone_dht_start (void *cls,
261 const struct GNUNET_SCHEDULER_TaskContext *tc);
265 * Function used to put all records successively into the DHT.
267 * @param cls the closure (NULL)
268 * @param key the public key of the authority (ours)
269 * @param expiration lifetime of the namestore entry
270 * @param name the name of the records
271 * @param rd_count the number of records in data
272 * @param rd the record data
273 * @param signature the signature for the record data
276 put_gns_record (void *cls,
277 const struct GNUNET_CRYPTO_EccPublicKey *key,
278 struct GNUNET_TIME_Absolute expiration,
280 unsigned int rd_count,
281 const struct GNUNET_NAMESTORE_RecordData *rd,
282 const struct GNUNET_CRYPTO_EccSignature *signature)
284 struct GNSNameRecordBlock *nrb;
285 struct GNUNET_CRYPTO_ShortHashCode zhash;
286 struct GNUNET_HashCode dht_key;
287 uint32_t rd_payload_length;
288 char* nrb_data = NULL;
290 struct GNUNET_TIME_Relative next_put_interval;
295 namestore_iter = NULL;
296 last_num_public_records = num_public_records;
297 first_zone_iteration = GNUNET_NO;
298 if (0 == num_public_records)
301 * If no records are known (startup) or none present
302 * we can safely set the interval to the value for a single
305 put_interval = zone_publish_time_window;
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
307 "No records in db.\n");
311 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
314 put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
319 GNUNET_STRINGS_relative_time_to_string (put_interval, GNUNET_YES));
320 GNUNET_STATISTICS_set (statistics,
321 "Current zone iteration interval (in ms)",
322 put_interval.rel_value_us / 1000LL,
324 GNUNET_STATISTICS_update (statistics,
325 "Number of zone iterations", 1, GNUNET_NO);
326 GNUNET_STATISTICS_set (statistics,
327 "Number of public records in DHT",
328 last_num_public_records,
330 if (0 == num_public_records)
331 zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
332 &publish_zone_dht_start,
335 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, NULL);
339 namelen = strlen (name) + 1;
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "No records for name `%s'! Skipping.\n",
345 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
349 if (NULL == signature)
352 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
357 /* TODO 2) AB: New publishing
359 * - Use new signature S_d
360 * - Obtain new derived public key V = H(H(i,Q) * Q)
362 * - Compute encrypte record block E with HKDF(i,Q) (rd, rd_count)
363 * - Create block B = |V,E,S_d|
364 * - Compute new DHT key H(V) in TODO 3)
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Putting records for `%s' into the DHT\n", name);
370 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
371 nrb = GNUNET_malloc (rd_payload_length + namelen
372 + sizeof (struct GNSNameRecordBlock));
373 nrb->signature = *signature;
374 nrb->public_key = *key;
375 nrb->rd_count = htonl (rd_count);
376 memcpy (&nrb[1], name, namelen);
377 nrb_data = (char *) &nrb[1];
379 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
380 GNUNET_CRYPTO_short_hash (key,
381 sizeof (struct GNUNET_CRYPTO_EccPublicKey),
383 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
388 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
389 _("Records for name `%s' in zone %s too large to fit into DHT"),
391 GNUNET_short_h2s (&zhash));
393 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
397 /* TODO AB: Here records are put in the DHT: modify dht_key to H(key) = H(H(name,zone) * zone) */
398 GNUNET_GNS_get_key_for_record (name, &zhash, &dht_key);
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "putting %u records from zone %s for `%s' under key: %s with size %u and timeout %s\n",
402 GNUNET_short_h2s (&zhash),
404 GNUNET_h2s (&dht_key),
405 (unsigned int) rd_payload_length,
406 GNUNET_STRINGS_relative_time_to_string (DHT_OPERATION_TIMEOUT, GNUNET_YES));
408 GNUNET_STATISTICS_update (statistics,
409 "Record bytes put into DHT",
410 rd_payload_length, GNUNET_NO);
412 (void) GNUNET_DHT_put (dht_handle, &dht_key,
413 DHT_GNS_REPLICATION_LEVEL,
414 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
415 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
419 DHT_OPERATION_TIMEOUT,
424 num_public_records++;
425 if ( (num_public_records > last_num_public_records)
426 && (GNUNET_NO == first_zone_iteration) )
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Last record count was lower than current record count. Reducing interval.\n");
430 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
432 next_put_interval = GNUNET_TIME_relative_divide (put_interval,
433 LATE_ITERATION_SPEEDUP_FACTOR);
436 next_put_interval = put_interval;
438 GNUNET_STATISTICS_set (statistics,
439 "Current zone iteration interval (ms)",
440 next_put_interval.rel_value_us / 1000LL,
442 zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
443 &publish_zone_dht_next,
449 * Periodically iterate over our zone and store everything in dht
452 * @param tc task context
455 publish_zone_dht_start (void *cls,
456 const struct GNUNET_SCHEDULER_TaskContext *tc)
458 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
461 "Scheduling DHT zone update!\n");
462 /* start counting again */
463 num_public_records = 0;
464 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
465 NULL, /* All zones */
466 GNUNET_NAMESTORE_RF_AUTHORITY,
467 GNUNET_NAMESTORE_RF_PRIVATE,
473 /* END DHT ZONE PROPAGATION */
477 * Reply to client with the result from our lookup.
479 * @param cls the closure (our client lookup handle)
480 * @param rd_count the number of records
481 * @param rd the record data
484 send_lookup_response (void* cls,
486 const struct GNUNET_NAMESTORE_RecordData *rd)
488 struct ClientLookupHandle* clh = cls;
489 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
493 "LOOKUP_RESULT", rd_count);
495 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
496 rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage));
498 rmsg->id = clh->request_id;
499 rmsg->rd_count = htonl(rd_count);
500 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
502 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
504 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
506 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
507 (const struct GNUNET_MessageHeader *) rmsg,
509 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
512 GNUNET_free(clh->name);
514 if (NULL != clh->shorten_key)
515 GNUNET_CRYPTO_ecc_key_free (clh->shorten_key);
517 GNUNET_STATISTICS_update (statistics,
518 "Completed lookups", 1, GNUNET_NO);
520 GNUNET_STATISTICS_update (statistics,
521 "Records resolved", rd_count, GNUNET_NO);
526 * Handle lookup requests from client
528 * @param cls the closure
529 * @param client the client
530 * @param message the message
533 handle_lookup (void *cls,
534 struct GNUNET_SERVER_Client * client,
535 const struct GNUNET_MessageHeader * message)
538 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
539 struct ClientLookupHandle *clh;
540 char* nameptr = name;
543 const struct GNUNET_CRYPTO_EccPrivateKey *key;
545 const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548 "Received `%s' message\n", "LOOKUP");
549 msg_size = ntohs(message->size);
550 if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
553 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
556 sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
557 GNUNET_SERVER_notification_context_add (nc, client);
558 if (GNUNET_YES == ntohl (sh_msg->have_key))
560 key = &sh_msg->shorten_key;
566 utf_in = (const char *) &sh_msg[1];
567 if ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientLookupMessage) - 1])
570 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
573 GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
575 namelen = strlen (name) + 1;
576 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
577 memset (clh, 0, sizeof (struct ClientLookupHandle));
578 clh->client = client;
579 clh->name = GNUNET_malloc (namelen);
580 strcpy (clh->name, name);
581 clh->request_id = sh_msg->id;
582 clh->type = ntohl (sh_msg->type);
585 clh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
586 *clh->shorten_key = *key;
588 only_cached = ntohl (sh_msg->only_cached);
590 if (strlen (name) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) {
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592 "LOOKUP: %s is too long", name);
594 send_lookup_response (clh, 0, NULL);
598 if ((GNUNET_DNSPARSER_TYPE_A == clh->type) &&
599 (GNUNET_OK != v4_enabled))
601 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
602 "LOOKUP: Query for A record but AF_INET not supported!");
604 send_lookup_response (clh, 0, NULL);
608 if ((GNUNET_DNSPARSER_TYPE_AAAA == clh->type) &&
609 (GNUNET_OK != v6_enabled))
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
612 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
614 send_lookup_response (clh, 0, NULL);
618 if (GNUNET_NO == ntohl (sh_msg->have_zone))
619 clh->zone = zone_hash; /* Default zone */
621 clh->zone = sh_msg->zone;
623 if (GNUNET_YES == auto_import_pkey)
625 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
627 default_lookup_timeout,
629 &send_lookup_response, clh);
633 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
635 default_lookup_timeout,
637 &send_lookup_response, clh);
639 GNUNET_STATISTICS_update (statistics,
640 "Record lookup attempts", 1, GNUNET_NO);
645 * Process GNS requests.
648 * @param server the initialized server
649 * @param c configuration to use
652 run (void *cls, struct GNUNET_SERVER_Handle *server,
653 const struct GNUNET_CONFIGURATION_Handle *c)
655 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
656 { &handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
659 struct GNUNET_CRYPTO_EccPublicKey pkey;
660 unsigned long long max_parallel_bg_queries = 0;
661 int ignore_pending = GNUNET_NO;
663 v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
664 v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
666 namestore_handle = GNUNET_NAMESTORE_connect (c);
667 if (NULL == namestore_handle)
669 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
670 _("Failed to connect to the namestore!\n"));
671 GNUNET_SCHEDULER_shutdown ();
675 auto_import_pkey = GNUNET_NO;
677 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
681 "Automatic PKEY import is enabled.\n");
682 auto_import_pkey = GNUNET_YES;
684 put_interval = INITIAL_PUT_INTERVAL;
685 zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
688 GNUNET_CONFIGURATION_get_value_time (c, "gns",
689 "ZONE_PUBLISH_TIME_WINDOW",
690 &zone_publish_time_window))
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693 "Time window for zone iteration: %s\n",
694 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window, GNUNET_YES));
697 GNUNET_CONFIGURATION_get_value_number (c, "gns",
698 "MAX_PARALLEL_BACKGROUND_QUERIES",
699 &max_parallel_bg_queries))
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "Number of allowed parallel background queries: %llu\n",
703 max_parallel_bg_queries);
707 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
708 "AUTO_IMPORT_CONFIRMATION_REQ"))
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
711 "Auto import requires user confirmation\n");
712 ignore_pending = GNUNET_YES;
716 GNUNET_CONFIGURATION_get_value_time (c, "gns",
717 "DEFAULT_LOOKUP_TIMEOUT",
718 &default_lookup_timeout))
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
721 "Default lookup timeout: %s\n",
722 GNUNET_STRINGS_relative_time_to_string (default_lookup_timeout,
726 dht_handle = GNUNET_DHT_connect (c,
727 (unsigned int) max_parallel_bg_queries);
728 if (NULL == dht_handle)
730 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
731 _("Could not connect to DHT!\n"));
732 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
737 gns_resolver_init (namestore_handle, dht_handle, zone_hash, c,
738 max_parallel_bg_queries,
741 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
742 _("Unable to initialize resolver!\n"));
743 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
748 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
750 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
751 "DNS hijacking enabled. Connecting to DNS service.\n");
754 gns_interceptor_init (zone_hash, zone_key, c))
756 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
757 "Failed to enable the DNS interceptor!\n");
762 * Schedule periodic put for our records We have roughly an hour for
765 first_zone_iteration = GNUNET_YES;
766 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, NULL);
767 GNUNET_SERVER_add_handlers (server, handlers);
768 statistics = GNUNET_STATISTICS_create ("gns", c);
769 nc = GNUNET_SERVER_notification_context_create (server, 1);
770 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
776 * The main function for the GNS service.
778 * @param argc number of arguments from the command line
779 * @param argv command line arguments
780 * @return 0 ok, 1 on error
783 main (int argc, char *const *argv)
789 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
794 /* end of gnunet-service-gns.c */