Эх сурвалжийг харах

dns: feature and unused fixes

Dario Nieuwenhuis 2 жил өмнө
parent
commit
ac12274029

+ 3 - 2
src/iface/interface.rs

@@ -384,7 +384,7 @@ impl<'a> IpPacket<'a> {
             IpPacket::Icmpv6((ipv6_repr, _)) => IpRepr::Ipv6(*ipv6_repr),
             #[cfg(feature = "socket-raw")]
             IpPacket::Raw((ip_repr, _)) => ip_repr.clone(),
-            #[cfg(feature = "socket-udp")]
+            #[cfg(any(feature = "socket-udp", feature = "socket-dns"))]
             IpPacket::Udp((ip_repr, _, _)) => ip_repr.clone(),
             #[cfg(feature = "socket-tcp")]
             IpPacket::Tcp((ip_repr, _)) => ip_repr.clone(),
@@ -1685,7 +1685,7 @@ impl<'a> InterfaceInner<'a> {
             #[cfg(feature = "proto-igmp")]
             IpProtocol::Igmp => self.process_igmp(ipv4_repr, ip_payload),
 
-            #[cfg(feature = "socket-udp")]
+            #[cfg(any(feature = "socket-udp", feature = "socket-dns"))]
             IpProtocol::Udp => {
                 self.process_udp(sockets, ip_repr, handled_by_raw_socket, ip_payload)
             }
@@ -2134,6 +2134,7 @@ impl<'a> InterfaceInner<'a> {
         let udp_repr = UdpRepr::parse(&udp_packet, &src_addr, &dst_addr, &self.caps.checksum)?;
         let udp_payload = udp_packet.payload();
 
+        #[cfg(feature = "socket-udp")]
         for udp_socket in sockets
             .iter_mut()
             .filter_map(|i| UdpSocket::downcast(&mut i.socket))

+ 2 - 1
src/lib.rs

@@ -112,9 +112,10 @@ compile_error!("You must enable at least one of the following features: proto-ip
         feature = "socket-tcp",
         feature = "socket-icmp",
         feature = "socket-dhcp",
+        feature = "socket-dns",
     ))
 ))]
-compile_error!("If you enable the socket feature, you must enable at least one of the following features: socket-raw, socket-udp, socket-tcp, socket-icmp, socket-dhcp");
+compile_error!("If you enable the socket feature, you must enable at least one of the following features: socket-raw, socket-udp, socket-tcp, socket-icmp, socket-dhcp, socket-dns");
 
 #[cfg(all(
     feature = "socket",

+ 12 - 8
src/socket/dns.rs

@@ -1,12 +1,11 @@
-#![allow(dead_code, unused)]
 use heapless::Vec;
 use managed::ManagedSlice;
 
 use crate::socket::{Context, PollAt, Socket};
 use crate::time::{Duration, Instant};
 use crate::wire::dns::{Flags, Opcode, Packet, Question, Rcode, Record, RecordData, Repr, Type};
-use crate::wire::{IpAddress, IpEndpoint, IpProtocol, IpRepr, Ipv4Address, UdpRepr};
-use crate::{rand, Error, Result};
+use crate::wire::{IpAddress, IpProtocol, IpRepr, UdpRepr};
+use crate::{Error, Result};
 
 const DNS_PORT: u16 = 53;
 const MAX_NAME_LEN: usize = 255;
@@ -230,7 +229,9 @@ impl<'a> DnsSocket<'a> {
 
     pub fn cancel_query(&mut self, handle: QueryHandle) -> Result<()> {
         let slot = self.queries.get_mut(handle.0).ok_or(Error::Illegal)?;
-        let q = slot.as_mut().ok_or(Error::Illegal)?;
+        if slot.is_none() {
+            return Err(Error::Illegal);
+        }
         *slot = None; // Free up the slot for recycling.
         Ok(())
     }
@@ -245,7 +246,7 @@ impl<'a> DnsSocket<'a> {
 
     pub(crate) fn process(
         &mut self,
-        cx: &mut Context,
+        _cx: &mut Context,
         ip_repr: &IpRepr,
         udp_repr: &UdpRepr,
         payload: &[u8],
@@ -316,12 +317,14 @@ impl<'a> DnsSocket<'a> {
                     }
 
                     match r.data {
+                        #[cfg(feature = "proto-ipv4")]
                         RecordData::A(addr) => {
                             net_trace!("A: {:?}", addr);
                             if addresses.push(addr.into()).is_err() {
                                 net_trace!("too many addresses in response, ignoring {:?}", addr);
                             }
                         }
+                        #[cfg(feature = "proto-ipv6")]
                         RecordData::Aaaa(addr) => {
                             net_trace!("AAAA: {:?}", addr);
                             if addresses.push(addr.into()).is_err() {
@@ -515,12 +518,13 @@ fn copy_name<'a, const N: usize>(
 
     for label in name {
         let label = label?;
-        dest.push(label.len() as u8).map_err(|_| Error::Truncated);
-        dest.extend_from_slice(label).map_err(|_| Error::Truncated);
+        dest.push(label.len() as u8).map_err(|_| Error::Truncated)?;
+        dest.extend_from_slice(label)
+            .map_err(|_| Error::Truncated)?;
     }
 
     // Write terminator 0x00
-    dest.push(0).map_err(|_| Error::Truncated);
+    dest.push(0).map_err(|_| Error::Truncated)?;
 
     Ok(())
 }

+ 2 - 0
src/wire/dns.rs

@@ -335,12 +335,14 @@ pub struct Record<'a> {
 impl<'a> RecordData<'a> {
     pub fn parse(type_: Type, data: &'a [u8]) -> Result<RecordData<'a>> {
         match type_ {
+            #[cfg(feature = "proto-ipv4")]
             Type::A => {
                 if data.len() != 4 {
                     return Err(Error::Malformed);
                 }
                 Ok(RecordData::A(Ipv4Address::from_bytes(data)))
             }
+            #[cfg(feature = "proto-ipv6")]
             Type::Aaaa => {
                 if data.len() != 16 {
                     return Err(Error::Malformed);