Kea  2.1.7-git
option.cc
Go to the documentation of this file.
1 // Copyright (C) 2011-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 #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 Option::Option(Universe u, uint16_t type)
40  : universe_(u), type_(type) {
41  check();
42 }
43 
44 Option::Option(Universe u, uint16_t type, const OptionBuffer& data)
45  : universe_(u), type_(type), data_(data) {
46  check();
47 }
48 
51  : universe_(u), type_(type), data_(first, last) {
52  check();
53 }
54 
55 Option::Option(const Option& option)
56  : universe_(option.universe_), type_(option.type_),
57  data_(option.data_), options_(),
59  option.getOptionsCopy(options_);
60 }
61 
63 Option::create(Universe u, uint16_t type) {
64  return (boost::make_shared<Option>(u, type));
65 }
66 
68 Option::create(Universe u, uint16_t type, const OptionBuffer& data) {
69  return (boost::make_shared<Option>(u, type, data));
70 }
71 
72 Option&
74  if (&rhs != this) {
75  universe_ = rhs.universe_;
76  type_ = rhs.type_;
77  data_ = rhs.data_;
80  }
81  return (*this);
82 }
83 
85 Option::clone() const {
86  return (cloneInternal<Option>());
87 }
88 
89 void
90 Option::check() const {
91  if ((universe_ != V4) && (universe_ != V6)) {
92  isc_throw(BadValue, "Invalid universe type specified. "
93  << "Only V4 and V6 are allowed.");
94  }
95 
96  if (universe_ == V4) {
97  if (type_ > 255) {
98  isc_throw(OutOfRange, "DHCPv4 Option type " << type_ << " is too big. "
99  << "For DHCPv4 allowed type range is 0..255");
100  }
101  }
102 
103  // no need to check anything for DHCPv6. It allows full range (0-64k) of
104  // both types and data size.
105 }
106 
108  // Write a header.
109  packHeader(buf, check);
110  // Write data.
111  if (!data_.empty()) {
112  buf.writeData(&data_[0], data_.size());
113  }
114  // Write sub-options.
115  packOptions(buf, check);
116 }
117 
118 void
120  if (universe_ == V4) {
121  if (check && len() > 255) {
122  isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big. "
123  << "At most 255 bytes are supported.");
124  }
125 
126  buf.writeUint8(type_);
127  buf.writeUint8(len() - getHeaderLen());
128 
129  } else {
130  buf.writeUint16(type_);
131  buf.writeUint16(len() - getHeaderLen());
132  }
133 }
134 
135 void
137  switch (universe_) {
138  case V4:
139  LibDHCP::packOptions4(buf, options_, false, check);
140  return;
141  case V6:
143  return;
144  default:
145  isc_throw(isc::BadValue, "Invalid universe type " << universe_);
146  }
147 }
148 
150  OptionBufferConstIter end) {
151  setData(begin, end);
152 }
153 
154 void
156  list<uint16_t> deferred;
157  switch (universe_) {
158  case V4:
160  options_, deferred,
162  return;
163  case V6:
165  return;
166  default:
167  isc_throw(isc::BadValue, "Invalid universe type " << universe_);
168  }
169 }
170 
171 uint16_t Option::len() const {
172  // Returns length of the complete option (data length + DHCPv4/DHCPv6
173  // option header)
174 
175  // length of the whole option is header and data stored in this option...
176  size_t length = getHeaderLen() + data_.size();
177 
178  // ... and sum of lengths of all suboptions
179  for (auto const& option : options_) {
180  length += option.second->len();
181  }
182 
183  // note that this is not equal to length field. This value denotes
184  // number of bytes required to store this option. length option should
185  // contain (len()-getHeaderLen()) value.
186  return (static_cast<uint16_t>(length));
187 }
188 
189 bool
190 Option::valid() const {
191  if (universe_ != V4 &&
192  universe_ != V6) {
193  return (false);
194  }
195 
196  return (true);
197 }
198 
199 OptionPtr Option::getOption(uint16_t opt_type) const {
200  auto const& x = options_.find(opt_type);
201  if (x != options_.end()) {
202  return (x->second);
203  }
204  return OptionPtr(); // NULL
205 }
206 
207 void
209  OptionCollection local_options;
210  for (auto const& option : options_) {
211  OptionPtr copy = option.second->clone();
212  local_options.insert(std::make_pair(option.second->getType(), copy));
213  }
214  // All options copied successfully, so assign them to the output
215  // parameter.
216  options_copy.swap(local_options);
217 }
218 
219 bool Option::delOption(uint16_t opt_type) {
220  auto const& x = options_.find(opt_type);
221  if (x != options_.end()) {
222  options_.erase(x);
223  return (true); // delete successful
224  }
225  return (false); // option not found, can't delete
226 }
227 
228 std::string Option::toText(int indent) const {
229  std::stringstream output;
230  output << headerToText(indent) << ": ";
231 
232  for (unsigned int i = 0; i < data_.size(); i++) {
233  if (i) {
234  output << ":";
235  }
236  output << setfill('0') << setw(2) << hex
237  << static_cast<unsigned short>(data_[i]);
238  }
239 
240  // Append suboptions.
241  output << suboptionsToText(indent + 2);
242 
243  return (output.str());
244 }
245 
246 std::string
249  return (toText(0));
250 }
251 
252 std::vector<uint8_t>
253 Option::toBinary(const bool include_header) const {
254  OutputBuffer buf(len());
255  try {
256  // The RFC3396 adds support for long options split over multiple options
257  // using the same code.
258  pack(buf, false);
259 
260  } catch (const std::exception &ex) {
261  isc_throw(OutOfRange, "unable to obtain hexadecimal representation"
262  " of option " << getType() << ": " << ex.what());
263  }
264  const uint8_t* option_data = static_cast<const uint8_t*>(buf.getData());
265 
266  // Assign option data to a vector, with or without option header depending
267  // on the value of "include_header" flag.
268  std::vector<uint8_t> option_vec(option_data + (include_header ? 0 : getHeaderLen()),
269  option_data + buf.getLength());
270  return (option_vec);
271 }
272 
273 std::string
274 Option::toHexString(const bool include_header) const {
275  // Prepare binary version of the option.
276  std::vector<uint8_t> option_vec = toBinary(include_header);
277 
278  // Return hexadecimal representation prepended with 0x or empty string
279  // if option has no payload and the header fields are excluded.
280  std::ostringstream s;
281  if (!option_vec.empty()) {
282  s << "0x" << encode::encodeHex(option_vec);
283  }
284  return (s.str());
285 }
286 
287 std::string
288 Option::headerToText(const int indent, const std::string& type_name) const {
289  std::stringstream output;
290  for (int i = 0; i < indent; i++)
291  output << " ";
292 
293  int field_len = (getUniverse() == V4 ? 3 : 5);
294  output << "type=" << std::setw(field_len) << std::setfill('0')
295  << type_;
296 
297  if (!type_name.empty()) {
298  output << "(" << type_name << ")";
299  }
300 
301  output << ", len=" << std::setw(field_len) << std::setfill('0')
302  << len() - getHeaderLen();
303  return (output.str());
304 }
305 
306 std::string
307 Option::suboptionsToText(const int indent) const {
308  std::stringstream output;
309 
310  if (!options_.empty()) {
311  output << "," << std::endl << "options:";
312  for (auto const& opt : options_) {
313  output << std::endl << opt.second->toText(indent);
314  }
315  }
316 
317  return (output.str());
318 }
319 
320 uint16_t
322  switch (universe_) {
323  case V4:
324  return OPTION4_HDR_LEN; // header length for v4
325  case V6:
326  return OPTION6_HDR_LEN; // header length for v6
327  }
328  return 0; // should not happen
329 }
330 
332  options_.insert(make_pair(opt->getType(), opt));
333 }
334 
335 uint8_t Option::getUint8() const {
336  if (data_.size() < sizeof(uint8_t) ) {
337  isc_throw(OutOfRange, "Attempt to read uint8 from option " << type_
338  << " that has size " << data_.size());
339  }
340  return (data_[0]);
341 }
342 
343 uint16_t Option::getUint16() const {
344  // readUint16() checks and throws OutOfRange if data_ is too small.
345  return (readUint16(&data_[0], data_.size()));
346 }
347 
348 uint32_t Option::getUint32() const {
349  // readUint32() checks and throws OutOfRange if data_ is too small.
350  return (readUint32(&data_[0], data_.size()));
351 }
352 
353 void Option::setUint8(uint8_t value) {
354  data_.resize(sizeof(value));
355  data_[0] = value;
356 }
357 
358 void Option::setUint16(uint16_t value) {
359  data_.resize(sizeof(value));
360  writeUint16(value, &data_[0], data_.size());
361 }
362 
363 void Option::setUint32(uint32_t value) {
364  data_.resize(sizeof(value));
365  writeUint32(value, &data_[0], data_.size());
366 }
367 
368 bool Option::equals(const OptionPtr& other) const {
369  return (equals(*other));
370 }
371 
372 bool Option::equals(const Option& other) const {
373  return ((getType() == other.getType()) &&
374  (getData() == other.getData()));
375 }
376 
378 }
379 
381 
382 } // end of isc::dhcp namespace
383 } // end of isc namespace
void getOptionsCopy(OptionCollection &options_copy) const
Performs deep copy of suboptions.
Definition: option.cc:208
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition: option.cc:307
uint16_t getUint16() const
Returns content of first word.
Definition: option.cc:343
uint8_t * writeUint32(uint32_t value, uint8_t *buffer, size_t length)
Write Unsigned 32-Bit Integer to Buffer.
Definition: io_utilities.h:136
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:293
static void packOptions6(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options)
Stores DHCPv6 options in a buffer.
Definition: libdhcp++.cc:1069
virtual ~Option()
just to force that every option has virtual dtor
Definition: option.cc:377
virtual std::vector< uint8_t > toBinary(const bool include_header=false) const
Returns binary representation of the option.
Definition: option.cc:253
void setUint16(uint16_t value)
Sets content of this option to a single uint16 value.
Definition: option.cc:358
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:83
void packHeader(isc::util::OutputBuffer &buf, bool check=true) const
Store option&#39;s header in a buffer.
Definition: option.cc:119
STL namespace.
uint32_t getUint32() const
Returns content of first double word.
Definition: option.cc:348
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
Definition: option.cc:39
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:321
Universe universe_
option universe (V4 or V6)
Definition: option.h:585
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:77
void setUint32(uint32_t value)
Sets content of this option to a single uint32 value.
Definition: option.cc:363
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:308
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...
void packOptions(isc::util::OutputBuffer &buf, bool check=true) const
Store sub options in a buffer.
Definition: option.cc:136
static void packOptions4(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options, bool top=false, bool check=true)
Stores DHCPv4 options in a buffer.
Definition: libdhcp++.cc:888
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:288
Definition: edns.h:19
Universe getUniverse() const
returns option universe (V4 or V6)
Definition: option.h:233
virtual std::string toString() const
Returns string representation of the value.
Definition: option.cc:247
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1152
uint8_t * writeUint16(uint16_t value, void *buffer, size_t length)
Write Unsigned 16-Bit Integer to Buffer.
Definition: io_utilities.h:55
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option.cc:85
bool equals(const OptionPtr &other) const
Checks if options are equal.
Definition: option.cc:368
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
Definition: option.cc:219
OptionPtr getOption(uint16_t type) const
Returns shared_ptr to suboption of specific type.
Definition: option.cc:199
OptionCollection options_
collection for storing suboptions
Definition: option.h:594
const void * getData() const
Return a pointer to the head of the data stored in the buffer.
Definition: buffer.h:401
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
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:331
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
size_t getLength() const
Return the length of data written in the buffer.
Definition: buffer.h:403
Defines the logger used by the top-level component of kea-lfc.
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header) ...
Definition: option.cc:171
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 (&#39;hex&#39;) format.
Definition: base_n.cc:469
virtual bool valid() const
returns if option is valid (e.g.
Definition: option.cc:190
Option & operator=(const Option &rhs)
Assignment operator.
Definition: option.cc:73
void unpackOptions(const OptionBuffer &buf)
Builds a collection of sub options from the buffer.
Definition: option.cc:155
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.
Definition: io_utilities.h:28
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in wire-format to a buffer.
Definition: option.cc:107
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition: buffer.h:466
std::string getEncapsulatedSpace() const
Returns the name of the option space encapsulated by this option.
Definition: option.h:442
static OptionPtr create(Universe u, uint16_t type)
Factory function creating an instance of the Option.
Definition: option.cc:63
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition: option.cc:149
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:353
OptionBuffer data_
contains content of this data
Definition: option.h:591
virtual std::string toHexString(const bool include_header=false) const
Returns string containing hexadecimal representation of option.
Definition: option.cc:274
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:588
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition: option.h:427
uint8_t getUint8() const
Returns content of first byte.
Definition: option.cc:335
static bool lenient_parsing_
Governs whether options should be parsed less strictly.
Definition: option.h:483
std::string encapsulated_space_
Name of the option space being encapsulated by this option.
Definition: option.h:597
virtual std::string toText(int indent=0) const
Returns string representation of the option.
Definition: option.cc:228
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:457
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:80