UTPexExtensionMessage.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "util.h"
  38. #include "bittorrent_helper.h"
  39. #include "PeerStorage.h"
  40. #include "PeerListProcessor.h"
  41. #include "DlAbortEx.h"
  42. #include "message.h"
  43. #include "StringFormat.h"
  44. #include "bencode.h"
  45. #include "a2functional.h"
  46. namespace aria2 {
  47. const std::string UTPexExtensionMessage::EXTENSION_NAME = "ut_pex";
  48. UTPexExtensionMessage::UTPexExtensionMessage(uint8_t extensionMessageID):
  49. _extensionMessageID(extensionMessageID),
  50. _interval(DEFAULT_INTERVAL),
  51. _maxFreshPeer(DEFAULT_MAX_FRESH_PEER),
  52. _maxDroppedPeer(DEFAULT_MAX_DROPPED_PEER) {}
  53. UTPexExtensionMessage::~UTPexExtensionMessage() {}
  54. std::string UTPexExtensionMessage::getPayload()
  55. {
  56. std::pair<std::string, std::string> freshPeerPair =
  57. createCompactPeerListAndFlag(_freshPeers);
  58. std::pair<std::string, std::string> droppedPeerPair =
  59. createCompactPeerListAndFlag(_droppedPeers);
  60. BDE dict = BDE::dict();
  61. dict["added"] = freshPeerPair.first;
  62. dict["added.f"] = freshPeerPair.second;
  63. dict["dropped"] = droppedPeerPair.first;
  64. return bencode::encode(dict);
  65. }
  66. std::pair<std::string, std::string>
  67. 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. unsigned char compact[6];
  73. if(bittorrent::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 strconcat("ut_pex added=", util::uitos(_freshPeers.size()),
  83. ", dropped=", util::uitos(_droppedPeers.size()));
  84. }
  85. void UTPexExtensionMessage::doReceivedAction()
  86. {
  87. _peerStorage->addPeer(_freshPeers);
  88. }
  89. bool UTPexExtensionMessage::addFreshPeer(const PeerHandle& peer)
  90. {
  91. if(!peer->isIncomingPeer() &&
  92. !peer->getFirstContactTime().elapsed(_interval)) {
  93. _freshPeers.push_back(peer);
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. }
  99. bool UTPexExtensionMessage::freshPeersAreFull() const
  100. {
  101. return _freshPeers.size() >= _maxFreshPeer;
  102. }
  103. bool UTPexExtensionMessage::addDroppedPeer(const PeerHandle& peer)
  104. {
  105. if(!peer->isIncomingPeer() &&
  106. !peer->getBadConditionStartTime().elapsed(_interval)) {
  107. _droppedPeers.push_back(peer);
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }
  113. bool UTPexExtensionMessage::droppedPeersAreFull() const
  114. {
  115. return _droppedPeers.size() >= _maxDroppedPeer;
  116. }
  117. void UTPexExtensionMessage::setMaxFreshPeer(size_t maxFreshPeer)
  118. {
  119. _maxFreshPeer = maxFreshPeer;
  120. }
  121. void UTPexExtensionMessage::setMaxDroppedPeer(size_t maxDroppedPeer)
  122. {
  123. _maxDroppedPeer = maxDroppedPeer;
  124. }
  125. void UTPexExtensionMessage::setPeerStorage
  126. (const SharedHandle<PeerStorage>& peerStorage)
  127. {
  128. _peerStorage = peerStorage;
  129. }
  130. UTPexExtensionMessageHandle
  131. UTPexExtensionMessage::create(const unsigned char* data, size_t len)
  132. {
  133. if(len < 1) {
  134. throw DL_ABORT_EX(StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE,
  135. EXTENSION_NAME.c_str(), len).str());
  136. }
  137. UTPexExtensionMessageHandle msg(new UTPexExtensionMessage(*data));
  138. const BDE dict = bencode::decode(data+1, len-1);
  139. if(dict.isDict()) {
  140. PeerListProcessor proc;
  141. const BDE& added = dict["added"];
  142. if(added.isString()) {
  143. proc.extractPeerFromCompact(added, std::back_inserter(msg->_freshPeers));
  144. }
  145. const BDE& dropped = dict["dropped"];
  146. if(dropped.isString()) {
  147. proc.extractPeerFromCompact(dropped,
  148. std::back_inserter(msg->_droppedPeers));
  149. }
  150. }
  151. return msg;
  152. }
  153. } // namespace aria2