2 This file is part of GNUnet.
3 (C) 2009 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 namestore/gnunet-service-namestore.c
23 * @brief namestore for the GNUnet naming system
24 * @author Matthias Wachs
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_service_lib.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_namestore_plugin.h"
31 #include "namestore.h"
36 * A namestore operation.
38 struct GNUNET_NAMESTORE_Operation
40 struct GNUNET_NAMESTORE_Operation *next;
41 struct GNUNET_NAMESTORE_Operation *prev;
45 char *data; /*stub data pointer*/
52 struct GNUNET_NAMESTORE_Client
54 struct GNUNET_NAMESTORE_Client *next;
55 struct GNUNET_NAMESTORE_Client *prev;
57 struct GNUNET_SERVER_Client * client;
59 struct GNUNET_NAMESTORE_Operation *op_head;
60 struct GNUNET_NAMESTORE_Operation *op_tail;
66 * Configuration handle.
68 const struct GNUNET_CONFIGURATION_Handle *GSN_cfg;
70 static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database;
73 * Our notification context.
75 static struct GNUNET_SERVER_NotificationContext *snc;
77 static char *db_lib_name;
79 static struct GNUNET_NAMESTORE_Client *client_head;
80 static struct GNUNET_NAMESTORE_Client *client_tail;
84 * Task run during shutdown.
90 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping namestore service\n");
94 struct GNUNET_NAMESTORE_Operation * no;
95 struct GNUNET_NAMESTORE_Operation * tmp;
96 struct GNUNET_NAMESTORE_Client * nc;
97 struct GNUNET_NAMESTORE_Client * next;
99 for (nc = client_head; nc != NULL; nc = next)
102 for (no = nc->op_head; no != NULL; no = tmp)
104 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
109 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
114 GNUNET_SERVER_notification_context_destroy (snc);
117 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
118 GNUNET_free (db_lib_name);
121 static struct GNUNET_NAMESTORE_Client *
122 client_lookup (struct GNUNET_SERVER_Client *client)
124 struct GNUNET_NAMESTORE_Client * nc;
126 GNUNET_assert (NULL != client);
128 for (nc = client_head; nc != NULL; nc = nc->next)
130 if (client == nc->client)
138 * Called whenever a client is disconnected. Frees our
139 * resources associated with that client.
142 * @param client identification of the client
145 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
147 struct GNUNET_NAMESTORE_Operation * no;
148 struct GNUNET_NAMESTORE_Client * nc;
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client);
154 nc = client_lookup (client);
156 if ((NULL == client) || (NULL == nc))
159 for (no = nc->op_head; no != NULL; no = no->next)
161 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
165 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
169 static void handle_start (void *cls,
170 struct GNUNET_SERVER_Client * client,
171 const struct GNUNET_MessageHeader * message)
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client);
175 struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client));
177 GNUNET_SERVER_notification_context_add (snc, client);
178 GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc);
180 GNUNET_SERVER_receive_done (client, GNUNET_OK);
183 struct LookupNameContext
185 struct GNUNET_NAMESTORE_Client *nc;
187 uint32_t record_type;
192 handle_lookup_name_it (void *cls,
193 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
194 struct GNUNET_TIME_Absolute expire,
196 unsigned int rd_count,
197 const struct GNUNET_NAMESTORE_RecordData *rd,
198 const struct GNUNET_CRYPTO_RsaSignature *signature)
201 struct LookupNameContext *lnc = cls;
202 struct LookupNameResponseMessage *lnr_msg;
204 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key_tmp;
205 struct GNUNET_NAMESTORE_RecordData * rd_tmp;
207 struct GNUNET_CRYPTO_RsaSignature *signature_tmp;
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
215 name_len = strlen(name) + 1;
217 int copied_elements = 0;
218 int contains_signature = 0;
221 /* count records to copy */
224 if (lnc->record_type != 0)
226 /* special record type needed */
227 for (c = 0; c < rd_count; c ++)
228 if (rd[c].record_type == lnc->record_type)
229 copied_elements++; /* found matching record */
232 copied_elements = rd_count;
235 if ((copied_elements == rd_count) && (signature != NULL))
236 contains_signature = GNUNET_YES;
238 r_size = sizeof (struct LookupNameResponseMessage) +
239 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
241 copied_elements * sizeof (struct GNUNET_NAMESTORE_RecordData) +
242 contains_signature * sizeof (struct GNUNET_CRYPTO_RsaSignature);
244 lnr_msg = GNUNET_malloc (r_size);
246 lnr_msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE);
247 lnr_msg->header.size = ntohs (r_size);
248 lnr_msg->op_id = htonl (lnc->id);
249 lnr_msg->rc_count = htonl (copied_elements);
250 lnr_msg->name_len = htons (name_len);
251 lnr_msg->expire = GNUNET_TIME_absolute_hton(expire);
252 lnr_msg->contains_sig = htons (contains_signature);
255 zone_key_tmp = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) &lnr_msg[1];
256 name_tmp = (char *) &zone_key_tmp[1];
257 rd_tmp = (struct GNUNET_NAMESTORE_RecordData *) &name_tmp[name_len];
258 signature_tmp = (struct GNUNET_CRYPTO_RsaSignature *) &rd_tmp[copied_elements];
260 if (zone_key != NULL)
261 memcpy (zone_key_tmp, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
264 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded dummy;
265 memset (&dummy, '0', sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
266 memcpy (zone_key_tmp, &dummy, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
268 memcpy (name_tmp, name, name_len);
273 if (lnc->record_type != 0)
275 /* special record type needed */
276 for (c = 0; c < rd_count; c ++)
277 if (rd[c].record_type == lnc->record_type)
279 /* found matching record */
280 memcpy (&rd_tmp[copied_elements], &rd[c], rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
285 memcpy (rd_tmp, rd, rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
288 if (GNUNET_YES == contains_signature)
289 memcpy (signature_tmp, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature));
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DONE `%s' message\n", "NAMESTORE_LOOKUP_NAME_RESPONSE");
291 GNUNET_SERVER_notification_context_unicast (snc, lnc->nc->client, (const struct GNUNET_MessageHeader *) lnr_msg, GNUNET_NO);
293 GNUNET_free (lnr_msg);
296 static void handle_lookup_name (void *cls,
297 struct GNUNET_SERVER_Client * client,
298 const struct GNUNET_MessageHeader * message)
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
301 struct LookupNameContext lnc;
302 struct GNUNET_NAMESTORE_Client *nc;
303 GNUNET_HashCode name_hash;
310 if (ntohs (message->size) < sizeof (struct LookupNameMessage))
313 GNUNET_SERVER_receive_done (client, GNUNET_OK);
317 nc = client_lookup(client);
321 GNUNET_SERVER_receive_done (client, GNUNET_OK);
325 struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message;
326 id = ntohl (ln_msg->op_id);
327 name_len = ntohl (ln_msg->name_len);
328 type = ntohl (ln_msg->record_type);
330 if ((name_len == 0) || (name_len > 256))
333 GNUNET_SERVER_receive_done (client, GNUNET_OK);
337 name = GNUNET_malloc (name_len);
338 memcpy (name, &ln_msg[1], name_len);
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking up record for name `%s'\n", name);
340 GNUNET_CRYPTO_hash(name, name_len-1, &name_hash);
343 /* do the actual lookup */
346 lnc.record_type = type;
347 GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, &ln_msg->zone, 0, &handle_lookup_name_it, &lnc);
349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
354 * Process template requests.
357 * @param server the initialized server
358 * @param cfg configuration to use
361 run (void *cls, struct GNUNET_SERVER_Handle *server,
362 const struct GNUNET_CONFIGURATION_Handle *cfg)
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
368 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
369 {&handle_start, NULL,
370 GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
371 {&handle_lookup_name, NULL,
372 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
378 /* Loading database plugin */
380 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
384 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
385 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
386 if (GSN_database == NULL)
387 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
389 GNUNET_free (database);
391 /* Configuring server handles */
392 GNUNET_SERVER_add_handlers (server, handlers);
393 snc = GNUNET_SERVER_notification_context_create (server, 16);
394 GNUNET_SERVER_disconnect_notify (server,
395 &client_disconnect_notification,
398 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
405 * The main function for the template service.
407 * @param argc number of arguments from the command line
408 * @param argv command line arguments
409 * @return 0 ok, 1 on error
412 main (int argc, char *const *argv)
415 GNUNET_SERVICE_run (argc, argv, "namestore",
416 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
419 /* end of gnunet-service-namestore.c */