Kea 2.7.5
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
32OptionPtr
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_) {
59 option.getOptionsCopy(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_;
78 rhs.getOptionsCopy(options_);
79 encapsulated_space_ = rhs.encapsulated_space_;
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 if (data_.empty()) {
240 output << "''";
241 } else if (str::isPrintable(data_)) {
242 std::string printable(data_.cbegin(), data_.cend());
243 output << " '" << printable << "'";
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 // The RFC3396 adds support for long options split over multiple options
263 // using the same code.
264 pack(buf, false);
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 (auto const& opt : options_) {
319 output << std::endl << opt.second->toText(indent);
320 }
321 }
322
323 return (output.str());
324}
325
326uint16_t
328 switch (universe_) {
329 case V4:
330 return OPTION4_HDR_LEN; // header length for v4
331 case V6:
332 return OPTION6_HDR_LEN; // header length for v6
333 }
334 return 0; // should not happen
335}
336
338 if (this == opt.get()) {
339 // Do not allow options to be added to themselves as this
340 // can lead to infinite recursion.
341 isc_throw(InvalidOperation, "option cannot be added to itself: " << toText());
342 }
343
344 options_.insert(make_pair(opt->getType(), opt));
345}
346
347uint8_t Option::getUint8() const {
348 if (data_.size() < sizeof(uint8_t) ) {
349 isc_throw(OutOfRange, "Attempt to read uint8 from option " << type_
350 << " that has size " << data_.size());
351 }
352 return (data_[0]);
353}
354
355uint16_t Option::getUint16() const {
356 // readUint16() checks and throws OutOfRange if data_ is too small.
357 return (readUint16(&data_[0], data_.size()));
358}
359
360uint32_t Option::getUint32() const {
361 // readUint32() checks and throws OutOfRange if data_ is too small.
362 return (readUint32(&data_[0], data_.size()));
363}
364
365void Option::setUint8(uint8_t value) {
366 data_.resize(sizeof(value));
367 data_[0] = value;
368}
369
370void Option::setUint16(uint16_t value) {
371 data_.resize(sizeof(value));
372 writeUint16(value, &data_[0], data_.size());
373}
374
375void Option::setUint32(uint32_t value) {
376 data_.resize(sizeof(value));
377 writeUint32(value, &data_[0], data_.size());
378}
379
380bool Option::equals(const OptionPtr& other) const {
381 return (equals(*other));
382}
383
384bool Option::equals(const Option& other) const {
385 return ((getType() == other.getType()) &&
386 (getData() == other.getData()));
387}
388
391
393
394} // end of isc::dhcp namespace
395} // 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:471
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 void packOptions4(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options, bool top=false, bool check=true)
Stores DHCPv4 options in a buffer.
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:317
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:597
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
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:606
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:380
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:389
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:327
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:90
Universe universe_
option universe (V4 or V6)
Definition option.h:594
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:337
void setUint32(uint32_t value)
Sets content of this option to a single uint32 value.
Definition option.cc:375
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:600
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: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:87
void setUint8(uint8_t value)
Sets content of this option to a single uint8 value.
Definition option.cc:365
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:190
OptionCollection options_
collection for storing suboptions
Definition option.h:603
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:347
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 a single uint16 value.
Definition option.cc:370
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:355
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:280
uint32_t getUint32() const
Returns content of first double word.
Definition option.cc:360
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:84
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:343
#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
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.