#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

#1216 in Network programming

Download history 990/week @ 2023-12-11 838/week @ 2023-12-18 525/week @ 2023-12-25 534/week @ 2024-01-01 1050/week @ 2024-01-08 1046/week @ 2024-01-15 985/week @ 2024-01-22 1017/week @ 2024-01-29 1306/week @ 2024-02-05 760/week @ 2024-02-12 790/week @ 2024-02-19 916/week @ 2024-02-26 1223/week @ 2024-03-04 1369/week @ 2024-03-11 1532/week @ 2024-03-18 944/week @ 2024-03-25

5,159 downloads per month
Used in 42 crates (7 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–18MB
~231K SLoC