BtSetup.cc 9.3 KB

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