Selaa lähdekoodia

Fix an issue where TCP packets would have zero IP payload length.

whitequark 7 vuotta sitten
vanhempi
commit
7684d50d6e
2 muutettua tiedostoa jossa 15 lisäystä ja 3 poistoa
  1. 4 3
      src/socket/tcp.rs
  2. 11 0
      src/wire/ip.rs

+ 4 - 3
src/socket/tcp.rs

@@ -756,11 +756,10 @@ impl<'a> TcpSocket<'a> {
         (ip_reply_repr, reply_repr)
     }
 
-    pub(crate) fn ack_reply(&self, ip_repr: &IpRepr, repr: &TcpRepr) ->
-                           (IpRepr, TcpRepr<'static>) {
+    fn ack_reply(&self, ip_repr: &IpRepr, repr: &TcpRepr) -> (IpRepr, TcpRepr<'static>) {
         let (ip_reply_repr, mut reply_repr) = Self::reply(ip_repr, repr);
 
-        // From RFC793:
+        // From RFC 793:
         // [...] an empty acknowledgment segment containing the current send-sequence number
         // and an acknowledgment indicating the next sequence number expected
         // to be received.
@@ -1338,6 +1337,7 @@ impl<'a> TcpSocket<'a> {
             repr.max_seg_size = Some(max_segment_size as u16);
         }
 
+        ip_repr.set_payload_len(repr.buffer_len());
         emit((ip_repr, repr))?;
 
         // We've sent something, whether useful data or a keep-alive packet, so rewind
@@ -1481,6 +1481,7 @@ mod test {
             assert_eq!(ip_repr.protocol(), IpProtocol::Tcp);
             assert_eq!(ip_repr.src_addr(), LOCAL_IP);
             assert_eq!(ip_repr.dst_addr(), REMOTE_IP);
+            assert_eq!(ip_repr.payload_len(), tcp_repr.buffer_len());
 
             trace!("recv: {}", tcp_repr);
             Ok(f(Ok(tcp_repr)))

+ 11 - 0
src/wire/ip.rs

@@ -237,6 +237,17 @@ impl IpRepr {
         }
     }
 
+    /// Set the payload length.
+    pub fn set_payload_len(&mut self, length: usize) {
+        match self {
+            &mut IpRepr::Unspecified { ref mut payload_len, .. } =>
+                *payload_len = length,
+            &mut IpRepr::Ipv4(Ipv4Repr { ref mut payload_len, .. }) =>
+                *payload_len = length,
+            &mut IpRepr::__Nonexhaustive => unreachable!()
+        }
+    }
+
     /// Convert an unspecified representation into a concrete one, or return
     /// `Err(Error::Unaddressable)` if not possible.
     ///