2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2014, 2018 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file namestore/gnunet-service-namestore.c
23 * @brief namestore for the GNUnet naming system
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 * - run testcases, make sure everything works!
31 #include "gnunet_util_lib.h"
32 #include "gnunet_dnsparser_lib.h"
33 #include "gnunet_gns_service.h"
34 #include "gnunet_namecache_service.h"
35 #include "gnunet_namestore_service.h"
36 #include "gnunet_namestore_plugin.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_signatures.h"
39 #include "namestore.h"
41 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
44 * If a monitor takes more than 1 minute to process an event, print a warning.
46 #define MONITOR_STALL_WARN_DELAY GNUNET_TIME_UNIT_MINUTES
52 struct NamestoreClient;
56 * A namestore iteration operation.
61 * Next element in the DLL
63 struct ZoneIteration *next;
66 * Previous element in the DLL
68 struct ZoneIteration *prev;
71 * Namestore client which intiated this zone iteration
73 struct NamestoreClient *nc;
76 * The nick to add to the records
78 struct GNUNET_GNSRECORD_Data *nick;
81 * Key of the zone we are iterating over.
83 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
86 * Last sequence number in the zone iteration used to address next
87 * result of the zone iteration in the store
90 * Updated in #zone_iterate_proc()
95 * The operation id fot the zone iteration in the response for the client
100 * Offset of the zone iteration used to address next result of the zone
101 * iteration in the store
103 * Initialy set to 0 in #handle_iteration_start
104 * Incremented with by every call to #handle_iteration_next
114 struct NamestoreClient
120 struct GNUNET_SERVICE_Client *client;
123 * Message queue for transmission to @e client
125 struct GNUNET_MQ_Handle *mq;
129 * Zone iteration operations in progress initiated by this client
131 struct ZoneIteration *op_head;
135 * Zone iteration operations in progress initiated by this client
137 struct ZoneIteration *op_tail;
142 * A namestore monitor.
147 * Next element in the DLL
149 struct ZoneMonitor *next;
152 * Previous element in the DLL
154 struct ZoneMonitor *prev;
157 * Namestore client which intiated this zone monitor
159 struct NamestoreClient *nc;
162 * Private key of the zone.
164 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
167 * Task active during initial iteration.
169 struct GNUNET_SCHEDULER_Task *task;
172 * Task to warn about slow monitors.
174 struct GNUNET_SCHEDULER_Task *sa_wait_warning;
177 * Since when are we blocked on this monitor?
179 struct GNUNET_TIME_Absolute sa_waiting_start;
182 * Last sequence number in the zone iteration used to address next
183 * result of the zone iteration in the store
186 * Updated in #monitor_iterate_cb()
191 * Current limit of how many more messages we are allowed
192 * to queue to this monitor.
197 * How many more requests may we receive from the iterator
198 * before it is at the limit we gave it? Will be below or
199 * equal to @e limit. The effective limit for monitor
200 * events is thus @e iteration_cnt - @e limit!
202 uint64_t iteration_cnt;
205 * Are we (still) in the initial iteration pass?
207 int in_first_iteration;
210 * Is there a store activity waiting for this monitor? We only raise the
211 * flag when it happens and search the DLL for the store activity when we
212 * had a limit increase. If we cannot find any waiting store activity at
213 * that time, we clear the flag again.
221 * Pending operation on the namecache.
223 struct CacheOperation
229 struct CacheOperation *prev;
234 struct CacheOperation *next;
237 * Handle to namecache queue.
239 struct GNUNET_NAMECACHE_QueueEntry *qe;
242 * Client to notify about the result.
244 struct NamestoreClient *nc;
247 * Client's request ID.
254 * Information for an ongoing #handle_record_store() operation.
255 * Needed as we may wait for monitors to be ready for the notification.
262 struct StoreActivity *next;
267 struct StoreActivity *prev;
270 * Which client triggered the store activity?
272 struct NamestoreClient *nc;
275 * Copy of the original store message (as data fields in @e rd will
278 const struct RecordStoreMessage *rsm;
281 * Next zone monitor that still needs to be notified about this PUT.
283 struct ZoneMonitor *zm_pos;
286 * Label nicely canonicalized (lower case).
294 * Public key of all zeros.
296 static const struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
299 * Configuration handle.
301 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
304 * Handle to the statistics service
306 static struct GNUNET_STATISTICS_Handle *statistics;
311 static struct GNUNET_NAMECACHE_Handle *namecache;
316 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
319 * Name of the database plugin
321 static char *db_lib_name;
326 static struct CacheOperation *cop_head;
331 static struct CacheOperation *cop_tail;
334 * First active zone monitor.
336 static struct ZoneMonitor *monitor_head;
339 * Last active zone monitor.
341 static struct ZoneMonitor *monitor_tail;
344 * Head of DLL of monitor-blocked store activities.
346 static struct StoreActivity *sa_head;
349 * Tail of DLL of monitor-blocked store activities.
351 static struct StoreActivity *sa_tail;
354 * Notification context shared by all monitors.
356 static struct GNUNET_NotificationContext *monitor_nc;
359 * Optimize block insertion by caching map of private keys to
360 * public keys in memory?
362 static int cache_keys;
365 * Use the namecache? Doing so creates additional cryptographic
366 * operations whenever we touch a record.
368 static int disable_namecache;
372 * Task run during shutdown.
377 cleanup_task (void *cls)
379 struct CacheOperation *cop;
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Stopping namestore service\n");
384 while (NULL != (cop = cop_head))
386 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
387 "Aborting incomplete namecache operation\n");
388 GNUNET_NAMECACHE_cancel (cop->qe);
389 GNUNET_CONTAINER_DLL_remove (cop_head,
394 if (NULL != namecache)
396 GNUNET_NAMECACHE_disconnect (namecache);
399 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name,
401 GNUNET_free (db_lib_name);
403 if (NULL != monitor_nc)
405 GNUNET_notification_context_destroy (monitor_nc);
408 if (NULL != statistics)
410 GNUNET_STATISTICS_destroy (statistics,
418 * Release memory used by @a sa.
420 * @param sa activity to free
423 free_store_activity (struct StoreActivity *sa)
425 GNUNET_CONTAINER_DLL_remove (sa_head,
428 GNUNET_free (sa->conv_name);
434 * Function called with the records for the #GNUNET_GNS_EMPTY_LABEL_AT
435 * label in the zone. Used to locate the #GNUNET_GNSRECORD_TYPE_NICK
436 * record, which (if found) is then copied to @a cls for future use.
438 * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
439 * @param seq sequence number of the record
440 * @param private_key the private key of the zone (unused)
441 * @param label should be #GNUNET_GNS_EMPTY_LABEL_AT
442 * @param rd_count number of records in @a rd
443 * @param rd records stored under @a label in the zone
446 lookup_nick_it (void *cls,
448 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
450 unsigned int rd_count,
451 const struct GNUNET_GNSRECORD_Data *rd)
453 struct GNUNET_GNSRECORD_Data **res = cls;
457 if (0 != strcmp (label, GNUNET_GNS_EMPTY_LABEL_AT))
462 for (unsigned int c = 0; c < rd_count; c++)
464 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
466 (*res) = GNUNET_malloc (rd[c].data_size + sizeof (struct GNUNET_GNSRECORD_Data));
467 (*res)->data = &(*res)[1];
468 GNUNET_memcpy ((void *) (*res)->data,
471 (*res)->data_size = rd[c].data_size;
472 (*res)->expiration_time = rd[c].expiration_time;
473 (*res)->flags = rd[c].flags;
474 (*res)->record_type = GNUNET_GNSRECORD_TYPE_NICK;
483 * Return the NICK record for the zone (if it exists).
485 * @param zone private key for the zone to look for nick
486 * @return NULL if no NICK record was found
488 static struct GNUNET_GNSRECORD_Data *
489 get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
491 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
492 struct GNUNET_GNSRECORD_Data *nick;
496 res = GSN_database->lookup_records (GSN_database->cls,
498 GNUNET_GNS_EMPTY_LABEL_AT,
501 if ( (GNUNET_OK != res) ||
504 GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
506 "No nick name set for zone `%s'\n",
507 GNUNET_GNSRECORD_z2s (&pub));
515 * Merge the nick record @a nick_rd with the rest of the
516 * record set given in @a rd2. Store the result in @a rdc_res
517 * and @a rd_res. The @a nick_rd's expiration time is set to
518 * the maximum expiration time of all of the records in @a rd2.
520 * @param nick_rd the nick record to integrate
521 * @param rd2_length length of the @a rd2 array
522 * @param rd2 array of records
523 * @param rdc_res[out] length of the resulting @a rd_res array
524 * @param rd_res[out] set to an array of records,
525 * including @a nick_rd and @a rd2;
526 * all of the variable-size 'data' fields in @a rd2 are
527 * allocated in the same chunk of memory!
530 merge_with_nick_records (const struct GNUNET_GNSRECORD_Data *nick_rd,
531 unsigned int rd2_length,
532 const struct GNUNET_GNSRECORD_Data *rd2,
533 unsigned int *rdc_res,
534 struct GNUNET_GNSRECORD_Data **rd_res)
536 uint64_t latest_expiration;
540 struct GNUNET_GNSRECORD_Data *target;
542 (*rdc_res) = 1 + rd2_length;
543 if (0 == 1 + rd2_length)
549 req = sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd->data_size;
550 for (unsigned int i=0; i<rd2_length; i++)
552 const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
554 if (req + sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size < req)
560 req += sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size;
562 target = GNUNET_malloc (req);
564 data = (char *) &target[1 + rd2_length];
566 latest_expiration = 0;
567 for (unsigned int i=0;i<rd2_length;i++)
569 const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
571 if (0 != (orig->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
573 if ((GNUNET_TIME_absolute_get().abs_value_us + orig->expiration_time) >
575 latest_expiration = orig->expiration_time;
577 else if (orig->expiration_time > latest_expiration)
578 latest_expiration = orig->expiration_time;
580 target[i].data = (void *) &data[data_offset];
581 GNUNET_memcpy (&data[data_offset],
584 data_offset += orig->data_size;
587 target[rd2_length] = *nick_rd;
588 target[rd2_length].expiration_time = latest_expiration;
589 target[rd2_length].data = (void *) &data[data_offset];
590 GNUNET_memcpy (&data[data_offset],
593 data_offset += nick_rd->data_size;
594 GNUNET_assert (req ==
595 (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
600 * Generate a `struct LookupNameResponseMessage` and send it to the
601 * given client using the given notification context.
603 * @param nc client to unicast to
604 * @param request_id request ID to use
605 * @param zone_key zone key of the zone
607 * @param rd_count number of records in @a rd
608 * @param rd array of records
611 send_lookup_response (struct NamestoreClient *nc,
613 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
615 unsigned int rd_count,
616 const struct GNUNET_GNSRECORD_Data *rd)
618 struct GNUNET_MQ_Envelope *env;
619 struct RecordResultMessage *zir_msg;
620 struct GNUNET_GNSRECORD_Data *nick;
621 struct GNUNET_GNSRECORD_Data *res;
622 unsigned int res_count;
628 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_log (GNUNET_ERROR_TYPE_DEBUG,
720 "Sending RECORD_STORE_RESPONSE message\n");
721 GNUNET_STATISTICS_update (statistics,
722 "Store requests completed",
725 env = GNUNET_MQ_msg (rcr_msg,
726 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
727 rcr_msg->gns_header.r_id = htonl (rid);
728 rcr_msg->op_result = htonl (res);
729 GNUNET_MQ_send (nc->mq,
735 * Cache operation complete, clean up.
737 * @param cls the `struct CacheOperation`
738 * @param success success
739 * @param emsg error messages
742 finish_cache_operation (void *cls,
746 struct CacheOperation *cop = cls;
749 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
750 _("Failed to replicate block in namecache: %s\n"),
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754 "CACHE operation completed\n");
755 GNUNET_CONTAINER_DLL_remove (cop_head,
759 send_store_response (cop->nc,
767 * We just touched the plaintext information about a name in our zone;
768 * refresh the corresponding (encrypted) block in the namecache.
770 * @param nc client responsible for the request, can be NULL
771 * @param rid request ID of the client
772 * @param zone_key private key of the zone
773 * @param name label for the records
774 * @param rd_count number of records
775 * @param rd records stored under the given @a name
778 refresh_block (struct NamestoreClient *nc,
780 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
782 unsigned int rd_count,
783 const struct GNUNET_GNSRECORD_Data *rd)
785 struct GNUNET_GNSRECORD_Block *block;
786 struct CacheOperation *cop;
787 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
788 struct GNUNET_GNSRECORD_Data *nick;
789 struct GNUNET_GNSRECORD_Data *res;
790 unsigned int res_count;
791 struct GNUNET_TIME_Absolute exp_time;
793 nick = get_nick_record (zone_key);
794 res_count = rd_count;
795 res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
798 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
799 merge_with_nick_records (nick,
807 send_store_response (nc,
810 return; /* no data, no need to update cache */
812 if (GNUNET_YES == disable_namecache)
814 GNUNET_STATISTICS_update (statistics,
815 "Namecache updates skipped (NC disabled)",
818 send_store_response (nc,
823 exp_time = GNUNET_GNSRECORD_record_get_expiration_time (res_count,
826 block = GNUNET_GNSRECORD_block_create2 (zone_key,
832 block = GNUNET_GNSRECORD_block_create (zone_key,
837 GNUNET_assert (NULL != block);
838 GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
841 "Caching block for label `%s' with %u records and expiration %s in zone `%s' in namecache\n",
844 GNUNET_STRINGS_absolute_time_to_string (exp_time),
845 GNUNET_GNSRECORD_z2s (&pkey));
846 GNUNET_STATISTICS_update (statistics,
847 "Namecache updates pushed",
850 cop = GNUNET_new (struct CacheOperation);
853 GNUNET_CONTAINER_DLL_insert (cop_head,
856 cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
858 &finish_cache_operation,
865 * Print a warning that one of our monitors is no longer reacting.
867 * @param cls a `struct ZoneMonitor` to warn about
870 warn_monitor_slow (void *cls)
872 struct ZoneMonitor *zm = cls;
874 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
875 "No response from monitor since %s\n",
876 GNUNET_STRINGS_absolute_time_to_string (zm->sa_waiting_start));
877 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
884 * Continue processing the @a sa.
886 * @param sa store activity to process
889 continue_store_activity (struct StoreActivity *sa)
891 const struct RecordStoreMessage *rp_msg = sa->rsm;
892 unsigned int rd_count;
896 const char *name_tmp;
899 rid = ntohl (rp_msg->gns_header.r_id);
900 name_len = ntohs (rp_msg->name_len);
901 rd_count = ntohs (rp_msg->rd_count);
902 rd_ser_len = ntohs (rp_msg->rd_len);
903 name_tmp = (const char *) &rp_msg[1];
904 rd_ser = &name_tmp[name_len];
906 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
908 /* We did this before, must succeed again */
909 GNUNET_assert (GNUNET_OK ==
910 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
915 for (struct ZoneMonitor *zm = sa->zm_pos;
919 if ( (0 != memcmp (&rp_msg->private_key,
921 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) &&
922 (0 != memcmp (&zm->zone,
924 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
925 sa->zm_pos = zm->next; /* not interesting to this monitor */
926 if (zm->limit == zm->iteration_cnt)
928 zm->sa_waiting = GNUNET_YES;
929 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
930 if (NULL != zm->sa_wait_warning)
931 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
932 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
935 return; /* blocked on zone monitor */
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 "Notifying monitor about changes under label `%s'\n",
941 send_lookup_response (zm->nc,
943 &rp_msg->private_key,
947 sa->zm_pos = zm->next;
949 /* great, done with the monitors, unpack (again) for refresh_block operation */
950 refresh_block (sa->nc,
952 &rp_msg->private_key,
957 GNUNET_SERVICE_client_continue (sa->nc->client);
958 free_store_activity (sa);
963 * Called whenever a client is disconnected.
964 * Frees our resources associated with that client.
967 * @param client identification of the client
968 * @param app_ctx the `struct NamestoreClient` of @a client
971 client_disconnect_cb (void *cls,
972 struct GNUNET_SERVICE_Client *client,
975 struct NamestoreClient *nc = app_ctx;
976 struct ZoneIteration *no;
977 struct CacheOperation *cop;
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "Client %p disconnected\n",
983 for (struct ZoneMonitor *zm = monitor_head; NULL != zm; zm = zm->next)
985 struct StoreActivity *san;
989 GNUNET_CONTAINER_DLL_remove (monitor_head,
992 if (NULL != zm->task)
994 GNUNET_SCHEDULER_cancel (zm->task);
997 if (NULL != zm->sa_wait_warning)
999 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1000 zm->sa_wait_warning = NULL;
1002 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = san)
1005 if (zm == sa->zm_pos)
1007 sa->zm_pos = zm->next;
1008 /* this may free sa */
1009 continue_store_activity (sa);
1015 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = sa->next)
1019 /* this may free sa */
1020 free_store_activity (sa);
1021 break; /* there can only be one per nc */
1024 while (NULL != (no = nc->op_head))
1026 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1031 for (cop = cop_head; NULL != cop; cop = cop->next)
1039 * Add a client to our list of active clients.
1042 * @param client client to add
1043 * @param mq message queue for @a client
1044 * @return internal namestore client structure for this client
1047 client_connect_cb (void *cls,
1048 struct GNUNET_SERVICE_Client *client,
1049 struct GNUNET_MQ_Handle *mq)
1051 struct NamestoreClient *nc;
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "Client %p connected\n",
1057 nc = GNUNET_new (struct NamestoreClient);
1058 nc->client = client;
1065 * Closure for #lookup_it().
1067 struct RecordLookupContext
1083 struct GNUNET_GNSRECORD_Data *nick;
1093 unsigned int res_rd_count;
1105 * @param seq sequence number of the record
1108 lookup_it (void *cls,
1110 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
1112 unsigned int rd_count,
1113 const struct GNUNET_GNSRECORD_Data *rd)
1115 struct RecordLookupContext *rlc = cls;
1119 if (0 != strcmp (label,
1122 rlc->found = GNUNET_YES;
1125 rlc->rd_ser_len = 0;
1126 rlc->res_rd_count = 0;
1130 if ( (NULL != rlc->nick) &&
1131 (0 != strcmp (label,
1132 GNUNET_GNS_EMPTY_LABEL_AT)) )
1135 struct GNUNET_GNSRECORD_Data *rd_res;
1136 unsigned int rdc_res;
1140 rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
1141 merge_with_nick_records (rlc->nick,
1146 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res,
1148 if (rlc->rd_ser_len < 0)
1151 GNUNET_free (rd_res);
1152 rlc->found = GNUNET_NO;
1153 rlc->rd_ser_len = 0;
1156 rlc->res_rd_count = rdc_res;
1157 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1158 if (rlc->rd_ser_len !=
1159 GNUNET_GNSRECORD_records_serialize (rdc_res,
1165 GNUNET_free (rlc->res_rd);
1167 rlc->res_rd_count = 0;
1168 rlc->rd_ser_len = 0;
1169 GNUNET_free (rd_res);
1170 rlc->found = GNUNET_NO;
1173 GNUNET_free (rd_res);
1174 GNUNET_free (rlc->nick);
1179 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1181 if (rlc->rd_ser_len < 0)
1184 rlc->found = GNUNET_NO;
1185 rlc->rd_ser_len = 0;
1188 rlc->res_rd_count = rd_count;
1189 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1190 if (rlc->rd_ser_len !=
1191 GNUNET_GNSRECORD_records_serialize (rd_count,
1197 GNUNET_free (rlc->res_rd);
1199 rlc->res_rd_count = 0;
1200 rlc->rd_ser_len = 0;
1201 rlc->found = GNUNET_NO;
1209 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1211 * @param cls client sending the message
1212 * @param ll_msg message of type `struct LabelLookupMessage`
1213 * @return #GNUNET_OK if @a ll_msg is well-formed
1216 check_record_lookup (void *cls,
1217 const struct LabelLookupMessage *ll_msg)
1221 const char *name_tmp;
1224 name_len = ntohl (ll_msg->label_len);
1225 src_size = ntohs (ll_msg->gns_header.header.size);
1226 if (name_len != src_size - sizeof (struct LabelLookupMessage))
1229 return GNUNET_SYSERR;
1232 name_tmp = (const char *) &ll_msg[1];
1233 if ('\0' != name_tmp[name_len -1])
1236 return GNUNET_SYSERR;
1243 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1245 * @param cls client sending the message
1246 * @param ll_msg message of type `struct LabelLookupMessage`
1249 handle_record_lookup (void *cls,
1250 const struct LabelLookupMessage *ll_msg)
1252 struct NamestoreClient *nc = cls;
1253 struct GNUNET_MQ_Envelope *env;
1254 struct LabelLookupResponseMessage *llr_msg;
1255 struct RecordLookupContext rlc;
1256 const char *name_tmp;
1262 name_len = ntohl (ll_msg->label_len);
1263 name_tmp = (const char *) &ll_msg[1];
1264 GNUNET_SERVICE_client_continue (nc->client);
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1266 "Received NAMESTORE_RECORD_LOOKUP message for name `%s'\n",
1269 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1270 if (NULL == conv_name)
1272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1273 "Error converting name `%s'\n",
1275 GNUNET_SERVICE_client_drop (nc->client);
1278 rlc.label = conv_name;
1279 rlc.found = GNUNET_NO;
1280 rlc.res_rd_count = 0;
1283 rlc.nick = get_nick_record (&ll_msg->zone);
1284 res = GSN_database->lookup_records (GSN_database->cls,
1289 GNUNET_free (conv_name);
1290 env = GNUNET_MQ_msg_extra (llr_msg,
1291 name_len + rlc.rd_ser_len,
1292 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
1293 llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
1294 llr_msg->private_key = ll_msg->zone;
1295 llr_msg->name_len = htons (name_len);
1296 llr_msg->rd_count = htons (rlc.res_rd_count);
1297 llr_msg->rd_len = htons (rlc.rd_ser_len);
1298 res_name = (char *) &llr_msg[1];
1299 if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
1300 llr_msg->found = ntohs (GNUNET_YES);
1302 llr_msg->found = ntohs (GNUNET_NO);
1303 GNUNET_memcpy (&llr_msg[1],
1306 GNUNET_memcpy (&res_name[name_len],
1309 GNUNET_MQ_send (nc->mq,
1311 GNUNET_free_non_null (rlc.res_rd);
1316 * Checks a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1318 * @param cls client sending the message
1319 * @param rp_msg message of type `struct RecordStoreMessage`
1320 * @return #GNUNET_OK if @a rp_msg is well-formed
1323 check_record_store (void *cls,
1324 const struct RecordStoreMessage *rp_msg)
1328 size_t msg_size_exp;
1330 const char *name_tmp;
1333 name_len = ntohs (rp_msg->name_len);
1334 msg_size = ntohs (rp_msg->gns_header.header.size);
1335 rd_ser_len = ntohs (rp_msg->rd_len);
1336 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1337 if (msg_size != msg_size_exp)
1340 return GNUNET_SYSERR;
1342 if ( (0 == name_len) ||
1343 (name_len > MAX_NAME_LEN) )
1346 return GNUNET_SYSERR;
1348 name_tmp = (const char *) &rp_msg[1];
1349 if ('\0' != name_tmp[name_len -1])
1352 return GNUNET_SYSERR;
1359 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1361 * @param cls client sending the message
1362 * @param rp_msg message of type `struct RecordStoreMessage`
1365 handle_record_store (void *cls,
1366 const struct RecordStoreMessage *rp_msg)
1368 struct NamestoreClient *nc = cls;
1372 const char *name_tmp;
1375 unsigned int rd_count;
1377 struct StoreActivity *sa;
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1380 "Received NAMESTORE_RECORD_STORE message\n");
1381 rid = ntohl (rp_msg->gns_header.r_id);
1382 name_len = ntohs (rp_msg->name_len);
1383 rd_count = ntohs (rp_msg->rd_count);
1384 rd_ser_len = ntohs (rp_msg->rd_len);
1385 GNUNET_break (0 == ntohs (rp_msg->reserved));
1386 name_tmp = (const char *) &rp_msg[1];
1387 rd_ser = &name_tmp[name_len];
1389 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
1392 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
1398 GNUNET_SERVICE_client_drop (nc->client);
1402 /* Extracting and converting private key */
1403 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1404 if (NULL == conv_name)
1406 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1407 "Error converting name `%s'\n",
1409 GNUNET_SERVICE_client_drop (nc->client);
1412 GNUNET_STATISTICS_update (statistics,
1413 "Well-formed store requests received",
1416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1417 "Creating %u records for name `%s'\n",
1418 (unsigned int) rd_count,
1420 if ( (0 == rd_count) &&
1422 GSN_database->lookup_records (GSN_database->cls,
1423 &rp_msg->private_key,
1428 /* This name does not exist, so cannot be removed */
1429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1430 "Name `%s' does not exist, no deletion required\n",
1436 /* remove "NICK" records, unless this is for the
1437 #GNUNET_GNS_EMPTY_LABEL_AT label */
1438 struct GNUNET_GNSRECORD_Data rd_clean[GNUNET_NZL(rd_count)];
1439 unsigned int rd_clean_off;
1442 for (unsigned int i=0;i<rd_count;i++)
1444 rd_clean[rd_clean_off] = rd[i];
1445 if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
1447 (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
1450 res = GSN_database->store_records (GSN_database->cls,
1451 &rp_msg->private_key,
1457 if (GNUNET_OK != res)
1459 /* store not successful, not need to tell monitors */
1460 send_store_response (nc,
1463 GNUNET_SERVICE_client_continue (nc->client);
1464 GNUNET_free (conv_name);
1468 sa = GNUNET_malloc (sizeof (struct StoreActivity) +
1469 ntohs (rp_msg->gns_header.header.size));
1470 GNUNET_CONTAINER_DLL_insert (sa_head,
1474 sa->rsm = (const struct RecordStoreMessage *) &sa[1];
1475 GNUNET_memcpy (&sa[1],
1477 ntohs (rp_msg->gns_header.header.size));
1478 sa->zm_pos = monitor_head;
1479 sa->conv_name = conv_name;
1480 continue_store_activity (sa);
1486 * Context for record remove operations passed from #handle_zone_to_name to
1487 * #handle_zone_to_name_it as closure
1489 struct ZoneToNameCtx
1494 struct NamestoreClient *nc;
1497 * Request id (to be used in the response to the client).
1502 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
1503 * not finding a name for the zone still counts as a 'success' here,
1504 * as this field is about the success of executing the IPC protocol.
1511 * Zone to name iterator
1513 * @param cls struct ZoneToNameCtx *
1514 * @param seq sequence number of the record
1515 * @param zone_key the zone key
1517 * @param rd_count number of records in @a rd
1518 * @param rd record data
1521 handle_zone_to_name_it (void *cls,
1523 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1525 unsigned int rd_count,
1526 const struct GNUNET_GNSRECORD_Data *rd)
1528 struct ZoneToNameCtx *ztn_ctx = cls;
1529 struct GNUNET_MQ_Envelope *env;
1530 struct ZoneToNameResponseMessage *ztnr_msg;
1539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1540 "Found result for zone-to-name lookup: `%s'\n",
1543 name_len = (NULL == name) ? 0 : strlen (name) + 1;
1544 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1549 ztn_ctx->success = GNUNET_SYSERR;
1552 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
1553 if (msg_size >= GNUNET_MAX_MESSAGE_SIZE)
1556 ztn_ctx->success = GNUNET_SYSERR;
1559 env = GNUNET_MQ_msg_extra (ztnr_msg,
1560 name_len + rd_ser_len,
1561 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1562 ztnr_msg->gns_header.header.size = htons (msg_size);
1563 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1564 ztnr_msg->res = htons (res);
1565 ztnr_msg->rd_len = htons (rd_ser_len);
1566 ztnr_msg->rd_count = htons (rd_count);
1567 ztnr_msg->name_len = htons (name_len);
1568 ztnr_msg->zone = *zone_key;
1569 name_tmp = (char *) &ztnr_msg[1];
1570 GNUNET_memcpy (name_tmp,
1573 rd_tmp = &name_tmp[name_len];
1574 GNUNET_assert (rd_ser_len ==
1575 GNUNET_GNSRECORD_records_serialize (rd_count,
1579 ztn_ctx->success = GNUNET_OK;
1580 GNUNET_MQ_send (ztn_ctx->nc->mq,
1586 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
1588 * @param cls client client sending the message
1589 * @param ztn_msg message of type 'struct ZoneToNameMessage'
1592 handle_zone_to_name (void *cls,
1593 const struct ZoneToNameMessage *ztn_msg)
1595 struct NamestoreClient *nc = cls;
1596 struct ZoneToNameCtx ztn_ctx;
1597 struct GNUNET_MQ_Envelope *env;
1598 struct ZoneToNameResponseMessage *ztnr_msg;
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1601 "Received ZONE_TO_NAME message\n");
1602 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
1604 ztn_ctx.success = GNUNET_NO;
1605 if (GNUNET_SYSERR ==
1606 GSN_database->zone_to_name (GSN_database->cls,
1608 &ztn_msg->value_zone,
1609 &handle_zone_to_name_it, &ztn_ctx))
1611 /* internal error, hang up instead of signalling something
1612 that might be wrong */
1614 GNUNET_SERVICE_client_drop (nc->client);
1617 if (GNUNET_NO == ztn_ctx.success)
1619 /* no result found, send empty response */
1620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1621 "Found no result for zone-to-name lookup.\n");
1622 env = GNUNET_MQ_msg (ztnr_msg,
1623 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1624 ztnr_msg->gns_header.r_id = ztn_msg->gns_header.r_id;
1625 ztnr_msg->res = htons (GNUNET_NO);
1626 GNUNET_MQ_send (nc->mq,
1629 GNUNET_SERVICE_client_continue (nc->client);
1634 * Context for record remove operations passed from
1635 * #run_zone_iteration_round to #zone_iterate_proc as closure
1637 struct ZoneIterationProcResult
1640 * The zone iteration handle
1642 struct ZoneIteration *zi;
1645 * Number of results left to be returned in this iteration.
1653 * Process results for zone iteration from database
1655 * @param cls struct ZoneIterationProcResult
1656 * @param seq sequence number of the record
1657 * @param zone_key the zone key
1659 * @param rd_count number of records for this name
1660 * @param rd record data
1663 zone_iterate_proc (void *cls,
1665 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1667 unsigned int rd_count,
1668 const struct GNUNET_GNSRECORD_Data *rd)
1670 struct ZoneIterationProcResult *proc = cls;
1671 int do_refresh_block;
1673 if ( (NULL == zone_key) &&
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1677 "Iteration done\n");
1680 if ( (NULL == zone_key) ||
1683 /* what is this!? should never happen */
1687 if (0 == proc->limit)
1689 /* what is this!? should never happen */
1694 proc->zi->seq = seq;
1695 send_lookup_response (proc->zi->nc,
1696 proc->zi->request_id,
1701 do_refresh_block = GNUNET_NO;
1702 for (unsigned int i=0;i<rd_count;i++)
1703 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
1705 do_refresh_block = GNUNET_YES;
1708 if (GNUNET_YES == do_refresh_block)
1709 refresh_block (NULL,
1719 * Perform the next round of the zone iteration.
1721 * @param zi zone iterator to process
1722 * @param limit number of results to return in one pass
1725 run_zone_iteration_round (struct ZoneIteration *zi,
1728 struct ZoneIterationProcResult proc;
1729 struct GNUNET_MQ_Envelope *env;
1730 struct RecordResultMessage *rrm;
1731 struct GNUNET_TIME_Absolute start;
1732 struct GNUNET_TIME_Relative duration;
1737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1738 "Asked to return up to %llu records at position %llu\n",
1739 (unsigned long long) limit,
1740 (unsigned long long) zi->seq);
1743 start = GNUNET_TIME_absolute_get ();
1744 GNUNET_break (GNUNET_SYSERR !=
1745 GSN_database->iterate_records (GSN_database->cls,
1746 (0 == memcmp (&zi->zone,
1755 duration = GNUNET_TIME_absolute_get_duration (start);
1756 duration = GNUNET_TIME_relative_divide (duration,
1757 limit - proc.limit);
1758 GNUNET_STATISTICS_set (statistics,
1759 "NAMESTORE iteration delay (μs/record)",
1760 duration.rel_value_us,
1762 if (0 == proc.limit)
1764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1765 "Returned %llu results, more results available\n",
1766 (unsigned long long) limit);
1767 return; /* more results later after we get the
1768 #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message */
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1771 "Completed iteration after %llu/%llu results\n",
1772 (unsigned long long) (limit - proc.limit),
1773 (unsigned long long) limit);
1774 /* send empty response to indicate end of list */
1775 env = GNUNET_MQ_msg (rrm,
1776 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
1777 rrm->gns_header.r_id = htonl (zi->request_id);
1778 GNUNET_MQ_send (zi->nc->mq,
1780 GNUNET_CONTAINER_DLL_remove (zi->nc->op_head,
1788 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1790 * @param cls the client sending the message
1791 * @param zis_msg message from the client
1794 handle_iteration_start (void *cls,
1795 const struct ZoneIterationStartMessage *zis_msg)
1797 struct NamestoreClient *nc = cls;
1798 struct ZoneIteration *zi;
1800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1801 "Received ZONE_ITERATION_START message\n");
1802 zi = GNUNET_new (struct ZoneIteration);
1803 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1806 zi->zone = zis_msg->zone;
1808 GNUNET_CONTAINER_DLL_insert (nc->op_head,
1811 run_zone_iteration_round (zi,
1813 GNUNET_SERVICE_client_continue (nc->client);
1818 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1820 * @param cls the client sending the message
1821 * @param zis_msg message from the client
1824 handle_iteration_stop (void *cls,
1825 const struct ZoneIterationStopMessage *zis_msg)
1827 struct NamestoreClient *nc = cls;
1828 struct ZoneIteration *zi;
1831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1832 "Received ZONE_ITERATION_STOP message\n");
1833 rid = ntohl (zis_msg->gns_header.r_id);
1834 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1835 if (zi->request_id == rid)
1840 GNUNET_SERVICE_client_drop (nc->client);
1843 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1847 GNUNET_SERVICE_client_continue (nc->client);
1852 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1854 * @param cls the client sending the message
1855 * @param message message from the client
1858 handle_iteration_next (void *cls,
1859 const struct ZoneIterationNextMessage *zis_msg)
1861 struct NamestoreClient *nc = cls;
1862 struct ZoneIteration *zi;
1866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1867 "Received ZONE_ITERATION_NEXT message\n");
1868 GNUNET_STATISTICS_update (statistics,
1869 "Iteration NEXT messages received",
1872 rid = ntohl (zis_msg->gns_header.r_id);
1873 limit = GNUNET_ntohll (zis_msg->limit);
1874 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1875 if (zi->request_id == rid)
1880 GNUNET_SERVICE_client_drop (nc->client);
1883 run_zone_iteration_round (zi,
1885 GNUNET_SERVICE_client_continue (nc->client);
1890 * Function called when the monitor is ready for more data, and we
1891 * should thus unblock PUT operations that were blocked on the
1892 * monitor not being ready.
1895 monitor_unblock (struct ZoneMonitor *zm)
1897 struct StoreActivity *sa = sa_head;
1899 while ( (NULL != sa) &&
1900 (zm->limit > zm->iteration_cnt) )
1902 struct StoreActivity *sn = sa->next;
1904 if (sa->zm_pos == zm)
1905 continue_store_activity (sa);
1908 if (zm->limit > zm->iteration_cnt)
1910 zm->sa_waiting = GNUNET_NO;
1911 if (NULL != zm->sa_wait_warning)
1913 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1914 zm->sa_wait_warning = NULL;
1917 else if (GNUNET_YES == zm->sa_waiting)
1919 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
1920 if (NULL != zm->sa_wait_warning)
1921 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1922 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
1930 * Send 'sync' message to zone monitor, we're now in sync.
1932 * @param zm monitor that is now in sync
1935 monitor_sync (struct ZoneMonitor *zm)
1937 struct GNUNET_MQ_Envelope *env;
1938 struct GNUNET_MessageHeader *sync;
1940 env = GNUNET_MQ_msg (sync,
1941 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1942 GNUNET_MQ_send (zm->nc->mq,
1944 /* mark iteration done */
1945 zm->in_first_iteration = GNUNET_NO;
1946 zm->iteration_cnt = 0;
1947 if ( (zm->limit > 0) &&
1949 monitor_unblock (zm);
1954 * Obtain the next datum during the zone monitor's zone initial iteration.
1956 * @param cls zone monitor that does its initial iteration
1959 monitor_iteration_next (void *cls);
1963 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1965 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1966 * @param seq sequence number of the record
1967 * @param zone_key zone key of the zone
1969 * @param rd_count number of records in @a rd
1970 * @param rd array of records
1973 monitor_iterate_cb (void *cls,
1975 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1977 unsigned int rd_count,
1978 const struct GNUNET_GNSRECORD_Data *rd)
1980 struct ZoneMonitor *zm = cls;
1985 /* finished with iteration */
1989 GNUNET_STATISTICS_update (statistics,
1990 "Monitor notifications sent",
1994 zm->iteration_cnt--;
1995 send_lookup_response (zm->nc,
2001 if ( (0 == zm->iteration_cnt) &&
2004 /* We are done with the current iteration batch, AND the
2005 client would right now accept more, so go again! */
2006 GNUNET_assert (NULL == zm->task);
2007 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2014 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
2016 * @param cls the client sending the message
2017 * @param zis_msg message from the client
2020 handle_monitor_start (void *cls,
2021 const struct ZoneMonitorStartMessage *zis_msg)
2023 struct NamestoreClient *nc = cls;
2024 struct ZoneMonitor *zm;
2026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2027 "Received ZONE_MONITOR_START message\n");
2028 zm = GNUNET_new (struct ZoneMonitor);
2030 zm->zone = zis_msg->zone;
2032 zm->in_first_iteration = (GNUNET_YES == ntohl (zis_msg->iterate_first));
2033 GNUNET_CONTAINER_DLL_insert (monitor_head,
2036 GNUNET_SERVICE_client_mark_monitor (nc->client);
2037 GNUNET_SERVICE_client_continue (nc->client);
2038 GNUNET_notification_context_add (monitor_nc,
2040 if (zm->in_first_iteration)
2041 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2049 * Obtain the next datum during the zone monitor's zone initial iteration.
2051 * @param cls zone monitor that does its initial iteration
2054 monitor_iteration_next (void *cls)
2056 struct ZoneMonitor *zm = cls;
2060 GNUNET_assert (0 == zm->iteration_cnt);
2062 zm->iteration_cnt = zm->limit / 2; /* leave half for monitor events */
2064 zm->iteration_cnt = zm->limit; /* use it all */
2065 ret = GSN_database->iterate_records (GSN_database->cls,
2066 (0 == memcmp (&zm->zone,
2073 &monitor_iterate_cb,
2075 if (GNUNET_SYSERR == ret)
2077 GNUNET_SERVICE_client_drop (zm->nc->client);
2080 if (GNUNET_NO == ret)
2090 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT message
2092 * @param cls the client sending the message
2093 * @param nm message from the client
2096 handle_monitor_next (void *cls,
2097 const struct ZoneMonitorNextMessage *nm)
2099 struct NamestoreClient *nc = cls;
2100 struct ZoneMonitor *zm;
2103 inc = GNUNET_ntohll (nm->limit);
2104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2105 "Received ZONE_MONITOR_NEXT message with limit %llu\n",
2106 (unsigned long long) inc);
2107 for (zm = monitor_head; NULL != zm; zm = zm->next)
2113 GNUNET_SERVICE_client_drop (nc->client);
2116 GNUNET_SERVICE_client_continue (nc->client);
2117 if (zm->limit + inc < zm->limit)
2120 GNUNET_SERVICE_client_drop (nc->client);
2124 if ( (zm->in_first_iteration) &&
2125 (zm->limit == inc) )
2127 /* We are still iterating, and the previous iteration must
2128 have stopped due to the client's limit, so continue it! */
2129 GNUNET_assert (NULL == zm->task);
2130 zm->task = GNUNET_SCHEDULER_add_now (&monitor_iteration_next,
2133 GNUNET_assert (zm->iteration_cnt <= zm->limit);
2134 if ( (zm->limit > zm->iteration_cnt) &&
2137 monitor_unblock (zm);
2139 else if (GNUNET_YES == zm->sa_waiting)
2141 if (NULL != zm->sa_wait_warning)
2142 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
2143 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
2144 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
2152 * Process namestore requests.
2154 * @param cls closure
2155 * @param cfg configuration to use
2156 * @param service the initialized service
2160 const struct GNUNET_CONFIGURATION_Handle *cfg,
2161 struct GNUNET_SERVICE_Handle *service)
2167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2168 "Starting namestore service\n");
2169 cache_keys = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2172 disable_namecache = GNUNET_CONFIGURATION_get_value_yesno (cfg,
2176 monitor_nc = GNUNET_notification_context_create (1);
2177 if (GNUNET_YES != disable_namecache)
2179 namecache = GNUNET_NAMECACHE_connect (cfg);
2180 GNUNET_assert (NULL != namecache);
2182 /* Loading database plugin */
2184 GNUNET_CONFIGURATION_get_value_string (cfg,
2188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2189 "No database backend configured\n");
2191 GNUNET_asprintf (&db_lib_name,
2192 "libgnunet_plugin_namestore_%s",
2194 GSN_database = GNUNET_PLUGIN_load (db_lib_name,
2196 GNUNET_free (database);
2197 statistics = GNUNET_STATISTICS_create ("namestore",
2199 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
2201 if (NULL == GSN_database)
2203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2204 "Could not load database backend `%s'\n",
2206 GNUNET_SCHEDULER_shutdown ();
2213 * Define "main" method using service macro.
2217 GNUNET_SERVICE_OPTION_NONE,
2220 &client_disconnect_cb,
2222 GNUNET_MQ_hd_var_size (record_store,
2223 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE,
2224 struct RecordStoreMessage,
2226 GNUNET_MQ_hd_var_size (record_lookup,
2227 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP,
2228 struct LabelLookupMessage,
2230 GNUNET_MQ_hd_fixed_size (zone_to_name,
2231 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME,
2232 struct ZoneToNameMessage,
2234 GNUNET_MQ_hd_fixed_size (iteration_start,
2235 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START,
2236 struct ZoneIterationStartMessage,
2238 GNUNET_MQ_hd_fixed_size (iteration_next,
2239 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT,
2240 struct ZoneIterationNextMessage,
2242 GNUNET_MQ_hd_fixed_size (iteration_stop,
2243 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP,
2244 struct ZoneIterationStopMessage,
2246 GNUNET_MQ_hd_fixed_size (monitor_start,
2247 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START,
2248 struct ZoneMonitorStartMessage,
2250 GNUNET_MQ_hd_fixed_size (monitor_next,
2251 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_NEXT,
2252 struct ZoneMonitorNextMessage,
2254 GNUNET_MQ_handler_end ());
2257 /* end of gnunet-service-namestore.c */