Kea 2.5.8
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
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
21using namespace std;
22using namespace isc::util;
23using namespace isc::asiolink;
24
25namespace isc {
26namespace dhcp {
27
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
55Option4AddrLst::Option4AddrLst(uint8_t type, const IOAddress& addr)
56 : Option(V4, type) {
57 setAddress(addr);
58}
59
62 return (cloneInternal<Option4AddrLst>());
63}
64
65void
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 (auto const& addr : addrs) {
97 addAddress(addr);
98 }
99}
100
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
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
std::vector< isc::asiolink::IOAddress > AddressContainer
Defines a collection of IPv4 addresses.
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Option4AddrLst(uint8_t type)
Constructor, creates an option with empty list of addresses.
void setAddresses(const AddressContainer &addrs)
Sets addresses list.
void setAddress(const isc::asiolink::IOAddress &addr)
Clears address list and sets a single address.
AddressContainer addrs_
contains list of addresses
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)
void addAddress(const isc::asiolink::IOAddress &addr)
Adds address to existing list of addresses.
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in a wire-format to a buffer.
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:590
std::string headerToText(const int indent=0, const std::string &type_name="") const
Returns option header in the textual format.
Definition: option.cc:288
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition: option.cc:321
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:347
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition: buffer.h:474
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:528
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
uint32_t readUint32(void const *const buffer, size_t const length)
uint32_t wrapper over readUint.
Definition: io.h:82
Defines the logger used by the top-level component of kea-lfc.