Kea  2.1.7-git
option6_ia.cc
Go to the documentation of this file.
1 // Copyright (C) 2011-2022 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_utilities.h>
14 
15 #include <arpa/inet.h>
16 #include <sstream>
17 #include <stdint.h>
18 
19 using namespace std;
20 using namespace isc::util;
21 
22 namespace isc {
23 namespace dhcp {
24 
25 Option6IA::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 
35 }
36 
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 
48 
49  unpack(begin, end);
50 }
51 
54  return (cloneInternal<Option6IA>());
55 }
56 
57 void Option6IA::pack(isc::util::OutputBuffer& buf, bool) const {
58  buf.writeUint16(type_);
60  buf.writeUint32(iaid_);
61  buf.writeUint32(t1_);
62  buf.writeUint32(t2_);
63 
64  packOptions(buf);
65 }
66 
69  // IA_NA and IA_PD have 12 bytes content (iaid, t1, t2 fields)
70  // followed by 0 or more sub-options.
71  if (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 
85 std::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 
105 uint16_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 (OptionCollection::const_iterator it = options_.begin();
112  it != options_.end();
113  ++it) {
114  length += (*it).second->len();
115  }
116  return (length);
117 }
118 
119 } // end of isc::dhcp namespace
120 } // end of isc namespace
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition: option.cc:307
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option6_ia.cc:53
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:293
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition: option6_ia.cc:67
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
STL namespace.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
void packOptions(isc::util::OutputBuffer &buf, bool check=true) const
Store sub options in a buffer.
Definition: option.cc:136
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:288
Definition: edns.h:19
virtual std::string toText(int indent=0) const
Provides human readable text representation.
Definition: option6_ia.cc:85
uint32_t t1_
keeps T1 timer value
Definition: option6_ia.h:111
OptionCollection options_
collection for storing suboptions
Definition: option.h:594
virtual uint16_t len() const
returns complete length of option
Definition: option6_ia.cc:105
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition: option.h:435
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
uint32_t readUint32(const uint8_t *buffer, size_t length)
Read Unsigned 32-Bit Integer from Buffer.
Definition: io_utilities.h:79
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
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:520
Defines the logger used by the top-level component of kea-lfc.
uint32_t iaid_
keeps IA identifier
Definition: option6_ia.h:108
void unpackOptions(const OptionBuffer &buf)
Builds a collection of sub options from the buffer.
Definition: option.cc:155
#define DHCP6_OPTION_SPACE
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:490
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 to buf, returns pointer to first unused byte after stored option...
Definition: option6_ia.cc:57
static const size_t OPTION6_IA_LEN
Length of IA_NA and IA_PD content.
Definition: option6_ia.h:26
uint32_t t2_
keeps T2 timer value
Definition: option6_ia.h:114
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:588
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:80
Option6IA(uint16_t type, uint32_t iaid)
Ctor, used for constructed options, usually during transmission.
Definition: option6_ia.cc:25