util: Add a MacAddr population function
This one is pretty trivial since there is only one size of mac
address, but we want to be sure we validate the buffer.
Tested:
Built and runs through unit tests. Works when integrated into
neighbor parsing code.
Change-Id: Iaf58fc398b51a3bcbbf70968cbe353beeb7b9f54
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/util.cpp b/util.cpp
index b1300fb..d1bcc71 100644
--- a/util.cpp
+++ b/util.cpp
@@ -603,6 +603,17 @@
return sdbusplus::message::variant_ns::get<std::string>(value);
}
+MacAddr fromBuf(std::string_view buf)
+{
+ 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;