DefaultExtensionMessageFactory.cc 5.8 KB

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