16 #include <boost/make_shared.hpp>
22 #include <arpa/inet.h>
36 return(LibDHCP::optionFactory(u, type, buf));
41 :universe_(u), type_(type) {
46 :universe_(u), type_(type), data_(data) {
52 :universe_(u), type_(type), data_(first, last) {
57 : universe_(option.universe_), type_(option.type_),
58 data_(option.data_), options_(),
59 encapsulated_space_(option.encapsulated_space_) {
65 return (boost::make_shared<Option>(u, type));
70 return (boost::make_shared<Option>(u, type, data));
87 return (cloneInternal<Option>());
94 <<
"Only V4 and V6 are allowed.");
101 <<
"For DHCPv4 allowed type range is 0..255");
102 }
else if (
data_.size() > 255) {
118 if (!
data_.empty()) {
130 <<
"At most 255 bytes are supported.");
166 list<uint16_t> deferred;
189 for (OptionCollection::const_iterator it =
options_.begin();
192 length += (*it).second->len();
198 return (static_cast<uint16_t>(length));
212 isc::dhcp::OptionCollection::const_iterator x =
223 for (OptionCollection::const_iterator it =
options_.begin();
226 local_options.insert(std::make_pair(it->second->getType(),
231 options_copy.swap(local_options);
235 isc::dhcp::OptionCollection::iterator x =
options_.find(opt_type);
245 std::stringstream output;
248 for (
unsigned int i = 0; i <
data_.size(); i++) {
252 output << setfill(
'0') << setw(2) << hex
253 <<
static_cast<unsigned short>(
data_[i]);
259 return (output.str());
276 }
catch (
const std::exception &ex) {
278 " of option " <<
getType() <<
": " << ex.what());
280 const uint8_t* option_data =
static_cast<const uint8_t*
>(buf.
getData());
284 std::vector<uint8_t> option_vec(option_data + (include_header ? 0 :
getHeaderLen()),
292 std::vector<uint8_t> option_vec =
toBinary(include_header);
296 std::ostringstream s;
297 if (!option_vec.empty()) {
305 std::stringstream output;
306 for (
int i = 0; i < indent; i++)
310 output <<
"type=" << std::setw(field_len) << std::setfill(
'0')
313 if (!type_name.empty()) {
314 output <<
"(" << type_name <<
")";
317 output <<
", len=" << std::setw(field_len) << std::setfill(
'0')
319 return (output.str());
324 std::stringstream output;
327 output <<
"," << std::endl <<
"options:";
328 for (OptionCollection::const_iterator opt =
options_.begin();
330 output << std::endl << (*opt).second->toText(indent);
334 return (output.str());
353 <<
" already present in this message.");
356 options_.insert(make_pair(opt->getType(), opt));
360 if (
data_.size() <
sizeof(uint8_t) ) {
362 <<
" that has size " <<
data_.size());
378 data_.resize(
sizeof(value));
383 data_.resize(
sizeof(value));
388 data_.resize(
sizeof(value));
Universe getUniverse() const
returns option universe (V4 or V6)
const void * getData() const
Return a pointer to the head of the data stored in the buffer.
void packHeader(isc::util::OutputBuffer &buf) const
Store option's header in a buffer.
void packOptions(isc::util::OutputBuffer &buf) const
Store sub options in a buffer.
virtual std::string toHexString(const bool include_header=false) const
Returns string containing hexadecimal representation of option.
uint8_t * writeUint32(uint32_t value, uint8_t *buffer, size_t length)
Write Unsigned 32-Bit Integer to Buffer.
static void packOptions6(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options)
Stores DHCPv6 options in a buffer.
virtual ~Option()
just to force that every option has virtual dtor
OptionPtr getOption(uint16_t type) const
Returns shared_ptr to suboption of specific type.
bool equals(const OptionPtr &other) const
Checks if options are equal.
virtual std::vector< uint8_t > toBinary(const bool include_header=false) const
Returns binary representation of the option.
virtual void pack(isc::util::OutputBuffer &buf) const
Writes option in wire-format to a buffer.
void setUint16(uint16_t value)
Sets content of this option to a single uint16 value.
boost::shared_ptr< Option > OptionPtr
Universe
defines option universe DHCPv4 or DHCPv6
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
virtual std::string toString() const
Returns string representation of the value.
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Universe universe_
option universe (V4 or V6)
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
static const size_t OPTION4_HDR_LEN
length of the usual DHCPv4 option header (there are exceptions)
void setUint32(uint32_t value)
Sets content of this option to a single uint32 value.
static void packOptions4(isc::util::OutputBuffer &buf, const isc::dhcp::OptionCollection &options, bool top=false)
Stores DHCPv4 options in a buffer.
uint8_t getUint8() const
Returns content of first byte.
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.
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
uint8_t * writeUint16(uint16_t value, void *buffer, size_t length)
Write Unsigned 16-Bit Integer to Buffer.
void check() const
A protected method used for option correctness.
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
OptionCollection options_
collection for storing suboptions
virtual std::string toText(int indent=0) const
Returns string representation of the option.
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
void addOption(OptionPtr opt)
Adds a sub-option.
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
uint32_t readUint32(const uint8_t *buffer, size_t length)
Read Unsigned 32-Bit Integer from Buffer.
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Defines the logger used by the top-level component of kea-dhcp-ddns.
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 ('hex') format.
Option & operator=(const Option &rhs)
Assignment operator.
void unpackOptions(const OptionBuffer &buf)
Builds a collection of sub options from the buffer.
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
static OptionPtr create(Universe u, uint16_t type)
Factory function creating an instance of the Option.
std::string getEncapsulatedSpace() const
Returns the name of the option space encapsulated by this option.
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
void writeUint16(uint16_t data)
Write an unsigned 16-bit integer in host byte order into the buffer in network byte order...
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
void setUint8(uint8_t value)
Sets content of this option to a single uint8 value.
uint32_t getUint32() const
Returns content of first double word.
OptionBuffer data_
contains content of this data
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
void getOptionsCopy(OptionCollection &options_copy) const
Performs deep copy of suboptions.
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header) ...
size_t getLength() const
Return the length of data written in the buffer.
uint16_t getUint16() const
Returns content of first word.
static bool lenient_parsing_
Governs whether options should be parsed less strictly.
std::string encapsulated_space_
Name of the option space being encapsulated by this option.
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.
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
virtual bool valid() const
returns if option is valid (e.g.