flex_dns/rdata/
loc.rs

1use crate::{Buffer, DnsMessage, DnsMessageError, MutBuffer};
2use crate::parse::Parse;
3use crate::rdata::{RData, RDataParse};
4use crate::write::WriteBytes;
5
6/// # Location information
7/// This record is used to return a location for a host
8#[derive(Copy, Clone, Debug, PartialEq)]
9pub struct Loc {
10    /// The version of the location record
11    pub version: u8,
12    /// The size of the location record
13    pub size: u8,
14    /// The horizontal precision of the location record
15    pub horizontal_precision: u8,
16    /// The vertical precision of the location record
17    pub vertical_precision: u8,
18    /// The latitude of the location record
19    pub latitude: u32,
20    /// The longitude of the location record
21    pub longitude: u32,
22    /// The altitude of the location record
23    pub altitude: u32,
24}
25
26impl<'a> RDataParse<'a> for Loc {
27    #[inline]
28    fn parse(rdata: &RData<'a>, i: &mut usize) -> Result<Self, DnsMessageError> {
29        let version = u8::parse(rdata.buffer, i)?;
30        let size = u8::parse(rdata.buffer, i)?;
31        let horizontal_precision = u8::parse(rdata.buffer, i)?;
32        let vertical_precision = u8::parse(rdata.buffer, i)?;
33        let latitude = u32::parse(rdata.buffer, i)?;
34        let longitude = u32::parse(rdata.buffer, i)?;
35        let altitude = u32::parse(rdata.buffer, i)?;
36
37        Ok(Self {
38            version,
39            size,
40            horizontal_precision,
41            vertical_precision,
42            latitude,
43            longitude,
44            altitude,
45        })
46    }
47}
48
49impl WriteBytes for Loc {
50    #[inline]
51    fn write<
52        const PTR_STORAGE: usize,
53        const DNS_SECTION: usize,
54        B: MutBuffer + Buffer,
55    >(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
56        let mut bytes = 0;
57
58        bytes += self.version.write(message)?;
59        bytes += self.size.write(message)?;
60        bytes += self.horizontal_precision.write(message)?;
61        bytes += self.vertical_precision.write(message)?;
62        bytes += self.latitude.write(message)?;
63        bytes += self.longitude.write(message)?;
64        bytes += self.altitude.write(message)?;
65
66        Ok(bytes)
67    }
68}
69
70#[cfg(test)]
71mod test {
72    use crate::rdata::testutils::parse_write_test;
73
74    use super::*;
75
76    parse_write_test!(
77        16,
78        [
79            0x0e, // version
80            0x0d, // size
81            0x0c, // horizontal precision
82            0x0b, // vertical precision
83            0x00, 0x00, 0x00, 0x0a, // latitude
84            0x00, 0x00, 0x00, 0x0b, // longitude
85            0x00, 0x00, 0x00, 0x0c, // altitude
86        ],
87        Loc {
88            version: 0x0e,
89            size: 0x0d,
90            horizontal_precision: 0x0c,
91            vertical_precision: 0x0b,
92            latitude: 0x0a,
93            longitude: 0x0b,
94            altitude: 0x0c,
95        },
96    );
97}