UTPexExtensionMessage.cc 5.4 KB

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