From 5a6f0a5623bf62771c8e1d0599d839a285cfb198 Mon Sep 17 00:00:00 2001 From: Yat Ho Date: Wed, 25 Oct 2023 23:53:46 +0800 Subject: [PATCH] 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()` --- libtransmission/global-ip-cache.cc | 2 +- libtransmission/net.h | 12 +++++++++--- libtransmission/session.h | 6 +++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/libtransmission/global-ip-cache.cc b/libtransmission/global-ip-cache.cc index b68731d7b..0ed9f92da 100644 --- a/libtransmission/global-ip-cache.cc +++ b/libtransmission/global-ip-cache.cc @@ -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) { diff --git a/libtransmission/net.h b/libtransmission/net.h index ce9a80ed9..274ff88b8 100644 --- a/libtransmission/net.h +++ b/libtransmission/net.h @@ -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; } }; diff --git a/libtransmission/session.h b/libtransmission/session.h index 258be8e56..10dc29cb1 100644 --- a/libtransmission/session.h +++ b/libtransmission/session.h @@ -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 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 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); }