Kea 2.2.0
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
12#include <sstream>
13
14namespace isc {
15namespace 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
35void
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
47void
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
67void
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
79void
80OptionOpaqueDataTuples::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
96OptionOpaqueDataTuples::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
105bool
106OptionOpaqueDataTuples::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
118uint16_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
131std::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
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.
void setTuple(const size_t at, const OpaqueDataTuple &tuple)
Replaces tuple at the specified index with a new tuple.
virtual std::string toText(int indent=0) const
Returns text representation of the option.
size_t getTuplesNum() const
Returns the number of opaque data tuples added to the option.
OptionOpaqueDataTuples(Option::Universe u, const uint16_t type)
Constructor.
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses buffer holding an option.
void addTuple(const OpaqueDataTuple &tuple)
Adds a new opaque data tuple to the option.
virtual uint16_t len() const
Returns the full length of the option, including option header.
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.
OpaqueDataTuple getTuple(const size_t at) const
Returns opaque data tuple at the specified position.
bool hasTuple(const std::string &tuple_str) const
Checks if the object 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
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
#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
Defines the logger used by the top-level component of kea-lfc.