mirror of
https://github.com/matrix-construct/construct
synced 2025-02-18 01:30:12 +01:00
ircd::net: Elaborate ipaddr interface; move apropos string utils from ipport.h
This commit is contained in:
parent
d1e0864590
commit
98a3535e06
3 changed files with 75 additions and 40 deletions
|
@ -21,9 +21,17 @@ namespace ircd::net
|
|||
{
|
||||
union ipaddr;
|
||||
|
||||
const uint128_t &host6(const ipaddr &);
|
||||
const uint32_t &host4(const ipaddr &);
|
||||
uint128_t &host6(ipaddr &);
|
||||
uint32_t &host4(ipaddr &);
|
||||
|
||||
bool operator!(const ipaddr &);
|
||||
bool operator<(const ipaddr &, const ipaddr &);
|
||||
bool operator==(const ipaddr &, const ipaddr &);
|
||||
|
||||
string_view string(const mutable_buffer &out, const uint32_t &);
|
||||
string_view string(const mutable_buffer &out, const uint128_t &);
|
||||
}
|
||||
|
||||
union ircd::net::ipaddr
|
||||
|
@ -60,3 +68,27 @@ ircd::net::operator!(const ipaddr &a)
|
|||
{
|
||||
return !a.v6;
|
||||
}
|
||||
|
||||
inline uint32_t &
|
||||
ircd::net::host4(ipaddr &ipaddr)
|
||||
{
|
||||
return ipaddr.v4;
|
||||
}
|
||||
|
||||
inline ircd::uint128_t &
|
||||
ircd::net::host6(ipaddr &ipaddr)
|
||||
{
|
||||
return ipaddr.v6;
|
||||
}
|
||||
|
||||
inline const uint32_t &
|
||||
ircd::net::host4(const ipaddr &ipaddr)
|
||||
{
|
||||
return ipaddr.v4;
|
||||
}
|
||||
|
||||
inline const ircd::uint128_t &
|
||||
ircd::net::host6(const ipaddr &ipaddr)
|
||||
{
|
||||
return ipaddr.v6;
|
||||
}
|
||||
|
|
|
@ -26,8 +26,6 @@ namespace ircd::net
|
|||
uint128_t &host6(ipport &);
|
||||
uint32_t &host4(ipport &);
|
||||
|
||||
string_view string(const mutable_buffer &out, const uint32_t &);
|
||||
string_view string(const mutable_buffer &out, const uint128_t &);
|
||||
string_view string(const mutable_buffer &out, const ipport &);
|
||||
std::ostream &operator<<(std::ostream &, const ipport &);
|
||||
}
|
||||
|
@ -103,13 +101,13 @@ const
|
|||
inline ircd::uint128_t &
|
||||
ircd::net::host6(ipport &ipp)
|
||||
{
|
||||
return std::get<ipp.IP>(ipp).v6;
|
||||
return host6(std::get<ipp.IP>(ipp));
|
||||
}
|
||||
|
||||
inline const ircd::uint128_t &
|
||||
ircd::net::host6(const ipport &ipp)
|
||||
{
|
||||
return std::get<ipp.IP>(ipp).v6;
|
||||
return host6(std::get<ipp.IP>(ipp));
|
||||
}
|
||||
|
||||
inline bool
|
||||
|
@ -121,13 +119,13 @@ ircd::net::is_v6(const ipport &ipp)
|
|||
inline uint32_t &
|
||||
ircd::net::host4(ipport &ipp)
|
||||
{
|
||||
return std::get<ipp.IP>(ipp).v4;
|
||||
return host4(std::get<ipp.IP>(ipp));
|
||||
}
|
||||
|
||||
inline const uint32_t &
|
||||
ircd::net::host4(const ipport &ipp)
|
||||
{
|
||||
return std::get<ipp.IP>(ipp).v4;
|
||||
return host4(std::get<ipp.IP>(ipp));
|
||||
}
|
||||
|
||||
inline bool
|
||||
|
|
73
ircd/net.cc
73
ircd/net.cc
|
@ -3186,40 +3186,6 @@ ircd::net::operator<<(std::ostream &s, const ipport &t)
|
|||
return s;
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::net::string(const mutable_buffer &buf,
|
||||
const uint32_t &ip)
|
||||
{
|
||||
const auto len
|
||||
{
|
||||
ip::address_v4{ip}.to_string().copy(data(buf), size(buf))
|
||||
};
|
||||
|
||||
return { data(buf), size_t(len) };
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::net::string(const mutable_buffer &buf,
|
||||
const uint128_t &ip)
|
||||
{
|
||||
const auto &pun
|
||||
{
|
||||
reinterpret_cast<const uint8_t (&)[16]>(ip)
|
||||
};
|
||||
|
||||
const auto &punpun
|
||||
{
|
||||
reinterpret_cast<const std::array<uint8_t, 16> &>(pun)
|
||||
};
|
||||
|
||||
const auto len
|
||||
{
|
||||
ip::address_v6{punpun}.to_string().copy(data(buf), size(buf))
|
||||
};
|
||||
|
||||
return { data(buf), size_t(len) };
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::net::string(const mutable_buffer &buf,
|
||||
const ipport &ipp)
|
||||
|
@ -3386,6 +3352,45 @@ ircd::net::ipport::ipport(const uint128_t &ip,
|
|||
port(*this) = p;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// net/ipaddr.h
|
||||
//
|
||||
|
||||
ircd::string_view
|
||||
ircd::net::string(const mutable_buffer &buf,
|
||||
const uint32_t &ip)
|
||||
{
|
||||
const auto len
|
||||
{
|
||||
ip::address_v4{ip}.to_string().copy(data(buf), size(buf))
|
||||
};
|
||||
|
||||
return { data(buf), size_t(len) };
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::net::string(const mutable_buffer &buf,
|
||||
const uint128_t &ip)
|
||||
{
|
||||
const auto &pun
|
||||
{
|
||||
reinterpret_cast<const uint8_t (&)[16]>(ip)
|
||||
};
|
||||
|
||||
const auto &punpun
|
||||
{
|
||||
reinterpret_cast<const std::array<uint8_t, 16> &>(pun)
|
||||
};
|
||||
|
||||
const auto len
|
||||
{
|
||||
ip::address_v6{punpun}.to_string().copy(data(buf), size(buf))
|
||||
};
|
||||
|
||||
return { data(buf), size_t(len) };
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// net/hostport.h
|
||||
|
|
Loading…
Add table
Reference in a new issue