summaryrefslogtreecommitdiffstats
path: root/util.cpp
diff options
context:
space:
mode:
authorWilliam A. Kennington III <wak@google.com>2019-02-01 18:57:17 -0800
committerWilliam A. Kennington III <wak@google.com>2019-02-14 12:10:12 -0800
commita00b1c3f74d329c1ca65826e430efa9105a72465 (patch)
treed72155b9b79b7de937dac308e34cbe9562b571a0 /util.cpp
parent5058f57a88d697034a8ed20145489d9cdb8c5275 (diff)
downloadphosphor-networkd-a00b1c3f74d329c1ca65826e430efa9105a72465.tar.gz
phosphor-networkd-a00b1c3f74d329c1ca65826e430efa9105a72465.zip
util: Add InAddrAny population function
We commonly want to be able to turn netlink network order address data into the corresponding InAddrAny when parsing netlink data. This makes it trivial given the address family and data buffer. Tested: Ran throgh unit tests and builds fine in a BMC image. Change-Id: I4ebe1dbd284f150ea03ee091cddda5a06806c01f Signed-off-by: William A. Kennington III <wak@google.com>
Diffstat (limited to 'util.cpp')
-rw-r--r--util.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/util.cpp b/util.cpp
index db164e6..b1300fb 100644
--- a/util.cpp
+++ b/util.cpp
@@ -204,6 +204,32 @@ std::string getNetworkID(int addressFamily, const std::string& ipaddress,
return networkString;
}
+InAddrAny addrFromBuf(int addressFamily, std::string_view buf)
+{
+ if (addressFamily == AF_INET)
+ {
+ struct in_addr ret;
+ if (buf.size() != sizeof(ret))
+ {
+ throw std::runtime_error("Buf not in_addr sized");
+ }
+ memcpy(&ret, buf.data(), sizeof(ret));
+ return ret;
+ }
+ else if (addressFamily == AF_INET6)
+ {
+ struct in6_addr ret;
+ if (buf.size() != sizeof(ret))
+ {
+ throw std::runtime_error("Buf not in6_addr sized");
+ }
+ memcpy(&ret, buf.data(), sizeof(ret));
+ return ret;
+ }
+
+ throw std::runtime_error("Unsupported address family");
+}
+
std::string toString(const InAddrAny& addr)
{
std::string ip;
OpenPOWER on IntegriCloud