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

viam-mdns

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

1 stable release

3.0.1 May 5, 2023

#3 in #chromecast

Download history 93/week @ 2024-01-06 22/week @ 2024-01-13 1/week @ 2024-01-20 15/week @ 2024-02-03 1/week @ 2024-02-10 6/week @ 2024-02-17 33/week @ 2024-02-24 114/week @ 2024-03-02 43/week @ 2024-03-09 80/week @ 2024-03-16 78/week @ 2024-03-23 126/week @ 2024-03-30 49/week @ 2024-04-06 114/week @ 2024-04-13 119/week @ 2024-04-20

415 downloads per month
Used in viam-rust-utils

MIT license

23KB
464 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
~221K SLoC