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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright (C) 2011-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/libdhcp++.h>
#include <dhcp/option.h>
#include <dhcp/option_space.h>
#include <exceptions/exceptions.h>
#include <util/encode/encode.h>
#include <util/io.h>

#include <boost/make_shared.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <iomanip><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <list><--- 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.

#include <arpa/inet.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stdint.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc::util;

namespace isc {
namespace dhcp {

OptionPtr
Option::factory(Option::Universe u,
        uint16_t type,
        const OptionBuffer& buf) {
    return (LibDHCP::optionFactory(u, type, buf));
}

Option::Option(Universe u, uint16_t type)
    : universe_(u), type_(type) {
    check();
}

Option::Option(Universe u, uint16_t type, const OptionBuffer& data)
    : universe_(u), type_(type), data_(data) {
    check();
}

Option::Option(Universe u, uint16_t type, OptionBufferConstIter first,
               OptionBufferConstIter last)
    : universe_(u), type_(type), data_(first, last) {
    check();
}

Option::Option(const Option& option)
    : universe_(option.universe_), type_(option.type_),
      data_(option.data_), options_(),
      encapsulated_space_(option.encapsulated_space_) {
    option.getOptionsCopy(options_);
}

OptionPtr
Option::create(Universe u, uint16_t type) {
    return (boost::make_shared<Option>(u, type));
}

OptionPtr
Option::create(Universe u, uint16_t type, const OptionBuffer& data) {
    return (boost::make_shared<Option>(u, type, data));
}

Option&
Option::operator=(const Option& rhs) {
    if (&rhs != this) {
        universe_ = rhs.universe_;
        type_ = rhs.type_;
        data_ = rhs.data_;
        rhs.getOptionsCopy(options_);
        encapsulated_space_ = rhs.encapsulated_space_;
    }
    return (*this);
}

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

void
Option::check() const {
    if ((universe_ != V4) && (universe_ != V6)) {
        isc_throw(BadValue, "Invalid universe type specified. "
                  << "Only V4 and V6 are allowed.");
    }

    if (universe_ == V4) {
        if (type_ > 255) {
            isc_throw(OutOfRange, "DHCPv4 Option type " << type_ << " is too big. "
                      << "For DHCPv4 allowed type range is 0..255");
        }
    }

    // no need to check anything for DHCPv6. It allows full range (0-64k) of
    // both types and data size.
}

void Option::pack(isc::util::OutputBuffer& buf, bool check) const {
    // Write a header.
    packHeader(buf, check);
    // Write data.
    if (!data_.empty()) {
        buf.writeData(&data_[0], data_.size());
    }
    // Write sub-options.
    packOptions(buf, check);
}

void
Option::packHeader(isc::util::OutputBuffer& buf, bool check) const {
    if (universe_ == V4) {
        if (check && len() > 255) {
            isc_throw(OutOfRange, "DHCPv4 Option " << type_ << " is too big. "
                      << "At most 255 bytes are supported.");
        }

        buf.writeUint8(type_);
        buf.writeUint8(len() - getHeaderLen());

    } else {
        buf.writeUint16(type_);
        buf.writeUint16(len() - getHeaderLen());
    }
}

void
Option::packOptions(isc::util::OutputBuffer& buf, bool check) const {
    switch (universe_) {
    case V4:
        LibDHCP::packOptions4(buf, options_, false, check);
        return;
    case V6:
        LibDHCP::packOptions6(buf, options_);
        return;
    default:
        isc_throw(isc::BadValue, "Invalid universe type " << universe_);
    }
}

void Option::unpack(OptionBufferConstIter begin,
                    OptionBufferConstIter end) {
    setData(begin, end);
}

void
Option::unpackOptions(const OptionBuffer& buf) {
    list<uint16_t> deferred;
    switch (universe_) {
    case V4:
        LibDHCP::unpackOptions4(buf, getEncapsulatedSpace(),
                                options_, deferred,
                                getType() == DHO_VENDOR_ENCAPSULATED_OPTIONS);
        return;
    case V6:
        LibDHCP::unpackOptions6(buf, getEncapsulatedSpace(), options_);
        return;
    default:
        isc_throw(isc::BadValue, "Invalid universe type " << universe_);
    }
}

uint16_t Option::len() const {
    // Returns length of the complete option (data length + DHCPv4/DHCPv6
    // option header)

    // length of the whole option is header and data stored in this option...
    size_t length = getHeaderLen() + data_.size();

    // ... and sum of lengths of all suboptions
    for (auto const& option : options_) {
        length += option.second->len();<--- Consider using std::accumulate algorithm instead of a raw loop.
    }

    // note that this is not equal to length field. This value denotes
    // number of bytes required to store this option. length option should
    // contain (len()-getHeaderLen()) value.
    return (static_cast<uint16_t>(length));
}

bool
Option::valid() const {
    if (universe_ != V4 &&
        universe_ != V6) {
        return (false);
    }

    return (true);
}

OptionPtr Option::getOption(uint16_t opt_type) const {
    auto const& x = options_.find(opt_type);
    if (x != options_.end()) {
        return (x->second);
    }
    return OptionPtr(); // NULL
}

void
Option::getOptionsCopy(OptionCollection& options_copy) const {
    OptionCollection local_options;
    for (auto const& option : options_) {
        OptionPtr copy = option.second->clone();
        local_options.insert(std::make_pair(option.second->getType(), copy));
    }
    // All options copied successfully, so assign them to the output
    // parameter.
    options_copy.swap(local_options);
}

bool Option::delOption(uint16_t opt_type) {
    auto const& x = options_.find(opt_type);
    if (x != options_.end()) {
        options_.erase(x);
        return (true); // delete successful
    }
    return (false); // option not found, can't delete
}

std::string Option::toText(int indent) const {
    std::stringstream output;
    output << headerToText(indent) << ": ";

    for (unsigned int i = 0; i < data_.size(); i++) {
        if (i) {
            output << ":";
        }
        output << setfill('0') << setw(2) << hex
               << static_cast<unsigned short>(data_[i]);
    }

    // Append suboptions.
    output << suboptionsToText(indent + 2);

    return (output.str());
}

std::string
Option::toString() const {
    /// @todo: Implement actual conversion in derived classes.
    return (toText(0));
}

std::vector<uint8_t>
Option::toBinary(const bool include_header) const {
    OutputBuffer buf(len());
    try {
        // The RFC3396 adds support for long options split over multiple options
        // using the same code.
        pack(buf, false);

    } catch (const std::exception &ex) {
        isc_throw(OutOfRange, "unable to obtain hexadecimal representation"
                  " of option " << getType() << ": " << ex.what());
    }
    const uint8_t* option_data = static_cast<const uint8_t*>(buf.getData());

    // Assign option data to a vector, with or without option header depending
    // on the value of "include_header" flag.
    std::vector<uint8_t> option_vec(option_data + (include_header ? 0 : getHeaderLen()),
                                    option_data + buf.getLength());
    return (option_vec);
}

std::string
Option::toHexString(const bool include_header) const {
    // Prepare binary version of the option.
    std::vector<uint8_t> option_vec = toBinary(include_header);

    // Return hexadecimal representation prepended with 0x or empty string
    // if option has no payload and the header fields are excluded.
    std::ostringstream s;
    if (!option_vec.empty()) {
        s << "0x" << encode::encodeHex(option_vec);
    }
    return (s.str());
}

std::string
Option::headerToText(const int indent, const std::string& type_name) const {
    std::stringstream output;
    for (int i = 0; i < indent; i++)
        output << " ";

    int field_len = (getUniverse() == V4 ? 3 : 5);
    output << "type=" << std::setw(field_len) << std::setfill('0')
           << type_;

    if (!type_name.empty()) {
        output << "(" << type_name << ")";
    }

    output << ", len=" << std::setw(field_len) << std::setfill('0')
           << len() - getHeaderLen();
    return (output.str());
}

std::string
Option::suboptionsToText(const int indent) const {
    std::stringstream output;

    if (!options_.empty()) {
        output << "," << std::endl << "options:";
        for (auto const& opt : options_) {
            output << std::endl << opt.second->toText(indent);
        }
    }

    return (output.str());
}

uint16_t
Option::getHeaderLen() const {
    switch (universe_) {
    case V4:
        return OPTION4_HDR_LEN; // header length for v4
    case V6:
        return OPTION6_HDR_LEN; // header length for v6
    }
    return 0; // should not happen
}

void Option::addOption(OptionPtr opt) {
    if (this == opt.get()) {
        // Do not allow options to be added to themselves as this
        // can lead to infinite recursion.
        isc_throw(InvalidOperation, "option cannot be added to itself: " << toText());
    }

    options_.insert(make_pair(opt->getType(), opt));
}

uint8_t Option::getUint8() const {
    if (data_.size() < sizeof(uint8_t) ) {
        isc_throw(OutOfRange, "Attempt to read uint8 from option " << type_
                  << " that has size " << data_.size());
    }
    return (data_[0]);
}

uint16_t Option::getUint16() const {
    // readUint16() checks and throws OutOfRange if data_ is too small.
    return (readUint16(&data_[0], data_.size()));
}

uint32_t Option::getUint32() const {
    // readUint32() checks and throws OutOfRange if data_ is too small.
    return (readUint32(&data_[0], data_.size()));
}

void Option::setUint8(uint8_t value) {
    data_.resize(sizeof(value));
    data_[0] = value;
}

void Option::setUint16(uint16_t value) {
    data_.resize(sizeof(value));
    writeUint16(value, &data_[0], data_.size());
}

void Option::setUint32(uint32_t value) {
    data_.resize(sizeof(value));
    writeUint32(value, &data_[0], data_.size());
}

bool Option::equals(const OptionPtr& other) const {
    return (equals(*other));
}

bool Option::equals(const Option& other) const {
    return ((getType() == other.getType()) &&
            (getData() == other.getData()));
}

Option::~Option() {
}

bool Option::lenient_parsing_;

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