| Kea 3.0.2-git

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