Kea 2.0.2
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
12#include <sstream>
13
14namespace isc {
15namespace 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
35void
37 packHeader(buf);
38
39 for (TuplesCollection::const_iterator it = tuples_.begin();
40 it != tuples_.end(); ++it) {
41 it->pack(buf);
42 }
43}
44
45void
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
65void
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
77void
78OptionOpaqueDataTuples::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
94OptionOpaqueDataTuples::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
103bool
104OptionOpaqueDataTuples::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
116uint16_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
129std::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
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Represents a single instance of the opaque data preceded by length.
int getDataFieldSize() const
Returns the size of the tuple length field.
LengthFieldType getLengthFieldType() const
Returns tuple length data field type.
size_t getTotalLength() const
Returns a total size of the tuple, including length field.
size_t getLength() const
Returns the length of the data in the tuple.
void setTuple(const size_t at, const OpaqueDataTuple &tuple)
Replaces tuple at the specified index with a new tuple.
virtual std::string toText(int indent=0) const
Returns text representation of the option.
size_t getTuplesNum() const
Returns the number of opaque data tuples added to the option.
OptionOpaqueDataTuples(Option::Universe u, const uint16_t type)
Constructor.
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses buffer holding an option.
void addTuple(const OpaqueDataTuple &tuple)
Adds a new opaque data tuple to the option.
virtual uint16_t len() const
Returns the full length of the option, including option header.
OptionPtr clone() const
Copies this option and returns a pointer to the copy.
OpaqueDataTuple getTuple(const size_t at) const
Returns opaque data tuple at the specified position.
bool hasTuple(const std::string &tuple_str) const
Checks if the object holds the opaque data tuple with the specified string.
virtual void pack(isc::util::OutputBuffer &buf) const
Renders option into the buffer in the wire format.
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:338
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:82
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:288
void packHeader(isc::util::OutputBuffer &buf) const
Store option's header in a buffer.
Definition: option.cc:126
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.
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
Defines the logger used by the top-level component of kea-lfc.