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_log (GNUNET_ERROR_TYPE_WARNING,
264 "Aborting incomplete namecache operation\n");
265 GNUNET_NAMECACHE_cancel (cop->qe);
266 GNUNET_CONTAINER_DLL_remove (cop_head,
271 GNUNET_NAMECACHE_disconnect (namecache);
273 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name,
275 GNUNET_free (db_lib_name);
277 if (NULL != monitor_nc)
279 GNUNET_notification_context_destroy (monitor_nc);
286 * Called whenever a client is disconnected.
287 * Frees our resources associated with that client.
290 * @param client identification of the client
291 * @param app_ctx the `struct NamestoreClient` of @a client
294 client_disconnect_cb (void *cls,
295 struct GNUNET_SERVICE_Client *client,
298 struct NamestoreClient *nc = app_ctx;
299 struct ZoneIteration *no;
300 struct ZoneMonitor *zm;
301 struct CacheOperation *cop;
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 "Client %p disconnected\n",
306 for (zm = monitor_head; NULL != zm; zm = zm->next)
310 GNUNET_CONTAINER_DLL_remove (monitor_head,
313 if (NULL != zm->task)
315 GNUNET_SCHEDULER_cancel (zm->task);
322 while (NULL != (no = nc->op_head))
324 GNUNET_CONTAINER_DLL_remove (nc->op_head,
329 for (cop = cop_head; NULL != cop; cop = cop->next)
337 * Add a client to our list of active clients.
340 * @param client client to add
341 * @param mq message queue for @a client
342 * @return internal namestore client structure for this client
345 client_connect_cb (void *cls,
346 struct GNUNET_SERVICE_Client *client,
347 struct GNUNET_MQ_Handle *mq)
349 struct NamestoreClient *nc;
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Client %p connected\n",
354 nc = GNUNET_new (struct NamestoreClient);
362 * Function called with the records for the #GNUNET_GNS_MASTERZONE_STR
363 * label in the zone. Used to locate the #GNUNET_GNSRECORD_TYPE_NICK
364 * record, which (if found) is then copied to @a cls for future use.
366 * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
367 * @param private_key the private key of the zone (unused)
368 * @param label should be #GNUNET_GNS_MASTERZONE_STR
369 * @param rd_count number of records in @a rd
370 * @param rd records stored under @a label in the zone
373 lookup_nick_it (void *cls,
374 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
376 unsigned int rd_count,
377 const struct GNUNET_GNSRECORD_Data *rd)
379 struct GNUNET_GNSRECORD_Data **res = cls;
381 if (0 != strcmp (label, GNUNET_GNS_MASTERZONE_STR))
386 for (unsigned int c = 0; c < rd_count; c++)
388 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
390 (*res) = GNUNET_malloc (rd[c].data_size + sizeof (struct GNUNET_GNSRECORD_Data));
391 (*res)->data = &(*res)[1];
392 GNUNET_memcpy ((void *) (*res)->data,
395 (*res)->data_size = rd[c].data_size;
396 (*res)->expiration_time = rd[c].expiration_time;
397 (*res)->flags = rd[c].flags;
398 (*res)->record_type = GNUNET_GNSRECORD_TYPE_NICK;
407 * Return the NICK record for the zone (if it exists).
409 * @param zone private key for the zone to look for nick
410 * @return NULL if no NICK record was found
412 static struct GNUNET_GNSRECORD_Data *
413 get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
415 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
416 struct GNUNET_GNSRECORD_Data *nick;
420 res = GSN_database->lookup_records (GSN_database->cls,
422 GNUNET_GNS_MASTERZONE_STR,
425 if ( (GNUNET_OK != res) ||
428 GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
429 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
430 "No nick name set for zone `%s'\n",
431 GNUNET_GNSRECORD_z2s (&pub));
439 merge_with_nick_records (const struct GNUNET_GNSRECORD_Data *nick_rd,
441 const struct GNUNET_GNSRECORD_Data *rd2,
442 unsigned int *rdc_res,
443 struct GNUNET_GNSRECORD_Data **rd_res)
445 uint64_t latest_expiration;
451 (*rdc_res) = 1 + rdc2;
459 for (unsigned int c=0; c< 1; c++)
460 req += sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd[c].data_size;
461 for (unsigned int c=0; c< rdc2; c++)
462 req += sizeof (struct GNUNET_GNSRECORD_Data) + rd2[c].data_size;
463 (*rd_res) = GNUNET_malloc (req);
464 data = (char *) &(*rd_res)[1 + rdc2];
466 latest_expiration = 0;
468 for (unsigned int c=0; c< rdc2; c++)
470 if (0 != (rd2[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
472 if ((GNUNET_TIME_absolute_get().abs_value_us + rd2[c].expiration_time) >
474 latest_expiration = rd2[c].expiration_time;
476 else if (rd2[c].expiration_time > latest_expiration)
477 latest_expiration = rd2[c].expiration_time;
478 (*rd_res)[c] = rd2[c];
479 (*rd_res)[c].data = (void *) &data[data_offset];
480 GNUNET_memcpy ((void *) (*rd_res)[c].data,
483 data_offset += (*rd_res)[c].data_size;
485 record_offset = rdc2;
486 for (unsigned int c=0; c< 1; c++)
488 (*rd_res)[c+record_offset] = nick_rd[c];
489 (*rd_res)[c+record_offset].expiration_time = latest_expiration;
490 (*rd_res)[c+record_offset].data = (void *) &data[data_offset];
491 GNUNET_memcpy ((void *) (*rd_res)[c+record_offset].data,
493 nick_rd[c].data_size);
494 data_offset += (*rd_res)[c+record_offset].data_size;
496 GNUNET_assert (req == (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
501 * Generate a `struct LookupNameResponseMessage` and send it to the
502 * given client using the given notification context.
504 * @param nc client to unicast to
505 * @param request_id request ID to use
506 * @param zone_key zone key of the zone
508 * @param rd_count number of records in @a rd
509 * @param rd array of records
512 send_lookup_response (struct NamestoreClient *nc,
514 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
516 unsigned int rd_count,
517 const struct GNUNET_GNSRECORD_Data *rd)
519 struct GNUNET_MQ_Envelope *env;
520 struct RecordResultMessage *zir_msg;
521 struct GNUNET_GNSRECORD_Data *nick;
522 struct GNUNET_GNSRECORD_Data *res;
523 unsigned int res_count;
529 nick = get_nick_record (zone_key);
530 if ((NULL != nick) && (0 != strcmp(name, GNUNET_GNS_MASTERZONE_STR)))
532 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
533 merge_with_nick_records (nick,
542 res_count = rd_count;
543 res = (struct GNUNET_GNSRECORD_Data *) rd;
546 name_len = strlen (name) + 1;
547 rd_ser_len = GNUNET_GNSRECORD_records_get_size (res_count, res);
548 env = GNUNET_MQ_msg_extra (zir_msg,
549 name_len + rd_ser_len,
550 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
551 zir_msg->gns_header.r_id = htonl (request_id);
552 zir_msg->name_len = htons (name_len);
553 zir_msg->rd_count = htons (res_count);
554 zir_msg->rd_len = htons (rd_ser_len);
555 zir_msg->private_key = *zone_key;
556 name_tmp = (char *) &zir_msg[1];
557 GNUNET_memcpy (name_tmp,
560 rd_ser = &name_tmp[name_len];
561 GNUNET_GNSRECORD_records_serialize (res_count,
565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
566 "Sending RECORD_RESULT message with %u records\n",
568 GNUNET_MQ_send (nc->mq,
576 * Send response to the store request to the client.
578 * @param client client to talk to
579 * @param res status of the operation
580 * @param rid client's request ID
583 send_store_response (struct NamestoreClient *nc,
587 struct GNUNET_MQ_Envelope *env;
588 struct RecordStoreResponseMessage *rcr_msg;
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 "Sending RECORD_STORE_RESPONSE message\n");
592 env = GNUNET_MQ_msg (rcr_msg,
593 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
594 rcr_msg->gns_header.r_id = htonl (rid);
595 rcr_msg->op_result = htonl (res);
596 GNUNET_MQ_send (nc->mq,
602 * Cache operation complete, clean up.
604 * @param cls the `struct CacheOperation`
605 * @param success success
606 * @param emsg error messages
609 finish_cache_operation (void *cls,
613 struct CacheOperation *cop = cls;
616 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
617 _("Failed to replicate block in namecache: %s\n"),
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "CACHE operation completed\n");
622 GNUNET_CONTAINER_DLL_remove (cop_head,
626 send_store_response (cop->nc,
634 * We just touched the plaintext information about a name in our zone;
635 * refresh the corresponding (encrypted) block in the namecache.
637 * @param nc client responsible for the request, can be NULL
638 * @param rid request ID of the client
639 * @param zone_key private key of the zone
640 * @param name label for the records
641 * @param rd_count number of records
642 * @param rd records stored under the given @a name
645 refresh_block (struct NamestoreClient *nc,
647 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
649 unsigned int rd_count,
650 const struct GNUNET_GNSRECORD_Data *rd)
652 struct GNUNET_GNSRECORD_Block *block;
653 struct CacheOperation *cop;
654 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
655 struct GNUNET_GNSRECORD_Data *nick;
656 struct GNUNET_GNSRECORD_Data *res;
657 unsigned int res_count;
659 nick = get_nick_record (zone_key);
660 res_count = rd_count;
661 res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
664 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
665 merge_with_nick_records (nick,
673 block = GNUNET_GNSRECORD_block_create (zone_key,
674 GNUNET_TIME_UNIT_ZERO_ABS,
678 block = GNUNET_GNSRECORD_block_create (zone_key,
679 GNUNET_GNSRECORD_record_get_expiration_time (res_count,
683 GNUNET_assert (NULL != block);
684 GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "Caching block for label `%s' with %u records in zone `%s' in namecache\n",
690 GNUNET_GNSRECORD_z2s (&pkey));
691 cop = GNUNET_new (struct CacheOperation);
694 GNUNET_CONTAINER_DLL_insert (cop_head,
697 cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
699 &finish_cache_operation,
706 * Closure for #lookup_it().
708 struct RecordLookupContext
714 unsigned int res_rd_count;
720 struct GNUNET_GNSRECORD_Data *nick;
725 lookup_it (void *cls,
726 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
728 unsigned int rd_count,
729 const struct GNUNET_GNSRECORD_Data *rd)
731 struct RecordLookupContext *rlc = cls;
732 struct GNUNET_GNSRECORD_Data *rd_res;
733 unsigned int rdc_res;
735 if (0 == strcmp (label, rlc->label))
737 rlc->found = GNUNET_YES;
740 if ( (NULL != rlc->nick) &&
742 GNUNET_GNS_MASTERZONE_STR)) )
747 rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
748 merge_with_nick_records (rlc->nick,
752 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res,
754 rlc->res_rd_count = rdc_res;
755 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
756 GNUNET_GNSRECORD_records_serialize (rdc_res,
761 GNUNET_free (rd_res);
762 GNUNET_free (rlc->nick);
767 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count,
769 rlc->res_rd_count = rd_count;
770 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
771 GNUNET_GNSRECORD_records_serialize (rd_count,
780 rlc->res_rd_count = 0;
788 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
790 * @param cls client sending the message
791 * @param ll_msg message of type `struct LabelLookupMessage`
792 * @return #GNUNET_OK if @a ll_msg is well-formed
795 check_record_lookup (void *cls,
796 const struct LabelLookupMessage *ll_msg)
800 const char *name_tmp;
802 name_len = ntohl (ll_msg->label_len);
803 src_size = ntohs (ll_msg->gns_header.header.size);
804 if (name_len != src_size - sizeof (struct LabelLookupMessage))
807 return GNUNET_SYSERR;
810 name_tmp = (const char *) &ll_msg[1];
811 if ('\0' != name_tmp[name_len -1])
814 return GNUNET_SYSERR;
821 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
823 * @param cls client sending the message
824 * @param ll_msg message of type `struct LabelLookupMessage`
827 handle_record_lookup (void *cls,
828 const struct LabelLookupMessage *ll_msg)
830 struct NamestoreClient *nc = cls;
831 struct GNUNET_MQ_Envelope *env;
832 struct LabelLookupResponseMessage *llr_msg;
833 struct RecordLookupContext rlc;
834 const char *name_tmp;
840 name_len = ntohl (ll_msg->label_len);
841 name_tmp = (const char *) &ll_msg[1];
842 GNUNET_SERVICE_client_continue (nc->client);
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844 "Received NAMESTORE_RECORD_LOOKUP message for name `%s'\n",
847 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
848 if (NULL == conv_name)
850 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
851 "Error converting name `%s'\n",
853 GNUNET_SERVICE_client_drop (nc->client);
856 rlc.label = conv_name;
857 rlc.found = GNUNET_NO;
858 rlc.res_rd_count = 0;
861 rlc.nick = get_nick_record (&ll_msg->zone);
862 res = GSN_database->lookup_records (GSN_database->cls,
867 GNUNET_free (conv_name);
868 env = GNUNET_MQ_msg_extra (llr_msg,
869 name_len + rlc.rd_ser_len,
870 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
871 llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
872 llr_msg->private_key = ll_msg->zone;
873 llr_msg->name_len = htons (name_len);
874 llr_msg->rd_count = htons (rlc.res_rd_count);
875 llr_msg->rd_len = htons (rlc.rd_ser_len);
876 res_name = (char *) &llr_msg[1];
877 if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
878 llr_msg->found = ntohs (GNUNET_YES);
880 llr_msg->found = ntohs (GNUNET_NO);
881 GNUNET_memcpy (&llr_msg[1],
884 GNUNET_memcpy (&res_name[name_len],
887 GNUNET_MQ_send (nc->mq,
889 GNUNET_free_non_null (rlc.res_rd);
894 * Checks a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
896 * @param cls client sending the message
897 * @param rp_msg message of type `struct RecordStoreMessage`
898 * @return #GNUNET_OK if @a rp_msg is well-formed
901 check_record_store (void *cls,
902 const struct RecordStoreMessage *rp_msg)
908 const char *name_tmp;
910 name_len = ntohs (rp_msg->name_len);
911 msg_size = ntohs (rp_msg->gns_header.header.size);
912 rd_ser_len = ntohs (rp_msg->rd_len);
913 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
914 if (msg_size != msg_size_exp)
917 return GNUNET_SYSERR;
919 if ((0 == name_len) || (name_len > MAX_NAME_LEN))
922 return GNUNET_SYSERR;
924 name_tmp = (const char *) &rp_msg[1];
925 if ('\0' != name_tmp[name_len -1])
928 return GNUNET_SYSERR;
935 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
937 * @param cls client sending the message
938 * @param rp_msg message of type `struct RecordStoreMessage`
941 handle_record_store (void *cls,
942 const struct RecordStoreMessage *rp_msg)
944 struct NamestoreClient *nc = cls;
948 const char *name_tmp;
951 unsigned int rd_count;
953 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
954 struct ZoneMonitor *zm;
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 "Received NAMESTORE_RECORD_STORE message\n");
958 rid = ntohl (rp_msg->gns_header.r_id);
959 name_len = ntohs (rp_msg->name_len);
960 rd_count = ntohs (rp_msg->rd_count);
961 rd_ser_len = ntohs (rp_msg->rd_len);
962 GNUNET_break (0 == ntohs (rp_msg->reserved));
963 name_tmp = (const char *) &rp_msg[1];
964 rd_ser = &name_tmp[name_len];
966 struct GNUNET_GNSRECORD_Data rd[rd_count];
969 GNUNET_GNSRECORD_records_deserialize (rd_ser_len,
975 GNUNET_SERVICE_client_drop (nc->client);
979 /* Extracting and converting private key */
980 GNUNET_CRYPTO_ecdsa_key_get_public (&rp_msg->private_key,
982 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
983 if (NULL == conv_name)
985 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
986 "Error converting name `%s'\n",
988 GNUNET_SERVICE_client_drop (nc->client);
991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
992 "Creating %u records for name `%s' in zone `%s'\n",
993 (unsigned int) rd_count,
995 GNUNET_GNSRECORD_z2s (&pubkey));
997 if ( (0 == rd_count) &&
999 GSN_database->iterate_records (GSN_database->cls,
1000 &rp_msg->private_key,
1005 /* This name does not exist, so cannot be removed */
1006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1007 "Name `%s' does not exist, no deletion required\n",
1013 struct GNUNET_GNSRECORD_Data rd_clean[rd_count];
1014 unsigned int rd_clean_off;
1016 /* remove "NICK" records, unless this is for the
1017 #GNUNET_GNS_MASTERZONE_STR label */
1019 for (unsigned int i=0;i<rd_count;i++)
1021 rd_clean[rd_clean_off] = rd[i];
1022 if ( (0 == strcmp (GNUNET_GNS_MASTERZONE_STR,
1024 (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
1027 res = GSN_database->store_records (GSN_database->cls,
1028 &rp_msg->private_key,
1032 if (GNUNET_OK == res)
1034 for (zm = monitor_head; NULL != zm; zm = zm->next)
1036 if ( (0 == memcmp (&rp_msg->private_key, &zm->zone,
1037 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) ||
1038 (0 == memcmp (&zm->zone,
1040 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 "Notifying monitor about changes under label `%s'\n",
1045 send_lookup_response (zm->nc,
1047 &rp_msg->private_key,
1052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1053 "Monitor is for another zone\n");
1055 if (NULL == monitor_head)
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1057 "No monitors active\n");
1061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1062 "Error storing record: %d\n",
1066 if (GNUNET_OK == res)
1070 &rp_msg->private_key,
1074 GNUNET_SERVICE_client_continue (nc->client);
1075 GNUNET_free (conv_name);
1078 GNUNET_free (conv_name);
1080 send_store_response (nc,
1083 GNUNET_SERVICE_client_continue (nc->client);
1088 * Context for record remove operations passed from #handle_zone_to_name to
1089 * #handle_zone_to_name_it as closure
1091 struct ZoneToNameCtx
1096 struct NamestoreClient *nc;
1099 * Request id (to be used in the response to the client).
1104 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
1105 * not finding a name for the zone still counts as a 'success' here,
1106 * as this field is about the success of executing the IPC protocol.
1113 * Zone to name iterator
1115 * @param cls struct ZoneToNameCtx *
1116 * @param zone_key the zone key
1118 * @param rd_count number of records in @a rd
1119 * @param rd record data
1122 handle_zone_to_name_it (void *cls,
1123 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1125 unsigned int rd_count,
1126 const struct GNUNET_GNSRECORD_Data *rd)
1128 struct ZoneToNameCtx *ztn_ctx = cls;
1129 struct GNUNET_MQ_Envelope *env;
1130 struct ZoneToNameResponseMessage *ztnr_msg;
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1139 "Found result for zone-to-name lookup: `%s'\n",
1142 name_len = (NULL == name) ? 0 : strlen (name) + 1;
1143 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
1144 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
1145 if (msg_size >= GNUNET_MAX_MESSAGE_SIZE)
1148 ztn_ctx->success = GNUNET_SYSERR;
1151 env = GNUNET_MQ_msg_extra (ztnr_msg,
1152 name_len + rd_ser_len,
1153 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1154 ztnr_msg->gns_header.header.size = htons (msg_size);
1155 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1156 ztnr_msg->res = htons (res);
1157 ztnr_msg->rd_len = htons (rd_ser_len);
1158 ztnr_msg->rd_count = htons (rd_count);
1159 ztnr_msg->name_len = htons (name_len);
1160 ztnr_msg->zone = *zone_key;
1161 name_tmp = (char *) &ztnr_msg[1];
1162 GNUNET_memcpy (name_tmp,
1165 rd_tmp = &name_tmp[name_len];
1166 GNUNET_GNSRECORD_records_serialize (rd_count,
1170 ztn_ctx->success = GNUNET_OK;
1171 GNUNET_MQ_send (ztn_ctx->nc->mq,
1177 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
1179 * @param cls client client sending the message
1180 * @param ztn_msg message of type 'struct ZoneToNameMessage'
1183 handle_zone_to_name (void *cls,
1184 const struct ZoneToNameMessage *ztn_msg)
1186 struct NamestoreClient *nc = cls;
1187 struct ZoneToNameCtx ztn_ctx;
1188 struct GNUNET_MQ_Envelope *env;
1189 struct ZoneToNameResponseMessage *ztnr_msg;
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1192 "Received `%s' message\n",
1194 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
1196 ztn_ctx.success = GNUNET_NO;
1197 if (GNUNET_SYSERR ==
1198 GSN_database->zone_to_name (GSN_database->cls,
1200 &ztn_msg->value_zone,
1201 &handle_zone_to_name_it, &ztn_ctx))
1203 /* internal error, hang up instead of signalling something
1204 that might be wrong */
1206 GNUNET_SERVICE_client_drop (nc->client);
1209 if (GNUNET_NO == ztn_ctx.success)
1211 /* no result found, send empty response */
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213 "Found no result for zone-to-name lookup.\n");
1214 env = GNUNET_MQ_msg (ztnr_msg,
1215 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1216 ztnr_msg->gns_header.r_id = ztn_msg->gns_header.r_id;
1217 ztnr_msg->res = htons (GNUNET_NO);
1218 GNUNET_MQ_send (nc->mq,
1221 GNUNET_SERVICE_client_continue (nc->client);
1226 * Zone iteration processor result
1228 enum ZoneIterationResult
1237 * Continue to iterate with next iteration_next call
1239 IT_SUCCESS_MORE_AVAILABLE = 1,
1242 * Iteration complete
1244 IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE = 2
1249 * Context for record remove operations passed from
1250 * #run_zone_iteration_round to #zone_iterate_proc as closure
1252 struct ZoneIterationProcResult
1255 * The zone iteration handle
1257 struct ZoneIteration *zi;
1260 * Iteration result: iteration done?
1261 * #IT_SUCCESS_MORE_AVAILABLE: if there may be more results overall but
1262 * we got one for now and have sent it to the client
1263 * #IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE: if there are no further results,
1264 * #IT_START: if we are still trying to find a result.
1266 int res_iteration_finished;
1272 * Process results for zone iteration from database
1274 * @param cls struct ZoneIterationProcResult *proc
1275 * @param zone_key the zone key
1277 * @param rd_count number of records for this name
1278 * @param rd record data
1281 zone_iterate_proc (void *cls,
1282 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1284 unsigned int rd_count,
1285 const struct GNUNET_GNSRECORD_Data *rd)
1287 struct ZoneIterationProcResult *proc = cls;
1288 int do_refresh_block;
1290 if ((NULL == zone_key) && (NULL == name))
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1293 "Iteration done\n");
1294 proc->res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1297 if ((NULL == zone_key) || (NULL == name))
1299 /* what is this!? should never happen */
1300 proc->res_iteration_finished = IT_START;
1304 proc->res_iteration_finished = IT_SUCCESS_MORE_AVAILABLE;
1305 send_lookup_response (proc->zi->nc,
1306 proc->zi->request_id,
1311 do_refresh_block = GNUNET_NO;
1312 for (unsigned int i=0;i<rd_count;i++)
1313 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
1315 do_refresh_block = GNUNET_YES;
1318 if (GNUNET_YES == do_refresh_block)
1319 refresh_block (NULL,
1330 * Perform the next round of the zone iteration.
1332 * @param zi zone iterator to process
1335 run_zone_iteration_round (struct ZoneIteration *zi)
1337 struct ZoneIterationProcResult proc;
1338 struct GNUNET_MQ_Envelope *env;
1339 struct RecordResultMessage *rrm;
1342 memset (&proc, 0, sizeof (proc));
1344 proc.res_iteration_finished = IT_START;
1345 while (IT_START == proc.res_iteration_finished)
1347 if (GNUNET_SYSERR ==
1348 (ret = GSN_database->iterate_records (GSN_database->cls,
1349 (0 == memcmp (&zi->zone, &zero, sizeof (zero)))
1353 &zone_iterate_proc, &proc)))
1358 if (GNUNET_NO == ret)
1359 proc.res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1362 if (IT_SUCCESS_MORE_AVAILABLE == proc.res_iteration_finished)
1364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1365 "More results available\n");
1366 return; /* more results later */
1368 /* send empty response to indicate end of list */
1369 env = GNUNET_MQ_msg (rrm,
1370 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
1371 rrm->gns_header.r_id = htonl (zi->request_id);
1372 GNUNET_MQ_send (zi->nc->mq,
1374 GNUNET_CONTAINER_DLL_remove (zi->nc->op_head,
1382 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1384 * @param cls the client sending the message
1385 * @param zis_msg message from the client
1388 handle_iteration_start (void *cls,
1389 const struct ZoneIterationStartMessage *zis_msg)
1391 struct NamestoreClient *nc = cls;
1392 struct ZoneIteration *zi;
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Received ZONE_ITERATION_START message\n");
1396 zi = GNUNET_new (struct ZoneIteration);
1397 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1400 zi->zone = zis_msg->zone;
1402 GNUNET_CONTAINER_DLL_insert (nc->op_head,
1405 run_zone_iteration_round (zi);
1406 GNUNET_SERVICE_client_continue (nc->client);
1411 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1413 * @param cls the client sending the message
1414 * @param zis_msg message from the client
1417 handle_iteration_stop (void *cls,
1418 const struct ZoneIterationStopMessage *zis_msg)
1420 struct NamestoreClient *nc = cls;
1421 struct ZoneIteration *zi;
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1425 "Received `%s' message\n",
1426 "ZONE_ITERATION_STOP");
1427 rid = ntohl (zis_msg->gns_header.r_id);
1428 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1429 if (zi->request_id == rid)
1434 GNUNET_SERVICE_client_drop (nc->client);
1437 GNUNET_CONTAINER_DLL_remove (nc->op_head,
1441 GNUNET_SERVICE_client_continue (nc->client);
1446 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1448 * @param cls the client sending the message
1449 * @param message message from the client
1452 handle_iteration_next (void *cls,
1453 const struct ZoneIterationNextMessage *zis_msg)
1455 struct NamestoreClient *nc = cls;
1456 struct ZoneIteration *zi;
1459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1460 "Received ZONE_ITERATION_NEXT message\n");
1461 rid = ntohl (zis_msg->gns_header.r_id);
1462 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1463 if (zi->request_id == rid)
1468 GNUNET_SERVICE_client_drop (nc->client);
1471 run_zone_iteration_round (zi);
1472 GNUNET_SERVICE_client_continue (nc->client);
1477 * Send 'sync' message to zone monitor, we're now in sync.
1479 * @param zm monitor that is now in sync
1482 monitor_sync (struct ZoneMonitor *zm)
1484 struct GNUNET_MQ_Envelope *env;
1485 struct GNUNET_MessageHeader *sync;
1487 env = GNUNET_MQ_msg (sync,
1488 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1489 GNUNET_MQ_send (zm->nc->mq,
1495 * Obtain the next datum during the zone monitor's zone intiial iteration.
1497 * @param cls zone monitor that does its initial iteration
1500 monitor_next (void *cls);
1504 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1506 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1507 * @param zone_key zone key of the zone
1509 * @param rd_count number of records in @a rd
1510 * @param rd array of records
1513 monitor_iterate_cb (void *cls,
1514 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1516 unsigned int rd_count,
1517 const struct GNUNET_GNSRECORD_Data *rd)
1519 struct ZoneMonitor *zm = cls;
1523 /* finished with iteration */
1527 send_lookup_response (zm->nc,
1533 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next,
1539 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
1541 * @param cls the client sending the message
1542 * @param message message from the client
1545 handle_monitor_start (void *cls,
1546 const struct ZoneMonitorStartMessage *zis_msg)
1548 struct NamestoreClient *nc = cls;
1549 struct ZoneMonitor *zm;
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "Received ZONE_MONITOR_START message\n");
1553 zm = GNUNET_new (struct ZoneMonitor);
1555 zm->zone = zis_msg->zone;
1556 GNUNET_CONTAINER_DLL_insert (monitor_head,
1559 GNUNET_SERVICE_client_mark_monitor (nc->client);
1560 GNUNET_SERVICE_client_disable_continue_warning (nc->client);
1561 GNUNET_notification_context_add (monitor_nc,
1563 if (GNUNET_YES == ntohl (zis_msg->iterate_first))
1564 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next,
1572 * Obtain the next datum during the zone monitor's zone intiial iteration.
1574 * @param cls zone monitor that does its initial iteration
1577 monitor_next (void *cls)
1579 struct ZoneMonitor *zm = cls;
1583 ret = GSN_database->iterate_records (GSN_database->cls,
1584 (0 == memcmp (&zm->zone,
1590 &monitor_iterate_cb,
1592 if (GNUNET_SYSERR == ret)
1594 GNUNET_SERVICE_client_drop (zm->nc->client);
1597 if (GNUNET_NO == ret)
1607 * Process namestore requests.
1609 * @param cls closure
1610 * @param cfg configuration to use
1611 * @param service the initialized service
1615 const struct GNUNET_CONFIGURATION_Handle *cfg,
1616 struct GNUNET_SERVICE_Handle *service)
1620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1621 "Starting namestore service\n");
1623 monitor_nc = GNUNET_notification_context_create (1);
1624 namecache = GNUNET_NAMECACHE_connect (cfg);
1625 /* Loading database plugin */
1627 GNUNET_CONFIGURATION_get_value_string (cfg,
1631 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1632 "No database backend configured\n");
1634 GNUNET_asprintf (&db_lib_name,
1635 "libgnunet_plugin_namestore_%s",
1637 GSN_database = GNUNET_PLUGIN_load (db_lib_name,
1639 GNUNET_free (database);
1640 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
1642 if (NULL == GSN_database)
1644 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1645 "Could not load database backend `%s'\n",
1647 GNUNET_SCHEDULER_shutdown ();
1654 * Define "main" method using service macro.
1658 GNUNET_SERVICE_OPTION_NONE,
1661 &client_disconnect_cb,
1663 GNUNET_MQ_hd_var_size (record_store,
1664 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE,
1665 struct RecordStoreMessage,
1667 GNUNET_MQ_hd_var_size (record_lookup,
1668 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP,
1669 struct LabelLookupMessage,
1671 GNUNET_MQ_hd_fixed_size (zone_to_name,
1672 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME,
1673 struct ZoneToNameMessage,
1675 GNUNET_MQ_hd_fixed_size (iteration_start,
1676 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START,
1677 struct ZoneIterationStartMessage,
1679 GNUNET_MQ_hd_fixed_size (iteration_next,
1680 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT,
1681 struct ZoneIterationNextMessage,
1683 GNUNET_MQ_hd_fixed_size (iteration_stop,
1684 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP,
1685 struct ZoneIterationStopMessage,
1687 GNUNET_MQ_hd_fixed_size (monitor_start,
1688 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START,
1689 struct ZoneMonitorStartMessage,
1691 GNUNET_MQ_handler_end ());
1694 /* end of gnunet-service-namestore.c */