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 | |
| 9 | #include <filesystem> |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 11 | #include <phosphor-logging/log.hpp> |
| 12 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace network |
| 17 | { |
| 18 | namespace dhcp |
| 19 | { |
| 20 | |
Nagaraju Goruganti | 210420a | 2018-03-07 09:22:28 -0600 | [diff] [blame] | 21 | using namespace phosphor::network; |
| 22 | using namespace phosphor::logging; |
| 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, |
| 26 | stdplus::const_zstring objPath, Manager& parent) : |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 27 | Iface(bus, objPath.c_str(), Iface::action::defer_emit), |
| 28 | bus(bus), manager(parent) |
| 29 | { |
| 30 | config::Parser conf; |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 31 | 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 III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 35 | { |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 36 | struct stat st = {}; |
| 37 | stat(dirent.path().native().c_str(), &st); |
| 38 | if (st.st_mtime > newest_time) |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 39 | { |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 40 | newest_file = dirent; |
| 41 | newest_time = st.st_mtime; |
William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
William A. Kennington III | 180498c | 2022-11-14 18:11:45 -0800 | [diff] [blame] | 44 | 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 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 | |
Nagaraju Goruganti | e8fca1d | 2018-02-05 20:32:45 -0600 | [diff] [blame] | 68 | manager.writeToConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 69 | manager.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); |
| 82 | manager.writeToConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 83 | manager.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); |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 96 | manager.writeToConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 97 | manager.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); |
Ratan Gupta | 935bc33 | 2017-07-11 17:47:14 +0530 | [diff] [blame] | 110 | manager.writeToConfigurationFile(); |
William A. Kennington III | 5b17938 | 2022-11-15 15:23:26 -0800 | [diff] [blame] | 111 | manager.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 |