From 4469b6f0b515cb2dd963938c8de127974c03e833 Mon Sep 17 00:00:00 2001 From: Xander Bil Date: Sun, 9 Jun 2024 16:33:31 +0200 Subject: [PATCH] Different trait for to bytes --- src/parser.rs | 37 +++++++++++++++++++++++++++++++------ src/resolver.rs | 2 +- src/structs.rs | 9 +++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 5455864..dba7b15 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,7 +4,8 @@ use crate::{ errors::ParseError, reader::Reader, structs::{ - Class, Header, KeyRData, LabelString, Message, Opcode, Question, RRClass, RRType, Type, RR, + Class, DNSKeyRData, Header, KeyRData, LabelString, Message, Opcode, Question, RRClass, + RRType, Type, RR, }, }; @@ -80,6 +81,9 @@ pub trait FromBytes { fn from_bytes(reader: &mut Reader) -> Result where Self: Sized; +} + +pub trait ToBytes { fn to_bytes(s: Self) -> Vec where Self: Sized; @@ -103,7 +107,9 @@ impl FromBytes for Header { }) } } +} +impl ToBytes for Header { fn to_bytes(header: Self) -> Vec { let mut result: [u8; size_of::
()] = [0; size_of::
()]; @@ -144,7 +150,9 @@ impl FromBytes for LabelString { Ok(out) } +} +impl ToBytes for LabelString { fn to_bytes(name: Self) -> Vec { let mut result: Vec = vec![]; for label in name { @@ -187,7 +195,9 @@ impl FromBytes for Question { } } } +} +impl ToBytes for Question { fn to_bytes(question: Self) -> Vec { let mut result = LabelString::to_bytes(question.qname); result.extend(u16::to_be_bytes(question.qtype.into())); @@ -226,7 +236,9 @@ impl FromBytes for RR { } } } +} +impl ToBytes for RR { fn to_bytes(rr: Self) -> Vec { let mut result = LabelString::to_bytes(rr.name); result.extend(u16::to_be_bytes(rr._type.into())); @@ -270,7 +282,9 @@ impl FromBytes for Message { additional, }) } +} +impl ToBytes for Message { fn to_bytes(message: Self) -> Vec { let mut result = vec![]; result.extend(Header::to_bytes(message.header)); @@ -312,11 +326,22 @@ impl FromBytes for KeyRData { }) } } +} - fn to_bytes(_: Self) -> Vec - where - Self: Sized, - { - todo!() +impl FromBytes for DNSKeyRData { + fn from_bytes(reader: &mut Reader) -> Result { + if reader.unread_bytes() < 18 { + Err(ParseError { + object: String::from("DNSKeyRData"), + message: String::from("invalid rdata"), + }) + } else { + Ok(DNSKeyRData { + flags: reader.read_u16()?, + protocol: reader.read_u8()?, + algorithm: reader.read_u8()?, + public_key: reader.read(reader.unread_bytes())?, + }) + } } } diff --git a/src/resolver.rs b/src/resolver.rs index d776631..3427407 100644 --- a/src/resolver.rs +++ b/src/resolver.rs @@ -6,7 +6,7 @@ use tokio::net::UdpSocket; use crate::errors::ParseError; use crate::handlers::{Handler, ResponseHandler}; -use crate::parser::FromBytes; +use crate::parser::{FromBytes, ToBytes}; use crate::reader::Reader; use crate::structs::{Header, Message, RCODE}; diff --git a/src/structs.rs b/src/structs.rs index edbcd0b..10a7e38 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -106,3 +106,12 @@ pub struct KeyRData { pub signer: LabelString, pub signature: Vec, } + +/// https://datatracker.ietf.org/doc/html/rfc4034#section-2 +#[derive(Debug)] +pub struct DNSKeyRData { + pub flags: u16, + pub protocol: u8, + pub algorithm: u8, + pub public_key: Vec, +}