From 5dd10dc0840c9efee23eef2e69a4091fe01024c9 Mon Sep 17 00:00:00 2001 From: Litr0 Date: Wed, 17 Jul 2024 12:35:20 -0400 Subject: [PATCH] feat: Implement FromBytes trait for SrvRdata --- src/message/rdata/srv_rdata.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/message/rdata/srv_rdata.rs b/src/message/rdata/srv_rdata.rs index c2e76ec1..c0ec4c34 100644 --- a/src/message/rdata/srv_rdata.rs +++ b/src/message/rdata/srv_rdata.rs @@ -46,6 +46,26 @@ impl ToBytes for SrvRdata { } } +impl FromBytes> for SrvRdata { + /// Return a `SrvRdata` from an array of bytes. + fn from_bytes(bytes: &[u8], full_msg: &[u8]) -> Result { + let bytes_len = bytes.len(); + + if bytes_len < 6 { + return Err("The bytes slice is too short to parse the SRV Rdata"); + } + + let priority = u16::from_be_bytes([bytes[0], bytes[1]]); + let weight = u16::from_be_bytes([bytes[2], bytes[3]]); + let port = u16::from_be_bytes([bytes[4], bytes[5]]); + let (target, _) = DomainName::from_bytes(&bytes[6..], full_msg).unwrap(); + + let srv_rdata = SrvRdata::new_with_values(priority, weight, port, target); + + Ok(srv_rdata) + } +} + impl SrvRdata { /// Creates a new `SrvRdata` with default values.