Kea: cfg_attribute.h Source File

| Kea 3.2.0-git

cfg_attribute.h
Go to the documentation of this file.
1// Copyright (C) 2020-2025 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 CFG_ATTRIBUTE_H
8#define CFG_ATTRIBUTE_H
9
10#include <client_attribute.h>
11#include <exceptions/exceptions.h>
12#include <cc/data.h>
13#include <cc/cfg_to_element.h>
14#include <dhcp/pkt.h>
15#include <eval/token.h>
16#include <util/buffer.h>
17#include <boost/multi_index_container.hpp>
18#include <boost/multi_index/hashed_index.hpp>
19#include <boost/multi_index/mem_fun.hpp>
20#include <boost/multi_index/composite_key.hpp>
21#include <boost/shared_ptr.hpp>
22#include <vector>
23#include <map>
24
25namespace isc {
26namespace radius {
27
38 class CfgAttributes : public data::CfgToElement {
39public:
40
42 CfgAttributes() : container_() {
43 }
44
45 // @brief Destructor.
46 virtual ~CfgAttributes() {
47 clear();
48 }
49
53 bool empty() const {
54 return (container_.empty());
55 }
56
60 size_t size() const {
61 return (container_.size());
62 }
63
70 void add(const AttrDefPtr& def,
71 const ConstAttributePtr& attr,
72 const dhcp::ExpressionPtr& expr,
73 const std::string& test);
74
79 void add(const AttrDefPtr& def, const ConstAttributePtr& attr) {
80 add(def, attr, dhcp::ExpressionPtr(), "");
81 }
82
90 bool del(const uint8_t type, const uint32_t vendor = 0);
91
93 void clear() {
94 container_.clear();
95 }
96
103 size_t count(const uint8_t type, const uint32_t vendor = 0) const {
104 return (container_.count(boost::make_tuple(vendor, type)));
105 }
106
112 AttrDefPtr getDef(const uint8_t type, const uint32_t vendor = 0) const;
113
119 ConstAttributePtr get(const uint8_t type, const uint32_t vendor = 0) const;
120
126 dhcp::ExpressionPtr getExpr(const uint8_t type,
127 const uint32_t vendor = 0) const;
128
134 std::string getTest(const uint8_t type, const uint32_t vendor = 0) const;
135
139 Attributes getAll() const;
140
149 Attributes getEvalAll(dhcp::Pkt& pkt);
150
154 data::ElementPtr toElement() const override;
155
156protected:
158 class AttributeValue {
159 public:
166 AttributeValue(const AttrDefPtr& def,
167 const ConstAttributePtr& attr,
168 const dhcp::ExpressionPtr& expr,
169 const std::string& test)
170 : def_(def), attr_(attr), expr_(expr), test_(test) {
171 if (!def) {
172 isc_throw(BadValue, "no attribute definition");
173 }
174 if (expr || !test.empty()) {
175 if (attr) {
176 isc_throw(BadValue, "once of attribute or test");
177 }
178 if (!expr || test.empty()) {
179 isc_throw(BadValue, "missing expression or test");
180 }
181 } else if (!attr) {
182 isc_throw(BadValue, "no attribute and test");
183 }
184 }
185
187 AttrDefPtr def_;
188
190 ConstAttributePtr attr_;
191
193 dhcp::ExpressionPtr expr_;
194
196 std::string test_;
197
199 uint8_t getType() const {
200 if (!def_) {
201 isc_throw(BadValue, "no attribute definition");
202 }
203 return (def_->type_);
204 }
205
207 uint32_t getVendor() const {
208 if (!def_) {
209 isc_throw(BadValue, "no attribute definition");
210 }
211 return (def_->vendor_);
212 }
213 };
214
216 boost::multi_index_container<
217 // This container stores AttributeValue objects.
218 AttributeValue,
219 // Start specification of indexes here.
220 boost::multi_index::indexed_by<
221 // Hash index for by vendor and type.
222 boost::multi_index::hashed_non_unique<
223 boost::multi_index::composite_key<
224 AttributeValue,
225 boost::multi_index::const_mem_fun<
226 AttributeValue, uint32_t, &AttributeValue::getVendor
227 >,
228 boost::multi_index::const_mem_fun<
229 AttributeValue, uint8_t, &AttributeValue::getType
230 >
231 >
232 >
233 >
234 > container_;
235};
236
237} // end of namespace isc::radius
238} // end of namespace isc
239
240#endif
buffer.h
cfg_to_element.h
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::dhcp::Pkt
Base class for classes representing DHCP messages.
Definition pkt.h:161
isc::radius::Attributes
Collection of attributes.
Definition client_attribute.h:819
isc::radius::CfgAttributes::AttributeValue::def_
AttrDefPtr def_
Pointer to the attribute definition (required).
Definition cfg_attribute.h:187
isc::radius::CfgAttributes::AttributeValue::attr_
ConstAttributePtr attr_
Pointer to the attribute.
Definition cfg_attribute.h:190
isc::radius::CfgAttributes::AttributeValue::getType
uint8_t getType() const
Return the type.
Definition cfg_attribute.h:199
isc::radius::CfgAttributes::AttributeValue::test_
std::string test_
Original expression.
Definition cfg_attribute.h:196
isc::radius::CfgAttributes::AttributeValue::expr_
dhcp::ExpressionPtr expr_
Expression to compute the value.
Definition cfg_attribute.h:193
isc::radius::CfgAttributes::AttributeValue::getVendor
uint32_t getVendor() const
Return the vendor.
Definition cfg_attribute.h:207
isc::radius::CfgAttributes::AttributeValue::AttributeValue
AttributeValue(const AttrDefPtr &def, const ConstAttributePtr &attr, const dhcp::ExpressionPtr &expr, const std::string &test)
Constructor.
Definition cfg_attribute.h:166
isc::radius::CfgAttributes::get
ConstAttributePtr get(const uint8_t type, const uint32_t vendor=0) const
Get instance of the attribute in the configuration.
Definition cfg_attribute.cc:54
isc::radius::CfgAttributes::getExpr
dhcp::ExpressionPtr getExpr(const uint8_t type, const uint32_t vendor=0) const
Returns the expression of an attribute.
Definition cfg_attribute.cc:63
isc::radius::CfgAttributes::getAll
Attributes getAll() const
Get all attributes in the configuration.
Definition cfg_attribute.cc:81
isc::radius::CfgAttributes::CfgAttributes
CfgAttributes()
Constructor.
Definition cfg_attribute.h:42
isc::radius::CfgAttributes::add
void add(const AttrDefPtr &def, const ConstAttributePtr &attr, const dhcp::ExpressionPtr &expr, const std::string &test)
Adds instance of the attribute to the configuration.
Definition cfg_attribute.cc:24
isc::radius::CfgAttributes::size
size_t size() const
Returns the number of elements.
Definition cfg_attribute.h:60
isc::radius::CfgAttributes::count
size_t count(const uint8_t type, const uint32_t vendor=0) const
Counts instance of the attribute in the configuration.
Definition cfg_attribute.h:103
isc::radius::CfgAttributes::getDef
AttrDefPtr getDef(const uint8_t type, const uint32_t vendor=0) const
Returns the definition of an attribute.
Definition cfg_attribute.cc:45
isc::radius::CfgAttributes::empty
bool empty() const
Checks if the object is empty.
Definition cfg_attribute.h:53
isc::radius::CfgAttributes::del
bool del(const uint8_t type, const uint32_t vendor=0)
Deletes an attribute from the configuration.
Definition cfg_attribute.cc:35
isc::radius::CfgAttributes::add
void add(const AttrDefPtr &def, const ConstAttributePtr &attr)
Adds instance of the attribute to the configuration.
Definition cfg_attribute.h:79
isc::radius::CfgAttributes::getTest
std::string getTest(const uint8_t type, const uint32_t vendor=0) const
Returns the text expression of an attribute.
Definition cfg_attribute.cc:72
isc::radius::CfgAttributes::~CfgAttributes
virtual ~CfgAttributes()
Definition cfg_attribute.h:46
isc::radius::CfgAttributes::container_
boost::multi_index_container< AttributeValue, boost::multi_index::indexed_by< boost::multi_index::hashed_non_unique< boost::multi_index::composite_key< AttributeValue, boost::multi_index::const_mem_fun< AttributeValue, uint32_t, &AttributeValue::getVendor >, boost::multi_index::const_mem_fun< AttributeValue, uint8_t, &AttributeValue::getType > > > > > container_
The container.
Definition cfg_attribute.h:234
isc::radius::CfgAttributes::clear
void clear()
Clear the container.
Definition cfg_attribute.h:93
isc::radius::CfgAttributes::getEvalAll
Attributes getEvalAll(dhcp::Pkt &pkt)
Get all evaluated attributes in the configuration.
Definition cfg_attribute.cc:90
isc::radius::CfgAttributes::toElement
data::ElementPtr toElement() const override
Unparse configuration.
Definition cfg_attribute.cc:116
client_attribute.h
if
if(!(yy_init))
Definition d2_lexer.cc:1515
data.h
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::dhcp::ExpressionPtr
boost::shared_ptr< Expression > ExpressionPtr
Definition token.h:31
isc::radius
Definition cfg_attribute.cc:21
isc::radius::ConstAttributePtr
boost::shared_ptr< const Attribute > ConstAttributePtr
Definition client_attribute.h:40
isc::radius::AttrDefPtr
boost::shared_ptr< AttrDef > AttrDefPtr
Shared pointers to Attribute definition.
Definition client_dictionary.h:81
isc::test
Definition env_var_wrapper.cc:14
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
pkt.h
isc::data::CfgToElement
Abstract class for configuration Cfg_* classes.
Definition cfg_to_element.h:29
token.h