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;
72 static char *db_lib_name;
74 static struct GNUNET_NAMESTORE_Client *client_head;
75 static struct GNUNET_NAMESTORE_Client *client_tail;
79 * Task run during shutdown.
85 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping namestore service\n");
89 struct GNUNET_NAMESTORE_Operation * no;
90 struct GNUNET_NAMESTORE_Client * nc;
92 for (nc = client_head; nc != NULL; nc = nc->next)
94 for (no = nc->op_head; no != NULL; no = no->next)
96 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
101 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
104 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database));
105 GNUNET_free (db_lib_name);
109 * Called whenever a client is disconnected. Frees our
110 * resources associated with that client.
113 * @param client identification of the client
116 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
118 struct GNUNET_NAMESTORE_Operation * no;
119 struct GNUNET_NAMESTORE_Client * nc;
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client);
125 for (nc = client_head; nc != NULL; nc = nc->next)
127 if (client == nc->client)
133 for (no = nc->op_head; no != NULL; no = no->next)
135 GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, no);
139 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
143 static void handle_start (void *cls,
144 struct GNUNET_SERVER_Client * client,
145 const struct GNUNET_MessageHeader * message)
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n");
149 struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client));
152 GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc);
154 GNUNET_SERVER_receive_done (client, GNUNET_OK);
157 static void handle_lookup_name (void *cls,
158 struct GNUNET_SERVER_Client * client,
159 const struct GNUNET_MessageHeader * message)
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME");
162 GNUNET_SERVER_receive_done (client, GNUNET_OK);
167 * Process template requests.
170 * @param server the initialized server
171 * @param cfg configuration to use
174 run (void *cls, struct GNUNET_SERVER_Handle *server,
175 const struct GNUNET_CONFIGURATION_Handle *cfg)
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting namestore service\n");
181 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
182 {&handle_start, NULL,
183 GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)},
184 {&handle_lookup_name, NULL,
185 GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0},
191 /* Loading database plugin */
193 GNUNET_CONFIGURATION_get_value_string (cfg, "namestore", "database",
195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No database backend configured\n");
197 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", database);
198 GSN_database = GNUNET_PLUGIN_load (db_lib_name, (void *) GSN_cfg);
199 if (GSN_database == NULL)
200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n",
202 GNUNET_free (database);
204 /* Configuring server handles */
205 GNUNET_SERVER_add_handlers (server, handlers);
206 GNUNET_SERVER_disconnect_notify (server,
207 &client_disconnect_notification,
210 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
217 * The main function for the template service.
219 * @param argc number of arguments from the command line
220 * @param argv command line arguments
221 * @return 0 ok, 1 on error
224 main (int argc, char *const *argv)
227 GNUNET_SERVICE_run (argc, argv, "namestore",
228 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
231 /* end of gnunet-service-namestore.c */