DefaultBtMessageFactory.cc 14 KB

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