Kea  1.9.9-git
option6_iaprefix.cc
Go to the documentation of this file.
1 // Copyright (C) 2013-2017 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_iaprefix.h>
13 #include <dhcp/option_space.h>
14 #include <exceptions/exceptions.h>
15 #include <util/io_utilities.h>
16 
17 #include <sstream>
18 
19 #include <stdint.h>
20 #include <arpa/inet.h>
21 
22 using namespace std;
23 using namespace isc::asiolink;
24 using namespace isc::util;
25 
26 namespace isc {
27 namespace dhcp {
28 
29 Option6IAPrefix::Option6IAPrefix(uint16_t type, const isc::asiolink::IOAddress& prefix,
30  uint8_t prefix_len, uint32_t pref, uint32_t valid)
31  :Option6IAAddr(type, prefix, pref, valid), prefix_len_(prefix_len) {
33  // Option6IAAddr will check if prefix is IPv6 and will throw if it is not
34  if (prefix_len > 128) {
35  isc_throw(BadValue, static_cast<unsigned>(prefix_len)
36  << " is not a valid prefix length. "
37  << "Allowed range is 0..128");
38  }
39 }
40 
41 Option6IAPrefix::Option6IAPrefix(uint32_t type, OptionBuffer::const_iterator begin,
42  OptionBuffer::const_iterator end)
43  :Option6IAAddr(type, begin, end) {
45  unpack(begin, end);
46 }
47 
50  return (cloneInternal<Option6IAPrefix>());
51 }
52 
54  if (!addr_.isV6()) {
55  isc_throw(isc::BadValue, addr_ << " is not an IPv6 address");
56  }
57 
58  buf.writeUint16(type_);
59 
60  // len() returns complete option length. len field contains
61  // length without 4-byte option header
62  buf.writeUint16(len() - getHeaderLen());
63 
65  buf.writeUint32(valid_);
66  buf.writeUint8(prefix_len_);
67 
68  buf.writeData(&addr_.toBytes()[0], isc::asiolink::V6ADDRESS_LEN);
69 
70  // store encapsulated options (the only defined so far is PD_EXCLUDE)
71  packOptions(buf);
72 }
73 
74 void Option6IAPrefix::unpack(OptionBuffer::const_iterator begin,
75  OptionBuffer::const_iterator end) {
76  if ( distance(begin, end) < OPTION6_IAPREFIX_LEN) {
77  isc_throw(OutOfRange, "Option " << type_ << " truncated");
78  }
79 
80  preferred_ = readUint32(&(*begin), distance(begin, end));
81  begin += sizeof(uint32_t);
82 
83  valid_ = readUint32(&(*begin), distance(begin, end));
84  begin += sizeof(uint32_t);
85 
86  prefix_len_ = *begin;
87  begin += sizeof(uint8_t);
88 
89  // 16 bytes: IPv6 address
90  OptionBuffer address_with_mask;
91  mask(begin, begin + V6ADDRESS_LEN, prefix_len_, address_with_mask);
92  addr_ = IOAddress::fromBytes(AF_INET6, &(*address_with_mask.begin()));
93  begin += V6ADDRESS_LEN;
94 
95  // unpack encapsulated options (the only defined so far is PD_EXCLUDE)
96  unpackOptions(OptionBuffer(begin, end));
97 }
98 
99 std::string Option6IAPrefix::toText(int indent) const {
100  std::stringstream output;
101  output << headerToText(indent, "IAPREFIX") << ": "
102  << "prefix=" << addr_ << "/" << static_cast<int>(prefix_len_)
103  << ", preferred-lft=" << preferred_
104  << ", valid-lft=" << valid_;
105 
106  output << suboptionsToText(indent + 2);
107  return (output.str());
108 }
109 
110 uint16_t Option6IAPrefix::len() const {
111 
112  uint16_t length = OPTION6_HDR_LEN + OPTION6_IAPREFIX_LEN;
113 
114  // length of all suboptions
115  for (OptionCollection::const_iterator it = options_.begin();
116  it != options_.end(); ++it) {
117  length += (*it).second->len();
118  }
119  return (length);
120 }
121 
122 void
123 Option6IAPrefix::mask(OptionBuffer::const_iterator begin,
124  OptionBuffer::const_iterator end,
125  const uint8_t len,
126  OptionBuffer& output_address) const {
127  output_address.resize(16, 0);
128  if (len >= 128) {
129  std::copy(begin, end, output_address.begin());
130  } else if (len > 0) {
131  // All the bits that represent whole octets of the prefix are copied with
132  // no change.
133  std::copy(begin, begin + static_cast<uint8_t>(len/8), output_address.begin());
134  // The remaining significant bits of the last octet have to be left unchanged,
135  // but the remaining bits of this octet must be set to zero. The number of
136  // significant bits is calculated as a reminder from the division of the
137  // prefix length by 8 (by size of the octet). The number of bits to be set
138  // to zero is therefore calculated as: 8 - (len % 8).
139  // Next, the mask is created by shifting the 0xFF by the number of bits
140  // to be set to 0. By performing logical AND of this mask with the original
141  // value of the last octet we get the final value for the new octet.
142  output_address[len/8] = (*(begin + len/8) & (0xFF << (8 - (len % 8))));
143  }
144 }
145 
146 
147 } // end of namespace isc::dhcp
148 } // end of namespace isc
virtual std::string toText(int indent=0) const
Returns string representation of the option.
void packOptions(isc::util::OutputBuffer &buf) const
Store sub options in a buffer.
Definition: option.cc:146
unsigned int valid_
contains valid-lifetime timer (in seconds)
isc::asiolink::IOAddress addr_
contains an IPv6 address
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
STL namespace.
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:338
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
virtual uint16_t len() const
returns data length (data length + DHCPv4/DHCPv6 option header)
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition: buffer.h:550
#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...
Definition: edns.h:19
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1097
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
OptionCollection options_
collection for storing suboptions
Definition: option.h:575
void setEncapsulatedSpace(const std::string &encapsulated_space)
Sets the name of the option space encapsulated by this option.
Definition: option.h:416
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
std::string suboptionsToText(const int indent=0) const
Returns collection of suboptions in the textual format.
Definition: option.cc:323
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-dhcp-ddns.
static const size_t OPTION6_IAPREFIX_LEN
length of the fixed part of the IAPREFIX option
void unpackOptions(const OptionBuffer &buf)
Builds a collection of sub options from the buffer.
Definition: option.cc:165
void pack(isc::util::OutputBuffer &buf) const
Writes option in wire-format.
#define DHCP6_OPTION_SPACE
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:304
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition: buffer.h:466
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...
Option6IAPrefix(uint16_t type, const isc::asiolink::IOAddress &addr, uint8_t prefix_length, uint32_t preferred, uint32_t valid)
Constructor, used for options constructed (during transmission).
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:569
unsigned int preferred_
contains preferred-lifetime timer (in seconds)
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:79