mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
services: dns: add RawLookup opcode
This lookup will be used by libstd in order to perform DNS queries. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
97ac5e1cd6
commit
331a0a120d
2 changed files with 175 additions and 30 deletions
|
|
@ -10,22 +10,56 @@ pub(crate) const DNS_PKT_MAX_LEN: usize = 512;
|
||||||
|
|
||||||
/// These opcodes can be called by anyone at any time
|
/// These opcodes can be called by anyone at any time
|
||||||
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
|
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
|
||||||
|
#[repr(C)]
|
||||||
pub(crate) enum Opcode {
|
pub(crate) enum Opcode {
|
||||||
Lookup,
|
Lookup = 0,
|
||||||
Flush,
|
Flush = 1,
|
||||||
|
|
||||||
/// used internally to update the TTL field, and eventually expire the cache (unless cache is frozen)
|
/// used internally to update the TTL field, and eventually expire the cache (unless cache is frozen)
|
||||||
UpdateTtl,
|
UpdateTtl = 2,
|
||||||
|
|
||||||
/// issuing this opcode causes all future attempts to change the DNS server configs to be ignored. This also freezes the cache.
|
/// issuing this opcode causes all future attempts to change the DNS server configs to be ignored. This also freezes the cache.
|
||||||
FreezeConfig,
|
FreezeConfig = 3,
|
||||||
/// this allows automatic updates to the DNS server configs based on DHCP. This is the default state.
|
|
||||||
ThawConfig,
|
|
||||||
|
|
||||||
Quit,
|
/// this allows automatic updates to the DNS server configs based on DHCP. This is the default state.
|
||||||
|
ThawConfig = 4,
|
||||||
|
|
||||||
|
Quit = 5,
|
||||||
|
|
||||||
|
/// Perform a DNS lookup and return the results in a raw format.
|
||||||
|
///
|
||||||
|
/// The query should be a `MutableBorrow` and should be a `&str` with the
|
||||||
|
/// `valid` parameter set to the length of the query.
|
||||||
|
///
|
||||||
|
/// The result will be a `&[u8]` with the first field being `0` and the second
|
||||||
|
/// field indicating the number of results.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// If there is an error, then the first field is `1` and the second field is
|
||||||
|
/// an error code corresponding to `DnsResponseCode`
|
||||||
|
///
|
||||||
|
/// # Success
|
||||||
|
///
|
||||||
|
/// Upon successful return, a series of entries will begin at offset 2. Each
|
||||||
|
/// entry is a tag, followed by a number of octets. The tag indicates
|
||||||
|
/// how many octets follow:
|
||||||
|
///
|
||||||
|
/// * 4: Ipv4 Address -- 4 octets follow, for a total of 5 bytes
|
||||||
|
/// * 6: Ipv6 Address -- 16 octets follow, for a total of 17 bytes
|
||||||
|
RawLookup = 6,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive, Archive, Serialize, Deserialize, Copy, Clone)]
|
#[derive(
|
||||||
|
Debug,
|
||||||
|
num_derive::FromPrimitive,
|
||||||
|
num_derive::ToPrimitive,
|
||||||
|
Archive,
|
||||||
|
Serialize,
|
||||||
|
Deserialize,
|
||||||
|
Copy,
|
||||||
|
Clone,
|
||||||
|
)]
|
||||||
#[repr(u16)]
|
#[repr(u16)]
|
||||||
pub enum DnsResponseCode {
|
pub enum DnsResponseCode {
|
||||||
NoError = 0,
|
NoError = 0,
|
||||||
|
|
|
||||||
|
|
@ -365,43 +365,120 @@ impl Resolver {
|
||||||
let qclass = QueryClass::IN;
|
let qclass = QueryClass::IN;
|
||||||
let query = Message::query(qname, qtype, qclass, self.trng.get_u32().unwrap() as u16);
|
let query = Message::query(qname, qtype, qclass, self.trng.get_u32().unwrap() as u16);
|
||||||
|
|
||||||
self.socket.send_to(&query.datagram, &server)
|
self.socket
|
||||||
.map_err(|_| DnsResponseCode::NetworkError)?;
|
.send_to(&query.datagram, &server)
|
||||||
|
.map_err(|_| DnsResponseCode::NetworkError)?;
|
||||||
|
|
||||||
match self.socket.recv(&mut self.buf) {
|
match self.socket.recv(&mut self.buf) {
|
||||||
Ok(len) => {
|
Ok(len) => {
|
||||||
let message = Message::from(&self.buf[..len]);
|
let message = Message::from(&self.buf[..len]);
|
||||||
if message.id() == query.id() && message.is_response() {
|
if message.id() == query.id() && message.is_response() {
|
||||||
return match message.rcode() {
|
return match message.rcode() {
|
||||||
DnsResponseCode::NoError => {
|
DnsResponseCode::NoError => message.parse_response(),
|
||||||
message.parse_response()
|
rcode => Err(rcode),
|
||||||
}
|
};
|
||||||
rcode => {
|
|
||||||
Err(rcode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Err(DnsResponseCode::NetworkError)
|
Err(DnsResponseCode::NetworkError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => match e.kind() {
|
||||||
match e.kind() {
|
ErrorKind::WouldBlock => Err(DnsResponseCode::NetworkError),
|
||||||
ErrorKind::WouldBlock => {
|
_ => Err(DnsResponseCode::UnknownError),
|
||||||
Err(DnsResponseCode::NetworkError)
|
},
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
Err(DnsResponseCode::UnknownError)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Err(DnsResponseCode::NoServerSpecified)
|
Err(DnsResponseCode::NoServerSpecified)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Debug)]
|
||||||
|
#[repr(C)]
|
||||||
|
enum NameConversionError {
|
||||||
|
/// The length of the memory buffer was invalid
|
||||||
|
InvalidMemoryBuffer = 1,
|
||||||
|
|
||||||
|
/// The specified nameserver string was not UTF-8
|
||||||
|
InvalidString = 3,
|
||||||
|
|
||||||
|
/// The message was not a mutable memory message
|
||||||
|
InvalidMessageType = 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn name_from_msg(env: &xous::MessageEnvelope) -> Result<&str, NameConversionError> {
|
||||||
|
let msg = env
|
||||||
|
.body
|
||||||
|
.memory_message()
|
||||||
|
.ok_or(NameConversionError::InvalidMessageType)?;
|
||||||
|
let valid_bytes = msg.valid.map(|v| v.get()).unwrap_or_else(|| msg.buf.len());
|
||||||
|
if valid_bytes > DNS_NAME_LENGTH_LIMIT || valid_bytes < 1 {
|
||||||
|
log::error!("valid bytes exceeded DNS name limit");
|
||||||
|
return Err(NameConversionError::InvalidMemoryBuffer);
|
||||||
|
}
|
||||||
|
// Safe because we've already validated that it's a valid range
|
||||||
|
let str_slice = unsafe { core::slice::from_raw_parts(msg.buf.as_ptr(), valid_bytes) };
|
||||||
|
let name_string =
|
||||||
|
core::str::from_utf8(str_slice).map_err(|_| NameConversionError::InvalidString)?;
|
||||||
|
|
||||||
|
Ok(name_string)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fill_response(mut env: xous::MessageEnvelope, entries: &HashMap<IpAddr, u32>) -> Option<()> {
|
||||||
|
let mem = env.body.memory_message_mut()?;
|
||||||
|
|
||||||
|
let s: &mut [u8] = mem.buf.as_slice_mut();
|
||||||
|
let mut i = s.iter_mut();
|
||||||
|
|
||||||
|
// First tag = 1 for "Error" -- we'll fill this in at the end when it's successful
|
||||||
|
*i.next()? = 1;
|
||||||
|
|
||||||
|
// Limit the number of entries to 128, which is a nice number. Given that an IPv6
|
||||||
|
// address is 17 bytes, that means that ~240 IPv6 addresses will fit in a 4 kB page.
|
||||||
|
// 128 is just a conservative value rounded down.
|
||||||
|
let mut entry_count = entries.len();
|
||||||
|
if entry_count > 128 {
|
||||||
|
entry_count = 128;
|
||||||
|
}
|
||||||
|
*i.next()? = entry_count.try_into().ok()?;
|
||||||
|
|
||||||
|
// Start filling in the addreses
|
||||||
|
for addr in entries.keys() {
|
||||||
|
match addr {
|
||||||
|
&IpAddr::V4(a) => {
|
||||||
|
// IPv4
|
||||||
|
*i.next()? = 4;
|
||||||
|
for entry in a.octets() {
|
||||||
|
*i.next()? = entry;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
&IpAddr::V6(a) => {
|
||||||
|
// IPv6
|
||||||
|
for entry in a.octets() {
|
||||||
|
*i.next()? = entry;
|
||||||
|
}
|
||||||
|
*i.next()? = 6;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the entry to a "Success" message
|
||||||
|
drop(i);
|
||||||
|
s[0] = 0;
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fill_error(mut env: xous::MessageEnvelope, code: DnsResponseCode) -> Option<()> {
|
||||||
|
let mem = env.body.memory_message_mut()?;
|
||||||
|
|
||||||
|
let s: &mut [u8] = mem.buf.as_slice_mut();
|
||||||
|
let mut i = s.iter_mut();
|
||||||
|
|
||||||
|
*i.next()? = 1;
|
||||||
|
*i.next()? = code as u8;
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
#[xous::xous_main]
|
#[xous::xous_main]
|
||||||
fn xmain() -> ! {
|
fn xmain() -> ! {
|
||||||
log_server::init_wait().unwrap();
|
log_server::init_wait().unwrap();
|
||||||
|
|
@ -448,9 +525,43 @@ fn xmain() -> ! {
|
||||||
loop {
|
loop {
|
||||||
let mut msg = xous::receive_message(dns_sid).unwrap();
|
let mut msg = xous::receive_message(dns_sid).unwrap();
|
||||||
match FromPrimitive::from_usize(msg.body.id()) {
|
match FromPrimitive::from_usize(msg.body.id()) {
|
||||||
|
Some(Opcode::RawLookup) => {
|
||||||
|
match name_from_msg(&msg).map(|s| s.to_owned()) {
|
||||||
|
Ok(owned_name) => {
|
||||||
|
log::trace!("performing a lookup of {}", owned_name);
|
||||||
|
// Try to get the result out of the DNS cache
|
||||||
|
if let Some(entries) = dns_cache.get(&owned_name) {
|
||||||
|
fill_response(msg, entries);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This entry is not in the cache, so perform a lookup
|
||||||
|
match resolver.resolve(&owned_name) {
|
||||||
|
Ok(cache_entry) => {
|
||||||
|
fill_response(msg, &cache_entry);
|
||||||
|
dns_cache.insert(owned_name, cache_entry);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
fill_error(msg, e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("unable to do name lookup: {:?}", e);
|
||||||
|
fill_error(msg, DnsResponseCode::NameError);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
Some(Opcode::Lookup) => {
|
Some(Opcode::Lookup) => {
|
||||||
let mut buf = unsafe{Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())};
|
let mut buf = unsafe {
|
||||||
let name = buf.to_original::<String::<DNS_NAME_LENGTH_LIMIT>, _>().unwrap();
|
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
|
||||||
|
};
|
||||||
|
let name = buf
|
||||||
|
.to_original::<String<DNS_NAME_LENGTH_LIMIT>, _>()
|
||||||
|
.unwrap();
|
||||||
let name_std = std::string::String::from(name.as_str().unwrap());
|
let name_std = std::string::String::from(name.as_str().unwrap());
|
||||||
if let Some(cache_entry) = dns_cache.get(&name_std) {
|
if let Some(cache_entry) = dns_cache.get(&name_std) {
|
||||||
// pick a random entry
|
// pick a random entry
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue