blob: 562c88c63e303533f976a766782f850203b348fb [file] [log] [blame]
Ratan Gupta82e1ef92017-06-15 08:39:15 +05301#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
10namespace phosphor
11{
12namespace network
13{
14
15// systemd service to kick start a target.
16constexpr auto HOSTNAMED_SERVICE = "org.freedesktop.hostname1";
17constexpr auto HOSTNAMED_SERVICE_PATH = "/org/freedesktop/hostname1";
18constexpr auto HOSTNAMED_INTERFACE = "org.freedesktop.hostname1";
19constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
20constexpr auto METHOD_GET = "Get";
21constexpr auto METHOD_SET = "SetStaticHostname";
22
23using namespace phosphor::logging;
24using namespace sdbusplus::xyz::openbmc_project::Common::Error;
25
26using SystemConfigIntf =
27 sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration;
28
29SystemConfiguration::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
45std::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
69std::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
96std::string SystemConfiguration::defaultGateway(std::string gateway)
97{
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -050098 auto gw = SystemConfigIntf::defaultGateway();
99 if (gw == gateway)
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530100 {
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500101 return gw;
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530102 }
Nagaraju Gorugantie44acde2017-11-02 05:22:21 -0500103
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 Gupta82e1ef92017-06-15 08:39:15 +0530111 manager.writeToConfigurationFile();
112 return gw;
113}
114
115}// namespace network
116}// namespace phosphor