Kea 2.5.8
option.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2024 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/encode.h>
14#include <util/io.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
39Option::Option(Universe u, uint16_t type)
40 : universe_(u), type_(type) {
41 check();
42}
43
44Option::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
55Option::Option(const Option& option)
56 : universe_(option.universe_), type_(option.type_),
57 data_(option.data_), options_(),
58 encapsulated_space_(option.encapsulated_space_) {
60}
61
63Option::create(Universe u, uint16_t type) {
64 return (boost::make_shared<Option>(u, type));
65}
66
68Option::create(Universe u, uint16_t type, const OptionBuffer& data) {
69 return (boost::make_shared<Option>(u, type, data));
70}
71
72Option&
74 if (&rhs != this) {
75 universe_ = rhs.universe_;
76 type_ = rhs.type_;
77 data_ = rhs.data_;
80 }
81 return (*this);
82}
83
86 return (cloneInternal<Option>());
87}
88
89void
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
107void Option::pack(isc::util::OutputBuffer& buf, bool check) const {
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
118void
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
135void
137 switch (universe_) {
138 case V4:
140 return;
141 case V6:
143 return;
144 default:
145 isc_throw(isc::BadValue, "Invalid universe type " << universe_);
146 }
147}
148
151 setData(begin, end);
152}
153
154void
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
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 (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
189bool
191 if (universe_ != V4 &&
192 universe_ != V6) {
193 return (false);
194 }
195
196 return (true);
197}
198
199OptionPtr 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
207void
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
219bool 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
228std::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
246std::string
249 return (toText(0));
250}
251
252std::vector<uint8_t>
253Option::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
273std::string
274Option::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
287std::string
288Option::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
306std::string
307Option::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
320uint16_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 if (this == opt.get()) {
333 // Do not allow options to be added to themselves as this
334 // can lead to infinite recursion.
335 isc_throw(InvalidOperation, "option cannot be added to itself: " << toText());
336 }
337
338 options_.insert(make_pair(opt->getType(), opt));
339}
340
341uint8_t Option::getUint8() const {
342 if (data_.size() < sizeof(uint8_t) ) {
343 isc_throw(OutOfRange, "Attempt to read uint8 from option " << type_
344 << " that has size " << data_.size());
345 }
346 return (data_[0]);
347}
348
349uint16_t Option::getUint16() const {
350 // readUint16() checks and throws OutOfRange if data_ is too small.
351 return (readUint16(&data_[0], data_.size()));
352}
353
354uint32_t Option::getUint32() const {
355 // readUint32() checks and throws OutOfRange if data_ is too small.
356 return (readUint32(&data_[0], data_.size()));
357}
358
359void Option::setUint8(uint8_t value) {
360 data_.resize(sizeof(value));
361 data_[0] = value;
362}
363
364void Option::setUint16(uint16_t value) {
365 data_.resize(sizeof(value));
366 writeUint16(value, &data_[0], data_.size());
367}
368
369void Option::setUint32(uint32_t value) {
370 data_.resize(sizeof(value));
371 writeUint32(value, &data_[0], data_.size());
372}
373
374bool Option::equals(const OptionPtr& other) const {
375 return (equals(*other));
376}
377
378bool Option::equals(const Option& other) const {
379 return ((getType() == other.getType()) &&
380 (getData() == other.getData()));
381}
382
384}
385
387
388} // end of isc::dhcp namespace
389} // 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 function is called in a prohibited way.
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:287
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:1012
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:311
static void packOptions6(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options)
Stores DHCPv6 options in a buffer.
Definition: libdhcp++.cc:1238
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:590
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:288
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition: option.cc:307
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:599
std::string getEncapsulatedSpace() const
Returns the name of the option space encapsulated by this option.
Definition: option.h:442
bool equals(const OptionPtr &other) const
Checks if options are equal.
Definition: option.cc:374
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition: option.h:317
virtual ~Option()
just to force that every option has virtual dtor
Definition: option.cc:383
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in wire-format to a buffer.
Definition: option.cc:107
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
Definition: option.cc:219
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:321
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:83
Universe universe_
option universe (V4 or V6)
Definition: option.h:587
OptionPtr getOption(uint16_t type) const
Returns shared_ptr to suboption of specific type.
Definition: option.cc:199
void addOption(OptionPtr opt)
Adds a sub-option.
Definition: option.cc:331
void setUint32(uint32_t value)
Sets content of this option to a single uint32 value.
Definition: option.cc:369
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:293
OptionBuffer data_
contains content of this data
Definition: option.h:593
virtual std::string toString() const
Returns string representation of the value.
Definition: option.cc:247
void unpackOptions(const OptionBuffer &buf)
Builds a collection of sub options from the buffer.
Definition: option.cc:155
void packOptions(isc::util::OutputBuffer &buf, bool check=true) const
Store sub options in a buffer.
Definition: option.cc:136
static OptionPtr create(Universe u, uint16_t type)
Factory function creating an instance of the Option.
Definition: option.cc:63
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:80
void setUint8(uint8_t value)
Sets content of this option to a single uint8 value.
Definition: option.cc:359
Option & operator=(const Option &rhs)
Assignment operator.
Definition: option.cc:73
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition: option.h:427
virtual bool valid() const
returns if option is valid (e.g.
Definition: option.cc:190
OptionCollection options_
collection for storing suboptions
Definition: option.h:596
Universe getUniverse() const
returns option universe (V4 or V6)
Definition: option.h:233
uint8_t getUint8() const
Returns content of first byte.
Definition: option.cc:341
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:364
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option.cc:85
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition: option.cc:149
uint16_t getUint16() const
Returns content of first word.
Definition: option.cc:349
virtual std::string toText(int indent=0) const
Returns string representation of the option.
Definition: option.cc:228
void packHeader(isc::util::OutputBuffer &buf, bool check=true) const
Store option's header in a buffer.
Definition: option.cc:119
virtual std::string toHexString(const bool include_header=false) const
Returns string containing hexadecimal representation of option.
Definition: option.cc:274
uint32_t getUint32() const
Returns content of first double word.
Definition: option.cc:354
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
Definition: option.cc:39
void check() const
A protected method used for option correctness.
Definition: option.cc:90
void getOptionsCopy(OptionCollection &options_copy) const
Performs deep copy of suboptions.
Definition: option.cc:208
static const size_t OPTION4_HDR_LEN
length of the usual DHCPv4 option header (there are exceptions)
Definition: option.h:77
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:347
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition: buffer.h:474
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:498
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition: buffer.h:556
const uint8_t * getData() const
Return a pointer to the head of the data stored in the buffer.
Definition: buffer.h:398
size_t getLength() const
Return the length of data written in the buffer.
Definition: buffer.h:412
#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:1420
@ 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:37
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 format.
Definition: encode.cc:361
uint8_t * writeUint32(uint32_t const value, void *const buffer, size_t const length)
uint32_t wrapper over writeUint.
Definition: io.h:100
uint16_t readUint16(void const *const buffer, size_t const length)
uint16_t wrapper over readUint.
Definition: io.h:76
uint8_t * writeUint16(uint16_t const value, void *const buffer, size_t const length)
uint16_t wrapper over writeUint.
Definition: io.h:94
uint32_t readUint32(void const *const buffer, size_t const length)
uint32_t wrapper over readUint.
Definition: io.h:82
Defines the logger used by the top-level component of kea-lfc.