InitiatorMSEHandshakeCommand.cc 8.6 KB

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