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
28 #include "gnunet_constants.h"
29 #include "gnunet_core_service.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_datastore_service.h"
32 #include "gnunet_load_lib.h"
33 #include "gnunet_peer_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_util_lib.h"
39 #include "gnunet-service-fs_cp.h"
40 #include "gnunet-service-fs_indexing.h"
41 #include "gnunet-service-fs_lc.h"
42 #include "gnunet-service-fs_pe.h"
43 #include "gnunet-service-fs_pr.h"
44 #include "gnunet-service-fs_push.h"
45 #include "gnunet-service-fs_put.h"
46 #include "gnunet-service-fs_stream.h"
50 * Size for the hash map for DHT requests from the FS
51 * service. Should be about the number of concurrent
52 * DHT requests we plan to make.
54 #define FS_DHT_HT_SIZE 1024
58 * How quickly do we age cover traffic? At the given
59 * time interval, remaining cover traffic counters are
60 * decremented by 1/16th.
62 #define COVER_AGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
65 * Collect an instane number of statistics? May cause excessive IPC.
67 #define INSANE_STATISTICS GNUNET_NO
70 /* ****************************** globals ****************************** */
73 * Our connection to the datastore.
75 struct GNUNET_DATASTORE_Handle *GSF_dsh;
80 const struct GNUNET_CONFIGURATION_Handle *GSF_cfg;
83 * Handle for reporting statistics.
85 struct GNUNET_STATISTICS_Handle *GSF_stats;
88 * Handle for DHT operations.
90 struct GNUNET_DHT_Handle *GSF_dht;
93 * How long do requests typically stay in the routing table?
95 struct GNUNET_LOAD_Value *GSF_rt_entry_lifetime;
98 * Running average of the observed latency to other peers (round trip).
99 * Initialized to 5s as the initial default.
101 struct GNUNET_TIME_Relative GSF_avg_latency = { 500 };
104 * Typical priorities we're seeing from other peers right now. Since
105 * most priorities will be zero, this value is the weighted average of
106 * non-zero priorities seen "recently". In order to ensure that new
107 * values do not dramatically change the ratio, values are first
108 * "capped" to a reasonable range (+N of the current value) and then
109 * averaged into the existing value by a ratio of 1:N. Hence
110 * receiving the largest possible priority can still only raise our
111 * "current_priorities" by at most 1.
113 double GSF_current_priorities;
116 * Size of the datastore queue we assume for common requests.
118 unsigned int GSF_datastore_queue_size;
121 * How many query messages have we received 'recently' that
122 * have not yet been claimed as cover traffic?
124 unsigned int GSF_cover_query_count;
127 * How many content messages have we received 'recently' that
128 * have not yet been claimed as cover traffic?
130 unsigned int GSF_cover_content_count;
135 struct GNUNET_BLOCK_Context *GSF_block_ctx;
138 * Pointer to handle to the core service (points to NULL until we've
141 struct GNUNET_CORE_Handle *GSF_core;
144 * Are we introducing randomized delays for better anonymity?
146 int GSF_enable_randomized_delays;
148 /* ***************************** locals ******************************* */
151 * Configuration for block library.
153 static struct GNUNET_CONFIGURATION_Handle *block_cfg;
156 * ID of our task that we use to age the cover counters.
158 static GNUNET_SCHEDULER_TaskIdentifier cover_age_task;
161 * Datastore 'GET' load tracking.
163 static struct GNUNET_LOAD_Value *datastore_get_load;
166 * Identity of this peer.
168 static struct GNUNET_PeerIdentity my_id;
171 * Task that periodically ages our cover traffic statistics.
173 * @param cls unused closure
174 * @param tc task context
177 age_cover_counters (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
179 GSF_cover_content_count = (GSF_cover_content_count * 15) / 16;
180 GSF_cover_query_count = (GSF_cover_query_count * 15) / 16;
182 GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, &age_cover_counters,
188 * We've just now completed a datastore request. Update our
189 * datastore load calculations.
191 * @param start time when the datastore request was issued
194 GSF_update_datastore_delay_ (struct GNUNET_TIME_Absolute start)
196 struct GNUNET_TIME_Relative delay;
198 delay = GNUNET_TIME_absolute_get_duration (start);
199 GNUNET_LOAD_update (datastore_get_load, delay.rel_value);
204 * Test if the DATABASE (GET) load on this peer is too high
205 * to even consider processing the query at
208 * @return GNUNET_YES if the load is too high to do anything (load high)
209 * GNUNET_NO to process normally (load normal)
210 * GNUNET_SYSERR to process for free (load low)
213 GSF_test_get_load_too_high_ (uint32_t priority)
217 ld = GNUNET_LOAD_get_load (datastore_get_load);
219 return GNUNET_SYSERR;
227 * We've received peer performance information. Update
228 * our running average for the P2P latency.
230 * @param atsi performance information
231 * @param atsi_count number of 'atsi' records
234 update_latencies (const struct GNUNET_ATS_Information *atsi,
235 unsigned int atsi_count)
239 for (i = 0; i < atsi_count; i++)
241 if (ntohl (atsi[i].type) == GNUNET_ATS_QUALITY_NET_DELAY)
243 GSF_avg_latency.rel_value =
244 (GSF_avg_latency.rel_value * 31 +
245 GNUNET_MIN (5000, ntohl (atsi[i].value))) / 32;
246 GNUNET_STATISTICS_set (GSF_stats,
248 ("# running average P2P latency (ms)"),
249 GSF_avg_latency.rel_value, GNUNET_NO);
257 * Handle P2P "PUT" message.
259 * @param cls closure, always NULL
260 * @param other the other peer involved (sender or receiver, NULL
261 * for loopback messages where we are both sender and receiver)
262 * @param message the actual message
263 * @param atsi performance information
264 * @param atsi_count number of records in 'atsi'
265 * @return GNUNET_OK to keep the connection open,
266 * GNUNET_SYSERR to close it (signal serious error)
269 handle_p2p_put (void *cls, const struct GNUNET_PeerIdentity *other,
270 const struct GNUNET_MessageHeader *message,
271 const struct GNUNET_ATS_Information *atsi,
272 unsigned int atsi_count)
274 struct GSF_ConnectedPeer *cp;
276 cp = GSF_peer_get_ (other);
282 GSF_cover_content_count++;
283 update_latencies (atsi, atsi_count);
284 return GSF_handle_p2p_content_ (cp, message);
289 * We have a new request, consider forwarding it to the given
292 * @param cls the 'struct GSF_PendingRequest'
293 * @param peer identity of the peer
294 * @param cp handle to the connected peer record
295 * @param ppd peer performance data
298 consider_request_for_forwarding (void *cls,
299 const struct GNUNET_PeerIdentity *peer,
300 struct GSF_ConnectedPeer *cp,
301 const struct GSF_PeerPerformanceData *ppd)
303 struct GSF_PendingRequest *pr = cls;
305 if (GNUNET_YES != GSF_pending_request_test_target_ (pr, peer))
307 #if INSANE_STATISTICS
308 GNUNET_STATISTICS_update (GSF_stats,
309 gettext_noop ("# Loopback routes suppressed"), 1,
314 GSF_plan_add_ (cp, pr);
319 * Function to be called after we're done processing
320 * replies from the local lookup. If the result status
321 * code indicates that there may be more replies, plan
322 * forwarding the request.
324 * @param cls closure (NULL)
325 * @param pr the pending request we were processing
326 * @param result final datastore lookup result
329 consider_forwarding (void *cls, struct GSF_PendingRequest *pr,
330 enum GNUNET_BLOCK_EvaluationResult result)
332 if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
333 return; /* we're done... */
334 GSF_iterate_connected_peers_ (&consider_request_for_forwarding, pr);
339 * Handle P2P "GET" request.
341 * @param cls closure, always NULL
342 * @param other the other peer involved (sender or receiver, NULL
343 * for loopback messages where we are both sender and receiver)
344 * @param message the actual message
345 * @param atsi performance information
346 * @param atsi_count number of records in 'atsi'
347 * @return GNUNET_OK to keep the connection open,
348 * GNUNET_SYSERR to close it (signal serious error)
351 handle_p2p_get (void *cls, const struct GNUNET_PeerIdentity *other,
352 const struct GNUNET_MessageHeader *message,
353 const struct GNUNET_ATS_Information *atsi,
354 unsigned int atsi_count)
356 struct GSF_PendingRequest *pr;
358 pr = GSF_handle_p2p_query_ (other, message);
360 return GNUNET_SYSERR;
361 GSF_pending_request_get_data_ (pr)->has_started = GNUNET_YES;
362 GSF_local_lookup_ (pr, &consider_forwarding, NULL);
363 update_latencies (atsi, atsi_count);
369 * We're done with the local lookup, now consider
370 * P2P processing (depending on request options and
371 * result status). Also signal that we can now
372 * receive more request information from the client.
374 * @param cls the client doing the request ('struct GNUNET_SERVER_Client')
375 * @param pr the pending request we were processing
376 * @param result final datastore lookup result
379 start_p2p_processing (void *cls, struct GSF_PendingRequest *pr,
380 enum GNUNET_BLOCK_EvaluationResult result)
382 struct GNUNET_SERVER_Client *client = cls;
383 struct GSF_PendingRequestData *prd;
385 prd = GSF_pending_request_get_data_ (pr);
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Finished database lookup for local request `%s' with result %d\n",
388 GNUNET_h2s (&prd->query), result);
389 GNUNET_SERVER_receive_done (client, GNUNET_OK);
390 if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
391 return; /* we're done, 'pr' was already destroyed... */
392 if (0 != (GSF_PRO_LOCAL_ONLY & prd->options))
394 GSF_pending_request_cancel_ (pr, GNUNET_YES);
397 if (0 == prd->anonymity_level)
401 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
402 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
403 /* the above block types MAY be available via 'stream' */
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 "Considering stream-based download for block\n");
406 GSF_stream_lookup_ (pr);
408 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
409 /* the above block types are in the DHT */
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Considering DHT-based search for block\n");
412 GSF_dht_lookup_ (pr);
419 consider_forwarding (NULL, pr, result);
424 * Handle START_SEARCH-message (search request from client).
427 * @param client identification of the client
428 * @param message the actual message
431 handle_start_search (void *cls, struct GNUNET_SERVER_Client *client,
432 const struct GNUNET_MessageHeader *message)
434 struct GSF_PendingRequest *pr;
438 ret = GSF_local_client_start_search_handler_ (client, message, &pr);
442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
445 GNUNET_SERVER_receive_done (client, GNUNET_OK);
448 GSF_pending_request_get_data_ (pr)->has_started = GNUNET_YES;
449 GSF_local_lookup_ (pr, &start_p2p_processing, client);
458 * Task run during shutdown.
464 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
467 if (NULL != GSF_core)
469 GNUNET_CORE_disconnect (GSF_core);
474 GSF_pending_request_done_ ();
476 GSF_connected_peer_done_ ();
477 GNUNET_DATASTORE_disconnect (GSF_dsh, GNUNET_NO);
479 GNUNET_DHT_disconnect (GSF_dht);
481 GNUNET_BLOCK_context_destroy (GSF_block_ctx);
482 GSF_block_ctx = NULL;
483 GNUNET_CONFIGURATION_destroy (block_cfg);
485 GNUNET_STATISTICS_destroy (GSF_stats, GNUNET_NO);
487 if (GNUNET_SCHEDULER_NO_TASK != cover_age_task)
489 GNUNET_SCHEDULER_cancel (cover_age_task);
490 cover_age_task = GNUNET_SCHEDULER_NO_TASK;
492 GNUNET_FS_indexing_done ();
493 GNUNET_LOAD_value_free (datastore_get_load);
494 datastore_get_load = NULL;
495 GNUNET_LOAD_value_free (GSF_rt_entry_lifetime);
496 GSF_rt_entry_lifetime = NULL;
501 * Function called for each pending request whenever a new
502 * peer connects, giving us a chance to decide about submitting
503 * the existing request to the new peer.
505 * @param cls the 'struct GSF_ConnectedPeer' of the new peer
506 * @param key query for the request
507 * @param pr handle to the pending request
508 * @return GNUNET_YES to continue to iterate
511 consider_peer_for_forwarding (void *cls, const struct GNUNET_HashCode * key,
512 struct GSF_PendingRequest *pr)
514 struct GSF_ConnectedPeer *cp = cls;
515 struct GNUNET_PeerIdentity pid;
517 GSF_connected_peer_get_identity_ (cp, &pid);
518 if (GNUNET_YES != GSF_pending_request_test_target_ (pr, &pid))
520 GNUNET_STATISTICS_update (GSF_stats,
521 gettext_noop ("# Loopback routes suppressed"), 1,
525 GSF_plan_add_ (cp, pr);
531 * Method called whenever a given peer connects.
533 * @param cls closure, not used
534 * @param peer peer identity this notification is about
535 * @param atsi performance information
536 * @param atsi_count number of records in 'atsi'
539 peer_connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
540 const struct GNUNET_ATS_Information *atsi,
541 unsigned int atsi_count)
543 struct GSF_ConnectedPeer *cp;
545 if (0 == memcmp (&my_id, peer, sizeof (struct GNUNET_PeerIdentity)))
547 cp = GSF_peer_connect_handler_ (peer, atsi, atsi_count);
550 GSF_iterate_pending_requests_ (&consider_peer_for_forwarding, cp);
555 * Function called after GNUNET_CORE_connect has succeeded
556 * (or failed for good). Note that the private key of the
557 * peer is intentionally not exposed here; if you need it,
558 * your process should try to read the private key file
559 * directly (which should work if you are authorized...).
562 * @param server handle to the server, NULL if we failed
563 * @param my_identity ID of this peer, NULL if we failed
566 peer_init_handler (void *cls, struct GNUNET_CORE_Handle *server,
567 const struct GNUNET_PeerIdentity *my_identity)
569 my_id = *my_identity;
574 * Process fs requests.
576 * @param server the initialized server
577 * @param c configuration to use
580 main_init (struct GNUNET_SERVER_Handle *server,
581 const struct GNUNET_CONFIGURATION_Handle *c)
583 static const struct GNUNET_CORE_MessageHandler no_p2p_handlers[] = {
586 static const struct GNUNET_CORE_MessageHandler p2p_handlers[] = {
588 GNUNET_MESSAGE_TYPE_FS_GET, 0},
590 GNUNET_MESSAGE_TYPE_FS_PUT, 0},
591 {&GSF_handle_p2p_migration_stop_,
592 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
593 sizeof (struct MigrationStopMessage)},
596 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
597 {&GNUNET_FS_handle_index_start, NULL,
598 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
599 {&GNUNET_FS_handle_index_list_get, NULL,
600 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET,
601 sizeof (struct GNUNET_MessageHeader)},
602 {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
603 sizeof (struct UnindexMessage)},
604 {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
610 /* this option is really only for testcases that need to disable
611 _anonymous_ file-sharing for some reason */
612 anon_p2p_off = (GNUNET_YES ==
613 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
615 "DISABLE_ANON_TRANSFER"));
617 GNUNET_CORE_connect (GSF_cfg, NULL, &peer_init_handler,
618 &peer_connect_handler, &GSF_peer_disconnect_handler_,
619 NULL, GNUNET_NO, NULL, GNUNET_NO,
620 (GNUNET_YES == anon_p2p_off)
623 if (NULL == GSF_core)
625 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
626 _("Failed to connect to `%s' service.\n"), "core");
627 return GNUNET_SYSERR;
629 GNUNET_SERVER_disconnect_notify (server, &GSF_client_disconnect_handler_,
631 GNUNET_SERVER_add_handlers (server, handlers);
633 GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, &age_cover_counters,
635 datastore_get_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
637 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
644 * Process fs requests.
647 * @param server the initialized server
648 * @param cfg configuration to use
651 run (void *cls, struct GNUNET_SERVER_Handle *server,
652 const struct GNUNET_CONFIGURATION_Handle *cfg)
654 unsigned long long dqs;
658 GNUNET_CONFIGURATION_get_value_size (GSF_cfg, "fs", "DATASTORE_QUEUE_SIZE",
661 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
662 "fs", "DATASTORE_QUEUE_SIZE");
665 GSF_datastore_queue_size = (unsigned int) dqs;
666 GSF_enable_randomized_delays =
667 GNUNET_CONFIGURATION_get_value_yesno (cfg, "fs", "DELAY");
668 GSF_dsh = GNUNET_DATASTORE_connect (cfg);
671 GNUNET_SCHEDULER_shutdown ();
674 GSF_rt_entry_lifetime = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_FOREVER_REL);
675 GSF_stats = GNUNET_STATISTICS_create ("fs", cfg);
676 block_cfg = GNUNET_CONFIGURATION_create ();
677 GNUNET_CONFIGURATION_set_value_string (block_cfg, "block", "PLUGINS", "fs");
678 GSF_block_ctx = GNUNET_BLOCK_context_create (block_cfg);
679 GNUNET_assert (NULL != GSF_block_ctx);
680 GSF_dht = GNUNET_DHT_connect (cfg, FS_DHT_HT_SIZE);
682 GSF_pending_request_init_ ();
683 GSF_connected_peer_init_ ();
686 if ((GNUNET_OK != GNUNET_FS_indexing_init (cfg, GSF_dsh)) ||
687 (GNUNET_OK != main_init (server, cfg)))
689 GNUNET_SCHEDULER_shutdown ();
690 shutdown_task (NULL, NULL);
697 * The main function for the fs service.
699 * @param argc number of arguments from the command line
700 * @param argv command line arguments
701 * @return 0 ok, 1 on error
704 main (int argc, char *const *argv)
707 GNUNET_SERVICE_run (argc, argv, "fs", GNUNET_SERVICE_OPTION_NONE,
708 &run, NULL)) ? 0 : 1;
711 /* end of gnunet-service-fs.c */