Kea  1.9.9-git
option.cc
Go to the documentation of this file.
1 // Copyright (C) 2011-2021 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/dhcp4.h>
9 #include <dhcp/libdhcp++.h>
10 #include <dhcp/option.h>
11 #include <dhcp/option_space.h>
12 #include <exceptions/exceptions.h>
13 #include <util/encode/hex.h>
14 #include <util/io_utilities.h>
15 
16 #include <boost/make_shared.hpp>
17 
18 #include <iomanip>
19 #include <list>
20 #include <sstream>
21 
22 #include <arpa/inet.h>
23 #include <stdint.h>
24 #include <string.h>
25 
26 using namespace std;
27 using namespace isc::util;
28 
29 namespace isc {
30 namespace dhcp {
31 
33 Option::factory(Option::Universe u,
34  uint16_t type,
35  const OptionBuffer& buf) {
36  return(LibDHCP::optionFactory(u, type, buf));
37 }
38 
39 
40 Option::Option(Universe u, uint16_t type)
41  :universe_(u), type_(type) {
42  check();
43 }
44 
45 Option::Option(Universe u, uint16_t type, const OptionBuffer& data)
46  :universe_(u), type_(type), data_(data) {
47  check();
48 }
49 
52  :universe_(u), type_(type), data_(first, last) {
53  check();
54 }
55 
56 Option::Option(const Option& option)
57  : universe_(option.universe_), type_(option.type_),
58  data_(option.data_), options_(),
59  encapsulated_space_(option.encapsulated_space_) {
60  option.getOptionsCopy(options_);
61 }
62 
64 Option::create(Universe u, uint16_t type) {
65  return (boost::make_shared<Option>(u, type));
66 }
67 
69 Option::create(Universe u, uint16_t type, const OptionBuffer& data) {
70  return (boost::make_shared<Option>(u, type, data));
71 }
72 
73 Option&
75  if (&rhs != this) {
76  universe_ = rhs.universe_;
77  type_ = rhs.type_;
78  data_ = rhs.data_;
81  }
82  return (*this);
83 }
84 
86 Option::clone() const {
87  return (cloneInternal<Option>());
88 }
89 
90 void
91 Option::check() const {
92  if ( (universe_ != V4) && (universe_ != V6) ) {
93  isc_throw(BadValue, "Invalid universe type specified. "
94  << "Only V4 and V6 are allowed.");
95  }
96 
97  if (universe_ == V4) {
98 
99  if (type_ > 255) {
100  isc_throw(OutOfRange, "DHCPv4 Option type " << type_ << " is too big. "
101  << "For DHCPv4 allowed type range is 0..255");
102  } else if (data_.size() > 255) {
103  isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big.");
107  }
108  }
109 
110  // no need to check anything for DHCPv6. It allows full range (0-64k) of
111  // both types and data size.
112 }
113 
115  // Write a header.
116  packHeader(buf);
117  // Write data.
118  if (!data_.empty()) {
119  buf.writeData(&data_[0], data_.size());
120  }
121  // Write sub-options.
122  packOptions(buf);
123 }
124 
125 void
127  if (universe_ == V4) {
128  if (len() > 255) {
129  isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big. "
130  << "At most 255 bytes are supported.");
134  }
135 
136  buf.writeUint8(type_);
137  buf.writeUint8(len() - getHeaderLen());
138 
139  } else {
140  buf.writeUint16(type_);
141  buf.writeUint16(len() - getHeaderLen());
142  }
143 }
144 
145 void
147  switch (universe_) {
148  case V4:
150  return;
151  case V6:
153  return;
154  default:
155  isc_throw(isc::BadValue, "Invalid universe type " << universe_);
156  }
157 }
158 
160  OptionBufferConstIter end) {
161  setData(begin, end);
162 }
163 
164 void
166  list<uint16_t> deferred;
167  switch (universe_) {
168  case V4:
170  options_, deferred,
172  return;
173  case V6:
175  return;
176  default:
177  isc_throw(isc::BadValue, "Invalid universe type " << universe_);
178  }
179 }
180 
181 uint16_t Option::len() const {
182  // Returns length of the complete option (data length + DHCPv4/DHCPv6
183  // option header)
184 
185  // length of the whole option is header and data stored in this option...
186  size_t length = getHeaderLen() + data_.size();
187 
188  // ... and sum of lengths of all suboptions
189  for (OptionCollection::const_iterator it = options_.begin();
190  it != options_.end();
191  ++it) {
192  length += (*it).second->len();
193  }
194 
195  // note that this is not equal to length field. This value denotes
196  // number of bytes required to store this option. length option should
197  // contain (len()-getHeaderLen()) value.
198  return (static_cast<uint16_t>(length));
199 }
200 
201 bool
202 Option::valid() const {
203  if (universe_ != V4 &&
204  universe_ != V6) {
205  return (false);
206  }
207 
208  return (true);
209 }
210 
211 OptionPtr Option::getOption(uint16_t opt_type) const {
212  isc::dhcp::OptionCollection::const_iterator x =
213  options_.find(opt_type);
214  if ( x != options_.end() ) {
215  return (*x).second;
216  }
217  return OptionPtr(); // NULL
218 }
219 
220 void
222  OptionCollection local_options;
223  for (OptionCollection::const_iterator it = options_.begin();
224  it != options_.end(); ++it) {
225  OptionPtr copy = it->second->clone();
226  local_options.insert(std::make_pair(it->second->getType(),
227  copy));
228  }
229  // All options copied successfully, so assign them to the output
230  // parameter.
231  options_copy.swap(local_options);
232 }
233 
234 bool Option::delOption(uint16_t opt_type) {
235  isc::dhcp::OptionCollection::iterator x = options_.find(opt_type);
236  if ( x != options_.end() ) {
237  options_.erase(x);
238  return true; // delete successful
239  }
240  return (false); // option not found, can't delete
241 }
242 
243 
244 std::string Option::toText(int indent) const {
245  std::stringstream output;
246  output << headerToText(indent) << ": ";
247 
248  for (unsigned int i = 0; i < data_.size(); i++) {
249  if (i) {
250  output << ":";
251  }
252  output << setfill('0') << setw(2) << hex
253  << static_cast<unsigned short>(data_[i]);
254  }
255 
256  // Append suboptions.
257  output << suboptionsToText(indent + 2);
258 
259  return (output.str());
260 }
261 
262 std::string
265  return (toText(0));
266 }
267 
268 std::vector<uint8_t>
269 Option::toBinary(const bool include_header) const {
270  OutputBuffer buf(len());
271  try {
272  // If the option is too long, exception will be thrown. We allow
273  // for this exception to propagate to not mask this error.
274  pack(buf);
275 
276  } catch (const std::exception &ex) {
277  isc_throw(OutOfRange, "unable to obtain hexadecimal representation"
278  " of option " << getType() << ": " << ex.what());
279  }
280  const uint8_t* option_data = static_cast<const uint8_t*>(buf.getData());
281 
282  // Assign option data to a vector, with or without option header depending
283  // on the value of "include_header" flag.
284  std::vector<uint8_t> option_vec(option_data + (include_header ? 0 : getHeaderLen()),
285  option_data + buf.getLength());
286  return (option_vec);
287 }
288 
289 std::string
290 Option::toHexString(const bool include_header) const {
291  // Prepare binary version of the option.
292  std::vector<uint8_t> option_vec = toBinary(include_header);
293 
294  // Return hexadecimal representation prepended with 0x or empty string
295  // if option has no payload and the header fields are excluded.
296  std::ostringstream s;
297  if (!option_vec.empty()) {
298  s << "0x" << encode::encodeHex(option_vec);
299  }
300  return (s.str());
301 }
302 
303 std::string
304 Option::headerToText(const int indent, const std::string& type_name) const {
305  std::stringstream output;
306  for (int i = 0; i < indent; i++)
307  output << " ";
308 
309  int field_len = (getUniverse() == V4 ? 3 : 5);
310  output << "type=" << std::setw(field_len) << std::setfill('0')
311  << type_;
312 
313  if (!type_name.empty()) {
314  output << "(" << type_name << ")";
315  }
316 
317  output << ", len=" << std::setw(field_len) << std::setfill('0')
318  << len()-getHeaderLen();
319  return (output.str());
320 }
321 
322 std::string
323 Option::suboptionsToText(const int indent) const {
324  std::stringstream output;
325 
326  if (!options_.empty()) {
327  output << "," << std::endl << "options:";
328  for (OptionCollection::const_iterator opt = options_.begin();
329  opt != options_.end(); ++opt) {
330  output << std::endl << (*opt).second->toText(indent);
331  }
332  }
333 
334  return (output.str());
335 }
336 
337 uint16_t
339  switch (universe_) {
340  case V4:
341  return OPTION4_HDR_LEN; // header length for v4
342  case V6:
343  return OPTION6_HDR_LEN; // header length for v6
344  }
345  return 0; // should not happen
346 }
347 
349  if (universe_ == V4) {
350  // check for uniqueness (DHCPv4 options must be unique)
351  if (getOption(opt->getType())) {
352  isc_throw(BadValue, "Option " << opt->getType()
353  << " already present in this message.");
354  }
355  }
356  options_.insert(make_pair(opt->getType(), opt));
357 }
358 
359 uint8_t Option::getUint8() const {
360  if (data_.size() < sizeof(uint8_t) ) {
361  isc_throw(OutOfRange, "Attempt to read uint8 from option " << type_
362  << " that has size " << data_.size());
363  }
364  return (data_[0]);
365 }
366 
367 uint16_t Option::getUint16() const {
368  // readUint16() checks and throws OutOfRange if data_ is too small.
369  return (readUint16(&data_[0], data_.size()));
370 }
371 
372 uint32_t Option::getUint32() const {
373  // readUint32() checks and throws OutOfRange if data_ is too small.
374  return (readUint32(&data_[0], data_.size()));
375 }
376 
377 void Option::setUint8(uint8_t value) {
378  data_.resize(sizeof(value));
379  data_[0] = value;
380 }
381 
382 void Option::setUint16(uint16_t value) {
383  data_.resize(sizeof(value));
384  writeUint16(value, &data_[0], data_.size());
385 }
386 
387 void Option::setUint32(uint32_t value) {
388  data_.resize(sizeof(value));
389  writeUint32(value, &data_[0], data_.size());
390 }
391 
392 bool Option::equals(const OptionPtr& other) const {
393  return (equals(*other));
394 }
395 
396 bool Option::equals(const Option& other) const {
397  return ( (getType() == other.getType()) &&
398  (getData() == other.getData()) );
399 }
400 
402 
403 }
404 
406 
407 } // end of isc::dhcp namespace
408 } // end of isc namespace
Universe getUniverse() const
returns option universe (V4 or V6)
Definition: option.h:232
const void * getData() const
Return a pointer to the head of the data stored in the buffer.
Definition: buffer.h:401
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 std::string toHexString(const bool include_header=false) const
Returns string containing hexadecimal representation of option.
Definition: option.cc:290
uint8_t * writeUint32(uint32_t value, uint8_t *buffer, size_t length)
Write Unsigned 32-Bit Integer to Buffer.
Definition: io_utilities.h:136
static void packOptions6(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options)
Stores DHCPv6 options in a buffer.
Definition: libdhcp++.cc:862
virtual ~Option()
just to force that every option has virtual dtor
Definition: option.cc:401
OptionPtr getOption(uint16_t type) const
Returns shared_ptr to suboption of specific type.
Definition: option.cc:211
bool equals(const OptionPtr &other) const
Checks if options are equal.
Definition: option.cc:392
virtual std::vector< uint8_t > toBinary(const bool include_header=false) const
Returns binary representation of the option.
Definition: option.cc:269
virtual void pack(isc::util::OutputBuffer &buf) const
Writes option in wire-format to a buffer.
Definition: option.cc:114
void setUint16(uint16_t value)
Sets content of this option to a single uint16 value.
Definition: option.cc:382
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:82
STL namespace.
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition: option.h:310
virtual std::string toString() const
Returns string representation of the value.
Definition: option.cc:263
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
Definition: option.cc:40
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:338
Universe universe_
option universe (V4 or V6)
Definition: option.h:566
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
static const size_t OPTION4_HDR_LEN
length of the usual DHCPv4 option header (there are exceptions)
Definition: option.h:76
void setUint32(uint32_t value)
Sets content of this option to a single uint32 value.
Definition: option.cc:387
static void packOptions4(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options, bool top=false)
Stores DHCPv4 options in a buffer.
Definition: libdhcp++.cc:814
uint8_t getUint8() const
Returns content of first byte.
Definition: option.cc:359
static size_t unpackOptions6(const OptionBuffer &buf, const std::string &option_space, isc::dhcp::OptionCollection &options, size_t *relay_msg_offset=0, size_t *relay_msg_len=0)
Parses provided buffer as DHCPv6 options and creates Option objects.
Definition: libdhcp++.cc:310
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.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition: edns.h:19
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1097
uint8_t * writeUint16(uint16_t value, void *buffer, size_t length)
Write Unsigned 16-Bit Integer to Buffer.
Definition: io_utilities.h:55
void check() const
A protected method used for option correctness.
Definition: option.cc:91
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
Definition: option.cc:234
OptionCollection options_
collection for storing suboptions
Definition: option.h:575
virtual std::string toText(int indent=0) const
Returns string representation of the option.
Definition: option.cc:244
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition: option.h:40
void addOption(OptionPtr opt)
Adds a sub-option.
Definition: option.cc:348
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option.cc:86
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
Defines the logger used by the top-level component of kea-dhcp-ddns.
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 ('hex') format.
Definition: base_n.cc:469
Option & operator=(const Option &rhs)
Assignment operator.
Definition: option.cc:74
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
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
static OptionPtr create(Universe u, uint16_t type)
Factory function creating an instance of the Option.
Definition: option.cc:64
std::string getEncapsulatedSpace() const
Returns the name of the option space encapsulated by this option.
Definition: option.h:423
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition: option.cc:159
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...
void setUint8(uint8_t value)
Sets content of this option to a single uint8 value.
Definition: option.cc:377
uint32_t getUint32() const
Returns content of first double word.
Definition: option.cc:372
OptionBuffer data_
contains content of this data
Definition: option.h:572
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:569
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition: option.h:408
void getOptionsCopy(OptionCollection &options_copy) const
Performs deep copy of suboptions.
Definition: option.cc:221
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header) ...
Definition: option.cc:181
size_t getLength() const
Return the length of data written in the buffer.
Definition: buffer.h:403
uint16_t getUint16() const
Returns content of first word.
Definition: option.cc:367
static bool lenient_parsing_
Governs whether options should be parsed less strictly.
Definition: option.h:464
std::string encapsulated_space_
Name of the option space being encapsulated by this option.
Definition: option.h:578
static size_t unpackOptions4(const OptionBuffer &buf, const std::string &option_space, isc::dhcp::OptionCollection &options, std::list< uint16_t > &deferred, bool flexible_pad_end=false)
Parses provided buffer as DHCPv4 options and creates Option objects.
Definition: libdhcp++.cc:459
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:79
virtual bool valid() const
returns if option is valid (e.g.
Definition: option.cc:202