flex_dns/rdata/
caa.rs

1use crate::{Buffer, DnsMessage, DnsMessageError, MutBuffer};
2use crate::characters::Characters;
3use crate::parse::Parse;
4use crate::rdata::{RData, RDataParse};
5use crate::write::WriteBytes;
6
7/// # Certificate authority authorization record (CAA)
8/// This record is used to specify which certificate authorities (CAs) are
9/// allowed to issue certificates for a domain.
10#[derive(Copy, Clone, Debug, PartialEq)]
11pub struct Caa<'a> {
12    /// The flags field is used to specify critical CAA flags.
13    pub flags: u8,
14    /// The tag field is used to specify the property represented by the record.
15    pub tag: Characters<'a>,
16    /// The value field is used to specify the value of the property.
17    pub value: Characters<'a>,
18}
19
20impl<'a> RDataParse<'a> for Caa<'a> {
21    #[inline]
22    fn parse(rdata: &RData<'a>, i: &mut usize) -> Result<Self, DnsMessageError> {
23        let flags = u8::parse(rdata, i)?;
24        let tag = Characters::parse(rdata, i)?;
25        let value = Characters::parse(rdata, i)?;
26
27        Ok(Self {
28            flags,
29            tag,
30            value,
31        })
32    }
33}
34
35impl<'a> WriteBytes for Caa<'a> {
36    #[inline]
37    fn write<
38        const PTR_STORAGE: usize,
39        const DNS_SECTION: usize,
40        B: MutBuffer + Buffer,
41    >(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
42        let mut bytes = 0;
43
44        bytes += self.flags.write(message)?;
45        bytes += self.tag.write(message)?;
46        bytes += self.value.write(message)?;
47
48        Ok(bytes)
49    }
50}
51
52#[cfg(test)]
53mod test {
54    use crate::rdata::testutils::parse_write_test;
55
56    use super::*;
57
58    parse_write_test!(
59        23,
60        [
61            0x42, // flags
62            0x05, // tag length
63            b'i', b's', b's', b'u', b'e', // tag
64            0x0f, // value length
65            b'w', b'w', b'w', b'.', b'e', b'x', b'a', b'm', b'p', b'l', b'e', b't',
66            b'e', b's', b't', // value
67        ],
68        Caa {
69            flags: 0x42,
70            tag: unsafe { Characters::new_unchecked(b"issue") },
71            value: unsafe { Characters::new_unchecked(b"www.exampletest") },
72        },
73    );
74}