mirror of
https://github.com/transmission/transmission
synced 2024-12-25 01:03:01 +00:00
refactor: add tr_address::fromCompact() (#2961)
This commit is contained in:
parent
a52edbcbbe
commit
a25da4f376
5 changed files with 46 additions and 32 deletions
|
@ -253,7 +253,8 @@ void tr_announcerParseHttpAnnounceResponse(tr_announce_response& response, std::
|
|||
}
|
||||
else if (key == "external ip"sv && std::size(value) == 4)
|
||||
{
|
||||
response_.external_ip = tr_address::from_4byte_ipv4(value);
|
||||
auto const [addr, out] = tr_address::fromCompact4(reinterpret_cast<uint8_t const*>(std::data(value)));
|
||||
response_.external_ip = addr;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -148,16 +148,6 @@ std::string tr_address::to_string(tr_port port) const
|
|||
return fmt::format(FMT_STRING("[{:s}]:{:d}"), std::data(addrbuf), port.host());
|
||||
}
|
||||
|
||||
tr_address tr_address::from_4byte_ipv4(std::string_view in)
|
||||
{
|
||||
TR_ASSERT(std::size(in) == 4);
|
||||
|
||||
auto addr = tr_address{};
|
||||
addr.type = TR_AF_INET;
|
||||
std::copy_n(std::begin(in), 4, reinterpret_cast<char*>(&addr.addr));
|
||||
return addr;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compare two tr_address structures.
|
||||
* Returns:
|
||||
|
@ -845,13 +835,41 @@ struct tr_peer_socket tr_peer_socket_utp_create(struct UTPSocket* const handle)
|
|||
|
||||
/// tr_port
|
||||
|
||||
static auto constexpr PortLen = size_t{ 2 };
|
||||
|
||||
std::pair<tr_port, uint8_t const*> tr_port::fromCompact(uint8_t const* compact) noexcept
|
||||
{
|
||||
static auto constexpr PortLen = size_t{ 2 };
|
||||
|
||||
static_assert(PortLen == sizeof(uint16_t));
|
||||
auto nport = uint16_t{};
|
||||
std::copy_n(compact, PortLen, reinterpret_cast<uint8_t*>(&nport));
|
||||
compact += PortLen;
|
||||
|
||||
return std::make_pair(tr_port::fromNetwork(nport), compact);
|
||||
}
|
||||
|
||||
/// tr_address
|
||||
|
||||
std::pair<tr_address, uint8_t const*> tr_address::fromCompact4(uint8_t const* compact) noexcept
|
||||
{
|
||||
static auto constexpr Addr4Len = size_t{ 4 };
|
||||
|
||||
auto address = tr_address{};
|
||||
static_assert(sizeof(address.addr.addr4) == Addr4Len);
|
||||
address.type = TR_AF_INET;
|
||||
std::copy_n(compact, Addr4Len, reinterpret_cast<uint8_t*>(&address.addr));
|
||||
compact += Addr4Len;
|
||||
|
||||
return std::make_pair(address, compact);
|
||||
}
|
||||
|
||||
std::pair<tr_address, uint8_t const*> tr_address::fromCompact6(uint8_t const* compact) noexcept
|
||||
{
|
||||
static auto constexpr Addr6Len = size_t{ 16 };
|
||||
|
||||
auto address = tr_address{};
|
||||
address.type = TR_AF_INET6;
|
||||
std::copy_n(compact, Addr6Len, reinterpret_cast<uint8_t*>(&address.addr.addr6.s6_addr));
|
||||
compact += Addr6Len;
|
||||
|
||||
return std::make_pair(address, compact);
|
||||
}
|
||||
|
|
|
@ -151,19 +151,12 @@ private:
|
|||
|
||||
struct tr_address
|
||||
{
|
||||
static tr_address from_4byte_ipv4(std::string_view in);
|
||||
[[nodiscard]] static std::optional<tr_address> from_string(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;
|
||||
|
||||
static std::optional<tr_address> from_string(std::string_view str);
|
||||
|
||||
std::string to_string() const;
|
||||
std::string to_string(tr_port port) const;
|
||||
|
||||
tr_address_type type;
|
||||
union
|
||||
{
|
||||
struct in6_addr addr6;
|
||||
struct in_addr addr4;
|
||||
} addr;
|
||||
[[nodiscard]] std::string to_string() const;
|
||||
[[nodiscard]] std::string to_string(tr_port port) const;
|
||||
|
||||
[[nodiscard]] int compare(tr_address const& that) const noexcept
|
||||
{
|
||||
|
@ -184,6 +177,13 @@ struct tr_address
|
|||
{
|
||||
return compare(that) > 0;
|
||||
}
|
||||
|
||||
tr_address_type type;
|
||||
union
|
||||
{
|
||||
struct in6_addr addr6;
|
||||
struct in_addr addr4;
|
||||
} addr;
|
||||
};
|
||||
|
||||
extern tr_address const tr_inaddr_any;
|
||||
|
|
|
@ -1128,9 +1128,7 @@ std::vector<tr_pex> tr_peerMgrCompactToPex(void const* compact, size_t compactLe
|
|||
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
{
|
||||
pex[i].addr.type = TR_AF_INET;
|
||||
std::copy_n(walk, 4, reinterpret_cast<uint8_t*>(&pex[i].addr.addr));
|
||||
walk += 4;
|
||||
std::tie(pex[i].addr, walk) = tr_address::fromCompact4(walk);
|
||||
std::tie(pex[i].port, walk) = tr_port::fromCompact(walk);
|
||||
|
||||
if (added_f != nullptr && n == added_f_len)
|
||||
|
@ -1150,9 +1148,7 @@ std::vector<tr_pex> tr_peerMgrCompact6ToPex(void const* compact, size_t compactL
|
|||
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
{
|
||||
pex[i].addr.type = TR_AF_INET6;
|
||||
std::copy_n(walk, 16, reinterpret_cast<uint8_t*>(&pex[i].addr.addr.addr6.s6_addr));
|
||||
walk += 16;
|
||||
std::tie(pex[i].addr, walk) = tr_address::fromCompact6(walk);
|
||||
std::tie(pex[i].port, walk) = tr_port::fromCompact(walk);
|
||||
|
||||
if (added_f != nullptr && n == added_f_len)
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
#include <cstring> /* memcpy(), memset(), memchr(), strlen() */
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
|
Loading…
Reference in a new issue