You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm writing some unit test for my personal project, so running into the issue that some unusual situation like retransmission, duplicated ack happened.
Code to trigger that problem
use log::{debug, error, info};
use smoltcp::iface::{Config, Interface, SocketSet};
use smoltcp::phy::{Loopback, Medium, PcapMode, PcapWriter};
use smoltcp::socket::tcp;
use smoltcp::time::{Duration, Instant};
use smoltcp::wire::{IpAddress, IpCidr};
use std::fs::File;
use std::thread::sleep;
fn main() {
env_logger::init();
let loopback = Loopback::new(Medium::Ip);
let mut device = PcapWriter::new(
loopback,
File::create_new("dump.pcap").unwrap(),
PcapMode::TxOnly,
);
// Create interface
let config = Config::new(smoltcp::wire::HardwareAddress::Ip);
let mut iface = Interface::new(config, &mut device, Instant::now());
iface.update_ip_addrs(|ip_addrs| {
ip_addrs
.push(IpCidr::new(IpAddress::v4(127, 0, 0, 1), 8))
.unwrap();
});
// Create sockets
let server_socket = tcp::Socket::new(
tcp::SocketBuffer::new(vec![0u8; 65535]),
tcp::SocketBuffer::new(vec![0u8; 65535]),
);
let client_socket = tcp::Socket::new(
tcp::SocketBuffer::new(vec![0u8; 65535]),
tcp::SocketBuffer::new(vec![0u8; 65535]),
);
let mut sockets: [_; 2] = Default::default();
let mut sockets = SocketSet::new(&mut sockets[..]);
let server_handle = sockets.add(server_socket);
let client_handle = sockets.add(client_socket);
let mut sent = false;
let mut received = 0usize;
loop {
iface.poll(Instant::now(), &mut device, &mut sockets);
let socket = sockets.get_mut::<tcp::Socket>(server_handle);
if !socket.is_active() && !socket.is_listening() {
socket.listen(1234).unwrap();
}
if socket.can_recv() {
match socket.recv(|buffer| (buffer.len(), buffer.len())) {
Ok(size) => {
received += size;
}
Err(e) => error!("{:?}", e),
}
debug!(
"got {}/{} bytes, left {}",
received,
65535,
socket.recv_queue()
);
if received == 65535 {
socket.close();
}
}
let socket = sockets.get_mut::<tcp::Socket>(client_handle);
if !socket.is_active() {
socket
.connect(iface.context(), (IpAddress::v4(127, 0, 0, 1), 1234), 65000)
.unwrap();
}
if !sent && socket.can_send() {
debug!("sending");
socket.send_slice(&vec![0u8; 65535]).unwrap();
sent = true;
}
match iface.poll_delay(Instant::now(), &sockets) {
Some(Duration::ZERO) => debug!("resuming"),
Some(delay) => {
debug!("sleeping for {}", delay);
sleep(delay.into());
}
None => {
if sent {
break;
}
}
}
}
info!("done")
}
Once run, it yields these log:
[2025-07-16T16:37:49Z DEBUG test1] resuming
[2025-07-16T16:37:49Z DEBUG test1] sleeping for 0.699s
[2025-07-16T16:37:50Z DEBUG smoltcp::socket::tcp] retransmitting at t+0.700s��������������������������������[2025-07-16T16:37:50Z DEBUG test1] sleeping for 0.699s
[2025-07-16T16:37:50Z DEBUG smoltcp::socket::tcp] expecting an ACK
[2025-07-16T16:37:50Z DEBUG smoltcp::socket::tcp] retransmitting at t+0.700s
[2025-07-16T16:37:50Z DEBUG test1] sending
[2025-07-16T16:37:50Z DEBUG test1] resuming
[2025-07-16T16:37:50Z DEBUG smoltcp::socket::tcp] received a keep-alive or window probe packet, will send an ACK
[2025-07-16T16:37:50Z DEBUG smoltcp::socket::tcp] sending sACK option with current assembler ranges
[2025-07-16T16:37:50Z DEBUG test1] sleeping for 0.699s
[2025-07-16T16:37:51Z DEBUG smoltcp::socket::tcp] retransmitting at t+0.700s
[2025-07-16T16:37:51Z DEBUG test1] got 65495/65535 bytes, left 0
[2025-07-16T16:37:51Z DEBUG test1] resuming
[2025-07-16T16:37:51Z DEBUG smoltcp::socket::tcp] segment not in receive window (2000063569..2000129064 not intersecting 2000129064..2000194599), will send challenge ACK
[2025-07-16T16:37:51Z DEBUG smoltcp::socket::tcp] sending sACK option with current assembler ranges
[2025-07-16T16:37:51Z DEBUG test1] sleeping for 0.699s
[2025-07-16T16:37:52Z DEBUG smoltcp::socket::tcp] retransmitting at t+0.700s
[2025-07-16T16:37:52Z DEBUG test1] got 65535/65535 bytes, left 0
[2025-07-16T16:37:52Z DEBUG test1] resuming
[2025-07-16T16:37:52Z DEBUG smoltcp::socket::tcp] segment not in receive window (2000129064..2000129104 not intersecting 2000129104..2000194639), will send challenge ACK
[2025-07-16T16:37:52Z DEBUG test1] sleeping for 0.699s
[2025-07-16T16:37:53Z DEBUG smoltcp::socket::tcp] retransmitting at t+0.700s
[2025-07-16T16:37:53Z DEBUG test1] sleeping for 0.699s
[2025-07-16T16:37:53Z DEBUG smoltcp::socket::tcp] received a keep-alive or window probe packet, will send an ACK
[2025-07-16T16:37:53Z DEBUG smoltcp::socket::tcp] sending sACK option with current assembler ranges
[2025-07-16T16:37:53Z INFO test1] done
Why this is happening? Am I doing something wrong?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I'm writing some unit test for my personal project, so running into the issue that some unusual situation like retransmission, duplicated ack happened.
Code to trigger that problem
Once run, it yields these log:
Why this is happening? Am I doing something wrong?
Beta Was this translation helpful? Give feedback.
All reactions