InitiatorMSEHandshakeCommand.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #include "util.h"
  57. namespace aria2 {
  58. InitiatorMSEHandshakeCommand::InitiatorMSEHandshakeCommand
  59. (cuid_t cuid,
  60. RequestGroup* requestGroup,
  61. const SharedHandle<Peer>& p,
  62. DownloadEngine* e,
  63. const SharedHandle<BtRuntime>& btRuntime,
  64. const SharedHandle<SocketCore>& s):
  65. PeerAbstractCommand(cuid, p, e, s),
  66. _requestGroup(requestGroup),
  67. _btRuntime(btRuntime),
  68. _sequence(INITIATOR_SEND_KEY),
  69. _mseHandshake(new MSEHandshake(cuid, socket, getOption().get()))
  70. {
  71. disableReadCheckSocket();
  72. setWriteCheckSocket(socket);
  73. setTimeout(getOption()->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(getOption()->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
  111. (bittorrent::getInfoHash(_requestGroup->getDownloadContext()));;
  112. if(_mseHandshake->sendInitiatorStep2()) {
  113. _sequence = INITIATOR_FIND_VC_MARKER;
  114. } else {
  115. setWriteCheckSocket(socket);
  116. _sequence = INITIATOR_SEND_STEP2_PENDING;
  117. }
  118. }
  119. break;
  120. }
  121. case INITIATOR_SEND_STEP2_PENDING:
  122. if(_mseHandshake->sendInitiatorStep2()) {
  123. disableWriteCheckSocket();
  124. _sequence = INITIATOR_FIND_VC_MARKER;
  125. }
  126. break;
  127. case INITIATOR_FIND_VC_MARKER: {
  128. if(_mseHandshake->findInitiatorVCMarker()) {
  129. _sequence = INITIATOR_RECEIVE_PAD_D_LENGTH;
  130. }
  131. break;
  132. }
  133. case INITIATOR_RECEIVE_PAD_D_LENGTH: {
  134. if(_mseHandshake->receiveInitiatorCryptoSelectAndPadDLength()) {
  135. _sequence = INITIATOR_RECEIVE_PAD_D;
  136. }
  137. break;
  138. }
  139. case INITIATOR_RECEIVE_PAD_D: {
  140. if(_mseHandshake->receivePad()) {
  141. SharedHandle<PeerConnection> peerConnection
  142. (new PeerConnection(cuid, socket));
  143. if(_mseHandshake->getNegotiatedCryptoType() == MSEHandshake::CRYPTO_ARC4) {
  144. peerConnection->enableEncryption(_mseHandshake->getEncryptor(),
  145. _mseHandshake->getDecryptor());
  146. }
  147. PeerInteractionCommand* c =
  148. new PeerInteractionCommand
  149. (cuid, _requestGroup, peer, e, _btRuntime, _pieceStorage, _peerStorage,
  150. socket,
  151. PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE,
  152. peerConnection);
  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(getOption()->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
  165. if(logger->info()) {
  166. logger->info("CUID#%s - Establishing connection using legacy BitTorrent"
  167. " handshake is disabled by preference.",
  168. util::itos(cuid).c_str());
  169. }
  170. if(_peerStorage->isPeerAvailable() && _btRuntime->lessThanEqMinPeers()) {
  171. SharedHandle<Peer> peer = _peerStorage->getUnusedPeer();
  172. peer->usedBy(e->newCUID());
  173. PeerInitiateConnectionCommand* command =
  174. new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer,
  175. e, _btRuntime);
  176. command->setPeerStorage(_peerStorage);
  177. command->setPieceStorage(_pieceStorage);
  178. e->commands.push_back(command);
  179. }
  180. return true;
  181. } else {
  182. // try legacy BitTorrent handshake
  183. if(logger->info()) {
  184. logger->info("CUID#%s - Retry using legacy BitTorrent handshake.",
  185. util::itos(cuid).c_str());
  186. }
  187. PeerInitiateConnectionCommand* command =
  188. new PeerInitiateConnectionCommand(cuid, _requestGroup, peer, e,
  189. _btRuntime, false);
  190. command->setPeerStorage(_peerStorage);
  191. command->setPieceStorage(_pieceStorage);
  192. e->commands.push_back(command);
  193. return true;
  194. }
  195. }
  196. void InitiatorMSEHandshakeCommand::onAbort()
  197. {
  198. if(getOption()->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
  199. _peerStorage->returnPeer(peer);
  200. }
  201. }
  202. bool InitiatorMSEHandshakeCommand::exitBeforeExecute()
  203. {
  204. return _btRuntime->isHalt();
  205. }
  206. void InitiatorMSEHandshakeCommand::setPeerStorage
  207. (const SharedHandle<PeerStorage>& peerStorage)
  208. {
  209. _peerStorage = peerStorage;
  210. }
  211. void InitiatorMSEHandshakeCommand::setPieceStorage
  212. (const SharedHandle<PieceStorage>& pieceStorage)
  213. {
  214. _pieceStorage = pieceStorage;
  215. }
  216. const SharedHandle<Option>& InitiatorMSEHandshakeCommand::getOption() const
  217. {
  218. return _requestGroup->getOption();
  219. }
  220. } // namespace aria2