From d6c6d0b2f85a00a10b85e09b2b1d4db3b1a443db Mon Sep 17 00:00:00 2001 From: Ximon Eighteen <3304436+ximon18@users.noreply.github.com> Date: Tue, 23 Jul 2024 10:55:41 +0200 Subject: [PATCH] Add `OptRcode::is_ext()` to test if an Rcode is extended per RFC 6891. (#358) --- src/base/iana/rcode.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/base/iana/rcode.rs b/src/base/iana/rcode.rs index 963f7a9d..64252aed 100644 --- a/src/base/iana/rcode.rs +++ b/src/base/iana/rcode.rs @@ -558,6 +558,20 @@ impl OptRcode { self.to_parts().1 } + /// Returns true if the RCODE is extended, false otherwise. + #[must_use] + pub fn is_ext(&self) -> bool { + // https://datatracker.ietf.org/doc/html/rfc6891#section-6.1.3 + // 6.1.3. OPT Record TTL Field Use + // ... + // "EXTENDED-RCODE + // Forms the upper 8 bits of extended 12-bit RCODE (together + // with the 4 bits defined in [RFC1035]. Note that + // EXTENDED-RCODE value 0 indicates that an unextended RCODE is + // in use (values 0 through 15)." + self.0 >> 4 != 0 + } + /// Returns the mnemonic for this value if there is one. #[must_use] pub const fn to_mnemonic(self) -> Option<&'static [u8]> { @@ -976,4 +990,21 @@ mod test { assert_eq!(Ok(OptRcode::BADCOOKIE), "BADCOOKIE".parse()); assert!("#$%!@".parse::().is_err()); } + + #[test] + fn optrcode_isext() { + assert!(!OptRcode::NOERROR.is_ext()); + assert!(!OptRcode::FORMERR.is_ext()); + assert!(!OptRcode::SERVFAIL.is_ext()); + assert!(!OptRcode::NXDOMAIN.is_ext()); + assert!(!OptRcode::NOTIMP.is_ext()); + assert!(!OptRcode::REFUSED.is_ext()); + assert!(!OptRcode::YXDOMAIN.is_ext()); + assert!(!OptRcode::YXRRSET.is_ext()); + assert!(!OptRcode::NXRRSET.is_ext()); + assert!(!OptRcode::NOTAUTH.is_ext()); + assert!(!OptRcode::NOTZONE.is_ext()); + assert!(OptRcode::BADVERS.is_ext()); + assert!(OptRcode::BADCOOKIE.is_ext()); + } }