2 This file is part of GNUnet.
3 (C) 2009, 2010 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 2, 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 dht/gnunet_dht_service.c
23 * @brief main DHT service shell, building block for DHT implementations
24 * @author Christian Grothoff
25 * @author Nathan Evans
29 #include "gnunet_client_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_service_lib.h"
34 #include "gnunet_core_service.h"
35 #include "gnunet_signal_lib.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_datastore_service.h"
41 * Handle to the datastore service (for inserting/retrieving data)
43 static struct GNUNET_DATASTORE_Handle *datastore;
46 * The main scheduler to use for the DHT service
48 static struct GNUNET_SCHEDULER_Handle *sched;
51 * The configuration the DHT service is running with
53 static const struct GNUNET_CONFIGURATION_Handle *cfg;
56 * Timeout for transmissions to clients
58 static struct GNUNET_TIME_Relative client_transmit_timeout;
61 * Handle to the core service
63 static struct GNUNET_CORE_Handle *coreAPI;
66 * The identity of our peer.
68 static struct GNUNET_PeerIdentity my_identity;
71 * Task to run when we shut down, cleaning up all our trash
73 static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
78 * This is a linked list
80 struct ClientList *next;
83 * The client in question
85 struct GNUNET_SERVER_Client *client;
89 * Server handler for initiating local dht get requests
91 static void handle_dht_get (void *cls, struct GNUNET_SERVER_Client * client,
92 const struct GNUNET_MessageHeader *message);
95 * Server handler for stopping local dht get requests
97 static void handle_dht_get_stop (void *cls, struct GNUNET_SERVER_Client * client,
98 const struct GNUNET_MessageHeader *message);
101 * Server handler for initiating local dht find peer requests
103 static void handle_dht_find_peer (void *cls, struct GNUNET_SERVER_Client *
104 client, const struct GNUNET_MessageHeader *
108 * Server handler for stopping local dht find peer requests
110 static void handle_dht_find_peer_stop (void *cls, struct GNUNET_SERVER_Client *
111 client, const struct GNUNET_MessageHeader *
115 * Server handler for initiating local dht put requests
117 static void handle_dht_put (void *cls, struct GNUNET_SERVER_Client * client,
118 const struct GNUNET_MessageHeader *message);
121 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
122 {&handle_dht_get, NULL, GNUNET_MESSAGE_TYPE_DHT_GET, 0},
123 {&handle_dht_get_stop, NULL, GNUNET_MESSAGE_TYPE_DHT_GET_STOP, 0},
124 {&handle_dht_put, NULL, GNUNET_MESSAGE_TYPE_DHT_PUT, 0},
125 {&handle_dht_find_peer, NULL, GNUNET_MESSAGE_TYPE_DHT_FIND_PEER, 0},
126 {&handle_dht_find_peer_stop, NULL, GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_STOP, 0},
132 * Core handler for p2p dht get requests.
134 static int handle_dht_p2p_get (void *cls,
135 const struct GNUNET_PeerIdentity * peer,
136 const struct GNUNET_MessageHeader * message,
137 struct GNUNET_TIME_Relative latency,
141 * Core handler for p2p dht put requests.
143 static int handle_dht_p2p_put (void *cls,
144 const struct GNUNET_PeerIdentity * peer,
145 const struct GNUNET_MessageHeader * message,
146 struct GNUNET_TIME_Relative latency,
150 * Core handler for p2p dht find peer requests.
152 static int handle_dht_p2p_find_peer (void *cls,
153 const struct GNUNET_PeerIdentity * peer,
154 const struct GNUNET_MessageHeader * message,
155 struct GNUNET_TIME_Relative latency,
158 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
159 {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_GET, 0},
160 {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_PUT, 0},
161 {&handle_dht_p2p_find_peer, GNUNET_MESSAGE_TYPE_DHT_PUT, 0},
167 * Server handler for initiating local dht get requests
169 static void handle_dht_get (void *cls, struct GNUNET_SERVER_Client * client,
170 const struct GNUNET_MessageHeader *message)
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174 "`%s': Received `%s' request from client\n", "DHT", "GET");
180 * Server handler for stopping local dht get requests
182 static void handle_dht_get_stop (void *cls, struct GNUNET_SERVER_Client * client,
183 const struct GNUNET_MessageHeader *message)
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 "`%s': Received `%s' request from client\n", "DHT", "GET STOP");
193 * Server handler for initiating local dht find peer requests
195 static void handle_dht_find_peer (void *cls, struct GNUNET_SERVER_Client *
196 client, const struct GNUNET_MessageHeader *
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201 "`%s': Received `%s' request from client\n", "DHT", "FIND PEER");
207 * Server handler for stopping local dht find peer requests
209 static void handle_dht_find_peer_stop (void *cls, struct GNUNET_SERVER_Client *
210 client, const struct GNUNET_MessageHeader *
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "`%s': Received `%s' request from client\n", "DHT", "FIND PEER STOP");
221 * Server handler for initiating local dht put requests
223 static void handle_dht_put (void *cls, struct GNUNET_SERVER_Client * client,
224 const struct GNUNET_MessageHeader *message)
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "`%s': Received `%s' request from client\n", "DHT", "PUT");
234 * Core handler for p2p dht get requests.
236 static int handle_dht_p2p_get (void *cls,
237 const struct GNUNET_PeerIdentity * peer,
238 const struct GNUNET_MessageHeader * message,
239 struct GNUNET_TIME_Relative latency,
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244 "`%s': Received `%s' request from another peer\n", "DHT", "GET");
251 * Core handler for p2p dht put requests.
253 static int handle_dht_p2p_put (void *cls,
254 const struct GNUNET_PeerIdentity * peer,
255 const struct GNUNET_MessageHeader * message,
256 struct GNUNET_TIME_Relative latency,
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "`%s': Received `%s' request from another peer\n", "DHT", "PUT");
268 * Core handler for p2p dht find peer requests.
270 static int handle_dht_p2p_find_peer (void *cls,
271 const struct GNUNET_PeerIdentity * peer,
272 const struct GNUNET_MessageHeader * message,
273 struct GNUNET_TIME_Relative latency,
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 "`%s': Received `%s' request from another peer\n", "DHT", "FIND PEER");
285 * Task run during shutdown.
291 shutdown_task (void *cls,
292 const struct GNUNET_SCHEDULER_TaskContext *tc)
294 GNUNET_CORE_disconnect (coreAPI);
298 * To be called on core init/fail.
300 void core_init (void *cls,
301 struct GNUNET_CORE_Handle * server,
302 const struct GNUNET_PeerIdentity *identity,
303 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
308 GNUNET_SCHEDULER_cancel(sched, cleanup_task);
309 GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL);
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "%s: Core connection initialized, I am peer: %s\n", "dht", GNUNET_i2s(identity));
316 memcpy(&my_identity, identity, sizeof(struct GNUNET_PeerIdentity));
321 * Process dht requests.
324 * @param scheduler scheduler to use
325 * @param server the initialized server
326 * @param c configuration to use
330 struct GNUNET_SCHEDULER_Handle *scheduler,
331 struct GNUNET_SERVER_Handle *server,
332 const struct GNUNET_CONFIGURATION_Handle *c)
337 datastore = GNUNET_DATASTORE_connect(c, scheduler);
339 client_transmit_timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5);
340 GNUNET_SERVER_add_handlers (server, plugin_handlers);
343 GNUNET_CORE_connect (sched,
345 client_transmit_timeout,
346 NULL, /* FIXME: anything we want to pass around? */
348 NULL, /* Don't care about pre-connects */
349 NULL, /* Don't care about connects */
350 NULL, /* Don't care about disconnects */
359 /* load (server); Huh? */
361 /* Scheduled the task to clean up when shutdown is called */
362 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
363 GNUNET_TIME_UNIT_FOREVER_REL,
370 * The main function for the dv service.
372 * @param argc number of arguments from the command line
373 * @param argv command line arguments
374 * @return 0 ok, 1 on error
377 main (int argc, char *const *argv)
380 GNUNET_SERVICE_run (argc,
383 GNUNET_SERVICE_OPTION_NONE,
384 &run, NULL)) ? 0 : 1;