InitiatorMSEHandshakeCommand.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. if(_mseHandshake->sendPublicKey()) {
  93. _sequence = INITIATOR_WAIT_KEY;
  94. } else {
  95. setWriteCheckSocket(socket);
  96. _sequence = INITIATOR_SEND_KEY_PENDING;
  97. }
  98. break;
  99. }
  100. case INITIATOR_SEND_KEY_PENDING:
  101. if(_mseHandshake->sendPublicKey()) {
  102. disableWriteCheckSocket();
  103. _sequence = INITIATOR_WAIT_KEY;
  104. }
  105. break;
  106. case INITIATOR_WAIT_KEY: {
  107. if(_mseHandshake->receivePublicKey()) {
  108. _mseHandshake->initCipher(btContext->getInfoHash());
  109. if(_mseHandshake->sendInitiatorStep2()) {
  110. _sequence = INITIATOR_FIND_VC_MARKER;
  111. } else {
  112. setWriteCheckSocket(socket);
  113. _sequence = INITIATOR_SEND_STEP2_PENDING;
  114. }
  115. }
  116. break;
  117. }
  118. case INITIATOR_SEND_STEP2_PENDING:
  119. if(_mseHandshake->sendInitiatorStep2()) {
  120. disableWriteCheckSocket();
  121. _sequence = INITIATOR_FIND_VC_MARKER;
  122. }
  123. break;
  124. case INITIATOR_FIND_VC_MARKER: {
  125. if(_mseHandshake->findInitiatorVCMarker()) {
  126. _sequence = INITIATOR_RECEIVE_PAD_D_LENGTH;
  127. }
  128. break;
  129. }
  130. case INITIATOR_RECEIVE_PAD_D_LENGTH: {
  131. if(_mseHandshake->receiveInitiatorCryptoSelectAndPadDLength()) {
  132. _sequence = INITIATOR_RECEIVE_PAD_D;
  133. }
  134. break;
  135. }
  136. case INITIATOR_RECEIVE_PAD_D: {
  137. if(_mseHandshake->receivePad()) {
  138. SharedHandle<PeerConnection> peerConnection
  139. (new PeerConnection(cuid, socket, e->option));
  140. if(_mseHandshake->getNegotiatedCryptoType() == MSEHandshake::CRYPTO_ARC4) {
  141. peerConnection->enableEncryption(_mseHandshake->getEncryptor(),
  142. _mseHandshake->getDecryptor());
  143. }
  144. Command* c =
  145. new PeerInteractionCommand(cuid, _requestGroup, peer, e, btContext,
  146. socket,
  147. PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE,
  148. peerConnection);
  149. e->commands.push_back(c);
  150. return true;
  151. }
  152. break;
  153. }
  154. }
  155. e->commands.push_back(this);
  156. return false;
  157. }
  158. bool InitiatorMSEHandshakeCommand::prepareForNextPeer(time_t wait)
  159. {
  160. if(e->option->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
  161. logger->info("CUID#%d - Establishing connection using legacy BitTorrent handshake is disabled by preference.", cuid);
  162. if(peerStorage->isPeerAvailable() && btRuntime->lessThanEqMinPeers()) {
  163. SharedHandle<Peer> peer = peerStorage->getUnusedPeer();
  164. peer->usedBy(CUIDCounterSingletonHolder::instance()->newID());
  165. Command* command =
  166. new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer, e,
  167. btContext);
  168. e->commands.push_back(command);
  169. }
  170. return true;
  171. } else {
  172. // try legacy BitTorrent handshake
  173. logger->info("CUID#%d - Retry using legacy BitTorrent handshake.", cuid);
  174. Command* command =
  175. new PeerInitiateConnectionCommand(cuid, _requestGroup, peer, e, btContext,
  176. false);
  177. e->commands.push_back(command);
  178. return true;
  179. }
  180. }
  181. void InitiatorMSEHandshakeCommand::onAbort()
  182. {
  183. if(e->option->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
  184. peerStorage->returnPeer(peer);
  185. }
  186. }
  187. bool InitiatorMSEHandshakeCommand::exitBeforeExecute()
  188. {
  189. return btRuntime->isHalt();
  190. }
  191. } // namespace aria2