From 97e786905e7400b871cf96fcc1325d3ec44eecbf Mon Sep 17 00:00:00 2001 From: ddidderr Date: Sun, 8 May 2022 14:43:25 +0200 Subject: [PATCH] (refactor) rename DNS header fields to understandable long names (#19) Renames the DNS header fields and adds a new `enum DNSMessageType`. Resolves #14 Co-authored-by: mice_on_drugs Reviewed-on: https://git.comff.net/rustics/dns/pulls/19 Co-authored-by: ddidderr Co-committed-by: ddidderr --- src/proto.rs | 92 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/src/proto.rs b/src/proto.rs index da0a989..6818126 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -110,34 +110,52 @@ impl TryFrom for DNSRCode { } } +#[derive(Debug)] +pub enum DNSMessageType { + Query, + Response, +} + +impl TryFrom for DNSMessageType { + type Error = DNSParseError; + + fn try_from(value: u8) -> Result { + match value { + 0 => Ok(Self::Query), + 1 => Ok(Self::Response), + _ => Err(DNSParseError::DNSRCodeInvalid), + } + } +} + #[derive(Debug)] pub struct DNSHeader { /// used by the requester to match up replies to outstanding queries pub id: u16, - /// specifies whether this message is a query (false), or a response (true) - pub qr: bool, + /// specifies whether this message is a query or a response + pub message_type: DNSMessageType, pub opcode: DNSOpCode, /// specifies that the responding name server is an authority for the domain name in question section - pub aa: bool, + pub authorative_answer: bool, /// specifies that this message was truncated due to length greater than that permitted on the transmission channel - pub tc: bool, + pub truncated: bool, /// it directs the name server to pursue the query recursively - pub rd: bool, + pub recursion_desired: bool, /// denotes whether recursive query support is available in the name server - pub ra: bool, + pub recursion_available: bool, /// TODO: add documuentation about this flag - pub ad: bool, + pub authentic_data: bool, /// TODO: add documuentation about this flag - pub cd: bool, - pub rcode: DNSRCode, + pub checking_disabled: bool, + pub response_code: DNSRCode, /// TODO: add documuentation about this count - pub qd_zo_count: u16, + pub query_count: u16, /// TODO: add documuentation about this count - pub an_pr_count: u16, + pub answer_count: u16, /// TODO: add documuentation about this count - pub ns_up_count: u16, + pub name_server_count: u16, /// TODO: add documuentation about this count - pub ar_count: u16, + pub additional_count: u16, } #[derive(Debug)] @@ -169,37 +187,37 @@ impl DNSHeader { let id = u16::from_be_bytes((&datagram[..2]).try_into().unwrap()); - let qr = (datagram[2] & Self::QR_MASK) != 0; + let message_type = DNSMessageType::try_from(datagram[2] & Self::QR_MASK)?; let opcode = DNSOpCode::try_from((datagram[2] & Self::OPCODE_MASK) >> Self::OPCODE_OFFSET)?; - let aa = (datagram[2] & Self::AA_MASK) != 0; - let tc = (datagram[2] & Self::TC_MASK) != 0; - let rd = (datagram[2] & Self::RD_MASK) != 0; + 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 ra = (datagram[3] & Self::RA_MASK) != 0; - let ad = (datagram[3] & Self::AD_MASK) != 0; - let cd = (datagram[3] & Self::CD_MASK) != 0; - let rcode = DNSRCode::try_from(datagram[3] & Self::RCODE_MASK)?; + let recursion_available = (datagram[3] & Self::RA_MASK) != 0; + let authentic_data = (datagram[3] & Self::AD_MASK) != 0; + let checking_disabled = (datagram[3] & Self::CD_MASK) != 0; + let response_code = DNSRCode::try_from(datagram[3] & Self::RCODE_MASK)?; - let qd_zo_count = u16::from_be_bytes((datagram[4..6]).try_into().unwrap()); - let an_pr_count = u16::from_be_bytes((datagram[6..8]).try_into().unwrap()); - let ns_up_count = u16::from_be_bytes((datagram[8..10]).try_into().unwrap()); - let ar_count = u16::from_be_bytes((datagram[10..12]).try_into().unwrap()); + 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()); + let additional_count = u16::from_be_bytes((datagram[10..12]).try_into().unwrap()); Ok(DNSHeader { id, - qr, + message_type, opcode, - aa, - tc, - rd, - ra, - ad, - cd, - rcode, - qd_zo_count, - an_pr_count, - ns_up_count, - ar_count, + authorative_answer, + truncated, + recursion_desired, + recursion_available, + authentic_data, + checking_disabled, + response_code, + query_count, + answer_count, + name_server_count, + additional_count, }) } }