UTPexExtensionMessage.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "PeerMessageUtil.h"
  39. #include "PeerStorage.h"
  40. #include "CompactPeerListProcessor.h"
  41. #include "DlAbortEx.h"
  42. #include "message.h"
  43. #include "StringFormat.h"
  44. #include "bencode.h"
  45. namespace aria2 {
  46. const std::string UTPexExtensionMessage::EXTENSION_NAME = "ut_pex";
  47. UTPexExtensionMessage::UTPexExtensionMessage(uint8_t extensionMessageID):
  48. _extensionMessageID(extensionMessageID),
  49. _interval(DEFAULT_INTERVAL),
  50. _maxFreshPeer(DEFAULT_MAX_FRESH_PEER),
  51. _maxDroppedPeer(DEFAULT_MAX_DROPPED_PEER) {}
  52. UTPexExtensionMessage::~UTPexExtensionMessage() {}
  53. std::string UTPexExtensionMessage::getBencodedData()
  54. {
  55. std::pair<std::string, std::string> freshPeerPair =
  56. createCompactPeerListAndFlag(_freshPeers);
  57. std::pair<std::string, std::string> droppedPeerPair =
  58. createCompactPeerListAndFlag(_droppedPeers);
  59. bencode::BDE dict = bencode::BDE::dict();
  60. dict["added"] = freshPeerPair.first;
  61. dict["added.f"] = freshPeerPair.second;
  62. dict["dropped"] = droppedPeerPair.first;
  63. return bencode::encode(dict);
  64. }
  65. std::pair<std::string, std::string>
  66. UTPexExtensionMessage::createCompactPeerListAndFlag(const Peers& peers)
  67. {
  68. std::string addrstring;
  69. std::string flagstring;
  70. for(Peers::const_iterator itr = peers.begin(); itr != peers.end(); ++itr) {
  71. unsigned char compact[6];
  72. if(PeerMessageUtil::createcompact(compact, (*itr)->ipaddr, (*itr)->port)) {
  73. addrstring.append(&compact[0], &compact[6]);
  74. flagstring += (*itr)->isSeeder() ? "2" : "0";
  75. }
  76. }
  77. return std::pair<std::string, std::string>(addrstring, flagstring);
  78. }
  79. std::string UTPexExtensionMessage::toString() const
  80. {
  81. return "ut_pex added="+Util::uitos(_freshPeers.size())+", dropped="+
  82. Util::uitos(_droppedPeers.size());
  83. }
  84. void UTPexExtensionMessage::doReceivedAction()
  85. {
  86. _peerStorage->addPeer(_freshPeers);
  87. }
  88. bool UTPexExtensionMessage::addFreshPeer(const PeerHandle& peer)
  89. {
  90. if(!peer->isIncomingPeer() &&
  91. !peer->getFirstContactTime().elapsed(_interval)) {
  92. _freshPeers.push_back(peer);
  93. return true;
  94. } else {
  95. return false;
  96. }
  97. }
  98. const Peers& UTPexExtensionMessage::getFreshPeers() const
  99. {
  100. return _freshPeers;
  101. }
  102. bool UTPexExtensionMessage::freshPeersAreFull() const
  103. {
  104. return _freshPeers.size() >= _maxFreshPeer;
  105. }
  106. bool UTPexExtensionMessage::addDroppedPeer(const PeerHandle& peer)
  107. {
  108. if(!peer->isIncomingPeer() &&
  109. !peer->getBadConditionStartTime().elapsed(_interval)) {
  110. _droppedPeers.push_back(peer);
  111. return true;
  112. } else {
  113. return false;
  114. }
  115. }
  116. const Peers& UTPexExtensionMessage::getDroppedPeers() const
  117. {
  118. return _droppedPeers;
  119. }
  120. bool UTPexExtensionMessage::droppedPeersAreFull() const
  121. {
  122. return _droppedPeers.size() >= _maxDroppedPeer;
  123. }
  124. void UTPexExtensionMessage::setMaxFreshPeer(size_t maxFreshPeer)
  125. {
  126. _maxFreshPeer = maxFreshPeer;
  127. }
  128. size_t UTPexExtensionMessage::getMaxFreshPeer() const
  129. {
  130. return _maxFreshPeer;
  131. }
  132. void UTPexExtensionMessage::setMaxDroppedPeer(size_t maxDroppedPeer)
  133. {
  134. _maxDroppedPeer = maxDroppedPeer;
  135. }
  136. size_t UTPexExtensionMessage::getMaxDroppedPeer() const
  137. {
  138. return _maxDroppedPeer;
  139. }
  140. void UTPexExtensionMessage::setPeerStorage
  141. (const SharedHandle<PeerStorage>& peerStorage)
  142. {
  143. _peerStorage = peerStorage;
  144. }
  145. UTPexExtensionMessageHandle
  146. UTPexExtensionMessage::create(const unsigned char* data, size_t len)
  147. {
  148. if(len < 1) {
  149. throw DlAbortEx(StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE,
  150. EXTENSION_NAME.c_str(), len).str());
  151. }
  152. UTPexExtensionMessageHandle msg(new UTPexExtensionMessage(*data));
  153. const bencode::BDE dict = bencode::decode(data+1, len-1);
  154. if(dict.isDict()) {
  155. CompactPeerListProcessor proc;
  156. const bencode::BDE& added = dict["added"];
  157. if(added.isString()) {
  158. proc.extractPeer(msg->_freshPeers, added);
  159. }
  160. const bencode::BDE& dropped = dict["dropped"];
  161. if(dropped.isString()) {
  162. proc.extractPeer(msg->_droppedPeers, dropped);
  163. }
  164. }
  165. return msg;
  166. }
  167. } // namespace aria2