ReceiverMSEHandshakeCommand.cc 7.2 KB

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