浏览代码

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.
Benjamin Brittain 2 年之前
父节点
当前提交
23cb8bd5d0
共有 2 个文件被更改,包括 25 次插入11 次删除
  1. 2 1
      Cargo.toml
  2. 23 10
      src/socket/dns.rs

+ 2 - 1
Cargo.toml

@@ -60,6 +60,7 @@ defmt = [ "dep:defmt", "heapless/defmt", "heapless/defmt-impl" ]
 "socket-icmp" = ["socket"]
 "socket-icmp" = ["socket"]
 "socket-dhcpv4" = ["socket", "medium-ethernet", "proto-dhcpv4"]
 "socket-dhcpv4" = ["socket", "medium-ethernet", "proto-dhcpv4"]
 "socket-dns" = ["socket", "proto-dns"]
 "socket-dns" = ["socket", "proto-dns"]
+"socket-mdns" = ["socket-dns"]
 
 
 "async" = []
 "async" = []
 
 
@@ -69,7 +70,7 @@ default = [
   "phy-raw_socket", "phy-tuntap_interface",
   "phy-raw_socket", "phy-tuntap_interface",
   "proto-ipv4", "proto-igmp", "proto-dhcpv4", "proto-ipv6", "proto-dns",
   "proto-ipv4", "proto-igmp", "proto-dhcpv4", "proto-ipv6", "proto-dns",
   "proto-ipv4-fragmentation", "proto-sixlowpan-fragmentation",
   "proto-ipv4-fragmentation", "proto-sixlowpan-fragmentation",
-  "socket-raw", "socket-icmp", "socket-udp", "socket-tcp", "socket-dhcpv4", "socket-dns",
+  "socket-raw", "socket-icmp", "socket-udp", "socket-tcp", "socket-dhcpv4", "socket-dns", "socket-mdns",
   "async"
   "async"
 ]
 ]
 
 

+ 23 - 10
src/socket/dns.rs

@@ -90,7 +90,14 @@ struct PendingQuery {
     delay: Duration,
     delay: Duration,
 
 
     server_idx: usize,
     server_idx: usize,
-    mdns: bool,
+    mdns: MulticastDns,
+}
+
+#[derive(Debug)]
+pub enum MulticastDns {
+    Disabled,
+    #[cfg(feature = "socket-mdns")]
+    Enabled,
 }
 }
 
 
 #[derive(Debug)]
 #[derive(Debug)]
@@ -211,10 +218,11 @@ impl<'a> Socket<'a> {
 
 
         let mut raw_name: Vec<u8, MAX_NAME_LEN> = Vec::new();
         let mut raw_name: Vec<u8, MAX_NAME_LEN> = Vec::new();
 
 
-        let mut mdns = false;
+        let mut mdns = MulticastDns::Disabled;
+        #[cfg(feature = "socket-mdns")]
         if name.split(|&c| c == b'.').last().unwrap() == b"local" {
         if name.split(|&c| c == b'.').last().unwrap() == b"local" {
             net_trace!("Starting a mDNS query");
             net_trace!("Starting a mDNS query");
-            mdns = true;
+            mdns = MulticastDns::Enabled;
         }
         }
 
 
         for s in name.split(|&c| c == b'.') {
         for s in name.split(|&c| c == b'.') {
@@ -253,7 +261,7 @@ impl<'a> Socket<'a> {
         cx: &mut Context,
         cx: &mut Context,
         raw_name: &[u8],
         raw_name: &[u8],
         query_type: Type,
         query_type: Type,
-        mdns: bool,
+        mdns: MulticastDns,
     ) -> Result<QueryHandle, StartQueryError> {
     ) -> Result<QueryHandle, StartQueryError> {
         let handle = self.find_free_query().ok_or(StartQueryError::NoFreeSlot)?;
         let handle = self.find_free_query().ok_or(StartQueryError::NoFreeSlot)?;
 
 
@@ -506,16 +514,17 @@ impl<'a> Socket<'a> {
                 // As per RFC 6762 any DNS query ending in .local. MUST be sent as mdns
                 // As per RFC 6762 any DNS query ending in .local. MUST be sent as mdns
                 // so we internally overwrite the servers for any of those queries
                 // so we internally overwrite the servers for any of those queries
                 // in this function.
                 // in this function.
-                let servers = if pq.mdns {
-                    &[
+                let servers = match pq.mdns {
+                    #[cfg(feature = "socket-mdns")]
+                    MulticastDns::Enabled => &[
                         #[cfg(feature = "proto-ipv6")]
                         #[cfg(feature = "proto-ipv6")]
                         MDNS_IPV6_ADDR,
                         MDNS_IPV6_ADDR,
                         #[cfg(feature = "proto-ipv4")]
                         #[cfg(feature = "proto-ipv4")]
                         MDNS_IPV4_ADDR,
                         MDNS_IPV4_ADDR,
-                    ]
-                } else {
-                    self.servers.as_slice()
+                    ],
+                    MulticastDns::Disabled => self.servers.as_slice(),
                 };
                 };
+
                 let timeout = if let Some(timeout) = pq.timeout_at {
                 let timeout = if let Some(timeout) = pq.timeout_at {
                     timeout
                     timeout
                 } else {
                 } else {
@@ -567,7 +576,11 @@ impl<'a> Socket<'a> {
                 let payload = &mut payload[..repr.buffer_len()];
                 let payload = &mut payload[..repr.buffer_len()];
                 repr.emit(&mut Packet::new_unchecked(payload));
                 repr.emit(&mut Packet::new_unchecked(payload));
 
 
-                let dst_port = if pq.mdns { MDNS_DNS_PORT } else { DNS_PORT };
+                let dst_port = match pq.mdns {
+                    #[cfg(feature = "socket-mdns")]
+                    MulticastDns::Enabled => MDNS_DNS_PORT,
+                    MulticastDns::Disabled => DNS_PORT,
+                };
 
 
                 let udp_repr = UdpRepr {
                 let udp_repr = UdpRepr {
                     src_port: pq.port,
                     src_port: pq.port,