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);
243 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
244 GNUNET_free (db_lib_name);
246 if (NULL != monitor_nc)
248 GNUNET_SERVER_notification_context_destroy (monitor_nc);
255 * Called whenever a client is disconnected.
256 * Frees our resources associated with that client.
259 * @param client identification of the client
262 client_disconnect_notification (void *cls,
263 struct GNUNET_SERVER_Client *client)
265 struct ZoneIteration *no;
266 struct NamestoreClient *nc;
267 struct ZoneMonitor *zm;
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272 "Client %p disconnected\n",
274 if (NULL == (nc = GNUNET_SERVER_client_get_user_context (client, struct NamestoreClient)))
276 while (NULL != (no = nc->op_head))
278 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
281 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
283 for (zm = monitor_head; NULL != zm; zm = zm->next)
285 if (client == zm->nc->client)
287 GNUNET_CONTAINER_DLL_remove (monitor_head,
290 if (GNUNET_SCHEDULER_NO_TASK != zm->task)
292 GNUNET_SCHEDULER_cancel (zm->task);
293 zm->task = GNUNET_SCHEDULER_NO_TASK;
303 * Add a client to our list of active clients, if it is not yet
306 * @param client client to add
307 * @return internal namestore client structure for this client
309 static struct NamestoreClient *
310 client_lookup (struct GNUNET_SERVER_Client *client)
312 struct NamestoreClient *nc;
314 nc = GNUNET_SERVER_client_get_user_context (client, struct NamestoreClient);
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "Client %p connected\n",
320 nc = GNUNET_new (struct NamestoreClient);
322 GNUNET_SERVER_notification_context_add (snc, client);
323 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, nc);
324 GNUNET_SERVER_client_set_user_context (client, nc);
330 * Context for name lookups passed from #handle_lookup_block to
331 * #handle_lookup_block_it as closure
333 struct LookupBlockContext
336 * The client to send the response to
338 struct NamestoreClient *nc;
341 * Operation id for the name lookup
349 * A #GNUNET_NAMESTORE_BlockCallback for name lookups in #handle_lookup_block
351 * @param cls a 'struct LookupNameContext *' with information about the request
352 * @param block the block
355 handle_lookup_block_it (void *cls,
356 const struct GNUNET_NAMESTORE_Block *block)
358 struct LookupBlockContext *lnc = cls;
359 struct LookupBlockResponseMessage *r;
362 esize = ntohl (block->purpose.size)
363 - sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)
364 - sizeof (struct GNUNET_TIME_AbsoluteNBO);
365 r = GNUNET_malloc (sizeof (struct LookupBlockResponseMessage) + esize);
366 r->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE);
367 r->gns_header.header.size = htons (sizeof (struct LookupBlockResponseMessage) + esize);
368 r->gns_header.r_id = htonl (lnc->request_id);
369 r->expire = block->expiration_time;
370 r->signature = block->signature;
371 r->derived_key = block->derived_key;
372 memcpy (&r[1], &block[1], esize);
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374 "Sending `%s' message\n",
375 "NAMESTORE_LOOKUP_BLOCK_RESPONSE");
376 GNUNET_SERVER_notification_context_unicast (snc,
378 &r->gns_header.header,
385 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK message
388 * @param client client sending the message
389 * @param message message of type 'struct LookupNameMessage'
392 handle_lookup_block (void *cls,
393 struct GNUNET_SERVER_Client *client,
394 const struct GNUNET_MessageHeader *message)
396 const struct LookupBlockMessage *ln_msg;
397 struct LookupBlockContext lnc;
398 struct NamestoreClient *nc;
399 struct LookupBlockResponseMessage zir_end;
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "Received `%s' message\n",
404 "NAMESTORE_LOOKUP_BLOCK");
405 nc = client_lookup(client);
406 ln_msg = (const struct LookupBlockMessage *) message;
407 lnc.request_id = ntohl (ln_msg->gns_header.r_id);
410 (ret = GSN_database->lookup_block (GSN_database->cls,
412 &handle_lookup_block_it, &lnc)))
414 /* internal error (in database plugin); might be best to just hang up on
415 plugin rather than to signal that there are 'no' results, which
416 might also be false... */
418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
423 /* no records match at all, generate empty response */
424 memset (&zir_end, 0, sizeof (zir_end));
425 zir_end.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK_RESPONSE);
426 zir_end.gns_header.header.size = htons (sizeof (struct LookupBlockResponseMessage));
427 zir_end.gns_header.r_id = ln_msg->gns_header.r_id;
428 GNUNET_SERVER_notification_context_unicast (snc,
430 &zir_end.gns_header.header,
434 GNUNET_SERVER_receive_done (client, GNUNET_OK);
439 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE message
442 * @param client GNUNET_SERVER_Client sending the message
443 * @param message message of type 'struct BlockCacheMessage'
446 handle_block_cache (void *cls,
447 struct GNUNET_SERVER_Client *client,
448 const struct GNUNET_MessageHeader *message)
450 struct NamestoreClient *nc;
451 const struct BlockCacheMessage *rp_msg;
452 struct BlockCacheResponseMessage rpr_msg;
453 struct GNUNET_NAMESTORE_Block *block;
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Received `%s' message\n",
459 "NAMESTORE_BLOCK_CACHE");
460 nc = client_lookup (client);
461 if (ntohs (message->size) < sizeof (struct BlockCacheMessage))
464 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
467 rp_msg = (const struct BlockCacheMessage *) message;
468 esize = ntohs (rp_msg->gns_header.header.size) - sizeof (struct BlockCacheMessage);
470 block = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Block) + esize);
471 block->signature = rp_msg->signature;
472 block->derived_key = rp_msg->derived_key;
473 block->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
474 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
476 block->expiration_time = rp_msg->expire;
477 memcpy (&block[1], &rp_msg[1], esize);
478 res = GSN_database->cache_block (GSN_database->cls,
482 rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE_RESPONSE);
483 rpr_msg.gns_header.header.size = htons (sizeof (struct BlockCacheResponseMessage));
484 rpr_msg.gns_header.r_id = rp_msg->gns_header.r_id;
485 rpr_msg.op_result = htonl (res);
486 GNUNET_SERVER_notification_context_unicast (snc,
488 &rpr_msg.gns_header.header,
490 GNUNET_SERVER_receive_done (client, GNUNET_OK);
495 * Generate a 'struct LookupNameResponseMessage' and send it to the
496 * given client using the given notification context.
498 * @param nc notification context to use
499 * @param client client to unicast to
500 * @param request_id request ID to use
501 * @param zone_key zone key of the zone
503 * @param rd_count number of records in @a rd
504 * @param rd array of records
507 send_lookup_response (struct GNUNET_SERVER_NotificationContext *nc,
508 struct GNUNET_SERVER_Client *client,
510 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
512 unsigned int rd_count,
513 const struct GNUNET_NAMESTORE_RecordData *rd)
515 struct RecordResultMessage *zir_msg;
522 name_len = strlen (name) + 1;
523 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
524 msg_size = sizeof (struct RecordResultMessage) + name_len + rd_ser_len;
526 zir_msg = GNUNET_malloc (msg_size);
527 zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
528 zir_msg->gns_header.header.size = htons (msg_size);
529 zir_msg->gns_header.r_id = htonl (request_id);
530 zir_msg->name_len = htons (name_len);
531 zir_msg->rd_count = htons (rd_count);
532 zir_msg->rd_len = htons (rd_ser_len);
533 zir_msg->private_key = *zone_key;
534 name_tmp = (char *) &zir_msg[1];
535 memcpy (name_tmp, name, name_len);
536 rd_ser = &name_tmp[name_len];
537 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_ser);
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Sending `%s' message with size %u\n",
542 GNUNET_SERVER_notification_context_unicast (nc,
544 &zir_msg->gns_header.header,
546 GNUNET_free (zir_msg);
551 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
554 * @param client client sending the message
555 * @param message message of type 'struct RecordCreateMessage'
558 handle_record_store (void *cls,
559 struct GNUNET_SERVER_Client *client,
560 const struct GNUNET_MessageHeader *message)
562 struct NamestoreClient *nc;
563 const struct RecordStoreMessage *rp_msg;
564 struct RecordStoreResponseMessage rcr_msg;
570 const char *name_tmp;
573 unsigned int rd_count;
575 struct GNUNET_CRYPTO_EccPublicKey pubkey;
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
578 "Received `%s' message\n",
579 "NAMESTORE_RECORD_STORE");
580 if (ntohs (message->size) < sizeof (struct RecordStoreMessage))
583 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
586 nc = client_lookup (client);
587 rp_msg = (const struct RecordStoreMessage *) message;
588 rid = ntohl (rp_msg->gns_header.r_id);
589 name_len = ntohs (rp_msg->name_len);
590 msg_size = ntohs (message->size);
591 rd_count = ntohs (rp_msg->rd_count);
592 rd_ser_len = ntohs (rp_msg->rd_len);
593 GNUNET_break (0 == ntohs (rp_msg->reserved));
594 msg_size_exp = sizeof (struct RecordStoreMessage) + name_len + rd_ser_len;
595 if (msg_size != msg_size_exp)
598 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
601 if ((0 == name_len) || (name_len > MAX_NAME_LEN))
604 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
607 name_tmp = (const char *) &rp_msg[1];
608 rd_ser = &name_tmp[name_len];
609 if ('\0' != name_tmp[name_len -1])
612 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
616 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
619 GNUNET_NAMESTORE_records_deserialize (rd_ser_len, rd_ser, rd_count, rd))
622 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
626 /* Extracting and converting private key */
627 GNUNET_CRYPTO_ecc_key_get_public (&rp_msg->private_key,
629 conv_name = GNUNET_NAMESTORE_normalize_string (name_tmp);
630 if (NULL == conv_name)
632 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
633 "Error converting name `%s'\n", name_tmp);
634 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "Creating %u records for name `%s' in zone `%s'\n",
639 (unsigned int) rd_count,
641 GNUNET_NAMESTORE_z2s (&pubkey));
642 res = GSN_database->store_records (GSN_database->cls,
643 &rp_msg->private_key,
646 if (GNUNET_OK == res)
648 struct ZoneMonitor *zm;
649 struct GNUNET_NAMESTORE_Block *block;
652 block = GNUNET_NAMESTORE_block_create (&rp_msg->private_key,
653 GNUNET_TIME_UNIT_ZERO_ABS,
657 block = GNUNET_NAMESTORE_block_create (&rp_msg->private_key,
658 GNUNET_TIME_UNIT_FOREVER_ABS,
662 GSN_database->cache_block (GSN_database->cls,
665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
666 _("Failed to cache encrypted block of my own zone!\n"));
671 for (zm = monitor_head; NULL != zm; zm = zm->next)
672 if (0 == memcmp (&rp_msg->private_key,
674 sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
675 send_lookup_response (monitor_nc,
678 &rp_msg->private_key,
682 GNUNET_free (conv_name);
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "Sending `%s' message\n",
688 "RECORD_STORE_RESPONSE");
689 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
690 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordStoreResponseMessage));
691 rcr_msg.gns_header.r_id = htonl (rid);
692 rcr_msg.op_result = htonl (res);
693 GNUNET_SERVER_notification_context_unicast (snc, nc->client,
694 &rcr_msg.gns_header.header,
696 GNUNET_SERVER_receive_done (client, GNUNET_OK);
701 * Context for record remove operations passed from #handle_zone_to_name to
702 * #handle_zone_to_name_it as closure
709 struct NamestoreClient *nc;
712 * Request id (to be used in the response to the client).
717 * Set to #GNUNET_OK on success, #GNUNET_SYSERR on error. Note that
718 * not finding a name for the zone still counts as a 'success' here,
719 * as this field is about the success of executing the IPC protocol.
726 * Zone to name iterator
728 * @param cls struct ZoneToNameCtx *
729 * @param zone_key the zone key
731 * @param rd_count number of records in @a rd
732 * @param rd record data
735 handle_zone_to_name_it (void *cls,
736 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
738 unsigned int rd_count,
739 const struct GNUNET_NAMESTORE_RecordData *rd)
741 struct ZoneToNameCtx *ztn_ctx = cls;
742 struct ZoneToNameResponseMessage *ztnr_msg;
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
751 "Found result for zone-to-name lookup: `%s'\n",
754 name_len = strlen (name) + 1;
755 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
756 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
757 if (msg_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
760 ztn_ctx->success = GNUNET_SYSERR;
763 ztnr_msg = GNUNET_malloc (msg_size);
764 ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
765 ztnr_msg->gns_header.header.size = htons (msg_size);
766 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
767 ztnr_msg->res = htons (res);
768 ztnr_msg->rd_len = htons (rd_ser_len);
769 ztnr_msg->rd_count = htons (rd_count);
770 ztnr_msg->name_len = htons (name_len);
771 ztnr_msg->zone = *zone_key;
772 name_tmp = (char *) &ztnr_msg[1];
774 memcpy (name_tmp, name, name_len);
775 rd_tmp = &name_tmp[name_len];
776 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_tmp);
777 ztn_ctx->success = GNUNET_OK;
778 GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client,
779 &ztnr_msg->gns_header.header,
781 GNUNET_free (ztnr_msg);
786 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME message
789 * @param client client sending the message
790 * @param message message of type 'struct ZoneToNameMessage'
793 handle_zone_to_name (void *cls,
794 struct GNUNET_SERVER_Client *client,
795 const struct GNUNET_MessageHeader *message)
797 struct NamestoreClient *nc;
798 const struct ZoneToNameMessage *ztn_msg;
799 struct ZoneToNameCtx ztn_ctx;
800 struct ZoneToNameResponseMessage ztnr_msg;
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
803 "Received `%s' message\n",
805 ztn_msg = (const struct ZoneToNameMessage *) message;
806 nc = client_lookup (client);
807 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
809 ztn_ctx.success = GNUNET_NO;
811 GSN_database->zone_to_name (GSN_database->cls,
813 &ztn_msg->value_zone,
814 &handle_zone_to_name_it, &ztn_ctx))
816 /* internal error, hang up instead of signalling something
817 that might be wrong */
819 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
822 if (GNUNET_NO == ztn_ctx.success)
824 /* no result found, send empty response */
825 memset (&ztnr_msg, 0, sizeof (ztnr_msg));
826 ztnr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
827 ztnr_msg.gns_header.header.size = htons (sizeof (ztnr_msg));
828 ztnr_msg.gns_header.r_id = ztn_msg->gns_header.r_id;
829 ztnr_msg.res = htons (GNUNET_NO);
830 GNUNET_SERVER_notification_context_unicast (snc,
832 &ztnr_msg.gns_header.header,
835 GNUNET_SERVER_receive_done (client, ztn_ctx.success);
840 * Zone iteration processor result
842 enum ZoneIterationResult
851 * Continue to iterate with next iteration_next call
853 IT_SUCCESS_MORE_AVAILABLE = 1,
858 IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE = 2
863 * Context for record remove operations passed from
864 * 'run_zone_iteration_round' to 'zone_iteraterate_proc' as closure
866 struct ZoneIterationProcResult
869 * The zone iteration handle
871 struct ZoneIteration *zi;
874 * Iteration result: iteration done?
875 * #IT_SUCCESS_MORE_AVAILABLE: if there may be more results overall but
876 * we got one for now and have sent it to the client
877 * #IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE: if there are no further results,
878 * #IT_START: if we are still trying to find a result.
880 int res_iteration_finished;
886 * Process results for zone iteration from database
888 * @param cls struct ZoneIterationProcResult *proc
889 * @param zone_key the zone key
891 * @param rd_count number of records for this name
892 * @param rd record data
895 zone_iteraterate_proc (void *cls,
896 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
898 unsigned int rd_count,
899 const struct GNUNET_NAMESTORE_RecordData *rd)
901 struct ZoneIterationProcResult *proc = cls;
903 if ((NULL == zone_key) && (NULL == name))
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907 proc->res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
910 if ((NULL == zone_key) || (NULL == name))
912 /* what is this!? should never happen */
913 proc->res_iteration_finished = IT_START;
917 proc->res_iteration_finished = IT_SUCCESS_MORE_AVAILABLE;
918 send_lookup_response (snc,
919 proc->zi->client->client,
920 proc->zi->request_id,
929 * Perform the next round of the zone iteration.
931 * @param zi zone iterator to process
934 run_zone_iteration_round (struct ZoneIteration *zi)
936 struct ZoneIterationProcResult proc;
937 struct RecordResultMessage rrm;
940 memset (&proc, 0, sizeof (proc));
942 proc.res_iteration_finished = IT_START;
943 while (IT_START == proc.res_iteration_finished)
946 (ret = GSN_database->iterate_records (GSN_database->cls,
949 &zone_iteraterate_proc, &proc)))
954 if (GNUNET_NO == ret)
955 proc.res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
958 if (IT_SUCCESS_MORE_AVAILABLE == proc.res_iteration_finished)
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
961 "More results available\n");
962 return; /* more results later */
964 /* send empty response to indicate end of list */
965 memset (&rrm, 0, sizeof (rrm));
966 rrm.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
967 rrm.gns_header.header.size = htons (sizeof (rrm));
968 rrm.gns_header.r_id = htonl (zi->request_id);
969 GNUNET_SERVER_notification_context_unicast (snc,
971 &rrm.gns_header.header,
973 GNUNET_CONTAINER_DLL_remove (zi->client->op_head,
981 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START message
984 * @param client GNUNET_SERVER_Client sending the message
985 * @param message message of type 'struct ZoneIterationStartMessage'
988 handle_iteration_start (void *cls,
989 struct GNUNET_SERVER_Client *client,
990 const struct GNUNET_MessageHeader *message)
992 const struct ZoneIterationStartMessage *zis_msg;
993 struct NamestoreClient *nc;
994 struct ZoneIteration *zi;
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
997 if (NULL == (nc = client_lookup (client)))
1000 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1003 zis_msg = (const struct ZoneIterationStartMessage *) message;
1004 zi = GNUNET_new (struct ZoneIteration);
1005 zi->request_id = ntohl (zis_msg->gns_header.r_id);
1008 zi->zone = zis_msg->zone;
1009 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
1010 run_zone_iteration_round (zi);
1011 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1016 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP message
1019 * @param client GNUNET_SERVER_Client sending the message
1020 * @param message message of type 'struct ZoneIterationStopMessage'
1023 handle_iteration_stop (void *cls,
1024 struct GNUNET_SERVER_Client *client,
1025 const struct GNUNET_MessageHeader *message)
1027 struct NamestoreClient *nc;
1028 struct ZoneIteration *zi;
1029 const struct ZoneIterationStopMessage *zis_msg;
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "Received `%s' message\n",
1034 "ZONE_ITERATION_STOP");
1035 if (NULL == (nc = client_lookup(client)))
1038 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1041 zis_msg = (const struct ZoneIterationStopMessage *) message;
1042 rid = ntohl (zis_msg->gns_header.r_id);
1043 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1044 if (zi->request_id == rid)
1049 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1052 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1054 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1059 * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message
1062 * @param client GNUNET_SERVER_Client sending the message
1063 * @param message message of type 'struct ZoneIterationNextMessage'
1066 handle_iteration_next (void *cls,
1067 struct GNUNET_SERVER_Client *client,
1068 const struct GNUNET_MessageHeader *message)
1070 struct NamestoreClient *nc;
1071 struct ZoneIteration *zi;
1072 const struct ZoneIterationNextMessage *zis_msg;
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1076 if (NULL == (nc = client_lookup(client)))
1079 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1082 zis_msg = (const struct ZoneIterationNextMessage *) message;
1083 rid = ntohl (zis_msg->gns_header.r_id);
1084 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1085 if (zi->request_id == rid)
1090 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1093 run_zone_iteration_round (zi);
1094 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1099 * Send 'sync' message to zone monitor, we're now in sync.
1101 * @param zm monitor that is now in sync
1104 monitor_sync (struct ZoneMonitor *zm)
1106 struct GNUNET_MessageHeader sync;
1108 sync.size = htons (sizeof (struct GNUNET_MessageHeader));
1109 sync.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1110 GNUNET_SERVER_notification_context_unicast (monitor_nc,
1118 * Obtain the next datum during the zone monitor's zone intiial iteration.
1120 * @param cls zone monitor that does its initial iteration
1121 * @param tc scheduler context
1124 monitor_next (void *cls,
1125 const struct GNUNET_SCHEDULER_TaskContext *tc);
1129 * A #GNUNET_NAMESTORE_RecordIterator for monitors.
1131 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1132 * @param zone_key zone key of the zone
1134 * @param rd_count number of records in @a rd
1135 * @param rd array of records
1138 monitor_iterate_cb (void *cls,
1139 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
1141 unsigned int rd_count,
1142 const struct GNUNET_NAMESTORE_RecordData *rd)
1144 struct ZoneMonitor *zm = cls;
1148 /* finished with iteration */
1152 send_lookup_response (monitor_nc,
1159 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1164 * Handles a 'GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START' message
1167 * @param client GNUNET_SERVER_Client sending the message
1168 * @param message message of type 'struct ZoneMonitorStartMessage'
1171 handle_monitor_start (void *cls,
1172 struct GNUNET_SERVER_Client *client,
1173 const struct GNUNET_MessageHeader *message)
1175 const struct ZoneMonitorStartMessage *zis_msg;
1176 struct ZoneMonitor *zm;
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1179 "Received `%s' message\n",
1180 "ZONE_MONITOR_START");
1181 zis_msg = (const struct ZoneMonitorStartMessage *) message;
1182 zm = GNUNET_new (struct ZoneMonitor);
1183 zm->request_id = ntohl (zis_msg->gns_header.r_id);
1185 zm->nc = client_lookup (client);
1186 zm->zone = zis_msg->zone;
1187 GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, zm);
1188 GNUNET_SERVER_client_mark_monitor (client);
1189 GNUNET_SERVER_disable_receive_done_warning (client);
1190 GNUNET_SERVER_notification_context_add (monitor_nc,
1192 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1197 * Obtain the next datum during the zone monitor's zone intiial iteration.
1199 * @param cls zone monitor that does its initial iteration
1200 * @param tc scheduler context
1203 monitor_next (void *cls,
1204 const struct GNUNET_SCHEDULER_TaskContext *tc)
1206 struct ZoneMonitor *zm = cls;
1209 zm->task = GNUNET_SCHEDULER_NO_TASK;
1210 ret = GSN_database->iterate_records (GSN_database->cls,
1213 &monitor_iterate_cb, zm);
1214 if (GNUNET_SYSERR == ret)
1216 GNUNET_SERVER_client_disconnect (zm->nc->client);
1219 if (GNUNET_NO == ret)
1229 * Process namestore requests.
1231 * @param cls closure
1232 * @param server the initialized server
1233 * @param cfg configuration to use
1236 run (void *cls, struct GNUNET_SERVER_Handle *server,
1237 const struct GNUNET_CONFIGURATION_Handle *cfg)
1239 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1240 {&handle_lookup_block, NULL,
1241 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK, sizeof (struct LookupBlockMessage)},
1242 {&handle_block_cache, NULL,
1243 GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE, 0},
1244 {&handle_record_store, NULL,
1245 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE, 0},
1246 {&handle_zone_to_name, NULL,
1247 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, sizeof (struct ZoneToNameMessage) },
1248 {&handle_iteration_start, NULL,
1249 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage) },
1250 {&handle_iteration_next, NULL,
1251 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, sizeof (struct ZoneIterationNextMessage) },
1252 {&handle_iteration_stop, NULL,
1253 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage) },
1254 {&handle_monitor_start, NULL,
1255 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START, sizeof (struct ZoneMonitorStartMessage) },
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1262 monitor_nc = GNUNET_SERVER_notification_context_create (server, 1);
1264 /* Loading database plugin */
1266 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1270 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1271 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1272 GNUNET_free (database);
1273 if (NULL == GSN_database)
1275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1276 "Could not load database backend `%s'\n",
1278 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1282 /* Configuring server handles */
1283 GNUNET_SERVER_add_handlers (server, handlers);
1284 snc = GNUNET_SERVER_notification_context_create (server, 16);
1285 GNUNET_SERVER_disconnect_notify (server,
1286 &client_disconnect_notification,
1288 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1294 * The main function for the template service.
1296 * @param argc number of arguments from the command line
1297 * @param argv command line arguments
1298 * @return 0 ok, 1 on error
1301 main (int argc, char *const *argv)
1303 return (GNUNET_OK ==
1304 GNUNET_SERVICE_run (argc, argv, "namestore",
1305 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1308 /* end of gnunet-service-namestore.c */