ethernet_interface: Simplify DHCPConfiguration enumeration
We don't have more than 2 types of DHCP objects, don't use an arbitrary
vector and depend on enum values for indices.
Change-Id: I5c519e8b95b273a4684e553f18027f038d025f17
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/dhcp_configuration.cpp b/src/dhcp_configuration.cpp
index 4acfa3d..149dd60 100644
--- a/src/dhcp_configuration.cpp
+++ b/src/dhcp_configuration.cpp
@@ -26,7 +26,7 @@
stdplus::PinnedRef<EthernetInterface> parent,
DHCPType type) :
Iface(bus, objPath.c_str(), Iface::action::defer_emit),
- parent(parent), type(type)
+ parent(parent)
{
config::Parser conf;
std::filesystem::directory_entry newest_file;
diff --git a/src/dhcp_configuration.hpp b/src/dhcp_configuration.hpp
index c5bcf8a..75feb26 100644
--- a/src/dhcp_configuration.hpp
+++ b/src/dhcp_configuration.hpp
@@ -89,7 +89,6 @@
private:
/** @brief Ethernet Interface object. */
stdplus::PinnedRef<EthernetInterface> parent;
- DHCPType type;
};
} // namespace dhcp
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index fffb53a..5aae789 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -118,7 +118,8 @@
{
EthernetInterface::defaultGateway6(stdplus::toStr(*info.defgw6), true);
}
- addDHCPConfigurations();
+ dhcp4Conf.emplace(bus, this->objPath + "/dhcp4", *this, DHCPType::v4);
+ dhcp6Conf.emplace(bus, this->objPath + "/dhcp6", *this, DHCPType::v6);
emit_object_added();
if (info.intf.vlan_id)
@@ -658,6 +659,11 @@
elog<NotAllowed>(NotAllowedArgument::REASON("ReadOnly Property"));
}
+static constexpr std::string_view tfStr(bool value)
+{
+ return value ? "true"sv : "false"sv;
+}
+
void EthernetInterface::writeConfigurationFile()
{
config::Parser config;
@@ -756,27 +762,19 @@
{
auto& dhcp4 = config.map["DHCPv4"].emplace_back();
dhcp4["ClientIdentifier"].emplace_back("mac");
- const auto& conf = *dhcpConfigs[static_cast<int>(DHCPType::v4)];
- auto dns_enabled = conf.dnsEnabled() ? "true" : "false";
- auto domain_enabled = conf.domainEnabled() ? "true" : "false";
- dhcp4["UseDNS"].emplace_back(dns_enabled);
- dhcp4["UseDomains"].emplace_back(domain_enabled);
- dhcp4["UseNTP"].emplace_back(conf.ntpEnabled() ? "true" : "false");
- dhcp4["UseHostname"].emplace_back(conf.hostNameEnabled() ? "true"
- : "false");
+ dhcp4["UseDNS"].emplace_back(tfStr(dhcp4Conf->dnsEnabled()));
+ dhcp4["UseDomains"].emplace_back(tfStr(dhcp4Conf->domainEnabled()));
+ dhcp4["UseNTP"].emplace_back(tfStr(dhcp4Conf->ntpEnabled()));
+ dhcp4["UseHostname"].emplace_back(tfStr(dhcp4Conf->hostNameEnabled()));
dhcp4["SendHostname"].emplace_back(
- conf.sendHostNameEnabled() ? "true" : "false");
+ tfStr(dhcp4Conf->sendHostNameEnabled()));
}
{
auto& dhcp6 = config.map["DHCPv6"].emplace_back();
- const auto& conf = *dhcpConfigs[static_cast<int>(DHCPType::v6)];
- auto dns_enabled = conf.dnsEnabled() ? "true" : "false";
- auto domain_enabled = conf.domainEnabled() ? "true" : "false";
- dhcp6["UseDNS"].emplace_back(dns_enabled);
- dhcp6["UseDomains"].emplace_back(domain_enabled);
- dhcp6["UseNTP"].emplace_back(conf.ntpEnabled() ? "true" : "false");
- dhcp6["UseHostname"].emplace_back(conf.hostNameEnabled() ? "true"
- : "false");
+ dhcp6["UseDNS"].emplace_back(tfStr(dhcp6Conf->dnsEnabled()));
+ dhcp6["UseDomains"].emplace_back(tfStr(dhcp6Conf->domainEnabled()));
+ dhcp6["UseNTP"].emplace_back(tfStr(dhcp6Conf->ntpEnabled()));
+ dhcp6["UseHostname"].emplace_back(tfStr(dhcp6Conf->hostNameEnabled()));
}
auto path = config::pathForIntfConf(manager.get().getConfDir(),
interfaceName());
@@ -970,14 +968,6 @@
eth.get().manager.get().reloadConfigs();
}
-void EthernetInterface::addDHCPConfigurations()
-{
- this->dhcpConfigs.emplace_back(std::make_unique<dhcp::Configuration>(
- bus, objPath + "/dhcp4", *this, DHCPType::v4));
- this->dhcpConfigs.emplace_back(std::make_unique<dhcp::Configuration>(
- bus, objPath + "/dhcp6", *this, DHCPType::v6));
-}
-
void EthernetInterface::reloadConfigs()
{
manager.get().reloadConfigs();
diff --git a/src/ethernet_interface.hpp b/src/ethernet_interface.hpp
index f41a381..846d9c9 100644
--- a/src/ethernet_interface.hpp
+++ b/src/ethernet_interface.hpp
@@ -248,6 +248,8 @@
};
std::optional<VlanProperties> vlan;
+ std::optional<dhcp::Configuration> dhcp4Conf, dhcp6Conf;
+
friend class TestEthernetInterface;
friend class TestNetworkManager;
@@ -262,14 +264,6 @@
* @returns true/false value if the address is static
*/
bool originIsManuallyAssigned(IP::AddressOrigin origin);
-
- /** @brief Function to add DHCP configurations.
- */
- void addDHCPConfigurations();
-
- /** @brief Map of DHCP conf objects.
- */
- std::vector<std::unique_ptr<dhcp::Configuration>> dhcpConfigs;
};
} // namespace network