Kea 3.1.3
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 {
21namespace radius {
22
23void
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
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
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
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
82 Attributes attrs;
83 for (auto const& it : container_) {
84 attrs.add(it.attr_);
85 }
86 return (attrs);
87}
88
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
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
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Base class for classes representing DHCP messages.
Definition pkt.h:161
static AttributePtr fromBytes(const std::vector< uint8_t > &bytes)
Generic factories.
Collection of attributes.
void add(const ConstAttributePtr &attr)
Adds instance of the attribute to the collection.
ConstAttributePtr get(const uint8_t type, const uint32_t vendor=0) const
Get instance of the attribute in the configuration.
dhcp::ExpressionPtr getExpr(const uint8_t type, const uint32_t vendor=0) const
Returns the expression of an attribute.
Attributes getAll() const
Get all attributes in the configuration.
void add(const AttrDefPtr &def, const ConstAttributePtr &attr, const dhcp::ExpressionPtr &expr, const std::string &test)
Adds instance of the attribute to the configuration.
AttrDefPtr getDef(const uint8_t type, const uint32_t vendor=0) const
Returns the definition of an attribute.
bool del(const uint8_t type, const uint32_t vendor=0)
Deletes an attribute from the configuration.
std::string getTest(const uint8_t type, const uint32_t vendor=0) const
Returns the text expression of an attribute.
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.
Attributes getEvalAll(dhcp::Pkt &pkt)
Get all evaluated attributes in the configuration.
data::ElementPtr toElement() const override
Unparse configuration.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
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
boost::shared_ptr< Expression > ExpressionPtr
Definition token.h:31
boost::shared_ptr< const Attribute > ConstAttributePtr
boost::shared_ptr< AttrDef > AttrDefPtr
Shared pointers to Attribute definition.
boost::shared_ptr< Attribute > AttributePtr
Defines the logger used by the top-level component of kea-lfc.