PEERSTORE api overhaul
[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 req_size;
94   uint16_t ss_size;
95   uint16_t key_size;
96   uint16_t value_size;
97   char *sub_system;
98   char *key;
99   void *value;
100   uint16_t response_type;
101   struct GNUNET_SERVER_TransmitContext *tc;
102
103   req_size = ntohs(message->size);
104   if(req_size < sizeof(struct StoreRequestMessage))
105   {
106     GNUNET_break(0);
107     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
108     return;
109   }
110   req = (struct StoreRequestMessage *)message;
111   ss_size = ntohs(req->sub_system_size);
112   key_size = ntohs(req->key_size);
113   value_size = ntohs(req->value_size);
114   if(ss_size + key_size + value_size + sizeof(struct StoreRequestMessage)
115       != req_size)
116   {
117     GNUNET_break(0);
118     GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
119     return;
120   }
121   sub_system = (char *)&req[1];
122   key = sub_system + ss_size;
123   value = key + key_size;
124   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a store request (size: %lu) for sub system `%s', peer `%s', key `%s'\n",
125       value_size,
126       sub_system,
127       GNUNET_i2s (&req->peer),
128       key);
129   if(GNUNET_OK == db->store_record(db->cls,
130       sub_system,
131       &req->peer,
132       key,
133       value,
134       value_size))
135   {
136     response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK;
137   }
138   else
139   {
140     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to store requested value, sqlite database error.");
141     response_type = GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL;
142   }
143
144   tc = GNUNET_SERVER_transmit_context_create (client);
145   GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, response_type);
146   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
147
148 }
149
150 /**
151  * Peerstore service runner.
152  *
153  * @param cls closure
154  * @param server the initialized server
155  * @param c configuration to use
156  */
157 static void
158 run (void *cls,
159      struct GNUNET_SERVER_Handle *server,
160      const struct GNUNET_CONFIGURATION_Handle *c)
161 {
162   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
163       {&handle_store, NULL, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, 0},
164       {NULL, NULL, 0, 0}
165   };
166   char *database;
167
168   cfg = c;
169   if (GNUNET_OK !=
170         GNUNET_CONFIGURATION_get_value_string (cfg, "peerstore", "DATABASE",
171                                                &database))
172     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
173
174   else
175   {
176     GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
177     db = GNUNET_PLUGIN_load(db_lib_name, (void *) cfg);
178     GNUNET_free(database);
179   }
180   if(NULL == db)
181           GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
182   else
183   {
184     GNUNET_SERVER_add_handlers (server, handlers);
185     GNUNET_SERVER_disconnect_notify (server,
186              &handle_client_disconnect,
187              NULL);
188   }
189   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
190                                 &shutdown_task,
191                                 NULL);
192 }
193
194
195 /**
196  * The main function for the peerstore service.
197  *
198  * @param argc number of arguments from the command line
199  * @param argv command line arguments
200  * @return 0 ok, 1 on error
201  */
202 int
203 main (int argc, char *const *argv)
204 {
205   return (GNUNET_OK ==
206           GNUNET_SERVICE_run (argc,
207                               argv,
208                               "peerstore",
209                               GNUNET_SERVICE_OPTION_NONE,
210                               &run, NULL)) ? 0 : 1;
211 }
212
213 /* end of gnunet-service-peerstore.c */