From c5f062cd6510b042c8fab3725f95a0588369b089 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Mon, 31 Aug 2015 21:58:02 +0300 Subject: [PATCH] Fixed config parser for mod_radius. --- .../stargazer/plugins/other/radius/config.cpp | 39 ++++++++++++++++--- .../stargazer/plugins/other/radius/config.h | 4 +- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/projects/stargazer/plugins/other/radius/config.cpp b/projects/stargazer/plugins/other/radius/config.cpp index 64ff3707..23339c02 100644 --- a/projects/stargazer/plugins/other/radius/config.cpp +++ b/projects/stargazer/plugins/other/radius/config.cpp @@ -64,7 +64,8 @@ size_t checkChar(const std::string& value, size_t start, char ch) std::pair readString(const std::string& value, size_t start) { std::string dest; - while (start < value.length() && !std::isspace(value[start])) + while (start < value.length() && !std::isspace(value[start]) && + value[start] != ',' && value[start] != '(' && value[start] != ')') dest.push_back(value[start++]); if (dest.empty()) { if (start == value.length()) @@ -85,16 +86,21 @@ Config::Pairs toPairs(const std::vector& values) while (start < value.size()) { Config::Pair pair; start = skipSpaces(value, start); + if (!res.empty()) + { + start = checkChar(value, start, ','); + start = skipSpaces(value, start); + } size_t pairStart = start; start = checkChar(value, start, '('); - std::pair key = readString(value, start); + const std::pair key = readString(value, start); start = key.first; pair.first = key.second; start = skipSpaces(value, start); start = checkChar(value, start, ','); start = skipSpaces(value, start); - std::pair val = readString(value, start); - start = key.first; + const std::pair val = readString(value, start); + start = val.first; pair.second = val.second; start = skipSpaces(value, start); start = checkChar(value, start, ')'); @@ -164,6 +170,27 @@ T parseInt(const std::string& paramName, const MODULE_SETTINGS& params) return 0; } +std::string parseAddress(const std::string& address) +{ + size_t pos = address.find_first_of(':'); + if (pos == std::string::npos) + throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'."); + return address.substr(pos + 1); +} + +Config::Type parseConnectionType(const std::string& address) +{ + size_t pos = address.find_first_of(':'); + if (pos == std::string::npos) + throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'."); + std::string type = ToLower(address.substr(0, pos)); + if (type == "unix") + return Config::UNIX; + else if (type == "tcp") + return Config::TCP; + throw ParserError(0, "Invalid connection type. Should be either 'unix' or 'tcp', got '" + type + "'"); +} + } // namespace anonymous Config::Config(const MODULE_SETTINGS& settings) @@ -171,7 +198,9 @@ Config::Config(const MODULE_SETTINGS& settings) modify(parseVector("modify", settings)), reply(parseVector("reply", settings)), verbose(parseBool("verbose", settings)), - bindAddress(parseString("bind_address", settings)), + address(parseString("bind_address", settings)), + bindAddress(parseAddress(address)), + connectionType(parseConnectionType(address)), portStr(parseString("port", settings)), port(parseInt("port", settings)), key(parseString("key", settings)) diff --git a/projects/stargazer/plugins/other/radius/config.h b/projects/stargazer/plugins/other/radius/config.h index d6553e14..28da964e 100644 --- a/projects/stargazer/plugins/other/radius/config.h +++ b/projects/stargazer/plugins/other/radius/config.h @@ -37,6 +37,7 @@ struct Config typedef std::pair Pair; enum Type { UNIX, TCP }; + Config() {} Config(const MODULE_SETTINGS& settings); Pairs match; @@ -45,8 +46,9 @@ struct Config bool verbose; - Type connectionType; + std::string address; std::string bindAddress; + Type connectionType; std::string portStr; uint16_t port; std::string key; -- 2.44.2