ReceiverMSEHandshakeCommand.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #include "array_fun.h"
  54. namespace aria2 {
  55. ReceiverMSEHandshakeCommand::ReceiverMSEHandshakeCommand
  56. (cuid_t cuid,
  57. const SharedHandle<Peer>& peer,
  58. DownloadEngine* e,
  59. const SharedHandle<SocketCore>& s):
  60. PeerAbstractCommand(cuid, peer, e, s),
  61. sequence_(RECEIVER_IDENTIFY_HANDSHAKE),
  62. mseHandshake_(new MSEHandshake(cuid, s, e->getOption()))
  63. {
  64. setTimeout(e->getOption()->getAsInt(PREF_PEER_CONNECTION_TIMEOUT));
  65. mseHandshake_->setWantRead(true);
  66. }
  67. ReceiverMSEHandshakeCommand::~ReceiverMSEHandshakeCommand()
  68. {
  69. delete mseHandshake_;
  70. }
  71. bool ReceiverMSEHandshakeCommand::exitBeforeExecute()
  72. {
  73. return getDownloadEngine()->isHaltRequested() ||
  74. getDownloadEngine()->getRequestGroupMan()->downloadFinished();
  75. }
  76. bool ReceiverMSEHandshakeCommand::executeInternal()
  77. {
  78. if(mseHandshake_->getWantRead()) {
  79. mseHandshake_->read();
  80. }
  81. bool done = false;
  82. while(!done) {
  83. switch(sequence_) {
  84. case RECEIVER_IDENTIFY_HANDSHAKE: {
  85. MSEHandshake::HANDSHAKE_TYPE type =
  86. mseHandshake_->identifyHandshakeType();
  87. switch(type) {
  88. case MSEHandshake::HANDSHAKE_NOT_YET:
  89. done = true;
  90. break;
  91. case MSEHandshake::HANDSHAKE_ENCRYPTED:
  92. mseHandshake_->initEncryptionFacility(false);
  93. sequence_ = RECEIVER_WAIT_KEY;
  94. break;
  95. case MSEHandshake::HANDSHAKE_LEGACY: {
  96. if(getDownloadEngine()->getOption()->getAsBool(PREF_BT_REQUIRE_CRYPTO)){
  97. throw DL_ABORT_EX
  98. ("The legacy BitTorrent handshake is not acceptable by the"
  99. " preference.");
  100. }
  101. SharedHandle<PeerConnection> peerConnection
  102. (new PeerConnection(getCuid(), getPeer(), getSocket()));
  103. peerConnection->presetBuffer(mseHandshake_->getBuffer(),
  104. mseHandshake_->getBufferLength());
  105. Command* c = new PeerReceiveHandshakeCommand(getCuid(),
  106. getPeer(),
  107. getDownloadEngine(),
  108. getSocket(),
  109. peerConnection);
  110. getDownloadEngine()->addCommand(c);
  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<SharedHandle<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. getDownloadEngine()->addCommand(this);
  199. return false;
  200. }
  201. void ReceiverMSEHandshakeCommand::createCommand()
  202. {
  203. SharedHandle<PeerConnection> peerConnection
  204. (new PeerConnection(getCuid(), getPeer(), getSocket()));
  205. if(mseHandshake_->getNegotiatedCryptoType() == MSEHandshake::CRYPTO_ARC4) {
  206. peerConnection->enableEncryption(mseHandshake_->getEncryptor(),
  207. mseHandshake_->getDecryptor());
  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. Command* c =
  217. new PeerReceiveHandshakeCommand(getCuid(), getPeer(), getDownloadEngine(),
  218. getSocket(), peerConnection);
  219. getDownloadEngine()->addCommand(c);
  220. }
  221. } // namespace aria2