Context.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 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 "Context.h"
  36. #include <unistd.h>
  37. #include <getopt.h>
  38. #ifdef HAVE_SYS_RESOURCE_H
  39. #include <sys/resource.h>
  40. #endif // HAVE_SYS_RESOURCE_H
  41. #include <numeric>
  42. #include <vector>
  43. #include <iostream>
  44. #include "LogFactory.h"
  45. #include "Logger.h"
  46. #include "util.h"
  47. #include "FeatureConfig.h"
  48. #include "MultiUrlRequestInfo.h"
  49. #include "SimpleRandomizer.h"
  50. #include "File.h"
  51. #include "message.h"
  52. #include "prefs.h"
  53. #include "Option.h"
  54. #include "a2algo.h"
  55. #include "a2io.h"
  56. #include "a2time.h"
  57. #include "Platform.h"
  58. #include "FileEntry.h"
  59. #include "RequestGroup.h"
  60. #include "download_helper.h"
  61. #include "Exception.h"
  62. #include "ProtocolDetector.h"
  63. #include "RecoverableException.h"
  64. #include "SocketCore.h"
  65. #include "DownloadContext.h"
  66. #include "fmt.h"
  67. #include "console.h"
  68. #include "UriListParser.h"
  69. #include "message_digest_helper.h"
  70. #ifdef ENABLE_BITTORRENT
  71. #include "bittorrent_helper.h"
  72. #endif // ENABLE_BITTORRENT
  73. #ifdef ENABLE_METALINK
  74. #include "metalink_helper.h"
  75. #include "MetalinkEntry.h"
  76. #endif // ENABLE_METALINK
  77. extern char* optarg;
  78. extern int optind, opterr, optopt;
  79. namespace aria2 {
  80. #ifdef ENABLE_BITTORRENT
  81. namespace {
  82. void showTorrentFile(const std::string& uri)
  83. {
  84. auto op = std::make_shared<Option>();
  85. auto dctx = std::make_shared<DownloadContext>();
  86. bittorrent::load(uri, dctx, op);
  87. bittorrent::print(*global::cout(), dctx);
  88. }
  89. } // namespace
  90. #endif // ENABLE_BITTORRENT
  91. #ifdef ENABLE_METALINK
  92. namespace {
  93. void showMetalinkFile(const std::string& uri, const std::shared_ptr<Option>& op)
  94. {
  95. auto fileEntries = MetalinkEntry::toFileEntry(
  96. metalink::parseAndQuery(uri, op.get(), op->get(PREF_METALINK_BASE_URI)));
  97. util::toStream(std::begin(fileEntries), std::end(fileEntries),
  98. *global::cout());
  99. global::cout()->write("\n");
  100. global::cout()->flush();
  101. }
  102. } // namespace
  103. #endif // ENABLE_METALINK
  104. #if defined(ENABLE_BITTORRENT) || defined(ENABLE_METALINK)
  105. namespace {
  106. void showFiles(const std::vector<std::string>& uris,
  107. const std::shared_ptr<Option>& op)
  108. {
  109. ProtocolDetector dt;
  110. for (const auto& uri : uris) {
  111. printf(">>> ");
  112. printf(MSG_SHOW_FILES, (uri).c_str());
  113. printf("\n");
  114. try {
  115. #ifdef ENABLE_BITTORRENT
  116. if (dt.guessTorrentFile(uri)) {
  117. showTorrentFile(uri);
  118. }
  119. else
  120. #endif // ENABLE_BITTORRENT
  121. #ifdef ENABLE_METALINK
  122. if (dt.guessMetalinkFile(uri)) {
  123. showMetalinkFile(uri, op);
  124. }
  125. else
  126. #endif // ENABLE_METALINK
  127. {
  128. printf("%s\n\n", MSG_NOT_TORRENT_METALINK);
  129. }
  130. }
  131. catch (RecoverableException& e) {
  132. global::cout()->printf("%s\n", e.stackTrace().c_str());
  133. }
  134. }
  135. }
  136. } // namespace
  137. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  138. extern error_code::Value option_processing(Option& option, bool standalone,
  139. std::vector<std::string>& uris,
  140. int argc, char** argv,
  141. const KeyVals& options);
  142. Context::Context(bool standalone, int argc, char** argv, const KeyVals& options)
  143. {
  144. std::vector<std::string> args;
  145. auto op = std::make_shared<Option>();
  146. error_code::Value rv;
  147. rv = option_processing(*op.get(), standalone, args, argc, argv, options);
  148. if (rv != error_code::FINISHED) {
  149. if (standalone) {
  150. exit(rv);
  151. }
  152. else {
  153. throw DL_ABORT_EX("Option processing failed");
  154. }
  155. }
  156. #ifdef ENABLE_BITTORRENT
  157. bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX));
  158. bittorrent::generateStaticPeerAgent(op->get(PREF_PEER_AGENT));
  159. #endif // ENABLE_BITTORRENT
  160. LogFactory::setLogFile(op->get(PREF_LOG));
  161. LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL));
  162. LogFactory::setConsoleLogLevel(op->get(PREF_CONSOLE_LOG_LEVEL));
  163. LogFactory::setColorOutput(op->getAsBool(PREF_ENABLE_COLOR));
  164. if (op->getAsBool(PREF_QUIET)) {
  165. LogFactory::setConsoleOutput(false);
  166. }
  167. LogFactory::reconfigure();
  168. A2_LOG_INFO("<<--- --- --- ---");
  169. A2_LOG_INFO(" --- --- --- ---");
  170. A2_LOG_INFO(" --- --- --- --->>");
  171. A2_LOG_INFO(fmt("%s %s", PACKAGE, PACKAGE_VERSION));
  172. A2_LOG_INFO(usedCompilerAndPlatform());
  173. A2_LOG_INFO(getOperatingSystemInfo());
  174. A2_LOG_INFO(usedLibs());
  175. A2_LOG_INFO(MSG_LOGGING_STARTED);
  176. #if defined(HAVE_SYS_RESOURCE_H) && defined(RLIMIT_NOFILE)
  177. rlimit r = {0, 0};
  178. if (getrlimit(RLIMIT_NOFILE, &r) >= 0 && r.rlim_cur != RLIM_INFINITY) {
  179. // Thanks portability, for making it easy :p
  180. auto rlim_new = r.rlim_cur; // So we get the right type for free.
  181. if (r.rlim_cur != RLIM_INFINITY) {
  182. rlim_new = op->getAsInt(PREF_RLIMIT_NOFILE);
  183. rlim_new = std::max(r.rlim_cur, rlim_new);
  184. if (r.rlim_max != RLIM_INFINITY) {
  185. rlim_new = std::min(r.rlim_max, rlim_new);
  186. }
  187. }
  188. if (rlim_new != r.rlim_cur) {
  189. if (setrlimit(RLIMIT_NOFILE, &r) != 0) {
  190. int errNum = errno;
  191. A2_LOG_WARN(fmt("Failed to set rlimit NO_FILE from %" PRIu64 " to "
  192. "%" PRIu64 ": %s",
  193. (uint64_t)r.rlim_cur, (uint64_t)rlim_new,
  194. util::safeStrerror(errNum).c_str()));
  195. }
  196. else {
  197. A2_LOG_DEBUG(fmt("Set rlimit NO_FILE from %" PRIu64 " to %" PRIu64,
  198. (uint64_t)r.rlim_cur, (uint64_t)rlim_new));
  199. }
  200. }
  201. else {
  202. rlim_new = op->getAsInt(PREF_RLIMIT_NOFILE);
  203. A2_LOG_DEBUG(fmt("Not setting rlimit NO_FILE: %" PRIu64 " >= %" PRIu64,
  204. (uint64_t)r.rlim_cur, (uint64_t)rlim_new));
  205. }
  206. }
  207. #endif // defined(HAVE_SYS_RESOURCE_H) && defined(RLIMIT_NOFILE)
  208. if (op->getAsBool(PREF_DISABLE_IPV6)) {
  209. SocketCore::setProtocolFamily(AF_INET);
  210. }
  211. SocketCore::setIpDscp(op->getAsInt(PREF_DSCP));
  212. SocketCore::setSocketRecvBufferSize(
  213. op->getAsInt(PREF_SOCKET_RECV_BUFFER_SIZE));
  214. net::checkAddrconfig();
  215. if (!net::getIPv4AddrConfigured() && !net::getIPv6AddrConfigured()) {
  216. // Get rid of AI_ADDRCONFIG. It causes name resolution error when
  217. // none of network interface has IPv4/v6 address.
  218. setDefaultAIFlags(0);
  219. }
  220. // Bind interface
  221. if (!op->get(PREF_INTERFACE).empty()) {
  222. std::string iface = op->get(PREF_INTERFACE);
  223. SocketCore::bindAddress(iface);
  224. }
  225. // Bind multiple interfaces
  226. if (!op->get(PREF_MULTIPLE_INTERFACE).empty() &&
  227. op->get(PREF_INTERFACE).empty()) {
  228. std::string ifaces = op->get(PREF_MULTIPLE_INTERFACE);
  229. SocketCore::bindAllAddress(ifaces);
  230. }
  231. std::vector<std::shared_ptr<RequestGroup>> requestGroups;
  232. std::shared_ptr<UriListParser> uriListParser;
  233. #ifdef ENABLE_BITTORRENT
  234. if (!op->blank(PREF_TORRENT_FILE)) {
  235. if (op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
  236. showTorrentFile(op->get(PREF_TORRENT_FILE));
  237. return;
  238. }
  239. else {
  240. createRequestGroupForBitTorrent(requestGroups, op, args,
  241. op->get(PREF_TORRENT_FILE));
  242. }
  243. }
  244. else
  245. #endif // ENABLE_BITTORRENT
  246. #ifdef ENABLE_METALINK
  247. if (!op->blank(PREF_METALINK_FILE)) {
  248. if (op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
  249. showMetalinkFile(op->get(PREF_METALINK_FILE), op);
  250. return;
  251. }
  252. else {
  253. createRequestGroupForMetalink(requestGroups, op);
  254. }
  255. }
  256. else
  257. #endif // ENABLE_METALINK
  258. if (!op->blank(PREF_INPUT_FILE)) {
  259. if (op->getAsBool(PREF_DEFERRED_INPUT)) {
  260. uriListParser = openUriListParser(op->get(PREF_INPUT_FILE));
  261. }
  262. else {
  263. createRequestGroupForUriList(requestGroups, op);
  264. }
  265. #if defined(ENABLE_BITTORRENT) || defined(ENABLE_METALINK)
  266. }
  267. else if (op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
  268. showFiles(args, op);
  269. return;
  270. #endif // ENABLE_METALINK || ENABLE_METALINK
  271. }
  272. else {
  273. createRequestGroupForUri(requestGroups, op, args, false, false, true);
  274. }
  275. // Remove option values which is only valid for URIs specified in
  276. // command-line. If they are left, because op is used as a template
  277. // for new RequestGroup(such as created in RPC command), they causes
  278. // unintentional effect.
  279. op->remove(PREF_OUT);
  280. op->remove(PREF_FORCE_SEQUENTIAL);
  281. op->remove(PREF_INPUT_FILE);
  282. op->remove(PREF_INDEX_OUT);
  283. op->remove(PREF_SELECT_FILE);
  284. op->remove(PREF_PAUSE);
  285. op->remove(PREF_CHECKSUM);
  286. op->remove(PREF_GID);
  287. if (standalone && !op->getAsBool(PREF_ENABLE_RPC) && requestGroups.empty() &&
  288. !uriListParser) {
  289. global::cout()->printf("%s\n", MSG_NO_FILES_TO_DOWNLOAD);
  290. }
  291. else {
  292. if (!requestGroups.empty()) {
  293. A2_LOG_NOTICE(fmt("Downloading %" PRId64 " item(s)",
  294. static_cast<uint64_t>(requestGroups.size())));
  295. }
  296. reqinfo = std::make_shared<MultiUrlRequestInfo>(std::move(requestGroups),
  297. op, uriListParser);
  298. }
  299. }
  300. Context::~Context() = default;
  301. } // namespace aria2