2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2014, 2018 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.
22 * @file namestore/gnunet-service-namestore.c
23 * @brief namestore for the GNUnet naming system
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 * - run testcases, make sure everything works!
31 #include "gnunet_util_lib.h"
32 #include "gnunet_dnsparser_lib.h"
33 #include "gnunet_gns_service.h"
34 #include "gnunet_namecache_service.h"
35 #include "gnunet_namestore_service.h"
36 #include "gnunet_namestore_plugin.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_signatures.h"
39 #include "namestore.h"
41 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
44 * If a monitor takes more than 1 minute to process an event, print a warning.
46 #define MONITOR_STALL_WARN_DELAY GNUNET_TIME_UNIT_MINUTES
52 struct NamestoreClient;
56 * A namestore iteration operation.
61 * Next element in the DLL
63 struct ZoneIteration *next;
66 * Previous element in the DLL
68 struct ZoneIteration *prev;
71 * Namestore client which intiated this zone iteration
73 struct NamestoreClient *nc;
76 * The nick to add to the records
78 struct GNUNET_GNSRECORD_Data *nick;
81 * Key of the zone we are iterating over.
83 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
86 * Last sequence number in the zone iteration used to address next
87 * result of the zone iteration in the store
90 * Updated in #zone_iterate_proc()
95 * The operation id fot the zone iteration in the response for the client
100 * Offset of the zone iteration used to address next result of the zone
101 * iteration in the store
103 * Initialy set to 0 in #handle_iteration_start
104 * Incremented with by every call to #handle_iteration_next
114 struct NamestoreClient
120 struct GNUNET_SERVICE_Client *client;
123 * Message queue for transmission to @e client
125 struct GNUNET_MQ_Handle *mq;
129 * Zone iteration operations in progress initiated by this client
131 struct ZoneIteration *op_head;
135 * Zone iteration operations in progress initiated by this client
137 struct ZoneIteration *op_tail;
142 * A namestore monitor.
147 * Next element in the DLL
149 struct ZoneMonitor *next;
152 * Previous element in the DLL
154 struct ZoneMonitor *prev;
157 * Namestore client which intiated this zone monitor
159 struct NamestoreClient *nc;
162 * Private key of the zone.
164 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
167 * Task active during initial iteration.
169 struct GNUNET_SCHEDULER_Task *task;
172 * Task to warn about slow monitors.
174 struct GNUNET_SCHEDULER_Task *sa_wait_warning;
177 * Since when are we blocked on this monitor?
179 struct GNUNET_TIME_Absolute sa_waiting_start;
182 * Last sequence number in the zone iteration used to address next
183 * result of the zone iteration in the store
186 * Updated in #monitor_iterate_cb()
191 * Current limit of how many more messages we are allowed
192 * to queue to this monitor.
197 * How many more requests may we receive from the iterator
198 * before it is at the limit we gave it? Will be below or
199 * equal to @e limit. The effective limit for monitor
200 * events is thus @e iteration_cnt - @e limit!
202 uint64_t iteration_cnt;
205 * Are we (still) in the initial iteration pass?
207 int in_first_iteration;
210 * Is there a store activity waiting for this monitor? We only raise the
211 * flag when it happens and search the DLL for the store activity when we
212 * had a limit increase. If we cannot find any waiting store activity at
213 * that time, we clear the flag again.
221 * Pending operation on the namecache.
223 struct CacheOperation
229 struct CacheOperation *prev;
234 struct CacheOperation *next;
237 * Handle to namecache queue.
239 struct GNUNET_NAMECACHE_QueueEntry *qe;
242 * Client to notify about the result.
244 struct NamestoreClient *nc;
247 * Client's request ID.
254 * Information for an ongoing #handle_record_store() operation.
255 * Needed as we may wait for monitors to be ready for the notification.
262 struct StoreActivity *next;
267 struct StoreActivity *prev;
270 * Which client triggered the store activity?
272 struct NamestoreClient *nc;
275 * Copy of the original store message (as data fields in @e rd will
278 const struct RecordStoreMessage *rsm;
281 * Array of record data to store (without NICK unless this is about
282 * #GNUNET_GNS_EMPTY_LABEL_AT). Length is in @e rd_count.
284 struct GNUNET_GNSRECORD_Data *rd;
287 * Next zone monitor that still needs to be notified about this PUT.
289 struct ZoneMonitor *zm_pos;
292 * Label nicely canonicalized (lower case).
297 * How many records do we try to store?
299 unsigned int rd_count;
305 * Public key of all zeros.
307 static const struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
310 * Configuration handle.
312 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
315 * Handle to the statistics service
317 static struct GNUNET_STATISTICS_Handle *statistics;
322 static struct GNUNET_NAMECACHE_Handle *namecache;
327 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
330 * Name of the database plugin
332 static char *db_lib_name;
337 static struct CacheOperation *cop_head;
342 static struct CacheOperation *cop_tail;
345 * First active zone monitor.
347 static struct ZoneMonitor *monitor_head;
350 * Last active zone monitor.
352 static struct ZoneMonitor *monitor_tail;
355 * Head of DLL of monitor-blocked store activities.
357 static struct StoreActivity *sa_head;
360 * Tail of DLL of monitor-blocked store activities.
362 static struct StoreActivity *sa_tail;
365 * Notification context shared by all monitors.
367 static struct GNUNET_NotificationContext *monitor_nc;
370 * Optimize block insertion by caching map of private keys to
371 * public keys in memory?
373 static int cache_keys;
376 * Use the namecache? Doing so creates additional cryptographic
377 * operations whenever we touch a record.
379 static int disable_namecache;
383 * Task run during shutdown.
388 cleanup_task (void *cls)
390 struct CacheOperation *cop;
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Stopping namestore service\n");
395 while (NULL != (cop = cop_head))
397 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
398 "Aborting incomplete namecache operation\n");
399 GNUNET_NAMECACHE_cancel (cop->qe);
400 GNUNET_CONTAINER_DLL_remove (cop_head,
405 if (NULL != namecache)
407 GNUNET_NAMECACHE_disconnect (namecache);
410 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name,
412 GNUNET_free (db_lib_name);
414 if (NULL != monitor_nc)
416 GNUNET_notification_context_destroy (monitor_nc);
419 if (NULL != statistics)
421 GNUNET_STATISTICS_destroy (statistics,
429 * Release memory used by @a sa.
431 * @param sa activity to free
434 free_store_activity (struct StoreActivity *sa)
436 GNUNET_CONTAINER_DLL_remove (sa_head,
439 GNUNET_array_grow (sa->rd,
442 GNUNET_free (sa->conv_name);
448 * Function called with the records for the #GNUNET_GNS_EMPTY_LABEL_AT
449 * label in the zone. Used to locate the #GNUNET_GNSRECORD_TYPE_NICK
450 * record, which (if found) is then copied to @a cls for future use.
452 * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
453 * @param seq sequence number of the record
454 * @param private_key the private key of the zone (unused)
455 * @param label should be #GNUNET_GNS_EMPTY_LABEL_AT
456 * @param rd_count number of records in @a rd
457 * @param rd records stored under @a label in the zone
460 lookup_nick_it (void *cls,
462 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
464 unsigned int rd_count,
465 const struct GNUNET_GNSRECORD_Data *rd)
467 struct GNUNET_GNSRECORD_Data **res = cls;
471 if (0 != strcmp (label, GNUNET_GNS_EMPTY_LABEL_AT))
476 for (unsigned int c = 0; c < rd_count; c++)
478 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
480 (*res) = GNUNET_malloc (rd[c].data_size + sizeof (struct GNUNET_GNSRECORD_Data));
481 (*res)->data = &(*res)[1];
482 GNUNET_memcpy ((void *) (*res)->data,
485 (*res)->data_size = rd[c].data_size;
486 (*res)->expiration_time = rd[c].expiration_time;
487 (*res)->flags = rd[c].flags;
488 (*res)->record_type = GNUNET_GNSRECORD_TYPE_NICK;
497 * Return the NICK record for the zone (if it exists).
499 * @param zone private key for the zone to look for nick
500 * @return NULL if no NICK record was found
502 static struct GNUNET_GNSRECORD_Data *
503 get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
505 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
506 struct GNUNET_GNSRECORD_Data *nick;
510 res = GSN_database->lookup_records (GSN_database->cls,
512 GNUNET_GNS_EMPTY_LABEL_AT,
515 if ( (GNUNET_OK != res) ||
518 GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
520 "No nick name set for zone `%s'\n",
521 GNUNET_GNSRECORD_z2s (&pub));
529 * Merge the nick record @a nick_rd with the rest of the
530 * record set given in @a rd2. Store the result in @a rdc_res
531 * and @a rd_res. The @a nick_rd's expiration time is set to
532 * the maximum expiration time of all of the records in @a rd2.
534 * @param nick_rd the nick record to integrate
535 * @param rd2_length length of the @a rd2 array
536 * @param rd2 array of records
537 * @param rdc_res[out] length of the resulting @a rd_res array
538 * @param rd_res[out] set to an array of records,
539 * including @a nick_rd and @a rd2;
540 * all of the variable-size 'data' fields in @a rd2 are
541 * allocated in the same chunk of memory!
544 merge_with_nick_records (const struct GNUNET_GNSRECORD_Data *nick_rd,
545 unsigned int rd2_length,
546 const struct GNUNET_GNSRECORD_Data *rd2,
547 unsigned int *rdc_res,
548 struct GNUNET_GNSRECORD_Data **rd_res)
550 uint64_t latest_expiration;
556 (*rdc_res) = 1 + rd2_length;
557 if (0 == 1 + rd2_length)
563 for (unsigned int c=0; c< 1; c++)
564 req += sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd[c].data_size;
565 for (unsigned int c=0; c< rd2_length; c++)
566 req += sizeof (struct GNUNET_GNSRECORD_Data) + rd2[c].data_size;
567 (*rd_res) = GNUNET_malloc (req);
568 data = (char *) &(*rd_res)[1 + rd2_length];
570 latest_expiration = 0;
571 for (unsigned int c=0; c< rd2_length; c++)
573 if (0 != (rd2[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
575 if ((GNUNET_TIME_absolute_get().abs_value_us + rd2[c].expiration_time) >
577 latest_expiration = rd2[c].expiration_time;
579 else if (rd2[c].expiration_time > latest_expiration)
580 latest_expiration = rd2[c].expiration_time;
581 (*rd_res)[c] = rd2[c];
582 (*rd_res)[c].data = (void *) &data[data_offset];
583 GNUNET_memcpy ((void *) (*rd_res)[c].data,
586 data_offset += (*rd_res)[c].data_size;
589 record_offset = rd2_length;
590 (*rd_res)[record_offset] = *nick_rd;
591 (*rd_res)[record_offset].expiration_time = latest_expiration;
592 (*rd_res)[record_offset].data = (void *) &data[data_offset];
593 GNUNET_memcpy ((void *) (*rd_res)[record_offset].data,
596 data_offset += (*rd_res)[record_offset].data_size;
597 GNUNET_assert (req == (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
602 * Generate a `struct LookupNameResponseMessage` and send it to the
603 * given client using the given notification context.
605 * @param nc client to unicast to
606 * @param request_id request ID to use
607 * @param zone_key zone key of the zone
609 * @param rd_count number of records in @a rd
610 * @param rd array of records
613 send_lookup_response (struct NamestoreClient *nc,
615 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
617 unsigned int rd_count,
618 const struct GNUNET_GNSRECORD_Data *rd)
620 struct GNUNET_MQ_Envelope *env;
621 struct RecordResultMessage *zir_msg;
622 struct GNUNET_GNSRECORD_Data *nick;
623 struct GNUNET_GNSRECORD_Data *res;
624 unsigned int res_count;
630 nick = get_nick_record (zone_key);
631 if ( (NULL != nick) &&
633 GNUNET_GNS_EMPTY_LABEL_AT)))
635 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
636 merge_with_nick_records (nick,
645 res_count = rd_count;
646 res = (struct GNUNET_GNSRECORD_Data *) rd;
649 name_len = strlen (name) + 1;
650 rd_ser_len = GNUNET_GNSRECORD_records_get_size (res_count, res);
651 env = GNUNET_MQ_msg_extra (zir_msg,
652 name_len + rd_ser_len,
653 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
654 zir_msg->gns_header.r_id = htonl (request_id);
655 zir_msg->name_len = htons (name_len);
656 zir_msg->rd_count = htons (res_count);
657 zir_msg->rd_len = htons (rd_ser_len);
658 zir_msg->private_key = *zone_key;
659 name_tmp = (char *) &zir_msg[1];
660 GNUNET_memcpy (name_tmp,
663 rd_ser = &name_tmp[name_len];
664 GNUNET_GNSRECORD_records_serialize (res_count,
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669 "Sending RECORD_RESULT message with %u records\n",
671 GNUNET_STATISTICS_update (statistics,
672 "Record sets sent to clients",
675 GNUNET_MQ_send (nc->mq,
683 * Send response to the store request to the client.
685 * @param client client to talk to
686 * @param res status of the operation
687 * @param rid client's request ID
690 send_store_response (struct NamestoreClient *nc,
694 struct GNUNET_MQ_Envelope *env;
695 struct RecordStoreResponseMessage *rcr_msg;
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698 "Sending RECORD_STORE_RESPONSE message\n");
699 GNUNET_STATISTICS_update (statistics,
700 "Store requests completed",
703 env = GNUNET_MQ_msg (rcr_msg,
704 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
705 rcr_msg->gns_header.r_id = htonl (rid);
706 rcr_msg->op_result = htonl (res);
707 GNUNET_MQ_send (nc->mq,
713 * Cache operation complete, clean up.
715 * @param cls the `struct CacheOperation`
716 * @param success success
717 * @param emsg error messages
720 finish_cache_operation (void *cls,
724 struct CacheOperation *cop = cls;
727 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
728 _("Failed to replicate block in namecache: %s\n"),
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "CACHE operation completed\n");
733 GNUNET_CONTAINER_DLL_remove (cop_head,
737 send_store_response (cop->nc,
745 * We just touched the plaintext information about a name in our zone;
746 * refresh the corresponding (encrypted) block in the namecache.
748 * @param nc client responsible for the request, can be NULL
749 * @param rid request ID of the client
750 * @param zone_key private key of the zone
751 * @param name label for the records
752 * @param rd_count number of records
753 * @param rd records stored under the given @a name
756 refresh_block (struct NamestoreClient *nc,
758 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
760 unsigned int rd_count,
761 const struct GNUNET_GNSRECORD_Data *rd)
763 struct GNUNET_GNSRECORD_Block *block;
764 struct CacheOperation *cop;
765 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
766 struct GNUNET_GNSRECORD_Data *nick;
767 struct GNUNET_GNSRECORD_Data *res;
768 unsigned int res_count;
769 struct GNUNET_TIME_Absolute exp_time;
771 nick = get_nick_record (zone_key);
772 res_count = rd_count;
773 res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
776 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
777 merge_with_nick_records (nick,
785 send_store_response (nc,
788 return; /* no data, no need to update cache */
790 if (GNUNET_YES == disable_namecache)
792 GNUNET_STATISTICS_update (statistics,
793 "Namecache updates skipped (NC disabled)",
796 send_store_response (nc,
801 exp_time = GNUNET_GNSRECORD_record_get_expiration_time (res_count,
804 block = GNUNET_GNSRECORD_block_create2 (zone_key,
810 block = GNUNET_GNSRECORD_block_create (zone_key,
815 GNUNET_assert (NULL != block);
816 GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819 "Caching block for label `%s' with %u records and expiration %s in zone `%s' in namecache\n",
822 GNUNET_STRINGS_absolute_time_to_string (exp_time),
823 GNUNET_GNSRECORD_z2s (&pkey));
824 GNUNET_STATISTICS_update (statistics,
825 "Namecache updates pushed",
828 cop = GNUNET_new (struct CacheOperation);
831 GNUNET_CONTAINER_DLL_insert (cop_head,
834 cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
836 &finish_cache_operation,
843 * Print a warning that one of our monitors is no longer reacting.
845 * @param cls a `struct ZoneMonitor` to warn about
848 warn_monitor_slow (void *cls)
850 struct ZoneMonitor *zm = cls;
852 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
853 "No response from monitor since %s\n",
854 GNUNET_STRINGS_absolute_time_to_string (zm->sa_waiting_start));
855 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
862 * Continue processing the @a sa.
864 * @param sa store activity to process
867 continue_store_activity (struct StoreActivity *sa)
869 const struct RecordStoreMessage *rp_msg = sa->rsm;
871 for (struct ZoneMonitor *zm = sa->zm_pos;
875 if ( (0 != memcmp (&rp_msg->private_key,
877 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) &&
878 (0 != memcmp (&zm->zone,
880 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
881 sa->zm_pos = zm->next; /* not interesting to this monitor */
882 if (zm->limit == zm->iteration_cnt)
884 zm->sa_waiting = GNUNET_YES;
885 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
886 if (NULL != zm->sa_wait_warning)
887 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
888 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
891 return; /* blocked on zone monitor */
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 "Notifying monitor about changes under label `%s'\n",
897 send_lookup_response (zm->nc,
899 &rp_msg->private_key,
903 sa->zm_pos = zm->next;
905 /* great, done with the monitors, unpack (again) for refresh_block operation */
910 const char *name_tmp;
912 unsigned int rd_count;
914 rid = ntohl (rp_msg->gns_header.r_id);
915 name_len = ntohs (rp_msg->name_len);
916 rd_count = ntohs (rp_msg->rd_count);
917 rd_ser_len = ntohs (rp_msg->rd_len);
918 name_tmp = (const char *) &rp_msg[1];
919 rd_ser = &name_tmp[name_len];
921 struct GNUNET_GNSRECORD_Data rd[rd_count];
923 /* We did this before, must succeed again */
924 GNUNET_assert (GNUNET_OK ==
925 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
929 refresh_block (sa->nc,
931 &rp_msg->private_key,
937 GNUNET_SERVICE_client_continue (sa->nc->client);
938 free_store_activity (sa);
943 * Called whenever a client is disconnected.
944 * Frees our resources associated with that client.
947 * @param client identification of the client
948 * @param app_ctx the `struct NamestoreClient` of @a client
951 client_disconnect_cb (void *cls,
952 struct GNUNET_SERVICE_Client *client,
955 struct NamestoreClient *nc = app_ctx;
956 struct ZoneIteration *no;
957 struct CacheOperation *cop;
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
961 "Client %p disconnected\n",
963 for (struct ZoneMonitor *zm = monitor_head; NULL != zm; zm = zm->next)
965 struct StoreActivity *san;
969 GNUNET_CONTAINER_DLL_remove (monitor_head,
972 if (NULL != zm->task)
974 GNUNET_SCHEDULER_cancel (zm->task);
977 if (NULL != zm->sa_wait_warning)
979 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
980 zm->sa_wait_warning = NULL;
982 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = san)
985 if (zm == sa->zm_pos)
987 sa->zm_pos = zm->next;
988 /* this may free sa */
989 continue_store_activity (sa);
995 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = sa->next)
999 /* this may free sa */
1000 free_store_activity (sa);
1001 break; /* there can only be one per nc */
1004 while (NULL != (no = nc->op_head))
1006 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1011 for (cop = cop_head; NULL != cop; cop = cop->next)
1019 * Add a client to our list of active clients.
1022 * @param client client to add
1023 * @param mq message queue for @a client
1024 * @return internal namestore client structure for this client
1027 client_connect_cb (void *cls,
1028 struct GNUNET_SERVICE_Client *client,
1029 struct GNUNET_MQ_Handle *mq)
1031 struct NamestoreClient *nc;
1034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1035 "Client %p connected\n",
1037 nc = GNUNET_new (struct NamestoreClient);
1038 nc->client = client;
1045 * Closure for #lookup_it().
1047 struct RecordLookupContext
1063 struct GNUNET_GNSRECORD_Data *nick;
1073 unsigned int res_rd_count;
1084 * @param seq sequence number of the record
1087 lookup_it (void *cls,
1089 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
1091 unsigned int rd_count,
1092 const struct GNUNET_GNSRECORD_Data *rd)
1094 struct RecordLookupContext *rlc = cls;
1095 struct GNUNET_GNSRECORD_Data *rd_res;
1096 unsigned int rdc_res;
1100 if (0 == strcmp (label,
1103 rlc->found = GNUNET_YES;
1106 if ( (NULL != rlc->nick) &&
1107 (0 != strcmp (label,
1108 GNUNET_GNS_EMPTY_LABEL_AT)) )
1113 rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
1114 merge_with_nick_records (rlc->nick,
1119 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res,
1121 rlc->res_rd_count = rdc_res;
1122 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1123 GNUNET_GNSRECORD_records_serialize (rdc_res,
1128 GNUNET_free (rd_res);
1129 GNUNET_free (rlc->nick);
1134 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1136 rlc->res_rd_count = rd_count;
1137 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1138 GNUNET_GNSRECORD_records_serialize (rd_count,
1146 rlc->rd_ser_len = 0;
1147 rlc->res_rd_count = 0;
1155 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1157 * @param cls client sending the message
1158 * @param ll_msg message of type `struct LabelLookupMessage`
1159 * @return #GNUNET_OK if @a ll_msg is well-formed
1162 check_record_lookup (void *cls,
1163 const struct LabelLookupMessage *ll_msg)
1167 const char *name_tmp;
1170 name_len = ntohl (ll_msg->label_len);
1171 src_size = ntohs (ll_msg->gns_header.header.size);
1172 if (name_len != src_size - sizeof (struct LabelLookupMessage))
1175 return GNUNET_SYSERR;
1178 name_tmp = (const char *) &ll_msg[1];
1179 if ('\0' != name_tmp[name_len -1])
1182 return GNUNET_SYSERR;
1189 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1191 * @param cls client sending the message
1192 * @param ll_msg message of type `struct LabelLookupMessage`
1195 handle_record_lookup (void *cls,
1196 const struct LabelLookupMessage *ll_msg)
1198 struct NamestoreClient *nc = cls;
1199 struct GNUNET_MQ_Envelope *env;
1200 struct LabelLookupResponseMessage *llr_msg;
1201 struct RecordLookupContext rlc;
1202 const char *name_tmp;
1208 name_len = ntohl (ll_msg->label_len);
1209 name_tmp = (const char *) &ll_msg[1];
1210 GNUNET_SERVICE_client_continue (nc->client);
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1212 "Received NAMESTORE_RECORD_LOOKUP message for name `%s'\n",
1215 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1216 if (NULL == conv_name)
1218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1219 "Error converting name `%s'\n",
1221 GNUNET_SERVICE_client_drop (nc->client);
1224 rlc.label = conv_name;
1225 rlc.found = GNUNET_NO;
1226 rlc.res_rd_count = 0;
1229 rlc.nick = get_nick_record (&ll_msg->zone);
1230 res = GSN_database->lookup_records (GSN_database->cls,
1235 GNUNET_free (conv_name);
1236 env = GNUNET_MQ_msg_extra (llr_msg,
1237 name_len + rlc.rd_ser_len,
1238 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
1239 llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
1240 llr_msg->private_key = ll_msg->zone;
1241 llr_msg->name_len = htons (name_len);
1242 llr_msg->rd_count = htons (rlc.res_rd_count);
1243 llr_msg->rd_len = htons (rlc.rd_ser_len);
1244 res_name = (char *) &llr_msg[1];
1245 if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
1246 llr_msg->found = ntohs (GNUNET_YES);
1248 llr_msg->found = ntohs (GNUNET_NO);
1249 GNUNET_memcpy (&llr_msg[1],
1252 GNUNET_memcpy (&res_name[name_len],
1255 GNUNET_MQ_send (nc->mq,
1257 GNUNET_free_non_null (rlc.res_rd);
1262 * Checks a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1264 * @param cls client sending the message
1265 * @param rp_msg message of type `struct RecordStoreMessage`
1266 * @return #GNUNET_OK if @a rp_msg is well-formed
1269 check_record_store (void *cls,
1270 const struct RecordStoreMessage *rp_msg)
1274 size_t msg_size_exp;
1276 const char *name_tmp;
1279 name_len = ntohs (rp_msg->name_len);
1280 msg_size = ntohs (rp_msg->gns_header.header.size);
1281 rd_ser_len = ntohs (rp_msg->rd_len);
1282 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1283 if (msg_size != msg_size_exp)
1286 return GNUNET_SYSERR;
1288 if ( (0 == name_len) ||
1289 (name_len > MAX_NAME_LEN) )
1292 return GNUNET_SYSERR;
1294 name_tmp = (const char *) &rp_msg[1];
1295 if ('\0' != name_tmp[name_len -1])
1298 return GNUNET_SYSERR;
1305 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1307 * @param cls client sending the message
1308 * @param rp_msg message of type `struct RecordStoreMessage`
1311 handle_record_store (void *cls,
1312 const struct RecordStoreMessage *rp_msg)
1314 struct NamestoreClient *nc = cls;
1318 const char *name_tmp;
1321 unsigned int rd_count;
1323 struct StoreActivity *sa;
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1326 "Received NAMESTORE_RECORD_STORE message\n");
1327 rid = ntohl (rp_msg->gns_header.r_id);
1328 name_len = ntohs (rp_msg->name_len);
1329 rd_count = ntohs (rp_msg->rd_count);
1330 rd_ser_len = ntohs (rp_msg->rd_len);
1331 GNUNET_break (0 == ntohs (rp_msg->reserved));
1332 name_tmp = (const char *) &rp_msg[1];
1333 rd_ser = &name_tmp[name_len];
1335 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
1336 struct GNUNET_GNSRECORD_Data rd_clean[GNUNET_NZL(rd_count)];
1337 unsigned int rd_clean_off;
1340 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
1346 GNUNET_SERVICE_client_drop (nc->client);
1350 /* Extracting and converting private key */
1351 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1352 if (NULL == conv_name)
1354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1355 "Error converting name `%s'\n",
1357 GNUNET_SERVICE_client_drop (nc->client);
1360 GNUNET_STATISTICS_update (statistics,
1361 "Well-formed store requests received",
1364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1365 "Creating %u records for name `%s'\n",
1366 (unsigned int) rd_count,
1368 if ( (0 == rd_count) &&
1370 GSN_database->lookup_records (GSN_database->cls,
1371 &rp_msg->private_key,
1376 /* This name does not exist, so cannot be removed */
1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378 "Name `%s' does not exist, no deletion required\n",
1384 /* remove "NICK" records, unless this is for the
1385 #GNUNET_GNS_EMPTY_LABEL_AT label */
1387 for (unsigned int i=0;i<rd_count;i++)
1389 rd_clean[rd_clean_off] = rd[i];
1390 if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
1392 (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
1395 res = GSN_database->store_records (GSN_database->cls,
1396 &rp_msg->private_key,
1402 if (GNUNET_OK != res)
1404 /* store not successful, not need to tell monitors */
1405 send_store_response (nc,
1408 GNUNET_SERVICE_client_continue (nc->client);
1409 GNUNET_free (conv_name);
1413 sa = GNUNET_malloc (sizeof (struct StoreActivity) +
1414 ntohs (rp_msg->gns_header.header.size));
1415 GNUNET_CONTAINER_DLL_insert (sa_head,
1419 sa->rsm = (const struct RecordStoreMessage *) &sa[1];
1420 GNUNET_memcpy (&sa[1],
1422 ntohs (rp_msg->gns_header.header.size));
1423 sa->zm_pos = monitor_head;
1424 sa->conv_name = conv_name;
1425 GNUNET_array_grow (sa->rd,
1428 GNUNET_memcpy (sa->rd,
1430 sizeof (struct GNUNET_GNSRECORD_Data) * rd_clean_off);
1431 continue_store_activity (sa);
1437 * Context for record remove operations passed from #handle_zone_to_name to
1438 * #handle_zone_to_name_it as closure
1440 struct ZoneToNameCtx
1445 struct NamestoreClient *nc;
1448 * Request id (to be used in the response to the client).
1453 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
1454 * not finding a name for the zone still counts as a 'success' here,
1455 * as this field is about the success of executing the IPC protocol.
1462 * Zone to name iterator
1464 * @param cls struct ZoneToNameCtx *
1465 * @param seq sequence number of the record
1466 * @param zone_key the zone key
1468 * @param rd_count number of records in @a rd
1469 * @param rd record data
1472 handle_zone_to_name_it (void *cls,
1474 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1476 unsigned int rd_count,
1477 const struct GNUNET_GNSRECORD_Data *rd)
1479 struct ZoneToNameCtx *ztn_ctx = cls;
1480 struct GNUNET_MQ_Envelope *env;
1481 struct ZoneToNameResponseMessage *ztnr_msg;
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1491 "Found result for zone-to-name lookup: `%s'\n",
1494 name_len = (NULL == name) ? 0 : strlen (name) + 1;
1495 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
1496 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
1497 if (msg_size >= GNUNET_MAX_MESSAGE_SIZE)
1500 ztn_ctx->success = GNUNET_SYSERR;
1503 env = GNUNET_MQ_msg_extra (ztnr_msg,
1504 name_len + rd_ser_len,
1505 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1506 ztnr_msg->gns_header.header.size = htons (msg_size);
1507 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1508 ztnr_msg->res = htons (res);
1509 ztnr_msg->rd_len = htons (rd_ser_len);
1510 ztnr_msg->rd_count = htons (rd_count);
1511 ztnr_msg->name_len = htons (name_len);
1512 ztnr_msg->zone = *zone_key;
1513 name_tmp = (char *) &ztnr_msg[1];
1514 GNUNET_memcpy (name_tmp,
1517 rd_tmp = &name_tmp[name_len];
1518 GNUNET_GNSRECORD_records_serialize (rd_count,
1522 ztn_ctx->success = GNUNET_OK;
1523 GNUNET_MQ_send (ztn_ctx->nc->mq,
1529 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
1531 * @param cls client client sending the message
1532 * @param ztn_msg message of type 'struct ZoneToNameMessage'
1535 handle_zone_to_name (void *cls,
1536 const struct ZoneToNameMessage *ztn_msg)
1538 struct NamestoreClient *nc = cls;
1539 struct ZoneToNameCtx ztn_ctx;
1540 struct GNUNET_MQ_Envelope *env;
1541 struct ZoneToNameResponseMessage *ztnr_msg;
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1544 "Received ZONE_TO_NAME message\n");
1545 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
1547 ztn_ctx.success = GNUNET_NO;
1548 if (GNUNET_SYSERR ==
1549 GSN_database->zone_to_name (GSN_database->cls,
1551 &ztn_msg->value_zone,
1552 &handle_zone_to_name_it, &ztn_ctx))
1554 /* internal error, hang up instead of signalling something
1555 that might be wrong */
1557 GNUNET_SERVICE_client_drop (nc->client);
1560 if (GNUNET_NO == ztn_ctx.success)
1562 /* no result found, send empty response */
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1564 "Found no result for zone-to-name lookup.\n");
1565 env = GNUNET_MQ_msg (ztnr_msg,
1566 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1567 ztnr_msg->gns_header.r_id = ztn_msg->gns_header.r_id;
1568 ztnr_msg->res = htons (GNUNET_NO);
1569 GNUNET_MQ_send (nc->mq,
1572 GNUNET_SERVICE_client_continue (nc->client);
1577 * Context for record remove operations passed from
1578 * #run_zone_iteration_round to #zone_iterate_proc as closure
1580 struct ZoneIterationProcResult
1583 * The zone iteration handle
1585 struct ZoneIteration *zi;
1588 * Number of results left to be returned in this iteration.
1596 * Process results for zone iteration from database
1598 * @param cls struct ZoneIterationProcResult
1599 * @param seq sequence number of the record
1600 * @param zone_key the zone key
1602 * @param rd_count number of records for this name
1603 * @param rd record data
1606 zone_iterate_proc (void *cls,
1608 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1610 unsigned int rd_count,
1611 const struct GNUNET_GNSRECORD_Data *rd)
1613 struct ZoneIterationProcResult *proc = cls;
1614 int do_refresh_block;
1616 if ( (NULL == zone_key) &&
1619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1620 "Iteration done\n");
1623 if ( (NULL == zone_key) ||
1626 /* what is this!? should never happen */
1630 if (0 == proc->limit)
1632 /* what is this!? should never happen */
1637 proc->zi->seq = seq;
1638 send_lookup_response (proc->zi->nc,
1639 proc->zi->request_id,
1644 do_refresh_block = GNUNET_NO;
1645 for (unsigned int i=0;i<rd_count;i++)
1646 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
1648 do_refresh_block = GNUNET_YES;
1651 if (GNUNET_YES == do_refresh_block)
1652 refresh_block (NULL,
1662 * Perform the next round of the zone iteration.
1664 * @param zi zone iterator to process
1665 * @param limit number of results to return in one pass
1668 run_zone_iteration_round (struct ZoneIteration *zi,
1671 struct ZoneIterationProcResult proc;
1672 struct GNUNET_MQ_Envelope *env;
1673 struct RecordResultMessage *rrm;
1674 struct GNUNET_TIME_Absolute start;
1675 struct GNUNET_TIME_Relative duration;
1680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1681 "Asked to return up to %llu records at position %llu\n",
1682 (unsigned long long) limit,
1683 (unsigned long long) zi->seq);
1686 start = GNUNET_TIME_absolute_get ();
1687 GNUNET_break (GNUNET_SYSERR !=
1688 GSN_database->iterate_records (GSN_database->cls,
1689 (0 == memcmp (&zi->zone,
1698 duration = GNUNET_TIME_absolute_get_duration (start);
1699 duration = GNUNET_TIME_relative_divide (duration,
1700 limit - proc.limit);
1701 GNUNET_STATISTICS_set (statistics,
1702 "NAMESTORE iteration delay (μs/record)",
1703 duration.rel_value_us,
1705 if (0 == proc.limit)
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1708 "Returned %llu results, more results available\n",
1709 (unsigned long long) limit);
1710 return; /* more results later after we get the
1711 #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message */
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1714 "Completed iteration after %llu/%llu results\n",
1715 (unsigned long long) (limit - proc.limit),
1716 (unsigned long long) limit);
1717 /* send empty response to indicate end of list */
1718 env = GNUNET_MQ_msg (rrm,
1719 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
1720 rrm->gns_header.r_id = htonl (zi->request_id);
1721 GNUNET_MQ_send (zi->nc->mq,
1723 GNUNET_CONTAINER_DLL_remove (zi->nc->op_head,
1731 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1733 * @param cls the client sending the message
1734 * @param zis_msg message from the client
1737 handle_iteration_start (void *cls,
1738 const struct ZoneIterationStartMessage *zis_msg)
1740 struct NamestoreClient *nc = cls;
1741 struct ZoneIteration *zi;
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1744 "Received ZONE_ITERATION_START message\n");
1745 zi = GNUNET_new (struct ZoneIteration);
1746 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1749 zi->zone = zis_msg->zone;
1751 GNUNET_CONTAINER_DLL_insert (nc->op_head,
1754 run_zone_iteration_round (zi,
1756 GNUNET_SERVICE_client_continue (nc->client);
1761 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1763 * @param cls the client sending the message
1764 * @param zis_msg message from the client
1767 handle_iteration_stop (void *cls,
1768 const struct ZoneIterationStopMessage *zis_msg)
1770 struct NamestoreClient *nc = cls;
1771 struct ZoneIteration *zi;
1774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1775 "Received ZONE_ITERATION_STOP message\n");
1776 rid = ntohl (zis_msg->gns_header.r_id);
1777 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1778 if (zi->request_id == rid)
1783 GNUNET_SERVICE_client_drop (nc->client);
1786 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1790 GNUNET_SERVICE_client_continue (nc->client);
1795 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1797 * @param cls the client sending the message
1798 * @param message message from the client
1801 handle_iteration_next (void *cls,
1802 const struct ZoneIterationNextMessage *zis_msg)
1804 struct NamestoreClient *nc = cls;
1805 struct ZoneIteration *zi;
1809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1810 "Received ZONE_ITERATION_NEXT message\n");
1811 GNUNET_STATISTICS_update (statistics,
1812 "Iteration NEXT messages received",
1815 rid = ntohl (zis_msg->gns_header.r_id);
1816 limit = GNUNET_ntohll (zis_msg->limit);
1817 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1818 if (zi->request_id == rid)
1823 GNUNET_SERVICE_client_drop (nc->client);
1826 run_zone_iteration_round (zi,
1828 GNUNET_SERVICE_client_continue (nc->client);
1833 * Function called when the monitor is ready for more data, and we
1834 * should thus unblock PUT operations that were blocked on the
1835 * monitor not being ready.
1838 monitor_unblock (struct ZoneMonitor *zm)
1840 struct StoreActivity *sa = sa_head;
1842 while ( (NULL != sa) &&
1843 (zm->limit > zm->iteration_cnt) )
1845 struct StoreActivity *sn = sa->next;
1847 if (sa->zm_pos == zm)
1848 continue_store_activity (sa);
1851 if (zm->limit > zm->iteration_cnt)
1853 zm->sa_waiting = GNUNET_NO;
1854 if (NULL != zm->sa_wait_warning)
1856 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1857 zm->sa_wait_warning = NULL;
1860 else if (GNUNET_YES == zm->sa_waiting)
1862 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
1863 if (NULL != zm->sa_wait_warning)
1864 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1865 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
1873 * Send 'sync' message to zone monitor, we're now in sync.
1875 * @param zm monitor that is now in sync
1878 monitor_sync (struct ZoneMonitor *zm)
1880 struct GNUNET_MQ_Envelope *env;
1881 struct GNUNET_MessageHeader *sync;
1883 env = GNUNET_MQ_msg (sync,
1884 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1885 GNUNET_MQ_send (zm->nc->mq,
1887 /* mark iteration done */
1888 zm->in_first_iteration = GNUNET_NO;
1889 zm->iteration_cnt = 0;
1890 if ( (zm->limit > 0) &&
1892 monitor_unblock (zm);
1897 * Obtain the next datum during the zone monitor's zone initial iteration.
1899 * @param cls zone monitor that does its initial iteration
1902 monitor_iteration_next (void *cls);
1906 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1908 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1909 * @param seq sequence number of the record
1910 * @param zone_key zone key of the zone
1912 * @param rd_count number of records in @a rd
1913 * @param rd array of records
1916 monitor_iterate_cb (void *cls,
1918 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1920 unsigned int rd_count,
1921 const struct GNUNET_GNSRECORD_Data *rd)
1923 struct ZoneMonitor *zm = cls;
1928 /* finished with iteration */
1932 GNUNET_STATISTICS_update (statistics,
1933 "Monitor notifications sent",
1937 zm->iteration_cnt--;
1938 send_lookup_response (zm->nc,
1944 if ( (0 == zm->iteration_cnt) &&
1947 /* We are done with the current iteration batch, AND the
1948 client would right now accept more, so go again! */
1949 GNUNET_assert (NULL == zm->task);
1950 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
1957 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
1959 * @param cls the client sending the message
1960 * @param zis_msg message from the client
1963 handle_monitor_start (void *cls,
1964 const struct ZoneMonitorStartMessage *zis_msg)
1966 struct NamestoreClient *nc = cls;
1967 struct ZoneMonitor *zm;
1969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1970 "Received ZONE_MONITOR_START message\n");
1971 zm = GNUNET_new (struct ZoneMonitor);
1973 zm->zone = zis_msg->zone;
1975 zm->in_first_iteration = (GNUNET_YES == ntohl (zis_msg->iterate_first));
1976 GNUNET_CONTAINER_DLL_insert (monitor_head,
1979 GNUNET_SERVICE_client_mark_monitor (nc->client);
1980 GNUNET_SERVICE_client_continue (nc->client);
1981 GNUNET_notification_context_add (monitor_nc,
1983 if (zm->in_first_iteration)
1984 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
1992 * Obtain the next datum during the zone monitor's zone initial iteration.
1994 * @param cls zone monitor that does its initial iteration
1997 monitor_iteration_next (void *cls)
1999 struct ZoneMonitor *zm = cls;
2003 GNUNET_assert (0 == zm->iteration_cnt);
2005 zm->iteration_cnt = zm->limit / 2; /* leave half for monitor events */
2007 zm->iteration_cnt = zm->limit; /* use it all */
2008 ret = GSN_database->iterate_records (GSN_database->cls,
2009 (0 == memcmp (&zm->zone,
2016 &monitor_iterate_cb,
2018 if (GNUNET_SYSERR == ret)
2020 GNUNET_SERVICE_client_drop (zm->nc->client);
2023 if (GNUNET_NO == ret)
2033 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT message
2035 * @param cls the client sending the message
2036 * @param nm message from the client
2039 handle_monitor_next (void *cls,
2040 const struct ZoneMonitorNextMessage *nm)
2042 struct NamestoreClient *nc = cls;
2043 struct ZoneMonitor *zm;
2046 inc = GNUNET_ntohll (nm->limit);
2047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2048 "Received ZONE_MONITOR_NEXT message with limit %llu\n",
2049 (unsigned long long) inc);
2050 for (zm = monitor_head; NULL != zm; zm = zm->next)
2056 GNUNET_SERVICE_client_drop (nc->client);
2059 GNUNET_SERVICE_client_continue (nc->client);
2060 if (zm->limit + inc < zm->limit)
2063 GNUNET_SERVICE_client_drop (nc->client);
2067 if ( (zm->in_first_iteration) &&
2068 (zm->limit == inc) )
2070 /* We are still iterating, and the previous iteration must
2071 have stopped due to the client's limit, so continue it! */
2072 GNUNET_assert (NULL == zm->task);
2073 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2076 GNUNET_assert (zm->iteration_cnt <= zm->limit);
2077 if ( (zm->limit > zm->iteration_cnt) &&
2080 monitor_unblock (zm);
2082 else if (GNUNET_YES == zm->sa_waiting)
2084 if (NULL != zm->sa_wait_warning)
2085 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
2086 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
2087 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
2095 * Process namestore requests.
2097 * @param cls closure
2098 * @param cfg configuration to use
2099 * @param service the initialized service
2103 const struct GNUNET_CONFIGURATION_Handle *cfg,
2104 struct GNUNET_SERVICE_Handle *service)
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 "Starting namestore service\n");
2112 cache_keys = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2115 disable_namecache = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2119 monitor_nc = GNUNET_notification_context_create (1);
2120 if (GNUNET_YES != disable_namecache)
2122 namecache = GNUNET_NAMECACHE_connect (cfg);
2123 GNUNET_assert (NULL != namecache);
2125 /* Loading database plugin */
2127 GNUNET_CONFIGURATION_get_value_string (cfg,
2131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2132 "No database backend configured\n");
2134 GNUNET_asprintf (&db_lib_name,
2135 "libgnunet_plugin_namestore_%s",
2137 GSN_database = GNUNET_PLUGIN_load (db_lib_name,
2139 GNUNET_free (database);
2140 statistics = GNUNET_STATISTICS_create ("namestore",
2142 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
2144 if (NULL == GSN_database)
2146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2147 "Could not load database backend `%s'\n",
2149 GNUNET_SCHEDULER_shutdown ();
2156 * Define "main" method using service macro.
2160 GNUNET_SERVICE_OPTION_NONE,
2163 &client_disconnect_cb,
2165 GNUNET_MQ_hd_var_size (record_store,
2166 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE,
2167 struct RecordStoreMessage,
2169 GNUNET_MQ_hd_var_size (record_lookup,
2170 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP,
2171 struct LabelLookupMessage,
2173 GNUNET_MQ_hd_fixed_size (zone_to_name,
2174 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME,
2175 struct ZoneToNameMessage,
2177 GNUNET_MQ_hd_fixed_size (iteration_start,
2178 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START,
2179 struct ZoneIterationStartMessage,
2181 GNUNET_MQ_hd_fixed_size (iteration_next,
2182 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT,
2183 struct ZoneIterationNextMessage,
2185 GNUNET_MQ_hd_fixed_size (iteration_stop,
2186 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP,
2187 struct ZoneIterationStopMessage,
2189 GNUNET_MQ_hd_fixed_size (monitor_start,
2190 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START,
2191 struct ZoneMonitorStartMessage,
2193 GNUNET_MQ_hd_fixed_size (monitor_next,
2194 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT,
2195 struct ZoneMonitorNextMessage,
2197 GNUNET_MQ_handler_end ());
2200 /* end of gnunet-service-namestore.c */