Add IPv6 helpers
This commit is contained in:
@@ -77,6 +77,42 @@ namespace ColumnLynx::Net {
|
||||
return ntohl(addr.s_addr);
|
||||
}
|
||||
|
||||
static inline std::string ipv6ToString(IPv6Addr &ip,
|
||||
bool flip = false)
|
||||
{
|
||||
struct in6_addr addr;
|
||||
|
||||
if (flip) {
|
||||
IPv6Addr flipped;
|
||||
for (size_t i = 0; i < 16; ++i)
|
||||
flipped[i] = ip[15 - i];
|
||||
memcpy(addr.s6_addr, flipped.data(), 16);
|
||||
} else {
|
||||
memcpy(addr.s6_addr, ip.data(), 16);
|
||||
}
|
||||
|
||||
char buf[INET6_ADDRSTRLEN];
|
||||
if (!inet_ntop(AF_INET6, &addr, buf, sizeof(buf)))
|
||||
return "::"; // Fallback
|
||||
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
static inline IPv6Addr stringToIpv6(const std::string &ipStr)
|
||||
{
|
||||
IPv6Addr result{};
|
||||
struct in6_addr addr;
|
||||
|
||||
if (inet_pton(AF_INET6, ipStr.c_str(), &addr) != 1) {
|
||||
// "::"
|
||||
result.fill(0);
|
||||
return result;
|
||||
}
|
||||
|
||||
memcpy(result.data(), addr.s6_addr, 16);
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline uint32_t prefixLengthToNetmask(uint8_t prefixLen) {
|
||||
if (prefixLen == 0) return 0;
|
||||
uint32_t mask = (0xFFFFFFFF << (32 - prefixLen)) & 0xFFFFFFFF;
|
||||
|
||||
Reference in New Issue
Block a user