DefaultBtMessageFactory.cc 14 KB

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