HandshakeExtensionMessage.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2009 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 "HandshakeExtensionMessage.h"
  36. #include "Peer.h"
  37. #include "util.h"
  38. #include "DlAbortEx.h"
  39. #include "LogFactory.h"
  40. #include "Logger.h"
  41. #include "message.h"
  42. #include "fmt.h"
  43. #include "bencode2.h"
  44. #include "DownloadContext.h"
  45. #include "bittorrent_helper.h"
  46. #include "RequestGroup.h"
  47. #include "PieceStorage.h"
  48. #include "FileEntry.h"
  49. namespace aria2 {
  50. const std::string HandshakeExtensionMessage::EXTENSION_NAME = "handshake";
  51. HandshakeExtensionMessage::HandshakeExtensionMessage()
  52. : tcpPort_(0),
  53. metadataSize_(0)
  54. {}
  55. HandshakeExtensionMessage::~HandshakeExtensionMessage() {}
  56. std::string HandshakeExtensionMessage::getPayload()
  57. {
  58. Dict dict;
  59. if(!clientVersion_.empty()) {
  60. dict.put("v", clientVersion_);
  61. }
  62. if(tcpPort_ > 0) {
  63. dict.put("p", Integer::g(tcpPort_));
  64. }
  65. SharedHandle<Dict> extDict = Dict::g();
  66. for(std::map<std::string, uint8_t>::const_iterator itr = extensions_.begin(),
  67. eoi = extensions_.end(); itr != eoi; ++itr) {
  68. const std::map<std::string, uint8_t>::value_type& vt = *itr;
  69. extDict->put(vt.first, Integer::g(vt.second));
  70. }
  71. dict.put("m", extDict);
  72. if(metadataSize_) {
  73. dict.put("metadata_size", Integer::g(metadataSize_));
  74. }
  75. return bencode2::encode(&dict);
  76. }
  77. std::string HandshakeExtensionMessage::toString() const
  78. {
  79. std::string s(fmt("%s client=%s, tcpPort=%u, metadataSize=%lu",
  80. getExtensionName().c_str(),
  81. util::percentEncode(clientVersion_).c_str(),
  82. tcpPort_,
  83. metadataSize_));
  84. for(std::map<std::string, uint8_t>::const_iterator itr = extensions_.begin(),
  85. eoi = extensions_.end(); itr != eoi; ++itr) {
  86. const std::map<std::string, uint8_t>::value_type& vt = *itr;
  87. s += fmt(", %s=%u", vt.first.c_str(), vt.second);
  88. }
  89. return s;
  90. }
  91. void HandshakeExtensionMessage::doReceivedAction()
  92. {
  93. if(tcpPort_ > 0) {
  94. peer_->setPort(tcpPort_);
  95. peer_->setIncomingPeer(false);
  96. }
  97. for(std::map<std::string, uint8_t>::const_iterator itr = extensions_.begin(),
  98. eoi = extensions_.end(); itr != eoi; ++itr) {
  99. const std::map<std::string, uint8_t>::value_type& vt = *itr;
  100. peer_->setExtension(vt.first, vt.second);
  101. }
  102. SharedHandle<TorrentAttribute> attrs = bittorrent::getTorrentAttrs(dctx_);
  103. if(attrs->metadata.empty()) {
  104. if(!peer_->getExtensionMessageID("ut_metadata")) {
  105. // TODO In metadataGetMode, if peer don't support metadata
  106. // transfer, should we drop connection? There is a possibility
  107. // that peer can still tell us peers using PEX.
  108. throw DL_ABORT_EX("Peer doesn't support ut_metadata extension. Goodbye.");
  109. }
  110. if(metadataSize_ > 0) {
  111. if(attrs->metadataSize) {
  112. if(metadataSize_ != attrs->metadataSize) {
  113. throw DL_ABORT_EX("Wrong metadata_size. Which one is correct!?");
  114. }
  115. } else {
  116. attrs->metadataSize = metadataSize_;
  117. dctx_->getFirstFileEntry()->setLength(metadataSize_);
  118. dctx_->markTotalLengthIsKnown();
  119. dctx_->getOwnerRequestGroup()->initPieceStorage();
  120. SharedHandle<PieceStorage> pieceStorage =
  121. dctx_->getOwnerRequestGroup()->getPieceStorage();
  122. // We enter 'end game' mode from the start to get metadata
  123. // quickly.
  124. pieceStorage->enterEndGame();
  125. }
  126. peer_->reconfigureSessionResource(dctx_->getPieceLength(),
  127. dctx_->getTotalLength());
  128. peer_->setAllBitfield();
  129. } else {
  130. throw DL_ABORT_EX("Peer didn't provide metadata_size."
  131. " It seems that it doesn't have whole metadata.");
  132. }
  133. }
  134. }
  135. void HandshakeExtensionMessage::setPeer(const SharedHandle<Peer>& peer)
  136. {
  137. peer_ = peer;
  138. }
  139. uint8_t HandshakeExtensionMessage::getExtensionMessageID(const std::string& name) const
  140. {
  141. std::map<std::string, uint8_t>::const_iterator i = extensions_.find(name);
  142. if(i == extensions_.end()) {
  143. return 0;
  144. } else {
  145. return (*i).second;
  146. }
  147. }
  148. HandshakeExtensionMessageHandle
  149. HandshakeExtensionMessage::create(const unsigned char* data, size_t length)
  150. {
  151. if(length < 1) {
  152. throw DL_ABORT_EX
  153. (fmt(MSG_TOO_SMALL_PAYLOAD_SIZE,
  154. EXTENSION_NAME.c_str(),
  155. static_cast<unsigned long>(length)));
  156. }
  157. HandshakeExtensionMessageHandle msg(new HandshakeExtensionMessage());
  158. A2_LOG_DEBUG(fmt("Creating HandshakeExtensionMessage from %s",
  159. util::percentEncode(data, length).c_str()));
  160. SharedHandle<ValueBase> decoded = bencode2::decode(data+1, data+length);
  161. const Dict* dict = downcast<Dict>(decoded);
  162. if(!dict) {
  163. throw DL_ABORT_EX
  164. ("Unexpected payload format for extended message handshake");
  165. }
  166. const Integer* port = downcast<Integer>(dict->get("p"));
  167. if(port && 0 < port->i() && port->i() < 65536) {
  168. msg->tcpPort_ = port->i();
  169. }
  170. const String* version = downcast<String>(dict->get("v"));
  171. if(version) {
  172. msg->clientVersion_ = version->s();
  173. }
  174. const Dict* extDict = downcast<Dict>(dict->get("m"));
  175. if(extDict) {
  176. for(Dict::ValueType::const_iterator i = extDict->begin(),
  177. eoi = extDict->end(); i != eoi; ++i) {
  178. const Integer* extId = downcast<Integer>((*i).second);
  179. if(extId) {
  180. msg->extensions_[(*i).first] = extId->i();
  181. }
  182. }
  183. }
  184. const Integer* metadataSize = downcast<Integer>(dict->get("metadata_size"));
  185. // Only accept metadata smaller than 1MiB
  186. if(metadataSize && metadataSize->i() <= 1024*1024) {
  187. msg->metadataSize_ = metadataSize->i();
  188. }
  189. return msg;
  190. }
  191. } // namespace aria2