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_indexing.h"
40 #define DEBUG_FS GNUNET_NO
44 * In-memory information about indexed files (also available
51 * This is a linked list.
53 struct IndexInfo *next;
56 * Name of the indexed file. Memory allocated
57 * at the end of this struct (do not free).
62 * Context for transmitting confirmation to client,
63 * NULL if we've done this already.
65 struct GNUNET_SERVER_TransmitContext *tc;
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 struct GNUNET_SCHEDULER_Handle *sched;
95 const struct GNUNET_CONFIGURATION_Handle *cfg;
99 * Write the current index information list to disk.
104 struct GNUNET_BIO_WriteHandle *wh;
106 struct IndexInfo *pos;
109 GNUNET_CONFIGURATION_get_value_filename (cfg,
114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
115 _("Configuration option `%s' in section `%s' missing.\n"),
120 wh = GNUNET_BIO_write_open (fn);
123 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
124 _("Could not open `%s'.\n"),
133 GNUNET_BIO_write (wh,
135 sizeof (GNUNET_HashCode))) ||
137 GNUNET_BIO_write_string (wh,
143 GNUNET_BIO_write_close (wh))
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
146 _("Error writing `%s'.\n"),
156 * Read index information from disk.
161 struct GNUNET_BIO_ReadHandle *rh;
163 struct IndexInfo *pos;
170 GNUNET_CONFIGURATION_get_value_filename (cfg,
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
176 _("Configuration option `%s' in section `%s' missing.\n"),
181 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
183 /* no index info yet */
187 rh = GNUNET_BIO_read_open (fn);
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
191 _("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",
208 slen = strlen (fname) + 1;
209 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
211 pos->filename = (const char *) &pos[1];
212 memcpy (&pos[1], fname, slen);
214 GNUNET_CONTAINER_multihashmap_put (ifm,
216 (void*) pos->filename,
217 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
223 pos->next = indexed_files;
229 GNUNET_BIO_read_close (rh, &emsg))
236 * We've validated the hash of the file we're about to index. Signal
237 * success to the client and update our internal data structures.
239 * @param ii the index info entry for the request
242 signal_index_ok (struct IndexInfo *ii)
245 GNUNET_CONTAINER_multihashmap_put (ifm,
247 (void*) ii->filename,
248 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
251 _("Index request received for file `%s' is indexed as `%s'. Permitting anyway.\n"),
253 (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
255 GNUNET_SERVER_transmit_context_append_data (ii->tc,
257 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
258 GNUNET_SERVER_transmit_context_run (ii->tc,
259 GNUNET_TIME_UNIT_MINUTES);
263 ii->next = indexed_files;
266 GNUNET_SERVER_transmit_context_append_data (ii->tc,
268 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
269 GNUNET_SERVER_transmit_context_run (ii->tc,
270 GNUNET_TIME_UNIT_MINUTES);
276 * Function called once the hash computation over an
277 * indexed file has completed.
279 * @param cls closure, our publishing context
280 * @param res resulting hash, NULL on error
283 hash_for_index_val (void *cls,
284 const GNUNET_HashCode *
287 struct IndexInfo *ii = cls;
289 if ( (res == NULL) ||
292 sizeof(GNUNET_HashCode))) )
294 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
295 _("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 GNUNET_h2s (&ii->file_id));
303 GNUNET_SERVER_transmit_context_append_data (ii->tc,
305 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
306 GNUNET_SERVER_transmit_context_run (ii->tc,
307 GNUNET_TIME_UNIT_MINUTES);
311 signal_index_ok (ii);
316 * Handle INDEX_START-message.
319 * @param client identification of the client
320 * @param message the actual message
323 GNUNET_FS_handle_index_start (void *cls,
324 struct GNUNET_SERVER_Client *client,
325 const struct GNUNET_MessageHeader *message)
327 const struct IndexStartMessage *ism;
330 struct IndexInfo *ii;
337 msize = ntohs(message->size);
338 if ( (msize <= sizeof (struct IndexStartMessage)) ||
339 ( ((const char *)message)[msize-1] != '\0') )
342 GNUNET_SERVER_receive_done (client,
346 ism = (const struct IndexStartMessage*) message;
347 fn = (const char*) &ism[1];
348 dev = ntohl (ism->device);
349 ino = GNUNET_ntohll (ism->inode);
350 ism = (const struct IndexStartMessage*) message;
351 slen = strlen (fn) + 1;
352 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
353 ii->filename = (const char*) &ii[1];
354 memcpy (&ii[1], fn, slen);
355 ii->file_id = ism->file_id;
356 ii->tc = GNUNET_SERVER_transmit_context_create (client);
359 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
365 /* fast validation OK! */
366 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,
388 * Handle INDEX_LIST_GET-message.
391 * @param client identification of the client
392 * @param message the actual message
395 GNUNET_FS_handle_index_list_get (void *cls,
396 struct GNUNET_SERVER_Client *client,
397 const struct GNUNET_MessageHeader *message)
399 struct GNUNET_SERVER_TransmitContext *tc;
400 struct IndexInfoMessage *iim;
401 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
404 struct IndexInfo *pos;
406 tc = GNUNET_SERVER_transmit_context_create (client);
407 iim = (struct IndexInfoMessage*) buf;
412 slen = strlen (fn) + 1;
413 if (slen + sizeof (struct IndexInfoMessage) >
414 GNUNET_SERVER_MAX_MESSAGE_SIZE)
419 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
420 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
422 iim->file_id = pos->file_id;
423 memcpy (&iim[1], fn, slen);
424 GNUNET_SERVER_transmit_context_append_message (tc,
428 GNUNET_SERVER_transmit_context_append_data (tc,
430 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
431 GNUNET_SERVER_transmit_context_run (tc,
432 GNUNET_TIME_UNIT_MINUTES);
437 * Handle UNINDEX-message.
440 * @param client identification of the client
441 * @param message the actual message
444 GNUNET_FS_handle_unindex (void *cls,
445 struct GNUNET_SERVER_Client *client,
446 const struct GNUNET_MessageHeader *message)
448 const struct UnindexMessage *um;
449 struct IndexInfo *pos;
450 struct IndexInfo *prev;
451 struct IndexInfo *next;
452 struct GNUNET_SERVER_TransmitContext *tc;
455 um = (const struct UnindexMessage*) message;
462 if (0 == memcmp (&pos->file_id,
464 sizeof (GNUNET_HashCode)))
467 indexed_files = next;
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481 "Client requested unindexing of file `%s': %s\n",
482 GNUNET_h2s (&um->file_id),
483 found ? "found" : "not found");
485 if (GNUNET_YES == found)
487 tc = GNUNET_SERVER_transmit_context_create (client);
488 GNUNET_SERVER_transmit_context_append_data (tc,
490 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
491 GNUNET_SERVER_transmit_context_run (tc,
492 GNUNET_TIME_UNIT_MINUTES);
499 * Continuation called from datastore's remove
503 * @param success did the deletion work?
504 * @param msg error message
507 remove_cont (void *cls,
511 struct GNUNET_DATASTORE_Handle *dsh = cls;
513 if (GNUNET_OK != success)
514 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
515 _("Failed to delete bogus block: %s\n"),
517 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
522 * We've received an on-demand encoded block from the datastore.
523 * Attempt to do on-demand encoding and (if successful), call the
524 * continuation with the resulting block. On error, clean up and ask
525 * the datastore for more results.
527 * @param key key for the content
528 * @param size number of bytes in data
529 * @param data content stored
530 * @param type type of the content
531 * @param priority priority of the content
532 * @param anonymity anonymity-level for the content
533 * @param expiration expiration time for the content
534 * @param uid unique identifier for the datum;
535 * maybe 0 if no unique identifier is available
536 * @param cont function to call with the actual block
537 * @param cont_cls closure for cont
540 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
546 struct GNUNET_TIME_Absolute
547 expiration, uint64_t uid,
548 struct GNUNET_DATASTORE_Handle *dsh,
549 GNUNET_DATASTORE_Iterator cont,
552 const struct OnDemandBlock *odb;
553 GNUNET_HashCode nkey;
554 struct GNUNET_CRYPTO_AesSessionKey skey;
555 struct GNUNET_CRYPTO_AesInitializationVector iv;
556 GNUNET_HashCode query;
558 char ndata[DBLOCK_SIZE];
559 char edata[DBLOCK_SIZE];
561 struct GNUNET_DISK_FileHandle *fh;
564 if (size != sizeof (struct OnDemandBlock))
567 GNUNET_DATASTORE_remove (dsh,
573 GNUNET_TIME_UNIT_FOREVER_REL);
576 odb = (const struct OnDemandBlock*) data;
577 off = GNUNET_ntohll (odb->offset);
578 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
582 (NULL == (fh = GNUNET_DISK_file_open (fn,
583 GNUNET_DISK_OPEN_READ,
584 GNUNET_DISK_PERM_NONE))) ||
586 GNUNET_DISK_file_seek (fh,
588 GNUNET_DISK_SEEK_SET)) ||
590 (nsize = GNUNET_DISK_file_read (fh,
594 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
595 _("Could not access indexed file `%s' at offset %llu: %s\n"),
596 GNUNET_h2s (&odb->file_id),
597 (unsigned long long) off,
600 GNUNET_DISK_file_close (fh);
601 /* FIXME: if this happens often, we need
602 to remove the OnDemand block from the DS! */
603 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
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 /* FIXME: if this happens often, we need
628 to remove the OnDemand block from the DS! */
629 GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
636 GNUNET_DATASTORE_BLOCKTYPE_DBLOCK,
645 * Task run during shutdown.
651 shutdown_task (void *cls,
652 const struct GNUNET_SCHEDULER_TaskContext *tc)
654 struct IndexInfo *pos;
656 GNUNET_CONTAINER_multihashmap_destroy (ifm);
658 while (NULL != (pos = indexed_files))
660 indexed_files = pos->next;
669 * Initialize the indexing submodule.
671 * @param s scheduler to use
672 * @param c configuration to use
675 GNUNET_FS_init_indexing (struct GNUNET_SCHEDULER_Handle *s,
676 const struct GNUNET_CONFIGURATION_Handle *c)
680 ifm = GNUNET_CONTAINER_multihashmap_create (128);
681 GNUNET_SCHEDULER_add_delayed (sched,
682 GNUNET_TIME_UNIT_FOREVER_REL,
688 /* end of gnunet-service-fs_indexing.c */