Kea: lease_query_impl.h Source File

| Kea 3.2.0-git

lease_query_impl.h
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#ifndef LEASE_QUERY_IMPL_H
8#define LEASE_QUERY_IMPL_H
9
10#include <exceptions/exceptions.h>
11#include <asiolink/io_address.h>
12#include <asiolink/io_service.h>
13#include <dhcp/pkt.h>
14#include <dhcpsrv/pool.h>
15#include <cc/data.h>
16#include <cc/simple_parser.h>
17
18#include <boost/functional/hash.hpp>
19#include <unordered_set>
20
21namespace isc {
22namespace lease_query {
23
25 class QueryTerminated : public isc::Exception {
26public:
27 QueryTerminated(const char* file, size_t line, const char* what) :
28 isc::Exception(file, line, what) {
29 }
30};
31
32#define CHECK_TERMINATED \
33 if (LeaseQueryImpl::terminated_) { \
34 isc_throw(isc::lease_query::QueryTerminated, "terminated"); \
35 }
36
38 class AddressList {
39public:
43 AddressList(uint16_t family)
44 : family_(family) { };
45
53 void insert(const isc::asiolink::IOAddress& address);
54
61 bool contains(const isc::asiolink::IOAddress& address) const;
62
64 size_t size() const {
65 return (addresses_.size());
66 }
67
69 uint16_t getFamily() {
70 return (family_);
71 }
72
73private:
75 uint16_t family_;
76
78 std::unordered_set<asiolink::IOAddress, asiolink::IOAddress::Hash> addresses_;
79};
80
82 struct PoolRangeHash {
83 std::size_t operator()(const isc::dhcp::PoolPtr& p) const noexcept {
84 const auto& f = p->getFirstAddress();
85 const auto& l = p->getLastAddress();
86
87 isc::asiolink::IOAddress::Hash haddr;
88 std::size_t h1 = haddr(f);
89 std::size_t h2 = haddr(l);
90
91 // hash_combine
92 return h1 ^ (h2 + 0x9e3779b97f4a7c15ULL + (h1 << 6) + (h1 >> 2));
93 }
94};
95
97 struct PoolRangeEqual {
98 bool operator()(const isc::dhcp::PoolPtr& a,
99 const isc::dhcp::PoolPtr& b) const noexcept {
100 return a->getFirstAddress() == b->getFirstAddress() &&
101 a->getLastAddress() == b->getLastAddress();
102 }
103};
104
106 using PoolRangeSet = std::unordered_set<isc::dhcp::PoolPtr,
107 PoolRangeHash,
108 PoolRangeEqual>;
109
112 class PoolSet {
113public:
117 PoolSet(uint16_t family)
118 : family_(family) { };
119
130 void insert(const isc::asiolink::IOAddress& prefix, uint8_t prefix_len);
131
138 bool contains(const isc::asiolink::IOAddress& address) const;
139
141 size_t size() const {
142 return (pools_.size());
143 }
144
146 uint16_t getFamily() const {
147 return (family_);
148 }
149
150private:
152 uint16_t family_;
153
155 PoolRangeSet pools_;
156};
157
159 class LeaseQueryImpl : public boost::noncopyable {
160public:
165 LeaseQueryImpl(uint16_t family, const isc::data::ConstElementPtr config);
166
168 virtual ~LeaseQueryImpl();
169
175 bool isRequester(const isc::asiolink::IOAddress& address) const;
176
178 size_t getNumRequesters() const {
179 return (address_list_.size());
180 }
181
183 size_t getNumRequesterPools() const {
184 return (pool_set_.size());
185 }
186
199 virtual void processQuery(isc::dhcp::PktPtr base_query,
200 bool& invalid) const = 0;
201
203 static const isc::data::SimpleKeywords LEASE_QUERY_KEYWORDS;
204
206 uint16_t getFamily() {
207 return (address_list_.getFamily());
208 }
209
213 isc::asiolink::IOServicePtr getIOService() {
214 return (io_service_);
215 }
216
220 void setIOService(isc::asiolink::IOServicePtr io_service) {
221 io_service_ = io_service;
222 }
223
225 static bool terminated_;
226
228 static size_t PageSize;
229
230private:
238 void parserRequesters(isc::data::ConstElementPtr requesters);
239
241 isc::asiolink::IOServicePtr io_service_;
242
244 AddressList address_list_;
245
247 PoolSet pool_set_;
248};
249
251 typedef boost::shared_ptr<LeaseQueryImpl> LeaseQueryImplPtr;
252
253} // end of namespace isc::lease_query
254} // end of namespace isc
255
256#endif // LEASE_QUERY_IMPL_H
isc::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition exceptions/exceptions.h:23
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:53
isc::lease_query::AddressList
Manages a unique list of IP addresses.
Definition lease_query_impl.h:38
isc::lease_query::AddressList::size
size_t size() const
Returns the number of addresses in the list.
Definition lease_query_impl.h:64
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::AddressList
AddressList(uint16_t family)
Constructor.
Definition lease_query_impl.h:43
isc::lease_query::AddressList::getFamily
uint16_t getFamily()
Returns the protocol family of the address list.
Definition lease_query_impl.h:69
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::LeaseQueryImpl::getFamily
uint16_t getFamily()
Returns the protocol family of the impl.
Definition lease_query_impl.h:206
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::getNumRequesters
size_t getNumRequesters() const
Returns the number of valid requester.
Definition lease_query_impl.h:178
isc::lease_query::LeaseQueryImpl::getNumRequesterPools
size_t getNumRequesterPools() const
Returns the number of valid requester pools.
Definition lease_query_impl.h:183
isc::lease_query::LeaseQueryImpl::getIOService
isc::asiolink::IOServicePtr getIOService()
Get the hook I/O service.
Definition lease_query_impl.h:213
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::setIOService
void setIOService(isc::asiolink::IOServicePtr io_service)
Set the hook I/O service.
Definition lease_query_impl.h:220
isc::lease_query::LeaseQueryImpl::PageSize
static size_t PageSize
Page size to commands.
Definition lease_query_impl.h:228
isc::lease_query::LeaseQueryImpl::processQuery
virtual void processQuery(isc::dhcp::PktPtr base_query, bool &invalid) const =0
Processes a single client Lease Query.
isc::lease_query::LeaseQueryImpl::~LeaseQueryImpl
virtual ~LeaseQueryImpl()
Destructor.
Definition lease_query_impl.cc:97
isc::lease_query::PoolSet
Manages a unique set of Pools of a given protocol family.
Definition lease_query_impl.h:112
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::size
size_t size() const
Returns the number of pools in the set.
Definition lease_query_impl.h:141
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::PoolSet
PoolSet(uint16_t family)
Constructor.
Definition lease_query_impl.h:117
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
isc::lease_query::QueryTerminated::QueryTerminated
QueryTerminated(const char *file, size_t line, const char *what)
Definition lease_query_impl.h:27
data.h
exceptions.h
io_address.h
io_service.h
simple_parser.h
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition io_service.h:35
isc::config
Definition command_interpreter.cc:23
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::PktPtr
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition pkt.h:999
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::lease_query::PoolRangeSet
std::unordered_set< isc::dhcp::PoolPtr, PoolRangeHash, PoolRangeEqual > PoolRangeSet
Defines an alias for a set of pools hashed by range.
Definition lease_query_impl.h:106
isc::lease_query::LeaseQueryImplPtr
boost::shared_ptr< LeaseQueryImpl > LeaseQueryImplPtr
Defines a smart pointer to LeaseQueryImpl instance.
Definition lease_query_impl.h:251
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
pkt.h
pool.h
isc::asiolink::IOAddress::Hash
An IOAddress hash enabling the use in the unordered STL containers.
Definition io_address.h:58
isc::lease_query::PoolRangeEqual
Equality comparator for two pools based on their address range.
Definition lease_query_impl.h:97
isc::lease_query::PoolRangeEqual::operator()
bool operator()(const isc::dhcp::PoolPtr &a, const isc::dhcp::PoolPtr &b) const noexcept
Definition lease_query_impl.h:98
isc::lease_query::PoolRangeHash
Hash for a Pool based on it's address range.
Definition lease_query_impl.h:82
isc::lease_query::PoolRangeHash::operator()
std::size_t operator()(const isc::dhcp::PoolPtr &p) const noexcept
Definition lease_query_impl.h:83