Kea: option6_iaaddr.cc Source File

| Kea 3.2.0-git

option6_iaaddr.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2026 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 <asiolink/io_address.h>
10#include <dhcp/dhcp6.h>
11#include <dhcp/libdhcp++.h>
12#include <dhcp/option6_iaaddr.h>
13#include <dhcp/option_space.h>
14#include <exceptions/exceptions.h>
15#include <util/io.h>
16
17#include <sstream>
18
19#include <stdint.h>
20#include <arpa/inet.h>
21
22using namespace std;
23using namespace isc::asiolink;
24using namespace isc::util;
25
26namespace isc {
27namespace dhcp {
28
29Option6IAAddr::Option6IAAddr(uint16_t type, const isc::asiolink::IOAddress& addr,
30 uint32_t pref, uint32_t valid)
31 :Option(V6, type), addr_(addr), preferred_(pref),
32 valid_(valid) {
33 setEncapsulatedSpace(DHCP6_OPTION_SPACE);
34 if (!addr.isV6()) {
35 isc_throw(isc::BadValue, addr_ << " is not an IPv6 address");
36 }
37}
38
39 Option6IAAddr::Option6IAAddr(uint32_t type, OptionBuffer::const_iterator begin,
40 OptionBuffer::const_iterator end, size_t rec_level)
41 :Option(V6, type), addr_("::") {
42 setEncapsulatedSpace(DHCP6_OPTION_SPACE);
43 unpack(begin, end, rec_level);
44}
45
46 OptionPtr
47Option6IAAddr::clone() const {
48 return (cloneInternal<Option6IAAddr>());
49}
50
51 void Option6IAAddr::pack(isc::util::OutputBuffer& buf, bool) const {
52
53 buf.writeUint16(type_);
54
55 // len() returns complete option length. len field contains
56 // length without 4-byte option header
57 buf.writeUint16(len() - getHeaderLen());
58
59 if (!addr_.isV6()) {
60 isc_throw(isc::BadValue, addr_ << " is not an IPv6 address");
61 }
62 buf.writeData(&addr_.toBytes()[0], isc::asiolink::V6ADDRESS_LEN);
63
64 buf.writeUint32(preferred_);
65 buf.writeUint32(valid_);
66
67 // parse suboption (there shouldn't be any for IAADDR)
68 packOptions(buf);
69}
70
71 void Option6IAAddr::unpack(OptionBuffer::const_iterator begin,
72 OptionBuffer::const_iterator end) {
73 unpack(begin, end, 0);
74}
75
76 void Option6IAAddr::unpack(OptionBuffer::const_iterator begin,
77 OptionBuffer::const_iterator end,
78 size_t rec_level) {
79 if (static_cast<size_t>(distance(begin, end)) < OPTION6_IAADDR_LEN) {
80 isc_throw(OutOfRange, "Option " << type_ << " truncated");
81 }
82
83 // 16 bytes: IPv6 address
84 addr_ = IOAddress::fromBytes(AF_INET6, &(*begin));
85 begin += V6ADDRESS_LEN;
86
87 preferred_ = readUint32(&(*begin), distance(begin, end));
88 begin += sizeof(uint32_t);
89
90 valid_ = readUint32(&(*begin), distance(begin, end));
91 begin += sizeof(uint32_t);
92
93 unpackOptions(OptionBuffer(begin, end), rec_level);
94}
95
96std::string Option6IAAddr::toText(int indent) const {
97 std::stringstream output;
98 output << headerToText(indent, "IAADDR") << ": "
99 << "address=" << addr_
100 << ", preferred-lft=" << preferred_
101 << ", valid-lft=" << valid_;
102
103 output << suboptionsToText(indent + 2);
104 return (output.str());
105}
106
107uint16_t Option6IAAddr::len() const {
108
109 uint16_t length = OPTION6_HDR_LEN + OPTION6_IAADDR_LEN;
110
111 // length of all suboptions
112 // TODO implement:
113 // protected: unsigned short Option::lenHelper(int header_size);
114 for (auto const& it : options_) {
115 length += it.second->len();
116 }
117 return (length);
118}
119
120} // end of namespace isc::dhcp
121} // end of namespace isc
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::OutOfRange
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Definition exceptions/exceptions.h:115
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:53
isc::asiolink::IOAddress::isV6
bool isV6() const
Convenience function to check for an IPv6 address.
Definition io_address.h:144
isc::asiolink::IOAddress::fromBytes
static IOAddress fromBytes(short family, const uint8_t *data)
Creates an address from over wire data.
Definition io_address.cc:65
isc::dhcp::Option6IAAddr::pack
void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in wire-format.
Definition option6_iaaddr.cc:51
isc::dhcp::Option6IAAddr::clone
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition option6_iaaddr.cc:47
isc::dhcp::Option6IAAddr::toText
virtual std::string toText(int indent=0) const
Returns string representation of the option.
Definition option6_iaaddr.cc:96
isc::dhcp::Option6IAAddr::OPTION6_IAADDR_LEN
static const size_t OPTION6_IAADDR_LEN
length of the fixed part of the IAADDR option
Definition option6_iaaddr.h:26
isc::dhcp::Option6IAAddr::len
virtual uint16_t len() const
returns data length (data length + DHCPv4/DHCPv6 option header)
Definition option6_iaaddr.cc:107
isc::dhcp::Option6IAAddr::unpack
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition option6_iaaddr.cc:71
isc::dhcp::Option6IAAddr::valid_
unsigned int valid_
contains valid-lifetime timer (in seconds)
Definition option6_iaaddr.h:133
isc::dhcp::Option6IAAddr::Option6IAAddr
Option6IAAddr(uint16_t type, const isc::asiolink::IOAddress &addr, uint32_t preferred, uint32_t valid)
Constructor, used for options constructed (during transmission).
Definition option6_iaaddr.cc:29
isc::dhcp::Option6IAAddr::addr_
isc::asiolink::IOAddress addr_
contains an IPv6 address
Definition option6_iaaddr.h:127
isc::dhcp::Option6IAAddr::preferred_
unsigned int preferred_
contains preferred-lifetime timer (in seconds)
Definition option6_iaaddr.h:130
isc::dhcp::Option::type_
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition option.h:598
isc::dhcp::Option::headerToText
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition option.cc:295
isc::dhcp::Option::suboptionsToText
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition option.cc:314
isc::dhcp::Option::setEncapsulatedSpace
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition option.h:442
isc::dhcp::Option::getHeaderLen
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6).
Definition option.cc:328
isc::dhcp::Option::V6
@ V6
Definition option.h:90
isc::dhcp::Option::packOptions
void packOptions(isc::util::OutputBuffer &buf, bool check=true) const
Store sub options in a buffer.
Definition option.cc:136
isc::dhcp::Option::OPTION6_HDR_LEN
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition option.h:87
isc::dhcp::Option::valid
virtual bool valid() const
returns if option is valid (e.g.
Definition option.cc:191
isc::dhcp::Option::options_
OptionCollection options_
collection for storing suboptions
Definition option.h:604
isc::dhcp::Option::unpackOptions
void unpackOptions(const OptionBuffer &buf, size_t rec_level=0)
Builds a collection of sub options from the buffer.
Definition option.cc:155
isc::dhcp::Option::cloneInternal
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition option.h:504
isc::dhcp::Option::Option
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
Definition option.cc:39
isc::util::OutputBuffer
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:346
isc::util::OutputBuffer::writeUint16
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:501
isc::util::OutputBuffer::writeData
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition buffer.h:559
isc::util::OutputBuffer::writeUint32
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:531
dhcp6.h
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
io.h
io_address.h
libdhcp++.h
isc::asiolink
Definition addr_utilities.cc:170
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::OptionBuffer
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition option.h:24
isc::dhcp::OptionPtr
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
isc::util
Definition time_utils.cc:43
isc::util::readUint32
uint32_t readUint32(void const *const buffer, size_t const length)
uint32_t wrapper over readUint.
Definition io.h:82
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
option6_iaaddr.h
option_space.h
DHCP6_OPTION_SPACE
#define DHCP6_OPTION_SPACE
Definition std_option_defs.h:17