summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam A. Kennington III <wak@google.com>2019-01-30 17:18:14 -0800
committerWilliam A. Kennington III <wak@google.com>2019-02-14 12:10:12 -0800
commit5058f57a88d697034a8ed20145489d9cdb8c5275 (patch)
tree2895821d2cc767a2739f1dcd7e2b59180eeb4f2e
parentd27410f19fe3277930d19938882d6382331e0377 (diff)
downloadphosphor-networkd-5058f57a88d697034a8ed20145489d9cdb8c5275.tar.gz
phosphor-networkd-5058f57a88d697034a8ed20145489d9cdb8c5275.zip
util: Add a function for converting IP bytes to strings
We need this for future work which turns netlink data into IP addresses. Tested: Run through unit tests. Change-Id: If078b28246509ca2ebd3bf7bab652b84258df0bd Signed-off-by: William A. Kennington III <wak@google.com>
-rw-r--r--test/test_util.cpp30
-rw-r--r--util.cpp30
-rw-r--r--util.hpp6
3 files changed, 66 insertions, 0 deletions
diff --git a/test/test_util.cpp b/test/test_util.cpp
index f536bd7..25953a1 100644
--- a/test/test_util.cpp
+++ b/test/test_util.cpp
@@ -1,5 +1,6 @@
#include "util.hpp"
+#include <arpa/inet.h>
#include <netinet/in.h>
#include <cstddef>
@@ -44,6 +45,35 @@ TEST_F(TestUtil, MacToString)
EXPECT_EQ("70:FF:84:09:35:09", mac_address::toString(mac2));
}
+TEST_F(TestUtil, IpToString)
+{
+ struct in_addr ip1;
+ EXPECT_EQ(1, inet_pton(AF_INET, "192.168.10.1", &ip1));
+ EXPECT_EQ("192.168.10.1", toString(InAddrAny(ip1)));
+
+ struct in6_addr ip2;
+ EXPECT_EQ(1, inet_pton(AF_INET6, "fdd8:b5ad:9d93:94ee::2:1", &ip2));
+ EXPECT_EQ("fdd8:b5ad:9d93:94ee::2:1", toString(InAddrAny(ip2)));
+
+ InAddrAny ip3;
+ try
+ {
+ struct E
+ {
+ operator struct in6_addr()
+ {
+ throw 1;
+ }
+ };
+ ip3.emplace<struct in6_addr>(E());
+ EXPECT_TRUE(false);
+ }
+ catch (...)
+ {
+ }
+ EXPECT_THROW(toString(ip3), std::runtime_error);
+}
+
TEST_F(TestUtil, IpValidation)
{
std::string ipaddress = "0.0.0.0";
diff --git a/util.cpp b/util.cpp
index 7aa4e2a..db164e6 100644
--- a/util.cpp
+++ b/util.cpp
@@ -14,6 +14,7 @@
#include <list>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/log.hpp>
+#include <stdexcept>
#include <string>
#include <xyz/openbmc_project/Common/error.hpp>
@@ -203,6 +204,35 @@ std::string getNetworkID(int addressFamily, const std::string& ipaddress,
return networkString;
}
+std::string toString(const InAddrAny& addr)
+{
+ std::string ip;
+ if (std::holds_alternative<struct in_addr>(addr))
+ {
+ const auto& v = std::get<struct in_addr>(addr);
+ ip.resize(INET_ADDRSTRLEN);
+ if (inet_ntop(AF_INET, &v, ip.data(), ip.size()) == NULL)
+ {
+ throw std::runtime_error("Failed to convert IP4 to string");
+ }
+ }
+ else if (std::holds_alternative<struct in6_addr>(addr))
+ {
+ const auto& v = std::get<struct in6_addr>(addr);
+ ip.resize(INET6_ADDRSTRLEN);
+ if (inet_ntop(AF_INET6, &v, ip.data(), ip.size()) == NULL)
+ {
+ throw std::runtime_error("Failed to convert IP6 to string");
+ }
+ }
+ else
+ {
+ throw std::runtime_error("Invalid addr type");
+ }
+ ip.resize(strlen(ip.c_str()));
+ return ip;
+}
+
bool isLinkLocalIP(const std::string& address)
{
return address.find(IPV4_PREFIX) == 0 || address.find(IPV6_PREFIX) == 0;
diff --git a/util.hpp b/util.hpp
index e01c744..537c6a0 100644
--- a/util.hpp
+++ b/util.hpp
@@ -93,6 +93,12 @@ constexpr auto timeSynchdService = "systemd-timesyncd.service";
*/
uint8_t toCidr(int addressFamily, const std::string& mask);
+/* @brief converts the ip bytes into a string representation
+ * @param[in] addr - input ip address to convert.
+ * @returns String representation of the ip.
+ */
+std::string toString(const InAddrAny& addr);
+
/* @brief converts the prefix into subnetmask.
* @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
* @param[in] prefix - prefix length.
OpenPOWER on IntegriCloud