InitiatorMSEHandshakeCommand.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "InitiatorMSEHandshakeCommand.h"
  36. #include "PeerInitiateConnectionCommand.h"
  37. #include "PeerInteractionCommand.h"
  38. #include "DownloadEngine.h"
  39. #include "DlAbortEx.h"
  40. #include "message.h"
  41. #include "prefs.h"
  42. #include "Socket.h"
  43. #include "Logger.h"
  44. #include "LogFactory.h"
  45. #include "Peer.h"
  46. #include "PeerConnection.h"
  47. #include "BtRuntime.h"
  48. #include "PeerStorage.h"
  49. #include "PieceStorage.h"
  50. #include "Option.h"
  51. #include "MSEHandshake.h"
  52. #include "ARC4Encryptor.h"
  53. #include "ARC4Decryptor.h"
  54. #include "RequestGroup.h"
  55. #include "DownloadContext.h"
  56. #include "bittorrent_helper.h"
  57. #include "util.h"
  58. #include "fmt.h"
  59. namespace aria2 {
  60. InitiatorMSEHandshakeCommand::InitiatorMSEHandshakeCommand
  61. (cuid_t cuid,
  62. RequestGroup* requestGroup,
  63. const SharedHandle<Peer>& p,
  64. DownloadEngine* e,
  65. const SharedHandle<BtRuntime>& btRuntime,
  66. const SharedHandle<SocketCore>& s)
  67. : PeerAbstractCommand(cuid, p, e, s),
  68. requestGroup_(requestGroup),
  69. btRuntime_(btRuntime),
  70. sequence_(INITIATOR_SEND_KEY),
  71. mseHandshake_(new MSEHandshake(cuid, s, getOption().get()))
  72. {
  73. disableReadCheckSocket();
  74. setWriteCheckSocket(getSocket());
  75. setTimeout(getOption()->getAsInt(PREF_PEER_CONNECTION_TIMEOUT));
  76. btRuntime_->increaseConnections();
  77. requestGroup_->increaseNumCommand();
  78. }
  79. InitiatorMSEHandshakeCommand::~InitiatorMSEHandshakeCommand()
  80. {
  81. requestGroup_->decreaseNumCommand();
  82. btRuntime_->decreaseConnections();
  83. delete mseHandshake_;
  84. }
  85. bool InitiatorMSEHandshakeCommand::executeInternal() {
  86. switch(sequence_) {
  87. case INITIATOR_SEND_KEY: {
  88. if(!getSocket()->isWritable(0)) {
  89. break;
  90. }
  91. disableWriteCheckSocket();
  92. setReadCheckSocket(getSocket());
  93. //socket->setBlockingMode();
  94. setTimeout(getOption()->getAsInt(PREF_BT_TIMEOUT));
  95. mseHandshake_->initEncryptionFacility(true);
  96. if(mseHandshake_->sendPublicKey()) {
  97. sequence_ = INITIATOR_WAIT_KEY;
  98. } else {
  99. setWriteCheckSocket(getSocket());
  100. sequence_ = INITIATOR_SEND_KEY_PENDING;
  101. }
  102. break;
  103. }
  104. case INITIATOR_SEND_KEY_PENDING:
  105. if(mseHandshake_->sendPublicKey()) {
  106. disableWriteCheckSocket();
  107. sequence_ = INITIATOR_WAIT_KEY;
  108. }
  109. break;
  110. case INITIATOR_WAIT_KEY: {
  111. if(mseHandshake_->receivePublicKey()) {
  112. mseHandshake_->initCipher
  113. (bittorrent::getInfoHash(requestGroup_->getDownloadContext()));;
  114. if(mseHandshake_->sendInitiatorStep2()) {
  115. sequence_ = INITIATOR_FIND_VC_MARKER;
  116. } else {
  117. setWriteCheckSocket(getSocket());
  118. sequence_ = INITIATOR_SEND_STEP2_PENDING;
  119. }
  120. }
  121. break;
  122. }
  123. case INITIATOR_SEND_STEP2_PENDING:
  124. if(mseHandshake_->sendInitiatorStep2()) {
  125. disableWriteCheckSocket();
  126. sequence_ = INITIATOR_FIND_VC_MARKER;
  127. }
  128. break;
  129. case INITIATOR_FIND_VC_MARKER: {
  130. if(mseHandshake_->findInitiatorVCMarker()) {
  131. sequence_ = INITIATOR_RECEIVE_PAD_D_LENGTH;
  132. }
  133. break;
  134. }
  135. case INITIATOR_RECEIVE_PAD_D_LENGTH: {
  136. if(mseHandshake_->receiveInitiatorCryptoSelectAndPadDLength()) {
  137. sequence_ = INITIATOR_RECEIVE_PAD_D;
  138. }
  139. break;
  140. }
  141. case INITIATOR_RECEIVE_PAD_D: {
  142. if(mseHandshake_->receivePad()) {
  143. SharedHandle<PeerConnection> peerConnection
  144. (new PeerConnection(getCuid(), getPeer(), getSocket()));
  145. if(mseHandshake_->getNegotiatedCryptoType() == MSEHandshake::CRYPTO_ARC4){
  146. peerConnection->enableEncryption(mseHandshake_->getEncryptor(),
  147. mseHandshake_->getDecryptor());
  148. }
  149. PeerInteractionCommand* c =
  150. new PeerInteractionCommand
  151. (getCuid(), requestGroup_, getPeer(), getDownloadEngine(), btRuntime_,
  152. pieceStorage_,
  153. peerStorage_,
  154. getSocket(),
  155. PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE,
  156. peerConnection);
  157. getDownloadEngine()->addCommand(c);
  158. return true;
  159. }
  160. break;
  161. }
  162. }
  163. getDownloadEngine()->addCommand(this);
  164. return false;
  165. }
  166. bool InitiatorMSEHandshakeCommand::prepareForNextPeer(time_t wait)
  167. {
  168. if(getOption()->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
  169. A2_LOG_INFO(fmt("CUID#%lld - Establishing connection using legacy BitTorrent"
  170. " handshake is disabled by preference.",
  171. getCuid()));
  172. if(peerStorage_->isPeerAvailable() && btRuntime_->lessThanEqMinPeers()) {
  173. SharedHandle<Peer> peer = peerStorage_->getUnusedPeer();
  174. peer->usedBy(getDownloadEngine()->newCUID());
  175. PeerInitiateConnectionCommand* command =
  176. new PeerInitiateConnectionCommand(peer->usedBy(), requestGroup_, peer,
  177. getDownloadEngine(), btRuntime_);
  178. command->setPeerStorage(peerStorage_);
  179. command->setPieceStorage(pieceStorage_);
  180. getDownloadEngine()->addCommand(command);
  181. }
  182. return true;
  183. } else {
  184. // try legacy BitTorrent handshake
  185. A2_LOG_INFO(fmt("CUID#%lld - Retry using legacy BitTorrent handshake.",
  186. getCuid()));
  187. PeerInitiateConnectionCommand* command =
  188. new PeerInitiateConnectionCommand(getCuid(), requestGroup_, getPeer(),
  189. getDownloadEngine(), btRuntime_, false);
  190. command->setPeerStorage(peerStorage_);
  191. command->setPieceStorage(pieceStorage_);
  192. getDownloadEngine()->addCommand(command);
  193. return true;
  194. }
  195. }
  196. void InitiatorMSEHandshakeCommand::onAbort()
  197. {
  198. if(getOption()->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
  199. peerStorage_->returnPeer(getPeer());
  200. }
  201. }
  202. bool InitiatorMSEHandshakeCommand::exitBeforeExecute()
  203. {
  204. return btRuntime_->isHalt();
  205. }
  206. void InitiatorMSEHandshakeCommand::setPeerStorage
  207. (const SharedHandle<PeerStorage>& peerStorage)
  208. {
  209. peerStorage_ = peerStorage;
  210. }
  211. void InitiatorMSEHandshakeCommand::setPieceStorage
  212. (const SharedHandle<PieceStorage>& pieceStorage)
  213. {
  214. pieceStorage_ = pieceStorage;
  215. }
  216. const SharedHandle<Option>& InitiatorMSEHandshakeCommand::getOption() const
  217. {
  218. return requestGroup_->getOption();
  219. }
  220. } // namespace aria2