Kea 2.7.5
client_class_def.h
Go to the documentation of this file.
1// Copyright (C) 2015-2024 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>
15#include <eval/token.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
37namespace isc {
38namespace dhcp {
39
43public:
44 DuplicateClientClassDef(const char* file, size_t line, const char* what)
45 : isc::Exception(file, line, what) {}
46};
47
50 public data::CfgToElement,
52public:
58 ClientClassDef(const std::string& name, const ExpressionPtr& match_expr,
59 const CfgOptionPtr& options = CfgOptionPtr());
60
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 getAdditional() const;
93
97 void setAdditional(bool additional);
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
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
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
245 virtual void test(PktPtr pkt, const ExpressionPtr& expr_ptr);
246
250 virtual isc::data::ElementPtr toElement() const;
251
252private:
254 std::string name_;
255
258 ExpressionPtr match_expr_;
259
262 std::string test_;
263
268 bool additional_;
269
278 bool depend_on_known_;
279
281 CfgOptionDefPtr cfg_option_def_;
282
284 CfgOptionPtr cfg_option_;
285
289 asiolink::IOAddress next_server_;
290
295 std::string sname_;
296
301 std::string filename_;
302
305
307 util::Triplet<uint32_t> preferred_;
308
310 util::Optional<uint32_t> offer_lft_;
311};
312
314public:
320 TemplateClientClassDef(const std::string& name, const ExpressionPtr& match_expr,
321 const CfgOptionPtr& options = CfgOptionPtr());
322
329 virtual void test(PktPtr pkt, const ExpressionPtr& expr_ptr) override;
330
334 virtual isc::data::ElementPtr toElement() const override;
335
343 static const std::string SPAWN_CLASS_PREFIX;
344};
345
347typedef boost::shared_ptr<ClientClassDef> ClientClassDefPtr;
348
350typedef std::unordered_map<std::string, ClientClassDefPtr> ClientClassDefMap;
351
353typedef boost::shared_ptr<ClientClassDefMap> ClientClassDefMapPtr;
354
356typedef std::vector<ClientClassDefPtr> ClientClassDefList;
357
359typedef boost::shared_ptr<ClientClassDefList> ClientClassDefListPtr;
360
363
364public:
367
369
372
394 void addClass(const std::string& name, const ExpressionPtr& match_expr,
395 const std::string& test, bool additional, bool depend_on_known,
396 const CfgOptionPtr& options,
399 asiolink::IOAddress next_server = asiolink::IOAddress("0.0.0.0"),
400 const std::string& sname = std::string(),
401 const std::string& filename = std::string(),
404 bool is_template = false,
406
413 void addClass(const ClientClassDefPtr& class_def);
414
421 ClientClassDefPtr findClass(const std::string& name) const;
422
429 void removeClass(const std::string& name);
430
434 void removeClass(const uint64_t id);
435
439 const ClientClassDefListPtr& getClasses() const;
440
444 bool empty() const;
445
453 bool dependOnClass(const std::string& name, std::string& dependent_class) const;
454
460 bool equals(const ClientClassDictionary& other) const;
461
466 void initMatchExpr(uint16_t family);
467
472 void createOptions(const CfgOptionDefPtr& cfg_option_def);
473
476 void encapsulateOptions() const;
477
483 bool operator==(const ClientClassDictionary& other) const {
484 return (equals(other));
485 }
486
492 bool operator!=(const ClientClassDictionary& other) const {
493 return (!equals(other));
494 }
495
501
505 virtual isc::data::ElementPtr toElement() const;
506
507private:
508
511
514};
515
517typedef boost::shared_ptr<ClientClassDictionary> ClientClassDictionaryPtr;
518
521extern std::list<std::string> builtinNames;
522
525extern std::list<std::string> builtinPrefixes;
526
531bool isClientClassBuiltIn(const ClientClass& client_class);
532
545 bool& depend_on_known,
546 const ClientClass& client_class);
547
548} // namespace isc::dhcp
549} // namespace isc
550
551#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.
util::Optional< uint32_t > getOfferLft() const
Returns offer lifetime for the class.
void setAdditional(bool additional)
Sets the only if additional flag.
bool equals(const ClientClassDef &other) const
Compares two ClientClassDef objects for equality.
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
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 & getSname() const
returns server-hostname value
const asiolink::IOAddress & getNextServer() const
returns next-server value
void setSname(const std::string &sname)
sets the server-name 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.
friend std::ostream & operator<<(std::ostream &os, const ClientClassDef &x)
Provides a convenient text representation of the class.
bool operator!=(const ClientClassDef &other) const
Inequality operator.
const CfgOptionPtr & getCfgOption() const
Fetches the class's option collection.
util::Triplet< uint32_t > getValid() const
Return valid-lifetime value.
void setMatchExpr(const ExpressionPtr &match_expr)
Sets the class's match expression.
void setTest(const std::string &test)
Sets the class's original match expression.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
std::string getTest() const
Fetches the class's original match expression.
bool getAdditional() const
Fetches the only if additional flag.
void setDependOnKnown(bool depend_on_known)
Sets the depend on known flag aka use host flag.
util::Triplet< uint32_t > getPreferred() const
Return preferred-lifetime value.
const std::string & getFilename() const
returns boot-file-name value
void setValid(const util::Triplet< uint32_t > &valid)
Sets new valid lifetime.
Maintains a list of ClientClassDef's.
void addClass(const std::string &name, const ExpressionPtr &match_expr, const std::string &test, bool additional, 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.
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 encapsulateOptions() const
Iterates over the classes in the dictionary and encapsulates suboptions.
void createOptions(const CfgOptionDefPtr &cfg_option_def)
Iterates over the classes in the dictionary and recreates the options.
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.
A template representing an optional value.
Definition optional.h:36
This template specifies a parameter value.
Definition triplet.h:37
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition pkt.h:998
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:832
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:31
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.