(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 <tobi@ottenweller.net>
Reviewed-on: #19
Co-authored-by: ddidderr <ddidderr@paul.network>
Co-committed-by: ddidderr <ddidderr@paul.network>
This commit is contained in:
ddidderr 2022-05-08 14:43:25 +02:00 committed by mice_on_drugs
parent 623a82c2fe
commit 97e786905e

View File

@ -110,34 +110,52 @@ impl TryFrom<u8> for DNSRCode {
} }
} }
#[derive(Debug)]
pub enum DNSMessageType {
Query,
Response,
}
impl TryFrom<u8> for DNSMessageType {
type Error = DNSParseError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::Query),
1 => Ok(Self::Response),
_ => Err(DNSParseError::DNSRCodeInvalid),
}
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct DNSHeader { pub struct DNSHeader {
/// used by the requester to match up replies to outstanding queries /// used by the requester to match up replies to outstanding queries
pub id: u16, pub id: u16,
/// specifies whether this message is a query (false), or a response (true) /// specifies whether this message is a query or a response
pub qr: bool, pub message_type: DNSMessageType,
pub opcode: DNSOpCode, pub opcode: DNSOpCode,
/// specifies that the responding name server is an authority for the domain name in question section /// 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 /// 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 /// 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 /// denotes whether recursive query support is available in the name server
pub ra: bool, pub recursion_available: bool,
/// TODO: add documuentation about this flag /// TODO: add documuentation about this flag
pub ad: bool, pub authentic_data: bool,
/// TODO: add documuentation about this flag /// TODO: add documuentation about this flag
pub cd: bool, pub checking_disabled: bool,
pub rcode: DNSRCode, pub response_code: DNSRCode,
/// TODO: add documuentation about this count /// TODO: add documuentation about this count
pub qd_zo_count: u16, pub query_count: u16,
/// TODO: add documuentation about this count /// TODO: add documuentation about this count
pub an_pr_count: u16, pub answer_count: u16,
/// TODO: add documuentation about this count /// TODO: add documuentation about this count
pub ns_up_count: u16, pub name_server_count: u16,
/// TODO: add documuentation about this count /// TODO: add documuentation about this count
pub ar_count: u16, pub additional_count: u16,
} }
#[derive(Debug)] #[derive(Debug)]
@ -169,37 +187,37 @@ impl DNSHeader {
let id = u16::from_be_bytes((&datagram[..2]).try_into().unwrap()); 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 opcode = DNSOpCode::try_from((datagram[2] & Self::OPCODE_MASK) >> Self::OPCODE_OFFSET)?;
let aa = (datagram[2] & Self::AA_MASK) != 0; let authorative_answer = (datagram[2] & Self::AA_MASK) != 0;
let tc = (datagram[2] & Self::TC_MASK) != 0; let truncated = (datagram[2] & Self::TC_MASK) != 0;
let rd = (datagram[2] & Self::RD_MASK) != 0; let recursion_desired = (datagram[2] & Self::RD_MASK) != 0;
let ra = (datagram[3] & Self::RA_MASK) != 0; let recursion_available = (datagram[3] & Self::RA_MASK) != 0;
let ad = (datagram[3] & Self::AD_MASK) != 0; let authentic_data = (datagram[3] & Self::AD_MASK) != 0;
let cd = (datagram[3] & Self::CD_MASK) != 0; let checking_disabled = (datagram[3] & Self::CD_MASK) != 0;
let rcode = DNSRCode::try_from(datagram[3] & Self::RCODE_MASK)?; 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 query_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 answer_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 name_server_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 additional_count = u16::from_be_bytes((datagram[10..12]).try_into().unwrap());
Ok(DNSHeader { Ok(DNSHeader {
id, id,
qr, message_type,
opcode, opcode,
aa, authorative_answer,
tc, truncated,
rd, recursion_desired,
ra, recursion_available,
ad, authentic_data,
cd, checking_disabled,
rcode, response_code,
qd_zo_count, query_count,
an_pr_count, answer_count,
ns_up_count, name_server_count,
ar_count, additional_count,
}) })
} }
} }