@@ -1796,7 +1796,6 @@ impl<'a> Socket<'a> {
1796
1796
// ACK packets in the SYN-RECEIVED state change it to ESTABLISHED.
1797
1797
( State :: SynReceived , TcpControl :: None ) => {
1798
1798
self . set_state ( State :: Established ) ;
1799
- self . timer . set_for_idle ( cx. now ( ) , self . keep_alive ) ;
1800
1799
}
1801
1800
1802
1801
// FIN packets in the SYN-RECEIVED state change it to CLOSE-WAIT.
@@ -1806,7 +1805,6 @@ impl<'a> Socket<'a> {
1806
1805
self . remote_seq_no += 1 ;
1807
1806
self . rx_fin_received = true ;
1808
1807
self . set_state ( State :: CloseWait ) ;
1809
- self . timer . set_for_idle ( cx. now ( ) , self . keep_alive ) ;
1810
1808
}
1811
1809
1812
1810
// SYN|ACK packets in the SYN-SENT state change it to ESTABLISHED.
@@ -1847,26 +1845,15 @@ impl<'a> Socket<'a> {
1847
1845
} else {
1848
1846
self . set_state ( State :: SynReceived ) ;
1849
1847
}
1850
- self . timer . set_for_idle ( cx. now ( ) , self . keep_alive ) ;
1851
1848
}
1852
1849
1853
- // RFC 6298: (5.2) ACK of all outstanding data turn off the retransmit timer.
1854
- // (5.3) ACK of new data in ESTABLISHED state restart the retransmit timer.
1855
- ( State :: Established , TcpControl :: None ) => {
1856
- if ack_all {
1857
- self . timer . set_for_idle ( cx. now ( ) , self . keep_alive ) ;
1858
- } else if ack_len > 0 {
1859
- self . timer
1860
- . set_for_retransmit ( cx. now ( ) , self . rtte . retransmission_timeout ( ) ) ;
1861
- }
1862
- }
1850
+ ( State :: Established , TcpControl :: None ) => { }
1863
1851
1864
1852
// FIN packets in ESTABLISHED state indicate the remote side has closed.
1865
1853
( State :: Established , TcpControl :: Fin ) => {
1866
1854
self . remote_seq_no += 1 ;
1867
1855
self . rx_fin_received = true ;
1868
1856
self . set_state ( State :: CloseWait ) ;
1869
- self . timer . set_for_idle ( cx. now ( ) , self . keep_alive ) ;
1870
1857
}
1871
1858
1872
1859
// ACK packets in FIN-WAIT-1 state change it to FIN-WAIT-2, if we've already
@@ -1875,9 +1862,6 @@ impl<'a> Socket<'a> {
1875
1862
if ack_of_fin {
1876
1863
self . set_state ( State :: FinWait2 ) ;
1877
1864
}
1878
- if ack_all {
1879
- self . timer . set_for_idle ( cx. now ( ) , self . keep_alive ) ;
1880
- }
1881
1865
}
1882
1866
1883
1867
// FIN packets in FIN-WAIT-1 state change it to CLOSING, or to TIME-WAIT
@@ -1890,14 +1874,10 @@ impl<'a> Socket<'a> {
1890
1874
self . timer . set_for_close ( cx. now ( ) ) ;
1891
1875
} else {
1892
1876
self . set_state ( State :: Closing ) ;
1893
- self . timer . set_for_idle ( cx. now ( ) , self . keep_alive ) ;
1894
1877
}
1895
1878
}
1896
1879
1897
- // Data packets in FIN-WAIT-2 reset the idle timer.
1898
- ( State :: FinWait2 , TcpControl :: None ) => {
1899
- self . timer . set_for_idle ( cx. now ( ) , self . keep_alive ) ;
1900
- }
1880
+ ( State :: FinWait2 , TcpControl :: None ) => { }
1901
1881
1902
1882
// FIN packets in FIN-WAIT-2 state change it to TIME-WAIT.
1903
1883
( State :: FinWait2 , TcpControl :: Fin ) => {
@@ -1912,24 +1892,17 @@ impl<'a> Socket<'a> {
1912
1892
if ack_of_fin {
1913
1893
self . set_state ( State :: TimeWait ) ;
1914
1894
self . timer . set_for_close ( cx. now ( ) ) ;
1915
- } else {
1916
- self . timer . set_for_idle ( cx. now ( ) , self . keep_alive ) ;
1917
1895
}
1918
1896
}
1919
1897
1920
- // ACK packets in CLOSE-WAIT state reset the retransmit timer.
1921
- ( State :: CloseWait , TcpControl :: None ) => {
1922
- self . timer . set_for_idle ( cx. now ( ) , self . keep_alive ) ;
1923
- }
1898
+ ( State :: CloseWait , TcpControl :: None ) => { }
1924
1899
1925
1900
// ACK packets in LAST-ACK state change it to CLOSED.
1926
1901
( State :: LastAck , TcpControl :: None ) => {
1927
1902
if ack_of_fin {
1928
1903
// Clear the remote endpoint, or we'll send an RST there.
1929
1904
self . set_state ( State :: Closed ) ;
1930
1905
self . tuple = None ;
1931
- } else {
1932
- self . timer . set_for_idle ( cx. now ( ) , self . keep_alive ) ;
1933
1906
}
1934
1907
}
1935
1908
@@ -2040,6 +2013,25 @@ impl<'a> Socket<'a> {
2040
2013
self . last_remote_tsval = timestamp. tsval ;
2041
2014
}
2042
2015
2016
+ // update timers.
2017
+ match self . timer {
2018
+ Timer :: Retransmit { .. } | Timer :: FastRetransmit => {
2019
+ if ack_all {
2020
+ // RFC 6298: (5.2) ACK of all outstanding data turn off the retransmit timer.
2021
+ self . timer . set_for_idle ( cx. now ( ) , self . keep_alive ) ;
2022
+ } else if ack_len > 0 {
2023
+ // (5.3) ACK of new data in ESTABLISHED state restart the retransmit timer.
2024
+ let rto = self . rtte . retransmission_timeout ( ) ;
2025
+ self . timer . set_for_retransmit ( cx. now ( ) , rto) ;
2026
+ }
2027
+ }
2028
+ Timer :: Idle { .. } => {
2029
+ // any packet on idle refresh the keepalive timer.
2030
+ self . timer . set_for_idle ( cx. now ( ) , self . keep_alive ) ;
2031
+ }
2032
+ _ => { }
2033
+ }
2034
+
2043
2035
let payload_len = payload. len ( ) ;
2044
2036
if payload_len == 0 {
2045
2037
return None ;
@@ -2537,12 +2529,12 @@ impl<'a> Socket<'a> {
2537
2529
. post_transmit ( cx. now ( ) , repr. segment_len ( ) ) ;
2538
2530
}
2539
2531
2540
- if ! self . seq_to_transmit ( cx ) && repr. segment_len ( ) > 0 && !self . timer . is_retransmit ( ) {
2541
- // RFC 6298: (5.1) If we've transmitted all data we could (and there was
2542
- // something at all, data or flag, to transmit, not just an ACK) , start the
2543
- // retransmit timer if it is not already running .
2544
- self . timer
2545
- . set_for_retransmit ( cx. now ( ) , self . rtte . retransmission_timeout ( ) ) ;
2532
+ if repr. segment_len ( ) > 0 && !self . timer . is_retransmit ( ) {
2533
+ // RFC 6298 (5.1) Every time a packet containing data is sent (including a
2534
+ // retransmission), if the timer is not running , start it running
2535
+ // so that it will expire after RTO seconds .
2536
+ let rto = self . rtte . retransmission_timeout ( ) ;
2537
+ self . timer . set_for_retransmit ( cx. now ( ) , rto ) ;
2546
2538
}
2547
2539
2548
2540
if self . state == State :: Closed {
@@ -2812,7 +2804,7 @@ mod test {
2812
2804
Ok ( ( ) )
2813
2805
} ) ;
2814
2806
if fail {
2815
- panic ! ( "Should not send a packet" )
2807
+ panic ! ( "Should not send a packet" )
2816
2808
}
2817
2809
2818
2810
assert_eq ! ( result, Ok ( ( ) ) )
@@ -2955,6 +2947,9 @@ mod test {
2955
2947
s. state = State :: Closing ;
2956
2948
s. remote_last_seq = LOCAL_SEQ + 1 + 1 ;
2957
2949
s. remote_seq_no = REMOTE_SEQ + 1 + 1 ;
2950
+ s. timer = Timer :: Retransmit {
2951
+ expires_at : Instant :: from_millis_const ( 1000 ) ,
2952
+ } ;
2958
2953
s
2959
2954
}
2960
2955
@@ -6529,6 +6524,85 @@ mod test {
6529
6524
recv_nothing ! ( s, time 5000 ) ;
6530
6525
}
6531
6526
6527
+ #[ test]
6528
+ fn test_retransmit_fin ( ) {
6529
+ let mut s = socket_established ( ) ;
6530
+ s. close ( ) ;
6531
+ recv ! ( s, time 0 , Ok ( TcpRepr {
6532
+ control: TcpControl :: Fin ,
6533
+ seq_number: LOCAL_SEQ + 1 ,
6534
+ ack_number: Some ( REMOTE_SEQ + 1 ) ,
6535
+ ..RECV_TEMPL
6536
+ } ) ) ;
6537
+
6538
+ recv_nothing ! ( s, time 999 ) ;
6539
+ recv ! ( s, time 1000 , Ok ( TcpRepr {
6540
+ control: TcpControl :: Fin ,
6541
+ seq_number: LOCAL_SEQ + 1 ,
6542
+ ack_number: Some ( REMOTE_SEQ + 1 ) ,
6543
+ ..RECV_TEMPL
6544
+ } ) ) ;
6545
+ }
6546
+
6547
+ #[ test]
6548
+ fn test_retransmit_fin_wait ( ) {
6549
+ let mut s = socket_fin_wait_1 ( ) ;
6550
+ // we send FIN
6551
+ recv ! (
6552
+ s,
6553
+ [ TcpRepr {
6554
+ control: TcpControl :: Fin ,
6555
+ seq_number: LOCAL_SEQ + 1 ,
6556
+ ack_number: Some ( REMOTE_SEQ + 1 ) ,
6557
+ ..RECV_TEMPL
6558
+ } ]
6559
+ ) ;
6560
+ // remote also sends FIN, does NOT ack ours.
6561
+ send ! (
6562
+ s,
6563
+ TcpRepr {
6564
+ control: TcpControl :: Fin ,
6565
+ seq_number: REMOTE_SEQ + 1 ,
6566
+ ack_number: Some ( LOCAL_SEQ + 1 ) ,
6567
+ ..SEND_TEMPL
6568
+ }
6569
+ ) ;
6570
+ // we ack it
6571
+ recv ! (
6572
+ s,
6573
+ [ TcpRepr {
6574
+ control: TcpControl :: None ,
6575
+ seq_number: LOCAL_SEQ + 2 ,
6576
+ ack_number: Some ( REMOTE_SEQ + 2 ) ,
6577
+ ..RECV_TEMPL
6578
+ } ]
6579
+ ) ;
6580
+
6581
+ // we haven't got an ACK for our FIN, we should retransmit.
6582
+ recv_nothing ! ( s, time 999 ) ;
6583
+ recv ! (
6584
+ s,
6585
+ time 1000 ,
6586
+ [ TcpRepr {
6587
+ control: TcpControl :: Fin ,
6588
+ seq_number: LOCAL_SEQ + 1 ,
6589
+ ack_number: Some ( REMOTE_SEQ + 2 ) ,
6590
+ ..RECV_TEMPL
6591
+ } ]
6592
+ ) ;
6593
+ recv_nothing ! ( s, time 2999 ) ;
6594
+ recv ! (
6595
+ s,
6596
+ time 3000 ,
6597
+ [ TcpRepr {
6598
+ control: TcpControl :: Fin ,
6599
+ seq_number: LOCAL_SEQ + 1 ,
6600
+ ack_number: Some ( REMOTE_SEQ + 2 ) ,
6601
+ ..RECV_TEMPL
6602
+ } ]
6603
+ ) ;
6604
+ }
6605
+
6532
6606
// =========================================================================================//
6533
6607
// Tests for window management.
6534
6608
// =========================================================================================//
0 commit comments