blob: 25342ead088083f24767e4ca1517e672d0344e18 [file] [log] [blame]
Ratan Gupta63476192018-04-19 16:55:32 +05301#include "snmp_util.hpp"
2#include "xyz/openbmc_project/Common/error.hpp"
3
Ratan Gupta213517b2018-04-28 13:41:09 +05304#include <arpa/inet.h>
5
Ratan Gupta63476192018-04-19 16:55:32 +05306#include <phosphor-logging/elog-errors.hpp>
7#include <phosphor-logging/log.hpp>
8
9#include <netdb.h>
10#include <arpa/inet.h>
11#include <string>
12
13namespace phosphor
14{
15
16using namespace phosphor::logging;
17using namespace sdbusplus::xyz::openbmc_project::Common::Error;
18
19ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus,
20 const std::string& service,
21 const std::string& objPath)
22{
23 ObjectValueTree interfaces;
24
25 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
26 "org.freedesktop.DBus.ObjectManager",
27 "GetManagedObjects");
28
29 auto reply = bus.call(method);
30
31 if (reply.is_method_error())
32 {
33 log<level::ERR>("Failed to get managed objects",
34 entry("PATH=%s", objPath.c_str()));
35 elog<InternalFailure>();
36 }
37
38 reply.read(interfaces);
39 return interfaces;
40}
41
42namespace network
43{
44
45std::string resolveAddress(const std::string& address)
46{
47 addrinfo hints{0};
48 addrinfo* addr = nullptr;
49
50 hints.ai_family = AF_UNSPEC;
51 hints.ai_socktype = SOCK_STREAM;
52 hints.ai_flags |= AI_CANONNAME;
53
54 auto result = getaddrinfo(address.c_str(), NULL, &hints, &addr);
55 if (result)
56 {
57 log<level::ERR>("getaddrinfo failed",
58 entry("ADDRESS=%s", address.c_str()));
59 elog<InternalFailure>();
60 }
61
62 AddrPtr addrPtr{addr};
63 addr = nullptr;
64
65 char ipaddress[INET6_ADDRSTRLEN]{0};
66 result = getnameinfo(addrPtr->ai_addr, addrPtr->ai_addrlen, ipaddress,
67 sizeof(ipaddress), NULL, 0, NI_NUMERICHOST);
68 if (result)
69 {
70 log<level::ERR>("getnameinfo failed",
71 entry("ADDRESS=%s", address.c_str()));
72 elog<InternalFailure>();
73 }
74
75 return ipaddress;
76}
77
78namespace snmp
79{
80
81static constexpr auto busName = "xyz.openbmc_project.Network.SNMP";
82static constexpr auto root = "/xyz/openbmc_project/network/snmp/manager";
83static constexpr auto clientIntf = "xyz.openbmc_project.Network.Client";
84
85/** @brief Gets the sdbus object for this process.
86 * @return the bus object.
87 */
88static auto& getBus()
89{
90 static auto bus = sdbusplus::bus::new_default();
91 return bus;
92}
93
94std::vector<std::string> getManagers()
95{
96 std::vector<std::string> managers;
97 auto& bus = getBus();
98 auto objTree = phosphor::getManagedObjects(bus, busName, root);
99 for (const auto& objIter : objTree)
100 {
101 try
102 {
103 auto& intfMap = objIter.second;
104 auto& snmpClientProps = intfMap.at(clientIntf);
105 auto& address = snmpClientProps.at("Address").get<std::string>();
106 auto& port = snmpClientProps.at("Port").get<uint16_t>();
107 auto ipaddress = phosphor::network::resolveAddress(address);
108 auto mgr = std::move(ipaddress);
109 if (port > 0)
110 {
111 mgr += ":";
112 mgr += std::to_string(port);
113 }
114 managers.push_back(mgr);
115 }
116 catch (const std::exception& e)
117 {
118 log<level::ERR>(e.what());
119 }
120 }
121 return managers;
122}
123
124} // namespace snmp
125} // namespace network
126} // namespace phosphor