blob: c64956849745ce9a3082794f2dd0d3b7ea43a660 [file] [log] [blame]
Ratan Gupta935bc332017-07-11 17:47:14 +05301#include "dhcp_configuration.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07002
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -07003#include "config_parser.hpp"
Ratan Gupta935bc332017-07-11 17:47:14 +05304#include "network_manager.hpp"
William A. Kennington III95530ec2022-08-19 01:44:39 -07005#include "util.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07006
William A. Kennington III180498c2022-11-14 18:11:45 -08007#include <sys/stat.h>
8
Nagaraju Goruganti210420a2018-03-07 09:22:28 -06009#include <phosphor-logging/elog-errors.hpp>
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -070010#include <phosphor-logging/lg2.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070011#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta935bc332017-07-11 17:47:14 +053012
Patrick Williams89d734b2023-05-10 07:50:25 -050013#include <filesystem>
14
Ratan Gupta935bc332017-07-11 17:47:14 +053015namespace phosphor
16{
17namespace network
18{
19namespace dhcp
20{
21
Nagaraju Goruganti210420a2018-03-07 09:22:28 -060022using namespace phosphor::network;
Nagaraju Goruganti210420a2018-03-07 09:22:28 -060023using namespace sdbusplus::xyz::openbmc_project::Common::Error;
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070024
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070025Configuration::Configuration(sdbusplus::bus_t& bus,
William A. Kennington III9ede1b72022-11-21 01:59:28 -080026 stdplus::const_zstring objPath,
27 stdplus::PinnedRef<Manager> parent) :
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070028 Iface(bus, objPath.c_str(), Iface::action::defer_emit),
William A. Kennington III9ede1b72022-11-21 01:59:28 -080029 manager(parent)
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070030{
31 config::Parser conf;
William A. Kennington III180498c2022-11-14 18:11:45 -080032 std::filesystem::directory_entry newest_file;
33 time_t newest_time = 0;
34 for (const auto& dirent :
William A. Kennington III9ede1b72022-11-21 01:59:28 -080035 std::filesystem::directory_iterator(manager.get().getConfDir()))
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070036 {
William A. Kennington III180498c2022-11-14 18:11:45 -080037 struct stat st = {};
38 stat(dirent.path().native().c_str(), &st);
39 if (st.st_mtime > newest_time)
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070040 {
William A. Kennington III180498c2022-11-14 18:11:45 -080041 newest_file = dirent;
42 newest_time = st.st_mtime;
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070043 }
44 }
William A. Kennington III180498c2022-11-14 18:11:45 -080045 if (newest_file != std::filesystem::directory_entry{})
46 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -070047 lg2::info("Using DHCP options from {CFG_FILE}", "CFG_FILE",
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -070048 newest_file.path().native());
William A. Kennington III180498c2022-11-14 18:11:45 -080049 conf.setFile(newest_file.path());
50 }
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070051
William A. Kennington IIId99e6db2022-11-15 20:39:45 -080052 ConfigIntf::dnsEnabled(getDHCPProp(conf, "UseDNS"), true);
53 ConfigIntf::ntpEnabled(getDHCPProp(conf, "UseNTP"), true);
54 ConfigIntf::hostNameEnabled(getDHCPProp(conf, "UseHostname"), true);
55 ConfigIntf::sendHostNameEnabled(getDHCPProp(conf, "SendHostname"), true);
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070056 emit_object_added();
57}
58
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -060059bool Configuration::sendHostNameEnabled(bool value)
60{
61 if (value == sendHostNameEnabled())
62 {
63 return value;
64 }
65
66 auto name = ConfigIntf::sendHostNameEnabled(value);
William A. Kennington IIIbd649af2021-10-08 17:55:13 -070067
William A. Kennington III9ede1b72022-11-21 01:59:28 -080068 manager.get().writeToConfigurationFile();
69 manager.get().reloadConfigs();
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -060070
71 return name;
72}
73
Ratan Gupta935bc332017-07-11 17:47:14 +053074bool Configuration::hostNameEnabled(bool value)
75{
76 if (value == hostNameEnabled())
77 {
78 return value;
79 }
80
81 auto name = ConfigIntf::hostNameEnabled(value);
William A. Kennington III9ede1b72022-11-21 01:59:28 -080082 manager.get().writeToConfigurationFile();
83 manager.get().reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +053084
85 return name;
86}
87
Patrick Williams6aef7692021-05-01 06:39:41 -050088bool Configuration::ntpEnabled(bool value)
Ratan Gupta935bc332017-07-11 17:47:14 +053089{
Patrick Williams6aef7692021-05-01 06:39:41 -050090 if (value == ntpEnabled())
Ratan Gupta935bc332017-07-11 17:47:14 +053091 {
92 return value;
93 }
94
Patrick Williams6aef7692021-05-01 06:39:41 -050095 auto ntp = ConfigIntf::ntpEnabled(value);
William A. Kennington III9ede1b72022-11-21 01:59:28 -080096 manager.get().writeToConfigurationFile();
97 manager.get().reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +053098
99 return ntp;
100}
101
Patrick Williams6aef7692021-05-01 06:39:41 -0500102bool Configuration::dnsEnabled(bool value)
Ratan Gupta935bc332017-07-11 17:47:14 +0530103{
Patrick Williams6aef7692021-05-01 06:39:41 -0500104 if (value == dnsEnabled())
Ratan Gupta935bc332017-07-11 17:47:14 +0530105 {
106 return value;
107 }
108
Patrick Williams6aef7692021-05-01 06:39:41 -0500109 auto dns = ConfigIntf::dnsEnabled(value);
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800110 manager.get().writeToConfigurationFile();
111 manager.get().reloadConfigs();
Ratan Gupta935bc332017-07-11 17:47:14 +0530112
113 return dns;
114}
115
Gunnar Mills57d9c502018-09-14 14:42:34 -0500116} // namespace dhcp
117} // namespace network
118} // namespace phosphor