simple_dns/dns/
wire_format.rs

1use std::{
2    collections::HashMap,
3    io::{Seek, Write},
4};
5
6use super::name::Label;
7
8/// Represents anything that can be part of a dns packet (Question, Resource Record, RData)
9pub(crate) trait WireFormat<'a> {
10    /// Parse the contents of the data buffer starting at the given `position`
11    /// It is necessary to pass the full buffer to this function, to be able to correctly implement name compression
12    /// The implementor must `position` to ensure that is at the end of the data just parsed
13    fn parse(data: &'a [u8], position: &mut usize) -> crate::Result<Self>
14    where
15        Self: Sized;
16
17    /// Write this part bytes to the writer
18    fn write_to<T: Write>(&self, out: &mut T) -> crate::Result<()>;
19
20    fn write_compressed_to<T: Write + Seek>(
21        &'a self,
22        out: &mut T,
23        _name_refs: &mut HashMap<&'a [Label<'a>], usize>,
24    ) -> crate::Result<()> {
25        self.write_to(out)
26    }
27
28    /// Returns the length in bytes of this content
29    fn len(&self) -> usize;
30}