rust-dnsbox/lib/dnsbox-base/src/ser/rrdata.rs

90 lines
2.3 KiB
Rust
Raw Normal View History

2017-12-21 12:32:14 +00:00
use bytes::Bytes;
2017-12-26 21:23:47 +00:00
use common_types::{Class, Type, classes};
2017-12-21 12:32:14 +00:00
use errors::*;
use ser::DnsPacketData;
2017-12-26 21:23:51 +00:00
use ser::text::{DnsTextData, DnsTextFormatter, DnsTextContext};
2017-12-26 21:23:47 +00:00
use std::borrow::Cow;
2017-12-26 16:30:08 +00:00
use std::fmt;
use std::io::Cursor;
pub trait RRDataPacket {
fn deserialize_rr_data(ttl: u32, rr_class: Class, rr_type: Type, data: &mut Cursor<Bytes>) -> Result<Self>
where
Self: Sized,
;
2017-12-26 21:23:47 +00:00
fn rr_type(&self) -> Type;
2017-12-26 16:30:08 +00:00
}
impl<T: DnsPacketData + StaticRRData> RRDataPacket for T {
fn deserialize_rr_data(_ttl: u32, rr_class: Class, rr_type: Type, data: &mut Cursor<Bytes>) -> Result<Self> {
ensure!(rr_type == T::TYPE, "type mismatch");
if T::CLASS != classes::ANY {
2017-12-26 21:23:47 +00:00
ensure!(rr_class == T::CLASS, "class mismatch: got {}, need {}", rr_class, T::CLASS);
2017-12-26 16:30:08 +00:00
}
T::deserialize(data)
}
2017-12-26 21:23:47 +00:00
fn rr_type(&self) -> Type {
T::TYPE
}
2017-12-26 16:30:08 +00:00
}
2017-12-26 21:23:47 +00:00
pub trait RRDataText {
2017-12-26 21:23:51 +00:00
fn dns_parse_rr_data(context: &DnsTextContext, data: &mut &str) -> Result<Self>
2017-12-26 16:30:08 +00:00
where
Self: Sized,
;
2017-12-21 12:32:14 +00:00
2017-12-26 16:30:08 +00:00
// format might fail if there is no (known) text representation.
fn dns_format_rr_data(&self, f: &mut DnsTextFormatter) -> fmt::Result;
2017-12-26 21:23:47 +00:00
2017-12-27 11:41:54 +00:00
fn rr_type_txt(&self) -> Cow<'static, str>;
2017-12-26 21:23:47 +00:00
// (type, rrdata)
fn text(&self) -> Result<(String, String)> {
let mut buf = String::new();
2017-12-27 11:41:54 +00:00
match self.dns_format_rr_data(&mut DnsTextFormatter::new(&mut buf)) {
2017-12-26 21:23:47 +00:00
Ok(()) => {
return Ok((self.rr_type_txt().into(), buf))
},
Err(_) => (),
}
buf.clear();
unimplemented!()
}
2017-12-21 12:32:14 +00:00
}
2017-12-26 21:23:47 +00:00
impl<T: DnsTextData + StaticRRData> RRDataText for T {
2017-12-26 21:23:51 +00:00
fn dns_parse_rr_data(context: &DnsTextContext, data: &mut &str) -> Result<Self>
2017-12-26 21:23:47 +00:00
where
Self: Sized,
{
2017-12-26 21:23:51 +00:00
ensure!(context.record_type() == Some(T::TYPE), "type mismatch");
let rr_class = context.zone_class().expect("require zone CLASS to parse record");
2017-12-26 21:23:47 +00:00
if T::CLASS != classes::ANY {
ensure!(rr_class == T::CLASS, "class mismatch: got {}, need {}", rr_class, T::CLASS);
}
2017-12-26 21:23:51 +00:00
ensure!(context.last_ttl().is_some(), "require TTL to parse record");
T::dns_parse(context, data)
2017-12-26 21:23:47 +00:00
}
fn dns_format_rr_data(&self, f: &mut DnsTextFormatter) -> fmt::Result {
self.dns_format(f)
2017-12-21 12:32:14 +00:00
}
2017-12-26 21:23:47 +00:00
2017-12-27 11:41:54 +00:00
fn rr_type_txt(&self) -> Cow<'static, str> {
Cow::Borrowed(T::NAME)
2017-12-26 21:23:47 +00:00
}
}
pub trait RRData: RRDataPacket + RRDataText {
2017-12-21 12:32:14 +00:00
}
2017-12-16 20:58:18 +00:00
2017-12-21 12:32:14 +00:00
pub trait StaticRRData: RRData {
const TYPE: Type;
const NAME: &'static str;
2017-12-26 16:30:08 +00:00
// classes::ANY marks class independent types
const CLASS: Class;
2017-12-16 20:58:18 +00:00
}