2 This file is part of GNUnet.
3 Copyright (C) 2009-2014, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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_pe.h"
42 #include "gnunet-service-fs_pr.h"
43 #include "gnunet-service-fs_push.h"
44 #include "gnunet-service-fs_put.h"
45 #include "gnunet-service-fs_cadet.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 ( \
63 GNUNET_TIME_UNIT_SECONDS, 5)
66 * Collect an instane number of statistics? May cause excessive IPC.
68 #define INSANE_STATISTICS GNUNET_NO
72 * Doubly-linked list of requests we are performing
73 * on behalf of the same client.
78 * This is a doubly-linked list.
80 struct ClientRequest *next;
83 * This is a doubly-linked list.
85 struct ClientRequest *prev;
88 * Request this entry represents.
90 struct GSF_PendingRequest *pr;
93 * Client list this request belongs to.
95 struct GSF_LocalClient *lc;
98 * Task scheduled to destroy the request.
100 struct GNUNET_SCHEDULER_Task *kill_task;
105 * Replies to be transmitted to the client. The actual
106 * response message is allocated after this struct.
108 struct ClientResponse
111 * This is a doubly-linked list.
113 struct ClientResponse *next;
116 * This is a doubly-linked list.
118 struct ClientResponse *prev;
121 * Client list entry this response belongs to.
123 struct GSF_LocalClient *lc;
126 * Number of bytes in the response.
133 * Information we track while handling an index
134 * start request from a client.
136 struct IndexStartContext
139 * This is a doubly linked list.
141 struct IndexStartContext *next;
144 * This is a doubly linked list.
146 struct IndexStartContext *prev;
149 * Name of the indexed file.
154 * Context for transmitting confirmation to client.
156 struct GSF_LocalClient *lc;
159 * Context for hashing of the file.
161 struct GNUNET_CRYPTO_FileHashContext *fhc;
164 * Hash of the contents of the file.
166 struct GNUNET_HashCode file_id;
173 struct GSF_LocalClient
178 struct GNUNET_SERVICE_Client *client;
181 * Queue for sending replies.
183 struct GNUNET_MQ_Handle *mq;
186 * Head of list of requests performed on behalf
187 * of this client right now.
189 struct ClientRequest *cr_head;
192 * Tail of list of requests performed on behalf
193 * of this client right now.
195 struct ClientRequest *cr_tail;
198 * This is a doubly linked list.
200 struct IndexStartContext *isc_head;
203 * This is a doubly linked list.
205 struct IndexStartContext *isc_tail;
208 * Head of linked list of responses.
210 struct ClientResponse *res_head;
213 * Tail of linked list of responses.
215 struct ClientResponse *res_tail;
219 /* ****************************** globals ****************************** */
222 * Our connection to the datastore.
224 struct GNUNET_DATASTORE_Handle *GSF_dsh;
229 const struct GNUNET_CONFIGURATION_Handle *GSF_cfg;
232 * Handle for reporting statistics.
234 struct GNUNET_STATISTICS_Handle *GSF_stats;
237 * Handle for DHT operations.
239 struct GNUNET_DHT_Handle *GSF_dht;
242 * How long do requests typically stay in the routing table?
244 struct GNUNET_LOAD_Value *GSF_rt_entry_lifetime;
247 * Running average of the observed latency to other peers (round trip).
248 * Initialized to 5s as the initial default.
250 struct GNUNET_TIME_Relative GSF_avg_latency = { 500 };
253 * Handle to ATS service.
255 struct GNUNET_ATS_PerformanceHandle *GSF_ats;
259 * Typical priorities we're seeing from other peers right now. Since
260 * most priorities will be zero, this value is the weighted average of
261 * non-zero priorities seen "recently". In order to ensure that new
262 * values do not dramatically change the ratio, values are first
263 * "capped" to a reasonable range (+N of the current value) and then
264 * averaged into the existing value by a ratio of 1:N. Hence
265 * receiving the largest possible priority can still only raise our
266 * "current_priorities" by at most 1.
268 double GSF_current_priorities;
271 * Size of the datastore queue we assume for common requests.
273 unsigned int GSF_datastore_queue_size;
276 * How many query messages have we received 'recently' that
277 * have not yet been claimed as cover traffic?
279 unsigned int GSF_cover_query_count;
282 * How many content messages have we received 'recently' that
283 * have not yet been claimed as cover traffic?
285 unsigned int GSF_cover_content_count;
290 struct GNUNET_BLOCK_Context *GSF_block_ctx;
293 * Pointer to handle to the core service (points to NULL until we've
296 struct GNUNET_CORE_Handle *GSF_core;
299 * Are we introducing randomized delays for better anonymity?
301 int GSF_enable_randomized_delays;
304 * Identity of this peer.
306 struct GNUNET_PeerIdentity GSF_my_id;
308 /* ***************************** locals ******************************* */
311 * Configuration for block library.
313 static struct GNUNET_CONFIGURATION_Handle *block_cfg;
316 * Private key of this peer. Used to sign LOC URI requests.
318 static struct GNUNET_CRYPTO_EddsaPrivateKey pk;
321 * ID of our task that we use to age the cover counters.
323 static struct GNUNET_SCHEDULER_Task *cover_age_task;
326 * Datastore 'GET' load tracking.
328 static struct GNUNET_LOAD_Value *datastore_get_load;
332 * Creates a fresh local client handle.
335 * @param client handle of the client
336 * @param mq message queue for @a client
337 * @return handle to local client entry
340 client_connect_cb (void *cls,
341 struct GNUNET_SERVICE_Client *client,
342 struct GNUNET_MQ_Handle *mq)
344 struct GSF_LocalClient *pos;
346 pos = GNUNET_new (struct GSF_LocalClient);
347 pos->client = client;
354 * Free the given client request.
356 * @param cls the client request to free
359 client_request_destroy (void *cls)
361 struct ClientRequest *cr = cls;
362 struct GSF_LocalClient *lc = cr->lc;
364 cr->kill_task = NULL;
365 GNUNET_CONTAINER_DLL_remove (lc->cr_head,
368 GSF_pending_request_cancel_ (cr->pr,
370 GNUNET_STATISTICS_update (GSF_stats,
371 gettext_noop ("# client searches active"),
379 * Handle a reply to a pending request. Also called if a request
380 * expires (then with data == NULL). The handler may be called
381 * many times (depending on the request type), but will not be
382 * called during or after a call to #GSF_pending_request_cancel()
383 * and will also not be called anymore after a call signalling
386 * @param cls user-specified closure
387 * @param eval evaluation of the result
388 * @param pr handle to the original pending request
389 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
390 * @param expiration when does @a data expire?
391 * @param last_transmission when was the last time we've tried to download this block? (FOREVER if unknown)
392 * @param type type of the block
393 * @param data response data, NULL on request expiration
394 * @param data_len number of bytes in @a data
397 client_response_handler (void *cls,
398 enum GNUNET_BLOCK_EvaluationResult eval,
399 struct GSF_PendingRequest *pr,
400 uint32_t reply_anonymity_level,
401 struct GNUNET_TIME_Absolute expiration,
402 struct GNUNET_TIME_Absolute last_transmission,
403 enum GNUNET_BLOCK_Type type,
407 struct ClientRequest *cr = cls;
408 struct GSF_LocalClient *lc;
409 struct GNUNET_MQ_Envelope *env;
410 struct ClientPutMessage *pm;
411 const struct GSF_PendingRequestData *prd;
415 /* local-only request, with no result, clean up. */
416 if (NULL == cr->kill_task)
417 cr->kill_task = GNUNET_SCHEDULER_add_now (&client_request_destroy,
421 prd = GSF_pending_request_get_data_ (pr);
422 GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
423 if ((prd->type != type) && (prd->type != GNUNET_BLOCK_TYPE_ANY))
428 GNUNET_STATISTICS_update (GSF_stats,
430 ("# replies received for local clients"), 1,
432 GNUNET_assert (pr == cr->pr);
434 env = GNUNET_MQ_msg_extra (pm,
436 GNUNET_MESSAGE_TYPE_FS_PUT);
437 pm->type = htonl (type);
438 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
439 pm->last_transmission = GNUNET_TIME_absolute_hton (last_transmission);
440 pm->num_transmissions = htonl (prd->num_transmissions);
441 pm->respect_offered = htonl (prd->respect_offered);
442 GNUNET_memcpy (&pm[1],
445 GNUNET_MQ_send (lc->mq,
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 "Queued reply to query `%s' for local client\n",
449 GNUNET_h2s (&prd->query));
450 if (GNUNET_BLOCK_EVALUATION_OK_LAST != eval)
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Evaluation %d - keeping query alive\n",
457 if (NULL == cr->kill_task)
458 cr->kill_task = GNUNET_SCHEDULER_add_now (&client_request_destroy,
464 * A client disconnected from us. Tear down the local client
468 * @param client handle of the client
469 * @param app_ctx the `struct GSF_LocalClient`
472 client_disconnect_cb (void *cls,
473 struct GNUNET_SERVICE_Client *client,
476 struct GSF_LocalClient *lc = app_ctx;
477 struct IndexStartContext *isc;
478 struct ClientRequest *cr;
479 struct ClientResponse *res;
481 while (NULL != (cr = lc->cr_head))
483 if (NULL != cr->kill_task)
484 GNUNET_SCHEDULER_cancel (cr->kill_task);
485 client_request_destroy (cr);
487 while (NULL != (res = lc->res_head))
489 GNUNET_CONTAINER_DLL_remove (lc->res_head,
494 while (NULL != (isc = lc->isc_head))
496 GNUNET_CONTAINER_DLL_remove (lc->isc_head,
499 GNUNET_CRYPTO_hash_file_cancel (isc->fhc);
507 * Task that periodically ages our cover traffic statistics.
509 * @param cls unused closure
512 age_cover_counters (void *cls)
514 GSF_cover_content_count = (GSF_cover_content_count * 15) / 16;
515 GSF_cover_query_count = (GSF_cover_query_count * 15) / 16;
517 GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY,
524 * We've just now completed a datastore request. Update our
525 * datastore load calculations.
527 * @param start time when the datastore request was issued
530 GSF_update_datastore_delay_ (struct GNUNET_TIME_Absolute start)
532 struct GNUNET_TIME_Relative delay;
534 delay = GNUNET_TIME_absolute_get_duration (start);
535 GNUNET_LOAD_update (datastore_get_load, delay.rel_value_us);
540 * Test if the DATABASE (GET) load on this peer is too high
541 * to even consider processing the query at
544 * @param priority priority of the request (used as a reference point to compare with the load)
545 * @return #GNUNET_YES if the load is too high to do anything (load high)
546 * #GNUNET_NO to process normally (load normal)
547 * #GNUNET_SYSERR to process for free (load low)
550 GSF_test_get_load_too_high_ (uint32_t priority)
554 ld = GNUNET_LOAD_get_load (datastore_get_load);
556 return GNUNET_SYSERR;
564 * We've received peer performance information. Update
565 * our running average for the P2P latency.
568 * @param address the address
569 * @param active is this address in active use
570 * @param bandwidth_out assigned outbound bandwidth for the connection
571 * @param bandwidth_in assigned inbound bandwidth for the connection
572 * @param prop performance data for the address (as far as known)
575 update_latencies (void *cls,
576 const struct GNUNET_HELLO_Address *address,
578 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
579 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
580 const struct GNUNET_ATS_Properties *prop)
584 /* ATS service temporarily disconnected */
588 if (GNUNET_YES != active)
590 GSF_update_peer_latency_ (&address->peer,
592 GSF_avg_latency.rel_value_us =
593 (GSF_avg_latency.rel_value_us * 31
594 + GNUNET_MIN (5000, prop->delay.rel_value_us)) / 32;
595 GNUNET_STATISTICS_set (GSF_stats,
596 gettext_noop ("# running average P2P latency (ms)"),
597 GSF_avg_latency.rel_value_us / 1000LL,
603 * Check P2P "PUT" message.
605 * @param cls closure with the `struct GSF_ConnectedPeer`
606 * @param message the actual message
607 * @return #GNUNET_OK to keep the connection open,
608 * #GNUNET_SYSERR to close it (signal serious error)
611 check_p2p_put (void *cls,
612 const struct PutMessage *put)
614 enum GNUNET_BLOCK_Type type;
616 type = ntohl (put->type);
617 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
620 return GNUNET_SYSERR;
627 * We have a new request, consider forwarding it to the given
630 * @param cls the `struct GSF_PendingRequest`
631 * @param peer identity of the peer
632 * @param cp handle to the connected peer record
633 * @param ppd peer performance data
636 consider_request_for_forwarding (void *cls,
637 const struct GNUNET_PeerIdentity *peer,
638 struct GSF_ConnectedPeer *cp,
639 const struct GSF_PeerPerformanceData *ppd)
641 struct GSF_PendingRequest *pr = cls;
644 GSF_pending_request_test_target_ (pr, peer))
646 #if INSANE_STATISTICS
647 GNUNET_STATISTICS_update (GSF_stats,
648 gettext_noop ("# Loopback routes suppressed"), 1,
659 * Function to be called after we're done processing
660 * replies from the local lookup. If the result status
661 * code indicates that there may be more replies, plan
662 * forwarding the request.
664 * @param cls closure (NULL)
665 * @param pr the pending request we were processing
666 * @param result final datastore lookup result
669 GSF_consider_forwarding (void *cls,
670 struct GSF_PendingRequest *pr,
671 enum GNUNET_BLOCK_EvaluationResult result)
673 if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
674 return; /* we're done... */
676 GSF_pending_request_test_active_ (pr))
677 return; /* request is not actually active, skip! */
678 GSF_iterate_connected_peers_ (&consider_request_for_forwarding,
684 * Check P2P "GET" request.
687 * @param gm the actual message
688 * @return #GNUNET_OK to keep the connection open,
689 * #GNUNET_SYSERR to close it (signal serious error)
692 check_p2p_get (void *cls,
693 const struct GetMessage *gm)
700 msize = ntohs (gm->header.size);
701 bm = ntohl (gm->hash_bitmap);
709 if (msize < sizeof(struct GetMessage) + bits * sizeof(struct
710 GNUNET_PeerIdentity))
713 return GNUNET_SYSERR;
715 bfsize = msize - sizeof(struct GetMessage) - bits * sizeof(struct
716 GNUNET_PeerIdentity);
717 /* bfsize must be power of 2, check! */
718 if (0 != ((bfsize - 1) & bfsize))
721 return GNUNET_SYSERR;
728 * We're done with the local lookup, now consider
729 * P2P processing (depending on request options and
730 * result status). Also signal that we can now
731 * receive more request information from the client.
733 * @param cls the client doing the request (`struct GSF_LocalClient`)
734 * @param pr the pending request we were processing
735 * @param result final datastore lookup result
738 start_p2p_processing (void *cls,
739 struct GSF_PendingRequest *pr,
740 enum GNUNET_BLOCK_EvaluationResult result)
742 struct GSF_LocalClient *lc = cls;
743 struct GSF_PendingRequestData *prd;
745 GNUNET_SERVICE_client_continue (lc->client);
746 if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
747 return; /* we're done, 'pr' was already destroyed... */
748 prd = GSF_pending_request_get_data_ (pr);
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
750 "Finished database lookup for local request `%s' with result %d\n",
751 GNUNET_h2s (&prd->query),
753 if (0 == prd->anonymity_level)
757 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
758 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
759 /* the above block types MAY be available via 'cadet' */
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
761 "Considering cadet-based download for block\n");
762 GSF_cadet_lookup_ (pr);
765 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
766 /* the above block types are in the DHT */
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "Considering DHT-based search for block\n");
769 GSF_dht_lookup_ (pr);
777 GSF_consider_forwarding (NULL,
784 * Check #GNUNET_MESSAGE_TYPE_FS_START_SEARCH-message (search request
787 * @param cls identification of the client
788 * @param sm the actual message
789 * @return #GNUNET_OK if @a sm is well-formed
792 check_client_start_search (void *cls,
793 const struct SearchMessage *sm)
797 msize = ntohs (sm->header.size) - sizeof(struct SearchMessage);
798 if (0 != msize % sizeof(struct GNUNET_HashCode))
801 return GNUNET_SYSERR;
808 * Handle #GNUNET_MESSAGE_TYPE_FS_START_SEARCH-message (search request
811 * Responsible for creating the request entry itself and setting
812 * up reply callback and cancellation on client disconnect.
814 * @param cls identification of the client
815 * @param sm the actual message
818 handle_client_start_search (void *cls,
819 const struct SearchMessage *sm)
821 static struct GNUNET_PeerIdentity all_zeros;
822 struct GSF_LocalClient *lc = cls;
823 struct ClientRequest *cr;
824 struct GSF_PendingRequestData *prd;
827 enum GNUNET_BLOCK_Type type;
828 enum GSF_PendingRequestOptions options;
830 GNUNET_STATISTICS_update (GSF_stats,
831 gettext_noop ("# client searches received"),
834 msize = ntohs (sm->header.size) - sizeof(struct SearchMessage);
835 sc = msize / sizeof(struct GNUNET_HashCode);
836 type = ntohl (sm->type);
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "Received request for `%s' of type %u from local client\n",
839 GNUNET_h2s (&sm->query),
840 (unsigned int) type);
842 /* detect duplicate UBLOCK requests */
843 if ((type == GNUNET_BLOCK_TYPE_FS_UBLOCK) ||
844 (type == GNUNET_BLOCK_TYPE_ANY))
849 prd = GSF_pending_request_get_data_ (cr->pr);
850 /* only unify with queries that hae not yet started local processing
851 (SEARCH_MESSAGE_OPTION_CONTINUED was always set) and that have a
852 matching query and type */
853 if ((GNUNET_YES != prd->has_started) &&
854 (0 != memcmp (&prd->query,
856 sizeof(struct GNUNET_HashCode))) &&
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
865 "Have existing request, merging content-seen lists.\n");
866 GSF_pending_request_update_ (cr->pr,
867 (const struct GNUNET_HashCode *) &sm[1],
869 GNUNET_STATISTICS_update (GSF_stats,
871 "# client searches updated (merged content seen list)"),
877 GNUNET_STATISTICS_update (GSF_stats,
878 gettext_noop ("# client searches active"),
881 cr = GNUNET_new (struct ClientRequest);
883 GNUNET_CONTAINER_DLL_insert (lc->cr_head,
886 options = GSF_PRO_LOCAL_REQUEST;
887 if (0 != (SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY & ntohl (sm->options)))
888 options |= GSF_PRO_LOCAL_ONLY;
889 cr->pr = GSF_pending_request_create_ (options, type,
895 GNUNET_PeerIdentity)))
896 ? &sm->target : NULL, NULL, 0,
898 ntohl (sm->anonymity_level),
904 GNUNET_HashCode *) &sm[1], sc,
905 &client_response_handler,
908 if (0 != (SEARCH_MESSAGE_OPTION_CONTINUED & ntohl (sm->options)))
910 GNUNET_SERVICE_client_continue (lc->client);
913 GSF_pending_request_get_data_ (cr->pr)->has_started = GNUNET_YES;
914 GSF_local_lookup_ (cr->pr,
915 &start_p2p_processing,
921 * Handle request to sign a LOC URI (from client).
923 * @param cls identification of the client
924 * @param msg the actual message
927 handle_client_loc_sign (void *cls,
928 const struct RequestLocSignatureMessage *msg)
930 struct GSF_LocalClient *lc = cls;
931 struct GNUNET_FS_Uri base;
932 struct GNUNET_FS_Uri *loc;
933 struct GNUNET_MQ_Envelope *env;
934 struct ResponseLocSignatureMessage *resp;
936 GNUNET_break (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT ==
937 ntohl (msg->purpose));
938 base.type = GNUNET_FS_URI_CHK;
939 base.data.chk.chk = msg->chk;
940 base.data.chk.file_length = GNUNET_ntohll (msg->file_length);
941 loc = GNUNET_FS_uri_loc_create (&base,
943 GNUNET_TIME_absolute_ntoh (
944 msg->expiration_time));
945 env = GNUNET_MQ_msg (resp,
946 GNUNET_MESSAGE_TYPE_FS_REQUEST_LOC_SIGNATURE);
947 resp->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
948 resp->expiration_time = GNUNET_TIME_absolute_hton (
949 loc->data.loc.expirationTime);
950 resp->signature = loc->data.loc.contentSignature;
951 resp->peer = loc->data.loc.peer;
952 GNUNET_FS_uri_destroy (loc);
953 GNUNET_MQ_send (lc->mq,
955 GNUNET_SERVICE_client_continue (lc->client);
960 * Check INDEX_START-message.
962 * @param cls identification of the client
963 * @param ism the actual message
964 * @return #GNUNET_OK if @a ism is well-formed
967 check_client_index_start (void *cls,
968 const struct IndexStartMessage *ism)
972 GNUNET_MQ_check_zero_termination (ism);
973 if (0 != ism->reserved)
976 return GNUNET_SYSERR;
978 fn = GNUNET_STRINGS_filename_expand ((const char *) &ism[1]);
982 return GNUNET_SYSERR;
990 * We've validated the hash of the file we're about to index. Signal
991 * success to the client and update our internal data structures.
993 * @param isc the data about the index info entry for the request
996 signal_index_ok (struct IndexStartContext *isc)
998 struct GSF_LocalClient *lc = isc->lc;
999 struct GNUNET_MQ_Envelope *env;
1000 struct GNUNET_MessageHeader *msg;
1002 GNUNET_FS_add_to_index (isc->filename,
1004 env = GNUNET_MQ_msg (msg,
1005 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
1006 GNUNET_MQ_send (lc->mq,
1008 GNUNET_free (isc->filename);
1010 GNUNET_SERVICE_client_continue (lc->client);
1015 * Function called once the hash computation over an
1016 * indexed file has completed.
1018 * @param cls closure, our publishing context
1019 * @param res resulting hash, NULL on error
1022 hash_for_index_val (void *cls,
1023 const struct GNUNET_HashCode *res)
1025 struct IndexStartContext *isc = cls;
1026 struct GSF_LocalClient *lc = isc->lc;
1027 struct GNUNET_MQ_Envelope *env;
1028 struct GNUNET_MessageHeader *msg;
1030 GNUNET_CONTAINER_DLL_remove (lc->isc_head,
1034 if ((NULL == res) ||
1037 sizeof(struct GNUNET_HashCode))))
1039 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1041 "Hash mismatch trying to index file `%s' which does not have hash `%s'\n"),
1043 GNUNET_h2s (&isc->file_id));
1044 env = GNUNET_MQ_msg (msg,
1045 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
1046 GNUNET_MQ_send (lc->mq,
1048 GNUNET_SERVICE_client_continue (lc->client);
1052 signal_index_ok (isc);
1057 * Handle INDEX_START-message.
1059 * @param cls identification of the client
1060 * @param message the actual message
1063 handle_client_index_start (void *cls,
1064 const struct IndexStartMessage *ism)
1066 struct GSF_LocalClient *lc = cls;
1067 struct IndexStartContext *isc;
1074 fn = GNUNET_STRINGS_filename_expand ((const char *) &ism[1]);
1075 GNUNET_assert (NULL != fn);
1076 dev = GNUNET_ntohll (ism->device);
1077 ino = GNUNET_ntohll (ism->inode);
1078 isc = GNUNET_new (struct IndexStartContext);
1080 isc->file_id = ism->file_id;
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1082 "Received START_INDEX message for file `%s'\n",
1089 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
1095 /* fast validation OK! */
1096 signal_index_ok (isc);
1099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1100 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
1101 (unsigned long long) ino,
1102 (unsigned long long) myino,
1104 (unsigned int) mydev);
1105 /* slow validation, need to hash full file (again) */
1106 GNUNET_CONTAINER_DLL_insert (lc->isc_head,
1109 isc->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
1112 &hash_for_index_val,
1114 if (NULL == isc->fhc)
1115 hash_for_index_val (isc,
1121 * Handle INDEX_LIST_GET-message.
1123 * @param cls closure
1124 * @param message the actual message
1127 handle_client_index_list_get (void *cls,
1128 const struct GNUNET_MessageHeader *message)
1130 struct GSF_LocalClient *lc = cls;
1132 GNUNET_FS_indexing_send_list (lc->mq);
1133 GNUNET_SERVICE_client_continue (lc->client);
1138 * Handle UNINDEX-message.
1140 * @param cls identification of the client
1141 * @param message the actual message
1144 handle_client_unindex (void *cls,
1145 const struct UnindexMessage *um)
1147 struct GSF_LocalClient *lc = cls;
1148 struct GNUNET_MQ_Envelope *env;
1149 struct GNUNET_MessageHeader *msg;
1152 GNUNET_break (0 == um->reserved);
1153 found = GNUNET_FS_indexing_do_unindex (&um->file_id);
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1155 "Client requested unindexing of file `%s': %s\n",
1156 GNUNET_h2s (&um->file_id),
1157 found ? "found" : "not found");
1158 env = GNUNET_MQ_msg (msg,
1159 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
1160 GNUNET_MQ_send (lc->mq,
1162 GNUNET_SERVICE_client_continue (lc->client);
1167 * Task run during shutdown.
1172 shutdown_task (void *cls)
1174 GSF_cadet_stop_server ();
1175 if (NULL != GSF_core)
1177 GNUNET_CORE_disconnect (GSF_core);
1180 if (NULL != GSF_ats)
1182 GNUNET_ATS_performance_done (GSF_ats);
1187 GSF_pending_request_done_ ();
1189 GSF_connected_peer_done_ ();
1190 GNUNET_DATASTORE_disconnect (GSF_dsh,
1193 GNUNET_DHT_disconnect (GSF_dht);
1195 GNUNET_BLOCK_context_destroy (GSF_block_ctx);
1196 GSF_block_ctx = NULL;
1197 GNUNET_CONFIGURATION_destroy (block_cfg);
1199 GNUNET_STATISTICS_destroy (GSF_stats, GNUNET_NO);
1201 if (NULL != cover_age_task)
1203 GNUNET_SCHEDULER_cancel (cover_age_task);
1204 cover_age_task = NULL;
1206 GNUNET_FS_indexing_done ();
1207 GNUNET_LOAD_value_free (datastore_get_load);
1208 datastore_get_load = NULL;
1209 GNUNET_LOAD_value_free (GSF_rt_entry_lifetime);
1210 GSF_rt_entry_lifetime = NULL;
1215 * Function called after GNUNET_CORE_connect has succeeded
1216 * (or failed for good). Note that the private key of the
1217 * peer is intentionally not exposed here; if you need it,
1218 * your process should try to read the private key file
1219 * directly (which should work if you are authorized...).
1221 * @param cls closure
1222 * @param my_identity ID of this peer, NULL if we failed
1225 peer_init_handler (void *cls,
1226 const struct GNUNET_PeerIdentity *my_identity)
1228 if (0 != GNUNET_memcmp (&GSF_my_id,
1231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1232 "Peer identity mismatch, refusing to start!\n");
1233 GNUNET_SCHEDULER_shutdown ();
1239 * Process fs requests.
1241 * @param c configuration to use
1244 main_init (const struct GNUNET_CONFIGURATION_Handle *c)
1246 struct GNUNET_MQ_MessageHandler no_p2p_handlers[] = {
1247 GNUNET_MQ_handler_end ()
1249 struct GNUNET_MQ_MessageHandler p2p_handlers[] = {
1250 GNUNET_MQ_hd_var_size (p2p_get,
1251 GNUNET_MESSAGE_TYPE_FS_GET,
1254 GNUNET_MQ_hd_var_size (p2p_put,
1255 GNUNET_MESSAGE_TYPE_FS_PUT,
1258 GNUNET_MQ_hd_fixed_size (p2p_migration_stop,
1259 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
1260 struct MigrationStopMessage,
1262 GNUNET_MQ_handler_end ()
1267 /* this option is really only for testcases that need to disable
1268 _anonymous_ file-sharing for some reason */
1269 anon_p2p_off = (GNUNET_YES ==
1270 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
1272 "DISABLE_ANON_TRANSFER"));
1275 GNUNET_CONFIGURATION_get_value_filename (GSF_cfg,
1280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1282 "FS service is lacking HOSTKEY configuration setting. Exiting.\n"));
1283 GNUNET_SCHEDULER_shutdown ();
1284 return GNUNET_SYSERR;
1286 if (GNUNET_SYSERR ==
1287 GNUNET_CRYPTO_eddsa_key_from_file (keyfile,
1291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1292 "Failed to setup peer's private key\n");
1293 GNUNET_SCHEDULER_shutdown ();
1294 GNUNET_free (keyfile);
1295 return GNUNET_SYSERR;
1297 GNUNET_free (keyfile);
1298 GNUNET_CRYPTO_eddsa_key_get_public (&pk,
1299 &GSF_my_id.public_key);
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 GNUNET_i2s (&GSF_my_id));
1305 = GNUNET_CORE_connect (GSF_cfg,
1308 &GSF_peer_connect_handler,
1309 &GSF_peer_disconnect_handler,
1310 (GNUNET_YES == anon_p2p_off)
1313 if (NULL == GSF_core)
1315 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1316 _ ("Failed to connect to `%s' service.\n"),
1318 return GNUNET_SYSERR;
1321 GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY,
1322 &age_cover_counters,
1324 datastore_get_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1325 GSF_cadet_start_server ();
1326 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1333 * Process fs requests.
1335 * @param cls closure
1336 * @param cfg configuration to use
1337 * @param service the initialized service
1341 const struct GNUNET_CONFIGURATION_Handle *cfg,
1342 struct GNUNET_SERVICE_Handle *service)
1344 unsigned long long dqs;
1348 GNUNET_CONFIGURATION_get_value_size (GSF_cfg,
1350 "DATASTORE_QUEUE_SIZE",
1353 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1355 "DATASTORE_QUEUE_SIZE");
1358 GSF_datastore_queue_size = (unsigned int) dqs;
1359 GSF_enable_randomized_delays =
1360 GNUNET_CONFIGURATION_get_value_yesno (cfg, "fs", "DELAY");
1361 GSF_dsh = GNUNET_DATASTORE_connect (cfg);
1362 if (NULL == GSF_dsh)
1364 GNUNET_SCHEDULER_shutdown ();
1367 GSF_rt_entry_lifetime = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_FOREVER_REL);
1368 GSF_stats = GNUNET_STATISTICS_create ("fs", cfg);
1369 block_cfg = GNUNET_CONFIGURATION_create ();
1370 GSF_block_ctx = GNUNET_BLOCK_context_create (block_cfg);
1371 GNUNET_assert (NULL != GSF_block_ctx);
1372 GSF_dht = GNUNET_DHT_connect (cfg, FS_DHT_HT_SIZE);
1374 GSF_pending_request_init_ ();
1375 GSF_connected_peer_init_ ();
1376 GSF_ats = GNUNET_ATS_performance_init (GSF_cfg,
1381 if ((GNUNET_OK != GNUNET_FS_indexing_init (cfg,
1383 (GNUNET_OK != main_init (cfg)))
1385 GNUNET_SCHEDULER_shutdown ();
1386 shutdown_task (NULL);
1393 * Define "main" method using service macro.
1397 GNUNET_SERVICE_OPTION_NONE,
1400 &client_disconnect_cb,
1402 GNUNET_MQ_hd_var_size (client_index_start,
1403 GNUNET_MESSAGE_TYPE_FS_INDEX_START,
1404 struct IndexStartMessage,
1406 GNUNET_MQ_hd_fixed_size (client_index_list_get,
1407 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET,
1408 struct GNUNET_MessageHeader,
1410 GNUNET_MQ_hd_fixed_size (client_unindex,
1411 GNUNET_MESSAGE_TYPE_FS_UNINDEX,
1412 struct UnindexMessage,
1414 GNUNET_MQ_hd_var_size (client_start_search,
1415 GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
1416 struct SearchMessage,
1418 GNUNET_MQ_hd_fixed_size (client_loc_sign,
1419 GNUNET_MESSAGE_TYPE_FS_REQUEST_LOC_SIGN,
1420 struct RequestLocSignatureMessage,
1422 GNUNET_MQ_handler_end ());
1425 /* end of gnunet-service-fs.c */