Kea 2.6.0
option_vendor.cc
Go to the documentation of this file.
1// Copyright (C) 2013-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>
11#include <dhcp/option_vendor.h>
12#include <util/io.h>
13
14#include <sstream>
15
16using namespace isc::dhcp;
17
18OptionVendor::OptionVendor(Option::Universe u, const uint32_t vendor_id)
19 : Option(u, u == Option::V4 ?
20 static_cast<uint16_t>(DHO_VIVSO_SUBOPTIONS) :
21 static_cast<uint16_t>(D6O_VENDOR_OPTS)), vendor_id_(vendor_id) {
22}
23
26 : Option(u, u == Option::V4?
27 static_cast<uint16_t>(DHO_VIVSO_SUBOPTIONS) :
28 static_cast<uint16_t>(D6O_VENDOR_OPTS)), vendor_id_(0) {
29 unpack(begin, end);
30}
31
34 return (cloneInternal<OptionVendor>());
35}
36
37void OptionVendor::pack(isc::util::OutputBuffer& buf, bool check) const {
38 packHeader(buf, check);
39
40 // Store vendor-id
41 buf.writeUint32(vendor_id_);
42
43 // The format is slightly different for v4
44 if (universe_ == Option::V4) {
45 // Calculate and store data-len as follows:
46 // data-len = total option length - header length
47 // - enterprise id field length - data-len field size
48 // length of all suboptions
49 uint8_t length = 0;
50 for (auto const& opt : options_) {
51 length += opt.second->len();
52 }
53 buf.writeUint8(length);
54 }
55
56 packOptions(buf, check);
57}
58
61 // We throw SkipRemainingOptionsError so callers can
62 // abandon further unpacking, if desired.
63 if (distance(begin, end) < sizeof(uint32_t)) {
65 "Truncated vendor-specific information option"
66 << ", length=" << distance(begin, end));
67 }
68
69 vendor_id_ = isc::util::readUint32(&(*begin), distance(begin, end));
70
71 OptionBuffer vendor_buffer(begin + 4, end);
72
73 if (universe_ == Option::V6) {
74 LibDHCP::unpackVendorOptions6(vendor_id_, vendor_buffer, options_);
75 } else {
76 LibDHCP::unpackVendorOptions4(vendor_id_, vendor_buffer, options_);
77 }
78}
79
80uint16_t OptionVendor::len() const {
81 uint16_t length = getHeaderLen();
82
83 length += sizeof(uint32_t); // Vendor-id field
84
85 // Data-len field exists in DHCPv4 vendor options only
86 if (universe_ == Option::V4) {
87 length += sizeof(uint8_t); // data-len
88 }
89
90 // length of all suboptions
91 for (auto const& opt : options_) {
92 length += opt.second->len();
93 }
94 return (length);
95}
96
97std::string
98OptionVendor::toText(int indent) const {
99 std::stringstream output;
100 output << headerToText(indent) << ": ";
101
102 output << vendor_id_ << " (uint32)";
103
104 // For the DHCPv4 there is one more field.
105 if (getUniverse() == Option::V4) {
106 uint32_t length = 0;
107 for (auto const& opt : options_) {
108 length += opt.second->len();
109 }
110 output << " " << length << " (uint8)";
111 }
112
113 // Append suboptions.
114 output << suboptionsToText(indent + 2);
115
116 return (output.str());
117}
static size_t unpackVendorOptions6(const uint32_t vendor_id, const OptionBuffer &buf, isc::dhcp::OptionCollection &options)
Parses provided buffer as DHCPv6 vendor options and creates Option objects.
Definition: libdhcp++.cc:810
static size_t unpackVendorOptions4(const uint32_t vendor_id, const OptionBuffer &buf, isc::dhcp::OptionCollection &options)
Parses provided buffer as DHCPv4 vendor options and creates Option objects.
Definition: libdhcp++.cc:906
OptionVendor(Option::Universe u, const uint32_t vendor_id)
Constructor.
virtual uint16_t len() const
returns complete length of option
OptionPtr clone() const
Copies this option and returns a pointer to the copy.
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
virtual std::string toText(int indent=0) const
Returns the option in the textual format.
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in wire-format to buf, returns pointer to first unused byte after stored option.
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:288
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition: option.cc:307
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:321
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:83
Universe universe_
option universe (V4 or V6)
Definition: option.h:587
void packOptions(isc::util::OutputBuffer &buf, bool check=true) const
Store sub options in a buffer.
Definition: option.cc:136
OptionCollection options_
collection for storing suboptions
Definition: option.h:596
Universe getUniverse() const
returns option universe (V4 or V6)
Definition: option.h:233
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
Exception thrown during option unpacking This exception is thrown when an error has occurred,...
Definition: option.h:52
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 writeUint32(uint32_t data)
Write an unsigned 32-bit integer in host byte order into the buffer in network byte order.
Definition: buffer.h:528
@ D6O_VENDOR_OPTS
Definition: dhcp6.h:37
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
@ DHO_VIVSO_SUBOPTIONS
Definition: dhcp4.h:189
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
uint32_t readUint32(void const *const buffer, size_t const length)
uint32_t wrapper over readUint.
Definition: io.h:82