Kea 2.5.5
option6_dnr.cc
Go to the documentation of this file.
1// Copyright (C) 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/option6_dnr.h>
10
11using namespace isc::asiolink;
12
13namespace isc {
14namespace dhcp {
15
17 : Option(V6, D6O_V6_DNR), DnrInstance(V6) {
18 unpack(begin, end);
19}
20
23 return (cloneInternal<Option6Dnr>());
24}
25
26void
27Option6Dnr::pack(util::OutputBuffer& buf, bool check) const {
28 packHeader(buf, check);
29
32 packAdn(buf);
33 if (adn_only_mode_) {
34 return;
35 }
36
38 packAddresses(buf);
39 packSvcParams(buf);
40}
41
42void
44 for (const auto& address : ip_addresses_) {
45 if (!address.isV6()) {
47 << address.toText() << " is not an IPv6 address");
48 }
49
50 buf.writeData(&address.toBytes()[0], V6ADDRESS_LEN);
51 }
52}
53
54void
56 if (std::distance(begin, end) < getMinimalLength()) {
58 << "data truncated to size " << std::distance(begin, end));
59 }
60
61 setData(begin, end);
62
63 // First two octets of Option data is Service Priority - this is mandatory field.
65
66 // Next come two octets of ADN Length plus the ADN data itself (variable length).
67 // This is Opaque Data Tuple so let's use this class to retrieve the ADN data.
68 unpackAdn(begin, end);
69
70 if (begin == end) {
71 // ADN only mode, other fields are not included.
72 return;
73 }
74
75 adn_only_mode_ = false;
76
77 unpackAddresses(begin, end);
78
79 // SvcParams (variable length) field is last.
80 unpackSvcParams(begin, end);
81}
82
83std::string
84Option6Dnr::toText(int indent) const {
85 std::ostringstream stream;
86 std::string in(indent, ' '); // base indentation
87 stream << in << "type=" << type_ << "(V6_DNR), "
88 << "len=" << (len() - getHeaderLen()) << ", " << getDnrInstanceAsText();
89 return (stream.str());
90}
91
92uint16_t
95}
96
97void
99 if (std::distance(begin, end) < getAddrLengthSize()) {
100 isc_throw(OutOfRange, getLogPrefix() << "after"
101 " ADN field, there should be at least "
102 "2 bytes long Addr Length field");
103 }
104
105 // Next come two octets of Addr Length.
107 begin += getAddrLengthSize();
108 // It MUST be a multiple of 16.
109 if ((addr_length_ % V6ADDRESS_LEN) != 0) {
111 << "Addr Len=" << addr_length_ << " is not divisible by 16");
112 }
113
114 // As per draft-ietf-add-dnr 3.1.8:
115 // If additional data is supplied (i.e. not ADN only mode),
116 // the option includes at least one valid IP address.
117 if (addr_length_ == 0) {
119 << "Addr Len=" << addr_length_
120 << " but it must contain at least one valid IP address");
121 }
122
123 // Check if IPv6 Address(es) field is not truncated.
124 if (std::distance(begin, end) < addr_length_) {
125 isc_throw(OutOfRange, getLogPrefix() << "Addr Len=" << addr_length_
126 << " but IPv6 address(es) are truncated to len="
127 << std::distance(begin, end));
128 }
129
130 // Let's unpack the ipv6-address(es).
131 auto addr_end = begin + addr_length_;
132 while (begin != addr_end) {
133 try {
134 ip_addresses_.push_back(IOAddress::fromBytes(AF_INET6, &(*begin)));
135 } catch (const Exception& ex) {
136 isc_throw(BadValue, getLogPrefix() << "failed to parse IPv6 address"
137 << " - " << ex.what());
138 }
139
140 begin += V6ADDRESS_LEN;
141 }
142}
143
144} // namespace dhcp
145} // namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Represents DNR Instance which is used both in DHCPv4 and DHCPv6 Encrypted DNS Option.
Definition: option4_dnr.h:50
std::string getDnrInstanceAsText() const
Returns string representation of the DNR instance.
Definition: option4_dnr.cc:377
void unpackSvcParams(OptionBufferConstIter &begin, OptionBufferConstIter end)
Unpacks Service Parameters from wire data buffer and stores it in svc_params_.
Definition: option4_dnr.cc:467
uint8_t getMinimalLength() const
Returns minimal length of the DNR instance data (without headers) in octets.
Definition: option4_dnr.h:177
AddressContainer ip_addresses_
Vector container holding one or more IP addresses.
Definition: option4_dnr.h:366
void packAdn(isc::util::OutputBuffer &buf) const
Writes the ADN FQDN in the wire format into a buffer.
Definition: option4_dnr.cc:159
uint16_t addr_length_
Length of included IP addresses in octets.
Definition: option4_dnr.h:358
uint8_t getAddrLengthSize() const
Returns size in octets of Addr Length field.
Definition: option4_dnr.h:182
void unpackServicePriority(OptionBufferConstIter &begin)
Unpacks Service Priority from wire data buffer and stores it in service_priority_.
Definition: option4_dnr.cc:423
uint16_t service_priority_
The priority of this instance compared to other DNR instances.
Definition: option4_dnr.h:352
void packSvcParams(isc::util::OutputBuffer &buf) const
Writes the Service Parameters in the wire format into a buffer.
Definition: option4_dnr.cc:186
std::string getLogPrefix() const
Returns Log prefix depending on V4/V6 Option universe.
Definition: option4_dnr.h:199
bool adn_only_mode_
Flag stating whether ADN only mode is used or not.
Definition: option4_dnr.h:375
uint16_t adn_length_
Length of the authentication-domain-name data in octets.
Definition: option4_dnr.h:355
uint16_t dnrInstanceLen() const
Calculates and returns length of DNR Instance data in octets.
Definition: option4_dnr.cc:396
void unpackAdn(OptionBufferConstIter &begin, OptionBufferConstIter end)
Unpacks the ADN from given wire data buffer and stores it in adn_ field.
Definition: option4_dnr.cc:230
uint16_t len() const override
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header)
Definition: option6_dnr.cc:93
void packAddresses(isc::util::OutputBuffer &buf) const override
Writes the IP address(es) in the wire format into a buffer.
Definition: option6_dnr.cc:43
void unpackAddresses(OptionBufferConstIter &begin, OptionBufferConstIter end) override
Unpacks IP address(es) from wire data and stores it/them in ip_addresses_.
Definition: option6_dnr.cc:98
std::string toText(int indent=0) const override
Returns string representation of the option.
Definition: option6_dnr.cc:84
void unpack(OptionBufferConstIter begin, OptionBufferConstIter end) override
Parses received wire data buffer.
Definition: option6_dnr.cc:55
void pack(util::OutputBuffer &buf, bool check=false) const override
Writes option in wire-format to a buffer.
Definition: option6_dnr.cc:27
OptionPtr clone() const override
Copies this option and returns a pointer to the copy.
Definition: option6_dnr.cc:22
Option6Dnr(OptionBufferConstIter begin, OptionBufferConstIter end)
Constructor of the Option from on-wire data.
Definition: option6_dnr.cc:16
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:590
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:321
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:80
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition: option.h:427
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
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
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
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition: buffer.h:550
@ D6O_V6_DNR
Definition: dhcp6.h:159
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.
Definition: io_utilities.h:28
Defines the logger used by the top-level component of kea-lfc.