Kea 2.2.0
option_vendor_class.cc
Go to the documentation of this file.
1// Copyright (C) 2014-2022 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.
73 OpaqueDataTuple tuple(getLengthFieldType(), begin + offset, end);
74 addTuple(tuple);
75 // The tuple has been parsed correctly which implies that it is safe to
76 // advance the offset by its total length.
77 offset += tuple.getTotalLength();
78 // For DHCPv4 option, there is enterprise id before every opaque data
79 // tuple. Let's read it, unless we have already reached the end of
80 // buffer.
81 if ((getUniverse() == V4) && (begin + offset != end)) {
82 // Advance the offset by the size of enterprise id.
83 offset += sizeof(vendor_id_);
84 // If the offset already ran over the buffer length or there is
85 // no space left for the empty tuple (thus we add 1), we have
86 // to signal the option truncation.
87 if (offset + 1 >= std::distance(begin, end)) {
88 isc_throw(isc::OutOfRange, "truncated DHCPv4 V-I Vendor Class"
89 " option - it should contain enterprise id followed"
90 " by opaque data field tuple");
91 }
92 }
93 }
94}
95
96void
98 if (tuple.getLengthFieldType() != getLengthFieldType()) {
99 isc_throw(isc::BadValue, "attempted to add opaque data tuple having"
100 " invalid size of the length field "
101 << tuple.getDataFieldSize() << " to Vendor Class option");
102 }
103
104 tuples_.push_back(tuple);
105}
106
107
108void
109OptionVendorClass::setTuple(const size_t at, const OpaqueDataTuple& tuple) {
110 if (at >= getTuplesNum()) {
111 isc_throw(isc::OutOfRange, "attempted to set an opaque data for the"
112 " vendor option at position " << at << " which is out of"
113 " range");
114
115 } else if (tuple.getLengthFieldType() != getLengthFieldType()) {
116 isc_throw(isc::BadValue, "attempted to set opaque data tuple having"
117 " invalid size of the length field "
118 << tuple.getDataFieldSize() << " to Vendor Class option");
119 }
120
121 tuples_[at] = tuple;
122}
123
125OptionVendorClass::getTuple(const size_t at) const {
126 if (at >= getTuplesNum()) {
127 isc_throw(isc::OutOfRange, "attempted to get an opaque data for the"
128 " vendor option at position " << at << " which is out of"
129 " range. There are only " << getTuplesNum() << " tuples");
130 }
131 return (tuples_[at]);
132}
133
134bool
135OptionVendorClass::hasTuple(const std::string& tuple_str) const {
136 // Iterate over existing tuples (there shouldn't be many of them),
137 // and try to match the searched one.
138 for (TuplesCollection::const_iterator it = tuples_.begin();
139 it != tuples_.end(); ++it) {
140 if (*it == tuple_str) {
141 return (true);
142 }
143 }
144 return (false);
145}
146
147
148uint16_t
150 // The option starts with the header and enterprise id.
151 uint16_t length = getHeaderLen() + sizeof(uint32_t);
152 // Now iterate over existing tuples and add their size.
153 for (TuplesCollection::const_iterator it = tuples_.begin();
154 it != tuples_.end(); ++it) {
155 // For DHCPv4 V-I Vendor Class option, there is enterprise id before
156 // every tuple.
157 if ((getUniverse() == V4) && (it != tuples_.begin())) {
158 length += sizeof(uint32_t);
159 }
160 length += it->getTotalLength();
161
162 }
163
164 return (length);
165}
166
167std::string
168OptionVendorClass::toText(int indent) const {
169 std::ostringstream s;
170
171 // Apply indentation
172 s << std::string(indent, ' ');
173 // Print type, length and first occurrence of enterprise id.
174 s << "type=" << getType() << ", len=" << len() - getHeaderLen() << ", "
175 " enterprise id=0x" << std::hex << getVendorId() << std::dec;
176 // Iterate over all tuples and print their size and contents.
177 for (unsigned i = 0; i < getTuplesNum(); ++i) {
178 // The DHCPv4 V-I Vendor Class has enterprise id before every tuple.
179 if ((getUniverse() == V4) && (i > 0)) {
180 s << ", enterprise id=0x" << std::hex << getVendorId() << std::dec;
181 }
182 // Print the tuple.
183 s << ", data-len" << i << "=" << getTuple(i).getLength();
184 s << ", vendor-class-data" << i << "='" << getTuple(i) << "'";
185 }
186
187 return (s.str());
188}
189
190} // namespace isc::dhcp
191} // 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.
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:36
uint32_t readUint32(const uint8_t *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.