gnunet-service-fs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. #include "platform.h"
  23. #include <float.h>
  24. #include "gnunet_constants.h"
  25. #include "gnunet_core_service.h"
  26. #include "gnunet_dht_service.h"
  27. #include "gnunet_datastore_service.h"
  28. #include "gnunet_load_lib.h"
  29. #include "gnunet_peer_lib.h"
  30. #include "gnunet_protocols.h"
  31. #include "gnunet_signatures.h"
  32. #include "gnunet_statistics_service.h"
  33. #include "gnunet_transport_service.h"
  34. #include "gnunet_util_lib.h"
  35. #include "gnunet-service-fs_cp.h"
  36. #include "gnunet-service-fs_indexing.h"
  37. #include "gnunet-service-fs_lc.h"
  38. #include "gnunet-service-fs_pe.h"
  39. #include "gnunet-service-fs_pr.h"
  40. #include "gnunet-service-fs_push.h"
  41. #include "gnunet-service-fs_put.h"
  42. #include "gnunet-service-fs_cadet.h"
  43. #include "fs.h"
  44. /**
  45. * Size for the hash map for DHT requests from the FS
  46. * service. Should be about the number of concurrent
  47. * DHT requests we plan to make.
  48. */
  49. #define FS_DHT_HT_SIZE 1024
  50. /**
  51. * How quickly do we age cover traffic? At the given
  52. * time interval, remaining cover traffic counters are
  53. * decremented by 1/16th.
  54. */
  55. #define COVER_AGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  56. /**
  57. * Collect an instane number of statistics? May cause excessive IPC.
  58. */
  59. #define INSANE_STATISTICS GNUNET_NO
  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. * Running average of the observed latency to other peers (round trip).
  83. * Initialized to 5s as the initial default.
  84. */
  85. struct GNUNET_TIME_Relative GSF_avg_latency = { 500 };
  86. /**
  87. * Handle to ATS service.
  88. */
  89. struct GNUNET_ATS_PerformanceHandle *GSF_ats;
  90. /**
  91. * Typical priorities we're seeing from other peers right now. Since
  92. * most priorities will be zero, this value is the weighted average of
  93. * non-zero priorities seen "recently". In order to ensure that new
  94. * values do not dramatically change the ratio, values are first
  95. * "capped" to a reasonable range (+N of the current value) and then
  96. * averaged into the existing value by a ratio of 1:N. Hence
  97. * receiving the largest possible priority can still only raise our
  98. * "current_priorities" by at most 1.
  99. */
  100. double GSF_current_priorities;
  101. /**
  102. * Size of the datastore queue we assume for common requests.
  103. */
  104. unsigned int GSF_datastore_queue_size;
  105. /**
  106. * How many query messages have we received 'recently' that
  107. * have not yet been claimed as cover traffic?
  108. */
  109. unsigned int GSF_cover_query_count;
  110. /**
  111. * How many content messages have we received 'recently' that
  112. * have not yet been claimed as cover traffic?
  113. */
  114. unsigned int GSF_cover_content_count;
  115. /**
  116. * Our block context.
  117. */
  118. struct GNUNET_BLOCK_Context *GSF_block_ctx;
  119. /**
  120. * Pointer to handle to the core service (points to NULL until we've
  121. * connected to it).
  122. */
  123. struct GNUNET_CORE_Handle *GSF_core;
  124. /**
  125. * Are we introducing randomized delays for better anonymity?
  126. */
  127. int GSF_enable_randomized_delays;
  128. /* ***************************** locals ******************************* */
  129. /**
  130. * Configuration for block library.
  131. */
  132. static struct GNUNET_CONFIGURATION_Handle *block_cfg;
  133. /**
  134. * ID of our task that we use to age the cover counters.
  135. */
  136. static GNUNET_SCHEDULER_TaskIdentifier cover_age_task;
  137. /**
  138. * Datastore 'GET' load tracking.
  139. */
  140. static struct GNUNET_LOAD_Value *datastore_get_load;
  141. /**
  142. * Identity of this peer.
  143. */
  144. static struct GNUNET_PeerIdentity my_id;
  145. /**
  146. * Task that periodically ages our cover traffic statistics.
  147. *
  148. * @param cls unused closure
  149. * @param tc task context
  150. */
  151. static void
  152. age_cover_counters (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  153. {
  154. GSF_cover_content_count = (GSF_cover_content_count * 15) / 16;
  155. GSF_cover_query_count = (GSF_cover_query_count * 15) / 16;
  156. cover_age_task =
  157. GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, &age_cover_counters,
  158. NULL);
  159. }
  160. /**
  161. * We've just now completed a datastore request. Update our
  162. * datastore load calculations.
  163. *
  164. * @param start time when the datastore request was issued
  165. */
  166. void
  167. GSF_update_datastore_delay_ (struct GNUNET_TIME_Absolute start)
  168. {
  169. struct GNUNET_TIME_Relative delay;
  170. delay = GNUNET_TIME_absolute_get_duration (start);
  171. GNUNET_LOAD_update (datastore_get_load, delay.rel_value_us);
  172. }
  173. /**
  174. * Test if the DATABASE (GET) load on this peer is too high
  175. * to even consider processing the query at
  176. * all.
  177. *
  178. * @param priority priority of the request (used as a reference point to compare with the load)
  179. * @return GNUNET_YES if the load is too high to do anything (load high)
  180. * GNUNET_NO to process normally (load normal)
  181. * GNUNET_SYSERR to process for free (load low)
  182. */
  183. int
  184. GSF_test_get_load_too_high_ (uint32_t priority)
  185. {
  186. double ld;
  187. ld = GNUNET_LOAD_get_load (datastore_get_load);
  188. if (ld < 1)
  189. return GNUNET_SYSERR;
  190. if (ld <= priority)
  191. return GNUNET_NO;
  192. return GNUNET_YES;
  193. }
  194. /**
  195. * We've received peer performance information. Update
  196. * our running average for the P2P latency.
  197. *
  198. * @param cls closure
  199. * @param address the address
  200. * @param active is this address in active use
  201. * @param bandwidth_out assigned outbound bandwidth for the connection
  202. * @param bandwidth_in assigned inbound bandwidth for the connection
  203. * @param ats performance data for the address (as far as known)
  204. * @param ats_count number of performance records in 'ats'
  205. */
  206. static void
  207. update_latencies (void *cls,
  208. const struct GNUNET_HELLO_Address *address,
  209. int active,
  210. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  211. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  212. const struct GNUNET_ATS_Information *ats,
  213. uint32_t ats_count)
  214. {
  215. unsigned int i;
  216. struct GNUNET_TIME_Relative latency;
  217. if (NULL == address)
  218. {
  219. /* ATS service temporarily disconnected */
  220. return;
  221. }
  222. if (GNUNET_YES != active)
  223. return;
  224. for (i = 0; i < ats_count; i++)
  225. {
  226. if (GNUNET_ATS_QUALITY_NET_DELAY != ntohl (ats[i].type))
  227. continue;
  228. latency.rel_value_us = ntohl (ats[i].value);
  229. GSF_update_peer_latency_ (&address->peer,
  230. latency);
  231. GSF_avg_latency.rel_value_us =
  232. (GSF_avg_latency.rel_value_us * 31 +
  233. GNUNET_MIN (5000, ntohl (ats[i].value))) / 32;
  234. GNUNET_STATISTICS_set (GSF_stats,
  235. gettext_noop
  236. ("# running average P2P latency (ms)"),
  237. GSF_avg_latency.rel_value_us / 1000LL, GNUNET_NO);
  238. break;
  239. }
  240. }
  241. /**
  242. * Handle P2P "PUT" message.
  243. *
  244. * @param cls closure, always NULL
  245. * @param other the other peer involved (sender or receiver, NULL
  246. * for loopback messages where we are both sender and receiver)
  247. * @param message the actual message
  248. * @return GNUNET_OK to keep the connection open,
  249. * GNUNET_SYSERR to close it (signal serious error)
  250. */
  251. static int
  252. handle_p2p_put (void *cls, const struct GNUNET_PeerIdentity *other,
  253. const struct GNUNET_MessageHeader *message)
  254. {
  255. struct GSF_ConnectedPeer *cp;
  256. cp = GSF_peer_get_ (other);
  257. if (NULL == cp)
  258. {
  259. GNUNET_break (0);
  260. return GNUNET_OK;
  261. }
  262. GSF_cover_content_count++;
  263. return GSF_handle_p2p_content_ (cp, message);
  264. }
  265. /**
  266. * We have a new request, consider forwarding it to the given
  267. * peer.
  268. *
  269. * @param cls the 'struct GSF_PendingRequest'
  270. * @param peer identity of the peer
  271. * @param cp handle to the connected peer record
  272. * @param ppd peer performance data
  273. */
  274. static void
  275. consider_request_for_forwarding (void *cls,
  276. const struct GNUNET_PeerIdentity *peer,
  277. struct GSF_ConnectedPeer *cp,
  278. const struct GSF_PeerPerformanceData *ppd)
  279. {
  280. struct GSF_PendingRequest *pr = cls;
  281. if (GNUNET_YES != GSF_pending_request_test_target_ (pr, peer))
  282. {
  283. #if INSANE_STATISTICS
  284. GNUNET_STATISTICS_update (GSF_stats,
  285. gettext_noop ("# Loopback routes suppressed"), 1,
  286. GNUNET_NO);
  287. #endif
  288. return;
  289. }
  290. GSF_plan_add_ (cp, pr);
  291. }
  292. /**
  293. * Function to be called after we're done processing
  294. * replies from the local lookup. If the result status
  295. * code indicates that there may be more replies, plan
  296. * forwarding the request.
  297. *
  298. * @param cls closure (NULL)
  299. * @param pr the pending request we were processing
  300. * @param result final datastore lookup result
  301. */
  302. static void
  303. consider_forwarding (void *cls, struct GSF_PendingRequest *pr,
  304. enum GNUNET_BLOCK_EvaluationResult result)
  305. {
  306. if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
  307. return; /* we're done... */
  308. GSF_iterate_connected_peers_ (&consider_request_for_forwarding, pr);
  309. }
  310. /**
  311. * Handle P2P "GET" request.
  312. *
  313. * @param cls closure, always NULL
  314. * @param other the other peer involved (sender or receiver, NULL
  315. * for loopback messages where we are both sender and receiver)
  316. * @param message the actual message
  317. * @return GNUNET_OK to keep the connection open,
  318. * GNUNET_SYSERR to close it (signal serious error)
  319. */
  320. static int
  321. handle_p2p_get (void *cls, const struct GNUNET_PeerIdentity *other,
  322. const struct GNUNET_MessageHeader *message)
  323. {
  324. struct GSF_PendingRequest *pr;
  325. pr = GSF_handle_p2p_query_ (other, message);
  326. if (NULL == pr)
  327. return GNUNET_SYSERR;
  328. GSF_pending_request_get_data_ (pr)->has_started = GNUNET_YES;
  329. GSF_local_lookup_ (pr, &consider_forwarding, NULL);
  330. return GNUNET_OK;
  331. }
  332. /**
  333. * We're done with the local lookup, now consider
  334. * P2P processing (depending on request options and
  335. * result status). Also signal that we can now
  336. * receive more request information from the client.
  337. *
  338. * @param cls the client doing the request ('struct GNUNET_SERVER_Client')
  339. * @param pr the pending request we were processing
  340. * @param result final datastore lookup result
  341. */
  342. static void
  343. start_p2p_processing (void *cls, struct GSF_PendingRequest *pr,
  344. enum GNUNET_BLOCK_EvaluationResult result)
  345. {
  346. struct GNUNET_SERVER_Client *client = cls;
  347. struct GSF_PendingRequestData *prd;
  348. prd = GSF_pending_request_get_data_ (pr);
  349. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  350. "Finished database lookup for local request `%s' with result %d\n",
  351. GNUNET_h2s (&prd->query), result);
  352. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  353. if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
  354. return; /* we're done, 'pr' was already destroyed... */
  355. if (0 != (GSF_PRO_LOCAL_ONLY & prd->options))
  356. {
  357. GSF_pending_request_cancel_ (pr, GNUNET_YES);
  358. return;
  359. }
  360. if (0 == prd->anonymity_level)
  361. {
  362. switch (prd->type)
  363. {
  364. case GNUNET_BLOCK_TYPE_FS_DBLOCK:
  365. case GNUNET_BLOCK_TYPE_FS_IBLOCK:
  366. /* the above block types MAY be available via 'cadet' */
  367. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  368. "Considering cadet-based download for block\n");
  369. GSF_cadet_lookup_ (pr);
  370. break;
  371. case GNUNET_BLOCK_TYPE_FS_UBLOCK:
  372. /* the above block types are in the DHT */
  373. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  374. "Considering DHT-based search for block\n");
  375. GSF_dht_lookup_ (pr);
  376. break;
  377. default:
  378. GNUNET_break (0);
  379. break;
  380. }
  381. }
  382. consider_forwarding (NULL, pr, result);
  383. }
  384. /**
  385. * Handle START_SEARCH-message (search request from client).
  386. *
  387. * @param cls closure
  388. * @param client identification of the client
  389. * @param message the actual message
  390. */
  391. static void
  392. handle_start_search (void *cls, struct GNUNET_SERVER_Client *client,
  393. const struct GNUNET_MessageHeader *message)
  394. {
  395. struct GSF_PendingRequest *pr;
  396. int ret;
  397. pr = NULL;
  398. ret = GSF_local_client_start_search_handler_ (client, message, &pr);
  399. switch (ret)
  400. {
  401. case GNUNET_SYSERR:
  402. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  403. break;
  404. case GNUNET_NO:
  405. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  406. break;
  407. case GNUNET_YES:
  408. GSF_pending_request_get_data_ (pr)->has_started = GNUNET_YES;
  409. GSF_local_lookup_ (pr, &start_p2p_processing, client);
  410. break;
  411. default:
  412. GNUNET_assert (0);
  413. }
  414. }
  415. /**
  416. * Task run during shutdown.
  417. *
  418. * @param cls unused
  419. * @param tc unused
  420. */
  421. static void
  422. shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  423. {
  424. GSF_cadet_stop_client ();
  425. GSF_cadet_stop_server ();
  426. if (NULL != GSF_core)
  427. {
  428. GNUNET_CORE_disconnect (GSF_core);
  429. GSF_core = NULL;
  430. }
  431. if (NULL != GSF_ats)
  432. {
  433. GNUNET_ATS_performance_done (GSF_ats);
  434. GSF_ats = NULL;
  435. }
  436. GSF_put_done_ ();
  437. GSF_push_done_ ();
  438. GSF_pending_request_done_ ();
  439. GSF_plan_done ();
  440. GSF_connected_peer_done_ ();
  441. GNUNET_DATASTORE_disconnect (GSF_dsh, GNUNET_NO);
  442. GSF_dsh = NULL;
  443. GNUNET_DHT_disconnect (GSF_dht);
  444. GSF_dht = NULL;
  445. GNUNET_BLOCK_context_destroy (GSF_block_ctx);
  446. GSF_block_ctx = NULL;
  447. GNUNET_CONFIGURATION_destroy (block_cfg);
  448. block_cfg = NULL;
  449. GNUNET_STATISTICS_destroy (GSF_stats, GNUNET_NO);
  450. GSF_stats = NULL;
  451. if (GNUNET_SCHEDULER_NO_TASK != cover_age_task)
  452. {
  453. GNUNET_SCHEDULER_cancel (cover_age_task);
  454. cover_age_task = GNUNET_SCHEDULER_NO_TASK;
  455. }
  456. GNUNET_FS_indexing_done ();
  457. GNUNET_LOAD_value_free (datastore_get_load);
  458. datastore_get_load = NULL;
  459. GNUNET_LOAD_value_free (GSF_rt_entry_lifetime);
  460. GSF_rt_entry_lifetime = NULL;
  461. }
  462. /**
  463. * Function called for each pending request whenever a new
  464. * peer connects, giving us a chance to decide about submitting
  465. * the existing request to the new peer.
  466. *
  467. * @param cls the 'struct GSF_ConnectedPeer' of the new peer
  468. * @param key query for the request
  469. * @param pr handle to the pending request
  470. * @return GNUNET_YES to continue to iterate
  471. */
  472. static int
  473. consider_peer_for_forwarding (void *cls, const struct GNUNET_HashCode * key,
  474. struct GSF_PendingRequest *pr)
  475. {
  476. struct GSF_ConnectedPeer *cp = cls;
  477. struct GNUNET_PeerIdentity pid;
  478. GSF_connected_peer_get_identity_ (cp, &pid);
  479. if (GNUNET_YES != GSF_pending_request_test_target_ (pr, &pid))
  480. {
  481. GNUNET_STATISTICS_update (GSF_stats,
  482. gettext_noop ("# Loopback routes suppressed"), 1,
  483. GNUNET_NO);
  484. return GNUNET_YES;
  485. }
  486. GSF_plan_add_ (cp, pr);
  487. return GNUNET_YES;
  488. }
  489. /**
  490. * Function called after the creation of a connected peer record is complete.
  491. *
  492. * @param cls closure (unused)
  493. * @param cp handle to the newly created connected peer record
  494. */
  495. static void
  496. connected_peer_cb (void *cls, struct GSF_ConnectedPeer *cp)
  497. {
  498. if (NULL == cp)
  499. return;
  500. GSF_iterate_pending_requests_ (&consider_peer_for_forwarding, cp);
  501. }
  502. /**
  503. * Method called whenever a given peer connects.
  504. *
  505. * @param cls closure, not used
  506. * @param peer peer identity this notification is about
  507. */
  508. static void
  509. peer_connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
  510. {
  511. if (0 == memcmp (&my_id, peer, sizeof (struct GNUNET_PeerIdentity)))
  512. return;
  513. GSF_peer_connect_handler_ (peer, &connected_peer_cb, NULL);
  514. }
  515. /**
  516. * Function called after GNUNET_CORE_connect has succeeded
  517. * (or failed for good). Note that the private key of the
  518. * peer is intentionally not exposed here; if you need it,
  519. * your process should try to read the private key file
  520. * directly (which should work if you are authorized...).
  521. *
  522. * @param cls closure
  523. * @param my_identity ID of this peer, NULL if we failed
  524. */
  525. static void
  526. peer_init_handler (void *cls,
  527. const struct GNUNET_PeerIdentity *my_identity)
  528. {
  529. my_id = *my_identity;
  530. }
  531. /**
  532. * Process fs requests.
  533. *
  534. * @param server the initialized server
  535. * @param c configuration to use
  536. */
  537. static int
  538. main_init (struct GNUNET_SERVER_Handle *server,
  539. const struct GNUNET_CONFIGURATION_Handle *c)
  540. {
  541. static const struct GNUNET_CORE_MessageHandler no_p2p_handlers[] = {
  542. {NULL, 0, 0}
  543. };
  544. static const struct GNUNET_CORE_MessageHandler p2p_handlers[] = {
  545. {&handle_p2p_get,
  546. GNUNET_MESSAGE_TYPE_FS_GET, 0},
  547. {&handle_p2p_put,
  548. GNUNET_MESSAGE_TYPE_FS_PUT, 0},
  549. {&GSF_handle_p2p_migration_stop_,
  550. GNUNET_MESSAGE_TYPE_FS_MIGRATION_STOP,
  551. sizeof (struct MigrationStopMessage)},
  552. {NULL, 0, 0}
  553. };
  554. static const struct GNUNET_SERVER_MessageHandler handlers[] = {
  555. {&GNUNET_FS_handle_index_start, NULL,
  556. GNUNET_MESSAGE_TYPE_FS_INDEX_START, 0},
  557. {&GNUNET_FS_handle_index_list_get, NULL,
  558. GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET,
  559. sizeof (struct GNUNET_MessageHeader)},
  560. {&GNUNET_FS_handle_unindex, NULL, GNUNET_MESSAGE_TYPE_FS_UNINDEX,
  561. sizeof (struct UnindexMessage)},
  562. {&handle_start_search, NULL, GNUNET_MESSAGE_TYPE_FS_START_SEARCH,
  563. 0},
  564. {NULL, NULL, 0, 0}
  565. };
  566. int anon_p2p_off;
  567. /* this option is really only for testcases that need to disable
  568. _anonymous_ file-sharing for some reason */
  569. anon_p2p_off = (GNUNET_YES ==
  570. GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
  571. "fs",
  572. "DISABLE_ANON_TRANSFER"));
  573. GSF_core =
  574. GNUNET_CORE_connect (GSF_cfg, NULL, &peer_init_handler,
  575. &peer_connect_handler, &GSF_peer_disconnect_handler_,
  576. NULL, GNUNET_NO, NULL, GNUNET_NO,
  577. (GNUNET_YES == anon_p2p_off)
  578. ? no_p2p_handlers
  579. : p2p_handlers);
  580. if (NULL == GSF_core)
  581. {
  582. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  583. _("Failed to connect to `%s' service.\n"), "core");
  584. return GNUNET_SYSERR;
  585. }
  586. GNUNET_SERVER_disconnect_notify (server, &GSF_client_disconnect_handler_,
  587. NULL);
  588. GNUNET_SERVER_add_handlers (server, handlers);
  589. cover_age_task =
  590. GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, &age_cover_counters,
  591. NULL);
  592. datastore_get_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
  593. GSF_cadet_start_server ();
  594. GSF_cadet_start_client ();
  595. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
  596. NULL);
  597. return GNUNET_OK;
  598. }
  599. /**
  600. * Process fs requests.
  601. *
  602. * @param cls closure
  603. * @param server the initialized server
  604. * @param cfg configuration to use
  605. */
  606. static void
  607. run (void *cls, struct GNUNET_SERVER_Handle *server,
  608. const struct GNUNET_CONFIGURATION_Handle *cfg)
  609. {
  610. unsigned long long dqs;
  611. GSF_cfg = cfg;
  612. if (GNUNET_OK !=
  613. GNUNET_CONFIGURATION_get_value_size (GSF_cfg, "fs", "DATASTORE_QUEUE_SIZE",
  614. &dqs))
  615. {
  616. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
  617. "fs", "DATASTORE_QUEUE_SIZE");
  618. dqs = 1024;
  619. }
  620. GSF_datastore_queue_size = (unsigned int) dqs;
  621. GSF_enable_randomized_delays =
  622. GNUNET_CONFIGURATION_get_value_yesno (cfg, "fs", "DELAY");
  623. GSF_dsh = GNUNET_DATASTORE_connect (cfg);
  624. if (NULL == GSF_dsh)
  625. {
  626. GNUNET_SCHEDULER_shutdown ();
  627. return;
  628. }
  629. GSF_rt_entry_lifetime = GNUNET_LOAD_value_init (GNUNET_TIME_UNIT_FOREVER_REL);
  630. GSF_stats = GNUNET_STATISTICS_create ("fs", cfg);
  631. block_cfg = GNUNET_CONFIGURATION_create ();
  632. GSF_block_ctx = GNUNET_BLOCK_context_create (block_cfg);
  633. GNUNET_assert (NULL != GSF_block_ctx);
  634. GSF_dht = GNUNET_DHT_connect (cfg, FS_DHT_HT_SIZE);
  635. GSF_plan_init ();
  636. GSF_pending_request_init_ ();
  637. GSF_connected_peer_init_ ();
  638. GSF_ats = GNUNET_ATS_performance_init (GSF_cfg, &update_latencies, NULL);
  639. GSF_push_init_ ();
  640. GSF_put_init_ ();
  641. if ((GNUNET_OK != GNUNET_FS_indexing_init (cfg, GSF_dsh)) ||
  642. (GNUNET_OK != main_init (server, cfg)))
  643. {
  644. GNUNET_SCHEDULER_shutdown ();
  645. shutdown_task (NULL, NULL);
  646. return;
  647. }
  648. }
  649. /**
  650. * The main function for the fs service.
  651. *
  652. * @param argc number of arguments from the command line
  653. * @param argv command line arguments
  654. * @return 0 ok, 1 on error
  655. */
  656. int
  657. main (int argc, char *const *argv)
  658. {
  659. return (GNUNET_OK ==
  660. GNUNET_SERVICE_run (argc, argv, "fs", GNUNET_SERVICE_OPTION_NONE,
  661. &run, NULL)) ? 0 : 1;
  662. }
  663. /* end of gnunet-service-fs.c */