PeerReceiveHandshakeCommand.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "PeerReceiveHandshakeCommand.h"
  36. #include <cstring>
  37. #include "PeerConnection.h"
  38. #include "DownloadEngine.h"
  39. #include "BtHandshakeMessage.h"
  40. #include "util.h"
  41. #include "DownloadContext.h"
  42. #include "DlAbortEx.h"
  43. #include "PeerInteractionCommand.h"
  44. #include "Peer.h"
  45. #include "BtRegistry.h"
  46. #include "PeerStorage.h"
  47. #include "PieceStorage.h"
  48. #include "BtRuntime.h"
  49. #include "BtConstants.h"
  50. #include "message.h"
  51. #include "SocketCore.h"
  52. #include "Logger.h"
  53. #include "LogFactory.h"
  54. #include "prefs.h"
  55. #include "Option.h"
  56. #include "RequestGroupMan.h"
  57. #include "fmt.h"
  58. #include "RequestGroup.h"
  59. namespace aria2 {
  60. PeerReceiveHandshakeCommand::PeerReceiveHandshakeCommand(
  61. cuid_t cuid, const std::shared_ptr<Peer>& peer, DownloadEngine* e,
  62. const std::shared_ptr<SocketCore>& s,
  63. std::unique_ptr<PeerConnection> peerConnection)
  64. : PeerAbstractCommand{cuid, peer, e, s},
  65. peerConnection_{std::move(peerConnection)}
  66. {
  67. if (peerConnection_) {
  68. if (peerConnection_->getBufferLength() > 0) {
  69. setStatus(Command::STATUS_ONESHOT_REALTIME);
  70. getDownloadEngine()->setNoWait(true);
  71. }
  72. }
  73. else {
  74. peerConnection_ = make_unique<PeerConnection>(cuid, getPeer(), getSocket());
  75. }
  76. }
  77. PeerReceiveHandshakeCommand::~PeerReceiveHandshakeCommand() {}
  78. bool PeerReceiveHandshakeCommand::exitBeforeExecute()
  79. {
  80. return getDownloadEngine()->isHaltRequested() ||
  81. getDownloadEngine()->getRequestGroupMan()->downloadFinished();
  82. }
  83. bool PeerReceiveHandshakeCommand::executeInternal()
  84. {
  85. // Handle tracker's NAT-checking feature
  86. if (peerConnection_->getBufferLength() < 48) {
  87. size_t dataLength = 0;
  88. // Ignore return value. The received data is kept in
  89. // PeerConnection object because of peek = true.
  90. peerConnection_->receiveHandshake(nullptr, dataLength, true);
  91. }
  92. if (peerConnection_->getBufferLength() >= 48) {
  93. const unsigned char* data = peerConnection_->getBuffer();
  94. // check info_hash
  95. std::string infoHash(&data[28], &data[28 + INFO_HASH_LENGTH]);
  96. std::shared_ptr<DownloadContext> downloadContext =
  97. getDownloadEngine()->getBtRegistry()->getDownloadContext(infoHash);
  98. if (!downloadContext) {
  99. throw DL_ABORT_EX(
  100. fmt("Unknown info hash %s", util::toHex(infoHash).c_str()));
  101. }
  102. auto btObject = getDownloadEngine()->getBtRegistry()->get(
  103. downloadContext->getOwnerRequestGroup()->getGID());
  104. const std::shared_ptr<BtRuntime>& btRuntime = btObject->btRuntime;
  105. const std::shared_ptr<PieceStorage>& pieceStorage = btObject->pieceStorage;
  106. const std::shared_ptr<PeerStorage>& peerStorage = btObject->peerStorage;
  107. if (!btRuntime->ready()) {
  108. throw DL_ABORT_EX(
  109. fmt("Unknown info hash %s", util::toHex(infoHash).c_str()));
  110. }
  111. if (btRuntime->isHalt()) {
  112. A2_LOG_DEBUG("Info hash found but the download is over."
  113. " Dropping connection.");
  114. return true;
  115. }
  116. NetStat& stat = downloadContext->getNetStat();
  117. const int maxDownloadLimit =
  118. downloadContext->getOwnerRequestGroup()->getMaxDownloadSpeedLimit();
  119. int thresholdSpeed =
  120. downloadContext->getOwnerRequestGroup()->getOption()->getAsInt(
  121. PREF_BT_REQUEST_PEER_SPEED_LIMIT);
  122. if (maxDownloadLimit > 0) {
  123. thresholdSpeed = std::min(maxDownloadLimit, thresholdSpeed);
  124. }
  125. if ((!pieceStorage->downloadFinished() &&
  126. stat.calculateDownloadSpeed() < thresholdSpeed) ||
  127. btRuntime->lessThanMaxPeers()) {
  128. // TODO addPeer and checkoutPeer must be "atomic", in a sense
  129. // that the added peer must be checked out.
  130. if (peerStorage->addPeer(getPeer()) &&
  131. peerStorage->checkoutPeer(getCuid())) {
  132. getDownloadEngine()->addCommand(make_unique<PeerInteractionCommand>(
  133. getCuid(), downloadContext->getOwnerRequestGroup(), getPeer(),
  134. getDownloadEngine(), btRuntime, pieceStorage, peerStorage,
  135. getSocket(), PeerInteractionCommand::RECEIVER_WAIT_HANDSHAKE,
  136. std::move(peerConnection_)));
  137. A2_LOG_DEBUG(
  138. fmt(MSG_INCOMING_PEER_CONNECTION, getCuid(), getPeer()->usedBy()));
  139. }
  140. }
  141. return true;
  142. }
  143. else {
  144. addCommandSelf();
  145. return false;
  146. }
  147. }
  148. } // namespace aria2