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
10#include <sstream>
11
12namespace isc {
13namespace dhcp {
14
15OptionString::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
23OptionString::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
37std::string
39 const OptionBuffer& data = getData();
40 return (std::string(data.begin(), data.end()));
41}
42
43void
44OptionString::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
57uint16_t
59 return (getHeaderLen() + getData().size());
60}
61
62void
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
74void
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
85std::string
86OptionString::toText(int indent) const {
87 std::ostringstream output;
88 output << headerToText(indent) << ": "
89 << "\"" << getValue() << "\" (string)";
90
91 return (output.str());
92}
93
94std::string
96 return (getValue());
97}
98
99} // end of isc::dhcp namespace
100} // end of isc namespace
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
virtual void pack(isc::util::OutputBuffer &buf) const
Creates on-wire format of the option.
void setValue(const std::string &value)
Sets the string value to be held by the option.
OptionPtr clone() const
Copies this option and returns a pointer to the copy.
virtual uint16_t len() const
Returns length of the whole option, including header.
virtual std::string toString() const
Returns actual value of the option in string format.
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Decodes option data from the provided buffer.
std::string getValue() const
Returns the string value held by the option.
virtual std::string toText(int indent=0) const
Returns option information in the textual format.
OptionString(const Option::Universe u, const uint16_t type, const std::string &value)
Constructor, used to create options to be sent.
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:294
virtual const OptionBuffer & getData() const
Returns pointer to actual data.
Definition: option.h:268
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:328
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:67
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:246
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition: option.h:366
void packHeader(isc::util::OutputBuffer &buf) const
Store option's header in a buffer.
Definition: option.cc:117
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition: buffer.h:547
#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:31
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:25
boost::shared_ptr< Option > OptionPtr
Definition: option.h:38
Defines the logger used by the top-level component of kea-dhcp-ddns.