/* */ #include "UTPexExtensionMessage.h" #include "Peer.h" #include "util.h" #include "bittorrent_helper.h" #include "PeerStorage.h" #include "DlAbortEx.h" #include "message.h" #include "fmt.h" #include "bencode2.h" #include "a2functional.h" #include "wallclock.h" namespace aria2 { namespace { // This is the hard limit of the number of "fresh peer" and "dropped // peer". This number is treated as the sum of IPv4 and IPv6 peers. const size_t DEFAULT_MAX_FRESH_PEER = 50; const size_t DEFAULT_MAX_DROPPED_PEER = 50; } // namespace const char UTPexExtensionMessage::EXTENSION_NAME[] = "ut_pex"; constexpr std::chrono::minutes UTPexExtensionMessage::DEFAULT_INTERVAL; UTPexExtensionMessage::UTPexExtensionMessage(uint8_t extensionMessageID) : extensionMessageID_{extensionMessageID}, peerStorage_{nullptr}, interval_{DEFAULT_INTERVAL}, maxFreshPeer_{DEFAULT_MAX_FRESH_PEER}, maxDroppedPeer_{DEFAULT_MAX_DROPPED_PEER} { } std::string UTPexExtensionMessage::getPayload() { auto freshPeerPair = createCompactPeerListAndFlag(freshPeers_); auto droppedPeerPair = createCompactPeerListAndFlag(droppedPeers_); Dict dict; if (!freshPeerPair.first.first.empty()) { dict.put("added", freshPeerPair.first.first); dict.put("added.f", freshPeerPair.first.second); } if (!droppedPeerPair.first.first.empty()) { dict.put("dropped", droppedPeerPair.first.first); } if (!freshPeerPair.second.first.empty()) { dict.put("added6", freshPeerPair.second.first); dict.put("added6.f", freshPeerPair.second.second); } if (!droppedPeerPair.second.first.empty()) { dict.put("dropped6", droppedPeerPair.second.first); } return bencode2::encode(&dict); } std::pair, std::pair> UTPexExtensionMessage::createCompactPeerListAndFlag( const std::vector>& peers) { std::string addrstring; std::string flagstring; std::string addrstring6; std::string flagstring6; for (auto itr = std::begin(peers), eoi = std::end(peers); itr != eoi; ++itr) { unsigned char compact[COMPACT_LEN_IPV6]; int compactlen = bittorrent::packcompact(compact, (*itr)->getIPAddress(), (*itr)->getPort()); if (compactlen == COMPACT_LEN_IPV4) { addrstring.append(&compact[0], &compact[compactlen]); flagstring += (*itr)->isSeeder() ? 0x02u : 0x00u; } else if (compactlen == COMPACT_LEN_IPV6) { addrstring6.append(&compact[0], &compact[compactlen]); flagstring6 += (*itr)->isSeeder() ? 0x02u : 0x00u; } } return std::make_pair( std::make_pair(std::move(addrstring), std::move(flagstring)), std::make_pair(std::move(addrstring6), std::move(flagstring6))); } std::string UTPexExtensionMessage::toString() const { return fmt("ut_pex added=%lu, dropped=%lu", static_cast(freshPeers_.size()), static_cast(droppedPeers_.size())); } void UTPexExtensionMessage::doReceivedAction() { peerStorage_->addPeer(freshPeers_); peerStorage_->addPeer(droppedPeers_); } bool UTPexExtensionMessage::addFreshPeer(const std::shared_ptr& peer) { if (!peer->isIncomingPeer() && peer->getFirstContactTime().difference(global::wallclock()) < interval_) { freshPeers_.push_back(peer); return true; } else { return false; } } const std::vector>& UTPexExtensionMessage::getFreshPeers() const { return freshPeers_; } bool UTPexExtensionMessage::freshPeersAreFull() const { return freshPeers_.size() >= maxFreshPeer_; } bool UTPexExtensionMessage::addDroppedPeer(const std::shared_ptr& peer) { if (!peer->isIncomingPeer() && peer->getDropStartTime().difference(global::wallclock()) < interval_) { droppedPeers_.push_back(peer); return true; } else { return false; } } const std::vector>& UTPexExtensionMessage::getDroppedPeers() const { return droppedPeers_; } bool UTPexExtensionMessage::droppedPeersAreFull() const { return droppedPeers_.size() >= maxDroppedPeer_; } void UTPexExtensionMessage::setMaxFreshPeer(size_t maxFreshPeer) { maxFreshPeer_ = maxFreshPeer; } void UTPexExtensionMessage::setMaxDroppedPeer(size_t maxDroppedPeer) { maxDroppedPeer_ = maxDroppedPeer; } void UTPexExtensionMessage::setPeerStorage(PeerStorage* peerStorage) { peerStorage_ = peerStorage; } std::unique_ptr UTPexExtensionMessage::create(const unsigned char* data, size_t len) { if (len < 1) { throw DL_ABORT_EX(fmt(MSG_TOO_SMALL_PAYLOAD_SIZE, EXTENSION_NAME, static_cast(len))); } auto msg = make_unique(*data); auto decoded = bencode2::decode(data + 1, len - 1); const Dict* dict = downcast(decoded); if (dict) { const String* added = downcast(dict->get("added")); if (added) { bittorrent::extractPeer(added, AF_INET, std::back_inserter(msg->freshPeers_)); } const String* dropped = downcast(dict->get("dropped")); if (dropped) { bittorrent::extractPeer(dropped, AF_INET, std::back_inserter(msg->droppedPeers_)); } const String* added6 = downcast(dict->get("added6")); if (added6) { bittorrent::extractPeer(added6, AF_INET6, std::back_inserter(msg->freshPeers_)); } const String* dropped6 = downcast(dict->get("dropped6")); if (dropped6) { bittorrent::extractPeer(dropped6, AF_INET6, std::back_inserter(msg->droppedPeers_)); } } return msg; } } // namespace aria2