Kea 2.7.8
binding_variables.cc
Go to the documentation of this file.
1// Copyright (C) 2025 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
8#include <config.h>
9
10#include <binding_variables.h>
11#include <iostream>
12
13#include <cc/data.h>
14#include <eval/eval_context.h>
16
17using namespace isc::dhcp;
18using namespace isc::data;
19
20namespace isc {
21namespace lease_cmds {
22
23BindingVariable::BindingVariable(const std::string& name,
24 const std::string& expression_str,
25 const Source& source,
26 uint32_t family)
27 : name_(name), expression_str_(expression_str), source_(source),
28 family_(family) {
29 if (name_.empty()) {
30 isc_throw(BadValue, "BindingVariable - name cannot be empty");
31 }
32
35 if (expression_str_.empty()) {
36 isc_throw(BadValue, "BindingVariable - '" << name_
37 << "' expression_str cannot be empty");
38 }
39
40 if (family_ != AF_INET && family_ != AF_INET6) {
41 isc_throw(BadValue, "BindingVariable - '" << name_
42 << "', invalid family: " << family_);
43 }
44
45 try {
46 EvalContext eval_ctx(family_ == AF_INET ? Option::V4 : Option::V6);
47 eval_ctx.parseString(expression_str_, EvalContext::PARSER_STRING);
48 expression_.reset(new Expression(eval_ctx.expression_));
49 } catch (const std::exception& ex) {
50 isc_throw(BadValue, "BindingVariable - '" << name_ << "', error parsing expression: '"
51 << expression_str_ << "' : " << ex.what());
52 }
53}
54
57{
58 { "name", Element::string },
59 { "expression", Element::string },
60 { "source", Element::string }
61};
62
65 // Note checkKeywords() will throw DhcpConfigError if there is a problem.
67
68 // Parse members.
69 std::string name = SimpleParser::getString(config, "name");;
70 std::string expression = SimpleParser::getString(config, "expression");;
71 std::string source_str = SimpleParser::getString(config, "source");;
72
73 try {
74 Source source;
75 if (source_str == "query") {
76 source = QUERY;
77 } else if (source_str == "response") {
78 source = RESPONSE;
79 } else {
80 isc_throw(BadValue, "invalid source '" << source_str
81 << "', must be either 'query' or 'response'");
82 }
83
84 // Attempt to create the variable.
85 return (BindingVariablePtr(new BindingVariable(name, expression, source, family)));
86 } catch (const std::exception& ex) {
87 isc_throw(DhcpConfigError, "invalid config: " << ex.what());
88 }
89}
90
91std::string
93 try {
94 return (evaluateString(*expression_, *packet));
95 } catch (const std::exception& ex) {
96 isc_throw(BadValue, "BindingVariable - " << name_ << ", error evaluating expression: ["
97 << expression_str_ << "] : " << ex.what());
98 }
99}
100
104 map->set("name", Element::create(name_));
105 map->set("expression", Element::create(expression_str_));
106 map->set("source", Element::create((source_ == QUERY ? "query" : "response")));
107 return (map);
108}
109
111 : variables_(), mutex_(new std::mutex) {
112}
113
114bool
116 util::MultiThreadingLock lock(*mutex_);
117 auto retpair = variables_.push_back(variable);
118 return(retpair.second);
119}
120
121void
123 util::MultiThreadingLock lock(*mutex_);
124 // Discard contents.
125 // We use modification time to remember the last time we flushed.
126 variables_.clear();
128}
129
130size_t
132 util::MultiThreadingLock lock(*mutex_);
133 return (variables_.size());
134}
135
136boost::posix_time::ptime
141
144 util::MultiThreadingLock lock(*mutex_);
145
147 const auto& index = variables_.get<VariableSequenceTag>();
148 for (auto const& variable : index) {
152 var_list->push_back(variable);
153 }
154
155 return (var_list);
156}
157
159BindingVariableCache::getByName(const std::string& name) {
160 util::MultiThreadingLock lock(*mutex_);
161
162 const auto& index = variables_.get<VariableNameTag>();
163 auto var_iter = index.find(name);
164 return (var_iter == index.end() ? BindingVariablePtr() : *var_iter);
165}
166
169 util::MultiThreadingLock lock(*mutex_);
170
172 const auto& index = variables_.get<VariableSourceTag>();
173 auto lower_limit = index.lower_bound(source);
174 auto upper_limit = index.upper_bound(source);
175 for (auto var_iter = lower_limit; var_iter != upper_limit; ++var_iter) {
176 var_list->push_back(*var_iter);
177 }
178
179 return (var_list);
180}
181
183 : family_(family) {
184 if (family_ != AF_INET && family_ != AF_INET6) {
185 isc_throw (BadValue, "BindingVariableMgr - invalid family: " << family_);
186 }
187
188 cache_.reset(new BindingVariableCache());
189}
190
191void
193 // Always wipe the cache.
194 cache_->clear();
195
196 ConstElementPtr elem = config->get("binding-variables");
197 if (!elem) {
198 // Nothing to do.
199 return;
200 }
201
202 // binding-variables should be a list.
203 if (elem->getType() != Element::list) {
204 isc_throw(DhcpConfigError, "'binding-variables' must be a list");
205 }
206
207 // iterate over the list adding variables to the cache
208 for (auto const& var_elem : elem->listValue()) {
209 try {
210 BindingVariablePtr variable = BindingVariable::parse(var_elem, family_);
211 cache_->add(variable);
212 } catch (const std::exception& ex) {
213 isc_throw(DhcpConfigError, "cannot add BindingVariable to cache: " << ex.what());
214 }
215 }
216}
217
218bool
220 if (!query || !response || !lease) {
221 isc_throw(BadValue, "evaluateVariables - missing query, response, and/or lease");
222 }
223
224 if (!cache_->size()) {
228 return(false);
229 }
230
231 auto const variables = cache_->getAll();
233 for (auto const& variable : *variables) {
234 try {
235 auto value = evaluateString(*(variable->getExpression()),
236 (variable->getSource() == BindingVariable::QUERY ?
237 *query : *response));
238 values->set(variable->getName(), Element::create(value));
239 } catch (const std::exception& ex) {
240 isc_throw(Unexpected, "expression blew up: " << ex.what());
241 }
242 }
243
244 return (lease->updateUserContextISC("binding-variables", values));
245}
246
247} // end of namespace lease_cmds
248} // end of namespace isc
if(!(yy_init))
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown when an unexpected error condition occurs.
boost::posix_time::ptime getModificationTime() const
Returns timestamp.
void updateModificationTime()
Sets timestamp to the current time.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
static void checkKeywords(const SimpleKeywords &keywords, isc::data::ConstElementPtr scope)
Checks acceptable keywords with their expected type.
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
To be removed. Please use ConfigError instead.
Evaluation context, an interface to the expression evaluation.
bool parseString(const std::string &str, ParserType type=PARSER_BOOL)
Run the parser on the string specified.
@ PARSER_STRING
expression is expected to evaluate to string
isc::dhcp::Expression expression_
Parsed expression (output tokens are stored here)
BindingVariableCache stores binding variables.
BindingVariableListPtr getBySource(const BindingVariable::Source &source)
Fetches all of the binding variables in the order they were added to the cache that use a specific so...
size_t size()
Returns number of entries in the cache.
void clear()
Delete all the entries in the cache.
BindingVariablePtr getByName(const std::string &name)
Fetches a binding variable by name.
BindingVariableListPtr getAll()
Fetches all of the binding variables in the order they were added to the cache.
boost::posix_time::ptime getLastFlushTime()
Returns the last time the cache was flushed (or the time it was created if it has never been flushed)...
bool add(BindingVariablePtr variable)
Adds (or replaces) the variable in the cache.
bool evaluateVariables(dhcp::PktPtr query, dhcp::PktPtr response, dhcp::LeasePtr lease)
Evaluates the binding variables for a given lease and packet pair.
BindingVariableMgr(uint32_t family)
Constructor.
void configure(data::ConstElementPtr config)
Configures the manager based on the given configuration.
static const data::SimpleKeywords CONFIG_KEYWORDS
List of valid configurable parameters for a BindingVariable.
Source
Specifies the packet that the expression should be evaluated against.
std::string evaluate(dhcp::PktPtr packet) const
Evaluate the variable against the given packet.
virtual data::ElementPtr toElement() const
Creates an Element tree for the variable's configurable members.
BindingVariable(const std::string &name, const std::string &expression_str, const Source &source, uint32_t family)
Constructor.
static BindingVariablePtr parse(data::ConstElementPtr config, uint16_t family)
Parses configuration elements into a BindingVarable instance.
const Name & name_
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
std::map< std::string, isc::data::Element::types > SimpleKeywords
This specifies all accepted keywords with their types.
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition pkt.h:998
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
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition lease.h:25
std::vector< TokenPtr > Expression
This is a structure that holds an expression converted to RPN.
Definition token.h:29
boost::shared_ptr< BindingVariable > BindingVariablePtr
Defines a shared pointer to a BindingVariable.
std::list< BindingVariablePtr > BindingVariableList
Defines a list of BindingVariablePtr instances.
boost::shared_ptr< BindingVariableList > BindingVariableListPtr
Defines a pointer to a list of BindingVariablePtrs.
Defines the logger used by the top-level component of kea-lfc.
RAII lock object to protect the code in the same scope with a mutex.