drogue_network/dns.rs
1use heapless::{consts, String};
2
3use crate::addr::HostAddr;
4use no_std_net::IpAddr;
5
6use core::fmt::Debug;
7
8/// DNS errors
9#[derive(Debug)]
10pub enum DnsError {
11 UnsupportedAddressType,
12 NoSuchHost,
13}
14
15/// This is the host address type to be returned by `gethostbyname`.
16///
17/// An IPv4 address type always looks for `A` records, while IPv6 address type
18/// will look for `AAAA` records
19#[derive(Clone, Debug, PartialEq)]
20pub enum AddrType {
21 /// Result is `A` record
22 IPv4,
23 /// Result is `AAAA` record
24 IPv6,
25 /// Result is either a `A` record, or a `AAAA` record
26 Either,
27}
28
29/// This trait is an extension trait for [`TcpStack`] and [`UdpStack`] for dns
30/// resolutions. It does not handle every DNS record type, but is meant as an
31/// embedded alternative to [`ToSocketAddrs`], and is as such meant to resolve
32/// an ip address from a hostname, or a hostname from an ip address. This means
33/// that it only deals in host address records `A` (IPv4) and `AAAA` (IPv6).
34///
35/// [`TcpStack`]: trait.TcpStack.html
36/// [`UdpStack`]: trait.UdpStack.html
37/// [`ToSocketAddrs`]:
38/// https://doc.rust-lang.org/std/net/trait.ToSocketAddrs.html
39pub trait Dns {
40 /// The type returned when we have an error
41 type Error: Into<DnsError> + Debug;
42
43 /// Resolve the first ip address of a host, given its hostname and a desired
44 /// address record type to look for
45 fn gethostbyname(&self, hostname: &str, addr_type: AddrType) -> Result<HostAddr, Self::Error>;
46
47 /// Resolve the hostname of a host, given its ip address
48 ///
49 /// **Note**: A fully qualified domain name (FQDN), has a maximum length of
50 /// 255 bytes [`rfc1035`]
51 ///
52 /// [`rfc1035`]: https://tools.ietf.org/html/rfc1035
53 fn gethostbyaddr(&self, addr: IpAddr) -> Result<String<consts::U256>, Self::Error>;
54}