diff options
-rw-r--r-- | test/test_util.cpp | 30 | ||||
-rw-r--r-- | util.cpp | 30 | ||||
-rw-r--r-- | util.hpp | 6 |
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"; @@ -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; @@ -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. |