2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 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 fs/gnunet-service-fs.c
23 * @brief gnunet anonymity protocol implementation
24 * @author Christian Grothoff
27 * - consider re-issue GSF_dht_lookup_ after non-DHT reply received
28 * - implement 'SUPPORT_DELAYS'
33 #include "gnunet_constants.h"
34 #include "gnunet_core_service.h"
35 #include "gnunet_dht_service.h"
36 #include "gnunet_datastore_service.h"
37 #include "gnunet_load_lib.h"
38 #include "gnunet_peer_lib.h"
39 #include "gnunet_protocols.h"
40 #include "gnunet_signatures.h"
41 #include "gnunet_statistics_service.h"
42 #include "gnunet_transport_service.h"
43 #include "gnunet_util_lib.h"
44 #include "gnunet-service-fs_cp.h"
45 #include "gnunet-service-fs_indexing.h"
46 #include "gnunet-service-fs_lc.h"
47 #include "gnunet-service-fs_pe.h"
48 #include "gnunet-service-fs_pr.h"
49 #include "gnunet-service-fs_push.h"
50 #include "gnunet-service-fs_put.h"
54 * Size for the hash map for DHT requests from the FS
55 * service. Should be about the number of concurrent
56 * DHT requests we plan to make.
58 #define FS_DHT_HT_SIZE 1024
62 * How quickly do we age cover traffic? At the given
63 * time interval, remaining cover traffic counters are
64 * decremented by 1/16th.
66 #define COVER_AGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
69 /* ****************************** globals ****************************** */
72 * Our connection to the datastore.
74 struct GNUNET_DATASTORE_Handle *GSF_dsh;
79 const struct GNUNET_CONFIGURATION_Handle *GSF_cfg;
82 * Handle for reporting statistics.
84 struct GNUNET_STATISTICS_Handle *GSF_stats;
87 * Handle for DHT operations.
89 struct GNUNET_DHT_Handle *GSF_dht;
92 * How long do requests typically stay in the routing table?
94 struct GNUNET_LOAD_Value *GSF_rt_entry_lifetime;
97 * Typical priorities we're seeing from other peers right now. Since
98 * most priorities will be zero, this value is the weighted average of
99 * non-zero priorities seen "recently". In order to ensure that new
100 * values do not dramatically change the ratio, values are first
101 * "capped" to a reasonable range (+N of the current value) and then
102 * averaged into the existing value by a ratio of 1:N. Hence
103 * receiving the largest possible priority can still only raise our
104 * "current_priorities" by at most 1.
106 double GSF_current_priorities;
109 * How many query messages have we received 'recently' that
110 * have not yet been claimed as cover traffic?
112 unsigned int GSF_cover_query_count;
115 * How many content messages have we received 'recently' that
116 * have not yet been claimed as cover traffic?
118 unsigned int GSF_cover_content_count;
123 struct GNUNET_BLOCK_Context *GSF_block_ctx;
126 * Pointer to handle to the core service (points to NULL until we've
129 struct GNUNET_CORE_Handle *GSF_core;
132 * Are we introducing randomized delays for better anonymity?
134 int GSF_enable_randomized_delays;
136 /* ***************************** locals ******************************* */
139 * Configuration for block library.
141 static struct GNUNET_CONFIGURATION_Handle *block_cfg;
144 * ID of our task that we use to age the cover counters.
146 static GNUNET_SCHEDULER_TaskIdentifier cover_age_task;
149 * Datastore 'GET' load tracking.
151 static struct GNUNET_LOAD_Value *datastore_get_load;
154 * Identity of this peer.
156 static struct GNUNET_PeerIdentity my_id;
159 * Task that periodically ages our cover traffic statistics.
161 * @param cls unused closure
162 * @param tc task context
165 age_cover_counters (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
167 GSF_cover_content_count = (GSF_cover_content_count * 15) / 16;
168 GSF_cover_query_count = (GSF_cover_query_count * 15) / 16;
170 GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, &age_cover_counters,
177 * We've just now completed a datastore request. Update our
178 * datastore load calculations.
180 * @param start time when the datastore request was issued
183 GSF_update_datastore_delay_ (struct GNUNET_TIME_Absolute start)
185 struct GNUNET_TIME_Relative delay;
187 delay = GNUNET_TIME_absolute_get_duration (start);
188 GNUNET_LOAD_update (datastore_get_load, delay.rel_value);
193 * Test if the DATABASE (GET) load on this peer is too high
194 * to even consider processing the query at
197 * @return GNUNET_YES if the load is too high to do anything (load high)
198 * GNUNET_NO to process normally (load normal)
199 * GNUNET_SYSERR to process for free (load low)
202 GSF_test_get_load_too_high_ (uint32_t priority)
206 ld = GNUNET_LOAD_get_load (datastore_get_load);
208 return GNUNET_SYSERR;
216 * Handle P2P "PUT" message.
218 * @param cls closure, always NULL
219 * @param other the other peer involved (sender or receiver, NULL
220 * for loopback messages where we are both sender and receiver)
221 * @param message the actual message
222 * @param atsi performance information
223 * @return GNUNET_OK to keep the connection open,
224 * GNUNET_SYSERR to close it (signal serious error)
227 handle_p2p_put (void *cls, const struct GNUNET_PeerIdentity *other,
228 const struct GNUNET_MessageHeader *message,
229 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
231 struct GSF_ConnectedPeer *cp;
233 cp = GSF_peer_get_ (other);
239 GSF_cover_content_count++;
240 return GSF_handle_p2p_content_ (cp, message);
245 * We have a new request, consider forwarding it to the given
248 * @param cls the 'struct GSF_PendingRequest'
249 * @param peer identity of the peer
250 * @param cp handle to the connected peer record
251 * @param ppd peer performance data
254 consider_request_for_forwarding (void *cls,
255 const struct GNUNET_PeerIdentity *peer,
256 struct GSF_ConnectedPeer *cp,
257 const struct GSF_PeerPerformanceData *ppd)
259 struct GSF_PendingRequest *pr = cls;
261 GSF_plan_add_ (cp, pr);
266 * Function to be called after we're done processing
267 * replies from the local lookup. If the result status
268 * code indicates that there may be more replies, plan
269 * forwarding the request.
271 * @param cls closure (NULL)
272 * @param pr the pending request we were processing
273 * @param result final datastore lookup result
276 consider_forwarding (void *cls, struct GSF_PendingRequest *pr,
277 enum GNUNET_BLOCK_EvaluationResult result)
279 if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
280 return; /* we're done... */
281 GSF_iterate_connected_peers_ (&consider_request_for_forwarding, pr);
286 * Handle P2P "GET" request.
288 * @param cls closure, always NULL
289 * @param other the other peer involved (sender or receiver, NULL
290 * for loopback messages where we are both sender and receiver)
291 * @param message the actual message
292 * @param atsi performance information
293 * @return GNUNET_OK to keep the connection open,
294 * GNUNET_SYSERR to close it (signal serious error)
297 handle_p2p_get (void *cls, const struct GNUNET_PeerIdentity *other,
298 const struct GNUNET_MessageHeader *message,
299 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
301 struct GSF_PendingRequest *pr;
303 pr = GSF_handle_p2p_query_ (other, message);
305 return GNUNET_SYSERR;
306 GSF_local_lookup_ (pr, &consider_forwarding, NULL);
312 * We're done with the local lookup, now consider
313 * P2P processing (depending on request options and
314 * result status). Also signal that we can now
315 * receive more request information from the client.
317 * @param cls the client doing the request ('struct GNUNET_SERVER_Client')
318 * @param pr the pending request we were processing
319 * @param result final datastore lookup result
322 start_p2p_processing (void *cls, struct GSF_PendingRequest *pr,
323 enum GNUNET_BLOCK_EvaluationResult result)
325 struct GNUNET_SERVER_Client *client = cls;
326 struct GSF_PendingRequestData *prd;
328 prd = GSF_pending_request_get_data_ (pr);
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "Finished database lookup for local request `%s' with result %d\n",
332 GNUNET_h2s (&prd->query), result);
334 GNUNET_SERVER_receive_done (client, GNUNET_OK);
335 if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
336 return; /* we're done, 'pr' was already destroyed... */
337 if (0 != (GSF_PRO_LOCAL_ONLY & prd->options))
339 GSF_pending_request_cancel_ (pr, GNUNET_YES);
342 GSF_dht_lookup_ (pr);
343 consider_forwarding (NULL, pr, result);
348 * Handle START_SEARCH-message (search request from client).
351 * @param client identification of the client
352 * @param message the actual message
355 handle_start_search (void *cls, struct GNUNET_SERVER_Client *client,
356 const struct GNUNET_MessageHeader *message)
358 struct GSF_PendingRequest *pr;
360 pr = GSF_local_client_start_search_handler_ (client, message);
363 /* GNUNET_SERVER_receive_done was already called! */
366 GSF_local_lookup_ (pr, &start_p2p_processing, client);
371 * Task run during shutdown.
377 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
379 if (NULL != GSF_core)
381 GNUNET_CORE_disconnect (GSF_core);
386 GSF_pending_request_done_ ();
388 GSF_connected_peer_done_ ();
389 GNUNET_DATASTORE_disconnect (GSF_dsh, GNUNET_NO);
391 GNUNET_DHT_disconnect (GSF_dht);
393 GNUNET_BLOCK_context_destroy (GSF_block_ctx);
394 GSF_block_ctx = NULL;
395 GNUNET_CONFIGURATION_destroy (block_cfg);
397 GNUNET_STATISTICS_destroy (GSF_stats, GNUNET_NO);
399 if (GNUNET_SCHEDULER_NO_TASK != cover_age_task)
401 GNUNET_SCHEDULER_cancel (cover_age_task);
402 cover_age_task = GNUNET_SCHEDULER_NO_TASK;
404 GNUNET_FS_indexing_done ();
405 GNUNET_LOAD_value_free (datastore_get_load);
406 datastore_get_load = NULL;
407 GNUNET_LOAD_value_free (GSF_rt_entry_lifetime);
408 GSF_rt_entry_lifetime = NULL;
413 * Function called for each pending request whenever a new
414 * peer connects, giving us a chance to decide about submitting
415 * the existing request to the new peer.
417 * @param cls the 'struct GSF_ConnectedPeer' of the new peer
418 * @param key query for the request
419 * @param pr handle to the pending request
420 * @return GNUNET_YES to continue to iterate
423 consider_peer_for_forwarding (void *cls, const GNUNET_HashCode * key,
424 struct GSF_PendingRequest *pr)
426 struct GSF_ConnectedPeer *cp = cls;
428 GSF_plan_add_ (cp, pr);
434 * Method called whenever a given peer connects.
436 * @param cls closure, not used
437 * @param peer peer identity this notification is about
438 * @param atsi performance information
441 peer_connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
442 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
444 struct GSF_ConnectedPeer *cp;
446 if (0 == memcmp (&my_id, peer, sizeof (struct GNUNET_PeerIdentity)))
448 cp = GSF_peer_connect_handler_ (peer, atsi);
451 GSF_iterate_pending_requests_ (&consider_peer_for_forwarding, cp);
456 * Function called after GNUNET_CORE_connect has succeeded
457 * (or failed for good). Note that the private key of the
458 * peer is intentionally not exposed here; if you need it,
459 * your process should try to read the private key file
460 * directly (which should work if you are authorized...).
463 * @param server handle to the server, NULL if we failed
464 * @param my_identity ID of this peer, NULL if we failed
465 * @param publicKey public key of this peer, NULL if we failed
468 peer_init_handler (void *cls, struct GNUNET_CORE_Handle *server,
469 const struct GNUNET_PeerIdentity *my_identity,
470 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
473 my_id = *my_identity;
478 * Process fs requests.
480 * @param server the initialized server
481 * @param c configuration to use
484 main_init (struct GNUNET_SERVER_Handle *server,
485 const struct GNUNET_CONFIGURATION_Handle *c)
487 static const struct GNUNET_CORE_MessageHandler p2p_handlers[] = {
489 GNUNET_MESSAGE_TYPE_FS_GET, 0},
491 GNUNET_MESSAGE_TYPE_FS_PUT, 0},
492 {&GSF_handle_p2p_migration_stop_,
493 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
494 sizeof (struct MigrationStopMessage)},
497 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
498 {&GNUNET_FS_handle_index_start, NULL,
499 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
500 {&GNUNET_FS_handle_index_list_get, NULL,
501 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET,
502 sizeof (struct GNUNET_MessageHeader)},
503 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
504 sizeof (struct UnindexMessage)},
505 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
510 GSF_core = GNUNET_CORE_connect (GSF_cfg, 2, /* larger? */
511 NULL, &peer_init_handler,
512 &peer_connect_handler,
513 &GSF_peer_disconnect_handler_,
514 &GSF_peer_status_handler_, NULL, GNUNET_NO,
515 NULL, GNUNET_NO, p2p_handlers);
516 if (NULL == GSF_core)
518 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
519 _("Failed to connect to `%s' service.\n"), "core");
520 return GNUNET_SYSERR;
522 GNUNET_SERVER_disconnect_notify (server, &GSF_client_disconnect_handler_,
524 GNUNET_SERVER_add_handlers (server, handlers);
526 GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, &age_cover_counters,
528 datastore_get_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
529 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
536 * Process fs requests.
539 * @param server the initialized server
540 * @param cfg configuration to use
543 run (void *cls, struct GNUNET_SERVER_Handle *server,
544 const struct GNUNET_CONFIGURATION_Handle *cfg)
547 GSF_enable_randomized_delays =
548 GNUNET_CONFIGURATION_get_value_yesno (cfg, "fs", "DELAY");
549 GSF_dsh = GNUNET_DATASTORE_connect (cfg);
552 GNUNET_SCHEDULER_shutdown ();
555 GSF_rt_entry_lifetime = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_FOREVER_REL);
556 GSF_stats = GNUNET_STATISTICS_create ("fs", cfg);
557 block_cfg = GNUNET_CONFIGURATION_create ();
558 GNUNET_CONFIGURATION_set_value_string (block_cfg, "block", "PLUGINS", "fs");
559 GSF_block_ctx = GNUNET_BLOCK_context_create (block_cfg);
560 GNUNET_assert (NULL != GSF_block_ctx);
561 GSF_dht = GNUNET_DHT_connect (cfg, FS_DHT_HT_SIZE);
563 GSF_pending_request_init_ ();
564 GSF_connected_peer_init_ ();
567 if ((GNUNET_OK != GNUNET_FS_indexing_init (cfg, GSF_dsh)) ||
568 (GNUNET_OK != main_init (server, cfg)))
570 GNUNET_SCHEDULER_shutdown ();
571 shutdown_task (NULL, NULL);
578 * The main function for the fs service.
580 * @param argc number of arguments from the command line
581 * @param argv command line arguments
582 * @return 0 ok, 1 on error
585 main (int argc, char *const *argv)
588 GNUNET_SERVICE_run (argc, argv, "fs", GNUNET_SERVICE_OPTION_NONE,
589 &run, NULL)) ? 0 : 1;
592 /* end of gnunet-service-fs.c */