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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
use core::fmt::Debug;
use core::future::Future;
use core::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV6};
use edge_nal::{UdpBind, UdpSplit};
use rs_matter::crypto::Crypto;
use rs_matter::error::{Error, ErrorCode};
use rs_matter::transport::network::mdns::builtin::BuiltinMdns;
use rs_matter::Matter;
use crate::udp;
/// A trait for running an mDNS responder.
pub trait Mdns {
/// Run the mDNS responder with the given UDP binding, MAC address, IPv4 and IPv6 addresses, and interface index.
///
/// NOTE: This trait might change once `rs-matter` starts supporting mDNS resolvers
///
/// # Arguments
/// - `matter`: A reference to the `Matter` instance.
/// - `crypto`: An object implementing the `Crypto` trait for cryptographic operations.
/// - `udp`: An object implementing the `UdpBind` trait for binding UDP sockets.
/// - `mac`: The MAC address of the host, used to generate the hostname.
/// - `ipv4`: The IPv4 address of the host.
/// - `ipv6`: The IPv6 address of the host.
/// - `interface`: The interface index for the host, used for IPv6 multicast.
#[allow(clippy::too_many_arguments)]
async fn run<C, U>(
&mut self,
matter: &Matter<'_>,
crypto: C,
udp: U,
mac: &[u8],
ipv4: Ipv4Addr,
ipv6: Ipv6Addr,
interface: u32,
) -> Result<(), Error>
where
C: Crypto,
U: UdpBind;
}
impl<T> Mdns for &mut T
where
T: Mdns,
{
fn run<C, U>(
&mut self,
matter: &Matter<'_>,
crypto: C,
udp: U,
mac: &[u8],
ipv4: Ipv4Addr,
ipv6: Ipv6Addr,
interface: u32,
) -> impl Future<Output = Result<(), Error>>
where
C: Crypto,
U: UdpBind,
{
(*self).run(matter, crypto, udp, mac, ipv4, ipv6, interface)
}
}
impl Mdns for BuiltinMdns {
async fn run<C, U>(
&mut self,
matter: &Matter<'_>,
crypto: C,
udp: U,
mac: &[u8],
ipv4: Ipv4Addr,
ipv6: Ipv6Addr,
interface: u32,
) -> Result<(), Error>
where
C: Crypto,
U: UdpBind,
{
use core::fmt::Write as _;
use edge_nal::{MulticastV4, MulticastV6};
use rs_matter::transport::network::mdns::builtin::Host;
use rs_matter::transport::network::mdns::{
MDNS_IPV4_BROADCAST_ADDR, MDNS_IPV6_BROADCAST_ADDR, MDNS_PORT,
};
let mut socket = udp
.bind(SocketAddr::V6(SocketAddrV6::new(
Ipv6Addr::UNSPECIFIED,
MDNS_PORT,
0,
0,
)))
.await
.map_err(map_err)?;
socket
.join_v4(MDNS_IPV4_BROADCAST_ADDR, ipv4)
.await
.map_err(map_err)?;
socket
.join_v6(MDNS_IPV6_BROADCAST_ADDR, interface)
.await
.map_err(map_err)?;
let (recv, send) = socket.split();
let mut hostname = heapless::String::<16>::new();
if mac.len() == 6 {
write_unwrap!(
hostname,
"{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}",
mac[0],
mac[1],
mac[2],
mac[3],
mac[4],
mac[5]
);
} else if mac.len() == 8 {
write_unwrap!(
hostname,
"{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}",
mac[0],
mac[1],
mac[2],
mac[3],
mac[4],
mac[5],
mac[6],
mac[7]
);
} else {
panic!("Invalid MAC address length: should be 6 or 8 bytes");
}
BuiltinMdns::run(
self,
udp::Udp(send),
udp::Udp(recv),
&Host {
hostname: &hostname,
ip: ipv4,
ipv6,
},
Some(ipv4),
Some(interface),
matter,
crypto,
)
.await
}
}
#[cfg(feature = "zbus")]
impl Mdns for rs_matter::transport::network::mdns::avahi::AvahiMdns {
fn run<C, U>(
&mut self,
matter: &Matter<'_>,
_crypto: C,
_udp: U,
_mac: &[u8],
_ipv4: Ipv4Addr,
_ipv6: Ipv6Addr,
_interface: u32,
) -> impl Future<Output = Result<(), Error>>
where
C: Crypto,
U: UdpBind,
{
Self::run(self, matter)
}
}
#[cfg(feature = "zbus")]
impl Mdns for rs_matter::transport::network::mdns::resolve::ResolveMdns {
fn run<C, U>(
&mut self,
matter: &Matter<'_>,
_crypto: C,
_udp: U,
_mac: &[u8],
_ipv4: Ipv4Addr,
_ipv6: Ipv6Addr,
_interface: u32,
) -> impl Future<Output = Result<(), Error>>
where
C: Crypto,
U: UdpBind,
{
Self::run(self, matter)
}
}
#[cfg(feature = "zeroconf")]
impl Mdns for rs_matter::transport::network::mdns::zeroconf::ZeroconfMdns {
fn run<C, U>(
&mut self,
matter: &Matter<'_>,
_crypto: C,
_udp: U,
_mac: &[u8],
_ipv4: Ipv4Addr,
_ipv6: Ipv6Addr,
_interface: u32,
) -> impl Future<Output = Result<(), Error>>
where
C: Crypto,
U: UdpBind,
{
Self::run(self, matter)
}
}
/// An mDNS responder for Matter using the `astro-dnssd` crate.
#[cfg(feature = "astro-dnssd")]
impl Mdns for rs_matter::transport::network::mdns::astro::AstroMdns {
async fn run<C, U>(
&mut self,
matter: &Matter<'_>,
_crypto: C,
_udp: U,
_mac: &[u8],
_ipv4: Ipv4Addr,
_ipv6: Ipv6Addr,
_interface: u32,
) -> Result<(), Error>
where
C: Crypto,
U: UdpBind,
{
Self::run(self, matter).await
}
}
// TODO: Need to fix edge-mdns's `Service` signature.
// /// Utilities for using `edge-mdns` as an mDNS responder for `rs-matter`.
// ///
// /// `rs-matter` does have a built-in mDNS imlementation, and that implementation is
// /// in fact primarily maintained by the same author who maintains `edge-mdns`.
// ///
// /// However, the key difference between the two is that the `rs-matter` built-in mDNS
// /// implementation - _for now_ -_only_ responds to queries which concern the Matter
// /// protocol itself and also does not expose a query interface. This makes it unsuitable
// /// for use in cases where the same host that operates an `rs-matter` stack needs to -
// /// for whatever reasons - to host additional service types different than the ones
// /// concerning Matter, and/or issue ad-hoc mDNS queries outside the queries necessary
// /// for operating the Matter stack.
// ///
// /// Using `edge-mdns` solves this problem by providing a general-purpose mDNS which can be
// /// shared between the `rs-matter` stack and other - user-specific use cases.
// #[cfg(feature = "edge-mdns")]
// pub mod edge_mdns {
// use rs_matter::error::Error;
// use rs_matter::Matter;
// /// An adaptor from `rs-matter` buffers to `edge-mdns` buffers.
// pub struct MatterBuffer<B>(B);
// impl<B> MatterBuffer<B> {
// /// Create a new instance of `MatterBuffer`
// pub const fn new(buffer: B) -> Self {
// Self(buffer)
// }
// }
// impl<B, T> edge_mdns::buf::BufferAccess<T> for MatterBuffer<B>
// where
// B: rs_matter::utils::storage::pooled::BufferAccess<T>,
// T: ?Sized,
// {
// type Buffer<'a>
// = B::Buffer<'a>
// where
// Self: 'a;
// async fn get(&self) -> Option<Self::Buffer<'_>> {
// self.0.get().await
// }
// }
// /// Visit all mDNS services registered by `rs-matter` as `edge_mdns::host::Service` instances.
// pub fn emdns_services<T>(matter: &Matter<'_>, mut visitor: T) -> Result<(), Error>
// where
// T: FnMut(&edge_mdns::host::Service) -> Result<(), Error>,
// {
// matter.mdns_services(|matter_service| {
// rs_matter::transport::network::mdns::Service::call_with(
// &matter_service,
// matter.dev_det(),
// matter.port(),
// |service| {
// let service = edge_mdns::host::Service {
// name: service.name,
// service: service.service,
// protocol: service.protocol,
// port: service.port,
// service_subtypes: service.service_subtypes,
// txt_kvs: service.txt_kvs,
// priority: 0,
// weight: 0,
// };
// visitor(&service)
// },
// )
// })
// }
// }
fn map_err<E: Debug>(e: E) -> Error {
warn!("mDNS network error: {:?}", debug2format!(e));
ErrorCode::StdIoError.into() // TODO
}