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
53 struct NamestoreClient;
57 * A namestore iteration operation.
62 * Next element in the DLL
64 struct ZoneIteration *next;
67 * Previous element in the DLL
69 struct ZoneIteration *prev;
72 * Namestore client which intiated this zone iteration
74 struct NamestoreClient *nc;
77 * The nick to add to the records
79 struct GNUNET_GNSRECORD_Data *nick;
82 * Key of the zone we are iterating over.
84 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
87 * Last sequence number in the zone iteration used to address next
88 * result of the zone iteration in the store
91 * Updated in #zone_iterate_proc()
96 * The operation id fot the zone iteration in the response for the client
101 * Offset of the zone iteration used to address next result of the zone
102 * iteration in the store
104 * Initialy set to 0 in #handle_iteration_start
105 * Incremented with by every call to #handle_iteration_next
115 struct NamestoreClient
121 struct GNUNET_SERVICE_Client *client;
124 * Message queue for transmission to @e client
126 struct GNUNET_MQ_Handle *mq;
130 * Zone iteration operations in progress initiated by this client
132 struct ZoneIteration *op_head;
136 * Zone iteration operations in progress initiated by this client
138 struct ZoneIteration *op_tail;
143 * A namestore monitor.
148 * Next element in the DLL
150 struct ZoneMonitor *next;
153 * Previous element in the DLL
155 struct ZoneMonitor *prev;
158 * Namestore client which intiated this zone monitor
160 struct NamestoreClient *nc;
163 * Private key of the zone.
165 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
168 * Task active during initial iteration.
170 struct GNUNET_SCHEDULER_Task *task;
173 * Task to warn about slow monitors.
175 struct GNUNET_SCHEDULER_Task *sa_wait_warning;
178 * Since when are we blocked on this monitor?
180 struct GNUNET_TIME_Absolute sa_waiting_start;
183 * Last sequence number in the zone iteration used to address next
184 * result of the zone iteration in the store
187 * Updated in #monitor_iterate_cb()
192 * Current limit of how many more messages we are allowed
193 * to queue to this monitor.
198 * How many more requests may we receive from the iterator
199 * before it is at the limit we gave it? Will be below or
200 * equal to @e limit. The effective limit for monitor
201 * events is thus @e iteration_cnt - @e limit!
203 uint64_t iteration_cnt;
206 * Are we (still) in the initial iteration pass?
208 int in_first_iteration;
211 * Is there a store activity waiting for this monitor? We only raise the
212 * flag when it happens and search the DLL for the store activity when we
213 * had a limit increase. If we cannot find any waiting store activity at
214 * that time, we clear the flag again.
222 * Pending operation on the namecache.
224 struct CacheOperation
230 struct CacheOperation *prev;
235 struct CacheOperation *next;
238 * Handle to namecache queue.
240 struct GNUNET_NAMECACHE_QueueEntry *qe;
243 * Client to notify about the result.
245 struct NamestoreClient *nc;
248 * Client's request ID.
255 * Information for an ongoing #handle_record_store() operation.
256 * Needed as we may wait for monitors to be ready for the notification.
263 struct StoreActivity *next;
268 struct StoreActivity *prev;
271 * Which client triggered the store activity?
273 struct NamestoreClient *nc;
276 * Copy of the original store message (as data fields in @e rd will
279 const struct RecordStoreMessage *rsm;
282 * Next zone monitor that still needs to be notified about this PUT.
284 struct ZoneMonitor *zm_pos;
287 * Label nicely canonicalized (lower case).
295 * Public key of all zeros.
297 static const struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
300 * Configuration handle.
302 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
305 * Handle to the statistics service
307 static struct GNUNET_STATISTICS_Handle *statistics;
312 static struct GNUNET_NAMECACHE_Handle *namecache;
317 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
320 * Name of the database plugin
322 static char *db_lib_name;
327 static struct CacheOperation *cop_head;
332 static struct CacheOperation *cop_tail;
335 * First active zone monitor.
337 static struct ZoneMonitor *monitor_head;
340 * Last active zone monitor.
342 static struct ZoneMonitor *monitor_tail;
345 * Head of DLL of monitor-blocked store activities.
347 static struct StoreActivity *sa_head;
350 * Tail of DLL of monitor-blocked store activities.
352 static struct StoreActivity *sa_tail;
355 * Notification context shared by all monitors.
357 static struct GNUNET_NotificationContext *monitor_nc;
360 * Optimize block insertion by caching map of private keys to
361 * public keys in memory?
363 static int cache_keys;
366 * Use the namecache? Doing so creates additional cryptographic
367 * operations whenever we touch a record.
369 static int disable_namecache;
373 * Task run during shutdown.
378 cleanup_task (void *cls)
380 struct CacheOperation *cop;
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Stopping namestore service\n");
385 while (NULL != (cop = cop_head))
387 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
388 "Aborting incomplete namecache operation\n");
389 GNUNET_NAMECACHE_cancel (cop->qe);
390 GNUNET_CONTAINER_DLL_remove (cop_head,
395 if (NULL != namecache)
397 GNUNET_NAMECACHE_disconnect (namecache);
400 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name,
402 GNUNET_free (db_lib_name);
404 if (NULL != monitor_nc)
406 GNUNET_notification_context_destroy (monitor_nc);
409 if (NULL != statistics)
411 GNUNET_STATISTICS_destroy (statistics,
419 * Release memory used by @a sa.
421 * @param sa activity to free
424 free_store_activity (struct StoreActivity *sa)
426 GNUNET_CONTAINER_DLL_remove (sa_head,
429 GNUNET_free (sa->conv_name);
435 * Function called with the records for the #GNUNET_GNS_EMPTY_LABEL_AT
436 * label in the zone. Used to locate the #GNUNET_GNSRECORD_TYPE_NICK
437 * record, which (if found) is then copied to @a cls for future use.
439 * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
440 * @param seq sequence number of the record
441 * @param private_key the private key of the zone (unused)
442 * @param label should be #GNUNET_GNS_EMPTY_LABEL_AT
443 * @param rd_count number of records in @a rd
444 * @param rd records stored under @a label in the zone
447 lookup_nick_it (void *cls,
449 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
451 unsigned int rd_count,
452 const struct GNUNET_GNSRECORD_Data *rd)
454 struct GNUNET_GNSRECORD_Data **res = cls;
458 if (0 != strcmp (label, GNUNET_GNS_EMPTY_LABEL_AT))
463 for (unsigned int c = 0; c < rd_count; c++)
465 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
467 (*res) = GNUNET_malloc (rd[c].data_size + sizeof (struct GNUNET_GNSRECORD_Data));
468 (*res)->data = &(*res)[1];
469 GNUNET_memcpy ((void *) (*res)->data,
472 (*res)->data_size = rd[c].data_size;
473 (*res)->expiration_time = rd[c].expiration_time;
474 (*res)->flags = rd[c].flags;
475 (*res)->record_type = GNUNET_GNSRECORD_TYPE_NICK;
484 * Return the NICK record for the zone (if it exists).
486 * @param zone private key for the zone to look for nick
487 * @return NULL if no NICK record was found
489 static struct GNUNET_GNSRECORD_Data *
490 get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
492 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
493 struct GNUNET_GNSRECORD_Data *nick;
497 res = GSN_database->lookup_records (GSN_database->cls,
499 GNUNET_GNS_EMPTY_LABEL_AT,
502 if ( (GNUNET_OK != res) ||
505 GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
507 "No nick name set for zone `%s'\n",
508 GNUNET_GNSRECORD_z2s (&pub));
516 * Merge the nick record @a nick_rd with the rest of the
517 * record set given in @a rd2. Store the result in @a rdc_res
518 * and @a rd_res. The @a nick_rd's expiration time is set to
519 * the maximum expiration time of all of the records in @a rd2.
521 * @param nick_rd the nick record to integrate
522 * @param rd2_length length of the @a rd2 array
523 * @param rd2 array of records
524 * @param rdc_res[out] length of the resulting @a rd_res array
525 * @param rd_res[out] set to an array of records,
526 * including @a nick_rd and @a rd2;
527 * all of the variable-size 'data' fields in @a rd2 are
528 * allocated in the same chunk of memory!
531 merge_with_nick_records (const struct GNUNET_GNSRECORD_Data *nick_rd,
532 unsigned int rd2_length,
533 const struct GNUNET_GNSRECORD_Data *rd2,
534 unsigned int *rdc_res,
535 struct GNUNET_GNSRECORD_Data **rd_res)
537 uint64_t latest_expiration;
541 struct GNUNET_GNSRECORD_Data *target;
543 (*rdc_res) = 1 + rd2_length;
544 if (0 == 1 + rd2_length)
550 req = sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd->data_size;
551 for (unsigned int i=0; i<rd2_length; i++)
553 const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
555 if (req + sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size < req)
561 req += sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size;
563 target = GNUNET_malloc (req);
565 data = (char *) &target[1 + rd2_length];
567 latest_expiration = 0;
568 for (unsigned int i=0;i<rd2_length;i++)
570 const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
572 if (0 != (orig->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
574 if ((GNUNET_TIME_absolute_get().abs_value_us + orig->expiration_time) >
576 latest_expiration = orig->expiration_time;
578 else if (orig->expiration_time > latest_expiration)
579 latest_expiration = orig->expiration_time;
581 target[i].data = (void *) &data[data_offset];
582 GNUNET_memcpy (&data[data_offset],
585 data_offset += orig->data_size;
588 target[rd2_length] = *nick_rd;
589 target[rd2_length].expiration_time = latest_expiration;
590 target[rd2_length].data = (void *) &data[data_offset];
591 GNUNET_memcpy (&data[data_offset],
594 data_offset += nick_rd->data_size;
595 GNUNET_assert (req ==
596 (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
601 * Generate a `struct LookupNameResponseMessage` and send it to the
602 * given client using the given notification context.
604 * @param nc client to unicast to
605 * @param request_id request ID to use
606 * @param zone_key zone key of the zone
608 * @param rd_count number of records in @a rd
609 * @param rd array of records
612 send_lookup_response (struct NamestoreClient *nc,
614 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
616 unsigned int rd_count,
617 const struct GNUNET_GNSRECORD_Data *rd)
619 struct GNUNET_MQ_Envelope *env;
620 struct RecordResultMessage *zir_msg;
621 struct GNUNET_GNSRECORD_Data *nick;
622 struct GNUNET_GNSRECORD_Data *res;
623 unsigned int res_count;
629 nick = get_nick_record (zone_key);
631 GNUNET_GNSRECORD_records_get_size (rd_count,
634 if ( (NULL != nick) &&
636 GNUNET_GNS_EMPTY_LABEL_AT)))
638 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
639 merge_with_nick_records (nick,
648 res_count = rd_count;
649 res = (struct GNUNET_GNSRECORD_Data *) rd;
653 GNUNET_GNSRECORD_records_get_size (res_count,
657 name_len = strlen (name) + 1;
658 rd_ser_len = GNUNET_GNSRECORD_records_get_size (res_count,
663 GNUNET_SERVICE_client_drop (nc->client);
666 if (rd_ser_len >= UINT16_MAX - name_len - sizeof (*zir_msg))
669 GNUNET_SERVICE_client_drop (nc->client);
672 env = GNUNET_MQ_msg_extra (zir_msg,
673 name_len + rd_ser_len,
674 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
675 zir_msg->gns_header.r_id = htonl (request_id);
676 zir_msg->name_len = htons (name_len);
677 zir_msg->rd_count = htons (res_count);
678 zir_msg->rd_len = htons ((uint16_t) rd_ser_len);
679 zir_msg->private_key = *zone_key;
680 name_tmp = (char *) &zir_msg[1];
681 GNUNET_memcpy (name_tmp,
684 rd_ser = &name_tmp[name_len];
685 GNUNET_assert (rd_ser_len ==
686 GNUNET_GNSRECORD_records_serialize (res_count,
690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
691 "Sending RECORD_RESULT message with %u records\n",
693 GNUNET_STATISTICS_update (statistics,
694 "Record sets sent to clients",
697 GNUNET_MQ_send (nc->mq,
705 * Send response to the store request to the client.
707 * @param client client to talk to
708 * @param res status of the operation
709 * @param rid client's request ID
712 send_store_response (struct NamestoreClient *nc,
716 struct GNUNET_MQ_Envelope *env;
717 struct RecordStoreResponseMessage *rcr_msg;
719 GNUNET_assert (NULL != nc);
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
721 "Sending RECORD_STORE_RESPONSE message\n");
722 GNUNET_STATISTICS_update (statistics,
723 "Store requests completed",
726 env = GNUNET_MQ_msg (rcr_msg,
727 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
728 rcr_msg->gns_header.r_id = htonl (rid);
729 rcr_msg->op_result = htonl (res);
730 GNUNET_MQ_send (nc->mq,
736 * Cache operation complete, clean up.
738 * @param cls the `struct CacheOperation`
739 * @param success success
740 * @param emsg error messages
743 finish_cache_operation (void *cls,
747 struct CacheOperation *cop = cls;
750 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
751 _("Failed to replicate block in namecache: %s\n"),
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
755 "CACHE operation completed\n");
756 GNUNET_CONTAINER_DLL_remove (cop_head,
760 send_store_response (cop->nc,
768 * We just touched the plaintext information about a name in our zone;
769 * refresh the corresponding (encrypted) block in the namecache.
771 * @param nc client responsible for the request, can be NULL
772 * @param rid request ID of the client
773 * @param zone_key private key of the zone
774 * @param name label for the records
775 * @param rd_count number of records
776 * @param rd records stored under the given @a name
779 refresh_block (struct NamestoreClient *nc,
781 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
783 unsigned int rd_count,
784 const struct GNUNET_GNSRECORD_Data *rd)
786 struct GNUNET_GNSRECORD_Block *block;
787 struct CacheOperation *cop;
788 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
789 struct GNUNET_GNSRECORD_Data *nick;
790 struct GNUNET_GNSRECORD_Data *res;
791 unsigned int res_count;
792 struct GNUNET_TIME_Absolute exp_time;
794 nick = get_nick_record (zone_key);
795 res_count = rd_count;
796 res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
799 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
800 merge_with_nick_records (nick,
809 send_store_response (nc,
812 return; /* no data, no need to update cache */
814 if (GNUNET_YES == disable_namecache)
816 GNUNET_STATISTICS_update (statistics,
817 "Namecache updates skipped (NC disabled)",
821 send_store_response (nc,
826 exp_time = GNUNET_GNSRECORD_record_get_expiration_time (res_count,
829 block = GNUNET_GNSRECORD_block_create2 (zone_key,
835 block = GNUNET_GNSRECORD_block_create (zone_key,
840 GNUNET_assert (NULL != block);
841 GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844 "Caching block for label `%s' with %u records and expiration %s in zone `%s' in namecache\n",
847 GNUNET_STRINGS_absolute_time_to_string (exp_time),
848 GNUNET_GNSRECORD_z2s (&pkey));
849 GNUNET_STATISTICS_update (statistics,
850 "Namecache updates pushed",
853 cop = GNUNET_new (struct CacheOperation);
856 GNUNET_CONTAINER_DLL_insert (cop_head,
859 cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
861 &finish_cache_operation,
868 * Print a warning that one of our monitors is no longer reacting.
870 * @param cls a `struct ZoneMonitor` to warn about
873 warn_monitor_slow (void *cls)
875 struct ZoneMonitor *zm = cls;
877 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
878 "No response from monitor since %s\n",
879 GNUNET_STRINGS_absolute_time_to_string (zm->sa_waiting_start));
880 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
887 * Continue processing the @a sa.
889 * @param sa store activity to process
892 continue_store_activity (struct StoreActivity *sa)
894 const struct RecordStoreMessage *rp_msg = sa->rsm;
895 unsigned int rd_count;
899 const char *name_tmp;
902 rid = ntohl (rp_msg->gns_header.r_id);
903 name_len = ntohs (rp_msg->name_len);
904 rd_count = ntohs (rp_msg->rd_count);
905 rd_ser_len = ntohs (rp_msg->rd_len);
906 name_tmp = (const char *) &rp_msg[1];
907 rd_ser = &name_tmp[name_len];
909 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
911 /* We did this before, must succeed again */
912 GNUNET_assert (GNUNET_OK ==
913 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
918 for (struct ZoneMonitor *zm = sa->zm_pos;
922 if ( (0 != memcmp (&rp_msg->private_key,
924 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) &&
925 (0 != memcmp (&zm->zone,
927 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
929 sa->zm_pos = zm->next; /* not interesting to this monitor */
932 if (zm->limit == zm->iteration_cnt)
934 zm->sa_waiting = GNUNET_YES;
935 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
936 if (NULL != zm->sa_wait_warning)
937 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
938 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
941 return; /* blocked on zone monitor */
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 "Notifying monitor about changes under label `%s'\n",
947 send_lookup_response (zm->nc,
949 &rp_msg->private_key,
953 sa->zm_pos = zm->next;
955 /* great, done with the monitors, unpack (again) for refresh_block operation */
956 refresh_block (sa->nc,
958 &rp_msg->private_key,
963 GNUNET_SERVICE_client_continue (sa->nc->client);
964 free_store_activity (sa);
969 * Called whenever a client is disconnected.
970 * Frees our resources associated with that client.
973 * @param client identification of the client
974 * @param app_ctx the `struct NamestoreClient` of @a client
977 client_disconnect_cb (void *cls,
978 struct GNUNET_SERVICE_Client *client,
981 struct NamestoreClient *nc = app_ctx;
982 struct ZoneIteration *no;
983 struct CacheOperation *cop;
986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
987 "Client %p disconnected\n",
989 for (struct ZoneMonitor *zm = monitor_head; NULL != zm; zm = zm->next)
991 struct StoreActivity *san;
995 GNUNET_CONTAINER_DLL_remove (monitor_head,
998 if (NULL != zm->task)
1000 GNUNET_SCHEDULER_cancel (zm->task);
1003 if (NULL != zm->sa_wait_warning)
1005 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1006 zm->sa_wait_warning = NULL;
1008 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = san)
1011 if (zm == sa->zm_pos)
1013 sa->zm_pos = zm->next;
1014 /* this may free sa */
1015 continue_store_activity (sa);
1021 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = sa->next)
1025 /* this may free sa */
1026 free_store_activity (sa);
1027 break; /* there can only be one per nc */
1030 while (NULL != (no = nc->op_head))
1032 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1037 for (cop = cop_head; NULL != cop; cop = cop->next)
1045 * Add a client to our list of active clients.
1048 * @param client client to add
1049 * @param mq message queue for @a client
1050 * @return internal namestore client structure for this client
1053 client_connect_cb (void *cls,
1054 struct GNUNET_SERVICE_Client *client,
1055 struct GNUNET_MQ_Handle *mq)
1057 struct NamestoreClient *nc;
1060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1061 "Client %p connected\n",
1063 nc = GNUNET_new (struct NamestoreClient);
1064 nc->client = client;
1071 * Closure for #lookup_it().
1073 struct RecordLookupContext
1089 struct GNUNET_GNSRECORD_Data *nick;
1099 unsigned int res_rd_count;
1111 * @param seq sequence number of the record
1114 lookup_it (void *cls,
1116 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
1118 unsigned int rd_count,
1119 const struct GNUNET_GNSRECORD_Data *rd)
1121 struct RecordLookupContext *rlc = cls;
1125 if (0 != strcmp (label,
1128 rlc->found = GNUNET_YES;
1131 rlc->rd_ser_len = 0;
1132 rlc->res_rd_count = 0;
1136 if ( (NULL != rlc->nick) &&
1137 (0 != strcmp (label,
1138 GNUNET_GNS_EMPTY_LABEL_AT)) )
1141 struct GNUNET_GNSRECORD_Data *rd_res;
1142 unsigned int rdc_res;
1146 rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
1147 merge_with_nick_records (rlc->nick,
1152 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res,
1154 if (rlc->rd_ser_len < 0)
1157 GNUNET_free (rd_res);
1158 rlc->found = GNUNET_NO;
1159 rlc->rd_ser_len = 0;
1162 rlc->res_rd_count = rdc_res;
1163 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1164 if (rlc->rd_ser_len !=
1165 GNUNET_GNSRECORD_records_serialize (rdc_res,
1171 GNUNET_free (rlc->res_rd);
1173 rlc->res_rd_count = 0;
1174 rlc->rd_ser_len = 0;
1175 GNUNET_free (rd_res);
1176 rlc->found = GNUNET_NO;
1179 GNUNET_free (rd_res);
1180 GNUNET_free (rlc->nick);
1185 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1187 if (rlc->rd_ser_len < 0)
1190 rlc->found = GNUNET_NO;
1191 rlc->rd_ser_len = 0;
1194 rlc->res_rd_count = rd_count;
1195 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1196 if (rlc->rd_ser_len !=
1197 GNUNET_GNSRECORD_records_serialize (rd_count,
1203 GNUNET_free (rlc->res_rd);
1205 rlc->res_rd_count = 0;
1206 rlc->rd_ser_len = 0;
1207 rlc->found = GNUNET_NO;
1215 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1217 * @param cls client sending the message
1218 * @param ll_msg message of type `struct LabelLookupMessage`
1219 * @return #GNUNET_OK if @a ll_msg is well-formed
1222 check_record_lookup (void *cls,
1223 const struct LabelLookupMessage *ll_msg)
1229 name_len = ntohl (ll_msg->label_len);
1230 src_size = ntohs (ll_msg->gns_header.header.size);
1231 if (name_len != src_size - sizeof (struct LabelLookupMessage))
1234 return GNUNET_SYSERR;
1236 GNUNET_MQ_check_zero_termination (ll_msg);
1242 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1244 * @param cls client sending the message
1245 * @param ll_msg message of type `struct LabelLookupMessage`
1248 handle_record_lookup (void *cls,
1249 const struct LabelLookupMessage *ll_msg)
1251 struct NamestoreClient *nc = cls;
1252 struct GNUNET_MQ_Envelope *env;
1253 struct LabelLookupResponseMessage *llr_msg;
1254 struct RecordLookupContext rlc;
1255 const char *name_tmp;
1261 name_len = ntohl (ll_msg->label_len);
1262 name_tmp = (const char *) &ll_msg[1];
1263 GNUNET_SERVICE_client_continue (nc->client);
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 "Received NAMESTORE_RECORD_LOOKUP message for name `%s'\n",
1268 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1269 if (NULL == conv_name)
1271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1272 "Error converting name `%s'\n",
1274 GNUNET_SERVICE_client_drop (nc->client);
1277 rlc.label = conv_name;
1278 rlc.found = GNUNET_NO;
1279 rlc.res_rd_count = 0;
1282 rlc.nick = get_nick_record (&ll_msg->zone);
1283 res = GSN_database->lookup_records (GSN_database->cls,
1288 GNUNET_free (conv_name);
1289 env = GNUNET_MQ_msg_extra (llr_msg,
1290 name_len + rlc.rd_ser_len,
1291 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
1292 llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
1293 llr_msg->private_key = ll_msg->zone;
1294 llr_msg->name_len = htons (name_len);
1295 llr_msg->rd_count = htons (rlc.res_rd_count);
1296 llr_msg->rd_len = htons (rlc.rd_ser_len);
1297 res_name = (char *) &llr_msg[1];
1298 if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
1299 llr_msg->found = ntohs (GNUNET_YES);
1301 llr_msg->found = ntohs (GNUNET_NO);
1302 GNUNET_memcpy (&llr_msg[1],
1305 GNUNET_memcpy (&res_name[name_len],
1308 GNUNET_MQ_send (nc->mq,
1310 GNUNET_free_non_null (rlc.res_rd);
1315 * Checks a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1317 * @param cls client sending the message
1318 * @param rp_msg message of type `struct RecordStoreMessage`
1319 * @return #GNUNET_OK if @a rp_msg is well-formed
1322 check_record_store (void *cls,
1323 const struct RecordStoreMessage *rp_msg)
1327 size_t msg_size_exp;
1329 const char *name_tmp;
1332 name_len = ntohs (rp_msg->name_len);
1333 msg_size = ntohs (rp_msg->gns_header.header.size);
1334 rd_ser_len = ntohs (rp_msg->rd_len);
1335 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1336 if (msg_size != msg_size_exp)
1339 return GNUNET_SYSERR;
1341 if ( (0 == name_len) ||
1342 (name_len > MAX_NAME_LEN) )
1345 return GNUNET_SYSERR;
1347 name_tmp = (const char *) &rp_msg[1];
1348 if ('\0' != name_tmp[name_len -1])
1351 return GNUNET_SYSERR;
1358 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1360 * @param cls client sending the message
1361 * @param rp_msg message of type `struct RecordStoreMessage`
1364 handle_record_store (void *cls,
1365 const struct RecordStoreMessage *rp_msg)
1367 struct NamestoreClient *nc = cls;
1371 const char *name_tmp;
1374 unsigned int rd_count;
1376 struct StoreActivity *sa;
1378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1379 "Received NAMESTORE_RECORD_STORE message\n");
1380 rid = ntohl (rp_msg->gns_header.r_id);
1381 name_len = ntohs (rp_msg->name_len);
1382 rd_count = ntohs (rp_msg->rd_count);
1383 rd_ser_len = ntohs (rp_msg->rd_len);
1384 GNUNET_break (0 == ntohs (rp_msg->reserved));
1385 name_tmp = (const char *) &rp_msg[1];
1386 rd_ser = &name_tmp[name_len];
1388 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
1391 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
1397 GNUNET_SERVICE_client_drop (nc->client);
1401 /* Extracting and converting private key */
1402 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1403 if (NULL == conv_name)
1405 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1406 "Error converting name `%s'\n",
1408 GNUNET_SERVICE_client_drop (nc->client);
1411 GNUNET_STATISTICS_update (statistics,
1412 "Well-formed store requests received",
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1416 "Creating %u records for name `%s'\n",
1417 (unsigned int) rd_count,
1419 if ( (0 == rd_count) &&
1421 GSN_database->lookup_records (GSN_database->cls,
1422 &rp_msg->private_key,
1427 /* This name does not exist, so cannot be removed */
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Name `%s' does not exist, no deletion required\n",
1435 /* remove "NICK" records, unless this is for the
1436 #GNUNET_GNS_EMPTY_LABEL_AT label */
1437 struct GNUNET_GNSRECORD_Data rd_clean[GNUNET_NZL(rd_count)];
1438 unsigned int rd_clean_off;
1441 for (unsigned int i=0;i<rd_count;i++)
1443 rd_clean[rd_clean_off] = rd[i];
1444 if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
1446 (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
1449 res = GSN_database->store_records (GSN_database->cls,
1450 &rp_msg->private_key,
1456 if (GNUNET_OK != res)
1458 /* store not successful, not need to tell monitors */
1459 send_store_response (nc,
1462 GNUNET_SERVICE_client_continue (nc->client);
1463 GNUNET_free (conv_name);
1467 sa = GNUNET_malloc (sizeof (struct StoreActivity) +
1468 ntohs (rp_msg->gns_header.header.size));
1469 GNUNET_CONTAINER_DLL_insert (sa_head,
1473 sa->rsm = (const struct RecordStoreMessage *) &sa[1];
1474 GNUNET_memcpy (&sa[1],
1476 ntohs (rp_msg->gns_header.header.size));
1477 sa->zm_pos = monitor_head;
1478 sa->conv_name = conv_name;
1479 continue_store_activity (sa);
1485 * Context for record remove operations passed from #handle_zone_to_name to
1486 * #handle_zone_to_name_it as closure
1488 struct ZoneToNameCtx
1493 struct NamestoreClient *nc;
1496 * Request id (to be used in the response to the client).
1501 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
1502 * not finding a name for the zone still counts as a 'success' here,
1503 * as this field is about the success of executing the IPC protocol.
1510 * Zone to name iterator
1512 * @param cls struct ZoneToNameCtx *
1513 * @param seq sequence number of the record
1514 * @param zone_key the zone key
1516 * @param rd_count number of records in @a rd
1517 * @param rd record data
1520 handle_zone_to_name_it (void *cls,
1522 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1524 unsigned int rd_count,
1525 const struct GNUNET_GNSRECORD_Data *rd)
1527 struct ZoneToNameCtx *ztn_ctx = cls;
1528 struct GNUNET_MQ_Envelope *env;
1529 struct ZoneToNameResponseMessage *ztnr_msg;
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539 "Found result for zone-to-name lookup: `%s'\n",
1542 name_len = (NULL == name) ? 0 : strlen (name) + 1;
1543 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1548 ztn_ctx->success = GNUNET_SYSERR;
1551 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
1552 if (msg_size >= GNUNET_MAX_MESSAGE_SIZE)
1555 ztn_ctx->success = GNUNET_SYSERR;
1558 env = GNUNET_MQ_msg_extra (ztnr_msg,
1559 name_len + rd_ser_len,
1560 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1561 ztnr_msg->gns_header.header.size = htons (msg_size);
1562 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1563 ztnr_msg->res = htons (res);
1564 ztnr_msg->rd_len = htons (rd_ser_len);
1565 ztnr_msg->rd_count = htons (rd_count);
1566 ztnr_msg->name_len = htons (name_len);
1567 ztnr_msg->zone = *zone_key;
1568 name_tmp = (char *) &ztnr_msg[1];
1569 GNUNET_memcpy (name_tmp,
1572 rd_tmp = &name_tmp[name_len];
1573 GNUNET_assert (rd_ser_len ==
1574 GNUNET_GNSRECORD_records_serialize (rd_count,
1578 ztn_ctx->success = GNUNET_OK;
1579 GNUNET_MQ_send (ztn_ctx->nc->mq,
1585 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
1587 * @param cls client client sending the message
1588 * @param ztn_msg message of type 'struct ZoneToNameMessage'
1591 handle_zone_to_name (void *cls,
1592 const struct ZoneToNameMessage *ztn_msg)
1594 struct NamestoreClient *nc = cls;
1595 struct ZoneToNameCtx ztn_ctx;
1596 struct GNUNET_MQ_Envelope *env;
1597 struct ZoneToNameResponseMessage *ztnr_msg;
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1600 "Received ZONE_TO_NAME message\n");
1601 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
1603 ztn_ctx.success = GNUNET_NO;
1604 if (GNUNET_SYSERR ==
1605 GSN_database->zone_to_name (GSN_database->cls,
1607 &ztn_msg->value_zone,
1608 &handle_zone_to_name_it, &ztn_ctx))
1610 /* internal error, hang up instead of signalling something
1611 that might be wrong */
1613 GNUNET_SERVICE_client_drop (nc->client);
1616 if (GNUNET_NO == ztn_ctx.success)
1618 /* no result found, send empty response */
1619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1620 "Found no result for zone-to-name lookup.\n");
1621 env = GNUNET_MQ_msg (ztnr_msg,
1622 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1623 ztnr_msg->gns_header.r_id = ztn_msg->gns_header.r_id;
1624 ztnr_msg->res = htons (GNUNET_NO);
1625 GNUNET_MQ_send (nc->mq,
1628 GNUNET_SERVICE_client_continue (nc->client);
1633 * Context for record remove operations passed from
1634 * #run_zone_iteration_round to #zone_iterate_proc as closure
1636 struct ZoneIterationProcResult
1639 * The zone iteration handle
1641 struct ZoneIteration *zi;
1644 * Number of results left to be returned in this iteration.
1652 * Process results for zone iteration from database
1654 * @param cls struct ZoneIterationProcResult
1655 * @param seq sequence number of the record
1656 * @param zone_key the zone key
1658 * @param rd_count number of records for this name
1659 * @param rd record data
1662 zone_iterate_proc (void *cls,
1664 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1666 unsigned int rd_count,
1667 const struct GNUNET_GNSRECORD_Data *rd)
1669 struct ZoneIterationProcResult *proc = cls;
1670 int do_refresh_block;
1672 if ( (NULL == zone_key) &&
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "Iteration done\n");
1679 if ( (NULL == zone_key) ||
1682 /* what is this!? should never happen */
1686 if (0 == proc->limit)
1688 /* what is this!? should never happen */
1693 proc->zi->seq = seq;
1694 send_lookup_response (proc->zi->nc,
1695 proc->zi->request_id,
1702 do_refresh_block = GNUNET_NO;
1703 for (unsigned int i=0;i<rd_count;i++)
1704 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
1706 do_refresh_block = GNUNET_YES;
1709 if (GNUNET_YES == do_refresh_block)
1710 refresh_block (NULL,
1720 * Perform the next round of the zone iteration.
1722 * @param zi zone iterator to process
1723 * @param limit number of results to return in one pass
1726 run_zone_iteration_round (struct ZoneIteration *zi,
1729 struct ZoneIterationProcResult proc;
1730 struct GNUNET_MQ_Envelope *env;
1731 struct GNUNET_NAMESTORE_Header *em;
1732 struct GNUNET_TIME_Absolute start;
1733 struct GNUNET_TIME_Relative duration;
1738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1739 "Asked to return up to %llu records at position %llu\n",
1740 (unsigned long long) limit,
1741 (unsigned long long) zi->seq);
1744 start = GNUNET_TIME_absolute_get ();
1745 GNUNET_break (GNUNET_SYSERR !=
1746 GSN_database->iterate_records (GSN_database->cls,
1747 (0 == memcmp (&zi->zone,
1756 duration = GNUNET_TIME_absolute_get_duration (start);
1757 duration = GNUNET_TIME_relative_divide (duration,
1758 limit - proc.limit);
1759 GNUNET_STATISTICS_set (statistics,
1760 "NAMESTORE iteration delay (μs/record)",
1761 duration.rel_value_us,
1763 if (0 == proc.limit)
1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1766 "Returned %llu results, more results available\n",
1767 (unsigned long long) limit);
1768 return; /* more results later after we get the
1769 #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message */
1771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1772 "Completed iteration after %llu/%llu results\n",
1773 (unsigned long long) (limit - proc.limit),
1774 (unsigned long long) limit);
1775 /* send empty response to indicate end of list */
1776 env = GNUNET_MQ_msg (em,
1777 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END);
1778 em->r_id = htonl (zi->request_id);
1779 GNUNET_MQ_send (zi->nc->mq,
1781 GNUNET_CONTAINER_DLL_remove (zi->nc->op_head,
1789 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1791 * @param cls the client sending the message
1792 * @param zis_msg message from the client
1795 handle_iteration_start (void *cls,
1796 const struct ZoneIterationStartMessage *zis_msg)
1798 struct NamestoreClient *nc = cls;
1799 struct ZoneIteration *zi;
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1802 "Received ZONE_ITERATION_START message\n");
1803 zi = GNUNET_new (struct ZoneIteration);
1804 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1807 zi->zone = zis_msg->zone;
1809 GNUNET_CONTAINER_DLL_insert (nc->op_head,
1812 run_zone_iteration_round (zi,
1814 GNUNET_SERVICE_client_continue (nc->client);
1819 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1821 * @param cls the client sending the message
1822 * @param zis_msg message from the client
1825 handle_iteration_stop (void *cls,
1826 const struct ZoneIterationStopMessage *zis_msg)
1828 struct NamestoreClient *nc = cls;
1829 struct ZoneIteration *zi;
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1833 "Received ZONE_ITERATION_STOP message\n");
1834 rid = ntohl (zis_msg->gns_header.r_id);
1835 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1836 if (zi->request_id == rid)
1841 GNUNET_SERVICE_client_drop (nc->client);
1844 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1848 GNUNET_SERVICE_client_continue (nc->client);
1853 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1855 * @param cls the client sending the message
1856 * @param message message from the client
1859 handle_iteration_next (void *cls,
1860 const struct ZoneIterationNextMessage *zis_msg)
1862 struct NamestoreClient *nc = cls;
1863 struct ZoneIteration *zi;
1867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1868 "Received ZONE_ITERATION_NEXT message\n");
1869 GNUNET_STATISTICS_update (statistics,
1870 "Iteration NEXT messages received",
1873 rid = ntohl (zis_msg->gns_header.r_id);
1874 limit = GNUNET_ntohll (zis_msg->limit);
1875 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1876 if (zi->request_id == rid)
1881 GNUNET_SERVICE_client_drop (nc->client);
1884 run_zone_iteration_round (zi,
1886 GNUNET_SERVICE_client_continue (nc->client);
1891 * Function called when the monitor is ready for more data, and we
1892 * should thus unblock PUT operations that were blocked on the
1893 * monitor not being ready.
1896 monitor_unblock (struct ZoneMonitor *zm)
1898 struct StoreActivity *sa = sa_head;
1900 while ( (NULL != sa) &&
1901 (zm->limit > zm->iteration_cnt) )
1903 struct StoreActivity *sn = sa->next;
1905 if (sa->zm_pos == zm)
1906 continue_store_activity (sa);
1909 if (zm->limit > zm->iteration_cnt)
1911 zm->sa_waiting = GNUNET_NO;
1912 if (NULL != zm->sa_wait_warning)
1914 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1915 zm->sa_wait_warning = NULL;
1918 else if (GNUNET_YES == zm->sa_waiting)
1920 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
1921 if (NULL != zm->sa_wait_warning)
1922 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1923 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
1931 * Send 'sync' message to zone monitor, we're now in sync.
1933 * @param zm monitor that is now in sync
1936 monitor_sync (struct ZoneMonitor *zm)
1938 struct GNUNET_MQ_Envelope *env;
1939 struct GNUNET_MessageHeader *sync;
1941 env = GNUNET_MQ_msg (sync,
1942 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1943 GNUNET_MQ_send (zm->nc->mq,
1945 /* mark iteration done */
1946 zm->in_first_iteration = GNUNET_NO;
1947 zm->iteration_cnt = 0;
1948 if ( (zm->limit > 0) &&
1950 monitor_unblock (zm);
1955 * Obtain the next datum during the zone monitor's zone initial iteration.
1957 * @param cls zone monitor that does its initial iteration
1960 monitor_iteration_next (void *cls);
1964 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1966 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1967 * @param seq sequence number of the record
1968 * @param zone_key zone key of the zone
1970 * @param rd_count number of records in @a rd
1971 * @param rd array of records
1974 monitor_iterate_cb (void *cls,
1976 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1978 unsigned int rd_count,
1979 const struct GNUNET_GNSRECORD_Data *rd)
1981 struct ZoneMonitor *zm = cls;
1984 GNUNET_assert (NULL != name);
1985 GNUNET_STATISTICS_update (statistics,
1986 "Monitor notifications sent",
1990 zm->iteration_cnt--;
1991 send_lookup_response (zm->nc,
1997 if ( (0 == zm->iteration_cnt) &&
2000 /* We are done with the current iteration batch, AND the
2001 client would right now accept more, so go again! */
2002 GNUNET_assert (NULL == zm->task);
2003 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2010 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
2012 * @param cls the client sending the message
2013 * @param zis_msg message from the client
2016 handle_monitor_start (void *cls,
2017 const struct ZoneMonitorStartMessage *zis_msg)
2019 struct NamestoreClient *nc = cls;
2020 struct ZoneMonitor *zm;
2022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2023 "Received ZONE_MONITOR_START message\n");
2024 zm = GNUNET_new (struct ZoneMonitor);
2026 zm->zone = zis_msg->zone;
2028 zm->in_first_iteration = (GNUNET_YES == ntohl (zis_msg->iterate_first));
2029 GNUNET_CONTAINER_DLL_insert (monitor_head,
2032 GNUNET_SERVICE_client_mark_monitor (nc->client);
2033 GNUNET_SERVICE_client_continue (nc->client);
2034 GNUNET_notification_context_add (monitor_nc,
2036 if (zm->in_first_iteration)
2037 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2045 * Obtain the next datum during the zone monitor's zone initial iteration.
2047 * @param cls zone monitor that does its initial iteration
2050 monitor_iteration_next (void *cls)
2052 struct ZoneMonitor *zm = cls;
2056 GNUNET_assert (0 == zm->iteration_cnt);
2058 zm->iteration_cnt = zm->limit / 2; /* leave half for monitor events */
2060 zm->iteration_cnt = zm->limit; /* use it all */
2061 ret = GSN_database->iterate_records (GSN_database->cls,
2062 (0 == memcmp (&zm->zone,
2069 &monitor_iterate_cb,
2071 if (GNUNET_SYSERR == ret)
2073 GNUNET_SERVICE_client_drop (zm->nc->client);
2076 if (GNUNET_NO == ret)
2086 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT message
2088 * @param cls the client sending the message
2089 * @param nm message from the client
2092 handle_monitor_next (void *cls,
2093 const struct ZoneMonitorNextMessage *nm)
2095 struct NamestoreClient *nc = cls;
2096 struct ZoneMonitor *zm;
2099 inc = GNUNET_ntohll (nm->limit);
2100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2101 "Received ZONE_MONITOR_NEXT message with limit %llu\n",
2102 (unsigned long long) inc);
2103 for (zm = monitor_head; NULL != zm; zm = zm->next)
2109 GNUNET_SERVICE_client_drop (nc->client);
2112 GNUNET_SERVICE_client_continue (nc->client);
2113 if (zm->limit + inc < zm->limit)
2116 GNUNET_SERVICE_client_drop (nc->client);
2120 if ( (zm->in_first_iteration) &&
2121 (zm->limit == inc) )
2123 /* We are still iterating, and the previous iteration must
2124 have stopped due to the client's limit, so continue it! */
2125 GNUNET_assert (NULL == zm->task);
2126 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2129 GNUNET_assert (zm->iteration_cnt <= zm->limit);
2130 if ( (zm->limit > zm->iteration_cnt) &&
2133 monitor_unblock (zm);
2135 else if (GNUNET_YES == zm->sa_waiting)
2137 if (NULL != zm->sa_wait_warning)
2138 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
2139 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
2140 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
2148 * Process namestore requests.
2150 * @param cls closure
2151 * @param cfg configuration to use
2152 * @param service the initialized service
2156 const struct GNUNET_CONFIGURATION_Handle *cfg,
2157 struct GNUNET_SERVICE_Handle *service)
2163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2164 "Starting namestore service\n");
2165 cache_keys = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2168 disable_namecache = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2172 monitor_nc = GNUNET_notification_context_create (1);
2173 if (GNUNET_YES != disable_namecache)
2175 namecache = GNUNET_NAMECACHE_connect (cfg);
2176 GNUNET_assert (NULL != namecache);
2178 /* Loading database plugin */
2180 GNUNET_CONFIGURATION_get_value_string (cfg,
2184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2185 "No database backend configured\n");
2187 GNUNET_asprintf (&db_lib_name,
2188 "libgnunet_plugin_namestore_%s",
2190 GSN_database = GNUNET_PLUGIN_load (db_lib_name,
2192 GNUNET_free (database);
2193 statistics = GNUNET_STATISTICS_create ("namestore",
2195 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
2197 if (NULL == GSN_database)
2199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2200 "Could not load database backend `%s'\n",
2202 GNUNET_SCHEDULER_shutdown ();
2209 * Define "main" method using service macro.
2213 GNUNET_SERVICE_OPTION_NONE,
2216 &client_disconnect_cb,
2218 GNUNET_MQ_hd_var_size (record_store,
2219 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE,
2220 struct RecordStoreMessage,
2222 GNUNET_MQ_hd_var_size (record_lookup,
2223 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP,
2224 struct LabelLookupMessage,
2226 GNUNET_MQ_hd_fixed_size (zone_to_name,
2227 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME,
2228 struct ZoneToNameMessage,
2230 GNUNET_MQ_hd_fixed_size (iteration_start,
2231 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START,
2232 struct ZoneIterationStartMessage,
2234 GNUNET_MQ_hd_fixed_size (iteration_next,
2235 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT,
2236 struct ZoneIterationNextMessage,
2238 GNUNET_MQ_hd_fixed_size (iteration_stop,
2239 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP,
2240 struct ZoneIterationStopMessage,
2242 GNUNET_MQ_hd_fixed_size (monitor_start,
2243 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START,
2244 struct ZoneMonitorStartMessage,
2246 GNUNET_MQ_hd_fixed_size (monitor_next,
2247 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT,
2248 struct ZoneMonitorNextMessage,
2250 GNUNET_MQ_handler_end ());
2253 /* end of gnunet-service-namestore.c */