Kea 2.5.5
option_vendor_class.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2023 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 <sstream>
13
14namespace isc {
15namespace dhcp {
16
18 const uint32_t vendor_id)
19 : Option(u, getOptionCode(u)), vendor_id_(vendor_id) {
20 if (u == Option::V4) {
22 }
23}
24
28 : Option(u, getOptionCode(u)) {
29 unpack(begin, end);
30}
31
34 return (cloneInternal<OptionVendorClass>());
35}
36
37void
39 packHeader(buf, check);
40
42
43 for (TuplesCollection::const_iterator it = tuples_.begin();
44 it != tuples_.end(); ++it) {
45 // For DHCPv4 V-I Vendor Class option, there is enterprise id before
46 // every tuple.
47 if ((getUniverse() == V4) && (it != tuples_.begin())) {
49 }
50 it->pack(buf);
51
52 }
53 // That's it. We don't pack any sub-options here, because this option
54 // must not contain sub-options.
55}
56
57void
60 if (std::distance(begin, end) < getMinimalLength() - getHeaderLen()) {
61 isc_throw(OutOfRange, "parsed Vendor Class option data truncated to"
62 " size " << std::distance(begin, end));
63 }
64 // Option must contain at least one enterprise id. It is ok to read 4-byte
65 // value here because we have checked that the buffer he minimal length.
66 vendor_id_ = isc::util::readUint32(&(*begin), distance(begin, end));
67 begin += sizeof(vendor_id_);
68
69 // Start reading opaque data.
70 size_t offset = 0;
71 while (offset < std::distance(begin, end)) {
72 // Parse a tuple.
74 begin + offset, end);
75 addTuple(tuple);
76 // The tuple has been parsed correctly which implies that it is safe to
77 // advance the offset by its total length.
78 offset += tuple.getTotalLength();
79 // For DHCPv4 option, there is enterprise id before every opaque data
80 // tuple. Let's read it, unless we have already reached the end of
81 // buffer.
82 if ((getUniverse() == V4) && (begin + offset != end)) {
83 // Get the other enterprise id.
84 uint32_t other_id = isc::util::readUint32(&(*(begin + offset)),
85 distance(begin + offset,
86 end));
87 // Throw if there are two different enterprise ids.
88 if (other_id != vendor_id_) {
89 isc_throw(isc::BadValue, "V-I Vendor Class option with two "
90 "different enterprise ids: " << vendor_id_
91 << " and " << other_id);
92 }
93 // Advance the offset by the size of enterprise id.
94 offset += sizeof(vendor_id_);
95 // If the offset already ran over the buffer length or there is
96 // no space left for the empty tuple (thus we add 1), we have
97 // to signal the option truncation.
98 if (offset + 1 >= std::distance(begin, end)) {
99 isc_throw(isc::OutOfRange, "truncated DHCPv4 V-I Vendor Class"
100 " option - it should contain enterprise id followed"
101 " by opaque data field tuple");
102 }
103 }
104 }
105}
106
107void
110 isc_throw(isc::BadValue, "attempted to add opaque data tuple having"
111 " invalid size of the length field "
112 << tuple.getDataFieldSize() << " to Vendor Class option");
113 }
114
115 tuples_.push_back(tuple);
116}
117
118
119void
120OptionVendorClass::setTuple(const size_t at, const OpaqueDataTuple& tuple) {
121 if (at >= getTuplesNum()) {
122 isc_throw(isc::OutOfRange, "attempted to set an opaque data for the"
123 " vendor option at position " << at << " which is out of"
124 " range");
125
127 isc_throw(isc::BadValue, "attempted to set opaque data tuple having"
128 " invalid size of the length field "
129 << tuple.getDataFieldSize() << " to Vendor Class option");
130 }
131
132 tuples_[at] = tuple;
133}
134
136OptionVendorClass::getTuple(const size_t at) const {
137 if (at >= getTuplesNum()) {
138 isc_throw(isc::OutOfRange, "attempted to get an opaque data for the"
139 " vendor option at position " << at << " which is out of"
140 " range. There are only " << getTuplesNum() << " tuples");
141 }
142 return (tuples_[at]);
143}
144
145bool
146OptionVendorClass::hasTuple(const std::string& tuple_str) const {
147 // Iterate over existing tuples (there shouldn't be many of them),
148 // and try to match the searched one.
149 for (TuplesCollection::const_iterator it = tuples_.begin();
150 it != tuples_.end(); ++it) {
151 if (*it == tuple_str) {
152 return (true);
153 }
154 }
155 return (false);
156}
157
158
159uint16_t
161 // The option starts with the header and enterprise id.
162 uint16_t length = getHeaderLen() + sizeof(uint32_t);
163 // Now iterate over existing tuples and add their size.
164 for (TuplesCollection::const_iterator it = tuples_.begin();
165 it != tuples_.end(); ++it) {
166 // For DHCPv4 V-I Vendor Class option, there is enterprise id before
167 // every tuple.
168 if ((getUniverse() == V4) && (it != tuples_.begin())) {
169 length += sizeof(uint32_t);
170 }
171 length += it->getTotalLength();
172
173 }
174
175 return (length);
176}
177
178std::string
179OptionVendorClass::toText(int indent) const {
180 std::ostringstream s;
181
182 // Apply indentation
183 s << std::string(indent, ' ');
184 // Print type, length and first occurrence of enterprise id.
185 s << "type=" << getType() << ", len=" << len() - getHeaderLen() << ", "
186 " enterprise id=0x" << std::hex << getVendorId() << std::dec;
187 // Iterate over all tuples and print their size and contents.
188 for (unsigned i = 0; i < getTuplesNum(); ++i) {
189 // The DHCPv4 V-I Vendor Class has enterprise id before every tuple.
190 if ((getUniverse() == V4) && (i > 0)) {
191 s << ", enterprise id=0x" << std::hex << getVendorId() << std::dec;
192 }
193 // Print the tuple.
194 s << ", data-len" << i << "=" << getTuple(i).getLength();
195 s << ", vendor-class-data" << i << "='" << getTuple(i) << "'";
196 }
197
198 return (s.str());
199}
200
201} // namespace isc::dhcp
202} // 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:294
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:520
#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(const void *buffer, size_t length)
Read Unsigned 32-Bit Integer from Buffer.
Definition: io_utilities.h:79
Defines the logger used by the top-level component of kea-lfc.