PEERSTORE store function
[oweals/gnunet.git] / src / peerstore / gnunet-service-peerstore.c
1 /*
2      This file is part of GNUnet.
3      (C) 
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 peerstore/gnunet-service-peerstore.c
23  * @brief peerstore service implementation
24  * @author Omar Tarabai
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "peerstore.h"
29 #include "gnunet_peerstore_plugin.h"
30
31 //TODO: GNUNET_SERVER_receive_done() ?
32 //TODO: implement value lifetime
33
34 /**
35  * Our configuration.
36  */
37 static const struct GNUNET_CONFIGURATION_Handle *cfg;
38
39 /**
40  * Database plugin library name
41  */
42 char *db_lib_name;
43
44 /**
45  * Database handle
46  */
47 static struct GNUNET_PEERSTORE_PluginFunctions *db;
48
49 /**
50  * Task run during shutdown.
51  *
52  * @param cls unused
53  * @param tc unused
54  */
55 static void
56 shutdown_task (void *cls,
57                const struct GNUNET_SCHEDULER_TaskContext *tc)
58 {
59   if(NULL != db_lib_name)
60   {
61     GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db));
62     GNUNET_free (db_lib_name);
63     db_lib_name = NULL;
64   }
65 }
66
67
68 /**
69  * A client disconnected.  Remove all of its data structure entries.
70  *
71  * @param cls closure, NULL
72  * @param client identification of the client
73  */
74 static void
75 handle_client_disconnect (void *cls,
76                           struct GNUNET_SERVER_Client
77                           * client)
78 {
79 }
80
81 /**
82  * Handle a store request from client
83  *
84  * @param cls unused
85  * @param client identification of the client
86  * @param message the actual message
87  */
88 void handle_store (void *cls,
89     struct GNUNET_SERVER_Client *client,
90     const struct GNUNET_MessageHeader *message)
91 {
92   struct StoreRequestMessage *req;
93   uint16_t msg_size;
94   uint16_t sub_system_size;
95   uint16_t value_size;
96   char *sub_system;
97   void *value;
98   struct GNUNET_SERVER_TransmitContext *tc;
99   struct StoreResponseMessage *res;
100   char *emsg;
101   size_t emsg_size = 0;
102   char *emsg_dest;
103
104   msg_size = ntohs(message->size);
105   if(msg_size < sizeof(struct StoreRequestMessage))
106   {
107     GNUNET_break(0);
108     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
109     return;
110   }
111   req = (struct StoreRequestMessage *)message;
112   sub_system_size = ntohs(req->sub_system_size);
113   value_size = ntohs(req->value_size);
114   if(sub_system_size + value_size + sizeof(struct StoreRequestMessage)
115       != msg_size)
116   {
117     GNUNET_break(0);
118     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
119     return;
120   }
121   sub_system = (char *)&req[1];
122   value = sub_system + sub_system_size;
123   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s' and peer `%s'\n",
124       msg_size,
125       sub_system,
126       GNUNET_i2s (&req->peer));
127   if(GNUNET_OK != db->store_record(db->cls,
128       &req->peer,
129      sub_system,
130      value,
131      value_size))
132   {
133     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
134     emsg = _("Failed to store requested value, sqlite database error.");
135     emsg_size = strlen(emsg) + 1;
136   }
137   res = GNUNET_malloc(sizeof(struct StoreResponseMessage) + emsg_size);
138   res->emsg_size = htons(emsg_size);
139   res->header.size = htons(sizeof(struct StoreResponseMessage) + emsg_size);
140   res->header.type = htons(GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT);
141   if(emsg_size > 0)
142   {
143     res->success = htons(GNUNET_NO);
144     emsg_dest = (char *)&res[1];
145     memcpy(emsg_dest, emsg, emsg_size);
146   }
147   else
148     res->success = htons(GNUNET_YES);
149   tc = GNUNET_SERVER_transmit_context_create (client);
150   GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)res);
151   GNUNET_free(res);
152   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
153
154 }
155
156 /**
157  * Peerstore service runner.
158  *
159  * @param cls closure
160  * @param server the initialized server
161  * @param c configuration to use
162  */
163 static void
164 run (void *cls,
165      struct GNUNET_SERVER_Handle *server,
166      const struct GNUNET_CONFIGURATION_Handle *c)
167 {
168   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
169       {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
170       {NULL, NULL, 0, 0}
171   };
172   char *database;
173
174   cfg = c;
175   if (GNUNET_OK !=
176         GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
177                                                &database))
178     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
179
180   else
181   {
182     GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
183     db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
184     GNUNET_free(database);
185   }
186   if(NULL == db)
187           GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
188   else
189   {
190     GNUNET_SERVER_add_handlers (server, handlers);
191     GNUNET_SERVER_disconnect_notify (server,
192              &handle_client_disconnect,
193              NULL);
194   }
195   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
196                                 &shutdown_task,
197                                 NULL);
198 }
199
200
201 /**
202  * The main function for the peerstore service.
203  *
204  * @param argc number of arguments from the command line
205  * @param argv command line arguments
206  * @return 0 ok, 1 on error
207  */
208 int
209 main (int argc, char *const *argv)
210 {
211   return (GNUNET_OK ==
212           GNUNET_SERVICE_run (argc,
213                               argv,
214                               "peerstore",
215                               GNUNET_SERVICE_OPTION_NONE,
216                               &run, NULL)) ? 0 : 1;
217 }
218
219 /* end of gnunet-service-peerstore.c */