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;
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "Stopping namestore service\n");
261 while (NULL != (cop = cop_head))
263 GNUNET_NAMECACHE_cancel (cop->qe);
264 GNUNET_CONTAINER_DLL_remove (cop_head,
269 GNUNET_NAMECACHE_disconnect (namecache);
271 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name,
273 GNUNET_free (db_lib_name);
275 if (NULL != monitor_nc)
277 GNUNET_notification_context_destroy (monitor_nc);
284 * Called whenever a client is disconnected.
285 * Frees our resources associated with that client.
288 * @param client identification of the client
289 * @param app_ctx the `struct NamestoreClient` of @a client
292 client_disconnect_cb (void *cls,
293 struct GNUNET_SERVICE_Client *client,
296 struct NamestoreClient *nc = app_ctx;
297 struct ZoneIteration *no;
298 struct ZoneMonitor *zm;
299 struct CacheOperation *cop;
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Client %p disconnected\n",
304 for (zm = monitor_head; NULL != zm; zm = zm->next)
308 GNUNET_CONTAINER_DLL_remove (monitor_head,
311 if (NULL != zm->task)
313 GNUNET_SCHEDULER_cancel (zm->task);
320 while (NULL != (no = nc->op_head))
322 GNUNET_CONTAINER_DLL_remove (nc->op_head,
327 for (cop = cop_head; NULL != cop; cop = cop->next)
335 * Add a client to our list of active clients.
338 * @param client client to add
339 * @param mq message queue for @a client
340 * @return internal namestore client structure for this client
343 client_connect_cb (void *cls,
344 struct GNUNET_SERVICE_Client *client,
345 struct GNUNET_MQ_Handle *mq)
347 struct NamestoreClient *nc;
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Client %p connected\n",
352 nc = GNUNET_new (struct NamestoreClient);
360 * Function called with the records for the #GNUNET_GNS_MASTERZONE_STR
361 * label in the zone. Used to locate the #GNUNET_GNSRECORD_TYPE_NICK
362 * record, which (if found) is then copied to @a cls for future use.
364 * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
365 * @param private_key the private key of the zone (unused)
366 * @param label should be #GNUNET_GNS_MASTERZONE_STR
367 * @param rd_count number of records in @a rd
368 * @param rd records stored under @a label in the zone
371 lookup_nick_it (void *cls,
372 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
374 unsigned int rd_count,
375 const struct GNUNET_GNSRECORD_Data *rd)
377 struct GNUNET_GNSRECORD_Data **res = cls;
379 if (0 != strcmp (label, GNUNET_GNS_MASTERZONE_STR))
384 for (unsigned int c = 0; c < rd_count; c++)
386 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
388 (*res) = GNUNET_malloc (rd[c].data_size + sizeof (struct GNUNET_GNSRECORD_Data));
389 (*res)->data = &(*res)[1];
390 GNUNET_memcpy ((void *) (*res)->data,
393 (*res)->data_size = rd[c].data_size;
394 (*res)->expiration_time = rd[c].expiration_time;
395 (*res)->flags = rd[c].flags;
396 (*res)->record_type = GNUNET_GNSRECORD_TYPE_NICK;
405 * Return the NICK record for the zone (if it exists).
407 * @param zone private key for the zone to look for nick
408 * @return NULL if no NICK record was found
410 static struct GNUNET_GNSRECORD_Data *
411 get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
413 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
414 struct GNUNET_GNSRECORD_Data *nick;
418 res = GSN_database->lookup_records (GSN_database->cls,
420 GNUNET_GNS_MASTERZONE_STR,
423 if ( (GNUNET_OK != res) ||
426 GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
427 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
428 "No nick name set for zone `%s'\n",
429 GNUNET_GNSRECORD_z2s (&pub));
437 merge_with_nick_records (const struct GNUNET_GNSRECORD_Data *nick_rd,
439 const struct GNUNET_GNSRECORD_Data *rd2,
440 unsigned int *rdc_res,
441 struct GNUNET_GNSRECORD_Data **rd_res)
443 uint64_t latest_expiration;
449 (*rdc_res) = 1 + rdc2;
457 for (unsigned int c=0; c< 1; c++)
458 req += sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd[c].data_size;
459 for (unsigned int c=0; c< rdc2; c++)
460 req += sizeof (struct GNUNET_GNSRECORD_Data) + rd2[c].data_size;
461 (*rd_res) = GNUNET_malloc (req);
462 data = (char *) &(*rd_res)[1 + rdc2];
464 latest_expiration = 0;
466 for (unsigned int c=0; c< rdc2; c++)
468 if (0 != (rd2[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
470 if ((GNUNET_TIME_absolute_get().abs_value_us + rd2[c].expiration_time) >
472 latest_expiration = rd2[c].expiration_time;
474 else if (rd2[c].expiration_time > latest_expiration)
475 latest_expiration = rd2[c].expiration_time;
476 (*rd_res)[c] = rd2[c];
477 (*rd_res)[c].data = (void *) &data[data_offset];
478 GNUNET_memcpy ((void *) (*rd_res)[c].data,
481 data_offset += (*rd_res)[c].data_size;
483 record_offset = rdc2;
484 for (unsigned int c=0; c< 1; c++)
486 (*rd_res)[c+record_offset] = nick_rd[c];
487 (*rd_res)[c+record_offset].expiration_time = latest_expiration;
488 (*rd_res)[c+record_offset].data = (void *) &data[data_offset];
489 GNUNET_memcpy ((void *) (*rd_res)[c+record_offset].data,
491 nick_rd[c].data_size);
492 data_offset += (*rd_res)[c+record_offset].data_size;
494 GNUNET_assert (req == (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
499 * Generate a `struct LookupNameResponseMessage` and send it to the
500 * given client using the given notification context.
502 * @param nc client to unicast to
503 * @param request_id request ID to use
504 * @param zone_key zone key of the zone
506 * @param rd_count number of records in @a rd
507 * @param rd array of records
510 send_lookup_response (struct NamestoreClient *nc,
512 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
514 unsigned int rd_count,
515 const struct GNUNET_GNSRECORD_Data *rd)
517 struct GNUNET_MQ_Envelope *env;
518 struct RecordResultMessage *zir_msg;
519 struct GNUNET_GNSRECORD_Data *nick;
520 struct GNUNET_GNSRECORD_Data *res;
521 unsigned int res_count;
527 nick = get_nick_record (zone_key);
528 if ((NULL != nick) && (0 != strcmp(name, GNUNET_GNS_MASTERZONE_STR)))
530 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
531 merge_with_nick_records (nick,
540 res_count = rd_count;
541 res = (struct GNUNET_GNSRECORD_Data *) rd;
544 name_len = strlen (name) + 1;
545 rd_ser_len = GNUNET_GNSRECORD_records_get_size (res_count, res);
546 env = GNUNET_MQ_msg_extra (zir_msg,
547 name_len + rd_ser_len,
548 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
549 zir_msg->gns_header.r_id = htonl (request_id);
550 zir_msg->name_len = htons (name_len);
551 zir_msg->rd_count = htons (res_count);
552 zir_msg->rd_len = htons (rd_ser_len);
553 zir_msg->private_key = *zone_key;
554 name_tmp = (char *) &zir_msg[1];
555 GNUNET_memcpy (name_tmp,
558 rd_ser = &name_tmp[name_len];
559 GNUNET_GNSRECORD_records_serialize (res_count,
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
564 "Sending RECORD_RESULT message with %u records\n",
566 GNUNET_MQ_send (nc->mq,
574 * Send response to the store request to the client.
576 * @param client client to talk to
577 * @param res status of the operation
578 * @param rid client's request ID
581 send_store_response (struct NamestoreClient *nc,
585 struct GNUNET_MQ_Envelope *env;
586 struct RecordStoreResponseMessage *rcr_msg;
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 "Sending RECORD_STORE_RESPONSE message\n");
590 env = GNUNET_MQ_msg (rcr_msg,
591 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
592 rcr_msg->gns_header.r_id = htonl (rid);
593 rcr_msg->op_result = htonl (res);
594 GNUNET_MQ_send (nc->mq,
600 * Cache operation complete, clean up.
602 * @param cls the `struct CacheOperation`
603 * @param success success
604 * @param emsg error messages
607 finish_cache_operation (void *cls,
611 struct CacheOperation *cop = cls;
614 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
615 _("Failed to replicate block in namecache: %s\n"),
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "CACHE operation completed\n");
620 GNUNET_CONTAINER_DLL_remove (cop_head,
624 send_store_response (cop->nc,
632 * We just touched the plaintext information about a name in our zone;
633 * refresh the corresponding (encrypted) block in the namecache.
635 * @param nc client responsible for the request, can be NULL
636 * @param rid request ID of the client
637 * @param zone_key private key of the zone
638 * @param name label for the records
639 * @param rd_count number of records
640 * @param rd records stored under the given @a name
643 refresh_block (struct NamestoreClient *nc,
645 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
647 unsigned int rd_count,
648 const struct GNUNET_GNSRECORD_Data *rd)
650 struct GNUNET_GNSRECORD_Block *block;
651 struct CacheOperation *cop;
652 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
653 struct GNUNET_GNSRECORD_Data *nick;
654 struct GNUNET_GNSRECORD_Data *res;
655 unsigned int res_count;
657 nick = get_nick_record (zone_key);
658 res_count = rd_count;
659 res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
662 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
663 merge_with_nick_records (nick,
671 block = GNUNET_GNSRECORD_block_create (zone_key,
672 GNUNET_TIME_UNIT_ZERO_ABS,
676 block = GNUNET_GNSRECORD_block_create (zone_key,
677 GNUNET_GNSRECORD_record_get_expiration_time (res_count,
681 GNUNET_assert (NULL != block);
682 GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "Caching block for label `%s' with %u records in zone `%s' in namecache\n",
688 GNUNET_GNSRECORD_z2s (&pkey));
689 cop = GNUNET_new (struct CacheOperation);
692 GNUNET_CONTAINER_DLL_insert (cop_head,
695 cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
697 &finish_cache_operation,
704 * Closure for #lookup_it().
706 struct RecordLookupContext
712 unsigned int res_rd_count;
718 struct GNUNET_GNSRECORD_Data *nick;
723 lookup_it (void *cls,
724 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
726 unsigned int rd_count,
727 const struct GNUNET_GNSRECORD_Data *rd)
729 struct RecordLookupContext *rlc = cls;
730 struct GNUNET_GNSRECORD_Data *rd_res;
731 unsigned int rdc_res;
733 if (0 == strcmp (label, rlc->label))
735 rlc->found = GNUNET_YES;
738 if ( (NULL != rlc->nick) &&
740 GNUNET_GNS_MASTERZONE_STR)) )
745 rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
746 merge_with_nick_records (rlc->nick,
750 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res,
752 rlc->res_rd_count = rdc_res;
753 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
754 GNUNET_GNSRECORD_records_serialize (rdc_res,
759 GNUNET_free (rd_res);
760 GNUNET_free (rlc->nick);
765 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
767 rlc->res_rd_count = rd_count;
768 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
769 GNUNET_GNSRECORD_records_serialize (rd_count,
778 rlc->res_rd_count = 0;
786 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
788 * @param cls client sending the message
789 * @param ll_msg message of type `struct LabelLookupMessage`
790 * @return #GNUNET_OK if @a ll_msg is well-formed
793 check_record_lookup (void *cls,
794 const struct LabelLookupMessage *ll_msg)
798 const char *name_tmp;
800 name_len = ntohl (ll_msg->label_len);
801 src_size = ntohs (ll_msg->gns_header.header.size);
802 if (name_len != src_size - sizeof (struct LabelLookupMessage))
805 return GNUNET_SYSERR;
808 name_tmp = (const char *) &ll_msg[1];
809 if ('\0' != name_tmp[name_len -1])
812 return GNUNET_SYSERR;
819 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
821 * @param cls client sending the message
822 * @param ll_msg message of type `struct LabelLookupMessage`
825 handle_record_lookup (void *cls,
826 const struct LabelLookupMessage *ll_msg)
828 struct NamestoreClient *nc = cls;
829 struct GNUNET_MQ_Envelope *env;
830 struct LabelLookupResponseMessage *llr_msg;
831 struct RecordLookupContext rlc;
832 const char *name_tmp;
838 name_len = ntohl (ll_msg->label_len);
839 name_tmp = (const char *) &ll_msg[1];
840 GNUNET_SERVICE_client_continue (nc->client);
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 "Received NAMESTORE_RECORD_LOOKUP message for name `%s'\n",
845 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
846 if (NULL == conv_name)
848 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
849 "Error converting name `%s'\n",
851 GNUNET_SERVICE_client_drop (nc->client);
854 rlc.label = conv_name;
855 rlc.found = GNUNET_NO;
856 rlc.res_rd_count = 0;
859 rlc.nick = get_nick_record (&ll_msg->zone);
860 res = GSN_database->lookup_records (GSN_database->cls,
865 GNUNET_free (conv_name);
866 env = GNUNET_MQ_msg_extra (llr_msg,
867 name_len + rlc.rd_ser_len,
868 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
869 llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
870 llr_msg->private_key = ll_msg->zone;
871 llr_msg->name_len = htons (name_len);
872 llr_msg->rd_count = htons (rlc.res_rd_count);
873 llr_msg->rd_len = htons (rlc.rd_ser_len);
874 res_name = (char *) &llr_msg[1];
875 if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
876 llr_msg->found = ntohs (GNUNET_YES);
878 llr_msg->found = ntohs (GNUNET_NO);
879 GNUNET_memcpy (&llr_msg[1],
882 GNUNET_memcpy (&res_name[name_len],
885 GNUNET_MQ_send (nc->mq,
887 GNUNET_free_non_null (rlc.res_rd);
892 * Checks a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
894 * @param cls client sending the message
895 * @param rp_msg message of type `struct RecordStoreMessage`
896 * @return #GNUNET_OK if @a rp_msg is well-formed
899 check_record_store (void *cls,
900 const struct RecordStoreMessage *rp_msg)
906 const char *name_tmp;
908 name_len = ntohs (rp_msg->name_len);
909 msg_size = ntohs (rp_msg->gns_header.header.size);
910 rd_ser_len = ntohs (rp_msg->rd_len);
911 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
912 if (msg_size != msg_size_exp)
915 return GNUNET_SYSERR;
917 if ((0 == name_len) || (name_len > MAX_NAME_LEN))
920 return GNUNET_SYSERR;
922 name_tmp = (const char *) &rp_msg[1];
923 if ('\0' != name_tmp[name_len -1])
926 return GNUNET_SYSERR;
933 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
935 * @param cls client sending the message
936 * @param rp_msg message of type `struct RecordStoreMessage`
939 handle_record_store (void *cls,
940 const struct RecordStoreMessage *rp_msg)
942 struct NamestoreClient *nc = cls;
946 const char *name_tmp;
949 unsigned int rd_count;
951 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
952 struct ZoneMonitor *zm;
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
955 "Received NAMESTORE_RECORD_STORE message\n");
956 rid = ntohl (rp_msg->gns_header.r_id);
957 name_len = ntohs (rp_msg->name_len);
958 rd_count = ntohs (rp_msg->rd_count);
959 rd_ser_len = ntohs (rp_msg->rd_len);
960 GNUNET_break (0 == ntohs (rp_msg->reserved));
961 name_tmp = (const char *) &rp_msg[1];
962 rd_ser = &name_tmp[name_len];
964 struct GNUNET_GNSRECORD_Data rd[rd_count];
967 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
973 GNUNET_SERVICE_client_drop (nc->client);
977 /* Extracting and converting private key */
978 GNUNET_CRYPTO_ecdsa_key_get_public (&rp_msg->private_key,
980 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
981 if (NULL == conv_name)
983 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
984 "Error converting name `%s'\n",
986 GNUNET_SERVICE_client_drop (nc->client);
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
990 "Creating %u records for name `%s' in zone `%s'\n",
991 (unsigned int) rd_count,
993 GNUNET_GNSRECORD_z2s (&pubkey));
995 if ( (0 == rd_count) &&
997 GSN_database->iterate_records (GSN_database->cls,
998 &rp_msg->private_key,
1003 /* This name does not exist, so cannot be removed */
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "Name `%s' does not exist, no deletion required\n",
1011 struct GNUNET_GNSRECORD_Data rd_clean[rd_count];
1012 unsigned int rd_clean_off;
1014 /* remove "NICK" records, unless this is for the "+" label */
1016 for (unsigned int i=0;i<rd_count;i++)
1018 rd_clean[rd_clean_off] = rd[i];
1019 if ( (0 == strcmp (GNUNET_GNS_MASTERZONE_STR,
1021 (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
1024 res = GSN_database->store_records (GSN_database->cls,
1025 &rp_msg->private_key,
1029 if (GNUNET_OK == res)
1031 for (zm = monitor_head; NULL != zm; zm = zm->next)
1033 if ( (0 == memcmp (&rp_msg->private_key, &zm->zone,
1034 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) ||
1035 (0 == memcmp (&zm->zone,
1037 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1040 "Notifying monitor about changes under label `%s'\n",
1042 send_lookup_response (zm->nc,
1044 &rp_msg->private_key,
1049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1050 "Monitor is for another zone\n");
1052 if (NULL == monitor_head)
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 "No monitors active\n");
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "Error storing record: %d\n",
1063 if (GNUNET_OK == res)
1067 &rp_msg->private_key,
1071 GNUNET_SERVICE_client_continue (nc->client);
1072 GNUNET_free (conv_name);
1075 GNUNET_free (conv_name);
1077 send_store_response (nc,
1080 GNUNET_SERVICE_client_continue (nc->client);
1085 * Context for record remove operations passed from #handle_zone_to_name to
1086 * #handle_zone_to_name_it as closure
1088 struct ZoneToNameCtx
1093 struct NamestoreClient *nc;
1096 * Request id (to be used in the response to the client).
1101 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
1102 * not finding a name for the zone still counts as a 'success' here,
1103 * as this field is about the success of executing the IPC protocol.
1110 * Zone to name iterator
1112 * @param cls struct ZoneToNameCtx *
1113 * @param zone_key the zone key
1115 * @param rd_count number of records in @a rd
1116 * @param rd record data
1119 handle_zone_to_name_it (void *cls,
1120 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1122 unsigned int rd_count,
1123 const struct GNUNET_GNSRECORD_Data *rd)
1125 struct ZoneToNameCtx *ztn_ctx = cls;
1126 struct GNUNET_MQ_Envelope *env;
1127 struct ZoneToNameResponseMessage *ztnr_msg;
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136 "Found result for zone-to-name lookup: `%s'\n",
1139 name_len = (NULL == name) ? 0 : strlen (name) + 1;
1140 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
1141 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
1142 if (msg_size >= GNUNET_MAX_MESSAGE_SIZE)
1145 ztn_ctx->success = GNUNET_SYSERR;
1148 env = GNUNET_MQ_msg_extra (ztnr_msg,
1149 name_len + rd_ser_len,
1150 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1151 ztnr_msg->gns_header.header.size = htons (msg_size);
1152 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1153 ztnr_msg->res = htons (res);
1154 ztnr_msg->rd_len = htons (rd_ser_len);
1155 ztnr_msg->rd_count = htons (rd_count);
1156 ztnr_msg->name_len = htons (name_len);
1157 ztnr_msg->zone = *zone_key;
1158 name_tmp = (char *) &ztnr_msg[1];
1159 GNUNET_memcpy (name_tmp,
1162 rd_tmp = &name_tmp[name_len];
1163 GNUNET_GNSRECORD_records_serialize (rd_count,
1167 ztn_ctx->success = GNUNET_OK;
1168 GNUNET_MQ_send (ztn_ctx->nc->mq,
1174 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
1176 * @param cls client client sending the message
1177 * @param ztn_msg message of type 'struct ZoneToNameMessage'
1180 handle_zone_to_name (void *cls,
1181 const struct ZoneToNameMessage *ztn_msg)
1183 struct NamestoreClient *nc = cls;
1184 struct ZoneToNameCtx ztn_ctx;
1185 struct GNUNET_MQ_Envelope *env;
1186 struct ZoneToNameResponseMessage *ztnr_msg;
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "Received `%s' message\n",
1191 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
1193 ztn_ctx.success = GNUNET_NO;
1194 if (GNUNET_SYSERR ==
1195 GSN_database->zone_to_name (GSN_database->cls,
1197 &ztn_msg->value_zone,
1198 &handle_zone_to_name_it, &ztn_ctx))
1200 /* internal error, hang up instead of signalling something
1201 that might be wrong */
1203 GNUNET_SERVICE_client_drop (nc->client);
1206 if (GNUNET_NO == ztn_ctx.success)
1208 /* no result found, send empty response */
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1210 "Found no result for zone-to-name lookup.\n");
1211 env = GNUNET_MQ_msg (ztnr_msg,
1212 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1213 ztnr_msg->gns_header.r_id = ztn_msg->gns_header.r_id;
1214 ztnr_msg->res = htons (GNUNET_NO);
1215 GNUNET_MQ_send (nc->mq,
1218 GNUNET_SERVICE_client_continue (nc->client);
1223 * Zone iteration processor result
1225 enum ZoneIterationResult
1234 * Continue to iterate with next iteration_next call
1236 IT_SUCCESS_MORE_AVAILABLE = 1,
1239 * Iteration complete
1241 IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE = 2
1246 * Context for record remove operations passed from
1247 * #run_zone_iteration_round to #zone_iterate_proc as closure
1249 struct ZoneIterationProcResult
1252 * The zone iteration handle
1254 struct ZoneIteration *zi;
1257 * Iteration result: iteration done?
1258 * #IT_SUCCESS_MORE_AVAILABLE: if there may be more results overall but
1259 * we got one for now and have sent it to the client
1260 * #IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE: if there are no further results,
1261 * #IT_START: if we are still trying to find a result.
1263 int res_iteration_finished;
1269 * Process results for zone iteration from database
1271 * @param cls struct ZoneIterationProcResult *proc
1272 * @param zone_key the zone key
1274 * @param rd_count number of records for this name
1275 * @param rd record data
1278 zone_iterate_proc (void *cls,
1279 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1281 unsigned int rd_count,
1282 const struct GNUNET_GNSRECORD_Data *rd)
1284 struct ZoneIterationProcResult *proc = cls;
1285 int do_refresh_block;
1287 if ((NULL == zone_key) && (NULL == name))
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Iteration done\n");
1291 proc->res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1294 if ((NULL == zone_key) || (NULL == name))
1296 /* what is this!? should never happen */
1297 proc->res_iteration_finished = IT_START;
1301 proc->res_iteration_finished = IT_SUCCESS_MORE_AVAILABLE;
1302 send_lookup_response (proc->zi->nc,
1303 proc->zi->request_id,
1308 do_refresh_block = GNUNET_NO;
1309 for (unsigned int i=0;i<rd_count;i++)
1310 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
1312 do_refresh_block = GNUNET_YES;
1315 if (GNUNET_YES == do_refresh_block)
1316 refresh_block (NULL,
1327 * Perform the next round of the zone iteration.
1329 * @param zi zone iterator to process
1332 run_zone_iteration_round (struct ZoneIteration *zi)
1334 struct ZoneIterationProcResult proc;
1335 struct GNUNET_MQ_Envelope *env;
1336 struct RecordResultMessage *rrm;
1339 memset (&proc, 0, sizeof (proc));
1341 proc.res_iteration_finished = IT_START;
1342 while (IT_START == proc.res_iteration_finished)
1344 if (GNUNET_SYSERR ==
1345 (ret = GSN_database->iterate_records (GSN_database->cls,
1346 (0 == memcmp (&zi->zone, &zero, sizeof (zero)))
1350 &zone_iterate_proc, &proc)))
1355 if (GNUNET_NO == ret)
1356 proc.res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1359 if (IT_SUCCESS_MORE_AVAILABLE == proc.res_iteration_finished)
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1362 "More results available\n");
1363 return; /* more results later */
1365 /* send empty response to indicate end of list */
1366 env = GNUNET_MQ_msg (rrm,
1367 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
1368 rrm->gns_header.r_id = htonl (zi->request_id);
1369 GNUNET_MQ_send (zi->nc->mq,
1371 GNUNET_CONTAINER_DLL_remove (zi->nc->op_head,
1379 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1381 * @param cls the client sending the message
1382 * @param zis_msg message from the client
1385 handle_iteration_start (void *cls,
1386 const struct ZoneIterationStartMessage *zis_msg)
1388 struct NamestoreClient *nc = cls;
1389 struct ZoneIteration *zi;
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "Received ZONE_ITERATION_START message\n");
1393 zi = GNUNET_new (struct ZoneIteration);
1394 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1397 zi->zone = zis_msg->zone;
1399 GNUNET_CONTAINER_DLL_insert (nc->op_head,
1402 run_zone_iteration_round (zi);
1403 GNUNET_SERVICE_client_continue (nc->client);
1408 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1410 * @param cls the client sending the message
1411 * @param zis_msg message from the client
1414 handle_iteration_stop (void *cls,
1415 const struct ZoneIterationStopMessage *zis_msg)
1417 struct NamestoreClient *nc = cls;
1418 struct ZoneIteration *zi;
1421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1422 "Received `%s' message\n",
1423 "ZONE_ITERATION_STOP");
1424 rid = ntohl (zis_msg->gns_header.r_id);
1425 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1426 if (zi->request_id == rid)
1431 GNUNET_SERVICE_client_drop (nc->client);
1434 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1438 GNUNET_SERVICE_client_continue (nc->client);
1443 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1445 * @param cls the client sending the message
1446 * @param message message from the client
1449 handle_iteration_next (void *cls,
1450 const struct ZoneIterationNextMessage *zis_msg)
1452 struct NamestoreClient *nc = cls;
1453 struct ZoneIteration *zi;
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1457 "Received ZONE_ITERATION_NEXT message\n");
1458 rid = ntohl (zis_msg->gns_header.r_id);
1459 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1460 if (zi->request_id == rid)
1465 GNUNET_SERVICE_client_drop (nc->client);
1468 run_zone_iteration_round (zi);
1469 GNUNET_SERVICE_client_continue (nc->client);
1474 * Send 'sync' message to zone monitor, we're now in sync.
1476 * @param zm monitor that is now in sync
1479 monitor_sync (struct ZoneMonitor *zm)
1481 struct GNUNET_MQ_Envelope *env;
1482 struct GNUNET_MessageHeader *sync;
1484 env = GNUNET_MQ_msg (sync,
1485 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1486 GNUNET_MQ_send (zm->nc->mq,
1492 * Obtain the next datum during the zone monitor's zone intiial iteration.
1494 * @param cls zone monitor that does its initial iteration
1497 monitor_next (void *cls);
1501 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1503 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1504 * @param zone_key zone key of the zone
1506 * @param rd_count number of records in @a rd
1507 * @param rd array of records
1510 monitor_iterate_cb (void *cls,
1511 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1513 unsigned int rd_count,
1514 const struct GNUNET_GNSRECORD_Data *rd)
1516 struct ZoneMonitor *zm = cls;
1520 /* finished with iteration */
1524 send_lookup_response (zm->nc,
1530 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next,
1536 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
1538 * @param cls the client sending the message
1539 * @param message message from the client
1542 handle_monitor_start (void *cls,
1543 const struct ZoneMonitorStartMessage *zis_msg)
1545 struct NamestoreClient *nc = cls;
1546 struct ZoneMonitor *zm;
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1549 "Received ZONE_MONITOR_START message\n");
1550 zm = GNUNET_new (struct ZoneMonitor);
1552 zm->zone = zis_msg->zone;
1553 GNUNET_CONTAINER_DLL_insert (monitor_head,
1556 GNUNET_SERVICE_client_mark_monitor (nc->client);
1557 GNUNET_SERVICE_client_disable_continue_warning (nc->client);
1558 GNUNET_notification_context_add (monitor_nc,
1560 if (GNUNET_YES == ntohl (zis_msg->iterate_first))
1561 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next,
1569 * Obtain the next datum during the zone monitor's zone intiial iteration.
1571 * @param cls zone monitor that does its initial iteration
1574 monitor_next (void *cls)
1576 struct ZoneMonitor *zm = cls;
1580 ret = GSN_database->iterate_records (GSN_database->cls,
1581 (0 == memcmp (&zm->zone,
1587 &monitor_iterate_cb,
1589 if (GNUNET_SYSERR == ret)
1591 GNUNET_SERVICE_client_drop (zm->nc->client);
1594 if (GNUNET_NO == ret)
1604 * Process namestore requests.
1606 * @param cls closure
1607 * @param cfg configuration to use
1608 * @param service the initialized service
1612 const struct GNUNET_CONFIGURATION_Handle *cfg,
1613 struct GNUNET_SERVICE_Handle *service)
1617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1618 "Starting namestore service\n");
1620 monitor_nc = GNUNET_notification_context_create (1);
1621 namecache = GNUNET_NAMECACHE_connect (cfg);
1622 /* Loading database plugin */
1624 GNUNET_CONFIGURATION_get_value_string (cfg,
1628 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1629 "No database backend configured\n");
1631 GNUNET_asprintf (&db_lib_name,
1632 "libgnunet_plugin_namestore_%s",
1634 GSN_database = GNUNET_PLUGIN_load (db_lib_name,
1636 GNUNET_free (database);
1637 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
1639 if (NULL == GSN_database)
1641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1642 "Could not load database backend `%s'\n",
1644 GNUNET_SCHEDULER_shutdown ();
1651 * Define "main" method using service macro.
1655 GNUNET_SERVICE_OPTION_NONE,
1658 &client_disconnect_cb,
1660 GNUNET_MQ_hd_var_size (record_store,
1661 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE,
1662 struct RecordStoreMessage,
1664 GNUNET_MQ_hd_var_size (record_lookup,
1665 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP,
1666 struct LabelLookupMessage,
1668 GNUNET_MQ_hd_fixed_size (zone_to_name,
1669 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME,
1670 struct ZoneToNameMessage,
1672 GNUNET_MQ_hd_fixed_size (iteration_start,
1673 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START,
1674 struct ZoneIterationStartMessage,
1676 GNUNET_MQ_hd_fixed_size (iteration_next,
1677 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT,
1678 struct ZoneIterationNextMessage,
1680 GNUNET_MQ_hd_fixed_size (iteration_stop,
1681 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP,
1682 struct ZoneIterationStopMessage,
1684 GNUNET_MQ_hd_fixed_size (monitor_start,
1685 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START,
1686 struct ZoneMonitorStartMessage,
1688 GNUNET_MQ_handler_end ());
1691 /* end of gnunet-service-namestore.c */