InitiatorMSEHandshakeCommand.cc 6.8 KB

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