Kea 2.5.8
evaluate.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <eval/evaluate.h>
10
11namespace isc {
12namespace dhcp {
13
14bool evaluateBool(const Expression& expr, Pkt& pkt) {
15 ValueStack values;
16 for (auto const& it : expr) {
17 it->evaluate(pkt, values);
18 }
19 if (values.size() != 1) {
20 isc_throw(EvalBadStack, "Incorrect stack order. Expected exactly "
21 "1 value at the end of evaluation, got " << values.size());
22 }
23 return (Token::toBool(values.top()));
24}
25
26std::string
27evaluateString(const Expression& expr, Pkt& pkt) {
28 ValueStack values;
29 for (auto const& it : expr) {
30 it->evaluate(pkt, values);
31 }
32 if (values.size() != 1) {
33 isc_throw(EvalBadStack, "Incorrect stack order. Expected exactly "
34 "1 value at the end of evaluation, got " << values.size());
35 }
36 return (values.top());
37}
38
39} // end of isc::dhcp namespace
40} // end of isc namespace
EvalBadStack is thrown when more or less parameters are on the stack than expected.
Definition: token.h:37
Base class for classes representing DHCP messages.
Definition: pkt.h:161
static bool toBool(std::string value)
Coverts a (string) value to a boolean.
Definition: token.h:89
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::string evaluateString(const Expression &expr, Pkt &pkt)
Definition: evaluate.cc:27
bool evaluateBool(const Expression &expr, Pkt &pkt)
Evaluate a RPN expression for a v4 or v6 packet and return a true or false decision.
Definition: evaluate.cc:14
std::vector< TokenPtr > Expression
This is a structure that holds an expression converted to RPN.
Definition: token.h:28
std::stack< std::string > ValueStack
Evaluated values are stored as a stack of strings.
Definition: token.h:33
Defines the logger used by the top-level component of kea-lfc.