Kea 3.2.0-git
option.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2026 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_(),
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
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
155Option::unpackOptions(const OptionBuffer& buf, size_t rec_level /* = 0 */) {
156 list<uint16_t> deferred;
157 switch (universe_) {
158 case V4:
160 options_, deferred,
162 return;
163 case V6:
165 0, 0, rec_level);
166 return;
167 default:
168 isc_throw(isc::BadValue, "Invalid universe type " << universe_);
169 }
170}
171
172uint16_t Option::len() const {
173 // Returns length of the complete option (data length + DHCPv4/DHCPv6
174 // option header)
175
176 // length of the whole option is header and data stored in this option...
177 size_t length = getHeaderLen() + data_.size();
178
179 // ... and sum of lengths of all suboptions
180 for (auto const& option : options_) {
181 length += option.second->len();
182 }
183
184 // note that this is not equal to length field. This value denotes
185 // number of bytes required to store this option. length option should
186 // contain (len()-getHeaderLen()) value.
187 return (static_cast<uint16_t>(length));
188}
189
190bool
192 if (universe_ != V4 &&
193 universe_ != V6) {
194 return (false);
195 }
196
197 return (true);
198}
199
200OptionPtr Option::getOption(uint16_t opt_type) const {
201 auto const& x = options_.find(opt_type);
202 if (x != options_.end()) {
203 return (x->second);
204 }
205 return OptionPtr(); // NULL
206}
207
208void
210 OptionCollection local_options;
211 for (auto const& option : options_) {
212 OptionPtr copy = option.second->clone();
213 local_options.insert(std::make_pair(option.second->getType(), copy));
214 }
215 // All options copied successfully, so assign them to the output
216 // parameter.
217 options_copy.swap(local_options);
218}
219
220bool Option::delOption(uint16_t opt_type) {
221 auto const& x = options_.find(opt_type);
222 if (x != options_.end()) {
223 options_.erase(x);
224 return (true); // delete successful
225 }
226 return (false); // option not found, can't delete
227}
228
229std::string Option::toText(int indent) const {
230 std::stringstream output;
231 output << headerToText(indent) << ": ";
232
233 for (unsigned int i = 0; i < data_.size(); i++) {
234 if (i) {
235 output << ":";
236 }
237 output << setfill('0') << setw(2) << hex
238 << static_cast<unsigned short>(data_[i]);
239 }
240 if (data_.empty()) {
241 output << "''";
242 } else if (str::isPrintable(data_)) {
243 std::string printable(data_.cbegin(), data_.cend());
244 output << " '" << printable << "'";
245 }
246
247 // Append suboptions.
248 output << suboptionsToText(indent + 2);
249
250 return (output.str());
251}
252
253std::string
256 return (toText(0));
257}
258
259std::vector<uint8_t>
260Option::toBinary(const bool include_header) const {
261 OutputBuffer buf(len());
262 try {
263 // The RFC3396 adds support for long options split over multiple options
264 // using the same code.
265 pack(buf, false);
266
267 } catch (const std::exception &ex) {
268 isc_throw(OutOfRange, "unable to obtain hexadecimal representation"
269 " of option " << getType() << ": " << ex.what());
270 }
271 const uint8_t* option_data = static_cast<const uint8_t*>(buf.getData());
272
273 // Assign option data to a vector, with or without option header depending
274 // on the value of "include_header" flag.
275 std::vector<uint8_t> option_vec(option_data + (include_header ? 0 : getHeaderLen()),
276 option_data + buf.getLength());
277 return (option_vec);
278}
279
280std::string
281Option::toHexString(const bool include_header) const {
282 // Prepare binary version of the option.
283 std::vector<uint8_t> option_vec = toBinary(include_header);
284
285 // Return hexadecimal representation prepended with 0x or empty string
286 // if option has no payload and the header fields are excluded.
287 std::ostringstream s;
288 if (!option_vec.empty()) {
289 s << "0x" << encode::encodeHex(option_vec);
290 }
291 return (s.str());
292}
293
294std::string
295Option::headerToText(const int indent, const std::string& type_name) const {
296 std::stringstream output;
297 for (int i = 0; i < indent; i++)
298 output << " ";
299
300 int field_len = (getUniverse() == V4 ? 3 : 5);
301 output << "type=" << std::setw(field_len) << std::setfill('0')
302 << type_;
303
304 if (!type_name.empty()) {
305 output << "(" << type_name << ")";
306 }
307
308 output << ", len=" << std::setw(field_len) << std::setfill('0')
309 << len() - getHeaderLen();
310 return (output.str());
311}
312
313std::string
314Option::suboptionsToText(const int indent) const {
315 std::stringstream output;
316
317 if (!options_.empty()) {
318 output << "," << std::endl << "options:";
319 for (auto const& opt : options_) {
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 (this == opt.get()) {
340 // Do not allow options to be added to themselves as this
341 // can lead to infinite recursion.
342 isc_throw(InvalidOperation, "option cannot be added to itself: " << toText());
343 }
344
345 options_.insert(make_pair(opt->getType(), opt));
346}
347
348uint8_t Option::getUint8() const {
349 if (data_.size() < sizeof(uint8_t) ) {
350 isc_throw(OutOfRange, "Attempt to read uint8 from option " << type_
351 << " that has size " << data_.size());
352 }
353 return (data_[0]);
354}
355
356uint16_t Option::getUint16() const {
357 // readUint16() checks and throws OutOfRange if data_ is too small.
358 return (readUint16(&data_[0], data_.size()));
359}
360
361uint32_t Option::getUint32() const {
362 // readUint32() checks and throws OutOfRange if data_ is too small.
363 return (readUint32(&data_[0], data_.size()));
364}
365
366void Option::setUint8(uint8_t value) {
367 data_.resize(sizeof(value));
368 data_[0] = value;
369}
370
371void Option::setUint16(uint16_t value) {
372 data_.resize(sizeof(value));
373 writeUint16(value, &data_[0], data_.size());
374}
375
376void Option::setUint32(uint32_t value) {
377 data_.resize(sizeof(value));
378 writeUint32(value, &data_[0], data_.size());
379}
380
381bool Option::equals(const OptionPtr& other) const {
382 return (equals(*other));
383}
384
385bool Option::equals(const Option& other) const {
386 return ((getType() == other.getType()) &&
387 (getData() == other.getData()));
388}
389
392
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 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:480
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:293
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, size_t rec_level=0)
Parses provided buffer as DHCPv6 options and creates Option objects.
Definition libdhcp++.cc:320
static void packOptions4(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options, bool top=false, bool check=true)
Stores DHCPv4 options in a buffer.
static void packOptions6(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options)
Stores DHCPv6 options in a buffer.
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:598
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition option.cc:295
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition option.cc:314
static bool lenient_parsing_
Governs whether options should be parsed less strictly.
Definition option.h:490
std::string encapsulated_space_
Name of the option space being encapsulated by this option.
Definition option.h:607
std::string getEncapsulatedSpace() const
Returns the name of the option space encapsulated by this option.
Definition option.h:449
bool equals(const OptionPtr &other) const
Checks if options are equal.
Definition option.cc:381
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition option.h:324
virtual ~Option()
just to force that every option has virtual dtor
Definition option.cc:390
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:220
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:172
Universe
defines option universe DHCPv4 or DHCPv6
Definition option.h:90
Universe universe_
option universe (V4 or V6)
Definition option.h:595
OptionPtr getOption(uint16_t type) const
Returns shared_ptr to suboption of specific type.
Definition option.cc:200
void addOption(OptionPtr opt)
Adds a sub-option.
Definition option.cc:338
void setUint32(uint32_t value)
Sets content of this option to a single uint32 value.
Definition option.cc:376
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6).
Definition option.h:300
OptionBuffer data_
contains content of this data
Definition option.h:601
virtual std::string toString() const
Returns string representation of the value.
Definition option.cc:254
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:87
void setUint8(uint8_t value)
Sets content of this option to a single uint8 value.
Definition option.cc:366
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:434
virtual bool valid() const
returns if option is valid (e.g.
Definition option.cc:191
OptionCollection options_
collection for storing suboptions
Definition option.h:604
void unpackOptions(const OptionBuffer &buf, size_t rec_level=0)
Builds a collection of sub options from the buffer.
Definition option.cc:155
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition option.h:504
Universe getUniverse() const
returns option universe (V4 or V6)
Definition option.h:240
uint8_t getUint8() const
Returns content of first byte.
Definition option.cc:348
virtual std::vector< uint8_t > toBinary(const bool include_header=false) const
Returns binary representation of the option.
Definition option.cc:260
void setUint16(uint16_t value)
Sets content of this option to a single uint16 value.
Definition option.cc:371
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:356
virtual std::string toText(int indent=0) const
Returns string representation of the option.
Definition option.cc:229
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:281
uint32_t getUint32() const
Returns content of first double word.
Definition option.cc:361
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:209
static const size_t OPTION4_HDR_LEN
length of the usual DHCPv4 option header (there are exceptions)
Definition option.h:84
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:346
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition buffer.h:476
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:501
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition buffer.h:559
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, unsigned level)
Copy the data up to a nesting level.
Definition data.cc:1522
@ 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
bool isPrintable(const string &content)
Check if a string is printable.
Definition str.cc:310
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.