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 if (0 != ism->reserved)
352 GNUNET_SERVER_receive_done (client,
356 fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
359 GNUNET_SERVER_receive_done (client,
363 dev = GNUNET_ntohll (ism->device);
364 ino = GNUNET_ntohll (ism->inode);
365 ism = (const struct IndexStartMessage*) message;
366 slen = strlen (fn) + 1;
367 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
368 ii->filename = (const char*) &ii[1];
369 memcpy (&ii[1], fn, slen);
370 ii->file_id = ism->file_id;
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Received `%s' message for file `%s'\n",
378 ii->tc = GNUNET_SERVER_transmit_context_create (client);
383 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
389 /* fast validation OK! */
390 signal_index_ok (ii);
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
397 (unsigned long long) ino,
398 (unsigned long long) myino,
400 (unsigned int) mydev);
402 /* slow validation, need to hash full file (again) */
403 ii->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
409 hash_for_index_val (ii, NULL);
415 * Handle INDEX_LIST_GET-message.
418 * @param client identification of the client
419 * @param message the actual message
422 GNUNET_FS_handle_index_list_get (void *cls,
423 struct GNUNET_SERVER_Client *client,
424 const struct GNUNET_MessageHeader *message)
426 struct GNUNET_SERVER_TransmitContext *tc;
427 struct IndexInfoMessage *iim;
428 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
431 struct IndexInfo *pos;
433 tc = GNUNET_SERVER_transmit_context_create (client);
434 iim = (struct IndexInfoMessage*) buf;
439 slen = strlen (fn) + 1;
440 if (slen + sizeof (struct IndexInfoMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
445 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
446 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
448 iim->file_id = pos->file_id;
449 memcpy (&iim[1], fn, slen);
450 GNUNET_SERVER_transmit_context_append_message (tc,
454 GNUNET_SERVER_transmit_context_append_data (tc,
456 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
457 GNUNET_SERVER_transmit_context_run (tc,
458 GNUNET_TIME_UNIT_MINUTES);
463 * Handle UNINDEX-message.
466 * @param client identification of the client
467 * @param message the actual message
470 GNUNET_FS_handle_unindex (void *cls,
471 struct GNUNET_SERVER_Client *client,
472 const struct GNUNET_MessageHeader *message)
474 const struct UnindexMessage *um;
475 struct IndexInfo *pos;
476 struct IndexInfo *prev;
477 struct IndexInfo *next;
478 struct GNUNET_SERVER_TransmitContext *tc;
481 um = (const struct UnindexMessage*) message;
482 if (0 != um->reserved)
485 GNUNET_SERVER_receive_done (client,
495 if (0 == memcmp (&pos->file_id,
497 sizeof (GNUNET_HashCode)))
500 indexed_files = next;
503 GNUNET_break (GNUNET_OK ==
504 GNUNET_CONTAINER_multihashmap_remove (ifm,
506 (void*) pos->filename));
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "Client requested unindexing of file `%s': %s\n",
519 GNUNET_h2s (&um->file_id),
520 found ? "found" : "not found");
522 if (GNUNET_YES == found)
524 tc = GNUNET_SERVER_transmit_context_create (client);
525 GNUNET_SERVER_transmit_context_append_data (tc,
527 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
528 GNUNET_SERVER_transmit_context_run (tc,
529 GNUNET_TIME_UNIT_MINUTES);
536 * Continuation called from datastore's remove
540 * @param success did the deletion work?
541 * @param msg error message
544 remove_cont (void *cls,
548 if (GNUNET_OK != success)
549 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
550 _("Failed to delete bogus block: %s\n"),
556 * We've received an on-demand encoded block from the datastore.
557 * Attempt to do on-demand encoding and (if successful), call the
558 * continuation with the resulting block. On error, clean up and ask
559 * the datastore for more results.
561 * @param key key for the content
562 * @param size number of bytes in data
563 * @param data content stored
564 * @param type type of the content
565 * @param priority priority of the content
566 * @param anonymity anonymity-level for the content
567 * @param expiration expiration time for the content
568 * @param uid unique identifier for the datum;
569 * maybe 0 if no unique identifier is available
570 * @param cont function to call with the actual block (at most once, on success)
571 * @param cont_cls closure for cont
572 * @return GNUNET_OK on success
575 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
578 enum GNUNET_BLOCK_Type type,
581 struct GNUNET_TIME_Absolute
582 expiration, uint64_t uid,
583 GNUNET_DATASTORE_DatumProcessor cont,
586 const struct OnDemandBlock *odb;
587 GNUNET_HashCode nkey;
588 struct GNUNET_CRYPTO_AesSessionKey skey;
589 struct GNUNET_CRYPTO_AesInitializationVector iv;
590 GNUNET_HashCode query;
592 char ndata[DBLOCK_SIZE];
593 char edata[DBLOCK_SIZE];
595 struct GNUNET_DISK_FileHandle *fh;
598 if (size != sizeof (struct OnDemandBlock))
601 GNUNET_DATASTORE_remove (dsh,
606 GNUNET_TIME_UNIT_FOREVER_REL,
609 return GNUNET_SYSERR;
611 odb = (const struct OnDemandBlock*) data;
612 off = GNUNET_ntohll (odb->offset);
613 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
617 (NULL == (fh = GNUNET_DISK_file_open (fn,
618 GNUNET_DISK_OPEN_READ,
619 GNUNET_DISK_PERM_NONE))) ||
621 GNUNET_DISK_file_seek (fh,
623 GNUNET_DISK_SEEK_SET)) ||
625 (nsize = GNUNET_DISK_file_read (fh,
629 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
630 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
631 GNUNET_h2s (&odb->file_id),
633 (unsigned long long) off,
634 (fn == NULL) ? _("not indexed") : STRERROR (errno));
636 GNUNET_DISK_file_close (fh);
637 GNUNET_DATASTORE_remove (dsh,
642 GNUNET_TIME_UNIT_FOREVER_REL,
645 return GNUNET_SYSERR;
647 GNUNET_DISK_file_close (fh);
648 GNUNET_CRYPTO_hash (ndata,
651 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
652 GNUNET_CRYPTO_aes_encrypt (ndata,
657 GNUNET_CRYPTO_hash (edata,
660 if (0 != memcmp (&query,
662 sizeof (GNUNET_HashCode)))
664 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
665 _("Indexed file `%s' changed at offset %llu\n"),
667 (unsigned long long) off);
668 GNUNET_DATASTORE_remove (dsh,
673 GNUNET_TIME_UNIT_FOREVER_REL,
676 return GNUNET_SYSERR;
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "On-demand encoded block for query `%s'\n",
687 GNUNET_BLOCK_TYPE_FS_DBLOCK,
697 * Shutdown the module.
700 GNUNET_FS_indexing_done ()
702 struct IndexInfo *pos;
704 GNUNET_CONTAINER_multihashmap_destroy (ifm);
706 while (NULL != (pos = indexed_files))
708 indexed_files = pos->next;
709 if (pos->fhc != NULL)
710 GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
718 * Initialize the indexing submodule.
720 * @param c configuration to use
721 * @param d datastore to use
724 GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
725 struct GNUNET_DATASTORE_Handle *d)
729 ifm = GNUNET_CONTAINER_multihashmap_create (128);
734 /* end of gnunet-service-fs_indexing.c */