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