Kea: binding_variables.h Source File

| Kea 3.2.0-git

binding_variables.h
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#ifndef BINDING_VARIABLES_H
8#define BINDING_VARIABLES_H
9
10#include <cc/base_stamped_element.h>
11#include <cc/cfg_to_element.h>
12#include <cc/data.h>
13#include <cc/simple_parser.h>
14#include <dhcp/pkt.h>
15#include <dhcpsrv/lease.h>
16#include <eval/evaluate.h>
17#include <eval/token.h>
18
19#include <boost/scoped_ptr.hpp>
20#include <boost/multi_index_container.hpp>
21#include <boost/multi_index/mem_fun.hpp>
22#include <boost/multi_index/hashed_index.hpp>
23#include <boost/multi_index/ordered_index.hpp>
24#include <boost/multi_index/sequenced_index.hpp>
25
26namespace isc {
27namespace lease_cmds {
28
29// Forward declaration for pointer.
30class BindingVariable;
31
33 typedef boost::shared_ptr<BindingVariable> BindingVariablePtr;
34
37 class BindingVariable : public isc::data::CfgToElement {
38public:
41 enum Source {
42 QUERY,
43 RESPONSE
44 };
45
47 static const data::SimpleKeywords CONFIG_KEYWORDS;
48
63 BindingVariable(const std::string& name,
64 const std::string& expression_str,
65 const Source& source,
66 uint32_t family);
67
69 virtual ~BindingVariable() = default;
70
77 static BindingVariablePtr parse(data::ConstElementPtr config, uint16_t family);
78
84 std::string evaluate(dhcp::PktPtr packet) const;
85
89 std::string getName() const {
90 return (name_);
91 }
92
96 std::string getExpressionStr() const {
97 return (expression_str_);
98 }
99
103 dhcp::ExpressionPtr getExpression() const {
104 return (expression_);
105 }
106
110 Source getSource() const {
111 return (source_);
112 }
113
117 uint32_t getFamily() const {
118 return (family_);
119 }
120
125 virtual data::ElementPtr toElement() const;
126
127private:
131
133 std::string name_;
134
136 std::string expression_str_;
137
139 Source source_;
140
142 uint32_t family_;
143
145 dhcp::ExpressionPtr expression_;
146};
147
149 typedef std::list<BindingVariablePtr> BindingVariableList;
150
152 typedef boost::shared_ptr<BindingVariableList> BindingVariableListPtr;
153
155 struct VariableSequenceTag { };
156
158 struct VariableNameTag { };
159
161 struct VariableSourceTag { };
162
164typedef boost::multi_index_container<
165 BindingVariablePtr,
166 boost::multi_index::indexed_by<
167 // First index is by sequence. -- Do we need this one?
168 boost::multi_index::sequenced<
169 boost::multi_index::tag<VariableSequenceTag>
170 >,
171 // Second index is by name.
172 boost::multi_index::hashed_unique<
173 boost::multi_index::tag<VariableNameTag>,
174 boost::multi_index::const_mem_fun<BindingVariable,
175 std::string,
176 &BindingVariable::getName>
177 >,
178 // Third index is by source.
179 boost::multi_index::ordered_non_unique<
180 boost::multi_index::tag<VariableSourceTag>,
181 boost::multi_index::const_mem_fun<BindingVariable,
182 BindingVariable::Source,
183 &BindingVariable::getSource>
184 >
185 >
186> BindingVariableContainer;
187
194 class BindingVariableCache : public data::BaseStampedElement {
195public:
197 BindingVariableCache();
198
200 virtual ~BindingVariableCache() = default;
201
210 bool add(BindingVariablePtr variable);
211
213 void clear();
214
216 size_t size();
217
220 boost::posix_time::ptime getLastFlushTime();
221
226 BindingVariableListPtr getAll();
227
232 BindingVariablePtr getByName(const std::string& name);
233
238 BindingVariableListPtr getBySource(const BindingVariable::Source& source);
239
240private:
242 BindingVariableContainer variables_;
243
245 const boost::scoped_ptr<std::mutex> mutex_;
246
247};
248
250 typedef boost::shared_ptr<BindingVariableCache> BindingVariableCachePtr;
251
254 class BindingVariableMgr {
255public:
260 explicit BindingVariableMgr(uint32_t family);
261
263 ~BindingVariableMgr() = default;
264
286 void configure(data::ConstElementPtr config);
287
302 bool evaluateVariables(dhcp::PktPtr query, dhcp::PktPtr response,
303 dhcp::LeasePtr lease);
304
308 BindingVariableCachePtr getCache() {
309 return(cache_);
310 }
311
312private:
314 uint32_t family_;
315
317 BindingVariableCachePtr cache_;
318};
319
321 typedef boost::shared_ptr<BindingVariableMgr> BindingVariableMgrPtr;
322
323
324} // end of namespace lease_cmds
325} // end of namespace isc
326#endif
base_stamped_element.h
cfg_to_element.h
isc::data::BaseStampedElement
This class represents configuration element which is associated with database identifier and the modi...
Definition base_stamped_element.h:30
isc::lease_cmds::BindingVariableCache::getBySource
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...
Definition binding_variables.cc:168
isc::lease_cmds::BindingVariableCache::size
size_t size()
Returns number of entries in the cache.
Definition binding_variables.cc:131
isc::lease_cmds::BindingVariableCache::clear
void clear()
Delete all the entries in the cache.
Definition binding_variables.cc:122
isc::lease_cmds::BindingVariableCache::getByName
BindingVariablePtr getByName(const std::string &name)
Fetches a binding variable by name.
Definition binding_variables.cc:159
isc::lease_cmds::BindingVariableCache::getAll
BindingVariableListPtr getAll()
Fetches all of the binding variables in the order they were added to the cache.
Definition binding_variables.cc:143
isc::lease_cmds::BindingVariableCache::getLastFlushTime
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)...
Definition binding_variables.cc:137
isc::lease_cmds::BindingVariableCache::BindingVariableCache
BindingVariableCache()
Constructor.
Definition binding_variables.cc:110
isc::lease_cmds::BindingVariableCache::add
bool add(BindingVariablePtr variable)
Adds (or replaces) the variable in the cache.
Definition binding_variables.cc:115
isc::lease_cmds::BindingVariableCache::~BindingVariableCache
virtual ~BindingVariableCache()=default
Destructor.
isc::lease_cmds::BindingVariableMgr::evaluateVariables
bool evaluateVariables(dhcp::PktPtr query, dhcp::PktPtr response, dhcp::LeasePtr lease)
Evaluates the binding variables for a given lease and packet pair.
Definition binding_variables.cc:219
isc::lease_cmds::BindingVariableMgr::getCache
BindingVariableCachePtr getCache()
Fetches the current variables cache.
Definition binding_variables.h:308
isc::lease_cmds::BindingVariableMgr::BindingVariableMgr
BindingVariableMgr(uint32_t family)
Constructor.
Definition binding_variables.cc:182
isc::lease_cmds::BindingVariableMgr::~BindingVariableMgr
~BindingVariableMgr()=default
Destructor;.
isc::lease_cmds::BindingVariableMgr::configure
void configure(data::ConstElementPtr config)
Configures the manager based on the given configuration.
Definition binding_variables.cc:192
isc::lease_cmds::BindingVariable
Embodies a named expression, whose output when evaluated can be stored in a lease's user-context.
Definition binding_variables.h:37
isc::lease_cmds::BindingVariable::CONFIG_KEYWORDS
static const data::SimpleKeywords CONFIG_KEYWORDS
List of valid configurable parameters for a BindingVariable.
Definition binding_variables.h:47
isc::lease_cmds::BindingVariable::Source
Source
Specifies the packet that the expression should be evaluated against.
Definition binding_variables.h:41
isc::lease_cmds::BindingVariable::QUERY
@ QUERY
Definition binding_variables.h:42
isc::lease_cmds::BindingVariable::RESPONSE
@ RESPONSE
Definition binding_variables.h:43
isc::lease_cmds::BindingVariable::~BindingVariable
virtual ~BindingVariable()=default
Destructor.
isc::lease_cmds::BindingVariable::evaluate
std::string evaluate(dhcp::PktPtr packet) const
Evaluate the variable against the given packet.
Definition binding_variables.cc:92
isc::lease_cmds::BindingVariable::toElement
virtual data::ElementPtr toElement() const
Creates an Element tree for the variable's configurable members.
Definition binding_variables.cc:102
isc::lease_cmds::BindingVariable::getFamily
uint32_t getFamily() const
Fetches the variable's protocol family.
Definition binding_variables.h:117
isc::lease_cmds::BindingVariable::getExpressionStr
std::string getExpressionStr() const
Fetches the variable's pre-parsed expression string.
Definition binding_variables.h:96
isc::lease_cmds::BindingVariable::getExpression
dhcp::ExpressionPtr getExpression() const
Fetches the variable's parsed expression.
Definition binding_variables.h:103
isc::lease_cmds::BindingVariable::getSource
Source getSource() const
Fetches the variable's packet source.
Definition binding_variables.h:110
isc::lease_cmds::BindingVariable::getName
std::string getName() const
Fetches the variable's name.
Definition binding_variables.h:89
isc::lease_cmds::BindingVariable::BindingVariable
BindingVariable(const std::string &name, const std::string &expression_str, const Source &source, uint32_t family)
Constructor.
Definition binding_variables.cc:23
isc::lease_cmds::BindingVariable::parse
static BindingVariablePtr parse(data::ConstElementPtr config, uint16_t family)
Parses configuration elements into a BindingVarable instance.
Definition binding_variables.cc:64
data.h
evaluate.h
lease.h
simple_parser.h
isc::config
Definition command_interpreter.cc:23
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::data::SimpleKeywords
std::map< std::string, isc::data::Element::types > SimpleKeywords
This specifies all accepted keywords with their types.
Definition lib/cc/simple_parser.h:36
isc::dhcp::PktPtr
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition pkt.h:999
isc::dhcp::ExpressionPtr
boost::shared_ptr< Expression > ExpressionPtr
Definition token.h:31
isc::dhcp::LeasePtr
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition lease.h:25
isc::lease_cmds
Definition binding_variables.cc:21
isc::lease_cmds::BindingVariableMgrPtr
boost::shared_ptr< BindingVariableMgr > BindingVariableMgrPtr
Defines a shared pointer to a BindingVariableMgr.
Definition binding_variables.h:321
isc::lease_cmds::BindingVariablePtr
boost::shared_ptr< BindingVariable > BindingVariablePtr
Defines a shared pointer to a BindingVariable.
Definition binding_variables.h:33
isc::lease_cmds::BindingVariableList
std::list< BindingVariablePtr > BindingVariableList
Defines a list of BindingVariablePtr instances.
Definition binding_variables.h:149
isc::lease_cmds::BindingVariableContainer
boost::multi_index_container< BindingVariablePtr, boost::multi_index::indexed_by< boost::multi_index::sequenced< boost::multi_index::tag< VariableSequenceTag > >, boost::multi_index::hashed_unique< boost::multi_index::tag< VariableNameTag >, boost::multi_index::const_mem_fun< BindingVariable, std::string, &BindingVariable::getName > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< VariableSourceTag >, boost::multi_index::const_mem_fun< BindingVariable, BindingVariable::Source, &BindingVariable::getSource > > > > BindingVariableContainer
the client class multi-index.
Definition binding_variables.h:186
isc::lease_cmds::BindingVariableListPtr
boost::shared_ptr< BindingVariableList > BindingVariableListPtr
Defines a pointer to a list of BindingVariablePtrs.
Definition binding_variables.h:152
isc::lease_cmds::BindingVariableCachePtr
boost::shared_ptr< BindingVariableCache > BindingVariableCachePtr
Defines a shared pointer to a BindingVariableCache.
Definition binding_variables.h:250
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
pkt.h
isc::data::CfgToElement
Abstract class for configuration Cfg_* classes.
Definition cfg_to_element.h:29
isc::lease_cmds::VariableNameTag
Tag for the name index.
Definition binding_variables.h:158
isc::lease_cmds::VariableSequenceTag
Tag for the sequence index.
Definition binding_variables.h:155
isc::lease_cmds::VariableSourceTag
Tag for the source index.
Definition binding_variables.h:161
token.h