fix(fake_tcp) fixed incorrect RST SEQ/ACK

This commit is contained in:
Datong Sun 2021-09-16 22:17:47 -07:00 committed by Datong Sun
parent 9251775856
commit 44b7c0f55e
2 changed files with 11 additions and 5 deletions

View File

@ -64,8 +64,8 @@ async fn main() {
info!("Created TUN device {}", tun.name()); info!("Created TUN device {}", tun.name());
let udp_sock = Arc::new(UdpSocket::bind(local_addr).await.unwrap()); let udp_sock = Arc::new(UdpSocket::bind(local_addr).await.unwrap());
let connections = Mutex::new(LruCache::<SocketAddrV4, Arc<Socket>>::with_expiry_duration( let connections = Arc::new(Mutex::new(
UDP_TTL, LruCache::<SocketAddrV4, Arc<Socket>>::with_expiry_duration(UDP_TTL),
)); ));
let mut stack = Stack::new(tun); let mut stack = Stack::new(tun);
@ -96,8 +96,10 @@ async fn main() {
} }
assert!(connections.lock().await.insert(addr, sock.clone()).is_none()); assert!(connections.lock().await.insert(addr, sock.clone()).is_none());
debug!("inserted fake TCP socket into LruCache");
let udp_sock = udp_sock.clone(); let udp_sock = udp_sock.clone();
let connections = connections.clone();
tokio::spawn(async move { tokio::spawn(async move {
loop { loop {
let mut buf_r = [0u8; MAX_PACKET_LEN]; let mut buf_r = [0u8; MAX_PACKET_LEN];
@ -105,7 +107,11 @@ async fn main() {
Some(size) => { Some(size) => {
udp_sock.send_to(&buf_r[..size], addr).await.unwrap(); udp_sock.send_to(&buf_r[..size], addr).await.unwrap();
}, },
None => { return; }, None => {
connections.lock().await.remove(&addr);
debug!("removed fake TCP socket from LruCache");
return;
},
} }
} }
}); });

View File

@ -399,7 +399,7 @@ impl Stack {
local_addr, local_addr,
remote_addr, remote_addr,
0, 0,
tcp_packet.get_acknowledgement() + 1, tcp_packet.get_sequence() + 1,
tcp::TcpFlags::RST, tcp::TcpFlags::RST,
None, None,
); );
@ -410,8 +410,8 @@ impl Stack {
let buf = build_tcp_packet( let buf = build_tcp_packet(
local_addr, local_addr,
remote_addr, remote_addr,
tcp_packet.get_acknowledgement(),
0, 0,
tcp_packet.get_acknowledgement() + 1,
tcp::TcpFlags::RST, tcp::TcpFlags::RST,
None, None,
); );