gnunet-service-fs_indexing.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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", "INDEXDB", &fn))
  100. {
  101. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  102. "fs",
  103. "INDEXDB");
  104. return;
  105. }
  106. wh = GNUNET_BIO_write_open_file (fn);
  107. if (NULL == wh)
  108. {
  109. GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  110. _ ("Could not open `%s'.\n"),
  111. fn);
  112. GNUNET_free (fn);
  113. return;
  114. }
  115. for (pos = indexed_files_head; NULL != pos; pos = pos->next)
  116. if ((GNUNET_OK != GNUNET_BIO_write (wh,
  117. "fs-indexing-file-id",
  118. &pos->file_id,
  119. sizeof(struct GNUNET_HashCode))) ||
  120. (GNUNET_OK != GNUNET_BIO_write_string (wh,
  121. "fs-indexing-filename",
  122. pos->filename)))
  123. break;
  124. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  125. {
  126. GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  127. _ ("Error writing `%s'.\n"),
  128. fn);
  129. GNUNET_free (fn);
  130. return;
  131. }
  132. GNUNET_free (fn);
  133. }
  134. /**
  135. * Read index information from disk.
  136. */
  137. static void
  138. read_index_list ()
  139. {
  140. struct GNUNET_BIO_ReadHandle *rh;
  141. char *fn;
  142. struct IndexInfo *pos;
  143. char *fname;
  144. struct GNUNET_HashCode hc;
  145. size_t slen;
  146. char *emsg;
  147. if (GNUNET_OK !=
  148. GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
  149. {
  150. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  151. "fs",
  152. "INDEXDB");
  153. return;
  154. }
  155. if (GNUNET_NO == GNUNET_DISK_file_test (fn))
  156. {
  157. /* no index info yet */
  158. GNUNET_free (fn);
  159. return;
  160. }
  161. rh = GNUNET_BIO_read_open_file (fn);
  162. if (NULL == rh)
  163. {
  164. GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  165. _ ("Could not open `%s'.\n"),
  166. fn);
  167. GNUNET_free (fn);
  168. return;
  169. }
  170. while (
  171. (GNUNET_OK == GNUNET_BIO_read (rh,
  172. "Hash of indexed file",
  173. &hc,
  174. sizeof(struct GNUNET_HashCode))) &&
  175. (GNUNET_OK ==
  176. GNUNET_BIO_read_string (rh, "Name of indexed file", &fname, 1024 * 16)) &&
  177. (fname != NULL))
  178. {
  179. slen = strlen (fname) + 1;
  180. pos = GNUNET_malloc (sizeof(struct IndexInfo) + slen);
  181. pos->file_id = hc;
  182. pos->filename = (const char *) &pos[1];
  183. GNUNET_memcpy (&pos[1], fname, slen);
  184. if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put (
  185. ifm,
  186. &pos->file_id,
  187. pos,
  188. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
  189. {
  190. GNUNET_free (pos);
  191. }
  192. else
  193. {
  194. GNUNET_CONTAINER_DLL_insert (indexed_files_head, indexed_files_tail, pos);
  195. }
  196. GNUNET_free (fname);
  197. }
  198. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  199. GNUNET_free (emsg);
  200. GNUNET_free (fn);
  201. }
  202. /**
  203. * Continuation called from datastore's remove
  204. * function.
  205. *
  206. * @param cls unused
  207. * @param success did the deletion work?
  208. * @param min_expiration minimum expiration time required for content to be stored
  209. * @param msg error message
  210. */
  211. static void
  212. remove_cont (void *cls,
  213. int success,
  214. struct GNUNET_TIME_Absolute min_expiration,
  215. const char *msg)
  216. {
  217. if (GNUNET_OK != success)
  218. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  219. _ ("Failed to delete bogus block: %s\n"),
  220. msg);
  221. }
  222. /**
  223. * We've received an on-demand encoded block from the datastore.
  224. * Attempt to do on-demand encoding and (if successful), call the
  225. * continuation with the resulting block. On error, clean up and ask
  226. * the datastore for more results.
  227. *
  228. * @param key key for the content
  229. * @param size number of bytes in data
  230. * @param data content stored
  231. * @param type type of the content
  232. * @param priority priority of the content
  233. * @param anonymity anonymity-level for the content
  234. * @param replication replication-level for the content
  235. * @param expiration expiration time for the content
  236. * @param uid unique identifier for the datum;
  237. * maybe 0 if no unique identifier is available
  238. * @param cont function to call with the actual block (at most once, on success)
  239. * @param cont_cls closure for cont
  240. * @return GNUNET_OK on success
  241. */
  242. int
  243. GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode *key,
  244. uint32_t size,
  245. const void *data,
  246. enum GNUNET_BLOCK_Type type,
  247. uint32_t priority,
  248. uint32_t anonymity,
  249. uint32_t replication,
  250. struct GNUNET_TIME_Absolute expiration,
  251. uint64_t uid,
  252. GNUNET_DATASTORE_DatumProcessor cont,
  253. void *cont_cls)
  254. {
  255. const struct OnDemandBlock *odb;
  256. struct GNUNET_HashCode nkey;
  257. struct GNUNET_CRYPTO_SymmetricSessionKey skey;
  258. struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
  259. struct GNUNET_HashCode query;
  260. ssize_t nsize;
  261. char ndata[DBLOCK_SIZE];
  262. char edata[DBLOCK_SIZE];
  263. const char *fn;
  264. struct GNUNET_DISK_FileHandle *fh;
  265. uint64_t off;
  266. struct IndexInfo *ii;
  267. if (size != sizeof(struct OnDemandBlock))
  268. {
  269. GNUNET_break (0);
  270. GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
  271. return GNUNET_SYSERR;
  272. }
  273. odb = (const struct OnDemandBlock *) data;
  274. off = GNUNET_ntohll (odb->offset);
  275. ii = GNUNET_CONTAINER_multihashmap_get (ifm, &odb->file_id);
  276. if (NULL == ii)
  277. {
  278. GNUNET_break (0);
  279. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  280. "Failed to find index %s\n",
  281. GNUNET_h2s (&odb->file_id));
  282. return GNUNET_SYSERR;
  283. }
  284. fn = ii->filename;
  285. if ((NULL == fn) || (0 != access (fn, R_OK)))
  286. {
  287. GNUNET_STATISTICS_update (
  288. GSF_stats,
  289. gettext_noop ("# index blocks removed: original file inaccessible"),
  290. 1,
  291. GNUNET_YES);
  292. GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
  293. return GNUNET_SYSERR;
  294. }
  295. if ((NULL == (fh = GNUNET_DISK_file_open (fn,
  296. GNUNET_DISK_OPEN_READ,
  297. GNUNET_DISK_PERM_NONE))) ||
  298. (off != GNUNET_DISK_file_seek (fh, off, GNUNET_DISK_SEEK_SET)) ||
  299. (-1 == (nsize = GNUNET_DISK_file_read (fh, ndata, sizeof(ndata)))))
  300. {
  301. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  302. _ (
  303. "Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
  304. GNUNET_h2s (&odb->file_id),
  305. fn,
  306. (unsigned long long) off,
  307. (fn == NULL) ? _ ("not indexed") : strerror (errno));
  308. if (fh != NULL)
  309. GNUNET_DISK_file_close (fh);
  310. GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
  311. return GNUNET_SYSERR;
  312. }
  313. GNUNET_DISK_file_close (fh);
  314. GNUNET_CRYPTO_hash (ndata, nsize, &nkey);
  315. GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
  316. GNUNET_CRYPTO_symmetric_encrypt (ndata, nsize, &skey, &iv, edata);
  317. GNUNET_CRYPTO_hash (edata, nsize, &query);
  318. if (0 != memcmp (&query, key, sizeof(struct GNUNET_HashCode)))
  319. {
  320. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  321. _ ("Indexed file `%s' changed at offset %llu\n"),
  322. fn,
  323. (unsigned long long) off);
  324. GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
  325. return GNUNET_SYSERR;
  326. }
  327. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  328. "On-demand encoded block for query `%s'\n",
  329. GNUNET_h2s (key));
  330. cont (cont_cls,
  331. key,
  332. nsize,
  333. edata,
  334. GNUNET_BLOCK_TYPE_FS_DBLOCK,
  335. priority,
  336. anonymity,
  337. replication,
  338. expiration,
  339. uid);
  340. return GNUNET_OK;
  341. }
  342. /**
  343. * Transmit information about indexed files to @a mq.
  344. *
  345. * @param mq message queue to send information to
  346. */
  347. void
  348. GNUNET_FS_indexing_send_list (struct GNUNET_MQ_Handle *mq)
  349. {
  350. struct GNUNET_MQ_Envelope *env;
  351. struct IndexInfoMessage *iim;
  352. struct GNUNET_MessageHeader *iem;
  353. size_t slen;
  354. const char *fn;
  355. struct IndexInfo *pos;
  356. for (pos = indexed_files_head; NULL != pos; pos = pos->next)
  357. {
  358. fn = pos->filename;
  359. slen = strlen (fn) + 1;
  360. if (slen + sizeof(struct IndexInfoMessage) >= GNUNET_MAX_MESSAGE_SIZE)
  361. {
  362. GNUNET_break (0);
  363. break;
  364. }
  365. env =
  366. GNUNET_MQ_msg_extra (iim, slen, GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
  367. iim->reserved = 0;
  368. iim->file_id = pos->file_id;
  369. GNUNET_memcpy (&iim[1], fn, slen);
  370. GNUNET_MQ_send (mq, env);
  371. }
  372. env = GNUNET_MQ_msg (iem, GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
  373. GNUNET_MQ_send (mq, env);
  374. }
  375. /**
  376. * Remove a file from the index.
  377. *
  378. * @param fid identifier of the file to remove
  379. * @return #GNUNET_YES if the @a fid was found
  380. */
  381. int
  382. GNUNET_FS_indexing_do_unindex (const struct GNUNET_HashCode *fid)
  383. {
  384. struct IndexInfo *pos;
  385. for (pos = indexed_files_head; NULL != pos; pos = pos->next)
  386. {
  387. if (0 == memcmp (&pos->file_id, fid, sizeof(struct GNUNET_HashCode)))
  388. {
  389. GNUNET_CONTAINER_DLL_remove (indexed_files_head, indexed_files_tail, pos);
  390. GNUNET_break (
  391. GNUNET_OK ==
  392. GNUNET_CONTAINER_multihashmap_remove (ifm, &pos->file_id, pos));
  393. GNUNET_free (pos);
  394. write_index_list ();
  395. return GNUNET_YES;
  396. }
  397. }
  398. return GNUNET_NO;
  399. }
  400. /**
  401. * Add the given file to the list of indexed files.
  402. *
  403. * @param filename name of the file
  404. * @param file_id hash identifier for @a filename
  405. */
  406. void
  407. GNUNET_FS_add_to_index (const char *filename,
  408. const struct GNUNET_HashCode *file_id)
  409. {
  410. struct IndexInfo *ii;
  411. size_t slen;
  412. ii = GNUNET_CONTAINER_multihashmap_get (ifm, file_id);
  413. if (NULL != ii)
  414. {
  415. GNUNET_log (
  416. GNUNET_ERROR_TYPE_INFO,
  417. _ (
  418. "Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
  419. filename,
  420. ii->filename);
  421. return;
  422. }
  423. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  424. "Adding file %s to index as %s\n",
  425. filename,
  426. GNUNET_h2s (file_id));
  427. slen = strlen (filename) + 1;
  428. ii = GNUNET_malloc (sizeof(struct IndexInfo) + slen);
  429. ii->file_id = *file_id;
  430. ii->filename = (const char *) &ii[1];
  431. GNUNET_memcpy (&ii[1], filename, slen);
  432. GNUNET_CONTAINER_DLL_insert (indexed_files_head, indexed_files_tail, ii);
  433. GNUNET_assert (GNUNET_OK ==
  434. GNUNET_CONTAINER_multihashmap_put (
  435. ifm,
  436. &ii->file_id,
  437. ii,
  438. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  439. write_index_list ();
  440. }
  441. /**
  442. * Shutdown the module.
  443. */
  444. void
  445. GNUNET_FS_indexing_done ()
  446. {
  447. struct IndexInfo *pos;
  448. while (NULL != (pos = indexed_files_head))
  449. {
  450. GNUNET_CONTAINER_DLL_remove (indexed_files_head, indexed_files_tail, pos);
  451. if (pos->fhc != NULL)
  452. GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
  453. GNUNET_break (
  454. GNUNET_OK ==
  455. GNUNET_CONTAINER_multihashmap_remove (ifm, &pos->file_id, pos));
  456. GNUNET_free (pos);
  457. }
  458. GNUNET_CONTAINER_multihashmap_destroy (ifm);
  459. ifm = NULL;
  460. cfg = NULL;
  461. }
  462. /**
  463. * Initialize the indexing submodule.
  464. *
  465. * @param c configuration to use
  466. * @param d datastore to use
  467. */
  468. int
  469. GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
  470. struct GNUNET_DATASTORE_Handle *d)
  471. {
  472. cfg = c;
  473. dsh = d;
  474. ifm = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_YES);
  475. read_index_list ();
  476. return GNUNET_OK;
  477. }
  478. /* end of gnunet-service-fs_indexing.c */