gnunet-service-fs_indexing.c 14 KB

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