2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2014 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 #include "gnunet_util_lib.h"
29 #include "gnunet_dnsparser_lib.h"
30 #include "gnunet_gns_service.h"
31 #include "gnunet_namecache_service.h"
32 #include "gnunet_namestore_service.h"
33 #include "gnunet_namestore_plugin.h"
34 #include "gnunet_signatures.h"
35 #include "namestore.h"
37 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
43 struct NamestoreClient;
47 * A namestore iteration operation.
52 * Next element in the DLL
54 struct ZoneIteration *next;
57 * Previous element in the DLL
59 struct ZoneIteration *prev;
62 * Namestore client which intiated this zone iteration
64 struct NamestoreClient *nc;
67 * The nick to add to the records
69 struct GNUNET_GNSRECORD_Data *nick;
72 * Key of the zone we are iterating over.
74 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
77 * The operation id fot the zone iteration in the response for the client
82 * Offset of the zone iteration used to address next result of the zone
83 * iteration in the store
85 * Initialy set to 0 in handle_iteration_start
86 * Incremented with by every call to handle_iteration_next
96 struct NamestoreClient
102 struct GNUNET_SERVICE_Client *client;
105 * Message queue for transmission to @e client
107 struct GNUNET_MQ_Handle *mq;
111 * Zone iteration operations in progress initiated by this client
113 struct ZoneIteration *op_head;
117 * Zone iteration operations in progress initiated by this client
119 struct ZoneIteration *op_tail;
124 * A namestore monitor.
129 * Next element in the DLL
131 struct ZoneMonitor *next;
134 * Previous element in the DLL
136 struct ZoneMonitor *prev;
139 * Namestore client which intiated this zone monitor
141 struct NamestoreClient *nc;
144 * Private key of the zone.
146 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
149 * Task active during initial iteration.
151 struct GNUNET_SCHEDULER_Task *task;
154 * Offset of the zone iteration used to address next result of the zone
155 * iteration in the store
158 * Incremented with by every call to #handle_iteration_next
166 * Pending operation on the namecache.
168 struct CacheOperation
174 struct CacheOperation *prev;
179 struct CacheOperation *next;
182 * Handle to namecache queue.
184 struct GNUNET_NAMECACHE_QueueEntry *qe;
187 * Client to notify about the result.
189 struct NamestoreClient *nc;
192 * Client's request ID.
199 * Public key of all zeros.
201 static const struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
204 * Configuration handle.
206 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
211 static struct GNUNET_NAMECACHE_Handle *namecache;
216 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
219 * Name of the database plugin
221 static char *db_lib_name;
226 static struct CacheOperation *cop_head;
231 static struct CacheOperation *cop_tail;
234 * First active zone monitor.
236 static struct ZoneMonitor *monitor_head;
239 * Last active zone monitor.
241 static struct ZoneMonitor *monitor_tail;
244 * Notification context shared by all monitors.
246 static struct GNUNET_NotificationContext *monitor_nc;
250 * Task run during shutdown.
255 cleanup_task (void *cls)
257 struct CacheOperation *cop;
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Stopping namestore service\n");
262 while (NULL != (cop = cop_head))
264 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
265 "Aborting incomplete namecache operation\n");
266 GNUNET_NAMECACHE_cancel (cop->qe);
267 GNUNET_CONTAINER_DLL_remove (cop_head,
272 GNUNET_NAMECACHE_disconnect (namecache);
274 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name,
276 GNUNET_free (db_lib_name);
278 if (NULL != monitor_nc)
280 GNUNET_notification_context_destroy (monitor_nc);
287 * Called whenever a client is disconnected.
288 * Frees our resources associated with that client.
291 * @param client identification of the client
292 * @param app_ctx the `struct NamestoreClient` of @a client
295 client_disconnect_cb (void *cls,
296 struct GNUNET_SERVICE_Client *client,
299 struct NamestoreClient *nc = app_ctx;
300 struct ZoneIteration *no;
301 struct ZoneMonitor *zm;
302 struct CacheOperation *cop;
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "Client %p disconnected\n",
308 for (zm = monitor_head; NULL != zm; zm = zm->next)
312 GNUNET_CONTAINER_DLL_remove (monitor_head,
315 if (NULL != zm->task)
317 GNUNET_SCHEDULER_cancel (zm->task);
324 while (NULL != (no = nc->op_head))
326 GNUNET_CONTAINER_DLL_remove (nc->op_head,
331 for (cop = cop_head; NULL != cop; cop = cop->next)
339 * Add a client to our list of active clients.
342 * @param client client to add
343 * @param mq message queue for @a client
344 * @return internal namestore client structure for this client
347 client_connect_cb (void *cls,
348 struct GNUNET_SERVICE_Client *client,
349 struct GNUNET_MQ_Handle *mq)
351 struct NamestoreClient *nc;
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Client %p connected\n",
357 nc = GNUNET_new (struct NamestoreClient);
365 * Function called with the records for the #GNUNET_GNS_EMPTY_LABEL_AT
366 * label in the zone. Used to locate the #GNUNET_GNSRECORD_TYPE_NICK
367 * record, which (if found) is then copied to @a cls for future use.
369 * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
370 * @param private_key the private key of the zone (unused)
371 * @param label should be #GNUNET_GNS_EMPTY_LABEL_AT
372 * @param rd_count number of records in @a rd
373 * @param rd records stored under @a label in the zone
376 lookup_nick_it (void *cls,
377 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
379 unsigned int rd_count,
380 const struct GNUNET_GNSRECORD_Data *rd)
382 struct GNUNET_GNSRECORD_Data **res = cls;
385 if (0 != strcmp (label, GNUNET_GNS_EMPTY_LABEL_AT))
390 for (unsigned int c = 0; c < rd_count; c++)
392 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
394 (*res) = GNUNET_malloc (rd[c].data_size + sizeof (struct GNUNET_GNSRECORD_Data));
395 (*res)->data = &(*res)[1];
396 GNUNET_memcpy ((void *) (*res)->data,
399 (*res)->data_size = rd[c].data_size;
400 (*res)->expiration_time = rd[c].expiration_time;
401 (*res)->flags = rd[c].flags;
402 (*res)->record_type = GNUNET_GNSRECORD_TYPE_NICK;
411 * Return the NICK record for the zone (if it exists).
413 * @param zone private key for the zone to look for nick
414 * @return NULL if no NICK record was found
416 static struct GNUNET_GNSRECORD_Data *
417 get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
419 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
420 struct GNUNET_GNSRECORD_Data *nick;
424 res = GSN_database->lookup_records (GSN_database->cls,
426 GNUNET_GNS_EMPTY_LABEL_AT,
429 if ( (GNUNET_OK != res) ||
432 GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
433 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
434 "No nick name set for zone `%s'\n",
435 GNUNET_GNSRECORD_z2s (&pub));
443 merge_with_nick_records (const struct GNUNET_GNSRECORD_Data *nick_rd,
445 const struct GNUNET_GNSRECORD_Data *rd2,
446 unsigned int *rdc_res,
447 struct GNUNET_GNSRECORD_Data **rd_res)
449 uint64_t latest_expiration;
455 (*rdc_res) = 1 + rdc2;
463 for (unsigned int c=0; c< 1; c++)
464 req += sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd[c].data_size;
465 for (unsigned int c=0; c< rdc2; c++)
466 req += sizeof (struct GNUNET_GNSRECORD_Data) + rd2[c].data_size;
467 (*rd_res) = GNUNET_malloc (req);
468 data = (char *) &(*rd_res)[1 + rdc2];
470 latest_expiration = 0;
472 for (unsigned int c=0; c< rdc2; c++)
474 if (0 != (rd2[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
476 if ((GNUNET_TIME_absolute_get().abs_value_us + rd2[c].expiration_time) >
478 latest_expiration = rd2[c].expiration_time;
480 else if (rd2[c].expiration_time > latest_expiration)
481 latest_expiration = rd2[c].expiration_time;
482 (*rd_res)[c] = rd2[c];
483 (*rd_res)[c].data = (void *) &data[data_offset];
484 GNUNET_memcpy ((void *) (*rd_res)[c].data,
487 data_offset += (*rd_res)[c].data_size;
489 record_offset = rdc2;
490 for (unsigned int c=0; c< 1; c++)
492 (*rd_res)[c+record_offset] = nick_rd[c];
493 (*rd_res)[c+record_offset].expiration_time = latest_expiration;
494 (*rd_res)[c+record_offset].data = (void *) &data[data_offset];
495 GNUNET_memcpy ((void *) (*rd_res)[c+record_offset].data,
497 nick_rd[c].data_size);
498 data_offset += (*rd_res)[c+record_offset].data_size;
500 GNUNET_assert (req == (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
505 * Generate a `struct LookupNameResponseMessage` and send it to the
506 * given client using the given notification context.
508 * @param nc client to unicast to
509 * @param request_id request ID to use
510 * @param zone_key zone key of the zone
512 * @param rd_count number of records in @a rd
513 * @param rd array of records
516 send_lookup_response (struct NamestoreClient *nc,
518 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
520 unsigned int rd_count,
521 const struct GNUNET_GNSRECORD_Data *rd)
523 struct GNUNET_MQ_Envelope *env;
524 struct RecordResultMessage *zir_msg;
525 struct GNUNET_GNSRECORD_Data *nick;
526 struct GNUNET_GNSRECORD_Data *res;
527 unsigned int res_count;
533 nick = get_nick_record (zone_key);
534 if ((NULL != nick) && (0 != strcmp(name, GNUNET_GNS_EMPTY_LABEL_AT)))
536 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
537 merge_with_nick_records (nick,
546 res_count = rd_count;
547 res = (struct GNUNET_GNSRECORD_Data *) rd;
550 name_len = strlen (name) + 1;
551 rd_ser_len = GNUNET_GNSRECORD_records_get_size (res_count, res);
552 env = GNUNET_MQ_msg_extra (zir_msg,
553 name_len + rd_ser_len,
554 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
555 zir_msg->gns_header.r_id = htonl (request_id);
556 zir_msg->name_len = htons (name_len);
557 zir_msg->rd_count = htons (res_count);
558 zir_msg->rd_len = htons (rd_ser_len);
559 zir_msg->private_key = *zone_key;
560 name_tmp = (char *) &zir_msg[1];
561 GNUNET_memcpy (name_tmp,
564 rd_ser = &name_tmp[name_len];
565 GNUNET_GNSRECORD_records_serialize (res_count,
569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
570 "Sending RECORD_RESULT message with %u records\n",
572 GNUNET_MQ_send (nc->mq,
580 * Send response to the store request to the client.
582 * @param client client to talk to
583 * @param res status of the operation
584 * @param rid client's request ID
587 send_store_response (struct NamestoreClient *nc,
591 struct GNUNET_MQ_Envelope *env;
592 struct RecordStoreResponseMessage *rcr_msg;
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
595 "Sending RECORD_STORE_RESPONSE message\n");
596 env = GNUNET_MQ_msg (rcr_msg,
597 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
598 rcr_msg->gns_header.r_id = htonl (rid);
599 rcr_msg->op_result = htonl (res);
600 GNUNET_MQ_send (nc->mq,
606 * Cache operation complete, clean up.
608 * @param cls the `struct CacheOperation`
609 * @param success success
610 * @param emsg error messages
613 finish_cache_operation (void *cls,
617 struct CacheOperation *cop = cls;
620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
621 _("Failed to replicate block in namecache: %s\n"),
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 "CACHE operation completed\n");
626 GNUNET_CONTAINER_DLL_remove (cop_head,
630 send_store_response (cop->nc,
638 * We just touched the plaintext information about a name in our zone;
639 * refresh the corresponding (encrypted) block in the namecache.
641 * @param nc client responsible for the request, can be NULL
642 * @param rid request ID of the client
643 * @param zone_key private key of the zone
644 * @param name label for the records
645 * @param rd_count number of records
646 * @param rd records stored under the given @a name
649 refresh_block (struct NamestoreClient *nc,
651 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
653 unsigned int rd_count,
654 const struct GNUNET_GNSRECORD_Data *rd)
656 struct GNUNET_GNSRECORD_Block *block;
657 struct CacheOperation *cop;
658 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
659 struct GNUNET_GNSRECORD_Data *nick;
660 struct GNUNET_GNSRECORD_Data *res;
661 unsigned int res_count;
663 nick = get_nick_record (zone_key);
664 res_count = rd_count;
665 res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
668 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
669 merge_with_nick_records (nick,
677 block = GNUNET_GNSRECORD_block_create (zone_key,
678 GNUNET_TIME_UNIT_ZERO_ABS,
682 block = GNUNET_GNSRECORD_block_create (zone_key,
683 GNUNET_GNSRECORD_record_get_expiration_time (res_count,
687 GNUNET_assert (NULL != block);
688 GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
691 "Caching block for label `%s' with %u records in zone `%s' in namecache\n",
694 GNUNET_GNSRECORD_z2s (&pkey));
695 cop = GNUNET_new (struct CacheOperation);
698 GNUNET_CONTAINER_DLL_insert (cop_head,
701 cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
703 &finish_cache_operation,
710 * Closure for #lookup_it().
712 struct RecordLookupContext
728 struct GNUNET_GNSRECORD_Data *nick;
738 unsigned int res_rd_count;
751 lookup_it (void *cls,
752 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
754 unsigned int rd_count,
755 const struct GNUNET_GNSRECORD_Data *rd)
757 struct RecordLookupContext *rlc = cls;
758 struct GNUNET_GNSRECORD_Data *rd_res;
759 unsigned int rdc_res;
762 if (0 == strcmp (label, rlc->label))
764 rlc->found = GNUNET_YES;
767 if ( (NULL != rlc->nick) &&
769 GNUNET_GNS_EMPTY_LABEL_AT)) )
774 rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
775 merge_with_nick_records (rlc->nick,
779 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res,
781 rlc->res_rd_count = rdc_res;
782 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
783 GNUNET_GNSRECORD_records_serialize (rdc_res,
788 GNUNET_free (rd_res);
789 GNUNET_free (rlc->nick);
794 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
796 rlc->res_rd_count = rd_count;
797 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
798 GNUNET_GNSRECORD_records_serialize (rd_count,
807 rlc->res_rd_count = 0;
815 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
817 * @param cls client sending the message
818 * @param ll_msg message of type `struct LabelLookupMessage`
819 * @return #GNUNET_OK if @a ll_msg is well-formed
822 check_record_lookup (void *cls,
823 const struct LabelLookupMessage *ll_msg)
827 const char *name_tmp;
830 name_len = ntohl (ll_msg->label_len);
831 src_size = ntohs (ll_msg->gns_header.header.size);
832 if (name_len != src_size - sizeof (struct LabelLookupMessage))
835 return GNUNET_SYSERR;
838 name_tmp = (const char *) &ll_msg[1];
839 if ('\0' != name_tmp[name_len -1])
842 return GNUNET_SYSERR;
849 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
851 * @param cls client sending the message
852 * @param ll_msg message of type `struct LabelLookupMessage`
855 handle_record_lookup (void *cls,
856 const struct LabelLookupMessage *ll_msg)
858 struct NamestoreClient *nc = cls;
859 struct GNUNET_MQ_Envelope *env;
860 struct LabelLookupResponseMessage *llr_msg;
861 struct RecordLookupContext rlc;
862 const char *name_tmp;
868 name_len = ntohl (ll_msg->label_len);
869 name_tmp = (const char *) &ll_msg[1];
870 GNUNET_SERVICE_client_continue (nc->client);
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
872 "Received NAMESTORE_RECORD_LOOKUP message for name `%s'\n",
875 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
876 if (NULL == conv_name)
878 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
879 "Error converting name `%s'\n",
881 GNUNET_SERVICE_client_drop (nc->client);
884 rlc.label = conv_name;
885 rlc.found = GNUNET_NO;
886 rlc.res_rd_count = 0;
889 rlc.nick = get_nick_record (&ll_msg->zone);
890 res = GSN_database->lookup_records (GSN_database->cls,
895 GNUNET_free (conv_name);
896 env = GNUNET_MQ_msg_extra (llr_msg,
897 name_len + rlc.rd_ser_len,
898 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
899 llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
900 llr_msg->private_key = ll_msg->zone;
901 llr_msg->name_len = htons (name_len);
902 llr_msg->rd_count = htons (rlc.res_rd_count);
903 llr_msg->rd_len = htons (rlc.rd_ser_len);
904 res_name = (char *) &llr_msg[1];
905 if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
906 llr_msg->found = ntohs (GNUNET_YES);
908 llr_msg->found = ntohs (GNUNET_NO);
909 GNUNET_memcpy (&llr_msg[1],
912 GNUNET_memcpy (&res_name[name_len],
915 GNUNET_MQ_send (nc->mq,
917 GNUNET_free_non_null (rlc.res_rd);
922 * Checks a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
924 * @param cls client sending the message
925 * @param rp_msg message of type `struct RecordStoreMessage`
926 * @return #GNUNET_OK if @a rp_msg is well-formed
929 check_record_store (void *cls,
930 const struct RecordStoreMessage *rp_msg)
936 const char *name_tmp;
939 name_len = ntohs (rp_msg->name_len);
940 msg_size = ntohs (rp_msg->gns_header.header.size);
941 rd_ser_len = ntohs (rp_msg->rd_len);
942 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
943 if (msg_size != msg_size_exp)
946 return GNUNET_SYSERR;
948 if ((0 == name_len) || (name_len > MAX_NAME_LEN))
951 return GNUNET_SYSERR;
953 name_tmp = (const char *) &rp_msg[1];
954 if ('\0' != name_tmp[name_len -1])
957 return GNUNET_SYSERR;
964 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
966 * @param cls client sending the message
967 * @param rp_msg message of type `struct RecordStoreMessage`
970 handle_record_store (void *cls,
971 const struct RecordStoreMessage *rp_msg)
973 struct NamestoreClient *nc = cls;
977 const char *name_tmp;
980 unsigned int rd_count;
982 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
983 struct ZoneMonitor *zm;
985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
986 "Received NAMESTORE_RECORD_STORE message\n");
987 rid = ntohl (rp_msg->gns_header.r_id);
988 name_len = ntohs (rp_msg->name_len);
989 rd_count = ntohs (rp_msg->rd_count);
990 rd_ser_len = ntohs (rp_msg->rd_len);
991 GNUNET_break (0 == ntohs (rp_msg->reserved));
992 name_tmp = (const char *) &rp_msg[1];
993 rd_ser = &name_tmp[name_len];
995 struct GNUNET_GNSRECORD_Data rd[rd_count];
998 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
1004 GNUNET_SERVICE_client_drop (nc->client);
1008 /* Extracting and converting private key */
1009 GNUNET_CRYPTO_ecdsa_key_get_public (&rp_msg->private_key,
1011 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
1012 if (NULL == conv_name)
1014 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1015 "Error converting name `%s'\n",
1017 GNUNET_SERVICE_client_drop (nc->client);
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021 "Creating %u records for name `%s' in zone `%s'\n",
1022 (unsigned int) rd_count,
1024 GNUNET_GNSRECORD_z2s (&pubkey));
1026 if ( (0 == rd_count) &&
1028 GSN_database->iterate_records (GSN_database->cls,
1029 &rp_msg->private_key,
1034 /* This name does not exist, so cannot be removed */
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "Name `%s' does not exist, no deletion required\n",
1042 struct GNUNET_GNSRECORD_Data rd_clean[rd_count];
1043 unsigned int rd_clean_off;
1045 /* remove "NICK" records, unless this is for the
1046 #GNUNET_GNS_EMPTY_LABEL_AT label */
1048 for (unsigned int i=0;i<rd_count;i++)
1050 rd_clean[rd_clean_off] = rd[i];
1051 if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
1053 (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
1056 res = GSN_database->store_records (GSN_database->cls,
1057 &rp_msg->private_key,
1061 if (GNUNET_OK == res)
1063 for (zm = monitor_head; NULL != zm; zm = zm->next)
1065 if ( (0 == memcmp (&rp_msg->private_key, &zm->zone,
1066 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) ||
1067 (0 == memcmp (&zm->zone,
1069 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072 "Notifying monitor about changes under label `%s'\n",
1074 send_lookup_response (zm->nc,
1076 &rp_msg->private_key,
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1082 "Monitor is for another zone\n");
1084 if (NULL == monitor_head)
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "No monitors active\n");
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1091 "Error storing record: %d\n",
1095 if (GNUNET_OK == res)
1099 &rp_msg->private_key,
1103 GNUNET_SERVICE_client_continue (nc->client);
1104 GNUNET_free (conv_name);
1107 GNUNET_free (conv_name);
1109 send_store_response (nc,
1112 GNUNET_SERVICE_client_continue (nc->client);
1117 * Context for record remove operations passed from #handle_zone_to_name to
1118 * #handle_zone_to_name_it as closure
1120 struct ZoneToNameCtx
1125 struct NamestoreClient *nc;
1128 * Request id (to be used in the response to the client).
1133 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
1134 * not finding a name for the zone still counts as a 'success' here,
1135 * as this field is about the success of executing the IPC protocol.
1142 * Zone to name iterator
1144 * @param cls struct ZoneToNameCtx *
1145 * @param zone_key the zone key
1147 * @param rd_count number of records in @a rd
1148 * @param rd record data
1151 handle_zone_to_name_it (void *cls,
1152 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1154 unsigned int rd_count,
1155 const struct GNUNET_GNSRECORD_Data *rd)
1157 struct ZoneToNameCtx *ztn_ctx = cls;
1158 struct GNUNET_MQ_Envelope *env;
1159 struct ZoneToNameResponseMessage *ztnr_msg;
1167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1168 "Found result for zone-to-name lookup: `%s'\n",
1171 name_len = (NULL == name) ? 0 : strlen (name) + 1;
1172 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
1173 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
1174 if (msg_size >= GNUNET_MAX_MESSAGE_SIZE)
1177 ztn_ctx->success = GNUNET_SYSERR;
1180 env = GNUNET_MQ_msg_extra (ztnr_msg,
1181 name_len + rd_ser_len,
1182 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1183 ztnr_msg->gns_header.header.size = htons (msg_size);
1184 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1185 ztnr_msg->res = htons (res);
1186 ztnr_msg->rd_len = htons (rd_ser_len);
1187 ztnr_msg->rd_count = htons (rd_count);
1188 ztnr_msg->name_len = htons (name_len);
1189 ztnr_msg->zone = *zone_key;
1190 name_tmp = (char *) &ztnr_msg[1];
1191 GNUNET_memcpy (name_tmp,
1194 rd_tmp = &name_tmp[name_len];
1195 GNUNET_GNSRECORD_records_serialize (rd_count,
1199 ztn_ctx->success = GNUNET_OK;
1200 GNUNET_MQ_send (ztn_ctx->nc->mq,
1206 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
1208 * @param cls client client sending the message
1209 * @param ztn_msg message of type 'struct ZoneToNameMessage'
1212 handle_zone_to_name (void *cls,
1213 const struct ZoneToNameMessage *ztn_msg)
1215 struct NamestoreClient *nc = cls;
1216 struct ZoneToNameCtx ztn_ctx;
1217 struct GNUNET_MQ_Envelope *env;
1218 struct ZoneToNameResponseMessage *ztnr_msg;
1220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1221 "Received `%s' message\n",
1223 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
1225 ztn_ctx.success = GNUNET_NO;
1226 if (GNUNET_SYSERR ==
1227 GSN_database->zone_to_name (GSN_database->cls,
1229 &ztn_msg->value_zone,
1230 &handle_zone_to_name_it, &ztn_ctx))
1232 /* internal error, hang up instead of signalling something
1233 that might be wrong */
1235 GNUNET_SERVICE_client_drop (nc->client);
1238 if (GNUNET_NO == ztn_ctx.success)
1240 /* no result found, send empty response */
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1242 "Found no result for zone-to-name lookup.\n");
1243 env = GNUNET_MQ_msg (ztnr_msg,
1244 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1245 ztnr_msg->gns_header.r_id = ztn_msg->gns_header.r_id;
1246 ztnr_msg->res = htons (GNUNET_NO);
1247 GNUNET_MQ_send (nc->mq,
1250 GNUNET_SERVICE_client_continue (nc->client);
1255 * Zone iteration processor result
1257 enum ZoneIterationResult
1266 * Continue to iterate with next iteration_next call
1268 IT_SUCCESS_MORE_AVAILABLE = 1,
1271 * Iteration complete
1273 IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE = 2
1278 * Context for record remove operations passed from
1279 * #run_zone_iteration_round to #zone_iterate_proc as closure
1281 struct ZoneIterationProcResult
1284 * The zone iteration handle
1286 struct ZoneIteration *zi;
1289 * Iteration result: iteration done?
1290 * #IT_SUCCESS_MORE_AVAILABLE: if there may be more results overall but
1291 * we got one for now and have sent it to the client
1292 * #IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE: if there are no further results,
1293 * #IT_START: if we are still trying to find a result.
1295 int res_iteration_finished;
1301 * Process results for zone iteration from database
1303 * @param cls struct ZoneIterationProcResult *proc
1304 * @param zone_key the zone key
1306 * @param rd_count number of records for this name
1307 * @param rd record data
1310 zone_iterate_proc (void *cls,
1311 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1313 unsigned int rd_count,
1314 const struct GNUNET_GNSRECORD_Data *rd)
1316 struct ZoneIterationProcResult *proc = cls;
1317 int do_refresh_block;
1319 if ((NULL == zone_key) && (NULL == name))
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 "Iteration done\n");
1323 proc->res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1326 if ((NULL == zone_key) || (NULL == name))
1328 /* what is this!? should never happen */
1329 proc->res_iteration_finished = IT_START;
1333 proc->res_iteration_finished = IT_SUCCESS_MORE_AVAILABLE;
1334 send_lookup_response (proc->zi->nc,
1335 proc->zi->request_id,
1340 do_refresh_block = GNUNET_NO;
1341 for (unsigned int i=0;i<rd_count;i++)
1342 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
1344 do_refresh_block = GNUNET_YES;
1347 if (GNUNET_YES == do_refresh_block)
1348 refresh_block (NULL,
1359 * Perform the next round of the zone iteration.
1361 * @param zi zone iterator to process
1364 run_zone_iteration_round (struct ZoneIteration *zi)
1366 struct ZoneIterationProcResult proc;
1367 struct GNUNET_MQ_Envelope *env;
1368 struct RecordResultMessage *rrm;
1371 memset (&proc, 0, sizeof (proc));
1373 proc.res_iteration_finished = IT_START;
1374 while (IT_START == proc.res_iteration_finished)
1376 if (GNUNET_SYSERR ==
1377 (ret = GSN_database->iterate_records (GSN_database->cls,
1378 (0 == memcmp (&zi->zone, &zero, sizeof (zero)))
1382 &zone_iterate_proc, &proc)))
1387 if (GNUNET_NO == ret)
1388 proc.res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1391 if (IT_SUCCESS_MORE_AVAILABLE == proc.res_iteration_finished)
1393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1394 "More results available\n");
1395 return; /* more results later */
1397 /* send empty response to indicate end of list */
1398 env = GNUNET_MQ_msg (rrm,
1399 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
1400 rrm->gns_header.r_id = htonl (zi->request_id);
1401 GNUNET_MQ_send (zi->nc->mq,
1403 GNUNET_CONTAINER_DLL_remove (zi->nc->op_head,
1411 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1413 * @param cls the client sending the message
1414 * @param zis_msg message from the client
1417 handle_iteration_start (void *cls,
1418 const struct ZoneIterationStartMessage *zis_msg)
1420 struct NamestoreClient *nc = cls;
1421 struct ZoneIteration *zi;
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1424 "Received ZONE_ITERATION_START message\n");
1425 zi = GNUNET_new (struct ZoneIteration);
1426 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1429 zi->zone = zis_msg->zone;
1431 GNUNET_CONTAINER_DLL_insert (nc->op_head,
1434 run_zone_iteration_round (zi);
1435 GNUNET_SERVICE_client_continue (nc->client);
1440 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1442 * @param cls the client sending the message
1443 * @param zis_msg message from the client
1446 handle_iteration_stop (void *cls,
1447 const struct ZoneIterationStopMessage *zis_msg)
1449 struct NamestoreClient *nc = cls;
1450 struct ZoneIteration *zi;
1453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1454 "Received `%s' message\n",
1455 "ZONE_ITERATION_STOP");
1456 rid = ntohl (zis_msg->gns_header.r_id);
1457 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1458 if (zi->request_id == rid)
1463 GNUNET_SERVICE_client_drop (nc->client);
1466 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1470 GNUNET_SERVICE_client_continue (nc->client);
1475 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1477 * @param cls the client sending the message
1478 * @param message message from the client
1481 handle_iteration_next (void *cls,
1482 const struct ZoneIterationNextMessage *zis_msg)
1484 struct NamestoreClient *nc = cls;
1485 struct ZoneIteration *zi;
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1489 "Received ZONE_ITERATION_NEXT message\n");
1490 rid = ntohl (zis_msg->gns_header.r_id);
1491 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1492 if (zi->request_id == rid)
1497 GNUNET_SERVICE_client_drop (nc->client);
1500 run_zone_iteration_round (zi);
1501 GNUNET_SERVICE_client_continue (nc->client);
1506 * Send 'sync' message to zone monitor, we're now in sync.
1508 * @param zm monitor that is now in sync
1511 monitor_sync (struct ZoneMonitor *zm)
1513 struct GNUNET_MQ_Envelope *env;
1514 struct GNUNET_MessageHeader *sync;
1516 env = GNUNET_MQ_msg (sync,
1517 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1518 GNUNET_MQ_send (zm->nc->mq,
1524 * Obtain the next datum during the zone monitor's zone intiial iteration.
1526 * @param cls zone monitor that does its initial iteration
1529 monitor_next (void *cls);
1533 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1535 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1536 * @param zone_key zone key of the zone
1538 * @param rd_count number of records in @a rd
1539 * @param rd array of records
1542 monitor_iterate_cb (void *cls,
1543 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1545 unsigned int rd_count,
1546 const struct GNUNET_GNSRECORD_Data *rd)
1548 struct ZoneMonitor *zm = cls;
1552 /* finished with iteration */
1556 send_lookup_response (zm->nc,
1562 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next,
1568 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
1570 * @param cls the client sending the message
1571 * @param message message from the client
1574 handle_monitor_start (void *cls,
1575 const struct ZoneMonitorStartMessage *zis_msg)
1577 struct NamestoreClient *nc = cls;
1578 struct ZoneMonitor *zm;
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1581 "Received ZONE_MONITOR_START message\n");
1582 zm = GNUNET_new (struct ZoneMonitor);
1584 zm->zone = zis_msg->zone;
1585 GNUNET_CONTAINER_DLL_insert (monitor_head,
1588 GNUNET_SERVICE_client_mark_monitor (nc->client);
1589 GNUNET_SERVICE_client_disable_continue_warning (nc->client);
1590 GNUNET_notification_context_add (monitor_nc,
1592 if (GNUNET_YES == ntohl (zis_msg->iterate_first))
1593 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next,
1601 * Obtain the next datum during the zone monitor's zone intiial iteration.
1603 * @param cls zone monitor that does its initial iteration
1606 monitor_next (void *cls)
1608 struct ZoneMonitor *zm = cls;
1612 ret = GSN_database->iterate_records (GSN_database->cls,
1613 (0 == memcmp (&zm->zone,
1619 &monitor_iterate_cb,
1621 if (GNUNET_SYSERR == ret)
1623 GNUNET_SERVICE_client_drop (zm->nc->client);
1626 if (GNUNET_NO == ret)
1636 * Process namestore requests.
1638 * @param cls closure
1639 * @param cfg configuration to use
1640 * @param service the initialized service
1644 const struct GNUNET_CONFIGURATION_Handle *cfg,
1645 struct GNUNET_SERVICE_Handle *service)
1651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1652 "Starting namestore service\n");
1654 monitor_nc = GNUNET_notification_context_create (1);
1655 namecache = GNUNET_NAMECACHE_connect (cfg);
1656 /* Loading database plugin */
1658 GNUNET_CONFIGURATION_get_value_string (cfg,
1662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1663 "No database backend configured\n");
1665 GNUNET_asprintf (&db_lib_name,
1666 "libgnunet_plugin_namestore_%s",
1668 GSN_database = GNUNET_PLUGIN_load (db_lib_name,
1670 GNUNET_free (database);
1671 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
1673 if (NULL == GSN_database)
1675 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1676 "Could not load database backend `%s'\n",
1678 GNUNET_SCHEDULER_shutdown ();
1685 * Define "main" method using service macro.
1689 GNUNET_SERVICE_OPTION_NONE,
1692 &client_disconnect_cb,
1694 GNUNET_MQ_hd_var_size (record_store,
1695 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE,
1696 struct RecordStoreMessage,
1698 GNUNET_MQ_hd_var_size (record_lookup,
1699 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP,
1700 struct LabelLookupMessage,
1702 GNUNET_MQ_hd_fixed_size (zone_to_name,
1703 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME,
1704 struct ZoneToNameMessage,
1706 GNUNET_MQ_hd_fixed_size (iteration_start,
1707 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START,
1708 struct ZoneIterationStartMessage,
1710 GNUNET_MQ_hd_fixed_size (iteration_next,
1711 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT,
1712 struct ZoneIterationNextMessage,
1714 GNUNET_MQ_hd_fixed_size (iteration_stop,
1715 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP,
1716 struct ZoneIterationStopMessage,
1718 GNUNET_MQ_hd_fixed_size (monitor_start,
1719 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START,
1720 struct ZoneMonitorStartMessage,
1722 GNUNET_MQ_handler_end ());
1725 /* end of gnunet-service-namestore.c */