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, "FS", "INDEXDB", &fn))
112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
113 _("Configuration option `%s' in section `%s' missing.\n"),
117 wh = GNUNET_BIO_write_open (fn);
120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
121 _("Could not open `%s'.\n"), fn);
129 GNUNET_BIO_write (wh, &pos->file_id, sizeof (GNUNET_HashCode))) ||
130 (GNUNET_OK != GNUNET_BIO_write_string (wh, pos->filename)))
134 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
137 _("Error writing `%s'.\n"), fn);
146 * Read index information from disk.
151 struct GNUNET_BIO_ReadHandle *rh;
153 struct IndexInfo *pos;
160 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
163 _("Configuration option `%s' in section `%s' missing.\n"),
167 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
169 /* no index info yet */
173 rh = GNUNET_BIO_read_open (fn);
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
177 _("Could not open `%s'.\n"), fn);
182 GNUNET_BIO_read (rh, "Hash of indexed file", &hc,
183 sizeof (GNUNET_HashCode))) &&
185 GNUNET_BIO_read_string (rh, "Name of indexed file", &fname,
186 1024 * 16)) && (fname != NULL))
188 slen = strlen (fname) + 1;
189 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
191 pos->filename = (const char *) &pos[1];
192 memcpy (&pos[1], fname, slen);
194 GNUNET_CONTAINER_multihashmap_put (ifm, &hc, (void *) pos->filename,
195 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
201 pos->next = indexed_files;
206 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
213 * We've validated the hash of the file we're about to index. Signal
214 * success to the client and update our internal data structures.
216 * @param ii the index info entry for the request
219 signal_index_ok (struct IndexInfo *ii)
222 GNUNET_CONTAINER_multihashmap_put (ifm, &ii->file_id,
223 (void *) ii->filename,
224 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
226 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
228 ("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
230 (const char *) GNUNET_CONTAINER_multihashmap_get (ifm,
233 GNUNET_SERVER_transmit_context_append_data (ii->tc, NULL, 0,
234 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
235 GNUNET_SERVER_transmit_context_run (ii->tc, GNUNET_TIME_UNIT_MINUTES);
239 ii->next = indexed_files;
242 GNUNET_SERVER_transmit_context_append_data (ii->tc, NULL, 0,
243 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
244 GNUNET_SERVER_transmit_context_run (ii->tc, GNUNET_TIME_UNIT_MINUTES);
250 * Function called once the hash computation over an
251 * indexed file has completed.
253 * @param cls closure, our publishing context
254 * @param res resulting hash, NULL on error
257 hash_for_index_val (void *cls, const GNUNET_HashCode * res)
259 struct IndexInfo *ii = cls;
263 (0 != memcmp (res, &ii->file_id, sizeof (GNUNET_HashCode))))
265 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
267 ("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
268 ii->filename, GNUNET_h2s (res));
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Wanted `%s'\n",
271 GNUNET_h2s (&ii->file_id));
273 GNUNET_SERVER_transmit_context_append_data (ii->tc, NULL, 0,
274 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
275 GNUNET_SERVER_transmit_context_run (ii->tc, GNUNET_TIME_UNIT_MINUTES);
279 signal_index_ok (ii);
284 * Handle INDEX_START-message.
287 * @param client identification of the client
288 * @param message the actual message
291 GNUNET_FS_handle_index_start (void *cls, struct GNUNET_SERVER_Client *client,
292 const struct GNUNET_MessageHeader *message)
294 const struct IndexStartMessage *ism;
297 struct IndexInfo *ii;
304 msize = ntohs (message->size);
305 if ((msize <= sizeof (struct IndexStartMessage)) ||
306 (((const char *) message)[msize - 1] != '\0'))
309 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
312 ism = (const struct IndexStartMessage *) message;
313 if (0 != ism->reserved)
316 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
319 fn = GNUNET_STRINGS_filename_expand ((const char *) &ism[1]);
322 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
325 dev = GNUNET_ntohll (ism->device);
326 ino = GNUNET_ntohll (ism->inode);
327 ism = (const struct IndexStartMessage *) message;
328 slen = strlen (fn) + 1;
329 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
330 ii->filename = (const char *) &ii[1];
331 memcpy (&ii[1], fn, slen);
332 ii->file_id = ism->file_id;
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message for file `%s'\n",
335 "START_INDEX", ii->filename);
338 ii->tc = GNUNET_SERVER_transmit_context_create (client);
341 if (((dev != 0) || (ino != 0)) &&
342 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn, &mydev, &myino)) &&
343 ((dev == mydev) && (ino == myino)))
345 /* fast validation OK! */
346 signal_index_ok (ii);
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
353 (unsigned long long) ino, (unsigned long long) myino,
354 (unsigned int) dev, (unsigned int) mydev);
356 /* slow validation, need to hash full file (again) */
358 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, fn,
359 HASHING_BLOCKSIZE, &hash_for_index_val, ii);
361 hash_for_index_val (ii, NULL);
367 * Handle INDEX_LIST_GET-message.
370 * @param client identification of the client
371 * @param message the actual message
374 GNUNET_FS_handle_index_list_get (void *cls, struct GNUNET_SERVER_Client *client,
375 const struct GNUNET_MessageHeader *message)
377 struct GNUNET_SERVER_TransmitContext *tc;
378 struct IndexInfoMessage *iim;
379 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
382 struct IndexInfo *pos;
384 tc = GNUNET_SERVER_transmit_context_create (client);
385 iim = (struct IndexInfoMessage *) buf;
390 slen = strlen (fn) + 1;
391 if (slen + sizeof (struct IndexInfoMessage) >=
392 GNUNET_SERVER_MAX_MESSAGE_SIZE)
397 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
398 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
400 iim->file_id = pos->file_id;
401 memcpy (&iim[1], fn, slen);
402 GNUNET_SERVER_transmit_context_append_message (tc, &iim->header);
405 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
406 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
407 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_MINUTES);
412 * Handle UNINDEX-message.
415 * @param client identification of the client
416 * @param message the actual message
419 GNUNET_FS_handle_unindex (void *cls, struct GNUNET_SERVER_Client *client,
420 const struct GNUNET_MessageHeader *message)
422 const struct UnindexMessage *um;
423 struct IndexInfo *pos;
424 struct IndexInfo *prev;
425 struct IndexInfo *next;
426 struct GNUNET_SERVER_TransmitContext *tc;
429 um = (const struct UnindexMessage *) message;
430 if (0 != um->reserved)
433 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
442 if (0 == memcmp (&pos->file_id, &um->file_id, sizeof (GNUNET_HashCode)))
445 indexed_files = next;
448 GNUNET_break (GNUNET_OK ==
449 GNUNET_CONTAINER_multihashmap_remove (ifm, &pos->file_id,
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 "Client requested unindexing of file `%s': %s\n",
464 GNUNET_h2s (&um->file_id), found ? "found" : "not found");
466 if (GNUNET_YES == found)
468 tc = GNUNET_SERVER_transmit_context_create (client);
469 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
470 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
471 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_MINUTES);
476 * Continuation called from datastore's remove
480 * @param success did the deletion work?
481 * @param msg error message
484 remove_cont (void *cls, int success, const char *msg)
486 if (GNUNET_OK != success)
487 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
488 _("Failed to delete bogus block: %s\n"), msg);
493 * We've received an on-demand encoded block from the datastore.
494 * Attempt to do on-demand encoding and (if successful), call the
495 * continuation with the resulting block. On error, clean up and ask
496 * the datastore for more results.
498 * @param key key for the content
499 * @param size number of bytes in data
500 * @param data content stored
501 * @param type type of the content
502 * @param priority priority of the content
503 * @param anonymity anonymity-level for the content
504 * @param expiration expiration time for the content
505 * @param uid unique identifier for the datum;
506 * maybe 0 if no unique identifier is available
507 * @param cont function to call with the actual block (at most once, on success)
508 * @param cont_cls closure for cont
509 * @return GNUNET_OK on success
512 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key, uint32_t size,
513 const void *data, enum GNUNET_BLOCK_Type type,
514 uint32_t priority, uint32_t anonymity,
515 struct GNUNET_TIME_Absolute expiration,
517 GNUNET_DATASTORE_DatumProcessor cont,
520 const struct OnDemandBlock *odb;
521 GNUNET_HashCode nkey;
522 struct GNUNET_CRYPTO_AesSessionKey skey;
523 struct GNUNET_CRYPTO_AesInitializationVector iv;
524 GNUNET_HashCode query;
526 char ndata[DBLOCK_SIZE];
527 char edata[DBLOCK_SIZE];
529 struct GNUNET_DISK_FileHandle *fh;
532 if (size != sizeof (struct OnDemandBlock))
535 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1,
536 GNUNET_TIME_UNIT_FOREVER_REL, &remove_cont, NULL);
537 return GNUNET_SYSERR;
539 odb = (const struct OnDemandBlock *) data;
540 off = GNUNET_ntohll (odb->offset);
541 fn = (const char *) GNUNET_CONTAINER_multihashmap_get (ifm, &odb->file_id);
546 GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
547 GNUNET_DISK_PERM_NONE))) ||
548 (off != GNUNET_DISK_file_seek (fh, off, GNUNET_DISK_SEEK_SET)) ||
549 (-1 == (nsize = GNUNET_DISK_file_read (fh, ndata, sizeof (ndata)))))
551 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
553 ("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
554 GNUNET_h2s (&odb->file_id), fn, (unsigned long long) off,
555 (fn == NULL) ? _("not indexed") : STRERROR (errno));
557 GNUNET_DISK_file_close (fh);
558 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1,
559 GNUNET_TIME_UNIT_FOREVER_REL, &remove_cont, NULL);
560 return GNUNET_SYSERR;
562 GNUNET_DISK_file_close (fh);
563 GNUNET_CRYPTO_hash (ndata, nsize, &nkey);
564 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
565 GNUNET_CRYPTO_aes_encrypt (ndata, nsize, &skey, &iv, edata);
566 GNUNET_CRYPTO_hash (edata, nsize, &query);
567 if (0 != memcmp (&query, key, sizeof (GNUNET_HashCode)))
569 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
570 _("Indexed file `%s' changed at offset %llu\n"), fn,
571 (unsigned long long) off);
572 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1,
573 GNUNET_TIME_UNIT_FOREVER_REL, &remove_cont, NULL);
574 return GNUNET_SYSERR;
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
578 "On-demand encoded block for query `%s'\n", GNUNET_h2s (key));
580 cont (cont_cls, key, nsize, edata, GNUNET_BLOCK_TYPE_FS_DBLOCK, priority,
581 anonymity, expiration, uid);
587 * Shutdown the module.
590 GNUNET_FS_indexing_done ()
592 struct IndexInfo *pos;
594 GNUNET_CONTAINER_multihashmap_destroy (ifm);
596 while (NULL != (pos = indexed_files))
598 indexed_files = pos->next;
599 if (pos->fhc != NULL)
600 GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
608 * Initialize the indexing submodule.
610 * @param c configuration to use
611 * @param d datastore to use
614 GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
615 struct GNUNET_DATASTORE_Handle *d)
619 ifm = GNUNET_CONTAINER_multihashmap_create (128);
624 /* end of gnunet-service-fs_indexing.c */