blob: 6684713f83c43963ce1910975d3852218adbc251 [file] [log] [blame]
Gunnar Mills57d9c502018-09-14 14:42:34 -05001#include "config.h"
2
Ratan Gupta935bc332017-07-11 17:47:14 +05303#include "dhcp_configuration.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07004
Ratan Gupta935bc332017-07-11 17:47:14 +05305#include "network_manager.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07006
William A. Kennington III25511a12022-08-04 16:32:28 -07007#include <fmt/format.h>
8
Nagaraju Goruganti210420a2018-03-07 09:22:28 -06009#include <phosphor-logging/elog-errors.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070010#include <phosphor-logging/log.hpp>
11#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta935bc332017-07-11 17:47:14 +053012
13namespace phosphor
14{
15namespace network
16{
17namespace dhcp
18{
19
Nagaraju Goruganti210420a2018-03-07 09:22:28 -060020using namespace phosphor::network;
21using namespace phosphor::logging;
22using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -060023bool Configuration::sendHostNameEnabled(bool value)
24{
25 if (value == sendHostNameEnabled())
26 {
27 return value;
28 }
29
30 auto name = ConfigIntf::sendHostNameEnabled(value);
William A. Kennington IIIbd649af2021-10-08 17:55:13 -070031
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -060032 manager.writeToConfigurationFile();
William A. Kennington IIIbd649af2021-10-08 17:55:13 -070033 manager.reloadConfigs();
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -060034
35 return name;
36}
37
Ratan Gupta935bc332017-07-11 17:47:14 +053038bool Configuration::hostNameEnabled(bool value)
39{
40 if (value == hostNameEnabled())
41 {
42 return value;
43 }
44
45 auto name = ConfigIntf::hostNameEnabled(value);
46 manager.writeToConfigurationFile();
William A. Kennington IIIbd649af2021-10-08 17:55:13 -070047 manager.reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +053048
49 return name;
50}
51
Patrick Williams6aef7692021-05-01 06:39:41 -050052bool Configuration::ntpEnabled(bool value)
Ratan Gupta935bc332017-07-11 17:47:14 +053053{
Patrick Williams6aef7692021-05-01 06:39:41 -050054 if (value == ntpEnabled())
Ratan Gupta935bc332017-07-11 17:47:14 +053055 {
56 return value;
57 }
58
Patrick Williams6aef7692021-05-01 06:39:41 -050059 auto ntp = ConfigIntf::ntpEnabled(value);
Ratan Gupta935bc332017-07-11 17:47:14 +053060 manager.writeToConfigurationFile();
William A. Kennington IIIbd649af2021-10-08 17:55:13 -070061 manager.reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +053062
63 return ntp;
64}
65
Patrick Williams6aef7692021-05-01 06:39:41 -050066bool Configuration::dnsEnabled(bool value)
Ratan Gupta935bc332017-07-11 17:47:14 +053067{
Patrick Williams6aef7692021-05-01 06:39:41 -050068 if (value == dnsEnabled())
Ratan Gupta935bc332017-07-11 17:47:14 +053069 {
70 return value;
71 }
72
Patrick Williams6aef7692021-05-01 06:39:41 -050073 auto dns = ConfigIntf::dnsEnabled(value);
Ratan Gupta935bc332017-07-11 17:47:14 +053074 manager.writeToConfigurationFile();
William A. Kennington IIIbd649af2021-10-08 17:55:13 -070075 manager.reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +053076
77 return dns;
78}
79
Nagaraju Goruganti210420a2018-03-07 09:22:28 -060080bool Configuration::getDHCPPropFromConf(const std::string& prop)
81{
Nagaraju Goruganti210420a2018-03-07 09:22:28 -060082 auto interfaceStrList = getInterfaces();
Nagaraju Goruganti210420a2018-03-07 09:22:28 -060083 // systemd default behaviour is all DHCP fields should be enabled by
84 // default.
William A. Kennington IIIa520a392022-08-08 12:17:34 -070085 config::Parser parser(config::pathForIntfConf(manager.getConfDir(),
86 *interfaceStrList.begin()));
Nagaraju Goruganti210420a2018-03-07 09:22:28 -060087
William A. Kennington III34bb3e22022-08-18 15:17:22 -070088 auto value = parser.map.getLastValueString("DHCP", prop);
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -070089 if (value == nullptr)
Nagaraju Goruganti210420a2018-03-07 09:22:28 -060090 {
William A. Kennington III25511a12022-08-04 16:32:28 -070091 auto msg = fmt::format("Missing config section DHCP[{}]", prop);
92 log<level::NOTICE>(msg.c_str(), entry("PROP=%s", prop.c_str()));
93 return true;
Nagaraju Goruganti210420a2018-03-07 09:22:28 -060094 }
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -070095 auto ret = config::parseBool(*value);
William A. Kennington III150753f2022-08-05 15:20:54 -070096 if (!ret.has_value())
97 {
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -070098 auto msg =
99 fmt::format("Failed to parse section DHCP[{}]: `{}`", prop, *value);
William A. Kennington III150753f2022-08-05 15:20:54 -0700100 log<level::NOTICE>(msg.c_str(), entry("PROP=%s", prop.c_str()));
101 }
102 return ret.value_or(true);
Nagaraju Goruganti210420a2018-03-07 09:22:28 -0600103}
Gunnar Mills57d9c502018-09-14 14:42:34 -0500104} // namespace dhcp
105} // namespace network
106} // namespace phosphor