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