2 This file is part of GNUnet.
3 Copyright (C) 2011-2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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_namecache_service.h"
32 #include "gnunet_namestore_service.h"
33 #include "gnunet_identity_service.h"
34 #include "gnunet_gns_service.h"
35 #include "gnunet_statistics_service.h"
37 #include "gnunet-service-gns_resolver.h"
38 #include "gnunet-service-gns_reverser.h"
39 #include "gnunet-service-gns_shorten.h"
40 #include "gnunet-service-gns_interceptor.h"
41 #include "gnunet_protocols.h"
44 * The initial interval in milliseconds btween puts in
47 #define INITIAL_PUT_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
50 * The lower bound for the zone iteration interval
52 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
55 * The upper bound for the zone iteration interval
57 #define MAXIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
60 * The default put interval for the zone iteration. In case
63 #define DEFAULT_ZONE_PUBLISH_TIME_WINDOW GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
66 * The factor the current zone iteration interval is divided by for each
67 * additional new record
69 #define LATE_ITERATION_SPEEDUP_FACTOR 2
72 * How long until a DHT PUT attempt should time out?
74 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
77 * What replication level do we use for DHT PUT operations?
79 #define DHT_GNS_REPLICATION_LEVEL 5
87 * Handle to a lookup operation from api
89 struct ClientLookupHandle
93 * We keep these in a DLL.
95 struct ClientLookupHandle *next;
98 * We keep these in a DLL.
100 struct ClientLookupHandle *prev;
105 struct GnsClient *gc;
108 * Active handle for the lookup.
110 struct GNS_ResolverHandle *lookup;
113 * Active handle for a reverse lookup
115 struct GNS_ReverserHandle *rev_lookup;
129 struct GNUNET_SERVICE_Client *client;
134 struct GNUNET_MQ_Handle *mq;
139 struct ClientLookupHandle *clh_head;
144 struct ClientLookupHandle *clh_tail;
149 * Handle for DHT PUT activity triggered from the namestore monitor.
151 struct MonitorActivity
156 struct MonitorActivity *next;
161 struct MonitorActivity *prev;
164 * Handle for the DHT PUT operation.
166 struct GNUNET_DHT_PutHandle *ph;
171 * Our handle to the DHT
173 static struct GNUNET_DHT_Handle *dht_handle;
176 * Active DHT put operation (or NULL)
178 static struct GNUNET_DHT_PutHandle *active_put;
181 * Our handle to the namestore service
183 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
186 * Our handle to the namecache service
188 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
191 * Our handle to the identity service
193 static struct GNUNET_IDENTITY_Handle *identity_handle;
196 * Our handle to the identity operation to find the master zone
197 * for intercepted queries.
199 static struct GNUNET_IDENTITY_Operation *identity_op;
202 * Handle to iterate over our authoritative zone in namestore
204 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
207 * Handle to monitor namestore changes to instant propagation.
209 static struct GNUNET_NAMESTORE_ZoneMonitor *zmon;
212 * Head of monitor activities; kept in a DLL.
214 static struct MonitorActivity *ma_head;
217 * Tail of monitor activities; kept in a DLL.
219 static struct MonitorActivity *ma_tail;
222 * Useful for zone update for DHT put
224 static unsigned long long num_public_records;
227 * Last seen record count
229 static unsigned long long last_num_public_records;
232 * Minimum relative expiration time of records seem during the current
235 static struct GNUNET_TIME_Relative min_relative_record_time;
238 * Zone iteration PUT interval.
240 static struct GNUNET_TIME_Relative put_interval;
243 * Default time window for zone iteration
245 static struct GNUNET_TIME_Relative zone_publish_time_window_default;
248 * Time window for zone iteration, adjusted based on relative record
249 * expiration times in our zone.
251 static struct GNUNET_TIME_Relative zone_publish_time_window;
256 static struct GNUNET_SCHEDULER_Task *zone_publish_task;
259 * #GNUNET_YES if zone has never been published before
261 static int first_zone_iteration;
264 * #GNUNET_YES if ipv6 is supported
266 static int v6_enabled;
269 * #GNUNET_YES if ipv4 is supported
271 static int v4_enabled;
274 * Handle to the statistics service
276 static struct GNUNET_STATISTICS_Handle *statistics;
280 * Task run during shutdown.
286 shutdown_task (void *cls)
288 struct MonitorActivity *ma;
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 GNS_interceptor_done ();
293 if (NULL != identity_op)
295 GNUNET_IDENTITY_cancel (identity_op);
298 if (NULL != identity_handle)
300 GNUNET_IDENTITY_disconnect (identity_handle);
301 identity_handle = NULL;
303 GNS_resolver_done ();
305 while (NULL != (ma = ma_head))
307 GNUNET_DHT_put_cancel (ma->ph);
308 GNUNET_CONTAINER_DLL_remove (ma_head,
313 if (NULL != statistics)
315 GNUNET_STATISTICS_destroy (statistics,
319 if (NULL != zone_publish_task)
321 GNUNET_SCHEDULER_cancel (zone_publish_task);
322 zone_publish_task = NULL;
324 if (NULL != namestore_iter)
326 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
327 namestore_iter = NULL;
331 GNUNET_NAMESTORE_zone_monitor_stop (zmon);
334 if (NULL != namestore_handle)
336 GNUNET_NAMESTORE_disconnect (namestore_handle);
337 namestore_handle = NULL;
339 if (NULL != namecache_handle)
341 GNUNET_NAMECACHE_disconnect (namecache_handle);
342 namecache_handle = NULL;
344 if (NULL != active_put)
346 GNUNET_DHT_put_cancel (active_put);
349 if (NULL != dht_handle)
351 GNUNET_DHT_disconnect (dht_handle);
357 * Called whenever a client is disconnected.
360 * @param client identification of the client
361 * @param app_ctx @a client
364 client_disconnect_cb (void *cls,
365 struct GNUNET_SERVICE_Client *client,
368 struct ClientLookupHandle *clh;
369 struct GnsClient *gc = app_ctx;
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "Client %p disconnected\n",
374 while (NULL != (clh = gc->clh_head))
376 if (NULL != clh->lookup)
377 GNS_resolver_lookup_cancel (clh->lookup);
378 if (NULL != clh->rev_lookup)
379 GNS_reverse_lookup_cancel (clh->rev_lookup);
380 GNUNET_CONTAINER_DLL_remove (gc->clh_head,
391 * Add a client to our list of active clients.
394 * @param client client to add
395 * @param mq message queue for @a client
396 * @return internal namestore client structure for this client
399 client_connect_cb (void *cls,
400 struct GNUNET_SERVICE_Client *client,
401 struct GNUNET_MQ_Handle *mq)
403 struct GnsClient *gc;
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 "Client %p connected\n",
407 gc = GNUNET_new (struct GnsClient);
415 * Method called periodically that triggers iteration over authoritative records
420 publish_zone_dht_next (void *cls)
422 zone_publish_task = NULL;
423 GNUNET_assert (NULL != namestore_iter);
424 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
429 * Periodically iterate over our zone and store everything in dht
434 publish_zone_dht_start (void *cls);
438 * Continuation called from DHT once the PUT operation is done.
440 * @param cls closure, NULL if called from regular iteration,
441 * `struct MonitorActivity` if called from #handle_monitor_event.
442 * @param success #GNUNET_OK on success
445 dht_put_continuation (void *cls,
448 struct MonitorActivity *ma = cls;
449 struct GNUNET_TIME_Relative next_put_interval;
451 num_public_records++;
455 if ( (num_public_records > last_num_public_records) &&
456 (GNUNET_NO == first_zone_iteration) )
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "Last record count was lower than current record count. Reducing interval.\n");
460 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
462 next_put_interval = GNUNET_TIME_relative_divide (put_interval,
463 LATE_ITERATION_SPEEDUP_FACTOR);
466 next_put_interval = put_interval;
467 next_put_interval = GNUNET_TIME_relative_min (next_put_interval,
468 MAXIMUM_ZONE_ITERATION_INTERVAL);
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470 "PUT complete, next PUT in %s!\n",
471 GNUNET_STRINGS_relative_time_to_string (next_put_interval,
474 GNUNET_STATISTICS_set (statistics,
475 "Current zone iteration interval (ms)",
476 next_put_interval.rel_value_us / 1000LL,
478 GNUNET_assert (NULL == zone_publish_task);
479 zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
480 &publish_zone_dht_next,
485 GNUNET_CONTAINER_DLL_remove (ma_head,
494 * Convert namestore records from the internal format to that
495 * suitable for publication (removes private records, converts
496 * to absolute expiration time).
498 * @param rd input records
499 * @param rd_count size of the @a rd and @a rd_public arrays
500 * @param rd_public where to write the converted records
501 * @return number of records written to @a rd_public
504 convert_records_for_export (const struct GNUNET_GNSRECORD_Data *rd,
505 unsigned int rd_count,
506 struct GNUNET_GNSRECORD_Data *rd_public)
508 struct GNUNET_TIME_Absolute now;
509 unsigned int rd_public_count;
513 now = GNUNET_TIME_absolute_get ();
514 for (i=0;i<rd_count;i++)
515 if (0 == (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE))
517 rd_public[rd_public_count] = rd[i];
518 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
520 /* GNUNET_GNSRECORD_block_create will convert to absolute time;
521 we just need to adjust our iteration frequency */
522 min_relative_record_time.rel_value_us =
523 GNUNET_MIN (rd_public[rd_public_count].expiration_time,
524 min_relative_record_time.rel_value_us);
526 else if (rd_public[rd_public_count].expiration_time < now.abs_value_us)
528 /* record already expired, skip it */
533 return rd_public_count;
538 * Store GNS records in the DHT.
540 * @param key key of the zone
541 * @param label label to store under
542 * @param rd_public public record data
543 * @param rd_public_count number of records in @a rd_public
544 * @param pc_arg closure argument to pass to the #dht_put_continuation
545 * @return DHT PUT handle, NULL on error
547 static struct GNUNET_DHT_PutHandle *
548 perform_dht_put (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
550 const struct GNUNET_GNSRECORD_Data *rd_public,
551 unsigned int rd_public_count,
554 struct GNUNET_GNSRECORD_Block *block;
555 struct GNUNET_HashCode query;
556 struct GNUNET_TIME_Absolute expire;
558 struct GNUNET_DHT_PutHandle *ret;
560 expire = GNUNET_GNSRECORD_record_get_expiration_time (rd_public_count,
562 block = GNUNET_GNSRECORD_block_create (key,
568 return NULL; /* whoops */
569 block_size = ntohl (block->purpose.size)
570 + sizeof (struct GNUNET_CRYPTO_EcdsaSignature)
571 + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
572 GNUNET_GNSRECORD_query_from_private_key (key,
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576 "Storing %u record(s) for label `%s' in DHT with expiration `%s' under key %s\n",
579 GNUNET_STRINGS_absolute_time_to_string (expire),
580 GNUNET_h2s (&query));
581 ret = GNUNET_DHT_put (dht_handle,
583 DHT_GNS_REPLICATION_LEVEL,
584 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
585 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
589 &dht_put_continuation,
597 * We encountered an error in our zone iteration.
600 zone_iteration_error (void *cls)
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
603 "Got disconnected from namestore database, retrying.\n");
604 namestore_iter = NULL;
605 /* We end up here on error/disconnect/shutdown, so potentially
606 while a zone publish task or a DHT put is still running; hence
607 we need to cancel those. */
608 if (NULL != zone_publish_task)
610 GNUNET_SCHEDULER_cancel (zone_publish_task);
611 zone_publish_task = NULL;
613 if (NULL != active_put)
615 GNUNET_DHT_put_cancel (active_put);
618 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
624 * Zone iteration is completed.
627 zone_iteration_finished (void *cls)
629 /* we're done with one iteration, calculate when to do the next one */
630 namestore_iter = NULL;
631 last_num_public_records = num_public_records;
632 first_zone_iteration = GNUNET_NO;
633 if (0 == num_public_records)
636 * If no records are known (startup) or none present
637 * we can safely set the interval to the value for a single
640 put_interval = zone_publish_time_window;
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
642 "No records in namestore database.\n");
646 /* If records are present, next publication is based on the minimum
647 * relative expiration time of the records published divided by 4
649 zone_publish_time_window
650 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_divide (min_relative_record_time, 4),
651 zone_publish_time_window_default);
652 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
655 /* reset for next iteration */
656 min_relative_record_time = GNUNET_TIME_UNIT_FOREVER_REL;
657 put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
659 put_interval = GNUNET_TIME_relative_min (put_interval,
660 MAXIMUM_ZONE_ITERATION_INTERVAL);
661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
662 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
663 GNUNET_STRINGS_relative_time_to_string (put_interval,
665 GNUNET_STATISTICS_set (statistics,
666 "Current zone iteration interval (in ms)",
667 put_interval.rel_value_us / 1000LL,
669 GNUNET_STATISTICS_update (statistics,
670 "Number of zone iterations",
673 GNUNET_STATISTICS_set (statistics,
674 "Number of public records in DHT",
675 last_num_public_records,
677 GNUNET_assert (NULL == zone_publish_task);
678 if (0 == num_public_records)
679 zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
680 &publish_zone_dht_start,
683 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
689 * Function used to put all records successively into the DHT.
691 * @param cls the closure (NULL)
692 * @param key the private key of the authority (ours)
693 * @param label the name of the records, NULL once the iteration is done
694 * @param rd_count the number of records in @a rd
695 * @param rd the record data
698 put_gns_record (void *cls,
699 const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
701 unsigned int rd_count,
702 const struct GNUNET_GNSRECORD_Data *rd)
704 struct GNUNET_GNSRECORD_Data rd_public[rd_count];
705 unsigned int rd_public_count;
707 rd_public_count = convert_records_for_export (rd,
711 if (0 == rd_public_count)
713 GNUNET_assert (NULL == zone_publish_task);
714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
715 "Record set empty, moving to next record set\n");
716 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
720 /* We got a set of records to publish */
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
722 "Starting DHT PUT\n");
723 active_put = perform_dht_put (key,
728 if (NULL == active_put)
731 dht_put_continuation (NULL, GNUNET_NO);
737 * Periodically iterate over all zones and store everything in DHT
742 publish_zone_dht_start (void *cls)
744 zone_publish_task = NULL;
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
747 "Starting DHT zone update!\n");
748 /* start counting again */
749 num_public_records = 0;
750 GNUNET_assert (NULL == namestore_iter);
752 = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
753 NULL, /* All zones */
754 &zone_iteration_error,
758 &zone_iteration_finished,
764 * Process a record that was stored in the namestore
765 * (invoked by the monitor).
767 * @param cls closure, NULL
768 * @param zone private key of the zone; NULL on disconnect
769 * @param label label of the records; NULL on disconnect
770 * @param rd_count number of entries in @a rd array, 0 if label was deleted
771 * @param rd array of records with data to store
774 handle_monitor_event (void *cls,
775 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
777 unsigned int rd_count,
778 const struct GNUNET_GNSRECORD_Data *rd)
780 struct GNUNET_GNSRECORD_Data rd_public[rd_count];
781 unsigned int rd_public_count;
782 struct MonitorActivity *ma;
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785 "Received %u records for label `%s' via namestore monitor\n",
788 /* filter out records that are not public, and convert to
789 absolute expiration time. */
790 rd_public_count = convert_records_for_export (rd,
793 if (0 == rd_public_count)
794 return; /* nothing to do */
795 ma = GNUNET_new (struct MonitorActivity);
796 ma->ph = perform_dht_put (zone,
803 /* PUT failed, do not remember operation */
807 GNUNET_CONTAINER_DLL_insert (ma_head,
813 /* END DHT ZONE PROPAGATION */
817 * Reply to client with the result from our lookup.
819 * @param cls the closure (our client lookup handle)
820 * @param rd_count the number of records in @a rd
821 * @param rd the record data
824 send_lookup_response (void* cls,
826 const struct GNUNET_GNSRECORD_Data *rd)
828 struct ClientLookupHandle *clh = cls;
829 struct GNUNET_MQ_Envelope *env;
830 struct LookupResultMessage *rmsg;
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
834 "Sending LOOKUP_RESULT message with %u results\n",
835 (unsigned int) rd_count);
837 len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
838 env = GNUNET_MQ_msg_extra (rmsg,
840 GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
841 rmsg->id = clh->request_id;
842 rmsg->rd_count = htonl (rd_count);
843 GNUNET_GNSRECORD_records_serialize (rd_count, rd, len,
845 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq(clh->gc->client),
847 GNUNET_CONTAINER_DLL_remove (clh->gc->clh_head, clh->gc->clh_tail, clh);
849 GNUNET_STATISTICS_update (statistics,
850 "Completed lookups", 1,
852 GNUNET_STATISTICS_update (statistics,
859 * Reply to client with the result from our reverse lookup.
861 * @param cls the closure (our client lookup handle)
862 * @param rd_count the number of records in @a rd
863 * @param rd the record data
866 send_reverse_lookup_response (void* cls,
869 struct ClientLookupHandle *clh = cls;
870 struct GNUNET_MQ_Envelope *env;
871 struct ReverseLookupResultMessage *rmsg;
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
875 "Sending LOOKUP_RESULT message with %s\n",
881 len = strlen (name) + 1;
882 env = GNUNET_MQ_msg_extra (rmsg,
884 GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP_RESULT);
885 rmsg->id = clh->request_id;
887 GNUNET_memcpy ((char*) &rmsg[1],
890 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq(clh->gc->client),
892 GNUNET_CONTAINER_DLL_remove (clh->gc->clh_head, clh->gc->clh_tail, clh);
894 GNUNET_STATISTICS_update (statistics,
895 "Completed reverse lookups", 1,
901 * Checks a #GNUNET_MESSAGE_TYPE_GNS_LOOKUP message
903 * @param cls client sending the message
904 * @param l_msg message of type `struct LookupMessage`
905 * @return #GNUNET_OK if @a l_msg is well-formed
908 check_lookup (void *cls,
909 const struct LookupMessage *l_msg)
914 msg_size = ntohs (l_msg->header.size);
915 if (msg_size < sizeof (struct LookupMessage))
918 return GNUNET_SYSERR;
920 name = (const char *) &l_msg[1];
921 if ( ('\0' != name[msg_size - sizeof (struct LookupMessage) - 1]) ||
922 (strlen (name) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) )
925 return GNUNET_SYSERR;
931 * Handle lookup requests from client
933 * @param cls the closure
934 * @param client the client
935 * @param message the message
938 handle_lookup (void *cls,
939 const struct LookupMessage *sh_msg)
941 struct GnsClient *gc = cls;
942 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
943 struct ClientLookupHandle *clh;
944 char *nameptr = name;
946 const struct GNUNET_CRYPTO_EcdsaPrivateKey *key;
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
949 "Received LOOKUP message\n");
950 GNUNET_SERVICE_client_continue (gc->client);
951 if (GNUNET_YES == ntohs (sh_msg->have_key))
952 key = &sh_msg->shorten_key;
955 utf_in = (const char *) &sh_msg[1];
956 GNUNET_STRINGS_utf8_tolower (utf_in, nameptr);
958 clh = GNUNET_new (struct ClientLookupHandle);
959 GNUNET_CONTAINER_DLL_insert (gc->clh_head, gc->clh_tail, clh);
961 clh->request_id = sh_msg->id;
962 if ( (GNUNET_DNSPARSER_TYPE_A == ntohl (sh_msg->type)) &&
963 (GNUNET_OK != v4_enabled) )
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966 "LOOKUP: Query for A record but AF_INET not supported!");
967 send_lookup_response (clh, 0, NULL);
970 if ( (GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
971 (GNUNET_OK != v6_enabled) )
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
975 send_lookup_response (clh, 0, NULL);
978 clh->lookup = GNS_resolver_lookup (&sh_msg->zone,
979 ntohl (sh_msg->type),
982 (enum GNUNET_GNS_LocalOptions) ntohs (sh_msg->options),
983 &send_lookup_response, clh);
984 GNUNET_STATISTICS_update (statistics,
990 * Handle reverse lookup requests from client
992 * @param cls the closure
993 * @param client the client
994 * @param message the message
997 handle_rev_lookup (void *cls,
998 const struct ReverseLookupMessage *sh_msg)
1000 struct GnsClient *gc = cls;
1001 struct ClientLookupHandle *clh;
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1004 "Received REVERSE_LOOKUP message\n");
1005 GNUNET_SERVICE_client_continue (gc->client);
1007 clh = GNUNET_new (struct ClientLookupHandle);
1008 GNUNET_CONTAINER_DLL_insert (gc->clh_head, gc->clh_tail, clh);
1010 clh->request_id = sh_msg->id;
1011 clh->rev_lookup = GNS_reverse_lookup (&sh_msg->zone_pkey,
1013 &send_reverse_lookup_response,
1015 GNUNET_STATISTICS_update (statistics,
1016 "Reverse lookup attempts",
1022 * The zone monitor is now in SYNC with the current state of the
1023 * name store. Start to perform periodic iterations.
1028 monitor_sync_event (void *cls)
1030 GNUNET_assert (NULL == zone_publish_task);
1031 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
1037 * The zone monitor is now in SYNC with the current state of the
1038 * name store. Start to perform periodic iterations.
1043 handle_monitor_error (void *cls)
1045 if (NULL != zone_publish_task)
1047 GNUNET_SCHEDULER_cancel (zone_publish_task);
1048 zone_publish_task = NULL;
1050 if (NULL != namestore_iter)
1052 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1053 namestore_iter = NULL;
1055 if (NULL != active_put)
1057 GNUNET_DHT_put_cancel (active_put);
1060 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
1066 * Method called to inform about the ego to be used for the master zone
1067 * for DNS interceptions.
1069 * This function is only called ONCE, and 'NULL' being passed in
1070 * @a ego does indicate that interception is not configured.
1071 * If @a ego is non-NULL, we should start to intercept DNS queries
1072 * and resolve ".gnu" queries using the given ego as the master zone.
1074 * @param cls closure, our `const struct GNUNET_CONFIGURATION_Handle *c`
1075 * @param ego ego handle
1076 * @param ctx context for application to store data for this ego
1077 * (during the lifetime of this process, initially NULL)
1078 * @param name name assigned by the user for this ego,
1079 * NULL if the user just deleted the ego and it
1080 * must thus no longer be used
1083 identity_intercept_cb (void *cls,
1084 struct GNUNET_IDENTITY_Ego *ego,
1088 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1089 struct GNUNET_CRYPTO_EcdsaPublicKey dns_root;
1094 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1095 _("No ego configured for `%s`\n"),
1099 GNUNET_IDENTITY_ego_get_public_key (ego,
1101 if (GNUNET_SYSERR ==
1102 GNS_interceptor_init (&dns_root, cfg))
1105 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1112 * Process GNS requests.
1114 * @param cls closure
1115 * @param server the initialized server
1116 * @param c configuration to use
1120 const struct GNUNET_CONFIGURATION_Handle *c,
1121 struct GNUNET_SERVICE_Handle *service)
1123 unsigned long long max_parallel_bg_queries = 0;
1125 v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
1126 v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
1127 min_relative_record_time = GNUNET_TIME_UNIT_FOREVER_REL;
1128 namestore_handle = GNUNET_NAMESTORE_connect (c);
1129 if (NULL == namestore_handle)
1131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1132 _("Failed to connect to the namestore!\n"));
1133 GNUNET_SCHEDULER_shutdown ();
1136 namecache_handle = GNUNET_NAMECACHE_connect (c);
1137 if (NULL == namecache_handle)
1139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1140 _("Failed to connect to the namecache!\n"));
1141 GNUNET_SCHEDULER_shutdown ();
1145 put_interval = INITIAL_PUT_INTERVAL;
1146 zone_publish_time_window_default = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
1148 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1149 "ZONE_PUBLISH_TIME_WINDOW",
1150 &zone_publish_time_window_default))
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "Time window for zone iteration: %s\n",
1154 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window,
1157 zone_publish_time_window = zone_publish_time_window_default;
1159 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1160 "MAX_PARALLEL_BACKGROUND_QUERIES",
1161 &max_parallel_bg_queries))
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 "Number of allowed parallel background queries: %llu\n",
1165 max_parallel_bg_queries);
1168 dht_handle = GNUNET_DHT_connect (c,
1169 (unsigned int) max_parallel_bg_queries);
1170 if (NULL == dht_handle)
1172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1173 _("Could not connect to DHT!\n"));
1174 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1178 identity_handle = GNUNET_IDENTITY_connect (c,
1181 if (NULL == identity_handle)
1183 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1184 "Could not connect to identity service!\n");
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "Looking for gns-intercept ego\n");
1190 identity_op = GNUNET_IDENTITY_get (identity_handle,
1192 &identity_intercept_cb,
1195 GNS_resolver_init (namecache_handle,
1198 max_parallel_bg_queries);
1199 GNS_shorten_init (namestore_handle,
1202 /* Schedule periodic put for our records. */
1203 first_zone_iteration = GNUNET_YES;
1204 statistics = GNUNET_STATISTICS_create ("gns", c);
1205 zmon = GNUNET_NAMESTORE_zone_monitor_start (c,
1208 &handle_monitor_error,
1210 &handle_monitor_event,
1212 &monitor_sync_event,
1214 GNUNET_break (NULL != zmon);
1215 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
1220 * Define "main" method using service macro.
1224 GNUNET_SERVICE_OPTION_NONE,
1227 &client_disconnect_cb,
1229 GNUNET_MQ_hd_var_size (lookup,
1230 GNUNET_MESSAGE_TYPE_GNS_LOOKUP,
1231 struct LookupMessage,
1233 GNUNET_MQ_hd_fixed_size (rev_lookup,
1234 GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP,
1235 struct ReverseLookupMessage,
1237 GNUNET_MQ_handler_end());
1240 /* end of gnunet-service-gns.c */