Kea 2.7.5
option6_dnr.cc
Go to the documentation of this file.
1// Copyright (C) 2023-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/option6_dnr.h>
10#include <util/io.h>
11
12using namespace isc::asiolink;
13using namespace isc::util;
14
15namespace isc {
16namespace dhcp {
17
20 bool convenient_notation)
21 : Option(V6, D6O_V6_DNR), DnrInstance(V6), convenient_notation_(convenient_notation) {
22 unpack(begin, end);
23}
24
27 return (cloneInternal<Option6Dnr>());
28}
29
30void
31Option6Dnr::pack(util::OutputBuffer& buf, bool check) const {
32 packHeader(buf, check);
33
34 buf.writeUint16(service_priority_);
35 buf.writeUint16(adn_length_);
36 packAdn(buf);
37 if (adn_only_mode_) {
38 return;
39 }
40
41 buf.writeUint16(addr_length_);
42 packAddresses(buf);
43 packSvcParams(buf);
44}
45
46void
48 for (auto const& address : ip_addresses_) {
49 if (!address.isV6()) {
51 << address.toText() << " is not an IPv6 address");
52 }
53
54 buf.writeData(&address.toBytes()[0], V6ADDRESS_LEN);
55 }
56}
57
58void
60 if (convenient_notation_) {
61 // parse convenient notation
62 std::string config_txt = std::string(begin, end);
64 } else {
65 if (std::distance(begin, end) < getMinimalLength()) {
67 << "data truncated to size " << std::distance(begin, end));
68 }
69
70 setData(begin, end);
71
72 // First two octets of Option data is Service Priority - this is mandatory field.
74
75 // Next come two octets of ADN Length plus the ADN data itself (variable length).
76 // This is Opaque Data Tuple so let's use this class to retrieve the ADN data.
77 unpackAdn(begin, end);
78
79 if (begin == end) {
80 // ADN only mode, other fields are not included.
81 return;
82 }
83
84 adn_only_mode_ = false;
85
86 unpackAddresses(begin, end);
87
88 // SvcParams (variable length) field is last.
89 unpackSvcParams(begin, end);
90 }
91}
92
93std::string
94Option6Dnr::toText(int indent) const {
95 std::ostringstream stream;
96 std::string in(indent, ' '); // base indentation
97 stream << in << "type=" << type_ << "(V6_DNR), "
98 << "len=" << (len() - getHeaderLen()) << ", " << getDnrInstanceAsText();
99 return (stream.str());
100}
101
102uint16_t
104 return (OPTION6_HDR_LEN + dnrInstanceLen());
105}
106
107void
109 if (std::distance(begin, end) < getAddrLengthSize()) {
110 isc_throw(OutOfRange, getLogPrefix() << "after"
111 " ADN field, there should be at least "
112 "2 bytes long Addr Length field");
113 }
114
115 // Next come two octets of Addr Length.
117 begin += getAddrLengthSize();
118 // It MUST be a multiple of 16.
119 if ((addr_length_ % V6ADDRESS_LEN) != 0) {
121 << "Addr Len=" << addr_length_ << " is not divisible by 16");
122 }
123
124 // As per RFC9463 3.1.8:
125 // If additional data is supplied (i.e. not ADN only mode),
126 // the option includes at least one valid IP address.
127 if (addr_length_ == 0) {
129 << "Addr Len=" << addr_length_
130 << " but it must contain at least one valid IP address");
131 }
132
133 // Check if IPv6 Address(es) field is not truncated.
134 if (std::distance(begin, end) < addr_length_) {
135 isc_throw(OutOfRange, getLogPrefix() << "Addr Len=" << addr_length_
136 << " but IPv6 address(es) are truncated to len="
137 << std::distance(begin, end));
138 }
139
140 // Let's unpack the ipv6-address(es).
141 auto addr_end = begin + addr_length_;
142 while (begin != addr_end) {
143 try {
144 addIpAddress(IOAddress::fromBytes(AF_INET6, &(*begin)));
145 } catch (const Exception& ex) {
146 isc_throw(BadValue, getLogPrefix() << "failed to parse IPv6 address"
147 << " - " << ex.what());
148 }
149
150 begin += V6ADDRESS_LEN;
151 }
152}
153
154} // namespace dhcp
155} // 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.
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:55
std::string getDnrInstanceAsText() const
Returns string representation of the DNR instance.
void unpackSvcParams(OptionBufferConstIter &begin, OptionBufferConstIter end)
Unpacks Service Parameters from wire data buffer and stores it in svc_params_buf_.
uint8_t getMinimalLength() const
Returns minimal length of the DNR instance data (without headers) in octets.
AddressContainer ip_addresses_
Vector container holding one or more IP addresses.
void packAdn(isc::util::OutputBuffer &buf) const
Writes the ADN FQDN in the wire format into a buffer.
uint16_t addr_length_
Length of included IP addresses in octets.
uint8_t getAddrLengthSize() const
Returns size in octets of Addr Length field.
void parseDnrInstanceConfigData(const std::string &config_txt)
Parses a convenient notation of the option data, which may be used in config.
void unpackServicePriority(OptionBufferConstIter &begin)
Unpacks Service Priority from wire data buffer and stores it in service_priority_.
uint16_t service_priority_
The priority of this instance compared to other DNR instances.
void packSvcParams(isc::util::OutputBuffer &buf) const
Writes the Service Parameters in the wire format into a buffer.
std::string getLogPrefix() const
Returns Log prefix depending on V4/V6 Option universe.
bool adn_only_mode_
Flag stating whether ADN only mode is used or not.
uint16_t adn_length_
Length of the authentication-domain-name data in octets.
uint16_t dnrInstanceLen() const
Calculates and returns length of DNR Instance data in octets.
void addIpAddress(const asiolink::IOAddress &ip_address)
Adds IP address to ip_addresses_ container.
void unpackAdn(OptionBufferConstIter &begin, OptionBufferConstIter end)
Unpacks the ADN from given wire data buffer and stores it in adn_ field.
uint16_t len() const override
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header)
Option6Dnr(OptionBufferConstIter begin, OptionBufferConstIter end, bool convenient_notation=false)
Constructor of the Option from on-wire data.
void packAddresses(isc::util::OutputBuffer &buf) const override
Writes the IP address(es) in the wire format into a buffer.
void unpackAddresses(OptionBufferConstIter &begin, OptionBufferConstIter end) override
Unpacks IP address(es) from wire data and stores it/them in ip_addresses_.
std::string toText(int indent=0) const override
Returns string representation of the option.
void unpack(OptionBufferConstIter begin, OptionBufferConstIter end) override
Parses received wire data buffer.
void pack(util::OutputBuffer &buf, bool check=false) const override
Writes option in wire-format to a buffer.
OptionPtr clone() const override
Copies this option and returns a pointer to the copy.
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition option.h:597
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition option.cc:327
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition option.h:87
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition option.h:434
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:343
@ 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(void const *const buffer, size_t const length)
uint16_t wrapper over readUint.
Definition io.h:76
Defines the logger used by the top-level component of kea-lfc.