Kea 2.7.6
option.h
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#ifndef OPTION_H
8#define OPTION_H
9
10#include <util/buffer.h>
11
12#include <boost/shared_ptr.hpp>
13
14#include <map>
15#include <string>
16#include <vector>
17
18namespace isc {
19namespace dhcp {
20
24typedef std::vector<uint8_t> OptionBuffer;
25
27typedef OptionBuffer::iterator OptionBufferIter;
28
30typedef OptionBuffer::const_iterator OptionBufferConstIter;
31
33typedef boost::shared_ptr<OptionBuffer> OptionBufferPtr;
34
36class Option;
37typedef boost::shared_ptr<Option> OptionPtr;
38
40typedef std::multimap<unsigned int, OptionPtr> OptionCollection;
41
43typedef boost::shared_ptr<OptionCollection> OptionCollectionPtr;
44
53public:
54 SkipRemainingOptionsError (const char* file, size_t line, const char* what) :
55 isc::Exception(file, line, what) { }
56};
57
68public:
69 SkipThisOptionError (const char* file, size_t line, const char* what) :
70 isc::Exception(file, line, what) { }
71};
72
76public:
77 OptionParseError (const char* file, size_t line, const char* what) :
78 isc::Exception(file, line, what) { }
79};
80
81class Option {
82public:
84 const static size_t OPTION4_HDR_LEN = 2;
85
87 const static size_t OPTION6_HDR_LEN = 4;
88
90 enum Universe { V4, V6 };
91
92
103 typedef OptionPtr Factory(Option::Universe u, uint16_t type, const OptionBuffer& buf);
104
120 uint16_t type,
121 const OptionBuffer& buf);
122
138 static OptionPtr factory(Option::Universe u, uint16_t type) {
139 return factory(u, type, OptionBuffer());
140 }
141
146 Option(Universe u, uint16_t type);
147
158 Option(Universe u, uint16_t type, const OptionBuffer& data);
159
180 Option(Universe u, uint16_t type, OptionBufferConstIter first,
182
189 Option(const Option& source);
190
202 static OptionPtr create(Universe u, uint16_t type);
203
216 static OptionPtr create(Universe u, uint16_t type, const OptionBuffer& data);
217
225 Option& operator=(const Option& rhs);
226
235 virtual OptionPtr clone() const;
236
241 return (universe_);
242 }
243
255 virtual void pack(isc::util::OutputBuffer& buf, bool check = true) const;
256
261 virtual void unpack(OptionBufferConstIter begin,
263
269 virtual std::string toText(int indent = 0) const;
270
277 virtual std::string toString() const;
278
286 virtual std::vector<uint8_t> toBinary(const bool include_header = false) const;
287
295 virtual std::string toHexString(const bool include_header = false) const;
296
300 uint16_t getType() const {
301 return (type_);
302 }
303
308 virtual uint16_t len() const;
309
313 virtual uint16_t getHeaderLen() const;
314
318 virtual bool valid() const;
319
324 virtual const OptionBuffer& getData() const {
325 return (data_);
326 }
327
340 void addOption(OptionPtr opt);
341
347 OptionPtr getOption(uint16_t type) const;
348
355 return (options_);
356 }
357
367
373 void getOptionsCopy(OptionCollection& options_copy) const;
374
380 bool delOption(uint16_t type);
381
387 uint8_t getUint8() const;
388
394 uint16_t getUint16() const;
395
401 uint32_t getUint32() const;
402
408 void setUint8(uint8_t value);
409
415 void setUint16(uint16_t value);
416
422 void setUint32(uint32_t value);
423
433 template<typename InputIterator>
434 void setData(InputIterator first, InputIterator last) {
435 data_.assign(first, last);
436 }
437
442 void setEncapsulatedSpace(const std::string& encapsulated_space) {
443 encapsulated_space_ = encapsulated_space;
444 }
445
449 std::string getEncapsulatedSpace() const {
450 return (encapsulated_space_);
451 }
452
454 virtual ~Option();
455
464 bool equals(const OptionPtr& other) const;
465
478 virtual bool equals(const Option& other) const;
479
490 static bool lenient_parsing_;
491
492protected:
493
503 template<typename OptionType>
505 const OptionType* cast_this = dynamic_cast<const OptionType*>(this);
506 if (cast_this) {
507 return (boost::shared_ptr<OptionType>(new OptionType(*cast_this)));
508 }
509 return (OptionPtr());
510 }
511
527 void packHeader(isc::util::OutputBuffer& buf, bool check = true) const;
528
543 void packOptions(isc::util::OutputBuffer& buf, bool check = true) const;
544
557 void unpackOptions(const OptionBuffer& buf);
558
569 std::string headerToText(const int indent = 0,
570 const std::string& type_name = "") const;
571
584 std::string suboptionsToText(const int indent = 0) const;
585
591 void check() const;
592
595
597 uint16_t type_;
598
601
604
607
610};
611
612} // namespace isc::dhcp
613} // namespace isc
614
615#endif // OPTION_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Wrapper exception thrown by unpackOptionsX functions to add option type and len to the underlying err...
Definition option.h:75
OptionParseError(const char *file, size_t line, const char *what)
Definition option.h:77
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
const OptionCollection & getOptions() const
Returns all encapsulated options.
Definition option.h:354
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
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition option.h:442
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
OptionPtr Factory(Option::Universe u, uint16_t type, const OptionBuffer &buf)
a factory function prototype
Definition option.h:103
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
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: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
static OptionPtr factory(Option::Universe u, uint16_t type)
Factory function to create instance of option.
Definition option.h:138
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
OptionCollection & getMutableOptions()
Returns all encapsulated options.
Definition option.h:364
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
Exception thrown during option unpacking This exception is thrown when an error has occurred,...
Definition option.h:52
SkipRemainingOptionsError(const char *file, size_t line, const char *what)
Definition option.h:54
Exception thrown during option unpacking This exception is thrown when an error has occurred unpackin...
Definition option.h:67
SkipThisOptionError(const char *file, size_t line, const char *what)
Definition option.h:69
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:343
boost::shared_ptr< OptionCollection > OptionCollectionPtr
A pointer to an OptionCollection.
Definition option.h:43
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
boost::shared_ptr< OptionBuffer > OptionBufferPtr
pointer to a DHCP buffer
Definition option.h:33
OptionBuffer::iterator OptionBufferIter
iterator for walking over OptionBuffer
Definition option.h:27
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition option.h:24
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
Defines the logger used by the top-level component of kea-lfc.