Kea  2.3.4-git
option_opaque_data_tuples.cc
Go to the documentation of this file.
1 // Copyright (C) 2015-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 
10 #include <dhcp/opaque_data_tuple.h>
12 #include <sstream>
13 
14 namespace isc {
15 namespace dhcp {
16 
18  const uint16_t type)
19  : Option(u, type) {
20 }
21 
23  const uint16_t type,
26  : Option(u, type) {
27  unpack(begin, end);
28 }
29 
32  return (cloneInternal<OptionOpaqueDataTuples>());
33 }
34 
35 void
37  packHeader(buf, check);
38 
39  for (TuplesCollection::const_iterator it = tuples_.begin();
40  it != tuples_.end(); ++it) {
41  it->pack(buf);
42  }
43  // That's it. We don't pack any sub-options here, because this option
44  // must not contain sub-options.
45 }
46 
47 void
50  if (std::distance(begin, end) < getMinimalLength() - getHeaderLen()) {
51  isc_throw(OutOfRange, "parsed data tuples option data truncated to"
52  " size " << std::distance(begin, end));
53  }
54 
55  // Start reading opaque data.
56  size_t offset = 0;
57  while (offset < std::distance(begin, end)) {
58  // Parse a tuple.
59  OpaqueDataTuple tuple(getLengthFieldType(), begin + offset, end);
60  addTuple(tuple);
61  // The tuple has been parsed correctly which implies that it is safe to
62  // advance the offset by its total length.
63  offset += tuple.getTotalLength();
64  }
65 }
66 
67 void
69  if (tuple.getLengthFieldType() != getLengthFieldType()) {
70  isc_throw(isc::BadValue, "attempted to add opaque data tuple having"
71  " invalid size of the length field "
72  << tuple.getDataFieldSize() << " to opaque data tuple option");
73  }
74 
75  tuples_.push_back(tuple);
76 }
77 
78 
79 void
80 OptionOpaqueDataTuples::setTuple(const size_t at, const OpaqueDataTuple& tuple) {
81  if (at >= getTuplesNum()) {
82  isc_throw(isc::OutOfRange, "attempted to set an opaque data for the"
83  " opaque data tuple option at position " << at << " which"
84  " is out of range");
85 
86  } else if (tuple.getLengthFieldType() != getLengthFieldType()) {
87  isc_throw(isc::BadValue, "attempted to set opaque data tuple having"
88  " invalid size of the length field "
89  << tuple.getDataFieldSize() << " to opaque data tuple option");
90  }
91 
92  tuples_[at] = tuple;
93 }
94 
96 OptionOpaqueDataTuples::getTuple(const size_t at) const {
97  if (at >= getTuplesNum()) {
98  isc_throw(isc::OutOfRange, "attempted to get an opaque data for the"
99  " opaque data tuple option at position " << at << " which is"
100  " out of range. There are only " << getTuplesNum() << " tuples");
101  }
102  return (tuples_[at]);
103 }
104 
105 bool
106 OptionOpaqueDataTuples::hasTuple(const std::string& tuple_str) const {
107  // Iterate over existing tuples (there shouldn't be many of them),
108  // and try to match the searched one.
109  for (TuplesCollection::const_iterator it = tuples_.begin();
110  it != tuples_.end(); ++it) {
111  if (*it == tuple_str) {
112  return (true);
113  }
114  }
115  return (false);
116 }
117 
118 uint16_t
120  // The option starts with the header.
121  uint16_t length = getHeaderLen();
122  // Now iterate over existing tuples and add their size.
123  for (TuplesCollection::const_iterator it = tuples_.begin();
124  it != tuples_.end(); ++it) {
125  length += it->getTotalLength();
126  }
127 
128  return (length);
129 }
130 
131 std::string
133  std::ostringstream s;
134 
135  // Apply indentation
136  s << std::string(indent, ' ');
137 
138  // Print type and length
139  s << "type=" << getType() << ", len=" << len() - getHeaderLen() << std::dec;
140  // Iterate over all tuples and print their size and contents.
141  for (unsigned i = 0; i < getTuplesNum(); ++i) {
142  // Print the tuple.
143  s << ", data-len" << i << "=" << getTuple(i).getLength();
144  s << ", data" << i << "='" << getTuple(i) << "'";
145  }
146 
147  return (s.str());
148 }
149 
150 } // namespace isc::dhcp
151 } // namespace isc
void setTuple(const size_t at, const OpaqueDataTuple &tuple)
Replaces tuple at the specified index with a new tuple.
size_t getTuplesNum() const
Returns the number of opaque data tuples added to the option.
void addTuple(const OpaqueDataTuple &tuple)
Adds a new opaque data tuple to the option.
OptionOpaqueDataTuples(Option::Universe u, const uint16_t type)
Constructor.
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:293
OpaqueDataTuple getTuple(const size_t at) const
Returns opaque data tuple at the specified position.
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:83
void packHeader(isc::util::OutputBuffer &buf, bool check=true) const
Store option&#39;s header in a buffer.
Definition: option.cc:119
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:321
LengthFieldType getLengthFieldType() const
Returns tuple length data field type.
#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...
size_t getTotalLength() const
Returns a total size of the tuple, including length field.
OptionPtr clone() const
Copies this option and returns a pointer to the copy.
bool hasTuple(const std::string &tuple_str) const
Checks if the object holds the opaque data tuple with the specified string.
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
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
Defines the logger used by the top-level component of kea-lfc.
int getDataFieldSize() const
Returns the size of the tuple length field.
virtual std::string toText(int indent=0) const
Returns text representation of the option.
Represents a single instance of the opaque data preceded by length.
size_t getLength() const
Returns the length of the data in the tuple.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses buffer holding an option.
virtual uint16_t len() const
Returns the full length of the option, including option header.
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Renders option into the buffer in the wire format.