| Kea 3.0.2-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
15 using namespace std;
16 using namespace isc;
17 using namespace isc::data;
18 using namespace isc::dhcp;
19
20 namespace isc {
21 namespace radius {
22
23 void
24 CfgAttributes::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(pair<const uint8_t, AttributeValue>
32 (def->type_, AttributeValue(def, attr, expr, test)));
33}
34
35 bool
36 CfgAttributes::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
45 AttrDefPtr
46 CfgAttributes::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
54 ConstAttributePtr
55 CfgAttributes::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
63 ExpressionPtr
64 CfgAttributes::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
72 string
73 CfgAttributes::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
81 Attributes
82 CfgAttributes::getAll() const {
83 Attributes attrs;
84 for (auto const& it : container_) {
85 attrs.add(it.second.attr_);
86 }
87 return (attrs);
88}
89
90 Attributes
91 CfgAttributes::getEvalAll(Pkt& pkt) {
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
116 ElementPtr
117 CfgAttributes::toElement() const {
118 ElementPtr result = Element::createList();
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
cfg_attribute.h
Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
Element::createList
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
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)
From bytes (wire format).
Definition client_attribute.cc:91
isc::radius::Attributes
Collection of attributes.
Definition client_attribute.h:652
isc::radius::Attributes::add
void add(const ConstAttributePtr &attr)
Adds instance of the attribute to the collection.
Definition client_attribute.cc:616
isc::radius::CfgAttributes::AttributeValue
Container value class.
Definition cfg_attribute.h:146
isc::radius::CfgAttributes::getAll
Attributes getAll() const
Get all attributes in the configuration.
Definition cfg_attribute.cc:82
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
Returns the definition of an attribute.
Definition cfg_attribute.cc:46
isc::radius::CfgAttributes::get
ConstAttributePtr get(const uint8_t type) const
Get instance of the attribute in the configuration.
Definition cfg_attribute.cc:55
isc::radius::CfgAttributes::container_
std::multimap< uint8_t, AttributeValue > container_
The container.
Definition cfg_attribute.h:188
isc::radius::CfgAttributes::getExpr
dhcp::ExpressionPtr getExpr(const uint8_t type) const
Returns the expression of an attribute.
Definition cfg_attribute.cc:64
isc::radius::CfgAttributes::getTest
std::string getTest(const uint8_t type) const
Returns the text expression of an attribute.
Definition cfg_attribute.cc:73
isc::radius::CfgAttributes::del
bool del(const uint8_t type)
Deletes an attribute from the configuration.
Definition cfg_attribute.cc:36
isc::radius::CfgAttributes::getEvalAll
Attributes getEvalAll(dhcp::Pkt &pkt)
Get all evaluated attributes in the configuration.
Definition cfg_attribute.cc:91
isc::radius::CfgAttributes::toElement
data::ElementPtr toElement() const override
Unparse configuration.
Definition cfg_attribute.cc:117
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:28
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:37
isc::radius::AttrDefPtr
boost::shared_ptr< AttrDef > AttrDefPtr
Shared pointers to Attribute definition.
Definition client_dictionary.h:76
isc::radius::AttributePtr
boost::shared_ptr< Attribute > AttributePtr
Definition client_attribute.h:36
isc::test
Definition env_var_wrapper.cc:12
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148