Kea: cfg_attribute.cc Source File

| Kea 3.2.0-git

cfg_attribute.cc
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#include <config.h>
8
9#include <cfg_attribute.h>
10#include <eval/evaluate.h>
11#include <util/encode/encode.h>
12#include <sstream>
13#include <iomanip>
14
15using namespace std;
16using namespace isc;
17using namespace isc::data;
18using namespace isc::dhcp;
19
20namespace isc {
21 namespace radius {
22
23void
24CfgAttributes::add(const AttrDefPtr& def,
25 const ConstAttributePtr& attr,
26 const dhcp::ExpressionPtr& expr,
27 const std::string& test) {
28 if (!def) {
29 isc_throw(BadValue, "no attribute definition");
30 }
31 container_.insert(AttributeValue(def, attr, expr, test));
32}
33
34bool
35CfgAttributes::del(const uint8_t type, const uint32_t vendor) {
36 auto it = container_.find(boost::make_tuple(vendor, type));
37 if (it != container_.end()) {
38 container_.erase(it);
39 return (true);
40 }
41 return (false);
42}
43
44 AttrDefPtr
45CfgAttributes::getDef(const uint8_t type, const uint32_t vendor) const {
46 auto it = container_.find(boost::make_tuple(vendor, type));
47 if (it == container_.end()) {
48 return (AttrDefPtr());
49 }
50 return (it->def_);
51}
52
53 ConstAttributePtr
54CfgAttributes::get(const uint8_t type, const uint32_t vendor) const {
55 auto it = container_.find(boost::make_tuple(vendor, type));
56 if (it == container_.end()) {
57 return (AttributePtr());
58 }
59 return (it->attr_);
60}
61
62 ExpressionPtr
63CfgAttributes::getExpr(const uint8_t type, const uint32_t vendor) const {
64 auto it = container_.find(boost::make_tuple(vendor, type));
65 if (it == container_.end()) {
66 return (ExpressionPtr());
67 }
68 return (it->expr_);
69}
70
71string
72CfgAttributes::getTest(const uint8_t type, const uint32_t vendor) const {
73 auto it = container_.find(boost::make_tuple(vendor, type));
74 if (it == container_.end()) {
75 return ("");
76 }
77 return (it->test_);
78}
79
80 Attributes
81CfgAttributes::getAll() const {
82 Attributes attrs;
83 for (auto const& it : container_) {
84 attrs.add(it.attr_);
85 }
86 return (attrs);
87}
88
89 Attributes
90CfgAttributes::getEvalAll(Pkt& pkt) {
91 Attributes attrs;
92 for (auto const& it : container_) {
93 const ExpressionPtr& match_expr = it.expr_;
94 if (!match_expr) {
95 attrs.add(it.attr_);
96 continue;
97 }
98 string value = evaluateString(*match_expr, pkt);
99 if (value.empty()) {
100 continue;
101 }
102 AttrDefPtr def = it.def_;
103 if (!def) {
104 continue;
105 }
106 AttributePtr attr;
107 vector<uint8_t> binary;
108 binary.resize(value.size());
109 memmove(&binary[0], value.c_str(), binary.size());
110 attrs.add(Attribute::fromBytes(def, binary));
111 }
112 return (attrs);
113}
114
115 ElementPtr
116CfgAttributes::toElement() const {
117 ElementPtr result = Element::createList();
118 for (auto const& it : container_) {
119 AttrDefPtr def = it.def_;
120 if (!def) {
121 continue;
122 }
123 ElementPtr map;
124 if (it.attr_) {
125 map = it.attr_->toElement();
126 } else if (!it.test_.empty()) {
127 map = Element::createMap();
128 map->set("type", Element::create(static_cast<int>(def->type_)));
129 map->set("expr", Element::create(it.test_));
130 map->set("name", Element::create(def->name_));
131 if (def->vendor_ != 0) {
132 ostringstream vendor;
133 vendor << def->vendor_;
134 map->set("vendor", Element::create(vendor.str()));
135 }
136 }
137 result->add(map);
138 }
139 return (result);
140}
141
142} // end of namespace isc::radius
143} // end of namespace isc
cfg_attribute.h
Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:299
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
Element::createList
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:349
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::Attribute::fromBytes
static AttributePtr fromBytes(const std::vector< uint8_t > &bytes)
Generic factories.
Definition client_attribute.cc:75
isc::radius::Attributes
Collection of attributes.
Definition client_attribute.h:819
isc::radius::Attributes::add
void add(const ConstAttributePtr &attr, bool back=true)
Adds instance of the attribute to the collection.
Definition client_attribute.cc:755
isc::radius::CfgAttributes::AttributeValue
Container value class.
Definition cfg_attribute.h:158
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::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::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::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::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::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::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
encode.h
evaluate.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
Definition base_stamped_element.cc:12
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::evaluateString
std::string evaluateString(const Expression &expr, Pkt &pkt)
Evaluate a RPN expression for a v4 or v6 packet and return a string value.
Definition evaluate.cc:45
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::radius::AttributePtr
boost::shared_ptr< Attribute > AttributePtr
Definition client_attribute.h:39
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