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/>.
20 * @file fs/gnunet-service-fs.c
21 * @brief gnunet anonymity protocol implementation
22 * @author Christian Grothoff
26 #include "gnunet_constants.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_dht_service.h"
29 #include "gnunet_datastore_service.h"
30 #include "gnunet_load_lib.h"
31 #include "gnunet_peer_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_signatures.h"
34 #include "gnunet_statistics_service.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet-service-fs_cp.h"
38 #include "gnunet-service-fs_indexing.h"
39 #include "gnunet-service-fs_pe.h"
40 #include "gnunet-service-fs_pr.h"
41 #include "gnunet-service-fs_push.h"
42 #include "gnunet-service-fs_put.h"
43 #include "gnunet-service-fs_cadet.h"
48 * Size for the hash map for DHT requests from the FS
49 * service. Should be about the number of concurrent
50 * DHT requests we plan to make.
52 #define FS_DHT_HT_SIZE 1024
56 * How quickly do we age cover traffic? At the given
57 * time interval, remaining cover traffic counters are
58 * decremented by 1/16th.
60 #define COVER_AGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
63 * Collect an instane number of statistics? May cause excessive IPC.
65 #define INSANE_STATISTICS GNUNET_NO
70 * Doubly-linked list of requests we are performing
71 * on behalf of the same client.
77 * This is a doubly-linked list.
79 struct ClientRequest *next;
82 * This is a doubly-linked list.
84 struct ClientRequest *prev;
87 * Request this entry represents.
89 struct GSF_PendingRequest *pr;
92 * Client list this request belongs to.
94 struct GSF_LocalClient *lc;
97 * Task scheduled to destroy the request.
99 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
140 * This is a doubly linked list.
142 struct IndexStartContext *next;
145 * This is a doubly linked list.
147 struct IndexStartContext *prev;
150 * Name of the indexed file.
155 * Context for transmitting confirmation to client.
157 struct GSF_LocalClient *lc;
160 * Context for hashing of the file.
162 struct GNUNET_CRYPTO_FileHashContext *fhc;
165 * Hash of the contents of the file.
167 struct GNUNET_HashCode file_id;
175 struct GSF_LocalClient
181 struct GNUNET_SERVICE_Client *client;
184 * Queue for sending replies.
186 struct GNUNET_MQ_Handle *mq;
189 * Head of list of requests performed on behalf
190 * of this client right now.
192 struct ClientRequest *cr_head;
195 * Tail of list of requests performed on behalf
196 * of this client right now.
198 struct ClientRequest *cr_tail;
201 * This is a doubly linked list.
203 struct IndexStartContext *isc_head;
206 * This is a doubly linked list.
208 struct IndexStartContext *isc_tail;
211 * Head of linked list of responses.
213 struct ClientResponse *res_head;
216 * Tail of linked list of responses.
218 struct ClientResponse *res_tail;
223 /* ****************************** globals ****************************** */
226 * Our connection to the datastore.
228 struct GNUNET_DATASTORE_Handle *GSF_dsh;
233 const struct GNUNET_CONFIGURATION_Handle *GSF_cfg;
236 * Handle for reporting statistics.
238 struct GNUNET_STATISTICS_Handle *GSF_stats;
241 * Handle for DHT operations.
243 struct GNUNET_DHT_Handle *GSF_dht;
246 * How long do requests typically stay in the routing table?
248 struct GNUNET_LOAD_Value *GSF_rt_entry_lifetime;
251 * Running average of the observed latency to other peers (round trip).
252 * Initialized to 5s as the initial default.
254 struct GNUNET_TIME_Relative GSF_avg_latency = { 500 };
257 * Handle to ATS service.
259 struct GNUNET_ATS_PerformanceHandle *GSF_ats;
263 * Typical priorities we're seeing from other peers right now. Since
264 * most priorities will be zero, this value is the weighted average of
265 * non-zero priorities seen "recently". In order to ensure that new
266 * values do not dramatically change the ratio, values are first
267 * "capped" to a reasonable range (+N of the current value) and then
268 * averaged into the existing value by a ratio of 1:N. Hence
269 * receiving the largest possible priority can still only raise our
270 * "current_priorities" by at most 1.
272 double GSF_current_priorities;
275 * Size of the datastore queue we assume for common requests.
277 unsigned int GSF_datastore_queue_size;
280 * How many query messages have we received 'recently' that
281 * have not yet been claimed as cover traffic?
283 unsigned int GSF_cover_query_count;
286 * How many content messages have we received 'recently' that
287 * have not yet been claimed as cover traffic?
289 unsigned int GSF_cover_content_count;
294 struct GNUNET_BLOCK_Context *GSF_block_ctx;
297 * Pointer to handle to the core service (points to NULL until we've
300 struct GNUNET_CORE_Handle *GSF_core;
303 * Are we introducing randomized delays for better anonymity?
305 int GSF_enable_randomized_delays;
308 * Identity of this peer.
310 struct GNUNET_PeerIdentity GSF_my_id;
312 /* ***************************** locals ******************************* */
315 * Configuration for block library.
317 static struct GNUNET_CONFIGURATION_Handle *block_cfg;
320 * Private key of this peer. Used to sign LOC URI requests.
322 static struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
325 * ID of our task that we use to age the cover counters.
327 static struct GNUNET_SCHEDULER_Task * cover_age_task;
330 * Datastore 'GET' load tracking.
332 static struct GNUNET_LOAD_Value *datastore_get_load;
336 * Creates a fresh local client handle.
339 * @param client handle of the client
340 * @param mq message queue for @a client
341 * @return handle to local client entry
344 client_connect_cb (void *cls,
345 struct GNUNET_SERVICE_Client *client,
346 struct GNUNET_MQ_Handle *mq)
348 struct GSF_LocalClient *pos;
350 pos = GNUNET_new (struct GSF_LocalClient);
351 pos->client = client;
358 * Free the given client request.
360 * @param cls the client request to free
363 client_request_destroy (void *cls)
365 struct ClientRequest *cr = cls;
366 struct GSF_LocalClient *lc = cr->lc;
368 cr->kill_task = NULL;
369 GNUNET_CONTAINER_DLL_remove (lc->cr_head,
372 GSF_pending_request_cancel_ (cr->pr,
374 GNUNET_STATISTICS_update (GSF_stats,
375 gettext_noop ("# client searches active"),
383 * Handle a reply to a pending request. Also called if a request
384 * expires (then with data == NULL). The handler may be called
385 * many times (depending on the request type), but will not be
386 * called during or after a call to #GSF_pending_request_cancel()
387 * and will also not be called anymore after a call signalling
390 * @param cls user-specified closure
391 * @param eval evaluation of the result
392 * @param pr handle to the original pending request
393 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
394 * @param expiration when does @a data expire?
395 * @param last_transmission when was the last time we've tried to download this block? (FOREVER if unknown)
396 * @param type type of the block
397 * @param data response data, NULL on request expiration
398 * @param data_len number of bytes in @a data
401 client_response_handler (void *cls,
402 enum GNUNET_BLOCK_EvaluationResult eval,
403 struct GSF_PendingRequest *pr,
404 uint32_t reply_anonymity_level,
405 struct GNUNET_TIME_Absolute expiration,
406 struct GNUNET_TIME_Absolute last_transmission,
407 enum GNUNET_BLOCK_Type type,
411 struct ClientRequest *cr = cls;
412 struct GSF_LocalClient *lc;
413 struct GNUNET_MQ_Envelope *env;
414 struct ClientPutMessage *pm;
415 const struct GSF_PendingRequestData *prd;
419 /* local-only request, with no result, clean up. */
420 if (NULL == cr->kill_task)
421 cr->kill_task = GNUNET_SCHEDULER_add_now (&client_request_destroy,
425 prd = GSF_pending_request_get_data_ (pr);
426 GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
427 if ((prd->type != type) && (prd->type != GNUNET_BLOCK_TYPE_ANY))
432 GNUNET_STATISTICS_update (GSF_stats,
434 ("# replies received for local clients"), 1,
436 GNUNET_assert (pr == cr->pr);
438 env = GNUNET_MQ_msg_extra (pm,
440 GNUNET_MESSAGE_TYPE_FS_PUT);
441 pm->type = htonl (type);
442 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
443 pm->last_transmission = GNUNET_TIME_absolute_hton (last_transmission);
444 pm->num_transmissions = htonl (prd->num_transmissions);
445 pm->respect_offered = htonl (prd->respect_offered);
446 GNUNET_memcpy (&pm[1],
449 GNUNET_MQ_send (lc->mq,
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 "Queued reply to query `%s' for local client\n",
453 GNUNET_h2s (&prd->query));
454 if (GNUNET_BLOCK_EVALUATION_OK_LAST != eval)
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Evaluation %d - keeping query alive\n",
461 if (NULL == cr->kill_task)
462 cr->kill_task = GNUNET_SCHEDULER_add_now (&client_request_destroy,
468 * A client disconnected from us. Tear down the local client
472 * @param client handle of the client
473 * @param app_ctx the `struct GSF_LocalClient`
476 client_disconnect_cb (void *cls,
477 struct GNUNET_SERVICE_Client *client,
480 struct GSF_LocalClient *lc = app_ctx;
481 struct IndexStartContext *isc;
482 struct ClientRequest *cr;
483 struct ClientResponse *res;
485 while (NULL != (cr = lc->cr_head))
487 if (NULL != cr->kill_task)
488 GNUNET_SCHEDULER_cancel (cr->kill_task);
489 client_request_destroy (cr);
491 while (NULL != (res = lc->res_head))
493 GNUNET_CONTAINER_DLL_remove (lc->res_head,
498 while (NULL != (isc = lc->isc_head))
500 GNUNET_CONTAINER_DLL_remove (lc->isc_head,
503 GNUNET_CRYPTO_hash_file_cancel (isc->fhc);
514 * Task that periodically ages our cover traffic statistics.
516 * @param cls unused closure
519 age_cover_counters (void *cls)
521 GSF_cover_content_count = (GSF_cover_content_count * 15) / 16;
522 GSF_cover_query_count = (GSF_cover_query_count * 15) / 16;
524 GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY,
531 * We've just now completed a datastore request. Update our
532 * datastore load calculations.
534 * @param start time when the datastore request was issued
537 GSF_update_datastore_delay_ (struct GNUNET_TIME_Absolute start)
539 struct GNUNET_TIME_Relative delay;
541 delay = GNUNET_TIME_absolute_get_duration (start);
542 GNUNET_LOAD_update (datastore_get_load, delay.rel_value_us);
547 * Test if the DATABASE (GET) load on this peer is too high
548 * to even consider processing the query at
551 * @param priority priority of the request (used as a reference point to compare with the load)
552 * @return #GNUNET_YES if the load is too high to do anything (load high)
553 * #GNUNET_NO to process normally (load normal)
554 * #GNUNET_SYSERR to process for free (load low)
557 GSF_test_get_load_too_high_ (uint32_t priority)
561 ld = GNUNET_LOAD_get_load (datastore_get_load);
563 return GNUNET_SYSERR;
571 * We've received peer performance information. Update
572 * our running average for the P2P latency.
575 * @param address the address
576 * @param active is this address in active use
577 * @param bandwidth_out assigned outbound bandwidth for the connection
578 * @param bandwidth_in assigned inbound bandwidth for the connection
579 * @param prop performance data for the address (as far as known)
582 update_latencies (void *cls,
583 const struct GNUNET_HELLO_Address *address,
585 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
586 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
587 const struct GNUNET_ATS_Properties *prop)
591 /* ATS service temporarily disconnected */
595 if (GNUNET_YES != active)
597 GSF_update_peer_latency_ (&address->peer,
599 GSF_avg_latency.rel_value_us =
600 (GSF_avg_latency.rel_value_us * 31 +
601 GNUNET_MIN (5000, prop->delay.rel_value_us)) / 32;
602 GNUNET_STATISTICS_set (GSF_stats,
603 gettext_noop ("# running average P2P latency (ms)"),
604 GSF_avg_latency.rel_value_us / 1000LL,
610 * Check P2P "PUT" message.
612 * @param cls closure with the `struct GSF_ConnectedPeer`
613 * @param message the actual message
614 * @return #GNUNET_OK to keep the connection open,
615 * #GNUNET_SYSERR to close it (signal serious error)
618 check_p2p_put (void *cls,
619 const struct PutMessage *put)
621 enum GNUNET_BLOCK_Type type;
623 type = ntohl (put->type);
624 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
627 return GNUNET_SYSERR;
634 * We have a new request, consider forwarding it to the given
637 * @param cls the `struct GSF_PendingRequest`
638 * @param peer identity of the peer
639 * @param cp handle to the connected peer record
640 * @param ppd peer performance data
643 consider_request_for_forwarding (void *cls,
644 const struct GNUNET_PeerIdentity *peer,
645 struct GSF_ConnectedPeer *cp,
646 const struct GSF_PeerPerformanceData *ppd)
648 struct GSF_PendingRequest *pr = cls;
651 GSF_pending_request_test_target_ (pr, peer))
653 #if INSANE_STATISTICS
654 GNUNET_STATISTICS_update (GSF_stats,
655 gettext_noop ("# Loopback routes suppressed"), 1,
666 * Function to be called after we're done processing
667 * replies from the local lookup. If the result status
668 * code indicates that there may be more replies, plan
669 * forwarding the request.
671 * @param cls closure (NULL)
672 * @param pr the pending request we were processing
673 * @param result final datastore lookup result
676 GSF_consider_forwarding (void *cls,
677 struct GSF_PendingRequest *pr,
678 enum GNUNET_BLOCK_EvaluationResult result)
680 if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
681 return; /* we're done... */
683 GSF_pending_request_test_active_ (pr))
684 return; /* request is not actually active, skip! */
685 GSF_iterate_connected_peers_ (&consider_request_for_forwarding,
691 * Check P2P "GET" request.
694 * @param gm the actual message
695 * @return #GNUNET_OK to keep the connection open,
696 * #GNUNET_SYSERR to close it (signal serious error)
699 check_p2p_get (void *cls,
700 const struct GetMessage *gm)
707 msize = ntohs (gm->header.size);
708 bm = ntohl (gm->hash_bitmap);
716 if (msize < sizeof (struct GetMessage) + bits * sizeof (struct GNUNET_PeerIdentity))
719 return GNUNET_SYSERR;
721 bfsize = msize - sizeof (struct GetMessage) - bits * sizeof (struct GNUNET_PeerIdentity);
722 /* bfsize must be power of 2, check! */
723 if (0 != ((bfsize - 1) & bfsize))
726 return GNUNET_SYSERR;
733 * We're done with the local lookup, now consider
734 * P2P processing (depending on request options and
735 * result status). Also signal that we can now
736 * receive more request information from the client.
738 * @param cls the client doing the request (`struct GSF_LocalClient`)
739 * @param pr the pending request we were processing
740 * @param result final datastore lookup result
743 start_p2p_processing (void *cls,
744 struct GSF_PendingRequest *pr,
745 enum GNUNET_BLOCK_EvaluationResult result)
747 struct GSF_LocalClient *lc = cls;
748 struct GSF_PendingRequestData *prd;
750 GNUNET_SERVICE_client_continue (lc->client);
751 if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
752 return; /* we're done, 'pr' was already destroyed... */
753 prd = GSF_pending_request_get_data_ (pr);
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
755 "Finished database lookup for local request `%s' with result %d\n",
756 GNUNET_h2s (&prd->query),
758 if (0 == prd->anonymity_level)
762 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
763 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
764 /* the above block types MAY be available via 'cadet' */
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "Considering cadet-based download for block\n");
767 GSF_cadet_lookup_ (pr);
769 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
770 /* the above block types are in the DHT */
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
772 "Considering DHT-based search for block\n");
773 GSF_dht_lookup_ (pr);
780 GSF_consider_forwarding (NULL,
787 * Check #GNUNET_MESSAGE_TYPE_FS_START_SEARCH-message (search request
790 * @param cls identification of the client
791 * @param sm the actual message
792 * @return #GNUNET_OK if @a sm is well-formed
795 check_client_start_search (void *cls,
796 const struct SearchMessage *sm)
800 msize = ntohs (sm->header.size) - sizeof (struct SearchMessage);
801 if (0 != msize % sizeof (struct GNUNET_HashCode))
804 return GNUNET_SYSERR;
811 * Handle #GNUNET_MESSAGE_TYPE_FS_START_SEARCH-message (search request
814 * Responsible for creating the request entry itself and setting
815 * up reply callback and cancellation on client disconnect.
817 * @param cls identification of the client
818 * @param sm the actual message
821 handle_client_start_search (void *cls,
822 const struct SearchMessage *sm)
824 static struct GNUNET_PeerIdentity all_zeros;
825 struct GSF_LocalClient *lc = cls;
826 struct ClientRequest *cr;
827 struct GSF_PendingRequestData *prd;
830 enum GNUNET_BLOCK_Type type;
831 enum GSF_PendingRequestOptions options;
833 GNUNET_STATISTICS_update (GSF_stats,
834 gettext_noop ("# client searches received"),
837 msize = ntohs (sm->header.size) - sizeof (struct SearchMessage);
838 sc = msize / sizeof (struct GNUNET_HashCode);
839 type = ntohl (sm->type);
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
841 "Received request for `%s' of type %u from local client\n",
842 GNUNET_h2s (&sm->query),
843 (unsigned int) type);
845 /* detect duplicate UBLOCK requests */
846 if ((type == GNUNET_BLOCK_TYPE_FS_UBLOCK) ||
847 (type == GNUNET_BLOCK_TYPE_ANY))
852 prd = GSF_pending_request_get_data_ (cr->pr);
853 /* only unify with queries that hae not yet started local processing
854 (SEARCH_MESSAGE_OPTION_CONTINUED was always set) and that have a
855 matching query and type */
856 if ((GNUNET_YES != prd->has_started) &&
857 (0 != memcmp (&prd->query,
859 sizeof (struct GNUNET_HashCode))) &&
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
868 "Have existing request, merging content-seen lists.\n");
869 GSF_pending_request_update_ (cr->pr,
870 (const struct GNUNET_HashCode *) &sm[1],
872 GNUNET_STATISTICS_update (GSF_stats,
873 gettext_noop ("# client searches updated (merged content seen list)"),
879 GNUNET_STATISTICS_update (GSF_stats,
880 gettext_noop ("# client searches active"),
883 cr = GNUNET_new (struct ClientRequest);
885 GNUNET_CONTAINER_DLL_insert (lc->cr_head,
888 options = GSF_PRO_LOCAL_REQUEST;
889 if (0 != (SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY & ntohl (sm->options)))
890 options |= GSF_PRO_LOCAL_ONLY;
891 cr->pr = GSF_pending_request_create_ (options, type,
896 sizeof (struct GNUNET_PeerIdentity)))
897 ? &sm->target : NULL, NULL, 0,
899 ntohl (sm->anonymity_level),
904 (const struct 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 (msg->expiration_time));
944 env = GNUNET_MQ_msg (resp,
945 GNUNET_MESSAGE_TYPE_FS_REQUEST_LOC_SIGNATURE);
946 resp->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
947 resp->expiration_time = GNUNET_TIME_absolute_hton (loc->data.loc.expirationTime);
948 resp->signature = loc->data.loc.contentSignature;
949 resp->peer = loc->data.loc.peer;
950 GNUNET_FS_uri_destroy (loc);
951 GNUNET_MQ_send (lc->mq,
953 GNUNET_SERVICE_client_continue (lc->client);
958 * Check INDEX_START-message.
960 * @param cls identification of the client
961 * @param ism the actual message
962 * @return #GNUNET_OK if @a ism is well-formed
965 check_client_index_start (void *cls,
966 const struct IndexStartMessage *ism)
971 msize = ntohs (ism->header.size);
972 if (((const char *) ism)[msize - 1] != '\0')
975 return GNUNET_SYSERR;
977 if (0 != ism->reserved)
980 return GNUNET_SYSERR;
982 fn = GNUNET_STRINGS_filename_expand ((const char *) &ism[1]);
986 return GNUNET_SYSERR;
994 * We've validated the hash of the file we're about to index. Signal
995 * success to the client and update our internal data structures.
997 * @param isc the data about the index info entry for the request
1000 signal_index_ok (struct IndexStartContext *isc)
1002 struct GSF_LocalClient *lc = isc->lc;
1003 struct GNUNET_MQ_Envelope *env;
1004 struct GNUNET_MessageHeader *msg;
1006 GNUNET_FS_add_to_index (isc->filename,
1008 env = GNUNET_MQ_msg (msg,
1009 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
1010 GNUNET_MQ_send (lc->mq,
1012 GNUNET_free (isc->filename);
1014 GNUNET_SERVICE_client_continue (lc->client);
1019 * Function called once the hash computation over an
1020 * indexed file has completed.
1022 * @param cls closure, our publishing context
1023 * @param res resulting hash, NULL on error
1026 hash_for_index_val (void *cls,
1027 const struct GNUNET_HashCode *res)
1029 struct IndexStartContext *isc = cls;
1030 struct GSF_LocalClient *lc = isc->lc;
1031 struct GNUNET_MQ_Envelope *env;
1032 struct GNUNET_MessageHeader *msg;
1034 GNUNET_CONTAINER_DLL_remove (lc->isc_head,
1038 if ( (NULL == res) ||
1041 sizeof (struct GNUNET_HashCode))))
1043 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1044 _("Hash mismatch trying to index file `%s' which does not have hash `%s'\n"),
1046 GNUNET_h2s (&isc->file_id));
1047 env = GNUNET_MQ_msg (msg,
1048 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
1049 GNUNET_MQ_send (lc->mq,
1051 GNUNET_SERVICE_client_continue (lc->client);
1055 signal_index_ok (isc);
1060 * Handle INDEX_START-message.
1062 * @param cls identification of the client
1063 * @param message the actual message
1066 handle_client_index_start (void *cls,
1067 const struct IndexStartMessage *ism)
1069 struct GSF_LocalClient *lc = cls;
1070 struct IndexStartContext *isc;
1077 fn = GNUNET_STRINGS_filename_expand ((const char *) &ism[1]);
1078 GNUNET_assert (NULL != fn);
1079 dev = GNUNET_ntohll (ism->device);
1080 ino = GNUNET_ntohll (ism->inode);
1081 isc = GNUNET_new (struct IndexStartContext);
1083 isc->file_id = ism->file_id;
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085 "Received START_INDEX message for file `%s'\n",
1090 if ( ( (dev != 0) ||
1092 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
1098 /* fast validation OK! */
1099 signal_index_ok (isc);
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1103 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
1104 (unsigned long long) ino,
1105 (unsigned long long) myino,
1107 (unsigned int) mydev);
1108 /* slow validation, need to hash full file (again) */
1109 GNUNET_CONTAINER_DLL_insert (lc->isc_head,
1112 isc->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
1115 &hash_for_index_val,
1117 if (NULL == isc->fhc)
1118 hash_for_index_val (isc,
1124 * Handle INDEX_LIST_GET-message.
1126 * @param cls closure
1127 * @param message the actual message
1130 handle_client_index_list_get (void *cls,
1131 const struct GNUNET_MessageHeader *message)
1133 struct GSF_LocalClient *lc = cls;
1135 GNUNET_FS_indexing_send_list (lc->mq);
1136 GNUNET_SERVICE_client_continue (lc->client);
1141 * Handle UNINDEX-message.
1143 * @param cls identification of the client
1144 * @param message the actual message
1147 handle_client_unindex (void *cls,
1148 const struct UnindexMessage *um)
1150 struct GSF_LocalClient *lc = cls;
1151 struct GNUNET_MQ_Envelope *env;
1152 struct GNUNET_MessageHeader *msg;
1155 GNUNET_break (0 == um->reserved);
1156 found = GNUNET_FS_indexing_do_unindex (&um->file_id);
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158 "Client requested unindexing of file `%s': %s\n",
1159 GNUNET_h2s (&um->file_id),
1160 found ? "found" : "not found");
1161 env = GNUNET_MQ_msg (msg,
1162 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
1163 GNUNET_MQ_send (lc->mq,
1165 GNUNET_SERVICE_client_continue (lc->client);
1170 * Task run during shutdown.
1175 shutdown_task (void *cls)
1177 GSF_cadet_stop_server ();
1178 if (NULL != GSF_core)
1180 GNUNET_CORE_disconnect (GSF_core);
1183 if (NULL != GSF_ats)
1185 GNUNET_ATS_performance_done (GSF_ats);
1190 GSF_pending_request_done_ ();
1192 GSF_connected_peer_done_ ();
1193 GNUNET_DATASTORE_disconnect (GSF_dsh,
1196 GNUNET_DHT_disconnect (GSF_dht);
1198 GNUNET_BLOCK_context_destroy (GSF_block_ctx);
1199 GSF_block_ctx = NULL;
1200 GNUNET_CONFIGURATION_destroy (block_cfg);
1202 GNUNET_STATISTICS_destroy (GSF_stats, GNUNET_NO);
1204 if (NULL != cover_age_task)
1206 GNUNET_SCHEDULER_cancel (cover_age_task);
1207 cover_age_task = NULL;
1209 GNUNET_FS_indexing_done ();
1210 GNUNET_LOAD_value_free (datastore_get_load);
1211 datastore_get_load = NULL;
1212 GNUNET_LOAD_value_free (GSF_rt_entry_lifetime);
1213 GSF_rt_entry_lifetime = NULL;
1218 * Function called after GNUNET_CORE_connect has succeeded
1219 * (or failed for good). Note that the private key of the
1220 * peer is intentionally not exposed here; if you need it,
1221 * your process should try to read the private key file
1222 * directly (which should work if you are authorized...).
1224 * @param cls closure
1225 * @param my_identity ID of this peer, NULL if we failed
1228 peer_init_handler (void *cls,
1229 const struct GNUNET_PeerIdentity *my_identity)
1231 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&GSF_my_id,
1234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1235 "Peer identity missmatch, refusing to start!\n");
1236 GNUNET_SCHEDULER_shutdown ();
1242 * Process fs requests.
1244 * @param c configuration to use
1247 main_init (const struct GNUNET_CONFIGURATION_Handle *c)
1249 struct GNUNET_MQ_MessageHandler no_p2p_handlers[] = {
1250 GNUNET_MQ_handler_end ()
1252 struct GNUNET_MQ_MessageHandler p2p_handlers[] = {
1253 GNUNET_MQ_hd_var_size (p2p_get,
1254 GNUNET_MESSAGE_TYPE_FS_GET,
1257 GNUNET_MQ_hd_var_size (p2p_put,
1258 GNUNET_MESSAGE_TYPE_FS_PUT,
1261 GNUNET_MQ_hd_fixed_size (p2p_migration_stop,
1262 GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
1263 struct MigrationStopMessage,
1265 GNUNET_MQ_handler_end ()
1270 /* this option is really only for testcases that need to disable
1271 _anonymous_ file-sharing for some reason */
1272 anon_p2p_off = (GNUNET_YES ==
1273 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
1275 "DISABLE_ANON_TRANSFER"));
1278 GNUNET_CONFIGURATION_get_value_filename (GSF_cfg,
1283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1284 _("FS service is lacking HOSTKEY configuration setting. Exiting.\n"));
1285 GNUNET_SCHEDULER_shutdown ();
1286 return GNUNET_SYSERR;
1288 pk = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
1289 GNUNET_free (keyfile);
1290 GNUNET_assert (NULL != pk);
1291 GNUNET_CRYPTO_eddsa_key_get_public (pk,
1292 &GSF_my_id.public_key);
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1296 GNUNET_i2s (&GSF_my_id));
1298 = GNUNET_CORE_connect (GSF_cfg,
1301 &GSF_peer_connect_handler,
1302 &GSF_peer_disconnect_handler,
1303 (GNUNET_YES == anon_p2p_off)
1306 if (NULL == GSF_core)
1308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1309 _("Failed to connect to `%s' service.\n"),
1311 return GNUNET_SYSERR;
1314 GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY,
1315 &age_cover_counters,
1317 datastore_get_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1318 GSF_cadet_start_server ();
1319 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1326 * Process fs requests.
1328 * @param cls closure
1329 * @param cfg configuration to use
1330 * @param service the initialized service
1334 const struct GNUNET_CONFIGURATION_Handle *cfg,
1335 struct GNUNET_SERVICE_Handle *service)
1337 unsigned long long dqs;
1341 GNUNET_CONFIGURATION_get_value_size (GSF_cfg,
1343 "DATASTORE_QUEUE_SIZE",
1346 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1348 "DATASTORE_QUEUE_SIZE");
1351 GSF_datastore_queue_size = (unsigned int) dqs;
1352 GSF_enable_randomized_delays =
1353 GNUNET_CONFIGURATION_get_value_yesno (cfg, "fs", "DELAY");
1354 GSF_dsh = GNUNET_DATASTORE_connect (cfg);
1355 if (NULL == GSF_dsh)
1357 GNUNET_SCHEDULER_shutdown ();
1360 GSF_rt_entry_lifetime = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_FOREVER_REL);
1361 GSF_stats = GNUNET_STATISTICS_create ("fs", cfg);
1362 block_cfg = GNUNET_CONFIGURATION_create ();
1363 GSF_block_ctx = GNUNET_BLOCK_context_create (block_cfg);
1364 GNUNET_assert (NULL != GSF_block_ctx);
1365 GSF_dht = GNUNET_DHT_connect (cfg, FS_DHT_HT_SIZE);
1367 GSF_pending_request_init_ ();
1368 GSF_connected_peer_init_ ();
1369 GSF_ats = GNUNET_ATS_performance_init (GSF_cfg,
1374 if ( (GNUNET_OK != GNUNET_FS_indexing_init (cfg,
1376 (GNUNET_OK != main_init (cfg)) )
1378 GNUNET_SCHEDULER_shutdown ();
1379 shutdown_task (NULL);
1386 * Define "main" method using service macro.
1390 GNUNET_SERVICE_OPTION_NONE,
1393 &client_disconnect_cb,
1395 GNUNET_MQ_hd_var_size (client_index_start,
1396 GNUNET_MESSAGE_TYPE_FS_INDEX_START,
1397 struct IndexStartMessage,
1399 GNUNET_MQ_hd_fixed_size (client_index_list_get,
1400 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET,
1401 struct GNUNET_MessageHeader,
1403 GNUNET_MQ_hd_fixed_size (client_unindex,
1404 GNUNET_MESSAGE_TYPE_FS_UNINDEX,
1405 struct UnindexMessage,
1407 GNUNET_MQ_hd_var_size (client_start_search,
1408 GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
1409 struct SearchMessage,
1411 GNUNET_MQ_hd_fixed_size (client_loc_sign,
1412 GNUNET_MESSAGE_TYPE_FS_REQUEST_LOC_SIGN,
1413 struct RequestLocSignatureMessage,
1415 GNUNET_MQ_handler_end ());
1418 /* end of gnunet-service-fs.c */