DHTGetPeersMessage.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "DHTGetPeersMessage.h"
  36. #include <cstring>
  37. #include <array>
  38. #include "DHTNode.h"
  39. #include "DHTRoutingTable.h"
  40. #include "DHTMessageFactory.h"
  41. #include "DHTMessageDispatcher.h"
  42. #include "DHTMessageCallback.h"
  43. #include "DHTPeerAnnounceStorage.h"
  44. #include "Peer.h"
  45. #include "DHTTokenTracker.h"
  46. #include "DHTGetPeersReplyMessage.h"
  47. #include "util.h"
  48. #include "BtRegistry.h"
  49. #include "DownloadContext.h"
  50. #include "Option.h"
  51. #include "SocketCore.h"
  52. namespace aria2 {
  53. const std::string DHTGetPeersMessage::GET_PEERS("get_peers");
  54. const std::string DHTGetPeersMessage::INFO_HASH("info_hash");
  55. DHTGetPeersMessage::DHTGetPeersMessage(
  56. const std::shared_ptr<DHTNode>& localNode,
  57. const std::shared_ptr<DHTNode>& remoteNode, const unsigned char* infoHash,
  58. const std::string& transactionID)
  59. : DHTQueryMessage{localNode, remoteNode, transactionID},
  60. peerAnnounceStorage_{nullptr},
  61. tokenTracker_{nullptr},
  62. btRegistry_{nullptr},
  63. family_{AF_INET}
  64. {
  65. memcpy(infoHash_, infoHash, DHT_ID_LENGTH);
  66. }
  67. void DHTGetPeersMessage::addLocalPeer(std::vector<std::shared_ptr<Peer>>& peers)
  68. {
  69. if (!btRegistry_) {
  70. return;
  71. }
  72. auto& dctx = btRegistry_->getDownloadContext(
  73. std::string(infoHash_, infoHash_ + DHT_ID_LENGTH));
  74. if (!dctx) {
  75. return;
  76. }
  77. auto group = dctx->getOwnerRequestGroup();
  78. auto& option = group->getOption();
  79. auto& externalIP = option->get(PREF_BT_EXTERNAL_IP);
  80. if (externalIP.empty()) {
  81. return;
  82. }
  83. std::array<uint8_t, sizeof(struct in6_addr)> dst;
  84. if (inetPton(family_, externalIP.c_str(), dst.data()) == -1) {
  85. return;
  86. }
  87. auto tcpPort = btRegistry_->getTcpPort();
  88. if (std::find_if(std::begin(peers), std::end(peers),
  89. [&externalIP, tcpPort](const std::shared_ptr<Peer>& peer) {
  90. return peer->getIPAddress() == externalIP &&
  91. peer->getPort() == tcpPort;
  92. }) != std::end(peers)) {
  93. return;
  94. }
  95. peers.push_back(std::make_shared<Peer>(externalIP, tcpPort));
  96. }
  97. void DHTGetPeersMessage::doReceivedAction()
  98. {
  99. std::string token = tokenTracker_->generateToken(
  100. infoHash_, getRemoteNode()->getIPAddress(), getRemoteNode()->getPort());
  101. std::vector<std::shared_ptr<Peer>> peers;
  102. peerAnnounceStorage_->getPeers(peers, infoHash_);
  103. // Check to see localhost has the contents which has same infohash
  104. addLocalPeer(peers);
  105. std::vector<std::shared_ptr<DHTNode>> nodes;
  106. getRoutingTable()->getClosestKNodes(nodes, infoHash_);
  107. getMessageDispatcher()->addMessageToQueue(
  108. getMessageFactory()->createGetPeersReplyMessage(
  109. getRemoteNode(), std::move(nodes), std::move(peers), token,
  110. getTransactionID()));
  111. }
  112. std::unique_ptr<Dict> DHTGetPeersMessage::getArgument()
  113. {
  114. auto aDict = Dict::g();
  115. aDict->put(DHTMessage::ID, String::g(getLocalNode()->getID(), DHT_ID_LENGTH));
  116. aDict->put(INFO_HASH, String::g(infoHash_, DHT_ID_LENGTH));
  117. return aDict;
  118. }
  119. const std::string& DHTGetPeersMessage::getMessageType() const
  120. {
  121. return GET_PEERS;
  122. }
  123. void DHTGetPeersMessage::setPeerAnnounceStorage(DHTPeerAnnounceStorage* storage)
  124. {
  125. peerAnnounceStorage_ = storage;
  126. }
  127. void DHTGetPeersMessage::setTokenTracker(DHTTokenTracker* tokenTracker)
  128. {
  129. tokenTracker_ = tokenTracker;
  130. }
  131. void DHTGetPeersMessage::setBtRegistry(BtRegistry* btRegistry)
  132. {
  133. btRegistry_ = btRegistry;
  134. }
  135. void DHTGetPeersMessage::setFamily(int family) { family_ = family; }
  136. std::string DHTGetPeersMessage::toStringOptional() const
  137. {
  138. return "info_hash=" + util::toHex(infoHash_, INFO_HASH_LENGTH);
  139. }
  140. } // namespace aria2