Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OutputError and improve handling of invalid RData #22

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 78 additions & 97 deletions src/output/dig.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
//! An output format compatible with dig.

use std::io;
use crate::client::Answer;
use domain::base::iana::Rtype;
use domain::base::opt::AllOptData;
use domain::base::ParsedRecord;
use domain::rdata::AllRecordData;
use crate::client::Answer;
use std::io;

use super::error::OutputError;

//------------ write ---------------------------------------------------------

pub fn write(
answer: &Answer,
target: &mut impl io::Write,
) -> Result<(), io::Error> {
pub fn write(answer: &Answer, target: &mut impl io::Write) -> Result<(), OutputError> {
let msg = answer.msg_slice();

// Header
let header = msg.header();
let counts = msg.header_counts();

writeln!(target,
writeln!(
target,
";; ->>HEADER<<- opcode: {}, rcode: {}, id: {}",
header.opcode(), header.rcode(), header.id()
header.opcode(),
header.rcode(),
header.id()
)?;
write!(target, ";; flags: {}", header.flags())?;
writeln!(target,
writeln!(
target,
"; QUERY: {}, ANSWER: {}, AUTHORITY: {}, ADDITIONAL: {}",
counts.qdcount(), counts.ancount(), counts.nscount(), counts.arcount()
counts.qdcount(),
counts.ancount(),
counts.nscount(),
counts.arcount()
)?;

let opt = msg.opt(); // We need it further down ...

if let Some(opt) = opt.as_ref() {
writeln!(target, "\n;; OPT PSEUDOSECTION:")?;
writeln!(target,
writeln!(
target,
"; EDNS: version {}; flags: {}; udp: {}",
opt.version(), opt.dnssec_ok(), opt.udp_payload_size()
opt.version(),
opt.dnssec_ok(),
opt.udp_payload_size()
)?;
for option in opt.opt().iter::<AllOptData<_, _>>() {
use AllOptData::*;
Expand All @@ -45,35 +55,19 @@ pub fn write(
Dau(dau) => writeln!(target, "; DAU: {}", dau)?,
Dhu(dhu) => writeln!(target, "; DHU: {}", dhu)?,
N3u(n3u) => writeln!(target, "; N3U: {}", n3u)?,
Expire(expire) => {
writeln!(target, "; EXPIRE: {}", expire)?
}
TcpKeepalive(opt) => {
writeln!(target, "; TCPKEEPALIVE: {}", opt)?
}
Padding(padding) => {
writeln!(target, "; PADDING: {}", padding)?
}
ClientSubnet(opt) => {
writeln!(target, "; CLIENTSUBNET: {}", opt)?
}
Cookie(cookie) => {
writeln!(target, "; COOKIE: {}", cookie)?
}
Chain(chain) => {
writeln!(target, "; CHAIN: {}", chain)?
}
KeyTag(keytag) => {
writeln!(target, "; KEYTAG: {}", keytag)?
}
ExtendedError(extendederror) => {
writeln!(target, "; EDE: {}", extendederror)?
}
Expire(expire) => writeln!(target, "; EXPIRE: {}", expire)?,
TcpKeepalive(opt) => writeln!(target, "; TCPKEEPALIVE: {}", opt)?,
Padding(padding) => writeln!(target, "; PADDING: {}", padding)?,
ClientSubnet(opt) => writeln!(target, "; CLIENTSUBNET: {}", opt)?,
Cookie(cookie) => writeln!(target, "; COOKIE: {}", cookie)?,
Chain(chain) => writeln!(target, "; CHAIN: {}", chain)?,
KeyTag(keytag) => writeln!(target, "; KEYTAG: {}", keytag)?,
ExtendedError(extendederror) => writeln!(target, "; EDE: {}", extendederror)?,
Other(other) => {
writeln!(target, "; {}", other.code())?;
}
_ => writeln!(target, "Unknown OPT")?,
}
},
Err(err) => {
writeln!(target, "; ERROR: bad option: {}.", err)?;
}
Expand All @@ -86,95 +80,57 @@ pub fn write(
if counts.qdcount() > 0 {
write!(target, ";; QUESTION SECTION:")?;
for item in questions {
match item {
Ok(item) => writeln!(target, "; {}", item)?,
Err(err) => {
writeln!(target, "; ERROR: bad question: {}.", err)?;
return Ok(())
}
}
let item = item?;
writeln!(target, "; {}", item)?;
}
}

/* Answer */
let mut section = match questions.answer() {
Ok(section) => section.limit_to::<AllRecordData<_, _>>(),
Err(err) => {
writeln!(target, "; ERROR: bad question: {}.", err)?;
return Ok(())
}
};

// Answer
let section = questions.answer()?;
if counts.ancount() > 0 {
writeln!(target, "\n;; ANSWER SECTION:")?;
for item in section.by_ref() {
match item {
Ok(item) => writeln!(target, "{}", item)?,
Err(err) => {
writeln!(target, "; Error: bad record: {}.", err)?;
return Ok(())
}
}
for item in section {
write_record_item(target, &item?)?;
}
}

// Authority
let mut section = match section.next_section() {
Ok(section) => section.unwrap().limit_to::<AllRecordData<_, _>>(),
Err(err) => {
writeln!(target, "; ERROR: bad record: {}.", err)?;
return Ok(())
}
};
let section = section.next_section()?.unwrap();
if counts.nscount() > 0 {
writeln!(target, "\n;; AUTHORITY SECTION:")?;
for item in section.by_ref() {
match item {
Ok(item) => writeln!(target, "{}", item)?,
Err(err) => {
writeln!(target, "; Error: bad record: {}.", err)?;
return Ok(())
}
}
for item in section {
write_record_item(target, &item?)?;
}
}

// Additional
let section = match section.next_section() {
Ok(section) => section.unwrap().limit_to::<AllRecordData<_, _>>(),
Err(err) => {
writeln!(target, "; ERROR: bad record: {}.", err)?;
return Ok(())
}
};
let section = section.next_section()?.unwrap();
if counts.arcount() > 1 || (opt.is_none() && counts.arcount() > 0) {
writeln!(target, "\n;; ADDITIONAL SECTION:")?;
for item in section {
match item {
Ok(item) => {
if item.rtype() != Rtype::OPT {
writeln!(target, "{}", item)?
}
}
Err(err) => {
writeln!(target, "; Error: bad record: {}.", err)?;
return Ok(())
}
let item = item?;
if item.rtype() != Rtype::OPT {
write_record_item(target, &item)?;
}
}
}

// Stats
let stats = answer.stats();
writeln!(target,
writeln!(
target,
"\n;; Query time: {} msec",
stats.duration.num_milliseconds()
)?;
writeln!(target,
writeln!(
target,
";; SERVER: {}#{} ({})",
stats.server_addr.ip(), stats.server_addr.port(),
stats.server_addr.ip(),
stats.server_addr.port(),
stats.server_proto
)?;
writeln!(target,
writeln!(
target,
";; WHEN: {}",
stats.start.format("%a %b %d %H:%M:%S %Z %Y")
)?;
Expand All @@ -183,3 +139,28 @@ pub fn write(
Ok(())
}

fn write_record_item(
target: &mut impl io::Write,
item: &ParsedRecord<&[u8]>,
) -> Result<(), io::Error> {
let parsed = item.to_any_record::<AllRecordData<_, _>>();

if parsed.is_err() {
write!(target, "; ")?;
}

let data = match parsed {
Ok(item) => item.data().to_string(),
Err(_) => "<invalid data>".into(),
};

writeln!(
target,
"{} {} {} {} {}",
item.owner(),
item.ttl().as_secs(),
item.class(),
item.rtype(),
data
)
}
20 changes: 20 additions & 0 deletions src/output/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::io;

use domain::base::wire::ParseError;

pub enum OutputError {
Io(io::Error),
BadRecord(ParseError),
}

impl From<io::Error> for OutputError {
fn from(value: io::Error) -> Self {
Self::Io(value)
}
}

impl From<ParseError> for OutputError {
fn from(value: ParseError) -> Self {
Self::BadRecord(value)
}
}
Loading
Loading