Kea: opaque_data_tuple.cc Source File

| Kea 3.2.0-git

opaque_data_tuple.cc
Go to the documentation of this file.
1// Copyright (C) 2014-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 <dhcp/opaque_data_tuple.h>
10#include <util/io.h>
11
12namespace isc {
13namespace dhcp {
14
15OpaqueDataTuple::OpaqueDataTuple(LengthFieldType length_field_type)
16 : length_field_type_(length_field_type) {
17}
18
19void
20 OpaqueDataTuple::append(const std::string& text) {
21 // Don't do anything if text is empty.
22 if (!text.empty()) {
23 append(&text[0], text.size());
24 }
25}
26
27void
28 OpaqueDataTuple::assign(const std::string& text) {
29 // If empty string is being assigned, reset the buffer.
30 if (text.empty()) {
31 clear();
32 } else {
33 assign(&text[0], text.size());
34 }
35}
36
37void
38OpaqueDataTuple::clear() {
39 data_.clear();
40}
41
42bool
43OpaqueDataTuple::equals(const std::string& other) const {
44 return (getText() == other);
45}
46
47std::string
48OpaqueDataTuple::getText() const {
49 // Convert the data carried in the buffer to a string.
50 return (std::string(data_.begin(), data_.end()));
51}
52
53void
54OpaqueDataTuple::pack(isc::util::OutputBuffer& buf) const {
55 if ((1U << (getDataFieldSize() * 8)) <= getLength()) {
56 isc_throw(OpaqueDataTupleError, "failed to create on-wire format of the"
57 " opaque data field, because current data length "
58 << getLength() << " exceeds the maximum size for the length"
59 << " field size " << getDataFieldSize());
60 }
61
62 if (getDataFieldSize() == 1) {
63 buf.writeUint8(static_cast<uint8_t>(getLength()));
64 } else {
65 buf.writeUint16(getLength());
66 }
67
68 if (getLength() > 0) {
69 buf.writeData(&getData()[0], getLength());
70 }
71}
72
73int
74OpaqueDataTuple::getDataFieldSize() const {
75 return (length_field_type_ == LENGTH_1_BYTE ? 1 : 2);
76}
77
78 OpaqueDataTuple&
79OpaqueDataTuple::operator=(const std::string& other) {
80 // Replace existing data with the new data converted from a string.
81 assign(&other[0], other.length());
82 return (*this);
83}
84
85bool
86OpaqueDataTuple::operator==(const std::string& other) const {
87 return (equals(other));
88}
89
90bool
91OpaqueDataTuple::operator!=(const std::string& other) {
92 return (!equals(other));
93}
94
95std::ostream& operator<<(std::ostream& os, const OpaqueDataTuple& tuple) {
96 os << tuple.getText();
97 return (os);
98}
99
100std::istream& operator>>(std::istream& is, OpaqueDataTuple& tuple) {
101 // We will replace the current data with new data.
102 tuple.clear();
103 char buf[256];
104 // Read chunks of data as long as we haven't reached the end of the stream.
105 while (!is.eof()) {
106 is.read(buf, sizeof(buf));
107 // Append the chunk of data we have just read. It is fine if the
108 // gcount is 0, because append() function will check that.
109 tuple.append(buf, is.gcount());
110 }
111 // Clear eof flag, so as the stream can be read again.
112 is.clear();
113 return (is);
114}
115
116void
117OpaqueDataTuple::unpack(OpaqueDataTuple::InputIterator begin, OpaqueDataTuple::InputIterator end) {
118 // The buffer must at least hold the size of the data.
119 if (std::distance(begin, end) < getDataFieldSize()) {
120 isc_throw(OpaqueDataTupleError,
121 "unable to parse the opaque data tuple, the buffer"
122 " length is " << std::distance(begin, end)
123 << ", expected at least " << getDataFieldSize());
124 }
125 // Read the data length from the length field, depending on the
126 // size of the data field (1 or 2 bytes).
127 size_t len = getDataFieldSize() == 1 ? *begin :
128 isc::util::readUint16(&(*begin), std::distance(begin, end));
129 // Now that we have the expected data size, let's check that the
130 // reminder of the buffer is long enough.
131 begin += getDataFieldSize();
132 // Attempt to parse as a length-value pair.
133 if (static_cast<size_t>(std::distance(begin, end)) < len) {
134 if (Option::lenient_parsing_) {
135 // Fallback to parsing the rest of the option as a single value.
136 len = std::distance(begin, end);
137 } else {
138 isc_throw(OpaqueDataTupleError,
139 "unable to parse the opaque data tuple, "
140 "the buffer length is " << std::distance(begin, end)
141 << ", but the tuple length is " << len);
142 }
143 }
144 // The buffer length is long enough to read the desired amount of data.
145 assign(begin, len);
146}
147
148} // namespace dhcp
149} // namespace isc
isc::dhcp::OpaqueDataTupleError
Exception to be thrown when the operation on OpaqueDataTuple object results in an error.
Definition opaque_data_tuple.h:23
isc::dhcp::OpaqueDataTuple
Represents a single instance of the opaque data preceded by length.
Definition opaque_data_tuple.h:46
isc::dhcp::OpaqueDataTuple::operator==
bool operator==(const std::string &other) const
Equality operator.
Definition opaque_data_tuple.cc:86
isc::dhcp::OpaqueDataTuple::clear
void clear()
Removes the contents of the tuple.
Definition opaque_data_tuple.cc:38
isc::dhcp::OpaqueDataTuple::getDataFieldSize
int getDataFieldSize() const
Returns the size of the tuple length field.
Definition opaque_data_tuple.cc:74
isc::dhcp::OpaqueDataTuple::operator=
OpaqueDataTuple & operator=(const std::string &other)
Assignment operator.
Definition opaque_data_tuple.cc:79
isc::dhcp::OpaqueDataTuple::OpaqueDataTuple
OpaqueDataTuple(LengthFieldType length_field_type)
Default constructor.
Definition opaque_data_tuple.cc:15
isc::dhcp::OpaqueDataTuple::getData
const Buffer & getData() const
Returns a reference to the buffer holding tuple data.
Definition opaque_data_tuple.h:174
isc::dhcp::OpaqueDataTuple::LengthFieldType
LengthFieldType
Size of the length field in the tuple.
Definition opaque_data_tuple.h:56
isc::dhcp::OpaqueDataTuple::LENGTH_1_BYTE
@ LENGTH_1_BYTE
Definition opaque_data_tuple.h:58
isc::dhcp::OpaqueDataTuple::pack
void pack(isc::util::OutputBuffer &buf) const
Renders the tuple to a buffer in the wire format.
Definition opaque_data_tuple.cc:54
isc::dhcp::OpaqueDataTuple::InputIterator
Buffer::const_iterator InputIterator
Definition opaque_data_tuple.h:64
isc::dhcp::OpaqueDataTuple::assign
void assign(const char *data, const size_t len)
Assigns data to the tuple.
Definition opaque_data_tuple.h:131
isc::dhcp::OpaqueDataTuple::getText
std::string getText() const
Return the tuple data in the textual format.
Definition opaque_data_tuple.cc:48
isc::dhcp::OpaqueDataTuple::append
void append(const char *data, const size_t len)
Appends data to the tuple.
Definition opaque_data_tuple.h:103
isc::dhcp::OpaqueDataTuple::equals
bool equals(const std::string &other) const
Checks if the data carried in the tuple match the string.
Definition opaque_data_tuple.cc:43
isc::dhcp::OpaqueDataTuple::unpack
void unpack(InputIterator begin, InputIterator end)
Parses wire data and creates a tuple from it.
Definition opaque_data_tuple.cc:117
isc::dhcp::OpaqueDataTuple::operator!=
bool operator!=(const std::string &other)
Inequality operator.
Definition opaque_data_tuple.cc:91
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::Option::lenient_parsing_
static bool lenient_parsing_
Governs whether options should be parsed less strictly.
Definition option.h:490
isc::util::OutputBuffer
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:346
isc::util::OutputBuffer::writeUint8
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition buffer.h:476
isc::util::OutputBuffer::writeUint16
void writeUint16(uint16_t data)
Write an unsigned 16-bit integer in host byte order into the buffer in network byte order.
Definition buffer.h:501
isc::util::OutputBuffer::writeData
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition buffer.h:559
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
io.h
isc::asiolink::operator<<
std::ostream & operator<<(std::ostream &os, const IOAddress &address)
Insert the IOAddress as a string into stream.
Definition io_address.cc:131
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::operator>>
std::istream & operator>>(std::istream &is, OpaqueDataTuple &tuple)
Inserts data carried in the stream into the tuple.
Definition opaque_data_tuple.cc:100
isc::util::readUint16
uint16_t readUint16(void const *const buffer, size_t const length)
uint16_t wrapper over readUint.
Definition io.h:76
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
opaque_data_tuple.h