HandshakeExtensionMessage.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "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 "bencode.h"
  44. namespace aria2 {
  45. const std::string HandshakeExtensionMessage::EXTENSION_NAME = "handshake";
  46. HandshakeExtensionMessage::HandshakeExtensionMessage():
  47. _tcpPort(0),
  48. _logger(LogFactory::getInstance()) {}
  49. HandshakeExtensionMessage::~HandshakeExtensionMessage() {}
  50. std::string HandshakeExtensionMessage::getBencodedData()
  51. {
  52. BDE dict = BDE::dict();
  53. if(!_clientVersion.empty()) {
  54. dict["v"] = _clientVersion;
  55. }
  56. if(_tcpPort > 0) {
  57. dict["p"] = _tcpPort;
  58. }
  59. BDE extDict = BDE::dict();
  60. for(std::map<std::string, uint8_t>::const_iterator itr = _extensions.begin();
  61. itr != _extensions.end(); ++itr) {
  62. const std::map<std::string, uint8_t>::value_type& vt = *itr;
  63. extDict[vt.first] = vt.second;
  64. }
  65. dict["m"] = extDict;
  66. return bencode::encode(dict);
  67. }
  68. std::string HandshakeExtensionMessage::toString() const
  69. {
  70. std::string s = getExtensionName();
  71. if(!_clientVersion.empty()) {
  72. strappend(s, " client=", util::urlencode(_clientVersion));
  73. }
  74. if(_tcpPort > 0) {
  75. strappend(s, ", tcpPort=", util::uitos(_tcpPort));
  76. }
  77. for(std::map<std::string, uint8_t>::const_iterator itr = _extensions.begin();
  78. itr != _extensions.end(); ++itr) {
  79. const std::map<std::string, uint8_t>::value_type& vt = *itr;
  80. strappend(s, ", ", vt.first, "=", util::uitos(vt.second));
  81. }
  82. return s;
  83. }
  84. void HandshakeExtensionMessage::doReceivedAction()
  85. {
  86. if(_tcpPort > 0) {
  87. _peer->port = _tcpPort;
  88. _peer->setIncomingPeer(false);
  89. }
  90. for(std::map<std::string, uint8_t>::const_iterator itr = _extensions.begin();
  91. itr != _extensions.end(); ++itr) {
  92. const std::map<std::string, uint8_t>::value_type& vt = *itr;
  93. _peer->setExtension(vt.first, vt.second);
  94. }
  95. }
  96. void HandshakeExtensionMessage::setPeer(const PeerHandle& peer)
  97. {
  98. _peer = peer;
  99. }
  100. uint8_t HandshakeExtensionMessage::getExtensionMessageID(const std::string& name) const
  101. {
  102. std::map<std::string, uint8_t>::const_iterator i = _extensions.find(name);
  103. if(i == _extensions.end()) {
  104. return 0;
  105. } else {
  106. return (*i).second;
  107. }
  108. }
  109. HandshakeExtensionMessageHandle
  110. HandshakeExtensionMessage::create(const unsigned char* data, size_t length)
  111. {
  112. if(length < 1) {
  113. throw DL_ABORT_EX
  114. (StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE,
  115. EXTENSION_NAME.c_str(), length).str());
  116. }
  117. HandshakeExtensionMessageHandle msg(new HandshakeExtensionMessage());
  118. msg->_logger->debug("Creating HandshakeExtensionMessage from %s",
  119. util::urlencode(data, length).c_str());
  120. const BDE dict = bencode::decode(data+1, length-1);
  121. if(!dict.isDict()) {
  122. throw DL_ABORT_EX("Unexpected payload format for extended message handshake");
  123. }
  124. const BDE& port = dict["p"];
  125. if(port.isInteger() && 0 < port.i() && port.i() < 65536) {
  126. msg->_tcpPort = port.i();
  127. }
  128. const BDE& version = dict["v"];
  129. if(version.isString()) {
  130. msg->_clientVersion = version.s();
  131. }
  132. const BDE& extDict = dict["m"];
  133. if(extDict.isDict()) {
  134. for(BDE::Dict::const_iterator i = extDict.dictBegin();
  135. i != extDict.dictEnd(); ++i) {
  136. if((*i).second.isInteger()) {
  137. msg->_extensions[(*i).first] = (*i).second.i();
  138. }
  139. }
  140. }
  141. return msg;
  142. }
  143. } // namespace aria2