mirror of
https://github.com/ZeusWPI/ZNS.git
synced 2024-11-21 21:41:10 +01:00
Different trait for to bytes
This commit is contained in:
parent
4847a13c08
commit
4469b6f0b5
3 changed files with 41 additions and 7 deletions
|
@ -4,7 +4,8 @@ use crate::{
|
||||||
errors::ParseError,
|
errors::ParseError,
|
||||||
reader::Reader,
|
reader::Reader,
|
||||||
structs::{
|
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<Self>
|
fn from_bytes(reader: &mut Reader) -> Result<Self>
|
||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait ToBytes {
|
||||||
fn to_bytes(s: Self) -> Vec<u8>
|
fn to_bytes(s: Self) -> Vec<u8>
|
||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
|
@ -103,7 +107,9 @@ impl FromBytes for Header {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToBytes for Header {
|
||||||
fn to_bytes(header: Self) -> Vec<u8> {
|
fn to_bytes(header: Self) -> Vec<u8> {
|
||||||
let mut result: [u8; size_of::<Header>()] = [0; size_of::<Header>()];
|
let mut result: [u8; size_of::<Header>()] = [0; size_of::<Header>()];
|
||||||
|
|
||||||
|
@ -144,7 +150,9 @@ impl FromBytes for LabelString {
|
||||||
|
|
||||||
Ok(out)
|
Ok(out)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToBytes for LabelString {
|
||||||
fn to_bytes(name: Self) -> Vec<u8> {
|
fn to_bytes(name: Self) -> Vec<u8> {
|
||||||
let mut result: Vec<u8> = vec![];
|
let mut result: Vec<u8> = vec![];
|
||||||
for label in name {
|
for label in name {
|
||||||
|
@ -187,7 +195,9 @@ impl FromBytes for Question {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToBytes for Question {
|
||||||
fn to_bytes(question: Self) -> Vec<u8> {
|
fn to_bytes(question: Self) -> Vec<u8> {
|
||||||
let mut result = LabelString::to_bytes(question.qname);
|
let mut result = LabelString::to_bytes(question.qname);
|
||||||
result.extend(u16::to_be_bytes(question.qtype.into()));
|
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<u8> {
|
fn to_bytes(rr: Self) -> Vec<u8> {
|
||||||
let mut result = LabelString::to_bytes(rr.name);
|
let mut result = LabelString::to_bytes(rr.name);
|
||||||
result.extend(u16::to_be_bytes(rr._type.into()));
|
result.extend(u16::to_be_bytes(rr._type.into()));
|
||||||
|
@ -270,7 +282,9 @@ impl FromBytes for Message {
|
||||||
additional,
|
additional,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToBytes for Message {
|
||||||
fn to_bytes(message: Self) -> Vec<u8> {
|
fn to_bytes(message: Self) -> Vec<u8> {
|
||||||
let mut result = vec![];
|
let mut result = vec![];
|
||||||
result.extend(Header::to_bytes(message.header));
|
result.extend(Header::to_bytes(message.header));
|
||||||
|
@ -312,11 +326,22 @@ impl FromBytes for KeyRData {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn to_bytes(_: Self) -> Vec<u8>
|
impl FromBytes for DNSKeyRData {
|
||||||
where
|
fn from_bytes(reader: &mut Reader) -> Result<Self> {
|
||||||
Self: Sized,
|
if reader.unread_bytes() < 18 {
|
||||||
{
|
Err(ParseError {
|
||||||
todo!()
|
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())?,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ use tokio::net::UdpSocket;
|
||||||
|
|
||||||
use crate::errors::ParseError;
|
use crate::errors::ParseError;
|
||||||
use crate::handlers::{Handler, ResponseHandler};
|
use crate::handlers::{Handler, ResponseHandler};
|
||||||
use crate::parser::FromBytes;
|
use crate::parser::{FromBytes, ToBytes};
|
||||||
use crate::reader::Reader;
|
use crate::reader::Reader;
|
||||||
use crate::structs::{Header, Message, RCODE};
|
use crate::structs::{Header, Message, RCODE};
|
||||||
|
|
||||||
|
|
|
@ -106,3 +106,12 @@ pub struct KeyRData {
|
||||||
pub signer: LabelString,
|
pub signer: LabelString,
|
||||||
pub signature: Vec<u8>,
|
pub signature: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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<u8>,
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue