Kea  1.9.9-git
option6_addrlst.cc
Go to the documentation of this file.
1 // Copyright (C) 2011-2016 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 
66  buf.writeUint16(type_);
67 
68  // len() returns complete option length.
69  // len field contains length without 4-byte option header
70  buf.writeUint16(len() - getHeaderLen());
71 
72  for (AddressContainer::const_iterator addr=addrs_.begin();
73  addr!=addrs_.end(); ++addr) {
74  if (!addr->isV6()) {
75  isc_throw(isc::BadValue, addr->toText()
76  << " is not an IPv6 address");
77  }
78  // If an address is IPv6 address it should have assumed
79  // length of V6ADDRESS_LEN.
80  buf.writeData(&addr->toBytes()[0], V6ADDRESS_LEN);
81  }
82 }
83 
86  if ((distance(begin, end) % V6ADDRESS_LEN) != 0) {
87  isc_throw(OutOfRange, "Option " << type_
88  << " malformed: len=" << distance(begin, end)
89  << " is not divisible by 16.");
90  }
91  while (begin != end) {
92  addrs_.push_back(IOAddress::fromBytes(AF_INET6, &(*begin)));
93  begin += V6ADDRESS_LEN;
94  }
95 }
96 
97 std::string Option6AddrLst::toText(int indent) const {
98  stringstream output;
99  output << headerToText(indent) << ":";
100 
101  for (AddressContainer::const_iterator addr = addrs_.begin();
102  addr != addrs_.end(); ++addr) {
103  output << " " << *addr;
104  }
105  return (output.str());
106 }
107 
108 uint16_t Option6AddrLst::len() const {
109  return (OPTION6_HDR_LEN + addrs_.size() * V6ADDRESS_LEN);
110 }
111 
112 } // end of namespace isc::dhcp
113 } // end of namespace isc
void pack(isc::util::OutputBuffer &buf) const
Assembles on-wire form of this option.
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
STL namespace.
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:338
virtual std::string toText(int indent=0) const
Returns string representation of the option.
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.
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-dhcp-ddns.
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
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 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:569
static const size_t OPTION6_HDR_LEN
length of any DHCPv6 option header
Definition: option.h:79
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header) ...