Kea 2.7.1
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) {
25 setEncapsulatedSpace(def.getEncapsulatedSpace());
26 createBuffers();
27}
28
30 Universe u,
31 const OptionBuffer& data)
32 : Option(u, def.getCode(), data.begin(), data.end()),
33 definition_(def) {
34 setEncapsulatedSpace(def.getEncapsulatedSpace());
35 createBuffers(getData());
36}
37
39 Universe u,
42 : Option(u, def.getCode(), first, last),
43 definition_(def) {
44 setEncapsulatedSpace(def.getEncapsulatedSpace());
45 createBuffers(getData());
46}
47
50 return (cloneInternal<OptionCustom>());
51}
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 std::string fqdn =
231 // The size of the buffer holding an FQDN is always
232 // 1 byte larger than the size of the string
233 // representation of this FQDN.
234 data_size = fqdn.size() + 1;
235 } else if (!definition_.getArrayType() &&
236 ((data_type == OPT_BINARY_TYPE) ||
237 (data_type == OPT_STRING_TYPE))) {
238 // In other case we are dealing with string or binary value
239 // which size can't be determined. Thus we consume the
240 // remaining part of the buffer for it. Note that variable
241 // size data can be laid at the end of the option only and
242 // that the validate() function in OptionDefinition object
243 // should have checked wheter it is a case for this option.
244 data_size = std::distance(begin, end);
245 } else if (data_type == OPT_IPV6_PREFIX_TYPE) {
246 // The size of the IPV6 prefix type is determined as
247 // one byte (which is the size of the prefix in bits)
248 // followed by the prefix bits (right-padded with
249 // zeros to the nearest octet boundary)
250 if ((begin == end) && !in_array)
251 return 0;
252 PrefixTuple prefix =
254 // Data size comprises 1 byte holding a prefix length and the
255 // prefix length (in bytes) rounded to the nearest byte boundary.
256 data_size = sizeof(uint8_t) + (prefix.first.asUint8() + 7) / 8;
257 } else if (data_type == OPT_TUPLE_TYPE) {
260 std::string value =
262 data_size = value.size();
263 // The size of the buffer holding a tuple is always
264 // 1 or 2 byte larger than the size of the string
265 data_size += getUniverse() == Option::V4 ? 1 : 2;
266 } else {
267 // If we reached the end of buffer we assume that this option is
268 // truncated because there is no remaining data to initialize
269 // an option field.
270 isc_throw(OutOfRange, "option buffer truncated");
271 }
272 }
273
274 return data_size;
275}
276
277void
278OptionCustom::createBuffers(const OptionBuffer& data_buf) {
279 // Check that the option definition is correct as we are going
280 // to use it to split the data_ buffer into set of sub buffers.
281 definition_.validate();
282
283 std::vector<OptionBuffer> buffers;
284 OptionBuffer::const_iterator data = data_buf.begin();
285
286 OptionDataType data_type = definition_.getType();
287 if (data_type == OPT_RECORD_TYPE) {
288 // An option comprises a record of data fields. We need to
289 // get types of these data fields to allocate enough space
290 // for each buffer.
292 definition_.getRecordFields();
293
294 // Go over all data fields within a record.
295 for (auto const& field : fields) {
296 size_t data_size = bufferLength(field, false,
297 data, data_buf.end());
298
299 // Our data field requires that there is a certain chunk of
300 // data left in the buffer. If not, option is truncated.
301 if (std::distance(data, data_buf.end()) < data_size) {
302 isc_throw(OutOfRange, "option buffer truncated");
303 }
304
305 // Store the created buffer.
306 buffers.push_back(OptionBuffer(data, data + data_size));
307 // Proceed to the next data field.
308 data += data_size;
309 }
310
311 // Get extra buffers when the last field is an array.
312 if (definition_.getArrayType()) {
313 while (data != data_buf.end()) {
314 // Code copied from the standard array case
315 size_t data_size = bufferLength(fields.back(), true,
316 data, data_buf.end());
317 isc_throw_assert(data_size > 0);
318 if (std::distance(data, data_buf.end()) < data_size) {
319 break;
320 }
321 buffers.push_back(OptionBuffer(data, data + data_size));
322 data += data_size;
323 }
324 }
325
326 // Unpack suboptions if any.
327 else if (data != data_buf.end() && !getEncapsulatedSpace().empty()) {
328 unpackOptions(OptionBuffer(data, data_buf.end()));
329 }
330
331 } else if (data_type != OPT_EMPTY_TYPE) {
332 // If data_type value is other than OPT_RECORD_TYPE, our option is
333 // empty (have no data at all) or it comprises one or more
334 // data fields of the same type. The type of those fields
335 // is held in the data_type variable so let's use it to determine
336 // a size of buffers.
337 size_t data_size = OptionDataTypeUtil::getDataTypeLen(data_type);
338 // The check below will fail if the input buffer is too short
339 // for the data size being held by this option.
340 // Note that data_size returned by getDataTypeLen may be zero
341 // if variable length data is being held by the option but
342 // this will not cause this check to throw exception.
343 if (std::distance(data, data_buf.end()) < data_size) {
344 isc_throw(OutOfRange, "option buffer truncated");
345 }
346 // For an array of values we are taking different path because
347 // we have to handle multiple buffers.
348 if (definition_.getArrayType()) {
349 while (data != data_buf.end()) {
350 data_size = bufferLength(data_type, true, data, data_buf.end());
351 // We don't perform other checks for data types that can't be
352 // used together with array indicator such as strings, empty field
353 // etc. This is because OptionDefinition::validate function should
354 // have checked this already. Thus data_size must be greater than
355 // zero.
356 isc_throw_assert(data_size > 0);
357 // Get chunks of data and store as a collection of buffers.
358 // Truncate any remaining part which length is not divisible by
359 // data_size. Note that it is ok to truncate the data if and only
360 // if the data buffer is long enough to keep at least one value.
361 // This has been checked above already.
362 if (std::distance(data, data_buf.end()) < data_size) {
363 break;
364 }
365 buffers.push_back(OptionBuffer(data, data + data_size));
366 data += data_size;
367 }
368 } else {
369 // For non-arrays the data_size can be zero because
370 // getDataTypeLen returns zero for variable size data types
371 // such as strings. Simply take whole buffer.
372 data_size = bufferLength(data_type, false, data, data_buf.end());
373 if ((data_size > 0) && (std::distance(data, data_buf.end()) >= data_size)) {
374 buffers.push_back(OptionBuffer(data, data + data_size));
375 data += data_size;
376 } else {
377 isc_throw(OutOfRange, "option buffer truncated");
378 }
379
380 // Unpack suboptions if any.
381 if (data != data_buf.end() && !getEncapsulatedSpace().empty()) {
382 unpackOptions(OptionBuffer(data, data_buf.end()));
383 }
384 }
385 } else {
386 // Unpack suboptions if any.
387 if (data != data_buf.end() && !getEncapsulatedSpace().empty()) {
388 unpackOptions(OptionBuffer(data, data_buf.end()));
389 }
390 }
391 // If everything went ok we can replace old buffer set with new ones.
392 std::swap(buffers_, buffers);
393}
394
395std::string
396OptionCustom::dataFieldToText(const OptionDataType data_type,
397 const uint32_t index) const {
398 std::ostringstream text;
399
400 // Get the value of the data field.
401 switch (data_type) {
402 case OPT_BINARY_TYPE:
403 {
404 auto data = readBinary(index);
405 if (data.empty()) {
406 text << "''";
407 } else {
408 text << util::encode::encodeHex(data);
409 if (str::isPrintable(data)) {
410 std::string printable(data.cbegin(), data.cend());
411 text << " '" << printable << "'";
412 }
413 }
414 break;
415 }
416 case OPT_BOOLEAN_TYPE:
417 text << (readBoolean(index) ? "true" : "false");
418 break;
419 case OPT_INT8_TYPE:
420 text << static_cast<int>(readInteger<int8_t>(index));
421 break;
422 case OPT_INT16_TYPE:
423 text << readInteger<int16_t>(index);
424 break;
425 case OPT_INT32_TYPE:
426 text << readInteger<int32_t>(index);
427 break;
428 case OPT_UINT8_TYPE:
429 text << static_cast<unsigned>(readInteger<uint8_t>(index));
430 break;
431 case OPT_UINT16_TYPE:
432 text << readInteger<uint16_t>(index);
433 break;
434 case OPT_UINT32_TYPE:
435 text << readInteger<uint32_t>(index);
436 break;
439 text << readAddress(index);
440 break;
441 case OPT_FQDN_TYPE:
442 text << "\"" << readFqdn(index) << "\"";
443 break;
444 case OPT_TUPLE_TYPE:
445 text << "\"" << readTuple(index) << "\"";
446 break;
447 case OPT_STRING_TYPE:
448 text << "\"" << readString(index) << "\"";
449 break;
450 case OPT_PSID_TYPE:
451 {
452 PSIDTuple t = readPsid(index);
453 text << "len=" << t.first.asUnsigned() << ",psid=" << t.second.asUint16();
454 break;
455 }
456 default:
457 break;
458 }
459
460 // Append data field type in brackets.
461 text << " (" << OptionDataTypeUtil::getDataTypeName(data_type) << ")";
462
463 return (text.str());
464}
465
466void
468
469 // Pack DHCP header (V4 or V6).
470 packHeader(buf, check);
471
472 // Write data from buffers.
473 for (auto const& it : buffers_) {
474 // In theory the createBuffers function should have taken
475 // care that there are no empty buffers added to the
476 // collection but it is almost always good to make sure.
477 if (!it.empty()) {
478 buf.writeData(&it[0], it.size());
479 }
480 }
481
482 // Write suboptions.
483 packOptions(buf, check);
484}
485
486
488OptionCustom::readAddress(const uint32_t index) const {
489 checkIndex(index);
490
491 // The address being read can be either IPv4 or IPv6. The decision
492 // is made based on the buffer length. If it holds 4 bytes it is IPv4
493 // address, if it holds 16 bytes it is IPv6.
494 if (buffers_[index].size() == asiolink::V4ADDRESS_LEN) {
495 return (OptionDataTypeUtil::readAddress(buffers_[index], AF_INET));
496 } else if (buffers_[index].size() == asiolink::V6ADDRESS_LEN) {
497 return (OptionDataTypeUtil::readAddress(buffers_[index], AF_INET6));
498 } else {
499 isc_throw(BadDataTypeCast, "unable to read data from the buffer as"
500 << " IP address. Invalid buffer length "
501 << buffers_[index].size() << ".");
502 }
503}
504
505void
507 const uint32_t index) {
508 checkIndex(index);
509
510 if ((address.isV4() && buffers_[index].size() != V4ADDRESS_LEN) ||
511 (address.isV6() && buffers_[index].size() != V6ADDRESS_LEN)) {
512 isc_throw(BadDataTypeCast, "invalid address specified "
513 << address << ". Expected a valid IPv"
514 << (buffers_[index].size() == V4ADDRESS_LEN ? "4" : "6")
515 << " address.");
516 }
517
518 OptionBuffer buf;
520 std::swap(buf, buffers_[index]);
521}
522
523const OptionBuffer&
524OptionCustom::readBinary(const uint32_t index) const {
525 checkIndex(index);
526 return (buffers_[index]);
527}
528
529void
531 const uint32_t index) {
532 checkIndex(index);
533 buffers_[index] = buf;
534}
535
536std::string
537OptionCustom::readTuple(const uint32_t index) const {
538 checkIndex(index);
540 return (OptionDataTypeUtil::readTuple(buffers_[index], lft));
541}
542
543void
545 const uint32_t index) const {
546 checkIndex(index);
547 OptionDataTypeUtil::readTuple(buffers_[index], tuple);
548}
549
550void
551OptionCustom::writeTuple(const std::string& value, const uint32_t index) {
552 checkIndex(index);
553
554 buffers_[index].clear();
556 OptionDataTypeUtil::writeTuple(value, lft, buffers_[index]);
557}
558
559void
560OptionCustom::writeTuple(const OpaqueDataTuple& value, const uint32_t index) {
561 checkIndex(index);
562
563 buffers_[index].clear();
564 OptionDataTypeUtil::writeTuple(value, buffers_[index]);
565}
566
567bool
568OptionCustom::readBoolean(const uint32_t index) const {
569 checkIndex(index);
570 return (OptionDataTypeUtil::readBool(buffers_[index]));
571}
572
573void
574OptionCustom::writeBoolean(const bool value, const uint32_t index) {
575 checkIndex(index);
576
577 buffers_[index].clear();
578 OptionDataTypeUtil::writeBool(value, buffers_[index]);
579}
580
581std::string
582OptionCustom::readFqdn(const uint32_t index) const {
583 checkIndex(index);
584 return (OptionDataTypeUtil::readFqdn(buffers_[index]));
585}
586
587void
588OptionCustom::writeFqdn(const std::string& fqdn, const uint32_t index) {
589 checkIndex(index);
590
591 // Create a temporary buffer where the FQDN will be written.
592 OptionBuffer buf;
593 // Try to write to the temporary buffer rather than to the
594 // buffers_ member directly guarantees that we don't modify
595 // (clear) buffers_ until we are sure that the provided FQDN
596 // is valid.
598 // If we got to this point it means that the FQDN is valid.
599 // We can move the contents of the temporary buffer to the
600 // target buffer.
601 std::swap(buffers_[index], buf);
602}
603
605OptionCustom::readPrefix(const uint32_t index) const {
606 checkIndex(index);
607 return (OptionDataTypeUtil::readPrefix(buffers_[index]));
608}
609
610void
612 const IOAddress& prefix,
613 const uint32_t index) {
614 checkIndex(index);
615
616 OptionBuffer buf;
617 OptionDataTypeUtil::writePrefix(prefix_len, prefix, buf);
618 // If there are no errors while writing PSID to a buffer, we can
619 // replace the current buffer with a new buffer.
620 std::swap(buffers_[index], buf);
621}
622
623
625OptionCustom::readPsid(const uint32_t index) const {
626 checkIndex(index);
627 return (OptionDataTypeUtil::readPsid(buffers_[index]));
628}
629
630void
631OptionCustom::writePsid(const PSIDLen& psid_len, const PSID& psid,
632 const uint32_t index) {
633 checkIndex(index);
634
635 OptionBuffer buf;
636 OptionDataTypeUtil::writePsid(psid_len, psid, buf);
637 // If there are no errors while writing PSID to a buffer, we can
638 // replace the current buffer with a new buffer.
639 std::swap(buffers_[index], buf);
640}
641
642
643std::string
644OptionCustom::readString(const uint32_t index) const {
645 checkIndex(index);
646 return (OptionDataTypeUtil::readString(buffers_[index]));
647}
648
649void
650OptionCustom::writeString(const std::string& text, const uint32_t index) {
651 checkIndex(index);
652
653 // Let's clear a buffer as we want to replace the value of the
654 // whole buffer. If we fail to clear the buffer the data will
655 // be appended.
656 buffers_[index].clear();
657 // If the text value is empty we can leave because the buffer
658 // is already empty.
659 if (!text.empty()) {
660 OptionDataTypeUtil::writeString(text, buffers_[index]);
661 }
662}
663
664void
669
670uint16_t
672 // The length of the option is a sum of option header ...
673 size_t length = getHeaderLen();
674
675 // ... lengths of all buffers that hold option data ...
676 for (auto const& buf : buffers_) {
677 length += buf.size();
678 }
679
680 // ... and lengths of all suboptions
681 for (auto const& it : options_) {
682 length += it.second->len();
683 }
684
685 return (static_cast<uint16_t>(length));
686}
687
689 const OptionBufferConstIter last) {
690 setData(first, last);
691
692 // Chop the data_ buffer into set of buffers that represent
693 // option fields data.
694 createBuffers(getData());
695}
696
697std::string OptionCustom::toText(int indent) const {
698 std::stringstream output;
699
700 output << headerToText(indent) << ":";
701
702 OptionDataType data_type = definition_.getType();
703 if (data_type == OPT_RECORD_TYPE) {
705 definition_.getRecordFields();
706
707 // For record types we iterate over fields defined in
708 // option definition and match the appropriate buffer
709 // with them.
710 size_t j = 0;
711 for (auto const& field : fields) {
712 output << " " << dataFieldToText(field, j);
713 j++;
714 }
715
716 // If the last record field is an array iterate on extra buffers
717 if (definition_.getArrayType()) {
718 for (unsigned int i = fields.size(); i < getDataFieldsNum(); ++i) {
719 output << " " << dataFieldToText(fields.back(), i);
720 }
721 }
722 } else {
723 // For non-record types we iterate over all buffers
724 // and print the data type set globally for an option
725 // definition. We take the same code path for arrays
726 // and non-arrays as they only differ in such a way that
727 // non-arrays have just single data field.
728 for (unsigned int i = 0; i < getDataFieldsNum(); ++i) {
729 output << " " << dataFieldToText(definition_.getType(), i);
730 }
731 }
732
733 // Append suboptions.
734 output << suboptionsToText(indent + 2);
735
736 return (output.str());
737}
738
739} // end of isc::dhcp namespace
740} // 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.
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.
const RecordFieldsCollection & getRecordFields() const
Return list of record fields.
void validate() const
Check if the option definition is valid.
std::vector< OptionDataType > RecordFieldsCollection
List of fields within the record.
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
std::string getEncapsulatedSpace() const
Returns the name of the option space encapsulated by this option.
Definition option.h:442
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition option.h:435
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition option.h:317
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:83
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:596
Universe getUniverse() const
returns option universe (V4 or V6)
Definition option.h:233
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
#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.