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 struct GNUNET_SCHEDULER_Handle *sched;
101 static const struct GNUNET_CONFIGURATION_Handle *cfg;
104 * Datastore handle. Created and destroyed by code in
105 * gnunet-service-fs (this is an alias).
107 static struct GNUNET_DATASTORE_Handle *dsh;
111 * Write the current index information list to disk.
116 struct GNUNET_BIO_WriteHandle *wh;
118 struct IndexInfo *pos;
121 GNUNET_CONFIGURATION_get_value_filename (cfg,
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
127 _("Configuration option `%s' in section `%s' missing.\n"),
132 wh = GNUNET_BIO_write_open (fn);
135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
136 _("Could not open `%s'.\n"),
145 GNUNET_BIO_write (wh,
147 sizeof (GNUNET_HashCode))) ||
149 GNUNET_BIO_write_string (wh,
155 GNUNET_BIO_write_close (wh))
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
158 _("Error writing `%s'.\n"),
168 * Read index information from disk.
173 struct GNUNET_BIO_ReadHandle *rh;
175 struct IndexInfo *pos;
182 GNUNET_CONFIGURATION_get_value_filename (cfg,
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
188 _("Configuration option `%s' in section `%s' missing.\n"),
193 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
195 /* no index info yet */
199 rh = GNUNET_BIO_read_open (fn);
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
203 _("Could not open `%s'.\n"),
208 while ( (GNUNET_OK ==
210 "Hash of indexed file",
212 sizeof (GNUNET_HashCode))) &&
214 GNUNET_BIO_read_string (rh,
215 "Name of indexed file",
219 slen = strlen (fname) + 1;
220 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
222 pos->filename = (const char *) &pos[1];
223 memcpy (&pos[1], fname, slen);
225 GNUNET_CONTAINER_multihashmap_put (ifm,
227 (void*) pos->filename,
228 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
234 pos->next = indexed_files;
240 GNUNET_BIO_read_close (rh, &emsg))
247 * We've validated the hash of the file we're about to index. Signal
248 * success to the client and update our internal data structures.
250 * @param ii the index info entry for the request
253 signal_index_ok (struct IndexInfo *ii)
256 GNUNET_CONTAINER_multihashmap_put (ifm,
258 (void*) ii->filename,
259 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
261 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
262 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
264 (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
266 GNUNET_SERVER_transmit_context_append_data (ii->tc,
268 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
269 GNUNET_SERVER_transmit_context_run (ii->tc,
270 GNUNET_TIME_UNIT_MINUTES);
274 ii->next = indexed_files;
277 GNUNET_SERVER_transmit_context_append_data (ii->tc,
279 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
280 GNUNET_SERVER_transmit_context_run (ii->tc,
281 GNUNET_TIME_UNIT_MINUTES);
287 * Function called once the hash computation over an
288 * indexed file has completed.
290 * @param cls closure, our publishing context
291 * @param res resulting hash, NULL on error
294 hash_for_index_val (void *cls,
295 const GNUNET_HashCode *
298 struct IndexInfo *ii = cls;
301 if ( (res == NULL) ||
304 sizeof(GNUNET_HashCode))) )
306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
307 _("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 GNUNET_h2s (&ii->file_id));
315 GNUNET_SERVER_transmit_context_append_data (ii->tc,
317 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
318 GNUNET_SERVER_transmit_context_run (ii->tc,
319 GNUNET_TIME_UNIT_MINUTES);
323 signal_index_ok (ii);
328 * Handle INDEX_START-message.
331 * @param client identification of the client
332 * @param message the actual message
335 GNUNET_FS_handle_index_start (void *cls,
336 struct GNUNET_SERVER_Client *client,
337 const struct GNUNET_MessageHeader *message)
339 const struct IndexStartMessage *ism;
342 struct IndexInfo *ii;
349 msize = ntohs(message->size);
350 if ( (msize <= sizeof (struct IndexStartMessage)) ||
351 ( ((const char *)message)[msize-1] != '\0') )
354 GNUNET_SERVER_receive_done (client,
358 ism = (const struct IndexStartMessage*) message;
359 fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
362 GNUNET_SERVER_receive_done (client,
366 dev = GNUNET_ntohll (ism->device);
367 ino = GNUNET_ntohll (ism->inode);
368 ism = (const struct IndexStartMessage*) message;
369 slen = strlen (fn) + 1;
370 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
371 ii->filename = (const char*) &ii[1];
372 memcpy (&ii[1], fn, slen);
373 ii->file_id = ism->file_id;
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Received `%s' message for file `%s'\n",
381 ii->tc = GNUNET_SERVER_transmit_context_create (client);
386 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
392 /* fast validation OK! */
393 signal_index_ok (ii);
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
400 (unsigned long long) ino,
401 (unsigned long long) myino,
403 (unsigned int) mydev);
405 /* slow validation, need to hash full file (again) */
406 ii->fhc = GNUNET_CRYPTO_hash_file (sched,
407 GNUNET_SCHEDULER_PRIORITY_IDLE,
413 hash_for_index_val (ii, NULL);
419 * Handle INDEX_LIST_GET-message.
422 * @param client identification of the client
423 * @param message the actual message
426 GNUNET_FS_handle_index_list_get (void *cls,
427 struct GNUNET_SERVER_Client *client,
428 const struct GNUNET_MessageHeader *message)
430 struct GNUNET_SERVER_TransmitContext *tc;
431 struct IndexInfoMessage *iim;
432 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
435 struct IndexInfo *pos;
437 tc = GNUNET_SERVER_transmit_context_create (client);
438 iim = (struct IndexInfoMessage*) buf;
443 slen = strlen (fn) + 1;
444 if (slen + sizeof (struct IndexInfoMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
449 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
450 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
452 iim->file_id = pos->file_id;
453 memcpy (&iim[1], fn, slen);
454 GNUNET_SERVER_transmit_context_append_message (tc,
458 GNUNET_SERVER_transmit_context_append_data (tc,
460 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
461 GNUNET_SERVER_transmit_context_run (tc,
462 GNUNET_TIME_UNIT_MINUTES);
467 * Handle UNINDEX-message.
470 * @param client identification of the client
471 * @param message the actual message
474 GNUNET_FS_handle_unindex (void *cls,
475 struct GNUNET_SERVER_Client *client,
476 const struct GNUNET_MessageHeader *message)
478 const struct UnindexMessage *um;
479 struct IndexInfo *pos;
480 struct IndexInfo *prev;
481 struct IndexInfo *next;
482 struct GNUNET_SERVER_TransmitContext *tc;
485 um = (const struct UnindexMessage*) message;
492 if (0 == memcmp (&pos->file_id,
494 sizeof (GNUNET_HashCode)))
497 indexed_files = next;
500 GNUNET_break (GNUNET_OK ==
501 GNUNET_CONTAINER_multihashmap_remove (ifm,
503 (void*) pos->filename));
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 "Client requested unindexing of file `%s': %s\n",
516 GNUNET_h2s (&um->file_id),
517 found ? "found" : "not found");
519 if (GNUNET_YES == found)
521 tc = GNUNET_SERVER_transmit_context_create (client);
522 GNUNET_SERVER_transmit_context_append_data (tc,
524 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
525 GNUNET_SERVER_transmit_context_run (tc,
526 GNUNET_TIME_UNIT_MINUTES);
533 * Continuation called from datastore's remove
537 * @param success did the deletion work?
538 * @param msg error message
541 remove_cont (void *cls,
545 if (GNUNET_OK != success)
546 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
547 _("Failed to delete bogus block: %s\n"),
553 * We've received an on-demand encoded block from the datastore.
554 * Attempt to do on-demand encoding and (if successful), call the
555 * continuation with the resulting block. On error, clean up and ask
556 * the datastore for more results.
558 * @param key key for the content
559 * @param size number of bytes in data
560 * @param data content stored
561 * @param type type of the content
562 * @param priority priority of the content
563 * @param anonymity anonymity-level for the content
564 * @param expiration expiration time for the content
565 * @param uid unique identifier for the datum;
566 * maybe 0 if no unique identifier is available
567 * @param cont function to call with the actual block (at most once, on success)
568 * @param cont_cls closure for cont
569 * @return GNUNET_OK on success
572 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
575 enum GNUNET_BLOCK_Type type,
578 struct GNUNET_TIME_Absolute
579 expiration, uint64_t uid,
580 GNUNET_DATASTORE_Iterator cont,
583 const struct OnDemandBlock *odb;
584 GNUNET_HashCode nkey;
585 struct GNUNET_CRYPTO_AesSessionKey skey;
586 struct GNUNET_CRYPTO_AesInitializationVector iv;
587 GNUNET_HashCode query;
589 char ndata[DBLOCK_SIZE];
590 char edata[DBLOCK_SIZE];
592 struct GNUNET_DISK_FileHandle *fh;
595 if (size != sizeof (struct OnDemandBlock))
598 GNUNET_DATASTORE_remove (dsh,
603 GNUNET_TIME_UNIT_FOREVER_REL,
606 return GNUNET_SYSERR;
608 odb = (const struct OnDemandBlock*) data;
609 off = GNUNET_ntohll (odb->offset);
610 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
614 (NULL == (fh = GNUNET_DISK_file_open (fn,
615 GNUNET_DISK_OPEN_READ,
616 GNUNET_DISK_PERM_NONE))) ||
618 GNUNET_DISK_file_seek (fh,
620 GNUNET_DISK_SEEK_SET)) ||
622 (nsize = GNUNET_DISK_file_read (fh,
626 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
627 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
628 GNUNET_h2s (&odb->file_id),
630 (unsigned long long) off,
631 (fn == NULL) ? _("not indexed") : STRERROR (errno));
633 GNUNET_DISK_file_close (fh);
634 GNUNET_DATASTORE_remove (dsh,
639 GNUNET_TIME_UNIT_FOREVER_REL,
642 return GNUNET_SYSERR;
644 GNUNET_DISK_file_close (fh);
645 GNUNET_CRYPTO_hash (ndata,
648 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
649 GNUNET_CRYPTO_aes_encrypt (ndata,
654 GNUNET_CRYPTO_hash (edata,
657 if (0 != memcmp (&query,
659 sizeof (GNUNET_HashCode)))
661 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
662 _("Indexed file `%s' changed at offset %llu\n"),
664 (unsigned long long) off);
665 GNUNET_DATASTORE_remove (dsh,
670 GNUNET_TIME_UNIT_FOREVER_REL,
673 return GNUNET_SYSERR;
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 "On-demand encoded block for query `%s'\n",
684 GNUNET_BLOCK_TYPE_DBLOCK,
694 * Task run during shutdown.
700 shutdown_task (void *cls,
701 const struct GNUNET_SCHEDULER_TaskContext *tc)
703 struct IndexInfo *pos;
705 GNUNET_CONTAINER_multihashmap_destroy (ifm);
707 while (NULL != (pos = indexed_files))
709 indexed_files = pos->next;
718 * Initialize the indexing submodule.
720 * @param s scheduler to use
721 * @param c configuration to use
722 * @param d datastore to use
725 GNUNET_FS_indexing_init (struct GNUNET_SCHEDULER_Handle *s,
726 const struct GNUNET_CONFIGURATION_Handle *c,
727 struct GNUNET_DATASTORE_Handle *d)
732 ifm = GNUNET_CONTAINER_multihashmap_create (128);
733 GNUNET_SCHEDULER_add_delayed (sched,
734 GNUNET_TIME_UNIT_FOREVER_REL,
741 /* end of gnunet-service-fs_indexing.c */