2 This file is part of GNUnet.
3 (C) 2012, 2013 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_namecache_service.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_namestore_plugin.h"
33 #include "gnunet_signatures.h"
34 #include "namestore.h"
36 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
42 struct NamestoreClient;
46 * A namestore iteration operation.
51 * Next element in the DLL
53 struct ZoneIteration *next;
56 * Previous element in the DLL
58 struct ZoneIteration *prev;
61 * Namestore client which intiated this zone iteration
63 struct NamestoreClient *client;
66 * Key of the zone we are iterating over.
68 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
71 * The operation id fot the zone iteration in the response for the client
76 * Offset of the zone iteration used to address next result of the zone
77 * iteration in the store
79 * Initialy set to 0 in handle_iteration_start
80 * Incremented with by every call to handle_iteration_next
90 struct NamestoreClient
93 * Next element in the DLL
95 struct NamestoreClient *next;
98 * Previous element in the DLL
100 struct NamestoreClient *prev;
105 struct GNUNET_SERVER_Client *client;
109 * Zone iteration operations in progress initiated by this client
111 struct ZoneIteration *op_head;
115 * Zone iteration operations in progress initiated by this client
117 struct ZoneIteration *op_tail;
122 * A namestore monitor.
127 * Next element in the DLL
129 struct ZoneMonitor *next;
132 * Previous element in the DLL
134 struct ZoneMonitor *prev;
137 * Namestore client which intiated this zone monitor
139 struct NamestoreClient *nc;
142 * Private key of the zone.
144 struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
147 * Task active during initial iteration.
149 GNUNET_SCHEDULER_TaskIdentifier task;
152 * Offset of the zone iteration used to address next result of the zone
153 * iteration in the store
156 * Incremented with by every call to #handle_iteration_next
164 * Pending operation on the namecache.
166 struct CacheOperation
172 struct CacheOperation *prev;
177 struct CacheOperation *next;
180 * Handle to namecache queue.
182 struct GNUNET_NAMECACHE_QueueEntry *qe;
185 * Client to notify about the result.
187 struct GNUNET_SERVER_Client *client;
190 * Client's request ID.
197 * Public key of all zeros.
199 static const struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
202 * Configuration handle.
204 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
209 static struct GNUNET_NAMECACHE_Handle *namecache;
214 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
217 * Name of the database plugin
219 static char *db_lib_name;
222 * Our notification context.
224 static struct GNUNET_SERVER_NotificationContext *snc;
227 * Head of the Client DLL
229 static struct NamestoreClient *client_head;
232 * Tail of the Client DLL
234 static struct NamestoreClient *client_tail;
239 static struct CacheOperation *cop_head;
244 static struct CacheOperation *cop_tail;
247 * First active zone monitor.
249 static struct ZoneMonitor *monitor_head;
252 * Last active zone monitor.
254 static struct ZoneMonitor *monitor_tail;
257 * Notification context shared by all monitors.
259 static struct GNUNET_SERVER_NotificationContext *monitor_nc;
264 * Task run during shutdown.
270 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
272 struct ZoneIteration *no;
273 struct NamestoreClient *nc;
274 struct CacheOperation *cop;
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
277 "Stopping namestore service\n");
280 GNUNET_SERVER_notification_context_destroy (snc);
283 while (NULL != (cop = cop_head))
285 GNUNET_NAMECACHE_cancel (cop->qe);
286 GNUNET_CONTAINER_DLL_remove (cop_head,
291 GNUNET_NAMECACHE_disconnect (namecache);
293 while (NULL != (nc = client_head))
295 while (NULL != (no = nc->op_head))
297 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
300 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
301 GNUNET_SERVER_client_set_user_context (nc->client, NULL);
304 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
305 GNUNET_free (db_lib_name);
307 if (NULL != monitor_nc)
309 GNUNET_SERVER_notification_context_destroy (monitor_nc);
316 * Called whenever a client is disconnected.
317 * Frees our resources associated with that client.
320 * @param client identification of the client
323 client_disconnect_notification (void *cls,
324 struct GNUNET_SERVER_Client *client)
326 struct ZoneIteration *no;
327 struct NamestoreClient *nc;
328 struct ZoneMonitor *zm;
329 struct CacheOperation *cop;
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334 "Client %p disconnected\n",
336 if (NULL == (nc = GNUNET_SERVER_client_get_user_context (client, struct NamestoreClient)))
338 while (NULL != (no = nc->op_head))
340 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
343 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
345 for (zm = monitor_head; NULL != zm; zm = zm->next)
347 if (client == zm->nc->client)
349 GNUNET_CONTAINER_DLL_remove (monitor_head,
352 if (GNUNET_SCHEDULER_NO_TASK != zm->task)
354 GNUNET_SCHEDULER_cancel (zm->task);
355 zm->task = GNUNET_SCHEDULER_NO_TASK;
361 for (cop = cop_head; NULL != cop; cop = cop->next)
362 if (client == cop->client)
368 * Add a client to our list of active clients, if it is not yet
371 * @param client client to add
372 * @return internal namestore client structure for this client
374 static struct NamestoreClient *
375 client_lookup (struct GNUNET_SERVER_Client *client)
377 struct NamestoreClient *nc;
379 nc = GNUNET_SERVER_client_get_user_context (client, struct NamestoreClient);
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Client %p connected\n",
385 nc = GNUNET_new (struct NamestoreClient);
387 GNUNET_SERVER_notification_context_add (snc, client);
388 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, nc);
389 GNUNET_SERVER_client_set_user_context (client, nc);
395 * Generate a 'struct LookupNameResponseMessage' and send it to the
396 * given client using the given notification context.
398 * @param nc notification context to use
399 * @param client client to unicast to
400 * @param request_id request ID to use
401 * @param zone_key zone key of the zone
403 * @param rd_count number of records in @a rd
404 * @param rd array of records
407 send_lookup_response (struct GNUNET_SERVER_NotificationContext *nc,
408 struct GNUNET_SERVER_Client *client,
410 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
412 unsigned int rd_count,
413 const struct GNUNET_GNSRECORD_Data *rd)
415 struct RecordResultMessage *zir_msg;
422 name_len = strlen (name) + 1;
423 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
424 msg_size = sizeof (struct RecordResultMessage) + name_len + rd_ser_len;
425 (void) client_lookup (client);
426 zir_msg = GNUNET_malloc (msg_size);
427 zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
428 zir_msg->gns_header.header.size = htons (msg_size);
429 zir_msg->gns_header.r_id = htonl (request_id);
430 zir_msg->name_len = htons (name_len);
431 zir_msg->rd_count = htons (rd_count);
432 zir_msg->rd_len = htons (rd_ser_len);
433 zir_msg->private_key = *zone_key;
434 name_tmp = (char *) &zir_msg[1];
435 memcpy (name_tmp, name, name_len);
436 rd_ser = &name_tmp[name_len];
437 GNUNET_GNSRECORD_records_serialize (rd_count, rd, rd_ser_len, rd_ser);
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "Sending `%s' message with %u records and size %u\n",
443 GNUNET_SERVER_notification_context_unicast (nc,
445 &zir_msg->gns_header.header,
447 GNUNET_free (zir_msg);
452 * Send response to the store request to the client.
454 * @param client client to talk to
455 * @param res status of the operation
456 * @param rid client's request ID
459 send_store_response (struct GNUNET_SERVER_Client *client,
463 struct RecordStoreResponseMessage rcr_msg;
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
466 "Sending `%s' message\n",
467 "RECORD_STORE_RESPONSE");
468 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
469 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordStoreResponseMessage));
470 rcr_msg.gns_header.r_id = htonl (rid);
471 rcr_msg.op_result = htonl (res);
472 GNUNET_SERVER_notification_context_unicast (snc, client,
473 &rcr_msg.gns_header.header,
479 * Cache operation complete, clean up.
481 * @param cls the `struct CacheOperation`
482 * @param success success
483 * @param emsg error messages
486 finish_cache_operation (void *cls,
490 struct CacheOperation *cop = cls;
493 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
494 _("Failed to replicate block in namecache: %s\n"),
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "CACHE operation completed\n");
499 GNUNET_CONTAINER_DLL_remove (cop_head,
502 if (NULL != cop->client)
503 send_store_response (cop->client,
511 * We just touched the plaintext information about a name in our zone;
512 * refresh the corresponding (encrypted) block in the namestore.
514 * @param client client responsible for the request
515 * @param rid request ID of the client
516 * @param zone_key private key of the zone
517 * @param name label for the records
518 * @param rd_count number of records
519 * @param rd records stored under the given @a name
522 refresh_block (struct GNUNET_SERVER_Client *client,
524 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
526 unsigned int rd_count,
527 const struct GNUNET_GNSRECORD_Data *rd)
529 struct GNUNET_GNSRECORD_Block *block;
530 struct CacheOperation *cop;
531 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
534 block = GNUNET_GNSRECORD_block_create (zone_key,
535 GNUNET_TIME_UNIT_ZERO_ABS,
539 block = GNUNET_GNSRECORD_block_create (zone_key,
540 GNUNET_GNSRECORD_record_get_expiration_time (rd_count,
544 GNUNET_assert (NULL != block);
545 GNUNET_CRYPTO_ecdsa_key_get_public (zone_key,
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548 "Caching block for label `%s' in zone `%s' in namecache\n",
550 GNUNET_GNSRECORD_z2s (&pkey));
551 cop = GNUNET_new (struct CacheOperation);
552 cop->client = client;
554 GNUNET_CONTAINER_DLL_insert (cop_head,
557 cop->qe = GNUNET_NAMECACHE_block_cache (namecache,
559 &finish_cache_operation,
565 struct RecordLookupContext
571 unsigned int res_rd_count;
578 static void lookup_it (void *cls,
579 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
581 unsigned int rd_count,
582 const struct GNUNET_GNSRECORD_Data *rd)
584 struct RecordLookupContext *rlc = cls;
586 if (0 == strcmp (label, rlc->label))
588 rlc->found = GNUNET_YES;
591 rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
592 rlc->res_rd_count = rd_count;
593 rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
594 GNUNET_GNSRECORD_records_serialize (rd_count, rd, rlc->rd_ser_len , rlc->res_rd);
599 rlc->res_rd_count = 0;
607 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
610 * @param client client sending the message
611 * @param message message of type 'struct RecordCreateMessage'
614 handle_record_lookup (void *cls,
615 struct GNUNET_SERVER_Client *client,
616 const struct GNUNET_MessageHeader *message)
618 const struct LabelLookupMessage *ll_msg;
619 struct LabelLookupResponseMessage *llr_msg;
620 struct RecordLookupContext rlc;
621 const char *name_tmp;
629 if (ntohs (message->size) < sizeof (struct LabelLookupMessage))
632 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
636 ll_msg = (const struct LabelLookupMessage *) message;
637 name_len = ntohl (ll_msg->label_len);
638 src_size = ntohs (message->size);
640 if (name_len != src_size - sizeof (struct LabelLookupMessage))
643 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
647 name_tmp = (const char *) &ll_msg[1];
648 if ('\0' != name_tmp[name_len -1])
651 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
655 GNUNET_SERVER_receive_done (client, GNUNET_OK);
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "Received `%s' message for name `%s'\n",
658 "NAMESTORE_RECORD_LOOKUP", name_tmp);
661 rlc.label = name_tmp;
662 rlc.found = GNUNET_NO;
663 rlc.res_rd_count = 0;
670 /* changee this call */
671 res = GSN_database->iterate_records (GSN_database->cls,
672 &ll_msg->zone, offset, &lookup_it, &rlc);
675 while ((GNUNET_NO == rlc.found) && (GNUNET_OK == res));
677 res_size = sizeof (struct LabelLookupResponseMessage) + name_len + rlc.rd_ser_len;
678 llr_msg = GNUNET_malloc (res_size);
679 llr_msg->gns_header.header.size = htons (res_size);
680 llr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
681 llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
682 llr_msg->private_key = ll_msg->zone;
683 llr_msg->name_len = htons (name_len);
684 llr_msg->rd_count = htons (rlc.res_rd_count);
685 llr_msg->rd_len = htons (rlc.rd_ser_len);
686 res_name = (char *) &llr_msg[1];
687 memcpy (&llr_msg[1], name_tmp, name_len);
688 memcpy (&res_name[name_len], rlc.res_rd, rlc.rd_ser_len);
690 GNUNET_SERVER_notification_context_unicast (snc, client, &llr_msg->gns_header.header,
693 GNUNET_free_non_null (rlc.res_rd);
694 GNUNET_free (llr_msg);
699 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
702 * @param client client sending the message
703 * @param message message of type 'struct RecordCreateMessage'
706 handle_record_store (void *cls,
707 struct GNUNET_SERVER_Client *client,
708 const struct GNUNET_MessageHeader *message)
710 const struct RecordStoreMessage *rp_msg;
716 const char *name_tmp;
719 unsigned int rd_count;
721 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
722 struct ZoneMonitor *zm;
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
725 "Received `%s' message\n",
726 "NAMESTORE_RECORD_STORE");
727 if (ntohs (message->size) < sizeof (struct RecordStoreMessage))
730 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
733 rp_msg = (const struct RecordStoreMessage *) message;
734 rid = ntohl (rp_msg->gns_header.r_id);
735 name_len = ntohs (rp_msg->name_len);
736 msg_size = ntohs (message->size);
737 rd_count = ntohs (rp_msg->rd_count);
738 rd_ser_len = ntohs (rp_msg->rd_len);
739 GNUNET_break (0 == ntohs (rp_msg->reserved));
740 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
741 if (msg_size != msg_size_exp)
744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
747 if ((0 == name_len) || (name_len > MAX_NAME_LEN))
750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
753 name_tmp = (const char *) &rp_msg[1];
754 rd_ser = &name_tmp[name_len];
755 if ('\0' != name_tmp[name_len -1])
758 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
761 (void) client_lookup (client);
763 struct GNUNET_GNSRECORD_Data rd[rd_count];
766 GNUNET_GNSRECORD_records_deserialize (rd_ser_len, rd_ser, rd_count, rd))
769 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
773 /* Extracting and converting private key */
774 GNUNET_CRYPTO_ecdsa_key_get_public (&rp_msg->private_key,
776 conv_name = GNUNET_GNSRECORD_string_to_lowercase (name_tmp);
777 if (NULL == conv_name)
779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
780 "Error converting name `%s'\n", name_tmp);
781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785 "Creating %u records for name `%s' in zone `%s'\n",
786 (unsigned int) rd_count,
788 GNUNET_GNSRECORD_z2s (&pubkey));
790 if ( (0 == rd_count) &&
792 GSN_database->iterate_records (GSN_database->cls,
793 &rp_msg->private_key, 0, NULL, 0)) )
795 /* This name does not exist, so cannot be removed */
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797 "Name `%s' does not exist, no deletion required\n",
803 res = GSN_database->store_records (GSN_database->cls,
804 &rp_msg->private_key,
807 if (GNUNET_OK == res)
809 for (zm = monitor_head; NULL != zm; zm = zm->next)
811 if ( (0 == memcmp (&rp_msg->private_key, &zm->zone,
812 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) ||
813 (0 == memcmp (&zm->zone,
815 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 "Notifying monitor about changes under label `%s'\n",
820 send_lookup_response (monitor_nc,
823 &rp_msg->private_key,
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 "Monitor is for another zone\n");
831 if (NULL == monitor_head)
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
833 "No monitors active\n");
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "Error storing record: %d\n",
842 if (GNUNET_OK == res)
844 refresh_block (client, rid,
845 &rp_msg->private_key,
848 GNUNET_SERVER_receive_done (client, GNUNET_OK);
849 GNUNET_free (conv_name);
852 GNUNET_free (conv_name);
854 send_store_response (client, res, rid);
855 GNUNET_SERVER_receive_done (client, GNUNET_OK);
860 * Context for record remove operations passed from #handle_zone_to_name to
861 * #handle_zone_to_name_it as closure
868 struct NamestoreClient *nc;
871 * Request id (to be used in the response to the client).
876 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
877 * not finding a name for the zone still counts as a 'success' here,
878 * as this field is about the success of executing the IPC protocol.
885 * Zone to name iterator
887 * @param cls struct ZoneToNameCtx *
888 * @param zone_key the zone key
890 * @param rd_count number of records in @a rd
891 * @param rd record data
894 handle_zone_to_name_it (void *cls,
895 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
897 unsigned int rd_count,
898 const struct GNUNET_GNSRECORD_Data *rd)
900 struct ZoneToNameCtx *ztn_ctx = cls;
901 struct ZoneToNameResponseMessage *ztnr_msg;
909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
910 "Found result for zone-to-name lookup: `%s'\n",
913 name_len = (NULL == name) ? 0 : strlen (name) + 1;
914 rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
915 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
916 if (msg_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
919 ztn_ctx->success = GNUNET_SYSERR;
922 ztnr_msg = GNUNET_malloc (msg_size);
923 ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
924 ztnr_msg->gns_header.header.size = htons (msg_size);
925 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
926 ztnr_msg->res = htons (res);
927 ztnr_msg->rd_len = htons (rd_ser_len);
928 ztnr_msg->rd_count = htons (rd_count);
929 ztnr_msg->name_len = htons (name_len);
930 ztnr_msg->zone = *zone_key;
931 name_tmp = (char *) &ztnr_msg[1];
933 memcpy (name_tmp, name, name_len);
934 rd_tmp = &name_tmp[name_len];
935 GNUNET_GNSRECORD_records_serialize (rd_count, rd, rd_ser_len, rd_tmp);
936 ztn_ctx->success = GNUNET_OK;
937 GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client,
938 &ztnr_msg->gns_header.header,
940 GNUNET_free (ztnr_msg);
945 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
948 * @param client client sending the message
949 * @param message message of type 'struct ZoneToNameMessage'
952 handle_zone_to_name (void *cls,
953 struct GNUNET_SERVER_Client *client,
954 const struct GNUNET_MessageHeader *message)
956 struct NamestoreClient *nc;
957 const struct ZoneToNameMessage *ztn_msg;
958 struct ZoneToNameCtx ztn_ctx;
959 struct ZoneToNameResponseMessage ztnr_msg;
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "Received `%s' message\n",
964 ztn_msg = (const struct ZoneToNameMessage *) message;
965 nc = client_lookup (client);
966 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
968 ztn_ctx.success = GNUNET_NO;
970 GSN_database->zone_to_name (GSN_database->cls,
972 &ztn_msg->value_zone,
973 &handle_zone_to_name_it, &ztn_ctx))
975 /* internal error, hang up instead of signalling something
976 that might be wrong */
978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
981 if (GNUNET_NO == ztn_ctx.success)
983 /* no result found, send empty response */
984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
985 "Found no result for zone-to-name lookup.\n");
986 memset (&ztnr_msg, 0, sizeof (ztnr_msg));
987 ztnr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
988 ztnr_msg.gns_header.header.size = htons (sizeof (ztnr_msg));
989 ztnr_msg.gns_header.r_id = ztn_msg->gns_header.r_id;
990 ztnr_msg.res = htons (GNUNET_NO);
991 GNUNET_SERVER_notification_context_unicast (snc,
993 &ztnr_msg.gns_header.header,
996 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1001 * Zone iteration processor result
1003 enum ZoneIterationResult
1012 * Continue to iterate with next iteration_next call
1014 IT_SUCCESS_MORE_AVAILABLE = 1,
1017 * Iteration complete
1019 IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE = 2
1024 * Context for record remove operations passed from
1025 * #run_zone_iteration_round to #zone_iterate_proc as closure
1027 struct ZoneIterationProcResult
1030 * The zone iteration handle
1032 struct ZoneIteration *zi;
1035 * Iteration result: iteration done?
1036 * #IT_SUCCESS_MORE_AVAILABLE: if there may be more results overall but
1037 * we got one for now and have sent it to the client
1038 * #IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE: if there are no further results,
1039 * #IT_START: if we are still trying to find a result.
1041 int res_iteration_finished;
1047 * Process results for zone iteration from database
1049 * @param cls struct ZoneIterationProcResult *proc
1050 * @param zone_key the zone key
1052 * @param rd_count number of records for this name
1053 * @param rd record data
1056 zone_iterate_proc (void *cls,
1057 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1059 unsigned int rd_count,
1060 const struct GNUNET_GNSRECORD_Data *rd)
1062 struct ZoneIterationProcResult *proc = cls;
1064 int do_refresh_block;
1066 if ((NULL == zone_key) && (NULL == name))
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "Iteration done\n");
1070 proc->res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1073 if ((NULL == zone_key) || (NULL == name))
1075 /* what is this!? should never happen */
1076 proc->res_iteration_finished = IT_START;
1080 proc->res_iteration_finished = IT_SUCCESS_MORE_AVAILABLE;
1081 send_lookup_response (snc,
1082 proc->zi->client->client,
1083 proc->zi->request_id,
1088 do_refresh_block = GNUNET_NO;
1089 for (i=0;i<rd_count;i++)
1090 if( (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) &&
1091 (0 == (rd[i].flags & GNUNET_GNSRECORD_RF_PENDING)) )
1093 do_refresh_block = GNUNET_YES;
1096 if (GNUNET_YES == do_refresh_block)
1097 refresh_block (NULL, 0,
1107 * Perform the next round of the zone iteration.
1109 * @param zi zone iterator to process
1112 run_zone_iteration_round (struct ZoneIteration *zi)
1114 struct ZoneIterationProcResult proc;
1115 struct RecordResultMessage rrm;
1118 memset (&proc, 0, sizeof (proc));
1120 proc.res_iteration_finished = IT_START;
1121 while (IT_START == proc.res_iteration_finished)
1123 if (GNUNET_SYSERR ==
1124 (ret = GSN_database->iterate_records (GSN_database->cls,
1125 (0 == memcmp (&zi->zone, &zero, sizeof (zero)))
1129 &zone_iterate_proc, &proc)))
1134 if (GNUNET_NO == ret)
1135 proc.res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1138 if (IT_SUCCESS_MORE_AVAILABLE == proc.res_iteration_finished)
1140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1141 "More results available\n");
1142 return; /* more results later */
1144 /* send empty response to indicate end of list */
1145 memset (&rrm, 0, sizeof (rrm));
1146 rrm.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
1147 rrm.gns_header.header.size = htons (sizeof (rrm));
1148 rrm.gns_header.r_id = htonl (zi->request_id);
1149 GNUNET_SERVER_notification_context_unicast (snc,
1151 &rrm.gns_header.header,
1153 GNUNET_CONTAINER_DLL_remove (zi->client->op_head,
1154 zi->client->op_tail,
1161 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1164 * @param client the client sending the message
1165 * @param message message of type 'struct ZoneIterationStartMessage'
1168 handle_iteration_start (void *cls,
1169 struct GNUNET_SERVER_Client *client,
1170 const struct GNUNET_MessageHeader *message)
1172 const struct ZoneIterationStartMessage *zis_msg;
1173 struct NamestoreClient *nc;
1174 struct ZoneIteration *zi;
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1177 if (NULL == (nc = client_lookup (client)))
1180 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1183 zis_msg = (const struct ZoneIterationStartMessage *) message;
1184 zi = GNUNET_new (struct ZoneIteration);
1185 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1188 zi->zone = zis_msg->zone;
1189 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1190 run_zone_iteration_round (zi);
1191 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1196 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1199 * @param client GNUNET_SERVER_Client sending the message
1200 * @param message message of type 'struct ZoneIterationStopMessage'
1203 handle_iteration_stop (void *cls,
1204 struct GNUNET_SERVER_Client *client,
1205 const struct GNUNET_MessageHeader *message)
1207 struct NamestoreClient *nc;
1208 struct ZoneIteration *zi;
1209 const struct ZoneIterationStopMessage *zis_msg;
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213 "Received `%s' message\n",
1214 "ZONE_ITERATION_STOP");
1215 if (NULL == (nc = client_lookup(client)))
1218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1221 zis_msg = (const struct ZoneIterationStopMessage *) message;
1222 rid = ntohl (zis_msg->gns_header.r_id);
1223 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1224 if (zi->request_id == rid)
1229 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1232 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1234 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1239 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1242 * @param client GNUNET_SERVER_Client sending the message
1243 * @param message message of type 'struct ZoneIterationNextMessage'
1246 handle_iteration_next (void *cls,
1247 struct GNUNET_SERVER_Client *client,
1248 const struct GNUNET_MessageHeader *message)
1250 struct NamestoreClient *nc;
1251 struct ZoneIteration *zi;
1252 const struct ZoneIterationNextMessage *zis_msg;
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1256 "Received `%s' message\n",
1257 "ZONE_ITERATION_NEXT");
1258 if (NULL == (nc = client_lookup(client)))
1261 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1264 zis_msg = (const struct ZoneIterationNextMessage *) message;
1265 rid = ntohl (zis_msg->gns_header.r_id);
1266 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1267 if (zi->request_id == rid)
1272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1275 run_zone_iteration_round (zi);
1276 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1281 * Send 'sync' message to zone monitor, we're now in sync.
1283 * @param zm monitor that is now in sync
1286 monitor_sync (struct ZoneMonitor *zm)
1288 struct GNUNET_MessageHeader sync;
1290 sync.size = htons (sizeof (struct GNUNET_MessageHeader));
1291 sync.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1292 GNUNET_SERVER_notification_context_unicast (monitor_nc,
1300 * Obtain the next datum during the zone monitor's zone intiial iteration.
1302 * @param cls zone monitor that does its initial iteration
1303 * @param tc scheduler context
1306 monitor_next (void *cls,
1307 const struct GNUNET_SCHEDULER_TaskContext *tc);
1311 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1313 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1314 * @param zone_key zone key of the zone
1316 * @param rd_count number of records in @a rd
1317 * @param rd array of records
1320 monitor_iterate_cb (void *cls,
1321 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
1323 unsigned int rd_count,
1324 const struct GNUNET_GNSRECORD_Data *rd)
1326 struct ZoneMonitor *zm = cls;
1330 /* finished with iteration */
1334 send_lookup_response (monitor_nc,
1341 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1346 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
1349 * @param client the client sending the message
1350 * @param message message of type 'struct ZoneMonitorStartMessage'
1353 handle_monitor_start (void *cls,
1354 struct GNUNET_SERVER_Client *client,
1355 const struct GNUNET_MessageHeader *message)
1357 const struct ZoneMonitorStartMessage *zis_msg;
1358 struct ZoneMonitor *zm;
1360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1361 "Received `%s' message\n",
1362 "ZONE_MONITOR_START");
1363 zis_msg = (const struct ZoneMonitorStartMessage *) message;
1364 zm = GNUNET_new (struct ZoneMonitor);
1366 zm->nc = client_lookup (client);
1367 zm->zone = zis_msg->zone;
1368 GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, zm);
1369 GNUNET_SERVER_client_mark_monitor (client);
1370 GNUNET_SERVER_disable_receive_done_warning (client);
1371 GNUNET_SERVER_notification_context_add (monitor_nc,
1373 if (GNUNET_YES == ntohl (zis_msg->iterate_first))
1374 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1381 * Obtain the next datum during the zone monitor's zone intiial iteration.
1383 * @param cls zone monitor that does its initial iteration
1384 * @param tc scheduler context
1387 monitor_next (void *cls,
1388 const struct GNUNET_SCHEDULER_TaskContext *tc)
1390 struct ZoneMonitor *zm = cls;
1393 zm->task = GNUNET_SCHEDULER_NO_TASK;
1394 ret = GSN_database->iterate_records (GSN_database->cls,
1395 (0 == memcmp (&zm->zone, &zero, sizeof (zero)))
1399 &monitor_iterate_cb, zm);
1400 if (GNUNET_SYSERR == ret)
1402 GNUNET_SERVER_client_disconnect (zm->nc->client);
1405 if (GNUNET_NO == ret)
1415 * Process namestore requests.
1417 * @param cls closure
1418 * @param server the initialized server
1419 * @param cfg configuration to use
1422 run (void *cls, struct GNUNET_SERVER_Handle *server,
1423 const struct GNUNET_CONFIGURATION_Handle *cfg)
1425 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1426 {&handle_record_store, NULL,
1427 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE, 0},
1428 {&handle_record_lookup, NULL,
1429 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP, 0},
1430 {&handle_zone_to_name, NULL,
1431 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, sizeof (struct ZoneToNameMessage) },
1432 {&handle_iteration_start, NULL,
1433 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage) },
1434 {&handle_iteration_next, NULL,
1435 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, sizeof (struct ZoneIterationNextMessage) },
1436 {&handle_iteration_stop, NULL,
1437 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage) },
1438 {&handle_monitor_start, NULL,
1439 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START, sizeof (struct ZoneMonitorStartMessage) },
1444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1446 monitor_nc = GNUNET_SERVER_notification_context_create (server, 1);
1447 namecache = GNUNET_NAMECACHE_connect (cfg);
1448 /* Loading database plugin */
1450 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1454 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1455 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1456 GNUNET_free (database);
1457 if (NULL == GSN_database)
1459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1460 "Could not load database backend `%s'\n",
1462 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1466 /* Configuring server handles */
1467 GNUNET_SERVER_add_handlers (server, handlers);
1468 snc = GNUNET_SERVER_notification_context_create (server, 16);
1469 GNUNET_SERVER_disconnect_notify (server,
1470 &client_disconnect_notification,
1472 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1478 * The main function for the template service.
1480 * @param argc number of arguments from the command line
1481 * @param argv command line arguments
1482 * @return 0 ok, 1 on error
1485 main (int argc, char *const *argv)
1487 return (GNUNET_OK ==
1488 GNUNET_SERVICE_run (argc, argv, "namestore",
1489 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1492 /* end of gnunet-service-namestore.c */