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;
106 * Write the current index information list to disk.
111 struct GNUNET_BIO_WriteHandle *wh;
113 struct IndexInfo *pos;
116 GNUNET_CONFIGURATION_get_value_filename (cfg,
121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
122 _("Configuration option `%s' in section `%s' missing.\n"),
127 wh = GNUNET_BIO_write_open (fn);
130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
131 _("Could not open `%s'.\n"),
140 GNUNET_BIO_write (wh,
142 sizeof (GNUNET_HashCode))) ||
144 GNUNET_BIO_write_string (wh,
150 GNUNET_BIO_write_close (wh))
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
153 _("Error writing `%s'.\n"),
163 * Read index information from disk.
168 struct GNUNET_BIO_ReadHandle *rh;
170 struct IndexInfo *pos;
177 GNUNET_CONFIGURATION_get_value_filename (cfg,
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
183 _("Configuration option `%s' in section `%s' missing.\n"),
188 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
190 /* no index info yet */
194 rh = GNUNET_BIO_read_open (fn);
197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
198 _("Could not open `%s'.\n"),
204 while ( (GNUNET_OK ==
206 "Hash of indexed file",
208 sizeof (GNUNET_HashCode))) &&
210 GNUNET_BIO_read_string (rh,
211 "Name of indexed file",
215 slen = strlen (fname) + 1;
216 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
218 pos->filename = (const char *) &pos[1];
219 memcpy (&pos[1], fname, slen);
221 GNUNET_CONTAINER_multihashmap_put (ifm,
223 (void*) pos->filename,
224 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
230 pos->next = indexed_files;
236 GNUNET_BIO_read_close (rh, &emsg))
243 * We've validated the hash of the file we're about to index. Signal
244 * success to the client and update our internal data structures.
246 * @param ii the index info entry for the request
249 signal_index_ok (struct IndexInfo *ii)
252 GNUNET_CONTAINER_multihashmap_put (ifm,
254 (void*) ii->filename,
255 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
257 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
258 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
260 (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
262 GNUNET_SERVER_transmit_context_append_data (ii->tc,
264 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
265 GNUNET_SERVER_transmit_context_run (ii->tc,
266 GNUNET_TIME_UNIT_MINUTES);
270 ii->next = indexed_files;
273 GNUNET_SERVER_transmit_context_append_data (ii->tc,
275 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
276 GNUNET_SERVER_transmit_context_run (ii->tc,
277 GNUNET_TIME_UNIT_MINUTES);
283 * Function called once the hash computation over an
284 * indexed file has completed.
286 * @param cls closure, our publishing context
287 * @param res resulting hash, NULL on error
290 hash_for_index_val (void *cls,
291 const GNUNET_HashCode *
294 struct IndexInfo *ii = cls;
297 if ( (res == NULL) ||
300 sizeof(GNUNET_HashCode))) )
302 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
303 _("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309 GNUNET_h2s (&ii->file_id));
311 GNUNET_SERVER_transmit_context_append_data (ii->tc,
313 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
314 GNUNET_SERVER_transmit_context_run (ii->tc,
315 GNUNET_TIME_UNIT_MINUTES);
319 signal_index_ok (ii);
324 * Handle INDEX_START-message.
327 * @param client identification of the client
328 * @param message the actual message
331 GNUNET_FS_handle_index_start (void *cls,
332 struct GNUNET_SERVER_Client *client,
333 const struct GNUNET_MessageHeader *message)
335 const struct IndexStartMessage *ism;
338 struct IndexInfo *ii;
345 msize = ntohs(message->size);
346 if ( (msize <= sizeof (struct IndexStartMessage)) ||
347 ( ((const char *)message)[msize-1] != '\0') )
350 GNUNET_SERVER_receive_done (client,
354 ism = (const struct IndexStartMessage*) message;
355 fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
356 dev = ntohl (ism->device);
357 ino = GNUNET_ntohll (ism->inode);
358 ism = (const struct IndexStartMessage*) message;
359 slen = strlen (fn) + 1;
360 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
361 ii->filename = (const char*) &ii[1];
362 memcpy (&ii[1], fn, slen);
363 ii->file_id = ism->file_id;
364 ii->tc = GNUNET_SERVER_transmit_context_create (client);
369 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
375 /* fast validation OK! */
376 signal_index_ok (ii);
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
383 (unsigned long long) ino,
384 (unsigned long long) myino,
386 (unsigned int) mydev);
388 /* slow validation, need to hash full file (again) */
389 ii->fhc = GNUNET_CRYPTO_hash_file (sched,
390 GNUNET_SCHEDULER_PRIORITY_IDLE,
396 hash_for_index_val (ii, NULL);
402 * Handle INDEX_LIST_GET-message.
405 * @param client identification of the client
406 * @param message the actual message
409 GNUNET_FS_handle_index_list_get (void *cls,
410 struct GNUNET_SERVER_Client *client,
411 const struct GNUNET_MessageHeader *message)
413 struct GNUNET_SERVER_TransmitContext *tc;
414 struct IndexInfoMessage *iim;
415 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
418 struct IndexInfo *pos;
420 tc = GNUNET_SERVER_transmit_context_create (client);
421 iim = (struct IndexInfoMessage*) buf;
426 slen = strlen (fn) + 1;
427 if (slen + sizeof (struct IndexInfoMessage) >
428 GNUNET_SERVER_MAX_MESSAGE_SIZE)
433 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
434 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
436 iim->file_id = pos->file_id;
437 memcpy (&iim[1], fn, slen);
438 GNUNET_SERVER_transmit_context_append_message (tc,
442 GNUNET_SERVER_transmit_context_append_data (tc,
444 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
445 GNUNET_SERVER_transmit_context_run (tc,
446 GNUNET_TIME_UNIT_MINUTES);
451 * Handle UNINDEX-message.
454 * @param client identification of the client
455 * @param message the actual message
458 GNUNET_FS_handle_unindex (void *cls,
459 struct GNUNET_SERVER_Client *client,
460 const struct GNUNET_MessageHeader *message)
462 const struct UnindexMessage *um;
463 struct IndexInfo *pos;
464 struct IndexInfo *prev;
465 struct IndexInfo *next;
466 struct GNUNET_SERVER_TransmitContext *tc;
469 um = (const struct UnindexMessage*) message;
476 if (0 == memcmp (&pos->file_id,
478 sizeof (GNUNET_HashCode)))
481 indexed_files = next;
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "Client requested unindexing of file `%s': %s\n",
496 GNUNET_h2s (&um->file_id),
497 found ? "found" : "not found");
499 if (GNUNET_YES == found)
501 tc = GNUNET_SERVER_transmit_context_create (client);
502 GNUNET_SERVER_transmit_context_append_data (tc,
504 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
505 GNUNET_SERVER_transmit_context_run (tc,
506 GNUNET_TIME_UNIT_MINUTES);
513 * Continuation called from datastore's remove
517 * @param success did the deletion work?
518 * @param msg error message
521 remove_cont (void *cls,
525 if (GNUNET_OK != success)
526 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
527 _("Failed to delete bogus block: %s\n"),
533 * We've received an on-demand encoded block from the datastore.
534 * Attempt to do on-demand encoding and (if successful), call the
535 * continuation with the resulting block. On error, clean up and ask
536 * the datastore for more results.
538 * @param key key for the content
539 * @param size number of bytes in data
540 * @param data content stored
541 * @param type type of the content
542 * @param priority priority of the content
543 * @param anonymity anonymity-level for the content
544 * @param expiration expiration time for the content
545 * @param uid unique identifier for the datum;
546 * maybe 0 if no unique identifier is available
547 * @param cont function to call with the actual block (at most once, on success)
548 * @param cont_cls closure for cont
549 * @return GNUNET_OK on success
552 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
555 enum GNUNET_BLOCK_Type type,
558 struct GNUNET_TIME_Absolute
559 expiration, uint64_t uid,
560 GNUNET_DATASTORE_Iterator cont,
563 const struct OnDemandBlock *odb;
564 GNUNET_HashCode nkey;
565 struct GNUNET_CRYPTO_AesSessionKey skey;
566 struct GNUNET_CRYPTO_AesInitializationVector iv;
567 GNUNET_HashCode query;
569 char ndata[DBLOCK_SIZE];
570 char edata[DBLOCK_SIZE];
572 struct GNUNET_DISK_FileHandle *fh;
575 if (size != sizeof (struct OnDemandBlock))
578 GNUNET_FS_drq_remove (key,
583 GNUNET_TIME_UNIT_FOREVER_REL);
584 return GNUNET_SYSERR;
586 odb = (const struct OnDemandBlock*) data;
587 off = GNUNET_ntohll (odb->offset);
588 fn = (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
592 (NULL == (fh = GNUNET_DISK_file_open (fn,
593 GNUNET_DISK_OPEN_READ,
594 GNUNET_DISK_PERM_NONE))) ||
596 GNUNET_DISK_file_seek (fh,
598 GNUNET_DISK_SEEK_SET)) ||
600 (nsize = GNUNET_DISK_file_read (fh,
604 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
605 _("Could not access indexed file `%s' at offset %llu: %s\n"),
606 GNUNET_h2s (&odb->file_id),
607 (unsigned long long) off,
610 GNUNET_DISK_file_close (fh);
611 GNUNET_FS_drq_remove (key,
616 GNUNET_TIME_UNIT_FOREVER_REL);
617 return GNUNET_SYSERR;
619 GNUNET_DISK_file_close (fh);
620 GNUNET_CRYPTO_hash (ndata,
623 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
624 GNUNET_CRYPTO_aes_encrypt (ndata,
629 GNUNET_CRYPTO_hash (edata,
632 if (0 != memcmp (&query,
634 sizeof (GNUNET_HashCode)))
636 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
637 _("Indexed file `%s' changed at offset %llu\n"),
639 (unsigned long long) off);
640 GNUNET_FS_drq_remove (key,
645 GNUNET_TIME_UNIT_FOREVER_REL);
646 return GNUNET_SYSERR;
649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
650 "On-demand encoded block for query `%s'\n",
657 GNUNET_BLOCK_TYPE_DBLOCK,
667 * Task run during shutdown.
673 shutdown_task (void *cls,
674 const struct GNUNET_SCHEDULER_TaskContext *tc)
676 struct IndexInfo *pos;
678 GNUNET_CONTAINER_multihashmap_destroy (ifm);
680 while (NULL != (pos = indexed_files))
682 indexed_files = pos->next;
691 * Initialize the indexing submodule.
693 * @param s scheduler to use
694 * @param c configuration to use
697 GNUNET_FS_indexing_init (struct GNUNET_SCHEDULER_Handle *s,
698 const struct GNUNET_CONFIGURATION_Handle *c)
702 ifm = GNUNET_CONTAINER_multihashmap_create (128);
703 GNUNET_SCHEDULER_add_delayed (sched,
704 GNUNET_TIME_UNIT_FOREVER_REL,
711 /* end of gnunet-service-fs_indexing.c */