TCP header format and encapsulation

12.3 TCP header and encapsulation

The following figure shows the encapsulation of TCP in an IP datagram: The
Insert picture description here
TCP header is immediately followed by the IP header or the IPv6 extension header, which is often 20 bytes (without the option field by default). With the option field, the TCP header can be up to 60 bytes long . Common option fields include maximum segment size , timestamp , window scaling, and selective ACK .

The TCP header is much more complicated than protocols such as UDP, because each segment that maintains the connection needs to support the latest status information. The TCP header format is shown below:
Insert picture description here

The standard length of the TCP header is 20 bytes unless the option field appears. The header length field gives the header size in units of 4 bytes (the minimum value is 5). Shaded field ( acknowledgment number (Acknowledagment Number), the window size (Window Size) and ECE bits and ACK bits) for data flow in the reverse direction associated with the sender of the message segment.

Field length meaning
Source port 16 bits Source port, identifying which application to send
Destination port 16 bits Destination port, identifying which application to receive
serial number 32 bits Sequence number field. Each byte in the data stream transmitted in the TCP link is coded with a sequence number. The value of the sequence number field refers to the sequence number of the first byte of the data sent in this segment.
Confirmation Number 32 bits The confirmation number is the sequence number of the first byte of the data expected to receive the next segment of the other party, that is, the sequence number of the data byte that was successfully received last time plus 1. Only the ACK flag is 1, this field is valid.
Header length 4 bits The length of the header indicates how far the data start of the TCP segment is from the start of the TCP segment, with 32 bits (4 bytes) as the calculation unit. There is a header of 60 bytes at most. If there is no option field, it is normally 20 bytes.
Reserved bit 4 bits 0 is required
CWR 1 bit Congestion window reduction (the sender reduces its sending rate)
ECE 1 bit ECN echo (the sender received an earlier congestion announcement)
URG 1 bit The effective pointer of the emergency pointer. It tells the system that there is urgent data in this segment, which should be transmitted as soon as possible (equivalent to high priority data). Rarely used
ACK 1 bit Confirm that the serial number is valid. The confirmation number field is only valid when ACK = 1. When ACK = 0, the confirmation number is invalid.
PA 1 bit The identification receiver should hand over this segment to the application layer as soon as possible. Upon receiving the TCP segment with PSH = 1, the receiving application process should be delivered as soon as possible, instead of waiting for the entire buffer to fill up before delivering it upwards.
RST 1 bit Rebuild the connection ID. When RST = 1, it indicates that a serious error occurred in the TCP connection (for example, due to host crash or other reasons), you must release the connection, and then re-establish the connection.
SYN 1 bit The synchronization serial number is used to initiate a connection. SYN = 1 means this is a connection request or connection acceptance request.
END 1 bit The originator completes the sending task ID. Used to release a connection. FIN = 1 indicates that the data on the sending end of this segment has been sent and requests to release the connection.
Window size 16 bits Window: TCP flow control. The window starts with the value indicated in the confirmation sequence number field. This value is the number of bytes the receiver is expecting to receive. The maximum window size is 65535 bytes.
Checksum 16 bits The check field, including the TCP header and TCP data, is a mandatory field, which must be calculated and stored by the sending end and verified by the receiving end. In calculating the checksum, 要在TCP报文段的前面加上12字节的伪首部.
Emergency pointer 16 bits Emergency pointer, emergency pointer is valid only when URG flag is set to 1. TCP's emergency mode is a way for the sender to send emergency data to the other end. The emergency pointer indicates how many bytes of emergency data in this segment (emergency data is placed at the top of the data in this segment).
Options 选项字段。TCP协议最初只规定了一种选项,即最长报文段长度(数据字段加上TCP首部),又称为MSS。MSS告诉对方TCP“我的缓存所能接收的报文段的数据字段的最大长度是MSS个字节”。 新的RFC规定有以下几种选型:选项表结束,无操作,最大报文段长度,窗口扩大因子,时间戳。 窗口扩大因子:3字节,其中一个字节表示偏移值S。新的窗口值等于TCP首部中的窗口位数增大到(16+S),相当于把窗口值向左移动S位后获得实际的窗口大小。 时间戳:10字节,其中最主要的字段是时间戳值(4字节)和时间戳回送应答字段(4字节)。 选项确认选项:

12.3.1 端口号

​ 每一个TCP头部都包含了源端口和目的端口。这两个值与IP头部中的源和目的IP地址唯一标识一个连接(四元组)。这个特点在TCP服务器同时与多个客户端提供服务时特别重要,服务器就是根据四元组信息(或者五元组信息)来确定客户端的。

12.3.2 序列号

用于判断接收方是否重复接收到某一报文以及报文的乱序重组功能

​ 该字段标识了TCP发送端到TCP接收端的数据流中的一个字节,该字节代表着包含该序列号报文段的数据中的第一个字节。如果我们考虑在两个应用程序之间的一个方向上流动数据流,TCP给每一个字节赋予一个序列号。这个序列号是一个32位的无符号整数,到达2^32 -1 后再循环从0开始。因为每一个没交换的字节都被编号,确认号字段(ACK位置1,确认号字段生效)包含的值是数据的接收方(即ACK的发送方)期待接收到的下一个序列号。即最后被成功接收的数据字节序号加1。这个字段之后再ACK位被启动的情况才有效。

  • 当建立一个连接时,从客户端发送至服务器的第一个报文段中的SYN位字段被启用。这样的报文段简称为SYN;并且序列号字段包含了在这个方向上要是用的第一个序列号后续的序列号和返回的ACK都在这个方向上

    • 注意:这个初始的序列号不是0或1,而是随机生成的一个数值,成为初始序列号(Inital Sequence Number, 简称ISN), ISN不是0/1,是为了提高安全性,防止被黑客攻击利用。

    • 客户端发送的第一个报文时,第一个字节的序列号为ISN+1, 这意味着SYN报文消耗一个序列号,而消耗一个序列号意味着可以使用重传机制进行可靠传输。因此SYN、以及FIN报文是可以可靠传输的,而ACK报文不消耗序列号,因此无法保证ACK报文的可靠传输

  • TCP可以被描述为“一种带积累正向确认的滑动窗口协议”。ACK确认号字段被用于构建知名在接收方已经顺序收到的最大字节(确认号 -1 = 成功接收的字节)。然而现代的TCP有一个选择确认(Selective Acknowledgment, SACK)选项,可以允许接收方告诉发送方它正确的接收到的次序杂乱的数据。 当它与一个具有选择重发(selective repeat)能力的TCP进行通信时,可以显著的提高通讯效率。

12.3.3 头部长度

​ 头部长度用来指出TCP头部的长度,以32比特为单位,由于存在选项字段,因此它是必须的。它的长度为4比特,因此TCP头部的长度限定为20~60字节(15*4=60),如果不带选项字段,即标准头部长度为20字节。

12.3.4 相关控制位

​ 当前,为TCP头部定义了8位的字段,尽管有些老的实现只能理解最后的6位。

  • CWR : 拥塞窗口减(发送方减低它的发送速率)
  • ECE : ECN回显(发送方接收到了一个更早的拥塞通告)
  • URG : 紧急(紧急指针字段有效,很少被用到)
  • ACK : 确认 (确认号字段有效,连接建立后一般都处于启用状态)
  • PSH : 推送 (接收方应尽快给应用程序发送这个数据,目前没有可靠地实现或者用到)
  • RST : 重置连接 (当前连接关闭,重新建立新的连接)
  • SYN : 用于初始化一个连接的同步序列号
  • FIN : 该报文的发送方已经没有数据要发送,请求对方关闭当前连接。

12.3.5 窗口大小

The flow control of TCP is controlled by the size of the announcement window . The window size refers to the number of bytes, specified from the confirmation number, which is calculated from the data that the receiver wants to receive. It is a 16-bit field, so the window size of TCP is limited to a maximum of 65535 bytes, thereby limiting TCP throughput performance. This value can be modified by means of window scaling .

12.3.6 Checksum

The TCP checksum field covers the TCP header and TCP data, and also includes a pseudo-header. This field is mandatory and must be calculated and saved by the sender and verified by the receiver. It is the same as calculating the checksum of UDP.

12.3.7 Option fields

Options field most important thing is the MSS field (also known as maximum segment size ). Each endpoint of the connection generally specifies the value of MSS on its first SYN message, which is used to indicate the largest segment of the message it wishes to receive.

81 original articles published · Liked 69 · Visitors 50,000+

Guess you like

Origin blog.csdn.net/s2603898260/article/details/105547805