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 doubly linked list.
48 struct IndexInfo *next;
51 * This is a doubly linked list.
53 struct IndexInfo *prev;
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 * Context for hashing of the file.
70 struct GNUNET_CRYPTO_FileHashContext *fhc;
73 * Hash of the contents of the file.
75 struct GNUNET_HashCode file_id;
81 * Head of linked list of indexed files.
83 static struct IndexInfo *indexed_files_head;
86 * Tail of linked list of indexed files.
88 static struct IndexInfo *indexed_files_tail;
91 * Maps hash over content of indexed files to the respective 'struct IndexInfo'.
92 * The filenames are pointers into the indexed_files linked list and
93 * do not need to be freed.
95 static struct GNUNET_CONTAINER_MultiHashMap *ifm;
100 static const struct GNUNET_CONFIGURATION_Handle *cfg;
103 * Datastore handle. Created and destroyed by code in
104 * gnunet-service-fs (this is an alias).
106 static struct GNUNET_DATASTORE_Handle *dsh;
110 * Write the current index information list to disk.
115 struct GNUNET_BIO_WriteHandle *wh;
117 struct IndexInfo *pos;
120 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
122 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
126 wh = GNUNET_BIO_write_open (fn);
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
130 _("Could not open `%s'.\n"), fn);
134 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
136 GNUNET_BIO_write (wh, &pos->file_id, sizeof (struct GNUNET_HashCode))) ||
137 (GNUNET_OK != GNUNET_BIO_write_string (wh, pos->filename)))
139 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
142 _("Error writing `%s'.\n"), fn);
151 * Read index information from disk.
156 struct GNUNET_BIO_ReadHandle *rh;
158 struct IndexInfo *pos;
160 struct GNUNET_HashCode hc;
165 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
167 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
171 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
173 /* no index info yet */
177 rh = GNUNET_BIO_read_open (fn);
180 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
181 _("Could not open `%s'.\n"), fn);
186 GNUNET_BIO_read (rh, "Hash of indexed file", &hc,
187 sizeof (struct GNUNET_HashCode))) &&
189 GNUNET_BIO_read_string (rh, "Name of indexed file", &fname,
190 1024 * 16)) && (fname != NULL))
192 slen = strlen (fname) + 1;
193 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
195 pos->filename = (const char *) &pos[1];
196 memcpy (&pos[1], fname, slen);
198 GNUNET_CONTAINER_multihashmap_put (ifm, &pos->file_id, pos,
199 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
205 GNUNET_CONTAINER_DLL_insert (indexed_files_head,
211 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
218 * We've validated the hash of the file we're about to index. Signal
219 * success to the client and update our internal data structures.
221 * @param ii the index info entry for the request
224 signal_index_ok (struct IndexInfo *ii)
226 struct IndexInfo *ir;
228 GNUNET_CONTAINER_multihashmap_put (ifm, &ii->file_id,
230 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
232 ir = GNUNET_CONTAINER_multihashmap_get (ifm,
234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
236 ("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
239 GNUNET_SERVER_transmit_context_append_data (ii->tc, NULL, 0,
240 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
241 GNUNET_SERVER_transmit_context_run (ii->tc, GNUNET_TIME_UNIT_MINUTES);
245 GNUNET_CONTAINER_DLL_insert (indexed_files_head,
249 GNUNET_SERVER_transmit_context_append_data (ii->tc, NULL, 0,
250 GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK);
251 GNUNET_SERVER_transmit_context_run (ii->tc, GNUNET_TIME_UNIT_MINUTES);
257 * Function called once the hash computation over an
258 * indexed file has completed.
260 * @param cls closure, our publishing context
261 * @param res resulting hash, NULL on error
264 hash_for_index_val (void *cls, const struct GNUNET_HashCode * res)
266 struct IndexInfo *ii = cls;
270 (0 != memcmp (res, &ii->file_id, sizeof (struct GNUNET_HashCode))))
272 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
274 ("Hash mismatch trying to index file `%s' which has hash `%s'\n"),
275 ii->filename, GNUNET_h2s (res));
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Wanted `%s'\n",
277 GNUNET_h2s (&ii->file_id));
278 GNUNET_SERVER_transmit_context_append_data (ii->tc, NULL, 0,
279 GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED);
280 GNUNET_SERVER_transmit_context_run (ii->tc, GNUNET_TIME_UNIT_MINUTES);
284 signal_index_ok (ii);
289 * Handle INDEX_START-message.
292 * @param client identification of the client
293 * @param message the actual message
296 GNUNET_FS_handle_index_start (void *cls, struct GNUNET_SERVER_Client *client,
297 const struct GNUNET_MessageHeader *message)
299 const struct IndexStartMessage *ism;
302 struct IndexInfo *ii;
309 msize = ntohs (message->size);
310 if ((msize <= sizeof (struct IndexStartMessage)) ||
311 (((const char *) message)[msize - 1] != '\0'))
314 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
317 ism = (const struct IndexStartMessage *) message;
318 if (0 != ism->reserved)
321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
324 fn = GNUNET_STRINGS_filename_expand ((const char *) &ism[1]);
327 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
330 dev = GNUNET_ntohll (ism->device);
331 ino = GNUNET_ntohll (ism->inode);
332 ism = (const struct IndexStartMessage *) message;
333 slen = strlen (fn) + 1;
334 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
335 ii->filename = (const char *) &ii[1];
336 memcpy (&ii[1], fn, slen);
337 ii->file_id = ism->file_id;
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message for file `%s'\n",
339 "START_INDEX", ii->filename);
340 ii->tc = GNUNET_SERVER_transmit_context_create (client);
343 if (((dev != 0) || (ino != 0)) &&
344 (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn, &mydev, &myino)) &&
345 ((dev == mydev) && (ino == myino)))
347 /* fast validation OK! */
348 signal_index_ok (ii);
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Mismatch in file identifiers (%llu != %llu or %u != %u), need to hash.\n",
354 (unsigned long long) ino, (unsigned long long) myino,
355 (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] GNUNET_ALIGN;
382 struct IndexInfo *pos;
384 tc = GNUNET_SERVER_transmit_context_create (client);
385 iim = (struct IndexInfoMessage *) buf;
386 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
389 slen = strlen (fn) + 1;
390 if (slen + sizeof (struct IndexInfoMessage) >=
391 GNUNET_SERVER_MAX_MESSAGE_SIZE)
396 iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
397 iim->header.size = htons (slen + sizeof (struct IndexInfoMessage));
399 iim->file_id = pos->file_id;
400 memcpy (&iim[1], fn, slen);
401 GNUNET_SERVER_transmit_context_append_message (tc, &iim->header);
403 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
404 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
405 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_MINUTES);
410 * Handle UNINDEX-message.
413 * @param client identification of the client
414 * @param message the actual message
417 GNUNET_FS_handle_unindex (void *cls, struct GNUNET_SERVER_Client *client,
418 const struct GNUNET_MessageHeader *message)
420 const struct UnindexMessage *um;
421 struct IndexInfo *pos;
422 struct GNUNET_SERVER_TransmitContext *tc;
425 um = (const struct UnindexMessage *) message;
426 if (0 != um->reserved)
429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
433 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
435 if (0 == memcmp (&pos->file_id, &um->file_id, sizeof (struct GNUNET_HashCode)))
437 GNUNET_CONTAINER_DLL_remove (indexed_files_head,
440 GNUNET_break (GNUNET_OK ==
441 GNUNET_CONTAINER_multihashmap_remove (ifm, &pos->file_id,
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
449 "Client requested unindexing of file `%s': %s\n",
450 GNUNET_h2s (&um->file_id), found ? "found" : "not found");
451 if (GNUNET_YES == found)
453 tc = GNUNET_SERVER_transmit_context_create (client);
454 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
455 GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK);
456 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_MINUTES);
461 * Continuation called from datastore's remove
465 * @param success did the deletion work?
466 * @param min_expiration minimum expiration time required for content to be stored
467 * @param msg error message
470 remove_cont (void *cls, int success,
471 struct GNUNET_TIME_Absolute min_expiration,
474 if (GNUNET_OK != success)
475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
476 _("Failed to delete bogus block: %s\n"), msg);
481 * We've received an on-demand encoded block from the datastore.
482 * Attempt to do on-demand encoding and (if successful), call the
483 * continuation with the resulting block. On error, clean up and ask
484 * the datastore for more results.
486 * @param key key for the content
487 * @param size number of bytes in data
488 * @param data content stored
489 * @param type type of the content
490 * @param priority priority of the content
491 * @param anonymity anonymity-level for the content
492 * @param expiration expiration time for the content
493 * @param uid unique identifier for the datum;
494 * maybe 0 if no unique identifier is available
495 * @param cont function to call with the actual block (at most once, on success)
496 * @param cont_cls closure for cont
497 * @return GNUNET_OK on success
500 GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode * key, uint32_t size,
501 const void *data, enum GNUNET_BLOCK_Type type,
502 uint32_t priority, uint32_t anonymity,
503 struct GNUNET_TIME_Absolute expiration,
505 GNUNET_DATASTORE_DatumProcessor cont,
508 const struct OnDemandBlock *odb;
509 struct GNUNET_HashCode nkey;
510 struct GNUNET_CRYPTO_AesSessionKey skey;
511 struct GNUNET_CRYPTO_AesInitializationVector iv;
512 struct GNUNET_HashCode query;
514 char ndata[DBLOCK_SIZE];
515 char edata[DBLOCK_SIZE];
517 struct GNUNET_DISK_FileHandle *fh;
519 struct IndexInfo *ii;
521 if (size != sizeof (struct OnDemandBlock))
524 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1,
525 GNUNET_TIME_UNIT_FOREVER_REL, &remove_cont, NULL);
526 return GNUNET_SYSERR;
528 odb = (const struct OnDemandBlock *) data;
529 off = GNUNET_ntohll (odb->offset);
530 ii = GNUNET_CONTAINER_multihashmap_get (ifm, &odb->file_id);
532 if ((NULL == fn) || (0 != ACCESS (fn, R_OK)))
534 GNUNET_STATISTICS_update (GSF_stats,
536 ("# index blocks removed: original file inaccessible"),
538 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1,
539 GNUNET_TIME_UNIT_FOREVER_REL, &remove_cont, NULL);
540 return GNUNET_SYSERR;
544 GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
545 GNUNET_DISK_PERM_NONE))) ||
546 (off != GNUNET_DISK_file_seek (fh, off, GNUNET_DISK_SEEK_SET)) ||
547 (-1 == (nsize = GNUNET_DISK_file_read (fh, ndata, sizeof (ndata)))))
549 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
551 ("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
552 GNUNET_h2s (&odb->file_id), fn, (unsigned long long) off,
553 (fn == NULL) ? _("not indexed") : STRERROR (errno));
555 GNUNET_DISK_file_close (fh);
556 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1,
557 GNUNET_TIME_UNIT_FOREVER_REL, &remove_cont, NULL);
558 return GNUNET_SYSERR;
560 GNUNET_DISK_file_close (fh);
561 GNUNET_CRYPTO_hash (ndata, nsize, &nkey);
562 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
563 GNUNET_CRYPTO_aes_encrypt (ndata, nsize, &skey, &iv, edata);
564 GNUNET_CRYPTO_hash (edata, nsize, &query);
565 if (0 != memcmp (&query, key, sizeof (struct GNUNET_HashCode)))
567 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
568 _("Indexed file `%s' changed at offset %llu\n"), fn,
569 (unsigned long long) off);
570 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1,
571 GNUNET_TIME_UNIT_FOREVER_REL, &remove_cont, NULL);
572 return GNUNET_SYSERR;
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "On-demand encoded block for query `%s'\n", GNUNET_h2s (key));
576 cont (cont_cls, key, nsize, edata, GNUNET_BLOCK_TYPE_FS_DBLOCK, priority,
577 anonymity, expiration, uid);
583 * Shutdown the module.
586 GNUNET_FS_indexing_done ()
588 struct IndexInfo *pos;
590 while (NULL != (pos = indexed_files_head))
592 GNUNET_CONTAINER_DLL_remove (indexed_files_head,
595 if (pos->fhc != NULL)
596 GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
597 GNUNET_break (GNUNET_OK ==
598 GNUNET_CONTAINER_multihashmap_remove (ifm,
599 &pos->file_id, pos));
602 GNUNET_CONTAINER_multihashmap_destroy (ifm);
609 * Initialize the indexing submodule.
611 * @param c configuration to use
612 * @param d datastore to use
615 GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
616 struct GNUNET_DATASTORE_Handle *d)
620 ifm = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_YES);
625 /* end of gnunet-service-fs_indexing.c */