BtSetup.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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<Command*>& commands,
  89. RequestGroup* requestGroup,
  90. DownloadEngine* e,
  91. const Option* option)
  92. {
  93. if(!requestGroup->getDownloadContext()->hasAttribute(CTX_ATTR_BT)){
  94. return;
  95. }
  96. std::shared_ptr<TorrentAttribute> torrentAttrs =
  97. bittorrent::getTorrentAttrs(requestGroup->getDownloadContext());
  98. bool metadataGetMode = torrentAttrs->metadata.empty();
  99. const std::shared_ptr<BtRegistry>& btReg = e->getBtRegistry();
  100. const std::shared_ptr<BtObject>& btObject = btReg->get(requestGroup->getGID());
  101. const std::shared_ptr<PieceStorage>& pieceStorage = btObject->pieceStorage;
  102. const std::shared_ptr<PeerStorage>& peerStorage = btObject->peerStorage;
  103. const std::shared_ptr<BtRuntime>& btRuntime = btObject->btRuntime;
  104. const std::shared_ptr<BtAnnounce>& btAnnounce = btObject->btAnnounce;
  105. // commands
  106. {
  107. TrackerWatcherCommand* c =
  108. new TrackerWatcherCommand(e->newCUID(), requestGroup, e);
  109. c->setPeerStorage(peerStorage);
  110. c->setPieceStorage(pieceStorage);
  111. c->setBtRuntime(btRuntime);
  112. c->setBtAnnounce(btAnnounce);
  113. commands.push_back(c);
  114. }
  115. if(!metadataGetMode) {
  116. PeerChokeCommand* c =
  117. new PeerChokeCommand(e->newCUID(), e);
  118. c->setPeerStorage(peerStorage);
  119. c->setBtRuntime(btRuntime);
  120. commands.push_back(c);
  121. }
  122. {
  123. ActivePeerConnectionCommand* c =
  124. new ActivePeerConnectionCommand(e->newCUID(), requestGroup, e,
  125. metadataGetMode?2:10);
  126. c->setBtRuntime(btRuntime);
  127. c->setPieceStorage(pieceStorage);
  128. c->setPeerStorage(peerStorage);
  129. c->setBtAnnounce(btAnnounce);
  130. commands.push_back(c);
  131. }
  132. if(metadataGetMode || !torrentAttrs->privateTorrent) {
  133. if(DHTRegistry::isInitialized()) {
  134. DHTGetPeersCommand* command =
  135. new DHTGetPeersCommand(e->newCUID(), requestGroup, e);
  136. command->setTaskQueue(DHTRegistry::getData().taskQueue);
  137. command->setTaskFactory(DHTRegistry::getData().taskFactory);
  138. command->setBtRuntime(btRuntime);
  139. command->setPeerStorage(peerStorage);
  140. commands.push_back(command);
  141. }
  142. if(DHTRegistry::isInitialized6()) {
  143. DHTGetPeersCommand* command =
  144. new DHTGetPeersCommand(e->newCUID(), requestGroup, e);
  145. command->setTaskQueue(DHTRegistry::getData6().taskQueue);
  146. command->setTaskFactory(DHTRegistry::getData6().taskFactory);
  147. command->setBtRuntime(btRuntime);
  148. command->setPeerStorage(peerStorage);
  149. commands.push_back(command);
  150. }
  151. }
  152. if(!metadataGetMode) {
  153. std::shared_ptr<UnionSeedCriteria> unionCri(new UnionSeedCriteria());
  154. if(option->defined(PREF_SEED_TIME)) {
  155. std::shared_ptr<SeedCriteria> cri
  156. (new TimeSeedCriteria(option->getAsInt(PREF_SEED_TIME)*60));
  157. unionCri->addSeedCriteria(cri);
  158. }
  159. {
  160. double ratio = option->getAsDouble(PREF_SEED_RATIO);
  161. if(ratio > 0.0) {
  162. std::shared_ptr<ShareRatioSeedCriteria> cri
  163. (new ShareRatioSeedCriteria(option->getAsDouble(PREF_SEED_RATIO),
  164. requestGroup->getDownloadContext()));
  165. cri->setPieceStorage(pieceStorage);
  166. cri->setBtRuntime(btRuntime);
  167. unionCri->addSeedCriteria(cri);
  168. }
  169. }
  170. if(!unionCri->getSeedCriterion().empty()) {
  171. SeedCheckCommand* c =
  172. new SeedCheckCommand(e->newCUID(), requestGroup, e, unionCri);
  173. c->setPieceStorage(pieceStorage);
  174. c->setBtRuntime(btRuntime);
  175. commands.push_back(c);
  176. }
  177. }
  178. if(btReg->getTcpPort() == 0) {
  179. static int families[] = { AF_INET, AF_INET6 };
  180. size_t familiesLength = e->getOption()->getAsBool(PREF_DISABLE_IPV6)?1:2;
  181. for(size_t i = 0; i < familiesLength; ++i) {
  182. PeerListenCommand* command =
  183. new PeerListenCommand(e->newCUID(), e, families[i]);
  184. bool ret;
  185. uint16_t port;
  186. if(btReg->getTcpPort()) {
  187. SegList<int> sgl;
  188. int usedPort = btReg->getTcpPort();
  189. sgl.add(usedPort, usedPort+1);
  190. ret = command->bindPort(port, sgl);
  191. } else {
  192. SegList<int> sgl;
  193. util::parseIntSegments(sgl, e->getOption()->get(PREF_LISTEN_PORT));
  194. sgl.normalize();
  195. ret = command->bindPort(port, sgl);
  196. }
  197. if(ret) {
  198. btReg->setTcpPort(port);
  199. // Add command to DownloadEngine directly.
  200. e->addCommand(command);
  201. } else {
  202. delete command;
  203. }
  204. }
  205. if(btReg->getTcpPort() == 0) {
  206. throw DL_ABORT_EX(_("Errors occurred while binding port.\n"));
  207. }
  208. }
  209. btAnnounce->setTcpPort(btReg->getTcpPort());
  210. if(option->getAsBool(PREF_BT_ENABLE_LPD) &&
  211. btReg->getTcpPort() &&
  212. (metadataGetMode || !torrentAttrs->privateTorrent)) {
  213. if(!btReg->getLpdMessageReceiver()) {
  214. A2_LOG_INFO("Initializing LpdMessageReceiver.");
  215. std::shared_ptr<LpdMessageReceiver> receiver
  216. (new LpdMessageReceiver(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT));
  217. bool initialized = false;
  218. const std::string& lpdInterface =
  219. e->getOption()->get(PREF_BT_LPD_INTERFACE);
  220. if(lpdInterface.empty()) {
  221. if(receiver->init("")) {
  222. initialized = true;
  223. }
  224. } else {
  225. std::vector<std::pair<sockaddr_union, socklen_t> > ifAddrs;
  226. getInterfaceAddress(ifAddrs, lpdInterface, AF_INET, AI_NUMERICHOST);
  227. for(std::vector<std::pair<sockaddr_union, socklen_t> >::const_iterator
  228. i = ifAddrs.begin(), eoi = ifAddrs.end(); i != eoi; ++i) {
  229. char host[NI_MAXHOST];
  230. if(inetNtop(AF_INET, &(*i).first.in.sin_addr, host,
  231. sizeof(host)) == 0 &&
  232. receiver->init(host)) {
  233. initialized = true;
  234. break;
  235. }
  236. }
  237. }
  238. if(initialized) {
  239. btReg->setLpdMessageReceiver(receiver);
  240. A2_LOG_INFO(fmt("LpdMessageReceiver initialized. multicastAddr=%s:%u,"
  241. " localAddr=%s",
  242. LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
  243. receiver->getLocalAddress().c_str()));
  244. LpdReceiveMessageCommand* cmd =
  245. new LpdReceiveMessageCommand(e->newCUID(), receiver, e);
  246. e->addCommand(cmd);
  247. } else {
  248. A2_LOG_INFO("LpdMessageReceiver not initialized.");
  249. }
  250. }
  251. if(btReg->getLpdMessageReceiver()) {
  252. const unsigned char* infoHash =
  253. bittorrent::getInfoHash(requestGroup->getDownloadContext());
  254. A2_LOG_INFO("Initializing LpdMessageDispatcher.");
  255. std::shared_ptr<LpdMessageDispatcher> dispatcher
  256. (new LpdMessageDispatcher
  257. (std::string(&infoHash[0], &infoHash[INFO_HASH_LENGTH]),
  258. btReg->getTcpPort(),
  259. LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT));
  260. if(dispatcher->init(btReg->getLpdMessageReceiver()->getLocalAddress(),
  261. /*ttl*/1, /*loop*/1)) {
  262. A2_LOG_INFO("LpdMessageDispatcher initialized.");
  263. LpdDispatchMessageCommand* cmd =
  264. new LpdDispatchMessageCommand(e->newCUID(), dispatcher, e);
  265. cmd->setBtRuntime(btRuntime);
  266. e->addCommand(cmd);
  267. } else {
  268. A2_LOG_INFO("LpdMessageDispatcher not initialized.");
  269. }
  270. }
  271. }
  272. time_t btStopTimeout = option->getAsInt(PREF_BT_STOP_TIMEOUT);
  273. if(btStopTimeout > 0) {
  274. BtStopDownloadCommand* stopDownloadCommand =
  275. new BtStopDownloadCommand(e->newCUID(), requestGroup, e, btStopTimeout);
  276. stopDownloadCommand->setBtRuntime(btRuntime);
  277. stopDownloadCommand->setPieceStorage(pieceStorage);
  278. commands.push_back(stopDownloadCommand);
  279. }
  280. btRuntime->setReady(true);
  281. }
  282. } // namespace aria2