BtSetup.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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() {}
  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(make_unique<TimeSeedCriteria>(
  152. std::chrono::seconds(option->getAsInt(PREF_SEED_TIME) * 60)));
  153. }
  154. {
  155. double ratio = option->getAsDouble(PREF_SEED_RATIO);
  156. if (ratio > 0.0) {
  157. auto cri = make_unique<ShareRatioSeedCriteria>(
  158. option->getAsDouble(PREF_SEED_RATIO),
  159. requestGroup->getDownloadContext());
  160. cri->setPieceStorage(pieceStorage);
  161. cri->setBtRuntime(btRuntime);
  162. unionCri->addSeedCriteria(std::move(cri));
  163. }
  164. }
  165. if (!unionCri->getSeedCriterion().empty()) {
  166. auto c = make_unique<SeedCheckCommand>(e->newCUID(), requestGroup, e,
  167. std::move(unionCri));
  168. c->setPieceStorage(pieceStorage);
  169. c->setBtRuntime(btRuntime);
  170. commands.push_back(std::move(c));
  171. }
  172. }
  173. if (btReg->getTcpPort() == 0) {
  174. static int families[] = {AF_INET, AF_INET6};
  175. size_t familiesLength =
  176. e->getOption()->getAsBool(PREF_DISABLE_IPV6) ? 1 : 2;
  177. for (size_t i = 0; i < familiesLength; ++i) {
  178. auto command =
  179. make_unique<PeerListenCommand>(e->newCUID(), e, families[i]);
  180. bool ret;
  181. uint16_t port;
  182. if (btReg->getTcpPort()) {
  183. SegList<int> sgl;
  184. int usedPort = btReg->getTcpPort();
  185. sgl.add(usedPort, usedPort + 1);
  186. ret = command->bindPort(port, sgl);
  187. }
  188. else {
  189. auto sgl =
  190. util::parseIntSegments(e->getOption()->get(PREF_LISTEN_PORT));
  191. sgl.normalize();
  192. ret = command->bindPort(port, sgl);
  193. }
  194. if (ret) {
  195. btReg->setTcpPort(port);
  196. // Add command to DownloadEngine directly.
  197. e->addCommand(std::move(command));
  198. }
  199. }
  200. if (btReg->getTcpPort() == 0) {
  201. throw DL_ABORT_EX(_("Errors occurred while binding port.\n"));
  202. }
  203. }
  204. btAnnounce->setTcpPort(btReg->getTcpPort());
  205. if (option->getAsBool(PREF_BT_ENABLE_LPD) && btReg->getTcpPort() &&
  206. (metadataGetMode || !torrentAttrs->privateTorrent)) {
  207. if (!btReg->getLpdMessageReceiver()) {
  208. A2_LOG_INFO("Initializing LpdMessageReceiver.");
  209. auto receiver = std::make_shared<LpdMessageReceiver>(LPD_MULTICAST_ADDR,
  210. LPD_MULTICAST_PORT);
  211. bool initialized = false;
  212. const std::string& lpdInterface =
  213. e->getOption()->get(PREF_BT_LPD_INTERFACE);
  214. if (lpdInterface.empty()) {
  215. if (receiver->init("")) {
  216. initialized = true;
  217. }
  218. }
  219. else {
  220. auto ifAddrs = SocketCore::getInterfaceAddress(lpdInterface, AF_INET,
  221. AI_NUMERICHOST);
  222. for (const auto& soaddr : ifAddrs) {
  223. char host[NI_MAXHOST];
  224. if (inetNtop(AF_INET, &soaddr.su.in.sin_addr, host, sizeof(host)) ==
  225. 0 &&
  226. receiver->init(host)) {
  227. initialized = true;
  228. break;
  229. }
  230. }
  231. }
  232. if (initialized) {
  233. btReg->setLpdMessageReceiver(receiver);
  234. A2_LOG_INFO(fmt("LpdMessageReceiver initialized. multicastAddr=%s:%u,"
  235. " localAddr=%s",
  236. LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
  237. receiver->getLocalAddress().c_str()));
  238. e->addCommand(
  239. make_unique<LpdReceiveMessageCommand>(e->newCUID(), receiver, e));
  240. }
  241. else {
  242. A2_LOG_INFO("LpdMessageReceiver not initialized.");
  243. }
  244. }
  245. if (btReg->getLpdMessageReceiver()) {
  246. const unsigned char* infoHash =
  247. bittorrent::getInfoHash(requestGroup->getDownloadContext());
  248. A2_LOG_INFO("Initializing LpdMessageDispatcher.");
  249. auto dispatcher = std::make_shared<LpdMessageDispatcher>(
  250. std::string(&infoHash[0], &infoHash[INFO_HASH_LENGTH]),
  251. btReg->getTcpPort(), LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
  252. if (dispatcher->init(btReg->getLpdMessageReceiver()->getLocalAddress(),
  253. /*ttl*/ 1, /*loop*/ 1)) {
  254. A2_LOG_INFO("LpdMessageDispatcher initialized.");
  255. auto cmd =
  256. make_unique<LpdDispatchMessageCommand>(e->newCUID(), dispatcher, e);
  257. cmd->setBtRuntime(btRuntime);
  258. e->addCommand(std::move(cmd));
  259. }
  260. else {
  261. A2_LOG_INFO("LpdMessageDispatcher not initialized.");
  262. }
  263. }
  264. }
  265. auto btStopTimeout = option->getAsInt(PREF_BT_STOP_TIMEOUT);
  266. if (btStopTimeout > 0) {
  267. auto stopDownloadCommand = make_unique<BtStopDownloadCommand>(
  268. e->newCUID(), requestGroup, e, std::chrono::seconds(btStopTimeout));
  269. stopDownloadCommand->setBtRuntime(btRuntime);
  270. stopDownloadCommand->setPieceStorage(pieceStorage);
  271. commands.push_back(std::move(stopDownloadCommand));
  272. }
  273. btRuntime->setReady(true);
  274. }
  275. } // namespace aria2