17#include <boost/lexical_cast.hpp>
39 if (def->vendor_ == 0) {
43 const vector<uint8_t> vsa_data = attr->toBytes();
48Attribute::fromText0(
const AttrDefPtr& def,
const string& value) {
49 switch (
static_cast<uint8_t
>(def->value_type_)) {
53 if (!isdigit(value[0])) {
58 return (
fromInt(def->type_, ic_def->value_));
73 <<
static_cast<unsigned>(def->value_type_));
80 if (bytes.size() < 2) {
83 uint8_t type = bytes[0];
84 uint8_t len = bytes[1];
86 if (len != bytes.size()) {
88 <<
" != " <<
static_cast<unsigned>(len));
90 vector<uint8_t> value;
91 value.resize(len - 2);
92 if (value.size() > 0) {
93 memmove(&value[0], &bytes[2], value.size());
111 if (def->vendor_ == 0) {
115 const vector<uint8_t> vsa_data = attr->toBytes();
120Attribute::fromBytes0(
const AttrDefPtr& def,
const vector<uint8_t>& value) {
121 switch (
static_cast<uint8_t
>(def->value_type_)) {
137 <<
static_cast<unsigned>(def->value_type_));
180 const std::string& value) {
186 const std::vector<uint8_t>& value) {
247 isc_throw(BadValue,
"value is empty");
249 if (value.size() > MAX_STRING_LEN) {
250 isc_throw(BadValue,
"value is too large " << value.size()
251 <<
" > " << MAX_STRING_LEN);
253 value_.resize(value.size());
254 memmove(&value_[0], &value[0], value_.size());
262 if (repr.size() > MAX_STRING_LEN) {
264 <<
" > " << MAX_STRING_LEN);
274 if (bytes.size() > MAX_STRING_LEN) {
276 <<
" > " << MAX_STRING_LEN);
283 ostringstream output;
284 for (
size_t i = 0; i < indent; i++) {
289 output <<
"'" << value_ <<
"'";
291 vector<uint8_t> binary;
292 binary.resize(value_.size());
293 if (binary.size() > 0) {
294 memmove(&binary[0], value_.c_str(), binary.size());
298 return (output.str());
303 vector<uint8_t> output;
307 if (output.size() > 2) {
308 memmove(&output[2], &value_[0], output.size() - 2);
315 vector<uint8_t> binary;
317 if (binary.size() > 0) {
318 memmove(&binary[0], &value_[0], binary.size());
334 vector<uint8_t> binary;
335 binary.resize(value_.size());
336 if (binary.size() > 0) {
337 memmove(&binary[0], value_.c_str(), binary.size());
351 int64_t val = boost::lexical_cast<int64_t>(repr);
352 if ((val < numeric_limits<int32_t>::min()) ||
353 (val > numeric_limits<uint32_t>::max())) {
368 if (bytes.size() != 4) {
370 msg <<
"bad value length " << bytes.size() <<
" != 4";
377 int32_t val = (
static_cast<int32_t
>(bytes[0])) << 24;
378 val |= (
static_cast<int32_t
>(bytes[1])) << 16;
379 val |= (
static_cast<int32_t
>(bytes[2])) << 8;
380 val |=
static_cast<int32_t
>(bytes[3]);
386 ostringstream output;
387 for (
size_t i = 0; i < indent; i++) {
393 output << ic_def->name_;
397 return (output.str());
402 vector<uint8_t> output;
406 output[2] =
static_cast<uint8_t
>((value_ & 0xff000000U) >> 24);
407 output[3] =
static_cast<uint8_t
>((value_ & 0xff0000U) >> 16);
408 output[4] =
static_cast<uint8_t
>((value_ & 0xff00U) >> 8);
409 output[5] =
static_cast<uint8_t
>(value_ & 0xffU);
443 if (bytes.size() != 4) {
445 msg <<
"bad value length " << bytes.size() <<
" != 4";
458 ostringstream output;
459 for (
size_t i = 0; i < indent; i++) {
464 return (output.str());
469 vector<uint8_t> output;
473 vector<uint8_t> binary = value_.toBytes();
474 memmove(&output[2], &binary[0], output.size() - 2);
487 val << value_.toText();
508 if (bytes.size() != 16) {
510 msg <<
"bad value length " << bytes.size() <<
" != 16";
523 ostringstream output;
524 for (
size_t i = 0; i < indent; i++) {
529 return (output.str());
534 vector<uint8_t> output;
538 vector<uint8_t> binary = value_.toBytes();
539 memmove(&output[2], &binary[0], output.size() - 2);
552 val << value_.toText();
560 auto pos = repr.find(
'/');
561 if ((pos == string::npos) ||
562 (pos == repr.size() - 1) ||
567 int len = boost::lexical_cast<int>(repr.substr(pos + 1));
568 if ((len < numeric_limits<uint8_t>::min()) ||
569 (len > numeric_limits<uint8_t>::max())) {
573 static_cast<uint8_t
>(len),
588 if (bytes.size() < 2) {
589 msg <<
"bad value length " << bytes.size() <<
" < 2";
590 }
else if (bytes.size() > 18) {
591 msg <<
"bad value length " << bytes.size() <<
" > 18";
592 }
else if (bytes[1] > 128) {
593 msg <<
"bad prefix length " <<
static_cast<unsigned>(bytes[1]) <<
" > 128";
595 string const msg_str(msg.str());
596 if (!msg_str.empty()) {
598 .arg(
static_cast<unsigned>(type))
603 uint8_t len = bytes[1];
604 vector<uint8_t> prefix(16);
605 if (bytes.size() > 2) {
606 memmove(&prefix[0], &bytes[2], bytes.size() - 2);
614 ostringstream output;
615 for (
size_t i = 0; i < indent; i++) {
619 << value_.toText() <<
"/" <<
static_cast<unsigned>(len_);
620 return (output.str());
625 vector<uint8_t> output;
632 vector<uint8_t> binary = value_.toBytes();
633 memmove(&output[4], &binary[0], output.size() - 4);
646 val << value_.toText() <<
"/" <<
static_cast<unsigned>(len_);
652 const vector<uint8_t>& value)
653 :
Attribute(type), vendor_(vendor), value_() {
655 isc_throw(BadValue,
"value is empty");
657 if (value.size() > MAX_VSA_DATA_LEN) {
658 isc_throw(BadValue,
"value is too large " << value.size()
659 <<
" > " << MAX_VSA_DATA_LEN);
661 value_.resize(value.size());
662 memmove(&value_[0], &value[0], value_.size());
675 if (bytes.size() < 5) {
677 }
else if (bytes.size() > MAX_STRING_LEN) {
679 <<
" > " << MAX_STRING_LEN);
681 uint32_t vendor = bytes[0] << 24;
682 vendor |= bytes[1] << 16;
683 vendor |= bytes[2] << 8;
685 vector<uint8_t> value;
686 value.resize(bytes.size() - 4);
687 if (value.size() > 0) {
688 memmove(&value[0], &bytes[4], value.size());
695 ostringstream output;
696 for (
size_t i = 0; i < indent; i++) {
701 vector<uint8_t> binary;
702 binary.resize(value_.size());
703 if (binary.size() > 0) {
704 memmove(&binary[0], value_.c_str(), binary.size());
707 return (output.str());
712 vector<uint8_t> output;
716 output[2] = (vendor_ & 0xff000000U) >> 24;
717 output[3] = (vendor_ & 0xff0000U) >> 16;
718 output[4] = (vendor_ & 0xff00U) >> 8;
719 output[5] = vendor_ & 0xffU;
720 if (output.size() > 6) {
721 memmove(&output[6], &value_[0], output.size() - 6);
728 vector<uint8_t> binary;
729 binary.resize(value_.size());
730 if (binary.size() > 0) {
731 memmove(&binary[0], &value_[0], binary.size());
744 ostringstream vendor;
747 vector<uint8_t> binary;
748 binary.resize(value_.size());
749 if (binary.size() > 0) {
750 memmove(&binary[0], value_.c_str(), binary.size());
763 static_cast<void>(
container_.push_back(attr));
765 static_cast<void>(
container_.push_front(attr));
772 auto it = idx.find(type);
773 if (it != idx.end()) {
782 for (
auto const& it : other) {
790 return (idx.count(type));
796 auto it = idx.find(type);
797 if (it != idx.end()) {
806 ostringstream output;
808 for (
auto const& it : *
this) {
814 output << it->toText(indent);
816 return (output.str());
822 for (
auto const& it : *
this) {
823 output->add(it->toElement());
834 for (
auto const& entry : attr_list->listValue()) {
849 (!raw->stringValue().empty())) {
851 vector<uint8_t> binary;
863 (!
data->stringValue().empty())) {
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
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.
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.
void add(const ConstAttributePtr &attr, bool back=true)
Adds instance of the attribute to the 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.