ReceiverMSEHandshakeCommand.cc 6.4 KB

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