DefaultExtensionMessageFactory.cc 6.9 KB

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