Kea 2.7.5
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
14void
15evaluateRaw(const Expression& expr, Pkt& pkt, ValueStack& values) {
16 for (auto it = expr.cbegin(); it != expr.cend(); ) {
17 unsigned label = (*it++)->evaluate(pkt, values);
18 if (label == 0) {
19 continue;
20 }
21 // Scan for the given label.
22 for (;;) {
23 if (it == expr.cend()) {
24 isc_throw(EvalBadLabel, "can't reach label " << label);
25 }
26 if ((*it++)->getLabel() == label) {
27 break;
28 }
29 }
30 }
31}
32
33bool
34evaluateBool(const Expression& expr, Pkt& pkt) {
35 ValueStack values;
36 evaluateRaw(expr, pkt, values);
37 if (values.size() != 1) {
38 isc_throw(EvalBadStack, "Incorrect stack order. Expected exactly "
39 "1 value at the end of evaluation, got " << values.size());
40 }
41 return (Token::toBool(values.top()));
42}
43
44std::string
45evaluateString(const Expression& expr, Pkt& pkt) {
46 ValueStack values;
47 evaluateRaw(expr, pkt, values);
48 if (values.size() != 1) {
49 isc_throw(EvalBadStack, "Incorrect stack order. Expected exactly "
50 "1 value at the end of evaluation, got " << values.size());
51 }
52 return (values.top());
53}
54
55} // end of isc::dhcp namespace
56} // end of isc namespace
EvalBadLabel is thrown when a label can't be found.
Definition token.h:53
EvalBadStack is thrown when more or less parameters are on the stack than expected.
Definition token.h:38
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:105
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::string evaluateString(const Expression &expr, Pkt &pkt)
Evaluate a RPN expression for a v4 or v6 packet and return a string value.
Definition evaluate.cc:45
void evaluateRaw(const Expression &expr, Pkt &pkt, ValueStack &values)
Evaluate a RPN expression for a v4 or v6 packet.
Definition evaluate.cc:15
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:34
std::vector< TokenPtr > Expression
This is a structure that holds an expression converted to RPN.
Definition token.h:29
std::stack< std::string > ValueStack
Evaluated values are stored as a stack of strings.
Definition token.h:34
Defines the logger used by the top-level component of kea-lfc.