flex_dns/rdata/
naptr.rs

1use crate::{Buffer, DnsMessage, DnsMessageError, MutBuffer};
2use crate::characters::Characters;
3use crate::name::DnsName;
4use crate::parse::Parse;
5use crate::rdata::{RData, RDataParse};
6use crate::write::WriteBytes;
7
8/// # Naming authority pointer
9/// This record is used to delegate a DNS zone to use the given authoritative name servers
10#[derive(Copy, Clone, Debug, PartialEq)]
11pub struct Naptr<'a> {
12    /// The order in which the NAPTR records MUST be processed in order to accurately represent the ordered list of Rules.
13    pub order: u16,
14    /// The preference value of this NAPTR record.
15    pub preference: u16,
16    /// The flags field specifies various flags that control the processing of the NAPTR record.
17    pub flags: Characters<'a>,
18    /// The service field specifies the service(s) available down this rewrite path.
19    pub service: Characters<'a>,
20    /// The regexp field specifies a substitution expression that is applied to the original string held by the client in order to construct the next domain name to lookup.
21    pub regexp: Characters<'a>,
22    /// The replacement field specifies the next domain-name to query for NAPTR, SRV, or Address records depending on the value of the flags field.
23    pub replacement: DnsName<'a>,
24}
25
26impl<'a> RDataParse<'a> for Naptr<'a> {
27    #[inline]
28    fn parse(rdata: &RData<'a>, i: &mut usize) -> Result<Self, DnsMessageError> {
29        let order = u16::parse(rdata.buffer, i)?;
30        let preference = u16::parse(rdata.buffer, i)?;
31        let flags = Characters::parse(rdata.buffer, i)?;
32        let service = Characters::parse(rdata.buffer, i)?;
33        let regexp = Characters::parse(rdata.buffer, i)?;
34        let replacement = DnsName::parse(rdata.buffer, i)?;
35
36        Ok(Self {
37            order,
38            preference,
39            flags,
40            service,
41            regexp,
42            replacement,
43        })
44    }
45}
46
47impl<'a> WriteBytes for Naptr<'a> {
48    #[inline]
49    fn write<
50        const PTR_STORAGE: usize,
51        const DNS_SECTION: usize,
52        B: MutBuffer + Buffer,
53    >(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
54        let mut bytes = 0;
55
56        bytes += self.order.write(message)?;
57        bytes += self.preference.write(message)?;
58        bytes += self.flags.write(message)?;
59        bytes += self.service.write(message)?;
60        bytes += self.regexp.write(message)?;
61        bytes += self.replacement.write(message)?;
62
63        Ok(bytes)
64    }
65}
66
67#[cfg(test)]
68mod test {
69    use crate::rdata::testutils::parse_write_test;
70
71    use super::*;
72
73    parse_write_test!(
74        21,
75        [
76            0x00, 0x0e, // order
77            0x00, 0x0f, // preference
78            0x03, b'a', b'b', b'c', // flags
79            0x03, b'd', b'e', b'f', // service
80            0x03, b'g', b'h', b'i', // regexp
81            0x03, b'j', b'k', b'l', 0x00, // replacement
82        ],
83        Naptr {
84            order: 14,
85            preference: 15,
86            flags: unsafe { Characters::new_unchecked(b"abc") },
87            service: unsafe { Characters::new_unchecked(b"def") },
88            regexp: unsafe { Characters::new_unchecked(b"ghi") },
89            replacement: unsafe { DnsName::new_unchecked(b"\x03jkl\x00") },
90        },
91    );
92}