2 This file is part of GNUnet.
3 (C) 2011-2013 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 GNU Name System (main service)
23 * @author Martin Schanzenbach
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.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 "gnunet-service-gns_resolver.h"
36 #include "gnunet-service-gns_shorten.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
64 * How long until a DHT PUT attempt should time out?
66 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
69 * What replication level do we use for DHT PUT operations?
71 #define DHT_GNS_REPLICATION_LEVEL 5
75 * Handle to a lookup operation from api
77 struct ClientLookupHandle
81 * We keep these in a DLL.
83 struct ClientLookupHandle *next;
86 * We keep these in a DLL.
88 struct ClientLookupHandle *prev;
91 * Handle to the requesting client
93 struct GNUNET_SERVER_Client *client;
96 * Active handle for the lookup.
98 struct GNS_ResolverHandle *lookup;
109 * Our handle to the DHT
111 static struct GNUNET_DHT_Handle *dht_handle;
114 * Active DHT put operation (or NULL)
116 static struct GNUNET_DHT_PutHandle *active_put;
119 * Our handle to the namestore service
121 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
124 * Handle to iterate over our authoritative zone in namestore
126 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
129 * Our notification context.
131 static struct GNUNET_SERVER_NotificationContext *nc;
136 static struct ClientLookupHandle *clh_head;
141 static struct ClientLookupHandle *clh_tail;
144 * Useful for zone update for DHT put
146 static unsigned long long num_public_records;
149 * Last seen record count
151 static unsigned long long last_num_public_records;
154 * Zone iteration PUT interval.
156 static struct GNUNET_TIME_Relative put_interval;
159 * Time window for zone iteration
161 static struct GNUNET_TIME_Relative zone_publish_time_window;
166 static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
169 * #GNUNET_YES if zone has never been published before
171 static int first_zone_iteration;
174 * #GNUNET_YES if ipv6 is supported
176 static int v6_enabled;
179 * #GNUNET_YES if ipv4 is supported
181 static int v4_enabled;
184 * Handle to the statistics service
186 static struct GNUNET_STATISTICS_Handle *statistics;
190 * Task run during shutdown.
196 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
198 struct ClientLookupHandle *clh;
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 GNUNET_SERVER_notification_context_destroy (nc);
203 while (NULL != (clh = clh_head))
205 GNUNET_SERVER_client_set_user_context (clh->client, NULL);
206 GNS_resolver_lookup_cancel (clh->lookup);
207 GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
211 GNS_interceptor_done ();
212 GNS_resolver_done ();
214 if (NULL != statistics)
216 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
219 if (GNUNET_SCHEDULER_NO_TASK != zone_publish_task)
221 GNUNET_SCHEDULER_cancel (zone_publish_task);
222 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
224 if (NULL != namestore_iter)
226 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
227 namestore_iter = NULL;
229 if (NULL != namestore_handle)
231 GNUNET_NAMESTORE_disconnect (namestore_handle);
232 namestore_handle = NULL;
234 if (NULL != active_put)
236 GNUNET_DHT_put_cancel (active_put);
239 if (NULL != dht_handle)
241 GNUNET_DHT_disconnect (dht_handle);
248 * Method called periodically that triggers iteration over authoritative records
251 * @param tc task context
254 publish_zone_dht_next (void *cls,
255 const struct GNUNET_SCHEDULER_TaskContext *tc)
257 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
258 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
263 * Periodically iterate over our zone and store everything in dht
266 * @param tc task context
269 publish_zone_dht_start (void *cls,
270 const struct GNUNET_SCHEDULER_TaskContext *tc);
274 * Continuation called from DHT once the PUT operation is done.
276 * @param cls closure, NULL
277 * @param success #GNUNET_OK on success
280 dht_put_continuation (void *cls,
283 struct GNUNET_TIME_Relative next_put_interval;
286 num_public_records++;
287 if ( (num_public_records > last_num_public_records) &&
288 (GNUNET_NO == first_zone_iteration) )
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Last record count was lower than current record count. Reducing interval.\n");
292 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
294 next_put_interval = GNUNET_TIME_relative_divide (put_interval,
295 LATE_ITERATION_SPEEDUP_FACTOR);
298 next_put_interval = put_interval;
300 GNUNET_STATISTICS_set (statistics,
301 "Current zone iteration interval (ms)",
302 next_put_interval.rel_value_us / 1000LL,
304 zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
305 &publish_zone_dht_next,
311 * Function used to put all records successively into the DHT.
313 * @param cls the closure (NULL)
314 * @param key the private key of the authority (ours)
315 * @param name the name of the records, NULL once the iteration is done
316 * @param rd_count the number of records in @a rd
317 * @param rd the record data
320 put_gns_record (void *cls,
321 const struct GNUNET_CRYPTO_EccPrivateKey *key,
323 unsigned int rd_count,
324 const struct GNUNET_NAMESTORE_RecordData *rd)
326 struct GNUNET_NAMESTORE_Block *block;
327 struct GNUNET_HashCode query;
328 struct GNUNET_TIME_Absolute expire;
329 struct GNUNET_TIME_Absolute now;
331 struct GNUNET_NAMESTORE_RecordData rd_public[rd_count];
332 unsigned int rd_public_count;
337 /* we're done with one iteration, calculate when to do the next one */
338 namestore_iter = NULL;
339 last_num_public_records = num_public_records;
340 first_zone_iteration = GNUNET_NO;
341 if (0 == num_public_records)
344 * If no records are known (startup) or none present
345 * we can safely set the interval to the value for a single
348 put_interval = zone_publish_time_window;
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
350 "No records in namestore database.\n");
354 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
357 put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
362 GNUNET_STRINGS_relative_time_to_string (put_interval, GNUNET_YES));
363 GNUNET_STATISTICS_set (statistics,
364 "Current zone iteration interval (in ms)",
365 put_interval.rel_value_us / 1000LL,
367 GNUNET_STATISTICS_update (statistics,
368 "Number of zone iterations",
371 GNUNET_STATISTICS_set (statistics,
372 "Number of public records in DHT",
373 last_num_public_records,
375 if (0 == num_public_records)
376 zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
377 &publish_zone_dht_start,
380 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
385 /* filter out records that are not public, and convert to
386 absolute expiration time. */
388 now = GNUNET_TIME_absolute_get ();
389 for (i=0;i<rd_count;i++)
390 if (0 == (rd[i].flags & (GNUNET_NAMESTORE_RF_PRIVATE |
391 GNUNET_NAMESTORE_RF_PENDING)))
393 rd_public[rd_public_count] = rd[i];
394 if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
396 rd_public[rd_public_count].flags &= ~GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
397 rd_public[rd_public_count].expiration_time += now.abs_value_us;
402 /* We got a set of records to publish */
403 if (0 == rd_public_count)
405 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
409 expire = GNUNET_NAMESTORE_record_get_expiration_time (rd_public_count,
411 block = GNUNET_NAMESTORE_block_create (key,
416 block_size = ntohl (block->purpose.size)
417 + sizeof (struct GNUNET_CRYPTO_EccSignature)
418 + sizeof (struct GNUNET_CRYPTO_EccPublicSignKey);
419 GNUNET_NAMESTORE_query_from_private_key (key,
423 active_put = GNUNET_DHT_put (dht_handle, &query,
424 DHT_GNS_REPLICATION_LEVEL,
425 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
426 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
430 DHT_OPERATION_TIMEOUT,
431 &dht_put_continuation,
433 if (NULL == active_put)
436 dht_put_continuation (NULL, GNUNET_NO);
443 * Periodically iterate over our zone and store everything in dht
446 * @param tc task context
449 publish_zone_dht_start (void *cls,
450 const struct GNUNET_SCHEDULER_TaskContext *tc)
452 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "Scheduling DHT zone update!\n");
456 /* start counting again */
457 num_public_records = 0;
458 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
459 NULL, /* All zones */
465 /* END DHT ZONE PROPAGATION */
469 * Reply to client with the result from our lookup.
471 * @param cls the closure (our client lookup handle)
472 * @param rd_count the number of records in @a rd
473 * @param rd the record data
476 send_lookup_response (void* cls,
478 const struct GNUNET_NAMESTORE_RecordData *rd)
480 struct ClientLookupHandle *clh = cls;
481 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "Sending `%s' message with %d results\n",
489 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
490 rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage));
491 rmsg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
492 rmsg->header.size = htons (len + sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
493 rmsg->id = clh->request_id;
494 rmsg->rd_count = htonl (rd_count);
495 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len,
497 GNUNET_SERVER_notification_context_unicast (nc,
502 GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
503 GNUNET_SERVER_client_set_user_context (clh->client, NULL);
505 GNUNET_STATISTICS_update (statistics,
506 "Completed lookups", 1,
508 GNUNET_STATISTICS_update (statistics,
516 * Handle lookup requests from client
518 * @param cls the closure
519 * @param client the client
520 * @param message the message
523 handle_lookup (void *cls,
524 struct GNUNET_SERVER_Client *client,
525 const struct GNUNET_MessageHeader *message)
527 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
528 struct ClientLookupHandle *clh;
529 char *nameptr = name;
531 const struct GNUNET_CRYPTO_EccPrivateKey *key;
533 const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
536 "Received `%s' message\n",
538 msg_size = ntohs (message->size);
539 if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
542 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
545 sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
546 GNUNET_SERVER_notification_context_add (nc, client);
547 if (GNUNET_YES == ntohs (sh_msg->have_key))
548 key = &sh_msg->shorten_key;
551 utf_in = (const char *) &sh_msg[1];
552 if ( ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientLookupMessage) - 1]) ||
553 (strlen (utf_in) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) )
556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
559 GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
560 GNUNET_SERVER_receive_done (client, GNUNET_OK);
562 clh = GNUNET_new (struct ClientLookupHandle);
563 GNUNET_SERVER_client_set_user_context (client, clh);
564 GNUNET_CONTAINER_DLL_insert (clh_head, clh_tail, clh);
565 clh->client = client;
566 clh->request_id = sh_msg->id;
567 if ( (GNUNET_DNSPARSER_TYPE_A == ntohl (sh_msg->type)) &&
568 (GNUNET_OK != v4_enabled) )
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
571 "LOOKUP: Query for A record but AF_INET not supported!");
572 send_lookup_response (clh, 0, NULL);
575 if ( (GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
576 (GNUNET_OK != v6_enabled) )
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
579 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
580 send_lookup_response (clh, 0, NULL);
583 clh->lookup = GNS_resolver_lookup (&sh_msg->zone,
584 ntohl (sh_msg->type),
587 ntohl (sh_msg->only_cached),
588 &send_lookup_response, clh);
589 GNUNET_STATISTICS_update (statistics,
596 * One of our clients disconnected, clean up after it.
599 * @param client the client that disconnected
602 notify_client_disconnect (void *cls,
603 struct GNUNET_SERVER_Client *client)
605 struct ClientLookupHandle *clh;
609 clh = GNUNET_SERVER_client_get_user_context (client, struct ClientLookupHandle);
612 GNS_resolver_lookup_cancel (clh->lookup);
613 GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
619 * Process GNS requests.
622 * @param server the initialized server
623 * @param c configuration to use
626 run (void *cls, struct GNUNET_SERVER_Handle *server,
627 const struct GNUNET_CONFIGURATION_Handle *c)
629 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
630 { &handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
633 struct GNUNET_CRYPTO_EccPublicSignKey dns_root;
634 unsigned long long max_parallel_bg_queries = 0;
637 v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
638 v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
640 namestore_handle = GNUNET_NAMESTORE_connect (c);
641 if (NULL == namestore_handle)
643 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
644 _("Failed to connect to the namestore!\n"));
645 GNUNET_SCHEDULER_shutdown ();
649 put_interval = INITIAL_PUT_INTERVAL;
650 zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
653 GNUNET_CONFIGURATION_get_value_time (c, "gns",
654 "ZONE_PUBLISH_TIME_WINDOW",
655 &zone_publish_time_window))
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
658 "Time window for zone iteration: %s\n",
659 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window, GNUNET_YES));
662 GNUNET_CONFIGURATION_get_value_number (c, "gns",
663 "MAX_PARALLEL_BACKGROUND_QUERIES",
664 &max_parallel_bg_queries))
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "Number of allowed parallel background queries: %llu\n",
668 max_parallel_bg_queries);
671 dht_handle = GNUNET_DHT_connect (c,
672 (unsigned int) max_parallel_bg_queries);
673 if (NULL == dht_handle)
675 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
676 _("Could not connect to DHT!\n"));
677 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
682 GNUNET_CONFIGURATION_get_value_string (c, "gns", "DNS_ROOT",
686 GNUNET_CRYPTO_ecc_public_sign_key_from_string (dns_root_name,
687 strlen (dns_root_name),
690 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
692 _("valid public key required"));
693 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
694 GNUNET_free (dns_root_name);
697 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
698 "DNS hijacking with root `%s' enabled. Connecting to DNS service.\n",
700 GNUNET_free (dns_root_name);
702 GNS_interceptor_init (&dns_root, c))
704 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
708 GNS_resolver_init (namestore_handle, dht_handle,
710 max_parallel_bg_queries);
711 GNS_shorten_init (namestore_handle, dht_handle);
712 GNUNET_SERVER_disconnect_notify (server,
713 ¬ify_client_disconnect,
715 /* Schedule periodic put for our records. */
716 first_zone_iteration = GNUNET_YES;
717 GNUNET_SERVER_add_handlers (server, handlers);
718 statistics = GNUNET_STATISTICS_create ("gns", c);
719 nc = GNUNET_SERVER_notification_context_create (server, 1);
720 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
722 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
723 &shutdown_task, NULL);
728 * The main function for the GNS service.
730 * @param argc number of arguments from the command line
731 * @param argv command line arguments
732 * @return 0 ok, 1 on error
735 main (int argc, char *const *argv)
741 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
746 /* end of gnunet-service-gns.c */