Kea 3.2.0-git
option_int.h
Go to the documentation of this file.
1// Copyright (C) 2012-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#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 OptionBufferConstIter end, size_t rec_level = 0)
91 : Option(u, type) {
93 isc_throw(dhcp::InvalidDataType, "non-integer type");
94 }
96 unpack(begin, end, rec_level);
97 }
98
100 virtual OptionPtr clone() const {
101 return (cloneInternal<OptionInt<T> >());
102 }
103
113 virtual void pack(isc::util::OutputBuffer& buf, bool check = true) const {
114 // Pack option header.
115 packHeader(buf, check);
116 // Depending on the data type length we use different utility functions
117 // writeUint16 or writeUint32 which write the data in the network byte
118 // order to the provided buffer. The same functions can be safely used
119 // for either unsigned or signed integers so there is not need to create
120 // special cases for intX_t types.
122 case 1:
123 buf.writeUint8(value_);
124 break;
125 case 2:
126 buf.writeUint16(value_);
127 break;
128 case 4:
129 buf.writeUint32(value_);
130 break;
131 default:
132 isc_throw(dhcp::InvalidDataType, "non-integer type");
133 }
134 packOptions(buf, check);
135 }
136
150 unpack(begin, end, 0);
151 }
152
167 size_t rec_level) {
168
169 if (static_cast<size_t>(distance(begin, end)) < sizeof(T)) {
170 isc_throw(OutOfRange, "OptionInt " << getType() << " truncated");
171 }
172 // @todo consider what to do if buffer is longer than data type.
173
174 // Depending on the data type length we use different utility functions
175 // readUint16 or readUint32 which read the data laid in the network byte
176 // order from the provided buffer. The same functions can be safely used
177 // for either unsigned or signed integers so there is not need to create
178 // special cases for intX_t types.
179 int data_size_len = OptionDataTypeTraits<T>::len;
180 switch (data_size_len) {
181 case 1:
182 value_ = *begin;
183 break;
184 case 2:
185 value_ = isc::util::readUint16(&(*begin),
186 std::distance(begin, end));
187 break;
188 case 4:
189 value_ = isc::util::readUint32(&(*begin),
190 std::distance(begin, end));
191 break;
192 default:
193 isc_throw(dhcp::InvalidDataType, "non-integer type");
194 }
195 // Use local variable to set a new value for this iterator.
196 // When using OptionDataTypeTraits<T>::len directly some versions
197 // of clang complain about unresolved reference to
198 // OptionDataTypeTraits structure during linking.
199 begin += data_size_len;
200 unpackOptions(OptionBuffer(begin, end), rec_level);
201 }
202
206 void setValue(T value) { value_ = value; }
207
211 T getValue() const { return value_; }
212
218 virtual uint16_t len() const {
219 // Calculate the length of the header.
220 uint16_t length = (getUniverse() == Option::V4) ? OPTION4_HDR_LEN : OPTION6_HDR_LEN;
221 // The data length is equal to size of T.
222 length += sizeof(T);;
223 // length of all suboptions
224 for (auto const& it : options_) {
225 length += it.second->len();
226 }
227 return (length);
228 }
229
236 virtual std::string toText(int indent = 0) const {
237 std::stringstream output;
238 output << headerToText(indent) << ": ";
239
240 // For 1 byte long data types we need to cast to the integer
241 // because they are usually implemented as "char" types, in
242 // which case the character rather than number would be printed.
244 output << static_cast<int>(getValue());
245 } else {
246 output << getValue();
247 }
248
249 // Append data type name.
250 output << " ("
252 << ")";
253
254 // Append suboptions.
255 output << suboptionsToText(indent + 2);
256
257 return (output.str());
258 }
259
260private:
261
262 T value_;
263};
264
265} // isc::dhcp namespace
266} // isc namespace
267
268#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:149
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition option_int.h:100
virtual std::string toText(int indent=0) const
Returns option carrying an integer value in the textual format.
Definition option_int.h:236
OptionInt(Option::Universe u, uint16_t type, OptionBufferConstIter begin, OptionBufferConstIter end, size_t rec_level=0)
Constructor.
Definition option_int.h:89
T getValue() const
Return option value.
Definition option_int.h:211
void setValue(T value)
Set option value.
Definition option_int.h:206
OptionInt(Option::Universe u, uint16_t type, T value)
Constructor.
Definition option_int.h:65
void unpack(OptionBufferConstIter begin, OptionBufferConstIter end, size_t rec_level)
Parses received buffer with limited recursion.
Definition option_int.h:166
virtual uint16_t len() const
returns complete length of option
Definition option_int.h:218
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:113
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
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition option.h:442
Universe
defines option universe DHCPv4 or DHCPv6
Definition option.h:90
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6).
Definition option.h:300
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:87
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
void packHeader(isc::util::OutputBuffer &buf, bool check=true) const
Store option's header in a buffer.
Definition option.cc:119
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
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 writeUint32(uint32_t data)
Write an unsigned 32-bit integer in host byte order into the buffer in network byte order.
Definition buffer.h:531
#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
static const OptionDataType type