1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (C) 2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

#include <config.h>

#include <cc/dhcp_config_error.h>
#include <exceptions/exceptions.h>
#include <limits/configuration.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <limits/limits_logger.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <log/macros.h>

#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace limits {

using namespace isc::dhcp;

using namespace std;

using isc::data::ConstElementPtr;
using isc::log::DBGLVL_TRACE_BASIC;

RateLimit::RateLimit(string const& text) {
    // Parse string.
    char const* pkt_per(" packets per ");
    size_t index(text.find(pkt_per));
    if (index == text.npos) {
        pkt_per = " packet per ";
        index = text.find(pkt_per);
        if (index == text.npos) {
            isc_throw(ConfigError,
                      "expected rate limit of format <n> packet[s] per <time-unit>, got: " << text);
        }
    }

    // Determine the number of allowed packets.
    int64_t const allowed_packets(stoll(text.substr(0, index)));
    checkForLimitBoundaries<decltype(allowed_packets_)>(allowed_packets);
    allowed_packets_ = allowed_packets;

    // Determine the time unit and convert it to a chrono::duration type.
    string const time_period(text.substr(index + strlen(pkt_per)));
    if (time_period == "second") {
        time_unit_ = chrono::seconds(1);
    } else if (time_period == "minute") {
        time_unit_ = chrono::minutes(1);
    } else if (time_period == "hour") {
        time_unit_ = chrono::hours(1);
    } else if (time_period == "day") {
        time_unit_ = chrono::hours(24);
    } else if (time_period == "week") {
        time_unit_ = chrono::hours(7 * 24);
    } else if (time_period == "month") {
        time_unit_ = chrono::hours(30 * 24);
    } else if (time_period == "year") {
        time_unit_ = chrono::hours(365 * 24);
    } else {
        isc_throw(ConfigError, "invalid time period " << time_period);
    }

    // Keep the entire text, it's used for logging purposes.
    text_ = text;
}

// -- AddressLimitConfiguration --

const std::string&
AddressLimitConfiguration::key() const {
    static std::string limit_str("address-limit");
    return (limit_str);
}

void
AddressLimitConfiguration::logClientClassLimit(ClientClass const& client_class,
                                                       ConstElementPtr const& limit) {
    if (!limit) {
        return;
    }

    int64_t const limit_candidate(limit->intValue());
    checkForLimitBoundaries<LeaseLimit>(limit_candidate);

    LOG_DEBUG(limits_logger, DBGLVL_TRACE_BASIC, LIMITS_CONFIGURED_ADDRESS_LIMIT_BY_CLIENT_CLASS)
        .arg(limit_candidate)
        .arg(client_class);
}

void
AddressLimitConfiguration::logSubnetLimit(SubnetID const subnet_id,
                                          ConstElementPtr const& limit) {
    if (!limit) {
        return;
    }

    int64_t const limit_candidate(limit->intValue());
    checkForLimitBoundaries<LeaseLimit>(limit_candidate);

    LOG_DEBUG(limits_logger, DBGLVL_TRACE_BASIC, LIMITS_CONFIGURED_ADDRESS_LIMIT_BY_SUBNET)
        .arg(limit_candidate)
        .arg(subnet_id);
}

// -- PrefixLimitConfiguration --

const std::string&
PrefixLimitConfiguration::key() const {
    static std::string limit_str ("prefix-limit");
    return (limit_str);
}

void
PrefixLimitConfiguration::logClientClassLimit(ClientClass const& client_class,
                                                      ConstElementPtr const& limit) {
    if (!limit) {
        return;
    }

    int64_t const limit_candidate(limit->intValue());
    checkForLimitBoundaries<LeaseLimit>(limit_candidate);

    LOG_DEBUG(limits_logger, DBGLVL_TRACE_BASIC, LIMITS_CONFIGURED_PREFIX_LIMIT_BY_CLIENT_CLASS)
        .arg(limit_candidate)
        .arg(client_class);
}

void
PrefixLimitConfiguration::logSubnetLimit(SubnetID const subnet_id,
                                         ConstElementPtr const& limit) {
    if (!limit) {
        return;
    }

    int64_t const limit_candidate(limit->intValue());
    checkForLimitBoundaries<LeaseLimit>(limit_candidate);

    LOG_DEBUG(limits_logger, DBGLVL_TRACE_BASIC, LIMITS_CONFIGURED_PREFIX_LIMIT_BY_SUBNET)
        .arg(limit_candidate)
        .arg(subnet_id);
}

// -- RateLimitConfiguration --

const std::string&
RateLimitConfiguration::key() const {
    static std::string limit_str("rate-limit");
    return (limit_str);
}

void
RateLimitConfiguration::logClientClassLimit(ClientClass const& client_class,
                                            ConstElementPtr const& limit) {
    if (!limit) {
        return;
    }

    string const& limit_candidate = limit->stringValue();
    RateLimit rate_limit(limit_candidate);

    LOG_DEBUG(limits_logger, DBGLVL_TRACE_BASIC, LIMITS_CONFIGURED_RATE_LIMIT_BY_CLIENT_CLASS)
        .arg(limit_candidate)
        .arg(client_class);
}

void
RateLimitConfiguration::logSubnetLimit(SubnetID const subnet_id,
                                               ConstElementPtr const& limit) {
    if (!limit) {
        return;
    }

    string const& limit_candidate = limit->stringValue();
    RateLimit rate_limit(limit_candidate);

    LOG_DEBUG(limits_logger, DBGLVL_TRACE_BASIC, LIMITS_CONFIGURED_RATE_LIMIT_BY_SUBNET)
        .arg(limit_candidate)
        .arg(subnet_id);
}

}  // namespace limits
}  // namespace isc