DefaultExtensionMessageFactory.cc 6.3 KB

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