GNUnet  0.17.6
gnunet-service-fs_indexing.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009, 2010 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
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  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #include "platform.h"
27 #include <float.h>
28 #include "gnunet_core_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"
36 #include "fs.h"
37 
42 struct IndexInfo
43 {
47  struct IndexInfo *next;
48 
52  struct IndexInfo *prev;
53 
58  const char *filename;
59 
64  struct GNUNET_SERVER_TransmitContext *tc;
65 
70 
74  struct GNUNET_HashCode file_id;
75 };
76 
77 
83 
88 
95 
99 static const struct GNUNET_CONFIGURATION_Handle *cfg;
100 
106 
107 
111 static void
113 {
114  struct GNUNET_BIO_WriteHandle *wh;
115  char *fn;
116  struct IndexInfo *pos;
117 
118  if (GNUNET_OK !=
119  GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
120  {
122  "fs",
123  "INDEXDB");
124  return;
125  }
127  if (NULL == wh)
128  {
130  _ ("Could not open `%s'.\n"),
131  fn);
132  GNUNET_free (fn);
133  return;
134  }
135  for (pos = indexed_files_head; NULL != pos; pos = pos->next)
136  if ((GNUNET_OK != GNUNET_BIO_write (wh,
137  "fs-indexing-file-id",
138  &pos->file_id,
139  sizeof(struct GNUNET_HashCode))) ||
141  "fs-indexing-filename",
142  pos->filename)))
143  break;
144  if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
145  {
147  _ ("Error writing `%s'.\n"),
148  fn);
149  GNUNET_free (fn);
150  return;
151  }
152  GNUNET_free (fn);
153 }
154 
155 
159 static void
161 {
162  struct GNUNET_BIO_ReadHandle *rh;
163  char *fn;
164  struct IndexInfo *pos;
165  char *fname;
166  struct GNUNET_HashCode hc;
167  size_t slen;
168  char *emsg;
169 
170  if (GNUNET_OK !=
171  GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
172  {
174  "fs",
175  "INDEXDB");
176  return;
177  }
178  if (GNUNET_NO == GNUNET_DISK_file_test (fn))
179  {
180  /* no index info yet */
181  GNUNET_free (fn);
182  return;
183  }
184  rh = GNUNET_BIO_read_open_file (fn);
185  if (NULL == rh)
186  {
188  _ ("Could not open `%s'.\n"),
189  fn);
190  GNUNET_free (fn);
191  return;
192  }
193  while (
194  (GNUNET_OK == GNUNET_BIO_read (rh,
195  "Hash of indexed file",
196  &hc,
197  sizeof(struct GNUNET_HashCode))) &&
198  (GNUNET_OK ==
199  GNUNET_BIO_read_string (rh, "Name of indexed file", &fname, 1024 * 16)) &&
200  (fname != NULL))
201  {
202  slen = strlen (fname) + 1;
203  pos = GNUNET_malloc (sizeof(struct IndexInfo) + slen);
204  pos->file_id = hc;
205  pos->filename = (const char *) &pos[1];
206  GNUNET_memcpy (&pos[1], fname, slen);
208  ifm,
209  &pos->file_id,
210  pos,
212  {
213  GNUNET_free (pos);
214  }
215  else
216  {
218  }
219  GNUNET_free (fname);
220  }
221  if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
222  GNUNET_free (emsg);
223  GNUNET_free (fn);
224 }
225 
226 
236 static void
237 remove_cont (void *cls,
238  int success,
240  const char *msg)
241 {
242  if (GNUNET_OK != success)
244  _ ("Failed to delete bogus block: %s\n"),
245  msg);
246 }
247 
248 
249 int
251  uint32_t size,
252  const void *data,
253  enum GNUNET_BLOCK_Type type,
254  uint32_t priority,
255  uint32_t anonymity,
256  uint32_t replication,
258  uint64_t uid,
260  void *cont_cls)
261 {
262  const struct OnDemandBlock *odb;
263  struct GNUNET_HashCode nkey;
266  struct GNUNET_HashCode query;
267  ssize_t nsize;
268  char ndata[DBLOCK_SIZE];
269  char edata[DBLOCK_SIZE];
270  const char *fn;
271  struct GNUNET_DISK_FileHandle *fh;
272  uint64_t off;
273  struct IndexInfo *ii;
274 
275  if (size != sizeof(struct OnDemandBlock))
276  {
277  GNUNET_break (0);
278  GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
279  return GNUNET_SYSERR;
280  }
281  odb = (const struct OnDemandBlock *) data;
282  off = GNUNET_ntohll (odb->offset);
284  if (NULL == ii)
285  {
286  GNUNET_break (0);
288  "Failed to find index %s\n",
289  GNUNET_h2s (&odb->file_id));
290  return GNUNET_SYSERR;
291  }
292  fn = ii->filename;
293  if ((NULL == fn) || (0 != access (fn, R_OK)))
294  {
296  GSF_stats,
297  gettext_noop ("# index blocks removed: original file inaccessible"),
298  1,
299  GNUNET_YES);
300  GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
301  return GNUNET_SYSERR;
302  }
303  if ((NULL == (fh = GNUNET_DISK_file_open (fn,
306  (off != GNUNET_DISK_file_seek (fh, off, GNUNET_DISK_SEEK_SET)) ||
307  (-1 == (nsize = GNUNET_DISK_file_read (fh, ndata, sizeof(ndata)))))
308  {
310  _ (
311  "Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
312  GNUNET_h2s (&odb->file_id),
313  fn,
314  (unsigned long long) off,
315  (fn == NULL) ? _ ("not indexed") : strerror (errno));
316  if (fh != NULL)
318  GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
319  return GNUNET_SYSERR;
320  }
322  GNUNET_CRYPTO_hash (ndata, nsize, &nkey);
323  GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
324  GNUNET_CRYPTO_symmetric_encrypt (ndata, nsize, &skey, &iv, edata);
325  GNUNET_CRYPTO_hash (edata, nsize, &query);
326  if (0 != memcmp (&query, key, sizeof(struct GNUNET_HashCode)))
327  {
329  _ ("Indexed file `%s' changed at offset %llu\n"),
330  fn,
331  (unsigned long long) off);
332  GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
333  return GNUNET_SYSERR;
334  }
336  "On-demand encoded block for query `%s'\n",
337  GNUNET_h2s (key));
338  cont (cont_cls,
339  key,
340  nsize,
341  edata,
343  priority,
344  anonymity,
345  replication,
346  expiration,
347  uid);
348  return GNUNET_OK;
349 }
350 
351 
357 void
359 {
360  struct GNUNET_MQ_Envelope *env;
361  struct IndexInfoMessage *iim;
362  struct GNUNET_MessageHeader *iem;
363  size_t slen;
364  const char *fn;
365  struct IndexInfo *pos;
366 
367  for (pos = indexed_files_head; NULL != pos; pos = pos->next)
368  {
369  fn = pos->filename;
370  slen = strlen (fn) + 1;
371  if (slen + sizeof(struct IndexInfoMessage) >= GNUNET_MAX_MESSAGE_SIZE)
372  {
373  GNUNET_break (0);
374  break;
375  }
376  env =
378  iim->reserved = 0;
379  iim->file_id = pos->file_id;
380  GNUNET_memcpy (&iim[1], fn, slen);
381  GNUNET_MQ_send (mq, env);
382  }
384  GNUNET_MQ_send (mq, env);
385 }
386 
387 
394 int
396 {
397  struct IndexInfo *pos;
398 
399  for (pos = indexed_files_head; NULL != pos; pos = pos->next)
400  {
401  if (0 == memcmp (&pos->file_id, fid, sizeof(struct GNUNET_HashCode)))
402  {
404  GNUNET_break (
405  GNUNET_OK ==
407  GNUNET_free (pos);
408  write_index_list ();
409  return GNUNET_YES;
410  }
411  }
412  return GNUNET_NO;
413 }
414 
415 
422 void
424  const struct GNUNET_HashCode *file_id)
425 {
426  struct IndexInfo *ii;
427  size_t slen;
428 
430  if (NULL != ii)
431  {
432  GNUNET_log (
434  _ (
435  "Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
436  filename,
437  ii->filename);
438  return;
439  }
441  "Adding file %s to index as %s\n",
442  filename,
443  GNUNET_h2s (file_id));
444  slen = strlen (filename) + 1;
445  ii = GNUNET_malloc (sizeof(struct IndexInfo) + slen);
446  ii->file_id = *file_id;
447  ii->filename = (const char *) &ii[1];
448  GNUNET_memcpy (&ii[1], filename, slen);
452  ifm,
453  &ii->file_id,
454  ii,
456  write_index_list ();
457 }
458 
459 
463 void
465 {
466  struct IndexInfo *pos;
467 
468  while (NULL != (pos = indexed_files_head))
469  {
471  if (pos->fhc != NULL)
473  GNUNET_break (
474  GNUNET_OK ==
476  GNUNET_free (pos);
477  }
479  ifm = NULL;
480  cfg = NULL;
481 }
482 
483 
484 int
486  struct GNUNET_DATASTORE_Handle *d)
487 {
488  cfg = c;
489  dsh = d;
491  read_index_list ();
492  return GNUNET_OK;
493 }
494 
495 
496 /* end of gnunet-service-fs_indexing.c */
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
definitions for the entire fs module
#define DBLOCK_SIZE
Size of the individual blocks used for file-sharing.
Definition: fs.h:40
#define gettext_noop(String)
Definition: gettext.h:69
static char * expiration
Credential TTL.
Definition: gnunet-abd.c:96
static unsigned int replication
struct GNUNET_HashCode key
The key used in the DHT.
static unsigned int anonymity
static char * filename
uint32_t data
The data value.
static struct GNUNET_TIME_Absolute min_expiration
Minimum time that content should have to not be discarded instantly (time stamp of any content that w...
struct GNUNET_STATISTICS_Handle * GSF_stats
Handle for reporting statistics.
shared data structures of gnunet-service-fs.c
static struct GNUNET_CONTAINER_MultiHashMap * ifm
Maps hash over content of indexed files to the respective 'struct IndexInfo'.
int GNUNET_FS_indexing_do_unindex(const struct GNUNET_HashCode *fid)
Remove a file from the index.
void GNUNET_FS_add_to_index(const char *filename, const struct GNUNET_HashCode *file_id)
Add the given file to the list of indexed files.
static void remove_cont(void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
Continuation called from datastore's remove function.
static struct IndexInfo * indexed_files_tail
Tail of linked list of indexed files.
int GNUNET_FS_indexing_init(const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_DATASTORE_Handle *d)
Initialize the indexing submodule.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static struct IndexInfo * indexed_files_head
Head of linked list of indexed files.
void GNUNET_FS_indexing_send_list(struct GNUNET_MQ_Handle *mq)
Transmit information about indexed files to mq.
static void read_index_list()
Read index information from disk.
void GNUNET_FS_indexing_done()
Shutdown the module.
static void write_index_list()
Write the current index information list to disk.
int GNUNET_FS_handle_on_demand_block(const struct GNUNET_HashCode *key, uint32_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, uint64_t uid, GNUNET_DATASTORE_DatumProcessor cont, void *cont_cls)
We've received an on-demand encoded block from the datastore.
static struct GNUNET_DATASTORE_Handle * dsh
Datastore handle.
indexing for the file-sharing service
static struct GNUNET_TESTBED_BarrierWaitHandle * wh
Our barrier wait handle.
static struct GNUNET_DISK_FileHandle * fh
File handle to STDIN, for reading restart/quit commands.
Core service; the main API for encrypted P2P communications.
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
@ GNUNET_BLOCK_TYPE_FS_DBLOCK
Data block (leaf) in the CHK tree.
Helper library for interning of peer identifiers.
Constants for network protocols.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
int GNUNET_BIO_read_string(struct GNUNET_BIO_ReadHandle *h, const char *what, char **result, size_t max_length)
Read 0-terminated string.
Definition: bio.c:327
struct GNUNET_BIO_ReadHandle * GNUNET_BIO_read_open_file(const char *fn)
Open a file for reading.
Definition: bio.c:119
int GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h, const char *what, const void *buffer, size_t n)
Write a buffer to a handle.
Definition: bio.c:796
int GNUNET_BIO_write_string(struct GNUNET_BIO_WriteHandle *h, const char *what, const char *s)
Write a 0-terminated string.
Definition: bio.c:833
int GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h, char **emsg)
Close an IO handle.
Definition: bio.c:609
int GNUNET_BIO_read_close(struct GNUNET_BIO_ReadHandle *h, char **emsg)
Close an open handle.
Definition: bio.c:167
int GNUNET_BIO_read(struct GNUNET_BIO_ReadHandle *h, const char *what, void *result, size_t len)
Read some contents into a buffer.
Definition: bio.c:288
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open_file(const char *fn)
Open a file for writing.
Definition: bio.c:560
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
ssize_t GNUNET_CRYPTO_symmetric_encrypt(const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
Encrypt a block using a symmetric sessionkey.
struct GNUNET_DATASTORE_QueueEntry * GNUNET_DATASTORE_remove(struct GNUNET_DATASTORE_Handle *h, const struct GNUNET_HashCode *key, size_t size, const void *data, unsigned int queue_priority, unsigned int max_queue_size, GNUNET_DATASTORE_ContinuationWithStatus cont, void *cont_cls)
Explicitly remove some content from the database.
void(* GNUNET_DATASTORE_DatumProcessor)(void *cls, const struct GNUNET_HashCode *key, size_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, uint64_t uid)
Process a datum that was stored in the datastore.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1235
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory).
Definition: disk.c:482
off_t GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h, off_t offset, enum GNUNET_DISK_Seek whence)
Move the read/write pointer in a file.
Definition: disk.c:206
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1306
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:622
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_PERM_NONE
Nobody is allowed to do anything to the file.
@ GNUNET_DISK_SEEK_SET
Seek an absolute position (from the start of the file).
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
void GNUNET_CRYPTO_hash_to_aes_key(const struct GNUNET_HashCode *hc, struct GNUNET_CRYPTO_SymmetricSessionKey *skey, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
Convert a hashcode into a key.
Definition: crypto_hash.c:151
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Remove the given key-value pair from the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
void * GNUNET_CONTAINER_multihashmap_get(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Given a key find a value in the map matching the key.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
#define GNUNET_log(kind,...)
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:53
void GNUNET_CRYPTO_hash_file_cancel(struct GNUNET_CRYPTO_FileHashContext *fhc)
Cancel a file hashing operation.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
Definition: gnunet_common.h:99
@ GNUNET_YES
@ GNUNET_NO
Definition: gnunet_common.h:98
@ GNUNET_SYSERR
Definition: gnunet_common.h:97
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_BULK
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:302
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
Definition: gnunet_mq_lib.h:56
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:71
#define GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY
Reply to client with an indexed file name.
#define GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END
Reply to client indicating end of list.
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
Handle for buffered reading.
Definition: bio.c:74
Handle for buffered writing.
Definition: bio.c:518
Internal representation of the hash map.
Context used when hashing a file.
Handle to the datastore service.
Handle used to access files (and pipes).
A 512-bit hashcode.
Handle to a message queue.
Definition: mq.c:86
Header for all communications.
Time for absolute times used by GNUnet, in microseconds.
Message send by FS service in response to a request asking for a list of all indexed files.
Definition: fs.h:196
struct GNUNET_HashCode file_id
Hash of the indexed file.
Definition: fs.h:211
uint32_t reserved
Always zero.
Definition: fs.h:206
In-memory information about indexed files (also available on-disk).
const char * filename
Name of the indexed file.
struct IndexInfo * next
This is a doubly linked list.
struct IndexInfo * prev
This is a doubly linked list.
struct GNUNET_SERVER_TransmitContext * tc
Context for transmitting confirmation to client, NULL if we've done this already.
struct GNUNET_HashCode file_id
Hash of the contents of the file.
struct GNUNET_CRYPTO_FileHashContext * fhc
Context for hashing of the file.
index block (indexing a DBlock that can be obtained directly from reading the plaintext file)
Definition: block_fs.h:82
uint64_t offset
At which offset should we be able to find this on-demand encoded block? (in NBO)
Definition: block_fs.h:94
struct GNUNET_HashCode file_id
Hash code of the entire content of the file that was indexed (used to uniquely identify the plaintext...
Definition: block_fs.h:88
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model