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