#dns-client #discovery #multicast #dns #chromecast #networking #ip-address

mdns

A multicast DNS client library. Supports discovery of any mDNS device on a LAN

17 releases (6 stable)

3.0.0 Jan 29, 2021
2.0.2 Jan 29, 2021
1.1.0 Jan 30, 2020
0.3.2 Jan 26, 2020
0.1.2 Nov 27, 2016

#1574 in Network programming

Download history 1417/week @ 2024-03-15 1260/week @ 2024-03-22 1059/week @ 2024-03-29 1277/week @ 2024-04-05 1333/week @ 2024-04-12 1244/week @ 2024-04-19 1585/week @ 2024-04-26 1259/week @ 2024-05-03 1379/week @ 2024-05-10 1624/week @ 2024-05-17 1493/week @ 2024-05-24 2035/week @ 2024-05-31 1467/week @ 2024-06-07 1568/week @ 2024-06-14 1403/week @ 2024-06-21 847/week @ 2024-06-28

5,665 downloads per month
Used in 39 crates (8 directly)

MIT license

22KB
419 lines

mdns

Build Status crates.io MIT license

Documentation

An multicast DNS client in Rust.

Error logging is handled with the log library.

Wikipedia

Example

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,
    }
}

Dependencies

~7–16MB
~233K SLoC