InitiatorMSEHandshakeCommand.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 "LogFactory.h"
  45. #include "Peer.h"
  46. #include "PeerConnection.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. #include "DownloadContext.h"
  56. #include "bittorrent_helper.h"
  57. #include "util.h"
  58. #include "fmt.h"
  59. #include "array_fun.h"
  60. namespace aria2 {
  61. InitiatorMSEHandshakeCommand::InitiatorMSEHandshakeCommand
  62. (cuid_t cuid,
  63. RequestGroup* requestGroup,
  64. const SharedHandle<Peer>& p,
  65. DownloadEngine* e,
  66. const SharedHandle<BtRuntime>& btRuntime,
  67. const SharedHandle<SocketCore>& s)
  68. : PeerAbstractCommand(cuid, p, e, s),
  69. requestGroup_(requestGroup),
  70. btRuntime_(btRuntime),
  71. sequence_(INITIATOR_SEND_KEY),
  72. mseHandshake_(new MSEHandshake(cuid, s, getOption().get()))
  73. {
  74. disableReadCheckSocket();
  75. setWriteCheckSocket(getSocket());
  76. setTimeout(getOption()->getAsInt(PREF_PEER_CONNECTION_TIMEOUT));
  77. btRuntime_->increaseConnections();
  78. requestGroup_->increaseNumCommand();
  79. }
  80. InitiatorMSEHandshakeCommand::~InitiatorMSEHandshakeCommand()
  81. {
  82. requestGroup_->decreaseNumCommand();
  83. btRuntime_->decreaseConnections();
  84. delete mseHandshake_;
  85. }
  86. bool InitiatorMSEHandshakeCommand::executeInternal() {
  87. if(mseHandshake_->getWantRead()) {
  88. mseHandshake_->read();
  89. }
  90. bool done = false;
  91. while(!done) {
  92. switch(sequence_) {
  93. case INITIATOR_SEND_KEY: {
  94. if(!getSocket()->isWritable(0)) {
  95. getDownloadEngine()->addCommand(this);
  96. return false;
  97. }
  98. setTimeout(getOption()->getAsInt(PREF_BT_TIMEOUT));
  99. mseHandshake_->initEncryptionFacility(true);
  100. mseHandshake_->sendPublicKey();
  101. sequence_ = INITIATOR_SEND_KEY_PENDING;
  102. break;
  103. }
  104. case INITIATOR_SEND_KEY_PENDING:
  105. if(mseHandshake_->send()) {
  106. sequence_ = INITIATOR_WAIT_KEY;
  107. } else {
  108. done = true;
  109. }
  110. break;
  111. case INITIATOR_WAIT_KEY: {
  112. if(mseHandshake_->receivePublicKey()) {
  113. mseHandshake_->initCipher
  114. (bittorrent::getInfoHash(requestGroup_->getDownloadContext()));;
  115. mseHandshake_->sendInitiatorStep2();
  116. sequence_ = INITIATOR_SEND_STEP2_PENDING;
  117. } else {
  118. done = true;
  119. }
  120. break;
  121. }
  122. case INITIATOR_SEND_STEP2_PENDING:
  123. if(mseHandshake_->send()) {
  124. sequence_ = INITIATOR_FIND_VC_MARKER;
  125. } else {
  126. done = true;
  127. }
  128. break;
  129. case INITIATOR_FIND_VC_MARKER: {
  130. if(mseHandshake_->findInitiatorVCMarker()) {
  131. sequence_ = INITIATOR_RECEIVE_PAD_D_LENGTH;
  132. } else {
  133. done = true;
  134. }
  135. break;
  136. }
  137. case INITIATOR_RECEIVE_PAD_D_LENGTH: {
  138. if(mseHandshake_->receiveInitiatorCryptoSelectAndPadDLength()) {
  139. sequence_ = INITIATOR_RECEIVE_PAD_D;
  140. } else {
  141. done = true;
  142. }
  143. break;
  144. }
  145. case INITIATOR_RECEIVE_PAD_D: {
  146. if(mseHandshake_->receivePad()) {
  147. SharedHandle<PeerConnection> peerConnection
  148. (new PeerConnection(getCuid(), getPeer(), getSocket()));
  149. if(mseHandshake_->getNegotiatedCryptoType() ==
  150. MSEHandshake::CRYPTO_ARC4){
  151. peerConnection->enableEncryption(mseHandshake_->getEncryptor(),
  152. mseHandshake_->getDecryptor());
  153. size_t buflen = mseHandshake_->getBufferLength();
  154. array_ptr<unsigned char> buffer(new unsigned char[buflen]);
  155. mseHandshake_->getDecryptor()->decrypt(buffer, buflen,
  156. mseHandshake_->getBuffer(),
  157. buflen);
  158. peerConnection->presetBuffer(buffer, buflen);
  159. } else {
  160. peerConnection->presetBuffer(mseHandshake_->getBuffer(),
  161. mseHandshake_->getBufferLength());
  162. }
  163. PeerInteractionCommand* c =
  164. new PeerInteractionCommand
  165. (getCuid(), requestGroup_, getPeer(), getDownloadEngine(), btRuntime_,
  166. pieceStorage_,
  167. peerStorage_,
  168. getSocket(),
  169. PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE,
  170. peerConnection);
  171. getDownloadEngine()->addCommand(c);
  172. return true;
  173. } else {
  174. done = true;
  175. }
  176. break;
  177. }
  178. }
  179. }
  180. if(mseHandshake_->getWantRead()) {
  181. setReadCheckSocket(getSocket());
  182. } else {
  183. disableReadCheckSocket();
  184. }
  185. if(mseHandshake_->getWantWrite()) {
  186. setWriteCheckSocket(getSocket());
  187. } else {
  188. disableWriteCheckSocket();
  189. }
  190. getDownloadEngine()->addCommand(this);
  191. return false;
  192. }
  193. void InitiatorMSEHandshakeCommand::tryNewPeer()
  194. {
  195. if(peerStorage_->isPeerAvailable() && btRuntime_->lessThanEqMinPeers()) {
  196. SharedHandle<Peer> peer = peerStorage_->getUnusedPeer();
  197. peer->usedBy(getDownloadEngine()->newCUID());
  198. PeerInitiateConnectionCommand* command =
  199. new PeerInitiateConnectionCommand(peer->usedBy(), requestGroup_, peer,
  200. getDownloadEngine(), btRuntime_);
  201. command->setPeerStorage(peerStorage_);
  202. command->setPieceStorage(pieceStorage_);
  203. getDownloadEngine()->addCommand(command);
  204. }
  205. }
  206. bool InitiatorMSEHandshakeCommand::prepareForNextPeer(time_t wait)
  207. {
  208. if(sequence_ == INITIATOR_SEND_KEY) {
  209. // We don't try legacy handshake when connection did not
  210. // established.
  211. tryNewPeer();
  212. return true;
  213. } else if(getOption()->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
  214. A2_LOG_INFO(fmt("CUID#%lld - Establishing connection using legacy"
  215. " BitTorrent handshake is disabled by preference.",
  216. getCuid()));
  217. tryNewPeer();
  218. return true;
  219. } else {
  220. // try legacy BitTorrent handshake
  221. A2_LOG_INFO(fmt("CUID#%lld - Retry using legacy BitTorrent handshake.",
  222. getCuid()));
  223. PeerInitiateConnectionCommand* command =
  224. new PeerInitiateConnectionCommand(getCuid(), requestGroup_, getPeer(),
  225. getDownloadEngine(), btRuntime_, false);
  226. command->setPeerStorage(peerStorage_);
  227. command->setPieceStorage(pieceStorage_);
  228. getDownloadEngine()->addCommand(command);
  229. return true;
  230. }
  231. }
  232. void InitiatorMSEHandshakeCommand::onAbort()
  233. {
  234. if(getOption()->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
  235. peerStorage_->returnPeer(getPeer());
  236. }
  237. }
  238. bool InitiatorMSEHandshakeCommand::exitBeforeExecute()
  239. {
  240. return btRuntime_->isHalt();
  241. }
  242. void InitiatorMSEHandshakeCommand::setPeerStorage
  243. (const SharedHandle<PeerStorage>& peerStorage)
  244. {
  245. peerStorage_ = peerStorage;
  246. }
  247. void InitiatorMSEHandshakeCommand::setPieceStorage
  248. (const SharedHandle<PieceStorage>& pieceStorage)
  249. {
  250. pieceStorage_ = pieceStorage;
  251. }
  252. const SharedHandle<Option>& InitiatorMSEHandshakeCommand::getOption() const
  253. {
  254. return requestGroup_->getOption();
  255. }
  256. } // namespace aria2