Kea  2.3.7
option_int_array.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2022 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 #ifndef OPTION_INT_ARRAY_H
8 #define OPTION_INT_ARRAY_H
9 
10 #include <dhcp/libdhcp++.h>
11 #include <dhcp/option.h>
12 #include <dhcp/option_data_types.h>
13 #include <util/io_utilities.h>
14 #include <boost/shared_ptr.hpp>
15 #include <sstream>
16 #include <stdint.h>
17 
18 namespace isc {
19 namespace dhcp {
20 
22 template<typename T>
23 class OptionIntArray;
24 
31 typedef boost::shared_ptr<OptionUint8Array> OptionUint8ArrayPtr;
33 typedef boost::shared_ptr<OptionUint16Array> OptionUint16ArrayPtr;
35 typedef boost::shared_ptr<OptionUint32Array> OptionUint32ArrayPtr;
37 
55 template<typename T>
56 class OptionIntArray : public Option {
57 private:
58 
60  typedef boost::shared_ptr<OptionIntArray<T> > OptionIntArrayTypePtr;
61 
62 public:
63 
73  OptionIntArray(const Option::Universe u, const uint16_t type)
74  : Option(u, type),
75  values_(0) {
77  isc_throw(dhcp::InvalidDataType, "non-integer type");
78  }
79  }
80 
91  OptionIntArray(const Option::Universe u, const uint16_t type,
92  const OptionBuffer& buf)
93  : Option(u, type) {
95  isc_throw(dhcp::InvalidDataType, "non-integer type");
96  }
97  unpack(buf.begin(), buf.end());
98  }
99 
115  OptionIntArray(const Option::Universe u, const uint16_t type,
117  : Option(u, type) {
119  isc_throw(dhcp::InvalidDataType, "non-integer type");
120  }
121  unpack(begin, end);
122  }
123 
125  virtual OptionPtr clone() const {
126  return (cloneInternal<OptionIntArray<T> >());
127  }
128 
132  void addValue(const T value) {
133  values_.push_back(value);
134  }
135 
145  virtual void pack(isc::util::OutputBuffer& buf, bool check = true) const {
146  // Pack option header.
147  packHeader(buf, check);
148  // Pack option data.
149  for (size_t i = 0; i < values_.size(); ++i) {
150  // Depending on the data type length we use different utility functions
151  // writeUint16 or writeUint32 which write the data in the network byte
152  // order to the provided buffer. The same functions can be safely used
153  // for either unsigned or signed integers so there is not need to create
154  // special cases for intX_t types.
156  case 1:
157  buf.writeUint8(values_[i]);
158  break;
159  case 2:
160  buf.writeUint16(values_[i]);
161  break;
162  case 4:
163  buf.writeUint32(values_[i]);
164  break;
165  default:
166  isc_throw(dhcp::InvalidDataType, "non-integer type");
167  }
168  }
169  // We don't pack sub-options here because we have array-type option.
170  // We don't allow sub-options in array-type options as there is no
171  // way to distinguish them from the data fields on option reception.
172  }
173 
186  if (distance(begin, end) == 0) {
187  isc_throw(OutOfRange, "option " << getType() << " empty");
188  }
189  if (distance(begin, end) % sizeof(T) != 0) {
190  isc_throw(OutOfRange, "OptionIntArray " << getType() << " truncated");
191  }
192  // @todo consider what to do if buffer is longer than data type.
193 
194  values_.clear();
195  while (begin != end) {
196  // Depending on the data type length we use different utility functions
197  // readUint16 or readUint32 which read the data laid in the network byte
198  // order from the provided buffer. The same functions can be safely used
199  // for either unsigned or signed integers so there is not need to create
200  // special cases for intX_t types.
201  int data_size_len = OptionDataTypeTraits<T>::len;
202  switch (data_size_len) {
203  case 1:
204  values_.push_back(*begin);
205  break;
206  case 2:
207  values_.push_back(isc::util::readUint16(&(*begin),
208  std::distance(begin, end)));
209  break;
210  case 4:
211  values_.push_back(isc::util::readUint32(&(*begin),
212  std::distance(begin, end)));
213  break;
214  default:
215  isc_throw(dhcp::InvalidDataType, "non-integer type");
216  }
217  // Use local variable to set a new value for this iterator.
218  // When using OptionDataTypeTraits<T>::len directly some versions
219  // of clang complain about unresolved reference to
220  // OptionDataTypeTraits structure during linking.
221  begin += data_size_len;
222  }
223  // We do not unpack sub-options here because we have array-type option.
224  // Such option have variable number of data fields, thus there is no
225  // way to assess where sub-options start.
226  }
227 
231  const std::vector<T>& getValues() const { return (values_); }
232 
236  void setValues(const std::vector<T>& values) { values_ = values; }
237 
243  virtual uint16_t len() const {
244  uint16_t length = (getUniverse() == Option::V4) ? OPTION4_HDR_LEN : OPTION6_HDR_LEN;
245  length += values_.size() * sizeof(T);
246  // length of all suboptions
247  for (OptionCollection::const_iterator it = options_.begin();
248  it != options_.end();
249  ++it) {
250  length += (*it).second->len();
251  }
252  return (length);
253  }
254 
261  virtual std::string toText(int indent = 0) const {
262  std::stringstream output;
263  output << headerToText(indent) << ":";
264 
266  for (typename std::vector<T>::const_iterator value = values_.begin();
267  value != values_.end(); ++value) {
268  output << " ";
269 
270  // For 1 byte long data types we need to cast to the integer
271  // because they are usually implemented as "char" types, in
272  // which case the character rather than number would be printed.
273  if (OptionDataTypeTraits<T>::len == 1) {
274  output << static_cast<int>(*value);
275 
276  } else {
277  output << *value;
278  }
279 
280  // Append data type.
281  output << "(" << data_type << ")";
282  }
283 
284  return (output.str());
285  }
286 
287 private:
288 
289  std::vector<T> values_;
290 };
291 
292 } // isc::dhcp namespace
293 } // isc namespace
294 
295 #endif // OPTION_INT_ARRAY_H
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 invalid type specified as template parameter.
static const std::string & getDataTypeName(const OptionDataType data_type)
Return option data type name from the data type enumerator.
Forward declaration to OptionIntArray.
OptionIntArray(const Option::Universe u, const uint16_t type)
Constructor.
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in wire-format to buf, returns pointer to first unused byte after stored option.
virtual uint16_t len() const
returns complete length of option
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
OptionIntArray(const Option::Universe u, const uint16_t type, const OptionBuffer &buf)
Constructor.
OptionIntArray(const Option::Universe u, const uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Constructor.
const std::vector< T > & getValues() const
Return collection of option values.
void setValues(const std::vector< T > &values)
Set option values.
virtual std::string toText(int indent=0) const
Returns textual representation of the option.
void addValue(const T value)
Adds a new value to the array.
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:288
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:83
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:293
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:80
OptionCollection options_
collection for storing suboptions
Definition: option.h:596
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition: option.h:497
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
static const size_t OPTION4_HDR_LEN
length of the usual DHCPv4 option header (there are exceptions)
Definition: option.h:77
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition: buffer.h:466
void writeUint16(uint16_t data)
Write an unsigned 16-bit integer in host byte order into the buffer in network byte order.
Definition: buffer.h:490
void writeUint32(uint32_t data)
Write an unsigned 32-bit integer in host byte order into the buffer in network byte order.
Definition: buffer.h:520
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< OptionUint8Array > OptionUint8ArrayPtr
OptionIntArray< uint32_t > OptionUint32Array
OptionIntArray< uint8_t > OptionUint8Array
boost::shared_ptr< OptionUint32Array > OptionUint32ArrayPtr
boost::shared_ptr< OptionUint16Array > OptionUint16ArrayPtr
OptionIntArray< uint16_t > OptionUint16Array
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
uint32_t readUint32(const uint8_t *buffer, size_t length)
Read Unsigned 32-Bit Integer from Buffer.
Definition: io_utilities.h:79
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.
Definition: io_utilities.h:28
Defines the logger used by the top-level component of kea-lfc.
Trait class for data types supported in DHCP option definitions.