Kea  1.9.9-git
option4_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/option4_addrlst.h>
11 #include <exceptions/exceptions.h>
12 #include <util/io_utilities.h>
13 
14 #include <iomanip>
15 #include <sstream>
16 
17 #include <arpa/inet.h>
18 #include <stdint.h>
19 #include <string.h>
20 
21 using namespace std;
22 using namespace isc::util;
23 using namespace isc::asiolink;
24 
25 namespace isc {
26 namespace dhcp {
27 
28 Option4AddrLst::Option4AddrLst(uint8_t type)
29  :Option(V4, type) {
30 }
31 
33  :Option(V4, type) {
34  setAddresses(addrs);
35  // don't set addrs_ directly. setAddresses() will do additional checks.
36 }
37 
38 
41  :Option(V4, type) {
42  if ( (distance(first, last) % V4ADDRESS_LEN) ) {
43  isc_throw(OutOfRange, "DHCPv4 Option4AddrLst " << type_
44  << " has invalid length=" << distance(first, last)
45  << ", must be divisible by 4.");
46  }
47 
48  while (first != last) {
49  const uint8_t* ptr = &(*first);
50  addAddress(IOAddress(readUint32(ptr, distance(first, last))));
51  first += V4ADDRESS_LEN;
52  }
53 }
54 
55 Option4AddrLst::Option4AddrLst(uint8_t type, const IOAddress& addr)
56  :Option(V4, type) {
57  setAddress(addr);
58 }
59 
62  return (cloneInternal<Option4AddrLst>());
63 }
64 
65 void
67 
68  if (addrs_.size() * V4ADDRESS_LEN > 255) {
69  isc_throw(OutOfRange, "DHCPv4 Option4AddrLst " << type_ << " is too big."
70  << "At most 255 bytes are supported.");
74  }
75 
76  buf.writeUint8(type_);
77  buf.writeUint8(len() - getHeaderLen());
78 
79  AddressContainer::const_iterator addr = addrs_.begin();
80 
81  while (addr != addrs_.end()) {
82  buf.writeUint32(addr->toUint32());
83  ++addr;
84  }
85 }
86 
88  if (!addr.isV4()) {
89  isc_throw(BadValue, "Can't store non-IPv4 address in "
90  << "Option4AddrLst option");
91  }
92  addrs_.clear();
93  addAddress(addr);
94 }
95 
97 
98  // Do not copy it as a whole. addAddress() does sanity checks.
99  // i.e. throw if someone tries to set IPv6 address.
100  addrs_.clear();
101  for (AddressContainer::const_iterator addr = addrs.begin();
102  addr != addrs.end(); ++addr) {
103  addAddress(*addr);
104  }
105 }
106 
107 
109  if (!addr.isV4()) {
110  isc_throw(BadValue, "Can't store non-IPv4 address in "
111  << "Option4AddrLst option");
112  }
113  addrs_.push_back(addr);
114 }
115 
116 uint16_t Option4AddrLst::len() const {
117 
118  // Returns length of the complete option (option header + data length)
119  return (getHeaderLen() + addrs_.size() * V4ADDRESS_LEN);
120 }
121 
122 std::string Option4AddrLst::toText(int indent) const {
123  std::stringstream output;
124  output << headerToText(indent) << ":";
125 
126  for (AddressContainer::const_iterator addr = addrs_.begin();
127  addr != addrs_.end(); ++addr) {
128  output << " " << (*addr);
129  }
130 
131  return (output.str());
132 }
133 
134 } // end of isc::dhcp namespace
135 } // end of isc namespace
void addAddress(const isc::asiolink::IOAddress &addr)
Adds address to existing list of addresses.
std::vector< isc::asiolink::IOAddress > AddressContainer
Defines a collection of IPv4 addresses.
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
Option4AddrLst(uint8_t type)
Constructor, creates an option with empty list of addresses.
STL namespace.
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:338
void setAddresses(const AddressContainer &addrs)
Sets addresses list.
#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
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
void setAddress(const isc::asiolink::IOAddress &addr)
Clears address list and sets a single address.
Defines the logger used by the top-level component of kea-dhcp-ddns.
AddressContainer addrs_
contains list of addresses
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 writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition: buffer.h:466
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
virtual std::string toText(int indent=0) const
Returns string representation of the option.
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header) ...
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:569
virtual void pack(isc::util::OutputBuffer &buf) const
Writes option in a wire-format to a buffer.