aboutsummaryrefslogtreecommitdiff
path: root/src/service/fs/gnunet-service-fs_indexing.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/fs/gnunet-service-fs_indexing.c')
-rw-r--r--src/service/fs/gnunet-service-fs_indexing.c495
1 files changed, 495 insertions, 0 deletions
diff --git a/src/service/fs/gnunet-service-fs_indexing.c b/src/service/fs/gnunet-service-fs_indexing.c
new file mode 100644
index 000000000..8fb34c067
--- /dev/null
+++ b/src/service/fs/gnunet-service-fs_indexing.c
@@ -0,0 +1,495 @@
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
21/**
22 * @file fs/gnunet-service-fs_indexing.c
23 * @brief program that provides indexing functions of the file-sharing service
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include <float.h>
28#include "gnunet_core_service.h"
29#include "gnunet_datastore_service.h"
30#include "gnunet_protocols.h"
31#include "gnunet_signatures.h"
32#include "gnunet_util_lib.h"
33#include "gnunet-service-fs.h"
34#include "gnunet-service-fs_indexing.h"
35#include "fs.h"
36
37/**
38 * In-memory information about indexed files (also available
39 * on-disk).
40 */
41struct IndexInfo
42{
43 /**
44 * This is a doubly linked list.
45 */
46 struct IndexInfo *next;
47
48 /**
49 * This is a doubly linked list.
50 */
51 struct IndexInfo *prev;
52
53 /**
54 * Name of the indexed file. Memory allocated
55 * at the end of this struct (do not free).
56 */
57 const char *filename;
58
59 /**
60 * Context for transmitting confirmation to client,
61 * NULL if we've done this already.
62 */
63 struct GNUNET_SERVER_TransmitContext *tc;
64
65 /**
66 * Context for hashing of the file.
67 */
68 struct GNUNET_CRYPTO_FileHashContext *fhc;
69
70 /**
71 * Hash of the contents of the file.
72 */
73 struct GNUNET_HashCode file_id;
74};
75
76
77/**
78 * Head of linked list of indexed files.
79 * FIXME: we don't need both a DLL and a hashmap here!
80 */
81static struct IndexInfo *indexed_files_head;
82
83/**
84 * Tail of linked list of indexed files.
85 */
86static struct IndexInfo *indexed_files_tail;
87
88/**
89 * Maps hash over content of indexed files to the respective 'struct IndexInfo'.
90 * The filenames are pointers into the indexed_files linked list and
91 * do not need to be freed.
92 */
93static struct GNUNET_CONTAINER_MultiHashMap *ifm;
94
95/**
96 * Our configuration.
97 */
98static const struct GNUNET_CONFIGURATION_Handle *cfg;
99
100/**
101 * Datastore handle. Created and destroyed by code in
102 * gnunet-service-fs (this is an alias).
103 */
104static struct GNUNET_DATASTORE_Handle *dsh;
105
106
107/**
108 * Write the current index information list to disk.
109 */
110static void
111write_index_list ()
112{
113 struct GNUNET_BIO_WriteHandle *wh;
114 char *fn;
115 struct IndexInfo *pos;
116
117 if (GNUNET_OK !=
118 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
119 {
120 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
121 "fs",
122 "INDEXDB");
123 return;
124 }
125 wh = GNUNET_BIO_write_open_file (fn);
126 if (NULL == wh)
127 {
128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
129 _ ("Could not open `%s'.\n"),
130 fn);
131 GNUNET_free (fn);
132 return;
133 }
134 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
135 if ((GNUNET_OK != GNUNET_BIO_write (wh,
136 "fs-indexing-file-id",
137 &pos->file_id,
138 sizeof(struct GNUNET_HashCode))) ||
139 (GNUNET_OK != GNUNET_BIO_write_string (wh,
140 "fs-indexing-filename",
141 pos->filename)))
142 break;
143 if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
144 {
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
146 _ ("Error writing `%s'.\n"),
147 fn);
148 GNUNET_free (fn);
149 return;
150 }
151 GNUNET_free (fn);
152}
153
154
155/**
156 * Read index information from disk.
157 */
158static void
159read_index_list ()
160{
161 struct GNUNET_BIO_ReadHandle *rh;
162 char *fn;
163 struct IndexInfo *pos;
164 char *fname;
165 struct GNUNET_HashCode hc;
166 size_t slen;
167 char *emsg;
168
169 if (GNUNET_OK !=
170 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
171 {
172 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
173 "fs",
174 "INDEXDB");
175 return;
176 }
177 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
178 {
179 /* no index info yet */
180 GNUNET_free (fn);
181 return;
182 }
183 rh = GNUNET_BIO_read_open_file (fn);
184 if (NULL == rh)
185 {
186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
187 _ ("Could not open `%s'.\n"),
188 fn);
189 GNUNET_free (fn);
190 return;
191 }
192 while (
193 (GNUNET_OK == GNUNET_BIO_read (rh,
194 "Hash of indexed file",
195 &hc,
196 sizeof(struct GNUNET_HashCode))) &&
197 (GNUNET_OK ==
198 GNUNET_BIO_read_string (rh, "Name of indexed file", &fname, 1024 * 16)) &&
199 (fname != NULL))
200 {
201 slen = strlen (fname) + 1;
202 pos = GNUNET_malloc (sizeof(struct IndexInfo) + slen);
203 pos->file_id = hc;
204 pos->filename = (const char *) &pos[1];
205 GNUNET_memcpy (&pos[1], fname, slen);
206 if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put (
207 ifm,
208 &pos->file_id,
209 pos,
210 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
211 {
212 GNUNET_free (pos);
213 }
214 else
215 {
216 GNUNET_CONTAINER_DLL_insert (indexed_files_head, indexed_files_tail, pos);
217 }
218 GNUNET_free (fname);
219 }
220 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
221 GNUNET_free (emsg);
222 GNUNET_free (fn);
223}
224
225
226/**
227 * Continuation called from datastore's remove
228 * function.
229 *
230 * @param cls unused
231 * @param success did the deletion work?
232 * @param min_expiration minimum expiration time required for content to be stored
233 * @param msg error message
234 */
235static void
236remove_cont (void *cls,
237 int success,
238 struct GNUNET_TIME_Absolute min_expiration,
239 const char *msg)
240{
241 if (GNUNET_OK != success)
242 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
243 _ ("Failed to delete bogus block: %s\n"),
244 msg);
245}
246
247
248int
249GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode *key,
250 uint32_t size,
251 const void *data,
252 enum GNUNET_BLOCK_Type type,
253 uint32_t priority,
254 uint32_t anonymity,
255 uint32_t replication,
256 struct GNUNET_TIME_Absolute expiration,
257 uint64_t uid,
258 GNUNET_DATASTORE_DatumProcessor cont,
259 void *cont_cls)
260{
261 const struct OnDemandBlock *odb;
262 struct GNUNET_HashCode nkey;
263 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
264 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
265 struct GNUNET_HashCode query;
266 ssize_t nsize;
267 char ndata[DBLOCK_SIZE];
268 char edata[DBLOCK_SIZE];
269 const char *fn;
270 struct GNUNET_DISK_FileHandle *fh;
271 uint64_t off;
272 struct IndexInfo *ii;
273
274 if (size != sizeof(struct OnDemandBlock))
275 {
276 GNUNET_break (0);
277 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
278 return GNUNET_SYSERR;
279 }
280 odb = (const struct OnDemandBlock *) data;
281 off = GNUNET_ntohll (odb->offset);
282 ii = GNUNET_CONTAINER_multihashmap_get (ifm, &odb->file_id);
283 if (NULL == ii)
284 {
285 GNUNET_break (0);
286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
287 "Failed to find index %s\n",
288 GNUNET_h2s (&odb->file_id));
289 return GNUNET_SYSERR;
290 }
291 fn = ii->filename;
292 if ((NULL == fn) || (0 != access (fn, R_OK)))
293 {
294 GNUNET_STATISTICS_update (
295 GSF_stats,
296 gettext_noop ("# index blocks removed: original file inaccessible"),
297 1,
298 GNUNET_YES);
299 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
300 return GNUNET_SYSERR;
301 }
302 if ((NULL == (fh = GNUNET_DISK_file_open (fn,
303 GNUNET_DISK_OPEN_READ,
304 GNUNET_DISK_PERM_NONE))) ||
305 (off != GNUNET_DISK_file_seek (fh, off, GNUNET_DISK_SEEK_SET)) ||
306 (-1 == (nsize = GNUNET_DISK_file_read (fh, ndata, sizeof(ndata)))))
307 {
308 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
309 _ (
310 "Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
311 GNUNET_h2s (&odb->file_id),
312 fn,
313 (unsigned long long) off,
314 (fn == NULL) ? _ ("not indexed") : strerror (errno));
315 if (fh != NULL)
316 GNUNET_DISK_file_close (fh);
317 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
318 return GNUNET_SYSERR;
319 }
320 GNUNET_DISK_file_close (fh);
321 GNUNET_CRYPTO_hash (ndata, nsize, &nkey);
322 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
323 GNUNET_CRYPTO_symmetric_encrypt (ndata, nsize, &skey, &iv, edata);
324 GNUNET_CRYPTO_hash (edata, nsize, &query);
325 if (0 != memcmp (&query, key, sizeof(struct GNUNET_HashCode)))
326 {
327 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
328 _ ("Indexed file `%s' changed at offset %llu\n"),
329 fn,
330 (unsigned long long) off);
331 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
332 return GNUNET_SYSERR;
333 }
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "On-demand encoded block for query `%s'\n",
336 GNUNET_h2s (key));
337 cont (cont_cls,
338 key,
339 nsize,
340 edata,
341 GNUNET_BLOCK_TYPE_FS_DBLOCK,
342 priority,
343 anonymity,
344 replication,
345 expiration,
346 uid);
347 return GNUNET_OK;
348}
349
350
351/**
352 * Transmit information about indexed files to @a mq.
353 *
354 * @param mq message queue to send information to
355 */
356void
357GNUNET_FS_indexing_send_list (struct GNUNET_MQ_Handle *mq)
358{
359 struct GNUNET_MQ_Envelope *env;
360 struct IndexInfoMessage *iim;
361 struct GNUNET_MessageHeader *iem;
362 size_t slen;
363 const char *fn;
364 struct IndexInfo *pos;
365
366 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
367 {
368 fn = pos->filename;
369 slen = strlen (fn) + 1;
370 if (slen + sizeof(struct IndexInfoMessage) >= GNUNET_MAX_MESSAGE_SIZE)
371 {
372 GNUNET_break (0);
373 break;
374 }
375 env =
376 GNUNET_MQ_msg_extra (iim, slen, GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
377 iim->reserved = 0;
378 iim->file_id = pos->file_id;
379 GNUNET_memcpy (&iim[1], fn, slen);
380 GNUNET_MQ_send (mq, env);
381 }
382 env = GNUNET_MQ_msg (iem, GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
383 GNUNET_MQ_send (mq, env);
384}
385
386
387/**
388 * Remove a file from the index.
389 *
390 * @param fid identifier of the file to remove
391 * @return #GNUNET_YES if the @a fid was found
392 */
393int
394GNUNET_FS_indexing_do_unindex (const struct GNUNET_HashCode *fid)
395{
396 struct IndexInfo *pos;
397
398 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
399 {
400 if (0 == memcmp (&pos->file_id, fid, sizeof(struct GNUNET_HashCode)))
401 {
402 GNUNET_CONTAINER_DLL_remove (indexed_files_head, indexed_files_tail, pos);
403 GNUNET_break (
404 GNUNET_OK ==
405 GNUNET_CONTAINER_multihashmap_remove (ifm, &pos->file_id, pos));
406 GNUNET_free (pos);
407 write_index_list ();
408 return GNUNET_YES;
409 }
410 }
411 return GNUNET_NO;
412}
413
414
415/**
416 * Add the given file to the list of indexed files.
417 *
418 * @param filename name of the file
419 * @param file_id hash identifier for @a filename
420 */
421void
422GNUNET_FS_add_to_index (const char *filename,
423 const struct GNUNET_HashCode *file_id)
424{
425 struct IndexInfo *ii;
426 size_t slen;
427
428 ii = GNUNET_CONTAINER_multihashmap_get (ifm, file_id);
429 if (NULL != ii)
430 {
431 GNUNET_log (
432 GNUNET_ERROR_TYPE_INFO,
433 _ (
434 "Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
435 filename,
436 ii->filename);
437 return;
438 }
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "Adding file %s to index as %s\n",
441 filename,
442 GNUNET_h2s (file_id));
443 slen = strlen (filename) + 1;
444 ii = GNUNET_malloc (sizeof(struct IndexInfo) + slen);
445 ii->file_id = *file_id;
446 ii->filename = (const char *) &ii[1];
447 GNUNET_memcpy (&ii[1], filename, slen);
448 GNUNET_CONTAINER_DLL_insert (indexed_files_head, indexed_files_tail, ii);
449 GNUNET_assert (GNUNET_OK ==
450 GNUNET_CONTAINER_multihashmap_put (
451 ifm,
452 &ii->file_id,
453 ii,
454 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
455 write_index_list ();
456}
457
458
459/**
460 * Shutdown the module.
461 */
462void
463GNUNET_FS_indexing_done ()
464{
465 struct IndexInfo *pos;
466
467 while (NULL != (pos = indexed_files_head))
468 {
469 GNUNET_CONTAINER_DLL_remove (indexed_files_head, indexed_files_tail, pos);
470 if (pos->fhc != NULL)
471 GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
472 GNUNET_break (
473 GNUNET_OK ==
474 GNUNET_CONTAINER_multihashmap_remove (ifm, &pos->file_id, pos));
475 GNUNET_free (pos);
476 }
477 GNUNET_CONTAINER_multihashmap_destroy (ifm);
478 ifm = NULL;
479 cfg = NULL;
480}
481
482
483int
484GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
485 struct GNUNET_DATASTORE_Handle *d)
486{
487 cfg = c;
488 dsh = d;
489 ifm = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_YES);
490 read_index_list ();
491 return GNUNET_OK;
492}
493
494
495/* end of gnunet-service-fs_indexing.c */