flex_dns/rdata/
uri.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/// # Uniform resource identifier record (URI)
8/// This record is used to publish mappings from hostnames to URIs.
9#[derive(Copy, Clone, Debug, PartialEq)]
10pub struct Uri<'a> {
11    /// The priority of this URI record. Lower values are preferred.
12    pub priority: u16,
13    /// The weight of this URI record. Higher values are preferred.
14    pub weight: u16,
15    /// The target URI.
16    pub target: Characters<'a>,
17}
18
19impl<'a> RDataParse<'a> for Uri<'a> {
20    #[inline]
21    fn parse(rdata: &RData<'a>, i: &mut usize) -> Result<Self, DnsMessageError> {
22        let priority = u16::parse(rdata, i)?;
23        let weight = u16::parse(rdata, i)?;
24        let target = Characters::parse(rdata, i)?;
25
26        Ok(Self {
27            priority,
28            weight,
29            target,
30        })
31    }
32}
33
34impl<'a> WriteBytes for Uri<'a> {
35    #[inline]
36    fn write<
37        const PTR_STORAGE: usize,
38        const DNS_SECTION: usize,
39        B: MutBuffer + Buffer,
40    >(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
41        let mut bytes = 0;
42
43        bytes += self.priority.write(message)?;
44        bytes += self.weight.write(message)?;
45        bytes += self.target.write(message)?;
46
47        Ok(bytes)
48    }
49}
50
51#[cfg(test)]
52mod test {
53    use crate::rdata::testutils::parse_write_test;
54
55    use super::*;
56
57    parse_write_test!(
58        8,
59        [
60            0x00, 0x0e, // priority
61            0x00, 0x0e, // weight
62            0x03, // length of "www"
63            b'w', b'w', b'w', // "www"
64        ],
65        Uri {
66            priority: 14,
67            weight: 14,
68            target: unsafe { Characters::new_unchecked(b"www") },
69        },
70    );
71}