main.cc 25 KB

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