ReceiverMSEHandshakeCommand.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "PeerStorage.h"
  54. #include "PieceStorage.h"
  55. #include "BtAnnounce.h"
  56. #include "BtRuntime.h"
  57. #include "BtProgressInfoFile.h"
  58. #include "ServerStatMan.h"
  59. #include "FileAllocationEntry.h"
  60. #include "CheckIntegrityEntry.h"
  61. namespace aria2 {
  62. ReceiverMSEHandshakeCommand::ReceiverMSEHandshakeCommand
  63. (cuid_t cuid,
  64. const SharedHandle<Peer>& peer,
  65. DownloadEngine* e,
  66. const SharedHandle<SocketCore>& s):
  67. PeerAbstractCommand(cuid, peer, e, s),
  68. _sequence(RECEIVER_IDENTIFY_HANDSHAKE),
  69. _mseHandshake(new MSEHandshake(cuid, s, e->getOption()))
  70. {
  71. setTimeout(e->getOption()->getAsInt(PREF_PEER_CONNECTION_TIMEOUT));
  72. }
  73. ReceiverMSEHandshakeCommand::~ReceiverMSEHandshakeCommand()
  74. {
  75. delete _mseHandshake;
  76. }
  77. bool ReceiverMSEHandshakeCommand::exitBeforeExecute()
  78. {
  79. return getDownloadEngine()->isHaltRequested() ||
  80. getDownloadEngine()->getRequestGroupMan()->downloadFinished();
  81. }
  82. bool ReceiverMSEHandshakeCommand::executeInternal()
  83. {
  84. switch(_sequence) {
  85. case RECEIVER_IDENTIFY_HANDSHAKE: {
  86. MSEHandshake::HANDSHAKE_TYPE type = _mseHandshake->identifyHandshakeType();
  87. switch(type) {
  88. case MSEHandshake::HANDSHAKE_NOT_YET:
  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. SharedHandle<PeerConnection> peerConnection
  101. (new PeerConnection(getCuid(), getSocket()));
  102. peerConnection->presetBuffer(_mseHandshake->getBuffer(),
  103. _mseHandshake->getBufferLength());
  104. Command* c = new PeerReceiveHandshakeCommand(getCuid(),
  105. getPeer(),
  106. getDownloadEngine(),
  107. getSocket(),
  108. peerConnection);
  109. getDownloadEngine()->addCommand(c);
  110. return true;
  111. }
  112. default:
  113. throw DL_ABORT_EX("Not supported handshake type.");
  114. }
  115. break;
  116. }
  117. case RECEIVER_WAIT_KEY: {
  118. if(_mseHandshake->receivePublicKey()) {
  119. if(_mseHandshake->sendPublicKey()) {
  120. _sequence = RECEIVER_FIND_HASH_MARKER;
  121. } else {
  122. setWriteCheckSocket(getSocket());
  123. _sequence = RECEIVER_SEND_KEY_PENDING;
  124. }
  125. }
  126. break;
  127. }
  128. case RECEIVER_SEND_KEY_PENDING:
  129. if(_mseHandshake->sendPublicKey()) {
  130. disableWriteCheckSocket();
  131. _sequence = RECEIVER_FIND_HASH_MARKER;
  132. }
  133. break;
  134. case RECEIVER_FIND_HASH_MARKER: {
  135. if(_mseHandshake->findReceiverHashMarker()) {
  136. _sequence = RECEIVER_RECEIVE_PAD_C_LENGTH;
  137. }
  138. break;
  139. }
  140. case RECEIVER_RECEIVE_PAD_C_LENGTH: {
  141. std::vector<SharedHandle<DownloadContext> > downloadContexts;
  142. getDownloadEngine()->getBtRegistry()->getAllDownloadContext
  143. (std::back_inserter(downloadContexts));
  144. if(_mseHandshake->receiveReceiverHashAndPadCLength(downloadContexts)) {
  145. _sequence = RECEIVER_RECEIVE_PAD_C;
  146. }
  147. break;
  148. }
  149. case RECEIVER_RECEIVE_PAD_C: {
  150. if(_mseHandshake->receivePad()) {
  151. _sequence = RECEIVER_RECEIVE_IA_LENGTH;
  152. }
  153. break;
  154. }
  155. case RECEIVER_RECEIVE_IA_LENGTH: {
  156. if(_mseHandshake->receiveReceiverIALength()) {
  157. _sequence = RECEIVER_RECEIVE_IA;
  158. }
  159. break;
  160. }
  161. case RECEIVER_RECEIVE_IA: {
  162. if(_mseHandshake->receiveReceiverIA()) {
  163. if(_mseHandshake->sendReceiverStep2()) {
  164. createCommand();
  165. return true;
  166. } else {
  167. setWriteCheckSocket(getSocket());
  168. _sequence = RECEIVER_SEND_STEP2_PENDING;
  169. }
  170. }
  171. break;
  172. }
  173. case RECEIVER_SEND_STEP2_PENDING:
  174. if(_mseHandshake->sendReceiverStep2()) {
  175. disableWriteCheckSocket();
  176. createCommand();
  177. return true;
  178. }
  179. break;
  180. }
  181. getDownloadEngine()->addCommand(this);
  182. return false;
  183. }
  184. void ReceiverMSEHandshakeCommand::createCommand()
  185. {
  186. SharedHandle<PeerConnection> peerConnection
  187. (new PeerConnection(getCuid(), getSocket()));
  188. if(_mseHandshake->getNegotiatedCryptoType() == MSEHandshake::CRYPTO_ARC4) {
  189. peerConnection->enableEncryption(_mseHandshake->getEncryptor(),
  190. _mseHandshake->getDecryptor());
  191. }
  192. if(_mseHandshake->getIALength() > 0) {
  193. peerConnection->presetBuffer(_mseHandshake->getIA(),
  194. _mseHandshake->getIALength());
  195. }
  196. // TODO add _mseHandshake->getInfoHash() to PeerReceiveHandshakeCommand
  197. // as a hint. If this info hash and one in BitTorrent Handshake does not
  198. // match, then drop connection.
  199. Command* c =
  200. new PeerReceiveHandshakeCommand(getCuid(), getPeer(), getDownloadEngine(),
  201. getSocket(), peerConnection);
  202. getDownloadEngine()->addCommand(c);
  203. }
  204. } // namespace aria2