Kea 1.5.0
opaque_data_tuple.h
Go to the documentation of this file.
1// Copyright (C) 2014-2017 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#ifndef OPAQUE_DATA_TUPLE_H
8#define OPAQUE_DATA_TUPLE_H
9
10#include <util/buffer.h>
11#include <util/io_utilities.h>
12#include <iostream>
13#include <iterator>
14#include <string>
15#include <vector>
16
17namespace isc {
18namespace dhcp {
19
23public:
24 OpaqueDataTupleError(const char* file, size_t line, const char* what) :
25 isc::Exception(file, line, what) { };
26};
27
28
46public:
47
58 };
59
61 typedef std::vector<uint8_t> Buffer;
62
67 OpaqueDataTuple(LengthFieldType length_field_type);
68
81 template<typename InputIterator>
82 OpaqueDataTuple(LengthFieldType length_field_type, InputIterator begin,
83 InputIterator end)
84 : length_field_type_(length_field_type) {
85 unpack(begin, end);
86 }
87
101 template<typename InputIterator>
102 void append(InputIterator data, const size_t len) {
103 data_.insert(data_.end(), data, data + len);
104 }
105
112 void append(const std::string& text);
113
126 template<typename InputIterator>
127 void assign(InputIterator data, const size_t len) {
128 data_.assign(data, data + len);
129 }
130
137 void assign(const std::string& text);
138
140 void clear();
141
145 bool equals(const std::string& other) const;
146
149 return (length_field_type_);
150 }
151
153 size_t getLength() const {
154 return (data_.size());
155 }
156
158 size_t getTotalLength() const {
159 return (getDataFieldSize() + getLength());
160 }
161
167 const Buffer& getData() const {
168 return (data_);
169 }
170
172 std::string getText() const;
173
195 void pack(isc::util::OutputBuffer& buf) const;
196
212 template<typename InputIterator>
213 void unpack(InputIterator begin, InputIterator end) {
214 Buffer buf(begin, end);
215 // The buffer must at least hold the size of the data.
216 if (std::distance(begin, end) < getDataFieldSize()) {
218 "unable to parse the opaque data tuple, the buffer"
219 " length is " << std::distance(begin, end)
220 << ", expected at least " << getDataFieldSize());
221 }
222 // Read the data length from the length field, depending on the
223 // size of the data field (1 or 2 bytes).
224 size_t len = getDataFieldSize() == 1 ? *begin :
225 isc::util::readUint16(&(*begin), std::distance(begin, end));
226 // Now that we have the expected data size, let's check that the
227 // reminder of the buffer is long enough.
228 begin += getDataFieldSize();
229 if (std::distance(begin, end) < len) {
231 "unable to parse the opaque data tuple, the buffer"
232 " length is " << std::distance(begin, end)
233 << ", but the length of the tuple in the length field"
234 " is " << len);
235 }
236 // The buffer length is long enough to read the desired amount of data.
237 assign(begin, len);
238 }
239
241 //{@
242
249 OpaqueDataTuple& operator=(const std::string& other);
250
258 bool operator==(const std::string& other) const;
259
268 bool operator!=(const std::string& other);
270
274 int getDataFieldSize() const;
275
276private:
277
279 Buffer data_;
281 LengthFieldType length_field_type_;
282};
283
285typedef boost::shared_ptr<OpaqueDataTuple> OpaqueDataTuplePtr;
286
296std::ostream& operator<<(std::ostream& os, const OpaqueDataTuple& tuple);
297
306std::istream& operator>>(std::istream& is, OpaqueDataTuple& tuple);
307
308} // namespace isc::dhcp
309} // namespace isc
310
311#endif
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Exception to be thrown when the operation on OpaqueDataTuple object results in an error.
OpaqueDataTupleError(const char *file, size_t line, const char *what)
Represents a single instance of the opaque data preceded by length.
bool operator==(const std::string &other) const
Equality operator.
void clear()
Removes the contents of the tuple.
int getDataFieldSize() const
Returns the size of the tuple length field.
OpaqueDataTuple(LengthFieldType length_field_type, InputIterator begin, InputIterator end)
Constructor.
OpaqueDataTuple & operator=(const std::string &other)
Assignment operator.
const Buffer & getData() const
Returns a reference to the buffer holding tuple data.
LengthFieldType
Size of the length field in the tuple.
void pack(isc::util::OutputBuffer &buf) const
Renders the tuple to a buffer in the wire format.
void unpack(InputIterator begin, InputIterator end)
Parses wire data and creates a tuple from it.
LengthFieldType getLengthFieldType() const
Returns tuple length data field type.
std::string getText() const
Return the tuple data in the textual format.
size_t getTotalLength() const
Returns a total size of the tuple, including length field.
std::vector< uint8_t > Buffer
Defines a type of the data buffer used to hold the opaque data.
void append(InputIterator data, const size_t len)
Appends data to the tuple.
bool equals(const std::string &other) const
Checks if the data carried in the tuple match the string.
bool operator!=(const std::string &other)
Inequality operator.
void assign(InputIterator data, const size_t len)
Assigns data to the tuple.
size_t getLength() const
Returns the length of the data in the tuple.
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.
std::istream & operator>>(std::istream &is, OpaqueDataTuple &tuple)
Inserts data carried in the stream into the tuple.
boost::shared_ptr< OpaqueDataTuple > OpaqueDataTuplePtr
Pointer to the OpaqueDataTuple object.
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.
Definition: io_utilities.h:28
Defines the logger used by the top-level component of kea-dhcp-ddns.