blob: 9516337a8071f171f2dba17b5a94fd5ba17651d4 [file] [log] [blame]
Ratan Gupta63476192018-04-19 16:55:32 +05301#include "snmp_util.hpp"
Patrick Williams1334b7b2021-02-22 17:15:12 -06002
Ratan Gupta63476192018-04-19 16:55:32 +05303#include "xyz/openbmc_project/Common/error.hpp"
4
Ratan Gupta213517b2018-04-28 13:41:09 +05305#include <arpa/inet.h>
Patrick Williams1334b7b2021-02-22 17:15:12 -06006#include <netdb.h>
Ratan Gupta213517b2018-04-28 13:41:09 +05307
Ratan Gupta63476192018-04-19 16:55:32 +05308#include <phosphor-logging/elog-errors.hpp>
George Liu4caedfb2022-05-10 16:26:53 +08009#include <phosphor-logging/lg2.hpp>
Ratan Gupta63476192018-04-19 16:55:32 +053010
Ratan Gupta63476192018-04-19 16:55:32 +053011#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
George Liue65b0a72022-05-10 16:47:27 +080029 try
30 {
31 auto reply = bus.call(method);
32 reply.read(interfaces);
33 }
34 catch (const sdbusplus::exception::exception& e)
Ratan Gupta63476192018-04-19 16:55:32 +053035 {
George Liu4caedfb2022-05-10 16:26:53 +080036 lg2::error("Failed to get managed objects: {PATH}", "PATH", objPath);
Ratan Gupta63476192018-04-19 16:55:32 +053037 elog<InternalFailure>();
38 }
39
Ratan Gupta63476192018-04-19 16:55:32 +053040 return interfaces;
41}
42
43namespace network
44{
45
46std::string resolveAddress(const std::string& address)
47{
Ratan Gupta34d129a2021-12-04 21:04:51 +053048 addrinfo hints{};
Ratan Gupta63476192018-04-19 16:55:32 +053049 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 {
George Liu4caedfb2022-05-10 16:26:53 +080058 lg2::error("getaddrinfo failed {ADDRESS}: {RC}", "ADDRESS", address,
59 "RC", result);
Ratan Gupta63476192018-04-19 16:55:32 +053060 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 {
George Liu4caedfb2022-05-10 16:26:53 +080071 lg2::error("getnameinfo failed {ADDRESS}: {RC}", "ADDRESS", address,
72 "RC", result);
Ratan Gupta63476192018-04-19 16:55:32 +053073 elog<InternalFailure>();
74 }
75
Chicago Duan7350c772021-12-03 16:54:17 +080076 unsigned char buf[sizeof(struct in6_addr)];
77 int isValid = inet_pton(AF_INET, ipaddress, buf);
78 if (isValid < 0)
79 {
George Liu4caedfb2022-05-10 16:26:53 +080080 lg2::error("Invalid address {ADDRESS}: {RC}", "ADDRESS", address, "RC",
81 isValid);
Chicago Duan7350c772021-12-03 16:54:17 +080082 elog<InternalFailure>();
83 }
84 if (isValid == 0)
85 {
86 int isValid6 = inet_pton(AF_INET6, ipaddress, buf);
87 if (isValid6 < 1)
88 {
George Liu4caedfb2022-05-10 16:26:53 +080089 lg2::error("Invalid address {ADDRESS}: {RC}", "ADDRESS", address,
90 "RC", isValid);
Chicago Duan7350c772021-12-03 16:54:17 +080091 elog<InternalFailure>();
92 }
93 }
94
Ratan Gupta63476192018-04-19 16:55:32 +053095 return ipaddress;
96}
97
98namespace snmp
99{
100
101static constexpr auto busName = "xyz.openbmc_project.Network.SNMP";
102static constexpr auto root = "/xyz/openbmc_project/network/snmp/manager";
103static constexpr auto clientIntf = "xyz.openbmc_project.Network.Client";
104
105/** @brief Gets the sdbus object for this process.
106 * @return the bus object.
107 */
108static auto& getBus()
109{
110 static auto bus = sdbusplus::bus::new_default();
111 return bus;
112}
113
114std::vector<std::string> getManagers()
115{
116 std::vector<std::string> managers;
117 auto& bus = getBus();
118 auto objTree = phosphor::getManagedObjects(bus, busName, root);
119 for (const auto& objIter : objTree)
120 {
121 try
122 {
123 auto& intfMap = objIter.second;
124 auto& snmpClientProps = intfMap.at(clientIntf);
William A. Kennington IIIbbee5d02018-11-06 15:52:22 -0800125 auto& address =
Patrick Williams7d4bd222020-05-13 11:05:10 -0500126 std::get<std::string>(snmpClientProps.at("Address"));
127 auto& port = std::get<uint16_t>(snmpClientProps.at("Port"));
Ratan Gupta63476192018-04-19 16:55:32 +0530128 auto ipaddress = phosphor::network::resolveAddress(address);
129 auto mgr = std::move(ipaddress);
130 if (port > 0)
131 {
132 mgr += ":";
133 mgr += std::to_string(port);
134 }
135 managers.push_back(mgr);
136 }
137 catch (const std::exception& e)
138 {
George Liu4caedfb2022-05-10 16:26:53 +0800139 lg2::error("Invalid address: {ERROR}", "ERROR", e);
Ratan Gupta63476192018-04-19 16:55:32 +0530140 }
141 }
142 return managers;
143}
144
145} // namespace snmp
146} // namespace network
147} // namespace phosphor