InitiatorMSEHandshakeCommand.cc 7.6 KB

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