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_YES
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 static 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 already 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);
361 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
367 /* fast validation OK! */
368 signal_index_ok (ii);
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
374 (unsigned long long) ino,
375 (unsigned long long) myino,
377 (unsigned int) mydev);
379 /* slow validation, need to hash full file (again) */
380 GNUNET_CRYPTO_hash_file (sched,
381 GNUNET_SCHEDULER_PRIORITY_IDLE,
390 * Handle INDEX_LIST_GET-message.
393 * @param client identification of the client
394 * @param message the actual message
397 GNUNET_FS_handle_index_list_get (void *cls,
398 struct GNUNET_SERVER_Client *client,
399 const struct GNUNET_MessageHeader *message)
401 struct GNUNET_SERVER_TransmitContext *tc;
402 struct IndexInfoMessage *iim;
403 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
406 struct IndexInfo *pos;
408 tc = GNUNET_SERVER_transmit_context_create (client);
409 iim = (struct IndexInfoMessage*) buf;
414 slen = strlen (fn) + 1;
415 if (slen + sizeof (struct IndexInfoMessage) >
416 GNUNET_SERVER_MAX_MESSAGE_SIZE)
421 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
422 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
424 iim->file_id = pos->file_id;
425 memcpy (&iim[1], fn, slen);
426 GNUNET_SERVER_transmit_context_append_message (tc,
430 GNUNET_SERVER_transmit_context_append_data (tc,
432 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
433 GNUNET_SERVER_transmit_context_run (tc,
434 GNUNET_TIME_UNIT_MINUTES);
439 * Handle UNINDEX-message.
442 * @param client identification of the client
443 * @param message the actual message
446 GNUNET_FS_handle_unindex (void *cls,
447 struct GNUNET_SERVER_Client *client,
448 const struct GNUNET_MessageHeader *message)
450 const struct UnindexMessage *um;
451 struct IndexInfo *pos;
452 struct IndexInfo *prev;
453 struct IndexInfo *next;
454 struct GNUNET_SERVER_TransmitContext *tc;
457 um = (const struct UnindexMessage*) message;
464 if (0 == memcmp (&pos->file_id,
466 sizeof (GNUNET_HashCode)))
469 indexed_files = next;
482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
483 "Client requested unindexing of file `%s': %s\n",
484 GNUNET_h2s (&um->file_id),
485 found ? "found" : "not found");
487 if (GNUNET_YES == found)
489 tc = GNUNET_SERVER_transmit_context_create (client);
490 GNUNET_SERVER_transmit_context_append_data (tc,
492 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
493 GNUNET_SERVER_transmit_context_run (tc,
494 GNUNET_TIME_UNIT_MINUTES);
501 * Continuation called from datastore's remove
505 * @param success did the deletion work?
506 * @param msg error message
509 remove_cont (void *cls,
513 if (GNUNET_OK != success)
514 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
515 _("Failed to delete bogus block: %s\n"),
521 * We've received an on-demand encoded block from the datastore.
522 * Attempt to do on-demand encoding and (if successful), call the
523 * continuation with the resulting block. On error, clean up and ask
524 * the datastore for more results.
526 * @param key key for the content
527 * @param size number of bytes in data
528 * @param data content stored
529 * @param type type of the content
530 * @param priority priority of the content
531 * @param anonymity anonymity-level for the content
532 * @param expiration expiration time for the content
533 * @param uid unique identifier for the datum;
534 * maybe 0 if no unique identifier is available
535 * @param cont function to call with the actual block (at most once, on success)
536 * @param cont_cls closure for cont
537 * @return GNUNET_OK on success
540 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
546 struct GNUNET_TIME_Absolute
547 expiration, uint64_t uid,
548 GNUNET_DATASTORE_Iterator cont,
551 const struct OnDemandBlock *odb;
552 GNUNET_HashCode nkey;
553 struct GNUNET_CRYPTO_AesSessionKey skey;
554 struct GNUNET_CRYPTO_AesInitializationVector iv;
555 GNUNET_HashCode query;
557 char ndata[DBLOCK_SIZE];
558 char edata[DBLOCK_SIZE];
560 struct GNUNET_DISK_FileHandle *fh;
563 if (size != sizeof (struct OnDemandBlock))
566 GNUNET_FS_drq_remove (key,
571 GNUNET_TIME_UNIT_FOREVER_REL);
572 return GNUNET_SYSERR;
574 odb = (const struct OnDemandBlock*) data;
575 off = GNUNET_ntohll (odb->offset);
576 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
580 (NULL == (fh = GNUNET_DISK_file_open (fn,
581 GNUNET_DISK_OPEN_READ,
582 GNUNET_DISK_PERM_NONE))) ||
584 GNUNET_DISK_file_seek (fh,
586 GNUNET_DISK_SEEK_SET)) ||
588 (nsize = GNUNET_DISK_file_read (fh,
592 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
593 _("Could not access indexed file `%s' at offset %llu: %s\n"),
594 GNUNET_h2s (&odb->file_id),
595 (unsigned long long) off,
598 GNUNET_DISK_file_close (fh);
599 /* FIXME: if this happens often, we need
600 to remove the OnDemand block from the DS! */
601 return GNUNET_SYSERR;
603 GNUNET_DISK_file_close (fh);
604 GNUNET_CRYPTO_hash (ndata,
607 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
608 GNUNET_CRYPTO_aes_encrypt (ndata,
613 GNUNET_CRYPTO_hash (edata,
616 if (0 != memcmp (&query,
618 sizeof (GNUNET_HashCode)))
620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
621 _("Indexed file `%s' changed at offset %llu\n"),
623 (unsigned long long) off);
624 /* FIXME: if this happens often, we need
625 to remove the OnDemand block from the DS! */
626 return GNUNET_SYSERR;
629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
630 "On-demand encoded block for query `%s'\n",
637 GNUNET_DATASTORE_BLOCKTYPE_DBLOCK,
647 * Task run during shutdown.
653 shutdown_task (void *cls,
654 const struct GNUNET_SCHEDULER_TaskContext *tc)
656 struct IndexInfo *pos;
658 GNUNET_CONTAINER_multihashmap_destroy (ifm);
660 while (NULL != (pos = indexed_files))
662 indexed_files = pos->next;
671 * Initialize the indexing submodule.
673 * @param s scheduler to use
674 * @param c configuration to use
677 GNUNET_FS_indexing_init (struct GNUNET_SCHEDULER_Handle *s,
678 const struct GNUNET_CONFIGURATION_Handle *c)
682 ifm = GNUNET_CONTAINER_multihashmap_create (128);
683 GNUNET_SCHEDULER_add_delayed (sched,
684 GNUNET_TIME_UNIT_FOREVER_REL,
691 /* end of gnunet-service-fs_indexing.c */