DefaultBtMessageFactory.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 "DefaultBtMessageFactory.h"
  36. #include "DlAbortEx.h"
  37. #include "bittorrent_helper.h"
  38. #include "BtKeepAliveMessage.h"
  39. #include "BtChokeMessage.h"
  40. #include "BtUnchokeMessage.h"
  41. #include "BtInterestedMessage.h"
  42. #include "BtNotInterestedMessage.h"
  43. #include "BtHaveMessage.h"
  44. #include "BtBitfieldMessage.h"
  45. #include "BtBitfieldMessageValidator.h"
  46. #include "RangeBtMessageValidator.h"
  47. #include "IndexBtMessageValidator.h"
  48. #include "BtRequestMessage.h"
  49. #include "BtCancelMessage.h"
  50. #include "BtPieceMessage.h"
  51. #include "BtPieceMessageValidator.h"
  52. #include "BtPortMessage.h"
  53. #include "BtHaveAllMessage.h"
  54. #include "BtHaveNoneMessage.h"
  55. #include "BtRejectMessage.h"
  56. #include "BtSuggestPieceMessage.h"
  57. #include "BtAllowedFastMessage.h"
  58. #include "BtHandshakeMessage.h"
  59. #include "BtHandshakeMessageValidator.h"
  60. #include "BtExtendedMessage.h"
  61. #include "ExtensionMessage.h"
  62. #include "Peer.h"
  63. #include "Piece.h"
  64. #include "DownloadContext.h"
  65. #include "PieceStorage.h"
  66. #include "PeerStorage.h"
  67. #include "fmt.h"
  68. #include "ExtensionMessageFactory.h"
  69. #include "bittorrent_helper.h"
  70. namespace aria2 {
  71. DefaultBtMessageFactory::DefaultBtMessageFactory()
  72. : cuid_{0},
  73. downloadContext_{nullptr},
  74. pieceStorage_{nullptr},
  75. peerStorage_{nullptr},
  76. dhtEnabled_(false),
  77. dispatcher_{nullptr},
  78. requestFactory_{nullptr},
  79. peerConnection_{nullptr},
  80. extensionMessageFactory_{nullptr},
  81. localNode_{nullptr},
  82. routingTable_{nullptr},
  83. taskQueue_{nullptr},
  84. taskFactory_{nullptr},
  85. metadataGetMode_(false)
  86. {
  87. }
  88. std::unique_ptr<BtMessage>
  89. DefaultBtMessageFactory::createBtMessage(const unsigned char* data,
  90. size_t dataLength)
  91. {
  92. auto msg = std::unique_ptr<AbstractBtMessage>{};
  93. if (dataLength == 0) {
  94. // keep-alive
  95. msg = make_unique<BtKeepAliveMessage>();
  96. }
  97. else {
  98. uint8_t id = bittorrent::getId(data);
  99. switch (id) {
  100. case BtChokeMessage::ID:
  101. msg = BtChokeMessage::create(data, dataLength);
  102. break;
  103. case BtUnchokeMessage::ID:
  104. msg = BtUnchokeMessage::create(data, dataLength);
  105. break;
  106. case BtInterestedMessage::ID: {
  107. auto m = BtInterestedMessage::create(data, dataLength);
  108. m->setPeerStorage(peerStorage_);
  109. msg = std::move(m);
  110. break;
  111. }
  112. case BtNotInterestedMessage::ID: {
  113. auto m = BtNotInterestedMessage::create(data, dataLength);
  114. m->setPeerStorage(peerStorage_);
  115. msg = std::move(m);
  116. break;
  117. }
  118. case BtHaveMessage::ID:
  119. msg = BtHaveMessage::create(data, dataLength);
  120. if (!metadataGetMode_) {
  121. msg->setBtMessageValidator(make_unique<IndexBtMessageValidator>(
  122. static_cast<BtHaveMessage*>(msg.get()),
  123. downloadContext_->getNumPieces()));
  124. }
  125. break;
  126. case BtBitfieldMessage::ID:
  127. msg = BtBitfieldMessage::create(data, dataLength);
  128. if (!metadataGetMode_) {
  129. msg->setBtMessageValidator(make_unique<BtBitfieldMessageValidator>(
  130. static_cast<BtBitfieldMessage*>(msg.get()),
  131. downloadContext_->getNumPieces()));
  132. }
  133. break;
  134. case BtRequestMessage::ID: {
  135. auto m = BtRequestMessage::create(data, dataLength);
  136. if (!metadataGetMode_) {
  137. m->setBtMessageValidator(make_unique<RangeBtMessageValidator>(
  138. static_cast<BtRequestMessage*>(m.get()),
  139. downloadContext_->getNumPieces(),
  140. pieceStorage_->getPieceLength(m->getIndex())));
  141. }
  142. msg = std::move(m);
  143. break;
  144. }
  145. case BtPieceMessage::ID: {
  146. auto m = BtPieceMessage::create(data, dataLength);
  147. if (!metadataGetMode_) {
  148. m->setBtMessageValidator(make_unique<BtPieceMessageValidator>(
  149. static_cast<BtPieceMessage*>(m.get()),
  150. downloadContext_->getNumPieces(),
  151. pieceStorage_->getPieceLength(m->getIndex())));
  152. }
  153. m->setDownloadContext(downloadContext_);
  154. m->setPeerStorage(peerStorage_);
  155. msg = std::move(m);
  156. break;
  157. }
  158. case BtCancelMessage::ID: {
  159. auto m = BtCancelMessage::create(data, dataLength);
  160. if (!metadataGetMode_) {
  161. m->setBtMessageValidator(make_unique<RangeBtMessageValidator>(
  162. static_cast<BtCancelMessage*>(m.get()),
  163. downloadContext_->getNumPieces(),
  164. pieceStorage_->getPieceLength(m->getIndex())));
  165. }
  166. msg = std::move(m);
  167. break;
  168. }
  169. case BtPortMessage::ID: {
  170. auto m = BtPortMessage::create(data, dataLength);
  171. m->setLocalNode(localNode_);
  172. m->setRoutingTable(routingTable_);
  173. m->setTaskQueue(taskQueue_);
  174. m->setTaskFactory(taskFactory_);
  175. msg = std::move(m);
  176. break;
  177. }
  178. case BtSuggestPieceMessage::ID: {
  179. auto m = BtSuggestPieceMessage::create(data, dataLength);
  180. if (!metadataGetMode_) {
  181. m->setBtMessageValidator(make_unique<IndexBtMessageValidator>(
  182. static_cast<BtSuggestPieceMessage*>(m.get()),
  183. downloadContext_->getNumPieces()));
  184. }
  185. msg = std::move(m);
  186. break;
  187. }
  188. case BtHaveAllMessage::ID:
  189. msg = BtHaveAllMessage::create(data, dataLength);
  190. break;
  191. case BtHaveNoneMessage::ID:
  192. msg = BtHaveNoneMessage::create(data, dataLength);
  193. break;
  194. case BtRejectMessage::ID: {
  195. auto m = BtRejectMessage::create(data, dataLength);
  196. if (!metadataGetMode_) {
  197. m->setBtMessageValidator(make_unique<RangeBtMessageValidator>(
  198. static_cast<BtRejectMessage*>(m.get()),
  199. downloadContext_->getNumPieces(),
  200. pieceStorage_->getPieceLength(m->getIndex())));
  201. }
  202. msg = std::move(m);
  203. break;
  204. }
  205. case BtAllowedFastMessage::ID: {
  206. auto m = BtAllowedFastMessage::create(data, dataLength);
  207. if (!metadataGetMode_) {
  208. m->setBtMessageValidator(make_unique<IndexBtMessageValidator>(
  209. static_cast<BtAllowedFastMessage*>(m.get()),
  210. downloadContext_->getNumPieces()));
  211. }
  212. msg = std::move(m);
  213. break;
  214. }
  215. case BtExtendedMessage::ID: {
  216. if (peer_->isExtendedMessagingEnabled()) {
  217. msg = BtExtendedMessage::create(extensionMessageFactory_, peer_, data,
  218. dataLength);
  219. }
  220. else {
  221. throw DL_ABORT_EX("Received extended message from peer during"
  222. " a session with extended messaging disabled.");
  223. }
  224. break;
  225. }
  226. default:
  227. throw DL_ABORT_EX(fmt("Invalid message ID. id=%u", id));
  228. }
  229. }
  230. setCommonProperty(msg.get());
  231. return std::move(msg);
  232. }
  233. void DefaultBtMessageFactory::setCommonProperty(AbstractBtMessage* msg)
  234. {
  235. msg->setCuid(cuid_);
  236. msg->setPeer(peer_);
  237. msg->setPieceStorage(pieceStorage_);
  238. msg->setBtMessageDispatcher(dispatcher_);
  239. msg->setBtRequestFactory(requestFactory_);
  240. msg->setBtMessageFactory(this);
  241. msg->setPeerConnection(peerConnection_);
  242. if (metadataGetMode_) {
  243. msg->enableMetadataGetMode();
  244. }
  245. }
  246. std::unique_ptr<BtHandshakeMessage>
  247. DefaultBtMessageFactory::createHandshakeMessage(const unsigned char* data,
  248. size_t dataLength)
  249. {
  250. auto msg = BtHandshakeMessage::create(data, dataLength);
  251. msg->setBtMessageValidator(make_unique<BtHandshakeMessageValidator>(
  252. msg.get(), bittorrent::getInfoHash(downloadContext_)));
  253. setCommonProperty(msg.get());
  254. return msg;
  255. }
  256. std::unique_ptr<BtHandshakeMessage>
  257. DefaultBtMessageFactory::createHandshakeMessage(const unsigned char* infoHash,
  258. const unsigned char* peerId)
  259. {
  260. auto msg = make_unique<BtHandshakeMessage>(infoHash, peerId);
  261. msg->setDHTEnabled(dhtEnabled_);
  262. setCommonProperty(msg.get());
  263. return msg;
  264. }
  265. std::unique_ptr<BtRequestMessage> DefaultBtMessageFactory::createRequestMessage(
  266. const std::shared_ptr<Piece>& piece, size_t blockIndex)
  267. {
  268. auto msg = make_unique<BtRequestMessage>(
  269. piece->getIndex(), blockIndex * piece->getBlockLength(),
  270. piece->getBlockLength(blockIndex), blockIndex);
  271. setCommonProperty(msg.get());
  272. return msg;
  273. }
  274. std::unique_ptr<BtCancelMessage>
  275. DefaultBtMessageFactory::createCancelMessage(size_t index, int32_t begin,
  276. int32_t length)
  277. {
  278. auto msg = make_unique<BtCancelMessage>(index, begin, length);
  279. setCommonProperty(msg.get());
  280. return msg;
  281. }
  282. std::unique_ptr<BtPieceMessage>
  283. DefaultBtMessageFactory::createPieceMessage(size_t index, int32_t begin,
  284. int32_t length)
  285. {
  286. auto msg = make_unique<BtPieceMessage>(index, begin, length);
  287. msg->setDownloadContext(downloadContext_);
  288. setCommonProperty(msg.get());
  289. return msg;
  290. }
  291. std::unique_ptr<BtHaveMessage>
  292. DefaultBtMessageFactory::createHaveMessage(size_t index)
  293. {
  294. auto msg = make_unique<BtHaveMessage>(index);
  295. setCommonProperty(msg.get());
  296. return msg;
  297. }
  298. std::unique_ptr<BtChokeMessage> DefaultBtMessageFactory::createChokeMessage()
  299. {
  300. auto msg = make_unique<BtChokeMessage>();
  301. setCommonProperty(msg.get());
  302. return msg;
  303. }
  304. std::unique_ptr<BtUnchokeMessage>
  305. DefaultBtMessageFactory::createUnchokeMessage()
  306. {
  307. auto msg = make_unique<BtUnchokeMessage>();
  308. setCommonProperty(msg.get());
  309. return msg;
  310. }
  311. std::unique_ptr<BtInterestedMessage>
  312. DefaultBtMessageFactory::createInterestedMessage()
  313. {
  314. auto msg = make_unique<BtInterestedMessage>();
  315. setCommonProperty(msg.get());
  316. return msg;
  317. }
  318. std::unique_ptr<BtNotInterestedMessage>
  319. DefaultBtMessageFactory::createNotInterestedMessage()
  320. {
  321. auto msg = make_unique<BtNotInterestedMessage>();
  322. setCommonProperty(msg.get());
  323. return msg;
  324. }
  325. std::unique_ptr<BtBitfieldMessage>
  326. DefaultBtMessageFactory::createBitfieldMessage()
  327. {
  328. auto msg = make_unique<BtBitfieldMessage>(pieceStorage_->getBitfield(),
  329. pieceStorage_->getBitfieldLength());
  330. setCommonProperty(msg.get());
  331. return msg;
  332. }
  333. std::unique_ptr<BtKeepAliveMessage>
  334. DefaultBtMessageFactory::createKeepAliveMessage()
  335. {
  336. auto msg = make_unique<BtKeepAliveMessage>();
  337. setCommonProperty(msg.get());
  338. return msg;
  339. }
  340. std::unique_ptr<BtHaveAllMessage>
  341. DefaultBtMessageFactory::createHaveAllMessage()
  342. {
  343. auto msg = make_unique<BtHaveAllMessage>();
  344. setCommonProperty(msg.get());
  345. return msg;
  346. }
  347. std::unique_ptr<BtHaveNoneMessage>
  348. DefaultBtMessageFactory::createHaveNoneMessage()
  349. {
  350. auto msg = make_unique<BtHaveNoneMessage>();
  351. setCommonProperty(msg.get());
  352. return msg;
  353. }
  354. std::unique_ptr<BtRejectMessage>
  355. DefaultBtMessageFactory::createRejectMessage(size_t index, int32_t begin,
  356. int32_t length)
  357. {
  358. auto msg = make_unique<BtRejectMessage>(index, begin, length);
  359. setCommonProperty(msg.get());
  360. return msg;
  361. }
  362. std::unique_ptr<BtAllowedFastMessage>
  363. DefaultBtMessageFactory::createAllowedFastMessage(size_t index)
  364. {
  365. auto msg = make_unique<BtAllowedFastMessage>(index);
  366. setCommonProperty(msg.get());
  367. return msg;
  368. }
  369. std::unique_ptr<BtPortMessage>
  370. DefaultBtMessageFactory::createPortMessage(uint16_t port)
  371. {
  372. auto msg = make_unique<BtPortMessage>(port);
  373. setCommonProperty(msg.get());
  374. return msg;
  375. }
  376. std::unique_ptr<BtExtendedMessage>
  377. DefaultBtMessageFactory::createBtExtendedMessage(
  378. std::unique_ptr<ExtensionMessage> exmsg)
  379. {
  380. auto msg = make_unique<BtExtendedMessage>(std::move(exmsg));
  381. setCommonProperty(msg.get());
  382. return msg;
  383. }
  384. void DefaultBtMessageFactory::setTaskQueue(DHTTaskQueue* taskQueue)
  385. {
  386. taskQueue_ = taskQueue;
  387. }
  388. void DefaultBtMessageFactory::setTaskFactory(DHTTaskFactory* taskFactory)
  389. {
  390. taskFactory_ = taskFactory;
  391. }
  392. void DefaultBtMessageFactory::setPeer(const std::shared_ptr<Peer>& peer)
  393. {
  394. peer_ = peer;
  395. }
  396. void DefaultBtMessageFactory::setDownloadContext(
  397. DownloadContext* downloadContext)
  398. {
  399. downloadContext_ = downloadContext;
  400. }
  401. void DefaultBtMessageFactory::setPieceStorage(PieceStorage* pieceStorage)
  402. {
  403. pieceStorage_ = pieceStorage;
  404. }
  405. void DefaultBtMessageFactory::setPeerStorage(PeerStorage* peerStorage)
  406. {
  407. peerStorage_ = peerStorage;
  408. }
  409. void DefaultBtMessageFactory::setBtMessageDispatcher(
  410. BtMessageDispatcher* dispatcher)
  411. {
  412. dispatcher_ = dispatcher;
  413. }
  414. void DefaultBtMessageFactory::setExtensionMessageFactory(
  415. ExtensionMessageFactory* factory)
  416. {
  417. extensionMessageFactory_ = factory;
  418. }
  419. void DefaultBtMessageFactory::setLocalNode(DHTNode* localNode)
  420. {
  421. localNode_ = localNode;
  422. }
  423. void DefaultBtMessageFactory::setRoutingTable(DHTRoutingTable* routingTable)
  424. {
  425. routingTable_ = routingTable;
  426. }
  427. void DefaultBtMessageFactory::setBtRequestFactory(BtRequestFactory* factory)
  428. {
  429. requestFactory_ = factory;
  430. }
  431. void DefaultBtMessageFactory::setPeerConnection(PeerConnection* connection)
  432. {
  433. peerConnection_ = connection;
  434. }
  435. } // namespace aria2