use crate::{dns::packet_part::PacketPart, SimpleDnsError};
use super::RR;
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct LOC {
pub version: u8,
pub size: u8,
pub horizontal_precision: u8,
pub vertical_precision: u8,
pub latitude: i32,
pub longitude: i32,
pub altitude: i32,
}
impl RR for LOC {
const TYPE_CODE: u16 = 29;
}
impl LOC {
pub fn into_owned(self) -> Self {
self
}
}
impl<'a> PacketPart<'a> for LOC {
fn parse(data: &'a [u8], position: usize) -> crate::Result<Self>
where
Self: Sized,
{
if data.len() < position + 16 {
return Err(SimpleDnsError::InsufficientData);
}
let data = &data[position..position + 16];
let version = u8::from_be(data[0]);
if version != 0 {
return Err(SimpleDnsError::InvalidDnsPacket);
}
let size = u8::from_be(data[1]);
let horizontal_precision = u8::from_be(data[2]);
let vertical_precision = u8::from_be(data[3]);
let latitude = i32::from_be_bytes(data[4..8].try_into()?);
let longitude = i32::from_be_bytes(data[8..12].try_into()?);
let altitude = i32::from_be_bytes(data[12..16].try_into()?);
Ok(LOC {
version,
size,
horizontal_precision,
vertical_precision,
latitude,
longitude,
altitude,
})
}
fn write_to<T: std::io::Write>(&self, out: &mut T) -> crate::Result<()> {
if self.version != 0 {
return Err(SimpleDnsError::InvalidDnsPacket);
}
out.write_all(&[
self.version.to_be(),
self.size.to_be(),
self.horizontal_precision.to_be(),
self.vertical_precision.to_be(),
])?;
out.write_all(&self.latitude.to_be_bytes())?;
out.write_all(&self.longitude.to_be_bytes())?;
out.write_all(&self.altitude.to_be_bytes())?;
Ok(())
}
fn len(&self) -> usize {
16
}
}
#[cfg(test)]
mod tests {
use crate::{rdata::RData, ResourceRecord};
use super::*;
#[test]
fn parse_and_write_loc() {
let loc = LOC {
version: 0,
size: 0x10,
vertical_precision: 0x11,
horizontal_precision: 0x12,
altitude: 1000,
longitude: 2000,
latitude: 3000,
};
let mut data = Vec::new();
assert!(loc.write_to(&mut data).is_ok());
let loc = LOC::parse(&data, 0);
assert!(loc.is_ok());
let loc = loc.unwrap();
assert_eq!(0x10, loc.size);
assert_eq!(0x11, loc.vertical_precision);
assert_eq!(0x12, loc.horizontal_precision);
assert_eq!(1000, loc.altitude);
assert_eq!(2000, loc.longitude);
assert_eq!(3000, loc.latitude);
assert_eq!(data.len(), loc.len());
}
#[test]
fn parse_sample() -> Result<(), Box<dyn std::error::Error>> {
let sample_file = std::fs::read("samples/zonefile/LOC.sample")?;
let sample_rdata = match ResourceRecord::parse(&sample_file, 0)?.rdata {
RData::LOC(rdata) => rdata,
_ => unreachable!(),
};
assert_eq!(35, sample_rdata.size);
assert_eq!(35, sample_rdata.vertical_precision);
assert_eq!(37, sample_rdata.horizontal_precision);
assert_eq!(10001000, sample_rdata.altitude);
assert_eq!(-2058743648, sample_rdata.longitude);
assert_eq!(-1930943648, sample_rdata.latitude);
Ok(())
}
}