main.cc 28 KB

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