1 #include "config.h" 2 #include "snmp_conf_manager.hpp" 3 #include "snmp_util.hpp" 4 #include "xyz/openbmc_project/Common/error.hpp" 5 6 #include <phosphor-logging/elog-errors.hpp> 7 #include <phosphor-logging/log.hpp> 8 9 #include <experimental/filesystem> 10 11 #include <arpa/inet.h> 12 13 namespace phosphor 14 { 15 namespace network 16 { 17 namespace snmp 18 { 19 20 using namespace phosphor::logging; 21 using namespace sdbusplus::xyz::openbmc_project::Common::Error; 22 using Argument = xyz::openbmc_project::Common::InvalidArgument; 23 24 ConfManager::ConfManager(sdbusplus::bus::bus& bus, const char* objPath) : 25 details::CreateIface(bus, objPath, true), bus(bus), objectPath(objPath) 26 { 27 } 28 29 void ConfManager::client(std::string address, uint16_t port) 30 { 31 auto clientEntry = this->clients.find(address); 32 if (clientEntry != this->clients.end()) 33 { 34 // address is already there 35 return; 36 } 37 try 38 { 39 // just to check whether given address is valid or not. 40 resolveAddress(address); 41 } 42 catch (InternalFailure& e) 43 { 44 log<level::ERR>("Not a valid address"), 45 entry("ADDRESS=%s", address.c_str()); 46 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"), 47 Argument::ARGUMENT_VALUE(address.c_str())); 48 } 49 // create the D-Bus object 50 std::experimental::filesystem::path objPath; 51 objPath /= objectPath; 52 objPath /= generateId(address, port); 53 54 this->clients.emplace( 55 address, std::make_unique<phosphor::network::snmp::Client>( 56 bus, objPath.string().c_str(), *this, address, port)); 57 } 58 59 std::string ConfManager::generateId(const std::string& address, uint16_t port) 60 { 61 std::stringstream hexId; 62 std::string hashString = address; 63 hashString += std::to_string(port); 64 65 // Only want 8 hex digits. 66 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); 67 return hexId.str(); 68 } 69 70 void ConfManager::deleteSNMPClient(const std::string& address) 71 { 72 auto it = clients.find(address); 73 if (it == clients.end()) 74 { 75 log<level::ERR>("Unable to delete the snmp client.", 76 entry("ADDRESS=%s", address.c_str())); 77 return; 78 } 79 this->clients.erase(it); 80 } 81 82 } // namespace snmp 83 } // namespace network 84 } // namespace phosphor 85