Kea  1.9.9-git
client_class_def.cc
Go to the documentation of this file.
1 // Copyright (C) 2015-2021 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 #include <config.h>
8 
9 #include <eval/dependency.h>
11 #include <dhcpsrv/cfgmgr.h>
12 #include <boost/foreach.hpp>
13 
14 using namespace isc::data;
15 
16 namespace isc {
17 namespace dhcp {
18 
19 //********** ClientClassDef ******************//
20 
21 ClientClassDef::ClientClassDef(const std::string& name,
22  const ExpressionPtr& match_expr,
23  const CfgOptionPtr& cfg_option)
24  : name_(name), match_expr_(match_expr), required_(false),
25  depend_on_known_(false), cfg_option_(cfg_option),
26  next_server_(asiolink::IOAddress::IPV4_ZERO_ADDRESS()), valid_() {
27 
28  // Name can't be blank
29  if (name_.empty()) {
30  isc_throw(BadValue, "Client Class name cannot be blank");
31  }
32 
33  // We permit an empty expression for now. This will likely be useful
34  // for automatic classes such as vendor class.
35 
36  // For classes without options, make sure we have an empty collection
37  if (!cfg_option_) {
38  cfg_option_.reset(new CfgOption());
39  }
40 }
41 
43  : name_(rhs.name_), match_expr_(ExpressionPtr()), required_(false),
44  depend_on_known_(false), cfg_option_(new CfgOption()),
45  next_server_(asiolink::IOAddress::IPV4_ZERO_ADDRESS()) {
46 
47  if (rhs.match_expr_) {
48  match_expr_.reset(new Expression());
49  *match_expr_ = *(rhs.match_expr_);
50  }
51 
52  if (rhs.cfg_option_def_) {
53  rhs.cfg_option_def_->copyTo(*cfg_option_def_);
54  }
55 
56  if (rhs.cfg_option_) {
57  rhs.cfg_option_->copyTo(*cfg_option_);
58  }
59 
60  required_ = rhs.required_;
61  depend_on_known_ = rhs.depend_on_known_;
62  next_server_ = rhs.next_server_;
63  sname_ = rhs.sname_;
64  filename_ = rhs.filename_;
65 }
66 
68 }
69 
70 std::string
72  return (name_);
73 }
74 
75 void
76 ClientClassDef::setName(const std::string& name) {
77  name_ = name;
78 }
79 
80 const ExpressionPtr&
82  return (match_expr_);
83 }
84 
85 void
87  match_expr_ = match_expr;
88 }
89 
90 std::string
92  return (test_);
93 }
94 
95 void
96 ClientClassDef::setTest(const std::string& test) {
97  test_ = test;
98 }
99 
100 bool
102  return (required_);
103 }
104 
105 void
107  required_ = required;
108 }
109 
110 bool
112  return (depend_on_known_);
113 }
114 
115 void
116 ClientClassDef::setDependOnKnown(bool depend_on_known) {
117  depend_on_known_ = depend_on_known;
118 }
119 
120 const CfgOptionDefPtr&
122  return (cfg_option_def_);
123 }
124 
125 void
127  cfg_option_def_ = cfg_option_def;
128 }
129 
130 const CfgOptionPtr&
132  return (cfg_option_);
133 }
134 
135 void
137  cfg_option_ = cfg_option;
138 }
139 
140 bool
141 ClientClassDef::dependOnClass(const std::string& name) const {
142  return (isc::dhcp::dependOnClass(match_expr_, name));
143 }
144 
145 bool
147  return ((name_ == other.name_) &&
148  ((!match_expr_ && !other.match_expr_) ||
149  (match_expr_ && other.match_expr_ &&
150  (*match_expr_ == *(other.match_expr_)))) &&
151  ((!cfg_option_ && !other.cfg_option_) ||
152  (cfg_option_ && other.cfg_option_ &&
153  (*cfg_option_ == *other.cfg_option_))) &&
154  ((!cfg_option_def_ && !other.cfg_option_def_) ||
155  (cfg_option_def_ && other.cfg_option_def_ &&
156  (*cfg_option_def_ == *other.cfg_option_def_))) &&
157  (required_ == other.required_) &&
158  (depend_on_known_ == other.depend_on_known_) &&
159  (next_server_ == other.next_server_) &&
160  (sname_ == other.sname_) &&
161  (filename_ == other.filename_));
162 }
163 
166  uint16_t family = CfgMgr::instance().getFamily();
167  ElementPtr result = Element::createMap();
168  // Set user-context
169  contextToElement(result);
170  // Set name
171  result->set("name", Element::create(name_));
172  // Set original match expression (empty string won't parse)
173  if (!test_.empty()) {
174  result->set("test", Element::create(test_));
175  }
176  // Set only-if-required
177  if (required_) {
178  result->set("only-if-required", Element::create(required_));
179  }
180  // Set option-def (used only by DHCPv4)
181  if (cfg_option_def_ && (family == AF_INET)) {
182  result->set("option-def", cfg_option_def_->toElement());
183  }
184  // Set option-data
185  result->set("option-data", cfg_option_->toElement());
186  if (family != AF_INET) {
187  // Other parameters are DHCPv4 specific
188  return (result);
189  }
190  // Set next-server
191  result->set("next-server", Element::create(next_server_.toText()));
192  // Set server-hostname
193  result->set("server-hostname", Element::create(sname_));
194  // Set boot-file-name
195  result->set("boot-file-name", Element::create(filename_));
196 
197  // Set valid-lifetime
198  if (!valid_.unspecified()) {
199  result->set("valid-lifetime",
200  Element::create(static_cast<long long>(valid_.get())));
201 
202  if (valid_.getMin() < valid_.get()) {
203  result->set("min-valid-lifetime",
204  Element::create(static_cast<long long>(valid_.getMin())));
205  }
206 
207  if (valid_.getMax() > valid_.get()) {
208  result->set("max-valid-lifetime",
209  Element::create(static_cast<long long>(valid_.getMax())));
210  }
211  }
212 
213  return (result);
214 }
215 
216 std::ostream& operator<<(std::ostream& os, const ClientClassDef& x) {
217  os << "ClientClassDef:" << x.getName();
218  return (os);
219 }
220 
221 //********** ClientClassDictionary ******************//
222 
224  : map_(new ClientClassDefMap()), list_(new ClientClassDefList()) {
225 }
226 
228  : map_(new ClientClassDefMap()), list_(new ClientClassDefList()) {
229  BOOST_FOREACH(ClientClassDefPtr cclass, *(rhs.list_)) {
230  ClientClassDefPtr copy(new ClientClassDef(*cclass));
231  addClass(copy);
232  }
233 }
234 
236 }
237 
238 void
239 ClientClassDictionary::addClass(const std::string& name,
240  const ExpressionPtr& match_expr,
241  const std::string& test,
242  bool required,
243  bool depend_on_known,
244  const CfgOptionPtr& cfg_option,
245  CfgOptionDefPtr cfg_option_def,
246  ConstElementPtr user_context,
247  asiolink::IOAddress next_server,
248  const std::string& sname,
249  const std::string& filename,
250  const Triplet<uint32_t>& valid) {
251  ClientClassDefPtr cclass(new ClientClassDef(name, match_expr, cfg_option));
252  cclass->setTest(test);
253  cclass->setRequired(required);
254  cclass->setDependOnKnown(depend_on_known);
255  cclass->setCfgOptionDef(cfg_option_def);
256  cclass->setContext(user_context),
257  cclass->setNextServer(next_server);
258  cclass->setSname(sname);
259  cclass->setFilename(filename);
260  cclass->setValid(valid);
261  addClass(cclass);
262 }
263 
264 void
266  if (!class_def) {
267  isc_throw(BadValue, "ClientClassDictionary::addClass "
268  " - class definition cannot be null");
269  }
270 
271  if (findClass(class_def->getName())) {
272  isc_throw(DuplicateClientClassDef, "Client Class: "
273  << class_def->getName() << " has already been defined");
274  }
275 
276  list_->push_back(class_def);
277  (*map_)[class_def->getName()] = class_def;
278 }
279 
281 ClientClassDictionary::findClass(const std::string& name) const {
282  ClientClassDefMap::iterator it = map_->find(name);
283  if (it != map_->end()) {
284  return (*it).second;
285  }
286 
287  return (ClientClassDefPtr());
288 }
289 
290 void
291 ClientClassDictionary::removeClass(const std::string& name) {
292  for (ClientClassDefList::iterator this_class = list_->begin();
293  this_class != list_->end(); ++this_class) {
294  if ((*this_class)->getName() == name) {
295  list_->erase(this_class);
296  break;
297  }
298  }
299  map_->erase(name);
300 }
301 
304  return (list_);
305 }
306 
307 bool
308 ClientClassDictionary::dependOnClass(const std::string& name,
309  std::string& dependent_class) const {
310  // Skip previous classes as they should not depend on name.
311  bool found = false;
312  for (ClientClassDefList::iterator this_class = list_->begin();
313  this_class != list_->end(); ++this_class) {
314  if (found) {
315  if ((*this_class)->dependOnClass(name)) {
316  dependent_class = (*this_class)->getName();
317  return (true);
318  }
319  } else {
320  if ((*this_class)->getName() == name) {
321  found = true;
322  }
323  }
324  }
325  return (false);
326 }
327 
328 bool
330  if (list_->size() != other.list_->size()) {
331  return (false);
332  }
333 
334  ClientClassDefList::const_iterator this_class = list_->cbegin();
335  ClientClassDefList::const_iterator other_class = other.list_->cbegin();
336  while (this_class != list_->cend() &&
337  other_class != other.list_->cend()) {
338  if (!*this_class || !*other_class ||
339  **this_class != **other_class) {
340  return false;
341  }
342 
343  ++this_class;
344  ++other_class;
345  }
346 
347  return (true);
348 }
349 
352  ElementPtr result = Element::createList();
353  // Iterate on the map
354  for (ClientClassDefList::const_iterator this_class = list_->begin();
355  this_class != list_->cend(); ++this_class) {
356  result->add((*this_class)->toElement());
357  }
358  return (result);
359 }
360 
361 std::list<std::string>
363  // DROP is not in this list because it is special but not built-in.
364  // In fact DROP is set from an expression as callouts can drop
365  // directly the incoming packet. The expression must not depend on
366  // KNOWN/UNKNOWN which are set far after the drop point.
367  "ALL", "KNOWN", "UNKNOWN", "BOOTP"
368 };
369 
370 std::list<std::string>
372  "VENDOR_CLASS_", "HA_", "AFTER_", "EXTERNAL_"
373 };
374 
375 bool
376 isClientClassBuiltIn(const ClientClass& client_class) {
377  for (std::list<std::string>::const_iterator bn = builtinNames.cbegin();
378  bn != builtinNames.cend(); ++bn) {
379  if (client_class == *bn) {
380  return true;
381  }
382  }
383 
384  for (std::list<std::string>::const_iterator bt = builtinPrefixes.cbegin();
385  bt != builtinPrefixes.cend(); ++bt) {
386  if (client_class.size() <= bt->size()) {
387  continue;
388  }
389  auto mis = std::mismatch(bt->cbegin(), bt->cend(), client_class.cbegin());
390  if (mis.first == bt->cend()) {
391  return true;
392  }
393  }
394 
395  return false;
396 }
397 
398 bool
400  bool& depend_on_known,
401  const ClientClass& client_class) {
402  // First check built-in classes
403  if (isClientClassBuiltIn(client_class)) {
404  // Check direct dependency on [UN]KNOWN
405  if ((client_class == "KNOWN") || (client_class == "UNKNOWN")) {
406  depend_on_known = true;
407  }
408  return (true);
409  }
410 
411  // Second check already defined, i.e. in the dictionary
412  ClientClassDefPtr def = class_dictionary->findClass(client_class);
413  if (def) {
414  // Check indirect dependency on [UN]KNOWN
415  if (def->getDependOnKnown()) {
416  depend_on_known = true;
417  }
418  return (true);
419  }
420 
421  // Not defined...
422  return (false);
423 }
424 
425 } // namespace isc::dhcp
426 } // namespace isc
void setMatchExpr(const ExpressionPtr &match_expr)
Sets the class's match expression.
const ExpressionPtr & getMatchExpr() const
Fetches the class's match expression.
ClientClassDefPtr findClass(const std::string &name) const
Fetches the class definition for a given class name.
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
Definition: optional.h:121
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition: cfg_option.h:706
void setDependOnKnown(bool depend_on_known)
Sets the depend on known flag aka use host flag.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
void setRequired(bool required)
Sets the only if required flag.
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition: cfgmgr.cc:25
std::string getTest() const
Fetches the class's original match expression.
std::unordered_map< std::string, ClientClassDefPtr > ClientClassDefMap
Defines a map of ClientClassDef's, keyed by the class name.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
void addClass(const std::string &name, const ExpressionPtr &match_expr, const std::string &test, bool required, bool depend_on_known, const CfgOptionPtr &options, CfgOptionDefPtr defs=CfgOptionDefPtr(), isc::data::ConstElementPtr user_context=isc::data::ConstElementPtr(), asiolink::IOAddress next_server=asiolink::IOAddress("0.0.0.0"), const std::string &sname=std::string(), const std::string &filename=std::string(), const Triplet< uint32_t > &valid=Triplet< uint32_t >())
Adds a new class to the list.
bool equals(const ClientClassDictionary &other) const
Compares two ClientClassDictionary objects for equality.
bool getDependOnKnown() const
Fetches the depend on known flag aka use host flag.
const CfgOptionDefPtr & getCfgOptionDef() const
Fetches the class's option definitions.
boost::shared_ptr< CfgOptionDef > CfgOptionDefPtr
Non-const pointer.
bool equals(const ClientClassDef &other) const
Compares two ClientClassDef objects for equality.
void setTest(const std::string &test)
Sets the class's original match expression.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15
Maintains a list of ClientClassDef's.
bool getRequired() const
Fetches the only if required flag.
bool isClientClassDefined(ClientClassDictionaryPtr &class_dictionary, bool &depend_on_known, const ClientClass &client_class)
Check if a client class name is already defined, i.e.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Represents option data configuration for the DHCP server.
Definition: cfg_option.h:314
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1097
T getMax() const
Returns a maximum allowed value.
Definition: triplet.h:112
Error that occurs when an attempt is made to add a duplicate class to a class dictionary.
const CfgOptionPtr & getCfgOption() const
Fetches the class's option collection.
boost::shared_ptr< ClientClassDictionary > ClientClassDictionaryPtr
Defines a pointer to a ClientClassDictionary.
Embodies a single client class definition.
uint16_t getFamily() const
Returns address family.
Definition: cfgmgr.h:280
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
bool dependOnClass(const std::string &name, std::string &dependent_class) const
Checks direct dependency.
bool dependOnClass(const TokenPtr &token, const std::string &name)
Checks dependency on a token.
Definition: dependency.cc:15
std::list< std::string > builtinNames
List of built-in client class names.
std::list< std::string > builtinPrefixes
List of built-in client class prefixes i.e.
boost::shared_ptr< ClientClassDef > ClientClassDefPtr
a pointer to an ClientClassDef
std::vector< ClientClassDefPtr > ClientClassDefList
Defines a list of ClientClassDefPtr's, using insert order.
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::vector< TokenPtr > Expression
This is a structure that holds an expression converted to RPN.
Definition: token.h:28
T get(T hint) const
Returns value with a hint.
Definition: triplet.h:99
virtual ~ClientClassDef()
Destructor.
T getMin() const
Returns a minimum allowed value.
Definition: triplet.h:85
boost::shared_ptr< ClientClassDefList > ClientClassDefListPtr
Defines a pointer to a ClientClassDefList.
const Name & name_
Definition: dns/message.cc:693
void removeClass(const std::string &name)
Removes a given class definition from the dictionary.
std::ostream & operator<<(std::ostream &os, const OpaqueDataTuple &tuple)
Inserts the OpaqueDataTuple as a string into stream.
const ClientClassDefListPtr & getClasses() const
Fetches the dictionary's list of classes.
bool dependOnClass(const std::string &name) const
Checks direct dependency.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
void setName(const std::string &name)
Sets the class's name.
void setCfgOption(const CfgOptionPtr &cfg_option)
Sets the class's option collection.
std::string getName() const
Fetches the class's name.
ClientClassDef(const std::string &name, const ExpressionPtr &match_expr, const CfgOptionPtr &options=CfgOptionPtr())
Constructor.
bool isClientClassBuiltIn(const ClientClass &client_class)
Check if a client class name is builtin.
std::string ClientClass
Defines a single class name.
Definition: classify.h:37
Defines classes for storing client class definitions.
boost::shared_ptr< Expression > ExpressionPtr
Definition: token.h:30
void setCfgOptionDef(const CfgOptionDefPtr &cfg_option_def)
Sets the class's option definition collection.