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/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();