2023-05-05 17:17:40 +00:00
|
|
|
// This file Copyright © 2023-2023 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
|
|
|
#include <algorithm> // std::all_of
|
|
|
|
#include <cstddef>
|
|
|
|
#include <string_view>
|
|
|
|
#include <utility> // std::move
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#else
|
|
|
|
#include <cerrno>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <fmt/core.h>
|
|
|
|
|
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/global-ip-cache.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
2023-06-24 19:30:37 +00:00
|
|
|
#include "libtransmission/tr-macros.h"
|
2023-05-05 17:17:40 +00:00
|
|
|
#include "libtransmission/utils.h"
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
using namespace std::literals;
|
|
|
|
|
2023-06-11 23:27:01 +00:00
|
|
|
static_assert(TR_AF_INET == 0);
|
|
|
|
static_assert(TR_AF_INET6 == 1);
|
|
|
|
|
2023-06-24 19:30:37 +00:00
|
|
|
auto TR_CONSTEXPR23 protocol_str(tr_address_type type) noexcept
|
2023-06-11 23:27:01 +00:00
|
|
|
{
|
2023-06-24 19:30:37 +00:00
|
|
|
static auto TR_CONSTEXPR23 map = std::array{ "IPv4"sv, "IPv6"sv };
|
|
|
|
return map[type];
|
2023-06-11 23:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto constexpr IPQueryServices = std::array{ std::array{ "https://ip4.transmissionbt.com/"sv },
|
|
|
|
std::array{ "https://ip6.transmissionbt.com/"sv } };
|
|
|
|
|
2023-05-05 17:17:40 +00:00
|
|
|
auto constexpr UpkeepInterval = 30min;
|
|
|
|
auto constexpr RetryUpkeepInterval = 30s;
|
|
|
|
|
2023-07-04 21:33:58 +00:00
|
|
|
// Normally there should only ever be 1 cache instance during the entire program execution
|
|
|
|
// This is a counter only to cater for SessionTest.honorsSettings
|
|
|
|
std::size_t cache_exists = 0;
|
|
|
|
|
2023-05-05 17:17:40 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2023-06-11 23:27:01 +00:00
|
|
|
// Functions contained in global_source_ip_helpers are modified from code
|
2023-05-05 17:17:40 +00:00
|
|
|
// by Juliusz Chroboczek and is covered under the same license as dht.cc.
|
|
|
|
// Please feel free to copy them into your software if it can help
|
|
|
|
// unbreaking the double-stack Internet.
|
2023-06-11 23:27:01 +00:00
|
|
|
namespace global_source_ip_helpers
|
2023-05-05 17:17:40 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
// Get the source address used for a given destination address.
|
|
|
|
// Since there is no official interface to get this information,
|
|
|
|
// we create a connected UDP socket (connected UDP... hmm...)
|
|
|
|
// and check its source address.
|
|
|
|
//
|
|
|
|
// Since it's a UDP socket, this doesn't actually send any packets
|
|
|
|
[[nodiscard]] std::optional<tr_address> get_source_address(
|
|
|
|
tr_address const& dst_addr,
|
|
|
|
tr_port dst_port,
|
|
|
|
tr_address const& bind_addr,
|
|
|
|
int& err_out) noexcept
|
|
|
|
{
|
|
|
|
TR_ASSERT(dst_addr.type == bind_addr.type);
|
|
|
|
|
|
|
|
auto const save = errno;
|
|
|
|
|
|
|
|
auto const [dst_ss, dst_sslen] = dst_addr.to_sockaddr(dst_port);
|
|
|
|
auto const [bind_ss, bind_sslen] = bind_addr.to_sockaddr(tr_port::fromHost(0));
|
|
|
|
if (auto const sock = socket(dst_ss.ss_family, SOCK_DGRAM, 0); sock != TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
if (bind(sock, reinterpret_cast<sockaddr const*>(&bind_ss), bind_sslen) == 0)
|
|
|
|
{
|
|
|
|
if (connect(sock, reinterpret_cast<sockaddr const*>(&dst_ss), dst_sslen) == 0)
|
|
|
|
{
|
|
|
|
auto src_ss = sockaddr_storage{};
|
|
|
|
auto src_sslen = socklen_t{ sizeof(src_ss) };
|
|
|
|
if (getsockname(sock, reinterpret_cast<sockaddr*>(&src_ss), &src_sslen) == 0)
|
|
|
|
{
|
|
|
|
if (auto const addrport = tr_address::from_sockaddr(reinterpret_cast<sockaddr*>(&src_ss)); addrport)
|
|
|
|
{
|
|
|
|
tr_net_close_socket(sock);
|
|
|
|
errno = save;
|
|
|
|
return addrport->first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_net_close_socket(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
err_out = errno;
|
|
|
|
errno = save;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::optional<tr_address> get_global_source_address(tr_address const& bind_addr, int& err_out) noexcept
|
|
|
|
{
|
|
|
|
// Pick some destination address to pretend to send a packet to
|
2023-06-24 19:30:37 +00:00
|
|
|
static auto constexpr DstIP = std::array{ "91.121.74.28"sv, "2001:1890:1112:1::20"sv };
|
|
|
|
auto const dst_addr = tr_address::from_string(DstIP[bind_addr.type]);
|
2023-05-05 17:17:40 +00:00
|
|
|
auto const dst_port = tr_port::fromHost(6969);
|
|
|
|
|
|
|
|
// In order for address selection to work right,
|
|
|
|
// this should be a global unicast address, not Teredo or 6to4
|
|
|
|
TR_ASSERT(dst_addr && dst_addr->is_global_unicast_address());
|
|
|
|
|
|
|
|
if (dst_addr)
|
|
|
|
{
|
|
|
|
return get_source_address(*dst_addr, dst_port, bind_addr, err_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-06-11 23:27:01 +00:00
|
|
|
} // namespace global_source_ip_helpers
|
2023-05-05 17:17:40 +00:00
|
|
|
} // namespace
|
|
|
|
|
2023-06-11 23:27:01 +00:00
|
|
|
tr_global_ip_cache::tr_global_ip_cache(Mediator& mediator_in)
|
|
|
|
: mediator_{ mediator_in }
|
|
|
|
, upkeep_timers_{ mediator_in.timer_maker().create(), mediator_in.timer_maker().create() }
|
2023-05-05 17:17:40 +00:00
|
|
|
{
|
|
|
|
static_assert(TR_AF_INET == 0);
|
|
|
|
static_assert(TR_AF_INET6 == 1);
|
|
|
|
static_assert(NUM_TR_AF_INET_TYPES == 2);
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < NUM_TR_AF_INET_TYPES; ++i)
|
|
|
|
{
|
|
|
|
auto const type = static_cast<tr_address_type>(i);
|
|
|
|
auto const cb = [this, type]()
|
|
|
|
{
|
|
|
|
update_addr(type);
|
|
|
|
};
|
2023-05-06 04:11:05 +00:00
|
|
|
upkeep_timers_[i]->set_callback(cb);
|
2023-05-05 17:17:40 +00:00
|
|
|
start_timer(type, UpkeepInterval);
|
|
|
|
}
|
2023-07-04 21:33:58 +00:00
|
|
|
|
|
|
|
++cache_exists;
|
2023-05-05 17:17:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-11 23:27:01 +00:00
|
|
|
std::unique_ptr<tr_global_ip_cache> tr_global_ip_cache::create(tr_global_ip_cache::Mediator& mediator_in)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<tr_global_ip_cache>(new tr_global_ip_cache(mediator_in));
|
|
|
|
}
|
|
|
|
|
2023-05-05 17:17:40 +00:00
|
|
|
tr_global_ip_cache::~tr_global_ip_cache()
|
|
|
|
{
|
|
|
|
// Destroying mutex while someone owns it is undefined behaviour, so we acquire it first
|
|
|
|
auto const locks = std::scoped_lock{ is_updating_mutex_[TR_AF_INET], is_updating_mutex_[TR_AF_INET6],
|
|
|
|
global_addr_mutex_[TR_AF_INET], global_addr_mutex_[TR_AF_INET6],
|
|
|
|
source_addr_mutex_[TR_AF_INET], source_addr_mutex_[TR_AF_INET6] };
|
|
|
|
|
2023-07-04 21:33:58 +00:00
|
|
|
if (std::all_of(
|
|
|
|
std::begin(is_updating_),
|
|
|
|
std::end(is_updating_),
|
|
|
|
[](is_updating_t const& v) { return v == is_updating_t::ABORT; }))
|
|
|
|
{
|
|
|
|
tr_logAddDebug("Destructed while some global IP queries were pending.");
|
|
|
|
}
|
|
|
|
|
|
|
|
--cache_exists;
|
2023-05-05 17:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_global_ip_cache::try_shutdown() noexcept
|
|
|
|
{
|
|
|
|
for (auto& timer : upkeep_timers_)
|
|
|
|
{
|
|
|
|
timer->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < NUM_TR_AF_INET_TYPES; ++i)
|
|
|
|
{
|
|
|
|
auto const lock = std::unique_lock{ is_updating_mutex_[i], std::try_to_lock };
|
|
|
|
if (!lock.owns_lock() || is_updating_[i] == is_updating_t::YES)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
is_updating_[i] = is_updating_t::ABORT; // Abort any future updates
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_address tr_global_ip_cache::bind_addr(tr_address_type type) const noexcept
|
|
|
|
{
|
|
|
|
if (type == TR_AF_INET || type == TR_AF_INET6)
|
|
|
|
{
|
2023-06-11 23:27:01 +00:00
|
|
|
if (auto const addr = tr_address::from_string(mediator_.settings_bind_addr(type)); addr && type == addr->type)
|
|
|
|
{
|
|
|
|
return *addr;
|
|
|
|
}
|
|
|
|
return type == TR_AF_INET ? tr_address::any_ipv4() : tr_address::any_ipv6();
|
2023-05-05 17:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TR_ASSERT_MSG(false, "invalid type");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_global_ip_cache::set_global_addr(tr_address_type type, tr_address const& addr) noexcept
|
|
|
|
{
|
|
|
|
if (type == addr.type && addr.is_global_unicast_address())
|
|
|
|
{
|
|
|
|
auto const lock = std::lock_guard{ global_addr_mutex_[addr.type] };
|
|
|
|
global_addr_[addr.type] = addr;
|
|
|
|
tr_logAddTrace(fmt::format("Cached global address {}", addr.display_name()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-11 23:27:01 +00:00
|
|
|
void tr_global_ip_cache::update_addr(tr_address_type type) noexcept
|
2023-05-05 17:17:40 +00:00
|
|
|
{
|
2023-06-11 23:27:01 +00:00
|
|
|
update_source_addr(type);
|
|
|
|
if (global_source_addr(type))
|
2023-05-05 17:17:40 +00:00
|
|
|
{
|
2023-06-11 23:27:01 +00:00
|
|
|
update_global_addr(type);
|
2023-05-05 17:17:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_global_ip_cache::update_global_addr(tr_address_type type) noexcept
|
|
|
|
{
|
|
|
|
TR_ASSERT(has_ip_protocol_[type]);
|
2023-06-11 23:27:01 +00:00
|
|
|
TR_ASSERT(ix_service_[type] < std::size(IPQueryServices[type]));
|
2023-05-05 17:17:40 +00:00
|
|
|
|
|
|
|
if (ix_service_[type] == 0U && !set_is_updating(type))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TR_ASSERT(is_updating_[type] == is_updating_t::YES);
|
|
|
|
|
|
|
|
// Update global address
|
2023-06-24 19:30:37 +00:00
|
|
|
static auto constexpr IPProtocolMap = std::array{ tr_web::FetchOptions::IPProtocol::V4,
|
|
|
|
tr_web::FetchOptions::IPProtocol::V6 };
|
2023-06-11 23:27:01 +00:00
|
|
|
auto options = tr_web::FetchOptions{ IPQueryServices[type][ix_service_[type]],
|
2023-05-05 17:17:40 +00:00
|
|
|
[this, type](tr_web::FetchResponse const& response)
|
2023-07-04 21:33:58 +00:00
|
|
|
{
|
|
|
|
// Check to avoid segfault
|
|
|
|
if (cache_exists != 0)
|
|
|
|
{
|
|
|
|
this->on_response_ip_query(type, response);
|
|
|
|
}
|
|
|
|
},
|
2023-05-05 17:17:40 +00:00
|
|
|
nullptr };
|
2023-06-24 19:30:37 +00:00
|
|
|
options.ip_proto = IPProtocolMap[type];
|
2023-05-05 17:17:40 +00:00
|
|
|
options.sndbuf = 4096;
|
|
|
|
options.rcvbuf = 4096;
|
2023-06-11 23:27:01 +00:00
|
|
|
mediator_.fetch(std::move(options));
|
2023-05-05 17:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_global_ip_cache::update_source_addr(tr_address_type type) noexcept
|
|
|
|
{
|
2023-06-11 23:27:01 +00:00
|
|
|
using namespace global_source_ip_helpers;
|
2023-05-05 17:17:40 +00:00
|
|
|
|
|
|
|
TR_ASSERT(has_ip_protocol_[type]);
|
|
|
|
|
|
|
|
if (!set_is_updating(type))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TR_ASSERT(is_updating_[type] == is_updating_t::YES);
|
|
|
|
|
2023-06-11 23:27:01 +00:00
|
|
|
auto const protocol = protocol_str(type);
|
2023-05-05 17:17:40 +00:00
|
|
|
|
2023-06-11 23:27:01 +00:00
|
|
|
auto err = int{ 0 };
|
2023-05-05 17:17:40 +00:00
|
|
|
auto const& source_addr = get_global_source_address(bind_addr(type), err);
|
|
|
|
if (source_addr)
|
|
|
|
{
|
|
|
|
set_source_addr(*source_addr);
|
|
|
|
tr_logAddInfo(fmt::format(
|
|
|
|
_("Successfully updated source {protocol} address to {ip}"),
|
|
|
|
fmt::arg("protocol", protocol),
|
|
|
|
fmt::arg("ip", source_addr->display_name())));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Stop the update process since we have no public internet connectivity
|
|
|
|
unset_addr(type);
|
2023-05-06 04:11:05 +00:00
|
|
|
upkeep_timers_[type]->set_interval(RetryUpkeepInterval);
|
2023-06-11 23:27:01 +00:00
|
|
|
tr_logAddDebug(fmt::format("Couldn't obtain source {} address", protocol));
|
2023-05-05 17:17:40 +00:00
|
|
|
if (err == EAFNOSUPPORT)
|
|
|
|
{
|
|
|
|
stop_timer(type); // No point in retrying
|
|
|
|
has_ip_protocol_[type] = false;
|
|
|
|
tr_logAddInfo(fmt::format(_("Your machine does not support {protocol}"), fmt::arg("protocol", protocol)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unset_is_updating(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_global_ip_cache::on_response_ip_query(tr_address_type type, tr_web::FetchResponse const& response) noexcept
|
|
|
|
{
|
|
|
|
TR_ASSERT(is_updating_[type] == is_updating_t::YES);
|
2023-06-11 23:27:01 +00:00
|
|
|
TR_ASSERT(ix_service_[type] < std::size(IPQueryServices[type]));
|
2023-05-05 17:17:40 +00:00
|
|
|
|
2023-06-11 23:27:01 +00:00
|
|
|
auto const protocol = protocol_str(type);
|
|
|
|
auto success = false;
|
2023-05-05 17:17:40 +00:00
|
|
|
|
|
|
|
if (response.status == 200 /* HTTP_OK */)
|
|
|
|
{
|
|
|
|
// Update member
|
2023-06-30 14:49:58 +00:00
|
|
|
if (auto const addr = tr_address::from_string(tr_strv_strip(response.body)); addr && set_global_addr(type, *addr))
|
2023-05-05 17:17:40 +00:00
|
|
|
{
|
|
|
|
success = true;
|
2023-05-06 04:11:05 +00:00
|
|
|
upkeep_timers_[type]->set_interval(UpkeepInterval);
|
2023-05-05 17:17:40 +00:00
|
|
|
|
|
|
|
tr_logAddInfo(fmt::format(
|
|
|
|
_("Successfully updated global {type} address to {ip} using {url}"),
|
|
|
|
fmt::arg("type", protocol),
|
|
|
|
fmt::arg("ip", addr->display_name()),
|
2023-06-11 23:27:01 +00:00
|
|
|
fmt::arg("url", IPQueryServices[type][ix_service_[type]])));
|
2023-05-05 17:17:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try next IP query URL
|
|
|
|
if (!success)
|
|
|
|
{
|
2023-07-04 21:33:58 +00:00
|
|
|
if (++ix_service_[type] < std::size(IPQueryServices[type]))
|
|
|
|
{
|
|
|
|
update_global_addr(type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-05 17:17:40 +00:00
|
|
|
tr_logAddDebug(fmt::format("Couldn't obtain global {} address", protocol));
|
|
|
|
unset_global_addr(type);
|
2023-05-06 04:11:05 +00:00
|
|
|
upkeep_timers_[type]->set_interval(RetryUpkeepInterval);
|
2023-05-05 17:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ix_service_[type] = 0U;
|
|
|
|
unset_is_updating(type);
|
|
|
|
}
|
2023-06-11 23:27:01 +00:00
|
|
|
|
|
|
|
void tr_global_ip_cache::unset_global_addr(tr_address_type type) noexcept
|
|
|
|
{
|
|
|
|
auto const lock = std::lock_guard{ global_addr_mutex_[type] };
|
|
|
|
global_addr_[type].reset();
|
|
|
|
tr_logAddTrace(fmt::format("Unset {} global address cache", protocol_str(type)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_global_ip_cache::set_source_addr(tr_address const& addr) noexcept
|
|
|
|
{
|
|
|
|
auto const lock = std::lock_guard{ source_addr_mutex_[addr.type] };
|
|
|
|
source_addr_[addr.type] = addr;
|
|
|
|
tr_logAddTrace(fmt::format("Cached source address {}", addr.display_name()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_global_ip_cache::unset_addr(tr_address_type type) noexcept
|
|
|
|
{
|
|
|
|
auto const lock = std::lock_guard{ source_addr_mutex_[type] };
|
|
|
|
source_addr_[type].reset();
|
|
|
|
tr_logAddTrace(fmt::format("Unset {} source address cache", protocol_str(type)));
|
|
|
|
|
|
|
|
// No public internet connectivity means no global IP address
|
|
|
|
unset_global_addr(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_global_ip_cache::set_is_updating(tr_address_type type) noexcept
|
|
|
|
{
|
|
|
|
auto lock = std::unique_lock{ is_updating_mutex_[type] };
|
|
|
|
is_updating_cv_[type].wait(
|
|
|
|
lock,
|
|
|
|
[this, type]() { return is_updating_[type] == is_updating_t::NO || is_updating_[type] == is_updating_t::ABORT; });
|
|
|
|
if (is_updating_[type] != is_updating_t::NO)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
is_updating_[type] = is_updating_t::YES;
|
|
|
|
lock.unlock();
|
|
|
|
is_updating_cv_[type].notify_one();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_global_ip_cache::unset_is_updating(tr_address_type type) noexcept
|
|
|
|
{
|
|
|
|
TR_ASSERT(is_updating_[type] == is_updating_t::YES);
|
|
|
|
auto lock = std::unique_lock{ is_updating_mutex_[type] };
|
|
|
|
is_updating_[type] = is_updating_t::NO;
|
|
|
|
lock.unlock();
|
|
|
|
is_updating_cv_[type].notify_one();
|
|
|
|
}
|