InitiatorMSEHandshakeCommand.cc 6.7 KB

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