客户端在关闭(SHUT_WR)后将RST发送到收到的数据包

Client sends RST to recieved packets after shutdown(SHUT_WR)

本文关键字:RST 数据包 后将 SHUT WR 客户端      更新时间:2023-10-16

在使用shutdown()关闭套接字连接的写入方向后,所有随后接收的数据都会导致发送RST数据包,并且read()返回大小为0,指示读取方向的EOF。为什么会这样,即使阅读方向没有关闭?

使用wireshark,我检查了发送的数据包:

No. Time     Source        Destination   Protocol Length Info
1   0.000000 192.168.0.175 3.85.154.144  TCP      78     55318 → 80 [SYN] Seq=0 Win=65535 Len=0 MSS=1460 WS=32
2   0.114608 3.85.154.144  192.168.0.175 TCP      74     80 → 55318 [SYN, ACK] Seq=0 Ack=1 Win=26847 Len=0 MSS=1460 WS=256
3   0.114706 192.168.0.175 3.85.154.144  TCP      66     55318 → 80 [ACK] Seq=1 Ack=1 Win=131744 Len=0
4   0.115371 192.168.0.175 3.85.154.144  HTTP     112    GET /bytes/512 HTTP/1.1 
5   0.115401 192.168.0.175 3.85.154.144  TCP      66     55318 → 80 [FIN, ACK] Seq=47 Ack=1 Win=131744 Len=0
6   0.222652 3.85.154.144  192.168.0.175 TCP      66     80 → 55318 [ACK] Seq=1 Ack=47 Win=26880 Len=0
7   0.224444 3.85.154.144  192.168.0.175 HTTP     801    HTTP/1.1 200 OK  (application/octet-stream)
8   0.224543 192.168.0.175 3.85.154.144  TCP      54     55318 → 80 [RST] Seq=48 Win=0 Len=0
9   0.226056 3.85.154.144  192.168.0.175 TCP      66     80 → 55318 [FIN, ACK] Seq=736 Ack=48 Win=26880 Len=0
10  0.226100 192.168.0.175 3.85.154.144  TCP      54     55318 → 80 [RST] Seq=48 Win=0 Len=0

发送 [FIN, ACK] 后,所有接收到的数据都将使用 RST 进行响应。似乎本地端认为连接已完全关闭,即使发送了 FIN(指示写入数据的结束)而不是 RST(指示连接结束)。远程端希望能够发送数据,但不能。 使用 netstat 监控套接字状态会显示,调用shutdown(sockfd, SHUT_WR)后连接会立即完全关闭。

这是C++的MWE。它假定所有不相关的函数都成功,否则中止。检查所有函数的返回代码是否存在错误,以确保它们对结果没有错误。

#include <arpa/inet.h>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define RUN_OR_ABORT(fun) {
int RETVAL = fun;
if (RETVAL == -1)
{
perror(#fun);
abort();
}
}
int main() {
// Establish connection to httpbin.org:80
int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd == -1) exit(EXIT_FAILURE);
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
// "httpbin.org" resolves to 3.85.154.144 or 52.71.234.219
int retcode = inet_pton(addr.sin_family, "3.85.154.144", &addr.sin_addr);
if (retcode != 1) exit(EXIT_FAILURE);
RUN_OR_ABORT(connect(sockfd, (sockaddr*)&addr, sizeof(addr)));
// Request 512 random bytes
const char* msg = "GET /bytes/512 HTTP/1.1rnHost: httpbin.orgrnrn";
ssize_t write_len = write(sockfd, msg, strlen(msg));
if (write_len != strlen(msg)) exit(EXIT_FAILURE);
RUN_OR_ABORT(shutdown(sockfd, SHUT_WR));
// Prepare buffer for recieving data
size_t buf_size = 1024, read_offset = 0;
char* buf = new char[buf_size];
// Read as long as there is data available
while (true) {
ssize_t read_len = read(sockfd, buf + read_offset, buf_size - read_offset);
if (read_len == -1 and (errno & (EAGAIN | EINTR)))
continue;
else if (read_len == -1) {
perror("recv()");
exit(EXIT_FAILURE);
}
read_offset += read_len;
if (read_len == 0) {
// EOF?
printf("%lu bytes have been readn", read_offset);
break;
}
if (read_offset >= buf_size) {
fprintf(stderr, "Unexpectedly large responsen");
exit(EXIT_FAILURE);
}
}
}

我本来希望read()调用返回非零大小,并且在以shutdown(sockfd, SHUT_WR)关闭写入结束后套接字保持打开状态以供读取。MWE的预期产出将是:

740 bytes have been read

(或类似,但读取字节数应大于 512 字节)。 实际输出为:

0 bytes have been read

问题是(行为不端)透明 HTTP 代理。Avast 的 Web Shield 会在发送或接收数据包之前拦截数据包,并检查它们是否存在恶意软件。Avast为此使用单独的内部套接字。发送第一个 FIN 后,Avast (错误地)关闭其内部套接字。这被传达到客户端的套接字,这就是为什么它之后不会显示在 netstat 中。然后,服务器的数据将被拒绝,因为负责处理传入数据的 Avast 套接字不再打开。 Avast 关闭第一个 FIN 数据包上的连接的策略(不告诉服务器;在接收数据之前没有 RST 数据包)有效,因为几乎所有 HTTP 客户端从不调用shutdown(sockfd, SHUT_WR),只有在没有更多数据要读取时才close()连接。