Kea 3.3.4
option_string.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2026 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
12#include <sstream>
13
14using namespace isc::util;
15
16namespace isc {
17namespace dhcp {
18
19OptionString::OptionString(const Option::Universe u, const uint16_t type,
20 const std::string& value)
21 : Option(u, type) {
22 // Try to assign the provided string value. This will throw exception
23 // if the provided value is empty.
24 setValue(value);
25}
26
27OptionString::OptionString(const Option::Universe u, const uint16_t type,
30 : Option(u, type) {
31 // Decode the data. This will throw exception if the buffer is
32 // truncated.
33 unpack(begin, end);
34}
35
40
41std::string
43 const OptionBuffer& data = getData();
44 return (std::string(data.begin(), data.end()));
45}
46
47void
48OptionString::setValue(const std::string& value) {
49 // Sanity check that the string value is at least one byte long.
50 // This is a requirement for all currently defined options which
51 // carry a string value.
52 if (value.empty()) {
53 isc_throw(isc::OutOfRange, "string value carried by the option '"
54 << getType() << "' must not be empty");
55 }
56
57 // Trim off any trailing nuls.
58 auto begin = value.begin();
59 auto end = util::str::seekTrimmed(begin, value.end(), 0x0);
60
61 if (std::distance(begin, end) == 0) {
62 isc_throw(isc::OutOfRange, "string value carried by the option '"
63 << getType() << "' contained only nuls");
64 }
65
66 // Now set the value.
67 setData(begin, end);
68}
69
70
71uint16_t
73 return (getHeaderLen() + getData().size());
74}
75
76void
78 // Pack option header.
79 packHeader(buf, check);
80 // Pack data.
81 const OptionBuffer& data = getData();
82 if (!data.empty()) {
83 buf.writeData(&data[0], data.size());
84 }
85
86 // That's it. We don't pack any sub-options here, because this option
87 // must not contain sub-options.
88}
89
90void
93 // Trim off trailing nul(s)
94 end = util::str::seekTrimmed(begin, end, 0x0);
95 if (std::distance(begin, end) == 0) {
96 isc_throw(isc::dhcp::SkipThisOptionError, "failed to parse an option '"
97 << getType() << "' holding string value"
98 << "' was empty or contained only nuls");
99 }
100
101 // Now set the data.
102 setData(begin, end);
103}
104
105std::string
106OptionString::toText(int indent) const {
107 std::ostringstream output;
108 auto v = getValue();
109
110 output << headerToText(indent) << ": ";
111 if (str::isPrintable(v)) {
112 output << "\"" << v << "\"";
113 } else {
114 output << str::dumpAsHex(getData());
115 }
116 output << " (string)";
117
118 return (output.str());
119}
120
121std::string
123 return (getValue());
124}
125
126} // end of isc::dhcp namespace
127} // 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:325
Universe
defines option universe DHCPv4 or DHCPv6
Definition option.h:91
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6).
Definition option.cc:327
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6).
Definition option.h:301
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition option.h:435
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition option.h:505
void packHeader(isc::util::OutputBuffer &buf, bool check=true) const
Store option's header in a buffer.
Definition option.cc:119
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
Definition option.cc:39
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:68
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:355
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition buffer.h:568
#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
Iterator seekTrimmed(Iterator const &begin, Iterator end, uint8_t const trim_val)
Finds the "trimmed" end of a buffer.
Definition str.h:65
string dumpAsHex(const uint8_t *data, size_t length)
Dumps a buffer of bytes as a string of hexadecimal digits.
Definition str.cc:330
bool isPrintable(const string &content)
Check if a string is printable.
Definition str.cc:310
Defines the logger used by the top-level component of kea-lfc.