13#include <boost/lexical_cast.hpp>
36 if (def->vendor_ == 0) {
40 const vector<uint8_t> vsa_data = attr->toBytes();
45Attribute::fromText0(
const AttrDefPtr& def,
const string& value) {
46 switch (
static_cast<uint8_t
>(def->value_type_)) {
50 if (!isdigit(value[0])) {
55 return (
fromInt(def->type_, ic_def->value_));
70 <<
static_cast<unsigned>(def->value_type_));
77 if (bytes.size() < 2) {
80 uint8_t type = bytes[0];
81 uint8_t len = bytes[1];
83 if (len != bytes.size()) {
85 <<
" != " <<
static_cast<unsigned>(len));
87 vector<uint8_t> value;
88 value.resize(len - 2);
89 if (value.size() > 0) {
90 memmove(&value[0], &bytes[2], value.size());
108 if (def->vendor_ == 0) {
112 const vector<uint8_t> vsa_data = attr->toBytes();
117Attribute::fromBytes0(
const AttrDefPtr& def,
const vector<uint8_t>& value) {
118 switch (
static_cast<uint8_t
>(def->value_type_)) {
134 <<
static_cast<unsigned>(def->value_type_));
177 const std::string& value) {
183 const std::vector<uint8_t>& value) {
244 isc_throw(BadValue,
"value is empty");
246 if (value.size() > MAX_STRING_LEN) {
247 isc_throw(BadValue,
"value is too large " << value.size()
248 <<
" > " << MAX_STRING_LEN);
250 value_.resize(value.size());
251 memmove(&value_[0], &value[0], value_.size());
259 if (repr.size() > MAX_STRING_LEN) {
261 <<
" > " << MAX_STRING_LEN);
271 if (bytes.size() > MAX_STRING_LEN) {
273 <<
" > " << MAX_STRING_LEN);
280 ostringstream output;
281 for (
size_t i = 0; i < indent; i++) {
286 output <<
"'" << value_ <<
"'";
288 vector<uint8_t> binary;
289 binary.resize(value_.size());
290 if (binary.size() > 0) {
291 memmove(&binary[0], value_.c_str(), binary.size());
295 return (output.str());
300 vector<uint8_t> output;
304 if (output.size() > 2) {
305 memmove(&output[2], &value_[0], output.size() - 2);
312 vector<uint8_t> binary;
314 if (binary.size() > 0) {
315 memmove(&binary[0], &value_[0], binary.size());
331 vector<uint8_t> binary;
332 binary.resize(value_.size());
333 if (binary.size() > 0) {
334 memmove(&binary[0], value_.c_str(), binary.size());
348 int64_t val = boost::lexical_cast<int64_t>(repr);
349 if ((val < numeric_limits<int32_t>::min()) ||
350 (val > numeric_limits<uint32_t>::max())) {
365 if (bytes.size() != 4) {
367 msg <<
"bad value length " << bytes.size() <<
" != 4";
374 int32_t val = (
static_cast<int32_t
>(bytes[0])) << 24;
375 val |= (
static_cast<int32_t
>(bytes[1])) << 16;
376 val |= (
static_cast<int32_t
>(bytes[2])) << 8;
377 val |=
static_cast<int32_t
>(bytes[3]);
383 ostringstream output;
384 for (
size_t i = 0; i < indent; i++) {
390 output << ic_def->name_;
394 return (output.str());
399 vector<uint8_t> output;
403 output[2] =
static_cast<uint8_t
>((value_ & 0xff000000U) >> 24);
404 output[3] =
static_cast<uint8_t
>((value_ & 0xff0000U) >> 16);
405 output[4] =
static_cast<uint8_t
>((value_ & 0xff00U) >> 8);
406 output[5] =
static_cast<uint8_t
>(value_ & 0xffU);
440 if (bytes.size() != 4) {
442 msg <<
"bad value length " << bytes.size() <<
" != 4";
455 ostringstream output;
456 for (
size_t i = 0; i < indent; i++) {
461 return (output.str());
466 vector<uint8_t> output;
470 vector<uint8_t> binary = value_.toBytes();
471 memmove(&output[2], &binary[0], output.size() - 2);
484 val << value_.toText();
505 if (bytes.size() != 16) {
507 msg <<
"bad value length " << bytes.size() <<
" != 16";
520 ostringstream output;
521 for (
size_t i = 0; i < indent; i++) {
526 return (output.str());
531 vector<uint8_t> output;
535 vector<uint8_t> binary = value_.toBytes();
536 memmove(&output[2], &binary[0], output.size() - 2);
549 val << value_.toText();
557 auto pos = repr.find(
'/');
558 if ((pos == string::npos) ||
559 (pos == repr.size() - 1) ||
564 int len = boost::lexical_cast<int>(repr.substr(pos + 1));
565 if ((len < numeric_limits<uint8_t>::min()) ||
566 (len > numeric_limits<uint8_t>::max())) {
570 static_cast<uint8_t
>(len),
585 if (bytes.size() < 2) {
586 msg <<
"bad value length " << bytes.size() <<
" < 2";
587 }
else if (bytes.size() > 18) {
588 msg <<
"bad value length " << bytes.size() <<
" > 18";
589 }
else if (bytes[1] > 128) {
590 msg <<
"bad prefix length " <<
static_cast<unsigned>(bytes[1]) <<
" > 128";
592 string const msg_str(msg.str());
593 if (!msg_str.empty()) {
595 .arg(
static_cast<unsigned>(type))
600 uint8_t len = bytes[1];
601 vector<uint8_t> prefix(16);
602 if (bytes.size() > 2) {
603 memmove(&prefix[0], &bytes[2], bytes.size() - 2);
611 ostringstream output;
612 for (
size_t i = 0; i < indent; i++) {
616 << value_.toText() <<
"/" <<
static_cast<unsigned>(len_);
617 return (output.str());
622 vector<uint8_t> output;
629 vector<uint8_t> binary = value_.toBytes();
630 memmove(&output[4], &binary[0], output.size() - 4);
643 val << value_.toText() <<
"/" <<
static_cast<unsigned>(len_);
649 const vector<uint8_t>& value)
650 :
Attribute(type), vendor_(vendor), value_() {
652 isc_throw(BadValue,
"value is empty");
654 if (value.size() > MAX_VSA_DATA_LEN) {
655 isc_throw(BadValue,
"value is too large " << value.size()
656 <<
" > " << MAX_VSA_DATA_LEN);
658 value_.resize(value.size());
659 memmove(&value_[0], &value[0], value_.size());
672 if (bytes.size() < 5) {
674 }
else if (bytes.size() > MAX_STRING_LEN) {
676 <<
" > " << MAX_STRING_LEN);
678 uint32_t vendor = bytes[0] << 24;
679 vendor |= bytes[1] << 16;
680 vendor |= bytes[2] << 8;
682 vector<uint8_t> value;
683 value.resize(bytes.size() - 4);
684 if (value.size() > 0) {
685 memmove(&value[0], &bytes[4], value.size());
692 ostringstream output;
693 for (
size_t i = 0; i < indent; i++) {
698 vector<uint8_t> binary;
699 binary.resize(value_.size());
700 if (binary.size() > 0) {
701 memmove(&binary[0], value_.c_str(), binary.size());
704 return (output.str());
709 vector<uint8_t> output;
713 output[2] = (vendor_ & 0xff000000U) >> 24;
714 output[3] = (vendor_ & 0xff0000U) >> 16;
715 output[4] = (vendor_ & 0xff00U) >> 8;
716 output[5] = vendor_ & 0xffU;
717 if (output.size() > 6) {
718 memmove(&output[6], &value_[0], output.size() - 6);
725 vector<uint8_t> binary;
726 binary.resize(value_.size());
727 if (binary.size() > 0) {
728 memmove(&binary[0], &value_[0], binary.size());
741 ostringstream vendor;
744 vector<uint8_t> binary;
745 binary.resize(value_.size());
746 if (binary.size() > 0) {
747 memmove(&binary[0], value_.c_str(), binary.size());
759 static_cast<void>(
container_.push_back(attr));
765 auto it = idx.find(type);
766 if (it != idx.end()) {
775 for (
auto const& it : other) {
783 return (idx.count(type));
789 auto it = idx.find(type);
790 if (it != idx.end()) {
799 ostringstream output;
801 for (
auto const& it : *
this) {
807 output << it->toText(indent);
809 return (output.str());
815 for (
auto const& it : *
this) {
816 output->add(it->toElement());
827 for (
auto const& entry : attr_list->listValue()) {
842 (!raw->stringValue().empty())) {
844 vector<uint8_t> binary;
856 (!
data->stringValue().empty())) {
static ElementPtr create(const Position &pos=ZERO_POSITION())
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown when a function is not implemented.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
The IOAddress class represents an IP addresses (version agnostic)
static IOAddress fromBytes(short family, const uint8_t *data)
Creates an address from over wire data.
A standard Data module exception that is thrown if a function is called for an Element that has a wro...
static AttrDefs & instance()
Returns a single instance.
IntCstDefPtr getByValue(const uint8_t type, const uint32_t value, const uint32_t vendor=0) const
Get integer constant definition by attribute type and value.
std::string getName(const uint8_t type, const uint32_t vendor=0) const
Get attribute name.
AttrDefPtr getByName(const std::string &name, const uint32_t vendor=0) const
Get attribute definition by name and vendor.
AttrDefPtr getByType(const uint8_t type, const uint32_t vendor=0) const
Get attribute definition by type and vendor.
RADIUS attribute holding integers.
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.
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.
RADIUS attribute holding IPv4 addresses.
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.
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.
RADIUS attribute holding IPv6 addresses.
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.
virtual std::vector< uint8_t > toBytes() const override
To bytes.
static AttributePtr fromText(const uint8_t type, const std::string &repr)
From text.
AttrIpv6Addr(const uint8_t type, const asiolink::IOAddress &value)
Constructor.
virtual data::ElementPtr toElement() const override
Unparse attribute.
RADIUS attribute holding IPv6 prefixes.
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.
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.
RADIUS attribute derived classes: do not use them directly outside unit tests, instead use 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.
RADIUS attribute holding vsa.
virtual data::ElementPtr toElement() const override
Unparse attribute.
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.
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.
static AttributePtr fromBytes(const std::vector< uint8_t > &bytes)
Generic factories.
virtual std::string toString() const
Specific 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.
virtual uint32_t toVendorId() const
To vendor id.
static AttributePtr fromString(const uint8_t type, const std::string &value)
From type specific factories.
uint8_t getType() const
Get type.
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.
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.
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.
void add(const ConstAttributePtr &attr)
Adds instance of the attribute to the collection.
AttributeContainer container_
The container.
std::string toText(size_t indent=0) const
Returns text representation of the collection.
void append(const Attributes &other)
Append another collection.
size_t count(const uint8_t type) const
Counts instance of the attribute in the collection.
ConstAttributePtr get(const uint8_t type) const
Get instance of the attribute in the collection.
data::ElementPtr toElement() const override
Unparse collection.
bool del(const uint8_t type)
Deletes an attribute from the collection.
static Attributes fromElement(const data::ConstElementPtr &attr_list)
Parse collection.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< Element > ElementPtr
boost::shared_ptr< IntCstDef > IntCstDefPtr
Shared pointers to Integer constant definition.
@ PW_VENDOR_SPECIFIC
string.
const isc::log::MessageID RADIUS_IPV6PREFIX_ATTRIBUTE_FROM_BYTES_FAILED
boost::shared_ptr< const Attribute > ConstAttributePtr
const isc::log::MessageID RADIUS_IPADDR_ATTRIBUTE_FROM_BYTES_FAILED
const isc::log::MessageID RADIUS_IPV6PREFIX_ATTRIBUTE_FROM_TEXT_FAILED
boost::shared_ptr< AttrDef > AttrDefPtr
Shared pointers to Attribute definition.
string attrValueTypeToText(const AttrValueType value)
AttrValueType value -> name function.
boost::shared_ptr< Attribute > AttributePtr
const isc::log::MessageID RADIUS_IPV6ADDR_ATTRIBUTE_FROM_TEXT_FAILED
isc::log::Logger radius_logger("radius-hooks")
Radius Logger.
const isc::log::MessageID RADIUS_INTEGER_ATTRIBUTE_FROM_TEXT_FAILED
const isc::log::MessageID RADIUS_INTEGER_ATTRIBUTE_FROM_BYTES_FAILED
const isc::log::MessageID RADIUS_IPADDR_ATTRIBUTE_FROM_TEXT_FAILED
const isc::log::MessageID RADIUS_IPV6ADDR_ATTRIBUTE_FROM_BYTES_FAILED
void decodeHex(const string &encoded_str, vector< uint8_t > &output)
Decode a base16 encoded string into binary data.
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 format.
bool isPrintable(const string &content)
Check if a string is printable.
Defines the logger used by the top-level component of kea-lfc.