DefaultExtensionMessageFactory.cc 6.2 KB

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