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 
17 namespace isc {
18 namespace dhcp {
19 
23 public:
24  OpaqueDataTupleError(const char* file, size_t line, const char* what) :
25  isc::Exception(file, line, what) { };
26 };
27 
28 
46 public:
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 
276 private:
277 
279  Buffer data_;
281  LengthFieldType length_field_type_;
282 };
283 
285 typedef boost::shared_ptr<OpaqueDataTuple> OpaqueDataTuplePtr;
286 
296 std::ostream& operator<<(std::ostream& os, const OpaqueDataTuple& tuple);
297 
306 std::istream& operator>>(std::istream& is, OpaqueDataTuple& tuple);
307 
308 } // namespace isc::dhcp
309 } // namespace isc
310 
311 #endif
isc::dhcp::OpaqueDataTuple::OpaqueDataTuple
OpaqueDataTuple(LengthFieldType length_field_type, InputIterator begin, InputIterator end)
Constructor.
Definition: opaque_data_tuple.h:82
isc::dhcp::OpaqueDataTuple::LENGTH_1_BYTE
@ LENGTH_1_BYTE
Definition: opaque_data_tuple.h:56
isc::dhcp::OpaqueDataTuple::getLength
size_t getLength() const
Returns the length of the data in the tuple.
Definition: opaque_data_tuple.h:153
isc::dhcp::OpaqueDataTuple::getText
std::string getText() const
Return the tuple data in the textual format.
Definition: opaque_data_tuple.cc:47
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:42
isc::dhcp::OpaqueDataTuple::append
void append(InputIterator data, const size_t len)
Appends data to the tuple.
Definition: opaque_data_tuple.h:102
isc::dhcp::OpaqueDataTuple::operator!=
bool operator!=(const std::string &other)
Inequality operator.
Definition: opaque_data_tuple.cc:91
isc::dhcp::OpaqueDataTuple::LengthFieldType
LengthFieldType
Size of the length field in the tuple.
Definition: opaque_data_tuple.h:55
isc::dhcp::OpaqueDataTuple::operator=
OpaqueDataTuple & operator=(const std::string &other)
Assignment operator.
Definition: opaque_data_tuple.cc:79
isc::dhcp::OpaqueDataTuple::getLengthFieldType
LengthFieldType getLengthFieldType() const
Returns tuple length data field type.
Definition: opaque_data_tuple.h:148
isc::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition: exceptions/exceptions.h:23
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::dhcp::OpaqueDataTuple::assign
void assign(InputIterator data, const size_t len)
Assigns data to the tuple.
Definition: opaque_data_tuple.h:127
isc::dhcp::OpaqueDataTupleError
Exception to be thrown when the operation on OpaqueDataTuple object results in an error.
Definition: opaque_data_tuple.h:22
isc::util::OutputBuffer
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
isc::util::readUint16
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.
Definition: io_utilities.h:28
isc::dhcp::OpaqueDataTupleError::OpaqueDataTupleError
OpaqueDataTupleError(const char *file, size_t line, const char *what)
Definition: opaque_data_tuple.h:24
isc::dhcp::OpaqueDataTuple::LENGTH_2_BYTES
@ LENGTH_2_BYTES
Definition: opaque_data_tuple.h:57
isc::dhcp::OpaqueDataTuple::getTotalLength
size_t getTotalLength() const
Returns a total size of the tuple, including length field.
Definition: opaque_data_tuple.h:158
buffer.h
isc::dhcp::OpaqueDataTuple::operator==
bool operator==(const std::string &other) const
Equality operator.
Definition: opaque_data_tuple.cc:86
isc::dhcp::OpaqueDataTuple::getDataFieldSize
int getDataFieldSize() const
Returns the size of the tuple length field.
Definition: opaque_data_tuple.cc:74
isc::dhcp::OpaqueDataTuplePtr
boost::shared_ptr< OpaqueDataTuple > OpaqueDataTuplePtr
Pointer to the OpaqueDataTuple object.
Definition: opaque_data_tuple.h:285
isc::dhcp::OpaqueDataTuple
Represents a single instance of the opaque data preceded by length.
Definition: opaque_data_tuple.h:45
io_utilities.h
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:53
isc::dhcp::OpaqueDataTuple::Buffer
std::vector< uint8_t > Buffer
Defines a type of the data buffer used to hold the opaque data.
Definition: opaque_data_tuple.h:61
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::dhcp::operator<<
std::ostream & operator<<(std::ostream &os, const OpaqueDataTuple &tuple)
Inserts the OpaqueDataTuple as a string into stream.
Definition: opaque_data_tuple.cc:95
isc::dhcp::OpaqueDataTuple::OpaqueDataTuple
OpaqueDataTuple(LengthFieldType length_field_type)
Default constructor.
Definition: opaque_data_tuple.cc:14
isc::dhcp::OpaqueDataTuple::getData
const Buffer & getData() const
Returns a reference to the buffer holding tuple data.
Definition: opaque_data_tuple.h:167
isc::dhcp::OpaqueDataTuple::unpack
void unpack(InputIterator begin, InputIterator end)
Parses wire data and creates a tuple from it.
Definition: opaque_data_tuple.h:213
isc::dhcp::OpaqueDataTuple::clear
void clear()
Removes the contents of the tuple.
Definition: opaque_data_tuple.cc:37