psycstore: sqlite plugin
[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 GNUNET_PSYCSTORE_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 GNUNET_PSYCSTORE_ResultCodeMessage) + elen);
112   rcm->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE);
113   rcm->header.size = htons (sizeof (struct GNUNET_PSYCSTORE_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, GNUNET_NO);
121   GNUNET_free (rcm);
122 }
123
124
125 /**
126  * Handle PSYCstore clients.
127  *
128  * @param cls closure
129  * @param server the initialized server
130  * @param c configuration to use
131  */
132 static void
133 run (void *cls,
134      struct GNUNET_SERVER_Handle *server,
135      const struct GNUNET_CONFIGURATION_Handle *c)
136 {
137   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
138     {NULL, NULL, 0, 0}
139   };
140
141   cfg = c;
142
143   /* Loading database plugin */
144   char *database;
145   if (GNUNET_OK !=
146       GNUNET_CONFIGURATION_get_value_string (cfg, "psycstore", "database",
147                                              &database))
148   {
149     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
150   }
151   else
152   {
153     GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_psycstore_%s", database);
154     db = GNUNET_PLUGIN_load (db_lib_name, (void *) cfg);
155     GNUNET_free (database);
156   }
157   if (NULL == db)
158   {
159     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160                 "Could not load database backend `%s'\n",
161                 db_lib_name);
162     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
163     return;
164   }
165
166   stats = GNUNET_STATISTICS_create ("psycstore", cfg);
167   GNUNET_SERVER_add_handlers (server, handlers);
168   nc = GNUNET_SERVER_notification_context_create (server, 1);
169   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
170                                 NULL);
171 }
172
173
174 /**
175  * The main function for the network size estimation service.
176  *
177  * @param argc number of arguments from the command line
178  * @param argv command line arguments
179  * @return 0 ok, 1 on error
180  */
181 int
182 main (int argc, char *const *argv)
183 {
184   return (GNUNET_OK ==
185           GNUNET_SERVICE_run (argc, argv, "psycstore",
186                               GNUNET_SERVICE_OPTION_NONE,
187                               &run, NULL)) ? 0 : 1;
188 }
189
190
191 /* end of gnunet-service-psycstore.c */