DefaultExtensionMessageFactory.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "Peer.h"
  37. #include "DlAbortEx.h"
  38. #include "HandshakeExtensionMessage.h"
  39. #include "UTPexExtensionMessage.h"
  40. #include "fmt.h"
  41. #include "PeerStorage.h"
  42. #include "ExtensionMessageRegistry.h"
  43. #include "DownloadContext.h"
  44. #include "BtMessageDispatcher.h"
  45. #include "BtMessageFactory.h"
  46. #include "UTMetadataRequestExtensionMessage.h"
  47. #include "UTMetadataDataExtensionMessage.h"
  48. #include "UTMetadataRejectExtensionMessage.h"
  49. #include "message.h"
  50. #include "PieceStorage.h"
  51. #include "UTMetadataRequestTracker.h"
  52. #include "RequestGroup.h"
  53. #include "bencode2.h"
  54. namespace aria2 {
  55. DefaultExtensionMessageFactory::DefaultExtensionMessageFactory()
  56. : messageFactory_(0),
  57. dispatcher_(0),
  58. tracker_(0)
  59. {}
  60. DefaultExtensionMessageFactory::DefaultExtensionMessageFactory
  61. (const SharedHandle<Peer>& peer,
  62. const SharedHandle<ExtensionMessageRegistry>& registry)
  63. : peer_(peer),
  64. registry_(registry)
  65. {}
  66. DefaultExtensionMessageFactory::~DefaultExtensionMessageFactory() {}
  67. ExtensionMessageHandle
  68. DefaultExtensionMessageFactory::createMessage(const unsigned char* data, size_t length)
  69. {
  70. uint8_t extensionMessageID = *data;
  71. if(extensionMessageID == 0) {
  72. // handshake
  73. HandshakeExtensionMessageHandle m =
  74. HandshakeExtensionMessage::create(data, length);
  75. m->setPeer(peer_);
  76. m->setDownloadContext(dctx_);
  77. return m;
  78. } else {
  79. std::string extensionName = registry_->getExtensionName(extensionMessageID);
  80. if(extensionName.empty()) {
  81. throw DL_ABORT_EX
  82. (fmt("No extension registered for extended message ID %u",
  83. extensionMessageID));
  84. }
  85. if(extensionName == "ut_pex") {
  86. // uTorrent compatible Peer-Exchange
  87. UTPexExtensionMessageHandle m =
  88. UTPexExtensionMessage::create(data, length);
  89. m->setPeerStorage(peerStorage_);
  90. return m;
  91. } else if(extensionName == "ut_metadata") {
  92. if(length == 0) {
  93. throw DL_ABORT_EX
  94. (fmt(MSG_TOO_SMALL_PAYLOAD_SIZE,
  95. "ut_metadata",
  96. static_cast<unsigned long>(length)));
  97. }
  98. size_t end;
  99. SharedHandle<ValueBase> decoded = bencode2::decode(data+1, length-1, end);
  100. const Dict* dict = asDict(decoded);
  101. if(!dict) {
  102. throw DL_ABORT_EX("Bad ut_metadata: dictionary not found");
  103. }
  104. const Integer* msgType = asInteger(dict->get("msg_type"));
  105. if(!msgType) {
  106. throw DL_ABORT_EX("Bad ut_metadata: msg_type not found");
  107. }
  108. const Integer* index = asInteger(dict->get("piece"));
  109. if(!index) {
  110. throw DL_ABORT_EX("Bad ut_metadata: piece not found");
  111. }
  112. switch(msgType->i()) {
  113. case 0: {
  114. SharedHandle<UTMetadataRequestExtensionMessage> m
  115. (new UTMetadataRequestExtensionMessage(extensionMessageID));
  116. m->setIndex(index->i());
  117. m->setDownloadContext(dctx_);
  118. m->setPeer(peer_);
  119. m->setBtMessageFactory(messageFactory_);
  120. m->setBtMessageDispatcher(dispatcher_);
  121. return m;
  122. }
  123. case 1: {
  124. if(end == length) {
  125. throw DL_ABORT_EX("Bad ut_metadata data: data not found");
  126. }
  127. const Integer* totalSize = asInteger(dict->get("total_size"));
  128. if(!totalSize) {
  129. throw DL_ABORT_EX("Bad ut_metadata data: total_size not found");
  130. }
  131. SharedHandle<UTMetadataDataExtensionMessage> m
  132. (new UTMetadataDataExtensionMessage(extensionMessageID));
  133. m->setIndex(index->i());
  134. m->setTotalSize(totalSize->i());
  135. m->setData(std::string(&data[1+end], &data[length]));
  136. m->setUTMetadataRequestTracker(tracker_);
  137. m->setPieceStorage(dctx_->getOwnerRequestGroup()->getPieceStorage());
  138. m->setDownloadContext(dctx_);
  139. return m;
  140. }
  141. case 2: {
  142. SharedHandle<UTMetadataRejectExtensionMessage> m
  143. (new UTMetadataRejectExtensionMessage(extensionMessageID));
  144. m->setIndex(index->i());
  145. // No need to inject tracker because peer will be disconnected.
  146. return m;
  147. }
  148. default:
  149. throw DL_ABORT_EX
  150. (fmt("Bad ut_metadata: unknown msg_type=%u",
  151. static_cast<unsigned int>(msgType->i())));
  152. }
  153. } else {
  154. throw DL_ABORT_EX
  155. (fmt("Unsupported extension message received."
  156. " extensionMessageID=%u, extensionName=%s",
  157. extensionMessageID, extensionName.c_str()));
  158. }
  159. }
  160. }
  161. void DefaultExtensionMessageFactory::setPeerStorage
  162. (const SharedHandle<PeerStorage>& peerStorage)
  163. {
  164. peerStorage_ = peerStorage;
  165. }
  166. void DefaultExtensionMessageFactory::setPeer(const SharedHandle<Peer>& peer)
  167. {
  168. peer_ = peer;
  169. }
  170. } // namespace aria2