Parcourir la source

chore(iface): move udp into own file

Signed-off-by: Thibaut Vandervelden <thvdveld@vub.be>
Thibaut Vandervelden il y a 1 an
Parent
commit
d96dd732b2

+ 2 - 73
src/iface/interface/mod.rs

@@ -19,6 +19,8 @@ mod sixlowpan;
 
 #[cfg(feature = "proto-igmp")]
 mod igmp;
+#[cfg(any(feature = "socket-udp", feature = "socket-dns"))]
+mod udp;
 
 #[cfg(feature = "proto-igmp")]
 pub use igmp::MulticastError;
@@ -45,8 +47,6 @@ use crate::iface::Routes;
 use crate::phy::PacketMeta;
 use crate::phy::{ChecksumCapabilities, Device, DeviceCapabilities, Medium, RxToken, TxToken};
 use crate::rand::Rand;
-#[cfg(feature = "socket-dns")]
-use crate::socket::dns;
 use crate::socket::*;
 use crate::time::{Duration, Instant};
 
@@ -1111,77 +1111,6 @@ impl InterfaceInner {
         address.is_unicast() && !self.is_broadcast_v4(address)
     }
 
-    #[cfg(any(feature = "socket-udp", feature = "socket-dns"))]
-    fn process_udp<'frame>(
-        &mut self,
-        sockets: &mut SocketSet,
-        meta: PacketMeta,
-        handled_by_raw_socket: bool,
-        ip_repr: IpRepr,
-        ip_payload: &'frame [u8],
-    ) -> Option<Packet<'frame>> {
-        let (src_addr, dst_addr) = (ip_repr.src_addr(), ip_repr.dst_addr());
-        let udp_packet = check!(UdpPacket::new_checked(ip_payload));
-        let udp_repr = check!(UdpRepr::parse(
-            &udp_packet,
-            &src_addr,
-            &dst_addr,
-            &self.caps.checksum
-        ));
-
-        #[cfg(feature = "socket-udp")]
-        for udp_socket in sockets
-            .items_mut()
-            .filter_map(|i| udp::Socket::downcast_mut(&mut i.socket))
-        {
-            if udp_socket.accepts(self, &ip_repr, &udp_repr) {
-                udp_socket.process(self, meta, &ip_repr, &udp_repr, udp_packet.payload());
-                return None;
-            }
-        }
-
-        #[cfg(feature = "socket-dns")]
-        for dns_socket in sockets
-            .items_mut()
-            .filter_map(|i| dns::Socket::downcast_mut(&mut i.socket))
-        {
-            if dns_socket.accepts(&ip_repr, &udp_repr) {
-                dns_socket.process(self, &ip_repr, &udp_repr, udp_packet.payload());
-                return None;
-            }
-        }
-
-        // The packet wasn't handled by a socket, send an ICMP port unreachable packet.
-        match ip_repr {
-            #[cfg(feature = "proto-ipv4")]
-            IpRepr::Ipv4(_) if handled_by_raw_socket => None,
-            #[cfg(feature = "proto-ipv6")]
-            IpRepr::Ipv6(_) if handled_by_raw_socket => None,
-            #[cfg(feature = "proto-ipv4")]
-            IpRepr::Ipv4(ipv4_repr) => {
-                let payload_len =
-                    icmp_reply_payload_len(ip_payload.len(), IPV4_MIN_MTU, ipv4_repr.buffer_len());
-                let icmpv4_reply_repr = Icmpv4Repr::DstUnreachable {
-                    reason: Icmpv4DstUnreachable::PortUnreachable,
-                    header: ipv4_repr,
-                    data: &ip_payload[0..payload_len],
-                };
-                self.icmpv4_reply(ipv4_repr, icmpv4_reply_repr)
-            }
-            #[cfg(feature = "proto-ipv6")]
-            IpRepr::Ipv6(ipv6_repr) => {
-                let payload_len =
-                    icmp_reply_payload_len(ip_payload.len(), IPV6_MIN_MTU, ipv6_repr.buffer_len());
-                let icmpv6_reply_repr = Icmpv6Repr::DstUnreachable {
-                    reason: Icmpv6DstUnreachable::PortUnreachable,
-                    header: ipv6_repr,
-                    data: &ip_payload[0..payload_len],
-                };
-                self.icmpv6_reply(ipv6_repr, icmpv6_reply_repr)
-            }
-        }
-    }
-
     #[cfg(feature = "socket-tcp")]
     pub(crate) fn process_tcp<'frame>(
         &mut self,

+ 1 - 0
src/iface/interface/tests/ipv4.rs

@@ -786,6 +786,7 @@ fn test_raw_socket_no_reply(#[case] medium: Medium) {
     feature = "medium-ethernet"
 ))]
 fn test_raw_socket_with_udp_socket(#[case] medium: Medium) {
+    use crate::socket::udp;
     use crate::wire::{IpEndpoint, IpVersion, UdpPacket, UdpRepr};
 
     static UDP_PAYLOAD: [u8; 5] = [0x48, 0x65, 0x6c, 0x6c, 0x6f];

+ 1 - 0
src/iface/interface/tests/mod.rs

@@ -66,6 +66,7 @@ fn test_new_panic() {
 fn test_handle_udp_broadcast(
     #[values(Medium::Ip, Medium::Ethernet, Medium::Ieee802154)] medium: Medium,
 ) {
+    use crate::socket::udp;
     use crate::wire::IpEndpoint;
 
     static UDP_PAYLOAD: [u8; 5] = [0x48, 0x65, 0x6c, 0x6c, 0x6f];

+ 1 - 0
src/iface/interface/tests/sixlowpan.rs

@@ -277,6 +277,7 @@ fn test_echo_request_sixlowpan_128_bytes() {
 #[cfg(feature = "proto-sixlowpan-fragmentation")]
 fn test_sixlowpan_udp_with_fragmentation() {
     use crate::phy::Checksum;
+    use crate::socket::udp;
 
     let mut ieee802154_repr = Ieee802154Repr {
         frame_type: Ieee802154FrameType::Data,

+ 79 - 0
src/iface/interface/udp.rs

@@ -0,0 +1,79 @@
+use super::*;
+
+#[cfg(feature = "socket-dns")]
+use crate::socket::dns::Socket as DnsSocket;
+
+#[cfg(feature = "socket-udp")]
+use crate::socket::udp::Socket as UdpSocket;
+
+impl InterfaceInner {
+    pub(super) fn process_udp<'frame>(
+        &mut self,
+        sockets: &mut SocketSet,
+        meta: PacketMeta,
+        handled_by_raw_socket: bool,
+        ip_repr: IpRepr,
+        ip_payload: &'frame [u8],
+    ) -> Option<Packet<'frame>> {
+        let (src_addr, dst_addr) = (ip_repr.src_addr(), ip_repr.dst_addr());
+        let udp_packet = check!(UdpPacket::new_checked(ip_payload));
+        let udp_repr = check!(UdpRepr::parse(
+            &udp_packet,
+            &src_addr,
+            &dst_addr,
+            &self.caps.checksum
+        ));
+
+        #[cfg(feature = "socket-udp")]
+        for udp_socket in sockets
+            .items_mut()
+            .filter_map(|i| UdpSocket::downcast_mut(&mut i.socket))
+        {
+            if udp_socket.accepts(self, &ip_repr, &udp_repr) {
+                udp_socket.process(self, meta, &ip_repr, &udp_repr, udp_packet.payload());
+                return None;
+            }
+        }
+
+        #[cfg(feature = "socket-dns")]
+        for dns_socket in sockets
+            .items_mut()
+            .filter_map(|i| DnsSocket::downcast_mut(&mut i.socket))
+        {
+            if dns_socket.accepts(&ip_repr, &udp_repr) {
+                dns_socket.process(self, &ip_repr, &udp_repr, udp_packet.payload());
+                return None;
+            }
+        }
+
+        // The packet wasn't handled by a socket, send an ICMP port unreachable packet.
+        match ip_repr {
+            #[cfg(feature = "proto-ipv4")]
+            IpRepr::Ipv4(_) if handled_by_raw_socket => None,
+            #[cfg(feature = "proto-ipv6")]
+            IpRepr::Ipv6(_) if handled_by_raw_socket => None,
+            #[cfg(feature = "proto-ipv4")]
+            IpRepr::Ipv4(ipv4_repr) => {
+                let payload_len =
+                    icmp_reply_payload_len(ip_payload.len(), IPV4_MIN_MTU, ipv4_repr.buffer_len());
+                let icmpv4_reply_repr = Icmpv4Repr::DstUnreachable {
+                    reason: Icmpv4DstUnreachable::PortUnreachable,
+                    header: ipv4_repr,
+                    data: &ip_payload[0..payload_len],
+                };
+                self.icmpv4_reply(ipv4_repr, icmpv4_reply_repr)
+            }
+            #[cfg(feature = "proto-ipv6")]
+            IpRepr::Ipv6(ipv6_repr) => {
+                let payload_len =
+                    icmp_reply_payload_len(ip_payload.len(), IPV6_MIN_MTU, ipv6_repr.buffer_len());
+                let icmpv6_reply_repr = Icmpv6Repr::DstUnreachable {
+                    reason: Icmpv6DstUnreachable::PortUnreachable,
+                    header: ipv6_repr,
+                    data: &ip_payload[0..payload_len],
+                };
+                self.icmpv6_reply(ipv6_repr, icmpv6_reply_repr)
+            }
+        }
+    }
+}