-fix
[oweals/gnunet.git] / src / fs / fs_download.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001-2012 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20 /**
21  * @file fs/fs_download.c
22  * @brief download methods
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_constants.h"
27 #include "gnunet_fs_service.h"
28 #include "fs_api.h"
29 #include "fs_tree.h"
30
31
32 /**
33  * Determine if the given download (options and meta data) should cause
34  * use to try to do a recursive download.
35  */
36 static int
37 is_recursive_download (struct GNUNET_FS_DownloadContext *dc)
38 {
39   return (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE)) &&
40       ((GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (dc->meta)) ||
41        ((dc->meta == NULL) &&
42         ((NULL == dc->filename) ||
43          ((strlen (dc->filename) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
44           (NULL !=
45            strstr (dc->filename + strlen (dc->filename) -
46                    strlen (GNUNET_FS_DIRECTORY_EXT),
47                    GNUNET_FS_DIRECTORY_EXT))))));
48 }
49
50
51 /**
52  * We're storing the IBLOCKS after the DBLOCKS on disk (so that we
53  * only have to truncate the file once we're done).
54  *
55  * Given the offset of a block (with respect to the DBLOCKS) and its
56  * depth, return the offset where we would store this block in the
57  * file.
58  *
59  * @param fsize overall file size
60  * @param off offset of the block in the file
61  * @param depth depth of the block in the tree, 0 for DBLOCK
62  * @return off for DBLOCKS (depth == treedepth),
63  *         otherwise an offset past the end
64  *         of the file that does not overlap
65  *         with the range for any other block
66  */
67 static uint64_t
68 compute_disk_offset (uint64_t fsize, uint64_t off, unsigned int depth)
69 {
70   unsigned int i;
71   uint64_t lsize;               /* what is the size of all IBlocks for depth "i"? */
72   uint64_t loff;                /* where do IBlocks for depth "i" start? */
73   unsigned int ioff;            /* which IBlock corresponds to "off" at depth "i"? */
74
75   if (depth == 0)
76     return off;
77   /* first IBlocks start at the end of file, rounded up
78    * to full DBLOCK_SIZE */
79   loff = ((fsize + DBLOCK_SIZE - 1) / DBLOCK_SIZE) * DBLOCK_SIZE;
80   lsize =
81       ((fsize + DBLOCK_SIZE -
82         1) / DBLOCK_SIZE) * sizeof (struct ContentHashKey);
83   GNUNET_assert (0 == (off % DBLOCK_SIZE));
84   ioff = (off / DBLOCK_SIZE);
85   for (i = 1; i < depth; i++)
86   {
87     loff += lsize;
88     lsize = (lsize + CHK_PER_INODE - 1) / CHK_PER_INODE;
89     GNUNET_assert (lsize > 0);
90     GNUNET_assert (0 == (ioff % CHK_PER_INODE));
91     ioff /= CHK_PER_INODE;
92   }
93   return loff + ioff * sizeof (struct ContentHashKey);
94 }
95
96
97 /**
98  * Fill in all of the generic fields for a download event and call the
99  * callback.
100  *
101  * @param pi structure to fill in
102  * @param dc overall download context
103  */
104 void
105 GNUNET_FS_download_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
106                                  struct GNUNET_FS_DownloadContext *dc)
107 {
108   pi->value.download.dc = dc;
109   pi->value.download.cctx = dc->client_info;
110   pi->value.download.pctx =
111       (dc->parent == NULL) ? NULL : dc->parent->client_info;
112   pi->value.download.sctx =
113       (dc->search == NULL) ? NULL : dc->search->client_info;
114   pi->value.download.uri = dc->uri;
115   pi->value.download.filename = dc->filename;
116   pi->value.download.size = dc->length;
117   /* FIXME: Fix duration calculation to account for pauses */
118   pi->value.download.duration =
119       GNUNET_TIME_absolute_get_duration (dc->start_time);
120   pi->value.download.completed = dc->completed;
121   pi->value.download.anonymity = dc->anonymity;
122   pi->value.download.eta =
123       GNUNET_TIME_calculate_eta (dc->start_time, dc->completed, dc->length);
124   pi->value.download.is_active = (dc->client == NULL) ? GNUNET_NO : GNUNET_YES;
125   if (0 == (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
126     dc->client_info = dc->h->upcb (dc->h->upcb_cls, pi);
127   else
128     dc->client_info = GNUNET_FS_search_probe_progress_ (NULL, pi);
129 }
130
131
132 /**
133  * We're ready to transmit a search request to the
134  * file-sharing service.  Do it.  If there is
135  * more than one request pending, try to send
136  * multiple or request another transmission.
137  *
138  * @param cls closure
139  * @param size number of bytes available in buf
140  * @param buf where the callee should write the message
141  * @return number of bytes written to buf
142  */
143 static size_t
144 transmit_download_request (void *cls, size_t size, void *buf);
145
146
147 /**
148  * Closure for iterator processing results.
149  */
150 struct ProcessResultClosure
151 {
152
153   /**
154    * Hash of data.
155    */
156   GNUNET_HashCode query;
157
158   /**
159    * Data found in P2P network.
160    */
161   const void *data;
162
163   /**
164    * Our download context.
165    */
166   struct GNUNET_FS_DownloadContext *dc;
167
168   /**
169    * Number of bytes in data.
170    */
171   size_t size;
172
173   /**
174    * Type of data.
175    */
176   enum GNUNET_BLOCK_Type type;
177
178   /**
179    * Flag to indicate if this block should be stored on disk.
180    */
181   int do_store;
182
183   /**
184    * When did we last transmit the request?
185    */
186   struct GNUNET_TIME_Absolute last_transmission;
187
188 };
189
190
191 /**
192  * Iterator over entries in the pending requests in the 'active' map for the
193  * reply that we just got.
194  *
195  * @param cls closure (our 'struct ProcessResultClosure')
196  * @param key query for the given value / request
197  * @param value value in the hash map (a 'struct DownloadRequest')
198  * @return GNUNET_YES (we should continue to iterate); unless serious error
199  */
200 static int
201 process_result_with_request (void *cls, const GNUNET_HashCode * key,
202                              void *value);
203
204
205 /**
206  * We've found a matching block without downloading it.
207  * Encrypt it and pass it to our "receive" function as
208  * if we had received it from the network.
209  *
210  * @param dc download in question
211  * @param chk request this relates to
212  * @param dr request details
213  * @param block plaintext data matching request
214  * @param len number of bytes in block
215  * @param do_store should we still store the block on disk?
216  * @return GNUNET_OK on success
217  */
218 static int
219 encrypt_existing_match (struct GNUNET_FS_DownloadContext *dc,
220                         const struct ContentHashKey *chk,
221                         struct DownloadRequest *dr, const char *block,
222                         size_t len, int do_store)
223 {
224   struct ProcessResultClosure prc;
225   char enc[len];
226   struct GNUNET_CRYPTO_AesSessionKey sk;
227   struct GNUNET_CRYPTO_AesInitializationVector iv;
228   GNUNET_HashCode query;
229
230   GNUNET_CRYPTO_hash_to_aes_key (&chk->key, &sk, &iv);
231   if (-1 == GNUNET_CRYPTO_aes_encrypt (block, len, &sk, &iv, enc))
232   {
233     GNUNET_break (0);
234     return GNUNET_SYSERR;
235   }
236   GNUNET_CRYPTO_hash (enc, len, &query);
237   if (0 != memcmp (&query, &chk->query, sizeof (GNUNET_HashCode)))
238   {
239     GNUNET_break_op (0);
240     return GNUNET_SYSERR;
241   }
242   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243               "Matching %u byte block for `%s' at offset %llu already present, no need for download!\n",
244               (unsigned int) len,
245               dc->filename, (unsigned long long) dr->offset);
246   /* already got it! */
247   prc.dc = dc;
248   prc.data = enc;
249   prc.size = len;
250   prc.type =
251       (0 ==
252        dr->depth) ? GNUNET_BLOCK_TYPE_FS_DBLOCK : GNUNET_BLOCK_TYPE_FS_IBLOCK;
253   prc.query = chk->query;
254   prc.do_store = do_store;
255   prc.last_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
256   process_result_with_request (&prc, &chk->key, dr);
257   return GNUNET_OK;
258 }
259
260
261 /**
262  * We've lost our connection with the FS service.
263  * Re-establish it and re-transmit all of our
264  * pending requests.
265  *
266  * @param dc download context that is having trouble
267  */
268 static void
269 try_reconnect (struct GNUNET_FS_DownloadContext *dc);
270
271
272 /**
273  * We found an entry in a directory.  Check if the respective child
274  * already exists and if not create the respective child download.
275  *
276  * @param cls the parent download
277  * @param filename name of the file in the directory
278  * @param uri URI of the file (CHK or LOC)
279  * @param meta meta data of the file
280  * @param length number of bytes in data
281  * @param data contents of the file (or NULL if they were not inlined)
282  */
283 static void
284 trigger_recursive_download (void *cls, const char *filename,
285                             const struct GNUNET_FS_Uri *uri,
286                             const struct GNUNET_CONTAINER_MetaData *meta,
287                             size_t length, const void *data);
288
289
290 /**
291  * We're done downloading a directory.  Open the file and
292  * trigger all of the (remaining) child downloads.
293  *
294  * @param dc context of download that just completed
295  */
296 static void
297 full_recursive_download (struct GNUNET_FS_DownloadContext *dc)
298 {
299   size_t size;
300   uint64_t size64;
301   void *data;
302   struct GNUNET_DISK_FileHandle *h;
303   struct GNUNET_DISK_MapHandle *m;
304
305   size64 = GNUNET_FS_uri_chk_get_file_size (dc->uri);
306   size = (size_t) size64;
307   if (size64 != (uint64_t) size)
308   {
309     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310                 _
311                 ("Recursive downloads of directories larger than 4 GB are not supported on 32-bit systems\n"));
312     return;
313   }
314   if (dc->filename != NULL)
315   {
316     h = GNUNET_DISK_file_open (dc->filename, GNUNET_DISK_OPEN_READ,
317                                GNUNET_DISK_PERM_NONE);
318   }
319   else
320   {
321     GNUNET_assert (dc->temp_filename != NULL);
322     h = GNUNET_DISK_file_open (dc->temp_filename, GNUNET_DISK_OPEN_READ,
323                                GNUNET_DISK_PERM_NONE);
324   }
325   if (h == NULL)
326     return;                     /* oops */
327   data = GNUNET_DISK_file_map (h, &m, GNUNET_DISK_MAP_TYPE_READ, size);
328   if (data == NULL)
329   {
330     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
331                 _("Directory too large for system address space\n"));
332   }
333   else
334   {
335     GNUNET_FS_directory_list_contents (size, data, 0,
336                                        &trigger_recursive_download, dc);
337     GNUNET_DISK_file_unmap (m);
338   }
339   GNUNET_DISK_file_close (h);
340   if (dc->filename == NULL)
341   {
342     if (0 != UNLINK (dc->temp_filename))
343       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
344                                 dc->temp_filename);
345     GNUNET_free (dc->temp_filename);
346     dc->temp_filename = NULL;
347   }
348 }
349
350
351 /**
352  * Check if all child-downloads have completed (or trigger them if
353  * necessary) and once we're completely done, signal completion (and
354  * possibly recurse to parent).  This function MUST be called when the
355  * download of a file itself is done or when the download of a file is
356  * done and then later a direct child download has completed (and
357  * hence this download may complete itself).
358  *
359  * @param dc download to check for completion of children
360  */
361 static void
362 check_completed (struct GNUNET_FS_DownloadContext *dc)
363 {
364   struct GNUNET_FS_ProgressInfo pi;
365   struct GNUNET_FS_DownloadContext *pos;
366
367   /* first, check if we need to download children */
368   if ((dc->child_head == NULL) && (is_recursive_download (dc)))
369     full_recursive_download (dc);
370   /* then, check if children are done already */
371   pos = dc->child_head;
372   while (pos != NULL)
373   {
374     if ((pos->emsg == NULL) && (pos->completed < pos->length))
375       return;                   /* not done yet */
376     if ((pos->child_head != NULL) && (pos->has_finished != GNUNET_YES))
377       return;                   /* not transitively done yet */
378     pos = pos->next;
379   }
380   /* All of our children are done, so mark this download done */
381   dc->has_finished = GNUNET_YES;
382   if (dc->job_queue != NULL)
383   {
384     GNUNET_FS_dequeue_ (dc->job_queue);
385     dc->job_queue = NULL;
386   }
387   if (GNUNET_SCHEDULER_NO_TASK != dc->task)
388   {
389     GNUNET_SCHEDULER_cancel (dc->task);
390     dc->task = GNUNET_SCHEDULER_NO_TASK;
391   }
392   if (dc->rfh != NULL)
393   {
394     GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (dc->rfh));
395     dc->rfh = NULL;
396   }
397   GNUNET_FS_download_sync_ (dc);
398
399   /* signal completion */
400   pi.status = GNUNET_FS_STATUS_DOWNLOAD_COMPLETED;
401   GNUNET_FS_download_make_status_ (&pi, dc);
402
403   /* let parent know */
404   if (dc->parent != NULL)
405     check_completed (dc->parent);
406 }
407
408
409 /**
410  * We got a block of plaintext data (from the meta data).
411  * Try it for upward reconstruction of the data.  On success,
412  * the top-level block will move to state BRS_DOWNLOAD_UP.
413  *
414  * @param dc context for the download
415  * @param dr download request to match against
416  * @param data plaintext data, starting from the beginning of the file
417  * @param data_len number of bytes in data
418  */
419 static void
420 try_match_block (struct GNUNET_FS_DownloadContext *dc,
421                  struct DownloadRequest *dr, const char *data, size_t data_len)
422 {
423   struct GNUNET_FS_ProgressInfo pi;
424   unsigned int i;
425   char enc[DBLOCK_SIZE];
426   struct ContentHashKey chks[CHK_PER_INODE];
427   struct ContentHashKey in_chk;
428   struct GNUNET_CRYPTO_AesSessionKey sk;
429   struct GNUNET_CRYPTO_AesInitializationVector iv;
430   size_t dlen;
431   struct DownloadRequest *drc;
432   struct GNUNET_DISK_FileHandle *fh;
433   int complete;
434   const char *fn;
435   const char *odata;
436   size_t odata_len;
437
438   odata = data;
439   odata_len = data_len;
440   if (BRS_DOWNLOAD_UP == dr->state)
441     return;
442   if (dr->depth > 0)
443   {
444     complete = GNUNET_YES;
445     for (i = 0; i < dr->num_children; i++)
446     {
447       drc = dr->children[i];
448       try_match_block (dc, drc, data, data_len);
449       if (drc->state != BRS_RECONSTRUCT_META_UP)
450         complete = GNUNET_NO;
451       else
452         chks[i] = drc->chk;
453     }
454     if (GNUNET_YES != complete)
455       return;
456     data = (const char *) chks;
457     dlen = dr->num_children * sizeof (struct ContentHashKey);
458   }
459   else
460   {
461     if (dr->offset > data_len)
462       return;                   /* oops */
463     dlen = GNUNET_MIN (data_len - dr->offset, DBLOCK_SIZE);
464   }
465   GNUNET_CRYPTO_hash (&data[dr->offset], dlen, &in_chk.key);
466   GNUNET_CRYPTO_hash_to_aes_key (&in_chk.key, &sk, &iv);
467   if (-1 == GNUNET_CRYPTO_aes_encrypt (&data[dr->offset], dlen, &sk, &iv, enc))
468   {
469     GNUNET_break (0);
470     return;
471   }
472   GNUNET_CRYPTO_hash (enc, dlen, &in_chk.query);
473   switch (dr->state)
474   {
475   case BRS_INIT:
476     dr->chk = in_chk;
477     dr->state = BRS_RECONSTRUCT_META_UP;
478     break;
479   case BRS_CHK_SET:
480     if (0 != memcmp (&in_chk, &dr->chk, sizeof (struct ContentHashKey)))
481     {
482       /* other peer provided bogus meta data */
483       GNUNET_break_op (0);
484       break;
485     }
486     /* write block to disk */
487     fn = dc->filename != NULL ? dc->filename : dc->temp_filename;
488     fh = GNUNET_DISK_file_open (fn,
489                                 GNUNET_DISK_OPEN_READWRITE |
490                                 GNUNET_DISK_OPEN_CREATE |
491                                 GNUNET_DISK_OPEN_TRUNCATE,
492                                 GNUNET_DISK_PERM_USER_READ |
493                                 GNUNET_DISK_PERM_USER_WRITE |
494                                 GNUNET_DISK_PERM_GROUP_READ |
495                                 GNUNET_DISK_PERM_OTHER_READ);
496     if (fh == NULL)
497     {
498       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
499       GNUNET_asprintf (&dc->emsg, _("Failed to open file `%s' for writing"),
500                        fn);
501       GNUNET_DISK_file_close (fh);
502       dr->state = BRS_ERROR;
503       pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
504       pi.value.download.specifics.error.message = dc->emsg;
505       GNUNET_FS_download_make_status_ (&pi, dc);
506       return;
507     }
508     if (data_len != GNUNET_DISK_file_write (fh, odata, odata_len))
509     {
510       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "write", fn);
511       GNUNET_asprintf (&dc->emsg, _("Failed to open file `%s' for writing"),
512                        fn);
513       GNUNET_DISK_file_close (fh);
514       dr->state = BRS_ERROR;
515       pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
516       pi.value.download.specifics.error.message = dc->emsg;
517       GNUNET_FS_download_make_status_ (&pi, dc);
518       return;
519     }
520     GNUNET_DISK_file_close (fh);
521     /* signal success */
522     dr->state = BRS_DOWNLOAD_UP;
523     dc->completed = dc->length;
524     GNUNET_FS_download_sync_ (dc);
525     pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
526     pi.value.download.specifics.progress.data = data;
527     pi.value.download.specifics.progress.offset = 0;
528     pi.value.download.specifics.progress.data_len = dlen;
529     pi.value.download.specifics.progress.depth = 0;
530     pi.value.download.specifics.progress.trust_offered = 0;
531     pi.value.download.specifics.progress.block_download_duration = GNUNET_TIME_UNIT_ZERO;
532     GNUNET_FS_download_make_status_ (&pi, dc);
533     if ((NULL != dc->filename) &&
534         (0 !=
535          truncate (dc->filename,
536                    GNUNET_ntohll (dc->uri->data.chk.file_length))))
537       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
538                                 dc->filename);
539     check_completed (dc);
540     break;
541   default:
542     /* how did we get here? */
543     GNUNET_break (0);
544     break;
545   }
546 }
547
548
549 /**
550  * Type of a function that libextractor calls for each
551  * meta data item found.  If we find full data meta data,
552  * call 'try_match_block' on it.
553  *
554  * @param cls our 'struct GNUNET_FS_DownloadContext*'
555  * @param plugin_name name of the plugin that produced this value;
556  *        special values can be used (i.e. '&lt;zlib&gt;' for zlib being
557  *        used in the main libextractor library and yielding
558  *        meta data).
559  * @param type libextractor-type describing the meta data
560  * @param format basic format information about data
561  * @param data_mime_type mime-type of data (not of the original file);
562  *        can be NULL (if mime-type is not known)
563  * @param data actual meta-data found
564  * @param data_len number of bytes in data
565  * @return 0 to continue extracting, 1 to abort
566  */
567 static int
568 match_full_data (void *cls, const char *plugin_name,
569                  enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
570                  const char *data_mime_type, const char *data, size_t data_len)
571 {
572   struct GNUNET_FS_DownloadContext *dc = cls;
573
574   if (type != EXTRACTOR_METATYPE_GNUNET_FULL_DATA)
575     return 0;
576   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found %u bytes of FD!\n",
577               (unsigned int) data_len);
578   if (GNUNET_FS_uri_chk_get_file_size (dc->uri) != data_len)
579   {
580     GNUNET_break_op (0);
581     return 1;                   /* bogus meta data */
582   }
583   try_match_block (dc, dc->top_request, data, data_len);
584   return 1;
585 }
586
587
588 /**
589  * Set the state of the given download request to
590  * BRS_DOWNLOAD_UP and propagate it up the tree.
591  *
592  * @param dr download request that is done
593  */
594 static void
595 propagate_up (struct DownloadRequest *dr)
596 {
597   unsigned int i;
598
599   do
600   {
601     dr->state = BRS_DOWNLOAD_UP;
602     dr = dr->parent;
603     if (dr == NULL)
604       break;
605     for (i = 0; i < dr->num_children; i++)
606       if (dr->children[i]->state != BRS_DOWNLOAD_UP)
607         break;
608   }
609   while (i == dr->num_children);
610 }
611
612
613 /**
614  * Try top-down reconstruction.  Before, the given request node
615  * must have the state BRS_CHK_SET.  Afterwards, more nodes may
616  * have that state or advanced to BRS_DOWNLOAD_DOWN or even
617  * BRS_DOWNLOAD_UP.  It is also possible to get BRS_ERROR on the
618  * top level.
619  *
620  * @param dc overall download this block belongs to
621  * @param dr block to reconstruct
622  */
623 static void
624 try_top_down_reconstruction (struct GNUNET_FS_DownloadContext *dc,
625                              struct DownloadRequest *dr)
626 {
627   uint64_t off;
628   char block[DBLOCK_SIZE];
629   GNUNET_HashCode key;
630   uint64_t total;
631   size_t len;
632   unsigned int i;
633   struct DownloadRequest *drc;
634   uint64_t child_block_size;
635   const struct ContentHashKey *chks;
636   int up_done;
637
638   GNUNET_assert (dc->rfh != NULL);
639   GNUNET_assert (dr->state == BRS_CHK_SET);
640   total = GNUNET_FS_uri_chk_get_file_size (dc->uri);
641   GNUNET_assert (dr->depth < dc->treedepth);
642   len = GNUNET_FS_tree_calculate_block_size (total, dr->offset, dr->depth);
643   GNUNET_assert (len <= DBLOCK_SIZE);
644   off = compute_disk_offset (total, dr->offset, dr->depth);
645   if (dc->old_file_size < off + len)
646     return;                     /* failure */
647   if (off != GNUNET_DISK_file_seek (dc->rfh, off, GNUNET_DISK_SEEK_SET))
648   {
649     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "seek", dc->filename);
650     return;                     /* failure */
651   }
652   if (len != GNUNET_DISK_file_read (dc->rfh, block, len))
653   {
654     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", dc->filename);
655     return;                     /* failure */
656   }
657   GNUNET_CRYPTO_hash (block, len, &key);
658   if (0 != memcmp (&key, &dr->chk.key, sizeof (GNUNET_HashCode)))
659     return;                     /* mismatch */
660   if (GNUNET_OK !=
661       encrypt_existing_match (dc, &dr->chk, dr, block, len, GNUNET_NO))
662   {
663     /* hash matches but encrypted block does not, really bad */
664     dr->state = BRS_ERROR;
665     /* propagate up */
666     while (dr->parent != NULL)
667     {
668       dr = dr->parent;
669       dr->state = BRS_ERROR;
670     }
671     return;
672   }
673   /* block matches */
674   dr->state = BRS_DOWNLOAD_DOWN;
675
676   /* set CHKs for children */
677   up_done = GNUNET_YES;
678   chks = (const struct ContentHashKey *) block;
679   for (i = 0; i < dr->num_children; i++)
680   {
681     drc = dr->children[i];
682     GNUNET_assert (drc->offset >= dr->offset);
683     child_block_size = GNUNET_FS_tree_compute_tree_size (drc->depth);
684     GNUNET_assert (0 == (drc->offset - dr->offset) % child_block_size);     
685     if (drc->state == BRS_INIT)
686     {
687       drc->state = BRS_CHK_SET;
688       drc->chk = chks[drc->chk_idx];
689       try_top_down_reconstruction (dc, drc);
690     }
691     if (drc->state != BRS_DOWNLOAD_UP)
692       up_done = GNUNET_NO;      /* children not all done */
693   }
694   if (up_done == GNUNET_YES)
695     propagate_up (dr);          /* children all done (or no children...) */
696 }
697
698
699 /**
700  * Schedule the download of the specified block in the tree.
701  *
702  * @param dc overall download this block belongs to
703  * @param dr request to schedule
704  */
705 static void
706 schedule_block_download (struct GNUNET_FS_DownloadContext *dc,
707                          struct DownloadRequest *dr)
708 {
709   unsigned int i;
710
711   switch (dr->state)
712   {
713   case BRS_INIT:
714     GNUNET_assert (0);
715     break;
716   case BRS_RECONSTRUCT_DOWN:
717     GNUNET_assert (0);
718     break;
719   case BRS_RECONSTRUCT_META_UP:
720     GNUNET_assert (0);
721     break;
722   case BRS_RECONSTRUCT_UP:
723     GNUNET_assert (0);
724     break;
725   case BRS_CHK_SET:
726     /* normal case, start download */
727     break;
728   case BRS_DOWNLOAD_DOWN:
729     for (i = 0; i < dr->num_children; i++)
730       schedule_block_download (dc, dr->children[i]);
731     return;
732   case BRS_DOWNLOAD_UP:
733     /* We're done! */
734     return;
735   case BRS_ERROR:
736     GNUNET_break (0);
737     return;
738   }
739   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740               "Scheduling download at offset %llu and depth %u for `%s'\n",
741               (unsigned long long) dr->offset, dr->depth,
742               GNUNET_h2s (&dr->chk.query));
743   if (GNUNET_NO !=
744       GNUNET_CONTAINER_multihashmap_contains_value (dc->active, &dr->chk.query,
745                                                     dr))
746     return;                     /* already active */
747   GNUNET_CONTAINER_multihashmap_put (dc->active, &dr->chk.query, dr,
748                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
749   if (dc->client == NULL)
750     return;                     /* download not active */
751   GNUNET_CONTAINER_DLL_insert (dc->pending_head, dc->pending_tail, dr);
752   dr->is_pending = GNUNET_YES;
753   if (NULL == dc->th)
754     dc->th =
755         GNUNET_CLIENT_notify_transmit_ready (dc->client,
756                                              sizeof (struct SearchMessage),
757                                              GNUNET_CONSTANTS_SERVICE_TIMEOUT,
758                                              GNUNET_NO,
759                                              &transmit_download_request, dc);
760 }
761
762
763 #define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
764
765 /**
766  * We found an entry in a directory.  Check if the respective child
767  * already exists and if not create the respective child download.
768  *
769  * @param cls the parent download
770  * @param filename name of the file in the directory
771  * @param uri URI of the file (CHK or LOC)
772  * @param meta meta data of the file
773  * @param length number of bytes in data
774  * @param data contents of the file (or NULL if they were not inlined)
775  */
776 static void
777 trigger_recursive_download (void *cls, const char *filename,
778                             const struct GNUNET_FS_Uri *uri,
779                             const struct GNUNET_CONTAINER_MetaData *meta,
780                             size_t length, const void *data)
781 {
782   struct GNUNET_FS_DownloadContext *dc = cls;
783   struct GNUNET_FS_DownloadContext *cpos;
784   char *temp_name;
785   char *fn;
786   char *us;
787   char *ext;
788   char *dn;
789   char *pos;
790   char *full_name;
791   char *sfn;
792
793   if (NULL == uri)
794     return;                     /* entry for the directory itself */
795   cpos = dc->child_head;
796   while (cpos != NULL)
797   {
798     if ((GNUNET_FS_uri_test_equal (uri, cpos->uri)) ||
799         ((filename != NULL) && (0 == strcmp (cpos->filename, filename))))
800       break;
801     cpos = cpos->next;
802   }
803   if (cpos != NULL)
804     return;                     /* already exists */
805   fn = NULL;
806   if (NULL == filename)
807   {
808     fn = GNUNET_FS_meta_data_suggest_filename (meta);
809     if (fn == NULL)
810     {
811       us = GNUNET_FS_uri_to_string (uri);
812       fn = GNUNET_strdup (&us[strlen (GNUNET_FS_URI_CHK_PREFIX)]);
813       GNUNET_free (us);
814     }
815     else if (fn[0] == '.')
816     {
817       ext = fn;
818       us = GNUNET_FS_uri_to_string (uri);
819       GNUNET_asprintf (&fn, "%s%s", &us[strlen (GNUNET_FS_URI_CHK_PREFIX)],
820                        ext);
821       GNUNET_free (ext);
822       GNUNET_free (us);
823     }
824     /* change '\' to '/' (this should have happened
825      * during insertion, but malicious peers may
826      * not have done this) */
827     while (NULL != (pos = strstr (fn, "\\")))
828       *pos = '/';
829     /* remove '../' everywhere (again, well-behaved
830      * peers don't do this, but don't trust that
831      * we did not get something nasty) */
832     while (NULL != (pos = strstr (fn, "../")))
833     {
834       pos[0] = '_';
835       pos[1] = '_';
836       pos[2] = '_';
837     }
838     filename = fn;
839   }
840   if (dc->filename == NULL)
841   {
842     full_name = NULL;
843   }
844   else
845   {
846     dn = GNUNET_strdup (dc->filename);
847     GNUNET_break ((strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
848                   (NULL !=
849                    strstr (dn + strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT),
850                            GNUNET_FS_DIRECTORY_EXT)));
851     sfn = GNUNET_strdup (filename);
852     while ((strlen (sfn) > 0) && (filename[strlen (sfn) - 1] == '/'))
853       sfn[strlen (sfn) - 1] = '\0';
854     if ((strlen (dn) >= strlen (GNUNET_FS_DIRECTORY_EXT)) &&
855         (NULL !=
856          strstr (dn + strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT),
857                  GNUNET_FS_DIRECTORY_EXT)))
858       dn[strlen (dn) - strlen (GNUNET_FS_DIRECTORY_EXT)] = '\0';
859     if ((GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta)) &&
860         ((strlen (filename) < strlen (GNUNET_FS_DIRECTORY_EXT)) ||
861          (NULL ==
862           strstr (filename + strlen (filename) -
863                   strlen (GNUNET_FS_DIRECTORY_EXT), GNUNET_FS_DIRECTORY_EXT))))
864     {
865       GNUNET_asprintf (&full_name, "%s%s%s%s", dn, DIR_SEPARATOR_STR, sfn,
866                        GNUNET_FS_DIRECTORY_EXT);
867     }
868     else
869     {
870       GNUNET_asprintf (&full_name, "%s%s%s", dn, DIR_SEPARATOR_STR, sfn);
871     }
872     GNUNET_free (sfn);
873     GNUNET_free (dn);
874   }
875   if ((full_name != NULL) &&
876       (GNUNET_OK != GNUNET_DISK_directory_create_for_file (full_name)))
877   {
878     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
879                 _
880                 ("Failed to create directory for recursive download of `%s'\n"),
881                 full_name);
882     GNUNET_free (full_name);
883     GNUNET_free_non_null (fn);
884     return;
885   }
886
887   temp_name = NULL;
888   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889               "Triggering recursive download of size %llu with %u bytes MD\n",
890               (unsigned long long) GNUNET_FS_uri_chk_get_file_size (uri),
891               (unsigned int)
892               GNUNET_CONTAINER_meta_data_get_serialized_size (meta));
893   GNUNET_FS_download_start (dc->h, uri, meta, full_name, temp_name, 0,
894                             GNUNET_FS_uri_chk_get_file_size (uri),
895                             dc->anonymity, dc->options, NULL, dc);
896   GNUNET_free_non_null (full_name);
897   GNUNET_free_non_null (temp_name);
898   GNUNET_free_non_null (fn);
899 }
900
901
902 /**
903  * (recursively) free download request structure
904  *
905  * @param dr request to free
906  */
907 void
908 GNUNET_FS_free_download_request_ (struct DownloadRequest *dr)
909 {
910   unsigned int i;
911
912   if (dr == NULL)
913     return;
914   for (i = 0; i < dr->num_children; i++)
915     GNUNET_FS_free_download_request_ (dr->children[i]);
916   GNUNET_free_non_null (dr->children);
917   GNUNET_free (dr);
918 }
919
920
921 /**
922  * Iterator over entries in the pending requests in the 'active' map for the
923  * reply that we just got.
924  *
925  * @param cls closure (our 'struct ProcessResultClosure')
926  * @param key query for the given value / request
927  * @param value value in the hash map (a 'struct DownloadRequest')
928  * @return GNUNET_YES (we should continue to iterate); unless serious error
929  */
930 static int
931 process_result_with_request (void *cls, const GNUNET_HashCode * key,
932                              void *value)
933 {
934   struct ProcessResultClosure *prc = cls;
935   struct DownloadRequest *dr = value;
936   struct GNUNET_FS_DownloadContext *dc = prc->dc;
937   struct DownloadRequest *drc;
938   struct GNUNET_DISK_FileHandle *fh = NULL;
939   struct GNUNET_CRYPTO_AesSessionKey skey;
940   struct GNUNET_CRYPTO_AesInitializationVector iv;
941   char pt[prc->size];
942   struct GNUNET_FS_ProgressInfo pi;
943   uint64_t off;
944   size_t bs;
945   size_t app;
946   int i;
947   struct ContentHashKey *chkarr;
948
949   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950               "Received %u byte block `%s' matching pending request at depth %u and offset %llu/%llu\n",
951               (unsigned int) prc->size,
952               GNUNET_h2s (key), dr->depth, (unsigned long long) dr->offset,
953               (unsigned long long) GNUNET_ntohll (dc->uri->data.
954                                                   chk.file_length));
955   bs = GNUNET_FS_tree_calculate_block_size (GNUNET_ntohll
956                                             (dc->uri->data.chk.file_length),
957                                             dr->offset, dr->depth);
958   if (prc->size != bs)
959   {
960     GNUNET_asprintf (&dc->emsg,
961                      _
962                      ("Internal error or bogus download URI (expected %u bytes at depth %u and offset %llu/%llu, got %u bytes)\n"),
963                      bs, dr->depth, (unsigned long long) dr->offset,
964                      (unsigned long long) GNUNET_ntohll (dc->uri->data.
965                                                          chk.file_length),
966                      prc->size);
967     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s", dc->emsg);
968     while (dr->parent != NULL)
969     {
970       dr->state = BRS_ERROR;
971       dr = dr->parent;
972     }
973     dr->state = BRS_ERROR;
974     goto signal_error;
975   }
976
977   (void) GNUNET_CONTAINER_multihashmap_remove (dc->active, &prc->query, dr);
978   if (GNUNET_YES == dr->is_pending)
979   {
980     GNUNET_CONTAINER_DLL_remove (dc->pending_head, dc->pending_tail, dr);
981     dr->is_pending = GNUNET_NO;
982   }
983
984   GNUNET_CRYPTO_hash_to_aes_key (&dr->chk.key, &skey, &iv);
985   if (-1 == GNUNET_CRYPTO_aes_decrypt (prc->data, prc->size, &skey, &iv, pt))
986   {
987     GNUNET_break (0);
988     dc->emsg = GNUNET_strdup (_("internal error decrypting content"));
989     goto signal_error;
990   }
991   off =
992       compute_disk_offset (GNUNET_ntohll (dc->uri->data.chk.file_length),
993                            dr->offset, dr->depth);
994   /* save to disk */
995   if ((GNUNET_YES == prc->do_store) &&
996       ((dc->filename != NULL) || (is_recursive_download (dc))) &&
997       ((dr->depth == dc->treedepth) ||
998        (0 == (dc->options & GNUNET_FS_DOWNLOAD_NO_TEMPORARIES))))
999   {
1000     fh = GNUNET_DISK_file_open (dc->filename !=
1001                                 NULL ? dc->filename : dc->temp_filename,
1002                                 GNUNET_DISK_OPEN_READWRITE |
1003                                 GNUNET_DISK_OPEN_CREATE,
1004                                 GNUNET_DISK_PERM_USER_READ |
1005                                 GNUNET_DISK_PERM_USER_WRITE |
1006                                 GNUNET_DISK_PERM_GROUP_READ |
1007                                 GNUNET_DISK_PERM_OTHER_READ);
1008     if (NULL == fh)
1009     {
1010       GNUNET_asprintf (&dc->emsg,
1011                        _("Download failed: could not open file `%s': %s\n"),
1012                        dc->filename, STRERROR (errno));
1013       goto signal_error;
1014     }
1015     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1016                 "Saving decrypted block to disk at offset %llu\n",
1017                 (unsigned long long) off);
1018     if ((off != GNUNET_DISK_file_seek (fh, off, GNUNET_DISK_SEEK_SET)))
1019     {
1020       GNUNET_asprintf (&dc->emsg,
1021                        _("Failed to seek to offset %llu in file `%s': %s\n"),
1022                        (unsigned long long) off, dc->filename,
1023                        STRERROR (errno));
1024       goto signal_error;
1025     }
1026     if (prc->size != GNUNET_DISK_file_write (fh, pt, prc->size))
1027     {
1028       GNUNET_asprintf (&dc->emsg,
1029                        _
1030                        ("Failed to write block of %u bytes at offset %llu in file `%s': %s\n"),
1031                        (unsigned int) prc->size, (unsigned long long) off,
1032                        dc->filename, STRERROR (errno));
1033       goto signal_error;
1034     }
1035     GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
1036     fh = NULL;
1037   }
1038
1039   if (dr->depth == 0)
1040   {
1041     /* DBLOCK, update progress and try recursion if applicable */
1042     app = prc->size;
1043     if (dr->offset < dc->offset)
1044     {
1045       /* starting offset begins in the middle of pt,
1046        * do not count first bytes as progress */
1047       GNUNET_assert (app > (dc->offset - dr->offset));
1048       app -= (dc->offset - dr->offset);
1049     }
1050     if (dr->offset + prc->size > dc->offset + dc->length)
1051     {
1052       /* end of block is after relevant range,
1053        * do not count last bytes as progress */
1054       GNUNET_assert (app >
1055                      (dr->offset + prc->size) - (dc->offset + dc->length));
1056       app -= (dr->offset + prc->size) - (dc->offset + dc->length);
1057     }
1058     dc->completed += app;
1059
1060     /* do recursive download if option is set and either meta data
1061      * says it is a directory or if no meta data is given AND filename
1062      * ends in '.gnd' (top-level case) */
1063     if (is_recursive_download (dc))
1064       GNUNET_FS_directory_list_contents (prc->size, pt, off,
1065                                          &trigger_recursive_download, dc);
1066
1067   }
1068   GNUNET_assert (dc->completed <= dc->length);
1069   dr->state = BRS_DOWNLOAD_DOWN;
1070   pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1071   pi.value.download.specifics.progress.data = pt;
1072   pi.value.download.specifics.progress.offset = dr->offset;
1073   pi.value.download.specifics.progress.data_len = prc->size;
1074   pi.value.download.specifics.progress.depth = dr->depth;
1075   pi.value.download.specifics.progress.trust_offered = 0;
1076   if (prc->last_transmission.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
1077     pi.value.download.specifics.progress.block_download_duration 
1078       = GNUNET_TIME_absolute_get_duration (prc->last_transmission);
1079   else
1080     pi.value.download.specifics.progress.block_download_duration
1081       = GNUNET_TIME_UNIT_ZERO; /* found locally */
1082   GNUNET_FS_download_make_status_ (&pi, dc);
1083   if (dr->depth == 0)
1084     propagate_up (dr);
1085
1086   if (dc->completed == dc->length)
1087   {
1088     /* download completed, signal */
1089     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090                 "Download completed, truncating file to desired length %llu\n",
1091                 (unsigned long long) GNUNET_ntohll (dc->uri->data.
1092                                                     chk.file_length));
1093     /* truncate file to size (since we store IBlocks at the end) */
1094     if (dc->filename != NULL)
1095     {
1096       if (0 !=
1097           truncate (dc->filename,
1098                     GNUNET_ntohll (dc->uri->data.chk.file_length)))
1099         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
1100                                   dc->filename);
1101     }
1102     GNUNET_assert (dr->depth == 0);
1103     check_completed (dc);
1104   }
1105   if (dr->depth == 0)
1106   {
1107     /* bottom of the tree, no child downloads possible, just sync */
1108     GNUNET_FS_download_sync_ (dc);
1109     return GNUNET_YES;
1110   }
1111
1112   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113               "Triggering downloads of children (this block was at depth %u and offset %llu)\n",
1114               dr->depth, (unsigned long long) dr->offset);
1115   GNUNET_assert (0 == (prc->size % sizeof (struct ContentHashKey)));
1116   chkarr = (struct ContentHashKey *) pt;
1117   for (i = dr->num_children - 1; i >= 0; i--)
1118   {
1119     drc = dr->children[i];
1120     switch (drc->state)
1121     {
1122     case BRS_INIT:
1123       drc->chk = chkarr[drc->chk_idx];
1124       drc->state = BRS_CHK_SET;
1125       if (GNUNET_YES == dc->issue_requests)
1126         schedule_block_download (dc, drc);
1127       break;
1128     case BRS_RECONSTRUCT_DOWN:
1129       GNUNET_assert (0);
1130       break;
1131     case BRS_RECONSTRUCT_META_UP:
1132       GNUNET_assert (0);
1133       break;
1134     case BRS_RECONSTRUCT_UP:
1135       GNUNET_assert (0);
1136       break;
1137     case BRS_CHK_SET:
1138       GNUNET_assert (0);
1139       break;
1140     case BRS_DOWNLOAD_DOWN:
1141       GNUNET_assert (0);
1142       break;
1143     case BRS_DOWNLOAD_UP:
1144       GNUNET_assert (0);
1145       break;
1146     case BRS_ERROR:
1147       GNUNET_assert (0);
1148       break;
1149     default:
1150       GNUNET_assert (0);
1151       break;
1152     }
1153   }
1154   GNUNET_FS_download_sync_ (dc);
1155   return GNUNET_YES;
1156
1157 signal_error:
1158   if (fh != NULL)
1159     GNUNET_DISK_file_close (fh);
1160   pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1161   pi.value.download.specifics.error.message = dc->emsg;
1162   GNUNET_FS_download_make_status_ (&pi, dc);
1163   /* abort all pending requests */
1164   if (NULL != dc->th)
1165   {
1166     GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1167     dc->th = NULL;
1168   }
1169   GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1170   dc->in_receive = GNUNET_NO;
1171   dc->client = NULL;
1172   GNUNET_FS_free_download_request_ (dc->top_request);
1173   dc->top_request = NULL;
1174   GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1175   dc->active = NULL;
1176   dc->pending_head = NULL;
1177   dc->pending_tail = NULL;
1178   GNUNET_FS_download_sync_ (dc);
1179   return GNUNET_NO;
1180 }
1181
1182
1183 /**
1184  * Process a download result.
1185  *
1186  * @param dc our download context
1187  * @param type type of the result
1188  * @param last_transmission when was this block requested the last time? (FOREVER if unknown/not applicable)
1189  * @param data the (encrypted) response
1190  * @param size size of data
1191  */
1192 static void
1193 process_result (struct GNUNET_FS_DownloadContext *dc,
1194                 enum GNUNET_BLOCK_Type type,
1195                 struct GNUNET_TIME_Absolute last_transmission,
1196                 const void *data, size_t size)
1197 {
1198   struct ProcessResultClosure prc;
1199
1200   prc.dc = dc;
1201   prc.data = data;
1202   prc.size = size;
1203   prc.type = type;
1204   prc.do_store = GNUNET_YES;
1205   prc.last_transmission = last_transmission;
1206   GNUNET_CRYPTO_hash (data, size, &prc.query);
1207   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208               "Received result for query `%s' from `%s'-service\n",
1209               GNUNET_h2s (&prc.query), "FS");
1210   GNUNET_CONTAINER_multihashmap_get_multiple (dc->active, &prc.query,
1211                                               &process_result_with_request,
1212                                               &prc);
1213 }
1214
1215
1216 /**
1217  * Type of a function to call when we receive a message
1218  * from the service.
1219  *
1220  * @param cls closure
1221  * @param msg message received, NULL on timeout or fatal error
1222  */
1223 static void
1224 receive_results (void *cls, const struct GNUNET_MessageHeader *msg)
1225 {
1226   struct GNUNET_FS_DownloadContext *dc = cls;
1227   const struct ClientPutMessage *cm;
1228   uint16_t msize;
1229
1230   if ((NULL == msg) || (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_PUT) ||
1231       (sizeof (struct ClientPutMessage) > ntohs (msg->size)))
1232   {
1233     GNUNET_break (msg == NULL);
1234     try_reconnect (dc);
1235     return;
1236   }
1237   msize = ntohs (msg->size);
1238   cm = (const struct ClientPutMessage *) msg;
1239   process_result (dc, ntohl (cm->type),
1240                   GNUNET_TIME_absolute_ntoh (cm->last_transmission), &cm[1],
1241                   msize - sizeof (struct ClientPutMessage));
1242   if (dc->client == NULL)
1243     return;                     /* fatal error */
1244   /* continue receiving */
1245   GNUNET_CLIENT_receive (dc->client, &receive_results, dc,
1246                          GNUNET_TIME_UNIT_FOREVER_REL);
1247 }
1248
1249
1250
1251 /**
1252  * We're ready to transmit a search request to the
1253  * file-sharing service.  Do it.  If there is
1254  * more than one request pending, try to send
1255  * multiple or request another transmission.
1256  *
1257  * @param cls closure
1258  * @param size number of bytes available in buf
1259  * @param buf where the callee should write the message
1260  * @return number of bytes written to buf
1261  */
1262 static size_t
1263 transmit_download_request (void *cls, size_t size, void *buf)
1264 {
1265   struct GNUNET_FS_DownloadContext *dc = cls;
1266   size_t msize;
1267   struct SearchMessage *sm;
1268   struct DownloadRequest *dr;
1269
1270   dc->th = NULL;
1271   if (NULL == buf)
1272   {
1273     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1274                 "Transmitting download request failed, trying to reconnect\n");
1275     try_reconnect (dc);
1276     return 0;
1277   }
1278   GNUNET_assert (size >= sizeof (struct SearchMessage));
1279   msize = 0;
1280   sm = buf;
1281   while ((NULL != (dr = dc->pending_head)) &&
1282          (size >= msize + sizeof (struct SearchMessage)))
1283   {
1284     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1285                 "Transmitting download request for `%s' to `%s'-service\n",
1286                 GNUNET_h2s (&dr->chk.query), "FS");
1287     memset (sm, 0, sizeof (struct SearchMessage));
1288     sm->header.size = htons (sizeof (struct SearchMessage));
1289     sm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_START_SEARCH);
1290     if (0 != (dc->options & GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY))
1291       sm->options = htonl (GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY);
1292     else
1293       sm->options = htonl (GNUNET_FS_SEARCH_OPTION_NONE);
1294     if (dr->depth == 0)
1295       sm->type = htonl (GNUNET_BLOCK_TYPE_FS_DBLOCK);
1296     else
1297       sm->type = htonl (GNUNET_BLOCK_TYPE_FS_IBLOCK);
1298     sm->anonymity_level = htonl (dc->anonymity);
1299     sm->target = dc->target.hashPubKey;
1300     sm->query = dr->chk.query;
1301     GNUNET_CONTAINER_DLL_remove (dc->pending_head, dc->pending_tail, dr);
1302     dr->is_pending = GNUNET_NO;
1303     msize += sizeof (struct SearchMessage);
1304     sm++;
1305   }
1306   if (dc->pending_head != NULL)
1307   {
1308     dc->th =
1309         GNUNET_CLIENT_notify_transmit_ready (dc->client,
1310                                              sizeof (struct SearchMessage),
1311                                              GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1312                                              GNUNET_NO,
1313                                              &transmit_download_request, dc);
1314     GNUNET_assert (dc->th != NULL);
1315   }
1316   if (GNUNET_NO == dc->in_receive)
1317   {
1318     dc->in_receive = GNUNET_YES;
1319     GNUNET_CLIENT_receive (dc->client, &receive_results, dc,
1320                            GNUNET_TIME_UNIT_FOREVER_REL);
1321   }
1322   return msize;
1323 }
1324
1325
1326 /**
1327  * Reconnect to the FS service and transmit our queries NOW.
1328  *
1329  * @param cls our download context
1330  * @param tc unused
1331  */
1332 static void
1333 do_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1334 {
1335   struct GNUNET_FS_DownloadContext *dc = cls;
1336   struct GNUNET_CLIENT_Connection *client;
1337
1338   dc->task = GNUNET_SCHEDULER_NO_TASK;
1339   client = GNUNET_CLIENT_connect ("fs", dc->h->cfg);
1340   if (NULL == client)
1341   {
1342     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1343                 "Connecting to `%s'-service failed, will try again.\n", "FS");
1344     try_reconnect (dc);
1345     return;
1346   }
1347   dc->client = client;
1348   if (dc->pending_head != NULL)
1349   {
1350     dc->th =
1351         GNUNET_CLIENT_notify_transmit_ready (client,
1352                                              sizeof (struct SearchMessage),
1353                                              GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1354                                              GNUNET_NO,
1355                                              &transmit_download_request, dc);
1356     GNUNET_assert (dc->th != NULL);
1357   }
1358 }
1359
1360
1361 /**
1362  * Add entries to the pending list.
1363  *
1364  * @param cls our download context
1365  * @param key unused
1366  * @param entry entry of type "struct DownloadRequest"
1367  * @return GNUNET_OK
1368  */
1369 static int
1370 retry_entry (void *cls, const GNUNET_HashCode * key, void *entry)
1371 {
1372   struct GNUNET_FS_DownloadContext *dc = cls;
1373   struct DownloadRequest *dr = entry;
1374
1375   dr->next = NULL;
1376   dr->prev = NULL;
1377   GNUNET_CONTAINER_DLL_insert (dc->pending_head, dc->pending_tail, dr);
1378   dr->is_pending = GNUNET_YES;
1379   return GNUNET_OK;
1380 }
1381
1382
1383 /**
1384  * We've lost our connection with the FS service.
1385  * Re-establish it and re-transmit all of our
1386  * pending requests.
1387  *
1388  * @param dc download context that is having trouble
1389  */
1390 static void
1391 try_reconnect (struct GNUNET_FS_DownloadContext *dc)
1392 {
1393
1394   if (NULL != dc->client)
1395   {
1396     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397                 "Moving all requests back to pending list\n");
1398     if (NULL != dc->th)
1399     {
1400       GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1401       dc->th = NULL;
1402     }
1403     /* full reset of the pending list */
1404     dc->pending_head = NULL;
1405     dc->pending_tail = NULL;
1406     GNUNET_CONTAINER_multihashmap_iterate (dc->active, &retry_entry, dc);
1407     GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1408     dc->in_receive = GNUNET_NO;
1409     dc->client = NULL;
1410   }
1411   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will try to reconnect in 1s\n");
1412   dc->task =
1413       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_reconnect,
1414                                     dc);
1415 }
1416
1417
1418 /**
1419  * We're allowed to ask the FS service for our blocks.  Start the download.
1420  *
1421  * @param cls the 'struct GNUNET_FS_DownloadContext'
1422  * @param client handle to use for communcation with FS (we must destroy it!)
1423  */
1424 static void
1425 activate_fs_download (void *cls, struct GNUNET_CLIENT_Connection *client)
1426 {
1427   struct GNUNET_FS_DownloadContext *dc = cls;
1428   struct GNUNET_FS_ProgressInfo pi;
1429
1430   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download activated\n");
1431   GNUNET_assert (NULL != client);
1432   GNUNET_assert (dc->client == NULL);
1433   GNUNET_assert (dc->th == NULL);
1434   dc->client = client;
1435   pi.status = GNUNET_FS_STATUS_DOWNLOAD_ACTIVE;
1436   GNUNET_FS_download_make_status_ (&pi, dc);
1437   dc->pending_head = NULL;
1438   dc->pending_tail = NULL;
1439   GNUNET_CONTAINER_multihashmap_iterate (dc->active, &retry_entry, dc);
1440   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1441               "Asking for transmission to FS service\n");
1442   if (dc->pending_head != NULL)
1443   {
1444     dc->th =
1445         GNUNET_CLIENT_notify_transmit_ready (dc->client,
1446                                              sizeof (struct SearchMessage),
1447                                              GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1448                                              GNUNET_NO,
1449                                              &transmit_download_request, dc);
1450     GNUNET_assert (dc->th != NULL);
1451   }
1452 }
1453
1454
1455 /**
1456  * We must stop to ask the FS service for our blocks.  Pause the download.
1457  *
1458  * @param cls the 'struct GNUNET_FS_DownloadContext'
1459  */
1460 static void
1461 deactivate_fs_download (void *cls)
1462 {
1463   struct GNUNET_FS_DownloadContext *dc = cls;
1464   struct GNUNET_FS_ProgressInfo pi;
1465
1466   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download deactivated\n");
1467   if (NULL != dc->th)
1468   {
1469     GNUNET_CLIENT_notify_transmit_ready_cancel (dc->th);
1470     dc->th = NULL;
1471   }
1472   if (NULL != dc->client)
1473   {
1474     GNUNET_CLIENT_disconnect (dc->client, GNUNET_NO);
1475     dc->in_receive = GNUNET_NO;
1476     dc->client = NULL;
1477   }
1478   dc->pending_head = NULL;
1479   dc->pending_tail = NULL;
1480   pi.status = GNUNET_FS_STATUS_DOWNLOAD_INACTIVE;
1481   GNUNET_FS_download_make_status_ (&pi, dc);
1482 }
1483
1484
1485 /**
1486  * (recursively) Create a download request structure.
1487  *
1488  * @param parent parent of the current entry
1489  * @param chk_idx index of the chk for this block in the parent block
1490  * @param depth depth of the current entry, 0 are the DBLOCKs,
1491  *              top level block is 'dc->treedepth - 1'
1492  * @param dr_offset offset in the original file this block maps to
1493  *              (as in, offset of the first byte of the first DBLOCK
1494  *               in the subtree rooted in the returned download request tree)
1495  * @param file_start_offset desired starting offset for the download
1496  *             in the original file; requesting tree should not contain
1497  *             DBLOCKs prior to the file_start_offset
1498  * @param desired_length desired number of bytes the user wanted to access
1499  *        (from file_start_offset).  Resulting tree should not contain
1500  *        DBLOCKs after file_start_offset + file_length.
1501  * @return download request tree for the given range of DBLOCKs at
1502  *         the specified depth
1503  */
1504 static struct DownloadRequest *
1505 create_download_request (struct DownloadRequest *parent, 
1506                          unsigned int chk_idx,
1507                          unsigned int depth,
1508                          uint64_t dr_offset, uint64_t file_start_offset,
1509                          uint64_t desired_length)
1510 {
1511   struct DownloadRequest *dr;
1512   unsigned int i;
1513   unsigned int head_skip;
1514   uint64_t child_block_size;
1515
1516   dr = GNUNET_malloc (sizeof (struct DownloadRequest));
1517   dr->parent = parent;
1518   dr->depth = depth;
1519   dr->offset = dr_offset;
1520   dr->chk_idx = chk_idx;
1521   if (depth > 0)
1522   {
1523     child_block_size = GNUNET_FS_tree_compute_tree_size (depth - 1);
1524
1525     /* calculate how many blocks at this level are not interesting
1526      * from the start (rounded down), either because of the requested
1527      * file offset or because this IBlock is further along */
1528     if (dr_offset < file_start_offset)
1529       head_skip = file_start_offset / child_block_size;
1530     else
1531       head_skip = 0;
1532
1533     /* calculate index of last block at this level that is interesting (rounded up) */
1534     dr->num_children = (file_start_offset + desired_length - dr_offset) / child_block_size;
1535     if (dr->num_children * child_block_size <
1536         file_start_offset + desired_length - dr_offset)
1537       dr->num_children++;       /* round up */
1538     if (dr->num_children > CHK_PER_INODE)
1539       dr->num_children = CHK_PER_INODE; /* cap at max */
1540     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1541                 "Block at offset %llu and depth %u has %u children\n",
1542                 (unsigned long long) dr_offset,
1543                 depth,
1544                 dr->num_children);
1545
1546     /* now we can get the total number of *interesting* children for this block */
1547     dr->num_children -= head_skip;
1548
1549     /* why else would we have gotten here to begin with? (that'd be a bad logic error) */
1550     GNUNET_assert (dr->num_children > 0);
1551
1552     dr->children =
1553         GNUNET_malloc (dr->num_children * sizeof (struct DownloadRequest *));
1554     for (i = 0; i < dr->num_children; i++)
1555       dr->children[i] =
1556           create_download_request (dr, i + head_skip, depth - 1,                                   
1557                                    dr_offset + (i + head_skip) * child_block_size,
1558                                    file_start_offset, desired_length);
1559   }
1560   return dr;
1561 }
1562
1563
1564 /**
1565  * Continuation after a possible attempt to reconstruct
1566  * the current IBlock from the existing file.
1567  *
1568  * @param cls the 'struct ReconstructContext'
1569  * @param tc scheduler context
1570  */
1571 static void
1572 reconstruct_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1573 {
1574   struct GNUNET_FS_DownloadContext *dc = cls;
1575
1576   /* clean up state from tree encoder */  
1577   if (dc->task != GNUNET_SCHEDULER_NO_TASK)
1578   {
1579     GNUNET_SCHEDULER_cancel (dc->task);
1580     dc->task = GNUNET_SCHEDULER_NO_TASK;
1581   }
1582   if (dc->rfh != NULL)
1583   {
1584     GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (dc->rfh));
1585     dc->rfh = NULL;
1586   }
1587   /* start "normal" download */
1588   dc->issue_requests = GNUNET_YES;
1589   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1590               "Starting normal download\n");
1591   schedule_block_download (dc, dc->top_request);
1592 }
1593
1594
1595 /**
1596  * Task requesting the next block from the tree encoder.
1597  *
1598  * @param cls the 'struct GNUJNET_FS_DownloadContext' we're processing
1599  * @param tc task context
1600  */
1601 static void
1602 get_next_block (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1603 {
1604   struct GNUNET_FS_DownloadContext *dc = cls;
1605
1606   dc->task = GNUNET_SCHEDULER_NO_TASK;
1607   GNUNET_FS_tree_encoder_next (dc->te);
1608 }
1609
1610
1611
1612 /**
1613  * Function called asking for the current (encoded)
1614  * block to be processed.  After processing the
1615  * client should either call "GNUNET_FS_tree_encode_next"
1616  * or (on error) "GNUNET_FS_tree_encode_finish".
1617  *
1618  * This function checks if the content on disk matches
1619  * the expected content based on the URI.
1620  *
1621  * @param cls closure
1622  * @param chk content hash key for the block
1623  * @param offset offset of the block
1624  * @param depth depth of the block, 0 for DBLOCK
1625  * @param type type of the block (IBLOCK or DBLOCK)
1626  * @param block the (encrypted) block
1627  * @param block_size size of block (in bytes)
1628  */
1629 static void
1630 reconstruct_cb (void *cls, const struct ContentHashKey *chk, uint64_t offset,
1631                 unsigned int depth, enum GNUNET_BLOCK_Type type,
1632                 const void *block, uint16_t block_size)
1633 {
1634   struct GNUNET_FS_DownloadContext *dc = cls;
1635   struct GNUNET_FS_ProgressInfo pi;
1636   struct DownloadRequest *dr;
1637   uint64_t blen;
1638   unsigned int chld;
1639
1640   /* find corresponding request entry */
1641   dr = dc->top_request;
1642   while (dr->depth > depth)
1643   {
1644     GNUNET_assert (dr->num_children > 0);
1645     blen = GNUNET_FS_tree_compute_tree_size (dr->depth - 1);
1646     chld = (offset - dr->offset) / blen;
1647     if (chld < dr->children[0]->chk_idx)
1648     {
1649       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1650                   "Block %u < %u irrelevant for our range\n",
1651                   chld,
1652                   dr->children[dr->num_children-1]->chk_idx);
1653       dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1654       return; /* irrelevant block */
1655     }
1656     if (chld > dr->children[dr->num_children-1]->chk_idx)
1657     {
1658       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1659                   "Block %u > %u irrelevant for our range\n",
1660                   chld,
1661                   dr->children[dr->num_children-1]->chk_idx);
1662       dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1663       return; /* irrelevant block */
1664     }
1665     dr = dr->children[chld - dr->children[0]->chk_idx];
1666   }
1667   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1668               "Matched TE block with request at offset %llu and depth %u in state %d\n",
1669               (unsigned long long) dr->offset,
1670               dr->depth,
1671               dr->state);
1672   /* FIXME: this code needs more testing and might
1673      need to handle more states... */
1674   switch (dr->state)
1675   {
1676   case BRS_INIT:
1677     break;
1678   case BRS_RECONSTRUCT_DOWN:
1679     break;
1680   case BRS_RECONSTRUCT_META_UP:
1681     break;
1682   case BRS_RECONSTRUCT_UP:
1683     break;
1684   case BRS_CHK_SET:
1685     if (0 == memcmp (chk, &dr->chk, sizeof (struct ContentHashKey)))
1686     {
1687       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1688                   "Reconstruction succeeded, can use block at offset %llu, depth %u\n",
1689                   (unsigned long long) offset,
1690                   depth);
1691       /* block matches, hence tree below matches;
1692        * this request is done! */
1693       dr->state = BRS_DOWNLOAD_UP;
1694       (void) GNUNET_CONTAINER_multihashmap_remove (dc->active, &dr->chk.query, dr);
1695       if (GNUNET_YES == dr->is_pending)
1696       {
1697         GNUNET_break (0); /* how did we get here? */
1698         GNUNET_CONTAINER_DLL_remove (dc->pending_head, dc->pending_tail, dr);
1699         dr->is_pending = GNUNET_NO;
1700       }
1701       /* calculate how many bytes of payload this block
1702        * corresponds to */
1703       blen = GNUNET_FS_tree_compute_tree_size (dr->depth);
1704       /* how many of those bytes are in the requested range? */
1705       blen = GNUNET_MIN (blen, dc->length + dc->offset - dr->offset);
1706       /* signal progress */
1707       dc->completed += blen;
1708       pi.status = GNUNET_FS_STATUS_DOWNLOAD_PROGRESS;
1709       pi.value.download.specifics.progress.data = NULL;
1710       pi.value.download.specifics.progress.offset = offset;
1711       pi.value.download.specifics.progress.data_len = 0;
1712       pi.value.download.specifics.progress.depth = 0;
1713       pi.value.download.specifics.progress.trust_offered = 0;
1714       pi.value.download.specifics.progress.block_download_duration = GNUNET_TIME_UNIT_ZERO;
1715       GNUNET_FS_download_make_status_ (&pi, dc);
1716       /* FIXME: duplicated code from 'process_result_with_request - refactor */
1717       if (dc->completed == dc->length)
1718       {
1719         /* download completed, signal */
1720         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1721                     "Download completed, truncating file to desired length %llu\n",
1722                     (unsigned long long) GNUNET_ntohll (dc->uri->data.
1723                                                         chk.file_length));
1724         /* truncate file to size (since we store IBlocks at the end) */
1725         if (dc->filename != NULL)
1726         {
1727           if (0 !=
1728               truncate (dc->filename,
1729                         GNUNET_ntohll (dc->uri->data.chk.file_length)))
1730             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate",
1731                                       dc->filename);
1732         }
1733       }
1734     }
1735     else
1736       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1737                   "Reconstruction failed, need to download block at offset %llu, depth %u\n",
1738                   (unsigned long long) offset,
1739                   depth);
1740     break;
1741   case BRS_DOWNLOAD_DOWN:
1742     break;
1743   case BRS_DOWNLOAD_UP:
1744     break;
1745   case BRS_ERROR:
1746     break;
1747   default:
1748     GNUNET_assert (0);
1749     break;
1750   }
1751   dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1752   if ((dr == dc->top_request) && (dr->state == BRS_DOWNLOAD_UP))
1753     check_completed (dc);
1754 }
1755
1756
1757 /**
1758  * Function called by the tree encoder to obtain a block of plaintext
1759  * data (for the lowest level of the tree).
1760  *
1761  * @param cls our 'struct ReconstructContext'
1762  * @param offset identifies which block to get
1763  * @param max (maximum) number of bytes to get; returning
1764  *        fewer will also cause errors
1765  * @param buf where to copy the plaintext buffer
1766  * @param emsg location to store an error message (on error)
1767  * @return number of bytes copied to buf, 0 on error
1768  */
1769 static size_t
1770 fh_reader (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
1771 {
1772   struct GNUNET_FS_DownloadContext *dc = cls;
1773   struct GNUNET_DISK_FileHandle *fh = dc->rfh;
1774   ssize_t ret;
1775
1776   *emsg = NULL;
1777   if (offset != GNUNET_DISK_file_seek (fh, offset, GNUNET_DISK_SEEK_SET))
1778   {
1779     *emsg = GNUNET_strdup (strerror (errno));
1780     return 0;
1781   }
1782   ret = GNUNET_DISK_file_read (fh, buf, max);
1783   if (ret < 0)
1784   {
1785     *emsg = GNUNET_strdup (strerror (errno));
1786     return 0;
1787   }
1788   return ret;
1789 }
1790
1791
1792 /**
1793  * Task that creates the initial (top-level) download
1794  * request for the file.
1795  *
1796  * @param cls the 'struct GNUNET_FS_DownloadContext'
1797  * @param tc scheduler context
1798  */
1799 void
1800 GNUNET_FS_download_start_task_ (void *cls,
1801                                 const struct GNUNET_SCHEDULER_TaskContext *tc)
1802 {
1803   struct GNUNET_FS_DownloadContext *dc = cls;
1804   struct GNUNET_FS_ProgressInfo pi;
1805   struct GNUNET_DISK_FileHandle *fh;
1806
1807   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start task running...\n");
1808   dc->task = GNUNET_SCHEDULER_NO_TASK;
1809   if (dc->length == 0)
1810   {
1811     /* no bytes required! */
1812     if (dc->filename != NULL)
1813     {
1814       fh = GNUNET_DISK_file_open (dc->filename,
1815                                   GNUNET_DISK_OPEN_READWRITE |
1816                                   GNUNET_DISK_OPEN_CREATE |
1817                                   ((0 ==
1818                                     GNUNET_FS_uri_chk_get_file_size (dc->uri)) ?
1819                                    GNUNET_DISK_OPEN_TRUNCATE : 0),
1820                                   GNUNET_DISK_PERM_USER_READ |
1821                                   GNUNET_DISK_PERM_USER_WRITE |
1822                                   GNUNET_DISK_PERM_GROUP_READ |
1823                                   GNUNET_DISK_PERM_OTHER_READ);
1824       GNUNET_DISK_file_close (fh);
1825     }
1826     GNUNET_FS_download_sync_ (dc);
1827     pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1828     pi.value.download.specifics.start.meta = dc->meta;
1829     GNUNET_FS_download_make_status_ (&pi, dc);
1830     check_completed (dc);
1831     return;
1832   }
1833   if (dc->emsg != NULL)
1834     return;
1835   if (dc->top_request == NULL)
1836   {
1837     dc->top_request =
1838       create_download_request (NULL, 0, dc->treedepth - 1, 0, dc->offset,
1839                                  dc->length);
1840     dc->top_request->state = BRS_CHK_SET;
1841     dc->top_request->chk =
1842         (dc->uri->type ==
1843          chk) ? dc->uri->data.chk.chk : dc->uri->data.loc.fi.chk;
1844     /* signal start */
1845     GNUNET_FS_download_sync_ (dc);
1846     if (NULL != dc->search)
1847       GNUNET_FS_search_result_sync_ (dc->search);
1848     pi.status = GNUNET_FS_STATUS_DOWNLOAD_START;
1849     pi.value.download.specifics.start.meta = dc->meta;
1850     GNUNET_FS_download_make_status_ (&pi, dc);
1851   }
1852   GNUNET_FS_download_start_downloading_ (dc);
1853   /* attempt reconstruction from disk */
1854   if (GNUNET_YES == GNUNET_DISK_file_test (dc->filename))
1855     dc->rfh =
1856         GNUNET_DISK_file_open (dc->filename, GNUNET_DISK_OPEN_READ,
1857                                GNUNET_DISK_PERM_NONE);
1858   if (dc->top_request->state == BRS_CHK_SET)
1859   {
1860     if (dc->rfh != NULL)
1861     {
1862       /* first, try top-down */
1863       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1864                   "Trying top-down reconstruction for `%s'\n", dc->filename);
1865       try_top_down_reconstruction (dc, dc->top_request);
1866       switch (dc->top_request->state)
1867       {
1868       case BRS_CHK_SET:
1869         break;                  /* normal */
1870       case BRS_DOWNLOAD_DOWN:
1871         break;                  /* normal, some blocks already down */
1872       case BRS_DOWNLOAD_UP:
1873         /* already done entirely, party! */
1874         if (dc->rfh != NULL)
1875         {
1876           /* avoid hanging on to file handle longer than
1877            * necessary */
1878           GNUNET_DISK_file_close (dc->rfh);
1879           dc->rfh = NULL;
1880         }
1881         return;
1882       case BRS_ERROR:
1883         GNUNET_asprintf (&dc->emsg, _("Invalid URI"));
1884         GNUNET_FS_download_sync_ (dc);
1885         pi.status = GNUNET_FS_STATUS_DOWNLOAD_ERROR;
1886         pi.value.download.specifics.error.message = dc->emsg;
1887         GNUNET_FS_download_make_status_ (&pi, dc);
1888         return;
1889       default:
1890         GNUNET_assert (0);
1891         break;
1892       }
1893     }
1894   }
1895   /* attempt reconstruction from meta data */
1896   if ((GNUNET_FS_uri_chk_get_file_size (dc->uri) <= MAX_INLINE_SIZE) &&
1897       (NULL != dc->meta))
1898   {
1899     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1900                 "Trying to find embedded meta data for download of size %llu with %u bytes MD\n",
1901                 (unsigned long long) GNUNET_FS_uri_chk_get_file_size (dc->uri),
1902                 (unsigned int)
1903                 GNUNET_CONTAINER_meta_data_get_serialized_size (dc->meta));
1904     GNUNET_CONTAINER_meta_data_iterate (dc->meta, &match_full_data, dc);
1905     if (dc->top_request->state == BRS_DOWNLOAD_UP)
1906     {
1907       if (dc->rfh != NULL)
1908       {
1909         /* avoid hanging on to file handle longer than
1910          * necessary */
1911         GNUNET_DISK_file_close (dc->rfh);
1912         dc->rfh = NULL;
1913       }
1914       return;                   /* finished, status update was already done for us */
1915     }
1916   }
1917   if (dc->rfh != NULL)
1918   {
1919     /* finally, actually run bottom-up */
1920     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1921                 "Trying bottom-up reconstruction of file `%s'\n", dc->filename);
1922     dc->te =
1923       GNUNET_FS_tree_encoder_create (dc->h, 
1924                                      GNUNET_FS_uri_chk_get_file_size (dc->uri),
1925                                      dc, &fh_reader,
1926                                      &reconstruct_cb, NULL,
1927                                      &reconstruct_cont);
1928     dc->task = GNUNET_SCHEDULER_add_now (&get_next_block, dc);
1929   }
1930   else
1931   {
1932     /* simple, top-level download */
1933     dc->issue_requests = GNUNET_YES;
1934     schedule_block_download (dc, dc->top_request);
1935   }
1936   if (dc->top_request->state == BRS_DOWNLOAD_UP)
1937     check_completed (dc);
1938 }
1939
1940
1941 /**
1942  * Create SUSPEND event for the given download operation
1943  * and then clean up our state (without stop signal).
1944  *
1945  * @param cls the 'struct GNUNET_FS_DownloadContext' to signal for
1946  */
1947 void
1948 GNUNET_FS_download_signal_suspend_ (void *cls)
1949 {
1950   struct GNUNET_FS_DownloadContext *dc = cls;
1951   struct GNUNET_FS_ProgressInfo pi;
1952
1953   if (dc->top != NULL)
1954     GNUNET_FS_end_top (dc->h, dc->top);
1955   while (NULL != dc->child_head)
1956     GNUNET_FS_download_signal_suspend_ (dc->child_head);
1957   if (dc->search != NULL)
1958   {
1959     dc->search->download = NULL;
1960     dc->search = NULL;
1961   }
1962   if (dc->job_queue != NULL)
1963   {
1964     GNUNET_FS_dequeue_ (dc->job_queue);
1965     dc->job_queue = NULL;
1966   }
1967   if (dc->parent != NULL)
1968     GNUNET_CONTAINER_DLL_remove (dc->parent->child_head, dc->parent->child_tail,
1969                                  dc);
1970   if (dc->task != GNUNET_SCHEDULER_NO_TASK)
1971   {
1972     GNUNET_SCHEDULER_cancel (dc->task);
1973     dc->task = GNUNET_SCHEDULER_NO_TASK;
1974   }
1975   pi.status = GNUNET_FS_STATUS_DOWNLOAD_SUSPEND;
1976   GNUNET_FS_download_make_status_ (&pi, dc);
1977   if (dc->te != NULL)
1978   {
1979     GNUNET_FS_tree_encoder_finish (dc->te, NULL, NULL);
1980     dc->te = NULL;
1981   }
1982   if (dc->rfh != NULL)
1983   {
1984     GNUNET_DISK_file_close (dc->rfh);
1985     dc->rfh = NULL;
1986   }
1987   GNUNET_FS_free_download_request_ (dc->top_request);
1988   if (dc->active != NULL)
1989   {
1990     GNUNET_CONTAINER_multihashmap_destroy (dc->active);
1991     dc->active = NULL;
1992   }
1993   GNUNET_free_non_null (dc->filename);
1994   GNUNET_CONTAINER_meta_data_destroy (dc->meta);
1995   GNUNET_FS_uri_destroy (dc->uri);
1996   GNUNET_free_non_null (dc->temp_filename);
1997   GNUNET_free_non_null (dc->serialization);
1998   GNUNET_free (dc);
1999 }
2000
2001
2002 /**
2003  * Helper function to setup the download context.
2004  *
2005  * @param h handle to the file sharing subsystem
2006  * @param uri the URI of the file (determines what to download); CHK or LOC URI
2007  * @param meta known metadata for the file (can be NULL)
2008  * @param filename where to store the file, maybe NULL (then no file is
2009  *        created on disk and data must be grabbed from the callbacks)
2010  * @param tempname where to store temporary file data, not used if filename is non-NULL;
2011  *        can be NULL (in which case we will pick a name if needed); the temporary file
2012  *        may already exist, in which case we will try to use the data that is there and
2013  *        if it is not what is desired, will overwrite it
2014  * @param offset at what offset should we start the download (typically 0)
2015  * @param length how many bytes should be downloaded starting at offset
2016  * @param anonymity anonymity level to use for the download
2017  * @param options various options
2018  * @param cctx initial value for the client context for this download
2019  * @return context that can be used to control this download
2020  */
2021 struct GNUNET_FS_DownloadContext *
2022 create_download_context (struct GNUNET_FS_Handle *h,
2023                          const struct GNUNET_FS_Uri *uri,
2024                          const struct GNUNET_CONTAINER_MetaData *meta,
2025                          const char *filename, const char *tempname,
2026                          uint64_t offset, uint64_t length, uint32_t anonymity,
2027                          enum GNUNET_FS_DownloadOptions options, void *cctx)
2028 {
2029   struct GNUNET_FS_DownloadContext *dc;
2030
2031   GNUNET_assert (GNUNET_FS_uri_test_chk (uri) || GNUNET_FS_uri_test_loc (uri));
2032   if ((offset + length < offset) ||
2033       (offset + length > GNUNET_FS_uri_chk_get_file_size (uri)))
2034   {
2035     GNUNET_break (0);
2036     return NULL;
2037   }
2038   dc = GNUNET_malloc (sizeof (struct GNUNET_FS_DownloadContext));
2039   dc->h = h;
2040   dc->uri = GNUNET_FS_uri_dup (uri);
2041   dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
2042   dc->client_info = cctx;
2043   dc->start_time = GNUNET_TIME_absolute_get ();
2044   if (NULL != filename)
2045   {
2046     dc->filename = GNUNET_strdup (filename);
2047     if (GNUNET_YES == GNUNET_DISK_file_test (filename))
2048       GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (filename, &dc->old_file_size, GNUNET_YES, GNUNET_YES));
2049   }
2050   if (GNUNET_FS_uri_test_loc (dc->uri))
2051     GNUNET_assert (GNUNET_OK ==
2052                    GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
2053   dc->offset = offset;
2054   dc->length = length;
2055   dc->anonymity = anonymity;
2056   dc->options = options;
2057   dc->active =
2058       GNUNET_CONTAINER_multihashmap_create (1 + 2 * (length / DBLOCK_SIZE));
2059   dc->treedepth =
2060       GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
2061   if ((NULL == filename) && (is_recursive_download (dc)))
2062   {
2063     if (NULL != tempname)
2064       dc->temp_filename = GNUNET_strdup (tempname);
2065     else
2066       dc->temp_filename = GNUNET_DISK_mktemp ("gnunet-directory-download-tmp");
2067   }
2068   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
2069               "Starting download `%s' of %llu bytes with tree depth %u\n",
2070               filename,
2071               (unsigned long long) length,
2072               dc->treedepth);
2073   dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
2074   return dc;
2075 }
2076
2077
2078 /**
2079  * Download parts of a file.  Note that this will store
2080  * the blocks at the respective offset in the given file.  Also, the
2081  * download is still using the blocking of the underlying FS
2082  * encoding.  As a result, the download may *write* outside of the
2083  * given boundaries (if offset and length do not match the 32k FS
2084  * block boundaries). <p>
2085  *
2086  * This function should be used to focus a download towards a
2087  * particular portion of the file (optimization), not to strictly
2088  * limit the download to exactly those bytes.
2089  *
2090  * @param h handle to the file sharing subsystem
2091  * @param uri the URI of the file (determines what to download); CHK or LOC URI
2092  * @param meta known metadata for the file (can be NULL)
2093  * @param filename where to store the file, maybe NULL (then no file is
2094  *        created on disk and data must be grabbed from the callbacks)
2095  * @param tempname where to store temporary file data, not used if filename is non-NULL;
2096  *        can be NULL (in which case we will pick a name if needed); the temporary file
2097  *        may already exist, in which case we will try to use the data that is there and
2098  *        if it is not what is desired, will overwrite it
2099  * @param offset at what offset should we start the download (typically 0)
2100  * @param length how many bytes should be downloaded starting at offset
2101  * @param anonymity anonymity level to use for the download
2102  * @param options various options
2103  * @param cctx initial value for the client context for this download
2104  * @param parent parent download to associate this download with (use NULL
2105  *        for top-level downloads; useful for manually-triggered recursive downloads)
2106  * @return context that can be used to control this download
2107  */
2108 struct GNUNET_FS_DownloadContext *
2109 GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
2110                           const struct GNUNET_FS_Uri *uri,
2111                           const struct GNUNET_CONTAINER_MetaData *meta,
2112                           const char *filename, const char *tempname,
2113                           uint64_t offset, uint64_t length, uint32_t anonymity,
2114                           enum GNUNET_FS_DownloadOptions options, void *cctx,
2115                           struct GNUNET_FS_DownloadContext *parent)
2116 {
2117   struct GNUNET_FS_DownloadContext *dc;
2118
2119   dc = create_download_context (h, uri, meta, filename, tempname,
2120                                 offset, length, anonymity, options, cctx);
2121   if (NULL == dc)
2122     return NULL;
2123   dc->parent = parent;
2124   if (NULL != parent)
2125     GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
2126   else
2127     dc->top =
2128         GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
2129   return dc;
2130 }
2131
2132
2133 /**
2134  * Download parts of a file based on a search result.  The download
2135  * will be associated with the search result (and the association
2136  * will be preserved when serializing/deserializing the state).
2137  * If the search is stopped, the download will not be aborted but
2138  * be 'promoted' to a stand-alone download.
2139  *
2140  * As with the other download function, this will store
2141  * the blocks at the respective offset in the given file.  Also, the
2142  * download is still using the blocking of the underlying FS
2143  * encoding.  As a result, the download may *write* outside of the
2144  * given boundaries (if offset and length do not match the 32k FS
2145  * block boundaries). <p>
2146  *
2147  * The given range can be used to focus a download towards a
2148  * particular portion of the file (optimization), not to strictly
2149  * limit the download to exactly those bytes.
2150  *
2151  * @param h handle to the file sharing subsystem
2152  * @param sr the search result to use for the download (determines uri and
2153  *        meta data and associations)
2154  * @param filename where to store the file, maybe NULL (then no file is
2155  *        created on disk and data must be grabbed from the callbacks)
2156  * @param tempname where to store temporary file data, not used if filename is non-NULL;
2157  *        can be NULL (in which case we will pick a name if needed); the temporary file
2158  *        may already exist, in which case we will try to use the data that is there and
2159  *        if it is not what is desired, will overwrite it
2160  * @param offset at what offset should we start the download (typically 0)
2161  * @param length how many bytes should be downloaded starting at offset
2162  * @param anonymity anonymity level to use for the download
2163  * @param options various download options
2164  * @param cctx initial value for the client context for this download
2165  * @return context that can be used to control this download
2166  */
2167 struct GNUNET_FS_DownloadContext *
2168 GNUNET_FS_download_start_from_search (struct GNUNET_FS_Handle *h,
2169                                       struct GNUNET_FS_SearchResult *sr,
2170                                       const char *filename,
2171                                       const char *tempname, uint64_t offset,
2172                                       uint64_t length, uint32_t anonymity,
2173                                       enum GNUNET_FS_DownloadOptions options,
2174                                       void *cctx)
2175 {
2176   struct GNUNET_FS_DownloadContext *dc;
2177
2178   if ((NULL == sr) || (NULL != sr->download))
2179   {
2180     GNUNET_break (0);
2181     return NULL;
2182   }
2183   dc = create_download_context (h, sr->uri, sr->meta, filename, tempname,
2184                                 offset, length, anonymity, options, cctx);
2185   if (NULL == dc)
2186     return NULL;
2187   dc->search = sr;
2188   sr->download = dc;
2189   if (NULL != sr->probe_ctx)
2190   {
2191     GNUNET_FS_download_stop (sr->probe_ctx, GNUNET_YES);
2192     sr->probe_ctx = NULL;
2193   }
2194   return dc;
2195 }
2196
2197
2198 /**
2199  * Start the downloading process (by entering the queue).
2200  *
2201  * @param dc our download context
2202  */
2203 void
2204 GNUNET_FS_download_start_downloading_ (struct GNUNET_FS_DownloadContext *dc)
2205 {
2206   if (dc->completed == dc->length)
2207     return;
2208   GNUNET_assert (dc->job_queue == NULL);
2209   dc->job_queue =
2210       GNUNET_FS_queue_ (dc->h, &activate_fs_download, &deactivate_fs_download,
2211                         dc, (dc->length + DBLOCK_SIZE - 1) / DBLOCK_SIZE,
2212                         (0 == (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
2213                         ? GNUNET_FS_QUEUE_PRIORITY_NORMAL 
2214                         : GNUNET_FS_QUEUE_PRIORITY_PROBE);
2215 }
2216
2217
2218 /**
2219  * Stop a download (aborts if download is incomplete).
2220  *
2221  * @param dc handle for the download
2222  * @param do_delete delete files of incomplete downloads
2223  */
2224 void
2225 GNUNET_FS_download_stop (struct GNUNET_FS_DownloadContext *dc, int do_delete)
2226 {
2227   struct GNUNET_FS_ProgressInfo pi;
2228   int have_children;
2229
2230   if (dc->top != NULL)
2231     GNUNET_FS_end_top (dc->h, dc->top);
2232   if (dc->task != GNUNET_SCHEDULER_NO_TASK)
2233   {
2234     GNUNET_SCHEDULER_cancel (dc->task);
2235     dc->task = GNUNET_SCHEDULER_NO_TASK;
2236   }
2237   if (dc->search != NULL)
2238   {
2239     dc->search->download = NULL;
2240     GNUNET_FS_search_result_sync_ (dc->search);
2241     dc->search = NULL;
2242   }
2243   if (dc->job_queue != NULL)
2244   {
2245     GNUNET_FS_dequeue_ (dc->job_queue);
2246     dc->job_queue = NULL;
2247   }
2248   if (dc->te != NULL)
2249   {
2250     GNUNET_FS_tree_encoder_finish (dc->te, NULL, NULL);
2251     dc->te = NULL;
2252   }
2253   have_children = (NULL != dc->child_head) ? GNUNET_YES : GNUNET_NO;
2254   while (NULL != dc->child_head)
2255     GNUNET_FS_download_stop (dc->child_head, do_delete);
2256   if (dc->parent != NULL)
2257     GNUNET_CONTAINER_DLL_remove (dc->parent->child_head, dc->parent->child_tail,
2258                                  dc);
2259   if (dc->serialization != NULL)
2260     GNUNET_FS_remove_sync_file_ (dc->h,
2261                                  ((dc->parent != NULL) ||
2262                                   (dc->search !=
2263                                    NULL)) ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
2264                                  GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2265                                  dc->serialization);
2266   if ((GNUNET_YES == have_children) && (dc->parent == NULL))
2267     GNUNET_FS_remove_sync_dir_ (dc->h,
2268                                 (dc->search !=
2269                                  NULL) ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD :
2270                                 GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
2271                                 dc->serialization);
2272   pi.status = GNUNET_FS_STATUS_DOWNLOAD_STOPPED;
2273   GNUNET_FS_download_make_status_ (&pi, dc);
2274   GNUNET_FS_free_download_request_ (dc->top_request);
2275   dc->top_request = NULL;
2276   if (dc->active != NULL)
2277   {
2278     GNUNET_CONTAINER_multihashmap_destroy (dc->active);
2279     dc->active = NULL;
2280   }
2281   if (dc->filename != NULL)
2282   {
2283     if ((dc->completed != dc->length) && (GNUNET_YES == do_delete))
2284     {
2285       if (0 != UNLINK (dc->filename))
2286         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
2287                                   dc->filename);
2288     }
2289     GNUNET_free (dc->filename);
2290   }
2291   GNUNET_CONTAINER_meta_data_destroy (dc->meta);
2292   GNUNET_FS_uri_destroy (dc->uri);
2293   if (NULL != dc->temp_filename)
2294   {
2295     if (0 != UNLINK (dc->temp_filename))
2296       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink",
2297                                 dc->temp_filename);
2298     GNUNET_free (dc->temp_filename);
2299   }
2300   GNUNET_free_non_null (dc->serialization);
2301   GNUNET_free (dc);
2302 }
2303
2304 /* end of fs_download.c */