Kea: option6_status_code.cc Source File

| Kea 3.2.0-git

option6_status_code.cc
Go to the documentation of this file.
1// Copyright (C) 2015-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
9#include <dhcp/dhcp4.h>
10#include <dhcp/dhcp6.h>
11#include <dhcp/option6_status_code.h>
12#include <dhcp/option_data_types.h>
13#include <iterator>
14#include <sstream>
15
16using namespace isc;
17using namespace isc::dhcp;
18
19namespace {
20
22const size_t OPTION6_STATUS_CODE_MIN_LEN = sizeof(uint16_t);
23const size_t OPTION4_SLP_SERVICE_SCOPEMIN_LEN = sizeof(uint8_t);
24
25} // end of anonymous namespace
26
27namespace isc {
28namespace dhcp {
29
30Option6StatusCode::Option6StatusCode(const uint16_t status_code,
31 const std::string& status_message)
32 : Option(Option::V6, D6O_STATUS_CODE),
33 status_code_(status_code), status_message_(status_message) {
34}
35
36 Option6StatusCode::Option6StatusCode(OptionBufferConstIter begin,
37 OptionBufferConstIter end)
38 : Option(Option::V6, D6O_STATUS_CODE),
39 status_code_(STATUS_Success), status_message_() {
40
41 // Parse data
42 Option6StatusCode::unpack(begin, end);
43}
44
45 OptionPtr
46Option6StatusCode::clone() const {
47 return (cloneInternal<Option6StatusCode>());
48}
49
50void
51Option6StatusCode::pack(isc::util::OutputBuffer& buf, bool) const {
52 // Pack option header.
53 packHeader(buf);
54 // Write numeric status code.
55 buf.writeUint16(getStatusCode());
56 // If there is any status message, write it.
57 if (!status_message_.empty()) {
58 buf.writeData(&status_message_[0], status_message_.size());
59 }
60
61 // Status code has no options, so leave here.
62}
63
64void
65Option6StatusCode::unpack(OptionBufferConstIter begin, OptionBufferConstIter end) {
66 // Make sure that the option is not truncated.
67 if (static_cast<size_t>(std::distance(begin, end)) < OPTION6_STATUS_CODE_MIN_LEN) {
68 isc_throw(OutOfRange, "Status Code option ("
69 << D6O_STATUS_CODE << ") truncated");
70 }
71
72 status_code_ = util::readUint16(&(*begin), std::distance(begin, end));
73 begin += sizeof(uint16_t);
74
75 status_message_.assign(begin, end);
76}
77
78uint16_t
79Option6StatusCode::len() const {
80 return (getHeaderLen() + sizeof(uint16_t) + status_message_.size());
81}
82
83std::string
84Option6StatusCode::toText(int indent) const {
85 std::ostringstream output;
86 output << headerToText(indent) << ": " << dataToText();
87
88 return (output.str());
89}
90
91std::string
92Option6StatusCode::dataToText() const {
93 std::ostringstream output;
94 // Add status code name and numeric status code.
95 output << getStatusCodeName() << "(" << getStatusCode() << ") ";
96
97 // Include status message in quotes if status code is
98 // non-empty.
99 if (!status_message_.empty()) {
100 output << "\"" << status_message_ << "\"";
101
102 } else {
103 output << "(no status message)";
104 }
105
106 return (output.str());
107}
108
109std::string
110Option6StatusCode::getStatusCodeName() const {
111 switch (getStatusCode()) {
112 case STATUS_Success:
113 return ("Success");
114 case STATUS_UnspecFail:
115 return ("UnspecFail");
116 case STATUS_NoAddrsAvail:
117 return ("NoAddrsAvail");
118 case STATUS_NoBinding:
119 return ("NoBinding");
120 case STATUS_NotOnLink:
121 return ("NotOnLink");
122 case STATUS_UseMulticast:
123 return ("UseMulticast");
124 case STATUS_NoPrefixAvail:
125 return ("NoPrefixAvail");
126 case STATUS_UnknownQueryType:
127 return ("UnknownQueryType");
128 case STATUS_MalformedQuery:
129 return ("MalformedQuery");
130 case STATUS_NotConfigured:
131 return ("NotConfigured");
132 case STATUS_NotAllowed:
133 return ("NotAllowed");
134 default:
135 ;
136 }
137 return ("(unknown status code)");
138}
139
140Option4SlpServiceScope::Option4SlpServiceScope(const bool mandatory_flag,
141 const std::string& scope_list)
142 : Option(Option::V4, DHO_SERVICE_SCOPE),
143 mandatory_flag_(mandatory_flag), scope_list_(scope_list) {
144}
145
146 Option4SlpServiceScope::Option4SlpServiceScope(OptionBufferConstIter begin,
147 OptionBufferConstIter end)
148 : Option(Option::V4, DHO_SERVICE_SCOPE),
149 mandatory_flag_(false), scope_list_() {
150
151 // Parse data
152 Option4SlpServiceScope::unpack(begin, end);
153}
154
155 OptionPtr
156Option4SlpServiceScope::clone() const {
157 return (cloneInternal<Option4SlpServiceScope>());
158}
159
160void
161Option4SlpServiceScope::pack(isc::util::OutputBuffer& buf, bool check) const {
162 // Pack option header.
163 packHeader(buf, check);
164 // Write mandatory flag.
165 buf.writeUint8(static_cast<uint8_t>(getMandatoryFlag() ? 1 : 0));
166 // If there is any scope list, write it.
167 if (!scope_list_.empty()) {
168 buf.writeData(&scope_list_[0], scope_list_.size());
169 }
170
171 // SLP service scope has no options, so leave here.
172}
173
174void
175Option4SlpServiceScope::unpack(OptionBufferConstIter begin, OptionBufferConstIter end) {
176 // Make sure that the option is not truncated.
177 if (static_cast<size_t>(std::distance(begin, end)) < OPTION4_SLP_SERVICE_SCOPEMIN_LEN) {
178 isc_throw(OutOfRange, "SLP Service Scope option ("
179 << DHO_SERVICE_SCOPE << ") truncated");
180 }
181
182 if (*begin == 1) {
183 mandatory_flag_ = true;
184 } else if (*begin == 0) {
185 mandatory_flag_ = false;
186 } else {
187 isc_throw(BadDataTypeCast, "unable to read the buffer as boolean"
188 << " value. Invalid value " << static_cast<int>(*begin));
189 }
190 begin += sizeof(uint8_t);
191
192 scope_list_.assign(begin, end);
193}
194
195uint16_t
196Option4SlpServiceScope::len() const {
197 return (getHeaderLen() + sizeof(uint8_t) + scope_list_.size());
198}
199
200std::string
201Option4SlpServiceScope::toText(int indent) const {
202 std::ostringstream output;
203 output << headerToText(indent) << ": " << dataToText();
204
205 return (output.str());
206}
207
208std::string
209Option4SlpServiceScope::dataToText() const {
210 std::ostringstream output;
211 output << "mandatory:" << getMandatoryFlag();
212 output << ", scope-list:\"" << scope_list_ << "\"";
213 return (output.str());
214}
215
216} // end of namespace isc::dhcp
217} // end of namespace isc
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::BadDataTypeCast
Exception to be thrown when cast to the data type was unsuccessful.
Definition option_data_types.h:30
isc::dhcp::Option4SlpServiceScope::pack
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in wire-format.
Definition option6_status_code.cc:161
isc::dhcp::Option4SlpServiceScope::len
virtual uint16_t len() const
Returns total length of the option.
Definition option6_status_code.cc:196
isc::dhcp::Option4SlpServiceScope::dataToText
std::string dataToText() const
Returns textual representation of the option data.
Definition option6_status_code.cc:209
isc::dhcp::Option4SlpServiceScope::unpack
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition option6_status_code.cc:175
isc::dhcp::Option4SlpServiceScope::getMandatoryFlag
bool getMandatoryFlag() const
Returns mandatory flag.
Definition option6_status_code.h:173
isc::dhcp::Option4SlpServiceScope::Option4SlpServiceScope
Option4SlpServiceScope(const bool mandatory_flag, const std::string &scope_list)
Constructor, used for options constructed (during transmission).
Definition option6_status_code.cc:140
isc::dhcp::Option4SlpServiceScope::toText
virtual std::string toText(int indent=0) const
Returns textual representation of the option.
Definition option6_status_code.cc:201
isc::dhcp::Option4SlpServiceScope::clone
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition option6_status_code.cc:156
isc::dhcp::Option6StatusCode::clone
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition option6_status_code.cc:46
isc::dhcp::Option6StatusCode::pack
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in wire-format.
Definition option6_status_code.cc:51
isc::dhcp::Option6StatusCode::Option6StatusCode
Option6StatusCode(const uint16_t status_code, const std::string &status_message)
Constructor, used for options constructed (during transmission).
Definition option6_status_code.cc:30
isc::dhcp::Option6StatusCode::len
virtual uint16_t len() const
Returns total length of the option.
Definition option6_status_code.cc:79
isc::dhcp::Option6StatusCode::getStatusCodeName
std::string getStatusCodeName() const
Returns the name of the status code.
Definition option6_status_code.cc:110
isc::dhcp::Option6StatusCode::dataToText
std::string dataToText() const
Returns textual representation of the option data.
Definition option6_status_code.cc:92
isc::dhcp::Option6StatusCode::toText
virtual std::string toText(int indent=0) const
Returns textual representation of the option.
Definition option6_status_code.cc:84
isc::dhcp::Option6StatusCode::unpack
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition option6_status_code.cc:65
isc::dhcp::Option6StatusCode::getStatusCode
uint16_t getStatusCode() const
Returns numeric status code.
Definition option6_status_code.h:77
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:295
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::Option::V6
@ V6
Definition option.h:90
isc::dhcp::Option::V4
@ V4
Definition option.h:90
isc::dhcp::Option::cloneInternal
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition option.h:504
isc::dhcp::Option::packHeader
void packHeader(isc::util::OutputBuffer &buf, bool check=true) const
Store option's header in a buffer.
Definition option.cc:119
isc::dhcp::Option::Option
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
Definition option.cc:39
isc::dhcp::Option::check
void check() const
A protected method used for option correctness.
Definition option.cc:90
isc::util::OutputBuffer
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:346
isc::util::OutputBuffer::writeUint8
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition buffer.h:476
isc::util::OutputBuffer::writeUint16
void writeUint16(uint16_t data)
Write an unsigned 16-bit integer in host byte order into the buffer in network byte order.
Definition buffer.h:501
isc::util::OutputBuffer::writeData
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition buffer.h:559
dhcp4.h
dhcp6.h
STATUS_NoAddrsAvail
@ STATUS_NoAddrsAvail
Definition dhcp6.h:178
STATUS_UseMulticast
@ STATUS_UseMulticast
Definition dhcp6.h:181
STATUS_NotAllowed
@ STATUS_NotAllowed
Definition dhcp6.h:186
STATUS_NoPrefixAvail
@ STATUS_NoPrefixAvail
Definition dhcp6.h:182
STATUS_MalformedQuery
@ STATUS_MalformedQuery
Definition dhcp6.h:184
STATUS_NotOnLink
@ STATUS_NotOnLink
Definition dhcp6.h:180
STATUS_Success
@ STATUS_Success
Definition dhcp6.h:176
STATUS_NoBinding
@ STATUS_NoBinding
Definition dhcp6.h:179
STATUS_UnspecFail
@ STATUS_UnspecFail
Definition dhcp6.h:177
STATUS_UnknownQueryType
@ STATUS_UnknownQueryType
Definition dhcp6.h:183
STATUS_NotConfigured
@ STATUS_NotConfigured
Definition dhcp6.h:185
D6O_STATUS_CODE
@ D6O_STATUS_CODE
Definition dhcp6.h:33
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::DHO_SERVICE_SCOPE
@ DHO_SERVICE_SCOPE
Definition dhcp4.h:148
isc::dhcp::OptionBufferConstIter
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition option.h:30
isc::dhcp::OptionPtr
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
isc::util::readUint16
uint16_t readUint16(void const *const buffer, size_t const length)
uint16_t wrapper over readUint.
Definition io.h:76
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
option6_status_code.h
option_data_types.h