2017-12-27 14:24:11 +00:00
|
|
|
use bytes::{Bytes, Buf, BufMut};
|
2019-07-01 15:43:34 +00:00
|
|
|
use crate::errors::*;
|
|
|
|
use crate::common_types::*;
|
2017-12-21 12:32:14 +00:00
|
|
|
use failure::ResultExt;
|
2019-07-01 15:43:34 +00:00
|
|
|
use crate::ser::RRData;
|
|
|
|
use crate::ser::packet::{DnsPacketData, DnsPacketWriteContext, remaining_bytes};
|
|
|
|
use crate::ser::text::{DnsTextData, DnsTextFormatter, DnsTextContext, next_field};
|
2017-12-21 12:32:14 +00:00
|
|
|
use std::fmt;
|
|
|
|
use std::io::Read;
|
2017-12-27 20:50:51 +00:00
|
|
|
use std::net::{Ipv4Addr, Ipv6Addr};
|
2017-12-21 12:32:14 +00:00
|
|
|
|
|
|
|
// deriving RRData will add a unit test to make sure the type is
|
|
|
|
// registered; there must be a records::types::$name `Type` constant
|
|
|
|
// with the same name as the struct.
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, RRData)]
|
2017-12-26 16:30:08 +00:00
|
|
|
#[RRClass(ANY)]
|
2017-12-21 12:32:14 +00:00
|
|
|
pub enum LOC {
|
|
|
|
Version0(LOC0),
|
|
|
|
UnknownVersion{
|
|
|
|
version: u8,
|
|
|
|
data: Bytes,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DnsPacketData for LOC {
|
2017-12-27 14:24:11 +00:00
|
|
|
fn deserialize(data: &mut ::std::io::Cursor<Bytes>) -> Result<Self> {
|
2017-12-21 12:32:14 +00:00
|
|
|
let version: u8 = DnsPacketData::deserialize(data)?;
|
|
|
|
if 0 == version {
|
|
|
|
Ok(LOC::Version0(DnsPacketData::deserialize(data)?))
|
|
|
|
} else {
|
|
|
|
Ok(LOC::UnknownVersion{
|
|
|
|
version: version,
|
2017-12-27 20:50:51 +00:00
|
|
|
data: remaining_bytes(data),
|
2017-12-21 12:32:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-12-27 14:24:11 +00:00
|
|
|
|
|
|
|
fn serialize(&self, context: &mut DnsPacketWriteContext, packet: &mut Vec<u8>) -> Result<()> {
|
|
|
|
match *self {
|
|
|
|
LOC::Version0(ref l0) => {
|
|
|
|
packet.reserve(1);
|
|
|
|
packet.put_u8(0);
|
|
|
|
l0.serialize(context, packet)
|
|
|
|
},
|
|
|
|
LOC::UnknownVersion{version, ref data} => {
|
|
|
|
packet.reserve(data.len() + 1);
|
|
|
|
packet.put_u8(version);
|
|
|
|
packet.put_slice(data);
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 12:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DnsTextData for LOC {
|
2017-12-29 13:07:58 +00:00
|
|
|
fn dns_parse(_context: &DnsTextContext, data: &mut &str) -> Result<Self> {
|
|
|
|
let degrees_latitude = next_field(data)?.parse::<u8>()?;
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(degrees_latitude <= 90, "degrees latitude out of range: {}", degrees_latitude);
|
2017-12-29 13:07:58 +00:00
|
|
|
let mut minutes_latitude = 0;
|
|
|
|
let mut seconds_latitude = 0.0;
|
|
|
|
let mut field = next_field(data)?;
|
|
|
|
if field != "N" && field != "n" && field != "S" && field != "s" {
|
|
|
|
minutes_latitude = field.parse::<u8>()?;
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(minutes_latitude < 60, "minutes latitude out of range: {}", minutes_latitude);
|
2017-12-29 13:07:58 +00:00
|
|
|
field = next_field(data)?;
|
|
|
|
if field != "N" && field != "n" && field != "S" && field != "s" {
|
|
|
|
seconds_latitude = field.parse::<f32>()?;
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(seconds_latitude >= 0.0 && seconds_latitude < 60.0, "seconds latitude out of range: {}", seconds_latitude);
|
2017-12-29 13:07:58 +00:00
|
|
|
field = next_field(data)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let latitude_off = (3600_000 * degrees_latitude as u32) + (60_000 * minutes_latitude as u32) + (1_000.0 * seconds_latitude).round() as u32;
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(latitude_off <= 3600_000 * 180, "latitude out of range");
|
2017-12-29 13:07:58 +00:00
|
|
|
let latitude = match field {
|
|
|
|
"N"|"n" => 0x8000_0000 + latitude_off,
|
|
|
|
"S"|"s" => 0x8000_0000 - latitude_off,
|
2019-07-01 15:43:34 +00:00
|
|
|
_ => failure::bail!("invalid latitude orientation [NS]: {}", field),
|
2017-12-29 13:07:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let degrees_longitude = next_field(data)?.parse::<u8>()?;
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(degrees_longitude <= 180, "degrees longitude out of range: {}", degrees_longitude);
|
2017-12-29 13:07:58 +00:00
|
|
|
let mut minutes_longitude = 0;
|
|
|
|
let mut seconds_longitude = 0.0;
|
|
|
|
let mut field = next_field(data)?;
|
|
|
|
if field != "E" && field != "e" && field != "W" && field != "w" {
|
|
|
|
minutes_longitude = field.parse::<u8>()?;
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(minutes_longitude < 60, "minutes longitude out of range: {}", minutes_longitude);
|
2017-12-29 13:07:58 +00:00
|
|
|
field = next_field(data)?;
|
|
|
|
if field != "E" && field != "e" && field != "W" && field != "w" {
|
|
|
|
seconds_longitude = field.parse::<f32>()?;
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(seconds_longitude >= 0.0 && seconds_longitude < 60.0, "seconds longitude out of range: {}", seconds_longitude);
|
2017-12-29 13:07:58 +00:00
|
|
|
field = next_field(data)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let longitude_off = (3600_000 * degrees_longitude as u32) + (60_000 * minutes_longitude as u32) + (1_000.0 * seconds_longitude).round() as u32;
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(longitude_off <= 3600_000 * 180, "longitude out of range");
|
2017-12-29 13:07:58 +00:00
|
|
|
let longitude = match field {
|
|
|
|
"E"|"e" => 0x8000_0000 + longitude_off,
|
|
|
|
"W"|"w" => 0x8000_0000 - longitude_off,
|
2019-07-01 15:43:34 +00:00
|
|
|
_ => failure::bail!("invalid longitude orientation [EW]: {}", field),
|
2017-12-29 13:07:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
fn trim_unit_m(s: &str) -> &str {
|
|
|
|
if s.ends_with('m') { &s[..s.len()-1] } else { s }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_precision(s: &str) -> Result<u8> {
|
|
|
|
let s = trim_unit_m(s);
|
|
|
|
let mut m = 0;
|
|
|
|
let mut e = 0;
|
|
|
|
let mut dec_point = None;
|
|
|
|
for &b in s.as_bytes() {
|
|
|
|
if b == b'.' {
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(dec_point.is_none(), "invalid precision (double decimal point): {:?}", s);
|
2017-12-29 13:07:58 +00:00
|
|
|
dec_point = Some(0);
|
|
|
|
continue;
|
|
|
|
}
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(b >= b'0' && b <= b'9', "invalid precision (invalid character): {:?}", s);
|
2017-12-29 13:07:58 +00:00
|
|
|
if let Some(ref mut dp) = dec_point {
|
|
|
|
if *dp == 2 { continue; } // ignore following digits
|
|
|
|
*dp += 1;
|
|
|
|
}
|
|
|
|
let d = b - b'0';
|
|
|
|
if 0 == m {
|
|
|
|
m = d;
|
|
|
|
} else {
|
|
|
|
e += 1;
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(e <= 9, "invalid precision (overflow): {:?}", s);
|
2017-12-29 13:07:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
e += 2 - dec_point.unwrap_or(0);
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(e <= 9, "invalid precision (overflow): {:?}", s);
|
2017-12-29 13:07:58 +00:00
|
|
|
Ok(m << 4 | e)
|
|
|
|
}
|
|
|
|
|
|
|
|
let altitude = match next_field(data) {
|
|
|
|
Ok(field) => {
|
|
|
|
let f_altitude = trim_unit_m(field).parse::<f64>()?;
|
|
|
|
let altitude = (f_altitude * 100.0 + 10000000.0).round() as i64;
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(altitude > 0 && (altitude as u32) as i64 == altitude, "altitude out of range");
|
2017-12-29 13:07:58 +00:00
|
|
|
altitude as u32
|
|
|
|
},
|
|
|
|
// standard requires the field, but the example parser doesn't..
|
|
|
|
Err(_) => 10000000, // 0m
|
|
|
|
};
|
|
|
|
|
|
|
|
let size = match next_field(data) {
|
|
|
|
Ok(field) => parse_precision(field)?,
|
|
|
|
Err(_) => 0x12, // => 1e2 cm = 1m
|
|
|
|
};
|
|
|
|
|
|
|
|
let horizontal_precision = match next_field(data) {
|
|
|
|
Ok(field) => parse_precision(field)?,
|
|
|
|
Err(_) => 0x16, // 1e6 cm = 10km */
|
|
|
|
};
|
|
|
|
|
|
|
|
let vertical_precision = match next_field(data) {
|
|
|
|
Ok(field) => parse_precision(field)?,
|
|
|
|
Err(_) => 0x13, // 1e3 cm = 10m */
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(LOC::Version0(LOC0{
|
|
|
|
size,
|
|
|
|
horizontal_precision,
|
|
|
|
vertical_precision,
|
|
|
|
latitude,
|
|
|
|
longitude,
|
|
|
|
altitude,
|
|
|
|
}))
|
2017-12-21 12:32:14 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 13:07:58 +00:00
|
|
|
fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result {
|
|
|
|
let this = match *self {
|
|
|
|
LOC::Version0(ref t) => t,
|
|
|
|
_ => return Err(fmt::Error),
|
|
|
|
};
|
|
|
|
const MAX_LAT_OFFSET: u32 = 3600_000 * 180;
|
|
|
|
const MAX_LON_OFFSET: u32 = 3600_000 * 180;
|
|
|
|
const LATLON_MID: u32 = 0x8000_0000;
|
|
|
|
if this.latitude < LATLON_MID - MAX_LAT_OFFSET || this.latitude > LATLON_MID + MAX_LAT_OFFSET {
|
|
|
|
return Err(fmt::Error);
|
|
|
|
}
|
|
|
|
if this.longitude < LATLON_MID - MAX_LON_OFFSET || this.longitude > LATLON_MID + MAX_LON_OFFSET {
|
|
|
|
return Err(fmt::Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_invalid_prec(v: u8) -> bool {
|
|
|
|
// "leading-digit" << 4 | "exponent(base 10)"
|
|
|
|
// if the leading digit is 0, the exponent must be 0 too.
|
|
|
|
(v > 0x00 && v < 0x10) || (v >> 4) > 9 || (v & 0xf) > 9
|
|
|
|
}
|
|
|
|
if is_invalid_prec(this.size) || is_invalid_prec(this.horizontal_precision) || is_invalid_prec(this.vertical_precision) {
|
|
|
|
return Err(fmt::Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn putlatlon2(v: u32, f: &mut DnsTextFormatter) -> fmt::Result {
|
|
|
|
let msecs = v % 1000;
|
|
|
|
let fullsecs = v / 1000;
|
|
|
|
let secs = fullsecs % 60;
|
|
|
|
let fullmins = fullsecs / 60;
|
|
|
|
let mins = fullmins % 60;
|
|
|
|
let deg = fullmins / 60;
|
|
|
|
write!(f, "{} {} {}.{:03}", deg, mins, secs, msecs)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn putlatlon(v: u32, f: &mut DnsTextFormatter, pos: char, neg: char) -> fmt::Result {
|
|
|
|
if v >= LATLON_MID {
|
|
|
|
putlatlon2(v - LATLON_MID, f)?;
|
|
|
|
write!(f, "{}", pos)
|
|
|
|
} else {
|
|
|
|
putlatlon2(LATLON_MID - v, f)?;
|
|
|
|
write!(f, "{}", neg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
putlatlon(this.latitude, f, 'N', 'S')?;
|
|
|
|
putlatlon(this.longitude, f, 'E', 'W')?;
|
|
|
|
|
|
|
|
write!(f, "{:.2}m", (this.altitude as f64 - 10000000.0) / 100.0)?;
|
|
|
|
|
|
|
|
fn put_prec(v: u8, f: &mut DnsTextFormatter) -> fmt::Result {
|
|
|
|
let m = v >> 4;
|
|
|
|
debug_assert!(m < 10);
|
|
|
|
let e = v & 0xf;
|
|
|
|
if e >= 2 {
|
|
|
|
write!(f, "{:0<width$}.00m", m, width = e as usize - 1)
|
|
|
|
} else if e == 1 {
|
|
|
|
write!(f, ".{}0m", m)
|
|
|
|
} else { // e == 0
|
|
|
|
write!(f, ".0{}m", m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
put_prec(this.size, f)?;
|
|
|
|
put_prec(this.horizontal_precision, f)?;
|
|
|
|
put_prec(this.vertical_precision, f)?;
|
|
|
|
|
|
|
|
Ok(())
|
2017-12-21 12:32:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, DnsPacketData)]
|
|
|
|
pub struct LOC0 {
|
|
|
|
size: u8,
|
|
|
|
horizontal_precision: u8,
|
|
|
|
vertical_precision: u8,
|
|
|
|
latitude: u32,
|
|
|
|
longitude: u32,
|
|
|
|
altitude: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, RRData)]
|
2017-12-26 16:30:08 +00:00
|
|
|
#[RRClass(IN)]
|
2017-12-21 12:32:14 +00:00
|
|
|
pub struct A6 {
|
|
|
|
prefix: u8, // [0...128]
|
|
|
|
// might include non-zero padding
|
|
|
|
dirty_suffix: Ipv6Addr,
|
|
|
|
suffix: Ipv6Addr,
|
|
|
|
prefix_name: Option<DnsCanonicalName>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DnsPacketData for A6 {
|
2017-12-27 14:24:11 +00:00
|
|
|
fn deserialize(data: &mut ::std::io::Cursor<Bytes>) -> Result<Self> {
|
2017-12-21 12:32:14 +00:00
|
|
|
let prefix: u8 = DnsPacketData::deserialize(data)
|
|
|
|
.context("failed parsing field A6::prefix")?;
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(prefix <= 128, "invalid A6::prefix {}", prefix);
|
2017-12-21 12:32:14 +00:00
|
|
|
let suffix_offset = (prefix / 8) as usize;
|
|
|
|
debug_assert!(suffix_offset <= 16);
|
|
|
|
let suffix_len = 16 - suffix_offset;
|
|
|
|
check_enough_data!(data, suffix_len, "A6::suffix");
|
|
|
|
let mut addr = [0u8; 16];
|
|
|
|
data.read_exact(&mut addr[suffix_offset..16])?;
|
|
|
|
|
|
|
|
let dirty_suffix = Ipv6Addr::from(addr);
|
|
|
|
if suffix_offset < 16 {
|
|
|
|
let mask = 0xff >> (prefix % 8);
|
|
|
|
addr[suffix_offset] &= mask;
|
|
|
|
}
|
|
|
|
let suffix = Ipv6Addr::from(addr);
|
|
|
|
|
|
|
|
let prefix_name = if data.has_remaining() {
|
|
|
|
Some(DnsPacketData::deserialize(data)?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(A6 {
|
|
|
|
prefix,
|
|
|
|
dirty_suffix,
|
|
|
|
suffix,
|
|
|
|
prefix_name,
|
|
|
|
})
|
|
|
|
}
|
2017-12-27 14:24:11 +00:00
|
|
|
|
|
|
|
fn serialize(&self, context: &mut DnsPacketWriteContext, packet: &mut Vec<u8>) -> Result<()> {
|
|
|
|
let suffix_offset = (self.prefix / 8) as usize;
|
|
|
|
debug_assert!(suffix_offset <= 16);
|
|
|
|
let suffix = self.dirty_suffix.octets();
|
|
|
|
let suffix_data = &suffix[suffix_offset..];
|
|
|
|
packet.reserve(1 /* prefix */ + suffix_data.len());
|
|
|
|
packet.put_u8(self.prefix);
|
|
|
|
packet.put_slice(suffix_data);
|
|
|
|
if let Some(ref n) = self.prefix_name {
|
|
|
|
n.serialize(context, packet)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-12-21 12:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DnsTextData for A6 {
|
2017-12-27 14:24:11 +00:00
|
|
|
fn dns_parse(context: &DnsTextContext, data: &mut &str) -> Result<Self> {
|
2017-12-26 21:23:51 +00:00
|
|
|
let prefix: u8 = DnsTextData::dns_parse(context, data)
|
2017-12-21 12:32:14 +00:00
|
|
|
.context("failed parsing field A6::prefix")?;
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(prefix <= 128, "invalid A6::prefix {}", prefix);
|
2017-12-21 12:32:14 +00:00
|
|
|
|
|
|
|
let suffix_offset = (prefix / 8) as usize;
|
|
|
|
debug_assert!(suffix_offset <= 16);
|
|
|
|
|
2017-12-26 21:23:51 +00:00
|
|
|
let suffix: Ipv6Addr = DnsTextData::dns_parse(context, data)
|
2017-12-21 12:32:14 +00:00
|
|
|
.context("failed parsing field A6::suffix")?;
|
|
|
|
|
|
|
|
// clear prefix bits
|
|
|
|
let mut suffix = suffix.octets();
|
|
|
|
for i in 0..suffix_offset {
|
|
|
|
suffix[i] = 0;
|
|
|
|
}
|
|
|
|
if suffix_offset < 16 {
|
|
|
|
let mask = 0xff >> (prefix % 8);
|
|
|
|
suffix[suffix_offset] &= mask;
|
|
|
|
}
|
|
|
|
let suffix = Ipv6Addr::from(suffix);
|
|
|
|
|
|
|
|
let prefix_name = if !data.is_empty() {
|
2017-12-26 21:23:51 +00:00
|
|
|
Some(DnsTextData::dns_parse(context, data)
|
2017-12-21 12:32:14 +00:00
|
|
|
.context("failed parsing field A6::prefix_name")?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(A6 {
|
|
|
|
prefix,
|
|
|
|
dirty_suffix: suffix.clone(),
|
|
|
|
suffix,
|
|
|
|
prefix_name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result {
|
|
|
|
if self.dirty_suffix != self.suffix {
|
|
|
|
// parsing text clears the padding, so we would loose data.
|
|
|
|
// use binary representation instead.
|
|
|
|
return Err(fmt::Error);
|
|
|
|
}
|
|
|
|
write!(f, "{} {}", self.prefix, self.suffix)?;
|
|
|
|
if let Some(ref prefix_name) = self.prefix_name {
|
|
|
|
write!(f, "{}", prefix_name)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2017-12-27 20:50:51 +00:00
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum IpsecKeyGateway {
|
|
|
|
None,
|
|
|
|
Ipv4(Ipv4Addr),
|
|
|
|
Ipv6(Ipv6Addr),
|
|
|
|
Name(DnsName),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, RRData)]
|
|
|
|
#[RRClass(ANY)]
|
|
|
|
pub enum IPSECKEY {
|
|
|
|
Known{
|
|
|
|
precedence: u8,
|
|
|
|
algorithm: u8,
|
|
|
|
gateway: IpsecKeyGateway,
|
|
|
|
public_key: Base64RemainingBlob,
|
|
|
|
},
|
|
|
|
UnknownGateway{
|
|
|
|
precedence: u8,
|
|
|
|
gateway_type: u8,
|
|
|
|
algorithm: u8,
|
|
|
|
// length of gateway is unknown, can't split gateway and public key
|
|
|
|
remaining: Bytes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DnsPacketData for IPSECKEY {
|
|
|
|
fn deserialize(data: &mut ::std::io::Cursor<Bytes>) -> Result<Self> {
|
|
|
|
let precedence = u8::deserialize(data)?;
|
|
|
|
let gateway_type = u8::deserialize(data)?;
|
|
|
|
let algorithm = u8::deserialize(data)?;
|
|
|
|
let gateway = match gateway_type {
|
|
|
|
0 => IpsecKeyGateway::None,
|
|
|
|
1 => IpsecKeyGateway::Ipv4(Ipv4Addr::deserialize(data)?),
|
|
|
|
2 => IpsecKeyGateway::Ipv6(Ipv6Addr::deserialize(data)?),
|
|
|
|
3 => IpsecKeyGateway::Name(DnsName::deserialize(data)?),
|
|
|
|
_ => return Ok(IPSECKEY::UnknownGateway{
|
|
|
|
precedence,
|
|
|
|
gateway_type,
|
|
|
|
algorithm,
|
|
|
|
remaining: remaining_bytes(data),
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
Ok(IPSECKEY::Known{
|
|
|
|
precedence,
|
|
|
|
algorithm,
|
|
|
|
gateway,
|
|
|
|
public_key: Base64RemainingBlob::deserialize(data)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize(&self, context: &mut DnsPacketWriteContext, packet: &mut Vec<u8>) -> Result<()> {
|
|
|
|
match *self {
|
|
|
|
IPSECKEY::Known{precedence, algorithm, ref gateway, ref public_key} => {
|
|
|
|
packet.reserve(3);
|
|
|
|
packet.put_u8(precedence);
|
|
|
|
let gateway_type: u8 = match *gateway {
|
|
|
|
IpsecKeyGateway::None => 0,
|
|
|
|
IpsecKeyGateway::Ipv4(_) => 1,
|
|
|
|
IpsecKeyGateway::Ipv6(_) => 2,
|
|
|
|
IpsecKeyGateway::Name(_) => 3,
|
|
|
|
};
|
|
|
|
packet.put_u8(gateway_type);
|
|
|
|
packet.put_u8(algorithm);
|
|
|
|
match *gateway {
|
|
|
|
IpsecKeyGateway::None => (),
|
|
|
|
IpsecKeyGateway::Ipv4(ref a) => a.serialize(context, packet)?,
|
|
|
|
IpsecKeyGateway::Ipv6(ref a) => a.serialize(context, packet)?,
|
|
|
|
IpsecKeyGateway::Name(ref n) => n.serialize(context, packet)?,
|
|
|
|
};
|
|
|
|
public_key.serialize(context, packet)?;
|
|
|
|
},
|
|
|
|
IPSECKEY::UnknownGateway{precedence, gateway_type, algorithm, ref remaining} => {
|
|
|
|
packet.reserve(3 + remaining.len());
|
|
|
|
packet.put_u8(precedence);
|
|
|
|
packet.put_u8(gateway_type);
|
|
|
|
packet.put_u8(algorithm);
|
|
|
|
packet.put_slice(remaining);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DnsTextData for IPSECKEY {
|
|
|
|
fn dns_parse(context: &DnsTextContext, data: &mut &str) -> Result<Self> {
|
|
|
|
let precedence = u8::dns_parse(context, data)?;
|
|
|
|
let gateway_type = u8::dns_parse(context, data)?;
|
|
|
|
let algorithm = u8::dns_parse(context, data)?;
|
|
|
|
let gateway = match gateway_type {
|
|
|
|
0 => IpsecKeyGateway::None,
|
|
|
|
1 => IpsecKeyGateway::Ipv4(Ipv4Addr::dns_parse(context, data)?),
|
|
|
|
2 => IpsecKeyGateway::Ipv6(Ipv6Addr::dns_parse(context, data)?),
|
|
|
|
3 => IpsecKeyGateway::Name(DnsName::dns_parse(context, data)?),
|
2019-07-01 15:43:34 +00:00
|
|
|
_ => failure::bail!("unknown gateway type {} for IPSECKEY", gateway_type),
|
2017-12-27 20:50:51 +00:00
|
|
|
};
|
|
|
|
Ok(IPSECKEY::Known{
|
|
|
|
precedence,
|
|
|
|
algorithm,
|
|
|
|
gateway,
|
|
|
|
public_key: Base64RemainingBlob::dns_parse(context, data)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
IPSECKEY::Known{precedence, algorithm, ref gateway, ref public_key} => {
|
|
|
|
let gateway_type: u8 = match *gateway {
|
|
|
|
IpsecKeyGateway::None => 0,
|
|
|
|
IpsecKeyGateway::Ipv4(_) => 1,
|
|
|
|
IpsecKeyGateway::Ipv6(_) => 2,
|
|
|
|
IpsecKeyGateway::Name(_) => 3,
|
|
|
|
};
|
|
|
|
write!(f, "{} {} {}", precedence, gateway_type, algorithm)?;
|
|
|
|
match *gateway {
|
|
|
|
IpsecKeyGateway::None => (),
|
|
|
|
IpsecKeyGateway::Ipv4(ref a) => a.dns_format(f)?,
|
|
|
|
IpsecKeyGateway::Ipv6(ref a) => a.dns_format(f)?,
|
|
|
|
IpsecKeyGateway::Name(ref n) => n.dns_format(f)?,
|
|
|
|
};
|
|
|
|
public_key.dns_format(f)?;
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
IPSECKEY::UnknownGateway{..} => Err(fmt::Error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|