From bc278b7d399d2192f327f29e4b44f46cc3e73723 Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Sun, 3 Apr 2022 17:01:07 +0200 Subject: [PATCH 1/2] Custom error type for parsing --- src/proto.rs | 48 ++++++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/src/proto.rs b/src/proto.rs index 6565a40..a29828a 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -38,6 +38,12 @@ pub enum DNSRCode { NOTINZONE = 10, } +#[derive(Debug)] +pub enum DNSParseErr { + DatagramLengthError, + SliceError(std::array::TryFromSliceError) +} + impl From for DNSRCode { fn from(val: u16) -> Self { match val { @@ -109,18 +115,14 @@ impl DNSHeader { const OPCODE_OFFSET: u16 = 1; const RCODE_OFFSET: u16 = 11; - pub fn from_udp_datagram(datagram: &[u8]) -> Result { + pub fn from_udp_datagram(datagram: &[u8]) -> Result { if datagram.len() < 11 { - return Err("Not enough data"); + return Err(DNSParseErr::DatagramLengthError); } - let id = u16::from_be_bytes((&datagram[..2]).try_into().map_err(|_err| "Invalid id")?); + let id = u16::from_be_bytes((&datagram[..2]).try_into()?); - let flags = u16::from_be_bytes( - (&datagram[2..4]) - .try_into() - .map_err(|_err| "Invalid flags")?, - ); + let flags = u16::from_be_bytes((&datagram[2..4]).try_into()?); let qr = (flags & DNSHeader::QR_MASK) != 0; let opcode = DNSOpCode::from((flags & DNSHeader::OPCODE_MASK) >> DNSHeader::OPCODE_OFFSET); let aa = (flags & DNSHeader::AA_MASK) != 0; @@ -131,26 +133,10 @@ impl DNSHeader { let cd = (flags & DNSHeader::CD_MASK) != 0; let rcode = DNSRCode::from((flags & DNSHeader::RCODE_MASK) >> DNSHeader::RCODE_OFFSET); - let qd_zo_count = u16::from_be_bytes( - (&datagram[4..6]) - .try_into() - .map_err(|_err| "Invalid qd_zo_count")?, - ); - let an_pr_count = u16::from_be_bytes( - (&datagram[6..8]) - .try_into() - .map_err(|_err| "Invalid an_pr_count")?, - ); - let ns_up_count = u16::from_be_bytes( - (&datagram[8..10]) - .try_into() - .map_err(|_err| "Invalid ns_up_count")?, - ); - let ar_count = u16::from_be_bytes( - (&datagram[10..12]) - .try_into() - .map_err(|_err| "Invalid ar_count")?, - ); + let qd_zo_count = u16::from_be_bytes((&datagram[4..6]).try_into()?); + let an_pr_count = u16::from_be_bytes((&datagram[6..8]).try_into()?); + let ns_up_count = u16::from_be_bytes((&datagram[8..10]).try_into()?); + let ar_count = u16::from_be_bytes((&datagram[10..12]).try_into()?); Ok(DNSHeader { id, @@ -170,3 +156,9 @@ impl DNSHeader { }) } } + +impl From for DNSParseErr { + fn from(err: std::array::TryFromSliceError) -> Self { + DNSParseErr::SliceError(err) + } +} -- 2.47.1 From c67c940ba973f7473dca1095913ad8c52ab32a4c Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Sun, 3 Apr 2022 17:10:59 +0200 Subject: [PATCH 2/2] Improve code styling --- src/proto.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/proto.rs b/src/proto.rs index a29828a..838c0c9 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -38,12 +38,6 @@ pub enum DNSRCode { NOTINZONE = 10, } -#[derive(Debug)] -pub enum DNSParseErr { - DatagramLengthError, - SliceError(std::array::TryFromSliceError) -} - impl From for DNSRCode { fn from(val: u16) -> Self { match val { @@ -115,9 +109,9 @@ impl DNSHeader { const OPCODE_OFFSET: u16 = 1; const RCODE_OFFSET: u16 = 11; - pub fn from_udp_datagram(datagram: &[u8]) -> Result { + pub fn from_udp_datagram(datagram: &[u8]) -> Result { if datagram.len() < 11 { - return Err(DNSParseErr::DatagramLengthError); + return Err(DNSParseError::DatagramLengthError); } let id = u16::from_be_bytes((&datagram[..2]).try_into()?); @@ -157,8 +151,14 @@ impl DNSHeader { } } -impl From for DNSParseErr { +#[derive(Debug)] +pub enum DNSParseError { + DatagramLengthError, + SliceError(std::array::TryFromSliceError) +} + +impl From for DNSParseError { fn from(err: std::array::TryFromSliceError) -> Self { - DNSParseErr::SliceError(err) + DNSParseError::SliceError(err) } } -- 2.47.1