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