diff --git a/src/kernel/mdns.rs b/src/kernel/mdns.rs index 31e9cf7..6c2b7c9 100644 --- a/src/kernel/mdns.rs +++ b/src/kernel/mdns.rs @@ -1,14 +1,39 @@ -use std::time::Duration; +use std::{net::IpAddr, time::Duration}; +use anyhow::{anyhow, Result}; use esp_idf_svc::mdns::{EspMdns, Interface, Protocol, QueryResult}; +use esp_idf_sys::ESP_ERR_NOT_FOUND; #[derive(Debug, Clone)] pub struct Service { pub hostname: String, + pub addr: IpAddr, pub port: u16, } -pub fn query_mdns(mdns: &EspMdns, service: &str, proto: &str) -> anyhow::Result> { +pub fn query_mdns_address(mdns: &EspMdns, server: &str, port: u16) -> Result> { + let response = mdns.query_a(server, Duration::from_secs(5)); + log::info!("MDNS query result: {:?}", response); + match response { + Ok(addr) => { + let addr = IpAddr::V4(addr); + Ok(Some(Service { + hostname: server.to_string(), + addr, + port, + })) + } + Err(e) => { + if e.code() == ESP_ERR_NOT_FOUND { + Ok(None) + } else { + Err(anyhow!(e.to_string())) + } + } + } +} + +pub fn query_mdns(mdns: &EspMdns, service: &str, proto: &str) -> Result> { let mut results = [QueryResult { instance_name: None, hostname: None, @@ -23,6 +48,7 @@ pub fn query_mdns(mdns: &EspMdns, service: &str, proto: &str) -> anyhow::Result< let result = results[0].clone(); Ok(result.hostname.map(|hostname| Service { hostname: format!("{}.local", hostname), + addr: result.addr[0], port: result.port, })) }