Kea 3.3.3
binding_variables.h
Go to the documentation of this file.
1// Copyright (C) 2025-2026 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 <cstdint>
20
21#include <boost/multi_index/hashed_index.hpp>
22#include <boost/multi_index/mem_fun.hpp>
23#include <boost/multi_index/ordered_index.hpp>
24#include <boost/multi_index/sequenced_index.hpp>
25#include <boost/multi_index_container.hpp>
26#include <boost/scoped_ptr.hpp>
27
28namespace isc {
29namespace lease_cmds {
30
31// Forward declaration for pointer.
32class BindingVariable;
33
35typedef boost::shared_ptr<BindingVariable> BindingVariablePtr;
36
40public:
43 enum Source : uint16_t {
46 };
47
50
65 BindingVariable(const std::string& name,
66 const std::string& expression_str,
67 const Source& source,
68 uint32_t family);
69
71 virtual ~BindingVariable() = default;
72
80
86 std::string evaluate(dhcp::PktPtr packet) const;
87
91 std::string getName() const {
92 return (name_);
93 }
94
98 std::string getExpressionStr() const {
99 return (expression_str_);
100 }
101
106 return (expression_);
107 }
108
113 return (source_);
114 }
115
119 uint32_t getFamily() const {
120 return (family_);
121 }
122
127 virtual data::ElementPtr toElement() const;
128
129private:
133
135 std::string name_;
136
138 std::string expression_str_;
139
141 Source source_;
142
144 uint32_t family_;
145
147 dhcp::ExpressionPtr expression_;
148};
149
151typedef std::list<BindingVariablePtr> BindingVariableList;
152
154typedef boost::shared_ptr<BindingVariableList> BindingVariableListPtr;
155
158
161
164
166typedef boost::multi_index_container<
168 boost::multi_index::indexed_by<
169 // First index is by sequence. -- Do we need this one?
170 boost::multi_index::sequenced<
171 boost::multi_index::tag<VariableSequenceTag>
172 >,
173 // Second index is by name.
174 boost::multi_index::hashed_unique<
175 boost::multi_index::tag<VariableNameTag>,
176 boost::multi_index::const_mem_fun<BindingVariable,
177 std::string,
179 >,
180 // Third index is by source.
181 boost::multi_index::ordered_non_unique<
182 boost::multi_index::tag<VariableSourceTag>,
183 boost::multi_index::const_mem_fun<BindingVariable,
186 >
187 >
189
197public:
200
202 virtual ~BindingVariableCache() = default;
203
212 bool add(BindingVariablePtr variable);
213
215 void clear();
216
218 size_t size();
219
222 boost::posix_time::ptime getLastFlushTime();
223
229
234 BindingVariablePtr getByName(const std::string& name);
235
241
242private:
244 BindingVariableContainer variables_;
245
247 const boost::scoped_ptr<std::mutex> mutex_;
248
249};
250
252typedef boost::shared_ptr<BindingVariableCache> BindingVariableCachePtr;
253
257public:
262 explicit BindingVariableMgr(uint32_t family);
263
266
289
304 bool evaluateVariables(dhcp::PktPtr query, dhcp::PktPtr response,
305 dhcp::LeasePtr lease);
306
311 return(cache_);
312 }
313
314private:
316 uint32_t family_;
317
320};
321
323typedef boost::shared_ptr<BindingVariableMgr> BindingVariableMgrPtr;
324
325
326} // end of namespace lease_cmds
327} // end of namespace isc
328#endif
This class represents configuration element which is associated with database identifier and the modi...
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.
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.
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.
Source
Specifies the packet that the expression should be evaluated against.
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:30
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
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:1006
boost::shared_ptr< Expression > ExpressionPtr
Definition token.h:33
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition lease.h:27
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.