blob: 9f235d8461258fdcff2e71e21e67aedae4f12198 [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
29 auto reply = bus.call(method);
30
31 if (reply.is_method_error())
32 {
George Liu4caedfb2022-05-10 16:26:53 +080033 lg2::error("Failed to get managed objects: {PATH}", "PATH", objPath);
Ratan Gupta63476192018-04-19 16:55:32 +053034 elog<InternalFailure>();
35 }
36
37 reply.read(interfaces);
38 return interfaces;
39}
40
41namespace network
42{
43
44std::string resolveAddress(const std::string& address)
45{
Ratan Gupta34d129a2021-12-04 21:04:51 +053046 addrinfo hints{};
Ratan Gupta63476192018-04-19 16:55:32 +053047 addrinfo* addr = nullptr;
48
49 hints.ai_family = AF_UNSPEC;
50 hints.ai_socktype = SOCK_STREAM;
51 hints.ai_flags |= AI_CANONNAME;
52
53 auto result = getaddrinfo(address.c_str(), NULL, &hints, &addr);
54 if (result)
55 {
George Liu4caedfb2022-05-10 16:26:53 +080056 lg2::error("getaddrinfo failed {ADDRESS}: {RC}", "ADDRESS", address,
57 "RC", result);
Ratan Gupta63476192018-04-19 16:55:32 +053058 elog<InternalFailure>();
59 }
60
61 AddrPtr addrPtr{addr};
62 addr = nullptr;
63
64 char ipaddress[INET6_ADDRSTRLEN]{0};
65 result = getnameinfo(addrPtr->ai_addr, addrPtr->ai_addrlen, ipaddress,
66 sizeof(ipaddress), NULL, 0, NI_NUMERICHOST);
67 if (result)
68 {
George Liu4caedfb2022-05-10 16:26:53 +080069 lg2::error("getnameinfo failed {ADDRESS}: {RC}", "ADDRESS", address,
70 "RC", result);
Ratan Gupta63476192018-04-19 16:55:32 +053071 elog<InternalFailure>();
72 }
73
Chicago Duan7350c772021-12-03 16:54:17 +080074 unsigned char buf[sizeof(struct in6_addr)];
75 int isValid = inet_pton(AF_INET, ipaddress, buf);
76 if (isValid < 0)
77 {
George Liu4caedfb2022-05-10 16:26:53 +080078 lg2::error("Invalid address {ADDRESS}: {RC}", "ADDRESS", address, "RC",
79 isValid);
Chicago Duan7350c772021-12-03 16:54:17 +080080 elog<InternalFailure>();
81 }
82 if (isValid == 0)
83 {
84 int isValid6 = inet_pton(AF_INET6, ipaddress, buf);
85 if (isValid6 < 1)
86 {
George Liu4caedfb2022-05-10 16:26:53 +080087 lg2::error("Invalid address {ADDRESS}: {RC}", "ADDRESS", address,
88 "RC", isValid);
Chicago Duan7350c772021-12-03 16:54:17 +080089 elog<InternalFailure>();
90 }
91 }
92
Ratan Gupta63476192018-04-19 16:55:32 +053093 return ipaddress;
94}
95
96namespace snmp
97{
98
99static constexpr auto busName = "xyz.openbmc_project.Network.SNMP";
100static constexpr auto root = "/xyz/openbmc_project/network/snmp/manager";
101static constexpr auto clientIntf = "xyz.openbmc_project.Network.Client";
102
103/** @brief Gets the sdbus object for this process.
104 * @return the bus object.
105 */
106static auto& getBus()
107{
108 static auto bus = sdbusplus::bus::new_default();
109 return bus;
110}
111
112std::vector<std::string> getManagers()
113{
114 std::vector<std::string> managers;
115 auto& bus = getBus();
116 auto objTree = phosphor::getManagedObjects(bus, busName, root);
117 for (const auto& objIter : objTree)
118 {
119 try
120 {
121 auto& intfMap = objIter.second;
122 auto& snmpClientProps = intfMap.at(clientIntf);
William A. Kennington IIIbbee5d02018-11-06 15:52:22 -0800123 auto& address =
Patrick Williams7d4bd222020-05-13 11:05:10 -0500124 std::get<std::string>(snmpClientProps.at("Address"));
125 auto& port = std::get<uint16_t>(snmpClientProps.at("Port"));
Ratan Gupta63476192018-04-19 16:55:32 +0530126 auto ipaddress = phosphor::network::resolveAddress(address);
127 auto mgr = std::move(ipaddress);
128 if (port > 0)
129 {
130 mgr += ":";
131 mgr += std::to_string(port);
132 }
133 managers.push_back(mgr);
134 }
135 catch (const std::exception& e)
136 {
George Liu4caedfb2022-05-10 16:26:53 +0800137 lg2::error("Invalid address: {ERROR}", "ERROR", e);
Ratan Gupta63476192018-04-19 16:55:32 +0530138 }
139 }
140 return managers;
141}
142
143} // namespace snmp
144} // namespace network
145} // namespace phosphor