Kea 2.7.6
option_custom.cc
Go to the documentation of this file.
1// Copyright (C) 2012-2024 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#include <dhcp/libdhcp++.h>
10#include <dhcp/option_custom.h>
12#include <util/str.h>
13#include <util/encode/encode.h>
14
15using namespace isc::asiolink;
16using namespace isc::util;
17
18namespace isc {
19namespace dhcp {
20
22 Universe u)
23 : Option(u, def.getCode(), OptionBuffer()),
24 definition_(def) {
26 createBuffers();
27}
28
30 Universe u,
31 const OptionBuffer& data)
32 : Option(u, def.getCode(), data.begin(), data.end()),
33 definition_(def) {
35 createBuffers(getData());
36}
37
39 Universe u,
42 : Option(u, def.getCode(), first, last),
43 definition_(def) {
45 createBuffers(getData());
46}
47
52
53void
55 checkArrayType();
56
57 if ((address.isV4() && definition_.getType() != OPT_IPV4_ADDRESS_TYPE) ||
58 (address.isV6() && definition_.getType() != OPT_IPV6_ADDRESS_TYPE)) {
59 isc_throw(BadDataTypeCast, "invalid address specified "
60 << address << ". Expected a valid IPv"
61 << (definition_.getType() == OPT_IPV4_ADDRESS_TYPE ?
62 "4" : "6") << " address.");
63 }
64
65 OptionBuffer buf;
67 buffers_.push_back(buf);
68}
69
70void
71OptionCustom::addArrayDataField(const std::string& value) {
72 checkArrayType();
73
75 OptionBuffer buf;
76 OptionDataTypeUtil::writeTuple(value, lft, buf);
77 buffers_.push_back(buf);
78}
79
80void
82 checkArrayType();
83
84 OptionBuffer buf;
86 buffers_.push_back(buf);
87}
88
89void
91 checkArrayType();
92
93 OptionBuffer buf;
95 buffers_.push_back(buf);
96}
97
98void
100 const asiolink::IOAddress& prefix) {
101 checkArrayType();
102
103 if (definition_.getType() != OPT_IPV6_PREFIX_TYPE) {
104 isc_throw(BadDataTypeCast, "IPv6 prefix can be specified only for"
105 " an option comprising an array of IPv6 prefix values");
106 }
107
108 OptionBuffer buf;
109 OptionDataTypeUtil::writePrefix(prefix_len, prefix, buf);
110 buffers_.push_back(buf);
111}
112
113void
114OptionCustom::addArrayDataField(const PSIDLen& psid_len, const PSID& psid) {
115 checkArrayType();
116
117 if (definition_.getType() != OPT_PSID_TYPE) {
118 isc_throw(BadDataTypeCast, "PSID value can be specified onlu for"
119 " an option comprising an array of PSID length / value"
120 " tuples");
121 }
122
123 OptionBuffer buf;
124 OptionDataTypeUtil::writePsid(psid_len, psid, buf);
125 buffers_.push_back(buf);
126}
127
128void
129OptionCustom::checkIndex(const uint32_t index) const {
130 if (index >= buffers_.size()) {
131 isc_throw(isc::OutOfRange, "specified data field index " << index
132 << " is out of range.");
133 }
134}
135
136void
137OptionCustom::createBuffer(OptionBuffer& buffer,
138 const OptionDataType data_type) const {
139 // For data types that have a fixed size we can use the
140 // utility function to get the buffer's size.
141 size_t data_size = OptionDataTypeUtil::getDataTypeLen(data_type);
142
143 // For variable data sizes the utility function returns zero.
144 // It is ok for string values because the default string
145 // is 'empty'. However for FQDN the empty value is not valid
146 // so we initialize it to '.'. For prefix there is a prefix
147 // length fixed field.
148 if (data_size == 0) {
149 if (data_type == OPT_FQDN_TYPE) {
151
152 } else if (data_type == OPT_IPV6_PREFIX_TYPE) {
155 buffer);
156 }
157 } else {
158 // At this point we can resize the buffer. Note that
159 // for string values we are setting the empty buffer
160 // here.
161 buffer.resize(data_size);
162 }
163}
164
165void
166OptionCustom::createBuffers() {
167 definition_.validate();
168
169 std::vector<OptionBuffer> buffers;
170
171 OptionDataType data_type = definition_.getType();
172 // This function is called when an empty data buffer has been
173 // passed to the constructor. In such cases values for particular
174 // data fields will be set using modifier functions but for now
175 // we need to initialize a set of buffers that are specified
176 // for an option by its definition. Since there is no data yet,
177 // we are going to fill these buffers with default values.
178 if (data_type == OPT_RECORD_TYPE) {
179 // For record types we need to iterate over all data fields
180 // specified in option definition and create corresponding
181 // buffers for each of them.
183 definition_.getRecordFields();
184
185 for (auto const& field : fields) {
186 OptionBuffer buf;
187 createBuffer(buf, field);
188 // We have the buffer with default value prepared so we
189 // add it to the set of buffers.
190 buffers.push_back(buf);
191 }
192 } else if (!definition_.getArrayType() &&
193 data_type != OPT_EMPTY_TYPE) {
194 // For either 'empty' options we don't have to create any buffers
195 // for obvious reason. For arrays we also don't create any buffers
196 // yet because the set of fields that belong to the array is open
197 // ended so we can't allocate required buffers until we know how
198 // many of them are needed.
199 // For non-arrays we have a single value being held by the option
200 // so we have to allocate exactly one buffer.
201 OptionBuffer buf;
202 createBuffer(buf, data_type);
203 // Add a buffer that we have created and leave.
204 buffers.push_back(buf);
205 }
206 // The 'swap' is used here because we want to make sure that we
207 // don't touch buffers_ until we successfully allocate all
208 // buffers to be stored there.
209 std::swap(buffers, buffers_);
210}
211
212size_t
213OptionCustom::bufferLength(const OptionDataType data_type, bool in_array,
214 OptionBuffer::const_iterator begin,
215 OptionBuffer::const_iterator end) const {
216 // For fixed-size data type such as boolean, integer, even
217 // IP address we can use the utility function to get the required
218 // buffer size.
219 size_t data_size = OptionDataTypeUtil::getDataTypeLen(data_type);
220
221 // For variable size types (e.g. string) the function above will
222 // return 0 so we need to do a runtime check of the length.
223 if (data_size == 0) {
224 // FQDN is a special data type as it stores variable length data
225 // but the data length is encoded in the buffer. The easiest way
226 // to obtain the length of the data is to read the FQDN. The
227 // utility function will return the size of the buffer on success.
228 if (data_type == OPT_FQDN_TYPE) {
229 try {
230 std::string fqdn =
232 // The size of the buffer holding an FQDN is always
233 // 1 byte larger than the size of the string
234 // representation of this FQDN.
235 data_size = fqdn.size() + 1;
236 } catch (const std::exception& ex) {
238 isc_throw(SkipThisOptionError, "failed to read "
239 "domain-name from wire format: "
240 << ex.what());
241 }
242
243 throw;
244 }
245 } else if (!definition_.getArrayType() &&
246 ((data_type == OPT_BINARY_TYPE) ||
247 (data_type == OPT_STRING_TYPE))) {
248 // In other case we are dealing with string or binary value
249 // which size can't be determined. Thus we consume the
250 // remaining part of the buffer for it. Note that variable
251 // size data can be laid at the end of the option only and
252 // that the validate() function in OptionDefinition object
253 // should have checked wheter it is a case for this option.
254 data_size = std::distance(begin, end);
255 } else if (data_type == OPT_IPV6_PREFIX_TYPE) {
256 // The size of the IPV6 prefix type is determined as
257 // one byte (which is the size of the prefix in bits)
258 // followed by the prefix bits (right-padded with
259 // zeros to the nearest octet boundary)
260 if ((begin == end) && !in_array)
261 return 0;
262 PrefixTuple prefix =
264 // Data size comprises 1 byte holding a prefix length and the
265 // prefix length (in bytes) rounded to the nearest byte boundary.
266 data_size = sizeof(uint8_t) + (prefix.first.asUint8() + 7) / 8;
267 } else if (data_type == OPT_TUPLE_TYPE) {
270 std::string value =
272 data_size = value.size();
273 // The size of the buffer holding a tuple is always
274 // 1 or 2 byte larger than the size of the string
275 data_size += getUniverse() == Option::V4 ? 1 : 2;
276 } else {
277 // If we reached the end of buffer we assume that this option is
278 // truncated because there is no remaining data to initialize
279 // an option field.
280 isc_throw(OutOfRange, "option buffer truncated");
281 }
282 }
283
284 return data_size;
285}
286
287void
288OptionCustom::createBuffers(const OptionBuffer& data_buf) {
289 // Check that the option definition is correct as we are going
290 // to use it to split the data_ buffer into set of sub buffers.
291 definition_.validate();
292
293 std::vector<OptionBuffer> buffers;
294 OptionBuffer::const_iterator data = data_buf.begin();
295
296 OptionDataType data_type = definition_.getType();
297 if (data_type == OPT_RECORD_TYPE) {
298 // An option comprises a record of data fields. We need to
299 // get types of these data fields to allocate enough space
300 // for each buffer.
302 definition_.getRecordFields();
303
304 // Go over all data fields within a record.
305 for (auto const& field : fields) {
306 size_t data_size = bufferLength(field, false,
307 data, data_buf.end());
308
309 // Our data field requires that there is a certain chunk of
310 // data left in the buffer. If not, option is truncated.
311 if (std::distance(data, data_buf.end()) < data_size) {
312 isc_throw(OutOfRange, "option buffer truncated");
313 }
314
315 // Store the created buffer.
316 buffers.push_back(OptionBuffer(data, data + data_size));
317 // Proceed to the next data field.
318 data += data_size;
319 }
320
321 // Get extra buffers when the last field is an array.
322 if (definition_.getArrayType()) {
323 while (data != data_buf.end()) {
324 // Code copied from the standard array case
325 size_t data_size = bufferLength(fields.back(), true,
326 data, data_buf.end());
327 isc_throw_assert(data_size > 0);
328 if (std::distance(data, data_buf.end()) < data_size) {
329 break;
330 }
331 buffers.push_back(OptionBuffer(data, data + data_size));
332 data += data_size;
333 }
334 }
335
336 // Unpack suboptions if any.
337 else if (data != data_buf.end() && !getEncapsulatedSpace().empty()) {
338 unpackOptions(OptionBuffer(data, data_buf.end()));
339 }
340
341 } else if (data_type != OPT_EMPTY_TYPE) {
342 // If data_type value is other than OPT_RECORD_TYPE, our option is
343 // empty (have no data at all) or it comprises one or more
344 // data fields of the same type. The type of those fields
345 // is held in the data_type variable so let's use it to determine
346 // a size of buffers.
347 size_t data_size = OptionDataTypeUtil::getDataTypeLen(data_type);
348 // The check below will fail if the input buffer is too short
349 // for the data size being held by this option.
350 // Note that data_size returned by getDataTypeLen may be zero
351 // if variable length data is being held by the option but
352 // this will not cause this check to throw exception.
353 if (std::distance(data, data_buf.end()) < data_size) {
354 isc_throw(OutOfRange, "option buffer truncated");
355 }
356 // For an array of values we are taking different path because
357 // we have to handle multiple buffers.
358 if (definition_.getArrayType()) {
359 while (data != data_buf.end()) {
360 data_size = bufferLength(data_type, true, data, data_buf.end());
361 // We don't perform other checks for data types that can't be
362 // used together with array indicator such as strings, empty field
363 // etc. This is because OptionDefinition::validate function should
364 // have checked this already. Thus data_size must be greater than
365 // zero.
366 isc_throw_assert(data_size > 0);
367 // Get chunks of data and store as a collection of buffers.
368 // Truncate any remaining part which length is not divisible by
369 // data_size. Note that it is ok to truncate the data if and only
370 // if the data buffer is long enough to keep at least one value.
371 // This has been checked above already.
372 if (std::distance(data, data_buf.end()) < data_size) {
373 break;
374 }
375 buffers.push_back(OptionBuffer(data, data + data_size));
376 data += data_size;
377 }
378 } else {
379 // For non-arrays the data_size can be zero because
380 // getDataTypeLen returns zero for variable size data types
381 // such as strings. Simply take whole buffer.
382 data_size = bufferLength(data_type, false, data, data_buf.end());
383 if ((data_size > 0) && (std::distance(data, data_buf.end()) >= data_size)) {
384 buffers.push_back(OptionBuffer(data, data + data_size));
385 data += data_size;
386 } else {
387 isc_throw(OutOfRange, "option buffer truncated");
388 }
389
390 // Unpack suboptions if any.
391 if (data != data_buf.end() && !getEncapsulatedSpace().empty()) {
392 unpackOptions(OptionBuffer(data, data_buf.end()));
393 }
394 }
395 } else {
396 // Unpack suboptions if any.
397 if (data != data_buf.end() && !getEncapsulatedSpace().empty()) {
398 unpackOptions(OptionBuffer(data, data_buf.end()));
399 }
400 }
401 // If everything went ok we can replace old buffer set with new ones.
402 std::swap(buffers_, buffers);
403}
404
405std::string
406OptionCustom::dataFieldToText(const OptionDataType data_type,
407 const uint32_t index) const {
408 std::ostringstream text;
409
410 // Get the value of the data field.
411 switch (data_type) {
412 case OPT_BINARY_TYPE:
413 {
414 auto data = readBinary(index);
415 if (data.empty()) {
416 text << "''";
417 } else {
418 text << util::encode::encodeHex(data);
419 if (str::isPrintable(data)) {
420 std::string printable(data.cbegin(), data.cend());
421 text << " '" << printable << "'";
422 }
423 }
424 break;
425 }
426 case OPT_BOOLEAN_TYPE:
427 text << (readBoolean(index) ? "true" : "false");
428 break;
429 case OPT_INT8_TYPE:
430 text << static_cast<int>(readInteger<int8_t>(index));
431 break;
432 case OPT_INT16_TYPE:
433 text << readInteger<int16_t>(index);
434 break;
435 case OPT_INT32_TYPE:
436 text << readInteger<int32_t>(index);
437 break;
438 case OPT_UINT8_TYPE:
439 text << static_cast<unsigned>(readInteger<uint8_t>(index));
440 break;
441 case OPT_UINT16_TYPE:
442 text << readInteger<uint16_t>(index);
443 break;
444 case OPT_UINT32_TYPE:
445 text << readInteger<uint32_t>(index);
446 break;
449 text << readAddress(index);
450 break;
451 case OPT_FQDN_TYPE:
452 text << "\"" << readFqdn(index) << "\"";
453 break;
454 case OPT_TUPLE_TYPE:
455 text << "\"" << readTuple(index) << "\"";
456 break;
457 case OPT_STRING_TYPE:
458 text << "\"" << readString(index) << "\"";
459 break;
460 case OPT_PSID_TYPE:
461 {
462 PSIDTuple t = readPsid(index);
463 text << "len=" << t.first.asUnsigned() << ",psid=" << t.second.asUint16();
464 break;
465 }
466 default:
467 break;
468 }
469
470 // Append data field type in brackets.
471 text << " (" << OptionDataTypeUtil::getDataTypeName(data_type) << ")";
472
473 return (text.str());
474}
475
476void
478
479 // Pack DHCP header (V4 or V6).
480 packHeader(buf, check);
481
482 // Write data from buffers.
483 for (auto const& it : buffers_) {
484 // In theory the createBuffers function should have taken
485 // care that there are no empty buffers added to the
486 // collection but it is almost always good to make sure.
487 if (!it.empty()) {
488 buf.writeData(&it[0], it.size());
489 }
490 }
491
492 // Write suboptions.
493 packOptions(buf, check);
494}
495
496
498OptionCustom::readAddress(const uint32_t index) const {
499 checkIndex(index);
500
501 // The address being read can be either IPv4 or IPv6. The decision
502 // is made based on the buffer length. If it holds 4 bytes it is IPv4
503 // address, if it holds 16 bytes it is IPv6.
504 if (buffers_[index].size() == asiolink::V4ADDRESS_LEN) {
505 return (OptionDataTypeUtil::readAddress(buffers_[index], AF_INET));
506 } else if (buffers_[index].size() == asiolink::V6ADDRESS_LEN) {
507 return (OptionDataTypeUtil::readAddress(buffers_[index], AF_INET6));
508 } else {
509 isc_throw(BadDataTypeCast, "unable to read data from the buffer as"
510 << " IP address. Invalid buffer length "
511 << buffers_[index].size() << ".");
512 }
513}
514
515void
517 const uint32_t index) {
518 checkIndex(index);
519
520 if ((address.isV4() && buffers_[index].size() != V4ADDRESS_LEN) ||
521 (address.isV6() && buffers_[index].size() != V6ADDRESS_LEN)) {
522 isc_throw(BadDataTypeCast, "invalid address specified "
523 << address << ". Expected a valid IPv"
524 << (buffers_[index].size() == V4ADDRESS_LEN ? "4" : "6")
525 << " address.");
526 }
527
528 OptionBuffer buf;
530 std::swap(buf, buffers_[index]);
531}
532
533const OptionBuffer&
534OptionCustom::readBinary(const uint32_t index) const {
535 checkIndex(index);
536 return (buffers_[index]);
537}
538
539void
541 const uint32_t index) {
542 checkIndex(index);
543 buffers_[index] = buf;
544}
545
546std::string
547OptionCustom::readTuple(const uint32_t index) const {
548 checkIndex(index);
550 return (OptionDataTypeUtil::readTuple(buffers_[index], lft));
551}
552
553void
555 const uint32_t index) const {
556 checkIndex(index);
557 OptionDataTypeUtil::readTuple(buffers_[index], tuple);
558}
559
560void
561OptionCustom::writeTuple(const std::string& value, const uint32_t index) {
562 checkIndex(index);
563
564 buffers_[index].clear();
566 OptionDataTypeUtil::writeTuple(value, lft, buffers_[index]);
567}
568
569void
570OptionCustom::writeTuple(const OpaqueDataTuple& value, const uint32_t index) {
571 checkIndex(index);
572
573 buffers_[index].clear();
574 OptionDataTypeUtil::writeTuple(value, buffers_[index]);
575}
576
577bool
578OptionCustom::readBoolean(const uint32_t index) const {
579 checkIndex(index);
580 return (OptionDataTypeUtil::readBool(buffers_[index]));
581}
582
583void
584OptionCustom::writeBoolean(const bool value, const uint32_t index) {
585 checkIndex(index);
586
587 buffers_[index].clear();
588 OptionDataTypeUtil::writeBool(value, buffers_[index]);
589}
590
591std::string
592OptionCustom::readFqdn(const uint32_t index) const {
593 checkIndex(index);
594 return (OptionDataTypeUtil::readFqdn(buffers_[index]));
595}
596
597void
598OptionCustom::writeFqdn(const std::string& fqdn, const uint32_t index) {
599 checkIndex(index);
600
601 // Create a temporary buffer where the FQDN will be written.
602 OptionBuffer buf;
603 // Try to write to the temporary buffer rather than to the
604 // buffers_ member directly guarantees that we don't modify
605 // (clear) buffers_ until we are sure that the provided FQDN
606 // is valid.
608 // If we got to this point it means that the FQDN is valid.
609 // We can move the contents of the temporary buffer to the
610 // target buffer.
611 std::swap(buffers_[index], buf);
612}
613
615OptionCustom::readPrefix(const uint32_t index) const {
616 checkIndex(index);
617 return (OptionDataTypeUtil::readPrefix(buffers_[index]));
618}
619
620void
622 const IOAddress& prefix,
623 const uint32_t index) {
624 checkIndex(index);
625
626 OptionBuffer buf;
627 OptionDataTypeUtil::writePrefix(prefix_len, prefix, buf);
628 // If there are no errors while writing PSID to a buffer, we can
629 // replace the current buffer with a new buffer.
630 std::swap(buffers_[index], buf);
631}
632
633
635OptionCustom::readPsid(const uint32_t index) const {
636 checkIndex(index);
637 return (OptionDataTypeUtil::readPsid(buffers_[index]));
638}
639
640void
641OptionCustom::writePsid(const PSIDLen& psid_len, const PSID& psid,
642 const uint32_t index) {
643 checkIndex(index);
644
645 OptionBuffer buf;
646 OptionDataTypeUtil::writePsid(psid_len, psid, buf);
647 // If there are no errors while writing PSID to a buffer, we can
648 // replace the current buffer with a new buffer.
649 std::swap(buffers_[index], buf);
650}
651
652
653std::string
654OptionCustom::readString(const uint32_t index) const {
655 checkIndex(index);
656 return (OptionDataTypeUtil::readString(buffers_[index]));
657}
658
659void
660OptionCustom::writeString(const std::string& text, const uint32_t index) {
661 checkIndex(index);
662
663 // Let's clear a buffer as we want to replace the value of the
664 // whole buffer. If we fail to clear the buffer the data will
665 // be appended.
666 buffers_[index].clear();
667 // If the text value is empty we can leave because the buffer
668 // is already empty.
669 if (!text.empty()) {
670 OptionDataTypeUtil::writeString(text, buffers_[index]);
671 }
672}
673
674void
679
680uint16_t
682 // The length of the option is a sum of option header ...
683 size_t length = getHeaderLen();
684
685 // ... lengths of all buffers that hold option data ...
686 for (auto const& buf : buffers_) {
687 length += buf.size();
688 }
689
690 // ... and lengths of all suboptions
691 for (auto const& it : options_) {
692 length += it.second->len();
693 }
694
695 return (static_cast<uint16_t>(length));
696}
697
699 const OptionBufferConstIter last) {
700 setData(first, last);
701
702 // Chop the data_ buffer into set of buffers that represent
703 // option fields data.
704 createBuffers(getData());
705}
706
707std::string OptionCustom::toText(int indent) const {
708 std::stringstream output;
709
710 output << headerToText(indent) << ":";
711
712 OptionDataType data_type = definition_.getType();
713 if (data_type == OPT_RECORD_TYPE) {
715 definition_.getRecordFields();
716
717 // For record types we iterate over fields defined in
718 // option definition and match the appropriate buffer
719 // with them.
720 size_t j = 0;
721 for (auto const& field : fields) {
722 output << " " << dataFieldToText(field, j);
723 j++;
724 }
725
726 // If the last record field is an array iterate on extra buffers
727 if (definition_.getArrayType()) {
728 for (unsigned int i = fields.size(); i < getDataFieldsNum(); ++i) {
729 output << " " << dataFieldToText(fields.back(), i);
730 }
731 }
732 } else {
733 // For non-record types we iterate over all buffers
734 // and print the data type set globally for an option
735 // definition. We take the same code path for arrays
736 // and non-arrays as they only differ in such a way that
737 // non-arrays have just single data field.
738 for (unsigned int i = 0; i < getDataFieldsNum(); ++i) {
739 output << " " << dataFieldToText(definition_.getType(), i);
740 }
741 }
742
743 // Append suboptions.
744 output << suboptionsToText(indent + 2);
745
746 return (output.str());
747}
748
749} // end of isc::dhcp namespace
750} // end of isc namespace
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Exception to be thrown when cast to the data type was unsuccessful.
Represents a single instance of the opaque data preceded by length.
LengthFieldType
Size of the length field in the tuple.
std::string readString(const uint32_t index=0) const
Read a buffer as string value.
bool readBoolean(const uint32_t index=0) const
Read a buffer as boolean value.
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header)
std::string readTuple(const uint32_t index=0) const
Read a buffer as length and string tuple.
void writeFqdn(const std::string &fqdn, const uint32_t index=0)
Write an FQDN into a buffer.
std::string readFqdn(const uint32_t index=0) const
Read a buffer as FQDN.
void writePrefix(const PrefixLen &prefix_len, const asiolink::IOAddress &prefix, const uint32_t index=0)
Write prefix length and value into a buffer.
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
void writeAddress(const asiolink::IOAddress &address, const uint32_t index=0)
Write an IP address into a buffer.
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes DHCP option in a wire format to a buffer.
void initialize(const OptionBufferConstIter first, const OptionBufferConstIter last)
Sets content of this option from buffer.
const OptionBuffer & readBinary(const uint32_t index=0) const
Read a buffer as binary data.
PrefixTuple readPrefix(const uint32_t index=0) const
Read a buffer as variable length prefix.
void writePsid(const PSIDLen &psid_len, const PSID &psid, const uint32_t index=0)
Write PSID length / value into a buffer.
void writeBoolean(const bool value, const uint32_t index=0)
Write a boolean value into a buffer.
asiolink::IOAddress readAddress(const uint32_t index=0) const
Read a buffer as IP address.
PSIDTuple readPsid(const uint32_t index=0) const
Read a buffer as a PSID length / value tuple.
void writeString(const std::string &text, const uint32_t index=0)
Write a string value into a buffer.
T readInteger(const uint32_t index=0) const
Read a buffer as integer value.
void writeBinary(const OptionBuffer &buf, const uint32_t index=0)
Write binary data into a buffer.
void addArrayDataField(const asiolink::IOAddress &address)
Create new buffer and set its value as an IP address.
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
void writeTuple(const std::string &value, const uint32_t index=0)
Write a length and string tuple into a buffer.
virtual std::string toText(int indent=0) const
Returns string representation of the option.
OptionCustom(const OptionDefinition &def, Universe u)
Constructor, used for options to be sent.
uint32_t getDataFieldsNum() const
Return a number of the data fields.
static PrefixTuple readPrefix(const std::vector< uint8_t > &buf)
Read prefix from a buffer.
static asiolink::IOAddress readAddress(const std::vector< uint8_t > &buf, const short family)
Read IPv4 or IPv6 address from a buffer.
static void writeFqdn(const std::string &fqdn, std::vector< uint8_t > &buf, const bool downcase=false)
Append FQDN into a buffer.
static void writePrefix(const PrefixLen &prefix_len, const asiolink::IOAddress &prefix, std::vector< uint8_t > &buf)
Append prefix into a buffer.
static const std::string & getDataTypeName(const OptionDataType data_type)
Return option data type name from the data type enumerator.
static int getDataTypeLen(const OptionDataType data_type)
Get data type buffer length.
static std::string readFqdn(const std::vector< uint8_t > &buf)
Read FQDN from a buffer as a string value.
static std::string readTuple(const std::vector< uint8_t > &buf, OpaqueDataTuple::LengthFieldType lengthfieldtype)
Read length and string tuple from a buffer.
static void writeAddress(const asiolink::IOAddress &address, std::vector< uint8_t > &buf)
Append IPv4 or IPv6 address to a buffer.
static PSIDTuple readPsid(const std::vector< uint8_t > &buf)
Read PSID length / value tuple from a buffer.
static void writePsid(const PSIDLen &psid_len, const PSID &psid, std::vector< uint8_t > &buf)
Append PSID length/value into a buffer.
static void writeString(const std::string &value, std::vector< uint8_t > &buf)
Write UTF8-encoded string into a buffer.
static void writeTuple(const std::string &value, OpaqueDataTuple::LengthFieldType lengthfieldtype, std::vector< uint8_t > &buf)
Append length and string tuple to a buffer.
static OpaqueDataTuple::LengthFieldType getTupleLenFieldType(Option::Universe u)
Returns Length Field Type for a tuple.
static void writeBool(const bool value, std::vector< uint8_t > &buf)
Append boolean value into a buffer.
static bool readBool(const std::vector< uint8_t > &buf)
Read boolean value from a buffer.
static std::string readString(const std::vector< uint8_t > &buf)
Read string value from a buffer.
Base class representing a DHCP option definition.
OptionDataType getType() const
Return option data type.
std::vector< OptionDataType > RecordFieldsCollection
List of fields within the record.
const RecordFieldsCollection & getRecordFields() const
Return list of record fields.
void validate() const
Check if the option definition is valid.
std::string getEncapsulatedSpace() const
Return name of the encapsulated option space.
bool getArrayType() const
Return array type indicator.
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition option.cc:294
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition option.cc:313
static bool lenient_parsing_
Governs whether options should be parsed less strictly.
Definition option.h:490
std::string getEncapsulatedSpace() const
Returns the name of the option space encapsulated by this option.
Definition option.h:449
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition option.h:442
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition option.h:324
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition option.cc:327
Universe
defines option universe DHCPv4 or DHCPv6
Definition option.h:90
void unpackOptions(const OptionBuffer &buf)
Builds a collection of sub options from the buffer.
Definition option.cc:155
void packOptions(isc::util::OutputBuffer &buf, bool check=true) const
Store sub options in a buffer.
Definition option.cc:136
OptionCollection options_
collection for storing suboptions
Definition option.h:603
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition option.h:504
Universe getUniverse() const
returns option universe (V4 or V6)
Definition option.h:240
void packHeader(isc::util::OutputBuffer &buf, bool check=true) const
Store option's header in a buffer.
Definition option.cc:119
void check() const
A protected method used for option correctness.
Definition option.cc:90
Encapsulates PSID length.
Encapsulates PSID value.
Encapsulates prefix length.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:343
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition buffer.h:556
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define isc_throw_assert(expr)
Replacement for assert() that throws if the expression is false.
Definition isc_assert.h:18
std::pair< PSIDLen, PSID > PSIDTuple
Defines a pair of PSID length / value.
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition option.h:30
OptionDataType
Data types of DHCP option fields.
std::pair< PrefixLen, asiolink::IOAddress > PrefixTuple
Defines a pair of prefix length / value.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition option.h:24
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
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.