Kea  1.9.9-git
option_vendor_class.cc
Go to the documentation of this file.
1 // Copyright (C) 2014-2017 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 
10 #include <dhcp/opaque_data_tuple.h>
12 #include <sstream>
13 
14 namespace isc {
15 namespace 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 
37 void
39  packHeader(buf);
40 
41  buf.writeUint32(getVendorId());
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())) {
48  buf.writeUint32(getVendorId());
49  }
50  it->pack(buf);
51 
52  }
53 
54 }
55 
56 void
59  if (std::distance(begin, end) < getMinimalLength() - getHeaderLen()) {
60  isc_throw(OutOfRange, "parsed Vendor Class option data truncated to"
61  " size " << std::distance(begin, end));
62  }
63  // Option must contain at least one enterprise id. It is ok to read 4-byte
64  // value here because we have checked that the buffer he minimal length.
65  vendor_id_ = isc::util::readUint32(&(*begin), distance(begin, end));
66  begin += sizeof(vendor_id_);
67 
68  // Start reading opaque data.
69  size_t offset = 0;
70  while (offset < std::distance(begin, end)) {
71  // Parse a tuple.
72  OpaqueDataTuple tuple(getLengthFieldType(), begin + offset, end);
73  addTuple(tuple);
74  // The tuple has been parsed correctly which implies that it is safe to
75  // advance the offset by its total length.
76  offset += tuple.getTotalLength();
77  // For DHCPv4 option, there is enterprise id before every opaque data
78  // tuple. Let's read it, unless we have already reached the end of
79  // buffer.
80  if ((getUniverse() == V4) && (begin + offset != end)) {
81  // Advance the offset by the size of enterprise id.
82  offset += sizeof(vendor_id_);
83  // If the offset already ran over the buffer length or there is
84  // no space left for the empty tuple (thus we add 1), we have
85  // to signal the option truncation.
86  if (offset + 1 >= std::distance(begin, end)) {
87  isc_throw(isc::OutOfRange, "truncated DHCPv4 V-I Vendor Class"
88  " option - it should contain enterprise id followed"
89  " by opaque data field tuple");
90  }
91  }
92  }
93 }
94 
95 void
97  if (tuple.getLengthFieldType() != getLengthFieldType()) {
98  isc_throw(isc::BadValue, "attempted to add opaque data tuple having"
99  " invalid size of the length field "
100  << tuple.getDataFieldSize() << " to Vendor Class option");
101  }
102 
103  tuples_.push_back(tuple);
104 }
105 
106 
107 void
108 OptionVendorClass::setTuple(const size_t at, const OpaqueDataTuple& tuple) {
109  if (at >= getTuplesNum()) {
110  isc_throw(isc::OutOfRange, "attempted to set an opaque data for the"
111  " vendor option at position " << at << " which is out of"
112  " range");
113 
114  } else if (tuple.getLengthFieldType() != getLengthFieldType()) {
115  isc_throw(isc::BadValue, "attempted to set opaque data tuple having"
116  " invalid size of the length field "
117  << tuple.getDataFieldSize() << " to Vendor Class option");
118  }
119 
120  tuples_[at] = tuple;
121 }
122 
124 OptionVendorClass::getTuple(const size_t at) const {
125  if (at >= getTuplesNum()) {
126  isc_throw(isc::OutOfRange, "attempted to get an opaque data for the"
127  " vendor option at position " << at << " which is out of"
128  " range. There are only " << getTuplesNum() << " tuples");
129  }
130  return (tuples_[at]);
131 }
132 
133 bool
134 OptionVendorClass::hasTuple(const std::string& tuple_str) const {
135  // Iterate over existing tuples (there shouldn't be many of them),
136  // and try to match the searched one.
137  for (TuplesCollection::const_iterator it = tuples_.begin();
138  it != tuples_.end(); ++it) {
139  if (*it == tuple_str) {
140  return (true);
141  }
142  }
143  return (false);
144 }
145 
146 
147 uint16_t
149  // The option starts with the header and enterprise id.
150  uint16_t length = getHeaderLen() + sizeof(uint32_t);
151  // Now iterate over existing tuples and add their size.
152  for (TuplesCollection::const_iterator it = tuples_.begin();
153  it != tuples_.end(); ++it) {
154  // For DHCPv4 V-I Vendor Class option, there is enterprise id before
155  // every tuple.
156  if ((getUniverse() == V4) && (it != tuples_.begin())) {
157  length += sizeof(uint32_t);
158  }
159  length += it->getTotalLength();
160 
161  }
162 
163  return (length);
164 }
165 
166 std::string
167 OptionVendorClass::toText(int indent) const {
168  std::ostringstream s;
169 
170  // Apply indentation
171  s << std::string(indent, ' ');
172  // Print type, length and first occurrence of enterprise id.
173  s << "type=" << getType() << ", len=" << len() - getHeaderLen() << ", "
174  " enterprise id=0x" << std::hex << getVendorId() << std::dec;
175  // Iterate over all tuples and print their size and contents.
176  for (unsigned i = 0; i < getTuplesNum(); ++i) {
177  // The DHCPv4 V-I Vendor Class has enterprise id before every tuple.
178  if ((getUniverse() == V4) && (i > 0)) {
179  s << ", enterprise id=0x" << std::hex << getVendorId() << std::dec;
180  }
181  // Print the tuple.
182  s << ", data-len" << i << "=" << getTuple(i).getLength();
183  s << ", vendor-class-data" << i << "='" << getTuple(i) << "'";
184  }
185 
186  return (s.str());
187 }
188 
189 } // namespace isc::dhcp
190 } // namespace isc
Universe getUniverse() const
returns option universe (V4 or V6)
Definition: option.h:232
virtual uint16_t len() const
Returns the full length of the option, including option header.
int getDataFieldSize() const
Returns the size of the tuple length field.
bool hasTuple(const std::string &tuple_str) const
Checks if the Vendor Class holds the opaque data tuple with the specified string. ...
void packHeader(isc::util::OutputBuffer &buf) const
Store option's header in a buffer.
Definition: option.cc:126
LengthFieldType getLengthFieldType() const
Returns tuple length data field type.
OpaqueDataTuple getTuple(const size_t at) const
Returns opaque data tuple at the specified position.
OptionVendorClass(Option::Universe u, const uint32_t vendor_id)
Constructor.
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:82
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:338
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
void addTuple(const OpaqueDataTuple &tuple)
Adds a new opaque data tuple to the option.
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses buffer holding an option.
virtual std::string toText(int indent=0) const
Returns text representation of the option.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
uint32_t readUint32(const uint8_t *buffer, size_t length)
Read Unsigned 32-Bit Integer from Buffer.
Definition: io_utilities.h:79
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
uint32_t getVendorId() const
Returns enterprise id.
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
Defines the logger used by the top-level component of kea-dhcp-ddns.
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:288
size_t getTotalLength() const
Returns a total size of the tuple, including length field.
Represents a single instance of the opaque data preceded by length.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
virtual void pack(isc::util::OutputBuffer &buf) const
Renders option into the buffer in the wire format.
size_t getLength() const
Returns the length of the data in the tuple.
size_t getTuplesNum() const
Returns the number of opaque data tuples added to the option.
OptionPtr clone() const
Copies this option and returns a pointer to the copy.
void setTuple(const size_t at, const OpaqueDataTuple &tuple)
Replaces tuple at the specified index with a new tuple.