simple_dns/dns/rdata/
minfo.rs

1use std::collections::HashMap;
2
3use crate::dns::{name::Label, Name, WireFormat};
4
5use super::RR;
6
7/// MINFO recors are used to acquire mailbox or mail list information
8#[derive(Debug, PartialEq, Eq, Hash, Clone)]
9pub struct MINFO<'a> {
10    /// A [Name](`Name`) which specifies a mailbox which is responsible for the mailing list or mailbox.  
11    pub rmailbox: Name<'a>,
12    /// A [Name](`Name`) which specifies a mailbox which is to receive error messages related to  
13    /// the mailing list or mailbox specified by the owner of the MINFO RR
14    pub emailbox: Name<'a>,
15}
16
17impl<'a> RR for MINFO<'a> {
18    const TYPE_CODE: u16 = 14;
19}
20
21impl<'a> MINFO<'a> {
22    /// Transforms the inner data into its owned type
23    pub fn into_owned<'b>(self) -> MINFO<'b> {
24        MINFO {
25            rmailbox: self.rmailbox.into_owned(),
26            emailbox: self.emailbox.into_owned(),
27        }
28    }
29}
30
31impl<'a> WireFormat<'a> for MINFO<'a> {
32    fn parse(data: &'a [u8], position: &mut usize) -> crate::Result<Self>
33    where
34        Self: Sized,
35    {
36        let rmailbox = Name::parse(data, position)?;
37        let emailbox = Name::parse(data, position)?;
38
39        Ok(Self { rmailbox, emailbox })
40    }
41
42    fn write_to<T: std::io::Write>(&self, out: &mut T) -> crate::Result<()> {
43        self.rmailbox.write_to(out)?;
44        self.emailbox.write_to(out)
45    }
46
47    fn write_compressed_to<T: std::io::Write + std::io::Seek>(
48        &'a self,
49        out: &mut T,
50        name_refs: &mut HashMap<&'a [Label<'a>], usize>,
51    ) -> crate::Result<()> {
52        self.rmailbox.write_compressed_to(out, name_refs)?;
53        self.emailbox.write_compressed_to(out, name_refs)
54    }
55
56    fn len(&self) -> usize {
57        self.rmailbox.len() + self.emailbox.len()
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn parse_and_write_minfo() {
67        let minfo = MINFO {
68            rmailbox: Name::new("r.mailbox.com").unwrap(),
69            emailbox: Name::new("e.mailbox.com").unwrap(),
70        };
71
72        let mut data = Vec::new();
73        assert!(minfo.write_to(&mut data).is_ok());
74
75        let minfo = MINFO::parse(&data, &mut 0);
76        assert!(minfo.is_ok());
77        let minfo = minfo.unwrap();
78
79        assert_eq!(data.len(), minfo.len());
80        assert_eq!("r.mailbox.com", minfo.rmailbox.to_string());
81        assert_eq!("e.mailbox.com", minfo.emailbox.to_string());
82    }
83}