fixup! refactor: re-organise net.h member functions (#5878) (#5933)

* fix: check if address is valid in `tr_address::is_any()`

* refactor: add static implementation of `tr_address::is_valid()`
This commit is contained in:
Yat Ho 2023-10-25 23:53:46 +08:00 committed by GitHub
parent 6e7fdef721
commit 5a6f0a5623
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 7 deletions

View File

@ -186,7 +186,7 @@ bool tr_global_ip_cache::try_shutdown() noexcept
tr_address tr_global_ip_cache::bind_addr(tr_address_type type) const noexcept
{
if (type == TR_AF_INET || type == TR_AF_INET6)
if (tr_address::is_valid(type))
{
if (auto const addr = tr_address::from_string(mediator_.settings_bind_addr(type)); addr && type == addr->type)
{

View File

@ -243,7 +243,7 @@ struct tr_address
static auto constexpr CompactAddrBytes = std::array{ 4U, 16U };
static_assert(std::size(CompactAddrBytes) == NUM_TR_AF_INET_TYPES);
[[nodiscard]] static auto constexpr any(tr_address_type type) noexcept
[[nodiscard]] static auto any(tr_address_type type) noexcept
{
switch (type)
{
@ -252,18 +252,24 @@ struct tr_address
case TR_AF_INET6:
return tr_address{ TR_AF_INET6, { IN6ADDR_ANY_INIT } };
default:
TR_ASSERT_MSG(false, "invalid type");
return tr_address{};
}
}
[[nodiscard]] constexpr auto is_valid() const noexcept
[[nodiscard]] static constexpr auto is_valid(tr_address_type type) noexcept
{
return type == TR_AF_INET || type == TR_AF_INET6;
}
[[nodiscard]] constexpr auto is_valid() const noexcept
{
return is_valid(type);
}
[[nodiscard]] auto is_any() const noexcept
{
return *this == any(type);
return is_valid() ? *this == any(type) : false;
}
};

View File

@ -818,7 +818,7 @@ public:
[[nodiscard]] bool has_ip_protocol(tr_address_type type) const noexcept
{
TR_ASSERT(type == TR_AF_INET || type == TR_AF_INET6);
TR_ASSERT(tr_address::is_valid(type));
return global_ip_cache_->has_ip_protocol(type);
}
@ -826,7 +826,7 @@ public:
[[nodiscard]] std::optional<tr_address> global_address(tr_address_type type) const noexcept
{
TR_ASSERT(type == TR_AF_INET || type == TR_AF_INET6);
TR_ASSERT(tr_address::is_valid(type));
return global_ip_cache_->global_addr(type);
}
@ -837,7 +837,7 @@ public:
[[nodiscard]] std::optional<tr_address> global_source_address(tr_address_type type) const noexcept
{
TR_ASSERT(type == TR_AF_INET || type == TR_AF_INET6);
TR_ASSERT(tr_address::is_valid(type));
return global_ip_cache_->global_source_addr(type);
}