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 * - consider doing GNUNET_CRYPTO_hash_file_cancel on active indexing
28 * jobs during shutdown (currently, shutdown will only happen after
29 * all of those are done, not sure if this is good or bad)
33 #include "gnunet_core_service.h"
34 #include "gnunet_datastore_service.h"
35 #include "gnunet_peer_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_signatures.h"
38 #include "gnunet_util_lib.h"
39 #include "gnunet-service-fs_drq.h"
40 #include "gnunet-service-fs_indexing.h"
43 #define DEBUG_FS GNUNET_NO
46 * In-memory information about indexed files (also available
53 * This is a linked list.
55 struct IndexInfo *next;
58 * Name of the indexed file. Memory allocated
59 * at the end of this struct (do not free).
64 * Context for transmitting confirmation to client,
65 * NULL if we've done this already.
67 struct GNUNET_SERVER_TransmitContext *tc;
70 * Context for hashing of the file.
72 struct GNUNET_CRYPTO_FileHashContext *fhc;
75 * Hash of the contents of the file.
77 GNUNET_HashCode file_id;
83 * Linked list of indexed files.
85 static struct IndexInfo *indexed_files;
88 * Maps hash over content of indexed files to the respective filename.
89 * The filenames are pointers into the indexed_files linked list and
90 * do not need to be freed.
92 static struct GNUNET_CONTAINER_MultiHashMap *ifm;
97 static struct GNUNET_SCHEDULER_Handle *sched;
102 static const struct GNUNET_CONFIGURATION_Handle *cfg;
105 * Datastore handle. Created and destroyed by code in
106 * gnunet-service-fs (this is an alias).
108 static struct GNUNET_DATASTORE_Handle *dsh;
112 * Write the current index information list to disk.
117 struct GNUNET_BIO_WriteHandle *wh;
119 struct IndexInfo *pos;
122 GNUNET_CONFIGURATION_get_value_filename (cfg,
127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
128 _("Configuration option `%s' in section `%s' missing.\n"),
133 wh = GNUNET_BIO_write_open (fn);
136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
137 _("Could not open `%s'.\n"),
146 GNUNET_BIO_write (wh,
148 sizeof (GNUNET_HashCode))) ||
150 GNUNET_BIO_write_string (wh,
156 GNUNET_BIO_write_close (wh))
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
159 _("Error writing `%s'.\n"),
169 * Read index information from disk.
174 struct GNUNET_BIO_ReadHandle *rh;
176 struct IndexInfo *pos;
183 GNUNET_CONFIGURATION_get_value_filename (cfg,
188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
189 _("Configuration option `%s' in section `%s' missing.\n"),
194 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
196 /* no index info yet */
200 rh = GNUNET_BIO_read_open (fn);
203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
204 _("Could not open `%s'.\n"),
209 while ( (GNUNET_OK ==
211 "Hash of indexed file",
213 sizeof (GNUNET_HashCode))) &&
215 GNUNET_BIO_read_string (rh,
216 "Name of indexed file",
220 slen = strlen (fname) + 1;
221 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
223 pos->filename = (const char *) &pos[1];
224 memcpy (&pos[1], fname, slen);
226 GNUNET_CONTAINER_multihashmap_put (ifm,
228 (void*) pos->filename,
229 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
235 pos->next = indexed_files;
241 GNUNET_BIO_read_close (rh, &emsg))
248 * We've validated the hash of the file we're about to index. Signal
249 * success to the client and update our internal data structures.
251 * @param ii the index info entry for the request
254 signal_index_ok (struct IndexInfo *ii)
257 GNUNET_CONTAINER_multihashmap_put (ifm,
259 (void*) ii->filename,
260 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
262 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
263 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
265 (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
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);
275 ii->next = indexed_files;
278 GNUNET_SERVER_transmit_context_append_data (ii->tc,
280 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
281 GNUNET_SERVER_transmit_context_run (ii->tc,
282 GNUNET_TIME_UNIT_MINUTES);
288 * Function called once the hash computation over an
289 * indexed file has completed.
291 * @param cls closure, our publishing context
292 * @param res resulting hash, NULL on error
295 hash_for_index_val (void *cls,
296 const GNUNET_HashCode *
299 struct IndexInfo *ii = cls;
302 if ( (res == NULL) ||
305 sizeof(GNUNET_HashCode))) )
307 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
308 _("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 GNUNET_h2s (&ii->file_id));
316 GNUNET_SERVER_transmit_context_append_data (ii->tc,
318 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
319 GNUNET_SERVER_transmit_context_run (ii->tc,
320 GNUNET_TIME_UNIT_MINUTES);
324 signal_index_ok (ii);
329 * Handle INDEX_START-message.
332 * @param client identification of the client
333 * @param message the actual message
336 GNUNET_FS_handle_index_start (void *cls,
337 struct GNUNET_SERVER_Client *client,
338 const struct GNUNET_MessageHeader *message)
340 const struct IndexStartMessage *ism;
343 struct IndexInfo *ii;
350 msize = ntohs(message->size);
351 if ( (msize <= sizeof (struct IndexStartMessage)) ||
352 ( ((const char *)message)[msize-1] != '\0') )
355 GNUNET_SERVER_receive_done (client,
359 ism = (const struct IndexStartMessage*) message;
360 fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
363 GNUNET_SERVER_receive_done (client,
367 dev = ntohl (ism->device);
368 ino = GNUNET_ntohll (ism->inode);
369 ism = (const struct IndexStartMessage*) message;
370 slen = strlen (fn) + 1;
371 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
372 ii->filename = (const char*) &ii[1];
373 memcpy (&ii[1], fn, slen);
374 ii->file_id = ism->file_id;
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Received `%s' message for file `%s'\n",
382 ii->tc = GNUNET_SERVER_transmit_context_create (client);
387 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
393 /* fast validation OK! */
394 signal_index_ok (ii);
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
401 (unsigned long long) ino,
402 (unsigned long long) myino,
404 (unsigned int) mydev);
406 /* slow validation, need to hash full file (again) */
407 ii->fhc = GNUNET_CRYPTO_hash_file (sched,
408 GNUNET_SCHEDULER_PRIORITY_IDLE,
414 hash_for_index_val (ii, NULL);
420 * Handle INDEX_LIST_GET-message.
423 * @param client identification of the client
424 * @param message the actual message
427 GNUNET_FS_handle_index_list_get (void *cls,
428 struct GNUNET_SERVER_Client *client,
429 const struct GNUNET_MessageHeader *message)
431 struct GNUNET_SERVER_TransmitContext *tc;
432 struct IndexInfoMessage *iim;
433 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
436 struct IndexInfo *pos;
438 tc = GNUNET_SERVER_transmit_context_create (client);
439 iim = (struct IndexInfoMessage*) buf;
444 slen = strlen (fn) + 1;
445 if (slen + sizeof (struct IndexInfoMessage) >
446 GNUNET_SERVER_MAX_MESSAGE_SIZE)
451 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
452 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
454 iim->file_id = pos->file_id;
455 memcpy (&iim[1], fn, slen);
456 GNUNET_SERVER_transmit_context_append_message (tc,
460 GNUNET_SERVER_transmit_context_append_data (tc,
462 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
463 GNUNET_SERVER_transmit_context_run (tc,
464 GNUNET_TIME_UNIT_MINUTES);
469 * Handle UNINDEX-message.
472 * @param client identification of the client
473 * @param message the actual message
476 GNUNET_FS_handle_unindex (void *cls,
477 struct GNUNET_SERVER_Client *client,
478 const struct GNUNET_MessageHeader *message)
480 const struct UnindexMessage *um;
481 struct IndexInfo *pos;
482 struct IndexInfo *prev;
483 struct IndexInfo *next;
484 struct GNUNET_SERVER_TransmitContext *tc;
487 um = (const struct UnindexMessage*) message;
494 if (0 == memcmp (&pos->file_id,
496 sizeof (GNUNET_HashCode)))
499 indexed_files = next;
502 GNUNET_break (GNUNET_OK ==
503 GNUNET_CONTAINER_multihashmap_remove (ifm,
505 (void*) pos->filename));
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517 "Client requested unindexing of file `%s': %s\n",
518 GNUNET_h2s (&um->file_id),
519 found ? "found" : "not found");
521 if (GNUNET_YES == found)
523 tc = GNUNET_SERVER_transmit_context_create (client);
524 GNUNET_SERVER_transmit_context_append_data (tc,
526 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
527 GNUNET_SERVER_transmit_context_run (tc,
528 GNUNET_TIME_UNIT_MINUTES);
535 * Continuation called from datastore's remove
539 * @param success did the deletion work?
540 * @param msg error message
543 remove_cont (void *cls,
547 if (GNUNET_OK != success)
548 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
549 _("Failed to delete bogus block: %s\n"),
555 * We've received an on-demand encoded block from the datastore.
556 * Attempt to do on-demand encoding and (if successful), call the
557 * continuation with the resulting block. On error, clean up and ask
558 * the datastore for more results.
560 * @param key key for the content
561 * @param size number of bytes in data
562 * @param data content stored
563 * @param type type of the content
564 * @param priority priority of the content
565 * @param anonymity anonymity-level for the content
566 * @param expiration expiration time for the content
567 * @param uid unique identifier for the datum;
568 * maybe 0 if no unique identifier is available
569 * @param cont function to call with the actual block (at most once, on success)
570 * @param cont_cls closure for cont
571 * @return GNUNET_OK on success
574 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
577 enum GNUNET_BLOCK_Type type,
580 struct GNUNET_TIME_Absolute
581 expiration, uint64_t uid,
582 GNUNET_DATASTORE_Iterator cont,
585 const struct OnDemandBlock *odb;
586 GNUNET_HashCode nkey;
587 struct GNUNET_CRYPTO_AesSessionKey skey;
588 struct GNUNET_CRYPTO_AesInitializationVector iv;
589 GNUNET_HashCode query;
591 char ndata[DBLOCK_SIZE];
592 char edata[DBLOCK_SIZE];
594 struct GNUNET_DISK_FileHandle *fh;
597 if (size != sizeof (struct OnDemandBlock))
600 GNUNET_DATASTORE_remove (dsh,
605 GNUNET_TIME_UNIT_FOREVER_REL,
608 return GNUNET_SYSERR;
610 odb = (const struct OnDemandBlock*) data;
611 off = GNUNET_ntohll (odb->offset);
612 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
616 (NULL == (fh = GNUNET_DISK_file_open (fn,
617 GNUNET_DISK_OPEN_READ,
618 GNUNET_DISK_PERM_NONE))) ||
620 GNUNET_DISK_file_seek (fh,
622 GNUNET_DISK_SEEK_SET)) ||
624 (nsize = GNUNET_DISK_file_read (fh,
628 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
629 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
630 GNUNET_h2s (&odb->file_id),
632 (unsigned long long) off,
633 (fn == NULL) ? _("not indexed") : STRERROR (errno));
635 GNUNET_DISK_file_close (fh);
636 GNUNET_DATASTORE_remove (dsh,
641 GNUNET_TIME_UNIT_FOREVER_REL,
644 return GNUNET_SYSERR;
646 GNUNET_DISK_file_close (fh);
647 GNUNET_CRYPTO_hash (ndata,
650 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
651 GNUNET_CRYPTO_aes_encrypt (ndata,
656 GNUNET_CRYPTO_hash (edata,
659 if (0 != memcmp (&query,
661 sizeof (GNUNET_HashCode)))
663 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
664 _("Indexed file `%s' changed at offset %llu\n"),
666 (unsigned long long) off);
667 GNUNET_DATASTORE_remove (dsh,
672 GNUNET_TIME_UNIT_FOREVER_REL,
675 return GNUNET_SYSERR;
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
679 "On-demand encoded block for query `%s'\n",
686 GNUNET_BLOCK_TYPE_DBLOCK,
696 * Task run during shutdown.
702 shutdown_task (void *cls,
703 const struct GNUNET_SCHEDULER_TaskContext *tc)
705 struct IndexInfo *pos;
707 GNUNET_CONTAINER_multihashmap_destroy (ifm);
709 while (NULL != (pos = indexed_files))
711 indexed_files = pos->next;
720 * Initialize the indexing submodule.
722 * @param s scheduler to use
723 * @param c configuration to use
724 * @param d datastore to use
727 GNUNET_FS_indexing_init (struct GNUNET_SCHEDULER_Handle *s,
728 const struct GNUNET_CONFIGURATION_Handle *c,
729 struct GNUNET_DATASTORE_Handle *d)
734 ifm = GNUNET_CONTAINER_multihashmap_create (128);
735 GNUNET_SCHEDULER_add_delayed (sched,
736 GNUNET_TIME_UNIT_FOREVER_REL,
743 /* end of gnunet-service-fs_indexing.c */