You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.5 KiB

5 years ago
use bytes::{Bytes, BufMut};
5 years ago
use common_types::*;
5 years ago
use common_types::binary::HEXLOWER_PERMISSIVE_ALLOW_WS;
use errors::*;
5 years ago
use failure::{ResultExt, Fail};
5 years ago
use ser::packet::{DnsPacketWriteContext, remaining_bytes};
5 years ago
use ser::{RRData, RRDataPacket, RRDataText};
use ser::text::{DnsTextFormatter, DnsTextContext, next_field};
use std::borrow::Cow;
5 years ago
use std::fmt;
5 years ago
use std::io::Cursor;
5 years ago
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct UnknownRecord {
rr_type: Type,
raw: Bytes,
}
impl UnknownRecord {
pub fn new(rr_type: Type, raw: Bytes) -> Self {
UnknownRecord {
rr_type: rr_type,
raw: raw,
}
}
pub fn deserialize(rr_type: Type, data: &mut ::std::io::Cursor<Bytes>) -> Result<Self> {
Ok(UnknownRecord::new(rr_type, remaining_bytes(data)))
}
pub fn dns_parse(rr_type: Type, data: &mut &str) -> Result<Self> {
let field = next_field(data)?;
ensure!(field == r"\#", "expect \\# token to mark generic encoding");
let field = next_field(data).context("generic record data length")?;
let len: usize = field.parse()?;
let result = HEXLOWER_PERMISSIVE_ALLOW_WS.decode(data.as_bytes())
.with_context(|e| e.context(format!("invalid hex: {:?}", data)))?;
ensure!(len == result.len(), "length {} doesn't match length of encoded data {}", len, result.len());
5 years ago
*data = ""; // read all data
5 years ago
Ok(UnknownRecord {
rr_type,
raw: result.into(),
})
}
}
5 years ago
impl RRDataPacket for UnknownRecord {
fn deserialize_rr_data(_ttl: u32, _rr_class: Class, rr_type: Type, data: &mut Cursor<Bytes>) -> Result<Self> {
UnknownRecord::deserialize(rr_type, data)
}
5 years ago
fn rr_type(&self) -> Type {
self.rr_type
}
5 years ago
fn serialize_rr_data(&self, _context: &mut DnsPacketWriteContext, packet: &mut Vec<u8>) -> Result<()> {
packet.reserve(self.raw.len());
packet.put_slice(&self.raw);
Ok(())
}
5 years ago
}
5 years ago
impl RRDataText for UnknownRecord {
fn dns_parse_rr_data(context: &DnsTextContext, data: &mut &str) -> Result<Self>
where
Self: Sized,
{
let t = match context.record_type() {
Some(t) => t,
None => bail!("must parse DNS record with record type context"),
};
UnknownRecord::dns_parse(t, data)
}
5 years ago
/// this must never fail unless the underlying buffer fails.
5 years ago
fn dns_format_rr_data(&self, f: &mut DnsTextFormatter) -> fmt::Result {
write!(f, "\\# {} {}", self.raw.len(), HEXLOWER_PERMISSIVE_ALLOW_WS.encode(&self.raw))
}
fn rr_type_txt(&self) -> Cow<'static, str> {
Cow::Owned(self.rr_type.generic_name())
}
}
impl RRData for UnknownRecord {
5 years ago
fn clone_box(&self) -> Box<RRData> {
Box::new(self.clone()) as _
}
5 years ago
}