1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use embedded_nal::{heapless::String, nb, AddrType, Dns, IpAddr};
use std::error;
use std::fmt::{self, Display, Formatter};
use std::io::{Error, ErrorKind};
use std::net::{SocketAddr, ToSocketAddrs};
#[derive(Debug)]
struct NotFound;
impl Display for NotFound {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Not found")
}
}
impl error::Error for NotFound {}
impl Dns for crate::Stack {
type Error = Error;
fn get_host_by_name(
&mut self,
hostname: &str,
addr_type: AddrType,
) -> Result<IpAddr, nb::Error<Error>> {
let with_fake_port = if hostname.find(':').is_some() {
format!("[{}]:1234", hostname)
} else {
format!("{}:1234", hostname)
};
let accept_v4 = addr_type != AddrType::IPv6;
let accept_v6 = addr_type != AddrType::IPv4;
for addr in with_fake_port.to_socket_addrs()? {
match addr {
SocketAddr::V4(v) if accept_v4 => {
return Ok(v.ip().octets().into());
}
SocketAddr::V6(v) if accept_v6 => {
return Ok(v.ip().octets().into());
}
_ => continue,
}
}
Err(nb::Error::Other(Error::new(ErrorKind::NotFound, NotFound)))
}
fn get_host_by_address(&mut self, _addr: IpAddr) -> Result<String<256>, nb::Error<Error>> {
Err(nb::Error::Other(Error::new(ErrorKind::NotFound, NotFound)))
}
}