Kea 2.5.5
option_vendor.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2023 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 <sstream>
13
14using namespace isc::dhcp;
15
16OptionVendor::OptionVendor(Option::Universe u, const uint32_t vendor_id)
17 : Option(u, u == Option::V4 ?
18 static_cast<uint16_t>(DHO_VIVSO_SUBOPTIONS) :
19 static_cast<uint16_t>(D6O_VENDOR_OPTS)), vendor_id_(vendor_id) {
20}
21
24 : Option(u, u == Option::V4?
25 static_cast<uint16_t>(DHO_VIVSO_SUBOPTIONS) :
26 static_cast<uint16_t>(D6O_VENDOR_OPTS)), vendor_id_(0) {
27 unpack(begin, end);
28}
29
32 return (cloneInternal<OptionVendor>());
33}
34
35void OptionVendor::pack(isc::util::OutputBuffer& buf, bool check) const {
36 packHeader(buf, check);
37
38 // Store vendor-id
39 buf.writeUint32(vendor_id_);
40
41 // The format is slightly different for v4
42 if (universe_ == Option::V4) {
43 // Calculate and store data-len as follows:
44 // data-len = total option length - header length
45 // - enterprise id field length - data-len field size
46 // length of all suboptions
47 uint8_t length = 0;
48 for (auto const& opt : options_) {
49 length += opt.second->len();
50 }
51 buf.writeUint8(length);
52 }
53
54 packOptions(buf, check);
55}
56
59 // We throw SkipRemainingOptionsError so callers can
60 // abandon further unpacking, if desired.
61 if (distance(begin, end) < sizeof(uint32_t)) {
63 "Truncated vendor-specific information option"
64 << ", length=" << distance(begin, end));
65 }
66
67 vendor_id_ = isc::util::readUint32(&(*begin), distance(begin, end));
68
69 OptionBuffer vendor_buffer(begin + 4, end);
70
71 if (universe_ == Option::V6) {
72 LibDHCP::unpackVendorOptions6(vendor_id_, vendor_buffer, options_);
73 } else {
74 LibDHCP::unpackVendorOptions4(vendor_id_, vendor_buffer, options_);
75 }
76}
77
78uint16_t OptionVendor::len() const {
79 uint16_t length = getHeaderLen();
80
81 length += sizeof(uint32_t); // Vendor-id field
82
83 // Data-len field exists in DHCPv4 vendor options only
84 if (universe_ == Option::V4) {
85 length += sizeof(uint8_t); // data-len
86 }
87
88 // length of all suboptions
89 for (auto const& opt : options_) {
90 length += opt.second->len();
91 }
92 return (length);
93}
94
95std::string
96OptionVendor::toText(int indent) const {
97 std::stringstream output;
98 output << headerToText(indent) << ": ";
99
100 output << vendor_id_ << " (uint32)";
101
102 // For the DHCPv4 there is one more field.
103 if (getUniverse() == Option::V4) {
104 uint32_t length = 0;
105 for (auto const& opt : options_) {
106 length += opt.second->len();
107 }
108 output << " " << length << " (uint8)";
109 }
110
111 // Append suboptions.
112 output << suboptionsToText(indent + 2);
113
114 return (output.str());
115}
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:808
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:904
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:294
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition: buffer.h:466
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:520
@ 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:190
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(const void *buffer, size_t length)
Read Unsigned 32-Bit Integer from Buffer.
Definition: io_utilities.h:79