diff --git a/src/proto.rs b/src/proto.rs index f0c8f9b..4f7e24d 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -186,15 +186,16 @@ impl DNSHeader { let message_type = DNSMessageType::from(datagram[2] & Self::QR_MASK != 0); let opcode = DNSOpCode::try_from((datagram[2] & Self::OPCODE_MASK) >> Self::OPCODE_OFFSET)?; + let authorative_answer = (datagram[2] & Self::AA_MASK) != 0; let truncated = (datagram[2] & Self::TC_MASK) != 0; let recursion_desired = (datagram[2] & Self::RD_MASK) != 0; + let recursion_available = (datagram[3] & Self::RA_MASK) != 0; - let recursion_available = (datagram[2] & Self::RA_MASK) != 0; let authentic_data = (datagram[2] & Self::AD_MASK) != 0; let checking_disabled = (datagram[2] & Self::CD_MASK) != 0; - let response_code = DNSRCode::try_from(datagram[3] & Self::RCODE_MASK)?; + let response_code = DNSRCode::try_from(datagram[3] & Self::RCODE_MASK)?; let query_count = u16::from_be_bytes((datagram[4..6]).try_into().unwrap()); let answer_count = u16::from_be_bytes((datagram[6..8]).try_into().unwrap()); let name_server_count = u16::from_be_bytes((datagram[8..10]).try_into().unwrap()); @@ -512,5 +513,101 @@ mod tests { Ok(()) } } + + mod authorative_answer { + use super::super::super::*; + + #[test] + fn yes() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.authorative_answer, true); + Ok(()) + } + + #[test] + fn no() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.authorative_answer, false); + Ok(()) + } + } + + mod truncated { + use super::super::super::*; + + #[test] + fn yes() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.truncated, true); + Ok(()) + } + + #[test] + fn no() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.truncated, false); + Ok(()) + } + } + + mod recursion_desired { + use super::super::super::*; + + #[test] + fn yes() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.recursion_desired, true); + Ok(()) + } + + #[test] + fn no() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.recursion_desired, false); + Ok(()) + } + } + + mod recursion_available { + use super::super::super::*; + + #[test] + fn yes() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.recursion_available, true); + Ok(()) + } + + #[test] + fn no() -> Result<(), DNSParseError> { + let dns_query = [ + 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + let parse_result = DNSHeader::from_udp_datagram(&dns_query); + assert_eq!(parse_result?.recursion_available, false); + Ok(()) + } + } } }