Skip to content

Commit 84680f3

Browse files
committed
change: use set_* api for window scale
1 parent 1535aac commit 84680f3

File tree

1 file changed

+49
-31
lines changed

1 file changed

+49
-31
lines changed

src/socket/tcp.rs

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -515,24 +515,6 @@ impl<'a> Socket<'a> {
515515
}
516516
let rx_cap_log2 = mem::size_of::<usize>() * 8 - rx_capacity.leading_zeros() as usize;
517517
let remote_win_shift = rx_cap_log2.saturating_sub(16) as u8;
518-
Self::new_with_window_scaling(rx_buffer, tx_buffer, remote_win_shift)
519-
}
520-
521-
/// Create a socket using the given buffers and window scaling factor defined in [RFC 1323].
522-
///
523-
/// # Panics
524-
///
525-
/// Panics if the window scaling factor is greater than 14.
526-
///
527-
/// See also the [local_recv_win_scale](#method.local_recv_win_scale) method.
528-
pub fn new_with_window_scaling<T>(rx_buffer: T, tx_buffer: T, recv_win_scale: u8) -> Socket<'a>
529-
where
530-
T: Into<SocketBuffer<'a>>,
531-
{
532-
if recv_win_scale > 14 {
533-
panic!("window scaling factor too large, must be <= 14")
534-
}
535-
536518
let (rx_buffer, tx_buffer) = (rx_buffer.into(), tx_buffer.into());
537519

538520
Socket {
@@ -554,7 +536,7 @@ impl<'a> Socket<'a> {
554536
remote_last_ack: None,
555537
remote_last_win: 0,
556538
remote_win_len: 0,
557-
remote_win_shift: recv_win_scale,
539+
remote_win_shift,
558540
remote_win_scale: None,
559541
remote_has_sack: false,
560542
remote_mss: DEFAULT_MSS,
@@ -777,6 +759,28 @@ impl<'a> Socket<'a> {
777759
self.remote_win_shift
778760
}
779761

762+
/// Set the local receive window scaling factor defined in [RFC 1323].
763+
///
764+
/// The value will become constant after the connection is established.
765+
/// It may be reset to 0 during the handshake if remote side does not support window scaling.
766+
///
767+
/// # Errors
768+
/// Returns an error if the socket is not in the `Closed` or `Listen` state, or if the
769+
/// receive buffer is smaller than (1<<scale) bytes.
770+
pub fn set_local_recv_win_scale(&mut self, scale: u8) -> Result<(), ()> {
771+
if self.rx_buffer.capacity() < (1 << scale) as usize {
772+
return Err(());
773+
}
774+
775+
match self.state {
776+
State::Closed | State::Listen => {
777+
self.remote_win_shift = scale;
778+
Ok(())
779+
}
780+
_ => Err(()),
781+
}
782+
}
783+
780784
/// Return the time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
781785
///
782786
/// See also the [set_hop_limit](#method.set_hop_limit) method
@@ -7802,23 +7806,37 @@ mod test {
78027806
}
78037807

78047808
#[test]
7805-
#[should_panic]
7806-
fn test_new_with_too_large_window_scale() {
7807-
Socket::new_with_window_scaling(
7808-
SocketBuffer::new(Vec::with_capacity(128)),
7809-
SocketBuffer::new(Vec::with_capacity(128)),
7810-
15,
7809+
fn test_too_large_window_scale() {
7810+
let mut socket = Socket::new(
7811+
SocketBuffer::new(vec![0; 128]),
7812+
SocketBuffer::new(vec![0; 128]),
78117813
);
7814+
assert!(socket.set_local_recv_win_scale(15).is_err())
78127815
}
78137816

78147817
#[test]
7815-
fn test_new_with_window_scale() {
7816-
let socket = Socket::new_with_window_scaling(
7817-
SocketBuffer::new(Vec::with_capacity(128)),
7818-
SocketBuffer::new(Vec::with_capacity(128)),
7819-
14,
7818+
fn test_set_window_scale() {
7819+
let mut socket = Socket::new(
7820+
SocketBuffer::new(vec![0; 128]),
7821+
SocketBuffer::new(vec![0; 128]),
78207822
);
7821-
assert_eq!(socket.local_recv_win_scale(), 14);
7823+
assert!(matches!(socket.state, State::Closed));
7824+
assert_eq!(socket.rx_buffer.capacity(), 128);
7825+
assert!(socket.set_local_recv_win_scale(6).is_ok());
7826+
assert!(socket.set_local_recv_win_scale(14).is_err());
7827+
assert_eq!(socket.local_recv_win_scale(), 6);
7828+
}
7829+
7830+
#[test]
7831+
fn test_set_scale_with_tcp_state() {
7832+
let mut socket = socket();
7833+
assert!(socket.set_local_recv_win_scale(1).is_ok());
7834+
let mut socket = socket_established();
7835+
assert!(socket.set_local_recv_win_scale(1).is_err());
7836+
let mut socket = socket_listen();
7837+
assert!(socket.set_local_recv_win_scale(1).is_ok());
7838+
let mut socket = socket_syn_received();
7839+
assert!(socket.set_local_recv_win_scale(1).is_err());
78227840
}
78237841

78247842
#[test]

0 commit comments

Comments
 (0)