2017-12-21 12:32:14 +00:00
|
|
|
use std::fmt;
|
|
|
|
use std::net::{Ipv4Addr, Ipv6Addr};
|
2019-07-01 15:43:34 +00:00
|
|
|
use crate::ser::text::{DnsTextData, DnsTextFormatter, DnsTextContext, next_field};
|
2017-12-21 12:32:14 +00:00
|
|
|
|
2019-07-02 13:27:08 +00:00
|
|
|
impl DnsTextData for () {
|
|
|
|
fn dns_parse(_context: &DnsTextContext, _data: &mut &str) -> crate::errors::Result<Self> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dns_format(&self, _f: &mut DnsTextFormatter) -> fmt::Result {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ?Sized> DnsTextData for std::marker::PhantomData<T> {
|
|
|
|
fn dns_parse(_context: &DnsTextContext, _data: &mut &str) -> crate::errors::Result<Self> {
|
|
|
|
Ok(std::marker::PhantomData)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dns_format(&self, _f: &mut DnsTextFormatter) -> fmt::Result {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 12:32:14 +00:00
|
|
|
/* only decimal representations are used for numbers */
|
|
|
|
impl DnsTextData for u8 {
|
2019-07-01 15:43:34 +00:00
|
|
|
fn dns_parse(_context: &DnsTextContext, data: &mut &str) -> crate::errors::Result<Self> {
|
2017-12-21 12:32:14 +00:00
|
|
|
Ok(next_field(data)?.parse()?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DnsTextData for u16 {
|
2019-07-01 15:43:34 +00:00
|
|
|
fn dns_parse(_context: &DnsTextContext, data: &mut &str) -> crate::errors::Result<Self> {
|
2017-12-21 12:32:14 +00:00
|
|
|
Ok(next_field(data)?.parse()?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DnsTextData for u32 {
|
2019-07-01 15:43:34 +00:00
|
|
|
fn dns_parse(_context: &DnsTextContext, data: &mut &str) -> crate::errors::Result<Self> {
|
2017-12-21 12:32:14 +00:00
|
|
|
Ok(next_field(data)?.parse()?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only decimal representations are needed for octets */
|
|
|
|
impl DnsTextData for Ipv4Addr {
|
2019-07-01 15:43:34 +00:00
|
|
|
fn dns_parse(_context: &DnsTextContext, data: &mut &str) -> crate::errors::Result<Self> {
|
2017-12-21 12:32:14 +00:00
|
|
|
Ok(next_field(data)?.parse()?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* representation as in RFC 3513: https://tools.ietf.org/html/rfc3513#section-2.2 */
|
|
|
|
impl DnsTextData for Ipv6Addr {
|
2019-07-01 15:43:34 +00:00
|
|
|
fn dns_parse(_context: &DnsTextContext, data: &mut &str) -> crate::errors::Result<Self> {
|
2017-12-21 12:32:14 +00:00
|
|
|
Ok(next_field(data)?.parse()?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2019-07-01 15:43:34 +00:00
|
|
|
fn deserialize<T: DnsTextData>(data: &str) -> crate::errors::Result<T> {
|
2017-12-26 21:23:51 +00:00
|
|
|
let context = DnsTextContext::new();
|
2017-12-21 12:32:14 +00:00
|
|
|
let mut data = data;
|
2017-12-26 21:23:51 +00:00
|
|
|
let res = T::dns_parse(&context, &mut data)?;
|
2017-12-21 12:32:14 +00:00
|
|
|
let data = data.trim();
|
2019-07-01 15:43:34 +00:00
|
|
|
failure::ensure!(data.is_empty(), "didn't read data completely");
|
2017-12-21 12:32:14 +00:00
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ipv6() {
|
|
|
|
assert_eq!(
|
|
|
|
deserialize::<Ipv6Addr>("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210").unwrap(),
|
|
|
|
Ipv6Addr::new(
|
|
|
|
0xfedc, 0xba98, 0x7654, 0x3210, 0xfedc, 0xba98, 0x7654, 0x3210
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
deserialize::<Ipv6Addr>("1080::8:800:200C:417A").unwrap(),
|
|
|
|
Ipv6Addr::new(
|
|
|
|
0x1080, 0, 0, 0, 0x8, 0x800, 0x200c, 0x417a
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
deserialize::<Ipv6Addr>("::13.1.68.3").unwrap(),
|
|
|
|
Ipv6Addr::new(
|
|
|
|
0, 0, 0, 0, 0, 0, 0x0d01, 0x4403
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
deserialize::<Ipv6Addr>("::FFFF:129.144.52.38").unwrap(),
|
|
|
|
Ipv6Addr::new(
|
|
|
|
0, 0, 0, 0, 0, 0xffff, 0x8190, 0x3426
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2019-07-02 13:27:08 +00:00
|
|
|
|
|
|
|
#[derive(DnsTextData, PartialEq, Eq, Debug)]
|
|
|
|
struct Unit;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unit() {
|
|
|
|
assert_eq!(deserialize::<Unit>("").unwrap(), Unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(DnsTextData, PartialEq, Eq, Debug)]
|
|
|
|
struct Tuple(u16);
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tuple() {
|
|
|
|
assert_eq!(deserialize::<Tuple>("1234").unwrap(), Tuple(1234));
|
|
|
|
}
|
2017-12-21 12:32:14 +00:00
|
|
|
}
|