Kea 2.0.2
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>
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
26using namespace std;
27using namespace isc::util;
28
29namespace isc {
30namespace dhcp {
31
34 uint16_t type,
35 const OptionBuffer& buf) {
36 return(LibDHCP::optionFactory(u, type, buf));
37}
38
39
40Option::Option(Universe u, uint16_t type)
41 :universe_(u), type_(type) {
42 check();
43}
44
45Option::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
56Option::Option(const Option& option)
57 : universe_(option.universe_), type_(option.type_),
58 data_(option.data_), options_(),
59 encapsulated_space_(option.encapsulated_space_) {
61}
62
64Option::create(Universe u, uint16_t type) {
65 return (boost::make_shared<Option>(u, type));
66}
67
69Option::create(Universe u, uint16_t type, const OptionBuffer& data) {
70 return (boost::make_shared<Option>(u, type, data));
71}
72
73Option&
75 if (&rhs != this) {
76 universe_ = rhs.universe_;
77 type_ = rhs.type_;
78 data_ = rhs.data_;
81 }
82 return (*this);
83}
84
87 return (cloneInternal<Option>());
88}
89
90void
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
125void
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
145void
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
161 setData(begin, end);
162}
163
164void
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
181uint16_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
201bool
203 if (universe_ != V4 &&
204 universe_ != V6) {
205 return (false);
206 }
207
208 return (true);
209}
210
211OptionPtr 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
220void
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
234bool 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
244std::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
262std::string
265 return (toText(0));
266}
267
268std::vector<uint8_t>
269Option::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
289std::string
290Option::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
303std::string
304Option::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
322std::string
323Option::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
337uint16_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
359uint8_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
367uint16_t Option::getUint16() const {
368 // readUint16() checks and throws OutOfRange if data_ is too small.
369 return (readUint16(&data_[0], data_.size()));
370}
371
372uint32_t Option::getUint32() const {
373 // readUint32() checks and throws OutOfRange if data_ is too small.
374 return (readUint32(&data_[0], data_.size()));
375}
376
377void Option::setUint8(uint8_t value) {
378 data_.resize(sizeof(value));
379 data_[0] = value;
380}
381
382void Option::setUint16(uint16_t value) {
383 data_.resize(sizeof(value));
384 writeUint16(value, &data_[0], data_.size());
385}
386
387void Option::setUint32(uint32_t value) {
388 data_.resize(sizeof(value));
389 writeUint32(value, &data_[0], data_.size());
390}
391
392bool Option::equals(const OptionPtr& other) const {
393 return (equals(*other));
394}
395
396bool 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
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
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 isc::dhcp::OptionPtr optionFactory(isc::dhcp::Option::Universe u, uint16_t type, const OptionBuffer &buf)
Factory function to create instance of option.
Definition: libdhcp++.cc:285
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
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
static void packOptions6(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options)
Stores DHCPv6 options in a buffer.
Definition: libdhcp++.cc:862
static OptionPtr factory(Option::Universe u, uint16_t type, const OptionBuffer &buf)
Factory function to create instance of option.
Definition: option.cc:33
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:569
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:304
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition: option.cc:323
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
std::string getEncapsulatedSpace() const
Returns the name of the option space encapsulated by this option.
Definition: option.h:423
bool equals(const OptionPtr &other) const
Checks if options are equal.
Definition: option.cc:392
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition: option.h:310
virtual ~Option()
just to force that every option has virtual dtor
Definition: option.cc:401
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
Definition: option.cc:234
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:338
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header)
Definition: option.cc:181
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:82
Universe universe_
option universe (V4 or V6)
Definition: option.h:566
virtual void pack(isc::util::OutputBuffer &buf) const
Writes option in wire-format to a buffer.
Definition: option.cc:114
OptionPtr getOption(uint16_t type) const
Returns shared_ptr to suboption of specific type.
Definition: option.cc:211
void addOption(OptionPtr opt)
Adds a sub-option.
Definition: option.cc:348
void setUint32(uint32_t value)
Sets content of this option to a single uint32 value.
Definition: option.cc:387
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:288
OptionBuffer data_
contains content of this data
Definition: option.h:572
virtual std::string toString() const
Returns string representation of the value.
Definition: option.cc:263
void unpackOptions(const OptionBuffer &buf)
Builds a collection of sub options from the buffer.
Definition: option.cc:165
static OptionPtr create(Universe u, uint16_t type)
Factory function creating an instance of the Option.
Definition: option.cc:64
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:79
void setUint8(uint8_t value)
Sets content of this option to a single uint8 value.
Definition: option.cc:377
void packOptions(isc::util::OutputBuffer &buf) const
Store sub options in a buffer.
Definition: option.cc:146
Option & operator=(const Option &rhs)
Assignment operator.
Definition: option.cc:74
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition: option.h:408
virtual bool valid() const
returns if option is valid (e.g.
Definition: option.cc:202
OptionCollection options_
collection for storing suboptions
Definition: option.h:575
Universe getUniverse() const
returns option universe (V4 or V6)
Definition: option.h:232
uint8_t getUint8() const
Returns content of first byte.
Definition: option.cc:359
virtual std::vector< uint8_t > toBinary(const bool include_header=false) const
Returns binary representation of the option.
Definition: option.cc:269
void setUint16(uint16_t value)
Sets content of this option to a single uint16 value.
Definition: option.cc:382
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option.cc:86
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition: option.cc:159
uint16_t getUint16() const
Returns content of first word.
Definition: option.cc:367
virtual std::string toText(int indent=0) const
Returns string representation of the option.
Definition: option.cc:244
void packHeader(isc::util::OutputBuffer &buf) const
Store option's header in a buffer.
Definition: option.cc:126
virtual std::string toHexString(const bool include_header=false) const
Returns string containing hexadecimal representation of option.
Definition: option.cc:290
uint32_t getUint32() const
Returns content of first double word.
Definition: option.cc:372
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
Definition: option.cc:40
void check() const
A protected method used for option correctness.
Definition: option.cc:91
void getOptionsCopy(OptionCollection &options_copy) const
Performs deep copy of suboptions.
Definition: option.cc:221
static const size_t OPTION4_HDR_LEN
length of the usual DHCPv4 option header (there are exceptions)
Definition: option.h:76
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 writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition: buffer.h:550
size_t getLength() const
Return the length of data written in the buffer.
Definition: buffer.h:403
const void * getData() const
Return a pointer to the head of the data stored in the buffer.
Definition: buffer.h:401
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1152
@ DHO_VENDOR_ENCAPSULATED_OPTIONS
Definition: dhcp4.h:112
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition: option.h:40
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 ('hex') format.
Definition: base_n.cc:469
Definition: edns.h:19
uint8_t * writeUint16(uint16_t value, void *buffer, size_t length)
Write Unsigned 16-Bit Integer to Buffer.
Definition: io_utilities.h:55
uint8_t * writeUint32(uint32_t value, uint8_t *buffer, size_t length)
Write Unsigned 32-Bit Integer to Buffer.
Definition: io_utilities.h:136
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.