main.cc 29 KB

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