Kea  1.5.0
option_string.cc
Go to the documentation of this file.
1 // Copyright (C) 2013-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 
9 #include <dhcp/option_string.h>
10 #include <sstream>
11 
12 namespace isc {
13 namespace dhcp {
14 
15 OptionString::OptionString(const Option::Universe u, const uint16_t type,
16  const std::string& value)
17  : Option(u, type) {
18  // Try to assign the provided string value. This will throw exception
19  // if the provided value is empty.
20  setValue(value);
21 }
22 
23 OptionString::OptionString(const Option::Universe u, const uint16_t type,
26  : Option(u, type) {
27  // Decode the data. This will throw exception if the buffer is
28  // truncated.
29  unpack(begin, end);
30 }
31 
34  return (cloneInternal<OptionString>());
35 }
36 
37 std::string
39  const OptionBuffer& data = getData();
40  return (std::string(data.begin(), data.end()));
41 }
42 
43 void
44 OptionString::setValue(const std::string& value) {
45  // Sanity check that the string value is at least one byte long.
46  // This is a requirement for all currently defined options which
47  // carry a string value.
48  if (value.empty()) {
49  isc_throw(isc::OutOfRange, "string value carried by the option '"
50  << getType() << "' must not be empty");
51  }
52 
53  setData(value.begin(), value.end());
54 }
55 
56 
57 uint16_t
59  return (getHeaderLen() + getData().size());
60 }
61 
62 void
64  // Pack option header.
65  packHeader(buf);
66  // Pack data.
67  const OptionBuffer& data = getData();
68  buf.writeData(&data[0], data.size());
69 
70  // That's it. We don't pack any sub-options here, because this option
71  // must not contain sub-options.
72 }
73 
74 void
77  if (std::distance(begin, end) == 0) {
78  isc_throw(isc::OutOfRange, "failed to parse an option '"
79  << getType() << "' holding string value"
80  << " - empty value is not accepted");
81  }
82  setData(begin, end);
83 }
84 
85 std::string
86 OptionString::toText(int indent) const {
87  std::ostringstream output;
88  output << headerToText(indent) << ": "
89  << "\"" << getValue() << "\" (string)";
90 
91  return (output.str());
92 }
93 
94 std::string
96  return (getValue());
97 }
98 
99 } // end of isc::dhcp namespace
100 } // end of isc namespace
isc::dhcp::OptionBuffer
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:25
isc::dhcp::OptionString::toText
virtual std::string toText(int indent=0) const
Returns option information in the textual format.
Definition: option_string.cc:86
isc::dhcp::OptionString::getValue
std::string getValue() const
Returns the string value held by the option.
Definition: option_string.cc:38
isc::dhcp::OptionString::OptionString
OptionString(const Option::Universe u, const uint16_t type, const std::string &value)
Constructor, used to create options to be sent.
Definition: option_string.cc:15
isc::dhcp::Option::getType
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:246
option_string.h
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::dhcp::OptionString::unpack
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Decodes option data from the provided buffer.
Definition: option_string.cc:75
isc::dhcp::OptionString::pack
virtual void pack(isc::util::OutputBuffer &buf) const
Creates on-wire format of the option.
Definition: option_string.cc:63
isc::dhcp::OptionString::toString
virtual std::string toString() const
Returns actual value of the option in string format.
Definition: option_string.cc:95
isc::dhcp::Option::packHeader
void packHeader(isc::util::OutputBuffer &buf) const
Store option's header in a buffer.
Definition: option.cc:117
isc::util::OutputBuffer
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
isc::dhcp::OptionString::setValue
void setValue(const std::string &value)
Sets the string value to be held by the option.
Definition: option_string.cc:44
isc::dhcp::Option::setData
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition: option.h:366
isc::OutOfRange
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Definition: exceptions/exceptions.h:115
isc::dhcp::OptionPtr
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
isc::dhcp::Option::Universe
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:67
isc::dhcp::OptionString::clone
OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option_string.cc:33
isc::dhcp::Option::getData
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition: option.h:268
isc::util::OutputBuffer::writeData
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition: buffer.h:547
isc::dhcp::OptionString::len
virtual uint16_t len() const
Returns length of the whole option, including header.
Definition: option_string.cc:58
isc::dhcp::Option
Definition: option.h:58
isc::dhcp::Option::headerToText
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:294
isc::dhcp::Option::getHeaderLen
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:328
isc::dhcp::OptionBufferConstIter
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:31