main.cc 26 KB

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