Skip to content

Commit

Permalink
add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
28Smiles committed Aug 31, 2023
1 parent 2f18af0 commit 0c81310
Show file tree
Hide file tree
Showing 62 changed files with 620 additions and 526 deletions.
56 changes: 36 additions & 20 deletions src/additional.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use crate::{Buffer, DnsMessage, DnsMessageError, DnsSection};
use crate::{Buffer, DnsMessage, DnsMessageError};
use crate::answer::DnsAClass;
use crate::name::DnsName;
use crate::parse::Parse;
use crate::parse::{Parse, ParseBytes};
use crate::question::DnsQType;
use crate::rdata::{DnsAType, RData};
use crate::write::WriteBytes;

/// A DNS message additionals section.
pub struct DnsAdditionals<
const PTR_STORAGE: usize,
B: Buffer,
> {
message: DnsMessage<PTR_STORAGE, { DnsSection::Additionals }, B>,
message: DnsMessage<PTR_STORAGE, 3, B>,
remaining: usize,
}

Expand All @@ -19,14 +20,16 @@ impl<
B: Buffer,
> DnsAdditionals<PTR_STORAGE, B> {
#[inline(always)]
pub(crate) fn new(message: DnsMessage<PTR_STORAGE, { DnsSection::Additionals }, B>) -> Self {
pub(crate) fn new(message: DnsMessage<PTR_STORAGE, 3, B>) -> Self {
let remaining = message.header().unwrap().answer_count() as usize;
Self {
message,
remaining,
}
}

/// Append an additional to the message. This will overwrite the next
/// additional or further sections, if any.
pub fn append(&mut self, answer: DnsAdditional<DnsAType>) -> Result<(), DnsMessageError> {
let (buffer, position) = self.message.buffer_and_position();
// Truncate the buffer to the current position.
Expand All @@ -43,7 +46,8 @@ impl<
Ok(())
}

#[inline]
/// Return an iterator over the additionals section.
#[inline(always)]
pub fn iter(&mut self) -> Result<DnsAdditionalsIterator, DnsMessageError> {
let (buffer, position) = self.message.buffer_and_position();
let buffer = buffer.bytes();
Expand All @@ -55,8 +59,9 @@ impl<
})
}

#[inline]
pub fn complete(mut self) -> Result<DnsMessage<PTR_STORAGE, { DnsSection::Additionals }, B>, DnsMessageError> {
/// Complete writing to the additionals section and return the message.
#[inline(always)]
pub fn complete(mut self) -> Result<DnsMessage<PTR_STORAGE, 3, B>, DnsMessageError> {
if self.remaining != 0 {
for x in self.iter()? { x?; }
}
Expand All @@ -70,6 +75,7 @@ impl<
}
}

/// An iterator over the additionals section of a DNS message.
pub struct DnsAdditionalsIterator<'a> {
buffer: &'a [u8],
current_position: &'a mut usize,
Expand All @@ -94,17 +100,37 @@ impl<'a> Iterator for DnsAdditionalsIterator<'a> {
}
}

/// A DNS message additional.
#[derive(Debug, PartialEq)]
pub struct DnsAdditional<'a, D> {
/// The name of the additional.
pub name: DnsName<'a>,
/// The data of the additional.
pub rdata: D,
/// Whether the additional should be cached.
pub cache_flush: bool,
/// The class of the additional.
pub aclass: DnsAClass,
/// The time to live of the additional.
pub ttl: u32,
}

impl<'a> DnsAdditional<'a, RData<'a>> {
pub fn parse(bytes: &'a [u8], i: &mut usize) -> Result<Self, DnsMessageError> {
/// Parse the rdata of the additional into a structured type.
#[inline(always)]
pub fn into_parsed(self) -> Result<DnsAdditional<'a, DnsAType<'a>>, DnsMessageError> {
Ok(DnsAdditional {
name: self.name,
rdata: self.rdata.into_parsed()?,
cache_flush: self.cache_flush,
aclass: self.aclass,
ttl: self.ttl,
})
}
}

impl<'a> ParseBytes<'a> for DnsAdditional<'a, RData<'a>> {
fn parse_bytes(bytes: &'a [u8], i: &mut usize) -> Result<Self, DnsMessageError> {
let name = DnsName::parse(bytes, i)?;
let atype_id = u16::parse(bytes, i)?;
let atype = DnsQType::from_id(atype_id);
Expand All @@ -121,22 +147,12 @@ impl<'a> DnsAdditional<'a, RData<'a>> {
ttl,
})
}

pub fn into_parsed(self) -> Result<DnsAdditional<'a, DnsAType<'a>>, DnsMessageError> {
Ok(DnsAdditional {
name: self.name,
rdata: self.rdata.into_parsed()?,
cache_flush: self.cache_flush,
aclass: self.aclass,
ttl: self.ttl,
})
}
}

impl<'a> DnsAdditional<'a, DnsAType<'a>> {
impl<'a> WriteBytes for DnsAdditional<'a, DnsAType<'a>> {
fn write<
const PTR_STORAGE: usize,
const DNS_SECTION: DnsSection,
const DNS_SECTION: usize,
B: Buffer,
>(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
let mut bytes = 0;
Expand Down
60 changes: 40 additions & 20 deletions src/answer.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::{Buffer, DnsMessage, DnsMessageError, DnsSection};
use crate::{Buffer, DnsMessage, DnsMessageError};
use crate::name::DnsName;
use crate::parse::Parse;
use crate::parse::{Parse, ParseBytes};
use crate::question::DnsQType;
use crate::rdata::{DnsAType, RData};
use crate::write::WriteBytes;

/// A DNS message answers section.
pub struct DnsAnswers<
const PTR_STORAGE: usize,
B: Buffer,
> {
message: DnsMessage<PTR_STORAGE, { DnsSection::Answers }, B>,
message: DnsMessage<PTR_STORAGE, 1, B>,
remaining: usize,
}

Expand All @@ -18,14 +19,16 @@ impl<
B: Buffer,
> DnsAnswers<PTR_STORAGE, B> {
#[inline(always)]
pub(crate) fn new(message: DnsMessage<PTR_STORAGE, { DnsSection::Answers }, B>) -> Self {
pub(crate) fn new(message: DnsMessage<PTR_STORAGE, 1, B>) -> Self {
let remaining = message.header().unwrap().answer_count() as usize;
Self {
message,
remaining,
}
}

/// Append an answer to the message. This will overwrite the next
/// answer or further sections, if any.
pub fn append(&mut self, answer: DnsAnswer<DnsAType>) -> Result<(), DnsMessageError> {
let (buffer, position) = self.message.buffer_and_position();
// Truncate the buffer to the current position.
Expand All @@ -42,7 +45,8 @@ impl<
Ok(())
}

#[inline]
/// Return an iterator over the answers section.
#[inline(always)]
pub fn iter(&mut self) -> Result<DnsAnswerIterator, DnsMessageError> {
let (buffer, position) = self.message.buffer_and_position();
let buffer = buffer.bytes();
Expand All @@ -54,8 +58,10 @@ impl<
})
}

#[inline]
pub fn complete(mut self) -> Result<DnsMessage<PTR_STORAGE, { DnsSection::NameServers }, B>, DnsMessageError> {
/// Complete the message. This will overwrite the next answer or further
/// sections, if any.
#[inline(always)]
pub fn complete(mut self) -> Result<DnsMessage<PTR_STORAGE, 2, B>, DnsMessageError> {
if self.remaining != 0 {
for x in self.iter()? { x?; }
}
Expand All @@ -69,6 +75,7 @@ impl<
}
}

/// An iterator over the answers section of a DNS message.
pub struct DnsAnswerIterator<'a> {
buffer: &'a [u8],
current_position: &'a mut usize,
Expand All @@ -93,17 +100,37 @@ impl<'a> Iterator for DnsAnswerIterator<'a> {
}
}

/// A DNS message answer.
#[derive(Debug, PartialEq)]
pub struct DnsAnswer<'a, D> {
/// The name of the answer.
pub name: DnsName<'a>,
/// The answer data.
pub rdata: D,
/// Whether the answer should be cached.
pub cache_flush: bool,
/// The class of the answer.
pub aclass: DnsAClass,
/// The time to live of the answer.
pub ttl: u32,
}

impl<'a> DnsAnswer<'a, RData<'a>> {
pub fn parse(bytes: &'a [u8], i: &mut usize) -> Result<Self, DnsMessageError> {
/// Parse the rdata of the additional into a structured type.
#[inline(always)]
pub fn into_parsed(self) -> Result<DnsAnswer<'a, DnsAType<'a>>, DnsMessageError> {
Ok(DnsAnswer {
name: self.name,
rdata: self.rdata.into_parsed()?,
cache_flush: self.cache_flush,
aclass: self.aclass,
ttl: self.ttl,
})
}
}

impl<'a> ParseBytes<'a> for DnsAnswer<'a, RData<'a>> {
fn parse_bytes(bytes: &'a [u8], i: &mut usize) -> Result<Self, DnsMessageError> {
let name = DnsName::parse(bytes, i)?;
let atype_id = u16::parse(bytes, i)?;
let atype = DnsQType::from_id(atype_id);
Expand All @@ -120,22 +147,12 @@ impl<'a> DnsAnswer<'a, RData<'a>> {
ttl,
})
}

pub fn into_parsed(self) -> Result<DnsAnswer<'a, DnsAType<'a>>, DnsMessageError> {
Ok(DnsAnswer {
name: self.name,
rdata: self.rdata.into_parsed()?,
cache_flush: self.cache_flush,
aclass: self.aclass,
ttl: self.ttl,
})
}
}

impl<'a> DnsAnswer<'a, DnsAType<'a>> {
impl<'a> WriteBytes for DnsAnswer<'a, DnsAType<'a>> {
fn write<
const PTR_STORAGE: usize,
const DNS_SECTION: DnsSection,
const DNS_SECTION: usize,
B: Buffer,
>(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
let mut bytes = 0;
Expand All @@ -160,6 +177,7 @@ impl<'a> DnsAnswer<'a, DnsAType<'a>> {
}
}

/// A DNS message answer class.
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(u16)]
pub enum DnsAClass {
Expand All @@ -171,6 +189,7 @@ pub enum DnsAClass {
}

impl DnsAClass {
/// Create a new QClass from an id.
#[inline(always)]
pub fn from_id(id: u16) -> Self {
match id {
Expand All @@ -179,6 +198,7 @@ impl DnsAClass {
}
}

/// Get the id of the QClass.
#[inline(always)]
pub fn id(&self) -> u16 {
match self {
Expand Down

0 comments on commit 0c81310

Please sign in to comment.