Skip to content

Commit 78ffed7

Browse files
committed
feat: add mtu option for dynamic calculation of mss
1 parent e240ff1 commit 78ffed7

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

src/stream/tcp.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const CLOSE_WAIT_TIMEOUT: Duration = Duration::from_secs(5);
2828
const LAST_ACK_MAX_RETRIES: usize = 3;
2929
const LAST_ACK_TIMEOUT: Duration = Duration::from_millis(500);
3030
const TIMEOUT: Duration = Duration::from_secs(60);
31+
const TCP_HEADER_LEN: u16 = 20;
3132

3233
#[non_exhaustive]
3334
#[derive(Debug, Clone)]
@@ -52,6 +53,9 @@ pub struct TcpConfig {
5253
pub enum TcpOptions {
5354
/// Maximum segment size (MSS) for TCP connections. Default is 1460 bytes.
5455
MaximumSegmentSize(u16),
56+
57+
/// MTU option,helps to set the MSS (Maximum Segment Size) option in the TCP header.
58+
IpMtu(u16),
5559
}
5660

5761
impl Default for TcpConfig {
@@ -913,7 +917,17 @@ pub(crate) fn create_raw_packet(
913917
let mut tcp_options = Vec::new();
914918
for opt in opts {
915919
match opt {
916-
TcpOptions::MaximumSegmentSize(mss) => tcp_options.push(TcpOptionElement::MaximumSegmentSize(*mss)),
920+
TcpOptions::MaximumSegmentSize(mss) => tcp_options.push(etherparse::TcpOptionElement::MaximumSegmentSize(*mss)),
921+
TcpOptions::IpMtu(mtu) => {
922+
tcp_options.push(etherparse::TcpOptionElement::MaximumSegmentSize(
923+
mtu - TCP_HEADER_LEN
924+
- if src_addr.is_ipv6() || dst_addr.is_ipv6() {
925+
40
926+
} else {
927+
20 // minimum IPv4 header size
928+
},
929+
));
930+
}
917931
}
918932
}
919933
tcp_header

0 commit comments

Comments
 (0)