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
28 #include "gnunet_core_service.h"
29 #include "gnunet_datastore_service.h"
30 #include "gnunet_peer_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_signatures.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet-service-fs.h"
35 #include "gnunet-service-fs_indexing.h"
39 * In-memory information about indexed files (also available
46 * This is a linked list.
48 struct IndexInfo *next;
51 * Name of the indexed file. Memory allocated
52 * at the end of this struct (do not free).
57 * Context for transmitting confirmation to client,
58 * NULL if we've done this already.
60 struct GNUNET_SERVER_TransmitContext *tc;
63 * Context for hashing of the file.
65 struct GNUNET_CRYPTO_FileHashContext *fhc;
68 * Hash of the contents of the file.
70 GNUNET_HashCode file_id;
76 * Linked list of indexed files.
78 static struct IndexInfo *indexed_files;
81 * Maps hash over content of indexed files to the respective filename.
82 * The filenames are pointers into the indexed_files linked list and
83 * do not need to be freed.
85 static struct GNUNET_CONTAINER_MultiHashMap *ifm;
90 static const struct GNUNET_CONFIGURATION_Handle *cfg;
93 * Datastore handle. Created and destroyed by code in
94 * gnunet-service-fs (this is an alias).
96 static struct GNUNET_DATASTORE_Handle *dsh;
100 * Write the current index information list to disk.
105 struct GNUNET_BIO_WriteHandle *wh;
107 struct IndexInfo *pos;
110 GNUNET_CONFIGURATION_get_value_filename (cfg,
115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
116 _("Configuration option `%s' in section `%s' missing.\n"),
121 wh = GNUNET_BIO_write_open (fn);
124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
125 _("Could not open `%s'.\n"),
134 GNUNET_BIO_write (wh,
136 sizeof (GNUNET_HashCode))) ||
138 GNUNET_BIO_write_string (wh,
144 GNUNET_BIO_write_close (wh))
146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
147 _("Error writing `%s'.\n"),
157 * Read index information from disk.
162 struct GNUNET_BIO_ReadHandle *rh;
164 struct IndexInfo *pos;
171 GNUNET_CONFIGURATION_get_value_filename (cfg,
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
177 _("Configuration option `%s' in section `%s' missing.\n"),
182 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
184 /* no index info yet */
188 rh = GNUNET_BIO_read_open (fn);
191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
192 _("Could not open `%s'.\n"),
197 while ( (GNUNET_OK ==
199 "Hash of indexed file",
201 sizeof (GNUNET_HashCode))) &&
203 GNUNET_BIO_read_string (rh,
204 "Name of indexed file",
209 slen = strlen (fname) + 1;
210 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
212 pos->filename = (const char *) &pos[1];
213 memcpy (&pos[1], fname, slen);
215 GNUNET_CONTAINER_multihashmap_put (ifm,
217 (void*) pos->filename,
218 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
224 pos->next = indexed_files;
230 GNUNET_BIO_read_close (rh, &emsg))
237 * We've validated the hash of the file we're about to index. Signal
238 * success to the client and update our internal data structures.
240 * @param ii the index info entry for the request
243 signal_index_ok (struct IndexInfo *ii)
246 GNUNET_CONTAINER_multihashmap_put (ifm,
248 (void*) ii->filename,
249 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
251 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
252 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
254 (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
256 GNUNET_SERVER_transmit_context_append_data (ii->tc,
258 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
259 GNUNET_SERVER_transmit_context_run (ii->tc,
260 GNUNET_TIME_UNIT_MINUTES);
264 ii->next = indexed_files;
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);
277 * Function called once the hash computation over an
278 * indexed file has completed.
280 * @param cls closure, our publishing context
281 * @param res resulting hash, NULL on error
284 hash_for_index_val (void *cls,
285 const GNUNET_HashCode *
288 struct IndexInfo *ii = cls;
291 if ( (res == NULL) ||
294 sizeof(GNUNET_HashCode))) )
296 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
297 _("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303 GNUNET_h2s (&ii->file_id));
305 GNUNET_SERVER_transmit_context_append_data (ii->tc,
307 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
308 GNUNET_SERVER_transmit_context_run (ii->tc,
309 GNUNET_TIME_UNIT_MINUTES);
313 signal_index_ok (ii);
318 * Handle INDEX_START-message.
321 * @param client identification of the client
322 * @param message the actual message
325 GNUNET_FS_handle_index_start (void *cls,
326 struct GNUNET_SERVER_Client *client,
327 const struct GNUNET_MessageHeader *message)
329 const struct IndexStartMessage *ism;
332 struct IndexInfo *ii;
339 msize = ntohs(message->size);
340 if ( (msize <= sizeof (struct IndexStartMessage)) ||
341 ( ((const char *)message)[msize-1] != '\0') )
344 GNUNET_SERVER_receive_done (client,
348 ism = (const struct IndexStartMessage*) message;
349 fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
352 GNUNET_SERVER_receive_done (client,
356 dev = GNUNET_ntohll (ism->device);
357 ino = GNUNET_ntohll (ism->inode);
358 ism = (const struct IndexStartMessage*) message;
359 slen = strlen (fn) + 1;
360 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
361 ii->filename = (const char*) &ii[1];
362 memcpy (&ii[1], fn, slen);
363 ii->file_id = ism->file_id;
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "Received `%s' message for file `%s'\n",
371 ii->tc = GNUNET_SERVER_transmit_context_create (client);
376 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
382 /* fast validation OK! */
383 signal_index_ok (ii);
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
390 (unsigned long long) ino,
391 (unsigned long long) myino,
393 (unsigned int) mydev);
395 /* slow validation, need to hash full file (again) */
396 ii->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
402 hash_for_index_val (ii, NULL);
408 * Handle INDEX_LIST_GET-message.
411 * @param client identification of the client
412 * @param message the actual message
415 GNUNET_FS_handle_index_list_get (void *cls,
416 struct GNUNET_SERVER_Client *client,
417 const struct GNUNET_MessageHeader *message)
419 struct GNUNET_SERVER_TransmitContext *tc;
420 struct IndexInfoMessage *iim;
421 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
424 struct IndexInfo *pos;
426 tc = GNUNET_SERVER_transmit_context_create (client);
427 iim = (struct IndexInfoMessage*) buf;
432 slen = strlen (fn) + 1;
433 if (slen + sizeof (struct IndexInfoMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
438 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
439 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
441 iim->file_id = pos->file_id;
442 memcpy (&iim[1], fn, slen);
443 GNUNET_SERVER_transmit_context_append_message (tc,
447 GNUNET_SERVER_transmit_context_append_data (tc,
449 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
450 GNUNET_SERVER_transmit_context_run (tc,
451 GNUNET_TIME_UNIT_MINUTES);
456 * Handle UNINDEX-message.
459 * @param client identification of the client
460 * @param message the actual message
463 GNUNET_FS_handle_unindex (void *cls,
464 struct GNUNET_SERVER_Client *client,
465 const struct GNUNET_MessageHeader *message)
467 const struct UnindexMessage *um;
468 struct IndexInfo *pos;
469 struct IndexInfo *prev;
470 struct IndexInfo *next;
471 struct GNUNET_SERVER_TransmitContext *tc;
474 um = (const struct UnindexMessage*) message;
481 if (0 == memcmp (&pos->file_id,
483 sizeof (GNUNET_HashCode)))
486 indexed_files = next;
489 GNUNET_break (GNUNET_OK ==
490 GNUNET_CONTAINER_multihashmap_remove (ifm,
492 (void*) pos->filename));
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
504 "Client requested unindexing of file `%s': %s\n",
505 GNUNET_h2s (&um->file_id),
506 found ? "found" : "not found");
508 if (GNUNET_YES == found)
510 tc = GNUNET_SERVER_transmit_context_create (client);
511 GNUNET_SERVER_transmit_context_append_data (tc,
513 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
514 GNUNET_SERVER_transmit_context_run (tc,
515 GNUNET_TIME_UNIT_MINUTES);
522 * Continuation called from datastore's remove
526 * @param success did the deletion work?
527 * @param msg error message
530 remove_cont (void *cls,
534 if (GNUNET_OK != success)
535 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
536 _("Failed to delete bogus block: %s\n"),
542 * We've received an on-demand encoded block from the datastore.
543 * Attempt to do on-demand encoding and (if successful), call the
544 * continuation with the resulting block. On error, clean up and ask
545 * the datastore for more results.
547 * @param key key for the content
548 * @param size number of bytes in data
549 * @param data content stored
550 * @param type type of the content
551 * @param priority priority of the content
552 * @param anonymity anonymity-level for the content
553 * @param expiration expiration time for the content
554 * @param uid unique identifier for the datum;
555 * maybe 0 if no unique identifier is available
556 * @param cont function to call with the actual block (at most once, on success)
557 * @param cont_cls closure for cont
558 * @return GNUNET_OK on success
561 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
564 enum GNUNET_BLOCK_Type type,
567 struct GNUNET_TIME_Absolute
568 expiration, uint64_t uid,
569 GNUNET_DATASTORE_DatumProcessor cont,
572 const struct OnDemandBlock *odb;
573 GNUNET_HashCode nkey;
574 struct GNUNET_CRYPTO_AesSessionKey skey;
575 struct GNUNET_CRYPTO_AesInitializationVector iv;
576 GNUNET_HashCode query;
578 char ndata[DBLOCK_SIZE];
579 char edata[DBLOCK_SIZE];
581 struct GNUNET_DISK_FileHandle *fh;
584 if (size != sizeof (struct OnDemandBlock))
587 GNUNET_DATASTORE_remove (dsh,
592 GNUNET_TIME_UNIT_FOREVER_REL,
595 return GNUNET_SYSERR;
597 odb = (const struct OnDemandBlock*) data;
598 off = GNUNET_ntohll (odb->offset);
599 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
603 (NULL == (fh = GNUNET_DISK_file_open (fn,
604 GNUNET_DISK_OPEN_READ,
605 GNUNET_DISK_PERM_NONE))) ||
607 GNUNET_DISK_file_seek (fh,
609 GNUNET_DISK_SEEK_SET)) ||
611 (nsize = GNUNET_DISK_file_read (fh,
615 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
616 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
617 GNUNET_h2s (&odb->file_id),
619 (unsigned long long) off,
620 (fn == NULL) ? _("not indexed") : STRERROR (errno));
622 GNUNET_DISK_file_close (fh);
623 GNUNET_DATASTORE_remove (dsh,
628 GNUNET_TIME_UNIT_FOREVER_REL,
631 return GNUNET_SYSERR;
633 GNUNET_DISK_file_close (fh);
634 GNUNET_CRYPTO_hash (ndata,
637 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
638 GNUNET_CRYPTO_aes_encrypt (ndata,
643 GNUNET_CRYPTO_hash (edata,
646 if (0 != memcmp (&query,
648 sizeof (GNUNET_HashCode)))
650 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
651 _("Indexed file `%s' changed at offset %llu\n"),
653 (unsigned long long) off);
654 GNUNET_DATASTORE_remove (dsh,
659 GNUNET_TIME_UNIT_FOREVER_REL,
662 return GNUNET_SYSERR;
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "On-demand encoded block for query `%s'\n",
673 GNUNET_BLOCK_TYPE_FS_DBLOCK,
683 * Shutdown the module.
686 GNUNET_FS_indexing_done ()
688 struct IndexInfo *pos;
690 GNUNET_CONTAINER_multihashmap_destroy (ifm);
692 while (NULL != (pos = indexed_files))
694 indexed_files = pos->next;
695 if (pos->fhc != NULL)
696 GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
704 * Initialize the indexing submodule.
706 * @param c configuration to use
707 * @param d datastore to use
710 GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
711 struct GNUNET_DATASTORE_Handle *d)
715 ifm = GNUNET_CONTAINER_multihashmap_create (128);
720 /* end of gnunet-service-fs_indexing.c */