Kea 3.3.4
option_vendor_class.cc
Go to the documentation of this file.
1// Copyright (C) 2014-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#include <config.h>
8
12#include <util/io.h>
13#include <util/str.h>
14
15#include <sstream>
16
17using namespace isc::util;
18
19namespace isc {
20namespace dhcp {
21
23 const uint32_t vendor_id)
24 : Option(u, getOptionCode(u)), vendor_id_(vendor_id) {
25 if (u == Option::V4) {
27 }
28}
29
36
41
42void
44 packHeader(buf, check);
45
47
48 bool first = true;
49 for (auto const& it : tuples_) {
50 // For DHCPv4 V-I Vendor Class option, there is enterprise id before
51 // every tuple.
52 if ((getUniverse() == V4) && (!first)) {
54 }
55 first = false;
56 it.pack(buf);
57
58 }
59 // That's it. We don't pack any sub-options here, because this option
60 // must not contain sub-options.
61}
62
63void
66 if (std::distance(begin, end) < getMinimalLength() - getHeaderLen()) {
67 isc_throw(OutOfRange, "parsed Vendor Class option data truncated to"
68 " size " << std::distance(begin, end));
69 }
70 // Option must contain at least one enterprise id. It is ok to read 4-byte
71 // value here because we have checked that the buffer he minimal length.
72 vendor_id_ = isc::util::readUint32(&(*begin), distance(begin, end));
73 begin += sizeof(vendor_id_);
74
75 // Start reading opaque data.
76 size_t offset = 0;
77 while (offset < static_cast<size_t>(std::distance(begin, end))) {
78 // Parse a tuple.
80 begin + offset, end);
81 addTuple(tuple);
82 // The tuple has been parsed correctly which implies that it is safe to
83 // advance the offset by its total length.
84 offset += tuple.getTotalLength();
85 // For DHCPv4 option, there is enterprise id before every opaque data
86 // tuple. Let's read it, unless we have already reached the end of
87 // buffer.
88 if ((getUniverse() == V4) && (begin + offset != end)) {
89 // Get the other enterprise id.
90 uint32_t other_id = isc::util::readUint32(&(*(begin + offset)),
91 distance(begin + offset,
92 end));
93 // Throw if there are two different enterprise ids.
94 if (other_id != vendor_id_) {
95 isc_throw(isc::BadValue, "V-I Vendor Class option with two "
96 "different enterprise ids: " << vendor_id_
97 << " and " << other_id);
98 }
99 // Advance the offset by the size of enterprise id.
100 offset += sizeof(vendor_id_);
101 // If the offset already ran over the buffer length or there is
102 // no space left for the empty tuple (thus we add 1), we have
103 // to signal the option truncation.
104 if (offset + 1 >= static_cast<size_t>(std::distance(begin, end))) {
105 isc_throw(isc::OutOfRange, "truncated DHCPv4 V-I Vendor Class"
106 " option - it should contain enterprise id followed"
107 " by opaque data field tuple");
108 }
109 }
110 }
111}
112
113void
116 isc_throw(isc::BadValue, "attempted to add opaque data tuple having"
117 " invalid size of the length field "
118 << tuple.getDataFieldSize() << " to Vendor Class option");
119 }
120
121 tuples_.push_back(tuple);
122}
123
124
125void
126OptionVendorClass::setTuple(const size_t at, const OpaqueDataTuple& tuple) {
127 if (at >= getTuplesNum()) {
128 isc_throw(isc::OutOfRange, "attempted to set an opaque data for the"
129 " vendor option at position " << at << " which is out of"
130 " range");
131
133 isc_throw(isc::BadValue, "attempted to set opaque data tuple having"
134 " invalid size of the length field "
135 << tuple.getDataFieldSize() << " to Vendor Class option");
136 }
137
138 tuples_[at] = tuple;
139}
140
142OptionVendorClass::getTuple(const size_t at) const {
143 if (at >= getTuplesNum()) {
144 isc_throw(isc::OutOfRange, "attempted to get an opaque data for the"
145 " vendor option at position " << at << " which is out of"
146 " range. There are only " << getTuplesNum() << " tuples");
147 }
148 return (tuples_[at]);
149}
150
151bool
152OptionVendorClass::hasTuple(const std::string& tuple_str) const {
153 // Iterate over existing tuples (there shouldn't be many of them),
154 // and try to match the searched one.
155 for (auto const& it : tuples_) {
156 if (it == tuple_str) {
157 return (true);
158 }
159 }
160 return (false);
161}
162
163
164uint16_t
166 // The option starts with the header and enterprise id.
167 uint16_t length = getHeaderLen() + sizeof(uint32_t);
168 // Now iterate over existing tuples and add their size.
169 bool first = true;
170 for (auto const& it : tuples_) {
171 // For DHCPv4 V-I Vendor Class option, there is enterprise id before
172 // every tuple.
173 if ((getUniverse() == V4) && (!first)) {
174 length += sizeof(uint32_t);
175 }
176 first = false;
177 length += it.getTotalLength();
178
179 }
180
181 return (length);
182}
183
184std::string
185OptionVendorClass::toText(int indent) const {
186 std::ostringstream s;
187
188 // Apply indentation
189 s << std::string(indent, ' ');
190 // Print type, length and first occurrence of enterprise id.
191 s << "type=" << getType() << ", len=" << len() - getHeaderLen() << ", "
192 " enterprise id=0x" << std::hex << getVendorId() << std::dec;
193 // Iterate over all tuples and print their size and contents.
194 for (unsigned i = 0; i < getTuplesNum(); ++i) {
195 auto t = getTuple(i);
196 // The DHCPv4 V-I Vendor Class has enterprise id before every tuple.
197 if ((getUniverse() == V4) && (i > 0)) {
198 s << ", enterprise id=0x" << std::hex << getVendorId() << std::dec;
199 }
200 // Print the tuple.
201 s << ", data-len" << i << "=" << t.getLength();
202 s << ", vendor-class-data" << i << "=" << str::dumpAsHex(t.getData());
203 auto txt = t.getText();
204 if (str::isPrintable(txt)) {
205 s << " '" << txt << "'";
206 }
207 }
208
209 return (s.str());
210}
211
212} // namespace isc::dhcp
213} // 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.
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.
Universe
defines option universe DHCPv4 or DHCPv6
Definition option.h:91
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6).
Definition option.cc:327
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6).
Definition option.h:301
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition option.h:505
Universe getUniverse() const
returns option universe (V4 or V6)
Definition option.h:241
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
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:355
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:540
#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:31
boost::shared_ptr< Option > OptionPtr
Definition option.h:38
string dumpAsHex(const uint8_t *data, size_t length)
Dumps a buffer of bytes as a string of hexadecimal digits.
Definition str.cc:330
bool isPrintable(const string &content)
Check if a string is printable.
Definition str.cc:310
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.