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
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;
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 == memcmp (zone,
564 GNUNET_free_non_null (oldest->rd);
565 oldest->zone = *zone;
566 oldest->rd = GNUNET_malloc (sizeof (*nick) +
569 oldest->rd->data = &oldest->rd[1];
570 memcpy (&oldest->rd[1],
573 oldest->last_used = GNUNET_TIME_absolute_get ();
578 * Return the NICK record for the zone (if it exists).
580 * @param zone private key for the zone to look for nick
581 * @return NULL if no NICK record was found
583 static struct GNUNET_GNSRECORD_Data *
584 get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
586 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
587 struct GNUNET_GNSRECORD_Data *nick;
590 /* check cache first */
591 for (unsigned int i=0;i<NC_SIZE;i++)
593 struct NickCache *pos = &nick_cache[i];
594 if ( (NULL != pos->rd) &&
599 nick = GNUNET_malloc (sizeof (*nick) +
602 nick->data = &nick[1];
606 pos->last_used = GNUNET_TIME_absolute_get ();
612 res = GSN_database->lookup_records (GSN_database->cls,
614 GNUNET_GNS_EMPTY_LABEL_AT,
617 if ( (GNUNET_OK != res) ||
620 GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
622 "No nick name set for zone `%s'\n",
623 GNUNET_GNSRECORD_z2s (&pub));
635 * Merge the nick record @a nick_rd with the rest of the
636 * record set given in @a rd2. Store the result in @a rdc_res
637 * and @a rd_res. The @a nick_rd's expiration time is set to
638 * the maximum expiration time of all of the records in @a rd2.
640 * @param nick_rd the nick record to integrate
641 * @param rd2_length length of the @a rd2 array
642 * @param rd2 array of records
643 * @param rdc_res[out] length of the resulting @a rd_res array
644 * @param rd_res[out] set to an array of records,
645 * including @a nick_rd and @a rd2;
646 * all of the variable-size 'data' fields in @a rd2 are
647 * allocated in the same chunk of memory!
650 merge_with_nick_records (const struct GNUNET_GNSRECORD_Data *nick_rd,
651 unsigned int rd2_length,
652 const struct GNUNET_GNSRECORD_Data *rd2,
653 unsigned int *rdc_res,
654 struct GNUNET_GNSRECORD_Data **rd_res)
656 uint64_t latest_expiration;
660 struct GNUNET_GNSRECORD_Data *target;
662 (*rdc_res) = 1 + rd2_length;
663 if (0 == 1 + rd2_length)
669 req = sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd->data_size;
670 for (unsigned int i=0; i<rd2_length; i++)
672 const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
674 if (req + sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size < req)
680 req += sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size;
682 target = GNUNET_malloc (req);
684 data = (char *) &target[1 + rd2_length];
686 latest_expiration = 0;
687 for (unsigned int i=0;i<rd2_length;i++)
689 const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
691 if (0 != (orig->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
693 if ((GNUNET_TIME_absolute_get().abs_value_us + orig->expiration_time) >
695 latest_expiration = orig->expiration_time;
697 else if (orig->expiration_time > latest_expiration)
698 latest_expiration = orig->expiration_time;
700 target[i].data = (void *) &data[data_offset];
701 GNUNET_memcpy (&data[data_offset],
704 data_offset += orig->data_size;
707 target[rd2_length] = *nick_rd;
708 target[rd2_length].expiration_time = latest_expiration;
709 target[rd2_length].data = (void *) &data[data_offset];
710 GNUNET_memcpy (&data[data_offset],
713 data_offset += nick_rd->data_size;
714 GNUNET_assert (req ==
715 (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
720 * Generate a `struct LookupNameResponseMessage` and send it to the
721 * given client using the given notification context.
723 * @param nc client to unicast to
724 * @param request_id request ID to use
725 * @param zone_key zone key of the zone
727 * @param rd_count number of records in @a rd
728 * @param rd array of records
731 send_lookup_response (struct NamestoreClient *nc,
733 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
735 unsigned int rd_count,
736 const struct GNUNET_GNSRECORD_Data *rd)
738 struct GNUNET_MQ_Envelope *env;
739 struct RecordResultMessage *zir_msg;
740 struct GNUNET_GNSRECORD_Data *nick;
741 struct GNUNET_GNSRECORD_Data *res;
742 unsigned int res_count;
748 nick = get_nick_record (zone_key);
750 GNUNET_GNSRECORD_records_get_size (rd_count,
753 if ( (NULL != nick) &&
755 GNUNET_GNS_EMPTY_LABEL_AT)))
757 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
758 merge_with_nick_records (nick,
767 res_count = rd_count;
768 res = (struct GNUNET_GNSRECORD_Data *) rd;
772 GNUNET_GNSRECORD_records_get_size (res_count,
776 name_len = strlen (name) + 1;
777 rd_ser_len = GNUNET_GNSRECORD_records_get_size (res_count,
782 GNUNET_SERVICE_client_drop (nc->client);
785 if (((size_t) rd_ser_len) >= UINT16_MAX - name_len - sizeof (*zir_msg))
788 GNUNET_SERVICE_client_drop (nc->client);
791 env = GNUNET_MQ_msg_extra (zir_msg,
792 name_len + rd_ser_len,
793 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
794 zir_msg->gns_header.r_id = htonl (request_id);
795 zir_msg->name_len = htons (name_len);
796 zir_msg->rd_count = htons (res_count);
797 zir_msg->rd_len = htons ((uint16_t) rd_ser_len);
798 zir_msg->private_key = *zone_key;
799 name_tmp = (char *) &zir_msg[1];
800 GNUNET_memcpy (name_tmp,
803 rd_ser = &name_tmp[name_len];
804 GNUNET_assert (rd_ser_len ==
805 GNUNET_GNSRECORD_records_serialize (res_count,
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Sending RECORD_RESULT message with %u records\n",
812 GNUNET_STATISTICS_update (statistics,
813 "Record sets sent to clients",
816 GNUNET_MQ_send (nc->mq,
824 * Send response to the store request to the client.
826 * @param client client to talk to
827 * @param res status of the operation
828 * @param rid client's request ID
831 send_store_response (struct NamestoreClient *nc,
835 struct GNUNET_MQ_Envelope *env;
836 struct RecordStoreResponseMessage *rcr_msg;
838 GNUNET_assert (NULL != nc);
839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
840 "Sending RECORD_STORE_RESPONSE message\n");
841 GNUNET_STATISTICS_update (statistics,
842 "Store requests completed",
845 env = GNUNET_MQ_msg (rcr_msg,
846 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
847 rcr_msg->gns_header.r_id = htonl (rid);
848 rcr_msg->op_result = htonl (res);
849 GNUNET_MQ_send (nc->mq,
855 * Function called once we are done with the zone iteration and
856 * allow the zone iteration client to send us more messages.
858 * @param zi zone iteration we are processing
861 zone_iteration_done_client_continue (struct ZoneIteration *zi)
863 struct GNUNET_MQ_Envelope *env;
864 struct GNUNET_NAMESTORE_Header *em;
866 GNUNET_SERVICE_client_continue (zi->nc->client);
869 /* send empty response to indicate end of list */
870 env = GNUNET_MQ_msg (em,
871 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END);
872 em->r_id = htonl (zi->request_id);
873 GNUNET_MQ_send (zi->nc->mq,
876 GNUNET_CONTAINER_DLL_remove (zi->nc->op_head,
884 * Cache operation complete, clean up.
886 * @param cls the `struct CacheOperation`
887 * @param success success
888 * @param emsg error messages
891 finish_cache_operation (void *cls,
895 struct CacheOperation *cop = cls;
896 struct ZoneIteration *zi;
899 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
900 _("Failed to replicate block in namecache: %s\n"),
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "CACHE operation completed\n");
905 GNUNET_CONTAINER_DLL_remove (cop_head,
909 send_store_response (cop->nc,
912 if (NULL != (zi = cop->zi))
915 if (0 == zi->cache_ops)
917 /* unchoke zone iteration, cache has caught up */
918 zone_iteration_done_client_continue (zi);
926 * We just touched the plaintext information about a name in our zone;
927 * refresh the corresponding (encrypted) block in the namecache.
929 * @param nc client responsible for the request, can be NULL
930 * @param zi zone iteration response for the request, can be NULL
931 * @param rid request ID of the client
932 * @param zone_key private key of the zone
933 * @param name label for the records
934 * @param rd_count number of records
935 * @param rd records stored under the given @a name
938 refresh_block (struct NamestoreClient *nc,
939 struct ZoneIteration *zi,
941 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
943 unsigned int rd_count,
944 const struct GNUNET_GNSRECORD_Data *rd)
946 struct GNUNET_GNSRECORD_Block *block;
947 struct CacheOperation *cop;
948 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
949 struct GNUNET_GNSRECORD_Data *nick;
950 struct GNUNET_GNSRECORD_Data *res;
951 unsigned int res_count;
952 struct GNUNET_TIME_Absolute exp_time;
954 nick = get_nick_record (zone_key);
955 res_count = rd_count;
956 res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
959 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
960 merge_with_nick_records (nick,
969 send_store_response (nc,
972 return; /* no data, no need to update cache */
974 if (GNUNET_YES == disable_namecache)
976 GNUNET_STATISTICS_update (statistics,
977 "Namecache updates skipped (NC disabled)",
981 send_store_response (nc,
986 exp_time = GNUNET_GNSRECORD_record_get_expiration_time (res_count,
989 block = GNUNET_GNSRECORD_block_create2 (zone_key,
995 block = GNUNET_GNSRECORD_block_create (zone_key,
1000 GNUNET_assert (NULL != block);
1001 GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1004 "Caching block for label `%s' with %u records and expiration %s in zone `%s' in namecache\n",
1007 GNUNET_STRINGS_absolute_time_to_string (exp_time),
1008 GNUNET_GNSRECORD_z2s (&pkey));
1009 GNUNET_STATISTICS_update (statistics,
1010 "Namecache updates pushed",
1013 cop = GNUNET_new (struct CacheOperation);
1019 GNUNET_CONTAINER_DLL_insert (cop_head,
1022 cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
1024 &finish_cache_operation,
1026 GNUNET_free (block);
1031 * Print a warning that one of our monitors is no longer reacting.
1033 * @param cls a `struct ZoneMonitor` to warn about
1036 warn_monitor_slow (void *cls)
1038 struct ZoneMonitor *zm = cls;
1040 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1041 "No response from monitor since %s\n",
1042 GNUNET_STRINGS_absolute_time_to_string (zm->sa_waiting_start));
1043 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
1050 * Continue processing the @a sa.
1052 * @param sa store activity to process
1055 continue_store_activity (struct StoreActivity *sa)
1057 const struct RecordStoreMessage *rp_msg = sa->rsm;
1058 unsigned int rd_count;
1062 const char *name_tmp;
1065 rid = ntohl (rp_msg->gns_header.r_id);
1066 name_len = ntohs (rp_msg->name_len);
1067 rd_count = ntohs (rp_msg->rd_count);
1068 rd_ser_len = ntohs (rp_msg->rd_len);
1069 name_tmp = (const char *) &rp_msg[1];
1070 rd_ser = &name_tmp[name_len];
1072 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
1074 /* We did this before, must succeed again */
1075 GNUNET_assert (GNUNET_OK ==
1076 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
1081 for (struct ZoneMonitor *zm = sa->zm_pos;
1085 if ( (0 != memcmp (&rp_msg->private_key,
1087 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) &&
1088 (0 != memcmp (&zm->zone,
1090 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
1092 sa->zm_pos = zm->next; /* not interesting to this monitor */
1095 if (zm->limit == zm->iteration_cnt)
1097 zm->sa_waiting = GNUNET_YES;
1098 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
1099 if (NULL != zm->sa_wait_warning)
1100 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1101 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
1104 return; /* blocked on zone monitor */
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1107 "Notifying monitor about changes under label `%s'\n",
1110 send_lookup_response (zm->nc,
1112 &rp_msg->private_key,
1116 sa->zm_pos = zm->next;
1118 /* great, done with the monitors, unpack (again) for refresh_block operation */
1119 refresh_block (sa->nc,
1122 &rp_msg->private_key,
1127 GNUNET_SERVICE_client_continue (sa->nc->client);
1128 free_store_activity (sa);
1133 * Called whenever a client is disconnected.
1134 * Frees our resources associated with that client.
1136 * @param cls closure
1137 * @param client identification of the client
1138 * @param app_ctx the `struct NamestoreClient` of @a client
1141 client_disconnect_cb (void *cls,
1142 struct GNUNET_SERVICE_Client *client,
1145 struct NamestoreClient *nc = app_ctx;
1146 struct ZoneIteration *no;
1147 struct CacheOperation *cop;
1150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1151 "Client %p disconnected\n",
1153 for (struct ZoneMonitor *zm = monitor_head; NULL != zm; zm = zm->next)
1155 struct StoreActivity *san;
1159 GNUNET_CONTAINER_DLL_remove (monitor_head,
1162 if (NULL != zm->task)
1164 GNUNET_SCHEDULER_cancel (zm->task);
1167 if (NULL != zm->sa_wait_warning)
1169 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1170 zm->sa_wait_warning = NULL;
1172 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = san)
1175 if (zm == sa->zm_pos)
1177 sa->zm_pos = zm->next;
1178 /* this may free sa */
1179 continue_store_activity (sa);
1185 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = sa->next)
1189 /* this may free sa */
1190 free_store_activity (sa);
1191 break; /* there can only be one per nc */
1194 while (NULL != (no = nc->op_head))
1196 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1201 for (cop = cop_head; NULL != cop; cop = cop->next)
1209 * Add a client to our list of active clients.
1212 * @param client client to add
1213 * @param mq message queue for @a client
1214 * @return internal namestore client structure for this client
1217 client_connect_cb (void *cls,
1218 struct GNUNET_SERVICE_Client *client,
1219 struct GNUNET_MQ_Handle *mq)
1221 struct NamestoreClient *nc;
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "Client %p connected\n",
1227 nc = GNUNET_new (struct NamestoreClient);
1228 nc->client = client;
1235 * Closure for #lookup_it().
1237 struct RecordLookupContext
1253 struct GNUNET_GNSRECORD_Data *nick;
1263 unsigned int res_rd_count;
1275 * @param seq sequence number of the record
1278 lookup_it (void *cls,
1280 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
1282 unsigned int rd_count,
1283 const struct GNUNET_GNSRECORD_Data *rd)
1285 struct RecordLookupContext *rlc = cls;
1289 if (0 != strcmp (label,
1292 rlc->found = GNUNET_YES;
1295 rlc->rd_ser_len = 0;
1296 rlc->res_rd_count = 0;
1300 if ( (NULL != rlc->nick) &&
1301 (0 != strcmp (label,
1302 GNUNET_GNS_EMPTY_LABEL_AT)) )
1305 struct GNUNET_GNSRECORD_Data *rd_res;
1306 unsigned int rdc_res;
1310 rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
1311 merge_with_nick_records (rlc->nick,
1316 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res,
1318 if (rlc->rd_ser_len < 0)
1321 GNUNET_free (rd_res);
1322 rlc->found = GNUNET_NO;
1323 rlc->rd_ser_len = 0;
1326 rlc->res_rd_count = rdc_res;
1327 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1328 if (rlc->rd_ser_len !=
1329 GNUNET_GNSRECORD_records_serialize (rdc_res,
1335 GNUNET_free (rlc->res_rd);
1337 rlc->res_rd_count = 0;
1338 rlc->rd_ser_len = 0;
1339 GNUNET_free (rd_res);
1340 rlc->found = GNUNET_NO;
1343 GNUNET_free (rd_res);
1344 GNUNET_free (rlc->nick);
1349 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1351 if (rlc->rd_ser_len < 0)
1354 rlc->found = GNUNET_NO;
1355 rlc->rd_ser_len = 0;
1358 rlc->res_rd_count = rd_count;
1359 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1360 if (rlc->rd_ser_len !=
1361 GNUNET_GNSRECORD_records_serialize (rd_count,
1367 GNUNET_free (rlc->res_rd);
1369 rlc->res_rd_count = 0;
1370 rlc->rd_ser_len = 0;
1371 rlc->found = GNUNET_NO;
1379 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1381 * @param cls client sending the message
1382 * @param ll_msg message of type `struct LabelLookupMessage`
1383 * @return #GNUNET_OK if @a ll_msg is well-formed
1386 check_record_lookup (void *cls,
1387 const struct LabelLookupMessage *ll_msg)
1393 name_len = ntohl (ll_msg->label_len);
1394 src_size = ntohs (ll_msg->gns_header.header.size);
1395 if (name_len != src_size - sizeof (struct LabelLookupMessage))
1398 return GNUNET_SYSERR;
1400 GNUNET_MQ_check_zero_termination (ll_msg);
1406 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1408 * @param cls client sending the message
1409 * @param ll_msg message of type `struct LabelLookupMessage`
1412 handle_record_lookup (void *cls,
1413 const struct LabelLookupMessage *ll_msg)
1415 struct NamestoreClient *nc = cls;
1416 struct GNUNET_MQ_Envelope *env;
1417 struct LabelLookupResponseMessage *llr_msg;
1418 struct RecordLookupContext rlc;
1419 const char *name_tmp;
1425 name_len = ntohl (ll_msg->label_len);
1426 name_tmp = (const char *) &ll_msg[1];
1427 GNUNET_SERVICE_client_continue (nc->client);
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Received NAMESTORE_RECORD_LOOKUP message for name `%s'\n",
1432 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1433 if (NULL == conv_name)
1435 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1436 "Error converting name `%s'\n",
1438 GNUNET_SERVICE_client_drop (nc->client);
1441 rlc.label = conv_name;
1442 rlc.found = GNUNET_NO;
1443 rlc.res_rd_count = 0;
1446 rlc.nick = get_nick_record (&ll_msg->zone);
1447 res = GSN_database->lookup_records (GSN_database->cls,
1452 GNUNET_free (conv_name);
1453 env = GNUNET_MQ_msg_extra (llr_msg,
1454 name_len + rlc.rd_ser_len,
1455 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
1456 llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
1457 llr_msg->private_key = ll_msg->zone;
1458 llr_msg->name_len = htons (name_len);
1459 llr_msg->rd_count = htons (rlc.res_rd_count);
1460 llr_msg->rd_len = htons (rlc.rd_ser_len);
1461 res_name = (char *) &llr_msg[1];
1462 if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
1463 llr_msg->found = ntohs (GNUNET_YES);
1465 llr_msg->found = ntohs (GNUNET_NO);
1466 GNUNET_memcpy (&llr_msg[1],
1469 GNUNET_memcpy (&res_name[name_len],
1472 GNUNET_MQ_send (nc->mq,
1474 GNUNET_free_non_null (rlc.res_rd);
1479 * Checks a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1481 * @param cls client sending the message
1482 * @param rp_msg message of type `struct RecordStoreMessage`
1483 * @return #GNUNET_OK if @a rp_msg is well-formed
1486 check_record_store (void *cls,
1487 const struct RecordStoreMessage *rp_msg)
1491 size_t msg_size_exp;
1493 const char *name_tmp;
1496 name_len = ntohs (rp_msg->name_len);
1497 msg_size = ntohs (rp_msg->gns_header.header.size);
1498 rd_ser_len = ntohs (rp_msg->rd_len);
1499 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1500 if (msg_size != msg_size_exp)
1503 return GNUNET_SYSERR;
1505 if ( (0 == name_len) ||
1506 (name_len > MAX_NAME_LEN) )
1509 return GNUNET_SYSERR;
1511 name_tmp = (const char *) &rp_msg[1];
1512 if ('\0' != name_tmp[name_len -1])
1515 return GNUNET_SYSERR;
1522 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1524 * @param cls client sending the message
1525 * @param rp_msg message of type `struct RecordStoreMessage`
1528 handle_record_store (void *cls,
1529 const struct RecordStoreMessage *rp_msg)
1531 struct NamestoreClient *nc = cls;
1535 const char *name_tmp;
1538 unsigned int rd_count;
1540 struct StoreActivity *sa;
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "Received NAMESTORE_RECORD_STORE message\n");
1544 rid = ntohl (rp_msg->gns_header.r_id);
1545 name_len = ntohs (rp_msg->name_len);
1546 rd_count = ntohs (rp_msg->rd_count);
1547 rd_ser_len = ntohs (rp_msg->rd_len);
1548 GNUNET_break (0 == ntohs (rp_msg->reserved));
1549 name_tmp = (const char *) &rp_msg[1];
1550 rd_ser = &name_tmp[name_len];
1552 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
1555 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
1561 GNUNET_SERVICE_client_drop (nc->client);
1565 /* Extracting and converting private key */
1566 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1567 if (NULL == conv_name)
1569 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1570 "Error converting name `%s'\n",
1572 GNUNET_SERVICE_client_drop (nc->client);
1575 GNUNET_STATISTICS_update (statistics,
1576 "Well-formed store requests received",
1579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1580 "Creating %u records for name `%s'\n",
1581 (unsigned int) rd_count,
1583 if ( (0 == rd_count) &&
1585 GSN_database->lookup_records (GSN_database->cls,
1586 &rp_msg->private_key,
1591 /* This name does not exist, so cannot be removed */
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1593 "Name `%s' does not exist, no deletion required\n",
1599 /* remove "NICK" records, unless this is for the
1600 #GNUNET_GNS_EMPTY_LABEL_AT label */
1601 struct GNUNET_GNSRECORD_Data rd_clean[GNUNET_NZL(rd_count)];
1602 unsigned int rd_clean_off;
1605 for (unsigned int i=0;i<rd_count;i++)
1607 rd_clean[rd_clean_off] = rd[i];
1608 if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
1610 (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
1613 if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
1615 (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) )
1616 cache_nick (&rp_msg->private_key,
1619 res = GSN_database->store_records (GSN_database->cls,
1620 &rp_msg->private_key,
1626 if (GNUNET_OK != res)
1628 /* store not successful, not need to tell monitors */
1629 send_store_response (nc,
1632 GNUNET_SERVICE_client_continue (nc->client);
1633 GNUNET_free (conv_name);
1637 sa = GNUNET_malloc (sizeof (struct StoreActivity) +
1638 ntohs (rp_msg->gns_header.header.size));
1639 GNUNET_CONTAINER_DLL_insert (sa_head,
1643 sa->rsm = (const struct RecordStoreMessage *) &sa[1];
1644 GNUNET_memcpy (&sa[1],
1646 ntohs (rp_msg->gns_header.header.size));
1647 sa->zm_pos = monitor_head;
1648 sa->conv_name = conv_name;
1649 continue_store_activity (sa);
1655 * Context for record remove operations passed from #handle_zone_to_name to
1656 * #handle_zone_to_name_it as closure
1658 struct ZoneToNameCtx
1663 struct NamestoreClient *nc;
1666 * Request id (to be used in the response to the client).
1671 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
1672 * not finding a name for the zone still counts as a 'success' here,
1673 * as this field is about the success of executing the IPC protocol.
1680 * Zone to name iterator
1682 * @param cls struct ZoneToNameCtx *
1683 * @param seq sequence number of the record
1684 * @param zone_key the zone key
1686 * @param rd_count number of records in @a rd
1687 * @param rd record data
1690 handle_zone_to_name_it (void *cls,
1692 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1694 unsigned int rd_count,
1695 const struct GNUNET_GNSRECORD_Data *rd)
1697 struct ZoneToNameCtx *ztn_ctx = cls;
1698 struct GNUNET_MQ_Envelope *env;
1699 struct ZoneToNameResponseMessage *ztnr_msg;
1708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1709 "Found result for zone-to-name lookup: `%s'\n",
1712 name_len = (NULL == name) ? 0 : strlen (name) + 1;
1713 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1718 ztn_ctx->success = GNUNET_SYSERR;
1721 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
1722 if (msg_size >= GNUNET_MAX_MESSAGE_SIZE)
1725 ztn_ctx->success = GNUNET_SYSERR;
1728 env = GNUNET_MQ_msg_extra (ztnr_msg,
1729 name_len + rd_ser_len,
1730 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1731 ztnr_msg->gns_header.header.size = htons (msg_size);
1732 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1733 ztnr_msg->res = htons (res);
1734 ztnr_msg->rd_len = htons (rd_ser_len);
1735 ztnr_msg->rd_count = htons (rd_count);
1736 ztnr_msg->name_len = htons (name_len);
1737 ztnr_msg->zone = *zone_key;
1738 name_tmp = (char *) &ztnr_msg[1];
1739 GNUNET_memcpy (name_tmp,
1742 rd_tmp = &name_tmp[name_len];
1743 GNUNET_assert (rd_ser_len ==
1744 GNUNET_GNSRECORD_records_serialize (rd_count,
1748 ztn_ctx->success = GNUNET_OK;
1749 GNUNET_MQ_send (ztn_ctx->nc->mq,
1755 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
1757 * @param cls client client sending the message
1758 * @param ztn_msg message of type 'struct ZoneToNameMessage'
1761 handle_zone_to_name (void *cls,
1762 const struct ZoneToNameMessage *ztn_msg)
1764 struct NamestoreClient *nc = cls;
1765 struct ZoneToNameCtx ztn_ctx;
1766 struct GNUNET_MQ_Envelope *env;
1767 struct ZoneToNameResponseMessage *ztnr_msg;
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770 "Received ZONE_TO_NAME message\n");
1771 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
1773 ztn_ctx.success = GNUNET_NO;
1774 if (GNUNET_SYSERR ==
1775 GSN_database->zone_to_name (GSN_database->cls,
1777 &ztn_msg->value_zone,
1778 &handle_zone_to_name_it, &ztn_ctx))
1780 /* internal error, hang up instead of signalling something
1781 that might be wrong */
1783 GNUNET_SERVICE_client_drop (nc->client);
1786 if (GNUNET_NO == ztn_ctx.success)
1788 /* no result found, send empty response */
1789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1790 "Found no result for zone-to-name lookup.\n");
1791 env = GNUNET_MQ_msg (ztnr_msg,
1792 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1793 ztnr_msg->gns_header.r_id = ztn_msg->gns_header.r_id;
1794 ztnr_msg->res = htons (GNUNET_NO);
1795 GNUNET_MQ_send (nc->mq,
1798 GNUNET_SERVICE_client_continue (nc->client);
1803 * Context for record remove operations passed from
1804 * #run_zone_iteration_round to #zone_iterate_proc as closure
1806 struct ZoneIterationProcResult
1809 * The zone iteration handle
1811 struct ZoneIteration *zi;
1814 * Number of results left to be returned in this iteration.
1822 * Process results for zone iteration from database
1824 * @param cls struct ZoneIterationProcResult
1825 * @param seq sequence number of the record
1826 * @param zone_key the zone key
1828 * @param rd_count number of records for this name
1829 * @param rd record data
1832 zone_iterate_proc (void *cls,
1834 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1836 unsigned int rd_count,
1837 const struct GNUNET_GNSRECORD_Data *rd)
1839 struct ZoneIterationProcResult *proc = cls;
1840 int do_refresh_block;
1842 if ( (NULL == zone_key) &&
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1846 "Iteration done\n");
1849 if ( (NULL == zone_key) ||
1852 /* what is this!? should never happen */
1856 if (0 == proc->limit)
1858 /* what is this!? should never happen */
1863 proc->zi->seq = seq;
1864 send_lookup_response (proc->zi->nc,
1865 proc->zi->request_id,
1872 do_refresh_block = GNUNET_NO;
1873 for (unsigned int i=0;i<rd_count;i++)
1874 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
1876 do_refresh_block = GNUNET_YES;
1879 if (GNUNET_YES == do_refresh_block)
1880 refresh_block (NULL,
1891 * Perform the next round of the zone iteration.
1893 * @param zi zone iterator to process
1894 * @param limit number of results to return in one pass
1897 run_zone_iteration_round (struct ZoneIteration *zi,
1900 struct ZoneIterationProcResult proc;
1901 struct GNUNET_TIME_Absolute start;
1902 struct GNUNET_TIME_Relative duration;
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1908 "Asked to return up to %llu records at position %llu\n",
1909 (unsigned long long) limit,
1910 (unsigned long long) zi->seq);
1913 start = GNUNET_TIME_absolute_get ();
1914 GNUNET_break (GNUNET_SYSERR !=
1915 GSN_database->iterate_records (GSN_database->cls,
1916 (0 == memcmp (&zi->zone,
1925 duration = GNUNET_TIME_absolute_get_duration (start);
1926 duration = GNUNET_TIME_relative_divide (duration,
1927 limit - proc.limit);
1928 GNUNET_STATISTICS_set (statistics,
1929 "NAMESTORE iteration delay (μs/record)",
1930 duration.rel_value_us,
1932 if (0 == proc.limit)
1933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1934 "Returned %llu results, more results available\n",
1935 (unsigned long long) limit);
1936 zi->send_end = (0 != proc.limit);
1937 if (0 == zi->cache_ops)
1938 zone_iteration_done_client_continue (zi);
1943 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1945 * @param cls the client sending the message
1946 * @param zis_msg message from the client
1949 handle_iteration_start (void *cls,
1950 const struct ZoneIterationStartMessage *zis_msg)
1952 struct NamestoreClient *nc = cls;
1953 struct ZoneIteration *zi;
1955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1956 "Received ZONE_ITERATION_START message\n");
1957 zi = GNUNET_new (struct ZoneIteration);
1958 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1961 zi->zone = zis_msg->zone;
1963 GNUNET_CONTAINER_DLL_insert (nc->op_head,
1966 run_zone_iteration_round (zi,
1972 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1974 * @param cls the client sending the message
1975 * @param zis_msg message from the client
1978 handle_iteration_stop (void *cls,
1979 const struct ZoneIterationStopMessage *zis_msg)
1981 struct NamestoreClient *nc = cls;
1982 struct ZoneIteration *zi;
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1986 "Received ZONE_ITERATION_STOP message\n");
1987 rid = ntohl (zis_msg->gns_header.r_id);
1988 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1989 if (zi->request_id == rid)
1994 GNUNET_SERVICE_client_drop (nc->client);
1997 GNUNET_CONTAINER_DLL_remove (nc->op_head,
2001 GNUNET_SERVICE_client_continue (nc->client);
2006 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
2008 * @param cls the client sending the message
2009 * @param message message from the client
2012 handle_iteration_next (void *cls,
2013 const struct ZoneIterationNextMessage *zis_msg)
2015 struct NamestoreClient *nc = cls;
2016 struct ZoneIteration *zi;
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2021 "Received ZONE_ITERATION_NEXT message\n");
2022 GNUNET_STATISTICS_update (statistics,
2023 "Iteration NEXT messages received",
2026 rid = ntohl (zis_msg->gns_header.r_id);
2027 limit = GNUNET_ntohll (zis_msg->limit);
2028 for (zi = nc->op_head; NULL != zi; zi = zi->next)
2029 if (zi->request_id == rid)
2034 GNUNET_SERVICE_client_drop (nc->client);
2037 run_zone_iteration_round (zi,
2043 * Function called when the monitor is ready for more data, and we
2044 * should thus unblock PUT operations that were blocked on the
2045 * monitor not being ready.
2048 monitor_unblock (struct ZoneMonitor *zm)
2050 struct StoreActivity *sa = sa_head;
2052 while ( (NULL != sa) &&
2053 (zm->limit > zm->iteration_cnt) )
2055 struct StoreActivity *sn = sa->next;
2057 if (sa->zm_pos == zm)
2058 continue_store_activity (sa);
2061 if (zm->limit > zm->iteration_cnt)
2063 zm->sa_waiting = GNUNET_NO;
2064 if (NULL != zm->sa_wait_warning)
2066 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
2067 zm->sa_wait_warning = NULL;
2070 else if (GNUNET_YES == zm->sa_waiting)
2072 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
2073 if (NULL != zm->sa_wait_warning)
2074 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
2075 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
2083 * Send 'sync' message to zone monitor, we're now in sync.
2085 * @param zm monitor that is now in sync
2088 monitor_sync (struct ZoneMonitor *zm)
2090 struct GNUNET_MQ_Envelope *env;
2091 struct GNUNET_MessageHeader *sync;
2093 env = GNUNET_MQ_msg (sync,
2094 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
2095 GNUNET_MQ_send (zm->nc->mq,
2097 /* mark iteration done */
2098 zm->in_first_iteration = GNUNET_NO;
2099 zm->iteration_cnt = 0;
2100 if ( (zm->limit > 0) &&
2102 monitor_unblock (zm);
2107 * Obtain the next datum during the zone monitor's zone initial iteration.
2109 * @param cls zone monitor that does its initial iteration
2112 monitor_iteration_next (void *cls);
2116 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
2118 * @param cls a 'struct ZoneMonitor *' with information about the monitor
2119 * @param seq sequence number of the record
2120 * @param zone_key zone key of the zone
2122 * @param rd_count number of records in @a rd
2123 * @param rd array of records
2126 monitor_iterate_cb (void *cls,
2128 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
2130 unsigned int rd_count,
2131 const struct GNUNET_GNSRECORD_Data *rd)
2133 struct ZoneMonitor *zm = cls;
2136 GNUNET_assert (NULL != name);
2137 GNUNET_STATISTICS_update (statistics,
2138 "Monitor notifications sent",
2142 zm->iteration_cnt--;
2143 send_lookup_response (zm->nc,
2149 if ( (0 == zm->iteration_cnt) &&
2152 /* We are done with the current iteration batch, AND the
2153 client would right now accept more, so go again! */
2154 GNUNET_assert (NULL == zm->task);
2155 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2162 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
2164 * @param cls the client sending the message
2165 * @param zis_msg message from the client
2168 handle_monitor_start (void *cls,
2169 const struct ZoneMonitorStartMessage *zis_msg)
2171 struct NamestoreClient *nc = cls;
2172 struct ZoneMonitor *zm;
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2175 "Received ZONE_MONITOR_START message\n");
2176 zm = GNUNET_new (struct ZoneMonitor);
2178 zm->zone = zis_msg->zone;
2180 zm->in_first_iteration = (GNUNET_YES == ntohl (zis_msg->iterate_first));
2181 GNUNET_CONTAINER_DLL_insert (monitor_head,
2184 GNUNET_SERVICE_client_mark_monitor (nc->client);
2185 GNUNET_SERVICE_client_continue (nc->client);
2186 GNUNET_notification_context_add (monitor_nc,
2188 if (zm->in_first_iteration)
2189 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2197 * Obtain the next datum during the zone monitor's zone initial iteration.
2199 * @param cls zone monitor that does its initial iteration
2202 monitor_iteration_next (void *cls)
2204 struct ZoneMonitor *zm = cls;
2208 GNUNET_assert (0 == zm->iteration_cnt);
2210 zm->iteration_cnt = zm->limit / 2; /* leave half for monitor events */
2212 zm->iteration_cnt = zm->limit; /* use it all */
2213 ret = GSN_database->iterate_records (GSN_database->cls,
2214 (0 == memcmp (&zm->zone,
2221 &monitor_iterate_cb,
2223 if (GNUNET_SYSERR == ret)
2225 GNUNET_SERVICE_client_drop (zm->nc->client);
2228 if (GNUNET_NO == ret)
2238 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT message
2240 * @param cls the client sending the message
2241 * @param nm message from the client
2244 handle_monitor_next (void *cls,
2245 const struct ZoneMonitorNextMessage *nm)
2247 struct NamestoreClient *nc = cls;
2248 struct ZoneMonitor *zm;
2251 inc = GNUNET_ntohll (nm->limit);
2252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2253 "Received ZONE_MONITOR_NEXT message with limit %llu\n",
2254 (unsigned long long) inc);
2255 for (zm = monitor_head; NULL != zm; zm = zm->next)
2261 GNUNET_SERVICE_client_drop (nc->client);
2264 GNUNET_SERVICE_client_continue (nc->client);
2265 if (zm->limit + inc < zm->limit)
2268 GNUNET_SERVICE_client_drop (nc->client);
2272 if ( (zm->in_first_iteration) &&
2273 (zm->limit == inc) )
2275 /* We are still iterating, and the previous iteration must
2276 have stopped due to the client's limit, so continue it! */
2277 GNUNET_assert (NULL == zm->task);
2278 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2281 GNUNET_assert (zm->iteration_cnt <= zm->limit);
2282 if ( (zm->limit > zm->iteration_cnt) &&
2285 monitor_unblock (zm);
2287 else if (GNUNET_YES == zm->sa_waiting)
2289 if (NULL != zm->sa_wait_warning)
2290 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
2291 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
2292 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
2300 * Process namestore requests.
2302 * @param cls closure
2303 * @param cfg configuration to use
2304 * @param service the initialized service
2308 const struct GNUNET_CONFIGURATION_Handle *cfg,
2309 struct GNUNET_SERVICE_Handle *service)
2315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2316 "Starting namestore service\n");
2317 cache_keys = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2320 disable_namecache = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2324 monitor_nc = GNUNET_notification_context_create (1);
2325 if (GNUNET_YES != disable_namecache)
2327 namecache = GNUNET_NAMECACHE_connect (cfg);
2328 GNUNET_assert (NULL != namecache);
2330 /* Loading database plugin */
2332 GNUNET_CONFIGURATION_get_value_string (cfg,
2336 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2337 "No database backend configured\n");
2339 GNUNET_asprintf (&db_lib_name,
2340 "libgnunet_plugin_namestore_%s",
2342 GSN_database = GNUNET_PLUGIN_load (db_lib_name,
2344 GNUNET_free (database);
2345 statistics = GNUNET_STATISTICS_create ("namestore",
2347 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
2349 if (NULL == GSN_database)
2351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2352 "Could not load database backend `%s'\n",
2354 GNUNET_SCHEDULER_shutdown ();
2361 * Define "main" method using service macro.
2365 GNUNET_SERVICE_OPTION_NONE,
2368 &client_disconnect_cb,
2370 GNUNET_MQ_hd_var_size (record_store,
2371 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE,
2372 struct RecordStoreMessage,
2374 GNUNET_MQ_hd_var_size (record_lookup,
2375 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP,
2376 struct LabelLookupMessage,
2378 GNUNET_MQ_hd_fixed_size (zone_to_name,
2379 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME,
2380 struct ZoneToNameMessage,
2382 GNUNET_MQ_hd_fixed_size (iteration_start,
2383 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START,
2384 struct ZoneIterationStartMessage,
2386 GNUNET_MQ_hd_fixed_size (iteration_next,
2387 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT,
2388 struct ZoneIterationNextMessage,
2390 GNUNET_MQ_hd_fixed_size (iteration_stop,
2391 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP,
2392 struct ZoneIterationStopMessage,
2394 GNUNET_MQ_hd_fixed_size (monitor_start,
2395 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START,
2396 struct ZoneMonitorStartMessage,
2398 GNUNET_MQ_hd_fixed_size (monitor_next,
2399 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT,
2400 struct ZoneMonitorNextMessage,
2402 GNUNET_MQ_handler_end ());
2405 /* end of gnunet-service-namestore.c */