-trying to fix #2391: limit connect rate from topology
[oweals/gnunet.git] / src / fs / gnunet-download.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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/gnunet-download.c
22  * @brief downloading for files on GNUnet
23  * @author Christian Grothoff
24  * @author Krista Bennett
25  * @author James Blackwell
26  * @author Igor Wronsky
27  */
28 #include "platform.h"
29 #include "gnunet_fs_service.h"
30
31 static int ret;
32
33 static int verbose;
34
35 static int delete_incomplete;
36
37 static const struct GNUNET_CONFIGURATION_Handle *cfg;
38
39 static struct GNUNET_FS_Handle *ctx;
40
41 static struct GNUNET_FS_DownloadContext *dc;
42
43 static unsigned int anonymity = 1;
44
45 static unsigned int parallelism = 16;
46
47 static unsigned int request_parallelism = 4092;
48
49 static int do_recursive;
50
51 static char *filename;
52
53 static int local_only;
54
55 static void
56 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
57 {
58   GNUNET_FS_stop (ctx);
59   ctx = NULL;
60 }
61
62
63 static void
64 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
65 {
66   struct GNUNET_FS_DownloadContext *d;
67
68   if (dc != NULL)
69   {
70     d = dc;
71     dc = NULL;
72     GNUNET_FS_download_stop (d, delete_incomplete);
73   }
74 }
75
76
77 /**
78  * Called by FS client to give information about the progress of an
79  * operation.
80  *
81  * @param cls closure
82  * @param info details about the event, specifying the event type
83  *        and various bits about the event
84  * @return client-context (for the next progress call
85  *         for this operation; should be set to NULL for
86  *         SUSPEND and STOPPED events).  The value returned
87  *         will be passed to future callbacks in the respective
88  *         field in the GNUNET_FS_ProgressInfo struct.
89  */
90 static void *
91 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
92 {
93   char *s;
94   char *s2;
95   char *t;
96
97   switch (info->status)
98   {
99   case GNUNET_FS_STATUS_DOWNLOAD_START:
100     if (verbose > 1)
101       FPRINTF (stderr, _("Starting download `%s'.\n"),
102                info->value.download.filename);
103     break;
104   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
105     if (verbose)
106     {
107       s = GNUNET_STRINGS_relative_time_to_string (info->value.download.eta);
108       if (info->value.download.specifics.progress.block_download_duration.rel_value 
109           == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
110         s2 = GNUNET_strdup (_("<unknown time>"));
111       else
112         s2 = GNUNET_STRINGS_relative_time_to_string (
113               info->value.download.specifics.progress.block_download_duration);
114       t = GNUNET_STRINGS_byte_size_fancy (info->value.download.completed *
115                                           1000LL /
116                                           (info->value.download.
117                                            duration.rel_value + 1));
118       FPRINTF (stdout,
119                _("Downloading `%s' at %llu/%llu (%s remaining, %s/s). Block took %s to download\n"),
120                info->value.download.filename,
121                (unsigned long long) info->value.download.completed,
122                (unsigned long long) info->value.download.size, s, t, s2);
123       GNUNET_free (s);
124       GNUNET_free (s2);
125       GNUNET_free (t);
126     }
127     break;
128   case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
129     FPRINTF (stderr, _("Error downloading: %s.\n"),
130              info->value.download.specifics.error.message);
131     GNUNET_SCHEDULER_shutdown ();
132     break;
133   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
134     s = GNUNET_STRINGS_byte_size_fancy (info->value.download.completed * 1000 /
135                                         (info->value.download.
136                                          duration.rel_value + 1));
137     FPRINTF (stdout, _("Downloading `%s' done (%s/s).\n"),
138              info->value.download.filename, s);
139     GNUNET_free (s);
140     if (info->value.download.dc == dc)
141       GNUNET_SCHEDULER_shutdown ();
142     break;
143   case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
144     if (info->value.download.dc == dc)
145       GNUNET_SCHEDULER_add_continuation (&cleanup_task, NULL,
146                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
147     break;
148   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
149   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
150     break;
151   default:
152     FPRINTF (stderr, _("Unexpected status: %d\n"), info->status);
153     break;
154   }
155   return NULL;
156 }
157
158
159 /**
160  * Main function that will be run by the scheduler.
161  *
162  * @param cls closure
163  * @param args remaining command-line arguments
164  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
165  * @param c configuration
166  */
167 static void
168 run (void *cls, char *const *args, const char *cfgfile,
169      const struct GNUNET_CONFIGURATION_Handle *c)
170 {
171   struct GNUNET_FS_Uri *uri;
172   char *emsg;
173   enum GNUNET_FS_DownloadOptions options;
174
175   if (NULL == args[0])
176   {
177     FPRINTF (stderr, "%s",  _("You need to specify a URI argument.\n"));
178     return;
179   }
180   uri = GNUNET_FS_uri_parse (args[0], &emsg);
181   if (NULL == uri)
182   {
183     FPRINTF (stderr, _("Failed to parse URI: %s\n"), emsg);
184     GNUNET_free (emsg);
185     ret = 1;
186     return;
187   }
188   if ((!GNUNET_FS_uri_test_chk (uri)) && (!GNUNET_FS_uri_test_loc (uri)))
189   {
190     FPRINTF (stderr, "%s",  _("Only CHK or LOC URIs supported.\n"));
191     ret = 1;
192     GNUNET_FS_uri_destroy (uri);
193     return;
194   }
195   if (NULL == filename)
196   {
197     FPRINTF (stderr, "%s",  _("Target filename must be specified.\n"));
198     ret = 1;
199     GNUNET_FS_uri_destroy (uri);
200     return;
201   }
202   cfg = c;
203   ctx =
204       GNUNET_FS_start (cfg, "gnunet-download", &progress_cb, NULL,
205                        GNUNET_FS_FLAGS_NONE,
206                        GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM, parallelism,
207                        GNUNET_FS_OPTIONS_REQUEST_PARALLELISM,
208                        request_parallelism, GNUNET_FS_OPTIONS_END);
209   if (NULL == ctx)
210   {
211     FPRINTF (stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
212     GNUNET_FS_uri_destroy (uri);
213     ret = 1;
214     return;
215   }
216   options = GNUNET_FS_DOWNLOAD_OPTION_NONE;
217   if (do_recursive)
218     options |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE;
219   if (local_only)
220     options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY;
221   dc = GNUNET_FS_download_start (ctx, uri, NULL, filename, NULL, 0,
222                                  GNUNET_FS_uri_chk_get_file_size (uri),
223                                  anonymity, options, NULL, NULL);
224   GNUNET_FS_uri_destroy (uri);
225   if (dc == NULL)
226   {
227     GNUNET_FS_stop (ctx);
228     ctx = NULL;
229     return;
230   }
231   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
232                                 NULL);
233 }
234
235
236 /**
237  * The main function to download GNUnet.
238  *
239  * @param argc number of arguments from the command line
240  * @param argv command line arguments
241  * @return 0 ok, 1 on error
242  */
243 int
244 main (int argc, char *const *argv)
245 {
246   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
247     {'a', "anonymity", "LEVEL",
248      gettext_noop ("set the desired LEVEL of receiver-anonymity"),
249      1, &GNUNET_GETOPT_set_uint, &anonymity},
250     {'D', "delete-incomplete", NULL,
251      gettext_noop ("delete incomplete downloads (when aborted with CTRL-C)"),
252      0, &GNUNET_GETOPT_set_one, &delete_incomplete},
253     {'n', "no-network", NULL,
254      gettext_noop ("only search the local peer (no P2P network search)"),
255      0, &GNUNET_GETOPT_set_uint, &local_only},
256     {'o', "output", "FILENAME",
257      gettext_noop ("write the file to FILENAME"),
258      1, &GNUNET_GETOPT_set_string, &filename},
259     {'p', "parallelism", "DOWNLOADS",
260      gettext_noop
261      ("set the maximum number of parallel downloads that is allowed"),
262      1, &GNUNET_GETOPT_set_uint, &parallelism},
263     {'r', "request-parallelism", "REQUESTS",
264      gettext_noop
265      ("set the maximum number of parallel requests for blocks that is allowed"),
266      1, &GNUNET_GETOPT_set_uint, &request_parallelism},
267     {'R', "recursive", NULL,
268      gettext_noop ("download a GNUnet directory recursively"),
269      0, &GNUNET_GETOPT_set_one, &do_recursive},
270     {'V', "verbose", NULL,
271      gettext_noop ("be verbose (print progress information)"),
272      0, &GNUNET_GETOPT_increment_value, &verbose},
273     GNUNET_GETOPT_OPTION_END
274   };
275   return (GNUNET_OK ==
276           GNUNET_PROGRAM_run (argc, argv, "gnunet-download [OPTIONS] URI",
277                               gettext_noop
278                               ("Download files from GNUnet using a GNUnet CHK or LOC URI (gnunet://fs/chk/...)"),
279                               options, &run, NULL)) ? ret : 1;
280 }
281
282 /* end of gnunet-download.c */