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
159 * Initialy set to 0 in handle_iteration_start
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_name' to
331 * 'handle_lookup_name_it' as closure
333 struct LookupNameContext
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_name
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 LookupNameContext *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_NAME_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 LookupNameContext 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_NAME");
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
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;
650 for (zm = monitor_head; NULL != zm; zm = zm->next)
651 if (0 == memcmp (&rp_msg->private_key,
653 sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
654 send_lookup_response (monitor_nc,
657 &rp_msg->private_key,
661 GNUNET_free (conv_name);
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "Sending `%s' message\n",
667 "RECORD_STORE_RESPONSE");
668 rcr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE);
669 rcr_msg.gns_header.header.size = htons (sizeof (struct RecordStoreResponseMessage));
670 rcr_msg.gns_header.r_id = htonl (rid);
671 rcr_msg.op_result = htonl (res);
672 GNUNET_SERVER_notification_context_unicast (snc, nc->client,
673 &rcr_msg.gns_header.header,
675 GNUNET_SERVER_receive_done (client, GNUNET_OK);
680 * Context for record remove operations passed from 'handle_zone_to_name' to
681 * 'handle_zone_to_name_it' as closure
688 struct NamestoreClient *nc;
691 * Request id (to be used in the response to the client).
696 * Set to GNUNET_OK on success, GNUNET_SYSERR on error. Note that
697 * not finding a name for the zone still counts as a 'success' here,
698 * as this field is about the success of executing the IPC protocol.
705 * Zone to name iterator
707 * @param cls struct ZoneToNameCtx *
708 * @param zone_key the zone key
710 * @param rd_count number of records
711 * @param rd record data
714 handle_zone_to_name_it (void *cls,
715 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
717 unsigned int rd_count,
718 const struct GNUNET_NAMESTORE_RecordData *rd)
720 struct ZoneToNameCtx *ztn_ctx = cls;
721 struct ZoneToNameResponseMessage *ztnr_msg;
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730 "Found result for zone-to-name lookup: `%s'\n",
733 name_len = strlen (name) + 1;
734 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
735 msg_size = sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len;
736 if (msg_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
739 ztn_ctx->success = GNUNET_SYSERR;
742 ztnr_msg = GNUNET_malloc (msg_size);
743 ztnr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
744 ztnr_msg->gns_header.header.size = htons (msg_size);
745 ztnr_msg->gns_header.r_id = htonl (ztn_ctx->rid);
746 ztnr_msg->res = htons (res);
747 ztnr_msg->rd_len = htons (rd_ser_len);
748 ztnr_msg->rd_count = htons (rd_count);
749 ztnr_msg->name_len = htons (name_len);
750 ztnr_msg->zone = *zone_key;
751 name_tmp = (char *) &ztnr_msg[1];
753 memcpy (name_tmp, name, name_len);
754 rd_tmp = &name_tmp[name_len];
755 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_tmp);
756 ztn_ctx->success = GNUNET_OK;
757 GNUNET_SERVER_notification_context_unicast (snc, ztn_ctx->nc->client,
758 &ztnr_msg->gns_header.header,
760 GNUNET_free (ztnr_msg);
765 * Handles a 'GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME' message
768 * @param client GNUNET_SERVER_Client sending the message
769 * @param message message of type 'struct ZoneToNameMessage'
772 handle_zone_to_name (void *cls,
773 struct GNUNET_SERVER_Client *client,
774 const struct GNUNET_MessageHeader *message)
776 struct NamestoreClient *nc;
777 const struct ZoneToNameMessage *ztn_msg;
778 struct ZoneToNameCtx ztn_ctx;
779 struct ZoneToNameResponseMessage ztnr_msg;
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "Received `%s' message\n",
784 ztn_msg = (const struct ZoneToNameMessage *) message;
785 nc = client_lookup (client);
786 ztn_ctx.rid = ntohl (ztn_msg->gns_header.r_id);
788 ztn_ctx.success = GNUNET_NO;
790 GSN_database->zone_to_name (GSN_database->cls,
792 &ztn_msg->value_zone,
793 &handle_zone_to_name_it, &ztn_ctx))
795 /* internal error, hang up instead of signalling something
796 that might be wrong */
798 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
801 if (GNUNET_NO == ztn_ctx.success)
803 /* no result found, send empty response */
804 memset (&ztnr_msg, 0, sizeof (ztnr_msg));
805 ztnr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE);
806 ztnr_msg.gns_header.header.size = htons (sizeof (ztnr_msg));
807 ztnr_msg.gns_header.r_id = ztn_msg->gns_header.r_id;
808 ztnr_msg.res = htons (GNUNET_NO);
809 GNUNET_SERVER_notification_context_unicast (snc,
811 &ztnr_msg.gns_header.header,
814 GNUNET_SERVER_receive_done (client, ztn_ctx.success);
819 * Zone iteration processor result
821 enum ZoneIterationResult
830 * Continue to iterate with next iteration_next call
832 IT_SUCCESS_MORE_AVAILABLE = 1,
837 IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE = 2
842 * Context for record remove operations passed from
843 * 'run_zone_iteration_round' to 'zone_iteraterate_proc' as closure
845 struct ZoneIterationProcResult
848 * The zone iteration handle
850 struct ZoneIteration *zi;
853 * Iteration result: iteration done?
854 * IT_SUCCESS_MORE_AVAILABLE: if there may be more results overall but
855 * we got one for now and have sent it to the client
856 * IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE: if there are no further results,
857 * IT_ALL_RECORDS_FILTERED: if all results were filtered so far.
859 int res_iteration_finished;
865 * Process results for zone iteration from database
867 * @param cls struct ZoneIterationProcResult *proc
868 * @param zone_key the zone key
869 * @param expire expiration time
871 * @param rd_count number of records for this name
872 * @param rd record data
873 * @param signature block signature
876 zone_iteraterate_proc (void *cls,
877 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
879 unsigned int rd_count,
880 const struct GNUNET_NAMESTORE_RecordData *rd)
882 struct ZoneIterationProcResult *proc = cls;
884 if ((NULL == zone_key) && (NULL == name))
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888 proc->res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
891 if ((NULL == zone_key) || (NULL == name))
893 /* what is this!? should never happen */
894 proc->res_iteration_finished = IT_START;
898 proc->res_iteration_finished = IT_SUCCESS_MORE_AVAILABLE;
899 send_lookup_response (snc,
900 proc->zi->client->client,
901 proc->zi->request_id,
910 * Perform the next round of the zone iteration.
912 * @param zi zone iterator to process
915 run_zone_iteration_round (struct ZoneIteration *zi)
917 struct ZoneIterationProcResult proc;
918 struct RecordResultMessage rrm;
921 memset (&proc, 0, sizeof (proc));
923 proc.res_iteration_finished = IT_START;
924 while (IT_START == proc.res_iteration_finished)
927 (ret = GSN_database->iterate_records (GSN_database->cls,
930 &zone_iteraterate_proc, &proc)))
935 if (GNUNET_NO == ret)
936 proc.res_iteration_finished = IT_SUCCESS_NOT_MORE_RESULTS_AVAILABLE;
939 if (IT_SUCCESS_MORE_AVAILABLE == proc.res_iteration_finished)
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942 "More results available\n");
943 return; /* more results later */
945 /* send empty response to indicate end of list */
946 memset (&rrm, 0, sizeof (rrm));
947 rrm.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT);
948 rrm.gns_header.header.size = htons (sizeof (rrm));
949 rrm.gns_header.r_id = htonl (zi->request_id);
950 GNUNET_SERVER_notification_context_unicast (snc,
952 &rrm.gns_header.header,
954 GNUNET_CONTAINER_DLL_remove (zi->client->op_head,
962 * Handles a 'GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START' message
965 * @param client GNUNET_SERVER_Client sending the message
966 * @param message message of type 'struct ZoneIterationStartMessage'
969 handle_iteration_start (void *cls,
970 struct GNUNET_SERVER_Client *client,
971 const struct GNUNET_MessageHeader *message)
973 const struct ZoneIterationStartMessage *zis_msg;
974 struct NamestoreClient *nc;
975 struct ZoneIteration *zi;
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
978 if (NULL == (nc = client_lookup (client)))
981 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
984 zis_msg = (const struct ZoneIterationStartMessage *) message;
985 zi = GNUNET_new (struct ZoneIteration);
986 zi->request_id = ntohl (zis_msg->gns_header.r_id);
989 zi->zone = zis_msg->zone;
990 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
991 run_zone_iteration_round (zi);
992 GNUNET_SERVER_receive_done (client, GNUNET_OK);
997 * Handles a 'GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP' message
1000 * @param client GNUNET_SERVER_Client sending the message
1001 * @param message message of type 'struct ZoneIterationStopMessage'
1004 handle_iteration_stop (void *cls,
1005 struct GNUNET_SERVER_Client *client,
1006 const struct GNUNET_MessageHeader *message)
1008 struct NamestoreClient *nc;
1009 struct ZoneIteration *zi;
1010 const struct ZoneIterationStopMessage *zis_msg;
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1014 "Received `%s' message\n",
1015 "ZONE_ITERATION_STOP");
1016 if (NULL == (nc = client_lookup(client)))
1019 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1022 zis_msg = (const struct ZoneIterationStopMessage *) message;
1023 rid = ntohl (zis_msg->gns_header.r_id);
1024 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1025 if (zi->request_id == rid)
1030 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1033 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
1035 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1040 * Handles a 'GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT' message
1043 * @param client GNUNET_SERVER_Client sending the message
1044 * @param message message of type 'struct ZoneIterationNextMessage'
1047 handle_iteration_next (void *cls,
1048 struct GNUNET_SERVER_Client *client,
1049 const struct GNUNET_MessageHeader *message)
1051 struct NamestoreClient *nc;
1052 struct ZoneIteration *zi;
1053 const struct ZoneIterationNextMessage *zis_msg;
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
1057 if (NULL == (nc = client_lookup(client)))
1060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1063 zis_msg = (const struct ZoneIterationNextMessage *) message;
1064 rid = ntohl (zis_msg->gns_header.r_id);
1065 for (zi = nc->op_head; NULL != zi; zi = zi->next)
1066 if (zi->request_id == rid)
1071 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1074 run_zone_iteration_round (zi);
1075 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1080 * Send 'sync' message to zone monitor, we're now in sync.
1082 * @param zm monitor that is now in sync
1085 monitor_sync (struct ZoneMonitor *zm)
1087 struct GNUNET_MessageHeader sync;
1089 sync.size = htons (sizeof (struct GNUNET_MessageHeader));
1090 sync.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_SYNC);
1091 GNUNET_SERVER_notification_context_unicast (monitor_nc,
1099 * Obtain the next datum during the zone monitor's zone intiial iteration.
1101 * @param cls zone monitor that does its initial iteration
1102 * @param tc scheduler context
1105 monitor_next (void *cls,
1106 const struct GNUNET_SCHEDULER_TaskContext *tc);
1110 * A 'GNUNET_NAMESTORE_RecordIterator' for monitors.
1112 * @param cls a 'struct ZoneMonitor *' with information about the monitor
1113 * @param zone_key zone key of the zone
1114 * @param expire expiration time
1116 * @param rd_count number of records
1117 * @param rd array of records
1118 * @param signature signature
1121 monitor_iterate_cb (void *cls,
1122 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
1124 unsigned int rd_count,
1125 const struct GNUNET_NAMESTORE_RecordData *rd)
1127 struct ZoneMonitor *zm = cls;
1131 /* finished with iteration */
1135 send_lookup_response (monitor_nc,
1142 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1147 * Handles a 'GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START' message
1150 * @param client GNUNET_SERVER_Client sending the message
1151 * @param message message of type 'struct ZoneMonitorStartMessage'
1154 handle_monitor_start (void *cls,
1155 struct GNUNET_SERVER_Client *client,
1156 const struct GNUNET_MessageHeader *message)
1158 const struct ZoneMonitorStartMessage *zis_msg;
1159 struct ZoneMonitor *zm;
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1162 "Received `%s' message\n",
1163 "ZONE_MONITOR_START");
1164 zis_msg = (const struct ZoneMonitorStartMessage *) message;
1165 zm = GNUNET_new (struct ZoneMonitor);
1166 zm->request_id = ntohl (zis_msg->gns_header.r_id);
1168 zm->nc = client_lookup (client);
1169 zm->zone = zis_msg->zone;
1170 GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, zm);
1171 GNUNET_SERVER_client_mark_monitor (client);
1172 GNUNET_SERVER_disable_receive_done_warning (client);
1173 GNUNET_SERVER_notification_context_add (monitor_nc,
1175 zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
1180 * Obtain the next datum during the zone monitor's zone intiial iteration.
1182 * @param cls zone monitor that does its initial iteration
1183 * @param tc scheduler context
1186 monitor_next (void *cls,
1187 const struct GNUNET_SCHEDULER_TaskContext *tc)
1189 struct ZoneMonitor *zm = cls;
1192 zm->task = GNUNET_SCHEDULER_NO_TASK;
1193 ret = GSN_database->iterate_records (GSN_database->cls,
1196 &monitor_iterate_cb, zm);
1197 if (GNUNET_SYSERR == ret)
1199 GNUNET_SERVER_client_disconnect (zm->nc->client);
1202 if (GNUNET_NO == ret)
1212 * Process namestore requests.
1214 * @param cls closure
1215 * @param server the initialized server
1216 * @param cfg configuration to use
1219 run (void *cls, struct GNUNET_SERVER_Handle *server,
1220 const struct GNUNET_CONFIGURATION_Handle *cfg)
1222 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1223 {&handle_lookup_block, NULL,
1224 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_BLOCK, sizeof (struct LookupBlockMessage)},
1225 {&handle_block_cache, NULL,
1226 GNUNET_MESSAGE_TYPE_NAMESTORE_BLOCK_CACHE, 0},
1227 {&handle_record_store, NULL,
1228 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE, 0},
1229 {&handle_zone_to_name, NULL,
1230 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, sizeof (struct ZoneToNameMessage) },
1231 {&handle_iteration_start, NULL,
1232 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage) },
1233 {&handle_iteration_next, NULL,
1234 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, sizeof (struct ZoneIterationNextMessage) },
1235 {&handle_iteration_stop, NULL,
1236 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage) },
1237 {&handle_monitor_start, NULL,
1238 GNUNET_MESSAGE_TYPE_NAMESTORE_MONITOR_START, sizeof (struct ZoneMonitorStartMessage) },
1243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
1245 monitor_nc = GNUNET_SERVER_notification_context_create (server, 1);
1247 /* Loading database plugin */
1249 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
1251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
1253 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
1254 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
1255 GNUNET_free (database);
1256 if (NULL == GSN_database)
1258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1259 "Could not load database backend `%s'\n",
1261 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
1265 /* Configuring server handles */
1266 GNUNET_SERVER_add_handlers (server, handlers);
1267 snc = GNUNET_SERVER_notification_context_create (server, 16);
1268 GNUNET_SERVER_disconnect_notify (server,
1269 &client_disconnect_notification,
1271 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1277 * The main function for the template service.
1279 * @param argc number of arguments from the command line
1280 * @param argv command line arguments
1281 * @return 0 ok, 1 on error
1284 main (int argc, char *const *argv)
1286 return (GNUNET_OK ==
1287 GNUNET_SERVICE_run (argc, argv, "namestore",
1288 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1291 /* end of gnunet-service-namestore.c */