2 This file is part of GNUnet.
3 (C) 2009 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
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_service_lib.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_namestore_plugin.h"
31 #include "namestore.h"
36 * A namestore operation.
38 struct GNUNET_NAMESTORE_ZoneIteration
40 struct GNUNET_NAMESTORE_ZoneIteration *next;
41 struct GNUNET_NAMESTORE_ZoneIteration *prev;
43 struct GNUNET_NAMESTORE_Client * client;
56 struct GNUNET_NAMESTORE_Client
58 struct GNUNET_NAMESTORE_Client *next;
59 struct GNUNET_NAMESTORE_Client *prev;
61 struct GNUNET_SERVER_Client * client;
63 struct GNUNET_NAMESTORE_ZoneIteration *op_head;
64 struct GNUNET_NAMESTORE_ZoneIteration *op_tail;
70 * Configuration handle.
72 const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
74 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
77 * Our notification context.
79 static struct GNUNET_SERVER_NotificationContext *snc;
81 static char *db_lib_name;
83 static struct GNUNET_NAMESTORE_Client *client_head;
84 static struct GNUNET_NAMESTORE_Client *client_tail;
88 * Task run during shutdown.
94 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
96 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping namestore service\n");
98 struct GNUNET_NAMESTORE_ZoneIteration * no;
99 struct GNUNET_NAMESTORE_ZoneIteration * tmp;
100 struct GNUNET_NAMESTORE_Client * nc;
101 struct GNUNET_NAMESTORE_Client * next;
103 for (nc = client_head; nc != NULL; nc = next)
106 for (no = nc->op_head; no != NULL; no = tmp)
108 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
113 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
118 GNUNET_SERVER_notification_context_destroy (snc);
121 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
122 GNUNET_free (db_lib_name);
125 static struct GNUNET_NAMESTORE_Client *
126 client_lookup (struct GNUNET_SERVER_Client *client)
128 struct GNUNET_NAMESTORE_Client * nc;
130 GNUNET_assert (NULL != client);
132 for (nc = client_head; nc != NULL; nc = nc->next)
134 if (client == nc->client)
142 * Called whenever a client is disconnected. Frees our
143 * resources associated with that client.
146 * @param client identification of the client
149 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
151 struct GNUNET_NAMESTORE_ZoneIteration * no;
152 struct GNUNET_NAMESTORE_Client * nc;
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client);
158 nc = client_lookup (client);
160 if ((NULL == client) || (NULL == nc))
163 for (no = nc->op_head; no != NULL; no = no->next)
165 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
169 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
173 static void handle_start (void *cls,
174 struct GNUNET_SERVER_Client * client,
175 const struct GNUNET_MessageHeader * message)
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client);
179 struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client));
181 GNUNET_SERVER_notification_context_add (snc, client);
182 GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc);
184 GNUNET_SERVER_receive_done (client, GNUNET_OK);
187 struct LookupNameContext
189 struct GNUNET_NAMESTORE_Client *nc;
191 uint32_t record_type;
198 handle_lookup_name_it (void *cls,
199 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
200 struct GNUNET_TIME_Absolute expire,
202 unsigned int rd_count,
203 const struct GNUNET_NAMESTORE_RecordData *rd,
204 const struct GNUNET_CRYPTO_RsaSignature *signature)
207 struct LookupNameContext *lnc = cls;
208 struct LookupNameResponseMessage *lnr_msg;
210 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key_tmp;
211 struct GNUNET_NAMESTORE_RecordData *rd_selected = NULL;
216 struct GNUNET_CRYPTO_RsaSignature *signature_tmp;
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
224 name_len = strlen(name) + 1;
226 int copied_elements = 0;
227 int contains_signature = 0;
230 /* count records to copy */
233 if (lnc->record_type != 0)
235 /* special record type needed */
236 for (c = 0; c < rd_count; c ++)
237 if (rd[c].record_type == lnc->record_type)
238 copied_elements++; /* found matching record */
239 rd_selected = GNUNET_malloc (copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData));
241 for (c = 0; c < rd_count; c ++)
243 if (rd[c].record_type == lnc->record_type)
245 /* found matching record */
246 memcpy (&rd_selected[copied_elements], &rd[c], sizeof (struct GNUNET_NAMESTORE_RecordData));
253 copied_elements = rd_count;
254 rd_selected = (struct GNUNET_NAMESTORE_RecordData *) rd;
256 rd_ser_len = GNUNET_NAMESTORE_records_serialize(&rd_ser, copied_elements, rd_selected);
267 if ((copied_elements == rd_count) && (signature != NULL))
268 contains_signature = GNUNET_YES;
270 if (rd_selected != rd)
271 GNUNET_free (rd_selected);
273 r_size = sizeof (struct LookupNameResponseMessage) +
274 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
277 contains_signature * sizeof (struct GNUNET_CRYPTO_RsaSignature);
279 lnr_msg = GNUNET_malloc (r_size);
281 lnr_msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
282 lnr_msg->header.size = ntohs (r_size);
283 lnr_msg->op_id = htonl (lnc->id);
284 lnr_msg->rd_len = htonl (rd_ser_len);
285 lnr_msg->name_len = htons (name_len);
286 lnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
287 lnr_msg->contains_sig = htons (contains_signature);
289 zone_key_tmp = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &lnr_msg[1];
290 name_tmp = (char *) &zone_key_tmp[1];
291 rd_tmp = &name_tmp[name_len];
292 signature_tmp = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[rd_ser_len];
294 if (zone_key != NULL)
295 memcpy (zone_key_tmp, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
298 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
299 memset (&dummy, '0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
300 memcpy (zone_key_tmp, &dummy, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
302 memcpy (name_tmp, name, name_len);
303 memcpy (rd_tmp, rd_ser, rd_ser_len);
304 GNUNET_free_non_null (rd_ser);
306 if (GNUNET_YES == contains_signature)
307 memcpy (signature_tmp, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature));
308 GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
310 GNUNET_free (lnr_msg);
313 static void handle_lookup_name (void *cls,
314 struct GNUNET_SERVER_Client * client,
315 const struct GNUNET_MessageHeader * message)
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
318 struct LookupNameContext lnc;
319 struct GNUNET_NAMESTORE_Client *nc;
326 if (ntohs (message->size) < sizeof (struct LookupNameMessage))
329 GNUNET_SERVER_receive_done (client, GNUNET_OK);
333 nc = client_lookup(client);
337 GNUNET_SERVER_receive_done (client, GNUNET_OK);
341 struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
342 id = ntohl (ln_msg->op_id);
343 name_len = ntohl (ln_msg->name_len);
344 type = ntohl (ln_msg->record_type);
346 if ((name_len == 0) || (name_len > 256))
349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
353 name = (char *) &ln_msg[1];
354 if (name[name_len -1] != '\0')
357 GNUNET_SERVER_receive_done (client, GNUNET_OK);
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up record for name `%s'\n", name);
363 /* do the actual lookup */
366 lnc.record_type = type;
367 res = GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, name, 0, &handle_lookup_name_it, &lnc);
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ITERATE RESULTS: %u\n" , res);
371 GNUNET_SERVER_receive_done (client, GNUNET_OK);
374 static void handle_record_put (void *cls,
375 struct GNUNET_SERVER_Client * client,
376 const struct GNUNET_MessageHeader * message)
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_PUT");
379 struct GNUNET_NAMESTORE_Client *nc;
380 struct GNUNET_TIME_Absolute expire;
381 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
382 struct GNUNET_NAMESTORE_RecordData *rd;
383 struct GNUNET_CRYPTO_RsaSignature *signature;
384 struct RecordPutResponseMessage rpr_msg;
393 int res = GNUNET_SYSERR;
395 if (ntohs (message->size) < sizeof (struct RecordPutMessage))
398 GNUNET_SERVER_receive_done (client, GNUNET_OK);
402 nc = client_lookup (client);
406 GNUNET_SERVER_receive_done (client, GNUNET_OK);
410 struct RecordPutMessage * rp_msg = (struct RecordPutMessage *) message;
411 id = ntohl (rp_msg->op_id);
412 name_len = ntohs (rp_msg->name_len);
413 rd_ser_len = ntohs(rp_msg->rd_len);
414 msg_size = ntohs (message->size);
415 msg_size_exp = sizeof (struct RecordPutMessage) + sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + name_len + rd_ser_len;
417 if (msg_size != msg_size_exp)
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
421 GNUNET_SERVER_receive_done (client, GNUNET_OK);
426 if ((name_len == 0) || (name_len > 256))
429 GNUNET_SERVER_receive_done (client, GNUNET_OK);
433 zone_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &rp_msg[1];
434 name = (char *) &zone_key[1];
435 expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
436 signature = (struct GNUNET_CRYPTO_RsaSignature *) &rp_msg->signature;
437 rd_ser = &name[name_len];
438 rd_count = GNUNET_NAMESTORE_records_deserialize(&rd, rd_ser, rd_ser_len);
440 /* Database operation */
441 res = GSN_database->put_records(GSN_database->cls,
448 GNUNET_NAMESTORE_records_free (rd_count, rd);
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting record for name `%s': %s\n",
451 name, (res == GNUNET_OK) ? "OK" : "FAIL");
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
456 rpr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
457 rpr_msg.op_id = rp_msg->op_id;
458 rpr_msg.header.size = htons (sizeof (struct RecordPutResponseMessage));
459 if (GNUNET_OK == res)
460 rpr_msg.op_result = htons (GNUNET_OK);
462 rpr_msg.op_result = htons (GNUNET_NO);
463 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
465 GNUNET_SERVER_receive_done (client, GNUNET_OK);
469 static void handle_record_create (void *cls,
470 struct GNUNET_SERVER_Client * client,
471 const struct GNUNET_MessageHeader * message)
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE");
474 struct GNUNET_NAMESTORE_Client *nc;
475 struct RecordCreateResponseMessage rcr_msg;
481 int res = GNUNET_SYSERR;
483 if (ntohs (message->size) < sizeof (struct RecordCreateMessage))
486 GNUNET_SERVER_receive_done (client, GNUNET_OK);
490 nc = client_lookup(client);
494 GNUNET_SERVER_receive_done (client, GNUNET_OK);
498 struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message;
499 id = ntohl (rp_msg->op_id);
500 name_len = ntohs (rp_msg->name_len);
501 msg_size = ntohs (message->size);
502 msg_size_exp = sizeof (struct RecordCreateMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData);
504 if (msg_size != msg_size_exp)
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
508 GNUNET_SERVER_receive_done (client, GNUNET_OK);
513 if ((name_len == 0) || (name_len > 256))
516 GNUNET_SERVER_receive_done (client, GNUNET_OK);
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE");
525 rcr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE);
526 rcr_msg.op_id = rp_msg->op_id;
527 rcr_msg.header.size = htons (sizeof (struct RecordCreateResponseMessage));
528 if (GNUNET_OK == res)
529 rcr_msg.op_result = htons (GNUNET_OK);
531 rcr_msg.op_result = htons (GNUNET_NO);
532 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO);
534 GNUNET_SERVER_receive_done (client, GNUNET_OK);
537 static void handle_record_remove (void *cls,
538 struct GNUNET_SERVER_Client * client,
539 const struct GNUNET_MessageHeader * message)
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE");
542 struct GNUNET_NAMESTORE_Client *nc;
543 struct RecordRemoveResponseMessage rrr_msg;
549 int res = GNUNET_SYSERR;
551 if (ntohs (message->size) < sizeof (struct RecordRemoveMessage))
554 GNUNET_SERVER_receive_done (client, GNUNET_OK);
558 nc = client_lookup(client);
562 GNUNET_SERVER_receive_done (client, GNUNET_OK);
566 struct RecordRemoveMessage * rp_msg = (struct RecordRemoveMessage *) message;
567 id = ntohl (rp_msg->op_id);
568 name_len = ntohs (rp_msg->name_len);
569 msg_size = ntohs (message->size);
570 msg_size_exp = sizeof (struct RecordRemoveMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData);
572 if (msg_size != msg_size_exp)
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size);
576 GNUNET_SERVER_receive_done (client, GNUNET_OK);
581 if ((name_len == 0) || (name_len > 256))
584 GNUNET_SERVER_receive_done (client, GNUNET_OK);
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
593 rrr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
594 rrr_msg.op_id = rp_msg->op_id;
595 rrr_msg.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
596 if (GNUNET_OK == res)
597 rrr_msg.op_result = htons (GNUNET_OK);
599 rrr_msg.op_result = htons (GNUNET_NO);
600 GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
602 GNUNET_SERVER_receive_done (client, GNUNET_OK);
605 struct ZoneIterationProcResult
608 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
611 struct GNUNET_CRYPTO_RsaSignature signature;
612 struct GNUNET_TIME_Absolute expire;
622 void zone_iteration_proc (void *cls,
623 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
624 struct GNUNET_TIME_Absolute expire,
626 unsigned int rd_count,
627 const struct GNUNET_NAMESTORE_RecordData *rd,
628 const struct GNUNET_CRYPTO_RsaSignature *signature)
630 struct ZoneIterationProcResult *zipr = cls;
632 if (zone_key != NULL)
634 zipr->zone_key = *zone_key;
635 zipr->have_zone_key = GNUNET_YES;
638 zipr->have_zone_key = GNUNET_NO;
640 zipr->expire = expire;
644 memcpy (zipr->name, name, strlen(name) + 1);
645 zipr->have_name = GNUNET_YES;
648 zipr->have_name = GNUNET_NO;
650 if (signature != NULL)
652 zipr->signature = *signature;
653 zipr->have_signature = GNUNET_YES;
656 zipr->have_signature = GNUNET_NO;
658 if ((rd_count > 0) && (rd != NULL))
660 len = GNUNET_NAMESTORE_records_serialize (&zipr->rd_ser, rd_count, rd);
661 zipr->rd_ser_len = len;
665 static void handle_iteration_start (void *cls,
666 struct GNUNET_SERVER_Client * client,
667 const struct GNUNET_MessageHeader * message)
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_START");
671 struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
672 struct GNUNET_NAMESTORE_Client *nc;
673 struct GNUNET_NAMESTORE_ZoneIteration *zi;
674 struct ZoneIterationResponseMessage zir_msg;
675 struct ZoneIterationProcResult zipr;
678 nc = client_lookup(client);
682 GNUNET_SERVER_receive_done (client, GNUNET_OK);
686 zi = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_ZoneIteration));
687 zi->op_id = ntohl (zis_msg->op_id);
690 zi->zone = zis_msg->zone;
692 GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
694 res = GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, &zipr);
695 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "iterate_records result %u\n", res);
698 /* GNUNET_OK on success */
702 /* GNUNET_SYSERR on error */
705 /* GNUNET_NO if there were no results, */
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_RESPONSE");
714 zir_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
715 zir_msg.op_id = htonl(zi->op_id);
716 zir_msg.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
719 GNUNET_SERVER_notification_context_unicast (snc, zi->client->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
722 GNUNET_SERVER_receive_done (client, GNUNET_OK);
725 static void handle_iteration_stop (void *cls,
726 struct GNUNET_SERVER_Client * client,
727 const struct GNUNET_MessageHeader * message)
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_STOP");
731 struct GNUNET_NAMESTORE_Client *nc;
732 struct GNUNET_NAMESTORE_ZoneIteration *zi;
733 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
736 nc = client_lookup(client);
740 GNUNET_SERVER_receive_done (client, GNUNET_OK);
744 id = ntohl (zis_msg->op_id);
745 for (zi = nc->op_head; zi != NULL; zi = zi->next)
753 GNUNET_SERVER_receive_done (client, GNUNET_OK);
757 GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
761 GNUNET_SERVER_receive_done (client, GNUNET_OK);
764 static void handle_iteration_next (void *cls,
765 struct GNUNET_SERVER_Client * client,
766 const struct GNUNET_MessageHeader * message)
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
770 struct GNUNET_NAMESTORE_Client *nc;
771 struct GNUNET_NAMESTORE_ZoneIteration *zi;
772 struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
776 nc = client_lookup(client);
780 GNUNET_SERVER_receive_done (client, GNUNET_OK);
784 id = ntohl (zis_msg->op_id);
785 for (zi = nc->op_head; zi != NULL; zi = zi->next)
793 GNUNET_SERVER_receive_done (client, GNUNET_OK);
798 res = GSN_database->iterate_records (GSN_database->cls, &zi->zone, NULL, zi->offset , &zone_iteration_proc, zi);
804 * Process template requests.
807 * @param server the initialized server
808 * @param cfg configuration to use
811 run (void *cls, struct GNUNET_SERVER_Handle *server,
812 const struct GNUNET_CONFIGURATION_Handle *cfg)
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
818 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
819 {&handle_start, NULL,
820 GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
821 {&handle_lookup_name, NULL,
822 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
823 {&handle_record_put, NULL,
824 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0},
825 {&handle_record_create, NULL,
826 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0},
827 {&handle_record_remove, NULL,
828 GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0},
829 {&handle_iteration_start, NULL,
830 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START, sizeof (struct ZoneIterationStartMessage)},
831 {&handle_iteration_stop, NULL,
832 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP, sizeof (struct ZoneIterationStopMessage)},
833 {&handle_iteration_next, NULL,
834 GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT, 0},
840 /* Loading database plugin */
842 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
844 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
846 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
847 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
848 if (GSN_database == NULL)
849 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
851 GNUNET_free (database);
853 /* Configuring server handles */
854 GNUNET_SERVER_add_handlers (server, handlers);
855 snc = GNUNET_SERVER_notification_context_create (server, 16);
856 GNUNET_SERVER_disconnect_notify (server,
857 &client_disconnect_notification,
860 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
867 * The main function for the template service.
869 * @param argc number of arguments from the command line
870 * @param argv command line arguments
871 * @return 0 ok, 1 on error
874 main (int argc, char *const *argv)
877 GNUNET_SERVICE_run (argc, argv, "namestore",
878 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
881 /* end of gnunet-service-namestore.c */