Kea  2.3.9
client_class_def.h
Go to the documentation of this file.
1 // Copyright (C) 2015-2023 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 CLIENT_CLASS_DEF_H
8 #define CLIENT_CLASS_DEF_H
9 
10 #include <cc/cfg_to_element.h>
11 #include <cc/stamped_element.h>
12 #include <cc/user_context.h>
13 #include <dhcpsrv/cfg_option.h>
14 #include <dhcpsrv/cfg_option_def.h>
15 #include <eval/token.h>
16 #include <exceptions/exceptions.h>
17 #include <util/triplet.h>
18 #include <util/optional.h>
19 
20 #include <string>
21 #include <unordered_map>
22 #include <list>
23 #include <vector>
24 
37 namespace isc {
38 namespace dhcp {
39 
43 public:
44  DuplicateClientClassDef(const char* file, size_t line, const char* what)
45  : isc::Exception(file, line, what) {}
46 };
47 
50  public data::CfgToElement,
51  public data::StampedElement {
52 public:
58  ClientClassDef(const std::string& name, const ExpressionPtr& match_expr,
59  const CfgOptionPtr& options = CfgOptionPtr());
60 
62  ClientClassDef(const ClientClassDef& rhs);
63 
65  virtual ~ClientClassDef();
66 
68  std::string getName() const;
69 
73  void setName(const std::string& name);
74 
76  const ExpressionPtr& getMatchExpr() const;
77 
81  void setMatchExpr(const ExpressionPtr& match_expr);
82 
84  std::string getTest() const;
85 
89  void setTest(const std::string& test);
90 
92  bool getRequired() const;
93 
97  void setRequired(bool required);
98 
100  bool getDependOnKnown() const;
101 
105  void setDependOnKnown(bool depend_on_known);
106 
108  const CfgOptionDefPtr& getCfgOptionDef() const;
109 
113  void setCfgOptionDef(const CfgOptionDefPtr& cfg_option_def);
114 
116  const CfgOptionPtr& getCfgOption() const;
117 
121  void setCfgOption(const CfgOptionPtr& cfg_option);
122 
128  bool dependOnClass(const std::string& name) const;
129 
135  bool equals(const ClientClassDef& other) const;
136 
142  bool operator==(const ClientClassDef& other) const {
143  return (equals(other));
144  }
145 
151  bool operator!=(const ClientClassDef& other) const {
152  return (!(equals(other)));
153  }
154 
156  friend std::ostream& operator<<(std::ostream& os, const ClientClassDef& x);
157 
161  return (next_server_);
162  }
163 
167  void setNextServer(const asiolink::IOAddress& addr) {
168  next_server_ = addr;
169  }
170 
174  void setSname(const std::string& sname) {
175  sname_ = sname;
176  }
177 
180  const std::string& getSname() const {
181  return (sname_);
182  }
183 
187  void setFilename(const std::string& filename) {
188  filename_ = filename;
189  }
190 
193  const std::string& getFilename() const {
194  return (filename_);
195  }
196 
201  return (valid_);
202  }
203 
207  void setValid(const util::Triplet<uint32_t>& valid) {
208  valid_ = valid;
209  }
210 
215  return (preferred_);
216  }
217 
221  void setPreferred(const util::Triplet<uint32_t>& preferred) {
222  preferred_ = preferred;
223  }
224 
228  void setOfferLft(const util::Optional<uint32_t>& offer_lft) {
229  offer_lft_ = offer_lft;
230  }
231 
236  return (offer_lft_);
237  }
238 
244  virtual void test(PktPtr pkt, const ExpressionPtr& expr_ptr);
245 
249  virtual isc::data::ElementPtr toElement() const;
250 
251 private:
253  std::string name_;
254 
257  ExpressionPtr match_expr_;
258 
261  std::string test_;
262 
267  bool required_;
268 
277  bool depend_on_known_;
278 
280  CfgOptionDefPtr cfg_option_def_;
281 
283  CfgOptionPtr cfg_option_;
284 
288  asiolink::IOAddress next_server_;
289 
294  std::string sname_;
295 
300  std::string filename_;
301 
304 
306  util::Triplet<uint32_t> preferred_;
307 
309  util::Optional<uint32_t> offer_lft_;
310 };
311 
313 public:
319  TemplateClientClassDef(const std::string& name, const ExpressionPtr& match_expr,
320  const CfgOptionPtr& options = CfgOptionPtr());
321 
327  virtual void test(PktPtr pkt, const ExpressionPtr& expr_ptr) override;
328 
332  virtual isc::data::ElementPtr toElement() const override;
333 
341  static const std::string SPAWN_CLASS_PREFIX;
342 };
343 
345 typedef boost::shared_ptr<ClientClassDef> ClientClassDefPtr;
346 
348 typedef std::unordered_map<std::string, ClientClassDefPtr> ClientClassDefMap;
349 
351 typedef boost::shared_ptr<ClientClassDefMap> ClientClassDefMapPtr;
352 
354 typedef std::vector<ClientClassDefPtr> ClientClassDefList;
355 
357 typedef boost::shared_ptr<ClientClassDefList> ClientClassDefListPtr;
358 
361 
362 public:
365 
367 
370 
390  void addClass(const std::string& name, const ExpressionPtr& match_expr,
391  const std::string& test, bool required, bool depend_on_known,
392  const CfgOptionPtr& options,
395  asiolink::IOAddress next_server = asiolink::IOAddress("0.0.0.0"),
396  const std::string& sname = std::string(),
397  const std::string& filename = std::string(),
400  bool is_template = false,
402 
409  void addClass(ClientClassDefPtr& class_def);
410 
417  ClientClassDefPtr findClass(const std::string& name) const;
418 
425  void removeClass(const std::string& name);
426 
430  void removeClass(const uint64_t id);
431 
435  const ClientClassDefListPtr& getClasses() const;
436 
440  bool empty() const;
441 
449  bool dependOnClass(const std::string& name, std::string& dependent_class) const;
450 
456  bool equals(const ClientClassDictionary& other) const;
457 
462  void initMatchExpr(uint16_t family);
463 
468  void createOptions(const CfgOptionDefPtr& cfg_option_def);
469 
475  bool operator==(const ClientClassDictionary& other) const {
476  return (equals(other));
477  }
478 
484  bool operator!=(const ClientClassDictionary& other) const {
485  return (!equals(other));
486  }
487 
493 
497  virtual isc::data::ElementPtr toElement() const;
498 
499 private:
500 
503 
505  ClientClassDefListPtr list_;
506 };
507 
509 typedef boost::shared_ptr<ClientClassDictionary> ClientClassDictionaryPtr;
510 
513 extern std::list<std::string> builtinNames;
514 
517 extern std::list<std::string> builtinPrefixes;
518 
523 bool isClientClassBuiltIn(const ClientClass& client_class);
524 
536 bool isClientClassDefined(ClientClassDictionaryPtr& class_dictionary,
537  bool& depend_on_known,
538  const ClientClass& client_class);
539 
540 } // namespace isc::dhcp
541 } // namespace isc
542 
543 #endif // CLIENT_CLASS_DEF_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
This class represents configuration element which is associated with database identifier,...
Embodies a single client class definition.
bool equals(const ClientClassDef &other) const
Compares two ClientClassDef objects for equality.
bool getRequired() const
Fetches the only if required flag.
const std::string & getSname() const
returns server-hostname value
void setCfgOption(const CfgOptionPtr &cfg_option)
Sets the class's option collection.
void setName(const std::string &name)
Sets the class's name.
bool dependOnClass(const std::string &name) const
Checks direct dependency.
bool getDependOnKnown() const
Fetches the depend on known flag aka use host flag.
void setPreferred(const util::Triplet< uint32_t > &preferred)
Sets new preferred lifetime.
void setFilename(const std::string &filename)
sets the boot-file-name value
void setRequired(bool required)
Sets the only if required flag.
virtual void test(PktPtr pkt, const ExpressionPtr &expr_ptr)
Test method which checks if the packet belongs to the class.
void setNextServer(const asiolink::IOAddress &addr)
sets the next-server value
const CfgOptionDefPtr & getCfgOptionDef() const
Fetches the class's option definitions.
ClientClassDef(const std::string &name, const ExpressionPtr &match_expr, const CfgOptionPtr &options=CfgOptionPtr())
Constructor.
const ExpressionPtr & getMatchExpr() const
Fetches the class's match expression.
void setCfgOptionDef(const CfgOptionDefPtr &cfg_option_def)
Sets the class's option definition collection.
virtual ~ClientClassDef()
Destructor.
const std::string & getFilename() const
returns boot-file-name value
void setSname(const std::string &sname)
sets the server-name value
util::Triplet< uint32_t > getValid() const
Return valid-lifetime value.
bool operator==(const ClientClassDef &other) const
Equality operator.
void setOfferLft(const util::Optional< uint32_t > &offer_lft)
Sets offer lifetime for the class.
std::string getName() const
Fetches the class's name.
bool operator!=(const ClientClassDef &other) const
Inequality operator.
const CfgOptionPtr & getCfgOption() const
Fetches the class's option collection.
util::Optional< uint32_t > getOfferLft() const
Returns offer lifetime for the class.
void setMatchExpr(const ExpressionPtr &match_expr)
Sets the class's match expression.
friend std::ostream & operator<<(std::ostream &os, const ClientClassDef &x)
Provides a convenient text representation of the class.
void setTest(const std::string &test)
Sets the class's original match expression.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
util::Triplet< uint32_t > getPreferred() const
Return preferred-lifetime value.
std::string getTest() const
Fetches the class's original match expression.
void setDependOnKnown(bool depend_on_known)
Sets the depend on known flag aka use host flag.
void setValid(const util::Triplet< uint32_t > &valid)
Sets new valid lifetime.
const asiolink::IOAddress & getNextServer() const
returns next-server value
Maintains a list of ClientClassDef's.
ClientClassDefPtr findClass(const std::string &name) const
Fetches the class definition for a given class name.
bool equals(const ClientClassDictionary &other) const
Compares two ClientClassDictionary objects for equality.
void removeClass(const std::string &name)
Removes a given class definition from the dictionary.
bool operator!=(const ClientClassDictionary &other) const
Inequality operator.
bool empty() const
Checks if the class dictionary is empty.
bool dependOnClass(const std::string &name, std::string &dependent_class) const
Checks direct dependency.
const ClientClassDefListPtr & getClasses() const
Fetches the dictionary's list of classes.
ClientClassDictionary & operator=(const ClientClassDictionary &rhs)
Copy assignment operator.
void createOptions(const CfgOptionDefPtr &cfg_option_def)
Iterates over the classes in the dictionary and recreates the options.
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 util::Triplet< uint32_t > &valid=util::Triplet< uint32_t >(), const util::Triplet< uint32_t > &preferred=util::Triplet< uint32_t >(), bool is_template=false, const util::Optional< uint32_t > &offer_lft=util::Optional< uint32_t >())
Adds a new class to the list.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
bool operator==(const ClientClassDictionary &other) const
Equality operator.
void initMatchExpr(uint16_t family)
Iterates over the classes in the dictionary and ensures that that match expressions are initialized.
Error that occurs when an attempt is made to add a duplicate class to a class dictionary.
DuplicateClientClassDef(const char *file, size_t line, const char *what)
static const std::string SPAWN_CLASS_PREFIX
This is a prefix added to the spawned class name.
TemplateClientClassDef(const std::string &name, const ExpressionPtr &match_expr, const CfgOptionPtr &options=CfgOptionPtr())
Constructor.
virtual void test(PktPtr pkt, const ExpressionPtr &expr_ptr) override
Test method which checks if the packet belongs to the class.
virtual isc::data::ElementPtr toElement() const override
Unparse a configuration object.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:26
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition: pkt.h:859
std::string ClientClass
Defines a single class name.
Definition: classify.h:42
std::vector< ClientClassDefPtr > ClientClassDefList
Defines a list of ClientClassDefPtr's, using insert order.
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition: cfg_option.h:780
boost::shared_ptr< CfgOptionDef > CfgOptionDefPtr
Non-const pointer.
boost::shared_ptr< ClientClassDef > ClientClassDefPtr
a pointer to an ClientClassDef
std::list< std::string > builtinPrefixes
The prefixes used to check if a class is BuiltIn class.
boost::shared_ptr< ClientClassDefMap > ClientClassDefMapPtr
Defines a pointer to a ClientClassDefMap.
boost::shared_ptr< Expression > ExpressionPtr
Definition: token.h:30
boost::shared_ptr< ClientClassDictionary > ClientClassDictionaryPtr
Defines a pointer to a ClientClassDictionary.
boost::shared_ptr< ClientClassDefList > ClientClassDefListPtr
Defines a pointer to a ClientClassDefList.
bool isClientClassDefined(ClientClassDictionaryPtr &class_dictionary, bool &depend_on_known, const ClientClass &client_class)
Check if a client class name is already defined, i.e.
bool isClientClassBuiltIn(const ClientClass &client_class)
Check if a client class name is builtin.
std::list< std::string > builtinNames
List of classes for which test expressions cannot be defined.
std::unordered_map< std::string, ClientClassDefPtr > ClientClassDefMap
Defines a map of ClientClassDef's, keyed by the class name.
Defines the logger used by the top-level component of kea-lfc.
Abstract class for configuration Cfg_* classes.
Base class for user context.
Definition: user_context.h:22