UTPexExtensionMessage.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "UTPexExtensionMessage.h"
  36. #include "Peer.h"
  37. #include "BtContext.h"
  38. #include "Dictionary.h"
  39. #include "Data.h"
  40. #include "BencodeVisitor.h"
  41. #include "Util.h"
  42. #include "PeerMessageUtil.h"
  43. #include "BtRegistry.h"
  44. #include "PeerStorage.h"
  45. #include "CompactPeerListProcessor.h"
  46. #include "MetaFileUtil.h"
  47. #include "DlAbortEx.h"
  48. #include "message.h"
  49. namespace aria2 {
  50. const std::string UTPexExtensionMessage::EXTENSION_NAME = "ut_pex";
  51. UTPexExtensionMessage::UTPexExtensionMessage(uint8_t extensionMessageID):
  52. _extensionMessageID(extensionMessageID),
  53. _btContext(0) {}
  54. UTPexExtensionMessage::~UTPexExtensionMessage() {}
  55. std::string UTPexExtensionMessage::getBencodedData()
  56. {
  57. SharedHandle<Dictionary> d = new Dictionary();
  58. std::pair<std::string, std::string> freshPeerPair = createCompactPeerListAndFlag(_freshPeers);
  59. std::pair<std::string, std::string> droppedPeerPair = createCompactPeerListAndFlag(_droppedPeers);
  60. d->put("added", new Data(freshPeerPair.first));
  61. d->put("added.f", new Data(freshPeerPair.second));
  62. d->put("dropped", new Data(droppedPeerPair.first));
  63. BencodeVisitor v;
  64. d->accept(&v);
  65. return v.getBencodedData();
  66. }
  67. std::pair<std::string, std::string> UTPexExtensionMessage::createCompactPeerListAndFlag(const Peers& peers)
  68. {
  69. std::string addrstring;
  70. std::string flagstring;
  71. for(Peers::const_iterator itr = peers.begin(); itr != peers.end(); ++itr) {
  72. char compact[6];
  73. if(PeerMessageUtil::createcompact(compact, (*itr)->ipaddr, (*itr)->port)) {
  74. addrstring.append(&compact[0], &compact[6]);
  75. flagstring += (*itr)->isSeeder() ? "2" : "0";
  76. }
  77. }
  78. return std::pair<std::string, std::string>(addrstring, flagstring);
  79. }
  80. std::string UTPexExtensionMessage::toString() const
  81. {
  82. return "ut_pex added="+Util::uitos(_freshPeers.size())+", dropped="+
  83. Util::uitos(_droppedPeers.size());
  84. }
  85. void UTPexExtensionMessage::doReceivedAction()
  86. {
  87. PEER_STORAGE(_btContext)->addPeer(_freshPeers);
  88. }
  89. void UTPexExtensionMessage::addFreshPeer(const PeerHandle& peer)
  90. {
  91. _freshPeers.push_back(peer);
  92. }
  93. const Peers& UTPexExtensionMessage::getFreshPeers() const
  94. {
  95. return _freshPeers;
  96. }
  97. void UTPexExtensionMessage::addDroppedPeer(const PeerHandle& peer)
  98. {
  99. _droppedPeers.push_back(peer);
  100. }
  101. const Peers& UTPexExtensionMessage::getDroppedPeers() const
  102. {
  103. return _droppedPeers;
  104. }
  105. void UTPexExtensionMessage::setBtContext(const BtContextHandle& btContext)
  106. {
  107. _btContext = btContext;
  108. }
  109. UTPexExtensionMessageHandle
  110. UTPexExtensionMessage::create(const BtContextHandle& btContext,
  111. const char* data, size_t len)
  112. {
  113. if(len < 1) {
  114. throw new DlAbortEx(MSG_TOO_SMALL_PAYLOAD_SIZE,
  115. EXTENSION_NAME.c_str(), len);
  116. }
  117. UTPexExtensionMessageHandle msg = new UTPexExtensionMessage(*data);
  118. SharedHandle<MetaEntry> root = MetaFileUtil::bdecoding(data+1, len-1);
  119. if(root.isNull()) {
  120. return msg;
  121. }
  122. const Dictionary* d = dynamic_cast<const Dictionary*>(root.get());
  123. if(d) {
  124. CompactPeerListProcessor proc(btContext->getPieceLength(),
  125. btContext->getTotalLength());
  126. const Data* added = dynamic_cast<const Data*>(d->get("added"));
  127. if(added) {
  128. msg->_freshPeers = proc.extractPeer(added);
  129. }
  130. const Data* dropped = dynamic_cast<const Data*>(d->get("dropped"));
  131. if(dropped) {
  132. msg->_droppedPeers = proc.extractPeer(dropped);
  133. }
  134. }
  135. return msg;
  136. }
  137. } // namespace aria2