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 2, 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_drq.h"
40 #include "gnunet-service-fs_indexing.h"
43 #define DEBUG_FS GNUNET_YES
46 * In-memory information about indexed files (also available
53 * This is a linked list.
55 struct IndexInfo *next;
58 * Name of the indexed file. Memory allocated
59 * at the end of this struct (do not free).
64 * Context for transmitting confirmation to client,
65 * NULL if we've done this already.
67 struct GNUNET_SERVER_TransmitContext *tc;
70 * Context for hashing of the file.
72 struct GNUNET_CRYPTO_FileHashContext *fhc;
75 * Hash of the contents of the file.
77 GNUNET_HashCode file_id;
83 * Linked list of indexed files.
85 static struct IndexInfo *indexed_files;
88 * Maps hash over content of indexed files to the respective filename.
89 * The filenames are pointers into the indexed_files linked list and
90 * do not need to be freed.
92 static struct GNUNET_CONTAINER_MultiHashMap *ifm;
97 static struct GNUNET_SCHEDULER_Handle *sched;
102 static const struct GNUNET_CONFIGURATION_Handle *cfg;
105 * Datastore handle. Created and destroyed by code in
106 * gnunet-service-fs (this is an alias).
108 static struct GNUNET_DATASTORE_Handle *dsh;
112 * Write the current index information list to disk.
117 struct GNUNET_BIO_WriteHandle *wh;
119 struct IndexInfo *pos;
122 GNUNET_CONFIGURATION_get_value_filename (cfg,
127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
128 _("Configuration option `%s' in section `%s' missing.\n"),
133 wh = GNUNET_BIO_write_open (fn);
136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
137 _("Could not open `%s'.\n"),
146 GNUNET_BIO_write (wh,
148 sizeof (GNUNET_HashCode))) ||
150 GNUNET_BIO_write_string (wh,
156 GNUNET_BIO_write_close (wh))
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
159 _("Error writing `%s'.\n"),
169 * Read index information from disk.
174 struct GNUNET_BIO_ReadHandle *rh;
176 struct IndexInfo *pos;
183 GNUNET_CONFIGURATION_get_value_filename (cfg,
188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
189 _("Configuration option `%s' in section `%s' missing.\n"),
194 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
196 /* no index info yet */
200 rh = GNUNET_BIO_read_open (fn);
203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
204 _("Could not open `%s'.\n"),
209 while ( (GNUNET_OK ==
211 "Hash of indexed file",
213 sizeof (GNUNET_HashCode))) &&
215 GNUNET_BIO_read_string (rh,
216 "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]);
361 dev = ntohl (ism->device);
362 ino = GNUNET_ntohll (ism->inode);
363 ism = (const struct IndexStartMessage*) message;
364 slen = strlen (fn) + 1;
365 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
366 ii->filename = (const char*) &ii[1];
367 memcpy (&ii[1], fn, slen);
368 ii->file_id = ism->file_id;
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Received `%s' message for file `%s'\n",
376 ii->tc = GNUNET_SERVER_transmit_context_create (client);
381 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
387 /* fast validation OK! */
388 signal_index_ok (ii);
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
395 (unsigned long long) ino,
396 (unsigned long long) myino,
398 (unsigned int) mydev);
400 /* slow validation, need to hash full file (again) */
401 ii->fhc = GNUNET_CRYPTO_hash_file (sched,
402 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];
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) >
440 GNUNET_SERVER_MAX_MESSAGE_SIZE)
445 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
446 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
448 iim->file_id = pos->file_id;
449 memcpy (&iim[1], fn, slen);
450 GNUNET_SERVER_transmit_context_append_message (tc,
454 GNUNET_SERVER_transmit_context_append_data (tc,
456 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
457 GNUNET_SERVER_transmit_context_run (tc,
458 GNUNET_TIME_UNIT_MINUTES);
463 * Handle UNINDEX-message.
466 * @param client identification of the client
467 * @param message the actual message
470 GNUNET_FS_handle_unindex (void *cls,
471 struct GNUNET_SERVER_Client *client,
472 const struct GNUNET_MessageHeader *message)
474 const struct UnindexMessage *um;
475 struct IndexInfo *pos;
476 struct IndexInfo *prev;
477 struct IndexInfo *next;
478 struct GNUNET_SERVER_TransmitContext *tc;
481 um = (const struct UnindexMessage*) message;
488 if (0 == memcmp (&pos->file_id,
490 sizeof (GNUNET_HashCode)))
493 indexed_files = next;
496 GNUNET_break (GNUNET_OK ==
497 GNUNET_CONTAINER_multihashmap_remove (ifm,
499 (void*) pos->filename));
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "Client requested unindexing of file `%s': %s\n",
512 GNUNET_h2s (&um->file_id),
513 found ? "found" : "not found");
515 if (GNUNET_YES == found)
517 tc = GNUNET_SERVER_transmit_context_create (client);
518 GNUNET_SERVER_transmit_context_append_data (tc,
520 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
521 GNUNET_SERVER_transmit_context_run (tc,
522 GNUNET_TIME_UNIT_MINUTES);
529 * Continuation called from datastore's remove
533 * @param success did the deletion work?
534 * @param msg error message
537 remove_cont (void *cls,
541 if (GNUNET_OK != success)
542 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
543 _("Failed to delete bogus block: %s\n"),
549 * We've received an on-demand encoded block from the datastore.
550 * Attempt to do on-demand encoding and (if successful), call the
551 * continuation with the resulting block. On error, clean up and ask
552 * the datastore for more results.
554 * @param key key for the content
555 * @param size number of bytes in data
556 * @param data content stored
557 * @param type type of the content
558 * @param priority priority of the content
559 * @param anonymity anonymity-level for the content
560 * @param expiration expiration time for the content
561 * @param uid unique identifier for the datum;
562 * maybe 0 if no unique identifier is available
563 * @param cont function to call with the actual block (at most once, on success)
564 * @param cont_cls closure for cont
565 * @return GNUNET_OK on success
568 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
571 enum GNUNET_BLOCK_Type type,
574 struct GNUNET_TIME_Absolute
575 expiration, uint64_t uid,
576 GNUNET_DATASTORE_Iterator cont,
579 const struct OnDemandBlock *odb;
580 GNUNET_HashCode nkey;
581 struct GNUNET_CRYPTO_AesSessionKey skey;
582 struct GNUNET_CRYPTO_AesInitializationVector iv;
583 GNUNET_HashCode query;
585 char ndata[DBLOCK_SIZE];
586 char edata[DBLOCK_SIZE];
588 struct GNUNET_DISK_FileHandle *fh;
591 if (size != sizeof (struct OnDemandBlock))
594 GNUNET_DATASTORE_remove (dsh,
599 GNUNET_TIME_UNIT_FOREVER_REL,
602 return GNUNET_SYSERR;
604 odb = (const struct OnDemandBlock*) data;
605 off = GNUNET_ntohll (odb->offset);
606 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
610 (NULL == (fh = GNUNET_DISK_file_open (fn,
611 GNUNET_DISK_OPEN_READ,
612 GNUNET_DISK_PERM_NONE))) ||
614 GNUNET_DISK_file_seek (fh,
616 GNUNET_DISK_SEEK_SET)) ||
618 (nsize = GNUNET_DISK_file_read (fh,
622 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
623 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
624 GNUNET_h2s (&odb->file_id),
626 (unsigned long long) off,
627 (fn == NULL) ? _("not indexed") : STRERROR (errno));
629 GNUNET_DISK_file_close (fh);
630 GNUNET_DATASTORE_remove (dsh,
635 GNUNET_TIME_UNIT_FOREVER_REL,
638 return GNUNET_SYSERR;
640 GNUNET_DISK_file_close (fh);
641 GNUNET_CRYPTO_hash (ndata,
644 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
645 GNUNET_CRYPTO_aes_encrypt (ndata,
650 GNUNET_CRYPTO_hash (edata,
653 if (0 != memcmp (&query,
655 sizeof (GNUNET_HashCode)))
657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
658 _("Indexed file `%s' changed at offset %llu\n"),
660 (unsigned long long) off);
661 GNUNET_DATASTORE_remove (dsh,
666 GNUNET_TIME_UNIT_FOREVER_REL,
669 return GNUNET_SYSERR;
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
673 "On-demand encoded block for query `%s'\n",
680 GNUNET_BLOCK_TYPE_DBLOCK,
690 * Task run during shutdown.
696 shutdown_task (void *cls,
697 const struct GNUNET_SCHEDULER_TaskContext *tc)
699 struct IndexInfo *pos;
701 GNUNET_CONTAINER_multihashmap_destroy (ifm);
703 while (NULL != (pos = indexed_files))
705 indexed_files = pos->next;
714 * Initialize the indexing submodule.
716 * @param s scheduler to use
717 * @param c configuration to use
718 * @param d datastore to use
721 GNUNET_FS_indexing_init (struct GNUNET_SCHEDULER_Handle *s,
722 const struct GNUNET_CONFIGURATION_Handle *c,
723 struct GNUNET_DATASTORE_Handle *d)
728 ifm = GNUNET_CONTAINER_multihashmap_create (128);
729 GNUNET_SCHEDULER_add_delayed (sched,
730 GNUNET_TIME_UNIT_FOREVER_REL,
737 /* end of gnunet-service-fs_indexing.c */