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",
220 slen = strlen (fname) + 1;
221 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
223 pos->filename = (const char *) &pos[1];
224 memcpy (&pos[1], fname, slen);
226 GNUNET_CONTAINER_multihashmap_put (ifm,
228 (void*) pos->filename,
229 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
235 pos->next = indexed_files;
241 GNUNET_BIO_read_close (rh, &emsg))
248 * We've validated the hash of the file we're about to index. Signal
249 * success to the client and update our internal data structures.
251 * @param ii the index info entry for the request
254 signal_index_ok (struct IndexInfo *ii)
257 GNUNET_CONTAINER_multihashmap_put (ifm,
259 (void*) ii->filename,
260 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
262 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
263 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
265 (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
267 GNUNET_SERVER_transmit_context_append_data (ii->tc,
269 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
270 GNUNET_SERVER_transmit_context_run (ii->tc,
271 GNUNET_TIME_UNIT_MINUTES);
275 ii->next = indexed_files;
278 GNUNET_SERVER_transmit_context_append_data (ii->tc,
280 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
281 GNUNET_SERVER_transmit_context_run (ii->tc,
282 GNUNET_TIME_UNIT_MINUTES);
288 * Function called once the hash computation over an
289 * indexed file has completed.
291 * @param cls closure, our publishing context
292 * @param res resulting hash, NULL on error
295 hash_for_index_val (void *cls,
296 const GNUNET_HashCode *
299 struct IndexInfo *ii = cls;
302 if ( (res == NULL) ||
305 sizeof(GNUNET_HashCode))) )
307 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
308 _("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 GNUNET_h2s (&ii->file_id));
316 GNUNET_SERVER_transmit_context_append_data (ii->tc,
318 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
319 GNUNET_SERVER_transmit_context_run (ii->tc,
320 GNUNET_TIME_UNIT_MINUTES);
324 signal_index_ok (ii);
329 * Handle INDEX_START-message.
332 * @param client identification of the client
333 * @param message the actual message
336 GNUNET_FS_handle_index_start (void *cls,
337 struct GNUNET_SERVER_Client *client,
338 const struct GNUNET_MessageHeader *message)
340 const struct IndexStartMessage *ism;
343 struct IndexInfo *ii;
350 msize = ntohs(message->size);
351 if ( (msize <= sizeof (struct IndexStartMessage)) ||
352 ( ((const char *)message)[msize-1] != '\0') )
355 GNUNET_SERVER_receive_done (client,
359 ism = (const struct IndexStartMessage*) message;
360 fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
363 GNUNET_SERVER_receive_done (client,
367 dev = GNUNET_ntohll (ism->device);
368 ino = GNUNET_ntohll (ism->inode);
369 ism = (const struct IndexStartMessage*) message;
370 slen = strlen (fn) + 1;
371 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
372 ii->filename = (const char*) &ii[1];
373 memcpy (&ii[1], fn, slen);
374 ii->file_id = ism->file_id;
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Received `%s' message for file `%s'\n",
382 ii->tc = GNUNET_SERVER_transmit_context_create (client);
387 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
393 /* fast validation OK! */
394 signal_index_ok (ii);
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
401 (unsigned long long) ino,
402 (unsigned long long) myino,
404 (unsigned int) mydev);
406 /* slow validation, need to hash full file (again) */
407 ii->fhc = GNUNET_CRYPTO_hash_file (sched,
408 GNUNET_SCHEDULER_PRIORITY_IDLE,
414 hash_for_index_val (ii, NULL);
420 * Handle INDEX_LIST_GET-message.
423 * @param client identification of the client
424 * @param message the actual message
427 GNUNET_FS_handle_index_list_get (void *cls,
428 struct GNUNET_SERVER_Client *client,
429 const struct GNUNET_MessageHeader *message)
431 struct GNUNET_SERVER_TransmitContext *tc;
432 struct IndexInfoMessage *iim;
433 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
436 struct IndexInfo *pos;
438 tc = GNUNET_SERVER_transmit_context_create (client);
439 iim = (struct IndexInfoMessage*) buf;
444 slen = strlen (fn) + 1;
445 if (slen + sizeof (struct IndexInfoMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
450 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
451 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
453 iim->file_id = pos->file_id;
454 memcpy (&iim[1], fn, slen);
455 GNUNET_SERVER_transmit_context_append_message (tc,
459 GNUNET_SERVER_transmit_context_append_data (tc,
461 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
462 GNUNET_SERVER_transmit_context_run (tc,
463 GNUNET_TIME_UNIT_MINUTES);
468 * Handle UNINDEX-message.
471 * @param client identification of the client
472 * @param message the actual message
475 GNUNET_FS_handle_unindex (void *cls,
476 struct GNUNET_SERVER_Client *client,
477 const struct GNUNET_MessageHeader *message)
479 const struct UnindexMessage *um;
480 struct IndexInfo *pos;
481 struct IndexInfo *prev;
482 struct IndexInfo *next;
483 struct GNUNET_SERVER_TransmitContext *tc;
486 um = (const struct UnindexMessage*) message;
493 if (0 == memcmp (&pos->file_id,
495 sizeof (GNUNET_HashCode)))
498 indexed_files = next;
501 GNUNET_break (GNUNET_OK ==
502 GNUNET_CONTAINER_multihashmap_remove (ifm,
504 (void*) pos->filename));
515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
516 "Client requested unindexing of file `%s': %s\n",
517 GNUNET_h2s (&um->file_id),
518 found ? "found" : "not found");
520 if (GNUNET_YES == found)
522 tc = GNUNET_SERVER_transmit_context_create (client);
523 GNUNET_SERVER_transmit_context_append_data (tc,
525 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
526 GNUNET_SERVER_transmit_context_run (tc,
527 GNUNET_TIME_UNIT_MINUTES);
534 * Continuation called from datastore's remove
538 * @param success did the deletion work?
539 * @param msg error message
542 remove_cont (void *cls,
546 if (GNUNET_OK != success)
547 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
548 _("Failed to delete bogus block: %s\n"),
554 * We've received an on-demand encoded block from the datastore.
555 * Attempt to do on-demand encoding and (if successful), call the
556 * continuation with the resulting block. On error, clean up and ask
557 * the datastore for more results.
559 * @param key key for the content
560 * @param size number of bytes in data
561 * @param data content stored
562 * @param type type of the content
563 * @param priority priority of the content
564 * @param anonymity anonymity-level for the content
565 * @param expiration expiration time for the content
566 * @param uid unique identifier for the datum;
567 * maybe 0 if no unique identifier is available
568 * @param cont function to call with the actual block (at most once, on success)
569 * @param cont_cls closure for cont
570 * @return GNUNET_OK on success
573 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
576 enum GNUNET_BLOCK_Type type,
579 struct GNUNET_TIME_Absolute
580 expiration, uint64_t uid,
581 GNUNET_DATASTORE_Iterator cont,
584 const struct OnDemandBlock *odb;
585 GNUNET_HashCode nkey;
586 struct GNUNET_CRYPTO_AesSessionKey skey;
587 struct GNUNET_CRYPTO_AesInitializationVector iv;
588 GNUNET_HashCode query;
590 char ndata[DBLOCK_SIZE];
591 char edata[DBLOCK_SIZE];
593 struct GNUNET_DISK_FileHandle *fh;
596 if (size != sizeof (struct OnDemandBlock))
599 GNUNET_DATASTORE_remove (dsh,
604 GNUNET_TIME_UNIT_FOREVER_REL,
607 return GNUNET_SYSERR;
609 odb = (const struct OnDemandBlock*) data;
610 off = GNUNET_ntohll (odb->offset);
611 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
615 (NULL == (fh = GNUNET_DISK_file_open (fn,
616 GNUNET_DISK_OPEN_READ,
617 GNUNET_DISK_PERM_NONE))) ||
619 GNUNET_DISK_file_seek (fh,
621 GNUNET_DISK_SEEK_SET)) ||
623 (nsize = GNUNET_DISK_file_read (fh,
627 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
628 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
629 GNUNET_h2s (&odb->file_id),
631 (unsigned long long) off,
632 (fn == NULL) ? _("not indexed") : STRERROR (errno));
634 GNUNET_DISK_file_close (fh);
635 GNUNET_DATASTORE_remove (dsh,
640 GNUNET_TIME_UNIT_FOREVER_REL,
643 return GNUNET_SYSERR;
645 GNUNET_DISK_file_close (fh);
646 GNUNET_CRYPTO_hash (ndata,
649 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
650 GNUNET_CRYPTO_aes_encrypt (ndata,
655 GNUNET_CRYPTO_hash (edata,
658 if (0 != memcmp (&query,
660 sizeof (GNUNET_HashCode)))
662 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
663 _("Indexed file `%s' changed at offset %llu\n"),
665 (unsigned long long) off);
666 GNUNET_DATASTORE_remove (dsh,
671 GNUNET_TIME_UNIT_FOREVER_REL,
674 return GNUNET_SYSERR;
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678 "On-demand encoded block for query `%s'\n",
685 GNUNET_BLOCK_TYPE_FS_DBLOCK,
695 * Task run during shutdown.
701 shutdown_task (void *cls,
702 const struct GNUNET_SCHEDULER_TaskContext *tc)
704 struct IndexInfo *pos;
706 GNUNET_CONTAINER_multihashmap_destroy (ifm);
708 while (NULL != (pos = indexed_files))
710 indexed_files = pos->next;
719 * Initialize the indexing submodule.
721 * @param s scheduler to use
722 * @param c configuration to use
723 * @param d datastore to use
726 GNUNET_FS_indexing_init (struct GNUNET_SCHEDULER_Handle *s,
727 const struct GNUNET_CONFIGURATION_Handle *c,
728 struct GNUNET_DATASTORE_Handle *d)
733 ifm = GNUNET_CONTAINER_multihashmap_create (128);
734 GNUNET_SCHEDULER_add_delayed (sched,
735 GNUNET_TIME_UNIT_FOREVER_REL,
742 /* end of gnunet-service-fs_indexing.c */