Kea 2.5.8
option_int.h
Go to the documentation of this file.
1// Copyright (C) 2012-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_INT_H
8#define OPTION_INT_H
9
10#include <dhcp/libdhcp++.h>
11#include <dhcp/option.h>
13#include <dhcp/option_space.h>
14#include <util/io.h>
15
16#include <stdint.h>
17#include <sstream>
18
19namespace isc {
20namespace dhcp {
21
22template<typename T>
23class OptionInt;
24
31typedef boost::shared_ptr<OptionUint8> OptionUint8Ptr;
33typedef boost::shared_ptr<OptionUint16> OptionUint16Ptr;
35typedef boost::shared_ptr<OptionUint32> OptionUint32Ptr;
37
48template<typename T>
49class OptionInt: public Option {
50private:
51
53 typedef boost::shared_ptr<OptionInt<T> > OptionIntTypePtr;
54
55public:
65 OptionInt(Option::Universe u, uint16_t type, T value)
66 : Option(u, type), value_(value) {
68 isc_throw(dhcp::InvalidDataType, "non-integer type");
69 }
71 }
72
90 : Option(u, type) {
92 isc_throw(dhcp::InvalidDataType, "non-integer type");
93 }
95 unpack(begin, end);
96 }
97
99 virtual OptionPtr clone() const {
100 return (cloneInternal<OptionInt<T> >());
101 }
102
112 virtual void pack(isc::util::OutputBuffer& buf, bool check = true) const {
113 // Pack option header.
114 packHeader(buf, check);
115 // Depending on the data type length we use different utility functions
116 // writeUint16 or writeUint32 which write the data in the network byte
117 // order to the provided buffer. The same functions can be safely used
118 // for either unsigned or signed integers so there is not need to create
119 // special cases for intX_t types.
121 case 1:
122 buf.writeUint8(value_);
123 break;
124 case 2:
125 buf.writeUint16(value_);
126 break;
127 case 4:
128 buf.writeUint32(value_);
129 break;
130 default:
131 isc_throw(dhcp::InvalidDataType, "non-integer type");
132 }
133 packOptions(buf, check);
134 }
135
149 if (distance(begin, end) < sizeof(T)) {
150 isc_throw(OutOfRange, "OptionInt " << getType() << " truncated");
151 }
152 // @todo consider what to do if buffer is longer than data type.
153
154 // Depending on the data type length we use different utility functions
155 // readUint16 or readUint32 which read the data laid in the network byte
156 // order from the provided buffer. The same functions can be safely used
157 // for either unsigned or signed integers so there is not need to create
158 // special cases for intX_t types.
159 int data_size_len = OptionDataTypeTraits<T>::len;
160 switch (data_size_len) {
161 case 1:
162 value_ = *begin;
163 break;
164 case 2:
165 value_ = isc::util::readUint16(&(*begin),
166 std::distance(begin, end));
167 break;
168 case 4:
169 value_ = isc::util::readUint32(&(*begin),
170 std::distance(begin, end));
171 break;
172 default:
173 isc_throw(dhcp::InvalidDataType, "non-integer type");
174 }
175 // Use local variable to set a new value for this iterator.
176 // When using OptionDataTypeTraits<T>::len directly some versions
177 // of clang complain about unresolved reference to
178 // OptionDataTypeTraits structure during linking.
179 begin += data_size_len;
180 unpackOptions(OptionBuffer(begin, end));
181 }
182
186 void setValue(T value) { value_ = value; }
187
191 T getValue() const { return value_; }
192
198 virtual uint16_t len() const {
199 // Calculate the length of the header.
200 uint16_t length = (getUniverse() == Option::V4) ? OPTION4_HDR_LEN : OPTION6_HDR_LEN;
201 // The data length is equal to size of T.
202 length += sizeof(T);;
203 // length of all suboptions
204 for (auto const& it : options_) {
205 length += it.second->len();
206 }
207 return (length);
208 }
209
216 virtual std::string toText(int indent = 0) const {
217 std::stringstream output;
218 output << headerToText(indent) << ": ";
219
220 // For 1 byte long data types we need to cast to the integer
221 // because they are usually implemented as "char" types, in
222 // which case the character rather than number would be printed.
224 output << static_cast<int>(getValue());
225 } else {
226 output << getValue();
227 }
228
229 // Append data type name.
230 output << " ("
232 << ")";
233
234 // Append suboptions.
235 output << suboptionsToText(indent + 2);
236
237 return (output.str());
238 }
239
240private:
241
242 T value_;
243};
244
245} // isc::dhcp namespace
246} // isc namespace
247
248#endif // OPTION_INT_H
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Exception to be thrown when invalid type specified as template parameter.
static const std::string & getDataTypeName(const OptionDataType data_type)
Return option data type name from the data type enumerator.
Forward declaration to OptionInt.
Definition: option_int.h:49
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition: option_int.h:148
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option_int.h:99
virtual std::string toText(int indent=0) const
Returns option carrying an integer value in the textual format.
Definition: option_int.h:216
T getValue() const
Return option value.
Definition: option_int.h:191
void setValue(T value)
Set option value.
Definition: option_int.h:186
OptionInt(Option::Universe u, uint16_t type, T value)
Constructor.
Definition: option_int.h:65
virtual uint16_t len() const
returns complete length of option
Definition: option_int.h:198
OptionInt(Option::Universe u, uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end)
Constructor.
Definition: option_int.h:88
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in wire-format to buf, returns pointer to first unused byte after stored option.
Definition: option_int.h:112
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
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition: option.h:435
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:83
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:293
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 const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:80
OptionCollection options_
collection for storing suboptions
Definition: option.h:596
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition: option.h:497
Universe getUniverse() const
returns option universe (V4 or V6)
Definition: option.h:233
void packHeader(isc::util::OutputBuffer &buf, bool check=true) const
Store option's header in a buffer.
Definition: option.cc:119
void check() const
A protected method used for option correctness.
Definition: option.cc:90
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 writeUint32(uint32_t data)
Write an unsigned 32-bit integer in host byte order into the buffer in network byte order.
Definition: buffer.h:528
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
OptionInt< uint8_t > OptionUint8
Definition: option_int.h:30
OptionInt< uint32_t > OptionUint32
Definition: option_int.h:34
OptionInt< uint16_t > OptionUint16
Definition: option_int.h:32
boost::shared_ptr< OptionUint8 > OptionUint8Ptr
Definition: option_int.h:31
boost::shared_ptr< OptionUint32 > OptionUint32Ptr
Definition: option_int.h:35
boost::shared_ptr< OptionUint16 > OptionUint16Ptr
Definition: option_int.h:33
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
uint16_t readUint16(void const *const buffer, size_t const length)
uint16_t wrapper over readUint.
Definition: io.h:76
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.
#define DHCP4_OPTION_SPACE
global std option spaces
#define DHCP6_OPTION_SPACE
Trait class for data types supported in DHCP option definitions.