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

viam-mdns

一个多播DNS客户端库。支持在局域网中查找任何mDNS设备

1个稳定版本

3.0.1 2023年5月5日

#3#chromecast

Download history 113/week @ 2024-04-14 122/week @ 2024-04-21 90/week @ 2024-04-28 54/week @ 2024-05-05 143/week @ 2024-05-12 187/week @ 2024-05-19 57/week @ 2024-05-26 77/week @ 2024-06-02 35/week @ 2024-06-09 24/week @ 2024-06-16 18/week @ 2024-06-23 1/week @ 2024-06-30 77/week @ 2024-07-07 2/week @ 2024-07-14 8/week @ 2024-07-28

87 每月下载量
viam-rust-utils 中使用

MIT 许可证

23KB
464

mdns

Build Status crates.io MIT license

文档

Rust中的多播DNS客户端。

错误日志使用log库处理。

维基百科

示例

查找本地网络中所有Chromecasts的IP地址。

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

依赖项

~7–19MB
~226K SLoC