viam_mdns/
lib.rs

1//! [Multicast DNS](https://en.wikipedia.org/wiki/Multicast_DNS) library with built-in networking.
2//!
3//! This crate can be used to discover mDNS devices that are listening
4//! on a network.
5//!
6//! # Basic usage
7//!
8//! This example finds all [Chromecast](https://en.wikipedia.org/wiki/Chromecast) devices on the
9//! same LAN as the executing computer.
10//!
11//! Once the devices are discovered, they respond with standard DNS records, with a few minor
12//! low-level protocol differences.
13//!
14//! The only Chromecast-specific piece of code here is the `SERVICE_NAME`. In order to discover
15//! other types of devices, simply change the service name to the one your device uses.
16//!
17//! This example obtains the IP addresses of the cast devices by looking up `A`/`AAAA` records.
18//!
19//! ```rust,no_run
20//! use futures_util::{pin_mut, stream::StreamExt};
21//! use mdns::{Error, Record, RecordKind};
22//! use std::{net::IpAddr, time::Duration};
23//!
24//! /// The hostname of the devices we are searching for.
25//! /// Every Chromecast will respond to the service name in this example.
26//! const SERVICE_NAME: &'static str = "_googlecast._tcp.local";
27//!
28//! #[async_std::main]
29//! async fn main() -> Result<(), Error> {
30//!     // Iterate through responses from each Cast device, asking for new devices every 15s
31//!     let stream = mdns::discover::all(SERVICE_NAME, Duration::from_secs(15))?.listen();
32//!     pin_mut!(stream);
33//!
34//!     while let Some(Ok(response)) = stream.next().await {
35//!         let addr = response.records()
36//!                            .filter_map(self::to_ip_addr)
37//!                            .next();
38//!
39//!         if let Some(addr) = addr {
40//!             println!("found cast device at {}", addr);
41//!         } else {
42//!             println!("cast device does not advertise address");
43//!         }
44//!     }
45//!
46//!     Ok(())
47//! }
48//!
49//! fn to_ip_addr(record: &Record) -> Option<IpAddr> {
50//!     match record.kind {
51//!         RecordKind::A(addr) => Some(addr.into()),
52//!         RecordKind::AAAA(addr) => Some(addr.into()),
53//!         _ => None,
54//!     }
55//! }
56//! ```
57
58#![recursion_limit = "1024"]
59
60pub use self::errors::Error;
61pub use self::response::{Record, RecordKind, Response};
62
63pub mod discover;
64pub mod resolve;
65
66mod errors;
67mod mdns;
68mod response;
69
70pub use self::mdns::mDNSListener;