UTPexExtensionMessage.cc 5.8 KB

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