2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009 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 2, 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.
21 * @file fs/fs_download.c
22 * @brief download methods
23 * @author Christian Grothoff
27 * - callback signaling
28 * - check if blocks exist already
29 * - location URI suppport (can wait)
30 * - persistence (can wait)
33 #include "gnunet_constants.h"
34 #include "gnunet_fs_service.h"
37 #define DEBUG_DOWNLOAD GNUNET_YES
41 * Schedule the download of the specified
44 * @param dc overall download this block belongs to
45 * @param chk content-hash-key of the block
46 * @param offset offset of the block in the file
47 * (for IBlocks, the offset is the lowest
48 * offset of any DBlock in the subtree under
50 * @param depth depth of the block, 0 is the root of the tree
53 schedule_block_download (struct GNUNET_FS_DownloadContext *dc,
54 const struct ContentHashKey *chk,
58 struct DownloadRequest *sm;
60 // FIXME: check if block exists on disk!
61 sm = GNUNET_malloc (sizeof (struct DownloadRequest));
65 sm->is_pending = GNUNET_YES;
66 sm->next = dc->pending;
68 GNUNET_CONTAINER_multihashmap_put (dc->active,
71 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
76 * We've lost our connection with the FS service.
77 * Re-establish it and re-transmit all of our
80 * @param dc download context that is having trouble
83 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
87 * Process a search result.
89 * @param sc our search context
90 * @param type type of the result
91 * @param data the (encrypted) response
92 * @param size size of data
95 process_result (struct GNUNET_FS_DownloadContext *dc,
100 GNUNET_HashCode query;
101 struct DownloadRequest *sm;
102 struct GNUNET_CRYPTO_AesSessionKey skey;
103 struct GNUNET_CRYPTO_AesInitializationVector iv;
106 GNUNET_CRYPTO_hash (data, size, &query);
107 sm = GNUNET_CONTAINER_multihashmap_get (dc->active,
114 GNUNET_assert (GNUNET_YES ==
115 GNUNET_CONTAINER_multihashmap_remove (dc->active,
118 GNUNET_CRYPTO_hash_to_aes_key (&sm->chk.key, &skey, &iv);
119 GNUNET_CRYPTO_aes_decrypt (data,
124 // FIXME: save to disk
125 // FIXME: make persistent
126 // FIXME: call progress callback
127 // FIXME: trigger next block (if applicable)
132 * Type of a function to call when we receive a message
136 * @param msg message received, NULL on timeout or fatal error
139 receive_results (void *cls,
140 const struct GNUNET_MessageHeader * msg)
142 struct GNUNET_FS_DownloadContext *dc = cls;
143 const struct ContentMessage *cm;
146 if ( (NULL == msg) ||
147 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_CONTENT) ||
148 (ntohs (msg->size) <= sizeof (struct ContentMessage)) )
153 msize = ntohs (msg->size);
154 cm = (const struct ContentMessage*) msg;
158 msize - sizeof (struct ContentMessage));
159 /* continue receiving */
160 GNUNET_CLIENT_receive (dc->client,
163 GNUNET_TIME_UNIT_FOREVER_REL);
169 * We're ready to transmit a search request to the
170 * file-sharing service. Do it. If there is
171 * more than one request pending, try to send
172 * multiple or request another transmission.
175 * @param size number of bytes available in buf
176 * @param buf where the callee should write the message
177 * @return number of bytes written to buf
180 transmit_download_request (void *cls,
184 struct GNUNET_FS_DownloadContext *dc = cls;
186 struct SearchMessage *sm;
193 GNUNET_assert (size >= sizeof (struct SearchMessage));
196 while ( (dc->pending == NULL) &&
197 (size > msize + sizeof (struct SearchMessage)) )
199 memset (sm, 0, sizeof (struct SearchMessage));
200 sm->header.size = htons (sizeof (struct SearchMessage));
201 sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
202 sm->anonymity_level = htonl (dc->anonymity);
203 sm->target = dc->target.hashPubKey;
204 sm->query = dc->pending->chk.query;
205 dc->pending->is_pending = GNUNET_NO;
206 dc->pending = dc->pending->next;
207 msize += sizeof (struct SearchMessage);
215 * Reconnect to the FS service and transmit
218 * @param cls our download context
222 do_reconnect (void *cls,
223 const struct GNUNET_SCHEDULER_TaskContext *tc)
225 struct GNUNET_FS_DownloadContext *dc = cls;
226 struct GNUNET_CLIENT_Connection *client;
228 dc->task = GNUNET_SCHEDULER_NO_TASK;
229 client = GNUNET_CLIENT_connect (dc->h->sched,
238 GNUNET_CLIENT_notify_transmit_ready (client,
239 sizeof (struct SearchMessage),
240 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
241 &transmit_download_request,
243 GNUNET_CLIENT_receive (client,
246 GNUNET_TIME_UNIT_FOREVER_REL);
251 * Add entries that are not yet pending back to
254 * @param cls our download context
256 * @param entry entry of type "struct DownloadRequest"
260 retry_entry (void *cls,
261 const GNUNET_HashCode *key,
264 struct GNUNET_FS_DownloadContext *dc = cls;
265 struct DownloadRequest *dr = entry;
267 if (! dr->is_pending)
269 dr->next = dc->pending;
270 dr->is_pending = GNUNET_YES;
278 * We've lost our connection with the FS service.
279 * Re-establish it and re-transmit all of our
282 * @param dc download context that is having trouble
285 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
288 if (NULL != dc->client)
290 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
293 GNUNET_CLIENT_disconnect (dc->client);
297 = GNUNET_SCHEDULER_add_delayed (dc->h->sched,
299 GNUNET_SCHEDULER_PRIORITY_IDLE,
300 GNUNET_SCHEDULER_NO_TASK,
301 GNUNET_TIME_UNIT_SECONDS,
308 * Download parts of a file. Note that this will store
309 * the blocks at the respective offset in the given file. Also, the
310 * download is still using the blocking of the underlying FS
311 * encoding. As a result, the download may *write* outside of the
312 * given boundaries (if offset and length do not match the 32k FS
313 * block boundaries). <p>
315 * This function should be used to focus a download towards a
316 * particular portion of the file (optimization), not to strictly
317 * limit the download to exactly those bytes.
319 * @param h handle to the file sharing subsystem
320 * @param uri the URI of the file (determines what to download); CHK or LOC URI
321 * @param filename where to store the file, maybe NULL (then no file is
322 * created on disk and data must be grabbed from the callbacks)
323 * @param offset at what offset should we start the download (typically 0)
324 * @param length how many bytes should be downloaded starting at offset
325 * @param anonymity anonymity level to use for the download
326 * @param options various options
327 * @param parent parent download to associate this download with (use NULL
328 * for top-level downloads; useful for manually-triggered recursive downloads)
329 * @return context that can be used to control this download
331 struct GNUNET_FS_DownloadContext *
332 GNUNET_FS_file_download_start (struct GNUNET_FS_Handle *h,
333 const struct GNUNET_FS_Uri *uri,
334 const char *filename,
338 enum GNUNET_FS_DownloadOptions options,
339 struct GNUNET_FS_DownloadContext *parent)
341 struct GNUNET_FS_DownloadContext *dc;
342 struct GNUNET_CLIENT_Connection *client;
344 client = GNUNET_CLIENT_connect (h->sched,
349 // FIXME: add support for "loc" URIs!
350 GNUNET_assert (GNUNET_FS_uri_test_chk (uri));
351 if ( (dc->offset + dc->length < dc->offset) ||
352 (dc->offset + dc->length > uri->data.chk.file_length) )
357 dc = GNUNET_malloc (sizeof(struct GNUNET_FS_DownloadContext));
361 dc->uri = GNUNET_FS_uri_dup (uri);
362 if (NULL != filename)
364 dc->filename = GNUNET_strdup (filename);
365 dc->handle = GNUNET_DISK_file_open (filename,
366 GNUNET_DISK_OPEN_READWRITE |
367 GNUNET_DISK_OPEN_CREATE,
368 GNUNET_DISK_PERM_USER_READ |
369 GNUNET_DISK_PERM_USER_WRITE |
370 GNUNET_DISK_PERM_GROUP_READ |
371 GNUNET_DISK_PERM_OTHER_READ);
372 if (dc->handle == NULL)
374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
375 _("Download failed: could not open file `%s': %s\n"),
378 GNUNET_FS_uri_destroy (dc->uri);
379 GNUNET_free (dc->filename);
380 GNUNET_CLIENT_disconnect (dc->client);
385 // FIXME: set "dc->target" for LOC uris!
388 dc->anonymity = anonymity;
389 dc->options = options;
390 dc->active = GNUNET_CONTAINER_multihashmap_create (1 + (length / DBLOCK_SIZE));
391 // FIXME: calculate tree depth
392 // FIXME: make persistent
393 schedule_block_download (dc,
394 &dc->uri->data.chk.chk,
397 GNUNET_CLIENT_notify_transmit_ready (client,
398 sizeof (struct SearchMessage),
399 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
400 &transmit_download_request,
402 GNUNET_CLIENT_receive (client,
405 GNUNET_TIME_UNIT_FOREVER_REL);
406 // FIXME: signal download start
412 * Free entries in the map.
414 * @param cls unused (NULL)
416 * @param entry entry of type "struct DownloadRequest" which is freed
420 free_entry (void *cls,
421 const GNUNET_HashCode *key,
430 * Stop a download (aborts if download is incomplete).
432 * @param dc handle for the download
433 * @param do_delete delete files of incomplete downloads
436 GNUNET_FS_file_download_stop (struct GNUNET_FS_DownloadContext *dc,
439 // FIXME: make unpersistent
440 // FIXME: signal download end
442 if (GNUNET_SCHEDULER_NO_TASK != dc->task)
443 GNUNET_SCHEDULER_cancel (dc->h->sched,
445 if (NULL != dc->client)
446 GNUNET_CLIENT_disconnect (dc->client);
447 GNUNET_CONTAINER_multihashmap_iterate (dc->active,
450 GNUNET_CONTAINER_multihashmap_destroy (dc->active);
451 if (dc->filename != NULL)
453 GNUNET_DISK_file_close (dc->handle);
454 if ( (dc->completed != dc->length) &&
455 (GNUNET_YES == do_delete) )
457 if (0 != UNLINK (dc->filename))
458 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
462 GNUNET_free (dc->filename);
464 GNUNET_FS_uri_destroy (dc->uri);
488 * Compute how many bytes of data are stored in
492 get_node_size (const struct Node *node)
496 unsigned long long rsize;
497 unsigned long long spos;
498 unsigned long long epos;
500 GNUNET_GE_ASSERT (node->ctx->ectx, node->offset < node->ctx->total);
501 if (node->level == 0)
503 ret = GNUNET_ECRS_DBLOCK_SIZE;
504 if (node->offset + (unsigned long long) ret > node->ctx->total)
505 ret = (unsigned int) (node->ctx->total - node->offset);
507 GNUNET_GE_LOG (node->ctx->rm->ectx,
508 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
509 "Node at offset %llu and level %d has size %u\n",
510 node->offset, node->level, ret);
514 rsize = GNUNET_ECRS_DBLOCK_SIZE;
515 for (i = 0; i < node->level - 1; i++)
516 rsize *= GNUNET_ECRS_CHK_PER_INODE;
517 spos = rsize * (node->offset / sizeof (GNUNET_EC_ContentHashKey));
518 epos = spos + rsize * GNUNET_ECRS_CHK_PER_INODE;
519 if (epos > node->ctx->total)
520 epos = node->ctx->total;
521 ret = (epos - spos) / rsize;
522 if (ret * rsize < epos - spos)
523 ret++; /* need to round up! */
525 GNUNET_GE_LOG (node->ctx->rm->ectx,
526 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
527 "Node at offset %llu and level %d has size %u\n",
528 node->offset, node->level,
529 ret * sizeof (GNUNET_EC_ContentHashKey));
531 return ret * sizeof (GNUNET_EC_ContentHashKey);
535 * Check if self block is already present on the drive. If the block
536 * is a dblock and present, the ProgressModel is notified. If the
537 * block is present and it is an iblock, downloading the children is
540 * Also checks if the block is within the range of blocks
541 * that we are supposed to download. If not, the method
542 * returns as if the block is present but does NOT signal
545 * @param node that is checked for presence
546 * @return GNUNET_YES if present, GNUNET_NO if not.
549 check_node_present (const struct Node *node)
557 size = get_node_size (node);
558 /* first check if node is within range.
559 For now, keeping it simple, we only do
560 this for level-0 nodes */
561 if ((node->level == 0) &&
562 ((node->offset + size < node->ctx->offset) ||
563 (node->offset >= node->ctx->offset + node->ctx->length)))
565 data = GNUNET_malloc (size);
567 res = read_from_files (node->ctx, node->level, node->offset, data, size);
570 GNUNET_hash (data, size, &hc);
571 if (0 == memcmp (&hc, &node->chk.key, sizeof (GNUNET_HashCode)))
573 notify_client_about_progress (node, data, size);
575 iblock_download_children (node, data, size);
584 * DOWNLOAD children of this GNUNET_EC_IBlock.
586 * @param node the node that should be downloaded
589 iblock_download_children (const struct Node *node,
590 const char *data, unsigned int size)
592 struct GNUNET_GE_Context *ectx = node->ctx->ectx;
595 unsigned int childcount;
596 const GNUNET_EC_ContentHashKey *chks;
597 unsigned int levelSize;
598 unsigned long long baseOffset;
600 GNUNET_GE_ASSERT (ectx, node->level > 0);
601 childcount = size / sizeof (GNUNET_EC_ContentHashKey);
602 if (size != childcount * sizeof (GNUNET_EC_ContentHashKey))
604 GNUNET_GE_BREAK (ectx, 0);
607 if (node->level == 1)
609 levelSize = GNUNET_ECRS_DBLOCK_SIZE;
611 node->offset / sizeof (GNUNET_EC_ContentHashKey) *
612 GNUNET_ECRS_DBLOCK_SIZE;
617 sizeof (GNUNET_EC_ContentHashKey) * GNUNET_ECRS_CHK_PER_INODE;
618 baseOffset = node->offset * GNUNET_ECRS_CHK_PER_INODE;
620 chks = (const GNUNET_EC_ContentHashKey *) data;
621 for (i = 0; i < childcount; i++)
623 child = GNUNET_malloc (sizeof (struct Node));
624 child->ctx = node->ctx;
625 child->chk = chks[i];
626 child->offset = baseOffset + i * levelSize;
627 GNUNET_GE_ASSERT (ectx, child->offset < node->ctx->total);
628 child->level = node->level - 1;
629 GNUNET_GE_ASSERT (ectx, (child->level != 0) ||
630 ((child->offset % GNUNET_ECRS_DBLOCK_SIZE) == 0));
631 if (GNUNET_NO == check_node_present (child))
634 GNUNET_free (child); /* done already! */
641 /* end of fs_download.c */