| Kea 3.0.2-git

lease_sync_filter.cc
Go to the documentation of this file.
1// Copyright (C) 2023-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 <dhcpsrv/cfgmgr.h>
10#include <ha_config.h>
11#include <lease_sync_filter.h>
12
13 using namespace isc::data;
14 using namespace isc::dhcp;
15
16 namespace isc {
17 namespace ha {
18
19 LeaseSyncFilter::LeaseSyncFilter(const HAServerType& server_type, const HAConfigPtr& config)
20 : server_type_(server_type), config_(config), subnet_ids_() {
21}
22
23 void
24 LeaseSyncFilter::apply() {
25 subnet_ids_.clear();
26 if (server_type_ == HAServerType::DHCPv4) {
27 for (auto const& subnet : *CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll()) {
28 conditionallyApplySubnetFilter(subnet);
29 }
30 } else {
31 for (auto const& subnet : *CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll()) {
32 conditionallyApplySubnetFilter(subnet);
33 }
34 }
35}
36
37 bool
38 LeaseSyncFilter::shouldSync(const LeasePtr& lease) const {
39 return (subnet_ids_.empty() || subnet_ids_.count(lease->subnet_id_));
40}
41
42 void
43LeaseSyncFilter::conditionallyApplySubnetFilter(const SubnetPtr& subnet) {
44 try {
45 auto server_name = HAConfig::getSubnetServerName(subnet);
46 if (!server_name.empty()) {
47 for (auto const& peer : config_->getAllServersConfig()) {
48 if (peer.first == server_name) {
49 subnet_ids_.insert(subnet->getID());
50 return;
51 }
52 }
53 }
54 } catch (...) {
55 // Don't add ID when there was no way to fetch
56 // the server name.
57 }
58}
59
60} // end of namespace isc::ha
61} // end of namespace isc
cfgmgr.h
isc::dhcp::CfgMgr::instance
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition cfgmgr.cc:29
isc::ha::HAConfig::getSubnetServerName
static std::string getSubnetServerName(const dhcp::ConstSubnetPtr &subnet)
Convenience function extracting a value of the ha-server-name parameter from a subnet context.
Definition ha_config.cc:524
isc::ha::LeaseSyncFilter::apply
void apply()
Applies filtering rules based on the current server configuration.
Definition lease_sync_filter.cc:24
isc::ha::LeaseSyncFilter::LeaseSyncFilter
LeaseSyncFilter(const HAServerType &server_type, const HAConfigPtr &config)
Constructor.
Definition lease_sync_filter.cc:19
isc::ha::LeaseSyncFilter::shouldSync
bool shouldSync(const dhcp::LeasePtr &lease) const
Checks if the lease should be accepted or discarded.
Definition lease_sync_filter.cc:38
ha_config.h
lease_sync_filter.h
isc::config
Definition command_interpreter.cc:23
isc::data
Definition base_stamped_element.cc:12
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::SubnetPtr
boost::shared_ptr< Subnet > SubnetPtr
A generic pointer to either Subnet4 or Subnet6 object.
Definition subnet.h:449
isc::dhcp::LeasePtr
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition lease.h:25
isc::ha
Definition command_creator.cc:20
isc::ha::HAConfigPtr
boost::shared_ptr< HAConfig > HAConfigPtr
Pointer to the High Availability configuration structure.
Definition ha_config.h:37
isc::ha::HAServerType
HAServerType
Lists possible server types for which HA service is created.
Definition ha_server_type.h:14
isc::ha::HAServerType::DHCPv4
@ DHCPv4
Definition ha_server_type.h:15
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148