use embedded_nal::nb::block;
use embedded_nal::{Dns, UdpClientStack};
#[cfg(not(unix))]
fn run<S, E>(stack: &mut S) -> Result<(), E>
where
E: core::fmt::Debug, S: UdpClientStack<Error = E> + Dns<Error = E>,
{
let target = core::net::SocketAddr::new(
block!(stack.get_host_by_name("localhost", embedded_nal::AddrType::IPv6))?,
5683,
);
let mut sock = stack.socket()?;
stack.connect(&mut sock, target)?;
block!(stack.send(&mut sock, b"\x50\x01\0\0\xbb.well-known\x04core"))?;
let mut respbuf = [0; 1500];
let (resplen, _) = block!(stack.receive(&mut sock, &mut respbuf))?;
let response = &respbuf[..resplen];
println!("Response: {}", String::from_utf8_lossy(response));
Ok(())
}
#[cfg(unix)]
fn run_with_unix(stack: &mut std_embedded_nal::Stack) -> Result<(), std::io::Error> {
let target = core::net::SocketAddr::new(
block!(stack.get_host_by_name("localhost", embedded_nal::AddrType::IPv6))?,
5683,
);
let mut sock = stack.socket()?;
stack.connect(&mut sock, target)?;
let fd = sock
.as_raw_fd()
.expect("Connected socket should already have an FD");
let mut poll = mio::Poll::new()?;
poll.registry().register(
&mut mio::unix::SourceFd(&fd),
mio::Token(0),
mio::Interest::READABLE,
)?;
let mut events = mio::Events::with_capacity(1);
block!(stack.send(&mut sock, b"\x50\x01\0\0\xbb.well-known\x04core"))?;
poll.poll(&mut events, None)?;
let mut respbuf = [0; 1500];
let (resplen, _) = stack
.receive(&mut sock, &mut respbuf)
.map_err(|e| match e {
embedded_nal::nb::Error::Other(o) => o,
embedded_nal::nb::Error::WouldBlock => unreachable!("Polling said this could be read."),
})?;
let response = &respbuf[..resplen];
println!("Response: {}", String::from_utf8_lossy(response));
Ok(())
}
fn main() {
let mut stack = std_embedded_nal::Stack::default();
#[cfg(unix)]
run_with_unix(&mut stack).expect("Error running the main program");
#[cfg(not(unix))]
run(&mut stack).expect("Error running the main program");
}