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