refactor: add tr_address.readable (#2962)

This commit is contained in:
Charles Kerr
2022-04-21 18:37:02 -05:00
committed by GitHub
parent a25da4f376
commit 41cb8cbc91
8 changed files with 88 additions and 54 deletions

View File

@@ -122,32 +122,6 @@ bool tr_address_from_string(tr_address* dst, std::string_view src)
return tr_address_from_string(dst, std::data(buf));
}
std::optional<tr_address> tr_address::from_string(std::string_view address_str)
{
auto addr = tr_address{};
if (!tr_address_from_string(&addr, address_str))
{
return {};
}
return addr;
}
std::string tr_address::to_string() const
{
auto addrbuf = std::array<char, TR_ADDRSTRLEN>{};
tr_address_to_string_with_buf(this, std::data(addrbuf), std::size(addrbuf));
return std::data(addrbuf);
}
std::string tr_address::to_string(tr_port port) const
{
auto addrbuf = std::array<char, TR_ADDRSTRLEN>{};
tr_address_to_string_with_buf(this, std::data(addrbuf), std::size(addrbuf));
return fmt::format(FMT_STRING("[{:s}]:{:d}"), std::data(addrbuf), port.host());
}
/*
* Compare two tr_address structures.
* Returns:
@@ -368,7 +342,7 @@ struct tr_peer_socket tr_netOpenPeerSocket(tr_session* session, tr_address const
{
tr_logAddWarn(fmt::format(
_("Couldn't set source address {address} on {socket}: {error} ({error_code})"),
fmt::arg("address", source_addr->to_string()),
fmt::arg("address", source_addr->readable()),
fmt::arg("socket", s),
fmt::arg("error", tr_net_strerror(sockerrno)),
fmt::arg("error_code", sockerrno)));
@@ -389,7 +363,7 @@ struct tr_peer_socket tr_netOpenPeerSocket(tr_session* session, tr_address const
tr_logAddWarn(fmt::format(
_("Couldn't connect socket {socket} to {address}:{port}: {error} ({error_code})"),
fmt::arg("socket", s),
fmt::arg("address", addr->to_string()),
fmt::arg("address", addr->readable()),
fmt::arg("port", port.host()),
fmt::arg("error", tr_net_strerror(tmperrno)),
fmt::arg("error_code", tmperrno)));
@@ -500,7 +474,7 @@ static tr_socket_t tr_netBindTCPImpl(tr_address const* addr, tr_port port, bool
err == EADDRINUSE ?
_("Couldn't bind port {port} on {address}: {error} ({error_code}) -- Is another copy of Transmission already running?") :
_("Couldn't bind port {port} on {address}: {error} ({error_code})"),
fmt::arg("address", addr->to_string()),
fmt::arg("address", addr->readable()),
fmt::arg("port", port.host()),
fmt::arg("error", tr_net_strerror(err)),
fmt::arg("error_code", err)));
@@ -513,7 +487,7 @@ static tr_socket_t tr_netBindTCPImpl(tr_address const* addr, tr_port port, bool
if (!suppressMsgs)
{
tr_logAddDebug(fmt::format(FMT_STRING("Bound socket {:d} to port {:d} on {:s}"), fd, port.host(), addr->to_string()));
tr_logAddDebug(fmt::format(FMT_STRING("Bound socket {:d} to port {:d} on {:s}"), fd, port.host(), addr->readable()));
}
#ifdef TCP_FASTOPEN
@@ -849,6 +823,54 @@ std::pair<tr_port, uint8_t const*> tr_port::fromCompact(uint8_t const* compact)
/// tr_address
std::optional<tr_address> tr_address::fromString(std::string_view address_str)
{
auto addr = tr_address{};
if (!tr_address_from_string(&addr, address_str))
{
return {};
}
return addr;
}
template<typename OutputIt>
OutputIt tr_address::readable(OutputIt out) const
{
auto buf = std::array<char, INET6_ADDRSTRLEN>{};
tr_address_to_string_with_buf(this, std::data(buf), std::size(buf));
return fmt::format_to(out, FMT_STRING("{:s}"), std::data(buf));
}
template char* tr_address::readable<char*>(char*) const;
std::string tr_address::readable() const
{
auto buf = std::string{};
buf.reserve(INET6_ADDRSTRLEN);
this->readable(std::back_inserter(buf));
return buf;
}
template<typename OutputIt>
OutputIt tr_address::readable(OutputIt out, tr_port port) const
{
auto buf = std::array<char, INET6_ADDRSTRLEN>{};
tr_address_to_string_with_buf(this, std::data(buf), std::size(buf));
return fmt::format_to(out, FMT_STRING("[{:s}]:{:d}"), std::data(buf), port.host());
}
template char* tr_address::readable<char*>(char*, tr_port) const;
std::string tr_address::readable(tr_port port) const
{
auto buf = std::string{};
buf.reserve(INET6_ADDRSTRLEN + 9);
this->readable(std::back_inserter(buf), port);
return buf;
}
std::pair<tr_address, uint8_t const*> tr_address::fromCompact4(uint8_t const* compact) noexcept
{
static auto constexpr Addr4Len = size_t{ 4 };
@@ -873,3 +895,8 @@ std::pair<tr_address, uint8_t const*> tr_address::fromCompact6(uint8_t const* co
return std::make_pair(address, compact);
}
int tr_address::compare(tr_address const& that) const noexcept // <=>
{
return tr_address_compare(this, &that);
}

View File

@@ -151,31 +151,38 @@ private:
struct tr_address
{
[[nodiscard]] static std::optional<tr_address> from_string(std::string_view str);
[[nodiscard]] static std::optional<tr_address> fromString(std::string_view str);
[[nodiscard]] static std::pair<tr_address, uint8_t const*> fromCompact4(uint8_t const* compact) noexcept;
[[nodiscard]] static std::pair<tr_address, uint8_t const*> fromCompact6(uint8_t const* compact) noexcept;
[[nodiscard]] std::string to_string() const;
[[nodiscard]] std::string to_string(tr_port port) const;
// human-readable formatting
[[nodiscard]] int compare(tr_address const& that) const noexcept
{
return tr_address_compare(this, &that);
}
template<typename OutputIt>
OutputIt readable(OutputIt out) const;
template<typename OutputIt>
OutputIt readable(OutputIt out, tr_port) const;
[[nodiscard]] std::string readable() const;
[[nodiscard]] std::string readable(tr_port) const;
// comparisons
[[nodiscard]] int compare(tr_address const& that) const noexcept;
[[nodiscard]] bool operator==(tr_address const& that) const noexcept
{
return compare(that) == 0;
return this->compare(that) == 0;
}
[[nodiscard]] bool operator<(tr_address const& that) const noexcept
{
return compare(that) < 0;
return this->compare(that) < 0;
}
[[nodiscard]] bool operator>(tr_address const& that) const noexcept
{
return compare(that) > 0;
return this->compare(that) > 0;
}
tr_address_type type;

View File

@@ -918,7 +918,7 @@ tr_address const* tr_peerIoGetAddress(tr_peerIo const* io, tr_port* port)
std::string tr_peerIo::addrStr() const
{
return tr_isPeerIo(this) ? this->addr.to_string(this->port) : "error";
return tr_isPeerIo(this) ? this->addr.readable(this->port) : "error";
}
char const* tr_peerIoGetAddrStr(tr_peerIo const* io, char* buf, size_t buflen)

View File

@@ -58,17 +58,17 @@ struct tr_pex
{
tr_address addr;
tr_port port; /* this field is in network byte order */
uint8_t flags;
uint8_t flags = 0;
std::string_view to_string(char* buf, size_t buflen) const
template<typename OutputIt>
[[nodiscard]] OutputIt readable(OutputIt out) const
{
tr_address_and_port_to_string(buf, buflen, &addr, port);
return buf;
return addr.readable(out, port);
}
[[nodiscard]] std::string to_string() const
[[nodiscard]] std::string readable() const
{
return addr.to_string(port);
return addr.readable(port);
}
};

View File

@@ -151,7 +151,7 @@ std::optional<std::string> tr_session::WebMediator::publicAddress() const
tr_address const* addr = tr_sessionGetPublicAddress(session_, type, &is_default_value);
if (addr != nullptr && !is_default_value)
{
return tr_address_to_string(addr);
return addr->readable();
}
}
@@ -303,7 +303,7 @@ tr_address const* tr_sessionGetPublicAddress(tr_session const* session, int tr_a
if (is_default_value != nullptr && bindinfo != nullptr)
{
*is_default_value = bindinfo->addr.to_string() == default_value;
*is_default_value = bindinfo->addr.readable() == default_value;
}
return bindinfo != nullptr ? &bindinfo->addr : nullptr;

View File

@@ -525,7 +525,7 @@ static int tr_lpdConsiderAnnounce(tr_pex* peer, char const* const msg)
tr_peerMgrAddPex(tor, TR_PEER_FROM_LPD, peer, 1);
tr_logAddDebugTor(
tor,
fmt::format(FMT_STRING("Found a local peer from LPD ({:s})"), peer->addr.to_string(peer->port)));
fmt::format(FMT_STRING("Found a local peer from LPD ({:s})"), peer->addr.readable(peer->port)));
/* periodic reconnectPulse() deals with the rest... */

View File

@@ -309,7 +309,7 @@ void tr_udpInit(tr_session* ss)
auto const error_code = errno;
tr_logAddWarn(fmt::format(
_("Couldn't bind IPv4 socket {address}: {error} ({error_code})"),
fmt::arg("address", public_addr != nullptr ? public_addr->to_string(ss->udp_port) : "?"),
fmt::arg("address", public_addr != nullptr ? public_addr->readable(ss->udp_port) : "?"),
fmt::arg("error", tr_strerror(error_code)),
fmt::arg("error_code", error_code)));
tr_netCloseSocket(ss->udp_socket);

View File

@@ -45,7 +45,7 @@ TEST_F(AnnouncerTest, parseHttpAnnounceResponseNoPeers)
EXPECT_EQ(3, response.seeders);
EXPECT_EQ(0, response.leechers);
EXPECT_EQ(2, response.downloads);
EXPECT_EQ(*tr_address::from_string("1.2.3.4"), response.external_ip);
EXPECT_EQ(*tr_address::fromString("1.2.3.4"), response.external_ip);
EXPECT_EQ(0U, std::size(response.pex));
EXPECT_EQ(0U, std::size(response.pex6));
EXPECT_EQ(""sv, response.errmsg);
@@ -81,7 +81,7 @@ TEST_F(AnnouncerTest, parseHttpAnnounceResponsePexCompact)
if (std::size(response.pex) == 1)
{
EXPECT_EQ("[127.0.0.1]:64551"sv, response.pex[0].to_string());
EXPECT_EQ("[127.0.0.1]:64551"sv, response.pex[0].readable());
}
}
@@ -120,7 +120,7 @@ TEST_F(AnnouncerTest, parseHttpAnnounceResponsePexList)
if (std::size(response.pex) == 1)
{
EXPECT_EQ("[8.8.4.4]:53"sv, response.pex[0].to_string());
EXPECT_EQ("[8.8.4.4]:53"sv, response.pex[0].readable());
}
}