Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 1 | #include "dhcp_configuration.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 2 | |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 3 | #include "config_parser.hpp" |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 4 | #include "network_manager.hpp" |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 5 | #include "util.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 6 | |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 7 | #include <sys/stat.h> |
| 8 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> |
Jagpal Singh Gill | a2947b4 | 2023-04-17 21:10:14 -0700 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 12 | |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 13 | #include <filesystem> |
| 14 | |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 15 | namespace phosphor |
| 16 | { |
| 17 | namespace network |
| 18 | { |
| 19 | namespace dhcp |
| 20 | { |
| 21 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 22 | using namespace phosphor::network; |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 23 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 24 | |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 25 | Configuration::Configuration(sdbusplus::bus_t& bus, |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 26 | stdplus::const_zstring objPath, |
| 27 | stdplus::PinnedRef<Manager> parent) : |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 28 | Iface(bus, objPath.c_str(), Iface::action::defer_emit), |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 29 | manager(parent) |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 30 | { |
| 31 | config::Parser conf; |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 32 | std::filesystem::directory_entry newest_file; |
| 33 | time_t newest_time = 0; |
| 34 | for (const auto& dirent : |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 35 | std::filesystem::directory_iterator(manager.get().getConfDir())) |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 36 | { |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 37 | struct stat st = {}; |
| 38 | stat(dirent.path().native().c_str(), &st); |
| 39 | if (st.st_mtime > newest_time) |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 40 | { |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 41 | newest_file = dirent; |
| 42 | newest_time = st.st_mtime; |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 45 | if (newest_file != std::filesystem::directory_entry{}) |
| 46 | { |
William A. Kennington III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 47 | lg2::info("Using DHCP options from {CFG_FILE}", "CFG_FILE", |
Jagpal Singh Gill | a2947b4 | 2023-04-17 21:10:14 -0700 | [diff] [blame] | 48 | newest_file.path().native()); |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 49 | conf.setFile(newest_file.path()); |
| 50 | } |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 51 | |
William A. Kennington III | d99e6db | 2022-11-15 20:39:45 -0800 | [diff] [blame] | 52 | 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 III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 56 | emit_object_added(); |
| 57 | } |
| 58 | |
Nagaraju Goruganti | e8fca1d | 2018-02-05 20:32:45 -0600 | [diff] [blame] | 59 | bool Configuration::sendHostNameEnabled(bool value) |
| 60 | { |
| 61 | if (value == sendHostNameEnabled()) |
| 62 | { |
| 63 | return value; |
| 64 | } |
| 65 | |
| 66 | auto name = ConfigIntf::sendHostNameEnabled(value); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 67 | |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 68 | manager.get().writeToConfigurationFile(); |
| 69 | manager.get().reloadConfigs(); |
Nagaraju Goruganti | e8fca1d | 2018-02-05 20:32:45 -0600 | [diff] [blame] | 70 | |
| 71 | return name; |
| 72 | } |
| 73 | |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 74 | bool Configuration::hostNameEnabled(bool value) |
| 75 | { |
| 76 | if (value == hostNameEnabled()) |
| 77 | { |
| 78 | return value; |
| 79 | } |
| 80 | |
| 81 | auto name = ConfigIntf::hostNameEnabled(value); |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 82 | manager.get().writeToConfigurationFile(); |
| 83 | manager.get().reloadConfigs(); |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 84 | |
| 85 | return name; |
| 86 | } |
| 87 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 88 | bool Configuration::ntpEnabled(bool value) |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 89 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 90 | if (value == ntpEnabled()) |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 91 | { |
| 92 | return value; |
| 93 | } |
| 94 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 95 | auto ntp = ConfigIntf::ntpEnabled(value); |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 96 | manager.get().writeToConfigurationFile(); |
| 97 | manager.get().reloadConfigs(); |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 98 | |
| 99 | return ntp; |
| 100 | } |
| 101 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 102 | bool Configuration::dnsEnabled(bool value) |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 103 | { |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 104 | if (value == dnsEnabled()) |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 105 | { |
| 106 | return value; |
| 107 | } |
| 108 | |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 109 | auto dns = ConfigIntf::dnsEnabled(value); |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 110 | manager.get().writeToConfigurationFile(); |
| 111 | manager.get().reloadConfigs(); |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 112 | |
| 113 | return dns; |
| 114 | } |
| 115 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 116 | } // namespace dhcp |
| 117 | } // namespace network |
| 118 | } // namespace phosphor |