Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gatekeep mdns ipv6 behind feature flag #523

Merged
merged 7 commits into from
Nov 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/mdns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ use alloc::vec::Vec;

use ::log::info;

use embedded_svc::ipv4::{IpAddr, Ipv4Addr, Ipv6Addr};
#[cfg(any(esp_idf_lwip_ipv4, esp_idf_lwip_ipv6))]
use embedded_svc::ipv4::IpAddr;
#[cfg(esp_idf_lwip_ipv4)]
use embedded_svc::ipv4::Ipv4Addr;
#[cfg(esp_idf_lwip_ipv6)]
use embedded_svc::ipv4::Ipv6Addr;

use crate::sys::*;

Expand All @@ -26,13 +31,17 @@ pub enum Interface {

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Protocol {
#[cfg(esp_idf_lwip_ipv4)]
V4,
#[cfg(esp_idf_lwip_ipv6)]
V6,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Type {
#[cfg(esp_idf_lwip_ipv4)]
A = MDNS_TYPE_A as _,
#[cfg(esp_idf_lwip_ipv6)]
AAAA = MDNS_TYPE_AAAA as _,
ANY = MDNS_TYPE_ANY as _,
NSEC = MDNS_TYPE_NSEC as _,
Expand Down Expand Up @@ -82,7 +91,9 @@ impl From<mdns_result_t> for QueryResult {
while !p.is_null() {
let a = unsafe { (*p).addr };
let a = match a.type_ as _ {
#[cfg(esp_idf_lwip_ipv4)]
ESP_IPADDR_TYPE_V4 => IpAddr::V4(from_esp_ip4_addr_t(unsafe { &a.u_addr.ip4 })),
#[cfg(esp_idf_lwip_ipv6)]
ESP_IPADDR_TYPE_V6 => IpAddr::V6(from_esp_ip6_addr_t(unsafe { &a.u_addr.ip6 })),
_ => unreachable!(),
};
Expand Down Expand Up @@ -112,7 +123,9 @@ impl From<mdns_result_t> for QueryResult {
};

let ip_protocol = match result.ip_protocol {
#[cfg(esp_idf_lwip_ipv4)]
mdns_ip_protocol_t_MDNS_IP_PROTOCOL_V4 => Protocol::V4,
#[cfg(esp_idf_lwip_ipv6)]
mdns_ip_protocol_t_MDNS_IP_PROTOCOL_V6 => Protocol::V6,
_ => unreachable!(),
};
Expand Down Expand Up @@ -361,6 +374,7 @@ impl EspMdns {
Ok(copy_query_results(result, results))
}

#[cfg(esp_idf_lwip_ipv4)]
pub fn query_a(
&self,
hostname: impl AsRef<str>,
Expand All @@ -374,6 +388,7 @@ impl EspMdns {
Ok(from_esp_ip4_addr_t(&addr))
}

#[cfg(esp_idf_lwip_ipv6)]
pub fn query_aaaa(
&self,
hostname: impl AsRef<str>,
Expand Down Expand Up @@ -493,10 +508,12 @@ fn copy_query_results(src: *mut mdns_result_t, dst: &mut [QueryResult]) -> usize
}
}

#[cfg(esp_idf_lwip_ipv4)]
fn from_esp_ip4_addr_t(addr: &esp_ip4_addr_t) -> Ipv4Addr {
Ipv4Addr::from(addr.addr.to_le_bytes())
}

#[cfg(esp_idf_lwip_ipv6)]
fn from_esp_ip6_addr_t(addr: &esp_ip6_addr_t) -> Ipv6Addr {
let mut buf = [0u8; 16];
let mut i = 0;
Expand Down