PSYCstore SQLite backend; API fixes/enhancements
[oweals/gnunet.git] / src / psycstore / gnunet-service-psycstore.c
1 /*
2   This file is part of GNUnet.
3   (C) 2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
22  * @file psycstore/gnunet-service-psycstore.c
23  * @brief PSYCstore service
24  * @author Gabor X Toth
25  * @author Christian Grothoff
26  *
27  * The purpose of this service is to manage private keys that
28  * represent the various egos/pseudonyms/identities of a GNUnet user.
29  *
30  */
31 #include "platform.h"
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"
39
40
41 /**
42  * Handle to our current configuration.
43  */
44 static const struct GNUNET_CONFIGURATION_Handle *cfg;
45
46 /**
47  * Handle to the statistics service.
48  */
49 static struct GNUNET_STATISTICS_Handle *stats;
50
51 /**
52  * Notification context, simplifies client broadcasts.
53  */
54 static struct GNUNET_SERVER_NotificationContext *nc;
55
56 /**
57  * Database handle
58  */
59 static struct GNUNET_PSYCSTORE_PluginFunctions *db;
60
61 /**
62  * Name of the database plugin
63  */
64 static char *db_lib_name;
65
66
67 /**
68  * Task run during shutdown.
69  *
70  * @param cls unused
71  * @param tc unused
72  */
73 static void
74 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
75 {
76   if (NULL != nc)
77   {
78     GNUNET_SERVER_notification_context_destroy (nc);
79     nc = NULL;
80   }
81   if (NULL != stats)
82   {
83     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
84     stats = NULL;
85   }
86   GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
87   GNUNET_free (db_lib_name);
88   db_lib_name = NULL;
89 }
90
91
92 /**
93  * Send a result code back to the client.
94  *
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)
98  */
99 static void
100 send_result_code (struct GNUNET_SERVER_Client *client,
101                   uint32_t result_code,
102                   const char *emsg)
103 {
104   struct ResultCodeMessage *rcm;
105   size_t elen;
106
107   if (NULL == emsg)
108     elen = 0;
109   else
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",
118               (int) result_code,
119               emsg);
120   GNUNET_SERVER_notification_context_unicast (nc, client, &rcm->header,
121                                               GNUNET_NO);
122   GNUNET_free (rcm);
123 }
124
125
126 static void
127 handle_membership_store (void *cls,
128                            struct GNUNET_SERVER_Client *client,
129                            const struct GNUNET_MessageHeader *message)
130 {
131   const struct MembershipStoreMessage *msg =
132     (const struct MembershipStoreMessage *) message;
133
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);
137
138   if (res != GNUNET_OK)
139     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
140                 _("Failed to store membership information!\n"));
141
142   send_result_code (client, res, NULL);
143 }
144
145
146 static void
147 handle_membership_test (void *cls,
148                         struct GNUNET_SERVER_Client *client,
149                         const struct GNUNET_MessageHeader *message)
150 {
151   const struct MembershipTestMessage *msg =
152     (const struct MembershipTestMessage *) message;
153
154   int res = db->membership_test (db->cls, msg->channel_key, msg->slave_key,
155                                  msg->message_id);
156   switch (res)
157   {
158   case GNUNET_YES:
159   case GNUNET_NO:
160     break;
161   default:
162     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
163                 _("Failed to test membership!\n"));
164   }
165
166   send_result_code (client, res, NULL);
167 }
168
169
170 /**
171  * Handle PSYCstore clients.
172  *
173  * @param cls closure
174  * @param server the initialized server
175  * @param c configuration to use
176  */
177 static void
178 run (void *cls,
179      struct GNUNET_SERVER_Handle *server,
180      const struct GNUNET_CONFIGURATION_Handle *c)
181 {
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)},
189     {NULL, NULL, 0, 0}
190   };
191
192   cfg = c;
193
194   /* Loading database plugin */
195   char *database;
196   if (GNUNET_OK !=
197       GNUNET_CONFIGURATION_get_value_string (cfg, "psycstore", "database",
198                                              &database))
199   {
200     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
201   }
202   else
203   {
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);
207   }
208   if (NULL == db)
209   {
210     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
211                 "Could not load database backend `%s'\n",
212                 db_lib_name);
213     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
214     return;
215   }
216
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,
221                                 NULL);
222 }
223
224
225 /**
226  * The main function for the network size estimation service.
227  *
228  * @param argc number of arguments from the command line
229  * @param argv command line arguments
230  * @return 0 ok, 1 on error
231  */
232 int
233 main (int argc, char *const *argv)
234 {
235   return (GNUNET_OK ==
236           GNUNET_SERVICE_run (argc, argv, "psycstore",
237                               GNUNET_SERVICE_OPTION_NONE,
238                               &run, NULL)) ? 0 : 1;
239 }
240
241
242 /* end of gnunet-service-psycstore.c */