clientlog: return enum from CLG_LimitServiceRate()
Change CLG_LimitServiceRate() to return an enum in preparation for adding KoD RATE support.
This commit is contained in:
parent
ce956c99a8
commit
c8c7f518b1
6 changed files with 20 additions and 12 deletions
10
clientlog.c
10
clientlog.c
|
@ -600,7 +600,7 @@ limit_response_random(int leak_rate)
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
int
|
CLG_Limit
|
||||||
CLG_LimitServiceRate(CLG_Service service, int index)
|
CLG_LimitServiceRate(CLG_Service service, int index)
|
||||||
{
|
{
|
||||||
Record *record;
|
Record *record;
|
||||||
|
@ -609,14 +609,14 @@ CLG_LimitServiceRate(CLG_Service service, int index)
|
||||||
check_service_number(service);
|
check_service_number(service);
|
||||||
|
|
||||||
if (tokens_per_hit[service] == 0)
|
if (tokens_per_hit[service] == 0)
|
||||||
return 0;
|
return CLG_PASS;
|
||||||
|
|
||||||
record = ARR_GetElement(records, index);
|
record = ARR_GetElement(records, index);
|
||||||
record->drop_flags &= ~(1U << service);
|
record->drop_flags &= ~(1U << service);
|
||||||
|
|
||||||
if (record->tokens[service] >= tokens_per_hit[service]) {
|
if (record->tokens[service] >= tokens_per_hit[service]) {
|
||||||
record->tokens[service] -= tokens_per_hit[service];
|
record->tokens[service] -= tokens_per_hit[service];
|
||||||
return 0;
|
return CLG_PASS;
|
||||||
}
|
}
|
||||||
|
|
||||||
drop = limit_response_random(leak_rate[service]);
|
drop = limit_response_random(leak_rate[service]);
|
||||||
|
@ -632,14 +632,14 @@ CLG_LimitServiceRate(CLG_Service service, int index)
|
||||||
|
|
||||||
if (!drop) {
|
if (!drop) {
|
||||||
record->tokens[service] = 0;
|
record->tokens[service] = 0;
|
||||||
return 0;
|
return CLG_PASS;
|
||||||
}
|
}
|
||||||
|
|
||||||
record->drop_flags |= 1U << service;
|
record->drop_flags |= 1U << service;
|
||||||
record->drops[service]++;
|
record->drops[service]++;
|
||||||
total_drops[service]++;
|
total_drops[service]++;
|
||||||
|
|
||||||
return 1;
|
return CLG_DROP;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
|
@ -37,11 +37,16 @@ typedef enum {
|
||||||
CLG_CMDMON,
|
CLG_CMDMON,
|
||||||
} CLG_Service;
|
} CLG_Service;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
CLG_PASS = 0,
|
||||||
|
CLG_DROP,
|
||||||
|
} CLG_Limit;
|
||||||
|
|
||||||
extern void CLG_Initialise(void);
|
extern void CLG_Initialise(void);
|
||||||
extern void CLG_Finalise(void);
|
extern void CLG_Finalise(void);
|
||||||
extern int CLG_GetClientIndex(IPAddr *client);
|
extern int CLG_GetClientIndex(IPAddr *client);
|
||||||
extern int CLG_LogServiceAccess(CLG_Service service, IPAddr *client, struct timespec *now);
|
extern int CLG_LogServiceAccess(CLG_Service service, IPAddr *client, struct timespec *now);
|
||||||
extern int CLG_LimitServiceRate(CLG_Service service, int index);
|
extern CLG_Limit CLG_LimitServiceRate(CLG_Service service, int index);
|
||||||
extern void CLG_UpdateNtpStats(int auth, NTP_Timestamp_Source rx_ts_src,
|
extern void CLG_UpdateNtpStats(int auth, NTP_Timestamp_Source rx_ts_src,
|
||||||
NTP_Timestamp_Source tx_ts_src);
|
NTP_Timestamp_Source tx_ts_src);
|
||||||
extern int CLG_GetNtpMinPoll(void);
|
extern int CLG_GetNtpMinPoll(void);
|
||||||
|
|
7
cmdmon.c
7
cmdmon.c
|
@ -1511,9 +1511,10 @@ read_from_cmd_socket(int sock_fd, int event, void *anything)
|
||||||
|
|
||||||
/* Don't reply to all requests from hosts other than localhost if the rate
|
/* Don't reply to all requests from hosts other than localhost if the rate
|
||||||
is excessive */
|
is excessive */
|
||||||
if (!localhost && log_index >= 0 && CLG_LimitServiceRate(CLG_CMDMON, log_index)) {
|
if (!localhost && log_index >= 0 &&
|
||||||
DEBUG_LOG("Command packet discarded to limit response rate");
|
CLG_LimitServiceRate(CLG_CMDMON, log_index) != CLG_PASS) {
|
||||||
return;
|
DEBUG_LOG("Command packet discarded to limit response rate");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
expected_length = PKL_CommandLength(&rx_message);
|
expected_length = PKL_CommandLength(&rx_message);
|
||||||
|
|
|
@ -2656,6 +2656,7 @@ NCR_ProcessRxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_a
|
||||||
NTP_Local_Timestamp local_tx, *tx_ts;
|
NTP_Local_Timestamp local_tx, *tx_ts;
|
||||||
NTP_int64 ntp_rx, *local_ntp_rx;
|
NTP_int64 ntp_rx, *local_ntp_rx;
|
||||||
int log_index, interleaved, poll, version;
|
int log_index, interleaved, poll, version;
|
||||||
|
CLG_Limit limit;
|
||||||
uint32_t kod;
|
uint32_t kod;
|
||||||
|
|
||||||
/* Ignore the packet if it wasn't received by server socket */
|
/* Ignore the packet if it wasn't received by server socket */
|
||||||
|
@ -2701,7 +2702,8 @@ NCR_ProcessRxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_a
|
||||||
log_index = CLG_LogServiceAccess(CLG_NTP, &remote_addr->ip_addr, &rx_ts->ts);
|
log_index = CLG_LogServiceAccess(CLG_NTP, &remote_addr->ip_addr, &rx_ts->ts);
|
||||||
|
|
||||||
/* Don't reply to all requests if the rate is excessive */
|
/* Don't reply to all requests if the rate is excessive */
|
||||||
if (log_index >= 0 && CLG_LimitServiceRate(CLG_NTP, log_index)) {
|
limit = log_index >= 0 ? CLG_LimitServiceRate(CLG_NTP, log_index) : CLG_PASS;
|
||||||
|
if (limit == CLG_DROP) {
|
||||||
DEBUG_LOG("NTP packet discarded to limit response rate");
|
DEBUG_LOG("NTP packet discarded to limit response rate");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -242,7 +242,7 @@ accept_connection(int listening_fd, int event, void *arg)
|
||||||
SCH_GetLastEventTime(&now, NULL, NULL);
|
SCH_GetLastEventTime(&now, NULL, NULL);
|
||||||
|
|
||||||
log_index = CLG_LogServiceAccess(CLG_NTSKE, &addr.ip_addr, &now);
|
log_index = CLG_LogServiceAccess(CLG_NTSKE, &addr.ip_addr, &now);
|
||||||
if (log_index >= 0 && CLG_LimitServiceRate(CLG_NTSKE, log_index)) {
|
if (log_index >= 0 && CLG_LimitServiceRate(CLG_NTSKE, log_index) != CLG_PASS) {
|
||||||
DEBUG_LOG("Rejected connection from %s (%s)",
|
DEBUG_LOG("Rejected connection from %s (%s)",
|
||||||
UTI_IPSockAddrToString(&addr), "rate limit");
|
UTI_IPSockAddrToString(&addr), "rate limit");
|
||||||
SCK_CloseSocket(sock_fd);
|
SCK_CloseSocket(sock_fd);
|
||||||
|
|
|
@ -86,7 +86,7 @@ test_unit(void)
|
||||||
ts.tv_sec += 1;
|
ts.tv_sec += 1;
|
||||||
index = CLG_LogServiceAccess(s, &ip, &ts);
|
index = CLG_LogServiceAccess(s, &ip, &ts);
|
||||||
TEST_CHECK(index >= 0);
|
TEST_CHECK(index >= 0);
|
||||||
if (!CLG_LimitServiceRate(s, index))
|
if (CLG_LimitServiceRate(s, index) == CLG_PASS)
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue