Kea 3.3.4
option_opaque_data_tuples.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2026 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 <util/str.h>
13
14#include <sstream>
15
16using namespace isc::util;
17
18namespace isc {
19namespace dhcp {
20
22 const uint16_t type,
23 OpaqueDataTuple::LengthFieldType length_field_type)
24 : Option(u, type), length_field_type_(length_field_type) {
25 if (length_field_type_ == OpaqueDataTuple::LENGTH_EMPTY) {
26 length_field_type_ = OptionDataTypeUtil::getTupleLenFieldType(u);
27 }
28}
29
31 const uint16_t type,
34 OpaqueDataTuple::LengthFieldType length_field_type)
35 : Option(u, type), length_field_type_(length_field_type) {
36 if (length_field_type_ == OpaqueDataTuple::LENGTH_EMPTY) {
37 length_field_type_ = OptionDataTypeUtil::getTupleLenFieldType(u);
38 }
40}
41
46
47void
49 packHeader(buf, check);
50
51 for (auto const& it : tuples_) {
52 it.pack(buf);
53 }
54 // That's it. We don't pack any sub-options here, because this option
55 // must not contain sub-options.
56}
57
58void
61 // We are skipping typical OutOfRange check for Option#unpack(begin, end),
62 // since empty collection of tuples is also a valid case where
63 // std::distance(begin, end) = 0
64
65 // Start reading opaque data.
66 size_t offset = 0;
67 while (offset < static_cast<size_t>(std::distance(begin, end))) {
68 // Parse a tuple.
69 OpaqueDataTuple tuple(length_field_type_, begin + offset, end);
70 addTuple(tuple);
71 // The tuple has been parsed correctly which implies that it is safe to
72 // advance the offset by its total length.
73 offset += tuple.getTotalLength();
74 }
75}
76
77void
79 if (tuple.getLengthFieldType() != length_field_type_) {
80 isc_throw(isc::BadValue, "attempted to add opaque data tuple having"
81 " invalid size of the length field "
82 << tuple.getDataFieldSize() << " to opaque data tuple option");
83 }
84
85 tuples_.push_back(tuple);
86}
87
88
89void
90OptionOpaqueDataTuples::setTuple(const size_t at, const OpaqueDataTuple& tuple) {
91 if (at >= getTuplesNum()) {
92 isc_throw(isc::OutOfRange, "attempted to set an opaque data for the"
93 " opaque data tuple option at position " << at << " which"
94 " is out of range");
95
96 } else if (tuple.getLengthFieldType() != length_field_type_) {
97 isc_throw(isc::BadValue, "attempted to set opaque data tuple having"
98 " invalid size of the length field "
99 << tuple.getDataFieldSize() << " to opaque data tuple option");
100 }
101
102 tuples_[at] = tuple;
103}
104
106OptionOpaqueDataTuples::getTuple(const size_t at) const {
107 if (at >= getTuplesNum()) {
108 isc_throw(isc::OutOfRange, "attempted to get an opaque data for the"
109 " opaque data tuple option at position " << at << " which is"
110 " out of range. There are only " << getTuplesNum() << " tuples");
111 }
112 return (tuples_[at]);
113}
114
115bool
116OptionOpaqueDataTuples::hasTuple(const std::string& tuple_str) const {
117 // Iterate over existing tuples (there shouldn't be many of them),
118 // and try to match the searched one.
119 for (auto const& it : tuples_) {
120 if (it == tuple_str) {
121 return (true);
122 }
123 }
124 return (false);
125}
126
127uint16_t
129 // The option starts with the header.
130 uint16_t length = getHeaderLen();
131 // Now iterate over existing tuples and add their size.
132 for (auto const& it : tuples_) {
133 length += it.getTotalLength();
134 }
135
136 return (length);
137}
138
139std::string
141 std::ostringstream s;
142
143 // Apply indentation
144 s << std::string(indent, ' ');
145
146 // Print type and length
147 s << "type=" << getType() << ", len=" << len() - getHeaderLen() << std::dec;
148 // Iterate over all tuples and print their size and contents.
149 for (unsigned i = 0; i < getTuplesNum(); ++i) {
150 // Print the tuple.
151 auto t = getTuple(i);
152 s << ", data-len" << i << "=" << t.getLength();
153 s << ", data" << i << "=" << str::dumpAsHex(t.getData());
154 auto txt = t.getText();
155 if (str::isPrintable(txt)) {
156 s << " '" << txt << "'";
157 }
158 }
159
160 return (s.str());
161}
162
163} // namespace isc::dhcp
164} // 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.
LengthFieldType
Size of the length field in the tuple.
static OpaqueDataTuple::LengthFieldType getTupleLenFieldType(Option::Universe u)
Returns Length Field Type for a 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.
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.
OptionOpaqueDataTuples(Option::Universe u, const uint16_t type, OpaqueDataTuple::LengthFieldType length_field_type=OpaqueDataTuple::LENGTH_EMPTY)
Constructor.
Universe
defines option universe DHCPv4 or DHCPv6
Definition option.h:91
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6).
Definition option.cc:327
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6).
Definition option.h:301
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition option.h:505
void packHeader(isc::util::OutputBuffer &buf, bool check=true) const
Store option's header in a buffer.
Definition option.cc:119
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
Definition option.cc:39
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:355
#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:31
boost::shared_ptr< Option > OptionPtr
Definition option.h:38
string dumpAsHex(const uint8_t *data, size_t length)
Dumps a buffer of bytes as a string of hexadecimal digits.
Definition str.cc:330
bool isPrintable(const string &content)
Check if a string is printable.
Definition str.cc:310
Defines the logger used by the top-level component of kea-lfc.