Kea  2.1.7-git
option6_addrlst.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 
9 #include <asiolink/io_address.h>
10 #include <dhcp/dhcp6.h>
11 #include <dhcp/libdhcp++.h>
12 #include <dhcp/option6_addrlst.h>
13 #include <exceptions/exceptions.h>
14 #include <util/io_utilities.h>
15 
16 #include <sstream>
17 
18 #include <arpa/inet.h>
19 #include <stdint.h>
20 
21 using namespace std;
22 using namespace isc;
23 using namespace isc::dhcp;
24 using namespace isc::asiolink;
25 using namespace isc::util;
26 
27 namespace isc {
28 namespace dhcp {
29 
30 Option6AddrLst::Option6AddrLst(uint16_t type, const AddressContainer& addrs)
31  : Option(V6, type), addrs_(addrs) {
32 }
33 
35  : Option(V6, type), addrs_(1,addr) {
36 }
37 
40  : Option(V6, type) {
41  unpack(begin, end);
42 }
43 
46  return (cloneInternal<Option6AddrLst>());
47 }
48 
49 void
51  if (!addr.isV6()) {
52  isc_throw(BadValue, "Can't store non-IPv6 address in Option6AddrLst option");
53  }
54 
55  addrs_.clear();
56  addrs_.push_back(addr);
57 }
58 
59 void
61  addrs_ = addrs;
62 }
63 
65  buf.writeUint16(type_);
66 
67  // len() returns complete option length.
68  // len field contains length without 4-byte option header
69  buf.writeUint16(len() - getHeaderLen());
70 
71  for (AddressContainer::const_iterator addr=addrs_.begin();
72  addr!=addrs_.end(); ++addr) {
73  if (!addr->isV6()) {
74  isc_throw(isc::BadValue, addr->toText()
75  << " is not an IPv6 address");
76  }
77  // If an address is IPv6 address it should have assumed
78  // length of V6ADDRESS_LEN.
79  buf.writeData(&addr->toBytes()[0], V6ADDRESS_LEN);
80  }
81 }
82 
85  if ((distance(begin, end) % V6ADDRESS_LEN) != 0) {
86  isc_throw(OutOfRange, "Option " << type_
87  << " malformed: len=" << distance(begin, end)
88  << " is not divisible by 16.");
89  }
90  while (begin != end) {
91  addrs_.push_back(IOAddress::fromBytes(AF_INET6, &(*begin)));
92  begin += V6ADDRESS_LEN;
93  }
94 }
95 
96 std::string Option6AddrLst::toText(int indent) const {
97  stringstream output;
98  output << headerToText(indent) << ":";
99 
100  for (AddressContainer::const_iterator addr = addrs_.begin();
101  addr != addrs_.end(); ++addr) {
102  output << " " << *addr;
103  }
104  return (output.str());
105 }
106 
107 uint16_t Option6AddrLst::len() const {
108  return (OPTION6_HDR_LEN + addrs_.size() * V6ADDRESS_LEN);
109 }
110 
111 } // end of namespace isc::dhcp
112 } // end of namespace isc
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
STL namespace.
virtual std::string toText(int indent=0) const
Returns string representation of the option.
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:321
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header) ...
std::vector< isc::asiolink::IOAddress > AddressContainer
a container for (IPv6) addresses
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...
void setAddress(const isc::asiolink::IOAddress &addr)
Sets a single address.
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 void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received data.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
Option6AddrLst(uint16_t type, const AddressContainer &addrs)
Constructor used during option generation.
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
Defines the logger used by the top-level component of kea-lfc.
void pack(isc::util::OutputBuffer &buf, bool check=true) const
Assembles on-wire form of this option.
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 setAddresses(const AddressContainer &addrs)
Sets list of addresses.
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