Skip to content

Commit 79df0a7

Browse files
committed
refactor: remove redundant IpMtu option, use TCB MTU value instead.
1 parent 78ffed7 commit 79df0a7

1 file changed

Lines changed: 14 additions & 19 deletions

File tree

src/stream/tcp.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ pub struct TcpConfig {
5353
pub enum TcpOptions {
5454
/// Maximum segment size (MSS) for TCP connections. Default is 1460 bytes.
5555
MaximumSegmentSize(u16),
56-
57-
/// MTU option,helps to set the MSS (Maximum Segment Size) option in the TCP header.
58-
IpMtu(u16),
5956
}
6057

6158
impl Default for TcpConfig {
@@ -884,6 +881,7 @@ pub(crate) fn write_packet_to_device(
884881
seq,
885882
ack,
886883
window_size,
884+
tcb.get_mtu(),
887885
payload.unwrap_or_default(),
888886
options,
889887
)?;
@@ -902,6 +900,7 @@ pub(crate) fn create_raw_packet(
902900
seq: u32,
903901
ack: u32,
904902
win: u16,
903+
mtu: u16,
905904
mut payload: Vec<u8>,
906905
options: Option<&Vec<TcpOptions>>,
907906
) -> std::io::Result<NetworkPacket> {
@@ -912,28 +911,24 @@ pub(crate) fn create_raw_packet(
912911
tcp_header.rst = flags & RST != 0;
913912
tcp_header.fin = flags & FIN != 0;
914913
tcp_header.psh = flags & PSH != 0;
915-
914+
let mut actual_mss = if src_addr.is_ipv6() || dst_addr.is_ipv6() {
915+
mtu - 40 - TCP_HEADER_LEN // IPv6 header is 40 bytes
916+
} else {
917+
mtu - 20 - TCP_HEADER_LEN
918+
};
919+
let mut tcp_options = Vec::new();
916920
if let Some(opts) = options {
917-
let mut tcp_options = Vec::new();
918921
for opt in opts {
919922
match opt {
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-
}
923+
TcpOptions::MaximumSegmentSize(mss) => actual_mss = *mss,
931924
}
932925
}
933-
tcp_header
934-
.set_options(&tcp_options)
935-
.map_err(|e| std::io::Error::new(InvalidInput, e))?;
936926
}
927+
tcp_options.push(etherparse::TcpOptionElement::MaximumSegmentSize(actual_mss));
928+
tcp_header
929+
.set_options(&tcp_options)
930+
.map_err(|e| std::io::Error::new(InvalidInput, e))?;
931+
937932
let ip_header = match (src_addr.ip(), dst_addr.ip()) {
938933
(std::net::IpAddr::V4(src), std::net::IpAddr::V4(dst)) => {
939934
let mut ip_h =

0 commit comments

Comments
 (0)