Skip to content

Commit

Permalink
Fix generation of domain name for IPv6 reverse lookup. (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
partim authored May 2, 2022
1 parent 041ef70 commit 3d200ad
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/resolv/lookup/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ fn dname_from_addr(addr: IpAddr) -> Dname<Octets128> {
IpAddr::V6(addr) => {
let mut res = DnameBuilder::<Octets128>::new();
for &item in addr.octets().iter().rev() {
res.append_label(&[hexdigit(item >> 4)]).unwrap();
res.append_label(&[hexdigit(item)]).unwrap();
res.append_label(&[hexdigit(item >> 4)]).unwrap();
}
res.append_label(b"ip6").unwrap();
res.append_label(b"arpa").unwrap();
Expand All @@ -118,7 +118,7 @@ fn dname_from_addr(addr: IpAddr) -> Dname<Octets128> {
}

fn hexdigit(nibble: u8) -> u8 {
match nibble % 0x0F {
match nibble & 0x0F {
0 => b'0',
1 => b'1',
2 => b'2',
Expand All @@ -128,6 +128,7 @@ fn hexdigit(nibble: u8) -> u8 {
6 => b'6',
7 => b'7',
8 => b'8',
9 => b'9',
10 => b'A',
11 => b'B',
12 => b'C',
Expand All @@ -137,3 +138,31 @@ fn hexdigit(nibble: u8) -> u8 {
_ => unreachable!(),
}
}

//============ Tests =========================================================

#[cfg(test)]
mod test {
use super::*;
use core::str::FromStr;

#[test]
fn test_dname_from_addr() {
assert_eq!(
dname_from_addr([192, 0, 2, 12].into()),
Dname::<Octets128>::from_str("12.2.0.192.in-addr.arpa").unwrap()
);
assert_eq!(
dname_from_addr(
[0x2001, 0xdb8, 0x1234, 0x0, 0x5678, 0x1, 0x9abc, 0xdef]
.into()
),
Dname::<Octets128>::from_str(
"f.e.d.0.c.b.a.9.1.0.0.0.8.7.6.5.\
0.0.0.0.4.3.2.1.8.b.d.0.1.0.0.2.\
ip6.arpa"
)
.unwrap()
);
}
}

0 comments on commit 3d200ad

Please sign in to comment.