aboutsummaryrefslogtreecommitdiff
path: root/embassy-net/src/dns.rs
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2023-02-10 17:43:23 +0100
committerUlf Lilleengen <[email protected]>2023-02-10 17:46:08 +0100
commitcd440a49d677f7dfc09e405d99b87a49fba9ba31 (patch)
tree78811f31ac70b5823fa88a3776f8aef11307f922 /embassy-net/src/dns.rs
parent614740a1b2ea1cada16e3391cb86d0394f64edfc (diff)
Rewrite to use a single socket
Diffstat (limited to 'embassy-net/src/dns.rs')
-rw-r--r--embassy-net/src/dns.rs120
1 files changed, 47 insertions, 73 deletions
diff --git a/embassy-net/src/dns.rs b/embassy-net/src/dns.rs
index e98247bfd..1815d258f 100644
--- a/embassy-net/src/dns.rs
+++ b/embassy-net/src/dns.rs
@@ -1,19 +1,10 @@
1//! DNS socket with async support. 1//! DNS socket with async support.
2use core::cell::RefCell;
3use core::future::poll_fn;
4use core::mem;
5use core::task::Poll;
6
7use embassy_hal_common::drop::OnDrop;
8use embassy_net_driver::Driver;
9use heapless::Vec; 2use heapless::Vec;
10use managed::ManagedSlice; 3pub use smoltcp::socket::dns::{DnsQuery, Socket, MAX_ADDRESS_COUNT};
11use smoltcp::iface::{Interface, SocketHandle}; 4pub(crate) use smoltcp::socket::dns::{GetQueryResultError, StartQueryError};
12pub use smoltcp::socket::dns::DnsQuery;
13use smoltcp::socket::dns::{self, GetQueryResultError, StartQueryError, MAX_ADDRESS_COUNT};
14pub use smoltcp::wire::{DnsQueryType, IpAddress}; 5pub use smoltcp::wire::{DnsQueryType, IpAddress};
15 6
16use crate::{SocketStack, Stack}; 7use crate::{Driver, Stack};
17 8
18/// Errors returned by DnsSocket. 9/// Errors returned by DnsSocket.
19#[derive(Debug, PartialEq, Eq, Clone, Copy)] 10#[derive(Debug, PartialEq, Eq, Clone, Copy)]
@@ -46,81 +37,64 @@ impl From<StartQueryError> for Error {
46} 37}
47 38
48/// Async socket for making DNS queries. 39/// Async socket for making DNS queries.
49pub struct DnsSocket<'a> { 40pub struct DnsSocket<'a, D>
50 stack: &'a RefCell<SocketStack>, 41where
51 handle: SocketHandle, 42 D: Driver + 'static,
43{
44 stack: &'a Stack<D>,
52} 45}
53 46
54impl<'a> DnsSocket<'a> { 47impl<'a, D> DnsSocket<'a, D>
48where
49 D: Driver + 'static,
50{
55 /// Create a new DNS socket using the provided stack and query storage. 51 /// Create a new DNS socket using the provided stack and query storage.
56 /// 52 ///
57 /// DNS servers are derived from the stack configuration. 53 /// DNS servers are derived from the stack configuration.
58 /// 54 ///
59 /// NOTE: If using DHCP, make sure it has reconfigured the stack to ensure the DNS servers are updated. 55 /// NOTE: If using DHCP, make sure it has reconfigured the stack to ensure the DNS servers are updated.
60 pub fn new<D, Q>(stack: &'a Stack<D>, queries: Q) -> Self 56 pub fn new(stack: &'a Stack<D>) -> Self {
61 where 57 Self { stack }
62 D: Driver + 'static,
63 Q: Into<ManagedSlice<'a, Option<DnsQuery>>>,
64 {
65 let servers = stack
66 .config()
67 .map(|c| {
68 let v: Vec<IpAddress, 3> = c.dns_servers.iter().map(|c| IpAddress::Ipv4(*c)).collect();
69 v
70 })
71 .unwrap_or(Vec::new());
72 let s = &mut *stack.socket.borrow_mut();
73 let queries: ManagedSlice<'static, Option<DnsQuery>> = unsafe { mem::transmute(queries.into()) };
74
75 let handle = s.sockets.add(dns::Socket::new(&servers[..], queries));
76 Self {
77 stack: &stack.socket,
78 handle,
79 }
80 }
81
82 fn with_mut<R>(&mut self, f: impl FnOnce(&mut dns::Socket, &mut Interface) -> R) -> R {
83 let s = &mut *self.stack.borrow_mut();
84 let socket = s.sockets.get_mut::<dns::Socket>(self.handle);
85 let res = f(socket, &mut s.iface);
86 s.waker.wake();
87 res
88 } 58 }
89 59
90 /// Make a query for a given name and return the corresponding IP addresses. 60 /// Make a query for a given name and return the corresponding IP addresses.
91 pub async fn query(&mut self, name: &str, qtype: DnsQueryType) -> Result<Vec<IpAddress, MAX_ADDRESS_COUNT>, Error> { 61 pub async fn query(&self, name: &str, qtype: DnsQueryType) -> Result<Vec<IpAddress, MAX_ADDRESS_COUNT>, Error> {
92 let query = match { self.with_mut(|s, i| s.start_query(i.context(), name, qtype)) } { 62 self.stack.dns_query(name, qtype).await
93 Ok(handle) => handle, 63 }
94 Err(e) => return Err(e.into()), 64}
95 };
96 65
97 let handle = self.handle; 66#[cfg(all(feature = "unstable-traits", feature = "nightly"))]
98 let drop = OnDrop::new(|| { 67impl<'a, D> embedded_nal_async::Dns for DnsSocket<'a, D>
99 let s = &mut *self.stack.borrow_mut(); 68where
100 let socket = s.sockets.get_mut::<dns::Socket>(handle); 69 D: Driver + 'static,
101 socket.cancel_query(query); 70{
102 s.waker.wake(); 71 type Error = Error;
103 });
104 72
105 let res = poll_fn(|cx| { 73 async fn get_host_by_name(
106 self.with_mut(|s, _| match s.get_query_result(query) { 74 &self,
107 Ok(addrs) => Poll::Ready(Ok(addrs)), 75 host: &str,
108 Err(GetQueryResultError::Pending) => { 76 addr_type: embedded_nal_async::AddrType,
109 s.register_query_waker(query, cx.waker()); 77 ) -> Result<embedded_nal_async::IpAddr, Self::Error> {
110 Poll::Pending 78 use embedded_nal_async::{AddrType, IpAddr};
111 } 79 let qtype = match addr_type {
112 Err(e) => Poll::Ready(Err(e.into())), 80 AddrType::IPv6 => DnsQueryType::Aaaa,
81 _ => DnsQueryType::A,
82 };
83 let addrs = self.query(host, qtype).await?;
84 if let Some(first) = addrs.get(0) {
85 Ok(match first {
86 IpAddress::Ipv4(addr) => IpAddr::V4(addr.0.into()),
87 IpAddress::Ipv6(addr) => IpAddr::V6(addr.0.into()),
113 }) 88 })
114 }) 89 } else {
115 .await; 90 Err(Error::Failed)
116 91 }
117 drop.defuse();
118 res
119 } 92 }
120}
121 93
122impl<'a> Drop for DnsSocket<'a> { 94 async fn get_host_by_address(
123 fn drop(&mut self) { 95 &self,
124 self.stack.borrow_mut().sockets.remove(self.handle); 96 _addr: embedded_nal_async::IpAddr,
97 ) -> Result<heapless::String<256>, Self::Error> {
98 todo!()
125 } 99 }
126} 100}