simple_dns/dns/rdata/
hinfo.rs

1use std::collections::HashMap;
2
3use crate::{
4    bytes_buffer::BytesBuffer,
5    dns::{name::Label, CharacterString, WireFormat},
6};
7
8use super::RR;
9
10/// HINFO records are used to acquire general information about a host.  
11/// The main use is for protocols such as FTP that can use special procedures
12/// when talking between machines or operating systems of the same type.
13#[derive(Debug, PartialEq, Eq, Hash, Clone)]
14pub struct HINFO<'a> {
15    /// A [CharacterString](`CharacterString`) which specifies the CPU type.
16    pub cpu: CharacterString<'a>,
17    /// A [CharacterString](`CharacterString`) which specifies the operating system type.
18    pub os: CharacterString<'a>,
19}
20
21impl RR for HINFO<'_> {
22    const TYPE_CODE: u16 = 13;
23}
24
25impl HINFO<'_> {
26    /// Transforms the inner data into its owned type
27    pub fn into_owned<'b>(self) -> HINFO<'b> {
28        HINFO {
29            cpu: self.cpu.into_owned(),
30            os: self.os.into_owned(),
31        }
32    }
33}
34
35impl<'a> WireFormat<'a> for HINFO<'a> {
36    const MINIMUM_LEN: usize = 0;
37
38    fn parse(data: &mut BytesBuffer<'a>) -> crate::Result<Self>
39    where
40        Self: Sized,
41    {
42        let cpu = CharacterString::parse(data)?;
43        let os = CharacterString::parse(data)?;
44
45        Ok(Self { cpu, os })
46    }
47
48    fn write_to<T: std::io::Write>(&self, out: &mut T) -> crate::Result<()> {
49        self.cpu.write_to(out)?;
50        self.os.write_to(out)
51    }
52
53    fn write_compressed_to<T: std::io::Write + std::io::Seek>(
54        &'a self,
55        out: &mut T,
56        name_refs: &mut HashMap<&'a [Label<'a>], usize>,
57    ) -> crate::Result<()> {
58        self.cpu.write_compressed_to(out, name_refs)?;
59        self.os.write_compressed_to(out, name_refs)
60    }
61
62    fn len(&self) -> usize {
63        self.cpu.len() + self.os.len()
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use crate::{rdata::RData, ResourceRecord};
70
71    use super::*;
72
73    #[test]
74    fn parse_and_write_hinfo() {
75        let hinfo = HINFO {
76            cpu: CharacterString::new(b"\"some cpu").unwrap(),
77            os: CharacterString::new(b"\"some os").unwrap(),
78        };
79
80        let mut data = Vec::new();
81        assert!(hinfo.write_to(&mut data).is_ok());
82
83        let hinfo = HINFO::parse(&mut (&data[..]).into());
84        assert!(hinfo.is_ok());
85        let hinfo = hinfo.unwrap();
86
87        assert_eq!(data.len(), hinfo.len());
88        assert_eq!("\"some cpu", hinfo.cpu.to_string());
89        assert_eq!("\"some os", hinfo.os.to_string());
90    }
91
92    #[test]
93    fn parse_sample() -> Result<(), Box<dyn std::error::Error>> {
94        let sample_file = std::fs::read("samples/zonefile/HINFO.sample")?;
95
96        let sample_rdata = match ResourceRecord::parse(&mut (&sample_file[..]).into())?.rdata {
97            RData::HINFO(rdata) => rdata,
98            _ => unreachable!(),
99        };
100
101        assert_eq!(sample_rdata.cpu, "Generic PC clone".try_into()?);
102        assert_eq!(sample_rdata.os, "NetBSD-1.4".try_into()?);
103        Ok(())
104    }
105}