Kea: lease_query_impl.cc Source File

| Kea 3.2.0-git

lease_query_impl.cc
Go to the documentation of this file.
1// Copyright (C) 2020-2025 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 <lease_query_impl.h>
10#include <blq_service.h>
11
12using namespace isc;
13using namespace isc::asiolink;
14using namespace isc::data;
15using namespace isc::dhcp;
16using namespace isc::lease_query;
17
18void
19AddressList::insert(const IOAddress& address) {
20 // Check for duplicates as inserts into set do not.
21 // Proper family check is done in by contains().
22 if (contains(address)) {
23 isc_throw(BadValue, "address is already in the list");
24 }
25
26 static_cast<void>(addresses_.insert(address));
27}
28
29bool
30AddressList::contains(const IOAddress& address) const {
31 // Make sure we have the right family.
32 if (address.getFamily() != family_) {
33 isc_throw(BadValue, "not a " << (family_ == AF_INET ? "IPv4" : "IPv6")
34 << " address");
35 }
36
37 return (addresses_.count(address));
38}
39
40void
41PoolSet::insert(const isc::asiolink::IOAddress& prefix, uint8_t prefix_len) {
42 PoolPtr pool;
43 if (getFamily() == AF_INET) {
44 pool = Pool4::create(prefix, prefix_len);
45 } else {
46 pool = Pool6::create(Lease::TYPE_NA, prefix, prefix_len);
47 }
48
49 if (pools_.find(pool) != pools_.end()) {
50 isc_throw(BadValue, "entry already exists");
51 }
52
53 pools_.emplace(pool);
54}
55
56bool
57PoolSet::contains(const IOAddress& address) const {
58 if (address.getFamily() != family_) {
59 isc_throw(BadValue, "not a " << (family_ == AF_INET ? "IPv4" : "IPv6")
60 << " address");
61 }
62
63 for (auto pool : pools_) {
64 if (pool->inRange(address)) {
65 return (true);
66 }
67 }
68
69 return (false);
70}
71
72const SimpleKeywords
73 LeaseQueryImpl::LEASE_QUERY_KEYWORDS =
74{
75 { "requesters", Element::list },
76 { "advanced", Element::map },
77 { "comment", Element::string },
78 { "prefix-lengths", Element::list }, // v6 only
79};
80
81LeaseQueryImpl::LeaseQueryImpl(uint16_t family,
82 const ConstElementPtr config)
83 : io_service_(new IOService()), address_list_(family), pool_set_(family) {
84
85 if (!config || (config->getType() != Element::map)) {
86 isc_throw(BadValue, "Lease Query config is empty or not a map");
87 }
88
89 parserRequesters(config->get("requesters"));
90
91 ConstElementPtr advanced = config->get("advanced");
92 if (advanced) {
93 BulkLeaseQueryService::create(this, advanced);
94 }
95}
96
97LeaseQueryImpl::~LeaseQueryImpl() {
98 io_service_->stopAndPoll();
99}
100
101void
102LeaseQueryImpl::parserRequesters(ConstElementPtr requesters) {
103 if (!requesters || (requesters->getType() != Element::list)) {
104 isc_throw(BadValue,
105 "'requesters' address list is missing or not a list");
106 }
107
108 for (auto const& address_elem : requesters->listValue()) {
109 auto entry_txt = address_elem->stringValue();
110 // Is this just an address or is it CIDR?
111 size_t pos = entry_txt.find("/");
112 if (pos == std::string::npos) {
113 try {
114 IOAddress address(entry_txt);
115 address_list_.insert(address);
116 } catch (const std::exception& ex) {
117 isc_throw(BadValue, "'requesters' address entry '"
118 << address_elem->stringValue()
119 << "' is invalid: " << ex.what());
120 }
121 } else {
122 try {
123 IOAddress prefix = IOAddress(entry_txt.substr(0, pos));
124
125 // start with the first character after /
126 auto len_txt = entry_txt.substr(pos + 1);
127 int prefix_len = boost::lexical_cast<int>(len_txt);
128 if ((prefix_len < std::numeric_limits<uint8_t>::min()) ||
129 (prefix_len > std::numeric_limits<uint8_t>::max())) {
130 // This exception will be handled 4 line later!
131 isc_throw(OutOfRange, "prefix length "
132 << len_txt << " is out of range");
133 }
134
135 pool_set_.insert(prefix, prefix_len);
136 } catch (const std::exception& ex) {
137 isc_throw(BadValue,
138 "'requesters' CIDR entry '" << entry_txt
139 << "' is invalid: " << ex.what());
140 }
141 }
142 }
143
144 if (address_list_.size() == 0 && pool_set_.size() == 0) {
145 isc_throw(BadValue, "'requesters' list cannot be empty");
146 }
147}
148
149bool
150LeaseQueryImpl::isRequester(const IOAddress& address) const {
151 if (address_list_.contains(address)) {
152 return (true);
153 }
154
155 return (pool_set_.contains(address));
156}
157
158bool
159 LeaseQueryImpl::terminated_ = false;
160
161size_t
162 LeaseQueryImpl::PageSize = 100;
blq_service.h
Element::map
@ map
Definition data.h:160
Element::list
@ list
Definition data.h:159
Element::string
@ string
Definition data.h:157
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::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:53
isc::asiolink::IOAddress::getFamily
short getFamily() const
Returns the address family.
Definition io_address.cc:96
isc::asiolink::IOService
The IOService class is a wrapper for the ASIO io_context class.
Definition io_service.h:39
isc::dhcp::Pool4::create
static Pool4Ptr create(const isc::asiolink::IOAddress &first, const isc::asiolink::IOAddress &last)
Factory function for creating an instance of the Pool4.
Definition pool.cc:127
isc::dhcp::Pool6::create
static Pool6Ptr create(Lease::Type type, const isc::asiolink::IOAddress &first, const isc::asiolink::IOAddress &last)
Factory function for creating an instance of the Pool6.
Definition pool.cc:351
isc::lease_query::AddressList::contains
bool contains(const isc::asiolink::IOAddress &address) const
Checks if an address is present in the list.
Definition lease_query_impl.cc:30
isc::lease_query::AddressList::insert
void insert(const isc::asiolink::IOAddress &address)
Inserts an address into the list.
Definition lease_query_impl.cc:19
isc::lease_query::BulkLeaseQueryService::create
static void create(LeaseQueryImpl *impl, isc::data::ConstElementPtr advanced)
Create a new instance of the BulkLeaseQueryService.
Definition blq_service.cc:230
isc::lease_query::LeaseQueryImpl::terminated_
static bool terminated_
Terminated flag.
Definition lease_query_impl.h:225
isc::lease_query::LeaseQueryImpl::LeaseQueryImpl
LeaseQueryImpl(uint16_t family, const isc::data::ConstElementPtr config)
Constructor.
Definition lease_query_impl.cc:81
isc::lease_query::LeaseQueryImpl::LEASE_QUERY_KEYWORDS
static const isc::data::SimpleKeywords LEASE_QUERY_KEYWORDS
Keywords for Lease Query configuration.
Definition lease_query_impl.h:203
isc::lease_query::LeaseQueryImpl::isRequester
bool isRequester(const isc::asiolink::IOAddress &address) const
Checks if the given address belongs to a valid requester.
Definition lease_query_impl.cc:150
isc::lease_query::LeaseQueryImpl::PageSize
static size_t PageSize
Page size to commands.
Definition lease_query_impl.h:228
isc::lease_query::LeaseQueryImpl::~LeaseQueryImpl
virtual ~LeaseQueryImpl()
Destructor.
Definition lease_query_impl.cc:97
isc::lease_query::PoolSet::getFamily
uint16_t getFamily() const
Returns the protocol family of the address set.
Definition lease_query_impl.h:146
isc::lease_query::PoolSet::insert
void insert(const isc::asiolink::IOAddress &prefix, uint8_t prefix_len)
Inserts an pool into the set.
Definition lease_query_impl.cc:41
isc::lease_query::PoolSet::contains
bool contains(const isc::asiolink::IOAddress &address) const
Checks if an address is present in the set.
Definition lease_query_impl.cc:57
if
if(!(yy_init))
Definition d2_lexer.cc:1515
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
lease_query_impl.h
isc::asiolink
Definition addr_utilities.cc:170
isc::config
Definition command_interpreter.cc:23
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::data::SimpleKeywords
std::map< std::string, isc::data::Element::types > SimpleKeywords
This specifies all accepted keywords with their types.
Definition lib/cc/simple_parser.h:36
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::PoolPtr
boost::shared_ptr< Pool > PoolPtr
a pointer to either IPv4 or IPv6 Pool
Definition pool.h:726
isc::lease_query
Definition blq_msg.h:15
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
isc::dhcp::Lease::TYPE_NA
@ TYPE_NA
the lease contains non-temporary IPv6 address
Definition lease.h:47