diff options
-rw-r--r-- | src/CommonAPI/DBus/DBusAddressTranslator.cpp | 2 | ||||
-rw-r--r-- | src/CommonAPI/DBus/DBusUtils.h | 42 |
2 files changed, 40 insertions, 4 deletions
diff --git a/src/CommonAPI/DBus/DBusAddressTranslator.cpp b/src/CommonAPI/DBus/DBusAddressTranslator.cpp index 9357510..a1c836e 100644 --- a/src/CommonAPI/DBus/DBusAddressTranslator.cpp +++ b/src/CommonAPI/DBus/DBusAddressTranslator.cpp @@ -129,7 +129,7 @@ void DBusAddressTranslator::readConfigFile(std::ifstream& addressConfigFile) { } reset(serviceDetails); std::string newAddress = readLine.substr(1, readLineLength - 2); - if(checkValidCommonApiAddress(newAddress)) { + if (isValidCommonApiAddress(newAddress)) { currentlyParsedCommonApiAddress = std::move(newAddress); newAddressFound = commonApiAddressDetails.find(currentlyParsedCommonApiAddress) == commonApiAddressDetails.end(); } diff --git a/src/CommonAPI/DBus/DBusUtils.h b/src/CommonAPI/DBus/DBusUtils.h index 32953d5..be39f28 100644 --- a/src/CommonAPI/DBus/DBusUtils.h +++ b/src/CommonAPI/DBus/DBusUtils.h @@ -31,11 +31,47 @@ inline std::vector<std::string> split(const std::string& s, char delim) { return split(s, delim, elems); } -inline bool checkValidCommonApiAddress(const std::string& commonApiAddress) { - if (split(commonApiAddress, ':').size() != 3) { +inline bool containsOnlyAlphanumericCharacters(const std::string& toCheck) { + auto firstNonAlphanumericCharacterIt = std::find_if(toCheck.begin(), + toCheck.end(), + [](char c) { + return !std::isalnum(c); + }); + + return firstNonAlphanumericCharacterIt == toCheck.end(); +} + +inline bool isValidDomainName(const std::string& domainName) { + return containsOnlyAlphanumericCharacters(domainName); +} + +inline bool isValidServiceName(const std::string& serviceName) { + bool isValid = serviceName[0] != '.' && serviceName[serviceName.size() - 1] != '.'; + + if (isValid) { + std::vector<std::string> splittedServiceName = split(serviceName, '.'); + + for (auto serviceNameElementIt = splittedServiceName.begin(); + serviceNameElementIt != splittedServiceName.end() && isValid; + ++serviceNameElementIt) { + isValid &= containsOnlyAlphanumericCharacters(*serviceNameElementIt); + } + } + + return isValid; +} + +inline bool isValidInstanceId(const std::string& instanceId) { + //Validation rules for ServiceName and InstanceID are equivalent + return isValidServiceName(instanceId); +} + +inline bool isValidCommonApiAddress(const std::string& commonApiAddress) { + std::vector<std::string> splittedAddress = split(commonApiAddress, ':'); + if (splittedAddress.size() != 3) { return false; } - return true; + return isValidDomainName(splittedAddress[0]) && isValidServiceName(splittedAddress[1]) && isValidInstanceId(splittedAddress[2]); } |