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 memset (&zir_end, 0, sizeof (zir_end));
426 zir_end.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE);
427 zir_end.gns_header.header.size = htons (sizeof (struct LookupBlockResponseMessage));
428 zir_end.gns_header.r_id = ln_msg->gns_header.r_id;
429 GNUNET_SERVER_notification_context_unicast (snc,
431 &zir_end.gns_header.header,
435 GNUNET_SERVER_receive_done (client, GNUNET_OK);
440 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE message
443 * @param client GNUNET_SERVER_Client sending the message
444 * @param message message of type 'struct BlockCacheMessage'
447 handle_block_cache (void *cls,
448 struct GNUNET_SERVER_Client *client,
449 const struct GNUNET_MessageHeader *message)
451 struct NamestoreClient *nc;
452 const struct BlockCacheMessage *rp_msg;
453 struct BlockCacheResponseMessage rpr_msg;
454 struct GNUNET_NAMESTORE_Block *block;
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "Received `%s' message\n",
460 "NAMESTORE_BLOCK_CACHE");
461 nc = client_lookup (client);
462 if (ntohs (message->size) < sizeof (struct BlockCacheMessage))
465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
468 rp_msg = (const struct BlockCacheMessage *) message;
469 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 memcpy (&block[1], &rp_msg[1], esize);
479 res = GSN_database->cache_block (GSN_database->cls,
483 rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE_RESPONSE);
484 rpr_msg.gns_header.header.size = htons (sizeof (struct BlockCacheResponseMessage));
485 rpr_msg.gns_header.r_id = rp_msg->gns_header.r_id;
486 rpr_msg.op_result = htonl (res);
487 GNUNET_SERVER_notification_context_unicast (snc,
489 &rpr_msg.gns_header.header,
491 GNUNET_SERVER_receive_done (client, GNUNET_OK);
496 * Generate a 'struct LookupNameResponseMessage' and send it to the
497 * given client using the given notification context.
499 * @param nc notification context to use
500 * @param client client to unicast to
501 * @param request_id request ID to use
502 * @param zone_key zone key of the zone
504 * @param rd_count number of records in @a rd
505 * @param rd array of records
508 send_lookup_response (struct GNUNET_SERVER_NotificationContext *nc,
509 struct GNUNET_SERVER_Client *client,
511 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
513 unsigned int rd_count,
514 const struct GNUNET_NAMESTORE_RecordData *rd)
516 struct RecordResultMessage *zir_msg;
523 name_len = strlen (name) + 1;
524 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
525 msg_size = sizeof (struct RecordResultMessage) + name_len + rd_ser_len;
527 zir_msg = GNUNET_malloc (msg_size);
528 zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
529 zir_msg->gns_header.header.size = htons (msg_size);
530 zir_msg->gns_header.r_id = htonl (request_id);
531 zir_msg->name_len = htons (name_len);
532 zir_msg->rd_count = htons (rd_count);
533 zir_msg->rd_len = htons (rd_ser_len);
534 zir_msg->private_key = *zone_key;
535 name_tmp = (char *) &zir_msg[1];
536 memcpy (name_tmp, name, name_len);
537 rd_ser = &name_tmp[name_len];
538 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_ser);
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
540 "Sending `%s' message with size %u\n",
543 GNUNET_SERVER_notification_context_unicast (nc,
545 &zir_msg->gns_header.header,
547 GNUNET_free (zir_msg);
552 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
555 * @param client client sending the message
556 * @param message message of type 'struct RecordCreateMessage'
559 handle_record_store (void *cls,
560 struct GNUNET_SERVER_Client *client,
561 const struct GNUNET_MessageHeader *message)
563 struct NamestoreClient *nc;
564 const struct RecordStoreMessage *rp_msg;
565 struct RecordStoreResponseMessage rcr_msg;
571 const char *name_tmp;
574 unsigned int rd_count;
576 struct GNUNET_CRYPTO_EccPublicSignKey pubkey;
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
579 "Received `%s' message\n",
580 "NAMESTORE_RECORD_STORE");
581 if (ntohs (message->size) < sizeof (struct RecordStoreMessage))
584 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
587 nc = client_lookup (client);
588 rp_msg = (const struct RecordStoreMessage *) message;
589 rid = ntohl (rp_msg->gns_header.r_id);
590 name_len = ntohs (rp_msg->name_len);
591 msg_size = ntohs (message->size);
592 rd_count = ntohs (rp_msg->rd_count);
593 rd_ser_len = ntohs (rp_msg->rd_len);
594 GNUNET_break (0 == ntohs (rp_msg->reserved));
595 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
596 if (msg_size != msg_size_exp)
599 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
602 if ((0 == name_len) || (name_len > MAX_NAME_LEN))
605 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
608 name_tmp = (const char *) &rp_msg[1];
609 rd_ser = &name_tmp[name_len];
610 if ('\0' != name_tmp[name_len -1])
613 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
617 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
620 GNUNET_NAMESTORE_records_deserialize (rd_ser_len, rd_ser, rd_count, rd))
623 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
627 /* Extracting and converting private key */
628 GNUNET_CRYPTO_ecc_key_get_public_for_signature (&rp_msg->private_key,
630 conv_name = GNUNET_NAMESTORE_normalize_string (name_tmp);
631 if (NULL == conv_name)
633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
634 "Error converting name `%s'\n", name_tmp);
635 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639 "Creating %u records for name `%s' in zone `%s'\n",
640 (unsigned int) rd_count,
642 GNUNET_NAMESTORE_z2s (&pubkey));
644 if ((rd_count == 0) && (GNUNET_NO == GSN_database->iterate_records (GSN_database->cls, &rp_msg->private_key, 0, NULL, 0)))
646 /* This name does not exist, so cannot be removed */
651 res = GSN_database->store_records (GSN_database->cls,
652 &rp_msg->private_key,
655 if (GNUNET_OK == res)
657 struct ZoneMonitor *zm;
658 struct GNUNET_NAMESTORE_Block *block;
661 block = GNUNET_NAMESTORE_block_create (&rp_msg->private_key,
662 GNUNET_TIME_UNIT_ZERO_ABS,
666 block = GNUNET_NAMESTORE_block_create (&rp_msg->private_key,
667 GNUNET_TIME_UNIT_FOREVER_ABS,
671 GSN_database->cache_block (GSN_database->cls,
674 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
675 _("Failed to cache encrypted block of my own zone!\n"));
680 for (zm = monitor_head; NULL != zm; zm = zm->next)
681 if (0 == memcmp (&rp_msg->private_key, &zm->zone,
682 sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
683 send_lookup_response (monitor_nc,
686 &rp_msg->private_key,
690 GNUNET_free (conv_name);
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
696 "Sending `%s' message\n",
697 "RECORD_STORE_RESPONSE");
698 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
699 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordStoreResponseMessage));
700 rcr_msg.gns_header.r_id = htonl (rid);
701 rcr_msg.op_result = htonl (res);
702 GNUNET_SERVER_notification_context_unicast (snc, nc->client,
703 &rcr_msg.gns_header.header,
705 GNUNET_SERVER_receive_done (client, GNUNET_OK);
710 * Context for record remove operations passed from #handle_zone_to_name to
711 * #handle_zone_to_name_it as closure
718 struct NamestoreClient *nc;
721 * Request id (to be used in the response to the client).
726 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
727 * not finding a name for the zone still counts as a 'success' here,
728 * as this field is about the success of executing the IPC protocol.
735 * Zone to name iterator
737 * @param cls struct ZoneToNameCtx *
738 * @param zone_key the zone key
740 * @param rd_count number of records in @a rd
741 * @param rd record data
744 handle_zone_to_name_it (void *cls,
745 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
747 unsigned int rd_count,
748 const struct GNUNET_NAMESTORE_RecordData *rd)
750 struct ZoneToNameCtx *ztn_ctx = cls;
751 struct ZoneToNameResponseMessage *ztnr_msg;
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Found result for zone-to-name lookup: `%s'\n",
763 name_len = (NULL == name) ? 0 : strlen (name) + 1;
764 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
765 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
766 if (msg_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
769 ztn_ctx->success = GNUNET_SYSERR;
772 ztnr_msg = GNUNET_malloc (msg_size);
773 ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
774 ztnr_msg->gns_header.header.size = htons (msg_size);
775 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
776 ztnr_msg->res = htons (res);
777 ztnr_msg->rd_len = htons (rd_ser_len);
778 ztnr_msg->rd_count = htons (rd_count);
779 ztnr_msg->name_len = htons (name_len);
780 ztnr_msg->zone = *zone_key;
781 name_tmp = (char *) &ztnr_msg[1];
783 memcpy (name_tmp, name, name_len);
784 rd_tmp = &name_tmp[name_len];
785 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_tmp);
786 ztn_ctx->success = GNUNET_OK;
787 GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client,
788 &ztnr_msg->gns_header.header,
790 GNUNET_free (ztnr_msg);
795 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
798 * @param client client sending the message
799 * @param message message of type 'struct ZoneToNameMessage'
802 handle_zone_to_name (void *cls,
803 struct GNUNET_SERVER_Client *client,
804 const struct GNUNET_MessageHeader *message)
806 struct NamestoreClient *nc;
807 const struct ZoneToNameMessage *ztn_msg;
808 struct ZoneToNameCtx ztn_ctx;
809 struct ZoneToNameResponseMessage ztnr_msg;
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "Received `%s' message\n",
814 ztn_msg = (const struct ZoneToNameMessage *) message;
815 nc = client_lookup (client);
816 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
818 ztn_ctx.success = GNUNET_NO;
820 GSN_database->zone_to_name (GSN_database->cls,
822 &ztn_msg->value_zone,
823 &handle_zone_to_name_it, &ztn_ctx))
825 /* internal error, hang up instead of signalling something
826 that might be wrong */
828 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
831 if (GNUNET_NO == ztn_ctx.success)
833 /* no result found, send empty response */
834 memset (&ztnr_msg, 0, sizeof (ztnr_msg));
835 ztnr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
836 ztnr_msg.gns_header.header.size = htons (sizeof (ztnr_msg));
837 ztnr_msg.gns_header.r_id = ztn_msg->gns_header.r_id;
838 ztnr_msg.res = htons (GNUNET_NO);
839 GNUNET_SERVER_notification_context_unicast (snc,
841 &ztnr_msg.gns_header.header,
844 GNUNET_SERVER_receive_done (client, ztn_ctx.success);
849 * Zone iteration processor result
851 enum ZoneIterationResult
860 * Continue to iterate with next iteration_next call
862 IT_SUCCESS_MORE_AVAILABLE = 1,
867 IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE = 2
872 * Context for record remove operations passed from
873 * #run_zone_iteration_round to #zone_iteraterate_proc as closure
875 struct ZoneIterationProcResult
878 * The zone iteration handle
880 struct ZoneIteration *zi;
883 * Iteration result: iteration done?
884 * #IT_SUCCESS_MORE_AVAILABLE: if there may be more results overall but
885 * we got one for now and have sent it to the client
886 * #IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE: if there are no further results,
887 * #IT_START: if we are still trying to find a result.
889 int res_iteration_finished;
895 * Process results for zone iteration from database
897 * @param cls struct ZoneIterationProcResult *proc
898 * @param zone_key the zone key
900 * @param rd_count number of records for this name
901 * @param rd record data
904 zone_iteraterate_proc (void *cls,
905 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
907 unsigned int rd_count,
908 const struct GNUNET_NAMESTORE_RecordData *rd)
910 struct ZoneIterationProcResult *proc = cls;
912 if ((NULL == zone_key) && (NULL == name))
914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 proc->res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
919 if ((NULL == zone_key) || (NULL == name))
921 /* what is this!? should never happen */
922 proc->res_iteration_finished = IT_START;
926 proc->res_iteration_finished = IT_SUCCESS_MORE_AVAILABLE;
927 send_lookup_response (snc,
928 proc->zi->client->client,
929 proc->zi->request_id,
938 * Perform the next round of the zone iteration.
940 * @param zi zone iterator to process
943 run_zone_iteration_round (struct ZoneIteration *zi)
945 static struct GNUNET_CRYPTO_EccPrivateKey zero;
946 struct ZoneIterationProcResult proc;
947 struct RecordResultMessage rrm;
950 memset (&proc, 0, sizeof (proc));
952 proc.res_iteration_finished = IT_START;
953 while (IT_START == proc.res_iteration_finished)
956 (ret = GSN_database->iterate_records (GSN_database->cls,
957 (0 == memcmp (&zi->zone, &zero, sizeof (zero)))
961 &zone_iteraterate_proc, &proc)))
966 if (GNUNET_NO == ret)
967 proc.res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
970 if (IT_SUCCESS_MORE_AVAILABLE == proc.res_iteration_finished)
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "More results available\n");
974 return; /* more results later */
976 /* send empty response to indicate end of list */
977 memset (&rrm, 0, sizeof (rrm));
978 rrm.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
979 rrm.gns_header.header.size = htons (sizeof (rrm));
980 rrm.gns_header.r_id = htonl (zi->request_id);
981 GNUNET_SERVER_notification_context_unicast (snc,
983 &rrm.gns_header.header,
985 GNUNET_CONTAINER_DLL_remove (zi->client->op_head,
993 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
996 * @param client the client sending the message
997 * @param message message of type 'struct ZoneIterationStartMessage'
1000 handle_iteration_start (void *cls,
1001 struct GNUNET_SERVER_Client *client,
1002 const struct GNUNET_MessageHeader *message)
1004 const struct ZoneIterationStartMessage *zis_msg;
1005 struct NamestoreClient *nc;
1006 struct ZoneIteration *zi;
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
1009 if (NULL == (nc = client_lookup (client)))
1012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1015 zis_msg = (const struct ZoneIterationStartMessage *) message;
1016 zi = GNUNET_new (struct ZoneIteration);
1017 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1020 zi->zone = zis_msg->zone;
1021 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1022 run_zone_iteration_round (zi);
1023 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1028 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1031 * @param client GNUNET_SERVER_Client sending the message
1032 * @param message message of type 'struct ZoneIterationStopMessage'
1035 handle_iteration_stop (void *cls,
1036 struct GNUNET_SERVER_Client *client,
1037 const struct GNUNET_MessageHeader *message)
1039 struct NamestoreClient *nc;
1040 struct ZoneIteration *zi;
1041 const struct ZoneIterationStopMessage *zis_msg;
1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1045 "Received `%s' message\n",
1046 "ZONE_ITERATION_STOP");
1047 if (NULL == (nc = client_lookup(client)))
1050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1053 zis_msg = (const struct ZoneIterationStopMessage *) message;
1054 rid = ntohl (zis_msg->gns_header.r_id);
1055 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1056 if (zi->request_id == rid)
1061 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1064 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1066 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1071 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1074 * @param client GNUNET_SERVER_Client sending the message
1075 * @param message message of type 'struct ZoneIterationNextMessage'
1078 handle_iteration_next (void *cls,
1079 struct GNUNET_SERVER_Client *client,
1080 const struct GNUNET_MessageHeader *message)
1082 struct NamestoreClient *nc;
1083 struct ZoneIteration *zi;
1084 const struct ZoneIterationNextMessage *zis_msg;
1087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1088 if (NULL == (nc = client_lookup(client)))
1091 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1094 zis_msg = (const struct ZoneIterationNextMessage *) message;
1095 rid = ntohl (zis_msg->gns_header.r_id);
1096 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1097 if (zi->request_id == rid)
1102 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1105 run_zone_iteration_round (zi);
1106 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1111 * Send 'sync' message to zone monitor, we're now in sync.
1113 * @param zm monitor that is now in sync
1116 monitor_sync (struct ZoneMonitor *zm)
1118 struct GNUNET_MessageHeader sync;
1120 sync.size = htons (sizeof (struct GNUNET_MessageHeader));
1121 sync.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1122 GNUNET_SERVER_notification_context_unicast (monitor_nc,
1130 * Obtain the next datum during the zone monitor's zone intiial iteration.
1132 * @param cls zone monitor that does its initial iteration
1133 * @param tc scheduler context
1136 monitor_next (void *cls,
1137 const struct GNUNET_SCHEDULER_TaskContext *tc);
1141 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1143 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1144 * @param zone_key zone key of the zone
1146 * @param rd_count number of records in @a rd
1147 * @param rd array of records
1150 monitor_iterate_cb (void *cls,
1151 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
1153 unsigned int rd_count,
1154 const struct GNUNET_NAMESTORE_RecordData *rd)
1156 struct ZoneMonitor *zm = cls;
1160 /* finished with iteration */
1164 send_lookup_response (monitor_nc,
1171 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1176 * Handles a 'GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START' message
1179 * @param client GNUNET_SERVER_Client sending the message
1180 * @param message message of type 'struct ZoneMonitorStartMessage'
1183 handle_monitor_start (void *cls,
1184 struct GNUNET_SERVER_Client *client,
1185 const struct GNUNET_MessageHeader *message)
1187 const struct ZoneMonitorStartMessage *zis_msg;
1188 struct ZoneMonitor *zm;
1190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1191 "Received `%s' message\n",
1192 "ZONE_MONITOR_START");
1193 zis_msg = (const struct ZoneMonitorStartMessage *) message;
1194 zm = GNUNET_new (struct ZoneMonitor);
1195 zm->request_id = ntohl (zis_msg->gns_header.r_id);
1197 zm->nc = client_lookup (client);
1198 zm->zone = zis_msg->zone;
1199 GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, zm);
1200 GNUNET_SERVER_client_mark_monitor (client);
1201 GNUNET_SERVER_disable_receive_done_warning (client);
1202 GNUNET_SERVER_notification_context_add (monitor_nc,
1204 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1209 * Obtain the next datum during the zone monitor's zone intiial iteration.
1211 * @param cls zone monitor that does its initial iteration
1212 * @param tc scheduler context
1215 monitor_next (void *cls,
1216 const struct GNUNET_SCHEDULER_TaskContext *tc)
1218 struct ZoneMonitor *zm = cls;
1221 zm->task = GNUNET_SCHEDULER_NO_TASK;
1222 ret = GSN_database->iterate_records (GSN_database->cls,
1225 &monitor_iterate_cb, zm);
1226 if (GNUNET_SYSERR == ret)
1228 GNUNET_SERVER_client_disconnect (zm->nc->client);
1231 if (GNUNET_NO == ret)
1241 * Process namestore requests.
1243 * @param cls closure
1244 * @param server the initialized server
1245 * @param cfg configuration to use
1248 run (void *cls, struct GNUNET_SERVER_Handle *server,
1249 const struct GNUNET_CONFIGURATION_Handle *cfg)
1251 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1252 {&handle_lookup_block, NULL,
1253 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK, sizeof (struct LookupBlockMessage)},
1254 {&handle_block_cache, NULL,
1255 GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE, 0},
1256 {&handle_record_store, NULL,
1257 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE, 0},
1258 {&handle_zone_to_name, NULL,
1259 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, sizeof (struct ZoneToNameMessage) },
1260 {&handle_iteration_start, NULL,
1261 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage) },
1262 {&handle_iteration_next, NULL,
1263 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, sizeof (struct ZoneIterationNextMessage) },
1264 {&handle_iteration_stop, NULL,
1265 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage) },
1266 {&handle_monitor_start, NULL,
1267 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START, sizeof (struct ZoneMonitorStartMessage) },
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1274 monitor_nc = GNUNET_SERVER_notification_context_create (server, 1);
1276 /* Loading database plugin */
1278 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1282 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1283 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1284 GNUNET_free (database);
1285 if (NULL == GSN_database)
1287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1288 "Could not load database backend `%s'\n",
1290 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1294 /* Configuring server handles */
1295 GNUNET_SERVER_add_handlers (server, handlers);
1296 snc = GNUNET_SERVER_notification_context_create (server, 16);
1297 GNUNET_SERVER_disconnect_notify (server,
1298 &client_disconnect_notification,
1300 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1306 * The main function for the template service.
1308 * @param argc number of arguments from the command line
1309 * @param argv command line arguments
1310 * @return 0 ok, 1 on error
1313 main (int argc, char *const *argv)
1315 return (GNUNET_OK ==
1316 GNUNET_SERVICE_run (argc, argv, "namestore",
1317 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1320 /* end of gnunet-service-namestore.c */