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 * - run testcases, make sure everything works!
29 #include "gnunet_util_lib.h"
30 #include "gnunet_dnsparser_lib.h"
31 #include "gnunet_gns_service.h"
32 #include "gnunet_namecache_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_namestore_plugin.h"
35 #include "gnunet_statistics_service.h"
36 #include "gnunet_signatures.h"
37 #include "namestore.h"
39 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
42 * If a monitor takes more than 1 minute to process an event, print a warning.
44 #define MONITOR_STALL_WARN_DELAY GNUNET_TIME_UNIT_MINUTES
50 struct NamestoreClient;
54 * A namestore iteration operation.
59 * Next element in the DLL
61 struct ZoneIteration *next;
64 * Previous element in the DLL
66 struct ZoneIteration *prev;
69 * Namestore client which intiated this zone iteration
71 struct NamestoreClient *nc;
74 * The nick to add to the records
76 struct GNUNET_GNSRECORD_Data *nick;
79 * Key of the zone we are iterating over.
81 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
84 * Last sequence number in the zone iteration used to address next
85 * result of the zone iteration in the store
88 * Updated in #zone_iterate_proc()
93 * The operation id fot the zone iteration in the response for the client
98 * Offset of the zone iteration used to address next result of the zone
99 * iteration in the store
101 * Initialy set to 0 in #handle_iteration_start
102 * Incremented with by every call to #handle_iteration_next
112 struct NamestoreClient
118 struct GNUNET_SERVICE_Client *client;
121 * Message queue for transmission to @e client
123 struct GNUNET_MQ_Handle *mq;
127 * Zone iteration operations in progress initiated by this client
129 struct ZoneIteration *op_head;
133 * Zone iteration operations in progress initiated by this client
135 struct ZoneIteration *op_tail;
140 * A namestore monitor.
145 * Next element in the DLL
147 struct ZoneMonitor *next;
150 * Previous element in the DLL
152 struct ZoneMonitor *prev;
155 * Namestore client which intiated this zone monitor
157 struct NamestoreClient *nc;
160 * Private key of the zone.
162 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
165 * Task active during initial iteration.
167 struct GNUNET_SCHEDULER_Task *task;
170 * Task to warn about slow monitors.
172 struct GNUNET_SCHEDULER_Task *sa_wait_warning;
175 * Since when are we blocked on this monitor?
177 struct GNUNET_TIME_Absolute sa_waiting_start;
180 * Last sequence number in the zone iteration used to address next
181 * result of the zone iteration in the store
184 * Updated in #monitor_iterate_cb()
189 * Current limit of how many more messages we are allowed
190 * to queue to this monitor.
195 * How many more requests may we receive from the iterator
196 * before it is at the limit we gave it? Will be below or
197 * equal to @e limit. The effective limit for monitor
198 * events is thus @e iteration_cnt - @e limit!
200 uint64_t iteration_cnt;
203 * Are we (still) in the initial iteration pass?
205 int in_first_iteration;
208 * Is there a store activity waiting for this monitor? We only raise the
209 * flag when it happens and search the DLL for the store activity when we
210 * had a limit increase. If we cannot find any waiting store activity at
211 * that time, we clear the flag again.
219 * Pending operation on the namecache.
221 struct CacheOperation
227 struct CacheOperation *prev;
232 struct CacheOperation *next;
235 * Handle to namecache queue.
237 struct GNUNET_NAMECACHE_QueueEntry *qe;
240 * Client to notify about the result.
242 struct NamestoreClient *nc;
245 * Client's request ID.
252 * Information for an ongoing #handle_record_store() operation.
253 * Needed as we may wait for monitors to be ready for the notification.
260 struct StoreActivity *next;
265 struct StoreActivity *prev;
268 * Which client triggered the store activity?
270 struct NamestoreClient *nc;
273 * Copy of the original store message (as data fields in @e rd will
276 const struct RecordStoreMessage *rsm;
279 * Next zone monitor that still needs to be notified about this PUT.
281 struct ZoneMonitor *zm_pos;
284 * Label nicely canonicalized (lower case).
292 * Public key of all zeros.
294 static const struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
297 * Configuration handle.
299 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
302 * Handle to the statistics service
304 static struct GNUNET_STATISTICS_Handle *statistics;
309 static struct GNUNET_NAMECACHE_Handle *namecache;
314 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
317 * Name of the database plugin
319 static char *db_lib_name;
324 static struct CacheOperation *cop_head;
329 static struct CacheOperation *cop_tail;
332 * First active zone monitor.
334 static struct ZoneMonitor *monitor_head;
337 * Last active zone monitor.
339 static struct ZoneMonitor *monitor_tail;
342 * Head of DLL of monitor-blocked store activities.
344 static struct StoreActivity *sa_head;
347 * Tail of DLL of monitor-blocked store activities.
349 static struct StoreActivity *sa_tail;
352 * Notification context shared by all monitors.
354 static struct GNUNET_NotificationContext *monitor_nc;
357 * Optimize block insertion by caching map of private keys to
358 * public keys in memory?
360 static int cache_keys;
363 * Use the namecache? Doing so creates additional cryptographic
364 * operations whenever we touch a record.
366 static int disable_namecache;
370 * Task run during shutdown.
375 cleanup_task (void *cls)
377 struct CacheOperation *cop;
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "Stopping namestore service\n");
382 while (NULL != (cop = cop_head))
384 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
385 "Aborting incomplete namecache operation\n");
386 GNUNET_NAMECACHE_cancel (cop->qe);
387 GNUNET_CONTAINER_DLL_remove (cop_head,
392 if (NULL != namecache)
394 GNUNET_NAMECACHE_disconnect (namecache);
397 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name,
399 GNUNET_free (db_lib_name);
401 if (NULL != monitor_nc)
403 GNUNET_notification_context_destroy (monitor_nc);
406 if (NULL != statistics)
408 GNUNET_STATISTICS_destroy (statistics,
416 * Release memory used by @a sa.
418 * @param sa activity to free
421 free_store_activity (struct StoreActivity *sa)
423 GNUNET_CONTAINER_DLL_remove (sa_head,
426 GNUNET_free (sa->conv_name);
432 * Function called with the records for the #GNUNET_GNS_EMPTY_LABEL_AT
433 * label in the zone. Used to locate the #GNUNET_GNSRECORD_TYPE_NICK
434 * record, which (if found) is then copied to @a cls for future use.
436 * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
437 * @param seq sequence number of the record
438 * @param private_key the private key of the zone (unused)
439 * @param label should be #GNUNET_GNS_EMPTY_LABEL_AT
440 * @param rd_count number of records in @a rd
441 * @param rd records stored under @a label in the zone
444 lookup_nick_it (void *cls,
446 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
448 unsigned int rd_count,
449 const struct GNUNET_GNSRECORD_Data *rd)
451 struct GNUNET_GNSRECORD_Data **res = cls;
455 if (0 != strcmp (label, GNUNET_GNS_EMPTY_LABEL_AT))
460 for (unsigned int c = 0; c < rd_count; c++)
462 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
464 (*res) = GNUNET_malloc (rd[c].data_size + sizeof (struct GNUNET_GNSRECORD_Data));
465 (*res)->data = &(*res)[1];
466 GNUNET_memcpy ((void *) (*res)->data,
469 (*res)->data_size = rd[c].data_size;
470 (*res)->expiration_time = rd[c].expiration_time;
471 (*res)->flags = rd[c].flags;
472 (*res)->record_type = GNUNET_GNSRECORD_TYPE_NICK;
481 * Return the NICK record for the zone (if it exists).
483 * @param zone private key for the zone to look for nick
484 * @return NULL if no NICK record was found
486 static struct GNUNET_GNSRECORD_Data *
487 get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
489 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
490 struct GNUNET_GNSRECORD_Data *nick;
494 res = GSN_database->lookup_records (GSN_database->cls,
496 GNUNET_GNS_EMPTY_LABEL_AT,
499 if ( (GNUNET_OK != res) ||
502 GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
504 "No nick name set for zone `%s'\n",
505 GNUNET_GNSRECORD_z2s (&pub));
513 * Merge the nick record @a nick_rd with the rest of the
514 * record set given in @a rd2. Store the result in @a rdc_res
515 * and @a rd_res. The @a nick_rd's expiration time is set to
516 * the maximum expiration time of all of the records in @a rd2.
518 * @param nick_rd the nick record to integrate
519 * @param rd2_length length of the @a rd2 array
520 * @param rd2 array of records
521 * @param rdc_res[out] length of the resulting @a rd_res array
522 * @param rd_res[out] set to an array of records,
523 * including @a nick_rd and @a rd2;
524 * all of the variable-size 'data' fields in @a rd2 are
525 * allocated in the same chunk of memory!
528 merge_with_nick_records (const struct GNUNET_GNSRECORD_Data *nick_rd,
529 unsigned int rd2_length,
530 const struct GNUNET_GNSRECORD_Data *rd2,
531 unsigned int *rdc_res,
532 struct GNUNET_GNSRECORD_Data **rd_res)
534 uint64_t latest_expiration;
538 struct GNUNET_GNSRECORD_Data *target;
540 (*rdc_res) = 1 + rd2_length;
541 if (0 == 1 + rd2_length)
547 req = sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd->data_size;
548 for (unsigned int i=0; i<rd2_length; i++)
550 const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
552 if (req + sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size < req)
558 req += sizeof (struct GNUNET_GNSRECORD_Data) + orig->data_size;
560 target = GNUNET_malloc (req);
562 data = (char *) &target[1 + rd2_length];
564 latest_expiration = 0;
565 for (unsigned int i=0;i<rd2_length;i++)
567 const struct GNUNET_GNSRECORD_Data *orig = &rd2[i];
569 if (0 != (orig->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
571 if ((GNUNET_TIME_absolute_get().abs_value_us + orig->expiration_time) >
573 latest_expiration = orig->expiration_time;
575 else if (orig->expiration_time > latest_expiration)
576 latest_expiration = orig->expiration_time;
578 target[i].data = (void *) &data[data_offset];
579 GNUNET_memcpy (&data[data_offset],
582 data_offset += orig->data_size;
585 target[rd2_length] = *nick_rd;
586 target[rd2_length].expiration_time = latest_expiration;
587 target[rd2_length].data = (void *) &data[data_offset];
588 GNUNET_memcpy (&data[data_offset],
591 data_offset += nick_rd->data_size;
592 GNUNET_assert (req ==
593 (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
598 * Generate a `struct LookupNameResponseMessage` and send it to the
599 * given client using the given notification context.
601 * @param nc client to unicast to
602 * @param request_id request ID to use
603 * @param zone_key zone key of the zone
605 * @param rd_count number of records in @a rd
606 * @param rd array of records
609 send_lookup_response (struct NamestoreClient *nc,
611 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
613 unsigned int rd_count,
614 const struct GNUNET_GNSRECORD_Data *rd)
616 struct GNUNET_MQ_Envelope *env;
617 struct RecordResultMessage *zir_msg;
618 struct GNUNET_GNSRECORD_Data *nick;
619 struct GNUNET_GNSRECORD_Data *res;
620 unsigned int res_count;
626 nick = get_nick_record (zone_key);
629 GNUNET_GNSRECORD_records_get_size (rd_count,
632 if ( (NULL != nick) &&
634 GNUNET_GNS_EMPTY_LABEL_AT)))
636 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
637 merge_with_nick_records (nick,
646 res_count = rd_count;
647 res = (struct GNUNET_GNSRECORD_Data *) rd;
651 GNUNET_GNSRECORD_records_get_size (res_count,
655 name_len = strlen (name) + 1;
656 rd_ser_len = GNUNET_GNSRECORD_records_get_size (res_count,
661 GNUNET_SERVICE_client_drop (nc->client);
664 if (rd_ser_len >= UINT16_MAX - name_len - sizeof (*zir_msg))
667 GNUNET_SERVICE_client_drop (nc->client);
670 env = GNUNET_MQ_msg_extra (zir_msg,
671 name_len + rd_ser_len,
672 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
673 zir_msg->gns_header.r_id = htonl (request_id);
674 zir_msg->name_len = htons (name_len);
675 zir_msg->rd_count = htons (res_count);
676 zir_msg->rd_len = htons ((uint16_t) rd_ser_len);
677 zir_msg->private_key = *zone_key;
678 name_tmp = (char *) &zir_msg[1];
679 GNUNET_memcpy (name_tmp,
682 rd_ser = &name_tmp[name_len];
683 GNUNET_assert (rd_ser_len ==
684 GNUNET_GNSRECORD_records_serialize (res_count,
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689 "Sending RECORD_RESULT message with %u records\n",
691 GNUNET_STATISTICS_update (statistics,
692 "Record sets sent to clients",
695 GNUNET_MQ_send (nc->mq,
703 * Send response to the store request to the client.
705 * @param client client to talk to
706 * @param res status of the operation
707 * @param rid client's request ID
710 send_store_response (struct NamestoreClient *nc,
714 struct GNUNET_MQ_Envelope *env;
715 struct RecordStoreResponseMessage *rcr_msg;
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "Sending RECORD_STORE_RESPONSE message\n");
719 GNUNET_STATISTICS_update (statistics,
720 "Store requests completed",
723 env = GNUNET_MQ_msg (rcr_msg,
724 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
725 rcr_msg->gns_header.r_id = htonl (rid);
726 rcr_msg->op_result = htonl (res);
727 GNUNET_MQ_send (nc->mq,
733 * Cache operation complete, clean up.
735 * @param cls the `struct CacheOperation`
736 * @param success success
737 * @param emsg error messages
740 finish_cache_operation (void *cls,
744 struct CacheOperation *cop = cls;
747 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
748 _("Failed to replicate block in namecache: %s\n"),
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "CACHE operation completed\n");
753 GNUNET_CONTAINER_DLL_remove (cop_head,
757 send_store_response (cop->nc,
765 * We just touched the plaintext information about a name in our zone;
766 * refresh the corresponding (encrypted) block in the namecache.
768 * @param nc client responsible for the request, can be NULL
769 * @param rid request ID of the client
770 * @param zone_key private key of the zone
771 * @param name label for the records
772 * @param rd_count number of records
773 * @param rd records stored under the given @a name
776 refresh_block (struct NamestoreClient *nc,
778 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
780 unsigned int rd_count,
781 const struct GNUNET_GNSRECORD_Data *rd)
783 struct GNUNET_GNSRECORD_Block *block;
784 struct CacheOperation *cop;
785 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
786 struct GNUNET_GNSRECORD_Data *nick;
787 struct GNUNET_GNSRECORD_Data *res;
788 unsigned int res_count;
789 struct GNUNET_TIME_Absolute exp_time;
791 nick = get_nick_record (zone_key);
792 res_count = rd_count;
793 res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
796 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
797 merge_with_nick_records (nick,
805 send_store_response (nc,
808 return; /* no data, no need to update cache */
810 if (GNUNET_YES == disable_namecache)
812 GNUNET_STATISTICS_update (statistics,
813 "Namecache updates skipped (NC disabled)",
816 send_store_response (nc,
821 exp_time = GNUNET_GNSRECORD_record_get_expiration_time (res_count,
824 block = GNUNET_GNSRECORD_block_create2 (zone_key,
830 block = GNUNET_GNSRECORD_block_create (zone_key,
835 GNUNET_assert (NULL != block);
836 GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
839 "Caching block for label `%s' with %u records and expiration %s in zone `%s' in namecache\n",
842 GNUNET_STRINGS_absolute_time_to_string (exp_time),
843 GNUNET_GNSRECORD_z2s (&pkey));
844 GNUNET_STATISTICS_update (statistics,
845 "Namecache updates pushed",
848 cop = GNUNET_new (struct CacheOperation);
851 GNUNET_CONTAINER_DLL_insert (cop_head,
854 cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
856 &finish_cache_operation,
863 * Print a warning that one of our monitors is no longer reacting.
865 * @param cls a `struct ZoneMonitor` to warn about
868 warn_monitor_slow (void *cls)
870 struct ZoneMonitor *zm = cls;
872 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
873 "No response from monitor since %s\n",
874 GNUNET_STRINGS_absolute_time_to_string (zm->sa_waiting_start));
875 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
882 * Continue processing the @a sa.
884 * @param sa store activity to process
887 continue_store_activity (struct StoreActivity *sa)
889 const struct RecordStoreMessage *rp_msg = sa->rsm;
890 unsigned int rd_count;
894 const char *name_tmp;
897 rid = ntohl (rp_msg->gns_header.r_id);
898 name_len = ntohs (rp_msg->name_len);
899 rd_count = ntohs (rp_msg->rd_count);
900 rd_ser_len = ntohs (rp_msg->rd_len);
901 name_tmp = (const char *) &rp_msg[1];
902 rd_ser = &name_tmp[name_len];
904 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
906 /* We did this before, must succeed again */
907 GNUNET_assert (GNUNET_OK ==
908 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
913 for (struct ZoneMonitor *zm = sa->zm_pos;
917 if ( (0 != memcmp (&rp_msg->private_key,
919 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) &&
920 (0 != memcmp (&zm->zone,
922 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
924 sa->zm_pos = zm->next; /* not interesting to this monitor */
925 continue; // -- fails tests, but why not here?
927 if (zm->limit == zm->iteration_cnt)
929 zm->sa_waiting = GNUNET_YES;
930 zm->sa_waiting_start = GNUNET_TIME_absolute_get ();
931 if (NULL != zm->sa_wait_warning)
932 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
933 zm->sa_wait_warning = GNUNET_SCHEDULER_add_delayed (MONITOR_STALL_WARN_DELAY,
936 return; /* blocked on zone monitor */
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
939 "Notifying monitor about changes under label `%s'\n",
942 send_lookup_response (zm->nc,
944 &rp_msg->private_key,
948 sa->zm_pos = zm->next;
950 /* great, done with the monitors, unpack (again) for refresh_block operation */
951 refresh_block (sa->nc,
953 &rp_msg->private_key,
958 GNUNET_SERVICE_client_continue (sa->nc->client);
959 free_store_activity (sa);
964 * Called whenever a client is disconnected.
965 * Frees our resources associated with that client.
968 * @param client identification of the client
969 * @param app_ctx the `struct NamestoreClient` of @a client
972 client_disconnect_cb (void *cls,
973 struct GNUNET_SERVICE_Client *client,
976 struct NamestoreClient *nc = app_ctx;
977 struct ZoneIteration *no;
978 struct CacheOperation *cop;
981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
982 "Client %p disconnected\n",
984 for (struct ZoneMonitor *zm = monitor_head; NULL != zm; zm = zm->next)
986 struct StoreActivity *san;
990 GNUNET_CONTAINER_DLL_remove (monitor_head,
993 if (NULL != zm->task)
995 GNUNET_SCHEDULER_cancel (zm->task);
998 if (NULL != zm->sa_wait_warning)
1000 GNUNET_SCHEDULER_cancel (zm->sa_wait_warning);
1001 zm->sa_wait_warning = NULL;
1003 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = san)
1006 if (zm == sa->zm_pos)
1008 sa->zm_pos = zm->next;
1009 /* this may free sa */
1010 continue_store_activity (sa);
1016 for (struct StoreActivity *sa = sa_head; NULL != sa; sa = sa->next)
1020 /* this may free sa */
1021 free_store_activity (sa);
1022 break; /* there can only be one per nc */
1025 while (NULL != (no = nc->op_head))
1027 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1032 for (cop = cop_head; NULL != cop; cop = cop->next)
1040 * Add a client to our list of active clients.
1043 * @param client client to add
1044 * @param mq message queue for @a client
1045 * @return internal namestore client structure for this client
1048 client_connect_cb (void *cls,
1049 struct GNUNET_SERVICE_Client *client,
1050 struct GNUNET_MQ_Handle *mq)
1052 struct NamestoreClient *nc;
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1056 "Client %p connected\n",
1058 nc = GNUNET_new (struct NamestoreClient);
1059 nc->client = client;
1066 * Closure for #lookup_it().
1068 struct RecordLookupContext
1084 struct GNUNET_GNSRECORD_Data *nick;
1094 unsigned int res_rd_count;
1106 * @param seq sequence number of the record
1109 lookup_it (void *cls,
1111 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
1113 unsigned int rd_count,
1114 const struct GNUNET_GNSRECORD_Data *rd)
1116 struct RecordLookupContext *rlc = cls;
1120 if (0 != strcmp (label,
1123 rlc->found = GNUNET_YES;
1126 rlc->rd_ser_len = 0;
1127 rlc->res_rd_count = 0;
1131 if ( (NULL != rlc->nick) &&
1132 (0 != strcmp (label,
1133 GNUNET_GNS_EMPTY_LABEL_AT)) )
1136 struct GNUNET_GNSRECORD_Data *rd_res;
1137 unsigned int rdc_res;
1141 rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
1142 merge_with_nick_records (rlc->nick,
1147 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res,
1149 if (rlc->rd_ser_len < 0)
1152 GNUNET_free (rd_res);
1153 rlc->found = GNUNET_NO;
1154 rlc->rd_ser_len = 0;
1157 rlc->res_rd_count = rdc_res;
1158 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1159 if (rlc->rd_ser_len !=
1160 GNUNET_GNSRECORD_records_serialize (rdc_res,
1166 GNUNET_free (rlc->res_rd);
1168 rlc->res_rd_count = 0;
1169 rlc->rd_ser_len = 0;
1170 GNUNET_free (rd_res);
1171 rlc->found = GNUNET_NO;
1174 GNUNET_free (rd_res);
1175 GNUNET_free (rlc->nick);
1180 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1182 if (rlc->rd_ser_len < 0)
1185 rlc->found = GNUNET_NO;
1186 rlc->rd_ser_len = 0;
1189 rlc->res_rd_count = rd_count;
1190 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
1191 if (rlc->rd_ser_len !=
1192 GNUNET_GNSRECORD_records_serialize (rd_count,
1198 GNUNET_free (rlc->res_rd);
1200 rlc->res_rd_count = 0;
1201 rlc->rd_ser_len = 0;
1202 rlc->found = GNUNET_NO;
1210 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1212 * @param cls client sending the message
1213 * @param ll_msg message of type `struct LabelLookupMessage`
1214 * @return #GNUNET_OK if @a ll_msg is well-formed
1217 check_record_lookup (void *cls,
1218 const struct LabelLookupMessage *ll_msg)
1222 const char *name_tmp;
1225 name_len = ntohl (ll_msg->label_len);
1226 src_size = ntohs (ll_msg->gns_header.header.size);
1227 if (name_len != src_size - sizeof (struct LabelLookupMessage))
1230 return GNUNET_SYSERR;
1233 name_tmp = (const char *) &ll_msg[1];
1234 if ('\0' != name_tmp[name_len -1])
1237 return GNUNET_SYSERR;
1244 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
1246 * @param cls client sending the message
1247 * @param ll_msg message of type `struct LabelLookupMessage`
1250 handle_record_lookup (void *cls,
1251 const struct LabelLookupMessage *ll_msg)
1253 struct NamestoreClient *nc = cls;
1254 struct GNUNET_MQ_Envelope *env;
1255 struct LabelLookupResponseMessage *llr_msg;
1256 struct RecordLookupContext rlc;
1257 const char *name_tmp;
1263 name_len = ntohl (ll_msg->label_len);
1264 name_tmp = (const char *) &ll_msg[1];
1265 GNUNET_SERVICE_client_continue (nc->client);
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267 "Received NAMESTORE_RECORD_LOOKUP message for name `%s'\n",
1270 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1271 if (NULL == conv_name)
1273 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1274 "Error converting name `%s'\n",
1276 GNUNET_SERVICE_client_drop (nc->client);
1279 rlc.label = conv_name;
1280 rlc.found = GNUNET_NO;
1281 rlc.res_rd_count = 0;
1284 rlc.nick = get_nick_record (&ll_msg->zone);
1285 res = GSN_database->lookup_records (GSN_database->cls,
1290 GNUNET_free (conv_name);
1291 env = GNUNET_MQ_msg_extra (llr_msg,
1292 name_len + rlc.rd_ser_len,
1293 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
1294 llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
1295 llr_msg->private_key = ll_msg->zone;
1296 llr_msg->name_len = htons (name_len);
1297 llr_msg->rd_count = htons (rlc.res_rd_count);
1298 llr_msg->rd_len = htons (rlc.rd_ser_len);
1299 res_name = (char *) &llr_msg[1];
1300 if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
1301 llr_msg->found = ntohs (GNUNET_YES);
1303 llr_msg->found = ntohs (GNUNET_NO);
1304 GNUNET_memcpy (&llr_msg[1],
1307 GNUNET_memcpy (&res_name[name_len],
1310 GNUNET_MQ_send (nc->mq,
1312 GNUNET_free_non_null (rlc.res_rd);
1317 * Checks a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1319 * @param cls client sending the message
1320 * @param rp_msg message of type `struct RecordStoreMessage`
1321 * @return #GNUNET_OK if @a rp_msg is well-formed
1324 check_record_store (void *cls,
1325 const struct RecordStoreMessage *rp_msg)
1329 size_t msg_size_exp;
1331 const char *name_tmp;
1334 name_len = ntohs (rp_msg->name_len);
1335 msg_size = ntohs (rp_msg->gns_header.header.size);
1336 rd_ser_len = ntohs (rp_msg->rd_len);
1337 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
1338 if (msg_size != msg_size_exp)
1341 return GNUNET_SYSERR;
1343 if ( (0 == name_len) ||
1344 (name_len > MAX_NAME_LEN) )
1347 return GNUNET_SYSERR;
1349 name_tmp = (const char *) &rp_msg[1];
1350 if ('\0' != name_tmp[name_len -1])
1353 return GNUNET_SYSERR;
1360 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
1362 * @param cls client sending the message
1363 * @param rp_msg message of type `struct RecordStoreMessage`
1366 handle_record_store (void *cls,
1367 const struct RecordStoreMessage *rp_msg)
1369 struct NamestoreClient *nc = cls;
1373 const char *name_tmp;
1376 unsigned int rd_count;
1378 struct StoreActivity *sa;
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1381 "Received NAMESTORE_RECORD_STORE message\n");
1382 rid = ntohl (rp_msg->gns_header.r_id);
1383 name_len = ntohs (rp_msg->name_len);
1384 rd_count = ntohs (rp_msg->rd_count);
1385 rd_ser_len = ntohs (rp_msg->rd_len);
1386 GNUNET_break (0 == ntohs (rp_msg->reserved));
1387 name_tmp = (const char *) &rp_msg[1];
1388 rd_ser = &name_tmp[name_len];
1390 struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
1393 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
1399 GNUNET_SERVICE_client_drop (nc->client);
1403 /* Extracting and converting private key */
1404 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1405 if (NULL == conv_name)
1407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1408 "Error converting name `%s'\n",
1410 GNUNET_SERVICE_client_drop (nc->client);
1413 GNUNET_STATISTICS_update (statistics,
1414 "Well-formed store requests received",
1417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1418 "Creating %u records for name `%s'\n",
1419 (unsigned int) rd_count,
1421 if ( (0 == rd_count) &&
1423 GSN_database->lookup_records (GSN_database->cls,
1424 &rp_msg->private_key,
1429 /* This name does not exist, so cannot be removed */
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1431 "Name `%s' does not exist, no deletion required\n",
1437 /* remove "NICK" records, unless this is for the
1438 #GNUNET_GNS_EMPTY_LABEL_AT label */
1439 struct GNUNET_GNSRECORD_Data rd_clean[GNUNET_NZL(rd_count)];
1440 unsigned int rd_clean_off;
1443 for (unsigned int i=0;i<rd_count;i++)
1445 rd_clean[rd_clean_off] = rd[i];
1446 if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
1448 (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
1451 res = GSN_database->store_records (GSN_database->cls,
1452 &rp_msg->private_key,
1458 if (GNUNET_OK != res)
1460 /* store not successful, not need to tell monitors */
1461 send_store_response (nc,
1464 GNUNET_SERVICE_client_continue (nc->client);
1465 GNUNET_free (conv_name);
1469 sa = GNUNET_malloc (sizeof (struct StoreActivity) +
1470 ntohs (rp_msg->gns_header.header.size));
1471 GNUNET_CONTAINER_DLL_insert (sa_head,
1475 sa->rsm = (const struct RecordStoreMessage *) &sa[1];
1476 GNUNET_memcpy (&sa[1],
1478 ntohs (rp_msg->gns_header.header.size));
1479 sa->zm_pos = monitor_head;
1480 sa->conv_name = conv_name;
1481 continue_store_activity (sa);
1487 * Context for record remove operations passed from #handle_zone_to_name to
1488 * #handle_zone_to_name_it as closure
1490 struct ZoneToNameCtx
1495 struct NamestoreClient *nc;
1498 * Request id (to be used in the response to the client).
1503 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
1504 * not finding a name for the zone still counts as a 'success' here,
1505 * as this field is about the success of executing the IPC protocol.
1512 * Zone to name iterator
1514 * @param cls struct ZoneToNameCtx *
1515 * @param seq sequence number of the record
1516 * @param zone_key the zone key
1518 * @param rd_count number of records in @a rd
1519 * @param rd record data
1522 handle_zone_to_name_it (void *cls,
1524 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1526 unsigned int rd_count,
1527 const struct GNUNET_GNSRECORD_Data *rd)
1529 struct ZoneToNameCtx *ztn_ctx = cls;
1530 struct GNUNET_MQ_Envelope *env;
1531 struct ZoneToNameResponseMessage *ztnr_msg;
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1541 "Found result for zone-to-name lookup: `%s'\n",
1544 name_len = (NULL == name) ? 0 : strlen (name) + 1;
1545 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
1550 ztn_ctx->success = GNUNET_SYSERR;
1553 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
1554 if (msg_size >= GNUNET_MAX_MESSAGE_SIZE)
1557 ztn_ctx->success = GNUNET_SYSERR;
1560 env = GNUNET_MQ_msg_extra (ztnr_msg,
1561 name_len + rd_ser_len,
1562 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1563 ztnr_msg->gns_header.header.size = htons (msg_size);
1564 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1565 ztnr_msg->res = htons (res);
1566 ztnr_msg->rd_len = htons (rd_ser_len);
1567 ztnr_msg->rd_count = htons (rd_count);
1568 ztnr_msg->name_len = htons (name_len);
1569 ztnr_msg->zone = *zone_key;
1570 name_tmp = (char *) &ztnr_msg[1];
1571 GNUNET_memcpy (name_tmp,
1574 rd_tmp = &name_tmp[name_len];
1575 GNUNET_assert (rd_ser_len ==
1576 GNUNET_GNSRECORD_records_serialize (rd_count,
1580 ztn_ctx->success = GNUNET_OK;
1581 GNUNET_MQ_send (ztn_ctx->nc->mq,
1587 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
1589 * @param cls client client sending the message
1590 * @param ztn_msg message of type 'struct ZoneToNameMessage'
1593 handle_zone_to_name (void *cls,
1594 const struct ZoneToNameMessage *ztn_msg)
1596 struct NamestoreClient *nc = cls;
1597 struct ZoneToNameCtx ztn_ctx;
1598 struct GNUNET_MQ_Envelope *env;
1599 struct ZoneToNameResponseMessage *ztnr_msg;
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 "Received ZONE_TO_NAME message\n");
1603 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
1605 ztn_ctx.success = GNUNET_NO;
1606 if (GNUNET_SYSERR ==
1607 GSN_database->zone_to_name (GSN_database->cls,
1609 &ztn_msg->value_zone,
1610 &handle_zone_to_name_it, &ztn_ctx))
1612 /* internal error, hang up instead of signalling something
1613 that might be wrong */
1615 GNUNET_SERVICE_client_drop (nc->client);
1618 if (GNUNET_NO == ztn_ctx.success)
1620 /* no result found, send empty response */
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1622 "Found no result for zone-to-name lookup.\n");
1623 env = GNUNET_MQ_msg (ztnr_msg,
1624 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1625 ztnr_msg->gns_header.r_id = ztn_msg->gns_header.r_id;
1626 ztnr_msg->res = htons (GNUNET_NO);
1627 GNUNET_MQ_send (nc->mq,
1630 GNUNET_SERVICE_client_continue (nc->client);
1635 * Context for record remove operations passed from
1636 * #run_zone_iteration_round to #zone_iterate_proc as closure
1638 struct ZoneIterationProcResult
1641 * The zone iteration handle
1643 struct ZoneIteration *zi;
1646 * Number of results left to be returned in this iteration.
1654 * Process results for zone iteration from database
1656 * @param cls struct ZoneIterationProcResult
1657 * @param seq sequence number of the record
1658 * @param zone_key the zone key
1660 * @param rd_count number of records for this name
1661 * @param rd record data
1664 zone_iterate_proc (void *cls,
1666 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1668 unsigned int rd_count,
1669 const struct GNUNET_GNSRECORD_Data *rd)
1671 struct ZoneIterationProcResult *proc = cls;
1672 int do_refresh_block;
1674 if ( (NULL == zone_key) &&
1677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1678 "Iteration done\n");
1681 if ( (NULL == zone_key) ||
1684 /* what is this!? should never happen */
1688 if (0 == proc->limit)
1690 /* what is this!? should never happen */
1695 proc->zi->seq = seq;
1696 send_lookup_response (proc->zi->nc,
1697 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 RecordResultMessage *rrm;
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 (rrm,
1777 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
1778 rrm->gns_header.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 */