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