HandshakeExtensionMessage.cc 7.1 KB

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