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\n",
376 "NAMESTORE_LOOKUP_BLOCK_RESPONSE");
377 GNUNET_SERVER_notification_context_unicast (snc,
379 &r->gns_header.header,
386 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK message
389 * @param client client sending the message
390 * @param message message of type 'struct LookupNameMessage'
393 handle_lookup_block (void *cls,
394 struct GNUNET_SERVER_Client *client,
395 const struct GNUNET_MessageHeader *message)
397 const struct LookupBlockMessage *ln_msg;
398 struct LookupBlockContext lnc;
399 struct NamestoreClient *nc;
400 struct LookupBlockResponseMessage zir_end;
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "Received `%s' message\n",
405 "NAMESTORE_LOOKUP_BLOCK");
406 nc = client_lookup(client);
407 ln_msg = (const struct LookupBlockMessage *) message;
408 lnc.request_id = ntohl (ln_msg->gns_header.r_id);
411 (ret = GSN_database->lookup_block (GSN_database->cls,
413 &handle_lookup_block_it, &lnc)))
415 /* internal error (in database plugin); might be best to just hang up on
416 plugin rather than to signal that there are 'no' results, which
417 might also be false... */
419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
424 /* no records match at all, generate empty response */
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "Sending empty `%s' message\n",
427 "NAMESTORE_LOOKUP_BLOCK_RESPONSE");
428 memset (&zir_end, 0, sizeof (zir_end));
429 zir_end.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE);
430 zir_end.gns_header.header.size = htons (sizeof (struct LookupBlockResponseMessage));
431 zir_end.gns_header.r_id = ln_msg->gns_header.r_id;
432 GNUNET_SERVER_notification_context_unicast (snc,
434 &zir_end.gns_header.header,
438 GNUNET_SERVER_receive_done (client, GNUNET_OK);
443 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE message
446 * @param client client sending the message
447 * @param message message of type 'struct BlockCacheMessage'
450 handle_block_cache (void *cls,
451 struct GNUNET_SERVER_Client *client,
452 const struct GNUNET_MessageHeader *message)
454 struct NamestoreClient *nc;
455 const struct BlockCacheMessage *rp_msg;
456 struct BlockCacheResponseMessage rpr_msg;
457 struct GNUNET_NAMESTORE_Block *block;
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 "Received `%s' message\n",
463 "NAMESTORE_BLOCK_CACHE");
464 nc = client_lookup (client);
465 if (ntohs (message->size) < sizeof (struct BlockCacheMessage))
468 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
471 rp_msg = (const struct BlockCacheMessage *) message;
472 esize = ntohs (rp_msg->gns_header.header.size) - sizeof (struct BlockCacheMessage);
473 block = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Block) + esize);
474 block->signature = rp_msg->signature;
475 block->derived_key = rp_msg->derived_key;
476 block->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
477 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
479 block->expiration_time = rp_msg->expire;
480 memcpy (&block[1], &rp_msg[1], esize);
481 res = GSN_database->cache_block (GSN_database->cls,
485 rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE_RESPONSE);
486 rpr_msg.gns_header.header.size = htons (sizeof (struct BlockCacheResponseMessage));
487 rpr_msg.gns_header.r_id = rp_msg->gns_header.r_id;
488 rpr_msg.op_result = htonl (res);
489 GNUNET_SERVER_notification_context_unicast (snc,
491 &rpr_msg.gns_header.header,
493 GNUNET_SERVER_receive_done (client, GNUNET_OK);
498 * Generate a 'struct LookupNameResponseMessage' and send it to the
499 * given client using the given notification context.
501 * @param nc notification context to use
502 * @param client client to unicast to
503 * @param request_id request ID to use
504 * @param zone_key zone key of the zone
506 * @param rd_count number of records in @a rd
507 * @param rd array of records
510 send_lookup_response (struct GNUNET_SERVER_NotificationContext *nc,
511 struct GNUNET_SERVER_Client *client,
513 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
515 unsigned int rd_count,
516 const struct GNUNET_NAMESTORE_RecordData *rd)
518 struct RecordResultMessage *zir_msg;
525 name_len = strlen (name) + 1;
526 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
527 msg_size = sizeof (struct RecordResultMessage) + name_len + rd_ser_len;
529 zir_msg = GNUNET_malloc (msg_size);
530 zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
531 zir_msg->gns_header.header.size = htons (msg_size);
532 zir_msg->gns_header.r_id = htonl (request_id);
533 zir_msg->name_len = htons (name_len);
534 zir_msg->rd_count = htons (rd_count);
535 zir_msg->rd_len = htons (rd_ser_len);
536 zir_msg->private_key = *zone_key;
537 name_tmp = (char *) &zir_msg[1];
538 memcpy (name_tmp, name, name_len);
539 rd_ser = &name_tmp[name_len];
540 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_ser);
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "Sending `%s' message with %u records and size %u\n",
546 GNUNET_SERVER_notification_context_unicast (nc,
548 &zir_msg->gns_header.header,
550 GNUNET_free (zir_msg);
555 * We just touched the plaintext information about a name in our zone;
556 * refresh the corresponding (encrypted) block in the namestore.
558 * @param zone_key private key of the zone
559 * @param name label for the records
560 * @param rd_count number of records
561 * @param rd records stored under the given @a name
564 refresh_block (const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
566 unsigned int rd_count,
567 const struct GNUNET_NAMESTORE_RecordData *rd)
569 struct GNUNET_NAMESTORE_Block *block;
572 block = GNUNET_NAMESTORE_block_create (zone_key,
573 GNUNET_TIME_UNIT_ZERO_ABS,
577 block = GNUNET_NAMESTORE_block_create (zone_key,
578 GNUNET_NAMESTORE_record_get_expiration_time (rd_count,
583 GSN_database->cache_block (GSN_database->cls,
586 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
587 _("Failed to cache encrypted block of my own zone!\n"));
594 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
597 * @param client client sending the message
598 * @param message message of type 'struct RecordCreateMessage'
601 handle_record_store (void *cls,
602 struct GNUNET_SERVER_Client *client,
603 const struct GNUNET_MessageHeader *message)
605 struct NamestoreClient *nc;
606 const struct RecordStoreMessage *rp_msg;
607 struct RecordStoreResponseMessage rcr_msg;
613 const char *name_tmp;
616 unsigned int rd_count;
618 struct GNUNET_CRYPTO_EccPublicSignKey pubkey;
619 struct ZoneMonitor *zm;
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
622 "Received `%s' message\n",
623 "NAMESTORE_RECORD_STORE");
624 if (ntohs (message->size) < sizeof (struct RecordStoreMessage))
627 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
630 nc = client_lookup (client);
631 rp_msg = (const struct RecordStoreMessage *) message;
632 rid = ntohl (rp_msg->gns_header.r_id);
633 name_len = ntohs (rp_msg->name_len);
634 msg_size = ntohs (message->size);
635 rd_count = ntohs (rp_msg->rd_count);
636 rd_ser_len = ntohs (rp_msg->rd_len);
637 GNUNET_break (0 == ntohs (rp_msg->reserved));
638 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
639 if (msg_size != msg_size_exp)
642 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
645 if ((0 == name_len) || (name_len > MAX_NAME_LEN))
648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
651 name_tmp = (const char *) &rp_msg[1];
652 rd_ser = &name_tmp[name_len];
653 if ('\0' != name_tmp[name_len -1])
656 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
660 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
663 GNUNET_NAMESTORE_records_deserialize (rd_ser_len, rd_ser, rd_count, rd))
666 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
670 /* Extracting and converting private key */
671 GNUNET_CRYPTO_ecc_key_get_public_for_signature (&rp_msg->private_key,
673 conv_name = GNUNET_NAMESTORE_normalize_string (name_tmp);
674 if (NULL == conv_name)
676 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
677 "Error converting name `%s'\n", name_tmp);
678 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682 "Creating %u records for name `%s' in zone `%s'\n",
683 (unsigned int) rd_count,
685 GNUNET_NAMESTORE_z2s (&pubkey));
687 if ( (0 == rd_count) &&
689 GSN_database->iterate_records (GSN_database->cls,
690 &rp_msg->private_key, 0, NULL, 0)) )
692 /* This name does not exist, so cannot be removed */
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694 "Name `%s' does not exist, no deletion required\n",
700 res = GSN_database->store_records (GSN_database->cls,
701 &rp_msg->private_key,
704 if (GNUNET_OK == res)
706 refresh_block (&rp_msg->private_key,
710 for (zm = monitor_head; NULL != zm; zm = zm->next)
711 if (0 == memcmp (&rp_msg->private_key, &zm->zone,
712 sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
713 send_lookup_response (monitor_nc,
716 &rp_msg->private_key,
720 GNUNET_free (conv_name);
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "Sending `%s' message\n",
727 "RECORD_STORE_RESPONSE");
728 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
729 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordStoreResponseMessage));
730 rcr_msg.gns_header.r_id = htonl (rid);
731 rcr_msg.op_result = htonl (res);
732 GNUNET_SERVER_notification_context_unicast (snc, nc->client,
733 &rcr_msg.gns_header.header,
735 GNUNET_SERVER_receive_done (client, GNUNET_OK);
740 * Context for record remove operations passed from #handle_zone_to_name to
741 * #handle_zone_to_name_it as closure
748 struct NamestoreClient *nc;
751 * Request id (to be used in the response to the client).
756 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
757 * not finding a name for the zone still counts as a 'success' here,
758 * as this field is about the success of executing the IPC protocol.
765 * Zone to name iterator
767 * @param cls struct ZoneToNameCtx *
768 * @param zone_key the zone key
770 * @param rd_count number of records in @a rd
771 * @param rd record data
774 handle_zone_to_name_it (void *cls,
775 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
777 unsigned int rd_count,
778 const struct GNUNET_NAMESTORE_RecordData *rd)
780 struct ZoneToNameCtx *ztn_ctx = cls;
781 struct ZoneToNameResponseMessage *ztnr_msg;
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 "Found result for zone-to-name lookup: `%s'\n",
793 name_len = (NULL == name) ? 0 : strlen (name) + 1;
794 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
795 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
796 if (msg_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
799 ztn_ctx->success = GNUNET_SYSERR;
802 ztnr_msg = GNUNET_malloc (msg_size);
803 ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
804 ztnr_msg->gns_header.header.size = htons (msg_size);
805 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
806 ztnr_msg->res = htons (res);
807 ztnr_msg->rd_len = htons (rd_ser_len);
808 ztnr_msg->rd_count = htons (rd_count);
809 ztnr_msg->name_len = htons (name_len);
810 ztnr_msg->zone = *zone_key;
811 name_tmp = (char *) &ztnr_msg[1];
813 memcpy (name_tmp, name, name_len);
814 rd_tmp = &name_tmp[name_len];
815 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_tmp);
816 ztn_ctx->success = GNUNET_OK;
817 GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client,
818 &ztnr_msg->gns_header.header,
820 GNUNET_free (ztnr_msg);
825 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
828 * @param client client sending the message
829 * @param message message of type 'struct ZoneToNameMessage'
832 handle_zone_to_name (void *cls,
833 struct GNUNET_SERVER_Client *client,
834 const struct GNUNET_MessageHeader *message)
836 struct NamestoreClient *nc;
837 const struct ZoneToNameMessage *ztn_msg;
838 struct ZoneToNameCtx ztn_ctx;
839 struct ZoneToNameResponseMessage ztnr_msg;
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 "Received `%s' message\n",
844 ztn_msg = (const struct ZoneToNameMessage *) message;
845 nc = client_lookup (client);
846 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
848 ztn_ctx.success = GNUNET_NO;
850 GSN_database->zone_to_name (GSN_database->cls,
852 &ztn_msg->value_zone,
853 &handle_zone_to_name_it, &ztn_ctx))
855 /* internal error, hang up instead of signalling something
856 that might be wrong */
858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
861 if (GNUNET_NO == ztn_ctx.success)
863 /* no result found, send empty response */
864 memset (&ztnr_msg, 0, sizeof (ztnr_msg));
865 ztnr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
866 ztnr_msg.gns_header.header.size = htons (sizeof (ztnr_msg));
867 ztnr_msg.gns_header.r_id = ztn_msg->gns_header.r_id;
868 ztnr_msg.res = htons (GNUNET_NO);
869 GNUNET_SERVER_notification_context_unicast (snc,
871 &ztnr_msg.gns_header.header,
874 GNUNET_SERVER_receive_done (client, GNUNET_OK);
879 * Zone iteration processor result
881 enum ZoneIterationResult
890 * Continue to iterate with next iteration_next call
892 IT_SUCCESS_MORE_AVAILABLE = 1,
897 IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE = 2
902 * Context for record remove operations passed from
903 * #run_zone_iteration_round to #zone_iteraterate_proc as closure
905 struct ZoneIterationProcResult
908 * The zone iteration handle
910 struct ZoneIteration *zi;
913 * Iteration result: iteration done?
914 * #IT_SUCCESS_MORE_AVAILABLE: if there may be more results overall but
915 * we got one for now and have sent it to the client
916 * #IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE: if there are no further results,
917 * #IT_START: if we are still trying to find a result.
919 int res_iteration_finished;
925 * Process results for zone iteration from database
927 * @param cls struct ZoneIterationProcResult *proc
928 * @param zone_key the zone key
930 * @param rd_count number of records for this name
931 * @param rd record data
934 zone_iteraterate_proc (void *cls,
935 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
937 unsigned int rd_count,
938 const struct GNUNET_NAMESTORE_RecordData *rd)
940 struct ZoneIterationProcResult *proc = cls;
942 int do_refresh_block;
944 if ((NULL == zone_key) && (NULL == name))
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 proc->res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
951 if ((NULL == zone_key) || (NULL == name))
953 /* what is this!? should never happen */
954 proc->res_iteration_finished = IT_START;
958 proc->res_iteration_finished = IT_SUCCESS_MORE_AVAILABLE;
959 send_lookup_response (snc,
960 proc->zi->client->client,
961 proc->zi->request_id,
966 do_refresh_block = GNUNET_NO;
967 for (i=0;i<rd_count;i++)
968 if( (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION)) &&
969 (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_PENDING)) )
971 do_refresh_block = GNUNET_YES;
974 if (GNUNET_YES == do_refresh_block)
975 refresh_block (zone_key,
984 * Perform the next round of the zone iteration.
986 * @param zi zone iterator to process
989 run_zone_iteration_round (struct ZoneIteration *zi)
991 static struct GNUNET_CRYPTO_EccPrivateKey zero;
992 struct ZoneIterationProcResult proc;
993 struct RecordResultMessage rrm;
996 memset (&proc, 0, sizeof (proc));
998 proc.res_iteration_finished = IT_START;
999 while (IT_START == proc.res_iteration_finished)
1001 if (GNUNET_SYSERR ==
1002 (ret = GSN_database->iterate_records (GSN_database->cls,
1003 (0 == memcmp (&zi->zone, &zero, sizeof (zero)))
1007 &zone_iteraterate_proc, &proc)))
1012 if (GNUNET_NO == ret)
1013 proc.res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
1016 if (IT_SUCCESS_MORE_AVAILABLE == proc.res_iteration_finished)
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 "More results available\n");
1020 return; /* more results later */
1022 /* send empty response to indicate end of list */
1023 memset (&rrm, 0, sizeof (rrm));
1024 rrm.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
1025 rrm.gns_header.header.size = htons (sizeof (rrm));
1026 rrm.gns_header.r_id = htonl (zi->request_id);
1027 GNUNET_SERVER_notification_context_unicast (snc,
1029 &rrm.gns_header.header,
1031 GNUNET_CONTAINER_DLL_remove (zi->client->op_head,
1032 zi->client->op_tail,
1039 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
1042 * @param client the client sending the message
1043 * @param message message of type 'struct ZoneIterationStartMessage'
1046 handle_iteration_start (void *cls,
1047 struct GNUNET_SERVER_Client *client,
1048 const struct GNUNET_MessageHeader *message)
1050 const struct ZoneIterationStartMessage *zis_msg;
1051 struct NamestoreClient *nc;
1052 struct ZoneIteration *zi;
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1055 if (NULL == (nc = client_lookup (client)))
1058 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1061 zis_msg = (const struct ZoneIterationStartMessage *) message;
1062 zi = GNUNET_new (struct ZoneIteration);
1063 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1066 zi->zone = zis_msg->zone;
1067 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1068 run_zone_iteration_round (zi);
1069 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1074 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1077 * @param client GNUNET_SERVER_Client sending the message
1078 * @param message message of type 'struct ZoneIterationStopMessage'
1081 handle_iteration_stop (void *cls,
1082 struct GNUNET_SERVER_Client *client,
1083 const struct GNUNET_MessageHeader *message)
1085 struct NamestoreClient *nc;
1086 struct ZoneIteration *zi;
1087 const struct ZoneIterationStopMessage *zis_msg;
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1091 "Received `%s' message\n",
1092 "ZONE_ITERATION_STOP");
1093 if (NULL == (nc = client_lookup(client)))
1096 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1099 zis_msg = (const struct ZoneIterationStopMessage *) message;
1100 rid = ntohl (zis_msg->gns_header.r_id);
1101 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1102 if (zi->request_id == rid)
1107 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1110 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1112 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1117 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1120 * @param client GNUNET_SERVER_Client sending the message
1121 * @param message message of type 'struct ZoneIterationNextMessage'
1124 handle_iteration_next (void *cls,
1125 struct GNUNET_SERVER_Client *client,
1126 const struct GNUNET_MessageHeader *message)
1128 struct NamestoreClient *nc;
1129 struct ZoneIteration *zi;
1130 const struct ZoneIterationNextMessage *zis_msg;
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1134 if (NULL == (nc = client_lookup(client)))
1137 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1140 zis_msg = (const struct ZoneIterationNextMessage *) message;
1141 rid = ntohl (zis_msg->gns_header.r_id);
1142 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1143 if (zi->request_id == rid)
1148 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1151 run_zone_iteration_round (zi);
1152 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1157 * Send 'sync' message to zone monitor, we're now in sync.
1159 * @param zm monitor that is now in sync
1162 monitor_sync (struct ZoneMonitor *zm)
1164 struct GNUNET_MessageHeader sync;
1166 sync.size = htons (sizeof (struct GNUNET_MessageHeader));
1167 sync.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1168 GNUNET_SERVER_notification_context_unicast (monitor_nc,
1176 * Obtain the next datum during the zone monitor's zone intiial iteration.
1178 * @param cls zone monitor that does its initial iteration
1179 * @param tc scheduler context
1182 monitor_next (void *cls,
1183 const struct GNUNET_SCHEDULER_TaskContext *tc);
1187 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1189 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1190 * @param zone_key zone key of the zone
1192 * @param rd_count number of records in @a rd
1193 * @param rd array of records
1196 monitor_iterate_cb (void *cls,
1197 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
1199 unsigned int rd_count,
1200 const struct GNUNET_NAMESTORE_RecordData *rd)
1202 struct ZoneMonitor *zm = cls;
1206 /* finished with iteration */
1210 send_lookup_response (monitor_nc,
1217 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1222 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START message
1225 * @param client GNUNET_SERVER_Client sending the message
1226 * @param message message of type 'struct ZoneMonitorStartMessage'
1229 handle_monitor_start (void *cls,
1230 struct GNUNET_SERVER_Client *client,
1231 const struct GNUNET_MessageHeader *message)
1233 const struct ZoneMonitorStartMessage *zis_msg;
1234 struct ZoneMonitor *zm;
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 "Received `%s' message\n",
1238 "ZONE_MONITOR_START");
1239 zis_msg = (const struct ZoneMonitorStartMessage *) message;
1240 zm = GNUNET_new (struct ZoneMonitor);
1241 zm->request_id = ntohl (zis_msg->gns_header.r_id);
1243 zm->nc = client_lookup (client);
1244 zm->zone = zis_msg->zone;
1245 GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, zm);
1246 GNUNET_SERVER_client_mark_monitor (client);
1247 GNUNET_SERVER_disable_receive_done_warning (client);
1248 GNUNET_SERVER_notification_context_add (monitor_nc,
1250 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1255 * Obtain the next datum during the zone monitor's zone intiial iteration.
1257 * @param cls zone monitor that does its initial iteration
1258 * @param tc scheduler context
1261 monitor_next (void *cls,
1262 const struct GNUNET_SCHEDULER_TaskContext *tc)
1264 struct ZoneMonitor *zm = cls;
1267 zm->task = GNUNET_SCHEDULER_NO_TASK;
1268 ret = GSN_database->iterate_records (GSN_database->cls,
1271 &monitor_iterate_cb, zm);
1272 if (GNUNET_SYSERR == ret)
1274 GNUNET_SERVER_client_disconnect (zm->nc->client);
1277 if (GNUNET_NO == ret)
1287 * Process namestore requests.
1289 * @param cls closure
1290 * @param server the initialized server
1291 * @param cfg configuration to use
1294 run (void *cls, struct GNUNET_SERVER_Handle *server,
1295 const struct GNUNET_CONFIGURATION_Handle *cfg)
1297 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1298 {&handle_lookup_block, NULL,
1299 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK, sizeof (struct LookupBlockMessage)},
1300 {&handle_block_cache, NULL,
1301 GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE, 0},
1302 {&handle_record_store, NULL,
1303 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE, 0},
1304 {&handle_zone_to_name, NULL,
1305 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, sizeof (struct ZoneToNameMessage) },
1306 {&handle_iteration_start, NULL,
1307 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage) },
1308 {&handle_iteration_next, NULL,
1309 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, sizeof (struct ZoneIterationNextMessage) },
1310 {&handle_iteration_stop, NULL,
1311 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage) },
1312 {&handle_monitor_start, NULL,
1313 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START, sizeof (struct ZoneMonitorStartMessage) },
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1320 monitor_nc = GNUNET_SERVER_notification_context_create (server, 1);
1322 /* Loading database plugin */
1324 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1328 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1329 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1330 GNUNET_free (database);
1331 if (NULL == GSN_database)
1333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1334 "Could not load database backend `%s'\n",
1336 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1340 /* Configuring server handles */
1341 GNUNET_SERVER_add_handlers (server, handlers);
1342 snc = GNUNET_SERVER_notification_context_create (server, 16);
1343 GNUNET_SERVER_disconnect_notify (server,
1344 &client_disconnect_notification,
1346 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1352 * The main function for the template service.
1354 * @param argc number of arguments from the command line
1355 * @param argv command line arguments
1356 * @return 0 ok, 1 on error
1359 main (int argc, char *const *argv)
1361 return (GNUNET_OK ==
1362 GNUNET_SERVICE_run (argc, argv, "namestore",
1363 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1366 /* end of gnunet-service-namestore.c */