1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 | // Copyright (C) 2025 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <binding_variables.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iostream><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cc/data.h>
#include <eval/eval_context.h>
#include <util/multi_threading_mgr.h>
using namespace isc::dhcp;
using namespace isc::data;
namespace isc {
namespace lease_cmds {
BindingVariable::BindingVariable(const std::string& name,
const std::string& expression_str,
const Source& source,
uint32_t family)
: name_(name), expression_str_(expression_str), source_(source),
family_(family) {
if (name_.empty()) {
isc_throw(BadValue, "BindingVariable - name cannot be empty");
}
/// @todo If we add scopes we may wish to allow higher order
/// scopes to override lower scopes with empty expressions.
if (expression_str_.empty()) {
isc_throw(BadValue, "BindingVariable - '" << name_
<< "' expression_str cannot be empty");
}
if (family_ != AF_INET && family_ != AF_INET6) {
isc_throw(BadValue, "BindingVariable - '" << name_
<< "', invalid family: " << family_);
}
try {
EvalContext eval_ctx(family_ == AF_INET ? Option::V4 : Option::V6);
eval_ctx.parseString(expression_str_, EvalContext::PARSER_STRING);
expression_.reset(new Expression(eval_ctx.expression_));
} catch (const std::exception& ex) {
isc_throw(BadValue, "BindingVariable - '" << name_ << "', error parsing expression: '"
<< expression_str_ << "' : " << ex.what());
}
}
const data::SimpleKeywords
BindingVariable::CONFIG_KEYWORDS =
{
{ "name", Element::string },
{ "expression", Element::string },
{ "source", Element::string }
};
BindingVariablePtr
BindingVariable::parse(data::ConstElementPtr config, uint16_t family) {
// Note checkKeywords() will throw DhcpConfigError if there is a problem.
SimpleParser::checkKeywords(CONFIG_KEYWORDS, config);
// Parse members.
std::string name = SimpleParser::getString(config, "name");;
std::string expression = SimpleParser::getString(config, "expression");;
std::string source_str = SimpleParser::getString(config, "source");;
try {
Source source;
if (source_str == "query") {
source = QUERY;
} else if (source_str == "response") {
source = RESPONSE;
} else {
isc_throw(BadValue, "invalid source '" << source_str
<< "', must be either 'query' or 'response'");
}
// Attempt to create the variable.
return (BindingVariablePtr(new BindingVariable(name, expression, source, family)));
} catch (const std::exception& ex) {
isc_throw(DhcpConfigError, "invalid config: " << ex.what());
}
}
std::string
BindingVariable::evaluate(PktPtr packet) const {
try {
return (evaluateString(*expression_, *packet));
} catch (const std::exception& ex) {
isc_throw(BadValue, "BindingVariable - " << name_ << ", error evaluating expression: ["
<< expression_str_ << "] : " << ex.what());
}
}
ElementPtr
BindingVariable::toElement() const {
ElementPtr map = Element::createMap();
map->set("name", Element::create(name_));
map->set("expression", Element::create(expression_str_));
map->set("source", Element::create((source_ == QUERY ? "query" : "response")));
return (map);
}
BindingVariableCache::BindingVariableCache()
: variables_(), mutex_(new std::mutex) {
}
bool
BindingVariableCache::add(BindingVariablePtr variable) {
util::MultiThreadingLock lock(*mutex_);
auto retpair = variables_.push_back(variable);
return(retpair.second);
}
void
BindingVariableCache::clear() {
util::MultiThreadingLock lock(*mutex_);
// Discard contents.
// We use modification time to remember the last time we flushed.
variables_.clear();
updateModificationTime();
}
size_t
BindingVariableCache::size() {
util::MultiThreadingLock lock(*mutex_);
return (variables_.size());
}
boost::posix_time::ptime
BindingVariableCache::getLastFlushTime() {
util::MultiThreadingLock lock(*mutex_);
return (BaseStampedElement::getModificationTime());
}
BindingVariableListPtr
BindingVariableCache::getAll() {
util::MultiThreadingLock lock(*mutex_);
BindingVariableListPtr var_list(new BindingVariableList());
const auto& index = variables_.get<VariableSequenceTag>();
for (auto const& variable : index) {
/// For now we'll return the pointer, w/o making a copy
/// of the variable itself. We never updates variables
/// so we should be OK.
var_list->push_back(variable);<--- Consider using std::copy algorithm instead of a raw loop.
}
return (var_list);
}
BindingVariablePtr
BindingVariableCache::getByName(const std::string& name) {
util::MultiThreadingLock lock(*mutex_);
const auto& index = variables_.get<VariableNameTag>();
auto var_iter = index.find(name);
return (var_iter == index.end() ? BindingVariablePtr() : *var_iter);
}
BindingVariableListPtr
BindingVariableCache::getBySource(const BindingVariable::Source& source) {
util::MultiThreadingLock lock(*mutex_);
BindingVariableListPtr var_list(new BindingVariableList());
const auto& index = variables_.get<VariableSourceTag>();
auto lower_limit = index.lower_bound(source);
auto upper_limit = index.upper_bound(source);
for (auto var_iter = lower_limit; var_iter != upper_limit; ++var_iter) {
var_list->push_back(*var_iter);
}
return (var_list);
}
BindingVariableMgr::BindingVariableMgr(uint32_t family)
: family_(family) {
if (family_ != AF_INET && family_ != AF_INET6) {
isc_throw (BadValue, "BindingVariableMgr - invalid family: " << family_);
}
cache_.reset(new BindingVariableCache());
}
void
BindingVariableMgr::configure(data::ConstElementPtr config) {
// Always wipe the cache.
cache_->clear();
ConstElementPtr elem = config->get("binding-variables");
if (!elem) {
// Nothing to do.
return;
}
// binding-variables should be a list.
if (elem->getType() != Element::list) {
isc_throw(DhcpConfigError, "'binding-variables' must be a list");
}
// iterate over the list adding variables to the cache
for (auto const& var_elem : elem->listValue()) {
try {
BindingVariablePtr variable = BindingVariable::parse(var_elem, family_);
cache_->add(variable);
} catch (const std::exception& ex) {
isc_throw(DhcpConfigError, "cannot add BindingVariable to cache: " << ex.what());
}
}
}
bool
BindingVariableMgr::evaluateVariables(PktPtr query, PktPtr response, LeasePtr lease) {
if (!query || !response || !lease) {
isc_throw(BadValue, "evaluateVariables - missing query, response, and/or lease");
}
if (!cache_->size()) {
/// @todo If the lease has binding-variables in its context from a prior
/// update, but config changed and now there are none defined, should we
/// removed them from the lease? For now, we'll leave them.
return(false);
}
auto const variables = cache_->getAll();
ElementPtr values = Element::createMap();
for (auto const& variable : *variables) {
try {
auto value = evaluateString(*(variable->getExpression()),
(variable->getSource() == BindingVariable::QUERY ?
*query : *response));
values->set(variable->getName(), Element::create(value));
} catch (const std::exception& ex) {
isc_throw(Unexpected, "expression blew up: " << ex.what());
}
}
return (lease->updateUserContextISC("binding-variables", values));
}
} // end of namespace lease_cmds
} // end of namespace isc
|