Kea 3.1.1
ddns_tuning.h
Go to the documentation of this file.
1// Copyright (C) 2022-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 DDNS_TUNING_H
8#define DDNS_TUNING_H
9
11#include <cc/data.h>
12#include <ddns_tuning_log.h>
13#include <dhcpsrv/cfgmgr.h>
14#include <dhcp/libdhcp++.h>
15#include <dhcp/option.h>
16#include <eval/evaluate.h>
17#include <eval/token.h>
18#include <dhcpsrv/subnet.h>
19#include <expression_cache.h>
20
21#include <boost/algorithm/string/split.hpp>
22#include <boost/algorithm/string/classification.hpp>
23
24#include <map>
25#include <string>
26
27namespace isc {
28namespace ddns_tuning {
29
38public:
42 DdnsTuningImpl(uint16_t family = AF_INET) : family_(family) {
43 }
44
47
53
59
64
71
76 void setHostnameExpression(const dhcp::SubnetID& subnet_id, dhcp::ExpressionPtr& expression);
77
103
115
121 dhcp::ExpressionPtr parseExpression(const std::string& expression_str) const;
122
134 std::string calculateHostname(dhcp::PktPtr query,
135 dhcp::ConstSubnetPtr subnet);
136
141 void flushCache(bool preserve_global = true);
142
146 size_t getCacheSize();
147
154 boost::posix_time::ptime getLastFlushTime();
155
157 uint16_t getFamily() {
158 return (family_);
159 }
160
171 template<typename CfgSubnetPtrType>
172 int repopulateCache(const CfgSubnetPtrType cfg_subnets) {
173 size_t err_count = 0;
174 flushCache();
175 auto const& subnets = cfg_subnets->getAll();
176 for (auto const& subnet : *subnets) {
177 try {
178 static_cast<void>(cacheExpression(subnet));
179 } catch (const std::exception& ex) {
180 // Log it but keep going, so we find all that are invalid.
182 .arg(subnet->toText()).arg(ex.what());
183 ++err_count;
184 }
185 }
186
187 return (err_count);
188 }
189
190private:
192 uint16_t family_;
193
194protected:
198};
199
201typedef boost::shared_ptr<DdnsTuningImpl> DdnsTuningImplPtr;
202
203} // end of namespace ddns_tuning
204} // end of namespace isc
205#endif
void configure(isc::data::ConstElementPtr params)
Configure the Ddns Tuning implementation.
dhcp::ExpressionPtr fetchScopedHostnameExpression(dhcp::ConstSubnetPtr subnet)
Fetches the expression that is in scope for the given subnet.
void flushCache(bool preserve_global=true)
Flushes the cache contents.
uint16_t getFamily()
Returns the protocol family of the address list.
DdnsTuningImpl(uint16_t family=AF_INET)
Constructor.
Definition ddns_tuning.h:42
ExpressionCache subnet_exprs_
Per subnet expression cache.
dhcp::ExpressionPtr getHostnameExpression(const dhcp::SubnetID &subnet_id)
Fetches the hostname for a given subnet_id.
void setHostnameExpression(const dhcp::SubnetID &subnet_id, dhcp::ExpressionPtr &expression)
Set the hostname expression for a given subnet.
dhcp::ExpressionPtr parseExpression(const std::string &expression_str) const
Parses an expression string into an Expression.
boost::posix_time::ptime getLastFlushTime()
Fetches the time (in seconds) of when the cache was last flushed.
int repopulateCache(const CfgSubnetPtrType cfg_subnets)
Repopulates the expression cache from the given subnet configuration.
void setGlobalHostnameExpression(dhcp::ExpressionPtr &expression)
Set the global hostname expression.
dhcp::ExpressionPtr getGlobalHostnameExpression()
Get the global hostname expression.
std::string calculateHostname(dhcp::PktPtr query, dhcp::ConstSubnetPtr subnet)
Calculate the hostname for a packet given a subnet.
dhcp::ExpressionPtr cacheExpression(dhcp::ConstSubnetPtr subnet)
Caches an expression entry for a given subnet.
size_t getCacheSize()
Fetches the number of entries in the cache.
ExpressionCache stores expressions per subnet.
const isc::log::MessageID DDNS_TUNING_SUBNET_EXPRESSION_PARSE_ERROR
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::log::Logger ddns_tuning_logger("ddns-tuning-hooks")
boost::shared_ptr< DdnsTuningImpl > DdnsTuningImplPtr
The type of shared pointers to DDNS Tuning implementations.
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition pkt.h:999
boost::shared_ptr< Expression > ExpressionPtr
Definition token.h:31
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
boost::shared_ptr< const Subnet > ConstSubnetPtr
A generic pointer to either const Subnet4 or const Subnet6 object.
Definition subnet.h:452
Defines the logger used by the top-level component of kea-lfc.