types: Migrate to stdplus ip
Change-Id: I777aa701204b0c4dbe5f0088cc2e6a4deb4f5d15
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index 91e5ed5..a38e7c8 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -104,11 +104,11 @@
if (info.defgw4)
{
- EthernetInterface::defaultGateway(std::to_string(*info.defgw4), true);
+ EthernetInterface::defaultGateway(stdplus::toStr(*info.defgw4), true);
}
if (info.defgw6)
{
- EthernetInterface::defaultGateway6(std::to_string(*info.defgw6), true);
+ EthernetInterface::defaultGateway6(stdplus::toStr(*info.defgw6), true);
}
emit_object_added();
@@ -217,16 +217,16 @@
ObjectPath EthernetInterface::ip(IP::Protocol protType, std::string ipaddress,
uint8_t prefixLength, std::string)
{
- InAddrAny addr;
+ std::optional<stdplus::InAnyAddr> addr;
try
{
switch (protType)
{
case IP::Protocol::IPv4:
- addr = ToAddr<in_addr>{}(ipaddress);
+ addr.emplace(stdplus::fromStr<stdplus::In4Addr>(ipaddress));
break;
case IP::Protocol::IPv6:
- addr = ToAddr<in6_addr>{}(ipaddress);
+ addr.emplace(stdplus::fromStr<stdplus::In6Addr>(ipaddress));
break;
default:
throw std::logic_error("Exhausted protocols");
@@ -239,10 +239,10 @@
elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipaddress"),
Argument::ARGUMENT_VALUE(ipaddress.c_str()));
}
- IfAddr ifaddr;
+ std::optional<stdplus::SubnetAny> ifaddr;
try
{
- ifaddr = {addr, prefixLength};
+ ifaddr.emplace(*addr, prefixLength);
}
catch (const std::exception& e)
{
@@ -250,16 +250,16 @@
prefixLength, "ERROR", e);
elog<InvalidArgument>(
Argument::ARGUMENT_NAME("prefixLength"),
- Argument::ARGUMENT_VALUE(std::to_string(prefixLength).c_str()));
+ Argument::ARGUMENT_VALUE(stdplus::toStr(prefixLength).c_str()));
}
- auto it = addrs.find(ifaddr);
+ auto it = addrs.find(*ifaddr);
if (it == addrs.end())
{
it = std::get<0>(addrs.emplace(
- ifaddr,
+ *ifaddr,
std::make_unique<IPAddress>(bus, std::string_view(objPath), *this,
- ifaddr, IP::AddressOrigin::Static)));
+ *ifaddr, IP::AddressOrigin::Static)));
}
else
{
@@ -279,10 +279,10 @@
ObjectPath EthernetInterface::neighbor(std::string ipAddress,
std::string macAddress)
{
- InAddrAny addr;
+ std::optional<stdplus::InAnyAddr> addr;
try
{
- addr = ToAddr<InAddrAny>{}(ipAddress);
+ addr.emplace(stdplus::fromStr<stdplus::InAnyAddr>(ipAddress));
}
catch (const std::exception& e)
{
@@ -292,10 +292,10 @@
Argument::ARGUMENT_VALUE(ipAddress.c_str()));
}
- stdplus::EtherAddr lladdr;
+ std::optional<stdplus::EtherAddr> lladdr;
try
{
- lladdr = stdplus::fromStr<stdplus::EtherAddr>(macAddress);
+ lladdr.emplace(stdplus::fromStr<stdplus::EtherAddr>(macAddress));
}
catch (const std::exception& e)
{
@@ -305,17 +305,17 @@
Argument::ARGUMENT_VALUE(macAddress.c_str()));
}
- auto it = staticNeighbors.find(addr);
+ auto it = staticNeighbors.find(*addr);
if (it == staticNeighbors.end())
{
it = std::get<0>(staticNeighbors.emplace(
- addr, std::make_unique<Neighbor>(bus, std::string_view(objPath),
- *this, addr, lladdr,
- Neighbor::State::Permanent)));
+ *addr, std::make_unique<Neighbor>(bus, std::string_view(objPath),
+ *this, *addr, *lladdr,
+ Neighbor::State::Permanent)));
}
else
{
- auto str = stdplus::toStr(lladdr);
+ auto str = stdplus::toStr(*lladdr);
if (it->second->macAddress() == str)
{
return it->second->getObjPath();
@@ -435,7 +435,7 @@
{
try
{
- ip = std::to_string(ToAddr<InAddrAny>{}(ip));
+ ip = stdplus::toStr(stdplus::fromStr<stdplus::InAnyAddr>(ip));
}
catch (const std::exception& e)
{
@@ -538,7 +538,7 @@
{
int addressFamily = std::get<0>(*i);
std::vector<uint8_t>& ipaddress = std::get<1>(*i);
- servers.push_back(std::to_string(
+ servers.push_back(stdplus::toStr(
addrFromBuf(addressFamily, stdplus::raw::asView<char>(ipaddress))));
}
return servers;
@@ -546,8 +546,8 @@
ObjectPath EthernetInterface::createVLAN(uint16_t id)
{
- auto intfName = fmt::format(FMT_COMPILE("{}.{}"), interfaceName(), id);
- auto idStr = std::to_string(id);
+ auto idStr = stdplus::toStr(id);
+ auto intfName = fmt::format(FMT_COMPILE("{}.{}"), interfaceName(), idStr);
if (manager.get().interfaces.find(intfName) !=
manager.get().interfaces.end())
{
@@ -817,7 +817,8 @@
{
if (!gateway.empty())
{
- gateway = std::to_string(ToAddr<in_addr>{}(gateway));
+ gateway =
+ stdplus::toStr(stdplus::fromStr<stdplus::In4Addr>(gateway));
}
}
catch (const std::exception& e)
@@ -846,7 +847,8 @@
{
if (!gateway.empty())
{
- gateway = std::to_string(ToAddr<in6_addr>{}(gateway));
+ gateway =
+ stdplus::toStr(stdplus::fromStr<stdplus::In6Addr>(gateway));
}
}
catch (const std::exception& e)
diff --git a/src/ethernet_interface.hpp b/src/ethernet_interface.hpp
index 87cfd68..0b6e21a 100644
--- a/src/ethernet_interface.hpp
+++ b/src/ethernet_interface.hpp
@@ -86,10 +86,11 @@
stdplus::PinnedRef<Manager> manager;
/** @brief Persistent map of IPAddress dbus objects and their names */
- std::unordered_map<IfAddr, std::unique_ptr<IPAddress>> addrs;
+ std::unordered_map<stdplus::SubnetAny, std::unique_ptr<IPAddress>> addrs;
/** @brief Persistent map of Neighbor dbus objects and their names */
- std::unordered_map<InAddrAny, std::unique_ptr<Neighbor>> staticNeighbors;
+ std::unordered_map<stdplus::InAnyAddr, std::unique_ptr<Neighbor>>
+ staticNeighbors;
void addAddr(const AddressInfo& info);
void addStaticNeigh(const NeighborInfo& info);
@@ -128,15 +129,15 @@
using EthernetInterfaceIntf::dhcp6;
bool dhcp6(bool value) override;
- inline bool dhcpIsEnabled(in_addr) const
+ inline bool dhcpIsEnabled(stdplus::In4Addr) const
{
return dhcp4();
}
- inline bool dhcpIsEnabled(in6_addr) const
+ inline bool dhcpIsEnabled(stdplus::In6Addr) const
{
return dhcp6();
}
- inline bool dhcpIsEnabled(InAddrAny addr) const
+ inline bool dhcpIsEnabled(stdplus::InAnyAddr addr) const
{
return std::visit([&](auto v) { return dhcpIsEnabled(v); }, addr);
}
diff --git a/src/ipaddress.cpp b/src/ipaddress.cpp
index 32c9462..e678725 100644
--- a/src/ipaddress.cpp
+++ b/src/ipaddress.cpp
@@ -22,10 +22,11 @@
using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
-static auto makeObjPath(std::string_view root, IfAddr addr)
+static auto makeObjPath(std::string_view root, stdplus::SubnetAny addr)
{
auto ret = sdbusplus::message::object_path(std::string(root));
- ret /= std::to_string(addr);
+ stdplus::ToStrHandle<stdplus::ToStr<stdplus::SubnetAny>> tsh;
+ ret /= tsh(addr);
return ret;
}
@@ -34,31 +35,31 @@
{};
template <>
-struct Proto<in_addr>
+struct Proto<stdplus::In4Addr>
{
static inline constexpr auto value = IP::Protocol::IPv4;
};
template <>
-struct Proto<in6_addr>
+struct Proto<stdplus::In6Addr>
{
static inline constexpr auto value = IP::Protocol::IPv6;
};
IPAddress::IPAddress(sdbusplus::bus_t& bus, std::string_view objRoot,
- stdplus::PinnedRef<EthernetInterface> parent, IfAddr addr,
- AddressOrigin origin) :
+ stdplus::PinnedRef<EthernetInterface> parent,
+ stdplus::SubnetAny addr, AddressOrigin origin) :
IPAddress(bus, makeObjPath(objRoot, addr), parent, addr, origin)
{}
IPAddress::IPAddress(sdbusplus::bus_t& bus,
sdbusplus::message::object_path objPath,
- stdplus::PinnedRef<EthernetInterface> parent, IfAddr addr,
- AddressOrigin origin) :
+ stdplus::PinnedRef<EthernetInterface> parent,
+ stdplus::SubnetAny addr, AddressOrigin origin) :
IPIfaces(bus, objPath.str.c_str(), IPIfaces::action::defer_emit),
parent(parent), objPath(std::move(objPath))
{
- IP::address(std::to_string(addr.getAddr()), true);
+ IP::address(stdplus::toStr(addr.getAddr()), true);
IP::prefixLength(addr.getPfx(), true);
IP::type(std::visit([](auto v) { return Proto<decltype(v)>::value; },
addr.getAddr()),
diff --git a/src/ipaddress.hpp b/src/ipaddress.hpp
index 1785e06..13ab94f 100644
--- a/src/ipaddress.hpp
+++ b/src/ipaddress.hpp
@@ -40,8 +40,8 @@
* @param[in] origin - origin of ipaddress(dhcp/static/SLAAC/LinkLocal).
*/
IPAddress(sdbusplus::bus_t& bus, std::string_view objRoot,
- stdplus::PinnedRef<EthernetInterface> parent, IfAddr addr,
- IP::AddressOrigin origin);
+ stdplus::PinnedRef<EthernetInterface> parent,
+ stdplus::SubnetAny addr, IP::AddressOrigin origin);
std::string address(std::string ipAddress) override;
uint8_t prefixLength(uint8_t) override;
@@ -72,8 +72,8 @@
sdbusplus::message::object_path objPath;
IPAddress(sdbusplus::bus_t& bus, sdbusplus::message::object_path objPath,
- stdplus::PinnedRef<EthernetInterface> parent, IfAddr addr,
- IP::AddressOrigin origin);
+ stdplus::PinnedRef<EthernetInterface> parent,
+ stdplus::SubnetAny addr, IP::AddressOrigin origin);
};
} // namespace network
diff --git a/src/neighbor.cpp b/src/neighbor.cpp
index afbc062..4d899c4 100644
--- a/src/neighbor.cpp
+++ b/src/neighbor.cpp
@@ -14,27 +14,30 @@
namespace network
{
-static auto makeObjPath(std::string_view root, InAddrAny addr)
+static auto makeObjPath(std::string_view root, stdplus::InAnyAddr addr)
{
auto ret = sdbusplus::message::object_path(std::string(root));
- ret /= std::to_string(addr);
+ stdplus::ToStrHandle<stdplus::ToStr<stdplus::InAnyAddr>> tsh;
+ ret /= tsh(addr);
return ret;
}
Neighbor::Neighbor(sdbusplus::bus_t& bus, std::string_view objRoot,
- stdplus::PinnedRef<EthernetInterface> parent, InAddrAny addr,
- stdplus::EtherAddr lladdr, State state) :
+ stdplus::PinnedRef<EthernetInterface> parent,
+ stdplus::InAnyAddr addr, stdplus::EtherAddr lladdr,
+ State state) :
Neighbor(bus, makeObjPath(objRoot, addr), parent, addr, lladdr, state)
{}
Neighbor::Neighbor(sdbusplus::bus_t& bus,
sdbusplus::message::object_path objPath,
- stdplus::PinnedRef<EthernetInterface> parent, InAddrAny addr,
- stdplus::EtherAddr lladdr, State state) :
+ stdplus::PinnedRef<EthernetInterface> parent,
+ stdplus::InAnyAddr addr, stdplus::EtherAddr lladdr,
+ State state) :
NeighborObj(bus, objPath.str.c_str(), NeighborObj::action::defer_emit),
parent(parent), objPath(std::move(objPath))
{
- NeighborObj::ipAddress(std::to_string(addr), true);
+ NeighborObj::ipAddress(stdplus::toStr(addr), true);
NeighborObj::macAddress(stdplus::toStr(lladdr), true);
NeighborObj::state(state, true);
emit_object_added();
diff --git a/src/neighbor.hpp b/src/neighbor.hpp
index 9f964e8..5072afe 100644
--- a/src/neighbor.hpp
+++ b/src/neighbor.hpp
@@ -39,8 +39,8 @@
* @param[in] state - The state of the neighbor entry.
*/
Neighbor(sdbusplus::bus_t& bus, std::string_view objRoot,
- stdplus::PinnedRef<EthernetInterface> parent, InAddrAny addr,
- stdplus::EtherAddr lladdr, State state);
+ stdplus::PinnedRef<EthernetInterface> parent,
+ stdplus::InAnyAddr addr, stdplus::EtherAddr lladdr, State state);
/** @brief Delete this d-bus object.
*/
@@ -66,8 +66,8 @@
sdbusplus::message::object_path objPath;
Neighbor(sdbusplus::bus_t& bus, sdbusplus::message::object_path objPath,
- stdplus::PinnedRef<EthernetInterface> parent, InAddrAny addr,
- stdplus::EtherAddr lladdr, State state);
+ stdplus::PinnedRef<EthernetInterface> parent,
+ stdplus::InAnyAddr addr, stdplus::EtherAddr lladdr, State state);
};
} // namespace network
diff --git a/src/network_manager.cpp b/src/network_manager.cpp
index ca3b517..26d3496 100644
--- a/src/network_manager.cpp
+++ b/src/network_manager.cpp
@@ -14,6 +14,7 @@
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/message.hpp>
+#include <stdplus/numeric/str.hpp>
#include <stdplus/pinned.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
@@ -66,7 +67,7 @@
{
throw std::invalid_argument("Invalid obj path");
}
- auto ifidx = DecodeInt<unsigned, 10>{}(obj.substr(sep + 3));
+ auto ifidx = stdplus::StrToInt<10, uint16_t>{}(obj.substr(sep + 3));
const auto& state = std::get<std::string>(it->second);
man.get().handleAdminState(state, ifidx);
}
@@ -356,23 +357,20 @@
}
}
-void Manager::addDefGw(unsigned ifidx, InAddrAny addr)
+void Manager::addDefGw(unsigned ifidx, stdplus::InAnyAddr addr)
{
if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
{
std::visit(
[&](auto addr) {
- if constexpr (std::is_same_v<in_addr, decltype(addr)>)
+ if constexpr (std::is_same_v<stdplus::In4Addr, decltype(addr)>)
{
it->second.defgw4.emplace(addr);
}
- else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
- {
- it->second.defgw6.emplace(addr);
- }
else
{
- static_assert(!std::is_same_v<void, decltype(addr)>);
+ static_assert(std::is_same_v<stdplus::In6Addr, decltype(addr)>);
+ it->second.defgw6.emplace(addr);
}
},
addr);
@@ -380,19 +378,17 @@
{
std::visit(
[&](auto addr) {
- if constexpr (std::is_same_v<in_addr, decltype(addr)>)
+ if constexpr (std::is_same_v<stdplus::In4Addr, decltype(addr)>)
{
it->second->EthernetInterfaceIntf::defaultGateway(
- std::to_string(addr));
- }
- else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
- {
- it->second->EthernetInterfaceIntf::defaultGateway6(
- std::to_string(addr));
+ stdplus::toStr(addr));
}
else
{
- static_assert(!std::is_same_v<void, decltype(addr)>);
+ static_assert(
+ std::is_same_v<stdplus::In6Addr, decltype(addr)>);
+ it->second->EthernetInterfaceIntf::defaultGateway6(
+ stdplus::toStr(addr));
}
},
addr);
@@ -404,54 +400,51 @@
}
}
-void Manager::removeDefGw(unsigned ifidx, InAddrAny addr)
+void Manager::removeDefGw(unsigned ifidx, stdplus::InAnyAddr addr)
{
if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
{
std::visit(
[&](auto addr) {
- if constexpr (std::is_same_v<in_addr, decltype(addr)>)
+ if constexpr (std::is_same_v<stdplus::In4Addr, decltype(addr)>)
{
if (it->second.defgw4 == addr)
{
it->second.defgw4.reset();
}
}
- else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
+ else
{
+ static_assert(std::is_same_v<stdplus::In6Addr, decltype(addr)>);
if (it->second.defgw6 == addr)
{
it->second.defgw6.reset();
}
}
- else
- {
- static_assert(!std::is_same_v<void, decltype(addr)>);
- }
},
addr);
if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
{
std::visit(
[&](auto addr) {
- if constexpr (std::is_same_v<in_addr, decltype(addr)>)
+ if constexpr (std::is_same_v<stdplus::In4Addr, decltype(addr)>)
{
- if (it->second->defaultGateway() == std::to_string(addr))
+ stdplus::ToStrHandle<stdplus::ToStr<stdplus::In4Addr>> tsh;
+ if (it->second->defaultGateway() == tsh(addr))
{
it->second->EthernetInterfaceIntf::defaultGateway("");
}
}
- else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
+ else
{
- if (it->second->defaultGateway6() == std::to_string(addr))
+ static_assert(
+ std::is_same_v<stdplus::In6Addr, decltype(addr)>);
+ stdplus::ToStrHandle<stdplus::ToStr<stdplus::In6Addr>> tsh;
+ if (it->second->defaultGateway6() == tsh(addr))
{
it->second->EthernetInterfaceIntf::defaultGateway6("");
}
}
- else
- {
- static_assert(!std::is_same_v<void, decltype(addr)>);
- }
},
addr);
}
diff --git a/src/network_manager.hpp b/src/network_manager.hpp
index f37234c..0ea8f2b 100644
--- a/src/network_manager.hpp
+++ b/src/network_manager.hpp
@@ -76,8 +76,8 @@
void removeNeighbor(const NeighborInfo& info);
/** @brief Add / remove default gateway for interface */
- void addDefGw(unsigned ifidx, InAddrAny addr);
- void removeDefGw(unsigned ifidx, InAddrAny addr);
+ void addDefGw(unsigned ifidx, stdplus::InAnyAddr addr);
+ void removeDefGw(unsigned ifidx, stdplus::InAnyAddr addr);
/** @brief gets the network conf directory.
*/
diff --git a/src/rtnetlink.cpp b/src/rtnetlink.cpp
index 4308d23..5229c94 100644
--- a/src/rtnetlink.cpp
+++ b/src/rtnetlink.cpp
@@ -88,11 +88,11 @@
}
template <typename Addr>
-static std::optional<std::tuple<unsigned, InAddrAny>>
+static std::optional<std::tuple<unsigned, stdplus::InAnyAddr>>
parse(std::string_view msg)
{
std::optional<unsigned> ifIdx;
- std::optional<InAddrAny> gw;
+ std::optional<stdplus::InAnyAddr> gw;
while (!msg.empty())
{
auto [hdr, data] = extractRtAttr(msg);
@@ -113,7 +113,7 @@
return std::nullopt;
}
-std::optional<std::tuple<unsigned, InAddrAny>>
+std::optional<std::tuple<unsigned, stdplus::InAnyAddr>>
gatewayFromRtm(std::string_view msg)
{
const auto& rtm = extractRtData<rtmsg>(msg);
@@ -124,9 +124,9 @@
switch (rtm.rtm_family)
{
case AF_INET:
- return parse<in_addr>(msg);
+ return parse<stdplus::In4Addr>(msg);
case AF_INET6:
- return parse<in6_addr>(msg);
+ return parse<stdplus::In6Addr>(msg);
}
return std::nullopt;
}
@@ -135,11 +135,8 @@
{
const auto& ifa = extractRtData<ifaddrmsg>(msg);
- AddressInfo ret;
- ret.ifidx = ifa.ifa_index;
- ret.flags = ifa.ifa_flags;
- ret.scope = ifa.ifa_scope;
- std::optional<InAddrAny> addr;
+ uint32_t flags = ifa.ifa_flags;
+ std::optional<stdplus::InAnyAddr> addr;
while (!msg.empty())
{
auto [hdr, data] = extractRtAttr(msg);
@@ -149,15 +146,17 @@
}
else if (hdr.rta_type == IFA_FLAGS)
{
- ret.flags = stdplus::raw::copyFromStrict<uint32_t>(data);
+ flags = stdplus::raw::copyFromStrict<uint32_t>(data);
}
}
if (!addr)
{
throw std::runtime_error("Missing address");
}
- ret.ifaddr = {*addr, ifa.ifa_prefixlen};
- return ret;
+ return AddressInfo{.ifidx = ifa.ifa_index,
+ .ifaddr = stdplus::SubnetAny{*addr, ifa.ifa_prefixlen},
+ .scope = ifa.ifa_scope,
+ .flags = flags};
}
NeighborInfo neighFromRtm(std::string_view msg)
diff --git a/src/rtnetlink.hpp b/src/rtnetlink.hpp
index 5436e9d..f35c25f 100644
--- a/src/rtnetlink.hpp
+++ b/src/rtnetlink.hpp
@@ -10,7 +10,7 @@
InterfaceInfo intfFromRtm(std::string_view msg);
-std::optional<std::tuple<unsigned, InAddrAny>>
+std::optional<std::tuple<unsigned, stdplus::InAnyAddr>>
gatewayFromRtm(std::string_view msg);
AddressInfo addrFromRtm(std::string_view msg);
diff --git a/src/rtnetlink_server.cpp b/src/rtnetlink_server.cpp
index cfa0b2c..4a211d5 100644
--- a/src/rtnetlink_server.cpp
+++ b/src/rtnetlink_server.cpp
@@ -21,7 +21,7 @@
{
return;
}
- cb(std::get<unsigned>(*ret), std::get<InAddrAny>(*ret));
+ cb(std::get<unsigned>(*ret), std::get<stdplus::InAnyAddr>(*ret));
}
static unsigned getIfIdx(const nlmsghdr& hdr, std::string_view data)
diff --git a/src/system_queries.cpp b/src/system_queries.cpp
index e922504..f58493a 100644
--- a/src/system_queries.cpp
+++ b/src/system_queries.cpp
@@ -17,6 +17,7 @@
#include <optional>
#include <stdexcept>
#include <string_view>
+#include <unordered_set>
namespace phosphor::network::system
{
diff --git a/src/types.cpp b/src/types.cpp
index 901d77a..0be9e32 100644
--- a/src/types.cpp
+++ b/src/types.cpp
@@ -1,46 +1 @@
#include "types.hpp"
-
-#include <fmt/format.h>
-
-#include <stdplus/hash.hpp>
-
-void phosphor::network::IfAddr::invalidPfx(uint8_t pfx)
-{
- throw std::invalid_argument(fmt::format("Invalid prefix {}", pfx));
-}
-
-std::size_t std::hash<in_addr>::operator()(in_addr addr) const noexcept
-{
- return stdplus::hashMulti(addr.s_addr);
-}
-
-std::size_t std::hash<in6_addr>::operator()(in6_addr addr) const noexcept
-{
- return stdplus::hashMulti(addr.s6_addr32);
-}
-
-std::size_t std::hash<phosphor::network::IfAddr>::operator()(
- phosphor::network::IfAddr addr) const noexcept
-{
- return stdplus::hashMulti(addr.getAddr(), addr.getPfx());
-}
-
-std::string std::to_string(in_addr value)
-{
- return string(phosphor::network::detail::ToStrBuf<in_addr>{}(value));
-}
-std::string std::to_string(in6_addr value)
-{
- return string(phosphor::network::detail::ToStrBuf<in6_addr>{}(value));
-}
-std::string std::to_string(phosphor::network::InAddrAny value)
-{
- phosphor::network::detail::ToStrBuf<phosphor::network::InAddrAny> tsb;
- return string(tsb(value));
-}
-
-std::string std::to_string(phosphor::network::IfAddr value)
-{
- phosphor::network::detail::ToStrBuf<phosphor::network::IfAddr> tsb;
- return string(tsb(value));
-}
diff --git a/src/types.hpp b/src/types.hpp
index 7db6a37..14de360 100644
--- a/src/types.hpp
+++ b/src/types.hpp
@@ -1,76 +1,15 @@
#pragma once
-#include <fmt/core.h>
-#include <netinet/in.h>
-
#include <stdplus/net/addr/ether.hpp>
-#include <stdplus/numeric/endian.hpp>
+#include <stdplus/net/addr/ip.hpp>
+#include <stdplus/net/addr/subnet.hpp>
-#include <algorithm>
#include <array>
-#include <numeric>
#include <optional>
#include <string>
-#include <string_view>
-#include <type_traits>
#include <unordered_map>
-#include <unordered_set>
-#include <variant>
-constexpr bool operator==(in_addr lhs, in_addr rhs) noexcept
+namespace phosphor::network
{
- return lhs.s_addr == rhs.s_addr;
-}
-
-constexpr bool operator==(in6_addr lhs, in6_addr rhs) noexcept
-{
- return std::equal(lhs.s6_addr32, lhs.s6_addr32 + 4, rhs.s6_addr32);
-}
-
-namespace phosphor
-{
-namespace network
-{
-
-// Byte representations for common address types in network byte order
-using InAddrAny = std::variant<in_addr, in6_addr>;
-class IfAddr
-{
- private:
- InAddrAny addr;
- uint8_t pfx;
-
- static void invalidPfx(uint8_t pfx);
-
- public:
- constexpr IfAddr() : addr({}), pfx(0) {}
-
- constexpr IfAddr(InAddrAny addr, uint8_t pfx) : addr(addr), pfx(pfx)
- {
- std::visit(
- [pfx](auto v) {
- if (sizeof(v) * 8 < pfx)
- {
- invalidPfx(pfx);
- }
- },
- addr);
- }
-
- constexpr auto getAddr() const
- {
- return addr;
- }
-
- constexpr auto getPfx() const
- {
- return pfx;
- }
-
- constexpr bool operator==(phosphor::network::IfAddr rhs) const noexcept
- {
- return addr == rhs.addr && pfx == rhs.pfx;
- }
-};
/** @class InterfaceInfo
* @brief Information about interfaces from the kernel
@@ -102,7 +41,7 @@
struct AddressInfo
{
unsigned ifidx;
- IfAddr ifaddr;
+ stdplus::SubnetAny ifaddr;
uint8_t scope;
uint32_t flags;
@@ -120,7 +59,7 @@
{
unsigned ifidx;
uint16_t state;
- std::optional<InAddrAny> addr;
+ std::optional<stdplus::InAnyAddr> addr;
std::optional<stdplus::EtherAddr> mac;
constexpr bool operator==(const NeighborInfo& rhs) const noexcept
@@ -130,538 +69,14 @@
}
};
-namespace detail
-{
-inline constexpr auto charLookup = []() {
- std::array<int8_t, 256> ret;
- std::fill(ret.begin(), ret.end(), -1);
- for (int8_t i = 0; i < 10; ++i)
- {
- ret[i + '0'] = i;
- }
- for (int8_t i = 0; i < 26; ++i)
- {
- ret[i + 'A'] = i + 10;
- ret[i + 'a'] = i + 10;
- }
- return ret;
-}();
-inline constexpr auto intLookup = []() {
- std::array<char, 36> ret;
- for (int8_t i = 0; i < 10; ++i)
- {
- ret[i] = i + '0';
- }
- for (int8_t i = 0; i < 26; ++i)
- {
- ret[i + 10] = i + 'a';
- }
- return ret;
-}();
-} // namespace detail
-
-template <typename T, uint8_t base>
-struct DecodeInt
-{
- static_assert(base > 1 && base <= 36);
- static_assert(std::is_unsigned_v<T>);
-
- constexpr T operator()(std::string_view str) const
- {
- if (str.empty())
- {
- throw std::invalid_argument("Empty Str");
- }
- constexpr auto max = std::numeric_limits<T>::max();
- auto ret = std::accumulate(str.begin(), str.end(), T{},
- [&](T r, char c) {
- auto v = detail::charLookup[c];
- if (v < 0 || v >= base)
- {
- throw std::invalid_argument("Invalid numeral");
- }
- if constexpr (std::popcount(base) == 1)
- {
- constexpr auto shift = std::countr_zero(base);
- constexpr auto maxshift = max >> shift;
- if (r > maxshift)
- {
- throw std::overflow_error("Integer Decode");
- }
- return (r << shift) | v;
- }
- else
- {
- constexpr auto maxbase = max / base;
- if (r > maxbase)
- {
- throw std::overflow_error("Integer Decode");
- }
- r *= base;
- if (max - v < r)
- {
- throw std::overflow_error("Integer Decode");
- }
- return r + v;
- }
- });
- return ret;
- }
-};
-
-template <typename T, uint8_t base>
-struct EncodeInt
-{
- static_assert(base > 1 && base <= 36);
- static_assert(std::is_unsigned_v<T>);
-
- static constexpr uint8_t buf_size = []() {
- T v = std::numeric_limits<T>::max();
- uint8_t i = 0;
- for (; v != 0; ++i)
- {
- v /= base;
- }
- return i;
- }();
- using buf_type = std::array<char, buf_size>;
-
- constexpr uint8_t reverseFill(char* buf, T v) const noexcept
- {
- uint8_t i = 0;
- do
- {
- if constexpr (std::popcount(base) == 1)
- {
- buf[i++] = detail::intLookup[v & 0xf];
- v >>= 4;
- }
- else
- {
- buf[i++] = detail::intLookup[v % base];
- v /= base;
- }
- } while (v > 0);
- return i;
- }
-
- constexpr char* operator()(char* buf, T v) const noexcept
- {
- uint8_t i = reverseFill(buf, v);
- std::reverse(buf, buf + i);
- return buf + i;
- }
-
- constexpr char* operator()(char* buf, T v, uint8_t min_width) const noexcept
- {
- uint8_t i = reverseFill(buf, v);
- auto end = buf + std::max(i, min_width);
- std::fill(buf + i, end, '0');
- std::reverse(buf, end);
- return end;
- }
-};
-
-template <typename T>
-struct ToAddr
-{};
-
-template <>
-struct ToAddr<in_addr>
-{
- constexpr in_addr operator()(std::string_view str) const
- {
- constexpr DecodeInt<uint8_t, 10> di;
- uint32_t addr = {};
- for (size_t i = 0; i < 3; ++i)
- {
- auto loc = str.find(".");
- addr |= di(str.substr(0, loc));
- addr <<= 8;
- str.remove_prefix(loc == str.npos ? str.size() : loc + 1);
- if (str.empty())
- {
- throw std::invalid_argument("Missing addr data");
- }
- }
- addr |= di(str);
- return {stdplus::hton(addr)};
- }
-};
-
-template <>
-struct ToAddr<in6_addr>
-{
- constexpr in6_addr operator()(std::string_view str) const
- {
- constexpr DecodeInt<uint16_t, 16> di;
- in6_addr ret = {};
- size_t i = 0;
- while (i < 8)
- {
- auto loc = str.find(':');
- if (i == 6 && loc == str.npos)
- {
- ret.s6_addr32[3] = ToAddr<in_addr>{}(str).s_addr;
- return ret;
- }
- if (loc != 0 && !str.empty())
- {
- ret.s6_addr16[i++] = stdplus::hton(di(str.substr(0, loc)));
- }
- if (i < 8 && str.size() > loc + 1 && str[loc + 1] == ':')
- {
- str.remove_prefix(loc + 2);
- break;
- }
- else if (str.empty())
- {
- throw std::invalid_argument("IPv6 Data");
- }
- str.remove_prefix(loc == str.npos ? str.size() : loc + 1);
- }
- if (str.starts_with(':'))
- {
- throw std::invalid_argument("Extra separator");
- }
- size_t j = 7;
- if (!str.empty() && i < 6 && str.find('.') != str.npos)
- {
- auto loc = str.rfind(':');
- ret.s6_addr32[3] =
- ToAddr<in_addr>{}(str.substr(loc == str.npos ? 0 : loc + 1))
- .s_addr;
- str.remove_suffix(loc == str.npos ? str.size() : str.size() - loc);
- j -= 2;
- }
- while (!str.empty() && j > i)
- {
- auto loc = str.rfind(':');
- ret.s6_addr16[j--] =
- stdplus::hton(di(str.substr(loc == str.npos ? 0 : loc + 1)));
- str.remove_suffix(loc == str.npos ? str.size() : str.size() - loc);
- }
- if (!str.empty())
- {
- throw std::invalid_argument("Too much data");
- }
- return ret;
- }
-};
-
-template <>
-struct ToAddr<InAddrAny>
-{
- constexpr InAddrAny operator()(std::string_view str) const
- {
- if (str.find(':') == str.npos)
- {
- return ToAddr<in_addr>{}(str);
- }
- return ToAddr<in6_addr>{}(str);
- }
-};
-
-template <>
-struct ToAddr<IfAddr>
-{
- constexpr IfAddr operator()(std::string_view str) const
- {
- auto pos = str.rfind('/');
- if (pos == str.npos)
- {
- throw std::invalid_argument("Invalid IfAddr");
- }
- return {ToAddr<InAddrAny>{}(str.substr(0, pos)),
- DecodeInt<uint8_t, 10>{}(str.substr(pos + 1))};
- }
-};
-
-template <typename T>
-struct ToStr
-{};
-
-template <>
-struct ToStr<char>
-{
- static constexpr uint8_t buf_size = 1;
- using buf_type = std::array<char, buf_size>;
-
- constexpr char* operator()(char* buf, char v) const noexcept
- {
- buf[0] = v;
- return buf + 1;
- }
-};
-
-template <>
-struct ToStr<in_addr>
-{
- // 4 octets * 3 dec chars + 3 separators
- static constexpr uint8_t buf_size = 15;
- using buf_type = std::array<char, buf_size>;
-
- constexpr char* operator()(char* buf, in_addr v) const noexcept
- {
- auto n = stdplus::bswap(stdplus::ntoh(v.s_addr));
- for (size_t i = 0; i < 3; ++i)
- {
- buf = ToStr<char>{}(EncodeInt<uint8_t, 10>{}(buf, n & 0xff), '.');
- n >>= 8;
- }
- return EncodeInt<uint8_t, 10>{}(buf, n & 0xff);
- }
-};
-
-template <>
-struct ToStr<in6_addr>
-{
- // 8 hextets * 4 hex chars + 7 separators
- static constexpr uint8_t buf_size = 39;
- using buf_type = std::array<char, buf_size>;
-
- constexpr char* operator()(char* buf, in6_addr v) const noexcept
- {
- // IPv4 in IPv6 Addr
- if (v.s6_addr32[0] == 0 && v.s6_addr32[1] == 0 &&
- v.s6_addr32[2] == stdplus::hton(uint32_t(0xffff)))
- {
- constexpr auto prefix = std::string_view("::ffff:");
- return ToStr<in_addr>{}(
- std::copy(prefix.begin(), prefix.end(), buf), {v.s6_addr32[3]});
- }
-
- size_t skip_start = 0;
- size_t skip_size = 0;
- {
- size_t new_start = 0;
- size_t new_size = 0;
- for (size_t i = 0; i < 9; ++i)
- {
- if (i < 8 && v.s6_addr16[i] == 0)
- {
- if (new_start + new_size == i)
- {
- new_size++;
- }
- else
- {
- new_start = i;
- new_size = 1;
- }
- }
- else if (new_start + new_size == i && new_size > skip_size)
- {
- skip_start = new_start;
- skip_size = new_size;
- }
- }
- }
- for (size_t i = 0; i < 8; ++i)
- {
- if (i == skip_start && skip_size > 1)
- {
- if (i == 0)
- {
- *(buf++) = ':';
- }
- *(buf++) = ':';
- i += skip_size - 1;
- continue;
- }
- buf = EncodeInt<uint16_t, 16>{}(buf, stdplus::ntoh(v.s6_addr16[i]));
- if (i < 7)
- {
- *(buf++) = ':';
- }
- }
- return buf;
- }
-};
-
-template <>
-struct ToStr<InAddrAny>
-{
- // IPv6 is the bigger of the addrs
- static constexpr uint8_t buf_size = ToStr<in6_addr>::buf_size;
- using buf_type = std::array<char, buf_size>;
-
- constexpr char* operator()(char* buf, InAddrAny v) const noexcept
- {
- return std::visit([=](auto v) { return ToStr<decltype(v)>{}(buf, v); },
- v);
- }
-};
-
-template <>
-struct ToStr<IfAddr>
-{
- // InAddrAny + sep + 3 prefix chars
- static constexpr uint8_t buf_size = ToStr<InAddrAny>::buf_size + 4;
- using buf_type = std::array<char, buf_size>;
-
- constexpr char* operator()(char* buf, IfAddr v) const noexcept
- {
- buf = ToStr<InAddrAny>{}(buf, v.getAddr());
- buf = ToStr<char>{}(buf, '/');
- return EncodeInt<uint8_t, 10>{}(buf, v.getPfx());
- }
-};
-
-namespace detail
-{
-
-template <typename T>
-constexpr bool vcontains() noexcept
-{
- return false;
-}
-
-template <typename T, typename V, typename... Vs>
-constexpr bool vcontains() noexcept
-{
- return vcontains<T, Vs...>() || std::is_same_v<T, V>;
-}
-
-template <typename T, typename... Types>
-constexpr std::enable_if_t<vcontains<T, Types...>(), bool>
- veq(T t, std::variant<Types...> v) noexcept
-{
- return std::visit(
- [t](auto v) {
- if constexpr (std::is_same_v<T, decltype(v)>)
- {
- return v == t;
- }
- else
- {
- return false;
- }
- },
- v);
-}
-
-template <typename T>
-struct ToStrBuf
-{
- public:
- constexpr std::string_view operator()(T v) noexcept
- {
- return {buf.data(), ToStr<T>{}(buf.data(), v)};
- }
-
- private:
- typename ToStr<T>::buf_type buf;
-};
-
-template <typename T>
-struct Format
-{
- private:
- fmt::formatter<std::string_view> formatter;
-
- public:
- template <typename ParseContext>
- constexpr auto parse(ParseContext& ctx)
- {
- return ctx.begin();
- }
-
- template <typename FormatContext>
- auto format(auto v, FormatContext& ctx) const
- {
- return formatter.format(ToStrBuf<T>{}(v), ctx);
- }
-};
-} // namespace detail
-} // namespace network
-} // namespace phosphor
-
-template <>
-struct std::hash<in_addr>
-{
- std::size_t operator()(in_addr addr) const noexcept;
-};
-
-template <>
-struct std::hash<in6_addr>
-{
- std::size_t operator()(in6_addr addr) const noexcept;
-};
-
-template <>
-struct std::hash<phosphor::network::IfAddr>
-{
- std::size_t operator()(phosphor::network::IfAddr addr) const noexcept;
-};
-
-namespace fmt
-{
-template <>
-struct formatter<in_addr> : phosphor::network::detail::Format<in_addr>
-{};
-template <>
-struct formatter<in6_addr> : phosphor::network::detail::Format<in6_addr>
-{};
-template <>
-struct formatter<phosphor::network::InAddrAny> :
- phosphor::network::detail::Format<phosphor::network::InAddrAny>
-{};
-template <>
-struct formatter<phosphor::network::IfAddr> :
- phosphor::network::detail::Format<phosphor::network::IfAddr>
-{};
-} // namespace fmt
-
-namespace std
-{
-string to_string(in_addr value);
-string to_string(in6_addr value);
-string to_string(phosphor::network::InAddrAny value);
-string to_string(phosphor::network::IfAddr value);
-} // namespace std
-
-template <typename T>
-constexpr std::enable_if_t<!std::is_same_v<phosphor::network::InAddrAny, T>,
- bool>
- operator==(phosphor::network::InAddrAny lhs, T rhs) noexcept
-{
- return phosphor::network::detail::veq(rhs, lhs);
-}
-
-auto& operator<<(auto& os, in_addr v)
-{
- return os << phosphor::network::detail::ToStrBuf<in_addr>{}(v);
-}
-
-auto& operator<<(auto& os, in6_addr v)
-{
- return os << phosphor::network::detail::ToStrBuf<in6_addr>{}(v);
-}
-
-auto& operator<<(auto& os, phosphor::network::InAddrAny v)
-{
- phosphor::network::detail::ToStrBuf<phosphor::network::InAddrAny> tsb;
- return os << tsb(v);
-}
-
-auto& operator<<(auto& os, phosphor::network::IfAddr v)
-{
- phosphor::network::detail::ToStrBuf<phosphor::network::IfAddr> tsb;
- return os << tsb(v);
-}
-
-namespace phosphor::network
-{
-
/** @brief Contains all of the object information about the interface */
struct AllIntfInfo
{
InterfaceInfo intf;
- std::optional<in_addr> defgw4 = std::nullopt;
- std::optional<in6_addr> defgw6 = std::nullopt;
- std::unordered_map<IfAddr, AddressInfo> addrs = {};
- std::unordered_map<InAddrAny, NeighborInfo> staticNeighs = {};
+ std::optional<stdplus::In4Addr> defgw4 = std::nullopt;
+ std::optional<stdplus::In6Addr> defgw6 = std::nullopt;
+ std::unordered_map<stdplus::SubnetAny, AddressInfo> addrs = {};
+ std::unordered_map<stdplus::InAnyAddr, NeighborInfo> staticNeighs = {};
};
} // namespace phosphor::network
diff --git a/src/util.cpp b/src/util.cpp
index 7102372..b04cfd6 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -11,6 +11,7 @@
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
+#include <stdplus/numeric/str.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <cctype>
@@ -134,7 +135,7 @@
unsigned idx;
try
{
- idx = DecodeInt<unsigned, 10>{}(intf);
+ idx = stdplus::StrToInt<10, unsigned>{}(intf);
}
catch (...)
{
diff --git a/src/util.hpp b/src/util.hpp
index 7736f67..abfd8eb 100644
--- a/src/util.hpp
+++ b/src/util.hpp
@@ -23,14 +23,14 @@
* @param[in] family - The address family of the buf
* @param[in] buf - The network byte order address
*/
-constexpr InAddrAny addrFromBuf(int family, std::string_view buf)
+constexpr stdplus::InAnyAddr addrFromBuf(int family, std::string_view buf)
{
switch (family)
{
case AF_INET:
- return stdplus::raw::copyFromStrict<in_addr>(buf);
+ return stdplus::raw::copyFromStrict<stdplus::In4Addr>(buf);
case AF_INET6:
- return stdplus::raw::copyFromStrict<in6_addr>(buf);
+ return stdplus::raw::copyFromStrict<stdplus::In6Addr>(buf);
}
throw std::invalid_argument("Unrecognized family");
}