Kea 3.1.3
client_attribute.cc
Go to the documentation of this file.
1// Copyright (C) 2023-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 <client_attribute.h>
10#include <radius_log.h>
11#include <util/encode/encode.h>
12#include <util/str.h>
13#include <boost/lexical_cast.hpp>
14#include <cctype>
15#include <limits>
16#include <sstream>
17
18using namespace isc;
19using namespace isc::asiolink;
20using namespace isc::data;
21using namespace isc::util;
22using namespace std;
23
24namespace isc {
25namespace radius {
26
28Attribute::fromText(const AttrDefPtr& def, const string& value) {
29 if (!def) {
30 isc_throw(BadValue, "null attribute definition");
31 }
32 if (value.empty()) {
33 isc_throw(BadValue, "empty attribute value");
34 }
35 AttributePtr attr = fromText0(def, value);
36 if (def->vendor_ == 0) {
37 return (attr);
38 }
39 // Encapsulate into a Vendor-Specific attribute.
40 const vector<uint8_t> vsa_data = attr->toBytes();
41 return (fromVsa(PW_VENDOR_SPECIFIC, def->vendor_, vsa_data));
42}
43
45Attribute::fromText0(const AttrDefPtr& def, const string& value) {
46 switch (static_cast<uint8_t>(def->value_type_)) {
47 case PW_TYPE_STRING:
48 return (AttrString::fromText(def->type_, value));
49 case PW_TYPE_INTEGER:
50 if (!isdigit(value[0])) {
51 IntCstDefPtr ic_def =
52 AttrDefs::instance().getByName(def->type_, value,
53 def->vendor_);
54 if (ic_def) {
55 return (fromInt(def->type_, ic_def->value_));
56 }
57 }
58 return (AttrInt::fromText(def->type_, value));
59 case PW_TYPE_IPADDR:
60 return (AttrIpAddr::fromText(def->type_, value));
62 return (AttrIpv6Addr::fromText(def->type_, value));
64 return (AttrIpv6Prefix::fromText(def->type_, value));
65 case PW_TYPE_VSA:
66 return (AttrVsa::fromText(def->type_, value));
67 default:
68 // Impossible case.
69 isc_throw(OutOfRange, "unknown value type "
70 << static_cast<unsigned>(def->value_type_));
71 }
72}
73
75Attribute::fromBytes(const vector<uint8_t>& bytes) {
76 // Checked by caller.
77 if (bytes.size() < 2) {
78 isc_throw(BadValue, "too short byte argument");
79 }
80 uint8_t type = bytes[0];
81 uint8_t len = bytes[1];
82 // Checked by caller.
83 if (len != bytes.size()) {
84 isc_throw(BadValue, "bad byte argument length " << bytes.size()
85 << " != " << static_cast<unsigned>(len));
86 }
87 vector<uint8_t> value;
88 value.resize(len - 2);
89 if (value.size() > 0) {
90 memmove(&value[0], &bytes[2], value.size());
91 }
93 if (!def) {
94 return (AttributePtr());
95 }
96 return (Attribute::fromBytes(def, value));
97}
98
100Attribute::fromBytes(const AttrDefPtr& def, const vector<uint8_t>& value) {
101 if (!def) {
102 isc_throw(BadValue, "null attribute definition");
103 }
104 if (value.empty()) {
105 isc_throw(BadValue, "empty attribute value");
106 }
107 AttributePtr attr = fromBytes0(def, value);
108 if (def->vendor_ == 0) {
109 return (attr);
110 }
111 // Encapsulate into a Vendor-Specific attribute.
112 const vector<uint8_t> vsa_data = attr->toBytes();
113 return (fromVsa(PW_VENDOR_SPECIFIC, def->vendor_, vsa_data));
114}
115
117Attribute::fromBytes0(const AttrDefPtr& def, const vector<uint8_t>& value) {
118 switch (static_cast<uint8_t>(def->value_type_)) {
119 case PW_TYPE_STRING:
120 return (AttrString::fromBytes(def->type_, value));
121 case PW_TYPE_INTEGER:
122 return (AttrInt::fromBytes(def->type_, value));
123 case PW_TYPE_IPADDR:
124 return (AttrIpAddr::fromBytes(def->type_, value));
125 case PW_TYPE_IPV6ADDR:
126 return (AttrIpv6Addr::fromBytes(def->type_, value));
128 return (AttrIpv6Prefix::fromBytes(def->type_, value));
129 case PW_TYPE_VSA:
130 return (AttrVsa::fromBytes(def->type_, value));
131 default:
132 // Impossible case.
133 isc_throw(OutOfRange, "unknown value type "
134 << static_cast<unsigned>(def->value_type_));
135 }
136}
137
139Attribute::fromString(const uint8_t type, const string& value) {
140 if (value.empty()) {
141 isc_throw(BadValue, "empty attribute value");
142 }
143 return (AttributePtr(new AttrString(type, value)));
144}
145
147Attribute::fromBinary(const uint8_t type, const vector<uint8_t>& value) {
148 if (value.empty()) {
149 isc_throw(BadValue, "empty attribute value");
150 }
151 return (AttributePtr(new AttrString(type, value)));
152}
153
155Attribute::fromInt(const uint8_t type, const uint32_t value) {
156 return (AttributePtr(new AttrInt(type, value)));
157}
158
160Attribute::fromIpAddr(const uint8_t type, const IOAddress& value) {
161 return (AttributePtr(new AttrIpAddr(type, value)));
162}
163
165Attribute::fromIpv6Addr(const uint8_t type, const IOAddress& value) {
166 return (AttributePtr(new AttrIpv6Addr(type, value)));
167}
168
170Attribute::fromIpv6Prefix(const uint8_t type, const uint8_t len,
171 const IOAddress& value) {
172 return (AttributePtr(new AttrIpv6Prefix(type, len, value)));
173}
174
176Attribute::fromVsa(const uint8_t type, const uint32_t vendor,
177 const std::string& value) {
178 return (AttributePtr(new AttrVsa(type, vendor, value)));
179}
180
182Attribute::fromVsa(const uint8_t type, const uint32_t vendor,
183 const std::vector<uint8_t>& value) {
184 return (AttributePtr(new AttrVsa(type, vendor, value)));
185}
186
187string
189 isc_throw(TypeError, "the attribute value type must be string, not "
191}
192
193vector<uint8_t>
195 isc_throw(TypeError, "the attribute value type must be string, not "
197}
198
199uint32_t
201 isc_throw(TypeError, "the attribute value type must be integer, not "
203}
204
207 isc_throw(TypeError, "the attribute value type must be ipaddr, not "
209}
210
213 isc_throw(TypeError, "the attribute value type must be ipv6addr, not "
215}
216
219 isc_throw(TypeError, "the attribute value type must be ipv6prefix, not "
221}
222
223uint8_t
225 isc_throw(TypeError, "the attribute value type must be ipv6prefix, not "
227}
228
229uint32_t
231 isc_throw(TypeError, "the attribute value type must be vsa, not "
233}
234
235std::vector<uint8_t>
237 isc_throw(TypeError, "the attribute value type must be vsa, not "
239}
240
241AttrString::AttrString(const uint8_t type, const vector<uint8_t>& value)
242 : Attribute(type), value_() {
243 if (value.empty()) {
244 isc_throw(BadValue, "value is empty");
245 }
246 if (value.size() > MAX_STRING_LEN) {
247 isc_throw(BadValue, "value is too large " << value.size()
248 << " > " << MAX_STRING_LEN);
249 }
250 value_.resize(value.size());
251 memmove(&value_[0], &value[0], value_.size());
252}
253
255AttrString::fromText(const uint8_t type, const string& repr) {
256 if (repr.empty()) {
257 isc_throw(BadValue, "empty attribute value");
258 }
259 if (repr.size() > MAX_STRING_LEN) {
260 isc_throw(BadValue, "value is too large " << repr.size()
261 << " > " << MAX_STRING_LEN);
262 }
263 return (AttributePtr(new AttrString(type, repr)));
264}
265
267AttrString::fromBytes(const uint8_t type, const vector<uint8_t>& bytes) {
268 if (bytes.empty()) {
269 isc_throw(BadValue, "empty attribute value");
270 }
271 if (bytes.size() > MAX_STRING_LEN) {
272 isc_throw(BadValue, "value is too large " << bytes.size()
273 << " > " << MAX_STRING_LEN);
274 }
275 return (AttributePtr(new AttrString(type, bytes)));
276}
277
278string
279AttrString::toText(size_t indent) const {
280 ostringstream output;
281 for (size_t i = 0; i < indent; i++) {
282 output << " ";
283 }
284 output << AttrDefs::instance().getName(getType()) << '=';
285 if (str::isPrintable(value_)) {
286 output << "'" << value_ << "'";
287 } else {
288 vector<uint8_t> binary;
289 binary.resize(value_.size());
290 if (binary.size() > 0) {
291 memmove(&binary[0], value_.c_str(), binary.size());
292 }
293 output << "0x" << encode::encodeHex(binary);
294 }
295 return (output.str());
296}
297
298vector<uint8_t>
300 vector<uint8_t> output;
301 output.resize(2 + getValueLen());
302 output[0] = getType();
303 output[1] = 2 + getValueLen();
304 if (output.size() > 2) {
305 memmove(&output[2], &value_[0], output.size() - 2);
306 }
307 return (output);
308}
309
310vector<uint8_t>
312 vector<uint8_t> binary;
313 binary.resize(getValueLen());
314 if (binary.size() > 0) {
315 memmove(&binary[0], &value_[0], binary.size());
316 }
317 return (binary);
318}
319
324 if (def) {
325 output->set("name", Element::create(def->name_));
326 }
327 output->set("type", Element::create(static_cast<int>(getType())));
328 if (str::isPrintable(value_)) {
329 output->set("data", Element::create(value_));
330 } else {
331 vector<uint8_t> binary;
332 binary.resize(value_.size());
333 if (binary.size() > 0) {
334 memmove(&binary[0], value_.c_str(), binary.size());
335 }
336 string raw = encode::encodeHex(binary);
337 output->set("raw", Element::create(raw));
338 }
339 return (output);
340}
341
343AttrInt::fromText(const uint8_t type, const string& repr) {
344 if (repr.empty()) {
345 isc_throw(BadValue, "empty attribute value");
346 }
347 try {
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())) {
351 isc_throw(BadValue, "not 32 bit " << repr);
352 }
353 return (AttributePtr(new AttrInt(type, static_cast<uint32_t>(val))));
354 } catch (...) {
356 .arg(unsigned(type))
357 .arg(AttrDefs::instance().getName(type))
358 .arg(repr);
359 throw;
360 }
361}
362
364AttrInt::fromBytes(const uint8_t type, const vector<uint8_t>& bytes) {
365 if (bytes.size() != 4) {
366 ostringstream msg;
367 msg << "bad value length " << bytes.size() << " != 4";
369 .arg(unsigned(type))
370 .arg(AttrDefs::instance().getName(type))
371 .arg(msg.str());
372 isc_throw(BadValue, msg.str());
373 }
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]);
378 return (AttributePtr(new AttrInt(type, val)));
379}
380
381string
382AttrInt::toText(size_t indent) const {
383 ostringstream output;
384 for (size_t i = 0; i < indent; i++) {
385 output << " ";
386 }
387 output << AttrDefs::instance().getName(getType()) << '=';
389 if (ic_def) {
390 output << ic_def->name_;
391 } else {
392 output << value_;
393 }
394 return (output.str());
395}
396
397vector<uint8_t>
399 vector<uint8_t> output;
400 output.resize(6);
401 output[0] = getType();
402 output[1] = 6;
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);
407 return (output);
408}
409
414 if (def) {
415 output->set("name", Element::create(def->name_));
416 }
417 output->set("type", Element::create(static_cast<int>(getType())));
418 ostringstream val;
419 val << value_;
420 output->set("data", Element::create(val.str()));
421 return (output);
422}
423
425AttrIpAddr::fromText(const uint8_t type, const string& repr) {
426 try {
427 IOAddress val(repr);
428 return (AttributePtr(new AttrIpAddr(type, val)));
429 } catch (...) {
431 .arg(unsigned(type))
432 .arg(AttrDefs::instance().getName(type))
433 .arg(repr);
434 throw;
435 }
436}
437
439AttrIpAddr::fromBytes(const uint8_t type, const vector<uint8_t>& bytes) {
440 if (bytes.size() != 4) {
441 ostringstream msg;
442 msg << "bad value length " << bytes.size() << " != 4";
444 .arg(unsigned(type))
445 .arg(AttrDefs::instance().getName(type))
446 .arg(msg.str());
447 isc_throw(BadValue, msg.str());
448 }
449 IOAddress val = IOAddress::fromBytes(AF_INET, &bytes[0]);
450 return (AttributePtr(new AttrIpAddr(type, val)));
451}
452
453string
454AttrIpAddr::toText(size_t indent) const {
455 ostringstream output;
456 for (size_t i = 0; i < indent; i++) {
457 output << " ";
458 }
459 output << AttrDefs::instance().getName(getType()) << '='
460 << value_.toText();
461 return (output.str());
462}
463
464vector<uint8_t>
466 vector<uint8_t> output;
467 output.resize(6);
468 output[0] = getType();
469 output[1] = 6;
470 vector<uint8_t> binary = value_.toBytes();
471 memmove(&output[2], &binary[0], output.size() - 2);
472 return (output);
473}
474
479 if (def) {
480 output->set("name", Element::create(def->name_));
481 }
482 output->set("type", Element::create(static_cast<int>(getType())));
483 ostringstream val;
484 val << value_.toText();
485 output->set("data", Element::create(val.str()));
486 return (output);
487}
488
490AttrIpv6Addr::fromText(const uint8_t type, const string& repr) {
491 try {
492 IOAddress val(repr);
493 return (AttributePtr(new AttrIpv6Addr(type, val)));
494 } catch (...) {
496 .arg(unsigned(type))
497 .arg(AttrDefs::instance().getName(type))
498 .arg(repr);
499 throw;
500 }
501}
502
504AttrIpv6Addr::fromBytes(const uint8_t type, const vector<uint8_t>& bytes) {
505 if (bytes.size() != 16) {
506 ostringstream msg;
507 msg << "bad value length " << bytes.size() << " != 16";
509 .arg(unsigned(type))
510 .arg(AttrDefs::instance().getName(type))
511 .arg(msg.str());
512 isc_throw(BadValue, msg.str());
513 }
514 IOAddress val = IOAddress::fromBytes(AF_INET6, &bytes[0]);
515 return (AttributePtr(new AttrIpv6Addr(type, val)));
516}
517
518string
519AttrIpv6Addr::toText(size_t indent) const {
520 ostringstream output;
521 for (size_t i = 0; i < indent; i++) {
522 output << " ";
523 }
524 output << AttrDefs::instance().getName(getType()) << '='
525 << value_.toText();
526 return (output.str());
527}
528
529vector<uint8_t>
531 vector<uint8_t> output;
532 output.resize(18);
533 output[0] = getType();
534 output[1] = 18;
535 vector<uint8_t> binary = value_.toBytes();
536 memmove(&output[2], &binary[0], output.size() - 2);
537 return (output);
538}
539
544 if (def) {
545 output->set("name", Element::create(def->name_));
546 }
547 output->set("type", Element::create(static_cast<int>(getType())));
548 ostringstream val;
549 val << value_.toText();
550 output->set("data", Element::create(val.str()));
551 return (output);
552}
553
555AttrIpv6Prefix::fromText(const uint8_t type, const string& repr) {
556 try {
557 auto pos = repr.find('/');
558 if ((pos == string::npos) ||
559 (pos == repr.size() - 1) ||
560 (pos == 0)) {
561 isc_throw(BadValue, "unable to parse prefix " << repr);
562 }
563 IOAddress val(repr.substr(0, pos));
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())) {
567 isc_throw(BadValue, "not 8 bit prefix length " << repr);
568 }
569 return (AttributePtr(new AttrIpv6Prefix(type,
570 static_cast<uint8_t>(len),
571 val)));
572 } catch (...) {
574 .arg(unsigned(type))
575 .arg(AttrDefs::instance().getName(type))
576 .arg(repr);
577 throw;
578 }
579}
580
582AttrIpv6Prefix::fromBytes(const uint8_t type, const vector<uint8_t>& bytes) {
584 ostringstream msg;
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";
591 }
592 string const msg_str(msg.str());
593 if (!msg_str.empty()) {
595 .arg(static_cast<unsigned>(type))
596 .arg(AttrDefs::instance().getName(type))
597 .arg(msg.str());
598 isc_throw(BadValue, msg_str);
599 }
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);
604 }
605 IOAddress val = IOAddress::fromBytes(AF_INET6, &prefix[0]);
606 return (AttributePtr(new AttrIpv6Prefix(type, len, val)));
607}
608
609string
610AttrIpv6Prefix::toText(size_t indent) const {
611 ostringstream output;
612 for (size_t i = 0; i < indent; i++) {
613 output << " ";
614 }
615 output << AttrDefs::instance().getName(getType()) << '='
616 << value_.toText() << "/" << static_cast<unsigned>(len_);
617 return (output.str());
618}
619
620vector<uint8_t>
622 vector<uint8_t> output;
623 // type(1) + length(1) + reserved(1) + prefix_len(1) + prefix(16)
624 output.resize(20);
625 output[0] = getType();
626 output[1] = 20;
627 output[2] = 0;
628 output[3] = len_;
629 vector<uint8_t> binary = value_.toBytes();
630 memmove(&output[4], &binary[0], output.size() - 4);
631 return (output);
632}
633
638 if (def) {
639 output->set("name", Element::create(def->name_));
640 }
641 output->set("type", Element::create(static_cast<int>(getType())));
642 ostringstream val;
643 val << value_.toText() << "/" << static_cast<unsigned>(len_);
644 output->set("data", Element::create(val.str()));
645 return (output);
646}
647
648AttrVsa::AttrVsa(const uint8_t type, const uint32_t vendor,
649 const vector<uint8_t>& value)
650 : Attribute(type), vendor_(vendor), value_() {
651 if (value.empty()) {
652 isc_throw(BadValue, "value is empty");
653 }
654 if (value.size() > MAX_VSA_DATA_LEN) {
655 isc_throw(BadValue, "value is too large " << value.size()
656 << " > " << MAX_VSA_DATA_LEN);
657 }
658 value_.resize(value.size());
659 memmove(&value_[0], &value[0], value_.size());
660}
661
663AttrVsa::fromText(const uint8_t /* type */, const string& /* repr */) {
664 isc_throw(NotImplemented, "Can't decode vsa from text");
665}
666
668AttrVsa::fromBytes(const uint8_t type, const vector<uint8_t>& bytes) {
669 if (bytes.empty()) {
670 isc_throw(BadValue, "empty attribute value");
671 }
672 if (bytes.size() < 5) {
673 isc_throw(BadValue, "value is too small " << bytes.size() << " < 5");
674 } else if (bytes.size() > MAX_STRING_LEN) {
675 isc_throw(BadValue, "value is too large " << bytes.size()
676 << " > " << MAX_STRING_LEN);
677 }
678 uint32_t vendor = bytes[0] << 24;
679 vendor |= bytes[1] << 16;
680 vendor |= bytes[2] << 8;
681 vendor |= bytes[3];
682 vector<uint8_t> value;
683 value.resize(bytes.size() - 4);
684 if (value.size() > 0) {
685 memmove(&value[0], &bytes[4], value.size());
686 }
687 return (AttributePtr(new AttrVsa(type, vendor, value)));
688}
689
690string
691AttrVsa::toText(size_t indent) const {
692 ostringstream output;
693 for (size_t i = 0; i < indent; i++) {
694 output << " ";
695 }
696 output << AttrDefs::instance().getName(getType()) << "=["
697 << vendor_ << "]";
698 vector<uint8_t> binary;
699 binary.resize(value_.size());
700 if (binary.size() > 0) {
701 memmove(&binary[0], value_.c_str(), binary.size());
702 }
703 output << "0x" << encode::encodeHex(binary);
704 return (output.str());
705}
706
707std::vector<uint8_t>
709 vector<uint8_t> output;
710 output.resize(2 + getValueLen());
711 output[0] = getType();
712 output[1] = 2 + getValueLen();
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);
719 }
720 return (output);
721}
722
723std::vector<uint8_t>
725 vector<uint8_t> binary;
726 binary.resize(value_.size());
727 if (binary.size() > 0) {
728 memmove(&binary[0], &value_[0], binary.size());
729 }
730 return (binary);
731}
732
737 if (def) {
738 output->set("name", Element::create(def->name_));
739 }
740 output->set("type", Element::create(static_cast<int>(getType())));
741 ostringstream vendor;
742 vendor << vendor_;
743 output->set("vendor", Element::create(vendor.str()));
744 vector<uint8_t> binary;
745 binary.resize(value_.size());
746 if (binary.size() > 0) {
747 memmove(&binary[0], value_.c_str(), binary.size());
748 }
749 string raw = encode::encodeHex(binary);
750 output->set("vsa-raw", Element::create(raw));
751 return (output);
752}
753
754void
756 if (!attr) {
757 return;
758 }
759 static_cast<void>(container_.push_back(attr));
760}
761
762bool
763Attributes::del(const uint8_t type) {
764 auto& idx = container_.get<1>();
765 auto it = idx.find(type);
766 if (it != idx.end()) {
767 idx.erase(it);
768 return (true);
769 }
770 return (false);
771}
772
773void
775 for (auto const& it : other) {
776 add(it);
777 }
778}
779
780size_t
781Attributes::count(const uint8_t type) const {
782 auto const& idx = container_.get<1>();
783 return (idx.count(type));
784}
785
787Attributes::get(const uint8_t type) const {
788 auto const& idx = container_.get<1>();
789 auto it = idx.find(type);
790 if (it != idx.end()) {
791 return (*it);
792 } else {
793 return(ConstAttributePtr());
794 }
795}
796
797string
798Attributes::toText(size_t indent) const {
799 ostringstream output;
800 bool first = true;
801 for (auto const& it : *this) {
802 if (!first) {
803 output << ",\n";
804 } else {
805 first = false;
806 }
807 output << it->toText(indent);
808 }
809 return (output.str());
810}
811
815 for (auto const& it : *this) {
816 output->add(it->toElement());
817 }
818 return (output);
819}
820
823 Attributes attrs;
824 if (!attr_list || (attr_list->getType() != Element::list)) {
825 return (attrs);
826 }
827 for (auto const& entry : attr_list->listValue()) {
828 if (!entry || (entry->getType() != Element::map)) {
829 continue;
830 }
831 ConstElementPtr attr_type = entry->get("type");
832 if (!attr_type || (attr_type->getType() != Element::integer)) {
833 continue;
834 }
835 AttrDefPtr def = AttrDefs::instance().getByType(attr_type->intValue());
836 if (!def) {
837 continue;
838 }
839 if (def->value_type_ == PW_TYPE_STRING) {
840 ConstElementPtr raw = entry->get("raw");
841 if (raw && (raw->getType() == Element::string) &&
842 (!raw->stringValue().empty())) {
843 try {
844 vector<uint8_t> binary;
845 encode::decodeHex(raw->stringValue(), binary);
846 AttributePtr attr = Attribute::fromBytes(def, binary);
847 attrs.add(attr);
848 continue;
849 } catch (...) {
850 continue;
851 }
852 }
853 }
854 ConstElementPtr data = entry->get("data");
855 if (data && (data->getType() == Element::string) &&
856 (!data->stringValue().empty())) {
857 AttributePtr attr = Attribute::fromText(def, data->stringValue());
858 attrs.add(attr);
859 continue;
860 }
861 }
862 return (attrs);
863}
864
865} // end of namespace isc::radius
866} // end of namespace isc
if(!(yy_init))
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
@ map
Definition data.h:147
@ integer
Definition data.h:140
@ list
Definition data.h:146
@ string
Definition data.h:144
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...
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...
A standard Data module exception that is thrown if a function is called for an Element that has a wro...
Definition data.h:36
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.
Definition macros.h:32
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
boost::shared_ptr< IntCstDef > IntCstDefPtr
Shared pointers to Integer constant definition.
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.
Definition radius_log.h:35
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.
Definition encode.cc:367
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 format.
Definition encode.cc:361
bool isPrintable(const string &content)
Check if a string is printable.
Definition str.cc:310
Defines the logger used by the top-level component of kea-lfc.