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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file namestore/gnunet-service-namestore.c
23 * @brief namestore for the GNUnet naming system
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 * - "get_nick_record" is a bottleneck, introduce a cache to
29 * avoid looking it up again and again (for the same few
30 * zones that the user will typically manage!)
31 * - run testcases, make sure everything works!
34 #include "gnunet_util_lib.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "gnunet_gns_service.h"
37 #include "gnunet_namecache_service.h"
38 #include "gnunet_namestore_service.h"
39 #include "gnunet_namestore_plugin.h"
40 #include "gnunet_statistics_service.h"
41 #include "gnunet_signatures.h"
42 #include "namestore.h"
44 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
47 * If a monitor takes more than 1 minute to process an event, print a warning.
49 #define MONITOR_STALL_WARN_DELAY GNUNET_TIME_UNIT_MINUTES
52 * Size of the cache used by #get_nick_record()
59 struct NamestoreClient;
63 * A namestore iteration operation.
68 * Next element in the DLL
70 struct ZoneIteration *next;
73 * Previous element in the DLL
75 struct ZoneIteration *prev;
78 * Namestore client which intiated this zone iteration
80 struct NamestoreClient *nc;
83 * The nick to add to the records
85 struct GNUNET_GNSRECORD_Data *nick;
88 * Key of the zone we are iterating over.
90 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
93 * Last sequence number in the zone iteration used to address next
94 * result of the zone iteration in the store
97 * Updated in #zone_iterate_proc()
102 * The operation id fot the zone iteration in the response for the client
107 * Offset of the zone iteration used to address next result of the zone
108 * iteration in the store
110 * Initialy set to 0 in #handle_iteration_start
111 * Incremented with by every call to #handle_iteration_next
116 * Number of pending cache operations triggered by this zone iteration which we
117 * need to wait for before allowing the client to continue.
119 unsigned int cache_ops;
122 * Set to #GNUNET_YES if the last iteration exhausted the limit set by the
123 * client and we should send the #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END
124 * message and free the data structure once @e cache_ops is zero.
134 struct NamestoreClient
140 struct GNUNET_SERVICE_Client *client;
143 * Message queue for transmission to @e client
145 struct GNUNET_MQ_Handle *mq;
149 * Zone iteration operations in progress initiated by this client
151 struct ZoneIteration *op_head;
155 * Zone iteration operations in progress initiated by this client
157 struct ZoneIteration *op_tail;
162 * A namestore monitor.
167 * Next element in the DLL
169 struct ZoneMonitor *next;
172 * Previous element in the DLL
174 struct ZoneMonitor *prev;
177 * Namestore client which intiated this zone monitor
179 struct NamestoreClient *nc;
182 * Private key of the zone.
184 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
187 * Task active during initial iteration.
189 struct GNUNET_SCHEDULER_Task *task;
192 * Task to warn about slow monitors.
194 struct GNUNET_SCHEDULER_Task *sa_wait_warning;
197 * Since when are we blocked on this monitor?
199 struct GNUNET_TIME_Absolute sa_waiting_start;
202 * Last sequence number in the zone iteration used to address next
203 * result of the zone iteration in the store
206 * Updated in #monitor_iterate_cb()
211 * Current limit of how many more messages we are allowed
212 * to queue to this monitor.
217 * How many more requests may we receive from the iterator
218 * before it is at the limit we gave it? Will be below or
219 * equal to @e limit. The effective limit for monitor
220 * events is thus @e iteration_cnt - @e limit!
222 uint64_t iteration_cnt;
225 * Are we (still) in the initial iteration pass?
227 int in_first_iteration;
230 * Is there a store activity waiting for this monitor? We only raise the
231 * flag when it happens and search the DLL for the store activity when we
232 * had a limit increase. If we cannot find any waiting store activity at
233 * that time, we clear the flag again.
241 * Pending operation on the namecache.
243 struct CacheOperation
249 struct CacheOperation *prev;
254 struct CacheOperation *next;
257 * Handle to namecache queue.
259 struct GNUNET_NAMECACHE_QueueEntry *qe;
262 * Client to notify about the result, can be NULL.
264 struct NamestoreClient *nc;
267 * Zone iteration to call #zone_iteration_done_client_continue()
268 * for if applicable, can be NULL.
270 struct ZoneIteration *zi;
273 * Client's request ID.
280 * Information for an ongoing #handle_record_store() operation.
281 * Needed as we may wait for monitors to be ready for the notification.
288 struct StoreActivity *next;
293 struct StoreActivity *prev;
296 * Which client triggered the store activity?
298 struct NamestoreClient *nc;
301 * Copy of the original store message (as data fields in @e rd will
304 const struct RecordStoreMessage *rsm;
307 * Next zone monitor that still needs to be notified about this PUT.
309 struct ZoneMonitor *zm_pos;
312 * Label nicely canonicalized (lower case).
320 * Entry in list of cached nick resolutions.
325 * Zone the cache entry is for.
327 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
330 * Cached record data.
332 struct GNUNET_GNSRECORD_Data *rd;
335 * Timestamp when this cache entry was used last.
337 struct GNUNET_TIME_Absolute last_used;
342 * We cache nick records to reduce DB load.
344 static struct NickCache nick_cache[NC_SIZE];
347 * Public key of all zeros.
349 static const struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
352 * Configuration handle.
354 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
357 * Handle to the statistics service
359 static struct GNUNET_STATISTICS_Handle *statistics;
364 static struct GNUNET_NAMECACHE_Handle *namecache;
369 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
372 * Name of the database plugin
374 static char *db_lib_name;
379 static struct CacheOperation *cop_head;
384 static struct CacheOperation *cop_tail;
387 * First active zone monitor.
389 static struct ZoneMonitor *monitor_head;
392 * Last active zone monitor.
394 static struct ZoneMonitor *monitor_tail;
397 * Head of DLL of monitor-blocked store activities.
399 static struct StoreActivity *sa_head;
402 * Tail of DLL of monitor-blocked store activities.
404 static struct StoreActivity *sa_tail;
407 * Notification context shared by all monitors.
409 static struct GNUNET_NotificationContext *monitor_nc;
412 * Optimize block insertion by caching map of private keys to
413 * public keys in memory?
415 static int cache_keys;
418 * Use the namecache? Doing so creates additional cryptographic
419 * operations whenever we touch a record.
421 static int disable_namecache;
425 * Task run during shutdown.
430 cleanup_task (void *cls)
432 struct CacheOperation *cop;
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "Stopping namestore service\n");
437 while (NULL != (cop = cop_head))
439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
440 "Aborting incomplete namecache operation\n");
441 GNUNET_NAMECACHE_cancel (cop->qe);
442 GNUNET_CONTAINER_DLL_remove (cop_head,
447 if (NULL != namecache)
449 GNUNET_NAMECACHE_disconnect (namecache);
452 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name,
454 GNUNET_free (db_lib_name);
456 if (NULL != monitor_nc)
458 GNUNET_notification_context_destroy (monitor_nc);
461 if (NULL != statistics)
463 GNUNET_STATISTICS_destroy (statistics,
471 * Release memory used by @a sa.
473 * @param sa activity to free
476 free_store_activity (struct StoreActivity *sa)
478 GNUNET_CONTAINER_DLL_remove (sa_head,
481 GNUNET_free (sa->conv_name);
487 * Function called with the records for the #GNUNET_GNS_EMPTY_LABEL_AT
488 * label in the zone. Used to locate the #GNUNET_GNSRECORD_TYPE_NICK
489 * record, which (if found) is then copied to @a cls for future use.
491 * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
492 * @param seq sequence number of the record, MUST NOT BE ZERO
493 * @param private_key the private key of the zone (unused)
494 * @param label should be #GNUNET_GNS_EMPTY_LABEL_AT
495 * @param rd_count number of records in @a rd
496 * @param rd records stored under @a label in the zone
499 lookup_nick_it (void *cls,
501 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
503 unsigned int rd_count,
504 const struct GNUNET_GNSRECORD_Data *rd)
506 struct GNUNET_GNSRECORD_Data **res = cls;
509 GNUNET_assert (0 != seq);
510 if (0 != strcmp (label, GNUNET_GNS_EMPTY_LABEL_AT))
515 for (unsigned int c = 0; c < rd_count; c++)
517 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
519 (*res) = GNUNET_malloc (rd[c].data_size + sizeof (struct GNUNET_GNSRECORD_Data));
520 (*res)->data = &(*res)[1];
521 GNUNET_memcpy ((void *) (*res)->data,
524 (*res)->data_size = rd[c].data_size;
525 (*res)->expiration_time = rd[c].expiration_time;
526 (*res)->flags = rd[c].flags;
527 (*res)->record_type = GNUNET_GNSRECORD_TYPE_NICK;
536 * Add entry to the cache for @a zone and @a nick
538 * @param zone zone key to cache under
539 * @param nick nick entry to cache
542 cache_nick (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
543 const struct GNUNET_GNSRECORD_Data *nick)
545 struct NickCache *oldest;
548 for (unsigned int i=0;i<NC_SIZE;i++)
550 struct NickCache *pos = &nick_cache[i];
552 if ( (NULL == oldest) ||
553 (oldest->last_used.abs_value_us >
554 pos->last_used.abs_value_us) )
556 if (0 == GNUNET_memcmp (zone,
563 GNUNET_free_non_null (oldest->rd);
564 oldest->zone = *zone;
565 oldest->rd = GNUNET_malloc (sizeof (*nick) +
568 oldest->rd->data = &oldest->rd[1];
569 memcpy (&oldest->rd[1],
572 oldest->last_used = GNUNET_TIME_absolute_get ();
577 * Return the NICK record for the zone (if it exists).
579 * @param zone private key for the zone to look for nick
580 * @return NULL if no NICK record was found
582 static struct GNUNET_GNSRECORD_Data *
583 get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
585 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
586 struct GNUNET_GNSRECORD_Data *nick;
589 /* check cache first */
590 for (unsigned int i=0;i<NC_SIZE;i++)
592 struct NickCache *pos = &nick_cache[i];
593 if ( (NULL != pos->rd) &&
594 (0 == GNUNET_memcmp (zone,
597 nick = GNUNET_malloc (sizeof (*nick) +
600 nick->data = &nick[1];
604 pos->last_used = GNUNET_TIME_absolute_get ();
610 res = GSN_database->lookup_records (GSN_database->cls,
612 GNUNET_GNS_EMPTY_LABEL_AT,
615 if ( (GNUNET_OK != res) ||
618 GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
620 "No nick name set for zone `%s'\n",
621 GNUNET_GNSRECORD_z2s (&pub));
633 * Merge the nick record @a nick_rd with the rest of the
634 * record set given in @a rd2. Store the result in @a rdc_res
635 * and @a rd_res. The @a nick_rd's expiration time is set to
636 * the maximum expiration time of all of the records in @a rd2.
638 * @param nick_rd the nick record to integrate
639 * @param rd2_length length of the @a rd2 array
640 * @param rd2 array of records
641 * @param rdc_res[out] length of the resulting @a rd_res array
642 * @param rd_res[out] set to an array of records,
643 * including @a nick_rd and @a rd2;
644 * all of the variable-size 'data' fields in @a rd2 are
645 * allocated in the same chunk of memory!
648 merge_with_nick_records (const struct GNUNET_GNSRECORD_Data *nick_rd,
649 unsigned int rd2_length,
650 const struct GNUNET_GNSRECORD_Data *rd2,
651 unsigned int *rdc_res,
652 struct GNUNET_GNSRECORD_Data **rd_res)
654 uint64_t latest_expiration;
658 struct GNUNET_GNSRECORD_Data *target;
660 (*rdc_res) = 1 + rd2_length;
661 if (0 == 1 + rd2_length)
667 req = sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd->data_size;
668 for (unsigned int i=0; i<rd2_length; i++)
670 const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
672 if (req + sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size < req)
678 req += sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size;
680 target = GNUNET_malloc (req);
682 data = (char *) &target[1 + rd2_length];
684 latest_expiration = 0;
685 for (unsigned int i=0;i<rd2_length;i++)
687 const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
689 if (0 != (orig->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
691 if ((GNUNET_TIME_absolute_get().abs_value_us + orig->expiration_time) >
693 latest_expiration = orig->expiration_time;
695 else if (orig->expiration_time > latest_expiration)
696 latest_expiration = orig->expiration_time;
698 target[i].data = (void *) &data[data_offset];
699 GNUNET_memcpy (&data[data_offset],
702 data_offset += orig->data_size;
705 target[rd2_length] = *nick_rd;
706 target[rd2_length].expiration_time = latest_expiration;
707 target[rd2_length].data = (void *) &data[data_offset];
708 GNUNET_memcpy (&data[data_offset],
711 data_offset += nick_rd->data_size;
712 GNUNET_assert (req ==
713 (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
718 * Generate a `struct LookupNameResponseMessage` and send it to the
719 * given client using the given notification context.
721 * @param nc client to unicast to
722 * @param request_id request ID to use
723 * @param zone_key zone key of the zone
725 * @param rd_count number of records in @a rd
726 * @param rd array of records
729 send_lookup_response (struct NamestoreClient *nc,
731 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
733 unsigned int rd_count,
734 const struct GNUNET_GNSRECORD_Data *rd)
736 struct GNUNET_MQ_Envelope *env;
737 struct RecordResultMessage *zir_msg;
738 struct GNUNET_GNSRECORD_Data *nick;
739 struct GNUNET_GNSRECORD_Data *res;
740 unsigned int res_count;
746 nick = get_nick_record (zone_key);
748 GNUNET_GNSRECORD_records_get_size (rd_count,
751 if ( (NULL != nick) &&
753 GNUNET_GNS_EMPTY_LABEL_AT)))
755 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
756 merge_with_nick_records (nick,
765 res_count = rd_count;
766 res = (struct GNUNET_GNSRECORD_Data *) rd;
770 GNUNET_GNSRECORD_records_get_size (res_count,
774 name_len = strlen (name) + 1;
775 rd_ser_len = GNUNET_GNSRECORD_records_get_size (res_count,
780 GNUNET_SERVICE_client_drop (nc->client);
783 if (((size_t) rd_ser_len) >= UINT16_MAX - name_len - sizeof (*zir_msg))
786 GNUNET_SERVICE_client_drop (nc->client);
789 env = GNUNET_MQ_msg_extra (zir_msg,
790 name_len + rd_ser_len,
791 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
792 zir_msg->gns_header.r_id = htonl (request_id);
793 zir_msg->name_len = htons (name_len);
794 zir_msg->rd_count = htons (res_count);
795 zir_msg->rd_len = htons ((uint16_t) rd_ser_len);
796 zir_msg->private_key = *zone_key;
797 name_tmp = (char *) &zir_msg[1];
798 GNUNET_memcpy (name_tmp,
801 rd_ser = &name_tmp[name_len];
802 GNUNET_assert (rd_ser_len ==
803 GNUNET_GNSRECORD_records_serialize (res_count,
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808 "Sending RECORD_RESULT message with %u records\n",
810 GNUNET_STATISTICS_update (statistics,
811 "Record sets sent to clients",
814 GNUNET_MQ_send (nc->mq,
822 * Send response to the store request to the client.
824 * @param client client to talk to
825 * @param res status of the operation
826 * @param rid client's request ID
829 send_store_response (struct NamestoreClient *nc,
833 struct GNUNET_MQ_Envelope *env;
834 struct RecordStoreResponseMessage *rcr_msg;
836 GNUNET_assert (NULL != nc);
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "Sending RECORD_STORE_RESPONSE message\n");
839 GNUNET_STATISTICS_update (statistics,
840 "Store requests completed",
843 env = GNUNET_MQ_msg (rcr_msg,
844 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
845 rcr_msg->gns_header.r_id = htonl (rid);
846 rcr_msg->op_result = htonl (res);
847 GNUNET_MQ_send (nc->mq,
853 * Function called once we are done with the zone iteration and
854 * allow the zone iteration client to send us more messages.
856 * @param zi zone iteration we are processing
859 zone_iteration_done_client_continue (struct ZoneIteration *zi)
861 struct GNUNET_MQ_Envelope *env;
862 struct GNUNET_NAMESTORE_Header *em;
864 GNUNET_SERVICE_client_continue (zi->nc->client);
867 /* send empty response to indicate end of list */
868 env = GNUNET_MQ_msg (em,
869 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END);
870 em->r_id = htonl (zi->request_id);
871 GNUNET_MQ_send (zi->nc->mq,
874 GNUNET_CONTAINER_DLL_remove (zi->nc->op_head,
882 * Cache operation complete, clean up.
884 * @param cls the `struct CacheOperation`
885 * @param success success
886 * @param emsg error messages
889 finish_cache_operation (void *cls,
893 struct CacheOperation *cop = cls;
894 struct ZoneIteration *zi;
897 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
898 _("Failed to replicate block in namecache: %s\n"),
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
902 "CACHE operation completed\n");
903 GNUNET_CONTAINER_DLL_remove (cop_head,
907 send_store_response (cop->nc,
910 if (NULL != (zi = cop->zi))
913 if (0 == zi->cache_ops)
915 /* unchoke zone iteration, cache has caught up */
916 zone_iteration_done_client_continue (zi);
924 * We just touched the plaintext information about a name in our zone;
925 * refresh the corresponding (encrypted) block in the namecache.
927 * @param nc client responsible for the request, can be NULL
928 * @param zi zone iteration response for the request, can be NULL
929 * @param rid request ID of the client
930 * @param zone_key private key of the zone
931 * @param name label for the records
932 * @param rd_count number of records
933 * @param rd records stored under the given @a name
936 refresh_block (struct NamestoreClient *nc,
937 struct ZoneIteration *zi,
939 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
941 unsigned int rd_count,
942 const struct GNUNET_GNSRECORD_Data *rd)
944 struct GNUNET_GNSRECORD_Block *block;
945 struct CacheOperation *cop;
946 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
947 struct GNUNET_GNSRECORD_Data *nick;
948 struct GNUNET_GNSRECORD_Data *res;
949 unsigned int res_count;
950 struct GNUNET_TIME_Absolute exp_time;
952 nick = get_nick_record (zone_key);
953 res_count = rd_count;
954 res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
957 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
958 merge_with_nick_records (nick,
967 send_store_response (nc,
970 return; /* no data, no need to update cache */
972 if (GNUNET_YES == disable_namecache)
974 GNUNET_STATISTICS_update (statistics,
975 "Namecache updates skipped (NC disabled)",
979 send_store_response (nc,
984 exp_time = GNUNET_GNSRECORD_record_get_expiration_time (res_count,
987 block = GNUNET_GNSRECORD_block_create2 (zone_key,
993 block = GNUNET_GNSRECORD_block_create (zone_key,
998 GNUNET_assert (NULL != block);
999 GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
1001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1002 "Caching block for label `%s' with %u records and expiration %s in zone `%s' in namecache\n",
1005 GNUNET_STRINGS_absolute_time_to_string (exp_time),
1006 GNUNET_GNSRECORD_z2s (&pkey));
1007 GNUNET_STATISTICS_update (statistics,
1008 "Namecache updates pushed",
1011 cop = GNUNET_new (struct CacheOperation);
1017 GNUNET_CONTAINER_DLL_insert (cop_head,
1020 cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
1022 &finish_cache_operation,
1024 GNUNET_free (block);
1029 * Print a warning that one of our monitors is no longer reacting.
1031 * @param cls a `struct ZoneMonitor` to warn about
1034 warn_monitor_slow (void *cls)
1036 struct ZoneMonitor *zm = cls;
1038 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1039 "No response from monitor since %s\n",
1040 GNUNET_STRINGS_absolute_time_to_string (zm->sa_waiting_start));
1041 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
1048 * Continue processing the @a sa.
1050 * @param sa store activity to process
1053 continue_store_activity (struct StoreActivity *sa)
1055 const struct RecordStoreMessage *rp_msg = sa->rsm;
1056 unsigned int rd_count;
1060 const char *name_tmp;
1063 rid = ntohl (rp_msg->gns_header.r_id);
1064 name_len = ntohs (rp_msg->name_len);
1065 rd_count = ntohs (rp_msg->rd_count);
1066 rd_ser_len = ntohs (rp_msg->rd_len);
1067 name_tmp = (const char *) &rp_msg[1];
1068 rd_ser = &name_tmp[name_len];
1070 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
1072 /* We did this before, must succeed again */
1073 GNUNET_assert (GNUNET_OK ==
1074 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
1079 for (struct ZoneMonitor *zm = sa->zm_pos;
1083 if ( (0 != GNUNET_memcmp (&rp_msg->private_key,
1085 (0 != GNUNET_memcmp (&zm->zone,
1088 sa->zm_pos = zm->next; /* not interesting to this monitor */
1091 if (zm->limit == zm->iteration_cnt)
1093 zm->sa_waiting = GNUNET_YES;
1094 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
1095 if (NULL != zm->sa_wait_warning)
1096 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1097 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
1100 return; /* blocked on zone monitor */
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1103 "Notifying monitor about changes under label `%s'\n",
1106 send_lookup_response (zm->nc,
1108 &rp_msg->private_key,
1112 sa->zm_pos = zm->next;
1114 /* great, done with the monitors, unpack (again) for refresh_block operation */
1115 refresh_block (sa->nc,
1118 &rp_msg->private_key,
1123 GNUNET_SERVICE_client_continue (sa->nc->client);
1124 free_store_activity (sa);
1129 * Called whenever a client is disconnected.
1130 * Frees our resources associated with that client.
1132 * @param cls closure
1133 * @param client identification of the client
1134 * @param app_ctx the `struct NamestoreClient` of @a client
1137 client_disconnect_cb (void *cls,
1138 struct GNUNET_SERVICE_Client *client,
1141 struct NamestoreClient *nc = app_ctx;
1142 struct ZoneIteration *no;
1143 struct CacheOperation *cop;
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "Client %p disconnected\n",
1149 for (struct ZoneMonitor *zm = monitor_head; NULL != zm; zm = zm->next)
1151 struct StoreActivity *san;
1155 GNUNET_CONTAINER_DLL_remove (monitor_head,
1158 if (NULL != zm->task)
1160 GNUNET_SCHEDULER_cancel (zm->task);
1163 if (NULL != zm->sa_wait_warning)
1165 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1166 zm->sa_wait_warning = NULL;
1168 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = san)
1171 if (zm == sa->zm_pos)
1173 sa->zm_pos = zm->next;
1174 /* this may free sa */
1175 continue_store_activity (sa);
1181 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = sa->next)
1185 /* this may free sa */
1186 free_store_activity (sa);
1187 break; /* there can only be one per nc */
1190 while (NULL != (no = nc->op_head))
1192 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1197 for (cop = cop_head; NULL != cop; cop = cop->next)
1205 * Add a client to our list of active clients.
1208 * @param client client to add
1209 * @param mq message queue for @a client
1210 * @return internal namestore client structure for this client
1213 client_connect_cb (void *cls,
1214 struct GNUNET_SERVICE_Client *client,
1215 struct GNUNET_MQ_Handle *mq)
1217 struct NamestoreClient *nc;
1220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1221 "Client %p connected\n",
1223 nc = GNUNET_new (struct NamestoreClient);
1224 nc->client = client;
1231 * Closure for #lookup_it().
1233 struct RecordLookupContext
1249 struct GNUNET_GNSRECORD_Data *nick;
1259 unsigned int res_rd_count;
1269 * Function called by the namestore plugin when we are trying to lookup
1270 * a record as part of #handle_record_lookup(). Merges all results into
1273 * @param cls closure with a `struct RecordLookupContext`
1274 * @param seq unique serial number of the record, MUST NOT BE ZERO
1275 * @param zone_key private key of the zone
1276 * @param label name that is being mapped (at most 255 characters long)
1277 * @param rd_count number of entries in @a rd array
1278 * @param rd array of records with data to store
1281 lookup_it (void *cls,
1283 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
1285 unsigned int rd_count,
1286 const struct GNUNET_GNSRECORD_Data *rd)
1288 struct RecordLookupContext *rlc = cls;
1291 GNUNET_assert (0 != seq);
1292 if (0 != strcmp (label,
1295 rlc->found = GNUNET_YES;
1298 rlc->rd_ser_len = 0;
1299 rlc->res_rd_count = 0;
1303 if ( (NULL != rlc->nick) &&
1304 (0 != strcmp (label,
1305 GNUNET_GNS_EMPTY_LABEL_AT)) )
1308 struct GNUNET_GNSRECORD_Data *rd_res;
1309 unsigned int rdc_res;
1313 rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
1314 merge_with_nick_records (rlc->nick,
1319 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res,
1321 if (rlc->rd_ser_len < 0)
1324 GNUNET_free (rd_res);
1325 rlc->found = GNUNET_NO;
1326 rlc->rd_ser_len = 0;
1329 rlc->res_rd_count = rdc_res;
1330 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1331 if (rlc->rd_ser_len !=
1332 GNUNET_GNSRECORD_records_serialize (rdc_res,
1338 GNUNET_free (rlc->res_rd);
1340 rlc->res_rd_count = 0;
1341 rlc->rd_ser_len = 0;
1342 GNUNET_free (rd_res);
1343 rlc->found = GNUNET_NO;
1346 GNUNET_free (rd_res);
1347 GNUNET_free (rlc->nick);
1352 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1354 if (rlc->rd_ser_len < 0)
1357 rlc->found = GNUNET_NO;
1358 rlc->rd_ser_len = 0;
1361 rlc->res_rd_count = rd_count;
1362 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1363 if (rlc->rd_ser_len !=
1364 GNUNET_GNSRECORD_records_serialize (rd_count,
1370 GNUNET_free (rlc->res_rd);
1372 rlc->res_rd_count = 0;
1373 rlc->rd_ser_len = 0;
1374 rlc->found = GNUNET_NO;
1382 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1384 * @param cls client sending the message
1385 * @param ll_msg message of type `struct LabelLookupMessage`
1386 * @return #GNUNET_OK if @a ll_msg is well-formed
1389 check_record_lookup (void *cls,
1390 const struct LabelLookupMessage *ll_msg)
1396 name_len = ntohl (ll_msg->label_len);
1397 src_size = ntohs (ll_msg->gns_header.header.size);
1398 if (name_len != src_size - sizeof (struct LabelLookupMessage))
1401 return GNUNET_SYSERR;
1403 GNUNET_MQ_check_zero_termination (ll_msg);
1409 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1411 * @param cls client sending the message
1412 * @param ll_msg message of type `struct LabelLookupMessage`
1415 handle_record_lookup (void *cls,
1416 const struct LabelLookupMessage *ll_msg)
1418 struct NamestoreClient *nc = cls;
1419 struct GNUNET_MQ_Envelope *env;
1420 struct LabelLookupResponseMessage *llr_msg;
1421 struct RecordLookupContext rlc;
1422 const char *name_tmp;
1428 name_len = ntohl (ll_msg->label_len);
1429 name_tmp = (const char *) &ll_msg[1];
1430 GNUNET_SERVICE_client_continue (nc->client);
1431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1432 "Received NAMESTORE_RECORD_LOOKUP message for name `%s'\n",
1435 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1436 if (NULL == conv_name)
1438 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1439 "Error converting name `%s'\n",
1441 GNUNET_SERVICE_client_drop (nc->client);
1444 rlc.label = conv_name;
1445 rlc.found = GNUNET_NO;
1446 rlc.res_rd_count = 0;
1449 rlc.nick = get_nick_record (&ll_msg->zone);
1450 res = GSN_database->lookup_records (GSN_database->cls,
1455 GNUNET_free (conv_name);
1456 env = GNUNET_MQ_msg_extra (llr_msg,
1457 name_len + rlc.rd_ser_len,
1458 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
1459 llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
1460 llr_msg->private_key = ll_msg->zone;
1461 llr_msg->name_len = htons (name_len);
1462 llr_msg->rd_count = htons (rlc.res_rd_count);
1463 llr_msg->rd_len = htons (rlc.rd_ser_len);
1464 res_name = (char *) &llr_msg[1];
1465 if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
1466 llr_msg->found = ntohs (GNUNET_YES);
1468 llr_msg->found = ntohs (GNUNET_NO);
1469 GNUNET_memcpy (&llr_msg[1],
1472 GNUNET_memcpy (&res_name[name_len],
1475 GNUNET_MQ_send (nc->mq,
1477 GNUNET_free_non_null (rlc.res_rd);
1482 * Checks a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1484 * @param cls client sending the message
1485 * @param rp_msg message of type `struct RecordStoreMessage`
1486 * @return #GNUNET_OK if @a rp_msg is well-formed
1489 check_record_store (void *cls,
1490 const struct RecordStoreMessage *rp_msg)
1494 size_t msg_size_exp;
1496 const char *name_tmp;
1499 name_len = ntohs (rp_msg->name_len);
1500 msg_size = ntohs (rp_msg->gns_header.header.size);
1501 rd_ser_len = ntohs (rp_msg->rd_len);
1502 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1503 if (msg_size != msg_size_exp)
1506 return GNUNET_SYSERR;
1508 if ( (0 == name_len) ||
1509 (name_len > MAX_NAME_LEN) )
1512 return GNUNET_SYSERR;
1514 name_tmp = (const char *) &rp_msg[1];
1515 if ('\0' != name_tmp[name_len -1])
1518 return GNUNET_SYSERR;
1525 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1527 * @param cls client sending the message
1528 * @param rp_msg message of type `struct RecordStoreMessage`
1531 handle_record_store (void *cls,
1532 const struct RecordStoreMessage *rp_msg)
1534 struct NamestoreClient *nc = cls;
1538 const char *name_tmp;
1541 unsigned int rd_count;
1543 struct StoreActivity *sa;
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546 "Received NAMESTORE_RECORD_STORE message\n");
1547 rid = ntohl (rp_msg->gns_header.r_id);
1548 name_len = ntohs (rp_msg->name_len);
1549 rd_count = ntohs (rp_msg->rd_count);
1550 rd_ser_len = ntohs (rp_msg->rd_len);
1551 GNUNET_break (0 == ntohs (rp_msg->reserved));
1552 name_tmp = (const char *) &rp_msg[1];
1553 rd_ser = &name_tmp[name_len];
1555 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
1558 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
1564 GNUNET_SERVICE_client_drop (nc->client);
1568 /* Extracting and converting private key */
1569 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1570 if (NULL == conv_name)
1572 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1573 "Error converting name `%s'\n",
1575 GNUNET_SERVICE_client_drop (nc->client);
1578 GNUNET_STATISTICS_update (statistics,
1579 "Well-formed store requests received",
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1583 "Creating %u records for name `%s'\n",
1584 (unsigned int) rd_count,
1586 if ( (0 == rd_count) &&
1588 GSN_database->lookup_records (GSN_database->cls,
1589 &rp_msg->private_key,
1594 /* This name does not exist, so cannot be removed */
1595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1596 "Name `%s' does not exist, no deletion required\n",
1602 /* remove "NICK" records, unless this is for the
1603 #GNUNET_GNS_EMPTY_LABEL_AT label */
1604 struct GNUNET_GNSRECORD_Data rd_clean[GNUNET_NZL(rd_count)];
1605 unsigned int rd_clean_off;
1608 for (unsigned int i=0;i<rd_count;i++)
1610 rd_clean[rd_clean_off] = rd[i];
1611 if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
1613 (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
1616 if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
1618 (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) )
1619 cache_nick (&rp_msg->private_key,
1622 res = GSN_database->store_records (GSN_database->cls,
1623 &rp_msg->private_key,
1629 if (GNUNET_OK != res)
1631 /* store not successful, not need to tell monitors */
1632 send_store_response (nc,
1635 GNUNET_SERVICE_client_continue (nc->client);
1636 GNUNET_free (conv_name);
1640 sa = GNUNET_malloc (sizeof (struct StoreActivity) +
1641 ntohs (rp_msg->gns_header.header.size));
1642 GNUNET_CONTAINER_DLL_insert (sa_head,
1646 sa->rsm = (const struct RecordStoreMessage *) &sa[1];
1647 GNUNET_memcpy (&sa[1],
1649 ntohs (rp_msg->gns_header.header.size));
1650 sa->zm_pos = monitor_head;
1651 sa->conv_name = conv_name;
1652 continue_store_activity (sa);
1658 * Context for record remove operations passed from #handle_zone_to_name to
1659 * #handle_zone_to_name_it as closure
1661 struct ZoneToNameCtx
1666 struct NamestoreClient *nc;
1669 * Request id (to be used in the response to the client).
1674 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
1675 * not finding a name for the zone still counts as a 'success' here,
1676 * as this field is about the success of executing the IPC protocol.
1683 * Zone to name iterator
1685 * @param cls struct ZoneToNameCtx *
1686 * @param seq sequence number of the record, MUST NOT BE ZERO
1687 * @param zone_key the zone key
1689 * @param rd_count number of records in @a rd
1690 * @param rd record data
1693 handle_zone_to_name_it (void *cls,
1695 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1697 unsigned int rd_count,
1698 const struct GNUNET_GNSRECORD_Data *rd)
1700 struct ZoneToNameCtx *ztn_ctx = cls;
1701 struct GNUNET_MQ_Envelope *env;
1702 struct ZoneToNameResponseMessage *ztnr_msg;
1710 GNUNET_assert (0 != seq);
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1712 "Found result for zone-to-name lookup: `%s'\n",
1715 name_len = (NULL == name) ? 0 : strlen (name) + 1;
1716 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1721 ztn_ctx->success = GNUNET_SYSERR;
1724 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
1725 if (msg_size >= GNUNET_MAX_MESSAGE_SIZE)
1728 ztn_ctx->success = GNUNET_SYSERR;
1731 env = GNUNET_MQ_msg_extra (ztnr_msg,
1732 name_len + rd_ser_len,
1733 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1734 ztnr_msg->gns_header.header.size = htons (msg_size);
1735 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1736 ztnr_msg->res = htons (res);
1737 ztnr_msg->rd_len = htons (rd_ser_len);
1738 ztnr_msg->rd_count = htons (rd_count);
1739 ztnr_msg->name_len = htons (name_len);
1740 ztnr_msg->zone = *zone_key;
1741 name_tmp = (char *) &ztnr_msg[1];
1742 GNUNET_memcpy (name_tmp,
1745 rd_tmp = &name_tmp[name_len];
1746 GNUNET_assert (rd_ser_len ==
1747 GNUNET_GNSRECORD_records_serialize (rd_count,
1751 ztn_ctx->success = GNUNET_OK;
1752 GNUNET_MQ_send (ztn_ctx->nc->mq,
1758 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
1760 * @param cls client client sending the message
1761 * @param ztn_msg message of type 'struct ZoneToNameMessage'
1764 handle_zone_to_name (void *cls,
1765 const struct ZoneToNameMessage *ztn_msg)
1767 struct NamestoreClient *nc = cls;
1768 struct ZoneToNameCtx ztn_ctx;
1769 struct GNUNET_MQ_Envelope *env;
1770 struct ZoneToNameResponseMessage *ztnr_msg;
1772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1773 "Received ZONE_TO_NAME message\n");
1774 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
1776 ztn_ctx.success = GNUNET_NO;
1777 if (GNUNET_SYSERR ==
1778 GSN_database->zone_to_name (GSN_database->cls,
1780 &ztn_msg->value_zone,
1781 &handle_zone_to_name_it, &ztn_ctx))
1783 /* internal error, hang up instead of signalling something
1784 that might be wrong */
1786 GNUNET_SERVICE_client_drop (nc->client);
1789 if (GNUNET_NO == ztn_ctx.success)
1791 /* no result found, send empty response */
1792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1793 "Found no result for zone-to-name lookup.\n");
1794 env = GNUNET_MQ_msg (ztnr_msg,
1795 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1796 ztnr_msg->gns_header.r_id = ztn_msg->gns_header.r_id;
1797 ztnr_msg->res = htons (GNUNET_NO);
1798 GNUNET_MQ_send (nc->mq,
1801 GNUNET_SERVICE_client_continue (nc->client);
1806 * Context for record remove operations passed from
1807 * #run_zone_iteration_round to #zone_iterate_proc as closure
1809 struct ZoneIterationProcResult
1812 * The zone iteration handle
1814 struct ZoneIteration *zi;
1817 * Number of results left to be returned in this iteration.
1825 * Process results for zone iteration from database
1827 * @param cls struct ZoneIterationProcResult
1828 * @param seq sequence number of the record, MUST NOT BE ZERO
1829 * @param zone_key the zone key
1831 * @param rd_count number of records for this name
1832 * @param rd record data
1835 zone_iterate_proc (void *cls,
1837 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1839 unsigned int rd_count,
1840 const struct GNUNET_GNSRECORD_Data *rd)
1842 struct ZoneIterationProcResult *proc = cls;
1843 int do_refresh_block;
1845 GNUNET_assert (0 != seq);
1846 if ( (NULL == zone_key) &&
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1850 "Iteration done\n");
1853 if ( (NULL == zone_key) ||
1856 /* what is this!? should never happen */
1860 if (0 == proc->limit)
1862 /* what is this!? should never happen */
1867 proc->zi->seq = seq;
1868 send_lookup_response (proc->zi->nc,
1869 proc->zi->request_id,
1876 do_refresh_block = GNUNET_NO;
1877 for (unsigned int i=0;i<rd_count;i++)
1878 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
1880 do_refresh_block = GNUNET_YES;
1883 if (GNUNET_YES == do_refresh_block)
1884 refresh_block (NULL,
1895 * Perform the next round of the zone iteration.
1897 * @param zi zone iterator to process
1898 * @param limit number of results to return in one pass
1901 run_zone_iteration_round (struct ZoneIteration *zi,
1904 struct ZoneIterationProcResult proc;
1905 struct GNUNET_TIME_Absolute start;
1906 struct GNUNET_TIME_Relative duration;
1911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1912 "Asked to return up to %llu records at position %llu\n",
1913 (unsigned long long) limit,
1914 (unsigned long long) zi->seq);
1917 start = GNUNET_TIME_absolute_get ();
1918 GNUNET_break (GNUNET_SYSERR !=
1919 GSN_database->iterate_records (GSN_database->cls,
1920 (0 == GNUNET_is_zero (&zi->zone))
1927 duration = GNUNET_TIME_absolute_get_duration (start);
1928 duration = GNUNET_TIME_relative_divide (duration,
1929 limit - proc.limit);
1930 GNUNET_STATISTICS_set (statistics,
1931 "NAMESTORE iteration delay (μs/record)",
1932 duration.rel_value_us,
1934 if (0 == proc.limit)
1935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1936 "Returned %llu results, more results available\n",
1937 (unsigned long long) limit);
1938 zi->send_end = (0 != proc.limit);
1939 if (0 == zi->cache_ops)
1940 zone_iteration_done_client_continue (zi);
1945 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1947 * @param cls the client sending the message
1948 * @param zis_msg message from the client
1951 handle_iteration_start (void *cls,
1952 const struct ZoneIterationStartMessage *zis_msg)
1954 struct NamestoreClient *nc = cls;
1955 struct ZoneIteration *zi;
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1958 "Received ZONE_ITERATION_START message\n");
1959 zi = GNUNET_new (struct ZoneIteration);
1960 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1963 zi->zone = zis_msg->zone;
1965 GNUNET_CONTAINER_DLL_insert (nc->op_head,
1968 run_zone_iteration_round (zi,
1974 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1976 * @param cls the client sending the message
1977 * @param zis_msg message from the client
1980 handle_iteration_stop (void *cls,
1981 const struct ZoneIterationStopMessage *zis_msg)
1983 struct NamestoreClient *nc = cls;
1984 struct ZoneIteration *zi;
1987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1988 "Received ZONE_ITERATION_STOP message\n");
1989 rid = ntohl (zis_msg->gns_header.r_id);
1990 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1991 if (zi->request_id == rid)
1996 GNUNET_SERVICE_client_drop (nc->client);
1999 GNUNET_CONTAINER_DLL_remove (nc->op_head,
2003 GNUNET_SERVICE_client_continue (nc->client);
2008 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
2010 * @param cls the client sending the message
2011 * @param message message from the client
2014 handle_iteration_next (void *cls,
2015 const struct ZoneIterationNextMessage *zis_msg)
2017 struct NamestoreClient *nc = cls;
2018 struct ZoneIteration *zi;
2022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2023 "Received ZONE_ITERATION_NEXT message\n");
2024 GNUNET_STATISTICS_update (statistics,
2025 "Iteration NEXT messages received",
2028 rid = ntohl (zis_msg->gns_header.r_id);
2029 limit = GNUNET_ntohll (zis_msg->limit);
2030 for (zi = nc->op_head; NULL != zi; zi = zi->next)
2031 if (zi->request_id == rid)
2036 GNUNET_SERVICE_client_drop (nc->client);
2039 run_zone_iteration_round (zi,
2045 * Function called when the monitor is ready for more data, and we
2046 * should thus unblock PUT operations that were blocked on the
2047 * monitor not being ready.
2050 monitor_unblock (struct ZoneMonitor *zm)
2052 struct StoreActivity *sa = sa_head;
2054 while ( (NULL != sa) &&
2055 (zm->limit > zm->iteration_cnt) )
2057 struct StoreActivity *sn = sa->next;
2059 if (sa->zm_pos == zm)
2060 continue_store_activity (sa);
2063 if (zm->limit > zm->iteration_cnt)
2065 zm->sa_waiting = GNUNET_NO;
2066 if (NULL != zm->sa_wait_warning)
2068 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
2069 zm->sa_wait_warning = NULL;
2072 else if (GNUNET_YES == zm->sa_waiting)
2074 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
2075 if (NULL != zm->sa_wait_warning)
2076 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
2077 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
2085 * Send 'sync' message to zone monitor, we're now in sync.
2087 * @param zm monitor that is now in sync
2090 monitor_sync (struct ZoneMonitor *zm)
2092 struct GNUNET_MQ_Envelope *env;
2093 struct GNUNET_MessageHeader *sync;
2095 env = GNUNET_MQ_msg (sync,
2096 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
2097 GNUNET_MQ_send (zm->nc->mq,
2099 /* mark iteration done */
2100 zm->in_first_iteration = GNUNET_NO;
2101 zm->iteration_cnt = 0;
2102 if ( (zm->limit > 0) &&
2104 monitor_unblock (zm);
2109 * Obtain the next datum during the zone monitor's zone initial iteration.
2111 * @param cls zone monitor that does its initial iteration
2114 monitor_iteration_next (void *cls);
2118 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
2120 * @param cls a 'struct ZoneMonitor *' with information about the monitor
2121 * @param seq sequence number of the record, MUST NOT BE ZERO
2122 * @param zone_key zone key of the zone
2124 * @param rd_count number of records in @a rd
2125 * @param rd array of records
2128 monitor_iterate_cb (void *cls,
2130 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
2132 unsigned int rd_count,
2133 const struct GNUNET_GNSRECORD_Data *rd)
2135 struct ZoneMonitor *zm = cls;
2137 GNUNET_assert (0 != seq);
2139 GNUNET_assert (NULL != name);
2140 GNUNET_STATISTICS_update (statistics,
2141 "Monitor notifications sent",
2145 zm->iteration_cnt--;
2146 send_lookup_response (zm->nc,
2152 if ( (0 == zm->iteration_cnt) &&
2155 /* We are done with the current iteration batch, AND the
2156 client would right now accept more, so go again! */
2157 GNUNET_assert (NULL == zm->task);
2158 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2165 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
2167 * @param cls the client sending the message
2168 * @param zis_msg message from the client
2171 handle_monitor_start (void *cls,
2172 const struct ZoneMonitorStartMessage *zis_msg)
2174 struct NamestoreClient *nc = cls;
2175 struct ZoneMonitor *zm;
2177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2178 "Received ZONE_MONITOR_START message\n");
2179 zm = GNUNET_new (struct ZoneMonitor);
2181 zm->zone = zis_msg->zone;
2183 zm->in_first_iteration = (GNUNET_YES == ntohl (zis_msg->iterate_first));
2184 GNUNET_CONTAINER_DLL_insert (monitor_head,
2187 GNUNET_SERVICE_client_mark_monitor (nc->client);
2188 GNUNET_SERVICE_client_continue (nc->client);
2189 GNUNET_notification_context_add (monitor_nc,
2191 if (zm->in_first_iteration)
2192 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2200 * Obtain the next datum during the zone monitor's zone initial iteration.
2202 * @param cls zone monitor that does its initial iteration
2205 monitor_iteration_next (void *cls)
2207 struct ZoneMonitor *zm = cls;
2211 GNUNET_assert (0 == zm->iteration_cnt);
2213 zm->iteration_cnt = zm->limit / 2; /* leave half for monitor events */
2215 zm->iteration_cnt = zm->limit; /* use it all */
2216 ret = GSN_database->iterate_records (GSN_database->cls,
2217 (0 == GNUNET_is_zero (&zm->zone))
2222 &monitor_iterate_cb,
2224 if (GNUNET_SYSERR == ret)
2226 GNUNET_SERVICE_client_drop (zm->nc->client);
2229 if (GNUNET_NO == ret)
2239 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT message
2241 * @param cls the client sending the message
2242 * @param nm message from the client
2245 handle_monitor_next (void *cls,
2246 const struct ZoneMonitorNextMessage *nm)
2248 struct NamestoreClient *nc = cls;
2249 struct ZoneMonitor *zm;
2252 inc = GNUNET_ntohll (nm->limit);
2253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2254 "Received ZONE_MONITOR_NEXT message with limit %llu\n",
2255 (unsigned long long) inc);
2256 for (zm = monitor_head; NULL != zm; zm = zm->next)
2262 GNUNET_SERVICE_client_drop (nc->client);
2265 GNUNET_SERVICE_client_continue (nc->client);
2266 if (zm->limit + inc < zm->limit)
2269 GNUNET_SERVICE_client_drop (nc->client);
2273 if ( (zm->in_first_iteration) &&
2274 (zm->limit == inc) )
2276 /* We are still iterating, and the previous iteration must
2277 have stopped due to the client's limit, so continue it! */
2278 GNUNET_assert (NULL == zm->task);
2279 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2282 GNUNET_assert (zm->iteration_cnt <= zm->limit);
2283 if ( (zm->limit > zm->iteration_cnt) &&
2286 monitor_unblock (zm);
2288 else if (GNUNET_YES == zm->sa_waiting)
2290 if (NULL != zm->sa_wait_warning)
2291 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
2292 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
2293 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
2301 * Process namestore requests.
2303 * @param cls closure
2304 * @param cfg configuration to use
2305 * @param service the initialized service
2309 const struct GNUNET_CONFIGURATION_Handle *cfg,
2310 struct GNUNET_SERVICE_Handle *service)
2316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2317 "Starting namestore service\n");
2318 cache_keys = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2321 disable_namecache = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2325 monitor_nc = GNUNET_notification_context_create (1);
2326 if (GNUNET_YES != disable_namecache)
2328 namecache = GNUNET_NAMECACHE_connect (cfg);
2329 GNUNET_assert (NULL != namecache);
2331 /* Loading database plugin */
2333 GNUNET_CONFIGURATION_get_value_string (cfg,
2337 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2338 "No database backend configured\n");
2340 GNUNET_asprintf (&db_lib_name,
2341 "libgnunet_plugin_namestore_%s",
2343 GSN_database = GNUNET_PLUGIN_load (db_lib_name,
2345 GNUNET_free (database);
2346 statistics = GNUNET_STATISTICS_create ("namestore",
2348 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
2350 if (NULL == GSN_database)
2352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2353 "Could not load database backend `%s'\n",
2355 GNUNET_SCHEDULER_shutdown ();
2362 * Define "main" method using service macro.
2366 GNUNET_SERVICE_OPTION_NONE,
2369 &client_disconnect_cb,
2371 GNUNET_MQ_hd_var_size (record_store,
2372 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE,
2373 struct RecordStoreMessage,
2375 GNUNET_MQ_hd_var_size (record_lookup,
2376 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP,
2377 struct LabelLookupMessage,
2379 GNUNET_MQ_hd_fixed_size (zone_to_name,
2380 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME,
2381 struct ZoneToNameMessage,
2383 GNUNET_MQ_hd_fixed_size (iteration_start,
2384 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START,
2385 struct ZoneIterationStartMessage,
2387 GNUNET_MQ_hd_fixed_size (iteration_next,
2388 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT,
2389 struct ZoneIterationNextMessage,
2391 GNUNET_MQ_hd_fixed_size (iteration_stop,
2392 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP,
2393 struct ZoneIterationStopMessage,
2395 GNUNET_MQ_hd_fixed_size (monitor_start,
2396 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START,
2397 struct ZoneMonitorStartMessage,
2399 GNUNET_MQ_hd_fixed_size (monitor_next,
2400 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT,
2401 struct ZoneMonitorNextMessage,
2403 GNUNET_MQ_handler_end ());
2406 /* end of gnunet-service-namestore.c */