main.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "HttpInitiateConnectionCommand.h"
  36. #include "ConsoleDownloadEngine.h"
  37. #include "SegmentMan.h"
  38. #include "LogFactory.h"
  39. #include "common.h"
  40. #include "DefaultDiskWriter.h"
  41. #include "Util.h"
  42. #include "InitiateConnectionCommandFactory.h"
  43. #include "prefs.h"
  44. #include "FeatureConfig.h"
  45. #include "DownloadEngineFactory.h"
  46. #include "UrlRequestInfo.h"
  47. #include "TorrentRequestInfo.h"
  48. #include <deque>
  49. #include <algorithm>
  50. #include <time.h>
  51. #include <signal.h>
  52. #include <unistd.h>
  53. #include <libgen.h>
  54. #include <utility>
  55. extern char* optarg;
  56. extern int optind, opterr, optopt;
  57. #include <getopt.h>
  58. #ifdef ENABLE_METALINK
  59. #include "MetalinkRequestInfo.h"
  60. #include "Xml2MetalinkProcessor.h"
  61. #endif
  62. #ifdef HAVE_LIBSSL
  63. // for SSL
  64. # include <openssl/err.h>
  65. # include <openssl/ssl.h>
  66. #endif // HAVE_LIBSSL
  67. #ifdef HAVE_LIBGNUTLS
  68. # include <gnutls/gnutls.h>
  69. #endif // HAVE_LIBGNUTLS
  70. using namespace std;
  71. RequestInfo* requestInfo;
  72. bool timeoutSpecified;
  73. void setSignalHander(int signal, void (*handler)(int), int flags) {
  74. struct sigaction sigact;
  75. sigact.sa_handler = handler;
  76. sigact.sa_flags = flags;
  77. sigemptyset(&sigact.sa_mask);
  78. sigaction(signal, &sigact, NULL);
  79. }
  80. void showVersion() {
  81. cout << PACKAGE << _(" version ") << PACKAGE_VERSION << endl;
  82. cout << "**Configuration**" << endl;
  83. cout << FeatureConfig::getInstance()->getConfigurationSummary();
  84. cout << endl;
  85. cout << "Copyright (C) 2006 Tatsuhiro Tsujikawa" << endl;
  86. cout << endl;
  87. cout <<
  88. _("This program is free software; you can redistribute it and/or modify\n"
  89. "it under the terms of the GNU General Public License as published by\n"
  90. "the Free Software Foundation; either version 2 of the License, or\n"
  91. "(at your option) any later version.\n"
  92. "\n"
  93. "This program is distributed in the hope that it will be useful,\n"
  94. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  95. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  96. "GNU General Public License for more details.\n"
  97. "\n"
  98. "You should have received a copy of the GNU General Public License\n"
  99. "along with this program; if not, write to the Free Software\n"
  100. "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n");
  101. cout << endl;
  102. printf(_("Contact Info: %s\n"), "Tasuhiro Tsujikawa <tujikawa at users dot sourceforge dot net>");
  103. cout << endl;
  104. }
  105. void showUsage() {
  106. printf(_("Usage: %s [options] URL ...\n"), PACKAGE_NAME);
  107. #ifdef ENABLE_BITTORRENT
  108. printf(_(" %s [options] -T TORRENT_FILE FILE ...\n"), PACKAGE_NAME);
  109. #endif // ENABLE_BITTORRENT
  110. #ifdef ENABLE_METALINK
  111. printf(_(" %s [options] -M METALINK_FILE\n"), PACKAGE_NAME);
  112. #endif // ENABLE_METALINK
  113. cout << endl;
  114. cout << _("Options:") << endl;
  115. cout << _(" -d, --dir=DIR The directory to store downloaded file.") << endl;
  116. cout << _(" -o, --out=FILE The file name for downloaded file.") << endl;
  117. cout << _(" -l, --log=LOG The file path to store log. If '-' is specified,\n"
  118. " log is written to stdout.") << endl;
  119. cout << _(" -D, --daemon Run as daemon.") << endl;
  120. cout << _(" -s, --split=N Download a file using N connections. N must be\n"
  121. " between 1 and 5. This option affects all URLs.\n"
  122. " Thus, aria2 connects to each URL with\n"
  123. " N connections.") << endl;
  124. cout << _(" --retry-wait=SEC Set amount of time in second between requests\n"
  125. " for errors. Specify a value between 0 and 60.\n"
  126. " Default: 5") << endl;
  127. cout << _(" -t, --timeout=SEC Set timeout in second. Default: 60") << endl;
  128. cout << _(" -m, --max-tries=N Set number of tries. 0 means unlimited.\n"
  129. " Default: 5") << endl;
  130. /*
  131. cout << _(" --min-segment-size=SIZE[K|M] Set minimum segment size. You can append\n"
  132. " K or M(1K = 1024, 1M = 1024K). This\n"
  133. " value must be greater than or equal to\n"
  134. " 1024. Default: 1M") << endl;
  135. */
  136. cout << _(" --http-proxy=HOST:PORT Use HTTP proxy server. This affects to all\n"
  137. " URLs.") << endl;
  138. cout << _(" --http-user=USER Set HTTP user. This affects to all URLs.") << endl;
  139. cout << _(" --http-passwd=PASSWD Set HTTP password. This affects to all URLs.") << endl;
  140. cout << _(" --http-proxy-user=USER Set HTTP proxy user. This affects to all URLs") << endl;
  141. cout << _(" --http-proxy-passwd=PASSWD Set HTTP proxy password. This affects to all URLs.") << endl;
  142. cout << _(" --http-proxy-method=METHOD Set the method to use in proxy request.\n"
  143. " METHOD is either 'get' or 'tunnel'.\n"
  144. " Default: tunnel") << endl;
  145. cout << _(" --http-auth-scheme=SCHEME Set HTTP authentication scheme. Currently, basic\n"
  146. " is the only supported scheme.\n"
  147. " Default: basic") << endl;
  148. cout << _(" --referer=REFERER Set Referer. This affects to all URLs.") << endl;
  149. cout << _(" --ftp-user=USER Set FTP user. This affects to all URLs.\n"
  150. " Default: anonymous") << endl;
  151. cout << _(" --ftp-passwd=PASSWD Set FTP password. This affects to all URLs.\n"
  152. " Default: ARIA2USER@") << endl;
  153. cout << _(" --ftp-type=TYPE Set FTP transfer type. TYPE is either 'binary'\n"
  154. " or 'ascii'.\n"
  155. " Default: binary") << endl;
  156. cout << _(" -p, --ftp-pasv Use passive mode in FTP.") << endl;
  157. cout << _(" --ftp-via-http-proxy=METHOD Use HTTP proxy in FTP. METHOD is either 'get' or\n"
  158. " 'tunnel'.\n"
  159. " Default: tunnel") << endl;
  160. cout << _(" --lowest-speed-limit Close connection if download speed is lower than\n"
  161. " or equal to this value(bytes per sec).\n"
  162. " 0 means aria2 does not care lowest speed limit.\n"
  163. " You can append K or M(1K = 1024, 1M = 1024K).\n"
  164. " This option does not affect BitTorrent download.\n"
  165. " Default: 0") << endl;
  166. cout << _(" --max-download-limit Set max download speed in bytes per sec.\n"
  167. " 0 means unrestricted.\n"
  168. " You can append K or M(1K = 1024, 1M = 1024K).\n"
  169. " Default: 0") << endl;
  170. #ifdef ENABLE_BITTORRENT
  171. cout << _(" -T, --torrent-file=TORRENT_FILE The file path to .torrent file.") << endl;
  172. cout << _(" --follow-torrent=true|false Setting this option to false prevents aria2 to\n"
  173. " enter BitTorrent mode even if the filename of\n"
  174. " downloaded file ends with .torrent.\n"
  175. " Default: true") << endl;
  176. cout << _(" -S, --show-files Print file listing of .torrent file and exit.") << endl;
  177. cout << _(" --direct-file-mapping=true|false Directly read from and write to each file\n"
  178. " mentioned in .torrent file.\n"
  179. " Default: true") << endl;
  180. cout << _(" --listen-port=PORT Set port number to listen to for peer connection.") << endl;
  181. cout << _(" --max-upload-limit Set max upload speed in bytes per sec.\n"
  182. " 0 means unrestricted.\n"
  183. " You can append K or M(1K = 1024, 1M = 1024K).\n"
  184. " Default: 0") << endl;
  185. cout << _(" --select-file=INDEX... Set file to download by specifing its index.\n"
  186. " You can know file index through --show-files\n"
  187. " option. Multiple indexes can be specified by using\n"
  188. " ',' like \"3,6\".\n"
  189. " You can also use '-' to specify rangelike \"1-5\".\n"
  190. " ',' and '-' can be used together.") << endl;
  191. cout << _(" --seed-time=MINUTES Specify seeding time in minutes. See also\n"
  192. " --seed-ratio option.") << endl;
  193. cout << _(" --seed-ratio=RATIO Specify share ratio. Seed completed torrents until\n"
  194. " share ratio reaches RATIO. 1.0 is encouraged.\n"
  195. " If --seed-time option is specified along with\n"
  196. " this option, seeding ends when at least one of\n"
  197. " the conditions is satisfied.") << endl;
  198. #endif // ENABLE_BITTORRENT
  199. #ifdef ENABLE_METALINK
  200. cout << _(" -M, --metalink-file=METALINK_FILE The file path to .metalink file.") << endl;
  201. cout << _(" -C, --metalink-servers=NUM_SERVERS The number of servers to connect to\n"
  202. " simultaneously. If more than one connection per\n"
  203. " server is required, use -s option.\n"
  204. " Default: 15") << endl;
  205. cout << _(" --metalink-version=VERSION The version of file to download.") << endl;
  206. cout << _(" --metalink-language=LANGUAGE The language of file to download.") << endl;
  207. cout << _(" --metalink-os=OS The operating system the file is targeted.") << endl;
  208. cout << _(" --follow-metalink=true|false Setting this option to false prevents aria2 to\n"
  209. " enter Metalink mode even if the filename of\n"
  210. " downloaded file ends with .metalink.\n"
  211. " Default: true") << endl;
  212. #endif // ENABLE_METALINK
  213. cout << _(" -v, --version Print the version number and exit.") << endl;
  214. cout << _(" -h, --help Print this message and exit.") << endl;
  215. cout << endl;
  216. cout << "URL:" << endl;
  217. cout << _(" You can specify multiple URLs. All URLs must point to the same file\n"
  218. " or downloading fails.") << endl;
  219. cout << endl;
  220. #ifdef ENABLE_BITTORRENT
  221. cout << "FILE:" << endl;
  222. cout << _(" Specify files in multi-file torrent to download. Use conjunction with\n"
  223. " -T option. This arguments are ignored if you specify --select-file option.") << endl;
  224. cout << endl;
  225. #endif // ENABLE_BITTORRENT
  226. cout << _("Examples:") << endl;
  227. cout << _(" Download a file by 1 connection:") << endl;
  228. cout << " aria2c http://AAA.BBB.CCC/file.zip" << endl;
  229. cout << _(" Download a file by 2 connections:") << endl;
  230. cout << " aria2c -s 2 http://AAA.BBB.CCC/file.zip" << endl;
  231. cout << _(" Download a file by 2 connections, each connects to a different server:") << endl;
  232. cout << " aria2c http://AAA.BBB.CCC/file.zip http://DDD.EEE.FFF/GGG/file.zip" << endl;
  233. cout << _(" You can mix up different protocols:") << endl;
  234. cout << " aria2c http://AAA.BBB.CCC/file.zip ftp://DDD.EEE.FFF/GGG/file.zip" << endl;
  235. #ifdef ENABLE_BITTORRENT
  236. cout << endl;
  237. cout << _(" Download a torrent:") << endl;
  238. cout << " aria2c -o test.torrent http://AAA.BBB.CCC/file.torrent" << endl;
  239. cout << _(" Download a torrent using local .torrent file:") << endl;
  240. cout << " aria2c -T test.torrent" << endl;
  241. cout << _(" Download only selected files:") << endl;
  242. cout << " aria2c -T test.torrent dir/file1.zip dir/file2.zip" << endl;
  243. cout << _(" Print file listing of .torrent file:") << endl;
  244. cout << " aria2c -T test.torrent -S" << endl;
  245. #endif // ENABLE_BITTORRENT
  246. #ifdef ENABLE_METALINK
  247. cout << endl;
  248. cout << _(" Metalink downloading:") << endl;
  249. cout << " aria2c http://AAA.BBB.CCC/file.metalink" << endl;
  250. cout << _(" Download a file using local .metalink file:") << endl;
  251. cout << " aria2c -M test.metalink" << endl;
  252. cout << _(" Metalink downloading with preferences:") << endl;
  253. cout << " aria2c -M test.metalink --metalink-version=1.1.1 --metalink-language=en-US" << endl;
  254. #endif // ENABLE_METALINK
  255. cout << endl;
  256. printf(_("Report bugs to %s"), "<tujikawa at users dot sourceforge dot net>");
  257. cout << endl;
  258. }
  259. long long int getRealSize(char* optarg) {
  260. string::size_type p = string(optarg).find_first_of("KM");
  261. int mult = 1;
  262. if(p != string::npos) {
  263. if(optarg[p] == 'K') {
  264. mult = 1024;
  265. } else if(optarg[p] == 'M') {
  266. mult = 1024*1024;
  267. }
  268. optarg[p] = '\0';
  269. }
  270. long long int size = strtoll(optarg, NULL, 10)*mult;
  271. return size;
  272. }
  273. int main(int argc, char* argv[]) {
  274. #ifdef ENABLE_NLS
  275. setlocale (LC_CTYPE, "");
  276. setlocale (LC_MESSAGES, "");
  277. bindtextdomain (PACKAGE, LOCALEDIR);
  278. textdomain (PACKAGE);
  279. #endif // ENABLE_NLS
  280. timeoutSpecified = false;
  281. int c;
  282. Option* op = new Option();
  283. op->put(PREF_STDOUT_LOG, V_FALSE);
  284. op->put(PREF_DIR, ".");
  285. op->put(PREF_SPLIT, "1");
  286. op->put(PREF_DAEMON, V_FALSE);
  287. op->put(PREF_SEGMENT_SIZE, Util::itos(1024*1024));
  288. op->put(PREF_HTTP_KEEP_ALIVE, V_FALSE);
  289. op->put(PREF_LISTEN_PORT, "-1");
  290. op->put(PREF_METALINK_SERVERS, "15");
  291. op->put(PREF_FOLLOW_TORRENT,
  292. #ifdef ENABLE_BITTORRENT
  293. V_TRUE
  294. #else
  295. V_FALSE
  296. #endif // ENABLE_BITTORRENT
  297. );
  298. op->put(PREF_FOLLOW_METALINK,
  299. #ifdef ENABLE_METALINK
  300. V_TRUE
  301. #else
  302. V_FALSE
  303. #endif // ENABLE_METALINK
  304. );
  305. op->put(PREF_RETRY_WAIT, "5");
  306. op->put(PREF_TIMEOUT, "60");
  307. op->put(PREF_PEER_CONNECTION_TIMEOUT, "60");
  308. op->put(PREF_MIN_SEGMENT_SIZE, "1048576");// 1M
  309. op->put(PREF_MAX_TRIES, "5");
  310. op->put(PREF_HTTP_AUTH_SCHEME, V_BASIC);
  311. op->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  312. op->put(PREF_FTP_USER, "anonymous");
  313. op->put(PREF_FTP_PASSWD, "ARIA2USER@");
  314. op->put(PREF_FTP_TYPE, V_BINARY);
  315. op->put(PREF_FTP_VIA_HTTP_PROXY, V_TUNNEL);
  316. op->put(PREF_AUTO_SAVE_INTERVAL, "60");
  317. op->put(PREF_DIRECT_FILE_MAPPING, V_TRUE);
  318. op->put(PREF_LOWEST_SPEED_LIMIT, "0");
  319. op->put(PREF_MAX_DOWNLOAD_LIMIT, "0");
  320. op->put(PREF_MAX_UPLOAD_LIMIT, "0");
  321. op->put(PREF_STARTUP_IDLE_TIME, "10");
  322. while(1) {
  323. int optIndex = 0;
  324. int lopt;
  325. static struct option longOpts[] = {
  326. { "daemon", no_argument, NULL, 'D' },
  327. { "dir", required_argument, NULL, 'd' },
  328. { "out", required_argument, NULL, 'o' },
  329. { "log", required_argument, NULL, 'l' },
  330. { "split", required_argument, NULL, 's' },
  331. { "timeout", required_argument, NULL, 't' },
  332. { "max-tries", required_argument, NULL, 'm' },
  333. { "http-proxy", required_argument, &lopt, 1 },
  334. { "http-user", required_argument, &lopt, 2 },
  335. { "http-passwd", required_argument, &lopt, 3 },
  336. { "http-proxy-user", required_argument, &lopt, 4 },
  337. { "http-proxy-passwd", required_argument, &lopt, 5 },
  338. { "http-auth-scheme", required_argument, &lopt, 6 },
  339. { "referer", required_argument, &lopt, 7 },
  340. { "retry-wait", required_argument, &lopt, 8 },
  341. { "ftp-user", required_argument, &lopt, 9 },
  342. { "ftp-passwd", required_argument, &lopt, 10 },
  343. { "ftp-type", required_argument, &lopt, 11 },
  344. { "ftp-pasv", no_argument, NULL, 'p' },
  345. { "ftp-via-http-proxy", required_argument, &lopt, 12 },
  346. //{ "min-segment-size", required_argument, &lopt, 13 },
  347. { "http-proxy-method", required_argument, &lopt, 14 },
  348. { "lowest-speed-limit", required_argument, &lopt, 200 },
  349. { "max-download-limit", required_argument, &lopt, 201 },
  350. #ifdef ENABLE_BITTORRENT
  351. { "torrent-file", required_argument, NULL, 'T' },
  352. { "listen-port", required_argument, &lopt, 15 },
  353. { "follow-torrent", required_argument, &lopt, 16 },
  354. { "show-files", no_argument, NULL, 'S' },
  355. { "no-preallocation", no_argument, &lopt, 18 },
  356. { "direct-file-mapping", required_argument, &lopt, 19 },
  357. // TODO remove upload-limit.
  358. { "upload-limit", required_argument, &lopt, 20 },
  359. { "select-file", required_argument, &lopt, 21 },
  360. { "seed-time", required_argument, &lopt, 22 },
  361. { "seed-ratio", required_argument, &lopt, 23 },
  362. { "max-upload-limit", required_argument, &lopt, 24 },
  363. #endif // ENABLE_BITTORRENT
  364. #ifdef ENABLE_METALINK
  365. { "metalink-file", required_argument, NULL, 'M' },
  366. { "metalink-servers", required_argument, NULL, 'C' },
  367. { "metalink-version", required_argument, &lopt, 100 },
  368. { "metalink-language", required_argument, &lopt, 101 },
  369. { "metalink-os", required_argument, &lopt, 102 },
  370. { "follow-metalink", required_argument, &lopt, 103 },
  371. #endif // ENABLE_METALINK
  372. { "version", no_argument, NULL, 'v' },
  373. { "help", no_argument, NULL, 'h' },
  374. { 0, 0, 0, 0 }
  375. };
  376. c = getopt_long(argc, argv, "Dd:o:l:s:pt:m:vhST:M:C:", longOpts, &optIndex);
  377. if(c == -1) {
  378. break;
  379. }
  380. switch(c) {
  381. case 0:{
  382. switch(lopt) {
  383. case 1: {
  384. pair<string, string> proxy;
  385. Util::split(proxy, optarg, ':');
  386. int port = (int)strtol(proxy.second.c_str(), NULL, 10);
  387. if(proxy.first.empty() || proxy.second.empty() ||
  388. !(0 < port && port <= 65535)) {
  389. cerr << _("unrecognized proxy format") << endl;
  390. showUsage();
  391. exit(EXIT_FAILURE);
  392. }
  393. op->put(PREF_HTTP_PROXY_HOST, proxy.first);
  394. op->put(PREF_HTTP_PROXY_PORT, Util::itos(port));
  395. op->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  396. break;
  397. }
  398. case 2:
  399. op->put(PREF_HTTP_USER, optarg);
  400. op->put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
  401. break;
  402. case 3:
  403. op->put(PREF_HTTP_PASSWD, optarg);
  404. break;
  405. case 4:
  406. op->put(PREF_HTTP_PROXY_USER, optarg);
  407. op->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  408. break;
  409. case 5:
  410. op->put(PREF_HTTP_PROXY_PASSWD, optarg);
  411. break;
  412. case 6:
  413. if(string(V_BASIC) == optarg) {
  414. op->put(PREF_HTTP_AUTH_SCHEME, V_BASIC);
  415. } else {
  416. cerr << _("Currently, supported authentication scheme is basic.") << endl;
  417. }
  418. break;
  419. case 7:
  420. op->put(PREF_REFERER, optarg);
  421. break;
  422. case 8: {
  423. int wait = (int)strtol(optarg, NULL, 10);
  424. if(!(0 <= wait && wait <= 60)) {
  425. cerr << _("retry-wait must be between 0 and 60.") << endl;
  426. showUsage();
  427. exit(EXIT_FAILURE);
  428. }
  429. op->put(PREF_RETRY_WAIT, Util::itos(wait));
  430. break;
  431. }
  432. case 9:
  433. op->put(PREF_FTP_USER, optarg);
  434. break;
  435. case 10:
  436. op->put(PREF_FTP_PASSWD, optarg);
  437. break;
  438. case 11:
  439. if(string(optarg) == V_BINARY || string(optarg) == V_ASCII) {
  440. op->put(PREF_FTP_TYPE, optarg);
  441. } else {
  442. cerr << _("ftp-type must be either 'binary' or 'ascii'.") << endl;
  443. showUsage();
  444. exit(EXIT_FAILURE);
  445. }
  446. break;
  447. case 12:
  448. if(string(optarg) == V_GET || string(optarg) == V_TUNNEL) {
  449. op->put(PREF_FTP_VIA_HTTP_PROXY, optarg);
  450. } else {
  451. cerr << _("ftp-via-http-proxy must be either 'get' or 'tunnel'.") << endl;
  452. showUsage();
  453. exit(EXIT_FAILURE);
  454. }
  455. break;
  456. case 13: {
  457. long long int size = getRealSize(optarg);
  458. if(size < 1024) {
  459. cerr << _("min-segment-size invalid") << endl;
  460. showUsage();
  461. exit(EXIT_FAILURE);
  462. }
  463. op->put(PREF_MIN_SEGMENT_SIZE, Util::llitos(size));
  464. break;
  465. }
  466. case 14:
  467. if(string(optarg) == V_GET || string(optarg) == V_TUNNEL) {
  468. op->put(PREF_HTTP_PROXY_METHOD, optarg);
  469. } else {
  470. cerr << _("http-proxy-method must be either 'get' or 'tunnel'.") << endl;
  471. showUsage();
  472. exit(EXIT_FAILURE);
  473. }
  474. break;
  475. case 15: {
  476. int listenPort = (int)strtol(optarg, NULL, 10);
  477. if(!(1024 <= listenPort && listenPort <= 65535)) {
  478. cerr << _("listen-port must be between 1024 and 65535.") << endl;
  479. showUsage();
  480. exit(EXIT_FAILURE);
  481. }
  482. op->put(PREF_LISTEN_PORT, Util::itos(listenPort));
  483. break;
  484. }
  485. case 16:
  486. if(string(optarg) == "true") {
  487. op->put(PREF_FOLLOW_TORRENT, V_TRUE);
  488. } else if(string(optarg) == "false") {
  489. op->put(PREF_FOLLOW_TORRENT, V_FALSE);
  490. } else {
  491. cerr << _("follow-torrent must be either 'true' or 'false'.") << endl;
  492. showUsage();
  493. exit(EXIT_FAILURE);
  494. }
  495. break;
  496. case 18:
  497. op->put(PREF_NO_PREALLOCATION, V_TRUE);
  498. break;
  499. case 19:
  500. if(string(optarg) == "true") {
  501. op->put(PREF_DIRECT_FILE_MAPPING, V_TRUE);
  502. } else if(string(optarg) == "false") {
  503. op->put(PREF_DIRECT_FILE_MAPPING, V_FALSE);
  504. } else {
  505. cerr << _("direct-file-mapping must be either 'true' or 'false'.") << endl;
  506. showUsage();
  507. exit(EXIT_FAILURE);
  508. }
  509. break;
  510. case 20: {
  511. cerr << "Warning: upload-limit will be deprecated in the future release.\n"
  512. "Use max-upload-limit instead. Because there is a difference between them,\n"
  513. "take a look at the description of max-upload-limit option." << endl;
  514. int uploadSpeed = strtol(optarg, NULL, 10)*1024;
  515. if(0 > uploadSpeed) {
  516. cerr << _("upload-limit must be greater than or equal to 0.") << endl;
  517. showUsage();
  518. exit(EXIT_FAILURE);
  519. }
  520. op->put(PREF_MAX_UPLOAD_LIMIT, Util::itos(uploadSpeed));
  521. break;
  522. }
  523. case 21:
  524. op->put(PREF_SELECT_FILE, optarg);
  525. break;
  526. case 22: {
  527. int seedTime = (int)strtol(optarg, NULL, 10);
  528. if(seedTime < 0) {
  529. cerr << _("seed-time must be greater than or equal to 0.") << endl;
  530. showUsage();
  531. exit(EXIT_FAILURE);
  532. }
  533. op->put(PREF_SEED_TIME, Util::itos(seedTime));
  534. break;
  535. }
  536. case 23: {
  537. double ratio = (int)strtod(optarg, NULL);
  538. if(ratio < 0.0) {
  539. cerr << _("seed-ratio must be greater than or equal to 0.0.") << endl;
  540. showUsage();
  541. exit(EXIT_FAILURE);
  542. }
  543. op->put(PREF_SEED_RATIO, optarg);
  544. break;
  545. }
  546. case 24: {
  547. int limit = getRealSize(optarg);
  548. if(limit < 0) {
  549. cerr << _("max-upload-limit must be greater than or equal to 0") << endl;
  550. showUsage();
  551. exit(EXIT_FAILURE);
  552. }
  553. op->put(PREF_MAX_UPLOAD_LIMIT, Util::itos(limit));
  554. break;
  555. }
  556. case 100:
  557. op->put(PREF_METALINK_VERSION, optarg);
  558. break;
  559. case 101:
  560. op->put(PREF_METALINK_LANGUAGE, optarg);
  561. break;
  562. case 102:
  563. op->put(PREF_METALINK_OS, optarg);
  564. break;
  565. case 103:
  566. if(string(optarg) == "true") {
  567. op->put(PREF_FOLLOW_METALINK, V_TRUE);
  568. } else if(string(optarg) == "false") {
  569. op->put(PREF_FOLLOW_METALINK, V_FALSE);
  570. } else {
  571. cerr << _("follow-metalink must be either 'true' or 'false'.") << endl;
  572. showUsage();
  573. exit(EXIT_FAILURE);
  574. }
  575. break;
  576. case 200: {
  577. int limit = getRealSize(optarg);
  578. if(limit < 0) {
  579. cerr << _("lowest-speed-limit must be greater than or equal to 0") << endl;
  580. showUsage();
  581. exit(EXIT_FAILURE);
  582. }
  583. op->put(PREF_LOWEST_SPEED_LIMIT, Util::itos(limit));
  584. break;
  585. }
  586. case 201: {
  587. int limit = getRealSize(optarg);
  588. if(limit < 0) {
  589. cerr << _("max-download-limit must be greater than or equal to 0") << endl;
  590. showUsage();
  591. exit(EXIT_FAILURE);
  592. }
  593. op->put(PREF_MAX_DOWNLOAD_LIMIT, Util::itos(limit));
  594. break;
  595. }
  596. }
  597. break;
  598. }
  599. case 'D':
  600. op->put(PREF_DAEMON, V_TRUE);
  601. break;
  602. case 'd':
  603. op->put(PREF_DIR, optarg);
  604. break;
  605. case 'o':
  606. op->put(PREF_OUT, optarg);
  607. break;
  608. case 'l':
  609. if(strcmp("-", optarg) == 0) {
  610. op->put(PREF_STDOUT_LOG, V_TRUE);
  611. } else {
  612. op->put(PREF_LOG, optarg);
  613. }
  614. break;
  615. case 's': {
  616. int split = (int)strtol(optarg, NULL, 10);
  617. if(!(1 <= split && split <= 5)) {
  618. cerr << _("split must be between 1 and 5.") << endl;
  619. showUsage();
  620. exit(EXIT_FAILURE);
  621. }
  622. op->put(PREF_SPLIT, Util::itos(split));
  623. break;
  624. }
  625. case 't': {
  626. int timeout = (int)strtol(optarg, NULL, 10);
  627. if(1 <= timeout && timeout <= 600) {
  628. op->put(PREF_TIMEOUT, Util::itos(timeout));
  629. timeoutSpecified = true;
  630. } else {
  631. cerr << _("timeout must be between 1 and 600") << endl;
  632. showUsage();
  633. exit(EXIT_FAILURE);
  634. }
  635. break;
  636. }
  637. case 'm': {
  638. int retries = (int)strtol(optarg, NULL, 10);
  639. if(retries < 0) {
  640. cerr << _("max-tries invalid") << endl;
  641. showUsage();
  642. exit(EXIT_FAILURE);
  643. }
  644. op->put(PREF_MAX_TRIES, Util::itos(retries));
  645. break;
  646. }
  647. case 'p':
  648. op->put(PREF_FTP_PASV_ENABLED, V_TRUE);
  649. break;
  650. case 'S':
  651. op->put(PREF_SHOW_FILES, V_TRUE);
  652. break;
  653. case 'T':
  654. op->put(PREF_TORRENT_FILE, optarg);
  655. break;
  656. case 'M':
  657. op->put(PREF_METALINK_FILE, optarg);
  658. break;
  659. case 'C': {
  660. int metalinkServers = (int)strtol(optarg, NULL, 10);
  661. if(metalinkServers <= 0) {
  662. cerr << _("metalink-servers must be greater than 0.") << endl;
  663. showUsage();
  664. exit(EXIT_FAILURE);
  665. }
  666. op->put(PREF_METALINK_SERVERS, Util::itos(metalinkServers));
  667. break;
  668. }
  669. case 'v':
  670. showVersion();
  671. exit(EXIT_SUCCESS);
  672. case 'h':
  673. showUsage();
  674. exit(EXIT_SUCCESS);
  675. default:
  676. showUsage();
  677. exit(EXIT_FAILURE);
  678. }
  679. }
  680. if(!op->defined(PREF_TORRENT_FILE) && !op->defined(PREF_METALINK_FILE)) {
  681. if(optind == argc) {
  682. cerr << _("specify at least one URL") << endl;
  683. showUsage();
  684. exit(EXIT_FAILURE);
  685. }
  686. }
  687. if(op->getAsBool(PREF_DAEMON)) {
  688. if(daemon(1, 1) < 0) {
  689. perror(_("daemon failed"));
  690. exit(EXIT_FAILURE);
  691. }
  692. }
  693. Strings args(argv+optind, argv+argc);
  694. #ifdef HAVE_LIBSSL
  695. // for SSL initialization
  696. SSL_load_error_strings();
  697. SSL_library_init();
  698. #endif // HAVE_LIBSSL
  699. #ifdef HAVE_LIBGNUTLS
  700. gnutls_global_init();
  701. #endif // HAVE_LIBGNUTLS
  702. #ifdef ENABLE_METALINK
  703. xmlInitParser();
  704. #endif // ENABLE_METALINK
  705. srandom(time(NULL));
  706. if(op->getAsBool(PREF_STDOUT_LOG)) {
  707. LogFactory::setLogFile("/dev/stdout");
  708. } else if(op->get(PREF_LOG).size()) {
  709. LogFactory::setLogFile(op->get(PREF_LOG));
  710. } else {
  711. LogFactory::setLogFile("/dev/null");
  712. }
  713. // make sure logger is configured properly.
  714. try {
  715. Logger* logger = LogFactory::getInstance();
  716. logger->info("%s %s", PACKAGE, PACKAGE_VERSION);
  717. logger->info("Logging started.");
  718. setSignalHander(SIGPIPE, SIG_IGN, 0);
  719. requestInfo = 0;
  720. #ifdef ENABLE_BITTORRENT
  721. if(op->defined(PREF_TORRENT_FILE)) {
  722. requestInfo = new TorrentRequestInfo(op->get(PREF_TORRENT_FILE),
  723. op);
  724. Strings targetFiles;
  725. if(op->defined(PREF_TORRENT_FILE) && !args.empty()) {
  726. targetFiles = args;
  727. }
  728. ((TorrentRequestInfo*)requestInfo)->setTargetFiles(targetFiles);
  729. }
  730. else
  731. #endif // ENABLE_BITTORRENT
  732. #ifdef ENABLE_METALINK
  733. if(op->defined(PREF_METALINK_FILE)) {
  734. requestInfo = new MetalinkRequestInfo(op->get(PREF_METALINK_FILE),
  735. op);
  736. } else
  737. #endif // ENABLE_METALINK
  738. {
  739. requestInfo = new UrlRequestInfo(args, 0, op);
  740. }
  741. while(requestInfo) {
  742. RequestInfo* next = requestInfo->execute();
  743. if(requestInfo->isFail()) {
  744. delete requestInfo;
  745. exit(EXIT_FAILURE);
  746. }
  747. if(requestInfo->getFileInfo().checkReady()) {
  748. cout << _("Now verifying checksum.\n"
  749. "This may take some time depending on your PC environment"
  750. " and the size of file.") << endl;
  751. if(requestInfo->getFileInfo().check()) {
  752. cout << _("checksum OK.") << endl;
  753. } else {
  754. // TODO
  755. cout << _("checksum ERROR.") << endl;
  756. exit(EXIT_FAILURE);
  757. }
  758. }
  759. delete requestInfo;
  760. requestInfo = next;
  761. }
  762. } catch(Exception* ex) {
  763. cerr << ex->getMsg() << endl;
  764. delete ex;
  765. exit(EXIT_FAILURE);
  766. }
  767. delete op;
  768. LogFactory::release();
  769. #ifdef HAVE_LIBGNUTLS
  770. gnutls_global_deinit();
  771. #endif // HAVE_LIBGNUTLS
  772. #ifdef ENABLE_METALINK
  773. xmlCleanupParser();
  774. #endif // ENABLE_METALINK
  775. FeatureConfig::release();
  776. return 0;
  777. }