Kea  2.3.9
option_custom.cc
Go to the documentation of this file.
1 // Copyright (C) 2012-2023 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>
11 #include <exceptions/isc_assert.h>
12 #include <util/encode/hex.h>
13 
14 using namespace isc::asiolink;
15 
16 namespace isc {
17 namespace dhcp {
18 
19 OptionCustom::OptionCustom(const OptionDefinition& def,
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 
51 void
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 
68 void
69 OptionCustom::addArrayDataField(const std::string& value) {
70  checkArrayType();
71 
73  OptionBuffer buf;
74  OptionDataTypeUtil::writeTuple(value, lft, buf);
75  buffers_.push_back(buf);
76 }
77 
78 void
80  checkArrayType();
81 
82  OptionBuffer buf;
84  buffers_.push_back(buf);
85 }
86 
87 void
89  checkArrayType();
90 
91  OptionBuffer buf;
93  buffers_.push_back(buf);
94 }
95 
96 void
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 
111 void
112 OptionCustom::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 
126 void
127 OptionCustom::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 
134 void
135 OptionCustom::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) {
148  OptionDataTypeUtil::writeFqdn(".", buffer);
149 
150  } else if (data_type == OPT_IPV6_PREFIX_TYPE) {
151  OptionDataTypeUtil::writePrefix(PrefixLen(0),
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 
163 void
164 OptionCustom::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 (OptionDefinition::RecordFieldsConstIter field = fields.begin();
184  field != fields.end(); ++field) {
185  OptionBuffer buf;
186  createBuffer(buf, *field);
187  // We have the buffer with default value prepared so we
188  // add it to the set of buffers.
189  buffers.push_back(buf);
190  }
191  } else if (!definition_.getArrayType() &&
192  data_type != OPT_EMPTY_TYPE) {
193  // For either 'empty' options we don't have to create any buffers
194  // for obvious reason. For arrays we also don't create any buffers
195  // yet because the set of fields that belong to the array is open
196  // ended so we can't allocate required buffers until we know how
197  // many of them are needed.
198  // For non-arrays we have a single value being held by the option
199  // so we have to allocate exactly one buffer.
200  OptionBuffer buf;
201  createBuffer(buf, data_type);
202  // Add a buffer that we have created and leave.
203  buffers.push_back(buf);
204  }
205  // The 'swap' is used here because we want to make sure that we
206  // don't touch buffers_ until we successfully allocate all
207  // buffers to be stored there.
208  std::swap(buffers, buffers_);
209 }
210 
211 size_t
212 OptionCustom::bufferLength(const OptionDataType data_type, bool in_array,
213  OptionBuffer::const_iterator begin,
214  OptionBuffer::const_iterator end) const {
215  // For fixed-size data type such as boolean, integer, even
216  // IP address we can use the utility function to get the required
217  // buffer size.
218  size_t data_size = OptionDataTypeUtil::getDataTypeLen(data_type);
219 
220  // For variable size types (e.g. string) the function above will
221  // return 0 so we need to do a runtime check of the length.
222  if (data_size == 0) {
223  // FQDN is a special data type as it stores variable length data
224  // but the data length is encoded in the buffer. The easiest way
225  // to obtain the length of the data is to read the FQDN. The
226  // utility function will return the size of the buffer on success.
227  if (data_type == OPT_FQDN_TYPE) {
228  std::string fqdn =
230  // The size of the buffer holding an FQDN is always
231  // 1 byte larger than the size of the string
232  // representation of this FQDN.
233  data_size = fqdn.size() + 1;
234  } else if (!definition_.getArrayType() &&
235  ((data_type == OPT_BINARY_TYPE) ||
236  (data_type == OPT_STRING_TYPE))) {
237  // In other case we are dealing with string or binary value
238  // which size can't be determined. Thus we consume the
239  // remaining part of the buffer for it. Note that variable
240  // size data can be laid at the end of the option only and
241  // that the validate() function in OptionDefinition object
242  // should have checked wheter it is a case for this option.
243  data_size = std::distance(begin, end);
244  } else if (data_type == OPT_IPV6_PREFIX_TYPE) {
245  // The size of the IPV6 prefix type is determined as
246  // one byte (which is the size of the prefix in bits)
247  // followed by the prefix bits (right-padded with
248  // zeros to the nearest octet boundary)
249  if ((begin == end) && !in_array)
250  return 0;
251  PrefixTuple prefix =
253  // Data size comprises 1 byte holding a prefix length and the
254  // prefix length (in bytes) rounded to the nearest byte boundary.
255  data_size = sizeof(uint8_t) + (prefix.first.asUint8() + 7) / 8;
256  } else if (data_type == OPT_TUPLE_TYPE) {
259  std::string value =
261  data_size = value.size();
262  // The size of the buffer holding a tuple is always
263  // 1 or 2 byte larger than the size of the string
264  data_size += getUniverse() == Option::V4 ? 1 : 2;
265  } else {
266  // If we reached the end of buffer we assume that this option is
267  // truncated because there is no remaining data to initialize
268  // an option field.
269  isc_throw(OutOfRange, "option buffer truncated");
270  }
271  }
272 
273  return data_size;
274 }
275 
276 void
277 OptionCustom::createBuffers(const OptionBuffer& data_buf) {
278  // Check that the option definition is correct as we are going
279  // to use it to split the data_ buffer into set of sub buffers.
280  definition_.validate();
281 
282  std::vector<OptionBuffer> buffers;
283  OptionBuffer::const_iterator data = data_buf.begin();
284 
285  OptionDataType data_type = definition_.getType();
286  if (data_type == OPT_RECORD_TYPE) {
287  // An option comprises a record of data fields. We need to
288  // get types of these data fields to allocate enough space
289  // for each buffer.
291  definition_.getRecordFields();
292 
293  // Go over all data fields within a record.
294  for (OptionDefinition::RecordFieldsConstIter field = fields.begin();
295  field != fields.end(); ++field) {
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 
395 std::string
396 OptionCustom::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  text << util::encode::encodeHex(readBinary(index));
404  break;
405  case OPT_BOOLEAN_TYPE:
406  text << (readBoolean(index) ? "true" : "false");
407  break;
408  case OPT_INT8_TYPE:
409  text << static_cast<int>(readInteger<int8_t>(index));
410  break;
411  case OPT_INT16_TYPE:
412  text << readInteger<int16_t>(index);
413  break;
414  case OPT_INT32_TYPE:
415  text << readInteger<int32_t>(index);
416  break;
417  case OPT_UINT8_TYPE:
418  text << static_cast<unsigned>(readInteger<uint8_t>(index));
419  break;
420  case OPT_UINT16_TYPE:
421  text << readInteger<uint16_t>(index);
422  break;
423  case OPT_UINT32_TYPE:
424  text << readInteger<uint32_t>(index);
425  break;
428  text << readAddress(index);
429  break;
430  case OPT_FQDN_TYPE:
431  text << "\"" << readFqdn(index) << "\"";
432  break;
433  case OPT_TUPLE_TYPE:
434  text << "\"" << readTuple(index) << "\"";
435  break;
436  case OPT_STRING_TYPE:
437  text << "\"" << readString(index) << "\"";
438  break;
439  case OPT_PSID_TYPE:
440  {
441  PSIDTuple t = readPsid(index);
442  text << "len=" << t.first.asUnsigned() << ",psid=" << t.second.asUint16();
443  }
444  default:
445  ;
446  }
447 
448  // Append data field type in brackets.
449  text << " (" << OptionDataTypeUtil::getDataTypeName(data_type) << ")";
450 
451  return (text.str());
452 }
453 
454 void
456 
457  // Pack DHCP header (V4 or V6).
458  packHeader(buf, check);
459 
460  // Write data from buffers.
461  for (std::vector<OptionBuffer>::const_iterator it = buffers_.begin();
462  it != buffers_.end(); ++it) {
463  // In theory the createBuffers function should have taken
464  // care that there are no empty buffers added to the
465  // collection but it is almost always good to make sure.
466  if (!it->empty()) {
467  buf.writeData(&(*it)[0], it->size());
468  }
469  }
470 
471  // Write suboptions.
472  packOptions(buf, check);
473 }
474 
475 
476 IOAddress
477 OptionCustom::readAddress(const uint32_t index) const {
478  checkIndex(index);
479 
480  // The address being read can be either IPv4 or IPv6. The decision
481  // is made based on the buffer length. If it holds 4 bytes it is IPv4
482  // address, if it holds 16 bytes it is IPv6.
483  if (buffers_[index].size() == asiolink::V4ADDRESS_LEN) {
484  return (OptionDataTypeUtil::readAddress(buffers_[index], AF_INET));
485  } else if (buffers_[index].size() == asiolink::V6ADDRESS_LEN) {
486  return (OptionDataTypeUtil::readAddress(buffers_[index], AF_INET6));
487  } else {
488  isc_throw(BadDataTypeCast, "unable to read data from the buffer as"
489  << " IP address. Invalid buffer length "
490  << buffers_[index].size() << ".");
491  }
492 }
493 
494 void
496  const uint32_t index) {
497  checkIndex(index);
498 
499  if ((address.isV4() && buffers_[index].size() != V4ADDRESS_LEN) ||
500  (address.isV6() && buffers_[index].size() != V6ADDRESS_LEN)) {
501  isc_throw(BadDataTypeCast, "invalid address specified "
502  << address << ". Expected a valid IPv"
503  << (buffers_[index].size() == V4ADDRESS_LEN ? "4" : "6")
504  << " address.");
505  }
506 
507  OptionBuffer buf;
508  OptionDataTypeUtil::writeAddress(address, buf);
509  std::swap(buf, buffers_[index]);
510 }
511 
512 const OptionBuffer&
513 OptionCustom::readBinary(const uint32_t index) const {
514  checkIndex(index);
515  return (buffers_[index]);
516 }
517 
518 void
520  const uint32_t index) {
521  checkIndex(index);
522  buffers_[index] = buf;
523 }
524 
525 std::string
526 OptionCustom::readTuple(const uint32_t index) const {
527  checkIndex(index);
529  return (OptionDataTypeUtil::readTuple(buffers_[index], lft));
530 }
531 
532 void
534  const uint32_t index) const {
535  checkIndex(index);
536  OptionDataTypeUtil::readTuple(buffers_[index], tuple);
537 }
538 
539 void
540 OptionCustom::writeTuple(const std::string& value, const uint32_t index) {
541  checkIndex(index);
542 
543  buffers_[index].clear();
545  OptionDataTypeUtil::writeTuple(value, lft, buffers_[index]);
546 }
547 
548 void
549 OptionCustom::writeTuple(const OpaqueDataTuple& value, const uint32_t index) {
550  checkIndex(index);
551 
552  buffers_[index].clear();
553  OptionDataTypeUtil::writeTuple(value, buffers_[index]);
554 }
555 
556 bool
557 OptionCustom::readBoolean(const uint32_t index) const {
558  checkIndex(index);
559  return (OptionDataTypeUtil::readBool(buffers_[index]));
560 }
561 
562 void
563 OptionCustom::writeBoolean(const bool value, const uint32_t index) {
564  checkIndex(index);
565 
566  buffers_[index].clear();
567  OptionDataTypeUtil::writeBool(value, buffers_[index]);
568 }
569 
570 std::string
571 OptionCustom::readFqdn(const uint32_t index) const {
572  checkIndex(index);
573  return (OptionDataTypeUtil::readFqdn(buffers_[index]));
574 }
575 
576 void
577 OptionCustom::writeFqdn(const std::string& fqdn, const uint32_t index) {
578  checkIndex(index);
579 
580  // Create a temporary buffer where the FQDN will be written.
581  OptionBuffer buf;
582  // Try to write to the temporary buffer rather than to the
583  // buffers_ member directly guarantees that we don't modify
584  // (clear) buffers_ until we are sure that the provided FQDN
585  // is valid.
587  // If we got to this point it means that the FQDN is valid.
588  // We can move the contents of the temporary buffer to the
589  // target buffer.
590  std::swap(buffers_[index], buf);
591 }
592 
594 OptionCustom::readPrefix(const uint32_t index) const {
595  checkIndex(index);
596  return (OptionDataTypeUtil::readPrefix(buffers_[index]));
597 }
598 
599 void
601  const IOAddress& prefix,
602  const uint32_t index) {
603  checkIndex(index);
604 
605  OptionBuffer buf;
606  OptionDataTypeUtil::writePrefix(prefix_len, prefix, buf);
607  // If there are no errors while writing PSID to a buffer, we can
608  // replace the current buffer with a new buffer.
609  std::swap(buffers_[index], buf);
610 }
611 
612 
613 PSIDTuple
614 OptionCustom::readPsid(const uint32_t index) const {
615  checkIndex(index);
616  return (OptionDataTypeUtil::readPsid(buffers_[index]));
617 }
618 
619 void
620 OptionCustom::writePsid(const PSIDLen& psid_len, const PSID& psid,
621  const uint32_t index) {
622  checkIndex(index);
623 
624  OptionBuffer buf;
625  OptionDataTypeUtil::writePsid(psid_len, psid, buf);
626  // If there are no errors while writing PSID to a buffer, we can
627  // replace the current buffer with a new buffer.
628  std::swap(buffers_[index], buf);
629 }
630 
631 
632 std::string
633 OptionCustom::readString(const uint32_t index) const {
634  checkIndex(index);
635  return (OptionDataTypeUtil::readString(buffers_[index]));
636 }
637 
638 void
639 OptionCustom::writeString(const std::string& text, const uint32_t index) {
640  checkIndex(index);
641 
642  // Let's clear a buffer as we want to replace the value of the
643  // whole buffer. If we fail to clear the buffer the data will
644  // be appended.
645  buffers_[index].clear();
646  // If the text value is empty we can leave because the buffer
647  // is already empty.
648  if (!text.empty()) {
649  OptionDataTypeUtil::writeString(text, buffers_[index]);
650  }
651 }
652 
653 void
655  OptionBufferConstIter end) {
656  initialize(begin, end);
657 }
658 
659 uint16_t
661  // The length of the option is a sum of option header ...
662  size_t length = getHeaderLen();
663 
664  // ... lengths of all buffers that hold option data ...
665  for (std::vector<OptionBuffer>::const_iterator buf = buffers_.begin();
666  buf != buffers_.end(); ++buf) {
667  length += buf->size();
668  }
669 
670  // ... and lengths of all suboptions
671  for (OptionCollection::const_iterator it = options_.begin();
672  it != options_.end();
673  ++it) {
674  length += (*it).second->len();
675  }
676 
677  return (static_cast<uint16_t>(length));
678 }
679 
681  const OptionBufferConstIter last) {
682  setData(first, last);
683 
684  // Chop the data_ buffer into set of buffers that represent
685  // option fields data.
686  createBuffers(getData());
687 }
688 
689 std::string OptionCustom::toText(int indent) const {
690  std::stringstream output;
691 
692  output << headerToText(indent) << ":";
693 
694  OptionDataType data_type = definition_.getType();
695  if (data_type == OPT_RECORD_TYPE) {
697  definition_.getRecordFields();
698 
699  // For record types we iterate over fields defined in
700  // option definition and match the appropriate buffer
701  // with them.
702  for (OptionDefinition::RecordFieldsConstIter field = fields.begin();
703  field != fields.end(); ++field) {
704  output << " " << dataFieldToText(*field, std::distance(fields.begin(),
705  field));
706  }
707 
708  // If the last record field is an array iterate on extra buffers
709  if (definition_.getArrayType()) {
710  for (unsigned int i = fields.size(); i < getDataFieldsNum(); ++i) {
711  output << " " << dataFieldToText(fields.back(), i);
712  }
713  }
714  } else {
715  // For non-record types we iterate over all buffers
716  // and print the data type set globally for an option
717  // definition. We take the same code path for arrays
718  // and non-arrays as they only differ in such a way that
719  // non-arrays have just single data field.
720  for (unsigned int i = 0; i < getDataFieldsNum(); ++i) {
721  output << " " << dataFieldToText(definition_.getType(), i);
722  }
723  }
724 
725  // Append suboptions.
726  output << suboptionsToText(indent + 2);
727 
728  return (output.str());
729 }
730 
731 } // end of isc::dhcp namespace
732 } // 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.
std::vector< OptionDataType >::const_iterator RecordFieldsConstIter
Const iterator for record data 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 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
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition: option.h:317
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:294
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition: buffer.h:550
#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:36
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 ('hex') format.
Definition: base_n.cc:483
Defines the logger used by the top-level component of kea-lfc.