BtSetup.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 "IntSequence.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 "BtProgressInfoFile.h"
  69. #include "BtAnnounce.h"
  70. #include "BtRuntime.h"
  71. #include "bittorrent_helper.h"
  72. #include "BtStopDownloadCommand.h"
  73. #include "LpdReceiveMessageCommand.h"
  74. #include "LpdDispatchMessageCommand.h"
  75. #include "LpdMessageReceiver.h"
  76. #include "LpdMessageDispatcher.h"
  77. #include "message.h"
  78. #include "SocketCore.h"
  79. #include "DlAbortEx.h"
  80. #include "array_fun.h"
  81. #include "DownloadContext.h"
  82. #include "PieceStorage.h"
  83. #include "PeerStorage.h"
  84. #include "fmt.h"
  85. namespace aria2 {
  86. BtSetup::BtSetup() {}
  87. void BtSetup::setup(std::vector<Command*>& commands,
  88. RequestGroup* requestGroup,
  89. DownloadEngine* e,
  90. const Option* option)
  91. {
  92. if(!requestGroup->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)){
  93. return;
  94. }
  95. SharedHandle<TorrentAttribute> torrentAttrs =
  96. bittorrent::getTorrentAttrs(requestGroup->getDownloadContext());
  97. bool metadataGetMode = torrentAttrs->metadata.empty();
  98. BtObject btObject = e->getBtRegistry()->get(requestGroup->getGID());
  99. SharedHandle<PieceStorage> pieceStorage = btObject.pieceStorage_;
  100. SharedHandle<PeerStorage> peerStorage = btObject.peerStorage_;
  101. SharedHandle<BtRuntime> btRuntime = btObject.btRuntime_;
  102. SharedHandle<BtAnnounce> btAnnounce = btObject.btAnnounce_;
  103. // commands
  104. {
  105. TrackerWatcherCommand* c =
  106. new 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(c);
  112. }
  113. if(!metadataGetMode) {
  114. PeerChokeCommand* c =
  115. new PeerChokeCommand(e->newCUID(), e);
  116. c->setPeerStorage(peerStorage);
  117. c->setBtRuntime(btRuntime);
  118. commands.push_back(c);
  119. }
  120. {
  121. ActivePeerConnectionCommand* c =
  122. new ActivePeerConnectionCommand(e->newCUID(), requestGroup, e,
  123. metadataGetMode?2:10);
  124. c->setBtRuntime(btRuntime);
  125. c->setPieceStorage(pieceStorage);
  126. c->setPeerStorage(peerStorage);
  127. c->setBtAnnounce(btAnnounce);
  128. commands.push_back(c);
  129. }
  130. if(metadataGetMode || !torrentAttrs->privateTorrent) {
  131. if(DHTRegistry::isInitialized()) {
  132. DHTGetPeersCommand* command =
  133. new DHTGetPeersCommand(e->newCUID(), requestGroup, e);
  134. command->setTaskQueue(DHTRegistry::getData().taskQueue);
  135. command->setTaskFactory(DHTRegistry::getData().taskFactory);
  136. command->setBtRuntime(btRuntime);
  137. command->setPeerStorage(peerStorage);
  138. commands.push_back(command);
  139. }
  140. if(DHTRegistry::isInitialized6()) {
  141. DHTGetPeersCommand* command =
  142. new DHTGetPeersCommand(e->newCUID(), requestGroup, e);
  143. command->setTaskQueue(DHTRegistry::getData6().taskQueue);
  144. command->setTaskFactory(DHTRegistry::getData6().taskFactory);
  145. command->setBtRuntime(btRuntime);
  146. command->setPeerStorage(peerStorage);
  147. commands.push_back(command);
  148. }
  149. }
  150. if(!metadataGetMode) {
  151. SharedHandle<UnionSeedCriteria> unionCri(new UnionSeedCriteria());
  152. if(option->defined(PREF_SEED_TIME)) {
  153. SharedHandle<SeedCriteria> cri
  154. (new TimeSeedCriteria(option->getAsInt(PREF_SEED_TIME)*60));
  155. unionCri->addSeedCriteria(cri);
  156. }
  157. {
  158. double ratio = option->getAsDouble(PREF_SEED_RATIO);
  159. if(ratio > 0.0) {
  160. SharedHandle<ShareRatioSeedCriteria> cri
  161. (new ShareRatioSeedCriteria(option->getAsDouble(PREF_SEED_RATIO),
  162. requestGroup->getDownloadContext()));
  163. cri->setPieceStorage(pieceStorage);
  164. cri->setPeerStorage(peerStorage);
  165. unionCri->addSeedCriteria(cri);
  166. }
  167. }
  168. if(!unionCri->getSeedCriterion().empty()) {
  169. SeedCheckCommand* c =
  170. new SeedCheckCommand(e->newCUID(), requestGroup, e, unionCri);
  171. c->setPieceStorage(pieceStorage);
  172. c->setBtRuntime(btRuntime);
  173. commands.push_back(c);
  174. }
  175. }
  176. if(PeerListenCommand::getNumInstance() == 0) {
  177. static int families[] = { AF_INET, AF_INET6 };
  178. size_t familiesLength = e->getOption()->getAsBool(PREF_DISABLE_IPV6)?1:2;
  179. for(size_t i = 0; i < familiesLength; ++i) {
  180. PeerListenCommand* listenCommand =
  181. PeerListenCommand::getInstance(e, families[i]);
  182. bool ret;
  183. uint16_t port;
  184. if(btRuntime->getListenPort()) {
  185. IntSequence seq =
  186. util::parseIntRange(util::uitos(btRuntime->getListenPort()));
  187. ret = listenCommand->bindPort(port, seq);
  188. } else {
  189. IntSequence seq =
  190. util::parseIntRange(e->getOption()->get(PREF_LISTEN_PORT));
  191. ret = listenCommand->bindPort(port, seq);
  192. }
  193. if(ret) {
  194. btRuntime->setListenPort(port);
  195. // Add command to DownloadEngine directly.
  196. e->addCommand(listenCommand);
  197. } else {
  198. delete listenCommand;
  199. }
  200. }
  201. if(PeerListenCommand::getNumInstance() == 0) {
  202. throw DL_ABORT_EX(_("Errors occurred while binding port.\n"));
  203. }
  204. } else {
  205. PeerListenCommand* listenCommand =
  206. PeerListenCommand::getInstance(e, AF_INET);
  207. if(!listenCommand) {
  208. listenCommand = PeerListenCommand::getInstance(e, AF_INET6);
  209. }
  210. btRuntime->setListenPort(listenCommand->getPort());
  211. }
  212. if(option->getAsBool(PREF_BT_ENABLE_LPD) &&
  213. (metadataGetMode || !torrentAttrs->privateTorrent)) {
  214. if(LpdReceiveMessageCommand::getNumInstance() == 0) {
  215. A2_LOG_INFO("Initializing LpdMessageReceiver.");
  216. SharedHandle<LpdMessageReceiver> receiver
  217. (new LpdMessageReceiver(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT));
  218. bool initialized = false;
  219. const std::string& lpdInterface =
  220. e->getOption()->get(PREF_BT_LPD_INTERFACE);
  221. if(lpdInterface.empty()) {
  222. if(receiver->init("")) {
  223. initialized = true;
  224. }
  225. } else {
  226. std::vector<std::pair<sockaddr_storage, socklen_t> > ifAddrs;
  227. getInterfaceAddress(ifAddrs, lpdInterface, AF_INET, AI_NUMERICHOST);
  228. for(std::vector<std::pair<sockaddr_storage, socklen_t> >::const_iterator
  229. i = ifAddrs.begin(), eoi = ifAddrs.end(); i != eoi; ++i) {
  230. sockaddr_in addr;
  231. memcpy(&addr, &(*i).first, (*i).second);
  232. if(receiver->init(inet_ntoa(addr.sin_addr))) {
  233. initialized = true;
  234. break;
  235. }
  236. }
  237. }
  238. if(initialized) {
  239. A2_LOG_INFO(fmt("LpdMessageReceiver initialized. multicastAddr=%s:%u,"
  240. " localAddr=%s",
  241. LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
  242. receiver->getLocalAddress().c_str()));
  243. LpdReceiveMessageCommand* cmd =
  244. LpdReceiveMessageCommand::getInstance(e, receiver);
  245. e->addCommand(cmd);
  246. } else {
  247. A2_LOG_INFO("LpdMessageReceiver not initialized.");
  248. }
  249. }
  250. if(LpdReceiveMessageCommand::getNumInstance()) {
  251. const unsigned char* infoHash =
  252. bittorrent::getInfoHash(requestGroup->getDownloadContext());
  253. SharedHandle<LpdMessageReceiver> receiver =
  254. LpdReceiveMessageCommand::getInstance()->getLpdMessageReceiver();
  255. A2_LOG_INFO("Initializing LpdMessageDispatcher.");
  256. SharedHandle<LpdMessageDispatcher> dispatcher
  257. (new LpdMessageDispatcher
  258. (std::string(&infoHash[0], &infoHash[INFO_HASH_LENGTH]),
  259. btRuntime->getListenPort(),
  260. LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT));
  261. if(dispatcher->init(receiver->getLocalAddress(), /*ttl*/1, /*loop*/0)) {
  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