embedded_nal_async/
dns.rs

1use core::net::IpAddr;
2use embedded_nal::AddrType;
3
4/// This trait is an extension trait for [`TcpStack`] and [`UdpStack`] for dns
5/// resolutions. It does not handle every DNS record type, but is meant as an
6/// embedded alternative to [`ToSocketAddrs`], and is as such meant to resolve
7/// an ip address from a hostname, or a hostname from an ip address. This means
8/// that it only deals in host address records `A` (IPv4) and `AAAA` (IPv6).
9///
10/// [`TcpStack`]: crate::trait@TcpStack
11/// [`UdpStack`]: crate::trait@UdpStack
12/// [`ToSocketAddrs`]:
13/// https://doc.rust-lang.org/std/net/trait.ToSocketAddrs.html
14pub trait Dns {
15	/// The type returned when we have an error
16	type Error: core::fmt::Debug;
17
18	/// Resolve the first ip address of a host, given its hostname and a desired
19	/// address record type to look for
20	async fn get_host_by_name(
21		&self,
22		host: &str,
23		addr_type: AddrType,
24	) -> Result<IpAddr, Self::Error>;
25
26	/// Resolve the hostname of a host, given its ip address.
27	///
28	/// The hostname is stored at the beginning of `result`, the length is returned.
29	///
30	/// If the buffer is too small to hold the domain name, an error should be returned.
31	///
32	/// **Note**: A fully qualified domain name (FQDN), has a maximum length of
33	/// 255 bytes according to [`rfc1035`]. Therefore, you can pass a 255-byte long
34	/// buffer to guarantee it'll always be large enough.
35	///
36	/// [`rfc1035`]: https://tools.ietf.org/html/rfc1035
37	async fn get_host_by_address(
38		&self,
39		addr: IpAddr,
40		result: &mut [u8],
41	) -> Result<usize, Self::Error>;
42}
43
44impl<T: Dns> Dns for &T {
45	type Error = T::Error;
46
47	async fn get_host_by_name(
48		&self,
49		host: &str,
50		addr_type: AddrType,
51	) -> Result<IpAddr, Self::Error> {
52		T::get_host_by_name(self, host, addr_type).await
53	}
54
55	async fn get_host_by_address(
56		&self,
57		addr: IpAddr,
58		result: &mut [u8],
59	) -> Result<usize, Self::Error> {
60		T::get_host_by_address(self, addr, result).await
61	}
62}