Kea  1.9.9-git
option_int.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2019 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 
111  void pack(isc::util::OutputBuffer& buf) const {
112  // Pack option header.
113  packHeader(buf);
114  // Depending on the data type length we use different utility functions
115  // writeUint16 or writeUint32 which write the data in the network byte
116  // order to the provided buffer. The same functions can be safely used
117  // for either unsigned or signed integers so there is not need to create
118  // special cases for intX_t types.
120  case 1:
121  buf.writeUint8(value_);
122  break;
123  case 2:
124  buf.writeUint16(value_);
125  break;
126  case 4:
127  buf.writeUint32(value_);
128  break;
129  default:
130  isc_throw(dhcp::InvalidDataType, "non-integer type");
131  }
132  packOptions(buf);
133  }
134 
148  if (distance(begin, end) < sizeof(T)) {
149  isc_throw(OutOfRange, "OptionInt " << getType() << " truncated");
150  }
151  // @todo consider what to do if buffer is longer than data type.
152 
153  // Depending on the data type length we use different utility functions
154  // readUint16 or readUint32 which read the data laid in the network byte
155  // order from the provided buffer. The same functions can be safely used
156  // for either unsigned or signed integers so there is not need to create
157  // special cases for intX_t types.
158  int data_size_len = OptionDataTypeTraits<T>::len;
159  switch (data_size_len) {
160  case 1:
161  value_ = *begin;
162  break;
163  case 2:
164  value_ = isc::util::readUint16(&(*begin),
165  std::distance(begin, end));
166  break;
167  case 4:
168  value_ = isc::util::readUint32(&(*begin),
169  std::distance(begin, end));
170  break;
171  default:
172  isc_throw(dhcp::InvalidDataType, "non-integer type");
173  }
174  // Use local variable to set a new value for this iterator.
175  // When using OptionDataTypeTraits<T>::len directly some versions
176  // of clang complain about unresolved reference to
177  // OptionDataTypeTraits structure during linking.
178  begin += data_size_len;
179  unpackOptions(OptionBuffer(begin, end));
180  }
181 
185  void setValue(T value) { value_ = value; }
186 
190  T getValue() const { return value_; }
191 
197  virtual uint16_t len() const {
198  // Calculate the length of the header.
199  uint16_t length = (getUniverse() == Option::V4) ? OPTION4_HDR_LEN : OPTION6_HDR_LEN;
200  // The data length is equal to size of T.
201  length += sizeof(T);;
202  // length of all suboptions
203  for (OptionCollection::const_iterator it = options_.begin();
204  it != options_.end();
205  ++it) {
206  length += (*it).second->len();
207  }
208  return (length);
209  }
210 
217  virtual std::string toText(int indent = 0) const {
218  std::stringstream output;
219  output << headerToText(indent) << ": ";
220 
221  // For 1 byte long data types we need to cast to the integer
222  // because they are usually implemented as "char" types, in
223  // which case the character rather than number would be printed.
224  if (OptionDataTypeTraits<T>::len == 1) {
225  output << static_cast<int>(getValue());
226  } else {
227  output << getValue();
228  }
229 
230  // Append data type name.
231  output << " ("
233  << ")";
234 
235  // Append suboptions.
236  output << suboptionsToText(indent + 2);
237 
238  return (output.str());
239  }
240 
241 private:
242 
243  T value_;
244 };
245 
246 } // isc::dhcp namespace
247 } // isc namespace
248 
249 #endif // OPTION_INT_H
Universe getUniverse() const
returns option universe (V4 or V6)
Definition: option.h:232
OptionInt(Option::Universe u, uint16_t type, T value)
Constructor.
Definition: option_int.h:65
void setValue(T value)
Set option value.
Definition: option_int.h:185
OptionInt< uint8_t > OptionUint8
Definition: option_int.h:30
void packHeader(isc::util::OutputBuffer &buf) const
Store option's header in a buffer.
Definition: option.cc:126
void packOptions(isc::util::OutputBuffer &buf) const
Store sub options in a buffer.
Definition: option.cc:146
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition: option_int.h:147
static const std::string & getDataTypeName(const OptionDataType data_type)
Return option data type name from the data type enumerator.
virtual uint16_t len() const
returns complete length of option
Definition: option_int.h:197
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:82
Forward declaration to OptionInt.
Exception to be thrown when invalid type specified as template parameter.
void pack(isc::util::OutputBuffer &buf) const
Writes option in wire-format to buf, returns pointer to first unused byte after stored option...
Definition: option_int.h:111
boost::shared_ptr< OptionUint32 > OptionUint32Ptr
Definition: option_int.h:35
boost::shared_ptr< OptionUint8 > OptionUint8Ptr
Definition: option_int.h:31
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition: option.h:478
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
OptionInt< uint16_t > OptionUint16
Definition: option_int.h:32
static const size_t OPTION4_HDR_LEN
length of the usual DHCPv4 option header (there are exceptions)
Definition: option.h:76
OptionInt< uint32_t > OptionUint32
Definition: option_int.h:34
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< OptionUint16 > OptionUint16Ptr
Definition: option_int.h:33
OptionCollection options_
collection for storing suboptions
Definition: option.h:575
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition: option.h:416
virtual std::string toText(int indent=0) const
Returns option carrying an integer value in the textual format.
Definition: option_int.h:217
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
uint32_t readUint32(const uint8_t *buffer, size_t length)
Read Unsigned 32-Bit Integer from Buffer.
Definition: io_utilities.h:79
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition: option.cc:323
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
Defines the logger used by the top-level component of kea-dhcp-ddns.
void unpackOptions(const OptionBuffer &buf)
Builds a collection of sub options from the buffer.
Definition: option.cc:165
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.
Definition: io_utilities.h:28
#define DHCP6_OPTION_SPACE
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:288
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:304
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition: buffer.h:466
#define DHCP4_OPTION_SPACE
global std option spaces
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
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option_int.h:99
Trait class for data types supported in DHCP option definitions.
T getValue() const
Return option value.
Definition: option_int.h:190
OptionInt(Option::Universe u, uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Constructor.
Definition: option_int.h:88
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:79