]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
ntp: add function for detection of experimental fields
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 21 Sep 2023 09:02:24 +0000 (11:02 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 26 Sep 2023 13:03:33 +0000 (15:03 +0200)
ntp_core.c

index d67c15229670cebaed24630bd7e88a099ba5acb5..0e4157311d0687cd8188f1d6265bd32334dd3373 100644 (file)
@@ -1492,6 +1492,14 @@ is_zero_data(unsigned char *data, int length)
 
 /* ================================================== */
 
+static int
+is_exp_ef(void *body, int body_length, int expected_body_length, uint32_t magic)
+{
+  return body_length == expected_body_length && *(uint32_t *)body == htonl(magic);
+}
+
+/* ================================================== */
+
 static int
 parse_packet(NTP_Packet *packet, int length, NTP_PacketInfo *info)
 {
@@ -1579,8 +1587,8 @@ parse_packet(NTP_Packet *packet, int length, NTP_PacketInfo *info)
         info->auth.mode = NTP_AUTH_NTS;
         break;
       case NTP_EF_EXP_MONO_ROOT:
-        if (ef_body_length == sizeof (NTP_EFExpMonoRoot) &&
-            ntohl(((NTP_EFExpMonoRoot *)ef_body)->magic) == NTP_EF_EXP_MONO_ROOT_MAGIC)
+        if (is_exp_ef(ef_body, ef_body_length, sizeof (NTP_EFExpMonoRoot),
+                      NTP_EF_EXP_MONO_ROOT_MAGIC))
           info->ext_field_flags |= NTP_EF_FLAG_EXP_MONO_ROOT;
         break;
       default:
@@ -1893,8 +1901,8 @@ process_response(NCR_Instance inst, int saved, NTP_Local_Address *local_addr,
       switch (ef_type) {
         case NTP_EF_EXP_MONO_ROOT:
           if (inst->ext_field_flags & NTP_EF_FLAG_EXP_MONO_ROOT &&
-              ef_body_length == sizeof (*ef_mono_root) &&
-              ntohl(((NTP_EFExpMonoRoot *)ef_body)->magic) == NTP_EF_EXP_MONO_ROOT_MAGIC)
+              is_exp_ef(ef_body, ef_body_length, sizeof (*ef_mono_root),
+                        NTP_EF_EXP_MONO_ROOT_MAGIC))
             ef_mono_root = ef_body;
           break;
       }