Kea 3.1.4
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
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
74{
75 { "requesters", Element::list },
76 { "advanced", Element::map },
77 { "comment", Element::string },
78 { "prefix-lengths", Element::list }, // v6 only
79};
80
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
98 io_service_->stopAndPoll();
99}
100
101void
102LeaseQueryImpl::parserRequesters(ConstElementPtr requesters) {
103 if (!requesters || (requesters->getType() != Element::list)) {
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
151 if (address_list_.contains(address)) {
152 return (true);
153 }
154
155 return (pool_set_.contains(address));
156}
157
158bool
160
161size_t
if(!(yy_init))
@ map
Definition data.h:147
@ list
Definition data.h:146
@ string
Definition data.h:144
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
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
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:352
bool contains(const isc::asiolink::IOAddress &address) const
Checks if an address is present in the list.
void insert(const isc::asiolink::IOAddress &address)
Inserts an address into the list.
static void create(LeaseQueryImpl *impl, isc::data::ConstElementPtr advanced)
Create a new instance of the BulkLeaseQueryService.
static bool terminated_
Terminated flag.
LeaseQueryImpl(uint16_t family, const isc::data::ConstElementPtr config)
Constructor.
static const isc::data::SimpleKeywords LEASE_QUERY_KEYWORDS
Keywords for Lease Query configuration.
bool isRequester(const isc::asiolink::IOAddress &address) const
Checks if the given address belongs to a valid requester.
static size_t PageSize
Page size to commands.
uint16_t getFamily() const
Returns the protocol family of the address set.
void insert(const isc::asiolink::IOAddress &prefix, uint8_t prefix_len)
Inserts an pool into the set.
bool contains(const isc::asiolink::IOAddress &address) const
Checks if an address is present in the set.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
std::map< std::string, isc::data::Element::types > SimpleKeywords
This specifies all accepted keywords with their types.
boost::shared_ptr< Pool > PoolPtr
a pointer to either IPv4 or IPv6 Pool
Definition pool.h:726
Defines the logger used by the top-level component of kea-lfc.
@ TYPE_NA
the lease contains non-temporary IPv6 address
Definition lease.h:47