Fix representation IPv6 DNS servers
The `getNameServerFromResolvd` function ignores the family type of
address received from `systemd-resolvd`. It leads to an incorrect
representation of IPv6 addresses.
This commit fixes that.
Resolves openbmc/phosphor-networkd#34
Testing:
Add an IPv6 DNS server:
```
busctl set-property xyz.openbmc_project.Network \
/xyz/openbmc_project/network/eth0 \
xyz.openbmc_project.Network.EthernetInterface \
StaticNameServers as 1 2001:db8:1::1
```
After the setting will be applied the `Nameservers` property must
contain valid representation:
```
busctl get-property xyz.openbmc_project.Network \
/xyz/openbmc_project/network/eth0 \
xyz.openbmc_project.Network.EthernetInterface \
Nameservers
as 1 "2001:db8:1::1"
```
Change-Id: Id69a0ffcceedd5be28e26ea9b9aca7f37e6a9345
Signed-off-by: Alexander Filippov <a.filippov@yadro.com>
diff --git a/util.cpp b/util.cpp
index 0c5dbff..e517f06 100644
--- a/util.cpp
+++ b/util.cpp
@@ -176,33 +176,44 @@
throw std::runtime_error("Unsupported address family");
}
+std::string toString(const struct in_addr& addr)
+{
+ std::string ip(INET_ADDRSTRLEN, '\0');
+ if (inet_ntop(AF_INET, &addr, ip.data(), ip.size()) == nullptr)
+ {
+ throw std::runtime_error("Failed to convert IP4 to string");
+ }
+
+ ip.resize(strlen(ip.c_str()));
+ return ip;
+}
+
+std::string toString(const struct in6_addr& addr)
+{
+ std::string ip(INET6_ADDRSTRLEN, '\0');
+ if (inet_ntop(AF_INET6, &addr, ip.data(), ip.size()) == nullptr)
+ {
+ throw std::runtime_error("Failed to convert IP6 to string");
+ }
+
+ ip.resize(strlen(ip.c_str()));
+ return ip;
+}
+
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");
- }
+ return toString(v);
}
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");
- }
+ return toString(v);
}
- else
- {
- throw std::runtime_error("Invalid addr type");
- }
- ip.resize(strlen(ip.c_str()));
- return ip;
+
+ throw std::runtime_error("Invalid addr type");
}
bool isLinkLocalIP(const std::string& address)