DefaultExtensionMessageFactory.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "DefaultExtensionMessageFactory.h"
  36. #include <cstring>
  37. #include "Peer.h"
  38. #include "DlAbortEx.h"
  39. #include "HandshakeExtensionMessage.h"
  40. #include "UTPexExtensionMessage.h"
  41. #include "fmt.h"
  42. #include "PeerStorage.h"
  43. #include "ExtensionMessageRegistry.h"
  44. #include "DownloadContext.h"
  45. #include "BtMessageDispatcher.h"
  46. #include "BtMessageFactory.h"
  47. #include "UTMetadataRequestExtensionMessage.h"
  48. #include "UTMetadataDataExtensionMessage.h"
  49. #include "UTMetadataRejectExtensionMessage.h"
  50. #include "message.h"
  51. #include "PieceStorage.h"
  52. #include "UTMetadataRequestTracker.h"
  53. #include "RequestGroup.h"
  54. #include "bencode2.h"
  55. namespace aria2 {
  56. // i686-w64-mingw32-g++ 4.6 does not support constructor delegate
  57. DefaultExtensionMessageFactory::DefaultExtensionMessageFactory()
  58. : peerStorage_{nullptr},
  59. registry_{nullptr},
  60. dctx_{nullptr},
  61. messageFactory_{nullptr},
  62. dispatcher_{nullptr},
  63. tracker_{nullptr}
  64. {
  65. }
  66. DefaultExtensionMessageFactory::DefaultExtensionMessageFactory(
  67. const std::shared_ptr<Peer>& peer, ExtensionMessageRegistry* registry)
  68. : peerStorage_{nullptr},
  69. peer_{peer},
  70. registry_{registry},
  71. dctx_{nullptr},
  72. messageFactory_{nullptr},
  73. dispatcher_{nullptr},
  74. tracker_{nullptr}
  75. {
  76. }
  77. std::unique_ptr<ExtensionMessage>
  78. DefaultExtensionMessageFactory::createMessage(const unsigned char* data,
  79. size_t length)
  80. {
  81. uint8_t extensionMessageID = *data;
  82. if (extensionMessageID == 0) {
  83. // handshake
  84. auto m = HandshakeExtensionMessage::create(data, length);
  85. m->setPeer(peer_);
  86. m->setDownloadContext(dctx_);
  87. return std::move(m);
  88. }
  89. else {
  90. const char* extensionName = registry_->getExtensionName(extensionMessageID);
  91. if (!extensionName) {
  92. throw DL_ABORT_EX(
  93. fmt("No extension registered for extended message ID %u",
  94. extensionMessageID));
  95. }
  96. if (strcmp(extensionName, "ut_pex") == 0) {
  97. // uTorrent compatible Peer-Exchange
  98. auto m = UTPexExtensionMessage::create(data, length);
  99. m->setPeerStorage(peerStorage_);
  100. return std::move(m);
  101. }
  102. else if (strcmp(extensionName, "ut_metadata") == 0) {
  103. if (length == 0) {
  104. throw DL_ABORT_EX(fmt(MSG_TOO_SMALL_PAYLOAD_SIZE, "ut_metadata",
  105. static_cast<unsigned long>(length)));
  106. }
  107. size_t end;
  108. auto decoded = bencode2::decode(data + 1, length - 1, end);
  109. const Dict* dict = downcast<Dict>(decoded);
  110. if (!dict) {
  111. throw DL_ABORT_EX("Bad ut_metadata: dictionary not found");
  112. }
  113. const Integer* msgType = downcast<Integer>(dict->get("msg_type"));
  114. if (!msgType) {
  115. throw DL_ABORT_EX("Bad ut_metadata: msg_type not found");
  116. }
  117. const Integer* index = downcast<Integer>(dict->get("piece"));
  118. if (!index || index->i() < 0) {
  119. throw DL_ABORT_EX("Bad ut_metadata: piece not found");
  120. }
  121. switch (msgType->i()) {
  122. case 0: {
  123. auto m =
  124. make_unique<UTMetadataRequestExtensionMessage>(extensionMessageID);
  125. m->setIndex(index->i());
  126. m->setDownloadContext(dctx_);
  127. m->setPeer(peer_);
  128. m->setBtMessageFactory(messageFactory_);
  129. m->setBtMessageDispatcher(dispatcher_);
  130. return std::move(m);
  131. }
  132. case 1: {
  133. if (end == length) {
  134. throw DL_ABORT_EX("Bad ut_metadata data: data not found");
  135. }
  136. const Integer* totalSize = downcast<Integer>(dict->get("total_size"));
  137. if (!totalSize || totalSize->i() < 0) {
  138. throw DL_ABORT_EX("Bad ut_metadata data: total_size not found");
  139. }
  140. auto m =
  141. make_unique<UTMetadataDataExtensionMessage>(extensionMessageID);
  142. m->setIndex(index->i());
  143. m->setTotalSize(totalSize->i());
  144. m->setData(&data[1 + end], &data[length]);
  145. m->setUTMetadataRequestTracker(tracker_);
  146. m->setPieceStorage(
  147. dctx_->getOwnerRequestGroup()->getPieceStorage().get());
  148. m->setDownloadContext(dctx_);
  149. return std::move(m);
  150. }
  151. case 2: {
  152. auto m =
  153. make_unique<UTMetadataRejectExtensionMessage>(extensionMessageID);
  154. m->setIndex(index->i());
  155. // No need to inject tracker because peer will be disconnected.
  156. return std::move(m);
  157. }
  158. default:
  159. throw DL_ABORT_EX(
  160. fmt("Bad ut_metadata: unknown msg_type=%" PRId64, msgType->i()));
  161. }
  162. }
  163. else {
  164. throw DL_ABORT_EX(fmt("Unsupported extension message received."
  165. " extensionMessageID=%u, extensionName=%s",
  166. extensionMessageID, extensionName));
  167. }
  168. }
  169. }
  170. void DefaultExtensionMessageFactory::setPeerStorage(PeerStorage* peerStorage)
  171. {
  172. peerStorage_ = peerStorage;
  173. }
  174. void DefaultExtensionMessageFactory::setPeer(const std::shared_ptr<Peer>& peer)
  175. {
  176. peer_ = peer;
  177. }
  178. void DefaultExtensionMessageFactory::setExtensionMessageRegistry(
  179. ExtensionMessageRegistry* registry)
  180. {
  181. registry_ = registry;
  182. }
  183. void DefaultExtensionMessageFactory::setDownloadContext(DownloadContext* dctx)
  184. {
  185. dctx_ = dctx;
  186. }
  187. void DefaultExtensionMessageFactory::setBtMessageFactory(
  188. BtMessageFactory* factory)
  189. {
  190. messageFactory_ = factory;
  191. }
  192. void DefaultExtensionMessageFactory::setBtMessageDispatcher(
  193. BtMessageDispatcher* disp)
  194. {
  195. dispatcher_ = disp;
  196. }
  197. void DefaultExtensionMessageFactory::setUTMetadataRequestTracker(
  198. UTMetadataRequestTracker* tracker)
  199. {
  200. tracker_ = tracker;
  201. }
  202. } // namespace aria2