2 This file is part of GNUnet.
3 (C) 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 psycstore/gnunet-service-psycstore.c
23 * @brief PSYCstore service
24 * @author Gabor X Toth
25 * @author Christian Grothoff
27 * The purpose of this service is to manage private keys that
28 * represent the various egos/pseudonyms/identities of a GNUnet user.
32 #include "gnunet_util_lib.h"
33 #include "gnunet_constants.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_statistics_service.h"
36 #include "gnunet_psycstore_service.h"
37 #include "gnunet_psycstore_plugin.h"
38 #include "psycstore.h"
42 * Handle to our current configuration.
44 static const struct GNUNET_CONFIGURATION_Handle *cfg;
47 * Handle to the statistics service.
49 static struct GNUNET_STATISTICS_Handle *stats;
52 * Notification context, simplifies client broadcasts.
54 static struct GNUNET_SERVER_NotificationContext *nc;
59 static struct GNUNET_PSYCSTORE_PluginFunctions *db;
62 * Name of the database plugin
64 static char *db_lib_name;
68 * Task run during shutdown.
74 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
78 GNUNET_SERVER_notification_context_destroy (nc);
83 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
86 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
87 GNUNET_free (db_lib_name);
93 * Send a result code back to the client.
95 * @param client client that should receive the result code
96 * @param result_code code to transmit
97 * @param emsg error message to include (or NULL for none)
100 send_result_code (struct GNUNET_SERVER_Client *client,
101 uint32_t result_code,
104 struct ResultCodeMessage *rcm;
110 elen = strlen (emsg) + 1;
111 rcm = GNUNET_malloc (sizeof (struct ResultCodeMessage) + elen);
112 rcm->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE);
113 rcm->header.size = htons (sizeof (struct ResultCodeMessage) + elen);
114 rcm->result_code = htonl (result_code);
115 memcpy (&rcm[1], emsg, elen);
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117 "Sending result %d (%s) to client\n",
120 GNUNET_SERVER_notification_context_unicast (nc, client, &rcm->header,
127 handle_membership_store (void *cls,
128 struct GNUNET_SERVER_Client *client,
129 const struct GNUNET_MessageHeader *message)
131 const struct MembershipStoreMessage *msg =
132 (const struct MembershipStoreMessage *) message;
134 int res = db->membership_store (db->cls, msg->channel_key, msg->slave_key,
135 msg->did_join, msg->announced_at,
136 msg->effective_since, msg->group_generation);
138 if (res != GNUNET_OK)
139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
140 _("Failed to store membership information!\n"));
142 send_result_code (client, res, NULL);
147 handle_membership_test (void *cls,
148 struct GNUNET_SERVER_Client *client,
149 const struct GNUNET_MessageHeader *message)
151 const struct MembershipTestMessage *msg =
152 (const struct MembershipTestMessage *) message;
154 int res = db->membership_test (db->cls, msg->channel_key, msg->slave_key,
162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
163 _("Failed to test membership!\n"));
166 send_result_code (client, res, NULL);
171 * Handle PSYCstore clients.
174 * @param server the initialized server
175 * @param c configuration to use
179 struct GNUNET_SERVER_Handle *server,
180 const struct GNUNET_CONFIGURATION_Handle *c)
182 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
183 {&handle_membership_store, NULL,
184 GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_STORE,
185 sizeof (struct MembershipStoreMessage)},
186 {&handle_membership_test, NULL,
187 GNUNET_MESSAGE_TYPE_PSYCSTORE_MEMBERSHIP_TEST,
188 sizeof (struct MembershipTestMessage)},
194 /* Loading database plugin */
197 GNUNET_CONFIGURATION_get_value_string (cfg, "psycstore", "database",
200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
204 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_psycstore_%s", database);
205 db = GNUNET_PLUGIN_load (db_lib_name, (void *) cfg);
206 GNUNET_free (database);
210 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
211 "Could not load database backend `%s'\n",
213 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
217 stats = GNUNET_STATISTICS_create ("psycstore", cfg);
218 GNUNET_SERVER_add_handlers (server, handlers);
219 nc = GNUNET_SERVER_notification_context_create (server, 1);
220 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
226 * The main function for the network size estimation service.
228 * @param argc number of arguments from the command line
229 * @param argv command line arguments
230 * @return 0 ok, 1 on error
233 main (int argc, char *const *argv)
236 GNUNET_SERVICE_run (argc, argv, "psycstore",
237 GNUNET_SERVICE_OPTION_NONE,
238 &run, NULL)) ? 0 : 1;
242 /* end of gnunet-service-psycstore.c */