| Kea 3.0.2-git

option4_addrlst.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2024 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.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
32 Option4AddrLst::Option4AddrLst(uint8_t type, const AddressContainer& addrs)
33 : Option(V4, type) {
34 setAddresses(addrs);
35 // don't set addrs_ directly. setAddresses() will do additional checks.
36}
37
38
39 Option4AddrLst::Option4AddrLst(uint8_t type, OptionBufferConstIter first,
40 OptionBufferConstIter last)
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
60 OptionPtr
61 Option4AddrLst::clone() const {
62 return (cloneInternal<Option4AddrLst>());
63}
64
65 void
66 Option4AddrLst::pack(isc::util::OutputBuffer& buf, bool check) const {
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
83 void Option4AddrLst::setAddress(const isc::asiolink::IOAddress& addr) {
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
92 void Option4AddrLst::setAddresses(const AddressContainer& addrs) {
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 (auto const& addr : addrs) {
97 addAddress(addr);
98 }
99}
100
101 void Option4AddrLst::addAddress(const isc::asiolink::IOAddress& addr) {
102 if (!addr.isV4()) {
103 isc_throw(BadValue, "Can't store non-IPv4 address in "
104 << "Option4AddrLst option");
105 }
106 addrs_.push_back(addr);
107}
108
109uint16_t Option4AddrLst::len() const {
110 // Returns length of the complete option (option header + data length)
111 return (getHeaderLen() + addrs_.size() * V4ADDRESS_LEN);
112}
113
114std::string Option4AddrLst::toText(int indent) const {
115 std::stringstream output;
116 output << headerToText(indent) << ":";
117
118 for (auto const& addr : addrs_) {
119 output << " " << addr;
120 }
121
122 return (output.str());
123}
124
125} // end of isc::dhcp namespace
126} // end of isc namespace
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::OutOfRange
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Definition exceptions/exceptions.h:115
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:45
isc::asiolink::IOAddress::isV4
bool isV4() const
Convenience function to check for an IPv4 address.
Definition io_address.h:114
isc::dhcp::Option4AddrLst::AddressContainer
std::vector< isc::asiolink::IOAddress > AddressContainer
Defines a collection of IPv4 addresses.
Definition option4_addrlst.h:38
isc::dhcp::Option4AddrLst::clone
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition option4_addrlst.cc:61
isc::dhcp::Option4AddrLst::Option4AddrLst
Option4AddrLst(uint8_t type)
Constructor, creates an option with empty list of addresses.
Definition option4_addrlst.cc:28
isc::dhcp::Option4AddrLst::setAddresses
void setAddresses(const AddressContainer &addrs)
Sets addresses list.
Definition option4_addrlst.cc:92
isc::dhcp::Option4AddrLst::setAddress
void setAddress(const isc::asiolink::IOAddress &addr)
Clears address list and sets a single address.
Definition option4_addrlst.cc:83
isc::dhcp::Option4AddrLst::addrs_
AddressContainer addrs_
contains list of addresses
Definition option4_addrlst.h:159
isc::dhcp::Option4AddrLst::toText
virtual std::string toText(int indent=0) const
Returns string representation of the option.
Definition option4_addrlst.cc:114
isc::dhcp::Option4AddrLst::len
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header).
Definition option4_addrlst.cc:109
isc::dhcp::Option4AddrLst::addAddress
void addAddress(const isc::asiolink::IOAddress &addr)
Adds address to existing list of addresses.
Definition option4_addrlst.cc:101
isc::dhcp::Option4AddrLst::pack
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in a wire-format to a buffer.
Definition option4_addrlst.cc:66
isc::dhcp::Option::type_
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition option.h:597
isc::dhcp::Option::headerToText
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition option.cc:294
isc::dhcp::Option::getHeaderLen
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6).
Definition option.cc:327
isc::dhcp::Option::V4
@ V4
Definition option.h:90
isc::dhcp::Option::cloneInternal
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition option.h:504
isc::dhcp::Option::Option
Option(Universe u, uint16_t type)
ctor, used for options constructed, usually during transmission
Definition option.cc:39
isc::dhcp::Option::check
void check() const
A protected method used for option correctness.
Definition option.cc:90
isc::util::OutputBuffer
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:346
isc::util::OutputBuffer::writeUint8
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition buffer.h:476
isc::util::OutputBuffer::writeUint32
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:531
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
io.h
io_address.h
isc::asiolink
Definition addr_utilities.cc:170
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::OptionBufferConstIter
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition option.h:30
isc::dhcp::OptionPtr
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
isc::util
Definition time_utils.cc:43
isc::util::readUint32
uint32_t readUint32(void const *const buffer, size_t const length)
uint32_t wrapper over readUint.
Definition io.h:82
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
option4_addrlst.h