From 31d9d4dbed319a3c1a0a493c1e48ed7d20621762 Mon Sep 17 00:00:00 2001 From: Nagi5Yeq Date: Thu, 4 Feb 2021 23:21:41 +0800 Subject: add ipv6 address in http announce --- src/DefaultBtAnnounce.cc | 32 +++++++++++++++++++------------- src/SocketCore.cc | 16 ++++++++++++++++ src/SocketCore.h | 3 +++ 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/DefaultBtAnnounce.cc b/src/DefaultBtAnnounce.cc index 087af61f..17daa390 100644 --- a/src/DefaultBtAnnounce.cc +++ b/src/DefaultBtAnnounce.cc @@ -165,22 +165,22 @@ std::string DefaultBtAnnounce::getAnnounceUrl() uri += fmt("info_hash=%s&" "peer_id=%s&", util::percentEncode(bittorrent::getInfoHash(downloadContext_), - INFO_HASH_LENGTH) - .c_str(), + INFO_HASH_LENGTH) + .c_str(), util::percentEncode(bittorrent::getStaticPeerId(), PEER_ID_LENGTH) - .c_str()); + .c_str()); if (tcpPort_) { uri += fmt("port=%u&", tcpPort_); } uri += fmt("uploaded=%" PRId64 "&" - "downloaded=%" PRId64 "&" - "left=%" PRId64 "&" - "numwant=%d&" - "key=%x&" - "compact=1", - stat.getSessionUploadLength(), stat.getSessionDownloadLength(), left, - numWant, - (unsigned int)httpAnnounceKey_); + "downloaded=%" PRId64 "&" + "left=%" PRId64 "&" + "numwant=%d&" + "key=%x&" + "compact=1&" + "supportcrypto=1", + stat.getSessionUploadLength(), stat.getSessionDownloadLength(), + left, numWant, (unsigned int)httpAnnounceKey_); const char* event = announceList_.getEventString(); if (option_->getAsBool(PREF_BT_FORCE_ENCRYPTION) || option_->getAsBool(PREF_BT_REQUIRE_CRYPTO)) { @@ -194,8 +194,14 @@ std::string DefaultBtAnnounce::getAnnounceUrl() uri += "&trackerid="; uri += util::percentEncode(trackerId_); } - else { - uri += "&supportcrypto=1"; + if (!option_->blank(PREF_BT_EXTERNAL_IP)) { + uri += "&ip="; + uri += option_->get(PREF_BT_EXTERNAL_IP); + } + const char* globalIpv6Addr = net::getGlobalIpv6Addr(); + if (globalIpv6Addr != nullptr) { + uri += "&ipv6="; + uri += globalIpv6Addr; } return uri; } diff --git a/src/SocketCore.cc b/src/SocketCore.cc index e235f512..8907eb4f 100644 --- a/src/SocketCore.cc +++ b/src/SocketCore.cc @@ -1570,6 +1570,9 @@ bool verifyHostname(const std::string& hostname, namespace { bool ipv4AddrConfigured = true; bool ipv6AddrConfigured = true; + +bool hasGlobalIpv6Addr = false; +char globalIpv6Addr[NI_MAXHOST]; } // namespace #ifdef __MINGW32__ @@ -1694,6 +1697,11 @@ void checkAddrconfig() !IN6_IS_ADDR_LINKLOCAL(&ad.in6.sin6_addr)) { ipv6AddrConfigured = true; found = true; + if (!hasGlobalIpv6Addr && + inetNtop(AF_INET6, &ad.in6.sin6_addr, globalIpv6Addr, + sizeof(globalIpv6Addr) == 0)) { + hasGlobalIpv6Addr = true; + } } break; } @@ -1723,6 +1731,14 @@ bool getIPv4AddrConfigured() { return ipv4AddrConfigured; } bool getIPv6AddrConfigured() { return ipv6AddrConfigured; } +const char* getGlobalIpv6Addr() +{ + if (hasGlobalIpv6Addr == true) { + return globalIpv6Addr; + } + return nullptr; +} + } // namespace net } // namespace aria2 diff --git a/src/SocketCore.h b/src/SocketCore.h index 60f33279..38cb3a95 100644 --- a/src/SocketCore.h +++ b/src/SocketCore.h @@ -425,6 +425,9 @@ void checkAddrconfig(); bool getIPv4AddrConfigured(); bool getIPv6AddrConfigured(); +// Returns a unicast ipv6 address or NULL if not exist. Used for announce. +const char* getGlobalIpv6Addr(); + } // namespace net } // namespace aria2 -- cgit