parse raw IP addresses when passed to the DNS lookup

This commit is contained in:
bunnie 2021-10-28 05:11:22 +08:00
parent 2c7a35a36c
commit 10a333eace

View file

@ -7,6 +7,7 @@ use xous_ipc::{Buffer, String};
use num_traits::ToPrimitive; use num_traits::ToPrimitive;
use net::NetIpAddr; use net::NetIpAddr;
use std::net::IpAddr;
#[derive(Debug)] #[derive(Debug)]
pub struct Dns { pub struct Dns {
@ -21,7 +22,12 @@ impl Dns {
}) })
} }
/// Checks first to see if the name could be just an IPv4 or IPv6 in string form,
/// then tries to pass it to the DNS resolver.
pub fn lookup(&self, name: &str) -> Result<NetIpAddr, DnsResponseCode> { pub fn lookup(&self, name: &str) -> Result<NetIpAddr, DnsResponseCode> {
if let Ok(simple_ip) = name.parse::<IpAddr>() {
Ok(NetIpAddr::from(simple_ip))
} else {
let alloc_name = String::<DNS_NAME_LENGTH_LIMIT>::from_str(name); let alloc_name = String::<DNS_NAME_LENGTH_LIMIT>::from_str(name);
let mut buf = Buffer::into_buf(alloc_name).or(Err(DnsResponseCode::UnknownError))?; let mut buf = Buffer::into_buf(alloc_name).or(Err(DnsResponseCode::UnknownError))?;
buf.lend_mut(self.conn, Opcode::Lookup.to_u32().unwrap()) buf.lend_mut(self.conn, Opcode::Lookup.to_u32().unwrap())
@ -33,6 +39,7 @@ impl Dns {
Err(response.code) Err(response.code)
} }
} }
}
pub fn flush_cache(&self) -> Result<(), xous::Error> { pub fn flush_cache(&self) -> Result<(), xous::Error> {
xous::send_message( xous::send_message(
self.conn, self.conn,