net/addr/sock: Add unix socket type

We have applications talking over unix sockets so we need to support
those.

Change-Id: I89e69aa212ed72f004b2332cb61bc857939630da
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 9a0238d..e1f2e74 100644
--- a/include/stdplus/net/addr/sock.hpp
+++ b/include/stdplus/net/addr/sock.hpp
@@ -1,5 +1,6 @@
 #pragma once
 #include <netinet/in.h>
+#include <sys/un.h>
 
 #include <stdplus/net/addr/ip.hpp>
 #include <stdplus/numeric/endian.hpp>
@@ -7,6 +8,7 @@
 
 #include <array>
 #include <cstdint>
+#include <string_view>
 
 namespace stdplus
 {
@@ -14,7 +16,8 @@
 struct SockAddrBuf : sockaddr
 {
     static inline constexpr std::size_t maxLen =
-        std::max({sizeof(sockaddr_in), sizeof(sockaddr_in6)}) -
+        std::max(
+            {sizeof(sockaddr_in), sizeof(sockaddr_in6), sizeof(sockaddr_un)}) -
         sizeof(sockaddr);
     std::array<std::uint8_t, maxLen> buf;
     std::uint8_t len;
@@ -118,10 +121,96 @@
 struct IsSockAddr<Sock6Addr> : std::true_type
 {};
 
+struct SockUAddr
+{
+    static inline constexpr std::size_t maxLen =
+        std::size(sockaddr_un{}.sun_path);
+
+    constexpr explicit SockUAddr(std::string_view path) : len_(path.size())
+    {
+        if (path.empty())
+        {
+            return;
+        }
+        bool abstract = path[0] == '@' || path[0] == '\0';
+        // Abstract sockets are not null terminated but path sockets are
+        if (path.size() >= maxLen + (abstract ? 1 : 0))
+        {
+            throw std::invalid_argument("Socket path too long");
+        }
+        if (!abstract && path.find('\0') != path.npos)
+        {
+            throw std::invalid_argument("Null bytes in non-abtract path");
+        }
+        buf_[0] = abstract ? '@' : path[0];
+        std::copy(path.begin() + 1, path.end(), buf_.begin() + 1);
+    }
+
+    constexpr bool operator==(const SockUAddr& rhs) const noexcept
+    {
+        return path() == rhs.path();
+    }
+
+    constexpr std::string_view path() const noexcept
+    {
+        return {buf_.data(), len_};
+    }
+
+    constexpr sockaddr_un sockaddr() const noexcept
+    {
+        sockaddr_un ret;
+        fill(ret);
+        return ret;
+    }
+    constexpr operator sockaddr_un() const noexcept
+    {
+        return sockaddr();
+    }
+
+    constexpr std::size_t sockaddrLen() const noexcept
+    {
+        return sizeof(sockaddr_un{}.sun_family) + len_ +
+               (len_ > 0 && buf_[0] != '@' ? 1 : 0);
+    }
+
+    constexpr SockAddrBuf buf() const noexcept
+    {
+        SockAddrBuf ret;
+        fill(reinterpret_cast<sockaddr_un&>(ret));
+        ret.len = sockaddrLen();
+        return ret;
+    }
+    constexpr operator SockAddrBuf() const noexcept
+    {
+        return buf();
+    }
+
+  private:
+    std::array<char, maxLen> buf_ = {};
+    std::uint8_t len_;
+    static_assert(maxLen <= std::numeric_limits<decltype(len_)>::max());
+
+    constexpr void fill(sockaddr_un& v) const noexcept
+    {
+        v.sun_family = AF_UNIX;
+        bool abstract = buf_[0] == '@';
+        v.sun_path[0] = abstract ? '\0' : buf_[0];
+        std::copy(buf_.begin() + 1, buf_.begin() + len_, v.sun_path + 1);
+        if (abstract)
+        {
+            v.sun_path[len_] = '\0';
+        }
+    };
+};
+
+template <>
+struct IsSockAddr<SockUAddr> : std::true_type
+{};
+
 namespace detail
 {
 
-using SockAnyAddrV = std::variant<Sock4Addr, Sock6Addr>;
+using SockAnyAddrV = std::variant<Sock4Addr, Sock6Addr, SockUAddr>;
 
 } // namespace detail
 
diff --git a/test/net/addr/sock.cpp b/test/net/addr/sock.cpp
index 4aee5c5..372092e 100644
--- a/test/net/addr/sock.cpp
+++ b/test/net/addr/sock.cpp
@@ -1,7 +1,11 @@
 #include <stdplus/net/addr/sock.hpp>
 
+#include <string_view>
+
 #include <gtest/gtest.h>
 
+using std::literals::string_view_literals::operator""sv;
+
 namespace stdplus
 {
 
@@ -41,14 +45,62 @@
     EXPECT_EQ(sizeof(sockaddr_in6), addr1.sockaddrLen());
 }
 
+TEST(SockUAddr, Basic)
+{
+    // Non-abstract Requires null-terminator
+    EXPECT_THROW(SockUAddr(std::string(SockUAddr::maxLen, 'a')),
+                 std::invalid_argument);
+    EXPECT_NO_THROW(SockUAddr(std::string(SockUAddr::maxLen - 1, 'a')));
+
+    // Abstract can use the full space
+    EXPECT_THROW(SockUAddr(std::string(SockUAddr::maxLen + 1, '\0')),
+                 std::invalid_argument);
+    EXPECT_NO_THROW(SockUAddr(std::string(SockUAddr::maxLen, '\0')));
+
+    EXPECT_THROW(SockUAddr("hi\0o"sv), std::invalid_argument);
+
+    constexpr SockUAddr addr1{"@hi"sv};
+    constexpr SockUAddr addr2{"\0hi"sv};
+    constexpr SockUAddr addr3{"/nope"sv};
+    constexpr SockUAddr addr4{""sv};
+
+    EXPECT_EQ(addr1, addr1);
+    EXPECT_EQ(addr1, addr2);
+    EXPECT_NE(addr3, addr1);
+    EXPECT_NE(addr4, addr1);
+    EXPECT_NE(addr4, addr3);
+
+    EXPECT_EQ(addr1.path(), "@hi"sv);
+    EXPECT_EQ(addr2.path(), "@hi"sv);
+    EXPECT_EQ(addr3.path(), "/nope"sv);
+    EXPECT_EQ(addr4.path(), ""sv);
+
+    auto addr = addr1.sockaddr();
+    EXPECT_EQ(AF_UNIX, addr.sun_family);
+    EXPECT_EQ("\0hi"sv, std::string_view(addr.sun_path, 3));
+    auto buf = addr2.buf();
+    auto ptr = reinterpret_cast<std::uint8_t*>(&buf);
+    EXPECT_TRUE(
+        std::equal(ptr, ptr + buf.len, reinterpret_cast<std::uint8_t*>(&addr)));
+    addr = addr3;
+    EXPECT_EQ("/nope\0"sv, std::string_view(addr.sun_path, 6));
+    EXPECT_EQ(addr1.sockaddrLen(), sizeof(addr.sun_family) + 3);
+    EXPECT_EQ(addr2.sockaddrLen(), sizeof(addr.sun_family) + 3);
+    EXPECT_EQ(addr3.sockaddrLen(), sizeof(addr.sun_family) + 6);
+    EXPECT_EQ(addr4.sockaddrLen(), sizeof(addr.sun_family));
+}
+
 TEST(SockAnyAddr, Basic)
 {
     constexpr SockAnyAddr addr1(std::in_place_type<Sock4Addr>, In4Addr{255},
                                 3959);
+    constexpr SockAnyAddr addr2(std::in_place_type<SockUAddr>, "/hi"sv);
+    EXPECT_NE(addr1, addr2);
     EXPECT_EQ(addr1, addr1);
     auto buf = addr1.buf();
     EXPECT_EQ(buf.len, sizeof(sockaddr_in));
     EXPECT_EQ(addr1.sockaddrLen(), sizeof(sockaddr_in));
+    EXPECT_EQ(addr2.sockaddrLen(), sizeof(sa_family_t) + 4);
 }
 
 } // namespace stdplus