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_namestore_service.h"
31 #include "gnunet_namestore_plugin.h"
32 #include "gnunet_signatures.h"
33 #include "namestore.h"
35 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
41 struct NamestoreClient;
45 * A namestore iteration operation.
50 * Next element in the DLL
52 struct ZoneIteration *next;
55 * Previous element in the DLL
57 struct ZoneIteration *prev;
60 * Namestore client which intiated this zone iteration
62 struct NamestoreClient *client;
65 * Key of the zone we are iterating over.
67 struct GNUNET_CRYPTO_EccPrivateKey zone;
70 * The operation id fot the zone iteration in the response for the client
75 * Offset of the zone iteration used to address next result of the zone
76 * iteration in the store
78 * Initialy set to 0 in handle_iteration_start
79 * Incremented with by every call to handle_iteration_next
89 struct NamestoreClient
92 * Next element in the DLL
94 struct NamestoreClient *next;
97 * Previous element in the DLL
99 struct NamestoreClient *prev;
104 struct GNUNET_SERVER_Client *client;
108 * Zone iteration operations in progress initiated by this client
110 struct ZoneIteration *op_head;
114 * Zone iteration operations in progress initiated by this client
116 struct ZoneIteration *op_tail;
121 * A namestore monitor.
126 * Next element in the DLL
128 struct ZoneMonitor *next;
131 * Previous element in the DLL
133 struct ZoneMonitor *prev;
136 * Namestore client which intiated this zone monitor
138 struct NamestoreClient *nc;
141 * Private key of the zone.
143 struct GNUNET_CRYPTO_EccPrivateKey zone;
146 * The operation id fot the zone iteration in the response for the client
151 * Task active during initial iteration.
153 GNUNET_SCHEDULER_TaskIdentifier task;
156 * Offset of the zone iteration used to address next result of the zone
157 * iteration in the store
160 * Incremented with by every call to #handle_iteration_next
168 * Configuration handle.
170 static const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
175 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
178 * Name of the database plugin
180 static char *db_lib_name;
183 * Our notification context.
185 static struct GNUNET_SERVER_NotificationContext *snc;
188 * Head of the Client DLL
190 static struct NamestoreClient *client_head;
193 * Tail of the Client DLL
195 static struct NamestoreClient *client_tail;
198 * First active zone monitor.
200 static struct ZoneMonitor *monitor_head;
203 * Last active zone monitor.
205 static struct ZoneMonitor *monitor_tail;
208 * Notification context shared by all monitors.
210 static struct GNUNET_SERVER_NotificationContext *monitor_nc;
215 * Task run during shutdown.
221 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
223 struct ZoneIteration *no;
224 struct NamestoreClient *nc;
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 "Stopping namestore service\n");
230 GNUNET_SERVER_notification_context_destroy (snc);
233 while (NULL != (nc = client_head))
235 while (NULL != (no = nc->op_head))
237 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
240 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
241 GNUNET_SERVER_client_set_user_context (nc->client, NULL);
244 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
245 GNUNET_free (db_lib_name);
247 if (NULL != monitor_nc)
249 GNUNET_SERVER_notification_context_destroy (monitor_nc);
256 * Called whenever a client is disconnected.
257 * Frees our resources associated with that client.
260 * @param client identification of the client
263 client_disconnect_notification (void *cls,
264 struct GNUNET_SERVER_Client *client)
266 struct ZoneIteration *no;
267 struct NamestoreClient *nc;
268 struct ZoneMonitor *zm;
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "Client %p disconnected\n",
275 if (NULL == (nc = GNUNET_SERVER_client_get_user_context (client, struct NamestoreClient)))
277 while (NULL != (no = nc->op_head))
279 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
282 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
284 for (zm = monitor_head; NULL != zm; zm = zm->next)
286 if (client == zm->nc->client)
288 GNUNET_CONTAINER_DLL_remove (monitor_head,
291 if (GNUNET_SCHEDULER_NO_TASK != zm->task)
293 GNUNET_SCHEDULER_cancel (zm->task);
294 zm->task = GNUNET_SCHEDULER_NO_TASK;
304 * Add a client to our list of active clients, if it is not yet
307 * @param client client to add
308 * @return internal namestore client structure for this client
310 static struct NamestoreClient *
311 client_lookup (struct GNUNET_SERVER_Client *client)
313 struct NamestoreClient *nc;
315 nc = GNUNET_SERVER_client_get_user_context (client, struct NamestoreClient);
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "Client %p connected\n",
321 nc = GNUNET_new (struct NamestoreClient);
323 GNUNET_SERVER_notification_context_add (snc, client);
324 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, nc);
325 GNUNET_SERVER_client_set_user_context (client, nc);
331 * Context for name lookups passed from #handle_lookup_block to
332 * #handle_lookup_block_it as closure
334 struct LookupBlockContext
337 * The client to send the response to
339 struct NamestoreClient *nc;
342 * Operation id for the name lookup
350 * A #GNUNET_NAMESTORE_BlockCallback for name lookups in #handle_lookup_block
352 * @param cls a `struct LookupNameContext *` with information about the request
353 * @param block the block
356 handle_lookup_block_it (void *cls,
357 const struct GNUNET_NAMESTORE_Block *block)
359 struct LookupBlockContext *lnc = cls;
360 struct LookupBlockResponseMessage *r;
363 esize = ntohl (block->purpose.size)
364 - sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)
365 - sizeof (struct GNUNET_TIME_AbsoluteNBO);
366 r = GNUNET_malloc (sizeof (struct LookupBlockResponseMessage) + esize);
367 r->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE);
368 r->gns_header.header.size = htons (sizeof (struct LookupBlockResponseMessage) + esize);
369 r->gns_header.r_id = htonl (lnc->request_id);
370 r->expire = block->expiration_time;
371 r->signature = block->signature;
372 r->derived_key = block->derived_key;
373 memcpy (&r[1], &block[1], esize);
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
375 "Sending `%s' message with expiration time %s\n",
376 "NAMESTORE_LOOKUP_BLOCK_RESPONSE",
377 GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (r->expire)));
378 GNUNET_SERVER_notification_context_unicast (snc,
380 &r->gns_header.header,
387 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK message
390 * @param client client sending the message
391 * @param message message of type 'struct LookupNameMessage'
394 handle_lookup_block (void *cls,
395 struct GNUNET_SERVER_Client *client,
396 const struct GNUNET_MessageHeader *message)
398 const struct LookupBlockMessage *ln_msg;
399 struct LookupBlockContext lnc;
400 struct NamestoreClient *nc;
401 struct LookupBlockResponseMessage zir_end;
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 "Received `%s' message\n",
406 "NAMESTORE_LOOKUP_BLOCK");
407 nc = client_lookup(client);
408 ln_msg = (const struct LookupBlockMessage *) message;
409 lnc.request_id = ntohl (ln_msg->gns_header.r_id);
412 (ret = GSN_database->lookup_block (GSN_database->cls,
414 &handle_lookup_block_it, &lnc)))
416 /* internal error (in database plugin); might be best to just hang up on
417 plugin rather than to signal that there are 'no' results, which
418 might also be false... */
420 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
425 /* no records match at all, generate empty response */
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "Sending empty `%s' message\n",
428 "NAMESTORE_LOOKUP_BLOCK_RESPONSE");
429 memset (&zir_end, 0, sizeof (zir_end));
430 zir_end.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE);
431 zir_end.gns_header.header.size = htons (sizeof (struct LookupBlockResponseMessage));
432 zir_end.gns_header.r_id = ln_msg->gns_header.r_id;
433 GNUNET_SERVER_notification_context_unicast (snc,
435 &zir_end.gns_header.header,
439 GNUNET_SERVER_receive_done (client, GNUNET_OK);
444 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE message
447 * @param client client sending the message
448 * @param message message of type 'struct BlockCacheMessage'
451 handle_block_cache (void *cls,
452 struct GNUNET_SERVER_Client *client,
453 const struct GNUNET_MessageHeader *message)
455 struct NamestoreClient *nc;
456 const struct BlockCacheMessage *rp_msg;
457 struct BlockCacheResponseMessage rpr_msg;
458 struct GNUNET_NAMESTORE_Block *block;
462 nc = client_lookup (client);
463 if (ntohs (message->size) < sizeof (struct BlockCacheMessage))
466 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
469 rp_msg = (const struct BlockCacheMessage *) message;
470 esize = ntohs (rp_msg->gns_header.header.size) - sizeof (struct BlockCacheMessage);
471 block = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Block) + esize);
472 block->signature = rp_msg->signature;
473 block->derived_key = rp_msg->derived_key;
474 block->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
475 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
477 block->expiration_time = rp_msg->expire;
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "Received `%s' message with expiration time %s\n",
480 "NAMESTORE_BLOCK_CACHE",
481 GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (block->expiration_time)));
482 memcpy (&block[1], &rp_msg[1], esize);
483 res = GSN_database->cache_block (GSN_database->cls,
487 rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE_RESPONSE);
488 rpr_msg.gns_header.header.size = htons (sizeof (struct BlockCacheResponseMessage));
489 rpr_msg.gns_header.r_id = rp_msg->gns_header.r_id;
490 rpr_msg.op_result = htonl (res);
491 GNUNET_SERVER_notification_context_unicast (snc,
493 &rpr_msg.gns_header.header,
495 GNUNET_SERVER_receive_done (client, GNUNET_OK);
500 * Generate a 'struct LookupNameResponseMessage' and send it to the
501 * given client using the given notification context.
503 * @param nc notification context to use
504 * @param client client to unicast to
505 * @param request_id request ID to use
506 * @param zone_key zone key of the zone
508 * @param rd_count number of records in @a rd
509 * @param rd array of records
512 send_lookup_response (struct GNUNET_SERVER_NotificationContext *nc,
513 struct GNUNET_SERVER_Client *client,
515 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
517 unsigned int rd_count,
518 const struct GNUNET_NAMESTORE_RecordData *rd)
520 struct RecordResultMessage *zir_msg;
527 name_len = strlen (name) + 1;
528 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
529 msg_size = sizeof (struct RecordResultMessage) + name_len + rd_ser_len;
531 zir_msg = GNUNET_malloc (msg_size);
532 zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
533 zir_msg->gns_header.header.size = htons (msg_size);
534 zir_msg->gns_header.r_id = htonl (request_id);
535 zir_msg->name_len = htons (name_len);
536 zir_msg->rd_count = htons (rd_count);
537 zir_msg->rd_len = htons (rd_ser_len);
538 zir_msg->private_key = *zone_key;
539 name_tmp = (char *) &zir_msg[1];
540 memcpy (name_tmp, name, name_len);
541 rd_ser = &name_tmp[name_len];
542 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_ser);
543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
544 "Sending `%s' message with %u records and size %u\n",
548 GNUNET_SERVER_notification_context_unicast (nc,
550 &zir_msg->gns_header.header,
552 GNUNET_free (zir_msg);
557 * We just touched the plaintext information about a name in our zone;
558 * refresh the corresponding (encrypted) block in the namestore.
560 * @param zone_key private key of the zone
561 * @param name label for the records
562 * @param rd_count number of records
563 * @param rd records stored under the given @a name
566 refresh_block (const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
568 unsigned int rd_count,
569 const struct GNUNET_NAMESTORE_RecordData *rd)
571 struct GNUNET_NAMESTORE_Block *block;
574 block = GNUNET_NAMESTORE_block_create (zone_key,
575 GNUNET_TIME_UNIT_ZERO_ABS,
579 block = GNUNET_NAMESTORE_block_create (zone_key,
580 GNUNET_NAMESTORE_record_get_expiration_time (rd_count,
585 GSN_database->cache_block (GSN_database->cls,
588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
589 _("Failed to cache encrypted block of my own zone!\n"));
596 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
599 * @param client client sending the message
600 * @param message message of type 'struct RecordCreateMessage'
603 handle_record_store (void *cls,
604 struct GNUNET_SERVER_Client *client,
605 const struct GNUNET_MessageHeader *message)
607 struct NamestoreClient *nc;
608 const struct RecordStoreMessage *rp_msg;
609 struct RecordStoreResponseMessage rcr_msg;
615 const char *name_tmp;
618 unsigned int rd_count;
620 struct GNUNET_CRYPTO_EccPublicSignKey pubkey;
621 struct ZoneMonitor *zm;
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
624 "Received `%s' message\n",
625 "NAMESTORE_RECORD_STORE");
626 if (ntohs (message->size) < sizeof (struct RecordStoreMessage))
629 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
632 nc = client_lookup (client);
633 rp_msg = (const struct RecordStoreMessage *) message;
634 rid = ntohl (rp_msg->gns_header.r_id);
635 name_len = ntohs (rp_msg->name_len);
636 msg_size = ntohs (message->size);
637 rd_count = ntohs (rp_msg->rd_count);
638 rd_ser_len = ntohs (rp_msg->rd_len);
639 GNUNET_break (0 == ntohs (rp_msg->reserved));
640 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
641 if (msg_size != msg_size_exp)
644 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
647 if ((0 == name_len) || (name_len > MAX_NAME_LEN))
650 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
653 name_tmp = (const char *) &rp_msg[1];
654 rd_ser = &name_tmp[name_len];
655 if ('\0' != name_tmp[name_len -1])
658 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
662 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
665 GNUNET_NAMESTORE_records_deserialize (rd_ser_len, rd_ser, rd_count, rd))
668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
672 /* Extracting and converting private key */
673 GNUNET_CRYPTO_ecc_key_get_public_for_signature (&rp_msg->private_key,
675 conv_name = GNUNET_NAMESTORE_normalize_string (name_tmp);
676 if (NULL == conv_name)
678 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
679 "Error converting name `%s'\n", name_tmp);
680 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684 "Creating %u records for name `%s' in zone `%s'\n",
685 (unsigned int) rd_count,
687 GNUNET_NAMESTORE_z2s (&pubkey));
689 if ( (0 == rd_count) &&
691 GSN_database->iterate_records (GSN_database->cls,
692 &rp_msg->private_key, 0, NULL, 0)) )
694 /* This name does not exist, so cannot be removed */
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
696 "Name `%s' does not exist, no deletion required\n",
702 res = GSN_database->store_records (GSN_database->cls,
703 &rp_msg->private_key,
706 if (GNUNET_OK == res)
708 refresh_block (&rp_msg->private_key,
712 for (zm = monitor_head; NULL != zm; zm = zm->next)
713 if (0 == memcmp (&rp_msg->private_key, &zm->zone,
714 sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
715 send_lookup_response (monitor_nc,
718 &rp_msg->private_key,
722 GNUNET_free (conv_name);
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 "Sending `%s' message\n",
729 "RECORD_STORE_RESPONSE");
730 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
731 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordStoreResponseMessage));
732 rcr_msg.gns_header.r_id = htonl (rid);
733 rcr_msg.op_result = htonl (res);
734 GNUNET_SERVER_notification_context_unicast (snc, nc->client,
735 &rcr_msg.gns_header.header,
737 GNUNET_SERVER_receive_done (client, GNUNET_OK);
742 * Context for record remove operations passed from #handle_zone_to_name to
743 * #handle_zone_to_name_it as closure
750 struct NamestoreClient *nc;
753 * Request id (to be used in the response to the client).
758 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
759 * not finding a name for the zone still counts as a 'success' here,
760 * as this field is about the success of executing the IPC protocol.
767 * Zone to name iterator
769 * @param cls struct ZoneToNameCtx *
770 * @param zone_key the zone key
772 * @param rd_count number of records in @a rd
773 * @param rd record data
776 handle_zone_to_name_it (void *cls,
777 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
779 unsigned int rd_count,
780 const struct GNUNET_NAMESTORE_RecordData *rd)
782 struct ZoneToNameCtx *ztn_ctx = cls;
783 struct ZoneToNameResponseMessage *ztnr_msg;
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
792 "Found result for zone-to-name lookup: `%s'\n",
795 name_len = (NULL == name) ? 0 : strlen (name) + 1;
796 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
797 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
798 if (msg_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
801 ztn_ctx->success = GNUNET_SYSERR;
804 ztnr_msg = GNUNET_malloc (msg_size);
805 ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
806 ztnr_msg->gns_header.header.size = htons (msg_size);
807 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
808 ztnr_msg->res = htons (res);
809 ztnr_msg->rd_len = htons (rd_ser_len);
810 ztnr_msg->rd_count = htons (rd_count);
811 ztnr_msg->name_len = htons (name_len);
812 ztnr_msg->zone = *zone_key;
813 name_tmp = (char *) &ztnr_msg[1];
815 memcpy (name_tmp, name, name_len);
816 rd_tmp = &name_tmp[name_len];
817 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_tmp);
818 ztn_ctx->success = GNUNET_OK;
819 GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client,
820 &ztnr_msg->gns_header.header,
822 GNUNET_free (ztnr_msg);
827 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
830 * @param client client sending the message
831 * @param message message of type 'struct ZoneToNameMessage'
834 handle_zone_to_name (void *cls,
835 struct GNUNET_SERVER_Client *client,
836 const struct GNUNET_MessageHeader *message)
838 struct NamestoreClient *nc;
839 const struct ZoneToNameMessage *ztn_msg;
840 struct ZoneToNameCtx ztn_ctx;
841 struct ZoneToNameResponseMessage ztnr_msg;
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844 "Received `%s' message\n",
846 ztn_msg = (const struct ZoneToNameMessage *) message;
847 nc = client_lookup (client);
848 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
850 ztn_ctx.success = GNUNET_NO;
852 GSN_database->zone_to_name (GSN_database->cls,
854 &ztn_msg->value_zone,
855 &handle_zone_to_name_it, &ztn_ctx))
857 /* internal error, hang up instead of signalling something
858 that might be wrong */
860 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
863 if (GNUNET_NO == ztn_ctx.success)
865 /* no result found, send empty response */
866 memset (&ztnr_msg, 0, sizeof (ztnr_msg));
867 ztnr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
868 ztnr_msg.gns_header.header.size = htons (sizeof (ztnr_msg));
869 ztnr_msg.gns_header.r_id = ztn_msg->gns_header.r_id;
870 ztnr_msg.res = htons (GNUNET_NO);
871 GNUNET_SERVER_notification_context_unicast (snc,
873 &ztnr_msg.gns_header.header,
876 GNUNET_SERVER_receive_done (client, GNUNET_OK);
881 * Zone iteration processor result
883 enum ZoneIterationResult
892 * Continue to iterate with next iteration_next call
894 IT_SUCCESS_MORE_AVAILABLE = 1,
899 IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE = 2
904 * Context for record remove operations passed from
905 * #run_zone_iteration_round to #zone_iteraterate_proc as closure
907 struct ZoneIterationProcResult
910 * The zone iteration handle
912 struct ZoneIteration *zi;
915 * Iteration result: iteration done?
916 * #IT_SUCCESS_MORE_AVAILABLE: if there may be more results overall but
917 * we got one for now and have sent it to the client
918 * #IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE: if there are no further results,
919 * #IT_START: if we are still trying to find a result.
921 int res_iteration_finished;
927 * Process results for zone iteration from database
929 * @param cls struct ZoneIterationProcResult *proc
930 * @param zone_key the zone key
932 * @param rd_count number of records for this name
933 * @param rd record data
936 zone_iteraterate_proc (void *cls,
937 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
939 unsigned int rd_count,
940 const struct GNUNET_NAMESTORE_RecordData *rd)
942 struct ZoneIterationProcResult *proc = cls;
944 int do_refresh_block;
946 if ((NULL == zone_key) && (NULL == name))
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 proc->res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
953 if ((NULL == zone_key) || (NULL == name))
955 /* what is this!? should never happen */
956 proc->res_iteration_finished = IT_START;
960 proc->res_iteration_finished = IT_SUCCESS_MORE_AVAILABLE;
961 send_lookup_response (snc,
962 proc->zi->client->client,
963 proc->zi->request_id,
968 do_refresh_block = GNUNET_NO;
969 for (i=0;i<rd_count;i++)
970 if( (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)) &&
971 (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING)) )
973 do_refresh_block = GNUNET_YES;
976 if (GNUNET_YES == do_refresh_block)
977 refresh_block (zone_key,
986 * Perform the next round of the zone iteration.
988 * @param zi zone iterator to process
991 run_zone_iteration_round (struct ZoneIteration *zi)
993 static struct GNUNET_CRYPTO_EccPrivateKey zero;
994 struct ZoneIterationProcResult proc;
995 struct RecordResultMessage rrm;
998 memset (&proc, 0, sizeof (proc));
1000 proc.res_iteration_finished = IT_START;
1001 while (IT_START == proc.res_iteration_finished)
1003 if (GNUNET_SYSERR ==
1004 (ret = GSN_database->iterate_records (GSN_database->cls,
1005 (0 == memcmp (&zi->zone, &zero, sizeof (zero)))
1009 &zone_iteraterate_proc, &proc)))
1014 if (GNUNET_NO == ret)
1015 proc.res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1018 if (IT_SUCCESS_MORE_AVAILABLE == proc.res_iteration_finished)
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021 "More results available\n");
1022 return; /* more results later */
1024 /* send empty response to indicate end of list */
1025 memset (&rrm, 0, sizeof (rrm));
1026 rrm.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
1027 rrm.gns_header.header.size = htons (sizeof (rrm));
1028 rrm.gns_header.r_id = htonl (zi->request_id);
1029 GNUNET_SERVER_notification_context_unicast (snc,
1031 &rrm.gns_header.header,
1033 GNUNET_CONTAINER_DLL_remove (zi->client->op_head,
1034 zi->client->op_tail,
1041 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1044 * @param client the client sending the message
1045 * @param message message of type 'struct ZoneIterationStartMessage'
1048 handle_iteration_start (void *cls,
1049 struct GNUNET_SERVER_Client *client,
1050 const struct GNUNET_MessageHeader *message)
1052 const struct ZoneIterationStartMessage *zis_msg;
1053 struct NamestoreClient *nc;
1054 struct ZoneIteration *zi;
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1057 if (NULL == (nc = client_lookup (client)))
1060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1063 zis_msg = (const struct ZoneIterationStartMessage *) message;
1064 zi = GNUNET_new (struct ZoneIteration);
1065 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1068 zi->zone = zis_msg->zone;
1069 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1070 run_zone_iteration_round (zi);
1071 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1076 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1079 * @param client GNUNET_SERVER_Client sending the message
1080 * @param message message of type 'struct ZoneIterationStopMessage'
1083 handle_iteration_stop (void *cls,
1084 struct GNUNET_SERVER_Client *client,
1085 const struct GNUNET_MessageHeader *message)
1087 struct NamestoreClient *nc;
1088 struct ZoneIteration *zi;
1089 const struct ZoneIterationStopMessage *zis_msg;
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1093 "Received `%s' message\n",
1094 "ZONE_ITERATION_STOP");
1095 if (NULL == (nc = client_lookup(client)))
1098 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1101 zis_msg = (const struct ZoneIterationStopMessage *) message;
1102 rid = ntohl (zis_msg->gns_header.r_id);
1103 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1104 if (zi->request_id == rid)
1109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1112 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1114 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1119 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1122 * @param client GNUNET_SERVER_Client sending the message
1123 * @param message message of type 'struct ZoneIterationNextMessage'
1126 handle_iteration_next (void *cls,
1127 struct GNUNET_SERVER_Client *client,
1128 const struct GNUNET_MessageHeader *message)
1130 struct NamestoreClient *nc;
1131 struct ZoneIteration *zi;
1132 const struct ZoneIterationNextMessage *zis_msg;
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1136 if (NULL == (nc = client_lookup(client)))
1139 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1142 zis_msg = (const struct ZoneIterationNextMessage *) message;
1143 rid = ntohl (zis_msg->gns_header.r_id);
1144 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1145 if (zi->request_id == rid)
1150 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1153 run_zone_iteration_round (zi);
1154 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1159 * Send 'sync' message to zone monitor, we're now in sync.
1161 * @param zm monitor that is now in sync
1164 monitor_sync (struct ZoneMonitor *zm)
1166 struct GNUNET_MessageHeader sync;
1168 sync.size = htons (sizeof (struct GNUNET_MessageHeader));
1169 sync.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1170 GNUNET_SERVER_notification_context_unicast (monitor_nc,
1178 * Obtain the next datum during the zone monitor's zone intiial iteration.
1180 * @param cls zone monitor that does its initial iteration
1181 * @param tc scheduler context
1184 monitor_next (void *cls,
1185 const struct GNUNET_SCHEDULER_TaskContext *tc);
1189 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1191 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1192 * @param zone_key zone key of the zone
1194 * @param rd_count number of records in @a rd
1195 * @param rd array of records
1198 monitor_iterate_cb (void *cls,
1199 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
1201 unsigned int rd_count,
1202 const struct GNUNET_NAMESTORE_RecordData *rd)
1204 struct ZoneMonitor *zm = cls;
1208 /* finished with iteration */
1212 send_lookup_response (monitor_nc,
1219 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1224 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
1227 * @param client GNUNET_SERVER_Client sending the message
1228 * @param message message of type 'struct ZoneMonitorStartMessage'
1231 handle_monitor_start (void *cls,
1232 struct GNUNET_SERVER_Client *client,
1233 const struct GNUNET_MessageHeader *message)
1235 const struct ZoneMonitorStartMessage *zis_msg;
1236 struct ZoneMonitor *zm;
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1239 "Received `%s' message\n",
1240 "ZONE_MONITOR_START");
1241 zis_msg = (const struct ZoneMonitorStartMessage *) message;
1242 zm = GNUNET_new (struct ZoneMonitor);
1243 zm->request_id = ntohl (zis_msg->gns_header.r_id);
1245 zm->nc = client_lookup (client);
1246 zm->zone = zis_msg->zone;
1247 GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, zm);
1248 GNUNET_SERVER_client_mark_monitor (client);
1249 GNUNET_SERVER_disable_receive_done_warning (client);
1250 GNUNET_SERVER_notification_context_add (monitor_nc,
1252 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1257 * Obtain the next datum during the zone monitor's zone intiial iteration.
1259 * @param cls zone monitor that does its initial iteration
1260 * @param tc scheduler context
1263 monitor_next (void *cls,
1264 const struct GNUNET_SCHEDULER_TaskContext *tc)
1266 struct ZoneMonitor *zm = cls;
1269 zm->task = GNUNET_SCHEDULER_NO_TASK;
1270 ret = GSN_database->iterate_records (GSN_database->cls,
1273 &monitor_iterate_cb, zm);
1274 if (GNUNET_SYSERR == ret)
1276 GNUNET_SERVER_client_disconnect (zm->nc->client);
1279 if (GNUNET_NO == ret)
1289 * Process namestore requests.
1291 * @param cls closure
1292 * @param server the initialized server
1293 * @param cfg configuration to use
1296 run (void *cls, struct GNUNET_SERVER_Handle *server,
1297 const struct GNUNET_CONFIGURATION_Handle *cfg)
1299 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1300 {&handle_lookup_block, NULL,
1301 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK, sizeof (struct LookupBlockMessage)},
1302 {&handle_block_cache, NULL,
1303 GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE, 0},
1304 {&handle_record_store, NULL,
1305 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE, 0},
1306 {&handle_zone_to_name, NULL,
1307 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, sizeof (struct ZoneToNameMessage) },
1308 {&handle_iteration_start, NULL,
1309 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage) },
1310 {&handle_iteration_next, NULL,
1311 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, sizeof (struct ZoneIterationNextMessage) },
1312 {&handle_iteration_stop, NULL,
1313 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage) },
1314 {&handle_monitor_start, NULL,
1315 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START, sizeof (struct ZoneMonitorStartMessage) },
1320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1322 monitor_nc = GNUNET_SERVER_notification_context_create (server, 1);
1324 /* Loading database plugin */
1326 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1330 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1331 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1332 GNUNET_free (database);
1333 if (NULL == GSN_database)
1335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1336 "Could not load database backend `%s'\n",
1338 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1342 /* Configuring server handles */
1343 GNUNET_SERVER_add_handlers (server, handlers);
1344 snc = GNUNET_SERVER_notification_context_create (server, 16);
1345 GNUNET_SERVER_disconnect_notify (server,
1346 &client_disconnect_notification,
1348 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1354 * The main function for the template service.
1356 * @param argc number of arguments from the command line
1357 * @param argv command line arguments
1358 * @return 0 ok, 1 on error
1361 main (int argc, char *const *argv)
1363 return (GNUNET_OK ==
1364 GNUNET_SERVICE_run (argc, argv, "namestore",
1365 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1368 /* end of gnunet-service-namestore.c */