Kea 3.1.1
configuration.cc
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#include <config.h>
8
13#include <log/macros.h>
14
15#include <string>
16
17namespace isc {
18namespace limits {
19
20using namespace isc::dhcp;
21
22using namespace std;
23
26
27RateLimit::RateLimit(string const& text) {
28 // Parse string.
29 char const* pkt_per(" packets per ");
30 size_t index(text.find(pkt_per));
31 if (index == text.npos) {
32 pkt_per = " packet per ";
33 index = text.find(pkt_per);
34 if (index == text.npos) {
36 "expected rate limit of format <n> packet[s] per <time-unit>, got: " << text);
37 }
38 }
39
40 // Determine the number of allowed packets.
41 int64_t const allowed_packets(stoll(text.substr(0, index)));
42 checkForLimitBoundaries<decltype(allowed_packets_)>(allowed_packets);
43 allowed_packets_ = allowed_packets;
44
45 // Determine the time unit and convert it to a chrono::duration type.
46 string const time_period(text.substr(index + strlen(pkt_per)));
47 if (time_period == "second") {
48 time_unit_ = chrono::seconds(1);
49 } else if (time_period == "minute") {
50 time_unit_ = chrono::minutes(1);
51 } else if (time_period == "hour") {
52 time_unit_ = chrono::hours(1);
53 } else if (time_period == "day") {
54 time_unit_ = chrono::hours(24);
55 } else if (time_period == "week") {
56 time_unit_ = chrono::hours(7 * 24);
57 } else if (time_period == "month") {
58 time_unit_ = chrono::hours(30 * 24);
59 } else if (time_period == "year") {
60 time_unit_ = chrono::hours(365 * 24);
61 } else {
62 isc_throw(ConfigError, "invalid time period " << time_period);
63 }
64
65 // Keep the entire text, it's used for logging purposes.
66 text_ = text;
67}
68
69// -- AddressLimitConfiguration --
70
71const std::string&
73 static std::string limit_str("address-limit");
74 return (limit_str);
75}
76
77void
79 ConstElementPtr const& limit) {
80 if (!limit) {
81 return;
82 }
83
84 int64_t const limit_candidate(limit->intValue());
85 checkForLimitBoundaries<LeaseLimit>(limit_candidate);
86
88 .arg(limit_candidate)
89 .arg(client_class);
90}
91
92void
94 ConstElementPtr const& limit) {
95 if (!limit) {
96 return;
97 }
98
99 int64_t const limit_candidate(limit->intValue());
100 checkForLimitBoundaries<LeaseLimit>(limit_candidate);
101
103 .arg(limit_candidate)
104 .arg(subnet_id);
105}
106
107// -- PrefixLimitConfiguration --
108
109const std::string&
111 static std::string limit_str ("prefix-limit");
112 return (limit_str);
113}
114
115void
117 ConstElementPtr const& limit) {
118 if (!limit) {
119 return;
120 }
121
122 int64_t const limit_candidate(limit->intValue());
123 checkForLimitBoundaries<LeaseLimit>(limit_candidate);
124
126 .arg(limit_candidate)
127 .arg(client_class);
128}
129
130void
132 ConstElementPtr const& limit) {
133 if (!limit) {
134 return;
135 }
136
137 int64_t const limit_candidate(limit->intValue());
138 checkForLimitBoundaries<LeaseLimit>(limit_candidate);
139
141 .arg(limit_candidate)
142 .arg(subnet_id);
143}
144
145// -- RateLimitConfiguration --
146
147const std::string&
149 static std::string limit_str("rate-limit");
150 return (limit_str);
151}
152
153void
155 ConstElementPtr const& limit) {
156 if (!limit) {
157 return;
158 }
159
160 string const& limit_candidate = limit->stringValue();
161 RateLimit rate_limit(limit_candidate);
162
164 .arg(limit_candidate)
165 .arg(client_class);
166}
167
168void
170 ConstElementPtr const& limit) {
171 if (!limit) {
172 return;
173 }
174
175 string const& limit_candidate = limit->stringValue();
176 RateLimit rate_limit(limit_candidate);
177
179 .arg(limit_candidate)
180 .arg(subnet_id);
181}
182
183} // namespace limits
184} // namespace isc
An exception that is thrown if an error occurs while configuring any server.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
std::string ClientClass
Defines a single class name.
Definition classify.h:44
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
const int DBGLVL_TRACE_BASIC
Trace basic operations.
const isc::log::MessageID LIMITS_CONFIGURED_PREFIX_LIMIT_BY_SUBNET
const isc::log::MessageID LIMITS_CONFIGURED_ADDRESS_LIMIT_BY_SUBNET
const isc::log::MessageID LIMITS_CONFIGURED_RATE_LIMIT_BY_SUBNET
const isc::log::MessageID LIMITS_CONFIGURED_RATE_LIMIT_BY_CLIENT_CLASS
const isc::log::MessageID LIMITS_CONFIGURED_ADDRESS_LIMIT_BY_CLIENT_CLASS
const isc::log::MessageID LIMITS_CONFIGURED_PREFIX_LIMIT_BY_CLIENT_CLASS
isc::log::Logger limits_logger("limits-hooks")
const int DBGLVL_TRACE_BASIC
Trace basic operations.
Defines the logger used by the top-level component of kea-lfc.
virtual void logClientClassLimit(isc::dhcp::ClientClass const &client_class, isc::data::ConstElementPtr const &user_context) override
Method that checks and logs limit which is to be applied to a client class.
virtual void logSubnetLimit(isc::dhcp::SubnetID const subnet_id, isc::data::ConstElementPtr const &user_context) override
Method that checks and logs limit which is to be applied to a client class.
const std::string & key() const override
Returns the key required to configure the limit in the user context.
virtual void logClientClassLimit(isc::dhcp::ClientClass const &client_class, isc::data::ConstElementPtr const &user_context) override
Method that checks and logs limit which is to be applied to a client class.
virtual void logSubnetLimit(isc::dhcp::SubnetID const subnet_id, isc::data::ConstElementPtr const &user_context) override
Method that checks and logs limit which is to be applied to a client class.
const std::string & key() const override
Returns the key required to configure the limit in the user context.
virtual void logClientClassLimit(isc::dhcp::ClientClass const &client_class, isc::data::ConstElementPtr const &user_context) override
Method that checks and logs limit which is to be applied to a client class.
const std::string & key() const override
Returns the key required to configure the limit in the user context.
virtual void logSubnetLimit(isc::dhcp::SubnetID const subnet_id, isc::data::ConstElementPtr const &user_context) override
Method that checks and logs limit which is to be applied to a client class.
a single rate-limiting entry configured as "rate-limit": "<n> packet[s] per <time-unit>"
std::string text_
a string representation of the rate limit as specified in the configuration used for logging purposes
RateLimit()
Constructor.
std::chrono::seconds time_unit_
Seconds of one time unit's worth.
uint32_t allowed_packets_
the configured limit