Kea  1.9.9-git
option6_status_code.cc
Go to the documentation of this file.
1 // Copyright (C) 2015-2017 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>
12 #include <dhcp/option_data_types.h>
13 #include <util/io_utilities.h>
14 #include <iterator>
15 #include <sstream>
16 
17 using namespace isc;
18 using namespace isc::dhcp;
19 
20 namespace {
21 
23 const size_t OPTION6_STATUS_CODE_MIN_LEN = sizeof(uint16_t);
24 const size_t OPTION4_SLP_SERVICE_SCOPEMIN_LEN = sizeof(uint8_t);
25 
26 }; // end of anonymous namespace
27 
28 namespace isc {
29 namespace dhcp {
30 
31 Option6StatusCode::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 
51 void
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 
65 void
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 
79 uint16_t
81  return (getHeaderLen() + sizeof(uint16_t) + status_message_.size());
82 }
83 
84 std::string
85 Option6StatusCode::toText(int indent) const {
86  std::ostringstream output;
87  output << headerToText(indent) << ": " << dataToText();
88 
89  return (output.str());
90 }
91 
92 std::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 
110 std::string
112  switch (getStatusCode()) {
113  case STATUS_Success:
114  return ("Success");
115  case STATUS_UnspecFail:
116  return ("UnspecFail");
117  case STATUS_NoAddrsAvail:
118  return ("NoAddrsAvail");
119  case STATUS_NoBinding:
120  return ("NoBinding");
121  case STATUS_NotOnLink:
122  return ("NotOnLink");
123  case STATUS_UseMulticast:
124  return ("UseMulticast");
126  return ("NoPrefixAvail");
128  return ("UnknownQueryType");
130  return ("MalformedQuery");
132  return ("NotConfigured");
133  case STATUS_NotAllowed:
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 
156 OptionPtr
158  return (cloneInternal<Option4SlpServiceScope>());
159 }
160 
161 void
163  // Pack option header.
164  packHeader(buf);
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 
175 void
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 
196 uint16_t
198  return (getHeaderLen() + sizeof(uint8_t) + scope_list_.size());
199 }
200 
201 std::string
203  std::ostringstream output;
204  output << headerToText(indent) << ": " << dataToText();
205 
206  return (output.str());
207 }
208 
209 std::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
virtual std::string toText(int indent=0) const
Returns textual representation of the option.
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
void packHeader(isc::util::OutputBuffer &buf) const
Store option's header in a buffer.
Definition: option.cc:126
std::string dataToText() const
Returns textual representation of the option data.
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
bool getMandatoryFlag() const
Returns mandatory flag.
virtual void pack(isc::util::OutputBuffer &buf) const
Writes option in wire-format.
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:338
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition: buffer.h:550
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Option4SlpServiceScope(const bool mandatory_flag, const std::string &scope_list)
Constructor, used for options constructed (during transmission).
std::string getStatusCodeName() const
Returns the name of the status code.
virtual uint16_t len() const
Returns total length of the option.
virtual uint16_t len() const
Returns total length of the option.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::string dataToText() const
Returns textual representation of the option data.
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.
Definition: io_utilities.h:28
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:304
virtual void pack(isc::util::OutputBuffer &buf) const
Writes option in wire-format.
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition: buffer.h:466
uint16_t getStatusCode() const
Returns numeric status code.
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
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.
Option6StatusCode(const uint16_t status_code, const std::string &status_message)
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.