Kea  2.3.6
option_int.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_H
8 #define OPTION_INT_H
9 
10 #include <dhcp/libdhcp++.h>
11 #include <dhcp/option.h>
12 #include <dhcp/option_data_types.h>
13 #include <dhcp/option_space.h>
14 #include <util/io_utilities.h>
15 
16 #include <stdint.h>
17 #include <sstream>
18 
19 namespace isc {
20 namespace dhcp {
21 
22 template<typename T>
23 class OptionInt;
24 
31 typedef boost::shared_ptr<OptionUint8> OptionUint8Ptr;
33 typedef boost::shared_ptr<OptionUint16> OptionUint16Ptr;
35 typedef boost::shared_ptr<OptionUint32> OptionUint32Ptr;
37 
48 template<typename T>
49 class OptionInt: public Option {
50 private:
51 
53  typedef boost::shared_ptr<OptionInt<T> > OptionIntTypePtr;
54 
55 public:
65  OptionInt(Option::Universe u, uint16_t type, T value)
66  : Option(u, type), value_(value) {
68  isc_throw(dhcp::InvalidDataType, "non-integer type");
69  }
71  }
72 
90  : Option(u, type) {
92  isc_throw(dhcp::InvalidDataType, "non-integer type");
93  }
95  unpack(begin, end);
96  }
97 
99  virtual OptionPtr clone() const {
100  return (cloneInternal<OptionInt<T> >());
101  }
102 
112  virtual void pack(isc::util::OutputBuffer& buf, bool check = true) const {
113  // Pack option header.
114  packHeader(buf, check);
115  // Depending on the data type length we use different utility functions
116  // writeUint16 or writeUint32 which write the data in the network byte
117  // order to the provided buffer. The same functions can be safely used
118  // for either unsigned or signed integers so there is not need to create
119  // special cases for intX_t types.
121  case 1:
122  buf.writeUint8(value_);
123  break;
124  case 2:
125  buf.writeUint16(value_);
126  break;
127  case 4:
128  buf.writeUint32(value_);
129  break;
130  default:
131  isc_throw(dhcp::InvalidDataType, "non-integer type");
132  }
133  packOptions(buf, check);
134  }
135 
149  if (distance(begin, end) < sizeof(T)) {
150  isc_throw(OutOfRange, "OptionInt " << getType() << " truncated");
151  }
152  // @todo consider what to do if buffer is longer than data type.
153 
154  // Depending on the data type length we use different utility functions
155  // readUint16 or readUint32 which read the data laid in the network byte
156  // order from the provided buffer. The same functions can be safely used
157  // for either unsigned or signed integers so there is not need to create
158  // special cases for intX_t types.
159  int data_size_len = OptionDataTypeTraits<T>::len;
160  switch (data_size_len) {
161  case 1:
162  value_ = *begin;
163  break;
164  case 2:
165  value_ = isc::util::readUint16(&(*begin),
166  std::distance(begin, end));
167  break;
168  case 4:
169  value_ = isc::util::readUint32(&(*begin),
170  std::distance(begin, end));
171  break;
172  default:
173  isc_throw(dhcp::InvalidDataType, "non-integer type");
174  }
175  // Use local variable to set a new value for this iterator.
176  // When using OptionDataTypeTraits<T>::len directly some versions
177  // of clang complain about unresolved reference to
178  // OptionDataTypeTraits structure during linking.
179  begin += data_size_len;
180  unpackOptions(OptionBuffer(begin, end));
181  }
182 
186  void setValue(T value) { value_ = value; }
187 
191  T getValue() const { return value_; }
192 
198  virtual uint16_t len() const {
199  // Calculate the length of the header.
200  uint16_t length = (getUniverse() == Option::V4) ? OPTION4_HDR_LEN : OPTION6_HDR_LEN;
201  // The data length is equal to size of T.
202  length += sizeof(T);;
203  // length of all suboptions
204  for (OptionCollection::const_iterator it = options_.begin();
205  it != options_.end();
206  ++it) {
207  length += (*it).second->len();
208  }
209  return (length);
210  }
211 
218  virtual std::string toText(int indent = 0) const {
219  std::stringstream output;
220  output << headerToText(indent) << ": ";
221 
222  // For 1 byte long data types we need to cast to the integer
223  // because they are usually implemented as "char" types, in
224  // which case the character rather than number would be printed.
225  if (OptionDataTypeTraits<T>::len == 1) {
226  output << static_cast<int>(getValue());
227  } else {
228  output << getValue();
229  }
230 
231  // Append data type name.
232  output << " ("
234  << ")";
235 
236  // Append suboptions.
237  output << suboptionsToText(indent + 2);
238 
239  return (output.str());
240  }
241 
242 private:
243 
244  T value_;
245 };
246 
247 } // isc::dhcp namespace
248 } // isc namespace
249 
250 #endif // OPTION_INT_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 OptionInt.
Definition: option_int.h:49
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition: option_int.h:148
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option_int.h:99
virtual std::string toText(int indent=0) const
Returns option carrying an integer value in the textual format.
Definition: option_int.h:218
T getValue() const
Return option value.
Definition: option_int.h:191
void setValue(T value)
Set option value.
Definition: option_int.h:186
OptionInt(Option::Universe u, uint16_t type, T value)
Constructor.
Definition: option_int.h:65
virtual uint16_t len() const
returns complete length of option
Definition: option_int.h:198
OptionInt(Option::Universe u, uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Constructor.
Definition: option_int.h:88
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.
Definition: option_int.h:112
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
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition: option.h:435
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
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
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.
OptionInt< uint8_t > OptionUint8
Definition: option_int.h:30
OptionInt< uint32_t > OptionUint32
Definition: option_int.h:34
OptionInt< uint16_t > OptionUint16
Definition: option_int.h:32
boost::shared_ptr< OptionUint8 > OptionUint8Ptr
Definition: option_int.h:31
boost::shared_ptr< OptionUint32 > OptionUint32Ptr
Definition: option_int.h:35
boost::shared_ptr< OptionUint16 > OptionUint16Ptr
Definition: option_int.h:33
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.
#define DHCP4_OPTION_SPACE
global std option spaces
#define DHCP6_OPTION_SPACE
Trait class for data types supported in DHCP option definitions.