Crates.io | viam-mdns |
lib.rs | viam-mdns |
version | 3.0.1 |
source | src |
created_at | 2023-05-05 14:09:53.736574 |
updated_at | 2023-05-05 14:09:53.736574 |
description | A multicast DNS client library. Supports discovery of any mDNS device on a LAN. |
homepage | |
repository | https://github.com/stuqdog/mdns |
max_upload_size | |
id | 857757 |
size | 48,029 |
An multicast DNS client in Rust.
Error logging is handled with the log
library.
Find IP addresses for all Chromecasts on the local network.
use futures_util::{pin_mut, stream::StreamExt};
use mdns::{Error, Record, RecordKind};
use std::{net::IpAddr, time::Duration};
const SERVICE_NAME: &'static str = "_googlecast._tcp.local";
#[async_std::main]
async fn main() -> Result<(), Error> {
// Iterate through responses from each Cast device, asking for new devices every 15s
let stream = mdns::discover::all(SERVICE_NAME, Duration::from_secs(15))?.listen();
pin_mut!(stream);
while let Some(Ok(response)) = stream.next().await {
let addr = response.records()
.filter_map(self::to_ip_addr)
.next();
if let Some(addr) = addr {
println!("found cast device at {}", addr);
} else {
println!("cast device does not advertise address");
}
}
Ok(())
}
fn to_ip_addr(record: &Record) -> Option<IpAddr> {
match record.kind {
RecordKind::A(addr) => Some(addr.into()),
RecordKind::AAAA(addr) => Some(addr.into()),
_ => None,
}
}