Kea 3.1.1
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(pair<const uint8_t, AttributeValue>
32 (def->type_, AttributeValue(def, attr, expr, test)));
33}
34
35bool
36CfgAttributes::del(const uint8_t type) {
37 auto it = container_.find(type);
38 if (it != container_.end()) {
39 container_.erase(it);
40 return (true);
41 }
42 return (false);
43}
44
46CfgAttributes::getDef(const uint8_t type) const {
47 auto it = container_.find(type);
48 if (it == container_.end()) {
49 return (AttrDefPtr());
50 }
51 return (it->second.def_);
52}
53
55CfgAttributes::get(const uint8_t type) const {
56 auto it = container_.find(type);
57 if (it == container_.end()) {
58 return (AttributePtr());
59 }
60 return (it->second.attr_);
61}
62
64CfgAttributes::getExpr(const uint8_t type) const {
65 auto it = container_.find(type);
66 if (it == container_.end()) {
67 return (ExpressionPtr());
68 }
69 return (it->second.expr_);
70}
71
72string
73CfgAttributes::getTest(const uint8_t type) const {
74 auto it = container_.find(type);
75 if (it == container_.end()) {
76 return ("");
77 }
78 return (it->second.test_);
79}
80
83 Attributes attrs;
84 for (auto const& it : container_) {
85 attrs.add(it.second.attr_);
86 }
87 return (attrs);
88}
89
92 Attributes attrs;
93 for (auto const& it : container_) {
94 const ExpressionPtr& match_expr = it.second.expr_;
95 if (!match_expr) {
96 attrs.add(it.second.attr_);
97 continue;
98 }
99 string value = evaluateString(*match_expr, pkt);
100 if (value.empty()) {
101 continue;
102 }
103 AttrDefPtr def = it.second.def_;
104 if (!def) {
105 continue;
106 }
107 AttributePtr attr;
108 vector<uint8_t> binary;
109 binary.resize(value.size());
110 memmove(&binary[0], value.c_str(), binary.size());
111 attrs.add(Attribute::fromBytes(def, binary));
112 }
113 return (attrs);
114}
115
119 for (auto const& it : container_) {
120 AttrDefPtr def = it.second.def_;
121 if (!def) {
122 continue;
123 }
124 ElementPtr map;
125 if (!it.second.test_.empty()) {
126 map = Element::createMap();
127 map->set("type", Element::create(static_cast<int>(it.first)));
128 map->set("expr", Element::create(it.second.test_));
129 map->set("name", Element::create(def->name_));
130 } else if (it.second.attr_) {
131 map = it.second.attr_->toElement();
132 }
133 result->add(map);
134 }
135 return (result);
136}
137
138} // end of namespace isc::radius
139} // 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)
From bytes (wire format).
Collection of attributes.
void add(const ConstAttributePtr &attr)
Adds instance of the attribute to the collection.
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
Returns the definition of an attribute.
ConstAttributePtr get(const uint8_t type) const
Get instance of the attribute in the configuration.
std::multimap< uint8_t, AttributeValue > container_
The container.
dhcp::ExpressionPtr getExpr(const uint8_t type) const
Returns the expression of an attribute.
std::string getTest(const uint8_t type) const
Returns the text expression of an attribute.
bool del(const uint8_t type)
Deletes an attribute from the configuration.
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.