Skip to content

Commit 974b966

Browse files
committed
Service responder mDNS example
1 parent 4060078 commit 974b966

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ required-features = ["std"]
7070
name = "mdns_responder"
7171
required-features = ["std"]
7272

73+
[[example]]
74+
name = "mdns_service_responder"
75+
required-features = ["std"]
76+
7377
[[example]]
7478
name = "ws_client"
7579
required-features = ["std"]

examples/mdns_service_responder.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use core::net::{Ipv4Addr, Ipv6Addr};
2+
3+
use edge_mdns::buf::{BufferAccess, VecBufAccess};
4+
use edge_mdns::domain::base::Ttl;
5+
use edge_mdns::host::{Service, ServiceAnswers};
6+
use edge_mdns::io::{self, MdnsIoError, DEFAULT_SOCKET};
7+
use edge_mdns::{host::Host, HostAnswersMdnsHandler};
8+
use edge_nal::{UdpBind, UdpSplit};
9+
10+
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
11+
use embassy_sync::signal::Signal;
12+
13+
use log::*;
14+
15+
use rand::{thread_rng, RngCore};
16+
17+
// Change this to the IP address of the machine where you'll run this example
18+
const OUR_IP: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
19+
20+
const OUR_NAME: &str = "mypc";
21+
22+
fn main() {
23+
env_logger::init_from_env(
24+
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
25+
);
26+
27+
let stack = edge_nal_std::Stack::new();
28+
29+
let (recv_buf, send_buf) = (
30+
VecBufAccess::<NoopRawMutex, 1500>::new(),
31+
VecBufAccess::<NoopRawMutex, 1500>::new(),
32+
);
33+
34+
futures_lite::future::block_on(run::<edge_nal_std::Stack, _, _>(
35+
&stack, &recv_buf, &send_buf, OUR_NAME, OUR_IP,
36+
))
37+
.unwrap();
38+
}
39+
40+
async fn run<T, RB, SB>(
41+
stack: &T,
42+
recv_buf: RB,
43+
send_buf: SB,
44+
our_name: &str,
45+
our_ip: Ipv4Addr,
46+
) -> Result<(), MdnsIoError<T::Error>>
47+
where
48+
T: UdpBind,
49+
RB: BufferAccess<[u8]>,
50+
SB: BufferAccess<[u8]>,
51+
{
52+
info!("About to run an mDNS responder for our PC. It will be addressable using {our_name}.local, so try to `ping {our_name}.local`.");
53+
54+
let mut socket = io::bind(stack, DEFAULT_SOCKET, Some(Ipv4Addr::UNSPECIFIED), Some(0)).await?;
55+
56+
let (recv, send) = socket.split();
57+
58+
let host = Host {
59+
hostname: our_name,
60+
ipv4: our_ip,
61+
ipv6: Ipv6Addr::UNSPECIFIED,
62+
ttl: Ttl::from_secs(60),
63+
};
64+
65+
let service = Service {
66+
name: "my-service",
67+
priority: 1,
68+
weight: 5,
69+
service: "_https",
70+
protocol: "_tcp",
71+
port: 443,
72+
service_subtypes: &[],
73+
txt_kvs: &[],
74+
};
75+
76+
// A way to notify the mDNS responder that the data in `Host` had changed
77+
// We don't use it in this example, because the data is hard-coded
78+
let signal = Signal::new();
79+
80+
let mdns = io::Mdns::<NoopRawMutex, _, _, _, _>::new(
81+
Some(Ipv4Addr::UNSPECIFIED),
82+
Some(0),
83+
recv,
84+
send,
85+
recv_buf,
86+
send_buf,
87+
|buf| thread_rng().fill_bytes(buf),
88+
&signal,
89+
);
90+
91+
mdns.run(HostAnswersMdnsHandler::new(ServiceAnswers::new(
92+
&host, &service,
93+
)))
94+
.await
95+
}

0 commit comments

Comments
 (0)