PeerReceiveHandshakeCommand.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "PeerConnection.h"
  37. #include "DownloadEngine.h"
  38. #include "BtHandshakeMessage.h"
  39. #include "Util.h"
  40. #include "BtContext.h"
  41. #include "DlAbortEx.h"
  42. #include "PeerInteractionCommand.h"
  43. #include "Peer.h"
  44. #include "BtRegistry.h"
  45. #include "PeerStorage.h"
  46. #include "PieceStorage.h"
  47. #include "BtRuntime.h"
  48. #include "BtConstants.h"
  49. #include "message.h"
  50. #include "Socket.h"
  51. #include "Logger.h"
  52. #include "prefs.h"
  53. #include "Option.h"
  54. #include "RequestGroupMan.h"
  55. #include "StringFormat.h"
  56. namespace aria2 {
  57. PeerReceiveHandshakeCommand::PeerReceiveHandshakeCommand(int32_t cuid,
  58. const PeerHandle& peer,
  59. DownloadEngine* e,
  60. const SocketHandle& s,
  61. const SharedHandle<PeerConnection>& peerConnection):
  62. PeerAbstractCommand(cuid, peer, e, s),
  63. _peerConnection(peerConnection),
  64. _thresholdSpeed(SLOW_SPEED_THRESHOLD)
  65. {
  66. if(_peerConnection.isNull()) {
  67. _peerConnection.reset(new PeerConnection(cuid, socket, e->option));
  68. }
  69. unsigned int maxDownloadSpeed = e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT);
  70. if(maxDownloadSpeed > 0) {
  71. _thresholdSpeed = std::min(maxDownloadSpeed, _thresholdSpeed);
  72. }
  73. }
  74. PeerReceiveHandshakeCommand::~PeerReceiveHandshakeCommand() {}
  75. bool PeerReceiveHandshakeCommand::exitBeforeExecute()
  76. {
  77. return e->isHaltRequested() || e->_requestGroupMan->downloadFinished();
  78. }
  79. bool PeerReceiveHandshakeCommand::executeInternal()
  80. {
  81. unsigned char data[BtHandshakeMessage::MESSAGE_LENGTH];
  82. size_t dataLength = BtHandshakeMessage::MESSAGE_LENGTH;
  83. // ignore return value. The received data is kept in PeerConnection object
  84. // because of peek = true.
  85. _peerConnection->receiveHandshake(data, dataLength, true);
  86. // To handle tracker's NAT-checking feature
  87. if(dataLength >= 48) {
  88. // check info_hash
  89. std::string infoHash = Util::toHex(&data[28], INFO_HASH_LENGTH);
  90. BtContextHandle btContext = BtRegistry::getBtContext(infoHash);
  91. if(btContext.isNull() || !BT_RUNTIME(btContext)->ready()) {
  92. throw DlAbortEx
  93. (StringFormat("Unknown info hash %s", infoHash.c_str()).str());
  94. }
  95. TransferStat tstat = PEER_STORAGE(btContext)->calculateStat();
  96. if((!PIECE_STORAGE(btContext)->downloadFinished() &&
  97. tstat.getDownloadSpeed() < _thresholdSpeed) ||
  98. BT_RUNTIME(btContext)->getConnections() < MAX_PEERS) {
  99. if(PEER_STORAGE(btContext)->addPeer(peer)) {
  100. peer->usedBy(cuid);
  101. PeerInteractionCommand* command =
  102. new PeerInteractionCommand(cuid,
  103. btContext->getOwnerRequestGroup(),
  104. peer,
  105. e,
  106. btContext,
  107. socket,
  108. PeerInteractionCommand::RECEIVER_WAIT_HANDSHAKE,
  109. _peerConnection);
  110. e->commands.push_back(command);
  111. logger->debug(MSG_INCOMING_PEER_CONNECTION, cuid, peer->usedBy());
  112. }
  113. }
  114. return true;
  115. } else {
  116. e->commands.push_back(this);
  117. return false;
  118. }
  119. }
  120. } // namespace aria2