Kea 2.7.7
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
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
33typedef boost::shared_ptr<BindingVariable> BindingVariablePtr;
34
38public:
45
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
104 return (expression_);
105 }
106
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
149typedef std::list<BindingVariablePtr> BindingVariableList;
150
152typedef boost::shared_ptr<BindingVariableList> BindingVariableListPtr;
153
156
159
162
164typedef boost::multi_index_container<
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,
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,
184 >
185 >
187
195public:
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
227
232 BindingVariablePtr getByName(const std::string& name);
233
239
240private:
242 BindingVariableContainer variables_;
243
245 const boost::scoped_ptr<std::mutex> mutex_;
246
247};
248
250typedef boost::shared_ptr<BindingVariableCache> BindingVariableCachePtr;
251
255public:
260 explicit BindingVariableMgr(uint32_t family);
261
264
286 void configure(data::ConstElementPtr config);
287
302 bool evaluateVariables(dhcp::PktPtr query, dhcp::PktPtr response,
303 dhcp::LeasePtr lease);
304
309 return(cache_);
310 }
311
312private:
314 uint32_t family_;
315
318};
319
321typedef boost::shared_ptr<BindingVariableMgr> BindingVariableMgrPtr;
322
323
324} // end of namespace lease_cmds
325} // end of namespace isc
326#endif
This class represents configuration element which is associated with database identifier and the modi...
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.
virtual ~BindingVariableCache()=default
Destructor.
Singleton which warehouses the configured binding variables, and evaluation of variables for a given ...
bool evaluateVariables(dhcp::PktPtr query, dhcp::PktPtr response, dhcp::LeasePtr lease)
Evaluates the binding variables for a given lease and packet pair.
BindingVariableCachePtr getCache()
Fetches the current variables cache.
BindingVariableMgr(uint32_t family)
Constructor.
~BindingVariableMgr()=default
Destructor;.
void configure(data::ConstElementPtr config)
Configures the manager based on the given configuration.
Embodies a named expression, whose output when evaluated can be stored in a lease's user-context.
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.
virtual ~BindingVariable()=default
Destructor.
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.
uint32_t getFamily() const
Fetches the variable's protocol family.
std::string getExpressionStr() const
Fetches the variable's pre-parsed expression string.
dhcp::ExpressionPtr getExpression() const
Fetches the variable's parsed expression.
Source getSource() const
Fetches the variable's packet source.
std::string getName() const
Fetches the variable's name.
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.
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
boost::shared_ptr< Expression > ExpressionPtr
Definition token.h:31
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition lease.h:25
boost::shared_ptr< BindingVariableMgr > BindingVariableMgrPtr
Defines a shared pointer to a BindingVariableMgr.
boost::shared_ptr< BindingVariable > BindingVariablePtr
Defines a shared pointer to a BindingVariable.
std::list< BindingVariablePtr > BindingVariableList
Defines a list of BindingVariablePtr instances.
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.
boost::shared_ptr< BindingVariableList > BindingVariableListPtr
Defines a pointer to a list of BindingVariablePtrs.
boost::shared_ptr< BindingVariableCache > BindingVariableCachePtr
Defines a shared pointer to a BindingVariableCache.
Defines the logger used by the top-level component of kea-lfc.
Abstract class for configuration Cfg_* classes.