net/addr/sock: Add string conversions for all socket types
Change-Id: Ic961b5b300f5cf16805383e27cbb6f63cd0b1d17
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/include/stdplus/net/addr/sock.hpp b/include/stdplus/net/addr/sock.hpp
index e1f2e74..2830f83 100644
--- a/include/stdplus/net/addr/sock.hpp
+++ b/include/stdplus/net/addr/sock.hpp
@@ -1,13 +1,17 @@
#pragma once
+#include <fmt/core.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <stdplus/net/addr/ip.hpp>
#include <stdplus/numeric/endian.hpp>
+#include <stdplus/numeric/str.hpp>
+#include <stdplus/str/conv.hpp>
#include <stdplus/variant.hpp>
#include <array>
#include <cstdint>
+#include <format>
#include <string_view>
namespace stdplus
@@ -250,4 +254,158 @@
struct IsSockAddr<SockAnyAddr> : std::true_type
{};
+template <>
+struct FromStr<Sock4Addr>
+{
+ template <typename CharT>
+ constexpr Sock4Addr operator()(std::basic_string_view<CharT> sv) const
+ {
+ const auto pos = sv.rfind(':');
+ if (pos == sv.npos)
+ {
+ throw std::invalid_argument("Invalid string for Sock4Addr");
+ }
+ return {FromStr<In4Addr>{}(sv.substr(0, pos)),
+ StrToInt<10, std::uint16_t>{}(sv.substr(pos + 1))};
+ }
+};
+
+template <>
+struct ToStr<Sock4Addr>
+{
+ using type = Sock4Addr;
+ using FromAddr = ToStr<In4Addr>;
+ using FromDec = IntToStr<10, std::uint16_t>;
+ // Addr + sep + port chars
+ static inline constexpr std::size_t buf_size = FromAddr::buf_size + 1 +
+ FromDec::buf_size;
+
+ template <typename CharT>
+ constexpr CharT* operator()(CharT* buf, Sock4Addr v) const noexcept
+ {
+ buf = FromAddr{}(buf, v.addr);
+ (buf++)[0] = ':';
+ return FromDec{}(buf, v.port);
+ }
+};
+
+template <>
+struct FromStr<Sock6Addr>
+{
+ template <typename CharT>
+ constexpr Sock6Addr operator()(std::basic_string_view<CharT> sv) const
+ {
+ const auto pos = sv.rfind(':');
+ if (pos == sv.npos)
+ {
+ throw std::invalid_argument("Invalid string for Sock6Addr");
+ }
+ const auto v6seg = sv.substr(0, pos);
+ if (!v6seg.starts_with('[') || !v6seg.ends_with(']'))
+ {
+ throw std::invalid_argument("Invalid string for Sock6Addr");
+ }
+ return {FromStr<In6Addr>{}(v6seg.substr(1, v6seg.size() - 2)),
+ StrToInt<10, std::uint16_t>{}(sv.substr(pos + 1)), 0};
+ }
+};
+
+template <>
+struct ToStr<Sock6Addr>
+{
+ using type = Sock6Addr;
+ using FromAddr = ToStr<In6Addr>;
+ using FromDec = IntToStr<10, std::uint16_t>;
+ // Addr + sep + port chars
+ static inline constexpr std::size_t buf_size = FromAddr::buf_size + 1 +
+ FromDec::buf_size;
+
+ template <typename CharT>
+ constexpr CharT* operator()(CharT* buf, Sock6Addr v) const noexcept
+ {
+ (buf++)[0] = '[';
+ buf = FromAddr{}(buf, v.addr);
+ (buf++)[0] = ']';
+ (buf++)[0] = ':';
+ return FromDec{}(buf, v.port);
+ }
+};
+
+namespace detail
+{
+static inline constexpr std::string_view upfx = "unix:";
+}
+
+template <>
+struct FromStr<SockUAddr>
+{
+ template <typename CharT>
+ constexpr SockUAddr operator()(std::basic_string_view<CharT> sv) const
+ {
+ if (sv.starts_with(detail::upfx))
+ {
+ sv = sv.substr(detail::upfx.size());
+ }
+ return SockUAddr{sv};
+ }
+};
+
+template <>
+struct ToStr<SockUAddr>
+{
+ using type = SockUAddr;
+ static inline constexpr std::size_t buf_size = detail::upfx.size() +
+ SockUAddr::maxLen;
+
+ template <typename CharT>
+ constexpr CharT* operator()(CharT* buf, const SockUAddr& v) const noexcept
+ {
+ buf = std::copy(detail::upfx.begin(), detail::upfx.end(), buf);
+ auto p = v.path();
+ return std::copy(p.begin(), p.end(), buf);
+ }
+};
+
+template <>
+struct FromStr<SockAnyAddr>
+{
+ template <typename CharT>
+ constexpr SockAnyAddr operator()(std::basic_string_view<CharT> sv) const
+ {
+ if (sv.starts_with('['))
+ {
+ return FromStr<Sock6Addr>{}(sv);
+ }
+ else if (sv.starts_with(detail::upfx))
+ {
+ return FromStr<SockUAddr>{}(sv);
+ }
+ return FromStr<Sock4Addr>{}(sv);
+ }
+};
+
+template <>
+struct ToStr<SockAnyAddr>
+{
+ using type = SockAnyAddr;
+ static inline constexpr std::size_t buf_size =
+ std::max({ToStr<Sock4Addr>::buf_size, ToStr<Sock6Addr>::buf_size,
+ ToStr<SockUAddr>::buf_size});
+
+ template <typename CharT>
+ constexpr CharT* operator()(CharT* buf, const SockAnyAddr& v) const noexcept
+ {
+ return std::visit(
+ [buf]<typename T>(const T& t) { return ToStr<T>{}(buf, t); }, v);
+ }
+};
+
} // namespace stdplus
+
+template <stdplus::SockAddr T, typename CharT>
+struct fmt::formatter<T, CharT> : stdplus::Format<stdplus::ToStr<T>, CharT>
+{};
+
+template <stdplus::SockAddr T, typename CharT>
+struct std::formatter<T, CharT> : stdplus::Format<stdplus::ToStr<T>, CharT>
+{};
diff --git a/test/net/addr/sock.cpp b/test/net/addr/sock.cpp
index 372092e..e326572 100644
--- a/test/net/addr/sock.cpp
+++ b/test/net/addr/sock.cpp
@@ -1,5 +1,7 @@
#include <stdplus/net/addr/sock.hpp>
+#include <format>
+#include <string>
#include <string_view>
#include <gtest/gtest.h>
@@ -26,6 +28,27 @@
EXPECT_EQ(sizeof(sockaddr_in), addr1.sockaddrLen());
}
+TEST(Sock4Addr, FromStr)
+{
+ constexpr FromStr<Sock4Addr> fs;
+ EXPECT_THROW(fs("10"sv), std::invalid_argument);
+ EXPECT_THROW(fs(":3959"sv), std::invalid_argument);
+ EXPECT_THROW(fs("0.0.0.0"sv), std::invalid_argument);
+ EXPECT_THROW(fs("0.0.0.0:"sv), std::invalid_argument);
+ EXPECT_THROW(fs(":::80"sv), std::invalid_argument);
+ EXPECT_EQ((Sock4Addr{In4Addr{}, 30}), fs("0.0.0.0:30"sv));
+}
+
+TEST(Sock4Addr, ToStr)
+{
+ ToStrHandle<ToStr<Sock4Addr>> tsh;
+ EXPECT_EQ("0.0.0.0:3959", tsh(Sock4Addr({}, 3959)));
+ EXPECT_EQ("255.0.255.255:28",
+ tsh(Sock4Addr(In4Addr{255, 0, 255, 255}, 28)));
+ EXPECT_EQ("a 1.2.3.4:32 b",
+ std::format("a {} b", Sock4Addr(In4Addr{1, 2, 3, 4}, 32)));
+}
+
TEST(Sock6Addr, Basic)
{
constexpr Sock6Addr addr1{.addr = In6Addr{255}, .port = 3959, .scope = 0};
@@ -45,6 +68,27 @@
EXPECT_EQ(sizeof(sockaddr_in6), addr1.sockaddrLen());
}
+TEST(Sock6Addr, FromStr)
+{
+ constexpr FromStr<Sock6Addr> fs;
+ EXPECT_THROW(fs("10"sv), std::invalid_argument);
+ EXPECT_THROW(fs(":10"sv), std::invalid_argument);
+ EXPECT_THROW(fs("ff::"sv), std::invalid_argument);
+ EXPECT_THROW(fs("[ff::]"sv), std::invalid_argument);
+ EXPECT_THROW(fs("[::]:"sv), std::invalid_argument);
+ EXPECT_THROW(fs("0.0.0.0:0"sv), std::invalid_argument);
+ EXPECT_EQ((Sock6Addr{In6Addr{}, 80, 0}), fs("[::]:80"sv));
+}
+
+TEST(Sock6Addr, ToStr)
+{
+ ToStrHandle<ToStr<Sock6Addr>> tsh;
+ EXPECT_EQ("[::]:0", tsh(Sock6Addr({}, 0, 0)));
+ EXPECT_EQ("[ff00::]:128", tsh(Sock6Addr(In6Addr{0xff}, 128, 0)));
+ EXPECT_EQ("a [102:304::]:32 b",
+ std::format("a {} b", Sock6Addr(In6Addr{1, 2, 3, 4}, 32, 0)));
+}
+
TEST(SockUAddr, Basic)
{
// Non-abstract Requires null-terminator
@@ -90,6 +134,24 @@
EXPECT_EQ(addr4.sockaddrLen(), sizeof(addr.sun_family));
}
+TEST(SockUAddr, FromStr)
+{
+ constexpr FromStr<SockUAddr> fs;
+ std::string as(sizeof(sockaddr_un), 'a');
+ EXPECT_THROW(fs(std::string_view(as)), std::invalid_argument);
+ EXPECT_THROW(fs("a\0"sv), std::invalid_argument);
+ EXPECT_EQ((SockUAddr{"/nope"sv}), fs("unix:/nope"sv));
+ EXPECT_EQ((SockUAddr{"@hi"sv}), fs("@hi"sv));
+}
+
+TEST(SockUAddr, ToStr)
+{
+ ToStrHandle<ToStr<SockUAddr>> tsh;
+ EXPECT_EQ("unix:/nope", tsh(SockUAddr("/nope"sv)));
+ EXPECT_EQ("unix:@hi", tsh(SockUAddr("\0hi"sv)));
+ EXPECT_EQ("a unix:a b", std::format("a {} b", SockUAddr("a"sv)));
+}
+
TEST(SockAnyAddr, Basic)
{
constexpr SockAnyAddr addr1(std::in_place_type<Sock4Addr>, In4Addr{255},
@@ -103,4 +165,23 @@
EXPECT_EQ(addr2.sockaddrLen(), sizeof(sa_family_t) + 4);
}
+TEST(SockAnyAddr, FromStr)
+{
+ constexpr FromStr<SockAnyAddr> fs;
+ EXPECT_THROW(fs("abcd"sv), std::invalid_argument);
+ EXPECT_THROW(fs("/nope"sv), std::invalid_argument);
+ EXPECT_EQ((Sock4Addr{In4Addr{}, 30}), fs("0.0.0.0:30"sv));
+ EXPECT_EQ((Sock6Addr{In6Addr{}, 80, 0}), fs("[::]:80"sv));
+ EXPECT_EQ((SockUAddr{"/nope"sv}), fs("unix:/nope"sv));
+}
+
+TEST(SockAnyAddr, ToStr)
+{
+ ToStrHandle<ToStr<SockAnyAddr>> tsh;
+ EXPECT_EQ("unix:/nope", tsh(SockUAddr("/nope"sv)));
+ EXPECT_EQ("0.0.0.0:3949", tsh(Sock4Addr(In4Addr{}, 3949)));
+ EXPECT_EQ("a [::]:356 b",
+ std::format("a {} b", Sock6Addr(In6Addr{}, 356, 0)));
+}
+
} // namespace stdplus