2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/gnunet-service-fs_indexing.c
23 * @brief program that provides indexing functions of the file-sharing service
24 * @author Christian Grothoff
27 * - consider doing GNUNET_CRYPTO_hash_file_cancel on active indexing
28 * jobs during shutdown (currently, shutdown will only happen after
29 * all of those are done, not sure if this is good or bad)
33 #include "gnunet_core_service.h"
34 #include "gnunet_datastore_service.h"
35 #include "gnunet_peer_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_signatures.h"
38 #include "gnunet_util_lib.h"
39 #include "gnunet-service-fs_indexing.h"
42 #define DEBUG_FS GNUNET_NO
45 * In-memory information about indexed files (also available
52 * This is a linked list.
54 struct IndexInfo *next;
57 * Name of the indexed file. Memory allocated
58 * at the end of this struct (do not free).
63 * Context for transmitting confirmation to client,
64 * NULL if we've done this already.
66 struct GNUNET_SERVER_TransmitContext *tc;
69 * Context for hashing of the file.
71 struct GNUNET_CRYPTO_FileHashContext *fhc;
74 * Hash of the contents of the file.
76 GNUNET_HashCode file_id;
82 * Linked list of indexed files.
84 static struct IndexInfo *indexed_files;
87 * Maps hash over content of indexed files to the respective filename.
88 * The filenames are pointers into the indexed_files linked list and
89 * do not need to be freed.
91 static struct GNUNET_CONTAINER_MultiHashMap *ifm;
96 static const struct GNUNET_CONFIGURATION_Handle *cfg;
99 * Datastore handle. Created and destroyed by code in
100 * gnunet-service-fs (this is an alias).
102 static struct GNUNET_DATASTORE_Handle *dsh;
106 * Write the current index information list to disk.
111 struct GNUNET_BIO_WriteHandle *wh;
113 struct IndexInfo *pos;
116 GNUNET_CONFIGURATION_get_value_filename (cfg,
121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
122 _("Configuration option `%s' in section `%s' missing.\n"),
127 wh = GNUNET_BIO_write_open (fn);
130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
131 _("Could not open `%s'.\n"),
140 GNUNET_BIO_write (wh,
142 sizeof (GNUNET_HashCode))) ||
144 GNUNET_BIO_write_string (wh,
150 GNUNET_BIO_write_close (wh))
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
153 _("Error writing `%s'.\n"),
163 * Read index information from disk.
168 struct GNUNET_BIO_ReadHandle *rh;
170 struct IndexInfo *pos;
177 GNUNET_CONFIGURATION_get_value_filename (cfg,
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
183 _("Configuration option `%s' in section `%s' missing.\n"),
188 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
190 /* no index info yet */
194 rh = GNUNET_BIO_read_open (fn);
197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
198 _("Could not open `%s'.\n"),
203 while ( (GNUNET_OK ==
205 "Hash of indexed file",
207 sizeof (GNUNET_HashCode))) &&
209 GNUNET_BIO_read_string (rh,
210 "Name of indexed file",
215 slen = strlen (fname) + 1;
216 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
218 pos->filename = (const char *) &pos[1];
219 memcpy (&pos[1], fname, slen);
221 GNUNET_CONTAINER_multihashmap_put (ifm,
223 (void*) pos->filename,
224 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
230 pos->next = indexed_files;
236 GNUNET_BIO_read_close (rh, &emsg))
243 * We've validated the hash of the file we're about to index. Signal
244 * success to the client and update our internal data structures.
246 * @param ii the index info entry for the request
249 signal_index_ok (struct IndexInfo *ii)
252 GNUNET_CONTAINER_multihashmap_put (ifm,
254 (void*) ii->filename,
255 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
257 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
258 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
260 (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
262 GNUNET_SERVER_transmit_context_append_data (ii->tc,
264 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
265 GNUNET_SERVER_transmit_context_run (ii->tc,
266 GNUNET_TIME_UNIT_MINUTES);
270 ii->next = indexed_files;
273 GNUNET_SERVER_transmit_context_append_data (ii->tc,
275 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
276 GNUNET_SERVER_transmit_context_run (ii->tc,
277 GNUNET_TIME_UNIT_MINUTES);
283 * Function called once the hash computation over an
284 * indexed file has completed.
286 * @param cls closure, our publishing context
287 * @param res resulting hash, NULL on error
290 hash_for_index_val (void *cls,
291 const GNUNET_HashCode *
294 struct IndexInfo *ii = cls;
297 if ( (res == NULL) ||
300 sizeof(GNUNET_HashCode))) )
302 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
303 _("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309 GNUNET_h2s (&ii->file_id));
311 GNUNET_SERVER_transmit_context_append_data (ii->tc,
313 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
314 GNUNET_SERVER_transmit_context_run (ii->tc,
315 GNUNET_TIME_UNIT_MINUTES);
319 signal_index_ok (ii);
324 * Handle INDEX_START-message.
327 * @param client identification of the client
328 * @param message the actual message
331 GNUNET_FS_handle_index_start (void *cls,
332 struct GNUNET_SERVER_Client *client,
333 const struct GNUNET_MessageHeader *message)
335 const struct IndexStartMessage *ism;
338 struct IndexInfo *ii;
345 msize = ntohs(message->size);
346 if ( (msize <= sizeof (struct IndexStartMessage)) ||
347 ( ((const char *)message)[msize-1] != '\0') )
350 GNUNET_SERVER_receive_done (client,
354 ism = (const struct IndexStartMessage*) message;
355 fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
358 GNUNET_SERVER_receive_done (client,
362 dev = GNUNET_ntohll (ism->device);
363 ino = GNUNET_ntohll (ism->inode);
364 ism = (const struct IndexStartMessage*) message;
365 slen = strlen (fn) + 1;
366 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
367 ii->filename = (const char*) &ii[1];
368 memcpy (&ii[1], fn, slen);
369 ii->file_id = ism->file_id;
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "Received `%s' message for file `%s'\n",
377 ii->tc = GNUNET_SERVER_transmit_context_create (client);
382 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
388 /* fast validation OK! */
389 signal_index_ok (ii);
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
396 (unsigned long long) ino,
397 (unsigned long long) myino,
399 (unsigned int) mydev);
401 /* slow validation, need to hash full file (again) */
402 ii->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
408 hash_for_index_val (ii, NULL);
414 * Handle INDEX_LIST_GET-message.
417 * @param client identification of the client
418 * @param message the actual message
421 GNUNET_FS_handle_index_list_get (void *cls,
422 struct GNUNET_SERVER_Client *client,
423 const struct GNUNET_MessageHeader *message)
425 struct GNUNET_SERVER_TransmitContext *tc;
426 struct IndexInfoMessage *iim;
427 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
430 struct IndexInfo *pos;
432 tc = GNUNET_SERVER_transmit_context_create (client);
433 iim = (struct IndexInfoMessage*) buf;
438 slen = strlen (fn) + 1;
439 if (slen + sizeof (struct IndexInfoMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
444 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
445 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
447 iim->file_id = pos->file_id;
448 memcpy (&iim[1], fn, slen);
449 GNUNET_SERVER_transmit_context_append_message (tc,
453 GNUNET_SERVER_transmit_context_append_data (tc,
455 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
456 GNUNET_SERVER_transmit_context_run (tc,
457 GNUNET_TIME_UNIT_MINUTES);
462 * Handle UNINDEX-message.
465 * @param client identification of the client
466 * @param message the actual message
469 GNUNET_FS_handle_unindex (void *cls,
470 struct GNUNET_SERVER_Client *client,
471 const struct GNUNET_MessageHeader *message)
473 const struct UnindexMessage *um;
474 struct IndexInfo *pos;
475 struct IndexInfo *prev;
476 struct IndexInfo *next;
477 struct GNUNET_SERVER_TransmitContext *tc;
480 um = (const struct UnindexMessage*) message;
487 if (0 == memcmp (&pos->file_id,
489 sizeof (GNUNET_HashCode)))
492 indexed_files = next;
495 GNUNET_break (GNUNET_OK ==
496 GNUNET_CONTAINER_multihashmap_remove (ifm,
498 (void*) pos->filename));
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "Client requested unindexing of file `%s': %s\n",
511 GNUNET_h2s (&um->file_id),
512 found ? "found" : "not found");
514 if (GNUNET_YES == found)
516 tc = GNUNET_SERVER_transmit_context_create (client);
517 GNUNET_SERVER_transmit_context_append_data (tc,
519 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
520 GNUNET_SERVER_transmit_context_run (tc,
521 GNUNET_TIME_UNIT_MINUTES);
528 * Continuation called from datastore's remove
532 * @param success did the deletion work?
533 * @param msg error message
536 remove_cont (void *cls,
540 if (GNUNET_OK != success)
541 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
542 _("Failed to delete bogus block: %s\n"),
548 * We've received an on-demand encoded block from the datastore.
549 * Attempt to do on-demand encoding and (if successful), call the
550 * continuation with the resulting block. On error, clean up and ask
551 * the datastore for more results.
553 * @param key key for the content
554 * @param size number of bytes in data
555 * @param data content stored
556 * @param type type of the content
557 * @param priority priority of the content
558 * @param anonymity anonymity-level for the content
559 * @param expiration expiration time for the content
560 * @param uid unique identifier for the datum;
561 * maybe 0 if no unique identifier is available
562 * @param cont function to call with the actual block (at most once, on success)
563 * @param cont_cls closure for cont
564 * @return GNUNET_OK on success
567 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
570 enum GNUNET_BLOCK_Type type,
573 struct GNUNET_TIME_Absolute
574 expiration, uint64_t uid,
575 GNUNET_DATASTORE_Iterator cont,
578 const struct OnDemandBlock *odb;
579 GNUNET_HashCode nkey;
580 struct GNUNET_CRYPTO_AesSessionKey skey;
581 struct GNUNET_CRYPTO_AesInitializationVector iv;
582 GNUNET_HashCode query;
584 char ndata[DBLOCK_SIZE];
585 char edata[DBLOCK_SIZE];
587 struct GNUNET_DISK_FileHandle *fh;
590 if (size != sizeof (struct OnDemandBlock))
593 GNUNET_DATASTORE_remove (dsh,
598 GNUNET_TIME_UNIT_FOREVER_REL,
601 return GNUNET_SYSERR;
603 odb = (const struct OnDemandBlock*) data;
604 off = GNUNET_ntohll (odb->offset);
605 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
609 (NULL == (fh = GNUNET_DISK_file_open (fn,
610 GNUNET_DISK_OPEN_READ,
611 GNUNET_DISK_PERM_NONE))) ||
613 GNUNET_DISK_file_seek (fh,
615 GNUNET_DISK_SEEK_SET)) ||
617 (nsize = GNUNET_DISK_file_read (fh,
621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
622 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
623 GNUNET_h2s (&odb->file_id),
625 (unsigned long long) off,
626 (fn == NULL) ? _("not indexed") : STRERROR (errno));
628 GNUNET_DISK_file_close (fh);
629 GNUNET_DATASTORE_remove (dsh,
634 GNUNET_TIME_UNIT_FOREVER_REL,
637 return GNUNET_SYSERR;
639 GNUNET_DISK_file_close (fh);
640 GNUNET_CRYPTO_hash (ndata,
643 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
644 GNUNET_CRYPTO_aes_encrypt (ndata,
649 GNUNET_CRYPTO_hash (edata,
652 if (0 != memcmp (&query,
654 sizeof (GNUNET_HashCode)))
656 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
657 _("Indexed file `%s' changed at offset %llu\n"),
659 (unsigned long long) off);
660 GNUNET_DATASTORE_remove (dsh,
665 GNUNET_TIME_UNIT_FOREVER_REL,
668 return GNUNET_SYSERR;
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
672 "On-demand encoded block for query `%s'\n",
679 GNUNET_BLOCK_TYPE_FS_DBLOCK,
689 * Task run during shutdown.
695 shutdown_task (void *cls,
696 const struct GNUNET_SCHEDULER_TaskContext *tc)
698 struct IndexInfo *pos;
700 GNUNET_CONTAINER_multihashmap_destroy (ifm);
702 while (NULL != (pos = indexed_files))
704 indexed_files = pos->next;
712 * Initialize the indexing submodule.
714 * @param c configuration to use
715 * @param d datastore to use
718 GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
719 struct GNUNET_DATASTORE_Handle *d)
723 ifm = GNUNET_CONTAINER_multihashmap_create (128);
724 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
731 /* end of gnunet-service-fs_indexing.c */