TCP断开连接时两端都进入TIMEWAIT状态

可紊 提交于 2021-01-07 09:35:33

TCP连接断开时,先挥手(发FIN)的一方会不出意外的话最终会进入TIMEWAIT状态。

那有没有可能两边都同时挥手(发FIN)然后都同时进入TIMEWAIT状态呢?

有。

下面我们就来模拟这种情况,我在写了两个python脚本,s.py 和 c.py 模拟通信的两端,在ubuntu上运行。

思路是,两端握手成功后,通过配置一条iptable规则把单方向的包DROP掉,模拟丢包的情况。

这样两端并不一定要在同时时间点发FIN,只要各自认为自己先发的FIN就可以模拟出两边TIMEWAIT了。

DROP包的iptable规则在2秒后失效,两边通信恢复,挥手过程完成。

s.py

import os, time
from socket import *
s = socket(AF_INET, SOCK_STREAM)
s.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
s.bind(('0.0.0.0', 9000))
s.listen(1)
c, _ = s.accept()
os.system('iptables -I INPUT -i lo -d 127.0.0.2 -j DROP')
time.sleep(2)
c.close()
os.system('iptables -D INPUT -i lo -d 127.0.0.2 -j DROP')

c.py

import time
from socket import *
s = socket(AF_INET, SOCK_STREAM)
s.connect(('127.0.0.2', 9000))
time.sleep(1)
s.close()

实验时先后运行s.py和c.py,然后查看系统状态

# ss -t state time-wait state close
State       Recv-Q Send-Q                                   Local Address:Port                                       Peer Address:Port
TIME-WAIT   0      0                                            127.0.0.2:9000                                          127.0.0.1:44838
TIME-WAIT   0      0                                            127.0.0.1:44838                                         127.0.0.2:9000

整个过程抓包的情况

# tcpdump -i any -nn port 9000
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
18:14:50.841479 IP 127.0.0.1.44838 > 127.0.0.2.9000: Flags [S], seq 3357650139, win 43690, options [mss 65495,sackOK,TS val 4294934045 ecr 0,nop,wscale 9], length 0
18:14:50.841489 IP 127.0.0.2.9000 > 127.0.0.1.44838: Flags [S.], seq 1485178514, ack 3357650140, win 43690, options [mss 65495,sackOK,TS val 4294934045 ecr 4294934045,nop,wscale 9], length 0
18:14:50.841499 IP 127.0.0.1.44838 > 127.0.0.2.9000: Flags [.], ack 1, win 86, options [nop,nop,TS val 4294934045 ecr 4294934045], length 0
18:14:51.844900 IP 127.0.0.1.44838 > 127.0.0.2.9000: Flags [F.], seq 1, ack 1, win 86, options [nop,nop,TS val 4294934296 ecr 4294934045], length 0
18:14:52.047343 IP 127.0.0.1.44838 > 127.0.0.2.9000: Flags [F.], seq 1, ack 1, win 86, options [nop,nop,TS val 4294934347 ecr 4294934045], length 0
18:14:52.251289 IP 127.0.0.1.44838 > 127.0.0.2.9000: Flags [F.], seq 1, ack 1, win 86, options [nop,nop,TS val 4294934398 ecr 4294934045], length 0
18:14:52.658130 IP 127.0.0.1.44838 > 127.0.0.2.9000: Flags [F.], seq 1, ack 1, win 86, options [nop,nop,TS val 4294934500 ecr 4294934045], length 0
18:14:52.845537 IP 127.0.0.2.9000 > 127.0.0.1.44838: Flags [F.], seq 1, ack 1, win 86, options [nop,nop,TS val 4294934546 ecr 4294934045], length 0
18:14:52.845553 IP 127.0.0.1.44838 > 127.0.0.2.9000: Flags [.], ack 2, win 86, options [nop,nop,TS val 4294934546 ecr 4294934546], length 0
18:14:53.042562 IP 127.0.0.2.9000 > 127.0.0.1.44838: Flags [F.], seq 1, ack 1, win 86, options [nop,nop,TS val 4294934596 ecr 4294934045], length 0
18:14:53.042578 IP 127.0.0.1.44838 > 127.0.0.2.9000: Flags [.], ack 2, win 86, options [nop,nop,TS val 4294934596 ecr 4294934596,nop,nop,sack 1 {1:2}], length 0
18:14:53.475691 IP 127.0.0.1.44838 > 127.0.0.2.9000: Flags [F.], seq 1, ack 2, win 86, options [nop,nop,TS val 4294934704 ecr 4294934596], length 0
18:14:53.475715 IP 127.0.0.2.9000 > 127.0.0.1.44838: Flags [.], ack 2, win 86, options [nop,nop,TS val 4294934704 ecr 4294934704], length 0

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!