Commit 1666189a authored by Francis Dupont's avatar Francis Dupont
Browse files

[4204fd] Moved convert_option_code to context code

parent 3f5189fb
......@@ -52,3 +52,29 @@ EvalContext::error (const std::string& what)
{
isc_throw(EvalParseError, what);
}
uint16_t
EvalContext::convert_option_code(const std::string& option_code,
const isc::eval::location& loc)
{
int n = 0;
try {
n = boost::lexical_cast<int>(option_code);
} catch (const boost::bad_lexical_cast &) {
// This can't happen...
error(loc, "Option code has invalid value in " + option_code);
}
if (option_universe_ == Option::V6) {
if (n < 0 || n > 65535) {
error(loc, "Option code has invalid value in "
+ option_code + ". Allowed range: 0..65535");
}
} else {
if (n < 0 || n > 255) {
error(loc, "Option code has invalid value in "
+ option_code + ". Allowed range: 0..255");
}
}
return (static_cast<uint16_t>(n));
}
......@@ -85,6 +85,16 @@ public:
/// cases when the EvalParser is not able to handle the packet.
void error(const std::string& what);
/// @brief Option code convertion
///
/// @param option_code a string representing the integer code
/// @param loc the location of the token
/// @result the option code
/// @throw calls the syntax error function if the value is no in
/// the range 0..255 or 0..65535
uint16_t convert_option_code(const std::string& option_code,
const isc::eval::location& loc);
/// @brief Option universe: DHCPv4 or DHCPv6.
///
/// This is used by the parser to determine which option definitions
......
......@@ -66,41 +66,6 @@ using namespace isc::eval;
%printer { yyoutput << $$; } <*>;
%code
{
namespace {
/* Convert option code specified as string to an 16 bit unsigned
representation. If the option code is not within the range of
0..65535 an error is reported. */
uint16_t
convert_option_code(const std::string& option_code,
const isc::eval::EvalParser::location_type& loc,
EvalContext& ctx) {
int n = 0;
try {
n = boost::lexical_cast<int>(option_code);
} catch (const boost::bad_lexical_cast &) {
// This can't happen...
ctx.error(loc, "Option code has invalid value in " + option_code);
}
if (ctx.option_universe_ == Option::V6) {
if (n < 0 || n > 65535) {
ctx.error(loc, "Option code has invalid value in "
+ option_code + ". Allowed range: 0..65535");
}
} else {
if (n < 0 || n > 255) {
ctx.error(loc, "Option code has invalid value in "
+ option_code + ". Allowed range: 0..255");
}
}
return (static_cast<uint16_t>(n));
}
}
}
%%
// The whole grammar starts with an expression.
......@@ -130,13 +95,13 @@ string_expr : STRING
}
| OPTION "[" INTEGER "]" DOT TEXT
{
uint16_t numeric_code = convert_option_code($3, @3, ctx);
uint16_t numeric_code = ctx.convert_option_code($3, @3);
TokenPtr opt(new TokenOption(numeric_code, TokenOption::TEXTUAL));
ctx.expression.push_back(opt);
}
| OPTION "[" INTEGER "]" DOT HEX
{
uint16_t numeric_code = convert_option_code($3, @3, ctx);
uint16_t numeric_code = ctx.convert_option_code($3, @3);
TokenPtr opt(new TokenOption(numeric_code, TokenOption::HEXADECIMAL));
ctx.expression.push_back(opt);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment