gnunet-service-fs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fs/gnunet-service-fs.c
  19. * @brief gnunet anonymity protocol implementation
  20. * @author Christian Grothoff
  21. *
  22. * To use:
  23. * - consider re-issue GSF_dht_lookup_ after non-DHT reply received
  24. * - implement 'SUPPORT_DELAYS'
  25. *
  26. */
  27. #include "platform.h"
  28. #include <float.h>
  29. #include "gnunet_constants.h"
  30. #include "gnunet_core_service.h"
  31. #include "gnunet_dht_service.h"
  32. #include "gnunet_datastore_service.h"
  33. #include "gnunet_load_lib.h"
  34. #include "gnunet_peer_lib.h"
  35. #include "gnunet_protocols.h"
  36. #include "gnunet_signatures.h"
  37. #include "gnunet_statistics_service.h"
  38. #include "gnunet_transport_service.h"
  39. #include "gnunet_util_lib.h"
  40. #include "gnunet-service-fs_cp.h"
  41. #include "gnunet-service-fs_indexing.h"
  42. #include "gnunet-service-fs_lc.h"
  43. #include "gnunet-service-fs_pe.h"
  44. #include "gnunet-service-fs_pr.h"
  45. #include "gnunet-service-fs_push.h"
  46. #include "gnunet-service-fs_put.h"
  47. #include "fs.h"
  48. /**
  49. * Size for the hash map for DHT requests from the FS
  50. * service. Should be about the number of concurrent
  51. * DHT requests we plan to make.
  52. */
  53. #define FS_DHT_HT_SIZE 1024
  54. /**
  55. * How quickly do we age cover traffic? At the given
  56. * time interval, remaining cover traffic counters are
  57. * decremented by 1/16th.
  58. */
  59. #define COVER_AGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  60. /* ****************************** globals ****************************** */
  61. /**
  62. * Our connection to the datastore.
  63. */
  64. struct GNUNET_DATASTORE_Handle *GSF_dsh;
  65. /**
  66. * Our configuration.
  67. */
  68. const struct GNUNET_CONFIGURATION_Handle *GSF_cfg;
  69. /**
  70. * Handle for reporting statistics.
  71. */
  72. struct GNUNET_STATISTICS_Handle *GSF_stats;
  73. /**
  74. * Handle for DHT operations.
  75. */
  76. struct GNUNET_DHT_Handle *GSF_dht;
  77. /**
  78. * How long do requests typically stay in the routing table?
  79. */
  80. struct GNUNET_LOAD_Value *GSF_rt_entry_lifetime;
  81. /**
  82. * Typical priorities we're seeing from other peers right now. Since
  83. * most priorities will be zero, this value is the weighted average of
  84. * non-zero priorities seen "recently". In order to ensure that new
  85. * values do not dramatically change the ratio, values are first
  86. * "capped" to a reasonable range (+N of the current value) and then
  87. * averaged into the existing value by a ratio of 1:N. Hence
  88. * receiving the largest possible priority can still only raise our
  89. * "current_priorities" by at most 1.
  90. */
  91. double GSF_current_priorities;
  92. /**
  93. * How many query messages have we received 'recently' that
  94. * have not yet been claimed as cover traffic?
  95. */
  96. unsigned int GSF_cover_query_count;
  97. /**
  98. * How many content messages have we received 'recently' that
  99. * have not yet been claimed as cover traffic?
  100. */
  101. unsigned int GSF_cover_content_count;
  102. /**
  103. * Our block context.
  104. */
  105. struct GNUNET_BLOCK_Context *GSF_block_ctx;
  106. /**
  107. * Pointer to handle to the core service (points to NULL until we've
  108. * connected to it).
  109. */
  110. struct GNUNET_CORE_Handle *GSF_core;
  111. /**
  112. * Are we introducing randomized delays for better anonymity?
  113. */
  114. int GSF_enable_randomized_delays;
  115. /* ***************************** locals ******************************* */
  116. /**
  117. * Configuration for block library.
  118. */
  119. static struct GNUNET_CONFIGURATION_Handle *block_cfg;
  120. /**
  121. * ID of our task that we use to age the cover counters.
  122. */
  123. static GNUNET_SCHEDULER_TaskIdentifier cover_age_task;
  124. /**
  125. * Datastore 'GET' load tracking.
  126. */
  127. static struct GNUNET_LOAD_Value *datastore_get_load;
  128. /**
  129. * Identity of this peer.
  130. */
  131. static struct GNUNET_PeerIdentity my_id;
  132. /**
  133. * Task that periodically ages our cover traffic statistics.
  134. *
  135. * @param cls unused closure
  136. * @param tc task context
  137. */
  138. static void
  139. age_cover_counters (void *cls,
  140. const struct GNUNET_SCHEDULER_TaskContext *tc)
  141. {
  142. GSF_cover_content_count = (GSF_cover_content_count * 15) / 16;
  143. GSF_cover_query_count = (GSF_cover_query_count * 15) / 16;
  144. cover_age_task = GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY,
  145. &age_cover_counters,
  146. NULL);
  147. }
  148. /**
  149. * We've just now completed a datastore request. Update our
  150. * datastore load calculations.
  151. *
  152. * @param start time when the datastore request was issued
  153. */
  154. void
  155. GSF_update_datastore_delay_ (struct GNUNET_TIME_Absolute start)
  156. {
  157. struct GNUNET_TIME_Relative delay;
  158. delay = GNUNET_TIME_absolute_get_duration (start);
  159. GNUNET_LOAD_update (datastore_get_load,
  160. delay.rel_value);
  161. }
  162. /**
  163. * Test if the DATABASE (GET) load on this peer is too high
  164. * to even consider processing the query at
  165. * all.
  166. *
  167. * @return GNUNET_YES if the load is too high to do anything (load high)
  168. * GNUNET_NO to process normally (load normal)
  169. * GNUNET_SYSERR to process for free (load low)
  170. */
  171. int
  172. GSF_test_get_load_too_high_ (uint32_t priority)
  173. {
  174. double ld;
  175. ld = GNUNET_LOAD_get_load (datastore_get_load);
  176. if (ld < 1)
  177. return GNUNET_SYSERR;
  178. if (ld <= priority)
  179. return GNUNET_NO;
  180. return GNUNET_YES;
  181. }
  182. /**
  183. * Handle P2P "PUT" message.
  184. *
  185. * @param cls closure, always NULL
  186. * @param other the other peer involved (sender or receiver, NULL
  187. * for loopback messages where we are both sender and receiver)
  188. * @param message the actual message
  189. * @param atsi performance information
  190. * @return GNUNET_OK to keep the connection open,
  191. * GNUNET_SYSERR to close it (signal serious error)
  192. */
  193. static int
  194. handle_p2p_put (void *cls,
  195. const struct GNUNET_PeerIdentity *other,
  196. const struct GNUNET_MessageHeader *message,
  197. const struct GNUNET_TRANSPORT_ATS_Information *atsi)
  198. {
  199. struct GSF_ConnectedPeer *cp;
  200. cp = GSF_peer_get_ (other);
  201. if (NULL == cp)
  202. {
  203. GNUNET_break (0);
  204. return GNUNET_OK;
  205. }
  206. GSF_cover_content_count++;
  207. return GSF_handle_p2p_content_ (cp, message);
  208. }
  209. /**
  210. * We have a new request, consider forwarding it to the given
  211. * peer.
  212. *
  213. * @param cls the 'struct GSF_PendingRequest'
  214. * @param peer identity of the peer
  215. * @param cp handle to the connected peer record
  216. * @param ppd peer performance data
  217. */
  218. static void
  219. consider_request_for_forwarding (void *cls,
  220. const struct GNUNET_PeerIdentity *peer,
  221. struct GSF_ConnectedPeer *cp,
  222. const struct GSF_PeerPerformanceData *ppd)
  223. {
  224. struct GSF_PendingRequest *pr = cls;
  225. GSF_plan_add_ (cp, pr);
  226. }
  227. /**
  228. * Function to be called after we're done processing
  229. * replies from the local lookup. If the result status
  230. * code indicates that there may be more replies, plan
  231. * forwarding the request.
  232. *
  233. * @param cls closure (NULL)
  234. * @param pr the pending request we were processing
  235. * @param result final datastore lookup result
  236. */
  237. static void
  238. consider_forwarding (void *cls,
  239. struct GSF_PendingRequest *pr,
  240. enum GNUNET_BLOCK_EvaluationResult result)
  241. {
  242. if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
  243. return; /* we're done... */
  244. GSF_iterate_connected_peers_ (&consider_request_for_forwarding,
  245. pr);
  246. }
  247. /**
  248. * Handle P2P "GET" request.
  249. *
  250. * @param cls closure, always NULL
  251. * @param other the other peer involved (sender or receiver, NULL
  252. * for loopback messages where we are both sender and receiver)
  253. * @param message the actual message
  254. * @param atsi performance information
  255. * @return GNUNET_OK to keep the connection open,
  256. * GNUNET_SYSERR to close it (signal serious error)
  257. */
  258. static int
  259. handle_p2p_get (void *cls,
  260. const struct GNUNET_PeerIdentity *other,
  261. const struct GNUNET_MessageHeader *message,
  262. const struct GNUNET_TRANSPORT_ATS_Information *atsi)
  263. {
  264. struct GSF_PendingRequest *pr;
  265. pr = GSF_handle_p2p_query_ (other, message);
  266. if (NULL == pr)
  267. return GNUNET_SYSERR;
  268. GSF_local_lookup_ (pr,
  269. &consider_forwarding,
  270. NULL);
  271. return GNUNET_OK;
  272. }
  273. /**
  274. * We're done with the local lookup, now consider
  275. * P2P processing (depending on request options and
  276. * result status). Also signal that we can now
  277. * receive more request information from the client.
  278. *
  279. * @param cls the client doing the request ('struct GNUNET_SERVER_Client')
  280. * @param pr the pending request we were processing
  281. * @param result final datastore lookup result
  282. */
  283. static void
  284. start_p2p_processing (void *cls,
  285. struct GSF_PendingRequest *pr,
  286. enum GNUNET_BLOCK_EvaluationResult result)
  287. {
  288. struct GNUNET_SERVER_Client *client = cls;
  289. struct GSF_PendingRequestData *prd;
  290. prd = GSF_pending_request_get_data_ (pr);
  291. #if DEBUG_FS_CLIENT
  292. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  293. "Finished database lookup for local request `%s' with result %d\n",
  294. GNUNET_h2s (&prd->query),
  295. result);
  296. #endif
  297. GNUNET_SERVER_receive_done (client,
  298. GNUNET_OK);
  299. if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
  300. return; /* we're done, 'pr' was already destroyed... */
  301. if (0 != (GSF_PRO_LOCAL_ONLY & prd->options) )
  302. {
  303. GSF_pending_request_cancel_ (pr, GNUNET_YES);
  304. return;
  305. }
  306. GSF_dht_lookup_ (pr);
  307. consider_forwarding (NULL, pr, result);
  308. }
  309. /**
  310. * Handle START_SEARCH-message (search request from client).
  311. *
  312. * @param cls closure
  313. * @param client identification of the client
  314. * @param message the actual message
  315. */
  316. static void
  317. handle_start_search (void *cls,
  318. struct GNUNET_SERVER_Client *client,
  319. const struct GNUNET_MessageHeader *message)
  320. {
  321. struct GSF_PendingRequest *pr;
  322. pr = GSF_local_client_start_search_handler_ (client, message);
  323. if (NULL == pr)
  324. {
  325. /* GNUNET_SERVER_receive_done was already called! */
  326. return;
  327. }
  328. GSF_local_lookup_ (pr,
  329. &start_p2p_processing,
  330. client);
  331. }
  332. /**
  333. * Task run during shutdown.
  334. *
  335. * @param cls unused
  336. * @param tc unused
  337. */
  338. static void
  339. shutdown_task (void *cls,
  340. const struct GNUNET_SCHEDULER_TaskContext *tc)
  341. {
  342. if (NULL != GSF_core)
  343. {
  344. GNUNET_CORE_disconnect (GSF_core);
  345. GSF_core = NULL;
  346. }
  347. GSF_put_done_ ();
  348. GSF_push_done_ ();
  349. GSF_pending_request_done_ ();
  350. GSF_plan_done ();
  351. GSF_connected_peer_done_ ();
  352. GNUNET_DATASTORE_disconnect (GSF_dsh, GNUNET_NO);
  353. GSF_dsh = NULL;
  354. GNUNET_DHT_disconnect (GSF_dht);
  355. GSF_dht = NULL;
  356. GNUNET_BLOCK_context_destroy (GSF_block_ctx);
  357. GSF_block_ctx = NULL;
  358. GNUNET_CONFIGURATION_destroy (block_cfg);
  359. block_cfg = NULL;
  360. GNUNET_STATISTICS_destroy (GSF_stats, GNUNET_NO);
  361. GSF_stats = NULL;
  362. if (GNUNET_SCHEDULER_NO_TASK != cover_age_task)
  363. {
  364. GNUNET_SCHEDULER_cancel (cover_age_task);
  365. cover_age_task = GNUNET_SCHEDULER_NO_TASK;
  366. }
  367. GNUNET_FS_indexing_done ();
  368. GNUNET_LOAD_value_free (datastore_get_load);
  369. datastore_get_load = NULL;
  370. GNUNET_LOAD_value_free (GSF_rt_entry_lifetime);
  371. GSF_rt_entry_lifetime = NULL;
  372. }
  373. /**
  374. * Function called for each pending request whenever a new
  375. * peer connects, giving us a chance to decide about submitting
  376. * the existing request to the new peer.
  377. *
  378. * @param cls the 'struct GSF_ConnectedPeer' of the new peer
  379. * @param key query for the request
  380. * @param pr handle to the pending request
  381. * @return GNUNET_YES to continue to iterate
  382. */
  383. static int
  384. consider_peer_for_forwarding (void *cls,
  385. const GNUNET_HashCode *key,
  386. struct GSF_PendingRequest *pr)
  387. {
  388. struct GSF_ConnectedPeer *cp = cls;
  389. GSF_plan_add_ (cp, pr);
  390. return GNUNET_YES;
  391. }
  392. /**
  393. * Method called whenever a given peer connects.
  394. *
  395. * @param cls closure, not used
  396. * @param peer peer identity this notification is about
  397. * @param atsi performance information
  398. */
  399. static void
  400. peer_connect_handler (void *cls,
  401. const struct GNUNET_PeerIdentity *peer,
  402. const struct GNUNET_TRANSPORT_ATS_Information *atsi)
  403. {
  404. struct GSF_ConnectedPeer *cp;
  405. if (0 == memcmp (&my_id, peer, sizeof (struct GNUNET_PeerIdentity)))
  406. return;
  407. cp = GSF_peer_connect_handler_ (peer, atsi);
  408. if (NULL == cp)
  409. return;
  410. GSF_iterate_pending_requests_ (&consider_peer_for_forwarding,
  411. cp);
  412. }
  413. /**
  414. * Function called after GNUNET_CORE_connect has succeeded
  415. * (or failed for good). Note that the private key of the
  416. * peer is intentionally not exposed here; if you need it,
  417. * your process should try to read the private key file
  418. * directly (which should work if you are authorized...).
  419. *
  420. * @param cls closure
  421. * @param server handle to the server, NULL if we failed
  422. * @param my_identity ID of this peer, NULL if we failed
  423. * @param publicKey public key of this peer, NULL if we failed
  424. */
  425. static void
  426. peer_init_handler (void *cls,
  427. struct GNUNET_CORE_Handle * server,
  428. const struct GNUNET_PeerIdentity *
  429. my_identity,
  430. const struct
  431. GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
  432. publicKey)
  433. {
  434. my_id = *my_identity;
  435. }
  436. /**
  437. * Process fs requests.
  438. *
  439. * @param server the initialized server
  440. * @param c configuration to use
  441. */
  442. static int
  443. main_init (struct GNUNET_SERVER_Handle *server,
  444. const struct GNUNET_CONFIGURATION_Handle *c)
  445. {
  446. static const struct GNUNET_CORE_MessageHandler p2p_handlers[] =
  447. {
  448. { &handle_p2p_get,
  449. GNUNET_MESSAGE_TYPE_FS_GET, 0 },
  450. { &handle_p2p_put,
  451. GNUNET_MESSAGE_TYPE_FS_PUT, 0 },
  452. { &GSF_handle_p2p_migration_stop_,
  453. GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
  454. sizeof (struct MigrationStopMessage) },
  455. { NULL, 0, 0 }
  456. };
  457. static const struct GNUNET_SERVER_MessageHandler handlers[] = {
  458. {&GNUNET_FS_handle_index_start, NULL,
  459. GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
  460. {&GNUNET_FS_handle_index_list_get, NULL,
  461. GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET, sizeof(struct GNUNET_MessageHeader) },
  462. {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
  463. sizeof (struct UnindexMessage) },
  464. {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
  465. 0 },
  466. {NULL, NULL, 0, 0}
  467. };
  468. GSF_core = GNUNET_CORE_connect (GSF_cfg,
  469. 2, /* larger? */
  470. NULL,
  471. &peer_init_handler,
  472. &peer_connect_handler,
  473. &GSF_peer_disconnect_handler_,
  474. &GSF_peer_status_handler_,
  475. NULL, GNUNET_NO,
  476. NULL, GNUNET_NO,
  477. p2p_handlers);
  478. if (NULL == GSF_core)
  479. {
  480. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  481. _("Failed to connect to `%s' service.\n"),
  482. "core");
  483. return GNUNET_SYSERR;
  484. }
  485. GNUNET_SERVER_disconnect_notify (server,
  486. &GSF_client_disconnect_handler_,
  487. NULL);
  488. GNUNET_SERVER_add_handlers (server, handlers);
  489. cover_age_task = GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY,
  490. &age_cover_counters,
  491. NULL);
  492. datastore_get_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
  493. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
  494. &shutdown_task,
  495. NULL);
  496. return GNUNET_OK;
  497. }
  498. /**
  499. * Process fs requests.
  500. *
  501. * @param cls closure
  502. * @param server the initialized server
  503. * @param cfg configuration to use
  504. */
  505. static void
  506. run (void *cls,
  507. struct GNUNET_SERVER_Handle *server,
  508. const struct GNUNET_CONFIGURATION_Handle *cfg)
  509. {
  510. GSF_cfg = cfg;
  511. GSF_enable_randomized_delays = GNUNET_CONFIGURATION_get_value_yesno (cfg, "fs", "DELAY");
  512. GSF_dsh = GNUNET_DATASTORE_connect (cfg);
  513. if (NULL == GSF_dsh)
  514. {
  515. GNUNET_SCHEDULER_shutdown ();
  516. return;
  517. }
  518. GSF_rt_entry_lifetime = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_FOREVER_REL);
  519. GSF_stats = GNUNET_STATISTICS_create ("fs", cfg);
  520. block_cfg = GNUNET_CONFIGURATION_create ();
  521. GNUNET_CONFIGURATION_set_value_string (block_cfg,
  522. "block",
  523. "PLUGINS",
  524. "fs");
  525. GSF_block_ctx = GNUNET_BLOCK_context_create (block_cfg);
  526. GNUNET_assert (NULL != GSF_block_ctx);
  527. GSF_dht = GNUNET_DHT_connect (cfg,
  528. FS_DHT_HT_SIZE);
  529. GSF_plan_init ();
  530. GSF_pending_request_init_ ();
  531. GSF_connected_peer_init_ ();
  532. GSF_push_init_ ();
  533. GSF_put_init_ ();
  534. if ( (GNUNET_OK != GNUNET_FS_indexing_init (cfg, GSF_dsh)) ||
  535. (GNUNET_OK != main_init (server, cfg)) )
  536. {
  537. GNUNET_SCHEDULER_shutdown ();
  538. shutdown_task (NULL, NULL);
  539. return;
  540. }
  541. }
  542. /**
  543. * The main function for the fs service.
  544. *
  545. * @param argc number of arguments from the command line
  546. * @param argv command line arguments
  547. * @return 0 ok, 1 on error
  548. */
  549. int
  550. main (int argc, char *const *argv)
  551. {
  552. return (GNUNET_OK ==
  553. GNUNET_SERVICE_run (argc,
  554. argv,
  555. "fs",
  556. GNUNET_SERVICE_OPTION_NONE,
  557. &run, NULL)) ? 0 : 1;
  558. }
  559. /* end of gnunet-service-fs.c */