Kea 2.5.8
option6_addrlst.cc
Go to the documentation of this file.
1// Copyright (C) 2011-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
10#include <dhcp/dhcp6.h>
11#include <dhcp/libdhcp++.h>
14
15#include <sstream>
16
17#include <arpa/inet.h>
18#include <stdint.h>
19
20using namespace std;
21using namespace isc;
22using namespace isc::dhcp;
23using namespace isc::asiolink;
24using namespace isc::util;
25
26namespace isc {
27namespace dhcp {
28
30 : Option(V6, type), addrs_(addrs) {
31}
32
34 : Option(V6, type), addrs_(1,addr) {
35}
36
39 : Option(V6, type) {
40 unpack(begin, end);
41}
42
45 return (cloneInternal<Option6AddrLst>());
46}
47
48void
50 if (!addr.isV6()) {
51 isc_throw(BadValue, "Can't store non-IPv6 address in Option6AddrLst option");
52 }
53
54 addrs_.clear();
55 addrs_.push_back(addr);
56}
57
58void
60 addrs_ = addrs;
61}
62
64 buf.writeUint16(type_);
65
66 // len() returns complete option length.
67 // len field contains length without 4-byte option header
68 buf.writeUint16(len() - getHeaderLen());
69
70 for (auto const& addr : addrs_) {
71 if (!addr.isV6()) {
72 isc_throw(isc::BadValue, addr.toText()
73 << " is not an IPv6 address");
74 }
75 // If an address is IPv6 address it should have assumed
76 // length of V6ADDRESS_LEN.
77 buf.writeData(&addr.toBytes()[0], V6ADDRESS_LEN);
78 }
79}
80
83 if ((distance(begin, end) % V6ADDRESS_LEN) != 0) {
84 isc_throw(OutOfRange, "Option " << type_
85 << " malformed: len=" << distance(begin, end)
86 << " is not divisible by 16.");
87 }
88 while (begin != end) {
89 addrs_.push_back(IOAddress::fromBytes(AF_INET6, &(*begin)));
90 begin += V6ADDRESS_LEN;
91 }
92}
93
94std::string Option6AddrLst::toText(int indent) const {
95 stringstream output;
96 output << headerToText(indent) << ":";
97
98 for (auto const& addr : addrs_) {
99 output << " " << addr;
100 }
101 return (output.str());
102}
103
104uint16_t Option6AddrLst::len() const {
105 return (OPTION6_HDR_LEN + addrs_.size() * V6ADDRESS_LEN);
106}
107
108} // end of namespace isc::dhcp
109} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
void setAddresses(const AddressContainer &addrs)
Sets list of addresses.
Option6AddrLst(uint16_t type, const AddressContainer &addrs)
Constructor used during option generation.
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header)
virtual std::string toText(int indent=0) const
Returns string representation of the option.
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received data.
void setAddress(const isc::asiolink::IOAddress &addr)
Sets a single address.
std::vector< isc::asiolink::IOAddress > AddressContainer
a container for (IPv6) addresses
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
void pack(isc::util::OutputBuffer &buf, bool check=true) const
Assembles on-wire form of this option.
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:590
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:288
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
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:347
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:498
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition: buffer.h:556
#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
Defines the logger used by the top-level component of kea-lfc.