types: Flesh out address comparison and string formatting

Change-Id: Ib505232e15e8db012d07759163f780b00ea4b7d3
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index 2d07e8b..09fd9dd 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -159,7 +159,7 @@
     EthernetInterfaceIntf::linkUp(info.running);
     if (info.mac)
     {
-        MacAddressIntf::macAddress(mac_address::toString(*info.mac));
+        MacAddressIntf::macAddress(std::to_string(*info.mac));
     }
     if (info.mtu)
     {
@@ -219,7 +219,7 @@
         {
             continue;
         }
-        auto address = toString(addr.address);
+        auto address = std::to_string(addr.address);
         IP::Protocol addressType = getProtocol(addr.address);
         IP::AddressOrigin origin = IP::AddressOrigin::Static;
         if (dhcpIsEnabled(addressType))
@@ -257,8 +257,8 @@
         {
             continue;
         }
-        auto ip = toString(neighbor.address);
-        auto mac = mac_address::toString(*neighbor.mac);
+        auto ip = std::to_string(neighbor.address);
+        auto mac = std::to_string(*neighbor.mac);
         auto objectPath = generateStaticNeighborObjectPath(ip, mac);
         staticNeighbors.emplace(
             ip, std::make_unique<Neighbor>(bus, objectPath, *this, ip, mac,
@@ -764,40 +764,8 @@
     {
         int addressFamily = std::get<0>(*i);
         std::vector<uint8_t>& ipaddress = std::get<1>(*i);
-
-        switch (addressFamily)
-        {
-            case AF_INET:
-                if (ipaddress.size() == sizeof(struct in_addr))
-                {
-                    servers.push_back(toString(
-                        *reinterpret_cast<struct in_addr*>(ipaddress.data())));
-                }
-                else
-                {
-                    log<level::ERR>(
-                        "Invalid data recived from Systemd-Resolved");
-                }
-                break;
-
-            case AF_INET6:
-                if (ipaddress.size() == sizeof(struct in6_addr))
-                {
-                    servers.push_back(toString(
-                        *reinterpret_cast<struct in6_addr*>(ipaddress.data())));
-                }
-                else
-                {
-                    log<level::ERR>(
-                        "Invalid data recived from Systemd-Resolved");
-                }
-                break;
-
-            default:
-                log<level::ERR>(
-                    "Unsupported address family in DNS from Systemd-Resolved");
-                break;
-        }
+        servers.push_back(std::to_string(
+            addrFromBuf(addressFamily, stdplus::raw::asView<char>(ipaddress))));
     }
     return servers;
 }
@@ -1032,11 +1000,11 @@
     }
 
     auto interface = interfaceName();
-    std::string validMAC = mac_address::toString(newMAC);
+    auto validMAC = std::to_string(newMAC);
 
     // We don't need to update the system if the address is unchanged
     ether_addr oldMAC = mac_address::fromString(MacAddressIntf::macAddress());
-    if (!stdplus::raw::equal(newMAC, oldMAC))
+    if (newMAC != oldMAC)
     {
         // Update everything that depends on the MAC value
         for (const auto& [name, intf] : vlanInterfaces)
diff --git a/src/meson.build b/src/meson.build
index 184eb4a..2e07084 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -55,6 +55,7 @@
   'network_manager.cpp',
   'system_configuration.cpp',
   'system_queries.cpp',
+  'types.cpp',
   'util.cpp',
   'routing_table.cpp',
   'config_parser.cpp',
diff --git a/src/network_manager_main.cpp b/src/network_manager_main.cpp
index 73df65d..5dc5b8d 100644
--- a/src/network_manager_main.cpp
+++ b/src/network_manager_main.cpp
@@ -64,14 +64,14 @@
     try
     {
         auto inventoryMAC = mac_address::getfromInventory(bus, intfname);
-        if (!mac_address::toString(inventoryMAC).empty())
+        if (inventoryMAC != ether_addr{})
         {
-            log<level::INFO>("Mac Address in Inventory on "),
-                entry("Interface : ", intfname.c_str()),
-                entry("MAC Address :",
-                      (mac_address::toString(inventoryMAC)).c_str());
-            manager->setFistBootMACOnInterface(std::make_pair(
-                intfname.c_str(), mac_address::toString(inventoryMAC)));
+            auto macStr = std::to_string(inventoryMAC);
+            log<level::INFO>("Mac Address in Inventory on ",
+                             entry("Interface : ", intfname.c_str()),
+                             entry("MAC Address :", macStr.c_str()));
+            manager->setFistBootMACOnInterface(
+                std::make_pair(intfname.c_str(), std::move(macStr)));
             first_boot_status.push_back(intfname.c_str());
             bool status = true;
             for (const auto& keys : configJson.items())
diff --git a/src/routing_table.cpp b/src/routing_table.cpp
index bae3926..20535e5 100644
--- a/src/routing_table.cpp
+++ b/src/routing_table.cpp
@@ -1,6 +1,7 @@
 #include "routing_table.hpp"
 
 #include "netlink.hpp"
+#include "types.hpp"
 #include "util.hpp"
 
 #include <net/if.h>
@@ -77,26 +78,16 @@
         }
     }
 
-    std::string dstStr;
-    if (dstAddr)
-    {
-        dstStr = toString(*dstAddr);
-    }
-    std::string gatewayStr;
-    if (gateWayAddr)
-    {
-        gatewayStr = toString(*gateWayAddr);
-    }
     if (rtm.rtm_dst_len == 0 && gateWayAddr)
     {
         std::string ifNameStr(ifName);
         if (rtm.rtm_family == AF_INET)
         {
-            defaultGateway[ifNameStr] = gatewayStr;
+            defaultGateway.emplace(ifNameStr, std::to_string(*gateWayAddr));
         }
         else if (rtm.rtm_family == AF_INET6)
         {
-            defaultGateway6[ifNameStr] = gatewayStr;
+            defaultGateway6.emplace(ifNameStr, std::to_string(*gateWayAddr));
         }
     }
 }
diff --git a/src/types.cpp b/src/types.cpp
new file mode 100644
index 0000000..0d642e5
--- /dev/null
+++ b/src/types.cpp
@@ -0,0 +1,114 @@
+#include "types.hpp"
+
+#include <arpa/inet.h>
+#include <byteswap.h>
+
+#include <charconv>
+
+namespace phosphor::network::detail
+{
+
+std::string_view AddrBufMaker<ether_addr>::operator()(ether_addr val) noexcept
+{
+    for (char* ptr = buf.data() + 2; ptr < buf.end(); ptr += 3)
+    {
+        *ptr = ':';
+    }
+    for (size_t i = 0; i < 6; ++i)
+    {
+        char* tmp = buf.data() + i * 3;
+        uint8_t byte = val.ether_addr_octet[i];
+        if (byte < 16)
+        {
+            *(tmp++) = '0';
+        }
+        std::to_chars(tmp, buf.end(), byte, 16);
+    }
+    return {buf.begin(), buf.size()};
+}
+
+std::string_view AddrBufMaker<in_addr>::operator()(in_addr val) noexcept
+{
+    auto v = bswap_32(ntohl(val.s_addr));
+    char* ptr = buf.begin();
+    for (size_t i = 0; i < 3; ++i)
+    {
+        const auto res = std::to_chars(ptr, buf.end(), v & 0xff, 10);
+        *res.ptr = '.';
+        ptr = res.ptr + 1;
+        v >>= 8;
+    }
+    const auto res = std::to_chars(ptr, buf.end(), v & 0xff, 10);
+    return {buf.data(), res.ptr};
+}
+
+std::string_view AddrBufMaker<in6_addr>::operator()(in6_addr val) noexcept
+{
+    size_t skip_start = 0;
+    size_t skip_size = 0;
+    {
+        size_t new_start = 0;
+        size_t new_size = 0;
+        for (size_t i = 0; i < 9; ++i)
+        {
+            if (i < 8 && val.s6_addr16[i] == 0)
+            {
+                if (new_start + new_size == i)
+                {
+                    new_size++;
+                }
+                else
+                {
+                    new_start = i;
+                    new_size = 1;
+                }
+            }
+            else if (new_start + new_size == i && new_size > skip_size)
+            {
+                skip_start = new_start;
+                skip_size = new_size;
+            }
+        }
+    }
+    char* ptr = buf.begin();
+    for (size_t i = 0; i < 8; ++i)
+    {
+        if (i == skip_start && skip_size > 0)
+        {
+            if (i == 0)
+            {
+                *(ptr++) = ':';
+            }
+            *(ptr++) = ':';
+            i += skip_size - 1;
+            continue;
+        }
+        const auto res =
+            std::to_chars(ptr, buf.end(), ntohs(val.s6_addr16[i]), 16);
+        ptr = res.ptr;
+        if (i < 7)
+        {
+            *(ptr++) = ':';
+        }
+    }
+    return {buf.data(), ptr};
+}
+
+} // namespace phosphor::network::detail
+
+std::string std::to_string(ether_addr value)
+{
+    return string(phosphor::network::detail::AddrBufMaker<ether_addr>{}(value));
+}
+std::string std::to_string(in_addr value)
+{
+    return string(phosphor::network::detail::AddrBufMaker<in_addr>{}(value));
+}
+std::string std::to_string(in6_addr value)
+{
+    return string(phosphor::network::detail::AddrBufMaker<in6_addr>{}(value));
+}
+std::string std::to_string(phosphor::network::InAddrAny value)
+{
+    return std::visit([](auto v) { return std::to_string(v); }, value);
+}
diff --git a/src/types.hpp b/src/types.hpp
index a78253c..a6011ff 100644
--- a/src/types.hpp
+++ b/src/types.hpp
@@ -1,12 +1,18 @@
 #pragma once
+#include <fmt/core.h>
+#include <net/ethernet.h>
 #include <netinet/in.h>
 #include <systemd/sd-event.h>
 
+#include <algorithm>
+#include <array>
 #include <chrono>
 #include <memory>
 #include <sdeventplus/clock.hpp>
 #include <sdeventplus/utility/timer.hpp>
 #include <string>
+#include <string_view>
+#include <type_traits>
 #include <unordered_map>
 #include <unordered_set>
 #include <variant>
@@ -36,7 +42,7 @@
 using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
 
 // Byte representations for common address types in network byte order
-using InAddrAny = std::variant<struct in_addr, struct in6_addr>;
+using InAddrAny = std::variant<in_addr, in6_addr>;
 
 using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
 
@@ -62,6 +68,94 @@
     return seed ^ (std::hash<T>{}(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
 }
 
+namespace detail
+{
+
+template <typename T>
+constexpr bool vcontains() noexcept
+{
+    return false;
+}
+
+template <typename T, typename V, typename... Vs>
+constexpr bool vcontains() noexcept
+{
+    return vcontains<T, Vs...>() || std::is_same_v<T, V>;
+}
+
+template <typename T, typename... Types>
+constexpr std::enable_if_t<vcontains<T, Types...>(), bool>
+    veq(T t, std::variant<Types...> v) noexcept
+{
+    return std::visit(
+        [t](auto v) {
+            if constexpr (std::is_same_v<T, decltype(v)>)
+            {
+                return v == t;
+            }
+            else
+            {
+                return false;
+            }
+        },
+        v);
+}
+
+template <typename T>
+struct AddrBufMaker
+{
+};
+
+template <>
+struct AddrBufMaker<ether_addr>
+{
+  public:
+    std::string_view operator()(ether_addr val) noexcept;
+
+  private:
+    std::array<char, /*octet*/ 2 * /*octets*/ 6 + /*seps*/ 5> buf;
+};
+
+template <>
+struct AddrBufMaker<in_addr>
+{
+  public:
+    std::string_view operator()(in_addr val) noexcept;
+
+  private:
+    std::array<char, /*octet*/ 3 * /*octets*/ 4 + /*seps*/ 3> buf;
+};
+
+template <>
+struct AddrBufMaker<in6_addr>
+{
+  public:
+    std::string_view operator()(in6_addr val) noexcept;
+
+  private:
+    std::array<char, /*hextet*/ 4 * /*hextets*/ 8 + /*seps*/ 7> buf;
+};
+
+template <typename BufMaker>
+struct FormatFromBuf
+{
+  private:
+    fmt::formatter<std::string_view> formatter;
+
+  public:
+    template <typename ParseContext>
+    constexpr auto parse(ParseContext& ctx)
+    {
+        return ctx.begin();
+    }
+
+    template <typename FormatContext>
+    auto format(auto v, FormatContext& ctx) const
+    {
+        return formatter.format(BufMaker{}(v), ctx);
+    }
+};
+} // namespace detail
 } // namespace network
 } // namespace phosphor
 
@@ -73,3 +167,116 @@
         return std::apply(phosphor::network::hash_multi<Ts...>, t);
     }
 };
+
+namespace fmt
+{
+template <>
+struct formatter<ether_addr>
+    : phosphor::network::detail::FormatFromBuf<
+          phosphor::network::detail::AddrBufMaker<ether_addr>>
+{
+};
+template <>
+struct formatter<in_addr>
+    : phosphor::network::detail::FormatFromBuf<
+          phosphor::network::detail::AddrBufMaker<in_addr>>
+{
+};
+template <>
+struct formatter<in6_addr>
+    : phosphor::network::detail::FormatFromBuf<
+          phosphor::network::detail::AddrBufMaker<in6_addr>>
+{
+};
+template <>
+struct formatter<phosphor::network::InAddrAny>
+{
+  private:
+    fmt::formatter<std::string_view> formatter;
+
+  public:
+    template <typename ParseContext>
+    constexpr auto parse(ParseContext& ctx)
+    {
+        return ctx.begin();
+    }
+
+    template <typename FormatContext>
+    auto format(auto v, FormatContext& ctx) const
+    {
+        return std::visit(
+            [&](auto v) {
+                auto abm =
+                    phosphor::network::detail::AddrBufMaker<decltype(v)>{};
+                return formatter.format(abm(v), ctx);
+            },
+            v);
+    }
+};
+} // namespace fmt
+
+namespace std
+{
+string to_string(ether_addr value);
+string to_string(in_addr value);
+string to_string(in6_addr value);
+string to_string(phosphor::network::InAddrAny value);
+} // namespace std
+
+constexpr bool operator==(ether_addr lhs, ether_addr rhs) noexcept
+{
+    return std::equal(lhs.ether_addr_octet, lhs.ether_addr_octet + 6,
+                      rhs.ether_addr_octet);
+}
+
+constexpr bool operator==(in_addr lhs, in_addr rhs) noexcept
+{
+    return lhs.s_addr == rhs.s_addr;
+}
+
+constexpr bool operator==(in6_addr lhs, in6_addr rhs) noexcept
+{
+    return std::equal(lhs.s6_addr32, lhs.s6_addr32 + 4, rhs.s6_addr32);
+}
+
+template <typename T>
+constexpr std::enable_if_t<!std::is_same_v<phosphor::network::InAddrAny, T>,
+                           bool>
+    operator==(phosphor::network::InAddrAny lhs, T rhs) noexcept
+{
+    return phosphor::network::detail::veq(rhs, lhs);
+}
+
+template <typename CharT, typename Traits>
+std::basic_ostream<CharT, Traits>&
+    operator<<(std::basic_ostream<CharT, Traits>& os, ether_addr v)
+{
+    return os << phosphor::network::detail::AddrBufMaker<ether_addr>{}(v);
+}
+
+template <typename CharT, typename Traits>
+std::basic_ostream<CharT, Traits>&
+    operator<<(std::basic_ostream<CharT, Traits>& os, in_addr v)
+{
+    return os << phosphor::network::detail::AddrBufMaker<in_addr>{}(v);
+}
+
+template <typename CharT, typename Traits>
+std::basic_ostream<CharT, Traits>&
+    operator<<(std::basic_ostream<CharT, Traits>& os, in6_addr v)
+{
+    return os << phosphor::network::detail::AddrBufMaker<in6_addr>{}(v);
+}
+
+template <typename CharT, typename Traits>
+std::basic_ostream<CharT, Traits>&
+    operator<<(std::basic_ostream<CharT, Traits>& os,
+               phosphor::network::InAddrAny v)
+{
+    return os << std::visit(
+               [](auto v) {
+                   return phosphor::network::detail::AddrBufMaker<
+                       decltype(v)>{}(v);
+               },
+               v);
+}
diff --git a/src/util.cpp b/src/util.cpp
index 186d0c2..111c090 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -159,25 +159,6 @@
         [=]<int f>() -> InAddrAny { return addrFromBuf<f>(buf); }, family);
 }
 
-template <typename Addr>
-std::string toString(const Addr& addr)
-{
-    static constexpr int family = AddrToFamily<Addr>::value;
-    std::string ret(FamilyTraits<family>::strlen, '\0');
-    if (inet_ntop(family, &addr, ret.data(), ret.size()) == nullptr)
-    {
-        throw std::runtime_error("Failed to convert IP to string");
-    }
-
-    ret.resize(strlen(ret.c_str()));
-    return ret;
-}
-
-std::string toString(const InAddrAny& addr)
-{
-    return std::visit([](auto&& a) { return toString(a); }, addr);
-}
-
 bool isValidIP(int family, stdplus::const_zstring address) noexcept
 {
     unsigned char buf[sizeof(struct in6_addr)];
@@ -476,15 +457,9 @@
     return ret;
 }
 
-std::string toString(const ether_addr& mac)
-{
-    return fmt::format(FMT_COMPILE("{:02x}"),
-                       fmt::join(mac.ether_addr_octet, ":"));
-}
-
 bool isEmpty(const ether_addr& mac)
 {
-    return stdplus::raw::equal(mac, ether_addr{});
+    return mac == ether_addr{};
 }
 
 bool isMulticast(const ether_addr& mac)
diff --git a/src/util.hpp b/src/util.hpp
index f6b9796..46353a4 100644
--- a/src/util.hpp
+++ b/src/util.hpp
@@ -43,12 +43,6 @@
  */
 ether_addr fromString(std::string_view str);
 
-/** @brief Converts the given mac address bytes into a string
- *  @param[in] mac - The mac address
- *  @returns A valid mac address string
- */
-std::string toString(const ether_addr& mac);
-
 /** @brief Determines if the mac address is empty
  *  @param[in] mac - The mac address
  *  @return True if 00:00:00:00:00:00
@@ -78,31 +72,12 @@
 struct FamilyTraits<AF_INET>
 {
     using addr = in_addr;
-    static constexpr std::size_t strlen = INET_ADDRSTRLEN;
 };
 
 template <>
 struct FamilyTraits<AF_INET6>
 {
     using addr = in6_addr;
-    static constexpr std::size_t strlen = INET6_ADDRSTRLEN;
-};
-
-template <typename Addr>
-struct AddrToFamily
-{
-};
-
-template <>
-struct AddrToFamily<in_addr>
-{
-    static constexpr int value = AF_INET;
-};
-
-template <>
-struct AddrToFamily<in6_addr>
-{
-    static constexpr int value = AF_INET6;
 };
 
 /* @brief converts a sockaddr for the specified address family into
@@ -114,14 +89,6 @@
 typename FamilyTraits<family>::addr addrFromBuf(std::string_view buf);
 InAddrAny addrFromBuf(int family, std::string_view buf);
 
-/* @brief converts the ip bytes into a string representation
- * @param[in] addr - input ip address to convert.
- * @returns String representation of the ip.
- */
-template <typename Addr>
-std::string toString(const Addr& addr);
-std::string toString(const InAddrAny& addr);
-
 /* @brief checks that the given ip address valid or not.
  * @param[in] family - IP address family(AF_INET/AF_INET6).
  * @param[in] address - IP address.