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"),
210 while ( (GNUNET_OK ==
212 "Hash of indexed file",
214 sizeof (GNUNET_HashCode))) &&
216 GNUNET_BIO_read_string (rh,
217 "Name of indexed file",
221 slen = strlen (fname) + 1;
222 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
224 pos->filename = (const char *) &pos[1];
225 memcpy (&pos[1], fname, slen);
227 GNUNET_CONTAINER_multihashmap_put (ifm,
229 (void*) pos->filename,
230 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
236 pos->next = indexed_files;
242 GNUNET_BIO_read_close (rh, &emsg))
249 * We've validated the hash of the file we're about to index. Signal
250 * success to the client and update our internal data structures.
252 * @param ii the index info entry for the request
255 signal_index_ok (struct IndexInfo *ii)
258 GNUNET_CONTAINER_multihashmap_put (ifm,
260 (void*) ii->filename,
261 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
263 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
264 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
266 (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
268 GNUNET_SERVER_transmit_context_append_data (ii->tc,
270 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
271 GNUNET_SERVER_transmit_context_run (ii->tc,
272 GNUNET_TIME_UNIT_MINUTES);
276 ii->next = indexed_files;
279 GNUNET_SERVER_transmit_context_append_data (ii->tc,
281 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
282 GNUNET_SERVER_transmit_context_run (ii->tc,
283 GNUNET_TIME_UNIT_MINUTES);
289 * Function called once the hash computation over an
290 * indexed file has completed.
292 * @param cls closure, our publishing context
293 * @param res resulting hash, NULL on error
296 hash_for_index_val (void *cls,
297 const GNUNET_HashCode *
300 struct IndexInfo *ii = cls;
303 if ( (res == NULL) ||
306 sizeof(GNUNET_HashCode))) )
308 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
309 _("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 GNUNET_h2s (&ii->file_id));
317 GNUNET_SERVER_transmit_context_append_data (ii->tc,
319 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
320 GNUNET_SERVER_transmit_context_run (ii->tc,
321 GNUNET_TIME_UNIT_MINUTES);
325 signal_index_ok (ii);
330 * Handle INDEX_START-message.
333 * @param client identification of the client
334 * @param message the actual message
337 GNUNET_FS_handle_index_start (void *cls,
338 struct GNUNET_SERVER_Client *client,
339 const struct GNUNET_MessageHeader *message)
341 const struct IndexStartMessage *ism;
344 struct IndexInfo *ii;
351 msize = ntohs(message->size);
352 if ( (msize <= sizeof (struct IndexStartMessage)) ||
353 ( ((const char *)message)[msize-1] != '\0') )
356 GNUNET_SERVER_receive_done (client,
360 ism = (const struct IndexStartMessage*) message;
361 fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
362 dev = ntohl (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 (sched,
403 GNUNET_SCHEDULER_PRIORITY_IDLE,
409 hash_for_index_val (ii, NULL);
415 * Handle INDEX_LIST_GET-message.
418 * @param client identification of the client
419 * @param message the actual message
422 GNUNET_FS_handle_index_list_get (void *cls,
423 struct GNUNET_SERVER_Client *client,
424 const struct GNUNET_MessageHeader *message)
426 struct GNUNET_SERVER_TransmitContext *tc;
427 struct IndexInfoMessage *iim;
428 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
431 struct IndexInfo *pos;
433 tc = GNUNET_SERVER_transmit_context_create (client);
434 iim = (struct IndexInfoMessage*) buf;
439 slen = strlen (fn) + 1;
440 if (slen + sizeof (struct IndexInfoMessage) >
441 GNUNET_SERVER_MAX_MESSAGE_SIZE)
446 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
447 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
449 iim->file_id = pos->file_id;
450 memcpy (&iim[1], fn, slen);
451 GNUNET_SERVER_transmit_context_append_message (tc,
455 GNUNET_SERVER_transmit_context_append_data (tc,
457 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
458 GNUNET_SERVER_transmit_context_run (tc,
459 GNUNET_TIME_UNIT_MINUTES);
464 * Handle UNINDEX-message.
467 * @param client identification of the client
468 * @param message the actual message
471 GNUNET_FS_handle_unindex (void *cls,
472 struct GNUNET_SERVER_Client *client,
473 const struct GNUNET_MessageHeader *message)
475 const struct UnindexMessage *um;
476 struct IndexInfo *pos;
477 struct IndexInfo *prev;
478 struct IndexInfo *next;
479 struct GNUNET_SERVER_TransmitContext *tc;
482 um = (const struct UnindexMessage*) message;
489 if (0 == memcmp (&pos->file_id,
491 sizeof (GNUNET_HashCode)))
494 indexed_files = next;
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "Client requested unindexing of file `%s': %s\n",
509 GNUNET_h2s (&um->file_id),
510 found ? "found" : "not found");
512 if (GNUNET_YES == found)
514 tc = GNUNET_SERVER_transmit_context_create (client);
515 GNUNET_SERVER_transmit_context_append_data (tc,
517 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
518 GNUNET_SERVER_transmit_context_run (tc,
519 GNUNET_TIME_UNIT_MINUTES);
526 * Continuation called from datastore's remove
530 * @param success did the deletion work?
531 * @param msg error message
534 remove_cont (void *cls,
538 if (GNUNET_OK != success)
539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
540 _("Failed to delete bogus block: %s\n"),
546 * We've received an on-demand encoded block from the datastore.
547 * Attempt to do on-demand encoding and (if successful), call the
548 * continuation with the resulting block. On error, clean up and ask
549 * the datastore for more results.
551 * @param key key for the content
552 * @param size number of bytes in data
553 * @param data content stored
554 * @param type type of the content
555 * @param priority priority of the content
556 * @param anonymity anonymity-level for the content
557 * @param expiration expiration time for the content
558 * @param uid unique identifier for the datum;
559 * maybe 0 if no unique identifier is available
560 * @param cont function to call with the actual block (at most once, on success)
561 * @param cont_cls closure for cont
562 * @return GNUNET_OK on success
565 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
568 enum GNUNET_BLOCK_Type type,
571 struct GNUNET_TIME_Absolute
572 expiration, uint64_t uid,
573 GNUNET_DATASTORE_Iterator cont,
576 const struct OnDemandBlock *odb;
577 GNUNET_HashCode nkey;
578 struct GNUNET_CRYPTO_AesSessionKey skey;
579 struct GNUNET_CRYPTO_AesInitializationVector iv;
580 GNUNET_HashCode query;
582 char ndata[DBLOCK_SIZE];
583 char edata[DBLOCK_SIZE];
585 struct GNUNET_DISK_FileHandle *fh;
588 if (size != sizeof (struct OnDemandBlock))
591 GNUNET_DATASTORE_remove (dsh,
596 GNUNET_TIME_UNIT_FOREVER_REL,
599 return GNUNET_SYSERR;
601 odb = (const struct OnDemandBlock*) data;
602 off = GNUNET_ntohll (odb->offset);
603 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
607 (NULL == (fh = GNUNET_DISK_file_open (fn,
608 GNUNET_DISK_OPEN_READ,
609 GNUNET_DISK_PERM_NONE))) ||
611 GNUNET_DISK_file_seek (fh,
613 GNUNET_DISK_SEEK_SET)) ||
615 (nsize = GNUNET_DISK_file_read (fh,
619 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
620 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
621 GNUNET_h2s (&odb->file_id),
623 (unsigned long long) off,
626 GNUNET_DISK_file_close (fh);
627 GNUNET_DATASTORE_remove (dsh,
632 GNUNET_TIME_UNIT_FOREVER_REL,
635 return GNUNET_SYSERR;
637 GNUNET_DISK_file_close (fh);
638 GNUNET_CRYPTO_hash (ndata,
641 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
642 GNUNET_CRYPTO_aes_encrypt (ndata,
647 GNUNET_CRYPTO_hash (edata,
650 if (0 != memcmp (&query,
652 sizeof (GNUNET_HashCode)))
654 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
655 _("Indexed file `%s' changed at offset %llu\n"),
657 (unsigned long long) off);
658 GNUNET_DATASTORE_remove (dsh,
663 GNUNET_TIME_UNIT_FOREVER_REL,
666 return GNUNET_SYSERR;
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "On-demand encoded block for query `%s'\n",
677 GNUNET_BLOCK_TYPE_DBLOCK,
687 * Task run during shutdown.
693 shutdown_task (void *cls,
694 const struct GNUNET_SCHEDULER_TaskContext *tc)
696 struct IndexInfo *pos;
698 GNUNET_CONTAINER_multihashmap_destroy (ifm);
700 while (NULL != (pos = indexed_files))
702 indexed_files = pos->next;
711 * Initialize the indexing submodule.
713 * @param s scheduler to use
714 * @param c configuration to use
715 * @param d datastore to use
718 GNUNET_FS_indexing_init (struct GNUNET_SCHEDULER_Handle *s,
719 const struct GNUNET_CONFIGURATION_Handle *c,
720 struct GNUNET_DATASTORE_Handle *d)
725 ifm = GNUNET_CONTAINER_multihashmap_create (128);
726 GNUNET_SCHEDULER_add_delayed (sched,
727 GNUNET_TIME_UNIT_FOREVER_REL,
734 /* end of gnunet-service-fs_indexing.c */