Kea 3.3.3
client_attribute.h
Go to the documentation of this file.
1// Copyright (C) 2023-2026 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 RADIUS_CLIENT_ATTRIBUTE_H
8#define RADIUS_CLIENT_ATTRIBUTE_H
9
10#include <asiolink/io_address.h>
11#include <cc/cfg_to_element.h>
12#include <cc/data.h>
13#include <cc/simple_parser.h>
16
17#include <string>
18#include <vector>
19
20#include <boost/multi_index/hashed_index.hpp>
21#include <boost/multi_index/member.hpp>
22#include <boost/multi_index/sequenced_index.hpp>
23#include <boost/multi_index_container.hpp>
24#include <boost/noncopyable.hpp>
25#include <boost/shared_ptr.hpp>
26
27namespace isc {
28namespace radius {
29
31static constexpr size_t MAX_STRING_LEN = 253;
32
34static constexpr size_t MAX_VSA_DATA_LEN = MAX_STRING_LEN - 4;
35
37using isc::data::TypeError;
38
40class Attribute;
41typedef boost::shared_ptr<Attribute> AttributePtr;
42typedef boost::shared_ptr<const Attribute> ConstAttributePtr;
43
46protected:
47
53 explicit Attribute(const uint8_t type) : type_(type) {
54 }
55
56public:
58 virtual ~Attribute() = default;
59
63 uint8_t getType() const {
64 return (type_);
65 }
66
70 virtual AttrValueType getValueType() const = 0;
71
73
79 static AttributePtr fromBytes(const std::vector<uint8_t>& bytes);
80
82
91 static AttributePtr fromText(const AttrDefPtr& def,
92 const std::string& value);
93
102 static AttributePtr fromBytes(const AttrDefPtr& def,
103 const std::vector<uint8_t>& value);
104
108
115 static AttributePtr fromString(const uint8_t type,
116 const std::string& value);
117
124 static AttributePtr fromBinary(const uint8_t type,
125 const std::vector<uint8_t>& value);
126
133 static AttributePtr fromInt(const uint8_t type, const uint32_t value);
134
141 static AttributePtr fromIpAddr(const uint8_t type,
142 const asiolink::IOAddress& value);
143
150 static AttributePtr fromIpv6Addr(const uint8_t type,
151 const asiolink::IOAddress& value);
152
160 static AttributePtr fromIpv6Prefix(const uint8_t type,
161 const uint8_t len,
162 const asiolink::IOAddress& value);
163
172 static AttributePtr fromVsa(const uint8_t type,
173 const uint32_t vendor,
174 const std::string& value);
175
183 static AttributePtr fromVsa(const uint8_t type,
184 const uint32_t vendor,
185 const std::vector<uint8_t>& value);
186
188
192 virtual size_t getValueLen() const = 0;
193
200 virtual std::string toText(size_t indent = 0) const = 0;
201
205 virtual std::vector<uint8_t> toBytes() const = 0;
206
210
215 virtual std::string toString() const;
216
221 virtual std::vector<uint8_t> toBinary() const;
222
227 virtual uint32_t toInt() const;
228
233 virtual asiolink::IOAddress toIpAddr() const;
234
239 virtual asiolink::IOAddress toIpv6Addr() const;
240
245 virtual asiolink::IOAddress toIpv6Prefix() const;
246
251 virtual uint8_t toIpv6PrefixLen() const;
252
257 virtual uint32_t toVendorId() const;
258
263 virtual std::vector<uint8_t> toVsaData() const;
264
266 const uint8_t type_;
267
270
271private:
272
281 static AttributePtr fromText0(const AttrDefPtr& def,
282 const std::string& value);
283
292 static AttributePtr fromBytes0(const AttrDefPtr& def,
293 const std::vector<uint8_t>& value);
294};
295
298
300class AttrString : public Attribute {
301protected:
302
307 AttrString(const uint8_t type, const std::string& value)
308 : Attribute(type), value_(value) {
309 if (value.empty()) {
310 isc_throw(BadValue, "value is empty");
311 }
312 if (value.size() > MAX_STRING_LEN) {
313 isc_throw(BadValue, "value is too large " << value.size()
314 << " > " << MAX_STRING_LEN);
315 }
316 }
317
322 AttrString(const uint8_t type, const std::vector<uint8_t>& value);
323
329 static AttributePtr fromText(const uint8_t type, const std::string& repr);
330
336 static AttributePtr fromBytes(const uint8_t type,
337 const std::vector<uint8_t>& bytes);
338
340 friend class Attribute;
341
342public:
346 virtual AttrValueType getValueType() const override {
347 return (PW_TYPE_STRING);
348 }
349
353 virtual size_t getValueLen() const override {
354 return (value_.size());
355 }
356
361 virtual std::string toText(size_t indent = 0) const override;
362
366 virtual std::vector<uint8_t> toBytes() const override;
367
371 virtual std::string toString() const override {
372 return (value_);
373 }
374
378 virtual std::vector<uint8_t> toBinary() const override;
379
383 virtual data::ElementPtr toElement() const override;
384
385private:
387 std::string value_;
388};
389
391class AttrInt : public Attribute {
392protected:
393
398 AttrInt(const uint8_t type, const int32_t value)
399 : Attribute(type), value_(static_cast<uint32_t>(value)) {
400 }
401
406 AttrInt(const uint8_t type, const uint32_t value)
407 : Attribute(type), value_(value) {
408 }
409
415 static AttributePtr fromText(const uint8_t type, const std::string& repr);
416
422 static AttributePtr fromBytes(const uint8_t type,
423 const std::vector<uint8_t>& bytes);
424
426 friend class Attribute;
427
428public:
429
433 virtual AttrValueType getValueType() const override {
434 return (PW_TYPE_INTEGER);
435 }
436
440 virtual size_t getValueLen() const override {
441 return (4);
442 }
443
448 virtual std::string toText(size_t indent = 0) const override;
449
453 virtual std::vector<uint8_t> toBytes() const override;
454
458 virtual uint32_t toInt() const override {
459 return (value_);
460 }
461
465 virtual data::ElementPtr toElement() const override;
466
467private:
469 const uint32_t value_;
470};
471
473class AttrIpAddr : public Attribute {
474protected:
475
480 AttrIpAddr(const uint8_t type, const asiolink::IOAddress& value)
481 : Attribute(type), value_(value) {
482 if (!value.isV4()) {
483 isc_throw(BadValue, "not v4 address " << value);
484 }
485 }
486
492 static AttributePtr fromText(const uint8_t type, const std::string& repr);
493
499 static AttributePtr fromBytes(const uint8_t type,
500 const std::vector<uint8_t>& bytes);
501
503 friend class Attribute;
504
505public:
509 virtual AttrValueType getValueType() const override {
510 return (PW_TYPE_IPADDR);
511 }
512
516 virtual size_t getValueLen() const override {
517 return (4);
518 }
519
524 virtual std::string toText(size_t indent = 0) const override;
525
529 virtual std::vector<uint8_t> toBytes() const override;
530
534 virtual asiolink::IOAddress toIpAddr() const override {
535 return (value_);
536 }
537
541 virtual data::ElementPtr toElement() const override;
542
543private:
545 asiolink::IOAddress value_;
546};
547
549class AttrIpv6Addr : public Attribute {
550protected:
551
556 AttrIpv6Addr(const uint8_t type, const asiolink::IOAddress& value)
557 : Attribute(type), value_(value) {
558 if (!value.isV6()) {
559 isc_throw(BadValue, "not v6 address " << value);
560 }
561 }
562
568 static AttributePtr fromText(const uint8_t type, const std::string& repr);
569
575 static AttributePtr fromBytes(const uint8_t type,
576 const std::vector<uint8_t>& bytes);
577
579 friend class Attribute;
580
581public:
582
586 virtual AttrValueType getValueType() const override {
587 return (PW_TYPE_IPV6ADDR);
588 }
589
593 virtual size_t getValueLen() const override {
594 return (16);
595 }
596
601 virtual std::string toText(size_t indent = 0) const override;
602
606 virtual std::vector<uint8_t> toBytes() const override;
607
611 virtual asiolink::IOAddress toIpv6Addr() const override {
612 return (value_);
613 }
614
618 virtual data::ElementPtr toElement() const override;
619
620private:
622 asiolink::IOAddress value_;
623};
624
626class AttrIpv6Prefix : public Attribute {
627protected:
628
634 AttrIpv6Prefix(const uint8_t type, const uint8_t len,
635 const asiolink::IOAddress& value)
636 : Attribute(type), len_(len), value_(value) {
637 if (!value.isV6()) {
638 isc_throw(BadValue, "not v6 address " << value);
639 }
640 if (len > 128) {
641 isc_throw(BadValue, "too long prefix "
642 << static_cast<unsigned>(len));
643 }
644 }
645
651 static AttributePtr fromText(const uint8_t type, const std::string& repr);
652
658 static AttributePtr fromBytes(const uint8_t type,
659 const std::vector<uint8_t>& bytes);
660
662 friend class Attribute;
663
664public:
665
669 virtual AttrValueType getValueType() const override {
670 return (PW_TYPE_IPV6PREFIX);
671 }
672
676 virtual size_t getValueLen() const override {
677 return (17);
678 }
679
684 virtual std::string toText(size_t indent = 0) const override;
685
689 virtual std::vector<uint8_t> toBytes() const override;
690
694 virtual asiolink::IOAddress toIpv6Prefix() const override {
695 return (value_);
696 }
697
701 virtual uint8_t toIpv6PrefixLen() const override {
702 return (len_);
703 }
704
708 virtual data::ElementPtr toElement() const override;
709
710private:
712 const uint8_t len_;
713
715 asiolink::IOAddress value_;
716};
717
719class AttrVsa : public Attribute {
720protected:
721
727 AttrVsa(const uint8_t type, const uint32_t vendor,
728 const std::string& value)
729 : Attribute(type), vendor_(vendor), value_(value) {
730 if (value.empty()) {
731 isc_throw(BadValue, "value is empty");
732 }
733 if (value.size() > MAX_VSA_DATA_LEN) {
734 isc_throw(BadValue, "value is too large " << value.size()
735 << " > " << MAX_VSA_DATA_LEN);
736 }
737 }
738
744 AttrVsa(const uint8_t type, const uint32_t vendor,
745 const std::vector<uint8_t>& value);
746
753 static AttributePtr fromText(const uint8_t type, const std::string& repr);
754
760 static AttributePtr fromBytes(const uint8_t type,
761 const std::vector<uint8_t>& bytes);
762
764 friend class Attribute;
765
766public:
767
771 virtual AttrValueType getValueType() const override {
772 return (PW_TYPE_VSA);
773 }
774
778 virtual size_t getValueLen() const override {
779 return (4 + value_.size());
780 }
781
786 virtual std::string toText(size_t indent = 0) const override;
787
791 virtual std::vector<uint8_t> toBytes() const override;
792
796 virtual uint32_t toVendorId() const override {
797 return (vendor_);
798 }
799
803 virtual std::vector<uint8_t> toVsaData() const override;
804
808 virtual data::ElementPtr toElement() const override;
809
810private:
812 uint32_t vendor_;
813
815 std::string value_;
816};
817
822public:
823
825 typedef boost::multi_index_container<
826 // This container stores pointers to attributes.
828 // Start specification of indexes here.
829 boost::multi_index::indexed_by<
830 // Sequenced index to keep insert order.
831 boost::multi_index::sequenced<>,
832 // Hash index for direct retrieval.
833 boost::multi_index::hashed_non_unique<
834 boost::multi_index::member<
835 Attribute, const uint8_t, &Attribute::type_
836 >
837 >
838 >
840
843 }
844
846 virtual ~Attributes() {
847 clear();
848 }
849
853 bool empty() const {
854 return (container_.empty());
855 }
856
860 size_t size() const {
861 return (container_.size());
862 }
863
868 void add(const ConstAttributePtr& attr, bool back = true);
869
876 bool del(const uint8_t type);
877
879 void clear() {
880 container_.clear();
881 }
882
886 void append(const Attributes& other);
887
892 size_t count(const uint8_t type) const;
893
898 ConstAttributePtr get(const uint8_t type) const;
899
906 std::string toText(size_t indent = 0) const;
907
911 data::ElementPtr toElement() const override;
912
917 static Attributes fromElement(const data::ConstElementPtr& attr_list);
918
922 AttributeContainer::iterator begin() {
923 return (container_.begin());
924 }
925
929 AttributeContainer::const_iterator begin() const {
930 return (container_.begin());
931 }
932
936 AttributeContainer::const_iterator cbegin() const {
937 return (container_.cbegin());
938 }
939
943 AttributeContainer::iterator end() {
944 return (container_.end());
945 }
946
950 AttributeContainer::const_iterator end() const {
951 return (container_.end());
952 }
953
955 AttributeContainer::const_iterator cend() const {
956 return (container_.cend());
957 }
958
959protected:
962};
963
965typedef boost::shared_ptr<Attributes> AttributesPtr;
966
967} // end of namespace isc::radius
968} // end of namespace isc
969
970#endif
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual uint32_t toInt() const override
To integer.
static AttributePtr fromText(const uint8_t type, const std::string &repr)
From text.
virtual std::vector< uint8_t > toBytes() const override
To bytes.
virtual size_t getValueLen() const override
Value length.
virtual data::ElementPtr toElement() const override
Unparse attribute.
AttrInt(const uint8_t type, const uint32_t value)
Constructor (unsigned).
friend class Attribute
Make Attribute a friend class.
virtual std::string toText(size_t indent=0) const override
Returns text representation of the attribute.
AttrInt(const uint8_t type, const int32_t value)
Constructor (signed).
static AttributePtr fromBytes(const uint8_t type, const std::vector< uint8_t > &bytes)
From bytes.
virtual AttrValueType getValueType() const override
Get value type.
AttrIpAddr(const uint8_t type, const asiolink::IOAddress &value)
Constructor.
virtual data::ElementPtr toElement() const override
Unparse attribute.
virtual std::vector< uint8_t > toBytes() const override
To bytes.
static AttributePtr fromText(const uint8_t type, const std::string &repr)
From text.
virtual asiolink::IOAddress toIpAddr() const override
To IPv4 address.
static AttributePtr fromBytes(const uint8_t type, const std::vector< uint8_t > &bytes)
From bytes.
virtual size_t getValueLen() const override
Value length.
friend class Attribute
Make Attribute a friend class.
virtual AttrValueType getValueType() const override
Get value type.
virtual std::string toText(size_t indent=0) const override
Returns text representation of the attribute.
virtual std::string toText(size_t indent=0) const override
Returns text representation of the attribute.
virtual asiolink::IOAddress toIpv6Addr() const override
To IPv6 address.
static AttributePtr fromBytes(const uint8_t type, const std::vector< uint8_t > &bytes)
From bytes.
virtual std::vector< uint8_t > toBytes() const override
To bytes.
virtual AttrValueType getValueType() const override
Get value type.
virtual size_t getValueLen() const override
Value length.
static AttributePtr fromText(const uint8_t type, const std::string &repr)
From text.
AttrIpv6Addr(const uint8_t type, const asiolink::IOAddress &value)
Constructor.
friend class Attribute
Make Attribute a friend class.
virtual data::ElementPtr toElement() const override
Unparse attribute.
virtual asiolink::IOAddress toIpv6Prefix() const override
To IPv6 prefix.
AttrIpv6Prefix(const uint8_t type, const uint8_t len, const asiolink::IOAddress &value)
Constructor.
virtual std::vector< uint8_t > toBytes() const override
To bytes.
virtual data::ElementPtr toElement() const override
Unparse attribute.
virtual size_t getValueLen() const override
Value length.
virtual uint8_t toIpv6PrefixLen() const override
To IPv6 prefix length.
friend class Attribute
Make Attribute a friend class.
virtual AttrValueType getValueType() const override
Get value type.
static AttributePtr fromText(const uint8_t type, const std::string &repr)
From text.
static AttributePtr fromBytes(const uint8_t type, const std::vector< uint8_t > &bytes)
From bytes.
virtual std::string toText(size_t indent=0) const override
Returns text representation of the attribute.
static AttributePtr fromBytes(const uint8_t type, const std::vector< uint8_t > &bytes)
From bytes.
static AttributePtr fromText(const uint8_t type, const std::string &repr)
From text.
virtual std::vector< uint8_t > toBytes() const override
To bytes.
virtual data::ElementPtr toElement() const override
Unparse attribute.
virtual std::string toText(size_t indent=0) const override
Returns text representation of the attribute.
AttrString(const uint8_t type, const std::string &value)
Constructor.
virtual std::vector< uint8_t > toBinary() const override
To binary.
virtual size_t getValueLen() const override
Value length.
friend class Attribute
Make Attribute a friend class.
virtual std::string toString() const override
To string.
virtual AttrValueType getValueType() const override
Get value type.
virtual data::ElementPtr toElement() const override
Unparse attribute.
virtual std::string toText(size_t indent=0) const override
Returns text representation of the attribute.
virtual uint32_t toVendorId() const override
To vendor id.
static AttributePtr fromBytes(const uint8_t type, const std::vector< uint8_t > &bytes)
From bytes.
virtual AttrValueType getValueType() const override
Get value type.
friend class Attribute
Make Attribute a friend class.
static AttributePtr fromText(const uint8_t type, const std::string &repr)
From text.
virtual std::vector< uint8_t > toVsaData() const override
To vsa data.
AttrVsa(const uint8_t type, const uint32_t vendor, const std::string &value)
Constructor.
virtual size_t getValueLen() const override
Value length.
virtual std::vector< uint8_t > toBytes() const override
To bytes.
RADIUS attribute base class.
static AttributePtr fromBytes(const std::vector< uint8_t > &bytes)
Generic factories.
virtual std::string toString() const
Specific get methods.
virtual size_t getValueLen() const =0
Generic get methods.
static AttributePtr fromInt(const uint8_t type, const uint32_t value)
From integer with type.
static AttributePtr fromIpAddr(const uint8_t type, const asiolink::IOAddress &value)
From IPv4 address with type.
virtual uint32_t toInt() const
To integer.
Attribute(const uint8_t type)
Constructor.
virtual uint32_t toVendorId() const
To vendor id.
static AttributePtr fromString(const uint8_t type, const std::string &value)
From type specific factories.
virtual std::vector< uint8_t > toBytes() const =0
To bytes (wire format).
uint8_t getType() const
Get type.
virtual ~Attribute()=default
Virtual destructor.
virtual asiolink::IOAddress toIpAddr() const
To IPv4 address.
virtual asiolink::IOAddress toIpv6Addr() const
To IPv6 address.
static AttributePtr fromBinary(const uint8_t type, const std::vector< uint8_t > &value)
From binary with type.
static AttributePtr fromText(const AttrDefPtr &def, const std::string &value)
From definition generic factories.
virtual uint8_t toIpv6PrefixLen() const
To IPv6 prefix length.
static AttributePtr fromIpv6Prefix(const uint8_t type, const uint8_t len, const asiolink::IOAddress &value)
From IPv6 prefix with type.
virtual std::string toText(size_t indent=0) const =0
Returns text representation of the attribute.
static AttributePtr fromIpv6Addr(const uint8_t type, const asiolink::IOAddress &value)
From IPv6 address with type.
virtual asiolink::IOAddress toIpv6Prefix() const
To IPv6 prefix.
virtual AttrValueType getValueType() const =0
Get value type.
const uint8_t type_
Type.
virtual std::vector< uint8_t > toBinary() const
To binary.
static AttributePtr fromVsa(const uint8_t type, const uint32_t vendor, const std::string &value)
From Vendor ID and string data with type.
virtual std::vector< uint8_t > toVsaData() const
To vsa data.
Collection of attributes.
boost::multi_index_container< ConstAttributePtr, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::hashed_non_unique< boost::multi_index::member< Attribute, const uint8_t, &Attribute::type_ > > > > AttributeContainer
Type of the container.
AttributeContainer container_
The container.
size_t size() const
Returns the number of elements.
AttributeContainer::const_iterator begin() const
Get the iterator to the beginning.
std::string toText(size_t indent=0) const
Returns text representation of the collection.
bool empty() const
Indicates the object is empty.
void append(const Attributes &other)
Append another collection.
size_t count(const uint8_t type) const
Counts instance of the attribute in the collection.
virtual ~Attributes()
Destructor.
AttributeContainer::const_iterator cend() const
Get the const iterator to the past-the-end.
ConstAttributePtr get(const uint8_t type) const
Get instance of the attribute in the collection.
data::ElementPtr toElement() const override
Unparse collection.
void add(const ConstAttributePtr &attr, bool back=true)
Adds instance of the attribute to the collection.
AttributeContainer::const_iterator cbegin() const
Get the const iterator to the beginning.
void clear()
Clear the collection.
AttributeContainer::iterator begin()
Get the iterator to the beginning.
bool del(const uint8_t type)
Deletes an attribute from the collection.
static Attributes fromElement(const data::ConstElementPtr &attr_list)
Parse collection.
AttributeContainer::iterator end()
Get the iterator to the past-the-end.
AttributeContainer::const_iterator end() const
Get the iterator to the past-the-end.
if(!(yy_init))
Definition d2_lexer.cc:1515
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
boost::shared_ptr< Attributes > AttributesPtr
Shared pointers to attribute collection.
boost::shared_ptr< const Attribute > ConstAttributePtr
boost::shared_ptr< AttrDef > AttrDefPtr
Shared pointers to Attribute definition.
AttrValueType
Attribute value types.
boost::shared_ptr< Attribute > AttributePtr
Defines the logger used by the top-level component of kea-lfc.
Abstract class for configuration Cfg_* classes.