flex_dns/rdata/
rp.rs

1use crate::{Buffer, DnsMessage, DnsMessageError, MutBuffer};
2use crate::name::DnsName;
3use crate::parse::Parse;
4use crate::rdata::{RData, RDataParse};
5use crate::write::WriteBytes;
6
7/// # Responsible person
8/// This record is used to identify the responsible person for a domain
9#[derive(Copy, Clone, Debug, PartialEq)]
10pub struct Rp<'a> {
11    /// The mailbox name of the responsible person
12    pub mbox: DnsName<'a>,
13    /// The domain name of the responsible person
14    pub txt: DnsName<'a>,
15}
16
17impl<'a> RDataParse<'a> for Rp<'a> {
18    #[inline]
19    fn parse(rdata: &RData<'a>, i: &mut usize) -> Result<Self, DnsMessageError> {
20        let mbox = DnsName::parse(rdata.buffer, i)?;
21        let txt = DnsName::parse(rdata.buffer, i)?;
22
23        Ok(Self {
24            mbox,
25            txt,
26        })
27    }
28}
29
30impl<'a> WriteBytes for Rp<'a> {
31    #[inline]
32    fn write<
33        const PTR_STORAGE: usize,
34        const DNS_SECTION: usize,
35        B: MutBuffer + Buffer,
36    >(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
37        let mut bytes = 0;
38        bytes += self.mbox.write(message)?;
39        bytes += self.txt.write(message)?;
40
41        Ok(bytes)
42    }
43}
44
45#[cfg(test)]
46mod test {
47    use crate::rdata::testutils::parse_write_test;
48
49    use super::*;
50
51    parse_write_test!(
52        33,
53        [
54            0x03, b'w', b'w', b'w',
55            0x07, b'e', b'x', b'a', b'm', b'p', b'l', b'e',
56            0x03, b'c', b'o', b'm',
57            0x00, // mbox
58            0x03, b'w', b'w', b'w',
59            0x06, b'g', b'o', b'o', b'g', b'l', b'e',
60            0x03, b'c', b'o', b'm',
61            0x00, // txt
62        ],
63        Rp {
64            mbox: unsafe { DnsName::new_unchecked(b"\x03www\x07example\x03com\x00") },
65            txt: unsafe { DnsName::new_unchecked(b"\x03www\x06google\x03com\x00") },
66        },
67    );
68}