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_YES
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"),
210 while ( (GNUNET_OK ==
212 "Hash of indexed file",
214 sizeof (GNUNET_HashCode))) &&
216 GNUNET_BIO_read_string (rh,
217 "Name of indexed file",
221 slen = strlen (fname) + 1;
222 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
224 pos->filename = (const char *) &pos[1];
225 memcpy (&pos[1], fname, slen);
227 GNUNET_CONTAINER_multihashmap_put (ifm,
229 (void*) pos->filename,
230 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
236 pos->next = indexed_files;
242 GNUNET_BIO_read_close (rh, &emsg))
249 * We've validated the hash of the file we're about to index. Signal
250 * success to the client and update our internal data structures.
252 * @param ii the index info entry for the request
255 signal_index_ok (struct IndexInfo *ii)
258 GNUNET_CONTAINER_multihashmap_put (ifm,
260 (void*) ii->filename,
261 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
263 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
264 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
266 (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
268 GNUNET_SERVER_transmit_context_append_data (ii->tc,
270 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
271 GNUNET_SERVER_transmit_context_run (ii->tc,
272 GNUNET_TIME_UNIT_MINUTES);
276 ii->next = indexed_files;
279 GNUNET_SERVER_transmit_context_append_data (ii->tc,
281 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
282 GNUNET_SERVER_transmit_context_run (ii->tc,
283 GNUNET_TIME_UNIT_MINUTES);
289 * Function called once the hash computation over an
290 * indexed file has completed.
292 * @param cls closure, our publishing context
293 * @param res resulting hash, NULL on error
296 hash_for_index_val (void *cls,
297 const GNUNET_HashCode *
300 struct IndexInfo *ii = cls;
303 if ( (res == NULL) ||
306 sizeof(GNUNET_HashCode))) )
308 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
309 _("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 GNUNET_h2s (&ii->file_id));
317 GNUNET_SERVER_transmit_context_append_data (ii->tc,
319 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
320 GNUNET_SERVER_transmit_context_run (ii->tc,
321 GNUNET_TIME_UNIT_MINUTES);
325 signal_index_ok (ii);
330 * Handle INDEX_START-message.
333 * @param client identification of the client
334 * @param message the actual message
337 GNUNET_FS_handle_index_start (void *cls,
338 struct GNUNET_SERVER_Client *client,
339 const struct GNUNET_MessageHeader *message)
341 const struct IndexStartMessage *ism;
344 struct IndexInfo *ii;
351 msize = ntohs(message->size);
352 if ( (msize <= sizeof (struct IndexStartMessage)) ||
353 ( ((const char *)message)[msize-1] != '\0') )
356 GNUNET_SERVER_receive_done (client,
360 ism = (const struct IndexStartMessage*) message;
361 fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
362 dev = ntohl (ism->device);
363 ino = GNUNET_ntohll (ism->inode);
364 ism = (const struct IndexStartMessage*) message;
365 slen = strlen (fn) + 1;
366 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
367 ii->filename = (const char*) &ii[1];
368 memcpy (&ii[1], fn, slen);
369 ii->file_id = ism->file_id;
370 ii->tc = GNUNET_SERVER_transmit_context_create (client);
375 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
381 /* fast validation OK! */
382 signal_index_ok (ii);
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
389 (unsigned long long) ino,
390 (unsigned long long) myino,
392 (unsigned int) mydev);
394 /* slow validation, need to hash full file (again) */
395 ii->fhc = GNUNET_CRYPTO_hash_file (sched,
396 GNUNET_SCHEDULER_PRIORITY_IDLE,
402 hash_for_index_val (ii, NULL);
408 * Handle INDEX_LIST_GET-message.
411 * @param client identification of the client
412 * @param message the actual message
415 GNUNET_FS_handle_index_list_get (void *cls,
416 struct GNUNET_SERVER_Client *client,
417 const struct GNUNET_MessageHeader *message)
419 struct GNUNET_SERVER_TransmitContext *tc;
420 struct IndexInfoMessage *iim;
421 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
424 struct IndexInfo *pos;
426 tc = GNUNET_SERVER_transmit_context_create (client);
427 iim = (struct IndexInfoMessage*) buf;
432 slen = strlen (fn) + 1;
433 if (slen + sizeof (struct IndexInfoMessage) >
434 GNUNET_SERVER_MAX_MESSAGE_SIZE)
439 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
440 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
442 iim->file_id = pos->file_id;
443 memcpy (&iim[1], fn, slen);
444 GNUNET_SERVER_transmit_context_append_message (tc,
448 GNUNET_SERVER_transmit_context_append_data (tc,
450 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
451 GNUNET_SERVER_transmit_context_run (tc,
452 GNUNET_TIME_UNIT_MINUTES);
457 * Handle UNINDEX-message.
460 * @param client identification of the client
461 * @param message the actual message
464 GNUNET_FS_handle_unindex (void *cls,
465 struct GNUNET_SERVER_Client *client,
466 const struct GNUNET_MessageHeader *message)
468 const struct UnindexMessage *um;
469 struct IndexInfo *pos;
470 struct IndexInfo *prev;
471 struct IndexInfo *next;
472 struct GNUNET_SERVER_TransmitContext *tc;
475 um = (const struct UnindexMessage*) message;
482 if (0 == memcmp (&pos->file_id,
484 sizeof (GNUNET_HashCode)))
487 indexed_files = next;
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Client requested unindexing of file `%s': %s\n",
502 GNUNET_h2s (&um->file_id),
503 found ? "found" : "not found");
505 if (GNUNET_YES == found)
507 tc = GNUNET_SERVER_transmit_context_create (client);
508 GNUNET_SERVER_transmit_context_append_data (tc,
510 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
511 GNUNET_SERVER_transmit_context_run (tc,
512 GNUNET_TIME_UNIT_MINUTES);
519 * Continuation called from datastore's remove
523 * @param success did the deletion work?
524 * @param msg error message
527 remove_cont (void *cls,
531 if (GNUNET_OK != success)
532 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
533 _("Failed to delete bogus block: %s\n"),
539 * We've received an on-demand encoded block from the datastore.
540 * Attempt to do on-demand encoding and (if successful), call the
541 * continuation with the resulting block. On error, clean up and ask
542 * the datastore for more results.
544 * @param key key for the content
545 * @param size number of bytes in data
546 * @param data content stored
547 * @param type type of the content
548 * @param priority priority of the content
549 * @param anonymity anonymity-level for the content
550 * @param expiration expiration time for the content
551 * @param uid unique identifier for the datum;
552 * maybe 0 if no unique identifier is available
553 * @param cont function to call with the actual block (at most once, on success)
554 * @param cont_cls closure for cont
555 * @return GNUNET_OK on success
558 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
561 enum GNUNET_BLOCK_Type type,
564 struct GNUNET_TIME_Absolute
565 expiration, uint64_t uid,
566 GNUNET_DATASTORE_Iterator cont,
569 const struct OnDemandBlock *odb;
570 GNUNET_HashCode nkey;
571 struct GNUNET_CRYPTO_AesSessionKey skey;
572 struct GNUNET_CRYPTO_AesInitializationVector iv;
573 GNUNET_HashCode query;
575 char ndata[DBLOCK_SIZE];
576 char edata[DBLOCK_SIZE];
578 struct GNUNET_DISK_FileHandle *fh;
581 if (size != sizeof (struct OnDemandBlock))
584 GNUNET_DATASTORE_remove (dsh,
589 GNUNET_TIME_UNIT_FOREVER_REL,
592 return GNUNET_SYSERR;
594 odb = (const struct OnDemandBlock*) data;
595 off = GNUNET_ntohll (odb->offset);
596 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
600 (NULL == (fh = GNUNET_DISK_file_open (fn,
601 GNUNET_DISK_OPEN_READ,
602 GNUNET_DISK_PERM_NONE))) ||
604 GNUNET_DISK_file_seek (fh,
606 GNUNET_DISK_SEEK_SET)) ||
608 (nsize = GNUNET_DISK_file_read (fh,
612 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
613 _("Could not access indexed file `%s' at offset %llu: %s\n"),
614 GNUNET_h2s (&odb->file_id),
615 (unsigned long long) off,
618 GNUNET_DISK_file_close (fh);
619 GNUNET_DATASTORE_remove (dsh,
624 GNUNET_TIME_UNIT_FOREVER_REL,
627 return GNUNET_SYSERR;
629 GNUNET_DISK_file_close (fh);
630 GNUNET_CRYPTO_hash (ndata,
633 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
634 GNUNET_CRYPTO_aes_encrypt (ndata,
639 GNUNET_CRYPTO_hash (edata,
642 if (0 != memcmp (&query,
644 sizeof (GNUNET_HashCode)))
646 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
647 _("Indexed file `%s' changed at offset %llu\n"),
649 (unsigned long long) off);
650 GNUNET_DATASTORE_remove (dsh,
655 GNUNET_TIME_UNIT_FOREVER_REL,
658 return GNUNET_SYSERR;
661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
662 "On-demand encoded block for query `%s'\n",
669 GNUNET_BLOCK_TYPE_DBLOCK,
679 * Task run during shutdown.
685 shutdown_task (void *cls,
686 const struct GNUNET_SCHEDULER_TaskContext *tc)
688 struct IndexInfo *pos;
690 GNUNET_CONTAINER_multihashmap_destroy (ifm);
692 while (NULL != (pos = indexed_files))
694 indexed_files = pos->next;
703 * Initialize the indexing submodule.
705 * @param s scheduler to use
706 * @param c configuration to use
707 * @param d datastore to use
710 GNUNET_FS_indexing_init (struct GNUNET_SCHEDULER_Handle *s,
711 const struct GNUNET_CONFIGURATION_Handle *c,
712 struct GNUNET_DATASTORE_Handle *d)
717 ifm = GNUNET_CONTAINER_multihashmap_create (128);
718 GNUNET_SCHEDULER_add_delayed (sched,
719 GNUNET_TIME_UNIT_FOREVER_REL,
726 /* end of gnunet-service-fs_indexing.c */