gnunet-service-fs_indexing.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010 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_indexing.c
  18. * @brief program that provides indexing functions of the file-sharing service
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include <float.h>
  23. #include "gnunet_core_service.h"
  24. #include "gnunet_datastore_service.h"
  25. #include "gnunet_peer_lib.h"
  26. #include "gnunet_protocols.h"
  27. #include "gnunet_signatures.h"
  28. #include "gnunet_util_lib.h"
  29. #include "gnunet-service-fs.h"
  30. #include "gnunet-service-fs_indexing.h"
  31. #include "fs.h"
  32. /**
  33. * In-memory information about indexed files (also available
  34. * on-disk).
  35. */
  36. struct IndexInfo
  37. {
  38. /**
  39. * This is a doubly linked list.
  40. */
  41. struct IndexInfo *next;
  42. /**
  43. * This is a doubly linked list.
  44. */
  45. struct IndexInfo *prev;
  46. /**
  47. * Name of the indexed file. Memory allocated
  48. * at the end of this struct (do not free).
  49. */
  50. const char *filename;
  51. /**
  52. * Context for transmitting confirmation to client,
  53. * NULL if we've done this already.
  54. */
  55. struct GNUNET_SERVER_TransmitContext *tc;
  56. /**
  57. * Context for hashing of the file.
  58. */
  59. struct GNUNET_CRYPTO_FileHashContext *fhc;
  60. /**
  61. * Hash of the contents of the file.
  62. */
  63. struct GNUNET_HashCode file_id;
  64. };
  65. /**
  66. * Head of linked list of indexed files.
  67. * FIXME: we don't need both a DLL and a hashmap here!
  68. */
  69. static struct IndexInfo *indexed_files_head;
  70. /**
  71. * Tail of linked list of indexed files.
  72. */
  73. static struct IndexInfo *indexed_files_tail;
  74. /**
  75. * Maps hash over content of indexed files to the respective 'struct IndexInfo'.
  76. * The filenames are pointers into the indexed_files linked list and
  77. * do not need to be freed.
  78. */
  79. static struct GNUNET_CONTAINER_MultiHashMap *ifm;
  80. /**
  81. * Our configuration.
  82. */
  83. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  84. /**
  85. * Datastore handle. Created and destroyed by code in
  86. * gnunet-service-fs (this is an alias).
  87. */
  88. static struct GNUNET_DATASTORE_Handle *dsh;
  89. /**
  90. * Write the current index information list to disk.
  91. */
  92. static void
  93. write_index_list ()
  94. {
  95. struct GNUNET_BIO_WriteHandle *wh;
  96. char *fn;
  97. struct IndexInfo *pos;
  98. if (GNUNET_OK !=
  99. GNUNET_CONFIGURATION_get_value_filename (cfg, "FS",
  100. "INDEXDB",
  101. &fn))
  102. {
  103. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  104. "fs",
  105. "INDEXDB");
  106. return;
  107. }
  108. wh = GNUNET_BIO_write_open (fn);
  109. if (NULL == wh)
  110. {
  111. GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  112. _("Could not open `%s'.\n"),
  113. fn);
  114. GNUNET_free (fn);
  115. return;
  116. }
  117. for (pos = indexed_files_head; NULL != pos; pos = pos->next)
  118. if ((GNUNET_OK !=
  119. GNUNET_BIO_write (wh,
  120. &pos->file_id,
  121. sizeof (struct GNUNET_HashCode))) ||
  122. (GNUNET_OK !=
  123. GNUNET_BIO_write_string (wh,
  124. pos->filename)))
  125. break;
  126. if (GNUNET_OK != GNUNET_BIO_write_close (wh))
  127. {
  128. GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  129. _("Error writing `%s'.\n"),
  130. fn);
  131. GNUNET_free (fn);
  132. return;
  133. }
  134. GNUNET_free (fn);
  135. }
  136. /**
  137. * Read index information from disk.
  138. */
  139. static void
  140. read_index_list ()
  141. {
  142. struct GNUNET_BIO_ReadHandle *rh;
  143. char *fn;
  144. struct IndexInfo *pos;
  145. char *fname;
  146. struct GNUNET_HashCode hc;
  147. size_t slen;
  148. char *emsg;
  149. if (GNUNET_OK !=
  150. GNUNET_CONFIGURATION_get_value_filename (cfg,
  151. "FS",
  152. "INDEXDB",
  153. &fn))
  154. {
  155. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  156. "fs",
  157. "INDEXDB");
  158. return;
  159. }
  160. if (GNUNET_NO == GNUNET_DISK_file_test (fn))
  161. {
  162. /* no index info yet */
  163. GNUNET_free (fn);
  164. return;
  165. }
  166. rh = GNUNET_BIO_read_open (fn);
  167. if (NULL == rh)
  168. {
  169. GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  170. _("Could not open `%s'.\n"),
  171. fn);
  172. GNUNET_free (fn);
  173. return;
  174. }
  175. while ( (GNUNET_OK ==
  176. GNUNET_BIO_read (rh,
  177. "Hash of indexed file",
  178. &hc,
  179. sizeof (struct GNUNET_HashCode))) &&
  180. (GNUNET_OK ==
  181. GNUNET_BIO_read_string (rh,
  182. "Name of indexed file",
  183. &fname,
  184. 1024 * 16)) &&
  185. (fname != NULL) )
  186. {
  187. slen = strlen (fname) + 1;
  188. pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
  189. pos->file_id = hc;
  190. pos->filename = (const char *) &pos[1];
  191. GNUNET_memcpy (&pos[1], fname, slen);
  192. if (GNUNET_SYSERR ==
  193. GNUNET_CONTAINER_multihashmap_put (ifm, &pos->file_id, pos,
  194. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
  195. {
  196. GNUNET_free (pos);
  197. }
  198. else
  199. {
  200. GNUNET_CONTAINER_DLL_insert (indexed_files_head,
  201. indexed_files_tail,
  202. pos);
  203. }
  204. GNUNET_free (fname);
  205. }
  206. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  207. GNUNET_free (emsg);
  208. GNUNET_free (fn);
  209. }
  210. /**
  211. * Continuation called from datastore's remove
  212. * function.
  213. *
  214. * @param cls unused
  215. * @param success did the deletion work?
  216. * @param min_expiration minimum expiration time required for content to be stored
  217. * @param msg error message
  218. */
  219. static void
  220. remove_cont (void *cls, int success,
  221. struct GNUNET_TIME_Absolute min_expiration,
  222. const char *msg)
  223. {
  224. if (GNUNET_OK != success)
  225. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  226. _("Failed to delete bogus block: %s\n"), msg);
  227. }
  228. /**
  229. * We've received an on-demand encoded block from the datastore.
  230. * Attempt to do on-demand encoding and (if successful), call the
  231. * continuation with the resulting block. On error, clean up and ask
  232. * the datastore for more results.
  233. *
  234. * @param key key for the content
  235. * @param size number of bytes in data
  236. * @param data content stored
  237. * @param type type of the content
  238. * @param priority priority of the content
  239. * @param anonymity anonymity-level for the content
  240. * @param replication replication-level for the content
  241. * @param expiration expiration time for the content
  242. * @param uid unique identifier for the datum;
  243. * maybe 0 if no unique identifier is available
  244. * @param cont function to call with the actual block (at most once, on success)
  245. * @param cont_cls closure for cont
  246. * @return GNUNET_OK on success
  247. */
  248. int
  249. GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode * key,
  250. uint32_t size,
  251. const void *data,
  252. enum GNUNET_BLOCK_Type type,
  253. uint32_t priority,
  254. uint32_t anonymity,
  255. uint32_t replication,
  256. struct GNUNET_TIME_Absolute expiration,
  257. uint64_t uid,
  258. GNUNET_DATASTORE_DatumProcessor cont,
  259. void *cont_cls)
  260. {
  261. const struct OnDemandBlock *odb;
  262. struct GNUNET_HashCode nkey;
  263. struct GNUNET_CRYPTO_SymmetricSessionKey skey;
  264. struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
  265. struct GNUNET_HashCode query;
  266. ssize_t nsize;
  267. char ndata[DBLOCK_SIZE];
  268. char edata[DBLOCK_SIZE];
  269. const char *fn;
  270. struct GNUNET_DISK_FileHandle *fh;
  271. uint64_t off;
  272. struct IndexInfo *ii;
  273. if (size != sizeof (struct OnDemandBlock))
  274. {
  275. GNUNET_break (0);
  276. GNUNET_DATASTORE_remove (dsh,
  277. key,
  278. size,
  279. data,
  280. -1,
  281. -1,
  282. &remove_cont, NULL);
  283. return GNUNET_SYSERR;
  284. }
  285. odb = (const struct OnDemandBlock *) data;
  286. off = GNUNET_ntohll (odb->offset);
  287. ii = GNUNET_CONTAINER_multihashmap_get (ifm,
  288. &odb->file_id);
  289. if (NULL == ii)
  290. {
  291. GNUNET_break (0);
  292. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  293. "Failed to find index %s\n",
  294. GNUNET_h2s (&odb->file_id));
  295. return GNUNET_SYSERR;
  296. }
  297. fn = ii->filename;
  298. if ((NULL == fn) || (0 != ACCESS (fn, R_OK)))
  299. {
  300. GNUNET_STATISTICS_update (GSF_stats,
  301. gettext_noop ("# index blocks removed: original file inaccessible"),
  302. 1,
  303. GNUNET_YES);
  304. GNUNET_DATASTORE_remove (dsh,
  305. key,
  306. size,
  307. data,
  308. -1,
  309. -1,
  310. &remove_cont,
  311. NULL);
  312. return GNUNET_SYSERR;
  313. }
  314. if ( (NULL ==
  315. (fh =
  316. GNUNET_DISK_file_open (fn,
  317. GNUNET_DISK_OPEN_READ,
  318. GNUNET_DISK_PERM_NONE))) ||
  319. (off != GNUNET_DISK_file_seek (fh,
  320. off,
  321. GNUNET_DISK_SEEK_SET)) ||
  322. (-1 == (nsize = GNUNET_DISK_file_read (fh,
  323. ndata,
  324. sizeof (ndata)))) )
  325. {
  326. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  327. _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
  328. GNUNET_h2s (&odb->file_id),
  329. fn,
  330. (unsigned long long) off,
  331. (fn == NULL) ? _("not indexed") : STRERROR (errno));
  332. if (fh != NULL)
  333. GNUNET_DISK_file_close (fh);
  334. GNUNET_DATASTORE_remove (dsh,
  335. key,
  336. size,
  337. data,
  338. -1,
  339. -1,
  340. &remove_cont,
  341. NULL);
  342. return GNUNET_SYSERR;
  343. }
  344. GNUNET_DISK_file_close (fh);
  345. GNUNET_CRYPTO_hash (ndata,
  346. nsize,
  347. &nkey);
  348. GNUNET_CRYPTO_hash_to_aes_key (&nkey,
  349. &skey,
  350. &iv);
  351. GNUNET_CRYPTO_symmetric_encrypt (ndata,
  352. nsize,
  353. &skey,
  354. &iv,
  355. edata);
  356. GNUNET_CRYPTO_hash (edata,
  357. nsize,
  358. &query);
  359. if (0 != memcmp (&query,
  360. key,
  361. sizeof (struct GNUNET_HashCode)))
  362. {
  363. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  364. _("Indexed file `%s' changed at offset %llu\n"),
  365. fn,
  366. (unsigned long long) off);
  367. GNUNET_DATASTORE_remove (dsh,
  368. key,
  369. size,
  370. data,
  371. -1,
  372. -1,
  373. &remove_cont,
  374. NULL);
  375. return GNUNET_SYSERR;
  376. }
  377. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  378. "On-demand encoded block for query `%s'\n",
  379. GNUNET_h2s (key));
  380. cont (cont_cls,
  381. key,
  382. nsize,
  383. edata,
  384. GNUNET_BLOCK_TYPE_FS_DBLOCK,
  385. priority,
  386. anonymity,
  387. replication,
  388. expiration,
  389. uid);
  390. return GNUNET_OK;
  391. }
  392. /**
  393. * Transmit information about indexed files to @a mq.
  394. *
  395. * @param mq message queue to send information to
  396. */
  397. void
  398. GNUNET_FS_indexing_send_list (struct GNUNET_MQ_Handle *mq)
  399. {
  400. struct GNUNET_MQ_Envelope *env;
  401. struct IndexInfoMessage *iim;
  402. struct GNUNET_MessageHeader *iem;
  403. size_t slen;
  404. const char *fn;
  405. struct IndexInfo *pos;
  406. for (pos = indexed_files_head; NULL != pos; pos = pos->next)
  407. {
  408. fn = pos->filename;
  409. slen = strlen (fn) + 1;
  410. if (slen + sizeof (struct IndexInfoMessage) >=
  411. GNUNET_MAX_MESSAGE_SIZE)
  412. {
  413. GNUNET_break (0);
  414. break;
  415. }
  416. env = GNUNET_MQ_msg_extra (iim,
  417. slen,
  418. GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
  419. iim->reserved = 0;
  420. iim->file_id = pos->file_id;
  421. GNUNET_memcpy (&iim[1],
  422. fn,
  423. slen);
  424. GNUNET_MQ_send (mq,
  425. env);
  426. }
  427. env = GNUNET_MQ_msg (iem,
  428. GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
  429. GNUNET_MQ_send (mq,
  430. env);
  431. }
  432. /**
  433. * Remove a file from the index.
  434. *
  435. * @param fid identifier of the file to remove
  436. * @return #GNUNET_YES if the @a fid was found
  437. */
  438. int
  439. GNUNET_FS_indexing_do_unindex (const struct GNUNET_HashCode *fid)
  440. {
  441. struct IndexInfo *pos;
  442. for (pos = indexed_files_head; NULL != pos; pos = pos->next)
  443. {
  444. if (0 == memcmp (&pos->file_id,
  445. fid,
  446. sizeof (struct GNUNET_HashCode)))
  447. {
  448. GNUNET_CONTAINER_DLL_remove (indexed_files_head,
  449. indexed_files_tail,
  450. pos);
  451. GNUNET_break (GNUNET_OK ==
  452. GNUNET_CONTAINER_multihashmap_remove (ifm,
  453. &pos->file_id,
  454. pos));
  455. GNUNET_free (pos);
  456. write_index_list ();
  457. return GNUNET_YES;
  458. }
  459. }
  460. return GNUNET_NO;
  461. }
  462. /**
  463. * Add the given file to the list of indexed files.
  464. *
  465. * @param filename name of the file
  466. * @param file_id hash identifier for @a filename
  467. */
  468. void
  469. GNUNET_FS_add_to_index (const char *filename,
  470. const struct GNUNET_HashCode *file_id)
  471. {
  472. struct IndexInfo *ii;
  473. size_t slen;
  474. ii = GNUNET_CONTAINER_multihashmap_get (ifm,
  475. file_id);
  476. if (NULL != ii)
  477. {
  478. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  479. _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
  480. filename,
  481. ii->filename);
  482. return;
  483. }
  484. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  485. "Adding file %s to index as %s\n",
  486. filename,
  487. GNUNET_h2s (file_id));
  488. slen = strlen (filename) + 1;
  489. ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
  490. ii->file_id = *file_id;
  491. ii->filename = (const char *) &ii[1];
  492. GNUNET_memcpy (&ii[1],
  493. filename,
  494. slen);
  495. GNUNET_CONTAINER_DLL_insert (indexed_files_head,
  496. indexed_files_tail,
  497. ii);
  498. GNUNET_assert (GNUNET_OK ==
  499. GNUNET_CONTAINER_multihashmap_put (ifm,
  500. &ii->file_id,
  501. ii,
  502. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  503. write_index_list ();
  504. }
  505. /**
  506. * Shutdown the module.
  507. */
  508. void
  509. GNUNET_FS_indexing_done ()
  510. {
  511. struct IndexInfo *pos;
  512. while (NULL != (pos = indexed_files_head))
  513. {
  514. GNUNET_CONTAINER_DLL_remove (indexed_files_head,
  515. indexed_files_tail,
  516. pos);
  517. if (pos->fhc != NULL)
  518. GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
  519. GNUNET_break (GNUNET_OK ==
  520. GNUNET_CONTAINER_multihashmap_remove (ifm,
  521. &pos->file_id,
  522. pos));
  523. GNUNET_free (pos);
  524. }
  525. GNUNET_CONTAINER_multihashmap_destroy (ifm);
  526. ifm = NULL;
  527. cfg = NULL;
  528. }
  529. /**
  530. * Initialize the indexing submodule.
  531. *
  532. * @param c configuration to use
  533. * @param d datastore to use
  534. */
  535. int
  536. GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
  537. struct GNUNET_DATASTORE_Handle *d)
  538. {
  539. cfg = c;
  540. dsh = d;
  541. ifm = GNUNET_CONTAINER_multihashmap_create (128,
  542. GNUNET_YES);
  543. read_index_list ();
  544. return GNUNET_OK;
  545. }
  546. /* end of gnunet-service-fs_indexing.c */