summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam A. Kennington III <wak@google.com>2019-02-01 21:43:11 -0800
committerWilliam A. Kennington III <wak@google.com>2019-02-14 12:10:12 -0800
commita14879e1f4d5ea2caa1cfccf02727974501d577f (patch)
treec7d34c5890a23bdb6c2cfc23e4e881adbc572447
parenta00b1c3f74d329c1ca65826e430efa9105a72465 (diff)
downloadphosphor-networkd-a14879e1f4d5ea2caa1cfccf02727974501d577f.tar.gz
phosphor-networkd-a14879e1f4d5ea2caa1cfccf02727974501d577f.zip
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>
-rw-r--r--test/test_util.cpp13
-rw-r--r--util.cpp11
-rw-r--r--util.hpp5
3 files changed, 29 insertions, 0 deletions
diff --git a/test/test_util.cpp b/test/test_util.cpp
index 0e58081..4e39821 100644
--- a/test/test_util.cpp
+++ b/test/test_util.cpp
@@ -31,6 +31,19 @@ TEST_F(TestUtil, ToHex)
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{
diff --git a/util.cpp b/util.cpp
index b1300fb..d1bcc71 100644
--- a/util.cpp
+++ b/util.cpp
@@ -603,6 +603,17 @@ std::string getfromInventory(sdbusplus::bus::bus& bus)
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;
diff --git a/util.hpp b/util.hpp
index a07fa49..9531d4f 100644
--- a/util.hpp
+++ b/util.hpp
@@ -49,6 +49,11 @@ inline bool validate(const std::string& value)
*/
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
OpenPOWER on IntegriCloud