Kea  1.9.9-git
option_string.cc
Go to the documentation of this file.
1 // Copyright (C) 2013-2019 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/option_string.h>
10 #include <util/strutil.h>
11 #include <sstream>
12 
13 namespace isc {
14 namespace dhcp {
15 
16 OptionString::OptionString(const Option::Universe u, const uint16_t type,
17  const std::string& value)
18  : Option(u, type) {
19  // Try to assign the provided string value. This will throw exception
20  // if the provided value is empty.
21  setValue(value);
22 }
23 
24 OptionString::OptionString(const Option::Universe u, const uint16_t type,
27  : Option(u, type) {
28  // Decode the data. This will throw exception if the buffer is
29  // truncated.
30  unpack(begin, end);
31 }
32 
35  return (cloneInternal<OptionString>());
36 }
37 
38 std::string
40  const OptionBuffer& data = getData();
41  return (std::string(data.begin(), data.end()));
42 }
43 
44 void
45 OptionString::setValue(const std::string& value) {
46  // Sanity check that the string value is at least one byte long.
47  // This is a requirement for all currently defined options which
48  // carry a string value.
49  if (value.empty()) {
50  isc_throw(isc::OutOfRange, "string value carried by the option '"
51  << getType() << "' must not be empty");
52  }
53 
54  // Trim off any trailing nuls.
55  auto begin = value.begin();
56  auto end = util::str::seekTrimmed(begin, value.end(), 0x0);
57 
58  if (std::distance(begin, end) == 0) {
59  isc_throw(isc::OutOfRange, "string value carried by the option '"
60  << getType() << "' contained only nuls");
61  }
62 
63  // Now set the value.
64  setData(begin, end);
65 }
66 
67 
68 uint16_t
70  return (getHeaderLen() + getData().size());
71 }
72 
73 void
75  // Pack option header.
76  packHeader(buf);
77  // Pack data.
78  const OptionBuffer& data = getData();
79  buf.writeData(&data[0], data.size());
80 
81  // That's it. We don't pack any sub-options here, because this option
82  // must not contain sub-options.
83 }
84 
85 void
88  // Trim off trailing nul(s)
89  end = util::str::seekTrimmed(begin, end, 0x0);
90  if (std::distance(begin, end) == 0) {
91  isc_throw(isc::dhcp::SkipThisOptionError, "failed to parse an option '"
92  << getType() << "' holding string value"
93  << "' was empty or contained only nuls");
94  }
95 
96  // Now set the data.
97  setData(begin, end);
98 }
99 
100 std::string
101 OptionString::toText(int indent) const {
102  std::ostringstream output;
103  output << headerToText(indent) << ": "
104  << "\"" << getValue() << "\" (string)";
105 
106  return (output.str());
107 }
108 
109 std::string
111  return (getValue());
112 }
113 
114 } // end of isc::dhcp namespace
115 } // end of isc namespace
void packHeader(isc::util::OutputBuffer &buf) const
Store option's header in a buffer.
Definition: option.cc:126
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Decodes option data from the provided buffer.
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:82
OptionPtr clone() const
Copies this option and returns a pointer to the copy.
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition: option.h:310
virtual void pack(isc::util::OutputBuffer &buf) const
Creates on-wire format of the option.
OptionString(const Option::Universe u, const uint16_t type, const std::string &value)
Constructor, used to create options to be sent.
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:338
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition: buffer.h:550
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
virtual std::string toText(int indent=0) const
Returns option information in the textual format.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
virtual std::string toString() const
Returns actual value of the option in string format.
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
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:304
std::string getValue() const
Returns the string value held by the option.
virtual uint16_t len() const
Returns length of the whole option, including header.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Exception thrown during option unpacking This exception is thrown when an error has occurred unpackin...
Definition: option.h:66
Iterator seekTrimmed(Iterator begin, Iterator end, uint8_t trim_val)
Finds the "trimmed" end of a buffer.
Definition: strutil.h:72
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition: option.h:408
void setValue(const std::string &value)
Sets the string value to be held by the option.