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