Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 1 | #include "system_configuration.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 2 | |
| 3 | #include "config.h" |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 4 | #include "network_manager.hpp" |
| 5 | #include "routing_table.hpp" |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 6 | |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 7 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
| 9 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 10 | |
| 11 | namespace phosphor |
| 12 | { |
| 13 | namespace network |
| 14 | { |
| 15 | |
| 16 | // systemd service to kick start a target. |
| 17 | constexpr auto HOSTNAMED_SERVICE = "org.freedesktop.hostname1"; |
| 18 | constexpr auto HOSTNAMED_SERVICE_PATH = "/org/freedesktop/hostname1"; |
| 19 | constexpr auto HOSTNAMED_INTERFACE = "org.freedesktop.hostname1"; |
| 20 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 21 | constexpr auto METHOD_GET = "Get"; |
| 22 | constexpr auto METHOD_SET = "SetStaticHostname"; |
| 23 | |
| 24 | using namespace phosphor::logging; |
| 25 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Gunnar Mills | a362922 | 2018-06-19 16:32:04 -0500 | [diff] [blame^] | 26 | using InvalidArgumentMetadata = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 27 | |
| 28 | using SystemConfigIntf = |
| 29 | sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration; |
| 30 | |
| 31 | SystemConfiguration::SystemConfiguration(sdbusplus::bus::bus& bus, |
| 32 | const std::string& objPath, |
| 33 | Manager& parent) : |
| 34 | Iface(bus, objPath.c_str(), true), |
| 35 | bus(bus), |
| 36 | manager(parent) |
| 37 | { |
| 38 | auto name = getHostNameFromSystem(); |
| 39 | route::Table routingTable; |
| 40 | |
| 41 | SystemConfigIntf::hostName(name); |
| 42 | SystemConfigIntf::defaultGateway(routingTable.getDefaultGateway()); |
| 43 | |
| 44 | this->emit_object_added(); |
| 45 | } |
| 46 | |
| 47 | std::string SystemConfiguration::hostName(std::string name) |
| 48 | { |
| 49 | if (SystemConfigIntf::hostName() == name) |
| 50 | { |
| 51 | return name; |
| 52 | } |
| 53 | auto method = bus.new_method_call( |
| 54 | HOSTNAMED_SERVICE, |
| 55 | HOSTNAMED_SERVICE_PATH, |
| 56 | HOSTNAMED_INTERFACE, |
| 57 | METHOD_SET); |
| 58 | |
| 59 | method.append(name, true); |
| 60 | |
| 61 | if (!bus.call(method)) |
| 62 | { |
| 63 | log<level::ERR>("Failed to set the hostname"); |
| 64 | report<InternalFailure>(); |
| 65 | return SystemConfigIntf::hostName(); |
| 66 | } |
| 67 | |
| 68 | return SystemConfigIntf::hostName(name); |
| 69 | } |
| 70 | |
| 71 | std::string SystemConfiguration::getHostNameFromSystem() const |
| 72 | { |
| 73 | sdbusplus::message::variant<std::string> name; |
| 74 | auto method = bus.new_method_call( |
| 75 | HOSTNAMED_SERVICE, |
| 76 | HOSTNAMED_SERVICE_PATH, |
| 77 | PROPERTY_INTERFACE, |
| 78 | METHOD_GET); |
| 79 | |
| 80 | method.append(HOSTNAMED_INTERFACE, "Hostname"); |
| 81 | |
| 82 | auto reply = bus.call(method); |
| 83 | |
| 84 | if (reply) |
| 85 | { |
| 86 | reply.read(name); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | log<level::ERR>("Failed to get hostname"); |
| 91 | report<InternalFailure>(); |
| 92 | return ""; |
| 93 | } |
| 94 | return name.get<std::string>(); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | std::string SystemConfiguration::defaultGateway(std::string gateway) |
| 99 | { |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 100 | auto gw = SystemConfigIntf::defaultGateway(); |
| 101 | if (gw == gateway) |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 102 | { |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 103 | return gw; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 104 | } |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 105 | |
| 106 | if (!isValidIP(AF_INET, gateway)) |
| 107 | { |
| 108 | log<level::ERR>("Not a valid Gateway", |
| 109 | entry("GATEWAY=%s", gateway.c_str())); |
Gunnar Mills | a362922 | 2018-06-19 16:32:04 -0500 | [diff] [blame^] | 110 | elog<InvalidArgument>(InvalidArgumentMetadata::ARGUMENT_NAME("GATEWAY"), |
| 111 | InvalidArgumentMetadata::ARGUMENT_VALUE( |
| 112 | gateway.c_str())); |
Nagaraju Goruganti | e44acde | 2017-11-02 05:22:21 -0500 | [diff] [blame] | 113 | } |
| 114 | gw = SystemConfigIntf::defaultGateway(gateway); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 115 | manager.writeToConfigurationFile(); |
| 116 | return gw; |
| 117 | } |
| 118 | |
| 119 | }// namespace network |
| 120 | }// namespace phosphor |