소스 검색

wire/udp: make `emit_header` not able to calculate checksum.

Currently it's not working properly, because you can call it when the UDP payload is not written yet to the buffer.
I've changed it to not be able to, and clarified in docs that it's for internal-packet-use only.

It's only used for 6lowpan decompression, and in that case we don't want to calculate and then later check the checksum.
Dario Nieuwenhuis 2 년 전
부모
커밋
71438338ee
2개의 변경된 파일9개의 추가작업 그리고 24개의 파일을 삭제
  1. 2 8
      src/iface/interface/sixlowpan.rs
  2. 7 16
      src/wire/udp.rs

+ 2 - 8
src/iface/interface/sixlowpan.rs

@@ -250,17 +250,11 @@ impl<'a> InterfaceInner<'a> {
                             &ChecksumCapabilities::ignored(),
                         )?;
 
-                        let mut udp_emit_packet = UdpPacket::new_unchecked(
+                        let mut udp = UdpPacket::new_unchecked(
                             &mut buffer[..udp_repr.0.header_len() + iphc.payload().len()
                                 - udp_repr.header_len()],
                         );
-                        udp_repr.0.emit_header(
-                            &mut udp_emit_packet,
-                            &iphc_repr.src_addr.into(),
-                            &iphc_repr.dst_addr.into(),
-                            ipv6_repr.payload_len - 8,
-                            &ChecksumCapabilities::ignored(),
-                        );
+                        udp_repr.0.emit_header(&mut udp, ipv6_repr.payload_len - 8);
 
                         buffer[8..].copy_from_slice(&iphc.payload()[udp_repr.header_len()..]);
                     }

+ 7 - 16
src/wire/udp.rs

@@ -251,27 +251,18 @@ impl Repr {
     }
 
     /// Emit a high-level representation into an User Datagram Protocol packet.
-    pub fn emit_header<T: ?Sized>(
-        &self,
-        packet: &mut Packet<&mut T>,
-        src_addr: &IpAddress,
-        dst_addr: &IpAddress,
-        payload_len: usize,
-        checksum_caps: &ChecksumCapabilities,
-    ) where
+    ///
+    /// This never calculates the checksum, and is intended for internal-use only,
+    /// not for packets that are going to be actually sent over the network. For
+    /// example, when decompressing 6lowpan.
+    pub(crate) fn emit_header<T: ?Sized>(&self, packet: &mut Packet<&mut T>, payload_len: usize)
+    where
         T: AsRef<[u8]> + AsMut<[u8]>,
     {
         packet.set_src_port(self.src_port);
         packet.set_dst_port(self.dst_port);
         packet.set_len((HEADER_LEN + payload_len) as u16);
-
-        if checksum_caps.udp.tx() {
-            packet.fill_checksum(src_addr, dst_addr)
-        } else {
-            // make sure we get a consistently zeroed checksum,
-            // since implementations might rely on it
-            packet.set_checksum(0);
-        }
+        packet.set_checksum(0);
     }
 
     /// Emit a high-level representation into an User Datagram Protocol packet.