InitiatorMSEHandshakeCommand.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "CUIDCounter.h"
  43. #include "Socket.h"
  44. #include "Logger.h"
  45. #include "Peer.h"
  46. #include "PeerConnection.h"
  47. #include "BtContext.h"
  48. #include "BtRuntime.h"
  49. #include "PieceStorage.h"
  50. #include "PeerStorage.h"
  51. #include "BtAnnounce.h"
  52. #include "BtProgressInfoFile.h"
  53. #include "Option.h"
  54. #include "MSEHandshake.h"
  55. #include "ARC4Encryptor.h"
  56. #include "ARC4Decryptor.h"
  57. namespace aria2 {
  58. InitiatorMSEHandshakeCommand::InitiatorMSEHandshakeCommand
  59. (int32_t cuid,
  60. RequestGroup* requestGroup,
  61. const SharedHandle<Peer>& p,
  62. DownloadEngine* e,
  63. const SharedHandle<BtContext>& btContext,
  64. const SharedHandle<SocketCore>& s):
  65. PeerAbstractCommand(cuid, p, e, s),
  66. BtContextAwareCommand(btContext),
  67. RequestGroupAware(requestGroup),
  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. }
  76. InitiatorMSEHandshakeCommand::~InitiatorMSEHandshakeCommand()
  77. {
  78. btRuntime->decreaseConnections();
  79. delete _mseHandshake;
  80. }
  81. bool InitiatorMSEHandshakeCommand::executeInternal() {
  82. switch(_sequence) {
  83. case INITIATOR_SEND_KEY: {
  84. if(!socket->isWritable(0)) {
  85. break;
  86. }
  87. disableWriteCheckSocket();
  88. setReadCheckSocket(socket);
  89. socket->setBlockingMode();
  90. setTimeout(e->option->getAsInt(PREF_BT_TIMEOUT));
  91. _mseHandshake->initEncryptionFacility(true);
  92. _mseHandshake->sendPublicKey();
  93. _sequence = INITIATOR_WAIT_KEY;
  94. break;
  95. }
  96. case INITIATOR_WAIT_KEY: {
  97. if(_mseHandshake->receivePublicKey()) {
  98. _mseHandshake->initCipher(btContext->getInfoHash());
  99. _mseHandshake->sendInitiatorStep2();
  100. _sequence = INITIATOR_FIND_VC_MARKER;
  101. }
  102. break;
  103. }
  104. case INITIATOR_FIND_VC_MARKER: {
  105. if(_mseHandshake->findInitiatorVCMarker()) {
  106. _sequence = INITIATOR_RECEIVE_PAD_D_LENGTH;
  107. }
  108. break;
  109. }
  110. case INITIATOR_RECEIVE_PAD_D_LENGTH: {
  111. if(_mseHandshake->receiveInitiatorCryptoSelectAndPadDLength()) {
  112. _sequence = INITIATOR_RECEIVE_PAD_D;
  113. }
  114. break;
  115. }
  116. case INITIATOR_RECEIVE_PAD_D: {
  117. if(_mseHandshake->receivePad()) {
  118. SharedHandle<PeerConnection> peerConnection =
  119. new PeerConnection(cuid, socket, e->option);
  120. if(_mseHandshake->getNegotiatedCryptoType() == MSEHandshake::CRYPTO_ARC4) {
  121. peerConnection->enableEncryption(_mseHandshake->getEncryptor(),
  122. _mseHandshake->getDecryptor());
  123. }
  124. Command* c =
  125. new PeerInteractionCommand(cuid, _requestGroup, peer, e, btContext,
  126. socket,
  127. PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE,
  128. peerConnection);
  129. e->commands.push_back(c);
  130. return true;
  131. }
  132. break;
  133. }
  134. }
  135. e->commands.push_back(this);
  136. return false;
  137. }
  138. bool InitiatorMSEHandshakeCommand::prepareForNextPeer(int32_t wait)
  139. {
  140. // try legacy BitTorrent handshake if preference allows it.
  141. // TODO preference is not created yet.
  142. logger->info("CUID#%d - Retry using legacy BitTorrent handshake.", cuid);
  143. Command* command =
  144. new PeerInitiateConnectionCommand(cuid, _requestGroup, peer, e, btContext,
  145. false);
  146. e->commands.push_back(command);
  147. return true;
  148. }
  149. void InitiatorMSEHandshakeCommand::onAbort(Exception* ex) {}
  150. bool InitiatorMSEHandshakeCommand::exitBeforeExecute()
  151. {
  152. return btRuntime->isHalt();
  153. }
  154. } // namespace aria2