]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
cmdmon: make length of manual list constant
authorMiroslav Lichvar <mlichvar@redhat.com>
Mon, 5 Mar 2018 17:37:20 +0000 (18:37 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 6 Mar 2018 12:47:25 +0000 (13:47 +0100)
Make the length of responses containing manual samples constant to
simplify the protocol. It was the only type of response that had a
variable length.

This reverts commit 2343e7a89c0bacdfccf892f881c4b89b2990c512.

candm.h
client.c
cmdmon.c
pktlength.c
pktlength.h

diff --git a/candm.h b/candm.h
index 2ff385373eb8130fcf2fef0967786e8ae670a328..06fcb05103577eeae173c07f19bae45db564c242 100644 (file)
--- a/candm.h
+++ b/candm.h
@@ -470,7 +470,8 @@ typedef struct {
 #define RPY_CLIENT_ACCESSES_BY_INDEX2 15
 #define RPY_NTP_DATA 16
 #define RPY_MANUAL_TIMESTAMP2 17
-#define N_REPLY_TYPES 18
+#define RPY_MANUAL_LIST2 18
+#define N_REPLY_TYPES 19
 
 /* Status codes */
 #define STT_SUCCESS 0
index a38077bcbd74c620eda6d7fe87a1b7513e39a6c7..94522072857dc7b5e5aa6d8a52184087850d6ce8 100644 (file)
--- a/client.c
+++ b/client.c
@@ -1429,9 +1429,14 @@ submit_request(CMD_Request *request, CMD_Reply *reply)
         DEBUG_LOG("Received %d bytes", recv_status);
         
         read_length = recv_status;
-        expected_length = PKL_ReplyLength(reply, read_length);
+        if (read_length >= offsetof(CMD_Reply, data)) {
+          expected_length = PKL_ReplyLength(reply);
+        } else {
+          expected_length = 0;
+        }
 
-        bad_length = !expected_length || read_length < expected_length;
+        bad_length = (read_length < expected_length ||
+                      expected_length < offsetof(CMD_Reply, data));
         
         if (!bad_length) {
           bad_sequence = reply->sequence != request->sequence;
@@ -2539,7 +2544,7 @@ process_cmd_manual_list(const char *line)
   struct timespec when;
 
   request.command = htons(REQ_MANUAL_LIST);
-  if (!request_reply(&request, &reply, RPY_MANUAL_LIST, 0))
+  if (!request_reply(&request, &reply, RPY_MANUAL_LIST2, 0))
     return 0;
 
   n_samples = ntohl(reply.data.manual_list.n_samples);
@@ -2547,7 +2552,7 @@ process_cmd_manual_list(const char *line)
 
   print_header("#    Date     Time(UTC)    Slewed   Original   Residual");
 
-  for (i = 0; i < n_samples; i++) {
+  for (i = 0; i < n_samples && i < MAX_MANUAL_LIST_SAMPLES; i++) {
     sample = &reply.data.manual_list.samples[i];
     UTI_TimespecNetworkToHost(&sample->when, &when);
 
index 85f77de5eeaa84a6485d2925ac38bd03df90bd2b..207165b9fa41ff27a826bcbbfd4e380d8c4bf4d5 100644 (file)
--- a/cmdmon.c
+++ b/cmdmon.c
@@ -279,8 +279,7 @@ do_size_checks(void)
   for (i = 1; i < N_REPLY_TYPES; i++) {
     reply.reply = htons(i);
     reply.status = STT_SUCCESS;
-    reply.data.manual_list.n_samples = htonl(MAX_MANUAL_LIST_SAMPLES);
-    reply_length = PKL_ReplyLength(&reply, sizeof (reply));
+    reply_length = PKL_ReplyLength(&reply);
     if ((reply_length && reply_length < offsetof(CMD_Reply, data)) ||
         reply_length > sizeof (CMD_Reply))
       assert(0);
@@ -394,7 +393,7 @@ transmit_reply(CMD_Reply *msg, union sockaddr_all *where_to)
       assert(0);
   }
 
-  tx_message_length = PKL_ReplyLength(msg, sizeof (*msg));
+  tx_message_length = PKL_ReplyLength(msg);
   status = sendto(sock_fd, (void *) msg, tx_message_length, 0,
                   &where_to->sa, addrlen);
 
@@ -1105,10 +1104,14 @@ handle_manual_list(CMD_Request *rx_message, CMD_Reply *tx_message)
   RPY_ManualListSample *sample;
   RPT_ManualSamplesReport report[MAX_MANUAL_LIST_SAMPLES];
 
-  tx_message->reply = htons(RPY_MANUAL_LIST);
+  tx_message->reply = htons(RPY_MANUAL_LIST2);
   
   MNL_ReportSamples(report, MAX_MANUAL_LIST_SAMPLES, &n_samples);
   tx_message->data.manual_list.n_samples = htonl(n_samples);
+
+  memset(tx_message->data.manual_list.samples, 0,
+         sizeof (tx_message->data.manual_list.samples));
+
   for (i=0; i<n_samples; i++) {
     sample = &tx_message->data.manual_list.samples[i];
     UTI_TimespecHostToNetwork(&report[i].when, &sample->when);
index 112ee26708b3f8c53fe75710a24ed9318d5fe782..14a43f7b6ebda601db41c2f39981be57dac573c2 100644 (file)
@@ -133,13 +133,14 @@ static const uint16_t reply_lengths[] = {
   0,                                            /* SUBNETS_ACCESSED - not supported */
   0,                                            /* CLIENT_ACCESSES - not supported */
   0,                                            /* CLIENT_ACCESSES_BY_INDEX - not supported */
-  0,                                            /* MANUAL_LIST - variable length */
+  0,                                            /* MANUAL_LIST - not supported */
   RPY_LENGTH_ENTRY(activity),                   /* ACTIVITY */
   RPY_LENGTH_ENTRY(smoothing),                  /* SMOOTHING */
   RPY_LENGTH_ENTRY(server_stats),               /* SERVER_STATS */
   RPY_LENGTH_ENTRY(client_accesses_by_index),   /* CLIENT_ACCESSES_BY_INDEX2 */
   RPY_LENGTH_ENTRY(ntp_data),                   /* NTP_DATA */
   RPY_LENGTH_ENTRY(manual_timestamp),           /* MANUAL_TIMESTAMP2 */
+  RPY_LENGTH_ENTRY(manual_list),                /* MANUAL_LIST2 */
 };
 
 /* ================================================== */
@@ -184,39 +185,18 @@ PKL_CommandPaddingLength(CMD_Request *r)
 /* ================================================== */
 
 int
-PKL_ReplyLength(CMD_Reply *r, int read_length)
+PKL_ReplyLength(CMD_Reply *r)
 {
   uint32_t type;
 
   assert(sizeof (reply_lengths) / sizeof (reply_lengths[0]) == N_REPLY_TYPES);
 
-  if (read_length < (int)offsetof(CMD_Reply, data))
-    return 0;
-
   type = ntohs(r->reply);
 
   /* Note that reply type codes start from 1, not 0 */
   if (type < 1 || type >= N_REPLY_TYPES)
     return 0;
 
-  /* Length of MANUAL_LIST depends on number of samples stored in it */
-  if (type == RPY_MANUAL_LIST) {
-    uint32_t ns;
-
-    if (r->status != htons(STT_SUCCESS))
-      return offsetof(CMD_Reply, data);
-
-    if (read_length < (int)offsetof(CMD_Reply, data.manual_list.samples))
-      return 0;
-
-    ns = ntohl(r->data.manual_list.n_samples);
-    if (ns > MAX_MANUAL_LIST_SAMPLES)
-      return 0;
-
-    return offsetof(CMD_Reply, data.manual_list.samples) +
-           ns * sizeof (RPY_ManualListSample);
-  }
-
   return reply_lengths[type];
 }
 
index 40646fe1ed96d5774cc540ae13e8ef63ee82a7ec..fad4c30f730e4923d58e1a6860e1f33c58587bfb 100644 (file)
@@ -35,6 +35,6 @@ extern int PKL_CommandLength(CMD_Request *r);
 
 extern int PKL_CommandPaddingLength(CMD_Request *r);
 
-extern int PKL_ReplyLength(CMD_Reply *r, int read_length);
+extern int PKL_ReplyLength(CMD_Reply *r);
 
 #endif /* GOT_PKTLENGTH_H */