Skip to content

Commit 23cb8bd

Browse files
committed
Move mDNS implementation behind feature flag
Swaps the bool in the raw query API to an enum that can statically prevent mdns usage without the feature flag enabled.
1 parent e4af711 commit 23cb8bd

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ defmt = [ "dep:defmt", "heapless/defmt", "heapless/defmt-impl" ]
6060
"socket-icmp" = ["socket"]
6161
"socket-dhcpv4" = ["socket", "medium-ethernet", "proto-dhcpv4"]
6262
"socket-dns" = ["socket", "proto-dns"]
63+
"socket-mdns" = ["socket-dns"]
6364

6465
"async" = []
6566

@@ -69,7 +70,7 @@ default = [
6970
"phy-raw_socket", "phy-tuntap_interface",
7071
"proto-ipv4", "proto-igmp", "proto-dhcpv4", "proto-ipv6", "proto-dns",
7172
"proto-ipv4-fragmentation", "proto-sixlowpan-fragmentation",
72-
"socket-raw", "socket-icmp", "socket-udp", "socket-tcp", "socket-dhcpv4", "socket-dns",
73+
"socket-raw", "socket-icmp", "socket-udp", "socket-tcp", "socket-dhcpv4", "socket-dns", "socket-mdns",
7374
"async"
7475
]
7576

src/socket/dns.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,14 @@ struct PendingQuery {
9090
delay: Duration,
9191

9292
server_idx: usize,
93-
mdns: bool,
93+
mdns: MulticastDns,
94+
}
95+
96+
#[derive(Debug)]
97+
pub enum MulticastDns {
98+
Disabled,
99+
#[cfg(feature = "socket-mdns")]
100+
Enabled,
94101
}
95102

96103
#[derive(Debug)]
@@ -211,10 +218,11 @@ impl<'a> Socket<'a> {
211218

212219
let mut raw_name: Vec<u8, MAX_NAME_LEN> = Vec::new();
213220

214-
let mut mdns = false;
221+
let mut mdns = MulticastDns::Disabled;
222+
#[cfg(feature = "socket-mdns")]
215223
if name.split(|&c| c == b'.').last().unwrap() == b"local" {
216224
net_trace!("Starting a mDNS query");
217-
mdns = true;
225+
mdns = MulticastDns::Enabled;
218226
}
219227

220228
for s in name.split(|&c| c == b'.') {
@@ -253,7 +261,7 @@ impl<'a> Socket<'a> {
253261
cx: &mut Context,
254262
raw_name: &[u8],
255263
query_type: Type,
256-
mdns: bool,
264+
mdns: MulticastDns,
257265
) -> Result<QueryHandle, StartQueryError> {
258266
let handle = self.find_free_query().ok_or(StartQueryError::NoFreeSlot)?;
259267

@@ -506,16 +514,17 @@ impl<'a> Socket<'a> {
506514
// As per RFC 6762 any DNS query ending in .local. MUST be sent as mdns
507515
// so we internally overwrite the servers for any of those queries
508516
// in this function.
509-
let servers = if pq.mdns {
510-
&[
517+
let servers = match pq.mdns {
518+
#[cfg(feature = "socket-mdns")]
519+
MulticastDns::Enabled => &[
511520
#[cfg(feature = "proto-ipv6")]
512521
MDNS_IPV6_ADDR,
513522
#[cfg(feature = "proto-ipv4")]
514523
MDNS_IPV4_ADDR,
515-
]
516-
} else {
517-
self.servers.as_slice()
524+
],
525+
MulticastDns::Disabled => self.servers.as_slice(),
518526
};
527+
519528
let timeout = if let Some(timeout) = pq.timeout_at {
520529
timeout
521530
} else {
@@ -567,7 +576,11 @@ impl<'a> Socket<'a> {
567576
let payload = &mut payload[..repr.buffer_len()];
568577
repr.emit(&mut Packet::new_unchecked(payload));
569578

570-
let dst_port = if pq.mdns { MDNS_DNS_PORT } else { DNS_PORT };
579+
let dst_port = match pq.mdns {
580+
#[cfg(feature = "socket-mdns")]
581+
MulticastDns::Enabled => MDNS_DNS_PORT,
582+
MulticastDns::Disabled => DNS_PORT,
583+
};
571584

572585
let udp_repr = UdpRepr {
573586
src_port: pq.port,

0 commit comments

Comments
 (0)