gnunet-service-fs_cadet_server.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012, 2013, 2017 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file fs/gnunet-service-fs_cadet_server.c
  18. * @brief non-anonymous file-transfer
  19. * @author Christian Grothoff
  20. *
  21. * TODO:
  22. * - PORT is set to old application type, unsure if we should keep
  23. * it that way (fine for now)
  24. */
  25. #include "platform.h"
  26. #include "gnunet_constants.h"
  27. #include "gnunet_util_lib.h"
  28. #include "gnunet_cadet_service.h"
  29. #include "gnunet_protocols.h"
  30. #include "gnunet_applications.h"
  31. #include "gnunet-service-fs.h"
  32. #include "gnunet-service-fs_indexing.h"
  33. #include "gnunet-service-fs_cadet.h"
  34. /**
  35. * After how long do we termiante idle connections?
  36. */
  37. #define IDLE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
  38. /**
  39. * A message in the queue to be written to the cadet.
  40. */
  41. struct WriteQueueItem
  42. {
  43. /**
  44. * Kept in a DLL.
  45. */
  46. struct WriteQueueItem *next;
  47. /**
  48. * Kept in a DLL.
  49. */
  50. struct WriteQueueItem *prev;
  51. /**
  52. * Number of bytes of payload, allocated at the end of this struct.
  53. */
  54. size_t msize;
  55. };
  56. /**
  57. * Information we keep around for each active cadeting client.
  58. */
  59. struct CadetClient
  60. {
  61. /**
  62. * DLL
  63. */
  64. struct CadetClient *next;
  65. /**
  66. * DLL
  67. */
  68. struct CadetClient *prev;
  69. /**
  70. * Channel for communication.
  71. */
  72. struct GNUNET_CADET_Channel *channel;
  73. /**
  74. * Head of write queue.
  75. */
  76. struct WriteQueueItem *wqi_head;
  77. /**
  78. * Tail of write queue.
  79. */
  80. struct WriteQueueItem *wqi_tail;
  81. /**
  82. * Current active request to the datastore, if we have one pending.
  83. */
  84. struct GNUNET_DATASTORE_QueueEntry *qe;
  85. /**
  86. * Task that is scheduled to asynchronously terminate the connection.
  87. */
  88. struct GNUNET_SCHEDULER_Task *terminate_task;
  89. /**
  90. * Task that is scheduled to terminate idle connections.
  91. */
  92. struct GNUNET_SCHEDULER_Task *timeout_task;
  93. /**
  94. * Size of the last write that was initiated.
  95. */
  96. size_t reply_size;
  97. };
  98. /**
  99. * Listen port for incoming requests.
  100. */
  101. static struct GNUNET_CADET_Port *cadet_port;
  102. /**
  103. * Head of DLL of cadet clients.
  104. */
  105. static struct CadetClient *sc_head;
  106. /**
  107. * Tail of DLL of cadet clients.
  108. */
  109. static struct CadetClient *sc_tail;
  110. /**
  111. * Number of active cadet clients in the 'sc_*'-DLL.
  112. */
  113. static unsigned int sc_count;
  114. /**
  115. * Maximum allowed number of cadet clients.
  116. */
  117. static unsigned long long sc_count_max;
  118. /**
  119. * Task run to asynchronously terminate the cadet due to timeout.
  120. *
  121. * @param cls the 'struct CadetClient'
  122. */
  123. static void
  124. timeout_cadet_task (void *cls)
  125. {
  126. struct CadetClient *sc = cls;
  127. struct GNUNET_CADET_Channel *tun;
  128. sc->timeout_task = NULL;
  129. tun = sc->channel;
  130. sc->channel = NULL;
  131. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  132. "Timeout for inactive cadet client %p\n",
  133. sc);
  134. GNUNET_CADET_channel_destroy (tun);
  135. }
  136. /**
  137. * Reset the timeout for the cadet client (due to activity).
  138. *
  139. * @param sc client handle to reset timeout for
  140. */
  141. static void
  142. refresh_timeout_task (struct CadetClient *sc)
  143. {
  144. if (NULL != sc->timeout_task)
  145. GNUNET_SCHEDULER_cancel (sc->timeout_task);
  146. sc->timeout_task = GNUNET_SCHEDULER_add_delayed (IDLE_TIMEOUT,
  147. &timeout_cadet_task,
  148. sc);
  149. }
  150. /**
  151. * Check if we are done with the write queue, and if so tell CADET
  152. * that we are ready to read more.
  153. *
  154. * @param cls where to process the write queue
  155. */
  156. static void
  157. continue_writing (void *cls)
  158. {
  159. struct CadetClient *sc = cls;
  160. struct GNUNET_MQ_Handle *mq;
  161. mq = GNUNET_CADET_get_mq (sc->channel);
  162. if (0 != GNUNET_MQ_get_length (mq))
  163. {
  164. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  165. "Write pending, waiting for it to complete\n");
  166. return;
  167. }
  168. refresh_timeout_task (sc);
  169. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  170. "Finished processing cadet request from client %p, ready to receive the next one\n",
  171. sc);
  172. GNUNET_CADET_receive_done (sc->channel);
  173. }
  174. /**
  175. * Process a datum that was stored in the datastore.
  176. *
  177. * @param cls closure with the `struct CadetClient` which sent the query
  178. * @param key key for the content
  179. * @param size number of bytes in @a data
  180. * @param data content stored
  181. * @param type type of the content
  182. * @param priority priority of the content
  183. * @param anonymity anonymity-level for the content
  184. * @param replication replication-level for the content
  185. * @param expiration expiration time for the content
  186. * @param uid unique identifier for the datum;
  187. * maybe 0 if no unique identifier is available
  188. */
  189. static void
  190. handle_datastore_reply (void *cls,
  191. const struct GNUNET_HashCode *key,
  192. size_t size,
  193. const void *data,
  194. enum GNUNET_BLOCK_Type type,
  195. uint32_t priority,
  196. uint32_t anonymity,
  197. uint32_t replication,
  198. struct GNUNET_TIME_Absolute expiration,
  199. uint64_t uid)
  200. {
  201. struct CadetClient *sc = cls;
  202. size_t msize = size + sizeof(struct CadetReplyMessage);
  203. struct GNUNET_MQ_Envelope *env;
  204. struct CadetReplyMessage *srm;
  205. sc->qe = NULL;
  206. if (NULL == data)
  207. {
  208. /* no result, this should not really happen, as for
  209. non-anonymous routing only peers that HAVE the
  210. answers should be queried; OTOH, this is not a
  211. hard error as we might have had the answer in the
  212. past and the user might have unindexed it. Hence
  213. we log at level "INFO" for now. */
  214. if (NULL == key)
  215. {
  216. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  217. "Have no answer and the query was NULL\n");
  218. }
  219. else
  220. {
  221. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  222. "Have no answer for query `%s'\n",
  223. GNUNET_h2s (key));
  224. }
  225. GNUNET_STATISTICS_update (GSF_stats,
  226. gettext_noop (
  227. "# queries received via CADET not answered"),
  228. 1,
  229. GNUNET_NO);
  230. continue_writing (sc);
  231. return;
  232. }
  233. if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
  234. {
  235. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  236. "Performing on-demand encoding for query %s\n",
  237. GNUNET_h2s (key));
  238. if (GNUNET_OK !=
  239. GNUNET_FS_handle_on_demand_block (key,
  240. size,
  241. data,
  242. type,
  243. priority,
  244. anonymity,
  245. replication,
  246. expiration,
  247. uid,
  248. &handle_datastore_reply,
  249. sc))
  250. {
  251. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  252. "On-demand encoding request failed\n");
  253. continue_writing (sc);
  254. }
  255. return;
  256. }
  257. if (msize > GNUNET_MAX_MESSAGE_SIZE)
  258. {
  259. GNUNET_break (0);
  260. continue_writing (sc);
  261. return;
  262. }
  263. GNUNET_break (GNUNET_BLOCK_TYPE_ANY != type);
  264. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  265. "Starting transmission of %u byte reply of type %d for query `%s' via cadet to %p\n",
  266. (unsigned int) size,
  267. (unsigned int) type,
  268. GNUNET_h2s (key),
  269. sc);
  270. env = GNUNET_MQ_msg_extra (srm,
  271. size,
  272. GNUNET_MESSAGE_TYPE_FS_CADET_REPLY);
  273. srm->type = htonl (type);
  274. srm->expiration = GNUNET_TIME_absolute_hton (expiration);
  275. GNUNET_memcpy (&srm[1],
  276. data,
  277. size);
  278. GNUNET_MQ_notify_sent (env,
  279. &continue_writing,
  280. sc);
  281. GNUNET_STATISTICS_update (GSF_stats,
  282. gettext_noop ("# Blocks transferred via cadet"),
  283. 1,
  284. GNUNET_NO);
  285. GNUNET_MQ_send (GNUNET_CADET_get_mq (sc->channel),
  286. env);
  287. }
  288. /**
  289. * Functions with this signature are called whenever a
  290. * complete query message is received.
  291. *
  292. * @param cls closure with the `struct CadetClient`
  293. * @param sqm the actual message
  294. */
  295. static void
  296. handle_request (void *cls,
  297. const struct CadetQueryMessage *sqm)
  298. {
  299. struct CadetClient *sc = cls;
  300. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  301. "Received query for `%s' via cadet from client %p\n",
  302. GNUNET_h2s (&sqm->query),
  303. sc);
  304. GNUNET_STATISTICS_update (GSF_stats,
  305. gettext_noop ("# queries received via cadet"),
  306. 1,
  307. GNUNET_NO);
  308. refresh_timeout_task (sc);
  309. sc->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
  310. 0 /* next_uid */,
  311. false /* random */,
  312. &sqm->query,
  313. ntohl (sqm->type),
  314. 0 /* priority */,
  315. GSF_datastore_queue_size,
  316. &handle_datastore_reply,
  317. sc);
  318. if (NULL == sc->qe)
  319. {
  320. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  321. "Queueing request with datastore failed (queue full?)\n");
  322. continue_writing (sc);
  323. }
  324. }
  325. /**
  326. * Functions of this type are called upon new cadet connection from other peers.
  327. *
  328. * @param cls the closure from GNUNET_CADET_connect
  329. * @param channel the channel representing the cadet
  330. * @param initiator the identity of the peer who wants to establish a cadet
  331. * with us; NULL on binding error
  332. * @return initial channel context (our `struct CadetClient`)
  333. */
  334. static void *
  335. connect_cb (void *cls,
  336. struct GNUNET_CADET_Channel *channel,
  337. const struct GNUNET_PeerIdentity *initiator)
  338. {
  339. struct CadetClient *sc;
  340. GNUNET_assert (NULL != channel);
  341. if (sc_count >= sc_count_max)
  342. {
  343. GNUNET_STATISTICS_update (GSF_stats,
  344. gettext_noop (
  345. "# cadet client connections rejected"),
  346. 1,
  347. GNUNET_NO);
  348. GNUNET_CADET_channel_destroy (channel);
  349. return NULL;
  350. }
  351. GNUNET_STATISTICS_update (GSF_stats,
  352. gettext_noop ("# cadet connections active"),
  353. 1,
  354. GNUNET_NO);
  355. sc = GNUNET_new (struct CadetClient);
  356. sc->channel = channel;
  357. GNUNET_CONTAINER_DLL_insert (sc_head,
  358. sc_tail,
  359. sc);
  360. sc_count++;
  361. refresh_timeout_task (sc);
  362. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  363. "Accepting inbound cadet connection from `%s' as client %p\n",
  364. GNUNET_i2s (initiator),
  365. sc);
  366. return sc;
  367. }
  368. /**
  369. * Function called by cadet when a client disconnects.
  370. * Cleans up our `struct CadetClient` of that channel.
  371. *
  372. * @param cls our `struct CadetClient`
  373. * @param channel channel of the disconnecting client
  374. * @param channel_ctx
  375. */
  376. static void
  377. disconnect_cb (void *cls,
  378. const struct GNUNET_CADET_Channel *channel)
  379. {
  380. struct CadetClient *sc = cls;
  381. struct WriteQueueItem *wqi;
  382. if (NULL == sc)
  383. return;
  384. sc->channel = NULL;
  385. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  386. "Terminating cadet connection with client %p\n",
  387. sc);
  388. GNUNET_STATISTICS_update (GSF_stats,
  389. gettext_noop ("# cadet connections active"), -1,
  390. GNUNET_NO);
  391. if (NULL != sc->terminate_task)
  392. GNUNET_SCHEDULER_cancel (sc->terminate_task);
  393. if (NULL != sc->timeout_task)
  394. GNUNET_SCHEDULER_cancel (sc->timeout_task);
  395. if (NULL != sc->qe)
  396. GNUNET_DATASTORE_cancel (sc->qe);
  397. while (NULL != (wqi = sc->wqi_head))
  398. {
  399. GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
  400. sc->wqi_tail,
  401. wqi);
  402. GNUNET_free (wqi);
  403. }
  404. GNUNET_CONTAINER_DLL_remove (sc_head,
  405. sc_tail,
  406. sc);
  407. sc_count--;
  408. GNUNET_free (sc);
  409. }
  410. /**
  411. * Function called whenever an MQ-channel's transmission window size changes.
  412. *
  413. * The first callback in an outgoing channel will be with a non-zero value
  414. * and will mean the channel is connected to the destination.
  415. *
  416. * For an incoming channel it will be called immediately after the
  417. * #GNUNET_CADET_ConnectEventHandler, also with a non-zero value.
  418. *
  419. * @param cls Channel closure.
  420. * @param channel Connection to the other end (henceforth invalid).
  421. * @param window_size New window size. If the is more messages than buffer size
  422. * this value will be negative..
  423. */
  424. static void
  425. window_change_cb (void *cls,
  426. const struct GNUNET_CADET_Channel *channel,
  427. int window_size)
  428. {
  429. /* FIXME: could do flow control here... */
  430. }
  431. /**
  432. * Initialize subsystem for non-anonymous file-sharing.
  433. */
  434. void
  435. GSF_cadet_start_server ()
  436. {
  437. struct GNUNET_MQ_MessageHandler handlers[] = {
  438. GNUNET_MQ_hd_fixed_size (request,
  439. GNUNET_MESSAGE_TYPE_FS_CADET_QUERY,
  440. struct CadetQueryMessage,
  441. NULL),
  442. GNUNET_MQ_handler_end ()
  443. };
  444. struct GNUNET_HashCode port;
  445. if (GNUNET_YES !=
  446. GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
  447. "fs",
  448. "MAX_CADET_CLIENTS",
  449. &sc_count_max))
  450. return;
  451. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  452. "Initializing cadet FS server with a limit of %llu connections\n",
  453. sc_count_max);
  454. cadet_map = GNUNET_CONTAINER_multipeermap_create (16, GNUNET_YES);
  455. cadet_handle = GNUNET_CADET_connect (GSF_cfg);
  456. GNUNET_assert (NULL != cadet_handle);
  457. GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_FS_BLOCK_TRANSFER,
  458. strlen (GNUNET_APPLICATION_PORT_FS_BLOCK_TRANSFER),
  459. &port);
  460. cadet_port = GNUNET_CADET_open_port (cadet_handle,
  461. &port,
  462. &connect_cb,
  463. NULL,
  464. &window_change_cb,
  465. &disconnect_cb,
  466. handlers);
  467. }
  468. /**
  469. * Shutdown subsystem for non-anonymous file-sharing.
  470. */
  471. void
  472. GSF_cadet_stop_server ()
  473. {
  474. GNUNET_CONTAINER_multipeermap_iterate (cadet_map,
  475. &GSF_cadet_release_clients,
  476. NULL);
  477. GNUNET_CONTAINER_multipeermap_destroy (cadet_map);
  478. cadet_map = NULL;
  479. if (NULL != cadet_port)
  480. {
  481. GNUNET_CADET_close_port (cadet_port);
  482. cadet_port = NULL;
  483. }
  484. if (NULL != cadet_handle)
  485. {
  486. GNUNET_CADET_disconnect (cadet_handle);
  487. cadet_handle = NULL;
  488. }
  489. GNUNET_assert (NULL == sc_head);
  490. GNUNET_assert (0 == sc_count);
  491. }
  492. /* end of gnunet-service-fs_cadet.c */