summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRatan Gupta <ratagupt@in.ibm.com>2017-05-23 13:20:44 +0530
committerPatrick Williams <patrick@stwcx.xyz>2017-05-23 11:58:44 +0000
commit65e5abefd6b73c8f383a335291c32111ae787be0 (patch)
treee9ffc8d1c840f22d7e8a7b3783f23fb7e24b189e
parent738a67fe79e3460541592cc64b139f0d2f5bd4d4 (diff)
downloadphosphor-networkd-65e5abefd6b73c8f383a335291c32111ae787be0.tar.gz
phosphor-networkd-65e5abefd6b73c8f383a335291c32111ae787be0.zip
Generate the ip address object path with the use of hash
ipaddress object path have the id for unique identification of ipaddres and the id would be hash of ipaddress,prefix and gateway. This was needed to make sure that there is no duplication of address object path. Change-Id: If6830d6e186e3271467ce0084c1dbf3c4995f1dd Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
-rw-r--r--ethernet_interface.cpp49
-rw-r--r--ethernet_interface.hpp23
2 files changed, 44 insertions, 28 deletions
diff --git a/ethernet_interface.cpp b/ethernet_interface.cpp
index 20b3e31..75cb761 100644
--- a/ethernet_interface.cpp
+++ b/ethernet_interface.cpp
@@ -15,6 +15,7 @@
#include <string>
#include <algorithm>
+#include <sstream>
#include <experimental/filesystem>
namespace phosphor
@@ -49,7 +50,10 @@ EthernetInterface::EthernetInterface(sdbusplus::bus::bus& bus,
addressType = IP::Protocol::IPv6;
}
- std::string ipAddressObjectPath = getAddressObjectPath(addressType);
+ std::string ipAddressObjectPath = generateObjectPath(addressType,
+ addr.ipaddress,
+ addr.prefix,
+ gateway);
this->addrs.emplace(
std::make_pair(
addr.ipaddress,
@@ -71,9 +75,10 @@ void EthernetInterface::iP(IP::Protocol protType,
uint8_t prefixLength,
std::string gateway)
{
-
- IP::Protocol protocolType = protType;
- std::string objectPath = getAddressObjectPath(protocolType);
+ std::string objectPath = generateObjectPath(protType,
+ ipaddress,
+ prefixLength,
+ gateway);
this->addrs.emplace(
std::make_pair(ipaddress,
@@ -81,7 +86,7 @@ void EthernetInterface::iP(IP::Protocol protType,
bus,
objectPath.c_str(),
*this,
- protocolType,
+ protType,
ipaddress,
prefixLength,
gateway)));
@@ -194,20 +199,19 @@ std::string EthernetInterface::getMACAddress() const
return macAddress;
}
-size_t EthernetInterface::getAddressCount(IP::Protocol addressType) const
+std::string EthernetInterface::generateId(const std::string& ipaddress,
+ uint8_t prefixLength,
+ const std::string& gateway)
{
- size_t count = 0;
-
- std::for_each(addrs.cbegin(), addrs.cend(),
- [&count,addressType](const auto & addr)
- {
- if (addr.second->type() == addressType)
- {
- count += 1;
- }
- });
-
- return count;
+ std::stringstream hexId;
+ std::string hashString = ipaddress;
+ hashString += std::to_string(prefixLength);
+ hashString += gateway;
+
+ // Only want 8 hex digits.
+ hexId << std::hex << ((std::hash<std::string> {}(
+ hashString)) & 0xFFFFFFFF);
+ return hexId.str();
}
void EthernetInterface::deleteObject(const std::string& ipaddress)
@@ -215,10 +219,11 @@ void EthernetInterface::deleteObject(const std::string& ipaddress)
this->addrs.erase(addrs.find(ipaddress));
}
-std::string EthernetInterface::getAddressObjectPath(IP::Protocol
- addressType) const
+std::string EthernetInterface::generateObjectPath(IP::Protocol addressType,
+ const std::string& ipaddress,
+ uint8_t prefixLength,
+ const std::string& gateway) const
{
-
std::string type = convertForMessage(addressType);
type = type.substr(type.rfind('.')+1);
std::transform(type.begin(), type.end(), type.begin(), ::tolower);
@@ -227,7 +232,7 @@ std::string EthernetInterface::getAddressObjectPath(IP::Protocol
objectPath /= std::string(OBJ_NETWORK);
objectPath /= interfaceName();
objectPath /= type;
- objectPath /= std::to_string(getAddressCount(addressType));
+ objectPath /= generateId(ipaddress,prefixLength,gateway);
return objectPath.string();
}
diff --git a/ethernet_interface.hpp b/ethernet_interface.hpp
index ab69a78..f9bd895 100644
--- a/ethernet_interface.hpp
+++ b/ethernet_interface.hpp
@@ -90,18 +90,29 @@ class EthernetInterface : public Ifaces
/** @brief construct the ip address dbus object path.
* @param[in] addressType - Type of ip address.
+ * @param[in] ipaddress - IP address.
+ * @param[in] prefixLength - Length of prefix.
+ * @param[in] gateway - Gateway addess.
+
* @return path of the address object.
*/
- std::string getAddressObjectPath(IP::Protocol addressType) const;
+ std::string generateObjectPath(IP::Protocol addressType,
+ const std::string& ipaddress,
+ uint8_t prefixLength,
+ const std::string& gateway) const;
- /** @brief get the ipadress count for a specific type on this interface.
- * @param[in] addressType - Type of ip address.
- * @return count of ipaddreses for the incoming type.
+ /** @brief generates the id by doing hash of ipaddress,
+ * prefixlength and the gateway.
+ * @param[in] ipaddress - IP address.
+ * @param[in] prefixLength - Length of prefix.
+ * @param[in] gateway - Gateway addess.
+ * @return hash string.
*/
- size_t getAddressCount(IP::Protocol addressType) const;
-
+ static std::string generateId(const std::string& ipaddress,
+ uint8_t prefixLength,
+ const std::string& gateway);
/** @brief Persistent sdbusplus DBus bus connection. */
sdbusplus::bus::bus& bus;
OpenPOWER on IntegriCloud