rust-embedded-community / embedded-nal

An Embedded Network Abstraction Layer
Apache License 2.0
177 stars 25 forks source link

`Dns` traits force caching in-flight DNS requests #89

Open ryan-summers opened 1 year ago

ryan-summers commented 1 year ago

Currently, because the Dns resolver functions return an nb::Result, this means that some higher-level has to actually cache the the hostname being looked up during repeated calls to resolve the name. However, this isn't really a great API because it enforces caching and RAM usage if the hostnames are long. Thus, if an implementation wants to support N simultanenous queries of 255 bytes, it must cache at least 255 * N bytes locally.

Ideally, we would refactor the functions to be get_host_by_name() -> Handle, where Handle is some unique ID that can be queried against in the future (and can ideally be really lightweight, like a u32).

So the API may be:

trait DnsLookup {
    type Handle;
    fn start_query(&mut self, hostname: &str, typ: QueryType) -> Result<Handle, Self::Error>;
    fn check_query(&mut self, handle: &Handle) -> Result<IpAddr, Self::Error>;
    fn cancel_query(&mut self, handle: Handle);
}

Also, it seems like get_host_by_ip() is a bit of a different DNS use-case and isn't super well-suited for embedded (I just mark it as unimplemented!() in my case because smoltcp doesn't support it). Perhaps it would be best to move this to a separate trait.

ryan-summers commented 1 year ago

Check out https://github.com/quartiq/smoltcp-nal/pull/46 for an example implementation using smoltcp