return 0;
}
+uint16_t toPort(const std::string& value)
+{
+ if (value.empty())
+ return 0;
+ uint16_t res = 0;
+ if (str2x(value, res) == 0)
+ return res;
+ throw ParserError(0, "'" + value + "' is not a valid port number.");
+}
+
+typedef std::map<std::string, Config::ReturnCode> Codes;
+
+// One-time call to initialize the list of codes.
+Codes getCodes()
+{
+ Codes res;
+ res["reject"] = Config::REJECT;
+ res["fail"] = Config::FAIL;
+ res["ok"] = Config::OK;
+ res["handled"] = Config::HANDLED;
+ res["invalid"] = Config::INVALID;
+ res["userlock"] = Config::USERLOCK;
+ res["notfound"] = Config::NOTFOUND;
+ res["noop"] = Config::NOOP;
+ res["updated"] = Config::UPDATED;
+ return res;
+}
+
+Config::ReturnCode toReturnCode(const std::vector<std::string>& values)
+{
+ static Codes codes(getCodes());
+ if (values.empty())
+ return Config::REJECT;
+ std::string code = ToLower(values[0]);
+ const Codes::const_iterator it = codes.find(code);
+ if (it == codes.end())
+ return Config::REJECT;
+ return it->second;
+}
+
Config::Pairs parseVector(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
{
for (size_t i = 0; i < params.size(); ++i)
return Config::Pairs();
}
+Config::ReturnCode parseReturnCode(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
+{
+ for (size_t i = 0; i < params.size(); ++i)
+ if (params[i].param == paramName)
+ return toReturnCode(params[i].value);
+ return Config::REJECT;
+}
+
bool parseBool(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
{
for (size_t i = 0; i < params.size(); ++i)
return "";
}
-std::string parseAddress(const std::string& address)
+std::string parseAddress(Config::Type connectionType, const std::string& value)
{
- size_t pos = address.find_first_of(':');
+ size_t pos = value.find_first_of(':');
+ if (pos == std::string::npos)
+ throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'.");
+ if (connectionType == Config::UNIX)
+ return value.substr(pos + 1);
+ std::string address(value.substr(pos + 1));
+ pos = address.find_first_of(':', pos + 1);
+ if (pos == std::string::npos)
+ throw ParserError(0, "Port is not specified.");
+ return address.substr(0, pos - 1);
+}
+
+std::string parsePort(Config::Type connectionType, const std::string& value)
+{
+ size_t pos = value.find_first_of(':');
if (pos == std::string::npos)
throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'.");
+ if (connectionType == Config::UNIX)
+ return "";
+ std::string address(value.substr(pos + 1));
+ pos = address.find_first_of(':', pos + 1);
+ if (pos == std::string::npos)
+ throw ParserError(0, "Port is not specified.");
return address.substr(pos + 1);
}
if (params[i].param == paramName)
return Config::Section(parseVector("match", params[i].sections),
parseVector("modify", params[i].sections),
- parseVector("reply", params[i].sections));
+ parseVector("reply", params[i].sections),
+ parseReturnCode("no_match", params[i].sections));
return Config::Section();
}
acct(parseSection("acct", settings.moduleParams)),
verbose(parseBool("verbose", settings.moduleParams)),
address(parseString("bind_address", settings.moduleParams)),
- bindAddress(parseAddress(address)),
connectionType(parseConnectionType(address)),
+ bindAddress(parseAddress(connectionType, address)),
+ portStr(parsePort(connectionType, address)),
+ port(toPort(portStr)),
key(parseString("key", settings.moduleParams)),
sockUID(parseUID("sock_owner", settings.moduleParams)),
sockGID(parseGID("sock_group", settings.moduleParams)),