ReceiverMSEHandshakeCommand.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "Socket.h"
  44. #include "Logger.h"
  45. #include "prefs.h"
  46. #include "Option.h"
  47. #include "MSEHandshake.h"
  48. #include "ARC4Encryptor.h"
  49. #include "ARC4Decryptor.h"
  50. #include "RequestGroupMan.h"
  51. #include "BtRegistry.h"
  52. #include "DownloadContext.h"
  53. namespace aria2 {
  54. ReceiverMSEHandshakeCommand::ReceiverMSEHandshakeCommand
  55. (cuid_t cuid,
  56. const SharedHandle<Peer>& peer,
  57. DownloadEngine* e,
  58. const SharedHandle<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. }
  65. ReceiverMSEHandshakeCommand::~ReceiverMSEHandshakeCommand()
  66. {
  67. delete mseHandshake_;
  68. }
  69. bool ReceiverMSEHandshakeCommand::exitBeforeExecute()
  70. {
  71. return getDownloadEngine()->isHaltRequested() ||
  72. getDownloadEngine()->getRequestGroupMan()->downloadFinished();
  73. }
  74. bool ReceiverMSEHandshakeCommand::executeInternal()
  75. {
  76. switch(sequence_) {
  77. case RECEIVER_IDENTIFY_HANDSHAKE: {
  78. MSEHandshake::HANDSHAKE_TYPE type = mseHandshake_->identifyHandshakeType();
  79. switch(type) {
  80. case MSEHandshake::HANDSHAKE_NOT_YET:
  81. break;
  82. case MSEHandshake::HANDSHAKE_ENCRYPTED:
  83. mseHandshake_->initEncryptionFacility(false);
  84. sequence_ = RECEIVER_WAIT_KEY;
  85. break;
  86. case MSEHandshake::HANDSHAKE_LEGACY: {
  87. if(getDownloadEngine()->getOption()->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
  88. throw DL_ABORT_EX
  89. ("The legacy BitTorrent handshake is not acceptable by the"
  90. " preference.");
  91. }
  92. SharedHandle<PeerConnection> peerConnection
  93. (new PeerConnection(getCuid(), getPeer(), getSocket()));
  94. peerConnection->presetBuffer(mseHandshake_->getBuffer(),
  95. mseHandshake_->getBufferLength());
  96. Command* c = new PeerReceiveHandshakeCommand(getCuid(),
  97. getPeer(),
  98. getDownloadEngine(),
  99. getSocket(),
  100. peerConnection);
  101. getDownloadEngine()->addCommand(c);
  102. return true;
  103. }
  104. default:
  105. throw DL_ABORT_EX("Not supported handshake type.");
  106. }
  107. break;
  108. }
  109. case RECEIVER_WAIT_KEY: {
  110. if(mseHandshake_->receivePublicKey()) {
  111. if(mseHandshake_->sendPublicKey()) {
  112. sequence_ = RECEIVER_FIND_HASH_MARKER;
  113. } else {
  114. setWriteCheckSocket(getSocket());
  115. sequence_ = RECEIVER_SEND_KEY_PENDING;
  116. }
  117. }
  118. break;
  119. }
  120. case RECEIVER_SEND_KEY_PENDING:
  121. if(mseHandshake_->sendPublicKey()) {
  122. disableWriteCheckSocket();
  123. sequence_ = RECEIVER_FIND_HASH_MARKER;
  124. }
  125. break;
  126. case RECEIVER_FIND_HASH_MARKER: {
  127. if(mseHandshake_->findReceiverHashMarker()) {
  128. sequence_ = RECEIVER_RECEIVE_PAD_C_LENGTH;
  129. }
  130. break;
  131. }
  132. case RECEIVER_RECEIVE_PAD_C_LENGTH: {
  133. std::vector<SharedHandle<DownloadContext> > downloadContexts;
  134. getDownloadEngine()->getBtRegistry()->getAllDownloadContext
  135. (std::back_inserter(downloadContexts));
  136. if(mseHandshake_->receiveReceiverHashAndPadCLength(downloadContexts)) {
  137. sequence_ = RECEIVER_RECEIVE_PAD_C;
  138. }
  139. break;
  140. }
  141. case RECEIVER_RECEIVE_PAD_C: {
  142. if(mseHandshake_->receivePad()) {
  143. sequence_ = RECEIVER_RECEIVE_IA_LENGTH;
  144. }
  145. break;
  146. }
  147. case RECEIVER_RECEIVE_IA_LENGTH: {
  148. if(mseHandshake_->receiveReceiverIALength()) {
  149. sequence_ = RECEIVER_RECEIVE_IA;
  150. }
  151. break;
  152. }
  153. case RECEIVER_RECEIVE_IA: {
  154. if(mseHandshake_->receiveReceiverIA()) {
  155. if(mseHandshake_->sendReceiverStep2()) {
  156. createCommand();
  157. return true;
  158. } else {
  159. setWriteCheckSocket(getSocket());
  160. sequence_ = RECEIVER_SEND_STEP2_PENDING;
  161. }
  162. }
  163. break;
  164. }
  165. case RECEIVER_SEND_STEP2_PENDING:
  166. if(mseHandshake_->sendReceiverStep2()) {
  167. disableWriteCheckSocket();
  168. createCommand();
  169. return true;
  170. }
  171. break;
  172. }
  173. getDownloadEngine()->addCommand(this);
  174. return false;
  175. }
  176. void ReceiverMSEHandshakeCommand::createCommand()
  177. {
  178. SharedHandle<PeerConnection> peerConnection
  179. (new PeerConnection(getCuid(), getPeer(), getSocket()));
  180. if(mseHandshake_->getNegotiatedCryptoType() == MSEHandshake::CRYPTO_ARC4) {
  181. peerConnection->enableEncryption(mseHandshake_->getEncryptor(),
  182. mseHandshake_->getDecryptor());
  183. }
  184. if(mseHandshake_->getIALength() > 0) {
  185. peerConnection->presetBuffer(mseHandshake_->getIA(),
  186. mseHandshake_->getIALength());
  187. }
  188. // TODO add mseHandshake_->getInfoHash() to PeerReceiveHandshakeCommand
  189. // as a hint. If this info hash and one in BitTorrent Handshake does not
  190. // match, then drop connection.
  191. Command* c =
  192. new PeerReceiveHandshakeCommand(getCuid(), getPeer(), getDownloadEngine(),
  193. getSocket(), peerConnection);
  194. getDownloadEngine()->addCommand(c);
  195. }
  196. } // namespace aria2