UTPexExtensionMessage.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "DlAbortEx.h"
  41. #include "message.h"
  42. #include "fmt.h"
  43. #include "bencode2.h"
  44. #include "a2functional.h"
  45. #include "wallclock.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::pair<std::string, std::string>,
  57. std::pair<std::string, std::string> > freshPeerPair =
  58. createCompactPeerListAndFlag(freshPeers_);
  59. std::pair<std::pair<std::string, std::string>,
  60. std::pair<std::string, std::string> > droppedPeerPair =
  61. createCompactPeerListAndFlag(droppedPeers_);
  62. Dict dict;
  63. if(!freshPeerPair.first.first.empty()) {
  64. dict.put("added", freshPeerPair.first.first);
  65. dict.put("added.f", freshPeerPair.first.second);
  66. }
  67. if(!droppedPeerPair.first.first.empty()) {
  68. dict.put("dropped", droppedPeerPair.first.first);
  69. }
  70. if(!freshPeerPair.second.first.empty()) {
  71. dict.put("added6", freshPeerPair.second.first);
  72. dict.put("added6.f", freshPeerPair.second.second);
  73. }
  74. if(!droppedPeerPair.second.first.empty()) {
  75. dict.put("dropped6", droppedPeerPair.second.first);
  76. }
  77. return bencode2::encode(&dict);
  78. }
  79. std::pair<std::pair<std::string, std::string>,
  80. std::pair<std::string, std::string> >
  81. UTPexExtensionMessage::createCompactPeerListAndFlag
  82. (const std::vector<SharedHandle<Peer> >& peers)
  83. {
  84. std::string addrstring;
  85. std::string flagstring;
  86. std::string addrstring6;
  87. std::string flagstring6;
  88. for(std::vector<SharedHandle<Peer> >::const_iterator itr = peers.begin(),
  89. eoi = peers.end(); itr != eoi; ++itr) {
  90. unsigned char compact[COMPACT_LEN_IPV6];
  91. int compactlen = bittorrent::packcompact
  92. (compact, (*itr)->getIPAddress(), (*itr)->getPort());
  93. if(compactlen == COMPACT_LEN_IPV4) {
  94. addrstring.append(&compact[0], &compact[compactlen]);
  95. flagstring += (*itr)->isSeeder() ? 0x02u : 0x00u;
  96. } else if(compactlen == COMPACT_LEN_IPV6) {
  97. addrstring6.append(&compact[0], &compact[compactlen]);
  98. flagstring6 += (*itr)->isSeeder() ? 0x02u : 0x00u;
  99. }
  100. }
  101. return std::make_pair(std::make_pair(addrstring, flagstring),
  102. std::make_pair(addrstring6, flagstring6));
  103. }
  104. std::string UTPexExtensionMessage::toString() const
  105. {
  106. return strconcat("ut_pex added=", util::uitos(freshPeers_.size()),
  107. ", dropped=", util::uitos(droppedPeers_.size()));
  108. }
  109. void UTPexExtensionMessage::doReceivedAction()
  110. {
  111. peerStorage_->addPeer(freshPeers_);
  112. peerStorage_->addPeer(droppedPeers_);
  113. }
  114. bool UTPexExtensionMessage::addFreshPeer(const SharedHandle<Peer>& peer)
  115. {
  116. if(!peer->isIncomingPeer() &&
  117. peer->getFirstContactTime().difference(global::wallclock) < interval_) {
  118. freshPeers_.push_back(peer);
  119. return true;
  120. } else {
  121. return false;
  122. }
  123. }
  124. bool UTPexExtensionMessage::freshPeersAreFull() const
  125. {
  126. return freshPeers_.size() >= maxFreshPeer_;
  127. }
  128. bool UTPexExtensionMessage::addDroppedPeer(const SharedHandle<Peer>& peer)
  129. {
  130. if(!peer->isIncomingPeer() &&
  131. peer->getBadConditionStartTime().
  132. difference(global::wallclock) < interval_) {
  133. droppedPeers_.push_back(peer);
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }
  139. bool UTPexExtensionMessage::droppedPeersAreFull() const
  140. {
  141. return droppedPeers_.size() >= maxDroppedPeer_;
  142. }
  143. void UTPexExtensionMessage::setMaxFreshPeer(size_t maxFreshPeer)
  144. {
  145. maxFreshPeer_ = maxFreshPeer;
  146. }
  147. void UTPexExtensionMessage::setMaxDroppedPeer(size_t maxDroppedPeer)
  148. {
  149. maxDroppedPeer_ = maxDroppedPeer;
  150. }
  151. void UTPexExtensionMessage::setPeerStorage
  152. (const SharedHandle<PeerStorage>& peerStorage)
  153. {
  154. peerStorage_ = peerStorage;
  155. }
  156. UTPexExtensionMessageHandle
  157. UTPexExtensionMessage::create(const unsigned char* data, size_t len)
  158. {
  159. if(len < 1) {
  160. throw DL_ABORT_EX(fmt(MSG_TOO_SMALL_PAYLOAD_SIZE,
  161. EXTENSION_NAME.c_str(),
  162. static_cast<unsigned long>(len)));
  163. }
  164. UTPexExtensionMessageHandle msg(new UTPexExtensionMessage(*data));
  165. SharedHandle<ValueBase> decoded = bencode2::decode(data+1, len-1);
  166. const Dict* dict = asDict(decoded);
  167. if(dict) {
  168. const String* added = asString(dict->get("added"));
  169. if(added) {
  170. bittorrent::extractPeer
  171. (added, AF_INET, std::back_inserter(msg->freshPeers_));
  172. }
  173. const String* dropped = asString(dict->get("dropped"));
  174. if(dropped) {
  175. bittorrent::extractPeer
  176. (dropped, AF_INET, std::back_inserter(msg->droppedPeers_));
  177. }
  178. const String* added6 = asString(dict->get("added6"));
  179. if(added6) {
  180. bittorrent::extractPeer
  181. (added6, AF_INET6, std::back_inserter(msg->freshPeers_));
  182. }
  183. const String* dropped6 = asString(dict->get("dropped6"));
  184. if(dropped6) {
  185. bittorrent::extractPeer
  186. (dropped6, AF_INET6, std::back_inserter(msg->droppedPeers_));
  187. }
  188. }
  189. return msg;
  190. }
  191. } // namespace aria2