BtSetup.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "BtSetup.h"
  36. #include <cstring>
  37. #include "RequestGroup.h"
  38. #include "DownloadEngine.h"
  39. #include "Option.h"
  40. #include "BtRegistry.h"
  41. #include "PeerListenCommand.h"
  42. #include "TrackerWatcherCommand.h"
  43. #include "SeedCheckCommand.h"
  44. #include "PeerChokeCommand.h"
  45. #include "ActivePeerConnectionCommand.h"
  46. #include "PeerListenCommand.h"
  47. #include "UnionSeedCriteria.h"
  48. #include "TimeSeedCriteria.h"
  49. #include "ShareRatioSeedCriteria.h"
  50. #include "prefs.h"
  51. #include "LogFactory.h"
  52. #include "Logger.h"
  53. #include "util.h"
  54. #include "SegList.h"
  55. #include "DHTGetPeersCommand.h"
  56. #include "DHTPeerAnnounceStorage.h"
  57. #include "DHTSetup.h"
  58. #include "DHTRegistry.h"
  59. #include "DHTNode.h"
  60. #include "DHTRoutingTable.h"
  61. #include "DHTTaskQueue.h"
  62. #include "DHTTaskFactory.h"
  63. #include "DHTTokenTracker.h"
  64. #include "DHTMessageDispatcher.h"
  65. #include "DHTMessageReceiver.h"
  66. #include "DHTMessageFactory.h"
  67. #include "DHTMessageCallback.h"
  68. #include "UDPTrackerClient.h"
  69. #include "BtProgressInfoFile.h"
  70. #include "BtAnnounce.h"
  71. #include "BtRuntime.h"
  72. #include "bittorrent_helper.h"
  73. #include "BtStopDownloadCommand.h"
  74. #include "LpdReceiveMessageCommand.h"
  75. #include "LpdDispatchMessageCommand.h"
  76. #include "LpdMessageReceiver.h"
  77. #include "LpdMessageDispatcher.h"
  78. #include "message.h"
  79. #include "SocketCore.h"
  80. #include "DlAbortEx.h"
  81. #include "array_fun.h"
  82. #include "DownloadContext.h"
  83. #include "PieceStorage.h"
  84. #include "PeerStorage.h"
  85. #include "fmt.h"
  86. namespace aria2 {
  87. BtSetup::BtSetup() = default;
  88. void BtSetup::setup(std::vector<std::unique_ptr<Command>>& commands,
  89. RequestGroup* requestGroup, DownloadEngine* e,
  90. const Option* option)
  91. {
  92. if (!requestGroup->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) {
  93. return;
  94. }
  95. auto torrentAttrs =
  96. bittorrent::getTorrentAttrs(requestGroup->getDownloadContext());
  97. bool metadataGetMode = torrentAttrs->metadata.empty();
  98. auto& btReg = e->getBtRegistry();
  99. auto btObject = btReg->get(requestGroup->getGID());
  100. auto& pieceStorage = btObject->pieceStorage;
  101. auto& peerStorage = btObject->peerStorage;
  102. auto& btRuntime = btObject->btRuntime;
  103. auto& btAnnounce = btObject->btAnnounce;
  104. // commands
  105. {
  106. auto c = make_unique<TrackerWatcherCommand>(e->newCUID(), requestGroup, e);
  107. c->setPeerStorage(peerStorage);
  108. c->setPieceStorage(pieceStorage);
  109. c->setBtRuntime(btRuntime);
  110. c->setBtAnnounce(btAnnounce);
  111. commands.push_back(std::move(c));
  112. }
  113. if (!metadataGetMode) {
  114. auto c = make_unique<PeerChokeCommand>(e->newCUID(), e);
  115. c->setPeerStorage(peerStorage);
  116. c->setBtRuntime(btRuntime);
  117. commands.push_back(std::move(c));
  118. }
  119. {
  120. auto c = make_unique<ActivePeerConnectionCommand>(
  121. e->newCUID(), requestGroup, e, metadataGetMode ? 2_s : 10_s);
  122. c->setBtRuntime(btRuntime);
  123. c->setPieceStorage(pieceStorage);
  124. c->setPeerStorage(peerStorage);
  125. c->setBtAnnounce(btAnnounce);
  126. commands.push_back(std::move(c));
  127. }
  128. if (metadataGetMode || !torrentAttrs->privateTorrent) {
  129. if (DHTRegistry::isInitialized()) {
  130. auto command =
  131. make_unique<DHTGetPeersCommand>(e->newCUID(), requestGroup, e);
  132. command->setTaskQueue(DHTRegistry::getData().taskQueue.get());
  133. command->setTaskFactory(DHTRegistry::getData().taskFactory.get());
  134. command->setBtRuntime(btRuntime);
  135. command->setPeerStorage(peerStorage);
  136. commands.push_back(std::move(command));
  137. }
  138. if (DHTRegistry::isInitialized6()) {
  139. auto command =
  140. make_unique<DHTGetPeersCommand>(e->newCUID(), requestGroup, e);
  141. command->setTaskQueue(DHTRegistry::getData6().taskQueue.get());
  142. command->setTaskFactory(DHTRegistry::getData6().taskFactory.get());
  143. command->setBtRuntime(btRuntime);
  144. command->setPeerStorage(peerStorage);
  145. commands.push_back(std::move(command));
  146. }
  147. }
  148. if (!metadataGetMode) {
  149. auto unionCri = make_unique<UnionSeedCriteria>();
  150. if (option->defined(PREF_SEED_TIME)) {
  151. unionCri->addSeedCriteria(
  152. make_unique<TimeSeedCriteria>(std::chrono::seconds(
  153. static_cast<int>(option->getAsDouble(PREF_SEED_TIME) * 60))));
  154. }
  155. {
  156. double ratio = option->getAsDouble(PREF_SEED_RATIO);
  157. if (ratio > 0.0) {
  158. auto cri = make_unique<ShareRatioSeedCriteria>(
  159. option->getAsDouble(PREF_SEED_RATIO),
  160. requestGroup->getDownloadContext());
  161. cri->setPieceStorage(pieceStorage);
  162. cri->setBtRuntime(btRuntime);
  163. unionCri->addSeedCriteria(std::move(cri));
  164. }
  165. }
  166. if (!unionCri->getSeedCriterion().empty()) {
  167. auto c = make_unique<SeedCheckCommand>(e->newCUID(), requestGroup, e,
  168. std::move(unionCri));
  169. c->setPieceStorage(pieceStorage);
  170. c->setBtRuntime(btRuntime);
  171. commands.push_back(std::move(c));
  172. }
  173. }
  174. if (btReg->getTcpPort() == 0) {
  175. static int families[] = {AF_INET, AF_INET6};
  176. size_t familiesLength =
  177. e->getOption()->getAsBool(PREF_DISABLE_IPV6) ? 1 : 2;
  178. for (size_t i = 0; i < familiesLength; ++i) {
  179. auto command =
  180. make_unique<PeerListenCommand>(e->newCUID(), e, families[i]);
  181. bool ret;
  182. uint16_t port;
  183. if (btReg->getTcpPort()) {
  184. SegList<int> sgl;
  185. int usedPort = btReg->getTcpPort();
  186. sgl.add(usedPort, usedPort + 1);
  187. ret = command->bindPort(port, sgl);
  188. }
  189. else {
  190. auto sgl =
  191. util::parseIntSegments(e->getOption()->get(PREF_LISTEN_PORT));
  192. sgl.normalize();
  193. ret = command->bindPort(port, sgl);
  194. }
  195. if (ret) {
  196. btReg->setTcpPort(port);
  197. // Add command to DownloadEngine directly.
  198. e->addCommand(std::move(command));
  199. }
  200. }
  201. if (btReg->getTcpPort() == 0) {
  202. throw DL_ABORT_EX(_("Errors occurred while binding port.\n"));
  203. }
  204. }
  205. btAnnounce->setTcpPort(btReg->getTcpPort());
  206. if (option->getAsBool(PREF_BT_ENABLE_LPD) && btReg->getTcpPort() &&
  207. (metadataGetMode || !torrentAttrs->privateTorrent)) {
  208. if (!btReg->getLpdMessageReceiver()) {
  209. A2_LOG_INFO("Initializing LpdMessageReceiver.");
  210. auto receiver = std::make_shared<LpdMessageReceiver>(LPD_MULTICAST_ADDR,
  211. LPD_MULTICAST_PORT);
  212. bool initialized = false;
  213. const std::string& lpdInterface =
  214. e->getOption()->get(PREF_BT_LPD_INTERFACE);
  215. if (lpdInterface.empty()) {
  216. if (receiver->init("")) {
  217. initialized = true;
  218. }
  219. }
  220. else {
  221. auto ifAddrs = SocketCore::getInterfaceAddress(lpdInterface, AF_INET,
  222. AI_NUMERICHOST);
  223. for (const auto& soaddr : ifAddrs) {
  224. char host[NI_MAXHOST];
  225. if (inetNtop(AF_INET, &soaddr.su.in.sin_addr, host, sizeof(host)) ==
  226. 0 &&
  227. receiver->init(host)) {
  228. initialized = true;
  229. break;
  230. }
  231. }
  232. }
  233. if (initialized) {
  234. btReg->setLpdMessageReceiver(receiver);
  235. A2_LOG_INFO(fmt("LpdMessageReceiver initialized. multicastAddr=%s:%u,"
  236. " localAddr=%s",
  237. LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
  238. receiver->getLocalAddress().c_str()));
  239. e->addCommand(
  240. make_unique<LpdReceiveMessageCommand>(e->newCUID(), receiver, e));
  241. }
  242. else {
  243. A2_LOG_INFO("LpdMessageReceiver not initialized.");
  244. }
  245. }
  246. if (btReg->getLpdMessageReceiver()) {
  247. const unsigned char* infoHash =
  248. bittorrent::getInfoHash(requestGroup->getDownloadContext());
  249. A2_LOG_INFO("Initializing LpdMessageDispatcher.");
  250. auto dispatcher = std::make_shared<LpdMessageDispatcher>(
  251. std::string(&infoHash[0], &infoHash[INFO_HASH_LENGTH]),
  252. btReg->getTcpPort(), LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
  253. if (dispatcher->init(btReg->getLpdMessageReceiver()->getLocalAddress(),
  254. /*ttl*/ 1, /*loop*/ 1)) {
  255. A2_LOG_INFO("LpdMessageDispatcher initialized.");
  256. auto cmd =
  257. make_unique<LpdDispatchMessageCommand>(e->newCUID(), dispatcher, e);
  258. cmd->setBtRuntime(btRuntime);
  259. e->addCommand(std::move(cmd));
  260. }
  261. else {
  262. A2_LOG_INFO("LpdMessageDispatcher not initialized.");
  263. }
  264. }
  265. }
  266. auto btStopTimeout = option->getAsInt(PREF_BT_STOP_TIMEOUT);
  267. if (btStopTimeout > 0) {
  268. auto stopDownloadCommand = make_unique<BtStopDownloadCommand>(
  269. e->newCUID(), requestGroup, e, std::chrono::seconds(btStopTimeout));
  270. stopDownloadCommand->setBtRuntime(btRuntime);
  271. stopDownloadCommand->setPieceStorage(pieceStorage);
  272. commands.push_back(std::move(stopDownloadCommand));
  273. }
  274. btRuntime->setReady(true);
  275. }
  276. } // namespace aria2