Kea 2.5.8
option_vendor_class.cc
Go to the documentation of this file.
1// Copyright (C) 2014-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
12#include <util/io.h>
13
14#include <sstream>
15
16namespace isc {
17namespace dhcp {
18
20 const uint32_t vendor_id)
21 : Option(u, getOptionCode(u)), vendor_id_(vendor_id) {
22 if (u == Option::V4) {
24 }
25}
26
30 : Option(u, getOptionCode(u)) {
31 unpack(begin, end);
32}
33
36 return (cloneInternal<OptionVendorClass>());
37}
38
39void
41 packHeader(buf, check);
42
44
45 bool first = true;
46 for (auto const& it : tuples_) {
47 // For DHCPv4 V-I Vendor Class option, there is enterprise id before
48 // every tuple.
49 if ((getUniverse() == V4) && (!first)) {
51 }
52 first = false;
53 it.pack(buf);
54
55 }
56 // That's it. We don't pack any sub-options here, because this option
57 // must not contain sub-options.
58}
59
60void
63 if (std::distance(begin, end) < getMinimalLength() - getHeaderLen()) {
64 isc_throw(OutOfRange, "parsed Vendor Class option data truncated to"
65 " size " << std::distance(begin, end));
66 }
67 // Option must contain at least one enterprise id. It is ok to read 4-byte
68 // value here because we have checked that the buffer he minimal length.
69 vendor_id_ = isc::util::readUint32(&(*begin), distance(begin, end));
70 begin += sizeof(vendor_id_);
71
72 // Start reading opaque data.
73 size_t offset = 0;
74 while (offset < std::distance(begin, end)) {
75 // Parse a tuple.
77 begin + offset, end);
78 addTuple(tuple);
79 // The tuple has been parsed correctly which implies that it is safe to
80 // advance the offset by its total length.
81 offset += tuple.getTotalLength();
82 // For DHCPv4 option, there is enterprise id before every opaque data
83 // tuple. Let's read it, unless we have already reached the end of
84 // buffer.
85 if ((getUniverse() == V4) && (begin + offset != end)) {
86 // Get the other enterprise id.
87 uint32_t other_id = isc::util::readUint32(&(*(begin + offset)),
88 distance(begin + offset,
89 end));
90 // Throw if there are two different enterprise ids.
91 if (other_id != vendor_id_) {
92 isc_throw(isc::BadValue, "V-I Vendor Class option with two "
93 "different enterprise ids: " << vendor_id_
94 << " and " << other_id);
95 }
96 // Advance the offset by the size of enterprise id.
97 offset += sizeof(vendor_id_);
98 // If the offset already ran over the buffer length or there is
99 // no space left for the empty tuple (thus we add 1), we have
100 // to signal the option truncation.
101 if (offset + 1 >= std::distance(begin, end)) {
102 isc_throw(isc::OutOfRange, "truncated DHCPv4 V-I Vendor Class"
103 " option - it should contain enterprise id followed"
104 " by opaque data field tuple");
105 }
106 }
107 }
108}
109
110void
113 isc_throw(isc::BadValue, "attempted to add opaque data tuple having"
114 " invalid size of the length field "
115 << tuple.getDataFieldSize() << " to Vendor Class option");
116 }
117
118 tuples_.push_back(tuple);
119}
120
121
122void
123OptionVendorClass::setTuple(const size_t at, const OpaqueDataTuple& tuple) {
124 if (at >= getTuplesNum()) {
125 isc_throw(isc::OutOfRange, "attempted to set an opaque data for the"
126 " vendor option at position " << at << " which is out of"
127 " range");
128
130 isc_throw(isc::BadValue, "attempted to set opaque data tuple having"
131 " invalid size of the length field "
132 << tuple.getDataFieldSize() << " to Vendor Class option");
133 }
134
135 tuples_[at] = tuple;
136}
137
139OptionVendorClass::getTuple(const size_t at) const {
140 if (at >= getTuplesNum()) {
141 isc_throw(isc::OutOfRange, "attempted to get an opaque data for the"
142 " vendor option at position " << at << " which is out of"
143 " range. There are only " << getTuplesNum() << " tuples");
144 }
145 return (tuples_[at]);
146}
147
148bool
149OptionVendorClass::hasTuple(const std::string& tuple_str) const {
150 // Iterate over existing tuples (there shouldn't be many of them),
151 // and try to match the searched one.
152 for (auto const& it : tuples_) {
153 if (it == tuple_str) {
154 return (true);
155 }
156 }
157 return (false);
158}
159
160
161uint16_t
163 // The option starts with the header and enterprise id.
164 uint16_t length = getHeaderLen() + sizeof(uint32_t);
165 // Now iterate over existing tuples and add their size.
166 bool first = true;
167 for (auto const& it : tuples_) {
168 // For DHCPv4 V-I Vendor Class option, there is enterprise id before
169 // every tuple.
170 if ((getUniverse() == V4) && (!first)) {
171 length += sizeof(uint32_t);
172 }
173 first = false;
174 length += it.getTotalLength();
175
176 }
177
178 return (length);
179}
180
181std::string
182OptionVendorClass::toText(int indent) const {
183 std::ostringstream s;
184
185 // Apply indentation
186 s << std::string(indent, ' ');
187 // Print type, length and first occurrence of enterprise id.
188 s << "type=" << getType() << ", len=" << len() - getHeaderLen() << ", "
189 " enterprise id=0x" << std::hex << getVendorId() << std::dec;
190 // Iterate over all tuples and print their size and contents.
191 for (unsigned i = 0; i < getTuplesNum(); ++i) {
192 // The DHCPv4 V-I Vendor Class has enterprise id before every tuple.
193 if ((getUniverse() == V4) && (i > 0)) {
194 s << ", enterprise id=0x" << std::hex << getVendorId() << std::dec;
195 }
196 // Print the tuple.
197 s << ", data-len" << i << "=" << getTuple(i).getLength();
198 s << ", vendor-class-data" << i << "='" << getTuple(i) << "'";
199 }
200
201 return (s.str());
202}
203
204} // namespace isc::dhcp
205} // namespace isc
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 parameter given to a method would refer to or modify out-of-r...
Represents a single instance of the opaque data preceded by length.
int getDataFieldSize() const
Returns the size of the tuple length field.
LengthFieldType getLengthFieldType() const
Returns tuple length data field type.
size_t getTotalLength() const
Returns a total size of the tuple, including length field.
size_t getLength() const
Returns the length of the data in the tuple.
static OpaqueDataTuple::LengthFieldType getTupleLenFieldType(Option::Universe u)
Returns Length Field Type for a tuple.
virtual std::string toText(int indent=0) const
Returns text representation of the option.
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses buffer holding an option.
OptionVendorClass(Option::Universe u, const uint32_t vendor_id)
Constructor.
OptionPtr clone() const
Copies this option and returns a pointer to the copy.
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Renders option into the buffer in the wire format.
virtual uint16_t len() const
Returns the full length of the option, including option header.
uint32_t getVendorId() const
Returns enterprise id.
void setTuple(const size_t at, const OpaqueDataTuple &tuple)
Replaces tuple at the specified index with a new tuple.
OpaqueDataTuple getTuple(const size_t at) const
Returns opaque data tuple at the specified position.
void addTuple(const OpaqueDataTuple &tuple)
Adds a new opaque data tuple to the option.
size_t getTuplesNum() const
Returns the number of opaque data tuples added to the option.
bool hasTuple(const std::string &tuple_str) const
Checks if the Vendor Class holds the opaque data tuple with the specified string.
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:321
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
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
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:343
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.
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
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.