Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 1 | #include "snmp_util.hpp" |
| 2 | #include "xyz/openbmc_project/Common/error.hpp" |
| 3 | |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 4 | #include <arpa/inet.h> |
| 5 | |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 6 | #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 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | |
William A. Kennington III | bbee5d0 | 2018-11-06 15:52:22 -0800 | [diff] [blame] | 16 | namespace variant_ns = sdbusplus::message::variant_ns; |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 17 | using namespace phosphor::logging; |
| 18 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 19 | |
| 20 | ObjectValueTree 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 | |
| 43 | namespace network |
| 44 | { |
| 45 | |
| 46 | std::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 | |
| 79 | namespace snmp |
| 80 | { |
| 81 | |
| 82 | static constexpr auto busName = "xyz.openbmc_project.Network.SNMP"; |
| 83 | static constexpr auto root = "/xyz/openbmc_project/network/snmp/manager"; |
| 84 | static constexpr auto clientIntf = "xyz.openbmc_project.Network.Client"; |
| 85 | |
| 86 | /** @brief Gets the sdbus object for this process. |
| 87 | * @return the bus object. |
| 88 | */ |
| 89 | static auto& getBus() |
| 90 | { |
| 91 | static auto bus = sdbusplus::bus::new_default(); |
| 92 | return bus; |
| 93 | } |
| 94 | |
| 95 | std::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 III | bbee5d0 | 2018-11-06 15:52:22 -0800 | [diff] [blame] | 106 | auto& address = |
| 107 | variant_ns::get<std::string>(snmpClientProps.at("Address")); |
| 108 | auto& port = variant_ns::get<uint16_t>(snmpClientProps.at("Port")); |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 109 | 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 |