blob: 41c364bf8fa42ea83ef413760d84ca4cbef5e77b [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
9#include <filesystem>
Nagaraju Goruganti210420a2018-03-07 09:22:28 -060010#include <phosphor-logging/elog-errors.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070011#include <phosphor-logging/log.hpp>
12#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta935bc332017-07-11 17:47:14 +053013
14namespace phosphor
15{
16namespace network
17{
18namespace dhcp
19{
20
Nagaraju Goruganti210420a2018-03-07 09:22:28 -060021using namespace phosphor::network;
22using namespace phosphor::logging;
23using 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,
26 stdplus::const_zstring objPath, Manager& parent) :
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070027 Iface(bus, objPath.c_str(), Iface::action::defer_emit),
28 bus(bus), manager(parent)
29{
30 config::Parser conf;
William A. Kennington III180498c2022-11-14 18:11:45 -080031 std::filesystem::directory_entry newest_file;
32 time_t newest_time = 0;
33 for (const auto& dirent :
34 std::filesystem::directory_iterator(manager.getConfDir()))
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070035 {
William A. Kennington III180498c2022-11-14 18:11:45 -080036 struct stat st = {};
37 stat(dirent.path().native().c_str(), &st);
38 if (st.st_mtime > newest_time)
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070039 {
William A. Kennington III180498c2022-11-14 18:11:45 -080040 newest_file = dirent;
41 newest_time = st.st_mtime;
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070042 }
43 }
William A. Kennington III180498c2022-11-14 18:11:45 -080044 if (newest_file != std::filesystem::directory_entry{})
45 {
46 log<level::INFO>(fmt::format("Using DHCP options from {}",
47 newest_file.path().native())
48 .c_str());
49 conf.setFile(newest_file.path());
50 }
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -070051
52 ConfigIntf::dnsEnabled(getDHCPProp(conf, "UseDNS"));
53 ConfigIntf::ntpEnabled(getDHCPProp(conf, "UseNTP"));
54 ConfigIntf::hostNameEnabled(getDHCPProp(conf, "UseHostname"));
55 ConfigIntf::sendHostNameEnabled(getDHCPProp(conf, "SendHostname"));
56 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
Nagaraju Gorugantie8fca1d2018-02-05 20:32:45 -060068 manager.writeToConfigurationFile();
William A. Kennington IIIffab00d2022-11-07 16:58:36 -080069 manager.reloadConfigsNoRefresh();
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);
82 manager.writeToConfigurationFile();
William A. Kennington IIIffab00d2022-11-07 16:58:36 -080083 manager.reloadConfigsNoRefresh();
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);
Ratan Gupta935bc332017-07-11 17:47:14 +053096 manager.writeToConfigurationFile();
William A. Kennington IIIffab00d2022-11-07 16:58:36 -080097 manager.reloadConfigsNoRefresh();
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);
Ratan Gupta935bc332017-07-11 17:47:14 +0530110 manager.writeToConfigurationFile();
William A. Kennington IIIffab00d2022-11-07 16:58:36 -0800111 manager.reloadConfigsNoRefresh();
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