Kea 3.1.1
option_custom.cc
Go to the documentation of this file.
1// Copyright (C) 2012-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#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 (static_cast<size_t>(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 (static_cast<size_t>(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 (static_cast<size_t>(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 (static_cast<size_t>(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) &&
384 (static_cast<size_t>(std::distance(data, data_buf.end())) >= data_size)) {
385 buffers.push_back(OptionBuffer(data, data + data_size));
386 data += data_size;
387 } else {
388 isc_throw(OutOfRange, "option buffer truncated");
389 }
390
391 // Unpack suboptions if any.
392 if (data != data_buf.end() && !getEncapsulatedSpace().empty()) {
393 unpackOptions(OptionBuffer(data, data_buf.end()));
394 }
395 }
396 } else {
397 // Unpack suboptions if any.
398 if (data != data_buf.end() && !getEncapsulatedSpace().empty()) {
399 unpackOptions(OptionBuffer(data, data_buf.end()));
400 }
401 }
402 // If everything went ok we can replace old buffer set with new ones.
403 std::swap(buffers_, buffers);
404}
405
406std::string
407OptionCustom::dataFieldToText(const OptionDataType data_type,
408 const uint32_t index) const {
409 std::ostringstream text;
410
411 // Get the value of the data field.
412 switch (data_type) {
413 case OPT_BINARY_TYPE:
414 {
415 auto data = readBinary(index);
416 if (data.empty()) {
417 text << "''";
418 } else {
419 text << util::encode::encodeHex(data);
420 if (str::isPrintable(data)) {
421 std::string printable(data.cbegin(), data.cend());
422 text << " '" << printable << "'";
423 }
424 }
425 break;
426 }
427 case OPT_BOOLEAN_TYPE:
428 text << (readBoolean(index) ? "true" : "false");
429 break;
430 case OPT_INT8_TYPE:
431 text << static_cast<int>(readInteger<int8_t>(index));
432 break;
433 case OPT_INT16_TYPE:
434 text << readInteger<int16_t>(index);
435 break;
436 case OPT_INT32_TYPE:
437 text << readInteger<int32_t>(index);
438 break;
439 case OPT_UINT8_TYPE:
440 text << static_cast<unsigned>(readInteger<uint8_t>(index));
441 break;
442 case OPT_UINT16_TYPE:
443 text << readInteger<uint16_t>(index);
444 break;
445 case OPT_UINT32_TYPE:
446 text << readInteger<uint32_t>(index);
447 break;
450 text << readAddress(index);
451 break;
452 case OPT_FQDN_TYPE:
453 text << "\"" << readFqdn(index) << "\"";
454 break;
455 case OPT_TUPLE_TYPE:
456 text << "\"" << readTuple(index) << "\"";
457 break;
458 case OPT_STRING_TYPE:
459 text << "\"" << readString(index) << "\"";
460 break;
461 case OPT_PSID_TYPE:
462 {
463 PSIDTuple t = readPsid(index);
464 text << "len=" << t.first.asUnsigned() << ",psid=" << t.second.asUint16();
465 break;
466 }
467 default:
468 break;
469 }
470
471 // Append data field type in brackets.
472 text << " (" << OptionDataTypeUtil::getDataTypeName(data_type) << ")";
473
474 return (text.str());
475}
476
477void
479
480 // Pack DHCP header (V4 or V6).
481 packHeader(buf, check);
482
483 // Write data from buffers.
484 for (auto const& it : buffers_) {
485 // In theory the createBuffers function should have taken
486 // care that there are no empty buffers added to the
487 // collection but it is almost always good to make sure.
488 if (!it.empty()) {
489 buf.writeData(&it[0], it.size());
490 }
491 }
492
493 // Write suboptions.
494 packOptions(buf, check);
495}
496
497
499OptionCustom::readAddress(const uint32_t index) const {
500 checkIndex(index);
501
502 // The address being read can be either IPv4 or IPv6. The decision
503 // is made based on the buffer length. If it holds 4 bytes it is IPv4
504 // address, if it holds 16 bytes it is IPv6.
505 if (buffers_[index].size() == asiolink::V4ADDRESS_LEN) {
506 return (OptionDataTypeUtil::readAddress(buffers_[index], AF_INET));
507 } else if (buffers_[index].size() == asiolink::V6ADDRESS_LEN) {
508 return (OptionDataTypeUtil::readAddress(buffers_[index], AF_INET6));
509 } else {
510 isc_throw(BadDataTypeCast, "unable to read data from the buffer as"
511 << " IP address. Invalid buffer length "
512 << buffers_[index].size() << ".");
513 }
514}
515
516void
518 const uint32_t index) {
519 checkIndex(index);
520
521 if ((address.isV4() && buffers_[index].size() != V4ADDRESS_LEN) ||
522 (address.isV6() && buffers_[index].size() != V6ADDRESS_LEN)) {
523 isc_throw(BadDataTypeCast, "invalid address specified "
524 << address << ". Expected a valid IPv"
525 << (buffers_[index].size() == V4ADDRESS_LEN ? "4" : "6")
526 << " address.");
527 }
528
529 OptionBuffer buf;
531 std::swap(buf, buffers_[index]);
532}
533
534const OptionBuffer&
535OptionCustom::readBinary(const uint32_t index) const {
536 checkIndex(index);
537 return (buffers_[index]);
538}
539
540void
542 const uint32_t index) {
543 checkIndex(index);
544 buffers_[index] = buf;
545}
546
547std::string
548OptionCustom::readTuple(const uint32_t index) const {
549 checkIndex(index);
551 return (OptionDataTypeUtil::readTuple(buffers_[index], lft));
552}
553
554void
556 const uint32_t index) const {
557 checkIndex(index);
558 OptionDataTypeUtil::readTuple(buffers_[index], tuple);
559}
560
561void
562OptionCustom::writeTuple(const std::string& value, const uint32_t index) {
563 checkIndex(index);
564
565 buffers_[index].clear();
567 OptionDataTypeUtil::writeTuple(value, lft, buffers_[index]);
568}
569
570void
571OptionCustom::writeTuple(const OpaqueDataTuple& value, const uint32_t index) {
572 checkIndex(index);
573
574 buffers_[index].clear();
575 OptionDataTypeUtil::writeTuple(value, buffers_[index]);
576}
577
578bool
579OptionCustom::readBoolean(const uint32_t index) const {
580 checkIndex(index);
581 return (OptionDataTypeUtil::readBool(buffers_[index]));
582}
583
584void
585OptionCustom::writeBoolean(const bool value, const uint32_t index) {
586 checkIndex(index);
587
588 buffers_[index].clear();
589 OptionDataTypeUtil::writeBool(value, buffers_[index]);
590}
591
592std::string
593OptionCustom::readFqdn(const uint32_t index) const {
594 checkIndex(index);
595 return (OptionDataTypeUtil::readFqdn(buffers_[index]));
596}
597
598void
599OptionCustom::writeFqdn(const std::string& fqdn, const uint32_t index) {
600 checkIndex(index);
601
602 // Create a temporary buffer where the FQDN will be written.
603 OptionBuffer buf;
604 // Try to write to the temporary buffer rather than to the
605 // buffers_ member directly guarantees that we don't modify
606 // (clear) buffers_ until we are sure that the provided FQDN
607 // is valid.
609 // If we got to this point it means that the FQDN is valid.
610 // We can move the contents of the temporary buffer to the
611 // target buffer.
612 std::swap(buffers_[index], buf);
613}
614
616OptionCustom::readPrefix(const uint32_t index) const {
617 checkIndex(index);
618 return (OptionDataTypeUtil::readPrefix(buffers_[index]));
619}
620
621void
623 const IOAddress& prefix,
624 const uint32_t index) {
625 checkIndex(index);
626
627 OptionBuffer buf;
628 OptionDataTypeUtil::writePrefix(prefix_len, prefix, buf);
629 // If there are no errors while writing PSID to a buffer, we can
630 // replace the current buffer with a new buffer.
631 std::swap(buffers_[index], buf);
632}
633
634
636OptionCustom::readPsid(const uint32_t index) const {
637 checkIndex(index);
638 return (OptionDataTypeUtil::readPsid(buffers_[index]));
639}
640
641void
642OptionCustom::writePsid(const PSIDLen& psid_len, const PSID& psid,
643 const uint32_t index) {
644 checkIndex(index);
645
646 OptionBuffer buf;
647 OptionDataTypeUtil::writePsid(psid_len, psid, buf);
648 // If there are no errors while writing PSID to a buffer, we can
649 // replace the current buffer with a new buffer.
650 std::swap(buffers_[index], buf);
651}
652
653
654std::string
655OptionCustom::readString(const uint32_t index) const {
656 checkIndex(index);
657 return (OptionDataTypeUtil::readString(buffers_[index]));
658}
659
660void
661OptionCustom::writeString(const std::string& text, const uint32_t index) {
662 checkIndex(index);
663
664 // Let's clear a buffer as we want to replace the value of the
665 // whole buffer. If we fail to clear the buffer the data will
666 // be appended.
667 buffers_[index].clear();
668 // If the text value is empty we can leave because the buffer
669 // is already empty.
670 if (!text.empty()) {
671 OptionDataTypeUtil::writeString(text, buffers_[index]);
672 }
673}
674
675void
680
681uint16_t
683 // The length of the option is a sum of option header ...
684 size_t length = getHeaderLen();
685
686 // ... lengths of all buffers that hold option data ...
687 for (auto const& buf : buffers_) {
688 length += buf.size();
689 }
690
691 // ... and lengths of all suboptions
692 for (auto const& it : options_) {
693 length += it.second->len();
694 }
695
696 return (static_cast<uint16_t>(length));
697}
698
700 const OptionBufferConstIter last) {
701 setData(first, last);
702
703 // Chop the data_ buffer into set of buffers that represent
704 // option fields data.
705 createBuffers(getData());
706}
707
708std::string OptionCustom::toText(int indent) const {
709 std::stringstream output;
710
711 output << headerToText(indent) << ":";
712
713 OptionDataType data_type = definition_.getType();
714 if (data_type == OPT_RECORD_TYPE) {
716 definition_.getRecordFields();
717
718 // For record types we iterate over fields defined in
719 // option definition and match the appropriate buffer
720 // with them.
721 size_t j = 0;
722 for (auto const& field : fields) {
723 output << " " << dataFieldToText(field, j);
724 j++;
725 }
726
727 // If the last record field is an array iterate on extra buffers
728 if (definition_.getArrayType()) {
729 for (unsigned int i = fields.size(); i < getDataFieldsNum(); ++i) {
730 output << " " << dataFieldToText(fields.back(), i);
731 }
732 }
733 } else {
734 // For non-record types we iterate over all buffers
735 // and print the data type set globally for an option
736 // definition. We take the same code path for arrays
737 // and non-arrays as they only differ in such a way that
738 // non-arrays have just single data field.
739 for (unsigned int i = 0; i < getDataFieldsNum(); ++i) {
740 output << " " << dataFieldToText(definition_.getType(), i);
741 }
742 }
743
744 // Append suboptions.
745 output << suboptionsToText(indent + 2);
746
747 return (output.str());
748}
749
750} // end of isc::dhcp namespace
751} // 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.
std::vector< OptionDataType > RecordFieldsCollection
List of fields within the record.
std::string getEncapsulatedSpace() const
Return name of the encapsulated option space.
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
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
Definition option.cc:39
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:346
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition buffer.h:559
#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.