Kea  2.3.3-git
option4_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/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  if (check && addrs_.size() * V4ADDRESS_LEN > 255) {
68  isc_throw(OutOfRange, "DHCPv4 Option4AddrLst " << type_ << " is too big."
69  << "At most 255 bytes are supported.");
70  }
71 
72  buf.writeUint8(type_);
73  buf.writeUint8(len() - getHeaderLen());
74 
75  AddressContainer::const_iterator addr = addrs_.begin();
76 
77  while (addr != addrs_.end()) {
78  buf.writeUint32(addr->toUint32());
79  ++addr;
80  }
81 }
82 
84  if (!addr.isV4()) {
85  isc_throw(BadValue, "Can't store non-IPv4 address in "
86  << "Option4AddrLst option");
87  }
88  addrs_.clear();
89  addAddress(addr);
90 }
91 
93  // Do not copy it as a whole. addAddress() does sanity checks.
94  // i.e. throw if someone tries to set IPv6 address.
95  addrs_.clear();
96  for (AddressContainer::const_iterator addr = addrs.begin();
97  addr != addrs.end(); ++addr) {
98  addAddress(*addr);
99  }
100 }
101 
103  if (!addr.isV4()) {
104  isc_throw(BadValue, "Can't store non-IPv4 address in "
105  << "Option4AddrLst option");
106  }
107  addrs_.push_back(addr);
108 }
109 
110 uint16_t Option4AddrLst::len() const {
111  // Returns length of the complete option (option header + data length)
112  return (getHeaderLen() + addrs_.size() * V4ADDRESS_LEN);
113 }
114 
115 std::string Option4AddrLst::toText(int indent) const {
116  std::stringstream output;
117  output << headerToText(indent) << ":";
118 
119  for (AddressContainer::const_iterator addr = addrs_.begin();
120  addr != addrs_.end(); ++addr) {
121  output << " " << (*addr);
122  }
123 
124  return (output.str());
125 }
126 
127 } // end of isc::dhcp namespace
128 } // 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:321
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...
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
void check() const
A protected method used for option correctness.
Definition: option.cc:90
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-lfc.
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in a wire-format to a buffer.
AddressContainer addrs_
contains list of addresses
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition: buffer.h:466
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header) ...
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
virtual std::string toText(int indent=0) const
Returns string representation of the option.
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:590