Browse Source

Use clearer length-related names

Benjamin Sago 4 years ago
parent
commit
e9535a1a5e

+ 7 - 7
dns/src/record/a.rs

@@ -22,9 +22,9 @@ impl Wire for A {
     const RR_TYPE: u16 = 1;
 
     #[cfg_attr(all(test, feature = "with_mutagen"), ::mutagen::mutate)]
-    fn read(len: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
+    fn read(stated_length: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
         let mut buf = Vec::new();
-        for _ in 0 .. len {
+        for _ in 0 .. stated_length {
             buf.push(c.read_u8()?);
         }
 
@@ -34,8 +34,8 @@ impl Wire for A {
             Ok(Self { address })
         }
         else {
-            warn!("Length is incorrect (record length {:?}, but should be four)", len);
-            Err(WireError::WrongRecordLength { expected: 4, got: len })
+            warn!("Length is incorrect (record length {:?}, but should be four)", stated_length);
+            Err(WireError::WrongRecordLength { stated_length, mandated_length: 4 })
         }
     }
 }
@@ -62,7 +62,7 @@ mod test {
         ];
 
         assert_eq!(A::read(buf.len() as _, &mut Cursor::new(buf)),
-                   Err(WireError::WrongRecordLength { expected: 4, got: 3 }));
+                   Err(WireError::WrongRecordLength { stated_length: 3, mandated_length: 4 }));
     }
 
     #[test]
@@ -73,13 +73,13 @@ mod test {
         ];
 
         assert_eq!(A::read(buf.len() as _, &mut Cursor::new(buf)),
-                   Err(WireError::WrongRecordLength { expected: 4, got: 5 }));
+                   Err(WireError::WrongRecordLength { stated_length: 5, mandated_length: 4 }));
     }
 
     #[test]
     fn record_empty() {
         assert_eq!(A::read(0, &mut Cursor::new(&[])),
-                   Err(WireError::WrongRecordLength { expected: 4, got: 0 }));
+                   Err(WireError::WrongRecordLength { stated_length: 0, mandated_length: 4 }));
     }
 
     #[test]

+ 7 - 7
dns/src/record/aaaa.rs

@@ -22,9 +22,9 @@ impl Wire for AAAA {
     const RR_TYPE: u16 = 28;
 
     #[cfg_attr(all(test, feature = "with_mutagen"), ::mutagen::mutate)]
-    fn read(len: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
+    fn read(stated_length: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
         let mut buf = Vec::new();
-        for _ in 0 .. len {
+        for _ in 0 .. stated_length {
             buf.push(c.read_u8()?);
         }
 
@@ -36,8 +36,8 @@ impl Wire for AAAA {
             Ok(Self { address })
         }
         else {
-            warn!("Length is incorrect (record length {:?}, but should be sixteen)", len);
-            Err(WireError::WrongRecordLength { expected: 16, got: len })
+            warn!("Length is incorrect (stated length {:?}, but should be sixteen)", stated_length);
+            Err(WireError::WrongRecordLength { stated_length, mandated_length: 16 })
         }
     }
 }
@@ -67,7 +67,7 @@ mod test {
         ];
 
         assert_eq!(AAAA::read(buf.len() as _, &mut Cursor::new(buf)),
-                   Err(WireError::WrongRecordLength { expected: 16, got: 17 }));
+                   Err(WireError::WrongRecordLength { stated_length: 17, mandated_length: 16 }));
     }
 
     #[test]
@@ -77,13 +77,13 @@ mod test {
         ];
 
         assert_eq!(AAAA::read(buf.len() as _, &mut Cursor::new(buf)),
-                   Err(WireError::WrongRecordLength { expected: 16, got: 5 }));
+                   Err(WireError::WrongRecordLength { stated_length: 5, mandated_length: 16 }));
     }
 
     #[test]
     fn record_empty() {
         assert_eq!(AAAA::read(0, &mut Cursor::new(&[])),
-                   Err(WireError::WrongRecordLength { expected: 16, got: 0 }));
+                   Err(WireError::WrongRecordLength { stated_length: 0, mandated_length: 16 }));
     }
 
     #[test]

+ 6 - 6
dns/src/record/caa.rs

@@ -27,7 +27,7 @@ impl Wire for CAA {
     const RR_TYPE: u16 = 257;
 
     #[cfg_attr(all(test, feature = "with_mutagen"), ::mutagen::mutate)]
-    fn read(len: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
+    fn read(stated_length: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
         let flags = c.read_u8()?;
         trace!("Parsed flags -> {:#08b}", flags);
 
@@ -46,7 +46,7 @@ impl Wire for CAA {
         let tag = String::from_utf8_lossy(&tag_buf).to_string();
         trace!("Parsed tag -> {:?}", tag);
 
-        let remaining_length = len.saturating_sub(u16::from(tag_length)).saturating_sub(2);
+        let remaining_length = stated_length.saturating_sub(u16::from(tag_length)).saturating_sub(2);
         trace!("Remaining length -> {:?}", remaining_length);
 
         let mut value_buf = Vec::new();
@@ -57,15 +57,15 @@ impl Wire for CAA {
         let value = String::from_utf8_lossy(&value_buf).to_string();
         trace!("Parsed value -> {:?}", value);
 
-        let got_len = 1 + 1 + u16::from(tag_length) + remaining_length;
-        if len == got_len {
+        let got_length = 1 + 1 + u16::from(tag_length) + remaining_length;
+        if stated_length == got_length {
             // This one’s a little weird, because remaining_len is based on len
             trace!("Length is correct");
             Ok(Self { critical, tag, value })
         }
         else {
-            warn!("Length is incorrect (record length {:?}, flags plus tag plus data length {:?}", len, got_len);
-            Err(WireError::WrongLabelLength { expected: len, got: got_len })
+            warn!("Length is incorrect (stated length {:?}, flags plus tag plus data length {:?}", stated_length, got_length);
+            Err(WireError::WrongLabelLength { stated_length, length_after_labels: got_length })
         }
     }
 }

+ 6 - 6
dns/src/record/cname.rs

@@ -21,17 +21,17 @@ impl Wire for CNAME {
     const RR_TYPE: u16 = 5;
 
     #[cfg_attr(all(test, feature = "with_mutagen"), ::mutagen::mutate)]
-    fn read(len: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
-        let (domain, domain_len) = c.read_labels()?;
+    fn read(stated_length: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
+        let (domain, domain_length) = c.read_labels()?;
         trace!("Parsed domain -> {:?}", domain);
 
-        if len == domain_len {
+        if stated_length == domain_length {
             trace!("Length is correct");
             Ok(Self { domain })
         }
         else {
-            warn!("Length is incorrect (record length {:?}, domain length {:?})", len, domain_len);
-            Err(WireError::WrongLabelLength { expected: len, got: domain_len })
+            warn!("Length is incorrect (stated length {:?}, domain length {:?})", stated_length, domain_length);
+            Err(WireError::WrongLabelLength { stated_length, length_after_labels: domain_length })
         }
     }
 }
@@ -62,7 +62,7 @@ mod test {
         ];
 
         assert_eq!(CNAME::read(6, &mut Cursor::new(buf)),
-                   Err(WireError::WrongLabelLength { expected: 6, got: 5 }));
+                   Err(WireError::WrongLabelLength { stated_length: 6, length_after_labels: 5 }));
     }
 
     #[test]

+ 7 - 7
dns/src/record/mx.rs

@@ -26,21 +26,21 @@ impl Wire for MX {
     const RR_TYPE: u16 = 15;
 
     #[cfg_attr(all(test, feature = "with_mutagen"), ::mutagen::mutate)]
-    fn read(len: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
+    fn read(stated_length: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
         let preference = c.read_u16::<BigEndian>()?;
         trace!("Parsed preference -> {:?}", preference);
 
-        let (exchange, exchange_len) = c.read_labels()?;
+        let (exchange, exchange_length) = c.read_labels()?;
         trace!("Parsed exchange -> {:?}", exchange);
 
-        let got_len = 2 + exchange_len;
-        if len == got_len {
+        let length_after_labels = 2 + exchange_length;
+        if stated_length == length_after_labels {
             trace!("Length is correct");
             Ok(Self { preference, exchange })
         }
         else {
-            warn!("Length is incorrect (record length {:?}, preference plus exchange length {:?}", len, got_len);
-            Err(WireError::WrongLabelLength { expected: len, got: got_len })
+            warn!("Length is incorrect (stated length {:?}, preference plus exchange length {:?}", stated_length, length_after_labels);
+            Err(WireError::WrongLabelLength { stated_length, length_after_labels })
         }
     }
 }
@@ -74,7 +74,7 @@ mod test {
         ];
 
         assert_eq!(MX::read(6, &mut Cursor::new(buf)),
-                   Err(WireError::WrongLabelLength { expected: 6, got: 7 }));
+                   Err(WireError::WrongLabelLength { stated_length: 6, length_after_labels: 7 }));
     }
 
     #[test]

+ 6 - 6
dns/src/record/ns.rs

@@ -22,17 +22,17 @@ impl Wire for NS {
     const RR_TYPE: u16 = 2;
 
     #[cfg_attr(all(test, feature = "with_mutagen"), ::mutagen::mutate)]
-    fn read(len: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
-        let (nameserver, nameserver_len) = c.read_labels()?;
+    fn read(stated_length: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
+        let (nameserver, nameserver_length) = c.read_labels()?;
         trace!("Parsed nameserver -> {:?}", nameserver);
 
-        if len == nameserver_len {
+        if stated_length == nameserver_length {
             trace!("Length is correct");
             Ok(Self { nameserver })
         }
         else {
-            warn!("Length is incorrect (record length {:?}, nameserver length {:?}", len, nameserver_len);
-            Err(WireError::WrongLabelLength { expected: len, got: nameserver_len })
+            warn!("Length is incorrect (stated length {:?}, nameserver length {:?}", stated_length, nameserver_length);
+            Err(WireError::WrongLabelLength { stated_length, length_after_labels: nameserver_length })
         }
     }
 }
@@ -64,7 +64,7 @@ mod test {
         ];
 
         assert_eq!(NS::read(66, &mut Cursor::new(buf)),
-                   Err(WireError::WrongLabelLength { expected: 66, got: 5 }));
+                   Err(WireError::WrongLabelLength { stated_length: 66, length_after_labels: 5 }));
     }
 
     #[test]

+ 6 - 6
dns/src/record/ptr.rs

@@ -27,17 +27,17 @@ impl Wire for PTR {
     const RR_TYPE: u16 = 12;
 
     #[cfg_attr(all(test, feature = "with_mutagen"), ::mutagen::mutate)]
-    fn read(len: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
-        let (cname, cname_len) = c.read_labels()?;
+    fn read(stated_length: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
+        let (cname, cname_length) = c.read_labels()?;
         trace!("Parsed cname -> {:?}", cname);
 
-        if len == cname_len {
+        if stated_length == cname_length {
             trace!("Length is correct");
             Ok(Self { cname })
         }
         else {
-            warn!("Length is incorrect (record length {:?}, cname length {:?}", len, cname_len);
-            Err(WireError::WrongLabelLength { expected: len, got: cname_len })
+            warn!("Length is incorrect (stated length {:?}, cname length {:?}", stated_length, cname_length);
+            Err(WireError::WrongLabelLength { stated_length, length_after_labels: cname_length })
         }
     }
 }
@@ -68,7 +68,7 @@ mod test {
         ];
 
         assert_eq!(PTR::read(6, &mut Cursor::new(buf)),
-                   Err(WireError::WrongLabelLength { expected: 6, got: 5 }));
+                   Err(WireError::WrongLabelLength { stated_length: 6, length_after_labels: 5 }));
     }
 
     #[test]

+ 8 - 8
dns/src/record/soa.rs

@@ -47,11 +47,11 @@ impl Wire for SOA {
 
     #[allow(clippy::similar_names)]
     #[cfg_attr(all(test, feature = "with_mutagen"), ::mutagen::mutate)]
-    fn read(len: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
-        let (mname, mname_len) = c.read_labels()?;
+    fn read(stated_length: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
+        let (mname, mname_length) = c.read_labels()?;
         trace!("Parsed mname -> {:?}", mname);
 
-        let (rname, rname_len) = c.read_labels()?;
+        let (rname, rname_length) = c.read_labels()?;
         trace!("Parsed rname -> {:?}", rname);
 
         let serial = c.read_u32::<BigEndian>()?;
@@ -69,8 +69,8 @@ impl Wire for SOA {
         let minimum_ttl = c.read_u32::<BigEndian>()?;
         trace!("Parsed minimum TTL -> {:?}", minimum_ttl);
 
-        let got_len = 4 * 5 + mname_len + rname_len;
-        if len == got_len {
+        let length_after_labels = 4 * 5 + mname_length + rname_length;
+        if stated_length == length_after_labels {
             trace!("Length is correct");
             Ok(Self {
                 mname, rname, serial, refresh_interval,
@@ -78,8 +78,8 @@ impl Wire for SOA {
             })
         }
         else {
-            warn!("Length is incorrect (record length {:?}, mname plus rname plus fields length {:?})", len, got_len);
-            Err(WireError::WrongLabelLength { expected: len, got: got_len })
+            warn!("Length is incorrect (stated length {:?}, mname plus rname plus fields length {:?})", stated_length, length_after_labels);
+            Err(WireError::WrongLabelLength { stated_length, length_after_labels })
         }
     }
 }
@@ -130,7 +130,7 @@ mod test {
         ];
 
         assert_eq!(SOA::read(89, &mut Cursor::new(buf)),
-                   Err(WireError::WrongLabelLength { expected: 89, got: 30 }));
+                   Err(WireError::WrongLabelLength { stated_length: 89, length_after_labels: 30 }));
     }
 
     #[test]

+ 7 - 7
dns/src/record/srv.rs

@@ -33,7 +33,7 @@ impl Wire for SRV {
     const RR_TYPE: u16 = 33;
 
     #[cfg_attr(all(test, feature = "with_mutagen"), ::mutagen::mutate)]
-    fn read(len: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
+    fn read(stated_length: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
         let priority = c.read_u16::<BigEndian>()?;
         trace!("Parsed priority -> {:?}", priority);
 
@@ -43,17 +43,17 @@ impl Wire for SRV {
         let port = c.read_u16::<BigEndian>()?;
         trace!("Parsed port -> {:?}", port);
 
-        let (target, target_len) = c.read_labels()?;
+        let (target, target_length) = c.read_labels()?;
         trace!("Parsed target -> {:?}", target);
 
-        let got_len = 3 * 2 + target_len;
-        if len == got_len {
+        let length_after_labels = 3 * 2 + target_length;
+        if stated_length == length_after_labels {
             trace!("Length is correct");
             Ok(Self { priority, weight, port, target })
         }
         else {
-            warn!("Length is incorrect (record length {:?}, fields plus target length {:?})", len, got_len);
-            Err(WireError::WrongLabelLength { expected: len, got: got_len })
+            warn!("Length is incorrect (stated length {:?}, fields plus target length {:?})", stated_length, length_after_labels);
+            Err(WireError::WrongLabelLength { stated_length, length_after_labels })
         }
     }
 }
@@ -95,7 +95,7 @@ mod test {
         ];
 
         assert_eq!(SRV::read(16, &mut Cursor::new(buf)),
-                   Err(WireError::WrongLabelLength { expected: 16, got: 11 }));
+                   Err(WireError::WrongLabelLength { stated_length: 16, length_after_labels: 11 }));
     }
 
     #[test]

+ 5 - 5
dns/src/record/txt.rs

@@ -25,7 +25,7 @@ impl Wire for TXT {
     const RR_TYPE: u16 = 16;
 
     #[cfg_attr(all(test, feature = "with_mutagen"), ::mutagen::mutate)]
-    fn read(len: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
+    fn read(stated_length: u16, c: &mut Cursor<&[u8]>) -> Result<Self, WireError> {
         let mut buf = Vec::new();
         let mut total_len = 0_u16;
 
@@ -49,13 +49,13 @@ impl Wire for TXT {
         let message = String::from_utf8_lossy(&buf).to_string();
         trace!("Parsed message -> {:?}", message);
 
-        if len == total_len {
+        if stated_length == total_len {
             trace!("Length is correct");
             Ok(Self { message })
         }
         else {
-            warn!("Length is incorrect (record length {:?}, message length {:?})", len, total_len);
-            Err(WireError::WrongLabelLength { expected: len, got: total_len })
+            warn!("Length is incorrect (stated length {:?}, message length {:?})", stated_length, total_len);
+            Err(WireError::WrongLabelLength { stated_length, length_after_labels: total_len })
         }
     }
 }
@@ -177,7 +177,7 @@ mod test {
         ];
 
         assert_eq!(TXT::read(123, &mut Cursor::new(buf)),
-                   Err(WireError::WrongLabelLength { expected: 123, got: 7 }));
+                   Err(WireError::WrongLabelLength { stated_length: 123, length_after_labels: 7 }));
     }
 
     #[test]

+ 9 - 8
dns/src/wire.rs

@@ -379,11 +379,11 @@ pub enum WireError {
     /// record, whatever it is.
     WrongRecordLength {
 
-        /// The expected size.
-        expected: u16,
+        /// The length of the record’s data, as specified in the packet.
+        stated_length: u16,
 
-        /// The size that was actually received.
-        got: u16,
+        /// The length of the record that the DNS specification mandates.
+        mandated_length: u16,
     },
 
     /// When the length of this record as specified in the packet differs from
@@ -420,11 +420,12 @@ pub enum WireError {
     /// having read a different number of bytes than the specified length.
     WrongLabelLength {
 
-        /// The expected size.
-        expected: u16,
+        /// The length of the record’s data, as specified in the packet.
+        stated_length: u16,
 
-        /// The size that was actually received.
-        got: u16,
+        /// The computed length of the record’s data, based on the number of
+        /// bytes consumed by reading labels from the packet.
+        length_after_labels: u16,
     },
 
     /// When the data contained a string containing a cycle of pointers.

+ 15 - 5
src/output.rs

@@ -442,10 +442,20 @@ fn error_message(error: TransportError) -> String {
 /// wrong with the packet we received.
 fn wire_error_message(error: WireError) -> String {
     match error {
-        WireError::IO                                   => "Malformed packet: insufficient data".into(),
-        WireError::WrongRecordLength { expected, got }  |
-        WireError::WrongLabelLength { expected, got }   => format!("Malformed packet: expected length {}, got {}", expected, got),
-        WireError::TooMuchRecursion(indices)            => format!("Malformed packet: too much recursion: {:?}", indices),
-        WireError::OutOfBounds(index)                   => format!("Malformed packet: out of bounds ({})", index),
+        WireError::IO => {
+            "Malformed packet: insufficient data".into()
+        }
+        WireError::WrongRecordLength { stated_length, mandated_length } => {
+            format!("Malformed packet: record length should be {}, got {}", mandated_length, stated_length )
+        }
+        WireError::WrongLabelLength { stated_length, length_after_labels } => {
+            format!("Malformed packet: length {} was specified, but read {} bytes", stated_length, length_after_labels)
+        }
+        WireError::TooMuchRecursion(indices) => {
+            format!("Malformed packet: too much recursion: {:?}", indices)
+        }
+        WireError::OutOfBounds(index) => {
+            format!("Malformed packet: out of bounds ({})", index)
+        }
     }
 }