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 * - indexed files/blocks not removed on errors
31 #include "gnunet_core_service.h"
32 #include "gnunet_datastore_service.h"
33 #include "gnunet_peer_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet-service-fs_drq.h"
38 #include "gnunet-service-fs_indexing.h"
41 #define DEBUG_FS GNUNET_NO
45 * In-memory information about indexed files (also available
52 * This is a linked list.
54 struct IndexInfo *next;
57 * Name of the indexed file. Memory allocated
58 * at the end of this struct (do not free).
63 * Context for transmitting confirmation to client,
64 * NULL if we've done this already.
66 struct GNUNET_SERVER_TransmitContext *tc;
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 struct GNUNET_SCHEDULER_Handle *sched;
96 const struct GNUNET_CONFIGURATION_Handle *cfg;
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"),
198 while ( (GNUNET_OK ==
200 "Hash of indexed file",
202 sizeof (GNUNET_HashCode))) &&
204 GNUNET_BIO_read_string (rh,
205 "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_WARNING,
252 _("Index request received for file `%s' is 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;
290 if ( (res == NULL) ||
293 sizeof(GNUNET_HashCode))) )
295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
296 _("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 GNUNET_h2s (&ii->file_id));
304 GNUNET_SERVER_transmit_context_append_data (ii->tc,
306 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
307 GNUNET_SERVER_transmit_context_run (ii->tc,
308 GNUNET_TIME_UNIT_MINUTES);
312 signal_index_ok (ii);
317 * Handle INDEX_START-message.
320 * @param client identification of the client
321 * @param message the actual message
324 GNUNET_FS_handle_index_start (void *cls,
325 struct GNUNET_SERVER_Client *client,
326 const struct GNUNET_MessageHeader *message)
328 const struct IndexStartMessage *ism;
331 struct IndexInfo *ii;
338 msize = ntohs(message->size);
339 if ( (msize <= sizeof (struct IndexStartMessage)) ||
340 ( ((const char *)message)[msize-1] != '\0') )
343 GNUNET_SERVER_receive_done (client,
347 ism = (const struct IndexStartMessage*) message;
348 fn = (const char*) &ism[1];
349 dev = ntohl (ism->device);
350 ino = GNUNET_ntohll (ism->inode);
351 ism = (const struct IndexStartMessage*) message;
352 slen = strlen (fn) + 1;
353 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
354 ii->filename = (const char*) &ii[1];
355 memcpy (&ii[1], fn, slen);
356 ii->file_id = ism->file_id;
357 ii->tc = GNUNET_SERVER_transmit_context_create (client);
360 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
366 /* fast validation OK! */
367 signal_index_ok (ii);
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
373 (unsigned long long) ino,
374 (unsigned long long) myino,
376 (unsigned int) mydev);
378 /* slow validation, need to hash full file (again) */
379 GNUNET_CRYPTO_hash_file (sched,
380 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 /* FIXME: if this happens often, we need
599 to remove the OnDemand block from the DS! */
600 return GNUNET_SYSERR;
602 GNUNET_DISK_file_close (fh);
603 GNUNET_CRYPTO_hash (ndata,
606 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
607 GNUNET_CRYPTO_aes_encrypt (ndata,
612 GNUNET_CRYPTO_hash (edata,
615 if (0 != memcmp (&query,
617 sizeof (GNUNET_HashCode)))
619 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
620 _("Indexed file `%s' changed at offset %llu\n"),
622 (unsigned long long) off);
623 /* FIXME: if this happens often, we need
624 to remove the OnDemand block from the DS! */
625 return GNUNET_SYSERR;
631 GNUNET_DATASTORE_BLOCKTYPE_DBLOCK,
641 * Task run during shutdown.
647 shutdown_task (void *cls,
648 const struct GNUNET_SCHEDULER_TaskContext *tc)
650 struct IndexInfo *pos;
652 GNUNET_CONTAINER_multihashmap_destroy (ifm);
654 while (NULL != (pos = indexed_files))
656 indexed_files = pos->next;
665 * Initialize the indexing submodule.
667 * @param s scheduler to use
668 * @param c configuration to use
671 GNUNET_FS_indexing_init (struct GNUNET_SCHEDULER_Handle *s,
672 const struct GNUNET_CONFIGURATION_Handle *c)
676 ifm = GNUNET_CONTAINER_multihashmap_create (128);
677 GNUNET_SCHEDULER_add_delayed (sched,
678 GNUNET_TIME_UNIT_FOREVER_REL,
685 /* end of gnunet-service-fs_indexing.c */