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_indexing.h"
37 #define DEBUG_FS GNUNET_NO
40 * In-memory information about indexed files (also available
47 * This is a linked list.
49 struct IndexInfo *next;
52 * Name of the indexed file. Memory allocated
53 * at the end of this struct (do not free).
58 * Context for transmitting confirmation to client,
59 * NULL if we've done this already.
61 struct GNUNET_SERVER_TransmitContext *tc;
64 * Context for hashing of the file.
66 struct GNUNET_CRYPTO_FileHashContext *fhc;
69 * Hash of the contents of the file.
71 GNUNET_HashCode file_id;
77 * Linked list of indexed files.
79 static struct IndexInfo *indexed_files;
82 * Maps hash over content of indexed files to the respective filename.
83 * The filenames are pointers into the indexed_files linked list and
84 * do not need to be freed.
86 static struct GNUNET_CONTAINER_MultiHashMap *ifm;
91 static const struct GNUNET_CONFIGURATION_Handle *cfg;
94 * Datastore handle. Created and destroyed by code in
95 * gnunet-service-fs (this is an alias).
97 static struct GNUNET_DATASTORE_Handle *dsh;
101 * Write the current index information list to disk.
106 struct GNUNET_BIO_WriteHandle *wh;
108 struct IndexInfo *pos;
111 GNUNET_CONFIGURATION_get_value_filename (cfg,
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
117 _("Configuration option `%s' in section `%s' missing.\n"),
122 wh = GNUNET_BIO_write_open (fn);
125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
126 _("Could not open `%s'.\n"),
135 GNUNET_BIO_write (wh,
137 sizeof (GNUNET_HashCode))) ||
139 GNUNET_BIO_write_string (wh,
145 GNUNET_BIO_write_close (wh))
147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
148 _("Error writing `%s'.\n"),
158 * Read index information from disk.
163 struct GNUNET_BIO_ReadHandle *rh;
165 struct IndexInfo *pos;
172 GNUNET_CONFIGURATION_get_value_filename (cfg,
177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
178 _("Configuration option `%s' in section `%s' missing.\n"),
183 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
185 /* no index info yet */
189 rh = GNUNET_BIO_read_open (fn);
192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
193 _("Could not open `%s'.\n"),
198 while ( (GNUNET_OK ==
200 "Hash of indexed file",
202 sizeof (GNUNET_HashCode))) &&
204 GNUNET_BIO_read_string (rh,
205 "Name of indexed file",
210 slen = strlen (fname) + 1;
211 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
213 pos->filename = (const char *) &pos[1];
214 memcpy (&pos[1], fname, slen);
216 GNUNET_CONTAINER_multihashmap_put (ifm,
218 (void*) pos->filename,
219 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
225 pos->next = indexed_files;
231 GNUNET_BIO_read_close (rh, &emsg))
238 * We've validated the hash of the file we're about to index. Signal
239 * success to the client and update our internal data structures.
241 * @param ii the index info entry for the request
244 signal_index_ok (struct IndexInfo *ii)
247 GNUNET_CONTAINER_multihashmap_put (ifm,
249 (void*) ii->filename,
250 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
252 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
253 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
255 (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
257 GNUNET_SERVER_transmit_context_append_data (ii->tc,
259 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
260 GNUNET_SERVER_transmit_context_run (ii->tc,
261 GNUNET_TIME_UNIT_MINUTES);
265 ii->next = indexed_files;
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);
278 * Function called once the hash computation over an
279 * indexed file has completed.
281 * @param cls closure, our publishing context
282 * @param res resulting hash, NULL on error
285 hash_for_index_val (void *cls,
286 const GNUNET_HashCode *
289 struct IndexInfo *ii = cls;
292 if ( (res == NULL) ||
295 sizeof(GNUNET_HashCode))) )
297 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
298 _("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 GNUNET_h2s (&ii->file_id));
306 GNUNET_SERVER_transmit_context_append_data (ii->tc,
308 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
309 GNUNET_SERVER_transmit_context_run (ii->tc,
310 GNUNET_TIME_UNIT_MINUTES);
314 signal_index_ok (ii);
319 * Handle INDEX_START-message.
322 * @param client identification of the client
323 * @param message the actual message
326 GNUNET_FS_handle_index_start (void *cls,
327 struct GNUNET_SERVER_Client *client,
328 const struct GNUNET_MessageHeader *message)
330 const struct IndexStartMessage *ism;
333 struct IndexInfo *ii;
340 msize = ntohs(message->size);
341 if ( (msize <= sizeof (struct IndexStartMessage)) ||
342 ( ((const char *)message)[msize-1] != '\0') )
345 GNUNET_SERVER_receive_done (client,
349 ism = (const struct IndexStartMessage*) message;
350 fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
353 GNUNET_SERVER_receive_done (client,
357 dev = GNUNET_ntohll (ism->device);
358 ino = GNUNET_ntohll (ism->inode);
359 ism = (const struct IndexStartMessage*) message;
360 slen = strlen (fn) + 1;
361 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
362 ii->filename = (const char*) &ii[1];
363 memcpy (&ii[1], fn, slen);
364 ii->file_id = ism->file_id;
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "Received `%s' message for file `%s'\n",
372 ii->tc = GNUNET_SERVER_transmit_context_create (client);
377 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
383 /* fast validation OK! */
384 signal_index_ok (ii);
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
391 (unsigned long long) ino,
392 (unsigned long long) myino,
394 (unsigned int) mydev);
396 /* slow validation, need to hash full file (again) */
397 ii->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
403 hash_for_index_val (ii, NULL);
409 * Handle INDEX_LIST_GET-message.
412 * @param client identification of the client
413 * @param message the actual message
416 GNUNET_FS_handle_index_list_get (void *cls,
417 struct GNUNET_SERVER_Client *client,
418 const struct GNUNET_MessageHeader *message)
420 struct GNUNET_SERVER_TransmitContext *tc;
421 struct IndexInfoMessage *iim;
422 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
425 struct IndexInfo *pos;
427 tc = GNUNET_SERVER_transmit_context_create (client);
428 iim = (struct IndexInfoMessage*) buf;
433 slen = strlen (fn) + 1;
434 if (slen + sizeof (struct IndexInfoMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
439 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
440 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
442 iim->file_id = pos->file_id;
443 memcpy (&iim[1], fn, slen);
444 GNUNET_SERVER_transmit_context_append_message (tc,
448 GNUNET_SERVER_transmit_context_append_data (tc,
450 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
451 GNUNET_SERVER_transmit_context_run (tc,
452 GNUNET_TIME_UNIT_MINUTES);
457 * Handle UNINDEX-message.
460 * @param client identification of the client
461 * @param message the actual message
464 GNUNET_FS_handle_unindex (void *cls,
465 struct GNUNET_SERVER_Client *client,
466 const struct GNUNET_MessageHeader *message)
468 const struct UnindexMessage *um;
469 struct IndexInfo *pos;
470 struct IndexInfo *prev;
471 struct IndexInfo *next;
472 struct GNUNET_SERVER_TransmitContext *tc;
475 um = (const struct UnindexMessage*) message;
482 if (0 == memcmp (&pos->file_id,
484 sizeof (GNUNET_HashCode)))
487 indexed_files = next;
490 GNUNET_break (GNUNET_OK ==
491 GNUNET_CONTAINER_multihashmap_remove (ifm,
493 (void*) pos->filename));
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "Client requested unindexing of file `%s': %s\n",
506 GNUNET_h2s (&um->file_id),
507 found ? "found" : "not found");
509 if (GNUNET_YES == found)
511 tc = GNUNET_SERVER_transmit_context_create (client);
512 GNUNET_SERVER_transmit_context_append_data (tc,
514 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
515 GNUNET_SERVER_transmit_context_run (tc,
516 GNUNET_TIME_UNIT_MINUTES);
523 * Continuation called from datastore's remove
527 * @param success did the deletion work?
528 * @param msg error message
531 remove_cont (void *cls,
535 if (GNUNET_OK != success)
536 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
537 _("Failed to delete bogus block: %s\n"),
543 * We've received an on-demand encoded block from the datastore.
544 * Attempt to do on-demand encoding and (if successful), call the
545 * continuation with the resulting block. On error, clean up and ask
546 * the datastore for more results.
548 * @param key key for the content
549 * @param size number of bytes in data
550 * @param data content stored
551 * @param type type of the content
552 * @param priority priority of the content
553 * @param anonymity anonymity-level for the content
554 * @param expiration expiration time for the content
555 * @param uid unique identifier for the datum;
556 * maybe 0 if no unique identifier is available
557 * @param cont function to call with the actual block (at most once, on success)
558 * @param cont_cls closure for cont
559 * @return GNUNET_OK on success
562 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
565 enum GNUNET_BLOCK_Type type,
568 struct GNUNET_TIME_Absolute
569 expiration, uint64_t uid,
570 GNUNET_DATASTORE_Iterator cont,
573 const struct OnDemandBlock *odb;
574 GNUNET_HashCode nkey;
575 struct GNUNET_CRYPTO_AesSessionKey skey;
576 struct GNUNET_CRYPTO_AesInitializationVector iv;
577 GNUNET_HashCode query;
579 char ndata[DBLOCK_SIZE];
580 char edata[DBLOCK_SIZE];
582 struct GNUNET_DISK_FileHandle *fh;
585 if (size != sizeof (struct OnDemandBlock))
588 GNUNET_DATASTORE_remove (dsh,
593 GNUNET_TIME_UNIT_FOREVER_REL,
596 return GNUNET_SYSERR;
598 odb = (const struct OnDemandBlock*) data;
599 off = GNUNET_ntohll (odb->offset);
600 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
604 (NULL == (fh = GNUNET_DISK_file_open (fn,
605 GNUNET_DISK_OPEN_READ,
606 GNUNET_DISK_PERM_NONE))) ||
608 GNUNET_DISK_file_seek (fh,
610 GNUNET_DISK_SEEK_SET)) ||
612 (nsize = GNUNET_DISK_file_read (fh,
616 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
617 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
618 GNUNET_h2s (&odb->file_id),
620 (unsigned long long) off,
621 (fn == NULL) ? _("not indexed") : STRERROR (errno));
623 GNUNET_DISK_file_close (fh);
624 GNUNET_DATASTORE_remove (dsh,
629 GNUNET_TIME_UNIT_FOREVER_REL,
632 return GNUNET_SYSERR;
634 GNUNET_DISK_file_close (fh);
635 GNUNET_CRYPTO_hash (ndata,
638 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
639 GNUNET_CRYPTO_aes_encrypt (ndata,
644 GNUNET_CRYPTO_hash (edata,
647 if (0 != memcmp (&query,
649 sizeof (GNUNET_HashCode)))
651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
652 _("Indexed file `%s' changed at offset %llu\n"),
654 (unsigned long long) off);
655 GNUNET_DATASTORE_remove (dsh,
660 GNUNET_TIME_UNIT_FOREVER_REL,
663 return GNUNET_SYSERR;
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "On-demand encoded block for query `%s'\n",
674 GNUNET_BLOCK_TYPE_FS_DBLOCK,
684 * Task run during shutdown.
690 shutdown_task (void *cls,
691 const struct GNUNET_SCHEDULER_TaskContext *tc)
693 struct IndexInfo *pos;
695 GNUNET_CONTAINER_multihashmap_destroy (ifm);
697 while (NULL != (pos = indexed_files))
699 indexed_files = pos->next;
700 if (pos->fhc != NULL)
701 GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
709 * Initialize the indexing submodule.
711 * @param c configuration to use
712 * @param d datastore to use
715 GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
716 struct GNUNET_DATASTORE_Handle *d)
720 ifm = GNUNET_CONTAINER_multihashmap_create (128);
721 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
728 /* end of gnunet-service-fs_indexing.c */