main.cc 26 KB

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