services: dns: run rustfmt on project

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2022-03-17 11:25:23 +08:00
parent dd9d56507c
commit d87a946bd9
2 changed files with 122 additions and 99 deletions

View file

@ -1,5 +1,5 @@
#[allow(dead_code)]
pub(crate) const SERVER_NAME_DNS: &str = "_DNS Resolver Middleware_";
pub(crate) const SERVER_NAME_DNS: &str = "_DNS Resolver Middleware_";
use net::NetIpAddr;
use rkyv::{Archive, Deserialize, Serialize};
@ -35,9 +35,9 @@ pub enum DnsResponseCode {
NotImplemented = 4,
Refused = 5,
UnknownError,
NetworkError,
NoServerSpecified,
UnknownError = 6,
NetworkError = 7,
NoServerSpecified = 8,
}
#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)]

View file

@ -4,16 +4,16 @@
mod api;
use api::*;
use net::{NetIpAddr, Duration};
use net::{Duration, NetIpAddr};
use num_traits::*;
use xous::msg_scalar_unpack;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::collections::HashMap;
use std::io::ErrorKind;
use xous_ipc::{String, Buffer};
use std::convert::TryInto;
use std::io::ErrorKind;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::thread;
use xous_ipc::{Buffer, String};
// KISS DNS
@ -132,16 +132,17 @@ impl Message {
index = self.fast_foward_name(index)?;
log::trace!("fast forward through qname to {}", index);
// index is now at qtype
let qtype = u16::from_be_bytes(self.datagram[index..index+2].try_into().unwrap());
if qtype != 1 && qtype != 28 { // A = 1, AAAA = 28
let qtype = u16::from_be_bytes(self.datagram[index..index + 2].try_into().unwrap());
// A = 1, AAAA = 28
if qtype != 1 && qtype != 28 {
log::error!("Problem parsing qname, qtype is not 1 or 28: {}", qtype);
return Err(FormatError)
return Err(FormatError);
}
index += 2;
let qclass = u16::from_be_bytes(self.datagram[index..index+2].try_into().unwrap());
let qclass = u16::from_be_bytes(self.datagram[index..index + 2].try_into().unwrap());
if qclass != 1 {
log::error!("Problem parsing qname, qclass is not 1: {}", qclass);
return Err(FormatError)
return Err(FormatError);
}
index += 2;
}
@ -153,8 +154,10 @@ impl Message {
// pointer
index += 1;
if self.datagram[index] != 0xc {
log::error!("Found aname pointer, but value does not conform to length of aname header");
return Err(FormatError)
log::error!(
"Found aname pointer, but value does not conform to length of aname header"
);
return Err(FormatError);
}
index += 1;
} else {
@ -163,48 +166,55 @@ impl Message {
log::trace!("fast forward aname to {}", index);
}
// index is now at type
let atype = u16::from_be_bytes(self.datagram[index..index+2].try_into().unwrap());
if atype != 1 && atype != 28 { // A = 1, AAAA = 28
let atype = u16::from_be_bytes(self.datagram[index..index + 2].try_into().unwrap());
// A = 1, AAAA = 28
if atype != 1 && atype != 28 {
log::error!("Problem parsing aname, type is not 1 or 28: {}", atype);
return Err(FormatError)
return Err(FormatError);
}
index += 2;
let aclass = u16::from_be_bytes(self.datagram[index..index+2].try_into().unwrap());
let aclass = u16::from_be_bytes(self.datagram[index..index + 2].try_into().unwrap());
if aclass != 1 {
log::error!("Problem parsing aname, aclass is not 1: {}", aclass);
return Err(FormatError)
return Err(FormatError);
}
index += 2;
// this is our TTL
let ttl = u32::from_be_bytes(self.datagram[index..index+4].try_into().unwrap());
let ttl = u32::from_be_bytes(self.datagram[index..index + 4].try_into().unwrap());
log::trace!("got ttl: {}", ttl);
index += 4;
// this is the payload length
let addr_len = u16::from_be_bytes(self.datagram[index..index+2].try_into().unwrap());
let addr_len = u16::from_be_bytes(self.datagram[index..index + 2].try_into().unwrap());
index += 2;
match addr_len {
4 => { // ipv4
// ipv4
4 => {
if atype != 1 {
log::error!("Got a 4-byte address, but ATYPE != A (1)");
return Err(FormatError)
return Err(FormatError);
}
// this copy happens because I can't figure out how to get Ipv4Addr::from() to realize it's casting from a [u8;4]
let mut rdata: [u8; 4] = [0; 4];
for (&src, dst) in self.datagram[index..index+4].iter().zip(rdata.iter_mut()) {
for (&src, dst) in self.datagram[index..index + 4].iter().zip(rdata.iter_mut())
{
*dst = src;
}
let addr = IpAddr::V4(Ipv4Addr::from(rdata));
index += 4;
map.insert(addr, ttl);
}
16 => { // ipv6
// ipv6
16 => {
if atype != 28 {
log::error!("Got a 16-byte address, but ATYPE != AAAA (28)");
return Err(FormatError)
return Err(FormatError);
}
// this copy happens because I can't figure out how to get Ipv6Addr::from() to realize it's casting from a [u8;4]
let mut rdata: [u8; 16] = [0; 16];
for (&src, dst) in self.datagram[index..index+16].iter().zip(rdata.iter_mut()) {
for (&src, dst) in self.datagram[index..index + 16]
.iter()
.zip(rdata.iter_mut())
{
*dst = src;
}
let addr = IpAddr::V6(Ipv6Addr::from(rdata));
@ -213,7 +223,7 @@ impl Message {
}
_ => {
log::error!("Length field does not match a known record type");
return Err(FormatError)
return Err(FormatError);
}
}
}
@ -222,57 +232,57 @@ impl Message {
}
/*
example response for: betrusted.io->185.199.111.153
Header:
61, ca, id
81, 80, header
0, 1, qdcount
0, 4, ancount
0, 0, nscount
0, 0, arcount
qname:
9, length 9
62, 65, 74, 72, 75, 73, 74, 65, 64, "betrusted"
2, length 2
69, 6f, "io"
0, end of name
qtype:
0, 1, type A
qclass:
0, 1, type IN
aname0:
c0, name is a pointer (any value > 192 is a pointer)
c, offset of 12 from start of aname0
0, 1, type A
0, 1, class IN
0, 0, e, 10, 0xe10 = 3600 seconds TTL
0, 4, 4 bytes address
b9, c7, 6c, 99, address
aname1:
c0, name is a pointer
c,
0, 1, type A
0, 1, class IN
0, 0, e, 10, TTL
0, 4, 4 byte address
b9, c7, 6d, 99, address
aname2:
c0,
c,
0, 1,
0, 1,
0, 0, e, 10,
0, 4,
b9, c7, 6e, 99,
aname3:
c0,
c,
0, 1,
0, 1,
0, 0, e, 10,
0, 4,
b9, c7, 6f, 99
*/
example response for: betrusted.io->185.199.111.153
Header:
61, ca, id
81, 80, header
0, 1, qdcount
0, 4, ancount
0, 0, nscount
0, 0, arcount
qname:
9, length 9
62, 65, 74, 72, 75, 73, 74, 65, 64, "betrusted"
2, length 2
69, 6f, "io"
0, end of name
qtype:
0, 1, type A
qclass:
0, 1, type IN
aname0:
c0, name is a pointer (any value > 192 is a pointer)
c, offset of 12 from start of aname0
0, 1, type A
0, 1, class IN
0, 0, e, 10, 0xe10 = 3600 seconds TTL
0, 4, 4 bytes address
b9, c7, 6c, 99, address
aname1:
c0, name is a pointer
c,
0, 1, type A
0, 1, class IN
0, 0, e, 10, TTL
0, 4, 4 byte address
b9, c7, 6d, 99, address
aname2:
c0,
c,
0, 1,
0, 1,
0, 0, e, 10,
0, 4,
b9, c7, 6e, 99,
aname3:
c0,
c,
0, 1,
0, 1,
0, 0, e, 10,
0, 4,
b9, c7, 6f, 99
*/
/*
pub fn is_query(&self) -> bool {
@ -307,16 +317,18 @@ impl Resolver {
let local_port = (49152 + trng.get_u32().unwrap() % 16384) as u16;
let mut socket = net::UdpSocket::bind_xous(
format!("127.0.0.1:{}", local_port),
Some(DNS_PKT_MAX_LEN as u16)
).expect("couldn't create socket for DNS resolver");
Some(DNS_PKT_MAX_LEN as u16),
)
.expect("couldn't create socket for DNS resolver");
let timeout = Duration::from_millis(10_000); // 10 seconds for DNS to resolve by default
socket.set_read_timeout(Some(timeout)).unwrap();
socket.set_nonblocking(false).unwrap(); // we want this to block.
// we /could/ do a non-blocking DNS resolver, but...what would you do in the meantime??
// blocking is probably what we actually want this time.
// we /could/ do a non-blocking DNS resolver, but...what would you do in the meantime??
// blocking is probably what we actually want this time.
Resolver {
mgr: net::DnsServerManager::register(&xns).expect("Couldn't register the DNS server list auto-manager"),
mgr: net::DnsServerManager::register(&xns)
.expect("Couldn't register the DNS server list auto-manager"),
socket,
buf: [0; DNS_PKT_MAX_LEN],
trng,
@ -336,12 +348,14 @@ impl Resolver {
self.freeze = freeze;
self.mgr.set_freeze(freeze);
}
pub fn get_freeze(&self) -> bool { self.freeze }
pub fn get_freeze(&self) -> bool {
self.freeze
}
/// this allows us to re-use the TRNG object
pub fn trng_u32(&self) -> u32 {
self.trng.get_u32().unwrap()
}
pub fn resolve(&mut self, name: &str) -> Result<HashMap::<IpAddr, u32>, DnsResponseCode> {
pub fn resolve(&mut self, name: &str) -> Result<HashMap<IpAddr, u32>, DnsResponseCode> {
if let Some(dns_address) = self.mgr.get_random() {
let dns_port = 53;
let server = SocketAddr::new(dns_address, dns_port);
@ -395,7 +409,9 @@ fn xmain() -> ! {
log::info!("my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap();
let dns_sid = xns.register_name(api::SERVER_NAME_DNS, None).expect("can't register server");
let dns_sid = xns
.register_name(api::SERVER_NAME_DNS, None)
.expect("can't register server");
log::trace!("registered with NS -- {:?}", dns_sid);
// this will magically populate a list of DNS servers when they become available
@ -403,7 +419,7 @@ fn xmain() -> ! {
// if you wanted to force a server into the initial config, you can do it here, for example:
// resolver.add_server(IpAddr::V4(Ipv4Addr::new(1,1,1,1)));
let mut dns_cache = HashMap::<std::string::String, HashMap::<IpAddr, u32>>::new();
let mut dns_cache = HashMap::<std::string::String, HashMap<IpAddr, u32>>::new();
// build a thread that pings the UpdateTtl function once every few minutes to expire the DNS cache
thread::spawn({
@ -413,10 +429,17 @@ fn xmain() -> ! {
let tt = ticktimer_server::Ticktimer::new().unwrap();
loop {
tt.sleep_ms(TTL_INTERVAL_SECS * 1000).unwrap();
xous::send_message(local_cid,
xous::Message::new_scalar(Opcode::UpdateTtl.to_usize().unwrap(),
TTL_INTERVAL_SECS, 0, 0, 0)
).expect("couldn't increment DNS cache");
xous::send_message(
local_cid,
xous::Message::new_scalar(
Opcode::UpdateTtl.to_usize().unwrap(),
TTL_INTERVAL_SECS,
0,
0,
0,
),
)
.expect("couldn't increment DNS cache");
}
}
});
@ -473,7 +496,7 @@ fn xmain() -> ! {
};
buf.replace(response).unwrap();
}
},
}
Err(e) => {
log::debug!("DNS query failed: {}->{:?}", name, e);
let response = DnsResponse {
@ -481,10 +504,10 @@ fn xmain() -> ! {
code: e,
};
buf.replace(response).unwrap();
},
}
}
}
},
}
Some(Opcode::UpdateTtl) => msg_scalar_unpack!(msg, incr_secs, _, _, _, {
let increment = if incr_secs < u32::MAX as usize {
incr_secs as u32
@ -526,16 +549,16 @@ fn xmain() -> ! {
}),
Some(Opcode::Flush) => {
dns_cache.clear();
},
}
Some(Opcode::FreezeConfig) => {
resolver.set_freeze_config(true);
},
}
Some(Opcode::ThawConfig) => {
resolver.set_freeze_config(false);
}
Some(Opcode::Quit) => {
log::warn!("got quit!");
break
break;
}
None => {
log::error!("couldn't convert opcode: {:?}", msg);