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