BtSetup.cc 11 KB

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