]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
clientlog: return enum from CLG_LimitServiceRate()
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 2 Apr 2024 09:37:39 +0000 (11:37 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 2 Apr 2024 09:55:02 +0000 (11:55 +0200)
Change CLG_LimitServiceRate() to return an enum in preparation for
adding KoD RATE support.

clientlog.c
clientlog.h
cmdmon.c
ntp_core.c
nts_ke_server.c
test/unit/clientlog.c

index c408e8d4b10525c1dbc643787285f18b00bcad38..5c8a981556083143bd8c67064957b68f5c3a1b1c 100644 (file)
@@ -600,7 +600,7 @@ limit_response_random(int leak_rate)
 
 /* ================================================== */
 
-int
+CLG_Limit
 CLG_LimitServiceRate(CLG_Service service, int index)
 {
   Record *record;
@@ -609,14 +609,14 @@ CLG_LimitServiceRate(CLG_Service service, int index)
   check_service_number(service);
 
   if (tokens_per_hit[service] == 0)
-    return 0;
+    return CLG_PASS;
 
   record = ARR_GetElement(records, index);
   record->drop_flags &= ~(1U << service);
 
   if (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]);
@@ -632,14 +632,14 @@ CLG_LimitServiceRate(CLG_Service service, int index)
 
   if (!drop) {
     record->tokens[service] = 0;
-    return 0;
+    return CLG_PASS;
   }
 
   record->drop_flags |= 1U << service;
   record->drops[service]++;
   total_drops[service]++;
 
-  return 1;
+  return CLG_DROP;
 }
 
 /* ================================================== */
index 9ea0a3fbb670c1b6c67502957db45d9603217871..0d7df002adfdc0f98eb233f65a3132401f3fd303 100644 (file)
@@ -37,11 +37,16 @@ typedef enum {
   CLG_CMDMON,
 } CLG_Service;
 
+typedef enum {
+  CLG_PASS = 0,
+  CLG_DROP,
+} CLG_Limit;
+
 extern void CLG_Initialise(void);
 extern void CLG_Finalise(void);
 extern int CLG_GetClientIndex(IPAddr *client);
 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,
                                NTP_Timestamp_Source tx_ts_src);
 extern int CLG_GetNtpMinPoll(void);
index 716775f6f348c90e63474573cf3105f2b788cb93..b2cdc14a31fc7e1ce4cd7807343cecc39b12e73c 100644 (file)
--- a/cmdmon.c
+++ b/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
      is excessive */
-  if (!localhost && log_index >= 0 && CLG_LimitServiceRate(CLG_CMDMON, log_index)) {
-      DEBUG_LOG("Command packet discarded to limit response rate");
-      return;
+  if (!localhost && log_index >= 0 &&
+      CLG_LimitServiceRate(CLG_CMDMON, log_index) != CLG_PASS) {
+    DEBUG_LOG("Command packet discarded to limit response rate");
+    return;
   }
 
   expected_length = PKL_CommandLength(&rx_message);
index 99360dec62eafc81645f1ac5996300abcb76373d..6aa8e18eb4166018ae2da74a0d156aaf9c89b659 100644 (file)
@@ -2656,6 +2656,7 @@ NCR_ProcessRxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_a
   NTP_Local_Timestamp local_tx, *tx_ts;
   NTP_int64 ntp_rx, *local_ntp_rx;
   int log_index, interleaved, poll, version;
+  CLG_Limit limit;
   uint32_t kod;
 
   /* 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);
 
   /* 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");
       return;
   }
index 3fe99dbf7311819e4c19315bc633231c08175ed4..6d617b47827780b71b9c679e9143e175c6517a88 100644 (file)
@@ -242,7 +242,7 @@ accept_connection(int listening_fd, int event, void *arg)
   SCH_GetLastEventTime(&now, NULL, NULL);
 
   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)",
               UTI_IPSockAddrToString(&addr), "rate limit");
     SCK_CloseSocket(sock_fd);
index e5bf1f4b6eacd58cbb0ca992d625e0a883e2e25b..59ec2b74e6d046114361e363a278df990fb9f83b 100644 (file)
@@ -86,7 +86,7 @@ test_unit(void)
     ts.tv_sec += 1;
     index = CLG_LogServiceAccess(s, &ip, &ts);
     TEST_CHECK(index >= 0);
-    if (!CLG_LimitServiceRate(s, index))
+    if (CLG_LimitServiceRate(s, index) == CLG_PASS)
       j++;
   }