aboutsummaryrefslogtreecommitdiff
path: root/src/service/fs/fs_tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/fs/fs_tree.c')
-rw-r--r--src/service/fs/fs_tree.c452
1 files changed, 452 insertions, 0 deletions
diff --git a/src/service/fs/fs_tree.c b/src/service/fs/fs_tree.c
new file mode 100644
index 000000000..65f589966
--- /dev/null
+++ b/src/service/fs/fs_tree.c
@@ -0,0 +1,452 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2011 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 * @file fs/fs_tree.c
22 * @brief Merkle-tree-ish-CHK file encoding for GNUnet
23 * @see http://gnunet.org/encoding.php3
24 * @author Krista Bennett
25 * @author Christian Grothoff
26 */
27#include "platform.h"
28#include "fs_tree.h"
29
30
31/**
32 * Context for an ECRS-based file encoder that computes
33 * the Merkle-ish-CHK tree.
34 */
35struct GNUNET_FS_TreeEncoder
36{
37 /**
38 * Global FS context.
39 */
40 struct GNUNET_FS_Handle *h;
41
42 /**
43 * Closure for all callbacks.
44 */
45 void *cls;
46
47 /**
48 * Function to call on encrypted blocks.
49 */
50 GNUNET_FS_TreeBlockProcessor proc;
51
52 /**
53 * Function to call with progress information.
54 */
55 GNUNET_FS_TreeProgressCallback progress;
56
57 /**
58 * Function to call to receive input data.
59 */
60 GNUNET_FS_DataReader reader;
61
62 /**
63 * Function to call once we're done with processing.
64 */
65 GNUNET_SCHEDULER_TaskCallback cont;
66
67 /**
68 * Set to an error message (if we had an error).
69 */
70 char *emsg;
71
72 /**
73 * Set to the URI (upon successful completion)
74 */
75 struct GNUNET_FS_Uri *uri;
76
77 /**
78 * Overall file size.
79 */
80 uint64_t size;
81
82 /**
83 * How far are we?
84 */
85 uint64_t publish_offset;
86
87 /**
88 * How deep are we? Depth 0 is for the DBLOCKs.
89 */
90 unsigned int current_depth;
91
92 /**
93 * How deep is the tree? Always > 0.
94 */
95 unsigned int chk_tree_depth;
96
97 /**
98 * In-memory cache of the current CHK tree.
99 * This struct will contain the CHK values
100 * from the root to the currently processed
101 * node in the tree as identified by
102 * "current_depth" and "publish_offset".
103 * The "chktree" will be initially NULL,
104 * then allocated to a sufficient number of
105 * entries for the size of the file and
106 * finally freed once the upload is complete.
107 */
108 struct ContentHashKey *chk_tree;
109
110 /**
111 * Are we currently in 'GNUNET_FS_tree_encoder_next'?
112 * Flag used to prevent recursion.
113 */
114 int in_next;
115};
116
117
118/**
119 * Compute the depth of the CHK tree.
120 *
121 * @param flen file length for which to compute the depth
122 * @return depth of the tree, always > 0. A depth of 1 means only a DBLOCK.
123 */
124unsigned int
125GNUNET_FS_compute_depth (uint64_t flen)
126{
127 unsigned int treeDepth;
128 uint64_t fl;
129
130 treeDepth = 1;
131 fl = DBLOCK_SIZE;
132 while (fl < flen)
133 {
134 treeDepth++;
135 if (fl * CHK_PER_INODE < fl)
136 {
137 /* integer overflow, this is a HUGE file... */
138 return treeDepth;
139 }
140 fl = fl * CHK_PER_INODE;
141 }
142 return treeDepth;
143}
144
145
146/**
147 * Calculate how many bytes of payload a block tree of the given
148 * depth MAY correspond to at most (this function ignores the fact that
149 * some blocks will only be present partially due to the total file
150 * size cutting some blocks off at the end).
151 *
152 * @param depth depth of the block. depth==0 is a DBLOCK.
153 * @return number of bytes of payload a subtree of this depth may correspond to
154 */
155uint64_t
156GNUNET_FS_tree_compute_tree_size (unsigned int depth)
157{
158 uint64_t rsize;
159 unsigned int i;
160
161 rsize = DBLOCK_SIZE;
162 for (i = 0; i < depth; i++)
163 rsize *= CHK_PER_INODE;
164 return rsize;
165}
166
167
168/**
169 * Compute the size of the current IBLOCK. The encoder is
170 * triggering the calculation of the size of an IBLOCK at the
171 * *end* (hence end_offset) of its construction. The IBLOCK
172 * maybe a full or a partial IBLOCK, and this function is to
173 * calculate how long it should be.
174 *
175 * @param depth depth of the IBlock in the tree, 0 would be a DBLOCK,
176 * must be > 0 (this function is for IBLOCKs only!)
177 * @param end_offset current offset in the payload (!) of the overall file,
178 * must be > 0 (since this function is called at the
179 * end of a block).
180 * @return size of the corresponding IBlock
181 */
182static uint16_t
183GNUNET_FS_tree_compute_iblock_size (unsigned int depth, uint64_t end_offset)
184{
185 unsigned int ret;
186 uint64_t mod;
187 uint64_t bds;
188
189 GNUNET_assert (depth > 0);
190 GNUNET_assert (end_offset > 0);
191 bds = GNUNET_FS_tree_compute_tree_size (depth);
192 mod = end_offset % bds;
193 if (0 == mod)
194 {
195 /* we were triggered at the end of a full block */
196 ret = CHK_PER_INODE;
197 }
198 else
199 {
200 /* we were triggered at the end of the file */
201 bds /= CHK_PER_INODE;
202 ret = mod / bds;
203 if (0 != mod % bds)
204 ret++;
205 }
206 return (uint16_t) (ret * sizeof(struct ContentHashKey));
207}
208
209
210size_t
211GNUNET_FS_tree_calculate_block_size (uint64_t fsize, uint64_t offset,
212 unsigned int depth)
213{
214 size_t ret;
215 uint64_t rsize;
216 uint64_t epos;
217 unsigned int chks;
218
219 GNUNET_assert (fsize > 0);
220 GNUNET_assert (offset <= fsize);
221 if (depth == 0)
222 {
223 ret = DBLOCK_SIZE;
224 if ((offset + ret > fsize) || (offset + ret < offset))
225 ret = (size_t) (fsize - offset);
226 return ret;
227 }
228
229 rsize = GNUNET_FS_tree_compute_tree_size (depth - 1);
230 epos = offset + rsize * CHK_PER_INODE;
231 if ((epos < offset) || (epos > fsize))
232 epos = fsize;
233 /* round up when computing #CHKs in our IBlock */
234 chks = (epos - offset + rsize - 1) / rsize;
235 GNUNET_assert (chks <= CHK_PER_INODE);
236 return chks * sizeof(struct ContentHashKey);
237}
238
239
240/**
241 * Initialize a tree encoder. This function will call @a proc and
242 * "progress" on each block in the tree. Once all blocks have been
243 * processed, "cont" will be scheduled. The @a reader will be called
244 * to obtain the (plaintext) blocks for the file. Note that this
245 * function will not actually call @a proc. The client must
246 * call #GNUNET_FS_tree_encoder_next to trigger encryption (and
247 * calling of @a proc) for the each block.
248 *
249 * @param h the global FS context
250 * @param size overall size of the file to encode
251 * @param cls closure for reader, proc, progress and cont
252 * @param reader function to call to read plaintext data
253 * @param proc function to call on each encrypted block
254 * @param progress function to call with progress information
255 * @param cont function to call when done
256 */
257struct GNUNET_FS_TreeEncoder *
258GNUNET_FS_tree_encoder_create (struct GNUNET_FS_Handle *h, uint64_t size,
259 void *cls,
260 GNUNET_FS_DataReader reader,
261 GNUNET_FS_TreeBlockProcessor proc,
262 GNUNET_FS_TreeProgressCallback progress,
263 GNUNET_SCHEDULER_TaskCallback cont)
264{
265 struct GNUNET_FS_TreeEncoder *te;
266
267 te = GNUNET_new (struct GNUNET_FS_TreeEncoder);
268 te->h = h;
269 te->size = size;
270 te->cls = cls;
271 te->reader = reader;
272 te->proc = proc;
273 te->progress = progress;
274 te->cont = cont;
275 te->chk_tree_depth = GNUNET_FS_compute_depth (size);
276 te->chk_tree
277 = GNUNET_new_array (te->chk_tree_depth * CHK_PER_INODE,
278 struct ContentHashKey);
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "Created tree encoder for file with %llu bytes and depth %u\n",
281 (unsigned long long) size,
282 te->chk_tree_depth);
283 return te;
284}
285
286
287/**
288 * Compute the offset of the CHK for the
289 * current block in the IBlock above.
290 *
291 * @param depth depth of the IBlock in the tree (aka overall
292 * number of tree levels minus depth); 0 == DBlock
293 * @param end_offset current offset in the overall file,
294 * at the *beginning* of the block for DBLOCKs (depth==0),
295 * otherwise at the *end* of the block (exclusive)
296 * @return (array of CHKs') offset in the above IBlock
297 */
298static unsigned int
299compute_chk_offset (unsigned int depth, uint64_t end_offset)
300{
301 uint64_t bds;
302 unsigned int ret;
303
304 bds = GNUNET_FS_tree_compute_tree_size (depth);
305 if (depth > 0)
306 end_offset--; /* round down since for depth > 0 offset is at the END of the block */
307 ret = end_offset / bds;
308 return ret % CHK_PER_INODE;
309}
310
311
312/**
313 * Encrypt the next block of the file (and call proc and progress
314 * accordingly; or of course "cont" if we have already completed
315 * encoding of the entire file).
316 *
317 * @param te tree encoder to use
318 */
319void
320GNUNET_FS_tree_encoder_next (struct GNUNET_FS_TreeEncoder *te)
321{
322 struct ContentHashKey *mychk;
323 const void *pt_block;
324 uint16_t pt_size;
325 char iob[DBLOCK_SIZE];
326 char enc[DBLOCK_SIZE];
327 struct GNUNET_CRYPTO_SymmetricSessionKey sk;
328 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
329 unsigned int off;
330
331 GNUNET_assert (GNUNET_NO == te->in_next);
332 te->in_next = GNUNET_YES;
333 if (te->chk_tree_depth == te->current_depth)
334 {
335 off = CHK_PER_INODE * (te->chk_tree_depth - 1);
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TE done, reading CHK `%s' from %u\n",
337 GNUNET_h2s (&te->chk_tree[off].query), off);
338 te->uri = GNUNET_new (struct GNUNET_FS_Uri);
339 te->uri->type = GNUNET_FS_URI_CHK;
340 te->uri->data.chk.chk = te->chk_tree[off];
341 te->uri->data.chk.file_length = GNUNET_htonll (te->size);
342 te->in_next = GNUNET_NO;
343 te->cont (te->cls);
344 return;
345 }
346 if (0 == te->current_depth)
347 {
348 /* read DBLOCK */
349 pt_size = GNUNET_MIN (DBLOCK_SIZE, te->size - te->publish_offset);
350 if (pt_size !=
351 te->reader (te->cls, te->publish_offset, pt_size, iob, &te->emsg))
352 {
353 te->in_next = GNUNET_NO;
354 te->cont (te->cls);
355 return;
356 }
357 pt_block = iob;
358 }
359 else
360 {
361 pt_size =
362 GNUNET_FS_tree_compute_iblock_size (te->current_depth,
363 te->publish_offset);
364 pt_block = &te->chk_tree[(te->current_depth - 1) * CHK_PER_INODE];
365 }
366 off = compute_chk_offset (te->current_depth, te->publish_offset);
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 "TE is at offset %llu and depth %u with block size %u and target-CHK-offset %u\n",
369 (unsigned long long) te->publish_offset, te->current_depth,
370 (unsigned int) pt_size, (unsigned int) off);
371 mychk = &te->chk_tree[te->current_depth * CHK_PER_INODE + off];
372 GNUNET_CRYPTO_hash (pt_block, pt_size, &mychk->key);
373 GNUNET_CRYPTO_hash_to_aes_key (&mychk->key, &sk, &iv);
374 GNUNET_CRYPTO_symmetric_encrypt (pt_block, pt_size, &sk, &iv, enc);
375 GNUNET_CRYPTO_hash (enc, pt_size, &mychk->query);
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "TE calculates query to be `%s', stored at %u\n",
378 GNUNET_h2s (&mychk->query),
379 te->current_depth * CHK_PER_INODE + off);
380 if (NULL != te->proc)
381 te->proc (te->cls, mychk, te->publish_offset, te->current_depth,
382 (0 ==
383 te->current_depth) ? GNUNET_BLOCK_TYPE_FS_DBLOCK :
384 GNUNET_BLOCK_TYPE_FS_IBLOCK, enc, pt_size);
385 if (NULL != te->progress)
386 te->progress (te->cls, te->publish_offset, pt_block, pt_size,
387 te->current_depth);
388 if (0 == te->current_depth)
389 {
390 te->publish_offset += pt_size;
391 if ((te->publish_offset == te->size) ||
392 (0 == te->publish_offset % (CHK_PER_INODE * DBLOCK_SIZE)))
393 te->current_depth++;
394 }
395 else
396 {
397 if ((off == CHK_PER_INODE) || (te->publish_offset == te->size))
398 te->current_depth++;
399 else
400 te->current_depth = 0;
401 }
402 te->in_next = GNUNET_NO;
403}
404
405
406/**
407 * Get the resulting URI from the encoding.
408 *
409 * @param te the tree encoder to clean up
410 * @return uri set to the resulting URI (if encoding finished), NULL otherwise
411 */
412struct GNUNET_FS_Uri *
413GNUNET_FS_tree_encoder_get_uri (struct GNUNET_FS_TreeEncoder *te)
414{
415 if (NULL != te->uri)
416 return GNUNET_FS_uri_dup (te->uri);
417 return NULL;
418}
419
420
421/**
422 * Clean up a tree encoder and return information
423 * about possible errors.
424 *
425 * @param te the tree encoder to clean up
426 * @param emsg set to an error message (if an error occurred
427 * within the tree encoder; if this function is called
428 * prior to completion and prior to an internal error,
429 * both "*emsg" will be set to NULL).
430 */
431void
432GNUNET_FS_tree_encoder_finish (struct GNUNET_FS_TreeEncoder *te,
433 char **emsg)
434{
435 if (NULL != te->reader)
436 {
437 (void) te->reader (te->cls, UINT64_MAX, 0, 0, NULL);
438 te->reader = NULL;
439 }
440 GNUNET_assert (GNUNET_NO == te->in_next);
441 if (NULL != te->uri)
442 GNUNET_FS_uri_destroy (te->uri);
443 if (emsg != NULL)
444 *emsg = te->emsg;
445 else
446 GNUNET_free (te->emsg);
447 GNUNET_free (te->chk_tree);
448 GNUNET_free (te);
449}
450
451
452/* end of fs_tree.c */