Kea: option6_ia.cc Source File

| Kea 3.0.3-git

option6_ia.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2025 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#include <dhcp/dhcp6.h>
9#include <dhcp/libdhcp++.h>
10#include <dhcp/option6_ia.h>
11#include <dhcp/option_space.h>
12#include <exceptions/exceptions.h>
13#include <util/io.h>
14
15#include <arpa/inet.h>
16#include <sstream>
17#include <stdint.h>
18
19using namespace std;
20using namespace isc::util;
21
22namespace isc {
23namespace dhcp {
24
25Option6IA::Option6IA(uint16_t type, uint32_t iaid)
26 :Option(Option::V6, type), iaid_(iaid), t1_(0), t2_(0) {
27
28 // IA_TA has different layout than IA_NA and IA_PD. We can't use this class
29 if (type == D6O_IA_TA) {
30 isc_throw(BadValue, "Can't use Option6IA for IA_TA as it has "
31 "a different layout");
32 }
33
34 setEncapsulatedSpace(DHCP6_OPTION_SPACE);
35}
36
37 Option6IA::Option6IA(uint16_t type, OptionBufferConstIter begin,
38 OptionBufferConstIter end)
39 :Option(Option::V6, type) {
40
41 // IA_TA has different layout than IA_NA and IA_PD. We can't use this class
42 if (type == D6O_IA_TA) {
43 isc_throw(BadValue, "Can't use Option6IA for IA_TA as it has "
44 "a different layout");
45 }
46
47 setEncapsulatedSpace(DHCP6_OPTION_SPACE);
48
49 unpack(begin, end);
50}
51
52 OptionPtr
53Option6IA::clone() const {
54 return (cloneInternal<Option6IA>());
55}
56
57 void Option6IA::pack(isc::util::OutputBuffer& buf, bool) const {
58 buf.writeUint16(type_);
59 buf.writeUint16(len() - OPTION6_HDR_LEN);
60 buf.writeUint32(iaid_);
61 buf.writeUint32(t1_);
62 buf.writeUint32(t2_);
63
64 packOptions(buf);
65}
66
67 void Option6IA::unpack(OptionBufferConstIter begin,
68 OptionBufferConstIter end) {
69 // IA_NA and IA_PD have 12 bytes content (iaid, t1, t2 fields)
70 // followed by 0 or more sub-options.
71 if (static_cast<size_t>(distance(begin, end)) < OPTION6_IA_LEN) {
72 isc_throw(OutOfRange, "Option " << type_ << " truncated");
73 }
74 iaid_ = readUint32(&(*begin), distance(begin, end));
75 begin += sizeof(uint32_t);
76 t1_ = readUint32(&(*begin), distance(begin, end));
77 begin += sizeof(uint32_t);
78
79 t2_ = readUint32(&(*begin), distance(begin, end));
80 begin += sizeof(uint32_t);
81
82 unpackOptions(OptionBuffer(begin, end));
83}
84
85std::string Option6IA::toText(int indent) const {
86 stringstream output;
87
88 switch(getType()) {
89 case D6O_IA_NA:
90 output << headerToText(indent, "IA_NA");
91 break;
92 case D6O_IA_PD:
93 output << headerToText(indent, "IA_PD");
94 break;
95 default:
96 output << headerToText(indent);
97 }
98
99 output << ": iaid=" << iaid_ << ", t1=" << t1_ << ", t2=" << t2_
100 << suboptionsToText(indent + 2);
101
102 return (output.str());
103}
104
105uint16_t Option6IA::len() const {
106
107 uint16_t length = OPTION6_HDR_LEN /*header (4)*/ +
108 OPTION6_IA_LEN /* option content (12) */;
109
110 // length of all suboptions
111 for (auto const& it : options_) {
112 length += it.second->len();
113 }
114 return (length);
115}
116
117} // end of isc::dhcp namespace
118} // end of isc namespace
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::dhcp::Option6IA::iaid_
uint32_t iaid_
keeps IA identifier
Definition option6_ia.h:109
isc::dhcp::Option6IA::OPTION6_IA_LEN
static const size_t OPTION6_IA_LEN
Length of IA_NA and IA_PD content.
Definition option6_ia.h:26
isc::dhcp::Option6IA::t1_
uint32_t t1_
keeps T1 timer value
Definition option6_ia.h:112
isc::dhcp::Option6IA::len
virtual uint16_t len() const
returns complete length of option
Definition option6_ia.cc:105
isc::dhcp::Option6IA::Option6IA
Option6IA(uint16_t type, uint32_t iaid)
Ctor, used for constructed options, usually during transmission.
Definition option6_ia.cc:25
isc::dhcp::Option6IA::clone
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition option6_ia.cc:53
isc::dhcp::Option6IA::pack
void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in wire-format to buf, returns pointer to first unused byte after stored option.
Definition option6_ia.cc:57
isc::dhcp::Option6IA::toText
virtual std::string toText(int indent=0) const
Provides human readable text representation.
Definition option6_ia.cc:85
isc::dhcp::Option6IA::t2_
uint32_t t2_
keeps T2 timer value
Definition option6_ia.h:115
isc::dhcp::Option6IA::unpack
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition option6_ia.cc:67
isc::dhcp::Option::type_
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition option.h:597
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:294
isc::dhcp::Option::suboptionsToText
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition option.cc:313
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::V6
@ V6
Definition option.h:90
isc::dhcp::Option::getType
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6).
Definition option.h:300
isc::dhcp::Option::unpackOptions
void unpackOptions(const OptionBuffer &buf)
Builds a collection of sub options from the buffer.
Definition option.cc:155
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::options_
OptionCollection options_
collection for storing suboptions
Definition option.h:603
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::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
D6O_IA_TA
@ D6O_IA_TA
Definition dhcp6.h:24
D6O_IA_NA
@ D6O_IA_NA
Definition dhcp6.h:23
D6O_IA_PD
@ D6O_IA_PD
Definition dhcp6.h:45
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
libdhcp++.h
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::OptionBufferConstIter
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition option.h:30
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 agent_parser.cc:148
option6_ia.h
option_space.h
DHCP6_OPTION_SPACE
#define DHCP6_OPTION_SPACE
Definition std_option_defs.h:17