mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
services: dns: run rustfmt on project
Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
dd9d56507c
commit
d87a946bd9
2 changed files with 122 additions and 99 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
#[allow(dead_code)]
|
#[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 net::NetIpAddr;
|
||||||
use rkyv::{Archive, Deserialize, Serialize};
|
use rkyv::{Archive, Deserialize, Serialize};
|
||||||
|
|
||||||
|
|
@ -35,9 +35,9 @@ pub enum DnsResponseCode {
|
||||||
NotImplemented = 4,
|
NotImplemented = 4,
|
||||||
Refused = 5,
|
Refused = 5,
|
||||||
|
|
||||||
UnknownError,
|
UnknownError = 6,
|
||||||
NetworkError,
|
NetworkError = 7,
|
||||||
NoServerSpecified,
|
NoServerSpecified = 8,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)]
|
#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)]
|
||||||
|
|
|
||||||
|
|
@ -4,16 +4,16 @@
|
||||||
mod api;
|
mod api;
|
||||||
use api::*;
|
use api::*;
|
||||||
|
|
||||||
use net::{NetIpAddr, Duration};
|
use net::{Duration, NetIpAddr};
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
use xous::msg_scalar_unpack;
|
use xous::msg_scalar_unpack;
|
||||||
|
|
||||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::ErrorKind;
|
|
||||||
use xous_ipc::{String, Buffer};
|
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
use std::io::ErrorKind;
|
||||||
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
use xous_ipc::{Buffer, String};
|
||||||
|
|
||||||
// KISS DNS
|
// KISS DNS
|
||||||
|
|
||||||
|
|
@ -132,16 +132,17 @@ impl Message {
|
||||||
index = self.fast_foward_name(index)?;
|
index = self.fast_foward_name(index)?;
|
||||||
log::trace!("fast forward through qname to {}", index);
|
log::trace!("fast forward through qname to {}", index);
|
||||||
// index is now at qtype
|
// index is now at qtype
|
||||||
let qtype = u16::from_be_bytes(self.datagram[index..index+2].try_into().unwrap());
|
let qtype = u16::from_be_bytes(self.datagram[index..index + 2].try_into().unwrap());
|
||||||
if qtype != 1 && qtype != 28 { // A = 1, AAAA = 28
|
// A = 1, AAAA = 28
|
||||||
|
if qtype != 1 && qtype != 28 {
|
||||||
log::error!("Problem parsing qname, qtype is not 1 or 28: {}", qtype);
|
log::error!("Problem parsing qname, qtype is not 1 or 28: {}", qtype);
|
||||||
return Err(FormatError)
|
return Err(FormatError);
|
||||||
}
|
}
|
||||||
index += 2;
|
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 {
|
if qclass != 1 {
|
||||||
log::error!("Problem parsing qname, qclass is not 1: {}", qclass);
|
log::error!("Problem parsing qname, qclass is not 1: {}", qclass);
|
||||||
return Err(FormatError)
|
return Err(FormatError);
|
||||||
}
|
}
|
||||||
index += 2;
|
index += 2;
|
||||||
}
|
}
|
||||||
|
|
@ -153,8 +154,10 @@ impl Message {
|
||||||
// pointer
|
// pointer
|
||||||
index += 1;
|
index += 1;
|
||||||
if self.datagram[index] != 0xc {
|
if self.datagram[index] != 0xc {
|
||||||
log::error!("Found aname pointer, but value does not conform to length of aname header");
|
log::error!(
|
||||||
return Err(FormatError)
|
"Found aname pointer, but value does not conform to length of aname header"
|
||||||
|
);
|
||||||
|
return Err(FormatError);
|
||||||
}
|
}
|
||||||
index += 1;
|
index += 1;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -163,48 +166,55 @@ impl Message {
|
||||||
log::trace!("fast forward aname to {}", index);
|
log::trace!("fast forward aname to {}", index);
|
||||||
}
|
}
|
||||||
// index is now at type
|
// index is now at type
|
||||||
let atype = u16::from_be_bytes(self.datagram[index..index+2].try_into().unwrap());
|
let atype = u16::from_be_bytes(self.datagram[index..index + 2].try_into().unwrap());
|
||||||
if atype != 1 && atype != 28 { // A = 1, AAAA = 28
|
// A = 1, AAAA = 28
|
||||||
|
if atype != 1 && atype != 28 {
|
||||||
log::error!("Problem parsing aname, type is not 1 or 28: {}", atype);
|
log::error!("Problem parsing aname, type is not 1 or 28: {}", atype);
|
||||||
return Err(FormatError)
|
return Err(FormatError);
|
||||||
}
|
}
|
||||||
index += 2;
|
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 {
|
if aclass != 1 {
|
||||||
log::error!("Problem parsing aname, aclass is not 1: {}", aclass);
|
log::error!("Problem parsing aname, aclass is not 1: {}", aclass);
|
||||||
return Err(FormatError)
|
return Err(FormatError);
|
||||||
}
|
}
|
||||||
index += 2;
|
index += 2;
|
||||||
// this is our TTL
|
// 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);
|
log::trace!("got ttl: {}", ttl);
|
||||||
index += 4;
|
index += 4;
|
||||||
// this is the payload length
|
// 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;
|
index += 2;
|
||||||
match addr_len {
|
match addr_len {
|
||||||
4 => { // ipv4
|
// ipv4
|
||||||
|
4 => {
|
||||||
if atype != 1 {
|
if atype != 1 {
|
||||||
log::error!("Got a 4-byte address, but ATYPE != A (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]
|
// 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];
|
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;
|
*dst = src;
|
||||||
}
|
}
|
||||||
let addr = IpAddr::V4(Ipv4Addr::from(rdata));
|
let addr = IpAddr::V4(Ipv4Addr::from(rdata));
|
||||||
index += 4;
|
index += 4;
|
||||||
map.insert(addr, ttl);
|
map.insert(addr, ttl);
|
||||||
}
|
}
|
||||||
16 => { // ipv6
|
// ipv6
|
||||||
|
16 => {
|
||||||
if atype != 28 {
|
if atype != 28 {
|
||||||
log::error!("Got a 16-byte address, but ATYPE != AAAA (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]
|
// 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];
|
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;
|
*dst = src;
|
||||||
}
|
}
|
||||||
let addr = IpAddr::V6(Ipv6Addr::from(rdata));
|
let addr = IpAddr::V6(Ipv6Addr::from(rdata));
|
||||||
|
|
@ -213,7 +223,7 @@ impl Message {
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
log::error!("Length field does not match a known record type");
|
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
|
example response for: betrusted.io->185.199.111.153
|
||||||
Header:
|
Header:
|
||||||
61, ca, id
|
61, ca, id
|
||||||
81, 80, header
|
81, 80, header
|
||||||
0, 1, qdcount
|
0, 1, qdcount
|
||||||
0, 4, ancount
|
0, 4, ancount
|
||||||
0, 0, nscount
|
0, 0, nscount
|
||||||
0, 0, arcount
|
0, 0, arcount
|
||||||
qname:
|
qname:
|
||||||
9, length 9
|
9, length 9
|
||||||
62, 65, 74, 72, 75, 73, 74, 65, 64, "betrusted"
|
62, 65, 74, 72, 75, 73, 74, 65, 64, "betrusted"
|
||||||
2, length 2
|
2, length 2
|
||||||
69, 6f, "io"
|
69, 6f, "io"
|
||||||
0, end of name
|
0, end of name
|
||||||
qtype:
|
qtype:
|
||||||
0, 1, type A
|
0, 1, type A
|
||||||
qclass:
|
qclass:
|
||||||
0, 1, type IN
|
0, 1, type IN
|
||||||
aname0:
|
aname0:
|
||||||
c0, name is a pointer (any value > 192 is a pointer)
|
c0, name is a pointer (any value > 192 is a pointer)
|
||||||
c, offset of 12 from start of aname0
|
c, offset of 12 from start of aname0
|
||||||
0, 1, type A
|
0, 1, type A
|
||||||
0, 1, class IN
|
0, 1, class IN
|
||||||
0, 0, e, 10, 0xe10 = 3600 seconds TTL
|
0, 0, e, 10, 0xe10 = 3600 seconds TTL
|
||||||
0, 4, 4 bytes address
|
0, 4, 4 bytes address
|
||||||
b9, c7, 6c, 99, address
|
b9, c7, 6c, 99, address
|
||||||
aname1:
|
aname1:
|
||||||
c0, name is a pointer
|
c0, name is a pointer
|
||||||
c,
|
c,
|
||||||
0, 1, type A
|
0, 1, type A
|
||||||
0, 1, class IN
|
0, 1, class IN
|
||||||
0, 0, e, 10, TTL
|
0, 0, e, 10, TTL
|
||||||
0, 4, 4 byte address
|
0, 4, 4 byte address
|
||||||
b9, c7, 6d, 99, address
|
b9, c7, 6d, 99, address
|
||||||
aname2:
|
aname2:
|
||||||
c0,
|
c0,
|
||||||
c,
|
c,
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 0, e, 10,
|
0, 0, e, 10,
|
||||||
0, 4,
|
0, 4,
|
||||||
b9, c7, 6e, 99,
|
b9, c7, 6e, 99,
|
||||||
aname3:
|
aname3:
|
||||||
c0,
|
c0,
|
||||||
c,
|
c,
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 0, e, 10,
|
0, 0, e, 10,
|
||||||
0, 4,
|
0, 4,
|
||||||
b9, c7, 6f, 99
|
b9, c7, 6f, 99
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
pub fn is_query(&self) -> bool {
|
pub fn is_query(&self) -> bool {
|
||||||
|
|
@ -307,16 +317,18 @@ impl Resolver {
|
||||||
let local_port = (49152 + trng.get_u32().unwrap() % 16384) as u16;
|
let local_port = (49152 + trng.get_u32().unwrap() % 16384) as u16;
|
||||||
let mut socket = net::UdpSocket::bind_xous(
|
let mut socket = net::UdpSocket::bind_xous(
|
||||||
format!("127.0.0.1:{}", local_port),
|
format!("127.0.0.1:{}", local_port),
|
||||||
Some(DNS_PKT_MAX_LEN as u16)
|
Some(DNS_PKT_MAX_LEN as u16),
|
||||||
).expect("couldn't create socket for DNS resolver");
|
)
|
||||||
|
.expect("couldn't create socket for DNS resolver");
|
||||||
let timeout = Duration::from_millis(10_000); // 10 seconds for DNS to resolve by default
|
let timeout = Duration::from_millis(10_000); // 10 seconds for DNS to resolve by default
|
||||||
socket.set_read_timeout(Some(timeout)).unwrap();
|
socket.set_read_timeout(Some(timeout)).unwrap();
|
||||||
socket.set_nonblocking(false).unwrap(); // we want this to block.
|
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??
|
// 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.
|
// blocking is probably what we actually want this time.
|
||||||
|
|
||||||
Resolver {
|
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,
|
socket,
|
||||||
buf: [0; DNS_PKT_MAX_LEN],
|
buf: [0; DNS_PKT_MAX_LEN],
|
||||||
trng,
|
trng,
|
||||||
|
|
@ -336,12 +348,14 @@ impl Resolver {
|
||||||
self.freeze = freeze;
|
self.freeze = freeze;
|
||||||
self.mgr.set_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
|
/// this allows us to re-use the TRNG object
|
||||||
pub fn trng_u32(&self) -> u32 {
|
pub fn trng_u32(&self) -> u32 {
|
||||||
self.trng.get_u32().unwrap()
|
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() {
|
if let Some(dns_address) = self.mgr.get_random() {
|
||||||
let dns_port = 53;
|
let dns_port = 53;
|
||||||
let server = SocketAddr::new(dns_address, dns_port);
|
let server = SocketAddr::new(dns_address, dns_port);
|
||||||
|
|
@ -395,7 +409,9 @@ fn xmain() -> ! {
|
||||||
log::info!("my PID is {}", xous::process::id());
|
log::info!("my PID is {}", xous::process::id());
|
||||||
|
|
||||||
let xns = xous_names::XousNames::new().unwrap();
|
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);
|
log::trace!("registered with NS -- {:?}", dns_sid);
|
||||||
|
|
||||||
// this will magically populate a list of DNS servers when they become available
|
// 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:
|
// 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)));
|
// 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
|
// build a thread that pings the UpdateTtl function once every few minutes to expire the DNS cache
|
||||||
thread::spawn({
|
thread::spawn({
|
||||||
|
|
@ -413,10 +429,17 @@ fn xmain() -> ! {
|
||||||
let tt = ticktimer_server::Ticktimer::new().unwrap();
|
let tt = ticktimer_server::Ticktimer::new().unwrap();
|
||||||
loop {
|
loop {
|
||||||
tt.sleep_ms(TTL_INTERVAL_SECS * 1000).unwrap();
|
tt.sleep_ms(TTL_INTERVAL_SECS * 1000).unwrap();
|
||||||
xous::send_message(local_cid,
|
xous::send_message(
|
||||||
xous::Message::new_scalar(Opcode::UpdateTtl.to_usize().unwrap(),
|
local_cid,
|
||||||
TTL_INTERVAL_SECS, 0, 0, 0)
|
xous::Message::new_scalar(
|
||||||
).expect("couldn't increment DNS cache");
|
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();
|
buf.replace(response).unwrap();
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::debug!("DNS query failed: {}->{:?}", name, e);
|
log::debug!("DNS query failed: {}->{:?}", name, e);
|
||||||
let response = DnsResponse {
|
let response = DnsResponse {
|
||||||
|
|
@ -481,10 +504,10 @@ fn xmain() -> ! {
|
||||||
code: e,
|
code: e,
|
||||||
};
|
};
|
||||||
buf.replace(response).unwrap();
|
buf.replace(response).unwrap();
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Some(Opcode::UpdateTtl) => msg_scalar_unpack!(msg, incr_secs, _, _, _, {
|
Some(Opcode::UpdateTtl) => msg_scalar_unpack!(msg, incr_secs, _, _, _, {
|
||||||
let increment = if incr_secs < u32::MAX as usize {
|
let increment = if incr_secs < u32::MAX as usize {
|
||||||
incr_secs as u32
|
incr_secs as u32
|
||||||
|
|
@ -526,16 +549,16 @@ fn xmain() -> ! {
|
||||||
}),
|
}),
|
||||||
Some(Opcode::Flush) => {
|
Some(Opcode::Flush) => {
|
||||||
dns_cache.clear();
|
dns_cache.clear();
|
||||||
},
|
}
|
||||||
Some(Opcode::FreezeConfig) => {
|
Some(Opcode::FreezeConfig) => {
|
||||||
resolver.set_freeze_config(true);
|
resolver.set_freeze_config(true);
|
||||||
},
|
}
|
||||||
Some(Opcode::ThawConfig) => {
|
Some(Opcode::ThawConfig) => {
|
||||||
resolver.set_freeze_config(false);
|
resolver.set_freeze_config(false);
|
||||||
}
|
}
|
||||||
Some(Opcode::Quit) => {
|
Some(Opcode::Quit) => {
|
||||||
log::warn!("got quit!");
|
log::warn!("got quit!");
|
||||||
break
|
break;
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
log::error!("couldn't convert opcode: {:?}", msg);
|
log::error!("couldn't convert opcode: {:?}", msg);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue