Kea 2.7.5
option_string.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2024 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 <util/str.h>
11#include <sstream>
12
13namespace isc {
14namespace dhcp {
15
16OptionString::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
24OptionString::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
38std::string
40 const OptionBuffer& data = getData();
41 return (std::string(data.begin(), data.end()));
42}
43
44void
45OptionString::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
68uint16_t
70 return (getHeaderLen() + getData().size());
71}
72
73void
75 // Pack option header.
76 packHeader(buf, check);
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
85void
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
100std::string
101OptionString::toText(int indent) const {
102 std::ostringstream output;
103 output << headerToText(indent) << ": "
104 << "\"" << getValue() << "\" (string)";
105
106 return (output.str());
107}
108
109std::string
111 return (getValue());
112}
113
114} // end of isc::dhcp namespace
115} // 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...
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 void pack(isc::util::OutputBuffer &buf, bool check=true) const
Creates on-wire format of the option.
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:324
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition option.cc:327
Universe
defines option universe DHCPv4 or DHCPv6
Definition option.h:90
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition option.h:300
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition option.h:434
void packHeader(isc::util::OutputBuffer &buf, bool check=true) const
Store option's header in a buffer.
Definition option.cc:119
void check() const
A protected method used for option correctness.
Definition option.cc:90
Exception thrown during option unpacking This exception is thrown when an error has occurred unpackin...
Definition option.h:67
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:343
#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
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition option.h:24
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
Iterator seekTrimmed(Iterator const &begin, Iterator end, uint8_t const trim_val)
Finds the "trimmed" end of a buffer.
Definition str.h:65
Defines the logger used by the top-level component of kea-lfc.