2 This file is part of GNUnet.
3 Copyright (C) 2009-2014 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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_cadet.h"
51 * Size for the hash map for DHT requests from the FS
52 * service. Should be about the number of concurrent
53 * DHT requests we plan to make.
55 #define FS_DHT_HT_SIZE 1024
59 * How quickly do we age cover traffic? At the given
60 * time interval, remaining cover traffic counters are
61 * decremented by 1/16th.
63 #define COVER_AGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
66 * Collect an instane number of statistics? May cause excessive IPC.
68 #define INSANE_STATISTICS GNUNET_NO
71 /* ****************************** globals ****************************** */
74 * Our connection to the datastore.
76 struct GNUNET_DATASTORE_Handle *GSF_dsh;
81 const struct GNUNET_CONFIGURATION_Handle *GSF_cfg;
84 * Handle for reporting statistics.
86 struct GNUNET_STATISTICS_Handle *GSF_stats;
89 * Handle for DHT operations.
91 struct GNUNET_DHT_Handle *GSF_dht;
94 * How long do requests typically stay in the routing table?
96 struct GNUNET_LOAD_Value *GSF_rt_entry_lifetime;
99 * Running average of the observed latency to other peers (round trip).
100 * Initialized to 5s as the initial default.
102 struct GNUNET_TIME_Relative GSF_avg_latency = { 500 };
105 * Handle to ATS service.
107 struct GNUNET_ATS_PerformanceHandle *GSF_ats;
111 * Typical priorities we're seeing from other peers right now. Since
112 * most priorities will be zero, this value is the weighted average of
113 * non-zero priorities seen "recently". In order to ensure that new
114 * values do not dramatically change the ratio, values are first
115 * "capped" to a reasonable range (+N of the current value) and then
116 * averaged into the existing value by a ratio of 1:N. Hence
117 * receiving the largest possible priority can still only raise our
118 * "current_priorities" by at most 1.
120 double GSF_current_priorities;
123 * Size of the datastore queue we assume for common requests.
125 unsigned int GSF_datastore_queue_size;
128 * How many query messages have we received 'recently' that
129 * have not yet been claimed as cover traffic?
131 unsigned int GSF_cover_query_count;
134 * How many content messages have we received 'recently' that
135 * have not yet been claimed as cover traffic?
137 unsigned int GSF_cover_content_count;
142 struct GNUNET_BLOCK_Context *GSF_block_ctx;
145 * Pointer to handle to the core service (points to NULL until we've
148 struct GNUNET_CORE_Handle *GSF_core;
151 * Are we introducing randomized delays for better anonymity?
153 int GSF_enable_randomized_delays;
155 /* ***************************** locals ******************************* */
158 * Configuration for block library.
160 static struct GNUNET_CONFIGURATION_Handle *block_cfg;
163 * Private key of this peer. Used to sign LOC URI requests.
165 static struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
168 * ID of our task that we use to age the cover counters.
170 static struct GNUNET_SCHEDULER_Task * cover_age_task;
173 * Datastore 'GET' load tracking.
175 static struct GNUNET_LOAD_Value *datastore_get_load;
178 * Identity of this peer.
180 static struct GNUNET_PeerIdentity my_id;
184 * Task that periodically ages our cover traffic statistics.
186 * @param cls unused closure
189 age_cover_counters (void *cls)
191 GSF_cover_content_count = (GSF_cover_content_count * 15) / 16;
192 GSF_cover_query_count = (GSF_cover_query_count * 15) / 16;
194 GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, &age_cover_counters,
200 * We've just now completed a datastore request. Update our
201 * datastore load calculations.
203 * @param start time when the datastore request was issued
206 GSF_update_datastore_delay_ (struct GNUNET_TIME_Absolute start)
208 struct GNUNET_TIME_Relative delay;
210 delay = GNUNET_TIME_absolute_get_duration (start);
211 GNUNET_LOAD_update (datastore_get_load, delay.rel_value_us);
216 * Test if the DATABASE (GET) load on this peer is too high
217 * to even consider processing the query at
220 * @param priority priority of the request (used as a reference point to compare with the load)
221 * @return #GNUNET_YES if the load is too high to do anything (load high)
222 * #GNUNET_NO to process normally (load normal)
223 * #GNUNET_SYSERR to process for free (load low)
226 GSF_test_get_load_too_high_ (uint32_t priority)
230 ld = GNUNET_LOAD_get_load (datastore_get_load);
232 return GNUNET_SYSERR;
240 * We've received peer performance information. Update
241 * our running average for the P2P latency.
244 * @param address the address
245 * @param active is this address in active use
246 * @param bandwidth_out assigned outbound bandwidth for the connection
247 * @param bandwidth_in assigned inbound bandwidth for the connection
248 * @param prop performance data for the address (as far as known)
251 update_latencies (void *cls,
252 const struct GNUNET_HELLO_Address *address,
254 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
255 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
256 const struct GNUNET_ATS_Properties *prop)
260 /* ATS service temporarily disconnected */
264 if (GNUNET_YES != active)
266 GSF_update_peer_latency_ (&address->peer,
268 GSF_avg_latency.rel_value_us =
269 (GSF_avg_latency.rel_value_us * 31 +
270 GNUNET_MIN (5000, prop->delay.rel_value_us)) / 32;
271 GNUNET_STATISTICS_set (GSF_stats,
272 gettext_noop ("# running average P2P latency (ms)"),
273 GSF_avg_latency.rel_value_us / 1000LL,
279 * Handle P2P "PUT" message.
281 * @param cls closure, always NULL
282 * @param other the other peer involved (sender or receiver, NULL
283 * for loopback messages where we are both sender and receiver)
284 * @param message the actual message
285 * @return #GNUNET_OK to keep the connection open,
286 * #GNUNET_SYSERR to close it (signal serious error)
289 handle_p2p_put (void *cls,
290 const struct GNUNET_PeerIdentity *other,
291 const struct GNUNET_MessageHeader *message)
293 struct GSF_ConnectedPeer *cp;
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Received P2P PUT from %s\n",
298 cp = GSF_peer_get_ (other);
304 GSF_cover_content_count++;
305 return GSF_handle_p2p_content_ (cp, message);
310 * We have a new request, consider forwarding it to the given
313 * @param cls the `struct GSF_PendingRequest`
314 * @param peer identity of the peer
315 * @param cp handle to the connected peer record
316 * @param ppd peer performance data
319 consider_request_for_forwarding (void *cls,
320 const struct GNUNET_PeerIdentity *peer,
321 struct GSF_ConnectedPeer *cp,
322 const struct GSF_PeerPerformanceData *ppd)
324 struct GSF_PendingRequest *pr = cls;
326 if (GNUNET_YES != GSF_pending_request_test_target_ (pr, peer))
328 #if INSANE_STATISTICS
329 GNUNET_STATISTICS_update (GSF_stats,
330 gettext_noop ("# Loopback routes suppressed"), 1,
335 GSF_plan_add_ (cp, pr);
340 * Function to be called after we're done processing
341 * replies from the local lookup. If the result status
342 * code indicates that there may be more replies, plan
343 * forwarding the request.
345 * @param cls closure (NULL)
346 * @param pr the pending request we were processing
347 * @param result final datastore lookup result
350 consider_forwarding (void *cls,
351 struct GSF_PendingRequest *pr,
352 enum GNUNET_BLOCK_EvaluationResult result)
354 if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
355 return; /* we're done... */
357 GSF_pending_request_test_active_ (pr))
358 return; /* request is not actually active, skip! */
359 GSF_iterate_connected_peers_ (&consider_request_for_forwarding,
365 * Handle P2P "GET" request.
367 * @param cls closure, always NULL
368 * @param other the other peer involved (sender or receiver, NULL
369 * for loopback messages where we are both sender and receiver)
370 * @param message the actual message
371 * @return #GNUNET_OK to keep the connection open,
372 * #GNUNET_SYSERR to close it (signal serious error)
375 handle_p2p_get (void *cls,
376 const struct GNUNET_PeerIdentity *other,
377 const struct GNUNET_MessageHeader *message)
379 struct GSF_PendingRequest *pr;
381 pr = GSF_handle_p2p_query_ (other,
384 return GNUNET_OK; /* exists, identical to existing request, or malformed */
385 GSF_pending_request_get_data_ (pr)->has_started = GNUNET_YES;
386 GSF_local_lookup_ (pr,
387 &consider_forwarding,
394 * We're done with the local lookup, now consider
395 * P2P processing (depending on request options and
396 * result status). Also signal that we can now
397 * receive more request information from the client.
399 * @param cls the client doing the request (`struct GNUNET_SERVER_Client`)
400 * @param pr the pending request we were processing
401 * @param result final datastore lookup result
404 start_p2p_processing (void *cls,
405 struct GSF_PendingRequest *pr,
406 enum GNUNET_BLOCK_EvaluationResult result)
408 struct GNUNET_SERVER_Client *client = cls;
409 struct GSF_PendingRequestData *prd;
411 GNUNET_SERVER_receive_done (client,
413 if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
414 return; /* we're done, 'pr' was already destroyed... */
415 prd = GSF_pending_request_get_data_ (pr);
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "Finished database lookup for local request `%s' with result %d\n",
418 GNUNET_h2s (&prd->query), result);
419 if (0 == prd->anonymity_level)
423 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
424 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
425 /* the above block types MAY be available via 'cadet' */
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "Considering cadet-based download for block\n");
428 GSF_cadet_lookup_ (pr);
430 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
431 /* the above block types are in the DHT */
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "Considering DHT-based search for block\n");
434 GSF_dht_lookup_ (pr);
441 consider_forwarding (NULL, pr, result);
446 * Handle #GNUNET_MESSAGE_TYPE_FS_START_SEARCH-message (search request
450 * @param client identification of the client
451 * @param message the actual message
454 handle_start_search (void *cls,
455 struct GNUNET_SERVER_Client *client,
456 const struct GNUNET_MessageHeader *message)
458 struct GSF_PendingRequest *pr;
462 ret = GSF_local_client_start_search_handler_ (client,
468 GNUNET_SERVER_receive_done (client,
472 GNUNET_SERVER_receive_done (client,
476 GSF_pending_request_get_data_ (pr)->has_started = GNUNET_YES;
477 GSF_local_lookup_ (pr,
478 &start_p2p_processing,
488 * Handle request to sign a LOC URI (from client).
490 * @param cls closure (NULL)
491 * @param client identification of the client
492 * @param message the actual message
495 handle_loc_sign (void *cls,
496 struct GNUNET_SERVER_Client *client,
497 const struct GNUNET_MessageHeader *message)
499 const struct RequestLocSignatureMessage *msg;
500 struct GNUNET_FS_Uri base;
501 struct GNUNET_FS_Uri *loc;
502 struct ResponseLocSignatureMessage resp;
503 struct GSF_LocalClient *lc;
505 msg = (const struct RequestLocSignatureMessage *) message;
506 GNUNET_break (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT ==
507 ntohl (msg->purpose));
508 base.type = GNUNET_FS_URI_CHK;
509 base.data.chk.chk = msg->chk;
510 base.data.chk.file_length = GNUNET_ntohll (msg->file_length);
511 loc = GNUNET_FS_uri_loc_create (&base,
513 GNUNET_TIME_absolute_ntoh (msg->expiration_time));
514 resp.header.size = htons (sizeof (struct ResponseLocSignatureMessage));
515 resp.header.type = htons (GNUNET_MESSAGE_TYPE_FS_REQUEST_LOC_SIGNATURE);
516 resp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
517 resp.expiration_time = GNUNET_TIME_absolute_hton (loc->data.loc.expirationTime);
518 resp.signature = loc->data.loc.contentSignature;
519 resp.peer = loc->data.loc.peer;
520 GNUNET_FS_uri_destroy (loc);
521 lc = GSF_local_client_lookup_ (client);
522 GSF_local_client_transmit_ (lc,
524 GNUNET_SERVER_receive_done (client, GNUNET_OK);
529 * Task run during shutdown.
534 shutdown_task (void *cls)
536 GSF_cadet_stop_client ();
537 GSF_cadet_stop_server ();
538 if (NULL != GSF_core)
540 GNUNET_CORE_disconnect (GSF_core);
545 GNUNET_ATS_performance_done (GSF_ats);
550 GSF_pending_request_done_ ();
552 GSF_connected_peer_done_ ();
553 GNUNET_DATASTORE_disconnect (GSF_dsh, GNUNET_NO);
555 GNUNET_DHT_disconnect (GSF_dht);
557 GNUNET_BLOCK_context_destroy (GSF_block_ctx);
558 GSF_block_ctx = NULL;
559 GNUNET_CONFIGURATION_destroy (block_cfg);
561 GNUNET_STATISTICS_destroy (GSF_stats, GNUNET_NO);
563 if (NULL != cover_age_task)
565 GNUNET_SCHEDULER_cancel (cover_age_task);
566 cover_age_task = NULL;
568 GNUNET_FS_indexing_done ();
569 GNUNET_LOAD_value_free (datastore_get_load);
570 datastore_get_load = NULL;
571 GNUNET_LOAD_value_free (GSF_rt_entry_lifetime);
572 GSF_rt_entry_lifetime = NULL;
577 * Function called for each pending request whenever a new
578 * peer connects, giving us a chance to decide about submitting
579 * the existing request to the new peer.
581 * @param cls the `struct GSF_ConnectedPeer` of the new peer
582 * @param key query for the request
583 * @param pr handle to the pending request
584 * @return #GNUNET_YES to continue to iterate
587 consider_peer_for_forwarding (void *cls,
588 const struct GNUNET_HashCode *key,
589 struct GSF_PendingRequest *pr)
591 struct GSF_ConnectedPeer *cp = cls;
592 struct GNUNET_PeerIdentity pid;
595 GSF_pending_request_test_active_ (pr))
596 return GNUNET_YES; /* request is not actually active, skip! */
597 GSF_connected_peer_get_identity_ (cp, &pid);
599 GSF_pending_request_test_target_ (pr, &pid))
601 GNUNET_STATISTICS_update (GSF_stats,
602 gettext_noop ("# Loopback routes suppressed"),
607 GSF_plan_add_ (cp, pr);
613 * Function called after the creation of a connected peer record is complete.
615 * @param cls closure (unused)
616 * @param cp handle to the newly created connected peer record
619 connected_peer_cb (void *cls,
620 struct GSF_ConnectedPeer *cp)
624 GSF_iterate_pending_requests_ (&consider_peer_for_forwarding,
630 * Method called whenever a given peer connects.
632 * @param cls closure, not used
633 * @param peer peer identity this notification is about
636 peer_connect_handler (void *cls,
637 const struct GNUNET_PeerIdentity *peer)
640 GNUNET_CRYPTO_cmp_peer_identity (&my_id,
643 GSF_peer_connect_handler_ (peer,
650 * Function called after GNUNET_CORE_connect has succeeded
651 * (or failed for good). Note that the private key of the
652 * peer is intentionally not exposed here; if you need it,
653 * your process should try to read the private key file
654 * directly (which should work if you are authorized...).
657 * @param my_identity ID of this peer, NULL if we failed
660 peer_init_handler (void *cls,
661 const struct GNUNET_PeerIdentity *my_identity)
663 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_id,
666 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
667 "Peer identity missmatch, refusing to start!\n");
668 GNUNET_SCHEDULER_shutdown ();
674 * Process fs requests.
676 * @param server the initialized server
677 * @param c configuration to use
680 main_init (struct GNUNET_SERVER_Handle *server,
681 const struct GNUNET_CONFIGURATION_Handle *c)
683 static const struct GNUNET_CORE_MessageHandler no_p2p_handlers[] = {
686 static const struct GNUNET_CORE_MessageHandler p2p_handlers[] = {
688 GNUNET_MESSAGE_TYPE_FS_GET, 0 },
690 GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
691 { &GSF_handle_p2p_migration_stop_,
692 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
693 sizeof (struct MigrationStopMessage) },
696 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
697 { &GNUNET_FS_handle_index_start, NULL,
698 GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0 },
699 { &GNUNET_FS_handle_index_list_get, NULL,
700 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET,
701 sizeof (struct GNUNET_MessageHeader) },
702 { &GNUNET_FS_handle_unindex, NULL,
703 GNUNET_MESSAGE_TYPE_FS_UNINDEX,
704 sizeof (struct UnindexMessage) },
705 { &handle_start_search, NULL,
706 GNUNET_MESSAGE_TYPE_FS_START_SEARCH, 0 },
707 { &handle_loc_sign, NULL,
708 GNUNET_MESSAGE_TYPE_FS_REQUEST_LOC_SIGN,
709 sizeof (struct RequestLocSignatureMessage) },
715 /* this option is really only for testcases that need to disable
716 _anonymous_ file-sharing for some reason */
717 anon_p2p_off = (GNUNET_YES ==
718 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
720 "DISABLE_ANON_TRANSFER"));
723 GNUNET_CONFIGURATION_get_value_filename (GSF_cfg,
728 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
729 _("FS service is lacking HOSTKEY configuration setting. Exiting.\n"));
730 GNUNET_SCHEDULER_shutdown ();
731 return GNUNET_SYSERR;
733 pk = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
734 GNUNET_free (keyfile);
735 GNUNET_assert (NULL != pk);
736 GNUNET_CRYPTO_eddsa_key_get_public (pk,
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
741 GNUNET_i2s (&my_id));
743 = GNUNET_CORE_connect (GSF_cfg, NULL,
745 &peer_connect_handler,
746 &GSF_peer_disconnect_handler_,
749 (GNUNET_YES == anon_p2p_off)
752 if (NULL == GSF_core)
754 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
755 _("Failed to connect to `%s' service.\n"), "core");
756 return GNUNET_SYSERR;
758 GNUNET_SERVER_disconnect_notify (server, &GSF_client_disconnect_handler_,
760 GNUNET_SERVER_add_handlers (server, handlers);
762 GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, &age_cover_counters,
764 datastore_get_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
765 GSF_cadet_start_server ();
766 GSF_cadet_start_client ();
767 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
774 * Process fs requests.
777 * @param server the initialized server
778 * @param cfg configuration to use
781 run (void *cls, struct GNUNET_SERVER_Handle *server,
782 const struct GNUNET_CONFIGURATION_Handle *cfg)
784 unsigned long long dqs;
788 GNUNET_CONFIGURATION_get_value_size (GSF_cfg, "fs", "DATASTORE_QUEUE_SIZE",
791 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
792 "fs", "DATASTORE_QUEUE_SIZE");
795 GSF_datastore_queue_size = (unsigned int) dqs;
796 GSF_enable_randomized_delays =
797 GNUNET_CONFIGURATION_get_value_yesno (cfg, "fs", "DELAY");
798 GSF_dsh = GNUNET_DATASTORE_connect (cfg);
801 GNUNET_SCHEDULER_shutdown ();
804 GSF_rt_entry_lifetime = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_FOREVER_REL);
805 GSF_stats = GNUNET_STATISTICS_create ("fs", cfg);
806 block_cfg = GNUNET_CONFIGURATION_create ();
807 GSF_block_ctx = GNUNET_BLOCK_context_create (block_cfg);
808 GNUNET_assert (NULL != GSF_block_ctx);
809 GSF_dht = GNUNET_DHT_connect (cfg, FS_DHT_HT_SIZE);
811 GSF_pending_request_init_ ();
812 GSF_connected_peer_init_ ();
813 GSF_ats = GNUNET_ATS_performance_init (GSF_cfg, &update_latencies, NULL);
816 if ((GNUNET_OK != GNUNET_FS_indexing_init (cfg, GSF_dsh)) ||
817 (GNUNET_OK != main_init (server, cfg)))
819 GNUNET_SCHEDULER_shutdown ();
820 shutdown_task (NULL);
827 * The main function for the fs service.
829 * @param argc number of arguments from the command line
830 * @param argv command line arguments
831 * @return 0 ok, 1 on error
834 main (int argc, char *const *argv)
837 GNUNET_SERVICE_run (argc, argv, "fs", GNUNET_SERVICE_OPTION_NONE,
838 &run, NULL)) ? 0 : 1;
841 /* end of gnunet-service-fs.c */