BtSetup.cc 9.2 KB

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