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
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_drq.h"
35 #include "gnunet-service-fs_indexing.h"
38 #define DEBUG_FS GNUNET_YES
41 * In-memory information about indexed files (also available
48 * This is a linked list.
50 struct IndexInfo *next;
53 * Name of the indexed file. Memory allocated
54 * at the end of this struct (do not free).
59 * Context for transmitting confirmation to client,
60 * NULL if we've done this already.
62 struct GNUNET_SERVER_TransmitContext *tc;
65 * Hash of the contents of the file.
67 GNUNET_HashCode file_id;
73 * Linked list of indexed files.
75 static struct IndexInfo *indexed_files;
78 * Maps hash over content of indexed files to the respective filename.
79 * The filenames are pointers into the indexed_files linked list and
80 * do not need to be freed.
82 static struct GNUNET_CONTAINER_MultiHashMap *ifm;
87 static struct GNUNET_SCHEDULER_Handle *sched;
92 static const struct GNUNET_CONFIGURATION_Handle *cfg;
96 * Write the current index information list to disk.
101 struct GNUNET_BIO_WriteHandle *wh;
103 struct IndexInfo *pos;
106 GNUNET_CONFIGURATION_get_value_filename (cfg,
111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
112 _("Configuration option `%s' in section `%s' missing.\n"),
117 wh = GNUNET_BIO_write_open (fn);
120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
121 _("Could not open `%s'.\n"),
130 GNUNET_BIO_write (wh,
132 sizeof (GNUNET_HashCode))) ||
134 GNUNET_BIO_write_string (wh,
140 GNUNET_BIO_write_close (wh))
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
143 _("Error writing `%s'.\n"),
153 * Read index information from disk.
158 struct GNUNET_BIO_ReadHandle *rh;
160 struct IndexInfo *pos;
167 GNUNET_CONFIGURATION_get_value_filename (cfg,
172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
173 _("Configuration option `%s' in section `%s' missing.\n"),
178 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
180 /* no index info yet */
184 rh = GNUNET_BIO_read_open (fn);
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
188 _("Could not open `%s'.\n"),
194 while ( (GNUNET_OK ==
196 "Hash of indexed file",
198 sizeof (GNUNET_HashCode))) &&
200 GNUNET_BIO_read_string (rh,
201 "Name of indexed file",
205 slen = strlen (fname) + 1;
206 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
208 pos->filename = (const char *) &pos[1];
209 memcpy (&pos[1], fname, slen);
211 GNUNET_CONTAINER_multihashmap_put (ifm,
213 (void*) pos->filename,
214 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
220 pos->next = indexed_files;
226 GNUNET_BIO_read_close (rh, &emsg))
233 * We've validated the hash of the file we're about to index. Signal
234 * success to the client and update our internal data structures.
236 * @param ii the index info entry for the request
239 signal_index_ok (struct IndexInfo *ii)
242 GNUNET_CONTAINER_multihashmap_put (ifm,
244 (void*) ii->filename,
245 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
247 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
248 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
250 (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
252 GNUNET_SERVER_transmit_context_append_data (ii->tc,
254 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
255 GNUNET_SERVER_transmit_context_run (ii->tc,
256 GNUNET_TIME_UNIT_MINUTES);
260 ii->next = indexed_files;
263 GNUNET_SERVER_transmit_context_append_data (ii->tc,
265 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
266 GNUNET_SERVER_transmit_context_run (ii->tc,
267 GNUNET_TIME_UNIT_MINUTES);
273 * Function called once the hash computation over an
274 * indexed file has completed.
276 * @param cls closure, our publishing context
277 * @param res resulting hash, NULL on error
280 hash_for_index_val (void *cls,
281 const GNUNET_HashCode *
284 struct IndexInfo *ii = cls;
286 if ( (res == NULL) ||
289 sizeof(GNUNET_HashCode))) )
291 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
292 _("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 GNUNET_h2s (&ii->file_id));
300 GNUNET_SERVER_transmit_context_append_data (ii->tc,
302 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
303 GNUNET_SERVER_transmit_context_run (ii->tc,
304 GNUNET_TIME_UNIT_MINUTES);
308 signal_index_ok (ii);
313 * Handle INDEX_START-message.
316 * @param client identification of the client
317 * @param message the actual message
320 GNUNET_FS_handle_index_start (void *cls,
321 struct GNUNET_SERVER_Client *client,
322 const struct GNUNET_MessageHeader *message)
324 const struct IndexStartMessage *ism;
327 struct IndexInfo *ii;
334 msize = ntohs(message->size);
335 if ( (msize <= sizeof (struct IndexStartMessage)) ||
336 ( ((const char *)message)[msize-1] != '\0') )
339 GNUNET_SERVER_receive_done (client,
343 ism = (const struct IndexStartMessage*) message;
344 fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
345 dev = ntohl (ism->device);
346 ino = GNUNET_ntohll (ism->inode);
347 ism = (const struct IndexStartMessage*) message;
348 slen = strlen (fn) + 1;
349 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
350 ii->filename = (const char*) &ii[1];
351 memcpy (&ii[1], fn, slen);
352 ii->file_id = ism->file_id;
353 ii->tc = GNUNET_SERVER_transmit_context_create (client);
358 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
364 /* fast validation OK! */
365 signal_index_ok (ii);
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
372 (unsigned long long) ino,
373 (unsigned long long) myino,
375 (unsigned int) mydev);
377 /* slow validation, need to hash full file (again) */
378 GNUNET_CRYPTO_hash_file (sched,
379 GNUNET_SCHEDULER_PRIORITY_IDLE,
389 * Handle INDEX_LIST_GET-message.
392 * @param client identification of the client
393 * @param message the actual message
396 GNUNET_FS_handle_index_list_get (void *cls,
397 struct GNUNET_SERVER_Client *client,
398 const struct GNUNET_MessageHeader *message)
400 struct GNUNET_SERVER_TransmitContext *tc;
401 struct IndexInfoMessage *iim;
402 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
405 struct IndexInfo *pos;
407 tc = GNUNET_SERVER_transmit_context_create (client);
408 iim = (struct IndexInfoMessage*) buf;
413 slen = strlen (fn) + 1;
414 if (slen + sizeof (struct IndexInfoMessage) >
415 GNUNET_SERVER_MAX_MESSAGE_SIZE)
420 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
421 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
423 iim->file_id = pos->file_id;
424 memcpy (&iim[1], fn, slen);
425 GNUNET_SERVER_transmit_context_append_message (tc,
429 GNUNET_SERVER_transmit_context_append_data (tc,
431 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
432 GNUNET_SERVER_transmit_context_run (tc,
433 GNUNET_TIME_UNIT_MINUTES);
438 * Handle UNINDEX-message.
441 * @param client identification of the client
442 * @param message the actual message
445 GNUNET_FS_handle_unindex (void *cls,
446 struct GNUNET_SERVER_Client *client,
447 const struct GNUNET_MessageHeader *message)
449 const struct UnindexMessage *um;
450 struct IndexInfo *pos;
451 struct IndexInfo *prev;
452 struct IndexInfo *next;
453 struct GNUNET_SERVER_TransmitContext *tc;
456 um = (const struct UnindexMessage*) message;
463 if (0 == memcmp (&pos->file_id,
465 sizeof (GNUNET_HashCode)))
468 indexed_files = next;
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Client requested unindexing of file `%s': %s\n",
483 GNUNET_h2s (&um->file_id),
484 found ? "found" : "not found");
486 if (GNUNET_YES == found)
488 tc = GNUNET_SERVER_transmit_context_create (client);
489 GNUNET_SERVER_transmit_context_append_data (tc,
491 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
492 GNUNET_SERVER_transmit_context_run (tc,
493 GNUNET_TIME_UNIT_MINUTES);
500 * Continuation called from datastore's remove
504 * @param success did the deletion work?
505 * @param msg error message
508 remove_cont (void *cls,
512 if (GNUNET_OK != success)
513 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
514 _("Failed to delete bogus block: %s\n"),
520 * We've received an on-demand encoded block from the datastore.
521 * Attempt to do on-demand encoding and (if successful), call the
522 * continuation with the resulting block. On error, clean up and ask
523 * the datastore for more results.
525 * @param key key for the content
526 * @param size number of bytes in data
527 * @param data content stored
528 * @param type type of the content
529 * @param priority priority of the content
530 * @param anonymity anonymity-level for the content
531 * @param expiration expiration time for the content
532 * @param uid unique identifier for the datum;
533 * maybe 0 if no unique identifier is available
534 * @param cont function to call with the actual block (at most once, on success)
535 * @param cont_cls closure for cont
536 * @return GNUNET_OK on success
539 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
545 struct GNUNET_TIME_Absolute
546 expiration, uint64_t uid,
547 GNUNET_DATASTORE_Iterator cont,
550 const struct OnDemandBlock *odb;
551 GNUNET_HashCode nkey;
552 struct GNUNET_CRYPTO_AesSessionKey skey;
553 struct GNUNET_CRYPTO_AesInitializationVector iv;
554 GNUNET_HashCode query;
556 char ndata[DBLOCK_SIZE];
557 char edata[DBLOCK_SIZE];
559 struct GNUNET_DISK_FileHandle *fh;
562 if (size != sizeof (struct OnDemandBlock))
565 GNUNET_FS_drq_remove (key,
570 GNUNET_TIME_UNIT_FOREVER_REL);
571 return GNUNET_SYSERR;
573 odb = (const struct OnDemandBlock*) data;
574 off = GNUNET_ntohll (odb->offset);
575 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
579 (NULL == (fh = GNUNET_DISK_file_open (fn,
580 GNUNET_DISK_OPEN_READ,
581 GNUNET_DISK_PERM_NONE))) ||
583 GNUNET_DISK_file_seek (fh,
585 GNUNET_DISK_SEEK_SET)) ||
587 (nsize = GNUNET_DISK_file_read (fh,
591 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
592 _("Could not access indexed file `%s' at offset %llu: %s\n"),
593 GNUNET_h2s (&odb->file_id),
594 (unsigned long long) off,
597 GNUNET_DISK_file_close (fh);
598 GNUNET_FS_drq_remove (key,
603 GNUNET_TIME_UNIT_FOREVER_REL);
604 return GNUNET_SYSERR;
606 GNUNET_DISK_file_close (fh);
607 GNUNET_CRYPTO_hash (ndata,
610 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
611 GNUNET_CRYPTO_aes_encrypt (ndata,
616 GNUNET_CRYPTO_hash (edata,
619 if (0 != memcmp (&query,
621 sizeof (GNUNET_HashCode)))
623 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
624 _("Indexed file `%s' changed at offset %llu\n"),
626 (unsigned long long) off);
627 GNUNET_FS_drq_remove (key,
632 GNUNET_TIME_UNIT_FOREVER_REL);
633 return GNUNET_SYSERR;
636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
637 "On-demand encoded block for query `%s'\n",
644 GNUNET_DATASTORE_BLOCKTYPE_DBLOCK,
654 * Task run during shutdown.
660 shutdown_task (void *cls,
661 const struct GNUNET_SCHEDULER_TaskContext *tc)
663 struct IndexInfo *pos;
665 GNUNET_CONTAINER_multihashmap_destroy (ifm);
667 while (NULL != (pos = indexed_files))
669 indexed_files = pos->next;
678 * Initialize the indexing submodule.
680 * @param s scheduler to use
681 * @param c configuration to use
684 GNUNET_FS_indexing_init (struct GNUNET_SCHEDULER_Handle *s,
685 const struct GNUNET_CONFIGURATION_Handle *c)
689 ifm = GNUNET_CONTAINER_multihashmap_create (128);
690 GNUNET_SCHEDULER_add_delayed (sched,
691 GNUNET_TIME_UNIT_FOREVER_REL,
698 /* end of gnunet-service-fs_indexing.c */