Validate the client address
Address could be hostname or IPaddress,Address validation
is being done by calling the gethostbyname system function.
Change-Id: I7d7eecd164c471c80af0b440a4b541badd2f966f
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/snmp_conf_manager.cpp b/snmp_conf_manager.cpp
index 30d0815..3c7eaa6 100644
--- a/snmp_conf_manager.cpp
+++ b/snmp_conf_manager.cpp
@@ -1,9 +1,15 @@
#include "config.h"
#include "snmp_conf_manager.hpp"
+#include "snmp_util.hpp"
+#include "xyz/openbmc_project/Common/error.hpp"
+
+#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/log.hpp>
#include <experimental/filesystem>
+#include <arpa/inet.h>
+
namespace phosphor
{
namespace network
@@ -12,6 +18,8 @@
{
using namespace phosphor::logging;
+using namespace sdbusplus::xyz::openbmc_project::Common::Error;
+using Argument = xyz::openbmc_project::Common::InvalidArgument;
ConfManager::ConfManager(sdbusplus::bus::bus& bus, const char* objPath) :
details::CreateIface(bus, objPath, true), bus(bus), objectPath(objPath)
@@ -21,16 +29,31 @@
void ConfManager::client(std::string address, uint16_t port)
{
auto clientEntry = this->clients.find(address);
- if (clientEntry == this->clients.end())
+ if (clientEntry != this->clients.end())
{
- std::experimental::filesystem::path objPath;
- objPath /= objectPath;
- objPath /= generateId(address, port);
-
- this->clients.emplace(
- address, std::make_unique<phosphor::network::snmp::Client>(
- bus, objPath.string().c_str(), *this, address, port));
+ // address is already there
+ return;
}
+ try
+ {
+ // just to check whether given address is valid or not.
+ resolveAddress(address);
+ }
+ catch (InternalFailure& e)
+ {
+ log<level::ERR>("Not a valid address"),
+ entry("ADDRESS=%s", address.c_str());
+ elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"),
+ Argument::ARGUMENT_VALUE(address.c_str()));
+ }
+ // create the D-Bus object
+ std::experimental::filesystem::path objPath;
+ objPath /= objectPath;
+ objPath /= generateId(address, port);
+
+ this->clients.emplace(
+ address, std::make_unique<phosphor::network::snmp::Client>(
+ bus, objPath.string().c_str(), *this, address, port));
}
std::string ConfManager::generateId(const std::string& address, uint16_t port)