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