ReceiverMSEHandshakeCommand.cc 7.3 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 "ReceiverMSEHandshakeCommand.h"
  36. #include "PeerReceiveHandshakeCommand.h"
  37. #include "PeerConnection.h"
  38. #include "DownloadEngine.h"
  39. #include "BtHandshakeMessage.h"
  40. #include "DlAbortEx.h"
  41. #include "Peer.h"
  42. #include "message.h"
  43. #include "SocketCore.h"
  44. #include "Logger.h"
  45. #include "prefs.h"
  46. #include "Option.h"
  47. #include "MSEHandshake.h"
  48. #include "ARC4Encryptor.h"
  49. #include "RequestGroupMan.h"
  50. #include "BtRegistry.h"
  51. #include "DownloadContext.h"
  52. #include "array_fun.h"
  53. namespace aria2 {
  54. ReceiverMSEHandshakeCommand::ReceiverMSEHandshakeCommand(
  55. cuid_t cuid, const std::shared_ptr<Peer>& peer, DownloadEngine* e,
  56. const std::shared_ptr<SocketCore>& s)
  57. :
  58. PeerAbstractCommand(cuid, peer, e, s),
  59. sequence_(RECEIVER_IDENTIFY_HANDSHAKE),
  60. mseHandshake_(make_unique<MSEHandshake>(cuid, s, e->getOption()))
  61. {
  62. setTimeout(std::chrono::seconds(
  63. e->getOption()->getAsInt(PREF_PEER_CONNECTION_TIMEOUT)));
  64. mseHandshake_->setWantRead(true);
  65. }
  66. ReceiverMSEHandshakeCommand::~ReceiverMSEHandshakeCommand() {}
  67. bool ReceiverMSEHandshakeCommand::exitBeforeExecute()
  68. {
  69. return getDownloadEngine()->isHaltRequested() ||
  70. getDownloadEngine()->getRequestGroupMan()->downloadFinished();
  71. }
  72. bool ReceiverMSEHandshakeCommand::executeInternal()
  73. {
  74. if (mseHandshake_->getWantRead()) {
  75. mseHandshake_->read();
  76. }
  77. bool done = false;
  78. while (!done) {
  79. switch (sequence_) {
  80. case RECEIVER_IDENTIFY_HANDSHAKE: {
  81. MSEHandshake::HANDSHAKE_TYPE type =
  82. mseHandshake_->identifyHandshakeType();
  83. switch (type) {
  84. case MSEHandshake::HANDSHAKE_NOT_YET:
  85. done = true;
  86. break;
  87. case MSEHandshake::HANDSHAKE_ENCRYPTED:
  88. mseHandshake_->initEncryptionFacility(false);
  89. sequence_ = RECEIVER_WAIT_KEY;
  90. break;
  91. case MSEHandshake::HANDSHAKE_LEGACY: {
  92. const auto option = getDownloadEngine()->getOption();
  93. if (option->getAsBool(PREF_BT_FORCE_ENCRYPTION) ||
  94. option->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
  95. throw DL_ABORT_EX(
  96. "The legacy BitTorrent handshake is not acceptable by the"
  97. " preference.");
  98. }
  99. auto peerConnection =
  100. make_unique<PeerConnection>(getCuid(), getPeer(), getSocket());
  101. peerConnection->presetBuffer(mseHandshake_->getBuffer(),
  102. mseHandshake_->getBufferLength());
  103. getDownloadEngine()->addCommand(
  104. make_unique<PeerReceiveHandshakeCommand>(
  105. getCuid(), getPeer(), getDownloadEngine(), getSocket(),
  106. std::move(peerConnection)));
  107. return true;
  108. }
  109. default:
  110. throw DL_ABORT_EX("Not supported handshake type.");
  111. }
  112. break;
  113. }
  114. case RECEIVER_WAIT_KEY: {
  115. if (mseHandshake_->receivePublicKey()) {
  116. mseHandshake_->sendPublicKey();
  117. sequence_ = RECEIVER_SEND_KEY_PENDING;
  118. }
  119. else {
  120. done = true;
  121. }
  122. break;
  123. }
  124. case RECEIVER_SEND_KEY_PENDING:
  125. if (mseHandshake_->send()) {
  126. sequence_ = RECEIVER_FIND_HASH_MARKER;
  127. }
  128. else {
  129. done = true;
  130. }
  131. break;
  132. case RECEIVER_FIND_HASH_MARKER: {
  133. if (mseHandshake_->findReceiverHashMarker()) {
  134. sequence_ = RECEIVER_RECEIVE_PAD_C_LENGTH;
  135. }
  136. else {
  137. done = true;
  138. }
  139. break;
  140. }
  141. case RECEIVER_RECEIVE_PAD_C_LENGTH: {
  142. std::vector<std::shared_ptr<DownloadContext>> downloadContexts;
  143. getDownloadEngine()->getBtRegistry()->getAllDownloadContext(
  144. std::back_inserter(downloadContexts));
  145. if (mseHandshake_->receiveReceiverHashAndPadCLength(downloadContexts)) {
  146. sequence_ = RECEIVER_RECEIVE_PAD_C;
  147. }
  148. else {
  149. done = true;
  150. }
  151. break;
  152. }
  153. case RECEIVER_RECEIVE_PAD_C: {
  154. if (mseHandshake_->receivePad()) {
  155. sequence_ = RECEIVER_RECEIVE_IA_LENGTH;
  156. }
  157. else {
  158. done = true;
  159. }
  160. break;
  161. }
  162. case RECEIVER_RECEIVE_IA_LENGTH: {
  163. if (mseHandshake_->receiveReceiverIALength()) {
  164. sequence_ = RECEIVER_RECEIVE_IA;
  165. }
  166. else {
  167. done = true;
  168. }
  169. break;
  170. }
  171. case RECEIVER_RECEIVE_IA: {
  172. if (mseHandshake_->receiveReceiverIA()) {
  173. mseHandshake_->sendReceiverStep2();
  174. sequence_ = RECEIVER_SEND_STEP2_PENDING;
  175. }
  176. else {
  177. done = true;
  178. }
  179. break;
  180. }
  181. case RECEIVER_SEND_STEP2_PENDING:
  182. if (mseHandshake_->send()) {
  183. createCommand();
  184. return true;
  185. }
  186. else {
  187. done = true;
  188. }
  189. break;
  190. }
  191. }
  192. if (mseHandshake_->getWantRead()) {
  193. setReadCheckSocket(getSocket());
  194. }
  195. else {
  196. disableReadCheckSocket();
  197. }
  198. if (mseHandshake_->getWantWrite()) {
  199. setWriteCheckSocket(getSocket());
  200. }
  201. else {
  202. disableWriteCheckSocket();
  203. }
  204. addCommandSelf();
  205. return false;
  206. }
  207. void ReceiverMSEHandshakeCommand::createCommand()
  208. {
  209. auto peerConnection =
  210. make_unique<PeerConnection>(getCuid(), getPeer(), getSocket());
  211. if (mseHandshake_->getNegotiatedCryptoType() == MSEHandshake::CRYPTO_ARC4) {
  212. peerConnection->enableEncryption(mseHandshake_->popEncryptor(),
  213. mseHandshake_->popDecryptor());
  214. }
  215. // Since initiator cannot send payload stream before reading step2
  216. // from receiver, mseHandshake_->getBufferLength() should be 0.
  217. peerConnection->presetBuffer(mseHandshake_->getIA(),
  218. mseHandshake_->getIALength());
  219. // TODO add mseHandshake_->getInfoHash() to PeerReceiveHandshakeCommand
  220. // as a hint. If this info hash and one in BitTorrent Handshake does not
  221. // match, then drop connection.
  222. getDownloadEngine()->addCommand(make_unique<PeerReceiveHandshakeCommand>(
  223. getCuid(), getPeer(), getDownloadEngine(), getSocket(),
  224. std::move(peerConnection)));
  225. }
  226. } // namespace aria2