types: Remove custom MacAddr type

The standard library has an ether_addr type that does the same job.

Change-Id: I98685b0b60dd07453ae2362adb33e0a7c9c3132c
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/neighbor.cpp b/neighbor.cpp
index 1961ad3..d1b1c46 100644
--- a/neighbor.cpp
+++ b/neighbor.cpp
@@ -50,7 +50,7 @@
         auto [hdr, data] = netlink::extractRtAttr(msg);
         if (hdr.rta_type == NDA_LLADDR)
         {
-            neighbor.mac = mac_address::fromBuf(data);
+            neighbor.mac = copyFrom<ether_addr>(data, "Bad neighbor MAC");
         }
         else if (hdr.rta_type == NDA_DST)
         {
diff --git a/neighbor.hpp b/neighbor.hpp
index 074cae0..ce1d34b 100644
--- a/neighbor.hpp
+++ b/neighbor.hpp
@@ -4,6 +4,7 @@
 #include "util.hpp"
 
 #include <linux/netlink.h>
+#include <net/ethernet.h>
 
 #include <optional>
 #include <sdbusplus/bus.hpp>
@@ -33,7 +34,7 @@
 {
     std::string interface;
     InAddrAny address;
-    std::optional<MacAddr> mac;
+    std::optional<ether_addr> mac;
     bool permanent;
 };
 
diff --git a/test/test_neighbor.cpp b/test/test_neighbor.cpp
index a76a7d5..6491b9f 100644
--- a/test/test_neighbor.cpp
+++ b/test/test_neighbor.cpp
@@ -164,7 +164,7 @@
     EXPECT_EQ(ifstr, neighbors[0].interface);
     EXPECT_FALSE(neighbors[0].permanent);
     EXPECT_TRUE(neighbors[0].mac);
-    EXPECT_EQ(0, std::memcmp(&mac, neighbors[0].mac->data(), sizeof(mac)));
+    EXPECT_TRUE(equal(mac, *neighbors[0].mac));
     EXPECT_TRUE(equal(addr, std::get<in6_addr>(neighbors[0].address)));
 }
 
diff --git a/test/test_util.cpp b/test/test_util.cpp
index 3cf28e3..f49e631 100644
--- a/test/test_util.cpp
+++ b/test/test_util.cpp
@@ -28,40 +28,6 @@
     }
 };
 
-TEST_F(TestUtil, ToHex)
-{
-    EXPECT_EQ('E', mac_address::internal::toHex(std::byte(0xfe)));
-    EXPECT_EQ('A', mac_address::internal::toHex(std::byte(10)));
-    EXPECT_EQ('4', mac_address::internal::toHex(std::byte(4)));
-}
-
-TEST_F(TestUtil, MacFromBuf)
-{
-    std::string tooSmall(1, 'a');
-    std::string tooLarge(24, 'a');
-    std::string buf{'\x00', '\xde', '\xad', '\x00', '\xbe', '\xef'};
-
-    MacAddr mac = mac_address::fromBuf(buf);
-    EXPECT_EQ(0, memcmp(buf.data(), mac.data(), buf.size()));
-
-    EXPECT_THROW(mac_address::fromBuf(tooSmall), std::runtime_error);
-    EXPECT_THROW(mac_address::fromBuf(tooLarge), std::runtime_error);
-}
-
-TEST_F(TestUtil, MacToString)
-{
-    MacAddr mac1{
-        std::byte(0x00), std::byte(0xDE), std::byte(0xAD),
-        std::byte(0x00), std::byte(0xBE), std::byte(0xEF),
-    };
-    EXPECT_EQ("00:DE:AD:00:BE:EF", mac_address::toString(mac1));
-    MacAddr mac2{
-        std::byte(0x70), std::byte(0xFF), std::byte(0x84),
-        std::byte(0x09), std::byte(0x35), std::byte(0x09),
-    };
-    EXPECT_EQ("70:FF:84:09:35:09", mac_address::toString(mac2));
-}
-
 TEST_F(TestUtil, AddrFromBuf)
 {
     std::string tooSmall(1, 'a');
diff --git a/types.hpp b/types.hpp
index 562c3a1..123067a 100644
--- a/types.hpp
+++ b/types.hpp
@@ -80,7 +80,6 @@
 
 // Byte representations for common address types in network byte order
 using InAddrAny = std::variant<struct in_addr, struct in6_addr>;
-using MacAddr = std::array<std::byte, 6>;
 
 using AddrList = std::list<AddrInfo>;
 using IntfAddrMap = std::map<IntfName, AddrList>;
diff --git a/util.cpp b/util.cpp
index 6bc1497..91030c8 100644
--- a/util.cpp
+++ b/util.cpp
@@ -603,30 +603,9 @@
     return sdbusplus::message::variant_ns::get<std::string>(value);
 }
 
-MacAddr fromBuf(std::string_view buf)
+std::string toString(const ether_addr& mac)
 {
-    MacAddr ret;
-    if (buf.size() != ret.size())
-    {
-        throw std::runtime_error("Invalid MacAddr size");
-    }
-    memcpy(ret.data(), buf.data(), ret.size());
-    return ret;
-}
-
-std::string toString(const MacAddr& mac)
-{
-    std::string str;
-    str.reserve(mac.size() * 3);
-    for (size_t i = 0; i < mac.size(); ++i)
-    {
-        str.push_back(internal::toHex(mac[i] >> 4));
-        str.push_back(internal::toHex(mac[i]));
-        str.push_back(':');
-    }
-    // Remove trailing semicolon
-    str.pop_back();
-    return str;
+    return ether_ntoa(&mac);
 }
 
 } // namespace mac_address
diff --git a/util.hpp b/util.hpp
index fe94444..cf61e16 100644
--- a/util.hpp
+++ b/util.hpp
@@ -52,16 +52,11 @@
  */
 std::string getfromInventory(sdbusplus::bus::bus& bus);
 
-/* @brief Marshalls the bytes for a mac address into a MacAddr.
- * @param[in] buf - The network byte order address
- */
-MacAddr fromBuf(std::string_view buf);
-
 /** @brief Converts the given mac address bytes into a string
  *  @param[in] bytes - The mac address
  *  @returns A valid mac address string
  */
-std::string toString(const MacAddr& mac);
+std::string toString(const ether_addr& mac);
 
 namespace internal
 {
@@ -82,13 +77,6 @@
     return ret;
 }
 
-/** @brief Converts the lower nibble of a byte value to a hex digit
- */
-inline char toHex(std::byte byte)
-{
-    uint8_t val = std::to_integer<uint8_t>(byte) & 0xf;
-    return val < 10 ? '0' + val : 'A' + (val - 10);
-}
 } // namespace internal
 } // namespace mac_address