Skip to content

Commit

Permalink
Lookup services with address
Browse files Browse the repository at this point in the history
  • Loading branch information
lptr committed Aug 10, 2024
1 parent 9c2d820 commit 92a87ed
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/kernel/mdns.rs
Original file line number Diff line number Diff line change
@@ -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<Option<Service>> {
pub fn query_mdns_address(mdns: &EspMdns, server: &str, port: u16) -> Result<Option<Service>> {
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<Option<Service>> {
let mut results = [QueryResult {
instance_name: None,
hostname: None,
Expand All @@ -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,
}))
}

0 comments on commit 92a87ed

Please sign in to comment.