Kea 2.5.8
option6_iaaddr.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>
12#include <dhcp/option6_iaaddr.h>
13#include <dhcp/option_space.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
30 uint32_t pref, uint32_t valid)
31 :Option(V6, type), addr_(addr), preferred_(pref),
32 valid_(valid) {
34 if (!addr.isV6()) {
35 isc_throw(isc::BadValue, addr_ << " is not an IPv6 address");
36 }
37}
38
39Option6IAAddr::Option6IAAddr(uint32_t type, OptionBuffer::const_iterator begin,
40 OptionBuffer::const_iterator end)
41 :Option(V6, type), addr_("::") {
43 unpack(begin, end);
44}
45
48 return (cloneInternal<Option6IAAddr>());
49}
50
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
66
67 // parse suboption (there shouldn't be any for IAADDR)
68 packOptions(buf);
69}
70
71void Option6IAAddr::unpack(OptionBuffer::const_iterator begin,
72 OptionBuffer::const_iterator end) {
73 if ( distance(begin, end) < OPTION6_IAADDR_LEN) {
74 isc_throw(OutOfRange, "Option " << type_ << " truncated");
75 }
76
77 // 16 bytes: IPv6 address
78 addr_ = IOAddress::fromBytes(AF_INET6, &(*begin));
79 begin += V6ADDRESS_LEN;
80
81 preferred_ = readUint32(&(*begin), distance(begin, end));
82 begin += sizeof(uint32_t);
83
84 valid_ = readUint32(&(*begin), distance(begin, end));
85 begin += sizeof(uint32_t);
86
87 unpackOptions(OptionBuffer(begin, end));
88}
89
90std::string Option6IAAddr::toText(int indent) const {
91 std::stringstream output;
92 output << headerToText(indent, "IAADDR") << ": "
93 << "address=" << addr_
94 << ", preferred-lft=" << preferred_
95 << ", valid-lft=" << valid_;
96
97 output << suboptionsToText(indent + 2);
98 return (output.str());
99}
100
101uint16_t Option6IAAddr::len() const {
102
103 uint16_t length = OPTION6_HDR_LEN + OPTION6_IAADDR_LEN;
104
105 // length of all suboptions
106 // TODO implement:
107 // protected: unsigned short Option::lenHelper(int header_size);
108 for (auto const& it : options_) {
109 length += it.second->len();
110 }
111 return (length);
112}
113
114} // end of namespace isc::dhcp
115} // 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 pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in wire-format.
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
virtual std::string toText(int indent=0) const
Returns string representation of the option.
static const size_t OPTION6_IAADDR_LEN
length of the fixed part of the IAADDR option
virtual uint16_t len() const
returns data length (data length + DHCPv4/DHCPv6 option header)
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
unsigned int valid_
contains valid-lifetime timer (in seconds)
Option6IAAddr(uint16_t type, const isc::asiolink::IOAddress &addr, uint32_t preferred, uint32_t valid)
Constructor, used for options constructed (during transmission).
isc::asiolink::IOAddress addr_
contains an IPv6 address
unsigned int preferred_
contains preferred-lifetime timer (in seconds)
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
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition: option.cc:307
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition: option.h:435
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:321
void unpackOptions(const OptionBuffer &buf)
Builds a collection of sub options from the buffer.
Definition: option.cc:155
void packOptions(isc::util::OutputBuffer &buf, bool check=true) const
Store sub options in a buffer.
Definition: option.cc:136
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:80
OptionCollection options_
collection for storing suboptions
Definition: option.h:596
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
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:528
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
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(void const *const buffer, size_t const length)
uint32_t wrapper over readUint.
Definition: io.h:82
Defines the logger used by the top-level component of kea-lfc.
#define DHCP6_OPTION_SPACE