InitiatorMSEHandshakeCommand.cc 7.1 KB

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