DefaultExtensionMessageFactory.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. HandshakeExtensionMessage* m =
  75. HandshakeExtensionMessage::create(data, length);
  76. m->setPeer(peer_);
  77. m->setDownloadContext(dctx_);
  78. return SharedHandle<ExtensionMessage>(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. UTPexExtensionMessage* m = UTPexExtensionMessage::create(data, length);
  89. m->setPeerStorage(peerStorage_);
  90. return SharedHandle<ExtensionMessage>(m);
  91. } else if(strcmp(extensionName, "ut_metadata") == 0) {
  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 =
  100. bencode2::decode(data+1, length - 1, end);
  101. const Dict* dict = downcast<Dict>(decoded);
  102. if(!dict) {
  103. throw DL_ABORT_EX("Bad ut_metadata: dictionary not found");
  104. }
  105. const Integer* msgType = downcast<Integer>(dict->get("msg_type"));
  106. if(!msgType) {
  107. throw DL_ABORT_EX("Bad ut_metadata: msg_type not found");
  108. }
  109. const Integer* index = downcast<Integer>(dict->get("piece"));
  110. if(!index) {
  111. throw DL_ABORT_EX("Bad ut_metadata: piece not found");
  112. }
  113. switch(msgType->i()) {
  114. case 0: {
  115. UTMetadataRequestExtensionMessage* m
  116. (new UTMetadataRequestExtensionMessage(extensionMessageID));
  117. m->setIndex(index->i());
  118. m->setDownloadContext(dctx_);
  119. m->setPeer(peer_);
  120. m->setBtMessageFactory(messageFactory_);
  121. m->setBtMessageDispatcher(dispatcher_);
  122. return SharedHandle<ExtensionMessage>(m);
  123. }
  124. case 1: {
  125. if(end == length) {
  126. throw DL_ABORT_EX("Bad ut_metadata data: data not found");
  127. }
  128. const Integer* totalSize = downcast<Integer>(dict->get("total_size"));
  129. if(!totalSize) {
  130. throw DL_ABORT_EX("Bad ut_metadata data: total_size not found");
  131. }
  132. UTMetadataDataExtensionMessage* m
  133. (new UTMetadataDataExtensionMessage(extensionMessageID));
  134. m->setIndex(index->i());
  135. m->setTotalSize(totalSize->i());
  136. m->setData(&data[1+end], &data[length]);
  137. m->setUTMetadataRequestTracker(tracker_);
  138. m->setPieceStorage(dctx_->getOwnerRequestGroup()->getPieceStorage());
  139. m->setDownloadContext(dctx_);
  140. return SharedHandle<ExtensionMessage>(m);
  141. }
  142. case 2: {
  143. UTMetadataRejectExtensionMessage* m
  144. (new UTMetadataRejectExtensionMessage(extensionMessageID));
  145. m->setIndex(index->i());
  146. // No need to inject tracker because peer will be disconnected.
  147. return SharedHandle<ExtensionMessage>(m);
  148. }
  149. default:
  150. throw DL_ABORT_EX
  151. (fmt("Bad ut_metadata: unknown msg_type=%u",
  152. static_cast<unsigned int>(msgType->i())));
  153. }
  154. } else {
  155. throw DL_ABORT_EX
  156. (fmt("Unsupported extension message received."
  157. " extensionMessageID=%u, extensionName=%s",
  158. extensionMessageID, extensionName));
  159. }
  160. }
  161. }
  162. void DefaultExtensionMessageFactory::setPeerStorage
  163. (const SharedHandle<PeerStorage>& peerStorage)
  164. {
  165. peerStorage_ = peerStorage;
  166. }
  167. void DefaultExtensionMessageFactory::setPeer(const SharedHandle<Peer>& peer)
  168. {
  169. peer_ = peer;
  170. }
  171. } // namespace aria2