Kea 2.7.6
option6_status_code.cc
Go to the documentation of this file.
1// Copyright (C) 2015-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
9#include <dhcp/dhcp4.h>
10#include <dhcp/dhcp6.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)
33 status_code_(status_code), status_message_(status_message) {
34}
35
39 status_code_(STATUS_Success), status_message_() {
40
41 // Parse data
42 unpack(begin, end);
43}
44
49
50void
52 // Pack option header.
53 packHeader(buf);
54 // Write numeric status code.
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
66 // Make sure that the option is not truncated.
67 if (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
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
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
111 switch (getStatusCode()) {
112 case STATUS_Success:
113 return ("Success");
115 return ("UnspecFail");
117 return ("NoAddrsAvail");
118 case STATUS_NoBinding:
119 return ("NoBinding");
120 case STATUS_NotOnLink:
121 return ("NotOnLink");
123 return ("UseMulticast");
125 return ("NoPrefixAvail");
127 return ("UnknownQueryType");
129 return ("MalformedQuery");
131 return ("NotConfigured");
133 return ("NotAllowed");
134 default:
135 ;
136 }
137 return ("(unknown status code)");
138}
139
141 const std::string& scope_list)
143 mandatory_flag_(mandatory_flag), scope_list_(scope_list) {
144}
145
149 mandatory_flag_(false), scope_list_() {
150
151 // Parse data
152 unpack(begin, end);
153}
154
159
160void
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
176 // Make sure that the option is not truncated.
177 if (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
197 return (getHeaderLen() + sizeof(uint8_t) + scope_list_.size());
198}
199
200std::string
202 std::ostringstream output;
203 output << headerToText(indent) << ": " << dataToText();
204
205 return (output.str());
206}
207
208std::string
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
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:294
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition option.cc:327
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition option.h:504
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:343
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition buffer.h:473
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:498
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition buffer.h:556
@ 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(void const *const buffer, size_t const length)
uint16_t wrapper over readUint.
Definition io.h:76
Defines the logger used by the top-level component of kea-lfc.