HandshakeExtensionMessage.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 = getExtensionName();
  80. if(!clientVersion_.empty()) {
  81. strappend(s, " client=", util::percentEncode(clientVersion_));
  82. }
  83. if(tcpPort_ > 0) {
  84. strappend(s, ", tcpPort=", util::uitos(tcpPort_));
  85. }
  86. if(metadataSize_) {
  87. strappend(s, ", metadataSize=", util::uitos(metadataSize_));
  88. }
  89. for(std::map<std::string, uint8_t>::const_iterator itr = extensions_.begin(),
  90. eoi = extensions_.end(); itr != eoi; ++itr) {
  91. const std::map<std::string, uint8_t>::value_type& vt = *itr;
  92. strappend(s, ", ", vt.first, "=", util::uitos(vt.second));
  93. }
  94. return s;
  95. }
  96. void HandshakeExtensionMessage::doReceivedAction()
  97. {
  98. if(tcpPort_ > 0) {
  99. peer_->setPort(tcpPort_);
  100. peer_->setIncomingPeer(false);
  101. }
  102. for(std::map<std::string, uint8_t>::const_iterator itr = extensions_.begin(),
  103. eoi = extensions_.end(); itr != eoi; ++itr) {
  104. const std::map<std::string, uint8_t>::value_type& vt = *itr;
  105. peer_->setExtension(vt.first, vt.second);
  106. }
  107. SharedHandle<TorrentAttribute> attrs =
  108. bittorrent::getTorrentAttrs(dctx_);
  109. if(attrs->metadata.empty() && !peer_->getExtensionMessageID("ut_metadata")) {
  110. // TODO In metadataGetMode, if peer don't support metadata
  111. // transfer, should we drop connection? There is a possibility
  112. // that peer can still tell us peers using PEX.
  113. throw DL_ABORT_EX("Peer doesn't support ut_metadata extension. Goodbye.");
  114. }
  115. if(metadataSize_ > 0) {
  116. if(attrs->metadataSize) {
  117. if(metadataSize_ != attrs->metadataSize) {
  118. throw DL_ABORT_EX("Wrong metadata_size. Which one is correct!?");
  119. }
  120. if(!peer_->isSeeder()) {
  121. peer_->reconfigureSessionResource(dctx_->getPieceLength(),
  122. dctx_->getTotalLength());
  123. peer_->setAllBitfield();
  124. }
  125. } else {
  126. attrs->metadataSize = metadataSize_;
  127. dctx_->getFirstFileEntry()->setLength(metadataSize_);
  128. dctx_->markTotalLengthIsKnown();
  129. dctx_->getOwnerRequestGroup()->initPieceStorage();
  130. SharedHandle<PieceStorage> pieceStorage =
  131. dctx_->getOwnerRequestGroup()->getPieceStorage();
  132. // We enter 'end game' mode from the start to get metadata
  133. // quickly.
  134. pieceStorage->enterEndGame();
  135. peer_->reconfigureSessionResource(dctx_->getPieceLength(),
  136. dctx_->getTotalLength());
  137. peer_->setAllBitfield();
  138. }
  139. } else if(attrs->metadata.empty()) {
  140. throw DL_ABORT_EX("Peer didn't provide metadata_size."
  141. " It seems that it doesn't have whole metadata.");
  142. }
  143. }
  144. void HandshakeExtensionMessage::setPeer(const SharedHandle<Peer>& peer)
  145. {
  146. peer_ = peer;
  147. }
  148. uint8_t HandshakeExtensionMessage::getExtensionMessageID(const std::string& name) const
  149. {
  150. std::map<std::string, uint8_t>::const_iterator i = extensions_.find(name);
  151. if(i == extensions_.end()) {
  152. return 0;
  153. } else {
  154. return (*i).second;
  155. }
  156. }
  157. HandshakeExtensionMessageHandle
  158. HandshakeExtensionMessage::create(const unsigned char* data, size_t length)
  159. {
  160. if(length < 1) {
  161. throw DL_ABORT_EX
  162. (fmt(MSG_TOO_SMALL_PAYLOAD_SIZE,
  163. EXTENSION_NAME.c_str(),
  164. static_cast<unsigned long>(length)));
  165. }
  166. HandshakeExtensionMessageHandle msg(new HandshakeExtensionMessage());
  167. A2_LOG_DEBUG(fmt("Creating HandshakeExtensionMessage from %s",
  168. util::percentEncode(data, length).c_str()));
  169. SharedHandle<ValueBase> decoded = bencode2::decode(data+1, length-1);
  170. const Dict* dict = asDict(decoded);
  171. if(!dict) {
  172. throw DL_ABORT_EX
  173. ("Unexpected payload format for extended message handshake");
  174. }
  175. const Integer* port = asInteger(dict->get("p"));
  176. if(port && 0 < port->i() && port->i() < 65536) {
  177. msg->tcpPort_ = port->i();
  178. }
  179. const String* version = asString(dict->get("v"));
  180. if(version) {
  181. msg->clientVersion_ = version->s();
  182. }
  183. const Dict* extDict = asDict(dict->get("m"));
  184. if(extDict) {
  185. for(Dict::ValueType::const_iterator i = extDict->begin(),
  186. eoi = extDict->end(); i != eoi; ++i) {
  187. const Integer* extId = asInteger((*i).second);
  188. if(extId) {
  189. msg->extensions_[(*i).first] = extId->i();
  190. }
  191. }
  192. }
  193. const Integer* metadataSize = asInteger(dict->get("metadata_size"));
  194. // Only accept metadata smaller than 1MiB
  195. if(metadataSize && metadataSize->i() <= 1024*1024) {
  196. msg->metadataSize_ = metadataSize->i();
  197. }
  198. return msg;
  199. }
  200. } // namespace aria2