TCP报文格式

简介

TCP 是传输层协议,为应用程序提供了稳定可靠的数据传输。它是一个滑动窗口协议,提供了超时和重传的处理。
TCP 是在两个端点之间建立的全双工虚拟连接。每个端点由 IP 地址和端口号定义。
数据以字节流的形式传输,字节流按段传输。窗口大小决定了在需要接收方确认之前可以发送的数据字节数。

TCP/IP 协议的模型

tcp-ip-model
首先,我们 TCP/IP 协议栈是一个三层模型的结构,包括传输层、网络层、数据链路层。在 TCP/IP 之上的是应用层,比较常用的就是 HTTP 协议。
在图中,没有被客户端和服务端圈中的方格,代表的是数据报。
其中,粉色的部分是当前协议的请求头,绿色的部分对当前协议而言就是数据部分。
举例说明:
以 HTTP 协议为例,客户端发送数据时,先封装一层 HTTP Header + HTTP Data,然后交给传输层。
传输层协议把应用层传递过来的数据整体当做 Data,再加上了 TCP Header,封装成数据报,然后交给网络层。
网络层再在传输层数据的基础上,加上 IP Header,封装后交给数据链路层。
数据链路层,将数据加上 MAC Header 之后,从客户端传送给服务端。
服务端接收到数据后,一层层剥离 Header 并且根据 Header 中的协议内容处理后,再将数据传递给上层进行操作。
数据传递到应用层后,由 HTTP 协议解析完成后,处理 Request 请求,并且生成相应的 Response,然后再反过来传递给客户端。
最终客户端(浏览器)中就会展示相应的网页。

TCP 报文格式

二进制位报文格式图:

tcp-header
Source Port. 16 bits. 发送端端口号
Destination Port. 16 bits. 接收端(目的地端)端口号
Sequence Number. 32 bits.
The sequence number of the first data byte in this segment. If the SYN bit is set, the sequence number is the initial sequence number and the first data byte is initial sequence number + 1.
序列号,当前段的第一个字节的序列号。如果设置了 SYN 同步标记(即 SYN=1),序列号的值是初始序列号。带 SYN 同步标记的段的第一个字节的序列号=初始序列号+1。
初始序列号(ISN)是随机的,网上很多图片上的Seq=0,其实指的是相对序列号。
为什么初始序列号是随机的,而不是固定的? 参考 初始化序列号(ISN)

Acknowledgment Number. 32 bits. 确认编号,回执号
If the ACK bit is set, this field contains the value of the next sequence number the sender of the segment is expecting to receive. Once a connection is established this is always sent.
如果设置了 ACK 确认标记(ACK=1),确认编号的值等于接收端希望收到的下一个字节流段的序列号,也等于接收端收到的由发送端发来的上一个字节流段的序列号 + 1。一旦客户端和服务端的连接建立起来,就会一直发送确认编号。

Data Offset. 4 bits.
The number of 32-bit words in the TCP header. This indicates where the data begins. The length of the TCP header is always a multiple of 32 bits.
TCP Header 中, 32-bit words 的数量,也就是本节中的 TCP 二进制位报文格式图的行数。这个字段表明了数据从什么地方开始。TCP 报头的长度始终是 32 位的倍数。

reserved. 3 bits. 保留字段
Must be cleared to zero.

ECN, Explicit Congestion Notification. 3 bits. 显式拥塞通知
Added in RFC 3168.
NCE

  • N, NS, Nonce Sum. 1 bit.
    Added in RFC 3540. This is an optional field added to ECN intended to protect against accidental or malicious concealment of marked packets from the TCP sender.
    这是添加到ECN的可选字段,用于防止来自TCP发送者的标记数据包的意外或恶意隐藏。

  • C, CWR. 1 bit.
    A Congestion Window Reduced (CWR) flag in the TCP header so that the data sender can inform the data receiver that the congestion window has been reduced.
    TCP报头中的减少拥塞窗口(CWR)标志,以便数据发送者可以通知数据接收者拥塞窗口已经减少。

  • E, ECE, ECN-Echo. 1 bit.
    An ECN-Echo (ECE) flag in the TCP header so that the data receiver can inform the data sender when a CE packet has been received.
    TCP报头中的ECN-Echo(ECE)标志,以便数据接收器可以在接收到CE数据包时通知数据发送者。
    CE: Routers can instead set the Congestion Experienced (CE) codepoint in the IP header of packets from ECN-capable transports.
    路由器可以在支持ECN的传输包的IP报头中设置拥塞经历(CE)码位。

Control Bits. 6 bits. 控制位
control-bits
U, URG. 1 bit.
Urgent pointer valid flag.

  • A, ACK. 1 bit.
    Acknowledgment number valid flag.

  • P, PSH. 1 bit.
    Push flag.

  • R, RST. 1 bit.
    Reset connection flag.
    重置连接标识

  • S, SYN. 1 bit.
    Synchronize sequence numbers flag.
    同步序列号标识

  • F, FIN. 1 bit.
    End of data flag.
    结束数据传输标识

Window. 16 bits, unsigned.
The number of data bytes beginning with the one indicated in the acknowledgment field which the sender of this segment is willing to accept.
此段的发送方愿意接受的以确认字段中指示的字节开始的数据字节数。

Checksum. 16 bits.
This is computed as the 16-bit one's complement of the one's complement sum of a pseudo header of information from the IP header, the TCP header, and the data, padded as needed with zero bytes at the end to make a multiple of two bytes. The pseudo header contains the following fields:
这是作为IP头,TCP头和数据中的伪信息头的补码之和的16位补码计算得出的,根据需要用最后的零字节填充以形成两个字节的倍数 。 伪头包含以下字段:

Urgent Pointer. 16 bits, unsigned.
If the URG bit is set, this field points to the sequence number of the last byte in a sequence of urgent data.
如果URG位置1,则此字段指向紧急数据序列中最后一个字节的序列号。

Options. 0 to 40 bytes.
Options occupy space at the end of the TCP header. All options are included in the checksum. An option may begin on any byte boundary. The TCP header must be padded with zeros to make the header length a multiple of 32 bits.
选项占据TCP报头末尾的空间。 所有选项都包含在校验和(checksum)中。选项可以在任何字节边界上开始。TCP报头必须用零填充,以使报头长度为32位的倍数。

Data. Variable length. 数据部分

十六进制图

每 1 个十六进制数位可以表示 4 位 bits 位。转换结果如下
tcp-header-0x

TCP 状态集

tcp-state
tcp-state-diagram

参考文档

www.networksorcery.com/enp/protocol/tcp.htm

扩展阅读

最详细的http协议、tcp/ip协议
TCP报文解析

原文地址:https://www.cnblogs.com/kendoziyu/p/protocol_tcp.html