From c7a24a6ee5bea5f8dd5595cf1a4ca2ac9a719469 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Tue, 22 Oct 2024 17:31:35 +0200 Subject: [PATCH] fix: make printing of IPv6 address portable this is compatible to musl and glibc, which it wasn't previously --- src/sockets/socket.cpp | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/sockets/socket.cpp b/src/sockets/socket.cpp index 6786d01..7a78533 100644 --- a/src/sockets/socket.cpp +++ b/src/sockets/socket.cpp @@ -12,6 +12,12 @@ #include #include + +#ifndef _WIN32 +#define IPV6_INFO_PORT(ipv6_info) ((ipv6_info)->sin6_addr.s6_addr) +#endif + + namespace c2k { [[nodiscard]] static constexpr int to_ai_family(AddressFamily const family) { switch (family) { @@ -241,22 +247,22 @@ namespace c2k { auto const ipv6_info = reinterpret_cast(&address); auto stream = std::stringstream{}; stream << std::hex << std::setfill('0'); - stream << std::setw(2) << ipv6_info->sin6_addr.__in6_u.__u6_addr8[0] << std::setw(2) - << ipv6_info->sin6_addr.__in6_u.__u6_addr8[1] << ':'; - stream << std::setw(2) << ipv6_info->sin6_addr.__in6_u.__u6_addr8[2] << std::setw(2) - << ipv6_info->sin6_addr.__in6_u.__u6_addr8[3] << ':'; - stream << std::setw(2) << ipv6_info->sin6_addr.__in6_u.__u6_addr8[4] << std::setw(2) - << ipv6_info->sin6_addr.__in6_u.__u6_addr8[5] << ':'; - stream << std::setw(2) << ipv6_info->sin6_addr.__in6_u.__u6_addr8[6] << std::setw(2) - << ipv6_info->sin6_addr.__in6_u.__u6_addr8[7] << ':'; - stream << std::setw(2) << ipv6_info->sin6_addr.__in6_u.__u6_addr8[8] << std::setw(2) - << ipv6_info->sin6_addr.__in6_u.__u6_addr8[9] << ':'; - stream << std::setw(2) << ipv6_info->sin6_addr.__in6_u.__u6_addr8[10] << std::setw(2) - << ipv6_info->sin6_addr.__in6_u.__u6_addr8[11] << ':'; - stream << std::setw(2) << ipv6_info->sin6_addr.__in6_u.__u6_addr8[12] << std::setw(2) - << ipv6_info->sin6_addr.__in6_u.__u6_addr8[13] << ':'; - stream << std::setw(2) << ipv6_info->sin6_addr.__in6_u.__u6_addr8[14] << std::setw(2) - << ipv6_info->sin6_addr.__in6_u.__u6_addr8[15]; + stream << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[0] << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[1] + << ':'; + stream << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[2] << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[3] + << ':'; + stream << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[4] << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[5] + << ':'; + stream << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[6] << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[7] + << ':'; + stream << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[8] << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[9] + << ':'; + stream << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[10] << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[11] + << ':'; + stream << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[12] << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[13] + << ':'; + stream << std::setw(2) << IPV6_INFO_PORT(ipv6_info)[14] << std::setw(2) + << IPV6_INFO_PORT(ipv6_info)[15]; return AddressInfo{ AddressFamily::Ipv6, std::move(stream).str(), from_network_byte_order(static_cast(ipv6_info->sin6_port)) };