1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (C) 2015-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <dhcp/dhcp4.h>
#include <dhcp/dhcp6.h>
#include <dhcp/option6_status_code.h>
#include <dhcp/option_data_types.h>
#include <iterator><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc;
using namespace isc::dhcp;

namespace {

/// @brief Minimum length of the option (when status message is empty).
const size_t OPTION6_STATUS_CODE_MIN_LEN = sizeof(uint16_t);
const size_t OPTION4_SLP_SERVICE_SCOPEMIN_LEN = sizeof(uint8_t);

}; // end of anonymous namespace

namespace isc {
namespace dhcp {

Option6StatusCode::Option6StatusCode(const uint16_t status_code,
                                     const std::string& status_message)
    : Option(Option::V6, D6O_STATUS_CODE),
      status_code_(status_code), status_message_(status_message) {
}

Option6StatusCode::Option6StatusCode(OptionBufferConstIter begin,
                                     OptionBufferConstIter end)
    : Option(Option::V6, D6O_STATUS_CODE),
      status_code_(STATUS_Success), status_message_() {

    // Parse data
    unpack(begin, end);
}

OptionPtr
Option6StatusCode::clone() const {
    return (cloneInternal<Option6StatusCode>());
}

void
Option6StatusCode::pack(isc::util::OutputBuffer& buf, bool) const {
    // Pack option header.
    packHeader(buf);
    // Write numeric status code.
    buf.writeUint16(getStatusCode());
    // If there is any status message, write it.
    if (!status_message_.empty()) {
        buf.writeData(&status_message_[0], status_message_.size());
    }

    // Status code has no options, so leave here.
}

void
Option6StatusCode::unpack(OptionBufferConstIter begin, OptionBufferConstIter end) {
    // Make sure that the option is not truncated.
    if (std::distance(begin, end) < OPTION6_STATUS_CODE_MIN_LEN) {
        isc_throw(OutOfRange, "Status Code option ("
                  << D6O_STATUS_CODE << ") truncated");
    }

    status_code_ = util::readUint16(&(*begin), std::distance(begin, end));
    begin += sizeof(uint16_t);

    status_message_.assign(begin, end);
}

uint16_t
Option6StatusCode::len() const {
    return (getHeaderLen() + sizeof(uint16_t) + status_message_.size());
}

std::string
Option6StatusCode::toText(int indent) const {
    std::ostringstream output;
    output << headerToText(indent) << ": " << dataToText();

    return (output.str());
}

std::string
Option6StatusCode::dataToText() const {
    std::ostringstream output;
    // Add status code name and numeric status code.
    output << getStatusCodeName() << "(" << getStatusCode() << ") ";

    // Include status message in quotes if status code is
    // non-empty.
    if (!status_message_.empty()) {
        output << "\"" << status_message_ << "\"";

    } else {
        output << "(no status message)";
    }

    return (output.str());
}

std::string
Option6StatusCode::getStatusCodeName() const {
    switch (getStatusCode()) {
    case STATUS_Success:
        return ("Success");
    case STATUS_UnspecFail:
        return ("UnspecFail");
    case STATUS_NoAddrsAvail:
        return ("NoAddrsAvail");
    case STATUS_NoBinding:
        return ("NoBinding");
    case STATUS_NotOnLink:
        return ("NotOnLink");
    case STATUS_UseMulticast:
        return ("UseMulticast");
    case STATUS_NoPrefixAvail:
        return ("NoPrefixAvail");
    case STATUS_UnknownQueryType:
        return ("UnknownQueryType");
    case STATUS_MalformedQuery:
        return ("MalformedQuery");
    case STATUS_NotConfigured:
        return ("NotConfigured");
    case STATUS_NotAllowed:
        return ("NotAllowed");
    default:
        ;
    }
    return ("(unknown status code)");
}

Option4SlpServiceScope::Option4SlpServiceScope(const bool mandatory_flag,
                                               const std::string& scope_list)
    : Option(Option::V4, DHO_SERVICE_SCOPE),
      mandatory_flag_(mandatory_flag), scope_list_(scope_list) {
}

Option4SlpServiceScope::Option4SlpServiceScope(OptionBufferConstIter begin,
                                               OptionBufferConstIter end)
    : Option(Option::V4, DHO_SERVICE_SCOPE),
      mandatory_flag_(false), scope_list_() {

    // Parse data
    unpack(begin, end);
}

OptionPtr
Option4SlpServiceScope::clone() const {
    return (cloneInternal<Option4SlpServiceScope>());
}

void
Option4SlpServiceScope::pack(isc::util::OutputBuffer& buf, bool check) const {
    // Pack option header.
    packHeader(buf, check);
    // Write mandatory flag.
    buf.writeUint8(static_cast<uint8_t>(getMandatoryFlag() ? 1 : 0));
    // If there is any scope list, write it.
    if (!scope_list_.empty()) {
        buf.writeData(&scope_list_[0], scope_list_.size());
    }

    // SLP service scope has no options, so leave here.
}

void
Option4SlpServiceScope::unpack(OptionBufferConstIter begin, OptionBufferConstIter end) {
    // Make sure that the option is not truncated.
    if (std::distance(begin, end) < OPTION4_SLP_SERVICE_SCOPEMIN_LEN) {
        isc_throw(OutOfRange, "SLP Service Scope option ("
                  << DHO_SERVICE_SCOPE << ") truncated");
    }

    if (*begin == 1) {
        mandatory_flag_ = true;
    } else if (*begin == 0) {
        mandatory_flag_ = false;
    } else {
        isc_throw(BadDataTypeCast, "unable to read the buffer as boolean"
                  << " value. Invalid value " << static_cast<int>(*begin));
    }
    begin += sizeof(uint8_t);

    scope_list_.assign(begin, end);
}

uint16_t
Option4SlpServiceScope::len() const {
    return (getHeaderLen() + sizeof(uint8_t) + scope_list_.size());
}

std::string
Option4SlpServiceScope::toText(int indent) const {
    std::ostringstream output;
    output << headerToText(indent) << ": " << dataToText();

    return (output.str());
}

std::string
Option4SlpServiceScope::dataToText() const {
    std::ostringstream output;
    output << "mandatory:" << getMandatoryFlag();
    output << ", scope-list:\"" << scope_list_ << "\"";
    return (output.str());
}

} // end of namespace isc::dhcp
} // end of namespace isc