DefaultBtMessageFactory.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. BtMessageHandle
  65. DefaultBtMessageFactory::createBtMessage(const unsigned char* data, int32_t dataLength)
  66. {
  67. AbstractBtMessageHandle msg(0);
  68. if(dataLength == 0) {
  69. // keep-alive
  70. msg = new BtKeepAliveMessage();
  71. } else {
  72. int8_t id = PeerMessageUtil::getId(data);
  73. switch(id) {
  74. case BtChokeMessage::ID:
  75. msg = BtChokeMessage::create(data, dataLength);
  76. break;
  77. case BtUnchokeMessage::ID:
  78. msg = BtUnchokeMessage::create(data, dataLength);
  79. break;
  80. case BtInterestedMessage::ID:
  81. msg = BtInterestedMessage::create(data, dataLength);
  82. break;
  83. case BtNotInterestedMessage::ID:
  84. msg = BtNotInterestedMessage::create(data, dataLength);
  85. break;
  86. case BtHaveMessage::ID:
  87. msg = BtHaveMessage::create(data, dataLength);
  88. msg->setBtMessageValidator(new BtHaveMessageValidator((BtHaveMessage*)msg.get(),
  89. btContext->getNumPieces()));
  90. break;
  91. case BtBitfieldMessage::ID:
  92. msg = BtBitfieldMessage::create(data, dataLength);
  93. msg->setBtMessageValidator(new BtBitfieldMessageValidator((BtBitfieldMessage*)msg.get(),
  94. btContext->getNumPieces()));
  95. break;
  96. case BtRequestMessage::ID: {
  97. BtRequestMessageHandle temp = BtRequestMessage::create(data, dataLength);
  98. BtMessageValidatorHandle validator =
  99. new BtRequestMessageValidator(temp.get(),
  100. btContext->getNumPieces(),
  101. pieceStorage->getPieceLength(temp->getIndex()));
  102. temp->setBtMessageValidator(validator);
  103. msg = temp;
  104. break;
  105. }
  106. case BtCancelMessage::ID: {
  107. BtCancelMessageHandle temp = BtCancelMessage::create(data, dataLength);
  108. BtMessageValidatorHandle validator =
  109. new BtCancelMessageValidator(temp.get(),
  110. btContext->getNumPieces(),
  111. pieceStorage->getPieceLength(temp->getIndex()));
  112. temp->setBtMessageValidator(validator);
  113. msg = temp;
  114. break;
  115. }
  116. case BtPieceMessage::ID: {
  117. BtPieceMessageHandle temp = BtPieceMessage::create(data, dataLength);
  118. BtMessageValidatorHandle validator =
  119. new BtPieceMessageValidator(temp.get(),
  120. btContext->getNumPieces(),
  121. pieceStorage->getPieceLength(temp->getIndex()));
  122. temp->setBtMessageValidator(validator);
  123. msg = temp;
  124. break;
  125. }
  126. case BtPortMessage::ID:
  127. msg = BtPortMessage::create(data, dataLength);
  128. break;
  129. case BtHaveAllMessage::ID:
  130. msg = BtHaveAllMessage::create(data, dataLength);
  131. break;
  132. case BtHaveNoneMessage::ID:
  133. msg = BtHaveNoneMessage::create(data, dataLength);
  134. break;
  135. case BtRejectMessage::ID: {
  136. BtRejectMessageHandle temp = BtRejectMessage::create(data, dataLength);
  137. BtMessageValidatorHandle validator =
  138. new BtRejectMessageValidator(temp.get(),
  139. btContext->getNumPieces(),
  140. pieceStorage->getPieceLength(temp->getIndex()));
  141. temp->setBtMessageValidator(validator);
  142. msg = temp;
  143. break;
  144. }
  145. case BtSuggestPieceMessage::ID: {
  146. BtSuggestPieceMessageHandle temp = BtSuggestPieceMessage::create(data, dataLength);
  147. BtMessageValidatorHandle validator =
  148. new BtSuggestPieceMessageValidator(temp.get(),
  149. btContext->getNumPieces());
  150. temp->setBtMessageValidator(validator);
  151. msg = temp;
  152. break;
  153. }
  154. case BtAllowedFastMessage::ID: {
  155. BtAllowedFastMessageHandle temp = BtAllowedFastMessage::create(data, dataLength);
  156. BtMessageValidatorHandle validator =
  157. new BtAllowedFastMessageValidator(temp.get(),
  158. btContext->getNumPieces());
  159. temp->setBtMessageValidator(validator);
  160. msg = temp;
  161. break;
  162. }
  163. default:
  164. throw new DlAbortEx("Invalid message ID. id=%d", id);
  165. }
  166. }
  167. setCommonProperty(msg);
  168. return msg;
  169. }
  170. void DefaultBtMessageFactory::setCommonProperty(const AbstractBtMessageHandle& msg) {
  171. msg->setCuid(cuid);
  172. msg->setPeer(peer);
  173. msg->setBtContext(btContext);
  174. msg->setBtMessageDispatcher(dispatcher);
  175. msg->setBtRequestFactory(requestFactory);
  176. msg->setBtMessageFactory(this);
  177. msg->setPeerConnection(peerConnection);
  178. }
  179. BtMessageHandle
  180. DefaultBtMessageFactory::createHandshakeMessage(const unsigned char* data, int32_t dataLength)
  181. {
  182. BtHandshakeMessageHandle msg = BtHandshakeMessage::create(data, dataLength);
  183. BtMessageValidatorHandle validator =
  184. new BtHandshakeMessageValidator(msg.get(),
  185. btContext->getInfoHash());
  186. msg->setBtMessageValidator(validator);
  187. setCommonProperty(msg);
  188. return msg;
  189. }
  190. BtMessageHandle
  191. DefaultBtMessageFactory::createHandshakeMessage(const unsigned char* infoHash,
  192. const unsigned char* peerId)
  193. {
  194. BtHandshakeMessageHandle msg = new BtHandshakeMessage(infoHash, peerId);
  195. BtMessageValidatorHandle validator =
  196. new BtHandshakeMessageValidator(msg.get(),
  197. btContext->getInfoHash());
  198. msg->setBtMessageValidator(validator);
  199. setCommonProperty(msg);
  200. return msg;
  201. }
  202. BtMessageHandle
  203. DefaultBtMessageFactory::createRequestMessage(const PieceHandle& piece, int32_t blockIndex)
  204. {
  205. BtRequestMessageHandle msg =
  206. new BtRequestMessage(piece->getIndex(),
  207. blockIndex*piece->getBlockLength(),
  208. piece->getBlockLength(blockIndex),
  209. blockIndex);
  210. BtMessageValidatorHandle validator =
  211. new BtRequestMessageValidator(msg.get(),
  212. btContext->getNumPieces(),
  213. pieceStorage->getPieceLength(msg->getIndex()));
  214. msg->setBtMessageValidator(validator);
  215. setCommonProperty(msg);
  216. return msg;
  217. }
  218. BtMessageHandle
  219. DefaultBtMessageFactory::createCancelMessage(int32_t index, int32_t begin, int32_t length)
  220. {
  221. BtCancelMessageHandle msg = new BtCancelMessage(index, begin, length);
  222. BtMessageValidatorHandle validator =
  223. new BtCancelMessageValidator(msg.get(),
  224. btContext->getNumPieces(),
  225. pieceStorage->getPieceLength(index));
  226. msg->setBtMessageValidator(validator);
  227. setCommonProperty(msg);
  228. return msg;
  229. }
  230. BtMessageHandle
  231. DefaultBtMessageFactory::createPieceMessage(int32_t index, int32_t begin, int32_t length)
  232. {
  233. BtPieceMessageHandle msg = new BtPieceMessage(index, begin, length);
  234. BtMessageValidatorHandle validator =
  235. new BtPieceMessageValidator(msg.get(),
  236. btContext->getNumPieces(),
  237. pieceStorage->getPieceLength(index));
  238. msg->setBtMessageValidator(validator);
  239. setCommonProperty(msg);
  240. return msg;
  241. }
  242. BtMessageHandle
  243. DefaultBtMessageFactory::createHaveMessage(int32_t index)
  244. {
  245. BtHaveMessageHandle msg = new BtHaveMessage(index);
  246. msg->setBtMessageValidator(new BtHaveMessageValidator(msg.get(),
  247. btContext->getNumPieces()));
  248. setCommonProperty(msg);
  249. return msg;
  250. }
  251. BtMessageHandle
  252. DefaultBtMessageFactory::createChokeMessage()
  253. {
  254. BtChokeMessageHandle msg = new BtChokeMessage();
  255. setCommonProperty(msg);
  256. return msg;
  257. }
  258. BtMessageHandle
  259. DefaultBtMessageFactory::createUnchokeMessage()
  260. {
  261. BtUnchokeMessageHandle msg = new BtUnchokeMessage();
  262. setCommonProperty(msg);
  263. return msg;
  264. }
  265. BtMessageHandle
  266. DefaultBtMessageFactory::createInterestedMessage()
  267. {
  268. BtInterestedMessageHandle msg = new BtInterestedMessage();
  269. setCommonProperty(msg);
  270. return msg;
  271. }
  272. BtMessageHandle
  273. DefaultBtMessageFactory::createNotInterestedMessage()
  274. {
  275. BtNotInterestedMessageHandle msg = new BtNotInterestedMessage();
  276. setCommonProperty(msg);
  277. return msg;
  278. }
  279. BtMessageHandle
  280. DefaultBtMessageFactory::createBitfieldMessage()
  281. {
  282. BtBitfieldMessageHandle msg =
  283. new BtBitfieldMessage(pieceStorage->getBitfield(),
  284. pieceStorage->getBitfieldLength());
  285. msg->setBtMessageValidator(new BtBitfieldMessageValidator(msg.get(),
  286. btContext->getNumPieces()));
  287. setCommonProperty(msg);
  288. return msg;
  289. }
  290. BtMessageHandle
  291. DefaultBtMessageFactory::createKeepAliveMessage()
  292. {
  293. BtKeepAliveMessageHandle msg = new BtKeepAliveMessage();
  294. setCommonProperty(msg);
  295. return msg;
  296. }
  297. BtMessageHandle
  298. DefaultBtMessageFactory::createHaveAllMessage()
  299. {
  300. BtHaveAllMessageHandle msg = new BtHaveAllMessage();
  301. setCommonProperty(msg);
  302. return msg;
  303. }
  304. BtMessageHandle
  305. DefaultBtMessageFactory::createHaveNoneMessage()
  306. {
  307. BtHaveNoneMessageHandle msg = new BtHaveNoneMessage();
  308. setCommonProperty(msg);
  309. return msg;
  310. }
  311. BtMessageHandle
  312. DefaultBtMessageFactory::createRejectMessage(int32_t index, int32_t begin, int32_t length)
  313. {
  314. BtRejectMessageHandle msg = new BtRejectMessage(index, begin, length);
  315. BtMessageValidatorHandle validator =
  316. new BtRejectMessageValidator(msg.get(),
  317. btContext->getNumPieces(),
  318. pieceStorage->getPieceLength(index));
  319. msg->setBtMessageValidator(validator);
  320. setCommonProperty(msg);
  321. return msg;
  322. }
  323. BtMessageHandle
  324. DefaultBtMessageFactory::createAllowedFastMessage(int32_t index)
  325. {
  326. BtAllowedFastMessageHandle msg = new BtAllowedFastMessage(index);
  327. BtMessageValidatorHandle validator =
  328. new BtAllowedFastMessageValidator(msg.get(),
  329. btContext->getNumPieces());
  330. msg->setBtMessageValidator(validator);
  331. setCommonProperty(msg);
  332. return msg;
  333. }