2 This file is part of GNUnet.
3 (C) 2012, 2013, 2014 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier 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.
276 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
278 struct ZoneIteration *no;
279 struct NamestoreClient *nc;
280 struct CacheOperation *cop;
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283 "Stopping namestore service\n");
286 GNUNET_SERVER_notification_context_destroy (snc);
289 while (NULL != (cop = cop_head))
291 GNUNET_NAMECACHE_cancel (cop->qe);
292 GNUNET_CONTAINER_DLL_remove (cop_head,
297 GNUNET_NAMECACHE_disconnect (namecache);
299 while (NULL != (nc = client_head))
301 while (NULL != (no = nc->op_head))
303 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
306 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
307 GNUNET_SERVER_client_set_user_context (nc->client, (void *)NULL);
310 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
311 GNUNET_free (db_lib_name);
313 if (NULL != monitor_nc)
315 GNUNET_SERVER_notification_context_destroy (monitor_nc);
322 * Called whenever a client is disconnected.
323 * Frees our resources associated with that client.
326 * @param client identification of the client
329 client_disconnect_notification (void *cls,
330 struct GNUNET_SERVER_Client *client)
332 struct ZoneIteration *no;
333 struct NamestoreClient *nc;
334 struct ZoneMonitor *zm;
335 struct CacheOperation *cop;
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "Client %p disconnected\n",
342 if (NULL == (nc = GNUNET_SERVER_client_get_user_context (client, struct NamestoreClient)))
344 while (NULL != (no = nc->op_head))
346 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
349 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
351 for (zm = monitor_head; NULL != zm; zm = zm->next)
353 if (client == zm->nc->client)
355 GNUNET_CONTAINER_DLL_remove (monitor_head,
358 if (GNUNET_SCHEDULER_NO_TASK != zm->task)
360 GNUNET_SCHEDULER_cancel (zm->task);
361 zm->task = GNUNET_SCHEDULER_NO_TASK;
367 for (cop = cop_head; NULL != cop; cop = cop->next)
368 if (client == cop->client)
374 * Add a client to our list of active clients, if it is not yet
377 * @param client client to add
378 * @return internal namestore client structure for this client
380 static struct NamestoreClient *
381 client_lookup (struct GNUNET_SERVER_Client *client)
383 struct NamestoreClient *nc;
385 nc = GNUNET_SERVER_client_get_user_context (client, struct NamestoreClient);
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Client %p connected\n",
391 nc = GNUNET_new (struct NamestoreClient);
393 GNUNET_SERVER_notification_context_add (snc, client);
394 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, nc);
395 GNUNET_SERVER_client_set_user_context (client, nc);
401 * Function called with the records for the #GNUNET_GNS_MASTERZONE_STR
402 * label in the zone. Used to locate the #GNUNET_GNSRECORD_TYPE_NICK
403 * record, which (if found) is then copied to @a cls for future use.
405 * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
406 * @param private_key the private key of the zone (unused)
407 * @param label should be #GNUNET_GNS_MASTERZONE_STR
408 * @param rd_count number of records in @a rd
409 * @param rd records stored under @a label in the zone
412 lookup_nick_it (void *cls,
413 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
415 unsigned int rd_count,
416 const struct GNUNET_GNSRECORD_Data *rd)
418 struct GNUNET_GNSRECORD_Data **res = cls;
421 if (0 != strcmp (label, GNUNET_GNS_MASTERZONE_STR))
426 for (c = 0; c < rd_count; c++)
428 if (GNUNET_GNSRECORD_TYPE_NICK == rd[c].record_type)
430 (*res) = GNUNET_malloc (rd[c].data_size + sizeof (struct GNUNET_GNSRECORD_Data));
431 (*res)->data = &(*res)[1];
432 memcpy ((char *)(*res)->data, rd[c].data, rd[c].data_size);
433 (*res)->data_size = rd[c].data_size;
434 (*res)->expiration_time = rd[c].expiration_time;
435 (*res)->flags = rd[c].flags;
436 (*res)->record_type = GNUNET_GNSRECORD_TYPE_NICK;
445 * Return the NICK record for the zone (if it exists).
447 * @param zone private key for the zone to look for nick
448 * @return NULL if no NICK record was found
450 static struct GNUNET_GNSRECORD_Data *
451 get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
453 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
454 struct GNUNET_GNSRECORD_Data *nick;
457 res = GSN_database->lookup_records (GSN_database->cls, zone,
458 GNUNET_GNS_MASTERZONE_STR,
459 &lookup_nick_it, &nick);
460 if ((NULL == nick) || (GNUNET_OK != res))
462 GNUNET_CRYPTO_ecdsa_key_get_public (zone, &pub);
463 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
464 "No nick name set for zone `%s'\n",
465 GNUNET_GNSRECORD_z2s (&pub));
473 merge_with_nick_records ( const struct GNUNET_GNSRECORD_Data *nick_rd,
475 const struct GNUNET_GNSRECORD_Data *rd2,
476 unsigned int *rdc_res,
477 struct GNUNET_GNSRECORD_Data **rd_res)
479 uint64_t latest_expiration;
485 (*rdc_res) = 1 + rdc2;
495 req += sizeof (struct GNUNET_GNSRECORD_Data) + nick_rd[c].data_size;
496 for (c=0; c< rdc2; c++)
497 req += sizeof (struct GNUNET_GNSRECORD_Data) + rd2[c].data_size;
498 (*rd_res) = GNUNET_malloc (req);
499 data = (char *) &(*rd_res)[1 + rdc2];
501 latest_expiration = 0;
503 for (c=0; c< rdc2; c++)
505 if (0 != (rd2[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
507 if ((GNUNET_TIME_absolute_get().abs_value_us + rd2[c].expiration_time) >
509 latest_expiration = rd2[c].expiration_time;
511 else if (rd2[c].expiration_time > latest_expiration)
512 latest_expiration = rd2[c].expiration_time;
513 (*rd_res)[c] = rd2[c];
514 (*rd_res)[c].data = (void *) &data[data_offset];
516 memcpy ((void *) (*rd_res)[c].data, rd2[c].data, rd2[c].data_size);
517 data_offset += (*rd_res)[c].data_size;
519 record_offset = rdc2;
522 (*rd_res)[c+record_offset] = nick_rd[c];
523 (*rd_res)[c+record_offset].expiration_time = latest_expiration;
524 (*rd_res)[c+record_offset].data = (void *) &data[data_offset];
526 memcpy ((void *) (*rd_res)[c+record_offset].data,
528 nick_rd[c].data_size);
529 data_offset += (*rd_res)[c+record_offset].data_size;
531 GNUNET_assert (req == (sizeof (struct GNUNET_GNSRECORD_Data)) * (*rdc_res) + data_offset);
536 * Generate a `struct LookupNameResponseMessage` and send it to the
537 * given client using the given notification context.
539 * @param nc notification context to use
540 * @param client client to unicast to
541 * @param request_id request ID to use
542 * @param zone_key zone key of the zone
544 * @param rd_count number of records in @a rd
545 * @param rd array of records
548 send_lookup_response (struct GNUNET_SERVER_NotificationContext *nc,
549 struct GNUNET_SERVER_Client *client,
551 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
553 unsigned int rd_count,
554 const struct GNUNET_GNSRECORD_Data *rd)
556 struct RecordResultMessage *zir_msg;
557 struct GNUNET_GNSRECORD_Data *nick;
558 struct GNUNET_GNSRECORD_Data *res;
559 unsigned int res_count;
566 nick = get_nick_record (zone_key);
567 if ((NULL != nick) && (0 != strcmp(name, GNUNET_GNS_MASTERZONE_STR)))
569 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
570 merge_with_nick_records (nick, rd_count, rd, &res_count, &res);
571 //fprintf (stderr, "Merging %u records for `%s' with NICK records\n",rd_count, name);
576 res_count = rd_count;
577 res = (struct GNUNET_GNSRECORD_Data *) rd;
578 //fprintf (stderr, "Not merging %u records for `%s' with NICK records\n",rd_count, name);
581 name_len = strlen (name) + 1;
582 rd_ser_len = GNUNET_GNSRECORD_records_get_size (res_count, res);
583 msg_size = sizeof (struct RecordResultMessage) + name_len + rd_ser_len;
584 (void) client_lookup (client);
585 zir_msg = GNUNET_malloc (msg_size);
586 zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
587 zir_msg->gns_header.header.size = htons (msg_size);
588 zir_msg->gns_header.r_id = htonl (request_id);
589 zir_msg->name_len = htons (name_len);
590 zir_msg->rd_count = htons (res_count);
591 zir_msg->rd_len = htons (rd_ser_len);
592 zir_msg->private_key = *zone_key;
593 name_tmp = (char *) &zir_msg[1];
594 memcpy (name_tmp, name, name_len);
595 rd_ser = &name_tmp[name_len];
596 GNUNET_GNSRECORD_records_serialize (res_count, res, rd_ser_len, rd_ser);
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Sending `%s' message with %u records and size %u\n",
602 GNUNET_SERVER_notification_context_unicast (nc,
604 &zir_msg->gns_header.header,
608 GNUNET_free (zir_msg);
613 * Send response to the store request to the client.
615 * @param client client to talk to
616 * @param res status of the operation
617 * @param rid client's request ID
620 send_store_response (struct GNUNET_SERVER_Client *client,
624 struct RecordStoreResponseMessage rcr_msg;
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627 "Sending `%s' message\n",
628 "RECORD_STORE_RESPONSE");
629 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
630 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordStoreResponseMessage));
631 rcr_msg.gns_header.r_id = htonl (rid);
632 rcr_msg.op_result = htonl (res);
633 GNUNET_SERVER_notification_context_unicast (snc, client,
634 &rcr_msg.gns_header.header,
640 * Cache operation complete, clean up.
642 * @param cls the `struct CacheOperation`
643 * @param success success
644 * @param emsg error messages
647 finish_cache_operation (void *cls,
651 struct CacheOperation *cop = cls;
654 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
655 _("Failed to replicate block in namecache: %s\n"),
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 "CACHE operation completed\n");
660 GNUNET_CONTAINER_DLL_remove (cop_head,
663 if (NULL != cop->client)
664 send_store_response (cop->client,
672 * We just touched the plaintext information about a name in our zone;
673 * refresh the corresponding (encrypted) block in the namecache.
675 * @param client client responsible for the request
676 * @param rid request ID of the client
677 * @param zone_key private key of the zone
678 * @param name label for the records
679 * @param rd_count number of records
680 * @param rd records stored under the given @a name
683 refresh_block (struct GNUNET_SERVER_Client *client,
685 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
687 unsigned int rd_count,
688 const struct GNUNET_GNSRECORD_Data *rd)
690 struct GNUNET_GNSRECORD_Block *block;
691 struct CacheOperation *cop;
692 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
693 struct GNUNET_GNSRECORD_Data *nick;
694 struct GNUNET_GNSRECORD_Data *res;
695 unsigned int res_count;
697 nick = get_nick_record (zone_key);
698 res_count = rd_count;
699 res = (struct GNUNET_GNSRECORD_Data *) rd; /* fixme: a bit unclean... */
702 nick->flags = (nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
703 merge_with_nick_records (nick, rd_count,rd, &res_count, &res);
708 block = GNUNET_GNSRECORD_block_create (zone_key,
709 GNUNET_TIME_UNIT_ZERO_ABS,
713 block = GNUNET_GNSRECORD_block_create (zone_key,
714 GNUNET_GNSRECORD_record_get_expiration_time (res_count,
718 GNUNET_assert (NULL != block);
719 GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
722 "Caching block for label `%s' with %u records in zone `%s' in namecache\n",
725 GNUNET_GNSRECORD_z2s (&pkey));
726 cop = GNUNET_new (struct CacheOperation);
727 cop->client = client;
729 GNUNET_CONTAINER_DLL_insert (cop_head,
732 cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
734 &finish_cache_operation,
741 * Closure for #lookup_it().
743 struct RecordLookupContext
749 unsigned int res_rd_count;
755 struct GNUNET_GNSRECORD_Data *nick;
760 lookup_it (void *cls,
761 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
763 unsigned int rd_count,
764 const struct GNUNET_GNSRECORD_Data *rd)
766 struct RecordLookupContext *rlc = cls;
767 struct GNUNET_GNSRECORD_Data *rd_res;
768 unsigned int rdc_res;
770 if (0 == strcmp (label, rlc->label))
772 rlc->found = GNUNET_YES;
775 if ((NULL != rlc->nick) && (0 != strcmp(label, GNUNET_GNS_MASTERZONE_STR)))
780 rlc->nick->flags = (rlc->nick->flags | GNUNET_GNSRECORD_RF_PRIVATE) ^ GNUNET_GNSRECORD_RF_PRIVATE;
781 merge_with_nick_records (rlc->nick,
785 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rdc_res, rd_res);
786 rlc->res_rd_count = rdc_res;
787 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
788 GNUNET_GNSRECORD_records_serialize (rdc_res, rd_res, rlc->rd_ser_len , rlc->res_rd);
790 GNUNET_free (rd_res);
791 GNUNET_free (rlc->nick);
796 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
797 rlc->res_rd_count = rd_count;
798 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
799 GNUNET_GNSRECORD_records_serialize (rd_count, rd, rlc->rd_ser_len , rlc->res_rd);
805 rlc->res_rd_count = 0;
813 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
816 * @param client client sending the message
817 * @param message message of type 'struct RecordCreateMessage'
820 handle_record_lookup (void *cls,
821 struct GNUNET_SERVER_Client *client,
822 const struct GNUNET_MessageHeader *message)
824 const struct LabelLookupMessage *ll_msg;
825 struct LabelLookupResponseMessage *llr_msg;
826 struct RecordLookupContext rlc;
827 const char *name_tmp;
834 if (ntohs (message->size) < sizeof (struct LabelLookupMessage))
837 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
841 ll_msg = (const struct LabelLookupMessage *) message;
842 name_len = ntohl (ll_msg->label_len);
843 src_size = ntohs (message->size);
845 if (name_len != src_size - sizeof (struct LabelLookupMessage))
848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
852 name_tmp = (const char *) &ll_msg[1];
853 if ('\0' != name_tmp[name_len -1])
856 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
860 GNUNET_SERVER_receive_done (client, GNUNET_OK);
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 "Received `%s' message for name `%s'\n",
863 "NAMESTORE_RECORD_LOOKUP", name_tmp);
865 if (NULL == (client_lookup (client)))
868 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
872 rlc.label = name_tmp;
873 rlc.found = GNUNET_NO;
874 rlc.res_rd_count = 0;
877 rlc.nick = get_nick_record (&ll_msg->zone);
879 res = GSN_database->lookup_records (GSN_database->cls,
880 &ll_msg->zone, name_tmp, &lookup_it, &rlc);
882 res_size = sizeof (struct LabelLookupResponseMessage) + name_len + rlc.rd_ser_len;
883 llr_msg = GNUNET_malloc (res_size);
884 llr_msg->gns_header.header.size = htons (res_size);
885 llr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
886 llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
887 llr_msg->private_key = ll_msg->zone;
888 llr_msg->name_len = htons (name_len);
889 llr_msg->rd_count = htons (rlc.res_rd_count);
890 llr_msg->rd_len = htons (rlc.rd_ser_len);
891 res_name = (char *) &llr_msg[1];
892 if ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
893 llr_msg->found = ntohs (GNUNET_YES);
895 llr_msg->found = ntohs (GNUNET_NO);
896 memcpy (&llr_msg[1], name_tmp, name_len);
897 memcpy (&res_name[name_len], rlc.res_rd, rlc.rd_ser_len);
899 GNUNET_SERVER_notification_context_unicast (snc, client, &llr_msg->gns_header.header,
902 GNUNET_free_non_null (rlc.res_rd);
903 GNUNET_free (llr_msg);
908 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
911 * @param client client sending the message
912 * @param message message of type 'struct RecordCreateMessage'
915 handle_record_store (void *cls,
916 struct GNUNET_SERVER_Client *client,
917 const struct GNUNET_MessageHeader *message)
919 const struct RecordStoreMessage *rp_msg;
925 const char *name_tmp;
928 unsigned int rd_count;
930 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
931 struct ZoneMonitor *zm;
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 "Received `%s' message\n",
935 "NAMESTORE_RECORD_STORE");
936 if (ntohs (message->size) < sizeof (struct RecordStoreMessage))
939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
942 rp_msg = (const struct RecordStoreMessage *) message;
943 rid = ntohl (rp_msg->gns_header.r_id);
944 name_len = ntohs (rp_msg->name_len);
945 msg_size = ntohs (message->size);
946 rd_count = ntohs (rp_msg->rd_count);
947 rd_ser_len = ntohs (rp_msg->rd_len);
948 GNUNET_break (0 == ntohs (rp_msg->reserved));
949 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
950 if (msg_size != msg_size_exp)
953 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
956 if ((0 == name_len) || (name_len > MAX_NAME_LEN))
959 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
962 name_tmp = (const char *) &rp_msg[1];
963 rd_ser = &name_tmp[name_len];
964 if ('\0' != name_tmp[name_len -1])
967 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
970 (void) client_lookup (client);
972 struct GNUNET_GNSRECORD_Data rd[rd_count];
975 GNUNET_GNSRECORD_records_deserialize (rd_ser_len, rd_ser, rd_count, rd))
978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
982 /* Extracting and converting private key */
983 GNUNET_CRYPTO_ecdsa_key_get_public (&rp_msg->private_key,
985 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
986 if (NULL == conv_name)
988 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
989 "Error converting name `%s'\n", name_tmp);
990 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
994 "Creating %u records for name `%s' in zone `%s'\n",
995 (unsigned int) rd_count,
997 GNUNET_GNSRECORD_z2s (&pubkey));
999 if ( (0 == rd_count) &&
1001 GSN_database->iterate_records (GSN_database->cls,
1002 &rp_msg->private_key, 0, NULL, 0)) )
1004 /* This name does not exist, so cannot be removed */
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "Name `%s' does not exist, no deletion required\n",
1012 struct GNUNET_GNSRECORD_Data rd_clean[rd_count];
1014 unsigned int rd_clean_off;
1016 /* remove "NICK" records, unless this is for the "+" label */
1018 for (i=0;i<rd_count;i++)
1020 rd_clean[rd_clean_off] = rd[i];
1021 if ( (0 == strcmp (GNUNET_GNS_MASTERZONE_STR,
1023 (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
1026 res = GSN_database->store_records (GSN_database->cls,
1027 &rp_msg->private_key,
1029 rd_clean_off, rd_clean);
1030 if (GNUNET_OK == res)
1032 for (zm = monitor_head; NULL != zm; zm = zm->next)
1034 if ( (0 == memcmp (&rp_msg->private_key, &zm->zone,
1035 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) ||
1036 (0 == memcmp (&zm->zone,
1038 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1041 "Notifying monitor about changes under label `%s'\n",
1043 send_lookup_response (monitor_nc,
1046 &rp_msg->private_key,
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 "Monitor is for another zone\n");
1054 if (NULL == monitor_head)
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1056 "No monitors active\n");
1060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1061 "Error storing record: %d\n",
1065 if (GNUNET_OK == res)
1067 refresh_block (client, rid,
1068 &rp_msg->private_key,
1071 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1072 GNUNET_free (conv_name);
1075 GNUNET_free (conv_name);
1077 send_store_response (client, res, rid);
1078 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1083 * Context for record remove operations passed from #handle_zone_to_name to
1084 * #handle_zone_to_name_it as closure
1086 struct ZoneToNameCtx
1091 struct NamestoreClient *nc;
1094 * Request id (to be used in the response to the client).
1099 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
1100 * not finding a name for the zone still counts as a 'success' here,
1101 * as this field is about the success of executing the IPC protocol.
1108 * Zone to name iterator
1110 * @param cls struct ZoneToNameCtx *
1111 * @param zone_key the zone key
1113 * @param rd_count number of records in @a rd
1114 * @param rd record data
1117 handle_zone_to_name_it (void *cls,
1118 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1120 unsigned int rd_count,
1121 const struct GNUNET_GNSRECORD_Data *rd)
1123 struct ZoneToNameCtx *ztn_ctx = cls;
1124 struct ZoneToNameResponseMessage *ztnr_msg;
1132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1133 "Found result for zone-to-name lookup: `%s'\n",
1136 name_len = (NULL == name) ? 0 : strlen (name) + 1;
1137 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
1138 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
1139 if (msg_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1142 ztn_ctx->success = GNUNET_SYSERR;
1145 ztnr_msg = GNUNET_malloc (msg_size);
1146 ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1147 ztnr_msg->gns_header.header.size = htons (msg_size);
1148 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
1149 ztnr_msg->res = htons (res);
1150 ztnr_msg->rd_len = htons (rd_ser_len);
1151 ztnr_msg->rd_count = htons (rd_count);
1152 ztnr_msg->name_len = htons (name_len);
1153 ztnr_msg->zone = *zone_key;
1154 name_tmp = (char *) &ztnr_msg[1];
1156 memcpy (name_tmp, name, name_len);
1157 rd_tmp = &name_tmp[name_len];
1158 GNUNET_GNSRECORD_records_serialize (rd_count, rd, rd_ser_len, rd_tmp);
1159 ztn_ctx->success = GNUNET_OK;
1160 GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client,
1161 &ztnr_msg->gns_header.header,
1163 GNUNET_free (ztnr_msg);
1168 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
1171 * @param client client sending the message
1172 * @param message message of type 'struct ZoneToNameMessage'
1175 handle_zone_to_name (void *cls,
1176 struct GNUNET_SERVER_Client *client,
1177 const struct GNUNET_MessageHeader *message)
1179 struct NamestoreClient *nc;
1180 const struct ZoneToNameMessage *ztn_msg;
1181 struct ZoneToNameCtx ztn_ctx;
1182 struct ZoneToNameResponseMessage ztnr_msg;
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 "Received `%s' message\n",
1187 ztn_msg = (const struct ZoneToNameMessage *) message;
1188 nc = client_lookup (client);
1189 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
1191 ztn_ctx.success = GNUNET_NO;
1192 if (GNUNET_SYSERR ==
1193 GSN_database->zone_to_name (GSN_database->cls,
1195 &ztn_msg->value_zone,
1196 &handle_zone_to_name_it, &ztn_ctx))
1198 /* internal error, hang up instead of signalling something
1199 that might be wrong */
1201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1204 if (GNUNET_NO == ztn_ctx.success)
1206 /* no result found, send empty response */
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "Found no result for zone-to-name lookup.\n");
1209 memset (&ztnr_msg, 0, sizeof (ztnr_msg));
1210 ztnr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
1211 ztnr_msg.gns_header.header.size = htons (sizeof (ztnr_msg));
1212 ztnr_msg.gns_header.r_id = ztn_msg->gns_header.r_id;
1213 ztnr_msg.res = htons (GNUNET_NO);
1214 GNUNET_SERVER_notification_context_unicast (snc,
1216 &ztnr_msg.gns_header.header,
1219 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1224 * Zone iteration processor result
1226 enum ZoneIterationResult
1235 * Continue to iterate with next iteration_next call
1237 IT_SUCCESS_MORE_AVAILABLE = 1,
1240 * Iteration complete
1242 IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE = 2
1247 * Context for record remove operations passed from
1248 * #run_zone_iteration_round to #zone_iterate_proc as closure
1250 struct ZoneIterationProcResult
1253 * The zone iteration handle
1255 struct ZoneIteration *zi;
1258 * Iteration result: iteration done?
1259 * #IT_SUCCESS_MORE_AVAILABLE: if there may be more results overall but
1260 * we got one for now and have sent it to the client
1261 * #IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE: if there are no further results,
1262 * #IT_START: if we are still trying to find a result.
1264 int res_iteration_finished;
1270 * Process results for zone iteration from database
1272 * @param cls struct ZoneIterationProcResult *proc
1273 * @param zone_key the zone key
1275 * @param rd_count number of records for this name
1276 * @param rd record data
1279 zone_iterate_proc (void *cls,
1280 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1282 unsigned int rd_count,
1283 const struct GNUNET_GNSRECORD_Data *rd)
1285 struct ZoneIterationProcResult *proc = cls;
1287 int do_refresh_block;
1289 if ((NULL == zone_key) && (NULL == name))
1291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1292 "Iteration done\n");
1293 proc->res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1296 if ((NULL == zone_key) || (NULL == name))
1298 /* what is this!? should never happen */
1299 proc->res_iteration_finished = IT_START;
1303 proc->res_iteration_finished = IT_SUCCESS_MORE_AVAILABLE;
1304 send_lookup_response (snc,
1305 proc->zi->client->client,
1306 proc->zi->request_id,
1311 do_refresh_block = GNUNET_NO;
1312 for (i=0;i<rd_count;i++)
1313 if( (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) &&
1314 (0 == (rd[i].flags & GNUNET_GNSRECORD_RF_PENDING)) )
1316 do_refresh_block = GNUNET_YES;
1319 if (GNUNET_YES == do_refresh_block)
1320 refresh_block (NULL, 0,
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 RecordResultMessage rrm;
1341 memset (&proc, 0, sizeof (proc));
1343 proc.res_iteration_finished = IT_START;
1344 while (IT_START == proc.res_iteration_finished)
1346 if (GNUNET_SYSERR ==
1347 (ret = GSN_database->iterate_records (GSN_database->cls,
1348 (0 == memcmp (&zi->zone, &zero, sizeof (zero)))
1352 &zone_iterate_proc, &proc)))
1357 if (GNUNET_NO == ret)
1358 proc.res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1361 if (IT_SUCCESS_MORE_AVAILABLE == proc.res_iteration_finished)
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "More results available\n");
1365 return; /* more results later */
1367 /* send empty response to indicate end of list */
1368 memset (&rrm, 0, sizeof (rrm));
1369 rrm.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
1370 rrm.gns_header.header.size = htons (sizeof (rrm));
1371 rrm.gns_header.r_id = htonl (zi->request_id);
1372 GNUNET_SERVER_notification_context_unicast (snc,
1374 &rrm.gns_header.header,
1376 GNUNET_CONTAINER_DLL_remove (zi->client->op_head,
1377 zi->client->op_tail,
1384 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1387 * @param client the client sending the message
1388 * @param message message of type 'struct ZoneIterationStartMessage'
1391 handle_iteration_start (void *cls,
1392 struct GNUNET_SERVER_Client *client,
1393 const struct GNUNET_MessageHeader *message)
1395 const struct ZoneIterationStartMessage *zis_msg;
1396 struct NamestoreClient *nc;
1397 struct ZoneIteration *zi;
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1400 if (NULL == (nc = client_lookup (client)))
1403 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1406 zis_msg = (const struct ZoneIterationStartMessage *) message;
1407 zi = GNUNET_new (struct ZoneIteration);
1408 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1411 zi->zone = zis_msg->zone;
1414 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1415 run_zone_iteration_round (zi);
1416 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1421 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1424 * @param client GNUNET_SERVER_Client sending the message
1425 * @param message message of type 'struct ZoneIterationStopMessage'
1428 handle_iteration_stop (void *cls,
1429 struct GNUNET_SERVER_Client *client,
1430 const struct GNUNET_MessageHeader *message)
1432 struct NamestoreClient *nc;
1433 struct ZoneIteration *zi;
1434 const struct ZoneIterationStopMessage *zis_msg;
1437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1438 "Received `%s' message\n",
1439 "ZONE_ITERATION_STOP");
1440 if (NULL == (nc = client_lookup(client)))
1443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1446 zis_msg = (const struct ZoneIterationStopMessage *) message;
1447 rid = ntohl (zis_msg->gns_header.r_id);
1448 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1449 if (zi->request_id == rid)
1454 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1457 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1459 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1464 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1467 * @param client GNUNET_SERVER_Client sending the message
1468 * @param message message of type 'struct ZoneIterationNextMessage'
1471 handle_iteration_next (void *cls,
1472 struct GNUNET_SERVER_Client *client,
1473 const struct GNUNET_MessageHeader *message)
1475 struct NamestoreClient *nc;
1476 struct ZoneIteration *zi;
1477 const struct ZoneIterationNextMessage *zis_msg;
1480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1481 "Received `%s' message\n",
1482 "ZONE_ITERATION_NEXT");
1483 if (NULL == (nc = client_lookup(client)))
1486 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1489 zis_msg = (const struct ZoneIterationNextMessage *) message;
1490 rid = ntohl (zis_msg->gns_header.r_id);
1491 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1492 if (zi->request_id == rid)
1497 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1500 run_zone_iteration_round (zi);
1501 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1506 * Send 'sync' message to zone monitor, we're now in sync.
1508 * @param zm monitor that is now in sync
1511 monitor_sync (struct ZoneMonitor *zm)
1513 struct GNUNET_MessageHeader sync;
1515 sync.size = htons (sizeof (struct GNUNET_MessageHeader));
1516 sync.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1517 GNUNET_SERVER_notification_context_unicast (monitor_nc,
1525 * Obtain the next datum during the zone monitor's zone intiial iteration.
1527 * @param cls zone monitor that does its initial iteration
1528 * @param tc scheduler context
1531 monitor_next (void *cls,
1532 const struct GNUNET_SCHEDULER_TaskContext *tc);
1536 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1538 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1539 * @param zone_key zone key of the zone
1541 * @param rd_count number of records in @a rd
1542 * @param rd array of records
1545 monitor_iterate_cb (void *cls,
1546 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1548 unsigned int rd_count,
1549 const struct GNUNET_GNSRECORD_Data *rd)
1551 struct ZoneMonitor *zm = cls;
1555 /* finished with iteration */
1559 send_lookup_response (monitor_nc,
1566 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1571 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
1574 * @param client the client sending the message
1575 * @param message message of type 'struct ZoneMonitorStartMessage'
1578 handle_monitor_start (void *cls,
1579 struct GNUNET_SERVER_Client *client,
1580 const struct GNUNET_MessageHeader *message)
1582 const struct ZoneMonitorStartMessage *zis_msg;
1583 struct ZoneMonitor *zm;
1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1586 "Received `%s' message\n",
1587 "ZONE_MONITOR_START");
1588 zis_msg = (const struct ZoneMonitorStartMessage *) message;
1589 zm = GNUNET_new (struct ZoneMonitor);
1591 zm->nc = client_lookup (client);
1592 zm->zone = zis_msg->zone;
1593 GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, zm);
1594 GNUNET_SERVER_client_mark_monitor (client);
1595 GNUNET_SERVER_disable_receive_done_warning (client);
1596 GNUNET_SERVER_notification_context_add (monitor_nc,
1598 if (GNUNET_YES == ntohl (zis_msg->iterate_first))
1599 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1606 * Obtain the next datum during the zone monitor's zone intiial iteration.
1608 * @param cls zone monitor that does its initial iteration
1609 * @param tc scheduler context
1612 monitor_next (void *cls,
1613 const struct GNUNET_SCHEDULER_TaskContext *tc)
1615 struct ZoneMonitor *zm = cls;
1618 zm->task = GNUNET_SCHEDULER_NO_TASK;
1619 ret = GSN_database->iterate_records (GSN_database->cls,
1620 (0 == memcmp (&zm->zone, &zero, sizeof (zero)))
1624 &monitor_iterate_cb, zm);
1625 if (GNUNET_SYSERR == ret)
1627 GNUNET_SERVER_client_disconnect (zm->nc->client);
1630 if (GNUNET_NO == ret)
1640 * Process namestore requests.
1642 * @param cls closure
1643 * @param server the initialized server
1644 * @param cfg configuration to use
1647 run (void *cls, struct GNUNET_SERVER_Handle *server,
1648 const struct GNUNET_CONFIGURATION_Handle *cfg)
1650 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1651 {&handle_record_store, NULL,
1652 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE, 0},
1653 {&handle_record_lookup, NULL,
1654 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP, 0},
1655 {&handle_zone_to_name, NULL,
1656 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, sizeof (struct ZoneToNameMessage) },
1657 {&handle_iteration_start, NULL,
1658 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage) },
1659 {&handle_iteration_next, NULL,
1660 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, sizeof (struct ZoneIterationNextMessage) },
1661 {&handle_iteration_stop, NULL,
1662 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage) },
1663 {&handle_monitor_start, NULL,
1664 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START, sizeof (struct ZoneMonitorStartMessage) },
1669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1671 monitor_nc = GNUNET_SERVER_notification_context_create (server, 1);
1672 namecache = GNUNET_NAMECACHE_connect (cfg);
1673 /* Loading database plugin */
1675 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1677 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1679 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1680 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1681 GNUNET_free (database);
1682 if (NULL == GSN_database)
1684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1685 "Could not load database backend `%s'\n",
1687 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1691 /* Configuring server handles */
1692 GNUNET_SERVER_add_handlers (server, handlers);
1693 snc = GNUNET_SERVER_notification_context_create (server, 16);
1694 GNUNET_SERVER_disconnect_notify (server,
1695 &client_disconnect_notification,
1697 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1703 * The main function for the template service.
1705 * @param argc number of arguments from the command line
1706 * @param argv command line arguments
1707 * @return 0 ok, 1 on error
1710 main (int argc, char *const *argv)
1712 return (GNUNET_OK ==
1713 GNUNET_SERVICE_run (argc, argv, "namestore",
1714 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1717 /* end of gnunet-service-namestore.c */