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 *client;
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
99 * Next element in the DLL
101 struct NamestoreClient *next;
104 * Previous element in the DLL
106 struct NamestoreClient *prev;
111 struct GNUNET_SERVER_Client *client;
115 * Zone iteration operations in progress initiated by this client
117 struct ZoneIteration *op_head;
121 * Zone iteration operations in progress initiated by this client
123 struct ZoneIteration *op_tail;
128 * A namestore monitor.
133 * Next element in the DLL
135 struct ZoneMonitor *next;
138 * Previous element in the DLL
140 struct ZoneMonitor *prev;
143 * Namestore client which intiated this zone monitor
145 struct NamestoreClient *nc;
148 * Private key of the zone.
150 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
153 * Task active during initial iteration.
155 struct GNUNET_SCHEDULER_Task * task;
158 * Offset of the zone iteration used to address next result of the zone
159 * iteration in the store
162 * Incremented with by every call to #handle_iteration_next
170 * Pending operation on the namecache.
172 struct CacheOperation
178 struct CacheOperation *prev;
183 struct CacheOperation *next;
186 * Handle to namecache queue.
188 struct GNUNET_NAMECACHE_QueueEntry *qe;
191 * Client to notify about the result.
193 struct GNUNET_SERVER_Client *client;
196 * Client's request ID.
203 * Public key of all zeros.
205 static const struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
208 * Configuration handle.
210 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
215 static struct GNUNET_NAMECACHE_Handle *namecache;
220 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
223 * Name of the database plugin
225 static char *db_lib_name;
228 * Our notification context.
230 static struct GNUNET_SERVER_NotificationContext *snc;
233 * Head of the Client DLL
235 static struct NamestoreClient *client_head;
238 * Tail of the Client DLL
240 static struct NamestoreClient *client_tail;
245 static struct CacheOperation *cop_head;
250 static struct CacheOperation *cop_tail;
253 * First active zone monitor.
255 static struct ZoneMonitor *monitor_head;
258 * Last active zone monitor.
260 static struct ZoneMonitor *monitor_tail;
263 * Notification context shared by all monitors.
265 static struct GNUNET_SERVER_NotificationContext *monitor_nc;
270 * Task run during shutdown.
275 cleanup_task (void *cls)
277 struct ZoneIteration *no;
278 struct NamestoreClient *nc;
279 struct CacheOperation *cop;
281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
282 "Stopping namestore service\n");
285 GNUNET_SERVER_notification_context_destroy (snc);
288 while (NULL != (cop = cop_head))
290 GNUNET_NAMECACHE_cancel (cop->qe);
291 GNUNET_CONTAINER_DLL_remove (cop_head,
296 GNUNET_NAMECACHE_disconnect (namecache);
298 while (NULL != (nc = client_head))
300 while (NULL != (no = nc->op_head))
302 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
305 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
306 GNUNET_SERVER_client_set_user_context (nc->client, NULL);
309 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
310 GNUNET_free (db_lib_name);
312 if (NULL != monitor_nc)
314 GNUNET_SERVER_notification_context_destroy (monitor_nc);
321 * Called whenever a client is disconnected.
322 * Frees our resources associated with that client.
325 * @param client identification of the client
328 client_disconnect_notification (void *cls,
329 struct GNUNET_SERVER_Client *client)
331 struct ZoneIteration *no;
332 struct NamestoreClient *nc;
333 struct ZoneMonitor *zm;
334 struct CacheOperation *cop;
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "Client %p disconnected\n",
341 if (NULL == (nc = GNUNET_SERVER_client_get_user_context (client, struct NamestoreClient)))
343 while (NULL != (no = nc->op_head))
345 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
348 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
350 for (zm = monitor_head; NULL != zm; zm = zm->next)
352 if (client == zm->nc->client)
354 GNUNET_CONTAINER_DLL_remove (monitor_head,
357 if (NULL != zm->task)
359 GNUNET_SCHEDULER_cancel (zm->task);
366 for (cop = cop_head; NULL != cop; cop = cop->next)
367 if (client == cop->client)
373 * Add a client to our list of active clients, if it is not yet
376 * @param client client to add
377 * @return internal namestore client structure for this client
379 static struct NamestoreClient *
380 client_lookup (struct GNUNET_SERVER_Client *client)
382 struct NamestoreClient *nc;
384 nc = GNUNET_SERVER_client_get_user_context (client, struct NamestoreClient);
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Client %p connected\n",
390 nc = GNUNET_new (struct NamestoreClient);
392 GNUNET_SERVER_notification_context_add (snc, client);
393 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, nc);
394 GNUNET_SERVER_client_set_user_context (client, nc);
400 * Function called with the records for the #GNUNET_GNS_MASTERZONE_STR
401 * label in the zone. Used to locate the #GNUNET_GNSRECORD_TYPE_NICK
402 * record, which (if found) is then copied to @a cls for future use.
404 * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
405 * @param private_key the private key of the zone (unused)
406 * @param label should be #GNUNET_GNS_MASTERZONE_STR
407 * @param rd_count number of records in @a rd
408 * @param rd records stored under @a label in the zone
411 lookup_nick_it (void *cls,
412 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
414 unsigned int rd_count,
415 const struct GNUNET_GNSRECORD_Data *rd)
417 struct GNUNET_GNSRECORD_Data **res = cls;
420 if (0 != strcmp (label, GNUNET_GNS_MASTERZONE_STR))
425 for (c = 0; c < rd_count; c++)
427 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
429 (*res) = GNUNET_malloc (rd[c].data_size + sizeof (struct GNUNET_GNSRECORD_Data));
430 (*res)->data = &(*res)[1];
431 GNUNET_memcpy ((void *) (*res)->data,
434 (*res)->data_size = rd[c].data_size;
435 (*res)->expiration_time = rd[c].expiration_time;
436 (*res)->flags = rd[c].flags;
437 (*res)->record_type = GNUNET_GNSRECORD_TYPE_NICK;
446 * Return the NICK record for the zone (if it exists).
448 * @param zone private key for the zone to look for nick
449 * @return NULL if no NICK record was found
451 static struct GNUNET_GNSRECORD_Data *
452 get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
454 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
455 struct GNUNET_GNSRECORD_Data *nick;
458 res = GSN_database->lookup_records (GSN_database->cls, zone,
459 GNUNET_GNS_MASTERZONE_STR,
460 &lookup_nick_it, &nick);
461 if ( (GNUNET_OK != res) ||
464 GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
465 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
466 "No nick name set for zone `%s'\n",
467 GNUNET_GNSRECORD_z2s (&pub));
475 merge_with_nick_records ( const struct GNUNET_GNSRECORD_Data *nick_rd,
477 const struct GNUNET_GNSRECORD_Data *rd2,
478 unsigned int *rdc_res,
479 struct GNUNET_GNSRECORD_Data **rd_res)
481 uint64_t latest_expiration;
487 (*rdc_res) = 1 + rdc2;
497 req += sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd[c].data_size;
498 for (c=0; c< rdc2; c++)
499 req += sizeof (struct GNUNET_GNSRECORD_Data) + rd2[c].data_size;
500 (*rd_res) = GNUNET_malloc (req);
501 data = (char *) &(*rd_res)[1 + rdc2];
503 latest_expiration = 0;
505 for (c=0; c< rdc2; c++)
507 if (0 != (rd2[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
509 if ((GNUNET_TIME_absolute_get().abs_value_us + rd2[c].expiration_time) >
511 latest_expiration = rd2[c].expiration_time;
513 else if (rd2[c].expiration_time > latest_expiration)
514 latest_expiration = rd2[c].expiration_time;
515 (*rd_res)[c] = rd2[c];
516 (*rd_res)[c].data = (void *) &data[data_offset];
518 GNUNET_memcpy ((void *) (*rd_res)[c].data,
521 data_offset += (*rd_res)[c].data_size;
523 record_offset = rdc2;
526 (*rd_res)[c+record_offset] = nick_rd[c];
527 (*rd_res)[c+record_offset].expiration_time = latest_expiration;
528 (*rd_res)[c+record_offset].data = (void *) &data[data_offset];
530 GNUNET_memcpy ((void *) (*rd_res)[c+record_offset].data,
532 nick_rd[c].data_size);
533 data_offset += (*rd_res)[c+record_offset].data_size;
535 GNUNET_assert (req == (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
540 * Generate a `struct LookupNameResponseMessage` and send it to the
541 * given client using the given notification context.
543 * @param nc notification context to use
544 * @param client client to unicast to
545 * @param request_id request ID to use
546 * @param zone_key zone key of the zone
548 * @param rd_count number of records in @a rd
549 * @param rd array of records
552 send_lookup_response (struct GNUNET_SERVER_NotificationContext *nc,
553 struct GNUNET_SERVER_Client *client,
555 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
557 unsigned int rd_count,
558 const struct GNUNET_GNSRECORD_Data *rd)
560 struct RecordResultMessage *zir_msg;
561 struct GNUNET_GNSRECORD_Data *nick;
562 struct GNUNET_GNSRECORD_Data *res;
563 unsigned int res_count;
570 nick = get_nick_record (zone_key);
571 if ((NULL != nick) && (0 != strcmp(name, GNUNET_GNS_MASTERZONE_STR)))
573 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
574 merge_with_nick_records (nick, rd_count, rd, &res_count, &res);
575 //fprintf (stderr, "Merging %u records for `%s' with NICK records\n",rd_count, name);
580 res_count = rd_count;
581 res = (struct GNUNET_GNSRECORD_Data *) rd;
582 //fprintf (stderr, "Not merging %u records for `%s' with NICK records\n",rd_count, name);
585 name_len = strlen (name) + 1;
586 rd_ser_len = GNUNET_GNSRECORD_records_get_size (res_count, res);
587 msg_size = sizeof (struct RecordResultMessage) + name_len + rd_ser_len;
588 (void) client_lookup (client);
589 zir_msg = GNUNET_malloc (msg_size);
590 zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
591 zir_msg->gns_header.header.size = htons (msg_size);
592 zir_msg->gns_header.r_id = htonl (request_id);
593 zir_msg->name_len = htons (name_len);
594 zir_msg->rd_count = htons (res_count);
595 zir_msg->rd_len = htons (rd_ser_len);
596 zir_msg->private_key = *zone_key;
597 name_tmp = (char *) &zir_msg[1];
598 GNUNET_memcpy (name_tmp, name, name_len);
599 rd_ser = &name_tmp[name_len];
600 GNUNET_GNSRECORD_records_serialize (res_count, res, rd_ser_len, rd_ser);
601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
602 "Sending `%s' message with %u records and size %zu\n",
606 GNUNET_SERVER_notification_context_unicast (nc,
608 &zir_msg->gns_header.header,
612 GNUNET_free (zir_msg);
617 * Send response to the store request to the client.
619 * @param client client to talk to
620 * @param res status of the operation
621 * @param rid client's request ID
624 send_store_response (struct GNUNET_SERVER_Client *client,
628 struct RecordStoreResponseMessage rcr_msg;
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
631 "Sending `%s' message\n",
632 "RECORD_STORE_RESPONSE");
633 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
634 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordStoreResponseMessage));
635 rcr_msg.gns_header.r_id = htonl (rid);
636 rcr_msg.op_result = htonl (res);
637 GNUNET_SERVER_notification_context_unicast (snc, client,
638 &rcr_msg.gns_header.header,
644 * Cache operation complete, clean up.
646 * @param cls the `struct CacheOperation`
647 * @param success success
648 * @param emsg error messages
651 finish_cache_operation (void *cls,
655 struct CacheOperation *cop = cls;
658 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
659 _("Failed to replicate block in namecache: %s\n"),
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663 "CACHE operation completed\n");
664 GNUNET_CONTAINER_DLL_remove (cop_head,
667 if (NULL != cop->client)
668 send_store_response (cop->client,
676 * We just touched the plaintext information about a name in our zone;
677 * refresh the corresponding (encrypted) block in the namecache.
679 * @param client client responsible for the request
680 * @param rid request ID of the client
681 * @param zone_key private key of the zone
682 * @param name label for the records
683 * @param rd_count number of records
684 * @param rd records stored under the given @a name
687 refresh_block (struct GNUNET_SERVER_Client *client,
689 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
691 unsigned int rd_count,
692 const struct GNUNET_GNSRECORD_Data *rd)
694 struct GNUNET_GNSRECORD_Block *block;
695 struct CacheOperation *cop;
696 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
697 struct GNUNET_GNSRECORD_Data *nick;
698 struct GNUNET_GNSRECORD_Data *res;
699 unsigned int res_count;
701 nick = get_nick_record (zone_key);
702 res_count = rd_count;
703 res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
706 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
707 merge_with_nick_records (nick, rd_count,rd, &res_count, &res);
712 block = GNUNET_GNSRECORD_block_create (zone_key,
713 GNUNET_TIME_UNIT_ZERO_ABS,
717 block = GNUNET_GNSRECORD_block_create (zone_key,
718 GNUNET_GNSRECORD_record_get_expiration_time (res_count,
722 GNUNET_assert (NULL != block);
723 GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "Caching block for label `%s' with %u records in zone `%s' in namecache\n",
729 GNUNET_GNSRECORD_z2s (&pkey));
730 cop = GNUNET_new (struct CacheOperation);
731 cop->client = client;
733 GNUNET_CONTAINER_DLL_insert (cop_head,
736 cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
738 &finish_cache_operation,
745 * Closure for #lookup_it().
747 struct RecordLookupContext
753 unsigned int res_rd_count;
759 struct GNUNET_GNSRECORD_Data *nick;
764 lookup_it (void *cls,
765 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
767 unsigned int rd_count,
768 const struct GNUNET_GNSRECORD_Data *rd)
770 struct RecordLookupContext *rlc = cls;
771 struct GNUNET_GNSRECORD_Data *rd_res;
772 unsigned int rdc_res;
774 if (0 == strcmp (label, rlc->label))
776 rlc->found = GNUNET_YES;
779 if ((NULL != rlc->nick) && (0 != strcmp(label, GNUNET_GNS_MASTERZONE_STR)))
784 rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
785 merge_with_nick_records (rlc->nick,
789 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res, rd_res);
790 rlc->res_rd_count = rdc_res;
791 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
792 GNUNET_GNSRECORD_records_serialize (rdc_res, rd_res, rlc->rd_ser_len , rlc->res_rd);
794 GNUNET_free (rd_res);
795 GNUNET_free (rlc->nick);
800 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
801 rlc->res_rd_count = rd_count;
802 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
803 GNUNET_GNSRECORD_records_serialize (rd_count, rd, rlc->rd_ser_len , rlc->res_rd);
809 rlc->res_rd_count = 0;
817 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
820 * @param client client sending the message
821 * @param message message of type 'struct RecordCreateMessage'
824 handle_record_lookup (void *cls,
825 struct GNUNET_SERVER_Client *client,
826 const struct GNUNET_MessageHeader *message)
828 const struct LabelLookupMessage *ll_msg;
829 struct LabelLookupResponseMessage *llr_msg;
830 struct RecordLookupContext rlc;
831 const char *name_tmp;
838 if (ntohs (message->size) < sizeof (struct LabelLookupMessage))
841 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
845 ll_msg = (const struct LabelLookupMessage *) message;
846 name_len = ntohl (ll_msg->label_len);
847 src_size = ntohs (message->size);
849 if (name_len != src_size - sizeof (struct LabelLookupMessage))
852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
856 name_tmp = (const char *) &ll_msg[1];
857 if ('\0' != name_tmp[name_len -1])
860 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
864 GNUNET_SERVER_receive_done (client, GNUNET_OK);
865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
866 "Received `%s' message for name `%s'\n",
867 "NAMESTORE_RECORD_LOOKUP", name_tmp);
869 if (NULL == (client_lookup (client)))
872 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
876 rlc.label = name_tmp;
877 rlc.found = GNUNET_NO;
878 rlc.res_rd_count = 0;
881 rlc.nick = get_nick_record (&ll_msg->zone);
883 res = GSN_database->lookup_records (GSN_database->cls,
884 &ll_msg->zone, name_tmp, &lookup_it, &rlc);
886 res_size = sizeof (struct LabelLookupResponseMessage) + name_len + rlc.rd_ser_len;
887 llr_msg = GNUNET_malloc (res_size);
888 llr_msg->gns_header.header.size = htons (res_size);
889 llr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
890 llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
891 llr_msg->private_key = ll_msg->zone;
892 llr_msg->name_len = htons (name_len);
893 llr_msg->rd_count = htons (rlc.res_rd_count);
894 llr_msg->rd_len = htons (rlc.rd_ser_len);
895 res_name = (char *) &llr_msg[1];
896 if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
897 llr_msg->found = ntohs (GNUNET_YES);
899 llr_msg->found = ntohs (GNUNET_NO);
900 GNUNET_memcpy (&llr_msg[1], name_tmp, name_len);
901 GNUNET_memcpy (&res_name[name_len], rlc.res_rd, rlc.rd_ser_len);
903 GNUNET_SERVER_notification_context_unicast (snc, client, &llr_msg->gns_header.header,
906 GNUNET_free_non_null (rlc.res_rd);
907 GNUNET_free (llr_msg);
912 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
915 * @param client client sending the message
916 * @param message message of type 'struct RecordCreateMessage'
919 handle_record_store (void *cls,
920 struct GNUNET_SERVER_Client *client,
921 const struct GNUNET_MessageHeader *message)
923 const struct RecordStoreMessage *rp_msg;
929 const char *name_tmp;
932 unsigned int rd_count;
934 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
935 struct ZoneMonitor *zm;
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 "Received `%s' message\n",
939 "NAMESTORE_RECORD_STORE");
940 if (ntohs (message->size) < sizeof (struct RecordStoreMessage))
943 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
946 rp_msg = (const struct RecordStoreMessage *) message;
947 rid = ntohl (rp_msg->gns_header.r_id);
948 name_len = ntohs (rp_msg->name_len);
949 msg_size = ntohs (message->size);
950 rd_count = ntohs (rp_msg->rd_count);
951 rd_ser_len = ntohs (rp_msg->rd_len);
952 GNUNET_break (0 == ntohs (rp_msg->reserved));
953 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
954 if (msg_size != msg_size_exp)
957 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
960 if ((0 == name_len) || (name_len > MAX_NAME_LEN))
963 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
966 name_tmp = (const char *) &rp_msg[1];
967 rd_ser = &name_tmp[name_len];
968 if ('\0' != name_tmp[name_len -1])
971 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
974 (void) client_lookup (client);
976 struct GNUNET_GNSRECORD_Data rd[rd_count];
979 GNUNET_GNSRECORD_records_deserialize (rd_ser_len, rd_ser, rd_count, rd))
982 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
986 /* Extracting and converting private key */
987 GNUNET_CRYPTO_ecdsa_key_get_public (&rp_msg->private_key,
989 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
990 if (NULL == conv_name)
992 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
993 "Error converting name `%s'\n", name_tmp);
994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
998 "Creating %u records for name `%s' in zone `%s'\n",
999 (unsigned int) rd_count,
1001 GNUNET_GNSRECORD_z2s (&pubkey));
1003 if ( (0 == rd_count) &&
1005 GSN_database->iterate_records (GSN_database->cls,
1006 &rp_msg->private_key, 0, NULL, 0)) )
1008 /* This name does not exist, so cannot be removed */
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "Name `%s' does not exist, no deletion required\n",
1016 struct GNUNET_GNSRECORD_Data rd_clean[rd_count];
1018 unsigned int rd_clean_off;
1020 /* remove "NICK" records, unless this is for the "+" label */
1022 for (i=0;i<rd_count;i++)
1024 rd_clean[rd_clean_off] = rd[i];
1025 if ( (0 == strcmp (GNUNET_GNS_MASTERZONE_STR,
1027 (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
1030 res = GSN_database->store_records (GSN_database->cls,
1031 &rp_msg->private_key,
1033 rd_clean_off, rd_clean);
1034 if (GNUNET_OK == res)
1036 for (zm = monitor_head; NULL != zm; zm = zm->next)
1038 if ( (0 == memcmp (&rp_msg->private_key, &zm->zone,
1039 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) ||
1040 (0 == memcmp (&zm->zone,
1042 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1045 "Notifying monitor about changes under label `%s'\n",
1047 send_lookup_response (monitor_nc,
1050 &rp_msg->private_key,
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1056 "Monitor is for another zone\n");
1058 if (NULL == monitor_head)
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 "No monitors active\n");
1064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1065 "Error storing record: %d\n",
1069 if (GNUNET_OK == res)
1071 refresh_block (client, rid,
1072 &rp_msg->private_key,
1075 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1076 GNUNET_free (conv_name);
1079 GNUNET_free (conv_name);
1081 send_store_response (client, res, rid);
1082 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1087 * Context for record remove operations passed from #handle_zone_to_name to
1088 * #handle_zone_to_name_it as closure
1090 struct ZoneToNameCtx
1095 struct NamestoreClient *nc;
1098 * Request id (to be used in the response to the client).
1103 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
1104 * not finding a name for the zone still counts as a 'success' here,
1105 * as this field is about the success of executing the IPC protocol.
1112 * Zone to name iterator
1114 * @param cls struct ZoneToNameCtx *
1115 * @param zone_key the zone key
1117 * @param rd_count number of records in @a rd
1118 * @param rd record data
1121 handle_zone_to_name_it (void *cls,
1122 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1124 unsigned int rd_count,
1125 const struct GNUNET_GNSRECORD_Data *rd)
1127 struct ZoneToNameCtx *ztn_ctx = cls;
1128 struct ZoneToNameResponseMessage *ztnr_msg;
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1137 "Found result for zone-to-name lookup: `%s'\n",
1140 name_len = (NULL == name) ? 0 : strlen (name) + 1;
1141 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
1142 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
1143 if (msg_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1146 ztn_ctx->success = GNUNET_SYSERR;
1149 ztnr_msg = GNUNET_malloc (msg_size);
1150 ztnr_msg->gns_header.header.type = htons (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];
1160 GNUNET_memcpy (name_tmp, name, name_len);
1161 rd_tmp = &name_tmp[name_len];
1162 GNUNET_GNSRECORD_records_serialize (rd_count, rd, rd_ser_len, rd_tmp);
1163 ztn_ctx->success = GNUNET_OK;
1164 GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client,
1165 &ztnr_msg->gns_header.header,
1167 GNUNET_free (ztnr_msg);
1172 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
1175 * @param client client sending the message
1176 * @param message message of type 'struct ZoneToNameMessage'
1179 handle_zone_to_name (void *cls,
1180 struct GNUNET_SERVER_Client *client,
1181 const struct GNUNET_MessageHeader *message)
1183 struct NamestoreClient *nc;
1184 const struct ZoneToNameMessage *ztn_msg;
1185 struct ZoneToNameCtx ztn_ctx;
1186 struct ZoneToNameResponseMessage ztnr_msg;
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "Received `%s' message\n",
1191 ztn_msg = (const struct ZoneToNameMessage *) message;
1192 nc = client_lookup (client);
1193 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
1195 ztn_ctx.success = GNUNET_NO;
1196 if (GNUNET_SYSERR ==
1197 GSN_database->zone_to_name (GSN_database->cls,
1199 &ztn_msg->value_zone,
1200 &handle_zone_to_name_it, &ztn_ctx))
1202 /* internal error, hang up instead of signalling something
1203 that might be wrong */
1205 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1208 if (GNUNET_NO == ztn_ctx.success)
1210 /* no result found, send empty response */
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1212 "Found no result for zone-to-name lookup.\n");
1213 memset (&ztnr_msg, 0, sizeof (ztnr_msg));
1214 ztnr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1215 ztnr_msg.gns_header.header.size = htons (sizeof (ztnr_msg));
1216 ztnr_msg.gns_header.r_id = ztn_msg->gns_header.r_id;
1217 ztnr_msg.res = htons (GNUNET_NO);
1218 GNUNET_SERVER_notification_context_unicast (snc,
1220 &ztnr_msg.gns_header.header,
1223 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1228 * Zone iteration processor result
1230 enum ZoneIterationResult
1239 * Continue to iterate with next iteration_next call
1241 IT_SUCCESS_MORE_AVAILABLE = 1,
1244 * Iteration complete
1246 IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE = 2
1251 * Context for record remove operations passed from
1252 * #run_zone_iteration_round to #zone_iterate_proc as closure
1254 struct ZoneIterationProcResult
1257 * The zone iteration handle
1259 struct ZoneIteration *zi;
1262 * Iteration result: iteration done?
1263 * #IT_SUCCESS_MORE_AVAILABLE: if there may be more results overall but
1264 * we got one for now and have sent it to the client
1265 * #IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE: if there are no further results,
1266 * #IT_START: if we are still trying to find a result.
1268 int res_iteration_finished;
1274 * Process results for zone iteration from database
1276 * @param cls struct ZoneIterationProcResult *proc
1277 * @param zone_key the zone key
1279 * @param rd_count number of records for this name
1280 * @param rd record data
1283 zone_iterate_proc (void *cls,
1284 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1286 unsigned int rd_count,
1287 const struct GNUNET_GNSRECORD_Data *rd)
1289 struct ZoneIterationProcResult *proc = cls;
1291 int do_refresh_block;
1293 if ((NULL == zone_key) && (NULL == name))
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1296 "Iteration done\n");
1297 proc->res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1300 if ((NULL == zone_key) || (NULL == name))
1302 /* what is this!? should never happen */
1303 proc->res_iteration_finished = IT_START;
1307 proc->res_iteration_finished = IT_SUCCESS_MORE_AVAILABLE;
1308 send_lookup_response (snc,
1309 proc->zi->client->client,
1310 proc->zi->request_id,
1315 do_refresh_block = GNUNET_NO;
1316 for (i=0;i<rd_count;i++)
1317 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
1319 do_refresh_block = GNUNET_YES;
1322 if (GNUNET_YES == do_refresh_block)
1323 refresh_block (NULL, 0,
1333 * Perform the next round of the zone iteration.
1335 * @param zi zone iterator to process
1338 run_zone_iteration_round (struct ZoneIteration *zi)
1340 struct ZoneIterationProcResult proc;
1341 struct RecordResultMessage rrm;
1344 memset (&proc, 0, sizeof (proc));
1346 proc.res_iteration_finished = IT_START;
1347 while (IT_START == proc.res_iteration_finished)
1349 if (GNUNET_SYSERR ==
1350 (ret = GSN_database->iterate_records (GSN_database->cls,
1351 (0 == memcmp (&zi->zone, &zero, sizeof (zero)))
1355 &zone_iterate_proc, &proc)))
1360 if (GNUNET_NO == ret)
1361 proc.res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1364 if (IT_SUCCESS_MORE_AVAILABLE == proc.res_iteration_finished)
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367 "More results available\n");
1368 return; /* more results later */
1370 /* send empty response to indicate end of list */
1371 memset (&rrm, 0, sizeof (rrm));
1372 rrm.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
1373 rrm.gns_header.header.size = htons (sizeof (rrm));
1374 rrm.gns_header.r_id = htonl (zi->request_id);
1375 GNUNET_SERVER_notification_context_unicast (snc,
1377 &rrm.gns_header.header,
1379 GNUNET_CONTAINER_DLL_remove (zi->client->op_head,
1380 zi->client->op_tail,
1387 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1390 * @param client the client sending the message
1391 * @param message message of type 'struct ZoneIterationStartMessage'
1394 handle_iteration_start (void *cls,
1395 struct GNUNET_SERVER_Client *client,
1396 const struct GNUNET_MessageHeader *message)
1398 const struct ZoneIterationStartMessage *zis_msg;
1399 struct NamestoreClient *nc;
1400 struct ZoneIteration *zi;
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1403 if (NULL == (nc = client_lookup (client)))
1406 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1409 zis_msg = (const struct ZoneIterationStartMessage *) message;
1410 zi = GNUNET_new (struct ZoneIteration);
1411 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1414 zi->zone = zis_msg->zone;
1417 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1418 run_zone_iteration_round (zi);
1419 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1424 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1427 * @param client GNUNET_SERVER_Client sending the message
1428 * @param message message of type 'struct ZoneIterationStopMessage'
1431 handle_iteration_stop (void *cls,
1432 struct GNUNET_SERVER_Client *client,
1433 const struct GNUNET_MessageHeader *message)
1435 struct NamestoreClient *nc;
1436 struct ZoneIteration *zi;
1437 const struct ZoneIterationStopMessage *zis_msg;
1440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1441 "Received `%s' message\n",
1442 "ZONE_ITERATION_STOP");
1443 if (NULL == (nc = client_lookup(client)))
1446 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1449 zis_msg = (const struct ZoneIterationStopMessage *) message;
1450 rid = ntohl (zis_msg->gns_header.r_id);
1451 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1452 if (zi->request_id == rid)
1457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1460 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1462 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1467 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1470 * @param client GNUNET_SERVER_Client sending the message
1471 * @param message message of type 'struct ZoneIterationNextMessage'
1474 handle_iteration_next (void *cls,
1475 struct GNUNET_SERVER_Client *client,
1476 const struct GNUNET_MessageHeader *message)
1478 struct NamestoreClient *nc;
1479 struct ZoneIteration *zi;
1480 const struct ZoneIterationNextMessage *zis_msg;
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1484 "Received `%s' message\n",
1485 "ZONE_ITERATION_NEXT");
1486 if (NULL == (nc = client_lookup(client)))
1489 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1492 zis_msg = (const struct ZoneIterationNextMessage *) message;
1493 rid = ntohl (zis_msg->gns_header.r_id);
1494 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1495 if (zi->request_id == rid)
1500 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1503 run_zone_iteration_round (zi);
1504 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1509 * Send 'sync' message to zone monitor, we're now in sync.
1511 * @param zm monitor that is now in sync
1514 monitor_sync (struct ZoneMonitor *zm)
1516 struct GNUNET_MessageHeader sync;
1518 sync.size = htons (sizeof (struct GNUNET_MessageHeader));
1519 sync.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1520 GNUNET_SERVER_notification_context_unicast (monitor_nc,
1528 * Obtain the next datum during the zone monitor's zone intiial iteration.
1530 * @param cls zone monitor that does its initial iteration
1533 monitor_next (void *cls);
1537 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1539 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1540 * @param zone_key zone key of the zone
1542 * @param rd_count number of records in @a rd
1543 * @param rd array of records
1546 monitor_iterate_cb (void *cls,
1547 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1549 unsigned int rd_count,
1550 const struct GNUNET_GNSRECORD_Data *rd)
1552 struct ZoneMonitor *zm = cls;
1556 /* finished with iteration */
1560 send_lookup_response (monitor_nc,
1567 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1572 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
1575 * @param client the client sending the message
1576 * @param message message of type 'struct ZoneMonitorStartMessage'
1579 handle_monitor_start (void *cls,
1580 struct GNUNET_SERVER_Client *client,
1581 const struct GNUNET_MessageHeader *message)
1583 const struct ZoneMonitorStartMessage *zis_msg;
1584 struct ZoneMonitor *zm;
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1587 "Received `%s' message\n",
1588 "ZONE_MONITOR_START");
1589 zis_msg = (const struct ZoneMonitorStartMessage *) message;
1590 zm = GNUNET_new (struct ZoneMonitor);
1592 zm->nc = client_lookup (client);
1593 zm->zone = zis_msg->zone;
1594 GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, zm);
1595 GNUNET_SERVER_client_mark_monitor (client);
1596 GNUNET_SERVER_disable_receive_done_warning (client);
1597 GNUNET_SERVER_notification_context_add (monitor_nc,
1599 if (GNUNET_YES == ntohl (zis_msg->iterate_first))
1600 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1607 * Obtain the next datum during the zone monitor's zone intiial iteration.
1609 * @param cls zone monitor that does its initial iteration
1612 monitor_next (void *cls)
1614 struct ZoneMonitor *zm = cls;
1618 ret = GSN_database->iterate_records (GSN_database->cls,
1619 (0 == memcmp (&zm->zone, &zero, sizeof (zero)))
1623 &monitor_iterate_cb, zm);
1624 if (GNUNET_SYSERR == ret)
1626 GNUNET_SERVER_client_disconnect (zm->nc->client);
1629 if (GNUNET_NO == ret)
1639 * Process namestore requests.
1641 * @param cls closure
1642 * @param server the initialized server
1643 * @param cfg configuration to use
1646 run (void *cls, struct GNUNET_SERVER_Handle *server,
1647 const struct GNUNET_CONFIGURATION_Handle *cfg)
1649 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1650 {&handle_record_store, NULL,
1651 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE, 0},
1652 {&handle_record_lookup, NULL,
1653 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP, 0},
1654 {&handle_zone_to_name, NULL,
1655 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, sizeof (struct ZoneToNameMessage) },
1656 {&handle_iteration_start, NULL,
1657 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage) },
1658 {&handle_iteration_next, NULL,
1659 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, sizeof (struct ZoneIterationNextMessage) },
1660 {&handle_iteration_stop, NULL,
1661 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage) },
1662 {&handle_monitor_start, NULL,
1663 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START, sizeof (struct ZoneMonitorStartMessage) },
1668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1670 monitor_nc = GNUNET_SERVER_notification_context_create (server, 1);
1671 namecache = GNUNET_NAMECACHE_connect (cfg);
1672 /* Loading database plugin */
1674 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1676 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1678 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1679 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1680 GNUNET_free (database);
1681 if (NULL == GSN_database)
1683 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1684 "Could not load database backend `%s'\n",
1686 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1690 /* Configuring server handles */
1691 GNUNET_SERVER_add_handlers (server, handlers);
1692 snc = GNUNET_SERVER_notification_context_create (server, 16);
1693 GNUNET_SERVER_disconnect_notify (server,
1694 &client_disconnect_notification,
1696 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
1702 * The main function for the template service.
1704 * @param argc number of arguments from the command line
1705 * @param argv command line arguments
1706 * @return 0 ok, 1 on error
1709 main (int argc, char *const *argv)
1711 return (GNUNET_OK ==
1712 GNUNET_SERVICE_run (argc, argv, "namestore",
1713 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1716 /* end of gnunet-service-namestore.c */