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/>.
20 * @file namestore/gnunet-service-namestore.c
21 * @brief namestore for the GNUnet naming system
22 * @author Matthias Wachs
23 * @author Christian Grothoff
26 * - "get_nick_record" is a bottleneck, introduce a cache to
27 * avoid looking it up again and again (for the same few
28 * zones that the user will typically manage!)
29 * - run testcases, make sure everything works!
32 #include "gnunet_util_lib.h"
33 #include "gnunet_dnsparser_lib.h"
34 #include "gnunet_gns_service.h"
35 #include "gnunet_namecache_service.h"
36 #include "gnunet_namestore_service.h"
37 #include "gnunet_namestore_plugin.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_signatures.h"
40 #include "namestore.h"
42 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
45 * If a monitor takes more than 1 minute to process an event, print a warning.
47 #define MONITOR_STALL_WARN_DELAY GNUNET_TIME_UNIT_MINUTES
50 * Size of the cache used by #get_nick_record()
57 struct NamestoreClient;
61 * A namestore iteration operation.
66 * Next element in the DLL
68 struct ZoneIteration *next;
71 * Previous element in the DLL
73 struct ZoneIteration *prev;
76 * Namestore client which intiated this zone iteration
78 struct NamestoreClient *nc;
81 * The nick to add to the records
83 struct GNUNET_GNSRECORD_Data *nick;
86 * Key of the zone we are iterating over.
88 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
91 * Last sequence number in the zone iteration used to address next
92 * result of the zone iteration in the store
95 * Updated in #zone_iterate_proc()
100 * The operation id fot the zone iteration in the response for the client
105 * Offset of the zone iteration used to address next result of the zone
106 * iteration in the store
108 * Initialy set to 0 in #handle_iteration_start
109 * Incremented with by every call to #handle_iteration_next
119 struct NamestoreClient
125 struct GNUNET_SERVICE_Client *client;
128 * Message queue for transmission to @e client
130 struct GNUNET_MQ_Handle *mq;
134 * Zone iteration operations in progress initiated by this client
136 struct ZoneIteration *op_head;
140 * Zone iteration operations in progress initiated by this client
142 struct ZoneIteration *op_tail;
147 * A namestore monitor.
152 * Next element in the DLL
154 struct ZoneMonitor *next;
157 * Previous element in the DLL
159 struct ZoneMonitor *prev;
162 * Namestore client which intiated this zone monitor
164 struct NamestoreClient *nc;
167 * Private key of the zone.
169 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
172 * Task active during initial iteration.
174 struct GNUNET_SCHEDULER_Task *task;
177 * Task to warn about slow monitors.
179 struct GNUNET_SCHEDULER_Task *sa_wait_warning;
182 * Since when are we blocked on this monitor?
184 struct GNUNET_TIME_Absolute sa_waiting_start;
187 * Last sequence number in the zone iteration used to address next
188 * result of the zone iteration in the store
191 * Updated in #monitor_iterate_cb()
196 * Current limit of how many more messages we are allowed
197 * to queue to this monitor.
202 * How many more requests may we receive from the iterator
203 * before it is at the limit we gave it? Will be below or
204 * equal to @e limit. The effective limit for monitor
205 * events is thus @e iteration_cnt - @e limit!
207 uint64_t iteration_cnt;
210 * Are we (still) in the initial iteration pass?
212 int in_first_iteration;
215 * Is there a store activity waiting for this monitor? We only raise the
216 * flag when it happens and search the DLL for the store activity when we
217 * had a limit increase. If we cannot find any waiting store activity at
218 * that time, we clear the flag again.
226 * Pending operation on the namecache.
228 struct CacheOperation
234 struct CacheOperation *prev;
239 struct CacheOperation *next;
242 * Handle to namecache queue.
244 struct GNUNET_NAMECACHE_QueueEntry *qe;
247 * Client to notify about the result.
249 struct NamestoreClient *nc;
252 * Client's request ID.
259 * Information for an ongoing #handle_record_store() operation.
260 * Needed as we may wait for monitors to be ready for the notification.
267 struct StoreActivity *next;
272 struct StoreActivity *prev;
275 * Which client triggered the store activity?
277 struct NamestoreClient *nc;
280 * Copy of the original store message (as data fields in @e rd will
283 const struct RecordStoreMessage *rsm;
286 * Next zone monitor that still needs to be notified about this PUT.
288 struct ZoneMonitor *zm_pos;
291 * Label nicely canonicalized (lower case).
299 * Entry in list of cached nick resolutions.
304 * Zone the cache entry is for.
306 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
309 * Cached record data.
311 struct GNUNET_GNSRECORD_Data *rd;
314 * Timestamp when this cache entry was used last.
316 struct GNUNET_TIME_Absolute last_used;
321 * We cache nick records to reduce DB load.
323 static struct NickCache nick_cache[NC_SIZE];
326 * Public key of all zeros.
328 static const struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
331 * Configuration handle.
333 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
336 * Handle to the statistics service
338 static struct GNUNET_STATISTICS_Handle *statistics;
343 static struct GNUNET_NAMECACHE_Handle *namecache;
348 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
351 * Name of the database plugin
353 static char *db_lib_name;
358 static struct CacheOperation *cop_head;
363 static struct CacheOperation *cop_tail;
366 * First active zone monitor.
368 static struct ZoneMonitor *monitor_head;
371 * Last active zone monitor.
373 static struct ZoneMonitor *monitor_tail;
376 * Head of DLL of monitor-blocked store activities.
378 static struct StoreActivity *sa_head;
381 * Tail of DLL of monitor-blocked store activities.
383 static struct StoreActivity *sa_tail;
386 * Notification context shared by all monitors.
388 static struct GNUNET_NotificationContext *monitor_nc;
391 * Optimize block insertion by caching map of private keys to
392 * public keys in memory?
394 static int cache_keys;
397 * Use the namecache? Doing so creates additional cryptographic
398 * operations whenever we touch a record.
400 static int disable_namecache;
404 * Task run during shutdown.
409 cleanup_task (void *cls)
411 struct CacheOperation *cop;
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Stopping namestore service\n");
416 while (NULL != (cop = cop_head))
418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
419 "Aborting incomplete namecache operation\n");
420 GNUNET_NAMECACHE_cancel (cop->qe);
421 GNUNET_CONTAINER_DLL_remove (cop_head,
426 if (NULL != namecache)
428 GNUNET_NAMECACHE_disconnect (namecache);
431 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name,
433 GNUNET_free (db_lib_name);
435 if (NULL != monitor_nc)
437 GNUNET_notification_context_destroy (monitor_nc);
440 if (NULL != statistics)
442 GNUNET_STATISTICS_destroy (statistics,
450 * Release memory used by @a sa.
452 * @param sa activity to free
455 free_store_activity (struct StoreActivity *sa)
457 GNUNET_CONTAINER_DLL_remove (sa_head,
460 GNUNET_free (sa->conv_name);
466 * Function called with the records for the #GNUNET_GNS_EMPTY_LABEL_AT
467 * label in the zone. Used to locate the #GNUNET_GNSRECORD_TYPE_NICK
468 * record, which (if found) is then copied to @a cls for future use.
470 * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
471 * @param seq sequence number of the record
472 * @param private_key the private key of the zone (unused)
473 * @param label should be #GNUNET_GNS_EMPTY_LABEL_AT
474 * @param rd_count number of records in @a rd
475 * @param rd records stored under @a label in the zone
478 lookup_nick_it (void *cls,
480 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
482 unsigned int rd_count,
483 const struct GNUNET_GNSRECORD_Data *rd)
485 struct GNUNET_GNSRECORD_Data **res = cls;
489 if (0 != strcmp (label, GNUNET_GNS_EMPTY_LABEL_AT))
494 for (unsigned int c = 0; c < rd_count; c++)
496 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
498 (*res) = GNUNET_malloc (rd[c].data_size + sizeof (struct GNUNET_GNSRECORD_Data));
499 (*res)->data = &(*res)[1];
500 GNUNET_memcpy ((void *) (*res)->data,
503 (*res)->data_size = rd[c].data_size;
504 (*res)->expiration_time = rd[c].expiration_time;
505 (*res)->flags = rd[c].flags;
506 (*res)->record_type = GNUNET_GNSRECORD_TYPE_NICK;
515 * Add entry to the cache for @a zone and @a nick
517 * @param zone zone key to cache under
518 * @param nick nick entry to cache
521 cache_nick (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
522 const struct GNUNET_GNSRECORD_Data *nick)
524 struct NickCache *oldest;
527 for (unsigned int i=0;i<NC_SIZE;i++)
529 struct NickCache *pos = &nick_cache[i];
531 if ( (NULL == oldest) ||
532 (oldest->last_used.abs_value_us >
533 pos->last_used.abs_value_us) )
535 if (0 == memcmp (zone,
543 GNUNET_free_non_null (oldest->rd);
544 oldest->zone = *zone;
545 oldest->rd = GNUNET_malloc (sizeof (*nick) +
548 oldest->rd->data = &oldest->rd[1];
549 memcpy (&oldest->rd[1],
552 oldest->last_used = GNUNET_TIME_absolute_get ();
557 * Return the NICK record for the zone (if it exists).
559 * @param zone private key for the zone to look for nick
560 * @return NULL if no NICK record was found
562 static struct GNUNET_GNSRECORD_Data *
563 get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
565 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
566 struct GNUNET_GNSRECORD_Data *nick;
569 /* check cache first */
570 for (unsigned int i=0;i<NC_SIZE;i++)
572 struct NickCache *pos = &nick_cache[i];
573 if ( (NULL != pos->rd) &&
578 nick = GNUNET_malloc (sizeof (*nick) +
581 nick->data = &nick[1];
585 pos->last_used = GNUNET_TIME_absolute_get ();
591 res = GSN_database->lookup_records (GSN_database->cls,
593 GNUNET_GNS_EMPTY_LABEL_AT,
596 if ( (GNUNET_OK != res) ||
599 GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
601 "No nick name set for zone `%s'\n",
602 GNUNET_GNSRECORD_z2s (&pub));
614 * Merge the nick record @a nick_rd with the rest of the
615 * record set given in @a rd2. Store the result in @a rdc_res
616 * and @a rd_res. The @a nick_rd's expiration time is set to
617 * the maximum expiration time of all of the records in @a rd2.
619 * @param nick_rd the nick record to integrate
620 * @param rd2_length length of the @a rd2 array
621 * @param rd2 array of records
622 * @param rdc_res[out] length of the resulting @a rd_res array
623 * @param rd_res[out] set to an array of records,
624 * including @a nick_rd and @a rd2;
625 * all of the variable-size 'data' fields in @a rd2 are
626 * allocated in the same chunk of memory!
629 merge_with_nick_records (const struct GNUNET_GNSRECORD_Data *nick_rd,
630 unsigned int rd2_length,
631 const struct GNUNET_GNSRECORD_Data *rd2,
632 unsigned int *rdc_res,
633 struct GNUNET_GNSRECORD_Data **rd_res)
635 uint64_t latest_expiration;
639 struct GNUNET_GNSRECORD_Data *target;
641 (*rdc_res) = 1 + rd2_length;
642 if (0 == 1 + rd2_length)
648 req = sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd->data_size;
649 for (unsigned int i=0; i<rd2_length; i++)
651 const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
653 if (req + sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size < req)
659 req += sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size;
661 target = GNUNET_malloc (req);
663 data = (char *) &target[1 + rd2_length];
665 latest_expiration = 0;
666 for (unsigned int i=0;i<rd2_length;i++)
668 const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
670 if (0 != (orig->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
672 if ((GNUNET_TIME_absolute_get().abs_value_us + orig->expiration_time) >
674 latest_expiration = orig->expiration_time;
676 else if (orig->expiration_time > latest_expiration)
677 latest_expiration = orig->expiration_time;
679 target[i].data = (void *) &data[data_offset];
680 GNUNET_memcpy (&data[data_offset],
683 data_offset += orig->data_size;
686 target[rd2_length] = *nick_rd;
687 target[rd2_length].expiration_time = latest_expiration;
688 target[rd2_length].data = (void *) &data[data_offset];
689 GNUNET_memcpy (&data[data_offset],
692 data_offset += nick_rd->data_size;
693 GNUNET_assert (req ==
694 (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
699 * Generate a `struct LookupNameResponseMessage` and send it to the
700 * given client using the given notification context.
702 * @param nc client to unicast to
703 * @param request_id request ID to use
704 * @param zone_key zone key of the zone
706 * @param rd_count number of records in @a rd
707 * @param rd array of records
710 send_lookup_response (struct NamestoreClient *nc,
712 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
714 unsigned int rd_count,
715 const struct GNUNET_GNSRECORD_Data *rd)
717 struct GNUNET_MQ_Envelope *env;
718 struct RecordResultMessage *zir_msg;
719 struct GNUNET_GNSRECORD_Data *nick;
720 struct GNUNET_GNSRECORD_Data *res;
721 unsigned int res_count;
727 nick = get_nick_record (zone_key);
729 GNUNET_GNSRECORD_records_get_size (rd_count,
732 if ( (NULL != nick) &&
734 GNUNET_GNS_EMPTY_LABEL_AT)))
736 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
737 merge_with_nick_records (nick,
746 res_count = rd_count;
747 res = (struct GNUNET_GNSRECORD_Data *) rd;
751 GNUNET_GNSRECORD_records_get_size (res_count,
755 name_len = strlen (name) + 1;
756 rd_ser_len = GNUNET_GNSRECORD_records_get_size (res_count,
761 GNUNET_SERVICE_client_drop (nc->client);
764 if (((size_t) rd_ser_len) >= UINT16_MAX - name_len - sizeof (*zir_msg))
767 GNUNET_SERVICE_client_drop (nc->client);
770 env = GNUNET_MQ_msg_extra (zir_msg,
771 name_len + rd_ser_len,
772 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
773 zir_msg->gns_header.r_id = htonl (request_id);
774 zir_msg->name_len = htons (name_len);
775 zir_msg->rd_count = htons (res_count);
776 zir_msg->rd_len = htons ((uint16_t) rd_ser_len);
777 zir_msg->private_key = *zone_key;
778 name_tmp = (char *) &zir_msg[1];
779 GNUNET_memcpy (name_tmp,
782 rd_ser = &name_tmp[name_len];
783 GNUNET_assert (rd_ser_len ==
784 GNUNET_GNSRECORD_records_serialize (res_count,
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "Sending RECORD_RESULT message with %u records\n",
791 GNUNET_STATISTICS_update (statistics,
792 "Record sets sent to clients",
795 GNUNET_MQ_send (nc->mq,
803 * Send response to the store request to the client.
805 * @param client client to talk to
806 * @param res status of the operation
807 * @param rid client's request ID
810 send_store_response (struct NamestoreClient *nc,
814 struct GNUNET_MQ_Envelope *env;
815 struct RecordStoreResponseMessage *rcr_msg;
817 GNUNET_assert (NULL != nc);
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819 "Sending RECORD_STORE_RESPONSE message\n");
820 GNUNET_STATISTICS_update (statistics,
821 "Store requests completed",
824 env = GNUNET_MQ_msg (rcr_msg,
825 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
826 rcr_msg->gns_header.r_id = htonl (rid);
827 rcr_msg->op_result = htonl (res);
828 GNUNET_MQ_send (nc->mq,
834 * Cache operation complete, clean up.
836 * @param cls the `struct CacheOperation`
837 * @param success success
838 * @param emsg error messages
841 finish_cache_operation (void *cls,
845 struct CacheOperation *cop = cls;
848 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
849 _("Failed to replicate block in namecache: %s\n"),
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
853 "CACHE operation completed\n");
854 GNUNET_CONTAINER_DLL_remove (cop_head,
858 send_store_response (cop->nc,
866 * We just touched the plaintext information about a name in our zone;
867 * refresh the corresponding (encrypted) block in the namecache.
869 * @param nc client responsible for the request, can be NULL
870 * @param rid request ID of the client
871 * @param zone_key private key of the zone
872 * @param name label for the records
873 * @param rd_count number of records
874 * @param rd records stored under the given @a name
877 refresh_block (struct NamestoreClient *nc,
879 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
881 unsigned int rd_count,
882 const struct GNUNET_GNSRECORD_Data *rd)
884 struct GNUNET_GNSRECORD_Block *block;
885 struct CacheOperation *cop;
886 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
887 struct GNUNET_GNSRECORD_Data *nick;
888 struct GNUNET_GNSRECORD_Data *res;
889 unsigned int res_count;
890 struct GNUNET_TIME_Absolute exp_time;
892 nick = get_nick_record (zone_key);
893 res_count = rd_count;
894 res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
897 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
898 merge_with_nick_records (nick,
907 send_store_response (nc,
910 return; /* no data, no need to update cache */
912 if (GNUNET_YES == disable_namecache)
914 GNUNET_STATISTICS_update (statistics,
915 "Namecache updates skipped (NC disabled)",
919 send_store_response (nc,
924 exp_time = GNUNET_GNSRECORD_record_get_expiration_time (res_count,
927 block = GNUNET_GNSRECORD_block_create2 (zone_key,
933 block = GNUNET_GNSRECORD_block_create (zone_key,
938 GNUNET_assert (NULL != block);
939 GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942 "Caching block for label `%s' with %u records and expiration %s in zone `%s' in namecache\n",
945 GNUNET_STRINGS_absolute_time_to_string (exp_time),
946 GNUNET_GNSRECORD_z2s (&pkey));
947 GNUNET_STATISTICS_update (statistics,
948 "Namecache updates pushed",
951 cop = GNUNET_new (struct CacheOperation);
954 GNUNET_CONTAINER_DLL_insert (cop_head,
957 cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
959 &finish_cache_operation,
966 * Print a warning that one of our monitors is no longer reacting.
968 * @param cls a `struct ZoneMonitor` to warn about
971 warn_monitor_slow (void *cls)
973 struct ZoneMonitor *zm = cls;
975 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
976 "No response from monitor since %s\n",
977 GNUNET_STRINGS_absolute_time_to_string (zm->sa_waiting_start));
978 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
985 * Continue processing the @a sa.
987 * @param sa store activity to process
990 continue_store_activity (struct StoreActivity *sa)
992 const struct RecordStoreMessage *rp_msg = sa->rsm;
993 unsigned int rd_count;
997 const char *name_tmp;
1000 rid = ntohl (rp_msg->gns_header.r_id);
1001 name_len = ntohs (rp_msg->name_len);
1002 rd_count = ntohs (rp_msg->rd_count);
1003 rd_ser_len = ntohs (rp_msg->rd_len);
1004 name_tmp = (const char *) &rp_msg[1];
1005 rd_ser = &name_tmp[name_len];
1007 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
1009 /* We did this before, must succeed again */
1010 GNUNET_assert (GNUNET_OK ==
1011 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
1016 for (struct ZoneMonitor *zm = sa->zm_pos;
1020 if ( (0 != memcmp (&rp_msg->private_key,
1022 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) &&
1023 (0 != memcmp (&zm->zone,
1025 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
1027 sa->zm_pos = zm->next; /* not interesting to this monitor */
1030 if (zm->limit == zm->iteration_cnt)
1032 zm->sa_waiting = GNUNET_YES;
1033 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
1034 if (NULL != zm->sa_wait_warning)
1035 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1036 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
1039 return; /* blocked on zone monitor */
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042 "Notifying monitor about changes under label `%s'\n",
1045 send_lookup_response (zm->nc,
1047 &rp_msg->private_key,
1051 sa->zm_pos = zm->next;
1053 /* great, done with the monitors, unpack (again) for refresh_block operation */
1054 refresh_block (sa->nc,
1056 &rp_msg->private_key,
1061 GNUNET_SERVICE_client_continue (sa->nc->client);
1062 free_store_activity (sa);
1067 * Called whenever a client is disconnected.
1068 * Frees our resources associated with that client.
1070 * @param cls closure
1071 * @param client identification of the client
1072 * @param app_ctx the `struct NamestoreClient` of @a client
1075 client_disconnect_cb (void *cls,
1076 struct GNUNET_SERVICE_Client *client,
1079 struct NamestoreClient *nc = app_ctx;
1080 struct ZoneIteration *no;
1081 struct CacheOperation *cop;
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085 "Client %p disconnected\n",
1087 for (struct ZoneMonitor *zm = monitor_head; NULL != zm; zm = zm->next)
1089 struct StoreActivity *san;
1093 GNUNET_CONTAINER_DLL_remove (monitor_head,
1096 if (NULL != zm->task)
1098 GNUNET_SCHEDULER_cancel (zm->task);
1101 if (NULL != zm->sa_wait_warning)
1103 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1104 zm->sa_wait_warning = NULL;
1106 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = san)
1109 if (zm == sa->zm_pos)
1111 sa->zm_pos = zm->next;
1112 /* this may free sa */
1113 continue_store_activity (sa);
1119 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = sa->next)
1123 /* this may free sa */
1124 free_store_activity (sa);
1125 break; /* there can only be one per nc */
1128 while (NULL != (no = nc->op_head))
1130 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1135 for (cop = cop_head; NULL != cop; cop = cop->next)
1143 * Add a client to our list of active clients.
1146 * @param client client to add
1147 * @param mq message queue for @a client
1148 * @return internal namestore client structure for this client
1151 client_connect_cb (void *cls,
1152 struct GNUNET_SERVICE_Client *client,
1153 struct GNUNET_MQ_Handle *mq)
1155 struct NamestoreClient *nc;
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1159 "Client %p connected\n",
1161 nc = GNUNET_new (struct NamestoreClient);
1162 nc->client = client;
1169 * Closure for #lookup_it().
1171 struct RecordLookupContext
1187 struct GNUNET_GNSRECORD_Data *nick;
1197 unsigned int res_rd_count;
1209 * @param seq sequence number of the record
1212 lookup_it (void *cls,
1214 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
1216 unsigned int rd_count,
1217 const struct GNUNET_GNSRECORD_Data *rd)
1219 struct RecordLookupContext *rlc = cls;
1223 if (0 != strcmp (label,
1226 rlc->found = GNUNET_YES;
1229 rlc->rd_ser_len = 0;
1230 rlc->res_rd_count = 0;
1234 if ( (NULL != rlc->nick) &&
1235 (0 != strcmp (label,
1236 GNUNET_GNS_EMPTY_LABEL_AT)) )
1239 struct GNUNET_GNSRECORD_Data *rd_res;
1240 unsigned int rdc_res;
1244 rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
1245 merge_with_nick_records (rlc->nick,
1250 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res,
1252 if (rlc->rd_ser_len < 0)
1255 GNUNET_free (rd_res);
1256 rlc->found = GNUNET_NO;
1257 rlc->rd_ser_len = 0;
1260 rlc->res_rd_count = rdc_res;
1261 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1262 if (rlc->rd_ser_len !=
1263 GNUNET_GNSRECORD_records_serialize (rdc_res,
1269 GNUNET_free (rlc->res_rd);
1271 rlc->res_rd_count = 0;
1272 rlc->rd_ser_len = 0;
1273 GNUNET_free (rd_res);
1274 rlc->found = GNUNET_NO;
1277 GNUNET_free (rd_res);
1278 GNUNET_free (rlc->nick);
1283 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1285 if (rlc->rd_ser_len < 0)
1288 rlc->found = GNUNET_NO;
1289 rlc->rd_ser_len = 0;
1292 rlc->res_rd_count = rd_count;
1293 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1294 if (rlc->rd_ser_len !=
1295 GNUNET_GNSRECORD_records_serialize (rd_count,
1301 GNUNET_free (rlc->res_rd);
1303 rlc->res_rd_count = 0;
1304 rlc->rd_ser_len = 0;
1305 rlc->found = GNUNET_NO;
1313 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1315 * @param cls client sending the message
1316 * @param ll_msg message of type `struct LabelLookupMessage`
1317 * @return #GNUNET_OK if @a ll_msg is well-formed
1320 check_record_lookup (void *cls,
1321 const struct LabelLookupMessage *ll_msg)
1327 name_len = ntohl (ll_msg->label_len);
1328 src_size = ntohs (ll_msg->gns_header.header.size);
1329 if (name_len != src_size - sizeof (struct LabelLookupMessage))
1332 return GNUNET_SYSERR;
1334 GNUNET_MQ_check_zero_termination (ll_msg);
1340 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1342 * @param cls client sending the message
1343 * @param ll_msg message of type `struct LabelLookupMessage`
1346 handle_record_lookup (void *cls,
1347 const struct LabelLookupMessage *ll_msg)
1349 struct NamestoreClient *nc = cls;
1350 struct GNUNET_MQ_Envelope *env;
1351 struct LabelLookupResponseMessage *llr_msg;
1352 struct RecordLookupContext rlc;
1353 const char *name_tmp;
1359 name_len = ntohl (ll_msg->label_len);
1360 name_tmp = (const char *) &ll_msg[1];
1361 GNUNET_SERVICE_client_continue (nc->client);
1362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1363 "Received NAMESTORE_RECORD_LOOKUP message for name `%s'\n",
1366 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1367 if (NULL == conv_name)
1369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1370 "Error converting name `%s'\n",
1372 GNUNET_SERVICE_client_drop (nc->client);
1375 rlc.label = conv_name;
1376 rlc.found = GNUNET_NO;
1377 rlc.res_rd_count = 0;
1380 rlc.nick = get_nick_record (&ll_msg->zone);
1381 res = GSN_database->lookup_records (GSN_database->cls,
1386 GNUNET_free (conv_name);
1387 env = GNUNET_MQ_msg_extra (llr_msg,
1388 name_len + rlc.rd_ser_len,
1389 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
1390 llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
1391 llr_msg->private_key = ll_msg->zone;
1392 llr_msg->name_len = htons (name_len);
1393 llr_msg->rd_count = htons (rlc.res_rd_count);
1394 llr_msg->rd_len = htons (rlc.rd_ser_len);
1395 res_name = (char *) &llr_msg[1];
1396 if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
1397 llr_msg->found = ntohs (GNUNET_YES);
1399 llr_msg->found = ntohs (GNUNET_NO);
1400 GNUNET_memcpy (&llr_msg[1],
1403 GNUNET_memcpy (&res_name[name_len],
1406 GNUNET_MQ_send (nc->mq,
1408 GNUNET_free_non_null (rlc.res_rd);
1413 * Checks a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1415 * @param cls client sending the message
1416 * @param rp_msg message of type `struct RecordStoreMessage`
1417 * @return #GNUNET_OK if @a rp_msg is well-formed
1420 check_record_store (void *cls,
1421 const struct RecordStoreMessage *rp_msg)
1425 size_t msg_size_exp;
1427 const char *name_tmp;
1430 name_len = ntohs (rp_msg->name_len);
1431 msg_size = ntohs (rp_msg->gns_header.header.size);
1432 rd_ser_len = ntohs (rp_msg->rd_len);
1433 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1434 if (msg_size != msg_size_exp)
1437 return GNUNET_SYSERR;
1439 if ( (0 == name_len) ||
1440 (name_len > MAX_NAME_LEN) )
1443 return GNUNET_SYSERR;
1445 name_tmp = (const char *) &rp_msg[1];
1446 if ('\0' != name_tmp[name_len -1])
1449 return GNUNET_SYSERR;
1456 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1458 * @param cls client sending the message
1459 * @param rp_msg message of type `struct RecordStoreMessage`
1462 handle_record_store (void *cls,
1463 const struct RecordStoreMessage *rp_msg)
1465 struct NamestoreClient *nc = cls;
1469 const char *name_tmp;
1472 unsigned int rd_count;
1474 struct StoreActivity *sa;
1476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1477 "Received NAMESTORE_RECORD_STORE message\n");
1478 rid = ntohl (rp_msg->gns_header.r_id);
1479 name_len = ntohs (rp_msg->name_len);
1480 rd_count = ntohs (rp_msg->rd_count);
1481 rd_ser_len = ntohs (rp_msg->rd_len);
1482 GNUNET_break (0 == ntohs (rp_msg->reserved));
1483 name_tmp = (const char *) &rp_msg[1];
1484 rd_ser = &name_tmp[name_len];
1486 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
1489 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
1495 GNUNET_SERVICE_client_drop (nc->client);
1499 /* Extracting and converting private key */
1500 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1501 if (NULL == conv_name)
1503 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1504 "Error converting name `%s'\n",
1506 GNUNET_SERVICE_client_drop (nc->client);
1509 GNUNET_STATISTICS_update (statistics,
1510 "Well-formed store requests received",
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 "Creating %u records for name `%s'\n",
1515 (unsigned int) rd_count,
1517 if ( (0 == rd_count) &&
1519 GSN_database->lookup_records (GSN_database->cls,
1520 &rp_msg->private_key,
1525 /* This name does not exist, so cannot be removed */
1526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1527 "Name `%s' does not exist, no deletion required\n",
1533 /* remove "NICK" records, unless this is for the
1534 #GNUNET_GNS_EMPTY_LABEL_AT label */
1535 struct GNUNET_GNSRECORD_Data rd_clean[GNUNET_NZL(rd_count)];
1536 unsigned int rd_clean_off;
1539 for (unsigned int i=0;i<rd_count;i++)
1541 rd_clean[rd_clean_off] = rd[i];
1542 if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
1544 (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
1547 if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
1549 (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) )
1550 cache_nick (&rp_msg->private_key,
1553 res = GSN_database->store_records (GSN_database->cls,
1554 &rp_msg->private_key,
1560 if (GNUNET_OK != res)
1562 /* store not successful, not need to tell monitors */
1563 send_store_response (nc,
1566 GNUNET_SERVICE_client_continue (nc->client);
1567 GNUNET_free (conv_name);
1571 sa = GNUNET_malloc (sizeof (struct StoreActivity) +
1572 ntohs (rp_msg->gns_header.header.size));
1573 GNUNET_CONTAINER_DLL_insert (sa_head,
1577 sa->rsm = (const struct RecordStoreMessage *) &sa[1];
1578 GNUNET_memcpy (&sa[1],
1580 ntohs (rp_msg->gns_header.header.size));
1581 sa->zm_pos = monitor_head;
1582 sa->conv_name = conv_name;
1583 continue_store_activity (sa);
1589 * Context for record remove operations passed from #handle_zone_to_name to
1590 * #handle_zone_to_name_it as closure
1592 struct ZoneToNameCtx
1597 struct NamestoreClient *nc;
1600 * Request id (to be used in the response to the client).
1605 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
1606 * not finding a name for the zone still counts as a 'success' here,
1607 * as this field is about the success of executing the IPC protocol.
1614 * Zone to name iterator
1616 * @param cls struct ZoneToNameCtx *
1617 * @param seq sequence number of the record
1618 * @param zone_key the zone key
1620 * @param rd_count number of records in @a rd
1621 * @param rd record data
1624 handle_zone_to_name_it (void *cls,
1626 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1628 unsigned int rd_count,
1629 const struct GNUNET_GNSRECORD_Data *rd)
1631 struct ZoneToNameCtx *ztn_ctx = cls;
1632 struct GNUNET_MQ_Envelope *env;
1633 struct ZoneToNameResponseMessage *ztnr_msg;
1642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1643 "Found result for zone-to-name lookup: `%s'\n",
1646 name_len = (NULL == name) ? 0 : strlen (name) + 1;
1647 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1652 ztn_ctx->success = GNUNET_SYSERR;
1655 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
1656 if (msg_size >= GNUNET_MAX_MESSAGE_SIZE)
1659 ztn_ctx->success = GNUNET_SYSERR;
1662 env = GNUNET_MQ_msg_extra (ztnr_msg,
1663 name_len + rd_ser_len,
1664 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1665 ztnr_msg->gns_header.header.size = htons (msg_size);
1666 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1667 ztnr_msg->res = htons (res);
1668 ztnr_msg->rd_len = htons (rd_ser_len);
1669 ztnr_msg->rd_count = htons (rd_count);
1670 ztnr_msg->name_len = htons (name_len);
1671 ztnr_msg->zone = *zone_key;
1672 name_tmp = (char *) &ztnr_msg[1];
1673 GNUNET_memcpy (name_tmp,
1676 rd_tmp = &name_tmp[name_len];
1677 GNUNET_assert (rd_ser_len ==
1678 GNUNET_GNSRECORD_records_serialize (rd_count,
1682 ztn_ctx->success = GNUNET_OK;
1683 GNUNET_MQ_send (ztn_ctx->nc->mq,
1689 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
1691 * @param cls client client sending the message
1692 * @param ztn_msg message of type 'struct ZoneToNameMessage'
1695 handle_zone_to_name (void *cls,
1696 const struct ZoneToNameMessage *ztn_msg)
1698 struct NamestoreClient *nc = cls;
1699 struct ZoneToNameCtx ztn_ctx;
1700 struct GNUNET_MQ_Envelope *env;
1701 struct ZoneToNameResponseMessage *ztnr_msg;
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1704 "Received ZONE_TO_NAME message\n");
1705 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
1707 ztn_ctx.success = GNUNET_NO;
1708 if (GNUNET_SYSERR ==
1709 GSN_database->zone_to_name (GSN_database->cls,
1711 &ztn_msg->value_zone,
1712 &handle_zone_to_name_it, &ztn_ctx))
1714 /* internal error, hang up instead of signalling something
1715 that might be wrong */
1717 GNUNET_SERVICE_client_drop (nc->client);
1720 if (GNUNET_NO == ztn_ctx.success)
1722 /* no result found, send empty response */
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1724 "Found no result for zone-to-name lookup.\n");
1725 env = GNUNET_MQ_msg (ztnr_msg,
1726 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1727 ztnr_msg->gns_header.r_id = ztn_msg->gns_header.r_id;
1728 ztnr_msg->res = htons (GNUNET_NO);
1729 GNUNET_MQ_send (nc->mq,
1732 GNUNET_SERVICE_client_continue (nc->client);
1737 * Context for record remove operations passed from
1738 * #run_zone_iteration_round to #zone_iterate_proc as closure
1740 struct ZoneIterationProcResult
1743 * The zone iteration handle
1745 struct ZoneIteration *zi;
1748 * Number of results left to be returned in this iteration.
1756 * Process results for zone iteration from database
1758 * @param cls struct ZoneIterationProcResult
1759 * @param seq sequence number of the record
1760 * @param zone_key the zone key
1762 * @param rd_count number of records for this name
1763 * @param rd record data
1766 zone_iterate_proc (void *cls,
1768 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1770 unsigned int rd_count,
1771 const struct GNUNET_GNSRECORD_Data *rd)
1773 struct ZoneIterationProcResult *proc = cls;
1774 int do_refresh_block;
1776 if ( (NULL == zone_key) &&
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1780 "Iteration done\n");
1783 if ( (NULL == zone_key) ||
1786 /* what is this!? should never happen */
1790 if (0 == proc->limit)
1792 /* what is this!? should never happen */
1797 proc->zi->seq = seq;
1798 send_lookup_response (proc->zi->nc,
1799 proc->zi->request_id,
1806 do_refresh_block = GNUNET_NO;
1807 for (unsigned int i=0;i<rd_count;i++)
1808 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
1810 do_refresh_block = GNUNET_YES;
1813 if (GNUNET_YES == do_refresh_block)
1814 refresh_block (NULL,
1824 * Perform the next round of the zone iteration.
1826 * @param zi zone iterator to process
1827 * @param limit number of results to return in one pass
1830 run_zone_iteration_round (struct ZoneIteration *zi,
1833 struct ZoneIterationProcResult proc;
1834 struct GNUNET_MQ_Envelope *env;
1835 struct GNUNET_NAMESTORE_Header *em;
1836 struct GNUNET_TIME_Absolute start;
1837 struct GNUNET_TIME_Relative duration;
1842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1843 "Asked to return up to %llu records at position %llu\n",
1844 (unsigned long long) limit,
1845 (unsigned long long) zi->seq);
1848 start = GNUNET_TIME_absolute_get ();
1849 GNUNET_break (GNUNET_SYSERR !=
1850 GSN_database->iterate_records (GSN_database->cls,
1851 (0 == memcmp (&zi->zone,
1860 duration = GNUNET_TIME_absolute_get_duration (start);
1861 duration = GNUNET_TIME_relative_divide (duration,
1862 limit - proc.limit);
1863 GNUNET_STATISTICS_set (statistics,
1864 "NAMESTORE iteration delay (μs/record)",
1865 duration.rel_value_us,
1867 if (0 == proc.limit)
1869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1870 "Returned %llu results, more results available\n",
1871 (unsigned long long) limit);
1872 return; /* more results later after we get the
1873 #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message */
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 "Completed iteration after %llu/%llu results\n",
1877 (unsigned long long) (limit - proc.limit),
1878 (unsigned long long) limit);
1879 /* send empty response to indicate end of list */
1880 env = GNUNET_MQ_msg (em,
1881 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END);
1882 em->r_id = htonl (zi->request_id);
1883 GNUNET_MQ_send (zi->nc->mq,
1885 GNUNET_CONTAINER_DLL_remove (zi->nc->op_head,
1893 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1895 * @param cls the client sending the message
1896 * @param zis_msg message from the client
1899 handle_iteration_start (void *cls,
1900 const struct ZoneIterationStartMessage *zis_msg)
1902 struct NamestoreClient *nc = cls;
1903 struct ZoneIteration *zi;
1905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1906 "Received ZONE_ITERATION_START message\n");
1907 zi = GNUNET_new (struct ZoneIteration);
1908 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1911 zi->zone = zis_msg->zone;
1913 GNUNET_CONTAINER_DLL_insert (nc->op_head,
1916 run_zone_iteration_round (zi,
1918 GNUNET_SERVICE_client_continue (nc->client);
1923 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1925 * @param cls the client sending the message
1926 * @param zis_msg message from the client
1929 handle_iteration_stop (void *cls,
1930 const struct ZoneIterationStopMessage *zis_msg)
1932 struct NamestoreClient *nc = cls;
1933 struct ZoneIteration *zi;
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1937 "Received ZONE_ITERATION_STOP message\n");
1938 rid = ntohl (zis_msg->gns_header.r_id);
1939 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1940 if (zi->request_id == rid)
1945 GNUNET_SERVICE_client_drop (nc->client);
1948 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1952 GNUNET_SERVICE_client_continue (nc->client);
1957 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1959 * @param cls the client sending the message
1960 * @param message message from the client
1963 handle_iteration_next (void *cls,
1964 const struct ZoneIterationNextMessage *zis_msg)
1966 struct NamestoreClient *nc = cls;
1967 struct ZoneIteration *zi;
1971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1972 "Received ZONE_ITERATION_NEXT message\n");
1973 GNUNET_STATISTICS_update (statistics,
1974 "Iteration NEXT messages received",
1977 rid = ntohl (zis_msg->gns_header.r_id);
1978 limit = GNUNET_ntohll (zis_msg->limit);
1979 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1980 if (zi->request_id == rid)
1985 GNUNET_SERVICE_client_drop (nc->client);
1988 run_zone_iteration_round (zi,
1990 GNUNET_SERVICE_client_continue (nc->client);
1995 * Function called when the monitor is ready for more data, and we
1996 * should thus unblock PUT operations that were blocked on the
1997 * monitor not being ready.
2000 monitor_unblock (struct ZoneMonitor *zm)
2002 struct StoreActivity *sa = sa_head;
2004 while ( (NULL != sa) &&
2005 (zm->limit > zm->iteration_cnt) )
2007 struct StoreActivity *sn = sa->next;
2009 if (sa->zm_pos == zm)
2010 continue_store_activity (sa);
2013 if (zm->limit > zm->iteration_cnt)
2015 zm->sa_waiting = GNUNET_NO;
2016 if (NULL != zm->sa_wait_warning)
2018 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
2019 zm->sa_wait_warning = NULL;
2022 else if (GNUNET_YES == zm->sa_waiting)
2024 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
2025 if (NULL != zm->sa_wait_warning)
2026 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
2027 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
2035 * Send 'sync' message to zone monitor, we're now in sync.
2037 * @param zm monitor that is now in sync
2040 monitor_sync (struct ZoneMonitor *zm)
2042 struct GNUNET_MQ_Envelope *env;
2043 struct GNUNET_MessageHeader *sync;
2045 env = GNUNET_MQ_msg (sync,
2046 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
2047 GNUNET_MQ_send (zm->nc->mq,
2049 /* mark iteration done */
2050 zm->in_first_iteration = GNUNET_NO;
2051 zm->iteration_cnt = 0;
2052 if ( (zm->limit > 0) &&
2054 monitor_unblock (zm);
2059 * Obtain the next datum during the zone monitor's zone initial iteration.
2061 * @param cls zone monitor that does its initial iteration
2064 monitor_iteration_next (void *cls);
2068 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
2070 * @param cls a 'struct ZoneMonitor *' with information about the monitor
2071 * @param seq sequence number of the record
2072 * @param zone_key zone key of the zone
2074 * @param rd_count number of records in @a rd
2075 * @param rd array of records
2078 monitor_iterate_cb (void *cls,
2080 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
2082 unsigned int rd_count,
2083 const struct GNUNET_GNSRECORD_Data *rd)
2085 struct ZoneMonitor *zm = cls;
2088 GNUNET_assert (NULL != name);
2089 GNUNET_STATISTICS_update (statistics,
2090 "Monitor notifications sent",
2094 zm->iteration_cnt--;
2095 send_lookup_response (zm->nc,
2101 if ( (0 == zm->iteration_cnt) &&
2104 /* We are done with the current iteration batch, AND the
2105 client would right now accept more, so go again! */
2106 GNUNET_assert (NULL == zm->task);
2107 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2114 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
2116 * @param cls the client sending the message
2117 * @param zis_msg message from the client
2120 handle_monitor_start (void *cls,
2121 const struct ZoneMonitorStartMessage *zis_msg)
2123 struct NamestoreClient *nc = cls;
2124 struct ZoneMonitor *zm;
2126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2127 "Received ZONE_MONITOR_START message\n");
2128 zm = GNUNET_new (struct ZoneMonitor);
2130 zm->zone = zis_msg->zone;
2132 zm->in_first_iteration = (GNUNET_YES == ntohl (zis_msg->iterate_first));
2133 GNUNET_CONTAINER_DLL_insert (monitor_head,
2136 GNUNET_SERVICE_client_mark_monitor (nc->client);
2137 GNUNET_SERVICE_client_continue (nc->client);
2138 GNUNET_notification_context_add (monitor_nc,
2140 if (zm->in_first_iteration)
2141 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2149 * Obtain the next datum during the zone monitor's zone initial iteration.
2151 * @param cls zone monitor that does its initial iteration
2154 monitor_iteration_next (void *cls)
2156 struct ZoneMonitor *zm = cls;
2160 GNUNET_assert (0 == zm->iteration_cnt);
2162 zm->iteration_cnt = zm->limit / 2; /* leave half for monitor events */
2164 zm->iteration_cnt = zm->limit; /* use it all */
2165 ret = GSN_database->iterate_records (GSN_database->cls,
2166 (0 == memcmp (&zm->zone,
2173 &monitor_iterate_cb,
2175 if (GNUNET_SYSERR == ret)
2177 GNUNET_SERVICE_client_drop (zm->nc->client);
2180 if (GNUNET_NO == ret)
2190 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT message
2192 * @param cls the client sending the message
2193 * @param nm message from the client
2196 handle_monitor_next (void *cls,
2197 const struct ZoneMonitorNextMessage *nm)
2199 struct NamestoreClient *nc = cls;
2200 struct ZoneMonitor *zm;
2203 inc = GNUNET_ntohll (nm->limit);
2204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2205 "Received ZONE_MONITOR_NEXT message with limit %llu\n",
2206 (unsigned long long) inc);
2207 for (zm = monitor_head; NULL != zm; zm = zm->next)
2213 GNUNET_SERVICE_client_drop (nc->client);
2216 GNUNET_SERVICE_client_continue (nc->client);
2217 if (zm->limit + inc < zm->limit)
2220 GNUNET_SERVICE_client_drop (nc->client);
2224 if ( (zm->in_first_iteration) &&
2225 (zm->limit == inc) )
2227 /* We are still iterating, and the previous iteration must
2228 have stopped due to the client's limit, so continue it! */
2229 GNUNET_assert (NULL == zm->task);
2230 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2233 GNUNET_assert (zm->iteration_cnt <= zm->limit);
2234 if ( (zm->limit > zm->iteration_cnt) &&
2237 monitor_unblock (zm);
2239 else if (GNUNET_YES == zm->sa_waiting)
2241 if (NULL != zm->sa_wait_warning)
2242 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
2243 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
2244 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
2252 * Process namestore requests.
2254 * @param cls closure
2255 * @param cfg configuration to use
2256 * @param service the initialized service
2260 const struct GNUNET_CONFIGURATION_Handle *cfg,
2261 struct GNUNET_SERVICE_Handle *service)
2267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2268 "Starting namestore service\n");
2269 cache_keys = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2272 disable_namecache = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2276 monitor_nc = GNUNET_notification_context_create (1);
2277 if (GNUNET_YES != disable_namecache)
2279 namecache = GNUNET_NAMECACHE_connect (cfg);
2280 GNUNET_assert (NULL != namecache);
2282 /* Loading database plugin */
2284 GNUNET_CONFIGURATION_get_value_string (cfg,
2288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2289 "No database backend configured\n");
2291 GNUNET_asprintf (&db_lib_name,
2292 "libgnunet_plugin_namestore_%s",
2294 GSN_database = GNUNET_PLUGIN_load (db_lib_name,
2296 GNUNET_free (database);
2297 statistics = GNUNET_STATISTICS_create ("namestore",
2299 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
2301 if (NULL == GSN_database)
2303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2304 "Could not load database backend `%s'\n",
2306 GNUNET_SCHEDULER_shutdown ();
2313 * Define "main" method using service macro.
2317 GNUNET_SERVICE_OPTION_NONE,
2320 &client_disconnect_cb,
2322 GNUNET_MQ_hd_var_size (record_store,
2323 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE,
2324 struct RecordStoreMessage,
2326 GNUNET_MQ_hd_var_size (record_lookup,
2327 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP,
2328 struct LabelLookupMessage,
2330 GNUNET_MQ_hd_fixed_size (zone_to_name,
2331 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME,
2332 struct ZoneToNameMessage,
2334 GNUNET_MQ_hd_fixed_size (iteration_start,
2335 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START,
2336 struct ZoneIterationStartMessage,
2338 GNUNET_MQ_hd_fixed_size (iteration_next,
2339 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT,
2340 struct ZoneIterationNextMessage,
2342 GNUNET_MQ_hd_fixed_size (iteration_stop,
2343 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP,
2344 struct ZoneIterationStopMessage,
2346 GNUNET_MQ_hd_fixed_size (monitor_start,
2347 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START,
2348 struct ZoneMonitorStartMessage,
2350 GNUNET_MQ_hd_fixed_size (monitor_next,
2351 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT,
2352 struct ZoneMonitorNextMessage,
2354 GNUNET_MQ_handler_end ());
2357 /* end of gnunet-service-namestore.c */