main.cc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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 "common.h"
  36. #include "LogFactory.h"
  37. #include "Util.h"
  38. #include "FeatureConfig.h"
  39. #include "MultiUrlRequestInfo.h"
  40. #include "TorrentRequestInfo.h"
  41. #include "BitfieldManFactory.h"
  42. #include "SimpleRandomizer.h"
  43. #include "ConsoleFileAllocationMonitor.h"
  44. #include "Netrc.h"
  45. #include "RequestFactory.h"
  46. #include "OptionParser.h"
  47. #include "OptionHandlerFactory.h"
  48. #include "FatalException.h"
  49. #include "File.h"
  50. #include "CUIDCounter.h"
  51. #include "FileUriListParser.h"
  52. #include "StreamUriListParser.h"
  53. #include "CookieBoxFactory.h"
  54. #include "a2algo.h"
  55. #include "message.h"
  56. #include "a2io.h"
  57. #include "a2time.h"
  58. #include "Platform.h"
  59. #include "prefs.h"
  60. #include <deque>
  61. #include <algorithm>
  62. #include <signal.h>
  63. #include <unistd.h>
  64. #include <libgen.h>
  65. #include <utility>
  66. #include <fstream>
  67. #include <sstream>
  68. extern char* optarg;
  69. extern int optind, opterr, optopt;
  70. #include <getopt.h>
  71. #ifdef ENABLE_METALINK
  72. #include "MetalinkRequestInfo.h"
  73. #include "Xml2MetalinkProcessor.h"
  74. #endif
  75. #ifdef HAVE_LIBSSL
  76. // for SSL
  77. # include <openssl/err.h>
  78. # include <openssl/ssl.h>
  79. #endif // HAVE_LIBSSL
  80. #ifdef HAVE_LIBGNUTLS
  81. # include <gnutls/gnutls.h>
  82. #endif // HAVE_LIBGNUTLS
  83. using namespace std;
  84. void showVersion() {
  85. cout << PACKAGE << _(" version ") << PACKAGE_VERSION << endl;
  86. cout << "**Configuration**" << endl;
  87. cout << FeatureConfig::getInstance()->getConfigurationSummary();
  88. #ifdef ENABLE_MESSAGE_DIGEST
  89. cout << "message digest algorithms: " << MessageDigestContext::getSupportedAlgoString() << endl;
  90. #endif // ENABLE_MESSAGE_DIGEST
  91. cout << endl;
  92. cout << "Copyright (C) 2006, 2007 Tatsuhiro Tsujikawa" << endl;
  93. cout << endl;
  94. cout <<
  95. _("This program is free software; you can redistribute it and/or modify\n"
  96. "it under the terms of the GNU General Public License as published by\n"
  97. "the Free Software Foundation; either version 2 of the License, or\n"
  98. "(at your option) any later version.\n"
  99. "\n"
  100. "This program is distributed in the hope that it will be useful,\n"
  101. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  102. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  103. "GNU General Public License for more details.\n"
  104. "\n"
  105. "You should have received a copy of the GNU General Public License\n"
  106. "along with this program; if not, write to the Free Software\n"
  107. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n");
  108. cout << endl;
  109. cout << _("Contact Info:") << endl;
  110. cout << "Tatsuhiro Tsujikawa <tujikawa at users dot sourceforge dot net>" << endl;
  111. cout << endl;
  112. }
  113. void showUsage() {
  114. printf(_("Usage: %s [options] URL ...\n"), PACKAGE_NAME);
  115. #ifdef ENABLE_BITTORRENT
  116. printf(_(" %s [options] -T TORRENT_FILE FILE ...\n"), PACKAGE_NAME);
  117. #endif // ENABLE_BITTORRENT
  118. #ifdef ENABLE_METALINK
  119. printf(_(" %s [options] -M METALINK_FILE\n"), PACKAGE_NAME);
  120. #endif // ENABLE_METALINK
  121. cout << endl;
  122. cout << _("Options:") << endl;
  123. cout << _(" -d, --dir=DIR The directory to store the downloaded file.") << endl;
  124. cout << _(" -o, --out=FILE The file name of the downloaded file.") << endl;
  125. cout << _(" -l, --log=LOG The file name of the log file. If '-' is\n"
  126. " specified, log is written to stdout.") << endl;
  127. #ifdef HAVE_DAEMON
  128. cout << _(" -D, --daemon Run as daemon.") << endl;
  129. #endif // HAVE_DAEMON
  130. cout << _(" -s, --split=N Download a file using N connections. N must be\n"
  131. " between 1 and 5. This option affects all URLs.\n"
  132. " Thus, aria2 connects to each URL with\n"
  133. " N connections.\n"
  134. " Default: 1") << endl;
  135. cout << _(" --retry-wait=SEC Set the seconds to wait to retry after an error\n"
  136. " has occured. Specify a value between 0 and 60.\n"
  137. " Default: 5") << endl;
  138. cout << _(" -t, --timeout=SEC Set timeout in seconds. Default: 60") << endl;
  139. cout << _(" -m, --max-tries=N Set number of tries. 0 means unlimited.\n"
  140. " Default: 5") << endl;
  141. /*
  142. cout << _(" --min-segment-size=SIZE[K|M] Set minimum segment size. You can append\n"
  143. " K or M(1K = 1024, 1M = 1024K). This\n"
  144. " value must be greater than or equal to\n"
  145. " 1024. Default: 1M") << endl;
  146. */
  147. cout << _(" --http-proxy=HOST:PORT Use HTTP proxy server. This affects all URLs.") << endl;
  148. cout << _(" --http-user=USER Set HTTP user. This affects all URLs.") << endl;
  149. cout << _(" --http-passwd=PASSWD Set HTTP password. This affects all URLs.") << endl;
  150. cout << _(" --http-proxy-user=USER Set HTTP proxy user. This affects all URLs.") << endl;
  151. cout << _(" --http-proxy-passwd=PASSWD Set HTTP proxy password. This affects all URLs.") << endl;
  152. cout << _(" --http-proxy-method=METHOD Set the method to use in proxy request.\n"
  153. " METHOD is either 'get' or 'tunnel'.\n"
  154. " Default: tunnel") << endl;
  155. cout << _(" --http-auth-scheme=SCHEME Set HTTP authentication scheme. Currently, basic\n"
  156. " is the only supported scheme.\n"
  157. " Default: basic") << endl;
  158. cout << _(" --referer=REFERER Set Referer. This affects all URLs.") << endl;
  159. cout << _(" --ftp-user=USER Set FTP user. This affects all URLs.\n"
  160. " Default: anonymous") << endl;
  161. cout << _(" --ftp-passwd=PASSWD Set FTP password. This affects all URLs.\n"
  162. " Default: ARIA2USER@") << endl;
  163. cout << _(" --ftp-type=TYPE Set FTP transfer type. TYPE is either 'binary'\n"
  164. " or 'ascii'.\n"
  165. " Default: binary") << endl;
  166. cout << _(" -p, --ftp-pasv Use passive mode in FTP.") << endl;
  167. cout << _(" --ftp-via-http-proxy=METHOD Use HTTP proxy in FTP. METHOD is either 'get' or\n"
  168. " 'tunnel'.\n"
  169. " Default: tunnel") << endl;
  170. cout << _(" --lowest-speed-limit=SPEED Close connection if download speed is lower than\n"
  171. " or equal to this value(bytes per sec).\n"
  172. " 0 means aria2 does not have a lowest speed limit.\n"
  173. " You can append K or M(1K = 1024, 1M = 1024K).\n"
  174. " This option does not affect BitTorrent downloads.\n"
  175. " Default: 0") << endl;
  176. cout << _(" --max-download-limit=SPEED Set max download speed in bytes per sec.\n"
  177. " 0 means unrestricted.\n"
  178. " You can append K or M(1K = 1024, 1M = 1024K).\n"
  179. " Default: 0") << endl;
  180. cout << _(" --file-allocation=METHOD Specify file allocation method. METHOD is either\n"
  181. " 'none' or 'prealloc'. 'none' doesn't pre-allocate\n"
  182. " file space. 'prealloc' pre-allocates file space\n"
  183. " before download begins. This may take some time\n"
  184. " depending on the size of the file.\n"
  185. " Default: none") << endl;
  186. cout << _(" --allow-overwrite=true|false If false, aria2 doesn't download a file which\n"
  187. " already exists but the corresponding .aria2 file\n"
  188. " doesn't exist.\n"
  189. " Default: false") << endl;
  190. #ifdef ENABLE_MESSAGE_DIGEST
  191. cout << _(" --check-integrity=true|false Check file integrity by validating piece hash.\n"
  192. " This option only affects in BitTorrent downloads\n"
  193. " and Metalink downloads with chunk checksums.\n"
  194. " Use this option to re-download a damaged portion\n"
  195. " of a file.\n"
  196. " You may need to specify --allow-overwrite=true\n"
  197. " if the .aria2 file doesn't exist.\n"
  198. " Default: false") << endl;
  199. cout << _(" --realtime-chunk-checksum=true|false Validate chunk checksum while\n"
  200. " downloading a file in Metalink mode. This option\n"
  201. " on affects Metalink mode with chunk checksums.\n"
  202. " Default: true") << endl;
  203. #endif // ENABLE_MESSAGE_DIGEST
  204. cout << _(" -c, --continue Continue downloading a partially downloaded\n"
  205. " file. Use this option to resume a download\n"
  206. " started by a web browser or another program\n"
  207. " which downloads files sequentially from the\n"
  208. " beginning. Currently this option is only\n"
  209. " applicable to http(s)/ftp downloads.") << endl;
  210. cout << _(" -U, --user-agent=USER_AGENT Set user agent for http(s) downloads.") << endl;
  211. cout << _(" -n, --no-netrc Disables netrc support.") << endl;
  212. cout << _(" -i, --input-file=FILE Downloads URIs found in FILE. You can specify\n"
  213. " multiple URIs for a single entity: separate\n"
  214. " URIs on a single line using the TAB character.\n"
  215. " Reads input from stdin when '-' is specified.") << endl;
  216. cout << _(" -j, --max-concurrent-downloads=N Set maximum number of concurrent downloads.\n"
  217. " It should be used with the -i option.\n"
  218. " Default: 5") << endl;
  219. cout << _(" --load-cookies=FILE Load cookies from FILE. The format of FILE is\n"
  220. " the same used by Netscape and Mozilla.") << endl;
  221. #if defined ENABLE_BITTORRENT || ENABLE_METALINK
  222. cout << _(" -S, --show-files Print file listing of .torrent or .metalink file\n"
  223. " and exit.") << endl;
  224. cout << _(" --select-file=INDEX... Set file to download by specifing its index.\n"
  225. " You can find the file index using the\n"
  226. " --show-files option. Multiple indexes can be\n"
  227. " specified by using ',', for example: \"3,6\".\n"
  228. " You can also use '-' to specify a range: \"1-5\".\n"
  229. " ',' and '-' can be used together.\n"
  230. " When used with the -M option, index may vary\n"
  231. " depending on the query(see --metalink-* options).") << endl;
  232. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  233. #ifdef ENABLE_BITTORRENT
  234. cout << _(" -T, --torrent-file=TORRENT_FILE The path to the .torrent file.") << endl;
  235. cout << _(" --follow-torrent=true|false Set to false to prevent aria2 from\n"
  236. " entering BitTorrent mode even if the filename of\n"
  237. " the downloaded file ends with .torrent.\n"
  238. " Default: true") << endl;
  239. cout << _(" --direct-file-mapping=true|false Directly read from and write to each file\n"
  240. " mentioned in .torrent file.\n"
  241. " Default: true") << endl;
  242. cout << _(" --listen-port=PORT Set TCP port number for BitTorrent downloads.\n"
  243. " Default: 6881-6999") << endl;
  244. cout << _(" --max-upload-limit=SPEED Set max upload speed in bytes per sec.\n"
  245. " 0 means unrestricted.\n"
  246. " You can append K or M(1K = 1024, 1M = 1024K).\n"
  247. " Default: 0") << endl;
  248. cout << _(" --seed-time=MINUTES Specify seeding time in minutes. Also see the\n"
  249. " --seed-ratio option.") << endl;
  250. cout << _(" --seed-ratio=RATIO Specify share ratio. Seed completed torrents\n"
  251. " until share ratio reaches RATIO. 1.0 is\n"
  252. " encouraged. If --seed-time option is specified\n"
  253. " along with this option, seeding ends when at\n"
  254. " least one of the conditions is satisfied.") << endl;
  255. cout << _(" --peer-id-prefix=PEERI_ID_PREFIX Specify the prefix of peer ID. The peer ID in\n"
  256. " in BitTorrent is 20 byte length. If more than 20\n"
  257. " bytes are specified, only first 20\n"
  258. " bytes are used. If less than 20 bytes are\n"
  259. " specified, the random alphabet characters are\n"
  260. " added to make it's length 20 bytes.\n"
  261. " Default: -aria2-") << endl;
  262. #endif // ENABLE_BITTORRENT
  263. #ifdef ENABLE_METALINK
  264. cout << _(" -M, --metalink-file=METALINK_FILE The file path to the .metalink file.") << endl;
  265. cout << _(" -C, --metalink-servers=NUM_SERVERS The number of servers to connect to\n"
  266. " simultaneously.\n"
  267. " Default: 5") << endl;
  268. cout << _(" --metalink-version=VERSION The version of the file to download.") << endl;
  269. cout << _(" --metalink-language=LANGUAGE The language of the file to download.") << endl;
  270. cout << _(" --metalink-os=OS The operating system of the file to download.") << endl;
  271. cout << _(" --metalink-location=LOCATION The location of the prefered server.") << endl;
  272. cout << _(" --follow-metalink=true|false Set to false to prevent aria2 from\n"
  273. " entering Metalink mode even if the filename of\n"
  274. " the downloaded file ends with .metalink.\n"
  275. " Default: true") << endl;
  276. #endif // ENABLE_METALINK
  277. cout << _(" -v, --version Print the version number and exit.") << endl;
  278. cout << _(" -h, --help Print this message and exit.") << endl;
  279. cout << endl;
  280. cout << "URL:" << endl;
  281. cout << _(" You can specify multiple URLs. All URLs must point to the same file\n"
  282. " or downloading will fail.") << endl;
  283. cout << endl;
  284. #ifdef ENABLE_BITTORRENT
  285. cout << "FILE:" << endl;
  286. cout << _(" Specify files in multi-file torrent to download. Use in conjunction with the\n"
  287. " -T option. This argument is ignored if you specify the --select-file option.") << endl;
  288. cout << endl;
  289. #endif // ENABLE_BITTORRENT
  290. cout << _("Examples:") << endl;
  291. cout << _(" Download a file using 1 connection:") << endl;
  292. cout << " aria2c http://AAA.BBB.CCC/file.zip" << endl;
  293. cout << _(" Download a file using 2 connections:") << endl;
  294. cout << " aria2c -s 2 http://AAA.BBB.CCC/file.zip" << endl;
  295. cout << _(" Download a file using 2 connections, each connects to a different server:") << endl;
  296. cout << " aria2c http://AAA.BBB.CCC/file.zip http://DDD.EEE.FFF/GGG/file.zip" << endl;
  297. cout << _(" You can mix up different protocols:") << endl;
  298. cout << " aria2c http://AAA.BBB.CCC/file.zip ftp://DDD.EEE.FFF/GGG/file.zip" << endl;
  299. #ifdef ENABLE_BITTORRENT
  300. cout << endl;
  301. cout << _(" Download a torrent:") << endl;
  302. cout << " aria2c -o test.torrent http://AAA.BBB.CCC/file.torrent" << endl;
  303. cout << _(" Download a torrent using a local .torrent file:") << endl;
  304. cout << " aria2c -T test.torrent" << endl;
  305. cout << _(" Download only selected files:") << endl;
  306. cout << " aria2c -T test.torrent dir/file1.zip dir/file2.zip" << endl;
  307. cout << _(" Print file listing of .torrent file:") << endl;
  308. cout << " aria2c -T test.torrent -S" << endl;
  309. #endif // ENABLE_BITTORRENT
  310. #ifdef ENABLE_METALINK
  311. cout << endl;
  312. cout << _(" Metalink downloading:") << endl;
  313. cout << " aria2c http://AAA.BBB.CCC/file.metalink" << endl;
  314. cout << _(" Download a file using local .metalink file:") << endl;
  315. cout << " aria2c -M test.metalink" << endl;
  316. cout << _(" Metalink downloading with preferences:") << endl;
  317. cout << " aria2c -M test.metalink --metalink-version=1.1.1 --metalink-language=en-US" << endl;
  318. cout << _(" Download only selected files:") << endl;
  319. cout << " aria2c -M test.metalink --metalink-language=en-US dir/file1.zip dir/file2.zip" << endl;
  320. cout << _(" Download only selected files using index:") << endl;
  321. cout << " aria2c -M test.metalink --metalink-language=en-US --select-file 1,3-5" << endl;
  322. cout << _(" Print file listing of .metalink file:") << endl;
  323. cout << " aria2c -M test.metalink -S --metalink-language=en-US" << endl;
  324. #endif // ENABLE_METALINK
  325. cout << endl;
  326. printf(_("Report bugs to %s"), "<tujikawa at users dot sourceforge dot net>");
  327. cout << endl;
  328. }
  329. int main(int argc, char* argv[]) {
  330. #ifdef HAVE_WINSOCK2_H
  331. Platform platform;
  332. #endif // HAVE_WINSOCK2_H
  333. #ifdef ENABLE_NLS
  334. setlocale (LC_CTYPE, "");
  335. setlocale (LC_MESSAGES, "");
  336. bindtextdomain (PACKAGE, LOCALEDIR);
  337. textdomain (PACKAGE);
  338. #endif // ENABLE_NLS
  339. stringstream cmdstream;
  340. int32_t c;
  341. Option* op = new Option();
  342. op->put(PREF_STDOUT_LOG, V_FALSE);
  343. op->put(PREF_DIR, ".");
  344. op->put(PREF_SPLIT, "1");
  345. op->put(PREF_DAEMON, V_FALSE);
  346. op->put(PREF_SEGMENT_SIZE, Util::itos((int32_t)(1024*1024)));
  347. op->put(PREF_HTTP_KEEP_ALIVE, V_FALSE);
  348. op->put(PREF_LISTEN_PORT, "-1");
  349. op->put(PREF_METALINK_SERVERS, "5");
  350. op->put(PREF_FOLLOW_TORRENT,
  351. #ifdef ENABLE_BITTORRENT
  352. V_TRUE
  353. #else
  354. V_FALSE
  355. #endif // ENABLE_BITTORRENT
  356. );
  357. op->put(PREF_FOLLOW_METALINK,
  358. #ifdef ENABLE_METALINK
  359. V_TRUE
  360. #else
  361. V_FALSE
  362. #endif // ENABLE_METALINK
  363. );
  364. op->put(PREF_RETRY_WAIT, "5");
  365. op->put(PREF_TIMEOUT, "60");
  366. op->put(PREF_DNS_TIMEOUT, "10");
  367. op->put(PREF_PEER_CONNECTION_TIMEOUT, "60");
  368. op->put(PREF_BT_TIMEOUT, "180");
  369. op->put(PREF_BT_REQUEST_TIMEOUT, "60");
  370. op->put(PREF_BT_KEEP_ALIVE_INTERVAL, "120");
  371. op->put(PREF_MIN_SEGMENT_SIZE, "1048576");// 1M
  372. op->put(PREF_MAX_TRIES, "5");
  373. op->put(PREF_HTTP_AUTH_SCHEME, V_BASIC);
  374. op->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  375. op->put(PREF_FTP_TYPE, V_BINARY);
  376. op->put(PREF_FTP_VIA_HTTP_PROXY, V_TUNNEL);
  377. op->put(PREF_AUTO_SAVE_INTERVAL, "60");
  378. op->put(PREF_DIRECT_FILE_MAPPING, V_TRUE);
  379. op->put(PREF_LOWEST_SPEED_LIMIT, "0");
  380. op->put(PREF_MAX_DOWNLOAD_LIMIT, "0");
  381. op->put(PREF_MAX_UPLOAD_LIMIT, "0");
  382. op->put(PREF_STARTUP_IDLE_TIME, "10");
  383. op->put(PREF_TRACKER_MAX_TRIES, "10");
  384. op->put(PREF_FILE_ALLOCATION, V_NONE);
  385. op->put(PREF_ALLOW_OVERWRITE, V_FALSE);
  386. op->put(PREF_REALTIME_CHUNK_CHECKSUM, V_TRUE);
  387. op->put(PREF_CHECK_INTEGRITY, V_FALSE);
  388. op->put(PREF_NETRC_PATH, Util::getHomeDir()+"/.netrc");
  389. op->put(PREF_CONTINUE, V_FALSE);
  390. op->put(PREF_USER_AGENT, "aria2");
  391. op->put(PREF_NO_NETRC, V_FALSE);
  392. op->put(PREF_MAX_CONCURRENT_DOWNLOADS, "5");
  393. op->put(PREF_DIRECT_DOWNLOAD_TIMEOUT, "15");
  394. while(1) {
  395. int optIndex = 0;
  396. int lopt;
  397. static struct option longOpts[] = {
  398. #ifdef HAVE_DAEMON
  399. { "daemon", no_argument, NULL, 'D' },
  400. #endif // HAVE_DAEMON
  401. { "dir", required_argument, NULL, 'd' },
  402. { "out", required_argument, NULL, 'o' },
  403. { "log", required_argument, NULL, 'l' },
  404. { "split", required_argument, NULL, 's' },
  405. { "timeout", required_argument, NULL, 't' },
  406. { "max-tries", required_argument, NULL, 'm' },
  407. { "http-proxy", required_argument, &lopt, 1 },
  408. { "http-user", required_argument, &lopt, 2 },
  409. { "http-passwd", required_argument, &lopt, 3 },
  410. { "http-proxy-user", required_argument, &lopt, 4 },
  411. { "http-proxy-passwd", required_argument, &lopt, 5 },
  412. { "http-auth-scheme", required_argument, &lopt, 6 },
  413. { "referer", required_argument, &lopt, 7 },
  414. { "retry-wait", required_argument, &lopt, 8 },
  415. { "ftp-user", required_argument, &lopt, 9 },
  416. { "ftp-passwd", required_argument, &lopt, 10 },
  417. { "ftp-type", required_argument, &lopt, 11 },
  418. { "ftp-pasv", no_argument, NULL, 'p' },
  419. { "ftp-via-http-proxy", required_argument, &lopt, 12 },
  420. //{ "min-segment-size", required_argument, &lopt, 13 },
  421. { "http-proxy-method", required_argument, &lopt, 14 },
  422. { "lowest-speed-limit", required_argument, &lopt, 200 },
  423. { "max-download-limit", required_argument, &lopt, 201 },
  424. { "file-allocation", required_argument, 0, 'a' },
  425. { "allow-overwrite", required_argument, &lopt, 202 },
  426. #ifdef ENABLE_MESSAGE_DIGEST
  427. { "check-integrity", required_argument, &lopt, 203 },
  428. { "realtime-chunk-checksum", required_argument, &lopt, 204 },
  429. #endif // ENABLE_MESSAGE_DIGEST
  430. { "continue", no_argument, 0, 'c' },
  431. { "user-agent", required_argument, 0, 'U' },
  432. { "no-netrc", no_argument, 0, 'n' },
  433. { "input-file", required_argument, 0, 'i' },
  434. { "max-concurrent-downloads", required_argument, 0, 'j' },
  435. { "load-cookies", required_argument, &lopt, 205 },
  436. #if defined ENABLE_BITTORRENT || ENABLE_METALINK
  437. { "show-files", no_argument, NULL, 'S' },
  438. { "select-file", required_argument, &lopt, 21 },
  439. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  440. #ifdef ENABLE_BITTORRENT
  441. { "torrent-file", required_argument, NULL, 'T' },
  442. { "listen-port", required_argument, &lopt, 15 },
  443. { "follow-torrent", required_argument, &lopt, 16 },
  444. { "no-preallocation", no_argument, &lopt, 18 },
  445. { "direct-file-mapping", required_argument, &lopt, 19 },
  446. // TODO remove upload-limit.
  447. //{ "upload-limit", required_argument, &lopt, 20 },
  448. { "seed-time", required_argument, &lopt, 22 },
  449. { "seed-ratio", required_argument, &lopt, 23 },
  450. { "max-upload-limit", required_argument, &lopt, 24 },
  451. { "peer-id-prefix", required_argument, &lopt, 25 },
  452. #endif // ENABLE_BITTORRENT
  453. #ifdef ENABLE_METALINK
  454. { "metalink-file", required_argument, NULL, 'M' },
  455. { "metalink-servers", required_argument, NULL, 'C' },
  456. { "metalink-version", required_argument, &lopt, 100 },
  457. { "metalink-language", required_argument, &lopt, 101 },
  458. { "metalink-os", required_argument, &lopt, 102 },
  459. { "follow-metalink", required_argument, &lopt, 103 },
  460. { "metalink-location", required_argument, &lopt, 104 },
  461. #endif // ENABLE_METALINK
  462. { "version", no_argument, NULL, 'v' },
  463. { "help", no_argument, NULL, 'h' },
  464. { 0, 0, 0, 0 }
  465. };
  466. c = getopt_long(argc, argv, "Dd:o:l:s:pt:m:vhST:M:C:a:cU:ni:j:", longOpts, &optIndex);
  467. if(c == -1) {
  468. break;
  469. }
  470. switch(c) {
  471. case 0:{
  472. switch(lopt) {
  473. case 1:
  474. cmdstream << PREF_HTTP_PROXY << "=" << optarg << "\n";
  475. break;
  476. case 2:
  477. cmdstream << PREF_HTTP_USER << "=" << optarg << "\n";
  478. break;
  479. case 3:
  480. cmdstream << PREF_HTTP_PASSWD << "=" << optarg << "\n";
  481. break;
  482. case 4:
  483. cmdstream << PREF_HTTP_PROXY_USER << "=" << optarg << "\n";
  484. break;
  485. case 5:
  486. cmdstream << PREF_HTTP_PROXY_PASSWD << "=" << optarg << "\n";
  487. break;
  488. case 6:
  489. cmdstream << PREF_HTTP_AUTH_SCHEME << "=" << optarg << "\n";
  490. break;
  491. case 7:
  492. cmdstream << PREF_REFERER << "=" << optarg << "\n";
  493. break;
  494. case 8:
  495. cmdstream << PREF_RETRY_WAIT << "=" << optarg << "\n";
  496. break;
  497. case 9:
  498. cmdstream << PREF_FTP_USER << "=" << optarg << "\n";
  499. break;
  500. case 10:
  501. cmdstream << PREF_FTP_PASSWD << "=" << optarg << "\n";
  502. break;
  503. case 11:
  504. cmdstream << PREF_FTP_TYPE << "=" << optarg << "\n";
  505. break;
  506. case 12:
  507. cmdstream << PREF_FTP_VIA_HTTP_PROXY << "=" << optarg << "\n";
  508. break;
  509. case 13:
  510. cmdstream << PREF_MIN_SEGMENT_SIZE << "=" << optarg << "\n";
  511. break;
  512. case 14:
  513. cmdstream << PREF_HTTP_PROXY_METHOD << "=" << optarg << "\n";
  514. break;
  515. case 15:
  516. cmdstream << PREF_LISTEN_PORT << "=" << optarg << "\n";
  517. break;
  518. case 16:
  519. cmdstream << PREF_FOLLOW_TORRENT << "=" << optarg << "\n";
  520. break;
  521. case 18:
  522. cmdstream << PREF_NO_PREALLOCATION << "=" << V_TRUE << "\n";
  523. break;
  524. case 19:
  525. cmdstream << PREF_DIRECT_FILE_MAPPING << "=" << optarg << "\n";
  526. break;
  527. case 21:
  528. cmdstream << PREF_SELECT_FILE << "=" << optarg << "\n";
  529. break;
  530. case 22:
  531. cmdstream << PREF_SEED_TIME << "=" << optarg << "\n";
  532. break;
  533. case 23:
  534. cmdstream << PREF_SEED_RATIO << "=" << optarg << "\n";
  535. break;
  536. case 24:
  537. cmdstream << PREF_MAX_UPLOAD_LIMIT << "=" << optarg << "\n";
  538. break;
  539. case 25:
  540. cmdstream << PREF_PEER_ID_PREFIX << "=" << optarg << "\n";
  541. case 100:
  542. cmdstream << PREF_METALINK_VERSION << "=" << optarg << "\n";
  543. break;
  544. case 101:
  545. cmdstream << PREF_METALINK_LANGUAGE << "=" << optarg << "\n";
  546. break;
  547. case 102:
  548. cmdstream << PREF_METALINK_OS << "=" << optarg << "\n";
  549. break;
  550. case 103:
  551. cmdstream << PREF_FOLLOW_METALINK << "=" << optarg << "\n";
  552. break;
  553. case 104:
  554. cmdstream << PREF_METALINK_LOCATION << "=" << optarg << "\n";
  555. break;
  556. case 200:
  557. cmdstream << PREF_LOWEST_SPEED_LIMIT << "=" << optarg << "\n";
  558. break;
  559. case 201:
  560. cmdstream << PREF_MAX_DOWNLOAD_LIMIT << "=" << optarg << "\n";
  561. break;
  562. case 202:
  563. cmdstream << PREF_ALLOW_OVERWRITE << "=" << optarg << "\n";
  564. break;
  565. case 203:
  566. cmdstream << PREF_CHECK_INTEGRITY << "=" << optarg << "\n";
  567. break;
  568. case 204:
  569. cmdstream << PREF_REALTIME_CHUNK_CHECKSUM << "=" << optarg << "\n";
  570. break;
  571. case 205:
  572. cmdstream << PREF_LOAD_COOKIES << "=" << optarg << "\n";
  573. break;
  574. }
  575. break;
  576. }
  577. #ifdef HAVE_DAEMON
  578. case 'D':
  579. cmdstream << PREF_DAEMON << "=" << V_TRUE << "\n";
  580. break;
  581. #endif // HAVE_DAEMON
  582. case 'd':
  583. cmdstream << PREF_DIR << "=" << optarg << "\n";
  584. break;
  585. case 'o':
  586. cmdstream << PREF_OUT << "=" << optarg << "\n";
  587. break;
  588. case 'l':
  589. cmdstream << PREF_LOG << "=" << optarg << "\n";
  590. break;
  591. case 's':
  592. cmdstream << PREF_SPLIT << "=" << optarg << "\n";
  593. break;
  594. case 't':
  595. cmdstream << PREF_TIMEOUT << "=" << optarg << "\n";
  596. break;
  597. case 'm':
  598. cmdstream << PREF_MAX_TRIES << "=" << optarg << "\n";
  599. break;
  600. case 'p':
  601. cmdstream << PREF_FTP_PASV << "=" << V_TRUE << "\n";
  602. break;
  603. case 'S':
  604. cmdstream << PREF_SHOW_FILES << "=" << V_TRUE << "\n";
  605. break;
  606. case 'T':
  607. cmdstream << PREF_TORRENT_FILE << "=" << optarg << "\n";
  608. break;
  609. case 'M':
  610. cmdstream << PREF_METALINK_FILE << "=" << optarg << "\n";
  611. break;
  612. case 'C':
  613. cmdstream << PREF_METALINK_SERVERS << "=" << optarg << "\n";
  614. break;
  615. case 'a':
  616. cmdstream << PREF_FILE_ALLOCATION << "=" << optarg << "\n";
  617. break;
  618. case 'c':
  619. cmdstream << PREF_CONTINUE << "=" << V_TRUE << "\n";
  620. break;
  621. case 'U':
  622. cmdstream << PREF_USER_AGENT << "=" << optarg << "\n";
  623. break;
  624. case 'n':
  625. cmdstream << PREF_NO_NETRC << "=" << V_TRUE << "\n";
  626. break;
  627. case 'i':
  628. cmdstream << PREF_INPUT_FILE << "=" << optarg << "\n";
  629. break;
  630. case 'j':
  631. cmdstream << PREF_MAX_CONCURRENT_DOWNLOADS << "=" << optarg << "\n";
  632. break;
  633. case 'v':
  634. showVersion();
  635. exit(EXIT_SUCCESS);
  636. case 'h':
  637. showUsage();
  638. exit(EXIT_SUCCESS);
  639. default:
  640. exit(EXIT_FAILURE);
  641. }
  642. }
  643. {
  644. OptionParser oparser;
  645. oparser.setOptionHandlers(OptionHandlerFactory::createOptionHandlers());
  646. string cfname = Util::getHomeDir()+"/.aria2/aria2.conf";
  647. ifstream cfstream(cfname.c_str());
  648. try {
  649. oparser.parse(op, cfstream);
  650. } catch(Exception* e) {
  651. cerr << "Parse error in " << cfname << endl;
  652. cerr << e->getMsg() << endl;
  653. delete e;
  654. exit(EXIT_FAILURE);
  655. }
  656. try {
  657. oparser.parse(op, cmdstream);
  658. } catch(Exception* e) {
  659. cerr << e->getMsg() << endl;
  660. delete e;
  661. exit(EXIT_FAILURE);
  662. }
  663. }
  664. if(op->defined(PREF_HTTP_USER)) {
  665. op->put(PREF_HTTP_AUTH_ENABLED, V_TRUE);
  666. }
  667. if(op->defined(PREF_HTTP_PROXY_USER)) {
  668. op->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  669. }
  670. if(
  671. #ifdef ENABLE_BITTORRENT
  672. !op->defined(PREF_TORRENT_FILE) &&
  673. #endif // ENABLE_BITTORRENT
  674. #ifdef ENABLE_METALINK
  675. !op->defined(PREF_METALINK_FILE) &&
  676. #endif // ENABLE_METALINK
  677. !op->defined(PREF_INPUT_FILE)) {
  678. if(optind == argc) {
  679. cerr << MSG_URI_REQUIRED << endl;
  680. exit(EXIT_FAILURE);
  681. }
  682. }
  683. #ifdef HAVE_DAEMON
  684. if(op->getAsBool(PREF_DAEMON)) {
  685. if(daemon(1, 1) < 0) {
  686. perror(MSG_DAEMON_FAILED);
  687. exit(EXIT_FAILURE);
  688. }
  689. }
  690. #endif // HAVE_DAEMON
  691. Strings args(argv+optind, argv+argc);
  692. #ifdef HAVE_LIBSSL
  693. // for SSL initialization
  694. SSL_load_error_strings();
  695. SSL_library_init();
  696. #endif // HAVE_LIBSSL
  697. #ifdef HAVE_LIBGNUTLS
  698. gnutls_global_init();
  699. #endif // HAVE_LIBGNUTLS
  700. #ifdef ENABLE_METALINK
  701. xmlInitParser();
  702. #endif // ENABLE_METALINK
  703. SimpleRandomizer::init();
  704. BitfieldManFactory::setDefaultRandomizer(SimpleRandomizer::getInstance());
  705. FileAllocationMonitorFactory::setFactory(new ConsoleFileAllocationMonitorFactory());
  706. if(op->getAsBool(PREF_STDOUT_LOG)) {
  707. LogFactory::setLogFile(DEV_STDOUT);
  708. } else if(op->get(PREF_LOG).size()) {
  709. LogFactory::setLogFile(op->get(PREF_LOG));
  710. } else {
  711. LogFactory::setLogFile(DEV_NULL);
  712. }
  713. int32_t exitStatus = EXIT_SUCCESS;
  714. try {
  715. Logger* logger = LogFactory::getInstance();
  716. logger->info("%s %s", PACKAGE, PACKAGE_VERSION);
  717. logger->info(MSG_LOGGING_STARTED);
  718. RequestFactoryHandle requestFactory = new RequestFactory();
  719. requestFactory->setOption(op);
  720. File netrccf(op->get(PREF_NETRC_PATH));
  721. if(!op->getAsBool(PREF_NO_NETRC) && netrccf.isFile()) {
  722. mode_t mode = netrccf.mode();
  723. if(mode&(S_IRWXG|S_IRWXO)) {
  724. logger->notice(MSG_INCORRECT_NETRC_PERMISSION,
  725. op->get(PREF_NETRC_PATH).c_str());
  726. } else {
  727. NetrcHandle netrc = new Netrc();
  728. netrc->parse(op->get(PREF_NETRC_PATH));
  729. requestFactory->setNetrc(netrc);
  730. }
  731. }
  732. CookieBoxFactoryHandle cookieBoxFactory = new CookieBoxFactory();
  733. CookieBoxFactorySingletonHolder::instance(cookieBoxFactory);
  734. if(op->defined(PREF_LOAD_COOKIES)) {
  735. File cookieFile(op->get(PREF_LOAD_COOKIES));
  736. if(cookieFile.isFile()) {
  737. ifstream in(op->get(PREF_LOAD_COOKIES).c_str());
  738. CookieBoxFactorySingletonHolder::instance()->loadDefaultCookie(in);
  739. } else {
  740. logger->error(MSG_LOADING_COOKIE_FAILED, op->get(PREF_LOAD_COOKIES).c_str());
  741. exit(EXIT_FAILURE);
  742. }
  743. }
  744. RequestFactorySingletonHolder::instance(requestFactory);
  745. CUIDCounterHandle cuidCounter = new CUIDCounter();
  746. CUIDCounterSingletonHolder::instance(cuidCounter);
  747. #ifdef SIGPIPE
  748. Util::setGlobalSignalHandler(SIGPIPE, SIG_IGN, 0);
  749. #endif
  750. RequestInfo* firstReqInfo;
  751. #ifdef ENABLE_BITTORRENT
  752. if(op->defined(PREF_TORRENT_FILE)) {
  753. firstReqInfo = new TorrentRequestInfo(op->get(PREF_TORRENT_FILE),
  754. op);
  755. Strings targetFiles;
  756. if(op->defined(PREF_TORRENT_FILE) && !args.empty()) {
  757. targetFiles = args;
  758. }
  759. ((TorrentRequestInfo*)firstReqInfo)->setTargetFiles(targetFiles);
  760. }
  761. else
  762. #endif // ENABLE_BITTORRENT
  763. #ifdef ENABLE_METALINK
  764. if(op->defined(PREF_METALINK_FILE)) {
  765. firstReqInfo = new MetalinkRequestInfo(op->get(PREF_METALINK_FILE),
  766. op);
  767. Strings targetFiles;
  768. if(op->defined(PREF_METALINK_FILE) && !args.empty()) {
  769. targetFiles = args;
  770. }
  771. ((MetalinkRequestInfo*)firstReqInfo)->setTargetFiles(targetFiles);
  772. }
  773. else
  774. #endif // ENABLE_METALINK
  775. if(op->defined(PREF_INPUT_FILE)) {
  776. SharedHandle<UriListParser> flparser(0);
  777. if(op->get(PREF_INPUT_FILE) == "-") {
  778. flparser = new StreamUriListParser(cin);
  779. } else {
  780. if(!File(op->get(PREF_INPUT_FILE)).isFile()) {
  781. throw new FatalException(EX_FILE_OPEN, op->get(PREF_INPUT_FILE).c_str(), "No such file");
  782. }
  783. flparser = new FileUriListParser(op->get(PREF_INPUT_FILE));
  784. }
  785. RequestGroups groups;
  786. while(flparser->hasNext()) {
  787. Strings uris = flparser->next();
  788. if(!uris.empty()) {
  789. Strings xuris;
  790. ncopy(uris.begin(), uris.end(), op->getAsInt(PREF_SPLIT),
  791. back_inserter(xuris));
  792. RequestGroupHandle rg = new RequestGroup(xuris, op);
  793. groups.push_back(rg);
  794. }
  795. }
  796. firstReqInfo = new MultiUrlRequestInfo(groups, op);
  797. }
  798. else
  799. {
  800. Strings xargs;
  801. ncopy(args.begin(), args.end(), op->getAsInt(PREF_SPLIT),
  802. back_inserter(xargs));
  803. firstReqInfo = new MultiUrlRequestInfo(xargs, op);
  804. }
  805. RequestInfos reqInfos;
  806. if(firstReqInfo) {
  807. reqInfos.push_front(firstReqInfo);
  808. }
  809. while(reqInfos.size()) {
  810. RequestInfoHandle reqInfo = reqInfos.front();
  811. reqInfos.pop_front();
  812. RequestInfos nextReqInfos = reqInfo->execute();
  813. copy(nextReqInfos.begin(), nextReqInfos.end(), front_inserter(reqInfos));
  814. /*
  815. if(reqInfo->isFail()) {
  816. exitStatus = EXIT_FAILURE;
  817. } else if(reqInfo->getFileInfo().checkReady()) {
  818. cout << _("Now verifying checksum.\n"
  819. "This may take some time depending on your PC environment"
  820. " and the size of file.") << endl;
  821. if(reqInfo->getFileInfo().check()) {
  822. cout << _("checksum OK.") << endl;
  823. } else {
  824. // TODO
  825. cout << _("checksum ERROR.") << endl;
  826. exitStatus = EXIT_FAILURE;
  827. }
  828. }
  829. */
  830. }
  831. } catch(Exception* ex) {
  832. cerr << EX_EXCEPTION_CAUGHT << "\n" << ex->getMsg() << endl;
  833. delete ex;
  834. exit(EXIT_FAILURE);
  835. }
  836. delete op;
  837. LogFactory::release();
  838. #ifdef HAVE_LIBGNUTLS
  839. gnutls_global_deinit();
  840. #endif // HAVE_LIBGNUTLS
  841. #ifdef ENABLE_METALINK
  842. xmlCleanupParser();
  843. #endif // ENABLE_METALINK
  844. FeatureConfig::release();
  845. return exitStatus;
  846. }