Skip to content

Commit

Permalink
fixed bug on alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
dandyvica committed Jan 15, 2025
1 parent 693f109 commit aad8228
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Those with (*) are not yet fully tested.
You can also use a `TYPEn` where `n` is an integer <=255 for the query type.

## General usage
Usage is similar to __dig__, without support for options starting with `+`.
Usage is akin to __dig__, without support for options starting with `+`.
Example:
```console
$ dqy A www.google.com
Expand Down
15 changes: 8 additions & 7 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ Supported query types: {}
}

//───────────────────────────────────────────────────────────────────────────────────
// if no domain to query, by default set root (.)
// if --domain, take it
//───────────────────────────────────────────────────────────────────────────────────
if let Some(domain) = matches.get_one::<String>("domain") {
options.protocol.domain_string = domain.to_string();
Expand Down Expand Up @@ -921,12 +921,13 @@ Supported query types: {}
// finally convert domain as a string to a domain name
// internal domain name processing (IDNA)
//───────────────────────────────────────────────────────────────────────────────────
if options.protocol.domain_string.len() != options.protocol.domain_string.chars().count() {
let puny = idna::domain_to_ascii(&options.protocol.domain_string).map_err(Error::IDNA)?;
options.protocol.domain_name = DomainName::try_from(puny.as_str())?;
} else {
options.protocol.domain_name = DomainName::try_from(options.protocol.domain_string.as_str())?;
}
// if options.protocol.domain_string.len() != options.protocol.domain_string.chars().count() {
// let puny = idna::domain_to_ascii(&options.protocol.domain_string).map_err(Error::IDNA)?;
// options.protocol.domain_name = DomainName::try_from(puny.as_str())?;
// } else {
// options.protocol.domain_name = DomainName::try_from(options.protocol.domain_string.as_str())?;
// }
options.protocol.domain_name = DomainName::try_from(options.protocol.domain_string.as_str())?;

// for some types, use TCP instead of UDP right away
if options.protocol.qtype.contains(&QType::ANY)
Expand Down
40 changes: 29 additions & 11 deletions src/dns/rfc/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ const PUNY_HEADER: &[u8; 4] = b"xn--";
struct Label(Vec<u8>);

impl Label {
pub fn len(&self) -> usize {
self.0.len()
}

pub fn size(&self) -> usize {
self.0.len() + 1
}

// true is label representes a punycode
#[inline]
fn is_puny(&self) -> bool {
Expand Down Expand Up @@ -309,24 +317,34 @@ impl<'a> TryFrom<&'a str> for DomainName {
return Err(Error::Dns(Dns::EmptyDomainName));
}

// root domain is a special case
let label_list = if domain == "." {
Vec::new()
} else {
// root domain
if domain == "." {
return Ok(DomainName::default());
}

// domain too long
if domain.len() > 255 {
return Err(Error::Dns(Dns::DomainNameTooLong));
}

// test IDNA: if so, convert to puny
let dom = if domain.is_ascii() {
domain
.split('.')
.filter(|x| !x.is_empty()) // filter to exclude any potential ending root
.map(|x| Label(x.as_bytes().to_vec()))
.collect()
} else {
&idna::domain_to_ascii(domain).map_err(Error::IDNA)?
};

// root domain is a special case
let label_list = dom
.split('.')
.filter(|x| !x.is_empty()) // filter to exclude any potential ending root
.map(|x| Label(x.as_bytes().to_vec()))
.collect();

// create the domain name struct
let dn = DomainName { labels: label_list };

// test for correctness
if dn.len() > 255 {
return Err(Error::Dns(Dns::DomainNameTooLong));
}
if dn.labels.iter().any(|x| x.len() > 63) {
return Err(Error::Dns(Dns::DomainLabelTooLong));
}
Expand Down
12 changes: 6 additions & 6 deletions src/dns/rfc/rrlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ impl fmt::Display for RRList {
}

impl Show for RRList {
fn show(&self, display_options: &DisplayOptions, max_length: Option<usize>) {
// let max_length = if display_options.align_names {
// self.max_length()
// } else {
// None
// };
fn show(&self, display_options: &DisplayOptions, _: Option<usize>) {
let max_length = if display_options.align_names {
self.max_length()
} else {
None
};

for rr in &self.0 {
// don't display OPT if not requested
Expand Down

0 comments on commit aad8228

Please sign in to comment.